1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <android/log.h>
#include <driver.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <media/NdkImageReader.h>
#include <vulkan/vulkan.h>
#define LOGI(...) \
__android_log_print(ANDROID_LOG_INFO, "libvulkan_test", __VA_ARGS__)
#define LOGE(...) \
__android_log_print(ANDROID_LOG_ERROR, "libvulkan_test", __VA_ARGS__)
#define VK_CHECK(result) ASSERT_EQ(VK_SUCCESS, result)
namespace android {
namespace libvulkantest {
class AImageReaderVulkanSwapchainTest : public ::testing::Test {
public:
AImageReaderVulkanSwapchainTest() {}
AImageReader* mReader = nullptr;
ANativeWindow* mWindow = nullptr;
VkInstance mVkInstance = VK_NULL_HANDLE;
VkPhysicalDevice mPhysicalDev = VK_NULL_HANDLE;
VkDevice mDevice = VK_NULL_HANDLE;
VkSurfaceKHR mSurface = VK_NULL_HANDLE;
VkQueue mPresentQueue = VK_NULL_HANDLE;
uint32_t mPresentQueueFamily = UINT32_MAX;
VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
void SetUp() override {}
void TearDown() override {}
// ------------------------------------------------------
// Helper methods
// ------------------------------------------------------
void createVulkanInstance(std::vector<const char*>& layers) {
const char* extensions[] = {
VK_KHR_SURFACE_EXTENSION_NAME,
VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
};
VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "AImageReader Vulkan Swapchain Test";
appInfo.applicationVersion = 1;
appInfo.pEngineName = "TestEngine";
appInfo.engineVersion = 1;
appInfo.apiVersion = VK_API_VERSION_1_0;
VkInstanceCreateInfo instInfo{};
instInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instInfo.pApplicationInfo = &appInfo;
instInfo.enabledExtensionCount =
sizeof(extensions) / sizeof(extensions[0]);
instInfo.ppEnabledExtensionNames = extensions;
instInfo.enabledLayerCount = layers.size();
instInfo.ppEnabledLayerNames = layers.data();
VkResult res = vkCreateInstance(&instInfo, nullptr, &mVkInstance);
VK_CHECK(res);
LOGE("Vulkan instance created");
}
void createAImageReader(int width, int height, int format, int maxImages) {
media_status_t status =
AImageReader_new(width, height, format, maxImages, &mReader);
ASSERT_EQ(AMEDIA_OK, status) << "Failed to create AImageReader";
ASSERT_NE(nullptr, mReader) << "AImageReader is null";
// Optionally set a listener
AImageReader_ImageListener listener{};
listener.context = this;
listener.onImageAvailable =
&AImageReaderVulkanSwapchainTest::onImageAvailable;
AImageReader_setImageListener(mReader, &listener);
LOGI("AImageReader created with %dx%d, format=%d", width, height,
format);
}
void getANativeWindowFromReader() {
ASSERT_NE(nullptr, mReader);
media_status_t status = AImageReader_getWindow(mReader, &mWindow);
ASSERT_EQ(AMEDIA_OK, status)
<< "Failed to get ANativeWindow from AImageReader";
ASSERT_NE(nullptr, mWindow) << "ANativeWindow is null";
LOGI("ANativeWindow obtained from AImageReader");
}
void createVulkanSurface() {
ASSERT_NE((VkInstance)VK_NULL_HANDLE, mVkInstance);
ASSERT_NE((ANativeWindow*)nullptr, mWindow);
VkAndroidSurfaceCreateInfoKHR surfaceCreateInfo{};
surfaceCreateInfo.sType =
VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
surfaceCreateInfo.window = mWindow;
VkResult res = vkCreateAndroidSurfaceKHR(
mVkInstance, &surfaceCreateInfo, nullptr, &mSurface);
VK_CHECK(res);
LOGI("Vulkan surface created from ANativeWindow");
}
void pickPhysicalDeviceAndQueueFamily() {
ASSERT_NE((VkInstance)VK_NULL_HANDLE, mVkInstance);
uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(mVkInstance, &deviceCount, nullptr);
ASSERT_GT(deviceCount, 0U) << "No Vulkan physical devices found!";
std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(mVkInstance, &deviceCount, devices.data());
for (auto& dev : devices) {
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(dev, &queueFamilyCount,
nullptr);
std::vector<VkQueueFamilyProperties> queueProps(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(dev, &queueFamilyCount,
queueProps.data());
for (uint32_t i = 0; i < queueFamilyCount; i++) {
VkBool32 support = VK_FALSE;
vkGetPhysicalDeviceSurfaceSupportKHR(dev, i, mSurface,
&support);
if (support == VK_TRUE) {
// Found a queue family that can present
mPhysicalDev = dev;
mPresentQueueFamily = i;
LOGI(
"Physical device found with queue family %u supporting "
"present",
i);
return;
}
}
}
FAIL()
<< "No physical device found that supports present to the surface!";
}
void createDeviceAndGetQueue(std::vector<const char*>& layers,
std::vector<const char*> inExtensions = {}) {
ASSERT_NE((void*)VK_NULL_HANDLE, mPhysicalDev);
ASSERT_NE(UINT32_MAX, mPresentQueueFamily);
float queuePriority = 1.0f;
VkDeviceQueueCreateInfo queueInfo{};
queueInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueInfo.queueFamilyIndex = mPresentQueueFamily;
queueInfo.queueCount = 1;
queueInfo.pQueuePriorities = &queuePriority;
VkDeviceCreateInfo deviceInfo{};
deviceInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
deviceInfo.queueCreateInfoCount = 1;
deviceInfo.pQueueCreateInfos = &queueInfo;
deviceInfo.enabledLayerCount = layers.size();
deviceInfo.ppEnabledLayerNames = layers.data();
std::vector<const char*> extensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
};
for (auto extension : inExtensions) {
extensions.push_back(extension);
}
deviceInfo.enabledExtensionCount = extensions.size();
deviceInfo.ppEnabledExtensionNames = extensions.data();
VkResult res =
vkCreateDevice(mPhysicalDev, &deviceInfo, nullptr, &mDevice);
VK_CHECK(res);
LOGI("Logical device created");
vkGetDeviceQueue(mDevice, mPresentQueueFamily, 0, &mPresentQueue);
ASSERT_NE((VkQueue)VK_NULL_HANDLE, mPresentQueue);
LOGI("Acquired present-capable queue");
}
void createSwapchain() {
ASSERT_NE((VkDevice)VK_NULL_HANDLE, mDevice);
ASSERT_NE((VkSurfaceKHR)VK_NULL_HANDLE, mSurface);
VkSurfaceCapabilitiesKHR surfaceCaps{};
VK_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
mPhysicalDev, mSurface, &surfaceCaps));
uint32_t formatCount = 0;
vkGetPhysicalDeviceSurfaceFormatsKHR(mPhysicalDev, mSurface,
&formatCount, nullptr);
ASSERT_GT(formatCount, 0U);
std::vector<VkSurfaceFormatKHR> formats(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(mPhysicalDev, mSurface,
&formatCount, formats.data());
VkSurfaceFormatKHR chosenFormat = formats[0];
LOGI("Chosen surface format: %d", chosenFormat.format);
uint32_t presentModeCount = 0;
vkGetPhysicalDeviceSurfacePresentModesKHR(mPhysicalDev, mSurface,
&presentModeCount, nullptr);
ASSERT_GT(presentModeCount, 0U);
std::vector<VkPresentModeKHR> presentModes(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(
mPhysicalDev, mSurface, &presentModeCount, presentModes.data());
VkPresentModeKHR chosenPresentMode = VK_PRESENT_MODE_FIFO_KHR;
for (auto mode : presentModes) {
if (mode == VK_PRESENT_MODE_FIFO_KHR) {
chosenPresentMode = mode;
break;
}
}
LOGI("Chosen present mode: %d", chosenPresentMode);
VkExtent2D swapchainExtent{};
if (surfaceCaps.currentExtent.width == 0xFFFFFFFF) {
swapchainExtent.width = 640; // fallback
swapchainExtent.height = 480; // fallback
} else {
swapchainExtent = surfaceCaps.currentExtent;
}
LOGI("Swapchain extent: %d x %d", swapchainExtent.width,
swapchainExtent.height);
uint32_t desiredImageCount = surfaceCaps.minImageCount + 1;
if (surfaceCaps.maxImageCount > 0 &&
desiredImageCount > surfaceCaps.maxImageCount) {
desiredImageCount = surfaceCaps.maxImageCount;
}
VkSwapchainCreateInfoKHR swapchainInfo{};
swapchainInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchainInfo.surface = mSurface;
swapchainInfo.minImageCount = desiredImageCount;
swapchainInfo.imageFormat = chosenFormat.format;
swapchainInfo.imageColorSpace = chosenFormat.colorSpace;
swapchainInfo.imageExtent = swapchainExtent;
swapchainInfo.imageArrayLayers = 1;
swapchainInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
swapchainInfo.preTransform = surfaceCaps.currentTransform;
swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
swapchainInfo.presentMode = chosenPresentMode;
swapchainInfo.clipped = VK_TRUE;
swapchainInfo.oldSwapchain = VK_NULL_HANDLE;
uint32_t queueFamilyIndices[] = {mPresentQueueFamily};
swapchainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchainInfo.queueFamilyIndexCount = 1;
swapchainInfo.pQueueFamilyIndices = queueFamilyIndices;
VkResult res =
vkCreateSwapchainKHR(mDevice, &swapchainInfo, nullptr, &mSwapchain);
if (res == VK_SUCCESS) {
LOGI("Swapchain created successfully");
uint32_t swapchainImageCount = 0;
vkGetSwapchainImagesKHR(mDevice, mSwapchain, &swapchainImageCount,
nullptr);
std::vector<VkImage> swapchainImages(swapchainImageCount);
vkGetSwapchainImagesKHR(mDevice, mSwapchain, &swapchainImageCount,
swapchainImages.data());
LOGI("Swapchain has %u images", swapchainImageCount);
} else {
LOGI("Swapchain creation failed");
}
}
// Image available callback (AImageReader)
static void onImageAvailable(void*, AImageReader* reader) {
LOGI("onImageAvailable callback triggered");
AImage* image = nullptr;
media_status_t status = AImageReader_acquireLatestImage(reader, &image);
if (status != AMEDIA_OK || !image) {
LOGE("Failed to acquire latest image");
return;
}
AImage_delete(image);
LOGI("Released acquired image");
}
void cleanUpSwapchainForTest() {
if (mSwapchain != VK_NULL_HANDLE) {
vkDestroySwapchainKHR(mDevice, mSwapchain, nullptr);
mSwapchain = VK_NULL_HANDLE;
}
if (mDevice != VK_NULL_HANDLE) {
vkDestroyDevice(mDevice, nullptr);
mDevice = VK_NULL_HANDLE;
}
if (mSurface != VK_NULL_HANDLE) {
vkDestroySurfaceKHR(mVkInstance, mSurface, nullptr);
mSurface = VK_NULL_HANDLE;
}
if (mVkInstance != VK_NULL_HANDLE) {
vkDestroyInstance(mVkInstance, nullptr);
mVkInstance = VK_NULL_HANDLE;
}
if (mReader) {
// AImageReader_delete(mReader);
mReader = nullptr;
}
// Note: The ANativeWindow from AImageReader is implicitly
// managed by the reader, so we don't explicitly delete it.
mWindow = nullptr;
}
void buildSwapchianForTest(std::vector<const char*>& instanceLayers,
std::vector<const char*>& deviceLayers) {
createVulkanInstance(instanceLayers);
// the "atest libvulkan_test" command will execute this test as a binary
// (not apk) on the device. Consequently we can't render to the screen
// and need to work around this by using AImageReader*
createAImageReader(640, 480, AIMAGE_FORMAT_PRIVATE, 3);
getANativeWindowFromReader();
createVulkanSurface();
pickPhysicalDeviceAndQueueFamily();
createDeviceAndGetQueue(deviceLayers);
createSwapchain();
}
};
TEST_F(AImageReaderVulkanSwapchainTest, TestHelperMethods) {
// Verify that the basic plumbing/helper functions of these tests is
// working. This doesn't directly test any of the layer code. It only
// verifies that we can successfully create a swapchain with an AImageReader
std::vector<const char*> instanceLayers;
std::vector<const char*> deviceLayers;
buildSwapchianForTest(deviceLayers, instanceLayers);
ASSERT_NE(mVkInstance, (VkInstance)VK_NULL_HANDLE);
ASSERT_NE(mPhysicalDev, (VkPhysicalDevice)VK_NULL_HANDLE);
ASSERT_NE(mDevice, (VkDevice)VK_NULL_HANDLE);
ASSERT_NE(mSurface, (VkSurfaceKHR)VK_NULL_HANDLE);
ASSERT_NE(mSwapchain, (VkSwapchainKHR)VK_NULL_HANDLE);
cleanUpSwapchainForTest();
}
// Passing state in these tests requires global state. Wrap each test in an
// anonymous namespace to prevent conflicting names.
namespace {
VKAPI_ATTR VkResult VKAPI_CALL hookedGetPhysicalDeviceImageFormatProperties2KHR(
VkPhysicalDevice,
const VkPhysicalDeviceImageFormatInfo2*,
VkImageFormatProperties2*) {
return VK_ERROR_SURFACE_LOST_KHR;
}
static PFN_vkGetSwapchainGrallocUsage2ANDROID
pfnNextGetSwapchainGrallocUsage2ANDROID = nullptr;
static bool g_grallocCalled = false;
VKAPI_ATTR VkResult VKAPI_CALL hookGetSwapchainGrallocUsage2ANDROID(
VkDevice device,
VkFormat format,
VkImageUsageFlags imageUsage,
VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
uint64_t* grallocConsumerUsage,
uint64_t* grallocProducerUsage) {
g_grallocCalled = true;
if (pfnNextGetSwapchainGrallocUsage2ANDROID) {
return pfnNextGetSwapchainGrallocUsage2ANDROID(
device, format, imageUsage, swapchainImageUsage,
grallocConsumerUsage, grallocProducerUsage);
}
return VK_ERROR_INITIALIZATION_FAILED;
}
TEST_F(AImageReaderVulkanSwapchainTest, getProducerUsageFallbackTest1) {
// BUG: 379230826
// Verify that getProducerUsage falls back to
// GetSwapchainGrallocUsage*ANDROID if GPDIFP2 fails
std::vector<const char*> instanceLayers = {};
std::vector<const char*> deviceLayers = {};
createVulkanInstance(instanceLayers);
createAImageReader(640, 480, AIMAGE_FORMAT_PRIVATE, 3);
getANativeWindowFromReader();
createVulkanSurface();
pickPhysicalDeviceAndQueueFamily();
createDeviceAndGetQueue(deviceLayers);
auto& pdev = vulkan::driver::GetData(mDevice).driver_physical_device;
auto& pdevDispatchTable = vulkan::driver::GetData(pdev).driver;
auto& deviceDispatchTable = vulkan::driver::GetData(mDevice).driver;
ASSERT_NE(deviceDispatchTable.GetSwapchainGrallocUsage2ANDROID, nullptr);
pdevDispatchTable.GetPhysicalDeviceImageFormatProperties2 =
hookedGetPhysicalDeviceImageFormatProperties2KHR;
deviceDispatchTable.GetSwapchainGrallocUsage2ANDROID =
hookGetSwapchainGrallocUsage2ANDROID;
ASSERT_FALSE(g_grallocCalled);
createSwapchain();
ASSERT_TRUE(g_grallocCalled);
ASSERT_NE(mVkInstance, (VkInstance)VK_NULL_HANDLE);
ASSERT_NE(mPhysicalDev, (VkPhysicalDevice)VK_NULL_HANDLE);
ASSERT_NE(mDevice, (VkDevice)VK_NULL_HANDLE);
ASSERT_NE(mSurface, (VkSurfaceKHR)VK_NULL_HANDLE);
cleanUpSwapchainForTest();
}
} // namespace
// Passing state in these tests requires global state. Wrap each test in an
// anonymous namespace to prevent conflicting names.
namespace {
static bool g_returnNotSupportedOnce = true;
VKAPI_ATTR VkResult VKAPI_CALL
Hook_GetPhysicalDeviceImageFormatProperties2_NotSupportedOnce(
VkPhysicalDevice /*physicalDevice*/,
const VkPhysicalDeviceImageFormatInfo2* /*pImageFormatInfo*/,
VkImageFormatProperties2* /*pImageFormatProperties*/) {
if (g_returnNotSupportedOnce) {
g_returnNotSupportedOnce = false;
return VK_ERROR_FORMAT_NOT_SUPPORTED;
}
return VK_SUCCESS;
}
TEST_F(AImageReaderVulkanSwapchainTest, SurfaceFormats2KHR_IgnoreNotSupported) {
// BUG: 357903074
// Verify that vkGetPhysicalDeviceSurfaceFormats2KHR properly
// ignores VK_ERROR_FORMAT_NOT_SUPPORTED and continues enumerating formats.
std::vector<const char*> instanceLayers;
createVulkanInstance(instanceLayers);
createAImageReader(640, 480, AIMAGE_FORMAT_PRIVATE, 3);
getANativeWindowFromReader();
createVulkanSurface();
pickPhysicalDeviceAndQueueFamily();
auto& pdevDispatchTable = vulkan::driver::GetData(mPhysicalDev).driver;
pdevDispatchTable.GetPhysicalDeviceImageFormatProperties2 =
Hook_GetPhysicalDeviceImageFormatProperties2_NotSupportedOnce;
PFN_vkGetPhysicalDeviceSurfaceFormats2KHR
pfnGetPhysicalDeviceSurfaceFormats2KHR =
reinterpret_cast<PFN_vkGetPhysicalDeviceSurfaceFormats2KHR>(
vkGetInstanceProcAddr(mVkInstance,
"vkGetPhysicalDeviceSurfaceFormats2KHR"));
ASSERT_NE(nullptr, pfnGetPhysicalDeviceSurfaceFormats2KHR)
<< "Could not get pointer to vkGetPhysicalDeviceSurfaceFormats2KHR";
VkPhysicalDeviceSurfaceInfo2KHR surfaceInfo2{};
surfaceInfo2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR;
surfaceInfo2.pNext = nullptr;
surfaceInfo2.surface = mSurface;
uint32_t formatCount = 0;
VkResult res = pfnGetPhysicalDeviceSurfaceFormats2KHR(
mPhysicalDev, &surfaceInfo2, &formatCount, nullptr);
// If the loader never tries a second format, it might fail or 0-out the
// formatCount. The patch ensures it continues to the next format rather
// than bailing out on the first NOT_SUPPORTED.
ASSERT_EQ(VK_SUCCESS, res)
<< "vkGetPhysicalDeviceSurfaceFormats2KHR failed unexpectedly";
ASSERT_GT(formatCount, 0U)
<< "No surface formats found; the loader may have bailed early.";
std::vector<VkSurfaceFormat2KHR> formats(formatCount);
for (auto& f : formats) {
f.sType = VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR;
f.pNext = nullptr;
}
res = pfnGetPhysicalDeviceSurfaceFormats2KHR(mPhysicalDev, &surfaceInfo2,
&formatCount, formats.data());
ASSERT_EQ(VK_SUCCESS, res) << "Failed to retrieve surface formats";
LOGI(
"SurfaceFormats2KHR_IgnoreNotSupported test: found %u formats after "
"ignoring NOT_SUPPORTED",
formatCount);
cleanUpSwapchainForTest();
}
} // namespace
namespace {
TEST_F(AImageReaderVulkanSwapchainTest, MutableFormatSwapchainTest) {
// Test swapchain with mutable format extension
std::vector<const char*> instanceLayers;
std::vector<const char*> deviceLayers;
std::vector<const char*> deviceExtensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME,
VK_KHR_MAINTENANCE2_EXTENSION_NAME,
VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME};
createVulkanInstance(instanceLayers);
createAImageReader(640, 480, AIMAGE_FORMAT_PRIVATE, 3);
getANativeWindowFromReader();
createVulkanSurface();
pickPhysicalDeviceAndQueueFamily();
createDeviceAndGetQueue(deviceLayers, deviceExtensions);
ASSERT_NE((VkDevice)VK_NULL_HANDLE, mDevice);
ASSERT_NE((VkSurfaceKHR)VK_NULL_HANDLE, mSurface);
VkSurfaceCapabilitiesKHR surfaceCaps{};
VK_CHECK(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(mPhysicalDev, mSurface,
&surfaceCaps));
uint32_t formatCount = 0;
vkGetPhysicalDeviceSurfaceFormatsKHR(mPhysicalDev, mSurface, &formatCount,
nullptr);
ASSERT_GT(formatCount, 0U);
std::vector<VkSurfaceFormatKHR> formats(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(mPhysicalDev, mSurface, &formatCount,
formats.data());
VkFormat viewFormats[2] = {formats[0].format, formats[0].format};
if (formatCount > 1) {
viewFormats[1] = formats[1].format;
}
VkImageFormatListCreateInfoKHR formatList{};
formatList.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO_KHR;
formatList.viewFormatCount = 2;
formatList.pViewFormats = viewFormats;
VkSwapchainCreateInfoKHR swapchainInfo{};
swapchainInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchainInfo.pNext = &formatList;
swapchainInfo.surface = mSurface;
swapchainInfo.minImageCount = surfaceCaps.minImageCount + 1;
swapchainInfo.imageFormat = formats[0].format;
swapchainInfo.imageColorSpace = formats[0].colorSpace;
swapchainInfo.imageExtent = surfaceCaps.currentExtent;
swapchainInfo.imageArrayLayers = 1;
swapchainInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchainInfo.preTransform = surfaceCaps.currentTransform;
swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
swapchainInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
swapchainInfo.clipped = VK_TRUE;
swapchainInfo.flags = VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR;
uint32_t queueFamilyIndices[] = {mPresentQueueFamily};
swapchainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchainInfo.queueFamilyIndexCount = 1;
swapchainInfo.pQueueFamilyIndices = queueFamilyIndices;
VkResult res =
vkCreateSwapchainKHR(mDevice, &swapchainInfo, nullptr, &mSwapchain);
if (res == VK_SUCCESS) {
LOGI("Mutable format swapchain created successfully");
uint32_t imageCount = 0;
vkGetSwapchainImagesKHR(mDevice, mSwapchain, &imageCount, nullptr);
ASSERT_GT(imageCount, 0U);
} else {
LOGI(
"Mutable format swapchain creation failed (extension may not be "
"supported)");
}
cleanUpSwapchainForTest();
}
} // namespace
} // namespace libvulkantest
} // namespace android
|