blob: 28c1b5f66330fc8e2fece8a2ea517cf97e5c1d75 [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
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700442 if (icd_api_version_ > VK_API_VERSION_1_0 ||
443 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
444 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700445
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700446 // override apiVersion to avoid error return from 1.0 icd
447 application_info_ = *instance_info_.pApplicationInfo;
448 application_info_.apiVersion = VK_API_VERSION_1_0;
449 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700450
451 return VK_SUCCESS;
452}
453
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800454VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800455 const struct StructHeader {
456 VkStructureType type;
457 const void* next;
458 } * header;
459
460 if (is_instance_) {
461 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
462
463 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
464 while (header &&
465 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
466 header = reinterpret_cast<const StructHeader*>(header->next);
467
468 instance_info_.pNext = header;
469 } else {
470 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
471
472 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
473 while (header &&
474 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
475 header = reinterpret_cast<const StructHeader*>(header->next);
476
477 dev_info_.pNext = header;
478 }
479
480 return VK_SUCCESS;
481}
482
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800483VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800484 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
485 : dev_info_.ppEnabledLayerNames;
486 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
487 : dev_info_.enabledLayerCount;
488
489 // remove all layers
490 layer_names = nullptr;
491 layer_count = 0;
492
493 return VK_SUCCESS;
494}
495
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800496VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800497 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
498 : dev_info_.ppEnabledExtensionNames;
499 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
500 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800501
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800502 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800503 if (result != VK_SUCCESS)
504 return result;
505
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700506 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700507 for (uint32_t i = 0; i < ext_count; i++) {
508 // Upon api downgrade, skip the promoted instance extensions in the
509 // first pass to avoid duplicate extensions.
510 const std::optional<uint32_t> version =
511 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700512 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700513 *version <= loader_api_version_)
514 continue;
515
516 FilterExtension(ext_names[i]);
517 }
518
519 // Enable the required extensions to support core functionalities.
520 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700521 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700522 for (const auto& promoted_extension : promoted_extensions)
523 FilterExtension(promoted_extension);
524 } else {
525 for (uint32_t i = 0; i < ext_count; i++)
526 FilterExtension(ext_names[i]);
527 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800528
Jesse Halld3d887a2018-03-05 13:34:45 -0800529 // Enable device extensions that contain physical-device commands, so that
530 // vkGetInstanceProcAddr will return those physical-device commands.
531 if (is_instance_) {
532 hook_extensions_.set(ProcHook::KHR_swapchain);
533 }
534
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700535 const uint32_t api_version =
536 is_instance_ ? loader_api_version_
537 : std::min(icd_api_version_, loader_api_version_);
538 switch (api_version) {
Chris Forbes8f9e4e12024-10-01 11:02:10 +1300539 case VK_API_VERSION_1_4:
540 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_4);
541 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_4);
542 [[clang::fallthrough]];
Trevor David Black628c41a2021-09-27 05:07:22 +0000543 case VK_API_VERSION_1_3:
544 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
545 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
546 [[clang::fallthrough]];
Ian Elliottfa7af492021-07-20 17:40:24 -0600547 case VK_API_VERSION_1_2:
548 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
549 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
550 [[clang::fallthrough]];
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700551 case VK_API_VERSION_1_1:
552 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
553 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
554 [[clang::fallthrough]];
555 case VK_API_VERSION_1_0:
556 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
557 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
558 break;
559 default:
560 ALOGE("Unknown API version[%u]", api_version);
561 break;
562 }
563
Chia-I Wu4901db72016-03-24 16:38:58 +0800564 ext_names = extension_filter_.names;
565 ext_count = extension_filter_.name_count;
566
567 return VK_SUCCESS;
568}
569
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800570VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800571 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800572 return Hal::Device().EnumerateInstanceExtensionProperties(
573 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800574 } else {
575 const auto& driver = GetData(physical_dev_).driver;
576 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
577 &count, nullptr);
578 }
579}
580
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800581VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800582 uint32_t& count,
583 VkExtensionProperties* props) const {
584 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800585 return Hal::Device().EnumerateInstanceExtensionProperties(
586 nullptr, &count, props);
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, props);
591 }
592}
593
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800594VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800595 // query extension count
596 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800597 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800598 if (result != VK_SUCCESS || count == 0)
599 return result;
600
601 auto& filter = extension_filter_;
602 filter.exts =
603 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
604 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
605 alignof(VkExtensionProperties),
606 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
607 if (!filter.exts)
608 return VK_ERROR_OUT_OF_HOST_MEMORY;
609
610 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800611 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800612 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
613 return result;
614
615 if (!count)
616 return VK_SUCCESS;
617
618 filter.ext_count = count;
619
620 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700621 if (is_instance_) {
622 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
623
624 // It requires enabling additional promoted extensions to downgrade api,
625 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700626 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700627 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700628 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700629 }
630
631 count = std::min(filter.ext_count, enabled_ext_count);
632 } else {
633 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
634 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700635
636 if (!count)
637 return VK_SUCCESS;
638
Chia-I Wu4901db72016-03-24 16:38:58 +0800639 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
640 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
641 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
642 if (!filter.names)
643 return VK_ERROR_OUT_OF_HOST_MEMORY;
644
645 return VK_SUCCESS;
646}
647
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800648void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800649 auto& filter = extension_filter_;
650
651 ProcHook::Extension ext_bit = GetProcHookExtension(name);
652 if (is_instance_) {
653 switch (ext_bit) {
654 case ProcHook::KHR_android_surface:
655 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600656 case ProcHook::KHR_surface_protected_capabilities:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700657 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300658 case ProcHook::KHR_get_surface_capabilities2:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600659 case ProcHook::GOOGLE_surfaceless_query:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000660 case ProcHook::EXT_surface_maintenance1:
Chia-I Wu4901db72016-03-24 16:38:58 +0800661 hook_extensions_.set(ext_bit);
662 // return now as these extensions do not require HAL support
663 return;
664 case ProcHook::EXT_debug_report:
665 // both we and HAL can take part in
666 hook_extensions_.set(ext_bit);
667 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300668 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700669 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700670 case ProcHook::KHR_external_memory_capabilities:
671 case ProcHook::KHR_external_semaphore_capabilities:
672 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700673 case ProcHook::EXTENSION_UNKNOWN:
674 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800675 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700676
Yiwei Zhang23143102019-04-10 18:24:05 -0700677 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700678 case ProcHook::KHR_incremental_present:
679 case ProcHook::KHR_shared_presentable_image:
680 case ProcHook::KHR_swapchain:
Tom Murphyea321842024-06-14 18:26:58 +0000681 case ProcHook::KHR_swapchain_mutable_format:
Jesse Hall7f983a82018-03-29 14:46:45 -0700682 case ProcHook::EXT_hdr_metadata:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000683 case ProcHook::EXT_swapchain_maintenance1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700684 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
685 case ProcHook::ANDROID_native_buffer:
686 case ProcHook::GOOGLE_display_timing:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000687 case ProcHook::KHR_external_fence_fd:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700688 case ProcHook::EXTENSION_CORE_1_0:
689 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700690 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000691 case ProcHook::EXTENSION_CORE_1_3:
Chris Forbes8f9e4e12024-10-01 11:02:10 +1300692 case ProcHook::EXTENSION_CORE_1_4:
Jesse Hall7f983a82018-03-29 14:46:45 -0700693 case ProcHook::EXTENSION_COUNT:
694 // Device and meta extensions. If we ever get here it's a bug in
695 // our code. But enumerating them lets us avoid having a default
696 // case, and default hides other bugs.
697 ALOGE(
698 "CreateInfoWrapper::FilterExtension: invalid instance "
699 "extension '%s'. FIX ME",
700 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800701 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700702
703 // Don't use a default case. Without it, -Wswitch will tell us
704 // at compile time if someone adds a new ProcHook extension but
705 // doesn't handle it above. That's a real bug that has
706 // not-immediately-obvious effects.
707 //
708 // default:
709 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800710 }
711 } else {
712 switch (ext_bit) {
713 case ProcHook::KHR_swapchain:
714 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
715 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
716 ext_bit = ProcHook::ANDROID_native_buffer;
717 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700718 case ProcHook::KHR_incremental_present:
Ian Elliott334a4102022-12-22 18:51:09 +0000719 case ProcHook::KHR_shared_presentable_image:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000720 case ProcHook::GOOGLE_display_timing:
Ian Elliott8a977262017-01-19 09:05:58 -0700721 hook_extensions_.set(ext_bit);
722 // return now as these extensions do not require HAL support
723 return;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000724 case ProcHook::EXT_swapchain_maintenance1:
725 // map VK_KHR_swapchain_maintenance1 to KHR_external_fence_fd
726 name = VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME;
727 ext_bit = ProcHook::KHR_external_fence_fd;
728 break;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700729 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700730 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700731 hook_extensions_.set(ext_bit);
732 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700733 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000734 case ProcHook::KHR_external_fence_fd:
Tom Murphyea321842024-06-14 18:26:58 +0000735 case ProcHook::KHR_swapchain_mutable_format:
Chia-I Wu4901db72016-03-24 16:38:58 +0800736 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700737 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800738 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700739
740 case ProcHook::KHR_android_surface:
741 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700742 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700743 case ProcHook::KHR_external_memory_capabilities:
744 case ProcHook::KHR_external_semaphore_capabilities:
745 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700746 case ProcHook::KHR_get_surface_capabilities2:
747 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600748 case ProcHook::KHR_surface_protected_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700749 case ProcHook::EXT_debug_report:
750 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000751 case ProcHook::EXT_surface_maintenance1:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600752 case ProcHook::GOOGLE_surfaceless_query:
Jesse Hall7f983a82018-03-29 14:46:45 -0700753 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700754 case ProcHook::EXTENSION_CORE_1_0:
755 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700756 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000757 case ProcHook::EXTENSION_CORE_1_3:
Chris Forbes8f9e4e12024-10-01 11:02:10 +1300758 case ProcHook::EXTENSION_CORE_1_4:
Jesse Hall7f983a82018-03-29 14:46:45 -0700759 case ProcHook::EXTENSION_COUNT:
760 // Instance and meta extensions. If we ever get here it's a bug
761 // in our code. But enumerating them lets us avoid having a
762 // default case, and default hides other bugs.
763 ALOGE(
764 "CreateInfoWrapper::FilterExtension: invalid device "
765 "extension '%s'. FIX ME",
766 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800767 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700768
769 // Don't use a default case. Without it, -Wswitch will tell us
770 // at compile time if someone adds a new ProcHook extension but
771 // doesn't handle it above. That's a real bug that has
772 // not-immediately-obvious effects.
773 //
774 // default:
775 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800776 }
777 }
778
779 for (uint32_t i = 0; i < filter.ext_count; i++) {
780 const VkExtensionProperties& props = filter.exts[i];
781 // ignore unknown extensions
782 if (strcmp(name, props.extensionName) != 0)
783 continue;
784
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000785 if (ext_bit != ProcHook::EXTENSION_UNKNOWN &&
786 hal_extensions_.test(ext_bit)) {
787 ALOGI("CreateInfoWrapper::FilterExtension: already have '%s'.", name);
788 continue;
789 }
790
Ian Elliott3b48e152023-08-10 13:32:55 -0600791 // Ignore duplicate extensions (see: b/288929054)
792 bool duplicate_entry = false;
793 for (uint32_t j = 0; j < filter.name_count; j++) {
794 if (strcmp(name, filter.names[j]) == 0) {
795 duplicate_entry = true;
796 break;
797 }
798 }
799 if (duplicate_entry == true)
800 continue;
801
Chia-I Wu4901db72016-03-24 16:38:58 +0800802 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800803 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
804 if (ext_bit == ProcHook::ANDROID_native_buffer)
805 hook_extensions_.set(ProcHook::KHR_swapchain);
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000806 if (ext_bit == ProcHook::KHR_external_fence_fd)
807 hook_extensions_.set(ProcHook::EXT_swapchain_maintenance1);
Chia-I Wu1600e262016-04-12 09:40:06 +0800808
809 hal_extensions_.set(ext_bit);
810 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800811
812 break;
813 }
814}
815
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800816InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
817 void* data_mem = allocator.pfnAllocation(
818 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
819 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
820 if (!data_mem)
821 return nullptr;
822
823 return new (data_mem) InstanceData(allocator);
824}
825
826void FreeInstanceData(InstanceData* data,
827 const VkAllocationCallbacks& allocator) {
828 data->~InstanceData();
829 allocator.pfnFree(allocator.pUserData, data);
830}
831
Chia-I Wu950d6e12016-05-03 09:12:35 +0800832DeviceData* AllocateDeviceData(
833 const VkAllocationCallbacks& allocator,
834 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800835 void* data_mem = allocator.pfnAllocation(
836 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
837 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
838 if (!data_mem)
839 return nullptr;
840
Chia-I Wu950d6e12016-05-03 09:12:35 +0800841 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800842}
843
844void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
845 data->~DeviceData();
846 allocator.pfnFree(allocator.pUserData, data);
847}
848
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800849} // anonymous namespace
850
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800851bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800852 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800853}
854
Chia-I Wueb7db122016-03-24 09:11:06 +0800855PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
856 const ProcHook* hook = GetProcHook(pName);
857 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800858 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800859
860 if (!instance) {
861 if (hook->type == ProcHook::GLOBAL)
862 return hook->proc;
863
Chia-I Wu109f8982016-04-22 06:40:40 +0800864 // v0 layers expect
865 //
866 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
867 //
868 // to work.
869 if (strcmp(pName, "vkCreateDevice") == 0)
870 return hook->proc;
871
Chia-I Wueb7db122016-03-24 09:11:06 +0800872 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800873 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800874 pName);
875
Chia-I Wu109f8982016-04-22 06:40:40 +0800876 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800877 }
878
879 PFN_vkVoidFunction proc;
880
881 switch (hook->type) {
882 case ProcHook::INSTANCE:
883 proc = (GetData(instance).hook_extensions[hook->extension])
884 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800885 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800886 break;
887 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700888 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800889 ? hook->proc
890 : hook->checked_proc;
891 break;
892 default:
893 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800894 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800895 pName);
896 proc = nullptr;
897 break;
898 }
899
900 return proc;
901}
902
903PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
904 const ProcHook* hook = GetProcHook(pName);
Jörg Wagner7b48c202024-05-13 19:44:08 +0000905 PFN_vkVoidFunction drv_func = GetData(device).driver.GetDeviceProcAddr(device, pName);
906
Chia-I Wueb7db122016-03-24 09:11:06 +0800907 if (!hook)
Jörg Wagner7b48c202024-05-13 19:44:08 +0000908 return drv_func;
Chia-I Wueb7db122016-03-24 09:11:06 +0800909
910 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800911 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800912 return nullptr;
913 }
914
Jörg Wagner7b48c202024-05-13 19:44:08 +0000915 // Don't hook if we don't have a device entry function below for the core function.
916 if (!drv_func && (hook->extension >= ProcHook::EXTENSION_CORE_1_0))
917 return nullptr;
918
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800919 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
920 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800921}
922
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800923VkResult EnumerateInstanceExtensionProperties(
924 const char* pLayerName,
925 uint32_t* pPropertyCount,
926 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700927 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700928 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600929 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600930 loader_extensions.push_back(
931 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
932 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600933 loader_extensions.push_back({
934 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
935 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
936 loader_extensions.push_back({
937 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
938 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -0600939 loader_extensions.push_back(
940 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
941 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -0600942 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
943 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000944 loader_extensions.push_back({
945 VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
946 VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600947
Chia-I Wu31938252016-05-23 15:31:02 +0800948 static const VkExtensionProperties loader_debug_report_extension = {
949 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
950 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800951
952 // enumerate our extensions first
953 if (!pLayerName && pProperties) {
954 uint32_t count = std::min(
955 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
956
Yiwei Zhang5e862202019-06-21 14:59:16 -0700957 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800958
959 if (count < loader_extensions.size()) {
960 *pPropertyCount = count;
961 return VK_INCOMPLETE;
962 }
963
964 pProperties += count;
965 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800966
967 if (Hal::Get().GetDebugReportIndex() < 0) {
968 if (!*pPropertyCount) {
969 *pPropertyCount = count;
970 return VK_INCOMPLETE;
971 }
972
973 pProperties[0] = loader_debug_report_extension;
974 pProperties += 1;
975 *pPropertyCount -= 1;
976 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800977 }
978
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800979 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800980 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800981 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800982 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800983
Chia-I Wu31938252016-05-23 15:31:02 +0800984 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
985 int idx = Hal::Get().GetDebugReportIndex();
986 if (idx < 0) {
987 *pPropertyCount += 1;
988 } else if (pProperties &&
989 static_cast<uint32_t>(idx) < *pPropertyCount) {
990 pProperties[idx].specVersion =
991 std::min(pProperties[idx].specVersion,
992 loader_debug_report_extension.specVersion);
993 }
994
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800995 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800996 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800997
998 return result;
999}
1000
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001001void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001002 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001003 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001004 ATRACE_CALL();
1005
Chris Forbesfa25e632017-02-22 12:36:02 +13001006 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001007 VkPhysicalDeviceProperties2 properties = {
1008 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001009 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001010 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001011 };
1012
1013#pragma clang diagnostic push
1014#pragma clang diagnostic ignored "-Wold-style-cast"
1015 presentation_properties->sType =
1016 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1017#pragma clang diagnostic pop
1018 presentation_properties->pNext = nullptr;
1019 presentation_properties->sharedImage = VK_FALSE;
1020
Chris Forbese056c122021-07-22 13:54:04 -07001021 const auto& driver = GetData(physicalDevice).driver;
1022
1023 if (driver.GetPhysicalDeviceProperties2) {
1024 // >= 1.1 driver, supports core GPDP2 entrypoint.
1025 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1026 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1027 // Old driver, but may support presentation properties
1028 // if we have the GPDP2 extension. Otherwise, no presentation
1029 // properties supported.
1030 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1031 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001032}
1033
Trevor David Black929e9cd2022-11-22 04:12:19 +00001034VkResult GetAndroidNativeBufferSpecVersion9Support(
1035 VkPhysicalDevice physicalDevice,
1036 bool& support) {
1037 support = false;
1038
Trevor David Black2cc44682022-03-09 00:31:38 +00001039 const InstanceData& data = GetData(physicalDevice);
1040
1041 // Call to get propertyCount
1042 uint32_t propertyCount = 0;
1043 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1044 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1045 physicalDevice, nullptr, &propertyCount, nullptr);
1046 ATRACE_END();
1047
Trevor David Black929e9cd2022-11-22 04:12:19 +00001048 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1049 return result;
1050 }
1051
Trevor David Black2cc44682022-03-09 00:31:38 +00001052 // Call to enumerate properties
1053 std::vector<VkExtensionProperties> properties(propertyCount);
1054 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1055 result = data.driver.EnumerateDeviceExtensionProperties(
1056 physicalDevice, nullptr, &propertyCount, properties.data());
1057 ATRACE_END();
1058
Trevor David Black929e9cd2022-11-22 04:12:19 +00001059 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1060 return result;
1061 }
1062
Trevor David Black2cc44682022-03-09 00:31:38 +00001063 for (uint32_t i = 0; i < propertyCount; i++) {
1064 auto& prop = properties[i];
1065
1066 if (strcmp(prop.extensionName,
1067 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1068 continue;
1069
1070 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001071 support = true;
1072 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001073 }
1074 }
1075
Trevor David Black929e9cd2022-11-22 04:12:19 +00001076 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001077}
1078
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001079bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1080 const auto& driver = GetData(physicalDevice).driver;
1081 if (!driver.GetPhysicalDeviceExternalFenceProperties)
1082 return false;
1083
1084 // Requires support for external fences imported from sync fds.
1085 // This is _almost_ universal on Android, but may be missing on
1086 // some extremely old drivers, or on strange implementations like
1087 // cuttlefish.
1088 VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1089 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1090 nullptr,
1091 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1092 };
1093 VkExternalFenceProperties fenceProperties = {
1094 VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1095 nullptr,
1096 0, 0, 0
1097 };
1098
1099 GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1100 if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1101 return true;
1102
1103 return false;
1104}
1105
Chia-I Wu01cf3052016-03-24 16:16:21 +08001106VkResult EnumerateDeviceExtensionProperties(
1107 VkPhysicalDevice physicalDevice,
1108 const char* pLayerName,
1109 uint32_t* pPropertyCount,
1110 VkExtensionProperties* pProperties) {
1111 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001112 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001113 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001114 loader_extensions.push_back({
1115 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1116 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001117
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001118 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001119 if (hdrBoardConfig) {
1120 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1121 VK_EXT_HDR_METADATA_SPEC_VERSION});
1122 }
1123
Chris Forbes16095002017-05-05 15:33:29 -07001124 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001125 QueryPresentationProperties(physicalDevice, &presentation_properties);
1126 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001127 loader_extensions.push_back({
1128 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1129 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001130 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001131
Ian Elliott5c34de22017-04-10 14:42:30 -06001132 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1133 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001134 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001135 loader_extensions.push_back({
1136 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1137 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1138 }
1139
Trevor David Black2cc44682022-03-09 00:31:38 +00001140 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1141 // support is provided by the driver
1142 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1143 swapchainCompFeats = {};
1144 swapchainCompFeats.sType =
1145 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1146 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001147 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001148 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1149 imageCompFeats.sType =
1150 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1151 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001152 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001153
1154 VkPhysicalDeviceFeatures2 feats2 = {};
1155 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1156 feats2.pNext = &imageCompFeats;
1157
Trevor David Black929e9cd2022-11-22 04:12:19 +00001158 const auto& driver = GetData(physicalDevice).driver;
1159 if (driver.GetPhysicalDeviceFeatures2 ||
1160 driver.GetPhysicalDeviceFeatures2KHR) {
1161 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1162 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001163
Trevor David Black929e9cd2022-11-22 04:12:19 +00001164 bool anb9 = false;
1165 VkResult result =
1166 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1167
1168 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1169 return result;
1170 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001171
1172 if (anb9 && imageCompFeats.imageCompressionControl) {
1173 loader_extensions.push_back(
1174 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1175 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1176 }
1177 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1178 loader_extensions.push_back(
1179 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1180 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1181 }
1182
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001183 if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1184 loader_extensions.push_back({
1185 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1186 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1187 }
1188
Tom Murphyea321842024-06-14 18:26:58 +00001189 VkPhysicalDeviceProperties pDeviceProperties;
1190 data.driver.GetPhysicalDeviceProperties(physicalDevice, &pDeviceProperties);
1191 if (flags::swapchain_mutable_format_ext() &&
1192 pDeviceProperties.apiVersion >= VK_API_VERSION_1_2) {
1193 loader_extensions.push_back(
1194 {VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME,
1195 VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_SPEC_VERSION});
1196 }
1197
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001198 // enumerate our extensions first
1199 if (!pLayerName && pProperties) {
1200 uint32_t count = std::min(
1201 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1202
Yiwei Zhang5e862202019-06-21 14:59:16 -07001203 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001204
1205 if (count < loader_extensions.size()) {
1206 *pPropertyCount = count;
1207 return VK_INCOMPLETE;
1208 }
1209
1210 pProperties += count;
1211 *pPropertyCount -= count;
1212 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001213
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001214 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001215 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001216 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001217 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001218
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001219 if (pProperties) {
1220 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1221 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1222 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001223
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001224 if (strcmp(prop.extensionName,
1225 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1226 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001227
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001228 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1229 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001230
1231 if (prop.specVersion >= 8) {
1232 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1233 } else {
1234 prop.specVersion = 68;
1235 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001236 }
1237 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001238
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001239 // restore loader extension count
1240 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1241 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001242 }
1243
1244 return result;
1245}
1246
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001247VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1248 const VkAllocationCallbacks* pAllocator,
1249 VkInstance* pInstance) {
1250 const VkAllocationCallbacks& data_allocator =
1251 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1252
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001253 VkResult result = VK_SUCCESS;
1254 uint32_t icd_api_version = VK_API_VERSION_1_0;
1255 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1256 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1257 Hal::Device().GetInstanceProcAddr(nullptr,
1258 "vkEnumerateInstanceVersion"));
1259 if (pfn_enumerate_instance_version) {
1260 ATRACE_BEGIN("pfn_enumerate_instance_version");
1261 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1262 ATRACE_END();
1263 if (result != VK_SUCCESS)
1264 return result;
1265
Trevor David Blackb68a2252021-08-23 16:37:18 +00001266 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001267 }
1268
1269 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1270 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001271 if (result != VK_SUCCESS)
1272 return result;
1273
1274 InstanceData* data = AllocateInstanceData(data_allocator);
1275 if (!data)
1276 return VK_ERROR_OUT_OF_HOST_MEMORY;
1277
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001278 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001279
1280 // call into the driver
1281 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001282 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001283 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001284 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1285 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001286 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001287 if (result != VK_SUCCESS) {
1288 FreeInstanceData(data, data_allocator);
1289 return result;
1290 }
1291
1292 // initialize InstanceDriverTable
1293 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001294 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001295 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001296 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001297 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001298 if (data->driver.DestroyInstance)
1299 data->driver.DestroyInstance(instance, pAllocator);
1300
1301 FreeInstanceData(data, data_allocator);
1302
1303 return VK_ERROR_INCOMPATIBLE_DRIVER;
1304 }
1305
1306 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001307 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001308 if (!data->get_device_proc_addr) {
1309 data->driver.DestroyInstance(instance, pAllocator);
1310 FreeInstanceData(data, data_allocator);
1311
1312 return VK_ERROR_INCOMPATIBLE_DRIVER;
1313 }
1314
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001315 // TODO(b/259516419) avoid getting stats from hwui
1316 // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1317 // || (strcmp("android framework",
1318 // pCreateInfo->pApplicationInfo->pEngineName) != 0);
1319 const bool reportStats = true;
1320 if (reportStats) {
1321 // Set stats for Vulkan api version requested with application info
1322 if (pCreateInfo->pApplicationInfo) {
1323 const uint32_t vulkanApiVersion =
1324 pCreateInfo->pApplicationInfo->apiVersion;
1325 android::GraphicsEnv::getInstance().setTargetStats(
1326 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1327 vulkanApiVersion);
Tom Murphyc23fcd02024-03-13 10:22:06 +00001328
1329 if (pCreateInfo->pApplicationInfo->pEngineName) {
1330 android::GraphicsEnv::getInstance().addVulkanEngineName(
1331 pCreateInfo->pApplicationInfo->pEngineName);
1332 }
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001333 }
1334
1335 // Update stats for the extensions requested
1336 android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1337 pCreateInfo->enabledExtensionCount,
1338 pCreateInfo->ppEnabledExtensionNames);
1339 }
1340
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001341 *pInstance = instance;
1342
1343 return VK_SUCCESS;
1344}
1345
1346void DestroyInstance(VkInstance instance,
1347 const VkAllocationCallbacks* pAllocator) {
1348 InstanceData& data = GetData(instance);
1349 data.driver.DestroyInstance(instance, pAllocator);
1350
1351 VkAllocationCallbacks local_allocator;
1352 if (!pAllocator) {
1353 local_allocator = data.allocator;
1354 pAllocator = &local_allocator;
1355 }
1356
1357 FreeInstanceData(&data, *pAllocator);
1358}
1359
Chia-I Wu4901db72016-03-24 16:38:58 +08001360VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1361 const VkDeviceCreateInfo* pCreateInfo,
1362 const VkAllocationCallbacks* pAllocator,
1363 VkDevice* pDevice) {
1364 const InstanceData& instance_data = GetData(physicalDevice);
1365 const VkAllocationCallbacks& data_allocator =
1366 (pAllocator) ? *pAllocator : instance_data.allocator;
1367
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001368 VkPhysicalDeviceProperties properties;
1369 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1370 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1371 &properties);
1372 ATRACE_END();
1373
1374 CreateInfoWrapper wrapper(
1375 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001376 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001377 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001378 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001379 if (result != VK_SUCCESS)
1380 return result;
1381
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001382 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001383 DeviceData* data = AllocateDeviceData(data_allocator,
1384 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001385 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001386 if (!data)
1387 return VK_ERROR_OUT_OF_HOST_MEMORY;
1388
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001389 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001390
1391 // call into the driver
1392 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001393 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001394 result = instance_data.driver.CreateDevice(
1395 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1396 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001397 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001398 if (result != VK_SUCCESS) {
1399 FreeDeviceData(data, data_allocator);
1400 return result;
1401 }
1402
1403 // initialize DeviceDriverTable
1404 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001405 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1406 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001407 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1408 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1409 if (data->driver.DestroyDevice)
1410 data->driver.DestroyDevice(dev, pAllocator);
1411
1412 FreeDeviceData(data, data_allocator);
1413
1414 return VK_ERROR_INCOMPATIBLE_DRIVER;
1415 }
Chris Forbesd8277912017-02-10 14:59:59 +13001416
Trevor David Black2cc44682022-03-09 00:31:38 +00001417 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001418 // entrypoints varies according to the spec version.
1419 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1420 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001421 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001422 !data->driver.GetSwapchainGrallocUsage3ANDROID &&
1423 !data->driver.GetSwapchainGrallocUsage4ANDROID) {
Trevor David Black2cc44682022-03-09 00:31:38 +00001424 ALOGE(
1425 "Driver's implementation of ANDROID_native_buffer is broken;"
1426 " must expose at least one of "
1427 "vkGetSwapchainGrallocUsageANDROID or "
1428 "vkGetSwapchainGrallocUsage2ANDROID or "
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001429 "vkGetSwapchainGrallocUsage3ANDROID or "
1430 "vkGetSwapchainGrallocUsage4ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001431
1432 data->driver.DestroyDevice(dev, pAllocator);
1433 FreeDeviceData(data, data_allocator);
1434
1435 return VK_ERROR_INCOMPATIBLE_DRIVER;
1436 }
1437
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001438 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1439 // Log that the app is hitting software Vulkan implementation
1440 android::GraphicsEnv::getInstance().setTargetStats(
1441 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1442 }
1443
Jesse Halldc225072016-05-30 22:40:14 -07001444 data->driver_device = dev;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001445 data->driver_physical_device = physicalDevice;
Chia-I Wu4901db72016-03-24 16:38:58 +08001446
1447 *pDevice = dev;
1448
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001449 // TODO(b/259516419) avoid getting stats from hwui
1450 const bool reportStats = true;
1451 if (reportStats) {
1452 android::GraphicsEnv::getInstance().setTargetStats(
1453 android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1454
1455 // Set stats for creating a Vulkan device and report features in use
1456 const VkPhysicalDeviceFeatures* pEnabledFeatures =
1457 pCreateInfo->pEnabledFeatures;
1458 if (!pEnabledFeatures) {
1459 // Use features from the chained VkPhysicalDeviceFeatures2
1460 // structure, if given
1461 const VkPhysicalDeviceFeatures2* features2 =
1462 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1463 pCreateInfo->pNext);
1464 while (features2 &&
1465 features2->sType !=
1466 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1467 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1468 features2->pNext);
1469 }
1470 if (features2) {
1471 pEnabledFeatures = &features2->features;
1472 }
1473 }
1474 const VkBool32* pFeatures =
1475 reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1476 if (pFeatures) {
1477 // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1478 // of them using pointer arithmetic here and save the features in a
1479 // 64-bit bitfield
1480 static_assert(
1481 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1482 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1483 "packing");
1484 static_assert(
1485 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1486 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1487 "packing");
1488 const int numFeatures =
1489 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1490
1491 uint64_t enableFeatureBits = 0;
1492 for (int i = 0; i < numFeatures; i++) {
1493 if (pFeatures[i] != VK_FALSE) {
1494 enableFeatureBits |= (uint64_t(1) << i);
1495 }
1496 }
1497 android::GraphicsEnv::getInstance().setTargetStats(
1498 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1499 enableFeatureBits);
1500 }
1501
1502 // Update stats for the extensions requested
1503 android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1504 pCreateInfo->enabledExtensionCount,
1505 pCreateInfo->ppEnabledExtensionNames);
1506 }
1507
Chia-I Wu4901db72016-03-24 16:38:58 +08001508 return VK_SUCCESS;
1509}
1510
1511void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1512 DeviceData& data = GetData(device);
1513 data.driver.DestroyDevice(device, pAllocator);
1514
1515 VkAllocationCallbacks local_allocator;
1516 if (!pAllocator) {
1517 local_allocator = data.allocator;
1518 pAllocator = &local_allocator;
1519 }
1520
1521 FreeDeviceData(&data, *pAllocator);
1522}
1523
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001524VkResult EnumeratePhysicalDevices(VkInstance instance,
1525 uint32_t* pPhysicalDeviceCount,
1526 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001527 ATRACE_CALL();
1528
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001529 const auto& data = GetData(instance);
1530
1531 VkResult result = data.driver.EnumeratePhysicalDevices(
1532 instance, pPhysicalDeviceCount, pPhysicalDevices);
1533 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1534 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1535 SetData(pPhysicalDevices[i], data);
1536 }
1537
1538 return result;
1539}
1540
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001541VkResult EnumeratePhysicalDeviceGroups(
1542 VkInstance instance,
1543 uint32_t* pPhysicalDeviceGroupCount,
1544 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001545 ATRACE_CALL();
1546
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001547 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001548 const auto& data = GetData(instance);
1549
Yiwei Zhange4f64172020-07-05 15:17:32 -07001550 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1551 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001552 uint32_t device_count = 0;
1553 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1554 if (result < 0)
1555 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001556
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001557 if (!pPhysicalDeviceGroupProperties) {
1558 *pPhysicalDeviceGroupCount = device_count;
1559 return result;
1560 }
1561
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001562 if (!device_count) {
1563 *pPhysicalDeviceGroupCount = 0;
1564 return result;
1565 }
Chad Versace32c087f2018-09-09 07:28:05 -07001566 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1567 if (!device_count)
1568 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001569
Yiwei Zhang5e862202019-06-21 14:59:16 -07001570 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001571 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001572 result =
1573 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001574 if (result < 0)
1575 return result;
1576
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001577 for (uint32_t i = 0; i < device_count; ++i) {
1578 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1579 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1580 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1581 }
1582 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001583 if (data.driver.EnumeratePhysicalDeviceGroups) {
1584 result = data.driver.EnumeratePhysicalDeviceGroups(
1585 instance, pPhysicalDeviceGroupCount,
1586 pPhysicalDeviceGroupProperties);
1587 } else {
1588 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1589 instance, pPhysicalDeviceGroupCount,
1590 pPhysicalDeviceGroupProperties);
1591 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001592 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1593 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1594 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1595 for (uint32_t j = 0;
1596 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1597 j++) {
1598 SetData(
1599 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001600 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001601 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001602 }
1603 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001604 }
1605
1606 return result;
1607}
1608
Chia-I Wuba0be412016-03-24 16:24:40 +08001609void GetDeviceQueue(VkDevice device,
1610 uint32_t queueFamilyIndex,
1611 uint32_t queueIndex,
1612 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001613 ATRACE_CALL();
1614
Chia-I Wuba0be412016-03-24 16:24:40 +08001615 const auto& data = GetData(device);
1616
1617 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1618 SetData(*pQueue, data);
1619}
1620
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001621void GetDeviceQueue2(VkDevice device,
1622 const VkDeviceQueueInfo2* pQueueInfo,
1623 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001624 ATRACE_CALL();
1625
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001626 const auto& data = GetData(device);
1627
1628 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001629 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001630}
1631
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001632VkResult AllocateCommandBuffers(
1633 VkDevice device,
1634 const VkCommandBufferAllocateInfo* pAllocateInfo,
1635 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001636 ATRACE_CALL();
1637
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001638 const auto& data = GetData(device);
1639
1640 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1641 pCommandBuffers);
1642 if (result == VK_SUCCESS) {
1643 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1644 SetData(pCommandBuffers[i], data);
1645 }
1646
1647 return result;
1648}
1649
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001650VkResult QueueSubmit(VkQueue queue,
1651 uint32_t submitCount,
1652 const VkSubmitInfo* pSubmits,
1653 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001654 ATRACE_CALL();
1655
1656 const auto& data = GetData(queue);
1657
1658 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1659}
1660
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001661void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1662 VkPhysicalDeviceFeatures2* pFeatures) {
1663 ATRACE_CALL();
1664
1665 const auto& driver = GetData(physicalDevice).driver;
1666
1667 if (driver.GetPhysicalDeviceFeatures2) {
1668 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001669 } else {
1670 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1671 }
1672
1673 // Conditionally add imageCompressionControlSwapchain if
1674 // imageCompressionControl is supported Check for imageCompressionControl in
1675 // the pChain
1676 bool imageCompressionControl = false;
1677 bool imageCompressionControlInChain = false;
1678 bool imageCompressionControlSwapchainInChain = false;
1679 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1680 while (pFeats) {
1681 switch (pFeats->sType) {
1682 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1683 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1684 compressionFeat = reinterpret_cast<
1685 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1686 pFeats);
1687 imageCompressionControl =
1688 compressionFeat->imageCompressionControl;
1689 imageCompressionControlInChain = true;
1690 } break;
1691
1692 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001693 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1694 compressionFeat = reinterpret_cast<
1695 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1696 pFeats);
1697 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001698 imageCompressionControlSwapchainInChain = true;
1699 } break;
1700
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001701 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1702 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1703 pFeats);
1704 smf->swapchainMaintenance1 = true;
1705 } break;
1706
Trevor David Black2cc44682022-03-09 00:31:38 +00001707 default:
1708 break;
1709 }
1710 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1711 }
1712
1713 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001714 return;
1715 }
1716
Trevor David Black2cc44682022-03-09 00:31:38 +00001717 // If not in pchain, explicitly query for imageCompressionControl
1718 if (!imageCompressionControlInChain) {
1719 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1720 imageCompFeats.sType =
1721 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1722 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001723 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001724
1725 VkPhysicalDeviceFeatures2 feats2 = {};
1726 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1727 feats2.pNext = &imageCompFeats;
1728
1729 if (driver.GetPhysicalDeviceFeatures2) {
1730 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1731 } else {
1732 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1733 }
1734
1735 imageCompressionControl = imageCompFeats.imageCompressionControl;
1736 }
1737
1738 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1739 if (imageCompressionControl) {
1740 pFeats = pFeatures;
1741 while (pFeats) {
1742 switch (pFeats->sType) {
1743 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1744 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1745 compressionFeat = reinterpret_cast<
1746 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1747 pFeats);
1748 compressionFeat->imageCompressionControlSwapchain = true;
1749 } break;
1750
1751 default:
1752 break;
1753 }
1754 pFeats =
1755 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1756 }
1757 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001758}
1759
1760void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1761 VkPhysicalDeviceProperties2* pProperties) {
1762 ATRACE_CALL();
1763
1764 const auto& driver = GetData(physicalDevice).driver;
1765
1766 if (driver.GetPhysicalDeviceProperties2) {
1767 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1768 return;
1769 }
1770
1771 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1772}
1773
1774void GetPhysicalDeviceFormatProperties2(
1775 VkPhysicalDevice physicalDevice,
1776 VkFormat format,
1777 VkFormatProperties2* pFormatProperties) {
1778 ATRACE_CALL();
1779
1780 const auto& driver = GetData(physicalDevice).driver;
1781
1782 if (driver.GetPhysicalDeviceFormatProperties2) {
1783 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1784 pFormatProperties);
1785 return;
1786 }
1787
1788 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1789 pFormatProperties);
1790}
1791
1792VkResult GetPhysicalDeviceImageFormatProperties2(
1793 VkPhysicalDevice physicalDevice,
1794 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1795 VkImageFormatProperties2* pImageFormatProperties) {
1796 ATRACE_CALL();
1797
1798 const auto& driver = GetData(physicalDevice).driver;
1799
1800 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1801 return driver.GetPhysicalDeviceImageFormatProperties2(
1802 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1803 }
1804
1805 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1806 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1807}
1808
1809void GetPhysicalDeviceQueueFamilyProperties2(
1810 VkPhysicalDevice physicalDevice,
1811 uint32_t* pQueueFamilyPropertyCount,
1812 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1813 ATRACE_CALL();
1814
1815 const auto& driver = GetData(physicalDevice).driver;
1816
1817 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1818 driver.GetPhysicalDeviceQueueFamilyProperties2(
1819 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1820 return;
1821 }
1822
1823 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1824 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1825}
1826
1827void GetPhysicalDeviceMemoryProperties2(
1828 VkPhysicalDevice physicalDevice,
1829 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1830 ATRACE_CALL();
1831
1832 const auto& driver = GetData(physicalDevice).driver;
1833
1834 if (driver.GetPhysicalDeviceMemoryProperties2) {
1835 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1836 pMemoryProperties);
1837 return;
1838 }
1839
1840 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1841 pMemoryProperties);
1842}
1843
1844void GetPhysicalDeviceSparseImageFormatProperties2(
1845 VkPhysicalDevice physicalDevice,
1846 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1847 uint32_t* pPropertyCount,
1848 VkSparseImageFormatProperties2* pProperties) {
1849 ATRACE_CALL();
1850
1851 const auto& driver = GetData(physicalDevice).driver;
1852
1853 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1854 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1855 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1856 return;
1857 }
1858
1859 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1860 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1861}
1862
Yiwei Zhange1f35012020-07-05 22:52:04 -07001863void GetPhysicalDeviceExternalBufferProperties(
1864 VkPhysicalDevice physicalDevice,
1865 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1866 VkExternalBufferProperties* pExternalBufferProperties) {
1867 ATRACE_CALL();
1868
1869 const auto& driver = GetData(physicalDevice).driver;
1870
1871 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1872 driver.GetPhysicalDeviceExternalBufferProperties(
1873 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1874 return;
1875 }
1876
1877 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1878 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1879 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1880 return;
1881 }
1882
1883 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1884 sizeof(VkExternalMemoryProperties));
1885}
1886
1887void GetPhysicalDeviceExternalSemaphoreProperties(
1888 VkPhysicalDevice physicalDevice,
1889 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1890 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1891 ATRACE_CALL();
1892
1893 const auto& driver = GetData(physicalDevice).driver;
1894
1895 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1896 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1897 physicalDevice, pExternalSemaphoreInfo,
1898 pExternalSemaphoreProperties);
1899 return;
1900 }
1901
1902 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1903 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1904 physicalDevice, pExternalSemaphoreInfo,
1905 pExternalSemaphoreProperties);
1906 return;
1907 }
1908
1909 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1910 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1911 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1912}
1913
1914void GetPhysicalDeviceExternalFenceProperties(
1915 VkPhysicalDevice physicalDevice,
1916 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1917 VkExternalFenceProperties* pExternalFenceProperties) {
1918 ATRACE_CALL();
1919
1920 const auto& driver = GetData(physicalDevice).driver;
1921
1922 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1923 driver.GetPhysicalDeviceExternalFenceProperties(
1924 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1925 return;
1926 }
1927
1928 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1929 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1930 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1931 return;
1932 }
1933
1934 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1935 pExternalFenceProperties->compatibleHandleTypes = 0;
1936 pExternalFenceProperties->externalFenceFeatures = 0;
1937}
1938
Chia-I Wu9d518162016-03-24 14:55:27 +08001939} // namespace driver
1940} // namespace vulkan