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         hook_extensions.set(ProcHook::EXTENSION_CORE);
72     }
73 
74     api::InstanceData opaque_api_data;
75 
76     const VkAllocationCallbacks allocator;
77 
78     std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
79 
80     InstanceDriverTable driver;
81     PFN_vkGetDeviceProcAddr get_device_proc_addr;
82 
83     DebugReportCallbackList debug_report_callbacks;
84 };
85 
86 struct DeviceData {
DeviceDataDeviceData87     DeviceData(const VkAllocationCallbacks& alloc,
88                const DebugReportCallbackList& debug_report_callbacks_)
89         : opaque_api_data(),
90           allocator(alloc),
91           debug_report_callbacks(debug_report_callbacks_),
92           driver() {
93         hook_extensions.set(ProcHook::EXTENSION_CORE);
94     }
95 
96     api::DeviceData opaque_api_data;
97 
98     const VkAllocationCallbacks allocator;
99     const DebugReportCallbackList& debug_report_callbacks;
100 
101     std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
102 
103     VkDevice driver_device;
104     DeviceDriverTable driver;
105     uint32_t driver_version;
106 };
107 
108 bool Debuggable();
109 bool OpenHAL();
110 const VkAllocationCallbacks& GetDefaultAllocator();
111 
112 bool QueryPresentationProperties(
113     VkPhysicalDevice physicalDevice,
114     VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties);
115 
116 // clang-format off
117 VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName);
118 VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName);
119 VKAPI_ATTR VkResult EnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
120 
121 VKAPI_ATTR VkResult EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
122 
123 VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance);
124 VKAPI_ATTR void DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator);
125 VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice);
126 VKAPI_ATTR void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator);
127 
128 VKAPI_ATTR VkResult EnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices);
129 VKAPI_ATTR VkResult EnumeratePhysicalDeviceGroups(VkInstance instance, uint32_t* pPhysicalDeviceGroupCount, VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties);
130 
131 VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);
132 VKAPI_ATTR void GetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2* pQueueInfo, VkQueue* pQueue);
133 VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers);
134 // clang-format on
135 
136 template <typename DispatchableType>
StaticAssertDispatchable(DispatchableType)137 void StaticAssertDispatchable(DispatchableType) {
138     static_assert(
139         std::is_same<DispatchableType, VkInstance>::value ||
140             std::is_same<DispatchableType, VkPhysicalDevice>::value ||
141             std::is_same<DispatchableType, VkDevice>::value ||
142             std::is_same<DispatchableType, InstanceDispatchable>::value ||
143             std::is_same<DispatchableType, VkQueue>::value ||
144             std::is_same<DispatchableType, VkCommandBuffer>::value ||
145             std::is_same<DispatchableType, DeviceDispatchable>::value,
146         "unrecognized dispatchable type");
147 }
148 
149 template <typename DispatchableType>
SetDataInternal(DispatchableType dispatchable,const void * data)150 bool SetDataInternal(DispatchableType dispatchable, const void* data) {
151     StaticAssertDispatchable(dispatchable);
152 
153     hwvulkan_dispatch_t* dispatch =
154         reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable);
155     // must be magic or already set
156     if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) {
157         ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic);
158         return false;
159     }
160 
161     dispatch->vtbl = data;
162 
163     return true;
164 }
165 
166 template <typename DispatchableType>
GetDataInternal(DispatchableType dispatchable)167 void* GetDataInternal(DispatchableType dispatchable) {
168     StaticAssertDispatchable(dispatchable);
169 
170     const hwvulkan_dispatch_t* dispatch =
171         reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable);
172 
173     return const_cast<void*>(dispatch->vtbl);
174 }
175 
SetData(VkInstance instance,const InstanceData & data)176 inline bool SetData(VkInstance instance, const InstanceData& data) {
177     return SetDataInternal(instance, &data);
178 }
179 
SetData(VkPhysicalDevice physical_dev,const InstanceData & data)180 inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) {
181     return SetDataInternal(physical_dev, &data);
182 }
183 
SetData(InstanceDispatchable dispatchable,const InstanceData & data)184 inline bool SetData(InstanceDispatchable dispatchable,
185                     const InstanceData& data) {
186     return SetDataInternal(dispatchable, &data);
187 }
188 
SetData(VkDevice dev,const DeviceData & data)189 inline bool SetData(VkDevice dev, const DeviceData& data) {
190     return SetDataInternal(dev, &data);
191 }
192 
SetData(VkQueue queue,const DeviceData & data)193 inline bool SetData(VkQueue queue, const DeviceData& data) {
194     return SetDataInternal(queue, &data);
195 }
196 
SetData(VkCommandBuffer cmd,const DeviceData & data)197 inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) {
198     return SetDataInternal(cmd, &data);
199 }
200 
SetData(DeviceDispatchable dispatchable,const DeviceData & data)201 inline bool SetData(DeviceDispatchable dispatchable, const DeviceData& data) {
202     return SetDataInternal(dispatchable, &data);
203 }
204 
GetData(VkInstance instance)205 inline InstanceData& GetData(VkInstance instance) {
206     return *reinterpret_cast<InstanceData*>(GetDataInternal(instance));
207 }
208 
GetData(VkPhysicalDevice physical_dev)209 inline InstanceData& GetData(VkPhysicalDevice physical_dev) {
210     return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev));
211 }
212 
GetData(InstanceDispatchable dispatchable)213 inline InstanceData& GetData(InstanceDispatchable dispatchable) {
214     return *reinterpret_cast<InstanceData*>(GetDataInternal(dispatchable));
215 }
216 
GetData(VkDevice dev)217 inline DeviceData& GetData(VkDevice dev) {
218     return *reinterpret_cast<DeviceData*>(GetDataInternal(dev));
219 }
220 
GetData(VkQueue queue)221 inline DeviceData& GetData(VkQueue queue) {
222     return *reinterpret_cast<DeviceData*>(GetDataInternal(queue));
223 }
224 
GetData(VkCommandBuffer cmd)225 inline DeviceData& GetData(VkCommandBuffer cmd) {
226     return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd));
227 }
228 
GetData(DeviceDispatchable dispatchable)229 inline DeviceData& GetData(DeviceDispatchable dispatchable) {
230     return *reinterpret_cast<DeviceData*>(GetDataInternal(dispatchable));
231 }
232 
233 template <typename DispatchableType>
Logger(DispatchableType dispatchable)234 const DebugReportLogger Logger(DispatchableType dispatchable) {
235     return DebugReportLogger(GetData(dispatchable).debug_report_callbacks);
236 }
237 
238 }  // namespace driver
239 }  // namespace vulkan
240 
241 #endif  // LIBVULKAN_DRIVER_H
242