| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +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 |  | 
|  | 17 | // The API layer of the loader defines Vulkan API and manages layers.  The | 
|  | 18 | // entrypoints are generated and defined in api_dispatch.cpp.  Most of them | 
|  | 19 | // simply find the dispatch table and jump. | 
|  | 20 | // | 
|  | 21 | // There are a few of them requiring manual code for things such as layer | 
|  | 22 | // discovery or chaining.  They call into functions defined in this file. | 
|  | 23 |  | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 24 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 25 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 26 | #include <stdlib.h> | 
|  | 27 | #include <string.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 28 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 29 | #include <algorithm> | 
|  | 30 | #include <mutex> | 
|  | 31 | #include <new> | 
| Peiyong Lin | 8f4435a | 2020-03-11 17:43:28 -0700 | [diff] [blame] | 32 | #include <string> | 
|  | 33 | #include <unordered_set> | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 34 | #include <utility> | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 35 |  | 
| Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 36 | #include <android-base/properties.h> | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 37 | #include <android-base/strings.h> | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 38 | #include <cutils/properties.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 39 | #include <log/log.h> | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 40 | #include <utils/Trace.h> | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 41 |  | 
|  | 42 | #include <vulkan/vk_layer_interface.h> | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 43 | #include <graphicsenv/GraphicsEnv.h> | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 44 | #include "api.h" | 
|  | 45 | #include "driver.h" | 
| Chia-I Wu | c96880f | 2016-03-26 06:56:45 +0800 | [diff] [blame] | 46 | #include "layers_extensions.h" | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 47 |  | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 48 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 49 | namespace vulkan { | 
|  | 50 | namespace api { | 
|  | 51 |  | 
|  | 52 | namespace { | 
|  | 53 |  | 
|  | 54 | // Provide overridden layer names when there are implicit layers.  No effect | 
|  | 55 | // otherwise. | 
|  | 56 | class OverrideLayerNames { | 
|  | 57 | public: | 
|  | 58 | OverrideLayerNames(bool is_instance, const VkAllocationCallbacks& allocator) | 
|  | 59 | : is_instance_(is_instance), | 
|  | 60 | allocator_(allocator), | 
|  | 61 | scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND), | 
|  | 62 | names_(nullptr), | 
|  | 63 | name_count_(0), | 
|  | 64 | implicit_layers_() { | 
|  | 65 | implicit_layers_.result = VK_SUCCESS; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | ~OverrideLayerNames() { | 
|  | 69 | allocator_.pfnFree(allocator_.pUserData, names_); | 
|  | 70 | allocator_.pfnFree(allocator_.pUserData, implicit_layers_.elements); | 
|  | 71 | allocator_.pfnFree(allocator_.pUserData, implicit_layers_.name_pool); | 
|  | 72 | } | 
|  | 73 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 74 | VkResult Parse(const char* const* names, uint32_t count) { | 
|  | 75 | AddImplicitLayers(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 76 |  | 
|  | 77 | const auto& arr = implicit_layers_; | 
|  | 78 | if (arr.result != VK_SUCCESS) | 
|  | 79 | return arr.result; | 
|  | 80 |  | 
|  | 81 | // no need to override when there is no implicit layer | 
|  | 82 | if (!arr.count) | 
|  | 83 | return VK_SUCCESS; | 
|  | 84 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 85 | names_ = AllocateNameArray(arr.count + count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 86 | if (!names_) | 
|  | 87 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 88 |  | 
|  | 89 | // add implicit layer names | 
|  | 90 | for (uint32_t i = 0; i < arr.count; i++) | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 91 | names_[i] = GetImplicitLayerName(i); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 92 |  | 
|  | 93 | name_count_ = arr.count; | 
|  | 94 |  | 
|  | 95 | // add explicit layer names | 
|  | 96 | for (uint32_t i = 0; i < count; i++) { | 
|  | 97 | // ignore explicit layers that are also implicit | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 98 | if (IsImplicitLayer(names[i])) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 99 | continue; | 
|  | 100 |  | 
|  | 101 | names_[name_count_++] = names[i]; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | return VK_SUCCESS; | 
|  | 105 | } | 
|  | 106 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 107 | const char* const* Names() const { return names_; } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 108 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 109 | uint32_t Count() const { return name_count_; } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 110 |  | 
|  | 111 | private: | 
|  | 112 | struct ImplicitLayer { | 
|  | 113 | int priority; | 
|  | 114 | size_t name_offset; | 
|  | 115 | }; | 
|  | 116 |  | 
|  | 117 | struct ImplicitLayerArray { | 
|  | 118 | ImplicitLayer* elements; | 
|  | 119 | uint32_t max_count; | 
|  | 120 | uint32_t count; | 
|  | 121 |  | 
|  | 122 | char* name_pool; | 
|  | 123 | size_t max_pool_size; | 
|  | 124 | size_t pool_size; | 
|  | 125 |  | 
|  | 126 | VkResult result; | 
|  | 127 | }; | 
|  | 128 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 129 | void AddImplicitLayers() { | 
| Yiwei Zhang | 279df62 | 2020-02-18 10:45:15 -0800 | [diff] [blame] | 130 | if (!is_instance_) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 131 | return; | 
|  | 132 |  | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 133 | GetLayersFromSettings(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 134 |  | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 135 | // If no layers specified via Settings, check legacy properties | 
|  | 136 | if (implicit_layers_.count <= 0) { | 
|  | 137 | ParseDebugVulkanLayers(); | 
| Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 138 | ParseDebugVulkanLayer(); | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 139 |  | 
|  | 140 | // sort by priorities | 
|  | 141 | auto& arr = implicit_layers_; | 
|  | 142 | std::sort(arr.elements, arr.elements + arr.count, | 
|  | 143 | [](const ImplicitLayer& a, const ImplicitLayer& b) { | 
|  | 144 | return (a.priority < b.priority); | 
|  | 145 | }); | 
|  | 146 | } | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | void GetLayersFromSettings() { | 
| Yiwei Zhang | 94b18d5 | 2019-11-07 17:12:11 -0800 | [diff] [blame] | 150 | // These will only be available if conditions are met in GraphicsEnvironment | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 151 | // gpu_debug_layers = layer1:layer2:layerN | 
|  | 152 | const std::string layers = android::GraphicsEnv::getInstance().getDebugLayers(); | 
|  | 153 | if (!layers.empty()) { | 
|  | 154 | ALOGV("Debug layer list: %s", layers.c_str()); | 
|  | 155 | std::vector<std::string> paths = android::base::Split(layers, ":"); | 
|  | 156 | for (uint32_t i = 0; i < paths.size(); i++) { | 
|  | 157 | AddImplicitLayer(int(i), paths[i].c_str(), paths[i].length()); | 
|  | 158 | } | 
|  | 159 | } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 160 | } | 
|  | 161 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 162 | void ParseDebugVulkanLayers() { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 163 | // debug.vulkan.layers specifies colon-separated layer names | 
|  | 164 | char prop[PROPERTY_VALUE_MAX]; | 
|  | 165 | if (!property_get("debug.vulkan.layers", prop, "")) | 
|  | 166 | return; | 
|  | 167 |  | 
|  | 168 | // assign negative/high priorities to them | 
|  | 169 | int prio = -PROPERTY_VALUE_MAX; | 
|  | 170 |  | 
|  | 171 | const char* p = prop; | 
|  | 172 | const char* delim; | 
|  | 173 | while ((delim = strchr(p, ':'))) { | 
|  | 174 | if (delim > p) | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 175 | AddImplicitLayer(prio, p, static_cast<size_t>(delim - p)); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 176 |  | 
|  | 177 | prio++; | 
|  | 178 | p = delim + 1; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | if (p[0] != '\0') | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 182 | AddImplicitLayer(prio, p, strlen(p)); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 183 | } | 
|  | 184 |  | 
| Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 185 | void ParseDebugVulkanLayer() { | 
|  | 186 | // Checks for consecutive debug.vulkan.layer.<priority> system | 
|  | 187 | // properties after always checking an initial fixed range. | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 188 | static const char prefix[] = "debug.vulkan.layer."; | 
| Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 189 | static constexpr int kFixedRangeBeginInclusive = 0; | 
|  | 190 | static constexpr int kFixedRangeEndInclusive = 9; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 191 |  | 
| Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 192 | bool logged = false; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 193 |  | 
| Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 194 | int priority = kFixedRangeBeginInclusive; | 
|  | 195 | while (true) { | 
|  | 196 | const std::string prop_key = | 
|  | 197 | std::string(prefix) + std::to_string(priority); | 
|  | 198 | const std::string prop_val = | 
|  | 199 | android::base::GetProperty(prop_key, ""); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 200 |  | 
| Jason Macnak | a8baab0 | 2021-02-19 12:53:47 -0800 | [diff] [blame] | 201 | if (!prop_val.empty()) { | 
|  | 202 | if (!logged) { | 
|  | 203 | ALOGI( | 
|  | 204 | "Detected Vulkan layers configured with " | 
|  | 205 | "debug.vulkan.layer.<priority>. Checking for " | 
|  | 206 | "debug.vulkan.layer.<priority> in the range [%d, %d] " | 
|  | 207 | "followed by a consecutive scan.", | 
|  | 208 | kFixedRangeBeginInclusive, kFixedRangeEndInclusive); | 
|  | 209 | logged = true; | 
|  | 210 | } | 
|  | 211 | AddImplicitLayer(priority, prop_val.c_str(), prop_val.length()); | 
|  | 212 | } else if (priority >= kFixedRangeEndInclusive) { | 
|  | 213 | return; | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | ++priority; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 217 | } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 220 | void AddImplicitLayer(int priority, const char* name, size_t len) { | 
|  | 221 | if (!GrowImplicitLayerArray(1, 0)) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 222 | return; | 
|  | 223 |  | 
|  | 224 | auto& arr = implicit_layers_; | 
|  | 225 | auto& layer = arr.elements[arr.count++]; | 
|  | 226 |  | 
|  | 227 | layer.priority = priority; | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 228 | layer.name_offset = AddImplicitLayerName(name, len); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 229 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 230 | ALOGV("Added implicit layer %s", GetImplicitLayerName(arr.count - 1)); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 231 | } | 
|  | 232 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 233 | size_t AddImplicitLayerName(const char* name, size_t len) { | 
|  | 234 | if (!GrowImplicitLayerArray(0, len + 1)) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 235 | return 0; | 
|  | 236 |  | 
|  | 237 | // add the name to the pool | 
|  | 238 | auto& arr = implicit_layers_; | 
|  | 239 | size_t offset = arr.pool_size; | 
|  | 240 | char* dst = arr.name_pool + offset; | 
|  | 241 |  | 
|  | 242 | std::copy(name, name + len, dst); | 
|  | 243 | dst[len] = '\0'; | 
|  | 244 |  | 
|  | 245 | arr.pool_size += len + 1; | 
|  | 246 |  | 
|  | 247 | return offset; | 
|  | 248 | } | 
|  | 249 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 250 | bool GrowImplicitLayerArray(uint32_t layer_count, size_t name_size) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 251 | const uint32_t initial_max_count = 16; | 
|  | 252 | const size_t initial_max_pool_size = 512; | 
|  | 253 |  | 
|  | 254 | auto& arr = implicit_layers_; | 
|  | 255 |  | 
|  | 256 | // grow the element array if needed | 
|  | 257 | while (arr.count + layer_count > arr.max_count) { | 
|  | 258 | uint32_t new_max_count = | 
|  | 259 | (arr.max_count) ? (arr.max_count << 1) : initial_max_count; | 
|  | 260 | void* new_mem = nullptr; | 
|  | 261 |  | 
|  | 262 | if (new_max_count > arr.max_count) { | 
|  | 263 | new_mem = allocator_.pfnReallocation( | 
|  | 264 | allocator_.pUserData, arr.elements, | 
|  | 265 | sizeof(ImplicitLayer) * new_max_count, | 
|  | 266 | alignof(ImplicitLayer), scope_); | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | if (!new_mem) { | 
|  | 270 | arr.result = VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 271 | arr.count = 0; | 
|  | 272 | return false; | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | arr.elements = reinterpret_cast<ImplicitLayer*>(new_mem); | 
|  | 276 | arr.max_count = new_max_count; | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | // grow the name pool if needed | 
|  | 280 | while (arr.pool_size + name_size > arr.max_pool_size) { | 
|  | 281 | size_t new_max_pool_size = (arr.max_pool_size) | 
|  | 282 | ? (arr.max_pool_size << 1) | 
|  | 283 | : initial_max_pool_size; | 
|  | 284 | void* new_mem = nullptr; | 
|  | 285 |  | 
|  | 286 | if (new_max_pool_size > arr.max_pool_size) { | 
|  | 287 | new_mem = allocator_.pfnReallocation( | 
|  | 288 | allocator_.pUserData, arr.name_pool, new_max_pool_size, | 
|  | 289 | alignof(char), scope_); | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | if (!new_mem) { | 
|  | 293 | arr.result = VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 294 | arr.pool_size = 0; | 
|  | 295 | return false; | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | arr.name_pool = reinterpret_cast<char*>(new_mem); | 
|  | 299 | arr.max_pool_size = new_max_pool_size; | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | return true; | 
|  | 303 | } | 
|  | 304 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 305 | const char* GetImplicitLayerName(uint32_t index) const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 306 | const auto& arr = implicit_layers_; | 
|  | 307 |  | 
|  | 308 | // this may return nullptr when arr.result is not VK_SUCCESS | 
|  | 309 | return implicit_layers_.name_pool + arr.elements[index].name_offset; | 
|  | 310 | } | 
|  | 311 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 312 | bool IsImplicitLayer(const char* name) const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 313 | const auto& arr = implicit_layers_; | 
|  | 314 |  | 
|  | 315 | for (uint32_t i = 0; i < arr.count; i++) { | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 316 | if (strcmp(name, GetImplicitLayerName(i)) == 0) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 317 | return true; | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | return false; | 
|  | 321 | } | 
|  | 322 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 323 | const char** AllocateNameArray(uint32_t count) const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 324 | return reinterpret_cast<const char**>(allocator_.pfnAllocation( | 
|  | 325 | allocator_.pUserData, sizeof(const char*) * count, | 
|  | 326 | alignof(const char*), scope_)); | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | const bool is_instance_; | 
|  | 330 | const VkAllocationCallbacks& allocator_; | 
|  | 331 | const VkSystemAllocationScope scope_; | 
|  | 332 |  | 
|  | 333 | const char** names_; | 
|  | 334 | uint32_t name_count_; | 
|  | 335 |  | 
|  | 336 | ImplicitLayerArray implicit_layers_; | 
|  | 337 | }; | 
|  | 338 |  | 
|  | 339 | // Provide overridden extension names when there are implicit extensions. | 
|  | 340 | // No effect otherwise. | 
|  | 341 | // | 
|  | 342 | // This is used only to enable VK_EXT_debug_report. | 
|  | 343 | class OverrideExtensionNames { | 
|  | 344 | public: | 
|  | 345 | OverrideExtensionNames(bool is_instance, | 
|  | 346 | const VkAllocationCallbacks& allocator) | 
|  | 347 | : is_instance_(is_instance), | 
|  | 348 | allocator_(allocator), | 
|  | 349 | scope_(VK_SYSTEM_ALLOCATION_SCOPE_COMMAND), | 
|  | 350 | names_(nullptr), | 
|  | 351 | name_count_(0), | 
|  | 352 | install_debug_callback_(false) {} | 
|  | 353 |  | 
|  | 354 | ~OverrideExtensionNames() { | 
|  | 355 | allocator_.pfnFree(allocator_.pUserData, names_); | 
|  | 356 | } | 
|  | 357 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 358 | VkResult Parse(const char* const* names, uint32_t count) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 359 | // this is only for debug.vulkan.enable_callback | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 360 | if (!EnableDebugCallback()) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 361 | return VK_SUCCESS; | 
|  | 362 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 363 | names_ = AllocateNameArray(count + 1); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 364 | if (!names_) | 
|  | 365 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 366 |  | 
|  | 367 | std::copy(names, names + count, names_); | 
|  | 368 |  | 
|  | 369 | name_count_ = count; | 
|  | 370 | names_[name_count_++] = "VK_EXT_debug_report"; | 
|  | 371 |  | 
|  | 372 | install_debug_callback_ = true; | 
|  | 373 |  | 
|  | 374 | return VK_SUCCESS; | 
|  | 375 | } | 
|  | 376 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 377 | const char* const* Names() const { return names_; } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 378 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 379 | uint32_t Count() const { return name_count_; } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 380 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 381 | bool InstallDebugCallback() const { return install_debug_callback_; } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 382 |  | 
|  | 383 | private: | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 384 | bool EnableDebugCallback() const { | 
| Yiwei Zhang | 6a674c9 | 2019-11-08 11:55:36 -0800 | [diff] [blame] | 385 | return (is_instance_ && | 
|  | 386 | android::GraphicsEnv::getInstance().isDebuggable() && | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 387 | property_get_bool("debug.vulkan.enable_callback", false)); | 
|  | 388 | } | 
|  | 389 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 390 | const char** AllocateNameArray(uint32_t count) const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 391 | return reinterpret_cast<const char**>(allocator_.pfnAllocation( | 
|  | 392 | allocator_.pUserData, sizeof(const char*) * count, | 
|  | 393 | alignof(const char*), scope_)); | 
|  | 394 | } | 
|  | 395 |  | 
|  | 396 | const bool is_instance_; | 
|  | 397 | const VkAllocationCallbacks& allocator_; | 
|  | 398 | const VkSystemAllocationScope scope_; | 
|  | 399 |  | 
|  | 400 | const char** names_; | 
|  | 401 | uint32_t name_count_; | 
|  | 402 | bool install_debug_callback_; | 
|  | 403 | }; | 
|  | 404 |  | 
|  | 405 | // vkCreateInstance and vkCreateDevice helpers with support for layer | 
|  | 406 | // chaining. | 
|  | 407 | class LayerChain { | 
|  | 408 | public: | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 409 | struct ActiveLayer { | 
|  | 410 | LayerRef ref; | 
|  | 411 | union { | 
|  | 412 | VkLayerInstanceLink instance_link; | 
|  | 413 | VkLayerDeviceLink device_link; | 
|  | 414 | }; | 
|  | 415 | }; | 
|  | 416 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 417 | static VkResult CreateInstance(const VkInstanceCreateInfo* create_info, | 
|  | 418 | const VkAllocationCallbacks* allocator, | 
|  | 419 | VkInstance* instance_out); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 420 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 421 | static VkResult CreateDevice(VkPhysicalDevice physical_dev, | 
|  | 422 | const VkDeviceCreateInfo* create_info, | 
|  | 423 | const VkAllocationCallbacks* allocator, | 
|  | 424 | VkDevice* dev_out); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 425 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 426 | static void DestroyInstance(VkInstance instance, | 
|  | 427 | const VkAllocationCallbacks* allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 428 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 429 | static void DestroyDevice(VkDevice dev, | 
|  | 430 | const VkAllocationCallbacks* allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 431 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 432 | static const ActiveLayer* GetActiveLayers(VkPhysicalDevice physical_dev, | 
|  | 433 | uint32_t& count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 434 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 435 | private: | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 436 | LayerChain(bool is_instance, | 
|  | 437 | const driver::DebugReportLogger& logger, | 
|  | 438 | const VkAllocationCallbacks& allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 439 | ~LayerChain(); | 
|  | 440 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 441 | VkResult ActivateLayers(const char* const* layer_names, | 
|  | 442 | uint32_t layer_count, | 
|  | 443 | const char* const* extension_names, | 
|  | 444 | uint32_t extension_count); | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 445 | VkResult ActivateLayers(VkPhysicalDevice physical_dev, | 
|  | 446 | const char* const* layer_names, | 
|  | 447 | uint32_t layer_count, | 
|  | 448 | const char* const* extension_names, | 
|  | 449 | uint32_t extension_count); | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 450 | ActiveLayer* AllocateLayerArray(uint32_t count) const; | 
|  | 451 | VkResult LoadLayer(ActiveLayer& layer, const char* name); | 
|  | 452 | void SetupLayerLinks(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 453 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 454 | bool Empty() const; | 
|  | 455 | void ModifyCreateInfo(VkInstanceCreateInfo& info); | 
|  | 456 | void ModifyCreateInfo(VkDeviceCreateInfo& info); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 457 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 458 | VkResult Create(const VkInstanceCreateInfo* create_info, | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 459 | const VkAllocationCallbacks* allocator, | 
|  | 460 | VkInstance* instance_out); | 
|  | 461 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 462 | VkResult Create(VkPhysicalDevice physical_dev, | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 463 | const VkDeviceCreateInfo* create_info, | 
|  | 464 | const VkAllocationCallbacks* allocator, | 
|  | 465 | VkDevice* dev_out); | 
|  | 466 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 467 | VkResult ValidateExtensions(const char* const* extension_names, | 
|  | 468 | uint32_t extension_count); | 
|  | 469 | VkResult ValidateExtensions(VkPhysicalDevice physical_dev, | 
|  | 470 | const char* const* extension_names, | 
|  | 471 | uint32_t extension_count); | 
|  | 472 | VkExtensionProperties* AllocateDriverExtensionArray(uint32_t count) const; | 
|  | 473 | bool IsLayerExtension(const char* name) const; | 
|  | 474 | bool IsDriverExtension(const char* name) const; | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 475 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 476 | template <typename DataType> | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 477 | void StealLayers(DataType& data); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 478 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 479 | static void DestroyLayers(ActiveLayer* layers, | 
|  | 480 | uint32_t count, | 
|  | 481 | const VkAllocationCallbacks& allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 482 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 483 | static VKAPI_ATTR VkResult SetInstanceLoaderData(VkInstance instance, | 
|  | 484 | void* object); | 
|  | 485 | static VKAPI_ATTR VkResult SetDeviceLoaderData(VkDevice device, | 
|  | 486 | void* object); | 
|  | 487 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 488 | static VKAPI_ATTR VkBool32 | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 489 | DebugReportCallback(VkDebugReportFlagsEXT flags, | 
|  | 490 | VkDebugReportObjectTypeEXT obj_type, | 
|  | 491 | uint64_t obj, | 
|  | 492 | size_t location, | 
|  | 493 | int32_t msg_code, | 
|  | 494 | const char* layer_prefix, | 
|  | 495 | const char* msg, | 
|  | 496 | void* user_data); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 497 |  | 
|  | 498 | const bool is_instance_; | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 499 | const driver::DebugReportLogger& logger_; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 500 | const VkAllocationCallbacks& allocator_; | 
|  | 501 |  | 
|  | 502 | OverrideLayerNames override_layers_; | 
|  | 503 | OverrideExtensionNames override_extensions_; | 
|  | 504 |  | 
|  | 505 | ActiveLayer* layers_; | 
|  | 506 | uint32_t layer_count_; | 
|  | 507 |  | 
|  | 508 | PFN_vkGetInstanceProcAddr get_instance_proc_addr_; | 
|  | 509 | PFN_vkGetDeviceProcAddr get_device_proc_addr_; | 
|  | 510 |  | 
|  | 511 | union { | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 512 | VkLayerInstanceCreateInfo instance_chain_info_[2]; | 
|  | 513 | VkLayerDeviceCreateInfo device_chain_info_[2]; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 514 | }; | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 515 |  | 
|  | 516 | VkExtensionProperties* driver_extensions_; | 
|  | 517 | uint32_t driver_extension_count_; | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 518 | std::bitset<driver::ProcHook::EXTENSION_COUNT> enabled_extensions_; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 519 | }; | 
|  | 520 |  | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 521 | LayerChain::LayerChain(bool is_instance, | 
|  | 522 | const driver::DebugReportLogger& logger, | 
|  | 523 | const VkAllocationCallbacks& allocator) | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 524 | : is_instance_(is_instance), | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 525 | logger_(logger), | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 526 | allocator_(allocator), | 
|  | 527 | override_layers_(is_instance, allocator), | 
|  | 528 | override_extensions_(is_instance, allocator), | 
|  | 529 | layers_(nullptr), | 
|  | 530 | layer_count_(0), | 
|  | 531 | get_instance_proc_addr_(nullptr), | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 532 | get_device_proc_addr_(nullptr), | 
|  | 533 | driver_extensions_(nullptr), | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 534 | driver_extension_count_(0) { | 
| Yiwei Zhang | 7cc36a5 | 2019-10-11 19:02:09 -0700 | [diff] [blame] | 535 | // advertise the loader supported core Vulkan API version at vulkan::api | 
|  | 536 | for (uint32_t i = driver::ProcHook::EXTENSION_CORE_1_0; | 
|  | 537 | i != driver::ProcHook::EXTENSION_COUNT; ++i) { | 
|  | 538 | enabled_extensions_.set(i); | 
|  | 539 | } | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 540 | } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 541 |  | 
|  | 542 | LayerChain::~LayerChain() { | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 543 | allocator_.pfnFree(allocator_.pUserData, driver_extensions_); | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 544 | DestroyLayers(layers_, layer_count_, allocator_); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 545 | } | 
|  | 546 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 547 | VkResult LayerChain::ActivateLayers(const char* const* layer_names, | 
|  | 548 | uint32_t layer_count, | 
|  | 549 | const char* const* extension_names, | 
|  | 550 | uint32_t extension_count) { | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 551 | VkResult result = override_layers_.Parse(layer_names, layer_count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 552 | if (result != VK_SUCCESS) | 
|  | 553 | return result; | 
|  | 554 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 555 | result = override_extensions_.Parse(extension_names, extension_count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 556 | if (result != VK_SUCCESS) | 
|  | 557 | return result; | 
|  | 558 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 559 | if (override_layers_.Count()) { | 
|  | 560 | layer_names = override_layers_.Names(); | 
|  | 561 | layer_count = override_layers_.Count(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 562 | } | 
|  | 563 |  | 
|  | 564 | if (!layer_count) { | 
|  | 565 | // point head of chain to the driver | 
|  | 566 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 567 |  | 
|  | 568 | return VK_SUCCESS; | 
|  | 569 | } | 
|  | 570 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 571 | layers_ = AllocateLayerArray(layer_count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 572 | if (!layers_) | 
|  | 573 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 574 |  | 
|  | 575 | // load layers | 
|  | 576 | for (uint32_t i = 0; i < layer_count; i++) { | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 577 | result = LoadLayer(layers_[i], layer_names[i]); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 578 | if (result != VK_SUCCESS) | 
|  | 579 | return result; | 
|  | 580 |  | 
|  | 581 | // count loaded layers for proper destructions on errors | 
|  | 582 | layer_count_++; | 
|  | 583 | } | 
|  | 584 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 585 | SetupLayerLinks(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 586 |  | 
|  | 587 | return VK_SUCCESS; | 
|  | 588 | } | 
|  | 589 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 590 | VkResult LayerChain::ActivateLayers(VkPhysicalDevice physical_dev, | 
|  | 591 | const char* const* layer_names, | 
|  | 592 | uint32_t layer_count, | 
|  | 593 | const char* const* extension_names, | 
|  | 594 | uint32_t extension_count) { | 
|  | 595 | uint32_t instance_layer_count; | 
|  | 596 | const ActiveLayer* instance_layers = | 
|  | 597 | GetActiveLayers(physical_dev, instance_layer_count); | 
|  | 598 |  | 
|  | 599 | // log a message if the application device layer array is not empty nor an | 
|  | 600 | // exact match of the instance layer array. | 
|  | 601 | if (layer_count) { | 
|  | 602 | bool exact_match = (instance_layer_count == layer_count); | 
|  | 603 | if (exact_match) { | 
|  | 604 | for (uint32_t i = 0; i < instance_layer_count; i++) { | 
|  | 605 | const Layer& l = *instance_layers[i].ref; | 
|  | 606 | if (strcmp(GetLayerProperties(l).layerName, layer_names[i])) { | 
|  | 607 | exact_match = false; | 
|  | 608 | break; | 
|  | 609 | } | 
|  | 610 | } | 
|  | 611 | } | 
|  | 612 |  | 
|  | 613 | if (!exact_match) { | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 614 | logger_.Warn(physical_dev, | 
|  | 615 | "Device layers disagree with instance layers and are " | 
|  | 616 | "overridden by instance layers"); | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 617 | } | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | VkResult result = | 
|  | 621 | override_extensions_.Parse(extension_names, extension_count); | 
|  | 622 | if (result != VK_SUCCESS) | 
|  | 623 | return result; | 
|  | 624 |  | 
|  | 625 | if (!instance_layer_count) { | 
|  | 626 | // point head of chain to the driver | 
|  | 627 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; | 
|  | 628 | get_device_proc_addr_ = driver::GetDeviceProcAddr; | 
|  | 629 |  | 
|  | 630 | return VK_SUCCESS; | 
|  | 631 | } | 
|  | 632 |  | 
|  | 633 | layers_ = AllocateLayerArray(instance_layer_count); | 
|  | 634 | if (!layers_) | 
|  | 635 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 636 |  | 
|  | 637 | for (uint32_t i = 0; i < instance_layer_count; i++) { | 
|  | 638 | const Layer& l = *instance_layers[i].ref; | 
|  | 639 |  | 
|  | 640 | // no need to and cannot chain non-global layers | 
|  | 641 | if (!IsLayerGlobal(l)) | 
|  | 642 | continue; | 
|  | 643 |  | 
|  | 644 | // this never fails | 
|  | 645 | new (&layers_[layer_count_++]) ActiveLayer{GetLayerRef(l), {}}; | 
|  | 646 | } | 
|  | 647 |  | 
| Chia-I Wu | 61b25fd | 2016-05-27 10:18:25 +0800 | [diff] [blame] | 648 | // this may happen when all layers are non-global ones | 
|  | 649 | if (!layer_count_) { | 
|  | 650 | get_instance_proc_addr_ = driver::GetInstanceProcAddr; | 
|  | 651 | get_device_proc_addr_ = driver::GetDeviceProcAddr; | 
|  | 652 | return VK_SUCCESS; | 
|  | 653 | } | 
|  | 654 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 655 | SetupLayerLinks(); | 
|  | 656 |  | 
|  | 657 | return VK_SUCCESS; | 
|  | 658 | } | 
|  | 659 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 660 | LayerChain::ActiveLayer* LayerChain::AllocateLayerArray(uint32_t count) const { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 661 | VkSystemAllocationScope scope = (is_instance_) | 
|  | 662 | ? VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 663 | : VK_SYSTEM_ALLOCATION_SCOPE_COMMAND; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 664 |  | 
|  | 665 | return reinterpret_cast<ActiveLayer*>(allocator_.pfnAllocation( | 
|  | 666 | allocator_.pUserData, sizeof(ActiveLayer) * count, alignof(ActiveLayer), | 
|  | 667 | scope)); | 
|  | 668 | } | 
|  | 669 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 670 | VkResult LayerChain::LoadLayer(ActiveLayer& layer, const char* name) { | 
| Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 671 | const Layer* l = FindLayer(name); | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 672 | if (!l) { | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 673 | logger_.Err(VK_NULL_HANDLE, "Failed to find layer %s", name); | 
| Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 674 | return VK_ERROR_LAYER_NOT_PRESENT; | 
|  | 675 | } | 
|  | 676 |  | 
| Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 677 | new (&layer) ActiveLayer{GetLayerRef(*l), {}}; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 678 | if (!layer.ref) { | 
| Chia-I Wu | d6e6f51 | 2016-04-28 07:39:32 +0800 | [diff] [blame] | 679 | ALOGW("Failed to open layer %s", name); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 680 | layer.ref.~LayerRef(); | 
|  | 681 | return VK_ERROR_LAYER_NOT_PRESENT; | 
|  | 682 | } | 
|  | 683 |  | 
| Adam Bodnar | 87edb95 | 2019-07-17 12:35:53 -0700 | [diff] [blame] | 684 | if (!layer.ref.GetGetInstanceProcAddr()) { | 
|  | 685 | ALOGW("Failed to locate vkGetInstanceProcAddr in layer %s", name); | 
|  | 686 | layer.ref.~LayerRef(); | 
|  | 687 | return VK_ERROR_LAYER_NOT_PRESENT; | 
|  | 688 | } | 
|  | 689 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 690 | ALOGI("Loaded layer %s", name); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 691 |  | 
|  | 692 | return VK_SUCCESS; | 
|  | 693 | } | 
|  | 694 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 695 | void LayerChain::SetupLayerLinks() { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 696 | if (is_instance_) { | 
|  | 697 | for (uint32_t i = 0; i < layer_count_; i++) { | 
|  | 698 | ActiveLayer& layer = layers_[i]; | 
|  | 699 |  | 
|  | 700 | // point head of chain to the first layer | 
|  | 701 | if (i == 0) | 
|  | 702 | get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr(); | 
|  | 703 |  | 
|  | 704 | // point tail of chain to the driver | 
|  | 705 | if (i == layer_count_ - 1) { | 
|  | 706 | layer.instance_link.pNext = nullptr; | 
|  | 707 | layer.instance_link.pfnNextGetInstanceProcAddr = | 
|  | 708 | driver::GetInstanceProcAddr; | 
|  | 709 | break; | 
|  | 710 | } | 
|  | 711 |  | 
|  | 712 | const ActiveLayer& next = layers_[i + 1]; | 
|  | 713 |  | 
|  | 714 | // const_cast as some naughty layers want to modify our links! | 
|  | 715 | layer.instance_link.pNext = | 
|  | 716 | const_cast<VkLayerInstanceLink*>(&next.instance_link); | 
|  | 717 | layer.instance_link.pfnNextGetInstanceProcAddr = | 
|  | 718 | next.ref.GetGetInstanceProcAddr(); | 
|  | 719 | } | 
|  | 720 | } else { | 
|  | 721 | for (uint32_t i = 0; i < layer_count_; i++) { | 
|  | 722 | ActiveLayer& layer = layers_[i]; | 
|  | 723 |  | 
|  | 724 | // point head of chain to the first layer | 
|  | 725 | if (i == 0) { | 
|  | 726 | get_instance_proc_addr_ = layer.ref.GetGetInstanceProcAddr(); | 
|  | 727 | get_device_proc_addr_ = layer.ref.GetGetDeviceProcAddr(); | 
|  | 728 | } | 
|  | 729 |  | 
|  | 730 | // point tail of chain to the driver | 
|  | 731 | if (i == layer_count_ - 1) { | 
|  | 732 | layer.device_link.pNext = nullptr; | 
|  | 733 | layer.device_link.pfnNextGetInstanceProcAddr = | 
|  | 734 | driver::GetInstanceProcAddr; | 
|  | 735 | layer.device_link.pfnNextGetDeviceProcAddr = | 
|  | 736 | driver::GetDeviceProcAddr; | 
|  | 737 | break; | 
|  | 738 | } | 
|  | 739 |  | 
|  | 740 | const ActiveLayer& next = layers_[i + 1]; | 
|  | 741 |  | 
|  | 742 | // const_cast as some naughty layers want to modify our links! | 
|  | 743 | layer.device_link.pNext = | 
|  | 744 | const_cast<VkLayerDeviceLink*>(&next.device_link); | 
|  | 745 | layer.device_link.pfnNextGetInstanceProcAddr = | 
|  | 746 | next.ref.GetGetInstanceProcAddr(); | 
|  | 747 | layer.device_link.pfnNextGetDeviceProcAddr = | 
|  | 748 | next.ref.GetGetDeviceProcAddr(); | 
|  | 749 | } | 
|  | 750 | } | 
|  | 751 | } | 
|  | 752 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 753 | bool LayerChain::Empty() const { | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 754 | return (!layer_count_ && !override_layers_.Count() && | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 755 | !override_extensions_.Count()); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 756 | } | 
|  | 757 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 758 | void LayerChain::ModifyCreateInfo(VkInstanceCreateInfo& info) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 759 | if (layer_count_) { | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 760 | auto& link_info = instance_chain_info_[1]; | 
|  | 761 | link_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; | 
|  | 762 | link_info.pNext = info.pNext; | 
|  | 763 | link_info.function = VK_LAYER_FUNCTION_LINK; | 
|  | 764 | link_info.u.pLayerInfo = &layers_[0].instance_link; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 765 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 766 | auto& cb_info = instance_chain_info_[0]; | 
|  | 767 | cb_info.sType = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO; | 
|  | 768 | cb_info.pNext = &link_info; | 
|  | 769 | cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK; | 
|  | 770 | cb_info.u.pfnSetInstanceLoaderData = SetInstanceLoaderData; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 771 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 772 | info.pNext = &cb_info; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 773 | } | 
|  | 774 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 775 | if (override_layers_.Count()) { | 
|  | 776 | info.enabledLayerCount = override_layers_.Count(); | 
|  | 777 | info.ppEnabledLayerNames = override_layers_.Names(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 778 | } | 
|  | 779 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 780 | if (override_extensions_.Count()) { | 
|  | 781 | info.enabledExtensionCount = override_extensions_.Count(); | 
|  | 782 | info.ppEnabledExtensionNames = override_extensions_.Names(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 783 | } | 
|  | 784 | } | 
|  | 785 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 786 | void LayerChain::ModifyCreateInfo(VkDeviceCreateInfo& info) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 787 | if (layer_count_) { | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 788 | auto& link_info = device_chain_info_[1]; | 
|  | 789 | link_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; | 
|  | 790 | link_info.pNext = info.pNext; | 
|  | 791 | link_info.function = VK_LAYER_FUNCTION_LINK; | 
|  | 792 | link_info.u.pLayerInfo = &layers_[0].device_link; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 793 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 794 | auto& cb_info = device_chain_info_[0]; | 
|  | 795 | cb_info.sType = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO; | 
|  | 796 | cb_info.pNext = &link_info; | 
|  | 797 | cb_info.function = VK_LAYER_FUNCTION_DATA_CALLBACK; | 
|  | 798 | cb_info.u.pfnSetDeviceLoaderData = SetDeviceLoaderData; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 799 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 800 | info.pNext = &cb_info; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 801 | } | 
|  | 802 |  | 
| Chia-I Wu | 026b8fa | 2016-04-11 13:44:13 +0800 | [diff] [blame] | 803 | if (override_layers_.Count()) { | 
|  | 804 | info.enabledLayerCount = override_layers_.Count(); | 
|  | 805 | info.ppEnabledLayerNames = override_layers_.Names(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 806 | } | 
|  | 807 |  | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 808 | if (override_extensions_.Count()) { | 
|  | 809 | info.enabledExtensionCount = override_extensions_.Count(); | 
|  | 810 | info.ppEnabledExtensionNames = override_extensions_.Names(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 811 | } | 
|  | 812 | } | 
|  | 813 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 814 | VkResult LayerChain::Create(const VkInstanceCreateInfo* create_info, | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 815 | const VkAllocationCallbacks* allocator, | 
|  | 816 | VkInstance* instance_out) { | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 817 | VkResult result = ValidateExtensions(create_info->ppEnabledExtensionNames, | 
|  | 818 | create_info->enabledExtensionCount); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 819 | if (result != VK_SUCCESS) | 
|  | 820 | return result; | 
|  | 821 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 822 | // call down the chain | 
|  | 823 | PFN_vkCreateInstance create_instance = | 
|  | 824 | reinterpret_cast<PFN_vkCreateInstance>( | 
|  | 825 | get_instance_proc_addr_(VK_NULL_HANDLE, "vkCreateInstance")); | 
|  | 826 | VkInstance instance; | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 827 | result = create_instance(create_info, allocator, &instance); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 828 | if (result != VK_SUCCESS) | 
|  | 829 | return result; | 
|  | 830 |  | 
|  | 831 | // initialize InstanceData | 
|  | 832 | InstanceData& data = GetData(instance); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 833 |  | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 834 | if (!InitDispatchTable(instance, get_instance_proc_addr_, | 
|  | 835 | enabled_extensions_)) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 836 | if (data.dispatch.DestroyInstance) | 
|  | 837 | data.dispatch.DestroyInstance(instance, allocator); | 
|  | 838 |  | 
|  | 839 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 840 | } | 
|  | 841 |  | 
|  | 842 | // install debug report callback | 
| Chia-I Wu | c3fa20c | 2016-04-11 13:47:31 +0800 | [diff] [blame] | 843 | if (override_extensions_.InstallDebugCallback()) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 844 | PFN_vkCreateDebugReportCallbackEXT create_debug_report_callback = | 
|  | 845 | reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>( | 
|  | 846 | get_instance_proc_addr_(instance, | 
|  | 847 | "vkCreateDebugReportCallbackEXT")); | 
|  | 848 | data.destroy_debug_callback = | 
|  | 849 | reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>( | 
|  | 850 | get_instance_proc_addr_(instance, | 
|  | 851 | "vkDestroyDebugReportCallbackEXT")); | 
|  | 852 | if (!create_debug_report_callback || !data.destroy_debug_callback) { | 
|  | 853 | ALOGE("Broken VK_EXT_debug_report support"); | 
|  | 854 | data.dispatch.DestroyInstance(instance, allocator); | 
|  | 855 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 856 | } | 
|  | 857 |  | 
|  | 858 | VkDebugReportCallbackCreateInfoEXT debug_callback_info = {}; | 
|  | 859 | debug_callback_info.sType = | 
|  | 860 | VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; | 
|  | 861 | debug_callback_info.flags = | 
|  | 862 | VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 863 | debug_callback_info.pfnCallback = DebugReportCallback; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 864 |  | 
|  | 865 | VkDebugReportCallbackEXT debug_callback; | 
|  | 866 | result = create_debug_report_callback(instance, &debug_callback_info, | 
|  | 867 | nullptr, &debug_callback); | 
|  | 868 | if (result != VK_SUCCESS) { | 
|  | 869 | ALOGE("Failed to install debug report callback"); | 
|  | 870 | data.dispatch.DestroyInstance(instance, allocator); | 
|  | 871 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 872 | } | 
|  | 873 |  | 
|  | 874 | data.debug_callback = debug_callback; | 
|  | 875 |  | 
|  | 876 | ALOGI("Installed debug report callback"); | 
|  | 877 | } | 
|  | 878 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 879 | StealLayers(data); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 880 |  | 
|  | 881 | *instance_out = instance; | 
|  | 882 |  | 
|  | 883 | return VK_SUCCESS; | 
|  | 884 | } | 
|  | 885 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 886 | VkResult LayerChain::Create(VkPhysicalDevice physical_dev, | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 887 | const VkDeviceCreateInfo* create_info, | 
|  | 888 | const VkAllocationCallbacks* allocator, | 
|  | 889 | VkDevice* dev_out) { | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 890 | VkResult result = | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 891 | ValidateExtensions(physical_dev, create_info->ppEnabledExtensionNames, | 
|  | 892 | create_info->enabledExtensionCount); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 893 | if (result != VK_SUCCESS) | 
|  | 894 | return result; | 
|  | 895 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 896 | // call down the chain | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 897 | PFN_vkCreateDevice create_device = | 
|  | 898 | GetData(physical_dev).dispatch.CreateDevice; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 899 | VkDevice dev; | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 900 | result = create_device(physical_dev, create_info, allocator, &dev); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 901 | if (result != VK_SUCCESS) | 
|  | 902 | return result; | 
|  | 903 |  | 
|  | 904 | // initialize DeviceData | 
|  | 905 | DeviceData& data = GetData(dev); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 906 |  | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 907 | if (!InitDispatchTable(dev, get_device_proc_addr_, enabled_extensions_)) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 908 | if (data.dispatch.DestroyDevice) | 
|  | 909 | data.dispatch.DestroyDevice(dev, allocator); | 
|  | 910 |  | 
|  | 911 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 912 | } | 
|  | 913 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 914 | // no StealLayers so that active layers are destroyed with this | 
|  | 915 | // LayerChain | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 916 | *dev_out = dev; | 
|  | 917 |  | 
|  | 918 | return VK_SUCCESS; | 
|  | 919 | } | 
|  | 920 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 921 | VkResult LayerChain::ValidateExtensions(const char* const* extension_names, | 
|  | 922 | uint32_t extension_count) { | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 923 | if (!extension_count) | 
|  | 924 | return VK_SUCCESS; | 
|  | 925 |  | 
|  | 926 | // query driver instance extensions | 
|  | 927 | uint32_t count; | 
|  | 928 | VkResult result = | 
|  | 929 | EnumerateInstanceExtensionProperties(nullptr, &count, nullptr); | 
|  | 930 | if (result == VK_SUCCESS && count) { | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 931 | driver_extensions_ = AllocateDriverExtensionArray(count); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 932 | result = (driver_extensions_) ? EnumerateInstanceExtensionProperties( | 
|  | 933 | nullptr, &count, driver_extensions_) | 
|  | 934 | : VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 935 | } | 
|  | 936 | if (result != VK_SUCCESS) | 
|  | 937 | return result; | 
|  | 938 |  | 
|  | 939 | driver_extension_count_ = count; | 
|  | 940 |  | 
|  | 941 | for (uint32_t i = 0; i < extension_count; i++) { | 
|  | 942 | const char* name = extension_names[i]; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 943 | if (!IsLayerExtension(name) && !IsDriverExtension(name)) { | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 944 | logger_.Err(VK_NULL_HANDLE, | 
|  | 945 | "Failed to enable missing instance extension %s", name); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 946 | return VK_ERROR_EXTENSION_NOT_PRESENT; | 
|  | 947 | } | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 948 |  | 
|  | 949 | auto ext_bit = driver::GetProcHookExtension(name); | 
|  | 950 | if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN) | 
|  | 951 | enabled_extensions_.set(ext_bit); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 952 | } | 
|  | 953 |  | 
|  | 954 | return VK_SUCCESS; | 
|  | 955 | } | 
|  | 956 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 957 | VkResult LayerChain::ValidateExtensions(VkPhysicalDevice physical_dev, | 
|  | 958 | const char* const* extension_names, | 
|  | 959 | uint32_t extension_count) { | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 960 | if (!extension_count) | 
|  | 961 | return VK_SUCCESS; | 
|  | 962 |  | 
|  | 963 | // query driver device extensions | 
|  | 964 | uint32_t count; | 
|  | 965 | VkResult result = EnumerateDeviceExtensionProperties(physical_dev, nullptr, | 
|  | 966 | &count, nullptr); | 
|  | 967 | if (result == VK_SUCCESS && count) { | 
| Ian Elliott | bfb7199 | 2021-11-24 16:32:41 -0700 | [diff] [blame] | 968 | // Work-around a race condition during Android start-up, that can result | 
|  | 969 | // in the second call to EnumerateDeviceExtensionProperties having | 
|  | 970 | // another extension.  That causes the second call to return | 
|  | 971 | // VK_INCOMPLETE.  A work-around is to add 1 to "count" and ask for one | 
|  | 972 | // more extension property.  See: http://anglebug.com/6715 and | 
|  | 973 | // internal-to-Google b/206733351. | 
|  | 974 | count++; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 975 | driver_extensions_ = AllocateDriverExtensionArray(count); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 976 | result = (driver_extensions_) | 
|  | 977 | ? EnumerateDeviceExtensionProperties( | 
|  | 978 | physical_dev, nullptr, &count, driver_extensions_) | 
|  | 979 | : VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 980 | } | 
|  | 981 | if (result != VK_SUCCESS) | 
|  | 982 | return result; | 
|  | 983 |  | 
|  | 984 | driver_extension_count_ = count; | 
|  | 985 |  | 
|  | 986 | for (uint32_t i = 0; i < extension_count; i++) { | 
|  | 987 | const char* name = extension_names[i]; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 988 | if (!IsLayerExtension(name) && !IsDriverExtension(name)) { | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 989 | logger_.Err(physical_dev, | 
|  | 990 | "Failed to enable missing device extension %s", name); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 991 | return VK_ERROR_EXTENSION_NOT_PRESENT; | 
|  | 992 | } | 
| Chia-I Wu | 8925efd | 2016-04-13 15:13:21 +0800 | [diff] [blame] | 993 |  | 
|  | 994 | auto ext_bit = driver::GetProcHookExtension(name); | 
|  | 995 | if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN) | 
|  | 996 | enabled_extensions_.set(ext_bit); | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 997 | } | 
|  | 998 |  | 
|  | 999 | return VK_SUCCESS; | 
|  | 1000 | } | 
|  | 1001 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1002 | VkExtensionProperties* LayerChain::AllocateDriverExtensionArray( | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 1003 | uint32_t count) const { | 
|  | 1004 | return reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation( | 
|  | 1005 | allocator_.pUserData, sizeof(VkExtensionProperties) * count, | 
|  | 1006 | alignof(VkExtensionProperties), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND)); | 
|  | 1007 | } | 
|  | 1008 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1009 | bool LayerChain::IsLayerExtension(const char* name) const { | 
| Chia-I Wu | dab2565 | 2016-04-28 07:15:51 +0800 | [diff] [blame] | 1010 | if (is_instance_) { | 
|  | 1011 | for (uint32_t i = 0; i < layer_count_; i++) { | 
|  | 1012 | const ActiveLayer& layer = layers_[i]; | 
|  | 1013 | if (FindLayerInstanceExtension(*layer.ref, name)) | 
|  | 1014 | return true; | 
|  | 1015 | } | 
|  | 1016 | } else { | 
|  | 1017 | for (uint32_t i = 0; i < layer_count_; i++) { | 
|  | 1018 | const ActiveLayer& layer = layers_[i]; | 
|  | 1019 | if (FindLayerDeviceExtension(*layer.ref, name)) | 
|  | 1020 | return true; | 
|  | 1021 | } | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 1022 | } | 
|  | 1023 |  | 
|  | 1024 | return false; | 
|  | 1025 | } | 
|  | 1026 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1027 | bool LayerChain::IsDriverExtension(const char* name) const { | 
| Chia-I Wu | 1f8f46b | 2016-04-06 14:17:48 +0800 | [diff] [blame] | 1028 | for (uint32_t i = 0; i < driver_extension_count_; i++) { | 
|  | 1029 | if (strcmp(driver_extensions_[i].extensionName, name) == 0) | 
|  | 1030 | return true; | 
|  | 1031 | } | 
|  | 1032 |  | 
|  | 1033 | return false; | 
|  | 1034 | } | 
|  | 1035 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1036 | template <typename DataType> | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1037 | void LayerChain::StealLayers(DataType& data) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1038 | data.layers = layers_; | 
|  | 1039 | data.layer_count = layer_count_; | 
|  | 1040 |  | 
|  | 1041 | layers_ = nullptr; | 
|  | 1042 | layer_count_ = 0; | 
|  | 1043 | } | 
|  | 1044 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1045 | void LayerChain::DestroyLayers(ActiveLayer* layers, | 
|  | 1046 | uint32_t count, | 
|  | 1047 | const VkAllocationCallbacks& allocator) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1048 | for (uint32_t i = 0; i < count; i++) | 
|  | 1049 | layers[i].ref.~LayerRef(); | 
|  | 1050 |  | 
|  | 1051 | allocator.pfnFree(allocator.pUserData, layers); | 
|  | 1052 | } | 
|  | 1053 |  | 
| Chia-I Wu | 94a2c0e | 2016-04-13 10:20:59 +0800 | [diff] [blame] | 1054 | VkResult LayerChain::SetInstanceLoaderData(VkInstance instance, void* object) { | 
|  | 1055 | driver::InstanceDispatchable dispatchable = | 
|  | 1056 | reinterpret_cast<driver::InstanceDispatchable>(object); | 
|  | 1057 |  | 
|  | 1058 | return (driver::SetDataInternal(dispatchable, &driver::GetData(instance))) | 
|  | 1059 | ? VK_SUCCESS | 
|  | 1060 | : VK_ERROR_INITIALIZATION_FAILED; | 
|  | 1061 | } | 
|  | 1062 |  | 
|  | 1063 | VkResult LayerChain::SetDeviceLoaderData(VkDevice device, void* object) { | 
|  | 1064 | driver::DeviceDispatchable dispatchable = | 
|  | 1065 | reinterpret_cast<driver::DeviceDispatchable>(object); | 
|  | 1066 |  | 
|  | 1067 | return (driver::SetDataInternal(dispatchable, &driver::GetData(device))) | 
|  | 1068 | ? VK_SUCCESS | 
|  | 1069 | : VK_ERROR_INITIALIZATION_FAILED; | 
|  | 1070 | } | 
|  | 1071 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1072 | VkBool32 LayerChain::DebugReportCallback(VkDebugReportFlagsEXT flags, | 
|  | 1073 | VkDebugReportObjectTypeEXT obj_type, | 
|  | 1074 | uint64_t obj, | 
|  | 1075 | size_t location, | 
|  | 1076 | int32_t msg_code, | 
|  | 1077 | const char* layer_prefix, | 
|  | 1078 | const char* msg, | 
|  | 1079 | void* user_data) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1080 | int prio; | 
|  | 1081 |  | 
|  | 1082 | if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) | 
|  | 1083 | prio = ANDROID_LOG_ERROR; | 
|  | 1084 | else if (flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT | | 
|  | 1085 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT)) | 
|  | 1086 | prio = ANDROID_LOG_WARN; | 
|  | 1087 | else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT) | 
|  | 1088 | prio = ANDROID_LOG_INFO; | 
|  | 1089 | else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT) | 
|  | 1090 | prio = ANDROID_LOG_DEBUG; | 
|  | 1091 | else | 
|  | 1092 | prio = ANDROID_LOG_UNKNOWN; | 
|  | 1093 |  | 
|  | 1094 | LOG_PRI(prio, LOG_TAG, "[%s] Code %d : %s", layer_prefix, msg_code, msg); | 
|  | 1095 |  | 
|  | 1096 | (void)obj_type; | 
|  | 1097 | (void)obj; | 
|  | 1098 | (void)location; | 
|  | 1099 | (void)user_data; | 
|  | 1100 |  | 
|  | 1101 | return false; | 
|  | 1102 | } | 
|  | 1103 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1104 | VkResult LayerChain::CreateInstance(const VkInstanceCreateInfo* create_info, | 
|  | 1105 | const VkAllocationCallbacks* allocator, | 
|  | 1106 | VkInstance* instance_out) { | 
| Courtney Goeltzenleuchter | 1531bf1 | 2016-12-29 08:46:18 -0700 | [diff] [blame] | 1107 | const driver::DebugReportLogger logger(*create_info); | 
|  | 1108 | LayerChain chain(true, logger, | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1109 | (allocator) ? *allocator : driver::GetDefaultAllocator()); | 
|  | 1110 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1111 | VkResult result = chain.ActivateLayers(create_info->ppEnabledLayerNames, | 
|  | 1112 | create_info->enabledLayerCount, | 
|  | 1113 | create_info->ppEnabledExtensionNames, | 
|  | 1114 | create_info->enabledExtensionCount); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1115 | if (result != VK_SUCCESS) | 
|  | 1116 | return result; | 
|  | 1117 |  | 
|  | 1118 | // use a local create info when the chain is not empty | 
|  | 1119 | VkInstanceCreateInfo local_create_info; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1120 | if (!chain.Empty()) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1121 | local_create_info = *create_info; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1122 | chain.ModifyCreateInfo(local_create_info); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1123 | create_info = &local_create_info; | 
|  | 1124 | } | 
|  | 1125 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1126 | return chain.Create(create_info, allocator, instance_out); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1127 | } | 
|  | 1128 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1129 | VkResult LayerChain::CreateDevice(VkPhysicalDevice physical_dev, | 
|  | 1130 | const VkDeviceCreateInfo* create_info, | 
|  | 1131 | const VkAllocationCallbacks* allocator, | 
|  | 1132 | VkDevice* dev_out) { | 
| Courtney Goeltzenleuchter | 1531bf1 | 2016-12-29 08:46:18 -0700 | [diff] [blame] | 1133 | const driver::DebugReportLogger logger = driver::Logger(physical_dev); | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 1134 | LayerChain chain( | 
| Courtney Goeltzenleuchter | 1531bf1 | 2016-12-29 08:46:18 -0700 | [diff] [blame] | 1135 | false, logger, | 
| Chia-I Wu | a4a0555 | 2016-05-05 11:57:23 +0800 | [diff] [blame] | 1136 | (allocator) ? *allocator : driver::GetData(physical_dev).allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1137 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1138 | VkResult result = chain.ActivateLayers( | 
|  | 1139 | physical_dev, create_info->ppEnabledLayerNames, | 
|  | 1140 | create_info->enabledLayerCount, create_info->ppEnabledExtensionNames, | 
|  | 1141 | create_info->enabledExtensionCount); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1142 | if (result != VK_SUCCESS) | 
|  | 1143 | return result; | 
|  | 1144 |  | 
|  | 1145 | // use a local create info when the chain is not empty | 
|  | 1146 | VkDeviceCreateInfo local_create_info; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1147 | if (!chain.Empty()) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1148 | local_create_info = *create_info; | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1149 | chain.ModifyCreateInfo(local_create_info); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1150 | create_info = &local_create_info; | 
|  | 1151 | } | 
|  | 1152 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1153 | return chain.Create(physical_dev, create_info, allocator, dev_out); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1154 | } | 
|  | 1155 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1156 | void LayerChain::DestroyInstance(VkInstance instance, | 
|  | 1157 | const VkAllocationCallbacks* allocator) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1158 | InstanceData& data = GetData(instance); | 
|  | 1159 |  | 
|  | 1160 | if (data.debug_callback != VK_NULL_HANDLE) | 
|  | 1161 | data.destroy_debug_callback(instance, data.debug_callback, allocator); | 
|  | 1162 |  | 
|  | 1163 | ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers); | 
|  | 1164 | uint32_t layer_count = data.layer_count; | 
|  | 1165 |  | 
|  | 1166 | VkAllocationCallbacks local_allocator; | 
|  | 1167 | if (!allocator) | 
|  | 1168 | local_allocator = driver::GetData(instance).allocator; | 
|  | 1169 |  | 
|  | 1170 | // this also destroys InstanceData | 
|  | 1171 | data.dispatch.DestroyInstance(instance, allocator); | 
|  | 1172 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1173 | DestroyLayers(layers, layer_count, | 
|  | 1174 | (allocator) ? *allocator : local_allocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1175 | } | 
|  | 1176 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1177 | void LayerChain::DestroyDevice(VkDevice device, | 
|  | 1178 | const VkAllocationCallbacks* allocator) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1179 | DeviceData& data = GetData(device); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1180 | // this also destroys DeviceData | 
|  | 1181 | data.dispatch.DestroyDevice(device, allocator); | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1182 | } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1183 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1184 | const LayerChain::ActiveLayer* LayerChain::GetActiveLayers( | 
|  | 1185 | VkPhysicalDevice physical_dev, | 
|  | 1186 | uint32_t& count) { | 
|  | 1187 | count = GetData(physical_dev).layer_count; | 
|  | 1188 | return reinterpret_cast<const ActiveLayer*>(GetData(physical_dev).layers); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1189 | } | 
|  | 1190 |  | 
|  | 1191 | // ---------------------------------------------------------------------------- | 
|  | 1192 |  | 
|  | 1193 | bool EnsureInitialized() { | 
| Yiwei Zhang | 901f8ee | 2020-07-31 13:18:49 -0700 | [diff] [blame] | 1194 | static bool initialized = false; | 
|  | 1195 | static pid_t init_attempted_for_pid = 0; | 
|  | 1196 | static std::mutex init_lock; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1197 |  | 
| Yiwei Zhang | 901f8ee | 2020-07-31 13:18:49 -0700 | [diff] [blame] | 1198 | std::lock_guard<std::mutex> lock(init_lock); | 
|  | 1199 | if (init_attempted_for_pid == getpid()) | 
|  | 1200 | return initialized; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1201 |  | 
| Yiwei Zhang | 901f8ee | 2020-07-31 13:18:49 -0700 | [diff] [blame] | 1202 | init_attempted_for_pid = getpid(); | 
|  | 1203 | if (driver::OpenHAL()) { | 
|  | 1204 | DiscoverLayers(); | 
|  | 1205 | initialized = true; | 
| Yiwei Zhang | 9dfc93a | 2019-08-09 17:25:24 -0700 | [diff] [blame] | 1206 | } | 
|  | 1207 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1208 | return initialized; | 
|  | 1209 | } | 
|  | 1210 |  | 
| Peiyong Lin | 2780049 | 2020-03-25 15:44:28 -0700 | [diff] [blame] | 1211 | template <typename Functor> | 
|  | 1212 | void ForEachLayerFromSettings(Functor functor) { | 
|  | 1213 | const std::string layersSetting = | 
|  | 1214 | android::GraphicsEnv::getInstance().getDebugLayers(); | 
|  | 1215 | if (!layersSetting.empty()) { | 
|  | 1216 | std::vector<std::string> layers = | 
|  | 1217 | android::base::Split(layersSetting, ":"); | 
|  | 1218 | for (uint32_t i = 0; i < layers.size(); i++) { | 
|  | 1219 | const Layer* layer = FindLayer(layers[i].c_str()); | 
|  | 1220 | if (!layer) { | 
|  | 1221 | continue; | 
|  | 1222 | } | 
|  | 1223 | functor(layer); | 
|  | 1224 | } | 
|  | 1225 | } | 
|  | 1226 | } | 
|  | 1227 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1228 | }  // anonymous namespace | 
|  | 1229 |  | 
|  | 1230 | VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo, | 
|  | 1231 | const VkAllocationCallbacks* pAllocator, | 
|  | 1232 | VkInstance* pInstance) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1233 | ATRACE_CALL(); | 
|  | 1234 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1235 | if (!EnsureInitialized()) | 
|  | 1236 | return VK_ERROR_INITIALIZATION_FAILED; | 
|  | 1237 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1238 | return LayerChain::CreateInstance(pCreateInfo, pAllocator, pInstance); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1239 | } | 
|  | 1240 |  | 
|  | 1241 | void DestroyInstance(VkInstance instance, | 
|  | 1242 | const VkAllocationCallbacks* pAllocator) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1243 | ATRACE_CALL(); | 
|  | 1244 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1245 | if (instance != VK_NULL_HANDLE) | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1246 | LayerChain::DestroyInstance(instance, pAllocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1247 | } | 
|  | 1248 |  | 
|  | 1249 | VkResult CreateDevice(VkPhysicalDevice physicalDevice, | 
|  | 1250 | const VkDeviceCreateInfo* pCreateInfo, | 
|  | 1251 | const VkAllocationCallbacks* pAllocator, | 
|  | 1252 | VkDevice* pDevice) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1253 | ATRACE_CALL(); | 
|  | 1254 |  | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1255 | return LayerChain::CreateDevice(physicalDevice, pCreateInfo, pAllocator, | 
|  | 1256 | pDevice); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1257 | } | 
|  | 1258 |  | 
|  | 1259 | void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1260 | ATRACE_CALL(); | 
|  | 1261 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1262 | if (device != VK_NULL_HANDLE) | 
| Chia-I Wu | eef27fa | 2016-04-11 13:52:39 +0800 | [diff] [blame] | 1263 | LayerChain::DestroyDevice(device, pAllocator); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1264 | } | 
|  | 1265 |  | 
|  | 1266 | VkResult EnumerateInstanceLayerProperties(uint32_t* pPropertyCount, | 
|  | 1267 | VkLayerProperties* pProperties) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1268 | ATRACE_CALL(); | 
|  | 1269 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1270 | if (!EnsureInitialized()) | 
| Yiwei Zhang | c889d3c | 2020-07-16 13:51:12 -0700 | [diff] [blame] | 1271 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1272 |  | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1273 | uint32_t count = GetLayerCount(); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1274 |  | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1275 | if (!pProperties) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1276 | *pPropertyCount = count; | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1277 | return VK_SUCCESS; | 
|  | 1278 | } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1279 |  | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1280 | uint32_t copied = std::min(*pPropertyCount, count); | 
|  | 1281 | for (uint32_t i = 0; i < copied; i++) | 
|  | 1282 | pProperties[i] = GetLayerProperties(GetLayer(i)); | 
|  | 1283 | *pPropertyCount = copied; | 
|  | 1284 |  | 
|  | 1285 | return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1286 | } | 
|  | 1287 |  | 
|  | 1288 | VkResult EnumerateInstanceExtensionProperties( | 
|  | 1289 | const char* pLayerName, | 
|  | 1290 | uint32_t* pPropertyCount, | 
|  | 1291 | VkExtensionProperties* pProperties) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1292 | ATRACE_CALL(); | 
|  | 1293 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1294 | if (!EnsureInitialized()) | 
| Yiwei Zhang | c889d3c | 2020-07-16 13:51:12 -0700 | [diff] [blame] | 1295 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1296 |  | 
|  | 1297 | if (pLayerName) { | 
| Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 1298 | const Layer* layer = FindLayer(pLayerName); | 
| Chia-I Wu | 6184b20 | 2016-04-27 11:57:53 +0800 | [diff] [blame] | 1299 | if (!layer) | 
|  | 1300 | return VK_ERROR_LAYER_NOT_PRESENT; | 
|  | 1301 |  | 
|  | 1302 | uint32_t count; | 
|  | 1303 | const VkExtensionProperties* props = | 
|  | 1304 | GetLayerInstanceExtensions(*layer, count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1305 |  | 
|  | 1306 | if (!pProperties || *pPropertyCount > count) | 
|  | 1307 | *pPropertyCount = count; | 
|  | 1308 | if (pProperties) | 
|  | 1309 | std::copy(props, props + *pPropertyCount, pProperties); | 
|  | 1310 |  | 
|  | 1311 | return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; | 
|  | 1312 | } | 
|  | 1313 |  | 
| Peiyong Lin | 8f4435a | 2020-03-11 17:43:28 -0700 | [diff] [blame] | 1314 | // If the pLayerName is nullptr, we must advertise all instance extensions | 
|  | 1315 | // from all implicitly enabled layers and the driver implementation. If | 
|  | 1316 | // there are duplicates among layers and the driver implementation, always | 
|  | 1317 | // only preserve the top layer closest to the application regardless of the | 
|  | 1318 | // spec version. | 
|  | 1319 | std::vector<VkExtensionProperties> properties; | 
|  | 1320 | std::unordered_set<std::string> extensionNames; | 
|  | 1321 |  | 
|  | 1322 | // Expose extensions from implicitly enabled layers. | 
| Peiyong Lin | 2780049 | 2020-03-25 15:44:28 -0700 | [diff] [blame] | 1323 | ForEachLayerFromSettings([&](const Layer* layer) { | 
|  | 1324 | uint32_t count = 0; | 
|  | 1325 | const VkExtensionProperties* props = | 
|  | 1326 | GetLayerInstanceExtensions(*layer, count); | 
|  | 1327 | if (count > 0) { | 
|  | 1328 | for (uint32_t i = 0; i < count; ++i) { | 
|  | 1329 | if (extensionNames.emplace(props[i].extensionName).second) { | 
|  | 1330 | properties.push_back(props[i]); | 
| Peiyong Lin | 8f4435a | 2020-03-11 17:43:28 -0700 | [diff] [blame] | 1331 | } | 
|  | 1332 | } | 
|  | 1333 | } | 
| Peiyong Lin | 2780049 | 2020-03-25 15:44:28 -0700 | [diff] [blame] | 1334 | }); | 
| Peiyong Lin | 8f4435a | 2020-03-11 17:43:28 -0700 | [diff] [blame] | 1335 |  | 
|  | 1336 | // TODO(b/143293104): Parse debug.vulkan.layers properties | 
|  | 1337 |  | 
|  | 1338 | // Expose extensions from driver implementation. | 
|  | 1339 | { | 
|  | 1340 | uint32_t count = 0; | 
|  | 1341 | VkResult result = vulkan::driver::EnumerateInstanceExtensionProperties( | 
|  | 1342 | nullptr, &count, nullptr); | 
|  | 1343 | if (result == VK_SUCCESS && count > 0) { | 
|  | 1344 | std::vector<VkExtensionProperties> props(count); | 
|  | 1345 | result = vulkan::driver::EnumerateInstanceExtensionProperties( | 
|  | 1346 | nullptr, &count, props.data()); | 
|  | 1347 | for (auto prop : props) { | 
|  | 1348 | if (extensionNames.emplace(prop.extensionName).second) { | 
|  | 1349 | properties.push_back(prop); | 
|  | 1350 | } | 
|  | 1351 | } | 
|  | 1352 | } | 
|  | 1353 | } | 
|  | 1354 |  | 
|  | 1355 | uint32_t totalCount = properties.size(); | 
|  | 1356 | if (!pProperties || *pPropertyCount > totalCount) { | 
|  | 1357 | *pPropertyCount = totalCount; | 
|  | 1358 | } | 
|  | 1359 | if (pProperties) { | 
|  | 1360 | std::copy(properties.data(), properties.data() + *pPropertyCount, | 
|  | 1361 | pProperties); | 
|  | 1362 | } | 
|  | 1363 | return *pPropertyCount < totalCount ? VK_INCOMPLETE : VK_SUCCESS; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1364 | } | 
|  | 1365 |  | 
|  | 1366 | VkResult EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, | 
|  | 1367 | uint32_t* pPropertyCount, | 
|  | 1368 | VkLayerProperties* pProperties) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1369 | ATRACE_CALL(); | 
|  | 1370 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1371 | uint32_t count; | 
|  | 1372 | const LayerChain::ActiveLayer* layers = | 
|  | 1373 | LayerChain::GetActiveLayers(physicalDevice, count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1374 |  | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1375 | if (!pProperties) { | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1376 | *pPropertyCount = count; | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1377 | return VK_SUCCESS; | 
|  | 1378 | } | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1379 |  | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1380 | uint32_t copied = std::min(*pPropertyCount, count); | 
|  | 1381 | for (uint32_t i = 0; i < copied; i++) | 
|  | 1382 | pProperties[i] = GetLayerProperties(*layers[i].ref); | 
| Chia-I Wu | 25700b4 | 2016-04-28 06:36:09 +0800 | [diff] [blame] | 1383 | *pPropertyCount = copied; | 
|  | 1384 |  | 
|  | 1385 | return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1386 | } | 
|  | 1387 |  | 
|  | 1388 | VkResult EnumerateDeviceExtensionProperties( | 
|  | 1389 | VkPhysicalDevice physicalDevice, | 
|  | 1390 | const char* pLayerName, | 
|  | 1391 | uint32_t* pPropertyCount, | 
|  | 1392 | VkExtensionProperties* pProperties) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1393 | ATRACE_CALL(); | 
|  | 1394 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1395 | if (pLayerName) { | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1396 | // EnumerateDeviceLayerProperties enumerates active layers for | 
|  | 1397 | // backward compatibility.  The extension query here should work for | 
|  | 1398 | // all layers. | 
| Chia-I Wu | 04c6551 | 2016-04-27 09:54:02 +0800 | [diff] [blame] | 1399 | const Layer* layer = FindLayer(pLayerName); | 
| Chia-I Wu | c3a2891 | 2016-04-14 11:55:51 +0800 | [diff] [blame] | 1400 | if (!layer) | 
| Chia-I Wu | 6184b20 | 2016-04-27 11:57:53 +0800 | [diff] [blame] | 1401 | return VK_ERROR_LAYER_NOT_PRESENT; | 
|  | 1402 |  | 
|  | 1403 | uint32_t count; | 
|  | 1404 | const VkExtensionProperties* props = | 
|  | 1405 | GetLayerDeviceExtensions(*layer, count); | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1406 |  | 
|  | 1407 | if (!pProperties || *pPropertyCount > count) | 
|  | 1408 | *pPropertyCount = count; | 
|  | 1409 | if (pProperties) | 
|  | 1410 | std::copy(props, props + *pPropertyCount, pProperties); | 
|  | 1411 |  | 
|  | 1412 | return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS; | 
|  | 1413 | } | 
|  | 1414 |  | 
| Peiyong Lin | 2780049 | 2020-03-25 15:44:28 -0700 | [diff] [blame] | 1415 | // If the pLayerName is nullptr, we must advertise all device extensions | 
|  | 1416 | // from all implicitly enabled layers and the driver implementation. If | 
|  | 1417 | // there are duplicates among layers and the driver implementation, always | 
|  | 1418 | // only preserve the top layer closest to the application regardless of the | 
|  | 1419 | // spec version. | 
|  | 1420 | std::vector<VkExtensionProperties> properties; | 
|  | 1421 | std::unordered_set<std::string> extensionNames; | 
|  | 1422 |  | 
|  | 1423 | // Expose extensions from implicitly enabled layers. | 
|  | 1424 | ForEachLayerFromSettings([&](const Layer* layer) { | 
|  | 1425 | uint32_t count = 0; | 
|  | 1426 | const VkExtensionProperties* props = | 
|  | 1427 | GetLayerDeviceExtensions(*layer, count); | 
|  | 1428 | if (count > 0) { | 
|  | 1429 | for (uint32_t i = 0; i < count; ++i) { | 
|  | 1430 | if (extensionNames.emplace(props[i].extensionName).second) { | 
|  | 1431 | properties.push_back(props[i]); | 
|  | 1432 | } | 
|  | 1433 | } | 
|  | 1434 | } | 
|  | 1435 | }); | 
|  | 1436 |  | 
|  | 1437 | // TODO(b/143293104): Parse debug.vulkan.layers properties | 
|  | 1438 |  | 
|  | 1439 | // Expose extensions from driver implementation. | 
|  | 1440 | { | 
|  | 1441 | const InstanceData& data = GetData(physicalDevice); | 
|  | 1442 | uint32_t count = 0; | 
|  | 1443 | VkResult result = data.dispatch.EnumerateDeviceExtensionProperties( | 
|  | 1444 | physicalDevice, nullptr, &count, nullptr); | 
|  | 1445 | if (result == VK_SUCCESS && count > 0) { | 
|  | 1446 | std::vector<VkExtensionProperties> props(count); | 
|  | 1447 | result = data.dispatch.EnumerateDeviceExtensionProperties( | 
|  | 1448 | physicalDevice, nullptr, &count, props.data()); | 
|  | 1449 | for (auto prop : props) { | 
|  | 1450 | if (extensionNames.emplace(prop.extensionName).second) { | 
|  | 1451 | properties.push_back(prop); | 
|  | 1452 | } | 
|  | 1453 | } | 
|  | 1454 | } | 
|  | 1455 | } | 
|  | 1456 |  | 
|  | 1457 | uint32_t totalCount = properties.size(); | 
|  | 1458 | if (!pProperties || *pPropertyCount > totalCount) { | 
|  | 1459 | *pPropertyCount = totalCount; | 
|  | 1460 | } | 
|  | 1461 | if (pProperties) { | 
|  | 1462 | std::copy(properties.data(), properties.data() + *pPropertyCount, | 
|  | 1463 | pProperties); | 
|  | 1464 | } | 
|  | 1465 | return *pPropertyCount < totalCount ? VK_INCOMPLETE : VK_SUCCESS; | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1466 | } | 
|  | 1467 |  | 
| Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1468 | VkResult EnumerateInstanceVersion(uint32_t* pApiVersion) { | 
| Yiwei Zhang | fdd0c2a | 2019-01-30 20:16:37 -0800 | [diff] [blame] | 1469 | ATRACE_CALL(); | 
|  | 1470 |  | 
| Yiwei Zhang | c889d3c | 2020-07-16 13:51:12 -0700 | [diff] [blame] | 1471 | // Load the driver here if not done yet. This api will be used in Zygote | 
|  | 1472 | // for Vulkan driver pre-loading because of the minimum overhead. | 
|  | 1473 | if (!EnsureInitialized()) | 
|  | 1474 | return VK_ERROR_OUT_OF_HOST_MEMORY; | 
|  | 1475 |  | 
| Trevor David Black | 628c41a | 2021-09-27 05:07:22 +0000 | [diff] [blame] | 1476 | *pApiVersion = VK_API_VERSION_1_3; | 
| Daniel Koch | f25f5bb | 2017-10-05 00:26:58 -0400 | [diff] [blame] | 1477 | return VK_SUCCESS; | 
|  | 1478 | } | 
|  | 1479 |  | 
| Chia-I Wu | 0c20324 | 2016-03-15 13:44:51 +0800 | [diff] [blame] | 1480 | }  // namespace api | 
|  | 1481 | }  // namespace vulkan |