blob: a544bc5ff8c68445ff7eed956eb5fce7e0fe1dac [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 Wu9d518162016-03-24 14:55:27 +0800758bool Debuggable() {
759 return (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) >= 0);
760}
761
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800762bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800763 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800764}
765
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800766const VkAllocationCallbacks& GetDefaultAllocator() {
767 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
768 .pUserData = nullptr,
769 .pfnAllocation = DefaultAllocate,
770 .pfnReallocation = DefaultReallocate,
771 .pfnFree = DefaultFree,
772 };
773
774 return kDefaultAllocCallbacks;
775}
776
Chia-I Wueb7db122016-03-24 09:11:06 +0800777PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
778 const ProcHook* hook = GetProcHook(pName);
779 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800780 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800781
782 if (!instance) {
783 if (hook->type == ProcHook::GLOBAL)
784 return hook->proc;
785
Chia-I Wu109f8982016-04-22 06:40:40 +0800786 // v0 layers expect
787 //
788 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
789 //
790 // to work.
791 if (strcmp(pName, "vkCreateDevice") == 0)
792 return hook->proc;
793
Chia-I Wueb7db122016-03-24 09:11:06 +0800794 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800795 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800796 pName);
797
Chia-I Wu109f8982016-04-22 06:40:40 +0800798 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800799 }
800
801 PFN_vkVoidFunction proc;
802
803 switch (hook->type) {
804 case ProcHook::INSTANCE:
805 proc = (GetData(instance).hook_extensions[hook->extension])
806 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800807 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800808 break;
809 case ProcHook::DEVICE:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700810 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800811 ? hook->proc
812 : hook->checked_proc;
813 break;
814 default:
815 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800816 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800817 pName);
818 proc = nullptr;
819 break;
820 }
821
822 return proc;
823}
824
825PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
826 const ProcHook* hook = GetProcHook(pName);
827 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800828 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800829
830 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800831 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800832 return nullptr;
833 }
834
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800835 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
836 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800837}
838
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800839VkResult EnumerateInstanceExtensionProperties(
840 const char* pLayerName,
841 uint32_t* pPropertyCount,
842 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700843 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600844 loader_extensions.push_back({
845 VK_KHR_SURFACE_EXTENSION_NAME,
846 VK_KHR_SURFACE_SPEC_VERSION});
847 loader_extensions.push_back({
848 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
849 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
850 loader_extensions.push_back({
851 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
852 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700853 loader_extensions.push_back({
854 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
855 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600856
Chia-I Wu31938252016-05-23 15:31:02 +0800857 static const VkExtensionProperties loader_debug_report_extension = {
858 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
859 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800860
861 // enumerate our extensions first
862 if (!pLayerName && pProperties) {
863 uint32_t count = std::min(
864 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
865
Yiwei Zhang5e862202019-06-21 14:59:16 -0700866 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800867
868 if (count < loader_extensions.size()) {
869 *pPropertyCount = count;
870 return VK_INCOMPLETE;
871 }
872
873 pProperties += count;
874 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800875
876 if (Hal::Get().GetDebugReportIndex() < 0) {
877 if (!*pPropertyCount) {
878 *pPropertyCount = count;
879 return VK_INCOMPLETE;
880 }
881
882 pProperties[0] = loader_debug_report_extension;
883 pProperties += 1;
884 *pPropertyCount -= 1;
885 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800886 }
887
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800888 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800889 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800890 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800891 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800892
Chia-I Wu31938252016-05-23 15:31:02 +0800893 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
894 int idx = Hal::Get().GetDebugReportIndex();
895 if (idx < 0) {
896 *pPropertyCount += 1;
897 } else if (pProperties &&
898 static_cast<uint32_t>(idx) < *pPropertyCount) {
899 pProperties[idx].specVersion =
900 std::min(pProperties[idx].specVersion,
901 loader_debug_report_extension.specVersion);
902 }
903
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800904 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800905 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800906
907 return result;
908}
909
Chris Forbesfa25e632017-02-22 12:36:02 +1300910bool QueryPresentationProperties(
911 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700912 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300913 const InstanceData& data = GetData(physicalDevice);
914
915 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700916 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
917 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300918 return false;
919
920 // Request the android-specific presentation properties via GPDP2
921 VkPhysicalDeviceProperties2KHR properties = {
922 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
923 presentation_properties,
924 {}
925 };
926
927#pragma clang diagnostic push
928#pragma clang diagnostic ignored "-Wold-style-cast"
929 presentation_properties->sType =
930 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
931#pragma clang diagnostic pop
932 presentation_properties->pNext = nullptr;
933 presentation_properties->sharedImage = VK_FALSE;
934
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700935 if (data.driver.GetPhysicalDeviceProperties2KHR) {
936 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
937 &properties);
938 } else {
939 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
940 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300941
942 return true;
943}
944
Chia-I Wu01cf3052016-03-24 16:16:21 +0800945VkResult EnumerateDeviceExtensionProperties(
946 VkPhysicalDevice physicalDevice,
947 const char* pLayerName,
948 uint32_t* pPropertyCount,
949 VkExtensionProperties* pProperties) {
950 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300951 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700952 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300953 loader_extensions.push_back({
954 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
955 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300956
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800957 bool hdrBoardConfig =
958 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(
959 false);
960 if (hdrBoardConfig) {
961 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
962 VK_EXT_HDR_METADATA_SPEC_VERSION});
963 }
964
Chris Forbes16095002017-05-05 15:33:29 -0700965 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
966 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
967 presentation_properties.sharedImage) {
968 loader_extensions.push_back({
969 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
970 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300971 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700972
Ian Elliott5c34de22017-04-10 14:42:30 -0600973 // conditionally add VK_GOOGLE_display_timing if present timestamps are
974 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700975 const std::string timestamp_property("service.sf.present_timestamp");
976 android::base::WaitForPropertyCreation(timestamp_property);
977 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600978 loader_extensions.push_back({
979 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
980 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
981 }
982
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700983 // enumerate our extensions first
984 if (!pLayerName && pProperties) {
985 uint32_t count = std::min(
986 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
987
Yiwei Zhang5e862202019-06-21 14:59:16 -0700988 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700989
990 if (count < loader_extensions.size()) {
991 *pPropertyCount = count;
992 return VK_INCOMPLETE;
993 }
994
995 pProperties += count;
996 *pPropertyCount -= count;
997 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800998
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800999 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001000 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1001 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001002 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001003
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001004 if (pProperties) {
1005 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1006 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1007 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001008
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001009 if (strcmp(prop.extensionName,
1010 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1011 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001012
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001013 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1014 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001015
1016 if (prop.specVersion >= 8) {
1017 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1018 } else {
1019 prop.specVersion = 68;
1020 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001021 }
1022 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001023
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001024 // restore loader extension count
1025 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1026 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001027 }
1028
1029 return result;
1030}
1031
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001032VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1033 const VkAllocationCallbacks* pAllocator,
1034 VkInstance* pInstance) {
1035 const VkAllocationCallbacks& data_allocator =
1036 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1037
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001038 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001039 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001040 if (result != VK_SUCCESS)
1041 return result;
1042
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001043 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001044 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001045 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001046 if (!data)
1047 return VK_ERROR_OUT_OF_HOST_MEMORY;
1048
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001049 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001050
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001051 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001052#pragma clang diagnostic push
1053#pragma clang diagnostic ignored "-Wold-style-cast"
1054 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1055 ? pCreateInfo->pApplicationInfo->apiVersion
1056 : VK_API_VERSION_1_0);
1057 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1058 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1059 uint32_t icd_api_version;
1060 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1061 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001062 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001063 "vkEnumerateInstanceVersion"));
1064 if (!pfn_enumerate_instance_version) {
1065 icd_api_version = VK_API_VERSION_1_0;
1066 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001067 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001068 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001069 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001070 }
1071 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1072 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1073
1074 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1075 ((api_major_version > 1) || (api_minor_version > 0))) {
1076 api_version = VK_API_VERSION_1_0;
1077 wrapper.DowngradeApiVersion();
1078 }
1079#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001080 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001081
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001082 // call into the driver
1083 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001084 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001085 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001086 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1087 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001088 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001089 if (result != VK_SUCCESS) {
1090 FreeInstanceData(data, data_allocator);
1091 return result;
1092 }
1093
1094 // initialize InstanceDriverTable
1095 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001096 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001097 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001098 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001099 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001100 if (data->driver.DestroyInstance)
1101 data->driver.DestroyInstance(instance, pAllocator);
1102
1103 FreeInstanceData(data, data_allocator);
1104
1105 return VK_ERROR_INCOMPATIBLE_DRIVER;
1106 }
1107
1108 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001109 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001110 if (!data->get_device_proc_addr) {
1111 data->driver.DestroyInstance(instance, pAllocator);
1112 FreeInstanceData(data, data_allocator);
1113
1114 return VK_ERROR_INCOMPATIBLE_DRIVER;
1115 }
1116
1117 *pInstance = instance;
1118
1119 return VK_SUCCESS;
1120}
1121
1122void DestroyInstance(VkInstance instance,
1123 const VkAllocationCallbacks* pAllocator) {
1124 InstanceData& data = GetData(instance);
1125 data.driver.DestroyInstance(instance, pAllocator);
1126
1127 VkAllocationCallbacks local_allocator;
1128 if (!pAllocator) {
1129 local_allocator = data.allocator;
1130 pAllocator = &local_allocator;
1131 }
1132
1133 FreeInstanceData(&data, *pAllocator);
1134}
1135
Chia-I Wu4901db72016-03-24 16:38:58 +08001136VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1137 const VkDeviceCreateInfo* pCreateInfo,
1138 const VkAllocationCallbacks* pAllocator,
1139 VkDevice* pDevice) {
1140 const InstanceData& instance_data = GetData(physicalDevice);
1141 const VkAllocationCallbacks& data_allocator =
1142 (pAllocator) ? *pAllocator : instance_data.allocator;
1143
1144 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001145 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001146 if (result != VK_SUCCESS)
1147 return result;
1148
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001149 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001150 DeviceData* data = AllocateDeviceData(data_allocator,
1151 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001152 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001153 if (!data)
1154 return VK_ERROR_OUT_OF_HOST_MEMORY;
1155
Yiwei Zhang7cc36a52019-10-11 19:02:09 -07001156 VkPhysicalDeviceProperties properties;
1157 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1158 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1159 &properties);
1160 ATRACE_END();
1161
1162 wrapper.UpgradeDeviceCoreApiVersion(properties.apiVersion);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001163 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001164
1165 // call into the driver
1166 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001167 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001168 result = instance_data.driver.CreateDevice(
1169 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1170 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001171 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001172 if (result != VK_SUCCESS) {
1173 FreeDeviceData(data, data_allocator);
1174 return result;
1175 }
1176
1177 // initialize DeviceDriverTable
1178 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001179 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1180 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001181 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1182 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1183 if (data->driver.DestroyDevice)
1184 data->driver.DestroyDevice(dev, pAllocator);
1185
1186 FreeDeviceData(data, data_allocator);
1187
1188 return VK_ERROR_INCOMPATIBLE_DRIVER;
1189 }
Chris Forbesd8277912017-02-10 14:59:59 +13001190
1191 // sanity check ANDROID_native_buffer implementation, whose set of
1192 // entrypoints varies according to the spec version.
1193 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1194 !data->driver.GetSwapchainGrallocUsageANDROID &&
1195 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1196 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1197 " must expose at least one of "
1198 "vkGetSwapchainGrallocUsageANDROID or "
1199 "vkGetSwapchainGrallocUsage2ANDROID");
1200
1201 data->driver.DestroyDevice(dev, pAllocator);
1202 FreeDeviceData(data, data_allocator);
1203
1204 return VK_ERROR_INCOMPATIBLE_DRIVER;
1205 }
1206
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001207 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1208 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangbcba4112019-07-03 13:39:32 -07001209 android::GraphicsEnv::getInstance().setTargetStats(
1210 android::GpuStatsInfo::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001211 }
1212
Jesse Halldc225072016-05-30 22:40:14 -07001213 data->driver_device = dev;
Chia-I Wu4901db72016-03-24 16:38:58 +08001214
1215 *pDevice = dev;
1216
1217 return VK_SUCCESS;
1218}
1219
1220void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1221 DeviceData& data = GetData(device);
1222 data.driver.DestroyDevice(device, pAllocator);
1223
1224 VkAllocationCallbacks local_allocator;
1225 if (!pAllocator) {
1226 local_allocator = data.allocator;
1227 pAllocator = &local_allocator;
1228 }
1229
1230 FreeDeviceData(&data, *pAllocator);
1231}
1232
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001233VkResult EnumeratePhysicalDevices(VkInstance instance,
1234 uint32_t* pPhysicalDeviceCount,
1235 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001236 ATRACE_CALL();
1237
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001238 const auto& data = GetData(instance);
1239
1240 VkResult result = data.driver.EnumeratePhysicalDevices(
1241 instance, pPhysicalDeviceCount, pPhysicalDevices);
1242 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1243 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1244 SetData(pPhysicalDevices[i], data);
1245 }
1246
1247 return result;
1248}
1249
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001250VkResult EnumeratePhysicalDeviceGroups(
1251 VkInstance instance,
1252 uint32_t* pPhysicalDeviceGroupCount,
1253 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001254 ATRACE_CALL();
1255
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001256 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001257 const auto& data = GetData(instance);
1258
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001259 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1260 uint32_t device_count = 0;
1261 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1262 if (result < 0)
1263 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001264
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001265 if (!pPhysicalDeviceGroupProperties) {
1266 *pPhysicalDeviceGroupCount = device_count;
1267 return result;
1268 }
1269
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001270 if (!device_count) {
1271 *pPhysicalDeviceGroupCount = 0;
1272 return result;
1273 }
Chad Versace32c087f2018-09-09 07:28:05 -07001274 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1275 if (!device_count)
1276 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001277
Yiwei Zhang5e862202019-06-21 14:59:16 -07001278 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001279 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001280 result =
1281 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001282 if (result < 0)
1283 return result;
1284
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001285 for (uint32_t i = 0; i < device_count; ++i) {
1286 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1287 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1288 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1289 }
1290 } else {
1291 result = data.driver.EnumeratePhysicalDeviceGroups(
1292 instance, pPhysicalDeviceGroupCount,
1293 pPhysicalDeviceGroupProperties);
1294 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1295 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1296 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1297 for (uint32_t j = 0;
1298 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1299 j++) {
1300 SetData(
1301 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001302 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001303 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001304 }
1305 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001306 }
1307
1308 return result;
1309}
1310
Chia-I Wuba0be412016-03-24 16:24:40 +08001311void GetDeviceQueue(VkDevice device,
1312 uint32_t queueFamilyIndex,
1313 uint32_t queueIndex,
1314 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001315 ATRACE_CALL();
1316
Chia-I Wuba0be412016-03-24 16:24:40 +08001317 const auto& data = GetData(device);
1318
1319 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1320 SetData(*pQueue, data);
1321}
1322
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001323void GetDeviceQueue2(VkDevice device,
1324 const VkDeviceQueueInfo2* pQueueInfo,
1325 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001326 ATRACE_CALL();
1327
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001328 const auto& data = GetData(device);
1329
1330 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001331 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001332}
1333
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001334VKAPI_ATTR VkResult
1335AllocateCommandBuffers(VkDevice device,
1336 const VkCommandBufferAllocateInfo* pAllocateInfo,
1337 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001338 ATRACE_CALL();
1339
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001340 const auto& data = GetData(device);
1341
1342 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1343 pCommandBuffers);
1344 if (result == VK_SUCCESS) {
1345 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1346 SetData(pCommandBuffers[i], data);
1347 }
1348
1349 return result;
1350}
1351
Yiwei Zhang899d1752019-09-23 16:05:35 -07001352VKAPI_ATTR VkResult QueueSubmit(VkQueue queue,
1353 uint32_t submitCount,
1354 const VkSubmitInfo* pSubmits,
1355 VkFence fence) {
1356 ATRACE_CALL();
1357
1358 const auto& data = GetData(queue);
1359
1360 return data.driver.QueueSubmit(queue, submitCount, pSubmits, fence);
1361}
1362
Chia-I Wu9d518162016-03-24 14:55:27 +08001363} // namespace driver
1364} // namespace vulkan