Flutter Engine
The Flutter Engine
texture_layer_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/flow/layers/texture_layer.h"
6
7#include "flutter/flow/testing/diff_context_test.h"
8#include "flutter/flow/testing/layer_test.h"
9#include "flutter/flow/testing/mock_layer.h"
10#include "flutter/flow/testing/mock_texture.h"
11#include "flutter/fml/macros.h"
12#include "flutter/testing/display_list_testing.h"
13
14namespace flutter {
15namespace testing {
16
18
19TEST_F(TextureLayerTest, InvalidTexture) {
20 const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f);
21 const SkSize layer_size = SkSize::Make(8.0f, 8.0f);
22 auto layer = std::make_shared<TextureLayer>(
23 layer_offset, layer_size, 0, false, DlImageSampling::kNearestNeighbor);
24
25 layer->Preroll(preroll_context());
26 EXPECT_EQ(layer->paint_bounds(),
27 (SkRect::MakeSize(layer_size)
28 .makeOffset(layer_offset.fX, layer_offset.fY)));
29 EXPECT_TRUE(layer->needs_painting(paint_context()));
30
31 layer->Paint(display_list_paint_context());
32 EXPECT_TRUE(display_list()->Equals(DisplayList()));
33}
34
35#ifndef NDEBUG
36TEST_F(TextureLayerTest, PaintingEmptyLayerDies) {
37 const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f);
38 const SkSize layer_size = SkSize::Make(0.0f, 0.0f);
39 const int64_t texture_id = 0;
40 auto mock_texture = std::make_shared<MockTexture>(texture_id);
41 auto layer =
42 std::make_shared<TextureLayer>(layer_offset, layer_size, texture_id,
44
45 // Ensure the texture is located by the Layer.
46 preroll_context()->texture_registry->RegisterTexture(mock_texture);
47
48 layer->Preroll(preroll_context());
49 EXPECT_EQ(layer->paint_bounds(), kEmptyRect);
50 EXPECT_FALSE(layer->needs_painting(paint_context()));
51
52 EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
53 "needs_painting\\(context\\)");
54}
55
56TEST_F(TextureLayerTest, PaintBeforePrerollDies) {
57 const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f);
58 const SkSize layer_size = SkSize::Make(8.0f, 8.0f);
59 const int64_t texture_id = 0;
60 auto mock_texture = std::make_shared<MockTexture>(texture_id);
61 auto layer = std::make_shared<TextureLayer>(
62 layer_offset, layer_size, texture_id, false, DlImageSampling::kLinear);
63
64 // Ensure the texture is located by the Layer.
65 preroll_context()->texture_registry->RegisterTexture(mock_texture);
66
67 EXPECT_DEATH_IF_SUPPORTED(layer->Paint(paint_context()),
68 "needs_painting\\(context\\)");
69}
70#endif
71
72TEST_F(TextureLayerTest, PaintingWithLinearSampling) {
73 const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f);
74 const SkSize layer_size = SkSize::Make(8.0f, 8.0f);
75 const SkRect layer_bounds =
76 SkRect::MakeSize(layer_size).makeOffset(layer_offset.fX, layer_offset.fY);
77 const int64_t texture_id = 0;
78 const auto texture_image = MockTexture::MakeTestTexture(20, 20, 5);
79 auto mock_texture = std::make_shared<MockTexture>(texture_id, texture_image);
80 auto layer = std::make_shared<TextureLayer>(
81 layer_offset, layer_size, texture_id, false, DlImageSampling::kLinear);
82
83 // Ensure the texture is located by the Layer.
84 preroll_context()->texture_registry->RegisterTexture(mock_texture);
85
86 layer->Preroll(preroll_context());
87 EXPECT_EQ(layer->paint_bounds(), layer_bounds);
88 EXPECT_TRUE(layer->needs_painting(paint_context()));
89
90 layer->Paint(display_list_paint_context());
91 DisplayListBuilder expected_builder;
92 /* (Texture)layer::Paint */ {
93 expected_builder.DrawImageRect(texture_image, layer_bounds,
95 }
96 EXPECT_TRUE(DisplayListsEQ_Verbose(display_list(), expected_builder.Build()));
97}
98
100
101TEST_F(TextureLayerDiffTest, TextureInRetainedLayer) {
102 MockLayerTree tree1;
103 auto container = std::make_shared<ContainerLayer>();
104 tree1.root()->Add(container);
105 auto layer = std::make_shared<TextureLayer>(SkPoint::Make(0, 0),
106 SkSize::Make(100, 100), 0, false,
108 container->Add(layer);
109
110 MockLayerTree tree2;
111 tree2.root()->Add(container); // retained layer
112
113 auto damage = DiffLayerTree(tree1, MockLayerTree());
114 EXPECT_EQ(damage.frame_damage, SkIRect::MakeLTRB(0, 0, 100, 100));
115
116 damage = DiffLayerTree(tree2, tree1);
117 EXPECT_EQ(damage.frame_damage, SkIRect::MakeLTRB(0, 0, 100, 100));
118}
119
120TEST_F(TextureLayerTest, OpacityInheritance) {
121 const SkPoint layer_offset = SkPoint::Make(0.0f, 0.0f);
122 const SkSize layer_size = SkSize::Make(8.0f, 8.0f);
123 const SkRect layer_bounds =
124 SkRect::MakeSize(layer_size).makeOffset(layer_offset.fX, layer_offset.fY);
125 const int64_t texture_id = 0;
126 const auto texture_image = MockTexture::MakeTestTexture(20, 20, 5);
127 auto mock_texture = std::make_shared<MockTexture>(texture_id, texture_image);
128 SkAlpha alpha = 0x7f;
129 auto texture_layer = std::make_shared<TextureLayer>(
130 layer_offset, layer_size, texture_id, false, DlImageSampling::kLinear);
131 auto layer = std::make_shared<OpacityLayer>(alpha, SkPoint::Make(0.0f, 0.0f));
132 layer->Add(texture_layer);
133
134 // Ensure the texture is located by the Layer.
135 PrerollContext* context = preroll_context();
136 context->texture_registry->RegisterTexture(mock_texture);
137
138 // The texture layer always reports opacity compatibility.
139 texture_layer->Preroll(context);
140 EXPECT_EQ(context->renderable_state_flags,
142
143 // Reset has_texture_layer since it is not supposed to be sent as we
144 // descend a tree in Preroll, but it was set by the previous test.
145 context->has_texture_layer = false;
146 layer->Preroll(context);
147 EXPECT_EQ(context->renderable_state_flags,
149
150 DlPaint texture_paint;
151 texture_paint.setAlpha(alpha);
152
153 layer->Paint(display_list_paint_context());
154 DisplayListBuilder expected_builder;
155 /* (Opacity)layer::Paint */ {
156 expected_builder.Save();
157 {
158 /* texture_layer::Paint */ {
159 expected_builder.DrawImageRect(texture_image, layer_bounds,
161 &texture_paint);
162 }
163 }
164 expected_builder.Restore();
165 }
166 EXPECT_TRUE(DisplayListsEQ_Verbose(display_list(), expected_builder.Build()));
167}
168
169} // namespace testing
170} // namespace flutter
uint8_t SkAlpha
Definition: SkColor.h:26
virtual void Add(std::shared_ptr< Layer > layer)
sk_sp< DisplayList > Build()
Definition: dl_builder.cc:67
void DrawImageRect(const sk_sp< DlImage > &image, const SkRect &src, const SkRect &dst, DlImageSampling sampling, const DlPaint *paint=nullptr, SrcRectConstraint constraint=SrcRectConstraint::kFast) override
Definition: dl_builder.cc:1396
DlPaint & setAlpha(uint8_t alpha)
Definition: dl_paint.h:76
static constexpr int kCallerCanApplyOpacity
static sk_sp< DlImage > MakeTestTexture(int w, int h, int checker_size)
Definition: mock_texture.cc:12
TEST_F(DisplayListTest, Defaults)
LayerTestBase<::testing::Test > LayerTest
Definition: layer_test.h:210
bool DisplayListsEQ_Verbose(const DisplayList *a, const DisplayList *b)
static constexpr SkRect kEmptyRect
Definition: mock_canvas.h:30
bool Equals(const T *a, const T *b)
Definition: dl_comparable.h:19
flutter::DisplayList DisplayList
static constexpr SkIRect MakeLTRB(int32_t l, int32_t t, int32_t r, int32_t b)
Definition: SkRect.h:91
float fX
x-axis value
Definition: SkPoint_impl.h:164
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
float fY
y-axis value
Definition: SkPoint_impl.h:165
constexpr SkRect makeOffset(float dx, float dy) const
Definition: SkRect.h:965
static constexpr SkRect MakeSize(const SkSize &size)
Definition: SkRect.h:633
Definition: SkSize.h:52
static constexpr SkSize Make(SkScalar w, SkScalar h)
Definition: SkSize.h:56
std::shared_ptr< TextureRegistry > texture_registry
Definition: layer.h:66
int renderable_state_flags
Definition: layer.h:83
int64_t texture_id
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678