blob: ef213f0c7a0a7e15ccdfefd0e27083956823fe6d [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);
Jörg Wagner7b48c202024-05-13 19:44:08 +0000970 PFN_vkVoidFunction drv_func = GetData(device).driver.GetDeviceProcAddr(device, pName);
971
Chia-I Wueb7db122016-03-24 09:11:06 +0800972 if (!hook)
Jörg Wagner7b48c202024-05-13 19:44:08 +0000973 return drv_func;
Chia-I Wueb7db122016-03-24 09:11:06 +0800974
975 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800976 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800977 return nullptr;
978 }
979
Jörg Wagner7b48c202024-05-13 19:44:08 +0000980 // Don't hook if we don't have a device entry function below for the core function.
981 if (!drv_func && (hook->extension >= ProcHook::EXTENSION_CORE_1_0))
982 return nullptr;
983
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800984 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
985 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800986}
987
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800988VkResult EnumerateInstanceExtensionProperties(
989 const char* pLayerName,
990 uint32_t* pPropertyCount,
991 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700992 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700993 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600994 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600995 loader_extensions.push_back(
996 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
997 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600998 loader_extensions.push_back({
999 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
1000 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
1001 loader_extensions.push_back({
1002 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
1003 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -06001004 loader_extensions.push_back(
1005 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
1006 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -06001007 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
1008 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001009 loader_extensions.push_back({
1010 VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
1011 VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -06001012
Chia-I Wu31938252016-05-23 15:31:02 +08001013 static const VkExtensionProperties loader_debug_report_extension = {
1014 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
1015 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001016
1017 // enumerate our extensions first
1018 if (!pLayerName && pProperties) {
1019 uint32_t count = std::min(
1020 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1021
Yiwei Zhang5e862202019-06-21 14:59:16 -07001022 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001023
1024 if (count < loader_extensions.size()) {
1025 *pPropertyCount = count;
1026 return VK_INCOMPLETE;
1027 }
1028
1029 pProperties += count;
1030 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +08001031
1032 if (Hal::Get().GetDebugReportIndex() < 0) {
1033 if (!*pPropertyCount) {
1034 *pPropertyCount = count;
1035 return VK_INCOMPLETE;
1036 }
1037
1038 pProperties[0] = loader_debug_report_extension;
1039 pProperties += 1;
1040 *pPropertyCount -= 1;
1041 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001042 }
1043
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001044 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001045 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001046 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001047 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001048
Chia-I Wu31938252016-05-23 15:31:02 +08001049 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1050 int idx = Hal::Get().GetDebugReportIndex();
1051 if (idx < 0) {
1052 *pPropertyCount += 1;
1053 } else if (pProperties &&
1054 static_cast<uint32_t>(idx) < *pPropertyCount) {
1055 pProperties[idx].specVersion =
1056 std::min(pProperties[idx].specVersion,
1057 loader_debug_report_extension.specVersion);
1058 }
1059
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001060 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +08001061 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001062
1063 return result;
1064}
1065
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001066void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001067 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001068 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001069 ATRACE_CALL();
1070
Chris Forbesfa25e632017-02-22 12:36:02 +13001071 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001072 VkPhysicalDeviceProperties2 properties = {
1073 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001074 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001075 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001076 };
1077
1078#pragma clang diagnostic push
1079#pragma clang diagnostic ignored "-Wold-style-cast"
1080 presentation_properties->sType =
1081 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1082#pragma clang diagnostic pop
1083 presentation_properties->pNext = nullptr;
1084 presentation_properties->sharedImage = VK_FALSE;
1085
Chris Forbese056c122021-07-22 13:54:04 -07001086 const auto& driver = GetData(physicalDevice).driver;
1087
1088 if (driver.GetPhysicalDeviceProperties2) {
1089 // >= 1.1 driver, supports core GPDP2 entrypoint.
1090 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1091 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1092 // Old driver, but may support presentation properties
1093 // if we have the GPDP2 extension. Otherwise, no presentation
1094 // properties supported.
1095 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1096 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001097}
1098
Trevor David Black929e9cd2022-11-22 04:12:19 +00001099VkResult GetAndroidNativeBufferSpecVersion9Support(
1100 VkPhysicalDevice physicalDevice,
1101 bool& support) {
1102 support = false;
1103
Trevor David Black2cc44682022-03-09 00:31:38 +00001104 const InstanceData& data = GetData(physicalDevice);
1105
1106 // Call to get propertyCount
1107 uint32_t propertyCount = 0;
1108 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1109 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1110 physicalDevice, nullptr, &propertyCount, nullptr);
1111 ATRACE_END();
1112
Trevor David Black929e9cd2022-11-22 04:12:19 +00001113 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1114 return result;
1115 }
1116
Trevor David Black2cc44682022-03-09 00:31:38 +00001117 // Call to enumerate properties
1118 std::vector<VkExtensionProperties> properties(propertyCount);
1119 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1120 result = data.driver.EnumerateDeviceExtensionProperties(
1121 physicalDevice, nullptr, &propertyCount, properties.data());
1122 ATRACE_END();
1123
Trevor David Black929e9cd2022-11-22 04:12:19 +00001124 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1125 return result;
1126 }
1127
Trevor David Black2cc44682022-03-09 00:31:38 +00001128 for (uint32_t i = 0; i < propertyCount; i++) {
1129 auto& prop = properties[i];
1130
1131 if (strcmp(prop.extensionName,
1132 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1133 continue;
1134
1135 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001136 support = true;
1137 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001138 }
1139 }
1140
Trevor David Black929e9cd2022-11-22 04:12:19 +00001141 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001142}
1143
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001144bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1145 const auto& driver = GetData(physicalDevice).driver;
1146 if (!driver.GetPhysicalDeviceExternalFenceProperties)
1147 return false;
1148
1149 // Requires support for external fences imported from sync fds.
1150 // This is _almost_ universal on Android, but may be missing on
1151 // some extremely old drivers, or on strange implementations like
1152 // cuttlefish.
1153 VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1154 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1155 nullptr,
1156 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1157 };
1158 VkExternalFenceProperties fenceProperties = {
1159 VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1160 nullptr,
1161 0, 0, 0
1162 };
1163
1164 GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1165 if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1166 return true;
1167
1168 return false;
1169}
1170
Chia-I Wu01cf3052016-03-24 16:16:21 +08001171VkResult EnumerateDeviceExtensionProperties(
1172 VkPhysicalDevice physicalDevice,
1173 const char* pLayerName,
1174 uint32_t* pPropertyCount,
1175 VkExtensionProperties* pProperties) {
1176 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001177 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001178 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001179 loader_extensions.push_back({
1180 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1181 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001182
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001183 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001184 if (hdrBoardConfig) {
1185 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1186 VK_EXT_HDR_METADATA_SPEC_VERSION});
1187 }
1188
Chris Forbes16095002017-05-05 15:33:29 -07001189 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001190 QueryPresentationProperties(physicalDevice, &presentation_properties);
1191 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001192 loader_extensions.push_back({
1193 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1194 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001195 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001196
Ian Elliott5c34de22017-04-10 14:42:30 -06001197 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1198 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001199 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001200 loader_extensions.push_back({
1201 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1202 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1203 }
1204
Trevor David Black2cc44682022-03-09 00:31:38 +00001205 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1206 // support is provided by the driver
1207 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1208 swapchainCompFeats = {};
1209 swapchainCompFeats.sType =
1210 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1211 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001212 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001213 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1214 imageCompFeats.sType =
1215 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1216 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001217 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001218
1219 VkPhysicalDeviceFeatures2 feats2 = {};
1220 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1221 feats2.pNext = &imageCompFeats;
1222
Trevor David Black929e9cd2022-11-22 04:12:19 +00001223 const auto& driver = GetData(physicalDevice).driver;
1224 if (driver.GetPhysicalDeviceFeatures2 ||
1225 driver.GetPhysicalDeviceFeatures2KHR) {
1226 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1227 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001228
Trevor David Black929e9cd2022-11-22 04:12:19 +00001229 bool anb9 = false;
1230 VkResult result =
1231 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1232
1233 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1234 return result;
1235 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001236
1237 if (anb9 && imageCompFeats.imageCompressionControl) {
1238 loader_extensions.push_back(
1239 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1240 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1241 }
1242 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1243 loader_extensions.push_back(
1244 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1245 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1246 }
1247
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001248 if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1249 loader_extensions.push_back({
1250 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1251 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1252 }
1253
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001254 // enumerate our extensions first
1255 if (!pLayerName && pProperties) {
1256 uint32_t count = std::min(
1257 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1258
Yiwei Zhang5e862202019-06-21 14:59:16 -07001259 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001260
1261 if (count < loader_extensions.size()) {
1262 *pPropertyCount = count;
1263 return VK_INCOMPLETE;
1264 }
1265
1266 pProperties += count;
1267 *pPropertyCount -= count;
1268 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001269
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001270 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001271 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001272 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001273 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001274
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001275 if (pProperties) {
1276 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1277 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1278 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001279
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001280 if (strcmp(prop.extensionName,
1281 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1282 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001283
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001284 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1285 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001286
1287 if (prop.specVersion >= 8) {
1288 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1289 } else {
1290 prop.specVersion = 68;
1291 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001292 }
1293 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001294
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001295 // restore loader extension count
1296 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1297 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001298 }
1299
1300 return result;
1301}
1302
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001303VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1304 const VkAllocationCallbacks* pAllocator,
1305 VkInstance* pInstance) {
1306 const VkAllocationCallbacks& data_allocator =
1307 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1308
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001309 VkResult result = VK_SUCCESS;
1310 uint32_t icd_api_version = VK_API_VERSION_1_0;
1311 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1312 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1313 Hal::Device().GetInstanceProcAddr(nullptr,
1314 "vkEnumerateInstanceVersion"));
1315 if (pfn_enumerate_instance_version) {
1316 ATRACE_BEGIN("pfn_enumerate_instance_version");
1317 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1318 ATRACE_END();
1319 if (result != VK_SUCCESS)
1320 return result;
1321
Trevor David Blackb68a2252021-08-23 16:37:18 +00001322 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001323 }
1324
1325 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1326 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001327 if (result != VK_SUCCESS)
1328 return result;
1329
1330 InstanceData* data = AllocateInstanceData(data_allocator);
1331 if (!data)
1332 return VK_ERROR_OUT_OF_HOST_MEMORY;
1333
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001334 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001335
1336 // call into the driver
1337 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001338 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001339 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001340 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1341 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001342 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001343 if (result != VK_SUCCESS) {
1344 FreeInstanceData(data, data_allocator);
1345 return result;
1346 }
1347
1348 // initialize InstanceDriverTable
1349 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001350 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001351 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001352 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001353 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001354 if (data->driver.DestroyInstance)
1355 data->driver.DestroyInstance(instance, pAllocator);
1356
1357 FreeInstanceData(data, data_allocator);
1358
1359 return VK_ERROR_INCOMPATIBLE_DRIVER;
1360 }
1361
1362 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001363 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001364 if (!data->get_device_proc_addr) {
1365 data->driver.DestroyInstance(instance, pAllocator);
1366 FreeInstanceData(data, data_allocator);
1367
1368 return VK_ERROR_INCOMPATIBLE_DRIVER;
1369 }
1370
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001371 // TODO(b/259516419) avoid getting stats from hwui
1372 // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1373 // || (strcmp("android framework",
1374 // pCreateInfo->pApplicationInfo->pEngineName) != 0);
1375 const bool reportStats = true;
1376 if (reportStats) {
1377 // Set stats for Vulkan api version requested with application info
1378 if (pCreateInfo->pApplicationInfo) {
1379 const uint32_t vulkanApiVersion =
1380 pCreateInfo->pApplicationInfo->apiVersion;
1381 android::GraphicsEnv::getInstance().setTargetStats(
1382 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1383 vulkanApiVersion);
Tom Murphyc23fcd02024-03-13 10:22:06 +00001384
1385 if (pCreateInfo->pApplicationInfo->pEngineName) {
1386 android::GraphicsEnv::getInstance().addVulkanEngineName(
1387 pCreateInfo->pApplicationInfo->pEngineName);
1388 }
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001389 }
1390
1391 // Update stats for the extensions requested
1392 android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1393 pCreateInfo->enabledExtensionCount,
1394 pCreateInfo->ppEnabledExtensionNames);
1395 }
1396
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001397 *pInstance = instance;
1398
1399 return VK_SUCCESS;
1400}
1401
1402void DestroyInstance(VkInstance instance,
1403 const VkAllocationCallbacks* pAllocator) {
1404 InstanceData& data = GetData(instance);
1405 data.driver.DestroyInstance(instance, pAllocator);
1406
1407 VkAllocationCallbacks local_allocator;
1408 if (!pAllocator) {
1409 local_allocator = data.allocator;
1410 pAllocator = &local_allocator;
1411 }
1412
1413 FreeInstanceData(&data, *pAllocator);
1414}
1415
Chia-I Wu4901db72016-03-24 16:38:58 +08001416VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1417 const VkDeviceCreateInfo* pCreateInfo,
1418 const VkAllocationCallbacks* pAllocator,
1419 VkDevice* pDevice) {
1420 const InstanceData& instance_data = GetData(physicalDevice);
1421 const VkAllocationCallbacks& data_allocator =
1422 (pAllocator) ? *pAllocator : instance_data.allocator;
1423
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001424 VkPhysicalDeviceProperties properties;
1425 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1426 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1427 &properties);
1428 ATRACE_END();
1429
1430 CreateInfoWrapper wrapper(
1431 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001432 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001433 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001434 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001435 if (result != VK_SUCCESS)
1436 return result;
1437
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001438 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001439 DeviceData* data = AllocateDeviceData(data_allocator,
1440 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001441 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001442 if (!data)
1443 return VK_ERROR_OUT_OF_HOST_MEMORY;
1444
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001445 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001446
1447 // call into the driver
1448 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001449 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001450 result = instance_data.driver.CreateDevice(
1451 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1452 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001453 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001454 if (result != VK_SUCCESS) {
1455 FreeDeviceData(data, data_allocator);
1456 return result;
1457 }
1458
1459 // initialize DeviceDriverTable
1460 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001461 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1462 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001463 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1464 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1465 if (data->driver.DestroyDevice)
1466 data->driver.DestroyDevice(dev, pAllocator);
1467
1468 FreeDeviceData(data, data_allocator);
1469
1470 return VK_ERROR_INCOMPATIBLE_DRIVER;
1471 }
Chris Forbesd8277912017-02-10 14:59:59 +13001472
Trevor David Black2cc44682022-03-09 00:31:38 +00001473 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001474 // entrypoints varies according to the spec version.
1475 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1476 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001477 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001478 !data->driver.GetSwapchainGrallocUsage3ANDROID &&
1479 !data->driver.GetSwapchainGrallocUsage4ANDROID) {
Trevor David Black2cc44682022-03-09 00:31:38 +00001480 ALOGE(
1481 "Driver's implementation of ANDROID_native_buffer is broken;"
1482 " must expose at least one of "
1483 "vkGetSwapchainGrallocUsageANDROID or "
1484 "vkGetSwapchainGrallocUsage2ANDROID or "
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001485 "vkGetSwapchainGrallocUsage3ANDROID or "
1486 "vkGetSwapchainGrallocUsage4ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001487
1488 data->driver.DestroyDevice(dev, pAllocator);
1489 FreeDeviceData(data, data_allocator);
1490
1491 return VK_ERROR_INCOMPATIBLE_DRIVER;
1492 }
1493
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001494 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1495 // Log that the app is hitting software Vulkan implementation
1496 android::GraphicsEnv::getInstance().setTargetStats(
1497 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1498 }
1499
Jesse Halldc225072016-05-30 22:40:14 -07001500 data->driver_device = dev;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001501 data->driver_physical_device = physicalDevice;
Chia-I Wu4901db72016-03-24 16:38:58 +08001502
1503 *pDevice = dev;
1504
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001505 // TODO(b/259516419) avoid getting stats from hwui
1506 const bool reportStats = true;
1507 if (reportStats) {
1508 android::GraphicsEnv::getInstance().setTargetStats(
1509 android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1510
1511 // Set stats for creating a Vulkan device and report features in use
1512 const VkPhysicalDeviceFeatures* pEnabledFeatures =
1513 pCreateInfo->pEnabledFeatures;
1514 if (!pEnabledFeatures) {
1515 // Use features from the chained VkPhysicalDeviceFeatures2
1516 // structure, if given
1517 const VkPhysicalDeviceFeatures2* features2 =
1518 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1519 pCreateInfo->pNext);
1520 while (features2 &&
1521 features2->sType !=
1522 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1523 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1524 features2->pNext);
1525 }
1526 if (features2) {
1527 pEnabledFeatures = &features2->features;
1528 }
1529 }
1530 const VkBool32* pFeatures =
1531 reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1532 if (pFeatures) {
1533 // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1534 // of them using pointer arithmetic here and save the features in a
1535 // 64-bit bitfield
1536 static_assert(
1537 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1538 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1539 "packing");
1540 static_assert(
1541 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1542 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1543 "packing");
1544 const int numFeatures =
1545 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1546
1547 uint64_t enableFeatureBits = 0;
1548 for (int i = 0; i < numFeatures; i++) {
1549 if (pFeatures[i] != VK_FALSE) {
1550 enableFeatureBits |= (uint64_t(1) << i);
1551 }
1552 }
1553 android::GraphicsEnv::getInstance().setTargetStats(
1554 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1555 enableFeatureBits);
1556 }
1557
1558 // Update stats for the extensions requested
1559 android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1560 pCreateInfo->enabledExtensionCount,
1561 pCreateInfo->ppEnabledExtensionNames);
1562 }
1563
Chia-I Wu4901db72016-03-24 16:38:58 +08001564 return VK_SUCCESS;
1565}
1566
1567void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1568 DeviceData& data = GetData(device);
1569 data.driver.DestroyDevice(device, pAllocator);
1570
1571 VkAllocationCallbacks local_allocator;
1572 if (!pAllocator) {
1573 local_allocator = data.allocator;
1574 pAllocator = &local_allocator;
1575 }
1576
1577 FreeDeviceData(&data, *pAllocator);
1578}
1579
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001580VkResult EnumeratePhysicalDevices(VkInstance instance,
1581 uint32_t* pPhysicalDeviceCount,
1582 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001583 ATRACE_CALL();
1584
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001585 const auto& data = GetData(instance);
1586
1587 VkResult result = data.driver.EnumeratePhysicalDevices(
1588 instance, pPhysicalDeviceCount, pPhysicalDevices);
1589 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1590 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1591 SetData(pPhysicalDevices[i], data);
1592 }
1593
1594 return result;
1595}
1596
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001597VkResult EnumeratePhysicalDeviceGroups(
1598 VkInstance instance,
1599 uint32_t* pPhysicalDeviceGroupCount,
1600 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001601 ATRACE_CALL();
1602
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001603 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001604 const auto& data = GetData(instance);
1605
Yiwei Zhange4f64172020-07-05 15:17:32 -07001606 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1607 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001608 uint32_t device_count = 0;
1609 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1610 if (result < 0)
1611 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001612
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001613 if (!pPhysicalDeviceGroupProperties) {
1614 *pPhysicalDeviceGroupCount = device_count;
1615 return result;
1616 }
1617
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001618 if (!device_count) {
1619 *pPhysicalDeviceGroupCount = 0;
1620 return result;
1621 }
Chad Versace32c087f2018-09-09 07:28:05 -07001622 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1623 if (!device_count)
1624 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001625
Yiwei Zhang5e862202019-06-21 14:59:16 -07001626 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001627 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001628 result =
1629 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001630 if (result < 0)
1631 return result;
1632
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001633 for (uint32_t i = 0; i < device_count; ++i) {
1634 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1635 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1636 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1637 }
1638 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001639 if (data.driver.EnumeratePhysicalDeviceGroups) {
1640 result = data.driver.EnumeratePhysicalDeviceGroups(
1641 instance, pPhysicalDeviceGroupCount,
1642 pPhysicalDeviceGroupProperties);
1643 } else {
1644 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1645 instance, pPhysicalDeviceGroupCount,
1646 pPhysicalDeviceGroupProperties);
1647 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001648 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1649 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1650 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1651 for (uint32_t j = 0;
1652 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1653 j++) {
1654 SetData(
1655 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001656 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001657 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001658 }
1659 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001660 }
1661
1662 return result;
1663}
1664
Chia-I Wuba0be412016-03-24 16:24:40 +08001665void GetDeviceQueue(VkDevice device,
1666 uint32_t queueFamilyIndex,
1667 uint32_t queueIndex,
1668 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001669 ATRACE_CALL();
1670
Chia-I Wuba0be412016-03-24 16:24:40 +08001671 const auto& data = GetData(device);
1672
1673 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1674 SetData(*pQueue, data);
1675}
1676
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001677void GetDeviceQueue2(VkDevice device,
1678 const VkDeviceQueueInfo2* pQueueInfo,
1679 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001680 ATRACE_CALL();
1681
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001682 const auto& data = GetData(device);
1683
1684 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001685 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001686}
1687
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001688VkResult AllocateCommandBuffers(
1689 VkDevice device,
1690 const VkCommandBufferAllocateInfo* pAllocateInfo,
1691 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001692 ATRACE_CALL();
1693
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001694 const auto& data = GetData(device);
1695
1696 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1697 pCommandBuffers);
1698 if (result == VK_SUCCESS) {
1699 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1700 SetData(pCommandBuffers[i], data);
1701 }
1702
1703 return result;
1704}
1705
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001706VkResult QueueSubmit(VkQueue queue,
1707 uint32_t submitCount,
1708 const VkSubmitInfo* pSubmits,
1709 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001710 ATRACE_CALL();
1711
1712 const auto& data = GetData(queue);
1713
1714 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1715}
1716
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001717void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1718 VkPhysicalDeviceFeatures2* pFeatures) {
1719 ATRACE_CALL();
1720
1721 const auto& driver = GetData(physicalDevice).driver;
1722
1723 if (driver.GetPhysicalDeviceFeatures2) {
1724 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001725 } else {
1726 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1727 }
1728
1729 // Conditionally add imageCompressionControlSwapchain if
1730 // imageCompressionControl is supported Check for imageCompressionControl in
1731 // the pChain
1732 bool imageCompressionControl = false;
1733 bool imageCompressionControlInChain = false;
1734 bool imageCompressionControlSwapchainInChain = false;
1735 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1736 while (pFeats) {
1737 switch (pFeats->sType) {
1738 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1739 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1740 compressionFeat = reinterpret_cast<
1741 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1742 pFeats);
1743 imageCompressionControl =
1744 compressionFeat->imageCompressionControl;
1745 imageCompressionControlInChain = true;
1746 } break;
1747
1748 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001749 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1750 compressionFeat = reinterpret_cast<
1751 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1752 pFeats);
1753 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001754 imageCompressionControlSwapchainInChain = true;
1755 } break;
1756
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001757 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1758 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1759 pFeats);
1760 smf->swapchainMaintenance1 = true;
1761 } break;
1762
Trevor David Black2cc44682022-03-09 00:31:38 +00001763 default:
1764 break;
1765 }
1766 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1767 }
1768
1769 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001770 return;
1771 }
1772
Trevor David Black2cc44682022-03-09 00:31:38 +00001773 // If not in pchain, explicitly query for imageCompressionControl
1774 if (!imageCompressionControlInChain) {
1775 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1776 imageCompFeats.sType =
1777 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1778 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001779 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001780
1781 VkPhysicalDeviceFeatures2 feats2 = {};
1782 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1783 feats2.pNext = &imageCompFeats;
1784
1785 if (driver.GetPhysicalDeviceFeatures2) {
1786 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1787 } else {
1788 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1789 }
1790
1791 imageCompressionControl = imageCompFeats.imageCompressionControl;
1792 }
1793
1794 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1795 if (imageCompressionControl) {
1796 pFeats = pFeatures;
1797 while (pFeats) {
1798 switch (pFeats->sType) {
1799 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1800 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1801 compressionFeat = reinterpret_cast<
1802 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1803 pFeats);
1804 compressionFeat->imageCompressionControlSwapchain = true;
1805 } break;
1806
1807 default:
1808 break;
1809 }
1810 pFeats =
1811 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1812 }
1813 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001814}
1815
1816void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1817 VkPhysicalDeviceProperties2* pProperties) {
1818 ATRACE_CALL();
1819
1820 const auto& driver = GetData(physicalDevice).driver;
1821
1822 if (driver.GetPhysicalDeviceProperties2) {
1823 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1824 return;
1825 }
1826
1827 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1828}
1829
1830void GetPhysicalDeviceFormatProperties2(
1831 VkPhysicalDevice physicalDevice,
1832 VkFormat format,
1833 VkFormatProperties2* pFormatProperties) {
1834 ATRACE_CALL();
1835
1836 const auto& driver = GetData(physicalDevice).driver;
1837
1838 if (driver.GetPhysicalDeviceFormatProperties2) {
1839 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1840 pFormatProperties);
1841 return;
1842 }
1843
1844 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1845 pFormatProperties);
1846}
1847
1848VkResult GetPhysicalDeviceImageFormatProperties2(
1849 VkPhysicalDevice physicalDevice,
1850 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1851 VkImageFormatProperties2* pImageFormatProperties) {
1852 ATRACE_CALL();
1853
1854 const auto& driver = GetData(physicalDevice).driver;
1855
1856 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1857 return driver.GetPhysicalDeviceImageFormatProperties2(
1858 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1859 }
1860
1861 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1862 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1863}
1864
1865void GetPhysicalDeviceQueueFamilyProperties2(
1866 VkPhysicalDevice physicalDevice,
1867 uint32_t* pQueueFamilyPropertyCount,
1868 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1869 ATRACE_CALL();
1870
1871 const auto& driver = GetData(physicalDevice).driver;
1872
1873 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1874 driver.GetPhysicalDeviceQueueFamilyProperties2(
1875 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1876 return;
1877 }
1878
1879 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1880 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1881}
1882
1883void GetPhysicalDeviceMemoryProperties2(
1884 VkPhysicalDevice physicalDevice,
1885 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1886 ATRACE_CALL();
1887
1888 const auto& driver = GetData(physicalDevice).driver;
1889
1890 if (driver.GetPhysicalDeviceMemoryProperties2) {
1891 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1892 pMemoryProperties);
1893 return;
1894 }
1895
1896 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1897 pMemoryProperties);
1898}
1899
1900void GetPhysicalDeviceSparseImageFormatProperties2(
1901 VkPhysicalDevice physicalDevice,
1902 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1903 uint32_t* pPropertyCount,
1904 VkSparseImageFormatProperties2* pProperties) {
1905 ATRACE_CALL();
1906
1907 const auto& driver = GetData(physicalDevice).driver;
1908
1909 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1910 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1911 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1912 return;
1913 }
1914
1915 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1916 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1917}
1918
Yiwei Zhange1f35012020-07-05 22:52:04 -07001919void GetPhysicalDeviceExternalBufferProperties(
1920 VkPhysicalDevice physicalDevice,
1921 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1922 VkExternalBufferProperties* pExternalBufferProperties) {
1923 ATRACE_CALL();
1924
1925 const auto& driver = GetData(physicalDevice).driver;
1926
1927 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1928 driver.GetPhysicalDeviceExternalBufferProperties(
1929 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1930 return;
1931 }
1932
1933 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1934 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1935 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1936 return;
1937 }
1938
1939 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1940 sizeof(VkExternalMemoryProperties));
1941}
1942
1943void GetPhysicalDeviceExternalSemaphoreProperties(
1944 VkPhysicalDevice physicalDevice,
1945 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1946 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1947 ATRACE_CALL();
1948
1949 const auto& driver = GetData(physicalDevice).driver;
1950
1951 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1952 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1953 physicalDevice, pExternalSemaphoreInfo,
1954 pExternalSemaphoreProperties);
1955 return;
1956 }
1957
1958 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1959 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1960 physicalDevice, pExternalSemaphoreInfo,
1961 pExternalSemaphoreProperties);
1962 return;
1963 }
1964
1965 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1966 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1967 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1968}
1969
1970void GetPhysicalDeviceExternalFenceProperties(
1971 VkPhysicalDevice physicalDevice,
1972 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1973 VkExternalFenceProperties* pExternalFenceProperties) {
1974 ATRACE_CALL();
1975
1976 const auto& driver = GetData(physicalDevice).driver;
1977
1978 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1979 driver.GetPhysicalDeviceExternalFenceProperties(
1980 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1981 return;
1982 }
1983
1984 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1985 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1986 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1987 return;
1988 }
1989
1990 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1991 pExternalFenceProperties->compatibleHandleTypes = 0;
1992 pExternalFenceProperties->externalFenceFeatures = 0;
1993}
1994
Chia-I Wu9d518162016-03-24 14:55:27 +08001995} // namespace driver
1996} // namespace vulkan