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