blob: a7b7a6f71ab1c1f65673dd2e9e64495f2e646cd9 [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
Chia-I Wuc96880f2016-03-26 06:56:45 +080017#include "layers_extensions.h"
Jesse Hall1a7eb592016-05-01 21:04:40 +020018
Jesse Hall80523e22016-01-06 16:47:54 -080019#include <alloca.h>
20#include <dirent.h>
21#include <dlfcn.h>
Jesse Hall80523e22016-01-06 16:47:54 -080022#include <string.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070023#include <sys/prctl.h>
24
25#include <mutex>
26#include <string>
Jesse Hall80523e22016-01-06 16:47:54 -080027#include <vector>
Jesse Hall1a7eb592016-05-01 21:04:40 +020028
Jesse Hall40c07a12016-05-11 22:56:29 -070029#include <android/dlext.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070030#include <android-base/strings.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020031#include <cutils/properties.h>
Cody Northropd2aa3ab2017-10-20 09:01:53 -060032#include <graphicsenv/GraphicsEnv.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070033#include <log/log.h>
Victor Khimenkobbf77002018-08-16 22:38:36 +020034#include <nativebridge/native_bridge.h>
35#include <nativeloader/native_loader.h>
Jesse Hall1a7eb592016-05-01 21:04:40 +020036#include <ziparchive/zip_archive.h>
37
Jesse Hallb1471272016-01-17 21:36:58 -080038// TODO(jessehall): The whole way we deal with extensions is pretty hokey, and
39// not a good long-term solution. Having a hard-coded enum of extensions is
40// bad, of course. Representing sets of extensions (requested, supported, etc.)
41// as a bitset isn't necessarily bad, if the mapping from extension to bit were
42// dynamic. Need to rethink this completely when there's a little more time.
43
Jesse Hallaa410942016-01-17 13:07:10 -080044// TODO(jessehall): This file currently builds up global data structures as it
45// loads, and never cleans them up. This means we're doing heap allocations
46// without going through an app-provided allocator, but worse, we'll leak those
47// allocations if the loader is unloaded.
48//
49// We should allocate "enough" BSS space, and suballocate from there. Will
50// probably want to intern strings, etc., and will need some custom/manual data
51// structures.
52
Jesse Hall80523e22016-01-06 16:47:54 -080053namespace vulkan {
Chia-I Wuc96880f2016-03-26 06:56:45 +080054namespace api {
55
Jesse Hall80523e22016-01-06 16:47:54 -080056struct Layer {
57 VkLayerProperties properties;
58 size_t library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +080059
Chia-I Wu25700b42016-04-28 06:36:09 +080060 // true if the layer intercepts vkCreateDevice and device commands
Chia-I Wubea09db2016-04-22 09:42:41 +080061 bool is_global;
62
63 std::vector<VkExtensionProperties> instance_extensions;
64 std::vector<VkExtensionProperties> device_extensions;
Jesse Hall80523e22016-01-06 16:47:54 -080065};
Jesse Hall80523e22016-01-06 16:47:54 -080066
67namespace {
68
Jesse Hall1a7eb592016-05-01 21:04:40 +020069const char kSystemLayerLibraryDir[] = "/data/local/debug/vulkan";
70
Chia-I Wu6693f5c2016-04-18 12:20:02 +080071class LayerLibrary {
72 public:
Cody Northropd2aa3ab2017-10-20 09:01:53 -060073 explicit LayerLibrary(const std::string& path,
74 const std::string& filename)
75 : path_(path),
76 filename_(filename),
77 dlhandle_(nullptr),
Victor Khimenkobbf77002018-08-16 22:38:36 +020078 native_bridge_(false),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060079 refcount_(0) {}
Chia-I Wu74349592016-04-18 12:08:39 +080080
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -070081 LayerLibrary(LayerLibrary&& other) noexcept
Chia-I Wu6693f5c2016-04-18 12:20:02 +080082 : path_(std::move(other.path_)),
Cody Northropd2aa3ab2017-10-20 09:01:53 -060083 filename_(std::move(other.filename_)),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080084 dlhandle_(other.dlhandle_),
Victor Khimenkobbf77002018-08-16 22:38:36 +020085 native_bridge_(other.native_bridge_),
Chia-I Wu6693f5c2016-04-18 12:20:02 +080086 refcount_(other.refcount_) {
87 other.dlhandle_ = nullptr;
88 other.refcount_ = 0;
Chia-I Wu50174ee2016-04-18 16:33:20 +080089 }
90
91 LayerLibrary(const LayerLibrary&) = delete;
92 LayerLibrary& operator=(const LayerLibrary&) = delete;
93
Chia-I Wua6229742016-04-26 07:37:44 +080094 // these are thread-safe
Chia-I Wufd0389f2016-04-18 12:11:00 +080095 bool Open();
Chia-I Wud91c74f2016-04-18 12:12:36 +080096 void Close();
Chia-I Wufd0389f2016-04-18 12:11:00 +080097
Chia-I Wu50174ee2016-04-18 16:33:20 +080098 bool EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +080099 std::vector<Layer>& instance_layers) const;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800100
Chia-I Wuba113272016-04-18 16:38:39 +0800101 void* GetGPA(const Layer& layer,
102 const char* gpa_name,
103 size_t gpa_name_len) const;
104
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600105 const std::string GetFilename() { return filename_; }
106
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800107 private:
Victor Khimenkobbf77002018-08-16 22:38:36 +0200108 // TODO(b/79940628): remove that adapter when we could use NativeBridgeGetTrampoline
109 // for native libraries.
110 template<typename Func = void*>
111 Func GetTrampoline(const char* name) const {
112 if (native_bridge_) {
113 return reinterpret_cast<Func>(android::NativeBridgeGetTrampoline(
114 dlhandle_, name, nullptr, 0));
115 }
116 return reinterpret_cast<Func>(dlsym(dlhandle_, name));
117 }
118
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800119 const std::string path_;
Chia-I Wua6229742016-04-26 07:37:44 +0800120
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600121 // Track the filename alone so we can detect duplicates
122 const std::string filename_;
123
Chia-I Wua6229742016-04-26 07:37:44 +0800124 std::mutex mutex_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800125 void* dlhandle_;
Victor Khimenkobbf77002018-08-16 22:38:36 +0200126 bool native_bridge_;
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800127 size_t refcount_;
Jesse Hall80523e22016-01-06 16:47:54 -0800128};
Chia-I Wu74349592016-04-18 12:08:39 +0800129
Chia-I Wufd0389f2016-04-18 12:11:00 +0800130bool LayerLibrary::Open() {
Chia-I Wua6229742016-04-26 07:37:44 +0800131 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800132 if (refcount_++ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200133 ALOGV("opening layer library '%s'", path_.c_str());
Jesse Hall40c07a12016-05-11 22:56:29 -0700134 // Libraries in the system layer library dir can't be loaded into
135 // the application namespace. That causes compatibility problems, since
136 // any symbol dependencies will be resolved by system libraries. They
137 // can't safely use libc++_shared, for example. Which is one reason
138 // (among several) we only allow them in non-user builds.
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600139 auto app_namespace = android::GraphicsEnv::getInstance().getAppNamespace();
Jesse Hall40c07a12016-05-11 22:56:29 -0700140 if (app_namespace &&
141 !android::base::StartsWith(path_, kSystemLayerLibraryDir)) {
Nicolas Geoffray26856ac2019-01-11 15:59:42 +0000142 char* error_msg = nullptr;
143 dlhandle_ = OpenNativeLibraryInNamespace(
Victor Khimenkobbf77002018-08-16 22:38:36 +0200144 app_namespace, path_.c_str(), &native_bridge_, &error_msg);
145 if (!dlhandle_) {
Nicolas Geoffray26856ac2019-01-11 15:59:42 +0000146 ALOGE("failed to load layer library '%s': %s", path_.c_str(), error_msg);
147 android::NativeLoaderFreeErrorMessage(error_msg);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200148 refcount_ = 0;
149 return false;
150 }
Jesse Hall40c07a12016-05-11 22:56:29 -0700151 } else {
Victor Khimenkobbf77002018-08-16 22:38:36 +0200152 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL);
153 if (!dlhandle_) {
154 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
155 dlerror());
156 refcount_ = 0;
157 return false;
158 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800159 }
160 }
Chia-I Wufd0389f2016-04-18 12:11:00 +0800161 return true;
162}
163
Chia-I Wud91c74f2016-04-18 12:12:36 +0800164void LayerLibrary::Close() {
Chia-I Wua6229742016-04-26 07:37:44 +0800165 std::lock_guard<std::mutex> lock(mutex_);
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800166 if (--refcount_ == 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200167 ALOGV("closing layer library '%s'", path_.c_str());
Nicolas Geoffray26856ac2019-01-11 15:59:42 +0000168 char* error_msg = nullptr;
Jacky.Deng01728c12018-08-28 15:25:31 +0800169 if (!android::CloseNativeLibrary(dlhandle_, native_bridge_, &error_msg)) {
Nicolas Geoffray26856ac2019-01-11 15:59:42 +0000170 ALOGE("failed to unload library '%s': %s", path_.c_str(), error_msg);
171 android::NativeLoaderFreeErrorMessage(error_msg);
Jacky.Deng01728c12018-08-28 15:25:31 +0800172 refcount_++;
173 } else {
174 dlhandle_ = nullptr;
175 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800176 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800177}
178
Chia-I Wu50174ee2016-04-18 16:33:20 +0800179bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800180 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800181 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200182 GetTrampoline<PFN_vkEnumerateInstanceLayerProperties>(
183 "vkEnumerateInstanceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800184 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200185 GetTrampoline<PFN_vkEnumerateInstanceExtensionProperties>(
186 "vkEnumerateInstanceExtensionProperties");
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800187 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200188 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800189 path_.c_str());
190 return false;
191 }
192
193 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800194 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200195 GetTrampoline<PFN_vkEnumerateDeviceLayerProperties>(
196 "vkEnumerateDeviceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800197 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200198 GetTrampoline<PFN_vkEnumerateDeviceExtensionProperties>(
199 "vkEnumerateDeviceExtensionProperties");
Jesse Hall80523e22016-01-06 16:47:54 -0800200
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800201 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800202 uint32_t num_instance_layers = 0;
203 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800204 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
205 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800206 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200207 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800208 "vkEnumerateInstanceLayerProperties failed for library '%s': "
209 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800210 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800211 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800212 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800213 }
Jesse Hallaa410942016-01-17 13:07:10 -0800214 if (enumerate_device_layers) {
215 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
216 nullptr);
217 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200218 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800219 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800220 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800221 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800222 }
223 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800224
225 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800226 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
227 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800228 result = enumerate_instance_layers(&num_instance_layers, properties);
229 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200230 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800231 path_.c_str(), result);
232 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800233 }
234 if (num_device_layers > 0) {
235 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
236 properties + num_instance_layers);
237 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200238 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800239 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800240 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800241 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800242 }
Jesse Hall80523e22016-01-06 16:47:54 -0800243 }
244
Chia-I Wubea09db2016-04-22 09:42:41 +0800245 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800246 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800247 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800248 for (size_t i = 0; i < num_instance_layers; i++) {
249 const VkLayerProperties& props = properties[i];
250
Jesse Hall80523e22016-01-06 16:47:54 -0800251 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800252 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800253 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800254 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800255
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800256 uint32_t count = 0;
257 result =
258 enumerate_instance_extensions(props.layerName, &count, nullptr);
259 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200260 ALOGE(
261 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
262 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800263 props.layerName, path_.c_str(), result);
264 instance_layers.resize(prev_num_instance_layers);
265 return false;
266 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800267 layer.instance_extensions.resize(count);
268 result = enumerate_instance_extensions(
269 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800270 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200271 ALOGE(
272 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
273 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800274 props.layerName, path_.c_str(), result);
275 instance_layers.resize(prev_num_instance_layers);
276 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800277 }
278
Chia-I Wu279ccc02016-04-18 16:45:15 +0800279 for (size_t j = 0; j < num_device_layers; j++) {
280 const auto& dev_props = properties[num_instance_layers + j];
281 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800282 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800283 break;
284 }
285 }
Jesse Hallaa410942016-01-17 13:07:10 -0800286
Chia-I Wubea09db2016-04-22 09:42:41 +0800287 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800288 result = enumerate_device_extensions(
289 VK_NULL_HANDLE, props.layerName, &count, nullptr);
290 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200291 ALOGE(
292 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800293 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800294 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800295 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800296 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800297 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800298 layer.device_extensions.resize(count);
299 result = enumerate_device_extensions(
300 VK_NULL_HANDLE, props.layerName, &count,
301 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800302 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200303 ALOGE(
304 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800305 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800306 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800307 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800308 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800309 }
310 }
311
Chia-I Wubea09db2016-04-22 09:42:41 +0800312 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200313 ALOGD("added %s layer '%s' from library '%s'",
314 (layer.is_global) ? "global" : "instance", props.layerName,
315 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800316 }
317
Chia-I Wu50174ee2016-04-18 16:33:20 +0800318 return true;
319}
Jesse Hall80523e22016-01-06 16:47:54 -0800320
Chia-I Wuba113272016-04-18 16:38:39 +0800321void* LayerLibrary::GetGPA(const Layer& layer,
322 const char* gpa_name,
323 size_t gpa_name_len) const {
324 void* gpa;
325 size_t layer_name_len =
326 std::max(size_t{2}, strlen(layer.properties.layerName));
327 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
328 strcpy(name, layer.properties.layerName);
329 strcpy(name + layer_name_len, gpa_name);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200330 if (!(gpa = GetTrampoline(name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800331 strcpy(name, "vk");
332 strcpy(name + 2, gpa_name);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200333 gpa = GetTrampoline(name);
Chia-I Wuba113272016-04-18 16:38:39 +0800334 }
335 return gpa;
336}
337
Jesse Hall1a7eb592016-05-01 21:04:40 +0200338// ----------------------------------------------------------------------------
339
Chia-I Wu50174ee2016-04-18 16:33:20 +0800340std::vector<LayerLibrary> g_layer_libraries;
341std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800342
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600343void AddLayerLibrary(const std::string& path, const std::string& filename) {
344 LayerLibrary library(path + "/" + filename, filename);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800345 if (!library.Open())
346 return;
347
Chia-I Wubea09db2016-04-22 09:42:41 +0800348 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800349 library.Close();
350 return;
351 }
352
353 library.Close();
354
355 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800356}
357
Jesse Hall1a7eb592016-05-01 21:04:40 +0200358template <typename Functor>
359void ForEachFileInDir(const std::string& dirname, Functor functor) {
360 auto dir_deleter = [](DIR* handle) { closedir(handle); };
361 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
362 dir_deleter);
363 if (!dir) {
364 // It's normal for some search directories to not exist, especially
365 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800366 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200367 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
368 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800369 return;
370 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200371 ALOGD("searching for layers in '%s'", dirname.c_str());
372 dirent* entry;
373 while ((entry = readdir(dir.get())) != nullptr)
374 functor(entry->d_name);
375}
Jesse Hall80523e22016-01-06 16:47:54 -0800376
Jesse Hall1a7eb592016-05-01 21:04:40 +0200377template <typename Functor>
378void ForEachFileInZip(const std::string& zipname,
379 const std::string& dir_in_zip,
380 Functor functor) {
381 int32_t err;
382 ZipArchiveHandle zip = nullptr;
383 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
384 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
385 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800386 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200387 std::string prefix(dir_in_zip + "/");
Jesse Hall1a7eb592016-05-01 21:04:40 +0200388 void* iter_cookie = nullptr;
Elliott Hughes54492042019-05-08 12:00:32 -0700389 if ((err = StartIteration(zip, &iter_cookie, prefix, "")) != 0) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200390 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
391 err);
392 CloseArchive(zip);
393 return;
394 }
395 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
396 dir_in_zip.c_str());
397 ZipEntry entry;
Elliott Hughesc1f24852019-05-22 19:47:17 -0700398 std::string name;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200399 while (Next(iter_cookie, &entry, &name) == 0) {
Elliott Hughesc1f24852019-05-22 19:47:17 -0700400 std::string filename(name.substr(prefix.length()));
Jesse Hall1a7eb592016-05-01 21:04:40 +0200401 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700402 if (filename.find('/') != filename.npos)
403 continue;
404 // Check whether it *may* be possible to load the library directly from
405 // the APK. Loading still may fail for other reasons, but this at least
406 // lets us avoid failed-to-load log messages in the typical case of
407 // compressed and/or unaligned libraries.
408 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
409 continue;
410 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200411 }
412 EndIteration(iter_cookie);
413 CloseArchive(zip);
414}
Jesse Hall80523e22016-01-06 16:47:54 -0800415
Jesse Hall1a7eb592016-05-01 21:04:40 +0200416template <typename Functor>
417void ForEachFileInPath(const std::string& path, Functor functor) {
418 size_t zip_pos = path.find("!/");
419 if (zip_pos == std::string::npos) {
420 ForEachFileInDir(path, functor);
421 } else {
422 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
423 functor);
424 }
425}
426
427void DiscoverLayersInPathList(const std::string& pathstr) {
428 std::vector<std::string> paths = android::base::Split(pathstr, ":");
429 for (const auto& path : paths) {
430 ForEachFileInPath(path, [&](const std::string& filename) {
431 if (android::base::StartsWith(filename, "libVkLayer") &&
432 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600433
434 // Check to ensure we haven't seen this layer already
435 // Let the first instance of the shared object be enumerated
436 // We're searching for layers in following order:
437 // 1. system path
438 // 2. libraryPermittedPath (if enabled)
439 // 3. libraryPath
440
441 bool duplicate = false;
442 for (auto& layer : g_layer_libraries) {
443 if (layer.GetFilename() == filename) {
444 ALOGV("Skipping duplicate layer %s in %s",
445 filename.c_str(), path.c_str());
446 duplicate = true;
447 }
448 }
449
450 if (!duplicate)
451 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200452 }
453 });
454 }
Jesse Hall80523e22016-01-06 16:47:54 -0800455}
456
Chia-I Wudab25652016-04-28 07:15:51 +0800457const VkExtensionProperties* FindExtension(
458 const std::vector<VkExtensionProperties>& extensions,
459 const char* name) {
460 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
461 [=](const VkExtensionProperties& ext) {
462 return (strcmp(ext.extensionName, name) == 0);
463 });
464 return (it != extensions.cend()) ? &*it : nullptr;
465}
466
Jesse Hall80523e22016-01-06 16:47:54 -0800467void* GetLayerGetProcAddr(const Layer& layer,
468 const char* gpa_name,
469 size_t gpa_name_len) {
470 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800471 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800472}
473
Jesse Hallaa410942016-01-17 13:07:10 -0800474} // anonymous namespace
475
Jesse Hallaa410942016-01-17 13:07:10 -0800476void DiscoverLayers() {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200477 if (property_get_bool("ro.debuggable", false) &&
478 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
479 DiscoverLayersInPathList(kSystemLayerLibraryDir);
480 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600481 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
482 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800483}
484
Chia-I Wu25700b42016-04-28 06:36:09 +0800485uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800486 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800487}
488
Chia-I Wu25700b42016-04-28 06:36:09 +0800489const Layer& GetLayer(uint32_t index) {
490 return g_instance_layers[index];
491}
Chia-I Wubea09db2016-04-22 09:42:41 +0800492
Chia-I Wu04c65512016-04-27 09:54:02 +0800493const Layer* FindLayer(const char* name) {
494 auto layer =
495 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
496 [=](const Layer& entry) {
497 return strcmp(entry.properties.layerName, name) == 0;
498 });
499 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
500}
501
Chia-I Wu25700b42016-04-28 06:36:09 +0800502const VkLayerProperties& GetLayerProperties(const Layer& layer) {
503 return layer.properties;
504}
Chia-I Wubea09db2016-04-22 09:42:41 +0800505
Chia-I Wu25700b42016-04-28 06:36:09 +0800506bool IsLayerGlobal(const Layer& layer) {
507 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800508}
509
Chia-I Wu04c65512016-04-27 09:54:02 +0800510const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
511 uint32_t& count) {
512 count = static_cast<uint32_t>(layer.instance_extensions.size());
513 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800514}
515
Chia-I Wu04c65512016-04-27 09:54:02 +0800516const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
517 uint32_t& count) {
518 count = static_cast<uint32_t>(layer.device_extensions.size());
519 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800520}
521
Chia-I Wudab25652016-04-28 07:15:51 +0800522const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
523 const char* name) {
524 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800525}
526
Chia-I Wudab25652016-04-28 07:15:51 +0800527const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
528 const char* name) {
529 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800530}
531
Chia-I Wudab25652016-04-28 07:15:51 +0800532LayerRef GetLayerRef(const Layer& layer) {
533 LayerLibrary& library = g_layer_libraries[layer.library_idx];
534 return LayerRef((library.Open()) ? &layer : nullptr);
535}
536
537LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800538
539LayerRef::~LayerRef() {
540 if (layer_) {
541 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800542 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800543 }
544}
545
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -0700546LayerRef::LayerRef(LayerRef&& other) noexcept : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600547 other.layer_ = nullptr;
548}
Jesse Hall80523e22016-01-06 16:47:54 -0800549
550PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
551 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
552 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
553 : nullptr;
554}
555
556PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
557 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
558 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
559 : nullptr;
560}
561
Chia-I Wuc96880f2016-03-26 06:56:45 +0800562} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800563} // namespace vulkan