From 00d6215be9c8287d697b7cc3c9e2f7d2b3f0a68d Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Tue, 30 Jun 2020 18:39:50 +0000 Subject: [PATCH] dawn_wire/client: Add ToAPI and FromAPI helpers. In the client code, we often need to translate between WGPUObject (the API type) to Object* (the internal client type). This added a bunch of reinterpret_casts that make the code less readable and more fragile. This CL adds FromAPI and ToAPI helpers in the autogenerated ApiObjects_autogen.h header, that convert between API and internal types in a type-safe way. Bug: dawn:445 Change-Id: Ia1bf624f0315ced496b95cb660adf88abd916d71 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/24063 Commit-Queue: Corentin Wallez Reviewed-by: Stephen White Reviewed-by: Austin Eng --- .../templates/dawn_wire/client/ApiObjects.h | 22 +++++++++++++++---- src/dawn_wire/client/Buffer.cpp | 18 +++++++-------- src/dawn_wire/client/Client.cpp | 4 ++-- src/dawn_wire/client/Device.cpp | 10 ++++----- src/dawn_wire/client/Queue.cpp | 10 ++++----- 5 files changed, 38 insertions(+), 26 deletions(-) diff --git a/generator/templates/dawn_wire/client/ApiObjects.h b/generator/templates/dawn_wire/client/ApiObjects.h index 5c2ae3f613..288c7004de 100644 --- a/generator/templates/dawn_wire/client/ApiObjects.h +++ b/generator/templates/dawn_wire/client/ApiObjects.h @@ -16,10 +16,24 @@ #define DAWNWIRE_CLIENT_APIOBJECTS_AUTOGEN_H_ namespace dawn_wire { namespace client { - {% for type in by_category["object"] if not type.name.CamelCase() in client_special_objects %} - struct {{type.name.CamelCase()}} : ObjectBase { - using ObjectBase::ObjectBase; - }; + + {% for type in by_category["object"] %} + {% set Type = type.name.CamelCase() %} + {% if type.name.CamelCase() in client_special_objects %} + class {{Type}}; + {% else %} + struct {{type.name.CamelCase()}} : ObjectBase { + using ObjectBase::ObjectBase; + }; + {% endif %} + + inline {{Type}}* FromAPI(WGPU{{Type}} obj) { + return reinterpret_cast<{{Type}}*>(obj); + } + inline WGPU{{Type}} ToAPI({{Type}}* obj) { + return reinterpret_cast(obj); + } + {% endfor %} }} // namespace dawn_wire::client diff --git a/src/dawn_wire/client/Buffer.cpp b/src/dawn_wire/client/Buffer.cpp index 0b4889e130..993b2b2e9a 100644 --- a/src/dawn_wire/client/Buffer.cpp +++ b/src/dawn_wire/client/Buffer.cpp @@ -47,7 +47,6 @@ namespace dawn_wire { namespace client { // static WGPUBuffer Buffer::Create(Device* device_, const WGPUBufferDescriptor* descriptor) { - WGPUDevice cDevice = reinterpret_cast(device_); Client* wireClient = device_->GetClient(); if ((descriptor->usage & (WGPUBufferUsage_MapRead | WGPUBufferUsage_MapWrite)) != 0 && @@ -63,19 +62,18 @@ namespace dawn_wire { namespace client { buffer->mSize = descriptor->size; DeviceCreateBufferCmd cmd; - cmd.self = cDevice; + cmd.self = ToAPI(device_); cmd.descriptor = descriptor; cmd.result = ObjectHandle{buffer->id, bufferObjectAndSerial->generation}; wireClient->SerializeCommand(cmd); - return reinterpret_cast(buffer); + return ToAPI(buffer); } // static WGPUCreateBufferMappedResult Buffer::CreateMapped(Device* device_, const WGPUBufferDescriptor* descriptor) { - WGPUDevice cDevice = reinterpret_cast(device_); Client* wireClient = device_->GetClient(); WGPUCreateBufferMappedResult result; @@ -119,13 +117,13 @@ namespace dawn_wire { namespace client { buffer->mWriteHandle = std::move(writeHandle); buffer->mMappedData = result.data; - result.buffer = reinterpret_cast(buffer); + result.buffer = ToAPI(buffer); // Get the serialization size of the WriteHandle. size_t handleCreateInfoLength = buffer->mWriteHandle->SerializeCreateSize(); DeviceCreateBufferMappedCmd cmd; - cmd.device = cDevice; + cmd.device = ToAPI(device_); cmd.descriptor = descriptor; cmd.result = ObjectHandle{buffer->id, bufferObjectAndSerial->generation}; cmd.handleCreateInfoLength = handleCreateInfoLength; @@ -145,11 +143,11 @@ namespace dawn_wire { namespace client { auto* allocation = device_->GetClient()->BufferAllocator().New(device_); DeviceCreateErrorBufferCmd cmd; - cmd.self = reinterpret_cast(device_); + cmd.self = ToAPI(device_); cmd.result = ObjectHandle{allocation->object->id, allocation->generation}; device_->GetClient()->SerializeCommand(cmd); - return reinterpret_cast(allocation->object.get()); + return ToAPI(allocation->object.get()); } Buffer::~Buffer() { @@ -420,7 +418,7 @@ namespace dawn_wire { namespace client { ClearMapRequests(WGPUBufferMapAsyncStatus_Unknown); BufferUnmapCmd cmd; - cmd.self = reinterpret_cast(this); + cmd.self = ToAPI(this); device->GetClient()->SerializeCommand(cmd); } @@ -432,7 +430,7 @@ namespace dawn_wire { namespace client { ClearMapRequests(WGPUBufferMapAsyncStatus_Unknown); BufferDestroyCmd cmd; - cmd.self = reinterpret_cast(this); + cmd.self = ToAPI(this); device->GetClient()->SerializeCommand(cmd); } diff --git a/src/dawn_wire/client/Client.cpp b/src/dawn_wire/client/Client.cpp index 1953347f44..ab96743a6f 100644 --- a/src/dawn_wire/client/Client.cpp +++ b/src/dawn_wire/client/Client.cpp @@ -44,11 +44,11 @@ namespace dawn_wire { namespace client { } ReservedTexture Client::ReserveTexture(WGPUDevice cDevice) { - Device* device = reinterpret_cast(cDevice); + Device* device = FromAPI(cDevice); ObjectAllocator::ObjectAndSerial* allocation = TextureAllocator().New(device); ReservedTexture result; - result.texture = reinterpret_cast(allocation->object.get()); + result.texture = ToAPI(allocation->object.get()); result.id = allocation->object->id; result.generation = allocation->generation; return result; diff --git a/src/dawn_wire/client/Device.cpp b/src/dawn_wire/client/Device.cpp index 4df6226e09..ce8aec45f4 100644 --- a/src/dawn_wire/client/Device.cpp +++ b/src/dawn_wire/client/Device.cpp @@ -31,7 +31,7 @@ namespace dawn_wire { namespace client { mDefaultQueue = allocation->object.get(); DeviceGetDefaultQueueCmd cmd; - cmd.self = reinterpret_cast(this); + cmd.self = ToAPI(this); cmd.result = ObjectHandle{allocation->object->id, allocation->generation}; mClient->SerializeCommand(cmd); @@ -85,7 +85,7 @@ namespace dawn_wire { namespace client { mErrorScopeStackSize++; DevicePushErrorScopeCmd cmd; - cmd.self = reinterpret_cast(this); + cmd.self = ToAPI(this); cmd.filter = filter; mClient->SerializeCommand(cmd); @@ -103,7 +103,7 @@ namespace dawn_wire { namespace client { mErrorScopes[serial] = {callback, userdata}; DevicePopErrorScopeCmd cmd; - cmd.device = reinterpret_cast(this); + cmd.device = ToAPI(this); cmd.requestSerial = serial; mClient->SerializeCommand(cmd); @@ -139,7 +139,7 @@ namespace dawn_wire { namespace client { void Device::InjectError(WGPUErrorType type, const char* message) { DeviceInjectErrorCmd cmd; - cmd.self = reinterpret_cast(this); + cmd.self = ToAPI(this); cmd.type = type; cmd.message = message; mClient->SerializeCommand(cmd); @@ -160,7 +160,7 @@ namespace dawn_wire { namespace client { WGPUQueue Device::GetDefaultQueue() { mDefaultQueue->refcount++; - return reinterpret_cast(mDefaultQueue); + return ToAPI(mDefaultQueue); } }} // namespace dawn_wire::client diff --git a/src/dawn_wire/client/Queue.cpp b/src/dawn_wire/client/Queue.cpp index f515f52380..902aac80ef 100644 --- a/src/dawn_wire/client/Queue.cpp +++ b/src/dawn_wire/client/Queue.cpp @@ -23,18 +23,18 @@ namespace dawn_wire { namespace client { auto* allocation = device->GetClient()->FenceAllocator().New(device); QueueCreateFenceCmd cmd; - cmd.self = reinterpret_cast(this); + cmd.self = ToAPI(this); cmd.result = ObjectHandle{allocation->object->id, allocation->generation}; cmd.descriptor = descriptor; device->GetClient()->SerializeCommand(cmd); Fence* fence = allocation->object.get(); fence->Initialize(this, descriptor); - return reinterpret_cast(fence); + return ToAPI(fence); } void Queue::Signal(WGPUFence cFence, uint64_t signalValue) { - Fence* fence = reinterpret_cast(cFence); + Fence* fence = FromAPI(cFence); if (fence->GetQueue() != this) { device->InjectError(WGPUErrorType_Validation, "Fence must be signaled on the queue on which it was created."); @@ -49,7 +49,7 @@ namespace dawn_wire { namespace client { fence->SetSignaledValue(signalValue); QueueSignalCmd cmd; - cmd.self = reinterpret_cast(this); + cmd.self = ToAPI(this); cmd.fence = cFence; cmd.signalValue = signalValue; @@ -60,7 +60,7 @@ namespace dawn_wire { namespace client { uint64_t bufferOffset, const void* data, size_t size) { - Buffer* buffer = reinterpret_cast(cBuffer); + Buffer* buffer = FromAPI(cBuffer); QueueWriteBufferInternalCmd cmd; cmd.queueId = id;