| 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(), | 
|  | 70 | get_device_proc_addr(nullptr) { | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 71 | hook_extensions.set(ProcHook::EXTENSION_CORE); | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 74 | api::InstanceData opaque_api_data; | 
|  | 75 |  | 
|  | 76 | const VkAllocationCallbacks allocator; | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 77 |  | 
|  | 78 | std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; | 
| Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 79 |  | 
|  | 80 | InstanceDriverTable driver; | 
|  | 81 | PFN_vkGetDeviceProcAddr get_device_proc_addr; | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 82 |  | 
|  | 83 | DebugReportCallbackList debug_report_callbacks; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 84 | }; | 
|  | 85 |  | 
|  | 86 | struct DeviceData { | 
| Chia-I Wu | 950d6e1 | 2016-05-03 09:12:35 +0800 | [diff] [blame] | 87 | 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() { | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 93 | hook_extensions.set(ProcHook::EXTENSION_CORE); | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 96 | api::DeviceData opaque_api_data; | 
|  | 97 |  | 
|  | 98 | const VkAllocationCallbacks allocator; | 
| Chia-I Wu | 950d6e1 | 2016-05-03 09:12:35 +0800 | [diff] [blame] | 99 | const DebugReportCallbackList& debug_report_callbacks; | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 100 |  | 
|  | 101 | std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 102 |  | 
| Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 103 | VkDevice driver_device; | 
| Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 104 | DeviceDriverTable driver; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 105 | }; | 
|  | 106 |  | 
|  | 107 | bool Debuggable(); | 
|  | 108 | bool OpenHAL(); | 
|  | 109 | const VkAllocationCallbacks& GetDefaultAllocator(); | 
|  | 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); | 
| Chia-I Wu | ba0be41 | 2016-03-24 16:24:40 +0800 | [diff] [blame] | 115 |  | 
| Chia-I Wu | 01cf305 | 2016-03-24 16:16:21 +0800 | [diff] [blame] | 116 | VKAPI_ATTR VkResult EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties); | 
|  | 117 |  | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 118 | VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance); | 
|  | 119 | VKAPI_ATTR void DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 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 |  | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 123 | VKAPI_ATTR VkResult EnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices); | 
| Chia-I Wu | ba0be41 | 2016-03-24 16:24:40 +0800 | [diff] [blame] | 124 | VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue); | 
| Chia-I Wu | 6a58a8a | 2016-03-24 16:29:51 +0800 | [diff] [blame] | 125 | VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 126 | // clang-format on | 
|  | 127 |  | 
|  | 128 | template <typename DispatchableType> | 
|  | 129 | void StaticAssertDispatchable(DispatchableType) { | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 130 | static_assert( | 
|  | 131 | std::is_same<DispatchableType, VkInstance>::value || | 
|  | 132 | std::is_same<DispatchableType, VkPhysicalDevice>::value || | 
|  | 133 | std::is_same<DispatchableType, VkDevice>::value || | 
|  | 134 | std::is_same<DispatchableType, InstanceDispatchable>::value || | 
|  | 135 | std::is_same<DispatchableType, VkQueue>::value || | 
|  | 136 | std::is_same<DispatchableType, VkCommandBuffer>::value || | 
|  | 137 | std::is_same<DispatchableType, DeviceDispatchable>::value, | 
|  | 138 | "unrecognized dispatchable type"); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 139 | } | 
|  | 140 |  | 
|  | 141 | template <typename DispatchableType> | 
|  | 142 | bool SetDataInternal(DispatchableType dispatchable, const void* data) { | 
|  | 143 | StaticAssertDispatchable(dispatchable); | 
|  | 144 |  | 
|  | 145 | hwvulkan_dispatch_t* dispatch = | 
|  | 146 | reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable); | 
|  | 147 | // must be magic or already set | 
|  | 148 | if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) { | 
|  | 149 | ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic); | 
|  | 150 | return false; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | dispatch->vtbl = data; | 
|  | 154 |  | 
|  | 155 | return true; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | template <typename DispatchableType> | 
|  | 159 | void* GetDataInternal(DispatchableType dispatchable) { | 
|  | 160 | StaticAssertDispatchable(dispatchable); | 
|  | 161 |  | 
|  | 162 | const hwvulkan_dispatch_t* dispatch = | 
|  | 163 | reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable); | 
|  | 164 |  | 
|  | 165 | return const_cast<void*>(dispatch->vtbl); | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | inline bool SetData(VkInstance instance, const InstanceData& data) { | 
|  | 169 | return SetDataInternal(instance, &data); | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) { | 
|  | 173 | return SetDataInternal(physical_dev, &data); | 
|  | 174 | } | 
|  | 175 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 176 | inline bool SetData(InstanceDispatchable dispatchable, | 
|  | 177 | const InstanceData& data) { | 
|  | 178 | return SetDataInternal(dispatchable, &data); | 
|  | 179 | } | 
|  | 180 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 181 | inline bool SetData(VkDevice dev, const DeviceData& data) { | 
|  | 182 | return SetDataInternal(dev, &data); | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | inline bool SetData(VkQueue queue, const DeviceData& data) { | 
|  | 186 | return SetDataInternal(queue, &data); | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) { | 
|  | 190 | return SetDataInternal(cmd, &data); | 
|  | 191 | } | 
|  | 192 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 193 | inline bool SetData(DeviceDispatchable dispatchable, const DeviceData& data) { | 
|  | 194 | return SetDataInternal(dispatchable, &data); | 
|  | 195 | } | 
|  | 196 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 197 | inline InstanceData& GetData(VkInstance instance) { | 
|  | 198 | return *reinterpret_cast<InstanceData*>(GetDataInternal(instance)); | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | inline InstanceData& GetData(VkPhysicalDevice physical_dev) { | 
|  | 202 | return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev)); | 
|  | 203 | } | 
|  | 204 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 205 | inline InstanceData& GetData(InstanceDispatchable dispatchable) { | 
|  | 206 | return *reinterpret_cast<InstanceData*>(GetDataInternal(dispatchable)); | 
|  | 207 | } | 
|  | 208 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 209 | inline DeviceData& GetData(VkDevice dev) { | 
|  | 210 | return *reinterpret_cast<DeviceData*>(GetDataInternal(dev)); | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | inline DeviceData& GetData(VkQueue queue) { | 
|  | 214 | return *reinterpret_cast<DeviceData*>(GetDataInternal(queue)); | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | inline DeviceData& GetData(VkCommandBuffer cmd) { | 
|  | 218 | return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd)); | 
|  | 219 | } | 
|  | 220 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 221 | inline DeviceData& GetData(DeviceDispatchable dispatchable) { | 
|  | 222 | return *reinterpret_cast<DeviceData*>(GetDataInternal(dispatchable)); | 
|  | 223 | } | 
|  | 224 |  | 
| Chia-I Wu | bc011fc | 2016-05-03 12:19:55 +0800 | [diff] [blame] | 225 | template <typename DispatchableType> | 
|  | 226 | const DebugReportLogger Logger(DispatchableType dispatchable) { | 
|  | 227 | return DebugReportLogger(GetData(dispatchable).debug_report_callbacks); | 
|  | 228 | } | 
|  | 229 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 230 | }  // namespace driver | 
|  | 231 | }  // namespace vulkan | 
|  | 232 |  | 
|  | 233 | #endif  // LIBVULKAN_DRIVER_H |