| Chia-I Wu | 9d51816 | 2016-03-24 14:55:27 +0800 | [diff] [blame] | 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 |  | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 18 |  | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 19 | #include "driver.h" | 
|  | 20 |  | 
|  | 21 | #include <dlfcn.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 22 | #include <malloc.h> | 
| Chia-I Wu | dbb7e9c | 2016-03-24 15:09:38 +0800 | [diff] [blame] | 23 | #include <stdlib.h> | 
|  | 24 | #include <string.h> | 
| Chia-I Wu | 9d51816 | 2016-03-24 14:55:27 +0800 | [diff] [blame] | 25 |  | 
| Sundong Ahn | bc37dd5 | 2020-04-23 21:21:00 +0900 | [diff] [blame] | 26 | #include <SurfaceFlingerProperties.h> | 
|  | 27 | #include <android-base/properties.h> | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 28 | #include <android/dlext.h> | 
| Courtney Goeltzenleuchter | 7671d46 | 2018-01-24 11:51:01 -0800 | [diff] [blame] | 29 | #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h> | 
|  | 30 | #include <configstore/Utils.h> | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 31 | #include <cutils/properties.h> | 
| Jiyong Park | 27c39e1 | 2017-05-08 13:00:02 +0900 | [diff] [blame] | 32 | #include <graphicsenv/GraphicsEnv.h> | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 33 | #include <log/log.h> | 
| Yiwei Zhang | e40dd73 | 2019-08-05 16:41:03 -0700 | [diff] [blame] | 34 | #include <nativeloader/dlext_namespaces.h> | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 35 | #include <sys/prctl.h> | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 36 | #include <utils/Timers.h> | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 37 | #include <utils/Trace.h> | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 38 |  | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 39 | #include <algorithm> | 
|  | 40 | #include <array> | 
| Nick Desaulniers | 7c123cc | 2019-10-21 13:52:41 -0700 | [diff] [blame] | 41 | #include <climits> | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 42 | #include <new> | 
| Nick Desaulniers | 7c123cc | 2019-10-21 13:52:41 -0700 | [diff] [blame] | 43 | #include <string_view> | 
|  | 44 | #include <sstream> | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 45 | #include <vector> | 
| Wei Wang | f9b05ee | 2017-07-19 20:59:39 -0700 | [diff] [blame] | 46 |  | 
| Jesse Hall | b7c4e3b | 2016-04-11 13:51:38 -0700 | [diff] [blame] | 47 | #include "stubhal.h" | 
| Chia-I Wu | 9d51816 | 2016-03-24 14:55:27 +0800 | [diff] [blame] | 48 |  | 
| Courtney Goeltzenleuchter | 7671d46 | 2018-01-24 11:51:01 -0800 | [diff] [blame] | 49 | using namespace android::hardware::configstore; | 
|  | 50 | using namespace android::hardware::configstore::V1_0; | 
|  | 51 |  | 
| Chia-I Wu | dbb7e9c | 2016-03-24 15:09:38 +0800 | [diff] [blame] | 52 | // #define ENABLE_ALLOC_CALLSTACKS 1 | 
|  | 53 | #if ENABLE_ALLOC_CALLSTACKS | 
|  | 54 | #include <utils/CallStack.h> | 
|  | 55 | #define ALOGD_CALLSTACK(...)                             \ | 
|  | 56 | do {                                                 \ | 
|  | 57 | ALOGD(__VA_ARGS__);                              \ | 
|  | 58 | android::CallStack callstack;                    \ | 
|  | 59 | callstack.update();                              \ | 
|  | 60 | callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, "  "); \ | 
|  | 61 | } while (false) | 
|  | 62 | #else | 
|  | 63 | #define ALOGD_CALLSTACK(...) \ | 
|  | 64 | do {                     \ | 
|  | 65 | } while (false) | 
|  | 66 | #endif | 
|  | 67 |  | 
| Chia-I Wu | 9d51816 | 2016-03-24 14:55:27 +0800 | [diff] [blame] | 68 | namespace vulkan { | 
|  | 69 | namespace driver { | 
|  | 70 |  | 
| Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 71 | namespace { | 
|  | 72 |  | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 73 | class Hal { | 
|  | 74 | public: | 
|  | 75 | static bool Open(); | 
|  | 76 |  | 
|  | 77 | static const Hal& Get() { return hal_; } | 
|  | 78 | static const hwvulkan_device_t& Device() { return *Get().dev_; } | 
|  | 79 |  | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 80 | int GetDebugReportIndex() const { return debug_report_index_; } | 
|  | 81 |  | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 82 | private: | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 83 | Hal() : dev_(nullptr), debug_report_index_(-1) {} | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 84 | Hal(const Hal&) = delete; | 
|  | 85 | Hal& operator=(const Hal&) = delete; | 
|  | 86 |  | 
| Yiwei Zhang | 901f8ee | 2020-07-31 13:18:49 -0700 | [diff] [blame] | 87 | bool ShouldUnloadBuiltinDriver(); | 
|  | 88 | void UnloadBuiltinDriver(); | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 89 | bool InitDebugReportIndex(); | 
|  | 90 |  | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 91 | static Hal hal_; | 
|  | 92 |  | 
|  | 93 | const hwvulkan_device_t* dev_; | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 94 | int debug_report_index_; | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 95 | }; | 
|  | 96 |  | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 97 | class CreateInfoWrapper { | 
|  | 98 | public: | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 99 | CreateInfoWrapper(const VkInstanceCreateInfo& create_info, | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 100 | uint32_t icd_api_version, | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 101 | const VkAllocationCallbacks& allocator); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 102 | CreateInfoWrapper(VkPhysicalDevice physical_dev, | 
|  | 103 | const VkDeviceCreateInfo& create_info, | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 104 | uint32_t icd_api_version, | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 105 | const VkAllocationCallbacks& allocator); | 
|  | 106 | ~CreateInfoWrapper(); | 
|  | 107 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 108 | VkResult Validate(); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 109 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 110 | const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const; | 
|  | 111 | const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const; | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 112 |  | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 113 | explicit operator const VkInstanceCreateInfo*() const; | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 114 | explicit operator const VkDeviceCreateInfo*() const; | 
|  | 115 |  | 
|  | 116 | private: | 
|  | 117 | struct ExtensionFilter { | 
|  | 118 | VkExtensionProperties* exts; | 
|  | 119 | uint32_t ext_count; | 
|  | 120 |  | 
|  | 121 | const char** names; | 
|  | 122 | uint32_t name_count; | 
| Yiwei Zhang | d7ea44a | 2020-08-13 12:54:27 -0700 | [diff] [blame] | 123 | ExtensionFilter() | 
|  | 124 | : exts(nullptr), ext_count(0), names(nullptr), name_count(0) {} | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 125 | }; | 
|  | 126 |  | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 127 | VkResult SanitizeApiVersion(); | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 128 | VkResult SanitizePNext(); | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 129 | VkResult SanitizeLayers(); | 
|  | 130 | VkResult SanitizeExtensions(); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 131 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 132 | VkResult QueryExtensionCount(uint32_t& count) const; | 
|  | 133 | VkResult EnumerateExtensions(uint32_t& count, | 
|  | 134 | VkExtensionProperties* props) const; | 
|  | 135 | VkResult InitExtensionFilter(); | 
|  | 136 | void FilterExtension(const char* name); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 137 |  | 
|  | 138 | const bool is_instance_; | 
|  | 139 | const VkAllocationCallbacks& allocator_; | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 140 | const uint32_t loader_api_version_; | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 141 | const uint32_t icd_api_version_; | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 142 |  | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 143 | VkPhysicalDevice physical_dev_; | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 144 |  | 
|  | 145 | union { | 
|  | 146 | VkInstanceCreateInfo instance_info_; | 
|  | 147 | VkDeviceCreateInfo dev_info_; | 
|  | 148 | }; | 
|  | 149 |  | 
| Ian Elliott | f3e872d | 2017-11-02 10:15:13 -0600 | [diff] [blame] | 150 | VkApplicationInfo application_info_; | 
|  | 151 |  | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 152 | ExtensionFilter extension_filter_; | 
|  | 153 |  | 
|  | 154 | std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_; | 
|  | 155 | std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_; | 
|  | 156 | }; | 
|  | 157 |  | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 158 | Hal Hal::hal_; | 
|  | 159 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 160 | void* LoadLibrary(const android_dlextinfo& dlextinfo, | 
| Nick Desaulniers | 60307ce | 2019-10-25 13:34:21 -0700 | [diff] [blame] | 161 | const std::string_view subname) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 162 | ATRACE_CALL(); | 
|  | 163 |  | 
| Nick Desaulniers | 60307ce | 2019-10-25 13:34:21 -0700 | [diff] [blame] | 164 | std::stringstream ss; | 
|  | 165 | ss << "vulkan." << subname << ".so"; | 
|  | 166 | return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo); | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 167 | } | 
|  | 168 |  | 
|  | 169 | const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{ | 
| Peter Collingbourne | 161c76b | 2020-04-22 13:12:23 -0700 | [diff] [blame] | 170 | "ro.hardware.vulkan", | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 171 | "ro.board.platform", | 
|  | 172 | }}; | 
|  | 173 |  | 
| Peiyong Lin | efa0cbd | 2020-01-29 20:51:50 -0800 | [diff] [blame] | 174 | // LoadDriver returns: | 
|  | 175 | // * 0 when succeed, or | 
|  | 176 | // * -ENOENT when fail to open binary libraries, or | 
|  | 177 | // * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or | 
|  | 178 | //   HWVULKAN_HARDWARE_MODULE_ID in the library. | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 179 | int LoadDriver(android_namespace_t* library_namespace, | 
|  | 180 | const hwvulkan_module_t** module) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 181 | ATRACE_CALL(); | 
|  | 182 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 183 | const android_dlextinfo dlextinfo = { | 
|  | 184 | .flags = ANDROID_DLEXT_USE_NAMESPACE, | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 185 | .library_namespace = library_namespace, | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 186 | }; | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 187 | void* so = nullptr; | 
|  | 188 | char prop[PROPERTY_VALUE_MAX]; | 
|  | 189 | for (auto key : HAL_SUBNAME_KEY_PROPERTIES) { | 
|  | 190 | int prop_len = property_get(key, prop, nullptr); | 
| Nick Desaulniers | 60307ce | 2019-10-25 13:34:21 -0700 | [diff] [blame] | 191 | if (prop_len > 0 && prop_len <= UINT_MAX) { | 
|  | 192 | std::string_view lib_name(prop, static_cast<unsigned int>(prop_len)); | 
|  | 193 | so = LoadLibrary(dlextinfo, lib_name); | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 194 | if (so) | 
|  | 195 | break; | 
|  | 196 | } | 
|  | 197 | } | 
|  | 198 | if (!so) | 
|  | 199 | return -ENOENT; | 
|  | 200 |  | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 201 | auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR)); | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 202 | if (!hmi) { | 
|  | 203 | ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror()); | 
|  | 204 | dlclose(so); | 
|  | 205 | return -EINVAL; | 
|  | 206 | } | 
|  | 207 | if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) { | 
|  | 208 | ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID); | 
|  | 209 | dlclose(so); | 
|  | 210 | return -EINVAL; | 
|  | 211 | } | 
|  | 212 | hmi->dso = so; | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 213 | *module = reinterpret_cast<const hwvulkan_module_t*>(hmi); | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 214 | return 0; | 
|  | 215 | } | 
|  | 216 |  | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 217 | int LoadBuiltinDriver(const hwvulkan_module_t** module) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 218 | ATRACE_CALL(); | 
|  | 219 |  | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 220 | auto ns = android_get_exported_namespace("sphal"); | 
|  | 221 | if (!ns) | 
|  | 222 | return -ENOENT; | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 223 | android::GraphicsEnv::getInstance().setDriverToLoad( | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 224 | android::GpuStatsInfo::Driver::VULKAN); | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 225 | return LoadDriver(ns, module); | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | int LoadUpdatedDriver(const hwvulkan_module_t** module) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 229 | ATRACE_CALL(); | 
|  | 230 |  | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 231 | auto ns = android::GraphicsEnv::getInstance().getDriverNamespace(); | 
|  | 232 | if (!ns) | 
|  | 233 | return -ENOENT; | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 234 | android::GraphicsEnv::getInstance().setDriverToLoad( | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 235 | android::GpuStatsInfo::Driver::VULKAN_UPDATED); | 
| Peiyong Lin | efa0cbd | 2020-01-29 20:51:50 -0800 | [diff] [blame] | 236 | int result = LoadDriver(ns, module); | 
|  | 237 | if (result != 0) { | 
|  | 238 | LOG_ALWAYS_FATAL( | 
|  | 239 | "couldn't find an updated Vulkan implementation from %s", | 
|  | 240 | android::GraphicsEnv::getInstance().getDriverPath().c_str()); | 
|  | 241 | } | 
|  | 242 | return result; | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 245 | bool Hal::Open() { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 246 | ATRACE_CALL(); | 
|  | 247 |  | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 248 | const nsecs_t openTime = systemTime(); | 
|  | 249 |  | 
| Yiwei Zhang | 901f8ee | 2020-07-31 13:18:49 -0700 | [diff] [blame] | 250 | if (hal_.ShouldUnloadBuiltinDriver()) { | 
|  | 251 | hal_.UnloadBuiltinDriver(); | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | if (hal_.dev_) | 
|  | 255 | return true; | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 256 |  | 
|  | 257 | // Use a stub device unless we successfully open a real HAL device. | 
|  | 258 | hal_.dev_ = &stubhal::kDevice; | 
|  | 259 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 260 | int result; | 
|  | 261 | const hwvulkan_module_t* module = nullptr; | 
|  | 262 |  | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 263 | result = LoadUpdatedDriver(&module); | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 264 | if (result == -ENOENT) { | 
| Jesse Hall | 00e61ff | 2017-04-07 16:48:02 -0700 | [diff] [blame] | 265 | result = LoadBuiltinDriver(&module); | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 266 | } | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 267 | if (result != 0) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 268 | android::GraphicsEnv::getInstance().setDriverLoaded( | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 269 | android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime); | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 270 | ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result); | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 271 | return true; | 
|  | 272 | } | 
|  | 273 |  | 
| Yiwei Zhang | cb9d4e4 | 2019-02-06 20:22:59 -0800 | [diff] [blame] | 274 |  | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 275 | hwvulkan_device_t* device; | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 276 | ATRACE_BEGIN("hwvulkan module open"); | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 277 | result = | 
|  | 278 | module->common.methods->open(&module->common, HWVULKAN_DEVICE_0, | 
|  | 279 | reinterpret_cast<hw_device_t**>(&device)); | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 280 | ATRACE_END(); | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 281 | if (result != 0) { | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 282 | android::GraphicsEnv::getInstance().setDriverLoaded( | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 283 | android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime); | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 284 | // Any device with a Vulkan HAL should be able to open the device. | 
|  | 285 | ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result), | 
|  | 286 | result); | 
|  | 287 | return false; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | hal_.dev_ = device; | 
|  | 291 |  | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 292 | hal_.InitDebugReportIndex(); | 
|  | 293 |  | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 294 | android::GraphicsEnv::getInstance().setDriverLoaded( | 
| Yiwei Zhang | 27ab3ac | 2019-07-02 18:10:55 -0700 | [diff] [blame] | 295 | android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime); | 
| Yiwei Zhang | d986181 | 2019-02-13 11:51:55 -0800 | [diff] [blame] | 296 |  | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 297 | return true; | 
|  | 298 | } | 
|  | 299 |  | 
| Yiwei Zhang | 901f8ee | 2020-07-31 13:18:49 -0700 | [diff] [blame] | 300 | bool Hal::ShouldUnloadBuiltinDriver() { | 
|  | 301 | // Should not unload since the driver was not loaded | 
|  | 302 | if (!hal_.dev_) | 
|  | 303 | return false; | 
|  | 304 |  | 
|  | 305 | // Should not unload if stubhal is used on the device | 
|  | 306 | if (hal_.dev_ == &stubhal::kDevice) | 
|  | 307 | return false; | 
|  | 308 |  | 
|  | 309 | // Unload the driver if updated driver is chosen | 
|  | 310 | if (android::GraphicsEnv::getInstance().getDriverNamespace()) | 
|  | 311 | return true; | 
|  | 312 |  | 
|  | 313 | return false; | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | void Hal::UnloadBuiltinDriver() { | 
|  | 317 | ATRACE_CALL(); | 
|  | 318 |  | 
|  | 319 | ALOGD("Unload builtin Vulkan driver."); | 
|  | 320 |  | 
|  | 321 | // Close the opened device | 
|  | 322 | ALOG_ASSERT(!hal_.dev_->common.close(hal_.dev_->common), | 
|  | 323 | "hw_device_t::close() failed."); | 
|  | 324 |  | 
|  | 325 | // Close the opened shared library in the hw_module_t | 
|  | 326 | dlclose(hal_.dev_->common.module->dso); | 
|  | 327 |  | 
|  | 328 | hal_.dev_ = nullptr; | 
|  | 329 | hal_.debug_report_index_ = -1; | 
|  | 330 | } | 
|  | 331 |  | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 332 | bool Hal::InitDebugReportIndex() { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 333 | ATRACE_CALL(); | 
|  | 334 |  | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 335 | uint32_t count; | 
|  | 336 | if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) != | 
|  | 337 | VK_SUCCESS) { | 
|  | 338 | ALOGE("failed to get HAL instance extension count"); | 
|  | 339 | return false; | 
|  | 340 | } | 
|  | 341 |  | 
|  | 342 | VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>( | 
|  | 343 | malloc(sizeof(VkExtensionProperties) * count)); | 
|  | 344 | if (!exts) { | 
|  | 345 | ALOGE("failed to allocate HAL instance extension array"); | 
|  | 346 | return false; | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) != | 
|  | 350 | VK_SUCCESS) { | 
|  | 351 | ALOGE("failed to enumerate HAL instance extensions"); | 
|  | 352 | free(exts); | 
|  | 353 | return false; | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | for (uint32_t i = 0; i < count; i++) { | 
|  | 357 | if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) == | 
|  | 358 | 0) { | 
|  | 359 | debug_report_index_ = static_cast<int>(i); | 
|  | 360 | break; | 
|  | 361 | } | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | free(exts); | 
|  | 365 |  | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 366 | return true; | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info, | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 370 | uint32_t icd_api_version, | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 371 | const VkAllocationCallbacks& allocator) | 
|  | 372 | : is_instance_(true), | 
|  | 373 | allocator_(allocator), | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 374 | loader_api_version_(VK_API_VERSION_1_1), | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 375 | icd_api_version_(icd_api_version), | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 376 | physical_dev_(VK_NULL_HANDLE), | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 377 | instance_info_(create_info), | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 378 | extension_filter_() {} | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 379 |  | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 380 | CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev, | 
|  | 381 | const VkDeviceCreateInfo& create_info, | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 382 | uint32_t icd_api_version, | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 383 | const VkAllocationCallbacks& allocator) | 
|  | 384 | : is_instance_(false), | 
|  | 385 | allocator_(allocator), | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 386 | loader_api_version_(VK_API_VERSION_1_1), | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 387 | icd_api_version_(icd_api_version), | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 388 | physical_dev_(physical_dev), | 
|  | 389 | dev_info_(create_info), | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 390 | extension_filter_() {} | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 391 |  | 
|  | 392 | CreateInfoWrapper::~CreateInfoWrapper() { | 
|  | 393 | allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts); | 
|  | 394 | allocator_.pfnFree(allocator_.pUserData, extension_filter_.names); | 
|  | 395 | } | 
|  | 396 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 397 | VkResult CreateInfoWrapper::Validate() { | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 398 | VkResult result = SanitizeApiVersion(); | 
|  | 399 | if (result == VK_SUCCESS) | 
|  | 400 | result = SanitizePNext(); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 401 | if (result == VK_SUCCESS) | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 402 | result = SanitizeLayers(); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 403 | if (result == VK_SUCCESS) | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 404 | result = SanitizeExtensions(); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 405 |  | 
|  | 406 | return result; | 
|  | 407 | } | 
|  | 408 |  | 
|  | 409 | const std::bitset<ProcHook::EXTENSION_COUNT>& | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 410 | CreateInfoWrapper::GetHookExtensions() const { | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 411 | return hook_extensions_; | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | const std::bitset<ProcHook::EXTENSION_COUNT>& | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 415 | CreateInfoWrapper::GetHalExtensions() const { | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 416 | return hal_extensions_; | 
|  | 417 | } | 
|  | 418 |  | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 419 | CreateInfoWrapper::operator const VkInstanceCreateInfo*() const { | 
|  | 420 | return &instance_info_; | 
|  | 421 | } | 
|  | 422 |  | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 423 | CreateInfoWrapper::operator const VkDeviceCreateInfo*() const { | 
|  | 424 | return &dev_info_; | 
|  | 425 | } | 
|  | 426 |  | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 427 | VkResult CreateInfoWrapper::SanitizeApiVersion() { | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 428 | if (!is_instance_ || !instance_info_.pApplicationInfo) | 
|  | 429 | return VK_SUCCESS; | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 430 |  | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 431 | if (icd_api_version_ > VK_API_VERSION_1_0 || | 
|  | 432 | instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1) | 
|  | 433 | return VK_SUCCESS; | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 434 |  | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 435 | // override apiVersion to avoid error return from 1.0 icd | 
|  | 436 | application_info_ = *instance_info_.pApplicationInfo; | 
|  | 437 | application_info_.apiVersion = VK_API_VERSION_1_0; | 
|  | 438 | instance_info_.pApplicationInfo = &application_info_; | 
| Yiwei Zhang | d4fd122 | 2020-07-03 22:18:42 -0700 | [diff] [blame] | 439 |  | 
|  | 440 | return VK_SUCCESS; | 
|  | 441 | } | 
|  | 442 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 443 | VkResult CreateInfoWrapper::SanitizePNext() { | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 444 | const struct StructHeader { | 
|  | 445 | VkStructureType type; | 
|  | 446 | const void* next; | 
|  | 447 | } * header; | 
|  | 448 |  | 
|  | 449 | if (is_instance_) { | 
|  | 450 | header = reinterpret_cast<const StructHeader*>(instance_info_.pNext); | 
|  | 451 |  | 
|  | 452 | // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs | 
|  | 453 | while (header && | 
|  | 454 | header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO) | 
|  | 455 | header = reinterpret_cast<const StructHeader*>(header->next); | 
|  | 456 |  | 
|  | 457 | instance_info_.pNext = header; | 
|  | 458 | } else { | 
|  | 459 | header = reinterpret_cast<const StructHeader*>(dev_info_.pNext); | 
|  | 460 |  | 
|  | 461 | // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs | 
|  | 462 | while (header && | 
|  | 463 | header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO) | 
|  | 464 | header = reinterpret_cast<const StructHeader*>(header->next); | 
|  | 465 |  | 
|  | 466 | dev_info_.pNext = header; | 
|  | 467 | } | 
|  | 468 |  | 
|  | 469 | return VK_SUCCESS; | 
|  | 470 | } | 
|  | 471 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 472 | VkResult CreateInfoWrapper::SanitizeLayers() { | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 473 | auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames | 
|  | 474 | : dev_info_.ppEnabledLayerNames; | 
|  | 475 | auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount | 
|  | 476 | : dev_info_.enabledLayerCount; | 
|  | 477 |  | 
|  | 478 | // remove all layers | 
|  | 479 | layer_names = nullptr; | 
|  | 480 | layer_count = 0; | 
|  | 481 |  | 
|  | 482 | return VK_SUCCESS; | 
|  | 483 | } | 
|  | 484 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 485 | VkResult CreateInfoWrapper::SanitizeExtensions() { | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 486 | auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames | 
|  | 487 | : dev_info_.ppEnabledExtensionNames; | 
|  | 488 | auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount | 
|  | 489 | : dev_info_.enabledExtensionCount; | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 490 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 491 | VkResult result = InitExtensionFilter(); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 492 | if (result != VK_SUCCESS) | 
|  | 493 | return result; | 
|  | 494 |  | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 495 | if (is_instance_ && icd_api_version_ < loader_api_version_) { | 
| Yiwei Zhang | 7c0c07c | 2020-07-04 23:49:47 -0700 | [diff] [blame] | 496 | for (uint32_t i = 0; i < ext_count; i++) { | 
|  | 497 | // Upon api downgrade, skip the promoted instance extensions in the | 
|  | 498 | // first pass to avoid duplicate extensions. | 
|  | 499 | const std::optional<uint32_t> version = | 
|  | 500 | GetInstanceExtensionPromotedVersion(ext_names[i]); | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 501 | if (version && *version > icd_api_version_ && | 
| Yiwei Zhang | 7c0c07c | 2020-07-04 23:49:47 -0700 | [diff] [blame] | 502 | *version <= loader_api_version_) | 
|  | 503 | continue; | 
|  | 504 |  | 
|  | 505 | FilterExtension(ext_names[i]); | 
|  | 506 | } | 
|  | 507 |  | 
|  | 508 | // Enable the required extensions to support core functionalities. | 
|  | 509 | const auto promoted_extensions = GetPromotedInstanceExtensions( | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 510 | icd_api_version_, loader_api_version_); | 
| Yiwei Zhang | 7c0c07c | 2020-07-04 23:49:47 -0700 | [diff] [blame] | 511 | for (const auto& promoted_extension : promoted_extensions) | 
|  | 512 | FilterExtension(promoted_extension); | 
|  | 513 | } else { | 
|  | 514 | for (uint32_t i = 0; i < ext_count; i++) | 
|  | 515 | FilterExtension(ext_names[i]); | 
|  | 516 | } | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 517 |  | 
| Jesse Hall | d3d887a | 2018-03-05 13:34:45 -0800 | [diff] [blame] | 518 | // Enable device extensions that contain physical-device commands, so that | 
|  | 519 | // vkGetInstanceProcAddr will return those physical-device commands. | 
|  | 520 | if (is_instance_) { | 
|  | 521 | hook_extensions_.set(ProcHook::KHR_swapchain); | 
|  | 522 | } | 
|  | 523 |  | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 524 | const uint32_t api_version = | 
|  | 525 | is_instance_ ? loader_api_version_ | 
|  | 526 | : std::min(icd_api_version_, loader_api_version_); | 
|  | 527 | switch (api_version) { | 
|  | 528 | case VK_API_VERSION_1_1: | 
|  | 529 | hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1); | 
|  | 530 | hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1); | 
|  | 531 | [[clang::fallthrough]]; | 
|  | 532 | case VK_API_VERSION_1_0: | 
|  | 533 | hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0); | 
|  | 534 | hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0); | 
|  | 535 | break; | 
|  | 536 | default: | 
|  | 537 | ALOGE("Unknown API version[%u]", api_version); | 
|  | 538 | break; | 
|  | 539 | } | 
|  | 540 |  | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 541 | ext_names = extension_filter_.names; | 
|  | 542 | ext_count = extension_filter_.name_count; | 
|  | 543 |  | 
|  | 544 | return VK_SUCCESS; | 
|  | 545 | } | 
|  | 546 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 547 | VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const { | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 548 | if (is_instance_) { | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 549 | return Hal::Device().EnumerateInstanceExtensionProperties( | 
|  | 550 | nullptr, &count, nullptr); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 551 | } else { | 
|  | 552 | const auto& driver = GetData(physical_dev_).driver; | 
|  | 553 | return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr, | 
|  | 554 | &count, nullptr); | 
|  | 555 | } | 
|  | 556 | } | 
|  | 557 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 558 | VkResult CreateInfoWrapper::EnumerateExtensions( | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 559 | uint32_t& count, | 
|  | 560 | VkExtensionProperties* props) const { | 
|  | 561 | if (is_instance_) { | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 562 | return Hal::Device().EnumerateInstanceExtensionProperties( | 
|  | 563 | nullptr, &count, props); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 564 | } else { | 
|  | 565 | const auto& driver = GetData(physical_dev_).driver; | 
|  | 566 | return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr, | 
|  | 567 | &count, props); | 
|  | 568 | } | 
|  | 569 | } | 
|  | 570 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 571 | VkResult CreateInfoWrapper::InitExtensionFilter() { | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 572 | // query extension count | 
|  | 573 | uint32_t count; | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 574 | VkResult result = QueryExtensionCount(count); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 575 | if (result != VK_SUCCESS || count == 0) | 
|  | 576 | return result; | 
|  | 577 |  | 
|  | 578 | auto& filter = extension_filter_; | 
|  | 579 | filter.exts = | 
|  | 580 | reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation( | 
|  | 581 | allocator_.pUserData, sizeof(VkExtensionProperties) * count, | 
|  | 582 | alignof(VkExtensionProperties), | 
|  | 583 | VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); | 
|  | 584 | if (!filter.exts) | 
|  | 585 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 586 |  | 
|  | 587 | // enumerate extensions | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 588 | result = EnumerateExtensions(count, filter.exts); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 589 | if (result != VK_SUCCESS && result != VK_INCOMPLETE) | 
|  | 590 | return result; | 
|  | 591 |  | 
|  | 592 | if (!count) | 
|  | 593 | return VK_SUCCESS; | 
|  | 594 |  | 
|  | 595 | filter.ext_count = count; | 
|  | 596 |  | 
|  | 597 | // allocate name array | 
| Yiwei Zhang | 7c0c07c | 2020-07-04 23:49:47 -0700 | [diff] [blame] | 598 | if (is_instance_) { | 
|  | 599 | uint32_t enabled_ext_count = instance_info_.enabledExtensionCount; | 
|  | 600 |  | 
|  | 601 | // It requires enabling additional promoted extensions to downgrade api, | 
|  | 602 | // so we reserve enough space here. | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 603 | if (icd_api_version_ < loader_api_version_) { | 
| Yiwei Zhang | 7c0c07c | 2020-07-04 23:49:47 -0700 | [diff] [blame] | 604 | enabled_ext_count += CountPromotedInstanceExtensions( | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 605 | icd_api_version_, loader_api_version_); | 
| Yiwei Zhang | 7c0c07c | 2020-07-04 23:49:47 -0700 | [diff] [blame] | 606 | } | 
|  | 607 |  | 
|  | 608 | count = std::min(filter.ext_count, enabled_ext_count); | 
|  | 609 | } else { | 
|  | 610 | count = std::min(filter.ext_count, dev_info_.enabledExtensionCount); | 
|  | 611 | } | 
| Yiwei Zhang | d7ea44a | 2020-08-13 12:54:27 -0700 | [diff] [blame] | 612 |  | 
|  | 613 | if (!count) | 
|  | 614 | return VK_SUCCESS; | 
|  | 615 |  | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 616 | filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation( | 
|  | 617 | allocator_.pUserData, sizeof(const char*) * count, alignof(const char*), | 
|  | 618 | VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); | 
|  | 619 | if (!filter.names) | 
|  | 620 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 621 |  | 
|  | 622 | return VK_SUCCESS; | 
|  | 623 | } | 
|  | 624 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 625 | void CreateInfoWrapper::FilterExtension(const char* name) { | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 626 | auto& filter = extension_filter_; | 
|  | 627 |  | 
|  | 628 | ProcHook::Extension ext_bit = GetProcHookExtension(name); | 
|  | 629 | if (is_instance_) { | 
|  | 630 | switch (ext_bit) { | 
|  | 631 | case ProcHook::KHR_android_surface: | 
|  | 632 | case ProcHook::KHR_surface: | 
| Courtney Goeltzenleuchter | e278daf | 2017-02-02 16:54:57 -0700 | [diff] [blame] | 633 | case ProcHook::EXT_swapchain_colorspace: | 
| Chris Forbes | 2452cf7 | 2017-03-16 16:30:17 +1300 | [diff] [blame] | 634 | case ProcHook::KHR_get_surface_capabilities2: | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 635 | hook_extensions_.set(ext_bit); | 
|  | 636 | // return now as these extensions do not require HAL support | 
|  | 637 | return; | 
|  | 638 | case ProcHook::EXT_debug_report: | 
|  | 639 | // both we and HAL can take part in | 
|  | 640 | hook_extensions_.set(ext_bit); | 
|  | 641 | break; | 
| Chris Forbes | 6aa30db | 2017-02-20 17:12:53 +1300 | [diff] [blame] | 642 | case ProcHook::KHR_get_physical_device_properties2: | 
| Yiwei Zhang | e4f6417 | 2020-07-05 15:17:32 -0700 | [diff] [blame] | 643 | case ProcHook::KHR_device_group_creation: | 
| Yiwei Zhang | e1f3501 | 2020-07-05 22:52:04 -0700 | [diff] [blame] | 644 | case ProcHook::KHR_external_memory_capabilities: | 
|  | 645 | case ProcHook::KHR_external_semaphore_capabilities: | 
|  | 646 | case ProcHook::KHR_external_fence_capabilities: | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 647 | case ProcHook::EXTENSION_UNKNOWN: | 
|  | 648 | // Extensions we don't need to do anything about at this level | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 649 | break; | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 650 |  | 
| Yiwei Zhang | 2314310 | 2019-04-10 18:24:05 -0700 | [diff] [blame] | 651 | case ProcHook::KHR_bind_memory2: | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 652 | case ProcHook::KHR_incremental_present: | 
|  | 653 | case ProcHook::KHR_shared_presentable_image: | 
|  | 654 | case ProcHook::KHR_swapchain: | 
|  | 655 | case ProcHook::EXT_hdr_metadata: | 
|  | 656 | case ProcHook::ANDROID_external_memory_android_hardware_buffer: | 
|  | 657 | case ProcHook::ANDROID_native_buffer: | 
|  | 658 | case ProcHook::GOOGLE_display_timing: | 
| Yiwei Zhang | 7cc36a5 | 2019-10-11 19:02:09 -0700 | [diff] [blame] | 659 | case ProcHook::EXTENSION_CORE_1_0: | 
|  | 660 | case ProcHook::EXTENSION_CORE_1_1: | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 661 | case ProcHook::EXTENSION_COUNT: | 
|  | 662 | // Device and meta extensions. If we ever get here it's a bug in | 
|  | 663 | // our code. But enumerating them lets us avoid having a default | 
|  | 664 | // case, and default hides other bugs. | 
|  | 665 | ALOGE( | 
|  | 666 | "CreateInfoWrapper::FilterExtension: invalid instance " | 
|  | 667 | "extension '%s'. FIX ME", | 
|  | 668 | name); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 669 | return; | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 670 |  | 
|  | 671 | // Don't use a default case. Without it, -Wswitch will tell us | 
|  | 672 | // at compile time if someone adds a new ProcHook extension but | 
|  | 673 | // doesn't handle it above. That's a real bug that has | 
|  | 674 | // not-immediately-obvious effects. | 
|  | 675 | // | 
|  | 676 | // default: | 
|  | 677 | //     break; | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 678 | } | 
|  | 679 | } else { | 
|  | 680 | switch (ext_bit) { | 
|  | 681 | case ProcHook::KHR_swapchain: | 
|  | 682 | // map VK_KHR_swapchain to VK_ANDROID_native_buffer | 
|  | 683 | name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME; | 
|  | 684 | ext_bit = ProcHook::ANDROID_native_buffer; | 
|  | 685 | break; | 
| Ian Elliott | 9e85373 | 2017-02-03 11:24:07 -0700 | [diff] [blame] | 686 | case ProcHook::KHR_incremental_present: | 
| Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 687 | case ProcHook::GOOGLE_display_timing: | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 688 | case ProcHook::KHR_shared_presentable_image: | 
| Ian Elliott | 8a97726 | 2017-01-19 09:05:58 -0700 | [diff] [blame] | 689 | hook_extensions_.set(ext_bit); | 
|  | 690 | // return now as these extensions do not require HAL support | 
|  | 691 | return; | 
| Courtney Goeltzenleuchter | d634c48 | 2017-01-05 15:55:31 -0700 | [diff] [blame] | 692 | case ProcHook::EXT_hdr_metadata: | 
| Yiwei Zhang | 2314310 | 2019-04-10 18:24:05 -0700 | [diff] [blame] | 693 | case ProcHook::KHR_bind_memory2: | 
| Courtney Goeltzenleuchter | d634c48 | 2017-01-05 15:55:31 -0700 | [diff] [blame] | 694 | hook_extensions_.set(ext_bit); | 
|  | 695 | break; | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 696 | case ProcHook::ANDROID_external_memory_android_hardware_buffer: | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 697 | case ProcHook::EXTENSION_UNKNOWN: | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 698 | // Extensions we don't need to do anything about at this level | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 699 | break; | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 700 |  | 
|  | 701 | case ProcHook::KHR_android_surface: | 
|  | 702 | case ProcHook::KHR_get_physical_device_properties2: | 
| Yiwei Zhang | e4f6417 | 2020-07-05 15:17:32 -0700 | [diff] [blame] | 703 | case ProcHook::KHR_device_group_creation: | 
| Yiwei Zhang | e1f3501 | 2020-07-05 22:52:04 -0700 | [diff] [blame] | 704 | case ProcHook::KHR_external_memory_capabilities: | 
|  | 705 | case ProcHook::KHR_external_semaphore_capabilities: | 
|  | 706 | case ProcHook::KHR_external_fence_capabilities: | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 707 | case ProcHook::KHR_get_surface_capabilities2: | 
|  | 708 | case ProcHook::KHR_surface: | 
|  | 709 | case ProcHook::EXT_debug_report: | 
|  | 710 | case ProcHook::EXT_swapchain_colorspace: | 
|  | 711 | case ProcHook::ANDROID_native_buffer: | 
| Yiwei Zhang | 7cc36a5 | 2019-10-11 19:02:09 -0700 | [diff] [blame] | 712 | case ProcHook::EXTENSION_CORE_1_0: | 
|  | 713 | case ProcHook::EXTENSION_CORE_1_1: | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 714 | case ProcHook::EXTENSION_COUNT: | 
|  | 715 | // Instance and meta extensions. If we ever get here it's a bug | 
|  | 716 | // in our code. But enumerating them lets us avoid having a | 
|  | 717 | // default case, and default hides other bugs. | 
|  | 718 | ALOGE( | 
|  | 719 | "CreateInfoWrapper::FilterExtension: invalid device " | 
|  | 720 | "extension '%s'. FIX ME", | 
|  | 721 | name); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 722 | return; | 
| Jesse Hall | 7f983a8 | 2018-03-29 14:46:45 -0700 | [diff] [blame] | 723 |  | 
|  | 724 | // Don't use a default case. Without it, -Wswitch will tell us | 
|  | 725 | // at compile time if someone adds a new ProcHook extension but | 
|  | 726 | // doesn't handle it above. That's a real bug that has | 
|  | 727 | // not-immediately-obvious effects. | 
|  | 728 | // | 
|  | 729 | // default: | 
|  | 730 | //     break; | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 731 | } | 
|  | 732 | } | 
|  | 733 |  | 
|  | 734 | for (uint32_t i = 0; i < filter.ext_count; i++) { | 
|  | 735 | const VkExtensionProperties& props = filter.exts[i]; | 
|  | 736 | // ignore unknown extensions | 
|  | 737 | if (strcmp(name, props.extensionName) != 0) | 
|  | 738 | continue; | 
|  | 739 |  | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 740 | filter.names[filter.name_count++] = name; | 
| Chia-I Wu | 1600e26 | 2016-04-12 09:40:06 +0800 | [diff] [blame] | 741 | if (ext_bit != ProcHook::EXTENSION_UNKNOWN) { | 
|  | 742 | if (ext_bit == ProcHook::ANDROID_native_buffer) | 
|  | 743 | hook_extensions_.set(ProcHook::KHR_swapchain); | 
|  | 744 |  | 
|  | 745 | hal_extensions_.set(ext_bit); | 
|  | 746 | } | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 747 |  | 
|  | 748 | break; | 
|  | 749 | } | 
|  | 750 | } | 
|  | 751 |  | 
| Chia-I Wu | dbb7e9c | 2016-03-24 15:09:38 +0800 | [diff] [blame] | 752 | VKAPI_ATTR void* DefaultAllocate(void*, | 
|  | 753 | size_t size, | 
|  | 754 | size_t alignment, | 
|  | 755 | VkSystemAllocationScope) { | 
|  | 756 | void* ptr = nullptr; | 
|  | 757 | // Vulkan requires 'alignment' to be a power of two, but posix_memalign | 
|  | 758 | // additionally requires that it be at least sizeof(void*). | 
|  | 759 | int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size); | 
|  | 760 | ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment, | 
|  | 761 | ret, ptr); | 
|  | 762 | return ret == 0 ? ptr : nullptr; | 
|  | 763 | } | 
|  | 764 |  | 
|  | 765 | VKAPI_ATTR void* DefaultReallocate(void*, | 
|  | 766 | void* ptr, | 
|  | 767 | size_t size, | 
|  | 768 | size_t alignment, | 
|  | 769 | VkSystemAllocationScope) { | 
|  | 770 | if (size == 0) { | 
|  | 771 | free(ptr); | 
|  | 772 | return nullptr; | 
|  | 773 | } | 
|  | 774 |  | 
| Yiwei Zhang | a885c06 | 2019-10-24 12:07:57 -0700 | [diff] [blame] | 775 | // TODO(b/143295633): Right now we never shrink allocations; if the new | 
| Chia-I Wu | dbb7e9c | 2016-03-24 15:09:38 +0800 | [diff] [blame] | 776 | // request is smaller than the existing chunk, we just continue using it. | 
|  | 777 | // Right now the loader never reallocs, so this doesn't matter. If that | 
|  | 778 | // changes, or if this code is copied into some other project, this should | 
|  | 779 | // probably have a heuristic to allocate-copy-free when doing so will save | 
|  | 780 | // "enough" space. | 
|  | 781 | size_t old_size = ptr ? malloc_usable_size(ptr) : 0; | 
|  | 782 | if (size <= old_size) | 
|  | 783 | return ptr; | 
|  | 784 |  | 
|  | 785 | void* new_ptr = nullptr; | 
|  | 786 | if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0) | 
|  | 787 | return nullptr; | 
|  | 788 | if (ptr) { | 
|  | 789 | memcpy(new_ptr, ptr, std::min(old_size, size)); | 
|  | 790 | free(ptr); | 
|  | 791 | } | 
|  | 792 | return new_ptr; | 
|  | 793 | } | 
|  | 794 |  | 
|  | 795 | VKAPI_ATTR void DefaultFree(void*, void* ptr) { | 
|  | 796 | ALOGD_CALLSTACK("Free: %p", ptr); | 
|  | 797 | free(ptr); | 
|  | 798 | } | 
|  | 799 |  | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 800 | InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) { | 
|  | 801 | void* data_mem = allocator.pfnAllocation( | 
|  | 802 | allocator.pUserData, sizeof(InstanceData), alignof(InstanceData), | 
|  | 803 | VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE); | 
|  | 804 | if (!data_mem) | 
|  | 805 | return nullptr; | 
|  | 806 |  | 
|  | 807 | return new (data_mem) InstanceData(allocator); | 
|  | 808 | } | 
|  | 809 |  | 
|  | 810 | void FreeInstanceData(InstanceData* data, | 
|  | 811 | const VkAllocationCallbacks& allocator) { | 
|  | 812 | data->~InstanceData(); | 
|  | 813 | allocator.pfnFree(allocator.pUserData, data); | 
|  | 814 | } | 
|  | 815 |  | 
| Chia-I Wu | 950d6e1 | 2016-05-03 09:12:35 +0800 | [diff] [blame] | 816 | DeviceData* AllocateDeviceData( | 
|  | 817 | const VkAllocationCallbacks& allocator, | 
|  | 818 | const DebugReportCallbackList& debug_report_callbacks) { | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 819 | void* data_mem = allocator.pfnAllocation( | 
|  | 820 | allocator.pUserData, sizeof(DeviceData), alignof(DeviceData), | 
|  | 821 | VK_SYSTEM_ALLOCATION_SCOPE_DEVICE); | 
|  | 822 | if (!data_mem) | 
|  | 823 | return nullptr; | 
|  | 824 |  | 
| Chia-I Wu | 950d6e1 | 2016-05-03 09:12:35 +0800 | [diff] [blame] | 825 | return new (data_mem) DeviceData(allocator, debug_report_callbacks); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 826 | } | 
|  | 827 |  | 
|  | 828 | void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) { | 
|  | 829 | data->~DeviceData(); | 
|  | 830 | allocator.pfnFree(allocator.pUserData, data); | 
|  | 831 | } | 
|  | 832 |  | 
| Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 833 | }  // anonymous namespace | 
|  | 834 |  | 
| Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 835 | bool OpenHAL() { | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 836 | return Hal::Open(); | 
| Chia-I Wu | 136b8eb | 2016-03-24 15:01:52 +0800 | [diff] [blame] | 837 | } | 
|  | 838 |  | 
| Chia-I Wu | dbb7e9c | 2016-03-24 15:09:38 +0800 | [diff] [blame] | 839 | const VkAllocationCallbacks& GetDefaultAllocator() { | 
|  | 840 | static const VkAllocationCallbacks kDefaultAllocCallbacks = { | 
|  | 841 | .pUserData = nullptr, | 
|  | 842 | .pfnAllocation = DefaultAllocate, | 
|  | 843 | .pfnReallocation = DefaultReallocate, | 
|  | 844 | .pfnFree = DefaultFree, | 
|  | 845 | }; | 
|  | 846 |  | 
|  | 847 | return kDefaultAllocCallbacks; | 
|  | 848 | } | 
|  | 849 |  | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 850 | PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) { | 
|  | 851 | const ProcHook* hook = GetProcHook(pName); | 
|  | 852 | if (!hook) | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 853 | return Hal::Device().GetInstanceProcAddr(instance, pName); | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 854 |  | 
|  | 855 | if (!instance) { | 
|  | 856 | if (hook->type == ProcHook::GLOBAL) | 
|  | 857 | return hook->proc; | 
|  | 858 |  | 
| Chia-I Wu | 109f898 | 2016-04-22 06:40:40 +0800 | [diff] [blame] | 859 | // v0 layers expect | 
|  | 860 | // | 
|  | 861 | //   vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice"); | 
|  | 862 | // | 
|  | 863 | // to work. | 
|  | 864 | if (strcmp(pName, "vkCreateDevice") == 0) | 
|  | 865 | return hook->proc; | 
|  | 866 |  | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 867 | ALOGE( | 
| Chia-I Wu | e201c3f | 2016-05-03 13:26:08 +0800 | [diff] [blame] | 868 | "internal vkGetInstanceProcAddr called for %s without an instance", | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 869 | pName); | 
|  | 870 |  | 
| Chia-I Wu | 109f898 | 2016-04-22 06:40:40 +0800 | [diff] [blame] | 871 | return nullptr; | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 872 | } | 
|  | 873 |  | 
|  | 874 | PFN_vkVoidFunction proc; | 
|  | 875 |  | 
|  | 876 | switch (hook->type) { | 
|  | 877 | case ProcHook::INSTANCE: | 
|  | 878 | proc = (GetData(instance).hook_extensions[hook->extension]) | 
|  | 879 | ? hook->proc | 
| Chia-I Wu | 36cc00a | 2016-04-13 16:52:06 +0800 | [diff] [blame] | 880 | : nullptr; | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 881 | break; | 
|  | 882 | case ProcHook::DEVICE: | 
| Yiwei Zhang | 7cc36a5 | 2019-10-11 19:02:09 -0700 | [diff] [blame] | 883 | proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0) | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 884 | ? hook->proc | 
|  | 885 | : hook->checked_proc; | 
|  | 886 | break; | 
|  | 887 | default: | 
|  | 888 | ALOGE( | 
| Chia-I Wu | e201c3f | 2016-05-03 13:26:08 +0800 | [diff] [blame] | 889 | "internal vkGetInstanceProcAddr called for %s with an instance", | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 890 | pName); | 
|  | 891 | proc = nullptr; | 
|  | 892 | break; | 
|  | 893 | } | 
|  | 894 |  | 
|  | 895 | return proc; | 
|  | 896 | } | 
|  | 897 |  | 
|  | 898 | PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) { | 
|  | 899 | const ProcHook* hook = GetProcHook(pName); | 
|  | 900 | if (!hook) | 
| Chia-I Wu | cc5e276 | 2016-03-24 13:01:16 +0800 | [diff] [blame] | 901 | return GetData(device).driver.GetDeviceProcAddr(device, pName); | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 902 |  | 
|  | 903 | if (hook->type != ProcHook::DEVICE) { | 
| Chia-I Wu | e201c3f | 2016-05-03 13:26:08 +0800 | [diff] [blame] | 904 | ALOGE("internal vkGetDeviceProcAddr called for %s", pName); | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 905 | return nullptr; | 
|  | 906 | } | 
|  | 907 |  | 
| Chia-I Wu | 36cc00a | 2016-04-13 16:52:06 +0800 | [diff] [blame] | 908 | return (GetData(device).hook_extensions[hook->extension]) ? hook->proc | 
|  | 909 | : nullptr; | 
| Chia-I Wu | eb7db12 | 2016-03-24 09:11:06 +0800 | [diff] [blame] | 910 | } | 
|  | 911 |  | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 912 | VkResult EnumerateInstanceExtensionProperties( | 
|  | 913 | const char* pLayerName, | 
|  | 914 | uint32_t* pPropertyCount, | 
|  | 915 | VkExtensionProperties* pProperties) { | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 916 | std::vector<VkExtensionProperties> loader_extensions; | 
| Ian Elliott | 34a327b | 2017-03-28 13:20:35 -0600 | [diff] [blame] | 917 | loader_extensions.push_back({ | 
|  | 918 | VK_KHR_SURFACE_EXTENSION_NAME, | 
|  | 919 | VK_KHR_SURFACE_SPEC_VERSION}); | 
|  | 920 | loader_extensions.push_back({ | 
|  | 921 | VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, | 
|  | 922 | VK_KHR_ANDROID_SURFACE_SPEC_VERSION}); | 
|  | 923 | loader_extensions.push_back({ | 
|  | 924 | VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME, | 
|  | 925 | VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION}); | 
| Chris Forbes | 1609500 | 2017-05-05 15:33:29 -0700 | [diff] [blame] | 926 | loader_extensions.push_back({ | 
|  | 927 | VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME, | 
|  | 928 | VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION}); | 
| Ian Elliott | 34a327b | 2017-03-28 13:20:35 -0600 | [diff] [blame] | 929 |  | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 930 | static const VkExtensionProperties loader_debug_report_extension = { | 
|  | 931 | VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION, | 
|  | 932 | }; | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 933 |  | 
|  | 934 | // enumerate our extensions first | 
|  | 935 | if (!pLayerName && pProperties) { | 
|  | 936 | uint32_t count = std::min( | 
|  | 937 | *pPropertyCount, static_cast<uint32_t>(loader_extensions.size())); | 
|  | 938 |  | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 939 | std::copy_n(loader_extensions.data(), count, pProperties); | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 940 |  | 
|  | 941 | if (count < loader_extensions.size()) { | 
|  | 942 | *pPropertyCount = count; | 
|  | 943 | return VK_INCOMPLETE; | 
|  | 944 | } | 
|  | 945 |  | 
|  | 946 | pProperties += count; | 
|  | 947 | *pPropertyCount -= count; | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 948 |  | 
|  | 949 | if (Hal::Get().GetDebugReportIndex() < 0) { | 
|  | 950 | if (!*pPropertyCount) { | 
|  | 951 | *pPropertyCount = count; | 
|  | 952 | return VK_INCOMPLETE; | 
|  | 953 | } | 
|  | 954 |  | 
|  | 955 | pProperties[0] = loader_debug_report_extension; | 
|  | 956 | pProperties += 1; | 
|  | 957 | *pPropertyCount -= 1; | 
|  | 958 | } | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 959 | } | 
|  | 960 |  | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 961 | ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties"); | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 962 | VkResult result = Hal::Device().EnumerateInstanceExtensionProperties( | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 963 | pLayerName, pPropertyCount, pProperties); | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 964 | ATRACE_END(); | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 965 |  | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 966 | if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) { | 
|  | 967 | int idx = Hal::Get().GetDebugReportIndex(); | 
|  | 968 | if (idx < 0) { | 
|  | 969 | *pPropertyCount += 1; | 
|  | 970 | } else if (pProperties && | 
|  | 971 | static_cast<uint32_t>(idx) < *pPropertyCount) { | 
|  | 972 | pProperties[idx].specVersion = | 
|  | 973 | std::min(pProperties[idx].specVersion, | 
|  | 974 | loader_debug_report_extension.specVersion); | 
|  | 975 | } | 
|  | 976 |  | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 977 | *pPropertyCount += loader_extensions.size(); | 
| Chia-I Wu | 3193825 | 2016-05-23 15:31:02 +0800 | [diff] [blame] | 978 | } | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 979 |  | 
|  | 980 | return result; | 
|  | 981 | } | 
|  | 982 |  | 
| Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 983 | void QueryPresentationProperties( | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 984 | VkPhysicalDevice physicalDevice, | 
| Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 985 | VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) { | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 986 | // Request the android-specific presentation properties via GPDP2 | 
| Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 987 | VkPhysicalDeviceProperties2 properties = { | 
|  | 988 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 989 | presentation_properties, | 
| Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 990 | {}, | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 991 | }; | 
|  | 992 |  | 
|  | 993 | #pragma clang diagnostic push | 
|  | 994 | #pragma clang diagnostic ignored "-Wold-style-cast" | 
|  | 995 | presentation_properties->sType = | 
|  | 996 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID; | 
|  | 997 | #pragma clang diagnostic pop | 
|  | 998 | presentation_properties->pNext = nullptr; | 
|  | 999 | presentation_properties->sharedImage = VK_FALSE; | 
|  | 1000 |  | 
| Yiwei Zhang | a55624b | 2020-07-05 16:05:26 -0700 | [diff] [blame] | 1001 | GetPhysicalDeviceProperties2(physicalDevice, &properties); | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 1002 | } | 
|  | 1003 |  | 
| Chia-I Wu | 01cf305 | 2016-03-24 16:16:21 +0800 | [diff] [blame] | 1004 | VkResult EnumerateDeviceExtensionProperties( | 
|  | 1005 | VkPhysicalDevice physicalDevice, | 
|  | 1006 | const char* pLayerName, | 
|  | 1007 | uint32_t* pPropertyCount, | 
|  | 1008 | VkExtensionProperties* pProperties) { | 
|  | 1009 | const InstanceData& data = GetData(physicalDevice); | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 1010 | // extensions that are unconditionally exposed by the loader | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 1011 | std::vector<VkExtensionProperties> loader_extensions; | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 1012 | loader_extensions.push_back({ | 
|  | 1013 | VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME, | 
|  | 1014 | VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION}); | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 1015 |  | 
| Sundong Ahn | bc37dd5 | 2020-04-23 21:21:00 +0900 | [diff] [blame] | 1016 | bool hdrBoardConfig = android::sysprop::has_HDR_display(false); | 
| Courtney Goeltzenleuchter | 7671d46 | 2018-01-24 11:51:01 -0800 | [diff] [blame] | 1017 | if (hdrBoardConfig) { | 
|  | 1018 | loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME, | 
|  | 1019 | VK_EXT_HDR_METADATA_SPEC_VERSION}); | 
|  | 1020 | } | 
|  | 1021 |  | 
| Chris Forbes | 1609500 | 2017-05-05 15:33:29 -0700 | [diff] [blame] | 1022 | VkPhysicalDevicePresentationPropertiesANDROID presentation_properties; | 
| Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 1023 | QueryPresentationProperties(physicalDevice, &presentation_properties); | 
|  | 1024 | if (presentation_properties.sharedImage) { | 
| Chris Forbes | 1609500 | 2017-05-05 15:33:29 -0700 | [diff] [blame] | 1025 | loader_extensions.push_back({ | 
|  | 1026 | VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME, | 
|  | 1027 | VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION}); | 
| Chris Forbes | fa25e63 | 2017-02-22 12:36:02 +1300 | [diff] [blame] | 1028 | } | 
| Ian Elliott | d4b50aa | 2017-01-09 16:21:36 -0700 | [diff] [blame] | 1029 |  | 
| Ian Elliott | 5c34de2 | 2017-04-10 14:42:30 -0600 | [diff] [blame] | 1030 | // conditionally add VK_GOOGLE_display_timing if present timestamps are | 
|  | 1031 | // supported by the driver: | 
| Yiwei Zhang | 98d15e0 | 2020-08-15 13:48:36 -0700 | [diff] [blame] | 1032 | if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) { | 
| Ian Elliott | 5c34de2 | 2017-04-10 14:42:30 -0600 | [diff] [blame] | 1033 | loader_extensions.push_back({ | 
|  | 1034 | VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME, | 
|  | 1035 | VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION}); | 
|  | 1036 | } | 
|  | 1037 |  | 
| Ian Elliott | d4b50aa | 2017-01-09 16:21:36 -0700 | [diff] [blame] | 1038 | // enumerate our extensions first | 
|  | 1039 | if (!pLayerName && pProperties) { | 
|  | 1040 | uint32_t count = std::min( | 
|  | 1041 | *pPropertyCount, static_cast<uint32_t>(loader_extensions.size())); | 
|  | 1042 |  | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 1043 | std::copy_n(loader_extensions.data(), count, pProperties); | 
| Ian Elliott | d4b50aa | 2017-01-09 16:21:36 -0700 | [diff] [blame] | 1044 |  | 
|  | 1045 | if (count < loader_extensions.size()) { | 
|  | 1046 | *pPropertyCount = count; | 
|  | 1047 | return VK_INCOMPLETE; | 
|  | 1048 | } | 
|  | 1049 |  | 
|  | 1050 | pProperties += count; | 
|  | 1051 | *pPropertyCount -= count; | 
|  | 1052 | } | 
| Chia-I Wu | 01cf305 | 2016-03-24 16:16:21 +0800 | [diff] [blame] | 1053 |  | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1054 | ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties"); | 
| Chia-I Wu | 01cf305 | 2016-03-24 16:16:21 +0800 | [diff] [blame] | 1055 | VkResult result = data.driver.EnumerateDeviceExtensionProperties( | 
|  | 1056 | physicalDevice, pLayerName, pPropertyCount, pProperties); | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1057 | ATRACE_END(); | 
| Chia-I Wu | 01cf305 | 2016-03-24 16:16:21 +0800 | [diff] [blame] | 1058 |  | 
| Ian Elliott | d4b50aa | 2017-01-09 16:21:36 -0700 | [diff] [blame] | 1059 | if (pProperties) { | 
|  | 1060 | // map VK_ANDROID_native_buffer to VK_KHR_swapchain | 
|  | 1061 | for (uint32_t i = 0; i < *pPropertyCount; i++) { | 
|  | 1062 | auto& prop = pProperties[i]; | 
| Chia-I Wu | 01cf305 | 2016-03-24 16:16:21 +0800 | [diff] [blame] | 1063 |  | 
| Ian Elliott | d4b50aa | 2017-01-09 16:21:36 -0700 | [diff] [blame] | 1064 | if (strcmp(prop.extensionName, | 
|  | 1065 | VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0) | 
|  | 1066 | continue; | 
| Chia-I Wu | 01cf305 | 2016-03-24 16:16:21 +0800 | [diff] [blame] | 1067 |  | 
| Ian Elliott | d4b50aa | 2017-01-09 16:21:36 -0700 | [diff] [blame] | 1068 | memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME, | 
|  | 1069 | sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME)); | 
| Yiwei Zhang | 14f4d42 | 2019-04-17 12:24:39 -0700 | [diff] [blame] | 1070 |  | 
|  | 1071 | if (prop.specVersion >= 8) { | 
|  | 1072 | prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION; | 
|  | 1073 | } else { | 
|  | 1074 | prop.specVersion = 68; | 
|  | 1075 | } | 
| Ian Elliott | d4b50aa | 2017-01-09 16:21:36 -0700 | [diff] [blame] | 1076 | } | 
|  | 1077 | } | 
| Chia-I Wu | 01cf305 | 2016-03-24 16:16:21 +0800 | [diff] [blame] | 1078 |  | 
| Ian Elliott | d4b50aa | 2017-01-09 16:21:36 -0700 | [diff] [blame] | 1079 | // restore loader extension count | 
|  | 1080 | if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) { | 
|  | 1081 | *pPropertyCount += loader_extensions.size(); | 
| Chia-I Wu | 01cf305 | 2016-03-24 16:16:21 +0800 | [diff] [blame] | 1082 | } | 
|  | 1083 |  | 
|  | 1084 | return result; | 
|  | 1085 | } | 
|  | 1086 |  | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 1087 | VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, | 
|  | 1088 | const VkAllocationCallbacks* pAllocator, | 
|  | 1089 | VkInstance* pInstance) { | 
|  | 1090 | const VkAllocationCallbacks& data_allocator = | 
|  | 1091 | (pAllocator) ? *pAllocator : GetDefaultAllocator(); | 
|  | 1092 |  | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 1093 | VkResult result = VK_SUCCESS; | 
|  | 1094 | uint32_t icd_api_version = VK_API_VERSION_1_0; | 
|  | 1095 | PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version = | 
|  | 1096 | reinterpret_cast<PFN_vkEnumerateInstanceVersion>( | 
|  | 1097 | Hal::Device().GetInstanceProcAddr(nullptr, | 
|  | 1098 | "vkEnumerateInstanceVersion")); | 
|  | 1099 | if (pfn_enumerate_instance_version) { | 
|  | 1100 | ATRACE_BEGIN("pfn_enumerate_instance_version"); | 
|  | 1101 | result = (*pfn_enumerate_instance_version)(&icd_api_version); | 
|  | 1102 | ATRACE_END(); | 
|  | 1103 | if (result != VK_SUCCESS) | 
|  | 1104 | return result; | 
|  | 1105 |  | 
|  | 1106 | icd_api_version ^= VK_VERSION_PATCH(icd_api_version); | 
|  | 1107 | } | 
|  | 1108 |  | 
|  | 1109 | CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator); | 
|  | 1110 | result = wrapper.Validate(); | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 1111 | if (result != VK_SUCCESS) | 
|  | 1112 | return result; | 
|  | 1113 |  | 
|  | 1114 | InstanceData* data = AllocateInstanceData(data_allocator); | 
|  | 1115 | if (!data) | 
|  | 1116 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 1117 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 1118 | data->hook_extensions |= wrapper.GetHookExtensions(); | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 1119 |  | 
|  | 1120 | // call into the driver | 
|  | 1121 | VkInstance instance; | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1122 | ATRACE_BEGIN("driver.CreateInstance"); | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 1123 | result = Hal::Device().CreateInstance( | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 1124 | static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator, | 
|  | 1125 | &instance); | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1126 | ATRACE_END(); | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 1127 | if (result != VK_SUCCESS) { | 
|  | 1128 | FreeInstanceData(data, data_allocator); | 
|  | 1129 | return result; | 
|  | 1130 | } | 
|  | 1131 |  | 
|  | 1132 | // initialize InstanceDriverTable | 
|  | 1133 | if (!SetData(instance, *data) || | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 1134 | !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr, | 
| Chia-I Wu | cbe07ef | 2016-04-13 15:01:00 +0800 | [diff] [blame] | 1135 | wrapper.GetHalExtensions())) { | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 1136 | data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>( | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 1137 | Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance")); | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 1138 | if (data->driver.DestroyInstance) | 
|  | 1139 | data->driver.DestroyInstance(instance, pAllocator); | 
|  | 1140 |  | 
|  | 1141 | FreeInstanceData(data, data_allocator); | 
|  | 1142 |  | 
|  | 1143 | return VK_ERROR_INCOMPATIBLE_DRIVER; | 
|  | 1144 | } | 
|  | 1145 |  | 
|  | 1146 | data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>( | 
| Chia-I Wu | 31b2e4f | 2016-05-23 10:47:57 +0800 | [diff] [blame] | 1147 | Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr")); | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 1148 | if (!data->get_device_proc_addr) { | 
|  | 1149 | data->driver.DestroyInstance(instance, pAllocator); | 
|  | 1150 | FreeInstanceData(data, data_allocator); | 
|  | 1151 |  | 
|  | 1152 | return VK_ERROR_INCOMPATIBLE_DRIVER; | 
|  | 1153 | } | 
|  | 1154 |  | 
|  | 1155 | *pInstance = instance; | 
|  | 1156 |  | 
|  | 1157 | return VK_SUCCESS; | 
|  | 1158 | } | 
|  | 1159 |  | 
|  | 1160 | void DestroyInstance(VkInstance instance, | 
|  | 1161 | const VkAllocationCallbacks* pAllocator) { | 
|  | 1162 | InstanceData& data = GetData(instance); | 
|  | 1163 | data.driver.DestroyInstance(instance, pAllocator); | 
|  | 1164 |  | 
|  | 1165 | VkAllocationCallbacks local_allocator; | 
|  | 1166 | if (!pAllocator) { | 
|  | 1167 | local_allocator = data.allocator; | 
|  | 1168 | pAllocator = &local_allocator; | 
|  | 1169 | } | 
|  | 1170 |  | 
|  | 1171 | FreeInstanceData(&data, *pAllocator); | 
|  | 1172 | } | 
|  | 1173 |  | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 1174 | VkResult CreateDevice(VkPhysicalDevice physicalDevice, | 
|  | 1175 | const VkDeviceCreateInfo* pCreateInfo, | 
|  | 1176 | const VkAllocationCallbacks* pAllocator, | 
|  | 1177 | VkDevice* pDevice) { | 
|  | 1178 | const InstanceData& instance_data = GetData(physicalDevice); | 
|  | 1179 | const VkAllocationCallbacks& data_allocator = | 
|  | 1180 | (pAllocator) ? *pAllocator : instance_data.allocator; | 
|  | 1181 |  | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 1182 | VkPhysicalDeviceProperties properties; | 
|  | 1183 | ATRACE_BEGIN("driver.GetPhysicalDeviceProperties"); | 
|  | 1184 | instance_data.driver.GetPhysicalDeviceProperties(physicalDevice, | 
|  | 1185 | &properties); | 
|  | 1186 | ATRACE_END(); | 
|  | 1187 |  | 
|  | 1188 | CreateInfoWrapper wrapper( | 
|  | 1189 | physicalDevice, *pCreateInfo, | 
|  | 1190 | properties.apiVersion ^ VK_VERSION_PATCH(properties.apiVersion), | 
|  | 1191 | data_allocator); | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 1192 | VkResult result = wrapper.Validate(); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 1193 | if (result != VK_SUCCESS) | 
|  | 1194 | return result; | 
|  | 1195 |  | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1196 | ATRACE_BEGIN("AllocateDeviceData"); | 
| Chia-I Wu | 950d6e1 | 2016-05-03 09:12:35 +0800 | [diff] [blame] | 1197 | DeviceData* data = AllocateDeviceData(data_allocator, | 
|  | 1198 | instance_data.debug_report_callbacks); | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1199 | ATRACE_END(); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 1200 | if (!data) | 
|  | 1201 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 1202 |  | 
| Chia-I Wu | 3e6c2d6 | 2016-04-11 13:55:56 +0800 | [diff] [blame] | 1203 | data->hook_extensions |= wrapper.GetHookExtensions(); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 1204 |  | 
|  | 1205 | // call into the driver | 
|  | 1206 | VkDevice dev; | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1207 | ATRACE_BEGIN("driver.CreateDevice"); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 1208 | result = instance_data.driver.CreateDevice( | 
|  | 1209 | physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper), | 
|  | 1210 | pAllocator, &dev); | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1211 | ATRACE_END(); | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 1212 | if (result != VK_SUCCESS) { | 
|  | 1213 | FreeDeviceData(data, data_allocator); | 
|  | 1214 | return result; | 
|  | 1215 | } | 
|  | 1216 |  | 
|  | 1217 | // initialize DeviceDriverTable | 
|  | 1218 | if (!SetData(dev, *data) || | 
| Chia-I Wu | cbe07ef | 2016-04-13 15:01:00 +0800 | [diff] [blame] | 1219 | !InitDriverTable(dev, instance_data.get_device_proc_addr, | 
|  | 1220 | wrapper.GetHalExtensions())) { | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 1221 | data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>( | 
|  | 1222 | instance_data.get_device_proc_addr(dev, "vkDestroyDevice")); | 
|  | 1223 | if (data->driver.DestroyDevice) | 
|  | 1224 | data->driver.DestroyDevice(dev, pAllocator); | 
|  | 1225 |  | 
|  | 1226 | FreeDeviceData(data, data_allocator); | 
|  | 1227 |  | 
|  | 1228 | return VK_ERROR_INCOMPATIBLE_DRIVER; | 
|  | 1229 | } | 
| Chris Forbes | d827791 | 2017-02-10 14:59:59 +1300 | [diff] [blame] | 1230 |  | 
|  | 1231 | // sanity check ANDROID_native_buffer implementation, whose set of | 
|  | 1232 | // entrypoints varies according to the spec version. | 
|  | 1233 | if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) && | 
|  | 1234 | !data->driver.GetSwapchainGrallocUsageANDROID && | 
|  | 1235 | !data->driver.GetSwapchainGrallocUsage2ANDROID) { | 
|  | 1236 | ALOGE("Driver's implementation of ANDROID_native_buffer is broken;" | 
|  | 1237 | " must expose at least one of " | 
|  | 1238 | "vkGetSwapchainGrallocUsageANDROID or " | 
|  | 1239 | "vkGetSwapchainGrallocUsage2ANDROID"); | 
|  | 1240 |  | 
|  | 1241 | data->driver.DestroyDevice(dev, pAllocator); | 
|  | 1242 | FreeDeviceData(data, data_allocator); | 
|  | 1243 |  | 
|  | 1244 | return VK_ERROR_INCOMPATIBLE_DRIVER; | 
|  | 1245 | } | 
|  | 1246 |  | 
| Yiwei Zhang | 2cefa73 | 2020-07-10 21:07:30 -0700 | [diff] [blame] | 1247 | if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) { | 
|  | 1248 | // Log that the app is hitting software Vulkan implementation | 
|  | 1249 | android::GraphicsEnv::getInstance().setTargetStats( | 
|  | 1250 | android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE); | 
|  | 1251 | } | 
|  | 1252 |  | 
| Jesse Hall | dc22507 | 2016-05-30 22:40:14 -0700 | [diff] [blame] | 1253 | data->driver_device = dev; | 
| Chia-I Wu | 4901db7 | 2016-03-24 16:38:58 +0800 | [diff] [blame] | 1254 |  | 
|  | 1255 | *pDevice = dev; | 
|  | 1256 |  | 
|  | 1257 | return VK_SUCCESS; | 
|  | 1258 | } | 
|  | 1259 |  | 
|  | 1260 | void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) { | 
|  | 1261 | DeviceData& data = GetData(device); | 
|  | 1262 | data.driver.DestroyDevice(device, pAllocator); | 
|  | 1263 |  | 
|  | 1264 | VkAllocationCallbacks local_allocator; | 
|  | 1265 | if (!pAllocator) { | 
|  | 1266 | local_allocator = data.allocator; | 
|  | 1267 | pAllocator = &local_allocator; | 
|  | 1268 | } | 
|  | 1269 |  | 
|  | 1270 | FreeDeviceData(&data, *pAllocator); | 
|  | 1271 | } | 
|  | 1272 |  | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 1273 | VkResult EnumeratePhysicalDevices(VkInstance instance, | 
|  | 1274 | uint32_t* pPhysicalDeviceCount, | 
|  | 1275 | VkPhysicalDevice* pPhysicalDevices) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1276 | ATRACE_CALL(); | 
|  | 1277 |  | 
| Chia-I Wu | ff4a6c7 | 2016-03-24 16:05:56 +0800 | [diff] [blame] | 1278 | const auto& data = GetData(instance); | 
|  | 1279 |  | 
|  | 1280 | VkResult result = data.driver.EnumeratePhysicalDevices( | 
|  | 1281 | instance, pPhysicalDeviceCount, pPhysicalDevices); | 
|  | 1282 | if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) { | 
|  | 1283 | for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++) | 
|  | 1284 | SetData(pPhysicalDevices[i], data); | 
|  | 1285 | } | 
|  | 1286 |  | 
|  | 1287 | return result; | 
|  | 1288 | } | 
|  | 1289 |  | 
| Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1290 | VkResult EnumeratePhysicalDeviceGroups( | 
|  | 1291 | VkInstance instance, | 
|  | 1292 | uint32_t* pPhysicalDeviceGroupCount, | 
|  | 1293 | VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1294 | ATRACE_CALL(); | 
|  | 1295 |  | 
| Yiwei Zhang | 4cd9cc9 | 2018-01-08 17:55:50 -0800 | [diff] [blame] | 1296 | VkResult result = VK_SUCCESS; | 
| Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1297 | const auto& data = GetData(instance); | 
|  | 1298 |  | 
| Yiwei Zhang | e4f6417 | 2020-07-05 15:17:32 -0700 | [diff] [blame] | 1299 | if (!data.driver.EnumeratePhysicalDeviceGroups && | 
|  | 1300 | !data.driver.EnumeratePhysicalDeviceGroupsKHR) { | 
| Yiwei Zhang | 4cd9cc9 | 2018-01-08 17:55:50 -0800 | [diff] [blame] | 1301 | uint32_t device_count = 0; | 
|  | 1302 | result = EnumeratePhysicalDevices(instance, &device_count, nullptr); | 
|  | 1303 | if (result < 0) | 
|  | 1304 | return result; | 
| Chad Versace | 32c087f | 2018-09-09 07:28:05 -0700 | [diff] [blame] | 1305 |  | 
| Yiwei Zhang | 4cd9cc9 | 2018-01-08 17:55:50 -0800 | [diff] [blame] | 1306 | if (!pPhysicalDeviceGroupProperties) { | 
|  | 1307 | *pPhysicalDeviceGroupCount = device_count; | 
|  | 1308 | return result; | 
|  | 1309 | } | 
|  | 1310 |  | 
| Yiwei Zhang | 4cd9cc9 | 2018-01-08 17:55:50 -0800 | [diff] [blame] | 1311 | if (!device_count) { | 
|  | 1312 | *pPhysicalDeviceGroupCount = 0; | 
|  | 1313 | return result; | 
|  | 1314 | } | 
| Chad Versace | 32c087f | 2018-09-09 07:28:05 -0700 | [diff] [blame] | 1315 | device_count = std::min(device_count, *pPhysicalDeviceGroupCount); | 
|  | 1316 | if (!device_count) | 
|  | 1317 | return VK_INCOMPLETE; | 
| Yiwei Zhang | 4cd9cc9 | 2018-01-08 17:55:50 -0800 | [diff] [blame] | 1318 |  | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 1319 | std::vector<VkPhysicalDevice> devices(device_count); | 
| Chad Versace | 32c087f | 2018-09-09 07:28:05 -0700 | [diff] [blame] | 1320 | *pPhysicalDeviceGroupCount = device_count; | 
| Yiwei Zhang | 5e86220 | 2019-06-21 14:59:16 -0700 | [diff] [blame] | 1321 | result = | 
|  | 1322 | EnumeratePhysicalDevices(instance, &device_count, devices.data()); | 
| Yiwei Zhang | 4cd9cc9 | 2018-01-08 17:55:50 -0800 | [diff] [blame] | 1323 | if (result < 0) | 
|  | 1324 | return result; | 
|  | 1325 |  | 
| Yiwei Zhang | 4cd9cc9 | 2018-01-08 17:55:50 -0800 | [diff] [blame] | 1326 | for (uint32_t i = 0; i < device_count; ++i) { | 
|  | 1327 | pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1; | 
|  | 1328 | pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i]; | 
|  | 1329 | pPhysicalDeviceGroupProperties[i].subsetAllocation = 0; | 
|  | 1330 | } | 
|  | 1331 | } else { | 
| Yiwei Zhang | e4f6417 | 2020-07-05 15:17:32 -0700 | [diff] [blame] | 1332 | if (data.driver.EnumeratePhysicalDeviceGroups) { | 
|  | 1333 | result = data.driver.EnumeratePhysicalDeviceGroups( | 
|  | 1334 | instance, pPhysicalDeviceGroupCount, | 
|  | 1335 | pPhysicalDeviceGroupProperties); | 
|  | 1336 | } else { | 
|  | 1337 | result = data.driver.EnumeratePhysicalDeviceGroupsKHR( | 
|  | 1338 | instance, pPhysicalDeviceGroupCount, | 
|  | 1339 | pPhysicalDeviceGroupProperties); | 
|  | 1340 | } | 
| Yiwei Zhang | 4cd9cc9 | 2018-01-08 17:55:50 -0800 | [diff] [blame] | 1341 | if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && | 
|  | 1342 | *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) { | 
|  | 1343 | for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) { | 
|  | 1344 | for (uint32_t j = 0; | 
|  | 1345 | j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount; | 
|  | 1346 | j++) { | 
|  | 1347 | SetData( | 
|  | 1348 | pPhysicalDeviceGroupProperties[i].physicalDevices[j], | 
| Ian Elliott | cd8ad33 | 2017-10-13 09:21:12 -0600 | [diff] [blame] | 1349 | data); | 
| Yiwei Zhang | 4cd9cc9 | 2018-01-08 17:55:50 -0800 | [diff] [blame] | 1350 | } | 
| Ian Elliott | cd8ad33 | 2017-10-13 09:21:12 -0600 | [diff] [blame] | 1351 | } | 
|  | 1352 | } | 
| Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1353 | } | 
|  | 1354 |  | 
|  | 1355 | return result; | 
|  | 1356 | } | 
|  | 1357 |  | 
| Chia-I Wu | ba0be41 | 2016-03-24 16:24:40 +0800 | [diff] [blame] | 1358 | void GetDeviceQueue(VkDevice device, | 
|  | 1359 | uint32_t queueFamilyIndex, | 
|  | 1360 | uint32_t queueIndex, | 
|  | 1361 | VkQueue* pQueue) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1362 | ATRACE_CALL(); | 
|  | 1363 |  | 
| Chia-I Wu | ba0be41 | 2016-03-24 16:24:40 +0800 | [diff] [blame] | 1364 | const auto& data = GetData(device); | 
|  | 1365 |  | 
|  | 1366 | data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue); | 
|  | 1367 | SetData(*pQueue, data); | 
|  | 1368 | } | 
|  | 1369 |  | 
| Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1370 | void GetDeviceQueue2(VkDevice device, | 
|  | 1371 | const VkDeviceQueueInfo2* pQueueInfo, | 
|  | 1372 | VkQueue* pQueue) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1373 | ATRACE_CALL(); | 
|  | 1374 |  | 
| Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1375 | const auto& data = GetData(device); | 
|  | 1376 |  | 
|  | 1377 | data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue); | 
| Yiwei Zhang | f5b9f73 | 2018-02-07 14:06:09 -0800 | [diff] [blame] | 1378 | if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data); | 
| Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1379 | } | 
|  | 1380 |  | 
| Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 1381 | VkResult AllocateCommandBuffers( | 
|  | 1382 | VkDevice device, | 
|  | 1383 | const VkCommandBufferAllocateInfo* pAllocateInfo, | 
|  | 1384 | VkCommandBuffer* pCommandBuffers) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1385 | ATRACE_CALL(); | 
|  | 1386 |  | 
| Chia-I Wu | 6a58a8a | 2016-03-24 16:29:51 +0800 | [diff] [blame] | 1387 | const auto& data = GetData(device); | 
|  | 1388 |  | 
|  | 1389 | VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo, | 
|  | 1390 | pCommandBuffers); | 
|  | 1391 | if (result == VK_SUCCESS) { | 
|  | 1392 | for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++) | 
|  | 1393 | SetData(pCommandBuffers[i], data); | 
|  | 1394 | } | 
|  | 1395 |  | 
|  | 1396 | return result; | 
|  | 1397 | } | 
|  | 1398 |  | 
| Yiwei Zhang | 93b521c | 2020-07-11 16:32:09 -0700 | [diff] [blame] | 1399 | VkResult QueueSubmit(VkQueue queue, | 
|  | 1400 | uint32_t submitCount, | 
|  | 1401 | const VkSubmitInfo* pSubmits, | 
|  | 1402 | VkFence fence) { | 
| Yiwei Zhang | 899d175 | 2019-09-23 16:05:35 -0700 | [diff] [blame] | 1403 | ATRACE_CALL(); | 
|  | 1404 |  | 
|  | 1405 | const auto& data = GetData(queue); | 
|  | 1406 |  | 
|  | 1407 | return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence); | 
|  | 1408 | } | 
|  | 1409 |  | 
| Yiwei Zhang | a55624b | 2020-07-05 16:05:26 -0700 | [diff] [blame] | 1410 | void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice, | 
|  | 1411 | VkPhysicalDeviceFeatures2* pFeatures) { | 
|  | 1412 | ATRACE_CALL(); | 
|  | 1413 |  | 
|  | 1414 | const auto& driver = GetData(physicalDevice).driver; | 
|  | 1415 |  | 
|  | 1416 | if (driver.GetPhysicalDeviceFeatures2) { | 
|  | 1417 | driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures); | 
|  | 1418 | return; | 
|  | 1419 | } | 
|  | 1420 |  | 
|  | 1421 | driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures); | 
|  | 1422 | } | 
|  | 1423 |  | 
|  | 1424 | void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice, | 
|  | 1425 | VkPhysicalDeviceProperties2* pProperties) { | 
|  | 1426 | ATRACE_CALL(); | 
|  | 1427 |  | 
|  | 1428 | const auto& driver = GetData(physicalDevice).driver; | 
|  | 1429 |  | 
|  | 1430 | if (driver.GetPhysicalDeviceProperties2) { | 
|  | 1431 | driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties); | 
|  | 1432 | return; | 
|  | 1433 | } | 
|  | 1434 |  | 
|  | 1435 | driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties); | 
|  | 1436 | } | 
|  | 1437 |  | 
|  | 1438 | void GetPhysicalDeviceFormatProperties2( | 
|  | 1439 | VkPhysicalDevice physicalDevice, | 
|  | 1440 | VkFormat format, | 
|  | 1441 | VkFormatProperties2* pFormatProperties) { | 
|  | 1442 | ATRACE_CALL(); | 
|  | 1443 |  | 
|  | 1444 | const auto& driver = GetData(physicalDevice).driver; | 
|  | 1445 |  | 
|  | 1446 | if (driver.GetPhysicalDeviceFormatProperties2) { | 
|  | 1447 | driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format, | 
|  | 1448 | pFormatProperties); | 
|  | 1449 | return; | 
|  | 1450 | } | 
|  | 1451 |  | 
|  | 1452 | driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format, | 
|  | 1453 | pFormatProperties); | 
|  | 1454 | } | 
|  | 1455 |  | 
|  | 1456 | VkResult GetPhysicalDeviceImageFormatProperties2( | 
|  | 1457 | VkPhysicalDevice physicalDevice, | 
|  | 1458 | const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo, | 
|  | 1459 | VkImageFormatProperties2* pImageFormatProperties) { | 
|  | 1460 | ATRACE_CALL(); | 
|  | 1461 |  | 
|  | 1462 | const auto& driver = GetData(physicalDevice).driver; | 
|  | 1463 |  | 
|  | 1464 | if (driver.GetPhysicalDeviceImageFormatProperties2) { | 
|  | 1465 | return driver.GetPhysicalDeviceImageFormatProperties2( | 
|  | 1466 | physicalDevice, pImageFormatInfo, pImageFormatProperties); | 
|  | 1467 | } | 
|  | 1468 |  | 
|  | 1469 | return driver.GetPhysicalDeviceImageFormatProperties2KHR( | 
|  | 1470 | physicalDevice, pImageFormatInfo, pImageFormatProperties); | 
|  | 1471 | } | 
|  | 1472 |  | 
|  | 1473 | void GetPhysicalDeviceQueueFamilyProperties2( | 
|  | 1474 | VkPhysicalDevice physicalDevice, | 
|  | 1475 | uint32_t* pQueueFamilyPropertyCount, | 
|  | 1476 | VkQueueFamilyProperties2* pQueueFamilyProperties) { | 
|  | 1477 | ATRACE_CALL(); | 
|  | 1478 |  | 
|  | 1479 | const auto& driver = GetData(physicalDevice).driver; | 
|  | 1480 |  | 
|  | 1481 | if (driver.GetPhysicalDeviceQueueFamilyProperties2) { | 
|  | 1482 | driver.GetPhysicalDeviceQueueFamilyProperties2( | 
|  | 1483 | physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties); | 
|  | 1484 | return; | 
|  | 1485 | } | 
|  | 1486 |  | 
|  | 1487 | driver.GetPhysicalDeviceQueueFamilyProperties2KHR( | 
|  | 1488 | physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties); | 
|  | 1489 | } | 
|  | 1490 |  | 
|  | 1491 | void GetPhysicalDeviceMemoryProperties2( | 
|  | 1492 | VkPhysicalDevice physicalDevice, | 
|  | 1493 | VkPhysicalDeviceMemoryProperties2* pMemoryProperties) { | 
|  | 1494 | ATRACE_CALL(); | 
|  | 1495 |  | 
|  | 1496 | const auto& driver = GetData(physicalDevice).driver; | 
|  | 1497 |  | 
|  | 1498 | if (driver.GetPhysicalDeviceMemoryProperties2) { | 
|  | 1499 | driver.GetPhysicalDeviceMemoryProperties2(physicalDevice, | 
|  | 1500 | pMemoryProperties); | 
|  | 1501 | return; | 
|  | 1502 | } | 
|  | 1503 |  | 
|  | 1504 | driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice, | 
|  | 1505 | pMemoryProperties); | 
|  | 1506 | } | 
|  | 1507 |  | 
|  | 1508 | void GetPhysicalDeviceSparseImageFormatProperties2( | 
|  | 1509 | VkPhysicalDevice physicalDevice, | 
|  | 1510 | const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo, | 
|  | 1511 | uint32_t* pPropertyCount, | 
|  | 1512 | VkSparseImageFormatProperties2* pProperties) { | 
|  | 1513 | ATRACE_CALL(); | 
|  | 1514 |  | 
|  | 1515 | const auto& driver = GetData(physicalDevice).driver; | 
|  | 1516 |  | 
|  | 1517 | if (driver.GetPhysicalDeviceSparseImageFormatProperties2) { | 
|  | 1518 | driver.GetPhysicalDeviceSparseImageFormatProperties2( | 
|  | 1519 | physicalDevice, pFormatInfo, pPropertyCount, pProperties); | 
|  | 1520 | return; | 
|  | 1521 | } | 
|  | 1522 |  | 
|  | 1523 | driver.GetPhysicalDeviceSparseImageFormatProperties2KHR( | 
|  | 1524 | physicalDevice, pFormatInfo, pPropertyCount, pProperties); | 
|  | 1525 | } | 
|  | 1526 |  | 
| Yiwei Zhang | e1f3501 | 2020-07-05 22:52:04 -0700 | [diff] [blame] | 1527 | void GetPhysicalDeviceExternalBufferProperties( | 
|  | 1528 | VkPhysicalDevice physicalDevice, | 
|  | 1529 | const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo, | 
|  | 1530 | VkExternalBufferProperties* pExternalBufferProperties) { | 
|  | 1531 | ATRACE_CALL(); | 
|  | 1532 |  | 
|  | 1533 | const auto& driver = GetData(physicalDevice).driver; | 
|  | 1534 |  | 
|  | 1535 | if (driver.GetPhysicalDeviceExternalBufferProperties) { | 
|  | 1536 | driver.GetPhysicalDeviceExternalBufferProperties( | 
|  | 1537 | physicalDevice, pExternalBufferInfo, pExternalBufferProperties); | 
|  | 1538 | return; | 
|  | 1539 | } | 
|  | 1540 |  | 
|  | 1541 | if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) { | 
|  | 1542 | driver.GetPhysicalDeviceExternalBufferPropertiesKHR( | 
|  | 1543 | physicalDevice, pExternalBufferInfo, pExternalBufferProperties); | 
|  | 1544 | return; | 
|  | 1545 | } | 
|  | 1546 |  | 
|  | 1547 | memset(&pExternalBufferProperties->externalMemoryProperties, 0, | 
|  | 1548 | sizeof(VkExternalMemoryProperties)); | 
|  | 1549 | } | 
|  | 1550 |  | 
|  | 1551 | void GetPhysicalDeviceExternalSemaphoreProperties( | 
|  | 1552 | VkPhysicalDevice physicalDevice, | 
|  | 1553 | const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo, | 
|  | 1554 | VkExternalSemaphoreProperties* pExternalSemaphoreProperties) { | 
|  | 1555 | ATRACE_CALL(); | 
|  | 1556 |  | 
|  | 1557 | const auto& driver = GetData(physicalDevice).driver; | 
|  | 1558 |  | 
|  | 1559 | if (driver.GetPhysicalDeviceExternalSemaphoreProperties) { | 
|  | 1560 | driver.GetPhysicalDeviceExternalSemaphoreProperties( | 
|  | 1561 | physicalDevice, pExternalSemaphoreInfo, | 
|  | 1562 | pExternalSemaphoreProperties); | 
|  | 1563 | return; | 
|  | 1564 | } | 
|  | 1565 |  | 
|  | 1566 | if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) { | 
|  | 1567 | driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR( | 
|  | 1568 | physicalDevice, pExternalSemaphoreInfo, | 
|  | 1569 | pExternalSemaphoreProperties); | 
|  | 1570 | return; | 
|  | 1571 | } | 
|  | 1572 |  | 
|  | 1573 | pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0; | 
|  | 1574 | pExternalSemaphoreProperties->compatibleHandleTypes = 0; | 
|  | 1575 | pExternalSemaphoreProperties->externalSemaphoreFeatures = 0; | 
|  | 1576 | } | 
|  | 1577 |  | 
|  | 1578 | void GetPhysicalDeviceExternalFenceProperties( | 
|  | 1579 | VkPhysicalDevice physicalDevice, | 
|  | 1580 | const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo, | 
|  | 1581 | VkExternalFenceProperties* pExternalFenceProperties) { | 
|  | 1582 | ATRACE_CALL(); | 
|  | 1583 |  | 
|  | 1584 | const auto& driver = GetData(physicalDevice).driver; | 
|  | 1585 |  | 
|  | 1586 | if (driver.GetPhysicalDeviceExternalFenceProperties) { | 
|  | 1587 | driver.GetPhysicalDeviceExternalFenceProperties( | 
|  | 1588 | physicalDevice, pExternalFenceInfo, pExternalFenceProperties); | 
|  | 1589 | return; | 
|  | 1590 | } | 
|  | 1591 |  | 
|  | 1592 | if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) { | 
|  | 1593 | driver.GetPhysicalDeviceExternalFencePropertiesKHR( | 
|  | 1594 | physicalDevice, pExternalFenceInfo, pExternalFenceProperties); | 
|  | 1595 | return; | 
|  | 1596 | } | 
|  | 1597 |  | 
|  | 1598 | pExternalFenceProperties->exportFromImportedHandleTypes = 0; | 
|  | 1599 | pExternalFenceProperties->compatibleHandleTypes = 0; | 
|  | 1600 | pExternalFenceProperties->externalFenceFeatures = 0; | 
|  | 1601 | } | 
|  | 1602 |  | 
| Chia-I Wu | 9d51816 | 2016-03-24 14:55:27 +0800 | [diff] [blame] | 1603 | }  // namespace driver | 
|  | 1604 | }  // namespace vulkan |