blob: 4beb315a65b6687ff4487298f1b0de95cfcc5095 [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
Mark Salyzyn7823e122016-09-29 08:08:05 -070017#include <malloc.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080018#include <stdlib.h>
19#include <string.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070020#include <sys/prctl.h>
21
Yiwei Zhang170d3702018-01-08 17:55:50 -080022#include <dlfcn.h>
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080023#include <algorithm>
Chia-I Wuff4a6c72016-03-24 16:05:56 +080024#include <array>
Chia-I Wu4901db72016-03-24 16:38:58 +080025#include <new>
Mark Salyzyn7823e122016-09-29 08:08:05 -070026
27#include <log/log.h>
Chia-I Wu9d518162016-03-24 14:55:27 +080028
Jesse Hall53457db2016-12-14 16:54:06 -080029#include <android/dlext.h>
30#include <cutils/properties.h>
Jiyong Park27c39e12017-05-08 13:00:02 +090031#include <graphicsenv/GraphicsEnv.h>
Chris Forbesfa25e632017-02-22 12:36:02 +130032#include <utils/Vector.h>
Jesse Hall53457db2016-12-14 16:54:06 -080033
Wei Wangf9b05ee2017-07-19 20:59:39 -070034#include "android-base/properties.h"
35
Chia-I Wu9d518162016-03-24 14:55:27 +080036#include "driver.h"
Jesse Hallb7c4e3b2016-04-11 13:51:38 -070037#include "stubhal.h"
Chia-I Wu9d518162016-03-24 14:55:27 +080038
Jesse Hall00e61ff2017-04-07 16:48:02 -070039// TODO(b/37049319) Get this from a header once one exists
40extern "C" {
41android_namespace_t* android_get_exported_namespace(const char*);
42}
43
Chia-I Wudbb7e9c2016-03-24 15:09:38 +080044// #define ENABLE_ALLOC_CALLSTACKS 1
45#if ENABLE_ALLOC_CALLSTACKS
46#include <utils/CallStack.h>
47#define ALOGD_CALLSTACK(...) \
48 do { \
49 ALOGD(__VA_ARGS__); \
50 android::CallStack callstack; \
51 callstack.update(); \
52 callstack.log(LOG_TAG, ANDROID_LOG_DEBUG, " "); \
53 } while (false)
54#else
55#define ALOGD_CALLSTACK(...) \
56 do { \
57 } while (false)
58#endif
59
Chia-I Wu9d518162016-03-24 14:55:27 +080060namespace vulkan {
61namespace driver {
62
Chia-I Wu136b8eb2016-03-24 15:01:52 +080063namespace {
64
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080065class Hal {
66 public:
67 static bool Open();
68
69 static const Hal& Get() { return hal_; }
70 static const hwvulkan_device_t& Device() { return *Get().dev_; }
71
Chia-I Wu31938252016-05-23 15:31:02 +080072 int GetDebugReportIndex() const { return debug_report_index_; }
73
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080074 private:
Chia-I Wu31938252016-05-23 15:31:02 +080075 Hal() : dev_(nullptr), debug_report_index_(-1) {}
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080076 Hal(const Hal&) = delete;
77 Hal& operator=(const Hal&) = delete;
78
Chia-I Wu31938252016-05-23 15:31:02 +080079 bool InitDebugReportIndex();
80
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080081 static Hal hal_;
82
83 const hwvulkan_device_t* dev_;
Chia-I Wu31938252016-05-23 15:31:02 +080084 int debug_report_index_;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080085};
86
Chia-I Wu4901db72016-03-24 16:38:58 +080087class CreateInfoWrapper {
88 public:
Chia-I Wu31b2e4f2016-05-23 10:47:57 +080089 CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +080090 const VkAllocationCallbacks& allocator);
Chia-I Wu4901db72016-03-24 16:38:58 +080091 CreateInfoWrapper(VkPhysicalDevice physical_dev,
92 const VkDeviceCreateInfo& create_info,
93 const VkAllocationCallbacks& allocator);
94 ~CreateInfoWrapper();
95
Chia-I Wu3e6c2d62016-04-11 13:55:56 +080096 VkResult Validate();
Ian Elliott1d990b22017-11-02 10:15:13 -060097 void DowngradeApiVersion();
Chia-I Wu4901db72016-03-24 16:38:58 +080098
Chia-I Wu3e6c2d62016-04-11 13:55:56 +080099 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHookExtensions() const;
100 const std::bitset<ProcHook::EXTENSION_COUNT>& GetHalExtensions() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800101
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800102 explicit operator const VkInstanceCreateInfo*() const;
Chia-I Wu4901db72016-03-24 16:38:58 +0800103 explicit operator const VkDeviceCreateInfo*() const;
104
105 private:
106 struct ExtensionFilter {
107 VkExtensionProperties* exts;
108 uint32_t ext_count;
109
110 const char** names;
111 uint32_t name_count;
112 };
113
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800114 VkResult SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800115
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800116 VkResult SanitizeLayers();
117 VkResult SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800118
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800119 VkResult QueryExtensionCount(uint32_t& count) const;
120 VkResult EnumerateExtensions(uint32_t& count,
121 VkExtensionProperties* props) const;
122 VkResult InitExtensionFilter();
123 void FilterExtension(const char* name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800124
125 const bool is_instance_;
126 const VkAllocationCallbacks& allocator_;
127
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800128 VkPhysicalDevice physical_dev_;
Chia-I Wu4901db72016-03-24 16:38:58 +0800129
130 union {
131 VkInstanceCreateInfo instance_info_;
132 VkDeviceCreateInfo dev_info_;
133 };
134
Ian Elliott1d990b22017-11-02 10:15:13 -0600135 VkApplicationInfo application_info_;
136
Chia-I Wu4901db72016-03-24 16:38:58 +0800137 ExtensionFilter extension_filter_;
138
139 std::bitset<ProcHook::EXTENSION_COUNT> hook_extensions_;
140 std::bitset<ProcHook::EXTENSION_COUNT> hal_extensions_;
141};
142
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800143Hal Hal::hal_;
144
Jesse Hall53457db2016-12-14 16:54:06 -0800145void* LoadLibrary(const android_dlextinfo& dlextinfo,
146 const char* subname,
147 int subname_len) {
148 const char kLibFormat[] = "vulkan.%*s.so";
149 char* name = static_cast<char*>(
150 alloca(sizeof(kLibFormat) + static_cast<size_t>(subname_len)));
151 sprintf(name, kLibFormat, subname_len, subname);
152 return android_dlopen_ext(name, RTLD_LOCAL | RTLD_NOW, &dlextinfo);
153}
154
155const std::array<const char*, 2> HAL_SUBNAME_KEY_PROPERTIES = {{
156 "ro.hardware." HWVULKAN_HARDWARE_MODULE_ID,
157 "ro.board.platform",
158}};
159
Jesse Hall00e61ff2017-04-07 16:48:02 -0700160int LoadDriver(android_namespace_t* library_namespace,
161 const hwvulkan_module_t** module) {
Jesse Hall53457db2016-12-14 16:54:06 -0800162 const android_dlextinfo dlextinfo = {
163 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Jesse Hall00e61ff2017-04-07 16:48:02 -0700164 .library_namespace = library_namespace,
Jesse Hall53457db2016-12-14 16:54:06 -0800165 };
Jesse Hall53457db2016-12-14 16:54:06 -0800166 void* so = nullptr;
167 char prop[PROPERTY_VALUE_MAX];
168 for (auto key : HAL_SUBNAME_KEY_PROPERTIES) {
169 int prop_len = property_get(key, prop, nullptr);
170 if (prop_len > 0) {
171 so = LoadLibrary(dlextinfo, prop, prop_len);
172 if (so)
173 break;
174 }
175 }
176 if (!so)
177 return -ENOENT;
178
Jesse Hall00e61ff2017-04-07 16:48:02 -0700179 auto hmi = static_cast<hw_module_t*>(dlsym(so, HAL_MODULE_INFO_SYM_AS_STR));
Jesse Hall53457db2016-12-14 16:54:06 -0800180 if (!hmi) {
181 ALOGE("couldn't find symbol '%s' in HAL library: %s", HAL_MODULE_INFO_SYM_AS_STR, dlerror());
182 dlclose(so);
183 return -EINVAL;
184 }
185 if (strcmp(hmi->id, HWVULKAN_HARDWARE_MODULE_ID) != 0) {
186 ALOGE("HAL id '%s' != '%s'", hmi->id, HWVULKAN_HARDWARE_MODULE_ID);
187 dlclose(so);
188 return -EINVAL;
189 }
190 hmi->dso = so;
Jesse Hall00e61ff2017-04-07 16:48:02 -0700191 *module = reinterpret_cast<const hwvulkan_module_t*>(hmi);
Jesse Hall53457db2016-12-14 16:54:06 -0800192 return 0;
193}
194
Jesse Hall00e61ff2017-04-07 16:48:02 -0700195int LoadBuiltinDriver(const hwvulkan_module_t** module) {
196 auto ns = android_get_exported_namespace("sphal");
197 if (!ns)
198 return -ENOENT;
199 return LoadDriver(ns, module);
200}
201
202int LoadUpdatedDriver(const hwvulkan_module_t** module) {
203 auto ns = android::GraphicsEnv::getInstance().getDriverNamespace();
204 if (!ns)
205 return -ENOENT;
206 return LoadDriver(ns, module);
207}
208
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800209bool Hal::Open() {
Jesse Halldc225072016-05-30 22:40:14 -0700210 ALOG_ASSERT(!hal_.dev_, "OpenHAL called more than once");
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800211
212 // Use a stub device unless we successfully open a real HAL device.
213 hal_.dev_ = &stubhal::kDevice;
214
Jesse Hall53457db2016-12-14 16:54:06 -0800215 int result;
216 const hwvulkan_module_t* module = nullptr;
217
Jesse Hall00e61ff2017-04-07 16:48:02 -0700218 result = LoadUpdatedDriver(&module);
Jesse Hall53457db2016-12-14 16:54:06 -0800219 if (result == -ENOENT) {
Jesse Hall00e61ff2017-04-07 16:48:02 -0700220 result = LoadBuiltinDriver(&module);
221 if (result != 0) {
222 // -ENOENT means the sphal namespace doesn't exist, not that there
223 // is a problem with the driver.
224 ALOGW_IF(
225 result != -ENOENT,
226 "Failed to load Vulkan driver into sphal namespace. This "
227 "usually means the driver has forbidden library dependencies."
228 "Please fix, this will soon stop working.");
229 result =
230 hw_get_module(HWVULKAN_HARDWARE_MODULE_ID,
231 reinterpret_cast<const hw_module_t**>(&module));
232 }
Jesse Hall53457db2016-12-14 16:54:06 -0800233 }
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800234 if (result != 0) {
Jesse Hall53457db2016-12-14 16:54:06 -0800235 ALOGV("unable to load Vulkan HAL, using stub HAL (result=%d)", result);
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800236 return true;
237 }
238
239 hwvulkan_device_t* device;
240 result =
241 module->common.methods->open(&module->common, HWVULKAN_DEVICE_0,
242 reinterpret_cast<hw_device_t**>(&device));
243 if (result != 0) {
244 // Any device with a Vulkan HAL should be able to open the device.
245 ALOGE("failed to open Vulkan HAL device: %s (%d)", strerror(-result),
246 result);
247 return false;
248 }
249
250 hal_.dev_ = device;
251
Chia-I Wu31938252016-05-23 15:31:02 +0800252 hal_.InitDebugReportIndex();
253
254 return true;
255}
256
257bool Hal::InitDebugReportIndex() {
258 uint32_t count;
259 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, nullptr) !=
260 VK_SUCCESS) {
261 ALOGE("failed to get HAL instance extension count");
262 return false;
263 }
264
265 VkExtensionProperties* exts = reinterpret_cast<VkExtensionProperties*>(
266 malloc(sizeof(VkExtensionProperties) * count));
267 if (!exts) {
268 ALOGE("failed to allocate HAL instance extension array");
269 return false;
270 }
271
272 if (dev_->EnumerateInstanceExtensionProperties(nullptr, &count, exts) !=
273 VK_SUCCESS) {
274 ALOGE("failed to enumerate HAL instance extensions");
275 free(exts);
276 return false;
277 }
278
279 for (uint32_t i = 0; i < count; i++) {
280 if (strcmp(exts[i].extensionName, VK_EXT_DEBUG_REPORT_EXTENSION_NAME) ==
281 0) {
282 debug_report_index_ = static_cast<int>(i);
283 break;
284 }
285 }
286
287 free(exts);
288
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800289 return true;
290}
291
292CreateInfoWrapper::CreateInfoWrapper(const VkInstanceCreateInfo& create_info,
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800293 const VkAllocationCallbacks& allocator)
294 : is_instance_(true),
295 allocator_(allocator),
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800296 physical_dev_(VK_NULL_HANDLE),
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800297 instance_info_(create_info),
298 extension_filter_() {
299 hook_extensions_.set(ProcHook::EXTENSION_CORE);
300 hal_extensions_.set(ProcHook::EXTENSION_CORE);
301}
302
Chia-I Wu4901db72016-03-24 16:38:58 +0800303CreateInfoWrapper::CreateInfoWrapper(VkPhysicalDevice physical_dev,
304 const VkDeviceCreateInfo& create_info,
305 const VkAllocationCallbacks& allocator)
306 : is_instance_(false),
307 allocator_(allocator),
308 physical_dev_(physical_dev),
309 dev_info_(create_info),
310 extension_filter_() {
311 hook_extensions_.set(ProcHook::EXTENSION_CORE);
312 hal_extensions_.set(ProcHook::EXTENSION_CORE);
313}
314
315CreateInfoWrapper::~CreateInfoWrapper() {
316 allocator_.pfnFree(allocator_.pUserData, extension_filter_.exts);
317 allocator_.pfnFree(allocator_.pUserData, extension_filter_.names);
318}
319
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800320VkResult CreateInfoWrapper::Validate() {
321 VkResult result = SanitizePNext();
Chia-I Wu4901db72016-03-24 16:38:58 +0800322 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800323 result = SanitizeLayers();
Chia-I Wu4901db72016-03-24 16:38:58 +0800324 if (result == VK_SUCCESS)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800325 result = SanitizeExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +0800326
327 return result;
328}
329
330const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800331CreateInfoWrapper::GetHookExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800332 return hook_extensions_;
333}
334
335const std::bitset<ProcHook::EXTENSION_COUNT>&
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800336CreateInfoWrapper::GetHalExtensions() const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800337 return hal_extensions_;
338}
339
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800340CreateInfoWrapper::operator const VkInstanceCreateInfo*() const {
341 return &instance_info_;
342}
343
Chia-I Wu4901db72016-03-24 16:38:58 +0800344CreateInfoWrapper::operator const VkDeviceCreateInfo*() const {
345 return &dev_info_;
346}
347
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800348VkResult CreateInfoWrapper::SanitizePNext() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800349 const struct StructHeader {
350 VkStructureType type;
351 const void* next;
352 } * header;
353
354 if (is_instance_) {
355 header = reinterpret_cast<const StructHeader*>(instance_info_.pNext);
356
357 // skip leading VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFOs
358 while (header &&
359 header->type == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO)
360 header = reinterpret_cast<const StructHeader*>(header->next);
361
362 instance_info_.pNext = header;
363 } else {
364 header = reinterpret_cast<const StructHeader*>(dev_info_.pNext);
365
366 // skip leading VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFOs
367 while (header &&
368 header->type == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO)
369 header = reinterpret_cast<const StructHeader*>(header->next);
370
371 dev_info_.pNext = header;
372 }
373
374 return VK_SUCCESS;
375}
376
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800377VkResult CreateInfoWrapper::SanitizeLayers() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800378 auto& layer_names = (is_instance_) ? instance_info_.ppEnabledLayerNames
379 : dev_info_.ppEnabledLayerNames;
380 auto& layer_count = (is_instance_) ? instance_info_.enabledLayerCount
381 : dev_info_.enabledLayerCount;
382
383 // remove all layers
384 layer_names = nullptr;
385 layer_count = 0;
386
387 return VK_SUCCESS;
388}
389
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800390VkResult CreateInfoWrapper::SanitizeExtensions() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800391 auto& ext_names = (is_instance_) ? instance_info_.ppEnabledExtensionNames
392 : dev_info_.ppEnabledExtensionNames;
393 auto& ext_count = (is_instance_) ? instance_info_.enabledExtensionCount
394 : dev_info_.enabledExtensionCount;
395 if (!ext_count)
396 return VK_SUCCESS;
397
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800398 VkResult result = InitExtensionFilter();
Chia-I Wu4901db72016-03-24 16:38:58 +0800399 if (result != VK_SUCCESS)
400 return result;
401
402 for (uint32_t i = 0; i < ext_count; i++)
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800403 FilterExtension(ext_names[i]);
Chia-I Wu4901db72016-03-24 16:38:58 +0800404
Jesse Hall9f5c65d2018-03-05 13:34:45 -0800405 // Enable device extensions that contain physical-device commands, so that
406 // vkGetInstanceProcAddr will return those physical-device commands.
407 if (is_instance_) {
408 hook_extensions_.set(ProcHook::KHR_swapchain);
409 }
410
Chia-I Wu4901db72016-03-24 16:38:58 +0800411 ext_names = extension_filter_.names;
412 ext_count = extension_filter_.name_count;
413
414 return VK_SUCCESS;
415}
416
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800417VkResult CreateInfoWrapper::QueryExtensionCount(uint32_t& count) const {
Chia-I Wu4901db72016-03-24 16:38:58 +0800418 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800419 return Hal::Device().EnumerateInstanceExtensionProperties(
420 nullptr, &count, nullptr);
Chia-I Wu4901db72016-03-24 16:38:58 +0800421 } else {
422 const auto& driver = GetData(physical_dev_).driver;
423 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
424 &count, nullptr);
425 }
426}
427
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800428VkResult CreateInfoWrapper::EnumerateExtensions(
Chia-I Wu4901db72016-03-24 16:38:58 +0800429 uint32_t& count,
430 VkExtensionProperties* props) const {
431 if (is_instance_) {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800432 return Hal::Device().EnumerateInstanceExtensionProperties(
433 nullptr, &count, props);
Chia-I Wu4901db72016-03-24 16:38:58 +0800434 } else {
435 const auto& driver = GetData(physical_dev_).driver;
436 return driver.EnumerateDeviceExtensionProperties(physical_dev_, nullptr,
437 &count, props);
438 }
439}
440
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800441VkResult CreateInfoWrapper::InitExtensionFilter() {
Chia-I Wu4901db72016-03-24 16:38:58 +0800442 // query extension count
443 uint32_t count;
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800444 VkResult result = QueryExtensionCount(count);
Chia-I Wu4901db72016-03-24 16:38:58 +0800445 if (result != VK_SUCCESS || count == 0)
446 return result;
447
448 auto& filter = extension_filter_;
449 filter.exts =
450 reinterpret_cast<VkExtensionProperties*>(allocator_.pfnAllocation(
451 allocator_.pUserData, sizeof(VkExtensionProperties) * count,
452 alignof(VkExtensionProperties),
453 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
454 if (!filter.exts)
455 return VK_ERROR_OUT_OF_HOST_MEMORY;
456
457 // enumerate extensions
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800458 result = EnumerateExtensions(count, filter.exts);
Chia-I Wu4901db72016-03-24 16:38:58 +0800459 if (result != VK_SUCCESS && result != VK_INCOMPLETE)
460 return result;
461
462 if (!count)
463 return VK_SUCCESS;
464
465 filter.ext_count = count;
466
467 // allocate name array
468 uint32_t enabled_ext_count = (is_instance_)
469 ? instance_info_.enabledExtensionCount
470 : dev_info_.enabledExtensionCount;
471 count = std::min(filter.ext_count, enabled_ext_count);
472 filter.names = reinterpret_cast<const char**>(allocator_.pfnAllocation(
473 allocator_.pUserData, sizeof(const char*) * count, alignof(const char*),
474 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND));
475 if (!filter.names)
476 return VK_ERROR_OUT_OF_HOST_MEMORY;
477
478 return VK_SUCCESS;
479}
480
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800481void CreateInfoWrapper::FilterExtension(const char* name) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800482 auto& filter = extension_filter_;
483
484 ProcHook::Extension ext_bit = GetProcHookExtension(name);
485 if (is_instance_) {
486 switch (ext_bit) {
487 case ProcHook::KHR_android_surface:
488 case ProcHook::KHR_surface:
Courtney Goeltzenleuchtere278daf2017-02-02 16:54:57 -0700489 case ProcHook::EXT_swapchain_colorspace:
Chris Forbes2452cf72017-03-16 16:30:17 +1300490 case ProcHook::KHR_get_surface_capabilities2:
Chia-I Wu4901db72016-03-24 16:38:58 +0800491 hook_extensions_.set(ext_bit);
492 // return now as these extensions do not require HAL support
493 return;
494 case ProcHook::EXT_debug_report:
495 // both we and HAL can take part in
496 hook_extensions_.set(ext_bit);
497 break;
Chris Forbes6aa30db2017-02-20 17:12:53 +1300498 case ProcHook::KHR_get_physical_device_properties2:
Jesse Hall19cc7542018-03-29 14:46:45 -0700499 case ProcHook::EXTENSION_UNKNOWN:
500 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800501 break;
Jesse Hall19cc7542018-03-29 14:46:45 -0700502
503 case ProcHook::KHR_incremental_present:
504 case ProcHook::KHR_shared_presentable_image:
505 case ProcHook::KHR_swapchain:
506 case ProcHook::EXT_hdr_metadata:
507 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
508 case ProcHook::ANDROID_native_buffer:
509 case ProcHook::GOOGLE_display_timing:
510 case ProcHook::EXTENSION_CORE:
511 case ProcHook::EXTENSION_COUNT:
512 // Device and meta extensions. If we ever get here it's a bug in
513 // our code. But enumerating them lets us avoid having a default
514 // case, and default hides other bugs.
515 ALOGE(
516 "CreateInfoWrapper::FilterExtension: invalid instance "
517 "extension '%s'. FIX ME",
518 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800519 return;
Jesse Hall19cc7542018-03-29 14:46:45 -0700520
521 // Don't use a default case. Without it, -Wswitch will tell us
522 // at compile time if someone adds a new ProcHook extension but
523 // doesn't handle it above. That's a real bug that has
524 // not-immediately-obvious effects.
525 //
526 // default:
527 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800528 }
529 } else {
530 switch (ext_bit) {
531 case ProcHook::KHR_swapchain:
532 // map VK_KHR_swapchain to VK_ANDROID_native_buffer
533 name = VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME;
534 ext_bit = ProcHook::ANDROID_native_buffer;
535 break;
Ian Elliott9e853732017-02-03 11:24:07 -0700536 case ProcHook::KHR_incremental_present:
Ian Elliott8a977262017-01-19 09:05:58 -0700537 case ProcHook::GOOGLE_display_timing:
Chris Forbesfa25e632017-02-22 12:36:02 +1300538 case ProcHook::KHR_shared_presentable_image:
Ian Elliott8a977262017-01-19 09:05:58 -0700539 hook_extensions_.set(ext_bit);
540 // return now as these extensions do not require HAL support
541 return;
Courtney Goeltzenleuchterd634c482017-01-05 15:55:31 -0700542 case ProcHook::EXT_hdr_metadata:
543 hook_extensions_.set(ext_bit);
544 break;
Jesse Hall19cc7542018-03-29 14:46:45 -0700545 case ProcHook::ANDROID_external_memory_android_hardware_buffer:
Chia-I Wu4901db72016-03-24 16:38:58 +0800546 case ProcHook::EXTENSION_UNKNOWN:
Jesse Hall19cc7542018-03-29 14:46:45 -0700547 // Extensions we don't need to do anything about at this level
Chia-I Wu4901db72016-03-24 16:38:58 +0800548 break;
Jesse Hall19cc7542018-03-29 14:46:45 -0700549
550 case ProcHook::KHR_android_surface:
551 case ProcHook::KHR_get_physical_device_properties2:
552 case ProcHook::KHR_get_surface_capabilities2:
553 case ProcHook::KHR_surface:
554 case ProcHook::EXT_debug_report:
555 case ProcHook::EXT_swapchain_colorspace:
556 case ProcHook::ANDROID_native_buffer:
557 case ProcHook::EXTENSION_CORE:
558 case ProcHook::EXTENSION_COUNT:
559 // Instance and meta extensions. If we ever get here it's a bug
560 // in our code. But enumerating them lets us avoid having a
561 // default case, and default hides other bugs.
562 ALOGE(
563 "CreateInfoWrapper::FilterExtension: invalid device "
564 "extension '%s'. FIX ME",
565 name);
Chia-I Wu4901db72016-03-24 16:38:58 +0800566 return;
Jesse Hall19cc7542018-03-29 14:46:45 -0700567
568 // Don't use a default case. Without it, -Wswitch will tell us
569 // at compile time if someone adds a new ProcHook extension but
570 // doesn't handle it above. That's a real bug that has
571 // not-immediately-obvious effects.
572 //
573 // default:
574 // break;
Chia-I Wu4901db72016-03-24 16:38:58 +0800575 }
576 }
577
578 for (uint32_t i = 0; i < filter.ext_count; i++) {
579 const VkExtensionProperties& props = filter.exts[i];
580 // ignore unknown extensions
581 if (strcmp(name, props.extensionName) != 0)
582 continue;
583
Chia-I Wu4901db72016-03-24 16:38:58 +0800584 filter.names[filter.name_count++] = name;
Chia-I Wu1600e262016-04-12 09:40:06 +0800585 if (ext_bit != ProcHook::EXTENSION_UNKNOWN) {
586 if (ext_bit == ProcHook::ANDROID_native_buffer)
587 hook_extensions_.set(ProcHook::KHR_swapchain);
588
589 hal_extensions_.set(ext_bit);
590 }
Chia-I Wu4901db72016-03-24 16:38:58 +0800591
592 break;
593 }
594}
595
Ian Elliott1d990b22017-11-02 10:15:13 -0600596void CreateInfoWrapper::DowngradeApiVersion() {
597 // If pApplicationInfo is NULL, apiVersion is assumed to be 1.0:
598 if (instance_info_.pApplicationInfo) {
599 application_info_ = *instance_info_.pApplicationInfo;
600 instance_info_.pApplicationInfo = &application_info_;
601 application_info_.apiVersion = VK_API_VERSION_1_0;
602 }
603}
604
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800605VKAPI_ATTR void* DefaultAllocate(void*,
606 size_t size,
607 size_t alignment,
608 VkSystemAllocationScope) {
609 void* ptr = nullptr;
610 // Vulkan requires 'alignment' to be a power of two, but posix_memalign
611 // additionally requires that it be at least sizeof(void*).
612 int ret = posix_memalign(&ptr, std::max(alignment, sizeof(void*)), size);
613 ALOGD_CALLSTACK("Allocate: size=%zu align=%zu => (%d) %p", size, alignment,
614 ret, ptr);
615 return ret == 0 ? ptr : nullptr;
616}
617
618VKAPI_ATTR void* DefaultReallocate(void*,
619 void* ptr,
620 size_t size,
621 size_t alignment,
622 VkSystemAllocationScope) {
623 if (size == 0) {
624 free(ptr);
625 return nullptr;
626 }
627
628 // TODO(jessehall): Right now we never shrink allocations; if the new
629 // request is smaller than the existing chunk, we just continue using it.
630 // Right now the loader never reallocs, so this doesn't matter. If that
631 // changes, or if this code is copied into some other project, this should
632 // probably have a heuristic to allocate-copy-free when doing so will save
633 // "enough" space.
634 size_t old_size = ptr ? malloc_usable_size(ptr) : 0;
635 if (size <= old_size)
636 return ptr;
637
638 void* new_ptr = nullptr;
639 if (posix_memalign(&new_ptr, std::max(alignment, sizeof(void*)), size) != 0)
640 return nullptr;
641 if (ptr) {
642 memcpy(new_ptr, ptr, std::min(old_size, size));
643 free(ptr);
644 }
645 return new_ptr;
646}
647
648VKAPI_ATTR void DefaultFree(void*, void* ptr) {
649 ALOGD_CALLSTACK("Free: %p", ptr);
650 free(ptr);
651}
652
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800653InstanceData* AllocateInstanceData(const VkAllocationCallbacks& allocator) {
654 void* data_mem = allocator.pfnAllocation(
655 allocator.pUserData, sizeof(InstanceData), alignof(InstanceData),
656 VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
657 if (!data_mem)
658 return nullptr;
659
660 return new (data_mem) InstanceData(allocator);
661}
662
663void FreeInstanceData(InstanceData* data,
664 const VkAllocationCallbacks& allocator) {
665 data->~InstanceData();
666 allocator.pfnFree(allocator.pUserData, data);
667}
668
Chia-I Wu950d6e12016-05-03 09:12:35 +0800669DeviceData* AllocateDeviceData(
670 const VkAllocationCallbacks& allocator,
671 const DebugReportCallbackList& debug_report_callbacks) {
Chia-I Wu4901db72016-03-24 16:38:58 +0800672 void* data_mem = allocator.pfnAllocation(
673 allocator.pUserData, sizeof(DeviceData), alignof(DeviceData),
674 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
675 if (!data_mem)
676 return nullptr;
677
Chia-I Wu950d6e12016-05-03 09:12:35 +0800678 return new (data_mem) DeviceData(allocator, debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +0800679}
680
681void FreeDeviceData(DeviceData* data, const VkAllocationCallbacks& allocator) {
682 data->~DeviceData();
683 allocator.pfnFree(allocator.pUserData, data);
684}
685
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800686} // anonymous namespace
687
Chia-I Wu9d518162016-03-24 14:55:27 +0800688bool Debuggable() {
689 return (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) >= 0);
690}
691
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800692bool OpenHAL() {
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800693 return Hal::Open();
Chia-I Wu136b8eb2016-03-24 15:01:52 +0800694}
695
Chia-I Wudbb7e9c2016-03-24 15:09:38 +0800696const VkAllocationCallbacks& GetDefaultAllocator() {
697 static const VkAllocationCallbacks kDefaultAllocCallbacks = {
698 .pUserData = nullptr,
699 .pfnAllocation = DefaultAllocate,
700 .pfnReallocation = DefaultReallocate,
701 .pfnFree = DefaultFree,
702 };
703
704 return kDefaultAllocCallbacks;
705}
706
Chia-I Wueb7db122016-03-24 09:11:06 +0800707PFN_vkVoidFunction GetInstanceProcAddr(VkInstance instance, const char* pName) {
708 const ProcHook* hook = GetProcHook(pName);
709 if (!hook)
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800710 return Hal::Device().GetInstanceProcAddr(instance, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800711
712 if (!instance) {
713 if (hook->type == ProcHook::GLOBAL)
714 return hook->proc;
715
Chia-I Wu109f8982016-04-22 06:40:40 +0800716 // v0 layers expect
717 //
718 // vkGetInstanceProcAddr(VK_NULL_HANDLE, "vkCreateDevice");
719 //
720 // to work.
721 if (strcmp(pName, "vkCreateDevice") == 0)
722 return hook->proc;
723
Chia-I Wueb7db122016-03-24 09:11:06 +0800724 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800725 "internal vkGetInstanceProcAddr called for %s without an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800726 pName);
727
Chia-I Wu109f8982016-04-22 06:40:40 +0800728 return nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800729 }
730
731 PFN_vkVoidFunction proc;
732
733 switch (hook->type) {
734 case ProcHook::INSTANCE:
735 proc = (GetData(instance).hook_extensions[hook->extension])
736 ? hook->proc
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800737 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800738 break;
739 case ProcHook::DEVICE:
740 proc = (hook->extension == ProcHook::EXTENSION_CORE)
741 ? hook->proc
742 : hook->checked_proc;
743 break;
744 default:
745 ALOGE(
Chia-I Wue201c3f2016-05-03 13:26:08 +0800746 "internal vkGetInstanceProcAddr called for %s with an instance",
Chia-I Wueb7db122016-03-24 09:11:06 +0800747 pName);
748 proc = nullptr;
749 break;
750 }
751
752 return proc;
753}
754
755PFN_vkVoidFunction GetDeviceProcAddr(VkDevice device, const char* pName) {
756 const ProcHook* hook = GetProcHook(pName);
757 if (!hook)
Chia-I Wucc5e2762016-03-24 13:01:16 +0800758 return GetData(device).driver.GetDeviceProcAddr(device, pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800759
760 if (hook->type != ProcHook::DEVICE) {
Chia-I Wue201c3f2016-05-03 13:26:08 +0800761 ALOGE("internal vkGetDeviceProcAddr called for %s", pName);
Chia-I Wueb7db122016-03-24 09:11:06 +0800762 return nullptr;
763 }
764
Chia-I Wu36cc00a2016-04-13 16:52:06 +0800765 return (GetData(device).hook_extensions[hook->extension]) ? hook->proc
766 : nullptr;
Chia-I Wueb7db122016-03-24 09:11:06 +0800767}
768
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800769VkResult EnumerateInstanceExtensionProperties(
770 const char* pLayerName,
771 uint32_t* pPropertyCount,
772 VkExtensionProperties* pProperties) {
Ian Elliott34a327b2017-03-28 13:20:35 -0600773
774 android::Vector<VkExtensionProperties> loader_extensions;
775 loader_extensions.push_back({
776 VK_KHR_SURFACE_EXTENSION_NAME,
777 VK_KHR_SURFACE_SPEC_VERSION});
778 loader_extensions.push_back({
779 VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
780 VK_KHR_ANDROID_SURFACE_SPEC_VERSION});
781 loader_extensions.push_back({
782 VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
783 VK_EXT_SWAPCHAIN_COLOR_SPACE_SPEC_VERSION});
Chris Forbes16095002017-05-05 15:33:29 -0700784 loader_extensions.push_back({
785 VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
786 VK_KHR_GET_SURFACE_CAPABILITIES_2_SPEC_VERSION});
Ian Elliott34a327b2017-03-28 13:20:35 -0600787
Chia-I Wu31938252016-05-23 15:31:02 +0800788 static const VkExtensionProperties loader_debug_report_extension = {
789 VK_EXT_DEBUG_REPORT_EXTENSION_NAME, VK_EXT_DEBUG_REPORT_SPEC_VERSION,
790 };
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800791
792 // enumerate our extensions first
793 if (!pLayerName && pProperties) {
794 uint32_t count = std::min(
795 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
796
797 std::copy_n(loader_extensions.begin(), count, pProperties);
798
799 if (count < loader_extensions.size()) {
800 *pPropertyCount = count;
801 return VK_INCOMPLETE;
802 }
803
804 pProperties += count;
805 *pPropertyCount -= count;
Chia-I Wu31938252016-05-23 15:31:02 +0800806
807 if (Hal::Get().GetDebugReportIndex() < 0) {
808 if (!*pPropertyCount) {
809 *pPropertyCount = count;
810 return VK_INCOMPLETE;
811 }
812
813 pProperties[0] = loader_debug_report_extension;
814 pProperties += 1;
815 *pPropertyCount -= 1;
816 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800817 }
818
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800819 VkResult result = Hal::Device().EnumerateInstanceExtensionProperties(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800820 pLayerName, pPropertyCount, pProperties);
821
Chia-I Wu31938252016-05-23 15:31:02 +0800822 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
823 int idx = Hal::Get().GetDebugReportIndex();
824 if (idx < 0) {
825 *pPropertyCount += 1;
826 } else if (pProperties &&
827 static_cast<uint32_t>(idx) < *pPropertyCount) {
828 pProperties[idx].specVersion =
829 std::min(pProperties[idx].specVersion,
830 loader_debug_report_extension.specVersion);
831 }
832
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800833 *pPropertyCount += loader_extensions.size();
Chia-I Wu31938252016-05-23 15:31:02 +0800834 }
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800835
836 return result;
837}
838
Chris Forbesfa25e632017-02-22 12:36:02 +1300839bool QueryPresentationProperties(
840 VkPhysicalDevice physicalDevice,
841 VkPhysicalDevicePresentationPropertiesANDROID *presentation_properties)
842{
843 const InstanceData& data = GetData(physicalDevice);
844
845 // GPDP2 must be present and enabled on the instance.
Yiwei Zhangc1c9d3c2018-03-13 17:12:11 -0700846 if (!data.driver.GetPhysicalDeviceProperties2KHR &&
847 !data.driver.GetPhysicalDeviceProperties2)
Chris Forbesfa25e632017-02-22 12:36:02 +1300848 return false;
849
850 // Request the android-specific presentation properties via GPDP2
851 VkPhysicalDeviceProperties2KHR properties = {
852 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
853 presentation_properties,
854 {}
855 };
856
857#pragma clang diagnostic push
858#pragma clang diagnostic ignored "-Wold-style-cast"
859 presentation_properties->sType =
860 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENTATION_PROPERTIES_ANDROID;
861#pragma clang diagnostic pop
862 presentation_properties->pNext = nullptr;
863 presentation_properties->sharedImage = VK_FALSE;
864
Yiwei Zhangc1c9d3c2018-03-13 17:12:11 -0700865 if (data.driver.GetPhysicalDeviceProperties2KHR) {
866 data.driver.GetPhysicalDeviceProperties2KHR(physicalDevice,
867 &properties);
868 } else {
869 data.driver.GetPhysicalDeviceProperties2(physicalDevice, &properties);
870 }
Chris Forbesfa25e632017-02-22 12:36:02 +1300871
872 return true;
873}
874
Chia-I Wu01cf3052016-03-24 16:16:21 +0800875VkResult EnumerateDeviceExtensionProperties(
876 VkPhysicalDevice physicalDevice,
877 const char* pLayerName,
878 uint32_t* pPropertyCount,
879 VkExtensionProperties* pProperties) {
880 const InstanceData& data = GetData(physicalDevice);
Chris Forbesfa25e632017-02-22 12:36:02 +1300881 // extensions that are unconditionally exposed by the loader
882 android::Vector<VkExtensionProperties> loader_extensions;
883 loader_extensions.push_back({
884 VK_KHR_INCREMENTAL_PRESENT_EXTENSION_NAME,
885 VK_KHR_INCREMENTAL_PRESENT_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300886
Chris Forbes16095002017-05-05 15:33:29 -0700887 VkPhysicalDevicePresentationPropertiesANDROID presentation_properties;
888 if (QueryPresentationProperties(physicalDevice, &presentation_properties) &&
889 presentation_properties.sharedImage) {
890 loader_extensions.push_back({
891 VK_KHR_SHARED_PRESENTABLE_IMAGE_EXTENSION_NAME,
892 VK_KHR_SHARED_PRESENTABLE_IMAGE_SPEC_VERSION});
Chris Forbesfa25e632017-02-22 12:36:02 +1300893 }
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700894
Ian Elliott5c34de22017-04-10 14:42:30 -0600895 // conditionally add VK_GOOGLE_display_timing if present timestamps are
896 // supported by the driver:
Wei Wangf9b05ee2017-07-19 20:59:39 -0700897 const std::string timestamp_property("service.sf.present_timestamp");
898 android::base::WaitForPropertyCreation(timestamp_property);
899 if (android::base::GetBoolProperty(timestamp_property, true)) {
Ian Elliott5c34de22017-04-10 14:42:30 -0600900 loader_extensions.push_back({
901 VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME,
902 VK_GOOGLE_DISPLAY_TIMING_SPEC_VERSION});
903 }
904
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700905 // enumerate our extensions first
906 if (!pLayerName && pProperties) {
907 uint32_t count = std::min(
908 *pPropertyCount, static_cast<uint32_t>(loader_extensions.size()));
909
910 std::copy_n(loader_extensions.begin(), count, pProperties);
911
912 if (count < loader_extensions.size()) {
913 *pPropertyCount = count;
914 return VK_INCOMPLETE;
915 }
916
917 pProperties += count;
918 *pPropertyCount -= count;
919 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800920
921 VkResult result = data.driver.EnumerateDeviceExtensionProperties(
922 physicalDevice, pLayerName, pPropertyCount, pProperties);
Chia-I Wu01cf3052016-03-24 16:16:21 +0800923
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700924 if (pProperties) {
925 // map VK_ANDROID_native_buffer to VK_KHR_swapchain
926 for (uint32_t i = 0; i < *pPropertyCount; i++) {
927 auto& prop = pProperties[i];
Chia-I Wu01cf3052016-03-24 16:16:21 +0800928
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700929 if (strcmp(prop.extensionName,
930 VK_ANDROID_NATIVE_BUFFER_EXTENSION_NAME) != 0)
931 continue;
Chia-I Wu01cf3052016-03-24 16:16:21 +0800932
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700933 memcpy(prop.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME,
934 sizeof(VK_KHR_SWAPCHAIN_EXTENSION_NAME));
935 prop.specVersion = VK_KHR_SWAPCHAIN_SPEC_VERSION;
936 }
937 }
Chia-I Wu01cf3052016-03-24 16:16:21 +0800938
Ian Elliottd4b50aa2017-01-09 16:21:36 -0700939 // restore loader extension count
940 if (!pLayerName && (result == VK_SUCCESS || result == VK_INCOMPLETE)) {
941 *pPropertyCount += loader_extensions.size();
Chia-I Wu01cf3052016-03-24 16:16:21 +0800942 }
943
944 return result;
945}
946
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800947VkResult CreateInstance(const VkInstanceCreateInfo* pCreateInfo,
948 const VkAllocationCallbacks* pAllocator,
949 VkInstance* pInstance) {
950 const VkAllocationCallbacks& data_allocator =
951 (pAllocator) ? *pAllocator : GetDefaultAllocator();
952
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800953 CreateInfoWrapper wrapper(*pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800954 VkResult result = wrapper.Validate();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800955 if (result != VK_SUCCESS)
956 return result;
957
958 InstanceData* data = AllocateInstanceData(data_allocator);
959 if (!data)
960 return VK_ERROR_OUT_OF_HOST_MEMORY;
961
Chia-I Wu3e6c2d62016-04-11 13:55:56 +0800962 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800963
Ian Elliott1d990b22017-11-02 10:15:13 -0600964#pragma clang diagnostic push
965#pragma clang diagnostic ignored "-Wold-style-cast"
966 uint32_t api_version = ((pCreateInfo->pApplicationInfo)
967 ? pCreateInfo->pApplicationInfo->apiVersion
968 : VK_API_VERSION_1_0);
969 uint32_t api_major_version = VK_VERSION_MAJOR(api_version);
970 uint32_t api_minor_version = VK_VERSION_MINOR(api_version);
971 uint32_t icd_api_version;
972 PFN_vkEnumerateInstanceVersion pfn_enumerate_instance_version =
973 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
974 Hal::Device().GetInstanceProcAddr(NULL,
975 "vkEnumerateInstanceVersion"));
976 if (!pfn_enumerate_instance_version) {
977 icd_api_version = VK_API_VERSION_1_0;
978 } else {
979 result = (*pfn_enumerate_instance_version)(&icd_api_version);
980 }
981 uint32_t icd_api_major_version = VK_VERSION_MAJOR(icd_api_version);
982 uint32_t icd_api_minor_version = VK_VERSION_MINOR(icd_api_version);
983
984 if ((icd_api_major_version == 1) && (icd_api_minor_version == 0) &&
985 ((api_major_version > 1) || (api_minor_version > 0))) {
986 api_version = VK_API_VERSION_1_0;
987 wrapper.DowngradeApiVersion();
988 }
989#pragma clang diagnostic pop
990
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800991 // call into the driver
992 VkInstance instance;
Chia-I Wu31b2e4f2016-05-23 10:47:57 +0800993 result = Hal::Device().CreateInstance(
Chia-I Wuff4a6c72016-03-24 16:05:56 +0800994 static_cast<const VkInstanceCreateInfo*>(wrapper), pAllocator,
995 &instance);
996 if (result != VK_SUCCESS) {
997 FreeInstanceData(data, data_allocator);
998 return result;
999 }
1000
1001 // initialize InstanceDriverTable
1002 if (!SetData(instance, *data) ||
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001003 !InitDriverTable(instance, Hal::Device().GetInstanceProcAddr,
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001004 wrapper.GetHalExtensions())) {
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001005 data->driver.DestroyInstance = reinterpret_cast<PFN_vkDestroyInstance>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001006 Hal::Device().GetInstanceProcAddr(instance, "vkDestroyInstance"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001007 if (data->driver.DestroyInstance)
1008 data->driver.DestroyInstance(instance, pAllocator);
1009
1010 FreeInstanceData(data, data_allocator);
1011
1012 return VK_ERROR_INCOMPATIBLE_DRIVER;
1013 }
1014
1015 data->get_device_proc_addr = reinterpret_cast<PFN_vkGetDeviceProcAddr>(
Chia-I Wu31b2e4f2016-05-23 10:47:57 +08001016 Hal::Device().GetInstanceProcAddr(instance, "vkGetDeviceProcAddr"));
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001017 if (!data->get_device_proc_addr) {
1018 data->driver.DestroyInstance(instance, pAllocator);
1019 FreeInstanceData(data, data_allocator);
1020
1021 return VK_ERROR_INCOMPATIBLE_DRIVER;
1022 }
1023
1024 *pInstance = instance;
1025
1026 return VK_SUCCESS;
1027}
1028
1029void DestroyInstance(VkInstance instance,
1030 const VkAllocationCallbacks* pAllocator) {
1031 InstanceData& data = GetData(instance);
1032 data.driver.DestroyInstance(instance, pAllocator);
1033
1034 VkAllocationCallbacks local_allocator;
1035 if (!pAllocator) {
1036 local_allocator = data.allocator;
1037 pAllocator = &local_allocator;
1038 }
1039
1040 FreeInstanceData(&data, *pAllocator);
1041}
1042
Chia-I Wu4901db72016-03-24 16:38:58 +08001043VkResult CreateDevice(VkPhysicalDevice physicalDevice,
1044 const VkDeviceCreateInfo* pCreateInfo,
1045 const VkAllocationCallbacks* pAllocator,
1046 VkDevice* pDevice) {
1047 const InstanceData& instance_data = GetData(physicalDevice);
1048 const VkAllocationCallbacks& data_allocator =
1049 (pAllocator) ? *pAllocator : instance_data.allocator;
1050
1051 CreateInfoWrapper wrapper(physicalDevice, *pCreateInfo, data_allocator);
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001052 VkResult result = wrapper.Validate();
Chia-I Wu4901db72016-03-24 16:38:58 +08001053 if (result != VK_SUCCESS)
1054 return result;
1055
Chia-I Wu950d6e12016-05-03 09:12:35 +08001056 DeviceData* data = AllocateDeviceData(data_allocator,
1057 instance_data.debug_report_callbacks);
Chia-I Wu4901db72016-03-24 16:38:58 +08001058 if (!data)
1059 return VK_ERROR_OUT_OF_HOST_MEMORY;
1060
Chia-I Wu3e6c2d62016-04-11 13:55:56 +08001061 data->hook_extensions |= wrapper.GetHookExtensions();
Chia-I Wu4901db72016-03-24 16:38:58 +08001062
1063 // call into the driver
1064 VkDevice dev;
1065 result = instance_data.driver.CreateDevice(
1066 physicalDevice, static_cast<const VkDeviceCreateInfo*>(wrapper),
1067 pAllocator, &dev);
1068 if (result != VK_SUCCESS) {
1069 FreeDeviceData(data, data_allocator);
1070 return result;
1071 }
1072
1073 // initialize DeviceDriverTable
1074 if (!SetData(dev, *data) ||
Chia-I Wucbe07ef2016-04-13 15:01:00 +08001075 !InitDriverTable(dev, instance_data.get_device_proc_addr,
1076 wrapper.GetHalExtensions())) {
Chia-I Wu4901db72016-03-24 16:38:58 +08001077 data->driver.DestroyDevice = reinterpret_cast<PFN_vkDestroyDevice>(
1078 instance_data.get_device_proc_addr(dev, "vkDestroyDevice"));
1079 if (data->driver.DestroyDevice)
1080 data->driver.DestroyDevice(dev, pAllocator);
1081
1082 FreeDeviceData(data, data_allocator);
1083
1084 return VK_ERROR_INCOMPATIBLE_DRIVER;
1085 }
Chris Forbesd8277912017-02-10 14:59:59 +13001086
1087 // sanity check ANDROID_native_buffer implementation, whose set of
1088 // entrypoints varies according to the spec version.
1089 if ((wrapper.GetHalExtensions()[ProcHook::ANDROID_native_buffer]) &&
1090 !data->driver.GetSwapchainGrallocUsageANDROID &&
1091 !data->driver.GetSwapchainGrallocUsage2ANDROID) {
1092 ALOGE("Driver's implementation of ANDROID_native_buffer is broken;"
1093 " must expose at least one of "
1094 "vkGetSwapchainGrallocUsageANDROID or "
1095 "vkGetSwapchainGrallocUsage2ANDROID");
1096
1097 data->driver.DestroyDevice(dev, pAllocator);
1098 FreeDeviceData(data, data_allocator);
1099
1100 return VK_ERROR_INCOMPATIBLE_DRIVER;
1101 }
1102
Jesse Hall85bb0c52017-02-09 22:13:02 -08001103 VkPhysicalDeviceProperties properties;
1104 instance_data.driver.GetPhysicalDeviceProperties(physicalDevice,
1105 &properties);
1106
Jesse Halldc225072016-05-30 22:40:14 -07001107 data->driver_device = dev;
Jesse Hall85bb0c52017-02-09 22:13:02 -08001108 data->driver_version = properties.driverVersion;
Chia-I Wu4901db72016-03-24 16:38:58 +08001109
1110 *pDevice = dev;
1111
1112 return VK_SUCCESS;
1113}
1114
1115void DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator) {
1116 DeviceData& data = GetData(device);
1117 data.driver.DestroyDevice(device, pAllocator);
1118
1119 VkAllocationCallbacks local_allocator;
1120 if (!pAllocator) {
1121 local_allocator = data.allocator;
1122 pAllocator = &local_allocator;
1123 }
1124
1125 FreeDeviceData(&data, *pAllocator);
1126}
1127
Chia-I Wuff4a6c72016-03-24 16:05:56 +08001128VkResult EnumeratePhysicalDevices(VkInstance instance,
1129 uint32_t* pPhysicalDeviceCount,
1130 VkPhysicalDevice* pPhysicalDevices) {
1131 const auto& data = GetData(instance);
1132
1133 VkResult result = data.driver.EnumeratePhysicalDevices(
1134 instance, pPhysicalDeviceCount, pPhysicalDevices);
1135 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) && pPhysicalDevices) {
1136 for (uint32_t i = 0; i < *pPhysicalDeviceCount; i++)
1137 SetData(pPhysicalDevices[i], data);
1138 }
1139
1140 return result;
1141}
1142
Daniel Koch09f7bf92017-10-05 00:26:58 -04001143VkResult EnumeratePhysicalDeviceGroups(
1144 VkInstance instance,
1145 uint32_t* pPhysicalDeviceGroupCount,
1146 VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties) {
Yiwei Zhang170d3702018-01-08 17:55:50 -08001147 VkResult result = VK_SUCCESS;
Daniel Koch09f7bf92017-10-05 00:26:58 -04001148 const auto& data = GetData(instance);
1149
Yiwei Zhang170d3702018-01-08 17:55:50 -08001150 if (!data.driver.EnumeratePhysicalDeviceGroups) {
1151 uint32_t device_count = 0;
1152 result = EnumeratePhysicalDevices(instance, &device_count, nullptr);
1153 if (result < 0)
1154 return result;
1155 if (!pPhysicalDeviceGroupProperties) {
1156 *pPhysicalDeviceGroupCount = device_count;
1157 return result;
1158 }
1159
1160 device_count = std::min(device_count, *pPhysicalDeviceGroupCount);
1161 if (!device_count) {
1162 *pPhysicalDeviceGroupCount = 0;
1163 return result;
1164 }
1165
1166 android::Vector<VkPhysicalDevice> devices;
1167 devices.resize(device_count);
1168
1169 result = EnumeratePhysicalDevices(instance, &device_count,
1170 devices.editArray());
1171 if (result < 0)
1172 return result;
1173
1174 devices.resize(device_count);
1175 *pPhysicalDeviceGroupCount = device_count;
1176 for (uint32_t i = 0; i < device_count; ++i) {
1177 pPhysicalDeviceGroupProperties[i].physicalDeviceCount = 1;
1178 pPhysicalDeviceGroupProperties[i].physicalDevices[0] = devices[i];
1179 pPhysicalDeviceGroupProperties[i].subsetAllocation = 0;
1180 }
1181 } else {
1182 result = data.driver.EnumeratePhysicalDeviceGroups(
1183 instance, pPhysicalDeviceGroupCount,
1184 pPhysicalDeviceGroupProperties);
1185 if ((result == VK_SUCCESS || result == VK_INCOMPLETE) &&
1186 *pPhysicalDeviceGroupCount && pPhysicalDeviceGroupProperties) {
1187 for (uint32_t i = 0; i < *pPhysicalDeviceGroupCount; i++) {
1188 for (uint32_t j = 0;
1189 j < pPhysicalDeviceGroupProperties[i].physicalDeviceCount;
1190 j++) {
1191 SetData(
1192 pPhysicalDeviceGroupProperties[i].physicalDevices[j],
Ian Elliott28bd2c32017-10-13 09:21:12 -06001193 data);
Yiwei Zhang170d3702018-01-08 17:55:50 -08001194 }
Ian Elliott28bd2c32017-10-13 09:21:12 -06001195 }
1196 }
Daniel Koch09f7bf92017-10-05 00:26:58 -04001197 }
1198
1199 return result;
1200}
1201
Chia-I Wuba0be412016-03-24 16:24:40 +08001202void GetDeviceQueue(VkDevice device,
1203 uint32_t queueFamilyIndex,
1204 uint32_t queueIndex,
1205 VkQueue* pQueue) {
1206 const auto& data = GetData(device);
1207
1208 data.driver.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
1209 SetData(*pQueue, data);
1210}
1211
Daniel Koch09f7bf92017-10-05 00:26:58 -04001212void GetDeviceQueue2(VkDevice device,
1213 const VkDeviceQueueInfo2* pQueueInfo,
1214 VkQueue* pQueue) {
1215 const auto& data = GetData(device);
1216
1217 data.driver.GetDeviceQueue2(device, pQueueInfo, pQueue);
Yiwei Zhang5f646a02018-02-02 12:49:28 -08001218 if (*pQueue != VK_NULL_HANDLE) SetData(*pQueue, data);
Daniel Koch09f7bf92017-10-05 00:26:58 -04001219}
1220
Chia-I Wu6a58a8a2016-03-24 16:29:51 +08001221VKAPI_ATTR VkResult
1222AllocateCommandBuffers(VkDevice device,
1223 const VkCommandBufferAllocateInfo* pAllocateInfo,
1224 VkCommandBuffer* pCommandBuffers) {
1225 const auto& data = GetData(device);
1226
1227 VkResult result = data.driver.AllocateCommandBuffers(device, pAllocateInfo,
1228 pCommandBuffers);
1229 if (result == VK_SUCCESS) {
1230 for (uint32_t i = 0; i < pAllocateInfo->commandBufferCount; i++)
1231 SetData(pCommandBuffers[i], data);
1232 }
1233
1234 return result;
1235}
1236
Chia-I Wu9d518162016-03-24 14:55:27 +08001237} // namespace driver
1238} // namespace vulkan