blob: 3f89960e328e8be18bd300e97b5edbfbdc4167d7 [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
342 // Close the opened device
Ian Elliotta5b72a12024-01-09 12:48:12 -0700343 int err = hal_.dev_->common.close(
344 const_cast<struct hw_device_t*>(&hal_.dev_->common));
345 ALOG_ASSERT(!err, "hw_device_t::close() failed.");
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700346
347 // Close the opened shared library in the hw_module_t
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700348 android_unload_sphal_library(hal_.dev_->common.module->dso);
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700349
350 hal_.dev_ = nullptr;
351 hal_.debug_report_index_ = -1;
352}
353
Chia-I Wu31938252016-05-23 15:31:02 +0800354bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800355 ATRACE_CALL();
356
Chia-I Wu31938252016-05-23 15:31:02 +0800357 uint32_t count;
358 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
359 VK_SUCCESS) {
360 ALOGE("failed to get HAL instance extension count");
361 return false;
362 }
363
364 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
365 malloc(sizeof(VkExtensionProperties) * count));
366 if (!exts) {
367 ALOGE("failed to allocate HAL instance extension array");
368 return false;
369 }
370
371 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
372 VK_SUCCESS) {
373 ALOGE("failed to enumerate HAL instance extensions");
374 free(exts);
375 return false;
376 }
377
378 for (uint32_t i = 0; i < count; i++) {
379 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
380 0) {
381 debug_report_index_ = static_cast<int>(i);
382 break;
383 }
384 }
385
386 free(exts);
387
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800388 return true;
389}
390
391CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700392 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800393 const VkAllocationCallbacks& allocator)
394 : is_instance_(true),
395 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000396 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700397 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800398 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800399 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700400 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800401
Chia-I Wu4901db72016-03-24 16:38:58 +0800402CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
403 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700404 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800405 const VkAllocationCallbacks& allocator)
406 : is_instance_(false),
407 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000408 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700409 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800410 physical_dev_(physical_dev),
411 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700412 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800413
414CreateInfoWrapper::~CreateInfoWrapper() {
415 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
416 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
417}
418
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800419VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700420 VkResult result = SanitizeApiVersion();
421 if (result == VK_SUCCESS)
422 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800423 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800424 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800425 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800426 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800427
428 return result;
429}
430
431const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800432CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800433 return hook_extensions_;
434}
435
436const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800437CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800438 return hal_extensions_;
439}
440
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800441CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
442 return &instance_info_;
443}
444
Chia-I Wu4901db72016-03-24 16:38:58 +0800445CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
446 return &dev_info_;
447}
448
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700449VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700450 if (!is_instance_ || !instance_info_.pApplicationInfo)
451 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700452
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700453 if (icd_api_version_ > VK_API_VERSION_1_0 ||
454 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
455 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700456
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700457 // override apiVersion to avoid error return from 1.0 icd
458 application_info_ = *instance_info_.pApplicationInfo;
459 application_info_.apiVersion = VK_API_VERSION_1_0;
460 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700461
462 return VK_SUCCESS;
463}
464
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800465VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800466 const struct StructHeader {
467 VkStructureType type;
468 const void* next;
469 } * header;
470
471 if (is_instance_) {
472 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
473
474 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
475 while (header &&
476 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
477 header = reinterpret_cast<const StructHeader*>(header->next);
478
479 instance_info_.pNext = header;
480 } else {
481 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
482
483 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
484 while (header &&
485 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
486 header = reinterpret_cast<const StructHeader*>(header->next);
487
488 dev_info_.pNext = header;
489 }
490
491 return VK_SUCCESS;
492}
493
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800494VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800495 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
496 : dev_info_.ppEnabledLayerNames;
497 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
498 : dev_info_.enabledLayerCount;
499
500 // remove all layers
501 layer_names = nullptr;
502 layer_count = 0;
503
504 return VK_SUCCESS;
505}
506
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800507VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800508 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
509 : dev_info_.ppEnabledExtensionNames;
510 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
511 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800512
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800513 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800514 if (result != VK_SUCCESS)
515 return result;
516
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700517 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700518 for (uint32_t i = 0; i < ext_count; i++) {
519 // Upon api downgrade, skip the promoted instance extensions in the
520 // first pass to avoid duplicate extensions.
521 const std::optional<uint32_t> version =
522 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700523 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700524 *version <= loader_api_version_)
525 continue;
526
527 FilterExtension(ext_names[i]);
528 }
529
530 // Enable the required extensions to support core functionalities.
531 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700532 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700533 for (const auto& promoted_extension : promoted_extensions)
534 FilterExtension(promoted_extension);
535 } else {
536 for (uint32_t i = 0; i < ext_count; i++)
537 FilterExtension(ext_names[i]);
538 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800539
Jesse Halld3d887a2018-03-05 13:34:45 -0800540 // Enable device extensions that contain physical-device commands, so that
541 // vkGetInstanceProcAddr will return those physical-device commands.
542 if (is_instance_) {
543 hook_extensions_.set(ProcHook::KHR_swapchain);
544 }
545
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700546 const uint32_t api_version =
547 is_instance_ ? loader_api_version_
548 : std::min(icd_api_version_, loader_api_version_);
549 switch (api_version) {
Trevor David Black628c41a2021-09-27 05:07:22 +0000550 case VK_API_VERSION_1_3:
551 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
552 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
553 [[clang::fallthrough]];
Ian Elliottfa7af492021-07-20 17:40:24 -0600554 case VK_API_VERSION_1_2:
555 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
556 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
557 [[clang::fallthrough]];
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700558 case VK_API_VERSION_1_1:
559 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
560 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
561 [[clang::fallthrough]];
562 case VK_API_VERSION_1_0:
563 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
564 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
565 break;
566 default:
567 ALOGE("Unknown API version[%u]", api_version);
568 break;
569 }
570
Chia-I Wu4901db72016-03-24 16:38:58 +0800571 ext_names = extension_filter_.names;
572 ext_count = extension_filter_.name_count;
573
574 return VK_SUCCESS;
575}
576
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800577VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800578 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800579 return Hal::Device().EnumerateInstanceExtensionProperties(
580 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800581 } else {
582 const auto& driver = GetData(physical_dev_).driver;
583 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
584 &count, nullptr);
585 }
586}
587
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800588VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800589 uint32_t& count,
590 VkExtensionProperties* props) const {
591 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800592 return Hal::Device().EnumerateInstanceExtensionProperties(
593 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800594 } else {
595 const auto& driver = GetData(physical_dev_).driver;
596 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
597 &count, props);
598 }
599}
600
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800601VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800602 // query extension count
603 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800604 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800605 if (result != VK_SUCCESS || count == 0)
606 return result;
607
608 auto& filter = extension_filter_;
609 filter.exts =
610 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
611 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
612 alignof(VkExtensionProperties),
613 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
614 if (!filter.exts)
615 return VK_ERROR_OUT_OF_HOST_MEMORY;
616
617 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800618 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800619 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
620 return result;
621
622 if (!count)
623 return VK_SUCCESS;
624
625 filter.ext_count = count;
626
627 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700628 if (is_instance_) {
629 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
630
631 // It requires enabling additional promoted extensions to downgrade api,
632 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700633 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700634 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700635 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700636 }
637
638 count = std::min(filter.ext_count, enabled_ext_count);
639 } else {
640 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
641 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700642
643 if (!count)
644 return VK_SUCCESS;
645
Chia-I Wu4901db72016-03-24 16:38:58 +0800646 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
647 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
648 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
649 if (!filter.names)
650 return VK_ERROR_OUT_OF_HOST_MEMORY;
651
652 return VK_SUCCESS;
653}
654
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800655void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800656 auto& filter = extension_filter_;
657
658 ProcHook::Extension ext_bit = GetProcHookExtension(name);
659 if (is_instance_) {
660 switch (ext_bit) {
661 case ProcHook::KHR_android_surface:
662 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600663 case ProcHook::KHR_surface_protected_capabilities:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700664 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300665 case ProcHook::KHR_get_surface_capabilities2:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600666 case ProcHook::GOOGLE_surfaceless_query:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000667 case ProcHook::EXT_surface_maintenance1:
Chia-I Wu4901db72016-03-24 16:38:58 +0800668 hook_extensions_.set(ext_bit);
669 // return now as these extensions do not require HAL support
670 return;
671 case ProcHook::EXT_debug_report:
672 // both we and HAL can take part in
673 hook_extensions_.set(ext_bit);
674 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300675 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700676 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700677 case ProcHook::KHR_external_memory_capabilities:
678 case ProcHook::KHR_external_semaphore_capabilities:
679 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700680 case ProcHook::EXTENSION_UNKNOWN:
681 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800682 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700683
Yiwei Zhang23143102019-04-10 18:24:05 -0700684 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700685 case ProcHook::KHR_incremental_present:
686 case ProcHook::KHR_shared_presentable_image:
687 case ProcHook::KHR_swapchain:
688 case ProcHook::EXT_hdr_metadata:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000689 case ProcHook::EXT_swapchain_maintenance1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700690 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
691 case ProcHook::ANDROID_native_buffer:
692 case ProcHook::GOOGLE_display_timing:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000693 case ProcHook::KHR_external_fence_fd:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700694 case ProcHook::EXTENSION_CORE_1_0:
695 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700696 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000697 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700698 case ProcHook::EXTENSION_COUNT:
699 // Device and meta extensions. If we ever get here it's a bug in
700 // our code. But enumerating them lets us avoid having a default
701 // case, and default hides other bugs.
702 ALOGE(
703 "CreateInfoWrapper::FilterExtension: invalid instance "
704 "extension '%s'. FIX ME",
705 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800706 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700707
708 // Don't use a default case. Without it, -Wswitch will tell us
709 // at compile time if someone adds a new ProcHook extension but
710 // doesn't handle it above. That's a real bug that has
711 // not-immediately-obvious effects.
712 //
713 // default:
714 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800715 }
716 } else {
717 switch (ext_bit) {
718 case ProcHook::KHR_swapchain:
719 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
720 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
721 ext_bit = ProcHook::ANDROID_native_buffer;
722 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700723 case ProcHook::KHR_incremental_present:
Ian Elliott334a4102022-12-22 18:51:09 +0000724 case ProcHook::KHR_shared_presentable_image:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000725 case ProcHook::GOOGLE_display_timing:
Ian Elliott8a977262017-01-19 09:05:58 -0700726 hook_extensions_.set(ext_bit);
727 // return now as these extensions do not require HAL support
728 return;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000729 case ProcHook::EXT_swapchain_maintenance1:
730 // map VK_KHR_swapchain_maintenance1 to KHR_external_fence_fd
731 name = VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME;
732 ext_bit = ProcHook::KHR_external_fence_fd;
733 break;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700734 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700735 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700736 hook_extensions_.set(ext_bit);
737 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700738 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000739 case ProcHook::KHR_external_fence_fd:
Chia-I Wu4901db72016-03-24 16:38:58 +0800740 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700741 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800742 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700743
744 case ProcHook::KHR_android_surface:
745 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700746 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700747 case ProcHook::KHR_external_memory_capabilities:
748 case ProcHook::KHR_external_semaphore_capabilities:
749 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700750 case ProcHook::KHR_get_surface_capabilities2:
751 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600752 case ProcHook::KHR_surface_protected_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700753 case ProcHook::EXT_debug_report:
754 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000755 case ProcHook::EXT_surface_maintenance1:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600756 case ProcHook::GOOGLE_surfaceless_query:
Jesse Hall7f983a82018-03-29 14:46:45 -0700757 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700758 case ProcHook::EXTENSION_CORE_1_0:
759 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700760 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000761 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700762 case ProcHook::EXTENSION_COUNT:
763 // Instance and meta extensions. If we ever get here it's a bug
764 // in our code. But enumerating them lets us avoid having a
765 // default case, and default hides other bugs.
766 ALOGE(
767 "CreateInfoWrapper::FilterExtension: invalid device "
768 "extension '%s'. FIX ME",
769 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800770 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700771
772 // Don't use a default case. Without it, -Wswitch will tell us
773 // at compile time if someone adds a new ProcHook extension but
774 // doesn't handle it above. That's a real bug that has
775 // not-immediately-obvious effects.
776 //
777 // default:
778 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800779 }
780 }
781
782 for (uint32_t i = 0; i < filter.ext_count; i++) {
783 const VkExtensionProperties& props = filter.exts[i];
784 // ignore unknown extensions
785 if (strcmp(name, props.extensionName) != 0)
786 continue;
787
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000788 if (ext_bit != ProcHook::EXTENSION_UNKNOWN &&
789 hal_extensions_.test(ext_bit)) {
790 ALOGI("CreateInfoWrapper::FilterExtension: already have '%s'.", name);
791 continue;
792 }
793
Ian Elliott3b48e152023-08-10 13:32:55 -0600794 // Ignore duplicate extensions (see: b/288929054)
795 bool duplicate_entry = false;
796 for (uint32_t j = 0; j < filter.name_count; j++) {
797 if (strcmp(name, filter.names[j]) == 0) {
798 duplicate_entry = true;
799 break;
800 }
801 }
802 if (duplicate_entry == true)
803 continue;
804
Chia-I Wu4901db72016-03-24 16:38:58 +0800805 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800806 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
807 if (ext_bit == ProcHook::ANDROID_native_buffer)
808 hook_extensions_.set(ProcHook::KHR_swapchain);
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000809 if (ext_bit == ProcHook::KHR_external_fence_fd)
810 hook_extensions_.set(ProcHook::EXT_swapchain_maintenance1);
Chia-I Wu1600e262016-04-12 09:40:06 +0800811
812 hal_extensions_.set(ext_bit);
813 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800814
815 break;
816 }
817}
818
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800819VKAPI_ATTR void* DefaultAllocate(void*,
820 size_t size,
821 size_t alignment,
822 VkSystemAllocationScope) {
823 void* ptr = nullptr;
824 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
825 // additionally requires that it be at least sizeof(void*).
826 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
827 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
828 ret, ptr);
829 return ret == 0 ? ptr : nullptr;
830}
831
832VKAPI_ATTR void* DefaultReallocate(void*,
833 void* ptr,
834 size_t size,
835 size_t alignment,
836 VkSystemAllocationScope) {
837 if (size == 0) {
838 free(ptr);
839 return nullptr;
840 }
841
Yiwei Zhanga885c062019-10-24 12:07:57 -0700842 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800843 // request is smaller than the existing chunk, we just continue using it.
844 // Right now the loader never reallocs, so this doesn't matter. If that
845 // changes, or if this code is copied into some other project, this should
846 // probably have a heuristic to allocate-copy-free when doing so will save
847 // "enough" space.
848 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
849 if (size <= old_size)
850 return ptr;
851
852 void* new_ptr = nullptr;
853 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
854 return nullptr;
855 if (ptr) {
856 memcpy(new_ptr, ptr, std::min(old_size, size));
857 free(ptr);
858 }
859 return new_ptr;
860}
861
862VKAPI_ATTR void DefaultFree(void*, void* ptr) {
863 ALOGD_CALLSTACK("Free: %p", ptr);
864 free(ptr);
865}
866
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800867InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
868 void* data_mem = allocator.pfnAllocation(
869 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
870 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
871 if (!data_mem)
872 return nullptr;
873
874 return new (data_mem) InstanceData(allocator);
875}
876
877void FreeInstanceData(InstanceData* data,
878 const VkAllocationCallbacks& allocator) {
879 data->~InstanceData();
880 allocator.pfnFree(allocator.pUserData, data);
881}
882
Chia-I Wu950d6e12016-05-03 09:12:35 +0800883DeviceData* AllocateDeviceData(
884 const VkAllocationCallbacks& allocator,
885 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800886 void* data_mem = allocator.pfnAllocation(
887 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
888 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
889 if (!data_mem)
890 return nullptr;
891
Chia-I Wu950d6e12016-05-03 09:12:35 +0800892 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800893}
894
895void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
896 data->~DeviceData();
897 allocator.pfnFree(allocator.pUserData, data);
898}
899
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800900} // anonymous namespace
901
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800902bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800903 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800904}
905
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800906const VkAllocationCallbacks& GetDefaultAllocator() {
907 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
908 .pUserData = nullptr,
909 .pfnAllocation = DefaultAllocate,
910 .pfnReallocation = DefaultReallocate,
911 .pfnFree = DefaultFree,
912 };
913
914 return kDefaultAllocCallbacks;
915}
916
Chia-I Wueb7db122016-03-24 09:11:06 +0800917PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
918 const ProcHook* hook = GetProcHook(pName);
919 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800920 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800921
922 if (!instance) {
923 if (hook->type == ProcHook::GLOBAL)
924 return hook->proc;
925
Chia-I Wu109f8982016-04-22 06:40:40 +0800926 // v0 layers expect
927 //
928 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
929 //
930 // to work.
931 if (strcmp(pName, "vkCreateDevice") == 0)
932 return hook->proc;
933
Chia-I Wueb7db122016-03-24 09:11:06 +0800934 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800935 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800936 pName);
937
Chia-I Wu109f8982016-04-22 06:40:40 +0800938 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800939 }
940
941 PFN_vkVoidFunction proc;
942
943 switch (hook->type) {
944 case ProcHook::INSTANCE:
945 proc = (GetData(instance).hook_extensions[hook->extension])
946 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800947 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800948 break;
949 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700950 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800951 ? hook->proc
952 : hook->checked_proc;
953 break;
954 default:
955 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800956 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800957 pName);
958 proc = nullptr;
959 break;
960 }
961
962 return proc;
963}
964
965PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
966 const ProcHook* hook = GetProcHook(pName);
Jörg Wagner7b48c202024-05-13 19:44:08 +0000967 PFN_vkVoidFunction drv_func = GetData(device).driver.GetDeviceProcAddr(device, pName);
968
Chia-I Wueb7db122016-03-24 09:11:06 +0800969 if (!hook)
Jörg Wagner7b48c202024-05-13 19:44:08 +0000970 return drv_func;
Chia-I Wueb7db122016-03-24 09:11:06 +0800971
972 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800973 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800974 return nullptr;
975 }
976
Jörg Wagner7b48c202024-05-13 19:44:08 +0000977 // Don't hook if we don't have a device entry function below for the core function.
978 if (!drv_func && (hook->extension >= ProcHook::EXTENSION_CORE_1_0))
979 return nullptr;
980
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800981 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
982 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800983}
984
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800985VkResult EnumerateInstanceExtensionProperties(
986 const char* pLayerName,
987 uint32_t* pPropertyCount,
988 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700989 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700990 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600991 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600992 loader_extensions.push_back(
993 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
994 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600995 loader_extensions.push_back({
996 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
997 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
998 loader_extensions.push_back({
999 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
1000 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -06001001 loader_extensions.push_back(
1002 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
1003 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -06001004 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
1005 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001006 loader_extensions.push_back({
1007 VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
1008 VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -06001009
Chia-I Wu31938252016-05-23 15:31:02 +08001010 static const VkExtensionProperties loader_debug_report_extension = {
1011 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
1012 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001013
1014 // enumerate our extensions first
1015 if (!pLayerName && pProperties) {
1016 uint32_t count = std::min(
1017 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1018
Yiwei Zhang5e862202019-06-21 14:59:16 -07001019 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001020
1021 if (count < loader_extensions.size()) {
1022 *pPropertyCount = count;
1023 return VK_INCOMPLETE;
1024 }
1025
1026 pProperties += count;
1027 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +08001028
1029 if (Hal::Get().GetDebugReportIndex() < 0) {
1030 if (!*pPropertyCount) {
1031 *pPropertyCount = count;
1032 return VK_INCOMPLETE;
1033 }
1034
1035 pProperties[0] = loader_debug_report_extension;
1036 pProperties += 1;
1037 *pPropertyCount -= 1;
1038 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001039 }
1040
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001041 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001042 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001043 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001044 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001045
Chia-I Wu31938252016-05-23 15:31:02 +08001046 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1047 int idx = Hal::Get().GetDebugReportIndex();
1048 if (idx < 0) {
1049 *pPropertyCount += 1;
1050 } else if (pProperties &&
1051 static_cast<uint32_t>(idx) < *pPropertyCount) {
1052 pProperties[idx].specVersion =
1053 std::min(pProperties[idx].specVersion,
1054 loader_debug_report_extension.specVersion);
1055 }
1056
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001057 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +08001058 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001059
1060 return result;
1061}
1062
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001063void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001064 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001065 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001066 ATRACE_CALL();
1067
Chris Forbesfa25e632017-02-22 12:36:02 +13001068 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001069 VkPhysicalDeviceProperties2 properties = {
1070 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001071 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001072 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001073 };
1074
1075#pragma clang diagnostic push
1076#pragma clang diagnostic ignored "-Wold-style-cast"
1077 presentation_properties->sType =
1078 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1079#pragma clang diagnostic pop
1080 presentation_properties->pNext = nullptr;
1081 presentation_properties->sharedImage = VK_FALSE;
1082
Chris Forbese056c122021-07-22 13:54:04 -07001083 const auto& driver = GetData(physicalDevice).driver;
1084
1085 if (driver.GetPhysicalDeviceProperties2) {
1086 // >= 1.1 driver, supports core GPDP2 entrypoint.
1087 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1088 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1089 // Old driver, but may support presentation properties
1090 // if we have the GPDP2 extension. Otherwise, no presentation
1091 // properties supported.
1092 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1093 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001094}
1095
Trevor David Black929e9cd2022-11-22 04:12:19 +00001096VkResult GetAndroidNativeBufferSpecVersion9Support(
1097 VkPhysicalDevice physicalDevice,
1098 bool& support) {
1099 support = false;
1100
Trevor David Black2cc44682022-03-09 00:31:38 +00001101 const InstanceData& data = GetData(physicalDevice);
1102
1103 // Call to get propertyCount
1104 uint32_t propertyCount = 0;
1105 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1106 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1107 physicalDevice, nullptr, &propertyCount, nullptr);
1108 ATRACE_END();
1109
Trevor David Black929e9cd2022-11-22 04:12:19 +00001110 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1111 return result;
1112 }
1113
Trevor David Black2cc44682022-03-09 00:31:38 +00001114 // Call to enumerate properties
1115 std::vector<VkExtensionProperties> properties(propertyCount);
1116 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1117 result = data.driver.EnumerateDeviceExtensionProperties(
1118 physicalDevice, nullptr, &propertyCount, properties.data());
1119 ATRACE_END();
1120
Trevor David Black929e9cd2022-11-22 04:12:19 +00001121 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1122 return result;
1123 }
1124
Trevor David Black2cc44682022-03-09 00:31:38 +00001125 for (uint32_t i = 0; i < propertyCount; i++) {
1126 auto& prop = properties[i];
1127
1128 if (strcmp(prop.extensionName,
1129 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1130 continue;
1131
1132 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001133 support = true;
1134 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001135 }
1136 }
1137
Trevor David Black929e9cd2022-11-22 04:12:19 +00001138 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001139}
1140
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001141bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1142 const auto& driver = GetData(physicalDevice).driver;
1143 if (!driver.GetPhysicalDeviceExternalFenceProperties)
1144 return false;
1145
1146 // Requires support for external fences imported from sync fds.
1147 // This is _almost_ universal on Android, but may be missing on
1148 // some extremely old drivers, or on strange implementations like
1149 // cuttlefish.
1150 VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1151 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1152 nullptr,
1153 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1154 };
1155 VkExternalFenceProperties fenceProperties = {
1156 VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1157 nullptr,
1158 0, 0, 0
1159 };
1160
1161 GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1162 if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1163 return true;
1164
1165 return false;
1166}
1167
Chia-I Wu01cf3052016-03-24 16:16:21 +08001168VkResult EnumerateDeviceExtensionProperties(
1169 VkPhysicalDevice physicalDevice,
1170 const char* pLayerName,
1171 uint32_t* pPropertyCount,
1172 VkExtensionProperties* pProperties) {
1173 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001174 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001175 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001176 loader_extensions.push_back({
1177 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1178 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001179
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001180 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001181 if (hdrBoardConfig) {
1182 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1183 VK_EXT_HDR_METADATA_SPEC_VERSION});
1184 }
1185
Chris Forbes16095002017-05-05 15:33:29 -07001186 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001187 QueryPresentationProperties(physicalDevice, &presentation_properties);
1188 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001189 loader_extensions.push_back({
1190 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1191 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001192 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001193
Ian Elliott5c34de22017-04-10 14:42:30 -06001194 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1195 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001196 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001197 loader_extensions.push_back({
1198 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1199 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1200 }
1201
Trevor David Black2cc44682022-03-09 00:31:38 +00001202 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1203 // support is provided by the driver
1204 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1205 swapchainCompFeats = {};
1206 swapchainCompFeats.sType =
1207 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1208 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001209 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001210 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1211 imageCompFeats.sType =
1212 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1213 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001214 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001215
1216 VkPhysicalDeviceFeatures2 feats2 = {};
1217 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1218 feats2.pNext = &imageCompFeats;
1219
Trevor David Black929e9cd2022-11-22 04:12:19 +00001220 const auto& driver = GetData(physicalDevice).driver;
1221 if (driver.GetPhysicalDeviceFeatures2 ||
1222 driver.GetPhysicalDeviceFeatures2KHR) {
1223 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1224 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001225
Trevor David Black929e9cd2022-11-22 04:12:19 +00001226 bool anb9 = false;
1227 VkResult result =
1228 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1229
1230 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1231 return result;
1232 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001233
1234 if (anb9 && imageCompFeats.imageCompressionControl) {
1235 loader_extensions.push_back(
1236 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1237 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1238 }
1239 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1240 loader_extensions.push_back(
1241 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1242 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1243 }
1244
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001245 if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1246 loader_extensions.push_back({
1247 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1248 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1249 }
1250
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001251 // enumerate our extensions first
1252 if (!pLayerName && pProperties) {
1253 uint32_t count = std::min(
1254 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1255
Yiwei Zhang5e862202019-06-21 14:59:16 -07001256 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001257
1258 if (count < loader_extensions.size()) {
1259 *pPropertyCount = count;
1260 return VK_INCOMPLETE;
1261 }
1262
1263 pProperties += count;
1264 *pPropertyCount -= count;
1265 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001266
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001267 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001268 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001269 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001270 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001271
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001272 if (pProperties) {
1273 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1274 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1275 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001276
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001277 if (strcmp(prop.extensionName,
1278 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1279 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001280
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001281 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1282 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001283
1284 if (prop.specVersion >= 8) {
1285 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1286 } else {
1287 prop.specVersion = 68;
1288 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001289 }
1290 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001291
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001292 // restore loader extension count
1293 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1294 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001295 }
1296
1297 return result;
1298}
1299
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001300VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1301 const VkAllocationCallbacks* pAllocator,
1302 VkInstance* pInstance) {
1303 const VkAllocationCallbacks& data_allocator =
1304 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1305
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001306 VkResult result = VK_SUCCESS;
1307 uint32_t icd_api_version = VK_API_VERSION_1_0;
1308 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1309 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1310 Hal::Device().GetInstanceProcAddr(nullptr,
1311 "vkEnumerateInstanceVersion"));
1312 if (pfn_enumerate_instance_version) {
1313 ATRACE_BEGIN("pfn_enumerate_instance_version");
1314 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1315 ATRACE_END();
1316 if (result != VK_SUCCESS)
1317 return result;
1318
Trevor David Blackb68a2252021-08-23 16:37:18 +00001319 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001320 }
1321
1322 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1323 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001324 if (result != VK_SUCCESS)
1325 return result;
1326
1327 InstanceData* data = AllocateInstanceData(data_allocator);
1328 if (!data)
1329 return VK_ERROR_OUT_OF_HOST_MEMORY;
1330
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001331 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001332
1333 // call into the driver
1334 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001335 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001336 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001337 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1338 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001339 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001340 if (result != VK_SUCCESS) {
1341 FreeInstanceData(data, data_allocator);
1342 return result;
1343 }
1344
1345 // initialize InstanceDriverTable
1346 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001347 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001348 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001349 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001350 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001351 if (data->driver.DestroyInstance)
1352 data->driver.DestroyInstance(instance, pAllocator);
1353
1354 FreeInstanceData(data, data_allocator);
1355
1356 return VK_ERROR_INCOMPATIBLE_DRIVER;
1357 }
1358
1359 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001360 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001361 if (!data->get_device_proc_addr) {
1362 data->driver.DestroyInstance(instance, pAllocator);
1363 FreeInstanceData(data, data_allocator);
1364
1365 return VK_ERROR_INCOMPATIBLE_DRIVER;
1366 }
1367
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001368 // TODO(b/259516419) avoid getting stats from hwui
1369 // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1370 // || (strcmp("android framework",
1371 // pCreateInfo->pApplicationInfo->pEngineName) != 0);
1372 const bool reportStats = true;
1373 if (reportStats) {
1374 // Set stats for Vulkan api version requested with application info
1375 if (pCreateInfo->pApplicationInfo) {
1376 const uint32_t vulkanApiVersion =
1377 pCreateInfo->pApplicationInfo->apiVersion;
1378 android::GraphicsEnv::getInstance().setTargetStats(
1379 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1380 vulkanApiVersion);
Tom Murphyc23fcd02024-03-13 10:22:06 +00001381
1382 if (pCreateInfo->pApplicationInfo->pEngineName) {
1383 android::GraphicsEnv::getInstance().addVulkanEngineName(
1384 pCreateInfo->pApplicationInfo->pEngineName);
1385 }
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001386 }
1387
1388 // Update stats for the extensions requested
1389 android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1390 pCreateInfo->enabledExtensionCount,
1391 pCreateInfo->ppEnabledExtensionNames);
1392 }
1393
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001394 *pInstance = instance;
1395
1396 return VK_SUCCESS;
1397}
1398
1399void DestroyInstance(VkInstance instance,
1400 const VkAllocationCallbacks* pAllocator) {
1401 InstanceData& data = GetData(instance);
1402 data.driver.DestroyInstance(instance, pAllocator);
1403
1404 VkAllocationCallbacks local_allocator;
1405 if (!pAllocator) {
1406 local_allocator = data.allocator;
1407 pAllocator = &local_allocator;
1408 }
1409
1410 FreeInstanceData(&data, *pAllocator);
1411}
1412
Chia-I Wu4901db72016-03-24 16:38:58 +08001413VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1414 const VkDeviceCreateInfo* pCreateInfo,
1415 const VkAllocationCallbacks* pAllocator,
1416 VkDevice* pDevice) {
1417 const InstanceData& instance_data = GetData(physicalDevice);
1418 const VkAllocationCallbacks& data_allocator =
1419 (pAllocator) ? *pAllocator : instance_data.allocator;
1420
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001421 VkPhysicalDeviceProperties properties;
1422 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1423 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1424 &properties);
1425 ATRACE_END();
1426
1427 CreateInfoWrapper wrapper(
1428 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001429 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001430 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001431 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001432 if (result != VK_SUCCESS)
1433 return result;
1434
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001435 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001436 DeviceData* data = AllocateDeviceData(data_allocator,
1437 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001438 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001439 if (!data)
1440 return VK_ERROR_OUT_OF_HOST_MEMORY;
1441
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001442 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001443
1444 // call into the driver
1445 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001446 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001447 result = instance_data.driver.CreateDevice(
1448 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1449 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001450 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001451 if (result != VK_SUCCESS) {
1452 FreeDeviceData(data, data_allocator);
1453 return result;
1454 }
1455
1456 // initialize DeviceDriverTable
1457 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001458 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1459 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001460 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1461 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1462 if (data->driver.DestroyDevice)
1463 data->driver.DestroyDevice(dev, pAllocator);
1464
1465 FreeDeviceData(data, data_allocator);
1466
1467 return VK_ERROR_INCOMPATIBLE_DRIVER;
1468 }
Chris Forbesd8277912017-02-10 14:59:59 +13001469
Trevor David Black2cc44682022-03-09 00:31:38 +00001470 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001471 // entrypoints varies according to the spec version.
1472 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1473 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001474 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001475 !data->driver.GetSwapchainGrallocUsage3ANDROID &&
1476 !data->driver.GetSwapchainGrallocUsage4ANDROID) {
Trevor David Black2cc44682022-03-09 00:31:38 +00001477 ALOGE(
1478 "Driver's implementation of ANDROID_native_buffer is broken;"
1479 " must expose at least one of "
1480 "vkGetSwapchainGrallocUsageANDROID or "
1481 "vkGetSwapchainGrallocUsage2ANDROID or "
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001482 "vkGetSwapchainGrallocUsage3ANDROID or "
1483 "vkGetSwapchainGrallocUsage4ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001484
1485 data->driver.DestroyDevice(dev, pAllocator);
1486 FreeDeviceData(data, data_allocator);
1487
1488 return VK_ERROR_INCOMPATIBLE_DRIVER;
1489 }
1490
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001491 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1492 // Log that the app is hitting software Vulkan implementation
1493 android::GraphicsEnv::getInstance().setTargetStats(
1494 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1495 }
1496
Jesse Halldc225072016-05-30 22:40:14 -07001497 data->driver_device = dev;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001498 data->driver_physical_device = physicalDevice;
Chia-I Wu4901db72016-03-24 16:38:58 +08001499
1500 *pDevice = dev;
1501
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001502 // TODO(b/259516419) avoid getting stats from hwui
1503 const bool reportStats = true;
1504 if (reportStats) {
1505 android::GraphicsEnv::getInstance().setTargetStats(
1506 android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1507
1508 // Set stats for creating a Vulkan device and report features in use
1509 const VkPhysicalDeviceFeatures* pEnabledFeatures =
1510 pCreateInfo->pEnabledFeatures;
1511 if (!pEnabledFeatures) {
1512 // Use features from the chained VkPhysicalDeviceFeatures2
1513 // structure, if given
1514 const VkPhysicalDeviceFeatures2* features2 =
1515 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1516 pCreateInfo->pNext);
1517 while (features2 &&
1518 features2->sType !=
1519 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1520 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1521 features2->pNext);
1522 }
1523 if (features2) {
1524 pEnabledFeatures = &features2->features;
1525 }
1526 }
1527 const VkBool32* pFeatures =
1528 reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1529 if (pFeatures) {
1530 // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1531 // of them using pointer arithmetic here and save the features in a
1532 // 64-bit bitfield
1533 static_assert(
1534 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1535 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1536 "packing");
1537 static_assert(
1538 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1539 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1540 "packing");
1541 const int numFeatures =
1542 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1543
1544 uint64_t enableFeatureBits = 0;
1545 for (int i = 0; i < numFeatures; i++) {
1546 if (pFeatures[i] != VK_FALSE) {
1547 enableFeatureBits |= (uint64_t(1) << i);
1548 }
1549 }
1550 android::GraphicsEnv::getInstance().setTargetStats(
1551 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1552 enableFeatureBits);
1553 }
1554
1555 // Update stats for the extensions requested
1556 android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1557 pCreateInfo->enabledExtensionCount,
1558 pCreateInfo->ppEnabledExtensionNames);
1559 }
1560
Chia-I Wu4901db72016-03-24 16:38:58 +08001561 return VK_SUCCESS;
1562}
1563
1564void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1565 DeviceData& data = GetData(device);
1566 data.driver.DestroyDevice(device, pAllocator);
1567
1568 VkAllocationCallbacks local_allocator;
1569 if (!pAllocator) {
1570 local_allocator = data.allocator;
1571 pAllocator = &local_allocator;
1572 }
1573
1574 FreeDeviceData(&data, *pAllocator);
1575}
1576
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001577VkResult EnumeratePhysicalDevices(VkInstance instance,
1578 uint32_t* pPhysicalDeviceCount,
1579 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001580 ATRACE_CALL();
1581
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001582 const auto& data = GetData(instance);
1583
1584 VkResult result = data.driver.EnumeratePhysicalDevices(
1585 instance, pPhysicalDeviceCount, pPhysicalDevices);
1586 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1587 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1588 SetData(pPhysicalDevices[i], data);
1589 }
1590
1591 return result;
1592}
1593
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001594VkResult EnumeratePhysicalDeviceGroups(
1595 VkInstance instance,
1596 uint32_t* pPhysicalDeviceGroupCount,
1597 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001598 ATRACE_CALL();
1599
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001600 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001601 const auto& data = GetData(instance);
1602
Yiwei Zhange4f64172020-07-05 15:17:32 -07001603 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1604 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001605 uint32_t device_count = 0;
1606 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1607 if (result < 0)
1608 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001609
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001610 if (!pPhysicalDeviceGroupProperties) {
1611 *pPhysicalDeviceGroupCount = device_count;
1612 return result;
1613 }
1614
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001615 if (!device_count) {
1616 *pPhysicalDeviceGroupCount = 0;
1617 return result;
1618 }
Chad Versace32c087f2018-09-09 07:28:05 -07001619 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1620 if (!device_count)
1621 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001622
Yiwei Zhang5e862202019-06-21 14:59:16 -07001623 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001624 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001625 result =
1626 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001627 if (result < 0)
1628 return result;
1629
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001630 for (uint32_t i = 0; i < device_count; ++i) {
1631 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1632 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1633 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1634 }
1635 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001636 if (data.driver.EnumeratePhysicalDeviceGroups) {
1637 result = data.driver.EnumeratePhysicalDeviceGroups(
1638 instance, pPhysicalDeviceGroupCount,
1639 pPhysicalDeviceGroupProperties);
1640 } else {
1641 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1642 instance, pPhysicalDeviceGroupCount,
1643 pPhysicalDeviceGroupProperties);
1644 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001645 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1646 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1647 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1648 for (uint32_t j = 0;
1649 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1650 j++) {
1651 SetData(
1652 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001653 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001654 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001655 }
1656 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001657 }
1658
1659 return result;
1660}
1661
Chia-I Wuba0be412016-03-24 16:24:40 +08001662void GetDeviceQueue(VkDevice device,
1663 uint32_t queueFamilyIndex,
1664 uint32_t queueIndex,
1665 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001666 ATRACE_CALL();
1667
Chia-I Wuba0be412016-03-24 16:24:40 +08001668 const auto& data = GetData(device);
1669
1670 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1671 SetData(*pQueue, data);
1672}
1673
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001674void GetDeviceQueue2(VkDevice device,
1675 const VkDeviceQueueInfo2* pQueueInfo,
1676 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001677 ATRACE_CALL();
1678
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001679 const auto& data = GetData(device);
1680
1681 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001682 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001683}
1684
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001685VkResult AllocateCommandBuffers(
1686 VkDevice device,
1687 const VkCommandBufferAllocateInfo* pAllocateInfo,
1688 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001689 ATRACE_CALL();
1690
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001691 const auto& data = GetData(device);
1692
1693 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1694 pCommandBuffers);
1695 if (result == VK_SUCCESS) {
1696 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1697 SetData(pCommandBuffers[i], data);
1698 }
1699
1700 return result;
1701}
1702
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001703VkResult QueueSubmit(VkQueue queue,
1704 uint32_t submitCount,
1705 const VkSubmitInfo* pSubmits,
1706 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001707 ATRACE_CALL();
1708
1709 const auto& data = GetData(queue);
1710
1711 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1712}
1713
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001714void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1715 VkPhysicalDeviceFeatures2* pFeatures) {
1716 ATRACE_CALL();
1717
1718 const auto& driver = GetData(physicalDevice).driver;
1719
1720 if (driver.GetPhysicalDeviceFeatures2) {
1721 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001722 } else {
1723 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1724 }
1725
1726 // Conditionally add imageCompressionControlSwapchain if
1727 // imageCompressionControl is supported Check for imageCompressionControl in
1728 // the pChain
1729 bool imageCompressionControl = false;
1730 bool imageCompressionControlInChain = false;
1731 bool imageCompressionControlSwapchainInChain = false;
1732 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1733 while (pFeats) {
1734 switch (pFeats->sType) {
1735 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1736 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1737 compressionFeat = reinterpret_cast<
1738 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1739 pFeats);
1740 imageCompressionControl =
1741 compressionFeat->imageCompressionControl;
1742 imageCompressionControlInChain = true;
1743 } break;
1744
1745 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001746 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1747 compressionFeat = reinterpret_cast<
1748 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1749 pFeats);
1750 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001751 imageCompressionControlSwapchainInChain = true;
1752 } break;
1753
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001754 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1755 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1756 pFeats);
1757 smf->swapchainMaintenance1 = true;
1758 } break;
1759
Trevor David Black2cc44682022-03-09 00:31:38 +00001760 default:
1761 break;
1762 }
1763 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1764 }
1765
1766 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001767 return;
1768 }
1769
Trevor David Black2cc44682022-03-09 00:31:38 +00001770 // If not in pchain, explicitly query for imageCompressionControl
1771 if (!imageCompressionControlInChain) {
1772 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1773 imageCompFeats.sType =
1774 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1775 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001776 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001777
1778 VkPhysicalDeviceFeatures2 feats2 = {};
1779 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1780 feats2.pNext = &imageCompFeats;
1781
1782 if (driver.GetPhysicalDeviceFeatures2) {
1783 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1784 } else {
1785 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1786 }
1787
1788 imageCompressionControl = imageCompFeats.imageCompressionControl;
1789 }
1790
1791 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1792 if (imageCompressionControl) {
1793 pFeats = pFeatures;
1794 while (pFeats) {
1795 switch (pFeats->sType) {
1796 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1797 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1798 compressionFeat = reinterpret_cast<
1799 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1800 pFeats);
1801 compressionFeat->imageCompressionControlSwapchain = true;
1802 } break;
1803
1804 default:
1805 break;
1806 }
1807 pFeats =
1808 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1809 }
1810 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001811}
1812
1813void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1814 VkPhysicalDeviceProperties2* pProperties) {
1815 ATRACE_CALL();
1816
1817 const auto& driver = GetData(physicalDevice).driver;
1818
1819 if (driver.GetPhysicalDeviceProperties2) {
1820 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1821 return;
1822 }
1823
1824 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1825}
1826
1827void GetPhysicalDeviceFormatProperties2(
1828 VkPhysicalDevice physicalDevice,
1829 VkFormat format,
1830 VkFormatProperties2* pFormatProperties) {
1831 ATRACE_CALL();
1832
1833 const auto& driver = GetData(physicalDevice).driver;
1834
1835 if (driver.GetPhysicalDeviceFormatProperties2) {
1836 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1837 pFormatProperties);
1838 return;
1839 }
1840
1841 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1842 pFormatProperties);
1843}
1844
1845VkResult GetPhysicalDeviceImageFormatProperties2(
1846 VkPhysicalDevice physicalDevice,
1847 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1848 VkImageFormatProperties2* pImageFormatProperties) {
1849 ATRACE_CALL();
1850
1851 const auto& driver = GetData(physicalDevice).driver;
1852
1853 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1854 return driver.GetPhysicalDeviceImageFormatProperties2(
1855 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1856 }
1857
1858 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1859 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1860}
1861
1862void GetPhysicalDeviceQueueFamilyProperties2(
1863 VkPhysicalDevice physicalDevice,
1864 uint32_t* pQueueFamilyPropertyCount,
1865 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1866 ATRACE_CALL();
1867
1868 const auto& driver = GetData(physicalDevice).driver;
1869
1870 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1871 driver.GetPhysicalDeviceQueueFamilyProperties2(
1872 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1873 return;
1874 }
1875
1876 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1877 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1878}
1879
1880void GetPhysicalDeviceMemoryProperties2(
1881 VkPhysicalDevice physicalDevice,
1882 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1883 ATRACE_CALL();
1884
1885 const auto& driver = GetData(physicalDevice).driver;
1886
1887 if (driver.GetPhysicalDeviceMemoryProperties2) {
1888 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1889 pMemoryProperties);
1890 return;
1891 }
1892
1893 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1894 pMemoryProperties);
1895}
1896
1897void GetPhysicalDeviceSparseImageFormatProperties2(
1898 VkPhysicalDevice physicalDevice,
1899 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1900 uint32_t* pPropertyCount,
1901 VkSparseImageFormatProperties2* pProperties) {
1902 ATRACE_CALL();
1903
1904 const auto& driver = GetData(physicalDevice).driver;
1905
1906 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1907 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1908 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1909 return;
1910 }
1911
1912 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1913 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1914}
1915
Yiwei Zhange1f35012020-07-05 22:52:04 -07001916void GetPhysicalDeviceExternalBufferProperties(
1917 VkPhysicalDevice physicalDevice,
1918 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1919 VkExternalBufferProperties* pExternalBufferProperties) {
1920 ATRACE_CALL();
1921
1922 const auto& driver = GetData(physicalDevice).driver;
1923
1924 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1925 driver.GetPhysicalDeviceExternalBufferProperties(
1926 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1927 return;
1928 }
1929
1930 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1931 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1932 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1933 return;
1934 }
1935
1936 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1937 sizeof(VkExternalMemoryProperties));
1938}
1939
1940void GetPhysicalDeviceExternalSemaphoreProperties(
1941 VkPhysicalDevice physicalDevice,
1942 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1943 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1944 ATRACE_CALL();
1945
1946 const auto& driver = GetData(physicalDevice).driver;
1947
1948 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1949 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1950 physicalDevice, pExternalSemaphoreInfo,
1951 pExternalSemaphoreProperties);
1952 return;
1953 }
1954
1955 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1956 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1957 physicalDevice, pExternalSemaphoreInfo,
1958 pExternalSemaphoreProperties);
1959 return;
1960 }
1961
1962 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1963 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1964 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1965}
1966
1967void GetPhysicalDeviceExternalFenceProperties(
1968 VkPhysicalDevice physicalDevice,
1969 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1970 VkExternalFenceProperties* pExternalFenceProperties) {
1971 ATRACE_CALL();
1972
1973 const auto& driver = GetData(physicalDevice).driver;
1974
1975 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1976 driver.GetPhysicalDeviceExternalFenceProperties(
1977 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1978 return;
1979 }
1980
1981 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1982 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1983 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1984 return;
1985 }
1986
1987 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1988 pExternalFenceProperties->compatibleHandleTypes = 0;
1989 pExternalFenceProperties->externalFenceFeatures = 0;
1990}
1991
Chia-I Wu9d518162016-03-24 14:55:27 +08001992} // namespace driver
1993} // namespace vulkan