Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 1 | /* |
| 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 Wu | c96880f | 2016-03-26 06:56:45 +0800 | [diff] [blame] | 17 | #include "layers_extensions.h" |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 18 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 19 | #include <alloca.h> |
| 20 | #include <dirent.h> |
| 21 | #include <dlfcn.h> |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 22 | #include <string.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 23 | #include <sys/prctl.h> |
| 24 | |
| 25 | #include <mutex> |
| 26 | #include <string> |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 27 | #include <vector> |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 28 | |
Jesse Hall | 40c07a1 | 2016-05-11 22:56:29 -0700 | [diff] [blame] | 29 | #include <android/dlext.h> |
Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 30 | #include <android-base/strings.h> |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 31 | #include <cutils/properties.h> |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame^] | 32 | #include <graphicsenv/GraphicsEnv.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 33 | #include <log/log.h> |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 34 | #include <ziparchive/zip_archive.h> |
| 35 | |
Jesse Hall | b147127 | 2016-01-17 21:36:58 -0800 | [diff] [blame] | 36 | // TODO(jessehall): The whole way we deal with extensions is pretty hokey, and |
| 37 | // not a good long-term solution. Having a hard-coded enum of extensions is |
| 38 | // bad, of course. Representing sets of extensions (requested, supported, etc.) |
| 39 | // as a bitset isn't necessarily bad, if the mapping from extension to bit were |
| 40 | // dynamic. Need to rethink this completely when there's a little more time. |
| 41 | |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 42 | // TODO(jessehall): This file currently builds up global data structures as it |
| 43 | // loads, and never cleans them up. This means we're doing heap allocations |
| 44 | // without going through an app-provided allocator, but worse, we'll leak those |
| 45 | // allocations if the loader is unloaded. |
| 46 | // |
| 47 | // We should allocate "enough" BSS space, and suballocate from there. Will |
| 48 | // probably want to intern strings, etc., and will need some custom/manual data |
| 49 | // structures. |
| 50 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 51 | namespace vulkan { |
Chia-I Wu | c96880f | 2016-03-26 06:56:45 +0800 | [diff] [blame] | 52 | namespace api { |
| 53 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 54 | struct Layer { |
| 55 | VkLayerProperties properties; |
| 56 | size_t library_idx; |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 57 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 58 | // true if the layer intercepts vkCreateDevice and device commands |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 59 | bool is_global; |
| 60 | |
| 61 | std::vector<VkExtensionProperties> instance_extensions; |
| 62 | std::vector<VkExtensionProperties> device_extensions; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 63 | }; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 64 | |
| 65 | namespace { |
| 66 | |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 67 | const char kSystemLayerLibraryDir[] = "/data/local/debug/vulkan"; |
| 68 | |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 69 | class LayerLibrary { |
| 70 | public: |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame^] | 71 | explicit LayerLibrary(const std::string& path, |
| 72 | const std::string& filename) |
| 73 | : path_(path), |
| 74 | filename_(filename), |
| 75 | dlhandle_(nullptr), |
| 76 | refcount_(0) {} |
Chia-I Wu | 7434959 | 2016-04-18 12:08:39 +0800 | [diff] [blame] | 77 | |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 78 | LayerLibrary(LayerLibrary&& other) |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 79 | : path_(std::move(other.path_)), |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame^] | 80 | filename_(std::move(other.filename_)), |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 81 | dlhandle_(other.dlhandle_), |
| 82 | refcount_(other.refcount_) { |
| 83 | other.dlhandle_ = nullptr; |
| 84 | other.refcount_ = 0; |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | LayerLibrary(const LayerLibrary&) = delete; |
| 88 | LayerLibrary& operator=(const LayerLibrary&) = delete; |
| 89 | |
Chia-I Wu | a622974 | 2016-04-26 07:37:44 +0800 | [diff] [blame] | 90 | // these are thread-safe |
Chia-I Wu | fd0389f | 2016-04-18 12:11:00 +0800 | [diff] [blame] | 91 | bool Open(); |
Chia-I Wu | d91c74f | 2016-04-18 12:12:36 +0800 | [diff] [blame] | 92 | void Close(); |
Chia-I Wu | fd0389f | 2016-04-18 12:11:00 +0800 | [diff] [blame] | 93 | |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 94 | bool EnumerateLayers(size_t library_idx, |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 95 | std::vector<Layer>& instance_layers) const; |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 96 | |
Chia-I Wu | ba11327 | 2016-04-18 16:38:39 +0800 | [diff] [blame] | 97 | void* GetGPA(const Layer& layer, |
| 98 | const char* gpa_name, |
| 99 | size_t gpa_name_len) const; |
| 100 | |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame^] | 101 | const std::string GetFilename() { return filename_; } |
| 102 | |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 103 | private: |
| 104 | const std::string path_; |
Chia-I Wu | a622974 | 2016-04-26 07:37:44 +0800 | [diff] [blame] | 105 | |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame^] | 106 | // Track the filename alone so we can detect duplicates |
| 107 | const std::string filename_; |
| 108 | |
Chia-I Wu | a622974 | 2016-04-26 07:37:44 +0800 | [diff] [blame] | 109 | std::mutex mutex_; |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 110 | void* dlhandle_; |
| 111 | size_t refcount_; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 112 | }; |
Chia-I Wu | 7434959 | 2016-04-18 12:08:39 +0800 | [diff] [blame] | 113 | |
Chia-I Wu | fd0389f | 2016-04-18 12:11:00 +0800 | [diff] [blame] | 114 | bool LayerLibrary::Open() { |
Chia-I Wu | a622974 | 2016-04-26 07:37:44 +0800 | [diff] [blame] | 115 | std::lock_guard<std::mutex> lock(mutex_); |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 116 | if (refcount_++ == 0) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 117 | ALOGV("opening layer library '%s'", path_.c_str()); |
Jesse Hall | 40c07a1 | 2016-05-11 22:56:29 -0700 | [diff] [blame] | 118 | // Libraries in the system layer library dir can't be loaded into |
| 119 | // the application namespace. That causes compatibility problems, since |
| 120 | // any symbol dependencies will be resolved by system libraries. They |
| 121 | // can't safely use libc++_shared, for example. Which is one reason |
| 122 | // (among several) we only allow them in non-user builds. |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame^] | 123 | auto app_namespace = android::GraphicsEnv::getInstance().getAppNamespace(); |
Jesse Hall | 40c07a1 | 2016-05-11 22:56:29 -0700 | [diff] [blame] | 124 | if (app_namespace && |
| 125 | !android::base::StartsWith(path_, kSystemLayerLibraryDir)) { |
| 126 | android_dlextinfo dlextinfo = {}; |
| 127 | dlextinfo.flags = ANDROID_DLEXT_USE_NAMESPACE; |
| 128 | dlextinfo.library_namespace = app_namespace; |
| 129 | dlhandle_ = android_dlopen_ext(path_.c_str(), RTLD_NOW | RTLD_LOCAL, |
| 130 | &dlextinfo); |
| 131 | } else { |
| 132 | dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL); |
| 133 | } |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 134 | if (!dlhandle_) { |
| 135 | ALOGE("failed to load layer library '%s': %s", path_.c_str(), |
Chia-I Wu | fd0389f | 2016-04-18 12:11:00 +0800 | [diff] [blame] | 136 | dlerror()); |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 137 | refcount_ = 0; |
Chia-I Wu | fd0389f | 2016-04-18 12:11:00 +0800 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | } |
Chia-I Wu | fd0389f | 2016-04-18 12:11:00 +0800 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
Chia-I Wu | d91c74f | 2016-04-18 12:12:36 +0800 | [diff] [blame] | 144 | void LayerLibrary::Close() { |
Chia-I Wu | a622974 | 2016-04-26 07:37:44 +0800 | [diff] [blame] | 145 | std::lock_guard<std::mutex> lock(mutex_); |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 146 | if (--refcount_ == 0) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 147 | ALOGV("closing layer library '%s'", path_.c_str()); |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 148 | dlclose(dlhandle_); |
| 149 | dlhandle_ = nullptr; |
Chia-I Wu | d91c74f | 2016-04-18 12:12:36 +0800 | [diff] [blame] | 150 | } |
Chia-I Wu | d91c74f | 2016-04-18 12:12:36 +0800 | [diff] [blame] | 151 | } |
| 152 | |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 153 | bool LayerLibrary::EnumerateLayers(size_t library_idx, |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 154 | std::vector<Layer>& instance_layers) const { |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 155 | PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers = |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 156 | reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>( |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 157 | dlsym(dlhandle_, "vkEnumerateInstanceLayerProperties")); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 158 | PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions = |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 159 | reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>( |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 160 | dlsym(dlhandle_, "vkEnumerateInstanceExtensionProperties")); |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 161 | if (!enumerate_instance_layers || !enumerate_instance_extensions) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 162 | ALOGE("layer library '%s' missing some instance enumeration functions", |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 163 | path_.c_str()); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | // device functions are optional |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 168 | PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers = |
| 169 | reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>( |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 170 | dlsym(dlhandle_, "vkEnumerateDeviceLayerProperties")); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 171 | PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions = |
| 172 | reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>( |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 173 | dlsym(dlhandle_, "vkEnumerateDeviceExtensionProperties")); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 174 | |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 175 | // get layer counts |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 176 | uint32_t num_instance_layers = 0; |
| 177 | uint32_t num_device_layers = 0; |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 178 | VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr); |
| 179 | if (result != VK_SUCCESS || !num_instance_layers) { |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 180 | if (result != VK_SUCCESS) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 181 | ALOGE( |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 182 | "vkEnumerateInstanceLayerProperties failed for library '%s': " |
| 183 | "%d", |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 184 | path_.c_str(), result); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 185 | } |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 186 | return false; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 187 | } |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 188 | if (enumerate_device_layers) { |
| 189 | result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers, |
| 190 | nullptr); |
| 191 | if (result != VK_SUCCESS) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 192 | ALOGE( |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 193 | "vkEnumerateDeviceLayerProperties failed for library '%s': %d", |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 194 | path_.c_str(), result); |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 195 | return false; |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 198 | |
| 199 | // get layer properties |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 200 | VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca( |
| 201 | (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties))); |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 202 | result = enumerate_instance_layers(&num_instance_layers, properties); |
| 203 | if (result != VK_SUCCESS) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 204 | ALOGE("vkEnumerateInstanceLayerProperties failed for library '%s': %d", |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 205 | path_.c_str(), result); |
| 206 | return false; |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 207 | } |
| 208 | if (num_device_layers > 0) { |
| 209 | result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers, |
| 210 | properties + num_instance_layers); |
| 211 | if (result != VK_SUCCESS) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 212 | ALOGE( |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 213 | "vkEnumerateDeviceLayerProperties failed for library '%s': %d", |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 214 | path_.c_str(), result); |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 215 | return false; |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 216 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 219 | // append layers to instance_layers |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 220 | size_t prev_num_instance_layers = instance_layers.size(); |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 221 | instance_layers.reserve(prev_num_instance_layers + num_instance_layers); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 222 | for (size_t i = 0; i < num_instance_layers; i++) { |
| 223 | const VkLayerProperties& props = properties[i]; |
| 224 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 225 | Layer layer; |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 226 | layer.properties = props; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 227 | layer.library_idx = library_idx; |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 228 | layer.is_global = false; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 229 | |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 230 | uint32_t count = 0; |
| 231 | result = |
| 232 | enumerate_instance_extensions(props.layerName, &count, nullptr); |
| 233 | if (result != VK_SUCCESS) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 234 | ALOGE( |
| 235 | "vkEnumerateInstanceExtensionProperties(\"%s\") failed for " |
| 236 | "library '%s': %d", |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 237 | props.layerName, path_.c_str(), result); |
| 238 | instance_layers.resize(prev_num_instance_layers); |
| 239 | return false; |
| 240 | } |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 241 | layer.instance_extensions.resize(count); |
| 242 | result = enumerate_instance_extensions( |
| 243 | props.layerName, &count, layer.instance_extensions.data()); |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 244 | if (result != VK_SUCCESS) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 245 | ALOGE( |
| 246 | "vkEnumerateInstanceExtensionProperties(\"%s\") failed for " |
| 247 | "library '%s': %d", |
Chia-I Wu | 5f093bf | 2016-04-19 12:22:52 +0800 | [diff] [blame] | 248 | props.layerName, path_.c_str(), result); |
| 249 | instance_layers.resize(prev_num_instance_layers); |
| 250 | return false; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Chia-I Wu | 279ccc0 | 2016-04-18 16:45:15 +0800 | [diff] [blame] | 253 | for (size_t j = 0; j < num_device_layers; j++) { |
| 254 | const auto& dev_props = properties[num_instance_layers + j]; |
| 255 | if (memcmp(&props, &dev_props, sizeof(props)) == 0) { |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 256 | layer.is_global = true; |
Chia-I Wu | 279ccc0 | 2016-04-18 16:45:15 +0800 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | } |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 260 | |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 261 | if (layer.is_global && enumerate_device_extensions) { |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 262 | result = enumerate_device_extensions( |
| 263 | VK_NULL_HANDLE, props.layerName, &count, nullptr); |
| 264 | if (result != VK_SUCCESS) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 265 | ALOGE( |
| 266 | "vkEnumerateDeviceExtensionProperties(\"%s\") failed for " |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 267 | "library '%s': %d", |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 268 | props.layerName, path_.c_str(), result); |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 269 | instance_layers.resize(prev_num_instance_layers); |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 270 | return false; |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 271 | } |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 272 | layer.device_extensions.resize(count); |
| 273 | result = enumerate_device_extensions( |
| 274 | VK_NULL_HANDLE, props.layerName, &count, |
| 275 | layer.device_extensions.data()); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 276 | if (result != VK_SUCCESS) { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 277 | ALOGE( |
| 278 | "vkEnumerateDeviceExtensionProperties(\"%s\") failed for " |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 279 | "library '%s': %d", |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 280 | props.layerName, path_.c_str(), result); |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 281 | instance_layers.resize(prev_num_instance_layers); |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 282 | return false; |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 286 | instance_layers.push_back(layer); |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 287 | ALOGD("added %s layer '%s' from library '%s'", |
| 288 | (layer.is_global) ? "global" : "instance", props.layerName, |
| 289 | path_.c_str()); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 292 | return true; |
| 293 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 294 | |
Chia-I Wu | ba11327 | 2016-04-18 16:38:39 +0800 | [diff] [blame] | 295 | void* LayerLibrary::GetGPA(const Layer& layer, |
| 296 | const char* gpa_name, |
| 297 | size_t gpa_name_len) const { |
| 298 | void* gpa; |
| 299 | size_t layer_name_len = |
| 300 | std::max(size_t{2}, strlen(layer.properties.layerName)); |
| 301 | char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1)); |
| 302 | strcpy(name, layer.properties.layerName); |
| 303 | strcpy(name + layer_name_len, gpa_name); |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 304 | if (!(gpa = dlsym(dlhandle_, name))) { |
Chia-I Wu | ba11327 | 2016-04-18 16:38:39 +0800 | [diff] [blame] | 305 | strcpy(name, "vk"); |
| 306 | strcpy(name + 2, gpa_name); |
Chia-I Wu | 6693f5c | 2016-04-18 12:20:02 +0800 | [diff] [blame] | 307 | gpa = dlsym(dlhandle_, name); |
Chia-I Wu | ba11327 | 2016-04-18 16:38:39 +0800 | [diff] [blame] | 308 | } |
| 309 | return gpa; |
| 310 | } |
| 311 | |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 312 | // ---------------------------------------------------------------------------- |
| 313 | |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 314 | std::vector<LayerLibrary> g_layer_libraries; |
| 315 | std::vector<Layer> g_instance_layers; |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 316 | |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame^] | 317 | void AddLayerLibrary(const std::string& path, const std::string& filename) { |
| 318 | LayerLibrary library(path + "/" + filename, filename); |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 319 | if (!library.Open()) |
| 320 | return; |
| 321 | |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 322 | if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers)) { |
Chia-I Wu | 50174ee | 2016-04-18 16:33:20 +0800 | [diff] [blame] | 323 | library.Close(); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | library.Close(); |
| 328 | |
| 329 | g_layer_libraries.emplace_back(std::move(library)); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 332 | template <typename Functor> |
| 333 | void ForEachFileInDir(const std::string& dirname, Functor functor) { |
| 334 | auto dir_deleter = [](DIR* handle) { closedir(handle); }; |
| 335 | std::unique_ptr<DIR, decltype(dir_deleter)> dir(opendir(dirname.c_str()), |
| 336 | dir_deleter); |
| 337 | if (!dir) { |
| 338 | // It's normal for some search directories to not exist, especially |
| 339 | // /data/local/debug/vulkan. |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 340 | int err = errno; |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 341 | ALOGW_IF(err != ENOENT, "failed to open layer directory '%s': %s", |
| 342 | dirname.c_str(), strerror(err)); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 343 | return; |
| 344 | } |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 345 | ALOGD("searching for layers in '%s'", dirname.c_str()); |
| 346 | dirent* entry; |
| 347 | while ((entry = readdir(dir.get())) != nullptr) |
| 348 | functor(entry->d_name); |
| 349 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 350 | |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 351 | template <typename Functor> |
| 352 | void ForEachFileInZip(const std::string& zipname, |
| 353 | const std::string& dir_in_zip, |
| 354 | Functor functor) { |
| 355 | int32_t err; |
| 356 | ZipArchiveHandle zip = nullptr; |
| 357 | if ((err = OpenArchive(zipname.c_str(), &zip)) != 0) { |
| 358 | ALOGE("failed to open apk '%s': %d", zipname.c_str(), err); |
| 359 | return; |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 360 | } |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 361 | std::string prefix(dir_in_zip + "/"); |
| 362 | const ZipString prefix_str(prefix.c_str()); |
| 363 | void* iter_cookie = nullptr; |
| 364 | if ((err = StartIteration(zip, &iter_cookie, &prefix_str, nullptr)) != 0) { |
| 365 | ALOGE("failed to iterate entries in apk '%s': %d", zipname.c_str(), |
| 366 | err); |
| 367 | CloseArchive(zip); |
| 368 | return; |
| 369 | } |
| 370 | ALOGD("searching for layers in '%s!/%s'", zipname.c_str(), |
| 371 | dir_in_zip.c_str()); |
| 372 | ZipEntry entry; |
| 373 | ZipString name; |
| 374 | while (Next(iter_cookie, &entry, &name) == 0) { |
| 375 | std::string filename( |
| 376 | reinterpret_cast<const char*>(name.name) + prefix.length(), |
| 377 | name.name_length - prefix.length()); |
| 378 | // only enumerate direct entries of the directory, not subdirectories |
Jesse Hall | 8c6c10e | 2016-05-19 10:58:35 -0700 | [diff] [blame] | 379 | if (filename.find('/') != filename.npos) |
| 380 | continue; |
| 381 | // Check whether it *may* be possible to load the library directly from |
| 382 | // the APK. Loading still may fail for other reasons, but this at least |
| 383 | // lets us avoid failed-to-load log messages in the typical case of |
| 384 | // compressed and/or unaligned libraries. |
| 385 | if (entry.method != kCompressStored || entry.offset % PAGE_SIZE != 0) |
| 386 | continue; |
| 387 | functor(filename); |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 388 | } |
| 389 | EndIteration(iter_cookie); |
| 390 | CloseArchive(zip); |
| 391 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 392 | |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 393 | template <typename Functor> |
| 394 | void ForEachFileInPath(const std::string& path, Functor functor) { |
| 395 | size_t zip_pos = path.find("!/"); |
| 396 | if (zip_pos == std::string::npos) { |
| 397 | ForEachFileInDir(path, functor); |
| 398 | } else { |
| 399 | ForEachFileInZip(path.substr(0, zip_pos), path.substr(zip_pos + 2), |
| 400 | functor); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void DiscoverLayersInPathList(const std::string& pathstr) { |
| 405 | std::vector<std::string> paths = android::base::Split(pathstr, ":"); |
| 406 | for (const auto& path : paths) { |
| 407 | ForEachFileInPath(path, [&](const std::string& filename) { |
| 408 | if (android::base::StartsWith(filename, "libVkLayer") && |
| 409 | android::base::EndsWith(filename, ".so")) { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame^] | 410 | |
| 411 | // Check to ensure we haven't seen this layer already |
| 412 | // Let the first instance of the shared object be enumerated |
| 413 | // We're searching for layers in following order: |
| 414 | // 1. system path |
| 415 | // 2. libraryPermittedPath (if enabled) |
| 416 | // 3. libraryPath |
| 417 | |
| 418 | bool duplicate = false; |
| 419 | for (auto& layer : g_layer_libraries) { |
| 420 | if (layer.GetFilename() == filename) { |
| 421 | ALOGV("Skipping duplicate layer %s in %s", |
| 422 | filename.c_str(), path.c_str()); |
| 423 | duplicate = true; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if (!duplicate) |
| 428 | AddLayerLibrary(path, filename); |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 429 | } |
| 430 | }); |
| 431 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 434 | const VkExtensionProperties* FindExtension( |
| 435 | const std::vector<VkExtensionProperties>& extensions, |
| 436 | const char* name) { |
| 437 | auto it = std::find_if(extensions.cbegin(), extensions.cend(), |
| 438 | [=](const VkExtensionProperties& ext) { |
| 439 | return (strcmp(ext.extensionName, name) == 0); |
| 440 | }); |
| 441 | return (it != extensions.cend()) ? &*it : nullptr; |
| 442 | } |
| 443 | |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 444 | void* GetLayerGetProcAddr(const Layer& layer, |
| 445 | const char* gpa_name, |
| 446 | size_t gpa_name_len) { |
| 447 | const LayerLibrary& library = g_layer_libraries[layer.library_idx]; |
Chia-I Wu | ba11327 | 2016-04-18 16:38:39 +0800 | [diff] [blame] | 448 | return library.GetGPA(layer, gpa_name, gpa_name_len); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 451 | } // anonymous namespace |
| 452 | |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 453 | void DiscoverLayers() { |
Jesse Hall | 1a7eb59 | 2016-05-01 21:04:40 +0200 | [diff] [blame] | 454 | if (property_get_bool("ro.debuggable", false) && |
| 455 | prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) { |
| 456 | DiscoverLayersInPathList(kSystemLayerLibraryDir); |
| 457 | } |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame^] | 458 | if (!android::GraphicsEnv::getInstance().getLayerPaths().empty()) |
| 459 | DiscoverLayersInPathList(android::GraphicsEnv::getInstance().getLayerPaths()); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 462 | uint32_t GetLayerCount() { |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 463 | return static_cast<uint32_t>(g_instance_layers.size()); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 466 | const Layer& GetLayer(uint32_t index) { |
| 467 | return g_instance_layers[index]; |
| 468 | } |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 469 | |
Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 470 | const Layer* FindLayer(const char* name) { |
| 471 | auto layer = |
| 472 | std::find_if(g_instance_layers.cbegin(), g_instance_layers.cend(), |
| 473 | [=](const Layer& entry) { |
| 474 | return strcmp(entry.properties.layerName, name) == 0; |
| 475 | }); |
| 476 | return (layer != g_instance_layers.cend()) ? &*layer : nullptr; |
| 477 | } |
| 478 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 479 | const VkLayerProperties& GetLayerProperties(const Layer& layer) { |
| 480 | return layer.properties; |
| 481 | } |
Chia-I Wu | bea09db | 2016-04-22 09:42:41 +0800 | [diff] [blame] | 482 | |
Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 483 | bool IsLayerGlobal(const Layer& layer) { |
| 484 | return layer.is_global; |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 487 | const VkExtensionProperties* GetLayerInstanceExtensions(const Layer& layer, |
| 488 | uint32_t& count) { |
| 489 | count = static_cast<uint32_t>(layer.instance_extensions.size()); |
| 490 | return layer.instance_extensions.data(); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 493 | const VkExtensionProperties* GetLayerDeviceExtensions(const Layer& layer, |
| 494 | uint32_t& count) { |
| 495 | count = static_cast<uint32_t>(layer.device_extensions.size()); |
| 496 | return layer.device_extensions.data(); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 499 | const VkExtensionProperties* FindLayerInstanceExtension(const Layer& layer, |
| 500 | const char* name) { |
| 501 | return FindExtension(layer.instance_extensions, name); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 504 | const VkExtensionProperties* FindLayerDeviceExtension(const Layer& layer, |
| 505 | const char* name) { |
| 506 | return FindExtension(layer.device_extensions, name); |
Jesse Hall | aa41094 | 2016-01-17 13:07:10 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 509 | LayerRef GetLayerRef(const Layer& layer) { |
| 510 | LayerLibrary& library = g_layer_libraries[layer.library_idx]; |
| 511 | return LayerRef((library.Open()) ? &layer : nullptr); |
| 512 | } |
| 513 | |
| 514 | LayerRef::LayerRef(const Layer* layer) : layer_(layer) {} |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 515 | |
| 516 | LayerRef::~LayerRef() { |
| 517 | if (layer_) { |
| 518 | LayerLibrary& library = g_layer_libraries[layer_->library_idx]; |
Chia-I Wu | d91c74f | 2016-04-18 12:12:36 +0800 | [diff] [blame] | 519 | library.Close(); |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 523 | LayerRef::LayerRef(LayerRef&& other) : layer_(other.layer_) { |
Michael Lentine | 54e6f08 | 2016-01-20 11:25:28 -0600 | [diff] [blame] | 524 | other.layer_ = nullptr; |
| 525 | } |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 526 | |
| 527 | PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const { |
| 528 | return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>( |
| 529 | GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19)) |
| 530 | : nullptr; |
| 531 | } |
| 532 | |
| 533 | PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const { |
| 534 | return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>( |
| 535 | GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17)) |
| 536 | : nullptr; |
| 537 | } |
| 538 | |
Chia-I Wu | c96880f | 2016-03-26 06:56:45 +0800 | [diff] [blame] | 539 | } // namespace api |
Jesse Hall | 80523e2 | 2016-01-06 16:47:54 -0800 | [diff] [blame] | 540 | } // namespace vulkan |