driver.h revision eb7db124e46da9a9210cf868353f5ea79502ffec
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 31namespace vulkan { 32 33// This is here so that we can embed api::{Instance,Device}Data in 34// driver::{Instance,Device}Data to avoid pointer chasing. They are 35// considered opaque to the driver layer. 36namespace api { 37 38struct InstanceData { 39 InstanceDispatchTable dispatch; 40 41 // for VkPhysicalDevice->VkInstance mapping 42 VkInstance instance; 43 44 // LayerChain::ActiveLayer array 45 void* layers; 46 uint32_t layer_count; 47 48 // debug.vulkan.enable_callback 49 PFN_vkDestroyDebugReportCallbackEXT destroy_debug_callback; 50 VkDebugReportCallbackEXT debug_callback; 51}; 52 53struct DeviceData { 54 DeviceDispatchTable dispatch; 55 56 // LayerChain::ActiveLayer array 57 void* layers; 58 uint32_t layer_count; 59}; 60 61} // namespace api 62 63namespace driver { 64 65struct InstanceData { 66 InstanceData(const VkAllocationCallbacks& alloc) 67 : opaque_api_data(), allocator(alloc) { 68 hook_extensions.set(ProcHook::EXTENSION_CORE); 69 hal_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 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions; 78}; 79 80struct DeviceData { 81 DeviceData(const VkAllocationCallbacks& alloc) 82 : opaque_api_data(), allocator(alloc), get_device_proc_addr(nullptr) { 83 hook_extensions.set(ProcHook::EXTENSION_CORE); 84 hal_extensions.set(ProcHook::EXTENSION_CORE); 85 } 86 87 api::DeviceData opaque_api_data; 88 89 const VkAllocationCallbacks allocator; 90 91 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions; 92 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions; 93 94 PFN_vkGetDeviceProcAddr get_device_proc_addr; 95}; 96 97bool Debuggable(); 98bool OpenHAL(); 99const VkAllocationCallbacks& GetDefaultAllocator(); 100 101// clang-format off 102VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName); 103VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName); 104VKAPI_ATTR VkResult EnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties); 105// clang-format on 106 107template <typename DispatchableType> 108void StaticAssertDispatchable(DispatchableType) { 109 static_assert(std::is_same<DispatchableType, VkInstance>::value || 110 std::is_same<DispatchableType, VkPhysicalDevice>::value || 111 std::is_same<DispatchableType, VkDevice>::value || 112 std::is_same<DispatchableType, VkQueue>::value || 113 std::is_same<DispatchableType, VkCommandBuffer>::value, 114 "unrecognized dispatchable type"); 115} 116 117template <typename DispatchableType> 118bool SetDataInternal(DispatchableType dispatchable, const void* data) { 119 StaticAssertDispatchable(dispatchable); 120 121 hwvulkan_dispatch_t* dispatch = 122 reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable); 123 // must be magic or already set 124 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) { 125 ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic); 126 return false; 127 } 128 129 dispatch->vtbl = data; 130 131 return true; 132} 133 134template <typename DispatchableType> 135void* GetDataInternal(DispatchableType dispatchable) { 136 StaticAssertDispatchable(dispatchable); 137 138 const hwvulkan_dispatch_t* dispatch = 139 reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable); 140 141 return const_cast<void*>(dispatch->vtbl); 142} 143 144inline bool SetData(VkInstance instance, const InstanceData& data) { 145 return SetDataInternal(instance, &data); 146} 147 148inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) { 149 return SetDataInternal(physical_dev, &data); 150} 151 152inline bool SetData(VkDevice dev, const DeviceData& data) { 153 return SetDataInternal(dev, &data); 154} 155 156inline bool SetData(VkQueue queue, const DeviceData& data) { 157 return SetDataInternal(queue, &data); 158} 159 160inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) { 161 return SetDataInternal(cmd, &data); 162} 163 164inline InstanceData& GetData(VkInstance instance) { 165 return *reinterpret_cast<InstanceData*>(GetDataInternal(instance)); 166} 167 168inline InstanceData& GetData(VkPhysicalDevice physical_dev) { 169 return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev)); 170} 171 172inline DeviceData& GetData(VkDevice dev) { 173 return *reinterpret_cast<DeviceData*>(GetDataInternal(dev)); 174} 175 176inline DeviceData& GetData(VkQueue queue) { 177 return *reinterpret_cast<DeviceData*>(GetDataInternal(queue)); 178} 179 180inline DeviceData& GetData(VkCommandBuffer cmd) { 181 return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd)); 182} 183 184} // namespace driver 185} // namespace vulkan 186 187#endif // LIBVULKAN_DRIVER_H 188