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