blob: 90a3827e4f83a34df54df343ad351215b72be9da [file] [log] [blame]
Jesse Hall80523e22016-01-06 16:47:54 -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
Jesse Hall80523e22016-01-06 16:47:54 -080017#include "loader.h"
18#include <alloca.h>
19#include <dirent.h>
20#include <dlfcn.h>
21#include <mutex>
22#include <sys/prctl.h>
23#include <string>
24#include <string.h>
25#include <vector>
26#include <log/log.h>
27#include <vulkan/vulkan_loader_data.h>
28
29using namespace vulkan;
30
31namespace vulkan {
32struct Layer {
33 VkLayerProperties properties;
34 size_t library_idx;
35 std::vector<VkExtensionProperties> extensions;
36};
37} // namespace vulkan
38
39namespace {
40
41std::mutex g_library_mutex;
42struct LayerLibrary {
43 std::string path;
44 void* dlhandle;
45 size_t refcount;
46};
47std::vector<LayerLibrary> g_layer_libraries;
48std::vector<Layer> g_layers;
49
50void AddLayerLibrary(const std::string& path) {
51 ALOGV("examining layer library '%s'", path.c_str());
52
53 void* dlhandle = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
54 if (!dlhandle) {
55 ALOGW("failed to load layer library '%s': %s", path.c_str(), dlerror());
56 return;
57 }
58
59 PFN_vkEnumerateInstanceLayerProperties enumerate_layer_properties =
60 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
61 dlsym(dlhandle, "vkEnumerateInstanceLayerProperties"));
62 if (!enumerate_layer_properties) {
63 ALOGW(
64 "failed to find vkEnumerateInstanceLayerProperties in library "
65 "'%s': %s",
66 path.c_str(), dlerror());
67 dlclose(dlhandle);
68 return;
69 }
70 PFN_vkEnumerateInstanceExtensionProperties enumerate_extension_properties =
71 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(
72 dlsym(dlhandle, "vkEnumerateInstanceExtensionProperties"));
73 if (!enumerate_extension_properties) {
74 ALOGW(
75 "failed to find vkEnumerateInstanceExtensionProperties in library "
76 "'%s': %s",
77 path.c_str(), dlerror());
78 dlclose(dlhandle);
79 return;
80 }
81
82 uint32_t layer_count;
83 VkResult result = enumerate_layer_properties(&layer_count, nullptr);
84 if (result != VK_SUCCESS) {
85 ALOGW("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
86 path.c_str(), result);
87 dlclose(dlhandle);
88 return;
89 }
90 VkLayerProperties* properties = static_cast<VkLayerProperties*>(
91 alloca(layer_count * sizeof(VkLayerProperties)));
92 result = enumerate_layer_properties(&layer_count, properties);
93 if (result != VK_SUCCESS) {
94 ALOGW("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
95 path.c_str(), result);
96 dlclose(dlhandle);
97 return;
98 }
99
100 size_t library_idx = g_layer_libraries.size();
101 g_layers.reserve(g_layers.size() + layer_count);
102 for (size_t i = 0; i < layer_count; i++) {
103 Layer layer;
104 layer.properties = properties[i];
105 layer.library_idx = library_idx;
106
107 uint32_t count;
108 result = enumerate_extension_properties(properties[i].layerName, &count,
109 nullptr);
110 if (result != VK_SUCCESS) {
111 ALOGW(
112 "vkEnumerateInstanceExtensionProperties(%s) failed for library "
113 "'%s': %d",
114 properties[i].layerName, path.c_str(), result);
115 g_layers.resize(g_layers.size() - (i + 1));
116 dlclose(dlhandle);
117 return;
118 }
119 layer.extensions.resize(count);
120 result = enumerate_extension_properties(properties[i].layerName, &count,
121 layer.extensions.data());
122 if (result != VK_SUCCESS) {
123 ALOGW(
124 "vkEnumerateInstanceExtensionProperties(%s) failed for library "
125 "'%s': %d",
126 properties[i].layerName, path.c_str(), result);
127 g_layers.resize(g_layers.size() - (i + 1));
128 dlclose(dlhandle);
129 return;
130 }
131
132 g_layers.push_back(layer);
133 ALOGV("found layer '%s'", properties[i].layerName);
134 }
135
136 dlclose(dlhandle);
137
138 g_layer_libraries.push_back(LayerLibrary{path, nullptr, 0});
139}
140
141void DiscoverLayersInDirectory(const std::string& dir_path) {
142 ALOGV("looking for layers in '%s'", dir_path.c_str());
143
144 DIR* directory = opendir(dir_path.c_str());
145 if (!directory) {
146 int err = errno;
147 ALOGV_IF(err != ENOENT, "failed to open layer directory '%s': %s (%d)",
148 dir_path.c_str(), strerror(err), err);
149 return;
150 }
151
152 std::string path;
153 path.reserve(dir_path.size() + 20);
154 path.append(dir_path);
155 path.append("/");
156
157 struct dirent* entry;
158 while ((entry = readdir(directory))) {
159 size_t libname_len = strlen(entry->d_name);
Jesse Halla7ac76d2016-01-08 22:29:42 -0800160 if (strncmp(entry->d_name, "libVkLayer", 10) != 0 ||
Jesse Hall80523e22016-01-06 16:47:54 -0800161 strncmp(entry->d_name + libname_len - 3, ".so", 3) != 0)
162 continue;
163 path.append(entry->d_name);
164 AddLayerLibrary(path);
165 path.resize(dir_path.size() + 1);
166 }
167
168 closedir(directory);
169}
170
171void* GetLayerGetProcAddr(const Layer& layer,
172 const char* gpa_name,
173 size_t gpa_name_len) {
174 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
175 void* gpa;
Jesse Hall30ac78b2016-01-11 21:29:40 -0800176 size_t layer_name_len = std::max(size_t{2}, strlen(layer.properties.layerName));
Jesse Hall80523e22016-01-06 16:47:54 -0800177 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
178 strcpy(name, layer.properties.layerName);
179 strcpy(name + layer_name_len, gpa_name);
180 if (!(gpa = dlsym(library.dlhandle, name))) {
181 strcpy(name, "vk");
182 strcpy(name + 2, gpa_name);
183 gpa = dlsym(library.dlhandle, name);
184 }
185 return gpa;
186}
187
188} // anonymous namespace
189
190namespace vulkan {
191
192void DiscoverLayers() {
193 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0))
194 DiscoverLayersInDirectory("/data/local/debug/vulkan");
195 if (!LoaderData::GetInstance().layer_path.empty())
196 DiscoverLayersInDirectory(LoaderData::GetInstance().layer_path.c_str());
197}
198
199uint32_t EnumerateLayers(uint32_t count, VkLayerProperties* properties) {
Jesse Hall30ac78b2016-01-11 21:29:40 -0800200 uint32_t n = std::min(count, static_cast<uint32_t>(g_layers.size()));
Jesse Hall80523e22016-01-06 16:47:54 -0800201 for (uint32_t i = 0; i < n; i++) {
202 properties[i] = g_layers[i].properties;
203 }
Jesse Hall30ac78b2016-01-11 21:29:40 -0800204 return static_cast<uint32_t>(g_layers.size());
Jesse Hall80523e22016-01-06 16:47:54 -0800205}
206
207void GetLayerExtensions(const char* name,
208 const VkExtensionProperties** properties,
209 uint32_t* count) {
210 for (const auto& layer : g_layers) {
211 if (strcmp(name, layer.properties.layerName) != 0)
212 continue;
213 *properties = layer.extensions.data();
Jesse Hall30ac78b2016-01-11 21:29:40 -0800214 *count = static_cast<uint32_t>(layer.extensions.size());
Jesse Hall80523e22016-01-06 16:47:54 -0800215 }
216}
217
218LayerRef GetLayerRef(const char* name) {
219 for (uint32_t id = 0; id < g_layers.size(); id++) {
220 if (strcmp(name, g_layers[id].properties.layerName) != 0) {
221 LayerLibrary& library = g_layer_libraries[g_layers[id].library_idx];
222 std::lock_guard<std::mutex> lock(g_library_mutex);
223 if (library.refcount++ == 0) {
224 library.dlhandle =
225 dlopen(library.path.c_str(), RTLD_NOW | RTLD_LOCAL);
226 if (!library.dlhandle) {
227 ALOGE("failed to load layer library '%s': %s",
228 library.path.c_str(), dlerror());
229 library.refcount = 0;
230 return LayerRef(nullptr);
231 }
232 }
233 return LayerRef(&g_layers[id]);
234 }
235 }
236 return LayerRef(nullptr);
237}
238
239LayerRef::LayerRef(Layer* layer) : layer_(layer) {}
240
241LayerRef::~LayerRef() {
242 if (layer_) {
243 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
244 std::lock_guard<std::mutex> lock(g_library_mutex);
245 if (--library.refcount == 0) {
246 dlclose(library.dlhandle);
247 library.dlhandle = nullptr;
248 }
249 }
250}
251
252LayerRef::LayerRef(LayerRef&& other) : layer_(std::move(other.layer_)) {}
253
254PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
255 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
256 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
257 : nullptr;
258}
259
260PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
261 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
262 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
263 : nullptr;
264}
265
Jesse Hall6bd5dfa2016-01-16 17:13:30 -0800266InstanceExtension InstanceExtensionFromName(const char* name) {
267 if (strcmp(name, VK_KHR_SURFACE_EXTENSION_NAME) == 0)
268 return kKHR_surface;
269 if (strcmp(name, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME) == 0)
270 return kKHR_android_surface;
271 if (strcmp(name, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == 0)
272 return kEXT_debug_report;
273 return kInstanceExtensionCount;
274}
275
Jesse Hall80523e22016-01-06 16:47:54 -0800276} // namespace vulkan