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 VkPhysicalDevice driver_physical_device;
102 };
103
104 bool OpenHAL();
105 const VkAllocationCallbacks& GetDefaultAllocator();
106
107 void QueryPresentationProperties(
108 VkPhysicalDevice physicalDevice,
109 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties);
110
111 bool GetAndroidNativeBufferSpecVersion9Support(VkPhysicalDevice physicalDevice);
112
113 VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance,
114 const char* pName);
115 VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device,
116 const char* pName);
117 VKAPI_ATTR VkResult
118 EnumerateInstanceExtensionProperties(const char* pLayerName,
119 uint32_t* pPropertyCount,
120 VkExtensionProperties* pProperties);
121 VKAPI_ATTR VkResult
122 EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,
123 const char* pLayerName,
124 uint32_t* pPropertyCount,
125 VkExtensionProperties* pProperties);
126 VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
127 const VkAllocationCallbacks* pAllocator,
128 VkInstance* pInstance);
129 VKAPI_ATTR void DestroyInstance(VkInstance instance,
130 const VkAllocationCallbacks* pAllocator);
131 VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice,
132 const VkDeviceCreateInfo* pCreateInfo,
133 const VkAllocationCallbacks* pAllocator,
134 VkDevice* pDevice);
135 VKAPI_ATTR void DestroyDevice(VkDevice device,
136 const VkAllocationCallbacks* pAllocator);
137 VKAPI_ATTR VkResult
138 EnumeratePhysicalDevices(VkInstance instance,
139 uint32_t* pPhysicalDeviceCount,
140 VkPhysicalDevice* pPhysicalDevices);
141 VKAPI_ATTR VkResult EnumeratePhysicalDeviceGroups(
142 VkInstance instance,
143 uint32_t* pPhysicalDeviceGroupCount,
144 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties);
145 VKAPI_ATTR void GetDeviceQueue(VkDevice device,
146 uint32_t queueFamilyIndex,
147 uint32_t queueIndex,
148 VkQueue* pQueue);
149 VKAPI_ATTR void GetDeviceQueue2(VkDevice device,
150 const VkDeviceQueueInfo2* pQueueInfo,
151 VkQueue* pQueue);
152 VKAPI_ATTR VkResult
153 AllocateCommandBuffers(VkDevice device,
154 const VkCommandBufferAllocateInfo* pAllocateInfo,
155 VkCommandBuffer* pCommandBuffers);
156 VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
157 uint32_t submitCount,
158 const VkSubmitInfo* pSubmits,
159 VkFence fence);
160 VKAPI_ATTR void GetPhysicalDeviceFeatures2(
161 VkPhysicalDevice physicalDevice,
162 VkPhysicalDeviceFeatures2* pFeatures);
163 VKAPI_ATTR void GetPhysicalDeviceProperties2(
164 VkPhysicalDevice physicalDevice,
165 VkPhysicalDeviceProperties2* pProperties);
166 VKAPI_ATTR void GetPhysicalDeviceFormatProperties2(
167 VkPhysicalDevice physicalDevice,
168 VkFormat format,
169 VkFormatProperties2* pFormatProperties);
170 VKAPI_ATTR VkResult GetPhysicalDeviceImageFormatProperties2(
171 VkPhysicalDevice physicalDevice,
172 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
173 VkImageFormatProperties2* pImageFormatProperties);
174 VKAPI_ATTR void GetPhysicalDeviceQueueFamilyProperties2(
175 VkPhysicalDevice physicalDevice,
176 uint32_t* pQueueFamilyPropertyCount,
177 VkQueueFamilyProperties2* pQueueFamilyProperties);
178 VKAPI_ATTR void GetPhysicalDeviceMemoryProperties2(
179 VkPhysicalDevice physicalDevice,
180 VkPhysicalDeviceMemoryProperties2* pMemoryProperties);
181 VKAPI_ATTR void GetPhysicalDeviceSparseImageFormatProperties2(
182 VkPhysicalDevice physicalDevice,
183 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
184 uint32_t* pPropertyCount,
185 VkSparseImageFormatProperties2* pProperties);
186 VKAPI_ATTR void GetPhysicalDeviceExternalBufferProperties(
187 VkPhysicalDevice physicalDevice,
188 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
189 VkExternalBufferProperties* pExternalBufferProperties);
190 VKAPI_ATTR void GetPhysicalDeviceExternalSemaphoreProperties(
191 VkPhysicalDevice physicalDevice,
192 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
193 VkExternalSemaphoreProperties* pExternalSemaphoreProperties);
194 VKAPI_ATTR void GetPhysicalDeviceExternalFenceProperties(
195 VkPhysicalDevice physicalDevice,
196 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
197 VkExternalFenceProperties* pExternalFenceProperties);
198
199 template <typename DispatchableType>
StaticAssertDispatchable(DispatchableType)200 void StaticAssertDispatchable(DispatchableType) {
201 static_assert(
202 std::is_same<DispatchableType, VkInstance>::value ||
203 std::is_same<DispatchableType, VkPhysicalDevice>::value ||
204 std::is_same<DispatchableType, VkDevice>::value ||
205 std::is_same<DispatchableType, InstanceDispatchable>::value ||
206 std::is_same<DispatchableType, VkQueue>::value ||
207 std::is_same<DispatchableType, VkCommandBuffer>::value ||
208 std::is_same<DispatchableType, DeviceDispatchable>::value,
209 "unrecognized dispatchable type");
210 }
211
212 template <typename DispatchableType>
SetDataInternal(DispatchableType dispatchable,const void * data)213 bool SetDataInternal(DispatchableType dispatchable, const void* data) {
214 StaticAssertDispatchable(dispatchable);
215
216 hwvulkan_dispatch_t* dispatch =
217 reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable);
218 // must be magic or already set
219 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) {
220 ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic);
221 return false;
222 }
223
224 dispatch->vtbl = data;
225
226 return true;
227 }
228
229 template <typename DispatchableType>
GetDataInternal(DispatchableType dispatchable)230 void* GetDataInternal(DispatchableType dispatchable) {
231 StaticAssertDispatchable(dispatchable);
232
233 const hwvulkan_dispatch_t* dispatch =
234 reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable);
235
236 return const_cast<void*>(dispatch->vtbl);
237 }
238
SetData(VkInstance instance,const InstanceData & data)239 inline bool SetData(VkInstance instance, const InstanceData& data) {
240 return SetDataInternal(instance, &data);
241 }
242
SetData(VkPhysicalDevice physical_dev,const InstanceData & data)243 inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) {
244 return SetDataInternal(physical_dev, &data);
245 }
246
SetData(InstanceDispatchable dispatchable,const InstanceData & data)247 inline bool SetData(InstanceDispatchable dispatchable,
248 const InstanceData& data) {
249 return SetDataInternal(dispatchable, &data);
250 }
251
SetData(VkDevice dev,const DeviceData & data)252 inline bool SetData(VkDevice dev, const DeviceData& data) {
253 return SetDataInternal(dev, &data);
254 }
255
SetData(VkQueue queue,const DeviceData & data)256 inline bool SetData(VkQueue queue, const DeviceData& data) {
257 return SetDataInternal(queue, &data);
258 }
259
SetData(VkCommandBuffer cmd,const DeviceData & data)260 inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) {
261 return SetDataInternal(cmd, &data);
262 }
263
SetData(DeviceDispatchable dispatchable,const DeviceData & data)264 inline bool SetData(DeviceDispatchable dispatchable, const DeviceData& data) {
265 return SetDataInternal(dispatchable, &data);
266 }
267
GetData(VkInstance instance)268 inline InstanceData& GetData(VkInstance instance) {
269 return *reinterpret_cast<InstanceData*>(GetDataInternal(instance));
270 }
271
GetData(VkPhysicalDevice physical_dev)272 inline InstanceData& GetData(VkPhysicalDevice physical_dev) {
273 return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev));
274 }
275
GetData(InstanceDispatchable dispatchable)276 inline InstanceData& GetData(InstanceDispatchable dispatchable) {
277 return *reinterpret_cast<InstanceData*>(GetDataInternal(dispatchable));
278 }
279
GetData(VkDevice dev)280 inline DeviceData& GetData(VkDevice dev) {
281 return *reinterpret_cast<DeviceData*>(GetDataInternal(dev));
282 }
283
GetData(VkQueue queue)284 inline DeviceData& GetData(VkQueue queue) {
285 return *reinterpret_cast<DeviceData*>(GetDataInternal(queue));
286 }
287
GetData(VkCommandBuffer cmd)288 inline DeviceData& GetData(VkCommandBuffer cmd) {
289 return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd));
290 }
291
GetData(DeviceDispatchable dispatchable)292 inline DeviceData& GetData(DeviceDispatchable dispatchable) {
293 return *reinterpret_cast<DeviceData*>(GetDataInternal(dispatchable));
294 }
295
296 template <typename DispatchableType>
Logger(DispatchableType dispatchable)297 const DebugReportLogger Logger(DispatchableType dispatchable) {
298 return DebugReportLogger(GetData(dispatchable).debug_report_callbacks);
299 }
300
301 } // namespace driver
302 } // namespace vulkan
303
304 #endif // LIBVULKAN_DRIVER_H
305