blob: 96c556393c08f2e74a1b62714900f996872c8c98 [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
Chia-I Wu50174ee2016-04-18 16:33:20 +080081 LayerLibrary(LayerLibrary&& other)
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)) {
Victor Khimenkobbf77002018-08-16 22:38:36 +0200142 std::string error_msg;
143 dlhandle_ = OpenNativeLibrary(
144 app_namespace, path_.c_str(), &native_bridge_, &error_msg);
145 if (!dlhandle_) {
146 ALOGE("failed to load layer library '%s': %s", path_.c_str(),
147 error_msg.c_str());
148 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());
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800168 dlclose(dlhandle_);
169 dlhandle_ = nullptr;
Chia-I Wud91c74f2016-04-18 12:12:36 +0800170 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800171}
172
Chia-I Wu50174ee2016-04-18 16:33:20 +0800173bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800174 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800175 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200176 GetTrampoline<PFN_vkEnumerateInstanceLayerProperties>(
177 "vkEnumerateInstanceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800178 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200179 GetTrampoline<PFN_vkEnumerateInstanceExtensionProperties>(
180 "vkEnumerateInstanceExtensionProperties");
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800181 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200182 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800183 path_.c_str());
184 return false;
185 }
186
187 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800188 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200189 GetTrampoline<PFN_vkEnumerateDeviceLayerProperties>(
190 "vkEnumerateDeviceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800191 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200192 GetTrampoline<PFN_vkEnumerateDeviceExtensionProperties>(
193 "vkEnumerateDeviceExtensionProperties");
Jesse Hall80523e22016-01-06 16:47:54 -0800194
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800195 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800196 uint32_t num_instance_layers = 0;
197 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800198 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
199 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800200 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200201 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800202 "vkEnumerateInstanceLayerProperties failed for library '%s': "
203 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800204 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800205 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800206 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800207 }
Jesse Hallaa410942016-01-17 13:07:10 -0800208 if (enumerate_device_layers) {
209 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
210 nullptr);
211 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200212 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800213 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800214 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800215 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800216 }
217 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800218
219 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800220 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
221 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800222 result = enumerate_instance_layers(&num_instance_layers, properties);
223 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200224 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800225 path_.c_str(), result);
226 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800227 }
228 if (num_device_layers > 0) {
229 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
230 properties + num_instance_layers);
231 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200232 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800233 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800234 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800235 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800236 }
Jesse Hall80523e22016-01-06 16:47:54 -0800237 }
238
Chia-I Wubea09db2016-04-22 09:42:41 +0800239 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800240 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800241 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800242 for (size_t i = 0; i < num_instance_layers; i++) {
243 const VkLayerProperties& props = properties[i];
244
Jesse Hall80523e22016-01-06 16:47:54 -0800245 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800246 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800247 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800248 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800249
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800250 uint32_t count = 0;
251 result =
252 enumerate_instance_extensions(props.layerName, &count, nullptr);
253 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200254 ALOGE(
255 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
256 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800257 props.layerName, path_.c_str(), result);
258 instance_layers.resize(prev_num_instance_layers);
259 return false;
260 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800261 layer.instance_extensions.resize(count);
262 result = enumerate_instance_extensions(
263 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800264 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200265 ALOGE(
266 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
267 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800268 props.layerName, path_.c_str(), result);
269 instance_layers.resize(prev_num_instance_layers);
270 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800271 }
272
Chia-I Wu279ccc02016-04-18 16:45:15 +0800273 for (size_t j = 0; j < num_device_layers; j++) {
274 const auto& dev_props = properties[num_instance_layers + j];
275 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800276 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800277 break;
278 }
279 }
Jesse Hallaa410942016-01-17 13:07:10 -0800280
Chia-I Wubea09db2016-04-22 09:42:41 +0800281 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800282 result = enumerate_device_extensions(
283 VK_NULL_HANDLE, props.layerName, &count, nullptr);
284 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200285 ALOGE(
286 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800287 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800288 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800289 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800290 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800291 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800292 layer.device_extensions.resize(count);
293 result = enumerate_device_extensions(
294 VK_NULL_HANDLE, props.layerName, &count,
295 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800296 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200297 ALOGE(
298 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800299 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800300 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800301 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800302 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800303 }
304 }
305
Chia-I Wubea09db2016-04-22 09:42:41 +0800306 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200307 ALOGD("added %s layer '%s' from library '%s'",
308 (layer.is_global) ? "global" : "instance", props.layerName,
309 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800310 }
311
Chia-I Wu50174ee2016-04-18 16:33:20 +0800312 return true;
313}
Jesse Hall80523e22016-01-06 16:47:54 -0800314
Chia-I Wuba113272016-04-18 16:38:39 +0800315void* LayerLibrary::GetGPA(const Layer& layer,
316 const char* gpa_name,
317 size_t gpa_name_len) const {
318 void* gpa;
319 size_t layer_name_len =
320 std::max(size_t{2}, strlen(layer.properties.layerName));
321 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
322 strcpy(name, layer.properties.layerName);
323 strcpy(name + layer_name_len, gpa_name);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200324 if (!(gpa = GetTrampoline(name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800325 strcpy(name, "vk");
326 strcpy(name + 2, gpa_name);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200327 gpa = GetTrampoline(name);
Chia-I Wuba113272016-04-18 16:38:39 +0800328 }
329 return gpa;
330}
331
Jesse Hall1a7eb592016-05-01 21:04:40 +0200332// ----------------------------------------------------------------------------
333
Chia-I Wu50174ee2016-04-18 16:33:20 +0800334std::vector<LayerLibrary> g_layer_libraries;
335std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800336
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600337void AddLayerLibrary(const std::string& path, const std::string& filename) {
338 LayerLibrary library(path + "/" + filename, filename);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800339 if (!library.Open())
340 return;
341
Chia-I Wubea09db2016-04-22 09:42:41 +0800342 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800343 library.Close();
344 return;
345 }
346
347 library.Close();
348
349 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800350}
351
Jesse Hall1a7eb592016-05-01 21:04:40 +0200352template <typename Functor>
353void ForEachFileInDir(const std::string& dirname, Functor functor) {
354 auto dir_deleter = [](DIR* handle) { closedir(handle); };
355 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
356 dir_deleter);
357 if (!dir) {
358 // It's normal for some search directories to not exist, especially
359 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800360 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200361 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
362 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800363 return;
364 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200365 ALOGD("searching for layers in '%s'", dirname.c_str());
366 dirent* entry;
367 while ((entry = readdir(dir.get())) != nullptr)
368 functor(entry->d_name);
369}
Jesse Hall80523e22016-01-06 16:47:54 -0800370
Jesse Hall1a7eb592016-05-01 21:04:40 +0200371template <typename Functor>
372void ForEachFileInZip(const std::string& zipname,
373 const std::string& dir_in_zip,
374 Functor functor) {
375 int32_t err;
376 ZipArchiveHandle zip = nullptr;
377 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
378 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
379 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800380 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200381 std::string prefix(dir_in_zip + "/");
382 const ZipString prefix_str(prefix.c_str());
383 void* iter_cookie = nullptr;
384 if ((err = StartIteration(zip, &iter_cookie, &prefix_str, nullptr)) != 0) {
385 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
386 err);
387 CloseArchive(zip);
388 return;
389 }
390 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
391 dir_in_zip.c_str());
392 ZipEntry entry;
393 ZipString name;
394 while (Next(iter_cookie, &entry, &name) == 0) {
395 std::string filename(
396 reinterpret_cast<const char*>(name.name) + prefix.length(),
397 name.name_length - prefix.length());
398 // 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) {
425 std::vector<std::string> paths = android::base::Split(pathstr, ":");
426 for (const auto& path : paths) {
427 ForEachFileInPath(path, [&](const std::string& filename) {
428 if (android::base::StartsWith(filename, "libVkLayer") &&
429 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600430
431 // Check to ensure we haven't seen this layer already
432 // Let the first instance of the shared object be enumerated
433 // We're searching for layers in following order:
434 // 1. system path
435 // 2. libraryPermittedPath (if enabled)
436 // 3. libraryPath
437
438 bool duplicate = false;
439 for (auto& layer : g_layer_libraries) {
440 if (layer.GetFilename() == filename) {
441 ALOGV("Skipping duplicate layer %s in %s",
442 filename.c_str(), path.c_str());
443 duplicate = true;
444 }
445 }
446
447 if (!duplicate)
448 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200449 }
450 });
451 }
Jesse Hall80523e22016-01-06 16:47:54 -0800452}
453
Chia-I Wudab25652016-04-28 07:15:51 +0800454const VkExtensionProperties* FindExtension(
455 const std::vector<VkExtensionProperties>& extensions,
456 const char* name) {
457 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
458 [=](const VkExtensionProperties& ext) {
459 return (strcmp(ext.extensionName, name) == 0);
460 });
461 return (it != extensions.cend()) ? &*it : nullptr;
462}
463
Jesse Hall80523e22016-01-06 16:47:54 -0800464void* GetLayerGetProcAddr(const Layer& layer,
465 const char* gpa_name,
466 size_t gpa_name_len) {
467 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800468 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800469}
470
Jesse Hallaa410942016-01-17 13:07:10 -0800471} // anonymous namespace
472
Jesse Hallaa410942016-01-17 13:07:10 -0800473void DiscoverLayers() {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200474 if (property_get_bool("ro.debuggable", false) &&
475 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
476 DiscoverLayersInPathList(kSystemLayerLibraryDir);
477 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600478 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
479 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800480}
481
Chia-I Wu25700b42016-04-28 06:36:09 +0800482uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800483 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800484}
485
Chia-I Wu25700b42016-04-28 06:36:09 +0800486const Layer& GetLayer(uint32_t index) {
487 return g_instance_layers[index];
488}
Chia-I Wubea09db2016-04-22 09:42:41 +0800489
Chia-I Wu04c65512016-04-27 09:54:02 +0800490const Layer* FindLayer(const char* name) {
491 auto layer =
492 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
493 [=](const Layer& entry) {
494 return strcmp(entry.properties.layerName, name) == 0;
495 });
496 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
497}
498
Chia-I Wu25700b42016-04-28 06:36:09 +0800499const VkLayerProperties& GetLayerProperties(const Layer& layer) {
500 return layer.properties;
501}
Chia-I Wubea09db2016-04-22 09:42:41 +0800502
Chia-I Wu25700b42016-04-28 06:36:09 +0800503bool IsLayerGlobal(const Layer& layer) {
504 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800505}
506
Chia-I Wu04c65512016-04-27 09:54:02 +0800507const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
508 uint32_t& count) {
509 count = static_cast<uint32_t>(layer.instance_extensions.size());
510 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800511}
512
Chia-I Wu04c65512016-04-27 09:54:02 +0800513const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
514 uint32_t& count) {
515 count = static_cast<uint32_t>(layer.device_extensions.size());
516 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800517}
518
Chia-I Wudab25652016-04-28 07:15:51 +0800519const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
520 const char* name) {
521 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800522}
523
Chia-I Wudab25652016-04-28 07:15:51 +0800524const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
525 const char* name) {
526 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800527}
528
Chia-I Wudab25652016-04-28 07:15:51 +0800529LayerRef GetLayerRef(const Layer& layer) {
530 LayerLibrary& library = g_layer_libraries[layer.library_idx];
531 return LayerRef((library.Open()) ? &layer : nullptr);
532}
533
534LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800535
536LayerRef::~LayerRef() {
537 if (layer_) {
538 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800539 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800540 }
541}
542
Chia-I Wudab25652016-04-28 07:15:51 +0800543LayerRef::LayerRef(LayerRef&& other) : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600544 other.layer_ = nullptr;
545}
Jesse Hall80523e22016-01-06 16:47:54 -0800546
547PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
548 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
549 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
550 : nullptr;
551}
552
553PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
554 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
555 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
556 : nullptr;
557}
558
Chia-I Wuc96880f2016-03-26 06:56:45 +0800559} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800560} // namespace vulkan