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"
10 #include "impeller/core/formats.h"
13 #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
14 #include "vulkan/vulkan_enums.hpp"
15 
16 namespace impeller {
17 namespace testing {
18 
19 TEST(AllocatorVKTest, ToVKImageUsageFlags) {
24  /*supports_memoryless_textures=*/true),
25  vk::ImageUsageFlagBits::eInputAttachment |
26  vk::ImageUsageFlagBits::eColorAttachment |
27  vk::ImageUsageFlagBits::eTransientAttachment);
28 
33  /*supports_memoryless_textures=*/true),
34  vk::ImageUsageFlagBits::eDepthStencilAttachment |
35  vk::ImageUsageFlagBits::eTransientAttachment);
36 }
37 
38 TEST(AllocatorVKTest, MemoryTypeSelectionSingleHeap) {
39  vk::PhysicalDeviceMemoryProperties properties;
40  properties.memoryTypeCount = 1;
41  properties.memoryHeapCount = 1;
42  properties.memoryTypes[0].heapIndex = 0;
43  properties.memoryTypes[0].propertyFlags =
44  vk::MemoryPropertyFlagBits::eDeviceLocal;
45  properties.memoryHeaps[0].size = 1024 * 1024 * 1024;
46  properties.memoryHeaps[0].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
47 
48  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(1, properties), 0);
49  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(2, properties), -1);
50  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(3, properties), 0);
51 }
52 
53 TEST(AllocatorVKTest, MemoryTypeSelectionTwoHeap) {
54  vk::PhysicalDeviceMemoryProperties properties;
55  properties.memoryTypeCount = 2;
56  properties.memoryHeapCount = 2;
57  properties.memoryTypes[0].heapIndex = 0;
58  properties.memoryTypes[0].propertyFlags =
59  vk::MemoryPropertyFlagBits::eHostVisible;
60  properties.memoryHeaps[0].size = 1024 * 1024 * 1024;
61  properties.memoryHeaps[0].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
62 
63  properties.memoryTypes[1].heapIndex = 1;
64  properties.memoryTypes[1].propertyFlags =
65  vk::MemoryPropertyFlagBits::eDeviceLocal;
66  properties.memoryHeaps[1].size = 1024 * 1024 * 1024;
67  properties.memoryHeaps[1].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
68 
69  // First fails because this only looks for eDeviceLocal.
70  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(1, properties), -1);
71  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(2, properties), 1);
72  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(3, properties), 1);
73  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(4, properties), -1);
74 }
75 
76 #ifdef IMPELLER_DEBUG
77 
78 TEST(AllocatorVKTest, RecreateSwapchainWhenSizeChanges) {
79  auto const context = MockVulkanContextBuilder().Build();
80  auto allocator = context->GetResourceAllocator();
81 
82  EXPECT_EQ(reinterpret_cast<AllocatorVK*>(allocator.get())
84  .GetByteSize(),
85  0u);
86 
87  allocator->CreateBuffer(DeviceBufferDescriptor{
89  .size = 1024,
90  });
91 
92  // Usage increases beyond the size of the allocated buffer since VMA will
93  // first allocate large blocks of memory and then suballocate small memory
94  // allocations.
95  EXPECT_EQ(reinterpret_cast<AllocatorVK*>(allocator.get())
96  ->DebugGetHeapUsage()
97  .ConvertTo<MebiBytes>()
98  .GetSize(),
99  16u);
100 }
101 
102 #endif // IMPELLER_DEBUG
103 
104 } // namespace testing
105 } // namespace impeller
constexpr uint64_t GetByteSize() const
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.
static vk::ImageUsageFlags ToVKImageUsageFlags(PixelFormat format, TextureUsageMask usage, StorageMode mode, bool supports_memoryless_textures)
Bytes DebugGetHeapUsage() const override
TEST(AllocationSizeTest, CanCreateTypedAllocations)
AllocationSize< 1 '024u *1 '024u > MebiBytes