blob: 8deca47c66edcf462d6c53509dce9828465d8389 [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>
Jesse Hall53457db2016-12-14 16:54:06 -080031#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090032#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070033#include <log/log.h>
Yiwei Zhange40dd732019-08-05 16:41:03 -070034#include <nativeloader/dlext_namespaces.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070035#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080036#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080037#include <utils/Trace.h>
Jesse Hall53457db2016-12-14 16:54:06 -080038
Yiwei Zhang5e862202019-06-21 14:59:16 -070039#include <algorithm>
40#include <array>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070041#include <climits>
Yiwei Zhang5e862202019-06-21 14:59:16 -070042#include <new>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070043#include <string_view>
44#include <sstream>
Yiwei Zhang5e862202019-06-21 14:59:16 -070045#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070046
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070047#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080048
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080049using namespace android::hardware::configstore;
50using namespace android::hardware::configstore::V1_0;
51
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080052// #define ENABLE_ALLOC_CALLSTACKS 1
53#if ENABLE_ALLOC_CALLSTACKS
54#include <utils/CallStack.h>
55#define ALOGD_CALLSTACK(...) \
56 do { \
57 ALOGD(__VA_ARGS__); \
58 android::CallStack callstack; \
59 callstack.update(); \
60 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
61 } while (false)
62#else
63#define ALOGD_CALLSTACK(...) \
64 do { \
65 } while (false)
66#endif
67
Chia-I Wu9d518162016-03-24 14:55:27 +080068namespace vulkan {
69namespace driver {
70
Chia-I Wu136b8eb2016-03-24 15:01:52 +080071namespace {
72
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080073class Hal {
74 public:
75 static bool Open();
76
77 static const Hal& Get() { return hal_; }
78 static const hwvulkan_device_t& Device() { return *Get().dev_; }
79
Chia-I Wu31938252016-05-23 15:31:02 +080080 int GetDebugReportIndex() const { return debug_report_index_; }
81
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080082 private:
Chia-I Wu31938252016-05-23 15:31:02 +080083 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080084 Hal(const Hal&) = delete;
85 Hal& operator=(const Hal&) = delete;
86
Yiwei Zhang901f8ee2020-07-31 13:18:49 -070087 bool ShouldUnloadBuiltinDriver();
88 void UnloadBuiltinDriver();
Chia-I Wu31938252016-05-23 15:31:02 +080089 bool InitDebugReportIndex();
90
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080091 static Hal hal_;
92
93 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080094 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080095};
96
Chia-I Wu4901db72016-03-24 16:38:58 +080097class CreateInfoWrapper {
98 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080099 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700100 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800101 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +0800102 CreateInfoWrapper(VkPhysicalDevice physical_dev,
103 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700104 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800105 const VkAllocationCallbacks& allocator);
106 ~CreateInfoWrapper();
107
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800108 VkResult Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +0800109
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800110 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
111 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800112
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800113 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800114 explicit operator const VkDeviceCreateInfo*() const;
115
116 private:
117 struct ExtensionFilter {
118 VkExtensionProperties* exts;
119 uint32_t ext_count;
120
121 const char** names;
122 uint32_t name_count;
123 };
124
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700125 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800126 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800127 VkResult SanitizeLayers();
128 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800129
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800130 VkResult QueryExtensionCount(uint32_t& count) const;
131 VkResult EnumerateExtensions(uint32_t& count,
132 VkExtensionProperties* props) const;
133 VkResult InitExtensionFilter();
134 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800135
136 const bool is_instance_;
137 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700138 const uint32_t loader_api_version_;
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700139 const uint32_t icd_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800140
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800141 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800142
143 union {
144 VkInstanceCreateInfo instance_info_;
145 VkDeviceCreateInfo dev_info_;
146 };
147
Ian Elliottf3e872d2017-11-02 10:15:13 -0600148 VkApplicationInfo application_info_;
149
Chia-I Wu4901db72016-03-24 16:38:58 +0800150 ExtensionFilter extension_filter_;
151
152 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
153 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
154};
155
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800156Hal Hal::hal_;
157
Jesse Hall53457db2016-12-14 16:54:06 -0800158void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700159 const std::string_view subname) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800160 ATRACE_CALL();
161
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700162 std::stringstream ss;
163 ss << "vulkan." << subname << ".so";
164 return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800165}
166
167const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700168 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800169 "ro.board.platform",
170}};
171
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800172// LoadDriver returns:
173// * 0 when succeed, or
174// * -ENOENT when fail to open binary libraries, or
175// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
176// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700177int LoadDriver(android_namespace_t* library_namespace,
178 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800179 ATRACE_CALL();
180
Jesse Hall53457db2016-12-14 16:54:06 -0800181 const android_dlextinfo dlextinfo = {
182 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700183 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800184 };
Jesse Hall53457db2016-12-14 16:54:06 -0800185 void* so = nullptr;
186 char prop[PROPERTY_VALUE_MAX];
187 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
188 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700189 if (prop_len > 0 && prop_len <= UINT_MAX) {
190 std::string_view lib_name(prop, static_cast<unsigned int>(prop_len));
191 so = LoadLibrary(dlextinfo, lib_name);
Jesse Hall53457db2016-12-14 16:54:06 -0800192 if (so)
193 break;
194 }
195 }
196 if (!so)
197 return -ENOENT;
198
Jesse Hall00e61ff2017-04-07 16:48:02 -0700199 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800200 if (!hmi) {
201 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
202 dlclose(so);
203 return -EINVAL;
204 }
205 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
206 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
207 dlclose(so);
208 return -EINVAL;
209 }
210 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700211 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800212 return 0;
213}
214
Jesse Hall00e61ff2017-04-07 16:48:02 -0700215int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800216 ATRACE_CALL();
217
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218 auto ns = android_get_exported_namespace("sphal");
219 if (!ns)
220 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800221 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700222 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700223 return LoadDriver(ns, module);
224}
225
226int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800227 ATRACE_CALL();
228
Jesse Hall00e61ff2017-04-07 16:48:02 -0700229 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
230 if (!ns)
231 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800232 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700233 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800234 int result = LoadDriver(ns, module);
235 if (result != 0) {
236 LOG_ALWAYS_FATAL(
237 "couldn't find an updated Vulkan implementation from %s",
238 android::GraphicsEnv::getInstance().getDriverPath().c_str());
239 }
240 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700241}
242
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800243bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800244 ATRACE_CALL();
245
Yiwei Zhangd9861812019-02-13 11:51:55 -0800246 const nsecs_t openTime = systemTime();
247
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700248 if (hal_.ShouldUnloadBuiltinDriver()) {
249 hal_.UnloadBuiltinDriver();
250 }
251
252 if (hal_.dev_)
253 return true;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800254
255 // Use a stub device unless we successfully open a real HAL device.
256 hal_.dev_ = &stubhal::kDevice;
257
Jesse Hall53457db2016-12-14 16:54:06 -0800258 int result;
259 const hwvulkan_module_t* module = nullptr;
260
Jesse Hall00e61ff2017-04-07 16:48:02 -0700261 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800262 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700263 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800264 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800265 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800266 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700267 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800268 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800269 return true;
270 }
271
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800272
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800273 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800274 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800275 result =
276 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
277 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800278 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800279 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800280 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700281 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800282 // Any device with a Vulkan HAL should be able to open the device.
283 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
284 result);
285 return false;
286 }
287
288 hal_.dev_ = device;
289
Chia-I Wu31938252016-05-23 15:31:02 +0800290 hal_.InitDebugReportIndex();
291
Yiwei Zhangd9861812019-02-13 11:51:55 -0800292 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700293 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800294
Chia-I Wu31938252016-05-23 15:31:02 +0800295 return true;
296}
297
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700298bool Hal::ShouldUnloadBuiltinDriver() {
299 // Should not unload since the driver was not loaded
300 if (!hal_.dev_)
301 return false;
302
303 // Should not unload if stubhal is used on the device
304 if (hal_.dev_ == &stubhal::kDevice)
305 return false;
306
307 // Unload the driver if updated driver is chosen
308 if (android::GraphicsEnv::getInstance().getDriverNamespace())
309 return true;
310
311 return false;
312}
313
314void Hal::UnloadBuiltinDriver() {
315 ATRACE_CALL();
316
317 ALOGD("Unload builtin Vulkan driver.");
318
319 // Close the opened device
320 ALOG_ASSERT(!hal_.dev_->common.close(hal_.dev_->common),
321 "hw_device_t::close() failed.");
322
323 // Close the opened shared library in the hw_module_t
324 dlclose(hal_.dev_->common.module->dso);
325
326 hal_.dev_ = nullptr;
327 hal_.debug_report_index_ = -1;
328}
329
Chia-I Wu31938252016-05-23 15:31:02 +0800330bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800331 ATRACE_CALL();
332
Chia-I Wu31938252016-05-23 15:31:02 +0800333 uint32_t count;
334 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
335 VK_SUCCESS) {
336 ALOGE("failed to get HAL instance extension count");
337 return false;
338 }
339
340 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
341 malloc(sizeof(VkExtensionProperties) * count));
342 if (!exts) {
343 ALOGE("failed to allocate HAL instance extension array");
344 return false;
345 }
346
347 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
348 VK_SUCCESS) {
349 ALOGE("failed to enumerate HAL instance extensions");
350 free(exts);
351 return false;
352 }
353
354 for (uint32_t i = 0; i < count; i++) {
355 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
356 0) {
357 debug_report_index_ = static_cast<int>(i);
358 break;
359 }
360 }
361
362 free(exts);
363
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800364 return true;
365}
366
367CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700368 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800369 const VkAllocationCallbacks& allocator)
370 : is_instance_(true),
371 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700372 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700373 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800374 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800375 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700376 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800377
Chia-I Wu4901db72016-03-24 16:38:58 +0800378CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
379 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700380 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800381 const VkAllocationCallbacks& allocator)
382 : is_instance_(false),
383 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700384 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700385 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800386 physical_dev_(physical_dev),
387 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700388 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800389
390CreateInfoWrapper::~CreateInfoWrapper() {
391 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
392 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
393}
394
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800395VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700396 VkResult result = SanitizeApiVersion();
397 if (result == VK_SUCCESS)
398 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800399 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800400 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800401 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800402 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800403
404 return result;
405}
406
407const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800408CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800409 return hook_extensions_;
410}
411
412const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800413CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800414 return hal_extensions_;
415}
416
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800417CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
418 return &instance_info_;
419}
420
Chia-I Wu4901db72016-03-24 16:38:58 +0800421CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
422 return &dev_info_;
423}
424
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700425VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700426 if (!is_instance_ || !instance_info_.pApplicationInfo)
427 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700428
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700429 if (icd_api_version_ > VK_API_VERSION_1_0 ||
430 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
431 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700432
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700433 // override apiVersion to avoid error return from 1.0 icd
434 application_info_ = *instance_info_.pApplicationInfo;
435 application_info_.apiVersion = VK_API_VERSION_1_0;
436 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700437
438 return VK_SUCCESS;
439}
440
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800441VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800442 const struct StructHeader {
443 VkStructureType type;
444 const void* next;
445 } * header;
446
447 if (is_instance_) {
448 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
449
450 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
451 while (header &&
452 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
453 header = reinterpret_cast<const StructHeader*>(header->next);
454
455 instance_info_.pNext = header;
456 } else {
457 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
458
459 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
460 while (header &&
461 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
462 header = reinterpret_cast<const StructHeader*>(header->next);
463
464 dev_info_.pNext = header;
465 }
466
467 return VK_SUCCESS;
468}
469
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800470VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800471 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
472 : dev_info_.ppEnabledLayerNames;
473 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
474 : dev_info_.enabledLayerCount;
475
476 // remove all layers
477 layer_names = nullptr;
478 layer_count = 0;
479
480 return VK_SUCCESS;
481}
482
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800483VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800484 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
485 : dev_info_.ppEnabledExtensionNames;
486 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
487 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800488
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800489 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800490 if (result != VK_SUCCESS)
491 return result;
492
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700493 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700494 for (uint32_t i = 0; i < ext_count; i++) {
495 // Upon api downgrade, skip the promoted instance extensions in the
496 // first pass to avoid duplicate extensions.
497 const std::optional<uint32_t> version =
498 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700499 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700500 *version <= loader_api_version_)
501 continue;
502
503 FilterExtension(ext_names[i]);
504 }
505
506 // Enable the required extensions to support core functionalities.
507 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700508 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700509 for (const auto& promoted_extension : promoted_extensions)
510 FilterExtension(promoted_extension);
511 } else {
512 for (uint32_t i = 0; i < ext_count; i++)
513 FilterExtension(ext_names[i]);
514 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800515
Jesse Halld3d887a2018-03-05 13:34:45 -0800516 // Enable device extensions that contain physical-device commands, so that
517 // vkGetInstanceProcAddr will return those physical-device commands.
518 if (is_instance_) {
519 hook_extensions_.set(ProcHook::KHR_swapchain);
520 }
521
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700522 const uint32_t api_version =
523 is_instance_ ? loader_api_version_
524 : std::min(icd_api_version_, loader_api_version_);
525 switch (api_version) {
526 case VK_API_VERSION_1_1:
527 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
528 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
529 [[clang::fallthrough]];
530 case VK_API_VERSION_1_0:
531 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
532 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
533 break;
534 default:
535 ALOGE("Unknown API version[%u]", api_version);
536 break;
537 }
538
Chia-I Wu4901db72016-03-24 16:38:58 +0800539 ext_names = extension_filter_.names;
540 ext_count = extension_filter_.name_count;
541
542 return VK_SUCCESS;
543}
544
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800545VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800546 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800547 return Hal::Device().EnumerateInstanceExtensionProperties(
548 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800549 } else {
550 const auto& driver = GetData(physical_dev_).driver;
551 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
552 &count, nullptr);
553 }
554}
555
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800556VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800557 uint32_t& count,
558 VkExtensionProperties* props) const {
559 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800560 return Hal::Device().EnumerateInstanceExtensionProperties(
561 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800562 } else {
563 const auto& driver = GetData(physical_dev_).driver;
564 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
565 &count, props);
566 }
567}
568
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800569VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800570 // query extension count
571 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800572 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800573 if (result != VK_SUCCESS || count == 0)
574 return result;
575
576 auto& filter = extension_filter_;
577 filter.exts =
578 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
579 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
580 alignof(VkExtensionProperties),
581 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
582 if (!filter.exts)
583 return VK_ERROR_OUT_OF_HOST_MEMORY;
584
585 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800586 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800587 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
588 return result;
589
590 if (!count)
591 return VK_SUCCESS;
592
593 filter.ext_count = count;
594
595 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700596 if (is_instance_) {
597 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
598
599 // It requires enabling additional promoted extensions to downgrade api,
600 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700601 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700602 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700603 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700604 }
605
606 count = std::min(filter.ext_count, enabled_ext_count);
607 } else {
608 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
609 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800610 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
611 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
612 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
613 if (!filter.names)
614 return VK_ERROR_OUT_OF_HOST_MEMORY;
615
616 return VK_SUCCESS;
617}
618
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800619void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800620 auto& filter = extension_filter_;
621
622 ProcHook::Extension ext_bit = GetProcHookExtension(name);
623 if (is_instance_) {
624 switch (ext_bit) {
625 case ProcHook::KHR_android_surface:
626 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700627 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300628 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800629 hook_extensions_.set(ext_bit);
630 // return now as these extensions do not require HAL support
631 return;
632 case ProcHook::EXT_debug_report:
633 // both we and HAL can take part in
634 hook_extensions_.set(ext_bit);
635 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300636 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700637 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700638 case ProcHook::KHR_external_memory_capabilities:
639 case ProcHook::KHR_external_semaphore_capabilities:
640 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700641 case ProcHook::EXTENSION_UNKNOWN:
642 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800643 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700644
Yiwei Zhang23143102019-04-10 18:24:05 -0700645 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700646 case ProcHook::KHR_incremental_present:
647 case ProcHook::KHR_shared_presentable_image:
648 case ProcHook::KHR_swapchain:
649 case ProcHook::EXT_hdr_metadata:
650 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
651 case ProcHook::ANDROID_native_buffer:
652 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700653 case ProcHook::EXTENSION_CORE_1_0:
654 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700655 case ProcHook::EXTENSION_COUNT:
656 // Device and meta extensions. If we ever get here it's a bug in
657 // our code. But enumerating them lets us avoid having a default
658 // case, and default hides other bugs.
659 ALOGE(
660 "CreateInfoWrapper::FilterExtension: invalid instance "
661 "extension '%s'. FIX ME",
662 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800663 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700664
665 // Don't use a default case. Without it, -Wswitch will tell us
666 // at compile time if someone adds a new ProcHook extension but
667 // doesn't handle it above. That's a real bug that has
668 // not-immediately-obvious effects.
669 //
670 // default:
671 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800672 }
673 } else {
674 switch (ext_bit) {
675 case ProcHook::KHR_swapchain:
676 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
677 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
678 ext_bit = ProcHook::ANDROID_native_buffer;
679 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700680 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700681 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300682 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700683 hook_extensions_.set(ext_bit);
684 // return now as these extensions do not require HAL support
685 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700686 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700687 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700688 hook_extensions_.set(ext_bit);
689 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700690 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800691 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700692 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800693 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700694
695 case ProcHook::KHR_android_surface:
696 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700697 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700698 case ProcHook::KHR_external_memory_capabilities:
699 case ProcHook::KHR_external_semaphore_capabilities:
700 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700701 case ProcHook::KHR_get_surface_capabilities2:
702 case ProcHook::KHR_surface:
703 case ProcHook::EXT_debug_report:
704 case ProcHook::EXT_swapchain_colorspace:
705 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700706 case ProcHook::EXTENSION_CORE_1_0:
707 case ProcHook::EXTENSION_CORE_1_1:
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:
Wei Wangf9b05ee2017-07-19 20:59:39 -07001026 const std::string timestamp_property("service.sf.present_timestamp");
1027 android::base::WaitForPropertyCreation(timestamp_property);
1028 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001029 loader_extensions.push_back({
1030 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1031 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1032 }
1033
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001034 // enumerate our extensions first
1035 if (!pLayerName && pProperties) {
1036 uint32_t count = std::min(
1037 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1038
Yiwei Zhang5e862202019-06-21 14:59:16 -07001039 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001040
1041 if (count < loader_extensions.size()) {
1042 *pPropertyCount = count;
1043 return VK_INCOMPLETE;
1044 }
1045
1046 pProperties += count;
1047 *pPropertyCount -= count;
1048 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001049
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001050 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001051 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1052 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001053 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001054
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001055 if (pProperties) {
1056 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1057 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1058 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001059
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001060 if (strcmp(prop.extensionName,
1061 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1062 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001063
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001064 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1065 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001066
1067 if (prop.specVersion >= 8) {
1068 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1069 } else {
1070 prop.specVersion = 68;
1071 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001072 }
1073 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001074
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001075 // restore loader extension count
1076 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1077 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001078 }
1079
1080 return result;
1081}
1082
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001083VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1084 const VkAllocationCallbacks* pAllocator,
1085 VkInstance* pInstance) {
1086 const VkAllocationCallbacks& data_allocator =
1087 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1088
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001089 VkResult result = VK_SUCCESS;
1090 uint32_t icd_api_version = VK_API_VERSION_1_0;
1091 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1092 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1093 Hal::Device().GetInstanceProcAddr(nullptr,
1094 "vkEnumerateInstanceVersion"));
1095 if (pfn_enumerate_instance_version) {
1096 ATRACE_BEGIN("pfn_enumerate_instance_version");
1097 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1098 ATRACE_END();
1099 if (result != VK_SUCCESS)
1100 return result;
1101
1102 icd_api_version ^= VK_VERSION_PATCH(icd_api_version);
1103 }
1104
1105 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1106 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001107 if (result != VK_SUCCESS)
1108 return result;
1109
1110 InstanceData* data = AllocateInstanceData(data_allocator);
1111 if (!data)
1112 return VK_ERROR_OUT_OF_HOST_MEMORY;
1113
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001114 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001115
1116 // call into the driver
1117 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001118 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001119 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001120 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1121 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001122 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001123 if (result != VK_SUCCESS) {
1124 FreeInstanceData(data, data_allocator);
1125 return result;
1126 }
1127
1128 // initialize InstanceDriverTable
1129 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001130 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001131 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001132 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001133 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001134 if (data->driver.DestroyInstance)
1135 data->driver.DestroyInstance(instance, pAllocator);
1136
1137 FreeInstanceData(data, data_allocator);
1138
1139 return VK_ERROR_INCOMPATIBLE_DRIVER;
1140 }
1141
1142 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001143 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001144 if (!data->get_device_proc_addr) {
1145 data->driver.DestroyInstance(instance, pAllocator);
1146 FreeInstanceData(data, data_allocator);
1147
1148 return VK_ERROR_INCOMPATIBLE_DRIVER;
1149 }
1150
1151 *pInstance = instance;
1152
1153 return VK_SUCCESS;
1154}
1155
1156void DestroyInstance(VkInstance instance,
1157 const VkAllocationCallbacks* pAllocator) {
1158 InstanceData& data = GetData(instance);
1159 data.driver.DestroyInstance(instance, pAllocator);
1160
1161 VkAllocationCallbacks local_allocator;
1162 if (!pAllocator) {
1163 local_allocator = data.allocator;
1164 pAllocator = &local_allocator;
1165 }
1166
1167 FreeInstanceData(&data, *pAllocator);
1168}
1169
Chia-I Wu4901db72016-03-24 16:38:58 +08001170VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1171 const VkDeviceCreateInfo* pCreateInfo,
1172 const VkAllocationCallbacks* pAllocator,
1173 VkDevice* pDevice) {
1174 const InstanceData& instance_data = GetData(physicalDevice);
1175 const VkAllocationCallbacks& data_allocator =
1176 (pAllocator) ? *pAllocator : instance_data.allocator;
1177
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001178 VkPhysicalDeviceProperties properties;
1179 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1180 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1181 &properties);
1182 ATRACE_END();
1183
1184 CreateInfoWrapper wrapper(
1185 physicalDevice, *pCreateInfo,
1186 properties.apiVersion ^ VK_VERSION_PATCH(properties.apiVersion),
1187 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001188 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001189 if (result != VK_SUCCESS)
1190 return result;
1191
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001192 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001193 DeviceData* data = AllocateDeviceData(data_allocator,
1194 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001195 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001196 if (!data)
1197 return VK_ERROR_OUT_OF_HOST_MEMORY;
1198
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001199 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001200
1201 // call into the driver
1202 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001203 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001204 result = instance_data.driver.CreateDevice(
1205 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1206 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001207 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001208 if (result != VK_SUCCESS) {
1209 FreeDeviceData(data, data_allocator);
1210 return result;
1211 }
1212
1213 // initialize DeviceDriverTable
1214 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001215 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1216 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001217 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1218 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1219 if (data->driver.DestroyDevice)
1220 data->driver.DestroyDevice(dev, pAllocator);
1221
1222 FreeDeviceData(data, data_allocator);
1223
1224 return VK_ERROR_INCOMPATIBLE_DRIVER;
1225 }
Chris Forbesd8277912017-02-10 14:59:59 +13001226
1227 // sanity check ANDROID_native_buffer implementation, whose set of
1228 // entrypoints varies according to the spec version.
1229 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1230 !data->driver.GetSwapchainGrallocUsageANDROID &&
1231 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1232 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1233 " must expose at least one of "
1234 "vkGetSwapchainGrallocUsageANDROID or "
1235 "vkGetSwapchainGrallocUsage2ANDROID");
1236
1237 data->driver.DestroyDevice(dev, pAllocator);
1238 FreeDeviceData(data, data_allocator);
1239
1240 return VK_ERROR_INCOMPATIBLE_DRIVER;
1241 }
1242
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001243 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1244 // Log that the app is hitting software Vulkan implementation
1245 android::GraphicsEnv::getInstance().setTargetStats(
1246 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1247 }
1248
Jesse Halldc225072016-05-30 22:40:14 -07001249 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001250
1251 *pDevice = dev;
1252
1253 return VK_SUCCESS;
1254}
1255
1256void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1257 DeviceData& data = GetData(device);
1258 data.driver.DestroyDevice(device, pAllocator);
1259
1260 VkAllocationCallbacks local_allocator;
1261 if (!pAllocator) {
1262 local_allocator = data.allocator;
1263 pAllocator = &local_allocator;
1264 }
1265
1266 FreeDeviceData(&data, *pAllocator);
1267}
1268
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001269VkResult EnumeratePhysicalDevices(VkInstance instance,
1270 uint32_t* pPhysicalDeviceCount,
1271 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001272 ATRACE_CALL();
1273
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001274 const auto& data = GetData(instance);
1275
1276 VkResult result = data.driver.EnumeratePhysicalDevices(
1277 instance, pPhysicalDeviceCount, pPhysicalDevices);
1278 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1279 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1280 SetData(pPhysicalDevices[i], data);
1281 }
1282
1283 return result;
1284}
1285
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001286VkResult EnumeratePhysicalDeviceGroups(
1287 VkInstance instance,
1288 uint32_t* pPhysicalDeviceGroupCount,
1289 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001290 ATRACE_CALL();
1291
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001292 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001293 const auto& data = GetData(instance);
1294
Yiwei Zhange4f64172020-07-05 15:17:32 -07001295 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1296 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001297 uint32_t device_count = 0;
1298 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1299 if (result < 0)
1300 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001301
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001302 if (!pPhysicalDeviceGroupProperties) {
1303 *pPhysicalDeviceGroupCount = device_count;
1304 return result;
1305 }
1306
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001307 if (!device_count) {
1308 *pPhysicalDeviceGroupCount = 0;
1309 return result;
1310 }
Chad Versace32c087f2018-09-09 07:28:05 -07001311 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1312 if (!device_count)
1313 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001314
Yiwei Zhang5e862202019-06-21 14:59:16 -07001315 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001316 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001317 result =
1318 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001319 if (result < 0)
1320 return result;
1321
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001322 for (uint32_t i = 0; i < device_count; ++i) {
1323 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1324 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1325 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1326 }
1327 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001328 if (data.driver.EnumeratePhysicalDeviceGroups) {
1329 result = data.driver.EnumeratePhysicalDeviceGroups(
1330 instance, pPhysicalDeviceGroupCount,
1331 pPhysicalDeviceGroupProperties);
1332 } else {
1333 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1334 instance, pPhysicalDeviceGroupCount,
1335 pPhysicalDeviceGroupProperties);
1336 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001337 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1338 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1339 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1340 for (uint32_t j = 0;
1341 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1342 j++) {
1343 SetData(
1344 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001345 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001346 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001347 }
1348 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001349 }
1350
1351 return result;
1352}
1353
Chia-I Wuba0be412016-03-24 16:24:40 +08001354void GetDeviceQueue(VkDevice device,
1355 uint32_t queueFamilyIndex,
1356 uint32_t queueIndex,
1357 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001358 ATRACE_CALL();
1359
Chia-I Wuba0be412016-03-24 16:24:40 +08001360 const auto& data = GetData(device);
1361
1362 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1363 SetData(*pQueue, data);
1364}
1365
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001366void GetDeviceQueue2(VkDevice device,
1367 const VkDeviceQueueInfo2* pQueueInfo,
1368 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001369 ATRACE_CALL();
1370
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001371 const auto& data = GetData(device);
1372
1373 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001374 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001375}
1376
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001377VkResult AllocateCommandBuffers(
1378 VkDevice device,
1379 const VkCommandBufferAllocateInfo* pAllocateInfo,
1380 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001381 ATRACE_CALL();
1382
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001383 const auto& data = GetData(device);
1384
1385 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1386 pCommandBuffers);
1387 if (result == VK_SUCCESS) {
1388 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1389 SetData(pCommandBuffers[i], data);
1390 }
1391
1392 return result;
1393}
1394
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001395VkResult QueueSubmit(VkQueue queue,
1396 uint32_t submitCount,
1397 const VkSubmitInfo* pSubmits,
1398 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001399 ATRACE_CALL();
1400
1401 const auto& data = GetData(queue);
1402
1403 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1404}
1405
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001406void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1407 VkPhysicalDeviceFeatures2* pFeatures) {
1408 ATRACE_CALL();
1409
1410 const auto& driver = GetData(physicalDevice).driver;
1411
1412 if (driver.GetPhysicalDeviceFeatures2) {
1413 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
1414 return;
1415 }
1416
1417 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1418}
1419
1420void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1421 VkPhysicalDeviceProperties2* pProperties) {
1422 ATRACE_CALL();
1423
1424 const auto& driver = GetData(physicalDevice).driver;
1425
1426 if (driver.GetPhysicalDeviceProperties2) {
1427 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1428 return;
1429 }
1430
1431 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1432}
1433
1434void GetPhysicalDeviceFormatProperties2(
1435 VkPhysicalDevice physicalDevice,
1436 VkFormat format,
1437 VkFormatProperties2* pFormatProperties) {
1438 ATRACE_CALL();
1439
1440 const auto& driver = GetData(physicalDevice).driver;
1441
1442 if (driver.GetPhysicalDeviceFormatProperties2) {
1443 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1444 pFormatProperties);
1445 return;
1446 }
1447
1448 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1449 pFormatProperties);
1450}
1451
1452VkResult GetPhysicalDeviceImageFormatProperties2(
1453 VkPhysicalDevice physicalDevice,
1454 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1455 VkImageFormatProperties2* pImageFormatProperties) {
1456 ATRACE_CALL();
1457
1458 const auto& driver = GetData(physicalDevice).driver;
1459
1460 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1461 return driver.GetPhysicalDeviceImageFormatProperties2(
1462 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1463 }
1464
1465 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1466 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1467}
1468
1469void GetPhysicalDeviceQueueFamilyProperties2(
1470 VkPhysicalDevice physicalDevice,
1471 uint32_t* pQueueFamilyPropertyCount,
1472 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1473 ATRACE_CALL();
1474
1475 const auto& driver = GetData(physicalDevice).driver;
1476
1477 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1478 driver.GetPhysicalDeviceQueueFamilyProperties2(
1479 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1480 return;
1481 }
1482
1483 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1484 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1485}
1486
1487void GetPhysicalDeviceMemoryProperties2(
1488 VkPhysicalDevice physicalDevice,
1489 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1490 ATRACE_CALL();
1491
1492 const auto& driver = GetData(physicalDevice).driver;
1493
1494 if (driver.GetPhysicalDeviceMemoryProperties2) {
1495 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1496 pMemoryProperties);
1497 return;
1498 }
1499
1500 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1501 pMemoryProperties);
1502}
1503
1504void GetPhysicalDeviceSparseImageFormatProperties2(
1505 VkPhysicalDevice physicalDevice,
1506 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1507 uint32_t* pPropertyCount,
1508 VkSparseImageFormatProperties2* pProperties) {
1509 ATRACE_CALL();
1510
1511 const auto& driver = GetData(physicalDevice).driver;
1512
1513 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1514 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1515 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1516 return;
1517 }
1518
1519 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1520 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1521}
1522
Yiwei Zhange1f35012020-07-05 22:52:04 -07001523void GetPhysicalDeviceExternalBufferProperties(
1524 VkPhysicalDevice physicalDevice,
1525 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1526 VkExternalBufferProperties* pExternalBufferProperties) {
1527 ATRACE_CALL();
1528
1529 const auto& driver = GetData(physicalDevice).driver;
1530
1531 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1532 driver.GetPhysicalDeviceExternalBufferProperties(
1533 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1534 return;
1535 }
1536
1537 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1538 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1539 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1540 return;
1541 }
1542
1543 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1544 sizeof(VkExternalMemoryProperties));
1545}
1546
1547void GetPhysicalDeviceExternalSemaphoreProperties(
1548 VkPhysicalDevice physicalDevice,
1549 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1550 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1551 ATRACE_CALL();
1552
1553 const auto& driver = GetData(physicalDevice).driver;
1554
1555 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1556 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1557 physicalDevice, pExternalSemaphoreInfo,
1558 pExternalSemaphoreProperties);
1559 return;
1560 }
1561
1562 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1563 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1564 physicalDevice, pExternalSemaphoreInfo,
1565 pExternalSemaphoreProperties);
1566 return;
1567 }
1568
1569 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1570 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1571 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1572}
1573
1574void GetPhysicalDeviceExternalFenceProperties(
1575 VkPhysicalDevice physicalDevice,
1576 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1577 VkExternalFenceProperties* pExternalFenceProperties) {
1578 ATRACE_CALL();
1579
1580 const auto& driver = GetData(physicalDevice).driver;
1581
1582 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1583 driver.GetPhysicalDeviceExternalFenceProperties(
1584 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1585 return;
1586 }
1587
1588 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1589 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1590 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1591 return;
1592 }
1593
1594 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1595 pExternalFenceProperties->compatibleHandleTypes = 0;
1596 pExternalFenceProperties->externalFenceFeatures = 0;
1597}
1598
Chia-I Wu9d518162016-03-24 14:55:27 +08001599} // namespace driver
1600} // namespace vulkan