blob: d33e5528e83e047a3d222a2b268c92cf73b956ef [file] [log] [blame]
Chia-I Wu9d518162016-03-24 14:55:27 +08001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Yiwei Zhang5e862202019-06-21 14:59:16 -070019#include "driver.h"
20
21#include <dlfcn.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070022#include <malloc.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080023#include <stdlib.h>
24#include <string.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080025
Jesse Hall53457db2016-12-14 16:54:06 -080026#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080027#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070028#include <android-base/properties.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080029#include <configstore/Utils.h>
Jesse Hall53457db2016-12-14 16:54:06 -080030#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhang5e862202019-06-21 14:59:16 -070032#include <log/log.h>
33#include <sys/prctl.h>
Yiwei Zhangd9861812019-02-13 11:51:55 -080034#include <utils/Timers.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080035#include <utils/Trace.h>
Jesse Hall53457db2016-12-14 16:54:06 -080036
Yiwei Zhang5e862202019-06-21 14:59:16 -070037#include <algorithm>
38#include <array>
39#include <new>
40#include <vector>
Wei Wangf9b05ee2017-07-19 20:59:39 -070041
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070042#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080043
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080044using namespace android::hardware::configstore;
45using namespace android::hardware::configstore::V1_0;
46
Jesse Hall00e61ff2017-04-07 16:48:02 -070047// TODO(b/37049319) Get this from a header once one exists
48extern "C" {
49android_namespace_t* android_get_exported_namespace(const char*);
50}
51
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080052// #define ENABLE_ALLOC_CALLSTACKS 1
53#if ENABLE_ALLOC_CALLSTACKS
54#include <utils/CallStack.h>
55#define ALOGD_CALLSTACK(...) \
56 do { \
57 ALOGD(__VA_ARGS__); \
58 android::CallStack callstack; \
59 callstack.update(); \
60 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
61 } while (false)
62#else
63#define ALOGD_CALLSTACK(...) \
64 do { \
65 } while (false)
66#endif
67
Chia-I Wu9d518162016-03-24 14:55:27 +080068namespace vulkan {
69namespace driver {
70
Chia-I Wu136b8eb2016-03-24 15:01:52 +080071namespace {
72
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080073class Hal {
74 public:
75 static bool Open();
76
77 static const Hal& Get() { return hal_; }
78 static const hwvulkan_device_t& Device() { return *Get().dev_; }
79
Chia-I Wu31938252016-05-23 15:31:02 +080080 int GetDebugReportIndex() const { return debug_report_index_; }
81
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080082 private:
Chia-I Wu31938252016-05-23 15:31:02 +080083 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080084 Hal(const Hal&) = delete;
85 Hal& operator=(const Hal&) = delete;
86
Chia-I Wu31938252016-05-23 15:31:02 +080087 bool InitDebugReportIndex();
88
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080089 static Hal hal_;
90
91 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080092 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080093};
94
Chia-I Wu4901db72016-03-24 16:38:58 +080095class CreateInfoWrapper {
96 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080097 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080098 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080099 CreateInfoWrapper(VkPhysicalDevice physical_dev,
100 const VkDeviceCreateInfo& create_info,
101 const VkAllocationCallbacks& allocator);
102 ~CreateInfoWrapper();
103
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800104 VkResult Validate();
Ian Elliottf3e872d2017-11-02 10:15:13 -0600105 void DowngradeApiVersion();
Chia-I Wu4901db72016-03-24 16:38:58 +0800106
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800107 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
108 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800109
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800110 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800111 explicit operator const VkDeviceCreateInfo*() const;
112
113 private:
114 struct ExtensionFilter {
115 VkExtensionProperties* exts;
116 uint32_t ext_count;
117
118 const char** names;
119 uint32_t name_count;
120 };
121
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800122 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800123
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800124 VkResult SanitizeLayers();
125 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800126
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800127 VkResult QueryExtensionCount(uint32_t& count) const;
128 VkResult EnumerateExtensions(uint32_t& count,
129 VkExtensionProperties* props) const;
130 VkResult InitExtensionFilter();
131 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800132
133 const bool is_instance_;
134 const VkAllocationCallbacks& allocator_;
135
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800136 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800137
138 union {
139 VkInstanceCreateInfo instance_info_;
140 VkDeviceCreateInfo dev_info_;
141 };
142
Ian Elliottf3e872d2017-11-02 10:15:13 -0600143 VkApplicationInfo application_info_;
144
Chia-I Wu4901db72016-03-24 16:38:58 +0800145 ExtensionFilter extension_filter_;
146
147 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
148 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
149};
150
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800151Hal Hal::hal_;
152
Jesse Hall53457db2016-12-14 16:54:06 -0800153void* LoadLibrary(const android_dlextinfo& dlextinfo,
154 const char* subname,
155 int subname_len) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800156 ATRACE_CALL();
157
Jesse Hall53457db2016-12-14 16:54:06 -0800158 const char kLibFormat[] = "vulkan.%*s.so";
159 char* name = static_cast<char*>(
160 alloca(sizeof(kLibFormat) + static_cast<size_t>(subname_len)));
161 sprintf(name, kLibFormat, subname_len, subname);
162 return android_dlopen_ext(name, RTLD_LOCAL | RTLD_NOW, &dlextinfo);
163}
164
165const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
166 "ro.hardware." HWVULKAN_HARDWARE_MODULE_ID,
167 "ro.board.platform",
168}};
169
Jesse Hall00e61ff2017-04-07 16:48:02 -0700170int LoadDriver(android_namespace_t* library_namespace,
171 const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800172 ATRACE_CALL();
173
Jesse Hall53457db2016-12-14 16:54:06 -0800174 const android_dlextinfo dlextinfo = {
175 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700176 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800177 };
Jesse Hall53457db2016-12-14 16:54:06 -0800178 void* so = nullptr;
179 char prop[PROPERTY_VALUE_MAX];
180 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
181 int prop_len = property_get(key, prop, nullptr);
182 if (prop_len > 0) {
183 so = LoadLibrary(dlextinfo, prop, prop_len);
184 if (so)
185 break;
186 }
187 }
188 if (!so)
189 return -ENOENT;
190
Jesse Hall00e61ff2017-04-07 16:48:02 -0700191 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800192 if (!hmi) {
193 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
194 dlclose(so);
195 return -EINVAL;
196 }
197 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
198 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
199 dlclose(so);
200 return -EINVAL;
201 }
202 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700203 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800204 return 0;
205}
206
Jesse Hall00e61ff2017-04-07 16:48:02 -0700207int LoadBuiltinDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800208 ATRACE_CALL();
209
Jesse Hall00e61ff2017-04-07 16:48:02 -0700210 auto ns = android_get_exported_namespace("sphal");
211 if (!ns)
212 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800213 android::GraphicsEnv::getInstance().setDriverToLoad(
214 android::GraphicsEnv::Driver::VULKAN);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700215 return LoadDriver(ns, module);
216}
217
218int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800219 ATRACE_CALL();
220
Jesse Hall00e61ff2017-04-07 16:48:02 -0700221 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
222 if (!ns)
223 return -ENOENT;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800224 android::GraphicsEnv::getInstance().setDriverToLoad(
225 android::GraphicsEnv::Driver::VULKAN_UPDATED);
Jesse Hall00e61ff2017-04-07 16:48:02 -0700226 return LoadDriver(ns, module);
227}
228
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800229bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800230 ATRACE_CALL();
231
Yiwei Zhangd9861812019-02-13 11:51:55 -0800232 const nsecs_t openTime = systemTime();
233
Jesse Halldc225072016-05-30 22:40:14 -0700234 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800235
236 // Use a stub device unless we successfully open a real HAL device.
237 hal_.dev_ = &stubhal::kDevice;
238
Jesse Hall53457db2016-12-14 16:54:06 -0800239 int result;
240 const hwvulkan_module_t* module = nullptr;
241
Jesse Hall00e61ff2017-04-07 16:48:02 -0700242 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800243 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700244 result = LoadBuiltinDriver(&module);
245 if (result != 0) {
246 // -ENOENT means the sphal namespace doesn't exist, not that there
247 // is a problem with the driver.
248 ALOGW_IF(
249 result != -ENOENT,
250 "Failed to load Vulkan driver into sphal namespace. This "
251 "usually means the driver has forbidden library dependencies."
252 "Please fix, this will soon stop working.");
253 result =
254 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
255 reinterpret_cast<const hw_module_t**>(&module));
256 }
Jesse Hall53457db2016-12-14 16:54:06 -0800257 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800258 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800259 android::GraphicsEnv::getInstance().setDriverLoaded(
260 android::GraphicsEnv::Api::API_VK, false, systemTime() - openTime);
Jesse Hall53457db2016-12-14 16:54:06 -0800261 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800262 return true;
263 }
264
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800265
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800266 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800267 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800268 result =
269 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
270 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800271 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800272 if (result != 0) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800273 android::GraphicsEnv::getInstance().setDriverLoaded(
274 android::GraphicsEnv::Api::API_VK, false, systemTime() - openTime);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800275 // Any device with a Vulkan HAL should be able to open the device.
276 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
277 result);
278 return false;
279 }
280
281 hal_.dev_ = device;
282
Chia-I Wu31938252016-05-23 15:31:02 +0800283 hal_.InitDebugReportIndex();
284
Yiwei Zhangd9861812019-02-13 11:51:55 -0800285 android::GraphicsEnv::getInstance().setDriverLoaded(
286 android::GraphicsEnv::Api::API_VK, true, systemTime() - openTime);
287
Chia-I Wu31938252016-05-23 15:31:02 +0800288 return true;
289}
290
291bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800292 ATRACE_CALL();
293
Chia-I Wu31938252016-05-23 15:31:02 +0800294 uint32_t count;
295 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
296 VK_SUCCESS) {
297 ALOGE("failed to get HAL instance extension count");
298 return false;
299 }
300
301 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
302 malloc(sizeof(VkExtensionProperties) * count));
303 if (!exts) {
304 ALOGE("failed to allocate HAL instance extension array");
305 return false;
306 }
307
308 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
309 VK_SUCCESS) {
310 ALOGE("failed to enumerate HAL instance extensions");
311 free(exts);
312 return false;
313 }
314
315 for (uint32_t i = 0; i < count; i++) {
316 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
317 0) {
318 debug_report_index_ = static_cast<int>(i);
319 break;
320 }
321 }
322
323 free(exts);
324
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800325 return true;
326}
327
328CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800329 const VkAllocationCallbacks& allocator)
330 : is_instance_(true),
331 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800332 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800333 instance_info_(create_info),
334 extension_filter_() {
335 hook_extensions_.set(ProcHook::EXTENSION_CORE);
336 hal_extensions_.set(ProcHook::EXTENSION_CORE);
337}
338
Chia-I Wu4901db72016-03-24 16:38:58 +0800339CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
340 const VkDeviceCreateInfo& create_info,
341 const VkAllocationCallbacks& allocator)
342 : is_instance_(false),
343 allocator_(allocator),
344 physical_dev_(physical_dev),
345 dev_info_(create_info),
346 extension_filter_() {
347 hook_extensions_.set(ProcHook::EXTENSION_CORE);
348 hal_extensions_.set(ProcHook::EXTENSION_CORE);
349}
350
351CreateInfoWrapper::~CreateInfoWrapper() {
352 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
353 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
354}
355
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800356VkResult CreateInfoWrapper::Validate() {
357 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800358 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800359 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800360 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800361 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800362
363 return result;
364}
365
366const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800367CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800368 return hook_extensions_;
369}
370
371const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800372CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800373 return hal_extensions_;
374}
375
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800376CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
377 return &instance_info_;
378}
379
Chia-I Wu4901db72016-03-24 16:38:58 +0800380CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
381 return &dev_info_;
382}
383
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800384VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800385 const struct StructHeader {
386 VkStructureType type;
387 const void* next;
388 } * header;
389
390 if (is_instance_) {
391 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
392
393 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
394 while (header &&
395 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
396 header = reinterpret_cast<const StructHeader*>(header->next);
397
398 instance_info_.pNext = header;
399 } else {
400 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
401
402 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
403 while (header &&
404 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
405 header = reinterpret_cast<const StructHeader*>(header->next);
406
407 dev_info_.pNext = header;
408 }
409
410 return VK_SUCCESS;
411}
412
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800413VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800414 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
415 : dev_info_.ppEnabledLayerNames;
416 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
417 : dev_info_.enabledLayerCount;
418
419 // remove all layers
420 layer_names = nullptr;
421 layer_count = 0;
422
423 return VK_SUCCESS;
424}
425
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800426VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800427 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
428 : dev_info_.ppEnabledExtensionNames;
429 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
430 : dev_info_.enabledExtensionCount;
431 if (!ext_count)
432 return VK_SUCCESS;
433
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800434 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800435 if (result != VK_SUCCESS)
436 return result;
437
438 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800439 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800440
Jesse Halld3d887a2018-03-05 13:34:45 -0800441 // Enable device extensions that contain physical-device commands, so that
442 // vkGetInstanceProcAddr will return those physical-device commands.
443 if (is_instance_) {
444 hook_extensions_.set(ProcHook::KHR_swapchain);
445 }
446
Chia-I Wu4901db72016-03-24 16:38:58 +0800447 ext_names = extension_filter_.names;
448 ext_count = extension_filter_.name_count;
449
450 return VK_SUCCESS;
451}
452
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800453VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800454 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800455 return Hal::Device().EnumerateInstanceExtensionProperties(
456 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800457 } else {
458 const auto& driver = GetData(physical_dev_).driver;
459 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
460 &count, nullptr);
461 }
462}
463
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800464VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800465 uint32_t& count,
466 VkExtensionProperties* props) const {
467 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800468 return Hal::Device().EnumerateInstanceExtensionProperties(
469 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800470 } else {
471 const auto& driver = GetData(physical_dev_).driver;
472 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
473 &count, props);
474 }
475}
476
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800477VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800478 // query extension count
479 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800480 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800481 if (result != VK_SUCCESS || count == 0)
482 return result;
483
484 auto& filter = extension_filter_;
485 filter.exts =
486 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
487 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
488 alignof(VkExtensionProperties),
489 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
490 if (!filter.exts)
491 return VK_ERROR_OUT_OF_HOST_MEMORY;
492
493 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800494 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800495 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
496 return result;
497
498 if (!count)
499 return VK_SUCCESS;
500
501 filter.ext_count = count;
502
503 // allocate name array
504 uint32_t enabled_ext_count = (is_instance_)
505 ? instance_info_.enabledExtensionCount
506 : dev_info_.enabledExtensionCount;
507 count = std::min(filter.ext_count, enabled_ext_count);
508 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
509 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
510 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
511 if (!filter.names)
512 return VK_ERROR_OUT_OF_HOST_MEMORY;
513
514 return VK_SUCCESS;
515}
516
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800517void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800518 auto& filter = extension_filter_;
519
520 ProcHook::Extension ext_bit = GetProcHookExtension(name);
521 if (is_instance_) {
522 switch (ext_bit) {
523 case ProcHook::KHR_android_surface:
524 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700525 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300526 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800527 hook_extensions_.set(ext_bit);
528 // return now as these extensions do not require HAL support
529 return;
530 case ProcHook::EXT_debug_report:
531 // both we and HAL can take part in
532 hook_extensions_.set(ext_bit);
533 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300534 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700535 case ProcHook::EXTENSION_UNKNOWN:
536 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800537 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700538
Yiwei Zhang23143102019-04-10 18:24:05 -0700539 case ProcHook::KHR_bind_memory2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700540 case ProcHook::KHR_incremental_present:
541 case ProcHook::KHR_shared_presentable_image:
542 case ProcHook::KHR_swapchain:
543 case ProcHook::EXT_hdr_metadata:
544 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
545 case ProcHook::ANDROID_native_buffer:
546 case ProcHook::GOOGLE_display_timing:
547 case ProcHook::EXTENSION_CORE:
548 case ProcHook::EXTENSION_COUNT:
549 // Device and meta extensions. If we ever get here it's a bug in
550 // our code. But enumerating them lets us avoid having a default
551 // case, and default hides other bugs.
552 ALOGE(
553 "CreateInfoWrapper::FilterExtension: invalid instance "
554 "extension '%s'. FIX ME",
555 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800556 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700557
558 // Don't use a default case. Without it, -Wswitch will tell us
559 // at compile time if someone adds a new ProcHook extension but
560 // doesn't handle it above. That's a real bug that has
561 // not-immediately-obvious effects.
562 //
563 // default:
564 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800565 }
566 } else {
567 switch (ext_bit) {
568 case ProcHook::KHR_swapchain:
569 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
570 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
571 ext_bit = ProcHook::ANDROID_native_buffer;
572 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700573 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700574 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300575 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700576 hook_extensions_.set(ext_bit);
577 // return now as these extensions do not require HAL support
578 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700579 case ProcHook::EXT_hdr_metadata:
Yiwei Zhang23143102019-04-10 18:24:05 -0700580 case ProcHook::KHR_bind_memory2:
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700581 hook_extensions_.set(ext_bit);
582 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700583 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800584 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700585 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800586 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700587
588 case ProcHook::KHR_android_surface:
589 case ProcHook::KHR_get_physical_device_properties2:
590 case ProcHook::KHR_get_surface_capabilities2:
591 case ProcHook::KHR_surface:
592 case ProcHook::EXT_debug_report:
593 case ProcHook::EXT_swapchain_colorspace:
594 case ProcHook::ANDROID_native_buffer:
595 case ProcHook::EXTENSION_CORE:
596 case ProcHook::EXTENSION_COUNT:
597 // Instance and meta extensions. If we ever get here it's a bug
598 // in our code. But enumerating them lets us avoid having a
599 // default case, and default hides other bugs.
600 ALOGE(
601 "CreateInfoWrapper::FilterExtension: invalid device "
602 "extension '%s'. FIX ME",
603 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800604 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700605
606 // Don't use a default case. Without it, -Wswitch will tell us
607 // at compile time if someone adds a new ProcHook extension but
608 // doesn't handle it above. That's a real bug that has
609 // not-immediately-obvious effects.
610 //
611 // default:
612 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800613 }
614 }
615
616 for (uint32_t i = 0; i < filter.ext_count; i++) {
617 const VkExtensionProperties& props = filter.exts[i];
618 // ignore unknown extensions
619 if (strcmp(name, props.extensionName) != 0)
620 continue;
621
Chia-I Wu4901db72016-03-24 16:38:58 +0800622 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800623 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
624 if (ext_bit == ProcHook::ANDROID_native_buffer)
625 hook_extensions_.set(ProcHook::KHR_swapchain);
626
627 hal_extensions_.set(ext_bit);
628 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800629
630 break;
631 }
632}
633
Ian Elliottf3e872d2017-11-02 10:15:13 -0600634void CreateInfoWrapper::DowngradeApiVersion() {
635 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
636 if (instance_info_.pApplicationInfo) {
637 application_info_ = *instance_info_.pApplicationInfo;
638 instance_info_.pApplicationInfo = &application_info_;
639 application_info_.apiVersion = VK_API_VERSION_1_0;
640 }
641}
642
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800643VKAPI_ATTR void* DefaultAllocate(void*,
644 size_t size,
645 size_t alignment,
646 VkSystemAllocationScope) {
647 void* ptr = nullptr;
648 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
649 // additionally requires that it be at least sizeof(void*).
650 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
651 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
652 ret, ptr);
653 return ret == 0 ? ptr : nullptr;
654}
655
656VKAPI_ATTR void* DefaultReallocate(void*,
657 void* ptr,
658 size_t size,
659 size_t alignment,
660 VkSystemAllocationScope) {
661 if (size == 0) {
662 free(ptr);
663 return nullptr;
664 }
665
666 // TODO(jessehall): Right now we never shrink allocations; if the new
667 // request is smaller than the existing chunk, we just continue using it.
668 // Right now the loader never reallocs, so this doesn't matter. If that
669 // changes, or if this code is copied into some other project, this should
670 // probably have a heuristic to allocate-copy-free when doing so will save
671 // "enough" space.
672 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
673 if (size <= old_size)
674 return ptr;
675
676 void* new_ptr = nullptr;
677 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
678 return nullptr;
679 if (ptr) {
680 memcpy(new_ptr, ptr, std::min(old_size, size));
681 free(ptr);
682 }
683 return new_ptr;
684}
685
686VKAPI_ATTR void DefaultFree(void*, void* ptr) {
687 ALOGD_CALLSTACK("Free: %p", ptr);
688 free(ptr);
689}
690
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800691InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
692 void* data_mem = allocator.pfnAllocation(
693 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
694 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
695 if (!data_mem)
696 return nullptr;
697
698 return new (data_mem) InstanceData(allocator);
699}
700
701void FreeInstanceData(InstanceData* data,
702 const VkAllocationCallbacks& allocator) {
703 data->~InstanceData();
704 allocator.pfnFree(allocator.pUserData, data);
705}
706
Chia-I Wu950d6e12016-05-03 09:12:35 +0800707DeviceData* AllocateDeviceData(
708 const VkAllocationCallbacks& allocator,
709 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800710 void* data_mem = allocator.pfnAllocation(
711 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
712 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
713 if (!data_mem)
714 return nullptr;
715
Chia-I Wu950d6e12016-05-03 09:12:35 +0800716 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800717}
718
719void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
720 data->~DeviceData();
721 allocator.pfnFree(allocator.pUserData, data);
722}
723
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800724} // anonymous namespace
725
Chia-I Wu9d518162016-03-24 14:55:27 +0800726bool Debuggable() {
727 return (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) >= 0);
728}
729
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800730bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800731 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800732}
733
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800734const VkAllocationCallbacks& GetDefaultAllocator() {
735 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
736 .pUserData = nullptr,
737 .pfnAllocation = DefaultAllocate,
738 .pfnReallocation = DefaultReallocate,
739 .pfnFree = DefaultFree,
740 };
741
742 return kDefaultAllocCallbacks;
743}
744
Chia-I Wueb7db122016-03-24 09:11:06 +0800745PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
746 const ProcHook* hook = GetProcHook(pName);
747 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800748 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800749
750 if (!instance) {
751 if (hook->type == ProcHook::GLOBAL)
752 return hook->proc;
753
Chia-I Wu109f8982016-04-22 06:40:40 +0800754 // v0 layers expect
755 //
756 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
757 //
758 // to work.
759 if (strcmp(pName, "vkCreateDevice") == 0)
760 return hook->proc;
761
Chia-I Wueb7db122016-03-24 09:11:06 +0800762 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800763 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800764 pName);
765
Chia-I Wu109f8982016-04-22 06:40:40 +0800766 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800767 }
768
769 PFN_vkVoidFunction proc;
770
771 switch (hook->type) {
772 case ProcHook::INSTANCE:
773 proc = (GetData(instance).hook_extensions[hook->extension])
774 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800775 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800776 break;
777 case ProcHook::DEVICE:
778 proc = (hook->extension == ProcHook::EXTENSION_CORE)
779 ? hook->proc
780 : hook->checked_proc;
781 break;
782 default:
783 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800784 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800785 pName);
786 proc = nullptr;
787 break;
788 }
789
790 return proc;
791}
792
793PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
794 const ProcHook* hook = GetProcHook(pName);
795 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800796 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800797
798 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800799 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800800 return nullptr;
801 }
802
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800803 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
804 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800805}
806
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800807VkResult EnumerateInstanceExtensionProperties(
808 const char* pLayerName,
809 uint32_t* pPropertyCount,
810 VkExtensionProperties* pProperties) {
Yiwei Zhang5e862202019-06-21 14:59:16 -0700811 std::vector<VkExtensionProperties> loader_extensions;
Ian Elliott34a327b2017-03-28 13:20:35 -0600812 loader_extensions.push_back({
813 VK_KHR_SURFACE_EXTENSION_NAME,
814 VK_KHR_SURFACE_SPEC_VERSION});
815 loader_extensions.push_back({
816 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
817 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
818 loader_extensions.push_back({
819 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
820 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700821 loader_extensions.push_back({
822 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
823 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600824
Chia-I Wu31938252016-05-23 15:31:02 +0800825 static const VkExtensionProperties loader_debug_report_extension = {
826 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
827 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800828
829 // enumerate our extensions first
830 if (!pLayerName && pProperties) {
831 uint32_t count = std::min(
832 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
833
Yiwei Zhang5e862202019-06-21 14:59:16 -0700834 std::copy_n(loader_extensions.data(), count, pProperties);
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800835
836 if (count < loader_extensions.size()) {
837 *pPropertyCount = count;
838 return VK_INCOMPLETE;
839 }
840
841 pProperties += count;
842 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800843
844 if (Hal::Get().GetDebugReportIndex() < 0) {
845 if (!*pPropertyCount) {
846 *pPropertyCount = count;
847 return VK_INCOMPLETE;
848 }
849
850 pProperties[0] = loader_debug_report_extension;
851 pProperties += 1;
852 *pPropertyCount -= 1;
853 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800854 }
855
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800856 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800857 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800858 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800859 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800860
Chia-I Wu31938252016-05-23 15:31:02 +0800861 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
862 int idx = Hal::Get().GetDebugReportIndex();
863 if (idx < 0) {
864 *pPropertyCount += 1;
865 } else if (pProperties &&
866 static_cast<uint32_t>(idx) < *pPropertyCount) {
867 pProperties[idx].specVersion =
868 std::min(pProperties[idx].specVersion,
869 loader_debug_report_extension.specVersion);
870 }
871
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800872 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800873 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800874
875 return result;
876}
877
Chris Forbesfa25e632017-02-22 12:36:02 +1300878bool QueryPresentationProperties(
879 VkPhysicalDevice physicalDevice,
Yiwei Zhang5e862202019-06-21 14:59:16 -0700880 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties) {
Chris Forbesfa25e632017-02-22 12:36:02 +1300881 const InstanceData& data = GetData(physicalDevice);
882
883 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700884 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
885 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300886 return false;
887
888 // Request the android-specific presentation properties via GPDP2
889 VkPhysicalDeviceProperties2KHR properties = {
890 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
891 presentation_properties,
892 {}
893 };
894
895#pragma clang diagnostic push
896#pragma clang diagnostic ignored "-Wold-style-cast"
897 presentation_properties->sType =
898 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
899#pragma clang diagnostic pop
900 presentation_properties->pNext = nullptr;
901 presentation_properties->sharedImage = VK_FALSE;
902
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700903 if (data.driver.GetPhysicalDeviceProperties2KHR) {
904 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
905 &properties);
906 } else {
907 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
908 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300909
910 return true;
911}
912
Chia-I Wu01cf3052016-03-24 16:16:21 +0800913VkResult EnumerateDeviceExtensionProperties(
914 VkPhysicalDevice physicalDevice,
915 const char* pLayerName,
916 uint32_t* pPropertyCount,
917 VkExtensionProperties* pProperties) {
918 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300919 // extensions that are unconditionally exposed by the loader
Yiwei Zhang5e862202019-06-21 14:59:16 -0700920 std::vector<VkExtensionProperties> loader_extensions;
Chris Forbesfa25e632017-02-22 12:36:02 +1300921 loader_extensions.push_back({
922 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
923 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300924
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800925 bool hdrBoardConfig =
926 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(
927 false);
928 if (hdrBoardConfig) {
929 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
930 VK_EXT_HDR_METADATA_SPEC_VERSION});
931 }
932
Chris Forbes16095002017-05-05 15:33:29 -0700933 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
934 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
935 presentation_properties.sharedImage) {
936 loader_extensions.push_back({
937 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
938 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300939 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700940
Ian Elliott5c34de22017-04-10 14:42:30 -0600941 // conditionally add VK_GOOGLE_display_timing if present timestamps are
942 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700943 const std::string timestamp_property("service.sf.present_timestamp");
944 android::base::WaitForPropertyCreation(timestamp_property);
945 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600946 loader_extensions.push_back({
947 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
948 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
949 }
950
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700951 // enumerate our extensions first
952 if (!pLayerName && pProperties) {
953 uint32_t count = std::min(
954 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
955
Yiwei Zhang5e862202019-06-21 14:59:16 -0700956 std::copy_n(loader_extensions.data(), count, pProperties);
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700957
958 if (count < loader_extensions.size()) {
959 *pPropertyCount = count;
960 return VK_INCOMPLETE;
961 }
962
963 pProperties += count;
964 *pPropertyCount -= count;
965 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800966
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800967 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +0800968 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
969 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800970 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800971
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700972 if (pProperties) {
973 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
974 for (uint32_t i = 0; i < *pPropertyCount; i++) {
975 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +0800976
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700977 if (strcmp(prop.extensionName,
978 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
979 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +0800980
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700981 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
982 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
Yiwei Zhang14f4d422019-04-17 12:24:39 -0700983
984 if (prop.specVersion >= 8) {
985 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
986 } else {
987 prop.specVersion = 68;
988 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700989 }
990 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800991
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700992 // restore loader extension count
993 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
994 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800995 }
996
997 return result;
998}
999
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001000VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
1001 const VkAllocationCallbacks* pAllocator,
1002 VkInstance* pInstance) {
1003 const VkAllocationCallbacks& data_allocator =
1004 (pAllocator) ? *pAllocator : GetDefaultAllocator();
1005
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001006 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001007 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001008 if (result != VK_SUCCESS)
1009 return result;
1010
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001011 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001012 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001013 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001014 if (!data)
1015 return VK_ERROR_OUT_OF_HOST_MEMORY;
1016
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001017 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001018
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001019 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001020#pragma clang diagnostic push
1021#pragma clang diagnostic ignored "-Wold-style-cast"
1022 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1023 ? pCreateInfo->pApplicationInfo->apiVersion
1024 : VK_API_VERSION_1_0);
1025 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1026 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1027 uint32_t icd_api_version;
1028 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1029 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001030 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001031 "vkEnumerateInstanceVersion"));
1032 if (!pfn_enumerate_instance_version) {
1033 icd_api_version = VK_API_VERSION_1_0;
1034 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001035 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001036 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001037 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001038 }
1039 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1040 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1041
1042 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1043 ((api_major_version > 1) || (api_minor_version > 0))) {
1044 api_version = VK_API_VERSION_1_0;
1045 wrapper.DowngradeApiVersion();
1046 }
1047#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001048 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001049
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001050 // call into the driver
1051 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001052 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001053 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001054 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1055 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001056 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001057 if (result != VK_SUCCESS) {
1058 FreeInstanceData(data, data_allocator);
1059 return result;
1060 }
1061
1062 // initialize InstanceDriverTable
1063 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001064 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001065 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001066 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001067 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001068 if (data->driver.DestroyInstance)
1069 data->driver.DestroyInstance(instance, pAllocator);
1070
1071 FreeInstanceData(data, data_allocator);
1072
1073 return VK_ERROR_INCOMPATIBLE_DRIVER;
1074 }
1075
1076 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001077 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001078 if (!data->get_device_proc_addr) {
1079 data->driver.DestroyInstance(instance, pAllocator);
1080 FreeInstanceData(data, data_allocator);
1081
1082 return VK_ERROR_INCOMPATIBLE_DRIVER;
1083 }
1084
1085 *pInstance = instance;
1086
1087 return VK_SUCCESS;
1088}
1089
1090void DestroyInstance(VkInstance instance,
1091 const VkAllocationCallbacks* pAllocator) {
1092 InstanceData& data = GetData(instance);
1093 data.driver.DestroyInstance(instance, pAllocator);
1094
1095 VkAllocationCallbacks local_allocator;
1096 if (!pAllocator) {
1097 local_allocator = data.allocator;
1098 pAllocator = &local_allocator;
1099 }
1100
1101 FreeInstanceData(&data, *pAllocator);
1102}
1103
Chia-I Wu4901db72016-03-24 16:38:58 +08001104VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1105 const VkDeviceCreateInfo* pCreateInfo,
1106 const VkAllocationCallbacks* pAllocator,
1107 VkDevice* pDevice) {
1108 const InstanceData& instance_data = GetData(physicalDevice);
1109 const VkAllocationCallbacks& data_allocator =
1110 (pAllocator) ? *pAllocator : instance_data.allocator;
1111
1112 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001113 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001114 if (result != VK_SUCCESS)
1115 return result;
1116
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001117 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001118 DeviceData* data = AllocateDeviceData(data_allocator,
1119 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001120 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001121 if (!data)
1122 return VK_ERROR_OUT_OF_HOST_MEMORY;
1123
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001124 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001125
1126 // call into the driver
1127 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001128 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001129 result = instance_data.driver.CreateDevice(
1130 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1131 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001132 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001133 if (result != VK_SUCCESS) {
1134 FreeDeviceData(data, data_allocator);
1135 return result;
1136 }
1137
1138 // initialize DeviceDriverTable
1139 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001140 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1141 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001142 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1143 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1144 if (data->driver.DestroyDevice)
1145 data->driver.DestroyDevice(dev, pAllocator);
1146
1147 FreeDeviceData(data, data_allocator);
1148
1149 return VK_ERROR_INCOMPATIBLE_DRIVER;
1150 }
Chris Forbesd8277912017-02-10 14:59:59 +13001151
1152 // sanity check ANDROID_native_buffer implementation, whose set of
1153 // entrypoints varies according to the spec version.
1154 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1155 !data->driver.GetSwapchainGrallocUsageANDROID &&
1156 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1157 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1158 " must expose at least one of "
1159 "vkGetSwapchainGrallocUsageANDROID or "
1160 "vkGetSwapchainGrallocUsage2ANDROID");
1161
1162 data->driver.DestroyDevice(dev, pAllocator);
1163 FreeDeviceData(data, data_allocator);
1164
1165 return VK_ERROR_INCOMPATIBLE_DRIVER;
1166 }
1167
Jesse Hall85bb0c52017-02-09 22:13:02 -08001168 VkPhysicalDeviceProperties properties;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001169 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
Jesse Hall85bb0c52017-02-09 22:13:02 -08001170 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1171 &properties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001172 ATRACE_END();
Jesse Hall85bb0c52017-02-09 22:13:02 -08001173
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -07001174 if (properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU) {
1175 // Log that the app is hitting software Vulkan implementation
1176 android::GraphicsEnv::getInstance().setCpuVulkanInUse();
1177 }
1178
Jesse Halldc225072016-05-30 22:40:14 -07001179 data->driver_device = dev;
Jesse Hall85bb0c52017-02-09 22:13:02 -08001180 data->driver_version = properties.driverVersion;
Chia-I Wu4901db72016-03-24 16:38:58 +08001181
1182 *pDevice = dev;
1183
1184 return VK_SUCCESS;
1185}
1186
1187void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1188 DeviceData& data = GetData(device);
1189 data.driver.DestroyDevice(device, pAllocator);
1190
1191 VkAllocationCallbacks local_allocator;
1192 if (!pAllocator) {
1193 local_allocator = data.allocator;
1194 pAllocator = &local_allocator;
1195 }
1196
1197 FreeDeviceData(&data, *pAllocator);
1198}
1199
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001200VkResult EnumeratePhysicalDevices(VkInstance instance,
1201 uint32_t* pPhysicalDeviceCount,
1202 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001203 ATRACE_CALL();
1204
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001205 const auto& data = GetData(instance);
1206
1207 VkResult result = data.driver.EnumeratePhysicalDevices(
1208 instance, pPhysicalDeviceCount, pPhysicalDevices);
1209 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1210 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1211 SetData(pPhysicalDevices[i], data);
1212 }
1213
1214 return result;
1215}
1216
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001217VkResult EnumeratePhysicalDeviceGroups(
1218 VkInstance instance,
1219 uint32_t* pPhysicalDeviceGroupCount,
1220 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001221 ATRACE_CALL();
1222
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001223 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001224 const auto& data = GetData(instance);
1225
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001226 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1227 uint32_t device_count = 0;
1228 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1229 if (result < 0)
1230 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001231
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001232 if (!pPhysicalDeviceGroupProperties) {
1233 *pPhysicalDeviceGroupCount = device_count;
1234 return result;
1235 }
1236
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001237 if (!device_count) {
1238 *pPhysicalDeviceGroupCount = 0;
1239 return result;
1240 }
Chad Versace32c087f2018-09-09 07:28:05 -07001241 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1242 if (!device_count)
1243 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001244
Yiwei Zhang5e862202019-06-21 14:59:16 -07001245 std::vector<VkPhysicalDevice> devices(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001246 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang5e862202019-06-21 14:59:16 -07001247 result =
1248 EnumeratePhysicalDevices(instance, &device_count, devices.data());
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001249 if (result < 0)
1250 return result;
1251
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001252 for (uint32_t i = 0; i < device_count; ++i) {
1253 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1254 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1255 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1256 }
1257 } else {
1258 result = data.driver.EnumeratePhysicalDeviceGroups(
1259 instance, pPhysicalDeviceGroupCount,
1260 pPhysicalDeviceGroupProperties);
1261 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1262 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1263 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1264 for (uint32_t j = 0;
1265 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1266 j++) {
1267 SetData(
1268 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001269 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001270 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001271 }
1272 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001273 }
1274
1275 return result;
1276}
1277
Chia-I Wuba0be412016-03-24 16:24:40 +08001278void GetDeviceQueue(VkDevice device,
1279 uint32_t queueFamilyIndex,
1280 uint32_t queueIndex,
1281 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001282 ATRACE_CALL();
1283
Chia-I Wuba0be412016-03-24 16:24:40 +08001284 const auto& data = GetData(device);
1285
1286 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1287 SetData(*pQueue, data);
1288}
1289
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001290void GetDeviceQueue2(VkDevice device,
1291 const VkDeviceQueueInfo2* pQueueInfo,
1292 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001293 ATRACE_CALL();
1294
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001295 const auto& data = GetData(device);
1296
1297 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001298 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001299}
1300
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001301VKAPI_ATTR VkResult
1302AllocateCommandBuffers(VkDevice device,
1303 const VkCommandBufferAllocateInfo* pAllocateInfo,
1304 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001305 ATRACE_CALL();
1306
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001307 const auto& data = GetData(device);
1308
1309 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1310 pCommandBuffers);
1311 if (result == VK_SUCCESS) {
1312 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1313 SetData(pCommandBuffers[i], data);
1314 }
1315
1316 return result;
1317}
1318
Chia-I Wu9d518162016-03-24 14:55:27 +08001319} // namespace driver
1320} // namespace vulkan