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         "vkGetPhysicalDevicePresentRectanglesKHR",
367         ProcHook::INSTANCE,
368         ProcHook::KHR_swapchain,
369         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDevicePresentRectanglesKHR),
370         nullptr,
371     },
372     {
373         "vkGetPhysicalDeviceSurfaceCapabilities2KHR",
374         ProcHook::INSTANCE,
375         ProcHook::KHR_get_surface_capabilities2,
376         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceCapabilities2KHR),
377         nullptr,
378     },
379     {
380         "vkGetPhysicalDeviceSurfaceCapabilitiesKHR",
381         ProcHook::INSTANCE,
382         ProcHook::KHR_surface,
383         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceCapabilitiesKHR),
384         nullptr,
385     },
386     {
387         "vkGetPhysicalDeviceSurfaceFormats2KHR",
388         ProcHook::INSTANCE,
389         ProcHook::KHR_get_surface_capabilities2,
390         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceFormats2KHR),
391         nullptr,
392     },
393     {
394         "vkGetPhysicalDeviceSurfaceFormatsKHR",
395         ProcHook::INSTANCE,
396         ProcHook::KHR_surface,
397         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceFormatsKHR),
398         nullptr,
399     },
400     {
401         "vkGetPhysicalDeviceSurfacePresentModesKHR",
402         ProcHook::INSTANCE,
403         ProcHook::KHR_surface,
404         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfacePresentModesKHR),
405         nullptr,
406     },
407     {
408         "vkGetPhysicalDeviceSurfaceSupportKHR",
409         ProcHook::INSTANCE,
410         ProcHook::KHR_surface,
411         reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceSupportKHR),
412         nullptr,
413     },
414     {
415         "vkGetRefreshCycleDurationGOOGLE",
416         ProcHook::DEVICE,
417         ProcHook::GOOGLE_display_timing,
418         reinterpret_cast<PFN_vkVoidFunction>(GetRefreshCycleDurationGOOGLE),
419         reinterpret_cast<PFN_vkVoidFunction>(checkedGetRefreshCycleDurationGOOGLE),
420     },
421     {
422         "vkGetSwapchainGrallocUsage2ANDROID",
423         ProcHook::DEVICE,
424         ProcHook::ANDROID_native_buffer,
425         nullptr,
426         nullptr,
427     },
428     {
429         "vkGetSwapchainGrallocUsageANDROID",
430         ProcHook::DEVICE,
431         ProcHook::ANDROID_native_buffer,
432         nullptr,
433         nullptr,
434     },
435     {
436         "vkGetSwapchainImagesKHR",
437         ProcHook::DEVICE,
438         ProcHook::KHR_swapchain,
439         reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR),
440         reinterpret_cast<PFN_vkVoidFunction>(checkedGetSwapchainImagesKHR),
441     },
442     {
443         "vkGetSwapchainStatusKHR",
444         ProcHook::DEVICE,
445         ProcHook::KHR_shared_presentable_image,
446         reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainStatusKHR),
447         reinterpret_cast<PFN_vkVoidFunction>(checkedGetSwapchainStatusKHR),
448     },
449     {
450         "vkQueuePresentKHR",
451         ProcHook::DEVICE,
452         ProcHook::KHR_swapchain,
453         reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR),
454         reinterpret_cast<PFN_vkVoidFunction>(checkedQueuePresentKHR),
455     },
456     {
457         "vkQueueSignalReleaseImageANDROID",
458         ProcHook::DEVICE,
459         ProcHook::ANDROID_native_buffer,
460         nullptr,
461         nullptr,
462     },
463     {
464         "vkQueueSubmit",
465         ProcHook::DEVICE,
466         ProcHook::EXTENSION_CORE_1_0,
467         reinterpret_cast<PFN_vkVoidFunction>(QueueSubmit),
468         nullptr,
469     },
470     {
471         "vkSetHdrMetadataEXT",
472         ProcHook::DEVICE,
473         ProcHook::EXT_hdr_metadata,
474         reinterpret_cast<PFN_vkVoidFunction>(SetHdrMetadataEXT),
475         reinterpret_cast<PFN_vkVoidFunction>(checkedSetHdrMetadataEXT),
476     },
477     // clang-format on
478 };
479 
480 }  // namespace
481 
GetProcHook(const char * name)482 const ProcHook* GetProcHook(const char* name) {
483     const auto& begin = g_proc_hooks;
484     const auto& end =
485         g_proc_hooks + sizeof(g_proc_hooks) / sizeof(g_proc_hooks[0]);
486     const auto hook = std::lower_bound(
487         begin, end, name,
488         [](const ProcHook& e, const char* n) { return strcmp(e.name, n) < 0; });
489     return (hook < end && strcmp(hook->name, name) == 0) ? hook : nullptr;
490 }
491 
GetProcHookExtension(const char * name)492 ProcHook::Extension GetProcHookExtension(const char* name) {
493     // clang-format off
494     if (strcmp(name, "VK_ANDROID_native_buffer") == 0) return ProcHook::ANDROID_native_buffer;
495     if (strcmp(name, "VK_EXT_debug_report") == 0) return ProcHook::EXT_debug_report;
496     if (strcmp(name, "VK_EXT_hdr_metadata") == 0) return ProcHook::EXT_hdr_metadata;
497     if (strcmp(name, "VK_EXT_swapchain_colorspace") == 0) return ProcHook::EXT_swapchain_colorspace;
498     if (strcmp(name, "VK_GOOGLE_display_timing") == 0) return ProcHook::GOOGLE_display_timing;
499     if (strcmp(name, "VK_KHR_android_surface") == 0) return ProcHook::KHR_android_surface;
500     if (strcmp(name, "VK_KHR_get_surface_capabilities2") == 0) return ProcHook::KHR_get_surface_capabilities2;
501     if (strcmp(name, "VK_KHR_incremental_present") == 0) return ProcHook::KHR_incremental_present;
502     if (strcmp(name, "VK_KHR_shared_presentable_image") == 0) return ProcHook::KHR_shared_presentable_image;
503     if (strcmp(name, "VK_KHR_surface") == 0) return ProcHook::KHR_surface;
504     if (strcmp(name, "VK_KHR_swapchain") == 0) return ProcHook::KHR_swapchain;
505     if (strcmp(name, "VK_ANDROID_external_memory_android_hardware_buffer") == 0) return ProcHook::ANDROID_external_memory_android_hardware_buffer;
506     if (strcmp(name, "VK_KHR_bind_memory2") == 0) return ProcHook::KHR_bind_memory2;
507     if (strcmp(name, "VK_KHR_get_physical_device_properties2") == 0) return ProcHook::KHR_get_physical_device_properties2;
508     // clang-format on
509     return ProcHook::EXTENSION_UNKNOWN;
510 }
511 
512 #define UNLIKELY(expr) __builtin_expect((expr), 0)
513 
514 #define INIT_PROC(required, obj, proc)                                 \
515     do {                                                               \
516         data.driver.proc =                                             \
517             reinterpret_cast<PFN_vk##proc>(get_proc(obj, "vk" #proc)); \
518         if (UNLIKELY(required && !data.driver.proc)) {                 \
519             ALOGE("missing " #obj " proc: vk" #proc);                  \
520             success = false;                                           \
521         }                                                              \
522     } while (0)
523 
524 #define INIT_PROC_EXT(ext, required, obj, proc) \
525     do {                                        \
526         if (extensions[ProcHook::ext])          \
527             INIT_PROC(required, obj, proc);     \
528     } while (0)
529 
InitDriverTable(VkInstance instance,PFN_vkGetInstanceProcAddr get_proc,const std::bitset<ProcHook::EXTENSION_COUNT> & extensions)530 bool InitDriverTable(VkInstance instance,
531                      PFN_vkGetInstanceProcAddr get_proc,
532                      const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
533     auto& data = GetData(instance);
534     bool success = true;
535 
536     // clang-format off
537     INIT_PROC(true, instance, DestroyInstance);
538     INIT_PROC(true, instance, EnumeratePhysicalDevices);
539     INIT_PROC(true, instance, GetInstanceProcAddr);
540     INIT_PROC(true, instance, GetPhysicalDeviceProperties);
541     INIT_PROC(true, instance, CreateDevice);
542     INIT_PROC(true, instance, EnumerateDeviceExtensionProperties);
543     INIT_PROC_EXT(EXT_debug_report, true, instance, CreateDebugReportCallbackEXT);
544     INIT_PROC_EXT(EXT_debug_report, true, instance, DestroyDebugReportCallbackEXT);
545     INIT_PROC_EXT(EXT_debug_report, true, instance, DebugReportMessageEXT);
546     INIT_PROC(false, instance, GetPhysicalDeviceProperties2);
547     INIT_PROC_EXT(KHR_get_physical_device_properties2, true, instance, GetPhysicalDeviceProperties2KHR);
548     INIT_PROC(false, instance, EnumeratePhysicalDeviceGroups);
549     // clang-format on
550 
551     return success;
552 }
553 
InitDriverTable(VkDevice dev,PFN_vkGetDeviceProcAddr get_proc,const std::bitset<ProcHook::EXTENSION_COUNT> & extensions)554 bool InitDriverTable(VkDevice dev,
555                      PFN_vkGetDeviceProcAddr get_proc,
556                      const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
557     auto& data = GetData(dev);
558     bool success = true;
559 
560     // clang-format off
561     INIT_PROC(true, dev, GetDeviceProcAddr);
562     INIT_PROC(true, dev, DestroyDevice);
563     INIT_PROC(true, dev, GetDeviceQueue);
564     INIT_PROC(true, dev, QueueSubmit);
565     INIT_PROC(true, dev, CreateImage);
566     INIT_PROC(true, dev, DestroyImage);
567     INIT_PROC(true, dev, AllocateCommandBuffers);
568     INIT_PROC(false, dev, BindImageMemory2);
569     INIT_PROC_EXT(KHR_bind_memory2, true, dev, BindImageMemory2KHR);
570     INIT_PROC(false, dev, GetDeviceQueue2);
571     INIT_PROC_EXT(ANDROID_native_buffer, false, dev, GetSwapchainGrallocUsageANDROID);
572     INIT_PROC_EXT(ANDROID_native_buffer, false, dev, GetSwapchainGrallocUsage2ANDROID);
573     INIT_PROC_EXT(ANDROID_native_buffer, true, dev, AcquireImageANDROID);
574     INIT_PROC_EXT(ANDROID_native_buffer, true, dev, QueueSignalReleaseImageANDROID);
575     // clang-format on
576 
577     return success;
578 }
579 
580 }  // namespace driver
581 }  // namespace vulkan
582