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