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