Autogenerate all of the wire callback mocks

This makes it less manual code and less error prone to
add new callbacks to the wire.

Bug: dawn:384
Change-Id: I8547af2dba8289d1badd41e53dd732c776fb5d06
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/35600
Reviewed-by: Jiawei Shao <jiawei.shao@intel.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Austin Eng <enga@chromium.org>
This commit is contained in:
Austin Eng
2020-12-17 17:59:37 +00:00
committed by Commit Bot service account
parent 5ca12a825c
commit 8d38c0164c
13 changed files with 286 additions and 388 deletions

View File

@@ -18,7 +18,7 @@ using namespace testing;
namespace {
{% for type in by_category["object"] %}
{% for method in c_methods(type) if len(method.arguments) < 10 %}
{% for method in c_methods(type) %}
{{as_cType(method.return_type.name)}} Forward{{as_MethodSuffix(type.name, method.name)}}(
{{-as_cType(type.name)}} self
{%- for arg in method.arguments -%}
@@ -44,124 +44,52 @@ void ProcTableAsClass::GetProcTableAndDevice(DawnProcTable* table, WGPUDevice* d
*device = GetNewDevice();
{% for type in by_category["object"] %}
{% for method in c_methods(type) if len(method.arguments) < 10 %}
{% for method in c_methods(type) %}
table->{{as_varName(type.name, method.name)}} = reinterpret_cast<{{as_cProc(type.name, method.name)}}>(Forward{{as_MethodSuffix(type.name, method.name)}});
{% endfor %}
{% endfor %}
}
void ProcTableAsClass::DeviceSetUncapturedErrorCallback(WGPUDevice self,
WGPUErrorCallback callback,
void* userdata) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
object->deviceErrorCallback = callback;
object->userdata = userdata;
{% for type in by_category["object"] %}
{% for method in type.methods if has_callback_arguments(method) %}
{% set Suffix = as_MethodSuffix(type.name, method.name) %}
OnDeviceSetUncapturedErrorCallback(self, callback, userdata);
}
{{as_cType(method.return_type.name)}} ProcTableAsClass::{{Suffix}}(
{{-as_cType(type.name)}} {{as_varName(type.name)}}
{%- for arg in method.arguments -%}
, {{as_annotated_cType(arg)}}
{%- endfor -%}
) {
ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>({{as_varName(type.name)}});
{% for callback_arg in method.arguments if callback_arg.type.category == 'callback' %}
object->m{{as_MethodSuffix(type.name, method.name)}}Callback = {{as_varName(callback_arg.name)}};
{% endfor %}
object->userdata = userdata;
return On{{as_MethodSuffix(type.name, method.name)}}(
{{-as_varName(type.name)}}
{%- for arg in method.arguments -%}
, {{as_varName(arg.name)}}
{%- endfor -%}
);
}
void ProcTableAsClass::DeviceSetDeviceLostCallback(WGPUDevice self,
WGPUDeviceLostCallback callback,
void* userdata) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
object->deviceLostCallback = callback;
object->userdata = userdata;
OnDeviceSetDeviceLostCallback(self, callback, userdata);
}
bool ProcTableAsClass::DevicePopErrorScope(WGPUDevice self,
WGPUErrorCallback callback,
void* userdata) {
return OnDevicePopErrorScopeCallback(self, callback, userdata);
}
void ProcTableAsClass::BufferMapAsync(WGPUBuffer self,
WGPUMapModeFlags mode,
size_t offset,
size_t size,
WGPUBufferMapCallback callback,
void* userdata) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
object->mapAsyncCallback = callback;
object->userdata = userdata;
OnBufferMapAsyncCallback(self, callback, userdata);
}
void ProcTableAsClass::FenceOnCompletion(WGPUFence self,
uint64_t value,
WGPUFenceOnCompletionCallback callback,
void* userdata) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
object->fenceOnCompletionCallback = callback;
object->userdata = userdata;
OnFenceOnCompletionCallback(self, value, callback, userdata);
}
void ProcTableAsClass::DeviceCreateReadyComputePipeline(
WGPUDevice self,
WGPUComputePipelineDescriptor const * descriptor,
WGPUCreateReadyComputePipelineCallback callback,
void* userdata) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
object->createReadyComputePipelineCallback = callback;
object->userdata = userdata;
OnDeviceCreateReadyComputePipelineCallback(self, descriptor, callback, userdata);
}
void ProcTableAsClass::DeviceCreateReadyRenderPipeline(
WGPUDevice self,
WGPURenderPipelineDescriptor const * descriptor,
WGPUCreateReadyRenderPipelineCallback callback,
void* userdata) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
object->createReadyRenderPipelineCallback = callback;
object->userdata = userdata;
OnDeviceCreateReadyRenderPipelineCallback(self, descriptor, callback, userdata);
}
void ProcTableAsClass::CallDeviceErrorCallback(WGPUDevice device,
WGPUErrorType type,
const char* message) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(device);
object->deviceErrorCallback(type, message, object->userdata);
}
void ProcTableAsClass::CallDeviceLostCallback(WGPUDevice device, const char* message) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(device);
object->deviceLostCallback(message, object->userdata);
}
void ProcTableAsClass::CallMapAsyncCallback(WGPUBuffer buffer, WGPUBufferMapAsyncStatus status) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(buffer);
object->mapAsyncCallback(status, object->userdata);
}
void ProcTableAsClass::CallFenceOnCompletionCallback(WGPUFence fence,
WGPUFenceCompletionStatus status) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(fence);
object->fenceOnCompletionCallback(status, object->userdata);
}
void ProcTableAsClass::CallDeviceCreateReadyComputePipelineCallback(WGPUDevice device,
WGPUCreateReadyPipelineStatus status,
WGPUComputePipeline pipeline,
const char* message) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(device);
object->createReadyComputePipelineCallback(status, pipeline, message, object->userdata);
}
void ProcTableAsClass::CallDeviceCreateReadyRenderPipelineCallback(WGPUDevice device,
WGPUCreateReadyPipelineStatus status,
WGPURenderPipeline pipeline,
const char* message) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(device);
object->createReadyRenderPipelineCallback(status, pipeline, message, object->userdata);
}
{% for callback_arg in method.arguments if callback_arg.type.category == 'callback' %}
void ProcTableAsClass::Call{{Suffix}}Callback(
{{-as_cType(type.name)}} {{as_varName(type.name)}}
{%- for arg in callback_arg.type.arguments -%}
{%- if not loop.last -%}, {{as_annotated_cType(arg)}}{%- endif -%}
{%- endfor -%}
) {
ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>({{as_varName(type.name)}});
object->m{{Suffix}}Callback(
{%- for arg in callback_arg.type.arguments -%}
{%- if not loop.last -%}{{as_varName(arg.name)}}, {% endif -%}
{%- endfor -%}
object->userdata);
}
{% endfor %}
{% endfor %}
{% endfor %}
{% for type in by_category["object"] %}
{{as_cType(type.name)}} ProcTableAsClass::GetNew{{type.name.CamelCase()}}() {

View File

@@ -39,7 +39,7 @@ class ProcTableAsClass {
{% endfor %}
{% for type in by_category["object"] %}
{% for method in type.methods if len(method.arguments) < 10 and not has_callback_arguments(method) %}
{% for method in type.methods if not has_callback_arguments(method) %}
virtual {{as_cType(method.return_type.name)}} {{as_MethodSuffix(type.name, method.name)}}(
{{-as_cType(type.name)}} {{as_varName(type.name)}}
{%- for arg in method.arguments -%}
@@ -47,87 +47,49 @@ class ProcTableAsClass {
{%- endfor -%}
) = 0;
{% endfor %}
virtual void {{as_MethodSuffix(type.name, Name("reference"))}}({{as_cType(type.name)}} self) = 0;
virtual void {{as_MethodSuffix(type.name, Name("release"))}}({{as_cType(type.name)}} self) = 0;
{% for method in type.methods if has_callback_arguments(method) %}
{% set Suffix = as_MethodSuffix(type.name, method.name) %}
//* Stores callback and userdata and calls the On* method.
{{as_cType(method.return_type.name)}} {{Suffix}}(
{{-as_cType(type.name)}} {{as_varName(type.name)}}
{%- for arg in method.arguments -%}
, {{as_annotated_cType(arg)}}
{%- endfor -%}
);
//* The virtual function to call after saving the callback and userdata in the proc.
//* This function can be mocked.
virtual {{as_cType(method.return_type.name)}} On{{Suffix}}(
{{-as_cType(type.name)}} {{as_varName(type.name)}}
{%- for arg in method.arguments -%}
, {{as_annotated_cType(arg)}}
{%- endfor -%}
) = 0;
//* Calls the stored callback.
{% for callback_arg in method.arguments if callback_arg.type.category == 'callback' %}
void Call{{as_MethodSuffix(type.name, method.name)}}Callback(
{{-as_cType(type.name)}} {{as_varName(type.name)}}
{%- for arg in callback_arg.type.arguments -%}
{%- if not loop.last -%}, {{as_annotated_cType(arg)}}{%- endif -%}
{%- endfor -%}
);
{% endfor %}
{% endfor %}
{% endfor %}
// Stores callback and userdata and calls the On* methods
void DeviceCreateReadyComputePipeline(WGPUDevice self,
WGPUComputePipelineDescriptor const * descriptor,
WGPUCreateReadyComputePipelineCallback callback,
void* userdata);
void DeviceCreateReadyRenderPipeline(WGPUDevice self,
WGPURenderPipelineDescriptor const * descriptor,
WGPUCreateReadyRenderPipelineCallback callback,
void* userdata);
void DeviceSetUncapturedErrorCallback(WGPUDevice self,
WGPUErrorCallback callback,
void* userdata);
void DeviceSetDeviceLostCallback(WGPUDevice self,
WGPUDeviceLostCallback callback,
void* userdata);
bool DevicePopErrorScope(WGPUDevice self, WGPUErrorCallback callback, void* userdata);
void BufferMapAsync(WGPUBuffer self,
WGPUMapModeFlags mode,
size_t offset,
size_t size,
WGPUBufferMapCallback callback,
void* userdata);
void FenceOnCompletion(WGPUFence self,
uint64_t value,
WGPUFenceOnCompletionCallback callback,
void* userdata);
// Special cased mockable methods
virtual void OnDeviceCreateReadyComputePipelineCallback(
WGPUDevice device,
WGPUComputePipelineDescriptor const * descriptor,
WGPUCreateReadyComputePipelineCallback callback,
void* userdata) = 0;
virtual void OnDeviceCreateReadyRenderPipelineCallback(
WGPUDevice device,
WGPURenderPipelineDescriptor const * descriptor,
WGPUCreateReadyRenderPipelineCallback callback,
void* userdata) = 0;
virtual void OnDeviceSetUncapturedErrorCallback(WGPUDevice device,
WGPUErrorCallback callback,
void* userdata) = 0;
virtual void OnDeviceSetDeviceLostCallback(WGPUDevice device,
WGPUDeviceLostCallback callback,
void* userdata) = 0;
virtual bool OnDevicePopErrorScopeCallback(WGPUDevice device,
WGPUErrorCallback callback,
void* userdata) = 0;
virtual void OnBufferMapAsyncCallback(WGPUBuffer buffer,
WGPUBufferMapCallback callback,
void* userdata) = 0;
virtual void OnFenceOnCompletionCallback(WGPUFence fence,
uint64_t value,
WGPUFenceOnCompletionCallback callback,
void* userdata) = 0;
// Calls the stored callbacks
void CallDeviceCreateReadyComputePipelineCallback(WGPUDevice device,
WGPUCreateReadyPipelineStatus status,
WGPUComputePipeline pipeline,
const char* message);
void CallDeviceCreateReadyRenderPipelineCallback(WGPUDevice device,
WGPUCreateReadyPipelineStatus status,
WGPURenderPipeline pipeline,
const char* message);
void CallDeviceErrorCallback(WGPUDevice device, WGPUErrorType type, const char* message);
void CallDeviceLostCallback(WGPUDevice device, const char* message);
void CallMapAsyncCallback(WGPUBuffer buffer, WGPUBufferMapAsyncStatus status);
void CallFenceOnCompletionCallback(WGPUFence fence, WGPUFenceCompletionStatus status);
struct Object {
ProcTableAsClass* procs = nullptr;
WGPUErrorCallback deviceErrorCallback = nullptr;
WGPUCreateReadyComputePipelineCallback createReadyComputePipelineCallback = nullptr;
WGPUCreateReadyRenderPipelineCallback createReadyRenderPipelineCallback = nullptr;
WGPUDeviceLostCallback deviceLostCallback = nullptr;
WGPUBufferMapCallback mapAsyncCallback = nullptr;
WGPUFenceOnCompletionCallback fenceOnCompletionCallback = nullptr;
{% for type in by_category["object"] %}
{% for method in type.methods if has_callback_arguments(method) %}
{% for callback_arg in method.arguments if callback_arg.type.category == 'callback' %}
{{as_cType(callback_arg.type.name)}} m{{as_MethodSuffix(type.name, method.name)}}Callback = nullptr;
{% endfor %}
{% endfor %}
{% endfor %}
void* userdata = 0;
};
@@ -144,7 +106,7 @@ class MockProcTable : public ProcTableAsClass {
void IgnoreAllReleaseCalls();
{% for type in by_category["object"] %}
{% for method in type.methods if len(method.arguments) < 10 and not has_callback_arguments(method) %}
{% for method in type.methods if not has_callback_arguments(method) %}
MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "}}
{{-as_MethodSuffix(type.name, method.name)}}, (
{{-as_cType(type.name)}} {{as_varName(type.name)}}
@@ -156,28 +118,17 @@ class MockProcTable : public ProcTableAsClass {
MOCK_METHOD(void, {{as_MethodSuffix(type.name, Name("reference"))}}, ({{as_cType(type.name)}} self), (override));
MOCK_METHOD(void, {{as_MethodSuffix(type.name, Name("release"))}}, ({{as_cType(type.name)}} self), (override));
{% endfor %}
MOCK_METHOD(void,
OnDeviceCreateReadyComputePipelineCallback,
(WGPUDevice device, WGPUComputePipelineDescriptor const * descriptor,
WGPUCreateReadyComputePipelineCallback callback,
void* userdata),
(override));
MOCK_METHOD(void,
OnDeviceCreateReadyRenderPipelineCallback,
(WGPUDevice device, WGPURenderPipelineDescriptor const * descriptor,
WGPUCreateReadyRenderPipelineCallback callback,
void* userdata),
(override));
MOCK_METHOD(void, OnDeviceSetUncapturedErrorCallback, (WGPUDevice device, WGPUErrorCallback callback, void* userdata), (override));
MOCK_METHOD(void, OnDeviceSetDeviceLostCallback, (WGPUDevice device, WGPUDeviceLostCallback callback, void* userdata), (override));
MOCK_METHOD(bool, OnDevicePopErrorScopeCallback, (WGPUDevice device, WGPUErrorCallback callback, void* userdata), (override));
MOCK_METHOD(void,
OnBufferMapAsyncCallback,
(WGPUBuffer buffer, WGPUBufferMapCallback callback, void* userdata),
(override));
MOCK_METHOD(void, OnFenceOnCompletionCallback, (WGPUFence fence, uint64_t value, WGPUFenceOnCompletionCallback callback, void* userdata), (override));
{% for method in type.methods if has_callback_arguments(method) %}
MOCK_METHOD({{as_cType(method.return_type.name)}},{{" "-}}
On{{as_MethodSuffix(type.name, method.name)}}, (
{{-as_cType(type.name)}} {{as_varName(type.name)}}
{%- for arg in method.arguments -%}
, {{as_annotated_cType(arg)}}
{%- endfor -%}
), (override));
{% endfor %}
{% endfor %}
};
#endif // MOCK_WEBGPU_H