blob: 6ddb9d9b9a3fce2f5af04e3147b2aa494f663268 [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 Wu136b8eb2016-03-24 15:01:52 +0800758bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800759 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800760}
761
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800762const VkAllocationCallbacks& GetDefaultAllocator() {
763 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
764 .pUserData = nullptr,
765 .pfnAllocation = DefaultAllocate,
766 .pfnReallocation = DefaultReallocate,
767 .pfnFree = DefaultFree,
768 };
769
770 return kDefaultAllocCallbacks;
771}
772
Chia-I Wueb7db122016-03-24 09:11:06 +0800773PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
774 const ProcHook* hook = GetProcHook(pName);
775 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800776 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800777
778 if (!instance) {
779 if (hook->type == ProcHook::GLOBAL)
780 return hook->proc;
781
Chia-I Wu109f8982016-04-22 06:40:40 +0800782 // v0 layers expect
783 //
784 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
785 //
786 // to work.
787 if (strcmp(pName, "vkCreateDevice") == 0)
788 return hook->proc;
789
Chia-I Wueb7db122016-03-24 09:11:06 +0800790 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800791 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800792 pName);
793
Chia-I Wu109f8982016-04-22 06:40:40 +0800794 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800795 }
796
797 PFN_vkVoidFunction proc;
798
799 switch (hook->type) {
800 case ProcHook::INSTANCE:
801 proc = (GetData(instance).hook_extensions[hook->extension])
802 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800803 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800804 break;
805 case ProcHook::DEVICE:
Yiwei Zhangec6c5052019-10-17 15:53:00 -0700806 proc = (hook->extension == ProcHook::EXTENSION_CORE_1_0)
Chia-I Wueb7db122016-03-24 09:11:06 +0800807 ? hook->proc
808 : hook->checked_proc;
809 break;
810 default:
811 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800812 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800813 pName);
814 proc = nullptr;
815 break;
816 }
817
818 return proc;
819}
820
821PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
822 const ProcHook* hook = GetProcHook(pName);
823 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800824 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800825
826 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800827 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800828 return nullptr;
829 }
830
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800831 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
832 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800833}
834
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800835VkResult EnumerateInstanceExtensionProperties(
836 const char* pLayerName,
837 uint32_t* pPropertyCount,
838 VkExtensionProperties* pProperties) {
Ian Elliott34a327b2017-03-28 13:20:35 -0600839
840 android::Vector<VkExtensionProperties> loader_extensions;
841 loader_extensions.push_back({
842 VK_KHR_SURFACE_EXTENSION_NAME,
843 VK_KHR_SURFACE_SPEC_VERSION});
844 loader_extensions.push_back({
845 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
846 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
847 loader_extensions.push_back({
848 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
849 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700850 loader_extensions.push_back({
851 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
852 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600853
Chia-I Wu31938252016-05-23 15:31:02 +0800854 static const VkExtensionProperties loader_debug_report_extension = {
855 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
856 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800857
858 // enumerate our extensions first
859 if (!pLayerName && pProperties) {
860 uint32_t count = std::min(
861 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
862
863 std::copy_n(loader_extensions.begin(), count, pProperties);
864
865 if (count < loader_extensions.size()) {
866 *pPropertyCount = count;
867 return VK_INCOMPLETE;
868 }
869
870 pProperties += count;
871 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800872
873 if (Hal::Get().GetDebugReportIndex() < 0) {
874 if (!*pPropertyCount) {
875 *pPropertyCount = count;
876 return VK_INCOMPLETE;
877 }
878
879 pProperties[0] = loader_debug_report_extension;
880 pProperties += 1;
881 *pPropertyCount -= 1;
882 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800883 }
884
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800885 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800886 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800887 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800888 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800889
Chia-I Wu31938252016-05-23 15:31:02 +0800890 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
891 int idx = Hal::Get().GetDebugReportIndex();
892 if (idx < 0) {
893 *pPropertyCount += 1;
894 } else if (pProperties &&
895 static_cast<uint32_t>(idx) < *pPropertyCount) {
896 pProperties[idx].specVersion =
897 std::min(pProperties[idx].specVersion,
898 loader_debug_report_extension.specVersion);
899 }
900
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800901 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800902 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800903
904 return result;
905}
906
Chris Forbesfa25e632017-02-22 12:36:02 +1300907bool QueryPresentationProperties(
908 VkPhysicalDevice physicalDevice,
909 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties)
910{
911 const InstanceData& data = GetData(physicalDevice);
912
913 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700914 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
915 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300916 return false;
917
918 // Request the android-specific presentation properties via GPDP2
919 VkPhysicalDeviceProperties2KHR properties = {
920 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
921 presentation_properties,
922 {}
923 };
924
925#pragma clang diagnostic push
926#pragma clang diagnostic ignored "-Wold-style-cast"
927 presentation_properties->sType =
928 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
929#pragma clang diagnostic pop
930 presentation_properties->pNext = nullptr;
931 presentation_properties->sharedImage = VK_FALSE;
932
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700933 if (data.driver.GetPhysicalDeviceProperties2KHR) {
934 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
935 &properties);
936 } else {
937 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
938 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300939
940 return true;
941}
942
Chia-I Wu01cf3052016-03-24 16:16:21 +0800943VkResult EnumerateDeviceExtensionProperties(
944 VkPhysicalDevice physicalDevice,
945 const char* pLayerName,
946 uint32_t* pPropertyCount,
947 VkExtensionProperties* pProperties) {
948 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300949 // extensions that are unconditionally exposed by the loader
950 android::Vector<VkExtensionProperties> loader_extensions;
951 loader_extensions.push_back({
952 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
953 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300954
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800955 bool hdrBoardConfig =
956 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(
957 false);
958 if (hdrBoardConfig) {
959 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
960 VK_EXT_HDR_METADATA_SPEC_VERSION});
961 }
962
Chris Forbes16095002017-05-05 15:33:29 -0700963 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
964 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
965 presentation_properties.sharedImage) {
966 loader_extensions.push_back({
967 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
968 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300969 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700970
Ian Elliott5c34de22017-04-10 14:42:30 -0600971 // conditionally add VK_GOOGLE_display_timing if present timestamps are
972 // supported by the driver:
Yiwei Zhang98d15e02020-08-15 13:48:36 -0700973 if (android::base::GetBoolProperty("service.sf.present_timestamp", false)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600974 loader_extensions.push_back({
975 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
976 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
977 }
978
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700979 // enumerate our extensions first
980 if (!pLayerName && pProperties) {
981 uint32_t count = std::min(
982 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
983
984 std::copy_n(loader_extensions.begin(), count, pProperties);
985
986 if (count < loader_extensions.size()) {
987 *pPropertyCount = count;
988 return VK_INCOMPLETE;
989 }
990
991 pProperties += count;
992 *pPropertyCount -= count;
993 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800994
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800995 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +0800996 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
997 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800998 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800999
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001000 if (pProperties) {
1001 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
1002 for (uint32_t i = 0; i < *pPropertyCount; i++) {
1003 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +08001004
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001005 if (strcmp(prop.extensionName,
1006 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
1007 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +08001008
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001009 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
1010 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -07001011
1012 if (prop.specVersion >= 8) {
1013 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
1014 } else {
1015 prop.specVersion = 68;
1016 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001017 }
1018 }
Chia-I Wu01cf3052016-03-24 16:16:21 +08001019
Ian Elliottd4b50aa2017-01-09 16:21:36 -07001020 // restore loader extension count
1021 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
1022 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +08001023 }
1024
1025 return result;
1026}
1027
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001028VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1029 const VkAllocationCallbacks* pAllocator,
1030 VkInstance* pInstance) {
1031 const VkAllocationCallbacks& data_allocator =
1032 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1033
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001034 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001035 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001036 if (result != VK_SUCCESS)
1037 return result;
1038
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001039 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001040 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001041 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001042 if (!data)
1043 return VK_ERROR_OUT_OF_HOST_MEMORY;
1044
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001045 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001046
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001047 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001048#pragma clang diagnostic push
1049#pragma clang diagnostic ignored "-Wold-style-cast"
1050 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1051 ? pCreateInfo->pApplicationInfo->apiVersion
1052 : VK_API_VERSION_1_0);
1053 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1054 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1055 uint32_t icd_api_version;
1056 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1057 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001058 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001059 "vkEnumerateInstanceVersion"));
1060 if (!pfn_enumerate_instance_version) {
1061 icd_api_version = VK_API_VERSION_1_0;
1062 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001063 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001064 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001065 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001066 }
1067 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1068 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1069
1070 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1071 ((api_major_version > 1) || (api_minor_version > 0))) {
1072 api_version = VK_API_VERSION_1_0;
1073 wrapper.DowngradeApiVersion();
1074 }
1075#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001076 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001077
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001078 // call into the driver
1079 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001080 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001081 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001082 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1083 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001084 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001085 if (result != VK_SUCCESS) {
1086 FreeInstanceData(data, data_allocator);
1087 return result;
1088 }
1089
1090 // initialize InstanceDriverTable
1091 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001092 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001093 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001094 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001095 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001096 if (data->driver.DestroyInstance)
1097 data->driver.DestroyInstance(instance, pAllocator);
1098
1099 FreeInstanceData(data, data_allocator);
1100
1101 return VK_ERROR_INCOMPATIBLE_DRIVER;
1102 }
1103
1104 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001105 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001106 if (!data->get_device_proc_addr) {
1107 data->driver.DestroyInstance(instance, pAllocator);
1108 FreeInstanceData(data, data_allocator);
1109
1110 return VK_ERROR_INCOMPATIBLE_DRIVER;
1111 }
1112
1113 *pInstance = instance;
1114
1115 return VK_SUCCESS;
1116}
1117
1118void DestroyInstance(VkInstance instance,
1119 const VkAllocationCallbacks* pAllocator) {
1120 InstanceData& data = GetData(instance);
1121 data.driver.DestroyInstance(instance, pAllocator);
1122
1123 VkAllocationCallbacks local_allocator;
1124 if (!pAllocator) {
1125 local_allocator = data.allocator;
1126 pAllocator = &local_allocator;
1127 }
1128
1129 FreeInstanceData(&data, *pAllocator);
1130}
1131
Chia-I Wu4901db72016-03-24 16:38:58 +08001132VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1133 const VkDeviceCreateInfo* pCreateInfo,
1134 const VkAllocationCallbacks* pAllocator,
1135 VkDevice* pDevice) {
1136 const InstanceData& instance_data = GetData(physicalDevice);
1137 const VkAllocationCallbacks& data_allocator =
1138 (pAllocator) ? *pAllocator : instance_data.allocator;
1139
1140 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001141 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001142 if (result != VK_SUCCESS)
1143 return result;
1144
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001145 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001146 DeviceData* data = AllocateDeviceData(data_allocator,
1147 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001148 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001149 if (!data)
1150 return VK_ERROR_OUT_OF_HOST_MEMORY;
1151
Yiwei Zhangec6c5052019-10-17 15:53:00 -07001152 VkPhysicalDeviceProperties properties;
1153 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
1154 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1155 &properties);
1156 ATRACE_END();
1157
1158 wrapper.UpgradeDeviceCoreApiVersion(properties.apiVersion);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001159 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001160
1161 // call into the driver
1162 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001163 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001164 result = instance_data.driver.CreateDevice(
1165 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1166 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001167 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001168 if (result != VK_SUCCESS) {
1169 FreeDeviceData(data, data_allocator);
1170 return result;
1171 }
1172
1173 // initialize DeviceDriverTable
1174 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001175 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1176 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001177 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1178 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1179 if (data->driver.DestroyDevice)
1180 data->driver.DestroyDevice(dev, pAllocator);
1181
1182 FreeDeviceData(data, data_allocator);
1183
1184 return VK_ERROR_INCOMPATIBLE_DRIVER;
1185 }
Chris Forbesd8277912017-02-10 14:59:59 +13001186
1187 // sanity check ANDROID_native_buffer implementation, whose set of
1188 // entrypoints varies according to the spec version.
1189 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1190 !data->driver.GetSwapchainGrallocUsageANDROID &&
1191 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1192 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1193 " must expose at least one of "
1194 "vkGetSwapchainGrallocUsageANDROID or "
1195 "vkGetSwapchainGrallocUsage2ANDROID");
1196
1197 data->driver.DestroyDevice(dev, pAllocator);
1198 FreeDeviceData(data, data_allocator);
1199
1200 return VK_ERROR_INCOMPATIBLE_DRIVER;
1201 }
1202
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001203 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1204 // Log that the app is hitting software Vulkan implementation
Yiwei Zhangb1c1a372019-07-03 13:39:32 -07001205 android::GraphicsEnv::getInstance().setTargetStats(
1206 android::GraphicsEnv::Stats::CPU_VULKAN_IN_USE);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001207 }
1208
Jesse Halldc225072016-05-30 22:40:14 -07001209 data->driver_device = dev;
Jesse Hall85bb0c52017-02-09 22:13:02 -08001210 data->driver_version = properties.driverVersion;
Chia-I Wu4901db72016-03-24 16:38:58 +08001211
1212 *pDevice = dev;
1213
1214 return VK_SUCCESS;
1215}
1216
1217void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1218 DeviceData& data = GetData(device);
1219 data.driver.DestroyDevice(device, pAllocator);
1220
1221 VkAllocationCallbacks local_allocator;
1222 if (!pAllocator) {
1223 local_allocator = data.allocator;
1224 pAllocator = &local_allocator;
1225 }
1226
1227 FreeDeviceData(&data, *pAllocator);
1228}
1229
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001230VkResult EnumeratePhysicalDevices(VkInstance instance,
1231 uint32_t* pPhysicalDeviceCount,
1232 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001233 ATRACE_CALL();
1234
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001235 const auto& data = GetData(instance);
1236
1237 VkResult result = data.driver.EnumeratePhysicalDevices(
1238 instance, pPhysicalDeviceCount, pPhysicalDevices);
1239 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1240 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1241 SetData(pPhysicalDevices[i], data);
1242 }
1243
1244 return result;
1245}
1246
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001247VkResult EnumeratePhysicalDeviceGroups(
1248 VkInstance instance,
1249 uint32_t* pPhysicalDeviceGroupCount,
1250 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001251 ATRACE_CALL();
1252
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001253 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001254 const auto& data = GetData(instance);
1255
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001256 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1257 uint32_t device_count = 0;
1258 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1259 if (result < 0)
1260 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001261
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001262 if (!pPhysicalDeviceGroupProperties) {
1263 *pPhysicalDeviceGroupCount = device_count;
1264 return result;
1265 }
1266
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001267 if (!device_count) {
1268 *pPhysicalDeviceGroupCount = 0;
1269 return result;
1270 }
Chad Versace32c087f2018-09-09 07:28:05 -07001271 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1272 if (!device_count)
1273 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001274
1275 android::Vector<VkPhysicalDevice> devices;
1276 devices.resize(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001277 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001278 result = EnumeratePhysicalDevices(instance, &device_count,
1279 devices.editArray());
1280 if (result < 0)
1281 return result;
1282
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001283 for (uint32_t i = 0; i < device_count; ++i) {
1284 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1285 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1286 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1287 }
1288 } else {
1289 result = data.driver.EnumeratePhysicalDeviceGroups(
1290 instance, pPhysicalDeviceGroupCount,
1291 pPhysicalDeviceGroupProperties);
1292 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1293 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1294 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1295 for (uint32_t j = 0;
1296 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1297 j++) {
1298 SetData(
1299 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001300 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001301 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001302 }
1303 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001304 }
1305
1306 return result;
1307}
1308
Chia-I Wuba0be412016-03-24 16:24:40 +08001309void GetDeviceQueue(VkDevice device,
1310 uint32_t queueFamilyIndex,
1311 uint32_t queueIndex,
1312 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001313 ATRACE_CALL();
1314
Chia-I Wuba0be412016-03-24 16:24:40 +08001315 const auto& data = GetData(device);
1316
1317 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1318 SetData(*pQueue, data);
1319}
1320
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001321void GetDeviceQueue2(VkDevice device,
1322 const VkDeviceQueueInfo2* pQueueInfo,
1323 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001324 ATRACE_CALL();
1325
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001326 const auto& data = GetData(device);
1327
1328 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001329 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001330}
1331
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001332VKAPI_ATTR VkResult
1333AllocateCommandBuffers(VkDevice device,
1334 const VkCommandBufferAllocateInfo* pAllocateInfo,
1335 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001336 ATRACE_CALL();
1337
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001338 const auto& data = GetData(device);
1339
1340 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1341 pCommandBuffers);
1342 if (result == VK_SUCCESS) {
1343 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1344 SetData(pCommandBuffers[i], data);
1345 }
1346
1347 return result;
1348}
1349
Chia-I Wu9d518162016-03-24 14:55:27 +08001350} // namespace driver
1351} // namespace vulkan