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