blob: aa35657e899090a0bfbe83b47d49a6f572852b5f [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>
Jesse Hall40c07a12016-05-11 22:56:29 -070029#include <android/dlext.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020030#include <cutils/properties.h>
Jesse Hall80523e22016-01-06 16:47:54 -080031#include <log/log.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020032#include <ziparchive/zip_archive.h>
33
Jesse Hall80523e22016-01-06 16:47:54 -080034#include <vulkan/vulkan_loader_data.h>
35
Jesse Hallb1471272016-01-17 21:36:58 -080036// TODO(jessehall): The whole way we deal with extensions is pretty hokey, and
37// not a good long-term solution. Having a hard-coded enum of extensions is
38// bad, of course. Representing sets of extensions (requested, supported, etc.)
39// as a bitset isn't necessarily bad, if the mapping from extension to bit were
40// dynamic. Need to rethink this completely when there's a little more time.
41
Jesse Hallaa410942016-01-17 13:07:10 -080042// TODO(jessehall): This file currently builds up global data structures as it
43// loads, and never cleans them up. This means we're doing heap allocations
44// without going through an app-provided allocator, but worse, we'll leak those
45// allocations if the loader is unloaded.
46//
47// We should allocate "enough" BSS space, and suballocate from there. Will
48// probably want to intern strings, etc., and will need some custom/manual data
49// structures.
50
Jesse Hall80523e22016-01-06 16:47:54 -080051namespace vulkan {
Chia-I Wuc96880f2016-03-26 06:56:45 +080052namespace api {
53
Jesse Hall80523e22016-01-06 16:47:54 -080054struct Layer {
55 VkLayerProperties properties;
56 size_t library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +080057
Chia-I Wu25700b42016-04-28 06:36:09 +080058 // true if the layer intercepts vkCreateDevice and device commands
Chia-I Wubea09db2016-04-22 09:42:41 +080059 bool is_global;
60
61 std::vector<VkExtensionProperties> instance_extensions;
62 std::vector<VkExtensionProperties> device_extensions;
Jesse Hall80523e22016-01-06 16:47:54 -080063};
Jesse Hall80523e22016-01-06 16:47:54 -080064
65namespace {
66
Jesse Hall1a7eb592016-05-01 21:04:40 +020067const char kSystemLayerLibraryDir[] = "/data/local/debug/vulkan";
68
Chia-I Wu6693f5c2016-04-18 12:20:02 +080069class LayerLibrary {
70 public:
71 LayerLibrary(const std::string& path)
72 : path_(path), dlhandle_(nullptr), refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080073
Chia-I Wu50174ee2016-04-18 16:33:20 +080074 LayerLibrary(LayerLibrary&& other)
Chia-I Wu6693f5c2016-04-18 12:20:02 +080075 : path_(std::move(other.path_)),
76 dlhandle_(other.dlhandle_),
77 refcount_(other.refcount_) {
78 other.dlhandle_ = nullptr;
79 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080080 }
81
82 LayerLibrary(const LayerLibrary&) = delete;
83 LayerLibrary& operator=(const LayerLibrary&) = delete;
84
Chia-I Wua6229742016-04-26 07:37:44 +080085 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080086 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080087 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080088
Chia-I Wu50174ee2016-04-18 16:33:20 +080089 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080090 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +080091
Chia-I Wuba113272016-04-18 16:38:39 +080092 void* GetGPA(const Layer& layer,
93 const char* gpa_name,
94 size_t gpa_name_len) const;
95
Chia-I Wu6693f5c2016-04-18 12:20:02 +080096 private:
97 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +080098
99 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800100 void* dlhandle_;
101 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800102};
Chia-I Wu74349592016-04-18 12:08:39 +0800103
Chia-I Wufd0389f2016-04-18 12:11:00 +0800104bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800105 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800106 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200107 ALOGV("opening layer library '%s'", path_.c_str());
Jesse Hall40c07a12016-05-11 22:56:29 -0700108 // Libraries in the system layer library dir can't be loaded into
109 // the application namespace. That causes compatibility problems, since
110 // any symbol dependencies will be resolved by system libraries. They
111 // can't safely use libc++_shared, for example. Which is one reason
112 // (among several) we only allow them in non-user builds.
113 auto app_namespace = LoaderData::GetInstance().app_namespace;
114 if (app_namespace &&
115 !android::base::StartsWith(path_, kSystemLayerLibraryDir)) {
116 android_dlextinfo dlextinfo = {};
117 dlextinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
118 dlextinfo.library_namespace = app_namespace;
119 dlhandle_ = android_dlopen_ext(path_.c_str(), RTLD_NOW | RTLD_LOCAL,
120 &dlextinfo);
121 } else {
122 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
123 }
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800124 if (!dlhandle_) {
125 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
Chia-I Wufd0389f2016-04-18 12:11:00 +0800126 dlerror());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800127 refcount_ = 0;
Chia-I Wufd0389f2016-04-18 12:11:00 +0800128 return false;
129 }
130 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800131 return true;
132}
133
Chia-I Wud91c74f2016-04-18 12:12:36 +0800134void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800135 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800136 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200137 ALOGV("closing layer library '%s'", path_.c_str());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800138 dlclose(dlhandle_);
139 dlhandle_ = nullptr;
Chia-I Wud91c74f2016-04-18 12:12:36 +0800140 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800141}
142
Chia-I Wu50174ee2016-04-18 16:33:20 +0800143bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800144 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800145 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Jesse Hall80523e22016-01-06 16:47:54 -0800146 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800147 dlsym(dlhandle_, "vkEnumerateInstanceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800148 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Jesse Hall80523e22016-01-06 16:47:54 -0800149 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800150 dlsym(dlhandle_, "vkEnumerateInstanceExtensionProperties"));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800151 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200152 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800153 path_.c_str());
154 return false;
155 }
156
157 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800158 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
159 reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800160 dlsym(dlhandle_, "vkEnumerateDeviceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800161 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
162 reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800163 dlsym(dlhandle_, "vkEnumerateDeviceExtensionProperties"));
Jesse Hall80523e22016-01-06 16:47:54 -0800164
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800165 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800166 uint32_t num_instance_layers = 0;
167 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800168 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
169 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800170 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200171 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800172 "vkEnumerateInstanceLayerProperties failed for library '%s': "
173 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800174 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800175 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800176 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800177 }
Jesse Hallaa410942016-01-17 13:07:10 -0800178 if (enumerate_device_layers) {
179 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
180 nullptr);
181 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200182 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800183 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800184 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800185 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800186 }
187 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800188
189 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800190 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
191 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800192 result = enumerate_instance_layers(&num_instance_layers, properties);
193 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200194 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800195 path_.c_str(), result);
196 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800197 }
198 if (num_device_layers > 0) {
199 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
200 properties + num_instance_layers);
201 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200202 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800203 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800204 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800205 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800206 }
Jesse Hall80523e22016-01-06 16:47:54 -0800207 }
208
Chia-I Wubea09db2016-04-22 09:42:41 +0800209 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800210 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800211 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800212 for (size_t i = 0; i < num_instance_layers; i++) {
213 const VkLayerProperties& props = properties[i];
214
Jesse Hall80523e22016-01-06 16:47:54 -0800215 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800216 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800217 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800218 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800219
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800220 uint32_t count = 0;
221 result =
222 enumerate_instance_extensions(props.layerName, &count, nullptr);
223 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200224 ALOGE(
225 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
226 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800227 props.layerName, path_.c_str(), result);
228 instance_layers.resize(prev_num_instance_layers);
229 return false;
230 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800231 layer.instance_extensions.resize(count);
232 result = enumerate_instance_extensions(
233 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800234 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200235 ALOGE(
236 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
237 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800238 props.layerName, path_.c_str(), result);
239 instance_layers.resize(prev_num_instance_layers);
240 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800241 }
242
Chia-I Wu279ccc02016-04-18 16:45:15 +0800243 for (size_t j = 0; j < num_device_layers; j++) {
244 const auto& dev_props = properties[num_instance_layers + j];
245 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800246 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800247 break;
248 }
249 }
Jesse Hallaa410942016-01-17 13:07:10 -0800250
Chia-I Wubea09db2016-04-22 09:42:41 +0800251 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800252 result = enumerate_device_extensions(
253 VK_NULL_HANDLE, props.layerName, &count, nullptr);
254 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200255 ALOGE(
256 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800257 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800258 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800259 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800260 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800261 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800262 layer.device_extensions.resize(count);
263 result = enumerate_device_extensions(
264 VK_NULL_HANDLE, props.layerName, &count,
265 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800266 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200267 ALOGE(
268 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800269 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800270 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800271 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800272 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800273 }
274 }
275
Chia-I Wubea09db2016-04-22 09:42:41 +0800276 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200277 ALOGD("added %s layer '%s' from library '%s'",
278 (layer.is_global) ? "global" : "instance", props.layerName,
279 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800280 }
281
Chia-I Wu50174ee2016-04-18 16:33:20 +0800282 return true;
283}
Jesse Hall80523e22016-01-06 16:47:54 -0800284
Chia-I Wuba113272016-04-18 16:38:39 +0800285void* LayerLibrary::GetGPA(const Layer& layer,
286 const char* gpa_name,
287 size_t gpa_name_len) const {
288 void* gpa;
289 size_t layer_name_len =
290 std::max(size_t{2}, strlen(layer.properties.layerName));
291 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
292 strcpy(name, layer.properties.layerName);
293 strcpy(name + layer_name_len, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800294 if (!(gpa = dlsym(dlhandle_, name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800295 strcpy(name, "vk");
296 strcpy(name + 2, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800297 gpa = dlsym(dlhandle_, name);
Chia-I Wuba113272016-04-18 16:38:39 +0800298 }
299 return gpa;
300}
301
Jesse Hall1a7eb592016-05-01 21:04:40 +0200302// ----------------------------------------------------------------------------
303
Chia-I Wu50174ee2016-04-18 16:33:20 +0800304std::vector<LayerLibrary> g_layer_libraries;
305std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800306
307void AddLayerLibrary(const std::string& path) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800308 LayerLibrary library(path);
309 if (!library.Open())
310 return;
311
Chia-I Wubea09db2016-04-22 09:42:41 +0800312 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800313 library.Close();
314 return;
315 }
316
317 library.Close();
318
319 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800320}
321
Jesse Hall1a7eb592016-05-01 21:04:40 +0200322template <typename Functor>
323void ForEachFileInDir(const std::string& dirname, Functor functor) {
324 auto dir_deleter = [](DIR* handle) { closedir(handle); };
325 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
326 dir_deleter);
327 if (!dir) {
328 // It's normal for some search directories to not exist, especially
329 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800330 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200331 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
332 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800333 return;
334 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200335 ALOGD("searching for layers in '%s'", dirname.c_str());
336 dirent* entry;
337 while ((entry = readdir(dir.get())) != nullptr)
338 functor(entry->d_name);
339}
Jesse Hall80523e22016-01-06 16:47:54 -0800340
Jesse Hall1a7eb592016-05-01 21:04:40 +0200341template <typename Functor>
342void ForEachFileInZip(const std::string& zipname,
343 const std::string& dir_in_zip,
344 Functor functor) {
345 int32_t err;
346 ZipArchiveHandle zip = nullptr;
347 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
348 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
349 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800350 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200351 std::string prefix(dir_in_zip + "/");
352 const ZipString prefix_str(prefix.c_str());
353 void* iter_cookie = nullptr;
354 if ((err = StartIteration(zip, &iter_cookie, &prefix_str, nullptr)) != 0) {
355 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
356 err);
357 CloseArchive(zip);
358 return;
359 }
360 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
361 dir_in_zip.c_str());
362 ZipEntry entry;
363 ZipString name;
364 while (Next(iter_cookie, &entry, &name) == 0) {
365 std::string filename(
366 reinterpret_cast<const char*>(name.name) + prefix.length(),
367 name.name_length - prefix.length());
368 // only enumerate direct entries of the directory, not subdirectories
369 if (filename.find('/') == filename.npos)
370 functor(filename);
371 }
372 EndIteration(iter_cookie);
373 CloseArchive(zip);
374}
Jesse Hall80523e22016-01-06 16:47:54 -0800375
Jesse Hall1a7eb592016-05-01 21:04:40 +0200376template <typename Functor>
377void ForEachFileInPath(const std::string& path, Functor functor) {
378 size_t zip_pos = path.find("!/");
379 if (zip_pos == std::string::npos) {
380 ForEachFileInDir(path, functor);
381 } else {
382 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
383 functor);
384 }
385}
386
387void DiscoverLayersInPathList(const std::string& pathstr) {
388 std::vector<std::string> paths = android::base::Split(pathstr, ":");
389 for (const auto& path : paths) {
390 ForEachFileInPath(path, [&](const std::string& filename) {
391 if (android::base::StartsWith(filename, "libVkLayer") &&
392 android::base::EndsWith(filename, ".so")) {
393 AddLayerLibrary(path + "/" + filename);
394 }
395 });
396 }
Jesse Hall80523e22016-01-06 16:47:54 -0800397}
398
Chia-I Wudab25652016-04-28 07:15:51 +0800399const VkExtensionProperties* FindExtension(
400 const std::vector<VkExtensionProperties>& extensions,
401 const char* name) {
402 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
403 [=](const VkExtensionProperties& ext) {
404 return (strcmp(ext.extensionName, name) == 0);
405 });
406 return (it != extensions.cend()) ? &*it : nullptr;
407}
408
Jesse Hall80523e22016-01-06 16:47:54 -0800409void* GetLayerGetProcAddr(const Layer& layer,
410 const char* gpa_name,
411 size_t gpa_name_len) {
412 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800413 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800414}
415
Jesse Hallaa410942016-01-17 13:07:10 -0800416} // anonymous namespace
417
Jesse Hallaa410942016-01-17 13:07:10 -0800418void DiscoverLayers() {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200419 if (property_get_bool("ro.debuggable", false) &&
420 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
421 DiscoverLayersInPathList(kSystemLayerLibraryDir);
422 }
Jesse Hallaa410942016-01-17 13:07:10 -0800423 if (!LoaderData::GetInstance().layer_path.empty())
Jesse Hall1a7eb592016-05-01 21:04:40 +0200424 DiscoverLayersInPathList(LoaderData::GetInstance().layer_path);
Jesse Hallaa410942016-01-17 13:07:10 -0800425}
426
Chia-I Wu25700b42016-04-28 06:36:09 +0800427uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800428 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800429}
430
Chia-I Wu25700b42016-04-28 06:36:09 +0800431const Layer& GetLayer(uint32_t index) {
432 return g_instance_layers[index];
433}
Chia-I Wubea09db2016-04-22 09:42:41 +0800434
Chia-I Wu04c65512016-04-27 09:54:02 +0800435const Layer* FindLayer(const char* name) {
436 auto layer =
437 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
438 [=](const Layer& entry) {
439 return strcmp(entry.properties.layerName, name) == 0;
440 });
441 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
442}
443
Chia-I Wu25700b42016-04-28 06:36:09 +0800444const VkLayerProperties& GetLayerProperties(const Layer& layer) {
445 return layer.properties;
446}
Chia-I Wubea09db2016-04-22 09:42:41 +0800447
Chia-I Wu25700b42016-04-28 06:36:09 +0800448bool IsLayerGlobal(const Layer& layer) {
449 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800450}
451
Chia-I Wu04c65512016-04-27 09:54:02 +0800452const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
453 uint32_t& count) {
454 count = static_cast<uint32_t>(layer.instance_extensions.size());
455 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800456}
457
Chia-I Wu04c65512016-04-27 09:54:02 +0800458const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
459 uint32_t& count) {
460 count = static_cast<uint32_t>(layer.device_extensions.size());
461 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800462}
463
Chia-I Wudab25652016-04-28 07:15:51 +0800464const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
465 const char* name) {
466 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800467}
468
Chia-I Wudab25652016-04-28 07:15:51 +0800469const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
470 const char* name) {
471 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800472}
473
Chia-I Wudab25652016-04-28 07:15:51 +0800474LayerRef GetLayerRef(const Layer& layer) {
475 LayerLibrary& library = g_layer_libraries[layer.library_idx];
476 return LayerRef((library.Open()) ? &layer : nullptr);
477}
478
479LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800480
481LayerRef::~LayerRef() {
482 if (layer_) {
483 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800484 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800485 }
486}
487
Chia-I Wudab25652016-04-28 07:15:51 +0800488LayerRef::LayerRef(LayerRef&& other) : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600489 other.layer_ = nullptr;
490}
Jesse Hall80523e22016-01-06 16:47:54 -0800491
492PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
493 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
494 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
495 : nullptr;
496}
497
498PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
499 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
500 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
501 : nullptr;
502}
503
Chia-I Wuc96880f2016-03-26 06:56:45 +0800504} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800505} // namespace vulkan