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