1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
cc / layers / effect_tree_layer_list_iterator_unittest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/layers/effect_tree_layer_list_iterator.h"
#include <memory>
#include <vector>
#include "base/memory/ptr_util.h"
#include "cc/layers/layer.h"
#include "cc/test/fake_layer_tree_host.h"
#include "cc/test/layer_tree_impl_test_base.h"
#include "cc/test/test_task_graph_runner.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/transform.h"
namespace cc {
namespace {
class TestLayerImpl : public LayerImpl {
public:
static std::unique_ptr<TestLayerImpl> Create(LayerTreeImpl* tree, int id) {
return base::WrapUnique(new TestLayerImpl(tree, id));
}
~TestLayerImpl() override = default;
int count_;
private:
explicit TestLayerImpl(LayerTreeImpl* tree, int id)
: LayerImpl(tree, id), count_(-1) {
SetBounds(gfx::Size(100, 100));
SetDrawsContent(true);
}
};
#define EXPECT_COUNT(layer, target, contrib, itself) \
if (GetRenderSurface(layer)) { \
EXPECT_EQ(target, target_surface_count_[layer->effect_tree_index()]); \
EXPECT_EQ(contrib, \
contributing_surface_count_[layer->effect_tree_index()]); \
} \
EXPECT_EQ(itself, layer->count_);
class EffectTreeLayerListIteratorTest : public LayerTreeImplTestBase,
public testing::Test {
public:
void SetUp() override {
// This test suite needs the root layer to be TestLayerImpl.
LayerTreeImpl* active_tree = host_impl()->active_tree();
active_tree->DetachLayers();
active_tree->property_trees()->clear();
active_tree->SetRootLayerForTesting(TestLayerImpl::Create(active_tree, 1));
root_layer()->SetBounds(gfx::Size(1, 1));
SetupRootProperties(root_layer());
}
void IterateFrontToBack() {
ResetCounts();
int count = 0;
for (EffectTreeLayerListIterator it(host_impl()->active_tree());
it.state() != EffectTreeLayerListIterator::State::kEnd;
++it, ++count) {
switch (it.state()) {
case EffectTreeLayerListIterator::State::kLayer:
static_cast<TestLayerImpl*>(it.current_layer())->count_ = count;
break;
case EffectTreeLayerListIterator::State::kTargetSurface:
target_surface_count_[it.target_render_surface()->EffectTreeIndex()] =
count;
break;
case EffectTreeLayerListIterator::State::kContributingSurface:
contributing_surface_count_[it.current_render_surface()
->EffectTreeIndex()] = count;
break;
default:
NOTREACHED();
}
}
}
void ResetCounts() {
for (LayerImpl* layer : *host_impl()->active_tree()) {
static_cast<TestLayerImpl*>(layer)->count_ = -1;
}
target_surface_count_ = std::vector<int>(
host_impl()->active_tree()->property_trees()->effect_tree().size(), -1);
contributing_surface_count_ = std::vector<int>(
host_impl()->active_tree()->property_trees()->effect_tree().size(), -1);
}
protected:
// Tracks when each render surface is visited as a target surface or
// contributing surface. Indexed by effect node id.
std::vector<int> target_surface_count_;
std::vector<int> contributing_surface_count_;
};
TEST_F(EffectTreeLayerListIteratorTest, TreeWithNoDrawnLayers) {
auto* root = static_cast<TestLayerImpl*>(root_layer());
root->SetDrawsContent(false);
UpdateActiveTreeDrawProperties();
IterateFrontToBack();
EXPECT_COUNT(root, 0, -1, -1);
}
TEST_F(EffectTreeLayerListIteratorTest, SimpleTree) {
auto* root = static_cast<TestLayerImpl*>(root_layer());
auto* first = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root, first);
auto* second = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root, second);
auto* third = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root, third);
auto* fourth = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root, fourth);
UpdateActiveTreeDrawProperties();
IterateFrontToBack();
EXPECT_COUNT(root, 5, -1, 4);
EXPECT_COUNT(first, 5, -1, 3);
EXPECT_COUNT(second, 5, -1, 2);
EXPECT_COUNT(third, 5, -1, 1);
EXPECT_COUNT(fourth, 5, -1, 0);
}
TEST_F(EffectTreeLayerListIteratorTest, ComplexTreeMultiSurface) {
auto* root = static_cast<TestLayerImpl*>(root_layer());
auto* root1 = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root, root1);
auto* root2 = AddLayerInActiveTree<TestLayerImpl>();
root2->SetDrawsContent(false);
CopyProperties(root, root2);
CreateEffectNode(root2).render_surface_reason = RenderSurfaceReason::kTest;
auto* root21 = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root2, root21);
auto* root22 = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root2, root22);
CreateEffectNode(root22).render_surface_reason = RenderSurfaceReason::kTest;
auto* root221 = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root22, root221);
auto* root23 = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root2, root23);
CreateEffectNode(root23).render_surface_reason = RenderSurfaceReason::kTest;
auto* root231 = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root23, root231);
auto* root3 = AddLayerInActiveTree<TestLayerImpl>();
CopyProperties(root, root3);
UpdateActiveTreeDrawProperties();
IterateFrontToBack();
EXPECT_COUNT(root, 14, -1, 13);
EXPECT_COUNT(root1, 14, -1, 12);
EXPECT_COUNT(root2, 10, 11, -1);
EXPECT_COUNT(root21, 10, 11, 9);
EXPECT_COUNT(root22, 7, 8, 6);
EXPECT_COUNT(root221, 7, 8, 5);
EXPECT_COUNT(root23, 3, 4, 2);
EXPECT_COUNT(root231, 3, 4, 1);
EXPECT_COUNT(root3, 14, -1, 0);
}
} // namespace
} // namespace cc