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