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 <string.h>
20 
21 #include <algorithm>
22 
23 #include <log/log.h>
24 
25 #include "driver.h"
26 
27 namespace vulkan {
28 namespace driver {
29 
30 namespace {
31 
32 // clang-format off
33 
checkedBindImageMemory2(VkDevice device,uint32_t bindInfoCount,const VkBindImageMemoryInfo * pBindInfos)34 VKAPI_ATTR VkResult checkedBindImageMemory2(VkDevice device, uint32_t bindInfoCount, const VkBindImageMemoryInfo* pBindInfos) {
35     if (GetData(device).hook_extensions[ProcHook::EXTENSION_CORE_1_1]) {
36         return BindImageMemory2(device, bindInfoCount, pBindInfos);
37     } else {
38         Logger(device).Err(device, "VK_VERSION_1_0 not enabled. vkBindImageMemory2 not executed.");
39         return VK_SUCCESS;
40     }
41 }
42 
checkedGetDeviceQueue2(VkDevice device,const VkDeviceQueueInfo2 * pQueueInfo,VkQueue * pQueue)43 VKAPI_ATTR void checkedGetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2* pQueueInfo, VkQueue* pQueue) {
44     if (GetData(device).hook_extensions[ProcHook::EXTENSION_CORE_1_1]) {
45         GetDeviceQueue2(device, pQueueInfo, pQueue);
46     } else {
47         Logger(device).Err(device, "VK_VERSION_1_0 not enabled. vkGetDeviceQueue2 not executed.");
48     }
49 }
50 
checkedCreateSwapchainKHR(VkDevice device,const VkSwapchainCreateInfoKHR * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkSwapchainKHR * pSwapchain)51 VKAPI_ATTR VkResult checkedCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) {
52     if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
53         return CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
54     } else {
55         Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkCreateSwapchainKHR not executed.");
56         return VK_SUCCESS;
57     }
58 }
59 
checkedDestroySwapchainKHR(VkDevice device,VkSwapchainKHR swapchain,const VkAllocationCallbacks * pAllocator)60 VKAPI_ATTR void checkedDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator) {
61     if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
62         DestroySwapchainKHR(device, swapchain, pAllocator);
63     } else {
64         Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkDestroySwapchainKHR not executed.");
65     }
66 }
67 
checkedGetSwapchainImagesKHR(VkDevice device,VkSwapchainKHR swapchain,uint32_t * pSwapchainImageCount,VkImage * pSwapchainImages)68 VKAPI_ATTR VkResult checkedGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages) {
69     if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
70         return GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
71     } else {
72         Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkGetSwapchainImagesKHR not executed.");
73         return VK_SUCCESS;
74     }
75 }
76 
checkedAcquireNextImageKHR(VkDevice device,VkSwapchainKHR swapchain,uint64_t timeout,VkSemaphore semaphore,VkFence fence,uint32_t * pImageIndex)77 VKAPI_ATTR VkResult checkedAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout, VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) {
78     if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
79         return AcquireNextImageKHR(device, swapchain, timeout, semaphore, fence, pImageIndex);
80     } else {
81         Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkAcquireNextImageKHR not executed.");
82         return VK_SUCCESS;
83     }
84 }
85 
checkedQueuePresentKHR(VkQueue queue,const VkPresentInfoKHR * pPresentInfo)86 VKAPI_ATTR VkResult checkedQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) {
87     if (GetData(queue).hook_extensions[ProcHook::KHR_swapchain]) {
88         return QueuePresentKHR(queue, pPresentInfo);
89     } else {
90         Logger(queue).Err(queue, "VK_KHR_swapchain not enabled. vkQueuePresentKHR 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 
checkedGetRefreshCycleDurationGOOGLE(VkDevice device,VkSwapchainKHR swapchain,VkRefreshCycleDurationGOOGLE * pDisplayTimingProperties)122 VKAPI_ATTR VkResult checkedGetRefreshCycleDurationGOOGLE(VkDevice device, VkSwapchainKHR swapchain, VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
123     if (GetData(device).hook_extensions[ProcHook::GOOGLE_display_timing]) {
124         return GetRefreshCycleDurationGOOGLE(device, swapchain, pDisplayTimingProperties);
125     } else {
126         Logger(device).Err(device, "VK_GOOGLE_display_timing not enabled. vkGetRefreshCycleDurationGOOGLE not executed.");
127         return VK_SUCCESS;
128     }
129 }
130 
checkedGetPastPresentationTimingGOOGLE(VkDevice device,VkSwapchainKHR swapchain,uint32_t * pPresentationTimingCount,VkPastPresentationTimingGOOGLE * pPresentationTimings)131 VKAPI_ATTR VkResult checkedGetPastPresentationTimingGOOGLE(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pPresentationTimingCount, VkPastPresentationTimingGOOGLE* pPresentationTimings) {
132     if (GetData(device).hook_extensions[ProcHook::GOOGLE_display_timing]) {
133         return GetPastPresentationTimingGOOGLE(device, swapchain, pPresentationTimingCount, pPresentationTimings);
134     } else {
135         Logger(device).Err(device, "VK_GOOGLE_display_timing not enabled. vkGetPastPresentationTimingGOOGLE not executed.");
136         return VK_SUCCESS;
137     }
138 }
139 
checkedSetHdrMetadataEXT(VkDevice device,uint32_t swapchainCount,const VkSwapchainKHR * pSwapchains,const VkHdrMetadataEXT * pMetadata)140 VKAPI_ATTR void checkedSetHdrMetadataEXT(VkDevice device, uint32_t swapchainCount, const VkSwapchainKHR* pSwapchains, const VkHdrMetadataEXT* pMetadata) {
141     if (GetData(device).hook_extensions[ProcHook::EXT_hdr_metadata]) {
142         SetHdrMetadataEXT(device, swapchainCount, pSwapchains, pMetadata);
143     } else {
144         Logger(device).Err(device, "VK_EXT_hdr_metadata not enabled. vkSetHdrMetadataEXT not executed.");
145     }
146 }
147 
checkedGetSwapchainStatusKHR(VkDevice device,VkSwapchainKHR swapchain)148 VKAPI_ATTR VkResult checkedGetSwapchainStatusKHR(VkDevice device, VkSwapchainKHR swapchain) {
149     if (GetData(device).hook_extensions[ProcHook::KHR_shared_presentable_image]) {
150         return GetSwapchainStatusKHR(device, swapchain);
151     } else {
152         Logger(device).Err(device, "VK_KHR_shared_presentable_image not enabled. vkGetSwapchainStatusKHR not executed.");
153         return VK_SUCCESS;
154     }
155 }
156 
checkedBindImageMemory2KHR(VkDevice device,uint32_t bindInfoCount,const VkBindImageMemoryInfoKHR * pBindInfos)157 VKAPI_ATTR VkResult checkedBindImageMemory2KHR(VkDevice device, uint32_t bindInfoCount, const VkBindImageMemoryInfoKHR* pBindInfos) {
158     if (GetData(device).hook_extensions[ProcHook::KHR_bind_memory2]) {
159         return BindImageMemory2KHR(device, bindInfoCount, pBindInfos);
160     } else {
161         Logger(device).Err(device, "VK_KHR_bind_memory2 not enabled. vkBindImageMemory2KHR not executed.");
162         return VK_SUCCESS;
163     }
164 }
165 
166 // clang-format on
167 
168 const ProcHook g_proc_hooks[] = {
169     // clang-format off
170     {
171         "vkAcquireImageANDROID",
172         ProcHook::DEVICE,
173         ProcHook::ANDROID_native_buffer,
174         nullptr,
175         nullptr,
176     },
177     {
178         "vkAcquireNextImage2KHR",
179         ProcHook::DEVICE,
180         ProcHook::KHR_swapchain,
181         reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImage2KHR),
182         reinterpret_cast<PFN_vkVoidFunction>(checkedAcquireNextImage2KHR),
183     },
184     {
185         "vkAcquireNextImageKHR",
186         ProcHook::DEVICE,
187         ProcHook::KHR_swapchain,
188         reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImageKHR),
189         reinterpret_cast<PFN_vkVoidFunction>(checkedAcquireNextImageKHR),
190     },
191     {
192         "vkAllocateCommandBuffers",
193         ProcHook::DEVICE,
194         ProcHook::EXTENSION_CORE_1_0,
195         reinterpret_cast<PFN_vkVoidFunction>(AllocateCommandBuffers),
196         nullptr,
197     },
198     {
199         "vkBindImageMemory2",
200         ProcHook::DEVICE,
201         ProcHook::EXTENSION_CORE_1_1,
202         reinterpret_cast<PFN_vkVoidFunction>(BindImageMemory2),
203         reinterpret_cast<PFN_vkVoidFunction>(checkedBindImageMemory2),
204     },
205     {
206         "vkBindImageMemory2KHR",
207         ProcHook::DEVICE,
208         ProcHook::KHR_bind_memory2,
209         reinterpret_cast<PFN_vkVoidFunction>(BindImageMemory2KHR),
210         reinterpret_cast<PFN_vkVoidFunction>(checkedBindImageMemory2KHR),
211     },
212     {
213         "vkCreateAndroidSurfaceKHR",
214         ProcHook::INSTANCE,
215         ProcHook::KHR_android_surface,
216         reinterpret_cast<PFN_vkVoidFunction>(CreateAndroidSurfaceKHR),
217         nullptr,
218     },
219     {
220         "vkCreateDebugReportCallbackEXT",
221         ProcHook::INSTANCE,
222         ProcHook::EXT_debug_report,
223         reinterpret_cast<PFN_vkVoidFunction>(CreateDebugReportCallbackEXT),
224         nullptr,
225     },
226     {
227         "vkCreateDevice",
228         ProcHook::INSTANCE,
229         ProcHook::EXTENSION_CORE_1_0,
230         reinterpret_cast<PFN_vkVoidFunction>(CreateDevice),
231         nullptr,
232     },
233     {
234         "vkCreateInstance",
235         ProcHook::GLOBAL,
236         ProcHook::EXTENSION_CORE_1_0,
237         reinterpret_cast<PFN_vkVoidFunction>(CreateInstance),
238         nullptr,
239     },
240     {
241         "vkCreateSwapchainKHR",
242         ProcHook::DEVICE,
243         ProcHook::KHR_swapchain,
244         reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR),
245         reinterpret_cast<PFN_vkVoidFunction>(checkedCreateSwapchainKHR),
246     },
247     {
248         "vkDebugReportMessageEXT",
249         ProcHook::INSTANCE,
250         ProcHook::EXT_debug_report,
251         reinterpret_cast<PFN_vkVoidFunction>(DebugReportMessageEXT),
252         nullptr,
253     },
254     {
255         "vkDestroyDebugReportCallbackEXT",
256         ProcHook::INSTANCE,
257         ProcHook::EXT_debug_report,
258         reinterpret_cast<PFN_vkVoidFunction>(DestroyDebugReportCallbackEXT),
259         nullptr,
260     },
261     {
262         "vkDestroyDevice",
263         ProcHook::DEVICE,
264         ProcHook::EXTENSION_CORE_1_0,
265         reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice),
266         nullptr,
267     },
268     {
269         "vkDestroyInstance",
270         ProcHook::INSTANCE,
271         ProcHook::EXTENSION_CORE_1_0,
272         reinterpret_cast<PFN_vkVoidFunction>(DestroyInstance),
273         nullptr,
274     },
275     {
276         "vkDestroySurfaceKHR",
277         ProcHook::INSTANCE,
278         ProcHook::KHR_surface,
279         reinterpret_cast<PFN_vkVoidFunction>(DestroySurfaceKHR),
280         nullptr,
281     },
282     {
283         "vkDestroySwapchainKHR",
284         ProcHook::DEVICE,
285         ProcHook::KHR_swapchain,
286         reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR),
287         reinterpret_cast<PFN_vkVoidFunction>(checkedDestroySwapchainKHR),
288     },
289     {
290         "vkEnumerateDeviceExtensionProperties",
291         ProcHook::INSTANCE,
292         ProcHook::EXTENSION_CORE_1_0,
293         reinterpret_cast<PFN_vkVoidFunction>(EnumerateDeviceExtensionProperties),
294         nullptr,
295     },
296     {
297         "vkEnumerateInstanceExtensionProperties",
298         ProcHook::GLOBAL,
299         ProcHook::EXTENSION_CORE_1_0,
300         reinterpret_cast<PFN_vkVoidFunction>(EnumerateInstanceExtensionProperties),
301         nullptr,
302     },
303     {
304         "vkEnumeratePhysicalDeviceGroups",
305         ProcHook::INSTANCE,
306         ProcHook::EXTENSION_CORE_1_1,
307         reinterpret_cast<PFN_vkVoidFunction>(EnumeratePhysicalDeviceGroups),
308         nullptr,
309     },
310     {
311         "vkEnumeratePhysicalDevices",
312         ProcHook::INSTANCE,
313         ProcHook::EXTENSION_CORE_1_0,
314         reinterpret_cast<PFN_vkVoidFunction>(EnumeratePhysicalDevices),
315         nullptr,
316     },
317     {
318         "vkGetDeviceGroupPresentCapabilitiesKHR",
319         ProcHook::DEVICE,
320         ProcHook::KHR_swapchain,
321         reinterpret_cast<PFN_vkVoidFunction>(GetDeviceGroupPresentCapabilitiesKHR),
322         reinterpret_cast<PFN_vkVoidFunction>(checkedGetDeviceGroupPresentCapabilitiesKHR),
323     },
324     {
325         "vkGetDeviceGroupSurfacePresentModesKHR",
326         ProcHook::DEVICE,
327         ProcHook::KHR_swapchain,
328         reinterpret_cast<PFN_vkVoidFunction>(GetDeviceGroupSurfacePresentModesKHR),
329         reinterpret_cast<PFN_vkVoidFunction>(checkedGetDeviceGroupSurfacePresentModesKHR),
330     },
331     {
332         "vkGetDeviceProcAddr",
333         ProcHook::DEVICE,
334         ProcHook::EXTENSION_CORE_1_0,
335         reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr),
336         nullptr,
337     },
338     {
339         "vkGetDeviceQueue",
340         ProcHook::DEVICE,
341         ProcHook::EXTENSION_CORE_1_0,
342         reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue),
343         nullptr,
344     },
345     {
346         "vkGetDeviceQueue2",
347         ProcHook::DEVICE,
348         ProcHook::EXTENSION_CORE_1_1,
349         reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue2),
350         reinterpret_cast<PFN_vkVoidFunction>(checkedGetDeviceQueue2),
351     },
352     {
353         "vkGetInstanceProcAddr",
354         ProcHook::INSTANCE,
355         ProcHook::EXTENSION_CORE_1_0,
356         reinterpret_cast<PFN_vkVoidFunction>(GetInstanceProcAddr),
357         nullptr,
358     },
359     {
360         "vkGetPastPresentationTimingGOOGLE",
361         ProcHook::DEVICE,
362         ProcHook::GOOGLE_display_timing,
363         reinterpret_cast<PFN_vkVoidFunction>(GetPastPresentationTimingGOOGLE),
364         reinterpret_cast<PFN_vkVoidFunction>(checkedGetPastPresentationTimingGOOGLE),
365     },
366     {
367         "vkGetPhysicalDevicePresentRectanglesKHR",
368         ProcHook::INSTANCE,
369         ProcHook::KHR_swapchain,
370         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDevicePresentRectanglesKHR),
371         nullptr,
372     },
373     {
374         "vkGetPhysicalDeviceSurfaceCapabilities2KHR",
375         ProcHook::INSTANCE,
376         ProcHook::KHR_get_surface_capabilities2,
377         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceCapabilities2KHR),
378         nullptr,
379     },
380     {
381         "vkGetPhysicalDeviceSurfaceCapabilitiesKHR",
382         ProcHook::INSTANCE,
383         ProcHook::KHR_surface,
384         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceCapabilitiesKHR),
385         nullptr,
386     },
387     {
388         "vkGetPhysicalDeviceSurfaceFormats2KHR",
389         ProcHook::INSTANCE,
390         ProcHook::KHR_get_surface_capabilities2,
391         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceFormats2KHR),
392         nullptr,
393     },
394     {
395         "vkGetPhysicalDeviceSurfaceFormatsKHR",
396         ProcHook::INSTANCE,
397         ProcHook::KHR_surface,
398         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceFormatsKHR),
399         nullptr,
400     },
401     {
402         "vkGetPhysicalDeviceSurfacePresentModesKHR",
403         ProcHook::INSTANCE,
404         ProcHook::KHR_surface,
405         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfacePresentModesKHR),
406         nullptr,
407     },
408     {
409         "vkGetPhysicalDeviceSurfaceSupportKHR",
410         ProcHook::INSTANCE,
411         ProcHook::KHR_surface,
412         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceSupportKHR),
413         nullptr,
414     },
415     {
416         "vkGetRefreshCycleDurationGOOGLE",
417         ProcHook::DEVICE,
418         ProcHook::GOOGLE_display_timing,
419         reinterpret_cast<PFN_vkVoidFunction>(GetRefreshCycleDurationGOOGLE),
420         reinterpret_cast<PFN_vkVoidFunction>(checkedGetRefreshCycleDurationGOOGLE),
421     },
422     {
423         "vkGetSwapchainGrallocUsage2ANDROID",
424         ProcHook::DEVICE,
425         ProcHook::ANDROID_native_buffer,
426         nullptr,
427         nullptr,
428     },
429     {
430         "vkGetSwapchainGrallocUsageANDROID",
431         ProcHook::DEVICE,
432         ProcHook::ANDROID_native_buffer,
433         nullptr,
434         nullptr,
435     },
436     {
437         "vkGetSwapchainImagesKHR",
438         ProcHook::DEVICE,
439         ProcHook::KHR_swapchain,
440         reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR),
441         reinterpret_cast<PFN_vkVoidFunction>(checkedGetSwapchainImagesKHR),
442     },
443     {
444         "vkGetSwapchainStatusKHR",
445         ProcHook::DEVICE,
446         ProcHook::KHR_shared_presentable_image,
447         reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainStatusKHR),
448         reinterpret_cast<PFN_vkVoidFunction>(checkedGetSwapchainStatusKHR),
449     },
450     {
451         "vkQueuePresentKHR",
452         ProcHook::DEVICE,
453         ProcHook::KHR_swapchain,
454         reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR),
455         reinterpret_cast<PFN_vkVoidFunction>(checkedQueuePresentKHR),
456     },
457     {
458         "vkQueueSignalReleaseImageANDROID",
459         ProcHook::DEVICE,
460         ProcHook::ANDROID_native_buffer,
461         nullptr,
462         nullptr,
463     },
464     {
465         "vkSetHdrMetadataEXT",
466         ProcHook::DEVICE,
467         ProcHook::EXT_hdr_metadata,
468         reinterpret_cast<PFN_vkVoidFunction>(SetHdrMetadataEXT),
469         reinterpret_cast<PFN_vkVoidFunction>(checkedSetHdrMetadataEXT),
470     },
471     // clang-format on
472 };
473 
474 }  // namespace
475 
GetProcHook(const char * name)476 const ProcHook* GetProcHook(const char* name) {
477     const auto& begin = g_proc_hooks;
478     const auto& end =
479         g_proc_hooks + sizeof(g_proc_hooks) / sizeof(g_proc_hooks[0]);
480     const auto hook = std::lower_bound(
481         begin, end, name,
482         [](const ProcHook& e, const char* n) { return strcmp(e.name, n) < 0; });
483     return (hook < end && strcmp(hook->name, name) == 0) ? hook : nullptr;
484 }
485 
GetProcHookExtension(const char * name)486 ProcHook::Extension GetProcHookExtension(const char* name) {
487     // clang-format off
488     if (strcmp(name, "VK_ANDROID_native_buffer") == 0) return ProcHook::ANDROID_native_buffer;
489     if (strcmp(name, "VK_EXT_debug_report") == 0) return ProcHook::EXT_debug_report;
490     if (strcmp(name, "VK_EXT_hdr_metadata") == 0) return ProcHook::EXT_hdr_metadata;
491     if (strcmp(name, "VK_EXT_swapchain_colorspace") == 0) return ProcHook::EXT_swapchain_colorspace;
492     if (strcmp(name, "VK_GOOGLE_display_timing") == 0) return ProcHook::GOOGLE_display_timing;
493     if (strcmp(name, "VK_KHR_android_surface") == 0) return ProcHook::KHR_android_surface;
494     if (strcmp(name, "VK_KHR_incremental_present") == 0) return ProcHook::KHR_incremental_present;
495     if (strcmp(name, "VK_KHR_shared_presentable_image") == 0) return ProcHook::KHR_shared_presentable_image;
496     if (strcmp(name, "VK_KHR_surface") == 0) return ProcHook::KHR_surface;
497     if (strcmp(name, "VK_KHR_swapchain") == 0) return ProcHook::KHR_swapchain;
498     if (strcmp(name, "VK_KHR_get_surface_capabilities2") == 0) return ProcHook::KHR_get_surface_capabilities2;
499     if (strcmp(name, "VK_KHR_get_physical_device_properties2") == 0) return ProcHook::KHR_get_physical_device_properties2;
500     if (strcmp(name, "VK_ANDROID_external_memory_android_hardware_buffer") == 0) return ProcHook::ANDROID_external_memory_android_hardware_buffer;
501     if (strcmp(name, "VK_KHR_bind_memory2") == 0) return ProcHook::KHR_bind_memory2;
502     // clang-format on
503     return ProcHook::EXTENSION_UNKNOWN;
504 }
505 
506 #define UNLIKELY(expr) __builtin_expect((expr), 0)
507 
508 #define INIT_PROC(required, obj, proc)                                 \
509     do {                                                               \
510         data.driver.proc =                                             \
511             reinterpret_cast<PFN_vk##proc>(get_proc(obj, "vk" #proc)); \
512         if (UNLIKELY(required && !data.driver.proc)) {                 \
513             ALOGE("missing " #obj " proc: vk" #proc);                  \
514             success = false;                                           \
515         }                                                              \
516     } while (0)
517 
518 #define INIT_PROC_EXT(ext, required, obj, proc) \
519     do {                                        \
520         if (extensions[ProcHook::ext])          \
521             INIT_PROC(required, obj, proc);     \
522     } while (0)
523 
InitDriverTable(VkInstance instance,PFN_vkGetInstanceProcAddr get_proc,const std::bitset<ProcHook::EXTENSION_COUNT> & extensions)524 bool InitDriverTable(VkInstance instance,
525                      PFN_vkGetInstanceProcAddr get_proc,
526                      const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
527     auto& data = GetData(instance);
528     bool success = true;
529 
530     // clang-format off
531     INIT_PROC(true, instance, DestroyInstance);
532     INIT_PROC(true, instance, EnumeratePhysicalDevices);
533     INIT_PROC(true, instance, GetInstanceProcAddr);
534     INIT_PROC(true, instance, GetPhysicalDeviceProperties);
535     INIT_PROC(true, instance, CreateDevice);
536     INIT_PROC(true, instance, EnumerateDeviceExtensionProperties);
537     INIT_PROC(false, instance, EnumeratePhysicalDeviceGroups);
538     INIT_PROC(false, instance, GetPhysicalDeviceProperties2);
539     INIT_PROC_EXT(EXT_debug_report, true, instance, CreateDebugReportCallbackEXT);
540     INIT_PROC_EXT(EXT_debug_report, true, instance, DestroyDebugReportCallbackEXT);
541     INIT_PROC_EXT(EXT_debug_report, true, instance, DebugReportMessageEXT);
542     INIT_PROC_EXT(KHR_get_physical_device_properties2, true, instance, GetPhysicalDeviceProperties2KHR);
543     // clang-format on
544 
545     return success;
546 }
547 
InitDriverTable(VkDevice dev,PFN_vkGetDeviceProcAddr get_proc,const std::bitset<ProcHook::EXTENSION_COUNT> & extensions)548 bool InitDriverTable(VkDevice dev,
549                      PFN_vkGetDeviceProcAddr get_proc,
550                      const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
551     auto& data = GetData(dev);
552     bool success = true;
553 
554     // clang-format off
555     INIT_PROC(true, dev, GetDeviceProcAddr);
556     INIT_PROC(true, dev, DestroyDevice);
557     INIT_PROC(true, dev, GetDeviceQueue);
558     INIT_PROC(true, dev, CreateImage);
559     INIT_PROC(true, dev, DestroyImage);
560     INIT_PROC(true, dev, AllocateCommandBuffers);
561     INIT_PROC(false, dev, BindImageMemory2);
562     INIT_PROC(false, dev, GetDeviceQueue2);
563     INIT_PROC_EXT(ANDROID_native_buffer, false, dev, GetSwapchainGrallocUsageANDROID);
564     INIT_PROC_EXT(ANDROID_native_buffer, false, dev, GetSwapchainGrallocUsage2ANDROID);
565     INIT_PROC_EXT(ANDROID_native_buffer, true, dev, AcquireImageANDROID);
566     INIT_PROC_EXT(ANDROID_native_buffer, true, dev, QueueSignalReleaseImageANDROID);
567     INIT_PROC_EXT(KHR_bind_memory2, true, dev, BindImageMemory2KHR);
568     // clang-format on
569 
570     return success;
571 }
572 
573 }  // namespace driver
574 }  // namespace vulkan
575 
576 // clang-format on
577