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"
11 #include "impeller/core/formats.h"
14 #include "impeller/renderer/backend/vulkan/test/mock_vulkan.h"
15 #include "vulkan/vulkan_enums.hpp"
16 
17 namespace impeller {
18 namespace testing {
19 
20 TEST(AllocatorVKTest, ToVKImageUsageFlags) {
25  /*supports_memoryless_textures=*/true),
26  vk::ImageUsageFlagBits::eInputAttachment |
27  vk::ImageUsageFlagBits::eColorAttachment |
28  vk::ImageUsageFlagBits::eTransientAttachment);
29 
34  /*supports_memoryless_textures=*/true),
35  vk::ImageUsageFlagBits::eDepthStencilAttachment |
36  vk::ImageUsageFlagBits::eTransientAttachment);
37 }
38 
39 TEST(AllocatorVKTest, MemoryTypeSelectionSingleHeap) {
40  vk::PhysicalDeviceMemoryProperties properties;
41  properties.memoryTypeCount = 1;
42  properties.memoryHeapCount = 1;
43  properties.memoryTypes[0].heapIndex = 0;
44  properties.memoryTypes[0].propertyFlags =
45  vk::MemoryPropertyFlagBits::eDeviceLocal;
46  properties.memoryHeaps[0].size = 1024 * 1024 * 1024;
47  properties.memoryHeaps[0].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
48 
49  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(1, properties), 0);
50  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(2, properties), -1);
51  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(3, properties), 0);
52 }
53 
54 TEST(AllocatorVKTest, MemoryTypeSelectionTwoHeap) {
55  vk::PhysicalDeviceMemoryProperties properties;
56  properties.memoryTypeCount = 2;
57  properties.memoryHeapCount = 2;
58  properties.memoryTypes[0].heapIndex = 0;
59  properties.memoryTypes[0].propertyFlags =
60  vk::MemoryPropertyFlagBits::eHostVisible;
61  properties.memoryHeaps[0].size = 1024 * 1024 * 1024;
62  properties.memoryHeaps[0].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
63 
64  properties.memoryTypes[1].heapIndex = 1;
65  properties.memoryTypes[1].propertyFlags =
66  vk::MemoryPropertyFlagBits::eDeviceLocal;
67  properties.memoryHeaps[1].size = 1024 * 1024 * 1024;
68  properties.memoryHeaps[1].flags = vk::MemoryHeapFlagBits::eDeviceLocal;
69 
70  // First fails because this only looks for eDeviceLocal.
71  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(1, properties), -1);
72  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(2, properties), 1);
73  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(3, properties), 1);
74  EXPECT_EQ(AllocatorVK::FindMemoryTypeIndex(4, properties), -1);
75 }
76 
77 TEST(AllocatorVKTest, ImageResourceKeepsVulkanDeviceAlive) {
78  std::shared_ptr<Texture> texture;
79  std::weak_ptr<Allocator> weak_allocator;
80  {
81  auto const context = MockVulkanContextBuilder().Build();
82  weak_allocator = context->GetResourceAllocator();
83  auto allocator = context->GetResourceAllocator();
84 
85  texture = allocator->CreateTexture(TextureDescriptor{
88  .size = {1, 1},
89  });
90  context->Shutdown();
91  }
92 
93  ASSERT_TRUE(weak_allocator.lock());
94 }
95 
96 #ifdef IMPELLER_DEBUG
97 
98 TEST(AllocatorVKTest, RecreateSwapchainWhenSizeChanges) {
99  auto const context = MockVulkanContextBuilder().Build();
100  auto allocator = context->GetResourceAllocator();
101 
102  EXPECT_EQ(reinterpret_cast<AllocatorVK*>(allocator.get())
104  .GetByteSize(),
105  0u);
106 
107  allocator->CreateBuffer(DeviceBufferDescriptor{
109  .size = 1024,
110  });
111 
112  // Usage increases beyond the size of the allocated buffer since VMA will
113  // first allocate large blocks of memory and then suballocate small memory
114  // allocations.
115  EXPECT_EQ(reinterpret_cast<AllocatorVK*>(allocator.get())
116  ->DebugGetHeapUsage()
117  .ConvertTo<MebiBytes>()
118  .GetSize(),
119  16u);
120 }
121 
122 #endif // IMPELLER_DEBUG
123 
124 } // namespace testing
125 } // 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
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...