Depend on stored sync session GUID for Android.
[chromium-blink-merge.git] / cc / damage_tracker_unittest.cc
blobd9e49b2fbb2634ae8bd5d8fa89a8035118599ad9
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 "cc/damage_tracker.h"
7 #include "cc/layer_impl.h"
8 #include "cc/layer_sorter.h"
9 #include "cc/layer_tree_host_common.h"
10 #include "cc/math_util.h"
11 #include "cc/single_thread_proxy.h"
12 #include "cc/test/geometry_test_utils.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
15 #include <public/WebFilterOperation.h>
16 #include <public/WebFilterOperations.h>
18 using namespace WebKit;
19 using namespace WebKitTests;
21 namespace cc {
22 namespace {
24 void executeCalculateDrawTransformsAndVisibility(LayerImpl* root, std::vector<LayerImpl*>& renderSurfaceLayerList)
26 LayerSorter layerSorter;
27 int dummyMaxTextureSize = 512;
29 // Sanity check: The test itself should create the root layer's render surface, so
30 // that the surface (and its damage tracker) can persist across multiple
31 // calls to this function.
32 ASSERT_TRUE(root->renderSurface());
33 ASSERT_FALSE(renderSurfaceLayerList.size());
35 LayerTreeHostCommon::calculateDrawTransforms(root, root->bounds(), 1, 1, &layerSorter, dummyMaxTextureSize, renderSurfaceLayerList);
38 void clearDamageForAllSurfaces(LayerImpl* layer)
40 if (layer->renderSurface())
41 layer->renderSurface()->damageTracker()->didDrawDamagedArea();
43 // Recursively clear damage for any existing surface.
44 for (size_t i = 0; i < layer->children().size(); ++i)
45 clearDamageForAllSurfaces(layer->children()[i]);
48 void emulateDrawingOneFrame(LayerImpl* root)
50 // This emulates only the steps that are relevant to testing the damage tracker:
51 // 1. computing the render passes and layerlists
52 // 2. updating all damage trackers in the correct order
53 // 3. resetting all updateRects and propertyChanged flags for all layers and surfaces.
55 std::vector<LayerImpl*> renderSurfaceLayerList;
56 executeCalculateDrawTransformsAndVisibility(root, renderSurfaceLayerList);
58 // Iterate back-to-front, so that damage correctly propagates from descendant surfaces to ancestors.
59 for (int i = renderSurfaceLayerList.size() - 1; i >= 0; --i) {
60 RenderSurfaceImpl* targetSurface = renderSurfaceLayerList[i]->renderSurface();
61 targetSurface->damageTracker()->updateDamageTrackingState(targetSurface->layerList(), targetSurface->owningLayerId(), targetSurface->surfacePropertyChangedOnlyFromDescendant(), targetSurface->contentRect(), renderSurfaceLayerList[i]->maskLayer(), renderSurfaceLayerList[i]->filters(), renderSurfaceLayerList[i]->filter().get());
64 root->resetAllChangeTrackingForSubtree();
67 scoped_ptr<LayerImpl> createTestTreeWithOneSurface()
69 scoped_ptr<LayerImpl> root = LayerImpl::create(1);
70 scoped_ptr<LayerImpl> child = LayerImpl::create(2);
72 root->setPosition(gfx::PointF());
73 root->setAnchorPoint(gfx::PointF());
74 root->setBounds(gfx::Size(500, 500));
75 root->setContentBounds(gfx::Size(500, 500));
76 root->setDrawsContent(true);
77 root->createRenderSurface();
78 root->renderSurface()->setContentRect(gfx::Rect(gfx::Point(), gfx::Size(500, 500)));
80 child->setPosition(gfx::PointF(100, 100));
81 child->setAnchorPoint(gfx::PointF());
82 child->setBounds(gfx::Size(30, 30));
83 child->setContentBounds(gfx::Size(30, 30));
84 child->setDrawsContent(true);
85 root->addChild(child.Pass());
87 return root.Pass();
90 scoped_ptr<LayerImpl> createTestTreeWithTwoSurfaces()
92 // This test tree has two render surfaces: one for the root, and one for
93 // child1. Additionally, the root has a second child layer, and child1 has two
94 // children of its own.
96 scoped_ptr<LayerImpl> root = LayerImpl::create(1);
97 scoped_ptr<LayerImpl> child1 = LayerImpl::create(2);
98 scoped_ptr<LayerImpl> child2 = LayerImpl::create(3);
99 scoped_ptr<LayerImpl> grandChild1 = LayerImpl::create(4);
100 scoped_ptr<LayerImpl> grandChild2 = LayerImpl::create(5);
102 root->setPosition(gfx::PointF());
103 root->setAnchorPoint(gfx::PointF());
104 root->setBounds(gfx::Size(500, 500));
105 root->setContentBounds(gfx::Size(500, 500));
106 root->setDrawsContent(true);
107 root->createRenderSurface();
108 root->renderSurface()->setContentRect(gfx::Rect(gfx::Point(), gfx::Size(500, 500)));
110 child1->setPosition(gfx::PointF(100, 100));
111 child1->setAnchorPoint(gfx::PointF());
112 child1->setBounds(gfx::Size(30, 30));
113 child1->setContentBounds(gfx::Size(30, 30));
114 child1->setOpacity(0.5); // with a child that drawsContent, this will cause the layer to create its own renderSurface.
115 child1->setDrawsContent(false); // this layer does not draw, but is intended to create its own renderSurface.
116 child1->setForceRenderSurface(true);
118 child2->setPosition(gfx::PointF(11, 11));
119 child2->setAnchorPoint(gfx::PointF());
120 child2->setBounds(gfx::Size(18, 18));
121 child2->setContentBounds(gfx::Size(18, 18));
122 child2->setDrawsContent(true);
124 grandChild1->setPosition(gfx::PointF(200, 200));
125 grandChild1->setAnchorPoint(gfx::PointF());
126 grandChild1->setBounds(gfx::Size(6, 8));
127 grandChild1->setContentBounds(gfx::Size(6, 8));
128 grandChild1->setDrawsContent(true);
130 grandChild2->setPosition(gfx::PointF(190, 190));
131 grandChild2->setAnchorPoint(gfx::PointF());
132 grandChild2->setBounds(gfx::Size(6, 8));
133 grandChild2->setContentBounds(gfx::Size(6, 8));
134 grandChild2->setDrawsContent(true);
136 child1->addChild(grandChild1.Pass());
137 child1->addChild(grandChild2.Pass());
138 root->addChild(child1.Pass());
139 root->addChild(child2.Pass());
141 return root.Pass();
144 scoped_ptr<LayerImpl> createAndSetUpTestTreeWithOneSurface()
146 scoped_ptr<LayerImpl> root = createTestTreeWithOneSurface();
148 // Setup includes going past the first frame which always damages everything, so
149 // that we can actually perform specific tests.
150 emulateDrawingOneFrame(root.get());
152 return root.Pass();
155 scoped_ptr<LayerImpl> createAndSetUpTestTreeWithTwoSurfaces()
157 scoped_ptr<LayerImpl> root = createTestTreeWithTwoSurfaces();
159 // Setup includes going past the first frame which always damages everything, so
160 // that we can actually perform specific tests.
161 emulateDrawingOneFrame(root.get());
163 return root.Pass();
166 class DamageTrackerTest : public testing::Test {
169 TEST_F(DamageTrackerTest, sanityCheckTestTreeWithOneSurface)
171 // Sanity check that the simple test tree will actually produce the expected render
172 // surfaces and layer lists.
174 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
176 EXPECT_EQ(2u, root->renderSurface()->layerList().size());
177 EXPECT_EQ(1, root->renderSurface()->layerList()[0]->id());
178 EXPECT_EQ(2, root->renderSurface()->layerList()[1]->id());
180 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
181 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 500, 500), rootDamageRect);
184 TEST_F(DamageTrackerTest, sanityCheckTestTreeWithTwoSurfaces)
186 // Sanity check that the complex test tree will actually produce the expected render
187 // surfaces and layer lists.
189 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
191 LayerImpl* child1 = root->children()[0];
192 LayerImpl* child2 = root->children()[1];
193 gfx::RectF childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
194 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
196 ASSERT_TRUE(child1->renderSurface());
197 EXPECT_FALSE(child2->renderSurface());
198 EXPECT_EQ(3u, root->renderSurface()->layerList().size());
199 EXPECT_EQ(2u, child1->renderSurface()->layerList().size());
201 // The render surface for child1 only has a contentRect that encloses grandChild1 and grandChild2, because child1 does not draw content.
202 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 16, 18), childDamageRect);
203 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 500, 500), rootDamageRect);
206 TEST_F(DamageTrackerTest, verifyDamageForUpdateRects)
208 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
209 LayerImpl* child = root->children()[0];
211 // CASE 1: Setting the update rect should cause the corresponding damage to the surface.
213 clearDamageForAllSurfaces(root.get());
214 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
215 emulateDrawingOneFrame(root.get());
217 // Damage position on the surface should be: position of updateRect (10, 11) relative to the child (100, 100).
218 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
219 EXPECT_FLOAT_RECT_EQ(gfx::RectF(110, 111, 12, 13), rootDamageRect);
221 // CASE 2: The same update rect twice in a row still produces the same damage.
223 clearDamageForAllSurfaces(root.get());
224 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
225 emulateDrawingOneFrame(root.get());
226 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
227 EXPECT_FLOAT_RECT_EQ(gfx::RectF(110, 111, 12, 13), rootDamageRect);
229 // CASE 3: Setting a different update rect should cause damage on the new update region, but no additional exposed old region.
231 clearDamageForAllSurfaces(root.get());
232 child->setUpdateRect(gfx::RectF(20, 25, 1, 2));
233 emulateDrawingOneFrame(root.get());
235 // Damage position on the surface should be: position of updateRect (20, 25) relative to the child (100, 100).
236 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
237 EXPECT_FLOAT_RECT_EQ(gfx::RectF(120, 125, 1, 2), rootDamageRect);
240 TEST_F(DamageTrackerTest, verifyDamageForPropertyChanges)
242 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
243 LayerImpl* child = root->children()[0];
245 // CASE 1: The layer's property changed flag takes priority over update rect.
247 clearDamageForAllSurfaces(root.get());
248 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
249 child->setOpacity(0.5);
250 emulateDrawingOneFrame(root.get());
252 // Sanity check - we should not have accidentally created a separate render surface for the translucent layer.
253 ASSERT_FALSE(child->renderSurface());
254 ASSERT_EQ(2u, root->renderSurface()->layerList().size());
256 // Damage should be the entire child layer in targetSurface space.
257 gfx::RectF expectedRect = gfx::RectF(100, 100, 30, 30);
258 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
259 EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
261 // CASE 2: If a layer moves due to property change, it damages both the new location
262 // and the old (exposed) location. The old location is the entire old layer,
263 // not just the updateRect.
265 // Cycle one frame of no change, just to sanity check that the next rect is not because of the old damage state.
266 clearDamageForAllSurfaces(root.get());
267 emulateDrawingOneFrame(root.get());
268 EXPECT_TRUE(root->renderSurface()->damageTracker()->currentDamageRect().IsEmpty());
270 // Then, test the actual layer movement.
271 clearDamageForAllSurfaces(root.get());
272 child->setPosition(gfx::PointF(200, 230));
273 emulateDrawingOneFrame(root.get());
275 // Expect damage to be the combination of the previous one and the new one.
276 expectedRect.Union(gfx::RectF(200, 230, 30, 30));
277 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
278 EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
281 TEST_F(DamageTrackerTest, verifyDamageForTransformedLayer)
283 // If a layer is transformed, the damage rect should still enclose the entire
284 // transformed layer.
286 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
287 LayerImpl* child = root->children()[0];
289 gfx::Transform rotation;
290 rotation.Rotate(45);
292 clearDamageForAllSurfaces(root.get());
293 child->setAnchorPoint(gfx::PointF(0.5, 0.5));
294 child->setPosition(gfx::PointF(85, 85));
295 emulateDrawingOneFrame(root.get());
297 // Sanity check that the layer actually moved to (85, 85), damaging its old location and new location.
298 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
299 EXPECT_FLOAT_RECT_EQ(gfx::RectF(85, 85, 45, 45), rootDamageRect);
301 // With the anchor on the layer's center, now we can test the rotation more
302 // intuitively, since it applies about the layer's anchor.
303 clearDamageForAllSurfaces(root.get());
304 child->setTransform(rotation);
305 emulateDrawingOneFrame(root.get());
307 // Since the child layer is square, rotation by 45 degrees about the center should
308 // increase the size of the expected rect by sqrt(2), centered around (100, 100). The
309 // old exposed region should be fully contained in the new region.
310 double expectedWidth = 30 * sqrt(2.0);
311 double expectedPosition = 100 - 0.5 * expectedWidth;
312 gfx::RectF expectedRect(expectedPosition, expectedPosition, expectedWidth, expectedWidth);
313 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
314 EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
317 TEST_F(DamageTrackerTest, verifyDamageForPerspectiveClippedLayer)
319 // If a layer has a perspective transform that causes w < 0, then not clipping the
320 // layer can cause an invalid damage rect. This test checks that the w < 0 case is
321 // tracked properly.
323 // The transform is constructed so that if w < 0 clipping is not performed, the
324 // incorrect rect will be very small, specifically: position (500.972504, 498.544617) and size 0.056610 x 2.910767.
325 // Instead, the correctly transformed rect should actually be very huge (i.e. in theory, -infinity on the left),
326 // and positioned so that the right-most bound rect will be approximately 501 units in root surface space.
329 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
330 LayerImpl* child = root->children()[0];
332 gfx::Transform transform;
333 transform.Translate3d(500, 500, 0);
334 transform.ApplyPerspectiveDepth(1);
335 MathUtil::rotateEulerAngles(&transform, 0, 45, 0);
336 transform.Translate3d(-50, -50, 0);
338 // Set up the child
339 child->setPosition(gfx::PointF(0, 0));
340 child->setBounds(gfx::Size(100, 100));
341 child->setContentBounds(gfx::Size(100, 100));
342 child->setTransform(transform);
343 emulateDrawingOneFrame(root.get());
345 // Sanity check that the child layer's bounds would actually get clipped by w < 0,
346 // otherwise this test is not actually testing the intended scenario.
347 gfx::QuadF testQuad(gfx::RectF(gfx::PointF(), gfx::SizeF(100, 100)));
348 bool clipped = false;
349 MathUtil::mapQuad(transform, testQuad, clipped);
350 EXPECT_TRUE(clipped);
352 // Damage the child without moving it.
353 clearDamageForAllSurfaces(root.get());
354 child->setOpacity(0.5);
355 emulateDrawingOneFrame(root.get());
357 // The expected damage should cover the entire root surface (500x500), but we don't
358 // care whether the damage rect was clamped or is larger than the surface for this test.
359 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
360 gfx::RectF damageWeCareAbout = gfx::RectF(gfx::PointF(), gfx::SizeF(500, 500));
361 EXPECT_TRUE(rootDamageRect.Contains(damageWeCareAbout));
364 TEST_F(DamageTrackerTest, verifyDamageForBlurredSurface)
366 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
367 LayerImpl* child = root->children()[0];
369 WebFilterOperations filters;
370 filters.append(WebFilterOperation::createBlurFilter(5));
371 int outsetTop, outsetRight, outsetBottom, outsetLeft;
372 filters.getOutsets(outsetTop, outsetRight, outsetBottom, outsetLeft);
374 // Setting the filter will damage the whole surface.
375 clearDamageForAllSurfaces(root.get());
376 root->setFilters(filters);
377 emulateDrawingOneFrame(root.get());
379 // Setting the update rect should cause the corresponding damage to the surface, blurred based on the size of the blur filter.
380 clearDamageForAllSurfaces(root.get());
381 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
382 emulateDrawingOneFrame(root.get());
384 // Damage position on the surface should be: position of updateRect (10, 11) relative to the child (100, 100), but expanded by the blur outsets.
385 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
386 gfx::RectF expectedDamageRect = gfx::RectF(110, 111, 12, 13);
387 expectedDamageRect.Inset(-outsetLeft, -outsetTop, -outsetRight, -outsetBottom);
388 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
391 TEST_F(DamageTrackerTest, verifyDamageForImageFilter)
393 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
394 LayerImpl* child = root->children()[0];
395 gfx::RectF rootDamageRect, childDamageRect;
397 // Allow us to set damage on child too.
398 child->setDrawsContent(true);
400 skia::RefPtr<SkImageFilter> filter = skia::AdoptRef(new SkBlurImageFilter(SkIntToScalar(2), SkIntToScalar(2)));
401 // Setting the filter will damage the whole surface.
402 clearDamageForAllSurfaces(root.get());
403 child->setFilter(filter);
404 emulateDrawingOneFrame(root.get());
405 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
406 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
407 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 30, 30), rootDamageRect);
408 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
410 // CASE 1: Setting the update rect should damage the whole surface (for now)
411 clearDamageForAllSurfaces(root.get());
412 child->setUpdateRect(gfx::RectF(0, 0, 1, 1));
413 emulateDrawingOneFrame(root.get());
415 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
416 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
417 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 30, 30), rootDamageRect);
418 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
421 TEST_F(DamageTrackerTest, verifyDamageForBackgroundBlurredChild)
423 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
424 LayerImpl* child1 = root->children()[0];
425 LayerImpl* child2 = root->children()[1];
427 // Allow us to set damage on child1 too.
428 child1->setDrawsContent(true);
430 WebFilterOperations filters;
431 filters.append(WebFilterOperation::createBlurFilter(2));
432 int outsetTop, outsetRight, outsetBottom, outsetLeft;
433 filters.getOutsets(outsetTop, outsetRight, outsetBottom, outsetLeft);
435 // Setting the filter will damage the whole surface.
436 clearDamageForAllSurfaces(root.get());
437 child1->setBackgroundFilters(filters);
438 emulateDrawingOneFrame(root.get());
440 // CASE 1: Setting the update rect should cause the corresponding damage to
441 // the surface, blurred based on the size of the child's background blur
442 // filter.
443 clearDamageForAllSurfaces(root.get());
444 root->setUpdateRect(gfx::RectF(297, 297, 2, 2));
445 emulateDrawingOneFrame(root.get());
447 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
448 // Damage position on the surface should be a composition of the damage on the root and on child2.
449 // Damage on the root should be: position of updateRect (297, 297), but expanded by the blur outsets.
450 gfx::RectF expectedDamageRect = gfx::RectF(297, 297, 2, 2);
451 expectedDamageRect.Inset(-outsetLeft, -outsetTop, -outsetRight, -outsetBottom);
452 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
454 // CASE 2: Setting the update rect should cause the corresponding damage to
455 // the surface, blurred based on the size of the child's background blur
456 // filter. Since the damage extends to the right/bottom outside of the
457 // blurred layer, only the left/top should end up expanded.
458 clearDamageForAllSurfaces(root.get());
459 root->setUpdateRect(gfx::RectF(297, 297, 30, 30));
460 emulateDrawingOneFrame(root.get());
462 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
463 // Damage position on the surface should be a composition of the damage on the root and on child2.
464 // Damage on the root should be: position of updateRect (297, 297), but expanded on the left/top
465 // by the blur outsets.
466 expectedDamageRect = gfx::RectF(297, 297, 30, 30);
467 expectedDamageRect.Inset(-outsetLeft, -outsetTop, 0, 0);
468 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
470 // CASE 3: Setting this update rect outside the blurred contentBounds of the blurred
471 // child1 will not cause it to be expanded.
472 clearDamageForAllSurfaces(root.get());
473 root->setUpdateRect(gfx::RectF(30, 30, 2, 2));
474 emulateDrawingOneFrame(root.get());
476 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
477 // Damage on the root should be: position of updateRect (30, 30), not
478 // expanded.
479 expectedDamageRect = gfx::RectF(30, 30, 2, 2);
480 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
482 // CASE 4: Setting this update rect inside the blurred contentBounds but outside the
483 // original contentBounds of the blurred child1 will cause it to be expanded.
484 clearDamageForAllSurfaces(root.get());
485 root->setUpdateRect(gfx::RectF(99, 99, 1, 1));
486 emulateDrawingOneFrame(root.get());
488 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
489 // Damage on the root should be: position of updateRect (99, 99), expanded
490 // by the blurring on child1, but since it is 1 pixel outside the layer, the
491 // expanding should be reduced by 1.
492 expectedDamageRect = gfx::RectF(99, 99, 1, 1);
493 expectedDamageRect.Inset(-outsetLeft + 1, -outsetTop + 1, -outsetRight, -outsetBottom);
494 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
496 // CASE 5: Setting the update rect on child2, which is above child1, will
497 // not get blurred by child1, so it does not need to get expanded.
498 clearDamageForAllSurfaces(root.get());
499 child2->setUpdateRect(gfx::RectF(0, 0, 1, 1));
500 emulateDrawingOneFrame(root.get());
502 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
503 // Damage on child2 should be: position of updateRect offset by the child's position (11, 11), and not expanded by anything.
504 expectedDamageRect = gfx::RectF(11, 11, 1, 1);
505 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
507 // CASE 6: Setting the update rect on child1 will also blur the damage, so
508 // that any pixels needed for the blur are redrawn in the current frame.
509 clearDamageForAllSurfaces(root.get());
510 child1->setUpdateRect(gfx::RectF(0, 0, 1, 1));
511 emulateDrawingOneFrame(root.get());
513 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
514 // Damage on child1 should be: position of updateRect offset by the child's position (100, 100), and expanded by the damage.
515 expectedDamageRect = gfx::RectF(100, 100, 1, 1);
516 expectedDamageRect.Inset(-outsetLeft, -outsetTop, -outsetRight, -outsetBottom);
517 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
520 TEST_F(DamageTrackerTest, verifyDamageForAddingAndRemovingLayer)
522 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
523 LayerImpl* child1 = root->children()[0];
525 // CASE 1: Adding a new layer should cause the appropriate damage.
527 clearDamageForAllSurfaces(root.get());
529 scoped_ptr<LayerImpl> child2 = LayerImpl::create(3);
530 child2->setPosition(gfx::PointF(400, 380));
531 child2->setAnchorPoint(gfx::PointF());
532 child2->setBounds(gfx::Size(6, 8));
533 child2->setContentBounds(gfx::Size(6, 8));
534 child2->setDrawsContent(true);
535 root->addChild(child2.Pass());
537 emulateDrawingOneFrame(root.get());
539 // Sanity check - all 3 layers should be on the same render surface; render surfaces are tested elsewhere.
540 ASSERT_EQ(3u, root->renderSurface()->layerList().size());
542 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
543 EXPECT_FLOAT_RECT_EQ(gfx::RectF(400, 380, 6, 8), rootDamageRect);
545 // CASE 2: If the layer is removed, its entire old layer becomes exposed, not just the
546 // last update rect.
548 // Advance one frame without damage so that we know the damage rect is not leftover from the previous case.
549 clearDamageForAllSurfaces(root.get());
550 emulateDrawingOneFrame(root.get());
551 EXPECT_TRUE(root->renderSurface()->damageTracker()->currentDamageRect().IsEmpty());
553 // Then, test removing child1.
554 child1->removeFromParent();
555 emulateDrawingOneFrame(root.get());
556 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
557 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 30, 30), rootDamageRect);
560 TEST_F(DamageTrackerTest, verifyDamageForNewUnchangedLayer)
562 // If child2 is added to the layer tree, but it doesn't have any explicit damage of
563 // its own, it should still indeed damage the target surface.
565 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
567 clearDamageForAllSurfaces(root.get());
569 scoped_ptr<LayerImpl> child2 = LayerImpl::create(3);
570 child2->setPosition(gfx::PointF(400, 380));
571 child2->setAnchorPoint(gfx::PointF());
572 child2->setBounds(gfx::Size(6, 8));
573 child2->setContentBounds(gfx::Size(6, 8));
574 child2->setDrawsContent(true);
575 child2->resetAllChangeTrackingForSubtree();
576 // Sanity check the initial conditions of the test, if these asserts trigger, it
577 // means the test no longer actually covers the intended scenario.
578 ASSERT_FALSE(child2->layerPropertyChanged());
579 ASSERT_TRUE(child2->updateRect().IsEmpty());
580 root->addChild(child2.Pass());
582 emulateDrawingOneFrame(root.get());
584 // Sanity check - all 3 layers should be on the same render surface; render surfaces are tested elsewhere.
585 ASSERT_EQ(3u, root->renderSurface()->layerList().size());
587 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
588 EXPECT_FLOAT_RECT_EQ(gfx::RectF(400, 380, 6, 8), rootDamageRect);
591 TEST_F(DamageTrackerTest, verifyDamageForMultipleLayers)
593 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
594 LayerImpl* child1 = root->children()[0];
596 // In this test we don't want the above tree manipulation to be considered part of the same frame.
597 clearDamageForAllSurfaces(root.get());
599 scoped_ptr<LayerImpl> child2 = LayerImpl::create(3);
600 child2->setPosition(gfx::PointF(400, 380));
601 child2->setAnchorPoint(gfx::PointF());
602 child2->setBounds(gfx::Size(6, 8));
603 child2->setContentBounds(gfx::Size(6, 8));
604 child2->setDrawsContent(true);
605 root->addChild(child2.Pass());
607 LayerImpl* child2 = root->children()[1];
608 emulateDrawingOneFrame(root.get());
610 // Damaging two layers simultaneously should cause combined damage.
611 // - child1 update rect in surface space: gfx::RectF(100, 100, 1, 2);
612 // - child2 update rect in surface space: gfx::RectF(400, 380, 3, 4);
613 clearDamageForAllSurfaces(root.get());
614 child1->setUpdateRect(gfx::RectF(0, 0, 1, 2));
615 child2->setUpdateRect(gfx::RectF(0, 0, 3, 4));
616 emulateDrawingOneFrame(root.get());
617 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
618 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 303, 284), rootDamageRect);
621 TEST_F(DamageTrackerTest, verifyDamageForNestedSurfaces)
623 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
624 LayerImpl* child1 = root->children()[0];
625 LayerImpl* child2 = root->children()[1];
626 LayerImpl* grandChild1 = root->children()[0]->children()[0];
627 gfx::RectF childDamageRect;
628 gfx::RectF rootDamageRect;
630 // CASE 1: Damage to a descendant surface should propagate properly to ancestor surface.
632 clearDamageForAllSurfaces(root.get());
633 grandChild1->setOpacity(0.5);
634 emulateDrawingOneFrame(root.get());
635 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
636 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
637 EXPECT_FLOAT_RECT_EQ(gfx::RectF(200, 200, 6, 8), childDamageRect);
638 EXPECT_FLOAT_RECT_EQ(gfx::RectF(300, 300, 6, 8), rootDamageRect);
640 // CASE 2: Same as previous case, but with additional damage elsewhere that should be properly unioned.
641 // - child1 surface damage in root surface space: gfx::RectF(300, 300, 6, 8);
642 // - child2 damage in root surface space: gfx::RectF(11, 11, 18, 18);
643 clearDamageForAllSurfaces(root.get());
644 grandChild1->setOpacity(0.7f);
645 child2->setOpacity(0.7f);
646 emulateDrawingOneFrame(root.get());
647 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
648 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
649 EXPECT_FLOAT_RECT_EQ(gfx::RectF(200, 200, 6, 8), childDamageRect);
650 EXPECT_FLOAT_RECT_EQ(gfx::RectF(11, 11, 295, 297), rootDamageRect);
653 TEST_F(DamageTrackerTest, verifyDamageForSurfaceChangeFromDescendantLayer)
655 // If descendant layer changes and affects the content bounds of the render surface,
656 // then the entire descendant surface should be damaged, and it should damage its
657 // ancestor surface with the old and new surface regions.
659 // This is a tricky case, since only the first grandChild changes, but the entire
660 // surface should be marked dirty.
662 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
663 LayerImpl* child1 = root->children()[0];
664 LayerImpl* grandChild1 = root->children()[0]->children()[0];
665 gfx::RectF childDamageRect;
666 gfx::RectF rootDamageRect;
668 clearDamageForAllSurfaces(root.get());
669 grandChild1->setPosition(gfx::PointF(195, 205));
670 emulateDrawingOneFrame(root.get());
671 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
672 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
674 // The new surface bounds should be damaged entirely, even though only one of the layers changed.
675 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 11, 23), childDamageRect);
677 // Damage to the root surface should be the union of child1's *entire* render surface
678 // (in target space), and its old exposed area (also in target space).
679 EXPECT_FLOAT_RECT_EQ(gfx::RectF(290, 290, 16, 23), rootDamageRect);
682 TEST_F(DamageTrackerTest, verifyDamageForSurfaceChangeFromAncestorLayer)
684 // An ancestor/owning layer changes that affects the position/transform of the render
685 // surface. Note that in this case, the layerPropertyChanged flag already propagates
686 // to the subtree (tested in LayerImpltest), which damages the entire child1
687 // surface, but the damage tracker still needs the correct logic to compute the
688 // exposed region on the root surface.
690 // FIXME: the expectations of this test case should change when we add support for a
691 // unique scissorRect per renderSurface. In that case, the child1 surface
692 // should be completely unchanged, since we are only transforming it, while the
693 // root surface would be damaged appropriately.
695 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
696 LayerImpl* child1 = root->children()[0];
697 gfx::RectF childDamageRect;
698 gfx::RectF rootDamageRect;
700 clearDamageForAllSurfaces(root.get());
701 child1->setPosition(gfx::PointF(50, 50));
702 emulateDrawingOneFrame(root.get());
703 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
704 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
706 // The new surface bounds should be damaged entirely.
707 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 16, 18), childDamageRect);
709 // The entire child1 surface and the old exposed child1 surface should damage the root surface.
710 // - old child1 surface in target space: gfx::RectF(290, 290, 16, 18)
711 // - new child1 surface in target space: gfx::RectF(240, 240, 16, 18)
712 EXPECT_FLOAT_RECT_EQ(gfx::RectF(240, 240, 66, 68), rootDamageRect);
715 TEST_F(DamageTrackerTest, verifyDamageForAddingAndRemovingRenderSurfaces)
717 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
718 LayerImpl* child1 = root->children()[0];
719 gfx::RectF childDamageRect;
720 gfx::RectF rootDamageRect;
722 // CASE 1: If a descendant surface disappears, its entire old area becomes exposed.
724 clearDamageForAllSurfaces(root.get());
725 child1->setOpacity(1);
726 child1->setForceRenderSurface(false);
727 emulateDrawingOneFrame(root.get());
729 // Sanity check that there is only one surface now.
730 ASSERT_FALSE(child1->renderSurface());
731 ASSERT_EQ(4u, root->renderSurface()->layerList().size());
733 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
734 EXPECT_FLOAT_RECT_EQ(gfx::RectF(290, 290, 16, 18), rootDamageRect);
736 // CASE 2: If a descendant surface appears, its entire old area becomes exposed.
738 // Cycle one frame of no change, just to sanity check that the next rect is not because of the old damage state.
739 clearDamageForAllSurfaces(root.get());
740 emulateDrawingOneFrame(root.get());
741 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
742 EXPECT_TRUE(rootDamageRect.IsEmpty());
744 // Then change the tree so that the render surface is added back.
745 clearDamageForAllSurfaces(root.get());
746 child1->setOpacity(0.5);
747 child1->setForceRenderSurface(true);
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 gfx::Transform 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 gfx::Transform 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 gfx::Transform 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 } // namespace
1161 } // namespace cc