blob: b3678dc242fbc1613a005d39ba65a94af435f27b [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 Wu0c203242016-03-15 13:44:51 +080030
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 {
Chia-I Wueb7db122016-03-24 09:11:06 +080066 InstanceData(const VkAllocationCallbacks& alloc)
Chia-I Wucc5e2762016-03-24 13:01:16 +080067 : opaque_api_data(),
68 allocator(alloc),
69 driver(),
70 get_device_proc_addr(nullptr) {
Chia-I Wueb7db122016-03-24 09:11:06 +080071 hook_extensions.set(ProcHook::EXTENSION_CORE);
72 hal_extensions.set(ProcHook::EXTENSION_CORE);
73 }
74
Chia-I Wu0c203242016-03-15 13:44:51 +080075 api::InstanceData opaque_api_data;
76
77 const VkAllocationCallbacks allocator;
Chia-I Wueb7db122016-03-24 09:11:06 +080078
79 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
80 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions;
Chia-I Wucc5e2762016-03-24 13:01:16 +080081
82 InstanceDriverTable driver;
83 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Chia-I Wu0c203242016-03-15 13:44:51 +080084};
85
86struct DeviceData {
Chia-I Wueb7db122016-03-24 09:11:06 +080087 DeviceData(const VkAllocationCallbacks& alloc)
Chia-I Wucc5e2762016-03-24 13:01:16 +080088 : opaque_api_data(), allocator(alloc), driver() {
Chia-I Wueb7db122016-03-24 09:11:06 +080089 hook_extensions.set(ProcHook::EXTENSION_CORE);
90 hal_extensions.set(ProcHook::EXTENSION_CORE);
91 }
92
Chia-I Wu0c203242016-03-15 13:44:51 +080093 api::DeviceData opaque_api_data;
94
95 const VkAllocationCallbacks allocator;
Chia-I Wueb7db122016-03-24 09:11:06 +080096
97 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
98 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions;
99
Chia-I Wucc5e2762016-03-24 13:01:16 +0800100 DeviceDriverTable driver;
Chia-I Wu0c203242016-03-15 13:44:51 +0800101};
102
103bool Debuggable();
104bool OpenHAL();
105const VkAllocationCallbacks& GetDefaultAllocator();
106
107// clang-format off
108VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName);
109VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName);
110VKAPI_ATTR VkResult EnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
Chia-I Wuba0be412016-03-24 16:24:40 +0800111
Chia-I Wu4901db72016-03-24 16:38:58 +0800112VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice);
113VKAPI_ATTR void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator);
114
Chia-I Wuba0be412016-03-24 16:24:40 +0800115VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);
Chia-I Wu6a58a8a2016-03-24 16:29:51 +0800116VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers);
Chia-I Wu0c203242016-03-15 13:44:51 +0800117// clang-format on
118
119template <typename DispatchableType>
120void StaticAssertDispatchable(DispatchableType) {
121 static_assert(std::is_same<DispatchableType, VkInstance>::value ||
122 std::is_same<DispatchableType, VkPhysicalDevice>::value ||
123 std::is_same<DispatchableType, VkDevice>::value ||
124 std::is_same<DispatchableType, VkQueue>::value ||
125 std::is_same<DispatchableType, VkCommandBuffer>::value,
126 "unrecognized dispatchable type");
127}
128
129template <typename DispatchableType>
130bool SetDataInternal(DispatchableType dispatchable, const void* data) {
131 StaticAssertDispatchable(dispatchable);
132
133 hwvulkan_dispatch_t* dispatch =
134 reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable);
135 // must be magic or already set
136 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) {
137 ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic);
138 return false;
139 }
140
141 dispatch->vtbl = data;
142
143 return true;
144}
145
146template <typename DispatchableType>
147void* GetDataInternal(DispatchableType dispatchable) {
148 StaticAssertDispatchable(dispatchable);
149
150 const hwvulkan_dispatch_t* dispatch =
151 reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable);
152
153 return const_cast<void*>(dispatch->vtbl);
154}
155
156inline bool SetData(VkInstance instance, const InstanceData& data) {
157 return SetDataInternal(instance, &data);
158}
159
160inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) {
161 return SetDataInternal(physical_dev, &data);
162}
163
164inline bool SetData(VkDevice dev, const DeviceData& data) {
165 return SetDataInternal(dev, &data);
166}
167
168inline bool SetData(VkQueue queue, const DeviceData& data) {
169 return SetDataInternal(queue, &data);
170}
171
172inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) {
173 return SetDataInternal(cmd, &data);
174}
175
176inline InstanceData& GetData(VkInstance instance) {
177 return *reinterpret_cast<InstanceData*>(GetDataInternal(instance));
178}
179
180inline InstanceData& GetData(VkPhysicalDevice physical_dev) {
181 return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev));
182}
183
184inline DeviceData& GetData(VkDevice dev) {
185 return *reinterpret_cast<DeviceData*>(GetDataInternal(dev));
186}
187
188inline DeviceData& GetData(VkQueue queue) {
189 return *reinterpret_cast<DeviceData*>(GetDataInternal(queue));
190}
191
192inline DeviceData& GetData(VkCommandBuffer cmd) {
193 return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd));
194}
195
196} // namespace driver
197} // namespace vulkan
198
199#endif // LIBVULKAN_DRIVER_H