| 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 "fml/mapping.h"
7 #include "gtest/gtest.h"
8 #include "impeller/base/validation.h"
9 #include "impeller/core/device_buffer.h"
10 #include "impeller/core/device_buffer_descriptor.h"
11 #include "impeller/core/formats.h"
12 #include "impeller/core/texture_descriptor.h"
13 #include "impeller/playground/playground_test.h"
14 #include "impeller/renderer/command_buffer.h"
15
16 namespace impeller {
17 namespace testing {
18
19 using BlitPassTest = PlaygroundTest;
20 INSTANTIATE_PLAYGROUND_SUITE(BlitPassTest);
21
22 TEST_P(BlitPassTest, BlitAcrossDifferentPixelFormatsFails) {
23 ScopedValidationDisable scope; // avoid noise in output.
24 auto context = GetContext();
25 auto cmd_buffer = context->CreateCommandBuffer();
26 auto blit_pass = cmd_buffer->CreateBlitPass();
27
28 TextureDescriptor src_desc;
29 src_desc.format = PixelFormat::kA8UNormInt;
30 src_desc.size = {100, 100};
31 src_desc.storage_mode = StorageMode::kHostVisible;
32 auto src = context->GetResourceAllocator()->CreateTexture(src_desc);
33
34 TextureDescriptor dst_format;
35 dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
36 dst_format.size = {100, 100};
37 dst_format.storage_mode = StorageMode::kHostVisible;
38 auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
39
40 EXPECT_FALSE(blit_pass->AddCopy(src, dst));
41 }
42
43 TEST_P(BlitPassTest, BlitAcrossDifferentSampleCountsFails) {
44 ScopedValidationDisable scope; // avoid noise in output.
45 auto context = GetContext();
46 auto cmd_buffer = context->CreateCommandBuffer();
47 auto blit_pass = cmd_buffer->CreateBlitPass();
48
49 TextureDescriptor src_desc;
50 src_desc.format = PixelFormat::kR8G8B8A8UNormInt;
51 src_desc.sample_count = SampleCount::kCount4;
52 src_desc.size = {100, 100};
53 auto src = context->GetResourceAllocator()->CreateTexture(src_desc);
54
55 TextureDescriptor dst_format;
56 dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
57 dst_format.size = {100, 100};
58 auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
59
60 EXPECT_FALSE(blit_pass->AddCopy(src, dst));
61 }
62
63 TEST_P(BlitPassTest, BlitPassesForMatchingFormats) {
64 ScopedValidationDisable scope; // avoid noise in output.
65 auto context = GetContext();
66 auto cmd_buffer = context->CreateCommandBuffer();
67 auto blit_pass = cmd_buffer->CreateBlitPass();
68
69 TextureDescriptor src_desc;
70 src_desc.format = PixelFormat::kR8G8B8A8UNormInt;
71 src_desc.size = {100, 100};
72 src_desc.storage_mode = StorageMode::kHostVisible;
73 auto src = context->GetResourceAllocator()->CreateTexture(src_desc);
74
75 TextureDescriptor dst_format;
76 dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
77 dst_format.size = {100, 100};
78 dst_format.storage_mode = StorageMode::kHostVisible;
79 auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
80
81 EXPECT_TRUE(blit_pass->AddCopy(src, dst));
82 }
83
84 TEST_P(BlitPassTest, ChecksInvalidSliceParameters) {
85 ScopedValidationDisable scope; // avoid noise in output.
86 auto context = GetContext();
87 auto cmd_buffer = context->CreateCommandBuffer();
88 auto blit_pass = cmd_buffer->CreateBlitPass();
89
90 TextureDescriptor dst_format;
91 dst_format.storage_mode = StorageMode::kDevicePrivate;
92 dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
93 dst_format.size = {100, 100};
94 auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
95
96 DeviceBufferDescriptor src_format;
97 src_format.size = 40000;
98 src_format.storage_mode = StorageMode::kHostVisible;
99 auto src = context->GetResourceAllocator()->CreateBuffer(src_format);
100
101 ASSERT_TRUE(dst);
102 ASSERT_TRUE(src);
103
104 EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
105 std::nullopt, "", /*slice=*/25));
106 EXPECT_FALSE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
107 std::nullopt, "", /*slice=*/6));
108 EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
109 std::nullopt, "", /*slice=*/0));
110 }
111
112 TEST_P(BlitPassTest, CanBlitSmallRegionToUninitializedTexture) {
113 auto context = GetContext();
114 auto cmd_buffer = context->CreateCommandBuffer();
115 auto blit_pass = cmd_buffer->CreateBlitPass();
116
117 TextureDescriptor dst_format;
118 dst_format.storage_mode = StorageMode::kDevicePrivate;
119 dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
120 dst_format.size = {1000, 1000};
121 auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
122
123 DeviceBufferDescriptor src_format;
124 src_format.size = 4;
125 src_format.storage_mode = StorageMode::kHostVisible;
126 auto src = context->GetResourceAllocator()->CreateBuffer(src_format);
127
128 ASSERT_TRUE(dst);
129
130 EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(src), dst,
131 IRect::MakeLTRB(0, 0, 1, 1), "", /*slice=*/0));
132 EXPECT_TRUE(blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()));
133 EXPECT_TRUE(context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok());
134 }
135
136 TEST_P(BlitPassTest, CanResizeTextures) {
137 auto context = GetContext();
138 auto cmd_buffer = context->CreateCommandBuffer();
139 auto blit_pass = cmd_buffer->CreateBlitPass();
140
141 TextureDescriptor dst_format;
142 dst_format.storage_mode = StorageMode::kDevicePrivate;
143 dst_format.format = PixelFormat::kR8G8B8A8UNormInt;
144 dst_format.size = {10, 10};
145 dst_format.usage = TextureUsage::kShaderRead | TextureUsage::kShaderWrite;
146 auto dst = context->GetResourceAllocator()->CreateTexture(dst_format);
147
148 TextureDescriptor src_format;
149 src_format.storage_mode = StorageMode::kDevicePrivate;
150 src_format.format = PixelFormat::kR8G8B8A8UNormInt;
151 src_format.size = {100, 100};
152 auto src = context->GetResourceAllocator()->CreateTexture(src_format);
153
154 std::vector<uint8_t> bytes(src_format.GetByteSizeOfBaseMipLevel());
155 for (auto i = 0u; i < src_format.GetByteSizeOfBaseMipLevel(); i += 4) {
156 // RGBA
157 bytes[i + 0] = 255;
158 bytes[i + 1] = 0;
159 bytes[i + 2] = 0;
160 bytes[i + 3] = 255;
161 }
162 auto mapping = fml::DataMapping(bytes);
163 auto staging = context->GetResourceAllocator()->CreateBufferWithCopy(mapping);
164
165 ASSERT_TRUE(dst);
166 ASSERT_TRUE(src);
167 ASSERT_TRUE(staging);
168
169 EXPECT_TRUE(blit_pass->AddCopy(DeviceBuffer::AsBufferView(staging), src));
170 EXPECT_TRUE(blit_pass->ResizeTexture(src, dst));
171 EXPECT_TRUE(blit_pass->EncodeCommands(GetContext()->GetResourceAllocator()));
172 EXPECT_TRUE(context->GetCommandQueue()->Submit({std::move(cmd_buffer)}).ok());
173 }
174
175 } // namespace testing
176 } // namespace impeller
impeller::DeviceBuffer::AsBufferView
static BufferView AsBufferView(std::shared_ptr< DeviceBuffer > buffer)
Create a buffer view of this entire buffer.
Definition: device_buffer.cc:18
impeller::TextureUsage::kShaderWrite
@ kShaderWrite
impeller::PixelFormat::kA8UNormInt
@ kA8UNormInt
playground_test.h
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:14
device_buffer.h
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:41
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
texture_descriptor.h
formats.h
impeller::DeviceBufferDescriptor::size
size_t size
Definition: device_buffer_descriptor.h:16
impeller::StorageMode::kHostVisible
@ kHostVisible
impeller::TextureDescriptor::sample_count
SampleCount sample_count
Definition: texture_descriptor.h:45
validation.h
impeller::TextureDescriptor::usage
TextureUsageMask usage
Definition: texture_descriptor.h:44
impeller::testing::INSTANTIATE_PLAYGROUND_SUITE
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::DeviceBufferDescriptor::storage_mode
StorageMode storage_mode
Definition: device_buffer_descriptor.h:15
impeller::testing::TEST_P
TEST_P(AiksTest, DrawAtlasNoColor)
Definition: aiks_dl_atlas_unittests.cc:78
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:42
command_buffer.h
impeller::TextureDescriptor::GetByteSizeOfBaseMipLevel
constexpr size_t GetByteSizeOfBaseMipLevel() const
Definition: texture_descriptor.h:48
impeller::TextureUsage::kShaderRead
@ kShaderRead
impeller::SampleCount::kCount4
@ kCount4
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:39
impeller::TRect::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
impeller::PlaygroundTest
Definition: playground_test.h:21
device_buffer_descriptor.h
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
impeller::ScopedValidationDisable
Definition: validation.h:56
impeller
Definition: allocation.cc:12