blob: 997b332dd4926a18308b5d147c7589117667fa74 [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;
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700123 ExtensionFilter()
124 : exts(nullptr), ext_count(0), names(nullptr), name_count(0) {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800125 };
126
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700127 VkResult SanitizeApiVersion();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800128 VkResult SanitizePNext();
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800129 VkResult SanitizeLayers();
130 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800131
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800132 VkResult QueryExtensionCount(uint32_t& count) const;
133 VkResult EnumerateExtensions(uint32_t& count,
134 VkExtensionProperties* props) const;
135 VkResult InitExtensionFilter();
136 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800137
138 const bool is_instance_;
139 const VkAllocationCallbacks& allocator_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700140 const uint32_t loader_api_version_;
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700141 const uint32_t icd_api_version_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800142
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800143 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800144
145 union {
146 VkInstanceCreateInfo instance_info_;
147 VkDeviceCreateInfo dev_info_;
148 };
149
Ian Elliottf3e872d2017-11-02 10:15:13 -0600150 VkApplicationInfo application_info_;
151
Chia-I Wu4901db72016-03-24 16:38:58 +0800152 ExtensionFilter extension_filter_;
153
154 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
155 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
156};
157
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800158Hal Hal::hal_;
159
Jesse Hall53457db2016-12-14 16:54:06 -0800160void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700161 const std::string_view subname) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800162 ATRACE_CALL();
163
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700164 std::stringstream ss;
165 ss << "vulkan." << subname << ".so";
166 return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800167}
168
169const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700170 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800171 "ro.board.platform",
172}};
173
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800174// LoadDriver returns:
175// * 0 when succeed, or
176// * -ENOENT when fail to open binary libraries, or
177// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
178// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700179int LoadDriver(android_namespace_t* library_namespace,
180 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800181 ATRACE_CALL();
182
Jesse Hall53457db2016-12-14 16:54:06 -0800183 const android_dlextinfo dlextinfo = {
184 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700185 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800186 };
Jesse Hall53457db2016-12-14 16:54:06 -0800187 void* so = nullptr;
188 char prop[PROPERTY_VALUE_MAX];
189 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
190 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700191 if (prop_len > 0 && prop_len <= UINT_MAX) {
192 std::string_view lib_name(prop, static_cast<unsigned int>(prop_len));
193 so = LoadLibrary(dlextinfo, lib_name);
Jesse Hall53457db2016-12-14 16:54:06 -0800194 if (so)
195 break;
196 }
197 }
198 if (!so)
199 return -ENOENT;
200
Jesse Hall00e61ff2017-04-07 16:48:02 -0700201 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800202 if (!hmi) {
203 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
204 dlclose(so);
205 return -EINVAL;
206 }
207 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
208 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
209 dlclose(so);
210 return -EINVAL;
211 }
212 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700213 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800214 return 0;
215}
216
Jesse Hall00e61ff2017-04-07 16:48:02 -0700217int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800218 ATRACE_CALL();
219
Jesse Hall00e61ff2017-04-07 16:48:02 -0700220 auto ns = android_get_exported_namespace("sphal");
221 if (!ns)
222 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800223 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700224 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700225 return LoadDriver(ns, module);
226}
227
228int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800229 ATRACE_CALL();
230
Jesse Hall00e61ff2017-04-07 16:48:02 -0700231 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
232 if (!ns)
233 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800234 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700235 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800236 int result = LoadDriver(ns, module);
237 if (result != 0) {
238 LOG_ALWAYS_FATAL(
239 "couldn't find an updated Vulkan implementation from %s",
240 android::GraphicsEnv::getInstance().getDriverPath().c_str());
241 }
242 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700243}
244
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800245bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800246 ATRACE_CALL();
247
Yiwei Zhangd9861812019-02-13 11:51:55 -0800248 const nsecs_t openTime = systemTime();
249
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700250 if (hal_.ShouldUnloadBuiltinDriver()) {
251 hal_.UnloadBuiltinDriver();
252 }
253
254 if (hal_.dev_)
255 return true;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800256
257 // Use a stub device unless we successfully open a real HAL device.
258 hal_.dev_ = &stubhal::kDevice;
259
Jesse Hall53457db2016-12-14 16:54:06 -0800260 int result;
261 const hwvulkan_module_t* module = nullptr;
262
Jesse Hall00e61ff2017-04-07 16:48:02 -0700263 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800264 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700265 result = LoadBuiltinDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800266 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800267 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800268 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700269 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800270 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800271 return true;
272 }
273
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800274
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800275 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800276 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800277 result =
278 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
279 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800280 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800281 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800282 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700283 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800284 // Any device with a Vulkan HAL should be able to open the device.
285 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
286 result);
287 return false;
288 }
289
290 hal_.dev_ = device;
291
Chia-I Wu31938252016-05-23 15:31:02 +0800292 hal_.InitDebugReportIndex();
293
Yiwei Zhangd9861812019-02-13 11:51:55 -0800294 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700295 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800296
Chia-I Wu31938252016-05-23 15:31:02 +0800297 return true;
298}
299
Yiwei Zhang901f8ee2020-07-31 13:18:49 -0700300bool Hal::ShouldUnloadBuiltinDriver() {
301 // Should not unload since the driver was not loaded
302 if (!hal_.dev_)
303 return false;
304
305 // Should not unload if stubhal is used on the device
306 if (hal_.dev_ == &stubhal::kDevice)
307 return false;
308
309 // Unload the driver if updated driver is chosen
310 if (android::GraphicsEnv::getInstance().getDriverNamespace())
311 return true;
312
313 return false;
314}
315
316void Hal::UnloadBuiltinDriver() {
317 ATRACE_CALL();
318
319 ALOGD("Unload builtin Vulkan driver.");
320
321 // Close the opened device
322 ALOG_ASSERT(!hal_.dev_->common.close(hal_.dev_->common),
323 "hw_device_t::close() failed.");
324
325 // Close the opened shared library in the hw_module_t
326 dlclose(hal_.dev_->common.module->dso);
327
328 hal_.dev_ = nullptr;
329 hal_.debug_report_index_ = -1;
330}
331
Chia-I Wu31938252016-05-23 15:31:02 +0800332bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800333 ATRACE_CALL();
334
Chia-I Wu31938252016-05-23 15:31:02 +0800335 uint32_t count;
336 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
337 VK_SUCCESS) {
338 ALOGE("failed to get HAL instance extension count");
339 return false;
340 }
341
342 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
343 malloc(sizeof(VkExtensionProperties) * count));
344 if (!exts) {
345 ALOGE("failed to allocate HAL instance extension array");
346 return false;
347 }
348
349 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
350 VK_SUCCESS) {
351 ALOGE("failed to enumerate HAL instance extensions");
352 free(exts);
353 return false;
354 }
355
356 for (uint32_t i = 0; i < count; i++) {
357 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
358 0) {
359 debug_report_index_ = static_cast<int>(i);
360 break;
361 }
362 }
363
364 free(exts);
365
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800366 return true;
367}
368
369CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700370 uint32_t icd_api_version,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800371 const VkAllocationCallbacks& allocator)
372 : is_instance_(true),
373 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700374 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700375 icd_api_version_(icd_api_version),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800376 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800377 instance_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700378 extension_filter_() {}
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800379
Chia-I Wu4901db72016-03-24 16:38:58 +0800380CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
381 const VkDeviceCreateInfo& create_info,
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700382 uint32_t icd_api_version,
Chia-I Wu4901db72016-03-24 16:38:58 +0800383 const VkAllocationCallbacks& allocator)
384 : is_instance_(false),
385 allocator_(allocator),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700386 loader_api_version_(VK_API_VERSION_1_1),
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700387 icd_api_version_(icd_api_version),
Chia-I Wu4901db72016-03-24 16:38:58 +0800388 physical_dev_(physical_dev),
389 dev_info_(create_info),
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700390 extension_filter_() {}
Chia-I Wu4901db72016-03-24 16:38:58 +0800391
392CreateInfoWrapper::~CreateInfoWrapper() {
393 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
394 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
395}
396
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800397VkResult CreateInfoWrapper::Validate() {
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700398 VkResult result = SanitizeApiVersion();
399 if (result == VK_SUCCESS)
400 result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800401 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800402 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800403 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800404 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800405
406 return result;
407}
408
409const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800410CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800411 return hook_extensions_;
412}
413
414const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800415CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800416 return hal_extensions_;
417}
418
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800419CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
420 return &instance_info_;
421}
422
Chia-I Wu4901db72016-03-24 16:38:58 +0800423CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
424 return &dev_info_;
425}
426
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700427VkResult CreateInfoWrapper::SanitizeApiVersion() {
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700428 if (!is_instance_ || !instance_info_.pApplicationInfo)
429 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700430
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700431 if (icd_api_version_ > VK_API_VERSION_1_0 ||
432 instance_info_.pApplicationInfo->apiVersion < VK_API_VERSION_1_1)
433 return VK_SUCCESS;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700434
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700435 // override apiVersion to avoid error return from 1.0 icd
436 application_info_ = *instance_info_.pApplicationInfo;
437 application_info_.apiVersion = VK_API_VERSION_1_0;
438 instance_info_.pApplicationInfo = &application_info_;
Yiwei Zhangd4fd1222020-07-03 22:18:42 -0700439
440 return VK_SUCCESS;
441}
442
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800443VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800444 const struct StructHeader {
445 VkStructureType type;
446 const void* next;
447 } * header;
448
449 if (is_instance_) {
450 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
451
452 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
453 while (header &&
454 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
455 header = reinterpret_cast<const StructHeader*>(header->next);
456
457 instance_info_.pNext = header;
458 } else {
459 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
460
461 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
462 while (header &&
463 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
464 header = reinterpret_cast<const StructHeader*>(header->next);
465
466 dev_info_.pNext = header;
467 }
468
469 return VK_SUCCESS;
470}
471
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800472VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800473 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
474 : dev_info_.ppEnabledLayerNames;
475 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
476 : dev_info_.enabledLayerCount;
477
478 // remove all layers
479 layer_names = nullptr;
480 layer_count = 0;
481
482 return VK_SUCCESS;
483}
484
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800485VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800486 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
487 : dev_info_.ppEnabledExtensionNames;
488 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
489 : dev_info_.enabledExtensionCount;
Chia-I Wu4901db72016-03-24 16:38:58 +0800490
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800491 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800492 if (result != VK_SUCCESS)
493 return result;
494
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700495 if (is_instance_ && icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700496 for (uint32_t i = 0; i < ext_count; i++) {
497 // Upon api downgrade, skip the promoted instance extensions in the
498 // first pass to avoid duplicate extensions.
499 const std::optional<uint32_t> version =
500 GetInstanceExtensionPromotedVersion(ext_names[i]);
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700501 if (version && *version > icd_api_version_ &&
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700502 *version <= loader_api_version_)
503 continue;
504
505 FilterExtension(ext_names[i]);
506 }
507
508 // Enable the required extensions to support core functionalities.
509 const auto promoted_extensions = GetPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700510 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700511 for (const auto& promoted_extension : promoted_extensions)
512 FilterExtension(promoted_extension);
513 } else {
514 for (uint32_t i = 0; i < ext_count; i++)
515 FilterExtension(ext_names[i]);
516 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800517
Jesse Halld3d887a2018-03-05 13:34:45 -0800518 // Enable device extensions that contain physical-device commands, so that
519 // vkGetInstanceProcAddr will return those physical-device commands.
520 if (is_instance_) {
521 hook_extensions_.set(ProcHook::KHR_swapchain);
522 }
523
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700524 const uint32_t api_version =
525 is_instance_ ? loader_api_version_
526 : std::min(icd_api_version_, loader_api_version_);
527 switch (api_version) {
528 case VK_API_VERSION_1_1:
529 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
530 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
531 [[clang::fallthrough]];
532 case VK_API_VERSION_1_0:
533 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
534 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
535 break;
536 default:
537 ALOGE("Unknown API version[%u]", api_version);
538 break;
539 }
540
Chia-I Wu4901db72016-03-24 16:38:58 +0800541 ext_names = extension_filter_.names;
542 ext_count = extension_filter_.name_count;
543
544 return VK_SUCCESS;
545}
546
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800547VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800548 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800549 return Hal::Device().EnumerateInstanceExtensionProperties(
550 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800551 } else {
552 const auto& driver = GetData(physical_dev_).driver;
553 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
554 &count, nullptr);
555 }
556}
557
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800558VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800559 uint32_t& count,
560 VkExtensionProperties* props) const {
561 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800562 return Hal::Device().EnumerateInstanceExtensionProperties(
563 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800564 } else {
565 const auto& driver = GetData(physical_dev_).driver;
566 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
567 &count, props);
568 }
569}
570
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800571VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800572 // query extension count
573 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800574 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800575 if (result != VK_SUCCESS || count == 0)
576 return result;
577
578 auto& filter = extension_filter_;
579 filter.exts =
580 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
581 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
582 alignof(VkExtensionProperties),
583 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
584 if (!filter.exts)
585 return VK_ERROR_OUT_OF_HOST_MEMORY;
586
587 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800588 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800589 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
590 return result;
591
592 if (!count)
593 return VK_SUCCESS;
594
595 filter.ext_count = count;
596
597 // allocate name array
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700598 if (is_instance_) {
599 uint32_t enabled_ext_count = instance_info_.enabledExtensionCount;
600
601 // It requires enabling additional promoted extensions to downgrade api,
602 // so we reserve enough space here.
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700603 if (icd_api_version_ < loader_api_version_) {
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700604 enabled_ext_count += CountPromotedInstanceExtensions(
Yiwei Zhang2cefa732020-07-10 21:07:30 -0700605 icd_api_version_, loader_api_version_);
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700606 }
607
608 count = std::min(filter.ext_count, enabled_ext_count);
609 } else {
610 count = std::min(filter.ext_count, dev_info_.enabledExtensionCount);
611 }
Yiwei Zhangd7ea44a2020-08-13 12:54:27 -0700612
613 if (!count)
614 return VK_SUCCESS;
615
Chia-I Wu4901db72016-03-24 16:38:58 +0800616 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
617 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
618 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
619 if (!filter.names)
620 return VK_ERROR_OUT_OF_HOST_MEMORY;
621
622 return VK_SUCCESS;
623}
624
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800625void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800626 auto& filter = extension_filter_;
627
628 ProcHook::Extension ext_bit = GetProcHookExtension(name);
629 if (is_instance_) {
630 switch (ext_bit) {
631 case ProcHook::KHR_android_surface:
632 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700633 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300634 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800635 hook_extensions_.set(ext_bit);
636 // return now as these extensions do not require HAL support
637 return;
638 case ProcHook::EXT_debug_report:
639 // both we and HAL can take part in
640 hook_extensions_.set(ext_bit);
641 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300642 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700643 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700644 case ProcHook::KHR_external_memory_capabilities:
645 case ProcHook::KHR_external_semaphore_capabilities:
646 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700647 case ProcHook::EXTENSION_UNKNOWN:
648 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800649 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700650
Yiwei Zhang23143102019-04-10 18:24:05 -0700651 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700652 case ProcHook::KHR_incremental_present:
653 case ProcHook::KHR_shared_presentable_image:
654 case ProcHook::KHR_swapchain:
655 case ProcHook::EXT_hdr_metadata:
656 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
657 case ProcHook::ANDROID_native_buffer:
658 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700659 case ProcHook::EXTENSION_CORE_1_0:
660 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700661 case ProcHook::EXTENSION_COUNT:
662 // Device and meta extensions. If we ever get here it's a bug in
663 // our code. But enumerating them lets us avoid having a default
664 // case, and default hides other bugs.
665 ALOGE(
666 "CreateInfoWrapper::FilterExtension: invalid instance "
667 "extension '%s'. FIX ME",
668 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800669 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700670
671 // Don't use a default case. Without it, -Wswitch will tell us
672 // at compile time if someone adds a new ProcHook extension but
673 // doesn't handle it above. That's a real bug that has
674 // not-immediately-obvious effects.
675 //
676 // default:
677 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800678 }
679 } else {
680 switch (ext_bit) {
681 case ProcHook::KHR_swapchain:
682 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
683 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
684 ext_bit = ProcHook::ANDROID_native_buffer;
685 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700686 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700687 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300688 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700689 hook_extensions_.set(ext_bit);
690 // return now as these extensions do not require HAL support
691 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700692 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700693 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700694 hook_extensions_.set(ext_bit);
695 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700696 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800697 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700698 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800699 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700700
701 case ProcHook::KHR_android_surface:
702 case ProcHook::KHR_get_physical_device_properties2:
Yiwei Zhange4f64172020-07-05 15:17:32 -0700703 case ProcHook::KHR_device_group_creation:
Yiwei Zhange1f35012020-07-05 22:52:04 -0700704 case ProcHook::KHR_external_memory_capabilities:
705 case ProcHook::KHR_external_semaphore_capabilities:
706 case ProcHook::KHR_external_fence_capabilities:
Jesse Hall7f983a82018-03-29 14:46:45 -0700707 case ProcHook::KHR_get_surface_capabilities2:
708 case ProcHook::KHR_surface:
709 case ProcHook::EXT_debug_report:
710 case ProcHook::EXT_swapchain_colorspace:
711 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700712 case ProcHook::EXTENSION_CORE_1_0:
713 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700714 case ProcHook::EXTENSION_COUNT:
715 // Instance and meta extensions. If we ever get here it's a bug
716 // in our code. But enumerating them lets us avoid having a
717 // default case, and default hides other bugs.
718 ALOGE(
719 "CreateInfoWrapper::FilterExtension: invalid device "
720 "extension '%s'. FIX ME",
721 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800722 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700723
724 // Don't use a default case. Without it, -Wswitch will tell us
725 // at compile time if someone adds a new ProcHook extension but
726 // doesn't handle it above. That's a real bug that has
727 // not-immediately-obvious effects.
728 //
729 // default:
730 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800731 }
732 }
733
734 for (uint32_t i = 0; i < filter.ext_count; i++) {
735 const VkExtensionProperties& props = filter.exts[i];
736 // ignore unknown extensions
737 if (strcmp(name, props.extensionName) != 0)
738 continue;
739
Chia-I Wu4901db72016-03-24 16:38:58 +0800740 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800741 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
742 if (ext_bit == ProcHook::ANDROID_native_buffer)
743 hook_extensions_.set(ProcHook::KHR_swapchain);
744
745 hal_extensions_.set(ext_bit);
746 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800747
748 break;
749 }
750}
751
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800752VKAPI_ATTR void* DefaultAllocate(void*,
753 size_t size,
754 size_t alignment,
755 VkSystemAllocationScope) {
756 void* ptr = nullptr;
757 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
758 // additionally requires that it be at least sizeof(void*).
759 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
760 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
761 ret, ptr);
762 return ret == 0 ? ptr : nullptr;
763}
764
765VKAPI_ATTR void* DefaultReallocate(void*,
766 void* ptr,
767 size_t size,
768 size_t alignment,
769 VkSystemAllocationScope) {
770 if (size == 0) {
771 free(ptr);
772 return nullptr;
773 }
774
Yiwei Zhanga885c062019-10-24 12:07:57 -0700775 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800776 // request is smaller than the existing chunk, we just continue using it.
777 // Right now the loader never reallocs, so this doesn't matter. If that
778 // changes, or if this code is copied into some other project, this should
779 // probably have a heuristic to allocate-copy-free when doing so will save
780 // "enough" space.
781 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
782 if (size <= old_size)
783 return ptr;
784
785 void* new_ptr = nullptr;
786 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
787 return nullptr;
788 if (ptr) {
789 memcpy(new_ptr, ptr, std::min(old_size, size));
790 free(ptr);
791 }
792 return new_ptr;
793}
794
795VKAPI_ATTR void DefaultFree(void*, void* ptr) {
796 ALOGD_CALLSTACK("Free: %p", ptr);
797 free(ptr);
798}
799
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800800InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
801 void* data_mem = allocator.pfnAllocation(
802 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
803 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
804 if (!data_mem)
805 return nullptr;
806
807 return new (data_mem) InstanceData(allocator);
808}
809
810void FreeInstanceData(InstanceData* data,
811 const VkAllocationCallbacks& allocator) {
812 data->~InstanceData();
813 allocator.pfnFree(allocator.pUserData, data);
814}
815
Chia-I Wu950d6e12016-05-03 09:12:35 +0800816DeviceData* AllocateDeviceData(
817 const VkAllocationCallbacks& allocator,
818 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800819 void* data_mem = allocator.pfnAllocation(
820 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
821 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
822 if (!data_mem)
823 return nullptr;
824
Chia-I Wu950d6e12016-05-03 09:12:35 +0800825 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800826}
827
828void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
829 data->~DeviceData();
830 allocator.pfnFree(allocator.pUserData, data);
831}
832
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800833} // anonymous namespace
834
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800835bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800836 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800837}
838
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800839const VkAllocationCallbacks& GetDefaultAllocator() {
840 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
841 .pUserData = nullptr,
842 .pfnAllocation = DefaultAllocate,
843 .pfnReallocation = DefaultReallocate,
844 .pfnFree = DefaultFree,
845 };
846
847 return kDefaultAllocCallbacks;
848}
849
Chia-I Wueb7db122016-03-24 09:11:06 +0800850PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
851 const ProcHook* hook = GetProcHook(pName);
852 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800853 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800854
855 if (!instance) {
856 if (hook->type == ProcHook::GLOBAL)
857 return hook->proc;
858
Chia-I Wu109f8982016-04-22 06:40:40 +0800859 // v0 layers expect
860 //
861 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
862 //
863 // to work.
864 if (strcmp(pName, "vkCreateDevice") == 0)
865 return hook->proc;
866
Chia-I Wueb7db122016-03-24 09:11:06 +0800867 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800868 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800869 pName);
870
Chia-I Wu109f8982016-04-22 06:40:40 +0800871 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800872 }
873
874 PFN_vkVoidFunction proc;
875
876 switch (hook->type) {
877 case ProcHook::INSTANCE:
878 proc = (GetData(instance).hook_extensions[hook->extension])
879 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800880 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800881 break;
882 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700883 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800884 ? hook->proc
885 : hook->checked_proc;
886 break;
887 default:
888 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800889 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800890 pName);
891 proc = nullptr;
892 break;
893 }
894
895 return proc;
896}
897
898PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
899 const ProcHook* hook = GetProcHook(pName);
900 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800901 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800902
903 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800904 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800905 return nullptr;
906 }
907
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800908 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
909 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800910}
911
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800912VkResult EnumerateInstanceExtensionProperties(
913 const char* pLayerName,
914 uint32_t* pPropertyCount,
915 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700916 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600917 loader_extensions.push_back({
918 VK_KHR_SURFACE_EXTENSION_NAME,
919 VK_KHR_SURFACE_SPEC_VERSION});
920 loader_extensions.push_back({
921 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
922 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
923 loader_extensions.push_back({
924 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
925 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700926 loader_extensions.push_back({
927 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
928 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600929
Chia-I Wu31938252016-05-23 15:31:02 +0800930 static const VkExtensionProperties loader_debug_report_extension = {
931 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
932 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800933
934 // enumerate our extensions first
935 if (!pLayerName && pProperties) {
936 uint32_t count = std::min(
937 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
938
Yiwei Zhang5e862202019-06-21 14:59:16 -0700939 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800940
941 if (count < loader_extensions.size()) {
942 *pPropertyCount = count;
943 return VK_INCOMPLETE;
944 }
945
946 pProperties += count;
947 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800948
949 if (Hal::Get().GetDebugReportIndex() < 0) {
950 if (!*pPropertyCount) {
951 *pPropertyCount = count;
952 return VK_INCOMPLETE;
953 }
954
955 pProperties[0] = loader_debug_report_extension;
956 pProperties += 1;
957 *pPropertyCount -= 1;
958 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800959 }
960
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800961 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800962 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800963 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800964 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800965
Chia-I Wu31938252016-05-23 15:31:02 +0800966 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
967 int idx = Hal::Get().GetDebugReportIndex();
968 if (idx < 0) {
969 *pPropertyCount += 1;
970 } else if (pProperties &&
971 static_cast<uint32_t>(idx) < *pPropertyCount) {
972 pProperties[idx].specVersion =
973 std::min(pProperties[idx].specVersion,
974 loader_debug_report_extension.specVersion);
975 }
976
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800977 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800978 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800979
980 return result;
981}
982
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700983void QueryPresentationProperties(
Chris Forbesfa25e632017-02-22 12:36:02 +1300984 VkPhysicalDevice physicalDevice,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700985 VkPhysicalDevicePresentationPropertiesANDROID* presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300986 // Request the android-specific presentation properties via GPDP2
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700987 VkPhysicalDeviceProperties2 properties = {
988 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
Chris Forbesfa25e632017-02-22 12:36:02 +1300989 presentation_properties,
Yiwei Zhang93b521c2020-07-11 16:32:09 -0700990 {},
Chris Forbesfa25e632017-02-22 12:36:02 +1300991 };
992
993#pragma clang diagnostic push
994#pragma clang diagnostic ignored "-Wold-style-cast"
995 presentation_properties->sType =
996 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
997#pragma clang diagnostic pop
998 presentation_properties->pNext = nullptr;
999 presentation_properties->sharedImage = VK_FALSE;
1000
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001001 GetPhysicalDeviceProperties2(physicalDevice, &properties);
Chris Forbesfa25e632017-02-22 12:36:02 +13001002}
1003
Chia-I Wu01cf3052016-03-24 16:16:21 +08001004VkResult EnumerateDeviceExtensionProperties(
1005 VkPhysicalDevice physicalDevice,
1006 const char* pLayerName,
1007 uint32_t* pPropertyCount,
1008 VkExtensionProperties* pProperties) {
1009 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +13001010 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -07001011 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +13001012 loader_extensions.push_back({
1013 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
1014 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001015
Sundong Ahnbc37dd52020-04-23 21:21:00 +09001016 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -08001017 if (hdrBoardConfig) {
1018 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
1019 VK_EXT_HDR_METADATA_SPEC_VERSION});
1020 }
1021
Chris Forbes16095002017-05-05 15:33:29 -07001022 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001023 QueryPresentationProperties(physicalDevice, &presentation_properties);
1024 if (presentation_properties.sharedImage) {
Chris Forbes16095002017-05-05 15:33:29 -07001025 loader_extensions.push_back({
1026 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
1027 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +13001028 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001029
Ian Elliott5c34de22017-04-10 14:42:30 -06001030 // conditionally add VK_GOOGLE_display_timing if present timestamps are
1031 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -07001032 const std::string timestamp_property("service.sf.present_timestamp");
1033 android::base::WaitForPropertyCreation(timestamp_property);
1034 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -06001035 loader_extensions.push_back({
1036 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
1037 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
1038 }
1039
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001040 // enumerate our extensions first
1041 if (!pLayerName && pProperties) {
1042 uint32_t count = std::min(
1043 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
1044
Yiwei Zhang5e862202019-06-21 14:59:16 -07001045 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001046
1047 if (count < loader_extensions.size()) {
1048 *pPropertyCount = count;
1049 return VK_INCOMPLETE;
1050 }
1051
1052 pProperties += count;
1053 *pPropertyCount -= count;
1054 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001055
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001056 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001057 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1058 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001059 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001060
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001061 if (pProperties) {
1062 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1063 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1064 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001065
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001066 if (strcmp(prop.extensionName,
1067 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1068 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001069
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001070 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1071 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001072
1073 if (prop.specVersion >= 8) {
1074 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1075 } else {
1076 prop.specVersion = 68;
1077 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001078 }
1079 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001080
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001081 // restore loader extension count
1082 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1083 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001084 }
1085
1086 return result;
1087}
1088
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001089VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1090 const VkAllocationCallbacks* pAllocator,
1091 VkInstance* pInstance) {
1092 const VkAllocationCallbacks& data_allocator =
1093 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1094
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001095 VkResult result = VK_SUCCESS;
1096 uint32_t icd_api_version = VK_API_VERSION_1_0;
1097 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1098 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
1099 Hal::Device().GetInstanceProcAddr(nullptr,
1100 "vkEnumerateInstanceVersion"));
1101 if (pfn_enumerate_instance_version) {
1102 ATRACE_BEGIN("pfn_enumerate_instance_version");
1103 result = (*pfn_enumerate_instance_version)(&icd_api_version);
1104 ATRACE_END();
1105 if (result != VK_SUCCESS)
1106 return result;
1107
1108 icd_api_version ^= VK_VERSION_PATCH(icd_api_version);
1109 }
1110
1111 CreateInfoWrapper wrapper(*pCreateInfo, icd_api_version, data_allocator);
1112 result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001113 if (result != VK_SUCCESS)
1114 return result;
1115
1116 InstanceData* data = AllocateInstanceData(data_allocator);
1117 if (!data)
1118 return VK_ERROR_OUT_OF_HOST_MEMORY;
1119
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001120 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001121
1122 // call into the driver
1123 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001124 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001125 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001126 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1127 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001128 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001129 if (result != VK_SUCCESS) {
1130 FreeInstanceData(data, data_allocator);
1131 return result;
1132 }
1133
1134 // initialize InstanceDriverTable
1135 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001136 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001137 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001138 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001139 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001140 if (data->driver.DestroyInstance)
1141 data->driver.DestroyInstance(instance, pAllocator);
1142
1143 FreeInstanceData(data, data_allocator);
1144
1145 return VK_ERROR_INCOMPATIBLE_DRIVER;
1146 }
1147
1148 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001149 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001150 if (!data->get_device_proc_addr) {
1151 data->driver.DestroyInstance(instance, pAllocator);
1152 FreeInstanceData(data, data_allocator);
1153
1154 return VK_ERROR_INCOMPATIBLE_DRIVER;
1155 }
1156
1157 *pInstance = instance;
1158
1159 return VK_SUCCESS;
1160}
1161
1162void DestroyInstance(VkInstance instance,
1163 const VkAllocationCallbacks* pAllocator) {
1164 InstanceData& data = GetData(instance);
1165 data.driver.DestroyInstance(instance, pAllocator);
1166
1167 VkAllocationCallbacks local_allocator;
1168 if (!pAllocator) {
1169 local_allocator = data.allocator;
1170 pAllocator = &local_allocator;
1171 }
1172
1173 FreeInstanceData(&data, *pAllocator);
1174}
1175
Chia-I Wu4901db72016-03-24 16:38:58 +08001176VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1177 const VkDeviceCreateInfo* pCreateInfo,
1178 const VkAllocationCallbacks* pAllocator,
1179 VkDevice* pDevice) {
1180 const InstanceData& instance_data = GetData(physicalDevice);
1181 const VkAllocationCallbacks& data_allocator =
1182 (pAllocator) ? *pAllocator : instance_data.allocator;
1183
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001184 VkPhysicalDeviceProperties properties;
1185 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1186 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1187 &properties);
1188 ATRACE_END();
1189
1190 CreateInfoWrapper wrapper(
1191 physicalDevice, *pCreateInfo,
1192 properties.apiVersion ^ VK_VERSION_PATCH(properties.apiVersion),
1193 data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001194 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001195 if (result != VK_SUCCESS)
1196 return result;
1197
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001198 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001199 DeviceData* data = AllocateDeviceData(data_allocator,
1200 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001201 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001202 if (!data)
1203 return VK_ERROR_OUT_OF_HOST_MEMORY;
1204
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001205 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001206
1207 // call into the driver
1208 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001209 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001210 result = instance_data.driver.CreateDevice(
1211 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1212 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001213 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001214 if (result != VK_SUCCESS) {
1215 FreeDeviceData(data, data_allocator);
1216 return result;
1217 }
1218
1219 // initialize DeviceDriverTable
1220 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001221 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1222 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001223 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1224 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1225 if (data->driver.DestroyDevice)
1226 data->driver.DestroyDevice(dev, pAllocator);
1227
1228 FreeDeviceData(data, data_allocator);
1229
1230 return VK_ERROR_INCOMPATIBLE_DRIVER;
1231 }
Chris Forbesd8277912017-02-10 14:59:59 +13001232
1233 // sanity check ANDROID_native_buffer implementation, whose set of
1234 // entrypoints varies according to the spec version.
1235 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1236 !data->driver.GetSwapchainGrallocUsageANDROID &&
1237 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1238 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1239 " must expose at least one of "
1240 "vkGetSwapchainGrallocUsageANDROID or "
1241 "vkGetSwapchainGrallocUsage2ANDROID");
1242
1243 data->driver.DestroyDevice(dev, pAllocator);
1244 FreeDeviceData(data, data_allocator);
1245
1246 return VK_ERROR_INCOMPATIBLE_DRIVER;
1247 }
1248
Yiwei Zhang2cefa732020-07-10 21:07:30 -07001249 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1250 // Log that the app is hitting software Vulkan implementation
1251 android::GraphicsEnv::getInstance().setTargetStats(
1252 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
1253 }
1254
Jesse Halldc225072016-05-30 22:40:14 -07001255 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001256
1257 *pDevice = dev;
1258
1259 return VK_SUCCESS;
1260}
1261
1262void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1263 DeviceData& data = GetData(device);
1264 data.driver.DestroyDevice(device, pAllocator);
1265
1266 VkAllocationCallbacks local_allocator;
1267 if (!pAllocator) {
1268 local_allocator = data.allocator;
1269 pAllocator = &local_allocator;
1270 }
1271
1272 FreeDeviceData(&data, *pAllocator);
1273}
1274
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001275VkResult EnumeratePhysicalDevices(VkInstance instance,
1276 uint32_t* pPhysicalDeviceCount,
1277 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001278 ATRACE_CALL();
1279
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001280 const auto& data = GetData(instance);
1281
1282 VkResult result = data.driver.EnumeratePhysicalDevices(
1283 instance, pPhysicalDeviceCount, pPhysicalDevices);
1284 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1285 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1286 SetData(pPhysicalDevices[i], data);
1287 }
1288
1289 return result;
1290}
1291
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001292VkResult EnumeratePhysicalDeviceGroups(
1293 VkInstance instance,
1294 uint32_t* pPhysicalDeviceGroupCount,
1295 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001296 ATRACE_CALL();
1297
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001298 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001299 const auto& data = GetData(instance);
1300
Yiwei Zhange4f64172020-07-05 15:17:32 -07001301 if (!data.driver.EnumeratePhysicalDeviceGroups &&
1302 !data.driver.EnumeratePhysicalDeviceGroupsKHR) {
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001303 uint32_t device_count = 0;
1304 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1305 if (result < 0)
1306 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001307
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001308 if (!pPhysicalDeviceGroupProperties) {
1309 *pPhysicalDeviceGroupCount = device_count;
1310 return result;
1311 }
1312
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001313 if (!device_count) {
1314 *pPhysicalDeviceGroupCount = 0;
1315 return result;
1316 }
Chad Versace32c087f2018-09-09 07:28:05 -07001317 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1318 if (!device_count)
1319 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001320
Yiwei Zhang5e862202019-06-21 14:59:16 -07001321 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001322 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001323 result =
1324 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001325 if (result < 0)
1326 return result;
1327
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001328 for (uint32_t i = 0; i < device_count; ++i) {
1329 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1330 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1331 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1332 }
1333 } else {
Yiwei Zhange4f64172020-07-05 15:17:32 -07001334 if (data.driver.EnumeratePhysicalDeviceGroups) {
1335 result = data.driver.EnumeratePhysicalDeviceGroups(
1336 instance, pPhysicalDeviceGroupCount,
1337 pPhysicalDeviceGroupProperties);
1338 } else {
1339 result = data.driver.EnumeratePhysicalDeviceGroupsKHR(
1340 instance, pPhysicalDeviceGroupCount,
1341 pPhysicalDeviceGroupProperties);
1342 }
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001343 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1344 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1345 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1346 for (uint32_t j = 0;
1347 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1348 j++) {
1349 SetData(
1350 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001351 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001352 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001353 }
1354 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001355 }
1356
1357 return result;
1358}
1359
Chia-I Wuba0be412016-03-24 16:24:40 +08001360void GetDeviceQueue(VkDevice device,
1361 uint32_t queueFamilyIndex,
1362 uint32_t queueIndex,
1363 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001364 ATRACE_CALL();
1365
Chia-I Wuba0be412016-03-24 16:24:40 +08001366 const auto& data = GetData(device);
1367
1368 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1369 SetData(*pQueue, data);
1370}
1371
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001372void GetDeviceQueue2(VkDevice device,
1373 const VkDeviceQueueInfo2* pQueueInfo,
1374 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001375 ATRACE_CALL();
1376
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001377 const auto& data = GetData(device);
1378
1379 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001380 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001381}
1382
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001383VkResult AllocateCommandBuffers(
1384 VkDevice device,
1385 const VkCommandBufferAllocateInfo* pAllocateInfo,
1386 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001387 ATRACE_CALL();
1388
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001389 const auto& data = GetData(device);
1390
1391 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1392 pCommandBuffers);
1393 if (result == VK_SUCCESS) {
1394 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1395 SetData(pCommandBuffers[i], data);
1396 }
1397
1398 return result;
1399}
1400
Yiwei Zhang93b521c2020-07-11 16:32:09 -07001401VkResult QueueSubmit(VkQueue queue,
1402 uint32_t submitCount,
1403 const VkSubmitInfo* pSubmits,
1404 VkFence fence) {
Yiwei Zhang899d1752019-09-23 16:05:35 -07001405 ATRACE_CALL();
1406
1407 const auto& data = GetData(queue);
1408
1409 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1410}
1411
Yiwei Zhanga55624b2020-07-05 16:05:26 -07001412void GetPhysicalDeviceFeatures2(VkPhysicalDevice physicalDevice,
1413 VkPhysicalDeviceFeatures2* pFeatures) {
1414 ATRACE_CALL();
1415
1416 const auto& driver = GetData(physicalDevice).driver;
1417
1418 if (driver.GetPhysicalDeviceFeatures2) {
1419 driver.GetPhysicalDeviceFeatures2(physicalDevice, pFeatures);
1420 return;
1421 }
1422
1423 driver.GetPhysicalDeviceFeatures2KHR(physicalDevice, pFeatures);
1424}
1425
1426void GetPhysicalDeviceProperties2(VkPhysicalDevice physicalDevice,
1427 VkPhysicalDeviceProperties2* pProperties) {
1428 ATRACE_CALL();
1429
1430 const auto& driver = GetData(physicalDevice).driver;
1431
1432 if (driver.GetPhysicalDeviceProperties2) {
1433 driver.GetPhysicalDeviceProperties2(physicalDevice, pProperties);
1434 return;
1435 }
1436
1437 driver.GetPhysicalDeviceProperties2KHR(physicalDevice, pProperties);
1438}
1439
1440void GetPhysicalDeviceFormatProperties2(
1441 VkPhysicalDevice physicalDevice,
1442 VkFormat format,
1443 VkFormatProperties2* pFormatProperties) {
1444 ATRACE_CALL();
1445
1446 const auto& driver = GetData(physicalDevice).driver;
1447
1448 if (driver.GetPhysicalDeviceFormatProperties2) {
1449 driver.GetPhysicalDeviceFormatProperties2(physicalDevice, format,
1450 pFormatProperties);
1451 return;
1452 }
1453
1454 driver.GetPhysicalDeviceFormatProperties2KHR(physicalDevice, format,
1455 pFormatProperties);
1456}
1457
1458VkResult GetPhysicalDeviceImageFormatProperties2(
1459 VkPhysicalDevice physicalDevice,
1460 const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
1461 VkImageFormatProperties2* pImageFormatProperties) {
1462 ATRACE_CALL();
1463
1464 const auto& driver = GetData(physicalDevice).driver;
1465
1466 if (driver.GetPhysicalDeviceImageFormatProperties2) {
1467 return driver.GetPhysicalDeviceImageFormatProperties2(
1468 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1469 }
1470
1471 return driver.GetPhysicalDeviceImageFormatProperties2KHR(
1472 physicalDevice, pImageFormatInfo, pImageFormatProperties);
1473}
1474
1475void GetPhysicalDeviceQueueFamilyProperties2(
1476 VkPhysicalDevice physicalDevice,
1477 uint32_t* pQueueFamilyPropertyCount,
1478 VkQueueFamilyProperties2* pQueueFamilyProperties) {
1479 ATRACE_CALL();
1480
1481 const auto& driver = GetData(physicalDevice).driver;
1482
1483 if (driver.GetPhysicalDeviceQueueFamilyProperties2) {
1484 driver.GetPhysicalDeviceQueueFamilyProperties2(
1485 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1486 return;
1487 }
1488
1489 driver.GetPhysicalDeviceQueueFamilyProperties2KHR(
1490 physicalDevice, pQueueFamilyPropertyCount, pQueueFamilyProperties);
1491}
1492
1493void GetPhysicalDeviceMemoryProperties2(
1494 VkPhysicalDevice physicalDevice,
1495 VkPhysicalDeviceMemoryProperties2* pMemoryProperties) {
1496 ATRACE_CALL();
1497
1498 const auto& driver = GetData(physicalDevice).driver;
1499
1500 if (driver.GetPhysicalDeviceMemoryProperties2) {
1501 driver.GetPhysicalDeviceMemoryProperties2(physicalDevice,
1502 pMemoryProperties);
1503 return;
1504 }
1505
1506 driver.GetPhysicalDeviceMemoryProperties2KHR(physicalDevice,
1507 pMemoryProperties);
1508}
1509
1510void GetPhysicalDeviceSparseImageFormatProperties2(
1511 VkPhysicalDevice physicalDevice,
1512 const VkPhysicalDeviceSparseImageFormatInfo2* pFormatInfo,
1513 uint32_t* pPropertyCount,
1514 VkSparseImageFormatProperties2* pProperties) {
1515 ATRACE_CALL();
1516
1517 const auto& driver = GetData(physicalDevice).driver;
1518
1519 if (driver.GetPhysicalDeviceSparseImageFormatProperties2) {
1520 driver.GetPhysicalDeviceSparseImageFormatProperties2(
1521 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1522 return;
1523 }
1524
1525 driver.GetPhysicalDeviceSparseImageFormatProperties2KHR(
1526 physicalDevice, pFormatInfo, pPropertyCount, pProperties);
1527}
1528
Yiwei Zhange1f35012020-07-05 22:52:04 -07001529void GetPhysicalDeviceExternalBufferProperties(
1530 VkPhysicalDevice physicalDevice,
1531 const VkPhysicalDeviceExternalBufferInfo* pExternalBufferInfo,
1532 VkExternalBufferProperties* pExternalBufferProperties) {
1533 ATRACE_CALL();
1534
1535 const auto& driver = GetData(physicalDevice).driver;
1536
1537 if (driver.GetPhysicalDeviceExternalBufferProperties) {
1538 driver.GetPhysicalDeviceExternalBufferProperties(
1539 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1540 return;
1541 }
1542
1543 if (driver.GetPhysicalDeviceExternalBufferPropertiesKHR) {
1544 driver.GetPhysicalDeviceExternalBufferPropertiesKHR(
1545 physicalDevice, pExternalBufferInfo, pExternalBufferProperties);
1546 return;
1547 }
1548
1549 memset(&pExternalBufferProperties->externalMemoryProperties, 0,
1550 sizeof(VkExternalMemoryProperties));
1551}
1552
1553void GetPhysicalDeviceExternalSemaphoreProperties(
1554 VkPhysicalDevice physicalDevice,
1555 const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
1556 VkExternalSemaphoreProperties* pExternalSemaphoreProperties) {
1557 ATRACE_CALL();
1558
1559 const auto& driver = GetData(physicalDevice).driver;
1560
1561 if (driver.GetPhysicalDeviceExternalSemaphoreProperties) {
1562 driver.GetPhysicalDeviceExternalSemaphoreProperties(
1563 physicalDevice, pExternalSemaphoreInfo,
1564 pExternalSemaphoreProperties);
1565 return;
1566 }
1567
1568 if (driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR) {
1569 driver.GetPhysicalDeviceExternalSemaphorePropertiesKHR(
1570 physicalDevice, pExternalSemaphoreInfo,
1571 pExternalSemaphoreProperties);
1572 return;
1573 }
1574
1575 pExternalSemaphoreProperties->exportFromImportedHandleTypes = 0;
1576 pExternalSemaphoreProperties->compatibleHandleTypes = 0;
1577 pExternalSemaphoreProperties->externalSemaphoreFeatures = 0;
1578}
1579
1580void GetPhysicalDeviceExternalFenceProperties(
1581 VkPhysicalDevice physicalDevice,
1582 const VkPhysicalDeviceExternalFenceInfo* pExternalFenceInfo,
1583 VkExternalFenceProperties* pExternalFenceProperties) {
1584 ATRACE_CALL();
1585
1586 const auto& driver = GetData(physicalDevice).driver;
1587
1588 if (driver.GetPhysicalDeviceExternalFenceProperties) {
1589 driver.GetPhysicalDeviceExternalFenceProperties(
1590 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1591 return;
1592 }
1593
1594 if (driver.GetPhysicalDeviceExternalFencePropertiesKHR) {
1595 driver.GetPhysicalDeviceExternalFencePropertiesKHR(
1596 physicalDevice, pExternalFenceInfo, pExternalFenceProperties);
1597 return;
1598 }
1599
1600 pExternalFenceProperties->exportFromImportedHandleTypes = 0;
1601 pExternalFenceProperties->compatibleHandleTypes = 0;
1602 pExternalFenceProperties->externalFenceFeatures = 0;
1603}
1604
Chia-I Wu9d518162016-03-24 14:55:27 +08001605} // namespace driver
1606} // namespace vulkan