| Flutter Impeller |

allocator_vk_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/testing/testing.h" // IWYU pragma: keep
6 #include "gtest/gtest.h"
7 #include "impeller/core/device_buffer_descriptor.h"
8 #include "impeller/core/formats.h"
9 #include "impeller/renderer/backend/vulkan/allocator_vk.h"
10 #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
11 #include "vulkan/vulkan_enums.hpp"
12
13 namespace impeller {
14 namespace testing {
15
16 TEST(AllocatorVKTest, ToVKImageUsageFlags) {
17 EXPECT_EQ(AllocatorVK::ToVKImageUsageFlags(
18 PixelFormat::kR8G8B8A8UNormInt,
19 static_cast<TextureUsageMask>(TextureUsage::kRenderTarget),
20 StorageMode::kDeviceTransient,
21 /*supports_memoryless_textures=*/true),
22 vk::ImageUsageFlagBits::eInputAttachment |
23 vk::ImageUsageFlagBits::eColorAttachment |
24 vk::ImageUsageFlagBits::eTransientAttachment);
25
26 EXPECT_EQ(AllocatorVK::ToVKImageUsageFlags(
27 PixelFormat::kD24UnormS8Uint,
28 static_cast<TextureUsageMask>(TextureUsage::kRenderTarget),
29 StorageMode::kDeviceTransient,
30 /*supports_memoryless_textures=*/true),
31 vk::ImageUsageFlagBits::eDepthStencilAttachment |
32 vk::ImageUsageFlagBits::eTransientAttachment);
33 }
34
35 TEST(AllocatorVKTest, MemoryTypeSelectionSingleHeap) {
36 vk::PhysicalDeviceMemoryProperties properties;
37 properties.memoryTypeCount = 1;
38 properties.memoryHeapCount = 1;
39 properties.memoryTypes[0].heapIndex = 0;
40 properties.memoryTypes[0].propertyFlags =
41 vk::MemoryPropertyFlagBits::eDeviceLocal;
42 properties.memoryHeaps[0].size = 1024 * 1024 * 1024;
43 properties.memoryHeaps[0].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
44
45 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(1, properties), 0);
46 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(2, properties), -1);
47 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(3, properties), 0);
48 }
49
50 TEST(AllocatorVKTest, MemoryTypeSelectionTwoHeap) {
51 vk::PhysicalDeviceMemoryProperties properties;
52 properties.memoryTypeCount = 2;
53 properties.memoryHeapCount = 2;
54 properties.memoryTypes[0].heapIndex = 0;
55 properties.memoryTypes[0].propertyFlags =
56 vk::MemoryPropertyFlagBits::eHostVisible;
57 properties.memoryHeaps[0].size = 1024 * 1024 * 1024;
58 properties.memoryHeaps[0].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
59
60 properties.memoryTypes[1].heapIndex = 1;
61 properties.memoryTypes[1].propertyFlags =
62 vk::MemoryPropertyFlagBits::eDeviceLocal;
63 properties.memoryHeaps[1].size = 1024 * 1024 * 1024;
64 properties.memoryHeaps[1].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
65
66 // First fails because this only looks for eDeviceLocal.
67 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(1, properties), -1);
68 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(2, properties), 1);
69 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(3, properties), 1);
70 EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(4, properties), -1);
71 }
72
73 #ifdef IMPELLER_DEBUG
74
75 TEST(AllocatorVKTest, RecreateSwapchainWhenSizeChanges) {
76 auto const context = MockVulkanContextBuilder().Build();
77 auto allocator = context->GetResourceAllocator();
78
79 EXPECT_EQ(
80 reinterpret_cast<AllocatorVK*>(allocator.get())->DebugGetHeapUsage(), 0u);
81
82 allocator->CreateBuffer(DeviceBufferDescriptor{
83 .storage_mode = StorageMode::kDevicePrivate,
84 .size = 1024,
85 });
86
87 // Usage increases beyond the size of the allocated buffer since VMA will
88 // first allocate large blocks of memory and then suballocate small memory
89 // allocations.
90 EXPECT_EQ(
91 reinterpret_cast<AllocatorVK*>(allocator.get())->DebugGetHeapUsage(),
92 16u);
93 }
94
95 #endif // IMPELLER_DEBUG
96
97 } // namespace testing
98 } // namespace impeller
impeller::AllocatorVK::FindMemoryTypeIndex
static int32_t FindMemoryTypeIndex(uint32_t memory_type_bits_requirement, vk::PhysicalDeviceMemoryProperties &memory_properties)
Select a matching memory type for the given [memory_type_bits_requirement], or -1 if none is found.
Definition: allocator_vk.cc:173
allocator_vk.h
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:14
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
formats.h
impeller::testing::TEST
TEST(AiksCanvasTest, EmptyCullRect)
Definition: canvas_unittests.cc:18
impeller::TextureUsage::kRenderTarget
@ kRenderTarget
impeller::AllocatorVK::ToVKImageUsageFlags
static vk::ImageUsageFlags ToVKImageUsageFlags(PixelFormat format, TextureUsageMask usage, StorageMode mode, bool supports_memoryless_textures)
Definition: allocator_vk.cc:199
impeller::Mask< TextureUsage >
impeller::StorageMode::kDeviceTransient
@ kDeviceTransient
impeller::AllocatorVK
Definition: allocator_vk.h:19
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::DeviceBufferDescriptor::storage_mode
StorageMode storage_mode
Definition: device_buffer_descriptor.h:15
impeller::PixelFormat::kD24UnormS8Uint
@ kD24UnormS8Uint
device_buffer_descriptor.h
impeller
Definition: aiks_blend_unittests.cc:18
impeller::AllocatorVK::DebugGetHeapUsage
size_t DebugGetHeapUsage() const override
Definition: allocator_vk.cc:512