1 /*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // WARNING: This file is generated. See ../README.md for instructions.
18
19 #include <log/log.h>
20 #include <string.h>
21
22 #include <algorithm>
23
24 #include "driver.h"
25
26 namespace vulkan {
27 namespace driver {
28
29 namespace {
30
31 // clang-format off
32
checkedCreateSwapchainKHR(VkDevice device,const VkSwapchainCreateInfoKHR * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkSwapchainKHR * pSwapchain)33 VKAPI_ATTR VkResult checkedCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) {
34 if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
35 return CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
36 } else {
37 Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkCreateSwapchainKHR not executed.");
38 return VK_SUCCESS;
39 }
40 }
41
checkedDestroySwapchainKHR(VkDevice device,VkSwapchainKHR swapchain,const VkAllocationCallbacks * pAllocator)42 VKAPI_ATTR void checkedDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator) {
43 if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
44 DestroySwapchainKHR(device, swapchain, pAllocator);
45 } else {
46 Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkDestroySwapchainKHR not executed.");
47 }
48 }
49
checkedGetSwapchainImagesKHR(VkDevice device,VkSwapchainKHR swapchain,uint32_t * pSwapchainImageCount,VkImage * pSwapchainImages)50 VKAPI_ATTR VkResult checkedGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages) {
51 if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
52 return GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
53 } else {
54 Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkGetSwapchainImagesKHR not executed.");
55 return VK_SUCCESS;
56 }
57 }
58
checkedAcquireNextImageKHR(VkDevice device,VkSwapchainKHR swapchain,uint64_t timeout,VkSemaphore semaphore,VkFence fence,uint32_t * pImageIndex)59 VKAPI_ATTR VkResult checkedAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout, VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) {
60 if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
61 return AcquireNextImageKHR(device, swapchain, timeout, semaphore, fence, pImageIndex);
62 } else {
63 Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkAcquireNextImageKHR not executed.");
64 return VK_SUCCESS;
65 }
66 }
67
checkedQueuePresentKHR(VkQueue queue,const VkPresentInfoKHR * pPresentInfo)68 VKAPI_ATTR VkResult checkedQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) {
69 if (GetData(queue).hook_extensions[ProcHook::KHR_swapchain]) {
70 return QueuePresentKHR(queue, pPresentInfo);
71 } else {
72 Logger(queue).Err(queue, "VK_KHR_swapchain not enabled. vkQueuePresentKHR not executed.");
73 return VK_SUCCESS;
74 }
75 }
76
checkedBindImageMemory2(VkDevice device,uint32_t bindInfoCount,const VkBindImageMemoryInfo * pBindInfos)77 VKAPI_ATTR VkResult checkedBindImageMemory2(VkDevice device, uint32_t bindInfoCount, const VkBindImageMemoryInfo* pBindInfos) {
78 if (GetData(device).hook_extensions[ProcHook::EXTENSION_CORE_1_1]) {
79 return BindImageMemory2(device, bindInfoCount, pBindInfos);
80 } else {
81 Logger(device).Err(device, "VK_VERSION_1_1 not enabled. vkBindImageMemory2 not executed.");
82 return VK_SUCCESS;
83 }
84 }
85
checkedBindImageMemory2KHR(VkDevice device,uint32_t bindInfoCount,const VkBindImageMemoryInfo * pBindInfos)86 VKAPI_ATTR VkResult checkedBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount, const VkBindImageMemoryInfo* pBindInfos) {
87 if (GetData(device).hook_extensions[ProcHook::KHR_bind_memory2]) {
88 return BindImageMemory2KHR(device, bindInfoCount, pBindInfos);
89 } else {
90 Logger(device).Err(device, "VK_KHR_bind_memory2 not enabled. vkBindImageMemory2KHR not executed.");
91 return VK_SUCCESS;
92 }
93 }
94
checkedGetDeviceGroupPresentCapabilitiesKHR(VkDevice device,VkDeviceGroupPresentCapabilitiesKHR * pDeviceGroupPresentCapabilities)95 VKAPI_ATTR VkResult checkedGetDeviceGroupPresentCapabilitiesKHR(VkDevice device, VkDeviceGroupPresentCapabilitiesKHR* pDeviceGroupPresentCapabilities) {
96 if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
97 return GetDeviceGroupPresentCapabilitiesKHR(device, pDeviceGroupPresentCapabilities);
98 } else {
99 Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkGetDeviceGroupPresentCapabilitiesKHR not executed.");
100 return VK_SUCCESS;
101 }
102 }
103
checkedGetDeviceGroupSurfacePresentModesKHR(VkDevice device,VkSurfaceKHR surface,VkDeviceGroupPresentModeFlagsKHR * pModes)104 VKAPI_ATTR VkResult checkedGetDeviceGroupSurfacePresentModesKHR(VkDevice device, VkSurfaceKHR surface, VkDeviceGroupPresentModeFlagsKHR* pModes) {
105 if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
106 return GetDeviceGroupSurfacePresentModesKHR(device, surface, pModes);
107 } else {
108 Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkGetDeviceGroupSurfacePresentModesKHR not executed.");
109 return VK_SUCCESS;
110 }
111 }
112
checkedAcquireNextImage2KHR(VkDevice device,const VkAcquireNextImageInfoKHR * pAcquireInfo,uint32_t * pImageIndex)113 VKAPI_ATTR VkResult checkedAcquireNextImage2KHR(VkDevice device, const VkAcquireNextImageInfoKHR* pAcquireInfo, uint32_t* pImageIndex) {
114 if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
115 return AcquireNextImage2KHR(device, pAcquireInfo, pImageIndex);
116 } else {
117 Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkAcquireNextImage2KHR not executed.");
118 return VK_SUCCESS;
119 }
120 }
121
checkedSetHdrMetadataEXT(VkDevice device,uint32_t swapchainCount,const VkSwapchainKHR * pSwapchains,const VkHdrMetadataEXT * pMetadata)122 VKAPI_ATTR void checkedSetHdrMetadataEXT(VkDevice device, uint32_t swapchainCount, const VkSwapchainKHR* pSwapchains, const VkHdrMetadataEXT* pMetadata) {
123 if (GetData(device).hook_extensions[ProcHook::EXT_hdr_metadata]) {
124 SetHdrMetadataEXT(device, swapchainCount, pSwapchains, pMetadata);
125 } else {
126 Logger(device).Err(device, "VK_EXT_hdr_metadata not enabled. vkSetHdrMetadataEXT not executed.");
127 }
128 }
129
checkedGetSwapchainStatusKHR(VkDevice device,VkSwapchainKHR swapchain)130 VKAPI_ATTR VkResult checkedGetSwapchainStatusKHR(VkDevice device, VkSwapchainKHR swapchain) {
131 if (GetData(device).hook_extensions[ProcHook::KHR_shared_presentable_image]) {
132 return GetSwapchainStatusKHR(device, swapchain);
133 } else {
134 Logger(device).Err(device, "VK_KHR_shared_presentable_image not enabled. vkGetSwapchainStatusKHR not executed.");
135 return VK_SUCCESS;
136 }
137 }
138
checkedGetRefreshCycleDurationGOOGLE(VkDevice device,VkSwapchainKHR swapchain,VkRefreshCycleDurationGOOGLE * pDisplayTimingProperties)139 VKAPI_ATTR VkResult checkedGetRefreshCycleDurationGOOGLE(VkDevice device, VkSwapchainKHR swapchain, VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
140 if (GetData(device).hook_extensions[ProcHook::GOOGLE_display_timing]) {
141 return GetRefreshCycleDurationGOOGLE(device, swapchain, pDisplayTimingProperties);
142 } else {
143 Logger(device).Err(device, "VK_GOOGLE_display_timing not enabled. vkGetRefreshCycleDurationGOOGLE not executed.");
144 return VK_SUCCESS;
145 }
146 }
147
checkedGetPastPresentationTimingGOOGLE(VkDevice device,VkSwapchainKHR swapchain,uint32_t * pPresentationTimingCount,VkPastPresentationTimingGOOGLE * pPresentationTimings)148 VKAPI_ATTR VkResult checkedGetPastPresentationTimingGOOGLE(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pPresentationTimingCount, VkPastPresentationTimingGOOGLE* pPresentationTimings) {
149 if (GetData(device).hook_extensions[ProcHook::GOOGLE_display_timing]) {
150 return GetPastPresentationTimingGOOGLE(device, swapchain, pPresentationTimingCount, pPresentationTimings);
151 } else {
152 Logger(device).Err(device, "VK_GOOGLE_display_timing not enabled. vkGetPastPresentationTimingGOOGLE not executed.");
153 return VK_SUCCESS;
154 }
155 }
156
checkedGetDeviceQueue2(VkDevice device,const VkDeviceQueueInfo2 * pQueueInfo,VkQueue * pQueue)157 VKAPI_ATTR void checkedGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2* pQueueInfo, VkQueue* pQueue) {
158 if (GetData(device).hook_extensions[ProcHook::EXTENSION_CORE_1_1]) {
159 GetDeviceQueue2(device, pQueueInfo, pQueue);
160 } else {
161 Logger(device).Err(device, "VK_VERSION_1_1 not enabled. vkGetDeviceQueue2 not executed.");
162 }
163 }
164
165 // clang-format on
166
167 const ProcHook g_proc_hooks[] = {
168 // clang-format off
169 {
170 "vkAcquireImageANDROID",
171 ProcHook::DEVICE,
172 ProcHook::ANDROID_native_buffer,
173 nullptr,
174 nullptr,
175 },
176 {
177 "vkAcquireNextImage2KHR",
178 ProcHook::DEVICE,
179 ProcHook::KHR_swapchain,
180 reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImage2KHR),
181 reinterpret_cast<PFN_vkVoidFunction>(checkedAcquireNextImage2KHR),
182 },
183 {
184 "vkAcquireNextImageKHR",
185 ProcHook::DEVICE,
186 ProcHook::KHR_swapchain,
187 reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImageKHR),
188 reinterpret_cast<PFN_vkVoidFunction>(checkedAcquireNextImageKHR),
189 },
190 {
191 "vkAllocateCommandBuffers",
192 ProcHook::DEVICE,
193 ProcHook::EXTENSION_CORE_1_0,
194 reinterpret_cast<PFN_vkVoidFunction>(AllocateCommandBuffers),
195 nullptr,
196 },
197 {
198 "vkBindImageMemory2",
199 ProcHook::DEVICE,
200 ProcHook::EXTENSION_CORE_1_1,
201 reinterpret_cast<PFN_vkVoidFunction>(BindImageMemory2),
202 reinterpret_cast<PFN_vkVoidFunction>(checkedBindImageMemory2),
203 },
204 {
205 "vkBindImageMemory2KHR",
206 ProcHook::DEVICE,
207 ProcHook::KHR_bind_memory2,
208 reinterpret_cast<PFN_vkVoidFunction>(BindImageMemory2KHR),
209 reinterpret_cast<PFN_vkVoidFunction>(checkedBindImageMemory2KHR),
210 },
211 {
212 "vkCreateAndroidSurfaceKHR",
213 ProcHook::INSTANCE,
214 ProcHook::KHR_android_surface,
215 reinterpret_cast<PFN_vkVoidFunction>(CreateAndroidSurfaceKHR),
216 nullptr,
217 },
218 {
219 "vkCreateDebugReportCallbackEXT",
220 ProcHook::INSTANCE,
221 ProcHook::EXT_debug_report,
222 reinterpret_cast<PFN_vkVoidFunction>(CreateDebugReportCallbackEXT),
223 nullptr,
224 },
225 {
226 "vkCreateDevice",
227 ProcHook::INSTANCE,
228 ProcHook::EXTENSION_CORE_1_0,
229 reinterpret_cast<PFN_vkVoidFunction>(CreateDevice),
230 nullptr,
231 },
232 {
233 "vkCreateInstance",
234 ProcHook::GLOBAL,
235 ProcHook::EXTENSION_CORE_1_0,
236 reinterpret_cast<PFN_vkVoidFunction>(CreateInstance),
237 nullptr,
238 },
239 {
240 "vkCreateSwapchainKHR",
241 ProcHook::DEVICE,
242 ProcHook::KHR_swapchain,
243 reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR),
244 reinterpret_cast<PFN_vkVoidFunction>(checkedCreateSwapchainKHR),
245 },
246 {
247 "vkDebugReportMessageEXT",
248 ProcHook::INSTANCE,
249 ProcHook::EXT_debug_report,
250 reinterpret_cast<PFN_vkVoidFunction>(DebugReportMessageEXT),
251 nullptr,
252 },
253 {
254 "vkDestroyDebugReportCallbackEXT",
255 ProcHook::INSTANCE,
256 ProcHook::EXT_debug_report,
257 reinterpret_cast<PFN_vkVoidFunction>(DestroyDebugReportCallbackEXT),
258 nullptr,
259 },
260 {
261 "vkDestroyDevice",
262 ProcHook::DEVICE,
263 ProcHook::EXTENSION_CORE_1_0,
264 reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice),
265 nullptr,
266 },
267 {
268 "vkDestroyInstance",
269 ProcHook::INSTANCE,
270 ProcHook::EXTENSION_CORE_1_0,
271 reinterpret_cast<PFN_vkVoidFunction>(DestroyInstance),
272 nullptr,
273 },
274 {
275 "vkDestroySurfaceKHR",
276 ProcHook::INSTANCE,
277 ProcHook::KHR_surface,
278 reinterpret_cast<PFN_vkVoidFunction>(DestroySurfaceKHR),
279 nullptr,
280 },
281 {
282 "vkDestroySwapchainKHR",
283 ProcHook::DEVICE,
284 ProcHook::KHR_swapchain,
285 reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR),
286 reinterpret_cast<PFN_vkVoidFunction>(checkedDestroySwapchainKHR),
287 },
288 {
289 "vkEnumerateDeviceExtensionProperties",
290 ProcHook::INSTANCE,
291 ProcHook::EXTENSION_CORE_1_0,
292 reinterpret_cast<PFN_vkVoidFunction>(EnumerateDeviceExtensionProperties),
293 nullptr,
294 },
295 {
296 "vkEnumerateInstanceExtensionProperties",
297 ProcHook::GLOBAL,
298 ProcHook::EXTENSION_CORE_1_0,
299 reinterpret_cast<PFN_vkVoidFunction>(EnumerateInstanceExtensionProperties),
300 nullptr,
301 },
302 {
303 "vkEnumeratePhysicalDeviceGroups",
304 ProcHook::INSTANCE,
305 ProcHook::EXTENSION_CORE_1_1,
306 reinterpret_cast<PFN_vkVoidFunction>(EnumeratePhysicalDeviceGroups),
307 nullptr,
308 },
309 {
310 "vkEnumeratePhysicalDevices",
311 ProcHook::INSTANCE,
312 ProcHook::EXTENSION_CORE_1_0,
313 reinterpret_cast<PFN_vkVoidFunction>(EnumeratePhysicalDevices),
314 nullptr,
315 },
316 {
317 "vkGetDeviceGroupPresentCapabilitiesKHR",
318 ProcHook::DEVICE,
319 ProcHook::KHR_swapchain,
320 reinterpret_cast<PFN_vkVoidFunction>(GetDeviceGroupPresentCapabilitiesKHR),
321 reinterpret_cast<PFN_vkVoidFunction>(checkedGetDeviceGroupPresentCapabilitiesKHR),
322 },
323 {
324 "vkGetDeviceGroupSurfacePresentModesKHR",
325 ProcHook::DEVICE,
326 ProcHook::KHR_swapchain,
327 reinterpret_cast<PFN_vkVoidFunction>(GetDeviceGroupSurfacePresentModesKHR),
328 reinterpret_cast<PFN_vkVoidFunction>(checkedGetDeviceGroupSurfacePresentModesKHR),
329 },
330 {
331 "vkGetDeviceProcAddr",
332 ProcHook::DEVICE,
333 ProcHook::EXTENSION_CORE_1_0,
334 reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr),
335 nullptr,
336 },
337 {
338 "vkGetDeviceQueue",
339 ProcHook::DEVICE,
340 ProcHook::EXTENSION_CORE_1_0,
341 reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue),
342 nullptr,
343 },
344 {
345 "vkGetDeviceQueue2",
346 ProcHook::DEVICE,
347 ProcHook::EXTENSION_CORE_1_1,
348 reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue2),
349 reinterpret_cast<PFN_vkVoidFunction>(checkedGetDeviceQueue2),
350 },
351 {
352 "vkGetInstanceProcAddr",
353 ProcHook::INSTANCE,
354 ProcHook::EXTENSION_CORE_1_0,
355 reinterpret_cast<PFN_vkVoidFunction>(GetInstanceProcAddr),
356 nullptr,
357 },
358 {
359 "vkGetPastPresentationTimingGOOGLE",
360 ProcHook::DEVICE,
361 ProcHook::GOOGLE_display_timing,
362 reinterpret_cast<PFN_vkVoidFunction>(GetPastPresentationTimingGOOGLE),
363 reinterpret_cast<PFN_vkVoidFunction>(checkedGetPastPresentationTimingGOOGLE),
364 },
365 {
366 "vkGetPhysicalDeviceExternalBufferProperties",
367 ProcHook::INSTANCE,
368 ProcHook::EXTENSION_CORE_1_1,
369 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceExternalBufferProperties),
370 nullptr,
371 },
372 {
373 "vkGetPhysicalDeviceExternalFenceProperties",
374 ProcHook::INSTANCE,
375 ProcHook::EXTENSION_CORE_1_1,
376 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceExternalFenceProperties),
377 nullptr,
378 },
379 {
380 "vkGetPhysicalDeviceExternalSemaphoreProperties",
381 ProcHook::INSTANCE,
382 ProcHook::EXTENSION_CORE_1_1,
383 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceExternalSemaphoreProperties),
384 nullptr,
385 },
386 {
387 "vkGetPhysicalDeviceFeatures2",
388 ProcHook::INSTANCE,
389 ProcHook::EXTENSION_CORE_1_1,
390 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceFeatures2),
391 nullptr,
392 },
393 {
394 "vkGetPhysicalDeviceFormatProperties2",
395 ProcHook::INSTANCE,
396 ProcHook::EXTENSION_CORE_1_1,
397 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceFormatProperties2),
398 nullptr,
399 },
400 {
401 "vkGetPhysicalDeviceImageFormatProperties2",
402 ProcHook::INSTANCE,
403 ProcHook::EXTENSION_CORE_1_1,
404 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceImageFormatProperties2),
405 nullptr,
406 },
407 {
408 "vkGetPhysicalDeviceMemoryProperties2",
409 ProcHook::INSTANCE,
410 ProcHook::EXTENSION_CORE_1_1,
411 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceMemoryProperties2),
412 nullptr,
413 },
414 {
415 "vkGetPhysicalDevicePresentRectanglesKHR",
416 ProcHook::INSTANCE,
417 ProcHook::KHR_swapchain,
418 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDevicePresentRectanglesKHR),
419 nullptr,
420 },
421 {
422 "vkGetPhysicalDeviceProperties2",
423 ProcHook::INSTANCE,
424 ProcHook::EXTENSION_CORE_1_1,
425 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceProperties2),
426 nullptr,
427 },
428 {
429 "vkGetPhysicalDeviceQueueFamilyProperties2",
430 ProcHook::INSTANCE,
431 ProcHook::EXTENSION_CORE_1_1,
432 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceQueueFamilyProperties2),
433 nullptr,
434 },
435 {
436 "vkGetPhysicalDeviceSparseImageFormatProperties2",
437 ProcHook::INSTANCE,
438 ProcHook::EXTENSION_CORE_1_1,
439 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSparseImageFormatProperties2),
440 nullptr,
441 },
442 {
443 "vkGetPhysicalDeviceSurfaceCapabilities2KHR",
444 ProcHook::INSTANCE,
445 ProcHook::KHR_get_surface_capabilities2,
446 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceCapabilities2KHR),
447 nullptr,
448 },
449 {
450 "vkGetPhysicalDeviceSurfaceCapabilitiesKHR",
451 ProcHook::INSTANCE,
452 ProcHook::KHR_surface,
453 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceCapabilitiesKHR),
454 nullptr,
455 },
456 {
457 "vkGetPhysicalDeviceSurfaceFormats2KHR",
458 ProcHook::INSTANCE,
459 ProcHook::KHR_get_surface_capabilities2,
460 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceFormats2KHR),
461 nullptr,
462 },
463 {
464 "vkGetPhysicalDeviceSurfaceFormatsKHR",
465 ProcHook::INSTANCE,
466 ProcHook::KHR_surface,
467 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceFormatsKHR),
468 nullptr,
469 },
470 {
471 "vkGetPhysicalDeviceSurfacePresentModesKHR",
472 ProcHook::INSTANCE,
473 ProcHook::KHR_surface,
474 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfacePresentModesKHR),
475 nullptr,
476 },
477 {
478 "vkGetPhysicalDeviceSurfaceSupportKHR",
479 ProcHook::INSTANCE,
480 ProcHook::KHR_surface,
481 reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceSupportKHR),
482 nullptr,
483 },
484 {
485 "vkGetRefreshCycleDurationGOOGLE",
486 ProcHook::DEVICE,
487 ProcHook::GOOGLE_display_timing,
488 reinterpret_cast<PFN_vkVoidFunction>(GetRefreshCycleDurationGOOGLE),
489 reinterpret_cast<PFN_vkVoidFunction>(checkedGetRefreshCycleDurationGOOGLE),
490 },
491 {
492 "vkGetSwapchainGrallocUsage2ANDROID",
493 ProcHook::DEVICE,
494 ProcHook::ANDROID_native_buffer,
495 nullptr,
496 nullptr,
497 },
498 {
499 "vkGetSwapchainGrallocUsageANDROID",
500 ProcHook::DEVICE,
501 ProcHook::ANDROID_native_buffer,
502 nullptr,
503 nullptr,
504 },
505 {
506 "vkGetSwapchainImagesKHR",
507 ProcHook::DEVICE,
508 ProcHook::KHR_swapchain,
509 reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR),
510 reinterpret_cast<PFN_vkVoidFunction>(checkedGetSwapchainImagesKHR),
511 },
512 {
513 "vkGetSwapchainStatusKHR",
514 ProcHook::DEVICE,
515 ProcHook::KHR_shared_presentable_image,
516 reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainStatusKHR),
517 reinterpret_cast<PFN_vkVoidFunction>(checkedGetSwapchainStatusKHR),
518 },
519 {
520 "vkQueuePresentKHR",
521 ProcHook::DEVICE,
522 ProcHook::KHR_swapchain,
523 reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR),
524 reinterpret_cast<PFN_vkVoidFunction>(checkedQueuePresentKHR),
525 },
526 {
527 "vkQueueSignalReleaseImageANDROID",
528 ProcHook::DEVICE,
529 ProcHook::ANDROID_native_buffer,
530 nullptr,
531 nullptr,
532 },
533 {
534 "vkQueueSubmit",
535 ProcHook::DEVICE,
536 ProcHook::EXTENSION_CORE_1_0,
537 reinterpret_cast<PFN_vkVoidFunction>(QueueSubmit),
538 nullptr,
539 },
540 {
541 "vkSetHdrMetadataEXT",
542 ProcHook::DEVICE,
543 ProcHook::EXT_hdr_metadata,
544 reinterpret_cast<PFN_vkVoidFunction>(SetHdrMetadataEXT),
545 reinterpret_cast<PFN_vkVoidFunction>(checkedSetHdrMetadataEXT),
546 },
547 // clang-format on
548 };
549
550 } // namespace
551
GetProcHook(const char * name)552 const ProcHook* GetProcHook(const char* name) {
553 auto begin = std::cbegin(g_proc_hooks);
554 auto end = std::cend(g_proc_hooks);
555 auto hook = std::lower_bound(
556 begin, end, name,
557 [](const ProcHook& e, const char* n) { return strcmp(e.name, n) < 0; });
558 return (hook < end && strcmp(hook->name, name) == 0) ? hook : nullptr;
559 }
560
GetProcHookExtension(const char * name)561 ProcHook::Extension GetProcHookExtension(const char* name) {
562 // clang-format off
563 if (strcmp(name, "VK_ANDROID_native_buffer") == 0) return ProcHook::ANDROID_native_buffer;
564 if (strcmp(name, "VK_EXT_debug_report") == 0) return ProcHook::EXT_debug_report;
565 if (strcmp(name, "VK_EXT_hdr_metadata") == 0) return ProcHook::EXT_hdr_metadata;
566 if (strcmp(name, "VK_EXT_swapchain_colorspace") == 0) return ProcHook::EXT_swapchain_colorspace;
567 if (strcmp(name, "VK_GOOGLE_display_timing") == 0) return ProcHook::GOOGLE_display_timing;
568 if (strcmp(name, "VK_KHR_android_surface") == 0) return ProcHook::KHR_android_surface;
569 if (strcmp(name, "VK_KHR_get_surface_capabilities2") == 0) return ProcHook::KHR_get_surface_capabilities2;
570 if (strcmp(name, "VK_KHR_incremental_present") == 0) return ProcHook::KHR_incremental_present;
571 if (strcmp(name, "VK_KHR_shared_presentable_image") == 0) return ProcHook::KHR_shared_presentable_image;
572 if (strcmp(name, "VK_KHR_surface") == 0) return ProcHook::KHR_surface;
573 if (strcmp(name, "VK_KHR_swapchain") == 0) return ProcHook::KHR_swapchain;
574 if (strcmp(name, "VK_ANDROID_external_memory_android_hardware_buffer") == 0) return ProcHook::ANDROID_external_memory_android_hardware_buffer;
575 if (strcmp(name, "VK_KHR_bind_memory2") == 0) return ProcHook::KHR_bind_memory2;
576 if (strcmp(name, "VK_KHR_get_physical_device_properties2") == 0) return ProcHook::KHR_get_physical_device_properties2;
577 if (strcmp(name, "VK_KHR_device_group_creation") == 0) return ProcHook::KHR_device_group_creation;
578 if (strcmp(name, "VK_KHR_external_memory_capabilities") == 0) return ProcHook::KHR_external_memory_capabilities;
579 if (strcmp(name, "VK_KHR_external_semaphore_capabilities") == 0) return ProcHook::KHR_external_semaphore_capabilities;
580 if (strcmp(name, "VK_KHR_external_fence_capabilities") == 0) return ProcHook::KHR_external_fence_capabilities;
581 // clang-format on
582 return ProcHook::EXTENSION_UNKNOWN;
583 }
584
585 #define UNLIKELY(expr) __builtin_expect((expr), 0)
586
587 #define INIT_PROC(required, obj, proc) \
588 do { \
589 data.driver.proc = \
590 reinterpret_cast<PFN_vk##proc>(get_proc(obj, "vk" #proc)); \
591 if (UNLIKELY(required && !data.driver.proc)) { \
592 ALOGE("missing " #obj " proc: vk" #proc); \
593 success = false; \
594 } \
595 } while (0)
596
597 #define INIT_PROC_EXT(ext, required, obj, proc) \
598 do { \
599 if (extensions[ProcHook::ext]) \
600 INIT_PROC(required, obj, proc); \
601 } while (0)
602
InitDriverTable(VkInstance instance,PFN_vkGetInstanceProcAddr get_proc,const std::bitset<ProcHook::EXTENSION_COUNT> & extensions)603 bool InitDriverTable(VkInstance instance,
604 PFN_vkGetInstanceProcAddr get_proc,
605 const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
606 auto& data = GetData(instance);
607 bool success = true;
608
609 // clang-format off
610 INIT_PROC(true, instance, DestroyInstance);
611 INIT_PROC(true, instance, EnumeratePhysicalDevices);
612 INIT_PROC(true, instance, GetInstanceProcAddr);
613 INIT_PROC(true, instance, GetPhysicalDeviceProperties);
614 INIT_PROC(true, instance, CreateDevice);
615 INIT_PROC(true, instance, EnumerateDeviceExtensionProperties);
616 INIT_PROC_EXT(EXT_debug_report, true, instance, CreateDebugReportCallbackEXT);
617 INIT_PROC_EXT(EXT_debug_report, true, instance, DestroyDebugReportCallbackEXT);
618 INIT_PROC_EXT(EXT_debug_report, true, instance, DebugReportMessageEXT);
619 INIT_PROC(false, instance, GetPhysicalDeviceFeatures2);
620 INIT_PROC_EXT(KHR_get_physical_device_properties2, true, instance, GetPhysicalDeviceFeatures2KHR);
621 INIT_PROC(false, instance, GetPhysicalDeviceProperties2);
622 INIT_PROC_EXT(KHR_get_physical_device_properties2, true, instance, GetPhysicalDeviceProperties2KHR);
623 INIT_PROC(false, instance, GetPhysicalDeviceFormatProperties2);
624 INIT_PROC_EXT(KHR_get_physical_device_properties2, true, instance, GetPhysicalDeviceFormatProperties2KHR);
625 INIT_PROC(false, instance, GetPhysicalDeviceImageFormatProperties2);
626 INIT_PROC_EXT(KHR_get_physical_device_properties2, true, instance, GetPhysicalDeviceImageFormatProperties2KHR);
627 INIT_PROC(false, instance, GetPhysicalDeviceQueueFamilyProperties2);
628 INIT_PROC_EXT(KHR_get_physical_device_properties2, true, instance, GetPhysicalDeviceQueueFamilyProperties2KHR);
629 INIT_PROC(false, instance, GetPhysicalDeviceMemoryProperties2);
630 INIT_PROC_EXT(KHR_get_physical_device_properties2, true, instance, GetPhysicalDeviceMemoryProperties2KHR);
631 INIT_PROC(false, instance, GetPhysicalDeviceSparseImageFormatProperties2);
632 INIT_PROC_EXT(KHR_get_physical_device_properties2, true, instance, GetPhysicalDeviceSparseImageFormatProperties2KHR);
633 INIT_PROC(false, instance, GetPhysicalDeviceExternalBufferProperties);
634 INIT_PROC_EXT(KHR_external_memory_capabilities, true, instance, GetPhysicalDeviceExternalBufferPropertiesKHR);
635 INIT_PROC(false, instance, GetPhysicalDeviceExternalSemaphoreProperties);
636 INIT_PROC_EXT(KHR_external_semaphore_capabilities, true, instance, GetPhysicalDeviceExternalSemaphorePropertiesKHR);
637 INIT_PROC(false, instance, GetPhysicalDeviceExternalFenceProperties);
638 INIT_PROC_EXT(KHR_external_fence_capabilities, true, instance, GetPhysicalDeviceExternalFencePropertiesKHR);
639 INIT_PROC(false, instance, EnumeratePhysicalDeviceGroups);
640 INIT_PROC_EXT(KHR_device_group_creation, true, instance, EnumeratePhysicalDeviceGroupsKHR);
641 // clang-format on
642
643 return success;
644 }
645
InitDriverTable(VkDevice dev,PFN_vkGetDeviceProcAddr get_proc,const std::bitset<ProcHook::EXTENSION_COUNT> & extensions)646 bool InitDriverTable(VkDevice dev,
647 PFN_vkGetDeviceProcAddr get_proc,
648 const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
649 auto& data = GetData(dev);
650 bool success = true;
651
652 // clang-format off
653 INIT_PROC(true, dev, GetDeviceProcAddr);
654 INIT_PROC(true, dev, DestroyDevice);
655 INIT_PROC(true, dev, GetDeviceQueue);
656 INIT_PROC(true, dev, QueueSubmit);
657 INIT_PROC(true, dev, CreateImage);
658 INIT_PROC(true, dev, DestroyImage);
659 INIT_PROC(true, dev, AllocateCommandBuffers);
660 INIT_PROC(false, dev, BindImageMemory2);
661 INIT_PROC_EXT(KHR_bind_memory2, true, dev, BindImageMemory2KHR);
662 INIT_PROC(false, dev, GetDeviceQueue2);
663 INIT_PROC_EXT(ANDROID_native_buffer, false, dev, GetSwapchainGrallocUsageANDROID);
664 INIT_PROC_EXT(ANDROID_native_buffer, false, dev, GetSwapchainGrallocUsage2ANDROID);
665 INIT_PROC_EXT(ANDROID_native_buffer, true, dev, AcquireImageANDROID);
666 INIT_PROC_EXT(ANDROID_native_buffer, true, dev, QueueSignalReleaseImageANDROID);
667 // clang-format on
668
669 return success;
670 }
671
672 const std::pair<const char*, uint32_t> g_promoted_instance_extensions[] = {
673 // clang-format off
674 std::make_pair("VK_KHR_device_group_creation", VK_API_VERSION_1_1),
675 std::make_pair("VK_KHR_external_fence_capabilities", VK_API_VERSION_1_1),
676 std::make_pair("VK_KHR_external_memory_capabilities", VK_API_VERSION_1_1),
677 std::make_pair("VK_KHR_external_semaphore_capabilities", VK_API_VERSION_1_1),
678 std::make_pair("VK_KHR_get_physical_device_properties2", VK_API_VERSION_1_1),
679 // clang-format on
680 };
681
GetInstanceExtensionPromotedVersion(const char * name)682 std::optional<uint32_t> GetInstanceExtensionPromotedVersion(const char* name) {
683 auto begin = std::cbegin(g_promoted_instance_extensions);
684 auto end = std::cend(g_promoted_instance_extensions);
685 auto iter =
686 std::lower_bound(begin, end, name,
687 [](const std::pair<const char*, uint32_t>& e,
688 const char* n) { return strcmp(e.first, n) < 0; });
689 return (iter < end && strcmp(iter->first, name) == 0)
690 ? std::optional<uint32_t>(iter->second)
691 : std::nullopt;
692 }
693
CountPromotedInstanceExtensions(uint32_t begin_version,uint32_t end_version)694 uint32_t CountPromotedInstanceExtensions(uint32_t begin_version,
695 uint32_t end_version) {
696 auto begin = std::cbegin(g_promoted_instance_extensions);
697 auto end = std::cend(g_promoted_instance_extensions);
698 uint32_t count = 0;
699
700 for (auto iter = begin; iter != end; iter++)
701 if (iter->second > begin_version && iter->second <= end_version)
702 count++;
703
704 return count;
705 }
706
GetPromotedInstanceExtensions(uint32_t begin_version,uint32_t end_version)707 std::vector<const char*> GetPromotedInstanceExtensions(uint32_t begin_version,
708 uint32_t end_version) {
709 auto begin = std::cbegin(g_promoted_instance_extensions);
710 auto end = std::cend(g_promoted_instance_extensions);
711 std::vector<const char*> extensions;
712
713 for (auto iter = begin; iter != end; iter++)
714 if (iter->second > begin_version && iter->second <= end_version)
715 extensions.emplace_back(iter->first);
716
717 return extensions;
718 }
719
720 } // namespace driver
721 } // namespace vulkan
722