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 <cwallez@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
This commit is contained in:
Corentin Wallez 2020-06-30 18:39:50 +00:00 committed by Commit Bot service account
parent 2f616dd108
commit 00d6215be9
5 changed files with 38 additions and 26 deletions

View File

@ -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 %}
{% 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<WGPU{{Type}}>(obj);
}
{% endfor %}
}} // namespace dawn_wire::client

View File

@ -47,7 +47,6 @@ namespace dawn_wire { namespace client {
// static
WGPUBuffer Buffer::Create(Device* device_, const WGPUBufferDescriptor* descriptor) {
WGPUDevice cDevice = reinterpret_cast<WGPUDevice>(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<WGPUBuffer>(buffer);
return ToAPI(buffer);
}
// static
WGPUCreateBufferMappedResult Buffer::CreateMapped(Device* device_,
const WGPUBufferDescriptor* descriptor) {
WGPUDevice cDevice = reinterpret_cast<WGPUDevice>(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<WGPUBuffer>(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<WGPUDevice>(device_);
cmd.self = ToAPI(device_);
cmd.result = ObjectHandle{allocation->object->id, allocation->generation};
device_->GetClient()->SerializeCommand(cmd);
return reinterpret_cast<WGPUBuffer>(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<WGPUBuffer>(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<WGPUBuffer>(this);
cmd.self = ToAPI(this);
device->GetClient()->SerializeCommand(cmd);
}

View File

@ -44,11 +44,11 @@ namespace dawn_wire { namespace client {
}
ReservedTexture Client::ReserveTexture(WGPUDevice cDevice) {
Device* device = reinterpret_cast<Device*>(cDevice);
Device* device = FromAPI(cDevice);
ObjectAllocator<Texture>::ObjectAndSerial* allocation = TextureAllocator().New(device);
ReservedTexture result;
result.texture = reinterpret_cast<WGPUTexture>(allocation->object.get());
result.texture = ToAPI(allocation->object.get());
result.id = allocation->object->id;
result.generation = allocation->generation;
return result;

View File

@ -31,7 +31,7 @@ namespace dawn_wire { namespace client {
mDefaultQueue = allocation->object.get();
DeviceGetDefaultQueueCmd cmd;
cmd.self = reinterpret_cast<WGPUDevice>(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<WGPUDevice>(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<WGPUDevice>(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<WGPUDevice>(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<WGPUQueue>(mDefaultQueue);
return ToAPI(mDefaultQueue);
}
}} // namespace dawn_wire::client

View File

@ -23,18 +23,18 @@ namespace dawn_wire { namespace client {
auto* allocation = device->GetClient()->FenceAllocator().New(device);
QueueCreateFenceCmd cmd;
cmd.self = reinterpret_cast<WGPUQueue>(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<WGPUFence>(fence);
return ToAPI(fence);
}
void Queue::Signal(WGPUFence cFence, uint64_t signalValue) {
Fence* fence = reinterpret_cast<Fence*>(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<WGPUQueue>(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<Buffer*>(cBuffer);
Buffer* buffer = FromAPI(cBuffer);
QueueWriteBufferInternalCmd cmd;
cmd.queueId = id;