| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 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> | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 21 |  | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 22 | #include <bitset> | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 23 | #include <type_traits> | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 24 |  | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 25 | #include <log/log.h> | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 26 |  | 
 | 27 | #include <vulkan/vulkan.h> | 
 | 28 | #include <hardware/hwvulkan.h> | 
 | 29 |  | 
 | 30 | #include "api_gen.h" | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 31 | #include "driver_gen.h" | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 32 | #include "debug_report.h" | 
| Chia-I Wu | 4a6a916 | 2016-03-26 07:17:34 +0800 | [diff] [blame] | 33 | #include "swapchain.h" | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 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 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 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; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 56 | }; | 
 | 57 |  | 
 | 58 | }  // namespace api | 
 | 59 |  | 
 | 60 | namespace driver { | 
 | 61 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 62 | VK_DEFINE_HANDLE(InstanceDispatchable) | 
 | 63 | VK_DEFINE_HANDLE(DeviceDispatchable) | 
 | 64 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 65 | struct InstanceData { | 
| Chih-Hung Hsieh | d56988d | 2016-09-01 11:37:47 -0700 | [diff] [blame] | 66 |     explicit InstanceData(const VkAllocationCallbacks& alloc) | 
| Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 67 |         : opaque_api_data(), | 
 | 68 |           allocator(alloc), | 
 | 69 |           driver(), | 
| Yiwei Zhang | 7cc36a5 | 2019-10-11 19:02:09 -0700 | [diff] [blame] | 70 |           get_device_proc_addr(nullptr) {} | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 71 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 72 |     api::InstanceData opaque_api_data; | 
 | 73 |  | 
 | 74 |     const VkAllocationCallbacks allocator; | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 75 |  | 
 | 76 |     std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; | 
| Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 77 |  | 
 | 78 |     InstanceDriverTable driver; | 
 | 79 |     PFN_vkGetDeviceProcAddr get_device_proc_addr; | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 80 |  | 
 | 81 |     DebugReportCallbackList debug_report_callbacks; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 82 | }; | 
 | 83 |  | 
 | 84 | struct DeviceData { | 
| Chia-I Wu | 950d6e1 | 2016-05-03 09:12:35 +0800 | [diff] [blame] | 85 |     DeviceData(const VkAllocationCallbacks& alloc, | 
 | 86 |                const DebugReportCallbackList& debug_report_callbacks_) | 
 | 87 |         : opaque_api_data(), | 
 | 88 |           allocator(alloc), | 
 | 89 |           debug_report_callbacks(debug_report_callbacks_), | 
| Yiwei Zhang | 7cc36a5 | 2019-10-11 19:02:09 -0700 | [diff] [blame] | 90 |           driver() {} | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 91 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 92 |     api::DeviceData opaque_api_data; | 
 | 93 |  | 
 | 94 |     const VkAllocationCallbacks allocator; | 
| Chia-I Wu | 950d6e1 | 2016-05-03 09:12:35 +0800 | [diff] [blame] | 95 |     const DebugReportCallbackList& debug_report_callbacks; | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 96 |  | 
 | 97 |     std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 98 |  | 
| Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 99 |     VkDevice driver_device; | 
| Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 100 |     DeviceDriverTable driver; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 101 | }; | 
 | 102 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 103 | bool OpenHAL(); | 
 | 104 | const VkAllocationCallbacks& GetDefaultAllocator(); | 
 | 105 |  | 
| Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 106 | void QueryPresentationProperties( | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 107 |     VkPhysicalDevice physicalDevice, | 
| Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 108 |     VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties); | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 109 |  | 
| Trevor David Black | 2cc4468 | 2022-03-09 00:31:38 +0000 | [diff] [blame] | 110 | bool GetAndroidNativeBufferSpecVersion9Support(VkPhysicalDevice physicalDevice); | 
 | 111 |  | 
| Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 112 | VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, | 
 | 113 |                                                   const char* pName); | 
 | 114 | VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, | 
 | 115 |                                                 const char* pName); | 
 | 116 | VKAPI_ATTR VkResult | 
 | 117 | EnumerateInstanceExtensionProperties(const char* pLayerName, | 
 | 118 |                                      uint32_t* pPropertyCount, | 
 | 119 |                                      VkExtensionProperties* pProperties); | 
 | 120 | VKAPI_ATTR VkResult | 
 | 121 | EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, | 
 | 122 |                                    const char* pLayerName, | 
 | 123 |                                    uint32_t* pPropertyCount, | 
 | 124 |                                    VkExtensionProperties* pProperties); | 
 | 125 | VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, | 
 | 126 |                                    const VkAllocationCallbacks* pAllocator, | 
 | 127 |                                    VkInstance* pInstance); | 
 | 128 | VKAPI_ATTR void DestroyInstance(VkInstance instance, | 
 | 129 |                                 const VkAllocationCallbacks* pAllocator); | 
 | 130 | VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, | 
 | 131 |                                  const VkDeviceCreateInfo* pCreateInfo, | 
 | 132 |                                  const VkAllocationCallbacks* pAllocator, | 
 | 133 |                                  VkDevice* pDevice); | 
 | 134 | VKAPI_ATTR void DestroyDevice(VkDevice device, | 
 | 135 |                               const VkAllocationCallbacks* pAllocator); | 
 | 136 | VKAPI_ATTR VkResult | 
 | 137 | EnumeratePhysicalDevices(VkInstance instance, | 
 | 138 |                          uint32_t* pPhysicalDeviceCount, | 
 | 139 |                          VkPhysicalDevice* pPhysicalDevices); | 
 | 140 | VKAPI_ATTR VkResult EnumeratePhysicalDeviceGroups( | 
 | 141 |     VkInstance instance, | 
 | 142 |     uint32_t* pPhysicalDeviceGroupCount, | 
 | 143 |     VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties); | 
 | 144 | VKAPI_ATTR void GetDeviceQueue(VkDevice device, | 
 | 145 |                                uint32_t queueFamilyIndex, | 
 | 146 |                                uint32_t queueIndex, | 
 | 147 |                                VkQueue* pQueue); | 
 | 148 | VKAPI_ATTR void GetDeviceQueue2(VkDevice device, | 
 | 149 |                                 const VkDeviceQueueInfo2* pQueueInfo, | 
 | 150 |                                 VkQueue* pQueue); | 
 | 151 | VKAPI_ATTR VkResult | 
 | 152 | AllocateCommandBuffers(VkDevice device, | 
 | 153 |                        const VkCommandBufferAllocateInfo* pAllocateInfo, | 
 | 154 |                        VkCommandBuffer* pCommandBuffers); | 
 | 155 | VKAPI_ATTR VkResult QueueSubmit(VkQueue queue, | 
 | 156 |                                 uint32_t submitCount, | 
 | 157 |                                 const VkSubmitInfo* pSubmits, | 
 | 158 |                                 VkFence fence); | 
 | 159 | VKAPI_ATTR void GetPhysicalDeviceFeatures2( | 
 | 160 |     VkPhysicalDevice physicalDevice, | 
 | 161 |     VkPhysicalDeviceFeatures2* pFeatures); | 
 | 162 | VKAPI_ATTR void GetPhysicalDeviceProperties2( | 
 | 163 |     VkPhysicalDevice physicalDevice, | 
 | 164 |     VkPhysicalDeviceProperties2* pProperties); | 
 | 165 | VKAPI_ATTR void GetPhysicalDeviceFormatProperties2( | 
 | 166 |     VkPhysicalDevice physicalDevice, | 
 | 167 |     VkFormat format, | 
 | 168 |     VkFormatProperties2* pFormatProperties); | 
 | 169 | VKAPI_ATTR VkResult GetPhysicalDeviceImageFormatProperties2( | 
 | 170 |     VkPhysicalDevice physicalDevice, | 
 | 171 |     const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo, | 
 | 172 |     VkImageFormatProperties2* pImageFormatProperties); | 
 | 173 | VKAPI_ATTR void GetPhysicalDeviceQueueFamilyProperties2( | 
 | 174 |     VkPhysicalDevice physicalDevice, | 
 | 175 |     uint32_t* pQueueFamilyPropertyCount, | 
 | 176 |     VkQueueFamilyProperties2* pQueueFamilyProperties); | 
 | 177 | VKAPI_ATTR void GetPhysicalDeviceMemoryProperties2( | 
 | 178 |     VkPhysicalDevice physicalDevice, | 
 | 179 |     VkPhysicalDeviceMemoryProperties2* pMemoryProperties); | 
 | 180 | VKAPI_ATTR void GetPhysicalDeviceSparseImageFormatProperties2( | 
 | 181 |     VkPhysicalDevice physicalDevice, | 
 | 182 |     const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo, | 
 | 183 |     uint32_t* pPropertyCount, | 
 | 184 |     VkSparseImageFormatProperties2* pProperties); | 
 | 185 | VKAPI_ATTR void GetPhysicalDeviceExternalBufferProperties( | 
 | 186 |     VkPhysicalDevice physicalDevice, | 
 | 187 |     const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo, | 
 | 188 |     VkExternalBufferProperties* pExternalBufferProperties); | 
 | 189 | VKAPI_ATTR void GetPhysicalDeviceExternalSemaphoreProperties( | 
 | 190 |     VkPhysicalDevice physicalDevice, | 
 | 191 |     const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo, | 
 | 192 |     VkExternalSemaphoreProperties* pExternalSemaphoreProperties); | 
 | 193 | VKAPI_ATTR void GetPhysicalDeviceExternalFenceProperties( | 
 | 194 |     VkPhysicalDevice physicalDevice, | 
 | 195 |     const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo, | 
 | 196 |     VkExternalFenceProperties* pExternalFenceProperties); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 197 |  | 
 | 198 | template <typename DispatchableType> | 
 | 199 | void StaticAssertDispatchable(DispatchableType) { | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 200 |     static_assert( | 
 | 201 |         std::is_same<DispatchableType, VkInstance>::value || | 
 | 202 |             std::is_same<DispatchableType, VkPhysicalDevice>::value || | 
 | 203 |             std::is_same<DispatchableType, VkDevice>::value || | 
 | 204 |             std::is_same<DispatchableType, InstanceDispatchable>::value || | 
 | 205 |             std::is_same<DispatchableType, VkQueue>::value || | 
 | 206 |             std::is_same<DispatchableType, VkCommandBuffer>::value || | 
 | 207 |             std::is_same<DispatchableType, DeviceDispatchable>::value, | 
 | 208 |         "unrecognized dispatchable type"); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 209 | } | 
 | 210 |  | 
 | 211 | template <typename DispatchableType> | 
 | 212 | bool SetDataInternal(DispatchableType dispatchable, const void* data) { | 
 | 213 |     StaticAssertDispatchable(dispatchable); | 
 | 214 |  | 
 | 215 |     hwvulkan_dispatch_t* dispatch = | 
 | 216 |         reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable); | 
 | 217 |     // must be magic or already set | 
 | 218 |     if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) { | 
 | 219 |         ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic); | 
 | 220 |         return false; | 
 | 221 |     } | 
 | 222 |  | 
 | 223 |     dispatch->vtbl = data; | 
 | 224 |  | 
 | 225 |     return true; | 
 | 226 | } | 
 | 227 |  | 
 | 228 | template <typename DispatchableType> | 
 | 229 | void* GetDataInternal(DispatchableType dispatchable) { | 
 | 230 |     StaticAssertDispatchable(dispatchable); | 
 | 231 |  | 
 | 232 |     const hwvulkan_dispatch_t* dispatch = | 
 | 233 |         reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable); | 
 | 234 |  | 
 | 235 |     return const_cast<void*>(dispatch->vtbl); | 
 | 236 | } | 
 | 237 |  | 
 | 238 | inline bool SetData(VkInstance instance, const InstanceData& data) { | 
 | 239 |     return SetDataInternal(instance, &data); | 
 | 240 | } | 
 | 241 |  | 
 | 242 | inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) { | 
 | 243 |     return SetDataInternal(physical_dev, &data); | 
 | 244 | } | 
 | 245 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 246 | inline bool SetData(InstanceDispatchable dispatchable, | 
 | 247 |                     const InstanceData& data) { | 
 | 248 |     return SetDataInternal(dispatchable, &data); | 
 | 249 | } | 
 | 250 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 251 | inline bool SetData(VkDevice dev, const DeviceData& data) { | 
 | 252 |     return SetDataInternal(dev, &data); | 
 | 253 | } | 
 | 254 |  | 
 | 255 | inline bool SetData(VkQueue queue, const DeviceData& data) { | 
 | 256 |     return SetDataInternal(queue, &data); | 
 | 257 | } | 
 | 258 |  | 
 | 259 | inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) { | 
 | 260 |     return SetDataInternal(cmd, &data); | 
 | 261 | } | 
 | 262 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 263 | inline bool SetData(DeviceDispatchable dispatchable, const DeviceData& data) { | 
 | 264 |     return SetDataInternal(dispatchable, &data); | 
 | 265 | } | 
 | 266 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 267 | inline InstanceData& GetData(VkInstance instance) { | 
 | 268 |     return *reinterpret_cast<InstanceData*>(GetDataInternal(instance)); | 
 | 269 | } | 
 | 270 |  | 
 | 271 | inline InstanceData& GetData(VkPhysicalDevice physical_dev) { | 
 | 272 |     return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev)); | 
 | 273 | } | 
 | 274 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 275 | inline InstanceData& GetData(InstanceDispatchable dispatchable) { | 
 | 276 |     return *reinterpret_cast<InstanceData*>(GetDataInternal(dispatchable)); | 
 | 277 | } | 
 | 278 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 279 | inline DeviceData& GetData(VkDevice dev) { | 
 | 280 |     return *reinterpret_cast<DeviceData*>(GetDataInternal(dev)); | 
 | 281 | } | 
 | 282 |  | 
 | 283 | inline DeviceData& GetData(VkQueue queue) { | 
 | 284 |     return *reinterpret_cast<DeviceData*>(GetDataInternal(queue)); | 
 | 285 | } | 
 | 286 |  | 
 | 287 | inline DeviceData& GetData(VkCommandBuffer cmd) { | 
 | 288 |     return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd)); | 
 | 289 | } | 
 | 290 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 291 | inline DeviceData& GetData(DeviceDispatchable dispatchable) { | 
 | 292 |     return *reinterpret_cast<DeviceData*>(GetDataInternal(dispatchable)); | 
 | 293 | } | 
 | 294 |  | 
| Chia-I Wu | bc011fc | 2016-05-03 12:19:55 +0800 | [diff] [blame] | 295 | template <typename DispatchableType> | 
 | 296 | const DebugReportLogger Logger(DispatchableType dispatchable) { | 
 | 297 |     return DebugReportLogger(GetData(dispatchable).debug_report_callbacks); | 
 | 298 | } | 
 | 299 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 300 | }  // namespace driver | 
 | 301 | }  // namespace vulkan | 
 | 302 |  | 
 | 303 | #endif  // LIBVULKAN_DRIVER_H |