driver_gen.cpp revision a5e161b1207ef447a51e99856097d69d4a6111e1
1/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// WARNING: This file is generated. See ../README.md for instructions.
18
19#include <string.h>
20
21#include <algorithm>
22
23#include <android/log.h>
24
25#include "driver.h"
26
27namespace vulkan {
28namespace driver {
29
30namespace {
31
32// clang-format off
33
34VKAPI_ATTR VkResult checkedCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkSwapchainKHR* pSwapchain) {
35    if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
36        return CreateSwapchainKHR(device, pCreateInfo, pAllocator, pSwapchain);
37    } else {
38        Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkCreateSwapchainKHR not executed.");
39        return VK_SUCCESS;
40    }
41}
42
43VKAPI_ATTR void checkedDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* pAllocator) {
44    if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
45        DestroySwapchainKHR(device, swapchain, pAllocator);
46    } else {
47        Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkDestroySwapchainKHR not executed.");
48    }
49}
50
51VKAPI_ATTR VkResult checkedGetSwapchainImagesKHR(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pSwapchainImageCount, VkImage* pSwapchainImages) {
52    if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
53        return GetSwapchainImagesKHR(device, swapchain, pSwapchainImageCount, pSwapchainImages);
54    } else {
55        Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkGetSwapchainImagesKHR not executed.");
56        return VK_SUCCESS;
57    }
58}
59
60VKAPI_ATTR VkResult checkedAcquireNextImageKHR(VkDevice device, VkSwapchainKHR swapchain, uint64_t timeout, VkSemaphore semaphore, VkFence fence, uint32_t* pImageIndex) {
61    if (GetData(device).hook_extensions[ProcHook::KHR_swapchain]) {
62        return AcquireNextImageKHR(device, swapchain, timeout, semaphore, fence, pImageIndex);
63    } else {
64        Logger(device).Err(device, "VK_KHR_swapchain not enabled. vkAcquireNextImageKHR not executed.");
65        return VK_SUCCESS;
66    }
67}
68
69VKAPI_ATTR VkResult checkedQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) {
70    if (GetData(queue).hook_extensions[ProcHook::KHR_swapchain]) {
71        return QueuePresentKHR(queue, pPresentInfo);
72    } else {
73        Logger(queue).Err(queue, "VK_KHR_swapchain not enabled. vkQueuePresentKHR not executed.");
74        return VK_SUCCESS;
75    }
76}
77
78// clang-format on
79
80const ProcHook g_proc_hooks[] = {
81    // clang-format off
82    {
83        "vkAcquireImageANDROID",
84        ProcHook::DEVICE,
85        ProcHook::ANDROID_native_buffer,
86        nullptr,
87        nullptr,
88    },
89    {
90        "vkAcquireNextImageKHR",
91        ProcHook::DEVICE,
92        ProcHook::KHR_swapchain,
93        reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImageKHR),
94        reinterpret_cast<PFN_vkVoidFunction>(checkedAcquireNextImageKHR),
95    },
96    {
97        "vkAllocateCommandBuffers",
98        ProcHook::DEVICE,
99        ProcHook::EXTENSION_CORE,
100        reinterpret_cast<PFN_vkVoidFunction>(AllocateCommandBuffers),
101        nullptr,
102    },
103    {
104        "vkCreateAndroidSurfaceKHR",
105        ProcHook::INSTANCE,
106        ProcHook::KHR_android_surface,
107        reinterpret_cast<PFN_vkVoidFunction>(CreateAndroidSurfaceKHR),
108        nullptr,
109    },
110    {
111        "vkCreateDebugReportCallbackEXT",
112        ProcHook::INSTANCE,
113        ProcHook::EXT_debug_report,
114        reinterpret_cast<PFN_vkVoidFunction>(CreateDebugReportCallbackEXT),
115        nullptr,
116    },
117    {
118        "vkCreateDevice",
119        ProcHook::INSTANCE,
120        ProcHook::EXTENSION_CORE,
121        reinterpret_cast<PFN_vkVoidFunction>(CreateDevice),
122        nullptr,
123    },
124    {
125        "vkCreateInstance",
126        ProcHook::GLOBAL,
127        ProcHook::EXTENSION_CORE,
128        reinterpret_cast<PFN_vkVoidFunction>(CreateInstance),
129        nullptr,
130    },
131    {
132        "vkCreateSwapchainKHR",
133        ProcHook::DEVICE,
134        ProcHook::KHR_swapchain,
135        reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR),
136        reinterpret_cast<PFN_vkVoidFunction>(checkedCreateSwapchainKHR),
137    },
138    {
139        "vkDebugReportMessageEXT",
140        ProcHook::INSTANCE,
141        ProcHook::EXT_debug_report,
142        reinterpret_cast<PFN_vkVoidFunction>(DebugReportMessageEXT),
143        nullptr,
144    },
145    {
146        "vkDestroyDebugReportCallbackEXT",
147        ProcHook::INSTANCE,
148        ProcHook::EXT_debug_report,
149        reinterpret_cast<PFN_vkVoidFunction>(DestroyDebugReportCallbackEXT),
150        nullptr,
151    },
152    {
153        "vkDestroyDevice",
154        ProcHook::DEVICE,
155        ProcHook::EXTENSION_CORE,
156        reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice),
157        nullptr,
158    },
159    {
160        "vkDestroyInstance",
161        ProcHook::INSTANCE,
162        ProcHook::EXTENSION_CORE,
163        reinterpret_cast<PFN_vkVoidFunction>(DestroyInstance),
164        nullptr,
165    },
166    {
167        "vkDestroySurfaceKHR",
168        ProcHook::INSTANCE,
169        ProcHook::KHR_surface,
170        reinterpret_cast<PFN_vkVoidFunction>(DestroySurfaceKHR),
171        nullptr,
172    },
173    {
174        "vkDestroySwapchainKHR",
175        ProcHook::DEVICE,
176        ProcHook::KHR_swapchain,
177        reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR),
178        reinterpret_cast<PFN_vkVoidFunction>(checkedDestroySwapchainKHR),
179    },
180    {
181        "vkEnumerateDeviceExtensionProperties",
182        ProcHook::INSTANCE,
183        ProcHook::EXTENSION_CORE,
184        reinterpret_cast<PFN_vkVoidFunction>(EnumerateDeviceExtensionProperties),
185        nullptr,
186    },
187    {
188        "vkEnumerateInstanceExtensionProperties",
189        ProcHook::GLOBAL,
190        ProcHook::EXTENSION_CORE,
191        reinterpret_cast<PFN_vkVoidFunction>(EnumerateInstanceExtensionProperties),
192        nullptr,
193    },
194    {
195        "vkEnumeratePhysicalDevices",
196        ProcHook::INSTANCE,
197        ProcHook::EXTENSION_CORE,
198        reinterpret_cast<PFN_vkVoidFunction>(EnumeratePhysicalDevices),
199        nullptr,
200    },
201    {
202        "vkGetDeviceProcAddr",
203        ProcHook::DEVICE,
204        ProcHook::EXTENSION_CORE,
205        reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr),
206        nullptr,
207    },
208    {
209        "vkGetDeviceQueue",
210        ProcHook::DEVICE,
211        ProcHook::EXTENSION_CORE,
212        reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue),
213        nullptr,
214    },
215    {
216        "vkGetInstanceProcAddr",
217        ProcHook::INSTANCE,
218        ProcHook::EXTENSION_CORE,
219        reinterpret_cast<PFN_vkVoidFunction>(GetInstanceProcAddr),
220        nullptr,
221    },
222    {
223        "vkGetPhysicalDeviceSurfaceCapabilitiesKHR",
224        ProcHook::INSTANCE,
225        ProcHook::KHR_surface,
226        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceCapabilitiesKHR),
227        nullptr,
228    },
229    {
230        "vkGetPhysicalDeviceSurfaceFormatsKHR",
231        ProcHook::INSTANCE,
232        ProcHook::KHR_surface,
233        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceFormatsKHR),
234        nullptr,
235    },
236    {
237        "vkGetPhysicalDeviceSurfacePresentModesKHR",
238        ProcHook::INSTANCE,
239        ProcHook::KHR_surface,
240        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfacePresentModesKHR),
241        nullptr,
242    },
243    {
244        "vkGetPhysicalDeviceSurfaceSupportKHR",
245        ProcHook::INSTANCE,
246        ProcHook::KHR_surface,
247        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceSupportKHR),
248        nullptr,
249    },
250    {
251        "vkGetSwapchainGrallocUsageANDROID",
252        ProcHook::DEVICE,
253        ProcHook::ANDROID_native_buffer,
254        nullptr,
255        nullptr,
256    },
257    {
258        "vkGetSwapchainImagesKHR",
259        ProcHook::DEVICE,
260        ProcHook::KHR_swapchain,
261        reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR),
262        reinterpret_cast<PFN_vkVoidFunction>(checkedGetSwapchainImagesKHR),
263    },
264    {
265        "vkQueuePresentKHR",
266        ProcHook::DEVICE,
267        ProcHook::KHR_swapchain,
268        reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR),
269        reinterpret_cast<PFN_vkVoidFunction>(checkedQueuePresentKHR),
270    },
271    {
272        "vkQueueSignalReleaseImageANDROID",
273        ProcHook::DEVICE,
274        ProcHook::ANDROID_native_buffer,
275        nullptr,
276        nullptr,
277    },
278    // clang-format on
279};
280
281}  // anonymous
282
283const ProcHook* GetProcHook(const char* name) {
284    const auto& begin = g_proc_hooks;
285    const auto& end =
286        g_proc_hooks + sizeof(g_proc_hooks) / sizeof(g_proc_hooks[0]);
287    const auto hook = std::lower_bound(
288        begin, end, name,
289        [](const ProcHook& e, const char* n) { return strcmp(e.name, n) < 0; });
290    return (hook < end && strcmp(hook->name, name) == 0) ? hook : nullptr;
291}
292
293ProcHook::Extension GetProcHookExtension(const char* name) {
294    // clang-format off
295    if (strcmp(name, "VK_ANDROID_native_buffer") == 0) return ProcHook::ANDROID_native_buffer;
296    if (strcmp(name, "VK_EXT_debug_report") == 0) return ProcHook::EXT_debug_report;
297    if (strcmp(name, "VK_KHR_android_surface") == 0) return ProcHook::KHR_android_surface;
298    if (strcmp(name, "VK_KHR_surface") == 0) return ProcHook::KHR_surface;
299    if (strcmp(name, "VK_KHR_swapchain") == 0) return ProcHook::KHR_swapchain;
300    // clang-format on
301    return ProcHook::EXTENSION_UNKNOWN;
302}
303
304#define UNLIKELY(expr) __builtin_expect((expr), 0)
305
306#define INIT_PROC(obj, proc)                                           \
307    do {                                                               \
308        data.driver.proc =                                             \
309            reinterpret_cast<PFN_vk##proc>(get_proc(obj, "vk" #proc)); \
310        if (UNLIKELY(!data.driver.proc)) {                             \
311            ALOGE("missing " #obj " proc: vk" #proc);                  \
312            success = false;                                           \
313        }                                                              \
314    } while (0)
315
316#define INIT_PROC_EXT(ext, obj, proc)  \
317    do {                               \
318        if (extensions[ProcHook::ext]) \
319            INIT_PROC(obj, proc);      \
320    } while (0)
321
322bool InitDriverTable(VkInstance instance,
323                     PFN_vkGetInstanceProcAddr get_proc,
324                     const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
325    auto& data = GetData(instance);
326    bool success = true;
327
328    // clang-format off
329    INIT_PROC(instance, DestroyInstance);
330    INIT_PROC(instance, EnumeratePhysicalDevices);
331    INIT_PROC(instance, GetInstanceProcAddr);
332    INIT_PROC(instance, CreateDevice);
333    INIT_PROC(instance, EnumerateDeviceExtensionProperties);
334    INIT_PROC_EXT(EXT_debug_report, instance, CreateDebugReportCallbackEXT);
335    INIT_PROC_EXT(EXT_debug_report, instance, DestroyDebugReportCallbackEXT);
336    INIT_PROC_EXT(EXT_debug_report, instance, DebugReportMessageEXT);
337    // clang-format on
338
339    return success;
340}
341
342bool InitDriverTable(VkDevice dev,
343                     PFN_vkGetDeviceProcAddr get_proc,
344                     const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
345    auto& data = GetData(dev);
346    bool success = true;
347
348    // clang-format off
349    INIT_PROC(dev, GetDeviceProcAddr);
350    INIT_PROC(dev, DestroyDevice);
351    INIT_PROC(dev, GetDeviceQueue);
352    INIT_PROC(dev, CreateImage);
353    INIT_PROC(dev, DestroyImage);
354    INIT_PROC(dev, AllocateCommandBuffers);
355    INIT_PROC_EXT(ANDROID_native_buffer, dev, GetSwapchainGrallocUsageANDROID);
356    INIT_PROC_EXT(ANDROID_native_buffer, dev, AcquireImageANDROID);
357    INIT_PROC_EXT(ANDROID_native_buffer, dev, QueueSignalReleaseImageANDROID);
358    // clang-format on
359
360    return success;
361}
362
363}  // namespace driver
364}  // namespace vulkan
365
366// clang-format on
367