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());
41 SetAnchorPoint(gfx::Point());
43 virtual ~TestLayer() {}
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 typedef LayerIterator
<Layer
> FrontToBack
;
55 void ResetCounts(RenderSurfaceLayerList
* render_surface_layer_list
) {
56 for (unsigned surface_index
= 0;
57 surface_index
< render_surface_layer_list
->size();
59 TestLayer
* render_surface_layer
= static_cast<TestLayer
*>(
60 render_surface_layer_list
->at(surface_index
));
61 RenderSurface
* render_surface
= render_surface_layer
->render_surface();
63 render_surface_layer
->count_representing_target_surface_
= -1;
64 render_surface_layer
->count_representing_contributing_surface_
= -1;
65 render_surface_layer
->count_representing_itself_
= -1;
67 for (unsigned layer_index
= 0;
68 layer_index
< render_surface
->layer_list().size();
70 TestLayer
* layer
= static_cast<TestLayer
*>(
71 render_surface
->layer_list().at(layer_index
).get());
73 layer
->count_representing_target_surface_
= -1;
74 layer
->count_representing_contributing_surface_
= -1;
75 layer
->count_representing_itself_
= -1;
80 void IterateFrontToBack(
81 RenderSurfaceLayerList
* render_surface_layer_list
) {
82 ResetCounts(render_surface_layer_list
);
84 for (FrontToBack it
= FrontToBack::Begin(render_surface_layer_list
);
85 it
!= FrontToBack::End(render_surface_layer_list
);
87 TestLayer
* layer
= static_cast<TestLayer
*>(*it
);
88 if (it
.represents_target_render_surface())
89 layer
->count_representing_target_surface_
= count
;
90 if (it
.represents_contributing_render_surface())
91 layer
->count_representing_contributing_surface_
= count
;
92 if (it
.represents_itself())
93 layer
->count_representing_itself_
= count
;
97 TEST(LayerIteratorTest
, EmptyTree
) {
98 RenderSurfaceLayerList render_surface_layer_list
;
100 IterateFrontToBack(&render_surface_layer_list
);
103 TEST(LayerIteratorTest
, SimpleTree
) {
104 scoped_refptr
<TestLayer
> root_layer
= TestLayer::Create();
105 scoped_refptr
<TestLayer
> first
= TestLayer::Create();
106 scoped_refptr
<TestLayer
> second
= TestLayer::Create();
107 scoped_refptr
<TestLayer
> third
= TestLayer::Create();
108 scoped_refptr
<TestLayer
> fourth
= TestLayer::Create();
110 root_layer
->AddChild(first
);
111 root_layer
->AddChild(second
);
112 root_layer
->AddChild(third
);
113 root_layer
->AddChild(fourth
);
115 scoped_ptr
<FakeLayerTreeHost
> host
= FakeLayerTreeHost::Create();
116 host
->SetRootLayer(root_layer
);
118 RenderSurfaceLayerList render_surface_layer_list
;
119 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting
inputs(
120 root_layer
.get(), root_layer
->bounds(), &render_surface_layer_list
);
121 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
123 IterateFrontToBack(&render_surface_layer_list
);
124 EXPECT_COUNT(root_layer
, 5, -1, 4);
125 EXPECT_COUNT(first
, -1, -1, 3);
126 EXPECT_COUNT(second
, -1, -1, 2);
127 EXPECT_COUNT(third
, -1, -1, 1);
128 EXPECT_COUNT(fourth
, -1, -1, 0);
131 TEST(LayerIteratorTest
, ComplexTree
) {
132 scoped_refptr
<TestLayer
> root_layer
= TestLayer::Create();
133 scoped_refptr
<TestLayer
> root1
= TestLayer::Create();
134 scoped_refptr
<TestLayer
> root2
= TestLayer::Create();
135 scoped_refptr
<TestLayer
> root3
= TestLayer::Create();
136 scoped_refptr
<TestLayer
> root21
= TestLayer::Create();
137 scoped_refptr
<TestLayer
> root22
= TestLayer::Create();
138 scoped_refptr
<TestLayer
> root23
= TestLayer::Create();
139 scoped_refptr
<TestLayer
> root221
= TestLayer::Create();
140 scoped_refptr
<TestLayer
> root231
= TestLayer::Create();
142 root_layer
->AddChild(root1
);
143 root_layer
->AddChild(root2
);
144 root_layer
->AddChild(root3
);
145 root2
->AddChild(root21
);
146 root2
->AddChild(root22
);
147 root2
->AddChild(root23
);
148 root22
->AddChild(root221
);
149 root23
->AddChild(root231
);
151 scoped_ptr
<FakeLayerTreeHost
> host
= FakeLayerTreeHost::Create();
152 host
->SetRootLayer(root_layer
);
154 RenderSurfaceLayerList render_surface_layer_list
;
155 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting
inputs(
156 root_layer
.get(), root_layer
->bounds(), &render_surface_layer_list
);
157 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
159 IterateFrontToBack(&render_surface_layer_list
);
160 EXPECT_COUNT(root_layer
, 9, -1, 8);
161 EXPECT_COUNT(root1
, -1, -1, 7);
162 EXPECT_COUNT(root2
, -1, -1, 6);
163 EXPECT_COUNT(root21
, -1, -1, 5);
164 EXPECT_COUNT(root22
, -1, -1, 4);
165 EXPECT_COUNT(root221
, -1, -1, 3);
166 EXPECT_COUNT(root23
, -1, -1, 2);
167 EXPECT_COUNT(root231
, -1, -1, 1);
168 EXPECT_COUNT(root3
, -1, -1, 0);
171 TEST(LayerIteratorTest
, ComplexTreeMultiSurface
) {
172 scoped_refptr
<TestLayer
> root_layer
= TestLayer::Create();
173 scoped_refptr
<TestLayer
> root1
= TestLayer::Create();
174 scoped_refptr
<TestLayer
> root2
= TestLayer::Create();
175 scoped_refptr
<TestLayer
> root3
= TestLayer::Create();
176 scoped_refptr
<TestLayer
> root21
= TestLayer::Create();
177 scoped_refptr
<TestLayer
> root22
= TestLayer::Create();
178 scoped_refptr
<TestLayer
> root23
= TestLayer::Create();
179 scoped_refptr
<TestLayer
> root221
= TestLayer::Create();
180 scoped_refptr
<TestLayer
> root231
= TestLayer::Create();
182 root_layer
->AddChild(root1
);
183 root_layer
->AddChild(root2
);
184 root_layer
->AddChild(root3
);
185 root2
->set_draws_content(false);
186 root2
->SetOpacity(0.5f
);
187 root2
->SetForceRenderSurface(true); // Force the layer to own a new surface.
188 root2
->AddChild(root21
);
189 root2
->AddChild(root22
);
190 root2
->AddChild(root23
);
191 root22
->SetOpacity(0.5f
);
192 root22
->AddChild(root221
);
193 root23
->SetOpacity(0.5f
);
194 root23
->AddChild(root231
);
196 scoped_ptr
<FakeLayerTreeHost
> host
= FakeLayerTreeHost::Create();
197 host
->SetRootLayer(root_layer
);
199 RenderSurfaceLayerList render_surface_layer_list
;
200 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting
inputs(
201 root_layer
.get(), root_layer
->bounds(), &render_surface_layer_list
);
202 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
204 IterateFrontToBack(&render_surface_layer_list
);
205 EXPECT_COUNT(root_layer
, 14, -1, 13);
206 EXPECT_COUNT(root1
, -1, -1, 12);
207 EXPECT_COUNT(root2
, 10, 11, -1);
208 EXPECT_COUNT(root21
, -1, -1, 9);
209 EXPECT_COUNT(root22
, 7, 8, 6);
210 EXPECT_COUNT(root221
, -1, -1, 5);
211 EXPECT_COUNT(root23
, 3, 4, 2);
212 EXPECT_COUNT(root231
, -1, -1, 1);
213 EXPECT_COUNT(root3
, -1, -1, 0);