Flutter Impeller
allocator_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 <memory>
6 #include "flutter/testing/testing.h"
10 #include "impeller/geometry/size.h"
11 #include "impeller/renderer/testing/mocks.h"
12 
13 namespace impeller {
14 namespace testing {
15 
16 TEST(AllocatorTest, TextureDescriptorCompatibility) {
17  // Size.
18  {
19  TextureDescriptor desc_a = {.size = ISize(100, 100)};
20  TextureDescriptor desc_b = {.size = ISize(100, 100)};
21  TextureDescriptor desc_c = {.size = ISize(101, 100)};
22 
23  ASSERT_EQ(desc_a, desc_b);
24  ASSERT_NE(desc_a, desc_c);
25  }
26  // Storage Mode.
27  {
31 
32  ASSERT_EQ(desc_a, desc_b);
33  ASSERT_NE(desc_a, desc_c);
34  }
35  // Format.
36  {
40 
41  ASSERT_EQ(desc_a, desc_b);
42  ASSERT_NE(desc_a, desc_c);
43  }
44  // Sample Count.
45  {
49 
50  ASSERT_EQ(desc_a, desc_b);
51  ASSERT_NE(desc_a, desc_c);
52  }
53  // Sample Count.
54  {
58 
59  ASSERT_EQ(desc_a, desc_b);
60  ASSERT_NE(desc_a, desc_c);
61  }
62  // Compression.
63  {
67 
68  ASSERT_EQ(desc_a, desc_b);
69  ASSERT_NE(desc_a, desc_c);
70  }
71  // Mip Count.
72  {
73  TextureDescriptor desc_a = {.mip_count = 1};
74  TextureDescriptor desc_b = {.mip_count = 1};
75  TextureDescriptor desc_c = {.mip_count = 4};
76 
77  ASSERT_EQ(desc_a, desc_b);
78  ASSERT_NE(desc_a, desc_c);
79  }
80 }
81 
82 TEST(AllocatorTest, RangeTest) {
83  {
84  Range a = Range{0, 10};
85  Range b = Range{10, 20};
86  auto merged = a.Merge(b);
87 
88  EXPECT_EQ(merged.offset, 0u);
89  EXPECT_EQ(merged.length, 30u);
90  }
91 
92  {
93  Range a = Range{0, 10};
94  Range b = Range{100, 20};
95  auto merged = a.Merge(b);
96 
97  EXPECT_EQ(merged.offset, 0u);
98  EXPECT_EQ(merged.length, 120u);
99  }
100 
101  {
102  Range a = Range{0, 10};
103  Range b = Range{100, 20};
104  auto merged = b.Merge(a);
105 
106  EXPECT_EQ(merged.offset, 0u);
107  EXPECT_EQ(merged.length, 120u);
108  }
109 
110  {
111  Range a = Range{0, 10};
112  Range b = Range{100, 0};
113  auto merged = b.Merge(a);
114 
115  EXPECT_EQ(merged.offset, 0u);
116  EXPECT_EQ(merged.length, 10u);
117  }
118 
119  {
120  Range a = Range{0, 10};
121  Range b = Range{0, 10};
122  auto merged = b.Merge(a);
123 
124  EXPECT_EQ(merged.offset, 0u);
125  EXPECT_EQ(merged.length, 10u);
126  }
127 }
128 
129 TEST(AllocatorTest, CompressedFormatClassification) {
131  EXPECT_FALSE(IsCompressed(PixelFormat::kR32Float));
136 
145 }
146 
147 TEST(AllocatorTest, CompressedFormatBlockMath) {
148  // Block dimensions: everything here is 4x4 except ASTC 8x8.
150  4u);
155  // Uncompressed formats report a 1x1 block.
157  1u);
158 
159  // Bytes per block: BC1/ETC2-RGB8 are 8 bytes, the rest here are 16.
165  // Uncompressed falls back to bytes-per-pixel.
167 }
168 
169 TEST(AllocatorTest, CompressedFormatRegionByteSizes) {
170  // BC1: 4x4 blocks of 8 bytes. Dimensions round up to whole blocks.
175 
176  // ASTC 8x8: a single 16-byte block covers up to 8x8 texels.
177  EXPECT_EQ(BytesForTextureRegion(PixelFormat::kASTC8x8LDR, 8, 8), 16u);
178  EXPECT_EQ(BytesForTextureRegion(PixelFormat::kASTC8x8LDR, 9, 9), 64u);
179 
180  // Uncompressed math is unchanged: width * height * bytes-per-pixel.
182  40000u);
184  400u);
185 }
186 
187 TEST(AllocatorTest, CompressedTextureDescriptorByteSizes) {
188  {
190  .size = ISize(8, 8)};
191  EXPECT_EQ(desc.GetByteSizeOfBaseMipLevel(), 32u);
192  EXPECT_EQ(desc.GetBytesPerRow(), 16u);
193  }
194  {
196  .size = ISize(16, 16)};
197  EXPECT_EQ(desc.GetByteSizeOfBaseMipLevel(), 64u);
198  }
199 }
200 
201 } // namespace testing
202 } // namespace impeller
TEST(AllocationSizeTest, CanCreateTypedAllocations)
constexpr size_t CompressedBlockHeightForPixelFormat(PixelFormat format)
The height, in texels, of one compression block. Uncompressed formats report 1.
Definition: formats.h:245
@ kASTCHDR
ASTC HDR. A separate device feature from ASTC LDR.
@ kBC
S3TC, RGTC, and BPTC (BC1 through BC7). Desktop GPUs.
@ kETC2
ETC2 and EAC. Mobile, OpenGL ES 3.0, and WebGL2.
@ kASTC
ASTC LDR. Modern mobile and some desktop.
constexpr size_t CompressedBlockWidthForPixelFormat(PixelFormat format)
The width, in texels, of one compression block. Uncompressed formats report 1.
Definition: formats.h:217
constexpr size_t BytesForTextureRegion(PixelFormat format, int64_t width, int64_t height)
The number of bytes required to store a width x height texel region in format. Block-compressed forma...
Definition: formats.h:727
constexpr size_t BytesPerRowForTextureWidth(PixelFormat format, int64_t width)
The number of bytes in a single row of texel blocks for a texture of the given width in format.
Definition: formats.h:741
constexpr size_t BytesPerBlockForPixelFormat(PixelFormat format)
The number of bytes used to store one block of format. For uncompressed formats a block is a single p...
Definition: formats.h:697
constexpr CompressedTextureFamily CompressedTextureFamilyForFormat(PixelFormat format)
The compression family that format belongs to. Only valid for formats where IsCompressed is true.
Definition: formats.h:185
ISize64 ISize
Definition: size.h:162
constexpr bool IsCompressed(PixelFormat format)
Whether format is a block-compressed format.
Definition: formats.h:158
constexpr Range Merge(const Range &other)
Create a new range that is a union of this range and other.
Definition: range.h:27
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
constexpr size_t GetByteSizeOfBaseMipLevel() const
constexpr size_t GetBytesPerRow() const