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