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