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/trees/layer_tree_host_common.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/transform.h"
16 using ::testing::Mock
;
18 using ::testing::AtLeast
;
19 using ::testing::AnyNumber
;
24 class TestLayer
: public Layer
{
26 static scoped_refptr
<TestLayer
> Create() {
27 return make_scoped_refptr(new TestLayer());
30 int count_representing_target_surface_
;
31 int count_representing_contributing_surface_
;
32 int count_representing_itself_
;
34 virtual bool DrawsContent() const OVERRIDE
{ return draws_content_
; }
35 void set_draws_content(bool draws_content
) { draws_content_
= draws_content
; }
38 TestLayer() : Layer(), draws_content_(true) {
39 SetBounds(gfx::Size(100, 100));
40 SetPosition(gfx::Point());
42 virtual ~TestLayer() {}
47 #define EXPECT_COUNT(layer, target, contrib, itself) \
48 EXPECT_EQ(target, layer->count_representing_target_surface_); \
49 EXPECT_EQ(contrib, layer->count_representing_contributing_surface_); \
50 EXPECT_EQ(itself, layer->count_representing_itself_);
52 typedef LayerIterator
<Layer
> FrontToBack
;
54 void ResetCounts(RenderSurfaceLayerList
* render_surface_layer_list
) {
55 for (unsigned surface_index
= 0;
56 surface_index
< render_surface_layer_list
->size();
58 TestLayer
* render_surface_layer
= static_cast<TestLayer
*>(
59 render_surface_layer_list
->at(surface_index
));
60 RenderSurface
* render_surface
= render_surface_layer
->render_surface();
62 render_surface_layer
->count_representing_target_surface_
= -1;
63 render_surface_layer
->count_representing_contributing_surface_
= -1;
64 render_surface_layer
->count_representing_itself_
= -1;
66 for (unsigned layer_index
= 0;
67 layer_index
< render_surface
->layer_list().size();
69 TestLayer
* layer
= static_cast<TestLayer
*>(
70 render_surface
->layer_list().at(layer_index
).get());
72 layer
->count_representing_target_surface_
= -1;
73 layer
->count_representing_contributing_surface_
= -1;
74 layer
->count_representing_itself_
= -1;
79 void IterateFrontToBack(
80 RenderSurfaceLayerList
* render_surface_layer_list
) {
81 ResetCounts(render_surface_layer_list
);
83 for (FrontToBack it
= FrontToBack::Begin(render_surface_layer_list
);
84 it
!= FrontToBack::End(render_surface_layer_list
);
86 TestLayer
* layer
= static_cast<TestLayer
*>(*it
);
87 if (it
.represents_target_render_surface())
88 layer
->count_representing_target_surface_
= count
;
89 if (it
.represents_contributing_render_surface())
90 layer
->count_representing_contributing_surface_
= count
;
91 if (it
.represents_itself())
92 layer
->count_representing_itself_
= count
;
96 TEST(LayerIteratorTest
, EmptyTree
) {
97 RenderSurfaceLayerList render_surface_layer_list
;
99 IterateFrontToBack(&render_surface_layer_list
);
102 TEST(LayerIteratorTest
, SimpleTree
) {
103 scoped_refptr
<TestLayer
> root_layer
= TestLayer::Create();
104 scoped_refptr
<TestLayer
> first
= TestLayer::Create();
105 scoped_refptr
<TestLayer
> second
= TestLayer::Create();
106 scoped_refptr
<TestLayer
> third
= TestLayer::Create();
107 scoped_refptr
<TestLayer
> fourth
= TestLayer::Create();
109 root_layer
->AddChild(first
);
110 root_layer
->AddChild(second
);
111 root_layer
->AddChild(third
);
112 root_layer
->AddChild(fourth
);
114 scoped_ptr
<FakeLayerTreeHost
> host
= FakeLayerTreeHost::Create();
115 host
->SetRootLayer(root_layer
);
117 RenderSurfaceLayerList render_surface_layer_list
;
118 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting
inputs(
119 root_layer
.get(), root_layer
->bounds(), &render_surface_layer_list
);
120 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
122 IterateFrontToBack(&render_surface_layer_list
);
123 EXPECT_COUNT(root_layer
, 5, -1, 4);
124 EXPECT_COUNT(first
, -1, -1, 3);
125 EXPECT_COUNT(second
, -1, -1, 2);
126 EXPECT_COUNT(third
, -1, -1, 1);
127 EXPECT_COUNT(fourth
, -1, -1, 0);
130 TEST(LayerIteratorTest
, ComplexTree
) {
131 scoped_refptr
<TestLayer
> root_layer
= TestLayer::Create();
132 scoped_refptr
<TestLayer
> root1
= TestLayer::Create();
133 scoped_refptr
<TestLayer
> root2
= TestLayer::Create();
134 scoped_refptr
<TestLayer
> root3
= TestLayer::Create();
135 scoped_refptr
<TestLayer
> root21
= TestLayer::Create();
136 scoped_refptr
<TestLayer
> root22
= TestLayer::Create();
137 scoped_refptr
<TestLayer
> root23
= TestLayer::Create();
138 scoped_refptr
<TestLayer
> root221
= TestLayer::Create();
139 scoped_refptr
<TestLayer
> root231
= TestLayer::Create();
141 root_layer
->AddChild(root1
);
142 root_layer
->AddChild(root2
);
143 root_layer
->AddChild(root3
);
144 root2
->AddChild(root21
);
145 root2
->AddChild(root22
);
146 root2
->AddChild(root23
);
147 root22
->AddChild(root221
);
148 root23
->AddChild(root231
);
150 scoped_ptr
<FakeLayerTreeHost
> host
= FakeLayerTreeHost::Create();
151 host
->SetRootLayer(root_layer
);
153 RenderSurfaceLayerList render_surface_layer_list
;
154 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting
inputs(
155 root_layer
.get(), root_layer
->bounds(), &render_surface_layer_list
);
156 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
158 IterateFrontToBack(&render_surface_layer_list
);
159 EXPECT_COUNT(root_layer
, 9, -1, 8);
160 EXPECT_COUNT(root1
, -1, -1, 7);
161 EXPECT_COUNT(root2
, -1, -1, 6);
162 EXPECT_COUNT(root21
, -1, -1, 5);
163 EXPECT_COUNT(root22
, -1, -1, 4);
164 EXPECT_COUNT(root221
, -1, -1, 3);
165 EXPECT_COUNT(root23
, -1, -1, 2);
166 EXPECT_COUNT(root231
, -1, -1, 1);
167 EXPECT_COUNT(root3
, -1, -1, 0);
170 TEST(LayerIteratorTest
, ComplexTreeMultiSurface
) {
171 scoped_refptr
<TestLayer
> root_layer
= TestLayer::Create();
172 scoped_refptr
<TestLayer
> root1
= TestLayer::Create();
173 scoped_refptr
<TestLayer
> root2
= TestLayer::Create();
174 scoped_refptr
<TestLayer
> root3
= TestLayer::Create();
175 scoped_refptr
<TestLayer
> root21
= TestLayer::Create();
176 scoped_refptr
<TestLayer
> root22
= TestLayer::Create();
177 scoped_refptr
<TestLayer
> root23
= TestLayer::Create();
178 scoped_refptr
<TestLayer
> root221
= TestLayer::Create();
179 scoped_refptr
<TestLayer
> root231
= TestLayer::Create();
181 root_layer
->AddChild(root1
);
182 root_layer
->AddChild(root2
);
183 root_layer
->AddChild(root3
);
184 root2
->set_draws_content(false);
185 root2
->SetOpacity(0.5f
);
186 root2
->SetForceRenderSurface(true); // Force the layer to own a new surface.
187 root2
->AddChild(root21
);
188 root2
->AddChild(root22
);
189 root2
->AddChild(root23
);
190 root22
->SetOpacity(0.5f
);
191 root22
->AddChild(root221
);
192 root23
->SetOpacity(0.5f
);
193 root23
->AddChild(root231
);
195 scoped_ptr
<FakeLayerTreeHost
> host
= FakeLayerTreeHost::Create();
196 host
->SetRootLayer(root_layer
);
198 RenderSurfaceLayerList render_surface_layer_list
;
199 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting
inputs(
200 root_layer
.get(), root_layer
->bounds(), &render_surface_layer_list
);
201 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
203 IterateFrontToBack(&render_surface_layer_list
);
204 EXPECT_COUNT(root_layer
, 14, -1, 13);
205 EXPECT_COUNT(root1
, -1, -1, 12);
206 EXPECT_COUNT(root2
, 10, 11, -1);
207 EXPECT_COUNT(root21
, -1, -1, 9);
208 EXPECT_COUNT(root22
, 7, 8, 6);
209 EXPECT_COUNT(root221
, -1, -1, 5);
210 EXPECT_COUNT(root23
, 3, 4, 2);
211 EXPECT_COUNT(root231
, -1, -1, 1);
212 EXPECT_COUNT(root3
, -1, -1, 0);