blob: c1994f9976c6cd1bfc1fec6365b1902eff218434 [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
Jesse Hall53457db2016-12-14 16:54:06 -080026#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080027#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070028#include <android-base/properties.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080029#include <configstore/Utils.h>
Jesse Hall53457db2016-12-14 16:54:06 -080030#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070032#include <log/log.h>
Yiwei Zhange40dd732019-08-05 16:41:03 -070033#include <nativeloader/dlext_namespaces.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070034#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080035#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080036#include <utils/Trace.h>
Jesse Hall53457db2016-12-14 16:54:06 -080037
Yiwei Zhang5e862202019-06-21 14:59:16 -070038#include <algorithm>
39#include <array>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070040#include <climits>
Yiwei Zhang5e862202019-06-21 14:59:16 -070041#include <new>
Nick Desaulniers7c123cc2019-10-21 13:52:41 -070042#include <string_view>
43#include <sstream>
Yiwei Zhang5e862202019-06-21 14:59:16 -070044#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070045
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070046#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080047
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080048using namespace android::hardware::configstore;
49using namespace android::hardware::configstore::V1_0;
50
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080051// #define ENABLE_ALLOC_CALLSTACKS 1
52#if ENABLE_ALLOC_CALLSTACKS
53#include <utils/CallStack.h>
54#define ALOGD_CALLSTACK(...) \
55 do { \
56 ALOGD(__VA_ARGS__); \
57 android::CallStack callstack; \
58 callstack.update(); \
59 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
60 } while (false)
61#else
62#define ALOGD_CALLSTACK(...) \
63 do { \
64 } while (false)
65#endif
66
Chia-I Wu9d518162016-03-24 14:55:27 +080067namespace vulkan {
68namespace driver {
69
Chia-I Wu136b8eb2016-03-24 15:01:52 +080070namespace {
71
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080072class Hal {
73 public:
74 static bool Open();
75
76 static const Hal& Get() { return hal_; }
77 static const hwvulkan_device_t& Device() { return *Get().dev_; }
78
Chia-I Wu31938252016-05-23 15:31:02 +080079 int GetDebugReportIndex() const { return debug_report_index_; }
80
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080081 private:
Chia-I Wu31938252016-05-23 15:31:02 +080082 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080083 Hal(const Hal&) = delete;
84 Hal& operator=(const Hal&) = delete;
85
Chia-I Wu31938252016-05-23 15:31:02 +080086 bool InitDebugReportIndex();
87
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080088 static Hal hal_;
89
90 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080091 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080092};
93
Chia-I Wu4901db72016-03-24 16:38:58 +080094class CreateInfoWrapper {
95 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080096 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080097 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080098 CreateInfoWrapper(VkPhysicalDevice physical_dev,
99 const VkDeviceCreateInfo& create_info,
100 const VkAllocationCallbacks& allocator);
101 ~CreateInfoWrapper();
102
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800103 VkResult Validate();
Ian Elliottf3e872d2017-11-02 10:15:13 -0600104 void DowngradeApiVersion();
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700105 void UpgradeDeviceCoreApiVersion(uint32_t api_version);
Chia-I Wu4901db72016-03-24 16:38:58 +0800106
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800107 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
108 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800109
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800110 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800111 explicit operator const VkDeviceCreateInfo*() const;
112
113 private:
114 struct ExtensionFilter {
115 VkExtensionProperties* exts;
116 uint32_t ext_count;
117
118 const char** names;
119 uint32_t name_count;
120 };
121
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800122 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800123
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800124 VkResult SanitizeLayers();
125 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800126
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800127 VkResult QueryExtensionCount(uint32_t& count) const;
128 VkResult EnumerateExtensions(uint32_t& count,
129 VkExtensionProperties* props) const;
130 VkResult InitExtensionFilter();
131 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800132
133 const bool is_instance_;
134 const VkAllocationCallbacks& allocator_;
135
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800136 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800137
138 union {
139 VkInstanceCreateInfo instance_info_;
140 VkDeviceCreateInfo dev_info_;
141 };
142
Ian Elliottf3e872d2017-11-02 10:15:13 -0600143 VkApplicationInfo application_info_;
144
Chia-I Wu4901db72016-03-24 16:38:58 +0800145 ExtensionFilter extension_filter_;
146
147 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
148 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
149};
150
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800151Hal Hal::hal_;
152
Jesse Hall53457db2016-12-14 16:54:06 -0800153void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniers03b11cf2019-10-25 11:23:08 -0700154 const char* subname,
155 int subname_len) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800156 ATRACE_CALL();
157
Nick Desaulniers03b11cf2019-10-25 11:23:08 -0700158 const char kLibFormat[] = "vulkan.%*s.so";
159 char* name = static_cast<char*>(
160 alloca(sizeof(kLibFormat) + static_cast<size_t>(subname_len)));
161 sprintf(name, kLibFormat, subname_len, subname);
162 return android_dlopen_ext(name, RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800163}
164
165const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
166 "ro.hardware." HWVULKAN_HARDWARE_MODULE_ID,
167 "ro.board.platform",
168}};
169
Jesse Hall00e61ff2017-04-07 16:48:02 -0700170int LoadDriver(android_namespace_t* library_namespace,
171 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800172 ATRACE_CALL();
173
Jesse Hall53457db2016-12-14 16:54:06 -0800174 const android_dlextinfo dlextinfo = {
175 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700176 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800177 };
Jesse Hall53457db2016-12-14 16:54:06 -0800178 void* so = nullptr;
179 char prop[PROPERTY_VALUE_MAX];
180 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
181 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniers03b11cf2019-10-25 11:23:08 -0700182 if (prop_len > 0) {
183 so = LoadLibrary(dlextinfo, prop, prop_len);
Jesse Hall53457db2016-12-14 16:54:06 -0800184 if (so)
185 break;
186 }
187 }
188 if (!so)
189 return -ENOENT;
190
Jesse Hall00e61ff2017-04-07 16:48:02 -0700191 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800192 if (!hmi) {
193 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
194 dlclose(so);
195 return -EINVAL;
196 }
197 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
198 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
199 dlclose(so);
200 return -EINVAL;
201 }
202 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700203 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800204 return 0;
205}
206
Jesse Hall00e61ff2017-04-07 16:48:02 -0700207int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800208 ATRACE_CALL();
209
Jesse Hall00e61ff2017-04-07 16:48:02 -0700210 auto ns = android_get_exported_namespace("sphal");
211 if (!ns)
212 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800213 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700214 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700215 return LoadDriver(ns, module);
216}
217
218int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800219 ATRACE_CALL();
220
Jesse Hall00e61ff2017-04-07 16:48:02 -0700221 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
222 if (!ns)
223 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800224 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700225 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700226 return LoadDriver(ns, module);
227}
228
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800229bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800230 ATRACE_CALL();
231
Yiwei Zhangd9861812019-02-13 11:51:55 -0800232 const nsecs_t openTime = systemTime();
233
Jesse Halldc225072016-05-30 22:40:14 -0700234 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800235
236 // Use a stub device unless we successfully open a real HAL device.
237 hal_.dev_ = &stubhal::kDevice;
238
Jesse Hall53457db2016-12-14 16:54:06 -0800239 int result;
240 const hwvulkan_module_t* module = nullptr;
241
Jesse Hall00e61ff2017-04-07 16:48:02 -0700242 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800243 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700244 result = LoadBuiltinDriver(&module);
245 if (result != 0) {
246 // -ENOENT means the sphal namespace doesn't exist, not that there
247 // is a problem with the driver.
248 ALOGW_IF(
249 result != -ENOENT,
250 "Failed to load Vulkan driver into sphal namespace. This "
251 "usually means the driver has forbidden library dependencies."
252 "Please fix, this will soon stop working.");
253 result =
254 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
255 reinterpret_cast<const hw_module_t**>(&module));
256 }
Jesse Hall53457db2016-12-14 16:54:06 -0800257 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800258 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800259 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700260 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800261 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800262 return true;
263 }
264
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800265
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800266 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800267 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800268 result =
269 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
270 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800271 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800272 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800273 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700274 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800275 // Any device with a Vulkan HAL should be able to open the device.
276 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
277 result);
278 return false;
279 }
280
281 hal_.dev_ = device;
282
Chia-I Wu31938252016-05-23 15:31:02 +0800283 hal_.InitDebugReportIndex();
284
Yiwei Zhangd9861812019-02-13 11:51:55 -0800285 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700286 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800287
Chia-I Wu31938252016-05-23 15:31:02 +0800288 return true;
289}
290
291bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800292 ATRACE_CALL();
293
Chia-I Wu31938252016-05-23 15:31:02 +0800294 uint32_t count;
295 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
296 VK_SUCCESS) {
297 ALOGE("failed to get HAL instance extension count");
298 return false;
299 }
300
301 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
302 malloc(sizeof(VkExtensionProperties) * count));
303 if (!exts) {
304 ALOGE("failed to allocate HAL instance extension array");
305 return false;
306 }
307
308 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
309 VK_SUCCESS) {
310 ALOGE("failed to enumerate HAL instance extensions");
311 free(exts);
312 return false;
313 }
314
315 for (uint32_t i = 0; i < count; i++) {
316 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
317 0) {
318 debug_report_index_ = static_cast<int>(i);
319 break;
320 }
321 }
322
323 free(exts);
324
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800325 return true;
326}
327
328CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800329 const VkAllocationCallbacks& allocator)
330 : is_instance_(true),
331 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800332 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800333 instance_info_(create_info),
334 extension_filter_() {
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700335 // instance core versions need to match the loader api version
336 for (uint32_t i = ProcHook::EXTENSION_CORE_1_0;
337 i != ProcHook::EXTENSION_COUNT; ++i) {
338 hook_extensions_.set(i);
339 hal_extensions_.set(i);
340 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800341}
342
Chia-I Wu4901db72016-03-24 16:38:58 +0800343CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
344 const VkDeviceCreateInfo& create_info,
345 const VkAllocationCallbacks& allocator)
346 : is_instance_(false),
347 allocator_(allocator),
348 physical_dev_(physical_dev),
349 dev_info_(create_info),
350 extension_filter_() {
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700351 // initialize with baseline core API version
352 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
353 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
Chia-I Wu4901db72016-03-24 16:38:58 +0800354}
355
356CreateInfoWrapper::~CreateInfoWrapper() {
357 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
358 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
359}
360
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800361VkResult CreateInfoWrapper::Validate() {
362 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800363 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800364 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800365 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800366 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800367
368 return result;
369}
370
371const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800372CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800373 return hook_extensions_;
374}
375
376const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800377CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800378 return hal_extensions_;
379}
380
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800381CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
382 return &instance_info_;
383}
384
Chia-I Wu4901db72016-03-24 16:38:58 +0800385CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
386 return &dev_info_;
387}
388
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800389VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800390 const struct StructHeader {
391 VkStructureType type;
392 const void* next;
393 } * header;
394
395 if (is_instance_) {
396 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
397
398 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
399 while (header &&
400 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
401 header = reinterpret_cast<const StructHeader*>(header->next);
402
403 instance_info_.pNext = header;
404 } else {
405 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
406
407 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
408 while (header &&
409 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
410 header = reinterpret_cast<const StructHeader*>(header->next);
411
412 dev_info_.pNext = header;
413 }
414
415 return VK_SUCCESS;
416}
417
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800418VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800419 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
420 : dev_info_.ppEnabledLayerNames;
421 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
422 : dev_info_.enabledLayerCount;
423
424 // remove all layers
425 layer_names = nullptr;
426 layer_count = 0;
427
428 return VK_SUCCESS;
429}
430
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800431VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800432 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
433 : dev_info_.ppEnabledExtensionNames;
434 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
435 : dev_info_.enabledExtensionCount;
436 if (!ext_count)
437 return VK_SUCCESS;
438
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800439 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800440 if (result != VK_SUCCESS)
441 return result;
442
443 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800444 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800445
Jesse Halld3d887a2018-03-05 13:34:45 -0800446 // Enable device extensions that contain physical-device commands, so that
447 // vkGetInstanceProcAddr will return those physical-device commands.
448 if (is_instance_) {
449 hook_extensions_.set(ProcHook::KHR_swapchain);
450 }
451
Chia-I Wu4901db72016-03-24 16:38:58 +0800452 ext_names = extension_filter_.names;
453 ext_count = extension_filter_.name_count;
454
455 return VK_SUCCESS;
456}
457
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800458VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800459 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800460 return Hal::Device().EnumerateInstanceExtensionProperties(
461 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800462 } else {
463 const auto& driver = GetData(physical_dev_).driver;
464 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
465 &count, nullptr);
466 }
467}
468
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800469VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800470 uint32_t& count,
471 VkExtensionProperties* props) const {
472 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800473 return Hal::Device().EnumerateInstanceExtensionProperties(
474 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800475 } else {
476 const auto& driver = GetData(physical_dev_).driver;
477 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
478 &count, props);
479 }
480}
481
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800482VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800483 // query extension count
484 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800485 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800486 if (result != VK_SUCCESS || count == 0)
487 return result;
488
489 auto& filter = extension_filter_;
490 filter.exts =
491 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
492 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
493 alignof(VkExtensionProperties),
494 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
495 if (!filter.exts)
496 return VK_ERROR_OUT_OF_HOST_MEMORY;
497
498 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800499 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800500 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
501 return result;
502
503 if (!count)
504 return VK_SUCCESS;
505
506 filter.ext_count = count;
507
508 // allocate name array
509 uint32_t enabled_ext_count = (is_instance_)
510 ? instance_info_.enabledExtensionCount
511 : dev_info_.enabledExtensionCount;
512 count = std::min(filter.ext_count, enabled_ext_count);
513 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
514 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
515 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
516 if (!filter.names)
517 return VK_ERROR_OUT_OF_HOST_MEMORY;
518
519 return VK_SUCCESS;
520}
521
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800522void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800523 auto& filter = extension_filter_;
524
525 ProcHook::Extension ext_bit = GetProcHookExtension(name);
526 if (is_instance_) {
527 switch (ext_bit) {
528 case ProcHook::KHR_android_surface:
529 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700530 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300531 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800532 hook_extensions_.set(ext_bit);
533 // return now as these extensions do not require HAL support
534 return;
535 case ProcHook::EXT_debug_report:
536 // both we and HAL can take part in
537 hook_extensions_.set(ext_bit);
538 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300539 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700540 case ProcHook::EXTENSION_UNKNOWN:
541 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800542 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700543
Yiwei Zhang23143102019-04-10 18:24:05 -0700544 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700545 case ProcHook::KHR_incremental_present:
546 case ProcHook::KHR_shared_presentable_image:
547 case ProcHook::KHR_swapchain:
548 case ProcHook::EXT_hdr_metadata:
549 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
550 case ProcHook::ANDROID_native_buffer:
551 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700552 case ProcHook::EXTENSION_CORE_1_0:
553 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700554 case ProcHook::EXTENSION_COUNT:
555 // Device and meta extensions. If we ever get here it's a bug in
556 // our code. But enumerating them lets us avoid having a default
557 // case, and default hides other bugs.
558 ALOGE(
559 "CreateInfoWrapper::FilterExtension: invalid instance "
560 "extension '%s'. FIX ME",
561 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800562 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700563
564 // Don't use a default case. Without it, -Wswitch will tell us
565 // at compile time if someone adds a new ProcHook extension but
566 // doesn't handle it above. That's a real bug that has
567 // not-immediately-obvious effects.
568 //
569 // default:
570 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800571 }
572 } else {
573 switch (ext_bit) {
574 case ProcHook::KHR_swapchain:
575 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
576 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
577 ext_bit = ProcHook::ANDROID_native_buffer;
578 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700579 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700580 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300581 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700582 hook_extensions_.set(ext_bit);
583 // return now as these extensions do not require HAL support
584 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700585 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700586 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700587 hook_extensions_.set(ext_bit);
588 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700589 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800590 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700591 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800592 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700593
594 case ProcHook::KHR_android_surface:
595 case ProcHook::KHR_get_physical_device_properties2:
596 case ProcHook::KHR_get_surface_capabilities2:
597 case ProcHook::KHR_surface:
598 case ProcHook::EXT_debug_report:
599 case ProcHook::EXT_swapchain_colorspace:
600 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700601 case ProcHook::EXTENSION_CORE_1_0:
602 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700603 case ProcHook::EXTENSION_COUNT:
604 // Instance and meta extensions. If we ever get here it's a bug
605 // in our code. But enumerating them lets us avoid having a
606 // default case, and default hides other bugs.
607 ALOGE(
608 "CreateInfoWrapper::FilterExtension: invalid device "
609 "extension '%s'. FIX ME",
610 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800611 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700612
613 // Don't use a default case. Without it, -Wswitch will tell us
614 // at compile time if someone adds a new ProcHook extension but
615 // doesn't handle it above. That's a real bug that has
616 // not-immediately-obvious effects.
617 //
618 // default:
619 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800620 }
621 }
622
623 for (uint32_t i = 0; i < filter.ext_count; i++) {
624 const VkExtensionProperties& props = filter.exts[i];
625 // ignore unknown extensions
626 if (strcmp(name, props.extensionName) != 0)
627 continue;
628
Chia-I Wu4901db72016-03-24 16:38:58 +0800629 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800630 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
631 if (ext_bit == ProcHook::ANDROID_native_buffer)
632 hook_extensions_.set(ProcHook::KHR_swapchain);
633
634 hal_extensions_.set(ext_bit);
635 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800636
637 break;
638 }
639}
640
Ian Elliottf3e872d2017-11-02 10:15:13 -0600641void CreateInfoWrapper::DowngradeApiVersion() {
642 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
643 if (instance_info_.pApplicationInfo) {
644 application_info_ = *instance_info_.pApplicationInfo;
645 instance_info_.pApplicationInfo = &application_info_;
646 application_info_.apiVersion = VK_API_VERSION_1_0;
647 }
648}
649
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700650void CreateInfoWrapper::UpgradeDeviceCoreApiVersion(uint32_t api_version) {
651 ALOG_ASSERT(!is_instance_, "Device only API called by instance wrapper.");
652
653#pragma clang diagnostic push
654#pragma clang diagnostic ignored "-Wold-style-cast"
655 api_version ^= VK_VERSION_PATCH(api_version);
656#pragma clang diagnostic pop
657
658 // cap the API version to the loader supported highest version
659 if (api_version > VK_API_VERSION_1_1)
660 api_version = VK_API_VERSION_1_1;
661
662 switch (api_version) {
663 case VK_API_VERSION_1_1:
664 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
665 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
666 [[clang::fallthrough]];
667 case VK_API_VERSION_1_0:
668 break;
669 default:
670 ALOGD("Unknown upgrade API version[%u]", api_version);
671 break;
672 }
673}
674
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800675VKAPI_ATTR void* DefaultAllocate(void*,
676 size_t size,
677 size_t alignment,
678 VkSystemAllocationScope) {
679 void* ptr = nullptr;
680 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
681 // additionally requires that it be at least sizeof(void*).
682 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
683 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
684 ret, ptr);
685 return ret == 0 ? ptr : nullptr;
686}
687
688VKAPI_ATTR void* DefaultReallocate(void*,
689 void* ptr,
690 size_t size,
691 size_t alignment,
692 VkSystemAllocationScope) {
693 if (size == 0) {
694 free(ptr);
695 return nullptr;
696 }
697
Yiwei Zhanga885c062019-10-24 12:07:57 -0700698 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800699 // request is smaller than the existing chunk, we just continue using it.
700 // Right now the loader never reallocs, so this doesn't matter. If that
701 // changes, or if this code is copied into some other project, this should
702 // probably have a heuristic to allocate-copy-free when doing so will save
703 // "enough" space.
704 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
705 if (size <= old_size)
706 return ptr;
707
708 void* new_ptr = nullptr;
709 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
710 return nullptr;
711 if (ptr) {
712 memcpy(new_ptr, ptr, std::min(old_size, size));
713 free(ptr);
714 }
715 return new_ptr;
716}
717
718VKAPI_ATTR void DefaultFree(void*, void* ptr) {
719 ALOGD_CALLSTACK("Free: %p", ptr);
720 free(ptr);
721}
722
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800723InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
724 void* data_mem = allocator.pfnAllocation(
725 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
726 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
727 if (!data_mem)
728 return nullptr;
729
730 return new (data_mem) InstanceData(allocator);
731}
732
733void FreeInstanceData(InstanceData* data,
734 const VkAllocationCallbacks& allocator) {
735 data->~InstanceData();
736 allocator.pfnFree(allocator.pUserData, data);
737}
738
Chia-I Wu950d6e12016-05-03 09:12:35 +0800739DeviceData* AllocateDeviceData(
740 const VkAllocationCallbacks& allocator,
741 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800742 void* data_mem = allocator.pfnAllocation(
743 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
744 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
745 if (!data_mem)
746 return nullptr;
747
Chia-I Wu950d6e12016-05-03 09:12:35 +0800748 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800749}
750
751void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
752 data->~DeviceData();
753 allocator.pfnFree(allocator.pUserData, data);
754}
755
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800756} // anonymous namespace
757
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800758bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800759 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800760}
761
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800762const VkAllocationCallbacks& GetDefaultAllocator() {
763 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
764 .pUserData = nullptr,
765 .pfnAllocation = DefaultAllocate,
766 .pfnReallocation = DefaultReallocate,
767 .pfnFree = DefaultFree,
768 };
769
770 return kDefaultAllocCallbacks;
771}
772
Chia-I Wueb7db122016-03-24 09:11:06 +0800773PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
774 const ProcHook* hook = GetProcHook(pName);
775 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800776 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800777
778 if (!instance) {
779 if (hook->type == ProcHook::GLOBAL)
780 return hook->proc;
781
Chia-I Wu109f8982016-04-22 06:40:40 +0800782 // v0 layers expect
783 //
784 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
785 //
786 // to work.
787 if (strcmp(pName, "vkCreateDevice") == 0)
788 return hook->proc;
789
Chia-I Wueb7db122016-03-24 09:11:06 +0800790 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800791 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800792 pName);
793
Chia-I Wu109f8982016-04-22 06:40:40 +0800794 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800795 }
796
797 PFN_vkVoidFunction proc;
798
799 switch (hook->type) {
800 case ProcHook::INSTANCE:
801 proc = (GetData(instance).hook_extensions[hook->extension])
802 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800803 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800804 break;
805 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700806 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800807 ? hook->proc
808 : hook->checked_proc;
809 break;
810 default:
811 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800812 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800813 pName);
814 proc = nullptr;
815 break;
816 }
817
818 return proc;
819}
820
821PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
822 const ProcHook* hook = GetProcHook(pName);
823 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800824 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800825
826 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800827 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800828 return nullptr;
829 }
830
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800831 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
832 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800833}
834
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800835VkResult EnumerateInstanceExtensionProperties(
836 const char* pLayerName,
837 uint32_t* pPropertyCount,
838 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700839 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600840 loader_extensions.push_back({
841 VK_KHR_SURFACE_EXTENSION_NAME,
842 VK_KHR_SURFACE_SPEC_VERSION});
843 loader_extensions.push_back({
844 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
845 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
846 loader_extensions.push_back({
847 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
848 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700849 loader_extensions.push_back({
850 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
851 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600852
Chia-I Wu31938252016-05-23 15:31:02 +0800853 static const VkExtensionProperties loader_debug_report_extension = {
854 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
855 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800856
857 // enumerate our extensions first
858 if (!pLayerName && pProperties) {
859 uint32_t count = std::min(
860 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
861
Yiwei Zhang5e862202019-06-21 14:59:16 -0700862 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800863
864 if (count < loader_extensions.size()) {
865 *pPropertyCount = count;
866 return VK_INCOMPLETE;
867 }
868
869 pProperties += count;
870 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800871
872 if (Hal::Get().GetDebugReportIndex() < 0) {
873 if (!*pPropertyCount) {
874 *pPropertyCount = count;
875 return VK_INCOMPLETE;
876 }
877
878 pProperties[0] = loader_debug_report_extension;
879 pProperties += 1;
880 *pPropertyCount -= 1;
881 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800882 }
883
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800884 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800885 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800886 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800887 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800888
Chia-I Wu31938252016-05-23 15:31:02 +0800889 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
890 int idx = Hal::Get().GetDebugReportIndex();
891 if (idx < 0) {
892 *pPropertyCount += 1;
893 } else if (pProperties &&
894 static_cast<uint32_t>(idx) < *pPropertyCount) {
895 pProperties[idx].specVersion =
896 std::min(pProperties[idx].specVersion,
897 loader_debug_report_extension.specVersion);
898 }
899
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800900 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800901 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800902
903 return result;
904}
905
Chris Forbesfa25e632017-02-22 12:36:02 +1300906bool QueryPresentationProperties(
907 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700908 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300909 const InstanceData& data = GetData(physicalDevice);
910
911 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700912 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
913 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300914 return false;
915
916 // Request the android-specific presentation properties via GPDP2
917 VkPhysicalDeviceProperties2KHR properties = {
918 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
919 presentation_properties,
920 {}
921 };
922
923#pragma clang diagnostic push
924#pragma clang diagnostic ignored "-Wold-style-cast"
925 presentation_properties->sType =
926 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
927#pragma clang diagnostic pop
928 presentation_properties->pNext = nullptr;
929 presentation_properties->sharedImage = VK_FALSE;
930
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700931 if (data.driver.GetPhysicalDeviceProperties2KHR) {
932 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
933 &properties);
934 } else {
935 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
936 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300937
938 return true;
939}
940
Chia-I Wu01cf3052016-03-24 16:16:21 +0800941VkResult EnumerateDeviceExtensionProperties(
942 VkPhysicalDevice physicalDevice,
943 const char* pLayerName,
944 uint32_t* pPropertyCount,
945 VkExtensionProperties* pProperties) {
946 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300947 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700948 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300949 loader_extensions.push_back({
950 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
951 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300952
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800953 bool hdrBoardConfig =
954 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(
955 false);
956 if (hdrBoardConfig) {
957 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
958 VK_EXT_HDR_METADATA_SPEC_VERSION});
959 }
960
Chris Forbes16095002017-05-05 15:33:29 -0700961 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
962 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
963 presentation_properties.sharedImage) {
964 loader_extensions.push_back({
965 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
966 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300967 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700968
Ian Elliott5c34de22017-04-10 14:42:30 -0600969 // conditionally add VK_GOOGLE_display_timing if present timestamps are
970 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700971 const std::string timestamp_property("service.sf.present_timestamp");
972 android::base::WaitForPropertyCreation(timestamp_property);
973 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600974 loader_extensions.push_back({
975 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
976 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
977 }
978
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700979 // enumerate our extensions first
980 if (!pLayerName && pProperties) {
981 uint32_t count = std::min(
982 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
983
Yiwei Zhang5e862202019-06-21 14:59:16 -0700984 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700985
986 if (count < loader_extensions.size()) {
987 *pPropertyCount = count;
988 return VK_INCOMPLETE;
989 }
990
991 pProperties += count;
992 *pPropertyCount -= count;
993 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800994
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800995 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +0800996 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
997 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800998 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800999
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001000 if (pProperties) {
1001 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1002 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1003 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001004
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001005 if (strcmp(prop.extensionName,
1006 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1007 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001008
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001009 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1010 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001011
1012 if (prop.specVersion >= 8) {
1013 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1014 } else {
1015 prop.specVersion = 68;
1016 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001017 }
1018 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001019
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001020 // restore loader extension count
1021 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1022 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001023 }
1024
1025 return result;
1026}
1027
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001028VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1029 const VkAllocationCallbacks* pAllocator,
1030 VkInstance* pInstance) {
1031 const VkAllocationCallbacks& data_allocator =
1032 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1033
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001034 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001035 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001036 if (result != VK_SUCCESS)
1037 return result;
1038
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001039 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001040 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001041 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001042 if (!data)
1043 return VK_ERROR_OUT_OF_HOST_MEMORY;
1044
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001045 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001046
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001047 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001048#pragma clang diagnostic push
1049#pragma clang diagnostic ignored "-Wold-style-cast"
1050 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1051 ? pCreateInfo->pApplicationInfo->apiVersion
1052 : VK_API_VERSION_1_0);
1053 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1054 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1055 uint32_t icd_api_version;
1056 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1057 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001058 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001059 "vkEnumerateInstanceVersion"));
1060 if (!pfn_enumerate_instance_version) {
1061 icd_api_version = VK_API_VERSION_1_0;
1062 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001063 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001064 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001065 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001066 }
1067 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1068 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1069
1070 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1071 ((api_major_version > 1) || (api_minor_version > 0))) {
1072 api_version = VK_API_VERSION_1_0;
1073 wrapper.DowngradeApiVersion();
1074 }
1075#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001076 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001077
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001078 // call into the driver
1079 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001080 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001081 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001082 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1083 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001084 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001085 if (result != VK_SUCCESS) {
1086 FreeInstanceData(data, data_allocator);
1087 return result;
1088 }
1089
1090 // initialize InstanceDriverTable
1091 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001092 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001093 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001094 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001095 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001096 if (data->driver.DestroyInstance)
1097 data->driver.DestroyInstance(instance, pAllocator);
1098
1099 FreeInstanceData(data, data_allocator);
1100
1101 return VK_ERROR_INCOMPATIBLE_DRIVER;
1102 }
1103
1104 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001105 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001106 if (!data->get_device_proc_addr) {
1107 data->driver.DestroyInstance(instance, pAllocator);
1108 FreeInstanceData(data, data_allocator);
1109
1110 return VK_ERROR_INCOMPATIBLE_DRIVER;
1111 }
1112
1113 *pInstance = instance;
1114
1115 return VK_SUCCESS;
1116}
1117
1118void DestroyInstance(VkInstance instance,
1119 const VkAllocationCallbacks* pAllocator) {
1120 InstanceData& data = GetData(instance);
1121 data.driver.DestroyInstance(instance, pAllocator);
1122
1123 VkAllocationCallbacks local_allocator;
1124 if (!pAllocator) {
1125 local_allocator = data.allocator;
1126 pAllocator = &local_allocator;
1127 }
1128
1129 FreeInstanceData(&data, *pAllocator);
1130}
1131
Chia-I Wu4901db72016-03-24 16:38:58 +08001132VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1133 const VkDeviceCreateInfo* pCreateInfo,
1134 const VkAllocationCallbacks* pAllocator,
1135 VkDevice* pDevice) {
1136 const InstanceData& instance_data = GetData(physicalDevice);
1137 const VkAllocationCallbacks& data_allocator =
1138 (pAllocator) ? *pAllocator : instance_data.allocator;
1139
1140 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001141 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001142 if (result != VK_SUCCESS)
1143 return result;
1144
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001145 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001146 DeviceData* data = AllocateDeviceData(data_allocator,
1147 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001148 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001149 if (!data)
1150 return VK_ERROR_OUT_OF_HOST_MEMORY;
1151
Yiwei Zhang7cc36a52019-10-11 19:02:09 -07001152 VkPhysicalDeviceProperties properties;
1153 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1154 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1155 &properties);
1156 ATRACE_END();
1157
1158 wrapper.UpgradeDeviceCoreApiVersion(properties.apiVersion);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001159 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001160
1161 // call into the driver
1162 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001163 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001164 result = instance_data.driver.CreateDevice(
1165 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1166 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001167 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001168 if (result != VK_SUCCESS) {
1169 FreeDeviceData(data, data_allocator);
1170 return result;
1171 }
1172
1173 // initialize DeviceDriverTable
1174 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001175 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1176 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001177 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1178 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1179 if (data->driver.DestroyDevice)
1180 data->driver.DestroyDevice(dev, pAllocator);
1181
1182 FreeDeviceData(data, data_allocator);
1183
1184 return VK_ERROR_INCOMPATIBLE_DRIVER;
1185 }
Chris Forbesd8277912017-02-10 14:59:59 +13001186
1187 // sanity check ANDROID_native_buffer implementation, whose set of
1188 // entrypoints varies according to the spec version.
1189 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1190 !data->driver.GetSwapchainGrallocUsageANDROID &&
1191 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1192 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1193 " must expose at least one of "
1194 "vkGetSwapchainGrallocUsageANDROID or "
1195 "vkGetSwapchainGrallocUsage2ANDROID");
1196
1197 data->driver.DestroyDevice(dev, pAllocator);
1198 FreeDeviceData(data, data_allocator);
1199
1200 return VK_ERROR_INCOMPATIBLE_DRIVER;
1201 }
1202
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001203 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1204 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangbcba4112019-07-03 13:39:32 -07001205 android::GraphicsEnv::getInstance().setTargetStats(
1206 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001207 }
1208
Jesse Halldc225072016-05-30 22:40:14 -07001209 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001210
1211 *pDevice = dev;
1212
1213 return VK_SUCCESS;
1214}
1215
1216void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1217 DeviceData& data = GetData(device);
1218 data.driver.DestroyDevice(device, pAllocator);
1219
1220 VkAllocationCallbacks local_allocator;
1221 if (!pAllocator) {
1222 local_allocator = data.allocator;
1223 pAllocator = &local_allocator;
1224 }
1225
1226 FreeDeviceData(&data, *pAllocator);
1227}
1228
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001229VkResult EnumeratePhysicalDevices(VkInstance instance,
1230 uint32_t* pPhysicalDeviceCount,
1231 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001232 ATRACE_CALL();
1233
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001234 const auto& data = GetData(instance);
1235
1236 VkResult result = data.driver.EnumeratePhysicalDevices(
1237 instance, pPhysicalDeviceCount, pPhysicalDevices);
1238 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1239 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1240 SetData(pPhysicalDevices[i], data);
1241 }
1242
1243 return result;
1244}
1245
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001246VkResult EnumeratePhysicalDeviceGroups(
1247 VkInstance instance,
1248 uint32_t* pPhysicalDeviceGroupCount,
1249 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001250 ATRACE_CALL();
1251
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001252 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001253 const auto& data = GetData(instance);
1254
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001255 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1256 uint32_t device_count = 0;
1257 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1258 if (result < 0)
1259 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001260
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001261 if (!pPhysicalDeviceGroupProperties) {
1262 *pPhysicalDeviceGroupCount = device_count;
1263 return result;
1264 }
1265
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001266 if (!device_count) {
1267 *pPhysicalDeviceGroupCount = 0;
1268 return result;
1269 }
Chad Versace32c087f2018-09-09 07:28:05 -07001270 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1271 if (!device_count)
1272 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001273
Yiwei Zhang5e862202019-06-21 14:59:16 -07001274 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001275 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001276 result =
1277 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001278 if (result < 0)
1279 return result;
1280
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001281 for (uint32_t i = 0; i < device_count; ++i) {
1282 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1283 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1284 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1285 }
1286 } else {
1287 result = data.driver.EnumeratePhysicalDeviceGroups(
1288 instance, pPhysicalDeviceGroupCount,
1289 pPhysicalDeviceGroupProperties);
1290 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1291 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1292 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1293 for (uint32_t j = 0;
1294 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1295 j++) {
1296 SetData(
1297 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001298 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001299 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001300 }
1301 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001302 }
1303
1304 return result;
1305}
1306
Chia-I Wuba0be412016-03-24 16:24:40 +08001307void GetDeviceQueue(VkDevice device,
1308 uint32_t queueFamilyIndex,
1309 uint32_t queueIndex,
1310 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001311 ATRACE_CALL();
1312
Chia-I Wuba0be412016-03-24 16:24:40 +08001313 const auto& data = GetData(device);
1314
1315 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1316 SetData(*pQueue, data);
1317}
1318
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001319void GetDeviceQueue2(VkDevice device,
1320 const VkDeviceQueueInfo2* pQueueInfo,
1321 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001322 ATRACE_CALL();
1323
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001324 const auto& data = GetData(device);
1325
1326 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001327 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001328}
1329
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001330VKAPI_ATTR VkResult
1331AllocateCommandBuffers(VkDevice device,
1332 const VkCommandBufferAllocateInfo* pAllocateInfo,
1333 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001334 ATRACE_CALL();
1335
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001336 const auto& data = GetData(device);
1337
1338 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1339 pCommandBuffers);
1340 if (result == VK_SUCCESS) {
1341 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1342 SetData(pCommandBuffers[i], data);
1343 }
1344
1345 return result;
1346}
1347
Yiwei Zhang899d1752019-09-23 16:05:35 -07001348VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1349 uint32_t submitCount,
1350 const VkSubmitInfo* pSubmits,
1351 VkFence fence) {
1352 ATRACE_CALL();
1353
1354 const auto& data = GetData(queue);
1355
1356 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1357}
1358
Chia-I Wu9d518162016-03-24 14:55:27 +08001359} // namespace driver
1360} // namespace vulkan