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