blob: 91d2d68bf04ce08315b736c2f8c5c31c9876262b [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>
Jesse Hall80523e22016-01-06 16:47:54 -080022#include <string.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070023#include <sys/prctl.h>
24
25#include <mutex>
26#include <string>
Jesse Hall80523e22016-01-06 16:47:54 -080027#include <vector>
Jesse Hall1a7eb592016-05-01 21:04:40 +020028
Mark Salyzyna5e161b2016-09-29 08:08:05 -070029#include <android/log.h>
Jesse Hall40c07a12016-05-11 22:56:29 -070030#include <android/dlext.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070031#include <android-base/strings.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020032#include <cutils/properties.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020033#include <ziparchive/zip_archive.h>
34
Jesse Hall80523e22016-01-06 16:47:54 -080035#include <vulkan/vulkan_loader_data.h>
36
Jesse Hallb1471272016-01-17 21:36:58 -080037// TODO(jessehall): The whole way we deal with extensions is pretty hokey, and
38// not a good long-term solution. Having a hard-coded enum of extensions is
39// bad, of course. Representing sets of extensions (requested, supported, etc.)
40// as a bitset isn't necessarily bad, if the mapping from extension to bit were
41// dynamic. Need to rethink this completely when there's a little more time.
42
Jesse Hallaa410942016-01-17 13:07:10 -080043// TODO(jessehall): This file currently builds up global data structures as it
44// loads, and never cleans them up. This means we're doing heap allocations
45// without going through an app-provided allocator, but worse, we'll leak those
46// allocations if the loader is unloaded.
47//
48// We should allocate "enough" BSS space, and suballocate from there. Will
49// probably want to intern strings, etc., and will need some custom/manual data
50// structures.
51
Jesse Hall80523e22016-01-06 16:47:54 -080052namespace vulkan {
Chia-I Wuc96880f2016-03-26 06:56:45 +080053namespace api {
54
Jesse Hall80523e22016-01-06 16:47:54 -080055struct Layer {
56 VkLayerProperties properties;
57 size_t library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +080058
Chia-I Wu25700b42016-04-28 06:36:09 +080059 // true if the layer intercepts vkCreateDevice and device commands
Chia-I Wubea09db2016-04-22 09:42:41 +080060 bool is_global;
61
62 std::vector<VkExtensionProperties> instance_extensions;
63 std::vector<VkExtensionProperties> device_extensions;
Jesse Hall80523e22016-01-06 16:47:54 -080064};
Jesse Hall80523e22016-01-06 16:47:54 -080065
66namespace {
67
Jesse Hall1a7eb592016-05-01 21:04:40 +020068const char kSystemLayerLibraryDir[] = "/data/local/debug/vulkan";
69
Chia-I Wu6693f5c2016-04-18 12:20:02 +080070class LayerLibrary {
71 public:
72 LayerLibrary(const std::string& path)
73 : path_(path), dlhandle_(nullptr), refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080074
Chia-I Wu50174ee2016-04-18 16:33:20 +080075 LayerLibrary(LayerLibrary&& other)
Chia-I Wu6693f5c2016-04-18 12:20:02 +080076 : path_(std::move(other.path_)),
77 dlhandle_(other.dlhandle_),
78 refcount_(other.refcount_) {
79 other.dlhandle_ = nullptr;
80 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080081 }
82
83 LayerLibrary(const LayerLibrary&) = delete;
84 LayerLibrary& operator=(const LayerLibrary&) = delete;
85
Chia-I Wua6229742016-04-26 07:37:44 +080086 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080087 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080088 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080089
Chia-I Wu50174ee2016-04-18 16:33:20 +080090 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080091 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +080092
Chia-I Wuba113272016-04-18 16:38:39 +080093 void* GetGPA(const Layer& layer,
94 const char* gpa_name,
95 size_t gpa_name_len) const;
96
Chia-I Wu6693f5c2016-04-18 12:20:02 +080097 private:
98 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +080099
100 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800101 void* dlhandle_;
102 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800103};
Chia-I Wu74349592016-04-18 12:08:39 +0800104
Chia-I Wufd0389f2016-04-18 12:11:00 +0800105bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800106 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800107 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200108 ALOGV("opening layer library '%s'", path_.c_str());
Jesse Hall40c07a12016-05-11 22:56:29 -0700109 // Libraries in the system layer library dir can't be loaded into
110 // the application namespace. That causes compatibility problems, since
111 // any symbol dependencies will be resolved by system libraries. They
112 // can't safely use libc++_shared, for example. Which is one reason
113 // (among several) we only allow them in non-user builds.
114 auto app_namespace = LoaderData::GetInstance().app_namespace;
115 if (app_namespace &&
116 !android::base::StartsWith(path_, kSystemLayerLibraryDir)) {
117 android_dlextinfo dlextinfo = {};
118 dlextinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
119 dlextinfo.library_namespace = app_namespace;
120 dlhandle_ = android_dlopen_ext(path_.c_str(), RTLD_NOW | RTLD_LOCAL,
121 &dlextinfo);
122 } else {
123 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
124 }
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800125 if (!dlhandle_) {
126 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
Chia-I Wufd0389f2016-04-18 12:11:00 +0800127 dlerror());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800128 refcount_ = 0;
Chia-I Wufd0389f2016-04-18 12:11:00 +0800129 return false;
130 }
131 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800132 return true;
133}
134
Chia-I Wud91c74f2016-04-18 12:12:36 +0800135void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800136 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800137 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200138 ALOGV("closing layer library '%s'", path_.c_str());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800139 dlclose(dlhandle_);
140 dlhandle_ = nullptr;
Chia-I Wud91c74f2016-04-18 12:12:36 +0800141 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800142}
143
Chia-I Wu50174ee2016-04-18 16:33:20 +0800144bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800145 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800146 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Jesse Hall80523e22016-01-06 16:47:54 -0800147 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800148 dlsym(dlhandle_, "vkEnumerateInstanceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800149 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Jesse Hall80523e22016-01-06 16:47:54 -0800150 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800151 dlsym(dlhandle_, "vkEnumerateInstanceExtensionProperties"));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800152 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200153 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800154 path_.c_str());
155 return false;
156 }
157
158 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800159 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
160 reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800161 dlsym(dlhandle_, "vkEnumerateDeviceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800162 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
163 reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800164 dlsym(dlhandle_, "vkEnumerateDeviceExtensionProperties"));
Jesse Hall80523e22016-01-06 16:47:54 -0800165
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800166 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800167 uint32_t num_instance_layers = 0;
168 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800169 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
170 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800171 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200172 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800173 "vkEnumerateInstanceLayerProperties failed for library '%s': "
174 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800175 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800176 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800177 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800178 }
Jesse Hallaa410942016-01-17 13:07:10 -0800179 if (enumerate_device_layers) {
180 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
181 nullptr);
182 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200183 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800184 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800185 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800186 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800187 }
188 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800189
190 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800191 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
192 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800193 result = enumerate_instance_layers(&num_instance_layers, properties);
194 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200195 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800196 path_.c_str(), result);
197 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800198 }
199 if (num_device_layers > 0) {
200 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
201 properties + num_instance_layers);
202 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200203 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800204 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800205 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800206 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800207 }
Jesse Hall80523e22016-01-06 16:47:54 -0800208 }
209
Chia-I Wubea09db2016-04-22 09:42:41 +0800210 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800211 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800212 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800213 for (size_t i = 0; i < num_instance_layers; i++) {
214 const VkLayerProperties& props = properties[i];
215
Jesse Hall80523e22016-01-06 16:47:54 -0800216 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800217 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800218 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800219 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800220
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800221 uint32_t count = 0;
222 result =
223 enumerate_instance_extensions(props.layerName, &count, nullptr);
224 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200225 ALOGE(
226 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
227 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800228 props.layerName, path_.c_str(), result);
229 instance_layers.resize(prev_num_instance_layers);
230 return false;
231 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800232 layer.instance_extensions.resize(count);
233 result = enumerate_instance_extensions(
234 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800235 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200236 ALOGE(
237 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
238 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800239 props.layerName, path_.c_str(), result);
240 instance_layers.resize(prev_num_instance_layers);
241 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800242 }
243
Chia-I Wu279ccc02016-04-18 16:45:15 +0800244 for (size_t j = 0; j < num_device_layers; j++) {
245 const auto& dev_props = properties[num_instance_layers + j];
246 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800247 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800248 break;
249 }
250 }
Jesse Hallaa410942016-01-17 13:07:10 -0800251
Chia-I Wubea09db2016-04-22 09:42:41 +0800252 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800253 result = enumerate_device_extensions(
254 VK_NULL_HANDLE, props.layerName, &count, nullptr);
255 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200256 ALOGE(
257 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800258 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800259 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800260 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800261 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800262 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800263 layer.device_extensions.resize(count);
264 result = enumerate_device_extensions(
265 VK_NULL_HANDLE, props.layerName, &count,
266 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800267 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200268 ALOGE(
269 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800270 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800271 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800272 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800273 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800274 }
275 }
276
Chia-I Wubea09db2016-04-22 09:42:41 +0800277 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200278 ALOGD("added %s layer '%s' from library '%s'",
279 (layer.is_global) ? "global" : "instance", props.layerName,
280 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800281 }
282
Chia-I Wu50174ee2016-04-18 16:33:20 +0800283 return true;
284}
Jesse Hall80523e22016-01-06 16:47:54 -0800285
Chia-I Wuba113272016-04-18 16:38:39 +0800286void* LayerLibrary::GetGPA(const Layer& layer,
287 const char* gpa_name,
288 size_t gpa_name_len) const {
289 void* gpa;
290 size_t layer_name_len =
291 std::max(size_t{2}, strlen(layer.properties.layerName));
292 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
293 strcpy(name, layer.properties.layerName);
294 strcpy(name + layer_name_len, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800295 if (!(gpa = dlsym(dlhandle_, name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800296 strcpy(name, "vk");
297 strcpy(name + 2, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800298 gpa = dlsym(dlhandle_, name);
Chia-I Wuba113272016-04-18 16:38:39 +0800299 }
300 return gpa;
301}
302
Jesse Hall1a7eb592016-05-01 21:04:40 +0200303// ----------------------------------------------------------------------------
304
Chia-I Wu50174ee2016-04-18 16:33:20 +0800305std::vector<LayerLibrary> g_layer_libraries;
306std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800307
308void AddLayerLibrary(const std::string& path) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800309 LayerLibrary library(path);
310 if (!library.Open())
311 return;
312
Chia-I Wubea09db2016-04-22 09:42:41 +0800313 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800314 library.Close();
315 return;
316 }
317
318 library.Close();
319
320 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800321}
322
Jesse Hall1a7eb592016-05-01 21:04:40 +0200323template <typename Functor>
324void ForEachFileInDir(const std::string& dirname, Functor functor) {
325 auto dir_deleter = [](DIR* handle) { closedir(handle); };
326 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
327 dir_deleter);
328 if (!dir) {
329 // It's normal for some search directories to not exist, especially
330 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800331 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200332 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
333 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800334 return;
335 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200336 ALOGD("searching for layers in '%s'", dirname.c_str());
337 dirent* entry;
338 while ((entry = readdir(dir.get())) != nullptr)
339 functor(entry->d_name);
340}
Jesse Hall80523e22016-01-06 16:47:54 -0800341
Jesse Hall1a7eb592016-05-01 21:04:40 +0200342template <typename Functor>
343void ForEachFileInZip(const std::string& zipname,
344 const std::string& dir_in_zip,
345 Functor functor) {
346 int32_t err;
347 ZipArchiveHandle zip = nullptr;
348 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
349 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
350 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800351 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200352 std::string prefix(dir_in_zip + "/");
353 const ZipString prefix_str(prefix.c_str());
354 void* iter_cookie = nullptr;
355 if ((err = StartIteration(zip, &iter_cookie, &prefix_str, nullptr)) != 0) {
356 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
357 err);
358 CloseArchive(zip);
359 return;
360 }
361 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
362 dir_in_zip.c_str());
363 ZipEntry entry;
364 ZipString name;
365 while (Next(iter_cookie, &entry, &name) == 0) {
366 std::string filename(
367 reinterpret_cast<const char*>(name.name) + prefix.length(),
368 name.name_length - prefix.length());
369 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700370 if (filename.find('/') != filename.npos)
371 continue;
372 // Check whether it *may* be possible to load the library directly from
373 // the APK. Loading still may fail for other reasons, but this at least
374 // lets us avoid failed-to-load log messages in the typical case of
375 // compressed and/or unaligned libraries.
376 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
377 continue;
378 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200379 }
380 EndIteration(iter_cookie);
381 CloseArchive(zip);
382}
Jesse Hall80523e22016-01-06 16:47:54 -0800383
Jesse Hall1a7eb592016-05-01 21:04:40 +0200384template <typename Functor>
385void ForEachFileInPath(const std::string& path, Functor functor) {
386 size_t zip_pos = path.find("!/");
387 if (zip_pos == std::string::npos) {
388 ForEachFileInDir(path, functor);
389 } else {
390 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
391 functor);
392 }
393}
394
395void DiscoverLayersInPathList(const std::string& pathstr) {
396 std::vector<std::string> paths = android::base::Split(pathstr, ":");
397 for (const auto& path : paths) {
398 ForEachFileInPath(path, [&](const std::string& filename) {
399 if (android::base::StartsWith(filename, "libVkLayer") &&
400 android::base::EndsWith(filename, ".so")) {
401 AddLayerLibrary(path + "/" + filename);
402 }
403 });
404 }
Jesse Hall80523e22016-01-06 16:47:54 -0800405}
406
Chia-I Wudab25652016-04-28 07:15:51 +0800407const VkExtensionProperties* FindExtension(
408 const std::vector<VkExtensionProperties>& extensions,
409 const char* name) {
410 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
411 [=](const VkExtensionProperties& ext) {
412 return (strcmp(ext.extensionName, name) == 0);
413 });
414 return (it != extensions.cend()) ? &*it : nullptr;
415}
416
Jesse Hall80523e22016-01-06 16:47:54 -0800417void* GetLayerGetProcAddr(const Layer& layer,
418 const char* gpa_name,
419 size_t gpa_name_len) {
420 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800421 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800422}
423
Jesse Hallaa410942016-01-17 13:07:10 -0800424} // anonymous namespace
425
Jesse Hallaa410942016-01-17 13:07:10 -0800426void DiscoverLayers() {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200427 if (property_get_bool("ro.debuggable", false) &&
428 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
429 DiscoverLayersInPathList(kSystemLayerLibraryDir);
430 }
Jesse Hallaa410942016-01-17 13:07:10 -0800431 if (!LoaderData::GetInstance().layer_path.empty())
Jesse Hall1a7eb592016-05-01 21:04:40 +0200432 DiscoverLayersInPathList(LoaderData::GetInstance().layer_path);
Jesse Hallaa410942016-01-17 13:07:10 -0800433}
434
Chia-I Wu25700b42016-04-28 06:36:09 +0800435uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800436 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800437}
438
Chia-I Wu25700b42016-04-28 06:36:09 +0800439const Layer& GetLayer(uint32_t index) {
440 return g_instance_layers[index];
441}
Chia-I Wubea09db2016-04-22 09:42:41 +0800442
Chia-I Wu04c65512016-04-27 09:54:02 +0800443const Layer* FindLayer(const char* name) {
444 auto layer =
445 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
446 [=](const Layer& entry) {
447 return strcmp(entry.properties.layerName, name) == 0;
448 });
449 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
450}
451
Chia-I Wu25700b42016-04-28 06:36:09 +0800452const VkLayerProperties& GetLayerProperties(const Layer& layer) {
453 return layer.properties;
454}
Chia-I Wubea09db2016-04-22 09:42:41 +0800455
Chia-I Wu25700b42016-04-28 06:36:09 +0800456bool IsLayerGlobal(const Layer& layer) {
457 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800458}
459
Chia-I Wu04c65512016-04-27 09:54:02 +0800460const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
461 uint32_t& count) {
462 count = static_cast<uint32_t>(layer.instance_extensions.size());
463 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800464}
465
Chia-I Wu04c65512016-04-27 09:54:02 +0800466const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
467 uint32_t& count) {
468 count = static_cast<uint32_t>(layer.device_extensions.size());
469 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800470}
471
Chia-I Wudab25652016-04-28 07:15:51 +0800472const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
473 const char* name) {
474 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800475}
476
Chia-I Wudab25652016-04-28 07:15:51 +0800477const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
478 const char* name) {
479 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800480}
481
Chia-I Wudab25652016-04-28 07:15:51 +0800482LayerRef GetLayerRef(const Layer& layer) {
483 LayerLibrary& library = g_layer_libraries[layer.library_idx];
484 return LayerRef((library.Open()) ? &layer : nullptr);
485}
486
487LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800488
489LayerRef::~LayerRef() {
490 if (layer_) {
491 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800492 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800493 }
494}
495
Chia-I Wudab25652016-04-28 07:15:51 +0800496LayerRef::LayerRef(LayerRef&& other) : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600497 other.layer_ = nullptr;
498}
Jesse Hall80523e22016-01-06 16:47:54 -0800499
500PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
501 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
502 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
503 : nullptr;
504}
505
506PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
507 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
508 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
509 : nullptr;
510}
511
Chia-I Wuc96880f2016-03-26 06:56:45 +0800512} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800513} // namespace vulkan