blob: c073579d3df7d73b0672c980224707e728b6cbaf [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>
Kalesh Singh66f3a962023-08-09 15:47:20 -070026#include <unistd.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070027
28#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),
Alina Kalyakina02efc3a2023-09-20 16:41:22 +000077 opened_with_native_loader_(false),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060078 refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080079
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -070080 LayerLibrary(LayerLibrary&& other) noexcept
Chia-I Wu6693f5c2016-04-18 12:20:02 +080081 : path_(std::move(other.path_)),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060082 filename_(std::move(other.filename_)),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080083 dlhandle_(other.dlhandle_),
Victor Khimenkobbf77002018-08-16 22:38:36 +020084 native_bridge_(other.native_bridge_),
Alina Kalyakina02efc3a2023-09-20 16:41:22 +000085 opened_with_native_loader_(other.opened_with_native_loader_),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080086 refcount_(other.refcount_) {
87 other.dlhandle_ = nullptr;
88 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080089 }
90
91 LayerLibrary(const LayerLibrary&) = delete;
92 LayerLibrary& operator=(const LayerLibrary&) = delete;
93
Chia-I Wua6229742016-04-26 07:37:44 +080094 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080095 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080096 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080097
Chia-I Wu50174ee2016-04-18 16:33:20 +080098 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080099 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800100
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700101 void* GetGPA(const Layer& layer, const std::string_view gpa_name) const;
Chia-I Wuba113272016-04-18 16:38:39 +0800102
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600103 const std::string GetFilename() { return filename_; }
104
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800105 private:
Victor Khimenkobbf77002018-08-16 22:38:36 +0200106 // TODO(b/79940628): remove that adapter when we could use NativeBridgeGetTrampoline
107 // for native libraries.
108 template<typename Func = void*>
109 Func GetTrampoline(const char* name) const {
110 if (native_bridge_) {
111 return reinterpret_cast<Func>(android::NativeBridgeGetTrampoline(
112 dlhandle_, name, nullptr, 0));
113 }
114 return reinterpret_cast<Func>(dlsym(dlhandle_, name));
115 }
116
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800117 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +0800118
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600119 // Track the filename alone so we can detect duplicates
120 const std::string filename_;
121
Chia-I Wua6229742016-04-26 07:37:44 +0800122 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800123 void* dlhandle_;
Victor Khimenkobbf77002018-08-16 22:38:36 +0200124 bool native_bridge_;
Alina Kalyakina02efc3a2023-09-20 16:41:22 +0000125 bool opened_with_native_loader_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800126 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800127};
Chia-I Wu74349592016-04-18 12:08:39 +0800128
Chia-I Wufd0389f2016-04-18 12:11:00 +0800129bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800130 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800131 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200132 ALOGV("opening layer library '%s'", path_.c_str());
Jesse Hall40c07a12016-05-11 22:56:29 -0700133 // Libraries in the system layer library dir can't be loaded into
134 // the application namespace. That causes compatibility problems, since
135 // any symbol dependencies will be resolved by system libraries. They
136 // can't safely use libc++_shared, for example. Which is one reason
137 // (among several) we only allow them in non-user builds.
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600138 auto app_namespace = android::GraphicsEnv::getInstance().getAppNamespace();
Jesse Hall40c07a12016-05-11 22:56:29 -0700139 if (app_namespace &&
140 !android::base::StartsWith(path_, kSystemLayerLibraryDir)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000141 char* error_msg = nullptr;
Alina Kalyakina02efc3a2023-09-20 16:41:22 +0000142 dlhandle_ = android::OpenNativeLibraryInNamespace(
Victor Khimenkobbf77002018-08-16 22:38:36 +0200143 app_namespace, path_.c_str(), &native_bridge_, &error_msg);
144 if (!dlhandle_) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000145 ALOGE("failed to load layer library '%s': %s", path_.c_str(), error_msg);
146 android::NativeLoaderFreeErrorMessage(error_msg);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200147 refcount_ = 0;
148 return false;
149 }
Alina Kalyakina02efc3a2023-09-20 16:41:22 +0000150 opened_with_native_loader_ = true;
Jesse Hall40c07a12016-05-11 22:56:29 -0700151 } else {
Alina Kalyakina02efc3a2023-09-20 16:41:22 +0000152 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200153 if (!dlhandle_) {
154 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
155 dlerror());
156 refcount_ = 0;
157 return false;
158 }
Alina Kalyakina02efc3a2023-09-20 16:41:22 +0000159 opened_with_native_loader_ = false;
Chia-I Wufd0389f2016-04-18 12:11:00 +0800160 }
161 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800162 return true;
163}
164
Chia-I Wud91c74f2016-04-18 12:12:36 +0800165void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800166 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800167 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200168 ALOGV("closing layer library '%s'", path_.c_str());
Alina Kalyakina02efc3a2023-09-20 16:41:22 +0000169 // we close the .so same way as we opened. It's importain, because
170 // android::CloseNativeLibrary lives in libnativeloader.so, which is
171 // not accessible for early loaded services like SurfaceFlinger
172 if (opened_with_native_loader_) {
173 char* error_msg = nullptr;
174 if (!android::CloseNativeLibrary(dlhandle_, native_bridge_, &error_msg)) {
175 ALOGE("failed to unload library '%s': %s", path_.c_str(), error_msg);
176 android::NativeLoaderFreeErrorMessage(error_msg);
177 refcount_++;
178 return;
179 }
Jacky.Deng01728c12018-08-28 15:25:31 +0800180 } else {
Alina Kalyakina02efc3a2023-09-20 16:41:22 +0000181 if (dlclose(dlhandle_) != 0) {
182 ALOGE("failed to unload library '%s': %s", path_.c_str(), dlerror());
183 refcount_++;
184 return;
185 }
Jacky.Deng01728c12018-08-28 15:25:31 +0800186 }
Alina Kalyakina02efc3a2023-09-20 16:41:22 +0000187 dlhandle_ = nullptr;
Chia-I Wud91c74f2016-04-18 12:12:36 +0800188 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800189}
190
Chia-I Wu50174ee2016-04-18 16:33:20 +0800191bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800192 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800193 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200194 GetTrampoline<PFN_vkEnumerateInstanceLayerProperties>(
195 "vkEnumerateInstanceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800196 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200197 GetTrampoline<PFN_vkEnumerateInstanceExtensionProperties>(
198 "vkEnumerateInstanceExtensionProperties");
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800199 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200200 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800201 path_.c_str());
202 return false;
203 }
204
205 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800206 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200207 GetTrampoline<PFN_vkEnumerateDeviceLayerProperties>(
208 "vkEnumerateDeviceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800209 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200210 GetTrampoline<PFN_vkEnumerateDeviceExtensionProperties>(
211 "vkEnumerateDeviceExtensionProperties");
Jesse Hall80523e22016-01-06 16:47:54 -0800212
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800213 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800214 uint32_t num_instance_layers = 0;
215 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800216 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
217 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800218 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200219 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800220 "vkEnumerateInstanceLayerProperties failed for library '%s': "
221 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800222 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800223 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800224 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800225 }
Jesse Hallaa410942016-01-17 13:07:10 -0800226 if (enumerate_device_layers) {
227 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
228 nullptr);
229 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200230 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800231 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800232 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800233 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800234 }
235 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800236
237 // get layer properties
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700238 std::vector<VkLayerProperties> properties(num_instance_layers + num_device_layers);
239 result = enumerate_instance_layers(&num_instance_layers, properties.data());
Nick Desaulniers03b11cf2019-10-25 11:23:08 -0700240 if (result != VK_SUCCESS) {
241 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
242 path_.c_str(), result);
243 return false;
244 }
Jesse Hallaa410942016-01-17 13:07:10 -0800245 if (num_device_layers > 0) {
246 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700247 &properties[num_instance_layers]);
Jesse Hallaa410942016-01-17 13:07:10 -0800248 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200249 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800250 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800251 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800252 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800253 }
Jesse Hall80523e22016-01-06 16:47:54 -0800254 }
255
Chia-I Wubea09db2016-04-22 09:42:41 +0800256 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800257 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800258 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800259 for (size_t i = 0; i < num_instance_layers; i++) {
260 const VkLayerProperties& props = properties[i];
261
Jesse Hall80523e22016-01-06 16:47:54 -0800262 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800263 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800264 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800265 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800266
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800267 uint32_t count = 0;
268 result =
269 enumerate_instance_extensions(props.layerName, &count, nullptr);
270 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200271 ALOGE(
272 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
273 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800274 props.layerName, path_.c_str(), result);
275 instance_layers.resize(prev_num_instance_layers);
276 return false;
277 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800278 layer.instance_extensions.resize(count);
279 result = enumerate_instance_extensions(
280 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800281 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200282 ALOGE(
283 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
284 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800285 props.layerName, path_.c_str(), result);
286 instance_layers.resize(prev_num_instance_layers);
287 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800288 }
289
Chia-I Wu279ccc02016-04-18 16:45:15 +0800290 for (size_t j = 0; j < num_device_layers; j++) {
291 const auto& dev_props = properties[num_instance_layers + j];
292 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800293 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800294 break;
295 }
296 }
Jesse Hallaa410942016-01-17 13:07:10 -0800297
Chia-I Wubea09db2016-04-22 09:42:41 +0800298 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800299 result = enumerate_device_extensions(
300 VK_NULL_HANDLE, props.layerName, &count, nullptr);
301 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200302 ALOGE(
303 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800304 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800305 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800306 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800307 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800308 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800309 layer.device_extensions.resize(count);
310 result = enumerate_device_extensions(
311 VK_NULL_HANDLE, props.layerName, &count,
312 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800313 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200314 ALOGE(
315 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800316 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800317 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800318 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800319 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800320 }
321 }
322
Chia-I Wubea09db2016-04-22 09:42:41 +0800323 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200324 ALOGD("added %s layer '%s' from library '%s'",
325 (layer.is_global) ? "global" : "instance", props.layerName,
326 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800327 }
328
Chia-I Wu50174ee2016-04-18 16:33:20 +0800329 return true;
330}
Jesse Hall80523e22016-01-06 16:47:54 -0800331
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700332void* LayerLibrary::GetGPA(const Layer& layer, const std::string_view gpa_name) const {
333 std::string layer_name { layer.properties.layerName };
334 if (void* gpa = GetTrampoline((layer_name.append(gpa_name).c_str())))
335 return gpa;
336 return GetTrampoline((std::string {"vk"}.append(gpa_name)).c_str());
Chia-I Wuba113272016-04-18 16:38:39 +0800337}
338
Jesse Hall1a7eb592016-05-01 21:04:40 +0200339// ----------------------------------------------------------------------------
340
Chia-I Wu50174ee2016-04-18 16:33:20 +0800341std::vector<LayerLibrary> g_layer_libraries;
342std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800343
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600344void AddLayerLibrary(const std::string& path, const std::string& filename) {
345 LayerLibrary library(path + "/" + filename, filename);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800346 if (!library.Open())
347 return;
348
Chia-I Wubea09db2016-04-22 09:42:41 +0800349 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800350 library.Close();
351 return;
352 }
353
354 library.Close();
355
356 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800357}
358
Jesse Hall1a7eb592016-05-01 21:04:40 +0200359template <typename Functor>
360void ForEachFileInDir(const std::string& dirname, Functor functor) {
361 auto dir_deleter = [](DIR* handle) { closedir(handle); };
362 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
363 dir_deleter);
364 if (!dir) {
365 // It's normal for some search directories to not exist, especially
366 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800367 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200368 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
369 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800370 return;
371 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200372 ALOGD("searching for layers in '%s'", dirname.c_str());
373 dirent* entry;
374 while ((entry = readdir(dir.get())) != nullptr)
375 functor(entry->d_name);
376}
Jesse Hall80523e22016-01-06 16:47:54 -0800377
Jesse Hall1a7eb592016-05-01 21:04:40 +0200378template <typename Functor>
379void ForEachFileInZip(const std::string& zipname,
380 const std::string& dir_in_zip,
381 Functor functor) {
Kalesh Singh66f3a962023-08-09 15:47:20 -0700382 static const size_t kPageSize = getpagesize();
Jesse Hall1a7eb592016-05-01 21:04:40 +0200383 int32_t err;
384 ZipArchiveHandle zip = nullptr;
385 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
386 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
387 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800388 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200389 std::string prefix(dir_in_zip + "/");
Jesse Hall1a7eb592016-05-01 21:04:40 +0200390 void* iter_cookie = nullptr;
Elliott Hughes54492042019-05-08 12:00:32 -0700391 if ((err = StartIteration(zip, &iter_cookie, prefix, "")) != 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200392 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
393 err);
394 CloseArchive(zip);
395 return;
396 }
397 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
398 dir_in_zip.c_str());
399 ZipEntry entry;
Elliott Hughesc1f24852019-05-22 19:47:17 -0700400 std::string name;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200401 while (Next(iter_cookie, &entry, &name) == 0) {
Elliott Hughesc1f24852019-05-22 19:47:17 -0700402 std::string filename(name.substr(prefix.length()));
Jesse Hall1a7eb592016-05-01 21:04:40 +0200403 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700404 if (filename.find('/') != filename.npos)
405 continue;
406 // Check whether it *may* be possible to load the library directly from
407 // the APK. Loading still may fail for other reasons, but this at least
408 // lets us avoid failed-to-load log messages in the typical case of
409 // compressed and/or unaligned libraries.
Kalesh Singh66f3a962023-08-09 15:47:20 -0700410 if (entry.method != kCompressStored || entry.offset % kPageSize != 0)
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700411 continue;
412 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200413 }
414 EndIteration(iter_cookie);
415 CloseArchive(zip);
416}
Jesse Hall80523e22016-01-06 16:47:54 -0800417
Jesse Hall1a7eb592016-05-01 21:04:40 +0200418template <typename Functor>
419void ForEachFileInPath(const std::string& path, Functor functor) {
420 size_t zip_pos = path.find("!/");
421 if (zip_pos == std::string::npos) {
422 ForEachFileInDir(path, functor);
423 } else {
424 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
425 functor);
426 }
427}
428
429void DiscoverLayersInPathList(const std::string& pathstr) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800430 ATRACE_CALL();
431
Jesse Hall1a7eb592016-05-01 21:04:40 +0200432 std::vector<std::string> paths = android::base::Split(pathstr, ":");
433 for (const auto& path : paths) {
434 ForEachFileInPath(path, [&](const std::string& filename) {
435 if (android::base::StartsWith(filename, "libVkLayer") &&
436 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600437
438 // Check to ensure we haven't seen this layer already
439 // Let the first instance of the shared object be enumerated
440 // We're searching for layers in following order:
441 // 1. system path
442 // 2. libraryPermittedPath (if enabled)
443 // 3. libraryPath
444
445 bool duplicate = false;
446 for (auto& layer : g_layer_libraries) {
447 if (layer.GetFilename() == filename) {
448 ALOGV("Skipping duplicate layer %s in %s",
449 filename.c_str(), path.c_str());
450 duplicate = true;
451 }
452 }
453
454 if (!duplicate)
455 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200456 }
457 });
458 }
Jesse Hall80523e22016-01-06 16:47:54 -0800459}
460
Chia-I Wudab25652016-04-28 07:15:51 +0800461const VkExtensionProperties* FindExtension(
462 const std::vector<VkExtensionProperties>& extensions,
463 const char* name) {
464 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
465 [=](const VkExtensionProperties& ext) {
466 return (strcmp(ext.extensionName, name) == 0);
467 });
468 return (it != extensions.cend()) ? &*it : nullptr;
469}
470
Jesse Hall80523e22016-01-06 16:47:54 -0800471void* GetLayerGetProcAddr(const Layer& layer,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700472 const std::string_view gpa_name) {
Jesse Hall80523e22016-01-06 16:47:54 -0800473 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700474 return library.GetGPA(layer, gpa_name);
Jesse Hall80523e22016-01-06 16:47:54 -0800475}
476
Jesse Hallaa410942016-01-17 13:07:10 -0800477} // anonymous namespace
478
Jesse Hallaa410942016-01-17 13:07:10 -0800479void DiscoverLayers() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800480 ATRACE_CALL();
481
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800482 if (android::GraphicsEnv::getInstance().isDebuggable()) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200483 DiscoverLayersInPathList(kSystemLayerLibraryDir);
484 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600485 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
486 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800487}
488
Chia-I Wu25700b42016-04-28 06:36:09 +0800489uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800490 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800491}
492
Chia-I Wu25700b42016-04-28 06:36:09 +0800493const Layer& GetLayer(uint32_t index) {
494 return g_instance_layers[index];
495}
Chia-I Wubea09db2016-04-22 09:42:41 +0800496
Chia-I Wu04c65512016-04-27 09:54:02 +0800497const Layer* FindLayer(const char* name) {
498 auto layer =
499 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
500 [=](const Layer& entry) {
501 return strcmp(entry.properties.layerName, name) == 0;
502 });
503 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
504}
505
Chia-I Wu25700b42016-04-28 06:36:09 +0800506const VkLayerProperties& GetLayerProperties(const Layer& layer) {
507 return layer.properties;
508}
Chia-I Wubea09db2016-04-22 09:42:41 +0800509
Chia-I Wu25700b42016-04-28 06:36:09 +0800510bool IsLayerGlobal(const Layer& layer) {
511 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800512}
513
Chia-I Wu04c65512016-04-27 09:54:02 +0800514const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
515 uint32_t& count) {
516 count = static_cast<uint32_t>(layer.instance_extensions.size());
517 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800518}
519
Chia-I Wu04c65512016-04-27 09:54:02 +0800520const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
521 uint32_t& count) {
522 count = static_cast<uint32_t>(layer.device_extensions.size());
523 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800524}
525
Chia-I Wudab25652016-04-28 07:15:51 +0800526const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
527 const char* name) {
528 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800529}
530
Chia-I Wudab25652016-04-28 07:15:51 +0800531const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
532 const char* name) {
533 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800534}
535
Chia-I Wudab25652016-04-28 07:15:51 +0800536LayerRef GetLayerRef(const Layer& layer) {
537 LayerLibrary& library = g_layer_libraries[layer.library_idx];
538 return LayerRef((library.Open()) ? &layer : nullptr);
539}
540
541LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800542
543LayerRef::~LayerRef() {
544 if (layer_) {
545 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800546 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800547 }
548}
549
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -0700550LayerRef::LayerRef(LayerRef&& other) noexcept : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600551 other.layer_ = nullptr;
552}
Jesse Hall80523e22016-01-06 16:47:54 -0800553
554PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
555 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700556 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr"))
Jesse Hall80523e22016-01-06 16:47:54 -0800557 : nullptr;
558}
559
560PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
561 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700562 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr"))
Jesse Hall80523e22016-01-06 16:47:54 -0800563 : nullptr;
564}
565
Chia-I Wuc96880f2016-03-26 06:56:45 +0800566} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800567} // namespace vulkan