blob: 567941273297dcaac8f8282de95f28a2118a4ade [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
Jesse Hallb1471272016-01-17 21:36:58 -080041// TODO(jessehall): The whole way we deal with extensions is pretty hokey, and
42// not a good long-term solution. Having a hard-coded enum of extensions is
43// bad, of course. Representing sets of extensions (requested, supported, etc.)
44// as a bitset isn't necessarily bad, if the mapping from extension to bit were
45// dynamic. Need to rethink this completely when there's a little more time.
46
Jesse Hallaa410942016-01-17 13:07:10 -080047// TODO(jessehall): This file currently builds up global data structures as it
48// loads, and never cleans them up. This means we're doing heap allocations
49// without going through an app-provided allocator, but worse, we'll leak those
50// allocations if the loader is unloaded.
51//
52// We should allocate "enough" BSS space, and suballocate from there. Will
53// probably want to intern strings, etc., and will need some custom/manual data
54// structures.
55
Jesse Hall80523e22016-01-06 16:47:54 -080056namespace vulkan {
Chia-I Wuc96880f2016-03-26 06:56:45 +080057namespace api {
58
Jesse Hall80523e22016-01-06 16:47:54 -080059struct Layer {
60 VkLayerProperties properties;
61 size_t library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +080062
Chia-I Wu25700b42016-04-28 06:36:09 +080063 // true if the layer intercepts vkCreateDevice and device commands
Chia-I Wubea09db2016-04-22 09:42:41 +080064 bool is_global;
65
66 std::vector<VkExtensionProperties> instance_extensions;
67 std::vector<VkExtensionProperties> device_extensions;
Jesse Hall80523e22016-01-06 16:47:54 -080068};
Jesse Hall80523e22016-01-06 16:47:54 -080069
70namespace {
71
Jesse Hall1a7eb592016-05-01 21:04:40 +020072const char kSystemLayerLibraryDir[] = "/data/local/debug/vulkan";
73
Chia-I Wu6693f5c2016-04-18 12:20:02 +080074class LayerLibrary {
75 public:
Cody Northropd2aa3ab2017-10-20 09:01:53 -060076 explicit LayerLibrary(const std::string& path,
77 const std::string& filename)
78 : path_(path),
79 filename_(filename),
80 dlhandle_(nullptr),
Victor Khimenkobbf77002018-08-16 22:38:36 +020081 native_bridge_(false),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060082 refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080083
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -070084 LayerLibrary(LayerLibrary&& other) noexcept
Chia-I Wu6693f5c2016-04-18 12:20:02 +080085 : path_(std::move(other.path_)),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060086 filename_(std::move(other.filename_)),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080087 dlhandle_(other.dlhandle_),
Victor Khimenkobbf77002018-08-16 22:38:36 +020088 native_bridge_(other.native_bridge_),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080089 refcount_(other.refcount_) {
90 other.dlhandle_ = nullptr;
91 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080092 }
93
94 LayerLibrary(const LayerLibrary&) = delete;
95 LayerLibrary& operator=(const LayerLibrary&) = delete;
96
Chia-I Wua6229742016-04-26 07:37:44 +080097 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080098 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080099 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +0800100
Chia-I Wu50174ee2016-04-18 16:33:20 +0800101 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800102 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800103
Chia-I Wuba113272016-04-18 16:38:39 +0800104 void* GetGPA(const Layer& layer,
105 const char* gpa_name,
106 size_t gpa_name_len) const;
107
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600108 const std::string GetFilename() { return filename_; }
109
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800110 private:
Victor Khimenkobbf77002018-08-16 22:38:36 +0200111 // TODO(b/79940628): remove that adapter when we could use NativeBridgeGetTrampoline
112 // for native libraries.
113 template<typename Func = void*>
114 Func GetTrampoline(const char* name) const {
115 if (native_bridge_) {
116 return reinterpret_cast<Func>(android::NativeBridgeGetTrampoline(
117 dlhandle_, name, nullptr, 0));
118 }
119 return reinterpret_cast<Func>(dlsym(dlhandle_, name));
120 }
121
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800122 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +0800123
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600124 // Track the filename alone so we can detect duplicates
125 const std::string filename_;
126
Chia-I Wua6229742016-04-26 07:37:44 +0800127 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800128 void* dlhandle_;
Victor Khimenkobbf77002018-08-16 22:38:36 +0200129 bool native_bridge_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800130 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800131};
Chia-I Wu74349592016-04-18 12:08:39 +0800132
Chia-I Wufd0389f2016-04-18 12:11:00 +0800133bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800134 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800135 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200136 ALOGV("opening layer library '%s'", path_.c_str());
Jesse Hall40c07a12016-05-11 22:56:29 -0700137 // Libraries in the system layer library dir can't be loaded into
138 // the application namespace. That causes compatibility problems, since
139 // any symbol dependencies will be resolved by system libraries. They
140 // can't safely use libc++_shared, for example. Which is one reason
141 // (among several) we only allow them in non-user builds.
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600142 auto app_namespace = android::GraphicsEnv::getInstance().getAppNamespace();
Jesse Hall40c07a12016-05-11 22:56:29 -0700143 if (app_namespace &&
144 !android::base::StartsWith(path_, kSystemLayerLibraryDir)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000145 char* error_msg = nullptr;
146 dlhandle_ = OpenNativeLibraryInNamespace(
Victor Khimenkobbf77002018-08-16 22:38:36 +0200147 app_namespace, path_.c_str(), &native_bridge_, &error_msg);
148 if (!dlhandle_) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000149 ALOGE("failed to load layer library '%s': %s", path_.c_str(), error_msg);
150 android::NativeLoaderFreeErrorMessage(error_msg);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200151 refcount_ = 0;
152 return false;
153 }
Jesse Hall40c07a12016-05-11 22:56:29 -0700154 } else {
Victor Khimenkobbf77002018-08-16 22:38:36 +0200155 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
156 if (!dlhandle_) {
157 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
158 dlerror());
159 refcount_ = 0;
160 return false;
161 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800162 }
163 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800164 return true;
165}
166
Chia-I Wud91c74f2016-04-18 12:12:36 +0800167void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800168 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800169 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200170 ALOGV("closing layer library '%s'", path_.c_str());
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000171 char* error_msg = nullptr;
Jacky.Deng01728c12018-08-28 15:25:31 +0800172 if (!android::CloseNativeLibrary(dlhandle_, native_bridge_, &error_msg)) {
Nicolas Geoffraya655dac2019-01-11 15:59:42 +0000173 ALOGE("failed to unload library '%s': %s", path_.c_str(), error_msg);
174 android::NativeLoaderFreeErrorMessage(error_msg);
Jacky.Deng01728c12018-08-28 15:25:31 +0800175 refcount_++;
176 } else {
177 dlhandle_ = nullptr;
178 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800179 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800180}
181
Chia-I Wu50174ee2016-04-18 16:33:20 +0800182bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800183 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800184 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200185 GetTrampoline<PFN_vkEnumerateInstanceLayerProperties>(
186 "vkEnumerateInstanceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800187 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200188 GetTrampoline<PFN_vkEnumerateInstanceExtensionProperties>(
189 "vkEnumerateInstanceExtensionProperties");
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800190 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200191 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800192 path_.c_str());
193 return false;
194 }
195
196 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800197 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200198 GetTrampoline<PFN_vkEnumerateDeviceLayerProperties>(
199 "vkEnumerateDeviceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800200 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200201 GetTrampoline<PFN_vkEnumerateDeviceExtensionProperties>(
202 "vkEnumerateDeviceExtensionProperties");
Jesse Hall80523e22016-01-06 16:47:54 -0800203
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800204 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800205 uint32_t num_instance_layers = 0;
206 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800207 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
208 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800209 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200210 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800211 "vkEnumerateInstanceLayerProperties failed for library '%s': "
212 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800213 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800214 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800215 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800216 }
Jesse Hallaa410942016-01-17 13:07:10 -0800217 if (enumerate_device_layers) {
218 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
219 nullptr);
220 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200221 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800222 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800223 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800224 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800225 }
226 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800227
228 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800229 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
230 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800231 result = enumerate_instance_layers(&num_instance_layers, properties);
232 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200233 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800234 path_.c_str(), result);
235 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800236 }
237 if (num_device_layers > 0) {
238 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
239 properties + num_instance_layers);
240 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200241 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800242 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800243 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800244 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800245 }
Jesse Hall80523e22016-01-06 16:47:54 -0800246 }
247
Chia-I Wubea09db2016-04-22 09:42:41 +0800248 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800249 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800250 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800251 for (size_t i = 0; i < num_instance_layers; i++) {
252 const VkLayerProperties& props = properties[i];
253
Jesse Hall80523e22016-01-06 16:47:54 -0800254 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800255 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800256 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800257 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800258
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800259 uint32_t count = 0;
260 result =
261 enumerate_instance_extensions(props.layerName, &count, nullptr);
262 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200263 ALOGE(
264 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
265 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800266 props.layerName, path_.c_str(), result);
267 instance_layers.resize(prev_num_instance_layers);
268 return false;
269 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800270 layer.instance_extensions.resize(count);
271 result = enumerate_instance_extensions(
272 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800273 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200274 ALOGE(
275 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
276 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800277 props.layerName, path_.c_str(), result);
278 instance_layers.resize(prev_num_instance_layers);
279 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800280 }
281
Chia-I Wu279ccc02016-04-18 16:45:15 +0800282 for (size_t j = 0; j < num_device_layers; j++) {
283 const auto& dev_props = properties[num_instance_layers + j];
284 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800285 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800286 break;
287 }
288 }
Jesse Hallaa410942016-01-17 13:07:10 -0800289
Chia-I Wubea09db2016-04-22 09:42:41 +0800290 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800291 result = enumerate_device_extensions(
292 VK_NULL_HANDLE, props.layerName, &count, nullptr);
293 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200294 ALOGE(
295 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800296 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800297 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800298 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800299 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800300 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800301 layer.device_extensions.resize(count);
302 result = enumerate_device_extensions(
303 VK_NULL_HANDLE, props.layerName, &count,
304 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800305 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200306 ALOGE(
307 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800308 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800309 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800310 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800311 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800312 }
313 }
314
Chia-I Wubea09db2016-04-22 09:42:41 +0800315 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200316 ALOGD("added %s layer '%s' from library '%s'",
317 (layer.is_global) ? "global" : "instance", props.layerName,
318 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800319 }
320
Chia-I Wu50174ee2016-04-18 16:33:20 +0800321 return true;
322}
Jesse Hall80523e22016-01-06 16:47:54 -0800323
Chia-I Wuba113272016-04-18 16:38:39 +0800324void* LayerLibrary::GetGPA(const Layer& layer,
325 const char* gpa_name,
326 size_t gpa_name_len) const {
327 void* gpa;
328 size_t layer_name_len =
329 std::max(size_t{2}, strlen(layer.properties.layerName));
330 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
331 strcpy(name, layer.properties.layerName);
332 strcpy(name + layer_name_len, gpa_name);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200333 if (!(gpa = GetTrampoline(name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800334 strcpy(name, "vk");
335 strcpy(name + 2, gpa_name);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200336 gpa = GetTrampoline(name);
Chia-I Wuba113272016-04-18 16:38:39 +0800337 }
338 return gpa;
339}
340
Jesse Hall1a7eb592016-05-01 21:04:40 +0200341// ----------------------------------------------------------------------------
342
Chia-I Wu50174ee2016-04-18 16:33:20 +0800343std::vector<LayerLibrary> g_layer_libraries;
344std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800345
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600346void AddLayerLibrary(const std::string& path, const std::string& filename) {
347 LayerLibrary library(path + "/" + filename, filename);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800348 if (!library.Open())
349 return;
350
Chia-I Wubea09db2016-04-22 09:42:41 +0800351 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800352 library.Close();
353 return;
354 }
355
356 library.Close();
357
358 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800359}
360
Jesse Hall1a7eb592016-05-01 21:04:40 +0200361template <typename Functor>
362void ForEachFileInDir(const std::string& dirname, Functor functor) {
363 auto dir_deleter = [](DIR* handle) { closedir(handle); };
364 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
365 dir_deleter);
366 if (!dir) {
367 // It's normal for some search directories to not exist, especially
368 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800369 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200370 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
371 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800372 return;
373 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200374 ALOGD("searching for layers in '%s'", dirname.c_str());
375 dirent* entry;
376 while ((entry = readdir(dir.get())) != nullptr)
377 functor(entry->d_name);
378}
Jesse Hall80523e22016-01-06 16:47:54 -0800379
Jesse Hall1a7eb592016-05-01 21:04:40 +0200380template <typename Functor>
381void ForEachFileInZip(const std::string& zipname,
382 const std::string& dir_in_zip,
383 Functor functor) {
384 int32_t err;
385 ZipArchiveHandle zip = nullptr;
386 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
387 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
388 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800389 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200390 std::string prefix(dir_in_zip + "/");
Jesse Hall1a7eb592016-05-01 21:04:40 +0200391 void* iter_cookie = nullptr;
Elliott Hughes54492042019-05-08 12:00:32 -0700392 if ((err = StartIteration(zip, &iter_cookie, prefix, "")) != 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200393 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
394 err);
395 CloseArchive(zip);
396 return;
397 }
398 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
399 dir_in_zip.c_str());
400 ZipEntry entry;
Elliott Hughesc1f24852019-05-22 19:47:17 -0700401 std::string name;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200402 while (Next(iter_cookie, &entry, &name) == 0) {
Elliott Hughesc1f24852019-05-22 19:47:17 -0700403 std::string filename(name.substr(prefix.length()));
Jesse Hall1a7eb592016-05-01 21:04:40 +0200404 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700405 if (filename.find('/') != filename.npos)
406 continue;
407 // Check whether it *may* be possible to load the library directly from
408 // the APK. Loading still may fail for other reasons, but this at least
409 // lets us avoid failed-to-load log messages in the typical case of
410 // compressed and/or unaligned libraries.
411 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
412 continue;
413 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200414 }
415 EndIteration(iter_cookie);
416 CloseArchive(zip);
417}
Jesse Hall80523e22016-01-06 16:47:54 -0800418
Jesse Hall1a7eb592016-05-01 21:04:40 +0200419template <typename Functor>
420void ForEachFileInPath(const std::string& path, Functor functor) {
421 size_t zip_pos = path.find("!/");
422 if (zip_pos == std::string::npos) {
423 ForEachFileInDir(path, functor);
424 } else {
425 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
426 functor);
427 }
428}
429
430void DiscoverLayersInPathList(const std::string& pathstr) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800431 ATRACE_CALL();
432
Jesse Hall1a7eb592016-05-01 21:04:40 +0200433 std::vector<std::string> paths = android::base::Split(pathstr, ":");
434 for (const auto& path : paths) {
435 ForEachFileInPath(path, [&](const std::string& filename) {
436 if (android::base::StartsWith(filename, "libVkLayer") &&
437 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600438
439 // Check to ensure we haven't seen this layer already
440 // Let the first instance of the shared object be enumerated
441 // We're searching for layers in following order:
442 // 1. system path
443 // 2. libraryPermittedPath (if enabled)
444 // 3. libraryPath
445
446 bool duplicate = false;
447 for (auto& layer : g_layer_libraries) {
448 if (layer.GetFilename() == filename) {
449 ALOGV("Skipping duplicate layer %s in %s",
450 filename.c_str(), path.c_str());
451 duplicate = true;
452 }
453 }
454
455 if (!duplicate)
456 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200457 }
458 });
459 }
Jesse Hall80523e22016-01-06 16:47:54 -0800460}
461
Chia-I Wudab25652016-04-28 07:15:51 +0800462const VkExtensionProperties* FindExtension(
463 const std::vector<VkExtensionProperties>& extensions,
464 const char* name) {
465 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
466 [=](const VkExtensionProperties& ext) {
467 return (strcmp(ext.extensionName, name) == 0);
468 });
469 return (it != extensions.cend()) ? &*it : nullptr;
470}
471
Jesse Hall80523e22016-01-06 16:47:54 -0800472void* GetLayerGetProcAddr(const Layer& layer,
473 const char* gpa_name,
474 size_t gpa_name_len) {
475 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800476 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800477}
478
Jesse Hallaa410942016-01-17 13:07:10 -0800479} // anonymous namespace
480
Jesse Hallaa410942016-01-17 13:07:10 -0800481void DiscoverLayers() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800482 ATRACE_CALL();
483
Jesse Hall1a7eb592016-05-01 21:04:40 +0200484 if (property_get_bool("ro.debuggable", false) &&
485 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
486 DiscoverLayersInPathList(kSystemLayerLibraryDir);
487 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600488 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
489 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800490}
491
Chia-I Wu25700b42016-04-28 06:36:09 +0800492uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800493 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800494}
495
Chia-I Wu25700b42016-04-28 06:36:09 +0800496const Layer& GetLayer(uint32_t index) {
497 return g_instance_layers[index];
498}
Chia-I Wubea09db2016-04-22 09:42:41 +0800499
Chia-I Wu04c65512016-04-27 09:54:02 +0800500const Layer* FindLayer(const char* name) {
501 auto layer =
502 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
503 [=](const Layer& entry) {
504 return strcmp(entry.properties.layerName, name) == 0;
505 });
506 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
507}
508
Chia-I Wu25700b42016-04-28 06:36:09 +0800509const VkLayerProperties& GetLayerProperties(const Layer& layer) {
510 return layer.properties;
511}
Chia-I Wubea09db2016-04-22 09:42:41 +0800512
Chia-I Wu25700b42016-04-28 06:36:09 +0800513bool IsLayerGlobal(const Layer& layer) {
514 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800515}
516
Chia-I Wu04c65512016-04-27 09:54:02 +0800517const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
518 uint32_t& count) {
519 count = static_cast<uint32_t>(layer.instance_extensions.size());
520 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800521}
522
Chia-I Wu04c65512016-04-27 09:54:02 +0800523const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
524 uint32_t& count) {
525 count = static_cast<uint32_t>(layer.device_extensions.size());
526 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800527}
528
Chia-I Wudab25652016-04-28 07:15:51 +0800529const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
530 const char* name) {
531 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800532}
533
Chia-I Wudab25652016-04-28 07:15:51 +0800534const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
535 const char* name) {
536 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800537}
538
Chia-I Wudab25652016-04-28 07:15:51 +0800539LayerRef GetLayerRef(const Layer& layer) {
540 LayerLibrary& library = g_layer_libraries[layer.library_idx];
541 return LayerRef((library.Open()) ? &layer : nullptr);
542}
543
544LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800545
546LayerRef::~LayerRef() {
547 if (layer_) {
548 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800549 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800550 }
551}
552
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -0700553LayerRef::LayerRef(LayerRef&& other) noexcept : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600554 other.layer_ = nullptr;
555}
Jesse Hall80523e22016-01-06 16:47:54 -0800556
557PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
558 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
559 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
560 : nullptr;
561}
562
563PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
564 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
565 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
566 : nullptr;
567}
568
Chia-I Wuc96880f2016-03-26 06:56:45 +0800569} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800570} // namespace vulkan