driver.h revision c3a28913b6a95d2faee0db537c48557e04267511
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#ifndef LIBVULKAN_DRIVER_H
18#define LIBVULKAN_DRIVER_H 1
19
20#include <inttypes.h>
21#include <bitset>
22#include <type_traits>
23#include <log/log.h>
24
25#include <vulkan/vulkan.h>
26#include <hardware/hwvulkan.h>
27
28#include "api_gen.h"
29#include "driver_gen.h"
30#include "debug_report.h"
31#include "swapchain.h"
32
33namespace vulkan {
34
35// This is here so that we can embed api::{Instance,Device}Data in
36// driver::{Instance,Device}Data to avoid pointer chasing.  They are
37// considered opaque to the driver layer.
38namespace api {
39
40struct InstanceData {
41    InstanceDispatchTable dispatch;
42
43    // LayerChain::ActiveLayer array
44    void* layers;
45    uint32_t layer_count;
46
47    // debug.vulkan.enable_callback
48    PFN_vkDestroyDebugReportCallbackEXT destroy_debug_callback;
49    VkDebugReportCallbackEXT debug_callback;
50};
51
52struct DeviceData {
53    DeviceDispatchTable dispatch;
54};
55
56}  // namespace api
57
58namespace driver {
59
60VK_DEFINE_HANDLE(InstanceDispatchable)
61VK_DEFINE_HANDLE(DeviceDispatchable)
62
63struct InstanceData {
64    InstanceData(const VkAllocationCallbacks& alloc)
65        : opaque_api_data(),
66          allocator(alloc),
67          driver(),
68          get_device_proc_addr(nullptr) {
69        hook_extensions.set(ProcHook::EXTENSION_CORE);
70    }
71
72    api::InstanceData opaque_api_data;
73
74    const VkAllocationCallbacks allocator;
75
76    std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
77
78    InstanceDriverTable driver;
79    PFN_vkGetDeviceProcAddr get_device_proc_addr;
80
81    DebugReportCallbackList debug_report_callbacks;
82};
83
84struct DeviceData {
85    DeviceData(const VkAllocationCallbacks& alloc)
86        : opaque_api_data(), allocator(alloc), driver() {
87        hook_extensions.set(ProcHook::EXTENSION_CORE);
88    }
89
90    api::DeviceData opaque_api_data;
91
92    const VkAllocationCallbacks allocator;
93
94    std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
95
96    DeviceDriverTable driver;
97};
98
99bool Debuggable();
100bool OpenHAL();
101const VkAllocationCallbacks& GetDefaultAllocator();
102
103// clang-format off
104VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName);
105VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName);
106VKAPI_ATTR VkResult EnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
107
108VKAPI_ATTR VkResult EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
109
110VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance);
111VKAPI_ATTR void DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator);
112VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice);
113VKAPI_ATTR void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator);
114
115VKAPI_ATTR VkResult EnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices);
116VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);
117VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers);
118// clang-format on
119
120template <typename DispatchableType>
121void StaticAssertDispatchable(DispatchableType) {
122    static_assert(
123        std::is_same<DispatchableType, VkInstance>::value ||
124            std::is_same<DispatchableType, VkPhysicalDevice>::value ||
125            std::is_same<DispatchableType, VkDevice>::value ||
126            std::is_same<DispatchableType, InstanceDispatchable>::value ||
127            std::is_same<DispatchableType, VkQueue>::value ||
128            std::is_same<DispatchableType, VkCommandBuffer>::value ||
129            std::is_same<DispatchableType, DeviceDispatchable>::value,
130        "unrecognized dispatchable type");
131}
132
133template <typename DispatchableType>
134bool SetDataInternal(DispatchableType dispatchable, const void* data) {
135    StaticAssertDispatchable(dispatchable);
136
137    hwvulkan_dispatch_t* dispatch =
138        reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable);
139    // must be magic or already set
140    if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) {
141        ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic);
142        return false;
143    }
144
145    dispatch->vtbl = data;
146
147    return true;
148}
149
150template <typename DispatchableType>
151void* GetDataInternal(DispatchableType dispatchable) {
152    StaticAssertDispatchable(dispatchable);
153
154    const hwvulkan_dispatch_t* dispatch =
155        reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable);
156
157    return const_cast<void*>(dispatch->vtbl);
158}
159
160inline bool SetData(VkInstance instance, const InstanceData& data) {
161    return SetDataInternal(instance, &data);
162}
163
164inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) {
165    return SetDataInternal(physical_dev, &data);
166}
167
168inline bool SetData(InstanceDispatchable dispatchable,
169                    const InstanceData& data) {
170    return SetDataInternal(dispatchable, &data);
171}
172
173inline bool SetData(VkDevice dev, const DeviceData& data) {
174    return SetDataInternal(dev, &data);
175}
176
177inline bool SetData(VkQueue queue, const DeviceData& data) {
178    return SetDataInternal(queue, &data);
179}
180
181inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) {
182    return SetDataInternal(cmd, &data);
183}
184
185inline bool SetData(DeviceDispatchable dispatchable, const DeviceData& data) {
186    return SetDataInternal(dispatchable, &data);
187}
188
189inline InstanceData& GetData(VkInstance instance) {
190    return *reinterpret_cast<InstanceData*>(GetDataInternal(instance));
191}
192
193inline InstanceData& GetData(VkPhysicalDevice physical_dev) {
194    return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev));
195}
196
197inline InstanceData& GetData(InstanceDispatchable dispatchable) {
198    return *reinterpret_cast<InstanceData*>(GetDataInternal(dispatchable));
199}
200
201inline DeviceData& GetData(VkDevice dev) {
202    return *reinterpret_cast<DeviceData*>(GetDataInternal(dev));
203}
204
205inline DeviceData& GetData(VkQueue queue) {
206    return *reinterpret_cast<DeviceData*>(GetDataInternal(queue));
207}
208
209inline DeviceData& GetData(VkCommandBuffer cmd) {
210    return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd));
211}
212
213inline DeviceData& GetData(DeviceDispatchable dispatchable) {
214    return *reinterpret_cast<DeviceData*>(GetDataInternal(dispatchable));
215}
216
217}  // namespace driver
218}  // namespace vulkan
219
220#endif  // LIBVULKAN_DRIVER_H
221