driver.h revision 01cf305325f3789c573d7eff435e409f04677c66
1/*
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>
21#include <bitset>
22#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"
29#include "driver_gen.h"
30
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 {
66    InstanceData(const VkAllocationCallbacks& alloc)
67        : opaque_api_data(),
68          allocator(alloc),
69          driver(),
70          get_device_proc_addr(nullptr) {
71        hook_extensions.set(ProcHook::EXTENSION_CORE);
72        hal_extensions.set(ProcHook::EXTENSION_CORE);
73    }
74
75    api::InstanceData opaque_api_data;
76
77    const VkAllocationCallbacks allocator;
78
79    std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
80    std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions;
81
82    InstanceDriverTable driver;
83    PFN_vkGetDeviceProcAddr get_device_proc_addr;
84};
85
86struct DeviceData {
87    DeviceData(const VkAllocationCallbacks& alloc)
88        : opaque_api_data(), allocator(alloc), driver() {
89        hook_extensions.set(ProcHook::EXTENSION_CORE);
90        hal_extensions.set(ProcHook::EXTENSION_CORE);
91    }
92
93    api::DeviceData opaque_api_data;
94
95    const VkAllocationCallbacks allocator;
96
97    std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions;
98    std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions;
99
100    DeviceDriverTable driver;
101};
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);
111
112VKAPI_ATTR VkResult EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice, const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
113
114VKAPI_ATTR VkResult CreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice);
115VKAPI_ATTR void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator);
116
117VKAPI_ATTR void GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);
118VKAPI_ATTR VkResult AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo* pAllocateInfo, VkCommandBuffer* pCommandBuffers);
119// 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
202