blob: 563e5c479b35fe3a194c0985cb5916144c87fced [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
Chia-I Wuc96880f2016-03-26 06:56:45 +080017#include "layers_extensions.h"
Jesse Hall1a7eb592016-05-01 21:04:40 +020018
Jesse Hall80523e22016-01-06 16:47:54 -080019#include <alloca.h>
20#include <dirent.h>
21#include <dlfcn.h>
22#include <mutex>
23#include <sys/prctl.h>
24#include <string>
25#include <string.h>
26#include <vector>
Jesse Hall1a7eb592016-05-01 21:04:40 +020027
28#include <android-base/strings.h>
29#include <cutils/properties.h>
Jesse Hall80523e22016-01-06 16:47:54 -080030#include <log/log.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020031#include <ziparchive/zip_archive.h>
32
Jesse Hall80523e22016-01-06 16:47:54 -080033#include <vulkan/vulkan_loader_data.h>
34
Jesse Hallb1471272016-01-17 21:36:58 -080035// TODO(jessehall): The whole way we deal with extensions is pretty hokey, and
36// not a good long-term solution. Having a hard-coded enum of extensions is
37// bad, of course. Representing sets of extensions (requested, supported, etc.)
38// as a bitset isn't necessarily bad, if the mapping from extension to bit were
39// dynamic. Need to rethink this completely when there's a little more time.
40
Jesse Hallaa410942016-01-17 13:07:10 -080041// TODO(jessehall): This file currently builds up global data structures as it
42// loads, and never cleans them up. This means we're doing heap allocations
43// without going through an app-provided allocator, but worse, we'll leak those
44// allocations if the loader is unloaded.
45//
46// We should allocate "enough" BSS space, and suballocate from there. Will
47// probably want to intern strings, etc., and will need some custom/manual data
48// structures.
49
Jesse Hall80523e22016-01-06 16:47:54 -080050namespace vulkan {
Chia-I Wuc96880f2016-03-26 06:56:45 +080051namespace api {
52
Jesse Hall80523e22016-01-06 16:47:54 -080053struct Layer {
54 VkLayerProperties properties;
55 size_t library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +080056
Chia-I Wu25700b42016-04-28 06:36:09 +080057 // true if the layer intercepts vkCreateDevice and device commands
Chia-I Wubea09db2016-04-22 09:42:41 +080058 bool is_global;
59
60 std::vector<VkExtensionProperties> instance_extensions;
61 std::vector<VkExtensionProperties> device_extensions;
Jesse Hall80523e22016-01-06 16:47:54 -080062};
Jesse Hall80523e22016-01-06 16:47:54 -080063
64namespace {
65
Jesse Hall1a7eb592016-05-01 21:04:40 +020066const char kSystemLayerLibraryDir[] = "/data/local/debug/vulkan";
67
Chia-I Wu6693f5c2016-04-18 12:20:02 +080068class LayerLibrary {
69 public:
70 LayerLibrary(const std::string& path)
71 : path_(path), dlhandle_(nullptr), refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080072
Chia-I Wu50174ee2016-04-18 16:33:20 +080073 LayerLibrary(LayerLibrary&& other)
Chia-I Wu6693f5c2016-04-18 12:20:02 +080074 : path_(std::move(other.path_)),
75 dlhandle_(other.dlhandle_),
76 refcount_(other.refcount_) {
77 other.dlhandle_ = nullptr;
78 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080079 }
80
81 LayerLibrary(const LayerLibrary&) = delete;
82 LayerLibrary& operator=(const LayerLibrary&) = delete;
83
Chia-I Wua6229742016-04-26 07:37:44 +080084 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080085 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080086 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080087
Chia-I Wu50174ee2016-04-18 16:33:20 +080088 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080089 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +080090
Chia-I Wuba113272016-04-18 16:38:39 +080091 void* GetGPA(const Layer& layer,
92 const char* gpa_name,
93 size_t gpa_name_len) const;
94
Chia-I Wu6693f5c2016-04-18 12:20:02 +080095 private:
96 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +080097
98 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +080099 void* dlhandle_;
100 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800101};
Chia-I Wu74349592016-04-18 12:08:39 +0800102
Chia-I Wufd0389f2016-04-18 12:11:00 +0800103bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800104 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800105 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200106 ALOGV("opening layer library '%s'", path_.c_str());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800107 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800108 if (!dlhandle_) {
109 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
Chia-I Wufd0389f2016-04-18 12:11:00 +0800110 dlerror());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800111 refcount_ = 0;
Chia-I Wufd0389f2016-04-18 12:11:00 +0800112 return false;
113 }
114 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800115 return true;
116}
117
Chia-I Wud91c74f2016-04-18 12:12:36 +0800118void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800119 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800120 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200121 ALOGV("closing layer library '%s'", path_.c_str());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800122 dlclose(dlhandle_);
123 dlhandle_ = nullptr;
Chia-I Wud91c74f2016-04-18 12:12:36 +0800124 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800125}
126
Chia-I Wu50174ee2016-04-18 16:33:20 +0800127bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800128 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800129 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Jesse Hall80523e22016-01-06 16:47:54 -0800130 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800131 dlsym(dlhandle_, "vkEnumerateInstanceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800132 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Jesse Hall80523e22016-01-06 16:47:54 -0800133 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800134 dlsym(dlhandle_, "vkEnumerateInstanceExtensionProperties"));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800135 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200136 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800137 path_.c_str());
138 return false;
139 }
140
141 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800142 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
143 reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800144 dlsym(dlhandle_, "vkEnumerateDeviceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800145 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
146 reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800147 dlsym(dlhandle_, "vkEnumerateDeviceExtensionProperties"));
Jesse Hall80523e22016-01-06 16:47:54 -0800148
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800149 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800150 uint32_t num_instance_layers = 0;
151 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800152 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
153 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800154 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200155 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800156 "vkEnumerateInstanceLayerProperties failed for library '%s': "
157 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800158 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800159 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800160 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800161 }
Jesse Hallaa410942016-01-17 13:07:10 -0800162 if (enumerate_device_layers) {
163 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
164 nullptr);
165 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200166 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800167 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800168 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800169 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800170 }
171 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800172
173 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800174 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
175 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800176 result = enumerate_instance_layers(&num_instance_layers, properties);
177 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200178 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800179 path_.c_str(), result);
180 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800181 }
182 if (num_device_layers > 0) {
183 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
184 properties + num_instance_layers);
185 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200186 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800187 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800188 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800189 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800190 }
Jesse Hall80523e22016-01-06 16:47:54 -0800191 }
192
Chia-I Wubea09db2016-04-22 09:42:41 +0800193 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800194 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800195 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800196 for (size_t i = 0; i < num_instance_layers; i++) {
197 const VkLayerProperties& props = properties[i];
198
Jesse Hall80523e22016-01-06 16:47:54 -0800199 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800200 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800201 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800202 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800203
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800204 uint32_t count = 0;
205 result =
206 enumerate_instance_extensions(props.layerName, &count, nullptr);
207 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200208 ALOGE(
209 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
210 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800211 props.layerName, path_.c_str(), result);
212 instance_layers.resize(prev_num_instance_layers);
213 return false;
214 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800215 layer.instance_extensions.resize(count);
216 result = enumerate_instance_extensions(
217 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800218 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200219 ALOGE(
220 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
221 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800222 props.layerName, path_.c_str(), result);
223 instance_layers.resize(prev_num_instance_layers);
224 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800225 }
226
Chia-I Wu279ccc02016-04-18 16:45:15 +0800227 for (size_t j = 0; j < num_device_layers; j++) {
228 const auto& dev_props = properties[num_instance_layers + j];
229 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800230 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800231 break;
232 }
233 }
Jesse Hallaa410942016-01-17 13:07:10 -0800234
Chia-I Wubea09db2016-04-22 09:42:41 +0800235 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800236 result = enumerate_device_extensions(
237 VK_NULL_HANDLE, props.layerName, &count, nullptr);
238 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200239 ALOGE(
240 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800241 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800242 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800243 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800244 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800245 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800246 layer.device_extensions.resize(count);
247 result = enumerate_device_extensions(
248 VK_NULL_HANDLE, props.layerName, &count,
249 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800250 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200251 ALOGE(
252 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800253 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800254 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800255 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800256 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800257 }
258 }
259
Chia-I Wubea09db2016-04-22 09:42:41 +0800260 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200261 ALOGD("added %s layer '%s' from library '%s'",
262 (layer.is_global) ? "global" : "instance", props.layerName,
263 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800264 }
265
Chia-I Wu50174ee2016-04-18 16:33:20 +0800266 return true;
267}
Jesse Hall80523e22016-01-06 16:47:54 -0800268
Chia-I Wuba113272016-04-18 16:38:39 +0800269void* LayerLibrary::GetGPA(const Layer& layer,
270 const char* gpa_name,
271 size_t gpa_name_len) const {
272 void* gpa;
273 size_t layer_name_len =
274 std::max(size_t{2}, strlen(layer.properties.layerName));
275 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
276 strcpy(name, layer.properties.layerName);
277 strcpy(name + layer_name_len, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800278 if (!(gpa = dlsym(dlhandle_, name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800279 strcpy(name, "vk");
280 strcpy(name + 2, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800281 gpa = dlsym(dlhandle_, name);
Chia-I Wuba113272016-04-18 16:38:39 +0800282 }
283 return gpa;
284}
285
Jesse Hall1a7eb592016-05-01 21:04:40 +0200286// ----------------------------------------------------------------------------
287
Chia-I Wu50174ee2016-04-18 16:33:20 +0800288std::vector<LayerLibrary> g_layer_libraries;
289std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800290
291void AddLayerLibrary(const std::string& path) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800292 LayerLibrary library(path);
293 if (!library.Open())
294 return;
295
Chia-I Wubea09db2016-04-22 09:42:41 +0800296 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800297 library.Close();
298 return;
299 }
300
301 library.Close();
302
303 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800304}
305
Jesse Hall1a7eb592016-05-01 21:04:40 +0200306template <typename Functor>
307void ForEachFileInDir(const std::string& dirname, Functor functor) {
308 auto dir_deleter = [](DIR* handle) { closedir(handle); };
309 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
310 dir_deleter);
311 if (!dir) {
312 // It's normal for some search directories to not exist, especially
313 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800314 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200315 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
316 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800317 return;
318 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200319 ALOGD("searching for layers in '%s'", dirname.c_str());
320 dirent* entry;
321 while ((entry = readdir(dir.get())) != nullptr)
322 functor(entry->d_name);
323}
Jesse Hall80523e22016-01-06 16:47:54 -0800324
Jesse Hall1a7eb592016-05-01 21:04:40 +0200325template <typename Functor>
326void ForEachFileInZip(const std::string& zipname,
327 const std::string& dir_in_zip,
328 Functor functor) {
329 int32_t err;
330 ZipArchiveHandle zip = nullptr;
331 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
332 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
333 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800334 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200335 std::string prefix(dir_in_zip + "/");
336 const ZipString prefix_str(prefix.c_str());
337 void* iter_cookie = nullptr;
338 if ((err = StartIteration(zip, &iter_cookie, &prefix_str, nullptr)) != 0) {
339 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
340 err);
341 CloseArchive(zip);
342 return;
343 }
344 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
345 dir_in_zip.c_str());
346 ZipEntry entry;
347 ZipString name;
348 while (Next(iter_cookie, &entry, &name) == 0) {
349 std::string filename(
350 reinterpret_cast<const char*>(name.name) + prefix.length(),
351 name.name_length - prefix.length());
352 // only enumerate direct entries of the directory, not subdirectories
353 if (filename.find('/') == filename.npos)
354 functor(filename);
355 }
356 EndIteration(iter_cookie);
357 CloseArchive(zip);
358}
Jesse Hall80523e22016-01-06 16:47:54 -0800359
Jesse Hall1a7eb592016-05-01 21:04:40 +0200360template <typename Functor>
361void ForEachFileInPath(const std::string& path, Functor functor) {
362 size_t zip_pos = path.find("!/");
363 if (zip_pos == std::string::npos) {
364 ForEachFileInDir(path, functor);
365 } else {
366 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
367 functor);
368 }
369}
370
371void DiscoverLayersInPathList(const std::string& pathstr) {
372 std::vector<std::string> paths = android::base::Split(pathstr, ":");
373 for (const auto& path : paths) {
374 ForEachFileInPath(path, [&](const std::string& filename) {
375 if (android::base::StartsWith(filename, "libVkLayer") &&
376 android::base::EndsWith(filename, ".so")) {
377 AddLayerLibrary(path + "/" + filename);
378 }
379 });
380 }
Jesse Hall80523e22016-01-06 16:47:54 -0800381}
382
Chia-I Wudab25652016-04-28 07:15:51 +0800383const VkExtensionProperties* FindExtension(
384 const std::vector<VkExtensionProperties>& extensions,
385 const char* name) {
386 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
387 [=](const VkExtensionProperties& ext) {
388 return (strcmp(ext.extensionName, name) == 0);
389 });
390 return (it != extensions.cend()) ? &*it : nullptr;
391}
392
Jesse Hall80523e22016-01-06 16:47:54 -0800393void* GetLayerGetProcAddr(const Layer& layer,
394 const char* gpa_name,
395 size_t gpa_name_len) {
396 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800397 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800398}
399
Jesse Hallaa410942016-01-17 13:07:10 -0800400} // anonymous namespace
401
Jesse Hallaa410942016-01-17 13:07:10 -0800402void DiscoverLayers() {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200403 if (property_get_bool("ro.debuggable", false) &&
404 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
405 DiscoverLayersInPathList(kSystemLayerLibraryDir);
406 }
Jesse Hallaa410942016-01-17 13:07:10 -0800407 if (!LoaderData::GetInstance().layer_path.empty())
Jesse Hall1a7eb592016-05-01 21:04:40 +0200408 DiscoverLayersInPathList(LoaderData::GetInstance().layer_path);
Jesse Hallaa410942016-01-17 13:07:10 -0800409}
410
Chia-I Wu25700b42016-04-28 06:36:09 +0800411uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800412 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800413}
414
Chia-I Wu25700b42016-04-28 06:36:09 +0800415const Layer& GetLayer(uint32_t index) {
416 return g_instance_layers[index];
417}
Chia-I Wubea09db2016-04-22 09:42:41 +0800418
Chia-I Wu04c65512016-04-27 09:54:02 +0800419const Layer* FindLayer(const char* name) {
420 auto layer =
421 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
422 [=](const Layer& entry) {
423 return strcmp(entry.properties.layerName, name) == 0;
424 });
425 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
426}
427
Chia-I Wu25700b42016-04-28 06:36:09 +0800428const VkLayerProperties& GetLayerProperties(const Layer& layer) {
429 return layer.properties;
430}
Chia-I Wubea09db2016-04-22 09:42:41 +0800431
Chia-I Wu25700b42016-04-28 06:36:09 +0800432bool IsLayerGlobal(const Layer& layer) {
433 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800434}
435
Chia-I Wu04c65512016-04-27 09:54:02 +0800436const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
437 uint32_t& count) {
438 count = static_cast<uint32_t>(layer.instance_extensions.size());
439 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800440}
441
Chia-I Wu04c65512016-04-27 09:54:02 +0800442const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
443 uint32_t& count) {
444 count = static_cast<uint32_t>(layer.device_extensions.size());
445 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800446}
447
Chia-I Wudab25652016-04-28 07:15:51 +0800448const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
449 const char* name) {
450 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800451}
452
Chia-I Wudab25652016-04-28 07:15:51 +0800453const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
454 const char* name) {
455 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800456}
457
Chia-I Wudab25652016-04-28 07:15:51 +0800458LayerRef GetLayerRef(const Layer& layer) {
459 LayerLibrary& library = g_layer_libraries[layer.library_idx];
460 return LayerRef((library.Open()) ? &layer : nullptr);
461}
462
463LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800464
465LayerRef::~LayerRef() {
466 if (layer_) {
467 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800468 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800469 }
470}
471
Chia-I Wudab25652016-04-28 07:15:51 +0800472LayerRef::LayerRef(LayerRef&& other) : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600473 other.layer_ = nullptr;
474}
Jesse Hall80523e22016-01-06 16:47:54 -0800475
476PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
477 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
478 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
479 : nullptr;
480}
481
482PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
483 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
484 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
485 : nullptr;
486}
487
Chia-I Wuc96880f2016-03-26 06:56:45 +0800488} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800489} // namespace vulkan