• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* NOTE:
18  * This stub HAL is only used internally by the loader when a real HAL
19  * implementation is not present, in order to avoid needing "null HAL" checks
20  * throughout the loader. It does not enumerate any physical devices, and is
21  * only as conformant to the Vulkan and Android HAL interfaces as the loader
22  * needs it to be. Do not use it as an example of a correct implementation; the
23  * code in ../null_driver is better for that.
24  */
25 
26 #undef LOG_TAG
27 #define LOG_TAG "vkstub"
28 
29 #include <array>
30 #include <bitset>
31 #include <cstring>
32 #include <mutex>
33 
34 #include <log/log.h>
35 #include <hardware/hwvulkan.h>
36 
37 #include "stubhal.h"
38 
39 namespace vulkan {
40 namespace stubhal {
41 
42 namespace {
43 
44 const size_t kMaxInstances = 32;
45 static std::mutex g_instance_mutex;
46 static std::bitset<kMaxInstances> g_instance_used(false);
47 static std::array<hwvulkan_dispatch_t, kMaxInstances> g_instances;
48 
NoOp()49 [[noreturn]] VKAPI_ATTR void NoOp() {
50     LOG_ALWAYS_FATAL("invalid stub function called");
51 }
52 
53 VKAPI_ATTR VkResult
EnumerateInstanceExtensionProperties(const char *,uint32_t * count,VkExtensionProperties *)54 EnumerateInstanceExtensionProperties(const char* /*layer_name*/,
55                                      uint32_t* count,
56                                      VkExtensionProperties* /*properties*/) {
57     *count = 0;
58     return VK_SUCCESS;
59 }
60 
61 VKAPI_ATTR VkResult
EnumerateInstanceLayerProperties(uint32_t * count,VkLayerProperties *)62 EnumerateInstanceLayerProperties(uint32_t* count,
63                                  VkLayerProperties* /*properties*/) {
64     *count = 0;
65     return VK_SUCCESS;
66 }
67 
CreateInstance(const VkInstanceCreateInfo *,const VkAllocationCallbacks *,VkInstance * instance)68 VKAPI_ATTR VkResult CreateInstance(const VkInstanceCreateInfo* /*create_info*/,
69                                    const VkAllocationCallbacks* /*allocator*/,
70                                    VkInstance* instance) {
71     std::lock_guard<std::mutex> lock(g_instance_mutex);
72     for (size_t i = 0; i < kMaxInstances; i++) {
73         if (!g_instance_used[i]) {
74             g_instance_used[i] = true;
75             g_instances[i].magic = HWVULKAN_DISPATCH_MAGIC;
76             *instance = reinterpret_cast<VkInstance>(&g_instances[i]);
77             return VK_SUCCESS;
78         }
79     }
80     ALOGE("no more instances available (max=%zu)", kMaxInstances);
81     return VK_ERROR_INITIALIZATION_FAILED;
82 }
83 
DestroyInstance(VkInstance instance,const VkAllocationCallbacks *)84 VKAPI_ATTR void DestroyInstance(VkInstance instance,
85                                 const VkAllocationCallbacks* /*allocator*/) {
86     std::lock_guard<std::mutex> lock(g_instance_mutex);
87     ssize_t idx =
88         reinterpret_cast<hwvulkan_dispatch_t*>(instance) - &g_instances[0];
89     ALOG_ASSERT(idx >= 0 && static_cast<size_t>(idx) < g_instance_used.size(),
90                 "DestroyInstance: invalid instance handle");
91     g_instance_used[static_cast<size_t>(idx)] = false;
92 }
93 
EnumeratePhysicalDevices(VkInstance,uint32_t * count,VkPhysicalDevice *)94 VKAPI_ATTR VkResult EnumeratePhysicalDevices(VkInstance /*instance*/,
95                                              uint32_t* count,
96                                              VkPhysicalDevice* /*gpus*/) {
97     *count = 0;
98     return VK_SUCCESS;
99 }
100 
101 VKAPI_ATTR VkResult
EnumeratePhysicalDeviceGroups(VkInstance,uint32_t * count,VkPhysicalDeviceGroupProperties *)102 EnumeratePhysicalDeviceGroups(VkInstance /*instance*/,
103                               uint32_t* count,
104                               VkPhysicalDeviceGroupProperties* /*properties*/) {
105     *count = 0;
106     return VK_SUCCESS;
107 }
108 
GetInstanceProcAddr(VkInstance instance,const char * name)109 VKAPI_ATTR PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance,
110                                                   const char* name) {
111     if (strcmp(name, "vkCreateInstance") == 0)
112         return reinterpret_cast<PFN_vkVoidFunction>(CreateInstance);
113     if (strcmp(name, "vkDestroyInstance") == 0)
114         return reinterpret_cast<PFN_vkVoidFunction>(DestroyInstance);
115     if (strcmp(name, "vkEnumerateInstanceExtensionProperties") == 0)
116         return reinterpret_cast<PFN_vkVoidFunction>(
117             EnumerateInstanceExtensionProperties);
118     if (strcmp(name, "vkEnumeratePhysicalDevices") == 0)
119         return reinterpret_cast<PFN_vkVoidFunction>(EnumeratePhysicalDevices);
120     if (strcmp(name, "vkEnumeratePhysicalDeviceGroups") == 0)
121         return reinterpret_cast<PFN_vkVoidFunction>(
122             EnumeratePhysicalDeviceGroups);
123     if (strcmp(name, "vkGetInstanceProcAddr") == 0)
124         return reinterpret_cast<PFN_vkVoidFunction>(GetInstanceProcAddr);
125     // Per the spec, return NULL if instance is NULL.
126     if (!instance)
127         return nullptr;
128     // None of the other Vulkan functions should ever be called, as they all
129     // take a VkPhysicalDevice or other object obtained from a physical device.
130     return reinterpret_cast<PFN_vkVoidFunction>(NoOp);
131 }
132 
133 }  // anonymous namespace
134 
135 const hwvulkan_device_t kDevice = {
136     .common =
137         {
138             .tag = HARDWARE_DEVICE_TAG,
139             .version = HWVULKAN_DEVICE_API_VERSION_0_1,
140             .module = nullptr,
141             .close = nullptr,
142         },
143     .EnumerateInstanceExtensionProperties =
144         EnumerateInstanceExtensionProperties,
145     .CreateInstance = CreateInstance,
146     .GetInstanceProcAddr = GetInstanceProcAddr,
147 };
148 
149 }  // namespace stubhal
150 }  // namespace vulkan
151