blob: a14fed222a2bdd952a6d2c583c21f74fda074936 [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
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Chia-I Wuc96880f2016-03-26 06:56:45 +080019#include "layers_extensions.h"
Jesse Hall1a7eb592016-05-01 21:04:40 +020020
Jesse Hall80523e22016-01-06 16:47:54 -080021#include <alloca.h>
22#include <dirent.h>
23#include <dlfcn.h>
Jesse Hall80523e22016-01-06 16:47:54 -080024#include <string.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070025#include <sys/prctl.h>
26
27#include <mutex>
28#include <string>
Jesse Hall80523e22016-01-06 16:47:54 -080029#include <vector>
Jesse Hall1a7eb592016-05-01 21:04:40 +020030
Jesse Hall40c07a12016-05-11 22:56:29 -070031#include <android/dlext.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070032#include <android-base/strings.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020033#include <cutils/properties.h>
Cody Northropd2aa3ab2017-10-20 09:01:53 -060034#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070035#include <log/log.h>
Victor Khimenkobbf77002018-08-16 22:38:36 +020036#include <nativebridge/native_bridge.h>
37#include <nativeloader/native_loader.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080038#include <utils/Trace.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020039#include <ziparchive/zip_archive.h>
40
Yiwei Zhanga885c062019-10-24 12:07:57 -070041// TODO(b/143296676): This file currently builds up global data structures as it
Jesse Hallaa410942016-01-17 13:07:10 -080042// loads, and never cleans them up. This means we're doing heap allocations
43// without going through an app-provided allocator, but worse, we'll leak those
44// allocations if the loader is unloaded.
45//
46// We should allocate "enough" BSS space, and suballocate from there. Will
47// probably want to intern strings, etc., and will need some custom/manual data
48// structures.
49
Jesse Hall80523e22016-01-06 16:47:54 -080050namespace vulkan {
Chia-I Wuc96880f2016-03-26 06:56:45 +080051namespace api {
52
Jesse Hall80523e22016-01-06 16:47:54 -080053struct Layer {
54 VkLayerProperties properties;
55 size_t library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +080056
Chia-I Wu25700b42016-04-28 06:36:09 +080057 // true if the layer intercepts vkCreateDevice and device commands
Chia-I Wubea09db2016-04-22 09:42:41 +080058 bool is_global;
59
60 std::vector<VkExtensionProperties> instance_extensions;
61 std::vector<VkExtensionProperties> device_extensions;
Jesse Hall80523e22016-01-06 16:47:54 -080062};
Jesse Hall80523e22016-01-06 16:47:54 -080063
64namespace {
65
Jesse Hall1a7eb592016-05-01 21:04:40 +020066const char kSystemLayerLibraryDir[] = "/data/local/debug/vulkan";
67
Chia-I Wu6693f5c2016-04-18 12:20:02 +080068class LayerLibrary {
69 public:
Cody Northropd2aa3ab2017-10-20 09:01:53 -060070 explicit LayerLibrary(const std::string& path,
71 const std::string& filename)
72 : path_(path),
73 filename_(filename),
74 dlhandle_(nullptr),
Victor Khimenkobbf77002018-08-16 22:38:36 +020075 native_bridge_(false),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060076 refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080077
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -070078 LayerLibrary(LayerLibrary&& other) noexcept
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_),
Victor Khimenkobbf77002018-08-16 22:38:36 +020082 native_bridge_(other.native_bridge_),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080083 refcount_(other.refcount_) {
84 other.dlhandle_ = nullptr;
85 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080086 }
87
88 LayerLibrary(const LayerLibrary&) = delete;
89 LayerLibrary& operator=(const LayerLibrary&) = delete;
90
Chia-I Wua6229742016-04-26 07:37:44 +080091 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080092 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080093 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080094
Chia-I Wu50174ee2016-04-18 16:33:20 +080095 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080096 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +080097
Nick Desaulniers60307ce2019-10-25 13:34:21 -070098 void* GetGPA(const Layer& layer, const std::string_view gpa_name) const;
Chia-I Wuba113272016-04-18 16:38:39 +080099
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600100 const std::string GetFilename() { return filename_; }
101
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800102 private:
Victor Khimenkobbf77002018-08-16 22:38:36 +0200103 // TODO(b/79940628): remove that adapter when we could use NativeBridgeGetTrampoline
104 // for native libraries.
105 template<typename Func = void*>
106 Func GetTrampoline(const char* name) const {
107 if (native_bridge_) {
108 return reinterpret_cast<Func>(android::NativeBridgeGetTrampoline(
109 dlhandle_, name, nullptr, 0));
110 }
111 return reinterpret_cast<Func>(dlsym(dlhandle_, name));
112 }
113
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800114 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +0800115
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600116 // Track the filename alone so we can detect duplicates
117 const std::string filename_;
118
Chia-I Wua6229742016-04-26 07:37:44 +0800119 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800120 void* dlhandle_;
Victor Khimenkobbf77002018-08-16 22:38:36 +0200121 bool native_bridge_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800122 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800123};
Chia-I Wu74349592016-04-18 12:08:39 +0800124
Chia-I Wufd0389f2016-04-18 12:11:00 +0800125bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800126 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800127 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200128 ALOGV("opening layer library '%s'", path_.c_str());
Jesse Hall40c07a12016-05-11 22:56:29 -0700129 // Libraries in the system layer library dir can't be loaded into
130 // the application namespace. That causes compatibility problems, since
131 // any symbol dependencies will be resolved by system libraries. They
132 // can't safely use libc++_shared, for example. Which is one reason
133 // (among several) we only allow them in non-user builds.
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600134 auto app_namespace = android::GraphicsEnv::getInstance().getAppNamespace();
Jesse Hall40c07a12016-05-11 22:56:29 -0700135 if (app_namespace &&
136 !android::base::StartsWith(path_, kSystemLayerLibraryDir)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000137 char* error_msg = nullptr;
138 dlhandle_ = OpenNativeLibraryInNamespace(
Victor Khimenkobbf77002018-08-16 22:38:36 +0200139 app_namespace, path_.c_str(), &native_bridge_, &error_msg);
140 if (!dlhandle_) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000141 ALOGE("failed to load layer library '%s': %s", path_.c_str(), error_msg);
142 android::NativeLoaderFreeErrorMessage(error_msg);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200143 refcount_ = 0;
144 return false;
145 }
Jesse Hall40c07a12016-05-11 22:56:29 -0700146 } else {
Victor Khimenkobbf77002018-08-16 22:38:36 +0200147 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
148 if (!dlhandle_) {
149 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
150 dlerror());
151 refcount_ = 0;
152 return false;
153 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800154 }
155 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800156 return true;
157}
158
Chia-I Wud91c74f2016-04-18 12:12:36 +0800159void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800160 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800161 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200162 ALOGV("closing layer library '%s'", path_.c_str());
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000163 char* error_msg = nullptr;
Jacky.Deng01728c12018-08-28 15:25:31 +0800164 if (!android::CloseNativeLibrary(dlhandle_, native_bridge_, &error_msg)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000165 ALOGE("failed to unload library '%s': %s", path_.c_str(), error_msg);
166 android::NativeLoaderFreeErrorMessage(error_msg);
Jacky.Deng01728c12018-08-28 15:25:31 +0800167 refcount_++;
168 } else {
169 dlhandle_ = nullptr;
170 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800171 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800172}
173
Chia-I Wu50174ee2016-04-18 16:33:20 +0800174bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800175 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800176 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200177 GetTrampoline<PFN_vkEnumerateInstanceLayerProperties>(
178 "vkEnumerateInstanceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800179 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200180 GetTrampoline<PFN_vkEnumerateInstanceExtensionProperties>(
181 "vkEnumerateInstanceExtensionProperties");
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800182 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200183 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800184 path_.c_str());
185 return false;
186 }
187
188 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800189 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200190 GetTrampoline<PFN_vkEnumerateDeviceLayerProperties>(
191 "vkEnumerateDeviceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800192 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200193 GetTrampoline<PFN_vkEnumerateDeviceExtensionProperties>(
194 "vkEnumerateDeviceExtensionProperties");
Jesse Hall80523e22016-01-06 16:47:54 -0800195
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800196 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800197 uint32_t num_instance_layers = 0;
198 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800199 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
200 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800201 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200202 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800203 "vkEnumerateInstanceLayerProperties failed for library '%s': "
204 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800205 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800206 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800207 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800208 }
Jesse Hallaa410942016-01-17 13:07:10 -0800209 if (enumerate_device_layers) {
210 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
211 nullptr);
212 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200213 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800214 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800215 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800216 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800217 }
218 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800219
220 // get layer properties
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700221 std::vector<VkLayerProperties> properties(num_instance_layers + num_device_layers);
222 result = enumerate_instance_layers(&num_instance_layers, properties.data());
Nick Desaulniers03b11cf2019-10-25 11:23:08 -0700223 if (result != VK_SUCCESS) {
224 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
225 path_.c_str(), result);
226 return false;
227 }
Jesse Hallaa410942016-01-17 13:07:10 -0800228 if (num_device_layers > 0) {
229 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700230 &properties[num_instance_layers]);
Jesse Hallaa410942016-01-17 13:07:10 -0800231 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200232 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800233 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800234 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800235 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800236 }
Jesse Hall80523e22016-01-06 16:47:54 -0800237 }
238
Chia-I Wubea09db2016-04-22 09:42:41 +0800239 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800240 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800241 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800242 for (size_t i = 0; i < num_instance_layers; i++) {
243 const VkLayerProperties& props = properties[i];
244
Jesse Hall80523e22016-01-06 16:47:54 -0800245 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800246 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800247 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800248 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800249
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800250 uint32_t count = 0;
251 result =
252 enumerate_instance_extensions(props.layerName, &count, nullptr);
253 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200254 ALOGE(
255 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
256 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800257 props.layerName, path_.c_str(), result);
258 instance_layers.resize(prev_num_instance_layers);
259 return false;
260 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800261 layer.instance_extensions.resize(count);
262 result = enumerate_instance_extensions(
263 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800264 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200265 ALOGE(
266 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
267 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800268 props.layerName, path_.c_str(), result);
269 instance_layers.resize(prev_num_instance_layers);
270 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800271 }
272
Chia-I Wu279ccc02016-04-18 16:45:15 +0800273 for (size_t j = 0; j < num_device_layers; j++) {
274 const auto& dev_props = properties[num_instance_layers + j];
275 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800276 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800277 break;
278 }
279 }
Jesse Hallaa410942016-01-17 13:07:10 -0800280
Chia-I Wubea09db2016-04-22 09:42:41 +0800281 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800282 result = enumerate_device_extensions(
283 VK_NULL_HANDLE, props.layerName, &count, nullptr);
284 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200285 ALOGE(
286 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800287 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800288 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800289 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800290 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800291 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800292 layer.device_extensions.resize(count);
293 result = enumerate_device_extensions(
294 VK_NULL_HANDLE, props.layerName, &count,
295 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800296 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200297 ALOGE(
298 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800299 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800300 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800301 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800302 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800303 }
304 }
305
Chia-I Wubea09db2016-04-22 09:42:41 +0800306 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200307 ALOGD("added %s layer '%s' from library '%s'",
308 (layer.is_global) ? "global" : "instance", props.layerName,
309 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800310 }
311
Chia-I Wu50174ee2016-04-18 16:33:20 +0800312 return true;
313}
Jesse Hall80523e22016-01-06 16:47:54 -0800314
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700315void* LayerLibrary::GetGPA(const Layer& layer, const std::string_view gpa_name) const {
316 std::string layer_name { layer.properties.layerName };
317 if (void* gpa = GetTrampoline((layer_name.append(gpa_name).c_str())))
318 return gpa;
319 return GetTrampoline((std::string {"vk"}.append(gpa_name)).c_str());
Chia-I Wuba113272016-04-18 16:38:39 +0800320}
321
Jesse Hall1a7eb592016-05-01 21:04:40 +0200322// ----------------------------------------------------------------------------
323
Chia-I Wu50174ee2016-04-18 16:33:20 +0800324std::vector<LayerLibrary> g_layer_libraries;
325std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800326
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600327void AddLayerLibrary(const std::string& path, const std::string& filename) {
328 LayerLibrary library(path + "/" + filename, filename);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800329 if (!library.Open())
330 return;
331
Chia-I Wubea09db2016-04-22 09:42:41 +0800332 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800333 library.Close();
334 return;
335 }
336
337 library.Close();
338
339 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800340}
341
Jesse Hall1a7eb592016-05-01 21:04:40 +0200342template <typename Functor>
343void ForEachFileInDir(const std::string& dirname, Functor functor) {
344 auto dir_deleter = [](DIR* handle) { closedir(handle); };
345 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
346 dir_deleter);
347 if (!dir) {
348 // It's normal for some search directories to not exist, especially
349 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800350 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200351 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
352 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800353 return;
354 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200355 ALOGD("searching for layers in '%s'", dirname.c_str());
356 dirent* entry;
357 while ((entry = readdir(dir.get())) != nullptr)
358 functor(entry->d_name);
359}
Jesse Hall80523e22016-01-06 16:47:54 -0800360
Jesse Hall1a7eb592016-05-01 21:04:40 +0200361template <typename Functor>
362void ForEachFileInZip(const std::string& zipname,
363 const std::string& dir_in_zip,
364 Functor functor) {
365 int32_t err;
366 ZipArchiveHandle zip = nullptr;
367 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
368 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
369 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800370 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200371 std::string prefix(dir_in_zip + "/");
Jesse Hall1a7eb592016-05-01 21:04:40 +0200372 void* iter_cookie = nullptr;
Elliott Hughes54492042019-05-08 12:00:32 -0700373 if ((err = StartIteration(zip, &iter_cookie, prefix, "")) != 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200374 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
375 err);
376 CloseArchive(zip);
377 return;
378 }
379 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
380 dir_in_zip.c_str());
381 ZipEntry entry;
Elliott Hughesc1f24852019-05-22 19:47:17 -0700382 std::string name;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200383 while (Next(iter_cookie, &entry, &name) == 0) {
Elliott Hughesc1f24852019-05-22 19:47:17 -0700384 std::string filename(name.substr(prefix.length()));
Jesse Hall1a7eb592016-05-01 21:04:40 +0200385 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700386 if (filename.find('/') != filename.npos)
387 continue;
388 // Check whether it *may* be possible to load the library directly from
389 // the APK. Loading still may fail for other reasons, but this at least
390 // lets us avoid failed-to-load log messages in the typical case of
391 // compressed and/or unaligned libraries.
392 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
393 continue;
394 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200395 }
396 EndIteration(iter_cookie);
397 CloseArchive(zip);
398}
Jesse Hall80523e22016-01-06 16:47:54 -0800399
Jesse Hall1a7eb592016-05-01 21:04:40 +0200400template <typename Functor>
401void ForEachFileInPath(const std::string& path, Functor functor) {
402 size_t zip_pos = path.find("!/");
403 if (zip_pos == std::string::npos) {
404 ForEachFileInDir(path, functor);
405 } else {
406 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
407 functor);
408 }
409}
410
411void DiscoverLayersInPathList(const std::string& pathstr) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800412 ATRACE_CALL();
413
Jesse Hall1a7eb592016-05-01 21:04:40 +0200414 std::vector<std::string> paths = android::base::Split(pathstr, ":");
415 for (const auto& path : paths) {
416 ForEachFileInPath(path, [&](const std::string& filename) {
417 if (android::base::StartsWith(filename, "libVkLayer") &&
418 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600419
420 // Check to ensure we haven't seen this layer already
421 // Let the first instance of the shared object be enumerated
422 // We're searching for layers in following order:
423 // 1. system path
424 // 2. libraryPermittedPath (if enabled)
425 // 3. libraryPath
426
427 bool duplicate = false;
428 for (auto& layer : g_layer_libraries) {
429 if (layer.GetFilename() == filename) {
430 ALOGV("Skipping duplicate layer %s in %s",
431 filename.c_str(), path.c_str());
432 duplicate = true;
433 }
434 }
435
436 if (!duplicate)
437 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200438 }
439 });
440 }
Jesse Hall80523e22016-01-06 16:47:54 -0800441}
442
Chia-I Wudab25652016-04-28 07:15:51 +0800443const VkExtensionProperties* FindExtension(
444 const std::vector<VkExtensionProperties>& extensions,
445 const char* name) {
446 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
447 [=](const VkExtensionProperties& ext) {
448 return (strcmp(ext.extensionName, name) == 0);
449 });
450 return (it != extensions.cend()) ? &*it : nullptr;
451}
452
Jesse Hall80523e22016-01-06 16:47:54 -0800453void* GetLayerGetProcAddr(const Layer& layer,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700454 const std::string_view gpa_name) {
Jesse Hall80523e22016-01-06 16:47:54 -0800455 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700456 return library.GetGPA(layer, gpa_name);
Jesse Hall80523e22016-01-06 16:47:54 -0800457}
458
Jesse Hallaa410942016-01-17 13:07:10 -0800459} // anonymous namespace
460
Jesse Hallaa410942016-01-17 13:07:10 -0800461void DiscoverLayers() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800462 ATRACE_CALL();
463
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800464 if (android::GraphicsEnv::getInstance().isDebuggable()) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200465 DiscoverLayersInPathList(kSystemLayerLibraryDir);
466 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600467 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
468 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800469}
470
Chia-I Wu25700b42016-04-28 06:36:09 +0800471uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800472 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800473}
474
Chia-I Wu25700b42016-04-28 06:36:09 +0800475const Layer& GetLayer(uint32_t index) {
476 return g_instance_layers[index];
477}
Chia-I Wubea09db2016-04-22 09:42:41 +0800478
Chia-I Wu04c65512016-04-27 09:54:02 +0800479const Layer* FindLayer(const char* name) {
480 auto layer =
481 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
482 [=](const Layer& entry) {
483 return strcmp(entry.properties.layerName, name) == 0;
484 });
485 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
486}
487
Chia-I Wu25700b42016-04-28 06:36:09 +0800488const VkLayerProperties& GetLayerProperties(const Layer& layer) {
489 return layer.properties;
490}
Chia-I Wubea09db2016-04-22 09:42:41 +0800491
Chia-I Wu25700b42016-04-28 06:36:09 +0800492bool IsLayerGlobal(const Layer& layer) {
493 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800494}
495
Chia-I Wu04c65512016-04-27 09:54:02 +0800496const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
497 uint32_t& count) {
498 count = static_cast<uint32_t>(layer.instance_extensions.size());
499 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800500}
501
Chia-I Wu04c65512016-04-27 09:54:02 +0800502const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
503 uint32_t& count) {
504 count = static_cast<uint32_t>(layer.device_extensions.size());
505 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800506}
507
Chia-I Wudab25652016-04-28 07:15:51 +0800508const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
509 const char* name) {
510 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800511}
512
Chia-I Wudab25652016-04-28 07:15:51 +0800513const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
514 const char* name) {
515 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800516}
517
Chia-I Wudab25652016-04-28 07:15:51 +0800518LayerRef GetLayerRef(const Layer& layer) {
519 LayerLibrary& library = g_layer_libraries[layer.library_idx];
520 return LayerRef((library.Open()) ? &layer : nullptr);
521}
522
523LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800524
525LayerRef::~LayerRef() {
526 if (layer_) {
527 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800528 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800529 }
530}
531
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -0700532LayerRef::LayerRef(LayerRef&& other) noexcept : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600533 other.layer_ = nullptr;
534}
Jesse Hall80523e22016-01-06 16:47:54 -0800535
536PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
537 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700538 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr"))
Jesse Hall80523e22016-01-06 16:47:54 -0800539 : nullptr;
540}
541
542PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
543 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700544 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr"))
Jesse Hall80523e22016-01-06 16:47:54 -0800545 : nullptr;
546}
547
Chia-I Wuc96880f2016-03-26 06:56:45 +0800548} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800549} // namespace vulkan