Flutter Impeller
blit_pass_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 <cstdint>
6 #include "flutter/display_list/dl_builder.h"
9 #include "fml/mapping.h"
10 #include "gtest/gtest.h"
14 #include "impeller/core/formats.h"
18 
19 namespace impeller {
20 namespace testing {
21 
22 using flutter::DisplayListBuilder;
23 using flutter::DlColor;
24 using flutter::DlImageSampling;
25 using flutter::DlPaint;
26 
29 
30 TEST_P(BlitPassTest, BlitAcrossDifferentPixelFormatsFails) {
31  ScopedValidationDisable scope; // avoid noise in output.
32  auto context = GetContext();
33  auto cmd_buffer = context->CreateCommandBuffer();
34  auto blit_pass = cmd_buffer->CreateBlitPass();
35 
36  TextureDescriptor src_desc;
38  src_desc.size = {100, 100};
40  auto src = context->GetResourceAllocator()->CreateTexture(src_desc);
41 
42  TextureDescriptor dst_format;
44  dst_format.size = {100, 100};
46  auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
47 
48  EXPECT_FALSE(blit_pass->AddCopy(src, dst));
49 }
50 
51 TEST_P(BlitPassTest, BlitAcrossDifferentSampleCountsFails) {
52  ScopedValidationDisable scope; // avoid noise in output.
53  auto context = GetContext();
54  auto cmd_buffer = context->CreateCommandBuffer();
55  auto blit_pass = cmd_buffer->CreateBlitPass();
56 
57  TextureDescriptor src_desc;
60  src_desc.size = {100, 100};
61  auto src = context->GetResourceAllocator()->CreateTexture(src_desc);
62 
63  TextureDescriptor dst_format;
65  dst_format.size = {100, 100};
66  auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
67 
68  EXPECT_FALSE(blit_pass->AddCopy(src, dst));
69 }
70 
71 TEST_P(BlitPassTest, BlitPassesForMatchingFormats) {
72  ScopedValidationDisable scope; // avoid noise in output.
73  auto context = GetContext();
74  auto cmd_buffer = context->CreateCommandBuffer();
75  auto blit_pass = cmd_buffer->CreateBlitPass();
76 
77  TextureDescriptor src_desc;
79  src_desc.size = {100, 100};
81  auto src = context->GetResourceAllocator()->CreateTexture(src_desc);
82 
83  TextureDescriptor dst_format;
85  dst_format.size = {100, 100};
87  auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
88 
89  EXPECT_TRUE(blit_pass->AddCopy(src, dst));
90 }
91 
92 TEST_P(BlitPassTest, ChecksInvalidSliceParameters) {
93  ScopedValidationDisable scope; // avoid noise in output.
94  auto context = GetContext();
95  auto cmd_buffer = context->CreateCommandBuffer();
96  auto blit_pass = cmd_buffer->CreateBlitPass();
97 
98  TextureDescriptor dst_format;
101  dst_format.size = {100, 100};
102  auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
103 
104  DeviceBufferDescriptor src_format;
105  src_format.size = 40000;
107  auto src = context->GetResourceAllocator()->CreateBuffer(src_format);
108 
109  ASSERT_TRUE(dst);
110  ASSERT_TRUE(src);
111 
112  EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
113  std::nullopt, "", /*mip_level=*/0,
114  /*slice=*/25));
115  EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
116  std::nullopt, "", /*mip_level=*/0,
117  /*slice=*/6));
118  EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
119  std::nullopt, "", /*mip_level=*/0,
120  /*slice=*/0));
121 }
122 
123 TEST_P(BlitPassTest, CanBlitSmallRegionToUninitializedTexture) {
124  auto context = GetContext();
125  auto cmd_buffer = context->CreateCommandBuffer();
126  auto blit_pass = cmd_buffer->CreateBlitPass();
127 
128  TextureDescriptor dst_format;
131  dst_format.size = {1000, 1000};
132  auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
133 
134  DeviceBufferDescriptor src_format;
135  src_format.size = 4;
137  auto src = context->GetResourceAllocator()->CreateBuffer(src_format);
138 
139  ASSERT_TRUE(dst);
140 
141  EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
142  IRect::MakeLTRB(0, 0, 1, 1), "",
143  /*mip_level=*/0, /*slice=*/0));
144  EXPECT_TRUE(blit_pass->EncodeCommands());
145  EXPECT_TRUE(context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok());
146 }
147 
148 TEST_P(BlitPassTest, ChecksInvalidMipLevelParameter) {
150  auto context = GetContext();
151  auto cmd_buffer = context->CreateCommandBuffer();
152  auto blit_pass = cmd_buffer->CreateBlitPass();
153 
154  TextureDescriptor dst_format;
157  dst_format.size = {1000, 1000};
158  auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
159 
160  DeviceBufferDescriptor src_format;
161  src_format.size = 4;
163  auto src = context->GetResourceAllocator()->CreateBuffer(src_format);
164 
165  ASSERT_TRUE(dst);
166 
167  EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
168  IRect::MakeLTRB(0, 0, 1, 1), "",
169  /*mip_level=*/1, /*slice=*/0));
170 
171  EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
172  IRect::MakeLTRB(0, 0, 1, 1), "",
173  /*mip_level=*/0, /*slice=*/0));
174 }
175 
176 TEST_P(BlitPassTest, CanBlitToHigherTextureMipLevels) {
177  auto context = GetContext();
178  auto cmd_buffer = context->CreateCommandBuffer();
179  auto blit_pass = cmd_buffer->CreateBlitPass();
180 
181  TextureDescriptor dst_format;
184  dst_format.size = {1000, 1000};
185  dst_format.mip_count = 4;
186  auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
187 
188  DeviceBufferDescriptor src_format;
189  src_format.size = 4;
191  auto src = context->GetResourceAllocator()->CreateBuffer(src_format);
192 
193  ASSERT_TRUE(dst);
194 
195  EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
196  IRect::MakeLTRB(0, 0, 1, 1), "",
197  /*mip_level=*/1, /*slice=*/0));
198  EXPECT_TRUE(blit_pass->EncodeCommands());
199  EXPECT_TRUE(context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok());
200 }
201 
202 TEST_P(BlitPassTest, CanResizeTextures) {
203  auto context = GetContext();
204  auto cmd_buffer = context->CreateCommandBuffer();
205  auto blit_pass = cmd_buffer->CreateBlitPass();
206 
207  TextureDescriptor dst_format;
210  dst_format.size = {10, 10};
212  auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
213 
214  TextureDescriptor src_format;
217  src_format.size = {100, 100};
218  auto src = context->GetResourceAllocator()->CreateTexture(src_format);
219 
220  std::vector<uint8_t> bytes(src_format.GetByteSizeOfBaseMipLevel());
221  for (auto i = 0u; i < src_format.GetByteSizeOfBaseMipLevel(); i += 4) {
222  // RGBA
223  bytes[i + 0] = 255;
224  bytes[i + 1] = 0;
225  bytes[i + 2] = 0;
226  bytes[i + 3] = 255;
227  }
228  auto mapping = fml::DataMapping(bytes);
229  auto staging = context->GetResourceAllocator()->CreateBufferWithCopy(mapping);
230 
231  ASSERT_TRUE(dst);
232  ASSERT_TRUE(src);
233  ASSERT_TRUE(staging);
234 
235  EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(staging), src));
236  EXPECT_TRUE(blit_pass->ResizeTexture(src, dst));
237  EXPECT_TRUE(blit_pass->EncodeCommands());
238  EXPECT_TRUE(context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok());
239 }
240 
241 TEST_P(BlitPassTest, CanResizeTexturesPlayground) {
242  auto context = GetContext();
243  auto cmd_buffer = context->CreateCommandBuffer();
244  auto blit_pass = cmd_buffer->CreateBlitPass();
245 
246  std::shared_ptr<Texture> src = CreateTextureForFixture("kalimba.jpg");
247 
248  TextureDescriptor dst_format;
251  dst_format.size = {src->GetSize().width / 2, src->GetSize().height};
253  auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
254 
255  ASSERT_TRUE(dst);
256  ASSERT_TRUE(src);
257 
258  EXPECT_TRUE(blit_pass->ResizeTexture(src, dst));
259  EXPECT_TRUE(blit_pass->EncodeCommands());
260  EXPECT_TRUE(context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok());
261 
262  DisplayListBuilder builder;
263  builder.Scale(GetContentScale().x, GetContentScale().y);
264  DlPaint paint;
265  paint.setColor(DlColor::kRed());
266  auto image = DlImageImpeller::Make(dst);
267  builder.DrawImage(image, flutter::DlPoint(100.0, 100.0),
268  DlImageSampling::kNearestNeighbor, &paint);
269  ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
270 }
271 
272 } // namespace testing
273 } // namespace impeller
static BufferView AsBufferView(std::shared_ptr< DeviceBuffer > buffer)
Create a buffer view of this entire buffer.
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
int32_t x
AiksPlayground AiksTest
TEST_P(AiksTest, DrawAtlasNoColor)
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
flutter::DlPoint DlPoint
Definition: dl_dispatcher.h:24
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
Type width
Definition: size.h:28
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
constexpr size_t GetByteSizeOfBaseMipLevel() const