1 // Copyright 2012 The Chromium 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.
5 #include "cc/layers/layer_iterator.h"
9 #include "cc/layers/layer.h"
10 #include "cc/test/fake_layer_tree_host.h"
11 #include "cc/test/test_task_graph_runner.h"
12 #include "cc/trees/layer_tree_host_common.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gfx/transform.h"
17 using ::testing::Mock
;
19 using ::testing::AtLeast
;
20 using ::testing::AnyNumber
;
25 class TestLayerImpl
: public LayerImpl
{
27 static scoped_ptr
<TestLayerImpl
> Create(LayerTreeImpl
* tree
, int id
) {
28 return make_scoped_ptr(new TestLayerImpl(tree
, id
));
30 ~TestLayerImpl() override
{}
32 int count_representing_target_surface_
;
33 int count_representing_contributing_surface_
;
34 int count_representing_itself_
;
37 explicit TestLayerImpl(LayerTreeImpl
* tree
, int id
)
38 : LayerImpl(tree
, id
, new SyncedScrollOffset
),
39 count_representing_target_surface_(-1),
40 count_representing_contributing_surface_(-1),
41 count_representing_itself_(-1) {
42 SetBounds(gfx::Size(100, 100));
43 SetPosition(gfx::Point());
44 SetDrawsContent(true);
48 #define EXPECT_COUNT(layer, target, contrib, itself) \
49 EXPECT_EQ(target, layer->count_representing_target_surface_); \
50 EXPECT_EQ(contrib, layer->count_representing_contributing_surface_); \
51 EXPECT_EQ(itself, layer->count_representing_itself_);
53 void ResetCounts(LayerImplList
* render_surface_layer_list
) {
54 for (unsigned surface_index
= 0;
55 surface_index
< render_surface_layer_list
->size();
57 TestLayerImpl
* render_surface_layer
= static_cast<TestLayerImpl
*>(
58 render_surface_layer_list
->at(surface_index
));
59 RenderSurfaceImpl
* render_surface
= render_surface_layer
->render_surface();
61 render_surface_layer
->count_representing_target_surface_
= -1;
62 render_surface_layer
->count_representing_contributing_surface_
= -1;
63 render_surface_layer
->count_representing_itself_
= -1;
65 for (unsigned layer_index
= 0;
66 layer_index
< render_surface
->layer_list().size();
68 TestLayerImpl
* layer
= static_cast<TestLayerImpl
*>(
69 render_surface
->layer_list()[layer_index
]);
71 layer
->count_representing_target_surface_
= -1;
72 layer
->count_representing_contributing_surface_
= -1;
73 layer
->count_representing_itself_
= -1;
78 void IterateFrontToBack(LayerImplList
* render_surface_layer_list
) {
79 ResetCounts(render_surface_layer_list
);
81 for (LayerIterator it
= LayerIterator::Begin(render_surface_layer_list
);
82 it
!= LayerIterator::End(render_surface_layer_list
); ++it
, ++count
) {
83 TestLayerImpl
* layer
= static_cast<TestLayerImpl
*>(*it
);
84 if (it
.represents_target_render_surface())
85 layer
->count_representing_target_surface_
= count
;
86 if (it
.represents_contributing_render_surface())
87 layer
->count_representing_contributing_surface_
= count
;
88 if (it
.represents_itself())
89 layer
->count_representing_itself_
= count
;
93 class LayerIteratorTest
: public testing::Test
{
96 : host_impl_(&proxy_
, &shared_bitmap_manager_
, &task_graph_runner_
),
99 scoped_ptr
<TestLayerImpl
> CreateLayer() {
100 return TestLayerImpl::Create(host_impl_
.active_tree(), id_
++);
104 FakeImplProxy proxy_
;
105 TestSharedBitmapManager shared_bitmap_manager_
;
106 TestTaskGraphRunner task_graph_runner_
;
107 FakeLayerTreeHostImpl host_impl_
;
112 TEST_F(LayerIteratorTest
, EmptyTree
) {
113 LayerImplList render_surface_layer_list
;
115 IterateFrontToBack(&render_surface_layer_list
);
118 TEST_F(LayerIteratorTest
, SimpleTree
) {
119 scoped_ptr
<TestLayerImpl
> root_layer
= CreateLayer();
120 scoped_ptr
<TestLayerImpl
> first
= CreateLayer();
121 scoped_ptr
<TestLayerImpl
> second
= CreateLayer();
122 scoped_ptr
<TestLayerImpl
> third
= CreateLayer();
123 scoped_ptr
<TestLayerImpl
> fourth
= CreateLayer();
125 TestLayerImpl
* root_ptr
= root_layer
.get();
126 TestLayerImpl
* first_ptr
= first
.get();
127 TestLayerImpl
* second_ptr
= second
.get();
128 TestLayerImpl
* third_ptr
= third
.get();
129 TestLayerImpl
* fourth_ptr
= fourth
.get();
131 root_layer
->AddChild(first
.Pass());
132 root_layer
->AddChild(second
.Pass());
133 root_layer
->AddChild(third
.Pass());
134 root_layer
->AddChild(fourth
.Pass());
136 root_layer
->SetHasRenderSurface(true);
137 host_impl_
.active_tree()->SetRootLayer(root_layer
.Pass());
139 LayerImplList render_surface_layer_list
;
140 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting
inputs(
141 root_ptr
, root_ptr
->bounds(), &render_surface_layer_list
);
142 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
144 IterateFrontToBack(&render_surface_layer_list
);
145 EXPECT_COUNT(root_ptr
, 5, -1, 4);
146 EXPECT_COUNT(first_ptr
, -1, -1, 3);
147 EXPECT_COUNT(second_ptr
, -1, -1, 2);
148 EXPECT_COUNT(third_ptr
, -1, -1, 1);
149 EXPECT_COUNT(fourth_ptr
, -1, -1, 0);
152 TEST_F(LayerIteratorTest
, ComplexTree
) {
153 scoped_ptr
<TestLayerImpl
> root_layer
= CreateLayer();
154 scoped_ptr
<TestLayerImpl
> root1
= CreateLayer();
155 scoped_ptr
<TestLayerImpl
> root2
= CreateLayer();
156 scoped_ptr
<TestLayerImpl
> root3
= CreateLayer();
157 scoped_ptr
<TestLayerImpl
> root21
= CreateLayer();
158 scoped_ptr
<TestLayerImpl
> root22
= CreateLayer();
159 scoped_ptr
<TestLayerImpl
> root23
= CreateLayer();
160 scoped_ptr
<TestLayerImpl
> root221
= CreateLayer();
161 scoped_ptr
<TestLayerImpl
> root231
= CreateLayer();
163 TestLayerImpl
* root_ptr
= root_layer
.get();
164 TestLayerImpl
* root1_ptr
= root1
.get();
165 TestLayerImpl
* root2_ptr
= root2
.get();
166 TestLayerImpl
* root3_ptr
= root3
.get();
167 TestLayerImpl
* root21_ptr
= root21
.get();
168 TestLayerImpl
* root22_ptr
= root22
.get();
169 TestLayerImpl
* root23_ptr
= root23
.get();
170 TestLayerImpl
* root221_ptr
= root221
.get();
171 TestLayerImpl
* root231_ptr
= root231
.get();
173 root22
->AddChild(root221
.Pass());
174 root23
->AddChild(root231
.Pass());
175 root2
->AddChild(root21
.Pass());
176 root2
->AddChild(root22
.Pass());
177 root2
->AddChild(root23
.Pass());
178 root_layer
->AddChild(root1
.Pass());
179 root_layer
->AddChild(root2
.Pass());
180 root_layer
->AddChild(root3
.Pass());
182 root_layer
->SetHasRenderSurface(true);
183 host_impl_
.active_tree()->SetRootLayer(root_layer
.Pass());
185 LayerImplList render_surface_layer_list
;
186 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting
inputs(
187 root_ptr
, root_ptr
->bounds(), &render_surface_layer_list
);
188 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
190 IterateFrontToBack(&render_surface_layer_list
);
191 EXPECT_COUNT(root_ptr
, 9, -1, 8);
192 EXPECT_COUNT(root1_ptr
, -1, -1, 7);
193 EXPECT_COUNT(root2_ptr
, -1, -1, 6);
194 EXPECT_COUNT(root21_ptr
, -1, -1, 5);
195 EXPECT_COUNT(root22_ptr
, -1, -1, 4);
196 EXPECT_COUNT(root221_ptr
, -1, -1, 3);
197 EXPECT_COUNT(root23_ptr
, -1, -1, 2);
198 EXPECT_COUNT(root231_ptr
, -1, -1, 1);
199 EXPECT_COUNT(root3_ptr
, -1, -1, 0);
202 TEST_F(LayerIteratorTest
, ComplexTreeMultiSurface
) {
203 scoped_ptr
<TestLayerImpl
> root_layer
= CreateLayer();
204 scoped_ptr
<TestLayerImpl
> root1
= CreateLayer();
205 scoped_ptr
<TestLayerImpl
> root2
= CreateLayer();
206 scoped_ptr
<TestLayerImpl
> root3
= CreateLayer();
207 scoped_ptr
<TestLayerImpl
> root21
= CreateLayer();
208 scoped_ptr
<TestLayerImpl
> root22
= CreateLayer();
209 scoped_ptr
<TestLayerImpl
> root23
= CreateLayer();
210 scoped_ptr
<TestLayerImpl
> root221
= CreateLayer();
211 scoped_ptr
<TestLayerImpl
> root231
= CreateLayer();
213 TestLayerImpl
* root_ptr
= root_layer
.get();
214 TestLayerImpl
* root1_ptr
= root1
.get();
215 TestLayerImpl
* root2_ptr
= root2
.get();
216 TestLayerImpl
* root3_ptr
= root3
.get();
217 TestLayerImpl
* root21_ptr
= root21
.get();
218 TestLayerImpl
* root22_ptr
= root22
.get();
219 TestLayerImpl
* root23_ptr
= root23
.get();
220 TestLayerImpl
* root221_ptr
= root221
.get();
221 TestLayerImpl
* root231_ptr
= root231
.get();
223 root22
->SetHasRenderSurface(true);
224 root22
->AddChild(root221
.Pass());
225 root23
->SetHasRenderSurface(true);
226 root23
->AddChild(root231
.Pass());
227 root2
->SetDrawsContent(false);
228 root2
->SetHasRenderSurface(true);
229 root2
->AddChild(root21
.Pass());
230 root2
->AddChild(root22
.Pass());
231 root2
->AddChild(root23
.Pass());
232 root_layer
->AddChild(root1
.Pass());
233 root_layer
->AddChild(root2
.Pass());
234 root_layer
->AddChild(root3
.Pass());
236 root_layer
->SetHasRenderSurface(true);
237 host_impl_
.active_tree()->SetRootLayer(root_layer
.Pass());
239 LayerImplList render_surface_layer_list
;
240 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting
inputs(
241 root_ptr
, root_ptr
->bounds(), &render_surface_layer_list
);
242 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
244 IterateFrontToBack(&render_surface_layer_list
);
245 EXPECT_COUNT(root_ptr
, 14, -1, 13);
246 EXPECT_COUNT(root1_ptr
, -1, -1, 12);
247 EXPECT_COUNT(root2_ptr
, 10, 11, -1);
248 EXPECT_COUNT(root21_ptr
, -1, -1, 9);
249 EXPECT_COUNT(root22_ptr
, 7, 8, 6);
250 EXPECT_COUNT(root221_ptr
, -1, -1, 5);
251 EXPECT_COUNT(root23_ptr
, 3, 4, 2);
252 EXPECT_COUNT(root231_ptr
, -1, -1, 1);
253 EXPECT_COUNT(root3_ptr
, -1, -1, 0);