blob: 6f09a8c45b2ffb74c32db74a9422147bd39ef01a [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);
186 } else {
187 // load built-in driver
188 so = android_load_sphal_library(lib_name.c_str(), LIB_DL_FLAGS);
Jesse Hall53457db2016-12-14 16:54:06 -0800189 }
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700190 if (so)
191 break;
Jesse Hall53457db2016-12-14 16:54:06 -0800192 }
193 if (!so)
194 return -ENOENT;
195
Jesse Hall00e61ff2017-04-07 16:48:02 -0700196 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800197 if (!hmi) {
198 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
199 dlclose(so);
200 return -EINVAL;
201 }
202 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
203 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
204 dlclose(so);
205 return -EINVAL;
206 }
207 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700208 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800209 return 0;
210}
211
Jesse Hall00e61ff2017-04-07 16:48:02 -0700212int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800213 ATRACE_CALL();
214
Yiwei Zhangd9861812019-02-13 11:51:55 -0800215 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700216 android::GpuStatsInfo::Driver::VULKAN);
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700217 return LoadDriver(nullptr, module);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218}
219
220int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800221 ATRACE_CALL();
222
Jesse Hall00e61ff2017-04-07 16:48:02 -0700223 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
224 if (!ns)
225 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800226 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700227 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800228 int result = LoadDriver(ns, module);
229 if (result != 0) {
230 LOG_ALWAYS_FATAL(
231 "couldn't find an updated Vulkan implementation from %s",
232 android::GraphicsEnv::getInstance().getDriverPath().c_str());
233 }
234 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700235}
236
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800237bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800238 ATRACE_CALL();
239
Yiwei Zhangd9861812019-02-13 11:51:55 -0800240 const nsecs_t openTime = systemTime();
241
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700242 if (hal_.ShouldUnloadBuiltinDriver()) {
243 hal_.UnloadBuiltinDriver();
244 }
245
246 if (hal_.dev_)
247 return true;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800248
249 // Use a stub device unless we successfully open a real HAL device.
250 hal_.dev_ = &stubhal::kDevice;
251
Jesse Hall53457db2016-12-14 16:54:06 -0800252 int result;
253 const hwvulkan_module_t* module = nullptr;
254
Jesse Hall00e61ff2017-04-07 16:48:02 -0700255 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800256 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700257 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800258 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800259 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800260 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700261 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800262 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800263 return true;
264 }
265
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800266
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800267 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800268 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800269 result =
270 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
271 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800272 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800273 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800274 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700275 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800276 // Any device with a Vulkan HAL should be able to open the device.
277 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
278 result);
279 return false;
280 }
281
282 hal_.dev_ = device;
283
Chia-I Wu31938252016-05-23 15:31:02 +0800284 hal_.InitDebugReportIndex();
285
Yiwei Zhangd9861812019-02-13 11:51:55 -0800286 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700287 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800288
Chia-I Wu31938252016-05-23 15:31:02 +0800289 return true;
290}
291
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700292bool Hal::ShouldUnloadBuiltinDriver() {
293 // Should not unload since the driver was not loaded
294 if (!hal_.dev_)
295 return false;
296
297 // Should not unload if stubhal is used on the device
298 if (hal_.dev_ == &stubhal::kDevice)
299 return false;
300
301 // Unload the driver if updated driver is chosen
302 if (android::GraphicsEnv::getInstance().getDriverNamespace())
303 return true;
304
305 return false;
306}
307
308void Hal::UnloadBuiltinDriver() {
309 ATRACE_CALL();
310
311 ALOGD("Unload builtin Vulkan driver.");
312
313 // Close the opened device
314 ALOG_ASSERT(!hal_.dev_->common.close(hal_.dev_->common),
315 "hw_device_t::close() failed.");
316
317 // Close the opened shared library in the hw_module_t
Yiwei Zhang40e84f12020-10-14 08:42:26 -0700318 android_unload_sphal_library(hal_.dev_->common.module->dso);
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700319
320 hal_.dev_ = nullptr;
321 hal_.debug_report_index_ = -1;
322}
323
Chia-I Wu31938252016-05-23 15:31:02 +0800324bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800325 ATRACE_CALL();
326
Chia-I Wu31938252016-05-23 15:31:02 +0800327 uint32_t count;
328 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
329 VK_SUCCESS) {
330 ALOGE("failed to get HAL instance extension count");
331 return false;
332 }
333
334 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
335 malloc(sizeof(VkExtensionProperties) * count));
336 if (!exts) {
337 ALOGE("failed to allocate HAL instance extension array");
338 return false;
339 }
340
341 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
342 VK_SUCCESS) {
343 ALOGE("failed to enumerate HAL instance extensions");
344 free(exts);
345 return false;
346 }
347
348 for (uint32_t i = 0; i < count; i++) {
349 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
350 0) {
351 debug_report_index_ = static_cast<int>(i);
352 break;
353 }
354 }
355
356 free(exts);
357
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800358 return true;
359}
360
361CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700362 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800363 const VkAllocationCallbacks& allocator)
364 : is_instance_(true),
365 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700366 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700367 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800368 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800369 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700370 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800371
Chia-I Wu4901db72016-03-24 16:38:58 +0800372CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
373 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700374 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800375 const VkAllocationCallbacks& allocator)
376 : is_instance_(false),
377 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700378 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700379 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800380 physical_dev_(physical_dev),
381 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700382 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800383
384CreateInfoWrapper::~CreateInfoWrapper() {
385 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
386 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
387}
388
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800389VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700390 VkResult result = SanitizeApiVersion();
391 if (result == VK_SUCCESS)
392 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800393 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800394 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800395 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800396 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800397
398 return result;
399}
400
401const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800402CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800403 return hook_extensions_;
404}
405
406const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800407CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800408 return hal_extensions_;
409}
410
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800411CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
412 return &instance_info_;
413}
414
Chia-I Wu4901db72016-03-24 16:38:58 +0800415CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
416 return &dev_info_;
417}
418
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700419VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700420 if (!is_instance_ || !instance_info_.pApplicationInfo)
421 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700422
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700423 if (icd_api_version_ > VK_API_VERSION_1_0 ||
424 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
425 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700426
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700427 // override apiVersion to avoid error return from 1.0 icd
428 application_info_ = *instance_info_.pApplicationInfo;
429 application_info_.apiVersion = VK_API_VERSION_1_0;
430 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700431
432 return VK_SUCCESS;
433}
434
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800435VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800436 const struct StructHeader {
437 VkStructureType type;
438 const void* next;
439 } * header;
440
441 if (is_instance_) {
442 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
443
444 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
445 while (header &&
446 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
447 header = reinterpret_cast<const StructHeader*>(header->next);
448
449 instance_info_.pNext = header;
450 } else {
451 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
452
453 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
454 while (header &&
455 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
456 header = reinterpret_cast<const StructHeader*>(header->next);
457
458 dev_info_.pNext = header;
459 }
460
461 return VK_SUCCESS;
462}
463
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800464VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800465 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
466 : dev_info_.ppEnabledLayerNames;
467 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
468 : dev_info_.enabledLayerCount;
469
470 // remove all layers
471 layer_names = nullptr;
472 layer_count = 0;
473
474 return VK_SUCCESS;
475}
476
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800477VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800478 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
479 : dev_info_.ppEnabledExtensionNames;
480 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
481 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800482
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800483 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800484 if (result != VK_SUCCESS)
485 return result;
486
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700487 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700488 for (uint32_t i = 0; i < ext_count; i++) {
489 // Upon api downgrade, skip the promoted instance extensions in the
490 // first pass to avoid duplicate extensions.
491 const std::optional<uint32_t> version =
492 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700493 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700494 *version <= loader_api_version_)
495 continue;
496
497 FilterExtension(ext_names[i]);
498 }
499
500 // Enable the required extensions to support core functionalities.
501 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700502 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700503 for (const auto& promoted_extension : promoted_extensions)
504 FilterExtension(promoted_extension);
505 } else {
506 for (uint32_t i = 0; i < ext_count; i++)
507 FilterExtension(ext_names[i]);
508 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800509
Jesse Halld3d887a2018-03-05 13:34:45 -0800510 // Enable device extensions that contain physical-device commands, so that
511 // vkGetInstanceProcAddr will return those physical-device commands.
512 if (is_instance_) {
513 hook_extensions_.set(ProcHook::KHR_swapchain);
514 }
515
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700516 const uint32_t api_version =
517 is_instance_ ? loader_api_version_
518 : std::min(icd_api_version_, loader_api_version_);
519 switch (api_version) {
520 case VK_API_VERSION_1_1:
521 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
522 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
523 [[clang::fallthrough]];
524 case VK_API_VERSION_1_0:
525 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
526 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
527 break;
528 default:
529 ALOGE("Unknown API version[%u]", api_version);
530 break;
531 }
532
Chia-I Wu4901db72016-03-24 16:38:58 +0800533 ext_names = extension_filter_.names;
534 ext_count = extension_filter_.name_count;
535
536 return VK_SUCCESS;
537}
538
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800539VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800540 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800541 return Hal::Device().EnumerateInstanceExtensionProperties(
542 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800543 } else {
544 const auto& driver = GetData(physical_dev_).driver;
545 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
546 &count, nullptr);
547 }
548}
549
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800550VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800551 uint32_t& count,
552 VkExtensionProperties* props) const {
553 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800554 return Hal::Device().EnumerateInstanceExtensionProperties(
555 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800556 } else {
557 const auto& driver = GetData(physical_dev_).driver;
558 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
559 &count, props);
560 }
561}
562
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800563VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800564 // query extension count
565 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800566 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800567 if (result != VK_SUCCESS || count == 0)
568 return result;
569
570 auto& filter = extension_filter_;
571 filter.exts =
572 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
573 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
574 alignof(VkExtensionProperties),
575 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
576 if (!filter.exts)
577 return VK_ERROR_OUT_OF_HOST_MEMORY;
578
579 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800580 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800581 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
582 return result;
583
584 if (!count)
585 return VK_SUCCESS;
586
587 filter.ext_count = count;
588
589 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700590 if (is_instance_) {
591 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
592
593 // It requires enabling additional promoted extensions to downgrade api,
594 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700595 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700596 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700597 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700598 }
599
600 count = std::min(filter.ext_count, enabled_ext_count);
601 } else {
602 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
603 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700604
605 if (!count)
606 return VK_SUCCESS;
607
Chia-I Wu4901db72016-03-24 16:38:58 +0800608 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
609 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
610 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
611 if (!filter.names)
612 return VK_ERROR_OUT_OF_HOST_MEMORY;
613
614 return VK_SUCCESS;
615}
616
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800617void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800618 auto& filter = extension_filter_;
619
620 ProcHook::Extension ext_bit = GetProcHookExtension(name);
621 if (is_instance_) {
622 switch (ext_bit) {
623 case ProcHook::KHR_android_surface:
624 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700625 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300626 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800627 hook_extensions_.set(ext_bit);
628 // return now as these extensions do not require HAL support
629 return;
630 case ProcHook::EXT_debug_report:
631 // both we and HAL can take part in
632 hook_extensions_.set(ext_bit);
633 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300634 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700635 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700636 case ProcHook::KHR_external_memory_capabilities:
637 case ProcHook::KHR_external_semaphore_capabilities:
638 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700639 case ProcHook::EXTENSION_UNKNOWN:
640 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800641 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700642
Yiwei Zhang23143102019-04-10 18:24:05 -0700643 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700644 case ProcHook::KHR_incremental_present:
645 case ProcHook::KHR_shared_presentable_image:
646 case ProcHook::KHR_swapchain:
647 case ProcHook::EXT_hdr_metadata:
648 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
649 case ProcHook::ANDROID_native_buffer:
650 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700651 case ProcHook::EXTENSION_CORE_1_0:
652 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700653 case ProcHook::EXTENSION_CORE_1_2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700654 case ProcHook::EXTENSION_COUNT:
655 // Device and meta extensions. If we ever get here it's a bug in
656 // our code. But enumerating them lets us avoid having a default
657 // case, and default hides other bugs.
658 ALOGE(
659 "CreateInfoWrapper::FilterExtension: invalid instance "
660 "extension '%s'. FIX ME",
661 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800662 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700663
664 // Don't use a default case. Without it, -Wswitch will tell us
665 // at compile time if someone adds a new ProcHook extension but
666 // doesn't handle it above. That's a real bug that has
667 // not-immediately-obvious effects.
668 //
669 // default:
670 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800671 }
672 } else {
673 switch (ext_bit) {
674 case ProcHook::KHR_swapchain:
675 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
676 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
677 ext_bit = ProcHook::ANDROID_native_buffer;
678 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700679 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700680 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300681 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700682 hook_extensions_.set(ext_bit);
683 // return now as these extensions do not require HAL support
684 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700685 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700686 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700687 hook_extensions_.set(ext_bit);
688 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700689 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800690 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700691 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800692 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700693
694 case ProcHook::KHR_android_surface:
695 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700696 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700697 case ProcHook::KHR_external_memory_capabilities:
698 case ProcHook::KHR_external_semaphore_capabilities:
699 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700700 case ProcHook::KHR_get_surface_capabilities2:
701 case ProcHook::KHR_surface:
702 case ProcHook::EXT_debug_report:
703 case ProcHook::EXT_swapchain_colorspace:
704 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700705 case ProcHook::EXTENSION_CORE_1_0:
706 case ProcHook::EXTENSION_CORE_1_1:
Yiwei Zhang6be097b2020-10-19 20:22:05 -0700707 case ProcHook::EXTENSION_CORE_1_2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700708 case ProcHook::EXTENSION_COUNT:
709 // Instance and meta extensions. If we ever get here it's a bug
710 // in our code. But enumerating them lets us avoid having a
711 // default case, and default hides other bugs.
712 ALOGE(
713 "CreateInfoWrapper::FilterExtension: invalid device "
714 "extension '%s'. FIX ME",
715 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800716 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700717
718 // Don't use a default case. Without it, -Wswitch will tell us
719 // at compile time if someone adds a new ProcHook extension but
720 // doesn't handle it above. That's a real bug that has
721 // not-immediately-obvious effects.
722 //
723 // default:
724 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800725 }
726 }
727
728 for (uint32_t i = 0; i < filter.ext_count; i++) {
729 const VkExtensionProperties& props = filter.exts[i];
730 // ignore unknown extensions
731 if (strcmp(name, props.extensionName) != 0)
732 continue;
733
Chia-I Wu4901db72016-03-24 16:38:58 +0800734 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800735 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
736 if (ext_bit == ProcHook::ANDROID_native_buffer)
737 hook_extensions_.set(ProcHook::KHR_swapchain);
738
739 hal_extensions_.set(ext_bit);
740 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800741
742 break;
743 }
744}
745
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800746VKAPI_ATTR void* DefaultAllocate(void*,
747 size_t size,
748 size_t alignment,
749 VkSystemAllocationScope) {
750 void* ptr = nullptr;
751 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
752 // additionally requires that it be at least sizeof(void*).
753 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
754 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
755 ret, ptr);
756 return ret == 0 ? ptr : nullptr;
757}
758
759VKAPI_ATTR void* DefaultReallocate(void*,
760 void* ptr,
761 size_t size,
762 size_t alignment,
763 VkSystemAllocationScope) {
764 if (size == 0) {
765 free(ptr);
766 return nullptr;
767 }
768
Yiwei Zhanga885c062019-10-24 12:07:57 -0700769 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800770 // request is smaller than the existing chunk, we just continue using it.
771 // Right now the loader never reallocs, so this doesn't matter. If that
772 // changes, or if this code is copied into some other project, this should
773 // probably have a heuristic to allocate-copy-free when doing so will save
774 // "enough" space.
775 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
776 if (size <= old_size)
777 return ptr;
778
779 void* new_ptr = nullptr;
780 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
781 return nullptr;
782 if (ptr) {
783 memcpy(new_ptr, ptr, std::min(old_size, size));
784 free(ptr);
785 }
786 return new_ptr;
787}
788
789VKAPI_ATTR void DefaultFree(void*, void* ptr) {
790 ALOGD_CALLSTACK("Free: %p", ptr);
791 free(ptr);
792}
793
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800794InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
795 void* data_mem = allocator.pfnAllocation(
796 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
797 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
798 if (!data_mem)
799 return nullptr;
800
801 return new (data_mem) InstanceData(allocator);
802}
803
804void FreeInstanceData(InstanceData* data,
805 const VkAllocationCallbacks& allocator) {
806 data->~InstanceData();
807 allocator.pfnFree(allocator.pUserData, data);
808}
809
Chia-I Wu950d6e12016-05-03 09:12:35 +0800810DeviceData* AllocateDeviceData(
811 const VkAllocationCallbacks& allocator,
812 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800813 void* data_mem = allocator.pfnAllocation(
814 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
815 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
816 if (!data_mem)
817 return nullptr;
818
Chia-I Wu950d6e12016-05-03 09:12:35 +0800819 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800820}
821
822void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
823 data->~DeviceData();
824 allocator.pfnFree(allocator.pUserData, data);
825}
826
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800827} // anonymous namespace
828
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800829bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800830 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800831}
832
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800833const VkAllocationCallbacks& GetDefaultAllocator() {
834 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
835 .pUserData = nullptr,
836 .pfnAllocation = DefaultAllocate,
837 .pfnReallocation = DefaultReallocate,
838 .pfnFree = DefaultFree,
839 };
840
841 return kDefaultAllocCallbacks;
842}
843
Chia-I Wueb7db122016-03-24 09:11:06 +0800844PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
845 const ProcHook* hook = GetProcHook(pName);
846 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800847 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800848
849 if (!instance) {
850 if (hook->type == ProcHook::GLOBAL)
851 return hook->proc;
852
Chia-I Wu109f8982016-04-22 06:40:40 +0800853 // v0 layers expect
854 //
855 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
856 //
857 // to work.
858 if (strcmp(pName, "vkCreateDevice") == 0)
859 return hook->proc;
860
Chia-I Wueb7db122016-03-24 09:11:06 +0800861 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800862 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800863 pName);
864
Chia-I Wu109f8982016-04-22 06:40:40 +0800865 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800866 }
867
868 PFN_vkVoidFunction proc;
869
870 switch (hook->type) {
871 case ProcHook::INSTANCE:
872 proc = (GetData(instance).hook_extensions[hook->extension])
873 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800874 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800875 break;
876 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700877 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800878 ? hook->proc
879 : hook->checked_proc;
880 break;
881 default:
882 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800883 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800884 pName);
885 proc = nullptr;
886 break;
887 }
888
889 return proc;
890}
891
892PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
893 const ProcHook* hook = GetProcHook(pName);
894 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800895 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800896
897 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800898 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800899 return nullptr;
900 }
901
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800902 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
903 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800904}
905
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800906VkResult EnumerateInstanceExtensionProperties(
907 const char* pLayerName,
908 uint32_t* pPropertyCount,
909 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700910 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600911 loader_extensions.push_back({
912 VK_KHR_SURFACE_EXTENSION_NAME,
913 VK_KHR_SURFACE_SPEC_VERSION});
914 loader_extensions.push_back({
915 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
916 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
917 loader_extensions.push_back({
918 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
919 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700920 loader_extensions.push_back({
921 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
922 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600923
Chia-I Wu31938252016-05-23 15:31:02 +0800924 static const VkExtensionProperties loader_debug_report_extension = {
925 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
926 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800927
928 // enumerate our extensions first
929 if (!pLayerName && pProperties) {
930 uint32_t count = std::min(
931 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
932
Yiwei Zhang5e862202019-06-21 14:59:16 -0700933 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800934
935 if (count < loader_extensions.size()) {
936 *pPropertyCount = count;
937 return VK_INCOMPLETE;
938 }
939
940 pProperties += count;
941 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800942
943 if (Hal::Get().GetDebugReportIndex() < 0) {
944 if (!*pPropertyCount) {
945 *pPropertyCount = count;
946 return VK_INCOMPLETE;
947 }
948
949 pProperties[0] = loader_debug_report_extension;
950 pProperties += 1;
951 *pPropertyCount -= 1;
952 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800953 }
954
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800955 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800956 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800957 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800958 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800959
Chia-I Wu31938252016-05-23 15:31:02 +0800960 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
961 int idx = Hal::Get().GetDebugReportIndex();
962 if (idx < 0) {
963 *pPropertyCount += 1;
964 } else if (pProperties &&
965 static_cast<uint32_t>(idx) < *pPropertyCount) {
966 pProperties[idx].specVersion =
967 std::min(pProperties[idx].specVersion,
968 loader_debug_report_extension.specVersion);
969 }
970
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800971 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800972 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800973
974 return result;
975}
976
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700977void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +1300978 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700979 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300980 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700981 VkPhysicalDeviceProperties2 properties = {
982 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +1300983 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700984 {},
Chris Forbesfa25e632017-02-22 12:36:02 +1300985 };
986
987#pragma clang diagnostic push
988#pragma clang diagnostic ignored "-Wold-style-cast"
989 presentation_properties->sType =
990 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
991#pragma clang diagnostic pop
992 presentation_properties->pNext = nullptr;
993 presentation_properties->sharedImage = VK_FALSE;
994
Yiwei Zhanga55624b2020-07-05 16:05:26 -0700995 GetPhysicalDeviceProperties2(physicalDevice, &properties);
Chris Forbesfa25e632017-02-22 12:36:02 +1300996}
997
Chia-I Wu01cf3052016-03-24 16:16:21 +0800998VkResult EnumerateDeviceExtensionProperties(
999 VkPhysicalDevice physicalDevice,
1000 const char* pLayerName,
1001 uint32_t* pPropertyCount,
1002 VkExtensionProperties* pProperties) {
1003 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001004 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001005 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001006 loader_extensions.push_back({
1007 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1008 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001009
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001010 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001011 if (hdrBoardConfig) {
1012 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1013 VK_EXT_HDR_METADATA_SPEC_VERSION});
1014 }
1015
Chris Forbes16095002017-05-05 15:33:29 -07001016 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001017 QueryPresentationProperties(physicalDevice, &presentation_properties);
1018 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001019 loader_extensions.push_back({
1020 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1021 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001022 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001023
Ian Elliott5c34de22017-04-10 14:42:30 -06001024 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1025 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001026 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001027 loader_extensions.push_back({
1028 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1029 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1030 }
1031
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001032 // enumerate our extensions first
1033 if (!pLayerName && pProperties) {
1034 uint32_t count = std::min(
1035 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1036
Yiwei Zhang5e862202019-06-21 14:59:16 -07001037 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001038
1039 if (count < loader_extensions.size()) {
1040 *pPropertyCount = count;
1041 return VK_INCOMPLETE;
1042 }
1043
1044 pProperties += count;
1045 *pPropertyCount -= count;
1046 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001047
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001048 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001049 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1050 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001051 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001052
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001053 if (pProperties) {
1054 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1055 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1056 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001057
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001058 if (strcmp(prop.extensionName,
1059 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1060 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001061
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001062 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1063 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001064
1065 if (prop.specVersion >= 8) {
1066 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1067 } else {
1068 prop.specVersion = 68;
1069 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001070 }
1071 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001072
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001073 // restore loader extension count
1074 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1075 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001076 }
1077
1078 return result;
1079}
1080
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001081VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1082 const VkAllocationCallbacks* pAllocator,
1083 VkInstance* pInstance) {
1084 const VkAllocationCallbacks& data_allocator =
1085 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1086
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001087 VkResult result = VK_SUCCESS;
1088 uint32_t icd_api_version = VK_API_VERSION_1_0;
1089 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1090 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1091 Hal::Device().GetInstanceProcAddr(nullptr,
1092 "vkEnumerateInstanceVersion"));
1093 if (pfn_enumerate_instance_version) {
1094 ATRACE_BEGIN("pfn_enumerate_instance_version");
1095 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1096 ATRACE_END();
1097 if (result != VK_SUCCESS)
1098 return result;
1099
1100 icd_api_version ^= VK_VERSION_PATCH(icd_api_version);
1101 }
1102
1103 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1104 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001105 if (result != VK_SUCCESS)
1106 return result;
1107
1108 InstanceData* data = AllocateInstanceData(data_allocator);
1109 if (!data)
1110 return VK_ERROR_OUT_OF_HOST_MEMORY;
1111
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001112 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001113
1114 // call into the driver
1115 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001116 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001117 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001118 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1119 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001120 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001121 if (result != VK_SUCCESS) {
1122 FreeInstanceData(data, data_allocator);
1123 return result;
1124 }
1125
1126 // initialize InstanceDriverTable
1127 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001128 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001129 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001130 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001131 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001132 if (data->driver.DestroyInstance)
1133 data->driver.DestroyInstance(instance, pAllocator);
1134
1135 FreeInstanceData(data, data_allocator);
1136
1137 return VK_ERROR_INCOMPATIBLE_DRIVER;
1138 }
1139
1140 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001141 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001142 if (!data->get_device_proc_addr) {
1143 data->driver.DestroyInstance(instance, pAllocator);
1144 FreeInstanceData(data, data_allocator);
1145
1146 return VK_ERROR_INCOMPATIBLE_DRIVER;
1147 }
1148
1149 *pInstance = instance;
1150
1151 return VK_SUCCESS;
1152}
1153
1154void DestroyInstance(VkInstance instance,
1155 const VkAllocationCallbacks* pAllocator) {
1156 InstanceData& data = GetData(instance);
1157 data.driver.DestroyInstance(instance, pAllocator);
1158
1159 VkAllocationCallbacks local_allocator;
1160 if (!pAllocator) {
1161 local_allocator = data.allocator;
1162 pAllocator = &local_allocator;
1163 }
1164
1165 FreeInstanceData(&data, *pAllocator);
1166}
1167
Chia-I Wu4901db72016-03-24 16:38:58 +08001168VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1169 const VkDeviceCreateInfo* pCreateInfo,
1170 const VkAllocationCallbacks* pAllocator,
1171 VkDevice* pDevice) {
1172 const InstanceData& instance_data = GetData(physicalDevice);
1173 const VkAllocationCallbacks& data_allocator =
1174 (pAllocator) ? *pAllocator : instance_data.allocator;
1175
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001176 VkPhysicalDeviceProperties properties;
1177 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1178 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1179 &properties);
1180 ATRACE_END();
1181
1182 CreateInfoWrapper wrapper(
1183 physicalDevice, *pCreateInfo,
1184 properties.apiVersion ^ VK_VERSION_PATCH(properties.apiVersion),
1185 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001186 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001187 if (result != VK_SUCCESS)
1188 return result;
1189
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001190 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001191 DeviceData* data = AllocateDeviceData(data_allocator,
1192 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001193 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001194 if (!data)
1195 return VK_ERROR_OUT_OF_HOST_MEMORY;
1196
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001197 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001198
1199 // call into the driver
1200 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001201 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001202 result = instance_data.driver.CreateDevice(
1203 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1204 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001205 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001206 if (result != VK_SUCCESS) {
1207 FreeDeviceData(data, data_allocator);
1208 return result;
1209 }
1210
1211 // initialize DeviceDriverTable
1212 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001213 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1214 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001215 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1216 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1217 if (data->driver.DestroyDevice)
1218 data->driver.DestroyDevice(dev, pAllocator);
1219
1220 FreeDeviceData(data, data_allocator);
1221
1222 return VK_ERROR_INCOMPATIBLE_DRIVER;
1223 }
Chris Forbesd8277912017-02-10 14:59:59 +13001224
1225 // sanity check ANDROID_native_buffer implementation, whose set of
1226 // entrypoints varies according to the spec version.
1227 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1228 !data->driver.GetSwapchainGrallocUsageANDROID &&
1229 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1230 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1231 " must expose at least one of "
1232 "vkGetSwapchainGrallocUsageANDROID or "
1233 "vkGetSwapchainGrallocUsage2ANDROID");
1234
1235 data->driver.DestroyDevice(dev, pAllocator);
1236 FreeDeviceData(data, data_allocator);
1237
1238 return VK_ERROR_INCOMPATIBLE_DRIVER;
1239 }
1240
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001241 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1242 // Log that the app is hitting software Vulkan implementation
1243 android::GraphicsEnv::getInstance().setTargetStats(
1244 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1245 }
1246
Jesse Halldc225072016-05-30 22:40:14 -07001247 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001248
1249 *pDevice = dev;
1250
1251 return VK_SUCCESS;
1252}
1253
1254void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1255 DeviceData& data = GetData(device);
1256 data.driver.DestroyDevice(device, pAllocator);
1257
1258 VkAllocationCallbacks local_allocator;
1259 if (!pAllocator) {
1260 local_allocator = data.allocator;
1261 pAllocator = &local_allocator;
1262 }
1263
1264 FreeDeviceData(&data, *pAllocator);
1265}
1266
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001267VkResult EnumeratePhysicalDevices(VkInstance instance,
1268 uint32_t* pPhysicalDeviceCount,
1269 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001270 ATRACE_CALL();
1271
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001272 const auto& data = GetData(instance);
1273
1274 VkResult result = data.driver.EnumeratePhysicalDevices(
1275 instance, pPhysicalDeviceCount, pPhysicalDevices);
1276 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1277 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1278 SetData(pPhysicalDevices[i], data);
1279 }
1280
1281 return result;
1282}
1283
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001284VkResult EnumeratePhysicalDeviceGroups(
1285 VkInstance instance,
1286 uint32_t* pPhysicalDeviceGroupCount,
1287 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001288 ATRACE_CALL();
1289
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001290 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001291 const auto& data = GetData(instance);
1292
Yiwei Zhange4f64172020-07-05 15:17:32 -07001293 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1294 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001295 uint32_t device_count = 0;
1296 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1297 if (result < 0)
1298 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001299
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001300 if (!pPhysicalDeviceGroupProperties) {
1301 *pPhysicalDeviceGroupCount = device_count;
1302 return result;
1303 }
1304
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001305 if (!device_count) {
1306 *pPhysicalDeviceGroupCount = 0;
1307 return result;
1308 }
Chad Versace32c087f2018-09-09 07:28:05 -07001309 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1310 if (!device_count)
1311 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001312
Yiwei Zhang5e862202019-06-21 14:59:16 -07001313 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001314 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001315 result =
1316 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001317 if (result < 0)
1318 return result;
1319
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001320 for (uint32_t i = 0; i < device_count; ++i) {
1321 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1322 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1323 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1324 }
1325 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001326 if (data.driver.EnumeratePhysicalDeviceGroups) {
1327 result = data.driver.EnumeratePhysicalDeviceGroups(
1328 instance, pPhysicalDeviceGroupCount,
1329 pPhysicalDeviceGroupProperties);
1330 } else {
1331 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1332 instance, pPhysicalDeviceGroupCount,
1333 pPhysicalDeviceGroupProperties);
1334 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001335 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1336 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1337 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1338 for (uint32_t j = 0;
1339 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1340 j++) {
1341 SetData(
1342 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001343 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001344 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001345 }
1346 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001347 }
1348
1349 return result;
1350}
1351
Chia-I Wuba0be412016-03-24 16:24:40 +08001352void GetDeviceQueue(VkDevice device,
1353 uint32_t queueFamilyIndex,
1354 uint32_t queueIndex,
1355 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001356 ATRACE_CALL();
1357
Chia-I Wuba0be412016-03-24 16:24:40 +08001358 const auto& data = GetData(device);
1359
1360 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1361 SetData(*pQueue, data);
1362}
1363
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001364void GetDeviceQueue2(VkDevice device,
1365 const VkDeviceQueueInfo2* pQueueInfo,
1366 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001367 ATRACE_CALL();
1368
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001369 const auto& data = GetData(device);
1370
1371 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001372 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001373}
1374
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001375VkResult AllocateCommandBuffers(
1376 VkDevice device,
1377 const VkCommandBufferAllocateInfo* pAllocateInfo,
1378 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001379 ATRACE_CALL();
1380
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001381 const auto& data = GetData(device);
1382
1383 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1384 pCommandBuffers);
1385 if (result == VK_SUCCESS) {
1386 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1387 SetData(pCommandBuffers[i], data);
1388 }
1389
1390 return result;
1391}
1392
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001393VkResult QueueSubmit(VkQueue queue,
1394 uint32_t submitCount,
1395 const VkSubmitInfo* pSubmits,
1396 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001397 ATRACE_CALL();
1398
1399 const auto& data = GetData(queue);
1400
1401 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1402}
1403
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001404void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1405 VkPhysicalDeviceFeatures2* pFeatures) {
1406 ATRACE_CALL();
1407
1408 const auto& driver = GetData(physicalDevice).driver;
1409
1410 if (driver.GetPhysicalDeviceFeatures2) {
1411 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
1412 return;
1413 }
1414
1415 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1416}
1417
1418void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1419 VkPhysicalDeviceProperties2* pProperties) {
1420 ATRACE_CALL();
1421
1422 const auto& driver = GetData(physicalDevice).driver;
1423
1424 if (driver.GetPhysicalDeviceProperties2) {
1425 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1426 return;
1427 }
1428
1429 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1430}
1431
1432void GetPhysicalDeviceFormatProperties2(
1433 VkPhysicalDevice physicalDevice,
1434 VkFormat format,
1435 VkFormatProperties2* pFormatProperties) {
1436 ATRACE_CALL();
1437
1438 const auto& driver = GetData(physicalDevice).driver;
1439
1440 if (driver.GetPhysicalDeviceFormatProperties2) {
1441 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1442 pFormatProperties);
1443 return;
1444 }
1445
1446 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1447 pFormatProperties);
1448}
1449
1450VkResult GetPhysicalDeviceImageFormatProperties2(
1451 VkPhysicalDevice physicalDevice,
1452 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1453 VkImageFormatProperties2* pImageFormatProperties) {
1454 ATRACE_CALL();
1455
1456 const auto& driver = GetData(physicalDevice).driver;
1457
1458 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1459 return driver.GetPhysicalDeviceImageFormatProperties2(
1460 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1461 }
1462
1463 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1464 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1465}
1466
1467void GetPhysicalDeviceQueueFamilyProperties2(
1468 VkPhysicalDevice physicalDevice,
1469 uint32_t* pQueueFamilyPropertyCount,
1470 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1471 ATRACE_CALL();
1472
1473 const auto& driver = GetData(physicalDevice).driver;
1474
1475 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1476 driver.GetPhysicalDeviceQueueFamilyProperties2(
1477 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1478 return;
1479 }
1480
1481 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1482 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1483}
1484
1485void GetPhysicalDeviceMemoryProperties2(
1486 VkPhysicalDevice physicalDevice,
1487 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1488 ATRACE_CALL();
1489
1490 const auto& driver = GetData(physicalDevice).driver;
1491
1492 if (driver.GetPhysicalDeviceMemoryProperties2) {
1493 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1494 pMemoryProperties);
1495 return;
1496 }
1497
1498 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1499 pMemoryProperties);
1500}
1501
1502void GetPhysicalDeviceSparseImageFormatProperties2(
1503 VkPhysicalDevice physicalDevice,
1504 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1505 uint32_t* pPropertyCount,
1506 VkSparseImageFormatProperties2* pProperties) {
1507 ATRACE_CALL();
1508
1509 const auto& driver = GetData(physicalDevice).driver;
1510
1511 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1512 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1513 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1514 return;
1515 }
1516
1517 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1518 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1519}
1520
Yiwei Zhange1f35012020-07-05 22:52:04 -07001521void GetPhysicalDeviceExternalBufferProperties(
1522 VkPhysicalDevice physicalDevice,
1523 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1524 VkExternalBufferProperties* pExternalBufferProperties) {
1525 ATRACE_CALL();
1526
1527 const auto& driver = GetData(physicalDevice).driver;
1528
1529 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1530 driver.GetPhysicalDeviceExternalBufferProperties(
1531 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1532 return;
1533 }
1534
1535 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1536 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1537 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1538 return;
1539 }
1540
1541 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1542 sizeof(VkExternalMemoryProperties));
1543}
1544
1545void GetPhysicalDeviceExternalSemaphoreProperties(
1546 VkPhysicalDevice physicalDevice,
1547 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1548 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1549 ATRACE_CALL();
1550
1551 const auto& driver = GetData(physicalDevice).driver;
1552
1553 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1554 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1555 physicalDevice, pExternalSemaphoreInfo,
1556 pExternalSemaphoreProperties);
1557 return;
1558 }
1559
1560 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1561 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1562 physicalDevice, pExternalSemaphoreInfo,
1563 pExternalSemaphoreProperties);
1564 return;
1565 }
1566
1567 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1568 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1569 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1570}
1571
1572void GetPhysicalDeviceExternalFenceProperties(
1573 VkPhysicalDevice physicalDevice,
1574 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1575 VkExternalFenceProperties* pExternalFenceProperties) {
1576 ATRACE_CALL();
1577
1578 const auto& driver = GetData(physicalDevice).driver;
1579
1580 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1581 driver.GetPhysicalDeviceExternalFenceProperties(
1582 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1583 return;
1584 }
1585
1586 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1587 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1588 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1589 return;
1590 }
1591
1592 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1593 pExternalFenceProperties->compatibleHandleTypes = 0;
1594 pExternalFenceProperties->externalFenceFeatures = 0;
1595}
1596
Chia-I Wu9d518162016-03-24 14:55:27 +08001597} // namespace driver
1598} // namespace vulkan