blob: 5c1d023ce9edd045711ca0b92161ac3d92981613 [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:
Jesse Hall7f983a82018-03-29 14:46:45 -0700653 case ProcHook::EXTENSION_COUNT:
654 // Device and meta extensions. If we ever get here it's a bug in
655 // our code. But enumerating them lets us avoid having a default
656 // case, and default hides other bugs.
657 ALOGE(
658 "CreateInfoWrapper::FilterExtension: invalid instance "
659 "extension '%s'. FIX ME",
660 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800661 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700662
663 // Don't use a default case. Without it, -Wswitch will tell us
664 // at compile time if someone adds a new ProcHook extension but
665 // doesn't handle it above. That's a real bug that has
666 // not-immediately-obvious effects.
667 //
668 // default:
669 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800670 }
671 } else {
672 switch (ext_bit) {
673 case ProcHook::KHR_swapchain:
674 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
675 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
676 ext_bit = ProcHook::ANDROID_native_buffer;
677 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700678 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700679 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300680 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700681 hook_extensions_.set(ext_bit);
682 // return now as these extensions do not require HAL support
683 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700684 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700685 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700686 hook_extensions_.set(ext_bit);
687 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700688 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800689 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700690 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800691 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700692
693 case ProcHook::KHR_android_surface:
694 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700695 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700696 case ProcHook::KHR_external_memory_capabilities:
697 case ProcHook::KHR_external_semaphore_capabilities:
698 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700699 case ProcHook::KHR_get_surface_capabilities2:
700 case ProcHook::KHR_surface:
701 case ProcHook::EXT_debug_report:
702 case ProcHook::EXT_swapchain_colorspace:
703 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700704 case ProcHook::EXTENSION_CORE_1_0:
705 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700706 case ProcHook::EXTENSION_COUNT:
707 // Instance and meta extensions. If we ever get here it's a bug
708 // in our code. But enumerating them lets us avoid having a
709 // default case, and default hides other bugs.
710 ALOGE(
711 "CreateInfoWrapper::FilterExtension: invalid device "
712 "extension '%s'. FIX ME",
713 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800714 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700715
716 // Don't use a default case. Without it, -Wswitch will tell us
717 // at compile time if someone adds a new ProcHook extension but
718 // doesn't handle it above. That's a real bug that has
719 // not-immediately-obvious effects.
720 //
721 // default:
722 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800723 }
724 }
725
726 for (uint32_t i = 0; i < filter.ext_count; i++) {
727 const VkExtensionProperties& props = filter.exts[i];
728 // ignore unknown extensions
729 if (strcmp(name, props.extensionName) != 0)
730 continue;
731
Chia-I Wu4901db72016-03-24 16:38:58 +0800732 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800733 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
734 if (ext_bit == ProcHook::ANDROID_native_buffer)
735 hook_extensions_.set(ProcHook::KHR_swapchain);
736
737 hal_extensions_.set(ext_bit);
738 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800739
740 break;
741 }
742}
743
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800744VKAPI_ATTR void* DefaultAllocate(void*,
745 size_t size,
746 size_t alignment,
747 VkSystemAllocationScope) {
748 void* ptr = nullptr;
749 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
750 // additionally requires that it be at least sizeof(void*).
751 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
752 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
753 ret, ptr);
754 return ret == 0 ? ptr : nullptr;
755}
756
757VKAPI_ATTR void* DefaultReallocate(void*,
758 void* ptr,
759 size_t size,
760 size_t alignment,
761 VkSystemAllocationScope) {
762 if (size == 0) {
763 free(ptr);
764 return nullptr;
765 }
766
Yiwei Zhanga885c062019-10-24 12:07:57 -0700767 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800768 // request is smaller than the existing chunk, we just continue using it.
769 // Right now the loader never reallocs, so this doesn't matter. If that
770 // changes, or if this code is copied into some other project, this should
771 // probably have a heuristic to allocate-copy-free when doing so will save
772 // "enough" space.
773 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
774 if (size <= old_size)
775 return ptr;
776
777 void* new_ptr = nullptr;
778 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
779 return nullptr;
780 if (ptr) {
781 memcpy(new_ptr, ptr, std::min(old_size, size));
782 free(ptr);
783 }
784 return new_ptr;
785}
786
787VKAPI_ATTR void DefaultFree(void*, void* ptr) {
788 ALOGD_CALLSTACK("Free: %p", ptr);
789 free(ptr);
790}
791
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800792InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
793 void* data_mem = allocator.pfnAllocation(
794 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
795 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
796 if (!data_mem)
797 return nullptr;
798
799 return new (data_mem) InstanceData(allocator);
800}
801
802void FreeInstanceData(InstanceData* data,
803 const VkAllocationCallbacks& allocator) {
804 data->~InstanceData();
805 allocator.pfnFree(allocator.pUserData, data);
806}
807
Chia-I Wu950d6e12016-05-03 09:12:35 +0800808DeviceData* AllocateDeviceData(
809 const VkAllocationCallbacks& allocator,
810 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800811 void* data_mem = allocator.pfnAllocation(
812 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
813 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
814 if (!data_mem)
815 return nullptr;
816
Chia-I Wu950d6e12016-05-03 09:12:35 +0800817 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800818}
819
820void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
821 data->~DeviceData();
822 allocator.pfnFree(allocator.pUserData, data);
823}
824
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800825} // anonymous namespace
826
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800827bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800828 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800829}
830
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800831const VkAllocationCallbacks& GetDefaultAllocator() {
832 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
833 .pUserData = nullptr,
834 .pfnAllocation = DefaultAllocate,
835 .pfnReallocation = DefaultReallocate,
836 .pfnFree = DefaultFree,
837 };
838
839 return kDefaultAllocCallbacks;
840}
841
Chia-I Wueb7db122016-03-24 09:11:06 +0800842PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
843 const ProcHook* hook = GetProcHook(pName);
844 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800845 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800846
847 if (!instance) {
848 if (hook->type == ProcHook::GLOBAL)
849 return hook->proc;
850
Chia-I Wu109f8982016-04-22 06:40:40 +0800851 // v0 layers expect
852 //
853 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
854 //
855 // to work.
856 if (strcmp(pName, "vkCreateDevice") == 0)
857 return hook->proc;
858
Chia-I Wueb7db122016-03-24 09:11:06 +0800859 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800860 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800861 pName);
862
Chia-I Wu109f8982016-04-22 06:40:40 +0800863 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800864 }
865
866 PFN_vkVoidFunction proc;
867
868 switch (hook->type) {
869 case ProcHook::INSTANCE:
870 proc = (GetData(instance).hook_extensions[hook->extension])
871 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800872 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800873 break;
874 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700875 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800876 ? hook->proc
877 : hook->checked_proc;
878 break;
879 default:
880 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800881 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800882 pName);
883 proc = nullptr;
884 break;
885 }
886
887 return proc;
888}
889
890PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
891 const ProcHook* hook = GetProcHook(pName);
892 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800893 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800894
895 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800896 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800897 return nullptr;
898 }
899
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800900 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
901 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800902}
903
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800904VkResult EnumerateInstanceExtensionProperties(
905 const char* pLayerName,
906 uint32_t* pPropertyCount,
907 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700908 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600909 loader_extensions.push_back({
910 VK_KHR_SURFACE_EXTENSION_NAME,
911 VK_KHR_SURFACE_SPEC_VERSION});
912 loader_extensions.push_back({
913 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
914 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
915 loader_extensions.push_back({
916 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
917 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700918 loader_extensions.push_back({
919 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
920 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600921
Chia-I Wu31938252016-05-23 15:31:02 +0800922 static const VkExtensionProperties loader_debug_report_extension = {
923 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
924 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800925
926 // enumerate our extensions first
927 if (!pLayerName && pProperties) {
928 uint32_t count = std::min(
929 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
930
Yiwei Zhang5e862202019-06-21 14:59:16 -0700931 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800932
933 if (count < loader_extensions.size()) {
934 *pPropertyCount = count;
935 return VK_INCOMPLETE;
936 }
937
938 pProperties += count;
939 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800940
941 if (Hal::Get().GetDebugReportIndex() < 0) {
942 if (!*pPropertyCount) {
943 *pPropertyCount = count;
944 return VK_INCOMPLETE;
945 }
946
947 pProperties[0] = loader_debug_report_extension;
948 pProperties += 1;
949 *pPropertyCount -= 1;
950 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800951 }
952
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800953 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800954 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800955 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800956 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800957
Chia-I Wu31938252016-05-23 15:31:02 +0800958 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
959 int idx = Hal::Get().GetDebugReportIndex();
960 if (idx < 0) {
961 *pPropertyCount += 1;
962 } else if (pProperties &&
963 static_cast<uint32_t>(idx) < *pPropertyCount) {
964 pProperties[idx].specVersion =
965 std::min(pProperties[idx].specVersion,
966 loader_debug_report_extension.specVersion);
967 }
968
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800969 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800970 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800971
972 return result;
973}
974
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700975void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +1300976 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700977 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300978 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700979 VkPhysicalDeviceProperties2 properties = {
980 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +1300981 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700982 {},
Chris Forbesfa25e632017-02-22 12:36:02 +1300983 };
984
985#pragma clang diagnostic push
986#pragma clang diagnostic ignored "-Wold-style-cast"
987 presentation_properties->sType =
988 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
989#pragma clang diagnostic pop
990 presentation_properties->pNext = nullptr;
991 presentation_properties->sharedImage = VK_FALSE;
992
Yiwei Zhanga55624b2020-07-05 16:05:26 -0700993 GetPhysicalDeviceProperties2(physicalDevice, &properties);
Chris Forbesfa25e632017-02-22 12:36:02 +1300994}
995
Chia-I Wu01cf3052016-03-24 16:16:21 +0800996VkResult EnumerateDeviceExtensionProperties(
997 VkPhysicalDevice physicalDevice,
998 const char* pLayerName,
999 uint32_t* pPropertyCount,
1000 VkExtensionProperties* pProperties) {
1001 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001002 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001003 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001004 loader_extensions.push_back({
1005 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1006 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001007
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001008 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001009 if (hdrBoardConfig) {
1010 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1011 VK_EXT_HDR_METADATA_SPEC_VERSION});
1012 }
1013
Chris Forbes16095002017-05-05 15:33:29 -07001014 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001015 QueryPresentationProperties(physicalDevice, &presentation_properties);
1016 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001017 loader_extensions.push_back({
1018 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1019 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001020 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001021
Ian Elliott5c34de22017-04-10 14:42:30 -06001022 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1023 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -07001024 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001025 loader_extensions.push_back({
1026 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1027 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1028 }
1029
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001030 // enumerate our extensions first
1031 if (!pLayerName && pProperties) {
1032 uint32_t count = std::min(
1033 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1034
Yiwei Zhang5e862202019-06-21 14:59:16 -07001035 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001036
1037 if (count < loader_extensions.size()) {
1038 *pPropertyCount = count;
1039 return VK_INCOMPLETE;
1040 }
1041
1042 pProperties += count;
1043 *pPropertyCount -= count;
1044 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001045
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001046 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001047 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1048 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001049 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001050
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001051 if (pProperties) {
1052 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1053 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1054 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001055
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001056 if (strcmp(prop.extensionName,
1057 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1058 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001059
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001060 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1061 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001062
1063 if (prop.specVersion >= 8) {
1064 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1065 } else {
1066 prop.specVersion = 68;
1067 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001068 }
1069 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001070
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001071 // restore loader extension count
1072 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1073 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001074 }
1075
1076 return result;
1077}
1078
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001079VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1080 const VkAllocationCallbacks* pAllocator,
1081 VkInstance* pInstance) {
1082 const VkAllocationCallbacks& data_allocator =
1083 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1084
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001085 VkResult result = VK_SUCCESS;
1086 uint32_t icd_api_version = VK_API_VERSION_1_0;
1087 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1088 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1089 Hal::Device().GetInstanceProcAddr(nullptr,
1090 "vkEnumerateInstanceVersion"));
1091 if (pfn_enumerate_instance_version) {
1092 ATRACE_BEGIN("pfn_enumerate_instance_version");
1093 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1094 ATRACE_END();
1095 if (result != VK_SUCCESS)
1096 return result;
1097
1098 icd_api_version ^= VK_VERSION_PATCH(icd_api_version);
1099 }
1100
1101 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1102 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001103 if (result != VK_SUCCESS)
1104 return result;
1105
1106 InstanceData* data = AllocateInstanceData(data_allocator);
1107 if (!data)
1108 return VK_ERROR_OUT_OF_HOST_MEMORY;
1109
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001110 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001111
1112 // call into the driver
1113 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001114 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001115 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001116 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1117 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001118 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001119 if (result != VK_SUCCESS) {
1120 FreeInstanceData(data, data_allocator);
1121 return result;
1122 }
1123
1124 // initialize InstanceDriverTable
1125 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001126 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001127 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001128 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001129 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001130 if (data->driver.DestroyInstance)
1131 data->driver.DestroyInstance(instance, pAllocator);
1132
1133 FreeInstanceData(data, data_allocator);
1134
1135 return VK_ERROR_INCOMPATIBLE_DRIVER;
1136 }
1137
1138 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001139 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001140 if (!data->get_device_proc_addr) {
1141 data->driver.DestroyInstance(instance, pAllocator);
1142 FreeInstanceData(data, data_allocator);
1143
1144 return VK_ERROR_INCOMPATIBLE_DRIVER;
1145 }
1146
1147 *pInstance = instance;
1148
1149 return VK_SUCCESS;
1150}
1151
1152void DestroyInstance(VkInstance instance,
1153 const VkAllocationCallbacks* pAllocator) {
1154 InstanceData& data = GetData(instance);
1155 data.driver.DestroyInstance(instance, pAllocator);
1156
1157 VkAllocationCallbacks local_allocator;
1158 if (!pAllocator) {
1159 local_allocator = data.allocator;
1160 pAllocator = &local_allocator;
1161 }
1162
1163 FreeInstanceData(&data, *pAllocator);
1164}
1165
Chia-I Wu4901db72016-03-24 16:38:58 +08001166VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1167 const VkDeviceCreateInfo* pCreateInfo,
1168 const VkAllocationCallbacks* pAllocator,
1169 VkDevice* pDevice) {
1170 const InstanceData& instance_data = GetData(physicalDevice);
1171 const VkAllocationCallbacks& data_allocator =
1172 (pAllocator) ? *pAllocator : instance_data.allocator;
1173
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001174 VkPhysicalDeviceProperties properties;
1175 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1176 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1177 &properties);
1178 ATRACE_END();
1179
1180 CreateInfoWrapper wrapper(
1181 physicalDevice, *pCreateInfo,
1182 properties.apiVersion ^ VK_VERSION_PATCH(properties.apiVersion),
1183 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001184 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001185 if (result != VK_SUCCESS)
1186 return result;
1187
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001188 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001189 DeviceData* data = AllocateDeviceData(data_allocator,
1190 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001191 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001192 if (!data)
1193 return VK_ERROR_OUT_OF_HOST_MEMORY;
1194
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001195 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001196
1197 // call into the driver
1198 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001199 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001200 result = instance_data.driver.CreateDevice(
1201 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1202 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001203 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001204 if (result != VK_SUCCESS) {
1205 FreeDeviceData(data, data_allocator);
1206 return result;
1207 }
1208
1209 // initialize DeviceDriverTable
1210 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001211 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1212 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001213 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1214 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1215 if (data->driver.DestroyDevice)
1216 data->driver.DestroyDevice(dev, pAllocator);
1217
1218 FreeDeviceData(data, data_allocator);
1219
1220 return VK_ERROR_INCOMPATIBLE_DRIVER;
1221 }
Chris Forbesd8277912017-02-10 14:59:59 +13001222
1223 // sanity check ANDROID_native_buffer implementation, whose set of
1224 // entrypoints varies according to the spec version.
1225 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1226 !data->driver.GetSwapchainGrallocUsageANDROID &&
1227 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1228 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1229 " must expose at least one of "
1230 "vkGetSwapchainGrallocUsageANDROID or "
1231 "vkGetSwapchainGrallocUsage2ANDROID");
1232
1233 data->driver.DestroyDevice(dev, pAllocator);
1234 FreeDeviceData(data, data_allocator);
1235
1236 return VK_ERROR_INCOMPATIBLE_DRIVER;
1237 }
1238
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001239 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1240 // Log that the app is hitting software Vulkan implementation
1241 android::GraphicsEnv::getInstance().setTargetStats(
1242 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1243 }
1244
Jesse Halldc225072016-05-30 22:40:14 -07001245 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001246
1247 *pDevice = dev;
1248
1249 return VK_SUCCESS;
1250}
1251
1252void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1253 DeviceData& data = GetData(device);
1254 data.driver.DestroyDevice(device, pAllocator);
1255
1256 VkAllocationCallbacks local_allocator;
1257 if (!pAllocator) {
1258 local_allocator = data.allocator;
1259 pAllocator = &local_allocator;
1260 }
1261
1262 FreeDeviceData(&data, *pAllocator);
1263}
1264
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001265VkResult EnumeratePhysicalDevices(VkInstance instance,
1266 uint32_t* pPhysicalDeviceCount,
1267 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001268 ATRACE_CALL();
1269
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001270 const auto& data = GetData(instance);
1271
1272 VkResult result = data.driver.EnumeratePhysicalDevices(
1273 instance, pPhysicalDeviceCount, pPhysicalDevices);
1274 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1275 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1276 SetData(pPhysicalDevices[i], data);
1277 }
1278
1279 return result;
1280}
1281
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001282VkResult EnumeratePhysicalDeviceGroups(
1283 VkInstance instance,
1284 uint32_t* pPhysicalDeviceGroupCount,
1285 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001286 ATRACE_CALL();
1287
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001288 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001289 const auto& data = GetData(instance);
1290
Yiwei Zhange4f64172020-07-05 15:17:32 -07001291 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1292 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001293 uint32_t device_count = 0;
1294 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1295 if (result < 0)
1296 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001297
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001298 if (!pPhysicalDeviceGroupProperties) {
1299 *pPhysicalDeviceGroupCount = device_count;
1300 return result;
1301 }
1302
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001303 if (!device_count) {
1304 *pPhysicalDeviceGroupCount = 0;
1305 return result;
1306 }
Chad Versace32c087f2018-09-09 07:28:05 -07001307 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1308 if (!device_count)
1309 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001310
Yiwei Zhang5e862202019-06-21 14:59:16 -07001311 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001312 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001313 result =
1314 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001315 if (result < 0)
1316 return result;
1317
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001318 for (uint32_t i = 0; i < device_count; ++i) {
1319 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1320 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1321 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1322 }
1323 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001324 if (data.driver.EnumeratePhysicalDeviceGroups) {
1325 result = data.driver.EnumeratePhysicalDeviceGroups(
1326 instance, pPhysicalDeviceGroupCount,
1327 pPhysicalDeviceGroupProperties);
1328 } else {
1329 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1330 instance, pPhysicalDeviceGroupCount,
1331 pPhysicalDeviceGroupProperties);
1332 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001333 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1334 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1335 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1336 for (uint32_t j = 0;
1337 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1338 j++) {
1339 SetData(
1340 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001341 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001342 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001343 }
1344 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001345 }
1346
1347 return result;
1348}
1349
Chia-I Wuba0be412016-03-24 16:24:40 +08001350void GetDeviceQueue(VkDevice device,
1351 uint32_t queueFamilyIndex,
1352 uint32_t queueIndex,
1353 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001354 ATRACE_CALL();
1355
Chia-I Wuba0be412016-03-24 16:24:40 +08001356 const auto& data = GetData(device);
1357
1358 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1359 SetData(*pQueue, data);
1360}
1361
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001362void GetDeviceQueue2(VkDevice device,
1363 const VkDeviceQueueInfo2* pQueueInfo,
1364 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001365 ATRACE_CALL();
1366
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001367 const auto& data = GetData(device);
1368
1369 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001370 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001371}
1372
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001373VkResult AllocateCommandBuffers(
1374 VkDevice device,
1375 const VkCommandBufferAllocateInfo* pAllocateInfo,
1376 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001377 ATRACE_CALL();
1378
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001379 const auto& data = GetData(device);
1380
1381 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1382 pCommandBuffers);
1383 if (result == VK_SUCCESS) {
1384 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1385 SetData(pCommandBuffers[i], data);
1386 }
1387
1388 return result;
1389}
1390
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001391VkResult QueueSubmit(VkQueue queue,
1392 uint32_t submitCount,
1393 const VkSubmitInfo* pSubmits,
1394 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001395 ATRACE_CALL();
1396
1397 const auto& data = GetData(queue);
1398
1399 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1400}
1401
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001402void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1403 VkPhysicalDeviceFeatures2* pFeatures) {
1404 ATRACE_CALL();
1405
1406 const auto& driver = GetData(physicalDevice).driver;
1407
1408 if (driver.GetPhysicalDeviceFeatures2) {
1409 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
1410 return;
1411 }
1412
1413 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1414}
1415
1416void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1417 VkPhysicalDeviceProperties2* pProperties) {
1418 ATRACE_CALL();
1419
1420 const auto& driver = GetData(physicalDevice).driver;
1421
1422 if (driver.GetPhysicalDeviceProperties2) {
1423 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1424 return;
1425 }
1426
1427 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1428}
1429
1430void GetPhysicalDeviceFormatProperties2(
1431 VkPhysicalDevice physicalDevice,
1432 VkFormat format,
1433 VkFormatProperties2* pFormatProperties) {
1434 ATRACE_CALL();
1435
1436 const auto& driver = GetData(physicalDevice).driver;
1437
1438 if (driver.GetPhysicalDeviceFormatProperties2) {
1439 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1440 pFormatProperties);
1441 return;
1442 }
1443
1444 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1445 pFormatProperties);
1446}
1447
1448VkResult GetPhysicalDeviceImageFormatProperties2(
1449 VkPhysicalDevice physicalDevice,
1450 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1451 VkImageFormatProperties2* pImageFormatProperties) {
1452 ATRACE_CALL();
1453
1454 const auto& driver = GetData(physicalDevice).driver;
1455
1456 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1457 return driver.GetPhysicalDeviceImageFormatProperties2(
1458 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1459 }
1460
1461 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1462 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1463}
1464
1465void GetPhysicalDeviceQueueFamilyProperties2(
1466 VkPhysicalDevice physicalDevice,
1467 uint32_t* pQueueFamilyPropertyCount,
1468 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1469 ATRACE_CALL();
1470
1471 const auto& driver = GetData(physicalDevice).driver;
1472
1473 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1474 driver.GetPhysicalDeviceQueueFamilyProperties2(
1475 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1476 return;
1477 }
1478
1479 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1480 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1481}
1482
1483void GetPhysicalDeviceMemoryProperties2(
1484 VkPhysicalDevice physicalDevice,
1485 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1486 ATRACE_CALL();
1487
1488 const auto& driver = GetData(physicalDevice).driver;
1489
1490 if (driver.GetPhysicalDeviceMemoryProperties2) {
1491 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1492 pMemoryProperties);
1493 return;
1494 }
1495
1496 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1497 pMemoryProperties);
1498}
1499
1500void GetPhysicalDeviceSparseImageFormatProperties2(
1501 VkPhysicalDevice physicalDevice,
1502 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1503 uint32_t* pPropertyCount,
1504 VkSparseImageFormatProperties2* pProperties) {
1505 ATRACE_CALL();
1506
1507 const auto& driver = GetData(physicalDevice).driver;
1508
1509 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1510 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1511 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1512 return;
1513 }
1514
1515 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1516 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1517}
1518
Yiwei Zhange1f35012020-07-05 22:52:04 -07001519void GetPhysicalDeviceExternalBufferProperties(
1520 VkPhysicalDevice physicalDevice,
1521 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1522 VkExternalBufferProperties* pExternalBufferProperties) {
1523 ATRACE_CALL();
1524
1525 const auto& driver = GetData(physicalDevice).driver;
1526
1527 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1528 driver.GetPhysicalDeviceExternalBufferProperties(
1529 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1530 return;
1531 }
1532
1533 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1534 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1535 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1536 return;
1537 }
1538
1539 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1540 sizeof(VkExternalMemoryProperties));
1541}
1542
1543void GetPhysicalDeviceExternalSemaphoreProperties(
1544 VkPhysicalDevice physicalDevice,
1545 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1546 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1547 ATRACE_CALL();
1548
1549 const auto& driver = GetData(physicalDevice).driver;
1550
1551 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1552 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1553 physicalDevice, pExternalSemaphoreInfo,
1554 pExternalSemaphoreProperties);
1555 return;
1556 }
1557
1558 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1559 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1560 physicalDevice, pExternalSemaphoreInfo,
1561 pExternalSemaphoreProperties);
1562 return;
1563 }
1564
1565 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1566 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1567 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1568}
1569
1570void GetPhysicalDeviceExternalFenceProperties(
1571 VkPhysicalDevice physicalDevice,
1572 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1573 VkExternalFenceProperties* pExternalFenceProperties) {
1574 ATRACE_CALL();
1575
1576 const auto& driver = GetData(physicalDevice).driver;
1577
1578 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1579 driver.GetPhysicalDeviceExternalFenceProperties(
1580 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1581 return;
1582 }
1583
1584 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1585 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1586 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1587 return;
1588 }
1589
1590 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1591 pExternalFenceProperties->compatibleHandleTypes = 0;
1592 pExternalFenceProperties->externalFenceFeatures = 0;
1593}
1594
Chia-I Wu9d518162016-03-24 14:55:27 +08001595} // namespace driver
1596} // namespace vulkan