blob: 0b5bb1504c0f3efefaef24110496740150da073c [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);
Jooyung Han27e31082023-11-13 13:50:42 +0900186 if (!so) {
187 ALOGE(
188 "Could not load %s from updatable gfx driver namespace: "
189 "%s.",
190 lib_name.c_str(), dlerror());
191 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700192 } else {
193 // load built-in driver
194 so = android_load_sphal_library(lib_name.c_str(), LIB_DL_FLAGS);
Jesse Hall53457db2016-12-14 16:54:06 -0800195 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700196 if (so)
197 break;
Jesse Hall53457db2016-12-14 16:54:06 -0800198 }
199 if (!so)
200 return -ENOENT;
201
Jesse Hall00e61ff2017-04-07 16:48:02 -0700202 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800203 if (!hmi) {
204 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
205 dlclose(so);
206 return -EINVAL;
207 }
208 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
209 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
210 dlclose(so);
211 return -EINVAL;
212 }
213 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700214 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800215 return 0;
216}
217
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800219 ATRACE_CALL();
220
Yiwei Zhangd9861812019-02-13 11:51:55 -0800221 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700222 android::GpuStatsInfo::Driver::VULKAN);
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700223 return LoadDriver(nullptr, module);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700224}
225
226int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800227 ATRACE_CALL();
228
Jesse Hall00e61ff2017-04-07 16:48:02 -0700229 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
230 if (!ns)
231 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800232 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700233 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800234 int result = LoadDriver(ns, module);
235 if (result != 0) {
236 LOG_ALWAYS_FATAL(
237 "couldn't find an updated Vulkan implementation from %s",
238 android::GraphicsEnv::getInstance().getDriverPath().c_str());
239 }
240 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700241}
242
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800243bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800244 ATRACE_CALL();
245
Yiwei Zhangd9861812019-02-13 11:51:55 -0800246 const nsecs_t openTime = systemTime();
247
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700248 if (hal_.ShouldUnloadBuiltinDriver()) {
249 hal_.UnloadBuiltinDriver();
250 }
251
252 if (hal_.dev_)
253 return true;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800254
255 // Use a stub device unless we successfully open a real HAL device.
256 hal_.dev_ = &stubhal::kDevice;
257
Jesse Hall53457db2016-12-14 16:54:06 -0800258 int result;
259 const hwvulkan_module_t* module = nullptr;
260
Jesse Hall00e61ff2017-04-07 16:48:02 -0700261 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800262 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700263 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800264 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800265 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800266 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700267 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800268 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800269 return true;
270 }
271
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800272
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800273 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800274 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800275 result =
276 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
277 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800278 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800279 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800280 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700281 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800282 // Any device with a Vulkan HAL should be able to open the device.
283 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
284 result);
285 return false;
286 }
287
288 hal_.dev_ = device;
289
Chia-I Wu31938252016-05-23 15:31:02 +0800290 hal_.InitDebugReportIndex();
291
Yiwei Zhangd9861812019-02-13 11:51:55 -0800292 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700293 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800294
Chia-I Wu31938252016-05-23 15:31:02 +0800295 return true;
296}
297
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700298bool Hal::ShouldUnloadBuiltinDriver() {
299 // Should not unload since the driver was not loaded
300 if (!hal_.dev_)
301 return false;
302
303 // Should not unload if stubhal is used on the device
304 if (hal_.dev_ == &stubhal::kDevice)
305 return false;
306
307 // Unload the driver if updated driver is chosen
308 if (android::GraphicsEnv::getInstance().getDriverNamespace())
309 return true;
310
311 return false;
312}
313
314void Hal::UnloadBuiltinDriver() {
315 ATRACE_CALL();
316
317 ALOGD("Unload builtin Vulkan driver.");
318
319 // Close the opened device
320 ALOG_ASSERT(!hal_.dev_->common.close(hal_.dev_->common),
321 "hw_device_t::close() failed.");
322
323 // Close the opened shared library in the hw_module_t
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700324 android_unload_sphal_library(hal_.dev_->common.module->dso);
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700325
326 hal_.dev_ = nullptr;
327 hal_.debug_report_index_ = -1;
328}
329
Chia-I Wu31938252016-05-23 15:31:02 +0800330bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800331 ATRACE_CALL();
332
Chia-I Wu31938252016-05-23 15:31:02 +0800333 uint32_t count;
334 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
335 VK_SUCCESS) {
336 ALOGE("failed to get HAL instance extension count");
337 return false;
338 }
339
340 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
341 malloc(sizeof(VkExtensionProperties) * count));
342 if (!exts) {
343 ALOGE("failed to allocate HAL instance extension array");
344 return false;
345 }
346
347 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
348 VK_SUCCESS) {
349 ALOGE("failed to enumerate HAL instance extensions");
350 free(exts);
351 return false;
352 }
353
354 for (uint32_t i = 0; i < count; i++) {
355 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
356 0) {
357 debug_report_index_ = static_cast<int>(i);
358 break;
359 }
360 }
361
362 free(exts);
363
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800364 return true;
365}
366
367CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700368 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800369 const VkAllocationCallbacks& allocator)
370 : is_instance_(true),
371 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000372 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700373 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800374 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800375 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700376 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800377
Chia-I Wu4901db72016-03-24 16:38:58 +0800378CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
379 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700380 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800381 const VkAllocationCallbacks& allocator)
382 : is_instance_(false),
383 allocator_(allocator),
Trevor David Black628c41a2021-09-27 05:07:22 +0000384 loader_api_version_(VK_API_VERSION_1_3),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700385 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800386 physical_dev_(physical_dev),
387 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700388 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800389
390CreateInfoWrapper::~CreateInfoWrapper() {
391 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
392 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
393}
394
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800395VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700396 VkResult result = SanitizeApiVersion();
397 if (result == VK_SUCCESS)
398 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800399 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800400 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800401 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800402 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800403
404 return result;
405}
406
407const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800408CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800409 return hook_extensions_;
410}
411
412const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800413CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800414 return hal_extensions_;
415}
416
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800417CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
418 return &instance_info_;
419}
420
Chia-I Wu4901db72016-03-24 16:38:58 +0800421CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
422 return &dev_info_;
423}
424
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700425VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700426 if (!is_instance_ || !instance_info_.pApplicationInfo)
427 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700428
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700429 if (icd_api_version_ > VK_API_VERSION_1_0 ||
430 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
431 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700432
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700433 // override apiVersion to avoid error return from 1.0 icd
434 application_info_ = *instance_info_.pApplicationInfo;
435 application_info_.apiVersion = VK_API_VERSION_1_0;
436 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700437
438 return VK_SUCCESS;
439}
440
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800441VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800442 const struct StructHeader {
443 VkStructureType type;
444 const void* next;
445 } * header;
446
447 if (is_instance_) {
448 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
449
450 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
451 while (header &&
452 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
453 header = reinterpret_cast<const StructHeader*>(header->next);
454
455 instance_info_.pNext = header;
456 } else {
457 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
458
459 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
460 while (header &&
461 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
462 header = reinterpret_cast<const StructHeader*>(header->next);
463
464 dev_info_.pNext = header;
465 }
466
467 return VK_SUCCESS;
468}
469
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800470VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800471 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
472 : dev_info_.ppEnabledLayerNames;
473 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
474 : dev_info_.enabledLayerCount;
475
476 // remove all layers
477 layer_names = nullptr;
478 layer_count = 0;
479
480 return VK_SUCCESS;
481}
482
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800483VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800484 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
485 : dev_info_.ppEnabledExtensionNames;
486 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
487 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800488
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800489 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800490 if (result != VK_SUCCESS)
491 return result;
492
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700493 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700494 for (uint32_t i = 0; i < ext_count; i++) {
495 // Upon api downgrade, skip the promoted instance extensions in the
496 // first pass to avoid duplicate extensions.
497 const std::optional<uint32_t> version =
498 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700499 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700500 *version <= loader_api_version_)
501 continue;
502
503 FilterExtension(ext_names[i]);
504 }
505
506 // Enable the required extensions to support core functionalities.
507 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700508 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700509 for (const auto& promoted_extension : promoted_extensions)
510 FilterExtension(promoted_extension);
511 } else {
512 for (uint32_t i = 0; i < ext_count; i++)
513 FilterExtension(ext_names[i]);
514 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800515
Jesse Halld3d887a2018-03-05 13:34:45 -0800516 // Enable device extensions that contain physical-device commands, so that
517 // vkGetInstanceProcAddr will return those physical-device commands.
518 if (is_instance_) {
519 hook_extensions_.set(ProcHook::KHR_swapchain);
520 }
521
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700522 const uint32_t api_version =
523 is_instance_ ? loader_api_version_
524 : std::min(icd_api_version_, loader_api_version_);
525 switch (api_version) {
Trevor David Black628c41a2021-09-27 05:07:22 +0000526 case VK_API_VERSION_1_3:
527 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
528 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_3);
529 [[clang::fallthrough]];
Ian Elliottfa7af492021-07-20 17:40:24 -0600530 case VK_API_VERSION_1_2:
531 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
532 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_2);
533 [[clang::fallthrough]];
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700534 case VK_API_VERSION_1_1:
535 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
536 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
537 [[clang::fallthrough]];
538 case VK_API_VERSION_1_0:
539 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
540 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
541 break;
542 default:
543 ALOGE("Unknown API version[%u]", api_version);
544 break;
545 }
546
Chia-I Wu4901db72016-03-24 16:38:58 +0800547 ext_names = extension_filter_.names;
548 ext_count = extension_filter_.name_count;
549
550 return VK_SUCCESS;
551}
552
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800553VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800554 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800555 return Hal::Device().EnumerateInstanceExtensionProperties(
556 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800557 } else {
558 const auto& driver = GetData(physical_dev_).driver;
559 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
560 &count, nullptr);
561 }
562}
563
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800564VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800565 uint32_t& count,
566 VkExtensionProperties* props) const {
567 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800568 return Hal::Device().EnumerateInstanceExtensionProperties(
569 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800570 } else {
571 const auto& driver = GetData(physical_dev_).driver;
572 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
573 &count, props);
574 }
575}
576
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800577VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800578 // query extension count
579 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800580 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800581 if (result != VK_SUCCESS || count == 0)
582 return result;
583
584 auto& filter = extension_filter_;
585 filter.exts =
586 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
587 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
588 alignof(VkExtensionProperties),
589 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
590 if (!filter.exts)
591 return VK_ERROR_OUT_OF_HOST_MEMORY;
592
593 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800594 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800595 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
596 return result;
597
598 if (!count)
599 return VK_SUCCESS;
600
601 filter.ext_count = count;
602
603 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700604 if (is_instance_) {
605 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
606
607 // It requires enabling additional promoted extensions to downgrade api,
608 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700609 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700610 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700611 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700612 }
613
614 count = std::min(filter.ext_count, enabled_ext_count);
615 } else {
616 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
617 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700618
619 if (!count)
620 return VK_SUCCESS;
621
Chia-I Wu4901db72016-03-24 16:38:58 +0800622 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
623 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
624 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
625 if (!filter.names)
626 return VK_ERROR_OUT_OF_HOST_MEMORY;
627
628 return VK_SUCCESS;
629}
630
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800631void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800632 auto& filter = extension_filter_;
633
634 ProcHook::Extension ext_bit = GetProcHookExtension(name);
635 if (is_instance_) {
636 switch (ext_bit) {
637 case ProcHook::KHR_android_surface:
638 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600639 case ProcHook::KHR_surface_protected_capabilities:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700640 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300641 case ProcHook::KHR_get_surface_capabilities2:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600642 case ProcHook::GOOGLE_surfaceless_query:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000643 case ProcHook::EXT_surface_maintenance1:
Chia-I Wu4901db72016-03-24 16:38:58 +0800644 hook_extensions_.set(ext_bit);
645 // return now as these extensions do not require HAL support
646 return;
647 case ProcHook::EXT_debug_report:
648 // both we and HAL can take part in
649 hook_extensions_.set(ext_bit);
650 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300651 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700652 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700653 case ProcHook::KHR_external_memory_capabilities:
654 case ProcHook::KHR_external_semaphore_capabilities:
655 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700656 case ProcHook::EXTENSION_UNKNOWN:
657 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800658 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700659
Yiwei Zhang23143102019-04-10 18:24:05 -0700660 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700661 case ProcHook::KHR_incremental_present:
662 case ProcHook::KHR_shared_presentable_image:
663 case ProcHook::KHR_swapchain:
664 case ProcHook::EXT_hdr_metadata:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000665 case ProcHook::EXT_swapchain_maintenance1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700666 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
667 case ProcHook::ANDROID_native_buffer:
668 case ProcHook::GOOGLE_display_timing:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000669 case ProcHook::KHR_external_fence_fd:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700670 case ProcHook::EXTENSION_CORE_1_0:
671 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700672 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000673 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700674 case ProcHook::EXTENSION_COUNT:
675 // Device and meta extensions. If we ever get here it's a bug in
676 // our code. But enumerating them lets us avoid having a default
677 // case, and default hides other bugs.
678 ALOGE(
679 "CreateInfoWrapper::FilterExtension: invalid instance "
680 "extension '%s'. FIX ME",
681 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800682 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700683
684 // Don't use a default case. Without it, -Wswitch will tell us
685 // at compile time if someone adds a new ProcHook extension but
686 // doesn't handle it above. That's a real bug that has
687 // not-immediately-obvious effects.
688 //
689 // default:
690 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800691 }
692 } else {
693 switch (ext_bit) {
694 case ProcHook::KHR_swapchain:
695 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
696 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
697 ext_bit = ProcHook::ANDROID_native_buffer;
698 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700699 case ProcHook::KHR_incremental_present:
Chris Forbesfa25e632017-02-22 12:36:02 +1300700 case ProcHook::KHR_shared_presentable_image:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000701 case ProcHook::GOOGLE_display_timing:
Ian Elliott8a977262017-01-19 09:05:58 -0700702 hook_extensions_.set(ext_bit);
703 // return now as these extensions do not require HAL support
704 return;
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000705 case ProcHook::EXT_swapchain_maintenance1:
706 // map VK_KHR_swapchain_maintenance1 to KHR_external_fence_fd
707 name = VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME;
708 ext_bit = ProcHook::KHR_external_fence_fd;
709 break;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700710 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700711 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700712 hook_extensions_.set(ext_bit);
713 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700714 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000715 case ProcHook::KHR_external_fence_fd:
Chia-I Wu4901db72016-03-24 16:38:58 +0800716 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700717 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800718 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700719
720 case ProcHook::KHR_android_surface:
721 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700722 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700723 case ProcHook::KHR_external_memory_capabilities:
724 case ProcHook::KHR_external_semaphore_capabilities:
725 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700726 case ProcHook::KHR_get_surface_capabilities2:
727 case ProcHook::KHR_surface:
Ian Elliottbb67b242022-03-16 09:52:28 -0600728 case ProcHook::KHR_surface_protected_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700729 case ProcHook::EXT_debug_report:
730 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000731 case ProcHook::EXT_surface_maintenance1:
Ian Elliott1ce053f2022-03-16 09:49:53 -0600732 case ProcHook::GOOGLE_surfaceless_query:
Jesse Hall7f983a82018-03-29 14:46:45 -0700733 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700734 case ProcHook::EXTENSION_CORE_1_0:
735 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700736 case ProcHook::EXTENSION_CORE_1_2:
Trevor David Blackb700ae82021-09-27 04:50:04 +0000737 case ProcHook::EXTENSION_CORE_1_3:
Jesse Hall7f983a82018-03-29 14:46:45 -0700738 case ProcHook::EXTENSION_COUNT:
739 // Instance and meta extensions. If we ever get here it's a bug
740 // in our code. But enumerating them lets us avoid having a
741 // default case, and default hides other bugs.
742 ALOGE(
743 "CreateInfoWrapper::FilterExtension: invalid device "
744 "extension '%s'. FIX ME",
745 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800746 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700747
748 // Don't use a default case. Without it, -Wswitch will tell us
749 // at compile time if someone adds a new ProcHook extension but
750 // doesn't handle it above. That's a real bug that has
751 // not-immediately-obvious effects.
752 //
753 // default:
754 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800755 }
756 }
757
758 for (uint32_t i = 0; i < filter.ext_count; i++) {
759 const VkExtensionProperties& props = filter.exts[i];
760 // ignore unknown extensions
761 if (strcmp(name, props.extensionName) != 0)
762 continue;
763
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000764 if (ext_bit != ProcHook::EXTENSION_UNKNOWN &&
765 hal_extensions_.test(ext_bit)) {
766 ALOGI("CreateInfoWrapper::FilterExtension: already have '%s'.", name);
767 continue;
768 }
769
Ian Elliott5b527a02023-08-10 13:32:55 -0600770 // Ignore duplicate extensions (see: b/288929054)
771 bool duplicate_entry = false;
772 for (uint32_t j = 0; j < filter.name_count; j++) {
773 if (strcmp(name, filter.names[j]) == 0) {
774 duplicate_entry = true;
775 break;
776 }
777 }
778 if (duplicate_entry == true)
779 continue;
780
Chia-I Wu4901db72016-03-24 16:38:58 +0800781 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800782 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
783 if (ext_bit == ProcHook::ANDROID_native_buffer)
784 hook_extensions_.set(ProcHook::KHR_swapchain);
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000785 if (ext_bit == ProcHook::KHR_external_fence_fd)
786 hook_extensions_.set(ProcHook::EXT_swapchain_maintenance1);
Chia-I Wu1600e262016-04-12 09:40:06 +0800787
788 hal_extensions_.set(ext_bit);
789 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800790
791 break;
792 }
793}
794
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800795VKAPI_ATTR void* DefaultAllocate(void*,
796 size_t size,
797 size_t alignment,
798 VkSystemAllocationScope) {
799 void* ptr = nullptr;
800 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
801 // additionally requires that it be at least sizeof(void*).
802 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
803 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
804 ret, ptr);
805 return ret == 0 ? ptr : nullptr;
806}
807
808VKAPI_ATTR void* DefaultReallocate(void*,
809 void* ptr,
810 size_t size,
811 size_t alignment,
812 VkSystemAllocationScope) {
813 if (size == 0) {
814 free(ptr);
815 return nullptr;
816 }
817
Yiwei Zhanga885c062019-10-24 12:07:57 -0700818 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800819 // request is smaller than the existing chunk, we just continue using it.
820 // Right now the loader never reallocs, so this doesn't matter. If that
821 // changes, or if this code is copied into some other project, this should
822 // probably have a heuristic to allocate-copy-free when doing so will save
823 // "enough" space.
824 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
825 if (size <= old_size)
826 return ptr;
827
828 void* new_ptr = nullptr;
829 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
830 return nullptr;
831 if (ptr) {
832 memcpy(new_ptr, ptr, std::min(old_size, size));
833 free(ptr);
834 }
835 return new_ptr;
836}
837
838VKAPI_ATTR void DefaultFree(void*, void* ptr) {
839 ALOGD_CALLSTACK("Free: %p", ptr);
840 free(ptr);
841}
842
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800843InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
844 void* data_mem = allocator.pfnAllocation(
845 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
846 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
847 if (!data_mem)
848 return nullptr;
849
850 return new (data_mem) InstanceData(allocator);
851}
852
853void FreeInstanceData(InstanceData* data,
854 const VkAllocationCallbacks& allocator) {
855 data->~InstanceData();
856 allocator.pfnFree(allocator.pUserData, data);
857}
858
Chia-I Wu950d6e12016-05-03 09:12:35 +0800859DeviceData* AllocateDeviceData(
860 const VkAllocationCallbacks& allocator,
861 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800862 void* data_mem = allocator.pfnAllocation(
863 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
864 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
865 if (!data_mem)
866 return nullptr;
867
Chia-I Wu950d6e12016-05-03 09:12:35 +0800868 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800869}
870
871void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
872 data->~DeviceData();
873 allocator.pfnFree(allocator.pUserData, data);
874}
875
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800876} // anonymous namespace
877
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800878bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800879 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800880}
881
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800882const VkAllocationCallbacks& GetDefaultAllocator() {
883 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
884 .pUserData = nullptr,
885 .pfnAllocation = DefaultAllocate,
886 .pfnReallocation = DefaultReallocate,
887 .pfnFree = DefaultFree,
888 };
889
890 return kDefaultAllocCallbacks;
891}
892
Chia-I Wueb7db122016-03-24 09:11:06 +0800893PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
894 const ProcHook* hook = GetProcHook(pName);
895 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800896 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800897
898 if (!instance) {
899 if (hook->type == ProcHook::GLOBAL)
900 return hook->proc;
901
Chia-I Wu109f8982016-04-22 06:40:40 +0800902 // v0 layers expect
903 //
904 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
905 //
906 // to work.
907 if (strcmp(pName, "vkCreateDevice") == 0)
908 return hook->proc;
909
Chia-I Wueb7db122016-03-24 09:11:06 +0800910 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800911 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800912 pName);
913
Chia-I Wu109f8982016-04-22 06:40:40 +0800914 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800915 }
916
917 PFN_vkVoidFunction proc;
918
919 switch (hook->type) {
920 case ProcHook::INSTANCE:
921 proc = (GetData(instance).hook_extensions[hook->extension])
922 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800923 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800924 break;
925 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700926 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800927 ? hook->proc
928 : hook->checked_proc;
929 break;
930 default:
931 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800932 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800933 pName);
934 proc = nullptr;
935 break;
936 }
937
938 return proc;
939}
940
941PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
942 const ProcHook* hook = GetProcHook(pName);
943 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800944 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800945
946 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800947 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800948 return nullptr;
949 }
950
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800951 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
952 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800953}
954
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800955VkResult EnumerateInstanceExtensionProperties(
956 const char* pLayerName,
957 uint32_t* pPropertyCount,
958 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700959 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliottc7c4aa32022-02-28 16:47:43 -0700960 loader_extensions.push_back(
Ian Elliott4d1ad472022-03-14 17:27:47 -0600961 {VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_SURFACE_SPEC_VERSION});
Ian Elliottbb67b242022-03-16 09:52:28 -0600962 loader_extensions.push_back(
963 {VK_KHR_SURFACE_PROTECTED_CAPABILITIES_EXTENSION_NAME,
964 VK_KHR_SURFACE_PROTECTED_CAPABILITIES_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600965 loader_extensions.push_back({
966 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
967 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
968 loader_extensions.push_back({
969 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
970 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Ian Elliotte7f036c2022-03-15 16:49:21 -0600971 loader_extensions.push_back(
972 {VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
973 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott1ce053f2022-03-16 09:49:53 -0600974 loader_extensions.push_back({VK_GOOGLE_SURFACELESS_QUERY_EXTENSION_NAME,
975 VK_GOOGLE_SURFACELESS_QUERY_SPEC_VERSION});
Chris Forbes9d0d9ff2022-12-28 01:58:31 +0000976 loader_extensions.push_back({
977 VK_EXT_SURFACE_MAINTENANCE_1_EXTENSION_NAME,
978 VK_EXT_SURFACE_MAINTENANCE_1_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600979
Chia-I Wu31938252016-05-23 15:31:02 +0800980 static const VkExtensionProperties loader_debug_report_extension = {
981 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
982 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800983
984 // enumerate our extensions first
985 if (!pLayerName && pProperties) {
986 uint32_t count = std::min(
987 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
988
Yiwei Zhang5e862202019-06-21 14:59:16 -0700989 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800990
991 if (count < loader_extensions.size()) {
992 *pPropertyCount = count;
993 return VK_INCOMPLETE;
994 }
995
996 pProperties += count;
997 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800998
999 if (Hal::Get().GetDebugReportIndex() < 0) {
1000 if (!*pPropertyCount) {
1001 *pPropertyCount = count;
1002 return VK_INCOMPLETE;
1003 }
1004
1005 pProperties[0] = loader_debug_report_extension;
1006 pProperties += 1;
1007 *pPropertyCount -= 1;
1008 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001009 }
1010
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001011 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001012 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001013 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001014 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001015
Chia-I Wu31938252016-05-23 15:31:02 +08001016 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1017 int idx = Hal::Get().GetDebugReportIndex();
1018 if (idx < 0) {
1019 *pPropertyCount += 1;
1020 } else if (pProperties &&
1021 static_cast<uint32_t>(idx) < *pPropertyCount) {
1022 pProperties[idx].specVersion =
1023 std::min(pProperties[idx].specVersion,
1024 loader_debug_report_extension.specVersion);
1025 }
1026
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001027 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +08001028 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001029
1030 return result;
1031}
1032
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001033void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +13001034 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001035 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbese056c122021-07-22 13:54:04 -07001036 ATRACE_CALL();
1037
Chris Forbesfa25e632017-02-22 12:36:02 +13001038 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001039 VkPhysicalDeviceProperties2 properties = {
1040 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +13001041 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001042 {},
Chris Forbesfa25e632017-02-22 12:36:02 +13001043 };
1044
1045#pragma clang diagnostic push
1046#pragma clang diagnostic ignored "-Wold-style-cast"
1047 presentation_properties->sType =
1048 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
1049#pragma clang diagnostic pop
1050 presentation_properties->pNext = nullptr;
1051 presentation_properties->sharedImage = VK_FALSE;
1052
Chris Forbese056c122021-07-22 13:54:04 -07001053 const auto& driver = GetData(physicalDevice).driver;
1054
1055 if (driver.GetPhysicalDeviceProperties2) {
1056 // >= 1.1 driver, supports core GPDP2 entrypoint.
1057 driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
1058 } else if (driver.GetPhysicalDeviceProperties2KHR) {
1059 // Old driver, but may support presentation properties
1060 // if we have the GPDP2 extension. Otherwise, no presentation
1061 // properties supported.
1062 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, &properties);
1063 }
Chris Forbesfa25e632017-02-22 12:36:02 +13001064}
1065
Trevor David Black929e9cd2022-11-22 04:12:19 +00001066VkResult GetAndroidNativeBufferSpecVersion9Support(
1067 VkPhysicalDevice physicalDevice,
1068 bool& support) {
1069 support = false;
1070
Trevor David Black2cc44682022-03-09 00:31:38 +00001071 const InstanceData& data = GetData(physicalDevice);
1072
1073 // Call to get propertyCount
1074 uint32_t propertyCount = 0;
1075 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1076 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1077 physicalDevice, nullptr, &propertyCount, nullptr);
1078 ATRACE_END();
1079
Trevor David Black929e9cd2022-11-22 04:12:19 +00001080 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1081 return result;
1082 }
1083
Trevor David Black2cc44682022-03-09 00:31:38 +00001084 // Call to enumerate properties
1085 std::vector<VkExtensionProperties> properties(propertyCount);
1086 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
1087 result = data.driver.EnumerateDeviceExtensionProperties(
1088 physicalDevice, nullptr, &propertyCount, properties.data());
1089 ATRACE_END();
1090
Trevor David Black929e9cd2022-11-22 04:12:19 +00001091 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1092 return result;
1093 }
1094
Trevor David Black2cc44682022-03-09 00:31:38 +00001095 for (uint32_t i = 0; i < propertyCount; i++) {
1096 auto& prop = properties[i];
1097
1098 if (strcmp(prop.extensionName,
1099 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1100 continue;
1101
1102 if (prop.specVersion >= 9) {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001103 support = true;
1104 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001105 }
1106 }
1107
Trevor David Black929e9cd2022-11-22 04:12:19 +00001108 return result;
Trevor David Black2cc44682022-03-09 00:31:38 +00001109}
1110
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001111bool CanSupportSwapchainMaintenance1Extension(VkPhysicalDevice physicalDevice) {
1112 const auto& driver = GetData(physicalDevice).driver;
1113 if (!driver.GetPhysicalDeviceExternalFenceProperties)
1114 return false;
1115
1116 // Requires support for external fences imported from sync fds.
1117 // This is _almost_ universal on Android, but may be missing on
1118 // some extremely old drivers, or on strange implementations like
1119 // cuttlefish.
1120 VkPhysicalDeviceExternalFenceInfo fenceInfo = {
1121 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_FENCE_INFO,
1122 nullptr,
1123 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
1124 };
1125 VkExternalFenceProperties fenceProperties = {
1126 VK_STRUCTURE_TYPE_EXTERNAL_FENCE_PROPERTIES,
1127 nullptr,
1128 0, 0, 0
1129 };
1130
1131 GetPhysicalDeviceExternalFenceProperties(physicalDevice, &fenceInfo, &fenceProperties);
1132 if (fenceProperties.externalFenceFeatures & VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT)
1133 return true;
1134
1135 return false;
1136}
1137
Chia-I Wu01cf3052016-03-24 16:16:21 +08001138VkResult EnumerateDeviceExtensionProperties(
1139 VkPhysicalDevice physicalDevice,
1140 const char* pLayerName,
1141 uint32_t* pPropertyCount,
1142 VkExtensionProperties* pProperties) {
1143 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001144 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001145 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001146 loader_extensions.push_back({
1147 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1148 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001149
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001150 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001151 if (hdrBoardConfig) {
1152 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1153 VK_EXT_HDR_METADATA_SPEC_VERSION});
1154 }
1155
Chris Forbes16095002017-05-05 15:33:29 -07001156 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001157 QueryPresentationProperties(physicalDevice, &presentation_properties);
1158 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001159 loader_extensions.push_back({
1160 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1161 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001162 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001163
Ian Elliott5c34de22017-04-10 14:42:30 -06001164 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1165 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001166 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001167 loader_extensions.push_back({
1168 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1169 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1170 }
1171
Trevor David Black2cc44682022-03-09 00:31:38 +00001172 // Conditionally add VK_EXT_IMAGE_COMPRESSION_CONTROL* if feature and ANB
1173 // support is provided by the driver
1174 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT
1175 swapchainCompFeats = {};
1176 swapchainCompFeats.sType =
1177 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT;
1178 swapchainCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001179 swapchainCompFeats.imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001180 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1181 imageCompFeats.sType =
1182 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1183 imageCompFeats.pNext = &swapchainCompFeats;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001184 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001185
1186 VkPhysicalDeviceFeatures2 feats2 = {};
1187 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1188 feats2.pNext = &imageCompFeats;
1189
Trevor David Black929e9cd2022-11-22 04:12:19 +00001190 const auto& driver = GetData(physicalDevice).driver;
1191 if (driver.GetPhysicalDeviceFeatures2 ||
1192 driver.GetPhysicalDeviceFeatures2KHR) {
1193 GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1194 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001195
Trevor David Black929e9cd2022-11-22 04:12:19 +00001196 bool anb9 = false;
1197 VkResult result =
1198 GetAndroidNativeBufferSpecVersion9Support(physicalDevice, anb9);
1199
1200 if (result != VK_SUCCESS && result != VK_INCOMPLETE) {
1201 return result;
1202 }
Trevor David Black2cc44682022-03-09 00:31:38 +00001203
1204 if (anb9 && imageCompFeats.imageCompressionControl) {
1205 loader_extensions.push_back(
1206 {VK_EXT_IMAGE_COMPRESSION_CONTROL_EXTENSION_NAME,
1207 VK_EXT_IMAGE_COMPRESSION_CONTROL_SPEC_VERSION});
1208 }
1209 if (anb9 && swapchainCompFeats.imageCompressionControlSwapchain) {
1210 loader_extensions.push_back(
1211 {VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_EXTENSION_NAME,
1212 VK_EXT_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_SPEC_VERSION});
1213 }
1214
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001215 if (CanSupportSwapchainMaintenance1Extension(physicalDevice)) {
1216 loader_extensions.push_back({
1217 VK_EXT_SWAPCHAIN_MAINTENANCE_1_EXTENSION_NAME,
1218 VK_EXT_SWAPCHAIN_MAINTENANCE_1_SPEC_VERSION});
1219 }
1220
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001221 // enumerate our extensions first
1222 if (!pLayerName && pProperties) {
1223 uint32_t count = std::min(
1224 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1225
Yiwei Zhang5e862202019-06-21 14:59:16 -07001226 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001227
1228 if (count < loader_extensions.size()) {
1229 *pPropertyCount = count;
1230 return VK_INCOMPLETE;
1231 }
1232
1233 pProperties += count;
1234 *pPropertyCount -= count;
1235 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001236
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001237 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Trevor David Black929e9cd2022-11-22 04:12:19 +00001238 result = data.driver.EnumerateDeviceExtensionProperties(
Chia-I Wu01cf3052016-03-24 16:16:21 +08001239 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001240 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001241
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001242 if (pProperties) {
1243 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1244 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1245 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001246
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001247 if (strcmp(prop.extensionName,
1248 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1249 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001250
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001251 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1252 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001253
1254 if (prop.specVersion >= 8) {
1255 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1256 } else {
1257 prop.specVersion = 68;
1258 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001259 }
1260 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001261
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001262 // restore loader extension count
1263 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1264 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001265 }
1266
1267 return result;
1268}
1269
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001270VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1271 const VkAllocationCallbacks* pAllocator,
1272 VkInstance* pInstance) {
1273 const VkAllocationCallbacks& data_allocator =
1274 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1275
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001276 VkResult result = VK_SUCCESS;
1277 uint32_t icd_api_version = VK_API_VERSION_1_0;
1278 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1279 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1280 Hal::Device().GetInstanceProcAddr(nullptr,
1281 "vkEnumerateInstanceVersion"));
1282 if (pfn_enumerate_instance_version) {
1283 ATRACE_BEGIN("pfn_enumerate_instance_version");
1284 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1285 ATRACE_END();
1286 if (result != VK_SUCCESS)
1287 return result;
1288
Trevor David Blackb68a2252021-08-23 16:37:18 +00001289 icd_api_version ^= VK_API_VERSION_PATCH(icd_api_version);
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001290 }
1291
1292 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1293 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001294 if (result != VK_SUCCESS)
1295 return result;
1296
1297 InstanceData* data = AllocateInstanceData(data_allocator);
1298 if (!data)
1299 return VK_ERROR_OUT_OF_HOST_MEMORY;
1300
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001301 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001302
1303 // call into the driver
1304 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001305 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001306 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001307 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1308 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001309 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001310 if (result != VK_SUCCESS) {
1311 FreeInstanceData(data, data_allocator);
1312 return result;
1313 }
1314
1315 // initialize InstanceDriverTable
1316 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001317 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001318 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001319 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001320 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001321 if (data->driver.DestroyInstance)
1322 data->driver.DestroyInstance(instance, pAllocator);
1323
1324 FreeInstanceData(data, data_allocator);
1325
1326 return VK_ERROR_INCOMPATIBLE_DRIVER;
1327 }
1328
1329 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001330 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001331 if (!data->get_device_proc_addr) {
1332 data->driver.DestroyInstance(instance, pAllocator);
1333 FreeInstanceData(data, data_allocator);
1334
1335 return VK_ERROR_INCOMPATIBLE_DRIVER;
1336 }
1337
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001338 // TODO(b/259516419) avoid getting stats from hwui
1339 // const bool reportStats = (pCreateInfo->pApplicationInfo == nullptr )
1340 // || (strcmp("android framework",
1341 // pCreateInfo->pApplicationInfo->pEngineName) != 0);
1342 const bool reportStats = true;
1343 if (reportStats) {
1344 // Set stats for Vulkan api version requested with application info
1345 if (pCreateInfo->pApplicationInfo) {
1346 const uint32_t vulkanApiVersion =
1347 pCreateInfo->pApplicationInfo->apiVersion;
1348 android::GraphicsEnv::getInstance().setTargetStats(
1349 android::GpuStatsInfo::Stats::CREATED_VULKAN_API_VERSION,
1350 vulkanApiVersion);
1351 }
1352
1353 // Update stats for the extensions requested
1354 android::GraphicsEnv::getInstance().setVulkanInstanceExtensions(
1355 pCreateInfo->enabledExtensionCount,
1356 pCreateInfo->ppEnabledExtensionNames);
1357 }
1358
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001359 *pInstance = instance;
1360
1361 return VK_SUCCESS;
1362}
1363
1364void DestroyInstance(VkInstance instance,
1365 const VkAllocationCallbacks* pAllocator) {
1366 InstanceData& data = GetData(instance);
1367 data.driver.DestroyInstance(instance, pAllocator);
1368
1369 VkAllocationCallbacks local_allocator;
1370 if (!pAllocator) {
1371 local_allocator = data.allocator;
1372 pAllocator = &local_allocator;
1373 }
1374
1375 FreeInstanceData(&data, *pAllocator);
1376}
1377
Chia-I Wu4901db72016-03-24 16:38:58 +08001378VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1379 const VkDeviceCreateInfo* pCreateInfo,
1380 const VkAllocationCallbacks* pAllocator,
1381 VkDevice* pDevice) {
1382 const InstanceData& instance_data = GetData(physicalDevice);
1383 const VkAllocationCallbacks& data_allocator =
1384 (pAllocator) ? *pAllocator : instance_data.allocator;
1385
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001386 VkPhysicalDeviceProperties properties;
1387 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1388 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1389 &properties);
1390 ATRACE_END();
1391
1392 CreateInfoWrapper wrapper(
1393 physicalDevice, *pCreateInfo,
Trevor David Blackb68a2252021-08-23 16:37:18 +00001394 properties.apiVersion ^ VK_API_VERSION_PATCH(properties.apiVersion),
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001395 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001396 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001397 if (result != VK_SUCCESS)
1398 return result;
1399
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001400 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001401 DeviceData* data = AllocateDeviceData(data_allocator,
1402 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001403 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001404 if (!data)
1405 return VK_ERROR_OUT_OF_HOST_MEMORY;
1406
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001407 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001408
1409 // call into the driver
1410 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001411 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001412 result = instance_data.driver.CreateDevice(
1413 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1414 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001415 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001416 if (result != VK_SUCCESS) {
1417 FreeDeviceData(data, data_allocator);
1418 return result;
1419 }
1420
1421 // initialize DeviceDriverTable
1422 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001423 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1424 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001425 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1426 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1427 if (data->driver.DestroyDevice)
1428 data->driver.DestroyDevice(dev, pAllocator);
1429
1430 FreeDeviceData(data, data_allocator);
1431
1432 return VK_ERROR_INCOMPATIBLE_DRIVER;
1433 }
Chris Forbesd8277912017-02-10 14:59:59 +13001434
Trevor David Black2cc44682022-03-09 00:31:38 +00001435 // Confirming ANDROID_native_buffer implementation, whose set of
Chris Forbesd8277912017-02-10 14:59:59 +13001436 // entrypoints varies according to the spec version.
1437 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1438 !data->driver.GetSwapchainGrallocUsageANDROID &&
Trevor David Black2cc44682022-03-09 00:31:38 +00001439 !data->driver.GetSwapchainGrallocUsage2ANDROID &&
1440 !data->driver.GetSwapchainGrallocUsage3ANDROID) {
1441 ALOGE(
1442 "Driver's implementation of ANDROID_native_buffer is broken;"
1443 " must expose at least one of "
1444 "vkGetSwapchainGrallocUsageANDROID or "
1445 "vkGetSwapchainGrallocUsage2ANDROID or "
1446 "vkGetSwapchainGrallocUsage3ANDROID");
Chris Forbesd8277912017-02-10 14:59:59 +13001447
1448 data->driver.DestroyDevice(dev, pAllocator);
1449 FreeDeviceData(data, data_allocator);
1450
1451 return VK_ERROR_INCOMPATIBLE_DRIVER;
1452 }
1453
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001454 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1455 // Log that the app is hitting software Vulkan implementation
1456 android::GraphicsEnv::getInstance().setTargetStats(
1457 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1458 }
1459
Jesse Halldc225072016-05-30 22:40:14 -07001460 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001461
1462 *pDevice = dev;
1463
Serdar Kocdemirb2901c92022-11-17 00:39:05 +00001464 // TODO(b/259516419) avoid getting stats from hwui
1465 const bool reportStats = true;
1466 if (reportStats) {
1467 android::GraphicsEnv::getInstance().setTargetStats(
1468 android::GpuStatsInfo::Stats::CREATED_VULKAN_DEVICE);
1469
1470 // Set stats for creating a Vulkan device and report features in use
1471 const VkPhysicalDeviceFeatures* pEnabledFeatures =
1472 pCreateInfo->pEnabledFeatures;
1473 if (!pEnabledFeatures) {
1474 // Use features from the chained VkPhysicalDeviceFeatures2
1475 // structure, if given
1476 const VkPhysicalDeviceFeatures2* features2 =
1477 reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1478 pCreateInfo->pNext);
1479 while (features2 &&
1480 features2->sType !=
1481 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2) {
1482 features2 = reinterpret_cast<const VkPhysicalDeviceFeatures2*>(
1483 features2->pNext);
1484 }
1485 if (features2) {
1486 pEnabledFeatures = &features2->features;
1487 }
1488 }
1489 const VkBool32* pFeatures =
1490 reinterpret_cast<const VkBool32*>(pEnabledFeatures);
1491 if (pFeatures) {
1492 // VkPhysicalDeviceFeatures consists of VkBool32 values, go over all
1493 // of them using pointer arithmetic here and save the features in a
1494 // 64-bit bitfield
1495 static_assert(
1496 (sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32)) <= 64,
1497 "VkPhysicalDeviceFeatures has too many elements for bitfield "
1498 "packing");
1499 static_assert(
1500 (sizeof(VkPhysicalDeviceFeatures) % sizeof(VkBool32)) == 0,
1501 "VkPhysicalDeviceFeatures has invalid size for bitfield "
1502 "packing");
1503 const int numFeatures =
1504 sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
1505
1506 uint64_t enableFeatureBits = 0;
1507 for (int i = 0; i < numFeatures; i++) {
1508 if (pFeatures[i] != VK_FALSE) {
1509 enableFeatureBits |= (uint64_t(1) << i);
1510 }
1511 }
1512 android::GraphicsEnv::getInstance().setTargetStats(
1513 android::GpuStatsInfo::Stats::VULKAN_DEVICE_FEATURES_ENABLED,
1514 enableFeatureBits);
1515 }
1516
1517 // Update stats for the extensions requested
1518 android::GraphicsEnv::getInstance().setVulkanDeviceExtensions(
1519 pCreateInfo->enabledExtensionCount,
1520 pCreateInfo->ppEnabledExtensionNames);
1521 }
1522
Chia-I Wu4901db72016-03-24 16:38:58 +08001523 return VK_SUCCESS;
1524}
1525
1526void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1527 DeviceData& data = GetData(device);
1528 data.driver.DestroyDevice(device, pAllocator);
1529
1530 VkAllocationCallbacks local_allocator;
1531 if (!pAllocator) {
1532 local_allocator = data.allocator;
1533 pAllocator = &local_allocator;
1534 }
1535
1536 FreeDeviceData(&data, *pAllocator);
1537}
1538
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001539VkResult EnumeratePhysicalDevices(VkInstance instance,
1540 uint32_t* pPhysicalDeviceCount,
1541 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001542 ATRACE_CALL();
1543
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001544 const auto& data = GetData(instance);
1545
1546 VkResult result = data.driver.EnumeratePhysicalDevices(
1547 instance, pPhysicalDeviceCount, pPhysicalDevices);
1548 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1549 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1550 SetData(pPhysicalDevices[i], data);
1551 }
1552
1553 return result;
1554}
1555
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001556VkResult EnumeratePhysicalDeviceGroups(
1557 VkInstance instance,
1558 uint32_t* pPhysicalDeviceGroupCount,
1559 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001560 ATRACE_CALL();
1561
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001562 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001563 const auto& data = GetData(instance);
1564
Yiwei Zhange4f64172020-07-05 15:17:32 -07001565 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1566 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001567 uint32_t device_count = 0;
1568 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1569 if (result < 0)
1570 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001571
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001572 if (!pPhysicalDeviceGroupProperties) {
1573 *pPhysicalDeviceGroupCount = device_count;
1574 return result;
1575 }
1576
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001577 if (!device_count) {
1578 *pPhysicalDeviceGroupCount = 0;
1579 return result;
1580 }
Chad Versace32c087f2018-09-09 07:28:05 -07001581 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1582 if (!device_count)
1583 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001584
Yiwei Zhang5e862202019-06-21 14:59:16 -07001585 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001586 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001587 result =
1588 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001589 if (result < 0)
1590 return result;
1591
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001592 for (uint32_t i = 0; i < device_count; ++i) {
1593 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1594 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1595 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1596 }
1597 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001598 if (data.driver.EnumeratePhysicalDeviceGroups) {
1599 result = data.driver.EnumeratePhysicalDeviceGroups(
1600 instance, pPhysicalDeviceGroupCount,
1601 pPhysicalDeviceGroupProperties);
1602 } else {
1603 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1604 instance, pPhysicalDeviceGroupCount,
1605 pPhysicalDeviceGroupProperties);
1606 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001607 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1608 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1609 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1610 for (uint32_t j = 0;
1611 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1612 j++) {
1613 SetData(
1614 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001615 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001616 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001617 }
1618 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001619 }
1620
1621 return result;
1622}
1623
Chia-I Wuba0be412016-03-24 16:24:40 +08001624void GetDeviceQueue(VkDevice device,
1625 uint32_t queueFamilyIndex,
1626 uint32_t queueIndex,
1627 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001628 ATRACE_CALL();
1629
Chia-I Wuba0be412016-03-24 16:24:40 +08001630 const auto& data = GetData(device);
1631
1632 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1633 SetData(*pQueue, data);
1634}
1635
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001636void GetDeviceQueue2(VkDevice device,
1637 const VkDeviceQueueInfo2* pQueueInfo,
1638 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001639 ATRACE_CALL();
1640
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001641 const auto& data = GetData(device);
1642
1643 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001644 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001645}
1646
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001647VkResult AllocateCommandBuffers(
1648 VkDevice device,
1649 const VkCommandBufferAllocateInfo* pAllocateInfo,
1650 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001651 ATRACE_CALL();
1652
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001653 const auto& data = GetData(device);
1654
1655 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1656 pCommandBuffers);
1657 if (result == VK_SUCCESS) {
1658 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1659 SetData(pCommandBuffers[i], data);
1660 }
1661
1662 return result;
1663}
1664
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001665VkResult QueueSubmit(VkQueue queue,
1666 uint32_t submitCount,
1667 const VkSubmitInfo* pSubmits,
1668 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001669 ATRACE_CALL();
1670
1671 const auto& data = GetData(queue);
1672
1673 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1674}
1675
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001676void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1677 VkPhysicalDeviceFeatures2* pFeatures) {
1678 ATRACE_CALL();
1679
1680 const auto& driver = GetData(physicalDevice).driver;
1681
1682 if (driver.GetPhysicalDeviceFeatures2) {
1683 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
Trevor David Black2cc44682022-03-09 00:31:38 +00001684 } else {
1685 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1686 }
1687
1688 // Conditionally add imageCompressionControlSwapchain if
1689 // imageCompressionControl is supported Check for imageCompressionControl in
1690 // the pChain
1691 bool imageCompressionControl = false;
1692 bool imageCompressionControlInChain = false;
1693 bool imageCompressionControlSwapchainInChain = false;
1694 VkPhysicalDeviceFeatures2* pFeats = pFeatures;
1695 while (pFeats) {
1696 switch (pFeats->sType) {
1697 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT: {
1698 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*
1699 compressionFeat = reinterpret_cast<
1700 const VkPhysicalDeviceImageCompressionControlFeaturesEXT*>(
1701 pFeats);
1702 imageCompressionControl =
1703 compressionFeat->imageCompressionControl;
1704 imageCompressionControlInChain = true;
1705 } break;
1706
1707 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
Trevor David Black929e9cd2022-11-22 04:12:19 +00001708 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1709 compressionFeat = reinterpret_cast<
1710 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1711 pFeats);
1712 compressionFeat->imageCompressionControlSwapchain = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001713 imageCompressionControlSwapchainInChain = true;
1714 } break;
1715
Chris Forbes9d0d9ff2022-12-28 01:58:31 +00001716 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SWAPCHAIN_MAINTENANCE_1_FEATURES_EXT: {
1717 auto smf = reinterpret_cast<VkPhysicalDeviceSwapchainMaintenance1FeaturesEXT *>(
1718 pFeats);
1719 smf->swapchainMaintenance1 = true;
1720 } break;
1721
Trevor David Black2cc44682022-03-09 00:31:38 +00001722 default:
1723 break;
1724 }
1725 pFeats = reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1726 }
1727
1728 if (!imageCompressionControlSwapchainInChain) {
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001729 return;
1730 }
1731
Trevor David Black2cc44682022-03-09 00:31:38 +00001732 // If not in pchain, explicitly query for imageCompressionControl
1733 if (!imageCompressionControlInChain) {
1734 VkPhysicalDeviceImageCompressionControlFeaturesEXT imageCompFeats = {};
1735 imageCompFeats.sType =
1736 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_FEATURES_EXT;
1737 imageCompFeats.pNext = nullptr;
Trevor David Black929e9cd2022-11-22 04:12:19 +00001738 imageCompFeats.imageCompressionControl = false;
Trevor David Black2cc44682022-03-09 00:31:38 +00001739
1740 VkPhysicalDeviceFeatures2 feats2 = {};
1741 feats2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1742 feats2.pNext = &imageCompFeats;
1743
1744 if (driver.GetPhysicalDeviceFeatures2) {
1745 driver.GetPhysicalDeviceFeatures2(physicalDevice, &feats2);
1746 } else {
1747 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, &feats2);
1748 }
1749
1750 imageCompressionControl = imageCompFeats.imageCompressionControl;
1751 }
1752
1753 // Only enumerate imageCompressionControlSwapchin if imageCompressionControl
1754 if (imageCompressionControl) {
1755 pFeats = pFeatures;
1756 while (pFeats) {
1757 switch (pFeats->sType) {
1758 case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_COMPRESSION_CONTROL_SWAPCHAIN_FEATURES_EXT: {
1759 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*
1760 compressionFeat = reinterpret_cast<
1761 VkPhysicalDeviceImageCompressionControlSwapchainFeaturesEXT*>(
1762 pFeats);
1763 compressionFeat->imageCompressionControlSwapchain = true;
1764 } break;
1765
1766 default:
1767 break;
1768 }
1769 pFeats =
1770 reinterpret_cast<VkPhysicalDeviceFeatures2*>(pFeats->pNext);
1771 }
1772 }
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001773}
1774
1775void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1776 VkPhysicalDeviceProperties2* pProperties) {
1777 ATRACE_CALL();
1778
1779 const auto& driver = GetData(physicalDevice).driver;
1780
1781 if (driver.GetPhysicalDeviceProperties2) {
1782 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1783 return;
1784 }
1785
1786 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1787}
1788
1789void GetPhysicalDeviceFormatProperties2(
1790 VkPhysicalDevice physicalDevice,
1791 VkFormat format,
1792 VkFormatProperties2* pFormatProperties) {
1793 ATRACE_CALL();
1794
1795 const auto& driver = GetData(physicalDevice).driver;
1796
1797 if (driver.GetPhysicalDeviceFormatProperties2) {
1798 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1799 pFormatProperties);
1800 return;
1801 }
1802
1803 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1804 pFormatProperties);
1805}
1806
1807VkResult GetPhysicalDeviceImageFormatProperties2(
1808 VkPhysicalDevice physicalDevice,
1809 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1810 VkImageFormatProperties2* pImageFormatProperties) {
1811 ATRACE_CALL();
1812
1813 const auto& driver = GetData(physicalDevice).driver;
1814
1815 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1816 return driver.GetPhysicalDeviceImageFormatProperties2(
1817 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1818 }
1819
1820 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1821 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1822}
1823
1824void GetPhysicalDeviceQueueFamilyProperties2(
1825 VkPhysicalDevice physicalDevice,
1826 uint32_t* pQueueFamilyPropertyCount,
1827 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1828 ATRACE_CALL();
1829
1830 const auto& driver = GetData(physicalDevice).driver;
1831
1832 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1833 driver.GetPhysicalDeviceQueueFamilyProperties2(
1834 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1835 return;
1836 }
1837
1838 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1839 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1840}
1841
1842void GetPhysicalDeviceMemoryProperties2(
1843 VkPhysicalDevice physicalDevice,
1844 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1845 ATRACE_CALL();
1846
1847 const auto& driver = GetData(physicalDevice).driver;
1848
1849 if (driver.GetPhysicalDeviceMemoryProperties2) {
1850 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1851 pMemoryProperties);
1852 return;
1853 }
1854
1855 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1856 pMemoryProperties);
1857}
1858
1859void GetPhysicalDeviceSparseImageFormatProperties2(
1860 VkPhysicalDevice physicalDevice,
1861 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1862 uint32_t* pPropertyCount,
1863 VkSparseImageFormatProperties2* pProperties) {
1864 ATRACE_CALL();
1865
1866 const auto& driver = GetData(physicalDevice).driver;
1867
1868 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1869 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1870 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1871 return;
1872 }
1873
1874 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1875 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1876}
1877
Yiwei Zhange1f35012020-07-05 22:52:04 -07001878void GetPhysicalDeviceExternalBufferProperties(
1879 VkPhysicalDevice physicalDevice,
1880 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1881 VkExternalBufferProperties* pExternalBufferProperties) {
1882 ATRACE_CALL();
1883
1884 const auto& driver = GetData(physicalDevice).driver;
1885
1886 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1887 driver.GetPhysicalDeviceExternalBufferProperties(
1888 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1889 return;
1890 }
1891
1892 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1893 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1894 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1895 return;
1896 }
1897
1898 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1899 sizeof(VkExternalMemoryProperties));
1900}
1901
1902void GetPhysicalDeviceExternalSemaphoreProperties(
1903 VkPhysicalDevice physicalDevice,
1904 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1905 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1906 ATRACE_CALL();
1907
1908 const auto& driver = GetData(physicalDevice).driver;
1909
1910 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1911 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1912 physicalDevice, pExternalSemaphoreInfo,
1913 pExternalSemaphoreProperties);
1914 return;
1915 }
1916
1917 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1918 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1919 physicalDevice, pExternalSemaphoreInfo,
1920 pExternalSemaphoreProperties);
1921 return;
1922 }
1923
1924 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1925 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1926 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1927}
1928
1929void GetPhysicalDeviceExternalFenceProperties(
1930 VkPhysicalDevice physicalDevice,
1931 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1932 VkExternalFenceProperties* pExternalFenceProperties) {
1933 ATRACE_CALL();
1934
1935 const auto& driver = GetData(physicalDevice).driver;
1936
1937 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1938 driver.GetPhysicalDeviceExternalFenceProperties(
1939 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1940 return;
1941 }
1942
1943 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1944 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1945 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1946 return;
1947 }
1948
1949 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1950 pExternalFenceProperties->compatibleHandleTypes = 0;
1951 pExternalFenceProperties->externalFenceFeatures = 0;
1952}
1953
Chia-I Wu9d518162016-03-24 14:55:27 +08001954} // namespace driver
1955} // namespace vulkan