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