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