layers_extensions.cpp revision 5f093bf18120a5cbf18d0f3e255b2178f524e438
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 17// #define LOG_NDEBUG 0 18 19#include "layers_extensions.h" 20#include <alloca.h> 21#include <dirent.h> 22#include <dlfcn.h> 23#include <mutex> 24#include <sys/prctl.h> 25#include <string> 26#include <string.h> 27#include <vector> 28#include <log/log.h> 29#include <vulkan/vulkan_loader_data.h> 30 31// TODO(jessehall): The whole way we deal with extensions is pretty hokey, and 32// not a good long-term solution. Having a hard-coded enum of extensions is 33// bad, of course. Representing sets of extensions (requested, supported, etc.) 34// as a bitset isn't necessarily bad, if the mapping from extension to bit were 35// dynamic. Need to rethink this completely when there's a little more time. 36 37// TODO(jessehall): This file currently builds up global data structures as it 38// loads, and never cleans them up. This means we're doing heap allocations 39// without going through an app-provided allocator, but worse, we'll leak those 40// allocations if the loader is unloaded. 41// 42// We should allocate "enough" BSS space, and suballocate from there. Will 43// probably want to intern strings, etc., and will need some custom/manual data 44// structures. 45 46// TODO(jessehall): Currently we have separate lists for instance and device 47// layers. Most layers are both; we should use one entry for each layer name, 48// with a mask saying what kind(s) it is. 49 50namespace vulkan { 51namespace api { 52 53struct Layer { 54 VkLayerProperties properties; 55 size_t library_idx; 56 std::vector<VkExtensionProperties> extensions; 57}; 58 59namespace { 60 61class LayerLibrary { 62 public: 63 LayerLibrary(const std::string& path) 64 : path_(path), dlhandle_(nullptr), refcount_(0) {} 65 66 LayerLibrary(LayerLibrary&& other) 67 : path_(std::move(other.path_)), 68 dlhandle_(other.dlhandle_), 69 refcount_(other.refcount_) { 70 other.dlhandle_ = nullptr; 71 other.refcount_ = 0; 72 } 73 74 LayerLibrary(const LayerLibrary&) = delete; 75 LayerLibrary& operator=(const LayerLibrary&) = delete; 76 77 // these are thread-safe 78 bool Open(); 79 void Close(); 80 81 bool EnumerateLayers(size_t library_idx, 82 std::vector<Layer>& instance_layers, 83 std::vector<Layer>& device_layers) const; 84 85 void* GetGPA(const Layer& layer, 86 const char* gpa_name, 87 size_t gpa_name_len) const; 88 89 private: 90 const std::string path_; 91 92 std::mutex mutex_; 93 void* dlhandle_; 94 size_t refcount_; 95}; 96 97bool LayerLibrary::Open() { 98 std::lock_guard<std::mutex> lock(mutex_); 99 100 if (refcount_++ == 0) { 101 dlhandle_ = dlopen(path_.c_str(), RTLD_NOW | RTLD_LOCAL); 102 ALOGV("Opening library %s", path_.c_str()); 103 if (!dlhandle_) { 104 ALOGE("failed to load layer library '%s': %s", path_.c_str(), 105 dlerror()); 106 refcount_ = 0; 107 return false; 108 } 109 } 110 ALOGV("Refcount on activate is %zu", refcount_); 111 return true; 112} 113 114void LayerLibrary::Close() { 115 std::lock_guard<std::mutex> lock(mutex_); 116 117 if (--refcount_ == 0) { 118 ALOGV("Closing library %s", path_.c_str()); 119 dlclose(dlhandle_); 120 dlhandle_ = nullptr; 121 } 122 ALOGV("Refcount on destruction is %zu", refcount_); 123} 124 125bool LayerLibrary::EnumerateLayers(size_t library_idx, 126 std::vector<Layer>& instance_layers, 127 std::vector<Layer>& device_layers) const { 128 PFN_vkEnumerateInstanceLayerProperties enumerate_instance_layers = 129 reinterpret_cast<PFN_vkEnumerateInstanceLayerProperties>( 130 dlsym(dlhandle_, "vkEnumerateInstanceLayerProperties")); 131 PFN_vkEnumerateInstanceExtensionProperties enumerate_instance_extensions = 132 reinterpret_cast<PFN_vkEnumerateInstanceExtensionProperties>( 133 dlsym(dlhandle_, "vkEnumerateInstanceExtensionProperties")); 134 if (!enumerate_instance_layers || !enumerate_instance_extensions) { 135 ALOGV("layer library '%s' misses some instance enumeraion functions", 136 path_.c_str()); 137 return false; 138 } 139 140 // device functions are optional 141 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers = 142 reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>( 143 dlsym(dlhandle_, "vkEnumerateDeviceLayerProperties")); 144 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions = 145 reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>( 146 dlsym(dlhandle_, "vkEnumerateDeviceExtensionProperties")); 147 148 // get layer counts 149 uint32_t num_instance_layers = 0; 150 uint32_t num_device_layers = 0; 151 VkResult result = enumerate_instance_layers(&num_instance_layers, nullptr); 152 if (result != VK_SUCCESS || !num_instance_layers) { 153 if (result != VK_SUCCESS) { 154 ALOGW( 155 "vkEnumerateInstanceLayerProperties failed for library '%s': " 156 "%d", 157 path_.c_str(), result); 158 } 159 return false; 160 } 161 if (enumerate_device_layers) { 162 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers, 163 nullptr); 164 if (result != VK_SUCCESS) { 165 ALOGW( 166 "vkEnumerateDeviceLayerProperties failed for library '%s': %d", 167 path_.c_str(), result); 168 return false; 169 } 170 } 171 172 // get layer properties 173 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca( 174 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties))); 175 result = enumerate_instance_layers(&num_instance_layers, properties); 176 if (result != VK_SUCCESS) { 177 ALOGW("vkEnumerateInstanceLayerProperties failed for library '%s': %d", 178 path_.c_str(), result); 179 return false; 180 } 181 if (num_device_layers > 0) { 182 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers, 183 properties + num_instance_layers); 184 if (result != VK_SUCCESS) { 185 ALOGW( 186 "vkEnumerateDeviceLayerProperties failed for library '%s': %d", 187 path_.c_str(), result); 188 return false; 189 } 190 } 191 192 // append layers to instance_layers/device_layers 193 size_t prev_num_instance_layers = instance_layers.size(); 194 size_t prev_num_device_layers = device_layers.size(); 195 instance_layers.reserve(prev_num_instance_layers + num_instance_layers); 196 device_layers.reserve(prev_num_device_layers + num_device_layers); 197 for (size_t i = 0; i < num_instance_layers; i++) { 198 const VkLayerProperties& props = properties[i]; 199 200 Layer layer; 201 layer.properties = props; 202 layer.library_idx = library_idx; 203 204 uint32_t count = 0; 205 result = 206 enumerate_instance_extensions(props.layerName, &count, nullptr); 207 if (result != VK_SUCCESS) { 208 ALOGW( 209 "vkEnumerateInstanceExtensionProperties(%s) failed for library " 210 "'%s': %d", 211 props.layerName, path_.c_str(), result); 212 instance_layers.resize(prev_num_instance_layers); 213 return false; 214 } 215 layer.extensions.resize(count); 216 result = enumerate_instance_extensions(props.layerName, &count, 217 layer.extensions.data()); 218 if (result != VK_SUCCESS) { 219 ALOGW( 220 "vkEnumerateInstanceExtensionProperties(%s) failed for library " 221 "'%s': %d", 222 props.layerName, path_.c_str(), result); 223 instance_layers.resize(prev_num_instance_layers); 224 return false; 225 } 226 227 instance_layers.push_back(layer); 228 ALOGV(" added instance layer '%s'", props.layerName); 229 } 230 for (size_t i = 0; i < num_device_layers; i++) { 231 const VkLayerProperties& props = properties[num_instance_layers + i]; 232 233 Layer layer; 234 layer.properties = props; 235 layer.library_idx = library_idx; 236 237 if (enumerate_device_extensions) { 238 uint32_t count; 239 result = enumerate_device_extensions( 240 VK_NULL_HANDLE, props.layerName, &count, nullptr); 241 if (result != VK_SUCCESS) { 242 ALOGW( 243 "vkEnumerateDeviceExtensionProperties(%s) failed for " 244 "library '%s': %d", 245 props.layerName, path_.c_str(), result); 246 instance_layers.resize(prev_num_instance_layers); 247 device_layers.resize(prev_num_device_layers); 248 return false; 249 } 250 layer.extensions.resize(count); 251 result = 252 enumerate_device_extensions(VK_NULL_HANDLE, props.layerName, 253 &count, layer.extensions.data()); 254 if (result != VK_SUCCESS) { 255 ALOGW( 256 "vkEnumerateDeviceExtensionProperties(%s) failed for " 257 "library '%s': %d", 258 props.layerName, path_.c_str(), result); 259 instance_layers.resize(prev_num_instance_layers); 260 device_layers.resize(prev_num_device_layers); 261 return false; 262 } 263 } 264 265 device_layers.push_back(layer); 266 ALOGV(" added device layer '%s'", props.layerName); 267 } 268 269 return true; 270} 271 272void* LayerLibrary::GetGPA(const Layer& layer, 273 const char* gpa_name, 274 size_t gpa_name_len) const { 275 void* gpa; 276 size_t layer_name_len = 277 std::max(size_t{2}, strlen(layer.properties.layerName)); 278 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1)); 279 strcpy(name, layer.properties.layerName); 280 strcpy(name + layer_name_len, gpa_name); 281 if (!(gpa = dlsym(dlhandle_, name))) { 282 strcpy(name, "vk"); 283 strcpy(name + 2, gpa_name); 284 gpa = dlsym(dlhandle_, name); 285 } 286 return gpa; 287} 288 289std::vector<LayerLibrary> g_layer_libraries; 290std::vector<Layer> g_instance_layers; 291std::vector<Layer> g_device_layers; 292 293void AddLayerLibrary(const std::string& path) { 294 ALOGV("examining layer library '%s'", path.c_str()); 295 296 LayerLibrary library(path); 297 if (!library.Open()) 298 return; 299 300 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers, 301 g_device_layers)) { 302 library.Close(); 303 return; 304 } 305 306 library.Close(); 307 308 g_layer_libraries.emplace_back(std::move(library)); 309} 310 311void DiscoverLayersInDirectory(const std::string& dir_path) { 312 ALOGV("looking for layers in '%s'", dir_path.c_str()); 313 314 DIR* directory = opendir(dir_path.c_str()); 315 if (!directory) { 316 int err = errno; 317 ALOGV_IF(err != ENOENT, "failed to open layer directory '%s': %s (%d)", 318 dir_path.c_str(), strerror(err), err); 319 return; 320 } 321 322 std::string path; 323 path.reserve(dir_path.size() + 20); 324 path.append(dir_path); 325 path.append("/"); 326 327 struct dirent* entry; 328 while ((entry = readdir(directory))) { 329 size_t libname_len = strlen(entry->d_name); 330 if (strncmp(entry->d_name, "libVkLayer", 10) != 0 || 331 strncmp(entry->d_name + libname_len - 3, ".so", 3) != 0) 332 continue; 333 path.append(entry->d_name); 334 AddLayerLibrary(path); 335 path.resize(dir_path.size() + 1); 336 } 337 338 closedir(directory); 339} 340 341const Layer* FindLayer(const std::vector<Layer>& layers, const char* name) { 342 auto layer = 343 std::find_if(layers.cbegin(), layers.cend(), [=](const Layer& entry) { 344 return strcmp(entry.properties.layerName, name) == 0; 345 }); 346 return (layer != layers.cend()) ? &*layer : nullptr; 347} 348 349void* GetLayerGetProcAddr(const Layer& layer, 350 const char* gpa_name, 351 size_t gpa_name_len) { 352 const LayerLibrary& library = g_layer_libraries[layer.library_idx]; 353 return library.GetGPA(layer, gpa_name, gpa_name_len); 354} 355 356uint32_t EnumerateLayers(const std::vector<Layer>& layers, 357 uint32_t count, 358 VkLayerProperties* properties) { 359 uint32_t n = std::min(count, static_cast<uint32_t>(layers.size())); 360 for (uint32_t i = 0; i < n; i++) { 361 properties[i] = layers[i].properties; 362 } 363 return static_cast<uint32_t>(layers.size()); 364} 365 366void GetLayerExtensions(const std::vector<Layer>& layers, 367 const char* name, 368 const VkExtensionProperties** properties, 369 uint32_t* count) { 370 const Layer* layer = FindLayer(layers, name); 371 if (layer) { 372 *properties = layer->extensions.data(); 373 *count = static_cast<uint32_t>(layer->extensions.size()); 374 } else { 375 *properties = nullptr; 376 *count = 0; 377 } 378} 379 380LayerRef GetLayerRef(std::vector<Layer>& layers, const char* name) { 381 const Layer* layer = FindLayer(layers, name); 382 if (layer) { 383 LayerLibrary& library = g_layer_libraries[layer->library_idx]; 384 if (!library.Open()) 385 layer = nullptr; 386 } 387 388 return LayerRef(layer); 389} 390 391} // anonymous namespace 392 393void DiscoverLayers() { 394 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) 395 DiscoverLayersInDirectory("/data/local/debug/vulkan"); 396 if (!LoaderData::GetInstance().layer_path.empty()) 397 DiscoverLayersInDirectory(LoaderData::GetInstance().layer_path.c_str()); 398} 399 400uint32_t EnumerateInstanceLayers(uint32_t count, 401 VkLayerProperties* properties) { 402 return EnumerateLayers(g_instance_layers, count, properties); 403} 404 405uint32_t EnumerateDeviceLayers(uint32_t count, VkLayerProperties* properties) { 406 return EnumerateLayers(g_device_layers, count, properties); 407} 408 409void GetInstanceLayerExtensions(const char* name, 410 const VkExtensionProperties** properties, 411 uint32_t* count) { 412 GetLayerExtensions(g_instance_layers, name, properties, count); 413} 414 415void GetDeviceLayerExtensions(const char* name, 416 const VkExtensionProperties** properties, 417 uint32_t* count) { 418 GetLayerExtensions(g_device_layers, name, properties, count); 419} 420 421LayerRef GetInstanceLayerRef(const char* name) { 422 return GetLayerRef(g_instance_layers, name); 423} 424 425LayerRef GetDeviceLayerRef(const char* name) { 426 return GetLayerRef(g_device_layers, name); 427} 428 429LayerRef::LayerRef(const Layer* layer) : layer_(layer) {} 430 431LayerRef::~LayerRef() { 432 if (layer_) { 433 LayerLibrary& library = g_layer_libraries[layer_->library_idx]; 434 library.Close(); 435 } 436} 437 438const char* LayerRef::GetName() const { 439 return layer_->properties.layerName; 440} 441 442uint32_t LayerRef::GetSpecVersion() const { 443 return layer_->properties.specVersion; 444} 445 446LayerRef::LayerRef(LayerRef&& other) : layer_(std::move(other.layer_)) { 447 other.layer_ = nullptr; 448} 449 450PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const { 451 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>( 452 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19)) 453 : nullptr; 454} 455 456PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const { 457 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>( 458 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17)) 459 : nullptr; 460} 461 462bool LayerRef::SupportsExtension(const char* name) const { 463 return std::find_if(layer_->extensions.cbegin(), layer_->extensions.cend(), 464 [=](const VkExtensionProperties& ext) { 465 return strcmp(ext.extensionName, name) == 0; 466 }) != layer_->extensions.cend(); 467} 468 469} // namespace api 470} // namespace vulkan 471