2018-07-18 09:40:26 +00:00
|
|
|
//* Copyright 2017 The Dawn Authors
|
2017-04-20 18:38:20 +00:00
|
|
|
//*
|
|
|
|
//* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
//* you may not use this file except in compliance with the License.
|
|
|
|
//* You may obtain a copy of the License at
|
|
|
|
//*
|
|
|
|
//* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//*
|
|
|
|
//* Unless required by applicable law or agreed to in writing, software
|
|
|
|
//* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
//* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//* See the License for the specific language governing permissions and
|
|
|
|
//* limitations under the License.
|
|
|
|
|
2017-07-06 18:41:13 +00:00
|
|
|
#include "wire/Wire.h"
|
|
|
|
#include "wire/WireCmd.h"
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2017-07-10 17:46:05 +00:00
|
|
|
#include "common/Assert.h"
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
2017-05-16 17:32:55 +00:00
|
|
|
#include <cstring>
|
2017-04-20 18:38:20 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-07-18 12:44:20 +00:00
|
|
|
namespace dawn { namespace wire {
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
namespace server {
|
2017-06-09 14:51:55 +00:00
|
|
|
class Server;
|
|
|
|
|
2018-06-07 16:27:56 +00:00
|
|
|
struct MapUserdata {
|
2017-06-09 14:51:55 +00:00
|
|
|
Server* server;
|
|
|
|
uint32_t bufferId;
|
|
|
|
uint32_t bufferSerial;
|
|
|
|
uint32_t requestSerial;
|
|
|
|
uint32_t size;
|
2018-06-07 16:27:56 +00:00
|
|
|
bool isWrite;
|
2017-06-09 14:51:55 +00:00
|
|
|
};
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
//* Stores what the backend knows about the type.
|
|
|
|
template<typename T>
|
|
|
|
struct ObjectDataBase {
|
2017-04-20 18:43:11 +00:00
|
|
|
//* The backend-provided handle and serial to this object.
|
2017-04-20 18:38:20 +00:00
|
|
|
T handle;
|
2017-04-20 18:43:11 +00:00
|
|
|
uint32_t serial = 0;
|
|
|
|
|
|
|
|
//* Built object ID and serial, needed to send to the client along with builder error callbacks
|
|
|
|
//* TODO(cwallez@chromium.org) only have this for builder T
|
|
|
|
uint32_t builtObjectId = 0;
|
|
|
|
uint32_t builtObjectSerial = 0;
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
//* Used by the error-propagation mechanism to know if this object is an error.
|
2017-04-20 18:42:36 +00:00
|
|
|
//* TODO(cwallez@chromium.org): this is doubling the memory usage of
|
2017-04-20 18:38:20 +00:00
|
|
|
//* std::vector<ObjectDataBase> consider making it a special marker value in handle instead.
|
|
|
|
bool valid;
|
|
|
|
//* Whether this object has been allocated, used by the KnownObjects queries
|
|
|
|
//* TODO(cwallez@chromium.org): make this an internal bit vector in KnownObjects.
|
|
|
|
bool allocated;
|
2018-06-07 16:27:56 +00:00
|
|
|
|
|
|
|
//* TODO(cwallez@chromium.org): this is only useful for buffers
|
|
|
|
void* mappedData = nullptr;
|
|
|
|
size_t mappedDataSize = 0;
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//* Keeps track of the mapping between client IDs and backend objects.
|
|
|
|
template<typename T>
|
|
|
|
class KnownObjects {
|
|
|
|
public:
|
|
|
|
using Data = ObjectDataBase<T>;
|
|
|
|
|
|
|
|
KnownObjects() {
|
|
|
|
//* Pre-allocate ID 0 to refer to the null handle.
|
|
|
|
Data nullObject;
|
|
|
|
nullObject.handle = nullptr;
|
|
|
|
nullObject.valid = true;
|
|
|
|
nullObject.allocated = true;
|
2017-11-23 21:04:26 +00:00
|
|
|
mKnown.push_back(nullObject);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//* Get a backend objects for a given client ID.
|
|
|
|
//* Returns nullptr if the ID hasn't previously been allocated.
|
2018-06-06 15:36:49 +00:00
|
|
|
const Data* Get(uint32_t id) const {
|
|
|
|
if (id >= mKnown.size()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Data* data = &mKnown[id];
|
|
|
|
|
|
|
|
if (!data->allocated) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
Data* Get(uint32_t id) {
|
2017-11-23 21:04:26 +00:00
|
|
|
if (id >= mKnown.size()) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-23 21:04:26 +00:00
|
|
|
Data* data = &mKnown[id];
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
if (!data->allocated) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//* Allocates the data for a given ID and returns it.
|
|
|
|
//* Returns nullptr if the ID is already allocated, or too far ahead.
|
|
|
|
//* Invalidates all the Data*
|
|
|
|
Data* Allocate(uint32_t id) {
|
2017-11-23 21:04:26 +00:00
|
|
|
if (id > mKnown.size()) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Data data;
|
|
|
|
data.allocated = true;
|
|
|
|
data.valid = false;
|
|
|
|
data.handle = nullptr;
|
|
|
|
|
2017-11-23 21:04:26 +00:00
|
|
|
if (id >= mKnown.size()) {
|
|
|
|
mKnown.push_back(data);
|
|
|
|
return &mKnown.back();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-23 21:04:26 +00:00
|
|
|
if (mKnown[id].allocated) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-23 21:04:26 +00:00
|
|
|
mKnown[id] = data;
|
|
|
|
return &mKnown[id];
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//* Marks an ID as deallocated
|
|
|
|
void Free(uint32_t id) {
|
2017-11-23 21:04:26 +00:00
|
|
|
ASSERT(id < mKnown.size());
|
|
|
|
mKnown[id].allocated = false;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-11-23 21:04:26 +00:00
|
|
|
std::vector<Data> mKnown;
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
void ForwardDeviceErrorToServer(const char* message, dawnCallbackUserdata userdata);
|
2017-04-20 18:42:36 +00:00
|
|
|
|
2017-04-20 18:43:11 +00:00
|
|
|
{% for type in by_category["object"] if type.is_builder%}
|
2018-07-18 13:12:52 +00:00
|
|
|
void Forward{{type.name.CamelCase()}}ToClient(dawnBuilderErrorStatus status, const char* message, dawnCallbackUserdata userdata1, dawnCallbackUserdata userdata2);
|
2017-04-20 18:43:11 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
void ForwardBufferMapReadAsync(dawnBufferMapAsyncStatus status, const void* ptr, dawnCallbackUserdata userdata);
|
|
|
|
void ForwardBufferMapWriteAsync(dawnBufferMapAsyncStatus status, void* ptr, dawnCallbackUserdata userdata);
|
2017-06-09 14:51:55 +00:00
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
// A really really simple implementation of the DeserializeAllocator. It's main feature
|
|
|
|
// is that it has some inline storage so as to avoid allocations for the majority of
|
|
|
|
// commands.
|
|
|
|
class ServerAllocator : public DeserializeAllocator {
|
|
|
|
public:
|
|
|
|
ServerAllocator() {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ServerAllocator() {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void* GetSpace(size_t size) override {
|
|
|
|
// Return space in the current buffer if possible first.
|
|
|
|
if (mRemainingSize >= size) {
|
|
|
|
char* buffer = mCurrentBuffer;
|
|
|
|
mCurrentBuffer += size;
|
|
|
|
mRemainingSize -= size;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise allocate a new buffer and try again.
|
|
|
|
size_t allocationSize = std::max(size, size_t(2048));
|
|
|
|
char* allocation = static_cast<char*>(malloc(allocationSize));
|
|
|
|
if (allocation == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAllocations.push_back(allocation);
|
|
|
|
mCurrentBuffer = allocation;
|
|
|
|
mRemainingSize = allocationSize;
|
|
|
|
return GetSpace(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() {
|
|
|
|
for (auto allocation : mAllocations) {
|
|
|
|
free(allocation);
|
|
|
|
}
|
|
|
|
mAllocations.clear();
|
|
|
|
|
|
|
|
// The initial buffer is the inline buffer so that some allocations can be skipped
|
|
|
|
mCurrentBuffer = mStaticBuffer;
|
|
|
|
mRemainingSize = sizeof(mStaticBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t mRemainingSize = 0;
|
|
|
|
char* mCurrentBuffer = nullptr;
|
|
|
|
char mStaticBuffer[2048];
|
|
|
|
std::vector<char*> mAllocations;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Server : public CommandHandler, public ObjectIdResolver {
|
2017-04-20 18:38:20 +00:00
|
|
|
public:
|
2018-07-18 13:15:07 +00:00
|
|
|
Server(dawnDevice device, const dawnProcTable& procs, CommandSerializer* serializer)
|
2017-11-23 21:04:26 +00:00
|
|
|
: mProcs(procs), mSerializer(serializer) {
|
2017-04-20 18:38:20 +00:00
|
|
|
//* The client-server knowledge is bootstrapped with device 1.
|
2017-11-23 21:04:26 +00:00
|
|
|
auto* deviceData = mKnownDevice.Allocate(1);
|
2017-04-20 18:38:20 +00:00
|
|
|
deviceData->handle = device;
|
|
|
|
deviceData->valid = true;
|
2017-04-20 18:42:36 +00:00
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
auto userdata = static_cast<dawnCallbackUserdata>(reinterpret_cast<intptr_t>(this));
|
2017-04-20 18:42:36 +00:00
|
|
|
procs.deviceSetErrorCallback(device, ForwardDeviceErrorToServer, userdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnDeviceError(const char* message) {
|
|
|
|
ReturnDeviceErrorCallbackCmd cmd;
|
|
|
|
cmd.messageStrlen = std::strlen(message);
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
auto allocCmd = static_cast<ReturnDeviceErrorCallbackCmd*>(GetCmdSpace(sizeof(cmd)));
|
2017-04-20 18:42:36 +00:00
|
|
|
*allocCmd = cmd;
|
2018-06-06 15:36:49 +00:00
|
|
|
|
|
|
|
char* messageAlloc = static_cast<char*>(GetCmdSpace(cmd.messageStrlen + 1));
|
|
|
|
strcpy(messageAlloc, message);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 18:43:11 +00:00
|
|
|
{% for type in by_category["object"] if type.is_builder%}
|
|
|
|
{% set Type = type.name.CamelCase() %}
|
2018-07-18 13:12:52 +00:00
|
|
|
void On{{Type}}Error(dawnBuilderErrorStatus status, const char* message, uint32_t id, uint32_t serial) {
|
2017-11-23 21:04:26 +00:00
|
|
|
auto* builder = mKnown{{Type}}.Get(id);
|
2017-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
if (builder == nullptr || builder->serial != serial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
if (status != DAWN_BUILDER_ERROR_STATUS_SUCCESS) {
|
2017-04-20 18:43:11 +00:00
|
|
|
builder->valid = false;
|
|
|
|
}
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
if (status != DAWN_BUILDER_ERROR_STATUS_UNKNOWN) {
|
2017-04-20 18:43:11 +00:00
|
|
|
//* Unknown is the only status that can be returned without a call to GetResult
|
|
|
|
//* so we are guaranteed to have created an object.
|
2017-07-10 17:46:05 +00:00
|
|
|
ASSERT(builder->builtObjectId != 0);
|
2017-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
Return{{Type}}ErrorCallbackCmd cmd;
|
|
|
|
cmd.builtObjectId = builder->builtObjectId;
|
|
|
|
cmd.builtObjectSerial = builder->builtObjectSerial;
|
|
|
|
cmd.status = status;
|
|
|
|
cmd.messageStrlen = std::strlen(message);
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
auto allocCmd = static_cast<Return{{Type}}ErrorCallbackCmd*>(GetCmdSpace(sizeof(cmd)));
|
2017-04-20 18:43:11 +00:00
|
|
|
*allocCmd = cmd;
|
2018-06-06 15:36:49 +00:00
|
|
|
char* messageAlloc = static_cast<char*>(GetCmdSpace(strlen(message) + 1));
|
|
|
|
strcpy(messageAlloc, message);
|
2017-04-20 18:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
void OnMapReadAsyncCallback(dawnBufferMapAsyncStatus status, const void* ptr, MapUserdata* data) {
|
2017-06-09 14:51:55 +00:00
|
|
|
ReturnBufferMapReadAsyncCallbackCmd cmd;
|
|
|
|
cmd.bufferId = data->bufferId;
|
|
|
|
cmd.bufferSerial = data->bufferSerial;
|
|
|
|
cmd.requestSerial = data->requestSerial;
|
|
|
|
cmd.status = status;
|
|
|
|
cmd.dataLength = 0;
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
auto allocCmd = static_cast<ReturnBufferMapReadAsyncCallbackCmd*>(GetCmdSpace(sizeof(cmd)));
|
2017-06-09 14:51:55 +00:00
|
|
|
*allocCmd = cmd;
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
if (status == DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS) {
|
2018-06-06 15:36:49 +00:00
|
|
|
allocCmd->dataLength = data->size;
|
|
|
|
|
|
|
|
void* dataAlloc = GetCmdSpace(data->size);
|
|
|
|
memcpy(dataAlloc, ptr, data->size);
|
2017-06-09 14:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
void OnMapWriteAsyncCallback(dawnBufferMapAsyncStatus status, void* ptr, MapUserdata* data) {
|
2018-06-07 16:27:56 +00:00
|
|
|
ReturnBufferMapWriteAsyncCallbackCmd cmd;
|
|
|
|
cmd.bufferId = data->bufferId;
|
|
|
|
cmd.bufferSerial = data->bufferSerial;
|
|
|
|
cmd.requestSerial = data->requestSerial;
|
|
|
|
cmd.status = status;
|
|
|
|
|
|
|
|
auto allocCmd = static_cast<ReturnBufferMapWriteAsyncCallbackCmd*>(GetCmdSpace(sizeof(cmd)));
|
|
|
|
*allocCmd = cmd;
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
if (status == DAWN_BUFFER_MAP_ASYNC_STATUS_SUCCESS) {
|
2018-06-07 16:27:56 +00:00
|
|
|
auto* selfData = mKnownBuffer.Get(data->bufferId);
|
|
|
|
ASSERT(selfData != nullptr);
|
|
|
|
|
|
|
|
selfData->mappedData = ptr;
|
|
|
|
selfData->mappedDataSize = data->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
const char* HandleCommands(const char* commands, size_t size) override {
|
2017-11-23 21:04:26 +00:00
|
|
|
mProcs.deviceTick(mKnownDevice.Get(1)->handle);
|
2017-06-14 17:33:45 +00:00
|
|
|
|
2018-06-21 01:54:18 +00:00
|
|
|
while (size >= sizeof(WireCmd)) {
|
2017-04-20 18:38:20 +00:00
|
|
|
WireCmd cmdId = *reinterpret_cast<const WireCmd*>(commands);
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
switch (cmdId) {
|
|
|
|
{% for type in by_category["object"] %}
|
|
|
|
{% for method in type.methods %}
|
|
|
|
{% set Suffix = as_MethodSuffix(type.name, method.name) %}
|
|
|
|
case WireCmd::{{Suffix}}:
|
|
|
|
success = Handle{{Suffix}}(&commands, &size);
|
|
|
|
break;
|
|
|
|
{% endfor %}
|
|
|
|
{% set Suffix = as_MethodSuffix(type.name, Name("destroy")) %}
|
|
|
|
case WireCmd::{{Suffix}}:
|
|
|
|
success = Handle{{Suffix}}(&commands, &size);
|
|
|
|
break;
|
|
|
|
{% endfor %}
|
2018-06-07 16:27:56 +00:00
|
|
|
case WireCmd::BufferMapAsync:
|
|
|
|
success = HandleBufferMapAsync(&commands, &size);
|
|
|
|
break;
|
|
|
|
case WireCmd::BufferUpdateMappedDataCmd:
|
|
|
|
success = HandleBufferUpdateMappedData(&commands, &size);
|
2017-06-09 14:51:55 +00:00
|
|
|
break;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-06-06 15:36:49 +00:00
|
|
|
mAllocator.Reset();
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (size != 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-07-18 13:15:07 +00:00
|
|
|
dawnProcTable mProcs;
|
2017-11-23 21:04:26 +00:00
|
|
|
CommandSerializer* mSerializer = nullptr;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
ServerAllocator mAllocator;
|
|
|
|
|
2017-04-20 18:42:36 +00:00
|
|
|
void* GetCmdSpace(size_t size) {
|
2017-11-23 21:04:26 +00:00
|
|
|
return mSerializer->GetCmdSpace(size);
|
2017-04-20 18:42:36 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
// Implementation of the ObjectIdResolver interface
|
|
|
|
{% for type in by_category["object"] %}
|
|
|
|
DeserializeResult GetFromId(ObjectId id, {{as_cType(type.name)}}* out) const override {
|
|
|
|
auto data = mKnown{{type.name.CamelCase()}}.Get(id);
|
|
|
|
if (data == nullptr) {
|
|
|
|
return DeserializeResult::FatalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = data->handle;
|
|
|
|
if (data->valid) {
|
|
|
|
return DeserializeResult::Success;
|
|
|
|
} else {
|
|
|
|
return DeserializeResult::ErrorObject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
//* The list of known IDs for each object type.
|
|
|
|
{% for type in by_category["object"] %}
|
2017-11-23 21:04:26 +00:00
|
|
|
KnownObjects<{{as_cType(type.name)}}> mKnown{{type.name.CamelCase()}};
|
2017-04-20 18:38:20 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
//* Helper function for the getting of the command data in command handlers.
|
|
|
|
//* Checks there is enough data left, updates the buffer / size and returns
|
|
|
|
//* the command (or nullptr for an error).
|
2018-06-07 16:27:56 +00:00
|
|
|
template <typename T>
|
|
|
|
static const T* GetData(const char** buffer, size_t* size, size_t count) {
|
|
|
|
// TODO(cwallez@chromium.org): Check for overflow
|
|
|
|
size_t totalSize = count * sizeof(T);
|
|
|
|
if (*size < totalSize) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-06-07 16:27:56 +00:00
|
|
|
const T* data = reinterpret_cast<const T*>(*buffer);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-06-07 16:27:56 +00:00
|
|
|
*buffer += totalSize;
|
|
|
|
*size -= totalSize;
|
2017-04-20 18:38:20 +00:00
|
|
|
|
2018-06-07 16:27:56 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
template <typename T>
|
|
|
|
static const T* GetCommand(const char** commands, size_t* size) {
|
|
|
|
return GetData<T>(commands, size, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
{% set custom_pre_handler_commands = ["BufferUnmap"] %}
|
|
|
|
|
|
|
|
bool PreHandleBufferUnmap(const BufferUnmapCmd& cmd) {
|
|
|
|
auto* selfData = mKnownBuffer.Get(cmd.selfId);
|
|
|
|
ASSERT(selfData != nullptr);
|
|
|
|
|
|
|
|
selfData->mappedData = nullptr;
|
|
|
|
|
|
|
|
return true;
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//* Implementation of the command handlers
|
|
|
|
{% for type in by_category["object"] %}
|
|
|
|
{% for method in type.methods %}
|
|
|
|
{% set Suffix = as_MethodSuffix(type.name, method.name) %}
|
|
|
|
|
|
|
|
//* The generic command handlers
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
bool Handle{{Suffix}}(const char** commands, size_t* size) {
|
|
|
|
{{Suffix}}Cmd cmd;
|
|
|
|
DeserializeResult deserializeResult = cmd.Deserialize(commands, size, &mAllocator, *this);
|
|
|
|
|
|
|
|
if (deserializeResult == DeserializeResult::FatalError) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-07 16:27:56 +00:00
|
|
|
{% if Suffix in custom_pre_handler_commands %}
|
|
|
|
if (!PreHandle{{Suffix}}(cmd)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
{% endif %}
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
//* Unpack 'self'
|
2018-06-06 15:36:49 +00:00
|
|
|
auto* selfData = mKnown{{type.name.CamelCase()}}.Get(cmd.selfId);
|
|
|
|
ASSERT(selfData != nullptr);
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
//* In all cases allocate the object data as it will be refered-to by the client.
|
2017-04-20 18:43:11 +00:00
|
|
|
{% set return_type = method.return_type %}
|
|
|
|
{% set returns = return_type.name.canonical_case() != "void" %}
|
2017-04-20 18:38:20 +00:00
|
|
|
{% if returns %}
|
|
|
|
{% set Type = method.return_type.name.CamelCase() %}
|
2018-06-06 15:36:49 +00:00
|
|
|
auto* resultData = mKnown{{Type}}.Allocate(cmd.resultId);
|
2017-04-20 18:38:20 +00:00
|
|
|
if (resultData == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-06 15:36:49 +00:00
|
|
|
resultData->serial = cmd.resultSerial;
|
2017-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
{% if type.is_builder %}
|
2018-06-06 15:36:49 +00:00
|
|
|
selfData->builtObjectId = cmd.resultId;
|
|
|
|
selfData->builtObjectSerial = cmd.resultSerial;
|
2017-04-20 18:43:11 +00:00
|
|
|
{% endif %}
|
2017-04-20 18:38:20 +00:00
|
|
|
{% endif %}
|
|
|
|
|
|
|
|
//* After the data is allocated, apply the argument error propagation mechanism
|
2018-06-06 15:36:49 +00:00
|
|
|
if (deserializeResult == DeserializeResult::ErrorObject) {
|
2017-05-18 15:44:25 +00:00
|
|
|
{% if type.is_builder %}
|
|
|
|
selfData->valid = false;
|
|
|
|
//* If we are in GetResult, fake an error callback
|
|
|
|
{% if returns %}
|
2018-07-18 13:12:52 +00:00
|
|
|
On{{type.name.CamelCase()}}Error(DAWN_BUILDER_ERROR_STATUS_ERROR, "Maybe monad", cmd.selfId, selfData->serial);
|
2017-05-18 15:44:25 +00:00
|
|
|
{% endif %}
|
|
|
|
{% endif %}
|
2017-04-20 18:38:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-09 13:34:13 +00:00
|
|
|
{% if returns %}
|
|
|
|
auto result ={{" "}}
|
|
|
|
{%- endif %}
|
2018-06-06 15:36:49 +00:00
|
|
|
mProcs.{{as_varName(type.name, method.name)}}(cmd.self
|
2017-04-20 18:38:20 +00:00
|
|
|
{%- for arg in method.arguments -%}
|
2018-06-06 15:36:49 +00:00
|
|
|
, cmd.{{as_varName(arg.name)}}
|
2017-04-20 18:38:20 +00:00
|
|
|
{%- endfor -%}
|
|
|
|
);
|
|
|
|
|
|
|
|
{% if returns %}
|
|
|
|
resultData->handle = result;
|
|
|
|
resultData->valid = result != nullptr;
|
|
|
|
|
2017-04-20 18:43:11 +00:00
|
|
|
//* builders remember the ID of the object they built so that they can send it
|
|
|
|
//* in the callback to the client.
|
|
|
|
{% if return_type.is_builder %}
|
|
|
|
if (result != nullptr) {
|
|
|
|
uint64_t userdata1 = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(this));
|
2018-06-06 15:36:49 +00:00
|
|
|
uint64_t userdata2 = (uint64_t(resultData->serial) << uint64_t(32)) + cmd.resultId;
|
2017-11-23 21:04:26 +00:00
|
|
|
mProcs.{{as_varName(return_type.name, Name("set error callback"))}}(result, Forward{{return_type.name.CamelCase()}}ToClient, userdata1, userdata2);
|
2017-04-20 18:43:11 +00:00
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
{% endif %}
|
2017-04-20 18:43:11 +00:00
|
|
|
{% endif %}
|
2017-04-20 18:38:20 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
{% endfor %}
|
|
|
|
|
|
|
|
//* Handlers for the destruction of objects: clients do the tracking of the
|
|
|
|
//* reference / release and only send destroy on refcount = 0.
|
|
|
|
{% set Suffix = as_MethodSuffix(type.name, Name("destroy")) %}
|
2018-06-06 15:36:49 +00:00
|
|
|
bool Handle{{Suffix}}(const char** commands, size_t* size) {
|
2017-04-20 18:38:20 +00:00
|
|
|
const auto* cmd = GetCommand<{{Suffix}}Cmd>(commands, size);
|
|
|
|
if (cmd == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
ObjectId objectId = cmd->objectId;
|
|
|
|
|
2017-04-20 18:38:20 +00:00
|
|
|
//* ID 0 are reserved for nullptr and cannot be destroyed.
|
2018-06-06 15:36:49 +00:00
|
|
|
if (objectId == 0) {
|
2017-04-20 18:38:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
auto* data = mKnown{{type.name.CamelCase()}}.Get(objectId);
|
2017-04-20 18:38:20 +00:00
|
|
|
if (data == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->valid) {
|
2017-11-23 21:04:26 +00:00
|
|
|
mProcs.{{as_varName(type.name, Name("release"))}}(data->handle);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
mKnown{{type.name.CamelCase()}}.Free(objectId);
|
2017-04-20 18:38:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
{% endfor %}
|
2017-06-09 14:51:55 +00:00
|
|
|
|
2018-06-07 16:27:56 +00:00
|
|
|
bool HandleBufferMapAsync(const char** commands, size_t* size) {
|
2017-06-09 14:51:55 +00:00
|
|
|
//* These requests are just forwarded to the buffer, with userdata containing what the client
|
|
|
|
//* will require in the return command.
|
2018-06-07 16:27:56 +00:00
|
|
|
const auto* cmd = GetCommand<BufferMapAsyncCmd>(commands, size);
|
2017-06-09 14:51:55 +00:00
|
|
|
if (cmd == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-06 15:36:49 +00:00
|
|
|
ObjectId bufferId = cmd->bufferId;
|
|
|
|
uint32_t requestSerial = cmd->requestSerial;
|
|
|
|
uint32_t requestSize = cmd->size;
|
|
|
|
uint32_t requestStart = cmd->start;
|
2018-06-07 16:27:56 +00:00
|
|
|
bool isWrite = cmd->isWrite;
|
2018-06-06 15:36:49 +00:00
|
|
|
|
|
|
|
auto* buffer = mKnownBuffer.Get(bufferId);
|
2017-06-09 14:51:55 +00:00
|
|
|
if (buffer == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-07 16:27:56 +00:00
|
|
|
auto* data = new MapUserdata;
|
2017-06-09 14:51:55 +00:00
|
|
|
data->server = this;
|
2018-06-06 15:36:49 +00:00
|
|
|
data->bufferId = bufferId;
|
2017-06-09 14:51:55 +00:00
|
|
|
data->bufferSerial = buffer->serial;
|
2018-06-06 15:36:49 +00:00
|
|
|
data->requestSerial = requestSerial;
|
|
|
|
data->size = requestSize;
|
2018-06-07 16:27:56 +00:00
|
|
|
data->isWrite = isWrite;
|
2017-06-09 14:51:55 +00:00
|
|
|
|
|
|
|
auto userdata = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(data));
|
|
|
|
|
|
|
|
if (!buffer->valid) {
|
|
|
|
//* Fake the buffer returning a failure, data will be freed in this call.
|
2018-06-07 16:27:56 +00:00
|
|
|
if (isWrite) {
|
2018-07-18 13:12:52 +00:00
|
|
|
ForwardBufferMapWriteAsync(DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, nullptr, userdata);
|
2018-06-07 16:27:56 +00:00
|
|
|
} else {
|
2018-07-18 13:12:52 +00:00
|
|
|
ForwardBufferMapReadAsync(DAWN_BUFFER_MAP_ASYNC_STATUS_ERROR, nullptr, userdata);
|
2018-06-07 16:27:56 +00:00
|
|
|
}
|
2017-06-09 14:51:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-07 16:27:56 +00:00
|
|
|
if (isWrite) {
|
|
|
|
mProcs.bufferMapWriteAsync(buffer->handle, requestStart, requestSize, ForwardBufferMapWriteAsync, userdata);
|
|
|
|
} else {
|
|
|
|
mProcs.bufferMapReadAsync(buffer->handle, requestStart, requestSize, ForwardBufferMapReadAsync, userdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HandleBufferUpdateMappedData(const char** commands, size_t* size) {
|
|
|
|
const auto* cmd = GetCommand<BufferUpdateMappedDataCmd>(commands, size);
|
|
|
|
if (cmd == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectId bufferId = cmd->bufferId;
|
|
|
|
size_t dataLength = cmd->dataLength;
|
|
|
|
|
|
|
|
auto* buffer = mKnownBuffer.Get(bufferId);
|
|
|
|
if (buffer == nullptr || !buffer->valid || buffer->mappedData == nullptr ||
|
|
|
|
buffer->mappedDataSize != dataLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* data = GetData<char>(commands, size, dataLength);
|
|
|
|
if (data == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer->mappedData, data, dataLength);
|
2017-06-09 14:51:55 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
};
|
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
void ForwardDeviceErrorToServer(const char* message, dawnCallbackUserdata userdata) {
|
2017-04-20 18:42:36 +00:00
|
|
|
auto server = reinterpret_cast<Server*>(static_cast<intptr_t>(userdata));
|
|
|
|
server->OnDeviceError(message);
|
|
|
|
}
|
2017-04-20 18:43:11 +00:00
|
|
|
|
|
|
|
{% for type in by_category["object"] if type.is_builder%}
|
2018-07-18 13:12:52 +00:00
|
|
|
void Forward{{type.name.CamelCase()}}ToClient(dawnBuilderErrorStatus status, const char* message, dawnCallbackUserdata userdata1, dawnCallbackUserdata userdata2) {
|
2017-06-09 14:51:55 +00:00
|
|
|
auto server = reinterpret_cast<Server*>(static_cast<uintptr_t>(userdata1));
|
2017-04-20 18:43:11 +00:00
|
|
|
uint32_t id = userdata2 & 0xFFFFFFFFu;
|
|
|
|
uint32_t serial = userdata2 >> uint64_t(32);
|
|
|
|
server->On{{type.name.CamelCase()}}Error(status, message, id, serial);
|
|
|
|
}
|
|
|
|
{% endfor %}
|
2017-06-09 14:51:55 +00:00
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
void ForwardBufferMapReadAsync(dawnBufferMapAsyncStatus status, const void* ptr, dawnCallbackUserdata userdata) {
|
2018-06-07 16:27:56 +00:00
|
|
|
auto data = reinterpret_cast<MapUserdata*>(static_cast<uintptr_t>(userdata));
|
2017-06-09 14:51:55 +00:00
|
|
|
data->server->OnMapReadAsyncCallback(status, ptr, data);
|
|
|
|
}
|
2018-06-07 16:27:56 +00:00
|
|
|
|
2018-07-18 13:12:52 +00:00
|
|
|
void ForwardBufferMapWriteAsync(dawnBufferMapAsyncStatus status, void* ptr, dawnCallbackUserdata userdata) {
|
2018-06-07 16:27:56 +00:00
|
|
|
auto data = reinterpret_cast<MapUserdata*>(static_cast<uintptr_t>(userdata));
|
|
|
|
data->server->OnMapWriteAsyncCallback(status, ptr, data);
|
|
|
|
}
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 13:15:07 +00:00
|
|
|
CommandHandler* NewServerCommandHandler(dawnDevice device, const dawnProcTable& procs, CommandSerializer* serializer) {
|
2017-04-20 18:42:36 +00:00
|
|
|
return new server::Server(device, procs, serializer);
|
2017-04-20 18:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-18 12:44:20 +00:00
|
|
|
}} // namespace dawn::wire
|