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