blob: 733e823bed506191478962f3ea479f979e7d7680 [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>
40#include <new>
41#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070042
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070043#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080044
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080045using namespace android::hardware::configstore;
46using namespace android::hardware::configstore::V1_0;
47
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080048// #define ENABLE_ALLOC_CALLSTACKS 1
49#if ENABLE_ALLOC_CALLSTACKS
50#include <utils/CallStack.h>
51#define ALOGD_CALLSTACK(...) \
52 do { \
53 ALOGD(__VA_ARGS__); \
54 android::CallStack callstack; \
55 callstack.update(); \
56 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
57 } while (false)
58#else
59#define ALOGD_CALLSTACK(...) \
60 do { \
61 } while (false)
62#endif
63
Chia-I Wu9d518162016-03-24 14:55:27 +080064namespace vulkan {
65namespace driver {
66
Chia-I Wu136b8eb2016-03-24 15:01:52 +080067namespace {
68
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080069class Hal {
70 public:
71 static bool Open();
72
73 static const Hal& Get() { return hal_; }
74 static const hwvulkan_device_t& Device() { return *Get().dev_; }
75
Chia-I Wu31938252016-05-23 15:31:02 +080076 int GetDebugReportIndex() const { return debug_report_index_; }
77
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080078 private:
Chia-I Wu31938252016-05-23 15:31:02 +080079 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080080 Hal(const Hal&) = delete;
81 Hal& operator=(const Hal&) = delete;
82
Chia-I Wu31938252016-05-23 15:31:02 +080083 bool InitDebugReportIndex();
84
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080085 static Hal hal_;
86
87 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080088 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080089};
90
Chia-I Wu4901db72016-03-24 16:38:58 +080091class CreateInfoWrapper {
92 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080093 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080094 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080095 CreateInfoWrapper(VkPhysicalDevice physical_dev,
96 const VkDeviceCreateInfo& create_info,
97 const VkAllocationCallbacks& allocator);
98 ~CreateInfoWrapper();
99
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800100 VkResult Validate();
Ian Elliottf3e872d2017-11-02 10:15:13 -0600101 void DowngradeApiVersion();
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700102 void UpgradeDeviceCoreApiVersion(uint32_t api_version);
Chia-I Wu4901db72016-03-24 16:38:58 +0800103
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800104 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
105 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800106
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800107 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800108 explicit operator const VkDeviceCreateInfo*() const;
109
110 private:
111 struct ExtensionFilter {
112 VkExtensionProperties* exts;
113 uint32_t ext_count;
114
115 const char** names;
116 uint32_t name_count;
117 };
118
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800119 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800120
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800121 VkResult SanitizeLayers();
122 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800123
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800124 VkResult QueryExtensionCount(uint32_t& count) const;
125 VkResult EnumerateExtensions(uint32_t& count,
126 VkExtensionProperties* props) const;
127 VkResult InitExtensionFilter();
128 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800129
130 const bool is_instance_;
131 const VkAllocationCallbacks& allocator_;
132
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800133 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800134
135 union {
136 VkInstanceCreateInfo instance_info_;
137 VkDeviceCreateInfo dev_info_;
138 };
139
Ian Elliottf3e872d2017-11-02 10:15:13 -0600140 VkApplicationInfo application_info_;
141
Chia-I Wu4901db72016-03-24 16:38:58 +0800142 ExtensionFilter extension_filter_;
143
144 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
145 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
146};
147
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800148Hal Hal::hal_;
149
Jesse Hall53457db2016-12-14 16:54:06 -0800150void* LoadLibrary(const android_dlextinfo& dlextinfo,
151 const char* subname,
152 int subname_len) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800153 ATRACE_CALL();
154
Jesse Hall53457db2016-12-14 16:54:06 -0800155 const char kLibFormat[] = "vulkan.%*s.so";
156 char* name = static_cast<char*>(
157 alloca(sizeof(kLibFormat) + static_cast<size_t>(subname_len)));
158 sprintf(name, kLibFormat, subname_len, subname);
159 return android_dlopen_ext(name, RTLD_LOCAL | RTLD_NOW, &dlextinfo);
160}
161
162const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
163 "ro.hardware." HWVULKAN_HARDWARE_MODULE_ID,
164 "ro.board.platform",
165}};
166
Jesse Hall00e61ff2017-04-07 16:48:02 -0700167int LoadDriver(android_namespace_t* library_namespace,
168 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800169 ATRACE_CALL();
170
Jesse Hall53457db2016-12-14 16:54:06 -0800171 const android_dlextinfo dlextinfo = {
172 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700173 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800174 };
Jesse Hall53457db2016-12-14 16:54:06 -0800175 void* so = nullptr;
176 char prop[PROPERTY_VALUE_MAX];
177 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
178 int prop_len = property_get(key, prop, nullptr);
179 if (prop_len > 0) {
180 so = LoadLibrary(dlextinfo, prop, prop_len);
181 if (so)
182 break;
183 }
184 }
185 if (!so)
186 return -ENOENT;
187
Jesse Hall00e61ff2017-04-07 16:48:02 -0700188 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800189 if (!hmi) {
190 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
191 dlclose(so);
192 return -EINVAL;
193 }
194 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
195 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
196 dlclose(so);
197 return -EINVAL;
198 }
199 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700200 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800201 return 0;
202}
203
Jesse Hall00e61ff2017-04-07 16:48:02 -0700204int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800205 ATRACE_CALL();
206
Jesse Hall00e61ff2017-04-07 16:48:02 -0700207 auto ns = android_get_exported_namespace("sphal");
208 if (!ns)
209 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800210 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700211 android::GpuStatsInfo::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700212 return LoadDriver(ns, module);
213}
214
215int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800216 ATRACE_CALL();
217
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
219 if (!ns)
220 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800221 android::GraphicsEnv::getInstance().setDriverToLoad(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700222 android::GpuStatsInfo::Driver::VULKAN_UPDATED);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700223 return LoadDriver(ns, module);
224}
225
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800226bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800227 ATRACE_CALL();
228
Yiwei Zhangd9861812019-02-13 11:51:55 -0800229 const nsecs_t openTime = systemTime();
230
Jesse Halldc225072016-05-30 22:40:14 -0700231 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800232
233 // Use a stub device unless we successfully open a real HAL device.
234 hal_.dev_ = &stubhal::kDevice;
235
Jesse Hall53457db2016-12-14 16:54:06 -0800236 int result;
237 const hwvulkan_module_t* module = nullptr;
238
Jesse Hall00e61ff2017-04-07 16:48:02 -0700239 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800240 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700241 result = LoadBuiltinDriver(&module);
242 if (result != 0) {
243 // -ENOENT means the sphal namespace doesn't exist, not that there
244 // is a problem with the driver.
245 ALOGW_IF(
246 result != -ENOENT,
247 "Failed to load Vulkan driver into sphal namespace. This "
248 "usually means the driver has forbidden library dependencies."
249 "Please fix, this will soon stop working.");
250 result =
251 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
252 reinterpret_cast<const hw_module_t**>(&module));
253 }
Jesse Hall53457db2016-12-14 16:54:06 -0800254 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800255 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800256 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700257 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800258 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800259 return true;
260 }
261
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800262
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800263 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800264 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800265 result =
266 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
267 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800268 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800269 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800270 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700271 android::GpuStatsInfo::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800272 // Any device with a Vulkan HAL should be able to open the device.
273 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
274 result);
275 return false;
276 }
277
278 hal_.dev_ = device;
279
Chia-I Wu31938252016-05-23 15:31:02 +0800280 hal_.InitDebugReportIndex();
281
Yiwei Zhangd9861812019-02-13 11:51:55 -0800282 android::GraphicsEnv::getInstance().setDriverLoaded(
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700283 android::GpuStatsInfo::Api::API_VK, true, systemTime() - openTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800284
Chia-I Wu31938252016-05-23 15:31:02 +0800285 return true;
286}
287
288bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800289 ATRACE_CALL();
290
Chia-I Wu31938252016-05-23 15:31:02 +0800291 uint32_t count;
292 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
293 VK_SUCCESS) {
294 ALOGE("failed to get HAL instance extension count");
295 return false;
296 }
297
298 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
299 malloc(sizeof(VkExtensionProperties) * count));
300 if (!exts) {
301 ALOGE("failed to allocate HAL instance extension array");
302 return false;
303 }
304
305 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
306 VK_SUCCESS) {
307 ALOGE("failed to enumerate HAL instance extensions");
308 free(exts);
309 return false;
310 }
311
312 for (uint32_t i = 0; i < count; i++) {
313 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
314 0) {
315 debug_report_index_ = static_cast<int>(i);
316 break;
317 }
318 }
319
320 free(exts);
321
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800322 return true;
323}
324
325CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800326 const VkAllocationCallbacks& allocator)
327 : is_instance_(true),
328 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800329 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800330 instance_info_(create_info),
331 extension_filter_() {
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700332 // instance core versions need to match the loader api version
333 for (uint32_t i = ProcHook::EXTENSION_CORE_1_0;
334 i != ProcHook::EXTENSION_COUNT; ++i) {
335 hook_extensions_.set(i);
336 hal_extensions_.set(i);
337 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800338}
339
Chia-I Wu4901db72016-03-24 16:38:58 +0800340CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
341 const VkDeviceCreateInfo& create_info,
342 const VkAllocationCallbacks& allocator)
343 : is_instance_(false),
344 allocator_(allocator),
345 physical_dev_(physical_dev),
346 dev_info_(create_info),
347 extension_filter_() {
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700348 // initialize with baseline core API version
349 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
350 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
Chia-I Wu4901db72016-03-24 16:38:58 +0800351}
352
353CreateInfoWrapper::~CreateInfoWrapper() {
354 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
355 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
356}
357
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800358VkResult CreateInfoWrapper::Validate() {
359 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800360 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800361 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800362 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800363 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800364
365 return result;
366}
367
368const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800369CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800370 return hook_extensions_;
371}
372
373const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800374CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800375 return hal_extensions_;
376}
377
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800378CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
379 return &instance_info_;
380}
381
Chia-I Wu4901db72016-03-24 16:38:58 +0800382CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
383 return &dev_info_;
384}
385
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800386VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800387 const struct StructHeader {
388 VkStructureType type;
389 const void* next;
390 } * header;
391
392 if (is_instance_) {
393 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
394
395 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
396 while (header &&
397 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
398 header = reinterpret_cast<const StructHeader*>(header->next);
399
400 instance_info_.pNext = header;
401 } else {
402 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
403
404 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
405 while (header &&
406 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
407 header = reinterpret_cast<const StructHeader*>(header->next);
408
409 dev_info_.pNext = header;
410 }
411
412 return VK_SUCCESS;
413}
414
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800415VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800416 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
417 : dev_info_.ppEnabledLayerNames;
418 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
419 : dev_info_.enabledLayerCount;
420
421 // remove all layers
422 layer_names = nullptr;
423 layer_count = 0;
424
425 return VK_SUCCESS;
426}
427
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800428VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800429 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
430 : dev_info_.ppEnabledExtensionNames;
431 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
432 : dev_info_.enabledExtensionCount;
433 if (!ext_count)
434 return VK_SUCCESS;
435
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800436 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800437 if (result != VK_SUCCESS)
438 return result;
439
440 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800441 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800442
Jesse Halld3d887a2018-03-05 13:34:45 -0800443 // Enable device extensions that contain physical-device commands, so that
444 // vkGetInstanceProcAddr will return those physical-device commands.
445 if (is_instance_) {
446 hook_extensions_.set(ProcHook::KHR_swapchain);
447 }
448
Chia-I Wu4901db72016-03-24 16:38:58 +0800449 ext_names = extension_filter_.names;
450 ext_count = extension_filter_.name_count;
451
452 return VK_SUCCESS;
453}
454
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800455VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800456 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800457 return Hal::Device().EnumerateInstanceExtensionProperties(
458 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800459 } else {
460 const auto& driver = GetData(physical_dev_).driver;
461 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
462 &count, nullptr);
463 }
464}
465
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800466VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800467 uint32_t& count,
468 VkExtensionProperties* props) const {
469 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800470 return Hal::Device().EnumerateInstanceExtensionProperties(
471 nullptr, &count, props);
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, props);
476 }
477}
478
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800479VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800480 // query extension count
481 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800482 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800483 if (result != VK_SUCCESS || count == 0)
484 return result;
485
486 auto& filter = extension_filter_;
487 filter.exts =
488 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
489 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
490 alignof(VkExtensionProperties),
491 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
492 if (!filter.exts)
493 return VK_ERROR_OUT_OF_HOST_MEMORY;
494
495 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800496 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800497 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
498 return result;
499
500 if (!count)
501 return VK_SUCCESS;
502
503 filter.ext_count = count;
504
505 // allocate name array
506 uint32_t enabled_ext_count = (is_instance_)
507 ? instance_info_.enabledExtensionCount
508 : dev_info_.enabledExtensionCount;
509 count = std::min(filter.ext_count, enabled_ext_count);
510 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
511 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
512 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
513 if (!filter.names)
514 return VK_ERROR_OUT_OF_HOST_MEMORY;
515
516 return VK_SUCCESS;
517}
518
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800519void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800520 auto& filter = extension_filter_;
521
522 ProcHook::Extension ext_bit = GetProcHookExtension(name);
523 if (is_instance_) {
524 switch (ext_bit) {
525 case ProcHook::KHR_android_surface:
526 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700527 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300528 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800529 hook_extensions_.set(ext_bit);
530 // return now as these extensions do not require HAL support
531 return;
532 case ProcHook::EXT_debug_report:
533 // both we and HAL can take part in
534 hook_extensions_.set(ext_bit);
535 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300536 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700537 case ProcHook::EXTENSION_UNKNOWN:
538 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800539 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700540
Yiwei Zhang23143102019-04-10 18:24:05 -0700541 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700542 case ProcHook::KHR_incremental_present:
543 case ProcHook::KHR_shared_presentable_image:
544 case ProcHook::KHR_swapchain:
545 case ProcHook::EXT_hdr_metadata:
546 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
547 case ProcHook::ANDROID_native_buffer:
548 case ProcHook::GOOGLE_display_timing:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700549 case ProcHook::EXTENSION_CORE_1_0:
550 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700551 case ProcHook::EXTENSION_COUNT:
552 // Device and meta extensions. If we ever get here it's a bug in
553 // our code. But enumerating them lets us avoid having a default
554 // case, and default hides other bugs.
555 ALOGE(
556 "CreateInfoWrapper::FilterExtension: invalid instance "
557 "extension '%s'. FIX ME",
558 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800559 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700560
561 // Don't use a default case. Without it, -Wswitch will tell us
562 // at compile time if someone adds a new ProcHook extension but
563 // doesn't handle it above. That's a real bug that has
564 // not-immediately-obvious effects.
565 //
566 // default:
567 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800568 }
569 } else {
570 switch (ext_bit) {
571 case ProcHook::KHR_swapchain:
572 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
573 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
574 ext_bit = ProcHook::ANDROID_native_buffer;
575 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700576 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700577 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300578 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700579 hook_extensions_.set(ext_bit);
580 // return now as these extensions do not require HAL support
581 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700582 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700583 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700584 hook_extensions_.set(ext_bit);
585 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700586 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800587 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700588 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800589 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700590
591 case ProcHook::KHR_android_surface:
592 case ProcHook::KHR_get_physical_device_properties2:
593 case ProcHook::KHR_get_surface_capabilities2:
594 case ProcHook::KHR_surface:
595 case ProcHook::EXT_debug_report:
596 case ProcHook::EXT_swapchain_colorspace:
597 case ProcHook::ANDROID_native_buffer:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700598 case ProcHook::EXTENSION_CORE_1_0:
599 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700600 case ProcHook::EXTENSION_COUNT:
601 // Instance and meta extensions. If we ever get here it's a bug
602 // in our code. But enumerating them lets us avoid having a
603 // default case, and default hides other bugs.
604 ALOGE(
605 "CreateInfoWrapper::FilterExtension: invalid device "
606 "extension '%s'. FIX ME",
607 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800608 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700609
610 // Don't use a default case. Without it, -Wswitch will tell us
611 // at compile time if someone adds a new ProcHook extension but
612 // doesn't handle it above. That's a real bug that has
613 // not-immediately-obvious effects.
614 //
615 // default:
616 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800617 }
618 }
619
620 for (uint32_t i = 0; i < filter.ext_count; i++) {
621 const VkExtensionProperties& props = filter.exts[i];
622 // ignore unknown extensions
623 if (strcmp(name, props.extensionName) != 0)
624 continue;
625
Chia-I Wu4901db72016-03-24 16:38:58 +0800626 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800627 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
628 if (ext_bit == ProcHook::ANDROID_native_buffer)
629 hook_extensions_.set(ProcHook::KHR_swapchain);
630
631 hal_extensions_.set(ext_bit);
632 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800633
634 break;
635 }
636}
637
Ian Elliottf3e872d2017-11-02 10:15:13 -0600638void CreateInfoWrapper::DowngradeApiVersion() {
639 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
640 if (instance_info_.pApplicationInfo) {
641 application_info_ = *instance_info_.pApplicationInfo;
642 instance_info_.pApplicationInfo = &application_info_;
643 application_info_.apiVersion = VK_API_VERSION_1_0;
644 }
645}
646
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700647void CreateInfoWrapper::UpgradeDeviceCoreApiVersion(uint32_t api_version) {
648 ALOG_ASSERT(!is_instance_, "Device only API called by instance wrapper.");
649
650#pragma clang diagnostic push
651#pragma clang diagnostic ignored "-Wold-style-cast"
652 api_version ^= VK_VERSION_PATCH(api_version);
653#pragma clang diagnostic pop
654
655 // cap the API version to the loader supported highest version
656 if (api_version > VK_API_VERSION_1_1)
657 api_version = VK_API_VERSION_1_1;
658
659 switch (api_version) {
660 case VK_API_VERSION_1_1:
661 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
662 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_1);
663 [[clang::fallthrough]];
664 case VK_API_VERSION_1_0:
665 break;
666 default:
667 ALOGD("Unknown upgrade API version[%u]", api_version);
668 break;
669 }
670}
671
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800672VKAPI_ATTR void* DefaultAllocate(void*,
673 size_t size,
674 size_t alignment,
675 VkSystemAllocationScope) {
676 void* ptr = nullptr;
677 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
678 // additionally requires that it be at least sizeof(void*).
679 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
680 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
681 ret, ptr);
682 return ret == 0 ? ptr : nullptr;
683}
684
685VKAPI_ATTR void* DefaultReallocate(void*,
686 void* ptr,
687 size_t size,
688 size_t alignment,
689 VkSystemAllocationScope) {
690 if (size == 0) {
691 free(ptr);
692 return nullptr;
693 }
694
Yiwei Zhanga885c062019-10-24 12:07:57 -0700695 // TODO(b/143295633): Right now we never shrink allocations; if the new
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800696 // request is smaller than the existing chunk, we just continue using it.
697 // Right now the loader never reallocs, so this doesn't matter. If that
698 // changes, or if this code is copied into some other project, this should
699 // probably have a heuristic to allocate-copy-free when doing so will save
700 // "enough" space.
701 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
702 if (size <= old_size)
703 return ptr;
704
705 void* new_ptr = nullptr;
706 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
707 return nullptr;
708 if (ptr) {
709 memcpy(new_ptr, ptr, std::min(old_size, size));
710 free(ptr);
711 }
712 return new_ptr;
713}
714
715VKAPI_ATTR void DefaultFree(void*, void* ptr) {
716 ALOGD_CALLSTACK("Free: %p", ptr);
717 free(ptr);
718}
719
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800720InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
721 void* data_mem = allocator.pfnAllocation(
722 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
723 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
724 if (!data_mem)
725 return nullptr;
726
727 return new (data_mem) InstanceData(allocator);
728}
729
730void FreeInstanceData(InstanceData* data,
731 const VkAllocationCallbacks& allocator) {
732 data->~InstanceData();
733 allocator.pfnFree(allocator.pUserData, data);
734}
735
Chia-I Wu950d6e12016-05-03 09:12:35 +0800736DeviceData* AllocateDeviceData(
737 const VkAllocationCallbacks& allocator,
738 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800739 void* data_mem = allocator.pfnAllocation(
740 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
741 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
742 if (!data_mem)
743 return nullptr;
744
Chia-I Wu950d6e12016-05-03 09:12:35 +0800745 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800746}
747
748void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
749 data->~DeviceData();
750 allocator.pfnFree(allocator.pUserData, data);
751}
752
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800753} // anonymous namespace
754
Chia-I Wu9d518162016-03-24 14:55:27 +0800755bool Debuggable() {
756 return (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) >= 0);
757}
758
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800759bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800760 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800761}
762
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800763const VkAllocationCallbacks& GetDefaultAllocator() {
764 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
765 .pUserData = nullptr,
766 .pfnAllocation = DefaultAllocate,
767 .pfnReallocation = DefaultReallocate,
768 .pfnFree = DefaultFree,
769 };
770
771 return kDefaultAllocCallbacks;
772}
773
Chia-I Wueb7db122016-03-24 09:11:06 +0800774PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
775 const ProcHook* hook = GetProcHook(pName);
776 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800777 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800778
779 if (!instance) {
780 if (hook->type == ProcHook::GLOBAL)
781 return hook->proc;
782
Chia-I Wu109f8982016-04-22 06:40:40 +0800783 // v0 layers expect
784 //
785 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
786 //
787 // to work.
788 if (strcmp(pName, "vkCreateDevice") == 0)
789 return hook->proc;
790
Chia-I Wueb7db122016-03-24 09:11:06 +0800791 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800792 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800793 pName);
794
Chia-I Wu109f8982016-04-22 06:40:40 +0800795 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800796 }
797
798 PFN_vkVoidFunction proc;
799
800 switch (hook->type) {
801 case ProcHook::INSTANCE:
802 proc = (GetData(instance).hook_extensions[hook->extension])
803 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800804 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800805 break;
806 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700807 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800808 ? hook->proc
809 : hook->checked_proc;
810 break;
811 default:
812 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800813 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800814 pName);
815 proc = nullptr;
816 break;
817 }
818
819 return proc;
820}
821
822PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
823 const ProcHook* hook = GetProcHook(pName);
824 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800825 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800826
827 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800828 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800829 return nullptr;
830 }
831
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800832 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
833 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800834}
835
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800836VkResult EnumerateInstanceExtensionProperties(
837 const char* pLayerName,
838 uint32_t* pPropertyCount,
839 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700840 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600841 loader_extensions.push_back({
842 VK_KHR_SURFACE_EXTENSION_NAME,
843 VK_KHR_SURFACE_SPEC_VERSION});
844 loader_extensions.push_back({
845 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
846 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
847 loader_extensions.push_back({
848 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
849 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700850 loader_extensions.push_back({
851 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
852 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600853
Chia-I Wu31938252016-05-23 15:31:02 +0800854 static const VkExtensionProperties loader_debug_report_extension = {
855 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
856 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800857
858 // enumerate our extensions first
859 if (!pLayerName && pProperties) {
860 uint32_t count = std::min(
861 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
862
Yiwei Zhang5e862202019-06-21 14:59:16 -0700863 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800864
865 if (count < loader_extensions.size()) {
866 *pPropertyCount = count;
867 return VK_INCOMPLETE;
868 }
869
870 pProperties += count;
871 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800872
873 if (Hal::Get().GetDebugReportIndex() < 0) {
874 if (!*pPropertyCount) {
875 *pPropertyCount = count;
876 return VK_INCOMPLETE;
877 }
878
879 pProperties[0] = loader_debug_report_extension;
880 pProperties += 1;
881 *pPropertyCount -= 1;
882 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800883 }
884
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800885 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800886 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800887 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800888 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800889
Chia-I Wu31938252016-05-23 15:31:02 +0800890 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
891 int idx = Hal::Get().GetDebugReportIndex();
892 if (idx < 0) {
893 *pPropertyCount += 1;
894 } else if (pProperties &&
895 static_cast<uint32_t>(idx) < *pPropertyCount) {
896 pProperties[idx].specVersion =
897 std::min(pProperties[idx].specVersion,
898 loader_debug_report_extension.specVersion);
899 }
900
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800901 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800902 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800903
904 return result;
905}
906
Chris Forbesfa25e632017-02-22 12:36:02 +1300907bool QueryPresentationProperties(
908 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700909 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300910 const InstanceData& data = GetData(physicalDevice);
911
912 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700913 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
914 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300915 return false;
916
917 // Request the android-specific presentation properties via GPDP2
918 VkPhysicalDeviceProperties2KHR properties = {
919 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
920 presentation_properties,
921 {}
922 };
923
924#pragma clang diagnostic push
925#pragma clang diagnostic ignored "-Wold-style-cast"
926 presentation_properties->sType =
927 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
928#pragma clang diagnostic pop
929 presentation_properties->pNext = nullptr;
930 presentation_properties->sharedImage = VK_FALSE;
931
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700932 if (data.driver.GetPhysicalDeviceProperties2KHR) {
933 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
934 &properties);
935 } else {
936 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
937 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300938
939 return true;
940}
941
Chia-I Wu01cf3052016-03-24 16:16:21 +0800942VkResult EnumerateDeviceExtensionProperties(
943 VkPhysicalDevice physicalDevice,
944 const char* pLayerName,
945 uint32_t* pPropertyCount,
946 VkExtensionProperties* pProperties) {
947 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300948 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700949 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300950 loader_extensions.push_back({
951 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
952 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300953
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800954 bool hdrBoardConfig =
955 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(
956 false);
957 if (hdrBoardConfig) {
958 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
959 VK_EXT_HDR_METADATA_SPEC_VERSION});
960 }
961
Chris Forbes16095002017-05-05 15:33:29 -0700962 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
963 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
964 presentation_properties.sharedImage) {
965 loader_extensions.push_back({
966 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
967 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300968 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700969
Ian Elliott5c34de22017-04-10 14:42:30 -0600970 // conditionally add VK_GOOGLE_display_timing if present timestamps are
971 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700972 const std::string timestamp_property("service.sf.present_timestamp");
973 android::base::WaitForPropertyCreation(timestamp_property);
974 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600975 loader_extensions.push_back({
976 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
977 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
978 }
979
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700980 // enumerate our extensions first
981 if (!pLayerName && pProperties) {
982 uint32_t count = std::min(
983 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
984
Yiwei Zhang5e862202019-06-21 14:59:16 -0700985 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700986
987 if (count < loader_extensions.size()) {
988 *pPropertyCount = count;
989 return VK_INCOMPLETE;
990 }
991
992 pProperties += count;
993 *pPropertyCount -= count;
994 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800995
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800996 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +0800997 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
998 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800999 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001000
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001001 if (pProperties) {
1002 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1003 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1004 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001005
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001006 if (strcmp(prop.extensionName,
1007 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1008 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001009
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001010 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1011 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001012
1013 if (prop.specVersion >= 8) {
1014 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1015 } else {
1016 prop.specVersion = 68;
1017 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001018 }
1019 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001020
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001021 // restore loader extension count
1022 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1023 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001024 }
1025
1026 return result;
1027}
1028
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001029VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1030 const VkAllocationCallbacks* pAllocator,
1031 VkInstance* pInstance) {
1032 const VkAllocationCallbacks& data_allocator =
1033 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1034
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001035 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001036 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001037 if (result != VK_SUCCESS)
1038 return result;
1039
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001040 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001041 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001042 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001043 if (!data)
1044 return VK_ERROR_OUT_OF_HOST_MEMORY;
1045
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001046 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001047
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001048 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001049#pragma clang diagnostic push
1050#pragma clang diagnostic ignored "-Wold-style-cast"
1051 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1052 ? pCreateInfo->pApplicationInfo->apiVersion
1053 : VK_API_VERSION_1_0);
1054 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1055 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1056 uint32_t icd_api_version;
1057 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1058 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001059 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001060 "vkEnumerateInstanceVersion"));
1061 if (!pfn_enumerate_instance_version) {
1062 icd_api_version = VK_API_VERSION_1_0;
1063 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001064 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001065 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001066 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001067 }
1068 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1069 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1070
1071 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1072 ((api_major_version > 1) || (api_minor_version > 0))) {
1073 api_version = VK_API_VERSION_1_0;
1074 wrapper.DowngradeApiVersion();
1075 }
1076#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001077 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001078
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001079 // call into the driver
1080 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001081 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001082 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001083 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1084 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001085 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001086 if (result != VK_SUCCESS) {
1087 FreeInstanceData(data, data_allocator);
1088 return result;
1089 }
1090
1091 // initialize InstanceDriverTable
1092 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001093 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001094 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001095 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001096 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001097 if (data->driver.DestroyInstance)
1098 data->driver.DestroyInstance(instance, pAllocator);
1099
1100 FreeInstanceData(data, data_allocator);
1101
1102 return VK_ERROR_INCOMPATIBLE_DRIVER;
1103 }
1104
1105 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001106 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001107 if (!data->get_device_proc_addr) {
1108 data->driver.DestroyInstance(instance, pAllocator);
1109 FreeInstanceData(data, data_allocator);
1110
1111 return VK_ERROR_INCOMPATIBLE_DRIVER;
1112 }
1113
1114 *pInstance = instance;
1115
1116 return VK_SUCCESS;
1117}
1118
1119void DestroyInstance(VkInstance instance,
1120 const VkAllocationCallbacks* pAllocator) {
1121 InstanceData& data = GetData(instance);
1122 data.driver.DestroyInstance(instance, pAllocator);
1123
1124 VkAllocationCallbacks local_allocator;
1125 if (!pAllocator) {
1126 local_allocator = data.allocator;
1127 pAllocator = &local_allocator;
1128 }
1129
1130 FreeInstanceData(&data, *pAllocator);
1131}
1132
Chia-I Wu4901db72016-03-24 16:38:58 +08001133VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1134 const VkDeviceCreateInfo* pCreateInfo,
1135 const VkAllocationCallbacks* pAllocator,
1136 VkDevice* pDevice) {
1137 const InstanceData& instance_data = GetData(physicalDevice);
1138 const VkAllocationCallbacks& data_allocator =
1139 (pAllocator) ? *pAllocator : instance_data.allocator;
1140
1141 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001142 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001143 if (result != VK_SUCCESS)
1144 return result;
1145
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001146 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001147 DeviceData* data = AllocateDeviceData(data_allocator,
1148 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001149 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001150 if (!data)
1151 return VK_ERROR_OUT_OF_HOST_MEMORY;
1152
Yiwei Zhang7cc36a52019-10-11 19:02:09 -07001153 VkPhysicalDeviceProperties properties;
1154 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1155 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1156 &properties);
1157 ATRACE_END();
1158
1159 wrapper.UpgradeDeviceCoreApiVersion(properties.apiVersion);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001160 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001161
1162 // call into the driver
1163 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001164 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001165 result = instance_data.driver.CreateDevice(
1166 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1167 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001168 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001169 if (result != VK_SUCCESS) {
1170 FreeDeviceData(data, data_allocator);
1171 return result;
1172 }
1173
1174 // initialize DeviceDriverTable
1175 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001176 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1177 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001178 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1179 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1180 if (data->driver.DestroyDevice)
1181 data->driver.DestroyDevice(dev, pAllocator);
1182
1183 FreeDeviceData(data, data_allocator);
1184
1185 return VK_ERROR_INCOMPATIBLE_DRIVER;
1186 }
Chris Forbesd8277912017-02-10 14:59:59 +13001187
1188 // sanity check ANDROID_native_buffer implementation, whose set of
1189 // entrypoints varies according to the spec version.
1190 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1191 !data->driver.GetSwapchainGrallocUsageANDROID &&
1192 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1193 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1194 " must expose at least one of "
1195 "vkGetSwapchainGrallocUsageANDROID or "
1196 "vkGetSwapchainGrallocUsage2ANDROID");
1197
1198 data->driver.DestroyDevice(dev, pAllocator);
1199 FreeDeviceData(data, data_allocator);
1200
1201 return VK_ERROR_INCOMPATIBLE_DRIVER;
1202 }
1203
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001204 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1205 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangbcba4112019-07-03 13:39:32 -07001206 android::GraphicsEnv::getInstance().setTargetStats(
1207 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001208 }
1209
Jesse Halldc225072016-05-30 22:40:14 -07001210 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001211
1212 *pDevice = dev;
1213
1214 return VK_SUCCESS;
1215}
1216
1217void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1218 DeviceData& data = GetData(device);
1219 data.driver.DestroyDevice(device, pAllocator);
1220
1221 VkAllocationCallbacks local_allocator;
1222 if (!pAllocator) {
1223 local_allocator = data.allocator;
1224 pAllocator = &local_allocator;
1225 }
1226
1227 FreeDeviceData(&data, *pAllocator);
1228}
1229
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001230VkResult EnumeratePhysicalDevices(VkInstance instance,
1231 uint32_t* pPhysicalDeviceCount,
1232 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001233 ATRACE_CALL();
1234
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001235 const auto& data = GetData(instance);
1236
1237 VkResult result = data.driver.EnumeratePhysicalDevices(
1238 instance, pPhysicalDeviceCount, pPhysicalDevices);
1239 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1240 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1241 SetData(pPhysicalDevices[i], data);
1242 }
1243
1244 return result;
1245}
1246
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001247VkResult EnumeratePhysicalDeviceGroups(
1248 VkInstance instance,
1249 uint32_t* pPhysicalDeviceGroupCount,
1250 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001251 ATRACE_CALL();
1252
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001253 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001254 const auto& data = GetData(instance);
1255
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001256 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1257 uint32_t device_count = 0;
1258 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1259 if (result < 0)
1260 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001261
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001262 if (!pPhysicalDeviceGroupProperties) {
1263 *pPhysicalDeviceGroupCount = device_count;
1264 return result;
1265 }
1266
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001267 if (!device_count) {
1268 *pPhysicalDeviceGroupCount = 0;
1269 return result;
1270 }
Chad Versace32c087f2018-09-09 07:28:05 -07001271 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1272 if (!device_count)
1273 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001274
Yiwei Zhang5e862202019-06-21 14:59:16 -07001275 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001276 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001277 result =
1278 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001279 if (result < 0)
1280 return result;
1281
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001282 for (uint32_t i = 0; i < device_count; ++i) {
1283 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1284 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1285 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1286 }
1287 } else {
1288 result = data.driver.EnumeratePhysicalDeviceGroups(
1289 instance, pPhysicalDeviceGroupCount,
1290 pPhysicalDeviceGroupProperties);
1291 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1292 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1293 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1294 for (uint32_t j = 0;
1295 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1296 j++) {
1297 SetData(
1298 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001299 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001300 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001301 }
1302 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001303 }
1304
1305 return result;
1306}
1307
Chia-I Wuba0be412016-03-24 16:24:40 +08001308void GetDeviceQueue(VkDevice device,
1309 uint32_t queueFamilyIndex,
1310 uint32_t queueIndex,
1311 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001312 ATRACE_CALL();
1313
Chia-I Wuba0be412016-03-24 16:24:40 +08001314 const auto& data = GetData(device);
1315
1316 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1317 SetData(*pQueue, data);
1318}
1319
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001320void GetDeviceQueue2(VkDevice device,
1321 const VkDeviceQueueInfo2* pQueueInfo,
1322 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001323 ATRACE_CALL();
1324
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001325 const auto& data = GetData(device);
1326
1327 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001328 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001329}
1330
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001331VKAPI_ATTR VkResult
1332AllocateCommandBuffers(VkDevice device,
1333 const VkCommandBufferAllocateInfo* pAllocateInfo,
1334 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001335 ATRACE_CALL();
1336
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001337 const auto& data = GetData(device);
1338
1339 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1340 pCommandBuffers);
1341 if (result == VK_SUCCESS) {
1342 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1343 SetData(pCommandBuffers[i], data);
1344 }
1345
1346 return result;
1347}
1348
Yiwei Zhang899d1752019-09-23 16:05:35 -07001349VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1350 uint32_t submitCount,
1351 const VkSubmitInfo* pSubmits,
1352 VkFence fence) {
1353 ATRACE_CALL();
1354
1355 const auto& data = GetData(queue);
1356
1357 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1358}
1359
Chia-I Wu9d518162016-03-24 14:55:27 +08001360} // namespace driver
1361} // namespace vulkan