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