Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2fd78cc

Browse files
committed
Unit tests for Layer readback flags.
1 parent eff7218 commit 2fd78cc

File tree

3 files changed

+178
-1
lines changed

3 files changed

+178
-1
lines changed

flow/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ executable("flow_unittests") {
109109
"flow_run_all_unittests.cc",
110110
"flow_test_utils.cc",
111111
"flow_test_utils.h",
112+
"layers/layer_unittests.cc",
112113
"layers/performance_overlay_layer_unittests.cc",
113114
"layers/physical_shape_layer_unittests.cc",
114115
"matrix_decomposition_unittests.cc",

flow/layers/layer_unittests.cc

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// Copyright 2019 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/container_layer.h"
6+
#include "flutter/flow/layers/clip_rect_layer.h"
7+
#include "flutter/flow/layers/clip_rrect_layer.h"
8+
#include "flutter/flow/layers/clip_path_layer.h"
9+
#include "flutter/flow/layers/color_filter_layer.h"
10+
#include "flutter/flow/layers/opacity_layer.h"
11+
#include "flutter/flow/layers/physical_shape_layer.h"
12+
#include "flutter/flow/layers/shader_mask_layer.h"
13+
14+
#include "gtest/gtest.h"
15+
16+
namespace flutter {
17+
18+
class ReadbackLayer : public ContainerLayer {
19+
public:
20+
ReadbackLayer(const bool reads, const bool saves) {
21+
set_layer_reads_surface(reads);
22+
set_renders_to_save_layer(saves);
23+
}
24+
~ReadbackLayer() override = default;
25+
26+
static std::shared_ptr<ReadbackLayer> Make(const bool reads,
27+
const bool saves) {
28+
return std::make_shared<ReadbackLayer>(reads, saves);
29+
}
30+
31+
void set_read(const bool reads) { set_layer_reads_surface(reads); }
32+
33+
void Paint(PaintContext& context) const override {}
34+
};
35+
36+
void TestLayerFlag(bool reads, bool saves) {
37+
EXPECT_EQ(ReadbackLayer(reads, saves).tree_reads_surface(), reads);
38+
}
39+
40+
void TestChildFlag(bool child_reads, bool uses_save_layer, bool ret) {
41+
ReadbackLayer parent = ReadbackLayer(false, uses_save_layer);
42+
parent.Add(ReadbackLayer::Make(child_reads, false));
43+
EXPECT_EQ(parent.tree_reads_surface(), ret);
44+
}
45+
46+
TEST(Layer, ReadbackFalse) {
47+
TestLayerFlag(false, false);
48+
TestLayerFlag(false, true);
49+
}
50+
51+
TEST(Layer, ReadbackTrue) {
52+
TestLayerFlag(true, false);
53+
TestLayerFlag(true, true);
54+
}
55+
56+
TEST(Layer, NoReadbackNoSaveLayer) {
57+
TestChildFlag(false, false, false);
58+
}
59+
60+
TEST(Layer, NoReadbackButSaveLayer) {
61+
TestChildFlag(false, true, false);
62+
}
63+
64+
TEST(Layer, ReadbackNoSaveLayer) {
65+
TestChildFlag(true, false, true);
66+
}
67+
68+
TEST(Layer, ReadbackButSaveLayer) {
69+
TestChildFlag(true, true, false);
70+
}
71+
72+
TEST(Layer, ChildChangesReadback) {
73+
ReadbackLayer parent = ReadbackLayer(false, false);
74+
EXPECT_FALSE(parent.tree_reads_surface());
75+
parent.Add(ReadbackLayer::Make(false, false));
76+
EXPECT_FALSE(parent.tree_reads_surface());
77+
std::shared_ptr<ReadbackLayer> child_reads = ReadbackLayer::Make(true, false);
78+
parent.Add(child_reads);
79+
EXPECT_TRUE(parent.tree_reads_surface());
80+
child_reads->set_read(false);
81+
EXPECT_FALSE(parent.tree_reads_surface());
82+
child_reads->set_read(true);
83+
EXPECT_TRUE(parent.tree_reads_surface());
84+
}
85+
86+
void TestClipRect(Clip clip_behavior, bool ret) {
87+
ClipRectLayer layer = ClipRectLayer(SkRect::MakeWH(5, 5), clip_behavior);
88+
layer.Add(ReadbackLayer::Make(true, false));
89+
EXPECT_EQ(layer.tree_reads_surface(), ret);
90+
}
91+
92+
TEST(Layer, ClipRectSaveLayer) {
93+
// TestClipRect(Clip::none, true); // ClipRectLayer asserts !Clip::none
94+
TestClipRect(Clip::hardEdge, true);
95+
TestClipRect(Clip::antiAlias, true);
96+
TestClipRect(Clip::antiAliasWithSaveLayer, false);
97+
}
98+
99+
void TestClipRRect(Clip clip_behavior, bool ret) {
100+
SkRRect r_rect = SkRRect::MakeRect(SkRect::MakeWH(5, 5));
101+
ClipRRectLayer layer = ClipRRectLayer(r_rect, clip_behavior);
102+
layer.Add(ReadbackLayer::Make(true, false));
103+
EXPECT_EQ(layer.tree_reads_surface(), ret);
104+
}
105+
106+
TEST(Layer, ClipRRectSaveLayer) {
107+
// TestClipRRect(Clip::none, true); // ClipRRectLayer asserts !Clip::none
108+
TestClipRRect(Clip::hardEdge, true);
109+
TestClipRRect(Clip::antiAlias, true);
110+
TestClipRRect(Clip::antiAliasWithSaveLayer, false);
111+
}
112+
113+
void TestClipPath(Clip clip_behavior, bool ret) {
114+
SkPath path = SkPath();
115+
path.moveTo(0, 0);
116+
path.lineTo(5, 0);
117+
path.lineTo(0, 5);
118+
path.close();
119+
ClipPathLayer layer = ClipPathLayer(path, clip_behavior);
120+
layer.Add(ReadbackLayer::Make(true, false));
121+
EXPECT_EQ(layer.tree_reads_surface(), ret);
122+
}
123+
124+
TEST(Layer, ClipPathSaveLayer) {
125+
// TestClipPath(Clip::none, true); // ClipRRectLayer asserts !Clip::none
126+
TestClipPath(Clip::hardEdge, true);
127+
TestClipPath(Clip::antiAlias, true);
128+
TestClipPath(Clip::antiAliasWithSaveLayer, false);
129+
}
130+
131+
TEST(Layer, ColorFilterSaveLayer) {
132+
sk_sp<SkColorFilter> filter = SkColorFilters::LinearToSRGBGamma();
133+
ColorFilterLayer layer = ColorFilterLayer(filter);
134+
layer.Add(ReadbackLayer::Make(true, false));
135+
EXPECT_FALSE(layer.tree_reads_surface());
136+
}
137+
138+
TEST(Layer, OpacitySaveLayer) {
139+
OpacityLayer layer = OpacityLayer(10, SkPoint::Make(0, 0));
140+
layer.Add(ReadbackLayer::Make(true, false));
141+
EXPECT_FALSE(layer.tree_reads_surface());
142+
}
143+
144+
void TestPhysicalShapeLayer(Clip clip_behavior, bool ret) {
145+
SkPath path = SkPath();
146+
path.moveTo(0, 0);
147+
path.lineTo(5, 0);
148+
path.lineTo(0, 5);
149+
path.close();
150+
PhysicalShapeLayer layer = PhysicalShapeLayer(SK_ColorRED, SK_ColorBLUE,
151+
1.0f, 100.0f, 10.0f,
152+
path, clip_behavior);
153+
layer.Add(ReadbackLayer::Make(true, false));
154+
EXPECT_EQ(layer.tree_reads_surface(), ret);
155+
}
156+
157+
TEST(Layer, PhysicalShapeSaveLayer) {
158+
TestPhysicalShapeLayer(Clip::none, true);
159+
TestPhysicalShapeLayer(Clip::hardEdge, true);
160+
TestPhysicalShapeLayer(Clip::antiAlias, true);
161+
TestPhysicalShapeLayer(Clip::antiAliasWithSaveLayer, false);
162+
}
163+
164+
TEST(Layer, ShaderMaskSaveLayer) {
165+
ShaderMaskLayer layer = ShaderMaskLayer(SkShaders::Empty(),
166+
SkRect::MakeWH(5, 5),
167+
SkBlendMode::kSrcOver);
168+
layer.Add(ReadbackLayer::Make(true, false));
169+
EXPECT_FALSE(layer.tree_reads_surface());
170+
}
171+
172+
}

testing/run_tests.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
fml_unittests_filter = '--gtest_filter=-*TimeSensitiveTest*:*GpuThreadMerger*'
2525

2626
def RunCmd(cmd, **kwargs):
27-
print(subprocess.check_output(cmd, **kwargs))
27+
try:
28+
print(subprocess.check_output(cmd, **kwargs))
29+
except subprocess.CalledProcessError as cpe:
30+
print(cpe.output)
31+
raise cpe
2832

2933
def IsMac():
3034
return sys.platform == 'darwin'

0 commit comments

Comments
 (0)