driver.h revision ff4a6c772aaf3ff0b71348647330031a059b1f51
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 32namespace vulkan { 33 34// This is here so that we can embed api::{Instance,Device}Data in 35// driver::{Instance,Device}Data to avoid pointer chasing. They are 36// considered opaque to the driver layer. 37namespace api { 38 39struct InstanceData { 40 InstanceDispatchTable dispatch; 41 42 // for VkPhysicalDevice->VkInstance mapping 43 VkInstance instance; 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 54struct DeviceData { 55 DeviceDispatchTable dispatch; 56 57 // LayerChain::ActiveLayer array 58 void* layers; 59 uint32_t layer_count; 60}; 61 62} // namespace api 63 64namespace driver { 65 66struct InstanceData { 67 InstanceData(const VkAllocationCallbacks& alloc) 68 : opaque_api_data(), 69 allocator(alloc), 70 driver(), 71 get_device_proc_addr(nullptr) { 72 hook_extensions.set(ProcHook::EXTENSION_CORE); 73 hal_extensions.set(ProcHook::EXTENSION_CORE); 74 } 75 76 api::InstanceData opaque_api_data; 77 78 const VkAllocationCallbacks allocator; 79 80 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; 81 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions; 82 83 InstanceDriverTable driver; 84 PFN_vkGetDeviceProcAddr get_device_proc_addr; 85 86 DebugReportCallbackList debug_report_callbacks; 87}; 88 89struct DeviceData { 90 DeviceData(const VkAllocationCallbacks& alloc) 91 : opaque_api_data(), allocator(alloc), driver() { 92 hook_extensions.set(ProcHook::EXTENSION_CORE); 93 hal_extensions.set(ProcHook::EXTENSION_CORE); 94 } 95 96 api::DeviceData opaque_api_data; 97 98 const VkAllocationCallbacks allocator; 99 100 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; 101 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions; 102 103 DeviceDriverTable driver; 104}; 105 106bool Debuggable(); 107bool OpenHAL(); 108const VkAllocationCallbacks& GetDefaultAllocator(); 109 110// clang-format off 111VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName); 112VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName); 113VKAPI_ATTR VkResult EnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties); 114 115VKAPI_ATTR VkResult EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties); 116 117VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance); 118VKAPI_ATTR void DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator); 119VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice); 120VKAPI_ATTR void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator); 121 122VKAPI_ATTR VkResult EnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices); 123VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue); 124VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers); 125// clang-format on 126 127template <typename DispatchableType> 128void StaticAssertDispatchable(DispatchableType) { 129 static_assert(std::is_same<DispatchableType, VkInstance>::value || 130 std::is_same<DispatchableType, VkPhysicalDevice>::value || 131 std::is_same<DispatchableType, VkDevice>::value || 132 std::is_same<DispatchableType, VkQueue>::value || 133 std::is_same<DispatchableType, VkCommandBuffer>::value, 134 "unrecognized dispatchable type"); 135} 136 137template <typename DispatchableType> 138bool SetDataInternal(DispatchableType dispatchable, const void* data) { 139 StaticAssertDispatchable(dispatchable); 140 141 hwvulkan_dispatch_t* dispatch = 142 reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable); 143 // must be magic or already set 144 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) { 145 ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic); 146 return false; 147 } 148 149 dispatch->vtbl = data; 150 151 return true; 152} 153 154template <typename DispatchableType> 155void* GetDataInternal(DispatchableType dispatchable) { 156 StaticAssertDispatchable(dispatchable); 157 158 const hwvulkan_dispatch_t* dispatch = 159 reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable); 160 161 return const_cast<void*>(dispatch->vtbl); 162} 163 164inline bool SetData(VkInstance instance, const InstanceData& data) { 165 return SetDataInternal(instance, &data); 166} 167 168inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) { 169 return SetDataInternal(physical_dev, &data); 170} 171 172inline bool SetData(VkDevice dev, const DeviceData& data) { 173 return SetDataInternal(dev, &data); 174} 175 176inline bool SetData(VkQueue queue, const DeviceData& data) { 177 return SetDataInternal(queue, &data); 178} 179 180inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) { 181 return SetDataInternal(cmd, &data); 182} 183 184inline InstanceData& GetData(VkInstance instance) { 185 return *reinterpret_cast<InstanceData*>(GetDataInternal(instance)); 186} 187 188inline InstanceData& GetData(VkPhysicalDevice physical_dev) { 189 return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev)); 190} 191 192inline DeviceData& GetData(VkDevice dev) { 193 return *reinterpret_cast<DeviceData*>(GetDataInternal(dev)); 194} 195 196inline DeviceData& GetData(VkQueue queue) { 197 return *reinterpret_cast<DeviceData*>(GetDataInternal(queue)); 198} 199 200inline DeviceData& GetData(VkCommandBuffer cmd) { 201 return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd)); 202} 203 204} // namespace driver 205} // namespace vulkan 206 207#endif // LIBVULKAN_DRIVER_H 208