blob: 01436db4aed4250f54b2584eb15048e1ea61011a [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 Wudbb7e9c2016-03-24 15:09:38 +080053// #define ENABLE_ALLOC_CALLSTACKS 1
54#if ENABLE_ALLOC_CALLSTACKS
55#include <utils/CallStack.h>
56#define ALOGD_CALLSTACK(...) \
57 do { \
58 ALOGD(__VA_ARGS__); \
59 android::CallStack callstack; \
60 callstack.update(); \
61 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
62 } while (false)
63#else
64#define ALOGD_CALLSTACK(...) \
65 do { \
66 } while (false)
67#endif
68
Chia-I Wu9d518162016-03-24 14:55:27 +080069namespace vulkan {
70namespace driver {
71
Chia-I Wu136b8eb2016-03-24 15:01:52 +080072namespace {
73
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080074class Hal {
75 public:
76 static bool Open();
77
78 static const Hal& Get() { return hal_; }
79 static const hwvulkan_device_t& Device() { return *Get().dev_; }
80
Chia-I Wu31938252016-05-23 15:31:02 +080081 int GetDebugReportIndex() const { return debug_report_index_; }
82
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080083 private:
Chia-I Wu31938252016-05-23 15:31:02 +080084 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080085 Hal(const Hal&) = delete;
86 Hal& operator=(const Hal&) = delete;
87
Yiwei Zhang901f8ee2020-07-31 13:18:49 -070088 bool ShouldUnloadBuiltinDriver();
89 void UnloadBuiltinDriver();
Chia-I Wu31938252016-05-23 15:31:02 +080090 bool InitDebugReportIndex();
91
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080092 static Hal hal_;
93
94 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080095 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080096};
97
Chia-I Wu4901db72016-03-24 16:38:58 +080098class CreateInfoWrapper {
99 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800100 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700101 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800102 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +0800103 CreateInfoWrapper(VkPhysicalDevice physical_dev,
104 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700105 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800106 const VkAllocationCallbacks& allocator);
107 ~CreateInfoWrapper();
108
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800109 VkResult Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +0800110
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800111 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
112 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800113
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800114 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800115 explicit operator const VkDeviceCreateInfo*() const;
116
117 private:
118 struct ExtensionFilter {
119 VkExtensionProperties* exts;
120 uint32_t ext_count;
121
122 const char** names;
123 uint32_t name_count;
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700124 ExtensionFilter()
125 : exts(nullptr), ext_count(0), names(nullptr), name_count(0) {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800126 };
127
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700128 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800129 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800130 VkResult SanitizeLayers();
131 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800132
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800133 VkResult QueryExtensionCount(uint32_t& count) const;
134 VkResult EnumerateExtensions(uint32_t& count,
135 VkExtensionProperties* props) const;
136 VkResult InitExtensionFilter();
137 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800138
139 const bool is_instance_;
140 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700141 const uint32_t loader_api_version_;
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700142 const uint32_t icd_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800143
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800144 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800145
146 union {
147 VkInstanceCreateInfo instance_info_;
148 VkDeviceCreateInfo dev_info_;
149 };
150
Ian Elliottf3e872d2017-11-02 10:15:13 -0600151 VkApplicationInfo application_info_;
152
Chia-I Wu4901db72016-03-24 16:38:58 +0800153 ExtensionFilter extension_filter_;
154
155 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
156 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
157};
158
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800159Hal Hal::hal_;
160
Jesse Hall53457db2016-12-14 16:54:06 -0800161const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700162 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800163 "ro.board.platform",
164}};
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700165constexpr int LIB_DL_FLAGS = RTLD_LOCAL | RTLD_NOW;
Jooyung Han749b0ec2023-11-15 13:38:06 +0900166constexpr char RO_VULKAN_APEX_PROPERTY[] = "ro.vulkan.apex";
Jesse Hall53457db2016-12-14 16:54:06 -0800167
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800168// LoadDriver returns:
169// * 0 when succeed, or
170// * -ENOENT when fail to open binary libraries, or
171// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
172// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700173int LoadDriver(android_namespace_t* library_namespace,
Jooyung Han749b0ec2023-11-15 13:38:06 +0900174 const char* ns_name,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700175 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800176 ATRACE_CALL();
177
Jesse Hall53457db2016-12-14 16:54:06 -0800178 void* so = nullptr;
Jesse Hall53457db2016-12-14 16:54:06 -0800179 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700180 std::string lib_name = android::base::GetProperty(key, "");
181 if (lib_name.empty())
182 continue;
183
184 lib_name = "vulkan." + lib_name + ".so";
185 if (library_namespace) {
186 // load updated driver
187 const android_dlextinfo dlextinfo = {
188 .flags = ANDROID_DLEXT_USE_NAMESPACE,
189 .library_namespace = library_namespace,
190 };
191 so = android_dlopen_ext(lib_name.c_str(), LIB_DL_FLAGS, &dlextinfo);
Jooyung Han27e31082023-11-13 13:50:42 +0900192 if (!so) {
Jooyung Han749b0ec2023-11-15 13:38:06 +0900193 ALOGE("Could not load %s from %s namespace: %s.",
194 lib_name.c_str(), ns_name, dlerror());
Jooyung Han27e31082023-11-13 13:50:42 +0900195 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700196 } else {
197 // load built-in driver
198 so = android_load_sphal_library(lib_name.c_str(), LIB_DL_FLAGS);
Jesse Hall53457db2016-12-14 16:54:06 -0800199 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700200 if (so)
201 break;
Jesse Hall53457db2016-12-14 16:54:06 -0800202 }
203 if (!so)
204 return -ENOENT;
205
Jesse Hall00e61ff2017-04-07 16:48:02 -0700206 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800207 if (!hmi) {
208 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
209 dlclose(so);
210 return -EINVAL;
211 }
212 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
213 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
214 dlclose(so);
215 return -EINVAL;
216 }
217 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800219 return 0;
220}
221
Jooyung Han749b0ec2023-11-15 13:38:06 +0900222int LoadDriverFromApex(const hwvulkan_module_t** module) {
223 ATRACE_CALL();
224
225 auto apex_name = android::base::GetProperty(RO_VULKAN_APEX_PROPERTY, "");
226 if (apex_name == "") {
227 return -ENOENT;
228 }
229 // Get linker namespace for Vulkan APEX
230 std::replace(apex_name.begin(), apex_name.end(), '.', '_');
231 auto ns = android_get_exported_namespace(apex_name.c_str());
232 if (!ns) {
233 return -ENOENT;
234 }
235 android::GraphicsEnv::getInstance().setDriverToLoad(
236 android::GpuStatsInfo::Driver::VULKAN);
237 return LoadDriver(ns, apex_name.c_str(), module);
238}
239
Jesse Hall00e61ff2017-04-07 16:48:02 -0700240int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800241 ATRACE_CALL();
242
Yiwei Zhangd9861812019-02-13 11:51:55 -0800243 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700244 android::GpuStatsInfo::Driver::VULKAN);
Jooyung Han749b0ec2023-11-15 13:38:06 +0900245 return LoadDriver(nullptr, nullptr, module);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700246}
247
248int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800249 ATRACE_CALL();
250
Jesse Hall00e61ff2017-04-07 16:48:02 -0700251 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
252 if (!ns)
253 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800254 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700255 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Jooyung Han749b0ec2023-11-15 13:38:06 +0900256 int result = LoadDriver(ns, "updatable gfx driver", module);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800257 if (result != 0) {
258 LOG_ALWAYS_FATAL(
259 "couldn't find an updated Vulkan implementation from %s",
260 android::GraphicsEnv::getInstance().getDriverPath().c_str());
261 }
262 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700263}
264
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800265bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800266 ATRACE_CALL();
267
Yiwei Zhangd9861812019-02-13 11:51:55 -0800268 const nsecs_t openTime = systemTime();
269
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700270 if (hal_.ShouldUnloadBuiltinDriver()) {
271 hal_.UnloadBuiltinDriver();
272 }
273
274 if (hal_.dev_)
275 return true;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800276
277 // Use a stub device unless we successfully open a real HAL device.
278 hal_.dev_ = &stubhal::kDevice;
279
Jesse Hall53457db2016-12-14 16:54:06 -0800280 int result;
281 const hwvulkan_module_t* module = nullptr;
282
Jesse Hall00e61ff2017-04-07 16:48:02 -0700283 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800284 if (result == -ENOENT) {
Jooyung Han749b0ec2023-11-15 13:38:06 +0900285 result = LoadDriverFromApex(&module);
286 }
287 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700288 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800289 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800290 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800291 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700292 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800293 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800294 return true;
295 }
296
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800297
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800298 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800299 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800300 result =
301 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
302 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800303 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800304 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800305 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700306 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800307 // Any device with a Vulkan HAL should be able to open the device.
308 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
309 result);
310 return false;
311 }
312
313 hal_.dev_ = device;
314
Chia-I Wu31938252016-05-23 15:31:02 +0800315 hal_.InitDebugReportIndex();
316
Yiwei Zhangd9861812019-02-13 11:51:55 -0800317 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700318 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800319
Chia-I Wu31938252016-05-23 15:31:02 +0800320 return true;
321}
322
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700323bool Hal::ShouldUnloadBuiltinDriver() {
324 // Should not unload since the driver was not loaded
325 if (!hal_.dev_)
326 return false;
327
328 // Should not unload if stubhal is used on the device
329 if (hal_.dev_ == &stubhal::kDevice)
330 return false;
331
332 // Unload the driver if updated driver is chosen
333 if (android::GraphicsEnv::getInstance().getDriverNamespace())
334 return true;
335
336 return false;
337}
338
339void Hal::UnloadBuiltinDriver() {
340 ATRACE_CALL();
341
342 ALOGD("Unload builtin Vulkan driver.");
343
Rob VanReenen189318d2024-08-01 12:07:00 -0700344 if (hal_.dev_->common.close != nullptr)
345 {
346 // Close the opened device
347 int err = hal_.dev_->common.close(
348 const_cast<struct hw_device_t*>(&hal_.dev_->common));
349 ALOG_ASSERT(!err, "hw_device_t::close() failed.");
350 }
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700351
352 // Close the opened shared library in the hw_module_t
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700353 android_unload_sphal_library(hal_.dev_->common.module->dso);
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700354
355 hal_.dev_ = nullptr;
356 hal_.debug_report_index_ = -1;
357}
358
Chia-I Wu31938252016-05-23 15:31:02 +0800359bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800360 ATRACE_CALL();
361
Chia-I Wu31938252016-05-23 15:31:02 +0800362 uint32_t count;
363 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
364 VK_SUCCESS) {
365 ALOGE("failed to get HAL instance extension count");
366 return false;
367 }
368
369 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
370 malloc(sizeof(VkExtensionProperties) * count));
371 if (!exts) {
372 ALOGE("failed to allocate HAL instance extension array");
373 return false;
374 }
375
376 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
377 VK_SUCCESS) {
378 ALOGE("failed to enumerate HAL instance extensions");
379 free(exts);
380 return false;
381 }
382
383 for (uint32_t i = 0; i < count; i++) {
384 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
385 0) {
386 debug_report_index_ = static_cast<int>(i);
387 break;
388 }
389 }
390
391 free(exts);
392
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800393 return true;
394}
395
396CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700397 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800398 const VkAllocationCallbacks& allocator)
399 : is_instance_(true),
400 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000401 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700402 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800403 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800404 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700405 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800406
Chia-I Wu4901db72016-03-24 16:38:58 +0800407CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
408 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700409 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800410 const VkAllocationCallbacks& allocator)
411 : is_instance_(false),
412 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000413 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700414 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800415 physical_dev_(physical_dev),
416 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700417 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800418
419CreateInfoWrapper::~CreateInfoWrapper() {
420 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
421 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
422}
423
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800424VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700425 VkResult result = SanitizeApiVersion();
426 if (result == VK_SUCCESS)
427 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800428 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800429 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800430 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800431 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800432
433 return result;
434}
435
436const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800437CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800438 return hook_extensions_;
439}
440
441const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800442CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800443 return hal_extensions_;
444}
445
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800446CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
447 return &instance_info_;
448}
449
Chia-I Wu4901db72016-03-24 16:38:58 +0800450CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
451 return &dev_info_;
452}
453
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700454VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700455 if (!is_instance_ || !instance_info_.pApplicationInfo)
456 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700457
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700458 if (icd_api_version_ > VK_API_VERSION_1_0 ||
459 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
460 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700461
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700462 // override apiVersion to avoid error return from 1.0 icd
463 application_info_ = *instance_info_.pApplicationInfo;
464 application_info_.apiVersion = VK_API_VERSION_1_0;
465 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700466
467 return VK_SUCCESS;
468}
469
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800470VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800471 const struct StructHeader {
472 VkStructureType type;
473 const void* next;
474 } * header;
475
476 if (is_instance_) {
477 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
478
479 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
480 while (header &&
481 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
482 header = reinterpret_cast<const StructHeader*>(header->next);
483
484 instance_info_.pNext = header;
485 } else {
486 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
487
488 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
489 while (header &&
490 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
491 header = reinterpret_cast<const StructHeader*>(header->next);
492
493 dev_info_.pNext = header;
494 }
495
496 return VK_SUCCESS;
497}
498
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800499VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800500 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
501 : dev_info_.ppEnabledLayerNames;
502 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
503 : dev_info_.enabledLayerCount;
504
505 // remove all layers
506 layer_names = nullptr;
507 layer_count = 0;
508
509 return VK_SUCCESS;
510}
511
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800512VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800513 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
514 : dev_info_.ppEnabledExtensionNames;
515 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
516 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800517
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800518 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800519 if (result != VK_SUCCESS)
520 return result;
521
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700522 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700523 for (uint32_t i = 0; i < ext_count; i++) {
524 // Upon api downgrade, skip the promoted instance extensions in the
525 // first pass to avoid duplicate extensions.
526 const std::optional<uint32_t> version =
527 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700528 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700529 *version <= loader_api_version_)
530 continue;
531
532 FilterExtension(ext_names[i]);
533 }
534
535 // Enable the required extensions to support core functionalities.
536 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700537 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700538 for (const auto& promoted_extension : promoted_extensions)
539 FilterExtension(promoted_extension);
540 } else {
541 for (uint32_t i = 0; i < ext_count; i++)
542 FilterExtension(ext_names[i]);
543 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800544
Jesse Halld3d887a2018-03-05 13:34:45 -0800545 // Enable device extensions that contain physical-device commands, so that
546 // vkGetInstanceProcAddr will return those physical-device commands.
547 if (is_instance_) {
548 hook_extensions_.set(ProcHook::KHR_swapchain);
549 }
550
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700551 const uint32_t api_version =
552 is_instance_ ? loader_api_version_
553 : std::min(icd_api_version_, loader_api_version_);
554 switch (api_version) {
Trevor David Black628c41a2021-09-27 05:07:22 +0000555 case VK_API_VERSION_1_3:
556 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
557 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
558 [[clang::fallthrough]];
Ian Elliottfa7af492021-07-20 17:40:24 -0600559 case VK_API_VERSION_1_2:
560 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
561 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
562 [[clang::fallthrough]];
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700563 case VK_API_VERSION_1_1:
564 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
565 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
566 [[clang::fallthrough]];
567 case VK_API_VERSION_1_0:
568 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
569 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
570 break;
571 default:
572 ALOGE("Unknown API version[%u]", api_version);
573 break;
574 }
575
Chia-I Wu4901db72016-03-24 16:38:58 +0800576 ext_names = extension_filter_.names;
577 ext_count = extension_filter_.name_count;
578
579 return VK_SUCCESS;
580}
581
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800582VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800583 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800584 return Hal::Device().EnumerateInstanceExtensionProperties(
585 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800586 } else {
587 const auto& driver = GetData(physical_dev_).driver;
588 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
589 &count, nullptr);
590 }
591}
592
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800593VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800594 uint32_t& count,
595 VkExtensionProperties* props) const {
596 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800597 return Hal::Device().EnumerateInstanceExtensionProperties(
598 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800599 } else {
600 const auto& driver = GetData(physical_dev_).driver;
601 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
602 &count, props);
603 }
604}
605
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800606VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800607 // query extension count
608 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800609 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800610 if (result != VK_SUCCESS || count == 0)
611 return result;
612
613 auto& filter = extension_filter_;
614 filter.exts =
615 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
616 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
617 alignof(VkExtensionProperties),
618 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
619 if (!filter.exts)
620 return VK_ERROR_OUT_OF_HOST_MEMORY;
621
622 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800623 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800624 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
625 return result;
626
627 if (!count)
628 return VK_SUCCESS;
629
630 filter.ext_count = count;
631
632 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700633 if (is_instance_) {
634 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
635
636 // It requires enabling additional promoted extensions to downgrade api,
637 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700638 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700639 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700640 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700641 }
642
643 count = std::min(filter.ext_count, enabled_ext_count);
644 } else {
645 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
646 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700647
648 if (!count)
649 return VK_SUCCESS;
650
Chia-I Wu4901db72016-03-24 16:38:58 +0800651 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
652 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
653 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
654 if (!filter.names)
655 return VK_ERROR_OUT_OF_HOST_MEMORY;
656
657 return VK_SUCCESS;
658}
659
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800660void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800661 auto& filter = extension_filter_;
662
663 ProcHook::Extension ext_bit = GetProcHookExtension(name);
664 if (is_instance_) {
665 switch (ext_bit) {
666 case ProcHook::KHR_android_surface:
667 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600668 case ProcHook::KHR_surface_protected_capabilities:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700669 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300670 case ProcHook::KHR_get_surface_capabilities2:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600671 case ProcHook::GOOGLE_surfaceless_query:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000672 case ProcHook::EXT_surface_maintenance1:
Chia-I Wu4901db72016-03-24 16:38:58 +0800673 hook_extensions_.set(ext_bit);
674 // return now as these extensions do not require HAL support
675 return;
676 case ProcHook::EXT_debug_report:
677 // both we and HAL can take part in
678 hook_extensions_.set(ext_bit);
679 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300680 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700681 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700682 case ProcHook::KHR_external_memory_capabilities:
683 case ProcHook::KHR_external_semaphore_capabilities:
684 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700685 case ProcHook::EXTENSION_UNKNOWN:
686 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800687 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700688
Yiwei Zhang23143102019-04-10 18:24:05 -0700689 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700690 case ProcHook::KHR_incremental_present:
691 case ProcHook::KHR_shared_presentable_image:
692 case ProcHook::KHR_swapchain:
Tom Murphyea321842024-06-14 18:26:58 +0000693 case ProcHook::KHR_swapchain_mutable_format:
Jesse Hall7f983a82018-03-29 14:46:45 -0700694 case ProcHook::EXT_hdr_metadata:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000695 case ProcHook::EXT_swapchain_maintenance1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700696 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
697 case ProcHook::ANDROID_native_buffer:
698 case ProcHook::GOOGLE_display_timing:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000699 case ProcHook::KHR_external_fence_fd:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700700 case ProcHook::EXTENSION_CORE_1_0:
701 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700702 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000703 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700704 case ProcHook::EXTENSION_COUNT:
705 // Device and meta extensions. If we ever get here it's a bug in
706 // our code. But enumerating them lets us avoid having a default
707 // case, and default hides other bugs.
708 ALOGE(
709 "CreateInfoWrapper::FilterExtension: invalid instance "
710 "extension '%s'. FIX ME",
711 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800712 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700713
714 // Don't use a default case. Without it, -Wswitch will tell us
715 // at compile time if someone adds a new ProcHook extension but
716 // doesn't handle it above. That's a real bug that has
717 // not-immediately-obvious effects.
718 //
719 // default:
720 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800721 }
722 } else {
723 switch (ext_bit) {
724 case ProcHook::KHR_swapchain:
725 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
726 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
727 ext_bit = ProcHook::ANDROID_native_buffer;
728 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700729 case ProcHook::KHR_incremental_present:
Ian Elliott334a4102022-12-22 18:51:09 +0000730 case ProcHook::KHR_shared_presentable_image:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000731 case ProcHook::GOOGLE_display_timing:
Ian Elliott8a977262017-01-19 09:05:58 -0700732 hook_extensions_.set(ext_bit);
733 // return now as these extensions do not require HAL support
734 return;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000735 case ProcHook::EXT_swapchain_maintenance1:
736 // map VK_KHR_swapchain_maintenance1 to KHR_external_fence_fd
737 name = VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME;
738 ext_bit = ProcHook::KHR_external_fence_fd;
739 break;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700740 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700741 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700742 hook_extensions_.set(ext_bit);
743 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700744 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000745 case ProcHook::KHR_external_fence_fd:
Tom Murphyea321842024-06-14 18:26:58 +0000746 case ProcHook::KHR_swapchain_mutable_format:
Chia-I Wu4901db72016-03-24 16:38:58 +0800747 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700748 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800749 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700750
751 case ProcHook::KHR_android_surface:
752 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700753 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700754 case ProcHook::KHR_external_memory_capabilities:
755 case ProcHook::KHR_external_semaphore_capabilities:
756 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700757 case ProcHook::KHR_get_surface_capabilities2:
758 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600759 case ProcHook::KHR_surface_protected_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700760 case ProcHook::EXT_debug_report:
761 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000762 case ProcHook::EXT_surface_maintenance1:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600763 case ProcHook::GOOGLE_surfaceless_query:
Jesse Hall7f983a82018-03-29 14:46:45 -0700764 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700765 case ProcHook::EXTENSION_CORE_1_0:
766 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700767 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000768 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700769 case ProcHook::EXTENSION_COUNT:
770 // Instance and meta extensions. If we ever get here it's a bug
771 // in our code. But enumerating them lets us avoid having a
772 // default case, and default hides other bugs.
773 ALOGE(
774 "CreateInfoWrapper::FilterExtension: invalid device "
775 "extension '%s'. FIX ME",
776 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800777 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700778
779 // Don't use a default case. Without it, -Wswitch will tell us
780 // at compile time if someone adds a new ProcHook extension but
781 // doesn't handle it above. That's a real bug that has
782 // not-immediately-obvious effects.
783 //
784 // default:
785 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800786 }
787 }
788
789 for (uint32_t i = 0; i < filter.ext_count; i++) {
790 const VkExtensionProperties& props = filter.exts[i];
791 // ignore unknown extensions
792 if (strcmp(name, props.extensionName) != 0)
793 continue;
794
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000795 if (ext_bit != ProcHook::EXTENSION_UNKNOWN &&
796 hal_extensions_.test(ext_bit)) {
797 ALOGI("CreateInfoWrapper::FilterExtension: already have '%s'.", name);
798 continue;
799 }
800
Ian Elliott3b48e152023-08-10 13:32:55 -0600801 // Ignore duplicate extensions (see: b/288929054)
802 bool duplicate_entry = false;
803 for (uint32_t j = 0; j < filter.name_count; j++) {
804 if (strcmp(name, filter.names[j]) == 0) {
805 duplicate_entry = true;
806 break;
807 }
808 }
809 if (duplicate_entry == true)
810 continue;
811
Chia-I Wu4901db72016-03-24 16:38:58 +0800812 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800813 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
814 if (ext_bit == ProcHook::ANDROID_native_buffer)
815 hook_extensions_.set(ProcHook::KHR_swapchain);
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000816 if (ext_bit == ProcHook::KHR_external_fence_fd)
817 hook_extensions_.set(ProcHook::EXT_swapchain_maintenance1);
Chia-I Wu1600e262016-04-12 09:40:06 +0800818
819 hal_extensions_.set(ext_bit);
820 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800821
822 break;
823 }
824}
825
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800826VKAPI_ATTR void* DefaultAllocate(void*,
827 size_t size,
828 size_t alignment,
829 VkSystemAllocationScope) {
830 void* ptr = nullptr;
831 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
832 // additionally requires that it be at least sizeof(void*).
833 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
834 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
835 ret, ptr);
836 return ret == 0 ? ptr : nullptr;
837}
838
839VKAPI_ATTR void* DefaultReallocate(void*,
840 void* ptr,
841 size_t size,
842 size_t alignment,
843 VkSystemAllocationScope) {
844 if (size == 0) {
845 free(ptr);
846 return nullptr;
847 }
848
Yiwei Zhanga885c062019-10-24 12:07:57 -0700849 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800850 // request is smaller than the existing chunk, we just continue using it.
851 // Right now the loader never reallocs, so this doesn't matter. If that
852 // changes, or if this code is copied into some other project, this should
853 // probably have a heuristic to allocate-copy-free when doing so will save
854 // "enough" space.
855 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
856 if (size <= old_size)
857 return ptr;
858
859 void* new_ptr = nullptr;
860 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
861 return nullptr;
862 if (ptr) {
863 memcpy(new_ptr, ptr, std::min(old_size, size));
864 free(ptr);
865 }
866 return new_ptr;
867}
868
869VKAPI_ATTR void DefaultFree(void*, void* ptr) {
870 ALOGD_CALLSTACK("Free: %p", ptr);
871 free(ptr);
872}
873
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800874InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
875 void* data_mem = allocator.pfnAllocation(
876 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
877 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
878 if (!data_mem)
879 return nullptr;
880
881 return new (data_mem) InstanceData(allocator);
882}
883
884void FreeInstanceData(InstanceData* data,
885 const VkAllocationCallbacks& allocator) {
886 data->~InstanceData();
887 allocator.pfnFree(allocator.pUserData, data);
888}
889
Chia-I Wu950d6e12016-05-03 09:12:35 +0800890DeviceData* AllocateDeviceData(
891 const VkAllocationCallbacks& allocator,
892 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800893 void* data_mem = allocator.pfnAllocation(
894 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
895 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
896 if (!data_mem)
897 return nullptr;
898
Chia-I Wu950d6e12016-05-03 09:12:35 +0800899 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800900}
901
902void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
903 data->~DeviceData();
904 allocator.pfnFree(allocator.pUserData, data);
905}
906
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800907} // anonymous namespace
908
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800909bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800910 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800911}
912
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800913const VkAllocationCallbacks& GetDefaultAllocator() {
914 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
915 .pUserData = nullptr,
916 .pfnAllocation = DefaultAllocate,
917 .pfnReallocation = DefaultReallocate,
918 .pfnFree = DefaultFree,
919 };
920
921 return kDefaultAllocCallbacks;
922}
923
Chia-I Wueb7db122016-03-24 09:11:06 +0800924PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
925 const ProcHook* hook = GetProcHook(pName);
926 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800927 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800928
929 if (!instance) {
930 if (hook->type == ProcHook::GLOBAL)
931 return hook->proc;
932
Chia-I Wu109f8982016-04-22 06:40:40 +0800933 // v0 layers expect
934 //
935 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
936 //
937 // to work.
938 if (strcmp(pName, "vkCreateDevice") == 0)
939 return hook->proc;
940
Chia-I Wueb7db122016-03-24 09:11:06 +0800941 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800942 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800943 pName);
944
Chia-I Wu109f8982016-04-22 06:40:40 +0800945 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800946 }
947
948 PFN_vkVoidFunction proc;
949
950 switch (hook->type) {
951 case ProcHook::INSTANCE:
952 proc = (GetData(instance).hook_extensions[hook->extension])
953 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800954 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800955 break;
956 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700957 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800958 ? hook->proc
959 : hook->checked_proc;
960 break;
961 default:
962 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800963 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800964 pName);
965 proc = nullptr;
966 break;
967 }
968
969 return proc;
970}
971
972PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
973 const ProcHook* hook = GetProcHook(pName);
Jörg Wagner7b48c202024-05-13 19:44:08 +0000974 PFN_vkVoidFunction drv_func = GetData(device).driver.GetDeviceProcAddr(device, pName);
975
Chia-I Wueb7db122016-03-24 09:11:06 +0800976 if (!hook)
Jörg Wagner7b48c202024-05-13 19:44:08 +0000977 return drv_func;
Chia-I Wueb7db122016-03-24 09:11:06 +0800978
979 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800980 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800981 return nullptr;
982 }
983
Jörg Wagner7b48c202024-05-13 19:44:08 +0000984 // Don't hook if we don't have a device entry function below for the core function.
985 if (!drv_func && (hook->extension >= ProcHook::EXTENSION_CORE_1_0))
986 return nullptr;
987
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800988 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
989 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800990}
991
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800992VkResult EnumerateInstanceExtensionProperties(
993 const char* pLayerName,
994 uint32_t* pPropertyCount,
995 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700996 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700997 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600998 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600999 loader_extensions.push_back(
1000 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
1001 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -06001002 loader_extensions.push_back({
1003 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
1004 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
1005 loader_extensions.push_back({
1006 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
1007 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -06001008 loader_extensions.push_back(
1009 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
1010 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -06001011 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
1012 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001013 loader_extensions.push_back({
1014 VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
1015 VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -06001016
Chia-I Wu31938252016-05-23 15:31:02 +08001017 static const VkExtensionProperties loader_debug_report_extension = {
1018 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
1019 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001020
1021 // enumerate our extensions first
1022 if (!pLayerName && pProperties) {
1023 uint32_t count = std::min(
1024 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1025
Yiwei Zhang5e862202019-06-21 14:59:16 -07001026 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001027
1028 if (count < loader_extensions.size()) {
1029 *pPropertyCount = count;
1030 return VK_INCOMPLETE;
1031 }
1032
1033 pProperties += count;
1034 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +08001035
1036 if (Hal::Get().GetDebugReportIndex() < 0) {
1037 if (!*pPropertyCount) {
1038 *pPropertyCount = count;
1039 return VK_INCOMPLETE;
1040 }
1041
1042 pProperties[0] = loader_debug_report_extension;
1043 pProperties += 1;
1044 *pPropertyCount -= 1;
1045 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001046 }
1047
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001048 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001049 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001050 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001051 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001052
Chia-I Wu31938252016-05-23 15:31:02 +08001053 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1054 int idx = Hal::Get().GetDebugReportIndex();
1055 if (idx < 0) {
1056 *pPropertyCount += 1;
1057 } else if (pProperties &&
1058 static_cast<uint32_t>(idx) < *pPropertyCount) {
1059 pProperties[idx].specVersion =
1060 std::min(pProperties[idx].specVersion,
1061 loader_debug_report_extension.specVersion);
1062 }
1063
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001064 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +08001065 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001066
1067 return result;
1068}
1069
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001070void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001071 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001072 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001073 ATRACE_CALL();
1074
Chris Forbesfa25e632017-02-22 12:36:02 +13001075 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001076 VkPhysicalDeviceProperties2 properties = {
1077 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001078 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001079 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001080 };
1081
1082#pragma clang diagnostic push
1083#pragma clang diagnostic ignored "-Wold-style-cast"
1084 presentation_properties->sType =
1085 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1086#pragma clang diagnostic pop
1087 presentation_properties->pNext = nullptr;
1088 presentation_properties->sharedImage = VK_FALSE;
1089
Chris Forbese056c122021-07-22 13:54:04 -07001090 const auto& driver = GetData(physicalDevice).driver;
1091
1092 if (driver.GetPhysicalDeviceProperties2) {
1093 // >= 1.1 driver, supports core GPDP2 entrypoint.
1094 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1095 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1096 // Old driver, but may support presentation properties
1097 // if we have the GPDP2 extension. Otherwise, no presentation
1098 // properties supported.
1099 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1100 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001101}
1102
Trevor David Black929e9cd2022-11-22 04:12:19 +00001103VkResult GetAndroidNativeBufferSpecVersion9Support(
1104 VkPhysicalDevice physicalDevice,
1105 bool& support) {
1106 support = false;
1107
Trevor David Black2cc44682022-03-09 00:31:38 +00001108 const InstanceData& data = GetData(physicalDevice);
1109
1110 // Call to get propertyCount
1111 uint32_t propertyCount = 0;
1112 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1113 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1114 physicalDevice, nullptr, &propertyCount, nullptr);
1115 ATRACE_END();
1116
Trevor David Black929e9cd2022-11-22 04:12:19 +00001117 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1118 return result;
1119 }
1120
Trevor David Black2cc44682022-03-09 00:31:38 +00001121 // Call to enumerate properties
1122 std::vector<VkExtensionProperties> properties(propertyCount);
1123 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1124 result = data.driver.EnumerateDeviceExtensionProperties(
1125 physicalDevice, nullptr, &propertyCount, properties.data());
1126 ATRACE_END();
1127
Trevor David Black929e9cd2022-11-22 04:12:19 +00001128 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1129 return result;
1130 }
1131
Trevor David Black2cc44682022-03-09 00:31:38 +00001132 for (uint32_t i = 0; i < propertyCount; i++) {
1133 auto& prop = properties[i];
1134
1135 if (strcmp(prop.extensionName,
1136 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1137 continue;
1138
1139 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001140 support = true;
1141 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001142 }
1143 }
1144
Trevor David Black929e9cd2022-11-22 04:12:19 +00001145 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001146}
1147
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001148bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1149 const auto& driver = GetData(physicalDevice).driver;
1150 if (!driver.GetPhysicalDeviceExternalFenceProperties)
1151 return false;
1152
1153 // Requires support for external fences imported from sync fds.
1154 // This is _almost_ universal on Android, but may be missing on
1155 // some extremely old drivers, or on strange implementations like
1156 // cuttlefish.
1157 VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1158 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1159 nullptr,
1160 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1161 };
1162 VkExternalFenceProperties fenceProperties = {
1163 VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1164 nullptr,
1165 0, 0, 0
1166 };
1167
1168 GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1169 if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1170 return true;
1171
1172 return false;
1173}
1174
Chia-I Wu01cf3052016-03-24 16:16:21 +08001175VkResult EnumerateDeviceExtensionProperties(
1176 VkPhysicalDevice physicalDevice,
1177 const char* pLayerName,
1178 uint32_t* pPropertyCount,
1179 VkExtensionProperties* pProperties) {
1180 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001181 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001182 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001183 loader_extensions.push_back({
1184 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1185 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001186
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001187 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001188 if (hdrBoardConfig) {
1189 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1190 VK_EXT_HDR_METADATA_SPEC_VERSION});
1191 }
1192
Chris Forbes16095002017-05-05 15:33:29 -07001193 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001194 QueryPresentationProperties(physicalDevice, &presentation_properties);
1195 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001196 loader_extensions.push_back({
1197 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1198 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001199 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001200
Ian Elliott5c34de22017-04-10 14:42:30 -06001201 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1202 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001203 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001204 loader_extensions.push_back({
1205 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1206 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1207 }
1208
Trevor David Black2cc44682022-03-09 00:31:38 +00001209 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1210 // support is provided by the driver
1211 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1212 swapchainCompFeats = {};
1213 swapchainCompFeats.sType =
1214 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1215 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001216 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001217 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1218 imageCompFeats.sType =
1219 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1220 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001221 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001222
1223 VkPhysicalDeviceFeatures2 feats2 = {};
1224 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1225 feats2.pNext = &imageCompFeats;
1226
Trevor David Black929e9cd2022-11-22 04:12:19 +00001227 const auto& driver = GetData(physicalDevice).driver;
1228 if (driver.GetPhysicalDeviceFeatures2 ||
1229 driver.GetPhysicalDeviceFeatures2KHR) {
1230 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1231 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001232
Trevor David Black929e9cd2022-11-22 04:12:19 +00001233 bool anb9 = false;
1234 VkResult result =
1235 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1236
1237 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1238 return result;
1239 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001240
1241 if (anb9 && imageCompFeats.imageCompressionControl) {
1242 loader_extensions.push_back(
1243 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1244 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1245 }
1246 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1247 loader_extensions.push_back(
1248 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1249 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1250 }
1251
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001252 if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1253 loader_extensions.push_back({
1254 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1255 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1256 }
1257
Tom Murphyea321842024-06-14 18:26:58 +00001258 VkPhysicalDeviceProperties pDeviceProperties;
1259 data.driver.GetPhysicalDeviceProperties(physicalDevice, &pDeviceProperties);
1260 if (flags::swapchain_mutable_format_ext() &&
1261 pDeviceProperties.apiVersion >= VK_API_VERSION_1_2) {
1262 loader_extensions.push_back(
1263 {VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME,
1264 VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_SPEC_VERSION});
1265 }
1266
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001267 // enumerate our extensions first
1268 if (!pLayerName && pProperties) {
1269 uint32_t count = std::min(
1270 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1271
Yiwei Zhang5e862202019-06-21 14:59:16 -07001272 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001273
1274 if (count < loader_extensions.size()) {
1275 *pPropertyCount = count;
1276 return VK_INCOMPLETE;
1277 }
1278
1279 pProperties += count;
1280 *pPropertyCount -= count;
1281 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001282
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001283 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001284 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001285 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001286 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001287
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001288 if (pProperties) {
1289 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1290 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1291 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001292
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001293 if (strcmp(prop.extensionName,
1294 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1295 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001296
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001297 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1298 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001299
1300 if (prop.specVersion >= 8) {
1301 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1302 } else {
1303 prop.specVersion = 68;
1304 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001305 }
1306 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001307
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001308 // restore loader extension count
1309 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1310 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001311 }
1312
1313 return result;
1314}
1315
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001316VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1317 const VkAllocationCallbacks* pAllocator,
1318 VkInstance* pInstance) {
1319 const VkAllocationCallbacks& data_allocator =
1320 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1321
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001322 VkResult result = VK_SUCCESS;
1323 uint32_t icd_api_version = VK_API_VERSION_1_0;
1324 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1325 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1326 Hal::Device().GetInstanceProcAddr(nullptr,
1327 "vkEnumerateInstanceVersion"));
1328 if (pfn_enumerate_instance_version) {
1329 ATRACE_BEGIN("pfn_enumerate_instance_version");
1330 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1331 ATRACE_END();
1332 if (result != VK_SUCCESS)
1333 return result;
1334
Trevor David Blackb68a2252021-08-23 16:37:18 +00001335 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001336 }
1337
1338 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1339 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001340 if (result != VK_SUCCESS)
1341 return result;
1342
1343 InstanceData* data = AllocateInstanceData(data_allocator);
1344 if (!data)
1345 return VK_ERROR_OUT_OF_HOST_MEMORY;
1346
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001347 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001348
1349 // call into the driver
1350 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001351 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001352 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001353 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1354 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001355 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001356 if (result != VK_SUCCESS) {
1357 FreeInstanceData(data, data_allocator);
1358 return result;
1359 }
1360
1361 // initialize InstanceDriverTable
1362 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001363 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001364 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001365 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001366 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001367 if (data->driver.DestroyInstance)
1368 data->driver.DestroyInstance(instance, pAllocator);
1369
1370 FreeInstanceData(data, data_allocator);
1371
1372 return VK_ERROR_INCOMPATIBLE_DRIVER;
1373 }
1374
1375 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001376 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001377 if (!data->get_device_proc_addr) {
1378 data->driver.DestroyInstance(instance, pAllocator);
1379 FreeInstanceData(data, data_allocator);
1380
1381 return VK_ERROR_INCOMPATIBLE_DRIVER;
1382 }
1383
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001384 // TODO(b/259516419) avoid getting stats from hwui
1385 // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1386 // || (strcmp("android framework",
1387 // pCreateInfo->pApplicationInfo->pEngineName) != 0);
1388 const bool reportStats = true;
1389 if (reportStats) {
1390 // Set stats for Vulkan api version requested with application info
1391 if (pCreateInfo->pApplicationInfo) {
1392 const uint32_t vulkanApiVersion =
1393 pCreateInfo->pApplicationInfo->apiVersion;
1394 android::GraphicsEnv::getInstance().setTargetStats(
1395 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1396 vulkanApiVersion);
Tom Murphyc23fcd02024-03-13 10:22:06 +00001397
1398 if (pCreateInfo->pApplicationInfo->pEngineName) {
1399 android::GraphicsEnv::getInstance().addVulkanEngineName(
1400 pCreateInfo->pApplicationInfo->pEngineName);
1401 }
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001402 }
1403
1404 // Update stats for the extensions requested
1405 android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1406 pCreateInfo->enabledExtensionCount,
1407 pCreateInfo->ppEnabledExtensionNames);
1408 }
1409
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001410 *pInstance = instance;
1411
1412 return VK_SUCCESS;
1413}
1414
1415void DestroyInstance(VkInstance instance,
1416 const VkAllocationCallbacks* pAllocator) {
1417 InstanceData& data = GetData(instance);
1418 data.driver.DestroyInstance(instance, pAllocator);
1419
1420 VkAllocationCallbacks local_allocator;
1421 if (!pAllocator) {
1422 local_allocator = data.allocator;
1423 pAllocator = &local_allocator;
1424 }
1425
1426 FreeInstanceData(&data, *pAllocator);
1427}
1428
Chia-I Wu4901db72016-03-24 16:38:58 +08001429VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1430 const VkDeviceCreateInfo* pCreateInfo,
1431 const VkAllocationCallbacks* pAllocator,
1432 VkDevice* pDevice) {
1433 const InstanceData& instance_data = GetData(physicalDevice);
1434 const VkAllocationCallbacks& data_allocator =
1435 (pAllocator) ? *pAllocator : instance_data.allocator;
1436
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001437 VkPhysicalDeviceProperties properties;
1438 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1439 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1440 &properties);
1441 ATRACE_END();
1442
1443 CreateInfoWrapper wrapper(
1444 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001445 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001446 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001447 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001448 if (result != VK_SUCCESS)
1449 return result;
1450
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001451 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001452 DeviceData* data = AllocateDeviceData(data_allocator,
1453 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001454 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001455 if (!data)
1456 return VK_ERROR_OUT_OF_HOST_MEMORY;
1457
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001458 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001459
1460 // call into the driver
1461 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001462 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001463 result = instance_data.driver.CreateDevice(
1464 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1465 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001466 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001467 if (result != VK_SUCCESS) {
1468 FreeDeviceData(data, data_allocator);
1469 return result;
1470 }
1471
1472 // initialize DeviceDriverTable
1473 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001474 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1475 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001476 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1477 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1478 if (data->driver.DestroyDevice)
1479 data->driver.DestroyDevice(dev, pAllocator);
1480
1481 FreeDeviceData(data, data_allocator);
1482
1483 return VK_ERROR_INCOMPATIBLE_DRIVER;
1484 }
Chris Forbesd8277912017-02-10 14:59:59 +13001485
Trevor David Black2cc44682022-03-09 00:31:38 +00001486 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001487 // entrypoints varies according to the spec version.
1488 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1489 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001490 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001491 !data->driver.GetSwapchainGrallocUsage3ANDROID &&
1492 !data->driver.GetSwapchainGrallocUsage4ANDROID) {
Trevor David Black2cc44682022-03-09 00:31:38 +00001493 ALOGE(
1494 "Driver's implementation of ANDROID_native_buffer is broken;"
1495 " must expose at least one of "
1496 "vkGetSwapchainGrallocUsageANDROID or "
1497 "vkGetSwapchainGrallocUsage2ANDROID or "
Trevor David Blackb6ca8422023-07-26 20:00:04 +00001498 "vkGetSwapchainGrallocUsage3ANDROID or "
1499 "vkGetSwapchainGrallocUsage4ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001500
1501 data->driver.DestroyDevice(dev, pAllocator);
1502 FreeDeviceData(data, data_allocator);
1503
1504 return VK_ERROR_INCOMPATIBLE_DRIVER;
1505 }
1506
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001507 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1508 // Log that the app is hitting software Vulkan implementation
1509 android::GraphicsEnv::getInstance().setTargetStats(
1510 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1511 }
1512
Jesse Halldc225072016-05-30 22:40:14 -07001513 data->driver_device = dev;
Trevor David Blackd7320ef2023-08-23 21:21:51 +00001514 data->driver_physical_device = physicalDevice;
Chia-I Wu4901db72016-03-24 16:38:58 +08001515
1516 *pDevice = dev;
1517
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001518 // TODO(b/259516419) avoid getting stats from hwui
1519 const bool reportStats = true;
1520 if (reportStats) {
1521 android::GraphicsEnv::getInstance().setTargetStats(
1522 android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1523
1524 // Set stats for creating a Vulkan device and report features in use
1525 const VkPhysicalDeviceFeatures* pEnabledFeatures =
1526 pCreateInfo->pEnabledFeatures;
1527 if (!pEnabledFeatures) {
1528 // Use features from the chained VkPhysicalDeviceFeatures2
1529 // structure, if given
1530 const VkPhysicalDeviceFeatures2* features2 =
1531 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1532 pCreateInfo->pNext);
1533 while (features2 &&
1534 features2->sType !=
1535 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1536 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1537 features2->pNext);
1538 }
1539 if (features2) {
1540 pEnabledFeatures = &features2->features;
1541 }
1542 }
1543 const VkBool32* pFeatures =
1544 reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1545 if (pFeatures) {
1546 // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1547 // of them using pointer arithmetic here and save the features in a
1548 // 64-bit bitfield
1549 static_assert(
1550 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1551 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1552 "packing");
1553 static_assert(
1554 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1555 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1556 "packing");
1557 const int numFeatures =
1558 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1559
1560 uint64_t enableFeatureBits = 0;
1561 for (int i = 0; i < numFeatures; i++) {
1562 if (pFeatures[i] != VK_FALSE) {
1563 enableFeatureBits |= (uint64_t(1) << i);
1564 }
1565 }
1566 android::GraphicsEnv::getInstance().setTargetStats(
1567 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1568 enableFeatureBits);
1569 }
1570
1571 // Update stats for the extensions requested
1572 android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1573 pCreateInfo->enabledExtensionCount,
1574 pCreateInfo->ppEnabledExtensionNames);
1575 }
1576
Chia-I Wu4901db72016-03-24 16:38:58 +08001577 return VK_SUCCESS;
1578}
1579
1580void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1581 DeviceData& data = GetData(device);
1582 data.driver.DestroyDevice(device, pAllocator);
1583
1584 VkAllocationCallbacks local_allocator;
1585 if (!pAllocator) {
1586 local_allocator = data.allocator;
1587 pAllocator = &local_allocator;
1588 }
1589
1590 FreeDeviceData(&data, *pAllocator);
1591}
1592
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001593VkResult EnumeratePhysicalDevices(VkInstance instance,
1594 uint32_t* pPhysicalDeviceCount,
1595 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001596 ATRACE_CALL();
1597
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001598 const auto& data = GetData(instance);
1599
1600 VkResult result = data.driver.EnumeratePhysicalDevices(
1601 instance, pPhysicalDeviceCount, pPhysicalDevices);
1602 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1603 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1604 SetData(pPhysicalDevices[i], data);
1605 }
1606
1607 return result;
1608}
1609
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001610VkResult EnumeratePhysicalDeviceGroups(
1611 VkInstance instance,
1612 uint32_t* pPhysicalDeviceGroupCount,
1613 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001614 ATRACE_CALL();
1615
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001616 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001617 const auto& data = GetData(instance);
1618
Yiwei Zhange4f64172020-07-05 15:17:32 -07001619 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1620 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001621 uint32_t device_count = 0;
1622 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1623 if (result < 0)
1624 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001625
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001626 if (!pPhysicalDeviceGroupProperties) {
1627 *pPhysicalDeviceGroupCount = device_count;
1628 return result;
1629 }
1630
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001631 if (!device_count) {
1632 *pPhysicalDeviceGroupCount = 0;
1633 return result;
1634 }
Chad Versace32c087f2018-09-09 07:28:05 -07001635 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1636 if (!device_count)
1637 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001638
Yiwei Zhang5e862202019-06-21 14:59:16 -07001639 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001640 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001641 result =
1642 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001643 if (result < 0)
1644 return result;
1645
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001646 for (uint32_t i = 0; i < device_count; ++i) {
1647 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1648 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1649 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1650 }
1651 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001652 if (data.driver.EnumeratePhysicalDeviceGroups) {
1653 result = data.driver.EnumeratePhysicalDeviceGroups(
1654 instance, pPhysicalDeviceGroupCount,
1655 pPhysicalDeviceGroupProperties);
1656 } else {
1657 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1658 instance, pPhysicalDeviceGroupCount,
1659 pPhysicalDeviceGroupProperties);
1660 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001661 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1662 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1663 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1664 for (uint32_t j = 0;
1665 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1666 j++) {
1667 SetData(
1668 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001669 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001670 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001671 }
1672 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001673 }
1674
1675 return result;
1676}
1677
Chia-I Wuba0be412016-03-24 16:24:40 +08001678void GetDeviceQueue(VkDevice device,
1679 uint32_t queueFamilyIndex,
1680 uint32_t queueIndex,
1681 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001682 ATRACE_CALL();
1683
Chia-I Wuba0be412016-03-24 16:24:40 +08001684 const auto& data = GetData(device);
1685
1686 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1687 SetData(*pQueue, data);
1688}
1689
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001690void GetDeviceQueue2(VkDevice device,
1691 const VkDeviceQueueInfo2* pQueueInfo,
1692 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001693 ATRACE_CALL();
1694
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001695 const auto& data = GetData(device);
1696
1697 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001698 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001699}
1700
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001701VkResult AllocateCommandBuffers(
1702 VkDevice device,
1703 const VkCommandBufferAllocateInfo* pAllocateInfo,
1704 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001705 ATRACE_CALL();
1706
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001707 const auto& data = GetData(device);
1708
1709 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1710 pCommandBuffers);
1711 if (result == VK_SUCCESS) {
1712 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1713 SetData(pCommandBuffers[i], data);
1714 }
1715
1716 return result;
1717}
1718
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001719VkResult QueueSubmit(VkQueue queue,
1720 uint32_t submitCount,
1721 const VkSubmitInfo* pSubmits,
1722 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001723 ATRACE_CALL();
1724
1725 const auto& data = GetData(queue);
1726
1727 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1728}
1729
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001730void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1731 VkPhysicalDeviceFeatures2* pFeatures) {
1732 ATRACE_CALL();
1733
1734 const auto& driver = GetData(physicalDevice).driver;
1735
1736 if (driver.GetPhysicalDeviceFeatures2) {
1737 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001738 } else {
1739 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1740 }
1741
1742 // Conditionally add imageCompressionControlSwapchain if
1743 // imageCompressionControl is supported Check for imageCompressionControl in
1744 // the pChain
1745 bool imageCompressionControl = false;
1746 bool imageCompressionControlInChain = false;
1747 bool imageCompressionControlSwapchainInChain = false;
1748 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1749 while (pFeats) {
1750 switch (pFeats->sType) {
1751 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1752 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1753 compressionFeat = reinterpret_cast<
1754 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1755 pFeats);
1756 imageCompressionControl =
1757 compressionFeat->imageCompressionControl;
1758 imageCompressionControlInChain = true;
1759 } break;
1760
1761 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001762 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1763 compressionFeat = reinterpret_cast<
1764 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1765 pFeats);
1766 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001767 imageCompressionControlSwapchainInChain = true;
1768 } break;
1769
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001770 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1771 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1772 pFeats);
1773 smf->swapchainMaintenance1 = true;
1774 } break;
1775
Trevor David Black2cc44682022-03-09 00:31:38 +00001776 default:
1777 break;
1778 }
1779 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1780 }
1781
1782 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001783 return;
1784 }
1785
Trevor David Black2cc44682022-03-09 00:31:38 +00001786 // If not in pchain, explicitly query for imageCompressionControl
1787 if (!imageCompressionControlInChain) {
1788 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1789 imageCompFeats.sType =
1790 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1791 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001792 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001793
1794 VkPhysicalDeviceFeatures2 feats2 = {};
1795 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1796 feats2.pNext = &imageCompFeats;
1797
1798 if (driver.GetPhysicalDeviceFeatures2) {
1799 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1800 } else {
1801 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1802 }
1803
1804 imageCompressionControl = imageCompFeats.imageCompressionControl;
1805 }
1806
1807 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1808 if (imageCompressionControl) {
1809 pFeats = pFeatures;
1810 while (pFeats) {
1811 switch (pFeats->sType) {
1812 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1813 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1814 compressionFeat = reinterpret_cast<
1815 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1816 pFeats);
1817 compressionFeat->imageCompressionControlSwapchain = true;
1818 } break;
1819
1820 default:
1821 break;
1822 }
1823 pFeats =
1824 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1825 }
1826 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001827}
1828
1829void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1830 VkPhysicalDeviceProperties2* pProperties) {
1831 ATRACE_CALL();
1832
1833 const auto& driver = GetData(physicalDevice).driver;
1834
1835 if (driver.GetPhysicalDeviceProperties2) {
1836 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1837 return;
1838 }
1839
1840 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1841}
1842
1843void GetPhysicalDeviceFormatProperties2(
1844 VkPhysicalDevice physicalDevice,
1845 VkFormat format,
1846 VkFormatProperties2* pFormatProperties) {
1847 ATRACE_CALL();
1848
1849 const auto& driver = GetData(physicalDevice).driver;
1850
1851 if (driver.GetPhysicalDeviceFormatProperties2) {
1852 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1853 pFormatProperties);
1854 return;
1855 }
1856
1857 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1858 pFormatProperties);
1859}
1860
1861VkResult GetPhysicalDeviceImageFormatProperties2(
1862 VkPhysicalDevice physicalDevice,
1863 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1864 VkImageFormatProperties2* pImageFormatProperties) {
1865 ATRACE_CALL();
1866
1867 const auto& driver = GetData(physicalDevice).driver;
1868
1869 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1870 return driver.GetPhysicalDeviceImageFormatProperties2(
1871 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1872 }
1873
1874 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1875 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1876}
1877
1878void GetPhysicalDeviceQueueFamilyProperties2(
1879 VkPhysicalDevice physicalDevice,
1880 uint32_t* pQueueFamilyPropertyCount,
1881 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1882 ATRACE_CALL();
1883
1884 const auto& driver = GetData(physicalDevice).driver;
1885
1886 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1887 driver.GetPhysicalDeviceQueueFamilyProperties2(
1888 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1889 return;
1890 }
1891
1892 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1893 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1894}
1895
1896void GetPhysicalDeviceMemoryProperties2(
1897 VkPhysicalDevice physicalDevice,
1898 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1899 ATRACE_CALL();
1900
1901 const auto& driver = GetData(physicalDevice).driver;
1902
1903 if (driver.GetPhysicalDeviceMemoryProperties2) {
1904 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1905 pMemoryProperties);
1906 return;
1907 }
1908
1909 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1910 pMemoryProperties);
1911}
1912
1913void GetPhysicalDeviceSparseImageFormatProperties2(
1914 VkPhysicalDevice physicalDevice,
1915 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1916 uint32_t* pPropertyCount,
1917 VkSparseImageFormatProperties2* pProperties) {
1918 ATRACE_CALL();
1919
1920 const auto& driver = GetData(physicalDevice).driver;
1921
1922 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1923 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1924 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1925 return;
1926 }
1927
1928 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1929 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1930}
1931
Yiwei Zhange1f35012020-07-05 22:52:04 -07001932void GetPhysicalDeviceExternalBufferProperties(
1933 VkPhysicalDevice physicalDevice,
1934 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1935 VkExternalBufferProperties* pExternalBufferProperties) {
1936 ATRACE_CALL();
1937
1938 const auto& driver = GetData(physicalDevice).driver;
1939
1940 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1941 driver.GetPhysicalDeviceExternalBufferProperties(
1942 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1943 return;
1944 }
1945
1946 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1947 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1948 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1949 return;
1950 }
1951
1952 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1953 sizeof(VkExternalMemoryProperties));
1954}
1955
1956void GetPhysicalDeviceExternalSemaphoreProperties(
1957 VkPhysicalDevice physicalDevice,
1958 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1959 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1960 ATRACE_CALL();
1961
1962 const auto& driver = GetData(physicalDevice).driver;
1963
1964 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1965 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1966 physicalDevice, pExternalSemaphoreInfo,
1967 pExternalSemaphoreProperties);
1968 return;
1969 }
1970
1971 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1972 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1973 physicalDevice, pExternalSemaphoreInfo,
1974 pExternalSemaphoreProperties);
1975 return;
1976 }
1977
1978 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1979 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1980 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1981}
1982
1983void GetPhysicalDeviceExternalFenceProperties(
1984 VkPhysicalDevice physicalDevice,
1985 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1986 VkExternalFenceProperties* pExternalFenceProperties) {
1987 ATRACE_CALL();
1988
1989 const auto& driver = GetData(physicalDevice).driver;
1990
1991 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1992 driver.GetPhysicalDeviceExternalFenceProperties(
1993 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1994 return;
1995 }
1996
1997 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1998 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1999 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
2000 return;
2001 }
2002
2003 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
2004 pExternalFenceProperties->compatibleHandleTypes = 0;
2005 pExternalFenceProperties->externalFenceFeatures = 0;
2006}
2007
Chia-I Wu9d518162016-03-24 14:55:27 +08002008} // namespace driver
2009} // namespace vulkan