blob: 7e28170f11d39a609b79947630af0c75cdf77f23 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001//* Copyright 2017 The Dawn & Tint Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04002//*
Austin Engcc2516a2023-10-17 20:57:54 +00003//* Redistribution and use in source and binary forms, with or without
4//* modification, are permitted provided that the following conditions are met:
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04005//*
Austin Engcc2516a2023-10-17 20:57:54 +00006//* 1. Redistributions of source code must retain the above copyright notice, this
7//* list of conditions and the following disclaimer.
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04008//*
Austin Engcc2516a2023-10-17 20:57:54 +00009//* 2. Redistributions in binary form must reproduce the above copyright notice,
10//* this list of conditions and the following disclaimer in the documentation
11//* and/or other materials provided with the distribution.
12//*
13//* 3. Neither the name of the copyright holder nor the names of its
14//* contributors may be used to endorse or promote products derived from
15//* this software without specific prior written permission.
16//*
17//* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18//* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19//* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20//* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21//* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22//* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23//* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24//* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25//* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26//* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040027
fujunwei16ae3b82021-12-15 04:35:26 +000028{% set api = metadata.api.lower() %}
Austin Eng643625a2023-08-09 00:59:33 +000029#include "dawn/common/Log.h"
fujunwei16ae3b82021-12-15 04:35:26 +000030#include "mock_{{api}}.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040031
Corentin Wallezd754fb22019-03-28 10:44:41 +000032using namespace testing;
33
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040034namespace {
35 {% for type in by_category["object"] %}
Austin Eng8d38c012020-12-17 17:59:37 +000036 {% for method in c_methods(type) %}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040037 {{as_cType(method.return_type.name)}} Forward{{as_MethodSuffix(type.name, method.name)}}(
38 {{-as_cType(type.name)}} self
39 {%- for arg in method.arguments -%}
40 , {{as_annotated_cType(arg)}}
41 {%- endfor -%}
42 ) {
Corentin Wallez1b7c5e32017-05-16 22:05:51 +020043 auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
Loko Kungd46120e2024-03-01 08:51:34 +000044 return object->procs->{{as_CppMethodSuffix(type.name, method.name)}}(self
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040045 {%- for arg in method.arguments -%}
46 , {{as_varName(arg.name)}}
47 {%- endfor -%}
48 );
49 }
50 {% endfor %}
51
52 {% endfor %}
53}
54
55ProcTableAsClass::~ProcTableAsClass() {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040056}
57
fujunwei16ae3b82021-12-15 04:35:26 +000058{% set Prefix = metadata.proc_table_prefix %}
59void ProcTableAsClass::GetProcTable({{Prefix}}ProcTable* table) {
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040060 {% for type in by_category["object"] %}
Austin Eng8d38c012020-12-17 17:59:37 +000061 {% for method in c_methods(type) %}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040062 table->{{as_varName(type.name, method.name)}} = reinterpret_cast<{{as_cProc(type.name, method.name)}}>(Forward{{as_MethodSuffix(type.name, method.name)}});
63 {% endfor %}
64 {% endfor %}
Austin Eng643625a2023-08-09 00:59:33 +000065
66 {% for type in by_category["structure"] if type.has_free_members_function %}
67 table->{{as_varName(type.name, Name("free members"))}} = []({{as_cType(type.name)}} {{as_varName(type.name)}}) {
Loko Kung75674432024-02-23 02:45:44 +000068 static bool calledOnce = false;
69 if (!calledOnce) {
70 calledOnce = true;
71 dawn::WarningLog() << "No mock available for {{as_varName(type.name, Name('free members'))}}";
72 }
Austin Eng643625a2023-08-09 00:59:33 +000073 };
74 {% endfor %}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040075}
76
Loko Kungd46120e2024-03-01 08:51:34 +000077//* Generate the older Call*Callback if there is no Future call equivalent.
78//* Includes:
Loko Kungd46120e2024-03-01 08:51:34 +000079//* - setLoggingCallback
Loko Kungf2a5e572024-06-20 21:56:42 +000080{% set LegacyCallbackFunctions = ['set logging callback'] %}
Loko Kung0a552b42024-04-10 03:39:38 +000081
82//* Manually implemented mock functions due to incompatibility.
Loko Kungf2a5e572024-06-20 21:56:42 +000083{% set ManuallyMockedFunctions = ['set device lost callback', 'set uncaptured error callback'] %}
Loko Kungd46120e2024-03-01 08:51:34 +000084
Austin Eng8d38c012020-12-17 17:59:37 +000085{% for type in by_category["object"] %}
Loko Kung0a552b42024-04-10 03:39:38 +000086 {% for method in type.methods if method.name.get() not in ManuallyMockedFunctions %}
Loko Kungd46120e2024-03-01 08:51:34 +000087 {% set Suffix = as_CppMethodSuffix(type.name, method.name) %}
88 {% if has_callback_arguments(method) %}
89 {{as_cType(method.return_type.name)}} ProcTableAsClass::{{Suffix}}(
Austin Eng8d38c012020-12-17 17:59:37 +000090 {{-as_cType(type.name)}} {{as_varName(type.name)}}
Loko Kungd46120e2024-03-01 08:51:34 +000091 {%- for arg in method.arguments -%}
92 , {{as_annotated_cType(arg)}}
Austin Eng8d38c012020-12-17 17:59:37 +000093 {%- endfor -%}
94 ) {
95 ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>({{as_varName(type.name)}});
Loko Kungd46120e2024-03-01 08:51:34 +000096 {% for arg in method.arguments if arg.type.category == 'function pointer' %}
97 object->m{{Suffix + arg.name.CamelCase()}} = {{as_varName(arg.name)}};
98 object->m{{Suffix}}Userdata = userdata;
99 {% endfor %}
100
101 {% if method.name.get() == 'pop error scope' %}
102 //* Currently special casing popErrorScope since it has an old callback type.
103 On{{Suffix}}({{-as_varName(type.name)}}, {.nextInChain = nullptr, .mode = WGPUCallbackMode_AllowProcessEvents, .callback = nullptr, .oldCallback = oldCallback, .userdata = userdata});
104 {% elif method.name.get() not in LegacyCallbackFunctions %}
105 On{{Suffix}}(
106 {{-as_varName(type.name)}}
107 {%- for arg in method.arguments if arg.type.category != 'function pointer' and arg.type.name.get() != 'void *' -%}
108 , {{as_varName(arg.name)}}
109 {%- endfor -%}
110 , {.nextInChain = nullptr, .mode = WGPUCallbackMode_AllowProcessEvents
111 {%- for arg in method.arguments if arg.type.category == 'function pointer' -%}
112 , .{{as_varName(arg.name)}} = {{as_varName(arg.name)}}
113 {%- endfor -%}
114 , .userdata = userdata});
115 {% else %}
116 On{{Suffix}}(
117 {{-as_varName(type.name)}}
118 {%- for arg in method.arguments -%}
119 , {{as_varName(arg.name)}}
120 {%- endfor -%}
121 );
122 {% endif %}
Austin Eng8d38c012020-12-17 17:59:37 +0000123 }
Loko Kungd46120e2024-03-01 08:51:34 +0000124 {% elif has_callback_info(method) %}
125 {{as_cType(method.return_type.name)}} ProcTableAsClass::{{Suffix}}(
126 {{-as_cType(type.name)}} {{as_varName(type.name)}}
127 {%- for arg in method.arguments -%}
128 , {{as_annotated_cType(arg)}}
129 {%- endfor -%}
130 ) {
131 ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>({{as_varName(type.name)}});
132 {% for arg in method.arguments %}
133 {% if arg.name.get() == 'callback info' %}
134 {% for callback in types[arg.type.name.get()].members if callback.type.category == 'function pointer' %}
135 object->m{{Suffix + callback.name.CamelCase()}} = {{as_varName(arg.name)}}.{{as_varName(callback.name)}};
136 object->m{{Suffix}}Userdata = {{as_varName(arg.name)}}.userdata;
137 {% endfor %}
138 {% endif %}
139 {% endfor %}
140
141 On{{Suffix}}(
142 {{-as_varName(type.name)}}
143 {%- for arg in method.arguments -%}
144 , {{as_varName(arg.name)}}
145 {%- endfor -%}
146 );
147 return {mNextFutureID++};
148 }
Loko Kung4e635252024-05-08 04:33:44 +0000149 {% elif has_callbackInfoStruct(method) %}
150 {{as_cType(method.return_type.name)}} ProcTableAsClass::{{Suffix}}(
151 {{-as_cType(type.name)}} {{as_varName(type.name)}}
152 {%- for arg in method.arguments -%}
153 , {{as_annotated_cType(arg)}}
154 {%- endfor -%}
155 ) {
156 ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>({{as_varName(type.name)}});
157 object->m{{Suffix}}Callback = callbackInfo.callback;
158 object->m{{Suffix}}Userdata1 = callbackInfo.userdata1;
159 object->m{{Suffix}}Userdata2 = callbackInfo.userdata2;
160
161 On{{Suffix}}(
162 {{-as_varName(type.name)}}
163 {%- for arg in method.arguments -%}
164 , {{as_varName(arg.name)}}
165 {%- endfor -%}
166 );
167 return {mNextFutureID++};
168 }
169 {% set CallbackInfoType = (method.arguments|last).type %}
Loko Kungf2a5e572024-06-20 21:56:42 +0000170 {% set CallbackType = find_by_name(CallbackInfoType.members, "callback").type %}
Loko Kung4e635252024-05-08 04:33:44 +0000171 void ProcTableAsClass::Call{{Suffix}}Callback(
172 {{-as_cType(type.name)}} {{as_varName(type.name)}}
173 {%- for arg in CallbackType.arguments -%}
174 , {{as_annotated_cType(arg)}}
175 {%- endfor -%}
176 ) {
177 ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>({{as_varName(type.name)}});
178 object->m{{Suffix}}Callback(
179 {%- for arg in CallbackType.arguments -%}
180 {{as_varName(arg.name)}}{{", "}}
181 {%- endfor -%}
182 object->m{{Suffix}}Userdata1, object->m{{Suffix}}Userdata2);
183 }
Loko Kungd46120e2024-03-01 08:51:34 +0000184 {% endif %}
185 {% endfor %}
186
187 {% for method in type.methods if has_callback_info(method) or method.name.get() in LegacyCallbackFunctions %}
188 {% set Suffix = as_CppMethodSuffix(type.name, method.name) %}
189 {% for arg in method.arguments %}
190 {% if arg.name.get() == 'callback info' %}
191 {% for callback in types[arg.type.name.get()].members if callback.type.category == 'function pointer' %}
192 void ProcTableAsClass::Call{{Suffix + callback.name.CamelCase()}}(
193 {{-as_cType(type.name)}} {{as_varName(type.name)}}
194 {%- for arg in callback.type.arguments -%}
195 {%- if not loop.last -%}, {{as_annotated_cType(arg)}}{%- endif -%}
196 {%- endfor -%}
197 ) {
198 ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>({{as_varName(type.name)}});
199 object->m{{Suffix + callback.name.CamelCase()}}(
200 {%- for arg in callback.type.arguments -%}
201 {%- if not loop.last -%}{{as_varName(arg.name)}}, {% endif -%}
202 {%- endfor -%}
203 object->m{{Suffix}}Userdata);
204 }
205 {% endfor %}
206 {% elif arg.type.category == 'function pointer' %}
207 void ProcTableAsClass::Call{{Suffix + arg.name.CamelCase()}}(
208 {{-as_cType(type.name)}} {{as_varName(type.name)}}
209 {%- for arg in arg.type.arguments -%}
210 {%- if not loop.last -%}, {{as_annotated_cType(arg)}}{%- endif -%}
211 {%- endfor -%}
212 ) {
213 ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>({{as_varName(type.name)}});
214 object->m{{Suffix + arg.name.CamelCase()}}(
215 {%- for arg in arg.type.arguments -%}
216 {%- if not loop.last -%}{{as_varName(arg.name)}}, {% endif -%}
217 {%- endfor -%}
218 object->m{{Suffix}}Userdata);
219 }
220 {% endif %}
Austin Eng8d38c012020-12-17 17:59:37 +0000221 {% endfor %}
222 {% endfor %}
223{% endfor %}
Jiawei Shao03e14002020-10-21 04:37:41 +0000224
Loko Kungf2a5e572024-06-20 21:56:42 +0000225// Manually implement some callback helpers for testing.
Loko Kung0a552b42024-04-10 03:39:38 +0000226void ProcTableAsClass::DeviceSetDeviceLostCallback(WGPUDevice device,
227 WGPUDeviceLostCallback callback,
228 void* userdata) {
229 ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>(device);
Loko Kungf2a5e572024-06-20 21:56:42 +0000230 object->mDeviceLostCallback = [](WGPUDevice const*, WGPUDeviceLostReason reason,
231 char const* message, void* callback, void* userdata) {
232 if (callback == nullptr) {
233 return;
234 }
235 auto cb = reinterpret_cast<WGPUDeviceLostCallback>(callback);
236 cb(reason, message, userdata);
237 };
238 object->mDeviceLostUserdata1 = reinterpret_cast<void*>(callback);
239 object->mDeviceLostUserdata2 = userdata;
Loko Kung0a552b42024-04-10 03:39:38 +0000240
241 OnDeviceSetDeviceLostCallback(device, callback, userdata);
242}
243void ProcTableAsClass::CallDeviceSetDeviceLostCallbackCallback(WGPUDevice device,
244 WGPUDeviceLostReason reason,
245 char const* message) {
246 ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>(device);
Loko Kungf2a5e572024-06-20 21:56:42 +0000247 object->mDeviceLostCallback(&device, reason, message, object->mDeviceLostUserdata1,
248 object->mDeviceLostUserdata2);
249}
250void ProcTableAsClass::DeviceSetUncapturedErrorCallback(WGPUDevice device,
251 WGPUErrorCallback callback,
252 void* userdata) {
253 ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>(device);
254 object->mUncapturedErrorCallback = [](WGPUDevice const*, WGPUErrorType type,
255 char const* message, void* callback, void* userdata) {
256 if (callback == nullptr) {
257 return;
258 }
259 auto cb = reinterpret_cast<WGPUErrorCallback>(callback);
260 cb(type, message, userdata);
261 };
262 object->mUncapturedErrorUserdata1 = reinterpret_cast<void*>(callback);
263 object->mUncapturedErrorUserdata2 = userdata;
264
265 OnDeviceSetUncapturedErrorCallback(device, callback, userdata);
266}
267void ProcTableAsClass::CallDeviceSetUncapturedErrorCallbackCallback(WGPUDevice device,
268 WGPUErrorType type,
269 char const* message) {
270 ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>(device);
271 object->mUncapturedErrorCallback(&device, type, message, object->mUncapturedErrorUserdata1,
272 object->mUncapturedErrorUserdata2);
Loko Kung0a552b42024-04-10 03:39:38 +0000273}
274
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400275{% for type in by_category["object"] %}
276 {{as_cType(type.name)}} ProcTableAsClass::GetNew{{type.name.CamelCase()}}() {
Corentin Wallez42dbde12017-11-23 16:04:26 -0500277 mObjects.emplace_back(new Object);
278 mObjects.back()->procs = this;
279 return reinterpret_cast<{{as_cType(type.name)}}>(mObjects.back().get());
Corentin Wallezf07e3bd2017-04-20 14:38:20 -0400280 }
281{% endfor %}
Corentin Wallez82b65732018-08-22 15:37:29 +0200282
Corentin Wallez3741f732020-04-16 18:08:23 +0000283MockProcTable::MockProcTable() = default;
284
285MockProcTable::~MockProcTable() = default;
Corentin Wallezd754fb22019-03-28 10:44:41 +0000286
287void MockProcTable::IgnoreAllReleaseCalls() {
288 {% for type in by_category["object"] %}
Loko Kungd46120e2024-03-01 08:51:34 +0000289 EXPECT_CALL(*this, {{as_CppMethodSuffix(type.name, Name("release"))}}(_)).Times(AnyNumber());
Corentin Wallezd754fb22019-03-28 10:44:41 +0000290 {% endfor %}
291}