blob: 8569a52d0ee15e27dd6c427846e273b67fd4bb3f [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
112VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);
Chia-I Wu0c203242016-03-15 13:44:51 +0800113// clang-format on
114
115template <typename DispatchableType>
116void StaticAssertDispatchable(DispatchableType) {
117 static_assert(std::is_same<DispatchableType, VkInstance>::value ||
118 std::is_same<DispatchableType, VkPhysicalDevice>::value ||
119 std::is_same<DispatchableType, VkDevice>::value ||
120 std::is_same<DispatchableType, VkQueue>::value ||
121 std::is_same<DispatchableType, VkCommandBuffer>::value,
122 "unrecognized dispatchable type");
123}
124
125template <typename DispatchableType>
126bool SetDataInternal(DispatchableType dispatchable, const void* data) {
127 StaticAssertDispatchable(dispatchable);
128
129 hwvulkan_dispatch_t* dispatch =
130 reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable);
131 // must be magic or already set
132 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) {
133 ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic);
134 return false;
135 }
136
137 dispatch->vtbl = data;
138
139 return true;
140}
141
142template <typename DispatchableType>
143void* GetDataInternal(DispatchableType dispatchable) {
144 StaticAssertDispatchable(dispatchable);
145
146 const hwvulkan_dispatch_t* dispatch =
147 reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable);
148
149 return const_cast<void*>(dispatch->vtbl);
150}
151
152inline bool SetData(VkInstance instance, const InstanceData& data) {
153 return SetDataInternal(instance, &data);
154}
155
156inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) {
157 return SetDataInternal(physical_dev, &data);
158}
159
160inline bool SetData(VkDevice dev, const DeviceData& data) {
161 return SetDataInternal(dev, &data);
162}
163
164inline bool SetData(VkQueue queue, const DeviceData& data) {
165 return SetDataInternal(queue, &data);
166}
167
168inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) {
169 return SetDataInternal(cmd, &data);
170}
171
172inline InstanceData& GetData(VkInstance instance) {
173 return *reinterpret_cast<InstanceData*>(GetDataInternal(instance));
174}
175
176inline InstanceData& GetData(VkPhysicalDevice physical_dev) {
177 return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev));
178}
179
180inline DeviceData& GetData(VkDevice dev) {
181 return *reinterpret_cast<DeviceData*>(GetDataInternal(dev));
182}
183
184inline DeviceData& GetData(VkQueue queue) {
185 return *reinterpret_cast<DeviceData*>(GetDataInternal(queue));
186}
187
188inline DeviceData& GetData(VkCommandBuffer cmd) {
189 return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd));
190}
191
192} // namespace driver
193} // namespace vulkan
194
195#endif // LIBVULKAN_DRIVER_H