layer_iterator_unittest.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
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.
4
5#include "cc/layers/layer_iterator.h"
6
7#include <vector>
8
9#include "cc/layers/layer.h"
10#include "cc/trees/layer_tree_host_common.h"
11#include "testing/gmock/include/gmock/gmock.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#include "ui/gfx/transform.h"
14
15using ::testing::Mock;
16using ::testing::_;
17using ::testing::AtLeast;
18using ::testing::AnyNumber;
19
20namespace cc {
21namespace {
22
23class TestLayer : public Layer {
24 public:
25  static scoped_refptr<TestLayer> Create() {
26    return make_scoped_refptr(new TestLayer());
27  }
28
29  int count_representing_target_surface_;
30  int count_representing_contributing_surface_;
31  int count_representing_itself_;
32
33  virtual bool DrawsContent() const OVERRIDE { return draws_content_; }
34  void set_draws_content(bool draws_content) { draws_content_ = draws_content; }
35
36 private:
37  TestLayer() : Layer(), draws_content_(true) {
38    SetBounds(gfx::Size(100, 100));
39    SetPosition(gfx::Point());
40    SetAnchorPoint(gfx::Point());
41  }
42  virtual ~TestLayer() {}
43
44  bool draws_content_;
45};
46
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_);
51
52typedef LayerIterator<Layer,
53                      RenderSurfaceLayerList,
54                      RenderSurface,
55                      LayerIteratorActions::FrontToBack> FrontToBack;
56typedef LayerIterator<Layer,
57                      RenderSurfaceLayerList,
58                      RenderSurface,
59                      LayerIteratorActions::BackToFront> BackToFront;
60
61void ResetCounts(RenderSurfaceLayerList* render_surface_layer_list) {
62  for (unsigned surface_index = 0;
63       surface_index < render_surface_layer_list->size();
64       ++surface_index) {
65    TestLayer* render_surface_layer = static_cast<TestLayer*>(
66        render_surface_layer_list->at(surface_index));
67    RenderSurface* render_surface = render_surface_layer->render_surface();
68
69    render_surface_layer->count_representing_target_surface_ = -1;
70    render_surface_layer->count_representing_contributing_surface_ = -1;
71    render_surface_layer->count_representing_itself_ = -1;
72
73    for (unsigned layer_index = 0;
74         layer_index < render_surface->layer_list().size();
75         ++layer_index) {
76      TestLayer* layer = static_cast<TestLayer*>(
77          render_surface->layer_list().at(layer_index));
78
79      layer->count_representing_target_surface_ = -1;
80      layer->count_representing_contributing_surface_ = -1;
81      layer->count_representing_itself_ = -1;
82    }
83  }
84}
85
86void IterateFrontToBack(
87    RenderSurfaceLayerList* render_surface_layer_list) {
88  ResetCounts(render_surface_layer_list);
89  int count = 0;
90  for (FrontToBack it = FrontToBack::Begin(render_surface_layer_list);
91       it != FrontToBack::End(render_surface_layer_list);
92       ++it, ++count) {
93    TestLayer* layer = static_cast<TestLayer*>(*it);
94    if (it.represents_target_render_surface())
95      layer->count_representing_target_surface_ = count;
96    if (it.represents_contributing_render_surface())
97      layer->count_representing_contributing_surface_ = count;
98    if (it.represents_itself())
99      layer->count_representing_itself_ = count;
100  }
101}
102
103void IterateBackToFront(
104    RenderSurfaceLayerList* render_surface_layer_list) {
105  ResetCounts(render_surface_layer_list);
106  int count = 0;
107  for (BackToFront it = BackToFront::Begin(render_surface_layer_list);
108       it != BackToFront::End(render_surface_layer_list);
109       ++it, ++count) {
110    TestLayer* layer = static_cast<TestLayer*>(*it);
111    if (it.represents_target_render_surface())
112      layer->count_representing_target_surface_ = count;
113    if (it.represents_contributing_render_surface())
114      layer->count_representing_contributing_surface_ = count;
115    if (it.represents_itself())
116      layer->count_representing_itself_ = count;
117  }
118}
119
120TEST(LayerIteratorTest, EmptyTree) {
121  RenderSurfaceLayerList render_surface_layer_list;
122
123  IterateBackToFront(&render_surface_layer_list);
124  IterateFrontToBack(&render_surface_layer_list);
125}
126
127TEST(LayerIteratorTest, SimpleTree) {
128  scoped_refptr<TestLayer> root_layer = TestLayer::Create();
129  scoped_refptr<TestLayer> first = TestLayer::Create();
130  scoped_refptr<TestLayer> second = TestLayer::Create();
131  scoped_refptr<TestLayer> third = TestLayer::Create();
132  scoped_refptr<TestLayer> fourth = TestLayer::Create();
133
134  root_layer->AddChild(first);
135  root_layer->AddChild(second);
136  root_layer->AddChild(third);
137  root_layer->AddChild(fourth);
138
139  RenderSurfaceLayerList render_surface_layer_list;
140  LayerTreeHostCommon::CalculateDrawProperties(root_layer.get(),
141                                               root_layer->bounds(),
142                                               gfx::Transform(),
143                                               1.f,
144                                               1.f,
145                                               NULL,
146                                               256,
147                                               false,
148                                               false,
149                                               &render_surface_layer_list);
150
151  IterateBackToFront(&render_surface_layer_list);
152  EXPECT_COUNT(root_layer, 0, -1, 1);
153  EXPECT_COUNT(first, -1, -1, 2);
154  EXPECT_COUNT(second, -1, -1, 3);
155  EXPECT_COUNT(third, -1, -1, 4);
156  EXPECT_COUNT(fourth, -1, -1, 5);
157
158  IterateFrontToBack(&render_surface_layer_list);
159  EXPECT_COUNT(root_layer, 5, -1, 4);
160  EXPECT_COUNT(first, -1, -1, 3);
161  EXPECT_COUNT(second, -1, -1, 2);
162  EXPECT_COUNT(third, -1, -1, 1);
163  EXPECT_COUNT(fourth, -1, -1, 0);
164}
165
166TEST(LayerIteratorTest, ComplexTree) {
167  scoped_refptr<TestLayer> root_layer = TestLayer::Create();
168  scoped_refptr<TestLayer> root1 = TestLayer::Create();
169  scoped_refptr<TestLayer> root2 = TestLayer::Create();
170  scoped_refptr<TestLayer> root3 = TestLayer::Create();
171  scoped_refptr<TestLayer> root21 = TestLayer::Create();
172  scoped_refptr<TestLayer> root22 = TestLayer::Create();
173  scoped_refptr<TestLayer> root23 = TestLayer::Create();
174  scoped_refptr<TestLayer> root221 = TestLayer::Create();
175  scoped_refptr<TestLayer> root231 = TestLayer::Create();
176
177  root_layer->AddChild(root1);
178  root_layer->AddChild(root2);
179  root_layer->AddChild(root3);
180  root2->AddChild(root21);
181  root2->AddChild(root22);
182  root2->AddChild(root23);
183  root22->AddChild(root221);
184  root23->AddChild(root231);
185
186  RenderSurfaceLayerList render_surface_layer_list;
187  LayerTreeHostCommon::CalculateDrawProperties(root_layer.get(),
188                                               root_layer->bounds(),
189                                               gfx::Transform(),
190                                               1.f,
191                                               1.f,
192                                               NULL,
193                                               256,
194                                               false,
195                                               false,
196                                               &render_surface_layer_list);
197
198  IterateBackToFront(&render_surface_layer_list);
199  EXPECT_COUNT(root_layer, 0, -1, 1);
200  EXPECT_COUNT(root1, -1, -1, 2);
201  EXPECT_COUNT(root2, -1, -1, 3);
202  EXPECT_COUNT(root21, -1, -1, 4);
203  EXPECT_COUNT(root22, -1, -1, 5);
204  EXPECT_COUNT(root221, -1, -1, 6);
205  EXPECT_COUNT(root23, -1, -1, 7);
206  EXPECT_COUNT(root231, -1, -1, 8);
207  EXPECT_COUNT(root3, -1, -1, 9);
208
209  IterateFrontToBack(&render_surface_layer_list);
210  EXPECT_COUNT(root_layer, 9, -1, 8);
211  EXPECT_COUNT(root1, -1, -1, 7);
212  EXPECT_COUNT(root2, -1, -1, 6);
213  EXPECT_COUNT(root21, -1, -1, 5);
214  EXPECT_COUNT(root22, -1, -1, 4);
215  EXPECT_COUNT(root221, -1, -1, 3);
216  EXPECT_COUNT(root23, -1, -1, 2);
217  EXPECT_COUNT(root231, -1, -1, 1);
218  EXPECT_COUNT(root3, -1, -1, 0);
219}
220
221TEST(LayerIteratorTest, ComplexTreeMultiSurface) {
222  scoped_refptr<TestLayer> root_layer = TestLayer::Create();
223  scoped_refptr<TestLayer> root1 = TestLayer::Create();
224  scoped_refptr<TestLayer> root2 = TestLayer::Create();
225  scoped_refptr<TestLayer> root3 = TestLayer::Create();
226  scoped_refptr<TestLayer> root21 = TestLayer::Create();
227  scoped_refptr<TestLayer> root22 = TestLayer::Create();
228  scoped_refptr<TestLayer> root23 = TestLayer::Create();
229  scoped_refptr<TestLayer> root221 = TestLayer::Create();
230  scoped_refptr<TestLayer> root231 = TestLayer::Create();
231
232  root_layer->AddChild(root1);
233  root_layer->AddChild(root2);
234  root_layer->AddChild(root3);
235  root2->set_draws_content(false);
236  root2->SetOpacity(0.5f);
237  root2->SetForceRenderSurface(true);  // Force the layer to own a new surface.
238  root2->AddChild(root21);
239  root2->AddChild(root22);
240  root2->AddChild(root23);
241  root22->SetOpacity(0.5f);
242  root22->AddChild(root221);
243  root23->SetOpacity(0.5f);
244  root23->AddChild(root231);
245
246  RenderSurfaceLayerList render_surface_layer_list;
247  LayerTreeHostCommon::CalculateDrawProperties(root_layer.get(),
248                                               root_layer->bounds(),
249                                               gfx::Transform(),
250                                               1.f,
251                                               1.f,
252                                               NULL,
253                                               256,
254                                               false,
255                                               false,
256                                               &render_surface_layer_list);
257
258  IterateBackToFront(&render_surface_layer_list);
259  EXPECT_COUNT(root_layer, 0, -1, 1);
260  EXPECT_COUNT(root1, -1, -1, 2);
261  EXPECT_COUNT(root2, 4, 3, -1);
262  EXPECT_COUNT(root21, -1, -1, 5);
263  EXPECT_COUNT(root22, 7, 6, 8);
264  EXPECT_COUNT(root221, -1, -1, 9);
265  EXPECT_COUNT(root23, 11, 10, 12);
266  EXPECT_COUNT(root231, -1, -1, 13);
267  EXPECT_COUNT(root3, -1, -1, 14);
268
269  IterateFrontToBack(&render_surface_layer_list);
270  EXPECT_COUNT(root_layer, 14, -1, 13);
271  EXPECT_COUNT(root1, -1, -1, 12);
272  EXPECT_COUNT(root2, 10, 11, -1);
273  EXPECT_COUNT(root21, -1, -1, 9);
274  EXPECT_COUNT(root22, 7, 8, 6);
275  EXPECT_COUNT(root221, -1, -1, 5);
276  EXPECT_COUNT(root23, 3, 4, 2);
277  EXPECT_COUNT(root231, -1, -1, 1);
278  EXPECT_COUNT(root3, -1, -1, 0);
279}
280
281}  // namespace
282}  // namespace cc
283