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