blob: 36d22dad3883a63f42d172b56473e310fbbeecd1 [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
5#include "config.h"
6
7#include "CCLayerIterator.h"
8
9#include "CCLayerTreeHostCommon.h"
10#include "LayerChromium.h"
[email protected]7f0c53db2012-10-02 00:23:1811#include "testing/gmock/include/gmock/gmock.h"
12#include "testing/gtest/include/gtest/gtest.h"
[email protected]94f206c12012-08-25 00:09:1413#include <public/WebTransformationMatrix.h>
14
[email protected]9c88e562012-09-14 22:21:3015using namespace cc;
[email protected]94f206c12012-08-25 00:09:1416using WebKit::WebTransformationMatrix;
17using ::testing::Mock;
18using ::testing::_;
19using ::testing::AtLeast;
20using ::testing::AnyNumber;
21
22namespace {
23
24class TestLayerChromium : public LayerChromium {
25public:
26 static PassRefPtr<TestLayerChromium> create() { return adoptRef(new TestLayerChromium()); }
27
28 int m_countRepresentingTargetSurface;
29 int m_countRepresentingContributingSurface;
30 int m_countRepresentingItself;
31
32 virtual bool drawsContent() const OVERRIDE { return m_drawsContent; }
33 void setDrawsContent(bool drawsContent) { m_drawsContent = drawsContent; }
34
35private:
36 TestLayerChromium()
37 : LayerChromium()
38 , m_drawsContent(true)
39 {
40 setBounds(IntSize(100, 100));
[email protected]9c88e562012-09-14 22:21:3041 setPosition(IntPoint());
42 setAnchorPoint(IntPoint());
[email protected]94f206c12012-08-25 00:09:1443 }
44
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
53typedef CCLayerIterator<LayerChromium, Vector<RefPtr<LayerChromium> >, RenderSurfaceChromium, CCLayerIteratorActions::FrontToBack> FrontToBack;
54typedef CCLayerIterator<LayerChromium, Vector<RefPtr<LayerChromium> >, RenderSurfaceChromium, CCLayerIteratorActions::BackToFront> BackToFront;
55
56void resetCounts(Vector<RefPtr<LayerChromium> >& renderSurfaceLayerList)
57{
58 for (unsigned surfaceIndex = 0; surfaceIndex < renderSurfaceLayerList.size(); ++surfaceIndex) {
59 TestLayerChromium* renderSurfaceLayer = static_cast<TestLayerChromium*>(renderSurfaceLayerList[surfaceIndex].get());
60 RenderSurfaceChromium* renderSurface = renderSurfaceLayer->renderSurface();
61
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) {
67 TestLayerChromium* layer = static_cast<TestLayerChromium*>(renderSurface->layerList()[layerIndex].get());
68
69 layer->m_countRepresentingTargetSurface = -1;
70 layer->m_countRepresentingContributingSurface = -1;
71 layer->m_countRepresentingItself = -1;
72 }
73 }
74}
75
76void iterateFrontToBack(Vector<RefPtr<LayerChromium> >* renderSurfaceLayerList)
77{
78 resetCounts(*renderSurfaceLayerList);
79 int count = 0;
80 for (FrontToBack it = FrontToBack::begin(renderSurfaceLayerList); it != FrontToBack::end(renderSurfaceLayerList); ++it, ++count) {
81 TestLayerChromium* layer = static_cast<TestLayerChromium*>(*it);
82 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
91void iterateBackToFront(Vector<RefPtr<LayerChromium> >* renderSurfaceLayerList)
92{
93 resetCounts(*renderSurfaceLayerList);
94 int count = 0;
95 for (BackToFront it = BackToFront::begin(renderSurfaceLayerList); it != BackToFront::end(renderSurfaceLayerList); ++it, ++count) {
96 TestLayerChromium* layer = static_cast<TestLayerChromium*>(*it);
97 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
106TEST(CCLayerIteratorTest, emptyTree)
107{
108 Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
109
110 iterateBackToFront(&renderSurfaceLayerList);
111 iterateFrontToBack(&renderSurfaceLayerList);
112}
113
114TEST(CCLayerIteratorTest, simpleTree)
115{
116 RefPtr<TestLayerChromium> rootLayer = TestLayerChromium::create();
117 RefPtr<TestLayerChromium> first = TestLayerChromium::create();
118 RefPtr<TestLayerChromium> second = TestLayerChromium::create();
119 RefPtr<TestLayerChromium> third = TestLayerChromium::create();
120 RefPtr<TestLayerChromium> fourth = TestLayerChromium::create();
121
122 rootLayer->createRenderSurface();
123
124 rootLayer->addChild(first);
125 rootLayer->addChild(second);
126 rootLayer->addChild(third);
127 rootLayer->addChild(fourth);
128
129 Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
130 CCLayerTreeHostCommon::calculateDrawTransforms(rootLayer.get(), rootLayer->bounds(), 1, 256, renderSurfaceLayerList);
131 CCLayerTreeHostCommon::calculateVisibleRects(renderSurfaceLayerList);
132
133 iterateBackToFront(&renderSurfaceLayerList);
134 EXPECT_COUNT(rootLayer, 0, -1, 1);
135 EXPECT_COUNT(first, -1, -1, 2);
136 EXPECT_COUNT(second, -1, -1, 3);
137 EXPECT_COUNT(third, -1, -1, 4);
138 EXPECT_COUNT(fourth, -1, -1, 5);
139
140 iterateFrontToBack(&renderSurfaceLayerList);
141 EXPECT_COUNT(rootLayer, 5, -1, 4);
142 EXPECT_COUNT(first, -1, -1, 3);
143 EXPECT_COUNT(second, -1, -1, 2);
144 EXPECT_COUNT(third, -1, -1, 1);
145 EXPECT_COUNT(fourth, -1, -1, 0);
146
147}
148
149TEST(CCLayerIteratorTest, complexTree)
150{
151 RefPtr<TestLayerChromium> rootLayer = TestLayerChromium::create();
152 RefPtr<TestLayerChromium> root1 = TestLayerChromium::create();
153 RefPtr<TestLayerChromium> root2 = TestLayerChromium::create();
154 RefPtr<TestLayerChromium> root3 = TestLayerChromium::create();
155 RefPtr<TestLayerChromium> root21 = TestLayerChromium::create();
156 RefPtr<TestLayerChromium> root22 = TestLayerChromium::create();
157 RefPtr<TestLayerChromium> root23 = TestLayerChromium::create();
158 RefPtr<TestLayerChromium> root221 = TestLayerChromium::create();
159 RefPtr<TestLayerChromium> root231 = TestLayerChromium::create();
160
161 rootLayer->createRenderSurface();
162
163 rootLayer->addChild(root1);
164 rootLayer->addChild(root2);
165 rootLayer->addChild(root3);
166 root2->addChild(root21);
167 root2->addChild(root22);
168 root2->addChild(root23);
169 root22->addChild(root221);
170 root23->addChild(root231);
171
172 Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
173 CCLayerTreeHostCommon::calculateDrawTransforms(rootLayer.get(), rootLayer->bounds(), 1, 256, renderSurfaceLayerList);
174 CCLayerTreeHostCommon::calculateVisibleRects(renderSurfaceLayerList);
175
176 iterateBackToFront(&renderSurfaceLayerList);
177 EXPECT_COUNT(rootLayer, 0, -1, 1);
178 EXPECT_COUNT(root1, -1, -1, 2);
179 EXPECT_COUNT(root2, -1, -1, 3);
180 EXPECT_COUNT(root21, -1, -1, 4);
181 EXPECT_COUNT(root22, -1, -1, 5);
182 EXPECT_COUNT(root221, -1, -1, 6);
183 EXPECT_COUNT(root23, -1, -1, 7);
184 EXPECT_COUNT(root231, -1, -1, 8);
185 EXPECT_COUNT(root3, -1, -1, 9);
186
187 iterateFrontToBack(&renderSurfaceLayerList);
188 EXPECT_COUNT(rootLayer, 9, -1, 8);
189 EXPECT_COUNT(root1, -1, -1, 7);
190 EXPECT_COUNT(root2, -1, -1, 6);
191 EXPECT_COUNT(root21, -1, -1, 5);
192 EXPECT_COUNT(root22, -1, -1, 4);
193 EXPECT_COUNT(root221, -1, -1, 3);
194 EXPECT_COUNT(root23, -1, -1, 2);
195 EXPECT_COUNT(root231, -1, -1, 1);
196 EXPECT_COUNT(root3, -1, -1, 0);
197
198}
199
200TEST(CCLayerIteratorTest, complexTreeMultiSurface)
201{
202 RefPtr<TestLayerChromium> rootLayer = TestLayerChromium::create();
203 RefPtr<TestLayerChromium> root1 = TestLayerChromium::create();
204 RefPtr<TestLayerChromium> root2 = TestLayerChromium::create();
205 RefPtr<TestLayerChromium> root3 = TestLayerChromium::create();
206 RefPtr<TestLayerChromium> root21 = TestLayerChromium::create();
207 RefPtr<TestLayerChromium> root22 = TestLayerChromium::create();
208 RefPtr<TestLayerChromium> root23 = TestLayerChromium::create();
209 RefPtr<TestLayerChromium> root221 = TestLayerChromium::create();
210 RefPtr<TestLayerChromium> root231 = TestLayerChromium::create();
211
212 rootLayer->createRenderSurface();
213 rootLayer->renderSurface()->setContentRect(IntRect(IntPoint(), rootLayer->bounds()));
214
215 rootLayer->addChild(root1);
216 rootLayer->addChild(root2);
217 rootLayer->addChild(root3);
218 root2->setDrawsContent(false);
219 root2->setOpacity(0.5); // Force the layer to own a new surface.
220 root2->addChild(root21);
221 root2->addChild(root22);
222 root2->addChild(root23);
223 root22->setOpacity(0.5);
224 root22->addChild(root221);
225 root23->setOpacity(0.5);
226 root23->addChild(root231);
227
228 Vector<RefPtr<LayerChromium> > renderSurfaceLayerList;
229 CCLayerTreeHostCommon::calculateDrawTransforms(rootLayer.get(), rootLayer->bounds(), 1, 256, renderSurfaceLayerList);
230 CCLayerTreeHostCommon::calculateVisibleRects(renderSurfaceLayerList);
231
232 iterateBackToFront(&renderSurfaceLayerList);
233 EXPECT_COUNT(rootLayer, 0, -1, 1);
234 EXPECT_COUNT(root1, -1, -1, 2);
235 EXPECT_COUNT(root2, 4, 3, -1);
236 EXPECT_COUNT(root21, -1, -1, 5);
237 EXPECT_COUNT(root22, 7, 6, 8);
238 EXPECT_COUNT(root221, -1, -1, 9);
239 EXPECT_COUNT(root23, 11, 10, 12);
240 EXPECT_COUNT(root231, -1, -1, 13);
241 EXPECT_COUNT(root3, -1, -1, 14);
242
243 iterateFrontToBack(&renderSurfaceLayerList);
244 EXPECT_COUNT(rootLayer, 14, -1, 13);
245 EXPECT_COUNT(root1, -1, -1, 12);
246 EXPECT_COUNT(root2, 10, 11, -1);
247 EXPECT_COUNT(root21, -1, -1, 9);
248 EXPECT_COUNT(root22, 7, 8, 6);
249 EXPECT_COUNT(root221, -1, -1, 5);
250 EXPECT_COUNT(root23, 3, 4, 2);
251 EXPECT_COUNT(root231, -1, -1, 1);
252 EXPECT_COUNT(root3, -1, -1, 0);
253}
254
255} // namespace