blob: 549886feaf29870816772dcfaf063db391b512f9 [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
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800419 VkResult validate_extensions(const char* const* extension_names,
420 uint32_t extension_count);
421 VkResult validate_extensions(VkPhysicalDevice physical_dev,
422 const char* const* extension_names,
423 uint32_t extension_count);
424 VkExtensionProperties* allocate_driver_extension_array(
425 uint32_t count) const;
426 bool is_layer_extension(const char* name) const;
427 bool is_driver_extension(const char* name) const;
428
Chia-I Wu0c203242016-03-15 13:44:51 +0800429 template <typename DataType>
430 void steal_layers(DataType& data);
431
432 static void destroy_layers(ActiveLayer* layers,
433 uint32_t count,
434 const VkAllocationCallbacks& allocator);
435
436 static VKAPI_ATTR VkBool32
437 debug_report_callback(VkDebugReportFlagsEXT flags,
438 VkDebugReportObjectTypeEXT obj_type,
439 uint64_t obj,
440 size_t location,
441 int32_t msg_code,
442 const char* layer_prefix,
443 const char* msg,
444 void* user_data);
445
446 const bool is_instance_;
447 const VkAllocationCallbacks& allocator_;
448
449 OverrideLayerNames override_layers_;
450 OverrideExtensionNames override_extensions_;
451
452 ActiveLayer* layers_;
453 uint32_t layer_count_;
454
455 PFN_vkGetInstanceProcAddr get_instance_proc_addr_;
456 PFN_vkGetDeviceProcAddr get_device_proc_addr_;
457
458 union {
459 VkLayerInstanceCreateInfo instance_chain_info_;
460 VkLayerDeviceCreateInfo device_chain_info_;
461 };
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800462
463 VkExtensionProperties* driver_extensions_;
464 uint32_t driver_extension_count_;
Chia-I Wu0c203242016-03-15 13:44:51 +0800465};
466
467LayerChain::LayerChain(bool is_instance, const VkAllocationCallbacks& allocator)
468 : is_instance_(is_instance),
469 allocator_(allocator),
470 override_layers_(is_instance, allocator),
471 override_extensions_(is_instance, allocator),
472 layers_(nullptr),
473 layer_count_(0),
474 get_instance_proc_addr_(nullptr),
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800475 get_device_proc_addr_(nullptr),
476 driver_extensions_(nullptr),
477 driver_extension_count_(0) {}
Chia-I Wu0c203242016-03-15 13:44:51 +0800478
479LayerChain::~LayerChain() {
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800480 allocator_.pfnFree(allocator_.pUserData, driver_extensions_);
Chia-I Wu0c203242016-03-15 13:44:51 +0800481 destroy_layers(layers_, layer_count_, allocator_);
482}
483
484VkResult LayerChain::activate_layers(const char* const* layer_names,
485 uint32_t layer_count,
486 const char* const* extension_names,
487 uint32_t extension_count) {
488 VkResult result = override_layers_.parse(layer_names, layer_count);
489 if (result != VK_SUCCESS)
490 return result;
491
492 result = override_extensions_.parse(extension_names, extension_count);
493 if (result != VK_SUCCESS)
494 return result;
495
496 if (override_layers_.count()) {
497 layer_names = override_layers_.names();
498 layer_count = override_layers_.count();
499 }
500
501 if (!layer_count) {
502 // point head of chain to the driver
503 get_instance_proc_addr_ = driver::GetInstanceProcAddr;
504 if (!is_instance_)
505 get_device_proc_addr_ = driver::GetDeviceProcAddr;
506
507 return VK_SUCCESS;
508 }
509
510 layers_ = allocate_layer_array(layer_count);
511 if (!layers_)
512 return VK_ERROR_OUT_OF_HOST_MEMORY;
513
514 // load layers
515 for (uint32_t i = 0; i < layer_count; i++) {
516 result = load_layer(layers_[i], layer_names[i]);
517 if (result != VK_SUCCESS)
518 return result;
519
520 // count loaded layers for proper destructions on errors
521 layer_count_++;
522 }
523
524 setup_layer_links();
525
526 return VK_SUCCESS;
527}
528
529LayerChain::ActiveLayer* LayerChain::allocate_layer_array(
530 uint32_t count) const {
531 VkSystemAllocationScope scope = (is_instance_)
532 ? VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
533 : VK_SYSTEM_ALLOCATION_SCOPE_DEVICE;
534
535 return reinterpret_cast<ActiveLayer*>(allocator_.pfnAllocation(
536 allocator_.pUserData, sizeof(ActiveLayer) * count, alignof(ActiveLayer),
537 scope));
538}
539
540VkResult LayerChain::load_layer(ActiveLayer& layer, const char* name) {
541 if (is_instance_)
542 new (&layer) ActiveLayer{GetInstanceLayerRef(name), {}};
543 else
544 new (&layer) ActiveLayer{GetDeviceLayerRef(name), {}};
545
546 if (!layer.ref) {
547 ALOGE("Failed to load layer %s", name);
548 layer.ref.~LayerRef();
549 return VK_ERROR_LAYER_NOT_PRESENT;
550 }
551
552 ALOGI("Loaded %s layer %s", (is_instance_) ? "instance" : "device", name);
553
554 return VK_SUCCESS;
555}
556
557void LayerChain::setup_layer_links() {
558 if (is_instance_) {
559 for (uint32_t i = 0; i < layer_count_; i++) {
560 ActiveLayer& layer = layers_[i];
561
562 // point head of chain to the first layer
563 if (i == 0)
564 get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr();
565
566 // point tail of chain to the driver
567 if (i == layer_count_ - 1) {
568 layer.instance_link.pNext = nullptr;
569 layer.instance_link.pfnNextGetInstanceProcAddr =
570 driver::GetInstanceProcAddr;
571 break;
572 }
573
574 const ActiveLayer& next = layers_[i + 1];
575
576 // const_cast as some naughty layers want to modify our links!
577 layer.instance_link.pNext =
578 const_cast<VkLayerInstanceLink*>(&next.instance_link);
579 layer.instance_link.pfnNextGetInstanceProcAddr =
580 next.ref.GetGetInstanceProcAddr();
581 }
582 } else {
583 for (uint32_t i = 0; i < layer_count_; i++) {
584 ActiveLayer& layer = layers_[i];
585
586 // point head of chain to the first layer
587 if (i == 0) {
588 get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr();
589 get_device_proc_addr_ = layer.ref.GetGetDeviceProcAddr();
590 }
591
592 // point tail of chain to the driver
593 if (i == layer_count_ - 1) {
594 layer.device_link.pNext = nullptr;
595 layer.device_link.pfnNextGetInstanceProcAddr =
596 driver::GetInstanceProcAddr;
597 layer.device_link.pfnNextGetDeviceProcAddr =
598 driver::GetDeviceProcAddr;
599 break;
600 }
601
602 const ActiveLayer& next = layers_[i + 1];
603
604 // const_cast as some naughty layers want to modify our links!
605 layer.device_link.pNext =
606 const_cast<VkLayerDeviceLink*>(&next.device_link);
607 layer.device_link.pfnNextGetInstanceProcAddr =
608 next.ref.GetGetInstanceProcAddr();
609 layer.device_link.pfnNextGetDeviceProcAddr =
610 next.ref.GetGetDeviceProcAddr();
611 }
612 }
613}
614
615bool LayerChain::empty() const {
616 return (!layer_count_ && !override_layers_.count() &&
617 !override_extensions_.count());
618}
619
620void LayerChain::modify_create_info(VkInstanceCreateInfo& info) {
621 if (layer_count_) {
622 const ActiveLayer& layer = layers_[0];
623
624 instance_chain_info_.sType =
625 VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
626 instance_chain_info_.function = VK_LAYER_FUNCTION_LINK;
627 // TODO fix vk_layer_interface.h and get rid of const_cast?
628 instance_chain_info_.u.pLayerInfo =
629 const_cast<VkLayerInstanceLink*>(&layer.instance_link);
630
631 // insert layer info
632 instance_chain_info_.pNext = info.pNext;
633 info.pNext = &instance_chain_info_;
634 }
635
636 if (override_layers_.count()) {
637 info.enabledLayerCount = override_layers_.count();
638 info.ppEnabledLayerNames = override_layers_.names();
639 }
640
641 if (override_extensions_.count()) {
642 info.enabledExtensionCount = override_extensions_.count();
643 info.ppEnabledExtensionNames = override_extensions_.names();
644 }
645}
646
647void LayerChain::modify_create_info(VkDeviceCreateInfo& info) {
648 if (layer_count_) {
649 const ActiveLayer& layer = layers_[0];
650
651 device_chain_info_.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
652 device_chain_info_.function = VK_LAYER_FUNCTION_LINK;
653 // TODO fix vk_layer_interface.h and get rid of const_cast?
654 device_chain_info_.u.pLayerInfo =
655 const_cast<VkLayerDeviceLink*>(&layer.device_link);
656
657 // insert layer info
658 device_chain_info_.pNext = info.pNext;
659 info.pNext = &device_chain_info_;
660 }
661
662 if (override_layers_.count()) {
663 info.enabledLayerCount = override_layers_.count();
664 info.ppEnabledLayerNames = override_layers_.names();
665 }
666
667 if (override_extensions_.count()) {
668 info.enabledExtensionCount = override_extensions_.count();
669 info.ppEnabledExtensionNames = override_extensions_.names();
670 }
671}
672
673VkResult LayerChain::create(const VkInstanceCreateInfo* create_info,
674 const VkAllocationCallbacks* allocator,
675 VkInstance* instance_out) {
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800676 VkResult result = validate_extensions(create_info->ppEnabledExtensionNames,
677 create_info->enabledExtensionCount);
678 if (result != VK_SUCCESS)
679 return result;
680
Chia-I Wu0c203242016-03-15 13:44:51 +0800681 // call down the chain
682 PFN_vkCreateInstance create_instance =
683 reinterpret_cast<PFN_vkCreateInstance>(
684 get_instance_proc_addr_(VK_NULL_HANDLE, "vkCreateInstance"));
685 VkInstance instance;
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800686 result = create_instance(create_info, allocator, &instance);
Chia-I Wu0c203242016-03-15 13:44:51 +0800687 if (result != VK_SUCCESS)
688 return result;
689
690 // initialize InstanceData
691 InstanceData& data = GetData(instance);
692 memset(&data, 0, sizeof(data));
693
694 data.instance = instance;
695
696 if (!InitDispatchTable(instance, get_instance_proc_addr_)) {
697 if (data.dispatch.DestroyInstance)
698 data.dispatch.DestroyInstance(instance, allocator);
699
700 return VK_ERROR_INITIALIZATION_FAILED;
701 }
702
703 // install debug report callback
704 if (override_extensions_.install_debug_callback()) {
705 PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
706 reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
707 get_instance_proc_addr_(instance,
708 "vkCreateDebugReportCallbackEXT"));
709 data.destroy_debug_callback =
710 reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
711 get_instance_proc_addr_(instance,
712 "vkDestroyDebugReportCallbackEXT"));
713 if (!create_debug_report_callback || !data.destroy_debug_callback) {
714 ALOGE("Broken VK_EXT_debug_report support");
715 data.dispatch.DestroyInstance(instance, allocator);
716 return VK_ERROR_INITIALIZATION_FAILED;
717 }
718
719 VkDebugReportCallbackCreateInfoEXT debug_callback_info = {};
720 debug_callback_info.sType =
721 VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
722 debug_callback_info.flags =
723 VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
724 debug_callback_info.pfnCallback = debug_report_callback;
725
726 VkDebugReportCallbackEXT debug_callback;
727 result = create_debug_report_callback(instance, &debug_callback_info,
728 nullptr, &debug_callback);
729 if (result != VK_SUCCESS) {
730 ALOGE("Failed to install debug report callback");
731 data.dispatch.DestroyInstance(instance, allocator);
732 return VK_ERROR_INITIALIZATION_FAILED;
733 }
734
735 data.debug_callback = debug_callback;
736
737 ALOGI("Installed debug report callback");
738 }
739
740 steal_layers(data);
741
742 *instance_out = instance;
743
744 return VK_SUCCESS;
745}
746
747VkResult LayerChain::create(VkPhysicalDevice physical_dev,
748 const VkDeviceCreateInfo* create_info,
749 const VkAllocationCallbacks* allocator,
750 VkDevice* dev_out) {
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800751 VkResult result =
752 validate_extensions(physical_dev, create_info->ppEnabledExtensionNames,
753 create_info->enabledExtensionCount);
754 if (result != VK_SUCCESS)
755 return result;
756
Chia-I Wu0c203242016-03-15 13:44:51 +0800757 // call down the chain
758 //
759 // TODO Instance call chain available at
760 // GetData(physical_dev).dispatch.CreateDevice is ignored. Is that
761 // right?
762 VkInstance instance = GetData(physical_dev).instance;
763 PFN_vkCreateDevice create_device = reinterpret_cast<PFN_vkCreateDevice>(
764 get_instance_proc_addr_(instance, "vkCreateDevice"));
765 VkDevice dev;
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800766 result = create_device(physical_dev, create_info, allocator, &dev);
Chia-I Wu0c203242016-03-15 13:44:51 +0800767 if (result != VK_SUCCESS)
768 return result;
769
770 // initialize DeviceData
771 DeviceData& data = GetData(dev);
772 memset(&data, 0, sizeof(data));
773
774 if (!InitDispatchTable(dev, get_device_proc_addr_)) {
775 if (data.dispatch.DestroyDevice)
776 data.dispatch.DestroyDevice(dev, allocator);
777
778 return VK_ERROR_INITIALIZATION_FAILED;
779 }
780
781 steal_layers(data);
782
783 *dev_out = dev;
784
785 return VK_SUCCESS;
786}
787
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800788VkResult LayerChain::validate_extensions(const char* const* extension_names,
789 uint32_t extension_count) {
790 if (!extension_count)
791 return VK_SUCCESS;
792
793 // query driver instance extensions
794 uint32_t count;
795 VkResult result =
796 EnumerateInstanceExtensionProperties(nullptr, &count, nullptr);
797 if (result == VK_SUCCESS && count) {
798 driver_extensions_ = allocate_driver_extension_array(count);
799 result = (driver_extensions_) ? EnumerateInstanceExtensionProperties(
800 nullptr, &count, driver_extensions_)
801 : VK_ERROR_OUT_OF_HOST_MEMORY;
802 }
803 if (result != VK_SUCCESS)
804 return result;
805
806 driver_extension_count_ = count;
807
808 for (uint32_t i = 0; i < extension_count; i++) {
809 const char* name = extension_names[i];
810 if (!is_layer_extension(name) && !is_driver_extension(name)) {
811 ALOGE("Failed to enable missing instance extension %s", name);
812 return VK_ERROR_EXTENSION_NOT_PRESENT;
813 }
814 }
815
816 return VK_SUCCESS;
817}
818
819VkResult LayerChain::validate_extensions(VkPhysicalDevice physical_dev,
820 const char* const* extension_names,
821 uint32_t extension_count) {
822 if (!extension_count)
823 return VK_SUCCESS;
824
825 // query driver device extensions
826 uint32_t count;
827 VkResult result = EnumerateDeviceExtensionProperties(physical_dev, nullptr,
828 &count, nullptr);
829 if (result == VK_SUCCESS && count) {
830 driver_extensions_ = allocate_driver_extension_array(count);
831 result = (driver_extensions_)
832 ? EnumerateDeviceExtensionProperties(
833 physical_dev, nullptr, &count, driver_extensions_)
834 : VK_ERROR_OUT_OF_HOST_MEMORY;
835 }
836 if (result != VK_SUCCESS)
837 return result;
838
839 driver_extension_count_ = count;
840
841 for (uint32_t i = 0; i < extension_count; i++) {
842 const char* name = extension_names[i];
843 if (!is_layer_extension(name) && !is_driver_extension(name)) {
844 ALOGE("Failed to enable missing device extension %s", name);
845 return VK_ERROR_EXTENSION_NOT_PRESENT;
846 }
847 }
848
849 return VK_SUCCESS;
850}
851
852VkExtensionProperties* LayerChain::allocate_driver_extension_array(
853 uint32_t count) const {
854 return reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
855 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
856 alignof(VkExtensionProperties), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
857}
858
859bool LayerChain::is_layer_extension(const char* name) const {
860 for (uint32_t i = 0; i < layer_count_; i++) {
861 const ActiveLayer& layer = layers_[i];
862 if (layer.ref.SupportsExtension(name))
863 return true;
864 }
865
866 return false;
867}
868
869bool LayerChain::is_driver_extension(const char* name) const {
870 for (uint32_t i = 0; i < driver_extension_count_; i++) {
871 if (strcmp(driver_extensions_[i].extensionName, name) == 0)
872 return true;
873 }
874
875 return false;
876}
877
Chia-I Wu0c203242016-03-15 13:44:51 +0800878template <typename DataType>
879void LayerChain::steal_layers(DataType& data) {
880 data.layers = layers_;
881 data.layer_count = layer_count_;
882
883 layers_ = nullptr;
884 layer_count_ = 0;
885}
886
887void LayerChain::destroy_layers(ActiveLayer* layers,
888 uint32_t count,
889 const VkAllocationCallbacks& allocator) {
890 for (uint32_t i = 0; i < count; i++)
891 layers[i].ref.~LayerRef();
892
893 allocator.pfnFree(allocator.pUserData, layers);
894}
895
896VkBool32 LayerChain::debug_report_callback(VkDebugReportFlagsEXT flags,
897 VkDebugReportObjectTypeEXT obj_type,
898 uint64_t obj,
899 size_t location,
900 int32_t msg_code,
901 const char* layer_prefix,
902 const char* msg,
903 void* user_data) {
904 int prio;
905
906 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT)
907 prio = ANDROID_LOG_ERROR;
908 else if (flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT |
909 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT))
910 prio = ANDROID_LOG_WARN;
911 else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)
912 prio = ANDROID_LOG_INFO;
913 else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT)
914 prio = ANDROID_LOG_DEBUG;
915 else
916 prio = ANDROID_LOG_UNKNOWN;
917
918 LOG_PRI(prio, LOG_TAG, "[%s] Code %d : %s", layer_prefix, msg_code, msg);
919
920 (void)obj_type;
921 (void)obj;
922 (void)location;
923 (void)user_data;
924
925 return false;
926}
927
928VkResult LayerChain::create_instance(const VkInstanceCreateInfo* create_info,
929 const VkAllocationCallbacks* allocator,
930 VkInstance* instance_out) {
931 LayerChain chain(true,
932 (allocator) ? *allocator : driver::GetDefaultAllocator());
933
934 VkResult result = chain.activate_layers(
935 create_info->ppEnabledLayerNames, create_info->enabledLayerCount,
936 create_info->ppEnabledExtensionNames,
937 create_info->enabledExtensionCount);
938 if (result != VK_SUCCESS)
939 return result;
940
941 // use a local create info when the chain is not empty
942 VkInstanceCreateInfo local_create_info;
943 if (!chain.empty()) {
944 local_create_info = *create_info;
945 chain.modify_create_info(local_create_info);
946 create_info = &local_create_info;
947 }
948
949 return chain.create(create_info, allocator, instance_out);
950}
951
952VkResult LayerChain::create_device(VkPhysicalDevice physical_dev,
953 const VkDeviceCreateInfo* create_info,
954 const VkAllocationCallbacks* allocator,
955 VkDevice* dev_out) {
956 LayerChain chain(false, (allocator)
957 ? *allocator
958 : driver::GetData(physical_dev).allocator);
959
960 VkResult result = chain.activate_layers(
961 create_info->ppEnabledLayerNames, create_info->enabledLayerCount,
962 create_info->ppEnabledExtensionNames,
963 create_info->enabledExtensionCount);
964 if (result != VK_SUCCESS)
965 return result;
966
967 // use a local create info when the chain is not empty
968 VkDeviceCreateInfo local_create_info;
969 if (!chain.empty()) {
970 local_create_info = *create_info;
971 chain.modify_create_info(local_create_info);
972 create_info = &local_create_info;
973 }
974
975 return chain.create(physical_dev, create_info, allocator, dev_out);
976}
977
978void LayerChain::destroy_instance(VkInstance instance,
979 const VkAllocationCallbacks* allocator) {
980 InstanceData& data = GetData(instance);
981
982 if (data.debug_callback != VK_NULL_HANDLE)
983 data.destroy_debug_callback(instance, data.debug_callback, allocator);
984
985 ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers);
986 uint32_t layer_count = data.layer_count;
987
988 VkAllocationCallbacks local_allocator;
989 if (!allocator)
990 local_allocator = driver::GetData(instance).allocator;
991
992 // this also destroys InstanceData
993 data.dispatch.DestroyInstance(instance, allocator);
994
995 destroy_layers(layers, layer_count,
996 (allocator) ? *allocator : local_allocator);
997}
998
999void LayerChain::destroy_device(VkDevice device,
1000 const VkAllocationCallbacks* allocator) {
1001 DeviceData& data = GetData(device);
1002
1003 ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers);
1004 uint32_t layer_count = data.layer_count;
1005
1006 VkAllocationCallbacks local_allocator;
1007 if (!allocator)
1008 local_allocator = driver::GetData(device).allocator;
1009
1010 // this also destroys DeviceData
1011 data.dispatch.DestroyDevice(device, allocator);
1012
1013 destroy_layers(layers, layer_count,
1014 (allocator) ? *allocator : local_allocator);
1015}
1016
1017// ----------------------------------------------------------------------------
1018
1019bool EnsureInitialized() {
1020 static std::once_flag once_flag;
1021 static bool initialized;
1022
1023 std::call_once(once_flag, []() {
1024 if (driver::OpenHAL()) {
1025 DiscoverLayers();
1026 initialized = true;
1027 }
1028 });
1029
1030 return initialized;
1031}
1032
1033} // anonymous namespace
1034
1035VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1036 const VkAllocationCallbacks* pAllocator,
1037 VkInstance* pInstance) {
1038 if (!EnsureInitialized())
1039 return VK_ERROR_INITIALIZATION_FAILED;
1040
1041 return LayerChain::create_instance(pCreateInfo, pAllocator, pInstance);
1042}
1043
1044void DestroyInstance(VkInstance instance,
1045 const VkAllocationCallbacks* pAllocator) {
1046 if (instance != VK_NULL_HANDLE)
1047 LayerChain::destroy_instance(instance, pAllocator);
1048}
1049
1050VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1051 const VkDeviceCreateInfo* pCreateInfo,
1052 const VkAllocationCallbacks* pAllocator,
1053 VkDevice* pDevice) {
1054 return LayerChain::create_device(physicalDevice, pCreateInfo, pAllocator,
1055 pDevice);
1056}
1057
1058void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1059 if (device != VK_NULL_HANDLE)
1060 LayerChain::destroy_device(device, pAllocator);
1061}
1062
1063VkResult EnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
1064 VkLayerProperties* pProperties) {
1065 if (!EnsureInitialized())
1066 return VK_ERROR_INITIALIZATION_FAILED;
1067
1068 uint32_t count =
1069 EnumerateInstanceLayers(pProperties ? *pPropertyCount : 0, pProperties);
1070
1071 if (!pProperties || *pPropertyCount > count)
1072 *pPropertyCount = count;
1073
1074 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1075}
1076
1077VkResult EnumerateInstanceExtensionProperties(
1078 const char* pLayerName,
1079 uint32_t* pPropertyCount,
1080 VkExtensionProperties* pProperties) {
1081 if (!EnsureInitialized())
1082 return VK_ERROR_INITIALIZATION_FAILED;
1083
1084 if (pLayerName) {
1085 const VkExtensionProperties* props;
1086 uint32_t count;
1087 GetInstanceLayerExtensions(pLayerName, &props, &count);
1088
1089 if (!pProperties || *pPropertyCount > count)
1090 *pPropertyCount = count;
1091 if (pProperties)
1092 std::copy(props, props + *pPropertyCount, pProperties);
1093
1094 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1095 }
1096
1097 // TODO how about extensions from implicitly enabled layers?
1098 return vulkan::driver::EnumerateInstanceExtensionProperties(
1099 nullptr, pPropertyCount, pProperties);
1100}
1101
1102VkResult EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
1103 uint32_t* pPropertyCount,
1104 VkLayerProperties* pProperties) {
1105 (void)physicalDevice;
1106
1107 uint32_t count =
1108 EnumerateDeviceLayers(pProperties ? *pPropertyCount : 0, pProperties);
1109
1110 if (!pProperties || *pPropertyCount > count)
1111 *pPropertyCount = count;
1112
1113 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1114}
1115
1116VkResult EnumerateDeviceExtensionProperties(
1117 VkPhysicalDevice physicalDevice,
1118 const char* pLayerName,
1119 uint32_t* pPropertyCount,
1120 VkExtensionProperties* pProperties) {
1121 if (pLayerName) {
1122 const VkExtensionProperties* props;
1123 uint32_t count;
1124 GetDeviceLayerExtensions(pLayerName, &props, &count);
1125
1126 if (!pProperties || *pPropertyCount > count)
1127 *pPropertyCount = count;
1128 if (pProperties)
1129 std::copy(props, props + *pPropertyCount, pProperties);
1130
1131 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1132 }
1133
1134 // TODO how about extensions from implicitly enabled layers?
1135 const InstanceData& data = GetData(physicalDevice);
1136 return data.dispatch.EnumerateDeviceExtensionProperties(
1137 physicalDevice, nullptr, pPropertyCount, pProperties);
1138}
1139
1140} // namespace api
1141} // namespace vulkan