blob: d30d580ced14633121b01cf92f658ce60ccc0696 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// 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
[email protected]d50c6862012-10-23 02:08:315#include "cc/layer_iterator.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]a8461d82012-10-16 21:11:147#include "cc/layer.h"
[email protected]d50c6862012-10-23 02:08:318#include "cc/layer_tree_host_common.h"
[email protected]7f0c53db2012-10-02 00:23:189#include "testing/gmock/include/gmock/gmock.h"
10#include "testing/gtest/include/gtest/gtest.h"
[email protected]c8686a02012-11-27 08:29:0011#include "ui/gfx/transform.h"
[email protected]94f206c12012-08-25 00:09:1412
[email protected]94f206c12012-08-25 00:09:1413using ::testing::Mock;
14using ::testing::_;
15using ::testing::AtLeast;
16using ::testing::AnyNumber;
17
[email protected]ba565742012-11-10 09:29:4818namespace cc {
[email protected]94f206c12012-08-25 00:09:1419namespace {
20
[email protected]96baf3e2012-10-22 23:09:5521class TestLayer : public Layer {
[email protected]94f206c12012-08-25 00:09:1422public:
[email protected]96baf3e2012-10-22 23:09:5523 static scoped_refptr<TestLayer> create() { return make_scoped_refptr(new TestLayer()); }
[email protected]94f206c12012-08-25 00:09:1424
25 int m_countRepresentingTargetSurface;
26 int m_countRepresentingContributingSurface;
27 int m_countRepresentingItself;
28
29 virtual bool drawsContent() const OVERRIDE { return m_drawsContent; }
30 void setDrawsContent(bool drawsContent) { m_drawsContent = drawsContent; }
31
32private:
[email protected]96baf3e2012-10-22 23:09:5533 TestLayer()
34 : Layer()
[email protected]94f206c12012-08-25 00:09:1435 , m_drawsContent(true)
36 {
[email protected]aad0a0072012-11-01 18:15:5837 setBounds(gfx::Size(100, 100));
38 setPosition(gfx::Point());
39 setAnchorPoint(gfx::Point());
[email protected]94f206c12012-08-25 00:09:1440 }
[email protected]96baf3e2012-10-22 23:09:5541 virtual ~TestLayer()
[email protected]d58499a2012-10-09 22:27:4742 {
43 }
[email protected]94f206c12012-08-25 00:09:1444
45 bool m_drawsContent;
46};
47
48#define EXPECT_COUNT(layer, target, contrib, itself) \
49 EXPECT_EQ(target, layer->m_countRepresentingTargetSurface); \
50 EXPECT_EQ(contrib, layer->m_countRepresentingContributingSurface); \
51 EXPECT_EQ(itself, layer->m_countRepresentingItself);
52
[email protected]96baf3e2012-10-22 23:09:5553typedef LayerIterator<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, LayerIteratorActions::FrontToBack> FrontToBack;
54typedef LayerIterator<Layer, std::vector<scoped_refptr<Layer> >, RenderSurface, LayerIteratorActions::BackToFront> BackToFront;
[email protected]94f206c12012-08-25 00:09:1455
[email protected]96baf3e2012-10-22 23:09:5556void resetCounts(std::vector<scoped_refptr<Layer> >& renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:1457{
58 for (unsigned surfaceIndex = 0; surfaceIndex < renderSurfaceLayerList.size(); ++surfaceIndex) {
[email protected]96baf3e2012-10-22 23:09:5559 TestLayer* renderSurfaceLayer = static_cast<TestLayer*>(renderSurfaceLayerList[surfaceIndex].get());
60 RenderSurface* renderSurface = renderSurfaceLayer->renderSurface();
[email protected]94f206c12012-08-25 00:09:1461
62 renderSurfaceLayer->m_countRepresentingTargetSurface = -1;
63 renderSurfaceLayer->m_countRepresentingContributingSurface = -1;
64 renderSurfaceLayer->m_countRepresentingItself = -1;
65
66 for (unsigned layerIndex = 0; layerIndex < renderSurface->layerList().size(); ++layerIndex) {
[email protected]96baf3e2012-10-22 23:09:5567 TestLayer* layer = static_cast<TestLayer*>(renderSurface->layerList()[layerIndex].get());
[email protected]94f206c12012-08-25 00:09:1468
69 layer->m_countRepresentingTargetSurface = -1;
70 layer->m_countRepresentingContributingSurface = -1;
71 layer->m_countRepresentingItself = -1;
72 }
73 }
74}
75
[email protected]96baf3e2012-10-22 23:09:5576void iterateFrontToBack(std::vector<scoped_refptr<Layer> >* renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:1477{
78 resetCounts(*renderSurfaceLayerList);
79 int count = 0;
80 for (FrontToBack it = FrontToBack::begin(renderSurfaceLayerList); it != FrontToBack::end(renderSurfaceLayerList); ++it, ++count) {
[email protected]96baf3e2012-10-22 23:09:5581 TestLayer* layer = static_cast<TestLayer*>(*it);
[email protected]94f206c12012-08-25 00:09:1482 if (it.representsTargetRenderSurface())
83 layer->m_countRepresentingTargetSurface = count;
84 if (it.representsContributingRenderSurface())
85 layer->m_countRepresentingContributingSurface = count;
86 if (it.representsItself())
87 layer->m_countRepresentingItself = count;
88 }
89}
90
[email protected]96baf3e2012-10-22 23:09:5591void iterateBackToFront(std::vector<scoped_refptr<Layer> >* renderSurfaceLayerList)
[email protected]94f206c12012-08-25 00:09:1492{
93 resetCounts(*renderSurfaceLayerList);
94 int count = 0;
95 for (BackToFront it = BackToFront::begin(renderSurfaceLayerList); it != BackToFront::end(renderSurfaceLayerList); ++it, ++count) {
[email protected]96baf3e2012-10-22 23:09:5596 TestLayer* layer = static_cast<TestLayer*>(*it);
[email protected]94f206c12012-08-25 00:09:1497 if (it.representsTargetRenderSurface())
98 layer->m_countRepresentingTargetSurface = count;
99 if (it.representsContributingRenderSurface())
100 layer->m_countRepresentingContributingSurface = count;
101 if (it.representsItself())
102 layer->m_countRepresentingItself = count;
103 }
104}
105
[email protected]96baf3e2012-10-22 23:09:55106TEST(LayerIteratorTest, emptyTree)
[email protected]94f206c12012-08-25 00:09:14107{
[email protected]96baf3e2012-10-22 23:09:55108 std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
[email protected]94f206c12012-08-25 00:09:14109
110 iterateBackToFront(&renderSurfaceLayerList);
111 iterateFrontToBack(&renderSurfaceLayerList);
112}
113
[email protected]96baf3e2012-10-22 23:09:55114TEST(LayerIteratorTest, simpleTree)
[email protected]94f206c12012-08-25 00:09:14115{
[email protected]96baf3e2012-10-22 23:09:55116 scoped_refptr<TestLayer> rootLayer = TestLayer::create();
117 scoped_refptr<TestLayer> first = TestLayer::create();
118 scoped_refptr<TestLayer> second = TestLayer::create();
119 scoped_refptr<TestLayer> third = TestLayer::create();
120 scoped_refptr<TestLayer> fourth = TestLayer::create();
[email protected]94f206c12012-08-25 00:09:14121
122 rootLayer->createRenderSurface();
123
124 rootLayer->addChild(first);
125 rootLayer->addChild(second);
126 rootLayer->addChild(third);
127 rootLayer->addChild(fourth);
128
[email protected]96baf3e2012-10-22 23:09:55129 std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
[email protected]10aabcc32012-12-13 09:18:59130 LayerTreeHostCommon::calculateDrawProperties(rootLayer.get(), rootLayer->bounds(), 1, 1, 256, false, renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14131
132 iterateBackToFront(&renderSurfaceLayerList);
133 EXPECT_COUNT(rootLayer, 0, -1, 1);
134 EXPECT_COUNT(first, -1, -1, 2);
135 EXPECT_COUNT(second, -1, -1, 3);
136 EXPECT_COUNT(third, -1, -1, 4);
137 EXPECT_COUNT(fourth, -1, -1, 5);
138
139 iterateFrontToBack(&renderSurfaceLayerList);
140 EXPECT_COUNT(rootLayer, 5, -1, 4);
141 EXPECT_COUNT(first, -1, -1, 3);
142 EXPECT_COUNT(second, -1, -1, 2);
143 EXPECT_COUNT(third, -1, -1, 1);
144 EXPECT_COUNT(fourth, -1, -1, 0);
145
146}
147
[email protected]96baf3e2012-10-22 23:09:55148TEST(LayerIteratorTest, complexTree)
[email protected]94f206c12012-08-25 00:09:14149{
[email protected]96baf3e2012-10-22 23:09:55150 scoped_refptr<TestLayer> rootLayer = TestLayer::create();
151 scoped_refptr<TestLayer> root1 = TestLayer::create();
152 scoped_refptr<TestLayer> root2 = TestLayer::create();
153 scoped_refptr<TestLayer> root3 = TestLayer::create();
154 scoped_refptr<TestLayer> root21 = TestLayer::create();
155 scoped_refptr<TestLayer> root22 = TestLayer::create();
156 scoped_refptr<TestLayer> root23 = TestLayer::create();
157 scoped_refptr<TestLayer> root221 = TestLayer::create();
158 scoped_refptr<TestLayer> root231 = TestLayer::create();
[email protected]94f206c12012-08-25 00:09:14159
160 rootLayer->createRenderSurface();
161
162 rootLayer->addChild(root1);
163 rootLayer->addChild(root2);
164 rootLayer->addChild(root3);
165 root2->addChild(root21);
166 root2->addChild(root22);
167 root2->addChild(root23);
168 root22->addChild(root221);
169 root23->addChild(root231);
170
[email protected]96baf3e2012-10-22 23:09:55171 std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
[email protected]10aabcc32012-12-13 09:18:59172 LayerTreeHostCommon::calculateDrawProperties(rootLayer.get(), rootLayer->bounds(), 1, 1, 256, false, renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14173
174 iterateBackToFront(&renderSurfaceLayerList);
175 EXPECT_COUNT(rootLayer, 0, -1, 1);
176 EXPECT_COUNT(root1, -1, -1, 2);
177 EXPECT_COUNT(root2, -1, -1, 3);
178 EXPECT_COUNT(root21, -1, -1, 4);
179 EXPECT_COUNT(root22, -1, -1, 5);
180 EXPECT_COUNT(root221, -1, -1, 6);
181 EXPECT_COUNT(root23, -1, -1, 7);
182 EXPECT_COUNT(root231, -1, -1, 8);
183 EXPECT_COUNT(root3, -1, -1, 9);
184
185 iterateFrontToBack(&renderSurfaceLayerList);
186 EXPECT_COUNT(rootLayer, 9, -1, 8);
187 EXPECT_COUNT(root1, -1, -1, 7);
188 EXPECT_COUNT(root2, -1, -1, 6);
189 EXPECT_COUNT(root21, -1, -1, 5);
190 EXPECT_COUNT(root22, -1, -1, 4);
191 EXPECT_COUNT(root221, -1, -1, 3);
192 EXPECT_COUNT(root23, -1, -1, 2);
193 EXPECT_COUNT(root231, -1, -1, 1);
194 EXPECT_COUNT(root3, -1, -1, 0);
195
196}
197
[email protected]96baf3e2012-10-22 23:09:55198TEST(LayerIteratorTest, complexTreeMultiSurface)
[email protected]94f206c12012-08-25 00:09:14199{
[email protected]96baf3e2012-10-22 23:09:55200 scoped_refptr<TestLayer> rootLayer = TestLayer::create();
201 scoped_refptr<TestLayer> root1 = TestLayer::create();
202 scoped_refptr<TestLayer> root2 = TestLayer::create();
203 scoped_refptr<TestLayer> root3 = TestLayer::create();
204 scoped_refptr<TestLayer> root21 = TestLayer::create();
205 scoped_refptr<TestLayer> root22 = TestLayer::create();
206 scoped_refptr<TestLayer> root23 = TestLayer::create();
207 scoped_refptr<TestLayer> root221 = TestLayer::create();
208 scoped_refptr<TestLayer> root231 = TestLayer::create();
[email protected]94f206c12012-08-25 00:09:14209
210 rootLayer->createRenderSurface();
[email protected]aad0a0072012-11-01 18:15:58211 rootLayer->renderSurface()->setContentRect(gfx::Rect(gfx::Point(), rootLayer->bounds()));
[email protected]94f206c12012-08-25 00:09:14212
213 rootLayer->addChild(root1);
214 rootLayer->addChild(root2);
215 rootLayer->addChild(root3);
216 root2->setDrawsContent(false);
[email protected]498ec6e0e2012-11-30 18:24:57217 root2->setOpacity(0.5);
218 root2->setForceRenderSurface(true); // Force the layer to own a new surface.
[email protected]94f206c12012-08-25 00:09:14219 root2->addChild(root21);
220 root2->addChild(root22);
221 root2->addChild(root23);
222 root22->setOpacity(0.5);
223 root22->addChild(root221);
224 root23->setOpacity(0.5);
225 root23->addChild(root231);
226
[email protected]96baf3e2012-10-22 23:09:55227 std::vector<scoped_refptr<Layer> > renderSurfaceLayerList;
[email protected]10aabcc32012-12-13 09:18:59228 LayerTreeHostCommon::calculateDrawProperties(rootLayer.get(), rootLayer->bounds(), 1, 1, 256, false, renderSurfaceLayerList);
[email protected]94f206c12012-08-25 00:09:14229
230 iterateBackToFront(&renderSurfaceLayerList);
231 EXPECT_COUNT(rootLayer, 0, -1, 1);
232 EXPECT_COUNT(root1, -1, -1, 2);
233 EXPECT_COUNT(root2, 4, 3, -1);
234 EXPECT_COUNT(root21, -1, -1, 5);
235 EXPECT_COUNT(root22, 7, 6, 8);
236 EXPECT_COUNT(root221, -1, -1, 9);
237 EXPECT_COUNT(root23, 11, 10, 12);
238 EXPECT_COUNT(root231, -1, -1, 13);
239 EXPECT_COUNT(root3, -1, -1, 14);
240
241 iterateFrontToBack(&renderSurfaceLayerList);
242 EXPECT_COUNT(rootLayer, 14, -1, 13);
243 EXPECT_COUNT(root1, -1, -1, 12);
244 EXPECT_COUNT(root2, 10, 11, -1);
245 EXPECT_COUNT(root21, -1, -1, 9);
246 EXPECT_COUNT(root22, 7, 8, 6);
247 EXPECT_COUNT(root221, -1, -1, 5);
248 EXPECT_COUNT(root23, 3, 4, 2);
249 EXPECT_COUNT(root231, -1, -1, 1);
250 EXPECT_COUNT(root3, -1, -1, 0);
251}
252
[email protected]ba565742012-11-10 09:29:48253} // namespace
254} // namespace cc