[chromium] Incorrect replica originTransform used in CCDamageTracker
[WebKit-https.git] / Source / WebKit / chromium / tests / CCDamageTrackerTest.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1.  Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  * 2.  Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "config.h"
26
27 #include "cc/CCDamageTracker.h"
28
29 #include "CCLayerTreeTestCommon.h"
30 #include "cc/CCLayerImpl.h"
31 #include "cc/CCLayerSorter.h"
32 #include "cc/CCLayerTreeHostCommon.h"
33 #include "cc/CCSingleThreadProxy.h"
34 #include <gtest/gtest.h>
35
36 using namespace WebCore;
37 using namespace WTF;
38 using namespace WebKitTests;
39
40 namespace {
41
42 void executeCalculateDrawTransformsAndVisibility(CCLayerImpl* root, Vector<CCLayerImpl*>& renderSurfaceLayerList)
43 {
44     CCLayerSorter layerSorter;
45     TransformationMatrix identityMatrix;
46     Vector<CCLayerImpl*> dummyLayerList;
47     int dummyMaxTextureSize = 512;
48
49     // Sanity check: The test itself should create the root layer's render surface, so
50     //               that the surface (and its damage tracker) can persist across multiple
51     //               calls to this function.
52     ASSERT_TRUE(root->renderSurface());
53     ASSERT_FALSE(renderSurfaceLayerList.size());
54
55     root->renderSurface()->clearLayerList();
56     renderSurfaceLayerList.append(root);
57     CCLayerTreeHostCommon::calculateDrawTransformsAndVisibility(root, root, identityMatrix, identityMatrix, renderSurfaceLayerList, dummyLayerList, &layerSorter, dummyMaxTextureSize);
58 }
59
60 void emulateDrawingOneFrame(CCLayerImpl* root)
61 {
62     // This emulates only the steps that are relevant to testing the damage tracker:
63     //   1. computing the render passes and layerlists
64     //   2. updating all damage trackers in the correct order
65     //   3. resetting all updateRects and propertyChanged flags for all layers and surfaces.
66
67     Vector<CCLayerImpl*> renderSurfaceLayerList;
68     executeCalculateDrawTransformsAndVisibility(root, renderSurfaceLayerList);
69
70     // Iterate back-to-front, so that damage correctly propagates from descendant surfaces to ancestors.
71     for (int i = renderSurfaceLayerList.size() - 1; i >= 0; --i) {
72         CCRenderSurface* targetSurface = renderSurfaceLayerList[i]->renderSurface();
73         targetSurface->damageTracker()->updateDamageTrackingState(targetSurface->layerList(), targetSurface->owningLayerId(), targetSurface->surfacePropertyChangedOnlyFromDescendant(), targetSurface->contentRect(), renderSurfaceLayerList[i]->maskLayer());
74     }
75
76     root->resetAllChangeTrackingForSubtree();
77 }
78
79 PassOwnPtr<CCLayerImpl> createTestTreeWithOneSurface()
80 {
81     OwnPtr<CCLayerImpl> root = CCLayerImpl::create(1);
82     OwnPtr<CCLayerImpl> child = CCLayerImpl::create(2);
83
84     root->setPosition(FloatPoint::zero());
85     root->setAnchorPoint(FloatPoint::zero());
86     root->setBounds(IntSize(500, 500));
87     root->setDrawsContent(true);
88     root->createRenderSurface();
89     root->renderSurface()->setContentRect(IntRect(IntPoint(), IntSize(500, 500)));
90
91     child->setPosition(FloatPoint(100, 100));
92     child->setAnchorPoint(FloatPoint::zero());
93     child->setBounds(IntSize(30, 30));
94     child->setDrawsContent(true);
95     root->addChild(child.release());
96
97     return root.release();
98 }
99
100 PassOwnPtr<CCLayerImpl> createTestTreeWithTwoSurfaces()
101 {
102     // This test tree has two render surfaces: one for the root, and one for
103     // child1. Additionally, the root has a second child layer, and child1 has two
104     // children of its own.
105
106     OwnPtr<CCLayerImpl> root = CCLayerImpl::create(1);
107     OwnPtr<CCLayerImpl> child1 = CCLayerImpl::create(2);
108     OwnPtr<CCLayerImpl> child2 = CCLayerImpl::create(3);
109     OwnPtr<CCLayerImpl> grandChild1 = CCLayerImpl::create(4);
110     OwnPtr<CCLayerImpl> grandChild2 = CCLayerImpl::create(5);
111
112     root->setPosition(FloatPoint::zero());
113     root->setAnchorPoint(FloatPoint::zero());
114     root->setBounds(IntSize(500, 500));
115     root->setDrawsContent(true);
116     root->createRenderSurface();
117     root->renderSurface()->setContentRect(IntRect(IntPoint(), IntSize(500, 500)));
118
119     child1->setPosition(FloatPoint(100, 100));
120     child1->setAnchorPoint(FloatPoint::zero());
121     child1->setBounds(IntSize(30, 30));
122     child1->setOpacity(0.5); // with a child that drawsContent, this will cause the layer to create its own renderSurface.
123     child1->setDrawsContent(false); // this layer does not draw, but is intended to create its own renderSurface.
124
125     child2->setPosition(FloatPoint(11, 11));
126     child2->setAnchorPoint(FloatPoint::zero());
127     child2->setBounds(IntSize(18, 18));
128     child2->setDrawsContent(true);
129
130     grandChild1->setPosition(FloatPoint(200, 200));
131     grandChild1->setAnchorPoint(FloatPoint::zero());
132     grandChild1->setBounds(IntSize(6, 8));
133     grandChild1->setDrawsContent(true);
134
135     grandChild2->setPosition(FloatPoint(190, 190));
136     grandChild2->setAnchorPoint(FloatPoint::zero());
137     grandChild2->setBounds(IntSize(6, 8));
138     grandChild2->setDrawsContent(true);
139
140     child1->addChild(grandChild1.release());
141     child1->addChild(grandChild2.release());
142     root->addChild(child1.release());
143     root->addChild(child2.release());
144
145     return root.release();
146 }
147
148 PassOwnPtr<CCLayerImpl> createAndSetUpTestTreeWithOneSurface()
149 {
150     OwnPtr<CCLayerImpl> root = createTestTreeWithOneSurface();
151
152     // Setup includes going past the first frame which always damages everything, so
153     // that we can actually perform specific tests.
154     emulateDrawingOneFrame(root.get());
155
156     return root.release();
157 }
158
159 PassOwnPtr<CCLayerImpl> createAndSetUpTestTreeWithTwoSurfaces()
160 {
161     OwnPtr<CCLayerImpl> root = createTestTreeWithTwoSurfaces();
162
163     // Setup includes going past the first frame which always damages everything, so
164     // that we can actually perform specific tests.
165     emulateDrawingOneFrame(root.get());
166
167     return root.release();
168 }
169
170 class CCDamageTrackerTest : public testing::Test {
171 private:
172     // For testing purposes, fake that we are on the impl thread.
173     DebugScopedSetImplThread setImplThread;
174 };
175
176 TEST_F(CCDamageTrackerTest, sanityCheckTestTreeWithOneSurface)
177 {
178     // Sanity check that the simple test tree will actually produce the expected render
179     // surfaces and layer lists.
180
181     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
182
183     EXPECT_EQ(2u, root->renderSurface()->layerList().size());
184     EXPECT_EQ(1, root->renderSurface()->layerList()[0]->id());
185     EXPECT_EQ(2, root->renderSurface()->layerList()[1]->id());
186
187     FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
188     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 500, 500), rootDamageRect);
189 }
190
191 TEST_F(CCDamageTrackerTest, sanityCheckTestTreeWithTwoSurfaces)
192 {
193     // Sanity check that the complex test tree will actually produce the expected render
194     // surfaces and layer lists.
195
196     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
197
198     CCLayerImpl* child1 = root->children()[0].get();
199     CCLayerImpl* child2 = root->children()[1].get();
200     FloatRect childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
201     FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
202
203     ASSERT_TRUE(child1->renderSurface());
204     EXPECT_FALSE(child2->renderSurface());
205     EXPECT_EQ(3u, root->renderSurface()->layerList().size());
206     EXPECT_EQ(2u, child1->renderSurface()->layerList().size());
207
208     // The render surface for child1 only has a contentRect that encloses grandChild1 and grandChild2, because child1 does not draw content.
209     EXPECT_FLOAT_RECT_EQ(FloatRect(190, 190, 16, 18), childDamageRect);
210     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 500, 500), rootDamageRect);
211 }
212
213 TEST_F(CCDamageTrackerTest, verifyDamageForUpdateRects)
214 {
215     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
216     CCLayerImpl* child = root->children()[0].get();
217
218     // CASE 1: Setting the update rect should cause the corresponding damage to the surface.
219     //
220     child->setUpdateRect(FloatRect(10, 11, 12, 13));
221     emulateDrawingOneFrame(root.get());
222
223     // Damage position on the surface should be: position of updateRect (10, 11) relative to the child (100, 100).
224     FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
225     EXPECT_FLOAT_RECT_EQ(FloatRect(110, 111, 12, 13), rootDamageRect);
226
227     // CASE 2: The same update rect twice in a row still produces the same damage.
228     //
229     child->setUpdateRect(FloatRect(10, 11, 12, 13));
230     emulateDrawingOneFrame(root.get());
231     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
232     EXPECT_FLOAT_RECT_EQ(FloatRect(110, 111, 12, 13), rootDamageRect);
233
234     // CASE 3: Setting a different update rect should cause damage on the new update region, but no additional exposed old region.
235     //
236     child->setUpdateRect(FloatRect(20, 25, 1, 2));
237     emulateDrawingOneFrame(root.get());
238
239     // Damage position on the surface should be: position of updateRect (20, 25) relative to the child (100, 100).
240     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
241     EXPECT_FLOAT_RECT_EQ(FloatRect(120, 125, 1, 2), rootDamageRect);
242 }
243
244 TEST_F(CCDamageTrackerTest, verifyDamageForPropertyChanges)
245 {
246     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
247     CCLayerImpl* child = root->children()[0].get();
248
249     // CASE 1: The layer's property changed flag takes priority over update rect.
250     //
251     child->setUpdateRect(FloatRect(10, 11, 12, 13));
252     child->setOpacity(0.5);
253     emulateDrawingOneFrame(root.get());
254
255     // Sanity check - we should not have accidentally created a separate render surface for the translucent layer.
256     ASSERT_FALSE(child->renderSurface());
257     ASSERT_EQ(2u, root->renderSurface()->layerList().size());
258
259     // Damage should be the entire child layer in targetSurface space.
260     FloatRect expectedRect = FloatRect(100, 100, 30, 30);
261     FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
262     EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
263
264     // CASE 2: If a layer moves due to property change, it damages both the new location
265     //         and the old (exposed) location. The old location is the entire old layer,
266     //         not just the updateRect.
267
268     // Cycle one frame of no change, just to sanity check that the next rect is not because of the old damage state.
269     emulateDrawingOneFrame(root.get());
270     EXPECT_TRUE(root->renderSurface()->damageTracker()->currentDamageRect().isEmpty());
271
272     // Then, test the actual layer movement.
273     child->setPosition(FloatPoint(200, 230));
274     emulateDrawingOneFrame(root.get());
275
276     // Expect damage to be the combination of the previous one and the new one.
277     expectedRect.uniteIfNonZero(FloatRect(200, 230, 30, 30));
278     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
279     EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
280 }
281
282 TEST_F(CCDamageTrackerTest, verifyDamageForTransformedLayer)
283 {
284     // If a layer is transformed, the damage rect should still enclose the entire
285     // transformed layer.
286
287     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
288     CCLayerImpl* child = root->children()[0].get();
289
290     TransformationMatrix rotation;
291     rotation.rotate(45);
292
293     // Note carefully, the anchor is actually part of layer->position(). By setting anchor
294     // to (0.5, 0.5), the layer's position (100, 100) now refers to the center of the
295     // layer, not the corner. This means the layer has actually changed position.
296     child->setAnchorPoint(FloatPoint(0.5, 0.5));
297     emulateDrawingOneFrame(root.get());
298
299     // Sanity check that the layer actually moved to (85, 85), damaging its old location and new location.
300     FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
301     EXPECT_FLOAT_RECT_EQ(FloatRect(85, 85, 45, 45), rootDamageRect);
302
303     // With the anchor on the layer's center, now we can test the rotation more
304     // intuitively, since it applies about the layer's anchor.
305     child->setTransform(rotation);
306     emulateDrawingOneFrame(root.get());
307
308     // Since the child layer is square, rotation by 45 degrees about the center should
309     // increase the size of the expected rect by sqrt(2), centered around (100, 100). The
310     // old exposed region should be fully contained in the new region.
311     double expectedWidth = 30.0 * sqrt(2.0);
312     double expectedPosition = 100.0 - 0.5 * expectedWidth;
313     FloatRect expectedRect(expectedPosition, expectedPosition, expectedWidth, expectedWidth);
314     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
315     EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
316 }
317
318 TEST_F(CCDamageTrackerTest, verifyDamageForAddingAndRemovingLayer)
319 {
320     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
321     CCLayerImpl* child1 = root->children()[0].get();
322
323     {
324         OwnPtr<CCLayerImpl> child2 = CCLayerImpl::create(3);
325         child2->setPosition(FloatPoint(400, 380));
326         child2->setAnchorPoint(FloatPoint::zero());
327         child2->setBounds(IntSize(6, 8));
328         child2->setDrawsContent(true);
329         root->addChild(child2.release());
330     }
331
332     // CASE 1: Adding a new layer should cause the appropriate damage.
333     //
334     emulateDrawingOneFrame(root.get());
335
336     // Sanity check - all 3 layers should be on the same render surface; render surfaces are tested elsewhere.
337     ASSERT_EQ(3u, root->renderSurface()->layerList().size());
338
339     FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
340     EXPECT_FLOAT_RECT_EQ(FloatRect(400, 380, 6, 8), rootDamageRect);
341
342     // CASE 2: If the layer is removed, its entire old layer becomes exposed, not just the
343     //         last update rect.
344
345     // Advance one frame without damage so that we know the damage rect is not leftover from the previous case.
346     emulateDrawingOneFrame(root.get());
347     EXPECT_TRUE(root->renderSurface()->damageTracker()->currentDamageRect().isEmpty());
348
349     // Then, test removing child1.
350     child1->removeFromParent();
351     emulateDrawingOneFrame(root.get());
352     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
353     EXPECT_FLOAT_RECT_EQ(FloatRect(100, 100, 30, 30), rootDamageRect);
354 }
355
356 TEST_F(CCDamageTrackerTest, verifyDamageForNewUnchangedLayer)
357 {
358     // If child2 is added to the layer tree, but it doesn't have any explicit damage of
359     // its own, it should still indeed damage the target surface.
360
361     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
362
363     {
364         OwnPtr<CCLayerImpl> child2 = CCLayerImpl::create(3);
365         child2->setPosition(FloatPoint(400, 380));
366         child2->setAnchorPoint(FloatPoint::zero());
367         child2->setBounds(IntSize(6, 8));
368         child2->setDrawsContent(true);
369         child2->resetAllChangeTrackingForSubtree();
370         // Sanity check the initial conditions of the test, if these asserts trigger, it
371         // means the test no longer actually covers the intended scenario.
372         ASSERT_FALSE(child2->layerPropertyChanged());
373         ASSERT_TRUE(child2->updateRect().isEmpty());
374         root->addChild(child2.release());
375     }
376
377     emulateDrawingOneFrame(root.get());
378
379     // Sanity check - all 3 layers should be on the same render surface; render surfaces are tested elsewhere.
380     ASSERT_EQ(3u, root->renderSurface()->layerList().size());
381
382     FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
383     EXPECT_FLOAT_RECT_EQ(FloatRect(400, 380, 6, 8), rootDamageRect);
384 }
385
386 TEST_F(CCDamageTrackerTest, verifyDamageForMultipleLayers)
387 {
388     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
389     CCLayerImpl* child1 = root->children()[0].get();
390
391     {
392         OwnPtr<CCLayerImpl> child2 = CCLayerImpl::create(3);
393         child2->setPosition(FloatPoint(400, 380));
394         child2->setAnchorPoint(FloatPoint::zero());
395         child2->setBounds(IntSize(6, 8));
396         child2->setDrawsContent(true);
397         root->addChild(child2.release());
398     }
399     CCLayerImpl* child2 = root->children()[1].get();
400
401     // In this test we don't want the above tree manipulation to be considered part of the same frame.
402     emulateDrawingOneFrame(root.get());
403
404     // Damaging two layers simultaneously should cause combined damage.
405     // - child1 update rect in surface space: FloatRect(100, 100, 1, 2);
406     // - child2 update rect in surface space: FloatRect(400, 380, 3, 4);
407     child1->setUpdateRect(FloatRect(0, 0, 1, 2));
408     child2->setUpdateRect(FloatRect(0, 0, 3, 4));
409     emulateDrawingOneFrame(root.get());
410     FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
411     EXPECT_FLOAT_RECT_EQ(FloatRect(100, 100, 303, 284), rootDamageRect);
412 }
413
414 TEST_F(CCDamageTrackerTest, verifyDamageForNestedSurfaces)
415 {
416     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
417     CCLayerImpl* child1 = root->children()[0].get();
418     CCLayerImpl* child2 = root->children()[1].get();
419     CCLayerImpl* grandChild1 = root->children()[0]->children()[0].get();
420     FloatRect childDamageRect;
421     FloatRect rootDamageRect;
422
423     // CASE 1: Damage to a descendant surface should propagate properly to ancestor surface.
424     //
425     grandChild1->setOpacity(0.5);
426     emulateDrawingOneFrame(root.get());
427     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
428     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
429     EXPECT_FLOAT_RECT_EQ(FloatRect(200, 200, 6, 8), childDamageRect);
430     EXPECT_FLOAT_RECT_EQ(FloatRect(300, 300, 6, 8), rootDamageRect);
431
432     // CASE 2: Same as previous case, but with additional damage elsewhere that should be properly unioned.
433     // - child1 surface damage in root surface space: FloatRect(300, 300, 6, 8);
434     // - child2 damage in root surface space: FloatRect(11, 11, 18, 18);
435     grandChild1->setOpacity(0.7);
436     child2->setOpacity(0.7);
437     emulateDrawingOneFrame(root.get());
438     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
439     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
440     EXPECT_FLOAT_RECT_EQ(FloatRect(200, 200, 6, 8), childDamageRect);
441     EXPECT_FLOAT_RECT_EQ(FloatRect(11, 11, 295, 297), rootDamageRect);
442 }
443
444 TEST_F(CCDamageTrackerTest, verifyDamageForSurfaceChangeFromDescendantLayer)
445 {
446     // If descendant layer changes and affects the content bounds of the render surface,
447     // then the entire descendant surface should be damaged, and it should damage its
448     // ancestor surface with the old and new surface regions.
449
450     // This is a tricky case, since only the first grandChild changes, but the entire
451     // surface should be marked dirty.
452
453     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
454     CCLayerImpl* child1 = root->children()[0].get();
455     CCLayerImpl* grandChild1 = root->children()[0]->children()[0].get();
456     FloatRect childDamageRect;
457     FloatRect rootDamageRect;
458
459     grandChild1->setPosition(FloatPoint(195, 205));
460     emulateDrawingOneFrame(root.get());
461     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
462     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
463
464     // The new surface bounds should be damaged entirely, even though only one of the layers changed.
465     EXPECT_FLOAT_RECT_EQ(FloatRect(190, 190, 11, 23), childDamageRect);
466
467     // Damage to the root surface should be the union of child1's *entire* render surface
468     // (in target space), and its old exposed area (also in target space).
469     EXPECT_FLOAT_RECT_EQ(FloatRect(290, 290, 16, 23), rootDamageRect);
470 }
471
472 TEST_F(CCDamageTrackerTest, verifyDamageForSurfaceChangeFromAncestorLayer)
473 {
474     // An ancestor/owning layer changes that affects the position/transform of the render
475     // surface. Note that in this case, the layerPropertyChanged flag already propagates
476     // to the subtree (tested in CCLayerImpltest), which damages the entire child1
477     // surface, but the damage tracker still needs the correct logic to compute the
478     // exposed region on the root surface.
479
480     // FIXME: the expectations of this test case should change when we add support for a
481     //        unique scissorRect per renderSurface. In that case, the child1 surface
482     //        should be completely unchanged, since we are only transforming it, while the
483     //        root surface would be damaged appropriately.
484
485     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
486     CCLayerImpl* child1 = root->children()[0].get();
487     FloatRect childDamageRect;
488     FloatRect rootDamageRect;
489
490     child1->setPosition(FloatPoint(50, 50));
491     emulateDrawingOneFrame(root.get());
492     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
493     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
494
495     // The new surface bounds should be damaged entirely.
496     EXPECT_FLOAT_RECT_EQ(FloatRect(190, 190, 16, 18), childDamageRect);
497
498     // The entire child1 surface and the old exposed child1 surface should damage the root surface.
499     //  - old child1 surface in target space: FloatRect(290, 290, 16, 18)
500     //  - new child1 surface in target space: FloatRect(240, 240, 16, 18)
501     EXPECT_FLOAT_RECT_EQ(FloatRect(240, 240, 66, 68), rootDamageRect);
502 }
503
504 TEST_F(CCDamageTrackerTest, verifyDamageForAddingAndRemovingRenderSurfaces)
505 {
506     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
507     CCLayerImpl* child1 = root->children()[0].get();
508     FloatRect childDamageRect;
509     FloatRect rootDamageRect;
510
511     // CASE 1: If a descendant surface disappears, its entire old area becomes exposed.
512     //
513     child1->setOpacity(1);
514     emulateDrawingOneFrame(root.get());
515
516     // Sanity check that there is only one surface now.
517     ASSERT_FALSE(child1->renderSurface());
518     ASSERT_EQ(4u, root->renderSurface()->layerList().size());
519
520     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
521     EXPECT_FLOAT_RECT_EQ(FloatRect(290, 290, 16, 18), rootDamageRect);
522
523     // CASE 2: If a descendant surface appears, its entire old area becomes exposed.
524
525     // Cycle one frame of no change, just to sanity check that the next rect is not because of the old damage state.
526     emulateDrawingOneFrame(root.get());
527     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
528     EXPECT_TRUE(rootDamageRect.isEmpty());
529
530     // Then change the tree so that the render surface is added back.
531     child1->setOpacity(0.5);
532     emulateDrawingOneFrame(root.get());
533
534     // Sanity check that there is a new surface now.
535     ASSERT_TRUE(child1->renderSurface());
536     EXPECT_EQ(3u, root->renderSurface()->layerList().size());
537     EXPECT_EQ(2u, child1->renderSurface()->layerList().size());
538
539     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
540     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
541     EXPECT_FLOAT_RECT_EQ(FloatRect(190, 190, 16, 18), childDamageRect);
542     EXPECT_FLOAT_RECT_EQ(FloatRect(290, 290, 16, 18), rootDamageRect);
543 }
544
545 TEST_F(CCDamageTrackerTest, verifyNoDamageWhenNothingChanged)
546 {
547     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
548     CCLayerImpl* child1 = root->children()[0].get();
549     FloatRect childDamageRect;
550     FloatRect rootDamageRect;
551
552     // CASE 1: If nothing changes, the damage rect should be empty.
553     //
554     emulateDrawingOneFrame(root.get());
555     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
556     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
557     EXPECT_TRUE(childDamageRect.isEmpty());
558     EXPECT_TRUE(rootDamageRect.isEmpty());
559
560     // CASE 2: If nothing changes twice in a row, the damage rect should still be empty.
561     //
562     emulateDrawingOneFrame(root.get());
563     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
564     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
565     EXPECT_TRUE(childDamageRect.isEmpty());
566     EXPECT_TRUE(rootDamageRect.isEmpty());
567 }
568
569 TEST_F(CCDamageTrackerTest, verifyNoDamageForUpdateRectThatDoesNotDrawContent)
570 {
571     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
572     CCLayerImpl* child1 = root->children()[0].get();
573     FloatRect childDamageRect;
574     FloatRect rootDamageRect;
575
576     // In our specific tree, the update rect of child1 should not cause any damage to any
577     // surface because it does not actually draw content.
578     child1->setUpdateRect(FloatRect(0, 0, 1, 2));
579     emulateDrawingOneFrame(root.get());
580     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
581     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
582     EXPECT_TRUE(childDamageRect.isEmpty());
583     EXPECT_TRUE(rootDamageRect.isEmpty());
584 }
585
586 TEST_F(CCDamageTrackerTest, verifyDamageForReplica)
587 {
588     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
589     CCLayerImpl* child1 = root->children()[0].get();
590     CCLayerImpl* grandChild1 = child1->children()[0].get();
591     CCLayerImpl* grandChild2 = child1->children()[1].get();
592
593     // Damage on a surface that has a reflection should cause the target surface to
594     // receive the surface's damage and the surface's reflected damage.
595
596     // For this test case, we modify grandChild2, and add grandChild3 to extend the bounds
597     // of child1's surface. This way, we can test reflection changes without changing
598     // contentBounds of the surface.
599     grandChild2->setPosition(FloatPoint(180, 180));
600     {
601         OwnPtr<CCLayerImpl> grandChild3 = CCLayerImpl::create(6);
602         grandChild3->setPosition(FloatPoint(240, 240));
603         grandChild3->setAnchorPoint(FloatPoint::zero());
604         grandChild3->setBounds(IntSize(10, 10));
605         grandChild3->setDrawsContent(true);
606         child1->addChild(grandChild3.release());
607     }
608     child1->setOpacity(0.5);
609     emulateDrawingOneFrame(root.get());
610
611     // CASE 1: adding a reflection about the left edge of grandChild1.
612     //
613     {
614         OwnPtr<CCLayerImpl> grandChild1Replica = CCLayerImpl::create(7);
615         grandChild1Replica->setPosition(FloatPoint::zero());
616         grandChild1Replica->setAnchorPoint(FloatPoint::zero());
617         TransformationMatrix reflection;
618         reflection.scale3d(-1.0, 1.0, 1.0);
619         grandChild1Replica->setTransform(reflection);
620         grandChild1->setReplicaLayer(grandChild1Replica.release());
621     }
622     emulateDrawingOneFrame(root.get());
623
624     FloatRect grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
625     FloatRect childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
626     FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
627
628     // The grandChild surface damage should not include its own replica. The child
629     // surface damage should include the normal and replica surfaces.
630     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 6, 8), grandChildDamageRect);
631     EXPECT_FLOAT_RECT_EQ(FloatRect(194, 200, 12, 8), childDamageRect);
632     EXPECT_FLOAT_RECT_EQ(FloatRect(294, 300, 12, 8), rootDamageRect);
633
634     // CASE 2: moving the descendant surface should cause both the original and reflected
635     //         areas to be damaged on the target.
636     IntRect oldContentRect = child1->renderSurface()->contentRect();
637     grandChild1->setPosition(FloatPoint(195.0, 205.0));
638     emulateDrawingOneFrame(root.get());
639     ASSERT_EQ(oldContentRect.width(), child1->renderSurface()->contentRect().width());
640     ASSERT_EQ(oldContentRect.height(), child1->renderSurface()->contentRect().height());
641
642     grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
643     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
644     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
645
646     // The child surface damage should include normal and replica surfaces for both old and new locations.
647     //  - old location in target space: FloatRect(194, 200, 12, 8)
648     //  - new location in target space: FloatRect(189, 205, 12, 8)
649     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 6, 8), grandChildDamageRect);
650     EXPECT_FLOAT_RECT_EQ(FloatRect(189, 200, 17, 13), childDamageRect);
651     EXPECT_FLOAT_RECT_EQ(FloatRect(289, 300, 17, 13), rootDamageRect);
652
653     // CASE 3: removing the reflection should cause the entire region including reflection
654     //         to damage the target surface.
655     grandChild1->setReplicaLayer(nullptr);
656     emulateDrawingOneFrame(root.get());
657     ASSERT_EQ(oldContentRect.width(), child1->renderSurface()->contentRect().width());
658     ASSERT_EQ(oldContentRect.height(), child1->renderSurface()->contentRect().height());
659
660     EXPECT_FALSE(grandChild1->renderSurface());
661     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
662     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
663
664     EXPECT_FLOAT_RECT_EQ(FloatRect(189, 205, 12, 8), childDamageRect);
665     EXPECT_FLOAT_RECT_EQ(FloatRect(289, 305, 12, 8), rootDamageRect);
666 }
667
668 TEST_F(CCDamageTrackerTest, verifyDamageForMask)
669 {
670     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
671     CCLayerImpl* child = root->children()[0].get();
672
673     // In the current implementation of the damage tracker, changes to mask layers should
674     // damage the entire corresponding surface.
675
676     // Set up the mask layer.
677     {
678         OwnPtr<CCLayerImpl> maskLayer = CCLayerImpl::create(3);
679         maskLayer->setPosition(child->position());
680         maskLayer->setAnchorPoint(FloatPoint::zero());
681         maskLayer->setBounds(child->bounds());
682         child->setMaskLayer(maskLayer.release());
683     }
684     CCLayerImpl* maskLayer = child->maskLayer();
685
686     // Add opacity and a grandChild so that the render surface persists even after we remove the mask.
687     child->setOpacity(0.5);
688     {
689         OwnPtr<CCLayerImpl> grandChild = CCLayerImpl::create(4);
690         grandChild->setPosition(FloatPoint(2.0, 2.0));
691         grandChild->setAnchorPoint(FloatPoint::zero());
692         grandChild->setBounds(IntSize(2, 2));
693         grandChild->setDrawsContent(true);
694         child->addChild(grandChild.release());
695     }
696     emulateDrawingOneFrame(root.get());
697
698     // Sanity check that a new surface was created for the child.
699     ASSERT_TRUE(child->renderSurface());
700
701     // CASE 1: the updateRect on a mask layer should damage the entire target surface.
702     //
703     maskLayer->setUpdateRect(FloatRect(1, 2, 3, 4));
704     emulateDrawingOneFrame(root.get());
705     FloatRect childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
706     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 30, 30), childDamageRect);
707
708     // CASE 2: a property change on the mask layer should damage the entire target surface.
709     //
710
711     // Advance one frame without damage so that we know the damage rect is not leftover from the previous case.
712     emulateDrawingOneFrame(root.get());
713     childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
714     EXPECT_TRUE(childDamageRect.isEmpty());
715
716     // Then test the property change.
717     maskLayer->setOpacity(0.5);
718     emulateDrawingOneFrame(root.get());
719     childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
720     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 30, 30), childDamageRect);
721
722     // CASE 3: removing the mask also damages the entire target surface.
723     //
724
725     // Advance one frame without damage so that we know the damage rect is not leftover from the previous case.
726     emulateDrawingOneFrame(root.get());
727     childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
728     EXPECT_TRUE(childDamageRect.isEmpty());
729
730     // Then test mask removal.
731     child->setMaskLayer(nullptr);
732     ASSERT_TRUE(child->layerPropertyChanged());
733     emulateDrawingOneFrame(root.get());
734
735     // Sanity check that a render surface still exists.
736     ASSERT_TRUE(child->renderSurface());
737
738     childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
739     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 30, 30), childDamageRect);
740 }
741
742 TEST_F(CCDamageTrackerTest, verifyDamageForReplicaMask)
743 {
744     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
745     CCLayerImpl* child1 = root->children()[0].get();
746     CCLayerImpl* grandChild1 = child1->children()[0].get();
747
748     // Changes to a replica's mask should not damage the original surface, because it is
749     // not masked. But it does damage the ancestor target surface.
750
751     // Create a reflection about the left edge of grandChild1.
752     {
753         OwnPtr<CCLayerImpl> grandChild1Replica = CCLayerImpl::create(6);
754         grandChild1Replica->setPosition(FloatPoint::zero());
755         grandChild1Replica->setAnchorPoint(FloatPoint::zero());
756         TransformationMatrix reflection;
757         reflection.scale3d(-1.0, 1.0, 1.0);
758         grandChild1Replica->setTransform(reflection);
759         grandChild1->setReplicaLayer(grandChild1Replica.release());
760     }
761     CCLayerImpl* grandChild1Replica = grandChild1->replicaLayer();
762
763     // Set up the mask layer on the replica layer
764     {
765         OwnPtr<CCLayerImpl> replicaMaskLayer = CCLayerImpl::create(7);
766         replicaMaskLayer->setPosition(FloatPoint::zero());
767         replicaMaskLayer->setAnchorPoint(FloatPoint::zero());
768         replicaMaskLayer->setBounds(grandChild1->bounds());
769         grandChild1Replica->setMaskLayer(replicaMaskLayer.release());
770     }
771     CCLayerImpl* replicaMaskLayer = grandChild1Replica->maskLayer();
772
773     emulateDrawingOneFrame(root.get());
774
775     // Sanity check that the appropriate render surfaces were created
776     ASSERT_TRUE(grandChild1->renderSurface());
777
778     // CASE 1: a property change on the mask should damage only the reflected region on the target surface.
779     replicaMaskLayer->setOpacity(0.6);
780     emulateDrawingOneFrame(root.get());
781
782     FloatRect grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
783     FloatRect childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
784
785     EXPECT_TRUE(grandChildDamageRect.isEmpty());
786     EXPECT_FLOAT_RECT_EQ(FloatRect(194, 200, 6, 8), childDamageRect);
787
788     // CASE 2: removing the replica mask damages only the reflected region on the target surface.
789     //
790     grandChild1Replica->setMaskLayer(nullptr);
791     emulateDrawingOneFrame(root.get());
792
793     grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
794     childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
795
796     EXPECT_TRUE(grandChildDamageRect.isEmpty());
797     EXPECT_FLOAT_RECT_EQ(FloatRect(194, 200, 6, 8), childDamageRect);
798 }
799
800 TEST_F(CCDamageTrackerTest, verifyDamageForReplicaMaskWithAnchor)
801 {
802     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
803     CCLayerImpl* child1 = root->children()[0].get();
804     CCLayerImpl* grandChild1 = child1->children()[0].get();
805
806     // Verify that the correct replicaOriginTransform is used for the replicaMask; the
807     // incorrect old code did not actually correctly account for the anchor for the
808     // replica.
809     //
810     // Create a reflection about the left edge, but the anchor point is shifted all the
811     // way to the right. this case the reflection should be directly on top (but
812     // horizontally flipped) of grandChild1.
813
814     grandChild1->setAnchorPoint(FloatPoint(1.0, 0.0)); // This is the anchor being tested.
815
816     {
817         OwnPtr<CCLayerImpl> grandChild1Replica = CCLayerImpl::create(6);
818         grandChild1Replica->setPosition(FloatPoint::zero());
819         grandChild1Replica->setAnchorPoint(FloatPoint::zero()); // note, this is not the anchor being tested.
820         TransformationMatrix reflection;
821         reflection.scale3d(-1.0, 1.0, 1.0);
822         grandChild1Replica->setTransform(reflection);
823         grandChild1->setReplicaLayer(grandChild1Replica.release());
824     }
825     CCLayerImpl* grandChild1Replica = grandChild1->replicaLayer();
826
827     // Set up the mask layer on the replica layer
828     {
829         OwnPtr<CCLayerImpl> replicaMaskLayer = CCLayerImpl::create(7);
830         replicaMaskLayer->setPosition(FloatPoint::zero());
831         replicaMaskLayer->setAnchorPoint(FloatPoint::zero()); // note, this is not the anchor being tested.
832         replicaMaskLayer->setBounds(grandChild1->bounds());
833         grandChild1Replica->setMaskLayer(replicaMaskLayer.release());
834     }
835     CCLayerImpl* replicaMaskLayer = grandChild1Replica->maskLayer();
836
837     emulateDrawingOneFrame(root.get());
838
839     // Sanity check that the appropriate render surfaces were created
840     ASSERT_TRUE(grandChild1->renderSurface());
841
842     // A property change on the replicaMask should damage the reflected region on the target surface.
843     replicaMaskLayer->setOpacity(0.6);
844     emulateDrawingOneFrame(root.get());
845
846     FloatRect childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
847     EXPECT_FLOAT_RECT_EQ(FloatRect(194, 200, 6, 8), childDamageRect);
848 }
849
850 TEST_F(CCDamageTrackerTest, verifyDamageWhenForcedFullDamage)
851 {
852     OwnPtr<CCLayerImpl> root = createAndSetUpTestTreeWithOneSurface();
853     CCLayerImpl* child = root->children()[0].get();
854
855     // Case 1: This test ensures that when the tracker is forced to have full damage, that
856     //         it takes priority over any other partial damage.
857     //
858     child->setUpdateRect(FloatRect(10, 11, 12, 13));
859     root->renderSurface()->damageTracker()->forceFullDamageNextUpdate();
860     emulateDrawingOneFrame(root.get());
861     FloatRect rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
862     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 500, 500), rootDamageRect);
863
864     // Case 2: An additional sanity check that forcing full damage works even when nothing
865     //         on the layer tree changed.
866     //
867     root->renderSurface()->damageTracker()->forceFullDamageNextUpdate();
868     emulateDrawingOneFrame(root.get());
869     rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
870     EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 500, 500), rootDamageRect);
871 }
872
873 TEST_F(CCDamageTrackerTest, verifyDamageForEmptyLayerList)
874 {
875     // Though it should never happen, its a good idea to verify that the damage tracker
876     // does not crash when it receives an empty layerList.
877
878     OwnPtr<CCLayerImpl> root = CCLayerImpl::create(1);
879     root->createRenderSurface();
880
881     ASSERT_TRUE(root->renderSurface() == root->targetRenderSurface());
882     CCRenderSurface* targetSurface = root->renderSurface();
883     targetSurface->clearLayerList();
884     targetSurface->damageTracker()->updateDamageTrackingState(targetSurface->layerList(), targetSurface->owningLayerId(), false, IntRect(), 0);
885
886     FloatRect damageRect = targetSurface->damageTracker()->currentDamageRect();
887     EXPECT_TRUE(damageRect.isEmpty());
888 }
889
890 } // namespace