api.cpp revision eef27fa3a0c7d153603b7fd69849fee73a07af5b
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// The API layer of the loader defines Vulkan API and manages layers. The 18// entrypoints are generated and defined in api_dispatch.cpp. Most of them 19// simply find the dispatch table and jump. 20// 21// There are a few of them requiring manual code for things such as layer 22// discovery or chaining. They call into functions defined in this file. 23 24#include <stdlib.h> 25#include <string.h> 26#include <algorithm> 27#include <mutex> 28#include <new> 29#include <utility> 30#include <cutils/properties.h> 31#include <log/log.h> 32 33#include <vulkan/vk_layer_interface.h> 34#include "api.h" 35#include "driver.h" 36#include "layers_extensions.h" 37 38namespace vulkan { 39namespace api { 40 41namespace { 42 43// Provide overridden layer names when there are implicit layers. No effect 44// otherwise. 45class OverrideLayerNames { 46 public: 47 OverrideLayerNames(bool is_instance, const VkAllocationCallbacks& allocator) 48 : is_instance_(is_instance), 49 allocator_(allocator), 50 scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND), 51 names_(nullptr), 52 name_count_(0), 53 implicit_layers_() { 54 implicit_layers_.result = VK_SUCCESS; 55 } 56 57 ~OverrideLayerNames() { 58 allocator_.pfnFree(allocator_.pUserData, names_); 59 allocator_.pfnFree(allocator_.pUserData, implicit_layers_.elements); 60 allocator_.pfnFree(allocator_.pUserData, implicit_layers_.name_pool); 61 } 62 63 VkResult Parse(const char* const* names, uint32_t count) { 64 AddImplicitLayers(); 65 66 const auto& arr = implicit_layers_; 67 if (arr.result != VK_SUCCESS) 68 return arr.result; 69 70 // no need to override when there is no implicit layer 71 if (!arr.count) 72 return VK_SUCCESS; 73 74 names_ = AllocateNameArray(arr.count + count); 75 if (!names_) 76 return VK_ERROR_OUT_OF_HOST_MEMORY; 77 78 // add implicit layer names 79 for (uint32_t i = 0; i < arr.count; i++) 80 names_[i] = GetImplicitLayerName(i); 81 82 name_count_ = arr.count; 83 84 // add explicit layer names 85 for (uint32_t i = 0; i < count; i++) { 86 // ignore explicit layers that are also implicit 87 if (IsImplicitLayer(names[i])) 88 continue; 89 90 names_[name_count_++] = names[i]; 91 } 92 93 return VK_SUCCESS; 94 } 95 96 const char* const* Names() const { return names_; } 97 98 uint32_t Count() const { return name_count_; } 99 100 private: 101 struct ImplicitLayer { 102 int priority; 103 size_t name_offset; 104 }; 105 106 struct ImplicitLayerArray { 107 ImplicitLayer* elements; 108 uint32_t max_count; 109 uint32_t count; 110 111 char* name_pool; 112 size_t max_pool_size; 113 size_t pool_size; 114 115 VkResult result; 116 }; 117 118 void AddImplicitLayers() { 119 if (!driver::Debuggable()) 120 return; 121 122 ParseDebugVulkanLayers(); 123 property_list(ParseDebugVulkanLayer, this); 124 125 // sort by priorities 126 auto& arr = implicit_layers_; 127 std::sort(arr.elements, arr.elements + arr.count, 128 [](const ImplicitLayer& a, const ImplicitLayer& b) { 129 return (a.priority < b.priority); 130 }); 131 } 132 133 void ParseDebugVulkanLayers() { 134 // debug.vulkan.layers specifies colon-separated layer names 135 char prop[PROPERTY_VALUE_MAX]; 136 if (!property_get("debug.vulkan.layers", prop, "")) 137 return; 138 139 // assign negative/high priorities to them 140 int prio = -PROPERTY_VALUE_MAX; 141 142 const char* p = prop; 143 const char* delim; 144 while ((delim = strchr(p, ':'))) { 145 if (delim > p) 146 AddImplicitLayer(prio, p, static_cast<size_t>(delim - p)); 147 148 prio++; 149 p = delim + 1; 150 } 151 152 if (p[0] != '\0') 153 AddImplicitLayer(prio, p, strlen(p)); 154 } 155 156 static void ParseDebugVulkanLayer(const char* key, 157 const char* val, 158 void* user_data) { 159 static const char prefix[] = "debug.vulkan.layer."; 160 const size_t prefix_len = sizeof(prefix) - 1; 161 162 if (strncmp(key, prefix, prefix_len) || val[0] == '\0') 163 return; 164 key += prefix_len; 165 166 // debug.vulkan.layer.<priority> 167 int priority = -1; 168 if (key[0] >= '0' && key[0] <= '9') 169 priority = atoi(key); 170 171 if (priority < 0) { 172 ALOGW("Ignored implicit layer %s with invalid priority %s", val, 173 key); 174 return; 175 } 176 177 OverrideLayerNames& override_layers = 178 *reinterpret_cast<OverrideLayerNames*>(user_data); 179 override_layers.AddImplicitLayer(priority, val, strlen(val)); 180 } 181 182 void AddImplicitLayer(int priority, const char* name, size_t len) { 183 if (!GrowImplicitLayerArray(1, 0)) 184 return; 185 186 auto& arr = implicit_layers_; 187 auto& layer = arr.elements[arr.count++]; 188 189 layer.priority = priority; 190 layer.name_offset = AddImplicitLayerName(name, len); 191 192 ALOGV("Added implicit layer %s", GetImplicitLayerName(arr.count - 1)); 193 } 194 195 size_t AddImplicitLayerName(const char* name, size_t len) { 196 if (!GrowImplicitLayerArray(0, len + 1)) 197 return 0; 198 199 // add the name to the pool 200 auto& arr = implicit_layers_; 201 size_t offset = arr.pool_size; 202 char* dst = arr.name_pool + offset; 203 204 std::copy(name, name + len, dst); 205 dst[len] = '\0'; 206 207 arr.pool_size += len + 1; 208 209 return offset; 210 } 211 212 bool GrowImplicitLayerArray(uint32_t layer_count, size_t name_size) { 213 const uint32_t initial_max_count = 16; 214 const size_t initial_max_pool_size = 512; 215 216 auto& arr = implicit_layers_; 217 218 // grow the element array if needed 219 while (arr.count + layer_count > arr.max_count) { 220 uint32_t new_max_count = 221 (arr.max_count) ? (arr.max_count << 1) : initial_max_count; 222 void* new_mem = nullptr; 223 224 if (new_max_count > arr.max_count) { 225 new_mem = allocator_.pfnReallocation( 226 allocator_.pUserData, arr.elements, 227 sizeof(ImplicitLayer) * new_max_count, 228 alignof(ImplicitLayer), scope_); 229 } 230 231 if (!new_mem) { 232 arr.result = VK_ERROR_OUT_OF_HOST_MEMORY; 233 arr.count = 0; 234 return false; 235 } 236 237 arr.elements = reinterpret_cast<ImplicitLayer*>(new_mem); 238 arr.max_count = new_max_count; 239 } 240 241 // grow the name pool if needed 242 while (arr.pool_size + name_size > arr.max_pool_size) { 243 size_t new_max_pool_size = (arr.max_pool_size) 244 ? (arr.max_pool_size << 1) 245 : initial_max_pool_size; 246 void* new_mem = nullptr; 247 248 if (new_max_pool_size > arr.max_pool_size) { 249 new_mem = allocator_.pfnReallocation( 250 allocator_.pUserData, arr.name_pool, new_max_pool_size, 251 alignof(char), scope_); 252 } 253 254 if (!new_mem) { 255 arr.result = VK_ERROR_OUT_OF_HOST_MEMORY; 256 arr.pool_size = 0; 257 return false; 258 } 259 260 arr.name_pool = reinterpret_cast<char*>(new_mem); 261 arr.max_pool_size = new_max_pool_size; 262 } 263 264 return true; 265 } 266 267 const char* GetImplicitLayerName(uint32_t index) const { 268 const auto& arr = implicit_layers_; 269 270 // this may return nullptr when arr.result is not VK_SUCCESS 271 return implicit_layers_.name_pool + arr.elements[index].name_offset; 272 } 273 274 bool IsImplicitLayer(const char* name) const { 275 const auto& arr = implicit_layers_; 276 277 for (uint32_t i = 0; i < arr.count; i++) { 278 if (strcmp(name, GetImplicitLayerName(i)) == 0) 279 return true; 280 } 281 282 return false; 283 } 284 285 const char** AllocateNameArray(uint32_t count) const { 286 return reinterpret_cast<const char**>(allocator_.pfnAllocation( 287 allocator_.pUserData, sizeof(const char*) * count, 288 alignof(const char*), scope_)); 289 } 290 291 const bool is_instance_; 292 const VkAllocationCallbacks& allocator_; 293 const VkSystemAllocationScope scope_; 294 295 const char** names_; 296 uint32_t name_count_; 297 298 ImplicitLayerArray implicit_layers_; 299}; 300 301// Provide overridden extension names when there are implicit extensions. 302// No effect otherwise. 303// 304// This is used only to enable VK_EXT_debug_report. 305class OverrideExtensionNames { 306 public: 307 OverrideExtensionNames(bool is_instance, 308 const VkAllocationCallbacks& allocator) 309 : is_instance_(is_instance), 310 allocator_(allocator), 311 scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND), 312 names_(nullptr), 313 name_count_(0), 314 install_debug_callback_(false) {} 315 316 ~OverrideExtensionNames() { 317 allocator_.pfnFree(allocator_.pUserData, names_); 318 } 319 320 VkResult Parse(const char* const* names, uint32_t count) { 321 // this is only for debug.vulkan.enable_callback 322 if (!EnableDebugCallback()) 323 return VK_SUCCESS; 324 325 names_ = AllocateNameArray(count + 1); 326 if (!names_) 327 return VK_ERROR_OUT_OF_HOST_MEMORY; 328 329 std::copy(names, names + count, names_); 330 331 name_count_ = count; 332 names_[name_count_++] = "VK_EXT_debug_report"; 333 334 install_debug_callback_ = true; 335 336 return VK_SUCCESS; 337 } 338 339 const char* const* Names() const { return names_; } 340 341 uint32_t Count() const { return name_count_; } 342 343 bool InstallDebugCallback() const { return install_debug_callback_; } 344 345 private: 346 bool EnableDebugCallback() const { 347 return (is_instance_ && driver::Debuggable() && 348 property_get_bool("debug.vulkan.enable_callback", false)); 349 } 350 351 const char** AllocateNameArray(uint32_t count) const { 352 return reinterpret_cast<const char**>(allocator_.pfnAllocation( 353 allocator_.pUserData, sizeof(const char*) * count, 354 alignof(const char*), scope_)); 355 } 356 357 const bool is_instance_; 358 const VkAllocationCallbacks& allocator_; 359 const VkSystemAllocationScope scope_; 360 361 const char** names_; 362 uint32_t name_count_; 363 bool install_debug_callback_; 364}; 365 366// vkCreateInstance and vkCreateDevice helpers with support for layer 367// chaining. 368class LayerChain { 369 public: 370 static VkResult CreateInstance(const VkInstanceCreateInfo* create_info, 371 const VkAllocationCallbacks* allocator, 372 VkInstance* instance_out); 373 374 static VkResult CreateDevice(VkPhysicalDevice physical_dev, 375 const VkDeviceCreateInfo* create_info, 376 const VkAllocationCallbacks* allocator, 377 VkDevice* dev_out); 378 379 static void DestroyInstance(VkInstance instance, 380 const VkAllocationCallbacks* allocator); 381 382 static void DestroyDevice(VkDevice dev, 383 const VkAllocationCallbacks* allocator); 384 385 private: 386 struct ActiveLayer { 387 LayerRef ref; 388 union { 389 VkLayerInstanceLink instance_link; 390 VkLayerDeviceLink device_link; 391 }; 392 }; 393 394 LayerChain(bool is_instance, const VkAllocationCallbacks& allocator); 395 ~LayerChain(); 396 397 VkResult ActivateLayers(const char* const* layer_names, 398 uint32_t layer_count, 399 const char* const* extension_names, 400 uint32_t extension_count); 401 ActiveLayer* AllocateLayerArray(uint32_t count) const; 402 VkResult LoadLayer(ActiveLayer& layer, const char* name); 403 void SetupLayerLinks(); 404 405 bool Empty() const; 406 void ModifyCreateInfo(VkInstanceCreateInfo& info); 407 void ModifyCreateInfo(VkDeviceCreateInfo& info); 408 409 VkResult Create(const VkInstanceCreateInfo* create_info, 410 const VkAllocationCallbacks* allocator, 411 VkInstance* instance_out); 412 413 VkResult Create(VkPhysicalDevice physical_dev, 414 const VkDeviceCreateInfo* create_info, 415 const VkAllocationCallbacks* allocator, 416 VkDevice* dev_out); 417 418 VkResult ValidateExtensions(const char* const* extension_names, 419 uint32_t extension_count); 420 VkResult ValidateExtensions(VkPhysicalDevice physical_dev, 421 const char* const* extension_names, 422 uint32_t extension_count); 423 VkExtensionProperties* AllocateDriverExtensionArray(uint32_t count) const; 424 bool IsLayerExtension(const char* name) const; 425 bool IsDriverExtension(const char* name) const; 426 427 template <typename DataType> 428 void StealLayers(DataType& data); 429 430 static void DestroyLayers(ActiveLayer* layers, 431 uint32_t count, 432 const VkAllocationCallbacks& allocator); 433 434 static VKAPI_ATTR VkBool32 435 DebugReportCallback(VkDebugReportFlagsEXT flags, 436 VkDebugReportObjectTypeEXT obj_type, 437 uint64_t obj, 438 size_t location, 439 int32_t msg_code, 440 const char* layer_prefix, 441 const char* msg, 442 void* user_data); 443 444 const bool is_instance_; 445 const VkAllocationCallbacks& allocator_; 446 447 OverrideLayerNames override_layers_; 448 OverrideExtensionNames override_extensions_; 449 450 ActiveLayer* layers_; 451 uint32_t layer_count_; 452 453 PFN_vkGetInstanceProcAddr get_instance_proc_addr_; 454 PFN_vkGetDeviceProcAddr get_device_proc_addr_; 455 456 union { 457 VkLayerInstanceCreateInfo instance_chain_info_; 458 VkLayerDeviceCreateInfo device_chain_info_; 459 }; 460 461 VkExtensionProperties* driver_extensions_; 462 uint32_t driver_extension_count_; 463}; 464 465LayerChain::LayerChain(bool is_instance, const VkAllocationCallbacks& allocator) 466 : is_instance_(is_instance), 467 allocator_(allocator), 468 override_layers_(is_instance, allocator), 469 override_extensions_(is_instance, allocator), 470 layers_(nullptr), 471 layer_count_(0), 472 get_instance_proc_addr_(nullptr), 473 get_device_proc_addr_(nullptr), 474 driver_extensions_(nullptr), 475 driver_extension_count_(0) {} 476 477LayerChain::~LayerChain() { 478 allocator_.pfnFree(allocator_.pUserData, driver_extensions_); 479 DestroyLayers(layers_, layer_count_, allocator_); 480} 481 482VkResult LayerChain::ActivateLayers(const char* const* layer_names, 483 uint32_t layer_count, 484 const char* const* extension_names, 485 uint32_t extension_count) { 486 VkResult result = override_layers_.Parse(layer_names, layer_count); 487 if (result != VK_SUCCESS) 488 return result; 489 490 result = override_extensions_.Parse(extension_names, extension_count); 491 if (result != VK_SUCCESS) 492 return result; 493 494 if (override_layers_.Count()) { 495 layer_names = override_layers_.Names(); 496 layer_count = override_layers_.Count(); 497 } 498 499 if (!layer_count) { 500 // point head of chain to the driver 501 get_instance_proc_addr_ = driver::GetInstanceProcAddr; 502 if (!is_instance_) 503 get_device_proc_addr_ = driver::GetDeviceProcAddr; 504 505 return VK_SUCCESS; 506 } 507 508 layers_ = AllocateLayerArray(layer_count); 509 if (!layers_) 510 return VK_ERROR_OUT_OF_HOST_MEMORY; 511 512 // load layers 513 for (uint32_t i = 0; i < layer_count; i++) { 514 result = LoadLayer(layers_[i], layer_names[i]); 515 if (result != VK_SUCCESS) 516 return result; 517 518 // count loaded layers for proper destructions on errors 519 layer_count_++; 520 } 521 522 SetupLayerLinks(); 523 524 return VK_SUCCESS; 525} 526 527LayerChain::ActiveLayer* LayerChain::AllocateLayerArray(uint32_t count) const { 528 VkSystemAllocationScope scope = (is_instance_) 529 ? VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE 530 : VK_SYSTEM_ALLOCATION_SCOPE_DEVICE; 531 532 return reinterpret_cast<ActiveLayer*>(allocator_.pfnAllocation( 533 allocator_.pUserData, sizeof(ActiveLayer) * count, alignof(ActiveLayer), 534 scope)); 535} 536 537VkResult LayerChain::LoadLayer(ActiveLayer& layer, const char* name) { 538 if (is_instance_) 539 new (&layer) ActiveLayer{GetInstanceLayerRef(name), {}}; 540 else 541 new (&layer) ActiveLayer{GetDeviceLayerRef(name), {}}; 542 543 if (!layer.ref) { 544 ALOGE("Failed to load layer %s", name); 545 layer.ref.~LayerRef(); 546 return VK_ERROR_LAYER_NOT_PRESENT; 547 } 548 549 ALOGI("Loaded %s layer %s", (is_instance_) ? "instance" : "device", name); 550 551 return VK_SUCCESS; 552} 553 554void LayerChain::SetupLayerLinks() { 555 if (is_instance_) { 556 for (uint32_t i = 0; i < layer_count_; i++) { 557 ActiveLayer& layer = layers_[i]; 558 559 // point head of chain to the first layer 560 if (i == 0) 561 get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr(); 562 563 // point tail of chain to the driver 564 if (i == layer_count_ - 1) { 565 layer.instance_link.pNext = nullptr; 566 layer.instance_link.pfnNextGetInstanceProcAddr = 567 driver::GetInstanceProcAddr; 568 break; 569 } 570 571 const ActiveLayer& next = layers_[i + 1]; 572 573 // const_cast as some naughty layers want to modify our links! 574 layer.instance_link.pNext = 575 const_cast<VkLayerInstanceLink*>(&next.instance_link); 576 layer.instance_link.pfnNextGetInstanceProcAddr = 577 next.ref.GetGetInstanceProcAddr(); 578 } 579 } else { 580 for (uint32_t i = 0; i < layer_count_; i++) { 581 ActiveLayer& layer = layers_[i]; 582 583 // point head of chain to the first layer 584 if (i == 0) { 585 get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr(); 586 get_device_proc_addr_ = layer.ref.GetGetDeviceProcAddr(); 587 } 588 589 // point tail of chain to the driver 590 if (i == layer_count_ - 1) { 591 layer.device_link.pNext = nullptr; 592 layer.device_link.pfnNextGetInstanceProcAddr = 593 driver::GetInstanceProcAddr; 594 layer.device_link.pfnNextGetDeviceProcAddr = 595 driver::GetDeviceProcAddr; 596 break; 597 } 598 599 const ActiveLayer& next = layers_[i + 1]; 600 601 // const_cast as some naughty layers want to modify our links! 602 layer.device_link.pNext = 603 const_cast<VkLayerDeviceLink*>(&next.device_link); 604 layer.device_link.pfnNextGetInstanceProcAddr = 605 next.ref.GetGetInstanceProcAddr(); 606 layer.device_link.pfnNextGetDeviceProcAddr = 607 next.ref.GetGetDeviceProcAddr(); 608 } 609 } 610} 611 612bool LayerChain::Empty() const { 613 return (!layer_count_ && !override_layers_.Count() && 614 !override_extensions_.Count()); 615} 616 617void LayerChain::ModifyCreateInfo(VkInstanceCreateInfo& info) { 618 if (layer_count_) { 619 const ActiveLayer& layer = layers_[0]; 620 621 instance_chain_info_.sType = 622 VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; 623 instance_chain_info_.function = VK_LAYER_FUNCTION_LINK; 624 // TODO fix vk_layer_interface.h and get rid of const_cast? 625 instance_chain_info_.u.pLayerInfo = 626 const_cast<VkLayerInstanceLink*>(&layer.instance_link); 627 628 // insert layer info 629 instance_chain_info_.pNext = info.pNext; 630 info.pNext = &instance_chain_info_; 631 } 632 633 if (override_layers_.Count()) { 634 info.enabledLayerCount = override_layers_.Count(); 635 info.ppEnabledLayerNames = override_layers_.Names(); 636 } 637 638 if (override_extensions_.Count()) { 639 info.enabledExtensionCount = override_extensions_.Count(); 640 info.ppEnabledExtensionNames = override_extensions_.Names(); 641 } 642} 643 644void LayerChain::ModifyCreateInfo(VkDeviceCreateInfo& info) { 645 if (layer_count_) { 646 const ActiveLayer& layer = layers_[0]; 647 648 device_chain_info_.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; 649 device_chain_info_.function = VK_LAYER_FUNCTION_LINK; 650 // TODO fix vk_layer_interface.h and get rid of const_cast? 651 device_chain_info_.u.pLayerInfo = 652 const_cast<VkLayerDeviceLink*>(&layer.device_link); 653 654 // insert layer info 655 device_chain_info_.pNext = info.pNext; 656 info.pNext = &device_chain_info_; 657 } 658 659 if (override_layers_.Count()) { 660 info.enabledLayerCount = override_layers_.Count(); 661 info.ppEnabledLayerNames = override_layers_.Names(); 662 } 663 664 if (override_extensions_.Count()) { 665 info.enabledExtensionCount = override_extensions_.Count(); 666 info.ppEnabledExtensionNames = override_extensions_.Names(); 667 } 668} 669 670VkResult LayerChain::Create(const VkInstanceCreateInfo* create_info, 671 const VkAllocationCallbacks* allocator, 672 VkInstance* instance_out) { 673 VkResult result = ValidateExtensions(create_info->ppEnabledExtensionNames, 674 create_info->enabledExtensionCount); 675 if (result != VK_SUCCESS) 676 return result; 677 678 // call down the chain 679 PFN_vkCreateInstance create_instance = 680 reinterpret_cast<PFN_vkCreateInstance>( 681 get_instance_proc_addr_(VK_NULL_HANDLE, "vkCreateInstance")); 682 VkInstance instance; 683 result = create_instance(create_info, allocator, &instance); 684 if (result != VK_SUCCESS) 685 return result; 686 687 // initialize InstanceData 688 InstanceData& data = GetData(instance); 689 memset(&data, 0, sizeof(data)); 690 691 data.instance = instance; 692 693 if (!InitDispatchTable(instance, get_instance_proc_addr_)) { 694 if (data.dispatch.DestroyInstance) 695 data.dispatch.DestroyInstance(instance, allocator); 696 697 return VK_ERROR_INITIALIZATION_FAILED; 698 } 699 700 // install debug report callback 701 if (override_extensions_.InstallDebugCallback()) { 702 PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback = 703 reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( 704 get_instance_proc_addr_(instance, 705 "vkCreateDebugReportCallbackEXT")); 706 data.destroy_debug_callback = 707 reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( 708 get_instance_proc_addr_(instance, 709 "vkDestroyDebugReportCallbackEXT")); 710 if (!create_debug_report_callback || !data.destroy_debug_callback) { 711 ALOGE("Broken VK_EXT_debug_report support"); 712 data.dispatch.DestroyInstance(instance, allocator); 713 return VK_ERROR_INITIALIZATION_FAILED; 714 } 715 716 VkDebugReportCallbackCreateInfoEXT debug_callback_info = {}; 717 debug_callback_info.sType = 718 VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; 719 debug_callback_info.flags = 720 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT; 721 debug_callback_info.pfnCallback = DebugReportCallback; 722 723 VkDebugReportCallbackEXT debug_callback; 724 result = create_debug_report_callback(instance, &debug_callback_info, 725 nullptr, &debug_callback); 726 if (result != VK_SUCCESS) { 727 ALOGE("Failed to install debug report callback"); 728 data.dispatch.DestroyInstance(instance, allocator); 729 return VK_ERROR_INITIALIZATION_FAILED; 730 } 731 732 data.debug_callback = debug_callback; 733 734 ALOGI("Installed debug report callback"); 735 } 736 737 StealLayers(data); 738 739 *instance_out = instance; 740 741 return VK_SUCCESS; 742} 743 744VkResult LayerChain::Create(VkPhysicalDevice physical_dev, 745 const VkDeviceCreateInfo* create_info, 746 const VkAllocationCallbacks* allocator, 747 VkDevice* dev_out) { 748 VkResult result = 749 ValidateExtensions(physical_dev, create_info->ppEnabledExtensionNames, 750 create_info->enabledExtensionCount); 751 if (result != VK_SUCCESS) 752 return result; 753 754 // call down the chain 755 // 756 // TODO Instance call chain available at 757 // GetData(physical_dev).dispatch.CreateDevice is ignored. Is that 758 // right? 759 VkInstance instance = GetData(physical_dev).instance; 760 PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>( 761 get_instance_proc_addr_(instance, "vkCreateDevice")); 762 VkDevice dev; 763 result = create_device(physical_dev, create_info, allocator, &dev); 764 if (result != VK_SUCCESS) 765 return result; 766 767 // initialize DeviceData 768 DeviceData& data = GetData(dev); 769 memset(&data, 0, sizeof(data)); 770 771 if (!InitDispatchTable(dev, get_device_proc_addr_)) { 772 if (data.dispatch.DestroyDevice) 773 data.dispatch.DestroyDevice(dev, allocator); 774 775 return VK_ERROR_INITIALIZATION_FAILED; 776 } 777 778 StealLayers(data); 779 780 *dev_out = dev; 781 782 return VK_SUCCESS; 783} 784 785VkResult LayerChain::ValidateExtensions(const char* const* extension_names, 786 uint32_t extension_count) { 787 if (!extension_count) 788 return VK_SUCCESS; 789 790 // query driver instance extensions 791 uint32_t count; 792 VkResult result = 793 EnumerateInstanceExtensionProperties(nullptr, &count, nullptr); 794 if (result == VK_SUCCESS && count) { 795 driver_extensions_ = AllocateDriverExtensionArray(count); 796 result = (driver_extensions_) ? EnumerateInstanceExtensionProperties( 797 nullptr, &count, driver_extensions_) 798 : VK_ERROR_OUT_OF_HOST_MEMORY; 799 } 800 if (result != VK_SUCCESS) 801 return result; 802 803 driver_extension_count_ = count; 804 805 for (uint32_t i = 0; i < extension_count; i++) { 806 const char* name = extension_names[i]; 807 if (!IsLayerExtension(name) && !IsDriverExtension(name)) { 808 ALOGE("Failed to enable missing instance extension %s", name); 809 return VK_ERROR_EXTENSION_NOT_PRESENT; 810 } 811 } 812 813 return VK_SUCCESS; 814} 815 816VkResult LayerChain::ValidateExtensions(VkPhysicalDevice physical_dev, 817 const char* const* extension_names, 818 uint32_t extension_count) { 819 if (!extension_count) 820 return VK_SUCCESS; 821 822 // query driver device extensions 823 uint32_t count; 824 VkResult result = EnumerateDeviceExtensionProperties(physical_dev, nullptr, 825 &count, nullptr); 826 if (result == VK_SUCCESS && count) { 827 driver_extensions_ = AllocateDriverExtensionArray(count); 828 result = (driver_extensions_) 829 ? EnumerateDeviceExtensionProperties( 830 physical_dev, nullptr, &count, driver_extensions_) 831 : VK_ERROR_OUT_OF_HOST_MEMORY; 832 } 833 if (result != VK_SUCCESS) 834 return result; 835 836 driver_extension_count_ = count; 837 838 for (uint32_t i = 0; i < extension_count; i++) { 839 const char* name = extension_names[i]; 840 if (!IsLayerExtension(name) && !IsDriverExtension(name)) { 841 ALOGE("Failed to enable missing device extension %s", name); 842 return VK_ERROR_EXTENSION_NOT_PRESENT; 843 } 844 } 845 846 return VK_SUCCESS; 847} 848 849VkExtensionProperties* LayerChain::AllocateDriverExtensionArray( 850 uint32_t count) const { 851 return reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation( 852 allocator_.pUserData, sizeof(VkExtensionProperties) * count, 853 alignof(VkExtensionProperties), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); 854} 855 856bool LayerChain::IsLayerExtension(const char* name) const { 857 for (uint32_t i = 0; i < layer_count_; i++) { 858 const ActiveLayer& layer = layers_[i]; 859 if (layer.ref.SupportsExtension(name)) 860 return true; 861 } 862 863 return false; 864} 865 866bool LayerChain::IsDriverExtension(const char* name) const { 867 for (uint32_t i = 0; i < driver_extension_count_; i++) { 868 if (strcmp(driver_extensions_[i].extensionName, name) == 0) 869 return true; 870 } 871 872 return false; 873} 874 875template <typename DataType> 876void LayerChain::StealLayers(DataType& data) { 877 data.layers = layers_; 878 data.layer_count = layer_count_; 879 880 layers_ = nullptr; 881 layer_count_ = 0; 882} 883 884void LayerChain::DestroyLayers(ActiveLayer* layers, 885 uint32_t count, 886 const VkAllocationCallbacks& allocator) { 887 for (uint32_t i = 0; i < count; i++) 888 layers[i].ref.~LayerRef(); 889 890 allocator.pfnFree(allocator.pUserData, layers); 891} 892 893VkBool32 LayerChain::DebugReportCallback(VkDebugReportFlagsEXT flags, 894 VkDebugReportObjectTypeEXT obj_type, 895 uint64_t obj, 896 size_t location, 897 int32_t msg_code, 898 const char* layer_prefix, 899 const char* msg, 900 void* user_data) { 901 int prio; 902 903 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) 904 prio = ANDROID_LOG_ERROR; 905 else if (flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT | 906 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)) 907 prio = ANDROID_LOG_WARN; 908 else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) 909 prio = ANDROID_LOG_INFO; 910 else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) 911 prio = ANDROID_LOG_DEBUG; 912 else 913 prio = ANDROID_LOG_UNKNOWN; 914 915 LOG_PRI(prio, LOG_TAG, "[%s] Code %d : %s", layer_prefix, msg_code, msg); 916 917 (void)obj_type; 918 (void)obj; 919 (void)location; 920 (void)user_data; 921 922 return false; 923} 924 925VkResult LayerChain::CreateInstance(const VkInstanceCreateInfo* create_info, 926 const VkAllocationCallbacks* allocator, 927 VkInstance* instance_out) { 928 LayerChain chain(true, 929 (allocator) ? *allocator : driver::GetDefaultAllocator()); 930 931 VkResult result = chain.ActivateLayers(create_info->ppEnabledLayerNames, 932 create_info->enabledLayerCount, 933 create_info->ppEnabledExtensionNames, 934 create_info->enabledExtensionCount); 935 if (result != VK_SUCCESS) 936 return result; 937 938 // use a local create info when the chain is not empty 939 VkInstanceCreateInfo local_create_info; 940 if (!chain.Empty()) { 941 local_create_info = *create_info; 942 chain.ModifyCreateInfo(local_create_info); 943 create_info = &local_create_info; 944 } 945 946 return chain.Create(create_info, allocator, instance_out); 947} 948 949VkResult LayerChain::CreateDevice(VkPhysicalDevice physical_dev, 950 const VkDeviceCreateInfo* create_info, 951 const VkAllocationCallbacks* allocator, 952 VkDevice* dev_out) { 953 LayerChain chain(false, (allocator) 954 ? *allocator 955 : driver::GetData(physical_dev).allocator); 956 957 VkResult result = chain.ActivateLayers(create_info->ppEnabledLayerNames, 958 create_info->enabledLayerCount, 959 create_info->ppEnabledExtensionNames, 960 create_info->enabledExtensionCount); 961 if (result != VK_SUCCESS) 962 return result; 963 964 // use a local create info when the chain is not empty 965 VkDeviceCreateInfo local_create_info; 966 if (!chain.Empty()) { 967 local_create_info = *create_info; 968 chain.ModifyCreateInfo(local_create_info); 969 create_info = &local_create_info; 970 } 971 972 return chain.Create(physical_dev, create_info, allocator, dev_out); 973} 974 975void LayerChain::DestroyInstance(VkInstance instance, 976 const VkAllocationCallbacks* allocator) { 977 InstanceData& data = GetData(instance); 978 979 if (data.debug_callback != VK_NULL_HANDLE) 980 data.destroy_debug_callback(instance, data.debug_callback, allocator); 981 982 ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers); 983 uint32_t layer_count = data.layer_count; 984 985 VkAllocationCallbacks local_allocator; 986 if (!allocator) 987 local_allocator = driver::GetData(instance).allocator; 988 989 // this also destroys InstanceData 990 data.dispatch.DestroyInstance(instance, allocator); 991 992 DestroyLayers(layers, layer_count, 993 (allocator) ? *allocator : local_allocator); 994} 995 996void LayerChain::DestroyDevice(VkDevice device, 997 const VkAllocationCallbacks* allocator) { 998 DeviceData& data = GetData(device); 999 1000 ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers); 1001 uint32_t layer_count = data.layer_count; 1002 1003 VkAllocationCallbacks local_allocator; 1004 if (!allocator) 1005 local_allocator = driver::GetData(device).allocator; 1006 1007 // this also destroys DeviceData 1008 data.dispatch.DestroyDevice(device, allocator); 1009 1010 DestroyLayers(layers, layer_count, 1011 (allocator) ? *allocator : local_allocator); 1012} 1013 1014// ---------------------------------------------------------------------------- 1015 1016bool EnsureInitialized() { 1017 static std::once_flag once_flag; 1018 static bool initialized; 1019 1020 std::call_once(once_flag, []() { 1021 if (driver::OpenHAL()) { 1022 DiscoverLayers(); 1023 initialized = true; 1024 } 1025 }); 1026 1027 return initialized; 1028} 1029 1030} // anonymous namespace 1031 1032VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, 1033 const VkAllocationCallbacks* pAllocator, 1034 VkInstance* pInstance) { 1035 if (!EnsureInitialized()) 1036 return VK_ERROR_INITIALIZATION_FAILED; 1037 1038 return LayerChain::CreateInstance(pCreateInfo, pAllocator, pInstance); 1039} 1040 1041void DestroyInstance(VkInstance instance, 1042 const VkAllocationCallbacks* pAllocator) { 1043 if (instance != VK_NULL_HANDLE) 1044 LayerChain::DestroyInstance(instance, pAllocator); 1045} 1046 1047VkResult CreateDevice(VkPhysicalDevice physicalDevice, 1048 const VkDeviceCreateInfo* pCreateInfo, 1049 const VkAllocationCallbacks* pAllocator, 1050 VkDevice* pDevice) { 1051 return LayerChain::CreateDevice(physicalDevice, pCreateInfo, pAllocator, 1052 pDevice); 1053} 1054 1055void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) { 1056 if (device != VK_NULL_HANDLE) 1057 LayerChain::DestroyDevice(device, pAllocator); 1058} 1059 1060VkResult EnumerateInstanceLayerProperties(uint32_t* pPropertyCount, 1061 VkLayerProperties* pProperties) { 1062 if (!EnsureInitialized()) 1063 return VK_ERROR_INITIALIZATION_FAILED; 1064 1065 uint32_t count = 1066 EnumerateInstanceLayers(pProperties ? *pPropertyCount : 0, pProperties); 1067 1068 if (!pProperties || *pPropertyCount > count) 1069 *pPropertyCount = count; 1070 1071 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; 1072} 1073 1074VkResult EnumerateInstanceExtensionProperties( 1075 const char* pLayerName, 1076 uint32_t* pPropertyCount, 1077 VkExtensionProperties* pProperties) { 1078 if (!EnsureInitialized()) 1079 return VK_ERROR_INITIALIZATION_FAILED; 1080 1081 if (pLayerName) { 1082 const VkExtensionProperties* props; 1083 uint32_t count; 1084 GetInstanceLayerExtensions(pLayerName, &props, &count); 1085 1086 if (!pProperties || *pPropertyCount > count) 1087 *pPropertyCount = count; 1088 if (pProperties) 1089 std::copy(props, props + *pPropertyCount, pProperties); 1090 1091 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; 1092 } 1093 1094 // TODO how about extensions from implicitly enabled layers? 1095 return vulkan::driver::EnumerateInstanceExtensionProperties( 1096 nullptr, pPropertyCount, pProperties); 1097} 1098 1099VkResult EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, 1100 uint32_t* pPropertyCount, 1101 VkLayerProperties* pProperties) { 1102 (void)physicalDevice; 1103 1104 uint32_t count = 1105 EnumerateDeviceLayers(pProperties ? *pPropertyCount : 0, pProperties); 1106 1107 if (!pProperties || *pPropertyCount > count) 1108 *pPropertyCount = count; 1109 1110 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; 1111} 1112 1113VkResult EnumerateDeviceExtensionProperties( 1114 VkPhysicalDevice physicalDevice, 1115 const char* pLayerName, 1116 uint32_t* pPropertyCount, 1117 VkExtensionProperties* pProperties) { 1118 if (pLayerName) { 1119 const VkExtensionProperties* props; 1120 uint32_t count; 1121 GetDeviceLayerExtensions(pLayerName, &props, &count); 1122 1123 if (!pProperties || *pPropertyCount > count) 1124 *pPropertyCount = count; 1125 if (pProperties) 1126 std::copy(props, props + *pPropertyCount, pProperties); 1127 1128 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; 1129 } 1130 1131 // TODO how about extensions from implicitly enabled layers? 1132 const InstanceData& data = GetData(physicalDevice); 1133 return data.dispatch.EnumerateDeviceExtensionProperties( 1134 physicalDevice, nullptr, pPropertyCount, pProperties); 1135} 1136 1137} // namespace api 1138} // namespace vulkan 1139