blob: 82169ff7a27cadd7161a046c280d9a2c2f73057f [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
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700369 if (filename.find('/') != filename.npos)
370 continue;
371 // Check whether it *may* be possible to load the library directly from
372 // the APK. Loading still may fail for other reasons, but this at least
373 // lets us avoid failed-to-load log messages in the typical case of
374 // compressed and/or unaligned libraries.
375 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
376 continue;
377 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200378 }
379 EndIteration(iter_cookie);
380 CloseArchive(zip);
381}
Jesse Hall80523e22016-01-06 16:47:54 -0800382
Jesse Hall1a7eb592016-05-01 21:04:40 +0200383template <typename Functor>
384void ForEachFileInPath(const std::string& path, Functor functor) {
385 size_t zip_pos = path.find("!/");
386 if (zip_pos == std::string::npos) {
387 ForEachFileInDir(path, functor);
388 } else {
389 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
390 functor);
391 }
392}
393
394void DiscoverLayersInPathList(const std::string& pathstr) {
395 std::vector<std::string> paths = android::base::Split(pathstr, ":");
396 for (const auto& path : paths) {
397 ForEachFileInPath(path, [&](const std::string& filename) {
398 if (android::base::StartsWith(filename, "libVkLayer") &&
399 android::base::EndsWith(filename, ".so")) {
400 AddLayerLibrary(path + "/" + filename);
401 }
402 });
403 }
Jesse Hall80523e22016-01-06 16:47:54 -0800404}
405
Chia-I Wudab25652016-04-28 07:15:51 +0800406const VkExtensionProperties* FindExtension(
407 const std::vector<VkExtensionProperties>& extensions,
408 const char* name) {
409 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
410 [=](const VkExtensionProperties& ext) {
411 return (strcmp(ext.extensionName, name) == 0);
412 });
413 return (it != extensions.cend()) ? &*it : nullptr;
414}
415
Jesse Hall80523e22016-01-06 16:47:54 -0800416void* GetLayerGetProcAddr(const Layer& layer,
417 const char* gpa_name,
418 size_t gpa_name_len) {
419 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800420 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800421}
422
Jesse Hallaa410942016-01-17 13:07:10 -0800423} // anonymous namespace
424
Jesse Hallaa410942016-01-17 13:07:10 -0800425void DiscoverLayers() {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200426 if (property_get_bool("ro.debuggable", false) &&
427 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
428 DiscoverLayersInPathList(kSystemLayerLibraryDir);
429 }
Jesse Hallaa410942016-01-17 13:07:10 -0800430 if (!LoaderData::GetInstance().layer_path.empty())
Jesse Hall1a7eb592016-05-01 21:04:40 +0200431 DiscoverLayersInPathList(LoaderData::GetInstance().layer_path);
Jesse Hallaa410942016-01-17 13:07:10 -0800432}
433
Chia-I Wu25700b42016-04-28 06:36:09 +0800434uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800435 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800436}
437
Chia-I Wu25700b42016-04-28 06:36:09 +0800438const Layer& GetLayer(uint32_t index) {
439 return g_instance_layers[index];
440}
Chia-I Wubea09db2016-04-22 09:42:41 +0800441
Chia-I Wu04c65512016-04-27 09:54:02 +0800442const Layer* FindLayer(const char* name) {
443 auto layer =
444 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
445 [=](const Layer& entry) {
446 return strcmp(entry.properties.layerName, name) == 0;
447 });
448 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
449}
450
Chia-I Wu25700b42016-04-28 06:36:09 +0800451const VkLayerProperties& GetLayerProperties(const Layer& layer) {
452 return layer.properties;
453}
Chia-I Wubea09db2016-04-22 09:42:41 +0800454
Chia-I Wu25700b42016-04-28 06:36:09 +0800455bool IsLayerGlobal(const Layer& layer) {
456 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800457}
458
Chia-I Wu04c65512016-04-27 09:54:02 +0800459const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
460 uint32_t& count) {
461 count = static_cast<uint32_t>(layer.instance_extensions.size());
462 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800463}
464
Chia-I Wu04c65512016-04-27 09:54:02 +0800465const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
466 uint32_t& count) {
467 count = static_cast<uint32_t>(layer.device_extensions.size());
468 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800469}
470
Chia-I Wudab25652016-04-28 07:15:51 +0800471const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
472 const char* name) {
473 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800474}
475
Chia-I Wudab25652016-04-28 07:15:51 +0800476const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
477 const char* name) {
478 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800479}
480
Chia-I Wudab25652016-04-28 07:15:51 +0800481LayerRef GetLayerRef(const Layer& layer) {
482 LayerLibrary& library = g_layer_libraries[layer.library_idx];
483 return LayerRef((library.Open()) ? &layer : nullptr);
484}
485
486LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800487
488LayerRef::~LayerRef() {
489 if (layer_) {
490 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800491 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800492 }
493}
494
Chia-I Wudab25652016-04-28 07:15:51 +0800495LayerRef::LayerRef(LayerRef&& other) : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600496 other.layer_ = nullptr;
497}
Jesse Hall80523e22016-01-06 16:47:54 -0800498
499PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
500 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
501 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
502 : nullptr;
503}
504
505PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
506 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
507 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
508 : nullptr;
509}
510
Chia-I Wuc96880f2016-03-26 06:56:45 +0800511} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800512} // namespace vulkan