api.cpp revision c3a28913b6a95d2faee0db537c48557e04267511
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 (!is_instance_ || !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    struct ActiveLayer {
371        LayerRef ref;
372        union {
373            VkLayerInstanceLink instance_link;
374            VkLayerDeviceLink device_link;
375        };
376    };
377
378    static VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
379                                   const VkAllocationCallbacks* allocator,
380                                   VkInstance* instance_out);
381
382    static VkResult CreateDevice(VkPhysicalDevice physical_dev,
383                                 const VkDeviceCreateInfo* create_info,
384                                 const VkAllocationCallbacks* allocator,
385                                 VkDevice* dev_out);
386
387    static void DestroyInstance(VkInstance instance,
388                                const VkAllocationCallbacks* allocator);
389
390    static void DestroyDevice(VkDevice dev,
391                              const VkAllocationCallbacks* allocator);
392
393    static const ActiveLayer* GetActiveLayers(VkPhysicalDevice physical_dev,
394                                              uint32_t& count);
395
396   private:
397    LayerChain(bool is_instance, const VkAllocationCallbacks& allocator);
398    ~LayerChain();
399
400    VkResult ActivateLayers(const char* const* layer_names,
401                            uint32_t layer_count,
402                            const char* const* extension_names,
403                            uint32_t extension_count);
404    VkResult ActivateLayers(VkPhysicalDevice physical_dev,
405                            const char* const* layer_names,
406                            uint32_t layer_count,
407                            const char* const* extension_names,
408                            uint32_t extension_count);
409    ActiveLayer* AllocateLayerArray(uint32_t count) const;
410    VkResult LoadLayer(ActiveLayer& layer, const char* name);
411    void SetupLayerLinks();
412
413    bool Empty() const;
414    void ModifyCreateInfo(VkInstanceCreateInfo& info);
415    void ModifyCreateInfo(VkDeviceCreateInfo& info);
416
417    VkResult Create(const VkInstanceCreateInfo* create_info,
418                    const VkAllocationCallbacks* allocator,
419                    VkInstance* instance_out);
420
421    VkResult Create(VkPhysicalDevice physical_dev,
422                    const VkDeviceCreateInfo* create_info,
423                    const VkAllocationCallbacks* allocator,
424                    VkDevice* dev_out);
425
426    VkResult ValidateExtensions(const char* const* extension_names,
427                                uint32_t extension_count);
428    VkResult ValidateExtensions(VkPhysicalDevice physical_dev,
429                                const char* const* extension_names,
430                                uint32_t extension_count);
431    VkExtensionProperties* AllocateDriverExtensionArray(uint32_t count) const;
432    bool IsLayerExtension(const char* name) const;
433    bool IsDriverExtension(const char* name) const;
434
435    template <typename DataType>
436    void StealLayers(DataType& data);
437
438    static void DestroyLayers(ActiveLayer* layers,
439                              uint32_t count,
440                              const VkAllocationCallbacks& allocator);
441
442    static VKAPI_ATTR VkResult SetInstanceLoaderData(VkInstance instance,
443                                                     void* object);
444    static VKAPI_ATTR VkResult SetDeviceLoaderData(VkDevice device,
445                                                   void* object);
446
447    static VKAPI_ATTR VkBool32
448    DebugReportCallback(VkDebugReportFlagsEXT flags,
449                        VkDebugReportObjectTypeEXT obj_type,
450                        uint64_t obj,
451                        size_t location,
452                        int32_t msg_code,
453                        const char* layer_prefix,
454                        const char* msg,
455                        void* user_data);
456
457    const bool is_instance_;
458    const VkAllocationCallbacks& allocator_;
459
460    OverrideLayerNames override_layers_;
461    OverrideExtensionNames override_extensions_;
462
463    ActiveLayer* layers_;
464    uint32_t layer_count_;
465
466    PFN_vkGetInstanceProcAddr get_instance_proc_addr_;
467    PFN_vkGetDeviceProcAddr get_device_proc_addr_;
468
469    union {
470        VkLayerInstanceCreateInfo instance_chain_info_[2];
471        VkLayerDeviceCreateInfo device_chain_info_[2];
472    };
473
474    VkExtensionProperties* driver_extensions_;
475    uint32_t driver_extension_count_;
476    std::bitset<driver::ProcHook::EXTENSION_COUNT> enabled_extensions_;
477};
478
479LayerChain::LayerChain(bool is_instance, const VkAllocationCallbacks& allocator)
480    : is_instance_(is_instance),
481      allocator_(allocator),
482      override_layers_(is_instance, allocator),
483      override_extensions_(is_instance, allocator),
484      layers_(nullptr),
485      layer_count_(0),
486      get_instance_proc_addr_(nullptr),
487      get_device_proc_addr_(nullptr),
488      driver_extensions_(nullptr),
489      driver_extension_count_(0) {
490    enabled_extensions_.set(driver::ProcHook::EXTENSION_CORE);
491}
492
493LayerChain::~LayerChain() {
494    allocator_.pfnFree(allocator_.pUserData, driver_extensions_);
495    DestroyLayers(layers_, layer_count_, allocator_);
496}
497
498VkResult LayerChain::ActivateLayers(const char* const* layer_names,
499                                    uint32_t layer_count,
500                                    const char* const* extension_names,
501                                    uint32_t extension_count) {
502    VkResult result = override_layers_.Parse(layer_names, layer_count);
503    if (result != VK_SUCCESS)
504        return result;
505
506    result = override_extensions_.Parse(extension_names, extension_count);
507    if (result != VK_SUCCESS)
508        return result;
509
510    if (override_layers_.Count()) {
511        layer_names = override_layers_.Names();
512        layer_count = override_layers_.Count();
513    }
514
515    if (!layer_count) {
516        // point head of chain to the driver
517        get_instance_proc_addr_ = driver::GetInstanceProcAddr;
518
519        return VK_SUCCESS;
520    }
521
522    layers_ = AllocateLayerArray(layer_count);
523    if (!layers_)
524        return VK_ERROR_OUT_OF_HOST_MEMORY;
525
526    // load layers
527    for (uint32_t i = 0; i < layer_count; i++) {
528        result = LoadLayer(layers_[i], layer_names[i]);
529        if (result != VK_SUCCESS)
530            return result;
531
532        // count loaded layers for proper destructions on errors
533        layer_count_++;
534    }
535
536    SetupLayerLinks();
537
538    return VK_SUCCESS;
539}
540
541VkResult LayerChain::ActivateLayers(VkPhysicalDevice physical_dev,
542                                    const char* const* layer_names,
543                                    uint32_t layer_count,
544                                    const char* const* extension_names,
545                                    uint32_t extension_count) {
546    uint32_t instance_layer_count;
547    const ActiveLayer* instance_layers =
548        GetActiveLayers(physical_dev, instance_layer_count);
549
550    // log a message if the application device layer array is not empty nor an
551    // exact match of the instance layer array.
552    if (layer_count) {
553        bool exact_match = (instance_layer_count == layer_count);
554        if (exact_match) {
555            for (uint32_t i = 0; i < instance_layer_count; i++) {
556                const Layer& l = *instance_layers[i].ref;
557                if (strcmp(GetLayerProperties(l).layerName, layer_names[i])) {
558                    exact_match = false;
559                    break;
560                }
561            }
562        }
563
564        if (!exact_match) {
565            ALOGW("Device layers");
566            for (uint32_t i = 0; i < layer_count; i++)
567                ALOGW("  %s", layer_names[i]);
568            ALOGW(
569                "disagree with instance layers and are overridden by "
570                "instance layers");
571        }
572    }
573
574    VkResult result =
575        override_extensions_.Parse(extension_names, extension_count);
576    if (result != VK_SUCCESS)
577        return result;
578
579    if (!instance_layer_count) {
580        // point head of chain to the driver
581        get_instance_proc_addr_ = driver::GetInstanceProcAddr;
582        get_device_proc_addr_ = driver::GetDeviceProcAddr;
583
584        return VK_SUCCESS;
585    }
586
587    layers_ = AllocateLayerArray(instance_layer_count);
588    if (!layers_)
589        return VK_ERROR_OUT_OF_HOST_MEMORY;
590
591    for (uint32_t i = 0; i < instance_layer_count; i++) {
592        const Layer& l = *instance_layers[i].ref;
593
594        // no need to and cannot chain non-global layers
595        if (!IsLayerGlobal(l))
596            continue;
597
598        // this never fails
599        new (&layers_[layer_count_++]) ActiveLayer{GetLayerRef(l), {}};
600    }
601
602    SetupLayerLinks();
603
604    return VK_SUCCESS;
605}
606
607LayerChain::ActiveLayer* LayerChain::AllocateLayerArray(uint32_t count) const {
608    VkSystemAllocationScope scope = (is_instance_)
609                                        ? VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
610                                        : VK_SYSTEM_ALLOCATION_SCOPE_COMMAND;
611
612    return reinterpret_cast<ActiveLayer*>(allocator_.pfnAllocation(
613        allocator_.pUserData, sizeof(ActiveLayer) * count, alignof(ActiveLayer),
614        scope));
615}
616
617VkResult LayerChain::LoadLayer(ActiveLayer& layer, const char* name) {
618    const Layer* l = FindLayer(name);
619    if (!l) {
620        ALOGW("Failed to find layer %s", name);
621        return VK_ERROR_LAYER_NOT_PRESENT;
622    }
623
624    new (&layer) ActiveLayer{GetLayerRef(*l), {}};
625    if (!layer.ref) {
626        ALOGW("Failed to open layer %s", name);
627        layer.ref.~LayerRef();
628        return VK_ERROR_LAYER_NOT_PRESENT;
629    }
630
631    ALOGI("Loaded layer %s", name);
632
633    return VK_SUCCESS;
634}
635
636void LayerChain::SetupLayerLinks() {
637    if (is_instance_) {
638        for (uint32_t i = 0; i < layer_count_; i++) {
639            ActiveLayer& layer = layers_[i];
640
641            // point head of chain to the first layer
642            if (i == 0)
643                get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr();
644
645            // point tail of chain to the driver
646            if (i == layer_count_ - 1) {
647                layer.instance_link.pNext = nullptr;
648                layer.instance_link.pfnNextGetInstanceProcAddr =
649                    driver::GetInstanceProcAddr;
650                break;
651            }
652
653            const ActiveLayer& next = layers_[i + 1];
654
655            // const_cast as some naughty layers want to modify our links!
656            layer.instance_link.pNext =
657                const_cast<VkLayerInstanceLink*>(&next.instance_link);
658            layer.instance_link.pfnNextGetInstanceProcAddr =
659                next.ref.GetGetInstanceProcAddr();
660        }
661    } else {
662        for (uint32_t i = 0; i < layer_count_; i++) {
663            ActiveLayer& layer = layers_[i];
664
665            // point head of chain to the first layer
666            if (i == 0) {
667                get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr();
668                get_device_proc_addr_ = layer.ref.GetGetDeviceProcAddr();
669            }
670
671            // point tail of chain to the driver
672            if (i == layer_count_ - 1) {
673                layer.device_link.pNext = nullptr;
674                layer.device_link.pfnNextGetInstanceProcAddr =
675                    driver::GetInstanceProcAddr;
676                layer.device_link.pfnNextGetDeviceProcAddr =
677                    driver::GetDeviceProcAddr;
678                break;
679            }
680
681            const ActiveLayer& next = layers_[i + 1];
682
683            // const_cast as some naughty layers want to modify our links!
684            layer.device_link.pNext =
685                const_cast<VkLayerDeviceLink*>(&next.device_link);
686            layer.device_link.pfnNextGetInstanceProcAddr =
687                next.ref.GetGetInstanceProcAddr();
688            layer.device_link.pfnNextGetDeviceProcAddr =
689                next.ref.GetGetDeviceProcAddr();
690        }
691    }
692}
693
694bool LayerChain::Empty() const {
695    return (!layer_count_ && !override_layers_.Count() &&
696            !override_extensions_.Count());
697}
698
699void LayerChain::ModifyCreateInfo(VkInstanceCreateInfo& info) {
700    if (layer_count_) {
701        auto& link_info = instance_chain_info_[1];
702        link_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
703        link_info.pNext = info.pNext;
704        link_info.function = VK_LAYER_FUNCTION_LINK;
705        link_info.u.pLayerInfo = &layers_[0].instance_link;
706
707        auto& cb_info = instance_chain_info_[0];
708        cb_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO;
709        cb_info.pNext = &link_info;
710        cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK;
711        cb_info.u.pfnSetInstanceLoaderData = SetInstanceLoaderData;
712
713        info.pNext = &cb_info;
714    }
715
716    if (override_layers_.Count()) {
717        info.enabledLayerCount = override_layers_.Count();
718        info.ppEnabledLayerNames = override_layers_.Names();
719    }
720
721    if (override_extensions_.Count()) {
722        info.enabledExtensionCount = override_extensions_.Count();
723        info.ppEnabledExtensionNames = override_extensions_.Names();
724    }
725}
726
727void LayerChain::ModifyCreateInfo(VkDeviceCreateInfo& info) {
728    if (layer_count_) {
729        auto& link_info = device_chain_info_[1];
730        link_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
731        link_info.pNext = info.pNext;
732        link_info.function = VK_LAYER_FUNCTION_LINK;
733        link_info.u.pLayerInfo = &layers_[0].device_link;
734
735        auto& cb_info = device_chain_info_[0];
736        cb_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO;
737        cb_info.pNext = &link_info;
738        cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK;
739        cb_info.u.pfnSetDeviceLoaderData = SetDeviceLoaderData;
740
741        info.pNext = &cb_info;
742    }
743
744    if (override_layers_.Count()) {
745        info.enabledLayerCount = override_layers_.Count();
746        info.ppEnabledLayerNames = override_layers_.Names();
747    }
748
749    if (override_extensions_.Count()) {
750        info.enabledExtensionCount = override_extensions_.Count();
751        info.ppEnabledExtensionNames = override_extensions_.Names();
752    }
753}
754
755VkResult LayerChain::Create(const VkInstanceCreateInfo* create_info,
756                            const VkAllocationCallbacks* allocator,
757                            VkInstance* instance_out) {
758    VkResult result = ValidateExtensions(create_info->ppEnabledExtensionNames,
759                                         create_info->enabledExtensionCount);
760    if (result != VK_SUCCESS)
761        return result;
762
763    // call down the chain
764    PFN_vkCreateInstance create_instance =
765        reinterpret_cast<PFN_vkCreateInstance>(
766            get_instance_proc_addr_(VK_NULL_HANDLE, "vkCreateInstance"));
767    VkInstance instance;
768    result = create_instance(create_info, allocator, &instance);
769    if (result != VK_SUCCESS)
770        return result;
771
772    // initialize InstanceData
773    InstanceData& data = GetData(instance);
774
775    if (!InitDispatchTable(instance, get_instance_proc_addr_,
776                           enabled_extensions_)) {
777        if (data.dispatch.DestroyInstance)
778            data.dispatch.DestroyInstance(instance, allocator);
779
780        return VK_ERROR_INITIALIZATION_FAILED;
781    }
782
783    // install debug report callback
784    if (override_extensions_.InstallDebugCallback()) {
785        PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback =
786            reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
787                get_instance_proc_addr_(instance,
788                                        "vkCreateDebugReportCallbackEXT"));
789        data.destroy_debug_callback =
790            reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
791                get_instance_proc_addr_(instance,
792                                        "vkDestroyDebugReportCallbackEXT"));
793        if (!create_debug_report_callback || !data.destroy_debug_callback) {
794            ALOGE("Broken VK_EXT_debug_report support");
795            data.dispatch.DestroyInstance(instance, allocator);
796            return VK_ERROR_INITIALIZATION_FAILED;
797        }
798
799        VkDebugReportCallbackCreateInfoEXT debug_callback_info = {};
800        debug_callback_info.sType =
801            VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
802        debug_callback_info.flags =
803            VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT;
804        debug_callback_info.pfnCallback = DebugReportCallback;
805
806        VkDebugReportCallbackEXT debug_callback;
807        result = create_debug_report_callback(instance, &debug_callback_info,
808                                              nullptr, &debug_callback);
809        if (result != VK_SUCCESS) {
810            ALOGE("Failed to install debug report callback");
811            data.dispatch.DestroyInstance(instance, allocator);
812            return VK_ERROR_INITIALIZATION_FAILED;
813        }
814
815        data.debug_callback = debug_callback;
816
817        ALOGI("Installed debug report callback");
818    }
819
820    StealLayers(data);
821
822    *instance_out = instance;
823
824    return VK_SUCCESS;
825}
826
827VkResult LayerChain::Create(VkPhysicalDevice physical_dev,
828                            const VkDeviceCreateInfo* create_info,
829                            const VkAllocationCallbacks* allocator,
830                            VkDevice* dev_out) {
831    VkResult result =
832        ValidateExtensions(physical_dev, create_info->ppEnabledExtensionNames,
833                           create_info->enabledExtensionCount);
834    if (result != VK_SUCCESS)
835        return result;
836
837    // call down the chain
838    PFN_vkCreateDevice create_device =
839        GetData(physical_dev).dispatch.CreateDevice;
840    VkDevice dev;
841    result = create_device(physical_dev, create_info, allocator, &dev);
842    if (result != VK_SUCCESS)
843        return result;
844
845    // initialize DeviceData
846    DeviceData& data = GetData(dev);
847
848    if (!InitDispatchTable(dev, get_device_proc_addr_, enabled_extensions_)) {
849        if (data.dispatch.DestroyDevice)
850            data.dispatch.DestroyDevice(dev, allocator);
851
852        return VK_ERROR_INITIALIZATION_FAILED;
853    }
854
855    // no StealLayers so that active layers are destroyed with this
856    // LayerChain
857    *dev_out = dev;
858
859    return VK_SUCCESS;
860}
861
862VkResult LayerChain::ValidateExtensions(const char* const* extension_names,
863                                        uint32_t extension_count) {
864    if (!extension_count)
865        return VK_SUCCESS;
866
867    // query driver instance extensions
868    uint32_t count;
869    VkResult result =
870        EnumerateInstanceExtensionProperties(nullptr, &count, nullptr);
871    if (result == VK_SUCCESS && count) {
872        driver_extensions_ = AllocateDriverExtensionArray(count);
873        result = (driver_extensions_) ? EnumerateInstanceExtensionProperties(
874                                            nullptr, &count, driver_extensions_)
875                                      : VK_ERROR_OUT_OF_HOST_MEMORY;
876    }
877    if (result != VK_SUCCESS)
878        return result;
879
880    driver_extension_count_ = count;
881
882    for (uint32_t i = 0; i < extension_count; i++) {
883        const char* name = extension_names[i];
884        if (!IsLayerExtension(name) && !IsDriverExtension(name)) {
885            ALOGE("Failed to enable missing instance extension %s", name);
886            return VK_ERROR_EXTENSION_NOT_PRESENT;
887        }
888
889        auto ext_bit = driver::GetProcHookExtension(name);
890        if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN)
891            enabled_extensions_.set(ext_bit);
892    }
893
894    return VK_SUCCESS;
895}
896
897VkResult LayerChain::ValidateExtensions(VkPhysicalDevice physical_dev,
898                                        const char* const* extension_names,
899                                        uint32_t extension_count) {
900    if (!extension_count)
901        return VK_SUCCESS;
902
903    // query driver device extensions
904    uint32_t count;
905    VkResult result = EnumerateDeviceExtensionProperties(physical_dev, nullptr,
906                                                         &count, nullptr);
907    if (result == VK_SUCCESS && count) {
908        driver_extensions_ = AllocateDriverExtensionArray(count);
909        result = (driver_extensions_)
910                     ? EnumerateDeviceExtensionProperties(
911                           physical_dev, nullptr, &count, driver_extensions_)
912                     : VK_ERROR_OUT_OF_HOST_MEMORY;
913    }
914    if (result != VK_SUCCESS)
915        return result;
916
917    driver_extension_count_ = count;
918
919    for (uint32_t i = 0; i < extension_count; i++) {
920        const char* name = extension_names[i];
921        if (!IsLayerExtension(name) && !IsDriverExtension(name)) {
922            ALOGE("Failed to enable missing device 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
934VkExtensionProperties* LayerChain::AllocateDriverExtensionArray(
935    uint32_t count) const {
936    return reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
937        allocator_.pUserData, sizeof(VkExtensionProperties) * count,
938        alignof(VkExtensionProperties), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
939}
940
941bool LayerChain::IsLayerExtension(const char* name) const {
942    if (is_instance_) {
943        for (uint32_t i = 0; i < layer_count_; i++) {
944            const ActiveLayer& layer = layers_[i];
945            if (FindLayerInstanceExtension(*layer.ref, name))
946                return true;
947        }
948    } else {
949        for (uint32_t i = 0; i < layer_count_; i++) {
950            const ActiveLayer& layer = layers_[i];
951            if (FindLayerDeviceExtension(*layer.ref, name))
952                return true;
953        }
954    }
955
956    return false;
957}
958
959bool LayerChain::IsDriverExtension(const char* name) const {
960    for (uint32_t i = 0; i < driver_extension_count_; i++) {
961        if (strcmp(driver_extensions_[i].extensionName, name) == 0)
962            return true;
963    }
964
965    return false;
966}
967
968template <typename DataType>
969void LayerChain::StealLayers(DataType& data) {
970    data.layers = layers_;
971    data.layer_count = layer_count_;
972
973    layers_ = nullptr;
974    layer_count_ = 0;
975}
976
977void LayerChain::DestroyLayers(ActiveLayer* layers,
978                               uint32_t count,
979                               const VkAllocationCallbacks& allocator) {
980    for (uint32_t i = 0; i < count; i++)
981        layers[i].ref.~LayerRef();
982
983    allocator.pfnFree(allocator.pUserData, layers);
984}
985
986VkResult LayerChain::SetInstanceLoaderData(VkInstance instance, void* object) {
987    driver::InstanceDispatchable dispatchable =
988        reinterpret_cast<driver::InstanceDispatchable>(object);
989
990    return (driver::SetDataInternal(dispatchable, &driver::GetData(instance)))
991               ? VK_SUCCESS
992               : VK_ERROR_INITIALIZATION_FAILED;
993}
994
995VkResult LayerChain::SetDeviceLoaderData(VkDevice device, void* object) {
996    driver::DeviceDispatchable dispatchable =
997        reinterpret_cast<driver::DeviceDispatchable>(object);
998
999    return (driver::SetDataInternal(dispatchable, &driver::GetData(device)))
1000               ? VK_SUCCESS
1001               : VK_ERROR_INITIALIZATION_FAILED;
1002}
1003
1004VkBool32 LayerChain::DebugReportCallback(VkDebugReportFlagsEXT flags,
1005                                         VkDebugReportObjectTypeEXT obj_type,
1006                                         uint64_t obj,
1007                                         size_t location,
1008                                         int32_t msg_code,
1009                                         const char* layer_prefix,
1010                                         const char* msg,
1011                                         void* user_data) {
1012    int prio;
1013
1014    if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT)
1015        prio = ANDROID_LOG_ERROR;
1016    else if (flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT |
1017                      VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT))
1018        prio = ANDROID_LOG_WARN;
1019    else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)
1020        prio = ANDROID_LOG_INFO;
1021    else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT)
1022        prio = ANDROID_LOG_DEBUG;
1023    else
1024        prio = ANDROID_LOG_UNKNOWN;
1025
1026    LOG_PRI(prio, LOG_TAG, "[%s] Code %d : %s", layer_prefix, msg_code, msg);
1027
1028    (void)obj_type;
1029    (void)obj;
1030    (void)location;
1031    (void)user_data;
1032
1033    return false;
1034}
1035
1036VkResult LayerChain::CreateInstance(const VkInstanceCreateInfo* create_info,
1037                                    const VkAllocationCallbacks* allocator,
1038                                    VkInstance* instance_out) {
1039    LayerChain chain(true,
1040                     (allocator) ? *allocator : driver::GetDefaultAllocator());
1041
1042    VkResult result = chain.ActivateLayers(create_info->ppEnabledLayerNames,
1043                                           create_info->enabledLayerCount,
1044                                           create_info->ppEnabledExtensionNames,
1045                                           create_info->enabledExtensionCount);
1046    if (result != VK_SUCCESS)
1047        return result;
1048
1049    // use a local create info when the chain is not empty
1050    VkInstanceCreateInfo local_create_info;
1051    if (!chain.Empty()) {
1052        local_create_info = *create_info;
1053        chain.ModifyCreateInfo(local_create_info);
1054        create_info = &local_create_info;
1055    }
1056
1057    return chain.Create(create_info, allocator, instance_out);
1058}
1059
1060VkResult LayerChain::CreateDevice(VkPhysicalDevice physical_dev,
1061                                  const VkDeviceCreateInfo* create_info,
1062                                  const VkAllocationCallbacks* allocator,
1063                                  VkDevice* dev_out) {
1064    LayerChain chain(false, (allocator)
1065                                ? *allocator
1066                                : driver::GetData(physical_dev).allocator);
1067
1068    VkResult result = chain.ActivateLayers(
1069        physical_dev, create_info->ppEnabledLayerNames,
1070        create_info->enabledLayerCount, create_info->ppEnabledExtensionNames,
1071        create_info->enabledExtensionCount);
1072    if (result != VK_SUCCESS)
1073        return result;
1074
1075    // use a local create info when the chain is not empty
1076    VkDeviceCreateInfo local_create_info;
1077    if (!chain.Empty()) {
1078        local_create_info = *create_info;
1079        chain.ModifyCreateInfo(local_create_info);
1080        create_info = &local_create_info;
1081    }
1082
1083    return chain.Create(physical_dev, create_info, allocator, dev_out);
1084}
1085
1086void LayerChain::DestroyInstance(VkInstance instance,
1087                                 const VkAllocationCallbacks* allocator) {
1088    InstanceData& data = GetData(instance);
1089
1090    if (data.debug_callback != VK_NULL_HANDLE)
1091        data.destroy_debug_callback(instance, data.debug_callback, allocator);
1092
1093    ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers);
1094    uint32_t layer_count = data.layer_count;
1095
1096    VkAllocationCallbacks local_allocator;
1097    if (!allocator)
1098        local_allocator = driver::GetData(instance).allocator;
1099
1100    // this also destroys InstanceData
1101    data.dispatch.DestroyInstance(instance, allocator);
1102
1103    DestroyLayers(layers, layer_count,
1104                  (allocator) ? *allocator : local_allocator);
1105}
1106
1107void LayerChain::DestroyDevice(VkDevice device,
1108                               const VkAllocationCallbacks* allocator) {
1109    DeviceData& data = GetData(device);
1110    // this also destroys DeviceData
1111    data.dispatch.DestroyDevice(device, allocator);
1112}
1113
1114const LayerChain::ActiveLayer* LayerChain::GetActiveLayers(
1115    VkPhysicalDevice physical_dev,
1116    uint32_t& count) {
1117    count = GetData(physical_dev).layer_count;
1118    return reinterpret_cast<const ActiveLayer*>(GetData(physical_dev).layers);
1119}
1120
1121// ----------------------------------------------------------------------------
1122
1123bool EnsureInitialized() {
1124    static std::once_flag once_flag;
1125    static bool initialized;
1126
1127    std::call_once(once_flag, []() {
1128        if (driver::OpenHAL()) {
1129            DiscoverLayers();
1130            initialized = true;
1131        }
1132    });
1133
1134    return initialized;
1135}
1136
1137}  // anonymous namespace
1138
1139VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1140                        const VkAllocationCallbacks* pAllocator,
1141                        VkInstance* pInstance) {
1142    if (!EnsureInitialized())
1143        return VK_ERROR_INITIALIZATION_FAILED;
1144
1145    return LayerChain::CreateInstance(pCreateInfo, pAllocator, pInstance);
1146}
1147
1148void DestroyInstance(VkInstance instance,
1149                     const VkAllocationCallbacks* pAllocator) {
1150    if (instance != VK_NULL_HANDLE)
1151        LayerChain::DestroyInstance(instance, pAllocator);
1152}
1153
1154VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1155                      const VkDeviceCreateInfo* pCreateInfo,
1156                      const VkAllocationCallbacks* pAllocator,
1157                      VkDevice* pDevice) {
1158    return LayerChain::CreateDevice(physicalDevice, pCreateInfo, pAllocator,
1159                                    pDevice);
1160}
1161
1162void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1163    if (device != VK_NULL_HANDLE)
1164        LayerChain::DestroyDevice(device, pAllocator);
1165}
1166
1167VkResult EnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
1168                                          VkLayerProperties* pProperties) {
1169    if (!EnsureInitialized())
1170        return VK_ERROR_INITIALIZATION_FAILED;
1171
1172    uint32_t count = GetLayerCount();
1173
1174    if (!pProperties) {
1175        *pPropertyCount = count;
1176        return VK_SUCCESS;
1177    }
1178
1179    uint32_t copied = std::min(*pPropertyCount, count);
1180    for (uint32_t i = 0; i < copied; i++)
1181        pProperties[i] = GetLayerProperties(GetLayer(i));
1182    *pPropertyCount = copied;
1183
1184    return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE;
1185}
1186
1187VkResult EnumerateInstanceExtensionProperties(
1188    const char* pLayerName,
1189    uint32_t* pPropertyCount,
1190    VkExtensionProperties* pProperties) {
1191    if (!EnsureInitialized())
1192        return VK_ERROR_INITIALIZATION_FAILED;
1193
1194    if (pLayerName) {
1195        const Layer* layer = FindLayer(pLayerName);
1196        if (!layer)
1197            return VK_ERROR_LAYER_NOT_PRESENT;
1198
1199        uint32_t count;
1200        const VkExtensionProperties* props =
1201            GetLayerInstanceExtensions(*layer, count);
1202
1203        if (!pProperties || *pPropertyCount > count)
1204            *pPropertyCount = count;
1205        if (pProperties)
1206            std::copy(props, props + *pPropertyCount, pProperties);
1207
1208        return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1209    }
1210
1211    // TODO how about extensions from implicitly enabled layers?
1212    return vulkan::driver::EnumerateInstanceExtensionProperties(
1213        nullptr, pPropertyCount, pProperties);
1214}
1215
1216VkResult EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
1217                                        uint32_t* pPropertyCount,
1218                                        VkLayerProperties* pProperties) {
1219    uint32_t count;
1220    const LayerChain::ActiveLayer* layers =
1221        LayerChain::GetActiveLayers(physicalDevice, count);
1222
1223    if (!pProperties) {
1224        *pPropertyCount = count;
1225        return VK_SUCCESS;
1226    }
1227
1228    uint32_t copied = std::min(*pPropertyCount, count);
1229    for (uint32_t i = 0; i < copied; i++)
1230        pProperties[i] = GetLayerProperties(*layers[i].ref);
1231    *pPropertyCount = copied;
1232
1233    return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE;
1234}
1235
1236VkResult EnumerateDeviceExtensionProperties(
1237    VkPhysicalDevice physicalDevice,
1238    const char* pLayerName,
1239    uint32_t* pPropertyCount,
1240    VkExtensionProperties* pProperties) {
1241    if (pLayerName) {
1242        // EnumerateDeviceLayerProperties enumerates active layers for
1243        // backward compatibility.  The extension query here should work for
1244        // all layers.
1245        const Layer* layer = FindLayer(pLayerName);
1246        if (!layer)
1247            return VK_ERROR_LAYER_NOT_PRESENT;
1248
1249        uint32_t count;
1250        const VkExtensionProperties* props =
1251            GetLayerDeviceExtensions(*layer, count);
1252
1253        if (!pProperties || *pPropertyCount > count)
1254            *pPropertyCount = count;
1255        if (pProperties)
1256            std::copy(props, props + *pPropertyCount, pProperties);
1257
1258        return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1259    }
1260
1261    // TODO how about extensions from implicitly enabled layers?
1262    const InstanceData& data = GetData(physicalDevice);
1263    return data.dispatch.EnumerateDeviceExtensionProperties(
1264        physicalDevice, nullptr, pPropertyCount, pProperties);
1265}
1266
1267}  // namespace api
1268}  // namespace vulkan
1269