blob: 421f7274ff6ce2384745bcc2434d9da808c78c57 [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>
Chia-I Wu4901db72016-03-24 16:38:58 +080027#include <new>
Mark Salyzyn7823e122016-09-29 08:08:05 -070028
29#include <log/log.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080030
Jesse Hall53457db2016-12-14 16:54:06 -080031#include <android/dlext.h>
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -080032#include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
33#include <configstore/Utils.h>
Jesse Hall53457db2016-12-14 16:54:06 -080034#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090035#include <graphicsenv/GraphicsEnv.h>
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -080036#include <utils/Trace.h>
Chris Forbesfa25e632017-02-22 12:36:02 +130037#include <utils/Vector.h>
Jesse Hall53457db2016-12-14 16:54:06 -080038
Wei Wangf9b05ee2017-07-19 20:59:39 -070039#include "android-base/properties.h"
40
Chia-I Wu9d518162016-03-24 14:55:27 +080041#include "driver.h"
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;
213 return LoadDriver(ns, module);
214}
215
216int LoadUpdatedDriver(const hwvulkan_module_t** module) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800217 ATRACE_CALL();
218
Jesse Hall00e61ff2017-04-07 16:48:02 -0700219 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
220 if (!ns)
221 return -ENOENT;
222 return LoadDriver(ns, module);
223}
224
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800225bool Hal::Open() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800226 ATRACE_CALL();
227
Jesse Halldc225072016-05-30 22:40:14 -0700228 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800229
230 // Use a stub device unless we successfully open a real HAL device.
231 hal_.dev_ = &stubhal::kDevice;
232
Jesse Hall53457db2016-12-14 16:54:06 -0800233 int result;
234 const hwvulkan_module_t* module = nullptr;
235
Jesse Hall00e61ff2017-04-07 16:48:02 -0700236 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800237 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700238 result = LoadBuiltinDriver(&module);
239 if (result != 0) {
240 // -ENOENT means the sphal namespace doesn't exist, not that there
241 // is a problem with the driver.
242 ALOGW_IF(
243 result != -ENOENT,
244 "Failed to load Vulkan driver into sphal namespace. This "
245 "usually means the driver has forbidden library dependencies."
246 "Please fix, this will soon stop working.");
247 result =
248 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
249 reinterpret_cast<const hw_module_t**>(&module));
250 }
Jesse Hall53457db2016-12-14 16:54:06 -0800251 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800252 if (result != 0) {
Jesse Hall53457db2016-12-14 16:54:06 -0800253 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800254 return true;
255 }
256
257 hwvulkan_device_t* device;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800258 ATRACE_BEGIN("hwvulkan module open");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800259 result =
260 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
261 reinterpret_cast<hw_device_t**>(&device));
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800262 ATRACE_END();
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800263 if (result != 0) {
264 // Any device with a Vulkan HAL should be able to open the device.
265 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
266 result);
267 return false;
268 }
269
270 hal_.dev_ = device;
271
Chia-I Wu31938252016-05-23 15:31:02 +0800272 hal_.InitDebugReportIndex();
273
274 return true;
275}
276
277bool Hal::InitDebugReportIndex() {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800278 ATRACE_CALL();
279
Chia-I Wu31938252016-05-23 15:31:02 +0800280 uint32_t count;
281 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
282 VK_SUCCESS) {
283 ALOGE("failed to get HAL instance extension count");
284 return false;
285 }
286
287 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
288 malloc(sizeof(VkExtensionProperties) * count));
289 if (!exts) {
290 ALOGE("failed to allocate HAL instance extension array");
291 return false;
292 }
293
294 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
295 VK_SUCCESS) {
296 ALOGE("failed to enumerate HAL instance extensions");
297 free(exts);
298 return false;
299 }
300
301 for (uint32_t i = 0; i < count; i++) {
302 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
303 0) {
304 debug_report_index_ = static_cast<int>(i);
305 break;
306 }
307 }
308
309 free(exts);
310
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800311 return true;
312}
313
314CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800315 const VkAllocationCallbacks& allocator)
316 : is_instance_(true),
317 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800318 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800319 instance_info_(create_info),
320 extension_filter_() {
321 hook_extensions_.set(ProcHook::EXTENSION_CORE);
322 hal_extensions_.set(ProcHook::EXTENSION_CORE);
323}
324
Chia-I Wu4901db72016-03-24 16:38:58 +0800325CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
326 const VkDeviceCreateInfo& create_info,
327 const VkAllocationCallbacks& allocator)
328 : is_instance_(false),
329 allocator_(allocator),
330 physical_dev_(physical_dev),
331 dev_info_(create_info),
332 extension_filter_() {
333 hook_extensions_.set(ProcHook::EXTENSION_CORE);
334 hal_extensions_.set(ProcHook::EXTENSION_CORE);
335}
336
337CreateInfoWrapper::~CreateInfoWrapper() {
338 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
339 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
340}
341
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800342VkResult CreateInfoWrapper::Validate() {
343 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800344 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800345 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800346 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800347 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800348
349 return result;
350}
351
352const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800353CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800354 return hook_extensions_;
355}
356
357const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800358CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800359 return hal_extensions_;
360}
361
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800362CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
363 return &instance_info_;
364}
365
Chia-I Wu4901db72016-03-24 16:38:58 +0800366CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
367 return &dev_info_;
368}
369
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800370VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800371 const struct StructHeader {
372 VkStructureType type;
373 const void* next;
374 } * header;
375
376 if (is_instance_) {
377 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
378
379 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
380 while (header &&
381 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
382 header = reinterpret_cast<const StructHeader*>(header->next);
383
384 instance_info_.pNext = header;
385 } else {
386 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
387
388 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
389 while (header &&
390 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
391 header = reinterpret_cast<const StructHeader*>(header->next);
392
393 dev_info_.pNext = header;
394 }
395
396 return VK_SUCCESS;
397}
398
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800399VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800400 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
401 : dev_info_.ppEnabledLayerNames;
402 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
403 : dev_info_.enabledLayerCount;
404
405 // remove all layers
406 layer_names = nullptr;
407 layer_count = 0;
408
409 return VK_SUCCESS;
410}
411
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800412VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800413 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
414 : dev_info_.ppEnabledExtensionNames;
415 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
416 : dev_info_.enabledExtensionCount;
417 if (!ext_count)
418 return VK_SUCCESS;
419
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800420 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800421 if (result != VK_SUCCESS)
422 return result;
423
424 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800425 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800426
Jesse Halld3d887a2018-03-05 13:34:45 -0800427 // Enable device extensions that contain physical-device commands, so that
428 // vkGetInstanceProcAddr will return those physical-device commands.
429 if (is_instance_) {
430 hook_extensions_.set(ProcHook::KHR_swapchain);
431 }
432
Chia-I Wu4901db72016-03-24 16:38:58 +0800433 ext_names = extension_filter_.names;
434 ext_count = extension_filter_.name_count;
435
436 return VK_SUCCESS;
437}
438
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800439VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800440 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800441 return Hal::Device().EnumerateInstanceExtensionProperties(
442 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800443 } else {
444 const auto& driver = GetData(physical_dev_).driver;
445 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
446 &count, nullptr);
447 }
448}
449
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800450VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800451 uint32_t& count,
452 VkExtensionProperties* props) const {
453 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800454 return Hal::Device().EnumerateInstanceExtensionProperties(
455 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800456 } else {
457 const auto& driver = GetData(physical_dev_).driver;
458 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
459 &count, props);
460 }
461}
462
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800463VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800464 // query extension count
465 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800466 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800467 if (result != VK_SUCCESS || count == 0)
468 return result;
469
470 auto& filter = extension_filter_;
471 filter.exts =
472 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
473 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
474 alignof(VkExtensionProperties),
475 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
476 if (!filter.exts)
477 return VK_ERROR_OUT_OF_HOST_MEMORY;
478
479 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800480 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800481 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
482 return result;
483
484 if (!count)
485 return VK_SUCCESS;
486
487 filter.ext_count = count;
488
489 // allocate name array
490 uint32_t enabled_ext_count = (is_instance_)
491 ? instance_info_.enabledExtensionCount
492 : dev_info_.enabledExtensionCount;
493 count = std::min(filter.ext_count, enabled_ext_count);
494 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
495 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
496 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
497 if (!filter.names)
498 return VK_ERROR_OUT_OF_HOST_MEMORY;
499
500 return VK_SUCCESS;
501}
502
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800503void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800504 auto& filter = extension_filter_;
505
506 ProcHook::Extension ext_bit = GetProcHookExtension(name);
507 if (is_instance_) {
508 switch (ext_bit) {
509 case ProcHook::KHR_android_surface:
510 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700511 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300512 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800513 hook_extensions_.set(ext_bit);
514 // return now as these extensions do not require HAL support
515 return;
516 case ProcHook::EXT_debug_report:
517 // both we and HAL can take part in
518 hook_extensions_.set(ext_bit);
519 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300520 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall7f983a82018-03-29 14:46:45 -0700521 case ProcHook::EXTENSION_UNKNOWN:
522 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800523 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700524
525 case ProcHook::KHR_incremental_present:
526 case ProcHook::KHR_shared_presentable_image:
527 case ProcHook::KHR_swapchain:
528 case ProcHook::EXT_hdr_metadata:
529 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
530 case ProcHook::ANDROID_native_buffer:
531 case ProcHook::GOOGLE_display_timing:
532 case ProcHook::EXTENSION_CORE:
533 case ProcHook::EXTENSION_COUNT:
534 // Device and meta extensions. If we ever get here it's a bug in
535 // our code. But enumerating them lets us avoid having a default
536 // case, and default hides other bugs.
537 ALOGE(
538 "CreateInfoWrapper::FilterExtension: invalid instance "
539 "extension '%s'. FIX ME",
540 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800541 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700542
543 // Don't use a default case. Without it, -Wswitch will tell us
544 // at compile time if someone adds a new ProcHook extension but
545 // doesn't handle it above. That's a real bug that has
546 // not-immediately-obvious effects.
547 //
548 // default:
549 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800550 }
551 } else {
552 switch (ext_bit) {
553 case ProcHook::KHR_swapchain:
554 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
555 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
556 ext_bit = ProcHook::ANDROID_native_buffer;
557 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700558 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700559 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300560 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700561 hook_extensions_.set(ext_bit);
562 // return now as these extensions do not require HAL support
563 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700564 case ProcHook::EXT_hdr_metadata:
565 hook_extensions_.set(ext_bit);
566 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700567 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800568 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall7f983a82018-03-29 14:46:45 -0700569 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800570 break;
Jesse Hall7f983a82018-03-29 14:46:45 -0700571
572 case ProcHook::KHR_android_surface:
573 case ProcHook::KHR_get_physical_device_properties2:
574 case ProcHook::KHR_get_surface_capabilities2:
575 case ProcHook::KHR_surface:
576 case ProcHook::EXT_debug_report:
577 case ProcHook::EXT_swapchain_colorspace:
578 case ProcHook::ANDROID_native_buffer:
579 case ProcHook::EXTENSION_CORE:
580 case ProcHook::EXTENSION_COUNT:
581 // Instance and meta extensions. If we ever get here it's a bug
582 // in our code. But enumerating them lets us avoid having a
583 // default case, and default hides other bugs.
584 ALOGE(
585 "CreateInfoWrapper::FilterExtension: invalid device "
586 "extension '%s'. FIX ME",
587 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800588 return;
Jesse Hall7f983a82018-03-29 14:46:45 -0700589
590 // Don't use a default case. Without it, -Wswitch will tell us
591 // at compile time if someone adds a new ProcHook extension but
592 // doesn't handle it above. That's a real bug that has
593 // not-immediately-obvious effects.
594 //
595 // default:
596 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800597 }
598 }
599
600 for (uint32_t i = 0; i < filter.ext_count; i++) {
601 const VkExtensionProperties& props = filter.exts[i];
602 // ignore unknown extensions
603 if (strcmp(name, props.extensionName) != 0)
604 continue;
605
Chia-I Wu4901db72016-03-24 16:38:58 +0800606 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800607 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
608 if (ext_bit == ProcHook::ANDROID_native_buffer)
609 hook_extensions_.set(ProcHook::KHR_swapchain);
610
611 hal_extensions_.set(ext_bit);
612 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800613
614 break;
615 }
616}
617
Ian Elliottf3e872d2017-11-02 10:15:13 -0600618void CreateInfoWrapper::DowngradeApiVersion() {
619 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
620 if (instance_info_.pApplicationInfo) {
621 application_info_ = *instance_info_.pApplicationInfo;
622 instance_info_.pApplicationInfo = &application_info_;
623 application_info_.apiVersion = VK_API_VERSION_1_0;
624 }
625}
626
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800627VKAPI_ATTR void* DefaultAllocate(void*,
628 size_t size,
629 size_t alignment,
630 VkSystemAllocationScope) {
631 void* ptr = nullptr;
632 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
633 // additionally requires that it be at least sizeof(void*).
634 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
635 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
636 ret, ptr);
637 return ret == 0 ? ptr : nullptr;
638}
639
640VKAPI_ATTR void* DefaultReallocate(void*,
641 void* ptr,
642 size_t size,
643 size_t alignment,
644 VkSystemAllocationScope) {
645 if (size == 0) {
646 free(ptr);
647 return nullptr;
648 }
649
650 // TODO(jessehall): Right now we never shrink allocations; if the new
651 // request is smaller than the existing chunk, we just continue using it.
652 // Right now the loader never reallocs, so this doesn't matter. If that
653 // changes, or if this code is copied into some other project, this should
654 // probably have a heuristic to allocate-copy-free when doing so will save
655 // "enough" space.
656 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
657 if (size <= old_size)
658 return ptr;
659
660 void* new_ptr = nullptr;
661 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
662 return nullptr;
663 if (ptr) {
664 memcpy(new_ptr, ptr, std::min(old_size, size));
665 free(ptr);
666 }
667 return new_ptr;
668}
669
670VKAPI_ATTR void DefaultFree(void*, void* ptr) {
671 ALOGD_CALLSTACK("Free: %p", ptr);
672 free(ptr);
673}
674
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800675InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
676 void* data_mem = allocator.pfnAllocation(
677 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
678 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
679 if (!data_mem)
680 return nullptr;
681
682 return new (data_mem) InstanceData(allocator);
683}
684
685void FreeInstanceData(InstanceData* data,
686 const VkAllocationCallbacks& allocator) {
687 data->~InstanceData();
688 allocator.pfnFree(allocator.pUserData, data);
689}
690
Chia-I Wu950d6e12016-05-03 09:12:35 +0800691DeviceData* AllocateDeviceData(
692 const VkAllocationCallbacks& allocator,
693 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800694 void* data_mem = allocator.pfnAllocation(
695 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
696 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
697 if (!data_mem)
698 return nullptr;
699
Chia-I Wu950d6e12016-05-03 09:12:35 +0800700 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800701}
702
703void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
704 data->~DeviceData();
705 allocator.pfnFree(allocator.pUserData, data);
706}
707
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800708} // anonymous namespace
709
Chia-I Wu9d518162016-03-24 14:55:27 +0800710bool Debuggable() {
711 return (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) >= 0);
712}
713
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800714bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800715 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800716}
717
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800718const VkAllocationCallbacks& GetDefaultAllocator() {
719 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
720 .pUserData = nullptr,
721 .pfnAllocation = DefaultAllocate,
722 .pfnReallocation = DefaultReallocate,
723 .pfnFree = DefaultFree,
724 };
725
726 return kDefaultAllocCallbacks;
727}
728
Chia-I Wueb7db122016-03-24 09:11:06 +0800729PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
730 const ProcHook* hook = GetProcHook(pName);
731 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800732 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800733
734 if (!instance) {
735 if (hook->type == ProcHook::GLOBAL)
736 return hook->proc;
737
Chia-I Wu109f8982016-04-22 06:40:40 +0800738 // v0 layers expect
739 //
740 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
741 //
742 // to work.
743 if (strcmp(pName, "vkCreateDevice") == 0)
744 return hook->proc;
745
Chia-I Wueb7db122016-03-24 09:11:06 +0800746 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800747 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800748 pName);
749
Chia-I Wu109f8982016-04-22 06:40:40 +0800750 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800751 }
752
753 PFN_vkVoidFunction proc;
754
755 switch (hook->type) {
756 case ProcHook::INSTANCE:
757 proc = (GetData(instance).hook_extensions[hook->extension])
758 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800759 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800760 break;
761 case ProcHook::DEVICE:
762 proc = (hook->extension == ProcHook::EXTENSION_CORE)
763 ? hook->proc
764 : hook->checked_proc;
765 break;
766 default:
767 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800768 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800769 pName);
770 proc = nullptr;
771 break;
772 }
773
774 return proc;
775}
776
777PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
778 const ProcHook* hook = GetProcHook(pName);
779 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800780 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800781
782 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800783 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800784 return nullptr;
785 }
786
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800787 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
788 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800789}
790
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800791VkResult EnumerateInstanceExtensionProperties(
792 const char* pLayerName,
793 uint32_t* pPropertyCount,
794 VkExtensionProperties* pProperties) {
Ian Elliott34a327b2017-03-28 13:20:35 -0600795
796 android::Vector<VkExtensionProperties> loader_extensions;
797 loader_extensions.push_back({
798 VK_KHR_SURFACE_EXTENSION_NAME,
799 VK_KHR_SURFACE_SPEC_VERSION});
800 loader_extensions.push_back({
801 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
802 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
803 loader_extensions.push_back({
804 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
805 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700806 loader_extensions.push_back({
807 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
808 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600809
Chia-I Wu31938252016-05-23 15:31:02 +0800810 static const VkExtensionProperties loader_debug_report_extension = {
811 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
812 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800813
814 // enumerate our extensions first
815 if (!pLayerName && pProperties) {
816 uint32_t count = std::min(
817 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
818
819 std::copy_n(loader_extensions.begin(), count, pProperties);
820
821 if (count < loader_extensions.size()) {
822 *pPropertyCount = count;
823 return VK_INCOMPLETE;
824 }
825
826 pProperties += count;
827 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800828
829 if (Hal::Get().GetDebugReportIndex() < 0) {
830 if (!*pPropertyCount) {
831 *pPropertyCount = count;
832 return VK_INCOMPLETE;
833 }
834
835 pProperties[0] = loader_debug_report_extension;
836 pProperties += 1;
837 *pPropertyCount -= 1;
838 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800839 }
840
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800841 ATRACE_BEGIN("driver.EnumerateInstanceExtensionProperties");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800842 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800843 pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800844 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800845
Chia-I Wu31938252016-05-23 15:31:02 +0800846 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
847 int idx = Hal::Get().GetDebugReportIndex();
848 if (idx < 0) {
849 *pPropertyCount += 1;
850 } else if (pProperties &&
851 static_cast<uint32_t>(idx) < *pPropertyCount) {
852 pProperties[idx].specVersion =
853 std::min(pProperties[idx].specVersion,
854 loader_debug_report_extension.specVersion);
855 }
856
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800857 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800858 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800859
860 return result;
861}
862
Chris Forbesfa25e632017-02-22 12:36:02 +1300863bool QueryPresentationProperties(
864 VkPhysicalDevice physicalDevice,
865 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties)
866{
867 const InstanceData& data = GetData(physicalDevice);
868
869 // GPDP2 must be present and enabled on the instance.
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700870 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
871 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300872 return false;
873
874 // Request the android-specific presentation properties via GPDP2
875 VkPhysicalDeviceProperties2KHR properties = {
876 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
877 presentation_properties,
878 {}
879 };
880
881#pragma clang diagnostic push
882#pragma clang diagnostic ignored "-Wold-style-cast"
883 presentation_properties->sType =
884 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
885#pragma clang diagnostic pop
886 presentation_properties->pNext = nullptr;
887 presentation_properties->sharedImage = VK_FALSE;
888
Yiwei Zhang922b1e32018-03-13 17:12:11 -0700889 if (data.driver.GetPhysicalDeviceProperties2KHR) {
890 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
891 &properties);
892 } else {
893 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
894 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300895
896 return true;
897}
898
Chia-I Wu01cf3052016-03-24 16:16:21 +0800899VkResult EnumerateDeviceExtensionProperties(
900 VkPhysicalDevice physicalDevice,
901 const char* pLayerName,
902 uint32_t* pPropertyCount,
903 VkExtensionProperties* pProperties) {
904 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300905 // extensions that are unconditionally exposed by the loader
906 android::Vector<VkExtensionProperties> loader_extensions;
907 loader_extensions.push_back({
908 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
909 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300910
Courtney Goeltzenleuchter7671d462018-01-24 11:51:01 -0800911 bool hdrBoardConfig =
912 getBool<ISurfaceFlingerConfigs, &ISurfaceFlingerConfigs::hasHDRDisplay>(
913 false);
914 if (hdrBoardConfig) {
915 loader_extensions.push_back({VK_EXT_HDR_METADATA_EXTENSION_NAME,
916 VK_EXT_HDR_METADATA_SPEC_VERSION});
917 }
918
Chris Forbes16095002017-05-05 15:33:29 -0700919 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
920 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
921 presentation_properties.sharedImage) {
922 loader_extensions.push_back({
923 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
924 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300925 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700926
Ian Elliott5c34de22017-04-10 14:42:30 -0600927 // conditionally add VK_GOOGLE_display_timing if present timestamps are
928 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700929 const std::string timestamp_property("service.sf.present_timestamp");
930 android::base::WaitForPropertyCreation(timestamp_property);
931 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600932 loader_extensions.push_back({
933 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
934 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
935 }
936
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700937 // enumerate our extensions first
938 if (!pLayerName && pProperties) {
939 uint32_t count = std::min(
940 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
941
942 std::copy_n(loader_extensions.begin(), count, pProperties);
943
944 if (count < loader_extensions.size()) {
945 *pPropertyCount = count;
946 return VK_INCOMPLETE;
947 }
948
949 pProperties += count;
950 *pPropertyCount -= count;
951 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800952
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800953 ATRACE_BEGIN("driver.EnumerateDeviceExtensionProperties");
Chia-I Wu01cf3052016-03-24 16:16:21 +0800954 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
955 physicalDevice, pLayerName, pPropertyCount, pProperties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800956 ATRACE_END();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800957
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700958 if (pProperties) {
959 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
960 for (uint32_t i = 0; i < *pPropertyCount; i++) {
961 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +0800962
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700963 if (strcmp(prop.extensionName,
964 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
965 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +0800966
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700967 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
968 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
969 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
970 }
971 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800972
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700973 // restore loader extension count
974 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
975 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800976 }
977
978 return result;
979}
980
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800981VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
982 const VkAllocationCallbacks* pAllocator,
983 VkInstance* pInstance) {
984 const VkAllocationCallbacks& data_allocator =
985 (pAllocator) ? *pAllocator : GetDefaultAllocator();
986
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800987 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800988 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800989 if (result != VK_SUCCESS)
990 return result;
991
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800992 ATRACE_BEGIN("AllocateInstanceData");
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800993 InstanceData* data = AllocateInstanceData(data_allocator);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -0800994 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800995 if (!data)
996 return VK_ERROR_OUT_OF_HOST_MEMORY;
997
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800998 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800999
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001000 ATRACE_BEGIN("autoDowngradeApiVersion");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001001#pragma clang diagnostic push
1002#pragma clang diagnostic ignored "-Wold-style-cast"
1003 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
1004 ? pCreateInfo->pApplicationInfo->apiVersion
1005 : VK_API_VERSION_1_0);
1006 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
1007 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
1008 uint32_t icd_api_version;
1009 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
1010 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
Yi Kongbcbc73a2018-07-18 10:13:04 -07001011 Hal::Device().GetInstanceProcAddr(nullptr,
Ian Elliottf3e872d2017-11-02 10:15:13 -06001012 "vkEnumerateInstanceVersion"));
1013 if (!pfn_enumerate_instance_version) {
1014 icd_api_version = VK_API_VERSION_1_0;
1015 } else {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001016 ATRACE_BEGIN("pfn_enumerate_instance_version");
Ian Elliottf3e872d2017-11-02 10:15:13 -06001017 result = (*pfn_enumerate_instance_version)(&icd_api_version);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001018 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001019 }
1020 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
1021 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
1022
1023 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
1024 ((api_major_version > 1) || (api_minor_version > 0))) {
1025 api_version = VK_API_VERSION_1_0;
1026 wrapper.DowngradeApiVersion();
1027 }
1028#pragma clang diagnostic pop
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001029 ATRACE_END();
Ian Elliottf3e872d2017-11-02 10:15:13 -06001030
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001031 // call into the driver
1032 VkInstance instance;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001033 ATRACE_BEGIN("driver.CreateInstance");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001034 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001035 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
1036 &instance);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001037 ATRACE_END();
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001038 if (result != VK_SUCCESS) {
1039 FreeInstanceData(data, data_allocator);
1040 return result;
1041 }
1042
1043 // initialize InstanceDriverTable
1044 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001045 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001046 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001047 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001048 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001049 if (data->driver.DestroyInstance)
1050 data->driver.DestroyInstance(instance, pAllocator);
1051
1052 FreeInstanceData(data, data_allocator);
1053
1054 return VK_ERROR_INCOMPATIBLE_DRIVER;
1055 }
1056
1057 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001058 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001059 if (!data->get_device_proc_addr) {
1060 data->driver.DestroyInstance(instance, pAllocator);
1061 FreeInstanceData(data, data_allocator);
1062
1063 return VK_ERROR_INCOMPATIBLE_DRIVER;
1064 }
1065
1066 *pInstance = instance;
1067
1068 return VK_SUCCESS;
1069}
1070
1071void DestroyInstance(VkInstance instance,
1072 const VkAllocationCallbacks* pAllocator) {
1073 InstanceData& data = GetData(instance);
1074 data.driver.DestroyInstance(instance, pAllocator);
1075
1076 VkAllocationCallbacks local_allocator;
1077 if (!pAllocator) {
1078 local_allocator = data.allocator;
1079 pAllocator = &local_allocator;
1080 }
1081
1082 FreeInstanceData(&data, *pAllocator);
1083}
1084
Chia-I Wu4901db72016-03-24 16:38:58 +08001085VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1086 const VkDeviceCreateInfo* pCreateInfo,
1087 const VkAllocationCallbacks* pAllocator,
1088 VkDevice* pDevice) {
1089 const InstanceData& instance_data = GetData(physicalDevice);
1090 const VkAllocationCallbacks& data_allocator =
1091 (pAllocator) ? *pAllocator : instance_data.allocator;
1092
1093 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001094 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001095 if (result != VK_SUCCESS)
1096 return result;
1097
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001098 ATRACE_BEGIN("AllocateDeviceData");
Chia-I Wu950d6e12016-05-03 09:12:35 +08001099 DeviceData* data = AllocateDeviceData(data_allocator,
1100 instance_data.debug_report_callbacks);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001101 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001102 if (!data)
1103 return VK_ERROR_OUT_OF_HOST_MEMORY;
1104
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001105 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001106
1107 // call into the driver
1108 VkDevice dev;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001109 ATRACE_BEGIN("driver.CreateDevice");
Chia-I Wu4901db72016-03-24 16:38:58 +08001110 result = instance_data.driver.CreateDevice(
1111 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1112 pAllocator, &dev);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001113 ATRACE_END();
Chia-I Wu4901db72016-03-24 16:38:58 +08001114 if (result != VK_SUCCESS) {
1115 FreeDeviceData(data, data_allocator);
1116 return result;
1117 }
1118
1119 // initialize DeviceDriverTable
1120 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001121 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1122 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001123 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1124 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1125 if (data->driver.DestroyDevice)
1126 data->driver.DestroyDevice(dev, pAllocator);
1127
1128 FreeDeviceData(data, data_allocator);
1129
1130 return VK_ERROR_INCOMPATIBLE_DRIVER;
1131 }
Chris Forbesd8277912017-02-10 14:59:59 +13001132
1133 // sanity check ANDROID_native_buffer implementation, whose set of
1134 // entrypoints varies according to the spec version.
1135 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1136 !data->driver.GetSwapchainGrallocUsageANDROID &&
1137 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1138 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1139 " must expose at least one of "
1140 "vkGetSwapchainGrallocUsageANDROID or "
1141 "vkGetSwapchainGrallocUsage2ANDROID");
1142
1143 data->driver.DestroyDevice(dev, pAllocator);
1144 FreeDeviceData(data, data_allocator);
1145
1146 return VK_ERROR_INCOMPATIBLE_DRIVER;
1147 }
1148
Jesse Hall85bb0c52017-02-09 22:13:02 -08001149 VkPhysicalDeviceProperties properties;
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001150 ATRACE_BEGIN("driver.GetPhysicalDeviceProperties");
Jesse Hall85bb0c52017-02-09 22:13:02 -08001151 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1152 &properties);
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001153 ATRACE_END();
Jesse Hall85bb0c52017-02-09 22:13:02 -08001154
Jesse Halldc225072016-05-30 22:40:14 -07001155 data->driver_device = dev;
Jesse Hall85bb0c52017-02-09 22:13:02 -08001156 data->driver_version = properties.driverVersion;
Chia-I Wu4901db72016-03-24 16:38:58 +08001157
1158 *pDevice = dev;
1159
1160 return VK_SUCCESS;
1161}
1162
1163void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1164 DeviceData& data = GetData(device);
1165 data.driver.DestroyDevice(device, pAllocator);
1166
1167 VkAllocationCallbacks local_allocator;
1168 if (!pAllocator) {
1169 local_allocator = data.allocator;
1170 pAllocator = &local_allocator;
1171 }
1172
1173 FreeDeviceData(&data, *pAllocator);
1174}
1175
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001176VkResult EnumeratePhysicalDevices(VkInstance instance,
1177 uint32_t* pPhysicalDeviceCount,
1178 VkPhysicalDevice* pPhysicalDevices) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001179 ATRACE_CALL();
1180
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001181 const auto& data = GetData(instance);
1182
1183 VkResult result = data.driver.EnumeratePhysicalDevices(
1184 instance, pPhysicalDeviceCount, pPhysicalDevices);
1185 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1186 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1187 SetData(pPhysicalDevices[i], data);
1188 }
1189
1190 return result;
1191}
1192
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001193VkResult EnumeratePhysicalDeviceGroups(
1194 VkInstance instance,
1195 uint32_t* pPhysicalDeviceGroupCount,
1196 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001197 ATRACE_CALL();
1198
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001199 VkResult result = VK_SUCCESS;
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001200 const auto& data = GetData(instance);
1201
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001202 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1203 uint32_t device_count = 0;
1204 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1205 if (result < 0)
1206 return result;
Chad Versace32c087f2018-09-09 07:28:05 -07001207
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001208 if (!pPhysicalDeviceGroupProperties) {
1209 *pPhysicalDeviceGroupCount = device_count;
1210 return result;
1211 }
1212
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001213 if (!device_count) {
1214 *pPhysicalDeviceGroupCount = 0;
1215 return result;
1216 }
Chad Versace32c087f2018-09-09 07:28:05 -07001217 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1218 if (!device_count)
1219 return VK_INCOMPLETE;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001220
1221 android::Vector<VkPhysicalDevice> devices;
1222 devices.resize(device_count);
Chad Versace32c087f2018-09-09 07:28:05 -07001223 *pPhysicalDeviceGroupCount = device_count;
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001224 result = EnumeratePhysicalDevices(instance, &device_count,
1225 devices.editArray());
1226 if (result < 0)
1227 return result;
1228
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001229 for (uint32_t i = 0; i < device_count; ++i) {
1230 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1231 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1232 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1233 }
1234 } else {
1235 result = data.driver.EnumeratePhysicalDeviceGroups(
1236 instance, pPhysicalDeviceGroupCount,
1237 pPhysicalDeviceGroupProperties);
1238 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1239 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1240 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1241 for (uint32_t j = 0;
1242 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1243 j++) {
1244 SetData(
1245 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliottcd8ad332017-10-13 09:21:12 -06001246 data);
Yiwei Zhang4cd9cc92018-01-08 17:55:50 -08001247 }
Ian Elliottcd8ad332017-10-13 09:21:12 -06001248 }
1249 }
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001250 }
1251
1252 return result;
1253}
1254
Chia-I Wuba0be412016-03-24 16:24:40 +08001255void GetDeviceQueue(VkDevice device,
1256 uint32_t queueFamilyIndex,
1257 uint32_t queueIndex,
1258 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001259 ATRACE_CALL();
1260
Chia-I Wuba0be412016-03-24 16:24:40 +08001261 const auto& data = GetData(device);
1262
1263 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1264 SetData(*pQueue, data);
1265}
1266
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001267void GetDeviceQueue2(VkDevice device,
1268 const VkDeviceQueueInfo2* pQueueInfo,
1269 VkQueue* pQueue) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001270 ATRACE_CALL();
1271
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001272 const auto& data = GetData(device);
1273
1274 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhangf5b9f732018-02-07 14:06:09 -08001275 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Kochf25f5bb2017-10-05 00:26:58 -04001276}
1277
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001278VKAPI_ATTR VkResult
1279AllocateCommandBuffers(VkDevice device,
1280 const VkCommandBufferAllocateInfo* pAllocateInfo,
1281 VkCommandBuffer* pCommandBuffers) {
Yiwei Zhangfdd0c2a2019-01-30 20:16:37 -08001282 ATRACE_CALL();
1283
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001284 const auto& data = GetData(device);
1285
1286 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1287 pCommandBuffers);
1288 if (result == VK_SUCCESS) {
1289 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1290 SetData(pCommandBuffers[i], data);
1291 }
1292
1293 return result;
1294}
1295
Chia-I Wu9d518162016-03-24 14:55:27 +08001296} // namespace driver
1297} // namespace vulkan