blob: b7e19d4296e00d54f5a160fac768e6cc065e60a5 [file] [log] [blame]
Chia-I Wu9d518162016-03-24 14:55:27 +08001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang5e862202019-06-21 14:59:16 -070019#include "driver.h"
20
21#include <dlfcn.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <malloc.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080023#include <stdlib.h>
24#include <string.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080025
Sundong Ahnbc37dd52020-04-23 21:21:00 +090026#include <SurfaceFlingerProperties.h>
27#include <android-base/properties.h>
Jesse Hall53457db2016-12-14 16:54:06 -080028#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080029#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
30#include <configstore/Utils.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070032#include <log/log.h>
33#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080034#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080035#include <utils/Trace.h>
Yiwei Zhang40e84f12020-10-14 08:42:26 -070036#include <vndksupport/linker.h>
Jesse Hall53457db2016-12-14 16:54:06 -080037
Yiwei Zhang5e862202019-06-21 14:59:16 -070038#include <algorithm>
39#include <array>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070040#include <climits>
Yiwei Zhang5e862202019-06-21 14:59:16 -070041#include <new>
42#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070043
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070044#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080045
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080046using namespace android::hardware::configstore;
47using namespace android::hardware::configstore::V1_0;
48
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080049// #define ENABLE_ALLOC_CALLSTACKS 1
50#if ENABLE_ALLOC_CALLSTACKS
51#include <utils/CallStack.h>
52#define ALOGD_CALLSTACK(...) \
53 do { \
54 ALOGD(__VA_ARGS__); \
55 android::CallStack callstack; \
56 callstack.update(); \
57 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
58 } while (false)
59#else
60#define ALOGD_CALLSTACK(...) \
61 do { \
62 } while (false)
63#endif
64
Chia-I Wu9d518162016-03-24 14:55:27 +080065namespace vulkan {
66namespace driver {
67
Chia-I Wu136b8eb2016-03-24 15:01:52 +080068namespace {
69
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080070class Hal {
71 public:
72 static bool Open();
73
74 static const Hal& Get() { return hal_; }
75 static const hwvulkan_device_t& Device() { return *Get().dev_; }
76
Chia-I Wu31938252016-05-23 15:31:02 +080077 int GetDebugReportIndex() const { return debug_report_index_; }
78
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080079 private:
Chia-I Wu31938252016-05-23 15:31:02 +080080 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080081 Hal(const Hal&) = delete;
82 Hal& operator=(const Hal&) = delete;
83
Yiwei Zhang901f8ee2020-07-31 13:18:49 -070084 bool ShouldUnloadBuiltinDriver();
85 void UnloadBuiltinDriver();
Chia-I Wu31938252016-05-23 15:31:02 +080086 bool InitDebugReportIndex();
87
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080088 static Hal hal_;
89
90 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080091 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080092};
93
Chia-I Wu4901db72016-03-24 16:38:58 +080094class CreateInfoWrapper {
95 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080096 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -070097 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080098 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080099 CreateInfoWrapper(VkPhysicalDevice physical_dev,
100 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700101 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800102 const VkAllocationCallbacks& allocator);
103 ~CreateInfoWrapper();
104
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800105 VkResult Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +0800106
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800107 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
108 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800109
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800110 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800111 explicit operator const VkDeviceCreateInfo*() const;
112
113 private:
114 struct ExtensionFilter {
115 VkExtensionProperties* exts;
116 uint32_t ext_count;
117
118 const char** names;
119 uint32_t name_count;
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700120 ExtensionFilter()
121 : exts(nullptr), ext_count(0), names(nullptr), name_count(0) {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800122 };
123
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700124 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800125 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800126 VkResult SanitizeLayers();
127 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800128
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800129 VkResult QueryExtensionCount(uint32_t& count) const;
130 VkResult EnumerateExtensions(uint32_t& count,
131 VkExtensionProperties* props) const;
132 VkResult InitExtensionFilter();
133 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800134
135 const bool is_instance_;
136 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700137 const uint32_t loader_api_version_;
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700138 const uint32_t icd_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800139
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800140 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800141
142 union {
143 VkInstanceCreateInfo instance_info_;
144 VkDeviceCreateInfo dev_info_;
145 };
146
Ian Elliottf3e872d2017-11-02 10:15:13 -0600147 VkApplicationInfo application_info_;
148
Chia-I Wu4901db72016-03-24 16:38:58 +0800149 ExtensionFilter extension_filter_;
150
151 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
152 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
153};
154
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800155Hal Hal::hal_;
156
Jesse Hall53457db2016-12-14 16:54:06 -0800157const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700158 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800159 "ro.board.platform",
160}};
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700161constexpr int LIB_DL_FLAGS = RTLD_LOCAL | RTLD_NOW;
Jesse Hall53457db2016-12-14 16:54:06 -0800162
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800163// LoadDriver returns:
164// * 0 when succeed, or
165// * -ENOENT when fail to open binary libraries, or
166// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
167// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700168int LoadDriver(android_namespace_t* library_namespace,
169 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800170 ATRACE_CALL();
171
Jesse Hall53457db2016-12-14 16:54:06 -0800172 void* so = nullptr;
Jesse Hall53457db2016-12-14 16:54:06 -0800173 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700174 std::string lib_name = android::base::GetProperty(key, "");
175 if (lib_name.empty())
176 continue;
177
178 lib_name = "vulkan." + lib_name + ".so";
179 if (library_namespace) {
180 // load updated driver
181 const android_dlextinfo dlextinfo = {
182 .flags = ANDROID_DLEXT_USE_NAMESPACE,
183 .library_namespace = library_namespace,
184 };
185 so = android_dlopen_ext(lib_name.c_str(), LIB_DL_FLAGS, &dlextinfo);
Yiwei Zhang9058b8f2020-11-12 20:23:00 +0000186 ALOGE("Could not load %s from updatable gfx driver namespace: %s.",
187 lib_name.c_str(), dlerror());
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700188 } else {
189 // load built-in driver
190 so = android_load_sphal_library(lib_name.c_str(), LIB_DL_FLAGS);
Jesse Hall53457db2016-12-14 16:54:06 -0800191 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700192 if (so)
193 break;
Jesse Hall53457db2016-12-14 16:54:06 -0800194 }
195 if (!so)
196 return -ENOENT;
197
Jesse Hall00e61ff2017-04-07 16:48:02 -0700198 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800199 if (!hmi) {
200 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
201 dlclose(so);
202 return -EINVAL;
203 }
204 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
205 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
206 dlclose(so);
207 return -EINVAL;
208 }
209 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700210 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800211 return 0;
212}
213
Jesse Hall00e61ff2017-04-07 16:48:02 -0700214int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800215 ATRACE_CALL();
216
Yiwei Zhangd9861812019-02-13 11:51:55 -0800217 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700218 android::GpuStatsInfo::Driver::VULKAN);
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700219 return LoadDriver(nullptr, module);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700220}
221
222int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800223 ATRACE_CALL();
224
Jesse Hall00e61ff2017-04-07 16:48:02 -0700225 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
226 if (!ns)
227 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800228 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700229 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800230 int result = LoadDriver(ns, module);
231 if (result != 0) {
232 LOG_ALWAYS_FATAL(
233 "couldn't find an updated Vulkan implementation from %s",
234 android::GraphicsEnv::getInstance().getDriverPath().c_str());
235 }
236 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700237}
238
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800239bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800240 ATRACE_CALL();
241
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242 const nsecs_t openTime = systemTime();
243
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700244 if (hal_.ShouldUnloadBuiltinDriver()) {
245 hal_.UnloadBuiltinDriver();
246 }
247
248 if (hal_.dev_)
249 return true;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800250
251 // Use a stub device unless we successfully open a real HAL device.
252 hal_.dev_ = &stubhal::kDevice;
253
Jesse Hall53457db2016-12-14 16:54:06 -0800254 int result;
255 const hwvulkan_module_t* module = nullptr;
256
Jesse Hall00e61ff2017-04-07 16:48:02 -0700257 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800258 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700259 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800260 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800261 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800262 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700263 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800264 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800265 return true;
266 }
267
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800268
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800269 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800270 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800271 result =
272 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
273 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800274 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800275 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800276 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700277 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800278 // Any device with a Vulkan HAL should be able to open the device.
279 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
280 result);
281 return false;
282 }
283
284 hal_.dev_ = device;
285
Chia-I Wu31938252016-05-23 15:31:02 +0800286 hal_.InitDebugReportIndex();
287
Yiwei Zhangd9861812019-02-13 11:51:55 -0800288 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700289 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800290
Chia-I Wu31938252016-05-23 15:31:02 +0800291 return true;
292}
293
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700294bool Hal::ShouldUnloadBuiltinDriver() {
295 // Should not unload since the driver was not loaded
296 if (!hal_.dev_)
297 return false;
298
299 // Should not unload if stubhal is used on the device
300 if (hal_.dev_ == &stubhal::kDevice)
301 return false;
302
303 // Unload the driver if updated driver is chosen
304 if (android::GraphicsEnv::getInstance().getDriverNamespace())
305 return true;
306
307 return false;
308}
309
310void Hal::UnloadBuiltinDriver() {
311 ATRACE_CALL();
312
313 ALOGD("Unload builtin Vulkan driver.");
314
315 // Close the opened device
316 ALOG_ASSERT(!hal_.dev_->common.close(hal_.dev_->common),
317 "hw_device_t::close() failed.");
318
319 // Close the opened shared library in the hw_module_t
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700320 android_unload_sphal_library(hal_.dev_->common.module->dso);
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700321
322 hal_.dev_ = nullptr;
323 hal_.debug_report_index_ = -1;
324}
325
Chia-I Wu31938252016-05-23 15:31:02 +0800326bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800327 ATRACE_CALL();
328
Chia-I Wu31938252016-05-23 15:31:02 +0800329 uint32_t count;
330 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
331 VK_SUCCESS) {
332 ALOGE("failed to get HAL instance extension count");
333 return false;
334 }
335
336 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
337 malloc(sizeof(VkExtensionProperties) * count));
338 if (!exts) {
339 ALOGE("failed to allocate HAL instance extension array");
340 return false;
341 }
342
343 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
344 VK_SUCCESS) {
345 ALOGE("failed to enumerate HAL instance extensions");
346 free(exts);
347 return false;
348 }
349
350 for (uint32_t i = 0; i < count; i++) {
351 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
352 0) {
353 debug_report_index_ = static_cast<int>(i);
354 break;
355 }
356 }
357
358 free(exts);
359
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800360 return true;
361}
362
363CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700364 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800365 const VkAllocationCallbacks& allocator)
366 : is_instance_(true),
367 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000368 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700369 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800370 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800371 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700372 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800373
Chia-I Wu4901db72016-03-24 16:38:58 +0800374CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
375 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700376 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800377 const VkAllocationCallbacks& allocator)
378 : is_instance_(false),
379 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000380 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700381 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800382 physical_dev_(physical_dev),
383 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700384 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800385
386CreateInfoWrapper::~CreateInfoWrapper() {
387 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
388 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
389}
390
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800391VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700392 VkResult result = SanitizeApiVersion();
393 if (result == VK_SUCCESS)
394 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800395 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800396 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800397 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800398 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800399
400 return result;
401}
402
403const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800404CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800405 return hook_extensions_;
406}
407
408const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800409CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800410 return hal_extensions_;
411}
412
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800413CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
414 return &instance_info_;
415}
416
Chia-I Wu4901db72016-03-24 16:38:58 +0800417CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
418 return &dev_info_;
419}
420
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700421VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700422 if (!is_instance_ || !instance_info_.pApplicationInfo)
423 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700424
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700425 if (icd_api_version_ > VK_API_VERSION_1_0 ||
426 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
427 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700428
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700429 // override apiVersion to avoid error return from 1.0 icd
430 application_info_ = *instance_info_.pApplicationInfo;
431 application_info_.apiVersion = VK_API_VERSION_1_0;
432 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700433
434 return VK_SUCCESS;
435}
436
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800437VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800438 const struct StructHeader {
439 VkStructureType type;
440 const void* next;
441 } * header;
442
443 if (is_instance_) {
444 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
445
446 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
447 while (header &&
448 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
449 header = reinterpret_cast<const StructHeader*>(header->next);
450
451 instance_info_.pNext = header;
452 } else {
453 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
454
455 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
456 while (header &&
457 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
458 header = reinterpret_cast<const StructHeader*>(header->next);
459
460 dev_info_.pNext = header;
461 }
462
463 return VK_SUCCESS;
464}
465
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800466VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800467 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
468 : dev_info_.ppEnabledLayerNames;
469 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
470 : dev_info_.enabledLayerCount;
471
472 // remove all layers
473 layer_names = nullptr;
474 layer_count = 0;
475
476 return VK_SUCCESS;
477}
478
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800479VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800480 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
481 : dev_info_.ppEnabledExtensionNames;
482 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
483 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800484
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800485 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800486 if (result != VK_SUCCESS)
487 return result;
488
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700489 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700490 for (uint32_t i = 0; i < ext_count; i++) {
491 // Upon api downgrade, skip the promoted instance extensions in the
492 // first pass to avoid duplicate extensions.
493 const std::optional<uint32_t> version =
494 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700495 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700496 *version <= loader_api_version_)
497 continue;
498
499 FilterExtension(ext_names[i]);
500 }
501
502 // Enable the required extensions to support core functionalities.
503 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700504 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700505 for (const auto& promoted_extension : promoted_extensions)
506 FilterExtension(promoted_extension);
507 } else {
508 for (uint32_t i = 0; i < ext_count; i++)
509 FilterExtension(ext_names[i]);
510 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800511
Jesse Halld3d887a2018-03-05 13:34:45 -0800512 // Enable device extensions that contain physical-device commands, so that
513 // vkGetInstanceProcAddr will return those physical-device commands.
514 if (is_instance_) {
515 hook_extensions_.set(ProcHook::KHR_swapchain);
516 }
517
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700518 const uint32_t api_version =
519 is_instance_ ? loader_api_version_
520 : std::min(icd_api_version_, loader_api_version_);
521 switch (api_version) {
Trevor David Black628c41a2021-09-27 05:07:22 +0000522 case VK_API_VERSION_1_3:
523 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
524 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
525 [[clang::fallthrough]];
Ian Elliottfa7af492021-07-20 17:40:24 -0600526 case VK_API_VERSION_1_2:
527 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
528 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
529 [[clang::fallthrough]];
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700530 case VK_API_VERSION_1_1:
531 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
532 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
533 [[clang::fallthrough]];
534 case VK_API_VERSION_1_0:
535 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
536 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
537 break;
538 default:
539 ALOGE("Unknown API version[%u]", api_version);
540 break;
541 }
542
Chia-I Wu4901db72016-03-24 16:38:58 +0800543 ext_names = extension_filter_.names;
544 ext_count = extension_filter_.name_count;
545
546 return VK_SUCCESS;
547}
548
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800549VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800550 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800551 return Hal::Device().EnumerateInstanceExtensionProperties(
552 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800553 } else {
554 const auto& driver = GetData(physical_dev_).driver;
555 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
556 &count, nullptr);
557 }
558}
559
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800560VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800561 uint32_t& count,
562 VkExtensionProperties* props) const {
563 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800564 return Hal::Device().EnumerateInstanceExtensionProperties(
565 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800566 } else {
567 const auto& driver = GetData(physical_dev_).driver;
568 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
569 &count, props);
570 }
571}
572
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800573VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800574 // query extension count
575 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800576 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800577 if (result != VK_SUCCESS || count == 0)
578 return result;
579
580 auto& filter = extension_filter_;
581 filter.exts =
582 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
583 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
584 alignof(VkExtensionProperties),
585 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
586 if (!filter.exts)
587 return VK_ERROR_OUT_OF_HOST_MEMORY;
588
589 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800590 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800591 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
592 return result;
593
594 if (!count)
595 return VK_SUCCESS;
596
597 filter.ext_count = count;
598
599 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700600 if (is_instance_) {
601 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
602
603 // It requires enabling additional promoted extensions to downgrade api,
604 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700605 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700606 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700607 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700608 }
609
610 count = std::min(filter.ext_count, enabled_ext_count);
611 } else {
612 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
613 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700614
615 if (!count)
616 return VK_SUCCESS;
617
Chia-I Wu4901db72016-03-24 16:38:58 +0800618 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
619 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
620 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
621 if (!filter.names)
622 return VK_ERROR_OUT_OF_HOST_MEMORY;
623
624 return VK_SUCCESS;
625}
626
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800627void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800628 auto& filter = extension_filter_;
629
630 ProcHook::Extension ext_bit = GetProcHookExtension(name);
631 if (is_instance_) {
632 switch (ext_bit) {
633 case ProcHook::KHR_android_surface:
634 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600635 case ProcHook::KHR_surface_protected_capabilities:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700636 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300637 case ProcHook::KHR_get_surface_capabilities2:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600638 case ProcHook::GOOGLE_surfaceless_query:
Chris Forbes08c7bb12022-12-09 08:10:18 +1300639 case ProcHook::EXT_surface_maintenance1:
Chia-I Wu4901db72016-03-24 16:38:58 +0800640 hook_extensions_.set(ext_bit);
641 // return now as these extensions do not require HAL support
642 return;
643 case ProcHook::EXT_debug_report:
644 // both we and HAL can take part in
645 hook_extensions_.set(ext_bit);
646 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300647 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700648 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700649 case ProcHook::KHR_external_memory_capabilities:
650 case ProcHook::KHR_external_semaphore_capabilities:
651 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700652 case ProcHook::EXTENSION_UNKNOWN:
653 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800654 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700655
Yiwei Zhang23143102019-04-10 18:24:05 -0700656 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700657 case ProcHook::KHR_incremental_present:
658 case ProcHook::KHR_shared_presentable_image:
659 case ProcHook::KHR_swapchain:
660 case ProcHook::EXT_hdr_metadata:
Chris Forbes08c7bb12022-12-09 08:10:18 +1300661 case ProcHook::EXT_swapchain_maintenance1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700662 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
663 case ProcHook::ANDROID_native_buffer:
664 case ProcHook::GOOGLE_display_timing:
Chris Forbes08c7bb12022-12-09 08:10:18 +1300665 case ProcHook::KHR_external_fence_fd:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700666 case ProcHook::EXTENSION_CORE_1_0:
667 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700668 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000669 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700670 case ProcHook::EXTENSION_COUNT:
671 // Device and meta extensions. If we ever get here it's a bug in
672 // our code. But enumerating them lets us avoid having a default
673 // case, and default hides other bugs.
674 ALOGE(
675 "CreateInfoWrapper::FilterExtension: invalid instance "
676 "extension '%s'. FIX ME",
677 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800678 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700679
680 // Don't use a default case. Without it, -Wswitch will tell us
681 // at compile time if someone adds a new ProcHook extension but
682 // doesn't handle it above. That's a real bug that has
683 // not-immediately-obvious effects.
684 //
685 // default:
686 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800687 }
688 } else {
689 switch (ext_bit) {
690 case ProcHook::KHR_swapchain:
691 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
692 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
693 ext_bit = ProcHook::ANDROID_native_buffer;
694 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700695 case ProcHook::KHR_incremental_present:
Chris Forbesfa25e632017-02-22 12:36:02 +1300696 case ProcHook::KHR_shared_presentable_image:
Chris Forbes08c7bb12022-12-09 08:10:18 +1300697 case ProcHook::GOOGLE_display_timing:
Ian Elliott8a977262017-01-19 09:05:58 -0700698 hook_extensions_.set(ext_bit);
699 // return now as these extensions do not require HAL support
700 return;
Chris Forbes08c7bb12022-12-09 08:10:18 +1300701 case ProcHook::EXT_swapchain_maintenance1:
702 // map VK_KHR_swapchain_maintenance1 to KHR_external_fence_fd
703 name = VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME;
704 ext_bit = ProcHook::KHR_external_fence_fd;
705 break;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700706 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700707 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700708 hook_extensions_.set(ext_bit);
709 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700710 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chris Forbes08c7bb12022-12-09 08:10:18 +1300711 case ProcHook::KHR_external_fence_fd:
Chia-I Wu4901db72016-03-24 16:38:58 +0800712 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700713 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800714 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700715
716 case ProcHook::KHR_android_surface:
717 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700718 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700719 case ProcHook::KHR_external_memory_capabilities:
720 case ProcHook::KHR_external_semaphore_capabilities:
721 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700722 case ProcHook::KHR_get_surface_capabilities2:
723 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600724 case ProcHook::KHR_surface_protected_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700725 case ProcHook::EXT_debug_report:
726 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes08c7bb12022-12-09 08:10:18 +1300727 case ProcHook::EXT_surface_maintenance1:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600728 case ProcHook::GOOGLE_surfaceless_query:
Jesse Hall7f983a82018-03-29 14:46:45 -0700729 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700730 case ProcHook::EXTENSION_CORE_1_0:
731 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700732 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000733 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700734 case ProcHook::EXTENSION_COUNT:
735 // Instance and meta extensions. If we ever get here it's a bug
736 // in our code. But enumerating them lets us avoid having a
737 // default case, and default hides other bugs.
738 ALOGE(
739 "CreateInfoWrapper::FilterExtension: invalid device "
740 "extension '%s'. FIX ME",
741 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800742 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700743
744 // Don't use a default case. Without it, -Wswitch will tell us
745 // at compile time if someone adds a new ProcHook extension but
746 // doesn't handle it above. That's a real bug that has
747 // not-immediately-obvious effects.
748 //
749 // default:
750 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800751 }
752 }
753
754 for (uint32_t i = 0; i < filter.ext_count; i++) {
755 const VkExtensionProperties& props = filter.exts[i];
756 // ignore unknown extensions
757 if (strcmp(name, props.extensionName) != 0)
758 continue;
759
Chia-I Wu4901db72016-03-24 16:38:58 +0800760 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800761 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
762 if (ext_bit == ProcHook::ANDROID_native_buffer)
763 hook_extensions_.set(ProcHook::KHR_swapchain);
Chris Forbes08c7bb12022-12-09 08:10:18 +1300764 if (ext_bit == ProcHook::KHR_external_fence_fd)
765 hook_extensions_.set(ProcHook::EXT_swapchain_maintenance1);
Chia-I Wu1600e262016-04-12 09:40:06 +0800766
767 hal_extensions_.set(ext_bit);
768 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800769
770 break;
771 }
772}
773
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800774VKAPI_ATTR void* DefaultAllocate(void*,
775 size_t size,
776 size_t alignment,
777 VkSystemAllocationScope) {
778 void* ptr = nullptr;
779 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
780 // additionally requires that it be at least sizeof(void*).
781 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
782 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
783 ret, ptr);
784 return ret == 0 ? ptr : nullptr;
785}
786
787VKAPI_ATTR void* DefaultReallocate(void*,
788 void* ptr,
789 size_t size,
790 size_t alignment,
791 VkSystemAllocationScope) {
792 if (size == 0) {
793 free(ptr);
794 return nullptr;
795 }
796
Yiwei Zhanga885c062019-10-24 12:07:57 -0700797 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800798 // request is smaller than the existing chunk, we just continue using it.
799 // Right now the loader never reallocs, so this doesn't matter. If that
800 // changes, or if this code is copied into some other project, this should
801 // probably have a heuristic to allocate-copy-free when doing so will save
802 // "enough" space.
803 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
804 if (size <= old_size)
805 return ptr;
806
807 void* new_ptr = nullptr;
808 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
809 return nullptr;
810 if (ptr) {
811 memcpy(new_ptr, ptr, std::min(old_size, size));
812 free(ptr);
813 }
814 return new_ptr;
815}
816
817VKAPI_ATTR void DefaultFree(void*, void* ptr) {
818 ALOGD_CALLSTACK("Free: %p", ptr);
819 free(ptr);
820}
821
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800822InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
823 void* data_mem = allocator.pfnAllocation(
824 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
825 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
826 if (!data_mem)
827 return nullptr;
828
829 return new (data_mem) InstanceData(allocator);
830}
831
832void FreeInstanceData(InstanceData* data,
833 const VkAllocationCallbacks& allocator) {
834 data->~InstanceData();
835 allocator.pfnFree(allocator.pUserData, data);
836}
837
Chia-I Wu950d6e12016-05-03 09:12:35 +0800838DeviceData* AllocateDeviceData(
839 const VkAllocationCallbacks& allocator,
840 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800841 void* data_mem = allocator.pfnAllocation(
842 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
843 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
844 if (!data_mem)
845 return nullptr;
846
Chia-I Wu950d6e12016-05-03 09:12:35 +0800847 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800848}
849
850void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
851 data->~DeviceData();
852 allocator.pfnFree(allocator.pUserData, data);
853}
854
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800855} // anonymous namespace
856
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800857bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800858 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800859}
860
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800861const VkAllocationCallbacks& GetDefaultAllocator() {
862 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
863 .pUserData = nullptr,
864 .pfnAllocation = DefaultAllocate,
865 .pfnReallocation = DefaultReallocate,
866 .pfnFree = DefaultFree,
867 };
868
869 return kDefaultAllocCallbacks;
870}
871
Chia-I Wueb7db122016-03-24 09:11:06 +0800872PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
873 const ProcHook* hook = GetProcHook(pName);
874 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800875 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800876
877 if (!instance) {
878 if (hook->type == ProcHook::GLOBAL)
879 return hook->proc;
880
Chia-I Wu109f8982016-04-22 06:40:40 +0800881 // v0 layers expect
882 //
883 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
884 //
885 // to work.
886 if (strcmp(pName, "vkCreateDevice") == 0)
887 return hook->proc;
888
Chia-I Wueb7db122016-03-24 09:11:06 +0800889 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800890 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800891 pName);
892
Chia-I Wu109f8982016-04-22 06:40:40 +0800893 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800894 }
895
896 PFN_vkVoidFunction proc;
897
898 switch (hook->type) {
899 case ProcHook::INSTANCE:
900 proc = (GetData(instance).hook_extensions[hook->extension])
901 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800902 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800903 break;
904 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700905 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800906 ? hook->proc
907 : hook->checked_proc;
908 break;
909 default:
910 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800911 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800912 pName);
913 proc = nullptr;
914 break;
915 }
916
917 return proc;
918}
919
920PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
921 const ProcHook* hook = GetProcHook(pName);
922 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800923 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800924
925 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800926 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800927 return nullptr;
928 }
929
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800930 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
931 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800932}
933
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800934VkResult EnumerateInstanceExtensionProperties(
935 const char* pLayerName,
936 uint32_t* pPropertyCount,
937 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700938 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700939 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600940 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600941 loader_extensions.push_back(
942 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
943 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600944 loader_extensions.push_back({
945 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
946 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
947 loader_extensions.push_back({
948 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
949 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -0600950 loader_extensions.push_back(
951 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
952 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -0600953 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
954 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Chris Forbes08c7bb12022-12-09 08:10:18 +1300955 loader_extensions.push_back({
956 VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
957 VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600958
Chia-I Wu31938252016-05-23 15:31:02 +0800959 static const VkExtensionProperties loader_debug_report_extension = {
960 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
961 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800962
963 // enumerate our extensions first
964 if (!pLayerName && pProperties) {
965 uint32_t count = std::min(
966 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
967
Yiwei Zhang5e862202019-06-21 14:59:16 -0700968 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800969
970 if (count < loader_extensions.size()) {
971 *pPropertyCount = count;
972 return VK_INCOMPLETE;
973 }
974
975 pProperties += count;
976 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800977
978 if (Hal::Get().GetDebugReportIndex() < 0) {
979 if (!*pPropertyCount) {
980 *pPropertyCount = count;
981 return VK_INCOMPLETE;
982 }
983
984 pProperties[0] = loader_debug_report_extension;
985 pProperties += 1;
986 *pPropertyCount -= 1;
987 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800988 }
989
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800990 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800991 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800992 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800993 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800994
Chia-I Wu31938252016-05-23 15:31:02 +0800995 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
996 int idx = Hal::Get().GetDebugReportIndex();
997 if (idx < 0) {
998 *pPropertyCount += 1;
999 } else if (pProperties &&
1000 static_cast<uint32_t>(idx) < *pPropertyCount) {
1001 pProperties[idx].specVersion =
1002 std::min(pProperties[idx].specVersion,
1003 loader_debug_report_extension.specVersion);
1004 }
1005
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001006 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +08001007 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001008
1009 return result;
1010}
1011
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001012void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001013 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001014 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001015 ATRACE_CALL();
1016
Chris Forbesfa25e632017-02-22 12:36:02 +13001017 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001018 VkPhysicalDeviceProperties2 properties = {
1019 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001020 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001021 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001022 };
1023
1024#pragma clang diagnostic push
1025#pragma clang diagnostic ignored "-Wold-style-cast"
1026 presentation_properties->sType =
1027 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1028#pragma clang diagnostic pop
1029 presentation_properties->pNext = nullptr;
1030 presentation_properties->sharedImage = VK_FALSE;
1031
Chris Forbese056c122021-07-22 13:54:04 -07001032 const auto& driver = GetData(physicalDevice).driver;
1033
1034 if (driver.GetPhysicalDeviceProperties2) {
1035 // >= 1.1 driver, supports core GPDP2 entrypoint.
1036 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1037 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1038 // Old driver, but may support presentation properties
1039 // if we have the GPDP2 extension. Otherwise, no presentation
1040 // properties supported.
1041 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1042 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001043}
1044
Trevor David Black929e9cd2022-11-22 04:12:19 +00001045VkResult GetAndroidNativeBufferSpecVersion9Support(
1046 VkPhysicalDevice physicalDevice,
1047 bool& support) {
1048 support = false;
1049
Trevor David Black2cc44682022-03-09 00:31:38 +00001050 const InstanceData& data = GetData(physicalDevice);
1051
1052 // Call to get propertyCount
1053 uint32_t propertyCount = 0;
1054 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1055 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1056 physicalDevice, nullptr, &propertyCount, nullptr);
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 // Call to enumerate properties
1064 std::vector<VkExtensionProperties> properties(propertyCount);
1065 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1066 result = data.driver.EnumerateDeviceExtensionProperties(
1067 physicalDevice, nullptr, &propertyCount, properties.data());
1068 ATRACE_END();
1069
Trevor David Black929e9cd2022-11-22 04:12:19 +00001070 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1071 return result;
1072 }
1073
Trevor David Black2cc44682022-03-09 00:31:38 +00001074 for (uint32_t i = 0; i < propertyCount; i++) {
1075 auto& prop = properties[i];
1076
1077 if (strcmp(prop.extensionName,
1078 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1079 continue;
1080
1081 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001082 support = true;
1083 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001084 }
1085 }
1086
Trevor David Black929e9cd2022-11-22 04:12:19 +00001087 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001088}
1089
Chris Forbes08c7bb12022-12-09 08:10:18 +13001090bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1091 const auto& driver = GetData(physicalDevice).driver;
1092 if (!driver.GetPhysicalDeviceExternalFenceProperties)
1093 return false;
1094
1095 // Requires support for external fences imported from sync fds.
1096 // This is _almost_ universal on Android, but may be missing on
1097 // some extremely old drivers, or on strange implementations like
1098 // cuttlefish.
1099 VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1100 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1101 nullptr,
1102 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1103 };
1104 VkExternalFenceProperties fenceProperties = {
1105 VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1106 nullptr,
1107 0, 0, 0
1108 };
1109
1110 GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1111 if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1112 return true;
1113
1114 return false;
1115}
1116
Chia-I Wu01cf3052016-03-24 16:16:21 +08001117VkResult EnumerateDeviceExtensionProperties(
1118 VkPhysicalDevice physicalDevice,
1119 const char* pLayerName,
1120 uint32_t* pPropertyCount,
1121 VkExtensionProperties* pProperties) {
1122 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001123 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001124 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001125 loader_extensions.push_back({
1126 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1127 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001128
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001129 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001130 if (hdrBoardConfig) {
1131 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1132 VK_EXT_HDR_METADATA_SPEC_VERSION});
1133 }
1134
Chris Forbes16095002017-05-05 15:33:29 -07001135 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001136 QueryPresentationProperties(physicalDevice, &presentation_properties);
1137 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001138 loader_extensions.push_back({
1139 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1140 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001141 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001142
Ian Elliott5c34de22017-04-10 14:42:30 -06001143 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1144 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001145 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001146 loader_extensions.push_back({
1147 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1148 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1149 }
1150
Trevor David Black2cc44682022-03-09 00:31:38 +00001151 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1152 // support is provided by the driver
1153 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1154 swapchainCompFeats = {};
1155 swapchainCompFeats.sType =
1156 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1157 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001158 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001159 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1160 imageCompFeats.sType =
1161 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1162 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001163 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001164
1165 VkPhysicalDeviceFeatures2 feats2 = {};
1166 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1167 feats2.pNext = &imageCompFeats;
1168
Trevor David Black929e9cd2022-11-22 04:12:19 +00001169 const auto& driver = GetData(physicalDevice).driver;
1170 if (driver.GetPhysicalDeviceFeatures2 ||
1171 driver.GetPhysicalDeviceFeatures2KHR) {
1172 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1173 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001174
Trevor David Black929e9cd2022-11-22 04:12:19 +00001175 bool anb9 = false;
1176 VkResult result =
1177 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1178
1179 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1180 return result;
1181 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001182
1183 if (anb9 && imageCompFeats.imageCompressionControl) {
1184 loader_extensions.push_back(
1185 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1186 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1187 }
1188 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1189 loader_extensions.push_back(
1190 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1191 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1192 }
1193
Chris Forbes08c7bb12022-12-09 08:10:18 +13001194 if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1195 loader_extensions.push_back({
1196 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1197 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1198 }
1199
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001200 // enumerate our extensions first
1201 if (!pLayerName && pProperties) {
1202 uint32_t count = std::min(
1203 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1204
Yiwei Zhang5e862202019-06-21 14:59:16 -07001205 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001206
1207 if (count < loader_extensions.size()) {
1208 *pPropertyCount = count;
1209 return VK_INCOMPLETE;
1210 }
1211
1212 pProperties += count;
1213 *pPropertyCount -= count;
1214 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001215
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001216 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001217 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001218 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001219 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001220
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001221 if (pProperties) {
1222 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1223 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1224 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001225
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001226 if (strcmp(prop.extensionName,
1227 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1228 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001229
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001230 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1231 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001232
1233 if (prop.specVersion >= 8) {
1234 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1235 } else {
1236 prop.specVersion = 68;
1237 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001238 }
1239 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001240
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001241 // restore loader extension count
1242 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1243 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001244 }
1245
1246 return result;
1247}
1248
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001249VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1250 const VkAllocationCallbacks* pAllocator,
1251 VkInstance* pInstance) {
1252 const VkAllocationCallbacks& data_allocator =
1253 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1254
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001255 VkResult result = VK_SUCCESS;
1256 uint32_t icd_api_version = VK_API_VERSION_1_0;
1257 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1258 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1259 Hal::Device().GetInstanceProcAddr(nullptr,
1260 "vkEnumerateInstanceVersion"));
1261 if (pfn_enumerate_instance_version) {
1262 ATRACE_BEGIN("pfn_enumerate_instance_version");
1263 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1264 ATRACE_END();
1265 if (result != VK_SUCCESS)
1266 return result;
1267
Trevor David Blackb68a2252021-08-23 16:37:18 +00001268 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001269 }
1270
1271 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1272 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001273 if (result != VK_SUCCESS)
1274 return result;
1275
1276 InstanceData* data = AllocateInstanceData(data_allocator);
1277 if (!data)
1278 return VK_ERROR_OUT_OF_HOST_MEMORY;
1279
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001280 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001281
1282 // call into the driver
1283 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001284 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001285 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001286 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1287 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001288 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001289 if (result != VK_SUCCESS) {
1290 FreeInstanceData(data, data_allocator);
1291 return result;
1292 }
1293
1294 // initialize InstanceDriverTable
1295 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001296 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001297 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001298 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001299 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001300 if (data->driver.DestroyInstance)
1301 data->driver.DestroyInstance(instance, pAllocator);
1302
1303 FreeInstanceData(data, data_allocator);
1304
1305 return VK_ERROR_INCOMPATIBLE_DRIVER;
1306 }
1307
1308 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001309 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001310 if (!data->get_device_proc_addr) {
1311 data->driver.DestroyInstance(instance, pAllocator);
1312 FreeInstanceData(data, data_allocator);
1313
1314 return VK_ERROR_INCOMPATIBLE_DRIVER;
1315 }
1316
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001317 // TODO(b/259516419) avoid getting stats from hwui
1318 // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1319 // || (strcmp("android framework",
1320 // pCreateInfo->pApplicationInfo->pEngineName) != 0);
1321 const bool reportStats = true;
1322 if (reportStats) {
1323 // Set stats for Vulkan api version requested with application info
1324 if (pCreateInfo->pApplicationInfo) {
1325 const uint32_t vulkanApiVersion =
1326 pCreateInfo->pApplicationInfo->apiVersion;
1327 android::GraphicsEnv::getInstance().setTargetStats(
1328 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1329 vulkanApiVersion);
1330 }
1331
1332 // Update stats for the extensions requested
1333 android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1334 pCreateInfo->enabledExtensionCount,
1335 pCreateInfo->ppEnabledExtensionNames);
1336 }
1337
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001338 *pInstance = instance;
1339
1340 return VK_SUCCESS;
1341}
1342
1343void DestroyInstance(VkInstance instance,
1344 const VkAllocationCallbacks* pAllocator) {
1345 InstanceData& data = GetData(instance);
1346 data.driver.DestroyInstance(instance, pAllocator);
1347
1348 VkAllocationCallbacks local_allocator;
1349 if (!pAllocator) {
1350 local_allocator = data.allocator;
1351 pAllocator = &local_allocator;
1352 }
1353
1354 FreeInstanceData(&data, *pAllocator);
1355}
1356
Chia-I Wu4901db72016-03-24 16:38:58 +08001357VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1358 const VkDeviceCreateInfo* pCreateInfo,
1359 const VkAllocationCallbacks* pAllocator,
1360 VkDevice* pDevice) {
1361 const InstanceData& instance_data = GetData(physicalDevice);
1362 const VkAllocationCallbacks& data_allocator =
1363 (pAllocator) ? *pAllocator : instance_data.allocator;
1364
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001365 VkPhysicalDeviceProperties properties;
1366 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1367 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1368 &properties);
1369 ATRACE_END();
1370
1371 CreateInfoWrapper wrapper(
1372 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001373 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001374 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001375 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001376 if (result != VK_SUCCESS)
1377 return result;
1378
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001379 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001380 DeviceData* data = AllocateDeviceData(data_allocator,
1381 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001382 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001383 if (!data)
1384 return VK_ERROR_OUT_OF_HOST_MEMORY;
1385
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001386 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001387
1388 // call into the driver
1389 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001390 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001391 result = instance_data.driver.CreateDevice(
1392 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1393 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001394 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001395 if (result != VK_SUCCESS) {
1396 FreeDeviceData(data, data_allocator);
1397 return result;
1398 }
1399
1400 // initialize DeviceDriverTable
1401 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001402 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1403 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001404 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1405 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1406 if (data->driver.DestroyDevice)
1407 data->driver.DestroyDevice(dev, pAllocator);
1408
1409 FreeDeviceData(data, data_allocator);
1410
1411 return VK_ERROR_INCOMPATIBLE_DRIVER;
1412 }
Chris Forbesd8277912017-02-10 14:59:59 +13001413
Trevor David Black2cc44682022-03-09 00:31:38 +00001414 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001415 // entrypoints varies according to the spec version.
1416 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1417 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001418 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
1419 !data->driver.GetSwapchainGrallocUsage3ANDROID) {
1420 ALOGE(
1421 "Driver's implementation of ANDROID_native_buffer is broken;"
1422 " must expose at least one of "
1423 "vkGetSwapchainGrallocUsageANDROID or "
1424 "vkGetSwapchainGrallocUsage2ANDROID or "
1425 "vkGetSwapchainGrallocUsage3ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001426
1427 data->driver.DestroyDevice(dev, pAllocator);
1428 FreeDeviceData(data, data_allocator);
1429
1430 return VK_ERROR_INCOMPATIBLE_DRIVER;
1431 }
1432
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001433 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1434 // Log that the app is hitting software Vulkan implementation
1435 android::GraphicsEnv::getInstance().setTargetStats(
1436 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1437 }
1438
Jesse Halldc225072016-05-30 22:40:14 -07001439 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001440
1441 *pDevice = dev;
1442
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001443 // TODO(b/259516419) avoid getting stats from hwui
1444 const bool reportStats = true;
1445 if (reportStats) {
1446 android::GraphicsEnv::getInstance().setTargetStats(
1447 android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1448
1449 // Set stats for creating a Vulkan device and report features in use
1450 const VkPhysicalDeviceFeatures* pEnabledFeatures =
1451 pCreateInfo->pEnabledFeatures;
1452 if (!pEnabledFeatures) {
1453 // Use features from the chained VkPhysicalDeviceFeatures2
1454 // structure, if given
1455 const VkPhysicalDeviceFeatures2* features2 =
1456 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1457 pCreateInfo->pNext);
1458 while (features2 &&
1459 features2->sType !=
1460 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1461 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1462 features2->pNext);
1463 }
1464 if (features2) {
1465 pEnabledFeatures = &features2->features;
1466 }
1467 }
1468 const VkBool32* pFeatures =
1469 reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1470 if (pFeatures) {
1471 // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1472 // of them using pointer arithmetic here and save the features in a
1473 // 64-bit bitfield
1474 static_assert(
1475 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1476 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1477 "packing");
1478 static_assert(
1479 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1480 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1481 "packing");
1482 const int numFeatures =
1483 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1484
1485 uint64_t enableFeatureBits = 0;
1486 for (int i = 0; i < numFeatures; i++) {
1487 if (pFeatures[i] != VK_FALSE) {
1488 enableFeatureBits |= (uint64_t(1) << i);
1489 }
1490 }
1491 android::GraphicsEnv::getInstance().setTargetStats(
1492 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1493 enableFeatureBits);
1494 }
1495
1496 // Update stats for the extensions requested
1497 android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1498 pCreateInfo->enabledExtensionCount,
1499 pCreateInfo->ppEnabledExtensionNames);
1500 }
1501
Chia-I Wu4901db72016-03-24 16:38:58 +08001502 return VK_SUCCESS;
1503}
1504
1505void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1506 DeviceData& data = GetData(device);
1507 data.driver.DestroyDevice(device, pAllocator);
1508
1509 VkAllocationCallbacks local_allocator;
1510 if (!pAllocator) {
1511 local_allocator = data.allocator;
1512 pAllocator = &local_allocator;
1513 }
1514
1515 FreeDeviceData(&data, *pAllocator);
1516}
1517
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001518VkResult EnumeratePhysicalDevices(VkInstance instance,
1519 uint32_t* pPhysicalDeviceCount,
1520 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001521 ATRACE_CALL();
1522
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001523 const auto& data = GetData(instance);
1524
1525 VkResult result = data.driver.EnumeratePhysicalDevices(
1526 instance, pPhysicalDeviceCount, pPhysicalDevices);
1527 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1528 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1529 SetData(pPhysicalDevices[i], data);
1530 }
1531
1532 return result;
1533}
1534
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001535VkResult EnumeratePhysicalDeviceGroups(
1536 VkInstance instance,
1537 uint32_t* pPhysicalDeviceGroupCount,
1538 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001539 ATRACE_CALL();
1540
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001541 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001542 const auto& data = GetData(instance);
1543
Yiwei Zhange4f64172020-07-05 15:17:32 -07001544 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1545 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001546 uint32_t device_count = 0;
1547 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1548 if (result < 0)
1549 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001550
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001551 if (!pPhysicalDeviceGroupProperties) {
1552 *pPhysicalDeviceGroupCount = device_count;
1553 return result;
1554 }
1555
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001556 if (!device_count) {
1557 *pPhysicalDeviceGroupCount = 0;
1558 return result;
1559 }
Chad Versace32c087f2018-09-09 07:28:05 -07001560 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1561 if (!device_count)
1562 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001563
Yiwei Zhang5e862202019-06-21 14:59:16 -07001564 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001565 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001566 result =
1567 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001568 if (result < 0)
1569 return result;
1570
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001571 for (uint32_t i = 0; i < device_count; ++i) {
1572 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1573 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1574 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1575 }
1576 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001577 if (data.driver.EnumeratePhysicalDeviceGroups) {
1578 result = data.driver.EnumeratePhysicalDeviceGroups(
1579 instance, pPhysicalDeviceGroupCount,
1580 pPhysicalDeviceGroupProperties);
1581 } else {
1582 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1583 instance, pPhysicalDeviceGroupCount,
1584 pPhysicalDeviceGroupProperties);
1585 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001586 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1587 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1588 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1589 for (uint32_t j = 0;
1590 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1591 j++) {
1592 SetData(
1593 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001594 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001595 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001596 }
1597 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001598 }
1599
1600 return result;
1601}
1602
Chia-I Wuba0be412016-03-24 16:24:40 +08001603void GetDeviceQueue(VkDevice device,
1604 uint32_t queueFamilyIndex,
1605 uint32_t queueIndex,
1606 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001607 ATRACE_CALL();
1608
Chia-I Wuba0be412016-03-24 16:24:40 +08001609 const auto& data = GetData(device);
1610
1611 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1612 SetData(*pQueue, data);
1613}
1614
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001615void GetDeviceQueue2(VkDevice device,
1616 const VkDeviceQueueInfo2* pQueueInfo,
1617 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001618 ATRACE_CALL();
1619
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001620 const auto& data = GetData(device);
1621
1622 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001623 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001624}
1625
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001626VkResult AllocateCommandBuffers(
1627 VkDevice device,
1628 const VkCommandBufferAllocateInfo* pAllocateInfo,
1629 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001630 ATRACE_CALL();
1631
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001632 const auto& data = GetData(device);
1633
1634 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1635 pCommandBuffers);
1636 if (result == VK_SUCCESS) {
1637 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1638 SetData(pCommandBuffers[i], data);
1639 }
1640
1641 return result;
1642}
1643
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001644VkResult QueueSubmit(VkQueue queue,
1645 uint32_t submitCount,
1646 const VkSubmitInfo* pSubmits,
1647 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001648 ATRACE_CALL();
1649
1650 const auto& data = GetData(queue);
1651
1652 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1653}
1654
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001655void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1656 VkPhysicalDeviceFeatures2* pFeatures) {
1657 ATRACE_CALL();
1658
1659 const auto& driver = GetData(physicalDevice).driver;
1660
1661 if (driver.GetPhysicalDeviceFeatures2) {
1662 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001663 } else {
1664 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1665 }
1666
1667 // Conditionally add imageCompressionControlSwapchain if
1668 // imageCompressionControl is supported Check for imageCompressionControl in
1669 // the pChain
1670 bool imageCompressionControl = false;
1671 bool imageCompressionControlInChain = false;
1672 bool imageCompressionControlSwapchainInChain = false;
1673 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1674 while (pFeats) {
1675 switch (pFeats->sType) {
1676 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1677 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1678 compressionFeat = reinterpret_cast<
1679 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1680 pFeats);
1681 imageCompressionControl =
1682 compressionFeat->imageCompressionControl;
1683 imageCompressionControlInChain = true;
1684 } break;
1685
1686 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001687 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1688 compressionFeat = reinterpret_cast<
1689 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1690 pFeats);
1691 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001692 imageCompressionControlSwapchainInChain = true;
1693 } break;
1694
Chris Forbes08c7bb12022-12-09 08:10:18 +13001695 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1696 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1697 pFeats);
1698 smf->swapchainMaintenance1 = true;
1699 } break;
1700
Trevor David Black2cc44682022-03-09 00:31:38 +00001701 default:
1702 break;
1703 }
1704 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1705 }
1706
1707 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001708 return;
1709 }
1710
Trevor David Black2cc44682022-03-09 00:31:38 +00001711 // If not in pchain, explicitly query for imageCompressionControl
1712 if (!imageCompressionControlInChain) {
1713 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1714 imageCompFeats.sType =
1715 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1716 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001717 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001718
1719 VkPhysicalDeviceFeatures2 feats2 = {};
1720 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1721 feats2.pNext = &imageCompFeats;
1722
1723 if (driver.GetPhysicalDeviceFeatures2) {
1724 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1725 } else {
1726 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1727 }
1728
1729 imageCompressionControl = imageCompFeats.imageCompressionControl;
1730 }
1731
1732 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1733 if (imageCompressionControl) {
1734 pFeats = pFeatures;
1735 while (pFeats) {
1736 switch (pFeats->sType) {
1737 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1738 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1739 compressionFeat = reinterpret_cast<
1740 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1741 pFeats);
1742 compressionFeat->imageCompressionControlSwapchain = true;
1743 } break;
1744
1745 default:
1746 break;
1747 }
1748 pFeats =
1749 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1750 }
1751 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001752}
1753
1754void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1755 VkPhysicalDeviceProperties2* pProperties) {
1756 ATRACE_CALL();
1757
1758 const auto& driver = GetData(physicalDevice).driver;
1759
1760 if (driver.GetPhysicalDeviceProperties2) {
1761 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1762 return;
1763 }
1764
1765 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1766}
1767
1768void GetPhysicalDeviceFormatProperties2(
1769 VkPhysicalDevice physicalDevice,
1770 VkFormat format,
1771 VkFormatProperties2* pFormatProperties) {
1772 ATRACE_CALL();
1773
1774 const auto& driver = GetData(physicalDevice).driver;
1775
1776 if (driver.GetPhysicalDeviceFormatProperties2) {
1777 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1778 pFormatProperties);
1779 return;
1780 }
1781
1782 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1783 pFormatProperties);
1784}
1785
1786VkResult GetPhysicalDeviceImageFormatProperties2(
1787 VkPhysicalDevice physicalDevice,
1788 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1789 VkImageFormatProperties2* pImageFormatProperties) {
1790 ATRACE_CALL();
1791
1792 const auto& driver = GetData(physicalDevice).driver;
1793
1794 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1795 return driver.GetPhysicalDeviceImageFormatProperties2(
1796 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1797 }
1798
1799 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1800 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1801}
1802
1803void GetPhysicalDeviceQueueFamilyProperties2(
1804 VkPhysicalDevice physicalDevice,
1805 uint32_t* pQueueFamilyPropertyCount,
1806 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1807 ATRACE_CALL();
1808
1809 const auto& driver = GetData(physicalDevice).driver;
1810
1811 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1812 driver.GetPhysicalDeviceQueueFamilyProperties2(
1813 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1814 return;
1815 }
1816
1817 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1818 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1819}
1820
1821void GetPhysicalDeviceMemoryProperties2(
1822 VkPhysicalDevice physicalDevice,
1823 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1824 ATRACE_CALL();
1825
1826 const auto& driver = GetData(physicalDevice).driver;
1827
1828 if (driver.GetPhysicalDeviceMemoryProperties2) {
1829 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1830 pMemoryProperties);
1831 return;
1832 }
1833
1834 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1835 pMemoryProperties);
1836}
1837
1838void GetPhysicalDeviceSparseImageFormatProperties2(
1839 VkPhysicalDevice physicalDevice,
1840 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1841 uint32_t* pPropertyCount,
1842 VkSparseImageFormatProperties2* pProperties) {
1843 ATRACE_CALL();
1844
1845 const auto& driver = GetData(physicalDevice).driver;
1846
1847 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1848 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1849 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1850 return;
1851 }
1852
1853 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1854 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1855}
1856
Yiwei Zhange1f35012020-07-05 22:52:04 -07001857void GetPhysicalDeviceExternalBufferProperties(
1858 VkPhysicalDevice physicalDevice,
1859 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1860 VkExternalBufferProperties* pExternalBufferProperties) {
1861 ATRACE_CALL();
1862
1863 const auto& driver = GetData(physicalDevice).driver;
1864
1865 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1866 driver.GetPhysicalDeviceExternalBufferProperties(
1867 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1868 return;
1869 }
1870
1871 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1872 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1873 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1874 return;
1875 }
1876
1877 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1878 sizeof(VkExternalMemoryProperties));
1879}
1880
1881void GetPhysicalDeviceExternalSemaphoreProperties(
1882 VkPhysicalDevice physicalDevice,
1883 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1884 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1885 ATRACE_CALL();
1886
1887 const auto& driver = GetData(physicalDevice).driver;
1888
1889 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1890 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1891 physicalDevice, pExternalSemaphoreInfo,
1892 pExternalSemaphoreProperties);
1893 return;
1894 }
1895
1896 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1897 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1898 physicalDevice, pExternalSemaphoreInfo,
1899 pExternalSemaphoreProperties);
1900 return;
1901 }
1902
1903 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1904 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1905 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1906}
1907
1908void GetPhysicalDeviceExternalFenceProperties(
1909 VkPhysicalDevice physicalDevice,
1910 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1911 VkExternalFenceProperties* pExternalFenceProperties) {
1912 ATRACE_CALL();
1913
1914 const auto& driver = GetData(physicalDevice).driver;
1915
1916 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1917 driver.GetPhysicalDeviceExternalFenceProperties(
1918 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1919 return;
1920 }
1921
1922 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1923 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1924 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1925 return;
1926 }
1927
1928 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1929 pExternalFenceProperties->compatibleHandleTypes = 0;
1930 pExternalFenceProperties->externalFenceFeatures = 0;
1931}
1932
Chia-I Wu9d518162016-03-24 14:55:27 +08001933} // namespace driver
1934} // namespace vulkan