Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | //* Copyright 2017 The Dawn & Tint Authors |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 2 | //* |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | //* Redistribution and use in source and binary forms, with or without |
| 4 | //* modification, are permitted provided that the following conditions are met: |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 5 | //* |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | //* 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | //* list of conditions and the following disclaimer. |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 8 | //* |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | //* 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 Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 27 | |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 28 | {% set api = metadata.api.lower() %} |
Austin Eng | 643625a | 2023-08-09 00:59:33 +0000 | [diff] [blame] | 29 | #include "dawn/common/Log.h" |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 30 | #include "mock_{{api}}.h" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 31 | |
Corentin Wallez | d754fb2 | 2019-03-28 10:44:41 +0000 | [diff] [blame] | 32 | using namespace testing; |
| 33 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 34 | namespace { |
| 35 | {% for type in by_category["object"] %} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 36 | {% for method in c_methods(type) %} |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 37 | {{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 Wallez | 1b7c5e3 | 2017-05-16 22:05:51 +0200 | [diff] [blame] | 43 | auto object = reinterpret_cast<ProcTableAsClass::Object*>(self); |
Loko Kung | d46120e | 2024-03-01 08:51:34 +0000 | [diff] [blame] | 44 | return object->procs->{{as_CppMethodSuffix(type.name, method.name)}}(self |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 45 | {%- for arg in method.arguments -%} |
| 46 | , {{as_varName(arg.name)}} |
| 47 | {%- endfor -%} |
| 48 | ); |
| 49 | } |
| 50 | {% endfor %} |
| 51 | |
| 52 | {% endfor %} |
| 53 | } |
| 54 | |
| 55 | ProcTableAsClass::~ProcTableAsClass() { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 56 | } |
| 57 | |
fujunwei | 16ae3b8 | 2021-12-15 04:35:26 +0000 | [diff] [blame] | 58 | {% set Prefix = metadata.proc_table_prefix %} |
| 59 | void ProcTableAsClass::GetProcTable({{Prefix}}ProcTable* table) { |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 60 | {% for type in by_category["object"] %} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 61 | {% for method in c_methods(type) %} |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 62 | 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 Eng | 643625a | 2023-08-09 00:59:33 +0000 | [diff] [blame] | 65 | |
| 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 Kung | 7567443 | 2024-02-23 02:45:44 +0000 | [diff] [blame] | 68 | 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 Eng | 643625a | 2023-08-09 00:59:33 +0000 | [diff] [blame] | 73 | }; |
| 74 | {% endfor %} |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Loko Kung | d46120e | 2024-03-01 08:51:34 +0000 | [diff] [blame] | 77 | //* Generate the older Call*Callback if there is no Future call equivalent. |
| 78 | //* Includes: |
Loko Kung | d46120e | 2024-03-01 08:51:34 +0000 | [diff] [blame] | 79 | //* - setLoggingCallback |
Loko Kung | f2a5e57 | 2024-06-20 21:56:42 +0000 | [diff] [blame] | 80 | {% set LegacyCallbackFunctions = ['set logging callback'] %} |
Loko Kung | 0a552b4 | 2024-04-10 03:39:38 +0000 | [diff] [blame] | 81 | |
| 82 | //* Manually implemented mock functions due to incompatibility. |
Loko Kung | f2a5e57 | 2024-06-20 21:56:42 +0000 | [diff] [blame] | 83 | {% set ManuallyMockedFunctions = ['set device lost callback', 'set uncaptured error callback'] %} |
Loko Kung | d46120e | 2024-03-01 08:51:34 +0000 | [diff] [blame] | 84 | |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 85 | {% for type in by_category["object"] %} |
Loko Kung | 0a552b4 | 2024-04-10 03:39:38 +0000 | [diff] [blame] | 86 | {% for method in type.methods if method.name.get() not in ManuallyMockedFunctions %} |
Loko Kung | d46120e | 2024-03-01 08:51:34 +0000 | [diff] [blame] | 87 | {% set Suffix = as_CppMethodSuffix(type.name, method.name) %} |
| 88 | {% if has_callback_arguments(method) %} |
| 89 | {{as_cType(method.return_type.name)}} ProcTableAsClass::{{Suffix}}( |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 90 | {{-as_cType(type.name)}} {{as_varName(type.name)}} |
Loko Kung | d46120e | 2024-03-01 08:51:34 +0000 | [diff] [blame] | 91 | {%- for arg in method.arguments -%} |
| 92 | , {{as_annotated_cType(arg)}} |
Austin Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 93 | {%- endfor -%} |
| 94 | ) { |
| 95 | ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>({{as_varName(type.name)}}); |
Loko Kung | d46120e | 2024-03-01 08:51:34 +0000 | [diff] [blame] | 96 | {% 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 Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 123 | } |
Loko Kung | d46120e | 2024-03-01 08:51:34 +0000 | [diff] [blame] | 124 | {% 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 Kung | 4e63525 | 2024-05-08 04:33:44 +0000 | [diff] [blame] | 149 | {% 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 Kung | f2a5e57 | 2024-06-20 21:56:42 +0000 | [diff] [blame] | 170 | {% set CallbackType = find_by_name(CallbackInfoType.members, "callback").type %} |
Loko Kung | 4e63525 | 2024-05-08 04:33:44 +0000 | [diff] [blame] | 171 | 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 Kung | d46120e | 2024-03-01 08:51:34 +0000 | [diff] [blame] | 184 | {% 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 Eng | 8d38c01 | 2020-12-17 17:59:37 +0000 | [diff] [blame] | 221 | {% endfor %} |
| 222 | {% endfor %} |
| 223 | {% endfor %} |
Jiawei Shao | 03e1400 | 2020-10-21 04:37:41 +0000 | [diff] [blame] | 224 | |
Loko Kung | f2a5e57 | 2024-06-20 21:56:42 +0000 | [diff] [blame] | 225 | // Manually implement some callback helpers for testing. |
Loko Kung | 0a552b4 | 2024-04-10 03:39:38 +0000 | [diff] [blame] | 226 | void ProcTableAsClass::DeviceSetDeviceLostCallback(WGPUDevice device, |
| 227 | WGPUDeviceLostCallback callback, |
| 228 | void* userdata) { |
| 229 | ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>(device); |
Loko Kung | f2a5e57 | 2024-06-20 21:56:42 +0000 | [diff] [blame] | 230 | 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 Kung | 0a552b4 | 2024-04-10 03:39:38 +0000 | [diff] [blame] | 240 | |
| 241 | OnDeviceSetDeviceLostCallback(device, callback, userdata); |
| 242 | } |
| 243 | void ProcTableAsClass::CallDeviceSetDeviceLostCallbackCallback(WGPUDevice device, |
| 244 | WGPUDeviceLostReason reason, |
| 245 | char const* message) { |
| 246 | ProcTableAsClass::Object* object = reinterpret_cast<ProcTableAsClass::Object*>(device); |
Loko Kung | f2a5e57 | 2024-06-20 21:56:42 +0000 | [diff] [blame] | 247 | object->mDeviceLostCallback(&device, reason, message, object->mDeviceLostUserdata1, |
| 248 | object->mDeviceLostUserdata2); |
| 249 | } |
| 250 | void 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 | } |
| 267 | void 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 Kung | 0a552b4 | 2024-04-10 03:39:38 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 275 | {% for type in by_category["object"] %} |
| 276 | {{as_cType(type.name)}} ProcTableAsClass::GetNew{{type.name.CamelCase()}}() { |
Corentin Wallez | 42dbde1 | 2017-11-23 16:04:26 -0500 | [diff] [blame] | 277 | mObjects.emplace_back(new Object); |
| 278 | mObjects.back()->procs = this; |
| 279 | return reinterpret_cast<{{as_cType(type.name)}}>(mObjects.back().get()); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 280 | } |
| 281 | {% endfor %} |
Corentin Wallez | 82b6573 | 2018-08-22 15:37:29 +0200 | [diff] [blame] | 282 | |
Corentin Wallez | 3741f73 | 2020-04-16 18:08:23 +0000 | [diff] [blame] | 283 | MockProcTable::MockProcTable() = default; |
| 284 | |
| 285 | MockProcTable::~MockProcTable() = default; |
Corentin Wallez | d754fb2 | 2019-03-28 10:44:41 +0000 | [diff] [blame] | 286 | |
| 287 | void MockProcTable::IgnoreAllReleaseCalls() { |
| 288 | {% for type in by_category["object"] %} |
Loko Kung | d46120e | 2024-03-01 08:51:34 +0000 | [diff] [blame] | 289 | EXPECT_CALL(*this, {{as_CppMethodSuffix(type.name, Name("release"))}}(_)).Times(AnyNumber()); |
Corentin Wallez | d754fb2 | 2019-03-28 10:44:41 +0000 | [diff] [blame] | 290 | {% endfor %} |
| 291 | } |