driver_gen.cpp revision cbe07ef239121dc99a31a19b9593d76d76abb08b
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#include <algorithm>
21#include <log/log.h>
22
23#include "driver.h"
24
25namespace vulkan {
26namespace driver {
27
28namespace {
29
30// clang-format off
31
32VKAPI_ATTR void disabledDestroySurfaceKHR(VkInstance, VkSurfaceKHR, const VkAllocationCallbacks*) {
33    ALOGE("VK_KHR_surface not enabled. vkDestroySurfaceKHR not executed.");
34}
35
36VKAPI_ATTR VkResult disabledGetPhysicalDeviceSurfaceSupportKHR(VkPhysicalDevice, uint32_t, VkSurfaceKHR, VkBool32*) {
37    ALOGE("VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceSupportKHR not executed.");
38    return VK_SUCCESS;
39}
40
41VKAPI_ATTR VkResult disabledGetPhysicalDeviceSurfaceCapabilitiesKHR(VkPhysicalDevice, VkSurfaceKHR, VkSurfaceCapabilitiesKHR*) {
42    ALOGE("VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceCapabilitiesKHR not executed.");
43    return VK_SUCCESS;
44}
45
46VKAPI_ATTR VkResult disabledGetPhysicalDeviceSurfaceFormatsKHR(VkPhysicalDevice, VkSurfaceKHR, uint32_t*, VkSurfaceFormatKHR*) {
47    ALOGE("VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfaceFormatsKHR not executed.");
48    return VK_SUCCESS;
49}
50
51VKAPI_ATTR VkResult disabledGetPhysicalDeviceSurfacePresentModesKHR(VkPhysicalDevice, VkSurfaceKHR, uint32_t*, VkPresentModeKHR*) {
52    ALOGE("VK_KHR_surface not enabled. vkGetPhysicalDeviceSurfacePresentModesKHR not executed.");
53    return VK_SUCCESS;
54}
55
56VKAPI_ATTR VkResult disabledCreateSwapchainKHR(VkDevice, const VkSwapchainCreateInfoKHR*, const VkAllocationCallbacks*, VkSwapchainKHR*) {
57    ALOGE("VK_KHR_swapchain not enabled. vkCreateSwapchainKHR not executed.");
58    return VK_SUCCESS;
59}
60
61VKAPI_ATTR VkResult checkedCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) {
62    return (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) ? CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain) : disabledCreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
63}
64
65VKAPI_ATTR void disabledDestroySwapchainKHR(VkDevice, VkSwapchainKHR, const VkAllocationCallbacks*) {
66    ALOGE("VK_KHR_swapchain not enabled. vkDestroySwapchainKHR not executed.");
67}
68
69VKAPI_ATTR void checkedDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator) {
70    (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) ? DestroySwapchainKHR(device, swapchain, pAllocator) : disabledDestroySwapchainKHR(device, swapchain, pAllocator);
71}
72
73VKAPI_ATTR VkResult disabledGetSwapchainImagesKHR(VkDevice, VkSwapchainKHR, uint32_t*, VkImage*) {
74    ALOGE("VK_KHR_swapchain not enabled. vkGetSwapchainImagesKHR not executed.");
75    return VK_SUCCESS;
76}
77
78VKAPI_ATTR VkResult checkedGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages) {
79    return (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) ? GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages) : disabledGetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
80}
81
82VKAPI_ATTR VkResult disabledAcquireNextImageKHR(VkDevice, VkSwapchainKHR, uint64_t, VkSemaphore, VkFence, uint32_t*) {
83    ALOGE("VK_KHR_swapchain not enabled. vkAcquireNextImageKHR not executed.");
84    return VK_SUCCESS;
85}
86
87VKAPI_ATTR VkResult checkedAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout, VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) {
88    return (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) ? AcquireNextImageKHR(device, swapchain, timeout, semaphore, fence, pImageIndex) : disabledAcquireNextImageKHR(device, swapchain, timeout, semaphore, fence, pImageIndex);
89}
90
91VKAPI_ATTR VkResult disabledQueuePresentKHR(VkQueue, const VkPresentInfoKHR*) {
92    ALOGE("VK_KHR_swapchain not enabled. vkQueuePresentKHR not executed.");
93    return VK_SUCCESS;
94}
95
96VKAPI_ATTR VkResult checkedQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) {
97    return (GetData(queue).hook_extensions[ProcHook::KHR_swapchain]) ? QueuePresentKHR(queue, pPresentInfo) : disabledQueuePresentKHR(queue, pPresentInfo);
98}
99
100VKAPI_ATTR VkResult disabledCreateAndroidSurfaceKHR(VkInstance, const VkAndroidSurfaceCreateInfoKHR*, const VkAllocationCallbacks*, VkSurfaceKHR*) {
101    ALOGE("VK_KHR_android_surface not enabled. vkCreateAndroidSurfaceKHR not executed.");
102    return VK_SUCCESS;
103}
104
105VKAPI_ATTR VkResult disabledCreateDebugReportCallbackEXT(VkInstance, const VkDebugReportCallbackCreateInfoEXT*, const VkAllocationCallbacks*, VkDebugReportCallbackEXT*) {
106    ALOGE("VK_EXT_debug_report not enabled. vkCreateDebugReportCallbackEXT not executed.");
107    return VK_SUCCESS;
108}
109
110VKAPI_ATTR void disabledDestroyDebugReportCallbackEXT(VkInstance, VkDebugReportCallbackEXT, const VkAllocationCallbacks*) {
111    ALOGE("VK_EXT_debug_report not enabled. vkDestroyDebugReportCallbackEXT not executed.");
112}
113
114VKAPI_ATTR void disabledDebugReportMessageEXT(VkInstance, VkDebugReportFlagsEXT, VkDebugReportObjectTypeEXT, uint64_t, size_t, int32_t, const char*, const char*) {
115    ALOGE("VK_EXT_debug_report not enabled. vkDebugReportMessageEXT not executed.");
116}
117
118// clang-format on
119
120const ProcHook g_proc_hooks[] = {
121    // clang-format off
122    {
123        "vkAcquireImageANDROID",
124        ProcHook::DEVICE,
125        ProcHook::ANDROID_native_buffer,
126        nullptr,
127        nullptr,
128        nullptr,
129    },
130    {
131        "vkAcquireNextImageKHR",
132        ProcHook::DEVICE,
133        ProcHook::KHR_swapchain,
134        reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImageKHR),
135        reinterpret_cast<PFN_vkVoidFunction>(disabledAcquireNextImageKHR),
136        reinterpret_cast<PFN_vkVoidFunction>(checkedAcquireNextImageKHR),
137    },
138    {
139        "vkAllocateCommandBuffers",
140        ProcHook::DEVICE,
141        ProcHook::EXTENSION_CORE,
142        reinterpret_cast<PFN_vkVoidFunction>(AllocateCommandBuffers),
143        nullptr,
144        nullptr,
145    },
146    {
147        "vkCreateAndroidSurfaceKHR",
148        ProcHook::INSTANCE,
149        ProcHook::KHR_android_surface,
150        reinterpret_cast<PFN_vkVoidFunction>(CreateAndroidSurfaceKHR),
151        reinterpret_cast<PFN_vkVoidFunction>(disabledCreateAndroidSurfaceKHR),
152        nullptr,
153    },
154    {
155        "vkCreateDebugReportCallbackEXT",
156        ProcHook::INSTANCE,
157        ProcHook::EXT_debug_report,
158        reinterpret_cast<PFN_vkVoidFunction>(CreateDebugReportCallbackEXT),
159        reinterpret_cast<PFN_vkVoidFunction>(disabledCreateDebugReportCallbackEXT),
160        nullptr,
161    },
162    {
163        "vkCreateDevice",
164        ProcHook::INSTANCE,
165        ProcHook::EXTENSION_CORE,
166        reinterpret_cast<PFN_vkVoidFunction>(CreateDevice),
167        nullptr,
168        nullptr,
169    },
170    {
171        "vkCreateInstance",
172        ProcHook::GLOBAL,
173        ProcHook::EXTENSION_CORE,
174        reinterpret_cast<PFN_vkVoidFunction>(CreateInstance),
175        nullptr,
176        nullptr,
177    },
178    {
179        "vkCreateSwapchainKHR",
180        ProcHook::DEVICE,
181        ProcHook::KHR_swapchain,
182        reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR),
183        reinterpret_cast<PFN_vkVoidFunction>(disabledCreateSwapchainKHR),
184        reinterpret_cast<PFN_vkVoidFunction>(checkedCreateSwapchainKHR),
185    },
186    {
187        "vkDebugReportMessageEXT",
188        ProcHook::INSTANCE,
189        ProcHook::EXT_debug_report,
190        reinterpret_cast<PFN_vkVoidFunction>(DebugReportMessageEXT),
191        reinterpret_cast<PFN_vkVoidFunction>(disabledDebugReportMessageEXT),
192        nullptr,
193    },
194    {
195        "vkDestroyDebugReportCallbackEXT",
196        ProcHook::INSTANCE,
197        ProcHook::EXT_debug_report,
198        reinterpret_cast<PFN_vkVoidFunction>(DestroyDebugReportCallbackEXT),
199        reinterpret_cast<PFN_vkVoidFunction>(disabledDestroyDebugReportCallbackEXT),
200        nullptr,
201    },
202    {
203        "vkDestroyDevice",
204        ProcHook::DEVICE,
205        ProcHook::EXTENSION_CORE,
206        reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice),
207        nullptr,
208        nullptr,
209    },
210    {
211        "vkDestroyInstance",
212        ProcHook::INSTANCE,
213        ProcHook::EXTENSION_CORE,
214        reinterpret_cast<PFN_vkVoidFunction>(DestroyInstance),
215        nullptr,
216        nullptr,
217    },
218    {
219        "vkDestroySurfaceKHR",
220        ProcHook::INSTANCE,
221        ProcHook::KHR_surface,
222        reinterpret_cast<PFN_vkVoidFunction>(DestroySurfaceKHR),
223        reinterpret_cast<PFN_vkVoidFunction>(disabledDestroySurfaceKHR),
224        nullptr,
225    },
226    {
227        "vkDestroySwapchainKHR",
228        ProcHook::DEVICE,
229        ProcHook::KHR_swapchain,
230        reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR),
231        reinterpret_cast<PFN_vkVoidFunction>(disabledDestroySwapchainKHR),
232        reinterpret_cast<PFN_vkVoidFunction>(checkedDestroySwapchainKHR),
233    },
234    {
235        "vkEnumerateDeviceExtensionProperties",
236        ProcHook::INSTANCE,
237        ProcHook::EXTENSION_CORE,
238        reinterpret_cast<PFN_vkVoidFunction>(EnumerateDeviceExtensionProperties),
239        nullptr,
240        nullptr,
241    },
242    {
243        "vkEnumerateInstanceExtensionProperties",
244        ProcHook::GLOBAL,
245        ProcHook::EXTENSION_CORE,
246        reinterpret_cast<PFN_vkVoidFunction>(EnumerateInstanceExtensionProperties),
247        nullptr,
248        nullptr,
249    },
250    {
251        "vkEnumeratePhysicalDevices",
252        ProcHook::INSTANCE,
253        ProcHook::EXTENSION_CORE,
254        reinterpret_cast<PFN_vkVoidFunction>(EnumeratePhysicalDevices),
255        nullptr,
256        nullptr,
257    },
258    {
259        "vkGetDeviceProcAddr",
260        ProcHook::DEVICE,
261        ProcHook::EXTENSION_CORE,
262        reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr),
263        nullptr,
264        nullptr,
265    },
266    {
267        "vkGetDeviceQueue",
268        ProcHook::DEVICE,
269        ProcHook::EXTENSION_CORE,
270        reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue),
271        nullptr,
272        nullptr,
273    },
274    {
275        "vkGetInstanceProcAddr",
276        ProcHook::INSTANCE,
277        ProcHook::EXTENSION_CORE,
278        reinterpret_cast<PFN_vkVoidFunction>(GetInstanceProcAddr),
279        nullptr,
280        nullptr,
281    },
282    {
283        "vkGetPhysicalDeviceSurfaceCapabilitiesKHR",
284        ProcHook::INSTANCE,
285        ProcHook::KHR_surface,
286        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceCapabilitiesKHR),
287        reinterpret_cast<PFN_vkVoidFunction>(disabledGetPhysicalDeviceSurfaceCapabilitiesKHR),
288        nullptr,
289    },
290    {
291        "vkGetPhysicalDeviceSurfaceFormatsKHR",
292        ProcHook::INSTANCE,
293        ProcHook::KHR_surface,
294        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceFormatsKHR),
295        reinterpret_cast<PFN_vkVoidFunction>(disabledGetPhysicalDeviceSurfaceFormatsKHR),
296        nullptr,
297    },
298    {
299        "vkGetPhysicalDeviceSurfacePresentModesKHR",
300        ProcHook::INSTANCE,
301        ProcHook::KHR_surface,
302        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfacePresentModesKHR),
303        reinterpret_cast<PFN_vkVoidFunction>(disabledGetPhysicalDeviceSurfacePresentModesKHR),
304        nullptr,
305    },
306    {
307        "vkGetPhysicalDeviceSurfaceSupportKHR",
308        ProcHook::INSTANCE,
309        ProcHook::KHR_surface,
310        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceSupportKHR),
311        reinterpret_cast<PFN_vkVoidFunction>(disabledGetPhysicalDeviceSurfaceSupportKHR),
312        nullptr,
313    },
314    {
315        "vkGetSwapchainGrallocUsageANDROID",
316        ProcHook::DEVICE,
317        ProcHook::ANDROID_native_buffer,
318        nullptr,
319        nullptr,
320        nullptr,
321    },
322    {
323        "vkGetSwapchainImagesKHR",
324        ProcHook::DEVICE,
325        ProcHook::KHR_swapchain,
326        reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR),
327        reinterpret_cast<PFN_vkVoidFunction>(disabledGetSwapchainImagesKHR),
328        reinterpret_cast<PFN_vkVoidFunction>(checkedGetSwapchainImagesKHR),
329    },
330    {
331        "vkQueuePresentKHR",
332        ProcHook::DEVICE,
333        ProcHook::KHR_swapchain,
334        reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR),
335        reinterpret_cast<PFN_vkVoidFunction>(disabledQueuePresentKHR),
336        reinterpret_cast<PFN_vkVoidFunction>(checkedQueuePresentKHR),
337    },
338    {
339        "vkQueueSignalReleaseImageANDROID",
340        ProcHook::DEVICE,
341        ProcHook::ANDROID_native_buffer,
342        nullptr,
343        nullptr,
344        nullptr,
345    },
346    // clang-format on
347};
348
349}  // anonymous
350
351const ProcHook* GetProcHook(const char* name) {
352    const auto& begin = g_proc_hooks;
353    const auto& end =
354        g_proc_hooks + sizeof(g_proc_hooks) / sizeof(g_proc_hooks[0]);
355    const auto hook = std::lower_bound(
356        begin, end, name,
357        [](const ProcHook& e, const char* n) { return strcmp(e.name, n) < 0; });
358    return (hook < end && strcmp(hook->name, name) == 0) ? hook : nullptr;
359}
360
361ProcHook::Extension GetProcHookExtension(const char* name) {
362    // clang-format off
363    if (strcmp(name, "VK_ANDROID_native_buffer") == 0) return ProcHook::ANDROID_native_buffer;
364    if (strcmp(name, "VK_EXT_debug_report") == 0) return ProcHook::EXT_debug_report;
365    if (strcmp(name, "VK_KHR_android_surface") == 0) return ProcHook::KHR_android_surface;
366    if (strcmp(name, "VK_KHR_surface") == 0) return ProcHook::KHR_surface;
367    if (strcmp(name, "VK_KHR_swapchain") == 0) return ProcHook::KHR_swapchain;
368    // clang-format on
369    return ProcHook::EXTENSION_UNKNOWN;
370}
371
372#define UNLIKELY(expr) __builtin_expect((expr), 0)
373
374#define INIT_PROC(obj, proc)                                           \
375    do {                                                               \
376        data.driver.proc =                                             \
377            reinterpret_cast<PFN_vk##proc>(get_proc(obj, "vk" #proc)); \
378        if (UNLIKELY(!data.driver.proc)) {                             \
379            ALOGE("missing " #obj " proc: vk" #proc);                  \
380            success = false;                                           \
381        }                                                              \
382    } while (0)
383
384#define INIT_PROC_EXT(ext, obj, proc)  \
385    do {                               \
386        if (extensions[ProcHook::ext]) \
387            INIT_PROC(obj, proc);      \
388    } while (0)
389
390bool InitDriverTable(VkInstance instance,
391                     PFN_vkGetInstanceProcAddr get_proc,
392                     const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
393    auto& data = GetData(instance);
394    bool success = true;
395
396    // clang-format off
397    INIT_PROC(instance, DestroyInstance);
398    INIT_PROC(instance, EnumeratePhysicalDevices);
399    INIT_PROC(instance, GetInstanceProcAddr);
400    INIT_PROC(instance, CreateDevice);
401    INIT_PROC(instance, EnumerateDeviceLayerProperties);
402    INIT_PROC(instance, EnumerateDeviceExtensionProperties);
403    INIT_PROC_EXT(EXT_debug_report, instance, CreateDebugReportCallbackEXT);
404    INIT_PROC_EXT(EXT_debug_report, instance, DestroyDebugReportCallbackEXT);
405    INIT_PROC_EXT(EXT_debug_report, instance, DebugReportMessageEXT);
406    // clang-format on
407
408    return success;
409}
410
411bool InitDriverTable(VkDevice dev,
412                     PFN_vkGetDeviceProcAddr get_proc,
413                     const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
414    auto& data = GetData(dev);
415    bool success = true;
416
417    // clang-format off
418    INIT_PROC(dev, GetDeviceProcAddr);
419    INIT_PROC(dev, DestroyDevice);
420    INIT_PROC(dev, GetDeviceQueue);
421    INIT_PROC(dev, CreateImage);
422    INIT_PROC(dev, DestroyImage);
423    INIT_PROC(dev, AllocateCommandBuffers);
424    INIT_PROC_EXT(ANDROID_native_buffer, dev, GetSwapchainGrallocUsageANDROID);
425    INIT_PROC_EXT(ANDROID_native_buffer, dev, AcquireImageANDROID);
426    INIT_PROC_EXT(ANDROID_native_buffer, dev, QueueSignalReleaseImageANDROID);
427    // clang-format on
428
429    return success;
430}
431
432}  // namespace driver
433}  // namespace vulkan
434
435// clang-format on
436