blob: 4cc3ee7df904b4697ceb372de5bb56b0d2dd3372 [file] [log] [blame]
Chia-I Wu0c203242016-03-15 13:44:51 +08001/*
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>
Chia-I Wueb7db122016-03-24 09:11:06 +080021#include <bitset>
Chia-I Wu0c203242016-03-15 13:44:51 +080022#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"
Chia-I Wueb7db122016-03-24 09:11:06 +080029#include "driver_gen.h"
Chia-I Wuff4a6c72016-03-24 16:05:56 +080030#include "debug_report.h"
Chia-I Wu0c203242016-03-15 13:44:51 +080031
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 {
Chia-I Wueb7db122016-03-24 09:11:06 +080067 InstanceData(const VkAllocationCallbacks& alloc)
Chia-I Wucc5e2762016-03-24 13:01:16 +080068 : opaque_api_data(),
69 allocator(alloc),
70 driver(),
71 get_device_proc_addr(nullptr) {
Chia-I Wueb7db122016-03-24 09:11:06 +080072 hook_extensions.set(ProcHook::EXTENSION_CORE);
73 hal_extensions.set(ProcHook::EXTENSION_CORE);
74 }
75
Chia-I Wu0c203242016-03-15 13:44:51 +080076 api::InstanceData opaque_api_data;
77
78 const VkAllocationCallbacks allocator;
Chia-I Wueb7db122016-03-24 09:11:06 +080079
80 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
81 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions;
Chia-I Wucc5e2762016-03-24 13:01:16 +080082
83 InstanceDriverTable driver;
84 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Chia-I Wuff4a6c72016-03-24 16:05:56 +080085
86 DebugReportCallbackList debug_report_callbacks;
Chia-I Wu0c203242016-03-15 13:44:51 +080087};
88
89struct DeviceData {
Chia-I Wueb7db122016-03-24 09:11:06 +080090 DeviceData(const VkAllocationCallbacks& alloc)
Chia-I Wucc5e2762016-03-24 13:01:16 +080091 : opaque_api_data(), allocator(alloc), driver() {
Chia-I Wueb7db122016-03-24 09:11:06 +080092 hook_extensions.set(ProcHook::EXTENSION_CORE);
93 hal_extensions.set(ProcHook::EXTENSION_CORE);
94 }
95
Chia-I Wu0c203242016-03-15 13:44:51 +080096 api::DeviceData opaque_api_data;
97
98 const VkAllocationCallbacks allocator;
Chia-I Wueb7db122016-03-24 09:11:06 +080099
100 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
101 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions;
102
Chia-I Wucc5e2762016-03-24 13:01:16 +0800103 DeviceDriverTable driver;
Chia-I Wu0c203242016-03-15 13:44:51 +0800104};
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);
Chia-I Wuba0be412016-03-24 16:24:40 +0800114
Chia-I Wu01cf3052016-03-24 16:16:21 +0800115VKAPI_ATTR VkResult EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
116
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800117VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance);
118VKAPI_ATTR void DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator);
Chia-I Wu4901db72016-03-24 16:38:58 +0800119VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice);
120VKAPI_ATTR void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator);
121
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800122VKAPI_ATTR VkResult EnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices);
Chia-I Wuba0be412016-03-24 16:24:40 +0800123VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);
Chia-I Wu6a58a8a2016-03-24 16:29:51 +0800124VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers);
Chia-I Wu0c203242016-03-15 13:44:51 +0800125// 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