blob: a16ab48a188da3d551c1d88115fad30f745b72b9 [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
343 ALOG_ASSERT(!hal_.dev_->common.close(hal_.dev_->common),
344 "hw_device_t::close() failed.");
345
346 // Close the opened shared library in the hw_module_t
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700347 android_unload_sphal_library(hal_.dev_->common.module->dso);
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700348
349 hal_.dev_ = nullptr;
350 hal_.debug_report_index_ = -1;
351}
352
Chia-I Wu31938252016-05-23 15:31:02 +0800353bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800354 ATRACE_CALL();
355
Chia-I Wu31938252016-05-23 15:31:02 +0800356 uint32_t count;
357 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
358 VK_SUCCESS) {
359 ALOGE("failed to get HAL instance extension count");
360 return false;
361 }
362
363 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
364 malloc(sizeof(VkExtensionProperties) * count));
365 if (!exts) {
366 ALOGE("failed to allocate HAL instance extension array");
367 return false;
368 }
369
370 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
371 VK_SUCCESS) {
372 ALOGE("failed to enumerate HAL instance extensions");
373 free(exts);
374 return false;
375 }
376
377 for (uint32_t i = 0; i < count; i++) {
378 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
379 0) {
380 debug_report_index_ = static_cast<int>(i);
381 break;
382 }
383 }
384
385 free(exts);
386
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800387 return true;
388}
389
390CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700391 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800392 const VkAllocationCallbacks& allocator)
393 : is_instance_(true),
394 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000395 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700396 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800397 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800398 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700399 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800400
Chia-I Wu4901db72016-03-24 16:38:58 +0800401CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
402 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700403 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800404 const VkAllocationCallbacks& allocator)
405 : is_instance_(false),
406 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000407 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700408 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800409 physical_dev_(physical_dev),
410 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700411 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800412
413CreateInfoWrapper::~CreateInfoWrapper() {
414 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
415 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
416}
417
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800418VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700419 VkResult result = SanitizeApiVersion();
420 if (result == VK_SUCCESS)
421 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800422 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800423 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800424 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800425 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800426
427 return result;
428}
429
430const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800431CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800432 return hook_extensions_;
433}
434
435const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800436CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800437 return hal_extensions_;
438}
439
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800440CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
441 return &instance_info_;
442}
443
Chia-I Wu4901db72016-03-24 16:38:58 +0800444CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
445 return &dev_info_;
446}
447
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700448VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700449 if (!is_instance_ || !instance_info_.pApplicationInfo)
450 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700451
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700452 if (icd_api_version_ > VK_API_VERSION_1_0 ||
453 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
454 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700455
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700456 // override apiVersion to avoid error return from 1.0 icd
457 application_info_ = *instance_info_.pApplicationInfo;
458 application_info_.apiVersion = VK_API_VERSION_1_0;
459 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700460
461 return VK_SUCCESS;
462}
463
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800464VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800465 const struct StructHeader {
466 VkStructureType type;
467 const void* next;
468 } * header;
469
470 if (is_instance_) {
471 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
472
473 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
474 while (header &&
475 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
476 header = reinterpret_cast<const StructHeader*>(header->next);
477
478 instance_info_.pNext = header;
479 } else {
480 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
481
482 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
483 while (header &&
484 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
485 header = reinterpret_cast<const StructHeader*>(header->next);
486
487 dev_info_.pNext = header;
488 }
489
490 return VK_SUCCESS;
491}
492
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800493VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800494 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
495 : dev_info_.ppEnabledLayerNames;
496 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
497 : dev_info_.enabledLayerCount;
498
499 // remove all layers
500 layer_names = nullptr;
501 layer_count = 0;
502
503 return VK_SUCCESS;
504}
505
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800506VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800507 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
508 : dev_info_.ppEnabledExtensionNames;
509 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
510 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800511
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800512 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800513 if (result != VK_SUCCESS)
514 return result;
515
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700516 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700517 for (uint32_t i = 0; i < ext_count; i++) {
518 // Upon api downgrade, skip the promoted instance extensions in the
519 // first pass to avoid duplicate extensions.
520 const std::optional<uint32_t> version =
521 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700522 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700523 *version <= loader_api_version_)
524 continue;
525
526 FilterExtension(ext_names[i]);
527 }
528
529 // Enable the required extensions to support core functionalities.
530 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700531 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700532 for (const auto& promoted_extension : promoted_extensions)
533 FilterExtension(promoted_extension);
534 } else {
535 for (uint32_t i = 0; i < ext_count; i++)
536 FilterExtension(ext_names[i]);
537 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800538
Jesse Halld3d887a2018-03-05 13:34:45 -0800539 // Enable device extensions that contain physical-device commands, so that
540 // vkGetInstanceProcAddr will return those physical-device commands.
541 if (is_instance_) {
542 hook_extensions_.set(ProcHook::KHR_swapchain);
543 }
544
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700545 const uint32_t api_version =
546 is_instance_ ? loader_api_version_
547 : std::min(icd_api_version_, loader_api_version_);
548 switch (api_version) {
Trevor David Black628c41a2021-09-27 05:07:22 +0000549 case VK_API_VERSION_1_3:
550 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
551 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
552 [[clang::fallthrough]];
Ian Elliottfa7af492021-07-20 17:40:24 -0600553 case VK_API_VERSION_1_2:
554 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
555 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
556 [[clang::fallthrough]];
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700557 case VK_API_VERSION_1_1:
558 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
559 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
560 [[clang::fallthrough]];
561 case VK_API_VERSION_1_0:
562 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
563 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
564 break;
565 default:
566 ALOGE("Unknown API version[%u]", api_version);
567 break;
568 }
569
Chia-I Wu4901db72016-03-24 16:38:58 +0800570 ext_names = extension_filter_.names;
571 ext_count = extension_filter_.name_count;
572
573 return VK_SUCCESS;
574}
575
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800576VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800577 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800578 return Hal::Device().EnumerateInstanceExtensionProperties(
579 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800580 } else {
581 const auto& driver = GetData(physical_dev_).driver;
582 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
583 &count, nullptr);
584 }
585}
586
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800587VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800588 uint32_t& count,
589 VkExtensionProperties* props) const {
590 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800591 return Hal::Device().EnumerateInstanceExtensionProperties(
592 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800593 } else {
594 const auto& driver = GetData(physical_dev_).driver;
595 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
596 &count, props);
597 }
598}
599
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800600VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800601 // query extension count
602 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800603 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800604 if (result != VK_SUCCESS || count == 0)
605 return result;
606
607 auto& filter = extension_filter_;
608 filter.exts =
609 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
610 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
611 alignof(VkExtensionProperties),
612 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
613 if (!filter.exts)
614 return VK_ERROR_OUT_OF_HOST_MEMORY;
615
616 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800617 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800618 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
619 return result;
620
621 if (!count)
622 return VK_SUCCESS;
623
624 filter.ext_count = count;
625
626 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700627 if (is_instance_) {
628 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
629
630 // It requires enabling additional promoted extensions to downgrade api,
631 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700632 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700633 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700634 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700635 }
636
637 count = std::min(filter.ext_count, enabled_ext_count);
638 } else {
639 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
640 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700641
642 if (!count)
643 return VK_SUCCESS;
644
Chia-I Wu4901db72016-03-24 16:38:58 +0800645 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
646 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
647 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
648 if (!filter.names)
649 return VK_ERROR_OUT_OF_HOST_MEMORY;
650
651 return VK_SUCCESS;
652}
653
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800654void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800655 auto& filter = extension_filter_;
656
657 ProcHook::Extension ext_bit = GetProcHookExtension(name);
658 if (is_instance_) {
659 switch (ext_bit) {
660 case ProcHook::KHR_android_surface:
661 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600662 case ProcHook::KHR_surface_protected_capabilities:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700663 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300664 case ProcHook::KHR_get_surface_capabilities2:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600665 case ProcHook::GOOGLE_surfaceless_query:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000666 case ProcHook::EXT_surface_maintenance1:
Chia-I Wu4901db72016-03-24 16:38:58 +0800667 hook_extensions_.set(ext_bit);
668 // return now as these extensions do not require HAL support
669 return;
670 case ProcHook::EXT_debug_report:
671 // both we and HAL can take part in
672 hook_extensions_.set(ext_bit);
673 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300674 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700675 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700676 case ProcHook::KHR_external_memory_capabilities:
677 case ProcHook::KHR_external_semaphore_capabilities:
678 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700679 case ProcHook::EXTENSION_UNKNOWN:
680 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800681 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700682
Yiwei Zhang23143102019-04-10 18:24:05 -0700683 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700684 case ProcHook::KHR_incremental_present:
685 case ProcHook::KHR_shared_presentable_image:
686 case ProcHook::KHR_swapchain:
687 case ProcHook::EXT_hdr_metadata:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000688 case ProcHook::EXT_swapchain_maintenance1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700689 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
690 case ProcHook::ANDROID_native_buffer:
691 case ProcHook::GOOGLE_display_timing:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000692 case ProcHook::KHR_external_fence_fd:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700693 case ProcHook::EXTENSION_CORE_1_0:
694 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700695 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000696 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700697 case ProcHook::EXTENSION_COUNT:
698 // Device and meta extensions. If we ever get here it's a bug in
699 // our code. But enumerating them lets us avoid having a default
700 // case, and default hides other bugs.
701 ALOGE(
702 "CreateInfoWrapper::FilterExtension: invalid instance "
703 "extension '%s'. FIX ME",
704 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800705 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700706
707 // Don't use a default case. Without it, -Wswitch will tell us
708 // at compile time if someone adds a new ProcHook extension but
709 // doesn't handle it above. That's a real bug that has
710 // not-immediately-obvious effects.
711 //
712 // default:
713 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800714 }
715 } else {
716 switch (ext_bit) {
717 case ProcHook::KHR_swapchain:
718 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
719 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
720 ext_bit = ProcHook::ANDROID_native_buffer;
721 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700722 case ProcHook::KHR_incremental_present:
Chris Forbesfa25e632017-02-22 12:36:02 +1300723 case ProcHook::KHR_shared_presentable_image:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000724 case ProcHook::GOOGLE_display_timing:
Ian Elliott8a977262017-01-19 09:05:58 -0700725 hook_extensions_.set(ext_bit);
726 // return now as these extensions do not require HAL support
727 return;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000728 case ProcHook::EXT_swapchain_maintenance1:
729 // map VK_KHR_swapchain_maintenance1 to KHR_external_fence_fd
730 name = VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME;
731 ext_bit = ProcHook::KHR_external_fence_fd;
732 break;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700733 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700734 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700735 hook_extensions_.set(ext_bit);
736 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700737 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000738 case ProcHook::KHR_external_fence_fd:
Chia-I Wu4901db72016-03-24 16:38:58 +0800739 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700740 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800741 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700742
743 case ProcHook::KHR_android_surface:
744 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700745 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700746 case ProcHook::KHR_external_memory_capabilities:
747 case ProcHook::KHR_external_semaphore_capabilities:
748 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700749 case ProcHook::KHR_get_surface_capabilities2:
750 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600751 case ProcHook::KHR_surface_protected_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700752 case ProcHook::EXT_debug_report:
753 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000754 case ProcHook::EXT_surface_maintenance1:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600755 case ProcHook::GOOGLE_surfaceless_query:
Jesse Hall7f983a82018-03-29 14:46:45 -0700756 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700757 case ProcHook::EXTENSION_CORE_1_0:
758 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700759 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000760 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700761 case ProcHook::EXTENSION_COUNT:
762 // Instance and meta extensions. If we ever get here it's a bug
763 // in our code. But enumerating them lets us avoid having a
764 // default case, and default hides other bugs.
765 ALOGE(
766 "CreateInfoWrapper::FilterExtension: invalid device "
767 "extension '%s'. FIX ME",
768 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800769 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700770
771 // Don't use a default case. Without it, -Wswitch will tell us
772 // at compile time if someone adds a new ProcHook extension but
773 // doesn't handle it above. That's a real bug that has
774 // not-immediately-obvious effects.
775 //
776 // default:
777 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800778 }
779 }
780
781 for (uint32_t i = 0; i < filter.ext_count; i++) {
782 const VkExtensionProperties& props = filter.exts[i];
783 // ignore unknown extensions
784 if (strcmp(name, props.extensionName) != 0)
785 continue;
786
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000787 if (ext_bit != ProcHook::EXTENSION_UNKNOWN &&
788 hal_extensions_.test(ext_bit)) {
789 ALOGI("CreateInfoWrapper::FilterExtension: already have '%s'.", name);
790 continue;
791 }
792
Ian Elliott5b527a02023-08-10 13:32:55 -0600793 // Ignore duplicate extensions (see: b/288929054)
794 bool duplicate_entry = false;
795 for (uint32_t j = 0; j < filter.name_count; j++) {
796 if (strcmp(name, filter.names[j]) == 0) {
797 duplicate_entry = true;
798 break;
799 }
800 }
801 if (duplicate_entry == true)
802 continue;
803
Chia-I Wu4901db72016-03-24 16:38:58 +0800804 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800805 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
806 if (ext_bit == ProcHook::ANDROID_native_buffer)
807 hook_extensions_.set(ProcHook::KHR_swapchain);
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000808 if (ext_bit == ProcHook::KHR_external_fence_fd)
809 hook_extensions_.set(ProcHook::EXT_swapchain_maintenance1);
Chia-I Wu1600e262016-04-12 09:40:06 +0800810
811 hal_extensions_.set(ext_bit);
812 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800813
814 break;
815 }
816}
817
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800818VKAPI_ATTR void* DefaultAllocate(void*,
819 size_t size,
820 size_t alignment,
821 VkSystemAllocationScope) {
822 void* ptr = nullptr;
823 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
824 // additionally requires that it be at least sizeof(void*).
825 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
826 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
827 ret, ptr);
828 return ret == 0 ? ptr : nullptr;
829}
830
831VKAPI_ATTR void* DefaultReallocate(void*,
832 void* ptr,
833 size_t size,
834 size_t alignment,
835 VkSystemAllocationScope) {
836 if (size == 0) {
837 free(ptr);
838 return nullptr;
839 }
840
Yiwei Zhanga885c062019-10-24 12:07:57 -0700841 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800842 // request is smaller than the existing chunk, we just continue using it.
843 // Right now the loader never reallocs, so this doesn't matter. If that
844 // changes, or if this code is copied into some other project, this should
845 // probably have a heuristic to allocate-copy-free when doing so will save
846 // "enough" space.
847 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
848 if (size <= old_size)
849 return ptr;
850
851 void* new_ptr = nullptr;
852 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
853 return nullptr;
854 if (ptr) {
855 memcpy(new_ptr, ptr, std::min(old_size, size));
856 free(ptr);
857 }
858 return new_ptr;
859}
860
861VKAPI_ATTR void DefaultFree(void*, void* ptr) {
862 ALOGD_CALLSTACK("Free: %p", ptr);
863 free(ptr);
864}
865
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800866InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
867 void* data_mem = allocator.pfnAllocation(
868 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
869 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
870 if (!data_mem)
871 return nullptr;
872
873 return new (data_mem) InstanceData(allocator);
874}
875
876void FreeInstanceData(InstanceData* data,
877 const VkAllocationCallbacks& allocator) {
878 data->~InstanceData();
879 allocator.pfnFree(allocator.pUserData, data);
880}
881
Chia-I Wu950d6e12016-05-03 09:12:35 +0800882DeviceData* AllocateDeviceData(
883 const VkAllocationCallbacks& allocator,
884 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800885 void* data_mem = allocator.pfnAllocation(
886 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
887 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
888 if (!data_mem)
889 return nullptr;
890
Chia-I Wu950d6e12016-05-03 09:12:35 +0800891 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800892}
893
894void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
895 data->~DeviceData();
896 allocator.pfnFree(allocator.pUserData, data);
897}
898
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800899} // anonymous namespace
900
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800901bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800902 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800903}
904
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800905const VkAllocationCallbacks& GetDefaultAllocator() {
906 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
907 .pUserData = nullptr,
908 .pfnAllocation = DefaultAllocate,
909 .pfnReallocation = DefaultReallocate,
910 .pfnFree = DefaultFree,
911 };
912
913 return kDefaultAllocCallbacks;
914}
915
Chia-I Wueb7db122016-03-24 09:11:06 +0800916PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
917 const ProcHook* hook = GetProcHook(pName);
918 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800919 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800920
921 if (!instance) {
922 if (hook->type == ProcHook::GLOBAL)
923 return hook->proc;
924
Chia-I Wu109f8982016-04-22 06:40:40 +0800925 // v0 layers expect
926 //
927 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
928 //
929 // to work.
930 if (strcmp(pName, "vkCreateDevice") == 0)
931 return hook->proc;
932
Chia-I Wueb7db122016-03-24 09:11:06 +0800933 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800934 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800935 pName);
936
Chia-I Wu109f8982016-04-22 06:40:40 +0800937 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800938 }
939
940 PFN_vkVoidFunction proc;
941
942 switch (hook->type) {
943 case ProcHook::INSTANCE:
944 proc = (GetData(instance).hook_extensions[hook->extension])
945 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800946 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800947 break;
948 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700949 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800950 ? hook->proc
951 : hook->checked_proc;
952 break;
953 default:
954 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800955 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800956 pName);
957 proc = nullptr;
958 break;
959 }
960
961 return proc;
962}
963
964PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
965 const ProcHook* hook = GetProcHook(pName);
966 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800967 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800968
969 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800970 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800971 return nullptr;
972 }
973
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800974 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
975 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800976}
977
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800978VkResult EnumerateInstanceExtensionProperties(
979 const char* pLayerName,
980 uint32_t* pPropertyCount,
981 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700982 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700983 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600984 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600985 loader_extensions.push_back(
986 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
987 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600988 loader_extensions.push_back({
989 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
990 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
991 loader_extensions.push_back({
992 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
993 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -0600994 loader_extensions.push_back(
995 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
996 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -0600997 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
998 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000999 loader_extensions.push_back({
1000 VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
1001 VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -06001002
Chia-I Wu31938252016-05-23 15:31:02 +08001003 static const VkExtensionProperties loader_debug_report_extension = {
1004 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
1005 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001006
1007 // enumerate our extensions first
1008 if (!pLayerName && pProperties) {
1009 uint32_t count = std::min(
1010 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1011
Yiwei Zhang5e862202019-06-21 14:59:16 -07001012 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001013
1014 if (count < loader_extensions.size()) {
1015 *pPropertyCount = count;
1016 return VK_INCOMPLETE;
1017 }
1018
1019 pProperties += count;
1020 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +08001021
1022 if (Hal::Get().GetDebugReportIndex() < 0) {
1023 if (!*pPropertyCount) {
1024 *pPropertyCount = count;
1025 return VK_INCOMPLETE;
1026 }
1027
1028 pProperties[0] = loader_debug_report_extension;
1029 pProperties += 1;
1030 *pPropertyCount -= 1;
1031 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001032 }
1033
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001034 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001035 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001036 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001037 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001038
Chia-I Wu31938252016-05-23 15:31:02 +08001039 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1040 int idx = Hal::Get().GetDebugReportIndex();
1041 if (idx < 0) {
1042 *pPropertyCount += 1;
1043 } else if (pProperties &&
1044 static_cast<uint32_t>(idx) < *pPropertyCount) {
1045 pProperties[idx].specVersion =
1046 std::min(pProperties[idx].specVersion,
1047 loader_debug_report_extension.specVersion);
1048 }
1049
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001050 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +08001051 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001052
1053 return result;
1054}
1055
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001056void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001057 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001058 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001059 ATRACE_CALL();
1060
Chris Forbesfa25e632017-02-22 12:36:02 +13001061 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001062 VkPhysicalDeviceProperties2 properties = {
1063 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001064 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001065 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001066 };
1067
1068#pragma clang diagnostic push
1069#pragma clang diagnostic ignored "-Wold-style-cast"
1070 presentation_properties->sType =
1071 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1072#pragma clang diagnostic pop
1073 presentation_properties->pNext = nullptr;
1074 presentation_properties->sharedImage = VK_FALSE;
1075
Chris Forbese056c122021-07-22 13:54:04 -07001076 const auto& driver = GetData(physicalDevice).driver;
1077
1078 if (driver.GetPhysicalDeviceProperties2) {
1079 // >= 1.1 driver, supports core GPDP2 entrypoint.
1080 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1081 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1082 // Old driver, but may support presentation properties
1083 // if we have the GPDP2 extension. Otherwise, no presentation
1084 // properties supported.
1085 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1086 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001087}
1088
Trevor David Black929e9cd2022-11-22 04:12:19 +00001089VkResult GetAndroidNativeBufferSpecVersion9Support(
1090 VkPhysicalDevice physicalDevice,
1091 bool& support) {
1092 support = false;
1093
Trevor David Black2cc44682022-03-09 00:31:38 +00001094 const InstanceData& data = GetData(physicalDevice);
1095
1096 // Call to get propertyCount
1097 uint32_t propertyCount = 0;
1098 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1099 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1100 physicalDevice, nullptr, &propertyCount, nullptr);
1101 ATRACE_END();
1102
Trevor David Black929e9cd2022-11-22 04:12:19 +00001103 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1104 return result;
1105 }
1106
Trevor David Black2cc44682022-03-09 00:31:38 +00001107 // Call to enumerate properties
1108 std::vector<VkExtensionProperties> properties(propertyCount);
1109 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1110 result = data.driver.EnumerateDeviceExtensionProperties(
1111 physicalDevice, nullptr, &propertyCount, properties.data());
1112 ATRACE_END();
1113
Trevor David Black929e9cd2022-11-22 04:12:19 +00001114 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1115 return result;
1116 }
1117
Trevor David Black2cc44682022-03-09 00:31:38 +00001118 for (uint32_t i = 0; i < propertyCount; i++) {
1119 auto& prop = properties[i];
1120
1121 if (strcmp(prop.extensionName,
1122 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1123 continue;
1124
1125 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001126 support = true;
1127 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001128 }
1129 }
1130
Trevor David Black929e9cd2022-11-22 04:12:19 +00001131 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001132}
1133
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001134bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1135 const auto& driver = GetData(physicalDevice).driver;
1136 if (!driver.GetPhysicalDeviceExternalFenceProperties)
1137 return false;
1138
1139 // Requires support for external fences imported from sync fds.
1140 // This is _almost_ universal on Android, but may be missing on
1141 // some extremely old drivers, or on strange implementations like
1142 // cuttlefish.
1143 VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1144 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1145 nullptr,
1146 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1147 };
1148 VkExternalFenceProperties fenceProperties = {
1149 VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1150 nullptr,
1151 0, 0, 0
1152 };
1153
1154 GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1155 if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1156 return true;
1157
1158 return false;
1159}
1160
Chia-I Wu01cf3052016-03-24 16:16:21 +08001161VkResult EnumerateDeviceExtensionProperties(
1162 VkPhysicalDevice physicalDevice,
1163 const char* pLayerName,
1164 uint32_t* pPropertyCount,
1165 VkExtensionProperties* pProperties) {
1166 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001167 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001168 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001169 loader_extensions.push_back({
1170 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1171 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001172
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001173 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001174 if (hdrBoardConfig) {
1175 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1176 VK_EXT_HDR_METADATA_SPEC_VERSION});
1177 }
1178
Chris Forbes16095002017-05-05 15:33:29 -07001179 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001180 QueryPresentationProperties(physicalDevice, &presentation_properties);
1181 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001182 loader_extensions.push_back({
1183 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1184 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001185 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001186
Ian Elliott5c34de22017-04-10 14:42:30 -06001187 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1188 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001189 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001190 loader_extensions.push_back({
1191 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1192 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1193 }
1194
Trevor David Black2cc44682022-03-09 00:31:38 +00001195 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1196 // support is provided by the driver
1197 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1198 swapchainCompFeats = {};
1199 swapchainCompFeats.sType =
1200 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1201 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001202 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001203 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1204 imageCompFeats.sType =
1205 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1206 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001207 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001208
1209 VkPhysicalDeviceFeatures2 feats2 = {};
1210 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1211 feats2.pNext = &imageCompFeats;
1212
Trevor David Black929e9cd2022-11-22 04:12:19 +00001213 const auto& driver = GetData(physicalDevice).driver;
1214 if (driver.GetPhysicalDeviceFeatures2 ||
1215 driver.GetPhysicalDeviceFeatures2KHR) {
1216 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1217 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001218
Trevor David Black929e9cd2022-11-22 04:12:19 +00001219 bool anb9 = false;
1220 VkResult result =
1221 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1222
1223 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1224 return result;
1225 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001226
1227 if (anb9 && imageCompFeats.imageCompressionControl) {
1228 loader_extensions.push_back(
1229 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1230 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1231 }
1232 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1233 loader_extensions.push_back(
1234 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1235 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1236 }
1237
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001238 if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1239 loader_extensions.push_back({
1240 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1241 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1242 }
1243
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001244 // enumerate our extensions first
1245 if (!pLayerName && pProperties) {
1246 uint32_t count = std::min(
1247 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1248
Yiwei Zhang5e862202019-06-21 14:59:16 -07001249 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001250
1251 if (count < loader_extensions.size()) {
1252 *pPropertyCount = count;
1253 return VK_INCOMPLETE;
1254 }
1255
1256 pProperties += count;
1257 *pPropertyCount -= count;
1258 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001259
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001260 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001261 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001262 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001263 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001264
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001265 if (pProperties) {
1266 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1267 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1268 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001269
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001270 if (strcmp(prop.extensionName,
1271 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1272 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001273
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001274 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1275 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001276
1277 if (prop.specVersion >= 8) {
1278 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1279 } else {
1280 prop.specVersion = 68;
1281 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001282 }
1283 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001284
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001285 // restore loader extension count
1286 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1287 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001288 }
1289
1290 return result;
1291}
1292
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001293VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1294 const VkAllocationCallbacks* pAllocator,
1295 VkInstance* pInstance) {
1296 const VkAllocationCallbacks& data_allocator =
1297 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1298
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001299 VkResult result = VK_SUCCESS;
1300 uint32_t icd_api_version = VK_API_VERSION_1_0;
1301 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1302 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1303 Hal::Device().GetInstanceProcAddr(nullptr,
1304 "vkEnumerateInstanceVersion"));
1305 if (pfn_enumerate_instance_version) {
1306 ATRACE_BEGIN("pfn_enumerate_instance_version");
1307 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1308 ATRACE_END();
1309 if (result != VK_SUCCESS)
1310 return result;
1311
Trevor David Blackb68a2252021-08-23 16:37:18 +00001312 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001313 }
1314
1315 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1316 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001317 if (result != VK_SUCCESS)
1318 return result;
1319
1320 InstanceData* data = AllocateInstanceData(data_allocator);
1321 if (!data)
1322 return VK_ERROR_OUT_OF_HOST_MEMORY;
1323
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001324 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001325
1326 // call into the driver
1327 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001328 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001329 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001330 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1331 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001332 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001333 if (result != VK_SUCCESS) {
1334 FreeInstanceData(data, data_allocator);
1335 return result;
1336 }
1337
1338 // initialize InstanceDriverTable
1339 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001340 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001341 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001342 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001343 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001344 if (data->driver.DestroyInstance)
1345 data->driver.DestroyInstance(instance, pAllocator);
1346
1347 FreeInstanceData(data, data_allocator);
1348
1349 return VK_ERROR_INCOMPATIBLE_DRIVER;
1350 }
1351
1352 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001353 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001354 if (!data->get_device_proc_addr) {
1355 data->driver.DestroyInstance(instance, pAllocator);
1356 FreeInstanceData(data, data_allocator);
1357
1358 return VK_ERROR_INCOMPATIBLE_DRIVER;
1359 }
1360
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001361 // TODO(b/259516419) avoid getting stats from hwui
1362 // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1363 // || (strcmp("android framework",
1364 // pCreateInfo->pApplicationInfo->pEngineName) != 0);
1365 const bool reportStats = true;
1366 if (reportStats) {
1367 // Set stats for Vulkan api version requested with application info
1368 if (pCreateInfo->pApplicationInfo) {
1369 const uint32_t vulkanApiVersion =
1370 pCreateInfo->pApplicationInfo->apiVersion;
1371 android::GraphicsEnv::getInstance().setTargetStats(
1372 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1373 vulkanApiVersion);
1374 }
1375
1376 // Update stats for the extensions requested
1377 android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1378 pCreateInfo->enabledExtensionCount,
1379 pCreateInfo->ppEnabledExtensionNames);
1380 }
1381
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001382 *pInstance = instance;
1383
1384 return VK_SUCCESS;
1385}
1386
1387void DestroyInstance(VkInstance instance,
1388 const VkAllocationCallbacks* pAllocator) {
1389 InstanceData& data = GetData(instance);
1390 data.driver.DestroyInstance(instance, pAllocator);
1391
1392 VkAllocationCallbacks local_allocator;
1393 if (!pAllocator) {
1394 local_allocator = data.allocator;
1395 pAllocator = &local_allocator;
1396 }
1397
1398 FreeInstanceData(&data, *pAllocator);
1399}
1400
Chia-I Wu4901db72016-03-24 16:38:58 +08001401VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1402 const VkDeviceCreateInfo* pCreateInfo,
1403 const VkAllocationCallbacks* pAllocator,
1404 VkDevice* pDevice) {
1405 const InstanceData& instance_data = GetData(physicalDevice);
1406 const VkAllocationCallbacks& data_allocator =
1407 (pAllocator) ? *pAllocator : instance_data.allocator;
1408
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001409 VkPhysicalDeviceProperties properties;
1410 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1411 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1412 &properties);
1413 ATRACE_END();
1414
1415 CreateInfoWrapper wrapper(
1416 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001417 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001418 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001419 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001420 if (result != VK_SUCCESS)
1421 return result;
1422
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001423 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001424 DeviceData* data = AllocateDeviceData(data_allocator,
1425 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001426 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001427 if (!data)
1428 return VK_ERROR_OUT_OF_HOST_MEMORY;
1429
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001430 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001431
1432 // call into the driver
1433 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001434 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001435 result = instance_data.driver.CreateDevice(
1436 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1437 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001438 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001439 if (result != VK_SUCCESS) {
1440 FreeDeviceData(data, data_allocator);
1441 return result;
1442 }
1443
1444 // initialize DeviceDriverTable
1445 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001446 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1447 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001448 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1449 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1450 if (data->driver.DestroyDevice)
1451 data->driver.DestroyDevice(dev, pAllocator);
1452
1453 FreeDeviceData(data, data_allocator);
1454
1455 return VK_ERROR_INCOMPATIBLE_DRIVER;
1456 }
Chris Forbesd8277912017-02-10 14:59:59 +13001457
Trevor David Black2cc44682022-03-09 00:31:38 +00001458 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001459 // entrypoints varies according to the spec version.
1460 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1461 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001462 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
1463 !data->driver.GetSwapchainGrallocUsage3ANDROID) {
1464 ALOGE(
1465 "Driver's implementation of ANDROID_native_buffer is broken;"
1466 " must expose at least one of "
1467 "vkGetSwapchainGrallocUsageANDROID or "
1468 "vkGetSwapchainGrallocUsage2ANDROID or "
1469 "vkGetSwapchainGrallocUsage3ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001470
1471 data->driver.DestroyDevice(dev, pAllocator);
1472 FreeDeviceData(data, data_allocator);
1473
1474 return VK_ERROR_INCOMPATIBLE_DRIVER;
1475 }
1476
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001477 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1478 // Log that the app is hitting software Vulkan implementation
1479 android::GraphicsEnv::getInstance().setTargetStats(
1480 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1481 }
1482
Jesse Halldc225072016-05-30 22:40:14 -07001483 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001484
1485 *pDevice = dev;
1486
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001487 // TODO(b/259516419) avoid getting stats from hwui
1488 const bool reportStats = true;
1489 if (reportStats) {
1490 android::GraphicsEnv::getInstance().setTargetStats(
1491 android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1492
1493 // Set stats for creating a Vulkan device and report features in use
1494 const VkPhysicalDeviceFeatures* pEnabledFeatures =
1495 pCreateInfo->pEnabledFeatures;
1496 if (!pEnabledFeatures) {
1497 // Use features from the chained VkPhysicalDeviceFeatures2
1498 // structure, if given
1499 const VkPhysicalDeviceFeatures2* features2 =
1500 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1501 pCreateInfo->pNext);
1502 while (features2 &&
1503 features2->sType !=
1504 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1505 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1506 features2->pNext);
1507 }
1508 if (features2) {
1509 pEnabledFeatures = &features2->features;
1510 }
1511 }
1512 const VkBool32* pFeatures =
1513 reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1514 if (pFeatures) {
1515 // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1516 // of them using pointer arithmetic here and save the features in a
1517 // 64-bit bitfield
1518 static_assert(
1519 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1520 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1521 "packing");
1522 static_assert(
1523 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1524 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1525 "packing");
1526 const int numFeatures =
1527 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1528
1529 uint64_t enableFeatureBits = 0;
1530 for (int i = 0; i < numFeatures; i++) {
1531 if (pFeatures[i] != VK_FALSE) {
1532 enableFeatureBits |= (uint64_t(1) << i);
1533 }
1534 }
1535 android::GraphicsEnv::getInstance().setTargetStats(
1536 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1537 enableFeatureBits);
1538 }
1539
1540 // Update stats for the extensions requested
1541 android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1542 pCreateInfo->enabledExtensionCount,
1543 pCreateInfo->ppEnabledExtensionNames);
1544 }
1545
Chia-I Wu4901db72016-03-24 16:38:58 +08001546 return VK_SUCCESS;
1547}
1548
1549void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1550 DeviceData& data = GetData(device);
1551 data.driver.DestroyDevice(device, pAllocator);
1552
1553 VkAllocationCallbacks local_allocator;
1554 if (!pAllocator) {
1555 local_allocator = data.allocator;
1556 pAllocator = &local_allocator;
1557 }
1558
1559 FreeDeviceData(&data, *pAllocator);
1560}
1561
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001562VkResult EnumeratePhysicalDevices(VkInstance instance,
1563 uint32_t* pPhysicalDeviceCount,
1564 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001565 ATRACE_CALL();
1566
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001567 const auto& data = GetData(instance);
1568
1569 VkResult result = data.driver.EnumeratePhysicalDevices(
1570 instance, pPhysicalDeviceCount, pPhysicalDevices);
1571 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1572 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1573 SetData(pPhysicalDevices[i], data);
1574 }
1575
1576 return result;
1577}
1578
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001579VkResult EnumeratePhysicalDeviceGroups(
1580 VkInstance instance,
1581 uint32_t* pPhysicalDeviceGroupCount,
1582 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001583 ATRACE_CALL();
1584
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001585 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001586 const auto& data = GetData(instance);
1587
Yiwei Zhange4f64172020-07-05 15:17:32 -07001588 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1589 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001590 uint32_t device_count = 0;
1591 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1592 if (result < 0)
1593 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001594
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001595 if (!pPhysicalDeviceGroupProperties) {
1596 *pPhysicalDeviceGroupCount = device_count;
1597 return result;
1598 }
1599
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001600 if (!device_count) {
1601 *pPhysicalDeviceGroupCount = 0;
1602 return result;
1603 }
Chad Versace32c087f2018-09-09 07:28:05 -07001604 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1605 if (!device_count)
1606 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001607
Yiwei Zhang5e862202019-06-21 14:59:16 -07001608 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001609 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001610 result =
1611 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001612 if (result < 0)
1613 return result;
1614
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001615 for (uint32_t i = 0; i < device_count; ++i) {
1616 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1617 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1618 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1619 }
1620 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001621 if (data.driver.EnumeratePhysicalDeviceGroups) {
1622 result = data.driver.EnumeratePhysicalDeviceGroups(
1623 instance, pPhysicalDeviceGroupCount,
1624 pPhysicalDeviceGroupProperties);
1625 } else {
1626 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1627 instance, pPhysicalDeviceGroupCount,
1628 pPhysicalDeviceGroupProperties);
1629 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001630 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1631 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1632 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1633 for (uint32_t j = 0;
1634 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1635 j++) {
1636 SetData(
1637 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001638 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001639 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001640 }
1641 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001642 }
1643
1644 return result;
1645}
1646
Chia-I Wuba0be412016-03-24 16:24:40 +08001647void GetDeviceQueue(VkDevice device,
1648 uint32_t queueFamilyIndex,
1649 uint32_t queueIndex,
1650 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001651 ATRACE_CALL();
1652
Chia-I Wuba0be412016-03-24 16:24:40 +08001653 const auto& data = GetData(device);
1654
1655 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1656 SetData(*pQueue, data);
1657}
1658
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001659void GetDeviceQueue2(VkDevice device,
1660 const VkDeviceQueueInfo2* pQueueInfo,
1661 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001662 ATRACE_CALL();
1663
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001664 const auto& data = GetData(device);
1665
1666 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001667 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001668}
1669
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001670VkResult AllocateCommandBuffers(
1671 VkDevice device,
1672 const VkCommandBufferAllocateInfo* pAllocateInfo,
1673 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001674 ATRACE_CALL();
1675
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001676 const auto& data = GetData(device);
1677
1678 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1679 pCommandBuffers);
1680 if (result == VK_SUCCESS) {
1681 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1682 SetData(pCommandBuffers[i], data);
1683 }
1684
1685 return result;
1686}
1687
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001688VkResult QueueSubmit(VkQueue queue,
1689 uint32_t submitCount,
1690 const VkSubmitInfo* pSubmits,
1691 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001692 ATRACE_CALL();
1693
1694 const auto& data = GetData(queue);
1695
1696 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1697}
1698
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001699void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1700 VkPhysicalDeviceFeatures2* pFeatures) {
1701 ATRACE_CALL();
1702
1703 const auto& driver = GetData(physicalDevice).driver;
1704
1705 if (driver.GetPhysicalDeviceFeatures2) {
1706 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001707 } else {
1708 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1709 }
1710
1711 // Conditionally add imageCompressionControlSwapchain if
1712 // imageCompressionControl is supported Check for imageCompressionControl in
1713 // the pChain
1714 bool imageCompressionControl = false;
1715 bool imageCompressionControlInChain = false;
1716 bool imageCompressionControlSwapchainInChain = false;
1717 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1718 while (pFeats) {
1719 switch (pFeats->sType) {
1720 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1721 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1722 compressionFeat = reinterpret_cast<
1723 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1724 pFeats);
1725 imageCompressionControl =
1726 compressionFeat->imageCompressionControl;
1727 imageCompressionControlInChain = true;
1728 } break;
1729
1730 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001731 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1732 compressionFeat = reinterpret_cast<
1733 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1734 pFeats);
1735 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001736 imageCompressionControlSwapchainInChain = true;
1737 } break;
1738
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001739 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1740 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1741 pFeats);
1742 smf->swapchainMaintenance1 = true;
1743 } break;
1744
Trevor David Black2cc44682022-03-09 00:31:38 +00001745 default:
1746 break;
1747 }
1748 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1749 }
1750
1751 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001752 return;
1753 }
1754
Trevor David Black2cc44682022-03-09 00:31:38 +00001755 // If not in pchain, explicitly query for imageCompressionControl
1756 if (!imageCompressionControlInChain) {
1757 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1758 imageCompFeats.sType =
1759 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1760 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001761 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001762
1763 VkPhysicalDeviceFeatures2 feats2 = {};
1764 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1765 feats2.pNext = &imageCompFeats;
1766
1767 if (driver.GetPhysicalDeviceFeatures2) {
1768 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1769 } else {
1770 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1771 }
1772
1773 imageCompressionControl = imageCompFeats.imageCompressionControl;
1774 }
1775
1776 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1777 if (imageCompressionControl) {
1778 pFeats = pFeatures;
1779 while (pFeats) {
1780 switch (pFeats->sType) {
1781 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1782 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1783 compressionFeat = reinterpret_cast<
1784 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1785 pFeats);
1786 compressionFeat->imageCompressionControlSwapchain = true;
1787 } break;
1788
1789 default:
1790 break;
1791 }
1792 pFeats =
1793 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1794 }
1795 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001796}
1797
1798void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1799 VkPhysicalDeviceProperties2* pProperties) {
1800 ATRACE_CALL();
1801
1802 const auto& driver = GetData(physicalDevice).driver;
1803
1804 if (driver.GetPhysicalDeviceProperties2) {
1805 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1806 return;
1807 }
1808
1809 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1810}
1811
1812void GetPhysicalDeviceFormatProperties2(
1813 VkPhysicalDevice physicalDevice,
1814 VkFormat format,
1815 VkFormatProperties2* pFormatProperties) {
1816 ATRACE_CALL();
1817
1818 const auto& driver = GetData(physicalDevice).driver;
1819
1820 if (driver.GetPhysicalDeviceFormatProperties2) {
1821 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1822 pFormatProperties);
1823 return;
1824 }
1825
1826 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1827 pFormatProperties);
1828}
1829
1830VkResult GetPhysicalDeviceImageFormatProperties2(
1831 VkPhysicalDevice physicalDevice,
1832 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1833 VkImageFormatProperties2* pImageFormatProperties) {
1834 ATRACE_CALL();
1835
1836 const auto& driver = GetData(physicalDevice).driver;
1837
1838 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1839 return driver.GetPhysicalDeviceImageFormatProperties2(
1840 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1841 }
1842
1843 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1844 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1845}
1846
1847void GetPhysicalDeviceQueueFamilyProperties2(
1848 VkPhysicalDevice physicalDevice,
1849 uint32_t* pQueueFamilyPropertyCount,
1850 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1851 ATRACE_CALL();
1852
1853 const auto& driver = GetData(physicalDevice).driver;
1854
1855 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1856 driver.GetPhysicalDeviceQueueFamilyProperties2(
1857 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1858 return;
1859 }
1860
1861 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1862 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1863}
1864
1865void GetPhysicalDeviceMemoryProperties2(
1866 VkPhysicalDevice physicalDevice,
1867 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1868 ATRACE_CALL();
1869
1870 const auto& driver = GetData(physicalDevice).driver;
1871
1872 if (driver.GetPhysicalDeviceMemoryProperties2) {
1873 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1874 pMemoryProperties);
1875 return;
1876 }
1877
1878 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1879 pMemoryProperties);
1880}
1881
1882void GetPhysicalDeviceSparseImageFormatProperties2(
1883 VkPhysicalDevice physicalDevice,
1884 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1885 uint32_t* pPropertyCount,
1886 VkSparseImageFormatProperties2* pProperties) {
1887 ATRACE_CALL();
1888
1889 const auto& driver = GetData(physicalDevice).driver;
1890
1891 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1892 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1893 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1894 return;
1895 }
1896
1897 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1898 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1899}
1900
Yiwei Zhange1f35012020-07-05 22:52:04 -07001901void GetPhysicalDeviceExternalBufferProperties(
1902 VkPhysicalDevice physicalDevice,
1903 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1904 VkExternalBufferProperties* pExternalBufferProperties) {
1905 ATRACE_CALL();
1906
1907 const auto& driver = GetData(physicalDevice).driver;
1908
1909 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1910 driver.GetPhysicalDeviceExternalBufferProperties(
1911 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1912 return;
1913 }
1914
1915 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1916 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1917 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1918 return;
1919 }
1920
1921 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1922 sizeof(VkExternalMemoryProperties));
1923}
1924
1925void GetPhysicalDeviceExternalSemaphoreProperties(
1926 VkPhysicalDevice physicalDevice,
1927 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1928 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1929 ATRACE_CALL();
1930
1931 const auto& driver = GetData(physicalDevice).driver;
1932
1933 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1934 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1935 physicalDevice, pExternalSemaphoreInfo,
1936 pExternalSemaphoreProperties);
1937 return;
1938 }
1939
1940 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1941 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1942 physicalDevice, pExternalSemaphoreInfo,
1943 pExternalSemaphoreProperties);
1944 return;
1945 }
1946
1947 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1948 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1949 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1950}
1951
1952void GetPhysicalDeviceExternalFenceProperties(
1953 VkPhysicalDevice physicalDevice,
1954 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1955 VkExternalFenceProperties* pExternalFenceProperties) {
1956 ATRACE_CALL();
1957
1958 const auto& driver = GetData(physicalDevice).driver;
1959
1960 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1961 driver.GetPhysicalDeviceExternalFenceProperties(
1962 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1963 return;
1964 }
1965
1966 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1967 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1968 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1969 return;
1970 }
1971
1972 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1973 pExternalFenceProperties->compatibleHandleTypes = 0;
1974 pExternalFenceProperties->externalFenceFeatures = 0;
1975}
1976
Chia-I Wu9d518162016-03-24 14:55:27 +08001977} // namespace driver
1978} // namespace vulkan