blob: e9204ab65a16d1e5fa5554e90cc9433a3a410c51 [file] [log] [blame]
Chia-I Wu9d518162016-03-24 14:55:27 +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
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang5e862202019-06-21 14:59:16 -070019#include "driver.h"
20
21#include <dlfcn.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <malloc.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080023#include <stdlib.h>
24#include <string.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080025
Sundong Ahnbc37dd52020-04-23 21:21:00 +090026#include <SurfaceFlingerProperties.h>
27#include <android-base/properties.h>
Jesse Hall53457db2016-12-14 16:54:06 -080028#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080029#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
30#include <configstore/Utils.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070032#include <log/log.h>
33#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080034#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080035#include <utils/Trace.h>
Yiwei Zhang40e84f12020-10-14 08:42:26 -070036#include <vndksupport/linker.h>
Jesse Hall53457db2016-12-14 16:54:06 -080037
Yiwei Zhang5e862202019-06-21 14:59:16 -070038#include <algorithm>
39#include <array>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070040#include <climits>
Yiwei Zhang5e862202019-06-21 14:59:16 -070041#include <new>
42#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070043
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070044#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080045
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080046using namespace android::hardware::configstore;
47using namespace android::hardware::configstore::V1_0;
48
Jooyung Han749b0ec2023-11-15 13:38:06 +090049extern "C" android_namespace_t* android_get_exported_namespace(const char*);
50
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080051// #define ENABLE_ALLOC_CALLSTACKS 1
52#if ENABLE_ALLOC_CALLSTACKS
53#include <utils/CallStack.h>
54#define ALOGD_CALLSTACK(...) \
55 do { \
56 ALOGD(__VA_ARGS__); \
57 android::CallStack callstack; \
58 callstack.update(); \
59 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
60 } while (false)
61#else
62#define ALOGD_CALLSTACK(...) \
63 do { \
64 } while (false)
65#endif
66
Chia-I Wu9d518162016-03-24 14:55:27 +080067namespace vulkan {
68namespace driver {
69
Chia-I Wu136b8eb2016-03-24 15:01:52 +080070namespace {
71
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080072class Hal {
73 public:
74 static bool Open();
75
76 static const Hal& Get() { return hal_; }
77 static const hwvulkan_device_t& Device() { return *Get().dev_; }
78
Chia-I Wu31938252016-05-23 15:31:02 +080079 int GetDebugReportIndex() const { return debug_report_index_; }
80
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080081 private:
Chia-I Wu31938252016-05-23 15:31:02 +080082 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080083 Hal(const Hal&) = delete;
84 Hal& operator=(const Hal&) = delete;
85
Yiwei Zhang901f8ee2020-07-31 13:18:49 -070086 bool ShouldUnloadBuiltinDriver();
87 void UnloadBuiltinDriver();
Chia-I Wu31938252016-05-23 15:31:02 +080088 bool InitDebugReportIndex();
89
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080090 static Hal hal_;
91
92 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080093 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080094};
95
Chia-I Wu4901db72016-03-24 16:38:58 +080096class CreateInfoWrapper {
97 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080098 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -070099 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800100 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +0800101 CreateInfoWrapper(VkPhysicalDevice physical_dev,
102 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700103 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800104 const VkAllocationCallbacks& allocator);
105 ~CreateInfoWrapper();
106
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800107 VkResult Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +0800108
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800109 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
110 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800111
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800112 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800113 explicit operator const VkDeviceCreateInfo*() const;
114
115 private:
116 struct ExtensionFilter {
117 VkExtensionProperties* exts;
118 uint32_t ext_count;
119
120 const char** names;
121 uint32_t name_count;
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700122 ExtensionFilter()
123 : exts(nullptr), ext_count(0), names(nullptr), name_count(0) {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800124 };
125
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700126 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800127 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800128 VkResult SanitizeLayers();
129 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800130
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800131 VkResult QueryExtensionCount(uint32_t& count) const;
132 VkResult EnumerateExtensions(uint32_t& count,
133 VkExtensionProperties* props) const;
134 VkResult InitExtensionFilter();
135 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800136
137 const bool is_instance_;
138 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700139 const uint32_t loader_api_version_;
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700140 const uint32_t icd_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800141
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800142 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800143
144 union {
145 VkInstanceCreateInfo instance_info_;
146 VkDeviceCreateInfo dev_info_;
147 };
148
Ian Elliottf3e872d2017-11-02 10:15:13 -0600149 VkApplicationInfo application_info_;
150
Chia-I Wu4901db72016-03-24 16:38:58 +0800151 ExtensionFilter extension_filter_;
152
153 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
154 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
155};
156
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800157Hal Hal::hal_;
158
Jesse Hall53457db2016-12-14 16:54:06 -0800159const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700160 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800161 "ro.board.platform",
162}};
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700163constexpr int LIB_DL_FLAGS = RTLD_LOCAL | RTLD_NOW;
Jooyung Han749b0ec2023-11-15 13:38:06 +0900164constexpr char RO_VULKAN_APEX_PROPERTY[] = "ro.vulkan.apex";
Jesse Hall53457db2016-12-14 16:54:06 -0800165
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800166// LoadDriver returns:
167// * 0 when succeed, or
168// * -ENOENT when fail to open binary libraries, or
169// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
170// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700171int LoadDriver(android_namespace_t* library_namespace,
Jooyung Han749b0ec2023-11-15 13:38:06 +0900172 const char* ns_name,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700173 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800174 ATRACE_CALL();
175
Jesse Hall53457db2016-12-14 16:54:06 -0800176 void* so = nullptr;
Jesse Hall53457db2016-12-14 16:54:06 -0800177 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700178 std::string lib_name = android::base::GetProperty(key, "");
179 if (lib_name.empty())
180 continue;
181
182 lib_name = "vulkan." + lib_name + ".so";
183 if (library_namespace) {
184 // load updated driver
185 const android_dlextinfo dlextinfo = {
186 .flags = ANDROID_DLEXT_USE_NAMESPACE,
187 .library_namespace = library_namespace,
188 };
189 so = android_dlopen_ext(lib_name.c_str(), LIB_DL_FLAGS, &dlextinfo);
Jooyung Han27e31082023-11-13 13:50:42 +0900190 if (!so) {
Jooyung Han749b0ec2023-11-15 13:38:06 +0900191 ALOGE("Could not load %s from %s namespace: %s.",
192 lib_name.c_str(), ns_name, dlerror());
Jooyung Han27e31082023-11-13 13:50:42 +0900193 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700194 } else {
195 // load built-in driver
196 so = android_load_sphal_library(lib_name.c_str(), LIB_DL_FLAGS);
Jesse Hall53457db2016-12-14 16:54:06 -0800197 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700198 if (so)
199 break;
Jesse Hall53457db2016-12-14 16:54:06 -0800200 }
201 if (!so)
202 return -ENOENT;
203
Jesse Hall00e61ff2017-04-07 16:48:02 -0700204 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800205 if (!hmi) {
206 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
207 dlclose(so);
208 return -EINVAL;
209 }
210 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
211 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
212 dlclose(so);
213 return -EINVAL;
214 }
215 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700216 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800217 return 0;
218}
219
Jooyung Han749b0ec2023-11-15 13:38:06 +0900220int LoadDriverFromApex(const hwvulkan_module_t** module) {
221 ATRACE_CALL();
222
223 auto apex_name = android::base::GetProperty(RO_VULKAN_APEX_PROPERTY, "");
224 if (apex_name == "") {
225 return -ENOENT;
226 }
227 // Get linker namespace for Vulkan APEX
228 std::replace(apex_name.begin(), apex_name.end(), '.', '_');
229 auto ns = android_get_exported_namespace(apex_name.c_str());
230 if (!ns) {
231 return -ENOENT;
232 }
233 android::GraphicsEnv::getInstance().setDriverToLoad(
234 android::GpuStatsInfo::Driver::VULKAN);
235 return LoadDriver(ns, apex_name.c_str(), module);
236}
237
Jesse Hall00e61ff2017-04-07 16:48:02 -0700238int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800239 ATRACE_CALL();
240
Yiwei Zhangd9861812019-02-13 11:51:55 -0800241 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700242 android::GpuStatsInfo::Driver::VULKAN);
Jooyung Han749b0ec2023-11-15 13:38:06 +0900243 return LoadDriver(nullptr, nullptr, module);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700244}
245
246int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800247 ATRACE_CALL();
248
Jesse Hall00e61ff2017-04-07 16:48:02 -0700249 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
250 if (!ns)
251 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800252 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700253 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Jooyung Han749b0ec2023-11-15 13:38:06 +0900254 int result = LoadDriver(ns, "updatable gfx driver", module);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800255 if (result != 0) {
256 LOG_ALWAYS_FATAL(
257 "couldn't find an updated Vulkan implementation from %s",
258 android::GraphicsEnv::getInstance().getDriverPath().c_str());
259 }
260 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700261}
262
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800263bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800264 ATRACE_CALL();
265
Yiwei Zhangd9861812019-02-13 11:51:55 -0800266 const nsecs_t openTime = systemTime();
267
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700268 if (hal_.ShouldUnloadBuiltinDriver()) {
269 hal_.UnloadBuiltinDriver();
270 }
271
272 if (hal_.dev_)
273 return true;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800274
275 // Use a stub device unless we successfully open a real HAL device.
276 hal_.dev_ = &stubhal::kDevice;
277
Jesse Hall53457db2016-12-14 16:54:06 -0800278 int result;
279 const hwvulkan_module_t* module = nullptr;
280
Jesse Hall00e61ff2017-04-07 16:48:02 -0700281 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800282 if (result == -ENOENT) {
Jooyung Han749b0ec2023-11-15 13:38:06 +0900283 result = LoadDriverFromApex(&module);
284 }
285 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700286 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800287 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800288 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800289 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700290 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800291 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800292 return true;
293 }
294
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800295
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800296 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800297 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800298 result =
299 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
300 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800301 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800302 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800303 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700304 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800305 // Any device with a Vulkan HAL should be able to open the device.
306 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
307 result);
308 return false;
309 }
310
311 hal_.dev_ = device;
312
Chia-I Wu31938252016-05-23 15:31:02 +0800313 hal_.InitDebugReportIndex();
314
Yiwei Zhangd9861812019-02-13 11:51:55 -0800315 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700316 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800317
Chia-I Wu31938252016-05-23 15:31:02 +0800318 return true;
319}
320
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700321bool Hal::ShouldUnloadBuiltinDriver() {
322 // Should not unload since the driver was not loaded
323 if (!hal_.dev_)
324 return false;
325
326 // Should not unload if stubhal is used on the device
327 if (hal_.dev_ == &stubhal::kDevice)
328 return false;
329
330 // Unload the driver if updated driver is chosen
331 if (android::GraphicsEnv::getInstance().getDriverNamespace())
332 return true;
333
334 return false;
335}
336
337void Hal::UnloadBuiltinDriver() {
338 ATRACE_CALL();
339
340 ALOGD("Unload builtin Vulkan driver.");
341
Rob VanReenen189318d2024-08-01 12:07:00 -0700342 if (hal_.dev_->common.close != nullptr)
343 {
344 // Close the opened device
345 int err = hal_.dev_->common.close(
346 const_cast<struct hw_device_t*>(&hal_.dev_->common));
347 ALOG_ASSERT(!err, "hw_device_t::close() failed.");
348 }
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700349
350 // Close the opened shared library in the hw_module_t
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700351 android_unload_sphal_library(hal_.dev_->common.module->dso);
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700352
353 hal_.dev_ = nullptr;
354 hal_.debug_report_index_ = -1;
355}
356
Chia-I Wu31938252016-05-23 15:31:02 +0800357bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800358 ATRACE_CALL();
359
Chia-I Wu31938252016-05-23 15:31:02 +0800360 uint32_t count;
361 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
362 VK_SUCCESS) {
363 ALOGE("failed to get HAL instance extension count");
364 return false;
365 }
366
367 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
368 malloc(sizeof(VkExtensionProperties) * count));
369 if (!exts) {
370 ALOGE("failed to allocate HAL instance extension array");
371 return false;
372 }
373
374 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
375 VK_SUCCESS) {
376 ALOGE("failed to enumerate HAL instance extensions");
377 free(exts);
378 return false;
379 }
380
381 for (uint32_t i = 0; i < count; i++) {
382 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
383 0) {
384 debug_report_index_ = static_cast<int>(i);
385 break;
386 }
387 }
388
389 free(exts);
390
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800391 return true;
392}
393
394CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700395 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800396 const VkAllocationCallbacks& allocator)
397 : is_instance_(true),
398 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000399 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700400 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800401 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800402 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700403 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800404
Chia-I Wu4901db72016-03-24 16:38:58 +0800405CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
406 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700407 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800408 const VkAllocationCallbacks& allocator)
409 : is_instance_(false),
410 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000411 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700412 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800413 physical_dev_(physical_dev),
414 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700415 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800416
417CreateInfoWrapper::~CreateInfoWrapper() {
418 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
419 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
420}
421
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800422VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700423 VkResult result = SanitizeApiVersion();
424 if (result == VK_SUCCESS)
425 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800426 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800427 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800428 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800429 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800430
431 return result;
432}
433
434const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800435CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800436 return hook_extensions_;
437}
438
439const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800440CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800441 return hal_extensions_;
442}
443
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800444CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
445 return &instance_info_;
446}
447
Chia-I Wu4901db72016-03-24 16:38:58 +0800448CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
449 return &dev_info_;
450}
451
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700452VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700453 if (!is_instance_ || !instance_info_.pApplicationInfo)
454 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700455
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700456 if (icd_api_version_ > VK_API_VERSION_1_0 ||
457 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
458 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700459
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700460 // override apiVersion to avoid error return from 1.0 icd
461 application_info_ = *instance_info_.pApplicationInfo;
462 application_info_.apiVersion = VK_API_VERSION_1_0;
463 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700464
465 return VK_SUCCESS;
466}
467
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800468VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800469 const struct StructHeader {
470 VkStructureType type;
471 const void* next;
472 } * header;
473
474 if (is_instance_) {
475 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
476
477 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
478 while (header &&
479 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
480 header = reinterpret_cast<const StructHeader*>(header->next);
481
482 instance_info_.pNext = header;
483 } else {
484 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
485
486 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
487 while (header &&
488 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
489 header = reinterpret_cast<const StructHeader*>(header->next);
490
491 dev_info_.pNext = header;
492 }
493
494 return VK_SUCCESS;
495}
496
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800497VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800498 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
499 : dev_info_.ppEnabledLayerNames;
500 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
501 : dev_info_.enabledLayerCount;
502
503 // remove all layers
504 layer_names = nullptr;
505 layer_count = 0;
506
507 return VK_SUCCESS;
508}
509
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800510VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800511 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
512 : dev_info_.ppEnabledExtensionNames;
513 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
514 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800515
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800516 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800517 if (result != VK_SUCCESS)
518 return result;
519
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700520 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700521 for (uint32_t i = 0; i < ext_count; i++) {
522 // Upon api downgrade, skip the promoted instance extensions in the
523 // first pass to avoid duplicate extensions.
524 const std::optional<uint32_t> version =
525 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700526 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700527 *version <= loader_api_version_)
528 continue;
529
530 FilterExtension(ext_names[i]);
531 }
532
533 // Enable the required extensions to support core functionalities.
534 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700535 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700536 for (const auto& promoted_extension : promoted_extensions)
537 FilterExtension(promoted_extension);
538 } else {
539 for (uint32_t i = 0; i < ext_count; i++)
540 FilterExtension(ext_names[i]);
541 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800542
Jesse Halld3d887a2018-03-05 13:34:45 -0800543 // Enable device extensions that contain physical-device commands, so that
544 // vkGetInstanceProcAddr will return those physical-device commands.
545 if (is_instance_) {
546 hook_extensions_.set(ProcHook::KHR_swapchain);
547 }
548
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700549 const uint32_t api_version =
550 is_instance_ ? loader_api_version_
551 : std::min(icd_api_version_, loader_api_version_);
552 switch (api_version) {
Trevor David Black628c41a2021-09-27 05:07:22 +0000553 case VK_API_VERSION_1_3:
554 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
555 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
556 [[clang::fallthrough]];
Ian Elliottfa7af492021-07-20 17:40:24 -0600557 case VK_API_VERSION_1_2:
558 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
559 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
560 [[clang::fallthrough]];
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700561 case VK_API_VERSION_1_1:
562 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
563 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
564 [[clang::fallthrough]];
565 case VK_API_VERSION_1_0:
566 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
567 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
568 break;
569 default:
570 ALOGE("Unknown API version[%u]", api_version);
571 break;
572 }
573
Chia-I Wu4901db72016-03-24 16:38:58 +0800574 ext_names = extension_filter_.names;
575 ext_count = extension_filter_.name_count;
576
577 return VK_SUCCESS;
578}
579
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800580VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800581 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800582 return Hal::Device().EnumerateInstanceExtensionProperties(
583 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800584 } else {
585 const auto& driver = GetData(physical_dev_).driver;
586 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
587 &count, nullptr);
588 }
589}
590
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800591VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800592 uint32_t& count,
593 VkExtensionProperties* props) const {
594 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800595 return Hal::Device().EnumerateInstanceExtensionProperties(
596 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800597 } else {
598 const auto& driver = GetData(physical_dev_).driver;
599 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
600 &count, props);
601 }
602}
603
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800604VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800605 // query extension count
606 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800607 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800608 if (result != VK_SUCCESS || count == 0)
609 return result;
610
611 auto& filter = extension_filter_;
612 filter.exts =
613 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
614 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
615 alignof(VkExtensionProperties),
616 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
617 if (!filter.exts)
618 return VK_ERROR_OUT_OF_HOST_MEMORY;
619
620 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800621 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800622 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
623 return result;
624
625 if (!count)
626 return VK_SUCCESS;
627
628 filter.ext_count = count;
629
630 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700631 if (is_instance_) {
632 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
633
634 // It requires enabling additional promoted extensions to downgrade api,
635 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700636 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700637 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700638 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700639 }
640
641 count = std::min(filter.ext_count, enabled_ext_count);
642 } else {
643 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
644 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700645
646 if (!count)
647 return VK_SUCCESS;
648
Chia-I Wu4901db72016-03-24 16:38:58 +0800649 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
650 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
651 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
652 if (!filter.names)
653 return VK_ERROR_OUT_OF_HOST_MEMORY;
654
655 return VK_SUCCESS;
656}
657
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800658void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800659 auto& filter = extension_filter_;
660
661 ProcHook::Extension ext_bit = GetProcHookExtension(name);
662 if (is_instance_) {
663 switch (ext_bit) {
664 case ProcHook::KHR_android_surface:
665 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600666 case ProcHook::KHR_surface_protected_capabilities:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700667 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300668 case ProcHook::KHR_get_surface_capabilities2:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600669 case ProcHook::GOOGLE_surfaceless_query:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000670 case ProcHook::EXT_surface_maintenance1:
Chia-I Wu4901db72016-03-24 16:38:58 +0800671 hook_extensions_.set(ext_bit);
672 // return now as these extensions do not require HAL support
673 return;
674 case ProcHook::EXT_debug_report:
675 // both we and HAL can take part in
676 hook_extensions_.set(ext_bit);
677 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300678 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700679 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700680 case ProcHook::KHR_external_memory_capabilities:
681 case ProcHook::KHR_external_semaphore_capabilities:
682 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700683 case ProcHook::EXTENSION_UNKNOWN:
684 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800685 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700686
Yiwei Zhang23143102019-04-10 18:24:05 -0700687 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700688 case ProcHook::KHR_incremental_present:
689 case ProcHook::KHR_shared_presentable_image:
690 case ProcHook::KHR_swapchain:
691 case ProcHook::EXT_hdr_metadata:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000692 case ProcHook::EXT_swapchain_maintenance1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700693 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
694 case ProcHook::ANDROID_native_buffer:
695 case ProcHook::GOOGLE_display_timing:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000696 case ProcHook::KHR_external_fence_fd:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700697 case ProcHook::EXTENSION_CORE_1_0:
698 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700699 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000700 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700701 case ProcHook::EXTENSION_COUNT:
702 // Device and meta extensions. If we ever get here it's a bug in
703 // our code. But enumerating them lets us avoid having a default
704 // case, and default hides other bugs.
705 ALOGE(
706 "CreateInfoWrapper::FilterExtension: invalid instance "
707 "extension '%s'. FIX ME",
708 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800709 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700710
711 // Don't use a default case. Without it, -Wswitch will tell us
712 // at compile time if someone adds a new ProcHook extension but
713 // doesn't handle it above. That's a real bug that has
714 // not-immediately-obvious effects.
715 //
716 // default:
717 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800718 }
719 } else {
720 switch (ext_bit) {
721 case ProcHook::KHR_swapchain:
722 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
723 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
724 ext_bit = ProcHook::ANDROID_native_buffer;
725 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700726 case ProcHook::KHR_incremental_present:
Ian Elliott334a4102022-12-22 18:51:09 +0000727 case ProcHook::KHR_shared_presentable_image:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000728 case ProcHook::GOOGLE_display_timing:
Ian Elliott8a977262017-01-19 09:05:58 -0700729 hook_extensions_.set(ext_bit);
730 // return now as these extensions do not require HAL support
731 return;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000732 case ProcHook::EXT_swapchain_maintenance1:
733 // map VK_KHR_swapchain_maintenance1 to KHR_external_fence_fd
734 name = VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME;
735 ext_bit = ProcHook::KHR_external_fence_fd;
736 break;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700737 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700738 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700739 hook_extensions_.set(ext_bit);
740 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700741 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000742 case ProcHook::KHR_external_fence_fd:
Chia-I Wu4901db72016-03-24 16:38:58 +0800743 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700744 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800745 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700746
747 case ProcHook::KHR_android_surface:
748 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700749 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700750 case ProcHook::KHR_external_memory_capabilities:
751 case ProcHook::KHR_external_semaphore_capabilities:
752 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700753 case ProcHook::KHR_get_surface_capabilities2:
754 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600755 case ProcHook::KHR_surface_protected_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700756 case ProcHook::EXT_debug_report:
757 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000758 case ProcHook::EXT_surface_maintenance1:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600759 case ProcHook::GOOGLE_surfaceless_query:
Jesse Hall7f983a82018-03-29 14:46:45 -0700760 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700761 case ProcHook::EXTENSION_CORE_1_0:
762 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700763 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000764 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700765 case ProcHook::EXTENSION_COUNT:
766 // Instance and meta extensions. If we ever get here it's a bug
767 // in our code. But enumerating them lets us avoid having a
768 // default case, and default hides other bugs.
769 ALOGE(
770 "CreateInfoWrapper::FilterExtension: invalid device "
771 "extension '%s'. FIX ME",
772 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800773 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700774
775 // Don't use a default case. Without it, -Wswitch will tell us
776 // at compile time if someone adds a new ProcHook extension but
777 // doesn't handle it above. That's a real bug that has
778 // not-immediately-obvious effects.
779 //
780 // default:
781 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800782 }
783 }
784
785 for (uint32_t i = 0; i < filter.ext_count; i++) {
786 const VkExtensionProperties& props = filter.exts[i];
787 // ignore unknown extensions
788 if (strcmp(name, props.extensionName) != 0)
789 continue;
790
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000791 if (ext_bit != ProcHook::EXTENSION_UNKNOWN &&
792 hal_extensions_.test(ext_bit)) {
793 ALOGI("CreateInfoWrapper::FilterExtension: already have '%s'.", name);
794 continue;
795 }
796
Ian Elliott3b48e152023-08-10 13:32:55 -0600797 // Ignore duplicate extensions (see: b/288929054)
798 bool duplicate_entry = false;
799 for (uint32_t j = 0; j < filter.name_count; j++) {
800 if (strcmp(name, filter.names[j]) == 0) {
801 duplicate_entry = true;
802 break;
803 }
804 }
805 if (duplicate_entry == true)
806 continue;
807
Chia-I Wu4901db72016-03-24 16:38:58 +0800808 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800809 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
810 if (ext_bit == ProcHook::ANDROID_native_buffer)
811 hook_extensions_.set(ProcHook::KHR_swapchain);
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000812 if (ext_bit == ProcHook::KHR_external_fence_fd)
813 hook_extensions_.set(ProcHook::EXT_swapchain_maintenance1);
Chia-I Wu1600e262016-04-12 09:40:06 +0800814
815 hal_extensions_.set(ext_bit);
816 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800817
818 break;
819 }
820}
821
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800822VKAPI_ATTR void* DefaultAllocate(void*,
823 size_t size,
824 size_t alignment,
825 VkSystemAllocationScope) {
826 void* ptr = nullptr;
827 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
828 // additionally requires that it be at least sizeof(void*).
829 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
830 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
831 ret, ptr);
832 return ret == 0 ? ptr : nullptr;
833}
834
835VKAPI_ATTR void* DefaultReallocate(void*,
836 void* ptr,
837 size_t size,
838 size_t alignment,
839 VkSystemAllocationScope) {
840 if (size == 0) {
841 free(ptr);
842 return nullptr;
843 }
844
Yiwei Zhanga885c062019-10-24 12:07:57 -0700845 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800846 // request is smaller than the existing chunk, we just continue using it.
847 // Right now the loader never reallocs, so this doesn't matter. If that
848 // changes, or if this code is copied into some other project, this should
849 // probably have a heuristic to allocate-copy-free when doing so will save
850 // "enough" space.
851 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
852 if (size <= old_size)
853 return ptr;
854
855 void* new_ptr = nullptr;
856 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
857 return nullptr;
858 if (ptr) {
859 memcpy(new_ptr, ptr, std::min(old_size, size));
860 free(ptr);
861 }
862 return new_ptr;
863}
864
865VKAPI_ATTR void DefaultFree(void*, void* ptr) {
866 ALOGD_CALLSTACK("Free: %p", ptr);
867 free(ptr);
868}
869
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800870InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
871 void* data_mem = allocator.pfnAllocation(
872 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
873 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
874 if (!data_mem)
875 return nullptr;
876
877 return new (data_mem) InstanceData(allocator);
878}
879
880void FreeInstanceData(InstanceData* data,
881 const VkAllocationCallbacks& allocator) {
882 data->~InstanceData();
883 allocator.pfnFree(allocator.pUserData, data);
884}
885
Chia-I Wu950d6e12016-05-03 09:12:35 +0800886DeviceData* AllocateDeviceData(
887 const VkAllocationCallbacks& allocator,
888 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800889 void* data_mem = allocator.pfnAllocation(
890 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
891 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
892 if (!data_mem)
893 return nullptr;
894
Chia-I Wu950d6e12016-05-03 09:12:35 +0800895 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800896}
897
898void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
899 data->~DeviceData();
900 allocator.pfnFree(allocator.pUserData, data);
901}
902
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800903} // anonymous namespace
904
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800905bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800906 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800907}
908
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800909const VkAllocationCallbacks& GetDefaultAllocator() {
910 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
911 .pUserData = nullptr,
912 .pfnAllocation = DefaultAllocate,
913 .pfnReallocation = DefaultReallocate,
914 .pfnFree = DefaultFree,
915 };
916
917 return kDefaultAllocCallbacks;
918}
919
Chia-I Wueb7db122016-03-24 09:11:06 +0800920PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
921 const ProcHook* hook = GetProcHook(pName);
922 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800923 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800924
925 if (!instance) {
926 if (hook->type == ProcHook::GLOBAL)
927 return hook->proc;
928
Chia-I Wu109f8982016-04-22 06:40:40 +0800929 // v0 layers expect
930 //
931 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
932 //
933 // to work.
934 if (strcmp(pName, "vkCreateDevice") == 0)
935 return hook->proc;
936
Chia-I Wueb7db122016-03-24 09:11:06 +0800937 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800938 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800939 pName);
940
Chia-I Wu109f8982016-04-22 06:40:40 +0800941 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800942 }
943
944 PFN_vkVoidFunction proc;
945
946 switch (hook->type) {
947 case ProcHook::INSTANCE:
948 proc = (GetData(instance).hook_extensions[hook->extension])
949 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800950 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800951 break;
952 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700953 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800954 ? hook->proc
955 : hook->checked_proc;
956 break;
957 default:
958 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800959 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800960 pName);
961 proc = nullptr;
962 break;
963 }
964
965 return proc;
966}
967
968PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
969 const ProcHook* hook = GetProcHook(pName);
970 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800971 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800972
973 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800974 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800975 return nullptr;
976 }
977
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800978 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
979 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800980}
981
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800982VkResult EnumerateInstanceExtensionProperties(
983 const char* pLayerName,
984 uint32_t* pPropertyCount,
985 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700986 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700987 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600988 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600989 loader_extensions.push_back(
990 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
991 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600992 loader_extensions.push_back({
993 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
994 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
995 loader_extensions.push_back({
996 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
997 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -0600998 loader_extensions.push_back(
999 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
1000 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -06001001 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
1002 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001003 loader_extensions.push_back({
1004 VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
1005 VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -06001006
Chia-I Wu31938252016-05-23 15:31:02 +08001007 static const VkExtensionProperties loader_debug_report_extension = {
1008 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
1009 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001010
1011 // enumerate our extensions first
1012 if (!pLayerName && pProperties) {
1013 uint32_t count = std::min(
1014 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1015
Yiwei Zhang5e862202019-06-21 14:59:16 -07001016 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001017
1018 if (count < loader_extensions.size()) {
1019 *pPropertyCount = count;
1020 return VK_INCOMPLETE;
1021 }
1022
1023 pProperties += count;
1024 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +08001025
1026 if (Hal::Get().GetDebugReportIndex() < 0) {
1027 if (!*pPropertyCount) {
1028 *pPropertyCount = count;
1029 return VK_INCOMPLETE;
1030 }
1031
1032 pProperties[0] = loader_debug_report_extension;
1033 pProperties += 1;
1034 *pPropertyCount -= 1;
1035 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001036 }
1037
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001038 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001039 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001040 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001041 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001042
Chia-I Wu31938252016-05-23 15:31:02 +08001043 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1044 int idx = Hal::Get().GetDebugReportIndex();
1045 if (idx < 0) {
1046 *pPropertyCount += 1;
1047 } else if (pProperties &&
1048 static_cast<uint32_t>(idx) < *pPropertyCount) {
1049 pProperties[idx].specVersion =
1050 std::min(pProperties[idx].specVersion,
1051 loader_debug_report_extension.specVersion);
1052 }
1053
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001054 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +08001055 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001056
1057 return result;
1058}
1059
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001060void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001061 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001062 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001063 ATRACE_CALL();
1064
Chris Forbesfa25e632017-02-22 12:36:02 +13001065 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001066 VkPhysicalDeviceProperties2 properties = {
1067 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001068 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001069 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001070 };
1071
1072#pragma clang diagnostic push
1073#pragma clang diagnostic ignored "-Wold-style-cast"
1074 presentation_properties->sType =
1075 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1076#pragma clang diagnostic pop
1077 presentation_properties->pNext = nullptr;
1078 presentation_properties->sharedImage = VK_FALSE;
1079
Chris Forbese056c122021-07-22 13:54:04 -07001080 const auto& driver = GetData(physicalDevice).driver;
1081
1082 if (driver.GetPhysicalDeviceProperties2) {
1083 // >= 1.1 driver, supports core GPDP2 entrypoint.
1084 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1085 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1086 // Old driver, but may support presentation properties
1087 // if we have the GPDP2 extension. Otherwise, no presentation
1088 // properties supported.
1089 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1090 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001091}
1092
Trevor David Black929e9cd2022-11-22 04:12:19 +00001093VkResult GetAndroidNativeBufferSpecVersion9Support(
1094 VkPhysicalDevice physicalDevice,
1095 bool& support) {
1096 support = false;
1097
Trevor David Black2cc44682022-03-09 00:31:38 +00001098 const InstanceData& data = GetData(physicalDevice);
1099
1100 // Call to get propertyCount
1101 uint32_t propertyCount = 0;
1102 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1103 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1104 physicalDevice, nullptr, &propertyCount, nullptr);
1105 ATRACE_END();
1106
Trevor David Black929e9cd2022-11-22 04:12:19 +00001107 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1108 return result;
1109 }
1110
Trevor David Black2cc44682022-03-09 00:31:38 +00001111 // Call to enumerate properties
1112 std::vector<VkExtensionProperties> properties(propertyCount);
1113 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1114 result = data.driver.EnumerateDeviceExtensionProperties(
1115 physicalDevice, nullptr, &propertyCount, properties.data());
1116 ATRACE_END();
1117
Trevor David Black929e9cd2022-11-22 04:12:19 +00001118 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1119 return result;
1120 }
1121
Trevor David Black2cc44682022-03-09 00:31:38 +00001122 for (uint32_t i = 0; i < propertyCount; i++) {
1123 auto& prop = properties[i];
1124
1125 if (strcmp(prop.extensionName,
1126 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1127 continue;
1128
1129 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001130 support = true;
1131 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001132 }
1133 }
1134
Trevor David Black929e9cd2022-11-22 04:12:19 +00001135 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001136}
1137
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001138bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1139 const auto& driver = GetData(physicalDevice).driver;
1140 if (!driver.GetPhysicalDeviceExternalFenceProperties)
1141 return false;
1142
1143 // Requires support for external fences imported from sync fds.
1144 // This is _almost_ universal on Android, but may be missing on
1145 // some extremely old drivers, or on strange implementations like
1146 // cuttlefish.
1147 VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1148 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1149 nullptr,
1150 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1151 };
1152 VkExternalFenceProperties fenceProperties = {
1153 VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1154 nullptr,
1155 0, 0, 0
1156 };
1157
1158 GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1159 if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1160 return true;
1161
1162 return false;
1163}
1164
Chia-I Wu01cf3052016-03-24 16:16:21 +08001165VkResult EnumerateDeviceExtensionProperties(
1166 VkPhysicalDevice physicalDevice,
1167 const char* pLayerName,
1168 uint32_t* pPropertyCount,
1169 VkExtensionProperties* pProperties) {
1170 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001171 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001172 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001173 loader_extensions.push_back({
1174 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1175 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001176
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001177 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001178 if (hdrBoardConfig) {
1179 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1180 VK_EXT_HDR_METADATA_SPEC_VERSION});
1181 }
1182
Chris Forbes16095002017-05-05 15:33:29 -07001183 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001184 QueryPresentationProperties(physicalDevice, &presentation_properties);
1185 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001186 loader_extensions.push_back({
1187 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1188 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001189 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001190
Ian Elliott5c34de22017-04-10 14:42:30 -06001191 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1192 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001193 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001194 loader_extensions.push_back({
1195 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1196 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1197 }
1198
Trevor David Black2cc44682022-03-09 00:31:38 +00001199 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1200 // support is provided by the driver
1201 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1202 swapchainCompFeats = {};
1203 swapchainCompFeats.sType =
1204 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1205 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001206 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001207 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1208 imageCompFeats.sType =
1209 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1210 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001211 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001212
1213 VkPhysicalDeviceFeatures2 feats2 = {};
1214 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1215 feats2.pNext = &imageCompFeats;
1216
Trevor David Black929e9cd2022-11-22 04:12:19 +00001217 const auto& driver = GetData(physicalDevice).driver;
1218 if (driver.GetPhysicalDeviceFeatures2 ||
1219 driver.GetPhysicalDeviceFeatures2KHR) {
1220 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1221 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001222
Trevor David Black929e9cd2022-11-22 04:12:19 +00001223 bool anb9 = false;
1224 VkResult result =
1225 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1226
1227 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1228 return result;
1229 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001230
1231 if (anb9 && imageCompFeats.imageCompressionControl) {
1232 loader_extensions.push_back(
1233 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1234 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1235 }
1236 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1237 loader_extensions.push_back(
1238 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1239 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1240 }
1241
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001242 if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1243 loader_extensions.push_back({
1244 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1245 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1246 }
1247
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001248 // enumerate our extensions first
1249 if (!pLayerName && pProperties) {
1250 uint32_t count = std::min(
1251 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1252
Yiwei Zhang5e862202019-06-21 14:59:16 -07001253 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001254
1255 if (count < loader_extensions.size()) {
1256 *pPropertyCount = count;
1257 return VK_INCOMPLETE;
1258 }
1259
1260 pProperties += count;
1261 *pPropertyCount -= count;
1262 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001263
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001264 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001265 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001266 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001267 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001268
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001269 if (pProperties) {
1270 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1271 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1272 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001273
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001274 if (strcmp(prop.extensionName,
1275 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1276 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001277
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001278 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1279 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001280
1281 if (prop.specVersion >= 8) {
1282 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1283 } else {
1284 prop.specVersion = 68;
1285 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001286 }
1287 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001288
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001289 // restore loader extension count
1290 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1291 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001292 }
1293
1294 return result;
1295}
1296
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001297VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1298 const VkAllocationCallbacks* pAllocator,
1299 VkInstance* pInstance) {
1300 const VkAllocationCallbacks& data_allocator =
1301 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1302
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001303 VkResult result = VK_SUCCESS;
1304 uint32_t icd_api_version = VK_API_VERSION_1_0;
1305 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1306 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1307 Hal::Device().GetInstanceProcAddr(nullptr,
1308 "vkEnumerateInstanceVersion"));
1309 if (pfn_enumerate_instance_version) {
1310 ATRACE_BEGIN("pfn_enumerate_instance_version");
1311 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1312 ATRACE_END();
1313 if (result != VK_SUCCESS)
1314 return result;
1315
Trevor David Blackb68a2252021-08-23 16:37:18 +00001316 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001317 }
1318
1319 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1320 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001321 if (result != VK_SUCCESS)
1322 return result;
1323
1324 InstanceData* data = AllocateInstanceData(data_allocator);
1325 if (!data)
1326 return VK_ERROR_OUT_OF_HOST_MEMORY;
1327
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001328 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001329
1330 // call into the driver
1331 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001332 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001333 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001334 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1335 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001336 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001337 if (result != VK_SUCCESS) {
1338 FreeInstanceData(data, data_allocator);
1339 return result;
1340 }
1341
1342 // initialize InstanceDriverTable
1343 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001344 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001345 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001346 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001347 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001348 if (data->driver.DestroyInstance)
1349 data->driver.DestroyInstance(instance, pAllocator);
1350
1351 FreeInstanceData(data, data_allocator);
1352
1353 return VK_ERROR_INCOMPATIBLE_DRIVER;
1354 }
1355
1356 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001357 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001358 if (!data->get_device_proc_addr) {
1359 data->driver.DestroyInstance(instance, pAllocator);
1360 FreeInstanceData(data, data_allocator);
1361
1362 return VK_ERROR_INCOMPATIBLE_DRIVER;
1363 }
1364
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001365 // TODO(b/259516419) avoid getting stats from hwui
1366 // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1367 // || (strcmp("android framework",
1368 // pCreateInfo->pApplicationInfo->pEngineName) != 0);
1369 const bool reportStats = true;
1370 if (reportStats) {
1371 // Set stats for Vulkan api version requested with application info
1372 if (pCreateInfo->pApplicationInfo) {
1373 const uint32_t vulkanApiVersion =
1374 pCreateInfo->pApplicationInfo->apiVersion;
1375 android::GraphicsEnv::getInstance().setTargetStats(
1376 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1377 vulkanApiVersion);
1378 }
1379
1380 // Update stats for the extensions requested
1381 android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1382 pCreateInfo->enabledExtensionCount,
1383 pCreateInfo->ppEnabledExtensionNames);
1384 }
1385
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001386 *pInstance = instance;
1387
1388 return VK_SUCCESS;
1389}
1390
1391void DestroyInstance(VkInstance instance,
1392 const VkAllocationCallbacks* pAllocator) {
1393 InstanceData& data = GetData(instance);
1394 data.driver.DestroyInstance(instance, pAllocator);
1395
1396 VkAllocationCallbacks local_allocator;
1397 if (!pAllocator) {
1398 local_allocator = data.allocator;
1399 pAllocator = &local_allocator;
1400 }
1401
1402 FreeInstanceData(&data, *pAllocator);
1403}
1404
Chia-I Wu4901db72016-03-24 16:38:58 +08001405VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1406 const VkDeviceCreateInfo* pCreateInfo,
1407 const VkAllocationCallbacks* pAllocator,
1408 VkDevice* pDevice) {
1409 const InstanceData& instance_data = GetData(physicalDevice);
1410 const VkAllocationCallbacks& data_allocator =
1411 (pAllocator) ? *pAllocator : instance_data.allocator;
1412
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001413 VkPhysicalDeviceProperties properties;
1414 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1415 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1416 &properties);
1417 ATRACE_END();
1418
1419 CreateInfoWrapper wrapper(
1420 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001421 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001422 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001423 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001424 if (result != VK_SUCCESS)
1425 return result;
1426
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001427 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001428 DeviceData* data = AllocateDeviceData(data_allocator,
1429 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001430 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001431 if (!data)
1432 return VK_ERROR_OUT_OF_HOST_MEMORY;
1433
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001434 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001435
1436 // call into the driver
1437 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001438 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001439 result = instance_data.driver.CreateDevice(
1440 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1441 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001442 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001443 if (result != VK_SUCCESS) {
1444 FreeDeviceData(data, data_allocator);
1445 return result;
1446 }
1447
1448 // initialize DeviceDriverTable
1449 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001450 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1451 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001452 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1453 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1454 if (data->driver.DestroyDevice)
1455 data->driver.DestroyDevice(dev, pAllocator);
1456
1457 FreeDeviceData(data, data_allocator);
1458
1459 return VK_ERROR_INCOMPATIBLE_DRIVER;
1460 }
Chris Forbesd8277912017-02-10 14:59:59 +13001461
Trevor David Black2cc44682022-03-09 00:31:38 +00001462 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001463 // entrypoints varies according to the spec version.
1464 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1465 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001466 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001467 !data->driver.GetSwapchainGrallocUsage3ANDROID &&
1468 !data->driver.GetSwapchainGrallocUsage4ANDROID) {
Trevor David Black2cc44682022-03-09 00:31:38 +00001469 ALOGE(
1470 "Driver's implementation of ANDROID_native_buffer is broken;"
1471 " must expose at least one of "
1472 "vkGetSwapchainGrallocUsageANDROID or "
1473 "vkGetSwapchainGrallocUsage2ANDROID or "
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001474 "vkGetSwapchainGrallocUsage3ANDROID or "
1475 "vkGetSwapchainGrallocUsage4ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001476
1477 data->driver.DestroyDevice(dev, pAllocator);
1478 FreeDeviceData(data, data_allocator);
1479
1480 return VK_ERROR_INCOMPATIBLE_DRIVER;
1481 }
1482
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001483 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1484 // Log that the app is hitting software Vulkan implementation
1485 android::GraphicsEnv::getInstance().setTargetStats(
1486 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1487 }
1488
Jesse Halldc225072016-05-30 22:40:14 -07001489 data->driver_device = dev;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001490 data->driver_physical_device = physicalDevice;
Chia-I Wu4901db72016-03-24 16:38:58 +08001491
1492 *pDevice = dev;
1493
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001494 // TODO(b/259516419) avoid getting stats from hwui
1495 const bool reportStats = true;
1496 if (reportStats) {
1497 android::GraphicsEnv::getInstance().setTargetStats(
1498 android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1499
1500 // Set stats for creating a Vulkan device and report features in use
1501 const VkPhysicalDeviceFeatures* pEnabledFeatures =
1502 pCreateInfo->pEnabledFeatures;
1503 if (!pEnabledFeatures) {
1504 // Use features from the chained VkPhysicalDeviceFeatures2
1505 // structure, if given
1506 const VkPhysicalDeviceFeatures2* features2 =
1507 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1508 pCreateInfo->pNext);
1509 while (features2 &&
1510 features2->sType !=
1511 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1512 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1513 features2->pNext);
1514 }
1515 if (features2) {
1516 pEnabledFeatures = &features2->features;
1517 }
1518 }
1519 const VkBool32* pFeatures =
1520 reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1521 if (pFeatures) {
1522 // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1523 // of them using pointer arithmetic here and save the features in a
1524 // 64-bit bitfield
1525 static_assert(
1526 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1527 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1528 "packing");
1529 static_assert(
1530 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1531 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1532 "packing");
1533 const int numFeatures =
1534 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1535
1536 uint64_t enableFeatureBits = 0;
1537 for (int i = 0; i < numFeatures; i++) {
1538 if (pFeatures[i] != VK_FALSE) {
1539 enableFeatureBits |= (uint64_t(1) << i);
1540 }
1541 }
1542 android::GraphicsEnv::getInstance().setTargetStats(
1543 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1544 enableFeatureBits);
1545 }
1546
1547 // Update stats for the extensions requested
1548 android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1549 pCreateInfo->enabledExtensionCount,
1550 pCreateInfo->ppEnabledExtensionNames);
1551 }
1552
Chia-I Wu4901db72016-03-24 16:38:58 +08001553 return VK_SUCCESS;
1554}
1555
1556void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1557 DeviceData& data = GetData(device);
1558 data.driver.DestroyDevice(device, pAllocator);
1559
1560 VkAllocationCallbacks local_allocator;
1561 if (!pAllocator) {
1562 local_allocator = data.allocator;
1563 pAllocator = &local_allocator;
1564 }
1565
1566 FreeDeviceData(&data, *pAllocator);
1567}
1568
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001569VkResult EnumeratePhysicalDevices(VkInstance instance,
1570 uint32_t* pPhysicalDeviceCount,
1571 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001572 ATRACE_CALL();
1573
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001574 const auto& data = GetData(instance);
1575
1576 VkResult result = data.driver.EnumeratePhysicalDevices(
1577 instance, pPhysicalDeviceCount, pPhysicalDevices);
1578 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1579 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1580 SetData(pPhysicalDevices[i], data);
1581 }
1582
1583 return result;
1584}
1585
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001586VkResult EnumeratePhysicalDeviceGroups(
1587 VkInstance instance,
1588 uint32_t* pPhysicalDeviceGroupCount,
1589 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001590 ATRACE_CALL();
1591
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001592 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001593 const auto& data = GetData(instance);
1594
Yiwei Zhange4f64172020-07-05 15:17:32 -07001595 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1596 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001597 uint32_t device_count = 0;
1598 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1599 if (result < 0)
1600 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001601
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001602 if (!pPhysicalDeviceGroupProperties) {
1603 *pPhysicalDeviceGroupCount = device_count;
1604 return result;
1605 }
1606
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001607 if (!device_count) {
1608 *pPhysicalDeviceGroupCount = 0;
1609 return result;
1610 }
Chad Versace32c087f2018-09-09 07:28:05 -07001611 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1612 if (!device_count)
1613 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001614
Yiwei Zhang5e862202019-06-21 14:59:16 -07001615 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001616 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001617 result =
1618 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001619 if (result < 0)
1620 return result;
1621
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001622 for (uint32_t i = 0; i < device_count; ++i) {
1623 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1624 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1625 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1626 }
1627 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001628 if (data.driver.EnumeratePhysicalDeviceGroups) {
1629 result = data.driver.EnumeratePhysicalDeviceGroups(
1630 instance, pPhysicalDeviceGroupCount,
1631 pPhysicalDeviceGroupProperties);
1632 } else {
1633 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1634 instance, pPhysicalDeviceGroupCount,
1635 pPhysicalDeviceGroupProperties);
1636 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001637 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1638 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1639 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1640 for (uint32_t j = 0;
1641 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1642 j++) {
1643 SetData(
1644 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001645 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001646 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001647 }
1648 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001649 }
1650
1651 return result;
1652}
1653
Chia-I Wuba0be412016-03-24 16:24:40 +08001654void GetDeviceQueue(VkDevice device,
1655 uint32_t queueFamilyIndex,
1656 uint32_t queueIndex,
1657 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001658 ATRACE_CALL();
1659
Chia-I Wuba0be412016-03-24 16:24:40 +08001660 const auto& data = GetData(device);
1661
1662 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1663 SetData(*pQueue, data);
1664}
1665
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001666void GetDeviceQueue2(VkDevice device,
1667 const VkDeviceQueueInfo2* pQueueInfo,
1668 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001669 ATRACE_CALL();
1670
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001671 const auto& data = GetData(device);
1672
1673 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001674 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001675}
1676
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001677VkResult AllocateCommandBuffers(
1678 VkDevice device,
1679 const VkCommandBufferAllocateInfo* pAllocateInfo,
1680 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001681 ATRACE_CALL();
1682
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001683 const auto& data = GetData(device);
1684
1685 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1686 pCommandBuffers);
1687 if (result == VK_SUCCESS) {
1688 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1689 SetData(pCommandBuffers[i], data);
1690 }
1691
1692 return result;
1693}
1694
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001695VkResult QueueSubmit(VkQueue queue,
1696 uint32_t submitCount,
1697 const VkSubmitInfo* pSubmits,
1698 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001699 ATRACE_CALL();
1700
1701 const auto& data = GetData(queue);
1702
1703 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1704}
1705
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001706void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1707 VkPhysicalDeviceFeatures2* pFeatures) {
1708 ATRACE_CALL();
1709
1710 const auto& driver = GetData(physicalDevice).driver;
1711
1712 if (driver.GetPhysicalDeviceFeatures2) {
1713 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001714 } else {
1715 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1716 }
1717
1718 // Conditionally add imageCompressionControlSwapchain if
1719 // imageCompressionControl is supported Check for imageCompressionControl in
1720 // the pChain
1721 bool imageCompressionControl = false;
1722 bool imageCompressionControlInChain = false;
1723 bool imageCompressionControlSwapchainInChain = false;
1724 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1725 while (pFeats) {
1726 switch (pFeats->sType) {
1727 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1728 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1729 compressionFeat = reinterpret_cast<
1730 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1731 pFeats);
1732 imageCompressionControl =
1733 compressionFeat->imageCompressionControl;
1734 imageCompressionControlInChain = true;
1735 } break;
1736
1737 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001738 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1739 compressionFeat = reinterpret_cast<
1740 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1741 pFeats);
1742 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001743 imageCompressionControlSwapchainInChain = true;
1744 } break;
1745
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001746 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1747 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1748 pFeats);
1749 smf->swapchainMaintenance1 = true;
1750 } break;
1751
Trevor David Black2cc44682022-03-09 00:31:38 +00001752 default:
1753 break;
1754 }
1755 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1756 }
1757
1758 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001759 return;
1760 }
1761
Trevor David Black2cc44682022-03-09 00:31:38 +00001762 // If not in pchain, explicitly query for imageCompressionControl
1763 if (!imageCompressionControlInChain) {
1764 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1765 imageCompFeats.sType =
1766 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1767 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001768 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001769
1770 VkPhysicalDeviceFeatures2 feats2 = {};
1771 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1772 feats2.pNext = &imageCompFeats;
1773
1774 if (driver.GetPhysicalDeviceFeatures2) {
1775 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1776 } else {
1777 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1778 }
1779
1780 imageCompressionControl = imageCompFeats.imageCompressionControl;
1781 }
1782
1783 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1784 if (imageCompressionControl) {
1785 pFeats = pFeatures;
1786 while (pFeats) {
1787 switch (pFeats->sType) {
1788 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1789 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1790 compressionFeat = reinterpret_cast<
1791 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1792 pFeats);
1793 compressionFeat->imageCompressionControlSwapchain = true;
1794 } break;
1795
1796 default:
1797 break;
1798 }
1799 pFeats =
1800 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1801 }
1802 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001803}
1804
1805void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1806 VkPhysicalDeviceProperties2* pProperties) {
1807 ATRACE_CALL();
1808
1809 const auto& driver = GetData(physicalDevice).driver;
1810
1811 if (driver.GetPhysicalDeviceProperties2) {
1812 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1813 return;
1814 }
1815
1816 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1817}
1818
1819void GetPhysicalDeviceFormatProperties2(
1820 VkPhysicalDevice physicalDevice,
1821 VkFormat format,
1822 VkFormatProperties2* pFormatProperties) {
1823 ATRACE_CALL();
1824
1825 const auto& driver = GetData(physicalDevice).driver;
1826
1827 if (driver.GetPhysicalDeviceFormatProperties2) {
1828 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1829 pFormatProperties);
1830 return;
1831 }
1832
1833 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1834 pFormatProperties);
1835}
1836
1837VkResult GetPhysicalDeviceImageFormatProperties2(
1838 VkPhysicalDevice physicalDevice,
1839 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1840 VkImageFormatProperties2* pImageFormatProperties) {
1841 ATRACE_CALL();
1842
1843 const auto& driver = GetData(physicalDevice).driver;
1844
1845 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1846 return driver.GetPhysicalDeviceImageFormatProperties2(
1847 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1848 }
1849
1850 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1851 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1852}
1853
1854void GetPhysicalDeviceQueueFamilyProperties2(
1855 VkPhysicalDevice physicalDevice,
1856 uint32_t* pQueueFamilyPropertyCount,
1857 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1858 ATRACE_CALL();
1859
1860 const auto& driver = GetData(physicalDevice).driver;
1861
1862 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1863 driver.GetPhysicalDeviceQueueFamilyProperties2(
1864 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1865 return;
1866 }
1867
1868 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1869 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1870}
1871
1872void GetPhysicalDeviceMemoryProperties2(
1873 VkPhysicalDevice physicalDevice,
1874 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1875 ATRACE_CALL();
1876
1877 const auto& driver = GetData(physicalDevice).driver;
1878
1879 if (driver.GetPhysicalDeviceMemoryProperties2) {
1880 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1881 pMemoryProperties);
1882 return;
1883 }
1884
1885 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1886 pMemoryProperties);
1887}
1888
1889void GetPhysicalDeviceSparseImageFormatProperties2(
1890 VkPhysicalDevice physicalDevice,
1891 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1892 uint32_t* pPropertyCount,
1893 VkSparseImageFormatProperties2* pProperties) {
1894 ATRACE_CALL();
1895
1896 const auto& driver = GetData(physicalDevice).driver;
1897
1898 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1899 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1900 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1901 return;
1902 }
1903
1904 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1905 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1906}
1907
Yiwei Zhange1f35012020-07-05 22:52:04 -07001908void GetPhysicalDeviceExternalBufferProperties(
1909 VkPhysicalDevice physicalDevice,
1910 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1911 VkExternalBufferProperties* pExternalBufferProperties) {
1912 ATRACE_CALL();
1913
1914 const auto& driver = GetData(physicalDevice).driver;
1915
1916 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1917 driver.GetPhysicalDeviceExternalBufferProperties(
1918 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1919 return;
1920 }
1921
1922 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1923 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1924 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1925 return;
1926 }
1927
1928 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1929 sizeof(VkExternalMemoryProperties));
1930}
1931
1932void GetPhysicalDeviceExternalSemaphoreProperties(
1933 VkPhysicalDevice physicalDevice,
1934 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1935 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1936 ATRACE_CALL();
1937
1938 const auto& driver = GetData(physicalDevice).driver;
1939
1940 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1941 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1942 physicalDevice, pExternalSemaphoreInfo,
1943 pExternalSemaphoreProperties);
1944 return;
1945 }
1946
1947 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1948 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1949 physicalDevice, pExternalSemaphoreInfo,
1950 pExternalSemaphoreProperties);
1951 return;
1952 }
1953
1954 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1955 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1956 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1957}
1958
1959void GetPhysicalDeviceExternalFenceProperties(
1960 VkPhysicalDevice physicalDevice,
1961 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1962 VkExternalFenceProperties* pExternalFenceProperties) {
1963 ATRACE_CALL();
1964
1965 const auto& driver = GetData(physicalDevice).driver;
1966
1967 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1968 driver.GetPhysicalDeviceExternalFenceProperties(
1969 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1970 return;
1971 }
1972
1973 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1974 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1975 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1976 return;
1977 }
1978
1979 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1980 pExternalFenceProperties->compatibleHandleTypes = 0;
1981 pExternalFenceProperties->externalFenceFeatures = 0;
1982}
1983
Chia-I Wu9d518162016-03-24 14:55:27 +08001984} // namespace driver
1985} // namespace vulkan