• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include <algorithm>
30 #include <mutex>
31 #include <new>
32 #include <utility>
33 
34 #include <android-base/strings.h>
35 #include <cutils/properties.h>
36 #include <log/log.h>
37 #include <utils/Trace.h>
38 
39 #include <vulkan/vk_layer_interface.h>
40 #include <graphicsenv/GraphicsEnv.h>
41 #include "api.h"
42 #include "driver.h"
43 #include "layers_extensions.h"
44 
45 
46 namespace vulkan {
47 namespace api {
48 
49 namespace {
50 
51 // Provide overridden layer names when there are implicit layers.  No effect
52 // otherwise.
53 class OverrideLayerNames {
54    public:
OverrideLayerNames(bool is_instance,const VkAllocationCallbacks & allocator)55     OverrideLayerNames(bool is_instance, const VkAllocationCallbacks& allocator)
56         : is_instance_(is_instance),
57           allocator_(allocator),
58           scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND),
59           names_(nullptr),
60           name_count_(0),
61           implicit_layers_() {
62         implicit_layers_.result = VK_SUCCESS;
63     }
64 
~OverrideLayerNames()65     ~OverrideLayerNames() {
66         allocator_.pfnFree(allocator_.pUserData, names_);
67         allocator_.pfnFree(allocator_.pUserData, implicit_layers_.elements);
68         allocator_.pfnFree(allocator_.pUserData, implicit_layers_.name_pool);
69     }
70 
Parse(const char * const * names,uint32_t count)71     VkResult Parse(const char* const* names, uint32_t count) {
72         AddImplicitLayers();
73 
74         const auto& arr = implicit_layers_;
75         if (arr.result != VK_SUCCESS)
76             return arr.result;
77 
78         // no need to override when there is no implicit layer
79         if (!arr.count)
80             return VK_SUCCESS;
81 
82         names_ = AllocateNameArray(arr.count + count);
83         if (!names_)
84             return VK_ERROR_OUT_OF_HOST_MEMORY;
85 
86         // add implicit layer names
87         for (uint32_t i = 0; i < arr.count; i++)
88             names_[i] = GetImplicitLayerName(i);
89 
90         name_count_ = arr.count;
91 
92         // add explicit layer names
93         for (uint32_t i = 0; i < count; i++) {
94             // ignore explicit layers that are also implicit
95             if (IsImplicitLayer(names[i]))
96                 continue;
97 
98             names_[name_count_++] = names[i];
99         }
100 
101         return VK_SUCCESS;
102     }
103 
Names() const104     const char* const* Names() const { return names_; }
105 
Count() const106     uint32_t Count() const { return name_count_; }
107 
108    private:
109     struct ImplicitLayer {
110         int priority;
111         size_t name_offset;
112     };
113 
114     struct ImplicitLayerArray {
115         ImplicitLayer* elements;
116         uint32_t max_count;
117         uint32_t count;
118 
119         char* name_pool;
120         size_t max_pool_size;
121         size_t pool_size;
122 
123         VkResult result;
124     };
125 
AddImplicitLayers()126     void AddImplicitLayers() {
127         if (!is_instance_ || !driver::Debuggable())
128             return;
129 
130         GetLayersFromSettings();
131 
132         // If no layers specified via Settings, check legacy properties
133         if (implicit_layers_.count <= 0) {
134             ParseDebugVulkanLayers();
135             property_list(ParseDebugVulkanLayer, this);
136 
137             // sort by priorities
138             auto& arr = implicit_layers_;
139             std::sort(arr.elements, arr.elements + arr.count,
140                       [](const ImplicitLayer& a, const ImplicitLayer& b) {
141                           return (a.priority < b.priority);
142                       });
143         }
144     }
145 
GetLayersFromSettings()146     void GetLayersFromSettings() {
147         // These will only be available if conditions are met in GraphicsEnvironemnt
148         // gpu_debug_layers = layer1:layer2:layerN
149         const std::string layers = android::GraphicsEnv::getInstance().getDebugLayers();
150         if (!layers.empty()) {
151             ALOGV("Debug layer list: %s", layers.c_str());
152             std::vector<std::string> paths = android::base::Split(layers, ":");
153             for (uint32_t i = 0; i < paths.size(); i++) {
154                 AddImplicitLayer(int(i), paths[i].c_str(), paths[i].length());
155             }
156         }
157     }
158 
ParseDebugVulkanLayers()159     void ParseDebugVulkanLayers() {
160         // debug.vulkan.layers specifies colon-separated layer names
161         char prop[PROPERTY_VALUE_MAX];
162         if (!property_get("debug.vulkan.layers", prop, ""))
163             return;
164 
165         // assign negative/high priorities to them
166         int prio = -PROPERTY_VALUE_MAX;
167 
168         const char* p = prop;
169         const char* delim;
170         while ((delim = strchr(p, ':'))) {
171             if (delim > p)
172                 AddImplicitLayer(prio, p, static_cast<size_t>(delim - p));
173 
174             prio++;
175             p = delim + 1;
176         }
177 
178         if (p[0] != '\0')
179             AddImplicitLayer(prio, p, strlen(p));
180     }
181 
ParseDebugVulkanLayer(const char * key,const char * val,void * user_data)182     static void ParseDebugVulkanLayer(const char* key,
183                                       const char* val,
184                                       void* user_data) {
185         static const char prefix[] = "debug.vulkan.layer.";
186         const size_t prefix_len = sizeof(prefix) - 1;
187 
188         if (strncmp(key, prefix, prefix_len) || val[0] == '\0')
189             return;
190         key += prefix_len;
191 
192         // debug.vulkan.layer.<priority>
193         int priority = -1;
194         if (key[0] >= '0' && key[0] <= '9')
195             priority = atoi(key);
196 
197         if (priority < 0) {
198             ALOGW("Ignored implicit layer %s with invalid priority %s", val,
199                   key);
200             return;
201         }
202 
203         OverrideLayerNames& override_layers =
204             *reinterpret_cast<OverrideLayerNames*>(user_data);
205         override_layers.AddImplicitLayer(priority, val, strlen(val));
206     }
207 
AddImplicitLayer(int priority,const char * name,size_t len)208     void AddImplicitLayer(int priority, const char* name, size_t len) {
209         if (!GrowImplicitLayerArray(1, 0))
210             return;
211 
212         auto& arr = implicit_layers_;
213         auto& layer = arr.elements[arr.count++];
214 
215         layer.priority = priority;
216         layer.name_offset = AddImplicitLayerName(name, len);
217 
218         ALOGV("Added implicit layer %s", GetImplicitLayerName(arr.count - 1));
219     }
220 
AddImplicitLayerName(const char * name,size_t len)221     size_t AddImplicitLayerName(const char* name, size_t len) {
222         if (!GrowImplicitLayerArray(0, len + 1))
223             return 0;
224 
225         // add the name to the pool
226         auto& arr = implicit_layers_;
227         size_t offset = arr.pool_size;
228         char* dst = arr.name_pool + offset;
229 
230         std::copy(name, name + len, dst);
231         dst[len] = '\0';
232 
233         arr.pool_size += len + 1;
234 
235         return offset;
236     }
237 
GrowImplicitLayerArray(uint32_t layer_count,size_t name_size)238     bool GrowImplicitLayerArray(uint32_t layer_count, size_t name_size) {
239         const uint32_t initial_max_count = 16;
240         const size_t initial_max_pool_size = 512;
241 
242         auto& arr = implicit_layers_;
243 
244         // grow the element array if needed
245         while (arr.count + layer_count > arr.max_count) {
246             uint32_t new_max_count =
247                 (arr.max_count) ? (arr.max_count << 1) : initial_max_count;
248             void* new_mem = nullptr;
249 
250             if (new_max_count > arr.max_count) {
251                 new_mem = allocator_.pfnReallocation(
252                     allocator_.pUserData, arr.elements,
253                     sizeof(ImplicitLayer) * new_max_count,
254                     alignof(ImplicitLayer), scope_);
255             }
256 
257             if (!new_mem) {
258                 arr.result = VK_ERROR_OUT_OF_HOST_MEMORY;
259                 arr.count = 0;
260                 return false;
261             }
262 
263             arr.elements = reinterpret_cast<ImplicitLayer*>(new_mem);
264             arr.max_count = new_max_count;
265         }
266 
267         // grow the name pool if needed
268         while (arr.pool_size + name_size > arr.max_pool_size) {
269             size_t new_max_pool_size = (arr.max_pool_size)
270                                            ? (arr.max_pool_size << 1)
271                                            : initial_max_pool_size;
272             void* new_mem = nullptr;
273 
274             if (new_max_pool_size > arr.max_pool_size) {
275                 new_mem = allocator_.pfnReallocation(
276                     allocator_.pUserData, arr.name_pool, new_max_pool_size,
277                     alignof(char), scope_);
278             }
279 
280             if (!new_mem) {
281                 arr.result = VK_ERROR_OUT_OF_HOST_MEMORY;
282                 arr.pool_size = 0;
283                 return false;
284             }
285 
286             arr.name_pool = reinterpret_cast<char*>(new_mem);
287             arr.max_pool_size = new_max_pool_size;
288         }
289 
290         return true;
291     }
292 
GetImplicitLayerName(uint32_t index) const293     const char* GetImplicitLayerName(uint32_t index) const {
294         const auto& arr = implicit_layers_;
295 
296         // this may return nullptr when arr.result is not VK_SUCCESS
297         return implicit_layers_.name_pool + arr.elements[index].name_offset;
298     }
299 
IsImplicitLayer(const char * name) const300     bool IsImplicitLayer(const char* name) const {
301         const auto& arr = implicit_layers_;
302 
303         for (uint32_t i = 0; i < arr.count; i++) {
304             if (strcmp(name, GetImplicitLayerName(i)) == 0)
305                 return true;
306         }
307 
308         return false;
309     }
310 
AllocateNameArray(uint32_t count) const311     const char** AllocateNameArray(uint32_t count) const {
312         return reinterpret_cast<const char**>(allocator_.pfnAllocation(
313             allocator_.pUserData, sizeof(const char*) * count,
314             alignof(const char*), scope_));
315     }
316 
317     const bool is_instance_;
318     const VkAllocationCallbacks& allocator_;
319     const VkSystemAllocationScope scope_;
320 
321     const char** names_;
322     uint32_t name_count_;
323 
324     ImplicitLayerArray implicit_layers_;
325 };
326 
327 // Provide overridden extension names when there are implicit extensions.
328 // No effect otherwise.
329 //
330 // This is used only to enable VK_EXT_debug_report.
331 class OverrideExtensionNames {
332    public:
OverrideExtensionNames(bool is_instance,const VkAllocationCallbacks & allocator)333     OverrideExtensionNames(bool is_instance,
334                            const VkAllocationCallbacks& allocator)
335         : is_instance_(is_instance),
336           allocator_(allocator),
337           scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND),
338           names_(nullptr),
339           name_count_(0),
340           install_debug_callback_(false) {}
341 
~OverrideExtensionNames()342     ~OverrideExtensionNames() {
343         allocator_.pfnFree(allocator_.pUserData, names_);
344     }
345 
Parse(const char * const * names,uint32_t count)346     VkResult Parse(const char* const* names, uint32_t count) {
347         // this is only for debug.vulkan.enable_callback
348         if (!EnableDebugCallback())
349             return VK_SUCCESS;
350 
351         names_ = AllocateNameArray(count + 1);
352         if (!names_)
353             return VK_ERROR_OUT_OF_HOST_MEMORY;
354 
355         std::copy(names, names + count, names_);
356 
357         name_count_ = count;
358         names_[name_count_++] = "VK_EXT_debug_report";
359 
360         install_debug_callback_ = true;
361 
362         return VK_SUCCESS;
363     }
364 
Names() const365     const char* const* Names() const { return names_; }
366 
Count() const367     uint32_t Count() const { return name_count_; }
368 
InstallDebugCallback() const369     bool InstallDebugCallback() const { return install_debug_callback_; }
370 
371    private:
EnableDebugCallback() const372     bool EnableDebugCallback() const {
373         return (is_instance_ && driver::Debuggable() &&
374                 property_get_bool("debug.vulkan.enable_callback", false));
375     }
376 
AllocateNameArray(uint32_t count) const377     const char** AllocateNameArray(uint32_t count) const {
378         return reinterpret_cast<const char**>(allocator_.pfnAllocation(
379             allocator_.pUserData, sizeof(const char*) * count,
380             alignof(const char*), scope_));
381     }
382 
383     const bool is_instance_;
384     const VkAllocationCallbacks& allocator_;
385     const VkSystemAllocationScope scope_;
386 
387     const char** names_;
388     uint32_t name_count_;
389     bool install_debug_callback_;
390 };
391 
392 // vkCreateInstance and vkCreateDevice helpers with support for layer
393 // chaining.
394 class LayerChain {
395    public:
396     struct ActiveLayer {
397         LayerRef ref;
398         union {
399             VkLayerInstanceLink instance_link;
400             VkLayerDeviceLink device_link;
401         };
402     };
403 
404     static VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
405                                    const VkAllocationCallbacks* allocator,
406                                    VkInstance* instance_out);
407 
408     static VkResult CreateDevice(VkPhysicalDevice physical_dev,
409                                  const VkDeviceCreateInfo* create_info,
410                                  const VkAllocationCallbacks* allocator,
411                                  VkDevice* dev_out);
412 
413     static void DestroyInstance(VkInstance instance,
414                                 const VkAllocationCallbacks* allocator);
415 
416     static void DestroyDevice(VkDevice dev,
417                               const VkAllocationCallbacks* allocator);
418 
419     static const ActiveLayer* GetActiveLayers(VkPhysicalDevice physical_dev,
420                                               uint32_t& count);
421 
422    private:
423     LayerChain(bool is_instance,
424                const driver::DebugReportLogger& logger,
425                const VkAllocationCallbacks& allocator);
426     ~LayerChain();
427 
428     VkResult ActivateLayers(const char* const* layer_names,
429                             uint32_t layer_count,
430                             const char* const* extension_names,
431                             uint32_t extension_count);
432     VkResult ActivateLayers(VkPhysicalDevice physical_dev,
433                             const char* const* layer_names,
434                             uint32_t layer_count,
435                             const char* const* extension_names,
436                             uint32_t extension_count);
437     ActiveLayer* AllocateLayerArray(uint32_t count) const;
438     VkResult LoadLayer(ActiveLayer& layer, const char* name);
439     void SetupLayerLinks();
440 
441     bool Empty() const;
442     void ModifyCreateInfo(VkInstanceCreateInfo& info);
443     void ModifyCreateInfo(VkDeviceCreateInfo& info);
444 
445     VkResult Create(const VkInstanceCreateInfo* create_info,
446                     const VkAllocationCallbacks* allocator,
447                     VkInstance* instance_out);
448 
449     VkResult Create(VkPhysicalDevice physical_dev,
450                     const VkDeviceCreateInfo* create_info,
451                     const VkAllocationCallbacks* allocator,
452                     VkDevice* dev_out);
453 
454     VkResult ValidateExtensions(const char* const* extension_names,
455                                 uint32_t extension_count);
456     VkResult ValidateExtensions(VkPhysicalDevice physical_dev,
457                                 const char* const* extension_names,
458                                 uint32_t extension_count);
459     VkExtensionProperties* AllocateDriverExtensionArray(uint32_t count) const;
460     bool IsLayerExtension(const char* name) const;
461     bool IsDriverExtension(const char* name) const;
462 
463     template <typename DataType>
464     void StealLayers(DataType& data);
465 
466     static void DestroyLayers(ActiveLayer* layers,
467                               uint32_t count,
468                               const VkAllocationCallbacks& allocator);
469 
470     static VKAPI_ATTR VkResult SetInstanceLoaderData(VkInstance instance,
471                                                      void* object);
472     static VKAPI_ATTR VkResult SetDeviceLoaderData(VkDevice device,
473                                                    void* object);
474 
475     static VKAPI_ATTR VkBool32
476     DebugReportCallback(VkDebugReportFlagsEXT flags,
477                         VkDebugReportObjectTypeEXT obj_type,
478                         uint64_t obj,
479                         size_t location,
480                         int32_t msg_code,
481                         const char* layer_prefix,
482                         const char* msg,
483                         void* user_data);
484 
485     const bool is_instance_;
486     const driver::DebugReportLogger& logger_;
487     const VkAllocationCallbacks& allocator_;
488 
489     OverrideLayerNames override_layers_;
490     OverrideExtensionNames override_extensions_;
491 
492     ActiveLayer* layers_;
493     uint32_t layer_count_;
494 
495     PFN_vkGetInstanceProcAddr get_instance_proc_addr_;
496     PFN_vkGetDeviceProcAddr get_device_proc_addr_;
497 
498     union {
499         VkLayerInstanceCreateInfo instance_chain_info_[2];
500         VkLayerDeviceCreateInfo device_chain_info_[2];
501     };
502 
503     VkExtensionProperties* driver_extensions_;
504     uint32_t driver_extension_count_;
505     std::bitset<driver::ProcHook::EXTENSION_COUNT> enabled_extensions_;
506 };
507 
LayerChain(bool is_instance,const driver::DebugReportLogger & logger,const VkAllocationCallbacks & allocator)508 LayerChain::LayerChain(bool is_instance,
509                        const driver::DebugReportLogger& logger,
510                        const VkAllocationCallbacks& allocator)
511     : is_instance_(is_instance),
512       logger_(logger),
513       allocator_(allocator),
514       override_layers_(is_instance, allocator),
515       override_extensions_(is_instance, allocator),
516       layers_(nullptr),
517       layer_count_(0),
518       get_instance_proc_addr_(nullptr),
519       get_device_proc_addr_(nullptr),
520       driver_extensions_(nullptr),
521       driver_extension_count_(0) {
522     enabled_extensions_.set(driver::ProcHook::EXTENSION_CORE);
523 }
524 
~LayerChain()525 LayerChain::~LayerChain() {
526     allocator_.pfnFree(allocator_.pUserData, driver_extensions_);
527     DestroyLayers(layers_, layer_count_, allocator_);
528 }
529 
ActivateLayers(const char * const * layer_names,uint32_t layer_count,const char * const * extension_names,uint32_t extension_count)530 VkResult LayerChain::ActivateLayers(const char* const* layer_names,
531                                     uint32_t layer_count,
532                                     const char* const* extension_names,
533                                     uint32_t extension_count) {
534     VkResult result = override_layers_.Parse(layer_names, layer_count);
535     if (result != VK_SUCCESS)
536         return result;
537 
538     result = override_extensions_.Parse(extension_names, extension_count);
539     if (result != VK_SUCCESS)
540         return result;
541 
542     if (override_layers_.Count()) {
543         layer_names = override_layers_.Names();
544         layer_count = override_layers_.Count();
545     }
546 
547     if (!layer_count) {
548         // point head of chain to the driver
549         get_instance_proc_addr_ = driver::GetInstanceProcAddr;
550 
551         return VK_SUCCESS;
552     }
553 
554     layers_ = AllocateLayerArray(layer_count);
555     if (!layers_)
556         return VK_ERROR_OUT_OF_HOST_MEMORY;
557 
558     // load layers
559     for (uint32_t i = 0; i < layer_count; i++) {
560         result = LoadLayer(layers_[i], layer_names[i]);
561         if (result != VK_SUCCESS)
562             return result;
563 
564         // count loaded layers for proper destructions on errors
565         layer_count_++;
566     }
567 
568     SetupLayerLinks();
569 
570     return VK_SUCCESS;
571 }
572 
ActivateLayers(VkPhysicalDevice physical_dev,const char * const * layer_names,uint32_t layer_count,const char * const * extension_names,uint32_t extension_count)573 VkResult LayerChain::ActivateLayers(VkPhysicalDevice physical_dev,
574                                     const char* const* layer_names,
575                                     uint32_t layer_count,
576                                     const char* const* extension_names,
577                                     uint32_t extension_count) {
578     uint32_t instance_layer_count;
579     const ActiveLayer* instance_layers =
580         GetActiveLayers(physical_dev, instance_layer_count);
581 
582     // log a message if the application device layer array is not empty nor an
583     // exact match of the instance layer array.
584     if (layer_count) {
585         bool exact_match = (instance_layer_count == layer_count);
586         if (exact_match) {
587             for (uint32_t i = 0; i < instance_layer_count; i++) {
588                 const Layer& l = *instance_layers[i].ref;
589                 if (strcmp(GetLayerProperties(l).layerName, layer_names[i])) {
590                     exact_match = false;
591                     break;
592                 }
593             }
594         }
595 
596         if (!exact_match) {
597             logger_.Warn(physical_dev,
598                          "Device layers disagree with instance layers and are "
599                          "overridden by instance layers");
600         }
601     }
602 
603     VkResult result =
604         override_extensions_.Parse(extension_names, extension_count);
605     if (result != VK_SUCCESS)
606         return result;
607 
608     if (!instance_layer_count) {
609         // point head of chain to the driver
610         get_instance_proc_addr_ = driver::GetInstanceProcAddr;
611         get_device_proc_addr_ = driver::GetDeviceProcAddr;
612 
613         return VK_SUCCESS;
614     }
615 
616     layers_ = AllocateLayerArray(instance_layer_count);
617     if (!layers_)
618         return VK_ERROR_OUT_OF_HOST_MEMORY;
619 
620     for (uint32_t i = 0; i < instance_layer_count; i++) {
621         const Layer& l = *instance_layers[i].ref;
622 
623         // no need to and cannot chain non-global layers
624         if (!IsLayerGlobal(l))
625             continue;
626 
627         // this never fails
628         new (&layers_[layer_count_++]) ActiveLayer{GetLayerRef(l), {}};
629     }
630 
631     // this may happen when all layers are non-global ones
632     if (!layer_count_) {
633         get_instance_proc_addr_ = driver::GetInstanceProcAddr;
634         get_device_proc_addr_ = driver::GetDeviceProcAddr;
635         return VK_SUCCESS;
636     }
637 
638     SetupLayerLinks();
639 
640     return VK_SUCCESS;
641 }
642 
AllocateLayerArray(uint32_t count) const643 LayerChain::ActiveLayer* LayerChain::AllocateLayerArray(uint32_t count) const {
644     VkSystemAllocationScope scope = (is_instance_)
645                                         ? VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
646                                         : VK_SYSTEM_ALLOCATION_SCOPE_COMMAND;
647 
648     return reinterpret_cast<ActiveLayer*>(allocator_.pfnAllocation(
649         allocator_.pUserData, sizeof(ActiveLayer) * count, alignof(ActiveLayer),
650         scope));
651 }
652 
LoadLayer(ActiveLayer & layer,const char * name)653 VkResult LayerChain::LoadLayer(ActiveLayer& layer, const char* name) {
654     const Layer* l = FindLayer(name);
655     if (!l) {
656         logger_.Err(VK_NULL_HANDLE, "Failed to find layer %s", name);
657         return VK_ERROR_LAYER_NOT_PRESENT;
658     }
659 
660     new (&layer) ActiveLayer{GetLayerRef(*l), {}};
661     if (!layer.ref) {
662         ALOGW("Failed to open layer %s", name);
663         layer.ref.~LayerRef();
664         return VK_ERROR_LAYER_NOT_PRESENT;
665     }
666 
667     ALOGI("Loaded layer %s", name);
668 
669     return VK_SUCCESS;
670 }
671 
SetupLayerLinks()672 void LayerChain::SetupLayerLinks() {
673     if (is_instance_) {
674         for (uint32_t i = 0; i < layer_count_; i++) {
675             ActiveLayer& layer = layers_[i];
676 
677             // point head of chain to the first layer
678             if (i == 0)
679                 get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr();
680 
681             // point tail of chain to the driver
682             if (i == layer_count_ - 1) {
683                 layer.instance_link.pNext = nullptr;
684                 layer.instance_link.pfnNextGetInstanceProcAddr =
685                     driver::GetInstanceProcAddr;
686                 break;
687             }
688 
689             const ActiveLayer& next = layers_[i + 1];
690 
691             // const_cast as some naughty layers want to modify our links!
692             layer.instance_link.pNext =
693                 const_cast<VkLayerInstanceLink*>(&next.instance_link);
694             layer.instance_link.pfnNextGetInstanceProcAddr =
695                 next.ref.GetGetInstanceProcAddr();
696         }
697     } else {
698         for (uint32_t i = 0; i < layer_count_; i++) {
699             ActiveLayer& layer = layers_[i];
700 
701             // point head of chain to the first layer
702             if (i == 0) {
703                 get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr();
704                 get_device_proc_addr_ = layer.ref.GetGetDeviceProcAddr();
705             }
706 
707             // point tail of chain to the driver
708             if (i == layer_count_ - 1) {
709                 layer.device_link.pNext = nullptr;
710                 layer.device_link.pfnNextGetInstanceProcAddr =
711                     driver::GetInstanceProcAddr;
712                 layer.device_link.pfnNextGetDeviceProcAddr =
713                     driver::GetDeviceProcAddr;
714                 break;
715             }
716 
717             const ActiveLayer& next = layers_[i + 1];
718 
719             // const_cast as some naughty layers want to modify our links!
720             layer.device_link.pNext =
721                 const_cast<VkLayerDeviceLink*>(&next.device_link);
722             layer.device_link.pfnNextGetInstanceProcAddr =
723                 next.ref.GetGetInstanceProcAddr();
724             layer.device_link.pfnNextGetDeviceProcAddr =
725                 next.ref.GetGetDeviceProcAddr();
726         }
727     }
728 }
729 
Empty() const730 bool LayerChain::Empty() const {
731     return (!layer_count_ && !override_layers_.Count() &&
732             !override_extensions_.Count());
733 }
734 
ModifyCreateInfo(VkInstanceCreateInfo & info)735 void LayerChain::ModifyCreateInfo(VkInstanceCreateInfo& info) {
736     if (layer_count_) {
737         auto& link_info = instance_chain_info_[1];
738         link_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
739         link_info.pNext = info.pNext;
740         link_info.function = VK_LAYER_FUNCTION_LINK;
741         link_info.u.pLayerInfo = &layers_[0].instance_link;
742 
743         auto& cb_info = instance_chain_info_[0];
744         cb_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
745         cb_info.pNext = &link_info;
746         cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK;
747         cb_info.u.pfnSetInstanceLoaderData = SetInstanceLoaderData;
748 
749         info.pNext = &cb_info;
750     }
751 
752     if (override_layers_.Count()) {
753         info.enabledLayerCount = override_layers_.Count();
754         info.ppEnabledLayerNames = override_layers_.Names();
755     }
756 
757     if (override_extensions_.Count()) {
758         info.enabledExtensionCount = override_extensions_.Count();
759         info.ppEnabledExtensionNames = override_extensions_.Names();
760     }
761 }
762 
ModifyCreateInfo(VkDeviceCreateInfo & info)763 void LayerChain::ModifyCreateInfo(VkDeviceCreateInfo& info) {
764     if (layer_count_) {
765         auto& link_info = device_chain_info_[1];
766         link_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
767         link_info.pNext = info.pNext;
768         link_info.function = VK_LAYER_FUNCTION_LINK;
769         link_info.u.pLayerInfo = &layers_[0].device_link;
770 
771         auto& cb_info = device_chain_info_[0];
772         cb_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
773         cb_info.pNext = &link_info;
774         cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK;
775         cb_info.u.pfnSetDeviceLoaderData = SetDeviceLoaderData;
776 
777         info.pNext = &cb_info;
778     }
779 
780     if (override_layers_.Count()) {
781         info.enabledLayerCount = override_layers_.Count();
782         info.ppEnabledLayerNames = override_layers_.Names();
783     }
784 
785     if (override_extensions_.Count()) {
786         info.enabledExtensionCount = override_extensions_.Count();
787         info.ppEnabledExtensionNames = override_extensions_.Names();
788     }
789 }
790 
Create(const VkInstanceCreateInfo * create_info,const VkAllocationCallbacks * allocator,VkInstance * instance_out)791 VkResult LayerChain::Create(const VkInstanceCreateInfo* create_info,
792                             const VkAllocationCallbacks* allocator,
793                             VkInstance* instance_out) {
794     VkResult result = ValidateExtensions(create_info->ppEnabledExtensionNames,
795                                          create_info->enabledExtensionCount);
796     if (result != VK_SUCCESS)
797         return result;
798 
799     // call down the chain
800     PFN_vkCreateInstance create_instance =
801         reinterpret_cast<PFN_vkCreateInstance>(
802             get_instance_proc_addr_(VK_NULL_HANDLE, "vkCreateInstance"));
803     VkInstance instance;
804     result = create_instance(create_info, allocator, &instance);
805     if (result != VK_SUCCESS)
806         return result;
807 
808     // initialize InstanceData
809     InstanceData& data = GetData(instance);
810 
811     if (!InitDispatchTable(instance, get_instance_proc_addr_,
812                            enabled_extensions_)) {
813         if (data.dispatch.DestroyInstance)
814             data.dispatch.DestroyInstance(instance, allocator);
815 
816         return VK_ERROR_INITIALIZATION_FAILED;
817     }
818 
819     // install debug report callback
820     if (override_extensions_.InstallDebugCallback()) {
821         PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
822             reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
823                 get_instance_proc_addr_(instance,
824                                         "vkCreateDebugReportCallbackEXT"));
825         data.destroy_debug_callback =
826             reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
827                 get_instance_proc_addr_(instance,
828                                         "vkDestroyDebugReportCallbackEXT"));
829         if (!create_debug_report_callback || !data.destroy_debug_callback) {
830             ALOGE("Broken VK_EXT_debug_report support");
831             data.dispatch.DestroyInstance(instance, allocator);
832             return VK_ERROR_INITIALIZATION_FAILED;
833         }
834 
835         VkDebugReportCallbackCreateInfoEXT debug_callback_info = {};
836         debug_callback_info.sType =
837             VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
838         debug_callback_info.flags =
839             VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
840         debug_callback_info.pfnCallback = DebugReportCallback;
841 
842         VkDebugReportCallbackEXT debug_callback;
843         result = create_debug_report_callback(instance, &debug_callback_info,
844                                               nullptr, &debug_callback);
845         if (result != VK_SUCCESS) {
846             ALOGE("Failed to install debug report callback");
847             data.dispatch.DestroyInstance(instance, allocator);
848             return VK_ERROR_INITIALIZATION_FAILED;
849         }
850 
851         data.debug_callback = debug_callback;
852 
853         ALOGI("Installed debug report callback");
854     }
855 
856     StealLayers(data);
857 
858     *instance_out = instance;
859 
860     return VK_SUCCESS;
861 }
862 
Create(VkPhysicalDevice physical_dev,const VkDeviceCreateInfo * create_info,const VkAllocationCallbacks * allocator,VkDevice * dev_out)863 VkResult LayerChain::Create(VkPhysicalDevice physical_dev,
864                             const VkDeviceCreateInfo* create_info,
865                             const VkAllocationCallbacks* allocator,
866                             VkDevice* dev_out) {
867     VkResult result =
868         ValidateExtensions(physical_dev, create_info->ppEnabledExtensionNames,
869                            create_info->enabledExtensionCount);
870     if (result != VK_SUCCESS)
871         return result;
872 
873     // call down the chain
874     PFN_vkCreateDevice create_device =
875         GetData(physical_dev).dispatch.CreateDevice;
876     VkDevice dev;
877     result = create_device(physical_dev, create_info, allocator, &dev);
878     if (result != VK_SUCCESS)
879         return result;
880 
881     // initialize DeviceData
882     DeviceData& data = GetData(dev);
883 
884     if (!InitDispatchTable(dev, get_device_proc_addr_, enabled_extensions_)) {
885         if (data.dispatch.DestroyDevice)
886             data.dispatch.DestroyDevice(dev, allocator);
887 
888         return VK_ERROR_INITIALIZATION_FAILED;
889     }
890 
891     // no StealLayers so that active layers are destroyed with this
892     // LayerChain
893     *dev_out = dev;
894 
895     return VK_SUCCESS;
896 }
897 
ValidateExtensions(const char * const * extension_names,uint32_t extension_count)898 VkResult LayerChain::ValidateExtensions(const char* const* extension_names,
899                                         uint32_t extension_count) {
900     if (!extension_count)
901         return VK_SUCCESS;
902 
903     // query driver instance extensions
904     uint32_t count;
905     VkResult result =
906         EnumerateInstanceExtensionProperties(nullptr, &count, nullptr);
907     if (result == VK_SUCCESS && count) {
908         driver_extensions_ = AllocateDriverExtensionArray(count);
909         result = (driver_extensions_) ? EnumerateInstanceExtensionProperties(
910                                             nullptr, &count, driver_extensions_)
911                                       : VK_ERROR_OUT_OF_HOST_MEMORY;
912     }
913     if (result != VK_SUCCESS)
914         return result;
915 
916     driver_extension_count_ = count;
917 
918     for (uint32_t i = 0; i < extension_count; i++) {
919         const char* name = extension_names[i];
920         if (!IsLayerExtension(name) && !IsDriverExtension(name)) {
921             logger_.Err(VK_NULL_HANDLE,
922                         "Failed to enable missing instance extension %s", name);
923             return VK_ERROR_EXTENSION_NOT_PRESENT;
924         }
925 
926         auto ext_bit = driver::GetProcHookExtension(name);
927         if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN)
928             enabled_extensions_.set(ext_bit);
929     }
930 
931     return VK_SUCCESS;
932 }
933 
ValidateExtensions(VkPhysicalDevice physical_dev,const char * const * extension_names,uint32_t extension_count)934 VkResult LayerChain::ValidateExtensions(VkPhysicalDevice physical_dev,
935                                         const char* const* extension_names,
936                                         uint32_t extension_count) {
937     if (!extension_count)
938         return VK_SUCCESS;
939 
940     // query driver device extensions
941     uint32_t count;
942     VkResult result = EnumerateDeviceExtensionProperties(physical_dev, nullptr,
943                                                          &count, nullptr);
944     if (result == VK_SUCCESS && count) {
945         driver_extensions_ = AllocateDriverExtensionArray(count);
946         result = (driver_extensions_)
947                      ? EnumerateDeviceExtensionProperties(
948                            physical_dev, nullptr, &count, driver_extensions_)
949                      : VK_ERROR_OUT_OF_HOST_MEMORY;
950     }
951     if (result != VK_SUCCESS)
952         return result;
953 
954     driver_extension_count_ = count;
955 
956     for (uint32_t i = 0; i < extension_count; i++) {
957         const char* name = extension_names[i];
958         if (!IsLayerExtension(name) && !IsDriverExtension(name)) {
959             logger_.Err(physical_dev,
960                         "Failed to enable missing device extension %s", name);
961             return VK_ERROR_EXTENSION_NOT_PRESENT;
962         }
963 
964         auto ext_bit = driver::GetProcHookExtension(name);
965         if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN)
966             enabled_extensions_.set(ext_bit);
967     }
968 
969     return VK_SUCCESS;
970 }
971 
AllocateDriverExtensionArray(uint32_t count) const972 VkExtensionProperties* LayerChain::AllocateDriverExtensionArray(
973     uint32_t count) const {
974     return reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
975         allocator_.pUserData, sizeof(VkExtensionProperties) * count,
976         alignof(VkExtensionProperties), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
977 }
978 
IsLayerExtension(const char * name) const979 bool LayerChain::IsLayerExtension(const char* name) const {
980     if (is_instance_) {
981         for (uint32_t i = 0; i < layer_count_; i++) {
982             const ActiveLayer& layer = layers_[i];
983             if (FindLayerInstanceExtension(*layer.ref, name))
984                 return true;
985         }
986     } else {
987         for (uint32_t i = 0; i < layer_count_; i++) {
988             const ActiveLayer& layer = layers_[i];
989             if (FindLayerDeviceExtension(*layer.ref, name))
990                 return true;
991         }
992     }
993 
994     return false;
995 }
996 
IsDriverExtension(const char * name) const997 bool LayerChain::IsDriverExtension(const char* name) const {
998     for (uint32_t i = 0; i < driver_extension_count_; i++) {
999         if (strcmp(driver_extensions_[i].extensionName, name) == 0)
1000             return true;
1001     }
1002 
1003     return false;
1004 }
1005 
1006 template <typename DataType>
StealLayers(DataType & data)1007 void LayerChain::StealLayers(DataType& data) {
1008     data.layers = layers_;
1009     data.layer_count = layer_count_;
1010 
1011     layers_ = nullptr;
1012     layer_count_ = 0;
1013 }
1014 
DestroyLayers(ActiveLayer * layers,uint32_t count,const VkAllocationCallbacks & allocator)1015 void LayerChain::DestroyLayers(ActiveLayer* layers,
1016                                uint32_t count,
1017                                const VkAllocationCallbacks& allocator) {
1018     for (uint32_t i = 0; i < count; i++)
1019         layers[i].ref.~LayerRef();
1020 
1021     allocator.pfnFree(allocator.pUserData, layers);
1022 }
1023 
SetInstanceLoaderData(VkInstance instance,void * object)1024 VkResult LayerChain::SetInstanceLoaderData(VkInstance instance, void* object) {
1025     driver::InstanceDispatchable dispatchable =
1026         reinterpret_cast<driver::InstanceDispatchable>(object);
1027 
1028     return (driver::SetDataInternal(dispatchable, &driver::GetData(instance)))
1029                ? VK_SUCCESS
1030                : VK_ERROR_INITIALIZATION_FAILED;
1031 }
1032 
SetDeviceLoaderData(VkDevice device,void * object)1033 VkResult LayerChain::SetDeviceLoaderData(VkDevice device, void* object) {
1034     driver::DeviceDispatchable dispatchable =
1035         reinterpret_cast<driver::DeviceDispatchable>(object);
1036 
1037     return (driver::SetDataInternal(dispatchable, &driver::GetData(device)))
1038                ? VK_SUCCESS
1039                : VK_ERROR_INITIALIZATION_FAILED;
1040 }
1041 
DebugReportCallback(VkDebugReportFlagsEXT flags,VkDebugReportObjectTypeEXT obj_type,uint64_t obj,size_t location,int32_t msg_code,const char * layer_prefix,const char * msg,void * user_data)1042 VkBool32 LayerChain::DebugReportCallback(VkDebugReportFlagsEXT flags,
1043                                          VkDebugReportObjectTypeEXT obj_type,
1044                                          uint64_t obj,
1045                                          size_t location,
1046                                          int32_t msg_code,
1047                                          const char* layer_prefix,
1048                                          const char* msg,
1049                                          void* user_data) {
1050     int prio;
1051 
1052     if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT)
1053         prio = ANDROID_LOG_ERROR;
1054     else if (flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT |
1055                       VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT))
1056         prio = ANDROID_LOG_WARN;
1057     else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)
1058         prio = ANDROID_LOG_INFO;
1059     else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT)
1060         prio = ANDROID_LOG_DEBUG;
1061     else
1062         prio = ANDROID_LOG_UNKNOWN;
1063 
1064     LOG_PRI(prio, LOG_TAG, "[%s] Code %d : %s", layer_prefix, msg_code, msg);
1065 
1066     (void)obj_type;
1067     (void)obj;
1068     (void)location;
1069     (void)user_data;
1070 
1071     return false;
1072 }
1073 
CreateInstance(const VkInstanceCreateInfo * create_info,const VkAllocationCallbacks * allocator,VkInstance * instance_out)1074 VkResult LayerChain::CreateInstance(const VkInstanceCreateInfo* create_info,
1075                                     const VkAllocationCallbacks* allocator,
1076                                     VkInstance* instance_out) {
1077     const driver::DebugReportLogger logger(*create_info);
1078     LayerChain chain(true, logger,
1079                      (allocator) ? *allocator : driver::GetDefaultAllocator());
1080 
1081     VkResult result = chain.ActivateLayers(create_info->ppEnabledLayerNames,
1082                                            create_info->enabledLayerCount,
1083                                            create_info->ppEnabledExtensionNames,
1084                                            create_info->enabledExtensionCount);
1085     if (result != VK_SUCCESS)
1086         return result;
1087 
1088     // use a local create info when the chain is not empty
1089     VkInstanceCreateInfo local_create_info;
1090     if (!chain.Empty()) {
1091         local_create_info = *create_info;
1092         chain.ModifyCreateInfo(local_create_info);
1093         create_info = &local_create_info;
1094     }
1095 
1096     return chain.Create(create_info, allocator, instance_out);
1097 }
1098 
CreateDevice(VkPhysicalDevice physical_dev,const VkDeviceCreateInfo * create_info,const VkAllocationCallbacks * allocator,VkDevice * dev_out)1099 VkResult LayerChain::CreateDevice(VkPhysicalDevice physical_dev,
1100                                   const VkDeviceCreateInfo* create_info,
1101                                   const VkAllocationCallbacks* allocator,
1102                                   VkDevice* dev_out) {
1103     const driver::DebugReportLogger logger = driver::Logger(physical_dev);
1104     LayerChain chain(
1105         false, logger,
1106         (allocator) ? *allocator : driver::GetData(physical_dev).allocator);
1107 
1108     VkResult result = chain.ActivateLayers(
1109         physical_dev, create_info->ppEnabledLayerNames,
1110         create_info->enabledLayerCount, create_info->ppEnabledExtensionNames,
1111         create_info->enabledExtensionCount);
1112     if (result != VK_SUCCESS)
1113         return result;
1114 
1115     // use a local create info when the chain is not empty
1116     VkDeviceCreateInfo local_create_info;
1117     if (!chain.Empty()) {
1118         local_create_info = *create_info;
1119         chain.ModifyCreateInfo(local_create_info);
1120         create_info = &local_create_info;
1121     }
1122 
1123     return chain.Create(physical_dev, create_info, allocator, dev_out);
1124 }
1125 
DestroyInstance(VkInstance instance,const VkAllocationCallbacks * allocator)1126 void LayerChain::DestroyInstance(VkInstance instance,
1127                                  const VkAllocationCallbacks* allocator) {
1128     InstanceData& data = GetData(instance);
1129 
1130     if (data.debug_callback != VK_NULL_HANDLE)
1131         data.destroy_debug_callback(instance, data.debug_callback, allocator);
1132 
1133     ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers);
1134     uint32_t layer_count = data.layer_count;
1135 
1136     VkAllocationCallbacks local_allocator;
1137     if (!allocator)
1138         local_allocator = driver::GetData(instance).allocator;
1139 
1140     // this also destroys InstanceData
1141     data.dispatch.DestroyInstance(instance, allocator);
1142 
1143     DestroyLayers(layers, layer_count,
1144                   (allocator) ? *allocator : local_allocator);
1145 }
1146 
DestroyDevice(VkDevice device,const VkAllocationCallbacks * allocator)1147 void LayerChain::DestroyDevice(VkDevice device,
1148                                const VkAllocationCallbacks* allocator) {
1149     DeviceData& data = GetData(device);
1150     // this also destroys DeviceData
1151     data.dispatch.DestroyDevice(device, allocator);
1152 }
1153 
GetActiveLayers(VkPhysicalDevice physical_dev,uint32_t & count)1154 const LayerChain::ActiveLayer* LayerChain::GetActiveLayers(
1155     VkPhysicalDevice physical_dev,
1156     uint32_t& count) {
1157     count = GetData(physical_dev).layer_count;
1158     return reinterpret_cast<const ActiveLayer*>(GetData(physical_dev).layers);
1159 }
1160 
1161 // ----------------------------------------------------------------------------
1162 
EnsureInitialized()1163 bool EnsureInitialized() {
1164     static std::once_flag once_flag;
1165     static bool initialized;
1166 
1167     std::call_once(once_flag, []() {
1168         if (driver::OpenHAL()) {
1169             DiscoverLayers();
1170             initialized = true;
1171         }
1172     });
1173 
1174     return initialized;
1175 }
1176 
1177 }  // anonymous namespace
1178 
CreateInstance(const VkInstanceCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkInstance * pInstance)1179 VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1180                         const VkAllocationCallbacks* pAllocator,
1181                         VkInstance* pInstance) {
1182     ATRACE_CALL();
1183 
1184     if (!EnsureInitialized())
1185         return VK_ERROR_INITIALIZATION_FAILED;
1186 
1187     return LayerChain::CreateInstance(pCreateInfo, pAllocator, pInstance);
1188 }
1189 
DestroyInstance(VkInstance instance,const VkAllocationCallbacks * pAllocator)1190 void DestroyInstance(VkInstance instance,
1191                      const VkAllocationCallbacks* pAllocator) {
1192     ATRACE_CALL();
1193 
1194     if (instance != VK_NULL_HANDLE)
1195         LayerChain::DestroyInstance(instance, pAllocator);
1196 }
1197 
CreateDevice(VkPhysicalDevice physicalDevice,const VkDeviceCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkDevice * pDevice)1198 VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1199                       const VkDeviceCreateInfo* pCreateInfo,
1200                       const VkAllocationCallbacks* pAllocator,
1201                       VkDevice* pDevice) {
1202     ATRACE_CALL();
1203 
1204     return LayerChain::CreateDevice(physicalDevice, pCreateInfo, pAllocator,
1205                                     pDevice);
1206 }
1207 
DestroyDevice(VkDevice device,const VkAllocationCallbacks * pAllocator)1208 void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1209     ATRACE_CALL();
1210 
1211     if (device != VK_NULL_HANDLE)
1212         LayerChain::DestroyDevice(device, pAllocator);
1213 }
1214 
EnumerateInstanceLayerProperties(uint32_t * pPropertyCount,VkLayerProperties * pProperties)1215 VkResult EnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
1216                                           VkLayerProperties* pProperties) {
1217     ATRACE_CALL();
1218 
1219     if (!EnsureInitialized())
1220         return VK_ERROR_INITIALIZATION_FAILED;
1221 
1222     uint32_t count = GetLayerCount();
1223 
1224     if (!pProperties) {
1225         *pPropertyCount = count;
1226         return VK_SUCCESS;
1227     }
1228 
1229     uint32_t copied = std::min(*pPropertyCount, count);
1230     for (uint32_t i = 0; i < copied; i++)
1231         pProperties[i] = GetLayerProperties(GetLayer(i));
1232     *pPropertyCount = copied;
1233 
1234     return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE;
1235 }
1236 
EnumerateInstanceExtensionProperties(const char * pLayerName,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)1237 VkResult EnumerateInstanceExtensionProperties(
1238     const char* pLayerName,
1239     uint32_t* pPropertyCount,
1240     VkExtensionProperties* pProperties) {
1241     ATRACE_CALL();
1242 
1243     if (!EnsureInitialized())
1244         return VK_ERROR_INITIALIZATION_FAILED;
1245 
1246     if (pLayerName) {
1247         const Layer* layer = FindLayer(pLayerName);
1248         if (!layer)
1249             return VK_ERROR_LAYER_NOT_PRESENT;
1250 
1251         uint32_t count;
1252         const VkExtensionProperties* props =
1253             GetLayerInstanceExtensions(*layer, count);
1254 
1255         if (!pProperties || *pPropertyCount > count)
1256             *pPropertyCount = count;
1257         if (pProperties)
1258             std::copy(props, props + *pPropertyCount, pProperties);
1259 
1260         return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1261     }
1262 
1263     // TODO how about extensions from implicitly enabled layers?
1264     return vulkan::driver::EnumerateInstanceExtensionProperties(
1265         nullptr, pPropertyCount, pProperties);
1266 }
1267 
EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,uint32_t * pPropertyCount,VkLayerProperties * pProperties)1268 VkResult EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
1269                                         uint32_t* pPropertyCount,
1270                                         VkLayerProperties* pProperties) {
1271     ATRACE_CALL();
1272 
1273     uint32_t count;
1274     const LayerChain::ActiveLayer* layers =
1275         LayerChain::GetActiveLayers(physicalDevice, count);
1276 
1277     if (!pProperties) {
1278         *pPropertyCount = count;
1279         return VK_SUCCESS;
1280     }
1281 
1282     uint32_t copied = std::min(*pPropertyCount, count);
1283     for (uint32_t i = 0; i < copied; i++)
1284         pProperties[i] = GetLayerProperties(*layers[i].ref);
1285     *pPropertyCount = copied;
1286 
1287     return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE;
1288 }
1289 
EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,const char * pLayerName,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)1290 VkResult EnumerateDeviceExtensionProperties(
1291     VkPhysicalDevice physicalDevice,
1292     const char* pLayerName,
1293     uint32_t* pPropertyCount,
1294     VkExtensionProperties* pProperties) {
1295     ATRACE_CALL();
1296 
1297     if (pLayerName) {
1298         // EnumerateDeviceLayerProperties enumerates active layers for
1299         // backward compatibility.  The extension query here should work for
1300         // all layers.
1301         const Layer* layer = FindLayer(pLayerName);
1302         if (!layer)
1303             return VK_ERROR_LAYER_NOT_PRESENT;
1304 
1305         uint32_t count;
1306         const VkExtensionProperties* props =
1307             GetLayerDeviceExtensions(*layer, count);
1308 
1309         if (!pProperties || *pPropertyCount > count)
1310             *pPropertyCount = count;
1311         if (pProperties)
1312             std::copy(props, props + *pPropertyCount, pProperties);
1313 
1314         return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1315     }
1316 
1317     // TODO how about extensions from implicitly enabled layers?
1318     const InstanceData& data = GetData(physicalDevice);
1319     return data.dispatch.EnumerateDeviceExtensionProperties(
1320         physicalDevice, nullptr, pPropertyCount, pProperties);
1321 }
1322 
EnumerateInstanceVersion(uint32_t * pApiVersion)1323 VkResult EnumerateInstanceVersion(uint32_t* pApiVersion) {
1324     ATRACE_CALL();
1325 
1326     *pApiVersion = VK_API_VERSION_1_1;
1327     return VK_SUCCESS;
1328 }
1329 
1330 }  // namespace api
1331 }  // namespace vulkan
1332