blob: ff84730a690bb0ed18de4a54fc8971613dfede40 [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
Mark Salyzyn7823e122016-09-29 08:08:05 -070019#include <malloc.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080020#include <stdlib.h>
21#include <string.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <sys/prctl.h>
23
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -080024#include <dlfcn.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080025#include <algorithm>
Chia-I Wuff4a6c72016-03-24 16:05:56 +080026#include <array>
Nick Desaulniersa7044712019-10-21 16:36:19 -070027#include <climits>
Chia-I Wu4901db72016-03-24 16:38:58 +080028#include <new>
Nick Desaulniersa7044712019-10-21 16:36:19 -070029#include <sstream>
30#include <string>
Mark Salyzyn7823e122016-09-29 08:08:05 -070031
32#include <log/log.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080033
Jesse Hall53457db2016-12-14 16:54:06 -080034#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080035#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
36#include <configstore/Utils.h>
Jesse Hall53457db2016-12-14 16:54:06 -080037#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090038#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080039#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080040#include <utils/Trace.h>
Chris Forbesfa25e632017-02-22 12:36:02 +130041#include <utils/Vector.h>
Jesse Hall53457db2016-12-14 16:54:06 -080042
Wei Wangf9b05ee2017-07-19 20:59:39 -070043#include "android-base/properties.h"
44
Chia-I Wu9d518162016-03-24 14:55:27 +080045#include "driver.h"
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
Jesse Hall00e61ff2017-04-07 16:48:02 -070051// TODO(b/37049319) Get this from a header once one exists
52extern "C" {
53android_namespace_t* android_get_exported_namespace(const char*);
54}
55
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080056// #define ENABLE_ALLOC_CALLSTACKS 1
57#if ENABLE_ALLOC_CALLSTACKS
58#include <utils/CallStack.h>
59#define ALOGD_CALLSTACK(...) \
60 do { \
61 ALOGD(__VA_ARGS__); \
62 android::CallStack callstack; \
63 callstack.update(); \
64 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
65 } while (false)
66#else
67#define ALOGD_CALLSTACK(...) \
68 do { \
69 } while (false)
70#endif
71
Chia-I Wu9d518162016-03-24 14:55:27 +080072namespace vulkan {
73namespace driver {
74
Chia-I Wu136b8eb2016-03-24 15:01:52 +080075namespace {
76
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080077class Hal {
78 public:
79 static bool Open();
80
81 static const Hal& Get() { return hal_; }
82 static const hwvulkan_device_t& Device() { return *Get().dev_; }
83
Chia-I Wu31938252016-05-23 15:31:02 +080084 int GetDebugReportIndex() const { return debug_report_index_; }
85
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080086 private:
Chia-I Wu31938252016-05-23 15:31:02 +080087 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080088 Hal(const Hal&) = delete;
89 Hal& operator=(const Hal&) = delete;
90
Chia-I Wu31938252016-05-23 15:31:02 +080091 bool InitDebugReportIndex();
92
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080093 static Hal hal_;
94
95 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080096 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080097};
98
Chia-I Wu4901db72016-03-24 16:38:58 +080099class CreateInfoWrapper {
100 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800101 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800102 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +0800103 CreateInfoWrapper(VkPhysicalDevice physical_dev,
104 const VkDeviceCreateInfo& create_info,
105 const VkAllocationCallbacks& allocator);
106 ~CreateInfoWrapper();
107
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800108 VkResult Validate();
Ian Elliottf3e872d2017-11-02 10:15:13 -0600109 void DowngradeApiVersion();
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700110 void UpgradeDeviceCoreApiVersion(uint32_t api_version);
Chia-I Wu4901db72016-03-24 16:38:58 +0800111
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800112 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
113 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800114
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800115 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800116 explicit operator const VkDeviceCreateInfo*() const;
117
118 private:
119 struct ExtensionFilter {
120 VkExtensionProperties* exts;
121 uint32_t ext_count;
122
123 const char** names;
124 uint32_t name_count;
125 };
126
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800127 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800128
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800129 VkResult SanitizeLayers();
130 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800131
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800132 VkResult QueryExtensionCount(uint32_t& count) const;
133 VkResult EnumerateExtensions(uint32_t& count,
134 VkExtensionProperties* props) const;
135 VkResult InitExtensionFilter();
136 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800137
138 const bool is_instance_;
139 const VkAllocationCallbacks& allocator_;
140
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800141 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800142
143 union {
144 VkInstanceCreateInfo instance_info_;
145 VkDeviceCreateInfo dev_info_;
146 };
147
Ian Elliottf3e872d2017-11-02 10:15:13 -0600148 VkApplicationInfo application_info_;
149
Chia-I Wu4901db72016-03-24 16:38:58 +0800150 ExtensionFilter extension_filter_;
151
152 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
153 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
154};
155
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800156Hal Hal::hal_;
157
Jesse Hall53457db2016-12-14 16:54:06 -0800158void* LoadLibrary(const android_dlextinfo& dlextinfo,
Nick Desaulniersa7044712019-10-21 16:36:19 -0700159 const std::string_view subname) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800160 ATRACE_CALL();
161
Nick Desaulniersa7044712019-10-21 16:36:19 -0700162 std::stringstream ss;
163 ss << "vulkan." << subname << ".so";
164 return android_dlopen_ext(ss.str().c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
Jesse Hall53457db2016-12-14 16:54:06 -0800165}
166
167const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
Peter Collingbourne161c76b2020-04-22 13:12:23 -0700168 "ro.hardware.vulkan",
Jesse Hall53457db2016-12-14 16:54:06 -0800169 "ro.board.platform",
170}};
171
Jesse Hall00e61ff2017-04-07 16:48:02 -0700172int LoadDriver(android_namespace_t* library_namespace,
173 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800174 ATRACE_CALL();
175
Jesse Hall53457db2016-12-14 16:54:06 -0800176 const android_dlextinfo dlextinfo = {
177 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700178 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800179 };
Jesse Hall53457db2016-12-14 16:54:06 -0800180 void* so = nullptr;
181 char prop[PROPERTY_VALUE_MAX];
182 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
183 int prop_len = property_get(key, prop, nullptr);
Nick Desaulniersa7044712019-10-21 16:36:19 -0700184 if (prop_len > 0 && prop_len <= UINT_MAX) {
185 std::string_view lib_name(prop, static_cast<unsigned int>(prop_len));
186 so = LoadLibrary(dlextinfo, lib_name);
Jesse Hall53457db2016-12-14 16:54:06 -0800187 if (so)
188 break;
189 }
190 }
191 if (!so)
192 return -ENOENT;
193
Jesse Hall00e61ff2017-04-07 16:48:02 -0700194 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800195 if (!hmi) {
196 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
197 dlclose(so);
198 return -EINVAL;
199 }
200 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
201 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
202 dlclose(so);
203 return -EINVAL;
204 }
205 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700206 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800207 return 0;
208}
209
Jesse Hall00e61ff2017-04-07 16:48:02 -0700210int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800211 ATRACE_CALL();
212
Jesse Hall00e61ff2017-04-07 16:48:02 -0700213 auto ns = android_get_exported_namespace("sphal");
214 if (!ns)
215 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800216 android::GraphicsEnv::getInstance().setDriverToLoad(
217 android::GraphicsEnv::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218 return LoadDriver(ns, module);
219}
220
221int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800222 ATRACE_CALL();
223
Jesse Hall00e61ff2017-04-07 16:48:02 -0700224 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
225 if (!ns)
226 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800227 android::GraphicsEnv::getInstance().setDriverToLoad(
228 android::GraphicsEnv::Driver::VULKAN_UPDATED);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700229 return LoadDriver(ns, module);
230}
231
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800232bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800233 ATRACE_CALL();
234
Yiwei Zhangd9861812019-02-13 11:51:55 -0800235 const nsecs_t openTime = systemTime();
236
Jesse Halldc225072016-05-30 22:40:14 -0700237 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800238
239 // Use a stub device unless we successfully open a real HAL device.
240 hal_.dev_ = &stubhal::kDevice;
241
Jesse Hall53457db2016-12-14 16:54:06 -0800242 int result;
243 const hwvulkan_module_t* module = nullptr;
244
Jesse Hall00e61ff2017-04-07 16:48:02 -0700245 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800246 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700247 result = LoadBuiltinDriver(&module);
248 if (result != 0) {
249 // -ENOENT means the sphal namespace doesn't exist, not that there
250 // is a problem with the driver.
251 ALOGW_IF(
252 result != -ENOENT,
253 "Failed to load Vulkan driver into sphal namespace. This "
254 "usually means the driver has forbidden library dependencies."
255 "Please fix, this will soon stop working.");
256 result =
257 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
258 reinterpret_cast<const hw_module_t**>(&module));
259 }
Jesse Hall53457db2016-12-14 16:54:06 -0800260 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800261 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800262 android::GraphicsEnv::getInstance().setDriverLoaded(
263 android::GraphicsEnv::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800264 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800265 return true;
266 }
267
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800268
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800269 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800270 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800271 result =
272 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
273 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800274 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800275 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800276 android::GraphicsEnv::getInstance().setDriverLoaded(
277 android::GraphicsEnv::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800278 // Any device with a Vulkan HAL should be able to open the device.
279 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
280 result);
281 return false;
282 }
283
284 hal_.dev_ = device;
285
Chia-I Wu31938252016-05-23 15:31:02 +0800286 hal_.InitDebugReportIndex();
287
Yiwei Zhangd9861812019-02-13 11:51:55 -0800288 android::GraphicsEnv::getInstance().setDriverLoaded(
289 android::GraphicsEnv::Api::API_VK, true, systemTime() - openTime);
290
Chia-I Wu31938252016-05-23 15:31:02 +0800291 return true;
292}
293
294bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800295 ATRACE_CALL();
296
Chia-I Wu31938252016-05-23 15:31:02 +0800297 uint32_t count;
298 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
299 VK_SUCCESS) {
300 ALOGE("failed to get HAL instance extension count");
301 return false;
302 }
303
304 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
305 malloc(sizeof(VkExtensionProperties) * count));
306 if (!exts) {
307 ALOGE("failed to allocate HAL instance extension array");
308 return false;
309 }
310
311 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
312 VK_SUCCESS) {
313 ALOGE("failed to enumerate HAL instance extensions");
314 free(exts);
315 return false;
316 }
317
318 for (uint32_t i = 0; i < count; i++) {
319 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
320 0) {
321 debug_report_index_ = static_cast<int>(i);
322 break;
323 }
324 }
325
326 free(exts);
327
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800328 return true;
329}
330
331CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800332 const VkAllocationCallbacks& allocator)
333 : is_instance_(true),
334 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800335 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800336 instance_info_(create_info),
337 extension_filter_() {
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700338 // instance core versions need to match the loader api version
339 for (uint32_t i = ProcHook::EXTENSION_CORE_1_0;
340 i != ProcHook::EXTENSION_COUNT; ++i) {
341 hook_extensions_.set(i);
342 hal_extensions_.set(i);
343 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800344}
345
Chia-I Wu4901db72016-03-24 16:38:58 +0800346CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
347 const VkDeviceCreateInfo& create_info,
348 const VkAllocationCallbacks& allocator)
349 : is_instance_(false),
350 allocator_(allocator),
351 physical_dev_(physical_dev),
352 dev_info_(create_info),
353 extension_filter_() {
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700354 // initialize with baseline core API version
355 hook_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
356 hal_extensions_.set(ProcHook::EXTENSION_CORE_1_0);
Chia-I Wu4901db72016-03-24 16:38:58 +0800357}
358
359CreateInfoWrapper::~CreateInfoWrapper() {
360 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
361 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
362}
363
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800364VkResult CreateInfoWrapper::Validate() {
365 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800366 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800367 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800368 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800369 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800370
371 return result;
372}
373
374const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800375CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800376 return hook_extensions_;
377}
378
379const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800380CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800381 return hal_extensions_;
382}
383
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800384CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
385 return &instance_info_;
386}
387
Chia-I Wu4901db72016-03-24 16:38:58 +0800388CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
389 return &dev_info_;
390}
391
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800392VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800393 const struct StructHeader {
394 VkStructureType type;
395 const void* next;
396 } * header;
397
398 if (is_instance_) {
399 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
400
401 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
402 while (header &&
403 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
404 header = reinterpret_cast<const StructHeader*>(header->next);
405
406 instance_info_.pNext = header;
407 } else {
408 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
409
410 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
411 while (header &&
412 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
413 header = reinterpret_cast<const StructHeader*>(header->next);
414
415 dev_info_.pNext = header;
416 }
417
418 return VK_SUCCESS;
419}
420
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800421VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800422 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
423 : dev_info_.ppEnabledLayerNames;
424 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
425 : dev_info_.enabledLayerCount;
426
427 // remove all layers
428 layer_names = nullptr;
429 layer_count = 0;
430
431 return VK_SUCCESS;
432}
433
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800434VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800435 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
436 : dev_info_.ppEnabledExtensionNames;
437 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
438 : dev_info_.enabledExtensionCount;
439 if (!ext_count)
440 return VK_SUCCESS;
441
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800442 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800443 if (result != VK_SUCCESS)
444 return result;
445
446 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800447 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800448
Jesse Halld3d887a2018-03-05 13:34:45 -0800449 // Enable device extensions that contain physical-device commands, so that
450 // vkGetInstanceProcAddr will return those physical-device commands.
451 if (is_instance_) {
452 hook_extensions_.set(ProcHook::KHR_swapchain);
453 }
454
Chia-I Wu4901db72016-03-24 16:38:58 +0800455 ext_names = extension_filter_.names;
456 ext_count = extension_filter_.name_count;
457
458 return VK_SUCCESS;
459}
460
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800461VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800462 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800463 return Hal::Device().EnumerateInstanceExtensionProperties(
464 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800465 } else {
466 const auto& driver = GetData(physical_dev_).driver;
467 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
468 &count, nullptr);
469 }
470}
471
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800472VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800473 uint32_t& count,
474 VkExtensionProperties* props) const {
475 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800476 return Hal::Device().EnumerateInstanceExtensionProperties(
477 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800478 } else {
479 const auto& driver = GetData(physical_dev_).driver;
480 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
481 &count, props);
482 }
483}
484
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800485VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800486 // query extension count
487 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800488 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800489 if (result != VK_SUCCESS || count == 0)
490 return result;
491
492 auto& filter = extension_filter_;
493 filter.exts =
494 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
495 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
496 alignof(VkExtensionProperties),
497 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
498 if (!filter.exts)
499 return VK_ERROR_OUT_OF_HOST_MEMORY;
500
501 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800502 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800503 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
504 return result;
505
506 if (!count)
507 return VK_SUCCESS;
508
509 filter.ext_count = count;
510
511 // allocate name array
512 uint32_t enabled_ext_count = (is_instance_)
513 ? instance_info_.enabledExtensionCount
514 : dev_info_.enabledExtensionCount;
515 count = std::min(filter.ext_count, enabled_ext_count);
516 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
517 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
518 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
519 if (!filter.names)
520 return VK_ERROR_OUT_OF_HOST_MEMORY;
521
522 return VK_SUCCESS;
523}
524
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800525void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800526 auto& filter = extension_filter_;
527
528 ProcHook::Extension ext_bit = GetProcHookExtension(name);
529 if (is_instance_) {
530 switch (ext_bit) {
531 case ProcHook::KHR_android_surface:
532 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700533 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300534 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800535 hook_extensions_.set(ext_bit);
536 // return now as these extensions do not require HAL support
537 return;
538 case ProcHook::EXT_debug_report:
539 // both we and HAL can take part in
540 hook_extensions_.set(ext_bit);
541 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300542 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700543 case ProcHook::EXTENSION_UNKNOWN:
544 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800545 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700546
Yiwei Zhang23143102019-04-10 18:24:05 -0700547 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700548 case ProcHook::KHR_incremental_present:
549 case ProcHook::KHR_shared_presentable_image:
550 case ProcHook::KHR_swapchain:
551 case ProcHook::EXT_hdr_metadata:
552 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
553 case ProcHook::ANDROID_native_buffer:
554 case ProcHook::GOOGLE_display_timing:
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700555 case ProcHook::EXTENSION_CORE_1_0:
556 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700557 case ProcHook::EXTENSION_COUNT:
558 // Device and meta extensions. If we ever get here it's a bug in
559 // our code. But enumerating them lets us avoid having a default
560 // case, and default hides other bugs.
561 ALOGE(
562 "CreateInfoWrapper::FilterExtension: invalid instance "
563 "extension '%s'. FIX ME",
564 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800565 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700566
567 // Don't use a default case. Without it, -Wswitch will tell us
568 // at compile time if someone adds a new ProcHook extension but
569 // doesn't handle it above. That's a real bug that has
570 // not-immediately-obvious effects.
571 //
572 // default:
573 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800574 }
575 } else {
576 switch (ext_bit) {
577 case ProcHook::KHR_swapchain:
578 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
579 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
580 ext_bit = ProcHook::ANDROID_native_buffer;
581 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700582 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700583 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300584 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700585 hook_extensions_.set(ext_bit);
586 // return now as these extensions do not require HAL support
587 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700588 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700589 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700590 hook_extensions_.set(ext_bit);
591 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700592 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800593 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700594 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800595 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700596
597 case ProcHook::KHR_android_surface:
598 case ProcHook::KHR_get_physical_device_properties2:
599 case ProcHook::KHR_get_surface_capabilities2:
600 case ProcHook::KHR_surface:
601 case ProcHook::EXT_debug_report:
602 case ProcHook::EXT_swapchain_colorspace:
603 case ProcHook::ANDROID_native_buffer:
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700604 case ProcHook::EXTENSION_CORE_1_0:
605 case ProcHook::EXTENSION_CORE_1_1:
Jesse Hall7f983a82018-03-29 14:46:45 -0700606 case ProcHook::EXTENSION_COUNT:
607 // Instance and meta extensions. If we ever get here it's a bug
608 // in our code. But enumerating them lets us avoid having a
609 // default case, and default hides other bugs.
610 ALOGE(
611 "CreateInfoWrapper::FilterExtension: invalid device "
612 "extension '%s'. FIX ME",
613 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800614 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700615
616 // Don't use a default case. Without it, -Wswitch will tell us
617 // at compile time if someone adds a new ProcHook extension but
618 // doesn't handle it above. That's a real bug that has
619 // not-immediately-obvious effects.
620 //
621 // default:
622 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800623 }
624 }
625
626 for (uint32_t i = 0; i < filter.ext_count; i++) {
627 const VkExtensionProperties& props = filter.exts[i];
628 // ignore unknown extensions
629 if (strcmp(name, props.extensionName) != 0)
630 continue;
631
Chia-I Wu4901db72016-03-24 16:38:58 +0800632 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800633 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
634 if (ext_bit == ProcHook::ANDROID_native_buffer)
635 hook_extensions_.set(ProcHook::KHR_swapchain);
636
637 hal_extensions_.set(ext_bit);
638 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800639
640 break;
641 }
642}
643
Ian Elliottf3e872d2017-11-02 10:15:13 -0600644void CreateInfoWrapper::DowngradeApiVersion() {
645 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
646 if (instance_info_.pApplicationInfo) {
647 application_info_ = *instance_info_.pApplicationInfo;
648 instance_info_.pApplicationInfo = &application_info_;
649 application_info_.apiVersion = VK_API_VERSION_1_0;
650 }
651}
652
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700653void CreateInfoWrapper::UpgradeDeviceCoreApiVersion(uint32_t api_version) {
654 ALOG_ASSERT(!is_instance_, "Device only API called by instance wrapper.");
655#pragma clang diagnostic push
656#pragma clang diagnostic ignored "-Wold-style-cast"
657 api_version ^= VK_VERSION_PATCH(api_version);
658#pragma clang diagnostic pop
659 // cap the API version to the loader supported highest version
660 if (api_version > VK_API_VERSION_1_1)
661 api_version = VK_API_VERSION_1_1;
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
698 // TODO(jessehall): Right now we never shrink allocations; if the new
699 // 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 Zhangec6c5052019-10-17 15:53:00 -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) {
Ian Elliott34a327b2017-03-28 13:20:35 -0600843
844 android::Vector<VkExtensionProperties> loader_extensions;
845 loader_extensions.push_back({
846 VK_KHR_SURFACE_EXTENSION_NAME,
847 VK_KHR_SURFACE_SPEC_VERSION});
848 loader_extensions.push_back({
849 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
850 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
851 loader_extensions.push_back({
852 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
853 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700854 loader_extensions.push_back({
855 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
856 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600857
Chia-I Wu31938252016-05-23 15:31:02 +0800858 static const VkExtensionProperties loader_debug_report_extension = {
859 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
860 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800861
862 // enumerate our extensions first
863 if (!pLayerName && pProperties) {
864 uint32_t count = std::min(
865 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
866
867 std::copy_n(loader_extensions.begin(), count, pProperties);
868
869 if (count < loader_extensions.size()) {
870 *pPropertyCount = count;
871 return VK_INCOMPLETE;
872 }
873
874 pProperties += count;
875 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800876
877 if (Hal::Get().GetDebugReportIndex() < 0) {
878 if (!*pPropertyCount) {
879 *pPropertyCount = count;
880 return VK_INCOMPLETE;
881 }
882
883 pProperties[0] = loader_debug_report_extension;
884 pProperties += 1;
885 *pPropertyCount -= 1;
886 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800887 }
888
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800889 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800890 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800891 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800892 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800893
Chia-I Wu31938252016-05-23 15:31:02 +0800894 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
895 int idx = Hal::Get().GetDebugReportIndex();
896 if (idx < 0) {
897 *pPropertyCount += 1;
898 } else if (pProperties &&
899 static_cast<uint32_t>(idx) < *pPropertyCount) {
900 pProperties[idx].specVersion =
901 std::min(pProperties[idx].specVersion,
902 loader_debug_report_extension.specVersion);
903 }
904
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800905 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800906 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800907
908 return result;
909}
910
Chris Forbesfa25e632017-02-22 12:36:02 +1300911bool QueryPresentationProperties(
912 VkPhysicalDevice physicalDevice,
913 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties)
914{
915 const InstanceData& data = GetData(physicalDevice);
916
917 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700918 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
919 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300920 return false;
921
922 // Request the android-specific presentation properties via GPDP2
923 VkPhysicalDeviceProperties2KHR properties = {
924 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
925 presentation_properties,
926 {}
927 };
928
929#pragma clang diagnostic push
930#pragma clang diagnostic ignored "-Wold-style-cast"
931 presentation_properties->sType =
932 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
933#pragma clang diagnostic pop
934 presentation_properties->pNext = nullptr;
935 presentation_properties->sharedImage = VK_FALSE;
936
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700937 if (data.driver.GetPhysicalDeviceProperties2KHR) {
938 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
939 &properties);
940 } else {
941 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
942 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300943
944 return true;
945}
946
Chia-I Wu01cf3052016-03-24 16:16:21 +0800947VkResult EnumerateDeviceExtensionProperties(
948 VkPhysicalDevice physicalDevice,
949 const char* pLayerName,
950 uint32_t* pPropertyCount,
951 VkExtensionProperties* pProperties) {
952 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300953 // extensions that are unconditionally exposed by the loader
954 android::Vector<VkExtensionProperties> loader_extensions;
955 loader_extensions.push_back({
956 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
957 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300958
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800959 bool hdrBoardConfig =
960 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(
961 false);
962 if (hdrBoardConfig) {
963 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
964 VK_EXT_HDR_METADATA_SPEC_VERSION});
965 }
966
Chris Forbes16095002017-05-05 15:33:29 -0700967 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
968 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
969 presentation_properties.sharedImage) {
970 loader_extensions.push_back({
971 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
972 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300973 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700974
Ian Elliott5c34de22017-04-10 14:42:30 -0600975 // conditionally add VK_GOOGLE_display_timing if present timestamps are
976 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700977 const std::string timestamp_property("service.sf.present_timestamp");
978 android::base::WaitForPropertyCreation(timestamp_property);
979 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600980 loader_extensions.push_back({
981 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
982 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
983 }
984
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700985 // enumerate our extensions first
986 if (!pLayerName && pProperties) {
987 uint32_t count = std::min(
988 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
989
990 std::copy_n(loader_extensions.begin(), count, pProperties);
991
992 if (count < loader_extensions.size()) {
993 *pPropertyCount = count;
994 return VK_INCOMPLETE;
995 }
996
997 pProperties += count;
998 *pPropertyCount -= count;
999 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001000
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001001 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +08001002 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
1003 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001004 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001005
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001006 if (pProperties) {
1007 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1008 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1009 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001010
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001011 if (strcmp(prop.extensionName,
1012 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1013 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001014
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001015 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1016 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001017
1018 if (prop.specVersion >= 8) {
1019 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1020 } else {
1021 prop.specVersion = 68;
1022 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001023 }
1024 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001025
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001026 // restore loader extension count
1027 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1028 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001029 }
1030
1031 return result;
1032}
1033
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001034VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1035 const VkAllocationCallbacks* pAllocator,
1036 VkInstance* pInstance) {
1037 const VkAllocationCallbacks& data_allocator =
1038 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1039
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001040 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001041 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001042 if (result != VK_SUCCESS)
1043 return result;
1044
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001045 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001046 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001047 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001048 if (!data)
1049 return VK_ERROR_OUT_OF_HOST_MEMORY;
1050
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001051 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001052
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001053 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001054#pragma clang diagnostic push
1055#pragma clang diagnostic ignored "-Wold-style-cast"
1056 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1057 ? pCreateInfo->pApplicationInfo->apiVersion
1058 : VK_API_VERSION_1_0);
1059 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1060 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1061 uint32_t icd_api_version;
1062 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1063 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001064 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001065 "vkEnumerateInstanceVersion"));
1066 if (!pfn_enumerate_instance_version) {
1067 icd_api_version = VK_API_VERSION_1_0;
1068 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001069 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001070 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001071 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001072 }
1073 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1074 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1075
1076 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1077 ((api_major_version > 1) || (api_minor_version > 0))) {
1078 api_version = VK_API_VERSION_1_0;
1079 wrapper.DowngradeApiVersion();
1080 }
1081#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001082 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001083
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001084 // call into the driver
1085 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001086 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001087 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001088 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1089 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001090 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001091 if (result != VK_SUCCESS) {
1092 FreeInstanceData(data, data_allocator);
1093 return result;
1094 }
1095
1096 // initialize InstanceDriverTable
1097 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001098 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001099 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001100 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001101 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001102 if (data->driver.DestroyInstance)
1103 data->driver.DestroyInstance(instance, pAllocator);
1104
1105 FreeInstanceData(data, data_allocator);
1106
1107 return VK_ERROR_INCOMPATIBLE_DRIVER;
1108 }
1109
1110 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001111 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001112 if (!data->get_device_proc_addr) {
1113 data->driver.DestroyInstance(instance, pAllocator);
1114 FreeInstanceData(data, data_allocator);
1115
1116 return VK_ERROR_INCOMPATIBLE_DRIVER;
1117 }
1118
1119 *pInstance = instance;
1120
1121 return VK_SUCCESS;
1122}
1123
1124void DestroyInstance(VkInstance instance,
1125 const VkAllocationCallbacks* pAllocator) {
1126 InstanceData& data = GetData(instance);
1127 data.driver.DestroyInstance(instance, pAllocator);
1128
1129 VkAllocationCallbacks local_allocator;
1130 if (!pAllocator) {
1131 local_allocator = data.allocator;
1132 pAllocator = &local_allocator;
1133 }
1134
1135 FreeInstanceData(&data, *pAllocator);
1136}
1137
Chia-I Wu4901db72016-03-24 16:38:58 +08001138VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1139 const VkDeviceCreateInfo* pCreateInfo,
1140 const VkAllocationCallbacks* pAllocator,
1141 VkDevice* pDevice) {
1142 const InstanceData& instance_data = GetData(physicalDevice);
1143 const VkAllocationCallbacks& data_allocator =
1144 (pAllocator) ? *pAllocator : instance_data.allocator;
1145
1146 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001147 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001148 if (result != VK_SUCCESS)
1149 return result;
1150
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001151 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001152 DeviceData* data = AllocateDeviceData(data_allocator,
1153 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001154 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001155 if (!data)
1156 return VK_ERROR_OUT_OF_HOST_MEMORY;
1157
Yiwei Zhangec6c5052019-10-17 15:53:00 -07001158 VkPhysicalDeviceProperties properties;
1159 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1160 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1161 &properties);
1162 ATRACE_END();
1163
1164 wrapper.UpgradeDeviceCoreApiVersion(properties.apiVersion);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001165 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001166
1167 // call into the driver
1168 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001169 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001170 result = instance_data.driver.CreateDevice(
1171 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1172 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001173 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001174 if (result != VK_SUCCESS) {
1175 FreeDeviceData(data, data_allocator);
1176 return result;
1177 }
1178
1179 // initialize DeviceDriverTable
1180 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001181 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1182 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001183 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1184 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1185 if (data->driver.DestroyDevice)
1186 data->driver.DestroyDevice(dev, pAllocator);
1187
1188 FreeDeviceData(data, data_allocator);
1189
1190 return VK_ERROR_INCOMPATIBLE_DRIVER;
1191 }
Chris Forbesd8277912017-02-10 14:59:59 +13001192
1193 // sanity check ANDROID_native_buffer implementation, whose set of
1194 // entrypoints varies according to the spec version.
1195 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1196 !data->driver.GetSwapchainGrallocUsageANDROID &&
1197 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1198 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1199 " must expose at least one of "
1200 "vkGetSwapchainGrallocUsageANDROID or "
1201 "vkGetSwapchainGrallocUsage2ANDROID");
1202
1203 data->driver.DestroyDevice(dev, pAllocator);
1204 FreeDeviceData(data, data_allocator);
1205
1206 return VK_ERROR_INCOMPATIBLE_DRIVER;
1207 }
1208
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001209 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1210 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangb1c1a372019-07-03 13:39:32 -07001211 android::GraphicsEnv::getInstance().setTargetStats(
1212 android::GraphicsEnv::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001213 }
1214
Jesse Halldc225072016-05-30 22:40:14 -07001215 data->driver_device = dev;
Jesse Hall85bb0c52017-02-09 22:13:02 -08001216 data->driver_version = properties.driverVersion;
Chia-I Wu4901db72016-03-24 16:38:58 +08001217
1218 *pDevice = dev;
1219
1220 return VK_SUCCESS;
1221}
1222
1223void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1224 DeviceData& data = GetData(device);
1225 data.driver.DestroyDevice(device, pAllocator);
1226
1227 VkAllocationCallbacks local_allocator;
1228 if (!pAllocator) {
1229 local_allocator = data.allocator;
1230 pAllocator = &local_allocator;
1231 }
1232
1233 FreeDeviceData(&data, *pAllocator);
1234}
1235
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001236VkResult EnumeratePhysicalDevices(VkInstance instance,
1237 uint32_t* pPhysicalDeviceCount,
1238 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001239 ATRACE_CALL();
1240
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001241 const auto& data = GetData(instance);
1242
1243 VkResult result = data.driver.EnumeratePhysicalDevices(
1244 instance, pPhysicalDeviceCount, pPhysicalDevices);
1245 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1246 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1247 SetData(pPhysicalDevices[i], data);
1248 }
1249
1250 return result;
1251}
1252
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001253VkResult EnumeratePhysicalDeviceGroups(
1254 VkInstance instance,
1255 uint32_t* pPhysicalDeviceGroupCount,
1256 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001257 ATRACE_CALL();
1258
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001259 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001260 const auto& data = GetData(instance);
1261
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001262 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1263 uint32_t device_count = 0;
1264 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1265 if (result < 0)
1266 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001267
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001268 if (!pPhysicalDeviceGroupProperties) {
1269 *pPhysicalDeviceGroupCount = device_count;
1270 return result;
1271 }
1272
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001273 if (!device_count) {
1274 *pPhysicalDeviceGroupCount = 0;
1275 return result;
1276 }
Chad Versace32c087f2018-09-09 07:28:05 -07001277 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1278 if (!device_count)
1279 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001280
1281 android::Vector<VkPhysicalDevice> devices;
1282 devices.resize(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001283 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001284 result = EnumeratePhysicalDevices(instance, &device_count,
1285 devices.editArray());
1286 if (result < 0)
1287 return result;
1288
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001289 for (uint32_t i = 0; i < device_count; ++i) {
1290 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1291 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1292 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1293 }
1294 } else {
1295 result = data.driver.EnumeratePhysicalDeviceGroups(
1296 instance, pPhysicalDeviceGroupCount,
1297 pPhysicalDeviceGroupProperties);
1298 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1299 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1300 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1301 for (uint32_t j = 0;
1302 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1303 j++) {
1304 SetData(
1305 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001306 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001307 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001308 }
1309 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001310 }
1311
1312 return result;
1313}
1314
Chia-I Wuba0be412016-03-24 16:24:40 +08001315void GetDeviceQueue(VkDevice device,
1316 uint32_t queueFamilyIndex,
1317 uint32_t queueIndex,
1318 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001319 ATRACE_CALL();
1320
Chia-I Wuba0be412016-03-24 16:24:40 +08001321 const auto& data = GetData(device);
1322
1323 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1324 SetData(*pQueue, data);
1325}
1326
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001327void GetDeviceQueue2(VkDevice device,
1328 const VkDeviceQueueInfo2* pQueueInfo,
1329 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001330 ATRACE_CALL();
1331
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001332 const auto& data = GetData(device);
1333
1334 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001335 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001336}
1337
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001338VKAPI_ATTR VkResult
1339AllocateCommandBuffers(VkDevice device,
1340 const VkCommandBufferAllocateInfo* pAllocateInfo,
1341 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001342 ATRACE_CALL();
1343
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001344 const auto& data = GetData(device);
1345
1346 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1347 pCommandBuffers);
1348 if (result == VK_SUCCESS) {
1349 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1350 SetData(pCommandBuffers[i], data);
1351 }
1352
1353 return result;
1354}
1355
Chia-I Wu9d518162016-03-24 14:55:27 +08001356} // namespace driver
1357} // namespace vulkan