diff --git a/generator/templates/dawn_wire/WireCmd.cpp b/generator/templates/dawn_wire/WireCmd.cpp index 46a3d310ca..a914de1424 100644 --- a/generator/templates/dawn_wire/WireCmd.cpp +++ b/generator/templates/dawn_wire/WireCmd.cpp @@ -380,7 +380,7 @@ namespace { } void {{Cmd}}::Serialize(char* buffer - {%- if command.may_have_dawn_object -%} + {%- if not is_return -%} , const ObjectIdProvider& objectIdProvider {%- endif -%} ) const { diff --git a/generator/templates/dawn_wire/WireCmd.h b/generator/templates/dawn_wire/WireCmd.h index cdc146abc0..3a87795dba 100644 --- a/generator/templates/dawn_wire/WireCmd.h +++ b/generator/templates/dawn_wire/WireCmd.h @@ -102,7 +102,7 @@ namespace dawn_wire { //* Serialize the structure and everything it points to into serializeBuffer which must be //* big enough to contain all the data (as queried from GetRequiredSize). void Serialize(char* serializeBuffer - {%- if command.may_have_dawn_object -%} + {%- if not is_return_command -%} , const ObjectIdProvider& objectIdProvider {%- endif -%} ) const; diff --git a/generator/templates/dawn_wire/client/ApiProcs.cpp b/generator/templates/dawn_wire/client/ApiProcs.cpp index f33e5eda9d..a604a7f2a5 100644 --- a/generator/templates/dawn_wire/client/ApiProcs.cpp +++ b/generator/templates/dawn_wire/client/ApiProcs.cpp @@ -196,9 +196,7 @@ namespace dawn_wire { namespace client { {% endfor %} //* Allocate space to send the command and copy the value args over. - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(device->GetClient()->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer, *device->GetClient()); + device->GetClient()->SerializeCommand(cmd); {% if method.return_type.category == "object" %} return reinterpret_cast<{{as_cType(method.return_type.name)}}>(allocation->object.get()); @@ -226,10 +224,7 @@ namespace dawn_wire { namespace client { cmd.objectType = ObjectType::{{type.name.CamelCase()}}; cmd.objectId = obj->id; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(obj->device->GetClient()->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); - + obj->device->GetClient()->SerializeCommand(cmd); obj->device->GetClient()->{{type.name.CamelCase()}}Allocator().Free(obj); } diff --git a/src/dawn_wire/client/ApiProcs.cpp b/src/dawn_wire/client/ApiProcs.cpp index 64f6f56061..358e717525 100644 --- a/src/dawn_wire/client/ApiProcs.cpp +++ b/src/dawn_wire/client/ApiProcs.cpp @@ -36,13 +36,11 @@ namespace dawn_wire { namespace client { cmd.handleCreateInfoLength = handleCreateInfoLength; cmd.handleCreateInfo = nullptr; - size_t commandSize = cmd.GetRequiredSize(); - size_t requiredSize = commandSize + handleCreateInfoLength; - char* allocatedBuffer = - static_cast(buffer->device->GetClient()->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + char* writeHandleSpace = + buffer->device->GetClient()->SerializeCommand(cmd, handleCreateInfoLength); + // Serialize the handle into the space after the command. - handle->SerializeCreate(allocatedBuffer + commandSize); + handle->SerializeCreate(writeHandleSpace); } } // namespace @@ -155,9 +153,7 @@ namespace dawn_wire { namespace client { cmd.descriptor = descriptor; cmd.result = ObjectHandle{buffer->id, bufferObjectAndSerial->generation}; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(wireClient->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer, *wireClient); + wireClient->SerializeCommand(cmd); return reinterpret_cast(buffer); } @@ -223,12 +219,11 @@ namespace dawn_wire { namespace client { cmd.handleCreateInfoLength = handleCreateInfoLength; cmd.handleCreateInfo = nullptr; - size_t commandSize = cmd.GetRequiredSize(); - size_t requiredSize = commandSize + handleCreateInfoLength; - char* allocatedBuffer = static_cast(wireClient->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer, *wireClient); + char* writeHandleSpace = + buffer->device->GetClient()->SerializeCommand(cmd, handleCreateInfoLength); + // Serialize the WriteHandle into the space after the command. - buffer->writeHandle->SerializeCreate(allocatedBuffer + commandSize); + buffer->writeHandle->SerializeCreate(writeHandleSpace); return result; } @@ -286,10 +281,7 @@ namespace dawn_wire { namespace client { cmd.count = count; cmd.data = static_cast(data); - Client* wireClient = buffer->device->GetClient(); - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(wireClient->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + buffer->device->GetClient()->SerializeCommand(cmd); } void ClientHandwrittenBufferUnmap(WGPUBuffer cBuffer) { @@ -316,14 +308,12 @@ namespace dawn_wire { namespace client { cmd.writeFlushInfoLength = writeFlushInfoLength; cmd.writeFlushInfo = nullptr; - size_t commandSize = cmd.GetRequiredSize(); - size_t requiredSize = commandSize + writeFlushInfoLength; - char* allocatedBuffer = - static_cast(buffer->device->GetClient()->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + char* writeHandleSpace = + buffer->device->GetClient()->SerializeCommand(cmd, writeFlushInfoLength); + // Serialize flush metadata into the space after the command. // This closes the handle for writing. - buffer->writeHandle->SerializeFlush(allocatedBuffer + commandSize); + buffer->writeHandle->SerializeFlush(writeHandleSpace); buffer->writeHandle = nullptr; } else if (buffer->readHandle) { @@ -333,10 +323,7 @@ namespace dawn_wire { namespace client { BufferUnmapCmd cmd; cmd.self = cBuffer; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = - static_cast(buffer->device->GetClient()->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer, *buffer->device->GetClient()); + buffer->device->GetClient()->SerializeCommand(cmd); } void ClientHandwrittenBufferDestroy(WGPUBuffer cBuffer) { @@ -349,10 +336,7 @@ namespace dawn_wire { namespace client { BufferDestroyCmd cmd; cmd.self = cBuffer; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = - static_cast(buffer->device->GetClient()->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer, *buffer->device->GetClient()); + buffer->device->GetClient()->SerializeCommand(cmd); } WGPUFence ClientHandwrittenQueueCreateFence(WGPUQueue cSelf, @@ -366,9 +350,7 @@ namespace dawn_wire { namespace client { cmd.result = ObjectHandle{allocation->object->id, allocation->generation}; cmd.descriptor = descriptor; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(device->GetClient()->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer, *device->GetClient()); + device->GetClient()->SerializeCommand(cmd); WGPUFence cFence = reinterpret_cast(allocation->object.get()); @@ -403,10 +385,7 @@ namespace dawn_wire { namespace client { cmd.fence = cFence; cmd.signalValue = signalValue; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = - static_cast(fence->device->GetClient()->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer, *fence->device->GetClient()); + queue->device->GetClient()->SerializeCommand(cmd); } void ClientHandwrittenQueueWriteBuffer(WGPUQueue cQueue, @@ -424,10 +403,7 @@ namespace dawn_wire { namespace client { cmd.data = static_cast(data); cmd.size = size; - Client* wireClient = buffer->device->GetClient(); - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(wireClient->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + queue->device->GetClient()->SerializeCommand(cmd); } void ClientDeviceReference(WGPUDevice) { diff --git a/src/dawn_wire/client/Client.cpp b/src/dawn_wire/client/Client.cpp index 9790d48af6..1953347f44 100644 --- a/src/dawn_wire/client/Client.cpp +++ b/src/dawn_wire/client/Client.cpp @@ -54,14 +54,14 @@ namespace dawn_wire { namespace client { return result; } - void* Client::GetCmdSpace(size_t size) { + char* Client::GetCmdSpace(size_t size) { if (DAWN_UNLIKELY(mIsDisconnected)) { if (size > mDummyCmdSpace.size()) { mDummyCmdSpace.resize(size); } return mDummyCmdSpace.data(); } - return mSerializer->GetCmdSpace(size); + return static_cast(mSerializer->GetCmdSpace(size)); } void Client::Disconnect() { diff --git a/src/dawn_wire/client/Client.h b/src/dawn_wire/client/Client.h index 7769e338ec..d8df86d8c9 100644 --- a/src/dawn_wire/client/Client.h +++ b/src/dawn_wire/client/Client.h @@ -42,13 +42,22 @@ namespace dawn_wire { namespace client { const volatile char* HandleCommands(const volatile char* commands, size_t size); ReservedTexture ReserveTexture(WGPUDevice device); - void* GetCmdSpace(size_t size); + template + char* SerializeCommand(const Cmd& cmd, size_t extraSize = 0) { + size_t requiredSize = cmd.GetRequiredSize(); + // TODO(cwallez@chromium.org): Check for overflows and allocation success? + char* allocatedBuffer = GetCmdSpace(requiredSize + extraSize); + cmd.Serialize(allocatedBuffer, *this); + return allocatedBuffer + requiredSize; + } void Disconnect(); private: #include "dawn_wire/client/ClientPrototypes_autogen.inc" + char* GetCmdSpace(size_t size); + Device* mDevice = nullptr; CommandSerializer* mSerializer = nullptr; WireDeserializeAllocator mAllocator; diff --git a/src/dawn_wire/client/Device.cpp b/src/dawn_wire/client/Device.cpp index c69983ae00..43361b3e62 100644 --- a/src/dawn_wire/client/Device.cpp +++ b/src/dawn_wire/client/Device.cpp @@ -34,9 +34,7 @@ namespace dawn_wire { namespace client { cmd.self = reinterpret_cast(this); cmd.result = ObjectHandle{allocation->object->id, allocation->generation}; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(mClient->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer, *mClient); + mClient->SerializeCommand(cmd); } Device::~Device() { @@ -51,9 +49,7 @@ namespace dawn_wire { namespace client { cmd.objectType = ObjectType::Queue; cmd.objectId = mDefaultQueue->id; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(mClient->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + mClient->SerializeCommand(cmd); mClient->QueueAllocator().Free(mDefaultQueue); } @@ -92,10 +88,7 @@ namespace dawn_wire { namespace client { cmd.self = reinterpret_cast(this); cmd.filter = filter; - Client* wireClient = GetClient(); - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(wireClient->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer, *wireClient); + mClient->SerializeCommand(cmd); } bool Device::RequestPopErrorScope(WGPUErrorCallback callback, void* userdata) { @@ -113,10 +106,7 @@ namespace dawn_wire { namespace client { cmd.device = reinterpret_cast(this); cmd.requestSerial = serial; - Client* wireClient = GetClient(); - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(wireClient->GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer, *wireClient); + mClient->SerializeCommand(cmd); return true; } diff --git a/src/dawn_wire/server/Server.cpp b/src/dawn_wire/server/Server.cpp index 31b52dccfa..0199aba42f 100644 --- a/src/dawn_wire/server/Server.cpp +++ b/src/dawn_wire/server/Server.cpp @@ -39,8 +39,8 @@ namespace dawn_wire { namespace server { DestroyAllObjects(mProcs); } - void* Server::GetCmdSpace(size_t size) { - return mSerializer->GetCmdSpace(size); + char* Server::GetCmdSpace(size_t size) { + return static_cast(mSerializer->GetCmdSpace(size)); } bool Server::InjectTexture(WGPUTexture texture, uint32_t id, uint32_t generation) { diff --git a/src/dawn_wire/server/Server.h b/src/dawn_wire/server/Server.h index effe69e25b..7dd0303df7 100644 --- a/src/dawn_wire/server/Server.h +++ b/src/dawn_wire/server/Server.h @@ -58,7 +58,15 @@ namespace dawn_wire { namespace server { bool InjectTexture(WGPUTexture texture, uint32_t id, uint32_t generation); private: - void* GetCmdSpace(size_t size); + template + char* SerializeCommand(const Cmd& cmd, size_t extraSize = 0) { + size_t requiredSize = cmd.GetRequiredSize(); + // TODO(cwallez@chromium.org): Check for overflows and allocation success? + char* allocatedBuffer = GetCmdSpace(requiredSize + extraSize); + cmd.Serialize(allocatedBuffer); + return allocatedBuffer + requiredSize; + } + char* GetCmdSpace(size_t size); // Forwarding callbacks static void ForwardUncapturedError(WGPUErrorType type, const char* message, void* userdata); diff --git a/src/dawn_wire/server/ServerBuffer.cpp b/src/dawn_wire/server/ServerBuffer.cpp index 84985129be..1b516f96be 100644 --- a/src/dawn_wire/server/ServerBuffer.cpp +++ b/src/dawn_wire/server/ServerBuffer.cpp @@ -252,14 +252,11 @@ namespace dawn_wire { namespace server { cmd.initialDataInfoLength = initialDataInfoLength; cmd.initialDataInfo = nullptr; - size_t commandSize = cmd.GetRequiredSize(); - size_t requiredSize = commandSize + initialDataInfoLength; - char* allocatedBuffer = static_cast(GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + char* readHandleSpace = SerializeCommand(cmd, initialDataInfoLength); if (status == WGPUBufferMapAsyncStatus_Success) { // Serialize the initialization message into the space after the command. - data->readHandle->SerializeInitialData(ptr, dataLength, allocatedBuffer + commandSize); + data->readHandle->SerializeInitialData(ptr, dataLength, readHandleSpace); // The in-flight map request returned successfully. // Move the ReadHandle so it is owned by the buffer. @@ -284,9 +281,7 @@ namespace dawn_wire { namespace server { cmd.requestSerial = data->requestSerial; cmd.status = status; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + SerializeCommand(cmd); if (status == WGPUBufferMapAsyncStatus_Success) { // The in-flight map request returned successfully. diff --git a/src/dawn_wire/server/ServerDevice.cpp b/src/dawn_wire/server/ServerDevice.cpp index 66c0d70153..64ad1bb0c5 100644 --- a/src/dawn_wire/server/ServerDevice.cpp +++ b/src/dawn_wire/server/ServerDevice.cpp @@ -31,18 +31,14 @@ namespace dawn_wire { namespace server { cmd.type = type; cmd.message = message; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + SerializeCommand(cmd); } void Server::OnDeviceLost(const char* message) { ReturnDeviceLostCallbackCmd cmd; cmd.message = message; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + SerializeCommand(cmd); } bool Server::DoDevicePopErrorScope(WGPUDevice cDevice, uint64_t requestSerial) { @@ -73,9 +69,7 @@ namespace dawn_wire { namespace server { cmd.type = type; cmd.message = message; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + SerializeCommand(cmd); } }} // namespace dawn_wire::server diff --git a/src/dawn_wire/server/ServerFence.cpp b/src/dawn_wire/server/ServerFence.cpp index de056a1d5e..1a4105c38b 100644 --- a/src/dawn_wire/server/ServerFence.cpp +++ b/src/dawn_wire/server/ServerFence.cpp @@ -35,9 +35,7 @@ namespace dawn_wire { namespace server { cmd.fence = data->fence; cmd.value = data->value; - size_t requiredSize = cmd.GetRequiredSize(); - char* allocatedBuffer = static_cast(GetCmdSpace(requiredSize)); - cmd.Serialize(allocatedBuffer); + SerializeCommand(cmd); } }} // namespace dawn_wire::server