blob: d059f8fe4c2fd26f78702499983a018152904a41 [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),
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 Desaulniers60307ce2019-10-25 13:34:21 -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 Desaulniers60307ce2019-10-25 13:34:21 -0700222 std::vector<VkLayerProperties> properties(num_instance_layers + num_device_layers);
223 result = enumerate_instance_layers(&num_instance_layers, properties.data());
Nick Desaulniers03b11cf2019-10-25 11:23:08 -0700224 if (result != VK_SUCCESS) {
225 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
226 path_.c_str(), result);
227 return false;
228 }
Jesse Hallaa410942016-01-17 13:07:10 -0800229 if (num_device_layers > 0) {
230 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700231 &properties[num_instance_layers]);
Jesse Hallaa410942016-01-17 13:07:10 -0800232 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200233 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800234 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800235 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800236 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800237 }
Jesse Hall80523e22016-01-06 16:47:54 -0800238 }
239
Chia-I Wubea09db2016-04-22 09:42:41 +0800240 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800241 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800242 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800243 for (size_t i = 0; i < num_instance_layers; i++) {
244 const VkLayerProperties& props = properties[i];
245
Jesse Hall80523e22016-01-06 16:47:54 -0800246 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800247 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800248 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800249 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800250
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800251 uint32_t count = 0;
252 result =
253 enumerate_instance_extensions(props.layerName, &count, nullptr);
254 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200255 ALOGE(
256 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
257 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800258 props.layerName, path_.c_str(), result);
259 instance_layers.resize(prev_num_instance_layers);
260 return false;
261 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800262 layer.instance_extensions.resize(count);
263 result = enumerate_instance_extensions(
264 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800265 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200266 ALOGE(
267 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
268 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800269 props.layerName, path_.c_str(), result);
270 instance_layers.resize(prev_num_instance_layers);
271 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800272 }
273
Chia-I Wu279ccc02016-04-18 16:45:15 +0800274 for (size_t j = 0; j < num_device_layers; j++) {
275 const auto& dev_props = properties[num_instance_layers + j];
276 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800277 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800278 break;
279 }
280 }
Jesse Hallaa410942016-01-17 13:07:10 -0800281
Chia-I Wubea09db2016-04-22 09:42:41 +0800282 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800283 result = enumerate_device_extensions(
284 VK_NULL_HANDLE, props.layerName, &count, nullptr);
285 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200286 ALOGE(
287 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800288 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800289 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800290 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800291 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800292 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800293 layer.device_extensions.resize(count);
294 result = enumerate_device_extensions(
295 VK_NULL_HANDLE, props.layerName, &count,
296 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800297 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200298 ALOGE(
299 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800300 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800301 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800302 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800303 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800304 }
305 }
306
Chia-I Wubea09db2016-04-22 09:42:41 +0800307 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200308 ALOGD("added %s layer '%s' from library '%s'",
309 (layer.is_global) ? "global" : "instance", props.layerName,
310 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800311 }
312
Chia-I Wu50174ee2016-04-18 16:33:20 +0800313 return true;
314}
Jesse Hall80523e22016-01-06 16:47:54 -0800315
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700316void* LayerLibrary::GetGPA(const Layer& layer, const std::string_view gpa_name) const {
317 std::string layer_name { layer.properties.layerName };
318 if (void* gpa = GetTrampoline((layer_name.append(gpa_name).c_str())))
319 return gpa;
320 return GetTrampoline((std::string {"vk"}.append(gpa_name)).c_str());
Chia-I Wuba113272016-04-18 16:38:39 +0800321}
322
Jesse Hall1a7eb592016-05-01 21:04:40 +0200323// ----------------------------------------------------------------------------
324
Chia-I Wu50174ee2016-04-18 16:33:20 +0800325std::vector<LayerLibrary> g_layer_libraries;
326std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800327
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600328void AddLayerLibrary(const std::string& path, const std::string& filename) {
329 LayerLibrary library(path + "/" + filename, filename);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800330 if (!library.Open())
331 return;
332
Chia-I Wubea09db2016-04-22 09:42:41 +0800333 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800334 library.Close();
335 return;
336 }
337
338 library.Close();
339
340 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800341}
342
Jesse Hall1a7eb592016-05-01 21:04:40 +0200343template <typename Functor>
344void ForEachFileInDir(const std::string& dirname, Functor functor) {
345 auto dir_deleter = [](DIR* handle) { closedir(handle); };
346 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
347 dir_deleter);
348 if (!dir) {
349 // It's normal for some search directories to not exist, especially
350 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800351 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200352 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
353 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800354 return;
355 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200356 ALOGD("searching for layers in '%s'", dirname.c_str());
357 dirent* entry;
358 while ((entry = readdir(dir.get())) != nullptr)
359 functor(entry->d_name);
360}
Jesse Hall80523e22016-01-06 16:47:54 -0800361
Jesse Hall1a7eb592016-05-01 21:04:40 +0200362template <typename Functor>
363void ForEachFileInZip(const std::string& zipname,
364 const std::string& dir_in_zip,
365 Functor functor) {
Kalesh Singh66f3a962023-08-09 15:47:20 -0700366 static const size_t kPageSize = getpagesize();
Jesse Hall1a7eb592016-05-01 21:04:40 +0200367 int32_t err;
368 ZipArchiveHandle zip = nullptr;
369 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
370 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
371 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800372 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200373 std::string prefix(dir_in_zip + "/");
Jesse Hall1a7eb592016-05-01 21:04:40 +0200374 void* iter_cookie = nullptr;
Elliott Hughes54492042019-05-08 12:00:32 -0700375 if ((err = StartIteration(zip, &iter_cookie, prefix, "")) != 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200376 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
377 err);
378 CloseArchive(zip);
379 return;
380 }
381 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
382 dir_in_zip.c_str());
383 ZipEntry entry;
Elliott Hughesc1f24852019-05-22 19:47:17 -0700384 std::string name;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200385 while (Next(iter_cookie, &entry, &name) == 0) {
Elliott Hughesc1f24852019-05-22 19:47:17 -0700386 std::string filename(name.substr(prefix.length()));
Jesse Hall1a7eb592016-05-01 21:04:40 +0200387 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700388 if (filename.find('/') != filename.npos)
389 continue;
390 // Check whether it *may* be possible to load the library directly from
391 // the APK. Loading still may fail for other reasons, but this at least
392 // lets us avoid failed-to-load log messages in the typical case of
393 // compressed and/or unaligned libraries.
Kalesh Singh66f3a962023-08-09 15:47:20 -0700394 if (entry.method != kCompressStored || entry.offset % kPageSize != 0)
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700395 continue;
396 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200397 }
398 EndIteration(iter_cookie);
399 CloseArchive(zip);
400}
Jesse Hall80523e22016-01-06 16:47:54 -0800401
Jesse Hall1a7eb592016-05-01 21:04:40 +0200402template <typename Functor>
403void ForEachFileInPath(const std::string& path, Functor functor) {
404 size_t zip_pos = path.find("!/");
405 if (zip_pos == std::string::npos) {
406 ForEachFileInDir(path, functor);
407 } else {
408 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
409 functor);
410 }
411}
412
413void DiscoverLayersInPathList(const std::string& pathstr) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800414 ATRACE_CALL();
415
Jesse Hall1a7eb592016-05-01 21:04:40 +0200416 std::vector<std::string> paths = android::base::Split(pathstr, ":");
417 for (const auto& path : paths) {
418 ForEachFileInPath(path, [&](const std::string& filename) {
419 if (android::base::StartsWith(filename, "libVkLayer") &&
420 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600421
422 // Check to ensure we haven't seen this layer already
423 // Let the first instance of the shared object be enumerated
424 // We're searching for layers in following order:
425 // 1. system path
426 // 2. libraryPermittedPath (if enabled)
427 // 3. libraryPath
428
429 bool duplicate = false;
430 for (auto& layer : g_layer_libraries) {
431 if (layer.GetFilename() == filename) {
432 ALOGV("Skipping duplicate layer %s in %s",
433 filename.c_str(), path.c_str());
434 duplicate = true;
435 }
436 }
437
438 if (!duplicate)
439 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200440 }
441 });
442 }
Jesse Hall80523e22016-01-06 16:47:54 -0800443}
444
Chia-I Wudab25652016-04-28 07:15:51 +0800445const VkExtensionProperties* FindExtension(
446 const std::vector<VkExtensionProperties>& extensions,
447 const char* name) {
448 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
449 [=](const VkExtensionProperties& ext) {
450 return (strcmp(ext.extensionName, name) == 0);
451 });
452 return (it != extensions.cend()) ? &*it : nullptr;
453}
454
Jesse Hall80523e22016-01-06 16:47:54 -0800455void* GetLayerGetProcAddr(const Layer& layer,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700456 const std::string_view gpa_name) {
Jesse Hall80523e22016-01-06 16:47:54 -0800457 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700458 return library.GetGPA(layer, gpa_name);
Jesse Hall80523e22016-01-06 16:47:54 -0800459}
460
Jesse Hallaa410942016-01-17 13:07:10 -0800461} // anonymous namespace
462
Jesse Hallaa410942016-01-17 13:07:10 -0800463void DiscoverLayers() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800464 ATRACE_CALL();
465
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800466 if (android::GraphicsEnv::getInstance().isDebuggable()) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200467 DiscoverLayersInPathList(kSystemLayerLibraryDir);
468 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600469 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
470 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800471}
472
Chia-I Wu25700b42016-04-28 06:36:09 +0800473uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800474 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800475}
476
Chia-I Wu25700b42016-04-28 06:36:09 +0800477const Layer& GetLayer(uint32_t index) {
478 return g_instance_layers[index];
479}
Chia-I Wubea09db2016-04-22 09:42:41 +0800480
Chia-I Wu04c65512016-04-27 09:54:02 +0800481const Layer* FindLayer(const char* name) {
482 auto layer =
483 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
484 [=](const Layer& entry) {
485 return strcmp(entry.properties.layerName, name) == 0;
486 });
487 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
488}
489
Chia-I Wu25700b42016-04-28 06:36:09 +0800490const VkLayerProperties& GetLayerProperties(const Layer& layer) {
491 return layer.properties;
492}
Chia-I Wubea09db2016-04-22 09:42:41 +0800493
Chia-I Wu25700b42016-04-28 06:36:09 +0800494bool IsLayerGlobal(const Layer& layer) {
495 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800496}
497
Chia-I Wu04c65512016-04-27 09:54:02 +0800498const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
499 uint32_t& count) {
500 count = static_cast<uint32_t>(layer.instance_extensions.size());
501 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800502}
503
Chia-I Wu04c65512016-04-27 09:54:02 +0800504const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
505 uint32_t& count) {
506 count = static_cast<uint32_t>(layer.device_extensions.size());
507 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800508}
509
Chia-I Wudab25652016-04-28 07:15:51 +0800510const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
511 const char* name) {
512 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800513}
514
Chia-I Wudab25652016-04-28 07:15:51 +0800515const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
516 const char* name) {
517 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800518}
519
Chia-I Wudab25652016-04-28 07:15:51 +0800520LayerRef GetLayerRef(const Layer& layer) {
521 LayerLibrary& library = g_layer_libraries[layer.library_idx];
522 return LayerRef((library.Open()) ? &layer : nullptr);
523}
524
525LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800526
527LayerRef::~LayerRef() {
528 if (layer_) {
529 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800530 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800531 }
532}
533
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -0700534LayerRef::LayerRef(LayerRef&& other) noexcept : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600535 other.layer_ = nullptr;
536}
Jesse Hall80523e22016-01-06 16:47:54 -0800537
538PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
539 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700540 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr"))
Jesse Hall80523e22016-01-06 16:47:54 -0800541 : nullptr;
542}
543
544PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
545 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700546 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr"))
Jesse Hall80523e22016-01-06 16:47:54 -0800547 : nullptr;
548}
549
Chia-I Wuc96880f2016-03-26 06:56:45 +0800550} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800551} // namespace vulkan