blob: 758ab258856d7fe69f7b5c4faa72827bd9bd3554 [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
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070027#include <memory>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070028#include <mutex>
29#include <string>
Jesse Hall80523e22016-01-06 16:47:54 -080030#include <vector>
Jesse Hall1a7eb592016-05-01 21:04:40 +020031
Jesse Hall40c07a12016-05-11 22:56:29 -070032#include <android/dlext.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070033#include <android-base/strings.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020034#include <cutils/properties.h>
Cody Northropd2aa3ab2017-10-20 09:01:53 -060035#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070036#include <log/log.h>
Victor Khimenkobbf77002018-08-16 22:38:36 +020037#include <nativebridge/native_bridge.h>
38#include <nativeloader/native_loader.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080039#include <utils/Trace.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020040#include <ziparchive/zip_archive.h>
41
Yiwei Zhanga885c062019-10-24 12:07:57 -070042// TODO(b/143296676): This file currently builds up global data structures as it
Jesse Hallaa410942016-01-17 13:07:10 -080043// 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),
Victor Khimenkobbf77002018-08-16 22:38:36 +020076 native_bridge_(false),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060077 refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080078
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -070079 LayerLibrary(LayerLibrary&& other) noexcept
Chia-I Wu6693f5c2016-04-18 12:20:02 +080080 : path_(std::move(other.path_)),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060081 filename_(std::move(other.filename_)),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080082 dlhandle_(other.dlhandle_),
Victor Khimenkobbf77002018-08-16 22:38:36 +020083 native_bridge_(other.native_bridge_),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080084 refcount_(other.refcount_) {
85 other.dlhandle_ = nullptr;
86 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080087 }
88
89 LayerLibrary(const LayerLibrary&) = delete;
90 LayerLibrary& operator=(const LayerLibrary&) = delete;
91
Chia-I Wua6229742016-04-26 07:37:44 +080092 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080093 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080094 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080095
Chia-I Wu50174ee2016-04-18 16:33:20 +080096 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080097 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +080098
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070099 void* GetGPA(const Layer& layer, const std::string_view gpa_name) const;
Chia-I Wuba113272016-04-18 16:38:39 +0800100
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:
Victor Khimenkobbf77002018-08-16 22:38:36 +0200104 // TODO(b/79940628): remove that adapter when we could use NativeBridgeGetTrampoline
105 // for native libraries.
106 template<typename Func = void*>
107 Func GetTrampoline(const char* name) const {
108 if (native_bridge_) {
109 return reinterpret_cast<Func>(android::NativeBridgeGetTrampoline(
110 dlhandle_, name, nullptr, 0));
111 }
112 return reinterpret_cast<Func>(dlsym(dlhandle_, name));
113 }
114
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800115 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +0800116
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600117 // Track the filename alone so we can detect duplicates
118 const std::string filename_;
119
Chia-I Wua6229742016-04-26 07:37:44 +0800120 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800121 void* dlhandle_;
Victor Khimenkobbf77002018-08-16 22:38:36 +0200122 bool native_bridge_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800123 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800124};
Chia-I Wu74349592016-04-18 12:08:39 +0800125
Chia-I Wufd0389f2016-04-18 12:11:00 +0800126bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800127 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800128 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200129 ALOGV("opening layer library '%s'", path_.c_str());
Jesse Hall40c07a12016-05-11 22:56:29 -0700130 // Libraries in the system layer library dir can't be loaded into
131 // the application namespace. That causes compatibility problems, since
132 // any symbol dependencies will be resolved by system libraries. They
133 // can't safely use libc++_shared, for example. Which is one reason
134 // (among several) we only allow them in non-user builds.
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600135 auto app_namespace = android::GraphicsEnv::getInstance().getAppNamespace();
Jesse Hall40c07a12016-05-11 22:56:29 -0700136 if (app_namespace &&
137 !android::base::StartsWith(path_, kSystemLayerLibraryDir)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000138 char* error_msg = nullptr;
139 dlhandle_ = OpenNativeLibraryInNamespace(
Victor Khimenkobbf77002018-08-16 22:38:36 +0200140 app_namespace, path_.c_str(), &native_bridge_, &error_msg);
141 if (!dlhandle_) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000142 ALOGE("failed to load layer library '%s': %s", path_.c_str(), error_msg);
143 android::NativeLoaderFreeErrorMessage(error_msg);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200144 refcount_ = 0;
145 return false;
146 }
Jesse Hall40c07a12016-05-11 22:56:29 -0700147 } else {
Victor Khimenkobbf77002018-08-16 22:38:36 +0200148 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
149 if (!dlhandle_) {
150 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
151 dlerror());
152 refcount_ = 0;
153 return false;
154 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800155 }
156 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800157 return true;
158}
159
Chia-I Wud91c74f2016-04-18 12:12:36 +0800160void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800161 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800162 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200163 ALOGV("closing layer library '%s'", path_.c_str());
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000164 char* error_msg = nullptr;
Jacky.Deng01728c12018-08-28 15:25:31 +0800165 if (!android::CloseNativeLibrary(dlhandle_, native_bridge_, &error_msg)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000166 ALOGE("failed to unload library '%s': %s", path_.c_str(), error_msg);
167 android::NativeLoaderFreeErrorMessage(error_msg);
Jacky.Deng01728c12018-08-28 15:25:31 +0800168 refcount_++;
169 } else {
170 dlhandle_ = nullptr;
171 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800172 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800173}
174
Chia-I Wu50174ee2016-04-18 16:33:20 +0800175bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800176 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800177 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200178 GetTrampoline<PFN_vkEnumerateInstanceLayerProperties>(
179 "vkEnumerateInstanceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800180 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200181 GetTrampoline<PFN_vkEnumerateInstanceExtensionProperties>(
182 "vkEnumerateInstanceExtensionProperties");
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800183 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200184 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800185 path_.c_str());
186 return false;
187 }
188
189 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800190 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200191 GetTrampoline<PFN_vkEnumerateDeviceLayerProperties>(
192 "vkEnumerateDeviceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800193 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200194 GetTrampoline<PFN_vkEnumerateDeviceExtensionProperties>(
195 "vkEnumerateDeviceExtensionProperties");
Jesse Hall80523e22016-01-06 16:47:54 -0800196
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800197 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800198 uint32_t num_instance_layers = 0;
199 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800200 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
201 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800202 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200203 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800204 "vkEnumerateInstanceLayerProperties failed for library '%s': "
205 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800206 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800207 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800208 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800209 }
Jesse Hallaa410942016-01-17 13:07:10 -0800210 if (enumerate_device_layers) {
211 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
212 nullptr);
213 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200214 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800215 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800216 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800217 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800218 }
219 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800220
221 // get layer properties
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700222 auto properties = std::make_unique<VkLayerProperties[]>(num_instance_layers + num_device_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800223 if (num_device_layers > 0) {
224 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700225 properties.get() + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800226 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200227 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800228 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800229 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800230 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800231 }
Jesse Hall80523e22016-01-06 16:47:54 -0800232 }
233
Chia-I Wubea09db2016-04-22 09:42:41 +0800234 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800235 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800236 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800237 for (size_t i = 0; i < num_instance_layers; i++) {
238 const VkLayerProperties& props = properties[i];
239
Jesse Hall80523e22016-01-06 16:47:54 -0800240 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800241 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800242 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800243 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800244
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800245 uint32_t count = 0;
246 result =
247 enumerate_instance_extensions(props.layerName, &count, nullptr);
248 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200249 ALOGE(
250 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
251 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800252 props.layerName, path_.c_str(), result);
253 instance_layers.resize(prev_num_instance_layers);
254 return false;
255 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800256 layer.instance_extensions.resize(count);
257 result = enumerate_instance_extensions(
258 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800259 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200260 ALOGE(
261 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
262 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800263 props.layerName, path_.c_str(), result);
264 instance_layers.resize(prev_num_instance_layers);
265 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800266 }
267
Chia-I Wu279ccc02016-04-18 16:45:15 +0800268 for (size_t j = 0; j < num_device_layers; j++) {
269 const auto& dev_props = properties[num_instance_layers + j];
270 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800271 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800272 break;
273 }
274 }
Jesse Hallaa410942016-01-17 13:07:10 -0800275
Chia-I Wubea09db2016-04-22 09:42:41 +0800276 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800277 result = enumerate_device_extensions(
278 VK_NULL_HANDLE, props.layerName, &count, nullptr);
279 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200280 ALOGE(
281 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800282 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800283 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800284 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800285 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800286 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800287 layer.device_extensions.resize(count);
288 result = enumerate_device_extensions(
289 VK_NULL_HANDLE, props.layerName, &count,
290 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800291 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200292 ALOGE(
293 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800294 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800295 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800296 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800297 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800298 }
299 }
300
Chia-I Wubea09db2016-04-22 09:42:41 +0800301 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200302 ALOGD("added %s layer '%s' from library '%s'",
303 (layer.is_global) ? "global" : "instance", props.layerName,
304 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800305 }
306
Chia-I Wu50174ee2016-04-18 16:33:20 +0800307 return true;
308}
Jesse Hall80523e22016-01-06 16:47:54 -0800309
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700310void* LayerLibrary::GetGPA(const Layer& layer, const std::string_view gpa_name) const {
311 std::string layer_name { layer.properties.layerName };
312 if (void* gpa = GetTrampoline((layer_name.append(gpa_name).c_str())))
313 return gpa;
314 return GetTrampoline((std::string {"vk"}.append(gpa_name)).c_str());
Chia-I Wuba113272016-04-18 16:38:39 +0800315}
316
Jesse Hall1a7eb592016-05-01 21:04:40 +0200317// ----------------------------------------------------------------------------
318
Chia-I Wu50174ee2016-04-18 16:33:20 +0800319std::vector<LayerLibrary> g_layer_libraries;
320std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800321
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600322void AddLayerLibrary(const std::string& path, const std::string& filename) {
323 LayerLibrary library(path + "/" + filename, filename);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800324 if (!library.Open())
325 return;
326
Chia-I Wubea09db2016-04-22 09:42:41 +0800327 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800328 library.Close();
329 return;
330 }
331
332 library.Close();
333
334 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800335}
336
Jesse Hall1a7eb592016-05-01 21:04:40 +0200337template <typename Functor>
338void ForEachFileInDir(const std::string& dirname, Functor functor) {
339 auto dir_deleter = [](DIR* handle) { closedir(handle); };
340 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
341 dir_deleter);
342 if (!dir) {
343 // It's normal for some search directories to not exist, especially
344 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800345 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200346 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
347 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800348 return;
349 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200350 ALOGD("searching for layers in '%s'", dirname.c_str());
351 dirent* entry;
352 while ((entry = readdir(dir.get())) != nullptr)
353 functor(entry->d_name);
354}
Jesse Hall80523e22016-01-06 16:47:54 -0800355
Jesse Hall1a7eb592016-05-01 21:04:40 +0200356template <typename Functor>
357void ForEachFileInZip(const std::string& zipname,
358 const std::string& dir_in_zip,
359 Functor functor) {
360 int32_t err;
361 ZipArchiveHandle zip = nullptr;
362 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
363 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
364 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800365 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200366 std::string prefix(dir_in_zip + "/");
Jesse Hall1a7eb592016-05-01 21:04:40 +0200367 void* iter_cookie = nullptr;
Elliott Hughes54492042019-05-08 12:00:32 -0700368 if ((err = StartIteration(zip, &iter_cookie, prefix, "")) != 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200369 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
370 err);
371 CloseArchive(zip);
372 return;
373 }
374 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
375 dir_in_zip.c_str());
376 ZipEntry entry;
Elliott Hughesc1f24852019-05-22 19:47:17 -0700377 std::string name;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200378 while (Next(iter_cookie, &entry, &name) == 0) {
Elliott Hughesc1f24852019-05-22 19:47:17 -0700379 std::string filename(name.substr(prefix.length()));
Jesse Hall1a7eb592016-05-01 21:04:40 +0200380 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700381 if (filename.find('/') != filename.npos)
382 continue;
383 // Check whether it *may* be possible to load the library directly from
384 // the APK. Loading still may fail for other reasons, but this at least
385 // lets us avoid failed-to-load log messages in the typical case of
386 // compressed and/or unaligned libraries.
387 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
388 continue;
389 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200390 }
391 EndIteration(iter_cookie);
392 CloseArchive(zip);
393}
Jesse Hall80523e22016-01-06 16:47:54 -0800394
Jesse Hall1a7eb592016-05-01 21:04:40 +0200395template <typename Functor>
396void ForEachFileInPath(const std::string& path, Functor functor) {
397 size_t zip_pos = path.find("!/");
398 if (zip_pos == std::string::npos) {
399 ForEachFileInDir(path, functor);
400 } else {
401 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
402 functor);
403 }
404}
405
406void DiscoverLayersInPathList(const std::string& pathstr) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800407 ATRACE_CALL();
408
Jesse Hall1a7eb592016-05-01 21:04:40 +0200409 std::vector<std::string> paths = android::base::Split(pathstr, ":");
410 for (const auto& path : paths) {
411 ForEachFileInPath(path, [&](const std::string& filename) {
412 if (android::base::StartsWith(filename, "libVkLayer") &&
413 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600414
415 // Check to ensure we haven't seen this layer already
416 // Let the first instance of the shared object be enumerated
417 // We're searching for layers in following order:
418 // 1. system path
419 // 2. libraryPermittedPath (if enabled)
420 // 3. libraryPath
421
422 bool duplicate = false;
423 for (auto& layer : g_layer_libraries) {
424 if (layer.GetFilename() == filename) {
425 ALOGV("Skipping duplicate layer %s in %s",
426 filename.c_str(), path.c_str());
427 duplicate = true;
428 }
429 }
430
431 if (!duplicate)
432 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200433 }
434 });
435 }
Jesse Hall80523e22016-01-06 16:47:54 -0800436}
437
Chia-I Wudab25652016-04-28 07:15:51 +0800438const VkExtensionProperties* FindExtension(
439 const std::vector<VkExtensionProperties>& extensions,
440 const char* name) {
441 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
442 [=](const VkExtensionProperties& ext) {
443 return (strcmp(ext.extensionName, name) == 0);
444 });
445 return (it != extensions.cend()) ? &*it : nullptr;
446}
447
Jesse Hall80523e22016-01-06 16:47:54 -0800448void* GetLayerGetProcAddr(const Layer& layer,
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700449 const std::string_view gpa_name) {
Jesse Hall80523e22016-01-06 16:47:54 -0800450 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700451 return library.GetGPA(layer, gpa_name);
Jesse Hall80523e22016-01-06 16:47:54 -0800452}
453
Jesse Hallaa410942016-01-17 13:07:10 -0800454} // anonymous namespace
455
Jesse Hallaa410942016-01-17 13:07:10 -0800456void DiscoverLayers() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800457 ATRACE_CALL();
458
Jesse Hall1a7eb592016-05-01 21:04:40 +0200459 if (property_get_bool("ro.debuggable", false) &&
460 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
461 DiscoverLayersInPathList(kSystemLayerLibraryDir);
462 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600463 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
464 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800465}
466
Chia-I Wu25700b42016-04-28 06:36:09 +0800467uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800468 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800469}
470
Chia-I Wu25700b42016-04-28 06:36:09 +0800471const Layer& GetLayer(uint32_t index) {
472 return g_instance_layers[index];
473}
Chia-I Wubea09db2016-04-22 09:42:41 +0800474
Chia-I Wu04c65512016-04-27 09:54:02 +0800475const Layer* FindLayer(const char* name) {
476 auto layer =
477 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
478 [=](const Layer& entry) {
479 return strcmp(entry.properties.layerName, name) == 0;
480 });
481 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
482}
483
Chia-I Wu25700b42016-04-28 06:36:09 +0800484const VkLayerProperties& GetLayerProperties(const Layer& layer) {
485 return layer.properties;
486}
Chia-I Wubea09db2016-04-22 09:42:41 +0800487
Chia-I Wu25700b42016-04-28 06:36:09 +0800488bool IsLayerGlobal(const Layer& layer) {
489 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800490}
491
Chia-I Wu04c65512016-04-27 09:54:02 +0800492const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
493 uint32_t& count) {
494 count = static_cast<uint32_t>(layer.instance_extensions.size());
495 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800496}
497
Chia-I Wu04c65512016-04-27 09:54:02 +0800498const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
499 uint32_t& count) {
500 count = static_cast<uint32_t>(layer.device_extensions.size());
501 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800502}
503
Chia-I Wudab25652016-04-28 07:15:51 +0800504const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
505 const char* name) {
506 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800507}
508
Chia-I Wudab25652016-04-28 07:15:51 +0800509const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
510 const char* name) {
511 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800512}
513
Chia-I Wudab25652016-04-28 07:15:51 +0800514LayerRef GetLayerRef(const Layer& layer) {
515 LayerLibrary& library = g_layer_libraries[layer.library_idx];
516 return LayerRef((library.Open()) ? &layer : nullptr);
517}
518
519LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800520
521LayerRef::~LayerRef() {
522 if (layer_) {
523 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800524 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800525 }
526}
527
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -0700528LayerRef::LayerRef(LayerRef&& other) noexcept : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600529 other.layer_ = nullptr;
530}
Jesse Hall80523e22016-01-06 16:47:54 -0800531
532PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
533 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700534 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr"))
Jesse Hall80523e22016-01-06 16:47:54 -0800535 : nullptr;
536}
537
538PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
539 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700540 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr"))
Jesse Hall80523e22016-01-06 16:47:54 -0800541 : nullptr;
542}
543
Chia-I Wuc96880f2016-03-26 06:56:45 +0800544} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800545} // namespace vulkan