driver_gen.cpp revision 48853713e328ee23884d2127ca763352e0681944
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 <log/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
78VKAPI_ATTR VkResult checkedGetRefreshCycleDurationGOOGLE(VkDevice device, VkSwapchainKHR swapchain, VkRefreshCycleDurationGOOGLE* pDisplayTimingProperties) {
79    if (GetData(device).hook_extensions[ProcHook::GOOGLE_display_timing]) {
80        return GetRefreshCycleDurationGOOGLE(device, swapchain, pDisplayTimingProperties);
81    } else {
82        Logger(device).Err(device, "VK_GOOGLE_display_timing not enabled. vkGetRefreshCycleDurationGOOGLE not executed.");
83        return VK_SUCCESS;
84    }
85}
86
87VKAPI_ATTR VkResult checkedGetPastPresentationTimingGOOGLE(VkDevice device, VkSwapchainKHR swapchain, uint32_t* pPresentationTimingCount, VkPastPresentationTimingGOOGLE* pPresentationTimings) {
88    if (GetData(device).hook_extensions[ProcHook::GOOGLE_display_timing]) {
89        return GetPastPresentationTimingGOOGLE(device, swapchain, pPresentationTimingCount, pPresentationTimings);
90    } else {
91        Logger(device).Err(device, "VK_GOOGLE_display_timing not enabled. vkGetPastPresentationTimingGOOGLE not executed.");
92        return VK_SUCCESS;
93    }
94}
95
96// clang-format on
97
98const ProcHook g_proc_hooks[] = {
99    // clang-format off
100    {
101        "vkAcquireImageANDROID",
102        ProcHook::DEVICE,
103        ProcHook::ANDROID_native_buffer,
104        nullptr,
105        nullptr,
106    },
107    {
108        "vkAcquireNextImageKHR",
109        ProcHook::DEVICE,
110        ProcHook::KHR_swapchain,
111        reinterpret_cast<PFN_vkVoidFunction>(AcquireNextImageKHR),
112        reinterpret_cast<PFN_vkVoidFunction>(checkedAcquireNextImageKHR),
113    },
114    {
115        "vkAllocateCommandBuffers",
116        ProcHook::DEVICE,
117        ProcHook::EXTENSION_CORE,
118        reinterpret_cast<PFN_vkVoidFunction>(AllocateCommandBuffers),
119        nullptr,
120    },
121    {
122        "vkCreateAndroidSurfaceKHR",
123        ProcHook::INSTANCE,
124        ProcHook::KHR_android_surface,
125        reinterpret_cast<PFN_vkVoidFunction>(CreateAndroidSurfaceKHR),
126        nullptr,
127    },
128    {
129        "vkCreateDebugReportCallbackEXT",
130        ProcHook::INSTANCE,
131        ProcHook::EXT_debug_report,
132        reinterpret_cast<PFN_vkVoidFunction>(CreateDebugReportCallbackEXT),
133        nullptr,
134    },
135    {
136        "vkCreateDevice",
137        ProcHook::INSTANCE,
138        ProcHook::EXTENSION_CORE,
139        reinterpret_cast<PFN_vkVoidFunction>(CreateDevice),
140        nullptr,
141    },
142    {
143        "vkCreateInstance",
144        ProcHook::GLOBAL,
145        ProcHook::EXTENSION_CORE,
146        reinterpret_cast<PFN_vkVoidFunction>(CreateInstance),
147        nullptr,
148    },
149    {
150        "vkCreateSwapchainKHR",
151        ProcHook::DEVICE,
152        ProcHook::KHR_swapchain,
153        reinterpret_cast<PFN_vkVoidFunction>(CreateSwapchainKHR),
154        reinterpret_cast<PFN_vkVoidFunction>(checkedCreateSwapchainKHR),
155    },
156    {
157        "vkDebugReportMessageEXT",
158        ProcHook::INSTANCE,
159        ProcHook::EXT_debug_report,
160        reinterpret_cast<PFN_vkVoidFunction>(DebugReportMessageEXT),
161        nullptr,
162    },
163    {
164        "vkDestroyDebugReportCallbackEXT",
165        ProcHook::INSTANCE,
166        ProcHook::EXT_debug_report,
167        reinterpret_cast<PFN_vkVoidFunction>(DestroyDebugReportCallbackEXT),
168        nullptr,
169    },
170    {
171        "vkDestroyDevice",
172        ProcHook::DEVICE,
173        ProcHook::EXTENSION_CORE,
174        reinterpret_cast<PFN_vkVoidFunction>(DestroyDevice),
175        nullptr,
176    },
177    {
178        "vkDestroyInstance",
179        ProcHook::INSTANCE,
180        ProcHook::EXTENSION_CORE,
181        reinterpret_cast<PFN_vkVoidFunction>(DestroyInstance),
182        nullptr,
183    },
184    {
185        "vkDestroySurfaceKHR",
186        ProcHook::INSTANCE,
187        ProcHook::KHR_surface,
188        reinterpret_cast<PFN_vkVoidFunction>(DestroySurfaceKHR),
189        nullptr,
190    },
191    {
192        "vkDestroySwapchainKHR",
193        ProcHook::DEVICE,
194        ProcHook::KHR_swapchain,
195        reinterpret_cast<PFN_vkVoidFunction>(DestroySwapchainKHR),
196        reinterpret_cast<PFN_vkVoidFunction>(checkedDestroySwapchainKHR),
197    },
198    {
199        "vkEnumerateDeviceExtensionProperties",
200        ProcHook::INSTANCE,
201        ProcHook::EXTENSION_CORE,
202        reinterpret_cast<PFN_vkVoidFunction>(EnumerateDeviceExtensionProperties),
203        nullptr,
204    },
205    {
206        "vkEnumerateInstanceExtensionProperties",
207        ProcHook::GLOBAL,
208        ProcHook::EXTENSION_CORE,
209        reinterpret_cast<PFN_vkVoidFunction>(EnumerateInstanceExtensionProperties),
210        nullptr,
211    },
212    {
213        "vkEnumeratePhysicalDevices",
214        ProcHook::INSTANCE,
215        ProcHook::EXTENSION_CORE,
216        reinterpret_cast<PFN_vkVoidFunction>(EnumeratePhysicalDevices),
217        nullptr,
218    },
219    {
220        "vkGetDeviceProcAddr",
221        ProcHook::DEVICE,
222        ProcHook::EXTENSION_CORE,
223        reinterpret_cast<PFN_vkVoidFunction>(GetDeviceProcAddr),
224        nullptr,
225    },
226    {
227        "vkGetDeviceQueue",
228        ProcHook::DEVICE,
229        ProcHook::EXTENSION_CORE,
230        reinterpret_cast<PFN_vkVoidFunction>(GetDeviceQueue),
231        nullptr,
232    },
233    {
234        "vkGetInstanceProcAddr",
235        ProcHook::INSTANCE,
236        ProcHook::EXTENSION_CORE,
237        reinterpret_cast<PFN_vkVoidFunction>(GetInstanceProcAddr),
238        nullptr,
239    },
240    {
241        "vkGetPastPresentationTimingGOOGLE",
242        ProcHook::DEVICE,
243        ProcHook::GOOGLE_display_timing,
244        reinterpret_cast<PFN_vkVoidFunction>(GetPastPresentationTimingGOOGLE),
245        reinterpret_cast<PFN_vkVoidFunction>(checkedGetPastPresentationTimingGOOGLE),
246    },
247    {
248        "vkGetPhysicalDeviceSurfaceCapabilitiesKHR",
249        ProcHook::INSTANCE,
250        ProcHook::KHR_surface,
251        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceCapabilitiesKHR),
252        nullptr,
253    },
254    {
255        "vkGetPhysicalDeviceSurfaceFormatsKHR",
256        ProcHook::INSTANCE,
257        ProcHook::KHR_surface,
258        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceFormatsKHR),
259        nullptr,
260    },
261    {
262        "vkGetPhysicalDeviceSurfacePresentModesKHR",
263        ProcHook::INSTANCE,
264        ProcHook::KHR_surface,
265        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfacePresentModesKHR),
266        nullptr,
267    },
268    {
269        "vkGetPhysicalDeviceSurfaceSupportKHR",
270        ProcHook::INSTANCE,
271        ProcHook::KHR_surface,
272        reinterpret_cast<PFN_vkVoidFunction>(GetPhysicalDeviceSurfaceSupportKHR),
273        nullptr,
274    },
275    {
276        "vkGetRefreshCycleDurationGOOGLE",
277        ProcHook::DEVICE,
278        ProcHook::GOOGLE_display_timing,
279        reinterpret_cast<PFN_vkVoidFunction>(GetRefreshCycleDurationGOOGLE),
280        reinterpret_cast<PFN_vkVoidFunction>(checkedGetRefreshCycleDurationGOOGLE),
281    },
282    {
283        "vkGetSwapchainGrallocUsage2ANDROID",
284        ProcHook::DEVICE,
285        ProcHook::ANDROID_native_buffer,
286        nullptr,
287        nullptr,
288    },
289    {
290        "vkGetSwapchainGrallocUsageANDROID",
291        ProcHook::DEVICE,
292        ProcHook::ANDROID_native_buffer,
293        nullptr,
294        nullptr,
295    },
296    {
297        "vkGetSwapchainImagesKHR",
298        ProcHook::DEVICE,
299        ProcHook::KHR_swapchain,
300        reinterpret_cast<PFN_vkVoidFunction>(GetSwapchainImagesKHR),
301        reinterpret_cast<PFN_vkVoidFunction>(checkedGetSwapchainImagesKHR),
302    },
303    {
304        "vkQueuePresentKHR",
305        ProcHook::DEVICE,
306        ProcHook::KHR_swapchain,
307        reinterpret_cast<PFN_vkVoidFunction>(QueuePresentKHR),
308        reinterpret_cast<PFN_vkVoidFunction>(checkedQueuePresentKHR),
309    },
310    {
311        "vkQueueSignalReleaseImageANDROID",
312        ProcHook::DEVICE,
313        ProcHook::ANDROID_native_buffer,
314        nullptr,
315        nullptr,
316    },
317    // clang-format on
318};
319
320}  // anonymous
321
322const ProcHook* GetProcHook(const char* name) {
323    const auto& begin = g_proc_hooks;
324    const auto& end =
325        g_proc_hooks + sizeof(g_proc_hooks) / sizeof(g_proc_hooks[0]);
326    const auto hook = std::lower_bound(
327        begin, end, name,
328        [](const ProcHook& e, const char* n) { return strcmp(e.name, n) < 0; });
329    return (hook < end && strcmp(hook->name, name) == 0) ? hook : nullptr;
330}
331
332ProcHook::Extension GetProcHookExtension(const char* name) {
333    // clang-format off
334    if (strcmp(name, "VK_ANDROID_native_buffer") == 0) return ProcHook::ANDROID_native_buffer;
335    if (strcmp(name, "VK_EXT_debug_report") == 0) return ProcHook::EXT_debug_report;
336    if (strcmp(name, "VK_KHR_android_surface") == 0) return ProcHook::KHR_android_surface;
337    if (strcmp(name, "VK_KHR_incremental_present") == 0) return ProcHook::KHR_incremental_present;
338    if (strcmp(name, "VK_KHR_surface") == 0) return ProcHook::KHR_surface;
339    if (strcmp(name, "VK_KHR_swapchain") == 0) return ProcHook::KHR_swapchain;
340    if (strcmp(name, "VK_GOOGLE_display_timing") == 0) return ProcHook::GOOGLE_display_timing;
341    // clang-format on
342    return ProcHook::EXTENSION_UNKNOWN;
343}
344
345#define UNLIKELY(expr) __builtin_expect((expr), 0)
346
347#define INIT_PROC(required, obj, proc)                                 \
348    do {                                                               \
349        data.driver.proc =                                             \
350            reinterpret_cast<PFN_vk##proc>(get_proc(obj, "vk" #proc)); \
351        if (UNLIKELY(required && !data.driver.proc)) {                 \
352            ALOGE("missing " #obj " proc: vk" #proc);                  \
353            success = false;                                           \
354        }                                                              \
355    } while (0)
356
357#define INIT_PROC_EXT(ext, required, obj, proc) \
358    do {                                        \
359        if (extensions[ProcHook::ext])          \
360            INIT_PROC(required, obj, proc);     \
361    } while (0)
362
363bool InitDriverTable(VkInstance instance,
364                     PFN_vkGetInstanceProcAddr get_proc,
365                     const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
366    auto& data = GetData(instance);
367    bool success = true;
368
369    // clang-format off
370    INIT_PROC(true, instance, DestroyInstance);
371    INIT_PROC(true, instance, EnumeratePhysicalDevices);
372    INIT_PROC(true, instance, GetInstanceProcAddr);
373    INIT_PROC(true, instance, CreateDevice);
374    INIT_PROC(true, instance, EnumerateDeviceExtensionProperties);
375    INIT_PROC_EXT(EXT_debug_report, true, instance, CreateDebugReportCallbackEXT);
376    INIT_PROC_EXT(EXT_debug_report, true, instance, DestroyDebugReportCallbackEXT);
377    INIT_PROC_EXT(EXT_debug_report, true, instance, DebugReportMessageEXT);
378    // clang-format on
379
380    return success;
381}
382
383bool InitDriverTable(VkDevice dev,
384                     PFN_vkGetDeviceProcAddr get_proc,
385                     const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
386    auto& data = GetData(dev);
387    bool success = true;
388
389    // clang-format off
390    INIT_PROC(true, dev, GetDeviceProcAddr);
391    INIT_PROC(true, dev, DestroyDevice);
392    INIT_PROC(true, dev, GetDeviceQueue);
393    INIT_PROC(true, dev, CreateImage);
394    INIT_PROC(true, dev, DestroyImage);
395    INIT_PROC(true, dev, AllocateCommandBuffers);
396    INIT_PROC_EXT(ANDROID_native_buffer, true, dev, GetSwapchainGrallocUsageANDROID);
397    INIT_PROC_EXT(ANDROID_native_buffer, false, dev, GetSwapchainGrallocUsage2ANDROID);
398    INIT_PROC_EXT(ANDROID_native_buffer, true, dev, AcquireImageANDROID);
399    INIT_PROC_EXT(ANDROID_native_buffer, true, dev, QueueSignalReleaseImageANDROID);
400    // clang-format on
401
402    return success;
403}
404
405}  // namespace driver
406}  // namespace vulkan
407
408// clang-format on
409