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