blob: 22db93f5778fe9248703c12811865b300475786d [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 Wu4a6a9162016-03-26 07:17:34 +080031#include "swapchain.h"
Chia-I Wu0c203242016-03-15 13:44:51 +080032
33namespace vulkan {
34
35// This is here so that we can embed api::{Instance,Device}Data in
36// driver::{Instance,Device}Data to avoid pointer chasing. They are
37// considered opaque to the driver layer.
38namespace api {
39
40struct InstanceData {
41 InstanceDispatchTable dispatch;
42
43 // for VkPhysicalDevice->VkInstance mapping
44 VkInstance instance;
45
46 // LayerChain::ActiveLayer array
47 void* layers;
48 uint32_t layer_count;
49
50 // debug.vulkan.enable_callback
51 PFN_vkDestroyDebugReportCallbackEXT destroy_debug_callback;
52 VkDebugReportCallbackEXT debug_callback;
53};
54
55struct DeviceData {
56 DeviceDispatchTable dispatch;
57
58 // LayerChain::ActiveLayer array
59 void* layers;
60 uint32_t layer_count;
61};
62
63} // namespace api
64
65namespace driver {
66
67struct InstanceData {
Chia-I Wueb7db122016-03-24 09:11:06 +080068 InstanceData(const VkAllocationCallbacks& alloc)
Chia-I Wucc5e2762016-03-24 13:01:16 +080069 : opaque_api_data(),
70 allocator(alloc),
71 driver(),
72 get_device_proc_addr(nullptr) {
Chia-I Wueb7db122016-03-24 09:11:06 +080073 hook_extensions.set(ProcHook::EXTENSION_CORE);
74 hal_extensions.set(ProcHook::EXTENSION_CORE);
75 }
76
Chia-I Wu0c203242016-03-15 13:44:51 +080077 api::InstanceData opaque_api_data;
78
79 const VkAllocationCallbacks allocator;
Chia-I Wueb7db122016-03-24 09:11:06 +080080
81 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
82 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions;
Chia-I Wucc5e2762016-03-24 13:01:16 +080083
84 InstanceDriverTable driver;
85 PFN_vkGetDeviceProcAddr get_device_proc_addr;
Chia-I Wuff4a6c72016-03-24 16:05:56 +080086
87 DebugReportCallbackList debug_report_callbacks;
Chia-I Wu0c203242016-03-15 13:44:51 +080088};
89
90struct DeviceData {
Chia-I Wueb7db122016-03-24 09:11:06 +080091 DeviceData(const VkAllocationCallbacks& alloc)
Chia-I Wucc5e2762016-03-24 13:01:16 +080092 : opaque_api_data(), allocator(alloc), driver() {
Chia-I Wueb7db122016-03-24 09:11:06 +080093 hook_extensions.set(ProcHook::EXTENSION_CORE);
94 hal_extensions.set(ProcHook::EXTENSION_CORE);
95 }
96
Chia-I Wu0c203242016-03-15 13:44:51 +080097 api::DeviceData opaque_api_data;
98
99 const VkAllocationCallbacks allocator;
Chia-I Wueb7db122016-03-24 09:11:06 +0800100
101 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
102 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions;
103
Chia-I Wucc5e2762016-03-24 13:01:16 +0800104 DeviceDriverTable driver;
Chia-I Wu0c203242016-03-15 13:44:51 +0800105};
106
107bool Debuggable();
108bool OpenHAL();
109const VkAllocationCallbacks& GetDefaultAllocator();
110
111// clang-format off
112VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName);
113VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName);
114VKAPI_ATTR VkResult EnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
Chia-I Wuba0be412016-03-24 16:24:40 +0800115
Chia-I Wu01cf3052016-03-24 16:16:21 +0800116VKAPI_ATTR VkResult EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
117
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800118VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance);
119VKAPI_ATTR void DestroyInstance(VkInstance instance, const VkAllocationCallbacks* pAllocator);
Chia-I Wu4901db72016-03-24 16:38:58 +0800120VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice);
121VKAPI_ATTR void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator);
122
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800123VKAPI_ATTR VkResult EnumeratePhysicalDevices(VkInstance instance, uint32_t* pPhysicalDeviceCount, VkPhysicalDevice* pPhysicalDevices);
Chia-I Wuba0be412016-03-24 16:24:40 +0800124VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);
Chia-I Wu6a58a8a2016-03-24 16:29:51 +0800125VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers);
Chia-I Wu0c203242016-03-15 13:44:51 +0800126// clang-format on
127
128template <typename DispatchableType>
129void StaticAssertDispatchable(DispatchableType) {
130 static_assert(std::is_same<DispatchableType, VkInstance>::value ||
131 std::is_same<DispatchableType, VkPhysicalDevice>::value ||
132 std::is_same<DispatchableType, VkDevice>::value ||
133 std::is_same<DispatchableType, VkQueue>::value ||
134 std::is_same<DispatchableType, VkCommandBuffer>::value,
135 "unrecognized dispatchable type");
136}
137
138template <typename DispatchableType>
139bool SetDataInternal(DispatchableType dispatchable, const void* data) {
140 StaticAssertDispatchable(dispatchable);
141
142 hwvulkan_dispatch_t* dispatch =
143 reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable);
144 // must be magic or already set
145 if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) {
146 ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic);
147 return false;
148 }
149
150 dispatch->vtbl = data;
151
152 return true;
153}
154
155template <typename DispatchableType>
156void* GetDataInternal(DispatchableType dispatchable) {
157 StaticAssertDispatchable(dispatchable);
158
159 const hwvulkan_dispatch_t* dispatch =
160 reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable);
161
162 return const_cast<void*>(dispatch->vtbl);
163}
164
165inline bool SetData(VkInstance instance, const InstanceData& data) {
166 return SetDataInternal(instance, &data);
167}
168
169inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) {
170 return SetDataInternal(physical_dev, &data);
171}
172
173inline bool SetData(VkDevice dev, const DeviceData& data) {
174 return SetDataInternal(dev, &data);
175}
176
177inline bool SetData(VkQueue queue, const DeviceData& data) {
178 return SetDataInternal(queue, &data);
179}
180
181inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) {
182 return SetDataInternal(cmd, &data);
183}
184
185inline InstanceData& GetData(VkInstance instance) {
186 return *reinterpret_cast<InstanceData*>(GetDataInternal(instance));
187}
188
189inline InstanceData& GetData(VkPhysicalDevice physical_dev) {
190 return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev));
191}
192
193inline DeviceData& GetData(VkDevice dev) {
194 return *reinterpret_cast<DeviceData*>(GetDataInternal(dev));
195}
196
197inline DeviceData& GetData(VkQueue queue) {
198 return *reinterpret_cast<DeviceData*>(GetDataInternal(queue));
199}
200
201inline DeviceData& GetData(VkCommandBuffer cmd) {
202 return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd));
203}
204
205} // namespace driver
206} // namespace vulkan
207
208#endif // LIBVULKAN_DRIVER_H