blob: 7ea98f546945098ec840d82a25f514a5bf220b5e [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);
967 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800968 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800969
970 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800971 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800972 return nullptr;
973 }
974
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800975 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
976 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800977}
978
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800979VkResult EnumerateInstanceExtensionProperties(
980 const char* pLayerName,
981 uint32_t* pPropertyCount,
982 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700983 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700984 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600985 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600986 loader_extensions.push_back(
987 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
988 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600989 loader_extensions.push_back({
990 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
991 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
992 loader_extensions.push_back({
993 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
994 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -0600995 loader_extensions.push_back(
996 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
997 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -0600998 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
999 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001000 loader_extensions.push_back({
1001 VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
1002 VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -06001003
Chia-I Wu31938252016-05-23 15:31:02 +08001004 static const VkExtensionProperties loader_debug_report_extension = {
1005 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
1006 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001007
1008 // enumerate our extensions first
1009 if (!pLayerName && pProperties) {
1010 uint32_t count = std::min(
1011 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1012
Yiwei Zhang5e862202019-06-21 14:59:16 -07001013 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001014
1015 if (count < loader_extensions.size()) {
1016 *pPropertyCount = count;
1017 return VK_INCOMPLETE;
1018 }
1019
1020 pProperties += count;
1021 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +08001022
1023 if (Hal::Get().GetDebugReportIndex() < 0) {
1024 if (!*pPropertyCount) {
1025 *pPropertyCount = count;
1026 return VK_INCOMPLETE;
1027 }
1028
1029 pProperties[0] = loader_debug_report_extension;
1030 pProperties += 1;
1031 *pPropertyCount -= 1;
1032 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001033 }
1034
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001035 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001036 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001037 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001038 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001039
Chia-I Wu31938252016-05-23 15:31:02 +08001040 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1041 int idx = Hal::Get().GetDebugReportIndex();
1042 if (idx < 0) {
1043 *pPropertyCount += 1;
1044 } else if (pProperties &&
1045 static_cast<uint32_t>(idx) < *pPropertyCount) {
1046 pProperties[idx].specVersion =
1047 std::min(pProperties[idx].specVersion,
1048 loader_debug_report_extension.specVersion);
1049 }
1050
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001051 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +08001052 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001053
1054 return result;
1055}
1056
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001057void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001058 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001059 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001060 ATRACE_CALL();
1061
Chris Forbesfa25e632017-02-22 12:36:02 +13001062 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001063 VkPhysicalDeviceProperties2 properties = {
1064 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001065 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001066 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001067 };
1068
1069#pragma clang diagnostic push
1070#pragma clang diagnostic ignored "-Wold-style-cast"
1071 presentation_properties->sType =
1072 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1073#pragma clang diagnostic pop
1074 presentation_properties->pNext = nullptr;
1075 presentation_properties->sharedImage = VK_FALSE;
1076
Chris Forbese056c122021-07-22 13:54:04 -07001077 const auto& driver = GetData(physicalDevice).driver;
1078
1079 if (driver.GetPhysicalDeviceProperties2) {
1080 // >= 1.1 driver, supports core GPDP2 entrypoint.
1081 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1082 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1083 // Old driver, but may support presentation properties
1084 // if we have the GPDP2 extension. Otherwise, no presentation
1085 // properties supported.
1086 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1087 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001088}
1089
Trevor David Black929e9cd2022-11-22 04:12:19 +00001090VkResult GetAndroidNativeBufferSpecVersion9Support(
1091 VkPhysicalDevice physicalDevice,
1092 bool& support) {
1093 support = false;
1094
Trevor David Black2cc44682022-03-09 00:31:38 +00001095 const InstanceData& data = GetData(physicalDevice);
1096
1097 // Call to get propertyCount
1098 uint32_t propertyCount = 0;
1099 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1100 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1101 physicalDevice, nullptr, &propertyCount, nullptr);
1102 ATRACE_END();
1103
Trevor David Black929e9cd2022-11-22 04:12:19 +00001104 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1105 return result;
1106 }
1107
Trevor David Black2cc44682022-03-09 00:31:38 +00001108 // Call to enumerate properties
1109 std::vector<VkExtensionProperties> properties(propertyCount);
1110 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1111 result = data.driver.EnumerateDeviceExtensionProperties(
1112 physicalDevice, nullptr, &propertyCount, properties.data());
1113 ATRACE_END();
1114
Trevor David Black929e9cd2022-11-22 04:12:19 +00001115 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1116 return result;
1117 }
1118
Trevor David Black2cc44682022-03-09 00:31:38 +00001119 for (uint32_t i = 0; i < propertyCount; i++) {
1120 auto& prop = properties[i];
1121
1122 if (strcmp(prop.extensionName,
1123 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1124 continue;
1125
1126 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001127 support = true;
1128 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001129 }
1130 }
1131
Trevor David Black929e9cd2022-11-22 04:12:19 +00001132 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001133}
1134
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001135bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1136 const auto& driver = GetData(physicalDevice).driver;
1137 if (!driver.GetPhysicalDeviceExternalFenceProperties)
1138 return false;
1139
1140 // Requires support for external fences imported from sync fds.
1141 // This is _almost_ universal on Android, but may be missing on
1142 // some extremely old drivers, or on strange implementations like
1143 // cuttlefish.
1144 VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1145 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1146 nullptr,
1147 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1148 };
1149 VkExternalFenceProperties fenceProperties = {
1150 VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1151 nullptr,
1152 0, 0, 0
1153 };
1154
1155 GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1156 if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1157 return true;
1158
1159 return false;
1160}
1161
Chia-I Wu01cf3052016-03-24 16:16:21 +08001162VkResult EnumerateDeviceExtensionProperties(
1163 VkPhysicalDevice physicalDevice,
1164 const char* pLayerName,
1165 uint32_t* pPropertyCount,
1166 VkExtensionProperties* pProperties) {
1167 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001168 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001169 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001170 loader_extensions.push_back({
1171 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1172 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001173
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001174 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001175 if (hdrBoardConfig) {
1176 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1177 VK_EXT_HDR_METADATA_SPEC_VERSION});
1178 }
1179
Chris Forbes16095002017-05-05 15:33:29 -07001180 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001181 QueryPresentationProperties(physicalDevice, &presentation_properties);
1182 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001183 loader_extensions.push_back({
1184 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1185 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001186 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001187
Ian Elliott5c34de22017-04-10 14:42:30 -06001188 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1189 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001190 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001191 loader_extensions.push_back({
1192 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1193 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1194 }
1195
Trevor David Black2cc44682022-03-09 00:31:38 +00001196 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1197 // support is provided by the driver
1198 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1199 swapchainCompFeats = {};
1200 swapchainCompFeats.sType =
1201 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1202 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001203 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001204 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1205 imageCompFeats.sType =
1206 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1207 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001208 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001209
1210 VkPhysicalDeviceFeatures2 feats2 = {};
1211 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1212 feats2.pNext = &imageCompFeats;
1213
Trevor David Black929e9cd2022-11-22 04:12:19 +00001214 const auto& driver = GetData(physicalDevice).driver;
1215 if (driver.GetPhysicalDeviceFeatures2 ||
1216 driver.GetPhysicalDeviceFeatures2KHR) {
1217 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1218 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001219
Trevor David Black929e9cd2022-11-22 04:12:19 +00001220 bool anb9 = false;
1221 VkResult result =
1222 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1223
1224 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1225 return result;
1226 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001227
1228 if (anb9 && imageCompFeats.imageCompressionControl) {
1229 loader_extensions.push_back(
1230 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1231 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1232 }
1233 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1234 loader_extensions.push_back(
1235 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1236 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1237 }
1238
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001239 if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1240 loader_extensions.push_back({
1241 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1242 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1243 }
1244
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001245 // enumerate our extensions first
1246 if (!pLayerName && pProperties) {
1247 uint32_t count = std::min(
1248 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1249
Yiwei Zhang5e862202019-06-21 14:59:16 -07001250 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001251
1252 if (count < loader_extensions.size()) {
1253 *pPropertyCount = count;
1254 return VK_INCOMPLETE;
1255 }
1256
1257 pProperties += count;
1258 *pPropertyCount -= count;
1259 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001260
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001261 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001262 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001263 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001264 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001265
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001266 if (pProperties) {
1267 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1268 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1269 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001270
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001271 if (strcmp(prop.extensionName,
1272 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1273 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001274
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001275 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1276 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001277
1278 if (prop.specVersion >= 8) {
1279 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1280 } else {
1281 prop.specVersion = 68;
1282 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001283 }
1284 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001285
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001286 // restore loader extension count
1287 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1288 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001289 }
1290
1291 return result;
1292}
1293
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001294VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1295 const VkAllocationCallbacks* pAllocator,
1296 VkInstance* pInstance) {
1297 const VkAllocationCallbacks& data_allocator =
1298 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1299
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001300 VkResult result = VK_SUCCESS;
1301 uint32_t icd_api_version = VK_API_VERSION_1_0;
1302 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1303 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1304 Hal::Device().GetInstanceProcAddr(nullptr,
1305 "vkEnumerateInstanceVersion"));
1306 if (pfn_enumerate_instance_version) {
1307 ATRACE_BEGIN("pfn_enumerate_instance_version");
1308 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1309 ATRACE_END();
1310 if (result != VK_SUCCESS)
1311 return result;
1312
Trevor David Blackb68a2252021-08-23 16:37:18 +00001313 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001314 }
1315
1316 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1317 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001318 if (result != VK_SUCCESS)
1319 return result;
1320
1321 InstanceData* data = AllocateInstanceData(data_allocator);
1322 if (!data)
1323 return VK_ERROR_OUT_OF_HOST_MEMORY;
1324
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001325 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001326
1327 // call into the driver
1328 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001329 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001330 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001331 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1332 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001333 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001334 if (result != VK_SUCCESS) {
1335 FreeInstanceData(data, data_allocator);
1336 return result;
1337 }
1338
1339 // initialize InstanceDriverTable
1340 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001341 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001342 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001343 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001344 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001345 if (data->driver.DestroyInstance)
1346 data->driver.DestroyInstance(instance, pAllocator);
1347
1348 FreeInstanceData(data, data_allocator);
1349
1350 return VK_ERROR_INCOMPATIBLE_DRIVER;
1351 }
1352
1353 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001354 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001355 if (!data->get_device_proc_addr) {
1356 data->driver.DestroyInstance(instance, pAllocator);
1357 FreeInstanceData(data, data_allocator);
1358
1359 return VK_ERROR_INCOMPATIBLE_DRIVER;
1360 }
1361
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001362 // TODO(b/259516419) avoid getting stats from hwui
1363 // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1364 // || (strcmp("android framework",
1365 // pCreateInfo->pApplicationInfo->pEngineName) != 0);
1366 const bool reportStats = true;
1367 if (reportStats) {
1368 // Set stats for Vulkan api version requested with application info
1369 if (pCreateInfo->pApplicationInfo) {
1370 const uint32_t vulkanApiVersion =
1371 pCreateInfo->pApplicationInfo->apiVersion;
1372 android::GraphicsEnv::getInstance().setTargetStats(
1373 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1374 vulkanApiVersion);
Tom Murphyc23fcd02024-03-13 10:22:06 +00001375
1376 if (pCreateInfo->pApplicationInfo->pEngineName) {
1377 android::GraphicsEnv::getInstance().addVulkanEngineName(
1378 pCreateInfo->pApplicationInfo->pEngineName);
1379 }
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001380 }
1381
1382 // Update stats for the extensions requested
1383 android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1384 pCreateInfo->enabledExtensionCount,
1385 pCreateInfo->ppEnabledExtensionNames);
1386 }
1387
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001388 *pInstance = instance;
1389
1390 return VK_SUCCESS;
1391}
1392
1393void DestroyInstance(VkInstance instance,
1394 const VkAllocationCallbacks* pAllocator) {
1395 InstanceData& data = GetData(instance);
1396 data.driver.DestroyInstance(instance, pAllocator);
1397
1398 VkAllocationCallbacks local_allocator;
1399 if (!pAllocator) {
1400 local_allocator = data.allocator;
1401 pAllocator = &local_allocator;
1402 }
1403
1404 FreeInstanceData(&data, *pAllocator);
1405}
1406
Chia-I Wu4901db72016-03-24 16:38:58 +08001407VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1408 const VkDeviceCreateInfo* pCreateInfo,
1409 const VkAllocationCallbacks* pAllocator,
1410 VkDevice* pDevice) {
1411 const InstanceData& instance_data = GetData(physicalDevice);
1412 const VkAllocationCallbacks& data_allocator =
1413 (pAllocator) ? *pAllocator : instance_data.allocator;
1414
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001415 VkPhysicalDeviceProperties properties;
1416 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1417 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1418 &properties);
1419 ATRACE_END();
1420
1421 CreateInfoWrapper wrapper(
1422 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001423 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001424 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001425 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001426 if (result != VK_SUCCESS)
1427 return result;
1428
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001429 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001430 DeviceData* data = AllocateDeviceData(data_allocator,
1431 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001432 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001433 if (!data)
1434 return VK_ERROR_OUT_OF_HOST_MEMORY;
1435
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001436 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001437
1438 // call into the driver
1439 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001440 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001441 result = instance_data.driver.CreateDevice(
1442 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1443 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001444 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001445 if (result != VK_SUCCESS) {
1446 FreeDeviceData(data, data_allocator);
1447 return result;
1448 }
1449
1450 // initialize DeviceDriverTable
1451 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001452 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1453 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001454 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1455 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1456 if (data->driver.DestroyDevice)
1457 data->driver.DestroyDevice(dev, pAllocator);
1458
1459 FreeDeviceData(data, data_allocator);
1460
1461 return VK_ERROR_INCOMPATIBLE_DRIVER;
1462 }
Chris Forbesd8277912017-02-10 14:59:59 +13001463
Trevor David Black2cc44682022-03-09 00:31:38 +00001464 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001465 // entrypoints varies according to the spec version.
1466 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1467 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001468 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001469 !data->driver.GetSwapchainGrallocUsage3ANDROID &&
1470 !data->driver.GetSwapchainGrallocUsage4ANDROID) {
Trevor David Black2cc44682022-03-09 00:31:38 +00001471 ALOGE(
1472 "Driver's implementation of ANDROID_native_buffer is broken;"
1473 " must expose at least one of "
1474 "vkGetSwapchainGrallocUsageANDROID or "
1475 "vkGetSwapchainGrallocUsage2ANDROID or "
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001476 "vkGetSwapchainGrallocUsage3ANDROID or "
1477 "vkGetSwapchainGrallocUsage4ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001478
1479 data->driver.DestroyDevice(dev, pAllocator);
1480 FreeDeviceData(data, data_allocator);
1481
1482 return VK_ERROR_INCOMPATIBLE_DRIVER;
1483 }
1484
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001485 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1486 // Log that the app is hitting software Vulkan implementation
1487 android::GraphicsEnv::getInstance().setTargetStats(
1488 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1489 }
1490
Jesse Halldc225072016-05-30 22:40:14 -07001491 data->driver_device = dev;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001492 data->driver_physical_device = physicalDevice;
Chia-I Wu4901db72016-03-24 16:38:58 +08001493
1494 *pDevice = dev;
1495
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001496 // TODO(b/259516419) avoid getting stats from hwui
1497 const bool reportStats = true;
1498 if (reportStats) {
1499 android::GraphicsEnv::getInstance().setTargetStats(
1500 android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1501
1502 // Set stats for creating a Vulkan device and report features in use
1503 const VkPhysicalDeviceFeatures* pEnabledFeatures =
1504 pCreateInfo->pEnabledFeatures;
1505 if (!pEnabledFeatures) {
1506 // Use features from the chained VkPhysicalDeviceFeatures2
1507 // structure, if given
1508 const VkPhysicalDeviceFeatures2* features2 =
1509 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1510 pCreateInfo->pNext);
1511 while (features2 &&
1512 features2->sType !=
1513 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1514 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1515 features2->pNext);
1516 }
1517 if (features2) {
1518 pEnabledFeatures = &features2->features;
1519 }
1520 }
1521 const VkBool32* pFeatures =
1522 reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1523 if (pFeatures) {
1524 // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1525 // of them using pointer arithmetic here and save the features in a
1526 // 64-bit bitfield
1527 static_assert(
1528 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1529 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1530 "packing");
1531 static_assert(
1532 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1533 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1534 "packing");
1535 const int numFeatures =
1536 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1537
1538 uint64_t enableFeatureBits = 0;
1539 for (int i = 0; i < numFeatures; i++) {
1540 if (pFeatures[i] != VK_FALSE) {
1541 enableFeatureBits |= (uint64_t(1) << i);
1542 }
1543 }
1544 android::GraphicsEnv::getInstance().setTargetStats(
1545 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1546 enableFeatureBits);
1547 }
1548
1549 // Update stats for the extensions requested
1550 android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1551 pCreateInfo->enabledExtensionCount,
1552 pCreateInfo->ppEnabledExtensionNames);
1553 }
1554
Chia-I Wu4901db72016-03-24 16:38:58 +08001555 return VK_SUCCESS;
1556}
1557
1558void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1559 DeviceData& data = GetData(device);
1560 data.driver.DestroyDevice(device, pAllocator);
1561
1562 VkAllocationCallbacks local_allocator;
1563 if (!pAllocator) {
1564 local_allocator = data.allocator;
1565 pAllocator = &local_allocator;
1566 }
1567
1568 FreeDeviceData(&data, *pAllocator);
1569}
1570
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001571VkResult EnumeratePhysicalDevices(VkInstance instance,
1572 uint32_t* pPhysicalDeviceCount,
1573 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001574 ATRACE_CALL();
1575
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001576 const auto& data = GetData(instance);
1577
1578 VkResult result = data.driver.EnumeratePhysicalDevices(
1579 instance, pPhysicalDeviceCount, pPhysicalDevices);
1580 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1581 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1582 SetData(pPhysicalDevices[i], data);
1583 }
1584
1585 return result;
1586}
1587
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001588VkResult EnumeratePhysicalDeviceGroups(
1589 VkInstance instance,
1590 uint32_t* pPhysicalDeviceGroupCount,
1591 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001592 ATRACE_CALL();
1593
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001594 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001595 const auto& data = GetData(instance);
1596
Yiwei Zhange4f64172020-07-05 15:17:32 -07001597 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1598 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001599 uint32_t device_count = 0;
1600 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1601 if (result < 0)
1602 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001603
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001604 if (!pPhysicalDeviceGroupProperties) {
1605 *pPhysicalDeviceGroupCount = device_count;
1606 return result;
1607 }
1608
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001609 if (!device_count) {
1610 *pPhysicalDeviceGroupCount = 0;
1611 return result;
1612 }
Chad Versace32c087f2018-09-09 07:28:05 -07001613 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1614 if (!device_count)
1615 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001616
Yiwei Zhang5e862202019-06-21 14:59:16 -07001617 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001618 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001619 result =
1620 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001621 if (result < 0)
1622 return result;
1623
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001624 for (uint32_t i = 0; i < device_count; ++i) {
1625 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1626 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1627 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1628 }
1629 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001630 if (data.driver.EnumeratePhysicalDeviceGroups) {
1631 result = data.driver.EnumeratePhysicalDeviceGroups(
1632 instance, pPhysicalDeviceGroupCount,
1633 pPhysicalDeviceGroupProperties);
1634 } else {
1635 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1636 instance, pPhysicalDeviceGroupCount,
1637 pPhysicalDeviceGroupProperties);
1638 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001639 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1640 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1641 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1642 for (uint32_t j = 0;
1643 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1644 j++) {
1645 SetData(
1646 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001647 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001648 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001649 }
1650 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001651 }
1652
1653 return result;
1654}
1655
Chia-I Wuba0be412016-03-24 16:24:40 +08001656void GetDeviceQueue(VkDevice device,
1657 uint32_t queueFamilyIndex,
1658 uint32_t queueIndex,
1659 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001660 ATRACE_CALL();
1661
Chia-I Wuba0be412016-03-24 16:24:40 +08001662 const auto& data = GetData(device);
1663
1664 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1665 SetData(*pQueue, data);
1666}
1667
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001668void GetDeviceQueue2(VkDevice device,
1669 const VkDeviceQueueInfo2* pQueueInfo,
1670 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001671 ATRACE_CALL();
1672
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001673 const auto& data = GetData(device);
1674
1675 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001676 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001677}
1678
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001679VkResult AllocateCommandBuffers(
1680 VkDevice device,
1681 const VkCommandBufferAllocateInfo* pAllocateInfo,
1682 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001683 ATRACE_CALL();
1684
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001685 const auto& data = GetData(device);
1686
1687 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1688 pCommandBuffers);
1689 if (result == VK_SUCCESS) {
1690 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1691 SetData(pCommandBuffers[i], data);
1692 }
1693
1694 return result;
1695}
1696
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001697VkResult QueueSubmit(VkQueue queue,
1698 uint32_t submitCount,
1699 const VkSubmitInfo* pSubmits,
1700 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001701 ATRACE_CALL();
1702
1703 const auto& data = GetData(queue);
1704
1705 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1706}
1707
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001708void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1709 VkPhysicalDeviceFeatures2* pFeatures) {
1710 ATRACE_CALL();
1711
1712 const auto& driver = GetData(physicalDevice).driver;
1713
1714 if (driver.GetPhysicalDeviceFeatures2) {
1715 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001716 } else {
1717 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1718 }
1719
1720 // Conditionally add imageCompressionControlSwapchain if
1721 // imageCompressionControl is supported Check for imageCompressionControl in
1722 // the pChain
1723 bool imageCompressionControl = false;
1724 bool imageCompressionControlInChain = false;
1725 bool imageCompressionControlSwapchainInChain = false;
1726 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1727 while (pFeats) {
1728 switch (pFeats->sType) {
1729 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1730 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1731 compressionFeat = reinterpret_cast<
1732 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1733 pFeats);
1734 imageCompressionControl =
1735 compressionFeat->imageCompressionControl;
1736 imageCompressionControlInChain = true;
1737 } break;
1738
1739 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001740 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1741 compressionFeat = reinterpret_cast<
1742 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1743 pFeats);
1744 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001745 imageCompressionControlSwapchainInChain = true;
1746 } break;
1747
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001748 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1749 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1750 pFeats);
1751 smf->swapchainMaintenance1 = true;
1752 } break;
1753
Trevor David Black2cc44682022-03-09 00:31:38 +00001754 default:
1755 break;
1756 }
1757 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1758 }
1759
1760 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001761 return;
1762 }
1763
Trevor David Black2cc44682022-03-09 00:31:38 +00001764 // If not in pchain, explicitly query for imageCompressionControl
1765 if (!imageCompressionControlInChain) {
1766 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1767 imageCompFeats.sType =
1768 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1769 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001770 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001771
1772 VkPhysicalDeviceFeatures2 feats2 = {};
1773 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1774 feats2.pNext = &imageCompFeats;
1775
1776 if (driver.GetPhysicalDeviceFeatures2) {
1777 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1778 } else {
1779 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1780 }
1781
1782 imageCompressionControl = imageCompFeats.imageCompressionControl;
1783 }
1784
1785 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1786 if (imageCompressionControl) {
1787 pFeats = pFeatures;
1788 while (pFeats) {
1789 switch (pFeats->sType) {
1790 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1791 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1792 compressionFeat = reinterpret_cast<
1793 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1794 pFeats);
1795 compressionFeat->imageCompressionControlSwapchain = true;
1796 } break;
1797
1798 default:
1799 break;
1800 }
1801 pFeats =
1802 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1803 }
1804 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001805}
1806
1807void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1808 VkPhysicalDeviceProperties2* pProperties) {
1809 ATRACE_CALL();
1810
1811 const auto& driver = GetData(physicalDevice).driver;
1812
1813 if (driver.GetPhysicalDeviceProperties2) {
1814 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1815 return;
1816 }
1817
1818 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1819}
1820
1821void GetPhysicalDeviceFormatProperties2(
1822 VkPhysicalDevice physicalDevice,
1823 VkFormat format,
1824 VkFormatProperties2* pFormatProperties) {
1825 ATRACE_CALL();
1826
1827 const auto& driver = GetData(physicalDevice).driver;
1828
1829 if (driver.GetPhysicalDeviceFormatProperties2) {
1830 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1831 pFormatProperties);
1832 return;
1833 }
1834
1835 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1836 pFormatProperties);
1837}
1838
1839VkResult GetPhysicalDeviceImageFormatProperties2(
1840 VkPhysicalDevice physicalDevice,
1841 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1842 VkImageFormatProperties2* pImageFormatProperties) {
1843 ATRACE_CALL();
1844
1845 const auto& driver = GetData(physicalDevice).driver;
1846
1847 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1848 return driver.GetPhysicalDeviceImageFormatProperties2(
1849 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1850 }
1851
1852 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1853 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1854}
1855
1856void GetPhysicalDeviceQueueFamilyProperties2(
1857 VkPhysicalDevice physicalDevice,
1858 uint32_t* pQueueFamilyPropertyCount,
1859 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1860 ATRACE_CALL();
1861
1862 const auto& driver = GetData(physicalDevice).driver;
1863
1864 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1865 driver.GetPhysicalDeviceQueueFamilyProperties2(
1866 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1867 return;
1868 }
1869
1870 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1871 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1872}
1873
1874void GetPhysicalDeviceMemoryProperties2(
1875 VkPhysicalDevice physicalDevice,
1876 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1877 ATRACE_CALL();
1878
1879 const auto& driver = GetData(physicalDevice).driver;
1880
1881 if (driver.GetPhysicalDeviceMemoryProperties2) {
1882 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1883 pMemoryProperties);
1884 return;
1885 }
1886
1887 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1888 pMemoryProperties);
1889}
1890
1891void GetPhysicalDeviceSparseImageFormatProperties2(
1892 VkPhysicalDevice physicalDevice,
1893 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1894 uint32_t* pPropertyCount,
1895 VkSparseImageFormatProperties2* pProperties) {
1896 ATRACE_CALL();
1897
1898 const auto& driver = GetData(physicalDevice).driver;
1899
1900 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1901 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1902 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1903 return;
1904 }
1905
1906 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1907 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1908}
1909
Yiwei Zhange1f35012020-07-05 22:52:04 -07001910void GetPhysicalDeviceExternalBufferProperties(
1911 VkPhysicalDevice physicalDevice,
1912 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1913 VkExternalBufferProperties* pExternalBufferProperties) {
1914 ATRACE_CALL();
1915
1916 const auto& driver = GetData(physicalDevice).driver;
1917
1918 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1919 driver.GetPhysicalDeviceExternalBufferProperties(
1920 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1921 return;
1922 }
1923
1924 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1925 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1926 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1927 return;
1928 }
1929
1930 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1931 sizeof(VkExternalMemoryProperties));
1932}
1933
1934void GetPhysicalDeviceExternalSemaphoreProperties(
1935 VkPhysicalDevice physicalDevice,
1936 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1937 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1938 ATRACE_CALL();
1939
1940 const auto& driver = GetData(physicalDevice).driver;
1941
1942 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1943 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1944 physicalDevice, pExternalSemaphoreInfo,
1945 pExternalSemaphoreProperties);
1946 return;
1947 }
1948
1949 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1950 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1951 physicalDevice, pExternalSemaphoreInfo,
1952 pExternalSemaphoreProperties);
1953 return;
1954 }
1955
1956 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1957 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1958 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1959}
1960
1961void GetPhysicalDeviceExternalFenceProperties(
1962 VkPhysicalDevice physicalDevice,
1963 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1964 VkExternalFenceProperties* pExternalFenceProperties) {
1965 ATRACE_CALL();
1966
1967 const auto& driver = GetData(physicalDevice).driver;
1968
1969 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1970 driver.GetPhysicalDeviceExternalFenceProperties(
1971 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1972 return;
1973 }
1974
1975 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1976 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1977 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1978 return;
1979 }
1980
1981 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1982 pExternalFenceProperties->compatibleHandleTypes = 0;
1983 pExternalFenceProperties->externalFenceFeatures = 0;
1984}
1985
Chia-I Wu9d518162016-03-24 14:55:27 +08001986} // namespace driver
1987} // namespace vulkan