driver.h revision 950d6e1102077d6a3905eb77268800002e792fb0
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#include <bitset> 22#include <type_traits> 23#include <log/log.h> 24 25#include <vulkan/vulkan.h> 26#include <hardware/hwvulkan.h> 27 28#include "api_gen.h" 29#include "driver_gen.h" 30#include "debug_report.h" 31#include "swapchain.h" 32 33namespace vulkan { 34 35// This is here so that we can embed api::{Instance,Device}Data in 36// driver::{Instance,Device}Data to avoid pointer chasing. They are 37// considered opaque to the driver layer. 38namespace api { 39 40struct InstanceData { 41 InstanceDispatchTable dispatch; 42 43 // LayerChain::ActiveLayer array 44 void* layers; 45 uint32_t layer_count; 46 47 // debug.vulkan.enable_callback 48 PFN_vkDestroyDebugReportCallbackEXT destroy_debug_callback; 49 VkDebugReportCallbackEXT debug_callback; 50}; 51 52struct DeviceData { 53 DeviceDispatchTable dispatch; 54}; 55 56} // namespace api 57 58namespace driver { 59 60VK_DEFINE_HANDLE(InstanceDispatchable) 61VK_DEFINE_HANDLE(DeviceDispatchable) 62 63struct InstanceData { 64 InstanceData(const VkAllocationCallbacks& alloc) 65 : opaque_api_data(), 66 allocator(alloc), 67 driver(), 68 get_device_proc_addr(nullptr) { 69 hook_extensions.set(ProcHook::EXTENSION_CORE); 70 } 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 84struct DeviceData { 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_), 90 driver() { 91 hook_extensions.set(ProcHook::EXTENSION_CORE); 92 } 93 94 api::DeviceData opaque_api_data; 95 96 const VkAllocationCallbacks allocator; 97 const DebugReportCallbackList& debug_report_callbacks; 98 99 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; 100 101 DeviceDriverTable driver; 102}; 103 104bool Debuggable(); 105bool OpenHAL(); 106const VkAllocationCallbacks& GetDefaultAllocator(); 107 108// clang-format off 109VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName); 110VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName); 111VKAPI_ATTR VkResult EnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties); 112 113VKAPI_ATTR VkResult EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties); 114 115VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance); 116VKAPI_ATTR void DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator); 117VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice); 118VKAPI_ATTR void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator); 119 120VKAPI_ATTR VkResult EnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices); 121VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue); 122VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers); 123// clang-format on 124 125template <typename DispatchableType> 126void StaticAssertDispatchable(DispatchableType) { 127 static_assert( 128 std::is_same<DispatchableType, VkInstance>::value || 129 std::is_same<DispatchableType, VkPhysicalDevice>::value || 130 std::is_same<DispatchableType, VkDevice>::value || 131 std::is_same<DispatchableType, InstanceDispatchable>::value || 132 std::is_same<DispatchableType, VkQueue>::value || 133 std::is_same<DispatchableType, VkCommandBuffer>::value || 134 std::is_same<DispatchableType, DeviceDispatchable>::value, 135 "unrecognized dispatchable type"); 136} 137 138template <typename DispatchableType> 139bool SetDataInternal(DispatchableType dispatchable, const void* data) { 140 StaticAssertDispatchable(dispatchable); 141 142 hwvulkan_dispatch_t* dispatch = 143 reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable); 144 // must be magic or already set 145 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) { 146 ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic); 147 return false; 148 } 149 150 dispatch->vtbl = data; 151 152 return true; 153} 154 155template <typename DispatchableType> 156void* GetDataInternal(DispatchableType dispatchable) { 157 StaticAssertDispatchable(dispatchable); 158 159 const hwvulkan_dispatch_t* dispatch = 160 reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable); 161 162 return const_cast<void*>(dispatch->vtbl); 163} 164 165inline bool SetData(VkInstance instance, const InstanceData& data) { 166 return SetDataInternal(instance, &data); 167} 168 169inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) { 170 return SetDataInternal(physical_dev, &data); 171} 172 173inline bool SetData(InstanceDispatchable dispatchable, 174 const InstanceData& data) { 175 return SetDataInternal(dispatchable, &data); 176} 177 178inline bool SetData(VkDevice dev, const DeviceData& data) { 179 return SetDataInternal(dev, &data); 180} 181 182inline bool SetData(VkQueue queue, const DeviceData& data) { 183 return SetDataInternal(queue, &data); 184} 185 186inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) { 187 return SetDataInternal(cmd, &data); 188} 189 190inline bool SetData(DeviceDispatchable dispatchable, const DeviceData& data) { 191 return SetDataInternal(dispatchable, &data); 192} 193 194inline InstanceData& GetData(VkInstance instance) { 195 return *reinterpret_cast<InstanceData*>(GetDataInternal(instance)); 196} 197 198inline InstanceData& GetData(VkPhysicalDevice physical_dev) { 199 return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev)); 200} 201 202inline InstanceData& GetData(InstanceDispatchable dispatchable) { 203 return *reinterpret_cast<InstanceData*>(GetDataInternal(dispatchable)); 204} 205 206inline DeviceData& GetData(VkDevice dev) { 207 return *reinterpret_cast<DeviceData*>(GetDataInternal(dev)); 208} 209 210inline DeviceData& GetData(VkQueue queue) { 211 return *reinterpret_cast<DeviceData*>(GetDataInternal(queue)); 212} 213 214inline DeviceData& GetData(VkCommandBuffer cmd) { 215 return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd)); 216} 217 218inline DeviceData& GetData(DeviceDispatchable dispatchable) { 219 return *reinterpret_cast<DeviceData*>(GetDataInternal(dispatchable)); 220} 221 222} // namespace driver 223} // namespace vulkan 224 225#endif // LIBVULKAN_DRIVER_H 226