blob: f92078d8dcdf749875b2b77da9ff32b22c061cda [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:
Chia-I Wu4901db72016-03-24 16:38:58 +0800639 hook_extensions_.set(ext_bit);
640 // return now as these extensions do not require HAL support
641 return;
642 case ProcHook::EXT_debug_report:
643 // both we and HAL can take part in
644 hook_extensions_.set(ext_bit);
645 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300646 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700647 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700648 case ProcHook::KHR_external_memory_capabilities:
649 case ProcHook::KHR_external_semaphore_capabilities:
650 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700651 case ProcHook::EXTENSION_UNKNOWN:
652 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800653 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700654
Yiwei Zhang23143102019-04-10 18:24:05 -0700655 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700656 case ProcHook::KHR_incremental_present:
657 case ProcHook::KHR_shared_presentable_image:
658 case ProcHook::KHR_swapchain:
659 case ProcHook::EXT_hdr_metadata:
660 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
661 case ProcHook::ANDROID_native_buffer:
662 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700663 case ProcHook::EXTENSION_CORE_1_0:
664 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700665 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000666 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700667 case ProcHook::EXTENSION_COUNT:
668 // Device and meta extensions. If we ever get here it's a bug in
669 // our code. But enumerating them lets us avoid having a default
670 // case, and default hides other bugs.
671 ALOGE(
672 "CreateInfoWrapper::FilterExtension: invalid instance "
673 "extension '%s'. FIX ME",
674 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800675 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700676
677 // Don't use a default case. Without it, -Wswitch will tell us
678 // at compile time if someone adds a new ProcHook extension but
679 // doesn't handle it above. That's a real bug that has
680 // not-immediately-obvious effects.
681 //
682 // default:
683 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800684 }
685 } else {
686 switch (ext_bit) {
687 case ProcHook::KHR_swapchain:
688 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
689 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
690 ext_bit = ProcHook::ANDROID_native_buffer;
691 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700692 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700693 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300694 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700695 hook_extensions_.set(ext_bit);
696 // return now as these extensions do not require HAL support
697 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700698 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700699 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700700 hook_extensions_.set(ext_bit);
701 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700702 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800703 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700704 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800705 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700706
707 case ProcHook::KHR_android_surface:
708 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700709 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700710 case ProcHook::KHR_external_memory_capabilities:
711 case ProcHook::KHR_external_semaphore_capabilities:
712 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700713 case ProcHook::KHR_get_surface_capabilities2:
714 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600715 case ProcHook::KHR_surface_protected_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700716 case ProcHook::EXT_debug_report:
717 case ProcHook::EXT_swapchain_colorspace:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600718 case ProcHook::GOOGLE_surfaceless_query:
Jesse Hall7f983a82018-03-29 14:46:45 -0700719 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700720 case ProcHook::EXTENSION_CORE_1_0:
721 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700722 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000723 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700724 case ProcHook::EXTENSION_COUNT:
725 // Instance and meta extensions. If we ever get here it's a bug
726 // in our code. But enumerating them lets us avoid having a
727 // default case, and default hides other bugs.
728 ALOGE(
729 "CreateInfoWrapper::FilterExtension: invalid device "
730 "extension '%s'. FIX ME",
731 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800732 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700733
734 // Don't use a default case. Without it, -Wswitch will tell us
735 // at compile time if someone adds a new ProcHook extension but
736 // doesn't handle it above. That's a real bug that has
737 // not-immediately-obvious effects.
738 //
739 // default:
740 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800741 }
742 }
743
744 for (uint32_t i = 0; i < filter.ext_count; i++) {
745 const VkExtensionProperties& props = filter.exts[i];
746 // ignore unknown extensions
747 if (strcmp(name, props.extensionName) != 0)
748 continue;
749
Ian Elliott5b527a02023-08-10 13:32:55 -0600750 // Ignore duplicate extensions (see: b/288929054)
751 bool duplicate_entry = false;
752 for (uint32_t j = 0; j < filter.name_count; j++) {
753 if (strcmp(name, filter.names[j]) == 0) {
754 duplicate_entry = true;
755 break;
756 }
757 }
758 if (duplicate_entry == true)
759 continue;
760
Chia-I Wu4901db72016-03-24 16:38:58 +0800761 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800762 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
763 if (ext_bit == ProcHook::ANDROID_native_buffer)
764 hook_extensions_.set(ProcHook::KHR_swapchain);
765
766 hal_extensions_.set(ext_bit);
767 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800768
769 break;
770 }
771}
772
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800773VKAPI_ATTR void* DefaultAllocate(void*,
774 size_t size,
775 size_t alignment,
776 VkSystemAllocationScope) {
777 void* ptr = nullptr;
778 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
779 // additionally requires that it be at least sizeof(void*).
780 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
781 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
782 ret, ptr);
783 return ret == 0 ? ptr : nullptr;
784}
785
786VKAPI_ATTR void* DefaultReallocate(void*,
787 void* ptr,
788 size_t size,
789 size_t alignment,
790 VkSystemAllocationScope) {
791 if (size == 0) {
792 free(ptr);
793 return nullptr;
794 }
795
Yiwei Zhanga885c062019-10-24 12:07:57 -0700796 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800797 // request is smaller than the existing chunk, we just continue using it.
798 // Right now the loader never reallocs, so this doesn't matter. If that
799 // changes, or if this code is copied into some other project, this should
800 // probably have a heuristic to allocate-copy-free when doing so will save
801 // "enough" space.
802 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
803 if (size <= old_size)
804 return ptr;
805
806 void* new_ptr = nullptr;
807 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
808 return nullptr;
809 if (ptr) {
810 memcpy(new_ptr, ptr, std::min(old_size, size));
811 free(ptr);
812 }
813 return new_ptr;
814}
815
816VKAPI_ATTR void DefaultFree(void*, void* ptr) {
817 ALOGD_CALLSTACK("Free: %p", ptr);
818 free(ptr);
819}
820
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800821InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
822 void* data_mem = allocator.pfnAllocation(
823 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
824 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
825 if (!data_mem)
826 return nullptr;
827
828 return new (data_mem) InstanceData(allocator);
829}
830
831void FreeInstanceData(InstanceData* data,
832 const VkAllocationCallbacks& allocator) {
833 data->~InstanceData();
834 allocator.pfnFree(allocator.pUserData, data);
835}
836
Chia-I Wu950d6e12016-05-03 09:12:35 +0800837DeviceData* AllocateDeviceData(
838 const VkAllocationCallbacks& allocator,
839 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800840 void* data_mem = allocator.pfnAllocation(
841 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
842 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
843 if (!data_mem)
844 return nullptr;
845
Chia-I Wu950d6e12016-05-03 09:12:35 +0800846 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800847}
848
849void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
850 data->~DeviceData();
851 allocator.pfnFree(allocator.pUserData, data);
852}
853
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800854} // anonymous namespace
855
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800856bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800857 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800858}
859
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800860const VkAllocationCallbacks& GetDefaultAllocator() {
861 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
862 .pUserData = nullptr,
863 .pfnAllocation = DefaultAllocate,
864 .pfnReallocation = DefaultReallocate,
865 .pfnFree = DefaultFree,
866 };
867
868 return kDefaultAllocCallbacks;
869}
870
Chia-I Wueb7db122016-03-24 09:11:06 +0800871PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
872 const ProcHook* hook = GetProcHook(pName);
873 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800874 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800875
876 if (!instance) {
877 if (hook->type == ProcHook::GLOBAL)
878 return hook->proc;
879
Chia-I Wu109f8982016-04-22 06:40:40 +0800880 // v0 layers expect
881 //
882 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
883 //
884 // to work.
885 if (strcmp(pName, "vkCreateDevice") == 0)
886 return hook->proc;
887
Chia-I Wueb7db122016-03-24 09:11:06 +0800888 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800889 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800890 pName);
891
Chia-I Wu109f8982016-04-22 06:40:40 +0800892 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800893 }
894
895 PFN_vkVoidFunction proc;
896
897 switch (hook->type) {
898 case ProcHook::INSTANCE:
899 proc = (GetData(instance).hook_extensions[hook->extension])
900 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800901 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800902 break;
903 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700904 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800905 ? hook->proc
906 : hook->checked_proc;
907 break;
908 default:
909 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800910 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800911 pName);
912 proc = nullptr;
913 break;
914 }
915
916 return proc;
917}
918
919PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
920 const ProcHook* hook = GetProcHook(pName);
921 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800922 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800923
924 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800925 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800926 return nullptr;
927 }
928
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800929 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
930 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800931}
932
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800933VkResult EnumerateInstanceExtensionProperties(
934 const char* pLayerName,
935 uint32_t* pPropertyCount,
936 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700937 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700938 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600939 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600940 loader_extensions.push_back(
941 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
942 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600943 loader_extensions.push_back({
944 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
945 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
946 loader_extensions.push_back({
947 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
948 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -0600949 loader_extensions.push_back(
950 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
951 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -0600952 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
953 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600954
Chia-I Wu31938252016-05-23 15:31:02 +0800955 static const VkExtensionProperties loader_debug_report_extension = {
956 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
957 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800958
959 // enumerate our extensions first
960 if (!pLayerName && pProperties) {
961 uint32_t count = std::min(
962 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
963
Yiwei Zhang5e862202019-06-21 14:59:16 -0700964 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800965
966 if (count < loader_extensions.size()) {
967 *pPropertyCount = count;
968 return VK_INCOMPLETE;
969 }
970
971 pProperties += count;
972 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800973
974 if (Hal::Get().GetDebugReportIndex() < 0) {
975 if (!*pPropertyCount) {
976 *pPropertyCount = count;
977 return VK_INCOMPLETE;
978 }
979
980 pProperties[0] = loader_debug_report_extension;
981 pProperties += 1;
982 *pPropertyCount -= 1;
983 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800984 }
985
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800986 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800987 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800988 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800989 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800990
Chia-I Wu31938252016-05-23 15:31:02 +0800991 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
992 int idx = Hal::Get().GetDebugReportIndex();
993 if (idx < 0) {
994 *pPropertyCount += 1;
995 } else if (pProperties &&
996 static_cast<uint32_t>(idx) < *pPropertyCount) {
997 pProperties[idx].specVersion =
998 std::min(pProperties[idx].specVersion,
999 loader_debug_report_extension.specVersion);
1000 }
1001
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001002 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +08001003 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001004
1005 return result;
1006}
1007
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001008void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001009 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001010 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001011 ATRACE_CALL();
1012
Chris Forbesfa25e632017-02-22 12:36:02 +13001013 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001014 VkPhysicalDeviceProperties2 properties = {
1015 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001016 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001017 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001018 };
1019
1020#pragma clang diagnostic push
1021#pragma clang diagnostic ignored "-Wold-style-cast"
1022 presentation_properties->sType =
1023 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1024#pragma clang diagnostic pop
1025 presentation_properties->pNext = nullptr;
1026 presentation_properties->sharedImage = VK_FALSE;
1027
Chris Forbese056c122021-07-22 13:54:04 -07001028 const auto& driver = GetData(physicalDevice).driver;
1029
1030 if (driver.GetPhysicalDeviceProperties2) {
1031 // >= 1.1 driver, supports core GPDP2 entrypoint.
1032 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1033 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1034 // Old driver, but may support presentation properties
1035 // if we have the GPDP2 extension. Otherwise, no presentation
1036 // properties supported.
1037 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1038 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001039}
1040
Trevor David Black929e9cd2022-11-22 04:12:19 +00001041VkResult GetAndroidNativeBufferSpecVersion9Support(
1042 VkPhysicalDevice physicalDevice,
1043 bool& support) {
1044 support = false;
1045
Trevor David Black2cc44682022-03-09 00:31:38 +00001046 const InstanceData& data = GetData(physicalDevice);
1047
1048 // Call to get propertyCount
1049 uint32_t propertyCount = 0;
1050 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1051 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1052 physicalDevice, nullptr, &propertyCount, nullptr);
1053 ATRACE_END();
1054
Trevor David Black929e9cd2022-11-22 04:12:19 +00001055 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1056 return result;
1057 }
1058
Trevor David Black2cc44682022-03-09 00:31:38 +00001059 // Call to enumerate properties
1060 std::vector<VkExtensionProperties> properties(propertyCount);
1061 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1062 result = data.driver.EnumerateDeviceExtensionProperties(
1063 physicalDevice, nullptr, &propertyCount, properties.data());
1064 ATRACE_END();
1065
Trevor David Black929e9cd2022-11-22 04:12:19 +00001066 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1067 return result;
1068 }
1069
Trevor David Black2cc44682022-03-09 00:31:38 +00001070 for (uint32_t i = 0; i < propertyCount; i++) {
1071 auto& prop = properties[i];
1072
1073 if (strcmp(prop.extensionName,
1074 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1075 continue;
1076
1077 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001078 support = true;
1079 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001080 }
1081 }
1082
Trevor David Black929e9cd2022-11-22 04:12:19 +00001083 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001084}
1085
Chia-I Wu01cf3052016-03-24 16:16:21 +08001086VkResult EnumerateDeviceExtensionProperties(
1087 VkPhysicalDevice physicalDevice,
1088 const char* pLayerName,
1089 uint32_t* pPropertyCount,
1090 VkExtensionProperties* pProperties) {
1091 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001092 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001093 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001094 loader_extensions.push_back({
1095 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1096 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001097
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001098 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001099 if (hdrBoardConfig) {
1100 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1101 VK_EXT_HDR_METADATA_SPEC_VERSION});
1102 }
1103
Chris Forbes16095002017-05-05 15:33:29 -07001104 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001105 QueryPresentationProperties(physicalDevice, &presentation_properties);
1106 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001107 loader_extensions.push_back({
1108 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1109 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001110 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001111
Ian Elliott5c34de22017-04-10 14:42:30 -06001112 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1113 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001114 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001115 loader_extensions.push_back({
1116 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1117 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1118 }
1119
Trevor David Black2cc44682022-03-09 00:31:38 +00001120 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1121 // support is provided by the driver
1122 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1123 swapchainCompFeats = {};
1124 swapchainCompFeats.sType =
1125 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1126 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001127 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001128 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1129 imageCompFeats.sType =
1130 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1131 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001132 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001133
1134 VkPhysicalDeviceFeatures2 feats2 = {};
1135 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1136 feats2.pNext = &imageCompFeats;
1137
Trevor David Black929e9cd2022-11-22 04:12:19 +00001138 const auto& driver = GetData(physicalDevice).driver;
1139 if (driver.GetPhysicalDeviceFeatures2 ||
1140 driver.GetPhysicalDeviceFeatures2KHR) {
1141 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1142 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001143
Trevor David Black929e9cd2022-11-22 04:12:19 +00001144 bool anb9 = false;
1145 VkResult result =
1146 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1147
1148 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1149 return result;
1150 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001151
1152 if (anb9 && imageCompFeats.imageCompressionControl) {
1153 loader_extensions.push_back(
1154 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1155 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1156 }
1157 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1158 loader_extensions.push_back(
1159 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1160 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1161 }
1162
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001163 // enumerate our extensions first
1164 if (!pLayerName && pProperties) {
1165 uint32_t count = std::min(
1166 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1167
Yiwei Zhang5e862202019-06-21 14:59:16 -07001168 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001169
1170 if (count < loader_extensions.size()) {
1171 *pPropertyCount = count;
1172 return VK_INCOMPLETE;
1173 }
1174
1175 pProperties += count;
1176 *pPropertyCount -= count;
1177 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001178
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001179 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001180 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001181 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001182 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001183
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001184 if (pProperties) {
1185 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1186 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1187 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001188
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001189 if (strcmp(prop.extensionName,
1190 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1191 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001192
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001193 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1194 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001195
1196 if (prop.specVersion >= 8) {
1197 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1198 } else {
1199 prop.specVersion = 68;
1200 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001201 }
1202 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001203
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001204 // restore loader extension count
1205 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1206 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001207 }
1208
1209 return result;
1210}
1211
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001212VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1213 const VkAllocationCallbacks* pAllocator,
1214 VkInstance* pInstance) {
1215 const VkAllocationCallbacks& data_allocator =
1216 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1217
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001218 VkResult result = VK_SUCCESS;
1219 uint32_t icd_api_version = VK_API_VERSION_1_0;
1220 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1221 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1222 Hal::Device().GetInstanceProcAddr(nullptr,
1223 "vkEnumerateInstanceVersion"));
1224 if (pfn_enumerate_instance_version) {
1225 ATRACE_BEGIN("pfn_enumerate_instance_version");
1226 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1227 ATRACE_END();
1228 if (result != VK_SUCCESS)
1229 return result;
1230
Trevor David Blackb68a2252021-08-23 16:37:18 +00001231 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001232 }
1233
1234 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1235 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001236 if (result != VK_SUCCESS)
1237 return result;
1238
1239 InstanceData* data = AllocateInstanceData(data_allocator);
1240 if (!data)
1241 return VK_ERROR_OUT_OF_HOST_MEMORY;
1242
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001243 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001244
1245 // call into the driver
1246 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001247 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001248 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001249 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1250 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001251 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001252 if (result != VK_SUCCESS) {
1253 FreeInstanceData(data, data_allocator);
1254 return result;
1255 }
1256
1257 // initialize InstanceDriverTable
1258 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001259 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001260 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001261 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001262 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001263 if (data->driver.DestroyInstance)
1264 data->driver.DestroyInstance(instance, pAllocator);
1265
1266 FreeInstanceData(data, data_allocator);
1267
1268 return VK_ERROR_INCOMPATIBLE_DRIVER;
1269 }
1270
1271 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001272 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001273 if (!data->get_device_proc_addr) {
1274 data->driver.DestroyInstance(instance, pAllocator);
1275 FreeInstanceData(data, data_allocator);
1276
1277 return VK_ERROR_INCOMPATIBLE_DRIVER;
1278 }
1279
1280 *pInstance = instance;
1281
1282 return VK_SUCCESS;
1283}
1284
1285void DestroyInstance(VkInstance instance,
1286 const VkAllocationCallbacks* pAllocator) {
1287 InstanceData& data = GetData(instance);
1288 data.driver.DestroyInstance(instance, pAllocator);
1289
1290 VkAllocationCallbacks local_allocator;
1291 if (!pAllocator) {
1292 local_allocator = data.allocator;
1293 pAllocator = &local_allocator;
1294 }
1295
1296 FreeInstanceData(&data, *pAllocator);
1297}
1298
Chia-I Wu4901db72016-03-24 16:38:58 +08001299VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1300 const VkDeviceCreateInfo* pCreateInfo,
1301 const VkAllocationCallbacks* pAllocator,
1302 VkDevice* pDevice) {
1303 const InstanceData& instance_data = GetData(physicalDevice);
1304 const VkAllocationCallbacks& data_allocator =
1305 (pAllocator) ? *pAllocator : instance_data.allocator;
1306
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001307 VkPhysicalDeviceProperties properties;
1308 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1309 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1310 &properties);
1311 ATRACE_END();
1312
1313 CreateInfoWrapper wrapper(
1314 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001315 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001316 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001317 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001318 if (result != VK_SUCCESS)
1319 return result;
1320
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001321 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001322 DeviceData* data = AllocateDeviceData(data_allocator,
1323 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001324 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001325 if (!data)
1326 return VK_ERROR_OUT_OF_HOST_MEMORY;
1327
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001328 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001329
1330 // call into the driver
1331 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001332 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001333 result = instance_data.driver.CreateDevice(
1334 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1335 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001336 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001337 if (result != VK_SUCCESS) {
1338 FreeDeviceData(data, data_allocator);
1339 return result;
1340 }
1341
1342 // initialize DeviceDriverTable
1343 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001344 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1345 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001346 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1347 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1348 if (data->driver.DestroyDevice)
1349 data->driver.DestroyDevice(dev, pAllocator);
1350
1351 FreeDeviceData(data, data_allocator);
1352
1353 return VK_ERROR_INCOMPATIBLE_DRIVER;
1354 }
Chris Forbesd8277912017-02-10 14:59:59 +13001355
Trevor David Black2cc44682022-03-09 00:31:38 +00001356 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001357 // entrypoints varies according to the spec version.
1358 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1359 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001360 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
1361 !data->driver.GetSwapchainGrallocUsage3ANDROID) {
1362 ALOGE(
1363 "Driver's implementation of ANDROID_native_buffer is broken;"
1364 " must expose at least one of "
1365 "vkGetSwapchainGrallocUsageANDROID or "
1366 "vkGetSwapchainGrallocUsage2ANDROID or "
1367 "vkGetSwapchainGrallocUsage3ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001368
1369 data->driver.DestroyDevice(dev, pAllocator);
1370 FreeDeviceData(data, data_allocator);
1371
1372 return VK_ERROR_INCOMPATIBLE_DRIVER;
1373 }
1374
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001375 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1376 // Log that the app is hitting software Vulkan implementation
1377 android::GraphicsEnv::getInstance().setTargetStats(
1378 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1379 }
1380
Jesse Halldc225072016-05-30 22:40:14 -07001381 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001382
1383 *pDevice = dev;
1384
1385 return VK_SUCCESS;
1386}
1387
1388void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1389 DeviceData& data = GetData(device);
1390 data.driver.DestroyDevice(device, pAllocator);
1391
1392 VkAllocationCallbacks local_allocator;
1393 if (!pAllocator) {
1394 local_allocator = data.allocator;
1395 pAllocator = &local_allocator;
1396 }
1397
1398 FreeDeviceData(&data, *pAllocator);
1399}
1400
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001401VkResult EnumeratePhysicalDevices(VkInstance instance,
1402 uint32_t* pPhysicalDeviceCount,
1403 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001404 ATRACE_CALL();
1405
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001406 const auto& data = GetData(instance);
1407
1408 VkResult result = data.driver.EnumeratePhysicalDevices(
1409 instance, pPhysicalDeviceCount, pPhysicalDevices);
1410 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1411 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1412 SetData(pPhysicalDevices[i], data);
1413 }
1414
1415 return result;
1416}
1417
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001418VkResult EnumeratePhysicalDeviceGroups(
1419 VkInstance instance,
1420 uint32_t* pPhysicalDeviceGroupCount,
1421 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001422 ATRACE_CALL();
1423
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001424 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001425 const auto& data = GetData(instance);
1426
Yiwei Zhange4f64172020-07-05 15:17:32 -07001427 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1428 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001429 uint32_t device_count = 0;
1430 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1431 if (result < 0)
1432 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001433
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001434 if (!pPhysicalDeviceGroupProperties) {
1435 *pPhysicalDeviceGroupCount = device_count;
1436 return result;
1437 }
1438
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001439 if (!device_count) {
1440 *pPhysicalDeviceGroupCount = 0;
1441 return result;
1442 }
Chad Versace32c087f2018-09-09 07:28:05 -07001443 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1444 if (!device_count)
1445 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001446
Yiwei Zhang5e862202019-06-21 14:59:16 -07001447 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001448 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001449 result =
1450 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001451 if (result < 0)
1452 return result;
1453
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001454 for (uint32_t i = 0; i < device_count; ++i) {
1455 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1456 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1457 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1458 }
1459 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001460 if (data.driver.EnumeratePhysicalDeviceGroups) {
1461 result = data.driver.EnumeratePhysicalDeviceGroups(
1462 instance, pPhysicalDeviceGroupCount,
1463 pPhysicalDeviceGroupProperties);
1464 } else {
1465 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1466 instance, pPhysicalDeviceGroupCount,
1467 pPhysicalDeviceGroupProperties);
1468 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001469 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1470 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1471 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1472 for (uint32_t j = 0;
1473 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1474 j++) {
1475 SetData(
1476 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001477 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001478 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001479 }
1480 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001481 }
1482
1483 return result;
1484}
1485
Chia-I Wuba0be412016-03-24 16:24:40 +08001486void GetDeviceQueue(VkDevice device,
1487 uint32_t queueFamilyIndex,
1488 uint32_t queueIndex,
1489 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001490 ATRACE_CALL();
1491
Chia-I Wuba0be412016-03-24 16:24:40 +08001492 const auto& data = GetData(device);
1493
1494 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1495 SetData(*pQueue, data);
1496}
1497
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001498void GetDeviceQueue2(VkDevice device,
1499 const VkDeviceQueueInfo2* pQueueInfo,
1500 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001501 ATRACE_CALL();
1502
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001503 const auto& data = GetData(device);
1504
1505 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001506 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001507}
1508
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001509VkResult AllocateCommandBuffers(
1510 VkDevice device,
1511 const VkCommandBufferAllocateInfo* pAllocateInfo,
1512 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001513 ATRACE_CALL();
1514
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001515 const auto& data = GetData(device);
1516
1517 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1518 pCommandBuffers);
1519 if (result == VK_SUCCESS) {
1520 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1521 SetData(pCommandBuffers[i], data);
1522 }
1523
1524 return result;
1525}
1526
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001527VkResult QueueSubmit(VkQueue queue,
1528 uint32_t submitCount,
1529 const VkSubmitInfo* pSubmits,
1530 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001531 ATRACE_CALL();
1532
1533 const auto& data = GetData(queue);
1534
1535 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1536}
1537
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001538void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1539 VkPhysicalDeviceFeatures2* pFeatures) {
1540 ATRACE_CALL();
1541
1542 const auto& driver = GetData(physicalDevice).driver;
1543
1544 if (driver.GetPhysicalDeviceFeatures2) {
1545 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001546 } else {
1547 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1548 }
1549
1550 // Conditionally add imageCompressionControlSwapchain if
1551 // imageCompressionControl is supported Check for imageCompressionControl in
1552 // the pChain
1553 bool imageCompressionControl = false;
1554 bool imageCompressionControlInChain = false;
1555 bool imageCompressionControlSwapchainInChain = false;
1556 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1557 while (pFeats) {
1558 switch (pFeats->sType) {
1559 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1560 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1561 compressionFeat = reinterpret_cast<
1562 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1563 pFeats);
1564 imageCompressionControl =
1565 compressionFeat->imageCompressionControl;
1566 imageCompressionControlInChain = true;
1567 } break;
1568
1569 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001570 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1571 compressionFeat = reinterpret_cast<
1572 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1573 pFeats);
1574 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001575 imageCompressionControlSwapchainInChain = true;
1576 } break;
1577
1578 default:
1579 break;
1580 }
1581 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1582 }
1583
1584 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001585 return;
1586 }
1587
Trevor David Black2cc44682022-03-09 00:31:38 +00001588 // If not in pchain, explicitly query for imageCompressionControl
1589 if (!imageCompressionControlInChain) {
1590 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1591 imageCompFeats.sType =
1592 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1593 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001594 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001595
1596 VkPhysicalDeviceFeatures2 feats2 = {};
1597 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1598 feats2.pNext = &imageCompFeats;
1599
1600 if (driver.GetPhysicalDeviceFeatures2) {
1601 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1602 } else {
1603 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1604 }
1605
1606 imageCompressionControl = imageCompFeats.imageCompressionControl;
1607 }
1608
1609 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1610 if (imageCompressionControl) {
1611 pFeats = pFeatures;
1612 while (pFeats) {
1613 switch (pFeats->sType) {
1614 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1615 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1616 compressionFeat = reinterpret_cast<
1617 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1618 pFeats);
1619 compressionFeat->imageCompressionControlSwapchain = true;
1620 } break;
1621
1622 default:
1623 break;
1624 }
1625 pFeats =
1626 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1627 }
1628 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001629}
1630
1631void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1632 VkPhysicalDeviceProperties2* pProperties) {
1633 ATRACE_CALL();
1634
1635 const auto& driver = GetData(physicalDevice).driver;
1636
1637 if (driver.GetPhysicalDeviceProperties2) {
1638 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1639 return;
1640 }
1641
1642 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1643}
1644
1645void GetPhysicalDeviceFormatProperties2(
1646 VkPhysicalDevice physicalDevice,
1647 VkFormat format,
1648 VkFormatProperties2* pFormatProperties) {
1649 ATRACE_CALL();
1650
1651 const auto& driver = GetData(physicalDevice).driver;
1652
1653 if (driver.GetPhysicalDeviceFormatProperties2) {
1654 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1655 pFormatProperties);
1656 return;
1657 }
1658
1659 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1660 pFormatProperties);
1661}
1662
1663VkResult GetPhysicalDeviceImageFormatProperties2(
1664 VkPhysicalDevice physicalDevice,
1665 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1666 VkImageFormatProperties2* pImageFormatProperties) {
1667 ATRACE_CALL();
1668
1669 const auto& driver = GetData(physicalDevice).driver;
1670
1671 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1672 return driver.GetPhysicalDeviceImageFormatProperties2(
1673 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1674 }
1675
1676 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1677 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1678}
1679
1680void GetPhysicalDeviceQueueFamilyProperties2(
1681 VkPhysicalDevice physicalDevice,
1682 uint32_t* pQueueFamilyPropertyCount,
1683 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1684 ATRACE_CALL();
1685
1686 const auto& driver = GetData(physicalDevice).driver;
1687
1688 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1689 driver.GetPhysicalDeviceQueueFamilyProperties2(
1690 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1691 return;
1692 }
1693
1694 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1695 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1696}
1697
1698void GetPhysicalDeviceMemoryProperties2(
1699 VkPhysicalDevice physicalDevice,
1700 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1701 ATRACE_CALL();
1702
1703 const auto& driver = GetData(physicalDevice).driver;
1704
1705 if (driver.GetPhysicalDeviceMemoryProperties2) {
1706 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1707 pMemoryProperties);
1708 return;
1709 }
1710
1711 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1712 pMemoryProperties);
1713}
1714
1715void GetPhysicalDeviceSparseImageFormatProperties2(
1716 VkPhysicalDevice physicalDevice,
1717 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1718 uint32_t* pPropertyCount,
1719 VkSparseImageFormatProperties2* pProperties) {
1720 ATRACE_CALL();
1721
1722 const auto& driver = GetData(physicalDevice).driver;
1723
1724 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1725 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1726 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1727 return;
1728 }
1729
1730 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1731 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1732}
1733
Yiwei Zhange1f35012020-07-05 22:52:04 -07001734void GetPhysicalDeviceExternalBufferProperties(
1735 VkPhysicalDevice physicalDevice,
1736 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1737 VkExternalBufferProperties* pExternalBufferProperties) {
1738 ATRACE_CALL();
1739
1740 const auto& driver = GetData(physicalDevice).driver;
1741
1742 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1743 driver.GetPhysicalDeviceExternalBufferProperties(
1744 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1745 return;
1746 }
1747
1748 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1749 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1750 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1751 return;
1752 }
1753
1754 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1755 sizeof(VkExternalMemoryProperties));
1756}
1757
1758void GetPhysicalDeviceExternalSemaphoreProperties(
1759 VkPhysicalDevice physicalDevice,
1760 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1761 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1762 ATRACE_CALL();
1763
1764 const auto& driver = GetData(physicalDevice).driver;
1765
1766 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1767 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1768 physicalDevice, pExternalSemaphoreInfo,
1769 pExternalSemaphoreProperties);
1770 return;
1771 }
1772
1773 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1774 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1775 physicalDevice, pExternalSemaphoreInfo,
1776 pExternalSemaphoreProperties);
1777 return;
1778 }
1779
1780 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1781 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1782 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1783}
1784
1785void GetPhysicalDeviceExternalFenceProperties(
1786 VkPhysicalDevice physicalDevice,
1787 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1788 VkExternalFenceProperties* pExternalFenceProperties) {
1789 ATRACE_CALL();
1790
1791 const auto& driver = GetData(physicalDevice).driver;
1792
1793 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1794 driver.GetPhysicalDeviceExternalFenceProperties(
1795 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1796 return;
1797 }
1798
1799 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1800 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1801 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1802 return;
1803 }
1804
1805 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1806 pExternalFenceProperties->compatibleHandleTypes = 0;
1807 pExternalFenceProperties->externalFenceFeatures = 0;
1808}
1809
Chia-I Wu9d518162016-03-24 14:55:27 +08001810} // namespace driver
1811} // namespace vulkan