Add Sad Tab resources to the iOS build.
[chromium-blink-merge.git] / cc / damage_tracker_unittest.cc
blob8f741bcb5b0fbcfa70cb69bb2006d06825273675
1 // Copyright 2011 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 "config.h"
7 #include "cc/damage_tracker.h"
9 #include "cc/layer_impl.h"
10 #include "cc/layer_sorter.h"
11 #include "cc/layer_tree_host_common.h"
12 #include "cc/math_util.h"
13 #include "cc/single_thread_proxy.h"
14 #include "cc/test/geometry_test_utils.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
17 #include <public/WebFilterOperation.h>
18 #include <public/WebFilterOperations.h>
20 using namespace cc;
21 using namespace WebKit;
22 using namespace WebKitTests;
24 namespace {
26 void executeCalculateDrawTransformsAndVisibility(LayerImpl* root, std::vector<LayerImpl*>& renderSurfaceLayerList)
28 LayerSorter layerSorter;
29 int dummyMaxTextureSize = 512;
31 // Sanity check: The test itself should create the root layer's render surface, so
32 // that the surface (and its damage tracker) can persist across multiple
33 // calls to this function.
34 ASSERT_TRUE(root->renderSurface());
35 ASSERT_FALSE(renderSurfaceLayerList.size());
37 LayerTreeHostCommon::calculateDrawTransforms(root, root->bounds(), 1, 1, &layerSorter, dummyMaxTextureSize, renderSurfaceLayerList);
40 void clearDamageForAllSurfaces(LayerImpl* layer)
42 if (layer->renderSurface())
43 layer->renderSurface()->damageTracker()->didDrawDamagedArea();
45 // Recursively clear damage for any existing surface.
46 for (size_t i = 0; i < layer->children().size(); ++i)
47 clearDamageForAllSurfaces(layer->children()[i]);
50 void emulateDrawingOneFrame(LayerImpl* root)
52 // This emulates only the steps that are relevant to testing the damage tracker:
53 // 1. computing the render passes and layerlists
54 // 2. updating all damage trackers in the correct order
55 // 3. resetting all updateRects and propertyChanged flags for all layers and surfaces.
57 std::vector<LayerImpl*> renderSurfaceLayerList;
58 executeCalculateDrawTransformsAndVisibility(root, renderSurfaceLayerList);
60 // Iterate back-to-front, so that damage correctly propagates from descendant surfaces to ancestors.
61 for (int i = renderSurfaceLayerList.size() - 1; i >= 0; --i) {
62 RenderSurfaceImpl* targetSurface = renderSurfaceLayerList[i]->renderSurface();
63 targetSurface->damageTracker()->updateDamageTrackingState(targetSurface->layerList(), targetSurface->owningLayerId(), targetSurface->surfacePropertyChangedOnlyFromDescendant(), targetSurface->contentRect(), renderSurfaceLayerList[i]->maskLayer(), renderSurfaceLayerList[i]->filters(), renderSurfaceLayerList[i]->filter());
66 root->resetAllChangeTrackingForSubtree();
69 scoped_ptr<LayerImpl> createTestTreeWithOneSurface()
71 scoped_ptr<LayerImpl> root = LayerImpl::create(1);
72 scoped_ptr<LayerImpl> child = LayerImpl::create(2);
74 root->setPosition(gfx::PointF());
75 root->setAnchorPoint(gfx::PointF());
76 root->setBounds(gfx::Size(500, 500));
77 root->setContentBounds(gfx::Size(500, 500));
78 root->setDrawsContent(true);
79 root->createRenderSurface();
80 root->renderSurface()->setContentRect(gfx::Rect(gfx::Point(), gfx::Size(500, 500)));
82 child->setPosition(gfx::PointF(100, 100));
83 child->setAnchorPoint(gfx::PointF());
84 child->setBounds(gfx::Size(30, 30));
85 child->setContentBounds(gfx::Size(30, 30));
86 child->setDrawsContent(true);
87 root->addChild(child.Pass());
89 return root.Pass();
92 scoped_ptr<LayerImpl> createTestTreeWithTwoSurfaces()
94 // This test tree has two render surfaces: one for the root, and one for
95 // child1. Additionally, the root has a second child layer, and child1 has two
96 // children of its own.
98 scoped_ptr<LayerImpl> root = LayerImpl::create(1);
99 scoped_ptr<LayerImpl> child1 = LayerImpl::create(2);
100 scoped_ptr<LayerImpl> child2 = LayerImpl::create(3);
101 scoped_ptr<LayerImpl> grandChild1 = LayerImpl::create(4);
102 scoped_ptr<LayerImpl> grandChild2 = LayerImpl::create(5);
104 root->setPosition(gfx::PointF());
105 root->setAnchorPoint(gfx::PointF());
106 root->setBounds(gfx::Size(500, 500));
107 root->setContentBounds(gfx::Size(500, 500));
108 root->setDrawsContent(true);
109 root->createRenderSurface();
110 root->renderSurface()->setContentRect(gfx::Rect(gfx::Point(), gfx::Size(500, 500)));
112 child1->setPosition(gfx::PointF(100, 100));
113 child1->setAnchorPoint(gfx::PointF());
114 child1->setBounds(gfx::Size(30, 30));
115 child1->setContentBounds(gfx::Size(30, 30));
116 child1->setOpacity(0.5); // with a child that drawsContent, this will cause the layer to create its own renderSurface.
117 child1->setDrawsContent(false); // this layer does not draw, but is intended to create its own renderSurface.
119 child2->setPosition(gfx::PointF(11, 11));
120 child2->setAnchorPoint(gfx::PointF());
121 child2->setBounds(gfx::Size(18, 18));
122 child2->setContentBounds(gfx::Size(18, 18));
123 child2->setDrawsContent(true);
125 grandChild1->setPosition(gfx::PointF(200, 200));
126 grandChild1->setAnchorPoint(gfx::PointF());
127 grandChild1->setBounds(gfx::Size(6, 8));
128 grandChild1->setContentBounds(gfx::Size(6, 8));
129 grandChild1->setDrawsContent(true);
131 grandChild2->setPosition(gfx::PointF(190, 190));
132 grandChild2->setAnchorPoint(gfx::PointF());
133 grandChild2->setBounds(gfx::Size(6, 8));
134 grandChild2->setContentBounds(gfx::Size(6, 8));
135 grandChild2->setDrawsContent(true);
137 child1->addChild(grandChild1.Pass());
138 child1->addChild(grandChild2.Pass());
139 root->addChild(child1.Pass());
140 root->addChild(child2.Pass());
142 return root.Pass();
145 scoped_ptr<LayerImpl> createAndSetUpTestTreeWithOneSurface()
147 scoped_ptr<LayerImpl> root = createTestTreeWithOneSurface();
149 // Setup includes going past the first frame which always damages everything, so
150 // that we can actually perform specific tests.
151 emulateDrawingOneFrame(root.get());
153 return root.Pass();
156 scoped_ptr<LayerImpl> createAndSetUpTestTreeWithTwoSurfaces()
158 scoped_ptr<LayerImpl> root = createTestTreeWithTwoSurfaces();
160 // Setup includes going past the first frame which always damages everything, so
161 // that we can actually perform specific tests.
162 emulateDrawingOneFrame(root.get());
164 return root.Pass();
167 class DamageTrackerTest : public testing::Test {
170 TEST_F(DamageTrackerTest, sanityCheckTestTreeWithOneSurface)
172 // Sanity check that the simple test tree will actually produce the expected render
173 // surfaces and layer lists.
175 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
177 EXPECT_EQ(2u, root->renderSurface()->layerList().size());
178 EXPECT_EQ(1, root->renderSurface()->layerList()[0]->id());
179 EXPECT_EQ(2, root->renderSurface()->layerList()[1]->id());
181 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
182 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 500, 500), rootDamageRect);
185 TEST_F(DamageTrackerTest, sanityCheckTestTreeWithTwoSurfaces)
187 // Sanity check that the complex test tree will actually produce the expected render
188 // surfaces and layer lists.
190 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
192 LayerImpl* child1 = root->children()[0];
193 LayerImpl* child2 = root->children()[1];
194 gfx::RectF childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
195 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
197 ASSERT_TRUE(child1->renderSurface());
198 EXPECT_FALSE(child2->renderSurface());
199 EXPECT_EQ(3u, root->renderSurface()->layerList().size());
200 EXPECT_EQ(2u, child1->renderSurface()->layerList().size());
202 // The render surface for child1 only has a contentRect that encloses grandChild1 and grandChild2, because child1 does not draw content.
203 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 16, 18), childDamageRect);
204 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 500, 500), rootDamageRect);
207 TEST_F(DamageTrackerTest, verifyDamageForUpdateRects)
209 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
210 LayerImpl* child = root->children()[0];
212 // CASE 1: Setting the update rect should cause the corresponding damage to the surface.
214 clearDamageForAllSurfaces(root.get());
215 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
216 emulateDrawingOneFrame(root.get());
218 // Damage position on the surface should be: position of updateRect (10, 11) relative to the child (100, 100).
219 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
220 EXPECT_FLOAT_RECT_EQ(gfx::RectF(110, 111, 12, 13), rootDamageRect);
222 // CASE 2: The same update rect twice in a row still produces the same damage.
224 clearDamageForAllSurfaces(root.get());
225 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
226 emulateDrawingOneFrame(root.get());
227 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
228 EXPECT_FLOAT_RECT_EQ(gfx::RectF(110, 111, 12, 13), rootDamageRect);
230 // CASE 3: Setting a different update rect should cause damage on the new update region, but no additional exposed old region.
232 clearDamageForAllSurfaces(root.get());
233 child->setUpdateRect(gfx::RectF(20, 25, 1, 2));
234 emulateDrawingOneFrame(root.get());
236 // Damage position on the surface should be: position of updateRect (20, 25) relative to the child (100, 100).
237 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
238 EXPECT_FLOAT_RECT_EQ(gfx::RectF(120, 125, 1, 2), rootDamageRect);
241 TEST_F(DamageTrackerTest, verifyDamageForPropertyChanges)
243 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
244 LayerImpl* child = root->children()[0];
246 // CASE 1: The layer's property changed flag takes priority over update rect.
248 clearDamageForAllSurfaces(root.get());
249 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
250 child->setOpacity(0.5);
251 emulateDrawingOneFrame(root.get());
253 // Sanity check - we should not have accidentally created a separate render surface for the translucent layer.
254 ASSERT_FALSE(child->renderSurface());
255 ASSERT_EQ(2u, root->renderSurface()->layerList().size());
257 // Damage should be the entire child layer in targetSurface space.
258 gfx::RectF expectedRect = gfx::RectF(100, 100, 30, 30);
259 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
260 EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
262 // CASE 2: If a layer moves due to property change, it damages both the new location
263 // and the old (exposed) location. The old location is the entire old layer,
264 // not just the updateRect.
266 // Cycle one frame of no change, just to sanity check that the next rect is not because of the old damage state.
267 clearDamageForAllSurfaces(root.get());
268 emulateDrawingOneFrame(root.get());
269 EXPECT_TRUE(root->renderSurface()->damageTracker()->currentDamageRect().IsEmpty());
271 // Then, test the actual layer movement.
272 clearDamageForAllSurfaces(root.get());
273 child->setPosition(gfx::PointF(200, 230));
274 emulateDrawingOneFrame(root.get());
276 // Expect damage to be the combination of the previous one and the new one.
277 expectedRect.Union(gfx::RectF(200, 230, 30, 30));
278 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
279 EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
282 TEST_F(DamageTrackerTest, verifyDamageForTransformedLayer)
284 // If a layer is transformed, the damage rect should still enclose the entire
285 // transformed layer.
287 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
288 LayerImpl* child = root->children()[0];
290 WebTransformationMatrix rotation;
291 rotation.rotate(45);
293 clearDamageForAllSurfaces(root.get());
294 child->setAnchorPoint(gfx::PointF(0.5, 0.5));
295 child->setPosition(gfx::PointF(85, 85));
296 emulateDrawingOneFrame(root.get());
298 // Sanity check that the layer actually moved to (85, 85), damaging its old location and new location.
299 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
300 EXPECT_FLOAT_RECT_EQ(gfx::RectF(85, 85, 45, 45), rootDamageRect);
302 // With the anchor on the layer's center, now we can test the rotation more
303 // intuitively, since it applies about the layer's anchor.
304 clearDamageForAllSurfaces(root.get());
305 child->setTransform(rotation);
306 emulateDrawingOneFrame(root.get());
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 * sqrt(2.0);
312 double expectedPosition = 100 - 0.5 * expectedWidth;
313 gfx::RectF expectedRect(expectedPosition, expectedPosition, expectedWidth, expectedWidth);
314 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
315 EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
318 TEST_F(DamageTrackerTest, verifyDamageForPerspectiveClippedLayer)
320 // If a layer has a perspective transform that causes w < 0, then not clipping the
321 // layer can cause an invalid damage rect. This test checks that the w < 0 case is
322 // tracked properly.
324 // The transform is constructed so that if w < 0 clipping is not performed, the
325 // incorrect rect will be very small, specifically: position (500.972504, 498.544617) and size 0.056610 x 2.910767.
326 // Instead, the correctly transformed rect should actually be very huge (i.e. in theory, -infinity on the left),
327 // and positioned so that the right-most bound rect will be approximately 501 units in root surface space.
330 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
331 LayerImpl* child = root->children()[0];
333 WebTransformationMatrix transform;
334 transform.translate3d(500, 500, 0);
335 transform.applyPerspective(1);
336 transform.rotate3d(0, 45, 0);
337 transform.translate3d(-50, -50, 0);
339 // Set up the child
340 child->setPosition(gfx::PointF(0, 0));
341 child->setBounds(gfx::Size(100, 100));
342 child->setContentBounds(gfx::Size(100, 100));
343 child->setTransform(transform);
344 emulateDrawingOneFrame(root.get());
346 // Sanity check that the child layer's bounds would actually get clipped by w < 0,
347 // otherwise this test is not actually testing the intended scenario.
348 gfx::QuadF testQuad(gfx::RectF(gfx::PointF(), gfx::SizeF(100, 100)));
349 bool clipped = false;
350 MathUtil::mapQuad(transform, testQuad, clipped);
351 EXPECT_TRUE(clipped);
353 // Damage the child without moving it.
354 clearDamageForAllSurfaces(root.get());
355 child->setOpacity(0.5);
356 emulateDrawingOneFrame(root.get());
358 // The expected damage should cover the entire root surface (500x500), but we don't
359 // care whether the damage rect was clamped or is larger than the surface for this test.
360 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
361 gfx::RectF damageWeCareAbout = gfx::RectF(gfx::PointF(), gfx::SizeF(500, 500));
362 EXPECT_TRUE(rootDamageRect.Contains(damageWeCareAbout));
365 TEST_F(DamageTrackerTest, verifyDamageForBlurredSurface)
367 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
368 LayerImpl* child = root->children()[0];
370 WebFilterOperations filters;
371 filters.append(WebFilterOperation::createBlurFilter(5));
372 int outsetTop, outsetRight, outsetBottom, outsetLeft;
373 filters.getOutsets(outsetTop, outsetRight, outsetBottom, outsetLeft);
375 // Setting the filter will damage the whole surface.
376 clearDamageForAllSurfaces(root.get());
377 root->setFilters(filters);
378 emulateDrawingOneFrame(root.get());
380 // Setting the update rect should cause the corresponding damage to the surface, blurred based on the size of the blur filter.
381 clearDamageForAllSurfaces(root.get());
382 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
383 emulateDrawingOneFrame(root.get());
385 // Damage position on the surface should be: position of updateRect (10, 11) relative to the child (100, 100), but expanded by the blur outsets.
386 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
387 gfx::RectF expectedDamageRect = gfx::RectF(110, 111, 12, 13);
388 expectedDamageRect.Inset(-outsetLeft, -outsetTop, -outsetRight, -outsetBottom);
389 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
392 TEST_F(DamageTrackerTest, verifyDamageForImageFilter)
394 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
395 LayerImpl* child = root->children()[0];
396 gfx::RectF rootDamageRect, childDamageRect;
398 // Allow us to set damage on child too.
399 child->setDrawsContent(true);
401 SkAutoTUnref<SkImageFilter> filter(new SkBlurImageFilter(SkIntToScalar(2),
402 SkIntToScalar(2)));
403 // Setting the filter will damage the whole surface.
404 clearDamageForAllSurfaces(root.get());
405 child->setFilter(filter);
406 emulateDrawingOneFrame(root.get());
407 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
408 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
409 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 30, 30), rootDamageRect);
410 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
412 // CASE 1: Setting the update rect should damage the whole surface (for now)
413 clearDamageForAllSurfaces(root.get());
414 child->setUpdateRect(gfx::RectF(0, 0, 1, 1));
415 emulateDrawingOneFrame(root.get());
417 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
418 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
419 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 30, 30), rootDamageRect);
420 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
423 TEST_F(DamageTrackerTest, verifyDamageForBackgroundBlurredChild)
425 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
426 LayerImpl* child1 = root->children()[0];
427 LayerImpl* child2 = root->children()[1];
429 // Allow us to set damage on child1 too.
430 child1->setDrawsContent(true);
432 WebFilterOperations filters;
433 filters.append(WebFilterOperation::createBlurFilter(2));
434 int outsetTop, outsetRight, outsetBottom, outsetLeft;
435 filters.getOutsets(outsetTop, outsetRight, outsetBottom, outsetLeft);
437 // Setting the filter will damage the whole surface.
438 clearDamageForAllSurfaces(root.get());
439 child1->setBackgroundFilters(filters);
440 emulateDrawingOneFrame(root.get());
442 // CASE 1: Setting the update rect should cause the corresponding damage to
443 // the surface, blurred based on the size of the child's background blur
444 // filter.
445 clearDamageForAllSurfaces(root.get());
446 root->setUpdateRect(gfx::RectF(297, 297, 2, 2));
447 emulateDrawingOneFrame(root.get());
449 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
450 // Damage position on the surface should be a composition of the damage on the root and on child2.
451 // Damage on the root should be: position of updateRect (297, 297), but expanded by the blur outsets.
452 gfx::RectF expectedDamageRect = gfx::RectF(297, 297, 2, 2);
453 expectedDamageRect.Inset(-outsetLeft, -outsetTop, -outsetRight, -outsetBottom);
454 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
456 // CASE 2: Setting the update rect should cause the corresponding damage to
457 // the surface, blurred based on the size of the child's background blur
458 // filter. Since the damage extends to the right/bottom outside of the
459 // blurred layer, only the left/top should end up expanded.
460 clearDamageForAllSurfaces(root.get());
461 root->setUpdateRect(gfx::RectF(297, 297, 30, 30));
462 emulateDrawingOneFrame(root.get());
464 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
465 // Damage position on the surface should be a composition of the damage on the root and on child2.
466 // Damage on the root should be: position of updateRect (297, 297), but expanded on the left/top
467 // by the blur outsets.
468 expectedDamageRect = gfx::RectF(297, 297, 30, 30);
469 expectedDamageRect.Inset(-outsetLeft, -outsetTop, 0, 0);
470 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
472 // CASE 3: Setting this update rect outside the blurred contentBounds of the blurred
473 // child1 will not cause it to be expanded.
474 clearDamageForAllSurfaces(root.get());
475 root->setUpdateRect(gfx::RectF(30, 30, 2, 2));
476 emulateDrawingOneFrame(root.get());
478 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
479 // Damage on the root should be: position of updateRect (30, 30), not
480 // expanded.
481 expectedDamageRect = gfx::RectF(30, 30, 2, 2);
482 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
484 // CASE 4: Setting this update rect inside the blurred contentBounds but outside the
485 // original contentBounds of the blurred child1 will cause it to be expanded.
486 clearDamageForAllSurfaces(root.get());
487 root->setUpdateRect(gfx::RectF(99, 99, 1, 1));
488 emulateDrawingOneFrame(root.get());
490 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
491 // Damage on the root should be: position of updateRect (99, 99), expanded
492 // by the blurring on child1, but since it is 1 pixel outside the layer, the
493 // expanding should be reduced by 1.
494 expectedDamageRect = gfx::RectF(99, 99, 1, 1);
495 expectedDamageRect.Inset(-outsetLeft + 1, -outsetTop + 1, -outsetRight, -outsetBottom);
496 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
498 // CASE 5: Setting the update rect on child2, which is above child1, will
499 // not get blurred by child1, so it does not need to get expanded.
500 clearDamageForAllSurfaces(root.get());
501 child2->setUpdateRect(gfx::RectF(0, 0, 1, 1));
502 emulateDrawingOneFrame(root.get());
504 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
505 // Damage on child2 should be: position of updateRect offset by the child's position (11, 11), and not expanded by anything.
506 expectedDamageRect = gfx::RectF(11, 11, 1, 1);
507 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
509 // CASE 6: Setting the update rect on child1 will also blur the damage, so
510 // that any pixels needed for the blur are redrawn in the current frame.
511 clearDamageForAllSurfaces(root.get());
512 child1->setUpdateRect(gfx::RectF(0, 0, 1, 1));
513 emulateDrawingOneFrame(root.get());
515 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
516 // Damage on child1 should be: position of updateRect offset by the child's position (100, 100), and expanded by the damage.
517 expectedDamageRect = gfx::RectF(100, 100, 1, 1);
518 expectedDamageRect.Inset(-outsetLeft, -outsetTop, -outsetRight, -outsetBottom);
519 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
522 TEST_F(DamageTrackerTest, verifyDamageForAddingAndRemovingLayer)
524 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
525 LayerImpl* child1 = root->children()[0];
527 // CASE 1: Adding a new layer should cause the appropriate damage.
529 clearDamageForAllSurfaces(root.get());
531 scoped_ptr<LayerImpl> child2 = LayerImpl::create(3);
532 child2->setPosition(gfx::PointF(400, 380));
533 child2->setAnchorPoint(gfx::PointF());
534 child2->setBounds(gfx::Size(6, 8));
535 child2->setContentBounds(gfx::Size(6, 8));
536 child2->setDrawsContent(true);
537 root->addChild(child2.Pass());
539 emulateDrawingOneFrame(root.get());
541 // Sanity check - all 3 layers should be on the same render surface; render surfaces are tested elsewhere.
542 ASSERT_EQ(3u, root->renderSurface()->layerList().size());
544 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
545 EXPECT_FLOAT_RECT_EQ(gfx::RectF(400, 380, 6, 8), rootDamageRect);
547 // CASE 2: If the layer is removed, its entire old layer becomes exposed, not just the
548 // last update rect.
550 // Advance one frame without damage so that we know the damage rect is not leftover from the previous case.
551 clearDamageForAllSurfaces(root.get());
552 emulateDrawingOneFrame(root.get());
553 EXPECT_TRUE(root->renderSurface()->damageTracker()->currentDamageRect().IsEmpty());
555 // Then, test removing child1.
556 child1->removeFromParent();
557 emulateDrawingOneFrame(root.get());
558 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
559 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 30, 30), rootDamageRect);
562 TEST_F(DamageTrackerTest, verifyDamageForNewUnchangedLayer)
564 // If child2 is added to the layer tree, but it doesn't have any explicit damage of
565 // its own, it should still indeed damage the target surface.
567 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
569 clearDamageForAllSurfaces(root.get());
571 scoped_ptr<LayerImpl> child2 = LayerImpl::create(3);
572 child2->setPosition(gfx::PointF(400, 380));
573 child2->setAnchorPoint(gfx::PointF());
574 child2->setBounds(gfx::Size(6, 8));
575 child2->setContentBounds(gfx::Size(6, 8));
576 child2->setDrawsContent(true);
577 child2->resetAllChangeTrackingForSubtree();
578 // Sanity check the initial conditions of the test, if these asserts trigger, it
579 // means the test no longer actually covers the intended scenario.
580 ASSERT_FALSE(child2->layerPropertyChanged());
581 ASSERT_TRUE(child2->updateRect().IsEmpty());
582 root->addChild(child2.Pass());
584 emulateDrawingOneFrame(root.get());
586 // Sanity check - all 3 layers should be on the same render surface; render surfaces are tested elsewhere.
587 ASSERT_EQ(3u, root->renderSurface()->layerList().size());
589 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
590 EXPECT_FLOAT_RECT_EQ(gfx::RectF(400, 380, 6, 8), rootDamageRect);
593 TEST_F(DamageTrackerTest, verifyDamageForMultipleLayers)
595 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
596 LayerImpl* child1 = root->children()[0];
598 // In this test we don't want the above tree manipulation to be considered part of the same frame.
599 clearDamageForAllSurfaces(root.get());
601 scoped_ptr<LayerImpl> child2 = LayerImpl::create(3);
602 child2->setPosition(gfx::PointF(400, 380));
603 child2->setAnchorPoint(gfx::PointF());
604 child2->setBounds(gfx::Size(6, 8));
605 child2->setContentBounds(gfx::Size(6, 8));
606 child2->setDrawsContent(true);
607 root->addChild(child2.Pass());
609 LayerImpl* child2 = root->children()[1];
610 emulateDrawingOneFrame(root.get());
612 // Damaging two layers simultaneously should cause combined damage.
613 // - child1 update rect in surface space: gfx::RectF(100, 100, 1, 2);
614 // - child2 update rect in surface space: gfx::RectF(400, 380, 3, 4);
615 clearDamageForAllSurfaces(root.get());
616 child1->setUpdateRect(gfx::RectF(0, 0, 1, 2));
617 child2->setUpdateRect(gfx::RectF(0, 0, 3, 4));
618 emulateDrawingOneFrame(root.get());
619 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
620 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 303, 284), rootDamageRect);
623 TEST_F(DamageTrackerTest, verifyDamageForNestedSurfaces)
625 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
626 LayerImpl* child1 = root->children()[0];
627 LayerImpl* child2 = root->children()[1];
628 LayerImpl* grandChild1 = root->children()[0]->children()[0];
629 gfx::RectF childDamageRect;
630 gfx::RectF rootDamageRect;
632 // CASE 1: Damage to a descendant surface should propagate properly to ancestor surface.
634 clearDamageForAllSurfaces(root.get());
635 grandChild1->setOpacity(0.5);
636 emulateDrawingOneFrame(root.get());
637 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
638 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
639 EXPECT_FLOAT_RECT_EQ(gfx::RectF(200, 200, 6, 8), childDamageRect);
640 EXPECT_FLOAT_RECT_EQ(gfx::RectF(300, 300, 6, 8), rootDamageRect);
642 // CASE 2: Same as previous case, but with additional damage elsewhere that should be properly unioned.
643 // - child1 surface damage in root surface space: gfx::RectF(300, 300, 6, 8);
644 // - child2 damage in root surface space: gfx::RectF(11, 11, 18, 18);
645 clearDamageForAllSurfaces(root.get());
646 grandChild1->setOpacity(0.7f);
647 child2->setOpacity(0.7f);
648 emulateDrawingOneFrame(root.get());
649 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
650 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
651 EXPECT_FLOAT_RECT_EQ(gfx::RectF(200, 200, 6, 8), childDamageRect);
652 EXPECT_FLOAT_RECT_EQ(gfx::RectF(11, 11, 295, 297), rootDamageRect);
655 TEST_F(DamageTrackerTest, verifyDamageForSurfaceChangeFromDescendantLayer)
657 // If descendant layer changes and affects the content bounds of the render surface,
658 // then the entire descendant surface should be damaged, and it should damage its
659 // ancestor surface with the old and new surface regions.
661 // This is a tricky case, since only the first grandChild changes, but the entire
662 // surface should be marked dirty.
664 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
665 LayerImpl* child1 = root->children()[0];
666 LayerImpl* grandChild1 = root->children()[0]->children()[0];
667 gfx::RectF childDamageRect;
668 gfx::RectF rootDamageRect;
670 clearDamageForAllSurfaces(root.get());
671 grandChild1->setPosition(gfx::PointF(195, 205));
672 emulateDrawingOneFrame(root.get());
673 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
674 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
676 // The new surface bounds should be damaged entirely, even though only one of the layers changed.
677 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 11, 23), childDamageRect);
679 // Damage to the root surface should be the union of child1's *entire* render surface
680 // (in target space), and its old exposed area (also in target space).
681 EXPECT_FLOAT_RECT_EQ(gfx::RectF(290, 290, 16, 23), rootDamageRect);
684 TEST_F(DamageTrackerTest, verifyDamageForSurfaceChangeFromAncestorLayer)
686 // An ancestor/owning layer changes that affects the position/transform of the render
687 // surface. Note that in this case, the layerPropertyChanged flag already propagates
688 // to the subtree (tested in LayerImpltest), which damages the entire child1
689 // surface, but the damage tracker still needs the correct logic to compute the
690 // exposed region on the root surface.
692 // FIXME: the expectations of this test case should change when we add support for a
693 // unique scissorRect per renderSurface. In that case, the child1 surface
694 // should be completely unchanged, since we are only transforming it, while the
695 // root surface would be damaged appropriately.
697 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
698 LayerImpl* child1 = root->children()[0];
699 gfx::RectF childDamageRect;
700 gfx::RectF rootDamageRect;
702 clearDamageForAllSurfaces(root.get());
703 child1->setPosition(gfx::PointF(50, 50));
704 emulateDrawingOneFrame(root.get());
705 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
706 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
708 // The new surface bounds should be damaged entirely.
709 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 16, 18), childDamageRect);
711 // The entire child1 surface and the old exposed child1 surface should damage the root surface.
712 // - old child1 surface in target space: gfx::RectF(290, 290, 16, 18)
713 // - new child1 surface in target space: gfx::RectF(240, 240, 16, 18)
714 EXPECT_FLOAT_RECT_EQ(gfx::RectF(240, 240, 66, 68), rootDamageRect);
717 TEST_F(DamageTrackerTest, verifyDamageForAddingAndRemovingRenderSurfaces)
719 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
720 LayerImpl* child1 = root->children()[0];
721 gfx::RectF childDamageRect;
722 gfx::RectF rootDamageRect;
724 // CASE 1: If a descendant surface disappears, its entire old area becomes exposed.
726 clearDamageForAllSurfaces(root.get());
727 child1->setOpacity(1);
728 emulateDrawingOneFrame(root.get());
730 // Sanity check that there is only one surface now.
731 ASSERT_FALSE(child1->renderSurface());
732 ASSERT_EQ(4u, root->renderSurface()->layerList().size());
734 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
735 EXPECT_FLOAT_RECT_EQ(gfx::RectF(290, 290, 16, 18), rootDamageRect);
737 // CASE 2: If a descendant surface appears, its entire old area becomes exposed.
739 // Cycle one frame of no change, just to sanity check that the next rect is not because of the old damage state.
740 clearDamageForAllSurfaces(root.get());
741 emulateDrawingOneFrame(root.get());
742 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
743 EXPECT_TRUE(rootDamageRect.IsEmpty());
745 // Then change the tree so that the render surface is added back.
746 clearDamageForAllSurfaces(root.get());
747 child1->setOpacity(0.5);
748 emulateDrawingOneFrame(root.get());
750 // Sanity check that there is a new surface now.
751 ASSERT_TRUE(child1->renderSurface());
752 EXPECT_EQ(3u, root->renderSurface()->layerList().size());
753 EXPECT_EQ(2u, child1->renderSurface()->layerList().size());
755 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
756 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
757 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 16, 18), childDamageRect);
758 EXPECT_FLOAT_RECT_EQ(gfx::RectF(290, 290, 16, 18), rootDamageRect);
761 TEST_F(DamageTrackerTest, verifyNoDamageWhenNothingChanged)
763 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
764 LayerImpl* child1 = root->children()[0];
765 gfx::RectF childDamageRect;
766 gfx::RectF rootDamageRect;
768 // CASE 1: If nothing changes, the damage rect should be empty.
770 clearDamageForAllSurfaces(root.get());
771 emulateDrawingOneFrame(root.get());
772 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
773 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
774 EXPECT_TRUE(childDamageRect.IsEmpty());
775 EXPECT_TRUE(rootDamageRect.IsEmpty());
777 // CASE 2: If nothing changes twice in a row, the damage rect should still be empty.
779 clearDamageForAllSurfaces(root.get());
780 emulateDrawingOneFrame(root.get());
781 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
782 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
783 EXPECT_TRUE(childDamageRect.IsEmpty());
784 EXPECT_TRUE(rootDamageRect.IsEmpty());
787 TEST_F(DamageTrackerTest, verifyNoDamageForUpdateRectThatDoesNotDrawContent)
789 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
790 LayerImpl* child1 = root->children()[0];
791 gfx::RectF childDamageRect;
792 gfx::RectF rootDamageRect;
794 // In our specific tree, the update rect of child1 should not cause any damage to any
795 // surface because it does not actually draw content.
796 clearDamageForAllSurfaces(root.get());
797 child1->setUpdateRect(gfx::RectF(0, 0, 1, 2));
798 emulateDrawingOneFrame(root.get());
799 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
800 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
801 EXPECT_TRUE(childDamageRect.IsEmpty());
802 EXPECT_TRUE(rootDamageRect.IsEmpty());
805 TEST_F(DamageTrackerTest, verifyDamageForReplica)
807 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
808 LayerImpl* child1 = root->children()[0];
809 LayerImpl* grandChild1 = child1->children()[0];
810 LayerImpl* grandChild2 = child1->children()[1];
812 // Damage on a surface that has a reflection should cause the target surface to
813 // receive the surface's damage and the surface's reflected damage.
815 // For this test case, we modify grandChild2, and add grandChild3 to extend the bounds
816 // of child1's surface. This way, we can test reflection changes without changing
817 // contentBounds of the surface.
818 grandChild2->setPosition(gfx::PointF(180, 180));
820 scoped_ptr<LayerImpl> grandChild3 = LayerImpl::create(6);
821 grandChild3->setPosition(gfx::PointF(240, 240));
822 grandChild3->setAnchorPoint(gfx::PointF());
823 grandChild3->setBounds(gfx::Size(10, 10));
824 grandChild3->setContentBounds(gfx::Size(10, 10));
825 grandChild3->setDrawsContent(true);
826 child1->addChild(grandChild3.Pass());
828 child1->setOpacity(0.5);
829 emulateDrawingOneFrame(root.get());
831 // CASE 1: adding a reflection about the left edge of grandChild1.
833 clearDamageForAllSurfaces(root.get());
835 scoped_ptr<LayerImpl> grandChild1Replica = LayerImpl::create(7);
836 grandChild1Replica->setPosition(gfx::PointF());
837 grandChild1Replica->setAnchorPoint(gfx::PointF());
838 WebTransformationMatrix reflection;
839 reflection.scale3d(-1, 1, 1);
840 grandChild1Replica->setTransform(reflection);
841 grandChild1->setReplicaLayer(grandChild1Replica.Pass());
843 emulateDrawingOneFrame(root.get());
845 gfx::RectF grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
846 gfx::RectF childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
847 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
849 // The grandChild surface damage should not include its own replica. The child
850 // surface damage should include the normal and replica surfaces.
851 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 6, 8), grandChildDamageRect);
852 EXPECT_FLOAT_RECT_EQ(gfx::RectF(194, 200, 12, 8), childDamageRect);
853 EXPECT_FLOAT_RECT_EQ(gfx::RectF(294, 300, 12, 8), rootDamageRect);
855 // CASE 2: moving the descendant surface should cause both the original and reflected
856 // areas to be damaged on the target.
857 clearDamageForAllSurfaces(root.get());
858 gfx::Rect oldContentRect = child1->renderSurface()->contentRect();
859 grandChild1->setPosition(gfx::PointF(195, 205));
860 emulateDrawingOneFrame(root.get());
861 ASSERT_EQ(oldContentRect.width(), child1->renderSurface()->contentRect().width());
862 ASSERT_EQ(oldContentRect.height(), child1->renderSurface()->contentRect().height());
864 grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
865 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
866 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
868 // The child surface damage should include normal and replica surfaces for both old and new locations.
869 // - old location in target space: gfx::RectF(194, 200, 12, 8)
870 // - new location in target space: gfx::RectF(189, 205, 12, 8)
871 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 6, 8), grandChildDamageRect);
872 EXPECT_FLOAT_RECT_EQ(gfx::RectF(189, 200, 17, 13), childDamageRect);
873 EXPECT_FLOAT_RECT_EQ(gfx::RectF(289, 300, 17, 13), rootDamageRect);
875 // CASE 3: removing the reflection should cause the entire region including reflection
876 // to damage the target surface.
877 clearDamageForAllSurfaces(root.get());
878 grandChild1->setReplicaLayer(scoped_ptr<LayerImpl>());
879 emulateDrawingOneFrame(root.get());
880 ASSERT_EQ(oldContentRect.width(), child1->renderSurface()->contentRect().width());
881 ASSERT_EQ(oldContentRect.height(), child1->renderSurface()->contentRect().height());
883 EXPECT_FALSE(grandChild1->renderSurface());
884 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
885 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
887 EXPECT_FLOAT_RECT_EQ(gfx::RectF(189, 205, 12, 8), childDamageRect);
888 EXPECT_FLOAT_RECT_EQ(gfx::RectF(289, 305, 12, 8), rootDamageRect);
891 TEST_F(DamageTrackerTest, verifyDamageForMask)
893 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
894 LayerImpl* child = root->children()[0];
896 // In the current implementation of the damage tracker, changes to mask layers should
897 // damage the entire corresponding surface.
899 clearDamageForAllSurfaces(root.get());
901 // Set up the mask layer.
903 scoped_ptr<LayerImpl> maskLayer = LayerImpl::create(3);
904 maskLayer->setPosition(child->position());
905 maskLayer->setAnchorPoint(gfx::PointF());
906 maskLayer->setBounds(child->bounds());
907 maskLayer->setContentBounds(child->bounds());
908 child->setMaskLayer(maskLayer.Pass());
910 LayerImpl* maskLayer = child->maskLayer();
912 // Add opacity and a grandChild so that the render surface persists even after we remove the mask.
913 child->setOpacity(0.5);
915 scoped_ptr<LayerImpl> grandChild = LayerImpl::create(4);
916 grandChild->setPosition(gfx::PointF(2, 2));
917 grandChild->setAnchorPoint(gfx::PointF());
918 grandChild->setBounds(gfx::Size(2, 2));
919 grandChild->setContentBounds(gfx::Size(2, 2));
920 grandChild->setDrawsContent(true);
921 child->addChild(grandChild.Pass());
923 emulateDrawingOneFrame(root.get());
925 // Sanity check that a new surface was created for the child.
926 ASSERT_TRUE(child->renderSurface());
928 // CASE 1: the updateRect on a mask layer should damage the entire target surface.
930 clearDamageForAllSurfaces(root.get());
931 maskLayer->setUpdateRect(gfx::RectF(1, 2, 3, 4));
932 emulateDrawingOneFrame(root.get());
933 gfx::RectF childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
934 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
936 // CASE 2: a property change on the mask layer should damage the entire target surface.
939 // Advance one frame without damage so that we know the damage rect is not leftover from the previous case.
940 clearDamageForAllSurfaces(root.get());
941 emulateDrawingOneFrame(root.get());
942 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
943 EXPECT_TRUE(childDamageRect.IsEmpty());
945 // Then test the property change.
946 clearDamageForAllSurfaces(root.get());
947 maskLayer->setStackingOrderChanged(true);
949 emulateDrawingOneFrame(root.get());
950 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
951 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
953 // CASE 3: removing the mask also damages the entire target surface.
956 // Advance one frame without damage so that we know the damage rect is not leftover from the previous case.
957 clearDamageForAllSurfaces(root.get());
958 emulateDrawingOneFrame(root.get());
959 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
960 EXPECT_TRUE(childDamageRect.IsEmpty());
962 // Then test mask removal.
963 clearDamageForAllSurfaces(root.get());
964 child->setMaskLayer(scoped_ptr<LayerImpl>());
965 ASSERT_TRUE(child->layerPropertyChanged());
966 emulateDrawingOneFrame(root.get());
968 // Sanity check that a render surface still exists.
969 ASSERT_TRUE(child->renderSurface());
971 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
972 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
975 TEST_F(DamageTrackerTest, verifyDamageForReplicaMask)
977 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
978 LayerImpl* child1 = root->children()[0];
979 LayerImpl* grandChild1 = child1->children()[0];
981 // Changes to a replica's mask should not damage the original surface, because it is
982 // not masked. But it does damage the ancestor target surface.
984 clearDamageForAllSurfaces(root.get());
986 // Create a reflection about the left edge of grandChild1.
988 scoped_ptr<LayerImpl> grandChild1Replica = LayerImpl::create(6);
989 grandChild1Replica->setPosition(gfx::PointF());
990 grandChild1Replica->setAnchorPoint(gfx::PointF());
991 WebTransformationMatrix reflection;
992 reflection.scale3d(-1, 1, 1);
993 grandChild1Replica->setTransform(reflection);
994 grandChild1->setReplicaLayer(grandChild1Replica.Pass());
996 LayerImpl* grandChild1Replica = grandChild1->replicaLayer();
998 // Set up the mask layer on the replica layer
1000 scoped_ptr<LayerImpl> replicaMaskLayer = LayerImpl::create(7);
1001 replicaMaskLayer->setPosition(gfx::PointF());
1002 replicaMaskLayer->setAnchorPoint(gfx::PointF());
1003 replicaMaskLayer->setBounds(grandChild1->bounds());
1004 replicaMaskLayer->setContentBounds(grandChild1->bounds());
1005 grandChild1Replica->setMaskLayer(replicaMaskLayer.Pass());
1007 LayerImpl* replicaMaskLayer = grandChild1Replica->maskLayer();
1009 emulateDrawingOneFrame(root.get());
1011 // Sanity check that the appropriate render surfaces were created
1012 ASSERT_TRUE(grandChild1->renderSurface());
1014 // CASE 1: a property change on the mask should damage only the reflected region on the target surface.
1015 clearDamageForAllSurfaces(root.get());
1016 replicaMaskLayer->setStackingOrderChanged(true);
1017 emulateDrawingOneFrame(root.get());
1019 gfx::RectF grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
1020 gfx::RectF childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
1022 EXPECT_TRUE(grandChildDamageRect.IsEmpty());
1023 EXPECT_FLOAT_RECT_EQ(gfx::RectF(194, 200, 6, 8), childDamageRect);
1025 // CASE 2: removing the replica mask damages only the reflected region on the target surface.
1027 clearDamageForAllSurfaces(root.get());
1028 grandChild1Replica->setMaskLayer(scoped_ptr<LayerImpl>());
1029 emulateDrawingOneFrame(root.get());
1031 grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
1032 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
1034 EXPECT_TRUE(grandChildDamageRect.IsEmpty());
1035 EXPECT_FLOAT_RECT_EQ(gfx::RectF(194, 200, 6, 8), childDamageRect);
1038 TEST_F(DamageTrackerTest, verifyDamageForReplicaMaskWithAnchor)
1040 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
1041 LayerImpl* child1 = root->children()[0];
1042 LayerImpl* grandChild1 = child1->children()[0];
1044 // Verify that the correct replicaOriginTransform is used for the replicaMask;
1045 clearDamageForAllSurfaces(root.get());
1047 grandChild1->setAnchorPoint(gfx::PointF(1, 0)); // This is not exactly the anchor being tested, but by convention its expected to be the same as the replica's anchor point.
1050 scoped_ptr<LayerImpl> grandChild1Replica = LayerImpl::create(6);
1051 grandChild1Replica->setPosition(gfx::PointF());
1052 grandChild1Replica->setAnchorPoint(gfx::PointF(1, 0)); // This is the anchor being tested.
1053 WebTransformationMatrix reflection;
1054 reflection.scale3d(-1, 1, 1);
1055 grandChild1Replica->setTransform(reflection);
1056 grandChild1->setReplicaLayer(grandChild1Replica.Pass());
1058 LayerImpl* grandChild1Replica = grandChild1->replicaLayer();
1060 // Set up the mask layer on the replica layer
1062 scoped_ptr<LayerImpl> replicaMaskLayer = LayerImpl::create(7);
1063 replicaMaskLayer->setPosition(gfx::PointF());
1064 replicaMaskLayer->setAnchorPoint(gfx::PointF()); // note, this is not the anchor being tested.
1065 replicaMaskLayer->setBounds(grandChild1->bounds());
1066 replicaMaskLayer->setContentBounds(grandChild1->bounds());
1067 grandChild1Replica->setMaskLayer(replicaMaskLayer.Pass());
1069 LayerImpl* replicaMaskLayer = grandChild1Replica->maskLayer();
1071 emulateDrawingOneFrame(root.get());
1073 // Sanity check that the appropriate render surfaces were created
1074 ASSERT_TRUE(grandChild1->renderSurface());
1076 // A property change on the replicaMask should damage the reflected region on the target surface.
1077 clearDamageForAllSurfaces(root.get());
1078 replicaMaskLayer->setStackingOrderChanged(true);
1080 emulateDrawingOneFrame(root.get());
1082 gfx::RectF childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
1083 EXPECT_FLOAT_RECT_EQ(gfx::RectF(206, 200, 6, 8), childDamageRect);
1086 TEST_F(DamageTrackerTest, verifyDamageWhenForcedFullDamage)
1088 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
1089 LayerImpl* child = root->children()[0];
1091 // Case 1: This test ensures that when the tracker is forced to have full damage, that
1092 // it takes priority over any other partial damage.
1094 clearDamageForAllSurfaces(root.get());
1095 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
1096 root->renderSurface()->damageTracker()->forceFullDamageNextUpdate();
1097 emulateDrawingOneFrame(root.get());
1098 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1099 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 500, 500), rootDamageRect);
1101 // Case 2: An additional sanity check that forcing full damage works even when nothing
1102 // on the layer tree changed.
1104 clearDamageForAllSurfaces(root.get());
1105 root->renderSurface()->damageTracker()->forceFullDamageNextUpdate();
1106 emulateDrawingOneFrame(root.get());
1107 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1108 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 500, 500), rootDamageRect);
1111 TEST_F(DamageTrackerTest, verifyDamageForEmptyLayerList)
1113 // Though it should never happen, its a good idea to verify that the damage tracker
1114 // does not crash when it receives an empty layerList.
1116 scoped_ptr<LayerImpl> root = LayerImpl::create(1);
1117 root->createRenderSurface();
1119 ASSERT_TRUE(root == root->renderTarget());
1120 RenderSurfaceImpl* targetSurface = root->renderSurface();
1121 targetSurface->clearLayerLists();
1122 targetSurface->damageTracker()->updateDamageTrackingState(targetSurface->layerList(), targetSurface->owningLayerId(), false, gfx::Rect(), 0, WebFilterOperations(), 0);
1124 gfx::RectF damageRect = targetSurface->damageTracker()->currentDamageRect();
1125 EXPECT_TRUE(damageRect.IsEmpty());
1128 TEST_F(DamageTrackerTest, verifyDamageAccumulatesUntilReset)
1130 // If damage is not cleared, it should accumulate.
1132 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
1133 LayerImpl* child = root->children()[0];
1135 clearDamageForAllSurfaces(root.get());
1136 child->setUpdateRect(gfx::RectF(10, 11, 1, 2));
1137 emulateDrawingOneFrame(root.get());
1139 // Sanity check damage after the first frame; this isnt the actual test yet.
1140 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1141 EXPECT_FLOAT_RECT_EQ(gfx::RectF(110, 111, 1, 2), rootDamageRect);
1143 // New damage, without having cleared the previous damage, should be unioned to the previous one.
1144 child->setUpdateRect(gfx::RectF(20, 25, 1, 2));
1145 emulateDrawingOneFrame(root.get());
1146 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1147 EXPECT_FLOAT_RECT_EQ(gfx::RectF(110, 111, 11, 16), rootDamageRect);
1149 // If we notify the damage tracker that we drew the damaged area, then damage should be emptied.
1150 root->renderSurface()->damageTracker()->didDrawDamagedArea();
1151 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1152 EXPECT_TRUE(rootDamageRect.IsEmpty());
1154 // Damage should remain empty even after one frame, since there's yet no new damage
1155 emulateDrawingOneFrame(root.get());
1156 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1157 EXPECT_TRUE(rootDamageRect.IsEmpty());
1160 } // anonymous namespace