blob: b32977a76080bec2f8408df2dadc4ae92542ff98 [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());
Jacky.Deng01728c12018-08-28 15:25:31 +0800168 std::string error_msg;
169 if (!android::CloseNativeLibrary(dlhandle_, native_bridge_, &error_msg)) {
170 ALOGE("failed to unload library '%s': %s", path_.c_str(), error_msg.c_str());
171 refcount_++;
172 } else {
173 dlhandle_ = nullptr;
174 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800175 }
Chia-I Wud91c74f2016-04-18 12:12:36 +0800176}
177
Chia-I Wu50174ee2016-04-18 16:33:20 +0800178bool LayerLibrary::EnumerateLayers(size_t library_idx,
Chia-I Wubea09db2016-04-22 09:42:41 +0800179 std::vector<Layer>& instance_layers) const {
Jesse Hallaa410942016-01-17 13:07:10 -0800180 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200181 GetTrampoline<PFN_vkEnumerateInstanceLayerProperties>(
182 "vkEnumerateInstanceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800183 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200184 GetTrampoline<PFN_vkEnumerateInstanceExtensionProperties>(
185 "vkEnumerateInstanceExtensionProperties");
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800186 if (!enumerate_instance_layers || !enumerate_instance_extensions) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200187 ALOGE("layer library '%s' missing some instance enumeration functions",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800188 path_.c_str());
189 return false;
190 }
191
192 // device functions are optional
Jesse Hallaa410942016-01-17 13:07:10 -0800193 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200194 GetTrampoline<PFN_vkEnumerateDeviceLayerProperties>(
195 "vkEnumerateDeviceLayerProperties");
Jesse Hallaa410942016-01-17 13:07:10 -0800196 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions =
Victor Khimenkobbf77002018-08-16 22:38:36 +0200197 GetTrampoline<PFN_vkEnumerateDeviceExtensionProperties>(
198 "vkEnumerateDeviceExtensionProperties");
Jesse Hall80523e22016-01-06 16:47:54 -0800199
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800200 // get layer counts
Jesse Hallaa410942016-01-17 13:07:10 -0800201 uint32_t num_instance_layers = 0;
202 uint32_t num_device_layers = 0;
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800203 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr);
204 if (result != VK_SUCCESS || !num_instance_layers) {
Jesse Hallaa410942016-01-17 13:07:10 -0800205 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200206 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800207 "vkEnumerateInstanceLayerProperties failed for library '%s': "
208 "%d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800209 path_.c_str(), result);
Jesse Hallaa410942016-01-17 13:07:10 -0800210 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800211 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800212 }
Jesse Hallaa410942016-01-17 13:07:10 -0800213 if (enumerate_device_layers) {
214 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
215 nullptr);
216 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200217 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800218 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800219 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800220 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800221 }
222 }
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800223
224 // get layer properties
Jesse Hallaa410942016-01-17 13:07:10 -0800225 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca(
226 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties)));
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800227 result = enumerate_instance_layers(&num_instance_layers, properties);
228 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200229 ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800230 path_.c_str(), result);
231 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800232 }
233 if (num_device_layers > 0) {
234 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers,
235 properties + num_instance_layers);
236 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200237 ALOGE(
Jesse Hallaa410942016-01-17 13:07:10 -0800238 "vkEnumerateDeviceLayerProperties failed for library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800239 path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800240 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800241 }
Jesse Hall80523e22016-01-06 16:47:54 -0800242 }
243
Chia-I Wubea09db2016-04-22 09:42:41 +0800244 // append layers to instance_layers
Chia-I Wu50174ee2016-04-18 16:33:20 +0800245 size_t prev_num_instance_layers = instance_layers.size();
Chia-I Wu50174ee2016-04-18 16:33:20 +0800246 instance_layers.reserve(prev_num_instance_layers + num_instance_layers);
Jesse Hallaa410942016-01-17 13:07:10 -0800247 for (size_t i = 0; i < num_instance_layers; i++) {
248 const VkLayerProperties& props = properties[i];
249
Jesse Hall80523e22016-01-06 16:47:54 -0800250 Layer layer;
Jesse Hallaa410942016-01-17 13:07:10 -0800251 layer.properties = props;
Jesse Hall80523e22016-01-06 16:47:54 -0800252 layer.library_idx = library_idx;
Chia-I Wubea09db2016-04-22 09:42:41 +0800253 layer.is_global = false;
Jesse Hall80523e22016-01-06 16:47:54 -0800254
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800255 uint32_t count = 0;
256 result =
257 enumerate_instance_extensions(props.layerName, &count, nullptr);
258 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200259 ALOGE(
260 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
261 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800262 props.layerName, path_.c_str(), result);
263 instance_layers.resize(prev_num_instance_layers);
264 return false;
265 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800266 layer.instance_extensions.resize(count);
267 result = enumerate_instance_extensions(
268 props.layerName, &count, layer.instance_extensions.data());
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800269 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200270 ALOGE(
271 "vkEnumerateInstanceExtensionProperties(\"%s\") failed for "
272 "library '%s': %d",
Chia-I Wu5f093bf2016-04-19 12:22:52 +0800273 props.layerName, path_.c_str(), result);
274 instance_layers.resize(prev_num_instance_layers);
275 return false;
Jesse Hall80523e22016-01-06 16:47:54 -0800276 }
277
Chia-I Wu279ccc02016-04-18 16:45:15 +0800278 for (size_t j = 0; j < num_device_layers; j++) {
279 const auto& dev_props = properties[num_instance_layers + j];
280 if (memcmp(&props, &dev_props, sizeof(props)) == 0) {
Chia-I Wubea09db2016-04-22 09:42:41 +0800281 layer.is_global = true;
Chia-I Wu279ccc02016-04-18 16:45:15 +0800282 break;
283 }
284 }
Jesse Hallaa410942016-01-17 13:07:10 -0800285
Chia-I Wubea09db2016-04-22 09:42:41 +0800286 if (layer.is_global && enumerate_device_extensions) {
Jesse Hallaa410942016-01-17 13:07:10 -0800287 result = enumerate_device_extensions(
288 VK_NULL_HANDLE, props.layerName, &count, nullptr);
289 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200290 ALOGE(
291 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800292 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800293 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800294 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800295 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800296 }
Chia-I Wubea09db2016-04-22 09:42:41 +0800297 layer.device_extensions.resize(count);
298 result = enumerate_device_extensions(
299 VK_NULL_HANDLE, props.layerName, &count,
300 layer.device_extensions.data());
Jesse Hallaa410942016-01-17 13:07:10 -0800301 if (result != VK_SUCCESS) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200302 ALOGE(
303 "vkEnumerateDeviceExtensionProperties(\"%s\") failed for "
Chia-I Wu50174ee2016-04-18 16:33:20 +0800304 "library '%s': %d",
Chia-I Wu6693f5c2016-04-18 12:20:02 +0800305 props.layerName, path_.c_str(), result);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800306 instance_layers.resize(prev_num_instance_layers);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800307 return false;
Jesse Hallaa410942016-01-17 13:07:10 -0800308 }
309 }
310
Chia-I Wubea09db2016-04-22 09:42:41 +0800311 instance_layers.push_back(layer);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200312 ALOGD("added %s layer '%s' from library '%s'",
313 (layer.is_global) ? "global" : "instance", props.layerName,
314 path_.c_str());
Jesse Hall80523e22016-01-06 16:47:54 -0800315 }
316
Chia-I Wu50174ee2016-04-18 16:33:20 +0800317 return true;
318}
Jesse Hall80523e22016-01-06 16:47:54 -0800319
Chia-I Wuba113272016-04-18 16:38:39 +0800320void* LayerLibrary::GetGPA(const Layer& layer,
321 const char* gpa_name,
322 size_t gpa_name_len) const {
323 void* gpa;
324 size_t layer_name_len =
325 std::max(size_t{2}, strlen(layer.properties.layerName));
326 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1));
327 strcpy(name, layer.properties.layerName);
328 strcpy(name + layer_name_len, gpa_name);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200329 if (!(gpa = GetTrampoline(name))) {
Chia-I Wuba113272016-04-18 16:38:39 +0800330 strcpy(name, "vk");
331 strcpy(name + 2, gpa_name);
Victor Khimenkobbf77002018-08-16 22:38:36 +0200332 gpa = GetTrampoline(name);
Chia-I Wuba113272016-04-18 16:38:39 +0800333 }
334 return gpa;
335}
336
Jesse Hall1a7eb592016-05-01 21:04:40 +0200337// ----------------------------------------------------------------------------
338
Chia-I Wu50174ee2016-04-18 16:33:20 +0800339std::vector<LayerLibrary> g_layer_libraries;
340std::vector<Layer> g_instance_layers;
Chia-I Wu50174ee2016-04-18 16:33:20 +0800341
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600342void AddLayerLibrary(const std::string& path, const std::string& filename) {
343 LayerLibrary library(path + "/" + filename, filename);
Chia-I Wu50174ee2016-04-18 16:33:20 +0800344 if (!library.Open())
345 return;
346
Chia-I Wubea09db2016-04-22 09:42:41 +0800347 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) {
Chia-I Wu50174ee2016-04-18 16:33:20 +0800348 library.Close();
349 return;
350 }
351
352 library.Close();
353
354 g_layer_libraries.emplace_back(std::move(library));
Jesse Hall80523e22016-01-06 16:47:54 -0800355}
356
Jesse Hall1a7eb592016-05-01 21:04:40 +0200357template <typename Functor>
358void ForEachFileInDir(const std::string& dirname, Functor functor) {
359 auto dir_deleter = [](DIR* handle) { closedir(handle); };
360 std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()),
361 dir_deleter);
362 if (!dir) {
363 // It's normal for some search directories to not exist, especially
364 // /data/local/debug/vulkan.
Jesse Hall80523e22016-01-06 16:47:54 -0800365 int err = errno;
Jesse Hall1a7eb592016-05-01 21:04:40 +0200366 ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s",
367 dirname.c_str(), strerror(err));
Jesse Hall80523e22016-01-06 16:47:54 -0800368 return;
369 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200370 ALOGD("searching for layers in '%s'", dirname.c_str());
371 dirent* entry;
372 while ((entry = readdir(dir.get())) != nullptr)
373 functor(entry->d_name);
374}
Jesse Hall80523e22016-01-06 16:47:54 -0800375
Jesse Hall1a7eb592016-05-01 21:04:40 +0200376template <typename Functor>
377void ForEachFileInZip(const std::string& zipname,
378 const std::string& dir_in_zip,
379 Functor functor) {
380 int32_t err;
381 ZipArchiveHandle zip = nullptr;
382 if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) {
383 ALOGE("failed to open apk '%s': %d", zipname.c_str(), err);
384 return;
Jesse Hall80523e22016-01-06 16:47:54 -0800385 }
Jesse Hall1a7eb592016-05-01 21:04:40 +0200386 std::string prefix(dir_in_zip + "/");
387 const ZipString prefix_str(prefix.c_str());
388 void* iter_cookie = nullptr;
389 if ((err = StartIteration(zip, &iter_cookie, &prefix_str, nullptr)) != 0) {
390 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;
398 ZipString name;
399 while (Next(iter_cookie, &entry, &name) == 0) {
400 std::string filename(
401 reinterpret_cast<const char*>(name.name) + prefix.length(),
402 name.name_length - prefix.length());
403 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700404 if (filename.find('/') != filename.npos)
405 continue;
406 // Check whether it *may* be possible to load the library directly from
407 // the APK. Loading still may fail for other reasons, but this at least
408 // lets us avoid failed-to-load log messages in the typical case of
409 // compressed and/or unaligned libraries.
410 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
411 continue;
412 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200413 }
414 EndIteration(iter_cookie);
415 CloseArchive(zip);
416}
Jesse Hall80523e22016-01-06 16:47:54 -0800417
Jesse Hall1a7eb592016-05-01 21:04:40 +0200418template <typename Functor>
419void ForEachFileInPath(const std::string& path, Functor functor) {
420 size_t zip_pos = path.find("!/");
421 if (zip_pos == std::string::npos) {
422 ForEachFileInDir(path, functor);
423 } else {
424 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
425 functor);
426 }
427}
428
429void DiscoverLayersInPathList(const std::string& pathstr) {
430 std::vector<std::string> paths = android::base::Split(pathstr, ":");
431 for (const auto& path : paths) {
432 ForEachFileInPath(path, [&](const std::string& filename) {
433 if (android::base::StartsWith(filename, "libVkLayer") &&
434 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600435
436 // Check to ensure we haven't seen this layer already
437 // Let the first instance of the shared object be enumerated
438 // We're searching for layers in following order:
439 // 1. system path
440 // 2. libraryPermittedPath (if enabled)
441 // 3. libraryPath
442
443 bool duplicate = false;
444 for (auto& layer : g_layer_libraries) {
445 if (layer.GetFilename() == filename) {
446 ALOGV("Skipping duplicate layer %s in %s",
447 filename.c_str(), path.c_str());
448 duplicate = true;
449 }
450 }
451
452 if (!duplicate)
453 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200454 }
455 });
456 }
Jesse Hall80523e22016-01-06 16:47:54 -0800457}
458
Chia-I Wudab25652016-04-28 07:15:51 +0800459const VkExtensionProperties* FindExtension(
460 const std::vector<VkExtensionProperties>& extensions,
461 const char* name) {
462 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
463 [=](const VkExtensionProperties& ext) {
464 return (strcmp(ext.extensionName, name) == 0);
465 });
466 return (it != extensions.cend()) ? &*it : nullptr;
467}
468
Jesse Hall80523e22016-01-06 16:47:54 -0800469void* GetLayerGetProcAddr(const Layer& layer,
470 const char* gpa_name,
471 size_t gpa_name_len) {
472 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800473 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800474}
475
Jesse Hallaa410942016-01-17 13:07:10 -0800476} // anonymous namespace
477
Jesse Hallaa410942016-01-17 13:07:10 -0800478void DiscoverLayers() {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200479 if (property_get_bool("ro.debuggable", false) &&
480 prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
481 DiscoverLayersInPathList(kSystemLayerLibraryDir);
482 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600483 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
484 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800485}
486
Chia-I Wu25700b42016-04-28 06:36:09 +0800487uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800488 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800489}
490
Chia-I Wu25700b42016-04-28 06:36:09 +0800491const Layer& GetLayer(uint32_t index) {
492 return g_instance_layers[index];
493}
Chia-I Wubea09db2016-04-22 09:42:41 +0800494
Chia-I Wu04c65512016-04-27 09:54:02 +0800495const Layer* FindLayer(const char* name) {
496 auto layer =
497 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
498 [=](const Layer& entry) {
499 return strcmp(entry.properties.layerName, name) == 0;
500 });
501 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
502}
503
Chia-I Wu25700b42016-04-28 06:36:09 +0800504const VkLayerProperties& GetLayerProperties(const Layer& layer) {
505 return layer.properties;
506}
Chia-I Wubea09db2016-04-22 09:42:41 +0800507
Chia-I Wu25700b42016-04-28 06:36:09 +0800508bool IsLayerGlobal(const Layer& layer) {
509 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800510}
511
Chia-I Wu04c65512016-04-27 09:54:02 +0800512const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
513 uint32_t& count) {
514 count = static_cast<uint32_t>(layer.instance_extensions.size());
515 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800516}
517
Chia-I Wu04c65512016-04-27 09:54:02 +0800518const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
519 uint32_t& count) {
520 count = static_cast<uint32_t>(layer.device_extensions.size());
521 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800522}
523
Chia-I Wudab25652016-04-28 07:15:51 +0800524const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
525 const char* name) {
526 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800527}
528
Chia-I Wudab25652016-04-28 07:15:51 +0800529const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
530 const char* name) {
531 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800532}
533
Chia-I Wudab25652016-04-28 07:15:51 +0800534LayerRef GetLayerRef(const Layer& layer) {
535 LayerLibrary& library = g_layer_libraries[layer.library_idx];
536 return LayerRef((library.Open()) ? &layer : nullptr);
537}
538
539LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800540
541LayerRef::~LayerRef() {
542 if (layer_) {
543 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800544 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800545 }
546}
547
Chia-I Wudab25652016-04-28 07:15:51 +0800548LayerRef::LayerRef(LayerRef&& other) : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600549 other.layer_ = nullptr;
550}
Jesse Hall80523e22016-01-06 16:47:54 -0800551
552PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
553 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
554 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
555 : nullptr;
556}
557
558PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
559 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
560 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
561 : nullptr;
562}
563
Chia-I Wuc96880f2016-03-26 06:56:45 +0800564} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800565} // namespace vulkan