blob: 2f33fee8ac6cc1da56796c251337da30cff493e1 [file] [log] [blame]
Jesse Hall80523e22016-01-06 16:47:54 -08001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Chia-I Wuc96880f2016-03-26 06:56:45 +080019#include "layers_extensions.h"
Jesse Hall1a7eb592016-05-01 21:04:40 +020020
Jesse Hall80523e22016-01-06 16:47:54 -080021#include <alloca.h>
22#include <dirent.h>
23#include <dlfcn.h>
Jesse Hall80523e22016-01-06 16:47:54 -080024#include <string.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070025#include <sys/prctl.h>
26
27#include <mutex>
28#include <string>
Jesse Hall80523e22016-01-06 16:47:54 -080029#include <vector>
Jesse Hall1a7eb592016-05-01 21:04:40 +020030
Jesse Hall40c07a12016-05-11 22:56:29 -070031#include <android/dlext.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070032#include <android-base/strings.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020033#include <cutils/properties.h>
Cody Northropd2aa3ab2017-10-20 09:01:53 -060034#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070035#include <log/log.h>
Victor Khimenkobbf77002018-08-16 22:38:36 +020036#include <nativebridge/native_bridge.h>
37#include <nativeloader/native_loader.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080038#include <utils/Trace.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020039#include <ziparchive/zip_archive.h>
40
Yiwei Zhanga885c062019-10-24 12:07:57 -070041// TODO(b/143296676): This file currently builds up global data structures as it
Jesse Hallaa410942016-01-17 13:07:10 -080042// loads, and never cleans them up. This means we're doing heap allocations
43// without going through an app-provided allocator, but worse, we'll leak those
44// allocations if the loader is unloaded.
45//
46// We should allocate "enough" BSS space, and suballocate from there. Will
47// probably want to intern strings, etc., and will need some custom/manual data
48// structures.
49
Jesse Hall80523e22016-01-06 16:47:54 -080050namespace vulkan {
Chia-I Wuc96880f2016-03-26 06:56:45 +080051namespace api {
52
Jesse Hall80523e22016-01-06 16:47:54 -080053struct Layer {
54 VkLayerProperties properties;
55 size_t library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +080056
Chia-I Wu25700b42016-04-28 06:36:09 +080057 // true if the layer intercepts vkCreateDevice and device commands
Chia-I Wubea09db2016-04-22 09:42:41 +080058 bool is_global;
59
60 std::vector<VkExtensionProperties> instance_extensions;
61 std::vector<VkExtensionProperties> device_extensions;
Jesse Hall80523e22016-01-06 16:47:54 -080062};
Jesse Hall80523e22016-01-06 16:47:54 -080063
64namespace {
65
Jesse Hall1a7eb592016-05-01 21:04:40 +020066const char kSystemLayerLibraryDir[] = "/data/local/debug/vulkan";
67
Chia-I Wu6693f5c2016-04-18 12:20:02 +080068class LayerLibrary {
69 public:
Cody Northropd2aa3ab2017-10-20 09:01:53 -060070 explicit LayerLibrary(const std::string& path,
71 const std::string& filename)
72 : path_(path),
73 filename_(filename),
74 dlhandle_(nullptr),
Victor Khimenkobbf77002018-08-16 22:38:36 +020075 native_bridge_(false),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060076 refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080077
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -070078 LayerLibrary(LayerLibrary&& other) noexcept
Chia-I Wu6693f5c2016-04-18 12:20:02 +080079 : path_(std::move(other.path_)),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060080 filename_(std::move(other.filename_)),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080081 dlhandle_(other.dlhandle_),
Victor Khimenkobbf77002018-08-16 22:38:36 +020082 native_bridge_(other.native_bridge_),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080083 refcount_(other.refcount_) {
84 other.dlhandle_ = nullptr;
85 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080086 }
87
88 LayerLibrary(const LayerLibrary&) = delete;
89 LayerLibrary& operator=(const LayerLibrary&) = delete;
90
Chia-I Wua6229742016-04-26 07:37:44 +080091 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080092 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080093 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080094
Chia-I Wu50174ee2016-04-18 16:33:20 +080095 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080096 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +080097
Chia-I Wuba113272016-04-18 16:38:39 +080098 void* GetGPA(const Layer& layer,
99 const char* gpa_name,
100 size_t gpa_name_len) const;
101
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600102 const std::string GetFilename() { return filename_; }
103
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800104 private:
Victor Khimenkobbf77002018-08-16 22:38:36 +0200105 // TODO(b/79940628): remove that adapter when we could use NativeBridgeGetTrampoline
106 // for native libraries.
107 template<typename Func = void*>
108 Func GetTrampoline(const char* name) const {
109 if (native_bridge_) {
110 return reinterpret_cast<Func>(android::NativeBridgeGetTrampoline(
111 dlhandle_, name, nullptr, 0));
112 }
113 return reinterpret_cast<Func>(dlsym(dlhandle_, name));
114 }
115
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800116 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +0800117
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600118 // Track the filename alone so we can detect duplicates
119 const std::string filename_;
120
Chia-I Wua6229742016-04-26 07:37:44 +0800121 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800122 void* dlhandle_;
Victor Khimenkobbf77002018-08-16 22:38:36 +0200123 bool native_bridge_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800124 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800125};
Chia-I Wu74349592016-04-18 12:08:39 +0800126
Chia-I Wufd0389f2016-04-18 12:11:00 +0800127bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800128 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800129 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200130 ALOGV("opening layer library '%s'", path_.c_str());
Jesse Hall40c07a12016-05-11 22:56:29 -0700131 // Libraries in the system layer library dir can't be loaded into
132 // the application namespace. That causes compatibility problems, since
133 // any symbol dependencies will be resolved by system libraries. They
134 // can't safely use libc++_shared, for example. Which is one reason
135 // (among several) we only allow them in non-user builds.
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600136 auto app_namespace = android::GraphicsEnv::getInstance().getAppNamespace();
Jesse Hall40c07a12016-05-11 22:56:29 -0700137 if (app_namespace &&
138 !android::base::StartsWith(path_, kSystemLayerLibraryDir)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000139 char* error_msg = nullptr;
140 dlhandle_ = OpenNativeLibraryInNamespace(
Victor Khimenkobbf77002018-08-16 22:38:36 +0200141 app_namespace, path_.c_str(), &native_bridge_, &error_msg);
142 if (!dlhandle_) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000143 ALOGE("failed to load layer library '%s': %s", path_.c_str(), error_msg);
144 android::NativeLoaderFreeErrorMessage(error_msg);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200145 refcount_ = 0;
146 return false;
147 }
Jesse Hall40c07a12016-05-11 22:56:29 -0700148 } else {
Victor Khimenkobbf77002018-08-16 22:38:36 +0200149 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
150 if (!dlhandle_) {
151 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
152 dlerror());
153 refcount_ = 0;
154 return false;
155 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800156 }
157 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800158 return true;
159}
160
Chia-I Wud91c74f2016-04-18 12:12:36 +0800161void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800162 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800163 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200164 ALOGV("closing layer library '%s'", path_.c_str());
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000165 char* error_msg = nullptr;
Jacky.Deng01728c12018-08-28 15:25:31 +0800166 if (!android::CloseNativeLibrary(dlhandle_, native_bridge_, &error_msg)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000167 ALOGE("failed to unload library '%s': %s", path_.c_str(), error_msg);
168 android::NativeLoaderFreeErrorMessage(error_msg);
Jacky.Deng01728c12018-08-28 15:25:31 +0800169 refcount_++;
170 } else {
171 dlhandle_ = nullptr;
172 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800173 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800174}
175
Chia-I Wu50174ee2016-04-18 16:33:20 +0800176bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800177 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800178 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200179 GetTrampoline<PFN_vkEnumerateInstanceLayerProperties>(
180 "vkEnumerateInstanceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800181 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200182 GetTrampoline<PFN_vkEnumerateInstanceExtensionProperties>(
183 "vkEnumerateInstanceExtensionProperties");
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800184 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200185 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800186 path_.c_str());
187 return false;
188 }
189
190 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800191 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200192 GetTrampoline<PFN_vkEnumerateDeviceLayerProperties>(
193 "vkEnumerateDeviceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800194 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200195 GetTrampoline<PFN_vkEnumerateDeviceExtensionProperties>(
196 "vkEnumerateDeviceExtensionProperties");
Jesse Hall80523e22016-01-06 16:47:54 -0800197
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800198 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800199 uint32_t num_instance_layers = 0;
200 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800201 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
202 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800203 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200204 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800205 "vkEnumerateInstanceLayerProperties failed for library '%s': "
206 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800207 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800208 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800209 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800210 }
Jesse Hallaa410942016-01-17 13:07:10 -0800211 if (enumerate_device_layers) {
212 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
213 nullptr);
214 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200215 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800216 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800217 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800218 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800219 }
220 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800221
222 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800223 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
224 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800225 result = enumerate_instance_layers(&num_instance_layers, properties);
226 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200227 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800228 path_.c_str(), result);
229 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800230 }
231 if (num_device_layers > 0) {
232 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
233 properties + num_instance_layers);
234 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200235 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800236 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800237 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800238 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800239 }
Jesse Hall80523e22016-01-06 16:47:54 -0800240 }
241
Chia-I Wubea09db2016-04-22 09:42:41 +0800242 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800243 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800244 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800245 for (size_t i = 0; i < num_instance_layers; i++) {
246 const VkLayerProperties& props = properties[i];
247
Jesse Hall80523e22016-01-06 16:47:54 -0800248 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800249 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800250 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800251 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800252
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800253 uint32_t count = 0;
254 result =
255 enumerate_instance_extensions(props.layerName, &count, nullptr);
256 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200257 ALOGE(
258 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
259 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800260 props.layerName, path_.c_str(), result);
261 instance_layers.resize(prev_num_instance_layers);
262 return false;
263 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800264 layer.instance_extensions.resize(count);
265 result = enumerate_instance_extensions(
266 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800267 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200268 ALOGE(
269 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
270 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800271 props.layerName, path_.c_str(), result);
272 instance_layers.resize(prev_num_instance_layers);
273 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800274 }
275
Chia-I Wu279ccc02016-04-18 16:45:15 +0800276 for (size_t j = 0; j < num_device_layers; j++) {
277 const auto& dev_props = properties[num_instance_layers + j];
278 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800279 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800280 break;
281 }
282 }
Jesse Hallaa410942016-01-17 13:07:10 -0800283
Chia-I Wubea09db2016-04-22 09:42:41 +0800284 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800285 result = enumerate_device_extensions(
286 VK_NULL_HANDLE, props.layerName, &count, nullptr);
287 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200288 ALOGE(
289 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800290 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800291 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800292 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800293 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800294 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800295 layer.device_extensions.resize(count);
296 result = enumerate_device_extensions(
297 VK_NULL_HANDLE, props.layerName, &count,
298 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800299 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200300 ALOGE(
301 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800302 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800303 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800304 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800305 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800306 }
307 }
308
Chia-I Wubea09db2016-04-22 09:42:41 +0800309 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200310 ALOGD("added %s layer '%s' from library '%s'",
311 (layer.is_global) ? "global" : "instance", props.layerName,
312 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800313 }
314
Chia-I Wu50174ee2016-04-18 16:33:20 +0800315 return true;
316}
Jesse Hall80523e22016-01-06 16:47:54 -0800317
Chia-I Wuba113272016-04-18 16:38:39 +0800318void* LayerLibrary::GetGPA(const Layer& layer,
319 const char* gpa_name,
320 size_t gpa_name_len) const {
321 void* gpa;
322 size_t layer_name_len =
323 std::max(size_t{2}, strlen(layer.properties.layerName));
324 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
325 strcpy(name, layer.properties.layerName);
326 strcpy(name + layer_name_len, gpa_name);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200327 if (!(gpa = GetTrampoline(name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800328 strcpy(name, "vk");
329 strcpy(name + 2, gpa_name);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200330 gpa = GetTrampoline(name);
Chia-I Wuba113272016-04-18 16:38:39 +0800331 }
332 return gpa;
333}
334
Jesse Hall1a7eb592016-05-01 21:04:40 +0200335// ----------------------------------------------------------------------------
336
Chia-I Wu50174ee2016-04-18 16:33:20 +0800337std::vector<LayerLibrary> g_layer_libraries;
338std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800339
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600340void AddLayerLibrary(const std::string& path, const std::string& filename) {
341 LayerLibrary library(path + "/" + filename, filename);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800342 if (!library.Open())
343 return;
344
Chia-I Wubea09db2016-04-22 09:42:41 +0800345 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800346 library.Close();
347 return;
348 }
349
350 library.Close();
351
352 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800353}
354
Jesse Hall1a7eb592016-05-01 21:04:40 +0200355template <typename Functor>
356void ForEachFileInDir(const std::string& dirname, Functor functor) {
357 auto dir_deleter = [](DIR* handle) { closedir(handle); };
358 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
359 dir_deleter);
360 if (!dir) {
361 // It's normal for some search directories to not exist, especially
362 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800363 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200364 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
365 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800366 return;
367 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200368 ALOGD("searching for layers in '%s'", dirname.c_str());
369 dirent* entry;
370 while ((entry = readdir(dir.get())) != nullptr)
371 functor(entry->d_name);
372}
Jesse Hall80523e22016-01-06 16:47:54 -0800373
Jesse Hall1a7eb592016-05-01 21:04:40 +0200374template <typename Functor>
375void ForEachFileInZip(const std::string& zipname,
376 const std::string& dir_in_zip,
377 Functor functor) {
378 int32_t err;
379 ZipArchiveHandle zip = nullptr;
380 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
381 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
382 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800383 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200384 std::string prefix(dir_in_zip + "/");
Jesse Hall1a7eb592016-05-01 21:04:40 +0200385 void* iter_cookie = nullptr;
Elliott Hughes54492042019-05-08 12:00:32 -0700386 if ((err = StartIteration(zip, &iter_cookie, prefix, "")) != 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200387 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
388 err);
389 CloseArchive(zip);
390 return;
391 }
392 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
393 dir_in_zip.c_str());
394 ZipEntry entry;
Elliott Hughesc1f24852019-05-22 19:47:17 -0700395 std::string name;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200396 while (Next(iter_cookie, &entry, &name) == 0) {
Elliott Hughesc1f24852019-05-22 19:47:17 -0700397 std::string filename(name.substr(prefix.length()));
Jesse Hall1a7eb592016-05-01 21:04:40 +0200398 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700399 if (filename.find('/') != filename.npos)
400 continue;
401 // Check whether it *may* be possible to load the library directly from
402 // the APK. Loading still may fail for other reasons, but this at least
403 // lets us avoid failed-to-load log messages in the typical case of
404 // compressed and/or unaligned libraries.
405 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
406 continue;
407 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200408 }
409 EndIteration(iter_cookie);
410 CloseArchive(zip);
411}
Jesse Hall80523e22016-01-06 16:47:54 -0800412
Jesse Hall1a7eb592016-05-01 21:04:40 +0200413template <typename Functor>
414void ForEachFileInPath(const std::string& path, Functor functor) {
415 size_t zip_pos = path.find("!/");
416 if (zip_pos == std::string::npos) {
417 ForEachFileInDir(path, functor);
418 } else {
419 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
420 functor);
421 }
422}
423
424void DiscoverLayersInPathList(const std::string& pathstr) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800425 ATRACE_CALL();
426
Jesse Hall1a7eb592016-05-01 21:04:40 +0200427 std::vector<std::string> paths = android::base::Split(pathstr, ":");
428 for (const auto& path : paths) {
429 ForEachFileInPath(path, [&](const std::string& filename) {
430 if (android::base::StartsWith(filename, "libVkLayer") &&
431 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600432
433 // Check to ensure we haven't seen this layer already
434 // Let the first instance of the shared object be enumerated
435 // We're searching for layers in following order:
436 // 1. system path
437 // 2. libraryPermittedPath (if enabled)
438 // 3. libraryPath
439
440 bool duplicate = false;
441 for (auto& layer : g_layer_libraries) {
442 if (layer.GetFilename() == filename) {
443 ALOGV("Skipping duplicate layer %s in %s",
444 filename.c_str(), path.c_str());
445 duplicate = true;
446 }
447 }
448
449 if (!duplicate)
450 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200451 }
452 });
453 }
Jesse Hall80523e22016-01-06 16:47:54 -0800454}
455
Chia-I Wudab25652016-04-28 07:15:51 +0800456const VkExtensionProperties* FindExtension(
457 const std::vector<VkExtensionProperties>& extensions,
458 const char* name) {
459 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
460 [=](const VkExtensionProperties& ext) {
461 return (strcmp(ext.extensionName, name) == 0);
462 });
463 return (it != extensions.cend()) ? &*it : nullptr;
464}
465
Jesse Hall80523e22016-01-06 16:47:54 -0800466void* GetLayerGetProcAddr(const Layer& layer,
467 const char* gpa_name,
468 size_t gpa_name_len) {
469 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800470 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800471}
472
Jesse Hallaa410942016-01-17 13:07:10 -0800473} // anonymous namespace
474
Jesse Hallaa410942016-01-17 13:07:10 -0800475void DiscoverLayers() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800476 ATRACE_CALL();
477
Jesse Hall1a7eb592016-05-01 21:04:40 +0200478 if (property_get_bool("ro.debuggable", false) &&
479 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
480 DiscoverLayersInPathList(kSystemLayerLibraryDir);
481 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600482 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
483 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800484}
485
Chia-I Wu25700b42016-04-28 06:36:09 +0800486uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800487 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800488}
489
Chia-I Wu25700b42016-04-28 06:36:09 +0800490const Layer& GetLayer(uint32_t index) {
491 return g_instance_layers[index];
492}
Chia-I Wubea09db2016-04-22 09:42:41 +0800493
Chia-I Wu04c65512016-04-27 09:54:02 +0800494const Layer* FindLayer(const char* name) {
495 auto layer =
496 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
497 [=](const Layer& entry) {
498 return strcmp(entry.properties.layerName, name) == 0;
499 });
500 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
501}
502
Chia-I Wu25700b42016-04-28 06:36:09 +0800503const VkLayerProperties& GetLayerProperties(const Layer& layer) {
504 return layer.properties;
505}
Chia-I Wubea09db2016-04-22 09:42:41 +0800506
Chia-I Wu25700b42016-04-28 06:36:09 +0800507bool IsLayerGlobal(const Layer& layer) {
508 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800509}
510
Chia-I Wu04c65512016-04-27 09:54:02 +0800511const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
512 uint32_t& count) {
513 count = static_cast<uint32_t>(layer.instance_extensions.size());
514 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800515}
516
Chia-I Wu04c65512016-04-27 09:54:02 +0800517const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
518 uint32_t& count) {
519 count = static_cast<uint32_t>(layer.device_extensions.size());
520 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800521}
522
Chia-I Wudab25652016-04-28 07:15:51 +0800523const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
524 const char* name) {
525 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800526}
527
Chia-I Wudab25652016-04-28 07:15:51 +0800528const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
529 const char* name) {
530 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800531}
532
Chia-I Wudab25652016-04-28 07:15:51 +0800533LayerRef GetLayerRef(const Layer& layer) {
534 LayerLibrary& library = g_layer_libraries[layer.library_idx];
535 return LayerRef((library.Open()) ? &layer : nullptr);
536}
537
538LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800539
540LayerRef::~LayerRef() {
541 if (layer_) {
542 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800543 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800544 }
545}
546
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -0700547LayerRef::LayerRef(LayerRef&& other) noexcept : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600548 other.layer_ = nullptr;
549}
Jesse Hall80523e22016-01-06 16:47:54 -0800550
551PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
552 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
553 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
554 : nullptr;
555}
556
557PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
558 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
559 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
560 : nullptr;
561}
562
Chia-I Wuc96880f2016-03-26 06:56:45 +0800563} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800564} // namespace vulkan