blob: 9c5aa3bc43171f3149e66fd34c3c9250efd6d850 [file] [log] [blame]
Chia-I Wu0c203242016-03-15 13:44:51 +08001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
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 "loader.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 add_implicit_layers();
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_ = allocate_name_array(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] = get_implicit_layer_name(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 (is_implicit_layer(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 add_implicit_layers() {
119 if (!driver::Debuggable())
120 return;
121
122 parse_debug_vulkan_layers();
123 property_list(parse_debug_vulkan_layer, 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 parse_debug_vulkan_layers() {
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 add_implicit_layer(prio, p, static_cast<size_t>(delim - p));
147
148 prio++;
149 p = delim + 1;
150 }
151
152 if (p[0] != '\0')
153 add_implicit_layer(prio, p, strlen(p));
154 }
155
156 static void parse_debug_vulkan_layer(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.add_implicit_layer(priority, val, strlen(val));
180 }
181
182 void add_implicit_layer(int priority, const char* name, size_t len) {
183 if (!grow_implicit_layer_array(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 = add_implicit_layer_name(name, len);
191
192 ALOGV("Added implicit layer %s",
193 get_implicit_layer_name(arr.count - 1));
194 }
195
196 size_t add_implicit_layer_name(const char* name, size_t len) {
197 if (!grow_implicit_layer_array(0, len + 1))
198 return 0;
199
200 // add the name to the pool
201 auto& arr = implicit_layers_;
202 size_t offset = arr.pool_size;
203 char* dst = arr.name_pool + offset;
204
205 std::copy(name, name + len, dst);
206 dst[len] = '\0';
207
208 arr.pool_size += len + 1;
209
210 return offset;
211 }
212
213 bool grow_implicit_layer_array(uint32_t layer_count, size_t name_size) {
214 const uint32_t initial_max_count = 16;
215 const size_t initial_max_pool_size = 512;
216
217 auto& arr = implicit_layers_;
218
219 // grow the element array if needed
220 while (arr.count + layer_count > arr.max_count) {
221 uint32_t new_max_count =
222 (arr.max_count) ? (arr.max_count << 1) : initial_max_count;
223 void* new_mem = nullptr;
224
225 if (new_max_count > arr.max_count) {
226 new_mem = allocator_.pfnReallocation(
227 allocator_.pUserData, arr.elements,
228 sizeof(ImplicitLayer) * new_max_count,
229 alignof(ImplicitLayer), scope_);
230 }
231
232 if (!new_mem) {
233 arr.result = VK_ERROR_OUT_OF_HOST_MEMORY;
234 arr.count = 0;
235 return false;
236 }
237
238 arr.elements = reinterpret_cast<ImplicitLayer*>(new_mem);
239 arr.max_count = new_max_count;
240 }
241
242 // grow the name pool if needed
243 while (arr.pool_size + name_size > arr.max_pool_size) {
244 size_t new_max_pool_size = (arr.max_pool_size)
245 ? (arr.max_pool_size << 1)
246 : initial_max_pool_size;
247 void* new_mem = nullptr;
248
249 if (new_max_pool_size > arr.max_pool_size) {
250 new_mem = allocator_.pfnReallocation(
251 allocator_.pUserData, arr.name_pool, new_max_pool_size,
252 alignof(char), scope_);
253 }
254
255 if (!new_mem) {
256 arr.result = VK_ERROR_OUT_OF_HOST_MEMORY;
257 arr.pool_size = 0;
258 return false;
259 }
260
261 arr.name_pool = reinterpret_cast<char*>(new_mem);
262 arr.max_pool_size = new_max_pool_size;
263 }
264
265 return true;
266 }
267
268 const char* get_implicit_layer_name(uint32_t index) const {
269 const auto& arr = implicit_layers_;
270
271 // this may return nullptr when arr.result is not VK_SUCCESS
272 return implicit_layers_.name_pool + arr.elements[index].name_offset;
273 }
274
275 bool is_implicit_layer(const char* name) const {
276 const auto& arr = implicit_layers_;
277
278 for (uint32_t i = 0; i < arr.count; i++) {
279 if (strcmp(name, get_implicit_layer_name(i)) == 0)
280 return true;
281 }
282
283 return false;
284 }
285
286 const char** allocate_name_array(uint32_t count) const {
287 return reinterpret_cast<const char**>(allocator_.pfnAllocation(
288 allocator_.pUserData, sizeof(const char*) * count,
289 alignof(const char*), scope_));
290 }
291
292 const bool is_instance_;
293 const VkAllocationCallbacks& allocator_;
294 const VkSystemAllocationScope scope_;
295
296 const char** names_;
297 uint32_t name_count_;
298
299 ImplicitLayerArray implicit_layers_;
300};
301
302// Provide overridden extension names when there are implicit extensions.
303// No effect otherwise.
304//
305// This is used only to enable VK_EXT_debug_report.
306class OverrideExtensionNames {
307 public:
308 OverrideExtensionNames(bool is_instance,
309 const VkAllocationCallbacks& allocator)
310 : is_instance_(is_instance),
311 allocator_(allocator),
312 scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND),
313 names_(nullptr),
314 name_count_(0),
315 install_debug_callback_(false) {}
316
317 ~OverrideExtensionNames() {
318 allocator_.pfnFree(allocator_.pUserData, names_);
319 }
320
321 VkResult parse(const char* const* names, uint32_t count) {
322 // this is only for debug.vulkan.enable_callback
323 if (!enable_debug_callback())
324 return VK_SUCCESS;
325
326 names_ = allocate_name_array(count + 1);
327 if (!names_)
328 return VK_ERROR_OUT_OF_HOST_MEMORY;
329
330 std::copy(names, names + count, names_);
331
332 name_count_ = count;
333 names_[name_count_++] = "VK_EXT_debug_report";
334
335 install_debug_callback_ = true;
336
337 return VK_SUCCESS;
338 }
339
340 const char* const* names() const { return names_; }
341
342 uint32_t count() const { return name_count_; }
343
344 bool install_debug_callback() const { return install_debug_callback_; }
345
346 private:
347 bool enable_debug_callback() const {
348 return (is_instance_ && driver::Debuggable() &&
349 property_get_bool("debug.vulkan.enable_callback", false));
350 }
351
352 const char** allocate_name_array(uint32_t count) const {
353 return reinterpret_cast<const char**>(allocator_.pfnAllocation(
354 allocator_.pUserData, sizeof(const char*) * count,
355 alignof(const char*), scope_));
356 }
357
358 const bool is_instance_;
359 const VkAllocationCallbacks& allocator_;
360 const VkSystemAllocationScope scope_;
361
362 const char** names_;
363 uint32_t name_count_;
364 bool install_debug_callback_;
365};
366
367// vkCreateInstance and vkCreateDevice helpers with support for layer
368// chaining.
369class LayerChain {
370 public:
371 static VkResult create_instance(const VkInstanceCreateInfo* create_info,
372 const VkAllocationCallbacks* allocator,
373 VkInstance* instance_out);
374
375 static VkResult create_device(VkPhysicalDevice physical_dev,
376 const VkDeviceCreateInfo* create_info,
377 const VkAllocationCallbacks* allocator,
378 VkDevice* dev_out);
379
380 static void destroy_instance(VkInstance instance,
381 const VkAllocationCallbacks* allocator);
382
383 static void destroy_device(VkDevice dev,
384 const VkAllocationCallbacks* allocator);
385
386 private:
387 struct ActiveLayer {
388 LayerRef ref;
389 union {
390 VkLayerInstanceLink instance_link;
391 VkLayerDeviceLink device_link;
392 };
393 };
394
395 LayerChain(bool is_instance, const VkAllocationCallbacks& allocator);
396 ~LayerChain();
397
398 VkResult activate_layers(const char* const* layer_names,
399 uint32_t layer_count,
400 const char* const* extension_names,
401 uint32_t extension_count);
402 ActiveLayer* allocate_layer_array(uint32_t count) const;
403 VkResult load_layer(ActiveLayer& layer, const char* name);
404 void setup_layer_links();
405
406 bool empty() const;
407 void modify_create_info(VkInstanceCreateInfo& info);
408 void modify_create_info(VkDeviceCreateInfo& info);
409
410 VkResult create(const VkInstanceCreateInfo* create_info,
411 const VkAllocationCallbacks* allocator,
412 VkInstance* instance_out);
413
414 VkResult create(VkPhysicalDevice physical_dev,
415 const VkDeviceCreateInfo* create_info,
416 const VkAllocationCallbacks* allocator,
417 VkDevice* dev_out);
418
419 template <typename DataType>
420 void steal_layers(DataType& data);
421
422 static void destroy_layers(ActiveLayer* layers,
423 uint32_t count,
424 const VkAllocationCallbacks& allocator);
425
426 static VKAPI_ATTR VkBool32
427 debug_report_callback(VkDebugReportFlagsEXT flags,
428 VkDebugReportObjectTypeEXT obj_type,
429 uint64_t obj,
430 size_t location,
431 int32_t msg_code,
432 const char* layer_prefix,
433 const char* msg,
434 void* user_data);
435
436 const bool is_instance_;
437 const VkAllocationCallbacks& allocator_;
438
439 OverrideLayerNames override_layers_;
440 OverrideExtensionNames override_extensions_;
441
442 ActiveLayer* layers_;
443 uint32_t layer_count_;
444
445 PFN_vkGetInstanceProcAddr get_instance_proc_addr_;
446 PFN_vkGetDeviceProcAddr get_device_proc_addr_;
447
448 union {
449 VkLayerInstanceCreateInfo instance_chain_info_;
450 VkLayerDeviceCreateInfo device_chain_info_;
451 };
452};
453
454LayerChain::LayerChain(bool is_instance, const VkAllocationCallbacks& allocator)
455 : is_instance_(is_instance),
456 allocator_(allocator),
457 override_layers_(is_instance, allocator),
458 override_extensions_(is_instance, allocator),
459 layers_(nullptr),
460 layer_count_(0),
461 get_instance_proc_addr_(nullptr),
462 get_device_proc_addr_(nullptr) {}
463
464LayerChain::~LayerChain() {
465 destroy_layers(layers_, layer_count_, allocator_);
466}
467
468VkResult LayerChain::activate_layers(const char* const* layer_names,
469 uint32_t layer_count,
470 const char* const* extension_names,
471 uint32_t extension_count) {
472 VkResult result = override_layers_.parse(layer_names, layer_count);
473 if (result != VK_SUCCESS)
474 return result;
475
476 result = override_extensions_.parse(extension_names, extension_count);
477 if (result != VK_SUCCESS)
478 return result;
479
480 if (override_layers_.count()) {
481 layer_names = override_layers_.names();
482 layer_count = override_layers_.count();
483 }
484
485 if (!layer_count) {
486 // point head of chain to the driver
487 get_instance_proc_addr_ = driver::GetInstanceProcAddr;
488 if (!is_instance_)
489 get_device_proc_addr_ = driver::GetDeviceProcAddr;
490
491 return VK_SUCCESS;
492 }
493
494 layers_ = allocate_layer_array(layer_count);
495 if (!layers_)
496 return VK_ERROR_OUT_OF_HOST_MEMORY;
497
498 // load layers
499 for (uint32_t i = 0; i < layer_count; i++) {
500 result = load_layer(layers_[i], layer_names[i]);
501 if (result != VK_SUCCESS)
502 return result;
503
504 // count loaded layers for proper destructions on errors
505 layer_count_++;
506 }
507
508 setup_layer_links();
509
510 return VK_SUCCESS;
511}
512
513LayerChain::ActiveLayer* LayerChain::allocate_layer_array(
514 uint32_t count) const {
515 VkSystemAllocationScope scope = (is_instance_)
516 ? VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
517 : VK_SYSTEM_ALLOCATION_SCOPE_DEVICE;
518
519 return reinterpret_cast<ActiveLayer*>(allocator_.pfnAllocation(
520 allocator_.pUserData, sizeof(ActiveLayer) * count, alignof(ActiveLayer),
521 scope));
522}
523
524VkResult LayerChain::load_layer(ActiveLayer& layer, const char* name) {
525 if (is_instance_)
526 new (&layer) ActiveLayer{GetInstanceLayerRef(name), {}};
527 else
528 new (&layer) ActiveLayer{GetDeviceLayerRef(name), {}};
529
530 if (!layer.ref) {
531 ALOGE("Failed to load layer %s", name);
532 layer.ref.~LayerRef();
533 return VK_ERROR_LAYER_NOT_PRESENT;
534 }
535
536 ALOGI("Loaded %s layer %s", (is_instance_) ? "instance" : "device", name);
537
538 return VK_SUCCESS;
539}
540
541void LayerChain::setup_layer_links() {
542 if (is_instance_) {
543 for (uint32_t i = 0; i < layer_count_; i++) {
544 ActiveLayer& layer = layers_[i];
545
546 // point head of chain to the first layer
547 if (i == 0)
548 get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr();
549
550 // point tail of chain to the driver
551 if (i == layer_count_ - 1) {
552 layer.instance_link.pNext = nullptr;
553 layer.instance_link.pfnNextGetInstanceProcAddr =
554 driver::GetInstanceProcAddr;
555 break;
556 }
557
558 const ActiveLayer& next = layers_[i + 1];
559
560 // const_cast as some naughty layers want to modify our links!
561 layer.instance_link.pNext =
562 const_cast<VkLayerInstanceLink*>(&next.instance_link);
563 layer.instance_link.pfnNextGetInstanceProcAddr =
564 next.ref.GetGetInstanceProcAddr();
565 }
566 } else {
567 for (uint32_t i = 0; i < layer_count_; i++) {
568 ActiveLayer& layer = layers_[i];
569
570 // point head of chain to the first layer
571 if (i == 0) {
572 get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr();
573 get_device_proc_addr_ = layer.ref.GetGetDeviceProcAddr();
574 }
575
576 // point tail of chain to the driver
577 if (i == layer_count_ - 1) {
578 layer.device_link.pNext = nullptr;
579 layer.device_link.pfnNextGetInstanceProcAddr =
580 driver::GetInstanceProcAddr;
581 layer.device_link.pfnNextGetDeviceProcAddr =
582 driver::GetDeviceProcAddr;
583 break;
584 }
585
586 const ActiveLayer& next = layers_[i + 1];
587
588 // const_cast as some naughty layers want to modify our links!
589 layer.device_link.pNext =
590 const_cast<VkLayerDeviceLink*>(&next.device_link);
591 layer.device_link.pfnNextGetInstanceProcAddr =
592 next.ref.GetGetInstanceProcAddr();
593 layer.device_link.pfnNextGetDeviceProcAddr =
594 next.ref.GetGetDeviceProcAddr();
595 }
596 }
597}
598
599bool LayerChain::empty() const {
600 return (!layer_count_ && !override_layers_.count() &&
601 !override_extensions_.count());
602}
603
604void LayerChain::modify_create_info(VkInstanceCreateInfo& info) {
605 if (layer_count_) {
606 const ActiveLayer& layer = layers_[0];
607
608 instance_chain_info_.sType =
609 VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
610 instance_chain_info_.function = VK_LAYER_FUNCTION_LINK;
611 // TODO fix vk_layer_interface.h and get rid of const_cast?
612 instance_chain_info_.u.pLayerInfo =
613 const_cast<VkLayerInstanceLink*>(&layer.instance_link);
614
615 // insert layer info
616 instance_chain_info_.pNext = info.pNext;
617 info.pNext = &instance_chain_info_;
618 }
619
620 if (override_layers_.count()) {
621 info.enabledLayerCount = override_layers_.count();
622 info.ppEnabledLayerNames = override_layers_.names();
623 }
624
625 if (override_extensions_.count()) {
626 info.enabledExtensionCount = override_extensions_.count();
627 info.ppEnabledExtensionNames = override_extensions_.names();
628 }
629}
630
631void LayerChain::modify_create_info(VkDeviceCreateInfo& info) {
632 if (layer_count_) {
633 const ActiveLayer& layer = layers_[0];
634
635 device_chain_info_.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
636 device_chain_info_.function = VK_LAYER_FUNCTION_LINK;
637 // TODO fix vk_layer_interface.h and get rid of const_cast?
638 device_chain_info_.u.pLayerInfo =
639 const_cast<VkLayerDeviceLink*>(&layer.device_link);
640
641 // insert layer info
642 device_chain_info_.pNext = info.pNext;
643 info.pNext = &device_chain_info_;
644 }
645
646 if (override_layers_.count()) {
647 info.enabledLayerCount = override_layers_.count();
648 info.ppEnabledLayerNames = override_layers_.names();
649 }
650
651 if (override_extensions_.count()) {
652 info.enabledExtensionCount = override_extensions_.count();
653 info.ppEnabledExtensionNames = override_extensions_.names();
654 }
655}
656
657VkResult LayerChain::create(const VkInstanceCreateInfo* create_info,
658 const VkAllocationCallbacks* allocator,
659 VkInstance* instance_out) {
660 // call down the chain
661 PFN_vkCreateInstance create_instance =
662 reinterpret_cast<PFN_vkCreateInstance>(
663 get_instance_proc_addr_(VK_NULL_HANDLE, "vkCreateInstance"));
664 VkInstance instance;
665 VkResult result = create_instance(create_info, allocator, &instance);
666 if (result != VK_SUCCESS)
667 return result;
668
669 // initialize InstanceData
670 InstanceData& data = GetData(instance);
671 memset(&data, 0, sizeof(data));
672
673 data.instance = instance;
674
675 if (!InitDispatchTable(instance, get_instance_proc_addr_)) {
676 if (data.dispatch.DestroyInstance)
677 data.dispatch.DestroyInstance(instance, allocator);
678
679 return VK_ERROR_INITIALIZATION_FAILED;
680 }
681
682 // install debug report callback
683 if (override_extensions_.install_debug_callback()) {
684 PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
685 reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
686 get_instance_proc_addr_(instance,
687 "vkCreateDebugReportCallbackEXT"));
688 data.destroy_debug_callback =
689 reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
690 get_instance_proc_addr_(instance,
691 "vkDestroyDebugReportCallbackEXT"));
692 if (!create_debug_report_callback || !data.destroy_debug_callback) {
693 ALOGE("Broken VK_EXT_debug_report support");
694 data.dispatch.DestroyInstance(instance, allocator);
695 return VK_ERROR_INITIALIZATION_FAILED;
696 }
697
698 VkDebugReportCallbackCreateInfoEXT debug_callback_info = {};
699 debug_callback_info.sType =
700 VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
701 debug_callback_info.flags =
702 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
703 debug_callback_info.pfnCallback = debug_report_callback;
704
705 VkDebugReportCallbackEXT debug_callback;
706 result = create_debug_report_callback(instance, &debug_callback_info,
707 nullptr, &debug_callback);
708 if (result != VK_SUCCESS) {
709 ALOGE("Failed to install debug report callback");
710 data.dispatch.DestroyInstance(instance, allocator);
711 return VK_ERROR_INITIALIZATION_FAILED;
712 }
713
714 data.debug_callback = debug_callback;
715
716 ALOGI("Installed debug report callback");
717 }
718
719 steal_layers(data);
720
721 *instance_out = instance;
722
723 return VK_SUCCESS;
724}
725
726VkResult LayerChain::create(VkPhysicalDevice physical_dev,
727 const VkDeviceCreateInfo* create_info,
728 const VkAllocationCallbacks* allocator,
729 VkDevice* dev_out) {
730 // call down the chain
731 //
732 // TODO Instance call chain available at
733 // GetData(physical_dev).dispatch.CreateDevice is ignored. Is that
734 // right?
735 VkInstance instance = GetData(physical_dev).instance;
736 PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>(
737 get_instance_proc_addr_(instance, "vkCreateDevice"));
738 VkDevice dev;
739 VkResult result = create_device(physical_dev, create_info, allocator, &dev);
740 if (result != VK_SUCCESS)
741 return result;
742
743 // initialize DeviceData
744 DeviceData& data = GetData(dev);
745 memset(&data, 0, sizeof(data));
746
747 if (!InitDispatchTable(dev, get_device_proc_addr_)) {
748 if (data.dispatch.DestroyDevice)
749 data.dispatch.DestroyDevice(dev, allocator);
750
751 return VK_ERROR_INITIALIZATION_FAILED;
752 }
753
754 steal_layers(data);
755
756 *dev_out = dev;
757
758 return VK_SUCCESS;
759}
760
761template <typename DataType>
762void LayerChain::steal_layers(DataType& data) {
763 data.layers = layers_;
764 data.layer_count = layer_count_;
765
766 layers_ = nullptr;
767 layer_count_ = 0;
768}
769
770void LayerChain::destroy_layers(ActiveLayer* layers,
771 uint32_t count,
772 const VkAllocationCallbacks& allocator) {
773 for (uint32_t i = 0; i < count; i++)
774 layers[i].ref.~LayerRef();
775
776 allocator.pfnFree(allocator.pUserData, layers);
777}
778
779VkBool32 LayerChain::debug_report_callback(VkDebugReportFlagsEXT flags,
780 VkDebugReportObjectTypeEXT obj_type,
781 uint64_t obj,
782 size_t location,
783 int32_t msg_code,
784 const char* layer_prefix,
785 const char* msg,
786 void* user_data) {
787 int prio;
788
789 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT)
790 prio = ANDROID_LOG_ERROR;
791 else if (flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT |
792 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT))
793 prio = ANDROID_LOG_WARN;
794 else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)
795 prio = ANDROID_LOG_INFO;
796 else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT)
797 prio = ANDROID_LOG_DEBUG;
798 else
799 prio = ANDROID_LOG_UNKNOWN;
800
801 LOG_PRI(prio, LOG_TAG, "[%s] Code %d : %s", layer_prefix, msg_code, msg);
802
803 (void)obj_type;
804 (void)obj;
805 (void)location;
806 (void)user_data;
807
808 return false;
809}
810
811VkResult LayerChain::create_instance(const VkInstanceCreateInfo* create_info,
812 const VkAllocationCallbacks* allocator,
813 VkInstance* instance_out) {
814 LayerChain chain(true,
815 (allocator) ? *allocator : driver::GetDefaultAllocator());
816
817 VkResult result = chain.activate_layers(
818 create_info->ppEnabledLayerNames, create_info->enabledLayerCount,
819 create_info->ppEnabledExtensionNames,
820 create_info->enabledExtensionCount);
821 if (result != VK_SUCCESS)
822 return result;
823
824 // use a local create info when the chain is not empty
825 VkInstanceCreateInfo local_create_info;
826 if (!chain.empty()) {
827 local_create_info = *create_info;
828 chain.modify_create_info(local_create_info);
829 create_info = &local_create_info;
830 }
831
832 return chain.create(create_info, allocator, instance_out);
833}
834
835VkResult LayerChain::create_device(VkPhysicalDevice physical_dev,
836 const VkDeviceCreateInfo* create_info,
837 const VkAllocationCallbacks* allocator,
838 VkDevice* dev_out) {
839 LayerChain chain(false, (allocator)
840 ? *allocator
841 : driver::GetData(physical_dev).allocator);
842
843 VkResult result = chain.activate_layers(
844 create_info->ppEnabledLayerNames, create_info->enabledLayerCount,
845 create_info->ppEnabledExtensionNames,
846 create_info->enabledExtensionCount);
847 if (result != VK_SUCCESS)
848 return result;
849
850 // use a local create info when the chain is not empty
851 VkDeviceCreateInfo local_create_info;
852 if (!chain.empty()) {
853 local_create_info = *create_info;
854 chain.modify_create_info(local_create_info);
855 create_info = &local_create_info;
856 }
857
858 return chain.create(physical_dev, create_info, allocator, dev_out);
859}
860
861void LayerChain::destroy_instance(VkInstance instance,
862 const VkAllocationCallbacks* allocator) {
863 InstanceData& data = GetData(instance);
864
865 if (data.debug_callback != VK_NULL_HANDLE)
866 data.destroy_debug_callback(instance, data.debug_callback, allocator);
867
868 ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers);
869 uint32_t layer_count = data.layer_count;
870
871 VkAllocationCallbacks local_allocator;
872 if (!allocator)
873 local_allocator = driver::GetData(instance).allocator;
874
875 // this also destroys InstanceData
876 data.dispatch.DestroyInstance(instance, allocator);
877
878 destroy_layers(layers, layer_count,
879 (allocator) ? *allocator : local_allocator);
880}
881
882void LayerChain::destroy_device(VkDevice device,
883 const VkAllocationCallbacks* allocator) {
884 DeviceData& data = GetData(device);
885
886 ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers);
887 uint32_t layer_count = data.layer_count;
888
889 VkAllocationCallbacks local_allocator;
890 if (!allocator)
891 local_allocator = driver::GetData(device).allocator;
892
893 // this also destroys DeviceData
894 data.dispatch.DestroyDevice(device, allocator);
895
896 destroy_layers(layers, layer_count,
897 (allocator) ? *allocator : local_allocator);
898}
899
900// ----------------------------------------------------------------------------
901
902bool EnsureInitialized() {
903 static std::once_flag once_flag;
904 static bool initialized;
905
906 std::call_once(once_flag, []() {
907 if (driver::OpenHAL()) {
908 DiscoverLayers();
909 initialized = true;
910 }
911 });
912
913 return initialized;
914}
915
916} // anonymous namespace
917
918VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
919 const VkAllocationCallbacks* pAllocator,
920 VkInstance* pInstance) {
921 if (!EnsureInitialized())
922 return VK_ERROR_INITIALIZATION_FAILED;
923
924 return LayerChain::create_instance(pCreateInfo, pAllocator, pInstance);
925}
926
927void DestroyInstance(VkInstance instance,
928 const VkAllocationCallbacks* pAllocator) {
929 if (instance != VK_NULL_HANDLE)
930 LayerChain::destroy_instance(instance, pAllocator);
931}
932
933VkResult CreateDevice(VkPhysicalDevice physicalDevice,
934 const VkDeviceCreateInfo* pCreateInfo,
935 const VkAllocationCallbacks* pAllocator,
936 VkDevice* pDevice) {
937 return LayerChain::create_device(physicalDevice, pCreateInfo, pAllocator,
938 pDevice);
939}
940
941void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
942 if (device != VK_NULL_HANDLE)
943 LayerChain::destroy_device(device, pAllocator);
944}
945
946VkResult EnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
947 VkLayerProperties* pProperties) {
948 if (!EnsureInitialized())
949 return VK_ERROR_INITIALIZATION_FAILED;
950
951 uint32_t count =
952 EnumerateInstanceLayers(pProperties ? *pPropertyCount : 0, pProperties);
953
954 if (!pProperties || *pPropertyCount > count)
955 *pPropertyCount = count;
956
957 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
958}
959
960VkResult EnumerateInstanceExtensionProperties(
961 const char* pLayerName,
962 uint32_t* pPropertyCount,
963 VkExtensionProperties* pProperties) {
964 if (!EnsureInitialized())
965 return VK_ERROR_INITIALIZATION_FAILED;
966
967 if (pLayerName) {
968 const VkExtensionProperties* props;
969 uint32_t count;
970 GetInstanceLayerExtensions(pLayerName, &props, &count);
971
972 if (!pProperties || *pPropertyCount > count)
973 *pPropertyCount = count;
974 if (pProperties)
975 std::copy(props, props + *pPropertyCount, pProperties);
976
977 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
978 }
979
980 // TODO how about extensions from implicitly enabled layers?
981 return vulkan::driver::EnumerateInstanceExtensionProperties(
982 nullptr, pPropertyCount, pProperties);
983}
984
985VkResult EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
986 uint32_t* pPropertyCount,
987 VkLayerProperties* pProperties) {
988 (void)physicalDevice;
989
990 uint32_t count =
991 EnumerateDeviceLayers(pProperties ? *pPropertyCount : 0, pProperties);
992
993 if (!pProperties || *pPropertyCount > count)
994 *pPropertyCount = count;
995
996 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
997}
998
999VkResult EnumerateDeviceExtensionProperties(
1000 VkPhysicalDevice physicalDevice,
1001 const char* pLayerName,
1002 uint32_t* pPropertyCount,
1003 VkExtensionProperties* pProperties) {
1004 if (pLayerName) {
1005 const VkExtensionProperties* props;
1006 uint32_t count;
1007 GetDeviceLayerExtensions(pLayerName, &props, &count);
1008
1009 if (!pProperties || *pPropertyCount > count)
1010 *pPropertyCount = count;
1011 if (pProperties)
1012 std::copy(props, props + *pPropertyCount, pProperties);
1013
1014 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1015 }
1016
1017 // TODO how about extensions from implicitly enabled layers?
1018 const InstanceData& data = GetData(physicalDevice);
1019 return data.dispatch.EnumerateDeviceExtensionProperties(
1020 physicalDevice, nullptr, pPropertyCount, pProperties);
1021}
1022
1023} // namespace api
1024} // namespace vulkan