blob: 3a59208446787a7bb57fee562acc0525870eeac3 [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
Jesse Hall40c07a12016-05-11 22:56:29 -070029#include <android/dlext.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070030#include <android-base/strings.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020031#include <cutils/properties.h>
Cody Northropd2aa3ab2017-10-20 09:01:53 -060032#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070033#include <log/log.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020034#include <ziparchive/zip_archive.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:
Cody Northropd2aa3ab2017-10-20 09:01:53 -060071 explicit LayerLibrary(const std::string& path,
72 const std::string& filename)
73 : path_(path),
74 filename_(filename),
75 dlhandle_(nullptr),
76 refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080077
Chia-I Wu50174ee2016-04-18 16:33:20 +080078 LayerLibrary(LayerLibrary&& other)
Chia-I Wu6693f5c2016-04-18 12:20:02 +080079 : path_(std::move(other.path_)),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060080 filename_(std::move(other.filename_)),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080081 dlhandle_(other.dlhandle_),
82 refcount_(other.refcount_) {
83 other.dlhandle_ = nullptr;
84 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080085 }
86
87 LayerLibrary(const LayerLibrary&) = delete;
88 LayerLibrary& operator=(const LayerLibrary&) = delete;
89
Chia-I Wua6229742016-04-26 07:37:44 +080090 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080091 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080092 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080093
Chia-I Wu50174ee2016-04-18 16:33:20 +080094 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080095 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +080096
Chia-I Wuba113272016-04-18 16:38:39 +080097 void* GetGPA(const Layer& layer,
98 const char* gpa_name,
99 size_t gpa_name_len) const;
100
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600101 const std::string GetFilename() { return filename_; }
102
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800103 private:
104 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +0800105
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600106 // Track the filename alone so we can detect duplicates
107 const std::string filename_;
108
Chia-I Wua6229742016-04-26 07:37:44 +0800109 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800110 void* dlhandle_;
111 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800112};
Chia-I Wu74349592016-04-18 12:08:39 +0800113
Chia-I Wufd0389f2016-04-18 12:11:00 +0800114bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800115 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800116 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200117 ALOGV("opening layer library '%s'", path_.c_str());
Jesse Hall40c07a12016-05-11 22:56:29 -0700118 // Libraries in the system layer library dir can't be loaded into
119 // the application namespace. That causes compatibility problems, since
120 // any symbol dependencies will be resolved by system libraries. They
121 // can't safely use libc++_shared, for example. Which is one reason
122 // (among several) we only allow them in non-user builds.
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600123 auto app_namespace = android::GraphicsEnv::getInstance().getAppNamespace();
Jesse Hall40c07a12016-05-11 22:56:29 -0700124 if (app_namespace &&
125 !android::base::StartsWith(path_, kSystemLayerLibraryDir)) {
126 android_dlextinfo dlextinfo = {};
127 dlextinfo.flags = ANDROID_DLEXT_USE_NAMESPACE;
128 dlextinfo.library_namespace = app_namespace;
129 dlhandle_ = android_dlopen_ext(path_.c_str(), RTLD_NOW | RTLD_LOCAL,
130 &dlextinfo);
131 } else {
132 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
133 }
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800134 if (!dlhandle_) {
135 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
Chia-I Wufd0389f2016-04-18 12:11:00 +0800136 dlerror());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800137 refcount_ = 0;
Chia-I Wufd0389f2016-04-18 12:11:00 +0800138 return false;
139 }
140 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800141 return true;
142}
143
Chia-I Wud91c74f2016-04-18 12:12:36 +0800144void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800145 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800146 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200147 ALOGV("closing layer library '%s'", path_.c_str());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800148 dlclose(dlhandle_);
149 dlhandle_ = nullptr;
Chia-I Wud91c74f2016-04-18 12:12:36 +0800150 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800151}
152
Chia-I Wu50174ee2016-04-18 16:33:20 +0800153bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800154 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800155 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Jesse Hall80523e22016-01-06 16:47:54 -0800156 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800157 dlsym(dlhandle_, "vkEnumerateInstanceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800158 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Jesse Hall80523e22016-01-06 16:47:54 -0800159 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800160 dlsym(dlhandle_, "vkEnumerateInstanceExtensionProperties"));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800161 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200162 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800163 path_.c_str());
164 return false;
165 }
166
167 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800168 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
169 reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800170 dlsym(dlhandle_, "vkEnumerateDeviceLayerProperties"));
Jesse Hallaa410942016-01-17 13:07:10 -0800171 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
172 reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>(
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800173 dlsym(dlhandle_, "vkEnumerateDeviceExtensionProperties"));
Jesse Hall80523e22016-01-06 16:47:54 -0800174
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800175 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800176 uint32_t num_instance_layers = 0;
177 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800178 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
179 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800180 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200181 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800182 "vkEnumerateInstanceLayerProperties failed for library '%s': "
183 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800184 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800185 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800186 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800187 }
Jesse Hallaa410942016-01-17 13:07:10 -0800188 if (enumerate_device_layers) {
189 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
190 nullptr);
191 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200192 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800193 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800194 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800195 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800196 }
197 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800198
199 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800200 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
201 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800202 result = enumerate_instance_layers(&num_instance_layers, properties);
203 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200204 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800205 path_.c_str(), result);
206 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800207 }
208 if (num_device_layers > 0) {
209 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
210 properties + num_instance_layers);
211 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200212 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800213 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800214 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800215 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800216 }
Jesse Hall80523e22016-01-06 16:47:54 -0800217 }
218
Chia-I Wubea09db2016-04-22 09:42:41 +0800219 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800220 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800221 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800222 for (size_t i = 0; i < num_instance_layers; i++) {
223 const VkLayerProperties& props = properties[i];
224
Jesse Hall80523e22016-01-06 16:47:54 -0800225 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800226 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800227 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800228 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800229
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800230 uint32_t count = 0;
231 result =
232 enumerate_instance_extensions(props.layerName, &count, nullptr);
233 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200234 ALOGE(
235 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
236 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800237 props.layerName, path_.c_str(), result);
238 instance_layers.resize(prev_num_instance_layers);
239 return false;
240 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800241 layer.instance_extensions.resize(count);
242 result = enumerate_instance_extensions(
243 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800244 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200245 ALOGE(
246 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
247 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800248 props.layerName, path_.c_str(), result);
249 instance_layers.resize(prev_num_instance_layers);
250 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800251 }
252
Chia-I Wu279ccc02016-04-18 16:45:15 +0800253 for (size_t j = 0; j < num_device_layers; j++) {
254 const auto& dev_props = properties[num_instance_layers + j];
255 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800256 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800257 break;
258 }
259 }
Jesse Hallaa410942016-01-17 13:07:10 -0800260
Chia-I Wubea09db2016-04-22 09:42:41 +0800261 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800262 result = enumerate_device_extensions(
263 VK_NULL_HANDLE, props.layerName, &count, nullptr);
264 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200265 ALOGE(
266 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800267 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800268 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800269 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800270 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800271 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800272 layer.device_extensions.resize(count);
273 result = enumerate_device_extensions(
274 VK_NULL_HANDLE, props.layerName, &count,
275 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800276 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200277 ALOGE(
278 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800279 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800280 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800281 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800282 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800283 }
284 }
285
Chia-I Wubea09db2016-04-22 09:42:41 +0800286 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200287 ALOGD("added %s layer '%s' from library '%s'",
288 (layer.is_global) ? "global" : "instance", props.layerName,
289 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800290 }
291
Chia-I Wu50174ee2016-04-18 16:33:20 +0800292 return true;
293}
Jesse Hall80523e22016-01-06 16:47:54 -0800294
Chia-I Wuba113272016-04-18 16:38:39 +0800295void* LayerLibrary::GetGPA(const Layer& layer,
296 const char* gpa_name,
297 size_t gpa_name_len) const {
298 void* gpa;
299 size_t layer_name_len =
300 std::max(size_t{2}, strlen(layer.properties.layerName));
301 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
302 strcpy(name, layer.properties.layerName);
303 strcpy(name + layer_name_len, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800304 if (!(gpa = dlsym(dlhandle_, name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800305 strcpy(name, "vk");
306 strcpy(name + 2, gpa_name);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800307 gpa = dlsym(dlhandle_, name);
Chia-I Wuba113272016-04-18 16:38:39 +0800308 }
309 return gpa;
310}
311
Jesse Hall1a7eb592016-05-01 21:04:40 +0200312// ----------------------------------------------------------------------------
313
Chia-I Wu50174ee2016-04-18 16:33:20 +0800314std::vector<LayerLibrary> g_layer_libraries;
315std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800316
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600317void AddLayerLibrary(const std::string& path, const std::string& filename) {
318 LayerLibrary library(path + "/" + filename, filename);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800319 if (!library.Open())
320 return;
321
Chia-I Wubea09db2016-04-22 09:42:41 +0800322 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800323 library.Close();
324 return;
325 }
326
327 library.Close();
328
329 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800330}
331
Jesse Hall1a7eb592016-05-01 21:04:40 +0200332template <typename Functor>
333void ForEachFileInDir(const std::string& dirname, Functor functor) {
334 auto dir_deleter = [](DIR* handle) { closedir(handle); };
335 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
336 dir_deleter);
337 if (!dir) {
338 // It's normal for some search directories to not exist, especially
339 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800340 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200341 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
342 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800343 return;
344 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200345 ALOGD("searching for layers in '%s'", dirname.c_str());
346 dirent* entry;
347 while ((entry = readdir(dir.get())) != nullptr)
348 functor(entry->d_name);
349}
Jesse Hall80523e22016-01-06 16:47:54 -0800350
Jesse Hall1a7eb592016-05-01 21:04:40 +0200351template <typename Functor>
352void ForEachFileInZip(const std::string& zipname,
353 const std::string& dir_in_zip,
354 Functor functor) {
355 int32_t err;
356 ZipArchiveHandle zip = nullptr;
357 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
358 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
359 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800360 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200361 std::string prefix(dir_in_zip + "/");
362 const ZipString prefix_str(prefix.c_str());
363 void* iter_cookie = nullptr;
364 if ((err = StartIteration(zip, &iter_cookie, &prefix_str, nullptr)) != 0) {
365 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
366 err);
367 CloseArchive(zip);
368 return;
369 }
370 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
371 dir_in_zip.c_str());
372 ZipEntry entry;
373 ZipString name;
374 while (Next(iter_cookie, &entry, &name) == 0) {
375 std::string filename(
376 reinterpret_cast<const char*>(name.name) + prefix.length(),
377 name.name_length - prefix.length());
378 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700379 if (filename.find('/') != filename.npos)
380 continue;
381 // Check whether it *may* be possible to load the library directly from
382 // the APK. Loading still may fail for other reasons, but this at least
383 // lets us avoid failed-to-load log messages in the typical case of
384 // compressed and/or unaligned libraries.
385 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
386 continue;
387 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200388 }
389 EndIteration(iter_cookie);
390 CloseArchive(zip);
391}
Jesse Hall80523e22016-01-06 16:47:54 -0800392
Jesse Hall1a7eb592016-05-01 21:04:40 +0200393template <typename Functor>
394void ForEachFileInPath(const std::string& path, Functor functor) {
395 size_t zip_pos = path.find("!/");
396 if (zip_pos == std::string::npos) {
397 ForEachFileInDir(path, functor);
398 } else {
399 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
400 functor);
401 }
402}
403
404void DiscoverLayersInPathList(const std::string& pathstr) {
405 std::vector<std::string> paths = android::base::Split(pathstr, ":");
406 for (const auto& path : paths) {
407 ForEachFileInPath(path, [&](const std::string& filename) {
408 if (android::base::StartsWith(filename, "libVkLayer") &&
409 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600410
411 // Check to ensure we haven't seen this layer already
412 // Let the first instance of the shared object be enumerated
413 // We're searching for layers in following order:
414 // 1. system path
415 // 2. libraryPermittedPath (if enabled)
416 // 3. libraryPath
417
418 bool duplicate = false;
419 for (auto& layer : g_layer_libraries) {
420 if (layer.GetFilename() == filename) {
421 ALOGV("Skipping duplicate layer %s in %s",
422 filename.c_str(), path.c_str());
423 duplicate = true;
424 }
425 }
426
427 if (!duplicate)
428 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200429 }
430 });
431 }
Jesse Hall80523e22016-01-06 16:47:54 -0800432}
433
Chia-I Wudab25652016-04-28 07:15:51 +0800434const VkExtensionProperties* FindExtension(
435 const std::vector<VkExtensionProperties>& extensions,
436 const char* name) {
437 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
438 [=](const VkExtensionProperties& ext) {
439 return (strcmp(ext.extensionName, name) == 0);
440 });
441 return (it != extensions.cend()) ? &*it : nullptr;
442}
443
Jesse Hall80523e22016-01-06 16:47:54 -0800444void* GetLayerGetProcAddr(const Layer& layer,
445 const char* gpa_name,
446 size_t gpa_name_len) {
447 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800448 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800449}
450
Jesse Hallaa410942016-01-17 13:07:10 -0800451} // anonymous namespace
452
Jesse Hallaa410942016-01-17 13:07:10 -0800453void DiscoverLayers() {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200454 if (property_get_bool("ro.debuggable", false) &&
455 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
456 DiscoverLayersInPathList(kSystemLayerLibraryDir);
457 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600458 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
459 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800460}
461
Chia-I Wu25700b42016-04-28 06:36:09 +0800462uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800463 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800464}
465
Chia-I Wu25700b42016-04-28 06:36:09 +0800466const Layer& GetLayer(uint32_t index) {
467 return g_instance_layers[index];
468}
Chia-I Wubea09db2016-04-22 09:42:41 +0800469
Chia-I Wu04c65512016-04-27 09:54:02 +0800470const Layer* FindLayer(const char* name) {
471 auto layer =
472 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
473 [=](const Layer& entry) {
474 return strcmp(entry.properties.layerName, name) == 0;
475 });
476 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
477}
478
Chia-I Wu25700b42016-04-28 06:36:09 +0800479const VkLayerProperties& GetLayerProperties(const Layer& layer) {
480 return layer.properties;
481}
Chia-I Wubea09db2016-04-22 09:42:41 +0800482
Chia-I Wu25700b42016-04-28 06:36:09 +0800483bool IsLayerGlobal(const Layer& layer) {
484 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800485}
486
Chia-I Wu04c65512016-04-27 09:54:02 +0800487const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
488 uint32_t& count) {
489 count = static_cast<uint32_t>(layer.instance_extensions.size());
490 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800491}
492
Chia-I Wu04c65512016-04-27 09:54:02 +0800493const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
494 uint32_t& count) {
495 count = static_cast<uint32_t>(layer.device_extensions.size());
496 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800497}
498
Chia-I Wudab25652016-04-28 07:15:51 +0800499const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
500 const char* name) {
501 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800502}
503
Chia-I Wudab25652016-04-28 07:15:51 +0800504const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
505 const char* name) {
506 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800507}
508
Chia-I Wudab25652016-04-28 07:15:51 +0800509LayerRef GetLayerRef(const Layer& layer) {
510 LayerLibrary& library = g_layer_libraries[layer.library_idx];
511 return LayerRef((library.Open()) ? &layer : nullptr);
512}
513
514LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800515
516LayerRef::~LayerRef() {
517 if (layer_) {
518 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800519 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800520 }
521}
522
Chia-I Wudab25652016-04-28 07:15:51 +0800523LayerRef::LayerRef(LayerRef&& other) : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600524 other.layer_ = nullptr;
525}
Jesse Hall80523e22016-01-06 16:47:54 -0800526
527PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
528 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
529 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
530 : nullptr;
531}
532
533PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
534 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
535 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
536 : nullptr;
537}
538
Chia-I Wuc96880f2016-03-26 06:56:45 +0800539} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800540} // namespace vulkan