blob: 22122a558c4c2fd3a44195dc4cd80884e9ff4bfd [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
Chia-I Wu31938252016-05-23 15:31:02 +080087 bool InitDebugReportIndex();
88
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080089 static Hal hal_;
90
91 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080092 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080093};
94
Chia-I Wu4901db72016-03-24 16:38:58 +080095class CreateInfoWrapper {
96 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080097 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080098 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080099 CreateInfoWrapper(VkPhysicalDevice physical_dev,
100 const VkDeviceCreateInfo& create_info,
101 const VkAllocationCallbacks& allocator);
102 ~CreateInfoWrapper();
103
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800104 VkResult Validate();
Ian Elliottf3e872d2017-11-02 10:15:13 -0600105 void DowngradeApiVersion();
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700106 void UpgradeDeviceCoreApiVersion(uint32_t api_version);
Chia-I Wu4901db72016-03-24 16:38:58 +0800107
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800108 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
109 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800110
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800111 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800112 explicit operator const VkDeviceCreateInfo*() const;
113
114 private:
115 struct ExtensionFilter {
116 VkExtensionProperties* exts;
117 uint32_t ext_count;
118
119 const char** names;
120 uint32_t name_count;
121 };
122
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800123 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800124
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800125 VkResult SanitizeLayers();
126 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800127
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800128 VkResult QueryExtensionCount(uint32_t& count) const;
129 VkResult EnumerateExtensions(uint32_t& count,
130 VkExtensionProperties* props) const;
131 VkResult InitExtensionFilter();
132 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800133
134 const bool is_instance_;
135 const VkAllocationCallbacks& allocator_;
136
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800137 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800138
139 union {
140 VkInstanceCreateInfo instance_info_;
141 VkDeviceCreateInfo dev_info_;
142 };
143
Ian Elliottf3e872d2017-11-02 10:15:13 -0600144 VkApplicationInfo application_info_;
145
Chia-I Wu4901db72016-03-24 16:38:58 +0800146 ExtensionFilter extension_filter_;
147
148 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
149 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
150};
151
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800152Hal Hal::hal_;
153
Jesse Hall53457db2016-12-14 16:54:06 -0800154void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700155 const std::string_view subname) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800156 ATRACE_CALL();
157
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700158 std::stringstream ss;
159 ss << "vulkan." << subname << ".so";
160 return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800161}
162
163const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700164 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800165 "ro.board.platform",
166}};
167
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800168// LoadDriver returns:
169// * 0 when succeed, or
170// * -ENOENT when fail to open binary libraries, or
171// * -EINVAL when fail to find HAL_MODULE_INFO_SYM_AS_STR or
172// HWVULKAN_HARDWARE_MODULE_ID in the library.
Jesse Hall00e61ff2017-04-07 16:48:02 -0700173int LoadDriver(android_namespace_t* library_namespace,
174 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800175 ATRACE_CALL();
176
Jesse Hall53457db2016-12-14 16:54:06 -0800177 const android_dlextinfo dlextinfo = {
178 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700179 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800180 };
Jesse Hall53457db2016-12-14 16:54:06 -0800181 void* so = nullptr;
182 char prop[PROPERTY_VALUE_MAX];
183 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
184 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniers60307ce2019-10-25 13:34:21 -0700185 if (prop_len > 0 && prop_len <= UINT_MAX) {
186 std::string_view lib_name(prop, static_cast<unsigned int>(prop_len));
187 so = LoadLibrary(dlextinfo, lib_name);
Jesse Hall53457db2016-12-14 16:54:06 -0800188 if (so)
189 break;
190 }
191 }
192 if (!so)
193 return -ENOENT;
194
Jesse Hall00e61ff2017-04-07 16:48:02 -0700195 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800196 if (!hmi) {
197 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
198 dlclose(so);
199 return -EINVAL;
200 }
201 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
202 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
203 dlclose(so);
204 return -EINVAL;
205 }
206 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700207 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800208 return 0;
209}
210
Jesse Hall00e61ff2017-04-07 16:48:02 -0700211int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800212 ATRACE_CALL();
213
Jesse Hall00e61ff2017-04-07 16:48:02 -0700214 auto ns = android_get_exported_namespace("sphal");
215 if (!ns)
216 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800217 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700218 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700219 return LoadDriver(ns, module);
220}
221
222int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800223 ATRACE_CALL();
224
Jesse Hall00e61ff2017-04-07 16:48:02 -0700225 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
226 if (!ns)
227 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800228 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700229 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Peiyong Linefa0cbd2020-01-29 20:51:50 -0800230 int result = LoadDriver(ns, module);
231 if (result != 0) {
232 LOG_ALWAYS_FATAL(
233 "couldn't find an updated Vulkan implementation from %s",
234 android::GraphicsEnv::getInstance().getDriverPath().c_str());
235 }
236 return result;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700237}
238
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800239bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800240 ATRACE_CALL();
241
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242 const nsecs_t openTime = systemTime();
243
Jesse Halldc225072016-05-30 22:40:14 -0700244 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800245
246 // Use a stub device unless we successfully open a real HAL device.
247 hal_.dev_ = &stubhal::kDevice;
248
Jesse Hall53457db2016-12-14 16:54:06 -0800249 int result;
250 const hwvulkan_module_t* module = nullptr;
251
Jesse Hall00e61ff2017-04-07 16:48:02 -0700252 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800253 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700254 result = LoadBuiltinDriver(&module);
255 if (result != 0) {
256 // -ENOENT means the sphal namespace doesn't exist, not that there
257 // is a problem with the driver.
258 ALOGW_IF(
259 result != -ENOENT,
260 "Failed to load Vulkan driver into sphal namespace. This "
261 "usually means the driver has forbidden library dependencies."
262 "Please fix, this will soon stop working.");
263 result =
264 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
265 reinterpret_cast<const hw_module_t**>(&module));
266 }
Jesse Hall53457db2016-12-14 16:54:06 -0800267 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800268 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800269 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700270 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800271 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800272 return true;
273 }
274
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800275
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800276 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800277 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800278 result =
279 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
280 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800281 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800282 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800283 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700284 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800285 // Any device with a Vulkan HAL should be able to open the device.
286 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
287 result);
288 return false;
289 }
290
291 hal_.dev_ = device;
292
Chia-I Wu31938252016-05-23 15:31:02 +0800293 hal_.InitDebugReportIndex();
294
Yiwei Zhangd9861812019-02-13 11:51:55 -0800295 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700296 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800297
Chia-I Wu31938252016-05-23 15:31:02 +0800298 return true;
299}
300
301bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800302 ATRACE_CALL();
303
Chia-I Wu31938252016-05-23 15:31:02 +0800304 uint32_t count;
305 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
306 VK_SUCCESS) {
307 ALOGE("failed to get HAL instance extension count");
308 return false;
309 }
310
311 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
312 malloc(sizeof(VkExtensionProperties) * count));
313 if (!exts) {
314 ALOGE("failed to allocate HAL instance extension array");
315 return false;
316 }
317
318 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
319 VK_SUCCESS) {
320 ALOGE("failed to enumerate HAL instance extensions");
321 free(exts);
322 return false;
323 }
324
325 for (uint32_t i = 0; i < count; i++) {
326 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
327 0) {
328 debug_report_index_ = static_cast<int>(i);
329 break;
330 }
331 }
332
333 free(exts);
334
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800335 return true;
336}
337
338CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800339 const VkAllocationCallbacks& allocator)
340 : is_instance_(true),
341 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800342 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800343 instance_info_(create_info),
344 extension_filter_() {
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700345 // instance core versions need to match the loader api version
346 for (uint32_t i = ProcHook::EXTENSION_CORE_1_0;
347 i != ProcHook::EXTENSION_COUNT; ++i) {
348 hook_extensions_.set(i);
349 hal_extensions_.set(i);
350 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800351}
352
Chia-I Wu4901db72016-03-24 16:38:58 +0800353CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
354 const VkDeviceCreateInfo& create_info,
355 const VkAllocationCallbacks& allocator)
356 : is_instance_(false),
357 allocator_(allocator),
358 physical_dev_(physical_dev),
359 dev_info_(create_info),
360 extension_filter_() {
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700361 // initialize with baseline core API version
362 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
363 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
Chia-I Wu4901db72016-03-24 16:38:58 +0800364}
365
366CreateInfoWrapper::~CreateInfoWrapper() {
367 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
368 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
369}
370
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800371VkResult CreateInfoWrapper::Validate() {
372 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800373 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800374 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800375 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800376 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800377
378 return result;
379}
380
381const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800382CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800383 return hook_extensions_;
384}
385
386const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800387CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800388 return hal_extensions_;
389}
390
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800391CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
392 return &instance_info_;
393}
394
Chia-I Wu4901db72016-03-24 16:38:58 +0800395CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
396 return &dev_info_;
397}
398
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800399VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800400 const struct StructHeader {
401 VkStructureType type;
402 const void* next;
403 } * header;
404
405 if (is_instance_) {
406 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
407
408 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
409 while (header &&
410 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
411 header = reinterpret_cast<const StructHeader*>(header->next);
412
413 instance_info_.pNext = header;
414 } else {
415 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
416
417 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
418 while (header &&
419 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
420 header = reinterpret_cast<const StructHeader*>(header->next);
421
422 dev_info_.pNext = header;
423 }
424
425 return VK_SUCCESS;
426}
427
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800428VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800429 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
430 : dev_info_.ppEnabledLayerNames;
431 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
432 : dev_info_.enabledLayerCount;
433
434 // remove all layers
435 layer_names = nullptr;
436 layer_count = 0;
437
438 return VK_SUCCESS;
439}
440
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800441VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800442 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
443 : dev_info_.ppEnabledExtensionNames;
444 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
445 : dev_info_.enabledExtensionCount;
446 if (!ext_count)
447 return VK_SUCCESS;
448
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800449 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800450 if (result != VK_SUCCESS)
451 return result;
452
453 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800454 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800455
Jesse Halld3d887a2018-03-05 13:34:45 -0800456 // Enable device extensions that contain physical-device commands, so that
457 // vkGetInstanceProcAddr will return those physical-device commands.
458 if (is_instance_) {
459 hook_extensions_.set(ProcHook::KHR_swapchain);
460 }
461
Chia-I Wu4901db72016-03-24 16:38:58 +0800462 ext_names = extension_filter_.names;
463 ext_count = extension_filter_.name_count;
464
465 return VK_SUCCESS;
466}
467
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800468VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800469 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800470 return Hal::Device().EnumerateInstanceExtensionProperties(
471 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800472 } else {
473 const auto& driver = GetData(physical_dev_).driver;
474 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
475 &count, nullptr);
476 }
477}
478
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800479VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800480 uint32_t& count,
481 VkExtensionProperties* props) const {
482 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800483 return Hal::Device().EnumerateInstanceExtensionProperties(
484 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800485 } else {
486 const auto& driver = GetData(physical_dev_).driver;
487 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
488 &count, props);
489 }
490}
491
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800492VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800493 // query extension count
494 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800495 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800496 if (result != VK_SUCCESS || count == 0)
497 return result;
498
499 auto& filter = extension_filter_;
500 filter.exts =
501 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
502 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
503 alignof(VkExtensionProperties),
504 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
505 if (!filter.exts)
506 return VK_ERROR_OUT_OF_HOST_MEMORY;
507
508 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800509 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800510 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
511 return result;
512
513 if (!count)
514 return VK_SUCCESS;
515
516 filter.ext_count = count;
517
518 // allocate name array
519 uint32_t enabled_ext_count = (is_instance_)
520 ? instance_info_.enabledExtensionCount
521 : dev_info_.enabledExtensionCount;
522 count = std::min(filter.ext_count, enabled_ext_count);
523 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
524 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
525 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
526 if (!filter.names)
527 return VK_ERROR_OUT_OF_HOST_MEMORY;
528
529 return VK_SUCCESS;
530}
531
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800532void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800533 auto& filter = extension_filter_;
534
535 ProcHook::Extension ext_bit = GetProcHookExtension(name);
536 if (is_instance_) {
537 switch (ext_bit) {
538 case ProcHook::KHR_android_surface:
539 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700540 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300541 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800542 hook_extensions_.set(ext_bit);
543 // return now as these extensions do not require HAL support
544 return;
545 case ProcHook::EXT_debug_report:
546 // both we and HAL can take part in
547 hook_extensions_.set(ext_bit);
548 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300549 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700550 case ProcHook::EXTENSION_UNKNOWN:
551 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800552 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700553
Yiwei Zhang23143102019-04-10 18:24:05 -0700554 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700555 case ProcHook::KHR_incremental_present:
556 case ProcHook::KHR_shared_presentable_image:
557 case ProcHook::KHR_swapchain:
558 case ProcHook::EXT_hdr_metadata:
559 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
560 case ProcHook::ANDROID_native_buffer:
561 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700562 case ProcHook::EXTENSION_CORE_1_0:
563 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700564 case ProcHook::EXTENSION_COUNT:
565 // Device and meta extensions. If we ever get here it's a bug in
566 // our code. But enumerating them lets us avoid having a default
567 // case, and default hides other bugs.
568 ALOGE(
569 "CreateInfoWrapper::FilterExtension: invalid instance "
570 "extension '%s'. FIX ME",
571 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800572 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700573
574 // Don't use a default case. Without it, -Wswitch will tell us
575 // at compile time if someone adds a new ProcHook extension but
576 // doesn't handle it above. That's a real bug that has
577 // not-immediately-obvious effects.
578 //
579 // default:
580 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800581 }
582 } else {
583 switch (ext_bit) {
584 case ProcHook::KHR_swapchain:
585 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
586 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
587 ext_bit = ProcHook::ANDROID_native_buffer;
588 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700589 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700590 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300591 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700592 hook_extensions_.set(ext_bit);
593 // return now as these extensions do not require HAL support
594 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700595 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700596 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700597 hook_extensions_.set(ext_bit);
598 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700599 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800600 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700601 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800602 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700603
604 case ProcHook::KHR_android_surface:
605 case ProcHook::KHR_get_physical_device_properties2:
606 case ProcHook::KHR_get_surface_capabilities2:
607 case ProcHook::KHR_surface:
608 case ProcHook::EXT_debug_report:
609 case ProcHook::EXT_swapchain_colorspace:
610 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700611 case ProcHook::EXTENSION_CORE_1_0:
612 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700613 case ProcHook::EXTENSION_COUNT:
614 // Instance and meta extensions. If we ever get here it's a bug
615 // in our code. But enumerating them lets us avoid having a
616 // default case, and default hides other bugs.
617 ALOGE(
618 "CreateInfoWrapper::FilterExtension: invalid device "
619 "extension '%s'. FIX ME",
620 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800621 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700622
623 // Don't use a default case. Without it, -Wswitch will tell us
624 // at compile time if someone adds a new ProcHook extension but
625 // doesn't handle it above. That's a real bug that has
626 // not-immediately-obvious effects.
627 //
628 // default:
629 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800630 }
631 }
632
633 for (uint32_t i = 0; i < filter.ext_count; i++) {
634 const VkExtensionProperties& props = filter.exts[i];
635 // ignore unknown extensions
636 if (strcmp(name, props.extensionName) != 0)
637 continue;
638
Chia-I Wu4901db72016-03-24 16:38:58 +0800639 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800640 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
641 if (ext_bit == ProcHook::ANDROID_native_buffer)
642 hook_extensions_.set(ProcHook::KHR_swapchain);
643
644 hal_extensions_.set(ext_bit);
645 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800646
647 break;
648 }
649}
650
Ian Elliottf3e872d2017-11-02 10:15:13 -0600651void CreateInfoWrapper::DowngradeApiVersion() {
652 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
653 if (instance_info_.pApplicationInfo) {
654 application_info_ = *instance_info_.pApplicationInfo;
655 instance_info_.pApplicationInfo = &application_info_;
656 application_info_.apiVersion = VK_API_VERSION_1_0;
657 }
658}
659
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700660void CreateInfoWrapper::UpgradeDeviceCoreApiVersion(uint32_t api_version) {
661 ALOG_ASSERT(!is_instance_, "Device only API called by instance wrapper.");
662
663#pragma clang diagnostic push
664#pragma clang diagnostic ignored "-Wold-style-cast"
665 api_version ^= VK_VERSION_PATCH(api_version);
666#pragma clang diagnostic pop
667
668 // cap the API version to the loader supported highest version
669 if (api_version > VK_API_VERSION_1_1)
670 api_version = VK_API_VERSION_1_1;
671
672 switch (api_version) {
673 case VK_API_VERSION_1_1:
674 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
675 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
676 [[clang::fallthrough]];
677 case VK_API_VERSION_1_0:
678 break;
679 default:
680 ALOGD("Unknown upgrade API version[%u]", api_version);
681 break;
682 }
683}
684
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800685VKAPI_ATTR void* DefaultAllocate(void*,
686 size_t size,
687 size_t alignment,
688 VkSystemAllocationScope) {
689 void* ptr = nullptr;
690 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
691 // additionally requires that it be at least sizeof(void*).
692 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
693 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
694 ret, ptr);
695 return ret == 0 ? ptr : nullptr;
696}
697
698VKAPI_ATTR void* DefaultReallocate(void*,
699 void* ptr,
700 size_t size,
701 size_t alignment,
702 VkSystemAllocationScope) {
703 if (size == 0) {
704 free(ptr);
705 return nullptr;
706 }
707
Yiwei Zhanga885c062019-10-24 12:07:57 -0700708 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800709 // request is smaller than the existing chunk, we just continue using it.
710 // Right now the loader never reallocs, so this doesn't matter. If that
711 // changes, or if this code is copied into some other project, this should
712 // probably have a heuristic to allocate-copy-free when doing so will save
713 // "enough" space.
714 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
715 if (size <= old_size)
716 return ptr;
717
718 void* new_ptr = nullptr;
719 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
720 return nullptr;
721 if (ptr) {
722 memcpy(new_ptr, ptr, std::min(old_size, size));
723 free(ptr);
724 }
725 return new_ptr;
726}
727
728VKAPI_ATTR void DefaultFree(void*, void* ptr) {
729 ALOGD_CALLSTACK("Free: %p", ptr);
730 free(ptr);
731}
732
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800733InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
734 void* data_mem = allocator.pfnAllocation(
735 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
736 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
737 if (!data_mem)
738 return nullptr;
739
740 return new (data_mem) InstanceData(allocator);
741}
742
743void FreeInstanceData(InstanceData* data,
744 const VkAllocationCallbacks& allocator) {
745 data->~InstanceData();
746 allocator.pfnFree(allocator.pUserData, data);
747}
748
Chia-I Wu950d6e12016-05-03 09:12:35 +0800749DeviceData* AllocateDeviceData(
750 const VkAllocationCallbacks& allocator,
751 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800752 void* data_mem = allocator.pfnAllocation(
753 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
754 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
755 if (!data_mem)
756 return nullptr;
757
Chia-I Wu950d6e12016-05-03 09:12:35 +0800758 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800759}
760
761void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
762 data->~DeviceData();
763 allocator.pfnFree(allocator.pUserData, data);
764}
765
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800766} // anonymous namespace
767
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800768bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800769 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800770}
771
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800772const VkAllocationCallbacks& GetDefaultAllocator() {
773 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
774 .pUserData = nullptr,
775 .pfnAllocation = DefaultAllocate,
776 .pfnReallocation = DefaultReallocate,
777 .pfnFree = DefaultFree,
778 };
779
780 return kDefaultAllocCallbacks;
781}
782
Chia-I Wueb7db122016-03-24 09:11:06 +0800783PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
784 const ProcHook* hook = GetProcHook(pName);
785 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800786 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800787
788 if (!instance) {
789 if (hook->type == ProcHook::GLOBAL)
790 return hook->proc;
791
Chia-I Wu109f8982016-04-22 06:40:40 +0800792 // v0 layers expect
793 //
794 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
795 //
796 // to work.
797 if (strcmp(pName, "vkCreateDevice") == 0)
798 return hook->proc;
799
Chia-I Wueb7db122016-03-24 09:11:06 +0800800 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800801 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800802 pName);
803
Chia-I Wu109f8982016-04-22 06:40:40 +0800804 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800805 }
806
807 PFN_vkVoidFunction proc;
808
809 switch (hook->type) {
810 case ProcHook::INSTANCE:
811 proc = (GetData(instance).hook_extensions[hook->extension])
812 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800813 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800814 break;
815 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700816 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800817 ? hook->proc
818 : hook->checked_proc;
819 break;
820 default:
821 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800822 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800823 pName);
824 proc = nullptr;
825 break;
826 }
827
828 return proc;
829}
830
831PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
832 const ProcHook* hook = GetProcHook(pName);
833 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800834 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800835
836 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800837 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800838 return nullptr;
839 }
840
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800841 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
842 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800843}
844
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800845VkResult EnumerateInstanceExtensionProperties(
846 const char* pLayerName,
847 uint32_t* pPropertyCount,
848 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700849 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600850 loader_extensions.push_back({
851 VK_KHR_SURFACE_EXTENSION_NAME,
852 VK_KHR_SURFACE_SPEC_VERSION});
853 loader_extensions.push_back({
854 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
855 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
856 loader_extensions.push_back({
857 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
858 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700859 loader_extensions.push_back({
860 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
861 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600862
Chia-I Wu31938252016-05-23 15:31:02 +0800863 static const VkExtensionProperties loader_debug_report_extension = {
864 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
865 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800866
867 // enumerate our extensions first
868 if (!pLayerName && pProperties) {
869 uint32_t count = std::min(
870 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
871
Yiwei Zhang5e862202019-06-21 14:59:16 -0700872 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800873
874 if (count < loader_extensions.size()) {
875 *pPropertyCount = count;
876 return VK_INCOMPLETE;
877 }
878
879 pProperties += count;
880 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800881
882 if (Hal::Get().GetDebugReportIndex() < 0) {
883 if (!*pPropertyCount) {
884 *pPropertyCount = count;
885 return VK_INCOMPLETE;
886 }
887
888 pProperties[0] = loader_debug_report_extension;
889 pProperties += 1;
890 *pPropertyCount -= 1;
891 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800892 }
893
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800894 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800895 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800896 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800897 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800898
Chia-I Wu31938252016-05-23 15:31:02 +0800899 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
900 int idx = Hal::Get().GetDebugReportIndex();
901 if (idx < 0) {
902 *pPropertyCount += 1;
903 } else if (pProperties &&
904 static_cast<uint32_t>(idx) < *pPropertyCount) {
905 pProperties[idx].specVersion =
906 std::min(pProperties[idx].specVersion,
907 loader_debug_report_extension.specVersion);
908 }
909
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800910 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800911 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800912
913 return result;
914}
915
Chris Forbesfa25e632017-02-22 12:36:02 +1300916bool QueryPresentationProperties(
917 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700918 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300919 const InstanceData& data = GetData(physicalDevice);
920
921 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700922 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
923 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300924 return false;
925
926 // Request the android-specific presentation properties via GPDP2
927 VkPhysicalDeviceProperties2KHR properties = {
928 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
929 presentation_properties,
930 {}
931 };
932
933#pragma clang diagnostic push
934#pragma clang diagnostic ignored "-Wold-style-cast"
935 presentation_properties->sType =
936 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
937#pragma clang diagnostic pop
938 presentation_properties->pNext = nullptr;
939 presentation_properties->sharedImage = VK_FALSE;
940
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700941 if (data.driver.GetPhysicalDeviceProperties2KHR) {
942 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
943 &properties);
944 } else {
945 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
946 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300947
948 return true;
949}
950
Chia-I Wu01cf3052016-03-24 16:16:21 +0800951VkResult EnumerateDeviceExtensionProperties(
952 VkPhysicalDevice physicalDevice,
953 const char* pLayerName,
954 uint32_t* pPropertyCount,
955 VkExtensionProperties* pProperties) {
956 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300957 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700958 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300959 loader_extensions.push_back({
960 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
961 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300962
Sundong Ahnbc37dd52020-04-23 21:21:00 +0900963 bool hdrBoardConfig = android::sysprop::has_HDR_display(false);
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800964 if (hdrBoardConfig) {
965 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
966 VK_EXT_HDR_METADATA_SPEC_VERSION});
967 }
968
Chris Forbes16095002017-05-05 15:33:29 -0700969 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
970 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
971 presentation_properties.sharedImage) {
972 loader_extensions.push_back({
973 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
974 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300975 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700976
Ian Elliott5c34de22017-04-10 14:42:30 -0600977 // conditionally add VK_GOOGLE_display_timing if present timestamps are
978 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700979 const std::string timestamp_property("service.sf.present_timestamp");
980 android::base::WaitForPropertyCreation(timestamp_property);
981 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600982 loader_extensions.push_back({
983 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
984 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
985 }
986
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700987 // enumerate our extensions first
988 if (!pLayerName && pProperties) {
989 uint32_t count = std::min(
990 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
991
Yiwei Zhang5e862202019-06-21 14:59:16 -0700992 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700993
994 if (count < loader_extensions.size()) {
995 *pPropertyCount = count;
996 return VK_INCOMPLETE;
997 }
998
999 pProperties += count;
1000 *pPropertyCount -= count;
1001 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001002
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001003 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001004 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1005 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001006 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001007
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001008 if (pProperties) {
1009 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1010 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1011 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001012
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001013 if (strcmp(prop.extensionName,
1014 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1015 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001016
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001017 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1018 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001019
1020 if (prop.specVersion >= 8) {
1021 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1022 } else {
1023 prop.specVersion = 68;
1024 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001025 }
1026 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001027
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001028 // restore loader extension count
1029 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1030 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001031 }
1032
1033 return result;
1034}
1035
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001036VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1037 const VkAllocationCallbacks* pAllocator,
1038 VkInstance* pInstance) {
1039 const VkAllocationCallbacks& data_allocator =
1040 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1041
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001042 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001043 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001044 if (result != VK_SUCCESS)
1045 return result;
1046
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001047 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001048 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001049 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001050 if (!data)
1051 return VK_ERROR_OUT_OF_HOST_MEMORY;
1052
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001053 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001054
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001055 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001056#pragma clang diagnostic push
1057#pragma clang diagnostic ignored "-Wold-style-cast"
1058 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1059 ? pCreateInfo->pApplicationInfo->apiVersion
1060 : VK_API_VERSION_1_0);
1061 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1062 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1063 uint32_t icd_api_version;
1064 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1065 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001066 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001067 "vkEnumerateInstanceVersion"));
1068 if (!pfn_enumerate_instance_version) {
1069 icd_api_version = VK_API_VERSION_1_0;
1070 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001071 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001072 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001073 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001074 }
1075 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1076 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1077
1078 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1079 ((api_major_version > 1) || (api_minor_version > 0))) {
1080 api_version = VK_API_VERSION_1_0;
1081 wrapper.DowngradeApiVersion();
1082 }
1083#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001084 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001085
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001086 // call into the driver
1087 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001088 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001089 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001090 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1091 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001092 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001093 if (result != VK_SUCCESS) {
1094 FreeInstanceData(data, data_allocator);
1095 return result;
1096 }
1097
1098 // initialize InstanceDriverTable
1099 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001100 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001101 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001102 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001103 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001104 if (data->driver.DestroyInstance)
1105 data->driver.DestroyInstance(instance, pAllocator);
1106
1107 FreeInstanceData(data, data_allocator);
1108
1109 return VK_ERROR_INCOMPATIBLE_DRIVER;
1110 }
1111
1112 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001113 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001114 if (!data->get_device_proc_addr) {
1115 data->driver.DestroyInstance(instance, pAllocator);
1116 FreeInstanceData(data, data_allocator);
1117
1118 return VK_ERROR_INCOMPATIBLE_DRIVER;
1119 }
1120
1121 *pInstance = instance;
1122
1123 return VK_SUCCESS;
1124}
1125
1126void DestroyInstance(VkInstance instance,
1127 const VkAllocationCallbacks* pAllocator) {
1128 InstanceData& data = GetData(instance);
1129 data.driver.DestroyInstance(instance, pAllocator);
1130
1131 VkAllocationCallbacks local_allocator;
1132 if (!pAllocator) {
1133 local_allocator = data.allocator;
1134 pAllocator = &local_allocator;
1135 }
1136
1137 FreeInstanceData(&data, *pAllocator);
1138}
1139
Chia-I Wu4901db72016-03-24 16:38:58 +08001140VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1141 const VkDeviceCreateInfo* pCreateInfo,
1142 const VkAllocationCallbacks* pAllocator,
1143 VkDevice* pDevice) {
1144 const InstanceData& instance_data = GetData(physicalDevice);
1145 const VkAllocationCallbacks& data_allocator =
1146 (pAllocator) ? *pAllocator : instance_data.allocator;
1147
1148 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001149 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001150 if (result != VK_SUCCESS)
1151 return result;
1152
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001153 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001154 DeviceData* data = AllocateDeviceData(data_allocator,
1155 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001156 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001157 if (!data)
1158 return VK_ERROR_OUT_OF_HOST_MEMORY;
1159
Yiwei Zhang7cc36a52019-10-11 19:02:09 -07001160 VkPhysicalDeviceProperties properties;
1161 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1162 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1163 &properties);
1164 ATRACE_END();
1165
1166 wrapper.UpgradeDeviceCoreApiVersion(properties.apiVersion);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001167 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001168
1169 // call into the driver
1170 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001171 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001172 result = instance_data.driver.CreateDevice(
1173 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1174 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001175 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001176 if (result != VK_SUCCESS) {
1177 FreeDeviceData(data, data_allocator);
1178 return result;
1179 }
1180
1181 // initialize DeviceDriverTable
1182 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001183 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1184 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001185 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1186 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1187 if (data->driver.DestroyDevice)
1188 data->driver.DestroyDevice(dev, pAllocator);
1189
1190 FreeDeviceData(data, data_allocator);
1191
1192 return VK_ERROR_INCOMPATIBLE_DRIVER;
1193 }
Chris Forbesd8277912017-02-10 14:59:59 +13001194
1195 // sanity check ANDROID_native_buffer implementation, whose set of
1196 // entrypoints varies according to the spec version.
1197 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1198 !data->driver.GetSwapchainGrallocUsageANDROID &&
1199 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1200 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1201 " must expose at least one of "
1202 "vkGetSwapchainGrallocUsageANDROID or "
1203 "vkGetSwapchainGrallocUsage2ANDROID");
1204
1205 data->driver.DestroyDevice(dev, pAllocator);
1206 FreeDeviceData(data, data_allocator);
1207
1208 return VK_ERROR_INCOMPATIBLE_DRIVER;
1209 }
1210
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001211 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1212 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangbcba4112019-07-03 13:39:32 -07001213 android::GraphicsEnv::getInstance().setTargetStats(
1214 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001215 }
1216
Jesse Halldc225072016-05-30 22:40:14 -07001217 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001218
1219 *pDevice = dev;
1220
1221 return VK_SUCCESS;
1222}
1223
1224void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1225 DeviceData& data = GetData(device);
1226 data.driver.DestroyDevice(device, pAllocator);
1227
1228 VkAllocationCallbacks local_allocator;
1229 if (!pAllocator) {
1230 local_allocator = data.allocator;
1231 pAllocator = &local_allocator;
1232 }
1233
1234 FreeDeviceData(&data, *pAllocator);
1235}
1236
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001237VkResult EnumeratePhysicalDevices(VkInstance instance,
1238 uint32_t* pPhysicalDeviceCount,
1239 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001240 ATRACE_CALL();
1241
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001242 const auto& data = GetData(instance);
1243
1244 VkResult result = data.driver.EnumeratePhysicalDevices(
1245 instance, pPhysicalDeviceCount, pPhysicalDevices);
1246 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1247 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1248 SetData(pPhysicalDevices[i], data);
1249 }
1250
1251 return result;
1252}
1253
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001254VkResult EnumeratePhysicalDeviceGroups(
1255 VkInstance instance,
1256 uint32_t* pPhysicalDeviceGroupCount,
1257 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001258 ATRACE_CALL();
1259
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001260 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001261 const auto& data = GetData(instance);
1262
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001263 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1264 uint32_t device_count = 0;
1265 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1266 if (result < 0)
1267 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001268
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001269 if (!pPhysicalDeviceGroupProperties) {
1270 *pPhysicalDeviceGroupCount = device_count;
1271 return result;
1272 }
1273
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001274 if (!device_count) {
1275 *pPhysicalDeviceGroupCount = 0;
1276 return result;
1277 }
Chad Versace32c087f2018-09-09 07:28:05 -07001278 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1279 if (!device_count)
1280 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001281
Yiwei Zhang5e862202019-06-21 14:59:16 -07001282 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001283 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001284 result =
1285 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001286 if (result < 0)
1287 return result;
1288
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001289 for (uint32_t i = 0; i < device_count; ++i) {
1290 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1291 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1292 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1293 }
1294 } else {
1295 result = data.driver.EnumeratePhysicalDeviceGroups(
1296 instance, pPhysicalDeviceGroupCount,
1297 pPhysicalDeviceGroupProperties);
1298 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1299 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1300 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1301 for (uint32_t j = 0;
1302 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1303 j++) {
1304 SetData(
1305 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001306 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001307 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001308 }
1309 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001310 }
1311
1312 return result;
1313}
1314
Chia-I Wuba0be412016-03-24 16:24:40 +08001315void GetDeviceQueue(VkDevice device,
1316 uint32_t queueFamilyIndex,
1317 uint32_t queueIndex,
1318 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001319 ATRACE_CALL();
1320
Chia-I Wuba0be412016-03-24 16:24:40 +08001321 const auto& data = GetData(device);
1322
1323 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1324 SetData(*pQueue, data);
1325}
1326
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001327void GetDeviceQueue2(VkDevice device,
1328 const VkDeviceQueueInfo2* pQueueInfo,
1329 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001330 ATRACE_CALL();
1331
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001332 const auto& data = GetData(device);
1333
1334 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001335 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001336}
1337
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001338VKAPI_ATTR VkResult
1339AllocateCommandBuffers(VkDevice device,
1340 const VkCommandBufferAllocateInfo* pAllocateInfo,
1341 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001342 ATRACE_CALL();
1343
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001344 const auto& data = GetData(device);
1345
1346 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1347 pCommandBuffers);
1348 if (result == VK_SUCCESS) {
1349 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1350 SetData(pCommandBuffers[i], data);
1351 }
1352
1353 return result;
1354}
1355
Yiwei Zhang899d1752019-09-23 16:05:35 -07001356VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1357 uint32_t submitCount,
1358 const VkSubmitInfo* pSubmits,
1359 VkFence fence) {
1360 ATRACE_CALL();
1361
1362 const auto& data = GetData(queue);
1363
1364 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1365}
1366
Chia-I Wu9d518162016-03-24 14:55:27 +08001367} // namespace driver
1368} // namespace vulkan