blob: 23c717cf5d48bfa9c9a49ec8571ad933b5f5b1ec [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>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070021
Chia-I Wueb7db122016-03-24 09:11:06 +080022#include <bitset>
Chia-I Wu0c203242016-03-15 13:44:51 +080023#include <type_traits>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070024
Mark Salyzyn7823e122016-09-29 08:08:05 -070025#include <log/log.h>
Chia-I Wu0c203242016-03-15 13:44:51 +080026
27#include <vulkan/vulkan.h>
28#include <hardware/hwvulkan.h>
29
30#include "api_gen.h"
Chia-I Wueb7db122016-03-24 09:11:06 +080031#include "driver_gen.h"
Chia-I Wuff4a6c72016-03-24 16:05:56 +080032#include "debug_report.h"
Chia-I Wu4a6a9162016-03-26 07:17:34 +080033#include "swapchain.h"
Chia-I Wu0c203242016-03-15 13:44:51 +080034
35namespace vulkan {
36
37// This is here so that we can embed api::{Instance,Device}Data in
38// driver::{Instance,Device}Data to avoid pointer chasing. They are
39// considered opaque to the driver layer.
40namespace api {
41
42struct InstanceData {
43 InstanceDispatchTable dispatch;
44
Chia-I Wu0c203242016-03-15 13:44:51 +080045 // 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;
Chia-I Wu0c203242016-03-15 13:44:51 +080056};
57
58} // namespace api
59
60namespace driver {
61
Chia-I Wu94a2c0e2016-04-13 10:20:59 +080062VK_DEFINE_HANDLE(InstanceDispatchable)
63VK_DEFINE_HANDLE(DeviceDispatchable)
64
Chia-I Wu0c203242016-03-15 13:44:51 +080065struct InstanceData {
Chih-Hung Hsiehd56988d2016-09-01 11:37:47 -070066 explicit InstanceData(const VkAllocationCallbacks& alloc)
Chia-I Wucc5e2762016-03-24 13:01:16 +080067 : opaque_api_data(),
68 allocator(alloc),
69 driver(),
Yiwei Zhang7cc36a52019-10-11 19:02:09 -070070 get_device_proc_addr(nullptr) {}
Chia-I Wueb7db122016-03-24 09:11:06 +080071
Chia-I Wu0c203242016-03-15 13:44:51 +080072 api::InstanceData opaque_api_data;
73
74 const VkAllocationCallbacks allocator;
Chia-I Wueb7db122016-03-24 09:11:06 +080075
76 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
Chia-I Wucc5e2762016-03-24 13:01:16 +080077
78 InstanceDriverTable driver;
79 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Chia-I Wuff4a6c72016-03-24 16:05:56 +080080
81 DebugReportCallbackList debug_report_callbacks;
Chia-I Wu0c203242016-03-15 13:44:51 +080082};
83
84struct DeviceData {
Chia-I Wu950d6e12016-05-03 09:12:35 +080085 DeviceData(const VkAllocationCallbacks& alloc,
86 const DebugReportCallbackList& debug_report_callbacks_)
87 : opaque_api_data(),
88 allocator(alloc),
89 debug_report_callbacks(debug_report_callbacks_),
Yiwei Zhang7cc36a52019-10-11 19:02:09 -070090 driver() {}
Chia-I Wueb7db122016-03-24 09:11:06 +080091
Chia-I Wu0c203242016-03-15 13:44:51 +080092 api::DeviceData opaque_api_data;
93
94 const VkAllocationCallbacks allocator;
Chia-I Wu950d6e12016-05-03 09:12:35 +080095 const DebugReportCallbackList& debug_report_callbacks;
Chia-I Wueb7db122016-03-24 09:11:06 +080096
97 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
Chia-I Wueb7db122016-03-24 09:11:06 +080098
Jesse Halldc225072016-05-30 22:40:14 -070099 VkDevice driver_device;
Chia-I Wucc5e2762016-03-24 13:01:16 +0800100 DeviceDriverTable driver;
Chia-I Wu0c203242016-03-15 13:44:51 +0800101};
102
Chia-I Wu0c203242016-03-15 13:44:51 +0800103bool OpenHAL();
104const VkAllocationCallbacks& GetDefaultAllocator();
105
Chris Forbesfa25e632017-02-22 12:36:02 +1300106bool QueryPresentationProperties(
107 VkPhysicalDevice physicalDevice,
108 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties);
109
Chia-I Wu0c203242016-03-15 13:44:51 +0800110// 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);
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400123VKAPI_ATTR VkResult EnumeratePhysicalDeviceGroups(VkInstance instance, uint32_t* pPhysicalDeviceGroupCount, VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties);
124
Chia-I Wuba0be412016-03-24 16:24:40 +0800125VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);
Daniel Kochf25f5bb2017-10-05 00:26:58 -0400126VKAPI_ATTR void GetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2* pQueueInfo, VkQueue* pQueue);
Chia-I Wu6a58a8a2016-03-24 16:29:51 +0800127VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers);
Yiwei Zhang899d1752019-09-23 16:05:35 -0700128VKAPI_ATTR VkResult QueueSubmit(VkQueue queue, uint32_t submitCount, const VkSubmitInfo* pSubmits, VkFence fence);
Chia-I Wu0c203242016-03-15 13:44:51 +0800129// clang-format on
130
131template <typename DispatchableType>
132void StaticAssertDispatchable(DispatchableType) {
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800133 static_assert(
134 std::is_same<DispatchableType, VkInstance>::value ||
135 std::is_same<DispatchableType, VkPhysicalDevice>::value ||
136 std::is_same<DispatchableType, VkDevice>::value ||
137 std::is_same<DispatchableType, InstanceDispatchable>::value ||
138 std::is_same<DispatchableType, VkQueue>::value ||
139 std::is_same<DispatchableType, VkCommandBuffer>::value ||
140 std::is_same<DispatchableType, DeviceDispatchable>::value,
141 "unrecognized dispatchable type");
Chia-I Wu0c203242016-03-15 13:44:51 +0800142}
143
144template <typename DispatchableType>
145bool SetDataInternal(DispatchableType dispatchable, const void* data) {
146 StaticAssertDispatchable(dispatchable);
147
148 hwvulkan_dispatch_t* dispatch =
149 reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable);
150 // must be magic or already set
151 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) {
152 ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic);
153 return false;
154 }
155
156 dispatch->vtbl = data;
157
158 return true;
159}
160
161template <typename DispatchableType>
162void* GetDataInternal(DispatchableType dispatchable) {
163 StaticAssertDispatchable(dispatchable);
164
165 const hwvulkan_dispatch_t* dispatch =
166 reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable);
167
168 return const_cast<void*>(dispatch->vtbl);
169}
170
171inline bool SetData(VkInstance instance, const InstanceData& data) {
172 return SetDataInternal(instance, &data);
173}
174
175inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) {
176 return SetDataInternal(physical_dev, &data);
177}
178
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800179inline bool SetData(InstanceDispatchable dispatchable,
180 const InstanceData& data) {
181 return SetDataInternal(dispatchable, &data);
182}
183
Chia-I Wu0c203242016-03-15 13:44:51 +0800184inline bool SetData(VkDevice dev, const DeviceData& data) {
185 return SetDataInternal(dev, &data);
186}
187
188inline bool SetData(VkQueue queue, const DeviceData& data) {
189 return SetDataInternal(queue, &data);
190}
191
192inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) {
193 return SetDataInternal(cmd, &data);
194}
195
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800196inline bool SetData(DeviceDispatchable dispatchable, const DeviceData& data) {
197 return SetDataInternal(dispatchable, &data);
198}
199
Chia-I Wu0c203242016-03-15 13:44:51 +0800200inline InstanceData& GetData(VkInstance instance) {
201 return *reinterpret_cast<InstanceData*>(GetDataInternal(instance));
202}
203
204inline InstanceData& GetData(VkPhysicalDevice physical_dev) {
205 return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev));
206}
207
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800208inline InstanceData& GetData(InstanceDispatchable dispatchable) {
209 return *reinterpret_cast<InstanceData*>(GetDataInternal(dispatchable));
210}
211
Chia-I Wu0c203242016-03-15 13:44:51 +0800212inline DeviceData& GetData(VkDevice dev) {
213 return *reinterpret_cast<DeviceData*>(GetDataInternal(dev));
214}
215
216inline DeviceData& GetData(VkQueue queue) {
217 return *reinterpret_cast<DeviceData*>(GetDataInternal(queue));
218}
219
220inline DeviceData& GetData(VkCommandBuffer cmd) {
221 return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd));
222}
223
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800224inline DeviceData& GetData(DeviceDispatchable dispatchable) {
225 return *reinterpret_cast<DeviceData*>(GetDataInternal(dispatchable));
226}
227
Chia-I Wubc011fc2016-05-03 12:19:55 +0800228template <typename DispatchableType>
229const DebugReportLogger Logger(DispatchableType dispatchable) {
230 return DebugReportLogger(GetData(dispatchable).debug_report_callbacks);
231}
232
Chia-I Wu0c203242016-03-15 13:44:51 +0800233} // namespace driver
234} // namespace vulkan
235
236#endif // LIBVULKAN_DRIVER_H