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