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