driver.h revision 0c2032490b80178ec823bf22a7f5d08398851cc3
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 <type_traits>
22#include <log/log.h>
23
24#include <vulkan/vulkan.h>
25#include <hardware/hwvulkan.h>
26
27#include "api_gen.h"
28
29namespace vulkan {
30
31// This is here so that we can embed api::{Instance,Device}Data in
32// driver::{Instance,Device}Data to avoid pointer chasing.  They are
33// considered opaque to the driver layer.
34namespace api {
35
36struct InstanceData {
37    InstanceDispatchTable dispatch;
38
39    // for VkPhysicalDevice->VkInstance mapping
40    VkInstance instance;
41
42    // LayerChain::ActiveLayer array
43    void* layers;
44    uint32_t layer_count;
45
46    // debug.vulkan.enable_callback
47    PFN_vkDestroyDebugReportCallbackEXT destroy_debug_callback;
48    VkDebugReportCallbackEXT debug_callback;
49};
50
51struct DeviceData {
52    DeviceDispatchTable dispatch;
53
54    // LayerChain::ActiveLayer array
55    void* layers;
56    uint32_t layer_count;
57};
58
59}  // namespace api
60
61namespace driver {
62
63struct InstanceData {
64    api::InstanceData opaque_api_data;
65
66    const VkAllocationCallbacks allocator;
67};
68
69struct DeviceData {
70    api::DeviceData opaque_api_data;
71
72    const VkAllocationCallbacks allocator;
73};
74
75bool Debuggable();
76bool OpenHAL();
77const VkAllocationCallbacks& GetDefaultAllocator();
78
79// clang-format off
80VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName);
81VKAPI_ATTR PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName);
82VKAPI_ATTR VkResult EnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties);
83// clang-format on
84
85template <typename DispatchableType>
86void StaticAssertDispatchable(DispatchableType) {
87    static_assert(std::is_same<DispatchableType, VkInstance>::value ||
88                      std::is_same<DispatchableType, VkPhysicalDevice>::value ||
89                      std::is_same<DispatchableType, VkDevice>::value ||
90                      std::is_same<DispatchableType, VkQueue>::value ||
91                      std::is_same<DispatchableType, VkCommandBuffer>::value,
92                  "unrecognized dispatchable type");
93}
94
95template <typename DispatchableType>
96bool SetDataInternal(DispatchableType dispatchable, const void* data) {
97    StaticAssertDispatchable(dispatchable);
98
99    hwvulkan_dispatch_t* dispatch =
100        reinterpret_cast<hwvulkan_dispatch_t*>(dispatchable);
101    // must be magic or already set
102    if (dispatch->magic != HWVULKAN_DISPATCH_MAGIC && dispatch->vtbl != data) {
103        ALOGE("invalid dispatchable object magic 0x%" PRIxPTR, dispatch->magic);
104        return false;
105    }
106
107    dispatch->vtbl = data;
108
109    return true;
110}
111
112template <typename DispatchableType>
113void* GetDataInternal(DispatchableType dispatchable) {
114    StaticAssertDispatchable(dispatchable);
115
116    const hwvulkan_dispatch_t* dispatch =
117        reinterpret_cast<const hwvulkan_dispatch_t*>(dispatchable);
118
119    return const_cast<void*>(dispatch->vtbl);
120}
121
122inline bool SetData(VkInstance instance, const InstanceData& data) {
123    return SetDataInternal(instance, &data);
124}
125
126inline bool SetData(VkPhysicalDevice physical_dev, const InstanceData& data) {
127    return SetDataInternal(physical_dev, &data);
128}
129
130inline bool SetData(VkDevice dev, const DeviceData& data) {
131    return SetDataInternal(dev, &data);
132}
133
134inline bool SetData(VkQueue queue, const DeviceData& data) {
135    return SetDataInternal(queue, &data);
136}
137
138inline bool SetData(VkCommandBuffer cmd, const DeviceData& data) {
139    return SetDataInternal(cmd, &data);
140}
141
142inline InstanceData& GetData(VkInstance instance) {
143    return *reinterpret_cast<InstanceData*>(GetDataInternal(instance));
144}
145
146inline InstanceData& GetData(VkPhysicalDevice physical_dev) {
147    return *reinterpret_cast<InstanceData*>(GetDataInternal(physical_dev));
148}
149
150inline DeviceData& GetData(VkDevice dev) {
151    return *reinterpret_cast<DeviceData*>(GetDataInternal(dev));
152}
153
154inline DeviceData& GetData(VkQueue queue) {
155    return *reinterpret_cast<DeviceData*>(GetDataInternal(queue));
156}
157
158inline DeviceData& GetData(VkCommandBuffer cmd) {
159    return *reinterpret_cast<DeviceData*>(GetDataInternal(cmd));
160}
161
162}  // namespace driver
163}  // namespace vulkan
164
165#endif  // LIBVULKAN_DRIVER_H
166