blob: dd917393d12fa3bd02466d6f28946d492335dbe8 [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
Jesse Hallb1471272016-01-17 21:36:58 -080042// TODO(jessehall): The whole way we deal with extensions is pretty hokey, and
43// not a good long-term solution. Having a hard-coded enum of extensions is
44// bad, of course. Representing sets of extensions (requested, supported, etc.)
45// as a bitset isn't necessarily bad, if the mapping from extension to bit were
46// dynamic. Need to rethink this completely when there's a little more time.
47
Jesse Hallaa410942016-01-17 13:07:10 -080048// TODO(jessehall): This file currently builds up global data structures as it
49// loads, and never cleans them up. This means we're doing heap allocations
50// without going through an app-provided allocator, but worse, we'll leak those
51// allocations if the loader is unloaded.
52//
53// We should allocate "enough" BSS space, and suballocate from there. Will
54// probably want to intern strings, etc., and will need some custom/manual data
55// structures.
56
Jesse Hall80523e22016-01-06 16:47:54 -080057namespace vulkan {
Chia-I Wuc96880f2016-03-26 06:56:45 +080058namespace api {
59
Jesse Hall80523e22016-01-06 16:47:54 -080060struct Layer {
61 VkLayerProperties properties;
62 size_t library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +080063
Chia-I Wu25700b42016-04-28 06:36:09 +080064 // true if the layer intercepts vkCreateDevice and device commands
Chia-I Wubea09db2016-04-22 09:42:41 +080065 bool is_global;
66
67 std::vector<VkExtensionProperties> instance_extensions;
68 std::vector<VkExtensionProperties> device_extensions;
Jesse Hall80523e22016-01-06 16:47:54 -080069};
Jesse Hall80523e22016-01-06 16:47:54 -080070
71namespace {
72
Jesse Hall1a7eb592016-05-01 21:04:40 +020073const char kSystemLayerLibraryDir[] = "/data/local/debug/vulkan";
74
Chia-I Wu6693f5c2016-04-18 12:20:02 +080075class LayerLibrary {
76 public:
Cody Northropd2aa3ab2017-10-20 09:01:53 -060077 explicit LayerLibrary(const std::string& path,
78 const std::string& filename)
79 : path_(path),
80 filename_(filename),
81 dlhandle_(nullptr),
Victor Khimenkobbf77002018-08-16 22:38:36 +020082 native_bridge_(false),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060083 refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080084
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -070085 LayerLibrary(LayerLibrary&& other) noexcept
Chia-I Wu6693f5c2016-04-18 12:20:02 +080086 : path_(std::move(other.path_)),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060087 filename_(std::move(other.filename_)),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080088 dlhandle_(other.dlhandle_),
Victor Khimenkobbf77002018-08-16 22:38:36 +020089 native_bridge_(other.native_bridge_),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080090 refcount_(other.refcount_) {
91 other.dlhandle_ = nullptr;
92 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080093 }
94
95 LayerLibrary(const LayerLibrary&) = delete;
96 LayerLibrary& operator=(const LayerLibrary&) = delete;
97
Chia-I Wua6229742016-04-26 07:37:44 +080098 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080099 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +0800100 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +0800101
Chia-I Wu50174ee2016-04-18 16:33:20 +0800102 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800103 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800104
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700105 void* GetGPA(const Layer& layer, const std::string_view gpa_name) const;
Chia-I Wuba113272016-04-18 16:38:39 +0800106
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600107 const std::string GetFilename() { return filename_; }
108
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800109 private:
Victor Khimenkobbf77002018-08-16 22:38:36 +0200110 // TODO(b/79940628): remove that adapter when we could use NativeBridgeGetTrampoline
111 // for native libraries.
112 template<typename Func = void*>
113 Func GetTrampoline(const char* name) const {
114 if (native_bridge_) {
115 return reinterpret_cast<Func>(android::NativeBridgeGetTrampoline(
116 dlhandle_, name, nullptr, 0));
117 }
118 return reinterpret_cast<Func>(dlsym(dlhandle_, name));
119 }
120
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800121 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +0800122
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600123 // Track the filename alone so we can detect duplicates
124 const std::string filename_;
125
Chia-I Wua6229742016-04-26 07:37:44 +0800126 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800127 void* dlhandle_;
Victor Khimenkobbf77002018-08-16 22:38:36 +0200128 bool native_bridge_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800129 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800130};
Chia-I Wu74349592016-04-18 12:08:39 +0800131
Chia-I Wufd0389f2016-04-18 12:11:00 +0800132bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800133 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800134 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200135 ALOGV("opening layer library '%s'", path_.c_str());
Jesse Hall40c07a12016-05-11 22:56:29 -0700136 // Libraries in the system layer library dir can't be loaded into
137 // the application namespace. That causes compatibility problems, since
138 // any symbol dependencies will be resolved by system libraries. They
139 // can't safely use libc++_shared, for example. Which is one reason
140 // (among several) we only allow them in non-user builds.
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600141 auto app_namespace = android::GraphicsEnv::getInstance().getAppNamespace();
Jesse Hall40c07a12016-05-11 22:56:29 -0700142 if (app_namespace &&
143 !android::base::StartsWith(path_, kSystemLayerLibraryDir)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000144 char* error_msg = nullptr;
145 dlhandle_ = OpenNativeLibraryInNamespace(
Victor Khimenkobbf77002018-08-16 22:38:36 +0200146 app_namespace, path_.c_str(), &native_bridge_, &error_msg);
147 if (!dlhandle_) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000148 ALOGE("failed to load layer library '%s': %s", path_.c_str(), error_msg);
149 android::NativeLoaderFreeErrorMessage(error_msg);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200150 refcount_ = 0;
151 return false;
152 }
Jesse Hall40c07a12016-05-11 22:56:29 -0700153 } else {
Victor Khimenkobbf77002018-08-16 22:38:36 +0200154 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
155 if (!dlhandle_) {
156 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
157 dlerror());
158 refcount_ = 0;
159 return false;
160 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800161 }
162 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800163 return true;
164}
165
Chia-I Wud91c74f2016-04-18 12:12:36 +0800166void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800167 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800168 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200169 ALOGV("closing layer library '%s'", path_.c_str());
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000170 char* error_msg = nullptr;
Jacky.Deng01728c12018-08-28 15:25:31 +0800171 if (!android::CloseNativeLibrary(dlhandle_, native_bridge_, &error_msg)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000172 ALOGE("failed to unload library '%s': %s", path_.c_str(), error_msg);
173 android::NativeLoaderFreeErrorMessage(error_msg);
Jacky.Deng01728c12018-08-28 15:25:31 +0800174 refcount_++;
175 } else {
176 dlhandle_ = nullptr;
177 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800178 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800179}
180
Chia-I Wu50174ee2016-04-18 16:33:20 +0800181bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800182 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800183 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200184 GetTrampoline<PFN_vkEnumerateInstanceLayerProperties>(
185 "vkEnumerateInstanceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800186 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200187 GetTrampoline<PFN_vkEnumerateInstanceExtensionProperties>(
188 "vkEnumerateInstanceExtensionProperties");
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800189 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200190 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800191 path_.c_str());
192 return false;
193 }
194
195 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800196 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200197 GetTrampoline<PFN_vkEnumerateDeviceLayerProperties>(
198 "vkEnumerateDeviceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800199 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200200 GetTrampoline<PFN_vkEnumerateDeviceExtensionProperties>(
201 "vkEnumerateDeviceExtensionProperties");
Jesse Hall80523e22016-01-06 16:47:54 -0800202
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800203 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800204 uint32_t num_instance_layers = 0;
205 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800206 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
207 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800208 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200209 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800210 "vkEnumerateInstanceLayerProperties failed for library '%s': "
211 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800212 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800213 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800214 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800215 }
Jesse Hallaa410942016-01-17 13:07:10 -0800216 if (enumerate_device_layers) {
217 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
218 nullptr);
219 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200220 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800221 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800222 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800223 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800224 }
225 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800226
227 // get layer properties
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700228 auto properties = std::make_unique<VkLayerProperties[]>(num_instance_layers + num_device_layers);
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 Desaulniers7c123cc2019-10-21 13:52:41 -0700231 properties.get() + 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 Desaulniers7c123cc2019-10-21 13:52:41 -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) {
366 int32_t err;
367 ZipArchiveHandle zip = nullptr;
368 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
369 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
370 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800371 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200372 std::string prefix(dir_in_zip + "/");
Jesse Hall1a7eb592016-05-01 21:04:40 +0200373 void* iter_cookie = nullptr;
Elliott Hughes54492042019-05-08 12:00:32 -0700374 if ((err = StartIteration(zip, &iter_cookie, prefix, "")) != 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200375 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
376 err);
377 CloseArchive(zip);
378 return;
379 }
380 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
381 dir_in_zip.c_str());
382 ZipEntry entry;
Elliott Hughesc1f24852019-05-22 19:47:17 -0700383 std::string name;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200384 while (Next(iter_cookie, &entry, &name) == 0) {
Elliott Hughesc1f24852019-05-22 19:47:17 -0700385 std::string filename(name.substr(prefix.length()));
Jesse Hall1a7eb592016-05-01 21:04:40 +0200386 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700387 if (filename.find('/') != filename.npos)
388 continue;
389 // Check whether it *may* be possible to load the library directly from
390 // the APK. Loading still may fail for other reasons, but this at least
391 // lets us avoid failed-to-load log messages in the typical case of
392 // compressed and/or unaligned libraries.
393 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
394 continue;
395 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200396 }
397 EndIteration(iter_cookie);
398 CloseArchive(zip);
399}
Jesse Hall80523e22016-01-06 16:47:54 -0800400
Jesse Hall1a7eb592016-05-01 21:04:40 +0200401template <typename Functor>
402void ForEachFileInPath(const std::string& path, Functor functor) {
403 size_t zip_pos = path.find("!/");
404 if (zip_pos == std::string::npos) {
405 ForEachFileInDir(path, functor);
406 } else {
407 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
408 functor);
409 }
410}
411
412void DiscoverLayersInPathList(const std::string& pathstr) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800413 ATRACE_CALL();
414
Jesse Hall1a7eb592016-05-01 21:04:40 +0200415 std::vector<std::string> paths = android::base::Split(pathstr, ":");
416 for (const auto& path : paths) {
417 ForEachFileInPath(path, [&](const std::string& filename) {
418 if (android::base::StartsWith(filename, "libVkLayer") &&
419 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600420
421 // Check to ensure we haven't seen this layer already
422 // Let the first instance of the shared object be enumerated
423 // We're searching for layers in following order:
424 // 1. system path
425 // 2. libraryPermittedPath (if enabled)
426 // 3. libraryPath
427
428 bool duplicate = false;
429 for (auto& layer : g_layer_libraries) {
430 if (layer.GetFilename() == filename) {
431 ALOGV("Skipping duplicate layer %s in %s",
432 filename.c_str(), path.c_str());
433 duplicate = true;
434 }
435 }
436
437 if (!duplicate)
438 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200439 }
440 });
441 }
Jesse Hall80523e22016-01-06 16:47:54 -0800442}
443
Chia-I Wudab25652016-04-28 07:15:51 +0800444const VkExtensionProperties* FindExtension(
445 const std::vector<VkExtensionProperties>& extensions,
446 const char* name) {
447 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
448 [=](const VkExtensionProperties& ext) {
449 return (strcmp(ext.extensionName, name) == 0);
450 });
451 return (it != extensions.cend()) ? &*it : nullptr;
452}
453
Jesse Hall80523e22016-01-06 16:47:54 -0800454void* GetLayerGetProcAddr(const Layer& layer,
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700455 const std::string_view gpa_name) {
Jesse Hall80523e22016-01-06 16:47:54 -0800456 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Nick Desaulniers7c123cc2019-10-21 13:52:41 -0700457 return library.GetGPA(layer, gpa_name);
Jesse Hall80523e22016-01-06 16:47:54 -0800458}
459
Jesse Hallaa410942016-01-17 13:07:10 -0800460} // anonymous namespace
461
Jesse Hallaa410942016-01-17 13:07:10 -0800462void DiscoverLayers() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800463 ATRACE_CALL();
464
Jesse Hall1a7eb592016-05-01 21:04:40 +0200465 if (property_get_bool("ro.debuggable", false) &&
466 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
467 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 Desaulniers7c123cc2019-10-21 13:52:41 -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 Desaulniers7c123cc2019-10-21 13:52:41 -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