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 35namespace 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. 40namespace api { 41 42struct 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 54struct DeviceData { 55 DeviceDispatchTable dispatch; 56}; 57 58} // namespace api 59 60namespace driver { 61 62VK_DEFINE_HANDLE(InstanceDispatchable) 63VK_DEFINE_HANDLE(DeviceDispatchable) 64 65struct InstanceData { 66 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 86struct DeviceData { 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() { 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 108bool Debuggable(); 109bool OpenHAL(); 110const VkAllocationCallbacks& GetDefaultAllocator(); 111 112bool QueryPresentationProperties( 113 VkPhysicalDevice physicalDevice, 114 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties); 115 116// clang-format off 117VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName); 118VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName); 119VKAPI_ATTR VkResult EnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties); 120 121VKAPI_ATTR VkResult EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties); 122 123VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance); 124VKAPI_ATTR void DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator); 125VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice); 126VKAPI_ATTR void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator); 127 128VKAPI_ATTR VkResult EnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices); 129VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue); 130VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers); 131// clang-format on 132 133template <typename DispatchableType> 134void StaticAssertDispatchable(DispatchableType) { 135 static_assert( 136 std::is_same<DispatchableType, VkInstance>::value || 137 std::is_same<DispatchableType, VkPhysicalDevice>::value || 138 std::is_same<DispatchableType, VkDevice>::value || 139 std::is_same<DispatchableType, InstanceDispatchable>::value || 140 std::is_same<DispatchableType, VkQueue>::value || 141 std::is_same<DispatchableType, VkCommandBuffer>::value || 142 std::is_same<DispatchableType, DeviceDispatchable>::value, 143 "unrecognized dispatchable type"); 144} 145 146template <typename DispatchableType> 147bool SetDataInternal(DispatchableType dispatchable, const void* data) { 148 StaticAssertDispatchable(dispatchable); 149 150 hwvulkan_dispatch_t* dispatch = 151 reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable); 152 // must be magic or already set 153 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) { 154 ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic); 155 return false; 156 } 157 158 dispatch->vtbl = data; 159 160 return true; 161} 162 163template <typename DispatchableType> 164void* GetDataInternal(DispatchableType dispatchable) { 165 StaticAssertDispatchable(dispatchable); 166 167 const hwvulkan_dispatch_t* dispatch = 168 reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable); 169 170 return const_cast<void*>(dispatch->vtbl); 171} 172 173inline bool SetData(VkInstance instance, const InstanceData& data) { 174 return SetDataInternal(instance, &data); 175} 176 177inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) { 178 return SetDataInternal(physical_dev, &data); 179} 180 181inline bool SetData(InstanceDispatchable dispatchable, 182 const InstanceData& data) { 183 return SetDataInternal(dispatchable, &data); 184} 185 186inline bool SetData(VkDevice dev, const DeviceData& data) { 187 return SetDataInternal(dev, &data); 188} 189 190inline bool SetData(VkQueue queue, const DeviceData& data) { 191 return SetDataInternal(queue, &data); 192} 193 194inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) { 195 return SetDataInternal(cmd, &data); 196} 197 198inline bool SetData(DeviceDispatchable dispatchable, const DeviceData& data) { 199 return SetDataInternal(dispatchable, &data); 200} 201 202inline InstanceData& GetData(VkInstance instance) { 203 return *reinterpret_cast<InstanceData*>(GetDataInternal(instance)); 204} 205 206inline InstanceData& GetData(VkPhysicalDevice physical_dev) { 207 return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev)); 208} 209 210inline InstanceData& GetData(InstanceDispatchable dispatchable) { 211 return *reinterpret_cast<InstanceData*>(GetDataInternal(dispatchable)); 212} 213 214inline DeviceData& GetData(VkDevice dev) { 215 return *reinterpret_cast<DeviceData*>(GetDataInternal(dev)); 216} 217 218inline DeviceData& GetData(VkQueue queue) { 219 return *reinterpret_cast<DeviceData*>(GetDataInternal(queue)); 220} 221 222inline DeviceData& GetData(VkCommandBuffer cmd) { 223 return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd)); 224} 225 226inline DeviceData& GetData(DeviceDispatchable dispatchable) { 227 return *reinterpret_cast<DeviceData*>(GetDataInternal(dispatchable)); 228} 229 230template <typename DispatchableType> 231const DebugReportLogger Logger(DispatchableType dispatchable) { 232 return DebugReportLogger(GetData(dispatchable).debug_report_callbacks); 233} 234 235} // namespace driver 236} // namespace vulkan 237 238#endif // LIBVULKAN_DRIVER_H 239