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 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 
19 #include "driver.h"
20 
21 #include <dlfcn.h>
22 #include <malloc.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include <SurfaceFlingerProperties.h>
27 #include <android-base/properties.h>
28 #include <android/dlext.h>
29 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
30 #include <configstore/Utils.h>
31 #include <graphicsenv/GraphicsEnv.h>
32 #include <log/log.h>
33 #include <sys/prctl.h>
34 #include <utils/Timers.h>
35 #include <utils/Trace.h>
36 #include <vndksupport/linker.h>
37 
38 #include <algorithm>
39 #include <array>
40 #include <climits>
41 #include <new>
42 #include <vector>
43 
44 #include "stubhal.h"
45 
46 using namespace android::hardware::configstore;
47 using namespace android::hardware::configstore::V1_0;
48 
49 extern "C" android_namespace_t* android_get_exported_namespace(const char*);
50 
51 // #define ENABLE_ALLOC_CALLSTACKS 1
52 #if ENABLE_ALLOC_CALLSTACKS
53 #include <utils/CallStack.h>
54 #define ALOGD_CALLSTACK(...)                             \
55     do {                                                 \
56         ALOGD(__VA_ARGS__);                              \
57         android::CallStack callstack;                    \
58         callstack.update();                              \
59         callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, "  "); \
60     } while (false)
61 #else
62 #define ALOGD_CALLSTACK(...) \
63     do {                     \
64     } while (false)
65 #endif
66 
67 namespace vulkan {
68 namespace driver {
69 
70 namespace {
71 
72 class Hal {
73    public:
74     static bool Open();
75 
Get()76     static const Hal& Get() { return hal_; }
Device()77     static const hwvulkan_device_t& Device() { return *Get().dev_; }
78 
GetDebugReportIndex() const79     int GetDebugReportIndex() const { return debug_report_index_; }
80 
81    private:
Hal()82     Hal() : dev_(nullptr), debug_report_index_(-1) {}
83     Hal(const Hal&) = delete;
84     Hal& operator=(const Hal&) = delete;
85 
86     bool ShouldUnloadBuiltinDriver();
87     void UnloadBuiltinDriver();
88     bool InitDebugReportIndex();
89 
90     static Hal hal_;
91 
92     const hwvulkan_device_t* dev_;
93     int debug_report_index_;
94 };
95 
96 class CreateInfoWrapper {
97    public:
98     CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
99                       uint32_t icd_api_version,
100                       const VkAllocationCallbacks& allocator);
101     CreateInfoWrapper(VkPhysicalDevice physical_dev,
102                       const VkDeviceCreateInfo& create_info,
103                       uint32_t icd_api_version,
104                       const VkAllocationCallbacks& allocator);
105     ~CreateInfoWrapper();
106 
107     VkResult Validate();
108 
109     const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
110     const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
111 
112     explicit operator const VkInstanceCreateInfo*() const;
113     explicit operator const VkDeviceCreateInfo*() const;
114 
115    private:
116     struct ExtensionFilter {
117         VkExtensionProperties* exts;
118         uint32_t ext_count;
119 
120         const char** names;
121         uint32_t name_count;
ExtensionFiltervulkan::driver::__anonbed2d0790111::CreateInfoWrapper::ExtensionFilter122         ExtensionFilter()
123             : exts(nullptr), ext_count(0), names(nullptr), name_count(0) {}
124     };
125 
126     VkResult SanitizeApiVersion();
127     VkResult SanitizePNext();
128     VkResult SanitizeLayers();
129     VkResult SanitizeExtensions();
130 
131     VkResult QueryExtensionCount(uint32_t& count) const;
132     VkResult EnumerateExtensions(uint32_t& count,
133                                  VkExtensionProperties* props) const;
134     VkResult InitExtensionFilter();
135     void FilterExtension(const char* name);
136 
137     const bool is_instance_;
138     const VkAllocationCallbacks& allocator_;
139     const uint32_t loader_api_version_;
140     const uint32_t icd_api_version_;
141 
142     VkPhysicalDevice physical_dev_;
143 
144     union {
145         VkInstanceCreateInfo instance_info_;
146         VkDeviceCreateInfo dev_info_;
147     };
148 
149     VkApplicationInfo application_info_;
150 
151     ExtensionFilter extension_filter_;
152 
153     std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
154     std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
155 };
156 
157 Hal Hal::hal_;
158 
159 const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
160     "ro.hardware.vulkan",
161     "ro.board.platform",
162 }};
163 constexpr int LIB_DL_FLAGS = RTLD_LOCAL | RTLD_NOW;
164 constexpr char RO_VULKAN_APEX_PROPERTY[] = "ro.vulkan.apex";
165 
166 // LoadDriver returns:
167 // * 0 when succeed, or
168 // * -ENOENT when fail to open binary libraries, or
169 // * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
170 //   HWVULKAN_HARDWARE_MODULE_ID in the library.
LoadDriver(android_namespace_t * library_namespace,const char * ns_name,const hwvulkan_module_t ** module)171 int LoadDriver(android_namespace_t* library_namespace,
172                const char* ns_name,
173                const hwvulkan_module_t** module) {
174     ATRACE_CALL();
175 
176     void* so = nullptr;
177     for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
178         std::string lib_name = android::base::GetProperty(key, "");
179         if (lib_name.empty())
180             continue;
181 
182         lib_name = "vulkan." + lib_name + ".so";
183         if (library_namespace) {
184             // load updated driver
185             const android_dlextinfo dlextinfo = {
186                 .flags = ANDROID_DLEXT_USE_NAMESPACE,
187                 .library_namespace = library_namespace,
188             };
189             so = android_dlopen_ext(lib_name.c_str(), LIB_DL_FLAGS, &dlextinfo);
190             if (!so) {
191                 ALOGE("Could not load %s from %s namespace: %s.",
192                       lib_name.c_str(), ns_name, dlerror());
193             }
194         } else {
195             // load built-in driver
196             so = android_load_sphal_library(lib_name.c_str(), LIB_DL_FLAGS);
197         }
198         if (so)
199             break;
200     }
201     if (!so)
202         return -ENOENT;
203 
204     auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
205     if (!hmi) {
206         ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
207         dlclose(so);
208         return -EINVAL;
209     }
210     if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
211         ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
212         dlclose(so);
213         return -EINVAL;
214     }
215     hmi->dso = so;
216     *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
217     return 0;
218 }
219 
LoadDriverFromApex(const hwvulkan_module_t ** module)220 int LoadDriverFromApex(const hwvulkan_module_t** module) {
221     ATRACE_CALL();
222 
223     auto apex_name = android::base::GetProperty(RO_VULKAN_APEX_PROPERTY, "");
224     if (apex_name == "") {
225         return -ENOENT;
226     }
227     // Get linker namespace for Vulkan APEX
228     std::replace(apex_name.begin(), apex_name.end(), '.', '_');
229     auto ns = android_get_exported_namespace(apex_name.c_str());
230     if (!ns) {
231         return -ENOENT;
232     }
233     android::GraphicsEnv::getInstance().setDriverToLoad(
234         android::GpuStatsInfo::Driver::VULKAN);
235     return LoadDriver(ns, apex_name.c_str(), module);
236 }
237 
LoadBuiltinDriver(const hwvulkan_module_t ** module)238 int LoadBuiltinDriver(const hwvulkan_module_t** module) {
239     ATRACE_CALL();
240 
241     android::GraphicsEnv::getInstance().setDriverToLoad(
242         android::GpuStatsInfo::Driver::VULKAN);
243     return LoadDriver(nullptr, nullptr, module);
244 }
245 
LoadUpdatedDriver(const hwvulkan_module_t ** module)246 int LoadUpdatedDriver(const hwvulkan_module_t** module) {
247     ATRACE_CALL();
248 
249     auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
250     if (!ns)
251         return -ENOENT;
252     android::GraphicsEnv::getInstance().setDriverToLoad(
253         android::GpuStatsInfo::Driver::VULKAN_UPDATED);
254     int result = LoadDriver(ns, "updatable gfx driver", module);
255     if (result != 0) {
256         LOG_ALWAYS_FATAL(
257             "couldn't find an updated Vulkan implementation from %s",
258             android::GraphicsEnv::getInstance().getDriverPath().c_str());
259     }
260     return result;
261 }
262 
Open()263 bool Hal::Open() {
264     ATRACE_CALL();
265 
266     const nsecs_t openTime = systemTime();
267 
268     if (hal_.ShouldUnloadBuiltinDriver()) {
269         hal_.UnloadBuiltinDriver();
270     }
271 
272     if (hal_.dev_)
273         return true;
274 
275     // Use a stub device unless we successfully open a real HAL device.
276     hal_.dev_ = &stubhal::kDevice;
277 
278     int result;
279     const hwvulkan_module_t* module = nullptr;
280 
281     result = LoadUpdatedDriver(&module);
282     if (result == -ENOENT) {
283         result = LoadDriverFromApex(&module);
284     }
285     if (result == -ENOENT) {
286         result = LoadBuiltinDriver(&module);
287     }
288     if (result != 0) {
289         android::GraphicsEnv::getInstance().setDriverLoaded(
290             android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
291         ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
292         return true;
293     }
294 
295 
296     hwvulkan_device_t* device;
297     ATRACE_BEGIN("hwvulkan module open");
298     result =
299         module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
300                                      reinterpret_cast<hw_device_t**>(&device));
301     ATRACE_END();
302     if (result != 0) {
303         android::GraphicsEnv::getInstance().setDriverLoaded(
304             android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
305         // Any device with a Vulkan HAL should be able to open the device.
306         ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
307               result);
308         return false;
309     }
310 
311     hal_.dev_ = device;
312 
313     hal_.InitDebugReportIndex();
314 
315     android::GraphicsEnv::getInstance().setDriverLoaded(
316         android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
317 
318     return true;
319 }
320 
ShouldUnloadBuiltinDriver()321 bool Hal::ShouldUnloadBuiltinDriver() {
322     // Should not unload since the driver was not loaded
323     if (!hal_.dev_)
324         return false;
325 
326     // Should not unload if stubhal is used on the device
327     if (hal_.dev_ == &stubhal::kDevice)
328         return false;
329 
330     // Unload the driver if updated driver is chosen
331     if (android::GraphicsEnv::getInstance().getDriverNamespace())
332         return true;
333 
334     return false;
335 }
336 
UnloadBuiltinDriver()337 void Hal::UnloadBuiltinDriver() {
338     ATRACE_CALL();
339 
340     ALOGD("Unload builtin Vulkan driver.");
341 
342     // Close the opened device
343     int err = hal_.dev_->common.close(
344         const_cast<struct hw_device_t*>(&hal_.dev_->common));
345     ALOG_ASSERT(!err, "hw_device_t::close() failed.");
346 
347     // Close the opened shared library in the hw_module_t
348     android_unload_sphal_library(hal_.dev_->common.module->dso);
349 
350     hal_.dev_ = nullptr;
351     hal_.debug_report_index_ = -1;
352 }
353 
InitDebugReportIndex()354 bool Hal::InitDebugReportIndex() {
355     ATRACE_CALL();
356 
357     uint32_t count;
358     if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
359         VK_SUCCESS) {
360         ALOGE("failed to get HAL instance extension count");
361         return false;
362     }
363 
364     VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
365         malloc(sizeof(VkExtensionProperties) * count));
366     if (!exts) {
367         ALOGE("failed to allocate HAL instance extension array");
368         return false;
369     }
370 
371     if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
372         VK_SUCCESS) {
373         ALOGE("failed to enumerate HAL instance extensions");
374         free(exts);
375         return false;
376     }
377 
378     for (uint32_t i = 0; i < count; i++) {
379         if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
380             0) {
381             debug_report_index_ = static_cast<int>(i);
382             break;
383         }
384     }
385 
386     free(exts);
387 
388     return true;
389 }
390 
CreateInfoWrapper(const VkInstanceCreateInfo & create_info,uint32_t icd_api_version,const VkAllocationCallbacks & allocator)391 CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
392                                      uint32_t icd_api_version,
393                                      const VkAllocationCallbacks& allocator)
394     : is_instance_(true),
395       allocator_(allocator),
396       loader_api_version_(VK_API_VERSION_1_3),
397       icd_api_version_(icd_api_version),
398       physical_dev_(VK_NULL_HANDLE),
399       instance_info_(create_info),
400       extension_filter_() {}
401 
CreateInfoWrapper(VkPhysicalDevice physical_dev,const VkDeviceCreateInfo & create_info,uint32_t icd_api_version,const VkAllocationCallbacks & allocator)402 CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
403                                      const VkDeviceCreateInfo& create_info,
404                                      uint32_t icd_api_version,
405                                      const VkAllocationCallbacks& allocator)
406     : is_instance_(false),
407       allocator_(allocator),
408       loader_api_version_(VK_API_VERSION_1_3),
409       icd_api_version_(icd_api_version),
410       physical_dev_(physical_dev),
411       dev_info_(create_info),
412       extension_filter_() {}
413 
~CreateInfoWrapper()414 CreateInfoWrapper::~CreateInfoWrapper() {
415     allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
416     allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
417 }
418 
Validate()419 VkResult CreateInfoWrapper::Validate() {
420     VkResult result = SanitizeApiVersion();
421     if (result == VK_SUCCESS)
422         result = SanitizePNext();
423     if (result == VK_SUCCESS)
424         result = SanitizeLayers();
425     if (result == VK_SUCCESS)
426         result = SanitizeExtensions();
427 
428     return result;
429 }
430 
431 const std::bitset<ProcHook::EXTENSION_COUNT>&
GetHookExtensions() const432 CreateInfoWrapper::GetHookExtensions() const {
433     return hook_extensions_;
434 }
435 
436 const std::bitset<ProcHook::EXTENSION_COUNT>&
GetHalExtensions() const437 CreateInfoWrapper::GetHalExtensions() const {
438     return hal_extensions_;
439 }
440 
operator const VkInstanceCreateInfo*() const441 CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
442     return &instance_info_;
443 }
444 
operator const VkDeviceCreateInfo*() const445 CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
446     return &dev_info_;
447 }
448 
SanitizeApiVersion()449 VkResult CreateInfoWrapper::SanitizeApiVersion() {
450     if (!is_instance_ || !instance_info_.pApplicationInfo)
451         return VK_SUCCESS;
452 
453     if (icd_api_version_ > VK_API_VERSION_1_0 ||
454         instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
455         return VK_SUCCESS;
456 
457     // override apiVersion to avoid error return from 1.0 icd
458     application_info_ = *instance_info_.pApplicationInfo;
459     application_info_.apiVersion = VK_API_VERSION_1_0;
460     instance_info_.pApplicationInfo = &application_info_;
461 
462     return VK_SUCCESS;
463 }
464 
SanitizePNext()465 VkResult CreateInfoWrapper::SanitizePNext() {
466     const struct StructHeader {
467         VkStructureType type;
468         const void* next;
469     } * header;
470 
471     if (is_instance_) {
472         header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
473 
474         // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
475         while (header &&
476                header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
477             header = reinterpret_cast<const StructHeader*>(header->next);
478 
479         instance_info_.pNext = header;
480     } else {
481         header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
482 
483         // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
484         while (header &&
485                header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
486             header = reinterpret_cast<const StructHeader*>(header->next);
487 
488         dev_info_.pNext = header;
489     }
490 
491     return VK_SUCCESS;
492 }
493 
SanitizeLayers()494 VkResult CreateInfoWrapper::SanitizeLayers() {
495     auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
496                                        : dev_info_.ppEnabledLayerNames;
497     auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
498                                        : dev_info_.enabledLayerCount;
499 
500     // remove all layers
501     layer_names = nullptr;
502     layer_count = 0;
503 
504     return VK_SUCCESS;
505 }
506 
SanitizeExtensions()507 VkResult CreateInfoWrapper::SanitizeExtensions() {
508     auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
509                                      : dev_info_.ppEnabledExtensionNames;
510     auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
511                                      : dev_info_.enabledExtensionCount;
512 
513     VkResult result = InitExtensionFilter();
514     if (result != VK_SUCCESS)
515         return result;
516 
517     if (is_instance_ && icd_api_version_ < loader_api_version_) {
518         for (uint32_t i = 0; i < ext_count; i++) {
519             // Upon api downgrade, skip the promoted instance extensions in the
520             // first pass to avoid duplicate extensions.
521             const std::optional<uint32_t> version =
522                 GetInstanceExtensionPromotedVersion(ext_names[i]);
523             if (version && *version > icd_api_version_ &&
524                 *version <= loader_api_version_)
525                 continue;
526 
527             FilterExtension(ext_names[i]);
528         }
529 
530         // Enable the required extensions to support core functionalities.
531         const auto promoted_extensions = GetPromotedInstanceExtensions(
532             icd_api_version_, loader_api_version_);
533         for (const auto& promoted_extension : promoted_extensions)
534             FilterExtension(promoted_extension);
535     } else {
536         for (uint32_t i = 0; i < ext_count; i++)
537             FilterExtension(ext_names[i]);
538     }
539 
540     // Enable device extensions that contain physical-device commands, so that
541     // vkGetInstanceProcAddr will return those physical-device commands.
542     if (is_instance_) {
543         hook_extensions_.set(ProcHook::KHR_swapchain);
544     }
545 
546     const uint32_t api_version =
547         is_instance_ ? loader_api_version_
548                      : std::min(icd_api_version_, loader_api_version_);
549     switch (api_version) {
550         case VK_API_VERSION_1_3:
551             hook_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
552             hal_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
553             [[clang::fallthrough]];
554         case VK_API_VERSION_1_2:
555             hook_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
556             hal_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
557             [[clang::fallthrough]];
558         case VK_API_VERSION_1_1:
559             hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
560             hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
561             [[clang::fallthrough]];
562         case VK_API_VERSION_1_0:
563             hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
564             hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
565             break;
566         default:
567             ALOGE("Unknown API version[%u]", api_version);
568             break;
569     }
570 
571     ext_names = extension_filter_.names;
572     ext_count = extension_filter_.name_count;
573 
574     return VK_SUCCESS;
575 }
576 
QueryExtensionCount(uint32_t & count) const577 VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
578     if (is_instance_) {
579         return Hal::Device().EnumerateInstanceExtensionProperties(
580             nullptr, &count, nullptr);
581     } else {
582         const auto& driver = GetData(physical_dev_).driver;
583         return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
584                                                          &count, nullptr);
585     }
586 }
587 
EnumerateExtensions(uint32_t & count,VkExtensionProperties * props) const588 VkResult CreateInfoWrapper::EnumerateExtensions(
589     uint32_t& count,
590     VkExtensionProperties* props) const {
591     if (is_instance_) {
592         return Hal::Device().EnumerateInstanceExtensionProperties(
593             nullptr, &count, props);
594     } else {
595         const auto& driver = GetData(physical_dev_).driver;
596         return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
597                                                          &count, props);
598     }
599 }
600 
InitExtensionFilter()601 VkResult CreateInfoWrapper::InitExtensionFilter() {
602     // query extension count
603     uint32_t count;
604     VkResult result = QueryExtensionCount(count);
605     if (result != VK_SUCCESS || count == 0)
606         return result;
607 
608     auto& filter = extension_filter_;
609     filter.exts =
610         reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
611             allocator_.pUserData, sizeof(VkExtensionProperties) * count,
612             alignof(VkExtensionProperties),
613             VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
614     if (!filter.exts)
615         return VK_ERROR_OUT_OF_HOST_MEMORY;
616 
617     // enumerate extensions
618     result = EnumerateExtensions(count, filter.exts);
619     if (result != VK_SUCCESS && result != VK_INCOMPLETE)
620         return result;
621 
622     if (!count)
623         return VK_SUCCESS;
624 
625     filter.ext_count = count;
626 
627     // allocate name array
628     if (is_instance_) {
629         uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
630 
631         // It requires enabling additional promoted extensions to downgrade api,
632         // so we reserve enough space here.
633         if (icd_api_version_ < loader_api_version_) {
634             enabled_ext_count += CountPromotedInstanceExtensions(
635                 icd_api_version_, loader_api_version_);
636         }
637 
638         count = std::min(filter.ext_count, enabled_ext_count);
639     } else {
640         count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
641     }
642 
643     if (!count)
644         return VK_SUCCESS;
645 
646     filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
647         allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
648         VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
649     if (!filter.names)
650         return VK_ERROR_OUT_OF_HOST_MEMORY;
651 
652     return VK_SUCCESS;
653 }
654 
FilterExtension(const char * name)655 void CreateInfoWrapper::FilterExtension(const char* name) {
656     auto& filter = extension_filter_;
657 
658     ProcHook::Extension ext_bit = GetProcHookExtension(name);
659     if (is_instance_) {
660         switch (ext_bit) {
661             case ProcHook::KHR_android_surface:
662             case ProcHook::KHR_surface:
663             case ProcHook::KHR_surface_protected_capabilities:
664             case ProcHook::EXT_swapchain_colorspace:
665             case ProcHook::KHR_get_surface_capabilities2:
666             case ProcHook::GOOGLE_surfaceless_query:
667             case ProcHook::EXT_surface_maintenance1:
668                 hook_extensions_.set(ext_bit);
669                 // return now as these extensions do not require HAL support
670                 return;
671             case ProcHook::EXT_debug_report:
672                 // both we and HAL can take part in
673                 hook_extensions_.set(ext_bit);
674                 break;
675             case ProcHook::KHR_get_physical_device_properties2:
676             case ProcHook::KHR_device_group_creation:
677             case ProcHook::KHR_external_memory_capabilities:
678             case ProcHook::KHR_external_semaphore_capabilities:
679             case ProcHook::KHR_external_fence_capabilities:
680             case ProcHook::EXTENSION_UNKNOWN:
681                 // Extensions we don't need to do anything about at this level
682                 break;
683 
684             case ProcHook::KHR_bind_memory2:
685             case ProcHook::KHR_incremental_present:
686             case ProcHook::KHR_shared_presentable_image:
687             case ProcHook::KHR_swapchain:
688             case ProcHook::EXT_hdr_metadata:
689             case ProcHook::EXT_swapchain_maintenance1:
690             case ProcHook::ANDROID_external_memory_android_hardware_buffer:
691             case ProcHook::ANDROID_native_buffer:
692             case ProcHook::GOOGLE_display_timing:
693             case ProcHook::KHR_external_fence_fd:
694             case ProcHook::EXTENSION_CORE_1_0:
695             case ProcHook::EXTENSION_CORE_1_1:
696             case ProcHook::EXTENSION_CORE_1_2:
697             case ProcHook::EXTENSION_CORE_1_3:
698             case ProcHook::EXTENSION_COUNT:
699                 // Device and meta extensions. If we ever get here it's a bug in
700                 // our code. But enumerating them lets us avoid having a default
701                 // case, and default hides other bugs.
702                 ALOGE(
703                     "CreateInfoWrapper::FilterExtension: invalid instance "
704                     "extension '%s'. FIX ME",
705                     name);
706                 return;
707 
708             // Don't use a default case. Without it, -Wswitch will tell us
709             // at compile time if someone adds a new ProcHook extension but
710             // doesn't handle it above. That's a real bug that has
711             // not-immediately-obvious effects.
712             //
713             // default:
714             //     break;
715         }
716     } else {
717         switch (ext_bit) {
718             case ProcHook::KHR_swapchain:
719                 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
720                 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
721                 ext_bit = ProcHook::ANDROID_native_buffer;
722                 break;
723             case ProcHook::KHR_incremental_present:
724             case ProcHook::KHR_shared_presentable_image:
725             case ProcHook::GOOGLE_display_timing:
726                 hook_extensions_.set(ext_bit);
727                 // return now as these extensions do not require HAL support
728                 return;
729             case ProcHook::EXT_swapchain_maintenance1:
730                 // map VK_KHR_swapchain_maintenance1 to KHR_external_fence_fd
731                 name = VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME;
732                 ext_bit = ProcHook::KHR_external_fence_fd;
733                 break;
734             case ProcHook::EXT_hdr_metadata:
735             case ProcHook::KHR_bind_memory2:
736                 hook_extensions_.set(ext_bit);
737                 break;
738             case ProcHook::ANDROID_external_memory_android_hardware_buffer:
739             case ProcHook::KHR_external_fence_fd:
740             case ProcHook::EXTENSION_UNKNOWN:
741                 // Extensions we don't need to do anything about at this level
742                 break;
743 
744             case ProcHook::KHR_android_surface:
745             case ProcHook::KHR_get_physical_device_properties2:
746             case ProcHook::KHR_device_group_creation:
747             case ProcHook::KHR_external_memory_capabilities:
748             case ProcHook::KHR_external_semaphore_capabilities:
749             case ProcHook::KHR_external_fence_capabilities:
750             case ProcHook::KHR_get_surface_capabilities2:
751             case ProcHook::KHR_surface:
752             case ProcHook::KHR_surface_protected_capabilities:
753             case ProcHook::EXT_debug_report:
754             case ProcHook::EXT_swapchain_colorspace:
755             case ProcHook::EXT_surface_maintenance1:
756             case ProcHook::GOOGLE_surfaceless_query:
757             case ProcHook::ANDROID_native_buffer:
758             case ProcHook::EXTENSION_CORE_1_0:
759             case ProcHook::EXTENSION_CORE_1_1:
760             case ProcHook::EXTENSION_CORE_1_2:
761             case ProcHook::EXTENSION_CORE_1_3:
762             case ProcHook::EXTENSION_COUNT:
763                 // Instance and meta extensions. If we ever get here it's a bug
764                 // in our code. But enumerating them lets us avoid having a
765                 // default case, and default hides other bugs.
766                 ALOGE(
767                     "CreateInfoWrapper::FilterExtension: invalid device "
768                     "extension '%s'. FIX ME",
769                     name);
770                 return;
771 
772             // Don't use a default case. Without it, -Wswitch will tell us
773             // at compile time if someone adds a new ProcHook extension but
774             // doesn't handle it above. That's a real bug that has
775             // not-immediately-obvious effects.
776             //
777             // default:
778             //     break;
779         }
780     }
781 
782     for (uint32_t i = 0; i < filter.ext_count; i++) {
783         const VkExtensionProperties& props = filter.exts[i];
784         // ignore unknown extensions
785         if (strcmp(name, props.extensionName) != 0)
786             continue;
787 
788         if (ext_bit != ProcHook::EXTENSION_UNKNOWN &&
789                 hal_extensions_.test(ext_bit)) {
790             ALOGI("CreateInfoWrapper::FilterExtension: already have '%s'.", name);
791             continue;
792         }
793 
794         // Ignore duplicate extensions (see: b/288929054)
795         bool duplicate_entry = false;
796         for (uint32_t j = 0; j < filter.name_count; j++) {
797             if (strcmp(name, filter.names[j]) == 0) {
798                 duplicate_entry = true;
799                 break;
800             }
801         }
802         if (duplicate_entry == true)
803             continue;
804 
805         filter.names[filter.name_count++] = name;
806         if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
807             if (ext_bit == ProcHook::ANDROID_native_buffer)
808                 hook_extensions_.set(ProcHook::KHR_swapchain);
809             if (ext_bit == ProcHook::KHR_external_fence_fd)
810                 hook_extensions_.set(ProcHook::EXT_swapchain_maintenance1);
811 
812             hal_extensions_.set(ext_bit);
813         }
814 
815         break;
816     }
817 }
818 
DefaultAllocate(void *,size_t size,size_t alignment,VkSystemAllocationScope)819 VKAPI_ATTR void* DefaultAllocate(void*,
820                                  size_t size,
821                                  size_t alignment,
822                                  VkSystemAllocationScope) {
823     void* ptr = nullptr;
824     // Vulkan requires 'alignment' to be a power of two, but posix_memalign
825     // additionally requires that it be at least sizeof(void*).
826     int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
827     ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
828                     ret, ptr);
829     return ret == 0 ? ptr : nullptr;
830 }
831 
DefaultReallocate(void *,void * ptr,size_t size,size_t alignment,VkSystemAllocationScope)832 VKAPI_ATTR void* DefaultReallocate(void*,
833                                    void* ptr,
834                                    size_t size,
835                                    size_t alignment,
836                                    VkSystemAllocationScope) {
837     if (size == 0) {
838         free(ptr);
839         return nullptr;
840     }
841 
842     // TODO(b/143295633): Right now we never shrink allocations; if the new
843     // request is smaller than the existing chunk, we just continue using it.
844     // Right now the loader never reallocs, so this doesn't matter. If that
845     // changes, or if this code is copied into some other project, this should
846     // probably have a heuristic to allocate-copy-free when doing so will save
847     // "enough" space.
848     size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
849     if (size <= old_size)
850         return ptr;
851 
852     void* new_ptr = nullptr;
853     if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
854         return nullptr;
855     if (ptr) {
856         memcpy(new_ptr, ptr, std::min(old_size, size));
857         free(ptr);
858     }
859     return new_ptr;
860 }
861 
DefaultFree(void *,void * ptr)862 VKAPI_ATTR void DefaultFree(void*, void* ptr) {
863     ALOGD_CALLSTACK("Free: %p", ptr);
864     free(ptr);
865 }
866 
AllocateInstanceData(const VkAllocationCallbacks & allocator)867 InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
868     void* data_mem = allocator.pfnAllocation(
869         allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
870         VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
871     if (!data_mem)
872         return nullptr;
873 
874     return new (data_mem) InstanceData(allocator);
875 }
876 
FreeInstanceData(InstanceData * data,const VkAllocationCallbacks & allocator)877 void FreeInstanceData(InstanceData* data,
878                       const VkAllocationCallbacks& allocator) {
879     data->~InstanceData();
880     allocator.pfnFree(allocator.pUserData, data);
881 }
882 
AllocateDeviceData(const VkAllocationCallbacks & allocator,const DebugReportCallbackList & debug_report_callbacks)883 DeviceData* AllocateDeviceData(
884     const VkAllocationCallbacks& allocator,
885     const DebugReportCallbackList& debug_report_callbacks) {
886     void* data_mem = allocator.pfnAllocation(
887         allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
888         VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
889     if (!data_mem)
890         return nullptr;
891 
892     return new (data_mem) DeviceData(allocator, debug_report_callbacks);
893 }
894 
FreeDeviceData(DeviceData * data,const VkAllocationCallbacks & allocator)895 void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
896     data->~DeviceData();
897     allocator.pfnFree(allocator.pUserData, data);
898 }
899 
900 }  // anonymous namespace
901 
OpenHAL()902 bool OpenHAL() {
903     return Hal::Open();
904 }
905 
GetDefaultAllocator()906 const VkAllocationCallbacks& GetDefaultAllocator() {
907     static const VkAllocationCallbacks kDefaultAllocCallbacks = {
908         .pUserData = nullptr,
909         .pfnAllocation = DefaultAllocate,
910         .pfnReallocation = DefaultReallocate,
911         .pfnFree = DefaultFree,
912     };
913 
914     return kDefaultAllocCallbacks;
915 }
916 
GetInstanceProcAddr(VkInstance instance,const char * pName)917 PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
918     const ProcHook* hook = GetProcHook(pName);
919     if (!hook)
920         return Hal::Device().GetInstanceProcAddr(instance, pName);
921 
922     if (!instance) {
923         if (hook->type == ProcHook::GLOBAL)
924             return hook->proc;
925 
926         // v0 layers expect
927         //
928         //   vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
929         //
930         // to work.
931         if (strcmp(pName, "vkCreateDevice") == 0)
932             return hook->proc;
933 
934         ALOGE(
935             "internal vkGetInstanceProcAddr called for %s without an instance",
936             pName);
937 
938         return nullptr;
939     }
940 
941     PFN_vkVoidFunction proc;
942 
943     switch (hook->type) {
944         case ProcHook::INSTANCE:
945             proc = (GetData(instance).hook_extensions[hook->extension])
946                        ? hook->proc
947                        : nullptr;
948             break;
949         case ProcHook::DEVICE:
950             proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
951                        ? hook->proc
952                        : hook->checked_proc;
953             break;
954         default:
955             ALOGE(
956                 "internal vkGetInstanceProcAddr called for %s with an instance",
957                 pName);
958             proc = nullptr;
959             break;
960     }
961 
962     return proc;
963 }
964 
GetDeviceProcAddr(VkDevice device,const char * pName)965 PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
966     const ProcHook* hook = GetProcHook(pName);
967     PFN_vkVoidFunction drv_func = GetData(device).driver.GetDeviceProcAddr(device, pName);
968 
969     if (!hook)
970         return drv_func;
971 
972     if (hook->type != ProcHook::DEVICE) {
973         ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
974         return nullptr;
975     }
976 
977     // Don't hook if we don't have a device entry function below for the core function.
978     if (!drv_func && (hook->extension >= ProcHook::EXTENSION_CORE_1_0))
979         return nullptr;
980 
981     return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
982                                                               : nullptr;
983 }
984 
EnumerateInstanceExtensionProperties(const char * pLayerName,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)985 VkResult EnumerateInstanceExtensionProperties(
986     const char* pLayerName,
987     uint32_t* pPropertyCount,
988     VkExtensionProperties* pProperties) {
989     std::vector<VkExtensionProperties> loader_extensions;
990     loader_extensions.push_back(
991         {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
992     loader_extensions.push_back(
993         {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
994          VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
995     loader_extensions.push_back({
996         VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
997         VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
998     loader_extensions.push_back({
999         VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
1000         VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
1001     loader_extensions.push_back(
1002         {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
1003          VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
1004     loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
1005                                  VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
1006     loader_extensions.push_back({
1007         VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
1008         VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
1009 
1010     static const VkExtensionProperties loader_debug_report_extension = {
1011         VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
1012     };
1013 
1014     // enumerate our extensions first
1015     if (!pLayerName && pProperties) {
1016         uint32_t count = std::min(
1017             *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1018 
1019         std::copy_n(loader_extensions.data(), count, pProperties);
1020 
1021         if (count < loader_extensions.size()) {
1022             *pPropertyCount = count;
1023             return VK_INCOMPLETE;
1024         }
1025 
1026         pProperties += count;
1027         *pPropertyCount -= count;
1028 
1029         if (Hal::Get().GetDebugReportIndex() < 0) {
1030             if (!*pPropertyCount) {
1031                 *pPropertyCount = count;
1032                 return VK_INCOMPLETE;
1033             }
1034 
1035             pProperties[0] = loader_debug_report_extension;
1036             pProperties += 1;
1037             *pPropertyCount -= 1;
1038         }
1039     }
1040 
1041     ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
1042     VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
1043         pLayerName, pPropertyCount, pProperties);
1044     ATRACE_END();
1045 
1046     if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1047         int idx = Hal::Get().GetDebugReportIndex();
1048         if (idx < 0) {
1049             *pPropertyCount += 1;
1050         } else if (pProperties &&
1051                    static_cast<uint32_t>(idx) < *pPropertyCount) {
1052             pProperties[idx].specVersion =
1053                 std::min(pProperties[idx].specVersion,
1054                          loader_debug_report_extension.specVersion);
1055         }
1056 
1057         *pPropertyCount += loader_extensions.size();
1058     }
1059 
1060     return result;
1061 }
1062 
QueryPresentationProperties(VkPhysicalDevice physicalDevice,VkPhysicalDevicePresentationPropertiesANDROID * presentation_properties)1063 void QueryPresentationProperties(
1064     VkPhysicalDevice physicalDevice,
1065     VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
1066     ATRACE_CALL();
1067 
1068     // Request the android-specific presentation properties via GPDP2
1069     VkPhysicalDeviceProperties2 properties = {
1070         VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
1071         presentation_properties,
1072         {},
1073     };
1074 
1075 #pragma clang diagnostic push
1076 #pragma clang diagnostic ignored "-Wold-style-cast"
1077     presentation_properties->sType =
1078         VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1079 #pragma clang diagnostic pop
1080     presentation_properties->pNext = nullptr;
1081     presentation_properties->sharedImage = VK_FALSE;
1082 
1083     const auto& driver = GetData(physicalDevice).driver;
1084 
1085     if (driver.GetPhysicalDeviceProperties2) {
1086         // >= 1.1 driver, supports core GPDP2 entrypoint.
1087         driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1088     } else if (driver.GetPhysicalDeviceProperties2KHR) {
1089         // Old driver, but may support presentation properties
1090         // if we have the GPDP2 extension. Otherwise, no presentation
1091         // properties supported.
1092         driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1093     }
1094 }
1095 
GetAndroidNativeBufferSpecVersion9Support(VkPhysicalDevice physicalDevice,bool & support)1096 VkResult GetAndroidNativeBufferSpecVersion9Support(
1097     VkPhysicalDevice physicalDevice,
1098     bool& support) {
1099     support = false;
1100 
1101     const InstanceData& data = GetData(physicalDevice);
1102 
1103     // Call to get propertyCount
1104     uint32_t propertyCount = 0;
1105     ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1106     VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1107         physicalDevice, nullptr, &propertyCount, nullptr);
1108     ATRACE_END();
1109 
1110     if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1111         return result;
1112     }
1113 
1114     // Call to enumerate properties
1115     std::vector<VkExtensionProperties> properties(propertyCount);
1116     ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1117     result = data.driver.EnumerateDeviceExtensionProperties(
1118         physicalDevice, nullptr, &propertyCount, properties.data());
1119     ATRACE_END();
1120 
1121     if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1122         return result;
1123     }
1124 
1125     for (uint32_t i = 0; i < propertyCount; i++) {
1126         auto& prop = properties[i];
1127 
1128         if (strcmp(prop.extensionName,
1129                    VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1130             continue;
1131 
1132         if (prop.specVersion >= 9) {
1133             support = true;
1134             return result;
1135         }
1136     }
1137 
1138     return result;
1139 }
1140 
CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice)1141 bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1142     const auto& driver = GetData(physicalDevice).driver;
1143     if (!driver.GetPhysicalDeviceExternalFenceProperties)
1144         return false;
1145 
1146     // Requires support for external fences imported from sync fds.
1147     // This is _almost_ universal on Android, but may be missing on
1148     // some extremely old drivers, or on strange implementations like
1149     // cuttlefish.
1150     VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1151         VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1152         nullptr,
1153         VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1154     };
1155     VkExternalFenceProperties fenceProperties = {
1156         VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1157         nullptr,
1158         0, 0, 0
1159     };
1160 
1161     GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1162     if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1163         return true;
1164 
1165     return false;
1166 }
1167 
EnumerateDeviceExtensionProperties(VkPhysicalDevice physicalDevice,const char * pLayerName,uint32_t * pPropertyCount,VkExtensionProperties * pProperties)1168 VkResult EnumerateDeviceExtensionProperties(
1169     VkPhysicalDevice physicalDevice,
1170     const char* pLayerName,
1171     uint32_t* pPropertyCount,
1172     VkExtensionProperties* pProperties) {
1173     const InstanceData& data = GetData(physicalDevice);
1174     // extensions that are unconditionally exposed by the loader
1175     std::vector<VkExtensionProperties> loader_extensions;
1176     loader_extensions.push_back({
1177         VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1178         VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
1179 
1180     bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
1181     if (hdrBoardConfig) {
1182         loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1183                                      VK_EXT_HDR_METADATA_SPEC_VERSION});
1184     }
1185 
1186     VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
1187     QueryPresentationProperties(physicalDevice, &presentation_properties);
1188     if (presentation_properties.sharedImage) {
1189         loader_extensions.push_back({
1190             VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1191             VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
1192     }
1193 
1194     // conditionally add VK_GOOGLE_display_timing if present timestamps are
1195     // supported by the driver:
1196     if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
1197         loader_extensions.push_back({
1198                 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1199                 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1200     }
1201 
1202     // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1203     // support is provided by the driver
1204     VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1205         swapchainCompFeats = {};
1206     swapchainCompFeats.sType =
1207         VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1208     swapchainCompFeats.pNext = nullptr;
1209     swapchainCompFeats.imageCompressionControlSwapchain = false;
1210     VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1211     imageCompFeats.sType =
1212         VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1213     imageCompFeats.pNext = &swapchainCompFeats;
1214     imageCompFeats.imageCompressionControl = false;
1215 
1216     VkPhysicalDeviceFeatures2 feats2 = {};
1217     feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1218     feats2.pNext = &imageCompFeats;
1219 
1220     const auto& driver = GetData(physicalDevice).driver;
1221     if (driver.GetPhysicalDeviceFeatures2 ||
1222         driver.GetPhysicalDeviceFeatures2KHR) {
1223         GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1224     }
1225 
1226     bool anb9 = false;
1227     VkResult result =
1228         GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1229 
1230     if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1231         return result;
1232     }
1233 
1234     if (anb9 && imageCompFeats.imageCompressionControl) {
1235         loader_extensions.push_back(
1236             {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1237              VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1238     }
1239     if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1240         loader_extensions.push_back(
1241             {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1242              VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1243     }
1244 
1245     if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1246         loader_extensions.push_back({
1247                 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1248                 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1249     }
1250 
1251     // enumerate our extensions first
1252     if (!pLayerName && pProperties) {
1253         uint32_t count = std::min(
1254             *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1255 
1256         std::copy_n(loader_extensions.data(), count, pProperties);
1257 
1258         if (count < loader_extensions.size()) {
1259             *pPropertyCount = count;
1260             return VK_INCOMPLETE;
1261         }
1262 
1263         pProperties += count;
1264         *pPropertyCount -= count;
1265     }
1266 
1267     ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1268     result = data.driver.EnumerateDeviceExtensionProperties(
1269         physicalDevice, pLayerName, pPropertyCount, pProperties);
1270     ATRACE_END();
1271 
1272     if (pProperties) {
1273         // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1274         for (uint32_t i = 0; i < *pPropertyCount; i++) {
1275             auto& prop = pProperties[i];
1276 
1277             if (strcmp(prop.extensionName,
1278                        VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1279                 continue;
1280 
1281             memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1282                    sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
1283 
1284             if (prop.specVersion >= 8) {
1285                 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1286             } else {
1287                 prop.specVersion = 68;
1288             }
1289         }
1290     }
1291 
1292     // restore loader extension count
1293     if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1294         *pPropertyCount += loader_extensions.size();
1295     }
1296 
1297     return result;
1298 }
1299 
CreateInstance(const VkInstanceCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkInstance * pInstance)1300 VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1301                         const VkAllocationCallbacks* pAllocator,
1302                         VkInstance* pInstance) {
1303     const VkAllocationCallbacks& data_allocator =
1304         (pAllocator) ? *pAllocator : GetDefaultAllocator();
1305 
1306     VkResult result = VK_SUCCESS;
1307     uint32_t icd_api_version = VK_API_VERSION_1_0;
1308     PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1309         reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1310             Hal::Device().GetInstanceProcAddr(nullptr,
1311                                               "vkEnumerateInstanceVersion"));
1312     if (pfn_enumerate_instance_version) {
1313         ATRACE_BEGIN("pfn_enumerate_instance_version");
1314         result = (*pfn_enumerate_instance_version)(&icd_api_version);
1315         ATRACE_END();
1316         if (result != VK_SUCCESS)
1317             return result;
1318 
1319         icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
1320     }
1321 
1322     CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1323     result = wrapper.Validate();
1324     if (result != VK_SUCCESS)
1325         return result;
1326 
1327     InstanceData* data = AllocateInstanceData(data_allocator);
1328     if (!data)
1329         return VK_ERROR_OUT_OF_HOST_MEMORY;
1330 
1331     data->hook_extensions |= wrapper.GetHookExtensions();
1332 
1333     // call into the driver
1334     VkInstance instance;
1335     ATRACE_BEGIN("driver.CreateInstance");
1336     result = Hal::Device().CreateInstance(
1337         static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1338         &instance);
1339     ATRACE_END();
1340     if (result != VK_SUCCESS) {
1341         FreeInstanceData(data, data_allocator);
1342         return result;
1343     }
1344 
1345     // initialize InstanceDriverTable
1346     if (!SetData(instance, *data) ||
1347         !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
1348                          wrapper.GetHalExtensions())) {
1349         data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
1350             Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
1351         if (data->driver.DestroyInstance)
1352             data->driver.DestroyInstance(instance, pAllocator);
1353 
1354         FreeInstanceData(data, data_allocator);
1355 
1356         return VK_ERROR_INCOMPATIBLE_DRIVER;
1357     }
1358 
1359     data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
1360         Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
1361     if (!data->get_device_proc_addr) {
1362         data->driver.DestroyInstance(instance, pAllocator);
1363         FreeInstanceData(data, data_allocator);
1364 
1365         return VK_ERROR_INCOMPATIBLE_DRIVER;
1366     }
1367 
1368     // TODO(b/259516419) avoid getting stats from hwui
1369     // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1370     //         || (strcmp("android framework",
1371     //         pCreateInfo->pApplicationInfo->pEngineName) != 0);
1372     const bool reportStats = true;
1373     if (reportStats) {
1374         // Set stats for Vulkan api version requested with application info
1375         if (pCreateInfo->pApplicationInfo) {
1376             const uint32_t vulkanApiVersion =
1377                 pCreateInfo->pApplicationInfo->apiVersion;
1378             android::GraphicsEnv::getInstance().setTargetStats(
1379                 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1380                 vulkanApiVersion);
1381 
1382             if (pCreateInfo->pApplicationInfo->pEngineName) {
1383                 android::GraphicsEnv::getInstance().addVulkanEngineName(
1384                     pCreateInfo->pApplicationInfo->pEngineName);
1385             }
1386         }
1387 
1388         // Update stats for the extensions requested
1389         android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1390             pCreateInfo->enabledExtensionCount,
1391             pCreateInfo->ppEnabledExtensionNames);
1392     }
1393 
1394     *pInstance = instance;
1395 
1396     return VK_SUCCESS;
1397 }
1398 
DestroyInstance(VkInstance instance,const VkAllocationCallbacks * pAllocator)1399 void DestroyInstance(VkInstance instance,
1400                      const VkAllocationCallbacks* pAllocator) {
1401     InstanceData& data = GetData(instance);
1402     data.driver.DestroyInstance(instance, pAllocator);
1403 
1404     VkAllocationCallbacks local_allocator;
1405     if (!pAllocator) {
1406         local_allocator = data.allocator;
1407         pAllocator = &local_allocator;
1408     }
1409 
1410     FreeInstanceData(&data, *pAllocator);
1411 }
1412 
CreateDevice(VkPhysicalDevice physicalDevice,const VkDeviceCreateInfo * pCreateInfo,const VkAllocationCallbacks * pAllocator,VkDevice * pDevice)1413 VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1414                       const VkDeviceCreateInfo* pCreateInfo,
1415                       const VkAllocationCallbacks* pAllocator,
1416                       VkDevice* pDevice) {
1417     const InstanceData& instance_data = GetData(physicalDevice);
1418     const VkAllocationCallbacks& data_allocator =
1419         (pAllocator) ? *pAllocator : instance_data.allocator;
1420 
1421     VkPhysicalDeviceProperties properties;
1422     ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1423     instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1424                                                      &properties);
1425     ATRACE_END();
1426 
1427     CreateInfoWrapper wrapper(
1428         physicalDevice, *pCreateInfo,
1429         properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
1430         data_allocator);
1431     VkResult result = wrapper.Validate();
1432     if (result != VK_SUCCESS)
1433         return result;
1434 
1435     ATRACE_BEGIN("AllocateDeviceData");
1436     DeviceData* data = AllocateDeviceData(data_allocator,
1437                                           instance_data.debug_report_callbacks);
1438     ATRACE_END();
1439     if (!data)
1440         return VK_ERROR_OUT_OF_HOST_MEMORY;
1441 
1442     data->hook_extensions |= wrapper.GetHookExtensions();
1443 
1444     // call into the driver
1445     VkDevice dev;
1446     ATRACE_BEGIN("driver.CreateDevice");
1447     result = instance_data.driver.CreateDevice(
1448         physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1449         pAllocator, &dev);
1450     ATRACE_END();
1451     if (result != VK_SUCCESS) {
1452         FreeDeviceData(data, data_allocator);
1453         return result;
1454     }
1455 
1456     // initialize DeviceDriverTable
1457     if (!SetData(dev, *data) ||
1458         !InitDriverTable(dev, instance_data.get_device_proc_addr,
1459                          wrapper.GetHalExtensions())) {
1460         data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1461             instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1462         if (data->driver.DestroyDevice)
1463             data->driver.DestroyDevice(dev, pAllocator);
1464 
1465         FreeDeviceData(data, data_allocator);
1466 
1467         return VK_ERROR_INCOMPATIBLE_DRIVER;
1468     }
1469 
1470     // Confirming ANDROID_native_buffer implementation, whose set of
1471     // entrypoints varies according to the spec version.
1472     if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1473         !data->driver.GetSwapchainGrallocUsageANDROID &&
1474         !data->driver.GetSwapchainGrallocUsage2ANDROID &&
1475         !data->driver.GetSwapchainGrallocUsage3ANDROID &&
1476         !data->driver.GetSwapchainGrallocUsage4ANDROID) {
1477         ALOGE(
1478             "Driver's implementation of ANDROID_native_buffer is broken;"
1479             " must expose at least one of "
1480             "vkGetSwapchainGrallocUsageANDROID or "
1481             "vkGetSwapchainGrallocUsage2ANDROID or "
1482             "vkGetSwapchainGrallocUsage3ANDROID or "
1483             "vkGetSwapchainGrallocUsage4ANDROID");
1484 
1485         data->driver.DestroyDevice(dev, pAllocator);
1486         FreeDeviceData(data, data_allocator);
1487 
1488         return VK_ERROR_INCOMPATIBLE_DRIVER;
1489     }
1490 
1491     if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1492         // Log that the app is hitting software Vulkan implementation
1493         android::GraphicsEnv::getInstance().setTargetStats(
1494             android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1495     }
1496 
1497     data->driver_device = dev;
1498     data->driver_physical_device = physicalDevice;
1499 
1500     *pDevice = dev;
1501 
1502     // TODO(b/259516419) avoid getting stats from hwui
1503     const bool reportStats = true;
1504     if (reportStats) {
1505         android::GraphicsEnv::getInstance().setTargetStats(
1506             android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1507 
1508         // Set stats for creating a Vulkan device and report features in use
1509         const VkPhysicalDeviceFeatures* pEnabledFeatures =
1510             pCreateInfo->pEnabledFeatures;
1511         if (!pEnabledFeatures) {
1512             // Use features from the chained VkPhysicalDeviceFeatures2
1513             // structure, if given
1514             const VkPhysicalDeviceFeatures2* features2 =
1515                 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1516                     pCreateInfo->pNext);
1517             while (features2 &&
1518                    features2->sType !=
1519                        VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1520                 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1521                     features2->pNext);
1522             }
1523             if (features2) {
1524                 pEnabledFeatures = &features2->features;
1525             }
1526         }
1527         const VkBool32* pFeatures =
1528             reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1529         if (pFeatures) {
1530             // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1531             // of them using pointer arithmetic here and save the features in a
1532             // 64-bit bitfield
1533             static_assert(
1534                 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1535                 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1536                 "packing");
1537             static_assert(
1538                 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1539                 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1540                 "packing");
1541             const int numFeatures =
1542                 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1543 
1544             uint64_t enableFeatureBits = 0;
1545             for (int i = 0; i < numFeatures; i++) {
1546                 if (pFeatures[i] != VK_FALSE) {
1547                     enableFeatureBits |= (uint64_t(1) << i);
1548                 }
1549             }
1550             android::GraphicsEnv::getInstance().setTargetStats(
1551                 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1552                 enableFeatureBits);
1553         }
1554 
1555         // Update stats for the extensions requested
1556         android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1557             pCreateInfo->enabledExtensionCount,
1558             pCreateInfo->ppEnabledExtensionNames);
1559     }
1560 
1561     return VK_SUCCESS;
1562 }
1563 
DestroyDevice(VkDevice device,const VkAllocationCallbacks * pAllocator)1564 void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1565     DeviceData& data = GetData(device);
1566     data.driver.DestroyDevice(device, pAllocator);
1567 
1568     VkAllocationCallbacks local_allocator;
1569     if (!pAllocator) {
1570         local_allocator = data.allocator;
1571         pAllocator = &local_allocator;
1572     }
1573 
1574     FreeDeviceData(&data, *pAllocator);
1575 }
1576 
EnumeratePhysicalDevices(VkInstance instance,uint32_t * pPhysicalDeviceCount,VkPhysicalDevice * pPhysicalDevices)1577 VkResult EnumeratePhysicalDevices(VkInstance instance,
1578                                   uint32_t* pPhysicalDeviceCount,
1579                                   VkPhysicalDevice* pPhysicalDevices) {
1580     ATRACE_CALL();
1581 
1582     const auto& data = GetData(instance);
1583 
1584     VkResult result = data.driver.EnumeratePhysicalDevices(
1585         instance, pPhysicalDeviceCount, pPhysicalDevices);
1586     if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1587         for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1588             SetData(pPhysicalDevices[i], data);
1589     }
1590 
1591     return result;
1592 }
1593 
EnumeratePhysicalDeviceGroups(VkInstance instance,uint32_t * pPhysicalDeviceGroupCount,VkPhysicalDeviceGroupProperties * pPhysicalDeviceGroupProperties)1594 VkResult EnumeratePhysicalDeviceGroups(
1595     VkInstance instance,
1596     uint32_t* pPhysicalDeviceGroupCount,
1597     VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
1598     ATRACE_CALL();
1599 
1600     VkResult result = VK_SUCCESS;
1601     const auto& data = GetData(instance);
1602 
1603     if (!data.driver.EnumeratePhysicalDeviceGroups &&
1604         !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
1605         uint32_t device_count = 0;
1606         result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1607         if (result < 0)
1608             return result;
1609 
1610         if (!pPhysicalDeviceGroupProperties) {
1611             *pPhysicalDeviceGroupCount = device_count;
1612             return result;
1613         }
1614 
1615         if (!device_count) {
1616             *pPhysicalDeviceGroupCount = 0;
1617             return result;
1618         }
1619         device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1620         if (!device_count)
1621             return VK_INCOMPLETE;
1622 
1623         std::vector<VkPhysicalDevice> devices(device_count);
1624         *pPhysicalDeviceGroupCount = device_count;
1625         result =
1626             EnumeratePhysicalDevices(instance, &device_count, devices.data());
1627         if (result < 0)
1628             return result;
1629 
1630         for (uint32_t i = 0; i < device_count; ++i) {
1631             pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1632             pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1633             pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1634         }
1635     } else {
1636         if (data.driver.EnumeratePhysicalDeviceGroups) {
1637             result = data.driver.EnumeratePhysicalDeviceGroups(
1638                 instance, pPhysicalDeviceGroupCount,
1639                 pPhysicalDeviceGroupProperties);
1640         } else {
1641             result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1642                 instance, pPhysicalDeviceGroupCount,
1643                 pPhysicalDeviceGroupProperties);
1644         }
1645         if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1646             *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1647             for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1648                 for (uint32_t j = 0;
1649                      j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1650                      j++) {
1651                     SetData(
1652                         pPhysicalDeviceGroupProperties[i].physicalDevices[j],
1653                         data);
1654                 }
1655             }
1656         }
1657     }
1658 
1659     return result;
1660 }
1661 
GetDeviceQueue(VkDevice device,uint32_t queueFamilyIndex,uint32_t queueIndex,VkQueue * pQueue)1662 void GetDeviceQueue(VkDevice device,
1663                     uint32_t queueFamilyIndex,
1664                     uint32_t queueIndex,
1665                     VkQueue* pQueue) {
1666     ATRACE_CALL();
1667 
1668     const auto& data = GetData(device);
1669 
1670     data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1671     SetData(*pQueue, data);
1672 }
1673 
GetDeviceQueue2(VkDevice device,const VkDeviceQueueInfo2 * pQueueInfo,VkQueue * pQueue)1674 void GetDeviceQueue2(VkDevice device,
1675                      const VkDeviceQueueInfo2* pQueueInfo,
1676                      VkQueue* pQueue) {
1677     ATRACE_CALL();
1678 
1679     const auto& data = GetData(device);
1680 
1681     data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
1682     if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
1683 }
1684 
AllocateCommandBuffers(VkDevice device,const VkCommandBufferAllocateInfo * pAllocateInfo,VkCommandBuffer * pCommandBuffers)1685 VkResult AllocateCommandBuffers(
1686     VkDevice device,
1687     const VkCommandBufferAllocateInfo* pAllocateInfo,
1688     VkCommandBuffer* pCommandBuffers) {
1689     ATRACE_CALL();
1690 
1691     const auto& data = GetData(device);
1692 
1693     VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1694                                                          pCommandBuffers);
1695     if (result == VK_SUCCESS) {
1696         for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1697             SetData(pCommandBuffers[i], data);
1698     }
1699 
1700     return result;
1701 }
1702 
QueueSubmit(VkQueue queue,uint32_t submitCount,const VkSubmitInfo * pSubmits,VkFence fence)1703 VkResult QueueSubmit(VkQueue queue,
1704                      uint32_t submitCount,
1705                      const VkSubmitInfo* pSubmits,
1706                      VkFence fence) {
1707     ATRACE_CALL();
1708 
1709     const auto& data = GetData(queue);
1710 
1711     return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1712 }
1713 
GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,VkPhysicalDeviceFeatures2 * pFeatures)1714 void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1715                                 VkPhysicalDeviceFeatures2* pFeatures) {
1716     ATRACE_CALL();
1717 
1718     const auto& driver = GetData(physicalDevice).driver;
1719 
1720     if (driver.GetPhysicalDeviceFeatures2) {
1721         driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
1722     } else {
1723         driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1724     }
1725 
1726     // Conditionally add imageCompressionControlSwapchain if
1727     // imageCompressionControl is supported Check for imageCompressionControl in
1728     // the pChain
1729     bool imageCompressionControl = false;
1730     bool imageCompressionControlInChain = false;
1731     bool imageCompressionControlSwapchainInChain = false;
1732     VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1733     while (pFeats) {
1734         switch (pFeats->sType) {
1735             case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1736                 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1737                     compressionFeat = reinterpret_cast<
1738                         const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1739                         pFeats);
1740                 imageCompressionControl =
1741                     compressionFeat->imageCompressionControl;
1742                 imageCompressionControlInChain = true;
1743             } break;
1744 
1745             case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1746                 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1747                     compressionFeat = reinterpret_cast<
1748                         VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1749                         pFeats);
1750                 compressionFeat->imageCompressionControlSwapchain = false;
1751                 imageCompressionControlSwapchainInChain = true;
1752             } break;
1753 
1754             case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1755                 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1756                         pFeats);
1757                 smf->swapchainMaintenance1 = true;
1758             } break;
1759 
1760             default:
1761                 break;
1762         }
1763         pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1764     }
1765 
1766     if (!imageCompressionControlSwapchainInChain) {
1767         return;
1768     }
1769 
1770     // If not in pchain, explicitly query for imageCompressionControl
1771     if (!imageCompressionControlInChain) {
1772         VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1773         imageCompFeats.sType =
1774             VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1775         imageCompFeats.pNext = nullptr;
1776         imageCompFeats.imageCompressionControl = false;
1777 
1778         VkPhysicalDeviceFeatures2 feats2 = {};
1779         feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1780         feats2.pNext = &imageCompFeats;
1781 
1782         if (driver.GetPhysicalDeviceFeatures2) {
1783             driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1784         } else {
1785             driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1786         }
1787 
1788         imageCompressionControl = imageCompFeats.imageCompressionControl;
1789     }
1790 
1791     // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1792     if (imageCompressionControl) {
1793         pFeats = pFeatures;
1794         while (pFeats) {
1795             switch (pFeats->sType) {
1796                 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1797                     VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1798                         compressionFeat = reinterpret_cast<
1799                             VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1800                             pFeats);
1801                     compressionFeat->imageCompressionControlSwapchain = true;
1802                 } break;
1803 
1804                 default:
1805                     break;
1806             }
1807             pFeats =
1808                 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1809         }
1810     }
1811 }
1812 
GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,VkPhysicalDeviceProperties2 * pProperties)1813 void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1814                                   VkPhysicalDeviceProperties2* pProperties) {
1815     ATRACE_CALL();
1816 
1817     const auto& driver = GetData(physicalDevice).driver;
1818 
1819     if (driver.GetPhysicalDeviceProperties2) {
1820         driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1821         return;
1822     }
1823 
1824     driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1825 }
1826 
GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physicalDevice,VkFormat format,VkFormatProperties2 * pFormatProperties)1827 void GetPhysicalDeviceFormatProperties2(
1828     VkPhysicalDevice physicalDevice,
1829     VkFormat format,
1830     VkFormatProperties2* pFormatProperties) {
1831     ATRACE_CALL();
1832 
1833     const auto& driver = GetData(physicalDevice).driver;
1834 
1835     if (driver.GetPhysicalDeviceFormatProperties2) {
1836         driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1837                                                   pFormatProperties);
1838         return;
1839     }
1840 
1841     driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1842                                                  pFormatProperties);
1843 }
1844 
GetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceImageFormatInfo2 * pImageFormatInfo,VkImageFormatProperties2 * pImageFormatProperties)1845 VkResult GetPhysicalDeviceImageFormatProperties2(
1846     VkPhysicalDevice physicalDevice,
1847     const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1848     VkImageFormatProperties2* pImageFormatProperties) {
1849     ATRACE_CALL();
1850 
1851     const auto& driver = GetData(physicalDevice).driver;
1852 
1853     if (driver.GetPhysicalDeviceImageFormatProperties2) {
1854         return driver.GetPhysicalDeviceImageFormatProperties2(
1855             physicalDevice, pImageFormatInfo, pImageFormatProperties);
1856     }
1857 
1858     return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1859         physicalDevice, pImageFormatInfo, pImageFormatProperties);
1860 }
1861 
GetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice physicalDevice,uint32_t * pQueueFamilyPropertyCount,VkQueueFamilyProperties2 * pQueueFamilyProperties)1862 void GetPhysicalDeviceQueueFamilyProperties2(
1863     VkPhysicalDevice physicalDevice,
1864     uint32_t* pQueueFamilyPropertyCount,
1865     VkQueueFamilyProperties2* pQueueFamilyProperties) {
1866     ATRACE_CALL();
1867 
1868     const auto& driver = GetData(physicalDevice).driver;
1869 
1870     if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1871         driver.GetPhysicalDeviceQueueFamilyProperties2(
1872             physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1873         return;
1874     }
1875 
1876     driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1877         physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1878 }
1879 
GetPhysicalDeviceMemoryProperties2(VkPhysicalDevice physicalDevice,VkPhysicalDeviceMemoryProperties2 * pMemoryProperties)1880 void GetPhysicalDeviceMemoryProperties2(
1881     VkPhysicalDevice physicalDevice,
1882     VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1883     ATRACE_CALL();
1884 
1885     const auto& driver = GetData(physicalDevice).driver;
1886 
1887     if (driver.GetPhysicalDeviceMemoryProperties2) {
1888         driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1889                                                   pMemoryProperties);
1890         return;
1891     }
1892 
1893     driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1894                                                  pMemoryProperties);
1895 }
1896 
GetPhysicalDeviceSparseImageFormatProperties2(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceSparseImageFormatInfo2 * pFormatInfo,uint32_t * pPropertyCount,VkSparseImageFormatProperties2 * pProperties)1897 void GetPhysicalDeviceSparseImageFormatProperties2(
1898     VkPhysicalDevice physicalDevice,
1899     const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1900     uint32_t* pPropertyCount,
1901     VkSparseImageFormatProperties2* pProperties) {
1902     ATRACE_CALL();
1903 
1904     const auto& driver = GetData(physicalDevice).driver;
1905 
1906     if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1907         driver.GetPhysicalDeviceSparseImageFormatProperties2(
1908             physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1909         return;
1910     }
1911 
1912     driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1913         physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1914 }
1915 
GetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalBufferInfo * pExternalBufferInfo,VkExternalBufferProperties * pExternalBufferProperties)1916 void GetPhysicalDeviceExternalBufferProperties(
1917     VkPhysicalDevice physicalDevice,
1918     const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1919     VkExternalBufferProperties* pExternalBufferProperties) {
1920     ATRACE_CALL();
1921 
1922     const auto& driver = GetData(physicalDevice).driver;
1923 
1924     if (driver.GetPhysicalDeviceExternalBufferProperties) {
1925         driver.GetPhysicalDeviceExternalBufferProperties(
1926             physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1927         return;
1928     }
1929 
1930     if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1931         driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1932             physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1933         return;
1934     }
1935 
1936     memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1937            sizeof(VkExternalMemoryProperties));
1938 }
1939 
GetPhysicalDeviceExternalSemaphoreProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalSemaphoreInfo * pExternalSemaphoreInfo,VkExternalSemaphoreProperties * pExternalSemaphoreProperties)1940 void GetPhysicalDeviceExternalSemaphoreProperties(
1941     VkPhysicalDevice physicalDevice,
1942     const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1943     VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1944     ATRACE_CALL();
1945 
1946     const auto& driver = GetData(physicalDevice).driver;
1947 
1948     if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1949         driver.GetPhysicalDeviceExternalSemaphoreProperties(
1950             physicalDevice, pExternalSemaphoreInfo,
1951             pExternalSemaphoreProperties);
1952         return;
1953     }
1954 
1955     if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1956         driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1957             physicalDevice, pExternalSemaphoreInfo,
1958             pExternalSemaphoreProperties);
1959         return;
1960     }
1961 
1962     pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1963     pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1964     pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1965 }
1966 
GetPhysicalDeviceExternalFenceProperties(VkPhysicalDevice physicalDevice,const VkPhysicalDeviceExternalFenceInfo * pExternalFenceInfo,VkExternalFenceProperties * pExternalFenceProperties)1967 void GetPhysicalDeviceExternalFenceProperties(
1968     VkPhysicalDevice physicalDevice,
1969     const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1970     VkExternalFenceProperties* pExternalFenceProperties) {
1971     ATRACE_CALL();
1972 
1973     const auto& driver = GetData(physicalDevice).driver;
1974 
1975     if (driver.GetPhysicalDeviceExternalFenceProperties) {
1976         driver.GetPhysicalDeviceExternalFenceProperties(
1977             physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1978         return;
1979     }
1980 
1981     if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1982         driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1983             physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1984         return;
1985     }
1986 
1987     pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1988     pExternalFenceProperties->compatibleHandleTypes = 0;
1989     pExternalFenceProperties->externalFenceFeatures = 0;
1990 }
1991 
1992 }  // namespace driver
1993 }  // namespace vulkan
1994