blob: caa3020e64f528dc591ed0709f4208d3acacfbbd [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
Tom Murphyea321842024-06-14 18:26:58 +000044#include <com_android_graphics_libvulkan_flags.h>
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070045#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080046
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080047using namespace android::hardware::configstore;
48using namespace android::hardware::configstore::V1_0;
Tom Murphyea321842024-06-14 18:26:58 +000049using namespace com::android::graphics::libvulkan;
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080050
Jooyung Han749b0ec2023-11-15 13:38:06 +090051extern "C" android_namespace_t* android_get_exported_namespace(const char*);
52
Chia-I Wu9d518162016-03-24 14:55:27 +080053namespace vulkan {
54namespace driver {
55
Chia-I Wu136b8eb2016-03-24 15:01:52 +080056namespace {
57
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080058class Hal {
59 public:
60 static bool Open();
61
62 static const Hal& Get() { return hal_; }
63 static const hwvulkan_device_t& Device() { return *Get().dev_; }
64
Chia-I Wu31938252016-05-23 15:31:02 +080065 int GetDebugReportIndex() const { return debug_report_index_; }
66
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080067 private:
Chia-I Wu31938252016-05-23 15:31:02 +080068 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080069 Hal(const Hal&) = delete;
70 Hal& operator=(const Hal&) = delete;
71
Yiwei Zhang901f8ee2020-07-31 13:18:49 -070072 bool ShouldUnloadBuiltinDriver();
73 void UnloadBuiltinDriver();
Chia-I Wu31938252016-05-23 15:31:02 +080074 bool InitDebugReportIndex();
75
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080076 static Hal hal_;
77
78 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080079 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080080};
81
Chia-I Wu4901db72016-03-24 16:38:58 +080082class CreateInfoWrapper {
83 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080084 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -070085 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080086 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080087 CreateInfoWrapper(VkPhysicalDevice physical_dev,
88 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -070089 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +080090 const VkAllocationCallbacks& allocator);
91 ~CreateInfoWrapper();
92
Chia-I Wu3e6c2d62016-04-11 13:55:56 +080093 VkResult Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +080094
Chia-I Wu3e6c2d62016-04-11 13:55:56 +080095 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
96 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +080097
Chia-I Wuff4a6c72016-03-24 16:05:56 +080098 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +080099 explicit operator const VkDeviceCreateInfo*() const;
100
101 private:
102 struct ExtensionFilter {
103 VkExtensionProperties* exts;
104 uint32_t ext_count;
105
106 const char** names;
107 uint32_t name_count;
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700108 ExtensionFilter()
109 : exts(nullptr), ext_count(0), names(nullptr), name_count(0) {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800110 };
111
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700112 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800113 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800114 VkResult SanitizeLayers();
115 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800116
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800117 VkResult QueryExtensionCount(uint32_t& count) const;
118 VkResult EnumerateExtensions(uint32_t& count,
119 VkExtensionProperties* props) const;
120 VkResult InitExtensionFilter();
121 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800122
123 const bool is_instance_;
124 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700125 const uint32_t loader_api_version_;
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700126 const uint32_t icd_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800127
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800128 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800129
130 union {
131 VkInstanceCreateInfo instance_info_;
132 VkDeviceCreateInfo dev_info_;
133 };
134
Ian Elliottf3e872d2017-11-02 10:15:13 -0600135 VkApplicationInfo application_info_;
136
Chia-I Wu4901db72016-03-24 16:38:58 +0800137 ExtensionFilter extension_filter_;
138
139 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
140 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
141};
142
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800143Hal Hal::hal_;
144
Jesse Hall53457db2016-12-14 16:54:06 -0800145const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700146 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800147 "ro.board.platform",
148}};
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700149constexpr int LIB_DL_FLAGS = RTLD_LOCAL | RTLD_NOW;
Jooyung Han749b0ec2023-11-15 13:38:06 +0900150constexpr char RO_VULKAN_APEX_PROPERTY[] = "ro.vulkan.apex";
Jesse Hall53457db2016-12-14 16:54:06 -0800151
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800152// LoadDriver returns:
153// * 0 when succeed, or
154// * -ENOENT when fail to open binary libraries, or
155// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
156// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700157int LoadDriver(android_namespace_t* library_namespace,
Jooyung Han749b0ec2023-11-15 13:38:06 +0900158 const char* ns_name,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700159 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800160 ATRACE_CALL();
161
Jesse Hall53457db2016-12-14 16:54:06 -0800162 void* so = nullptr;
Jesse Hall53457db2016-12-14 16:54:06 -0800163 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700164 std::string lib_name = android::base::GetProperty(key, "");
165 if (lib_name.empty())
166 continue;
167
168 lib_name = "vulkan." + lib_name + ".so";
169 if (library_namespace) {
170 // load updated driver
171 const android_dlextinfo dlextinfo = {
172 .flags = ANDROID_DLEXT_USE_NAMESPACE,
173 .library_namespace = library_namespace,
174 };
175 so = android_dlopen_ext(lib_name.c_str(), LIB_DL_FLAGS, &dlextinfo);
Jooyung Han27e31082023-11-13 13:50:42 +0900176 if (!so) {
Jooyung Han749b0ec2023-11-15 13:38:06 +0900177 ALOGE("Could not load %s from %s namespace: %s.",
178 lib_name.c_str(), ns_name, dlerror());
Jooyung Han27e31082023-11-13 13:50:42 +0900179 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700180 } else {
181 // load built-in driver
182 so = android_load_sphal_library(lib_name.c_str(), LIB_DL_FLAGS);
Jesse Hall53457db2016-12-14 16:54:06 -0800183 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700184 if (so)
185 break;
Jesse Hall53457db2016-12-14 16:54:06 -0800186 }
187 if (!so)
188 return -ENOENT;
189
Jesse Hall00e61ff2017-04-07 16:48:02 -0700190 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800191 if (!hmi) {
192 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
193 dlclose(so);
194 return -EINVAL;
195 }
196 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
197 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
198 dlclose(so);
199 return -EINVAL;
200 }
201 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700202 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800203 return 0;
204}
205
Jooyung Han749b0ec2023-11-15 13:38:06 +0900206int LoadDriverFromApex(const hwvulkan_module_t** module) {
207 ATRACE_CALL();
208
209 auto apex_name = android::base::GetProperty(RO_VULKAN_APEX_PROPERTY, "");
210 if (apex_name == "") {
211 return -ENOENT;
212 }
213 // Get linker namespace for Vulkan APEX
214 std::replace(apex_name.begin(), apex_name.end(), '.', '_');
215 auto ns = android_get_exported_namespace(apex_name.c_str());
216 if (!ns) {
217 return -ENOENT;
218 }
219 android::GraphicsEnv::getInstance().setDriverToLoad(
220 android::GpuStatsInfo::Driver::VULKAN);
221 return LoadDriver(ns, apex_name.c_str(), module);
222}
223
Jesse Hall00e61ff2017-04-07 16:48:02 -0700224int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800225 ATRACE_CALL();
226
Yiwei Zhangd9861812019-02-13 11:51:55 -0800227 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700228 android::GpuStatsInfo::Driver::VULKAN);
Jooyung Han749b0ec2023-11-15 13:38:06 +0900229 return LoadDriver(nullptr, nullptr, module);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700230}
231
232int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800233 ATRACE_CALL();
234
Jesse Hall00e61ff2017-04-07 16:48:02 -0700235 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
236 if (!ns)
237 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800238 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700239 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Jooyung Han749b0ec2023-11-15 13:38:06 +0900240 int result = LoadDriver(ns, "updatable gfx driver", module);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800241 if (result != 0) {
242 LOG_ALWAYS_FATAL(
243 "couldn't find an updated Vulkan implementation from %s",
244 android::GraphicsEnv::getInstance().getDriverPath().c_str());
245 }
246 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700247}
248
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800249bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800250 ATRACE_CALL();
251
Yiwei Zhangd9861812019-02-13 11:51:55 -0800252 const nsecs_t openTime = systemTime();
253
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700254 if (hal_.ShouldUnloadBuiltinDriver()) {
255 hal_.UnloadBuiltinDriver();
256 }
257
258 if (hal_.dev_)
259 return true;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800260
261 // Use a stub device unless we successfully open a real HAL device.
262 hal_.dev_ = &stubhal::kDevice;
263
Jesse Hall53457db2016-12-14 16:54:06 -0800264 int result;
265 const hwvulkan_module_t* module = nullptr;
266
Jesse Hall00e61ff2017-04-07 16:48:02 -0700267 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800268 if (result == -ENOENT) {
Jooyung Han749b0ec2023-11-15 13:38:06 +0900269 result = LoadDriverFromApex(&module);
270 }
271 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700272 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800273 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800274 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800275 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700276 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800277 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800278 return true;
279 }
280
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800281
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800282 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800283 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800284 result =
285 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
286 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800287 ATRACE_END();
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);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800291 // Any device with a Vulkan HAL should be able to open the device.
292 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
293 result);
294 return false;
295 }
296
297 hal_.dev_ = device;
298
Chia-I Wu31938252016-05-23 15:31:02 +0800299 hal_.InitDebugReportIndex();
300
Yiwei Zhangd9861812019-02-13 11:51:55 -0800301 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700302 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800303
Chia-I Wu31938252016-05-23 15:31:02 +0800304 return true;
305}
306
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700307bool Hal::ShouldUnloadBuiltinDriver() {
308 // Should not unload since the driver was not loaded
309 if (!hal_.dev_)
310 return false;
311
312 // Should not unload if stubhal is used on the device
313 if (hal_.dev_ == &stubhal::kDevice)
314 return false;
315
316 // Unload the driver if updated driver is chosen
317 if (android::GraphicsEnv::getInstance().getDriverNamespace())
318 return true;
319
320 return false;
321}
322
323void Hal::UnloadBuiltinDriver() {
324 ATRACE_CALL();
325
326 ALOGD("Unload builtin Vulkan driver.");
327
Rob VanReenen189318d2024-08-01 12:07:00 -0700328 if (hal_.dev_->common.close != nullptr)
329 {
330 // Close the opened device
331 int err = hal_.dev_->common.close(
332 const_cast<struct hw_device_t*>(&hal_.dev_->common));
333 ALOG_ASSERT(!err, "hw_device_t::close() failed.");
334 }
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700335
336 // Close the opened shared library in the hw_module_t
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700337 android_unload_sphal_library(hal_.dev_->common.module->dso);
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700338
339 hal_.dev_ = nullptr;
340 hal_.debug_report_index_ = -1;
341}
342
Chia-I Wu31938252016-05-23 15:31:02 +0800343bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800344 ATRACE_CALL();
345
Chia-I Wu31938252016-05-23 15:31:02 +0800346 uint32_t count;
347 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
348 VK_SUCCESS) {
349 ALOGE("failed to get HAL instance extension count");
350 return false;
351 }
352
353 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
354 malloc(sizeof(VkExtensionProperties) * count));
355 if (!exts) {
356 ALOGE("failed to allocate HAL instance extension array");
357 return false;
358 }
359
360 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
361 VK_SUCCESS) {
362 ALOGE("failed to enumerate HAL instance extensions");
363 free(exts);
364 return false;
365 }
366
367 for (uint32_t i = 0; i < count; i++) {
368 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
369 0) {
370 debug_report_index_ = static_cast<int>(i);
371 break;
372 }
373 }
374
375 free(exts);
376
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800377 return true;
378}
379
380CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700381 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800382 const VkAllocationCallbacks& allocator)
383 : is_instance_(true),
384 allocator_(allocator),
Chris Forbes8f9e4e12024-10-01 11:02:10 +1300385 loader_api_version_(flags::vulkan_1_4_instance_api() ? VK_API_VERSION_1_4 : VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700386 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800387 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800388 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700389 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800390
Chia-I Wu4901db72016-03-24 16:38:58 +0800391CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
392 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700393 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800394 const VkAllocationCallbacks& allocator)
395 : is_instance_(false),
396 allocator_(allocator),
Chris Forbes8f9e4e12024-10-01 11:02:10 +1300397 loader_api_version_(flags::vulkan_1_4_instance_api() ? VK_API_VERSION_1_4 : VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700398 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800399 physical_dev_(physical_dev),
400 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700401 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800402
403CreateInfoWrapper::~CreateInfoWrapper() {
404 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
405 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
406}
407
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800408VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700409 VkResult result = SanitizeApiVersion();
410 if (result == VK_SUCCESS)
411 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800412 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800413 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800414 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800415 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800416
417 return result;
418}
419
420const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800421CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800422 return hook_extensions_;
423}
424
425const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800426CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800427 return hal_extensions_;
428}
429
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800430CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
431 return &instance_info_;
432}
433
Chia-I Wu4901db72016-03-24 16:38:58 +0800434CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
435 return &dev_info_;
436}
437
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700438VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700439 if (!is_instance_ || !instance_info_.pApplicationInfo)
440 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700441
Chris Forbes4c9d5122025-03-28 12:23:26 +1300442 // certain 1.3 icds in the wild may misbehave if the app requests
443 // the 1.4 instance api. since there are no actual instance api
444 // differences between these versions, downgrade the instance api
445 // to 1.3 for such icds.
446 if (icd_api_version_ >= VK_API_VERSION_1_3 &&
447 icd_api_version_ < VK_API_VERSION_1_4 &&
448 instance_info_.pApplicationInfo->apiVersion >= VK_API_VERSION_1_4) {
449 application_info_ = *instance_info_.pApplicationInfo;
450 application_info_.apiVersion = icd_api_version_;
451 instance_info_.pApplicationInfo = &application_info_;
452 return VK_SUCCESS;
453 }
454
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700455 if (icd_api_version_ > VK_API_VERSION_1_0 ||
456 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
457 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700458
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700459 // override apiVersion to avoid error return from 1.0 icd
460 application_info_ = *instance_info_.pApplicationInfo;
461 application_info_.apiVersion = VK_API_VERSION_1_0;
462 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700463
464 return VK_SUCCESS;
465}
466
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800467VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800468 const struct StructHeader {
469 VkStructureType type;
470 const void* next;
471 } * header;
472
473 if (is_instance_) {
474 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
475
476 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
477 while (header &&
478 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
479 header = reinterpret_cast<const StructHeader*>(header->next);
480
481 instance_info_.pNext = header;
482 } else {
483 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
484
485 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
486 while (header &&
487 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
488 header = reinterpret_cast<const StructHeader*>(header->next);
489
490 dev_info_.pNext = header;
491 }
492
493 return VK_SUCCESS;
494}
495
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800496VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800497 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
498 : dev_info_.ppEnabledLayerNames;
499 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
500 : dev_info_.enabledLayerCount;
501
502 // remove all layers
503 layer_names = nullptr;
504 layer_count = 0;
505
506 return VK_SUCCESS;
507}
508
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800509VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800510 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
511 : dev_info_.ppEnabledExtensionNames;
512 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
513 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800514
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800515 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800516 if (result != VK_SUCCESS)
517 return result;
518
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700519 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700520 for (uint32_t i = 0; i < ext_count; i++) {
521 // Upon api downgrade, skip the promoted instance extensions in the
522 // first pass to avoid duplicate extensions.
523 const std::optional<uint32_t> version =
524 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700525 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700526 *version <= loader_api_version_)
527 continue;
528
529 FilterExtension(ext_names[i]);
530 }
531
532 // Enable the required extensions to support core functionalities.
533 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700534 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700535 for (const auto& promoted_extension : promoted_extensions)
536 FilterExtension(promoted_extension);
537 } else {
538 for (uint32_t i = 0; i < ext_count; i++)
539 FilterExtension(ext_names[i]);
540 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800541
Jesse Halld3d887a2018-03-05 13:34:45 -0800542 // Enable device extensions that contain physical-device commands, so that
543 // vkGetInstanceProcAddr will return those physical-device commands.
544 if (is_instance_) {
545 hook_extensions_.set(ProcHook::KHR_swapchain);
546 }
547
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700548 const uint32_t api_version =
549 is_instance_ ? loader_api_version_
550 : std::min(icd_api_version_, loader_api_version_);
551 switch (api_version) {
Chris Forbes8f9e4e12024-10-01 11:02:10 +1300552 case VK_API_VERSION_1_4:
553 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_4);
554 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_4);
555 [[clang::fallthrough]];
Trevor David Black628c41a2021-09-27 05:07:22 +0000556 case VK_API_VERSION_1_3:
557 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
558 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
559 [[clang::fallthrough]];
Ian Elliottfa7af492021-07-20 17:40:24 -0600560 case VK_API_VERSION_1_2:
561 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
562 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
563 [[clang::fallthrough]];
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700564 case VK_API_VERSION_1_1:
565 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
566 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
567 [[clang::fallthrough]];
568 case VK_API_VERSION_1_0:
569 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
570 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
571 break;
572 default:
573 ALOGE("Unknown API version[%u]", api_version);
574 break;
575 }
576
Chia-I Wu4901db72016-03-24 16:38:58 +0800577 ext_names = extension_filter_.names;
578 ext_count = extension_filter_.name_count;
579
580 return VK_SUCCESS;
581}
582
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800583VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800584 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800585 return Hal::Device().EnumerateInstanceExtensionProperties(
586 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800587 } else {
588 const auto& driver = GetData(physical_dev_).driver;
589 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
590 &count, nullptr);
591 }
592}
593
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800594VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800595 uint32_t& count,
596 VkExtensionProperties* props) const {
597 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800598 return Hal::Device().EnumerateInstanceExtensionProperties(
599 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800600 } else {
601 const auto& driver = GetData(physical_dev_).driver;
602 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
603 &count, props);
604 }
605}
606
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800607VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800608 // query extension count
609 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800610 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800611 if (result != VK_SUCCESS || count == 0)
612 return result;
613
614 auto& filter = extension_filter_;
615 filter.exts =
616 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
617 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
618 alignof(VkExtensionProperties),
619 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
620 if (!filter.exts)
621 return VK_ERROR_OUT_OF_HOST_MEMORY;
622
623 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800624 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800625 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
626 return result;
627
628 if (!count)
629 return VK_SUCCESS;
630
631 filter.ext_count = count;
632
633 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700634 if (is_instance_) {
635 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
636
637 // It requires enabling additional promoted extensions to downgrade api,
638 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700639 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700640 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700641 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700642 }
643
644 count = std::min(filter.ext_count, enabled_ext_count);
645 } else {
646 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
647 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700648
649 if (!count)
650 return VK_SUCCESS;
651
Chia-I Wu4901db72016-03-24 16:38:58 +0800652 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
653 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
654 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
655 if (!filter.names)
656 return VK_ERROR_OUT_OF_HOST_MEMORY;
657
658 return VK_SUCCESS;
659}
660
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800661void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800662 auto& filter = extension_filter_;
663
664 ProcHook::Extension ext_bit = GetProcHookExtension(name);
665 if (is_instance_) {
666 switch (ext_bit) {
667 case ProcHook::KHR_android_surface:
668 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600669 case ProcHook::KHR_surface_protected_capabilities:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700670 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300671 case ProcHook::KHR_get_surface_capabilities2:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600672 case ProcHook::GOOGLE_surfaceless_query:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000673 case ProcHook::EXT_surface_maintenance1:
Chia-I Wu4901db72016-03-24 16:38:58 +0800674 hook_extensions_.set(ext_bit);
675 // return now as these extensions do not require HAL support
676 return;
677 case ProcHook::EXT_debug_report:
678 // both we and HAL can take part in
679 hook_extensions_.set(ext_bit);
680 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300681 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700682 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700683 case ProcHook::KHR_external_memory_capabilities:
684 case ProcHook::KHR_external_semaphore_capabilities:
685 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700686 case ProcHook::EXTENSION_UNKNOWN:
687 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800688 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700689
Yiwei Zhang23143102019-04-10 18:24:05 -0700690 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700691 case ProcHook::KHR_incremental_present:
692 case ProcHook::KHR_shared_presentable_image:
693 case ProcHook::KHR_swapchain:
Tom Murphyea321842024-06-14 18:26:58 +0000694 case ProcHook::KHR_swapchain_mutable_format:
Jesse Hall7f983a82018-03-29 14:46:45 -0700695 case ProcHook::EXT_hdr_metadata:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000696 case ProcHook::EXT_swapchain_maintenance1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700697 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
698 case ProcHook::ANDROID_native_buffer:
699 case ProcHook::GOOGLE_display_timing:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000700 case ProcHook::KHR_external_fence_fd:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700701 case ProcHook::EXTENSION_CORE_1_0:
702 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700703 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000704 case ProcHook::EXTENSION_CORE_1_3:
Chris Forbes8f9e4e12024-10-01 11:02:10 +1300705 case ProcHook::EXTENSION_CORE_1_4:
Jesse Hall7f983a82018-03-29 14:46:45 -0700706 case ProcHook::EXTENSION_COUNT:
707 // Device and meta extensions. If we ever get here it's a bug in
708 // our code. But enumerating them lets us avoid having a default
709 // case, and default hides other bugs.
710 ALOGE(
711 "CreateInfoWrapper::FilterExtension: invalid instance "
712 "extension '%s'. FIX ME",
713 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800714 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700715
716 // Don't use a default case. Without it, -Wswitch will tell us
717 // at compile time if someone adds a new ProcHook extension but
718 // doesn't handle it above. That's a real bug that has
719 // not-immediately-obvious effects.
720 //
721 // default:
722 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800723 }
724 } else {
725 switch (ext_bit) {
726 case ProcHook::KHR_swapchain:
727 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
728 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
729 ext_bit = ProcHook::ANDROID_native_buffer;
730 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700731 case ProcHook::KHR_incremental_present:
Ian Elliott334a4102022-12-22 18:51:09 +0000732 case ProcHook::KHR_shared_presentable_image:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000733 case ProcHook::GOOGLE_display_timing:
Ian Elliott8a977262017-01-19 09:05:58 -0700734 hook_extensions_.set(ext_bit);
735 // return now as these extensions do not require HAL support
736 return;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000737 case ProcHook::EXT_swapchain_maintenance1:
738 // map VK_KHR_swapchain_maintenance1 to KHR_external_fence_fd
739 name = VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME;
740 ext_bit = ProcHook::KHR_external_fence_fd;
741 break;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700742 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700743 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700744 hook_extensions_.set(ext_bit);
745 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700746 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000747 case ProcHook::KHR_external_fence_fd:
Tom Murphyea321842024-06-14 18:26:58 +0000748 case ProcHook::KHR_swapchain_mutable_format:
Chia-I Wu4901db72016-03-24 16:38:58 +0800749 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700750 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800751 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700752
753 case ProcHook::KHR_android_surface:
754 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700755 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700756 case ProcHook::KHR_external_memory_capabilities:
757 case ProcHook::KHR_external_semaphore_capabilities:
758 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700759 case ProcHook::KHR_get_surface_capabilities2:
760 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600761 case ProcHook::KHR_surface_protected_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700762 case ProcHook::EXT_debug_report:
763 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000764 case ProcHook::EXT_surface_maintenance1:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600765 case ProcHook::GOOGLE_surfaceless_query:
Jesse Hall7f983a82018-03-29 14:46:45 -0700766 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700767 case ProcHook::EXTENSION_CORE_1_0:
768 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700769 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000770 case ProcHook::EXTENSION_CORE_1_3:
Chris Forbes8f9e4e12024-10-01 11:02:10 +1300771 case ProcHook::EXTENSION_CORE_1_4:
Jesse Hall7f983a82018-03-29 14:46:45 -0700772 case ProcHook::EXTENSION_COUNT:
773 // Instance and meta extensions. If we ever get here it's a bug
774 // in our code. But enumerating them lets us avoid having a
775 // default case, and default hides other bugs.
776 ALOGE(
777 "CreateInfoWrapper::FilterExtension: invalid device "
778 "extension '%s'. FIX ME",
779 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800780 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700781
782 // Don't use a default case. Without it, -Wswitch will tell us
783 // at compile time if someone adds a new ProcHook extension but
784 // doesn't handle it above. That's a real bug that has
785 // not-immediately-obvious effects.
786 //
787 // default:
788 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800789 }
790 }
791
792 for (uint32_t i = 0; i < filter.ext_count; i++) {
793 const VkExtensionProperties& props = filter.exts[i];
794 // ignore unknown extensions
795 if (strcmp(name, props.extensionName) != 0)
796 continue;
797
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000798 if (ext_bit != ProcHook::EXTENSION_UNKNOWN &&
799 hal_extensions_.test(ext_bit)) {
800 ALOGI("CreateInfoWrapper::FilterExtension: already have '%s'.", name);
801 continue;
802 }
803
Ian Elliott3b48e152023-08-10 13:32:55 -0600804 // Ignore duplicate extensions (see: b/288929054)
805 bool duplicate_entry = false;
806 for (uint32_t j = 0; j < filter.name_count; j++) {
807 if (strcmp(name, filter.names[j]) == 0) {
808 duplicate_entry = true;
809 break;
810 }
811 }
812 if (duplicate_entry == true)
813 continue;
814
Chia-I Wu4901db72016-03-24 16:38:58 +0800815 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800816 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
817 if (ext_bit == ProcHook::ANDROID_native_buffer)
818 hook_extensions_.set(ProcHook::KHR_swapchain);
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000819 if (ext_bit == ProcHook::KHR_external_fence_fd)
820 hook_extensions_.set(ProcHook::EXT_swapchain_maintenance1);
Chia-I Wu1600e262016-04-12 09:40:06 +0800821
822 hal_extensions_.set(ext_bit);
823 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800824
825 break;
826 }
827}
828
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800829InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
830 void* data_mem = allocator.pfnAllocation(
831 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
832 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
833 if (!data_mem)
834 return nullptr;
835
836 return new (data_mem) InstanceData(allocator);
837}
838
839void FreeInstanceData(InstanceData* data,
840 const VkAllocationCallbacks& allocator) {
841 data->~InstanceData();
842 allocator.pfnFree(allocator.pUserData, data);
843}
844
Chia-I Wu950d6e12016-05-03 09:12:35 +0800845DeviceData* AllocateDeviceData(
846 const VkAllocationCallbacks& allocator,
847 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800848 void* data_mem = allocator.pfnAllocation(
849 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
850 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
851 if (!data_mem)
852 return nullptr;
853
Chia-I Wu950d6e12016-05-03 09:12:35 +0800854 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800855}
856
857void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
858 data->~DeviceData();
859 allocator.pfnFree(allocator.pUserData, data);
860}
861
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800862} // anonymous namespace
863
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800864bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800865 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800866}
867
Chia-I Wueb7db122016-03-24 09:11:06 +0800868PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
869 const ProcHook* hook = GetProcHook(pName);
870 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800871 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800872
873 if (!instance) {
874 if (hook->type == ProcHook::GLOBAL)
875 return hook->proc;
876
Chia-I Wu109f8982016-04-22 06:40:40 +0800877 // v0 layers expect
878 //
879 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
880 //
881 // to work.
882 if (strcmp(pName, "vkCreateDevice") == 0)
883 return hook->proc;
884
Chia-I Wueb7db122016-03-24 09:11:06 +0800885 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800886 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800887 pName);
888
Chia-I Wu109f8982016-04-22 06:40:40 +0800889 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800890 }
891
892 PFN_vkVoidFunction proc;
893
894 switch (hook->type) {
895 case ProcHook::INSTANCE:
896 proc = (GetData(instance).hook_extensions[hook->extension])
897 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800898 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800899 break;
900 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700901 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800902 ? hook->proc
903 : hook->checked_proc;
904 break;
905 default:
906 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800907 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800908 pName);
909 proc = nullptr;
910 break;
911 }
912
913 return proc;
914}
915
916PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
917 const ProcHook* hook = GetProcHook(pName);
Jörg Wagner7b48c202024-05-13 19:44:08 +0000918 PFN_vkVoidFunction drv_func = GetData(device).driver.GetDeviceProcAddr(device, pName);
919
Chia-I Wueb7db122016-03-24 09:11:06 +0800920 if (!hook)
Jörg Wagner7b48c202024-05-13 19:44:08 +0000921 return drv_func;
Chia-I Wueb7db122016-03-24 09:11:06 +0800922
923 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800924 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800925 return nullptr;
926 }
927
Jörg Wagner7b48c202024-05-13 19:44:08 +0000928 // Don't hook if we don't have a device entry function below for the core function.
929 if (!drv_func && (hook->extension >= ProcHook::EXTENSION_CORE_1_0))
930 return nullptr;
931
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800932 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
933 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800934}
935
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800936VkResult EnumerateInstanceExtensionProperties(
937 const char* pLayerName,
938 uint32_t* pPropertyCount,
939 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700940 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700941 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600942 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600943 loader_extensions.push_back(
944 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
945 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600946 loader_extensions.push_back({
947 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
948 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
949 loader_extensions.push_back({
950 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
951 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -0600952 loader_extensions.push_back(
953 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
954 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -0600955 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
956 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000957 loader_extensions.push_back({
958 VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
959 VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600960
Chia-I Wu31938252016-05-23 15:31:02 +0800961 static const VkExtensionProperties loader_debug_report_extension = {
962 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
963 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800964
965 // enumerate our extensions first
966 if (!pLayerName && pProperties) {
967 uint32_t count = std::min(
968 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
969
Yiwei Zhang5e862202019-06-21 14:59:16 -0700970 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800971
972 if (count < loader_extensions.size()) {
973 *pPropertyCount = count;
974 return VK_INCOMPLETE;
975 }
976
977 pProperties += count;
978 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800979
980 if (Hal::Get().GetDebugReportIndex() < 0) {
981 if (!*pPropertyCount) {
982 *pPropertyCount = count;
983 return VK_INCOMPLETE;
984 }
985
986 pProperties[0] = loader_debug_report_extension;
987 pProperties += 1;
988 *pPropertyCount -= 1;
989 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800990 }
991
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800992 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800993 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800994 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800995 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800996
Chia-I Wu31938252016-05-23 15:31:02 +0800997 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
998 int idx = Hal::Get().GetDebugReportIndex();
999 if (idx < 0) {
1000 *pPropertyCount += 1;
1001 } else if (pProperties &&
1002 static_cast<uint32_t>(idx) < *pPropertyCount) {
1003 pProperties[idx].specVersion =
1004 std::min(pProperties[idx].specVersion,
1005 loader_debug_report_extension.specVersion);
1006 }
1007
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001008 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +08001009 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001010
1011 return result;
1012}
1013
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001014void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001015 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001016 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001017 ATRACE_CALL();
1018
Chris Forbesfa25e632017-02-22 12:36:02 +13001019 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001020 VkPhysicalDeviceProperties2 properties = {
1021 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001022 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001023 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001024 };
1025
1026#pragma clang diagnostic push
1027#pragma clang diagnostic ignored "-Wold-style-cast"
1028 presentation_properties->sType =
1029 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1030#pragma clang diagnostic pop
1031 presentation_properties->pNext = nullptr;
1032 presentation_properties->sharedImage = VK_FALSE;
1033
Chris Forbese056c122021-07-22 13:54:04 -07001034 const auto& driver = GetData(physicalDevice).driver;
1035
1036 if (driver.GetPhysicalDeviceProperties2) {
1037 // >= 1.1 driver, supports core GPDP2 entrypoint.
1038 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1039 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1040 // Old driver, but may support presentation properties
1041 // if we have the GPDP2 extension. Otherwise, no presentation
1042 // properties supported.
1043 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1044 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001045}
1046
Trevor David Black929e9cd2022-11-22 04:12:19 +00001047VkResult GetAndroidNativeBufferSpecVersion9Support(
1048 VkPhysicalDevice physicalDevice,
1049 bool& support) {
1050 support = false;
1051
Trevor David Black2cc44682022-03-09 00:31:38 +00001052 const InstanceData& data = GetData(physicalDevice);
1053
1054 // Call to get propertyCount
1055 uint32_t propertyCount = 0;
1056 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1057 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1058 physicalDevice, nullptr, &propertyCount, nullptr);
1059 ATRACE_END();
1060
Trevor David Black929e9cd2022-11-22 04:12:19 +00001061 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1062 return result;
1063 }
1064
Trevor David Black2cc44682022-03-09 00:31:38 +00001065 // Call to enumerate properties
1066 std::vector<VkExtensionProperties> properties(propertyCount);
1067 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1068 result = data.driver.EnumerateDeviceExtensionProperties(
1069 physicalDevice, nullptr, &propertyCount, properties.data());
1070 ATRACE_END();
1071
Trevor David Black929e9cd2022-11-22 04:12:19 +00001072 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1073 return result;
1074 }
1075
Trevor David Black2cc44682022-03-09 00:31:38 +00001076 for (uint32_t i = 0; i < propertyCount; i++) {
1077 auto& prop = properties[i];
1078
1079 if (strcmp(prop.extensionName,
1080 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1081 continue;
1082
1083 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001084 support = true;
1085 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001086 }
1087 }
1088
Trevor David Black929e9cd2022-11-22 04:12:19 +00001089 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001090}
1091
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001092bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1093 const auto& driver = GetData(physicalDevice).driver;
1094 if (!driver.GetPhysicalDeviceExternalFenceProperties)
1095 return false;
1096
1097 // Requires support for external fences imported from sync fds.
1098 // This is _almost_ universal on Android, but may be missing on
1099 // some extremely old drivers, or on strange implementations like
1100 // cuttlefish.
1101 VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1102 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1103 nullptr,
1104 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1105 };
1106 VkExternalFenceProperties fenceProperties = {
1107 VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1108 nullptr,
1109 0, 0, 0
1110 };
1111
1112 GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1113 if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1114 return true;
1115
1116 return false;
1117}
1118
Chia-I Wu01cf3052016-03-24 16:16:21 +08001119VkResult EnumerateDeviceExtensionProperties(
1120 VkPhysicalDevice physicalDevice,
1121 const char* pLayerName,
1122 uint32_t* pPropertyCount,
1123 VkExtensionProperties* pProperties) {
1124 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001125 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001126 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001127 loader_extensions.push_back({
1128 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1129 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001130
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001131 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001132 if (hdrBoardConfig) {
1133 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1134 VK_EXT_HDR_METADATA_SPEC_VERSION});
1135 }
1136
Chris Forbes16095002017-05-05 15:33:29 -07001137 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001138 QueryPresentationProperties(physicalDevice, &presentation_properties);
1139 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001140 loader_extensions.push_back({
1141 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1142 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001143 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001144
Ian Elliott5c34de22017-04-10 14:42:30 -06001145 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1146 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001147 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001148 loader_extensions.push_back({
1149 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1150 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1151 }
1152
Trevor David Black2cc44682022-03-09 00:31:38 +00001153 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1154 // support is provided by the driver
1155 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1156 swapchainCompFeats = {};
1157 swapchainCompFeats.sType =
1158 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1159 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001160 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001161 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1162 imageCompFeats.sType =
1163 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1164 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001165 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001166
1167 VkPhysicalDeviceFeatures2 feats2 = {};
1168 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1169 feats2.pNext = &imageCompFeats;
1170
Trevor David Black929e9cd2022-11-22 04:12:19 +00001171 const auto& driver = GetData(physicalDevice).driver;
1172 if (driver.GetPhysicalDeviceFeatures2 ||
1173 driver.GetPhysicalDeviceFeatures2KHR) {
1174 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1175 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001176
Trevor David Black929e9cd2022-11-22 04:12:19 +00001177 bool anb9 = false;
1178 VkResult result =
1179 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1180
1181 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1182 return result;
1183 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001184
1185 if (anb9 && imageCompFeats.imageCompressionControl) {
1186 loader_extensions.push_back(
1187 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1188 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1189 }
1190 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1191 loader_extensions.push_back(
1192 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1193 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1194 }
1195
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001196 if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1197 loader_extensions.push_back({
1198 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1199 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1200 }
1201
Tom Murphyea321842024-06-14 18:26:58 +00001202 VkPhysicalDeviceProperties pDeviceProperties;
1203 data.driver.GetPhysicalDeviceProperties(physicalDevice, &pDeviceProperties);
1204 if (flags::swapchain_mutable_format_ext() &&
1205 pDeviceProperties.apiVersion >= VK_API_VERSION_1_2) {
1206 loader_extensions.push_back(
1207 {VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME,
1208 VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_SPEC_VERSION});
1209 }
1210
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001211 // enumerate our extensions first
1212 if (!pLayerName && pProperties) {
1213 uint32_t count = std::min(
1214 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1215
Yiwei Zhang5e862202019-06-21 14:59:16 -07001216 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001217
1218 if (count < loader_extensions.size()) {
1219 *pPropertyCount = count;
1220 return VK_INCOMPLETE;
1221 }
1222
1223 pProperties += count;
1224 *pPropertyCount -= count;
1225 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001226
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001227 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001228 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001229 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001230 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001231
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001232 if (pProperties) {
1233 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1234 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1235 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001236
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001237 if (strcmp(prop.extensionName,
1238 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1239 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001240
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001241 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1242 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001243
1244 if (prop.specVersion >= 8) {
1245 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1246 } else {
1247 prop.specVersion = 68;
1248 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001249 }
1250 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001251
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001252 // restore loader extension count
1253 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1254 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001255 }
1256
1257 return result;
1258}
1259
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001260VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1261 const VkAllocationCallbacks* pAllocator,
1262 VkInstance* pInstance) {
1263 const VkAllocationCallbacks& data_allocator =
1264 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1265
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001266 VkResult result = VK_SUCCESS;
1267 uint32_t icd_api_version = VK_API_VERSION_1_0;
1268 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1269 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1270 Hal::Device().GetInstanceProcAddr(nullptr,
1271 "vkEnumerateInstanceVersion"));
1272 if (pfn_enumerate_instance_version) {
1273 ATRACE_BEGIN("pfn_enumerate_instance_version");
1274 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1275 ATRACE_END();
1276 if (result != VK_SUCCESS)
1277 return result;
1278
Trevor David Blackb68a2252021-08-23 16:37:18 +00001279 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001280 }
1281
1282 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1283 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001284 if (result != VK_SUCCESS)
1285 return result;
1286
1287 InstanceData* data = AllocateInstanceData(data_allocator);
1288 if (!data)
1289 return VK_ERROR_OUT_OF_HOST_MEMORY;
1290
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001291 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001292
1293 // call into the driver
1294 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001295 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001296 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001297 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1298 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001299 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001300 if (result != VK_SUCCESS) {
1301 FreeInstanceData(data, data_allocator);
1302 return result;
1303 }
1304
1305 // initialize InstanceDriverTable
1306 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001307 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001308 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001309 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001310 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001311 if (data->driver.DestroyInstance)
1312 data->driver.DestroyInstance(instance, pAllocator);
1313
1314 FreeInstanceData(data, data_allocator);
1315
1316 return VK_ERROR_INCOMPATIBLE_DRIVER;
1317 }
1318
1319 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001320 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001321 if (!data->get_device_proc_addr) {
1322 data->driver.DestroyInstance(instance, pAllocator);
1323 FreeInstanceData(data, data_allocator);
1324
1325 return VK_ERROR_INCOMPATIBLE_DRIVER;
1326 }
1327
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001328 // TODO(b/259516419) avoid getting stats from hwui
1329 // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1330 // || (strcmp("android framework",
1331 // pCreateInfo->pApplicationInfo->pEngineName) != 0);
1332 const bool reportStats = true;
1333 if (reportStats) {
1334 // Set stats for Vulkan api version requested with application info
1335 if (pCreateInfo->pApplicationInfo) {
1336 const uint32_t vulkanApiVersion =
1337 pCreateInfo->pApplicationInfo->apiVersion;
1338 android::GraphicsEnv::getInstance().setTargetStats(
1339 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1340 vulkanApiVersion);
Tom Murphyc23fcd02024-03-13 10:22:06 +00001341
1342 if (pCreateInfo->pApplicationInfo->pEngineName) {
1343 android::GraphicsEnv::getInstance().addVulkanEngineName(
1344 pCreateInfo->pApplicationInfo->pEngineName);
1345 }
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001346 }
1347
1348 // Update stats for the extensions requested
1349 android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1350 pCreateInfo->enabledExtensionCount,
1351 pCreateInfo->ppEnabledExtensionNames);
1352 }
1353
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001354 *pInstance = instance;
1355
1356 return VK_SUCCESS;
1357}
1358
1359void DestroyInstance(VkInstance instance,
1360 const VkAllocationCallbacks* pAllocator) {
1361 InstanceData& data = GetData(instance);
1362 data.driver.DestroyInstance(instance, pAllocator);
1363
1364 VkAllocationCallbacks local_allocator;
1365 if (!pAllocator) {
1366 local_allocator = data.allocator;
1367 pAllocator = &local_allocator;
1368 }
1369
1370 FreeInstanceData(&data, *pAllocator);
1371}
1372
Chia-I Wu4901db72016-03-24 16:38:58 +08001373VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1374 const VkDeviceCreateInfo* pCreateInfo,
1375 const VkAllocationCallbacks* pAllocator,
1376 VkDevice* pDevice) {
1377 const InstanceData& instance_data = GetData(physicalDevice);
1378 const VkAllocationCallbacks& data_allocator =
1379 (pAllocator) ? *pAllocator : instance_data.allocator;
1380
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001381 VkPhysicalDeviceProperties properties;
1382 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1383 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1384 &properties);
1385 ATRACE_END();
1386
1387 CreateInfoWrapper wrapper(
1388 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001389 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001390 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001391 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001392 if (result != VK_SUCCESS)
1393 return result;
1394
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001395 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001396 DeviceData* data = AllocateDeviceData(data_allocator,
1397 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001398 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001399 if (!data)
1400 return VK_ERROR_OUT_OF_HOST_MEMORY;
1401
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001402 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001403
1404 // call into the driver
1405 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001406 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001407 result = instance_data.driver.CreateDevice(
1408 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1409 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001410 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001411 if (result != VK_SUCCESS) {
1412 FreeDeviceData(data, data_allocator);
1413 return result;
1414 }
1415
1416 // initialize DeviceDriverTable
1417 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001418 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1419 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001420 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1421 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1422 if (data->driver.DestroyDevice)
1423 data->driver.DestroyDevice(dev, pAllocator);
1424
1425 FreeDeviceData(data, data_allocator);
1426
1427 return VK_ERROR_INCOMPATIBLE_DRIVER;
1428 }
Chris Forbesd8277912017-02-10 14:59:59 +13001429
Trevor David Black2cc44682022-03-09 00:31:38 +00001430 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001431 // entrypoints varies according to the spec version.
1432 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1433 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001434 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001435 !data->driver.GetSwapchainGrallocUsage3ANDROID &&
1436 !data->driver.GetSwapchainGrallocUsage4ANDROID) {
Trevor David Black2cc44682022-03-09 00:31:38 +00001437 ALOGE(
1438 "Driver's implementation of ANDROID_native_buffer is broken;"
1439 " must expose at least one of "
1440 "vkGetSwapchainGrallocUsageANDROID or "
1441 "vkGetSwapchainGrallocUsage2ANDROID or "
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001442 "vkGetSwapchainGrallocUsage3ANDROID or "
1443 "vkGetSwapchainGrallocUsage4ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001444
1445 data->driver.DestroyDevice(dev, pAllocator);
1446 FreeDeviceData(data, data_allocator);
1447
1448 return VK_ERROR_INCOMPATIBLE_DRIVER;
1449 }
1450
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001451 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1452 // Log that the app is hitting software Vulkan implementation
1453 android::GraphicsEnv::getInstance().setTargetStats(
1454 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1455 }
1456
Jesse Halldc225072016-05-30 22:40:14 -07001457 data->driver_device = dev;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001458 data->driver_physical_device = physicalDevice;
Chia-I Wu4901db72016-03-24 16:38:58 +08001459
1460 *pDevice = dev;
1461
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001462 // TODO(b/259516419) avoid getting stats from hwui
1463 const bool reportStats = true;
1464 if (reportStats) {
1465 android::GraphicsEnv::getInstance().setTargetStats(
1466 android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1467
1468 // Set stats for creating a Vulkan device and report features in use
1469 const VkPhysicalDeviceFeatures* pEnabledFeatures =
1470 pCreateInfo->pEnabledFeatures;
1471 if (!pEnabledFeatures) {
1472 // Use features from the chained VkPhysicalDeviceFeatures2
1473 // structure, if given
1474 const VkPhysicalDeviceFeatures2* features2 =
1475 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1476 pCreateInfo->pNext);
1477 while (features2 &&
1478 features2->sType !=
1479 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1480 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1481 features2->pNext);
1482 }
1483 if (features2) {
1484 pEnabledFeatures = &features2->features;
1485 }
1486 }
1487 const VkBool32* pFeatures =
1488 reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1489 if (pFeatures) {
1490 // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1491 // of them using pointer arithmetic here and save the features in a
1492 // 64-bit bitfield
1493 static_assert(
1494 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1495 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1496 "packing");
1497 static_assert(
1498 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1499 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1500 "packing");
1501 const int numFeatures =
1502 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1503
1504 uint64_t enableFeatureBits = 0;
1505 for (int i = 0; i < numFeatures; i++) {
1506 if (pFeatures[i] != VK_FALSE) {
1507 enableFeatureBits |= (uint64_t(1) << i);
1508 }
1509 }
1510 android::GraphicsEnv::getInstance().setTargetStats(
1511 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1512 enableFeatureBits);
1513 }
1514
1515 // Update stats for the extensions requested
1516 android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1517 pCreateInfo->enabledExtensionCount,
1518 pCreateInfo->ppEnabledExtensionNames);
1519 }
1520
Chia-I Wu4901db72016-03-24 16:38:58 +08001521 return VK_SUCCESS;
1522}
1523
1524void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1525 DeviceData& data = GetData(device);
1526 data.driver.DestroyDevice(device, pAllocator);
1527
1528 VkAllocationCallbacks local_allocator;
1529 if (!pAllocator) {
1530 local_allocator = data.allocator;
1531 pAllocator = &local_allocator;
1532 }
1533
1534 FreeDeviceData(&data, *pAllocator);
1535}
1536
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001537VkResult EnumeratePhysicalDevices(VkInstance instance,
1538 uint32_t* pPhysicalDeviceCount,
1539 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001540 ATRACE_CALL();
1541
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001542 const auto& data = GetData(instance);
1543
1544 VkResult result = data.driver.EnumeratePhysicalDevices(
1545 instance, pPhysicalDeviceCount, pPhysicalDevices);
1546 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1547 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1548 SetData(pPhysicalDevices[i], data);
1549 }
1550
1551 return result;
1552}
1553
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001554VkResult EnumeratePhysicalDeviceGroups(
1555 VkInstance instance,
1556 uint32_t* pPhysicalDeviceGroupCount,
1557 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001558 ATRACE_CALL();
1559
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001560 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001561 const auto& data = GetData(instance);
1562
Yiwei Zhange4f64172020-07-05 15:17:32 -07001563 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1564 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001565 uint32_t device_count = 0;
1566 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1567 if (result < 0)
1568 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001569
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001570 if (!pPhysicalDeviceGroupProperties) {
1571 *pPhysicalDeviceGroupCount = device_count;
1572 return result;
1573 }
1574
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001575 if (!device_count) {
1576 *pPhysicalDeviceGroupCount = 0;
1577 return result;
1578 }
Chad Versace32c087f2018-09-09 07:28:05 -07001579 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1580 if (!device_count)
1581 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001582
Yiwei Zhang5e862202019-06-21 14:59:16 -07001583 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001584 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001585 result =
1586 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001587 if (result < 0)
1588 return result;
1589
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001590 for (uint32_t i = 0; i < device_count; ++i) {
1591 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1592 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1593 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1594 }
1595 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001596 if (data.driver.EnumeratePhysicalDeviceGroups) {
1597 result = data.driver.EnumeratePhysicalDeviceGroups(
1598 instance, pPhysicalDeviceGroupCount,
1599 pPhysicalDeviceGroupProperties);
1600 } else {
1601 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1602 instance, pPhysicalDeviceGroupCount,
1603 pPhysicalDeviceGroupProperties);
1604 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001605 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1606 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1607 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1608 for (uint32_t j = 0;
1609 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1610 j++) {
1611 SetData(
1612 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001613 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001614 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001615 }
1616 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001617 }
1618
1619 return result;
1620}
1621
Chia-I Wuba0be412016-03-24 16:24:40 +08001622void GetDeviceQueue(VkDevice device,
1623 uint32_t queueFamilyIndex,
1624 uint32_t queueIndex,
1625 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001626 ATRACE_CALL();
1627
Chia-I Wuba0be412016-03-24 16:24:40 +08001628 const auto& data = GetData(device);
1629
1630 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1631 SetData(*pQueue, data);
1632}
1633
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001634void GetDeviceQueue2(VkDevice device,
1635 const VkDeviceQueueInfo2* pQueueInfo,
1636 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001637 ATRACE_CALL();
1638
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001639 const auto& data = GetData(device);
1640
1641 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001642 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001643}
1644
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001645VkResult AllocateCommandBuffers(
1646 VkDevice device,
1647 const VkCommandBufferAllocateInfo* pAllocateInfo,
1648 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001649 ATRACE_CALL();
1650
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001651 const auto& data = GetData(device);
1652
1653 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1654 pCommandBuffers);
1655 if (result == VK_SUCCESS) {
1656 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1657 SetData(pCommandBuffers[i], data);
1658 }
1659
1660 return result;
1661}
1662
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001663VkResult QueueSubmit(VkQueue queue,
1664 uint32_t submitCount,
1665 const VkSubmitInfo* pSubmits,
1666 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001667 ATRACE_CALL();
1668
1669 const auto& data = GetData(queue);
1670
1671 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1672}
1673
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001674void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1675 VkPhysicalDeviceFeatures2* pFeatures) {
1676 ATRACE_CALL();
1677
1678 const auto& driver = GetData(physicalDevice).driver;
1679
1680 if (driver.GetPhysicalDeviceFeatures2) {
1681 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001682 } else {
1683 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1684 }
1685
1686 // Conditionally add imageCompressionControlSwapchain if
1687 // imageCompressionControl is supported Check for imageCompressionControl in
1688 // the pChain
1689 bool imageCompressionControl = false;
1690 bool imageCompressionControlInChain = false;
1691 bool imageCompressionControlSwapchainInChain = false;
1692 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1693 while (pFeats) {
1694 switch (pFeats->sType) {
1695 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1696 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1697 compressionFeat = reinterpret_cast<
1698 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1699 pFeats);
1700 imageCompressionControl =
1701 compressionFeat->imageCompressionControl;
1702 imageCompressionControlInChain = true;
1703 } break;
1704
1705 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001706 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1707 compressionFeat = reinterpret_cast<
1708 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1709 pFeats);
1710 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001711 imageCompressionControlSwapchainInChain = true;
1712 } break;
1713
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001714 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1715 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1716 pFeats);
1717 smf->swapchainMaintenance1 = true;
1718 } break;
1719
Trevor David Black2cc44682022-03-09 00:31:38 +00001720 default:
1721 break;
1722 }
1723 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1724 }
1725
1726 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001727 return;
1728 }
1729
Trevor David Black2cc44682022-03-09 00:31:38 +00001730 // If not in pchain, explicitly query for imageCompressionControl
1731 if (!imageCompressionControlInChain) {
1732 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1733 imageCompFeats.sType =
1734 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1735 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001736 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001737
1738 VkPhysicalDeviceFeatures2 feats2 = {};
1739 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1740 feats2.pNext = &imageCompFeats;
1741
1742 if (driver.GetPhysicalDeviceFeatures2) {
1743 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1744 } else {
1745 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1746 }
1747
1748 imageCompressionControl = imageCompFeats.imageCompressionControl;
1749 }
1750
1751 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1752 if (imageCompressionControl) {
1753 pFeats = pFeatures;
1754 while (pFeats) {
1755 switch (pFeats->sType) {
1756 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1757 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1758 compressionFeat = reinterpret_cast<
1759 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1760 pFeats);
1761 compressionFeat->imageCompressionControlSwapchain = true;
1762 } break;
1763
1764 default:
1765 break;
1766 }
1767 pFeats =
1768 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1769 }
1770 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001771}
1772
1773void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1774 VkPhysicalDeviceProperties2* pProperties) {
1775 ATRACE_CALL();
1776
1777 const auto& driver = GetData(physicalDevice).driver;
1778
1779 if (driver.GetPhysicalDeviceProperties2) {
1780 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1781 return;
1782 }
1783
1784 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1785}
1786
1787void GetPhysicalDeviceFormatProperties2(
1788 VkPhysicalDevice physicalDevice,
1789 VkFormat format,
1790 VkFormatProperties2* pFormatProperties) {
1791 ATRACE_CALL();
1792
1793 const auto& driver = GetData(physicalDevice).driver;
1794
1795 if (driver.GetPhysicalDeviceFormatProperties2) {
1796 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1797 pFormatProperties);
1798 return;
1799 }
1800
1801 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1802 pFormatProperties);
1803}
1804
1805VkResult GetPhysicalDeviceImageFormatProperties2(
1806 VkPhysicalDevice physicalDevice,
1807 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1808 VkImageFormatProperties2* pImageFormatProperties) {
1809 ATRACE_CALL();
1810
1811 const auto& driver = GetData(physicalDevice).driver;
1812
1813 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1814 return driver.GetPhysicalDeviceImageFormatProperties2(
1815 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1816 }
1817
1818 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1819 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1820}
1821
1822void GetPhysicalDeviceQueueFamilyProperties2(
1823 VkPhysicalDevice physicalDevice,
1824 uint32_t* pQueueFamilyPropertyCount,
1825 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1826 ATRACE_CALL();
1827
1828 const auto& driver = GetData(physicalDevice).driver;
1829
1830 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1831 driver.GetPhysicalDeviceQueueFamilyProperties2(
1832 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1833 return;
1834 }
1835
1836 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1837 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1838}
1839
1840void GetPhysicalDeviceMemoryProperties2(
1841 VkPhysicalDevice physicalDevice,
1842 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1843 ATRACE_CALL();
1844
1845 const auto& driver = GetData(physicalDevice).driver;
1846
1847 if (driver.GetPhysicalDeviceMemoryProperties2) {
1848 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1849 pMemoryProperties);
1850 return;
1851 }
1852
1853 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1854 pMemoryProperties);
1855}
1856
1857void GetPhysicalDeviceSparseImageFormatProperties2(
1858 VkPhysicalDevice physicalDevice,
1859 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1860 uint32_t* pPropertyCount,
1861 VkSparseImageFormatProperties2* pProperties) {
1862 ATRACE_CALL();
1863
1864 const auto& driver = GetData(physicalDevice).driver;
1865
1866 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1867 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1868 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1869 return;
1870 }
1871
1872 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1873 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1874}
1875
Yiwei Zhange1f35012020-07-05 22:52:04 -07001876void GetPhysicalDeviceExternalBufferProperties(
1877 VkPhysicalDevice physicalDevice,
1878 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1879 VkExternalBufferProperties* pExternalBufferProperties) {
1880 ATRACE_CALL();
1881
1882 const auto& driver = GetData(physicalDevice).driver;
1883
1884 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1885 driver.GetPhysicalDeviceExternalBufferProperties(
1886 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1887 return;
1888 }
1889
1890 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1891 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1892 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1893 return;
1894 }
1895
1896 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1897 sizeof(VkExternalMemoryProperties));
1898}
1899
1900void GetPhysicalDeviceExternalSemaphoreProperties(
1901 VkPhysicalDevice physicalDevice,
1902 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1903 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1904 ATRACE_CALL();
1905
1906 const auto& driver = GetData(physicalDevice).driver;
1907
1908 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1909 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1910 physicalDevice, pExternalSemaphoreInfo,
1911 pExternalSemaphoreProperties);
1912 return;
1913 }
1914
1915 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1916 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1917 physicalDevice, pExternalSemaphoreInfo,
1918 pExternalSemaphoreProperties);
1919 return;
1920 }
1921
1922 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1923 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1924 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1925}
1926
1927void GetPhysicalDeviceExternalFenceProperties(
1928 VkPhysicalDevice physicalDevice,
1929 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1930 VkExternalFenceProperties* pExternalFenceProperties) {
1931 ATRACE_CALL();
1932
1933 const auto& driver = GetData(physicalDevice).driver;
1934
1935 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1936 driver.GetPhysicalDeviceExternalFenceProperties(
1937 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1938 return;
1939 }
1940
1941 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1942 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1943 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1944 return;
1945 }
1946
1947 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1948 pExternalFenceProperties->compatibleHandleTypes = 0;
1949 pExternalFenceProperties->externalFenceFeatures = 0;
1950}
1951
Chia-I Wu9d518162016-03-24 14:55:27 +08001952} // namespace driver
1953} // namespace vulkan