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