blob: d1cd397da433e57f7df2659bcd10233aad8d1b31 [file] [log] [blame]
Chia-I Wu0c203242016-03-15 13:44:51 +08001/*
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 Zhangfdd0c2a2019-01-30 20:16:37 -080024#define ATRACE_TAG ATRACE_TAG_GRAPHICS
25
Chia-I Wu0c203242016-03-15 13:44:51 +080026#include <stdlib.h>
27#include <string.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070028
Chia-I Wu0c203242016-03-15 13:44:51 +080029#include <algorithm>
30#include <mutex>
31#include <new>
Peiyong Lin8f4435a2020-03-11 17:43:28 -070032#include <string>
33#include <unordered_set>
Chia-I Wu0c203242016-03-15 13:44:51 +080034#include <utility>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070035
Jason Macnaka8baab02021-02-19 12:53:47 -080036#include <android-base/properties.h>
Cody Northropd2aa3ab2017-10-20 09:01:53 -060037#include <android-base/strings.h>
Chia-I Wu0c203242016-03-15 13:44:51 +080038#include <cutils/properties.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070039#include <log/log.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080040#include <utils/Trace.h>
Chia-I Wu0c203242016-03-15 13:44:51 +080041
42#include <vulkan/vk_layer_interface.h>
Cody Northropd2aa3ab2017-10-20 09:01:53 -060043#include <graphicsenv/GraphicsEnv.h>
Chia-I Wu0c203242016-03-15 13:44:51 +080044#include "api.h"
45#include "driver.h"
Chia-I Wuc96880f2016-03-26 06:56:45 +080046#include "layers_extensions.h"
Chia-I Wu0c203242016-03-15 13:44:51 +080047
Cody Northropd2aa3ab2017-10-20 09:01:53 -060048
Chia-I Wu0c203242016-03-15 13:44:51 +080049namespace vulkan {
50namespace api {
51
52namespace {
53
54// Provide overridden layer names when there are implicit layers. No effect
55// otherwise.
56class 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 Wu026b8fa2016-04-11 13:44:13 +080074 VkResult Parse(const char* const* names, uint32_t count) {
75 AddImplicitLayers();
Chia-I Wu0c203242016-03-15 13:44:51 +080076
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 Wu026b8fa2016-04-11 13:44:13 +080085 names_ = AllocateNameArray(arr.count + count);
Chia-I Wu0c203242016-03-15 13:44:51 +080086 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 Wu026b8fa2016-04-11 13:44:13 +080091 names_[i] = GetImplicitLayerName(i);
Chia-I Wu0c203242016-03-15 13:44:51 +080092
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 Wu026b8fa2016-04-11 13:44:13 +080098 if (IsImplicitLayer(names[i]))
Chia-I Wu0c203242016-03-15 13:44:51 +080099 continue;
100
101 names_[name_count_++] = names[i];
102 }
103
104 return VK_SUCCESS;
105 }
106
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800107 const char* const* Names() const { return names_; }
Chia-I Wu0c203242016-03-15 13:44:51 +0800108
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800109 uint32_t Count() const { return name_count_; }
Chia-I Wu0c203242016-03-15 13:44:51 +0800110
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 Wu026b8fa2016-04-11 13:44:13 +0800129 void AddImplicitLayers() {
Yiwei Zhang279df622020-02-18 10:45:15 -0800130 if (!is_instance_)
Chia-I Wu0c203242016-03-15 13:44:51 +0800131 return;
132
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600133 GetLayersFromSettings();
Chia-I Wu0c203242016-03-15 13:44:51 +0800134
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600135 // If no layers specified via Settings, check legacy properties
136 if (implicit_layers_.count <= 0) {
137 ParseDebugVulkanLayers();
Jason Macnaka8baab02021-02-19 12:53:47 -0800138 ParseDebugVulkanLayer();
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600139
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 Zhang94b18d52019-11-07 17:12:11 -0800150 // These will only be available if conditions are met in GraphicsEnvironment
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600151 // 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 Wu0c203242016-03-15 13:44:51 +0800160 }
161
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800162 void ParseDebugVulkanLayers() {
Chia-I Wu0c203242016-03-15 13:44:51 +0800163 // 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 Wu026b8fa2016-04-11 13:44:13 +0800175 AddImplicitLayer(prio, p, static_cast<size_t>(delim - p));
Chia-I Wu0c203242016-03-15 13:44:51 +0800176
177 prio++;
178 p = delim + 1;
179 }
180
181 if (p[0] != '\0')
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800182 AddImplicitLayer(prio, p, strlen(p));
Chia-I Wu0c203242016-03-15 13:44:51 +0800183 }
184
Jason Macnaka8baab02021-02-19 12:53:47 -0800185 void ParseDebugVulkanLayer() {
186 // Checks for consecutive debug.vulkan.layer.<priority> system
187 // properties after always checking an initial fixed range.
Chia-I Wu0c203242016-03-15 13:44:51 +0800188 static const char prefix[] = "debug.vulkan.layer.";
Jason Macnaka8baab02021-02-19 12:53:47 -0800189 static constexpr int kFixedRangeBeginInclusive = 0;
190 static constexpr int kFixedRangeEndInclusive = 9;
Chia-I Wu0c203242016-03-15 13:44:51 +0800191
Jason Macnaka8baab02021-02-19 12:53:47 -0800192 bool logged = false;
Chia-I Wu0c203242016-03-15 13:44:51 +0800193
Jason Macnaka8baab02021-02-19 12:53:47 -0800194 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 Wu0c203242016-03-15 13:44:51 +0800200
Jason Macnaka8baab02021-02-19 12:53:47 -0800201 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 Wu0c203242016-03-15 13:44:51 +0800217 }
Chia-I Wu0c203242016-03-15 13:44:51 +0800218 }
219
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800220 void AddImplicitLayer(int priority, const char* name, size_t len) {
221 if (!GrowImplicitLayerArray(1, 0))
Chia-I Wu0c203242016-03-15 13:44:51 +0800222 return;
223
224 auto& arr = implicit_layers_;
225 auto& layer = arr.elements[arr.count++];
226
227 layer.priority = priority;
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800228 layer.name_offset = AddImplicitLayerName(name, len);
Chia-I Wu0c203242016-03-15 13:44:51 +0800229
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800230 ALOGV("Added implicit layer %s", GetImplicitLayerName(arr.count - 1));
Chia-I Wu0c203242016-03-15 13:44:51 +0800231 }
232
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800233 size_t AddImplicitLayerName(const char* name, size_t len) {
234 if (!GrowImplicitLayerArray(0, len + 1))
Chia-I Wu0c203242016-03-15 13:44:51 +0800235 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 Wu026b8fa2016-04-11 13:44:13 +0800250 bool GrowImplicitLayerArray(uint32_t layer_count, size_t name_size) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800251 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 Wu026b8fa2016-04-11 13:44:13 +0800305 const char* GetImplicitLayerName(uint32_t index) const {
Chia-I Wu0c203242016-03-15 13:44:51 +0800306 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 Wu026b8fa2016-04-11 13:44:13 +0800312 bool IsImplicitLayer(const char* name) const {
Chia-I Wu0c203242016-03-15 13:44:51 +0800313 const auto& arr = implicit_layers_;
314
315 for (uint32_t i = 0; i < arr.count; i++) {
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800316 if (strcmp(name, GetImplicitLayerName(i)) == 0)
Chia-I Wu0c203242016-03-15 13:44:51 +0800317 return true;
318 }
319
320 return false;
321 }
322
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800323 const char** AllocateNameArray(uint32_t count) const {
Chia-I Wu0c203242016-03-15 13:44:51 +0800324 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.
343class 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 Wuc3fa20c2016-04-11 13:47:31 +0800358 VkResult Parse(const char* const* names, uint32_t count) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800359 // this is only for debug.vulkan.enable_callback
Chia-I Wuc3fa20c2016-04-11 13:47:31 +0800360 if (!EnableDebugCallback())
Chia-I Wu0c203242016-03-15 13:44:51 +0800361 return VK_SUCCESS;
362
Chia-I Wuc3fa20c2016-04-11 13:47:31 +0800363 names_ = AllocateNameArray(count + 1);
Chia-I Wu0c203242016-03-15 13:44:51 +0800364 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 Wuc3fa20c2016-04-11 13:47:31 +0800377 const char* const* Names() const { return names_; }
Chia-I Wu0c203242016-03-15 13:44:51 +0800378
Chia-I Wuc3fa20c2016-04-11 13:47:31 +0800379 uint32_t Count() const { return name_count_; }
Chia-I Wu0c203242016-03-15 13:44:51 +0800380
Chia-I Wuc3fa20c2016-04-11 13:47:31 +0800381 bool InstallDebugCallback() const { return install_debug_callback_; }
Chia-I Wu0c203242016-03-15 13:44:51 +0800382
383 private:
Chia-I Wuc3fa20c2016-04-11 13:47:31 +0800384 bool EnableDebugCallback() const {
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800385 return (is_instance_ &&
386 android::GraphicsEnv::getInstance().isDebuggable() &&
Chia-I Wu0c203242016-03-15 13:44:51 +0800387 property_get_bool("debug.vulkan.enable_callback", false));
388 }
389
Chia-I Wuc3fa20c2016-04-11 13:47:31 +0800390 const char** AllocateNameArray(uint32_t count) const {
Chia-I Wu0c203242016-03-15 13:44:51 +0800391 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.
407class LayerChain {
408 public:
Chia-I Wuc3a28912016-04-14 11:55:51 +0800409 struct ActiveLayer {
410 LayerRef ref;
411 union {
412 VkLayerInstanceLink instance_link;
413 VkLayerDeviceLink device_link;
414 };
415 };
416
Chia-I Wueef27fa2016-04-11 13:52:39 +0800417 static VkResult CreateInstance(const VkInstanceCreateInfo* create_info,
418 const VkAllocationCallbacks* allocator,
419 VkInstance* instance_out);
Chia-I Wu0c203242016-03-15 13:44:51 +0800420
Chia-I Wueef27fa2016-04-11 13:52:39 +0800421 static VkResult CreateDevice(VkPhysicalDevice physical_dev,
422 const VkDeviceCreateInfo* create_info,
423 const VkAllocationCallbacks* allocator,
424 VkDevice* dev_out);
Chia-I Wu0c203242016-03-15 13:44:51 +0800425
Chia-I Wueef27fa2016-04-11 13:52:39 +0800426 static void DestroyInstance(VkInstance instance,
427 const VkAllocationCallbacks* allocator);
Chia-I Wu0c203242016-03-15 13:44:51 +0800428
Chia-I Wueef27fa2016-04-11 13:52:39 +0800429 static void DestroyDevice(VkDevice dev,
430 const VkAllocationCallbacks* allocator);
Chia-I Wu0c203242016-03-15 13:44:51 +0800431
Chia-I Wuc3a28912016-04-14 11:55:51 +0800432 static const ActiveLayer* GetActiveLayers(VkPhysicalDevice physical_dev,
433 uint32_t& count);
Chia-I Wu0c203242016-03-15 13:44:51 +0800434
Chia-I Wuc3a28912016-04-14 11:55:51 +0800435 private:
Chia-I Wua4a05552016-05-05 11:57:23 +0800436 LayerChain(bool is_instance,
437 const driver::DebugReportLogger& logger,
438 const VkAllocationCallbacks& allocator);
Chia-I Wu0c203242016-03-15 13:44:51 +0800439 ~LayerChain();
440
Chia-I Wueef27fa2016-04-11 13:52:39 +0800441 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 Wuc3a28912016-04-14 11:55:51 +0800445 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 Wueef27fa2016-04-11 13:52:39 +0800450 ActiveLayer* AllocateLayerArray(uint32_t count) const;
451 VkResult LoadLayer(ActiveLayer& layer, const char* name);
452 void SetupLayerLinks();
Chia-I Wu0c203242016-03-15 13:44:51 +0800453
Chia-I Wueef27fa2016-04-11 13:52:39 +0800454 bool Empty() const;
455 void ModifyCreateInfo(VkInstanceCreateInfo& info);
456 void ModifyCreateInfo(VkDeviceCreateInfo& info);
Chia-I Wu0c203242016-03-15 13:44:51 +0800457
Chia-I Wueef27fa2016-04-11 13:52:39 +0800458 VkResult Create(const VkInstanceCreateInfo* create_info,
Chia-I Wu0c203242016-03-15 13:44:51 +0800459 const VkAllocationCallbacks* allocator,
460 VkInstance* instance_out);
461
Chia-I Wueef27fa2016-04-11 13:52:39 +0800462 VkResult Create(VkPhysicalDevice physical_dev,
Chia-I Wu0c203242016-03-15 13:44:51 +0800463 const VkDeviceCreateInfo* create_info,
464 const VkAllocationCallbacks* allocator,
465 VkDevice* dev_out);
466
Chia-I Wueef27fa2016-04-11 13:52:39 +0800467 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 Wu1f8f46b2016-04-06 14:17:48 +0800475
Chia-I Wu0c203242016-03-15 13:44:51 +0800476 template <typename DataType>
Chia-I Wueef27fa2016-04-11 13:52:39 +0800477 void StealLayers(DataType& data);
Chia-I Wu0c203242016-03-15 13:44:51 +0800478
Chia-I Wueef27fa2016-04-11 13:52:39 +0800479 static void DestroyLayers(ActiveLayer* layers,
480 uint32_t count,
481 const VkAllocationCallbacks& allocator);
Chia-I Wu0c203242016-03-15 13:44:51 +0800482
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800483 static VKAPI_ATTR VkResult SetInstanceLoaderData(VkInstance instance,
484 void* object);
485 static VKAPI_ATTR VkResult SetDeviceLoaderData(VkDevice device,
486 void* object);
487
Chia-I Wu0c203242016-03-15 13:44:51 +0800488 static VKAPI_ATTR VkBool32
Chia-I Wueef27fa2016-04-11 13:52:39 +0800489 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 Wu0c203242016-03-15 13:44:51 +0800497
498 const bool is_instance_;
Chia-I Wua4a05552016-05-05 11:57:23 +0800499 const driver::DebugReportLogger& logger_;
Chia-I Wu0c203242016-03-15 13:44:51 +0800500 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 Wu94a2c0e2016-04-13 10:20:59 +0800512 VkLayerInstanceCreateInfo instance_chain_info_[2];
513 VkLayerDeviceCreateInfo device_chain_info_[2];
Chia-I Wu0c203242016-03-15 13:44:51 +0800514 };
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800515
516 VkExtensionProperties* driver_extensions_;
517 uint32_t driver_extension_count_;
Chia-I Wu8925efd2016-04-13 15:13:21 +0800518 std::bitset<driver::ProcHook::EXTENSION_COUNT> enabled_extensions_;
Chia-I Wu0c203242016-03-15 13:44:51 +0800519};
520
Chia-I Wua4a05552016-05-05 11:57:23 +0800521LayerChain::LayerChain(bool is_instance,
522 const driver::DebugReportLogger& logger,
523 const VkAllocationCallbacks& allocator)
Chia-I Wu0c203242016-03-15 13:44:51 +0800524 : is_instance_(is_instance),
Chia-I Wua4a05552016-05-05 11:57:23 +0800525 logger_(logger),
Chia-I Wu0c203242016-03-15 13:44:51 +0800526 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 Wu1f8f46b2016-04-06 14:17:48 +0800532 get_device_proc_addr_(nullptr),
533 driver_extensions_(nullptr),
Chia-I Wu8925efd2016-04-13 15:13:21 +0800534 driver_extension_count_(0) {
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700535 // 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 Wu8925efd2016-04-13 15:13:21 +0800540}
Chia-I Wu0c203242016-03-15 13:44:51 +0800541
542LayerChain::~LayerChain() {
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800543 allocator_.pfnFree(allocator_.pUserData, driver_extensions_);
Chia-I Wueef27fa2016-04-11 13:52:39 +0800544 DestroyLayers(layers_, layer_count_, allocator_);
Chia-I Wu0c203242016-03-15 13:44:51 +0800545}
546
Chia-I Wueef27fa2016-04-11 13:52:39 +0800547VkResult 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 Wu026b8fa2016-04-11 13:44:13 +0800551 VkResult result = override_layers_.Parse(layer_names, layer_count);
Chia-I Wu0c203242016-03-15 13:44:51 +0800552 if (result != VK_SUCCESS)
553 return result;
554
Chia-I Wuc3fa20c2016-04-11 13:47:31 +0800555 result = override_extensions_.Parse(extension_names, extension_count);
Chia-I Wu0c203242016-03-15 13:44:51 +0800556 if (result != VK_SUCCESS)
557 return result;
558
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800559 if (override_layers_.Count()) {
560 layer_names = override_layers_.Names();
561 layer_count = override_layers_.Count();
Chia-I Wu0c203242016-03-15 13:44:51 +0800562 }
563
564 if (!layer_count) {
565 // point head of chain to the driver
566 get_instance_proc_addr_ = driver::GetInstanceProcAddr;
Chia-I Wu0c203242016-03-15 13:44:51 +0800567
568 return VK_SUCCESS;
569 }
570
Chia-I Wueef27fa2016-04-11 13:52:39 +0800571 layers_ = AllocateLayerArray(layer_count);
Chia-I Wu0c203242016-03-15 13:44:51 +0800572 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 Wueef27fa2016-04-11 13:52:39 +0800577 result = LoadLayer(layers_[i], layer_names[i]);
Chia-I Wu0c203242016-03-15 13:44:51 +0800578 if (result != VK_SUCCESS)
579 return result;
580
581 // count loaded layers for proper destructions on errors
582 layer_count_++;
583 }
584
Chia-I Wueef27fa2016-04-11 13:52:39 +0800585 SetupLayerLinks();
Chia-I Wu0c203242016-03-15 13:44:51 +0800586
587 return VK_SUCCESS;
588}
589
Chia-I Wuc3a28912016-04-14 11:55:51 +0800590VkResult 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 Wua4a05552016-05-05 11:57:23 +0800614 logger_.Warn(physical_dev,
615 "Device layers disagree with instance layers and are "
616 "overridden by instance layers");
Chia-I Wuc3a28912016-04-14 11:55:51 +0800617 }
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 Wu61b25fd2016-05-27 10:18:25 +0800648 // 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 Wuc3a28912016-04-14 11:55:51 +0800655 SetupLayerLinks();
656
657 return VK_SUCCESS;
658}
659
Chia-I Wueef27fa2016-04-11 13:52:39 +0800660LayerChain::ActiveLayer* LayerChain::AllocateLayerArray(uint32_t count) const {
Chia-I Wu0c203242016-03-15 13:44:51 +0800661 VkSystemAllocationScope scope = (is_instance_)
662 ? VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE
Chia-I Wuc3a28912016-04-14 11:55:51 +0800663 : VK_SYSTEM_ALLOCATION_SCOPE_COMMAND;
Chia-I Wu0c203242016-03-15 13:44:51 +0800664
665 return reinterpret_cast<ActiveLayer*>(allocator_.pfnAllocation(
666 allocator_.pUserData, sizeof(ActiveLayer) * count, alignof(ActiveLayer),
667 scope));
668}
669
Chia-I Wueef27fa2016-04-11 13:52:39 +0800670VkResult LayerChain::LoadLayer(ActiveLayer& layer, const char* name) {
Chia-I Wud6e6f512016-04-28 07:39:32 +0800671 const Layer* l = FindLayer(name);
Chia-I Wuc3a28912016-04-14 11:55:51 +0800672 if (!l) {
Chia-I Wua4a05552016-05-05 11:57:23 +0800673 logger_.Err(VK_NULL_HANDLE, "Failed to find layer %s", name);
Chia-I Wud6e6f512016-04-28 07:39:32 +0800674 return VK_ERROR_LAYER_NOT_PRESENT;
675 }
676
Chia-I Wudab25652016-04-28 07:15:51 +0800677 new (&layer) ActiveLayer{GetLayerRef(*l), {}};
Chia-I Wu0c203242016-03-15 13:44:51 +0800678 if (!layer.ref) {
Chia-I Wud6e6f512016-04-28 07:39:32 +0800679 ALOGW("Failed to open layer %s", name);
Chia-I Wu0c203242016-03-15 13:44:51 +0800680 layer.ref.~LayerRef();
681 return VK_ERROR_LAYER_NOT_PRESENT;
682 }
683
Adam Bodnar87edb952019-07-17 12:35:53 -0700684 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 Wuc3a28912016-04-14 11:55:51 +0800690 ALOGI("Loaded layer %s", name);
Chia-I Wu0c203242016-03-15 13:44:51 +0800691
692 return VK_SUCCESS;
693}
694
Chia-I Wueef27fa2016-04-11 13:52:39 +0800695void LayerChain::SetupLayerLinks() {
Chia-I Wu0c203242016-03-15 13:44:51 +0800696 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 Wueef27fa2016-04-11 13:52:39 +0800753bool LayerChain::Empty() const {
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800754 return (!layer_count_ && !override_layers_.Count() &&
Chia-I Wuc3fa20c2016-04-11 13:47:31 +0800755 !override_extensions_.Count());
Chia-I Wu0c203242016-03-15 13:44:51 +0800756}
757
Chia-I Wueef27fa2016-04-11 13:52:39 +0800758void LayerChain::ModifyCreateInfo(VkInstanceCreateInfo& info) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800759 if (layer_count_) {
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800760 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 Wu0c203242016-03-15 13:44:51 +0800765
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800766 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 Wu0c203242016-03-15 13:44:51 +0800771
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800772 info.pNext = &cb_info;
Chia-I Wu0c203242016-03-15 13:44:51 +0800773 }
774
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800775 if (override_layers_.Count()) {
776 info.enabledLayerCount = override_layers_.Count();
777 info.ppEnabledLayerNames = override_layers_.Names();
Chia-I Wu0c203242016-03-15 13:44:51 +0800778 }
779
Chia-I Wuc3fa20c2016-04-11 13:47:31 +0800780 if (override_extensions_.Count()) {
781 info.enabledExtensionCount = override_extensions_.Count();
782 info.ppEnabledExtensionNames = override_extensions_.Names();
Chia-I Wu0c203242016-03-15 13:44:51 +0800783 }
784}
785
Chia-I Wueef27fa2016-04-11 13:52:39 +0800786void LayerChain::ModifyCreateInfo(VkDeviceCreateInfo& info) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800787 if (layer_count_) {
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800788 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 Wu0c203242016-03-15 13:44:51 +0800793
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800794 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 Wu0c203242016-03-15 13:44:51 +0800799
Chia-I Wu94a2c0e2016-04-13 10:20:59 +0800800 info.pNext = &cb_info;
Chia-I Wu0c203242016-03-15 13:44:51 +0800801 }
802
Chia-I Wu026b8fa2016-04-11 13:44:13 +0800803 if (override_layers_.Count()) {
804 info.enabledLayerCount = override_layers_.Count();
805 info.ppEnabledLayerNames = override_layers_.Names();
Chia-I Wu0c203242016-03-15 13:44:51 +0800806 }
807
Chia-I Wuc3fa20c2016-04-11 13:47:31 +0800808 if (override_extensions_.Count()) {
809 info.enabledExtensionCount = override_extensions_.Count();
810 info.ppEnabledExtensionNames = override_extensions_.Names();
Chia-I Wu0c203242016-03-15 13:44:51 +0800811 }
812}
813
Chia-I Wueef27fa2016-04-11 13:52:39 +0800814VkResult LayerChain::Create(const VkInstanceCreateInfo* create_info,
Chia-I Wu0c203242016-03-15 13:44:51 +0800815 const VkAllocationCallbacks* allocator,
816 VkInstance* instance_out) {
Chia-I Wueef27fa2016-04-11 13:52:39 +0800817 VkResult result = ValidateExtensions(create_info->ppEnabledExtensionNames,
818 create_info->enabledExtensionCount);
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800819 if (result != VK_SUCCESS)
820 return result;
821
Chia-I Wu0c203242016-03-15 13:44:51 +0800822 // 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 Wu1f8f46b2016-04-06 14:17:48 +0800827 result = create_instance(create_info, allocator, &instance);
Chia-I Wu0c203242016-03-15 13:44:51 +0800828 if (result != VK_SUCCESS)
829 return result;
830
831 // initialize InstanceData
832 InstanceData& data = GetData(instance);
Chia-I Wu0c203242016-03-15 13:44:51 +0800833
Chia-I Wu8925efd2016-04-13 15:13:21 +0800834 if (!InitDispatchTable(instance, get_instance_proc_addr_,
835 enabled_extensions_)) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800836 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 Wuc3fa20c2016-04-11 13:47:31 +0800843 if (override_extensions_.InstallDebugCallback()) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800844 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 Wueef27fa2016-04-11 13:52:39 +0800863 debug_callback_info.pfnCallback = DebugReportCallback;
Chia-I Wu0c203242016-03-15 13:44:51 +0800864
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 Wueef27fa2016-04-11 13:52:39 +0800879 StealLayers(data);
Chia-I Wu0c203242016-03-15 13:44:51 +0800880
881 *instance_out = instance;
882
883 return VK_SUCCESS;
884}
885
Chia-I Wueef27fa2016-04-11 13:52:39 +0800886VkResult LayerChain::Create(VkPhysicalDevice physical_dev,
Chia-I Wu0c203242016-03-15 13:44:51 +0800887 const VkDeviceCreateInfo* create_info,
888 const VkAllocationCallbacks* allocator,
889 VkDevice* dev_out) {
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800890 VkResult result =
Chia-I Wueef27fa2016-04-11 13:52:39 +0800891 ValidateExtensions(physical_dev, create_info->ppEnabledExtensionNames,
892 create_info->enabledExtensionCount);
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800893 if (result != VK_SUCCESS)
894 return result;
895
Chia-I Wu0c203242016-03-15 13:44:51 +0800896 // call down the chain
Chia-I Wuc3a28912016-04-14 11:55:51 +0800897 PFN_vkCreateDevice create_device =
898 GetData(physical_dev).dispatch.CreateDevice;
Chia-I Wu0c203242016-03-15 13:44:51 +0800899 VkDevice dev;
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800900 result = create_device(physical_dev, create_info, allocator, &dev);
Chia-I Wu0c203242016-03-15 13:44:51 +0800901 if (result != VK_SUCCESS)
902 return result;
903
904 // initialize DeviceData
905 DeviceData& data = GetData(dev);
Chia-I Wu0c203242016-03-15 13:44:51 +0800906
Chia-I Wu8925efd2016-04-13 15:13:21 +0800907 if (!InitDispatchTable(dev, get_device_proc_addr_, enabled_extensions_)) {
Chia-I Wu0c203242016-03-15 13:44:51 +0800908 if (data.dispatch.DestroyDevice)
909 data.dispatch.DestroyDevice(dev, allocator);
910
911 return VK_ERROR_INITIALIZATION_FAILED;
912 }
913
Chia-I Wuc3a28912016-04-14 11:55:51 +0800914 // no StealLayers so that active layers are destroyed with this
915 // LayerChain
Chia-I Wu0c203242016-03-15 13:44:51 +0800916 *dev_out = dev;
917
918 return VK_SUCCESS;
919}
920
Chia-I Wueef27fa2016-04-11 13:52:39 +0800921VkResult LayerChain::ValidateExtensions(const char* const* extension_names,
922 uint32_t extension_count) {
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800923 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 Wueef27fa2016-04-11 13:52:39 +0800931 driver_extensions_ = AllocateDriverExtensionArray(count);
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800932 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 Wueef27fa2016-04-11 13:52:39 +0800943 if (!IsLayerExtension(name) && !IsDriverExtension(name)) {
Chia-I Wua4a05552016-05-05 11:57:23 +0800944 logger_.Err(VK_NULL_HANDLE,
945 "Failed to enable missing instance extension %s", name);
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800946 return VK_ERROR_EXTENSION_NOT_PRESENT;
947 }
Chia-I Wu8925efd2016-04-13 15:13:21 +0800948
949 auto ext_bit = driver::GetProcHookExtension(name);
950 if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN)
951 enabled_extensions_.set(ext_bit);
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800952 }
953
954 return VK_SUCCESS;
955}
956
Chia-I Wueef27fa2016-04-11 13:52:39 +0800957VkResult LayerChain::ValidateExtensions(VkPhysicalDevice physical_dev,
958 const char* const* extension_names,
959 uint32_t extension_count) {
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800960 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) {
Chia-I Wueef27fa2016-04-11 13:52:39 +0800968 driver_extensions_ = AllocateDriverExtensionArray(count);
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800969 result = (driver_extensions_)
970 ? EnumerateDeviceExtensionProperties(
971 physical_dev, nullptr, &count, driver_extensions_)
972 : VK_ERROR_OUT_OF_HOST_MEMORY;
973 }
974 if (result != VK_SUCCESS)
975 return result;
976
977 driver_extension_count_ = count;
978
979 for (uint32_t i = 0; i < extension_count; i++) {
980 const char* name = extension_names[i];
Chia-I Wueef27fa2016-04-11 13:52:39 +0800981 if (!IsLayerExtension(name) && !IsDriverExtension(name)) {
Chia-I Wua4a05552016-05-05 11:57:23 +0800982 logger_.Err(physical_dev,
983 "Failed to enable missing device extension %s", name);
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800984 return VK_ERROR_EXTENSION_NOT_PRESENT;
985 }
Chia-I Wu8925efd2016-04-13 15:13:21 +0800986
987 auto ext_bit = driver::GetProcHookExtension(name);
988 if (ext_bit != driver::ProcHook::EXTENSION_UNKNOWN)
989 enabled_extensions_.set(ext_bit);
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800990 }
991
992 return VK_SUCCESS;
993}
994
Chia-I Wueef27fa2016-04-11 13:52:39 +0800995VkExtensionProperties* LayerChain::AllocateDriverExtensionArray(
Chia-I Wu1f8f46b2016-04-06 14:17:48 +0800996 uint32_t count) const {
997 return reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
998 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
999 alignof(VkExtensionProperties), VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
1000}
1001
Chia-I Wueef27fa2016-04-11 13:52:39 +08001002bool LayerChain::IsLayerExtension(const char* name) const {
Chia-I Wudab25652016-04-28 07:15:51 +08001003 if (is_instance_) {
1004 for (uint32_t i = 0; i < layer_count_; i++) {
1005 const ActiveLayer& layer = layers_[i];
1006 if (FindLayerInstanceExtension(*layer.ref, name))
1007 return true;
1008 }
1009 } else {
1010 for (uint32_t i = 0; i < layer_count_; i++) {
1011 const ActiveLayer& layer = layers_[i];
1012 if (FindLayerDeviceExtension(*layer.ref, name))
1013 return true;
1014 }
Chia-I Wu1f8f46b2016-04-06 14:17:48 +08001015 }
1016
1017 return false;
1018}
1019
Chia-I Wueef27fa2016-04-11 13:52:39 +08001020bool LayerChain::IsDriverExtension(const char* name) const {
Chia-I Wu1f8f46b2016-04-06 14:17:48 +08001021 for (uint32_t i = 0; i < driver_extension_count_; i++) {
1022 if (strcmp(driver_extensions_[i].extensionName, name) == 0)
1023 return true;
1024 }
1025
1026 return false;
1027}
1028
Chia-I Wu0c203242016-03-15 13:44:51 +08001029template <typename DataType>
Chia-I Wueef27fa2016-04-11 13:52:39 +08001030void LayerChain::StealLayers(DataType& data) {
Chia-I Wu0c203242016-03-15 13:44:51 +08001031 data.layers = layers_;
1032 data.layer_count = layer_count_;
1033
1034 layers_ = nullptr;
1035 layer_count_ = 0;
1036}
1037
Chia-I Wueef27fa2016-04-11 13:52:39 +08001038void LayerChain::DestroyLayers(ActiveLayer* layers,
1039 uint32_t count,
1040 const VkAllocationCallbacks& allocator) {
Chia-I Wu0c203242016-03-15 13:44:51 +08001041 for (uint32_t i = 0; i < count; i++)
1042 layers[i].ref.~LayerRef();
1043
1044 allocator.pfnFree(allocator.pUserData, layers);
1045}
1046
Chia-I Wu94a2c0e2016-04-13 10:20:59 +08001047VkResult LayerChain::SetInstanceLoaderData(VkInstance instance, void* object) {
1048 driver::InstanceDispatchable dispatchable =
1049 reinterpret_cast<driver::InstanceDispatchable>(object);
1050
1051 return (driver::SetDataInternal(dispatchable, &driver::GetData(instance)))
1052 ? VK_SUCCESS
1053 : VK_ERROR_INITIALIZATION_FAILED;
1054}
1055
1056VkResult LayerChain::SetDeviceLoaderData(VkDevice device, void* object) {
1057 driver::DeviceDispatchable dispatchable =
1058 reinterpret_cast<driver::DeviceDispatchable>(object);
1059
1060 return (driver::SetDataInternal(dispatchable, &driver::GetData(device)))
1061 ? VK_SUCCESS
1062 : VK_ERROR_INITIALIZATION_FAILED;
1063}
1064
Chia-I Wueef27fa2016-04-11 13:52:39 +08001065VkBool32 LayerChain::DebugReportCallback(VkDebugReportFlagsEXT flags,
1066 VkDebugReportObjectTypeEXT obj_type,
1067 uint64_t obj,
1068 size_t location,
1069 int32_t msg_code,
1070 const char* layer_prefix,
1071 const char* msg,
1072 void* user_data) {
Chia-I Wu0c203242016-03-15 13:44:51 +08001073 int prio;
1074
1075 if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT)
1076 prio = ANDROID_LOG_ERROR;
1077 else if (flags & (VK_DEBUG_REPORT_WARNING_BIT_EXT |
1078 VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT))
1079 prio = ANDROID_LOG_WARN;
1080 else if (flags & VK_DEBUG_REPORT_INFORMATION_BIT_EXT)
1081 prio = ANDROID_LOG_INFO;
1082 else if (flags & VK_DEBUG_REPORT_DEBUG_BIT_EXT)
1083 prio = ANDROID_LOG_DEBUG;
1084 else
1085 prio = ANDROID_LOG_UNKNOWN;
1086
1087 LOG_PRI(prio, LOG_TAG, "[%s] Code %d : %s", layer_prefix, msg_code, msg);
1088
1089 (void)obj_type;
1090 (void)obj;
1091 (void)location;
1092 (void)user_data;
1093
1094 return false;
1095}
1096
Chia-I Wueef27fa2016-04-11 13:52:39 +08001097VkResult LayerChain::CreateInstance(const VkInstanceCreateInfo* create_info,
1098 const VkAllocationCallbacks* allocator,
1099 VkInstance* instance_out) {
Courtney Goeltzenleuchter1531bf12016-12-29 08:46:18 -07001100 const driver::DebugReportLogger logger(*create_info);
1101 LayerChain chain(true, logger,
Chia-I Wu0c203242016-03-15 13:44:51 +08001102 (allocator) ? *allocator : driver::GetDefaultAllocator());
1103
Chia-I Wueef27fa2016-04-11 13:52:39 +08001104 VkResult result = chain.ActivateLayers(create_info->ppEnabledLayerNames,
1105 create_info->enabledLayerCount,
1106 create_info->ppEnabledExtensionNames,
1107 create_info->enabledExtensionCount);
Chia-I Wu0c203242016-03-15 13:44:51 +08001108 if (result != VK_SUCCESS)
1109 return result;
1110
1111 // use a local create info when the chain is not empty
1112 VkInstanceCreateInfo local_create_info;
Chia-I Wueef27fa2016-04-11 13:52:39 +08001113 if (!chain.Empty()) {
Chia-I Wu0c203242016-03-15 13:44:51 +08001114 local_create_info = *create_info;
Chia-I Wueef27fa2016-04-11 13:52:39 +08001115 chain.ModifyCreateInfo(local_create_info);
Chia-I Wu0c203242016-03-15 13:44:51 +08001116 create_info = &local_create_info;
1117 }
1118
Chia-I Wueef27fa2016-04-11 13:52:39 +08001119 return chain.Create(create_info, allocator, instance_out);
Chia-I Wu0c203242016-03-15 13:44:51 +08001120}
1121
Chia-I Wueef27fa2016-04-11 13:52:39 +08001122VkResult LayerChain::CreateDevice(VkPhysicalDevice physical_dev,
1123 const VkDeviceCreateInfo* create_info,
1124 const VkAllocationCallbacks* allocator,
1125 VkDevice* dev_out) {
Courtney Goeltzenleuchter1531bf12016-12-29 08:46:18 -07001126 const driver::DebugReportLogger logger = driver::Logger(physical_dev);
Chia-I Wua4a05552016-05-05 11:57:23 +08001127 LayerChain chain(
Courtney Goeltzenleuchter1531bf12016-12-29 08:46:18 -07001128 false, logger,
Chia-I Wua4a05552016-05-05 11:57:23 +08001129 (allocator) ? *allocator : driver::GetData(physical_dev).allocator);
Chia-I Wu0c203242016-03-15 13:44:51 +08001130
Chia-I Wuc3a28912016-04-14 11:55:51 +08001131 VkResult result = chain.ActivateLayers(
1132 physical_dev, create_info->ppEnabledLayerNames,
1133 create_info->enabledLayerCount, create_info->ppEnabledExtensionNames,
1134 create_info->enabledExtensionCount);
Chia-I Wu0c203242016-03-15 13:44:51 +08001135 if (result != VK_SUCCESS)
1136 return result;
1137
1138 // use a local create info when the chain is not empty
1139 VkDeviceCreateInfo local_create_info;
Chia-I Wueef27fa2016-04-11 13:52:39 +08001140 if (!chain.Empty()) {
Chia-I Wu0c203242016-03-15 13:44:51 +08001141 local_create_info = *create_info;
Chia-I Wueef27fa2016-04-11 13:52:39 +08001142 chain.ModifyCreateInfo(local_create_info);
Chia-I Wu0c203242016-03-15 13:44:51 +08001143 create_info = &local_create_info;
1144 }
1145
Chia-I Wueef27fa2016-04-11 13:52:39 +08001146 return chain.Create(physical_dev, create_info, allocator, dev_out);
Chia-I Wu0c203242016-03-15 13:44:51 +08001147}
1148
Chia-I Wueef27fa2016-04-11 13:52:39 +08001149void LayerChain::DestroyInstance(VkInstance instance,
1150 const VkAllocationCallbacks* allocator) {
Chia-I Wu0c203242016-03-15 13:44:51 +08001151 InstanceData& data = GetData(instance);
1152
1153 if (data.debug_callback != VK_NULL_HANDLE)
1154 data.destroy_debug_callback(instance, data.debug_callback, allocator);
1155
1156 ActiveLayer* layers = reinterpret_cast<ActiveLayer*>(data.layers);
1157 uint32_t layer_count = data.layer_count;
1158
1159 VkAllocationCallbacks local_allocator;
1160 if (!allocator)
1161 local_allocator = driver::GetData(instance).allocator;
1162
1163 // this also destroys InstanceData
1164 data.dispatch.DestroyInstance(instance, allocator);
1165
Chia-I Wueef27fa2016-04-11 13:52:39 +08001166 DestroyLayers(layers, layer_count,
1167 (allocator) ? *allocator : local_allocator);
Chia-I Wu0c203242016-03-15 13:44:51 +08001168}
1169
Chia-I Wueef27fa2016-04-11 13:52:39 +08001170void LayerChain::DestroyDevice(VkDevice device,
1171 const VkAllocationCallbacks* allocator) {
Chia-I Wu0c203242016-03-15 13:44:51 +08001172 DeviceData& data = GetData(device);
Chia-I Wu0c203242016-03-15 13:44:51 +08001173 // this also destroys DeviceData
1174 data.dispatch.DestroyDevice(device, allocator);
Chia-I Wuc3a28912016-04-14 11:55:51 +08001175}
Chia-I Wu0c203242016-03-15 13:44:51 +08001176
Chia-I Wuc3a28912016-04-14 11:55:51 +08001177const LayerChain::ActiveLayer* LayerChain::GetActiveLayers(
1178 VkPhysicalDevice physical_dev,
1179 uint32_t& count) {
1180 count = GetData(physical_dev).layer_count;
1181 return reinterpret_cast<const ActiveLayer*>(GetData(physical_dev).layers);
Chia-I Wu0c203242016-03-15 13:44:51 +08001182}
1183
1184// ----------------------------------------------------------------------------
1185
1186bool EnsureInitialized() {
Yiwei Zhang901f8ee2020-07-31 13:18:49 -07001187 static bool initialized = false;
1188 static pid_t init_attempted_for_pid = 0;
1189 static std::mutex init_lock;
Chia-I Wu0c203242016-03-15 13:44:51 +08001190
Yiwei Zhang901f8ee2020-07-31 13:18:49 -07001191 std::lock_guard<std::mutex> lock(init_lock);
1192 if (init_attempted_for_pid == getpid())
1193 return initialized;
Chia-I Wu0c203242016-03-15 13:44:51 +08001194
Yiwei Zhang901f8ee2020-07-31 13:18:49 -07001195 init_attempted_for_pid = getpid();
1196 if (driver::OpenHAL()) {
1197 DiscoverLayers();
1198 initialized = true;
Yiwei Zhang9dfc93a2019-08-09 17:25:24 -07001199 }
1200
Chia-I Wu0c203242016-03-15 13:44:51 +08001201 return initialized;
1202}
1203
Peiyong Lin27800492020-03-25 15:44:28 -07001204template <typename Functor>
1205void ForEachLayerFromSettings(Functor functor) {
1206 const std::string layersSetting =
1207 android::GraphicsEnv::getInstance().getDebugLayers();
1208 if (!layersSetting.empty()) {
1209 std::vector<std::string> layers =
1210 android::base::Split(layersSetting, ":");
1211 for (uint32_t i = 0; i < layers.size(); i++) {
1212 const Layer* layer = FindLayer(layers[i].c_str());
1213 if (!layer) {
1214 continue;
1215 }
1216 functor(layer);
1217 }
1218 }
1219}
1220
Chia-I Wu0c203242016-03-15 13:44:51 +08001221} // anonymous namespace
1222
1223VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1224 const VkAllocationCallbacks* pAllocator,
1225 VkInstance* pInstance) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001226 ATRACE_CALL();
1227
Chia-I Wu0c203242016-03-15 13:44:51 +08001228 if (!EnsureInitialized())
1229 return VK_ERROR_INITIALIZATION_FAILED;
1230
Chia-I Wueef27fa2016-04-11 13:52:39 +08001231 return LayerChain::CreateInstance(pCreateInfo, pAllocator, pInstance);
Chia-I Wu0c203242016-03-15 13:44:51 +08001232}
1233
1234void DestroyInstance(VkInstance instance,
1235 const VkAllocationCallbacks* pAllocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001236 ATRACE_CALL();
1237
Chia-I Wu0c203242016-03-15 13:44:51 +08001238 if (instance != VK_NULL_HANDLE)
Chia-I Wueef27fa2016-04-11 13:52:39 +08001239 LayerChain::DestroyInstance(instance, pAllocator);
Chia-I Wu0c203242016-03-15 13:44:51 +08001240}
1241
1242VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1243 const VkDeviceCreateInfo* pCreateInfo,
1244 const VkAllocationCallbacks* pAllocator,
1245 VkDevice* pDevice) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001246 ATRACE_CALL();
1247
Chia-I Wueef27fa2016-04-11 13:52:39 +08001248 return LayerChain::CreateDevice(physicalDevice, pCreateInfo, pAllocator,
1249 pDevice);
Chia-I Wu0c203242016-03-15 13:44:51 +08001250}
1251
1252void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001253 ATRACE_CALL();
1254
Chia-I Wu0c203242016-03-15 13:44:51 +08001255 if (device != VK_NULL_HANDLE)
Chia-I Wueef27fa2016-04-11 13:52:39 +08001256 LayerChain::DestroyDevice(device, pAllocator);
Chia-I Wu0c203242016-03-15 13:44:51 +08001257}
1258
1259VkResult EnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
1260 VkLayerProperties* pProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001261 ATRACE_CALL();
1262
Chia-I Wu0c203242016-03-15 13:44:51 +08001263 if (!EnsureInitialized())
Yiwei Zhangc889d3c2020-07-16 13:51:12 -07001264 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu0c203242016-03-15 13:44:51 +08001265
Chia-I Wu25700b42016-04-28 06:36:09 +08001266 uint32_t count = GetLayerCount();
Chia-I Wu0c203242016-03-15 13:44:51 +08001267
Chia-I Wu25700b42016-04-28 06:36:09 +08001268 if (!pProperties) {
Chia-I Wu0c203242016-03-15 13:44:51 +08001269 *pPropertyCount = count;
Chia-I Wu25700b42016-04-28 06:36:09 +08001270 return VK_SUCCESS;
1271 }
Chia-I Wu0c203242016-03-15 13:44:51 +08001272
Chia-I Wu25700b42016-04-28 06:36:09 +08001273 uint32_t copied = std::min(*pPropertyCount, count);
1274 for (uint32_t i = 0; i < copied; i++)
1275 pProperties[i] = GetLayerProperties(GetLayer(i));
1276 *pPropertyCount = copied;
1277
1278 return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE;
Chia-I Wu0c203242016-03-15 13:44:51 +08001279}
1280
1281VkResult EnumerateInstanceExtensionProperties(
1282 const char* pLayerName,
1283 uint32_t* pPropertyCount,
1284 VkExtensionProperties* pProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001285 ATRACE_CALL();
1286
Chia-I Wu0c203242016-03-15 13:44:51 +08001287 if (!EnsureInitialized())
Yiwei Zhangc889d3c2020-07-16 13:51:12 -07001288 return VK_ERROR_OUT_OF_HOST_MEMORY;
Chia-I Wu0c203242016-03-15 13:44:51 +08001289
1290 if (pLayerName) {
Chia-I Wu04c65512016-04-27 09:54:02 +08001291 const Layer* layer = FindLayer(pLayerName);
Chia-I Wu6184b202016-04-27 11:57:53 +08001292 if (!layer)
1293 return VK_ERROR_LAYER_NOT_PRESENT;
1294
1295 uint32_t count;
1296 const VkExtensionProperties* props =
1297 GetLayerInstanceExtensions(*layer, count);
Chia-I Wu0c203242016-03-15 13:44:51 +08001298
1299 if (!pProperties || *pPropertyCount > count)
1300 *pPropertyCount = count;
1301 if (pProperties)
1302 std::copy(props, props + *pPropertyCount, pProperties);
1303
1304 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1305 }
1306
Peiyong Lin8f4435a2020-03-11 17:43:28 -07001307 // If the pLayerName is nullptr, we must advertise all instance extensions
1308 // from all implicitly enabled layers and the driver implementation. If
1309 // there are duplicates among layers and the driver implementation, always
1310 // only preserve the top layer closest to the application regardless of the
1311 // spec version.
1312 std::vector<VkExtensionProperties> properties;
1313 std::unordered_set<std::string> extensionNames;
1314
1315 // Expose extensions from implicitly enabled layers.
Peiyong Lin27800492020-03-25 15:44:28 -07001316 ForEachLayerFromSettings([&](const Layer* layer) {
1317 uint32_t count = 0;
1318 const VkExtensionProperties* props =
1319 GetLayerInstanceExtensions(*layer, count);
1320 if (count > 0) {
1321 for (uint32_t i = 0; i < count; ++i) {
1322 if (extensionNames.emplace(props[i].extensionName).second) {
1323 properties.push_back(props[i]);
Peiyong Lin8f4435a2020-03-11 17:43:28 -07001324 }
1325 }
1326 }
Peiyong Lin27800492020-03-25 15:44:28 -07001327 });
Peiyong Lin8f4435a2020-03-11 17:43:28 -07001328
1329 // TODO(b/143293104): Parse debug.vulkan.layers properties
1330
1331 // Expose extensions from driver implementation.
1332 {
1333 uint32_t count = 0;
1334 VkResult result = vulkan::driver::EnumerateInstanceExtensionProperties(
1335 nullptr, &count, nullptr);
1336 if (result == VK_SUCCESS && count > 0) {
1337 std::vector<VkExtensionProperties> props(count);
1338 result = vulkan::driver::EnumerateInstanceExtensionProperties(
1339 nullptr, &count, props.data());
1340 for (auto prop : props) {
1341 if (extensionNames.emplace(prop.extensionName).second) {
1342 properties.push_back(prop);
1343 }
1344 }
1345 }
1346 }
1347
1348 uint32_t totalCount = properties.size();
1349 if (!pProperties || *pPropertyCount > totalCount) {
1350 *pPropertyCount = totalCount;
1351 }
1352 if (pProperties) {
1353 std::copy(properties.data(), properties.data() + *pPropertyCount,
1354 pProperties);
1355 }
1356 return *pPropertyCount < totalCount ? VK_INCOMPLETE : VK_SUCCESS;
Chia-I Wu0c203242016-03-15 13:44:51 +08001357}
1358
1359VkResult EnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice,
1360 uint32_t* pPropertyCount,
1361 VkLayerProperties* pProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001362 ATRACE_CALL();
1363
Chia-I Wuc3a28912016-04-14 11:55:51 +08001364 uint32_t count;
1365 const LayerChain::ActiveLayer* layers =
1366 LayerChain::GetActiveLayers(physicalDevice, count);
Chia-I Wu0c203242016-03-15 13:44:51 +08001367
Chia-I Wu25700b42016-04-28 06:36:09 +08001368 if (!pProperties) {
Chia-I Wu0c203242016-03-15 13:44:51 +08001369 *pPropertyCount = count;
Chia-I Wu25700b42016-04-28 06:36:09 +08001370 return VK_SUCCESS;
1371 }
Chia-I Wu0c203242016-03-15 13:44:51 +08001372
Chia-I Wuc3a28912016-04-14 11:55:51 +08001373 uint32_t copied = std::min(*pPropertyCount, count);
1374 for (uint32_t i = 0; i < copied; i++)
1375 pProperties[i] = GetLayerProperties(*layers[i].ref);
Chia-I Wu25700b42016-04-28 06:36:09 +08001376 *pPropertyCount = copied;
1377
1378 return (copied == count) ? VK_SUCCESS : VK_INCOMPLETE;
Chia-I Wu0c203242016-03-15 13:44:51 +08001379}
1380
1381VkResult EnumerateDeviceExtensionProperties(
1382 VkPhysicalDevice physicalDevice,
1383 const char* pLayerName,
1384 uint32_t* pPropertyCount,
1385 VkExtensionProperties* pProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001386 ATRACE_CALL();
1387
Chia-I Wu0c203242016-03-15 13:44:51 +08001388 if (pLayerName) {
Chia-I Wuc3a28912016-04-14 11:55:51 +08001389 // EnumerateDeviceLayerProperties enumerates active layers for
1390 // backward compatibility. The extension query here should work for
1391 // all layers.
Chia-I Wu04c65512016-04-27 09:54:02 +08001392 const Layer* layer = FindLayer(pLayerName);
Chia-I Wuc3a28912016-04-14 11:55:51 +08001393 if (!layer)
Chia-I Wu6184b202016-04-27 11:57:53 +08001394 return VK_ERROR_LAYER_NOT_PRESENT;
1395
1396 uint32_t count;
1397 const VkExtensionProperties* props =
1398 GetLayerDeviceExtensions(*layer, count);
Chia-I Wu0c203242016-03-15 13:44:51 +08001399
1400 if (!pProperties || *pPropertyCount > count)
1401 *pPropertyCount = count;
1402 if (pProperties)
1403 std::copy(props, props + *pPropertyCount, pProperties);
1404
1405 return *pPropertyCount < count ? VK_INCOMPLETE : VK_SUCCESS;
1406 }
1407
Peiyong Lin27800492020-03-25 15:44:28 -07001408 // If the pLayerName is nullptr, we must advertise all device extensions
1409 // from all implicitly enabled layers and the driver implementation. If
1410 // there are duplicates among layers and the driver implementation, always
1411 // only preserve the top layer closest to the application regardless of the
1412 // spec version.
1413 std::vector<VkExtensionProperties> properties;
1414 std::unordered_set<std::string> extensionNames;
1415
1416 // Expose extensions from implicitly enabled layers.
1417 ForEachLayerFromSettings([&](const Layer* layer) {
1418 uint32_t count = 0;
1419 const VkExtensionProperties* props =
1420 GetLayerDeviceExtensions(*layer, count);
1421 if (count > 0) {
1422 for (uint32_t i = 0; i < count; ++i) {
1423 if (extensionNames.emplace(props[i].extensionName).second) {
1424 properties.push_back(props[i]);
1425 }
1426 }
1427 }
1428 });
1429
1430 // TODO(b/143293104): Parse debug.vulkan.layers properties
1431
1432 // Expose extensions from driver implementation.
1433 {
1434 const InstanceData& data = GetData(physicalDevice);
1435 uint32_t count = 0;
1436 VkResult result = data.dispatch.EnumerateDeviceExtensionProperties(
1437 physicalDevice, nullptr, &count, nullptr);
1438 if (result == VK_SUCCESS && count > 0) {
1439 std::vector<VkExtensionProperties> props(count);
1440 result = data.dispatch.EnumerateDeviceExtensionProperties(
1441 physicalDevice, nullptr, &count, props.data());
1442 for (auto prop : props) {
1443 if (extensionNames.emplace(prop.extensionName).second) {
1444 properties.push_back(prop);
1445 }
1446 }
1447 }
1448 }
1449
1450 uint32_t totalCount = properties.size();
1451 if (!pProperties || *pPropertyCount > totalCount) {
1452 *pPropertyCount = totalCount;
1453 }
1454 if (pProperties) {
1455 std::copy(properties.data(), properties.data() + *pPropertyCount,
1456 pProperties);
1457 }
1458 return *pPropertyCount < totalCount ? VK_INCOMPLETE : VK_SUCCESS;
Chia-I Wu0c203242016-03-15 13:44:51 +08001459}
1460
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001461VkResult EnumerateInstanceVersion(uint32_t* pApiVersion) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001462 ATRACE_CALL();
1463
Yiwei Zhangc889d3c2020-07-16 13:51:12 -07001464 // Load the driver here if not done yet. This api will be used in Zygote
1465 // for Vulkan driver pre-loading because of the minimum overhead.
1466 if (!EnsureInitialized())
1467 return VK_ERROR_OUT_OF_HOST_MEMORY;
1468
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001469 *pApiVersion = VK_API_VERSION_1_1;
1470 return VK_SUCCESS;
1471}
1472
Chia-I Wu0c203242016-03-15 13:44:51 +08001473} // namespace api
1474} // namespace vulkan