layers_extensions.cpp revision 50db9035b034385a27fb557e4da3c48b51a0e3eb
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 PFN_vkEnumerateDeviceLayerProperties enumerate_device_layers = 135 reinterpret_cast<PFN_vkEnumerateDeviceLayerProperties>( 136 dlsym(dlhandle_, "vkEnumerateDeviceLayerProperties")); 137 PFN_vkEnumerateDeviceExtensionProperties enumerate_device_extensions = 138 reinterpret_cast<PFN_vkEnumerateDeviceExtensionProperties>( 139 dlsym(dlhandle_, "vkEnumerateDeviceExtensionProperties")); 140 if (!((enumerate_instance_layers && enumerate_instance_extensions) || 141 (enumerate_device_layers && enumerate_device_extensions))) { 142 ALOGV( 143 "layer library '%s' has neither instance nor device enumeraion " 144 "functions", 145 path_.c_str()); 146 return false; 147 } 148 149 VkResult result; 150 uint32_t num_instance_layers = 0; 151 uint32_t num_device_layers = 0; 152 if (enumerate_instance_layers) { 153 result = enumerate_instance_layers(&num_instance_layers, nullptr); 154 if (result != VK_SUCCESS) { 155 ALOGW( 156 "vkEnumerateInstanceLayerProperties failed for library '%s': " 157 "%d", 158 path_.c_str(), result); 159 return false; 160 } 161 } 162 if (enumerate_device_layers) { 163 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers, 164 nullptr); 165 if (result != VK_SUCCESS) { 166 ALOGW( 167 "vkEnumerateDeviceLayerProperties failed for library '%s': %d", 168 path_.c_str(), result); 169 return false; 170 } 171 } 172 VkLayerProperties* properties = static_cast<VkLayerProperties*>(alloca( 173 (num_instance_layers + num_device_layers) * sizeof(VkLayerProperties))); 174 if (num_instance_layers > 0) { 175 result = enumerate_instance_layers(&num_instance_layers, properties); 176 if (result != VK_SUCCESS) { 177 ALOGW( 178 "vkEnumerateInstanceLayerProperties failed for library '%s': " 179 "%d", 180 path_.c_str(), result); 181 return false; 182 } 183 } 184 if (num_device_layers > 0) { 185 result = enumerate_device_layers(VK_NULL_HANDLE, &num_device_layers, 186 properties + num_instance_layers); 187 if (result != VK_SUCCESS) { 188 ALOGW( 189 "vkEnumerateDeviceLayerProperties failed for library '%s': %d", 190 path_.c_str(), result); 191 return false; 192 } 193 } 194 195 size_t prev_num_instance_layers = instance_layers.size(); 196 size_t prev_num_device_layers = device_layers.size(); 197 instance_layers.reserve(prev_num_instance_layers + num_instance_layers); 198 device_layers.reserve(prev_num_device_layers + num_device_layers); 199 for (size_t i = 0; i < num_instance_layers; i++) { 200 const VkLayerProperties& props = properties[i]; 201 202 Layer layer; 203 layer.properties = props; 204 layer.library_idx = library_idx; 205 206 if (enumerate_instance_extensions) { 207 uint32_t count = 0; 208 result = 209 enumerate_instance_extensions(props.layerName, &count, nullptr); 210 if (result != VK_SUCCESS) { 211 ALOGW( 212 "vkEnumerateInstanceExtensionProperties(%s) failed for " 213 "library '%s': %d", 214 props.layerName, path_.c_str(), result); 215 instance_layers.resize(prev_num_instance_layers); 216 return false; 217 } 218 layer.extensions.resize(count); 219 result = enumerate_instance_extensions(props.layerName, &count, 220 layer.extensions.data()); 221 if (result != VK_SUCCESS) { 222 ALOGW( 223 "vkEnumerateInstanceExtensionProperties(%s) failed for " 224 "library '%s': %d", 225 props.layerName, path_.c_str(), result); 226 instance_layers.resize(prev_num_instance_layers); 227 return false; 228 } 229 } 230 231 instance_layers.push_back(layer); 232 ALOGV(" added instance layer '%s'", props.layerName); 233 } 234 for (size_t i = 0; i < num_device_layers; i++) { 235 const VkLayerProperties& props = properties[num_instance_layers + i]; 236 237 Layer layer; 238 layer.properties = props; 239 layer.library_idx = library_idx; 240 241 if (enumerate_device_extensions) { 242 uint32_t count; 243 result = enumerate_device_extensions( 244 VK_NULL_HANDLE, props.layerName, &count, nullptr); 245 if (result != VK_SUCCESS) { 246 ALOGW( 247 "vkEnumerateDeviceExtensionProperties(%s) failed for " 248 "library '%s': %d", 249 props.layerName, path_.c_str(), result); 250 instance_layers.resize(prev_num_instance_layers); 251 device_layers.resize(prev_num_device_layers); 252 return false; 253 } 254 layer.extensions.resize(count); 255 result = 256 enumerate_device_extensions(VK_NULL_HANDLE, props.layerName, 257 &count, layer.extensions.data()); 258 if (result != VK_SUCCESS) { 259 ALOGW( 260 "vkEnumerateDeviceExtensionProperties(%s) failed for " 261 "library '%s': %d", 262 props.layerName, path_.c_str(), result); 263 instance_layers.resize(prev_num_instance_layers); 264 device_layers.resize(prev_num_device_layers); 265 return false; 266 } 267 } 268 269 device_layers.push_back(layer); 270 ALOGV(" added device layer '%s'", props.layerName); 271 } 272 273 return true; 274} 275 276void* LayerLibrary::GetGPA(const Layer& layer, 277 const char* gpa_name, 278 size_t gpa_name_len) const { 279 void* gpa; 280 size_t layer_name_len = 281 std::max(size_t{2}, strlen(layer.properties.layerName)); 282 char* name = static_cast<char*>(alloca(layer_name_len + gpa_name_len + 1)); 283 strcpy(name, layer.properties.layerName); 284 strcpy(name + layer_name_len, gpa_name); 285 if (!(gpa = dlsym(dlhandle_, name))) { 286 strcpy(name, "vk"); 287 strcpy(name + 2, gpa_name); 288 gpa = dlsym(dlhandle_, name); 289 } 290 return gpa; 291} 292 293std::vector<LayerLibrary> g_layer_libraries; 294std::vector<Layer> g_instance_layers; 295std::vector<Layer> g_device_layers; 296 297void AddLayerLibrary(const std::string& path) { 298 ALOGV("examining layer library '%s'", path.c_str()); 299 300 LayerLibrary library(path); 301 if (!library.Open()) 302 return; 303 304 if (!library.EnumerateLayers(g_layer_libraries.size(), g_instance_layers, 305 g_device_layers)) { 306 library.Close(); 307 return; 308 } 309 310 library.Close(); 311 312 g_layer_libraries.emplace_back(std::move(library)); 313} 314 315void DiscoverLayersInDirectory(const std::string& dir_path) { 316 ALOGV("looking for layers in '%s'", dir_path.c_str()); 317 318 DIR* directory = opendir(dir_path.c_str()); 319 if (!directory) { 320 int err = errno; 321 ALOGV_IF(err != ENOENT, "failed to open layer directory '%s': %s (%d)", 322 dir_path.c_str(), strerror(err), err); 323 return; 324 } 325 326 std::string path; 327 path.reserve(dir_path.size() + 20); 328 path.append(dir_path); 329 path.append("/"); 330 331 struct dirent* entry; 332 while ((entry = readdir(directory))) { 333 size_t libname_len = strlen(entry->d_name); 334 if (strncmp(entry->d_name, "libVkLayer", 10) != 0 || 335 strncmp(entry->d_name + libname_len - 3, ".so", 3) != 0) 336 continue; 337 path.append(entry->d_name); 338 AddLayerLibrary(path); 339 path.resize(dir_path.size() + 1); 340 } 341 342 closedir(directory); 343} 344 345const Layer* FindLayer(const std::vector<Layer>& layers, const char* name) { 346 auto layer = 347 std::find_if(layers.cbegin(), layers.cend(), [=](const Layer& entry) { 348 return strcmp(entry.properties.layerName, name) == 0; 349 }); 350 return (layer != layers.cend()) ? &*layer : nullptr; 351} 352 353void* GetLayerGetProcAddr(const Layer& layer, 354 const char* gpa_name, 355 size_t gpa_name_len) { 356 const LayerLibrary& library = g_layer_libraries[layer.library_idx]; 357 return library.GetGPA(layer, gpa_name, gpa_name_len); 358} 359 360uint32_t EnumerateLayers(const std::vector<Layer>& layers, 361 uint32_t count, 362 VkLayerProperties* properties) { 363 uint32_t n = std::min(count, static_cast<uint32_t>(layers.size())); 364 for (uint32_t i = 0; i < n; i++) { 365 properties[i] = layers[i].properties; 366 } 367 return static_cast<uint32_t>(layers.size()); 368} 369 370void GetLayerExtensions(const std::vector<Layer>& layers, 371 const char* name, 372 const VkExtensionProperties** properties, 373 uint32_t* count) { 374 const Layer* layer = FindLayer(layers, name); 375 if (layer) { 376 *properties = layer->extensions.data(); 377 *count = static_cast<uint32_t>(layer->extensions.size()); 378 } else { 379 *properties = nullptr; 380 *count = 0; 381 } 382} 383 384LayerRef GetLayerRef(std::vector<Layer>& layers, const char* name) { 385 const Layer* layer = FindLayer(layers, name); 386 if (layer) { 387 LayerLibrary& library = g_layer_libraries[layer->library_idx]; 388 if (!library.Open()) 389 layer = nullptr; 390 } 391 392 return LayerRef(layer); 393} 394 395} // anonymous namespace 396 397void DiscoverLayers() { 398 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) 399 DiscoverLayersInDirectory("/data/local/debug/vulkan"); 400 if (!LoaderData::GetInstance().layer_path.empty()) 401 DiscoverLayersInDirectory(LoaderData::GetInstance().layer_path.c_str()); 402} 403 404uint32_t EnumerateInstanceLayers(uint32_t count, 405 VkLayerProperties* properties) { 406 return EnumerateLayers(g_instance_layers, count, properties); 407} 408 409uint32_t EnumerateDeviceLayers(uint32_t count, VkLayerProperties* properties) { 410 return EnumerateLayers(g_device_layers, count, properties); 411} 412 413void GetInstanceLayerExtensions(const char* name, 414 const VkExtensionProperties** properties, 415 uint32_t* count) { 416 GetLayerExtensions(g_instance_layers, name, properties, count); 417} 418 419void GetDeviceLayerExtensions(const char* name, 420 const VkExtensionProperties** properties, 421 uint32_t* count) { 422 GetLayerExtensions(g_device_layers, name, properties, count); 423} 424 425LayerRef GetInstanceLayerRef(const char* name) { 426 return GetLayerRef(g_instance_layers, name); 427} 428 429LayerRef GetDeviceLayerRef(const char* name) { 430 return GetLayerRef(g_device_layers, name); 431} 432 433LayerRef::LayerRef(const Layer* layer) : layer_(layer) {} 434 435LayerRef::~LayerRef() { 436 if (layer_) { 437 LayerLibrary& library = g_layer_libraries[layer_->library_idx]; 438 library.Close(); 439 } 440} 441 442const char* LayerRef::GetName() const { 443 return layer_->properties.layerName; 444} 445 446uint32_t LayerRef::GetSpecVersion() const { 447 return layer_->properties.specVersion; 448} 449 450LayerRef::LayerRef(LayerRef&& other) : layer_(std::move(other.layer_)) { 451 other.layer_ = nullptr; 452} 453 454PFN_vkGetInstanceProcAddr LayerRef::GetGetInstanceProcAddr() const { 455 return layer_ ? reinterpret_cast<PFN_vkGetInstanceProcAddr>( 456 GetLayerGetProcAddr(*layer_, "GetInstanceProcAddr", 19)) 457 : nullptr; 458} 459 460PFN_vkGetDeviceProcAddr LayerRef::GetGetDeviceProcAddr() const { 461 return layer_ ? reinterpret_cast<PFN_vkGetDeviceProcAddr>( 462 GetLayerGetProcAddr(*layer_, "GetDeviceProcAddr", 17)) 463 : nullptr; 464} 465 466bool LayerRef::SupportsExtension(const char* name) const { 467 return std::find_if(layer_->extensions.cbegin(), layer_->extensions.cend(), 468 [=](const VkExtensionProperties& ext) { 469 return strcmp(ext.extensionName, name) == 0; 470 }) != layer_->extensions.cend(); 471} 472 473} // namespace api 474} // namespace vulkan 475