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