blob: 691f3b0f52597c724f16084119c9ce3394d17e98 [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 + "/");
391 const ZipString prefix_str(prefix.c_str());
392 void* iter_cookie = nullptr;
393 if ((err = StartIteration(zip, &iter_cookie, &prefix_str, nullptr)) != 0) {
394 ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(),
395 err);
396 CloseArchive(zip);
397 return;
398 }
399 ALOGD("searching for layers in '%s!/%s'", zipname.c_str(),
400 dir_in_zip.c_str());
401 ZipEntry entry;
402 ZipString name;
403 while (Next(iter_cookie, &entry, &name) == 0) {
404 std::string filename(
405 reinterpret_cast<const char*>(name.name) + prefix.length(),
406 name.name_length - prefix.length());
407 // only enumerate direct entries of the directory, not subdirectories
Jesse Hall8c6c10e2016-05-19 10:58:35 -0700408 if (filename.find('/') != filename.npos)
409 continue;
410 // Check whether it *may* be possible to load the library directly from
411 // the APK. Loading still may fail for other reasons, but this at least
412 // lets us avoid failed-to-load log messages in the typical case of
413 // compressed and/or unaligned libraries.
414 if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0)
415 continue;
416 functor(filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200417 }
418 EndIteration(iter_cookie);
419 CloseArchive(zip);
420}
Jesse Hall80523e22016-01-06 16:47:54 -0800421
Jesse Hall1a7eb592016-05-01 21:04:40 +0200422template <typename Functor>
423void ForEachFileInPath(const std::string& path, Functor functor) {
424 size_t zip_pos = path.find("!/");
425 if (zip_pos == std::string::npos) {
426 ForEachFileInDir(path, functor);
427 } else {
428 ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2),
429 functor);
430 }
431}
432
433void DiscoverLayersInPathList(const std::string& pathstr) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800434 ATRACE_CALL();
435
Jesse Hall1a7eb592016-05-01 21:04:40 +0200436 std::vector<std::string> paths = android::base::Split(pathstr, ":");
437 for (const auto& path : paths) {
438 ForEachFileInPath(path, [&](const std::string& filename) {
439 if (android::base::StartsWith(filename, "libVkLayer") &&
440 android::base::EndsWith(filename, ".so")) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600441
442 // Check to ensure we haven't seen this layer already
443 // Let the first instance of the shared object be enumerated
444 // We're searching for layers in following order:
445 // 1. system path
446 // 2. libraryPermittedPath (if enabled)
447 // 3. libraryPath
448
449 bool duplicate = false;
450 for (auto& layer : g_layer_libraries) {
451 if (layer.GetFilename() == filename) {
452 ALOGV("Skipping duplicate layer %s in %s",
453 filename.c_str(), path.c_str());
454 duplicate = true;
455 }
456 }
457
458 if (!duplicate)
459 AddLayerLibrary(path, filename);
Jesse Hall1a7eb592016-05-01 21:04:40 +0200460 }
461 });
462 }
Jesse Hall80523e22016-01-06 16:47:54 -0800463}
464
Chia-I Wudab25652016-04-28 07:15:51 +0800465const VkExtensionProperties* FindExtension(
466 const std::vector<VkExtensionProperties>& extensions,
467 const char* name) {
468 auto it = std::find_if(extensions.cbegin(), extensions.cend(),
469 [=](const VkExtensionProperties& ext) {
470 return (strcmp(ext.extensionName, name) == 0);
471 });
472 return (it != extensions.cend()) ? &*it : nullptr;
473}
474
Jesse Hall80523e22016-01-06 16:47:54 -0800475void* GetLayerGetProcAddr(const Layer& layer,
476 const char* gpa_name,
477 size_t gpa_name_len) {
478 const LayerLibrary& library = g_layer_libraries[layer.library_idx];
Chia-I Wuba113272016-04-18 16:38:39 +0800479 return library.GetGPA(layer, gpa_name, gpa_name_len);
Jesse Hall80523e22016-01-06 16:47:54 -0800480}
481
Jesse Hallaa410942016-01-17 13:07:10 -0800482} // anonymous namespace
483
Jesse Hallaa410942016-01-17 13:07:10 -0800484void DiscoverLayers() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800485 ATRACE_CALL();
486
Yiwei Zhangfe622712019-11-08 11:55:36 -0800487 if (android::GraphicsEnv::getInstance().isDebuggable()) {
Jesse Hall1a7eb592016-05-01 21:04:40 +0200488 DiscoverLayersInPathList(kSystemLayerLibraryDir);
489 }
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600490 if (!android::GraphicsEnv::getInstance().getLayerPaths().empty())
491 DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths());
Jesse Hallaa410942016-01-17 13:07:10 -0800492}
493
Chia-I Wu25700b42016-04-28 06:36:09 +0800494uint32_t GetLayerCount() {
Chia-I Wubea09db2016-04-22 09:42:41 +0800495 return static_cast<uint32_t>(g_instance_layers.size());
Jesse Hallaa410942016-01-17 13:07:10 -0800496}
497
Chia-I Wu25700b42016-04-28 06:36:09 +0800498const Layer& GetLayer(uint32_t index) {
499 return g_instance_layers[index];
500}
Chia-I Wubea09db2016-04-22 09:42:41 +0800501
Chia-I Wu04c65512016-04-27 09:54:02 +0800502const Layer* FindLayer(const char* name) {
503 auto layer =
504 std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(),
505 [=](const Layer& entry) {
506 return strcmp(entry.properties.layerName, name) == 0;
507 });
508 return (layer != g_instance_layers.cend()) ? &*layer : nullptr;
509}
510
Chia-I Wu25700b42016-04-28 06:36:09 +0800511const VkLayerProperties& GetLayerProperties(const Layer& layer) {
512 return layer.properties;
513}
Chia-I Wubea09db2016-04-22 09:42:41 +0800514
Chia-I Wu25700b42016-04-28 06:36:09 +0800515bool IsLayerGlobal(const Layer& layer) {
516 return layer.is_global;
Jesse Hallaa410942016-01-17 13:07:10 -0800517}
518
Chia-I Wu04c65512016-04-27 09:54:02 +0800519const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer,
520 uint32_t& count) {
521 count = static_cast<uint32_t>(layer.instance_extensions.size());
522 return layer.instance_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800523}
524
Chia-I Wu04c65512016-04-27 09:54:02 +0800525const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer,
526 uint32_t& count) {
527 count = static_cast<uint32_t>(layer.device_extensions.size());
528 return layer.device_extensions.data();
Jesse Hallaa410942016-01-17 13:07:10 -0800529}
530
Chia-I Wudab25652016-04-28 07:15:51 +0800531const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer,
532 const char* name) {
533 return FindExtension(layer.instance_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800534}
535
Chia-I Wudab25652016-04-28 07:15:51 +0800536const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer,
537 const char* name) {
538 return FindExtension(layer.device_extensions, name);
Jesse Hallaa410942016-01-17 13:07:10 -0800539}
540
Chia-I Wudab25652016-04-28 07:15:51 +0800541LayerRef GetLayerRef(const Layer& layer) {
542 LayerLibrary& library = g_layer_libraries[layer.library_idx];
543 return LayerRef((library.Open()) ? &layer : nullptr);
544}
545
546LayerRef::LayerRef(const Layer* layer) : layer_(layer) {}
Jesse Hall80523e22016-01-06 16:47:54 -0800547
548LayerRef::~LayerRef() {
549 if (layer_) {
550 LayerLibrary& library = g_layer_libraries[layer_->library_idx];
Chia-I Wud91c74f2016-04-18 12:12:36 +0800551 library.Close();
Jesse Hall80523e22016-01-06 16:47:54 -0800552 }
553}
554
Chih-Hung Hsieh5bc849f2018-09-25 14:21:50 -0700555LayerRef::LayerRef(LayerRef&& other) noexcept : layer_(other.layer_) {
Michael Lentine54e6f082016-01-20 11:25:28 -0600556 other.layer_ = nullptr;
557}
Jesse Hall80523e22016-01-06 16:47:54 -0800558
559PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const {
560 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>(
561 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19))
562 : nullptr;
563}
564
565PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const {
566 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>(
567 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17))
568 : nullptr;
569}
570
Chia-I Wuc96880f2016-03-26 06:56:45 +0800571} // namespace api
Jesse Hall80523e22016-01-06 16:47:54 -0800572} // namespace vulkan