blob: f8daa00a9bcf88394670cf10c831781c90f1c698 [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 Wu01cf3052016-03-24 16:16:21 +0800112VKAPI_ATTR VkResult EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
113
Chia-I Wu4901db72016-03-24 16:38:58 +0800114VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice);
115VKAPI_ATTR void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator);
116
Chia-I Wuba0be412016-03-24 16:24:40 +0800117VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);
Chia-I Wu6a58a8a2016-03-24 16:29:51 +0800118VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers);
Chia-I Wu0c203242016-03-15 13:44:51 +0800119// clang-format on
120
121template <typename DispatchableType>
122void StaticAssertDispatchable(DispatchableType) {
123 static_assert(std::is_same<DispatchableType, VkInstance>::value ||
124 std::is_same<DispatchableType, VkPhysicalDevice>::value ||
125 std::is_same<DispatchableType, VkDevice>::value ||
126 std::is_same<DispatchableType, VkQueue>::value ||
127 std::is_same<DispatchableType, VkCommandBuffer>::value,
128 "unrecognized dispatchable type");
129}
130
131template <typename DispatchableType>
132bool SetDataInternal(DispatchableType dispatchable, const void* data) {
133 StaticAssertDispatchable(dispatchable);
134
135 hwvulkan_dispatch_t* dispatch =
136 reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable);
137 // must be magic or already set
138 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) {
139 ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic);
140 return false;
141 }
142
143 dispatch->vtbl = data;
144
145 return true;
146}
147
148template <typename DispatchableType>
149void* GetDataInternal(DispatchableType dispatchable) {
150 StaticAssertDispatchable(dispatchable);
151
152 const hwvulkan_dispatch_t* dispatch =
153 reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable);
154
155 return const_cast<void*>(dispatch->vtbl);
156}
157
158inline bool SetData(VkInstance instance, const InstanceData& data) {
159 return SetDataInternal(instance, &data);
160}
161
162inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) {
163 return SetDataInternal(physical_dev, &data);
164}
165
166inline bool SetData(VkDevice dev, const DeviceData& data) {
167 return SetDataInternal(dev, &data);
168}
169
170inline bool SetData(VkQueue queue, const DeviceData& data) {
171 return SetDataInternal(queue, &data);
172}
173
174inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) {
175 return SetDataInternal(cmd, &data);
176}
177
178inline InstanceData& GetData(VkInstance instance) {
179 return *reinterpret_cast<InstanceData*>(GetDataInternal(instance));
180}
181
182inline InstanceData& GetData(VkPhysicalDevice physical_dev) {
183 return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev));
184}
185
186inline DeviceData& GetData(VkDevice dev) {
187 return *reinterpret_cast<DeviceData*>(GetDataInternal(dev));
188}
189
190inline DeviceData& GetData(VkQueue queue) {
191 return *reinterpret_cast<DeviceData*>(GetDataInternal(queue));
192}
193
194inline DeviceData& GetData(VkCommandBuffer cmd) {
195 return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd));
196}
197
198} // namespace driver
199} // namespace vulkan
200
201#endif // LIBVULKAN_DRIVER_H