In iossim, ignore harmless messages from launchd.
[chromium-blink-merge.git] / cc / damage_tracker_unittest.cc
blob1d81e5895262030848c0c68895ed4d56bb349533
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_tree_host_common.h"
9 #include "cc/math_util.h"
10 #include "cc/single_thread_proxy.h"
11 #include "cc/test/fake_impl_proxy.h"
12 #include "cc/test/fake_layer_tree_host_impl.h"
13 #include "cc/test/geometry_test_utils.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperation.h"
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebFilterOperations.h"
17 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
18 #include "ui/gfx/quad_f.h"
20 using namespace WebKit;
22 namespace cc {
23 namespace {
25 void executeCalculateDrawProperties(LayerImpl* root, std::vector<LayerImpl*>& renderSurfaceLayerList)
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::calculateDrawProperties(root, root->bounds(), 1, 1, dummyMaxTextureSize, false, 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 executeCalculateDrawProperties(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 class DamageTrackerTest : public testing::Test {
68 public:
69 DamageTrackerTest()
70 : m_hostImpl(&m_proxy)
74 scoped_ptr<LayerImpl> createTestTreeWithOneSurface()
76 scoped_ptr<LayerImpl> root = LayerImpl::create(m_hostImpl.activeTree(), 1);
77 scoped_ptr<LayerImpl> child = LayerImpl::create(m_hostImpl.activeTree(), 2);
79 root->setPosition(gfx::PointF());
80 root->setAnchorPoint(gfx::PointF());
81 root->setBounds(gfx::Size(500, 500));
82 root->setContentBounds(gfx::Size(500, 500));
83 root->setDrawsContent(true);
84 root->createRenderSurface();
85 root->renderSurface()->setContentRect(gfx::Rect(gfx::Point(), gfx::Size(500, 500)));
87 child->setPosition(gfx::PointF(100, 100));
88 child->setAnchorPoint(gfx::PointF());
89 child->setBounds(gfx::Size(30, 30));
90 child->setContentBounds(gfx::Size(30, 30));
91 child->setDrawsContent(true);
92 root->addChild(child.Pass());
94 return root.Pass();
97 scoped_ptr<LayerImpl> createTestTreeWithTwoSurfaces()
99 // This test tree has two render surfaces: one for the root, and one for
100 // child1. Additionally, the root has a second child layer, and child1 has two
101 // children of its own.
103 scoped_ptr<LayerImpl> root = LayerImpl::create(m_hostImpl.activeTree(), 1);
104 scoped_ptr<LayerImpl> child1 = LayerImpl::create(m_hostImpl.activeTree(), 2);
105 scoped_ptr<LayerImpl> child2 = LayerImpl::create(m_hostImpl.activeTree(), 3);
106 scoped_ptr<LayerImpl> grandChild1 = LayerImpl::create(m_hostImpl.activeTree(), 4);
107 scoped_ptr<LayerImpl> grandChild2 = LayerImpl::create(m_hostImpl.activeTree(), 5);
109 root->setPosition(gfx::PointF());
110 root->setAnchorPoint(gfx::PointF());
111 root->setBounds(gfx::Size(500, 500));
112 root->setContentBounds(gfx::Size(500, 500));
113 root->setDrawsContent(true);
114 root->createRenderSurface();
115 root->renderSurface()->setContentRect(gfx::Rect(gfx::Point(), gfx::Size(500, 500)));
117 child1->setPosition(gfx::PointF(100, 100));
118 child1->setAnchorPoint(gfx::PointF());
119 child1->setBounds(gfx::Size(30, 30));
120 child1->setContentBounds(gfx::Size(30, 30));
121 child1->setOpacity(0.5); // with a child that drawsContent, this will cause the layer to create its own renderSurface.
122 child1->setDrawsContent(false); // this layer does not draw, but is intended to create its own renderSurface.
123 child1->setForceRenderSurface(true);
125 child2->setPosition(gfx::PointF(11, 11));
126 child2->setAnchorPoint(gfx::PointF());
127 child2->setBounds(gfx::Size(18, 18));
128 child2->setContentBounds(gfx::Size(18, 18));
129 child2->setDrawsContent(true);
131 grandChild1->setPosition(gfx::PointF(200, 200));
132 grandChild1->setAnchorPoint(gfx::PointF());
133 grandChild1->setBounds(gfx::Size(6, 8));
134 grandChild1->setContentBounds(gfx::Size(6, 8));
135 grandChild1->setDrawsContent(true);
137 grandChild2->setPosition(gfx::PointF(190, 190));
138 grandChild2->setAnchorPoint(gfx::PointF());
139 grandChild2->setBounds(gfx::Size(6, 8));
140 grandChild2->setContentBounds(gfx::Size(6, 8));
141 grandChild2->setDrawsContent(true);
143 child1->addChild(grandChild1.Pass());
144 child1->addChild(grandChild2.Pass());
145 root->addChild(child1.Pass());
146 root->addChild(child2.Pass());
148 return root.Pass();
151 scoped_ptr<LayerImpl> createAndSetUpTestTreeWithOneSurface()
153 scoped_ptr<LayerImpl> root = createTestTreeWithOneSurface();
155 // Setup includes going past the first frame which always damages everything, so
156 // that we can actually perform specific tests.
157 emulateDrawingOneFrame(root.get());
159 return root.Pass();
162 scoped_ptr<LayerImpl> createAndSetUpTestTreeWithTwoSurfaces()
164 scoped_ptr<LayerImpl> root = createTestTreeWithTwoSurfaces();
166 // Setup includes going past the first frame which always damages everything, so
167 // that we can actually perform specific tests.
168 emulateDrawingOneFrame(root.get());
170 return root.Pass();
173 protected:
174 FakeImplProxy m_proxy;
175 FakeLayerTreeHostImpl m_hostImpl;
178 TEST_F(DamageTrackerTest, sanityCheckTestTreeWithOneSurface)
180 // Sanity check that the simple test tree will actually produce the expected render
181 // surfaces and layer lists.
183 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
185 EXPECT_EQ(2u, root->renderSurface()->layerList().size());
186 EXPECT_EQ(1, root->renderSurface()->layerList()[0]->id());
187 EXPECT_EQ(2, root->renderSurface()->layerList()[1]->id());
189 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
190 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 500, 500), rootDamageRect);
193 TEST_F(DamageTrackerTest, sanityCheckTestTreeWithTwoSurfaces)
195 // Sanity check that the complex test tree will actually produce the expected render
196 // surfaces and layer lists.
198 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
200 LayerImpl* child1 = root->children()[0];
201 LayerImpl* child2 = root->children()[1];
202 gfx::RectF childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
203 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
205 ASSERT_TRUE(child1->renderSurface());
206 EXPECT_FALSE(child2->renderSurface());
207 EXPECT_EQ(3u, root->renderSurface()->layerList().size());
208 EXPECT_EQ(2u, child1->renderSurface()->layerList().size());
210 // The render surface for child1 only has a contentRect that encloses grandChild1 and grandChild2, because child1 does not draw content.
211 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 16, 18), childDamageRect);
212 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 500, 500), rootDamageRect);
215 TEST_F(DamageTrackerTest, verifyDamageForUpdateRects)
217 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
218 LayerImpl* child = root->children()[0];
220 // CASE 1: Setting the update rect should cause the corresponding damage to the surface.
222 clearDamageForAllSurfaces(root.get());
223 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
224 emulateDrawingOneFrame(root.get());
226 // Damage position on the surface should be: position of updateRect (10, 11) relative to the child (100, 100).
227 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
228 EXPECT_FLOAT_RECT_EQ(gfx::RectF(110, 111, 12, 13), rootDamageRect);
230 // CASE 2: The same update rect twice in a row still produces the same damage.
232 clearDamageForAllSurfaces(root.get());
233 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
234 emulateDrawingOneFrame(root.get());
235 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
236 EXPECT_FLOAT_RECT_EQ(gfx::RectF(110, 111, 12, 13), rootDamageRect);
238 // CASE 3: Setting a different update rect should cause damage on the new update region, but no additional exposed old region.
240 clearDamageForAllSurfaces(root.get());
241 child->setUpdateRect(gfx::RectF(20, 25, 1, 2));
242 emulateDrawingOneFrame(root.get());
244 // Damage position on the surface should be: position of updateRect (20, 25) relative to the child (100, 100).
245 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
246 EXPECT_FLOAT_RECT_EQ(gfx::RectF(120, 125, 1, 2), rootDamageRect);
249 TEST_F(DamageTrackerTest, verifyDamageForPropertyChanges)
251 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
252 LayerImpl* child = root->children()[0];
254 // CASE 1: The layer's property changed flag takes priority over update rect.
256 clearDamageForAllSurfaces(root.get());
257 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
258 child->setOpacity(0.5);
259 emulateDrawingOneFrame(root.get());
261 // Sanity check - we should not have accidentally created a separate render surface for the translucent layer.
262 ASSERT_FALSE(child->renderSurface());
263 ASSERT_EQ(2u, root->renderSurface()->layerList().size());
265 // Damage should be the entire child layer in targetSurface space.
266 gfx::RectF expectedRect = gfx::RectF(100, 100, 30, 30);
267 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
268 EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
270 // CASE 2: If a layer moves due to property change, it damages both the new location
271 // and the old (exposed) location. The old location is the entire old layer,
272 // not just the updateRect.
274 // Cycle one frame of no change, just to sanity check that the next rect is not because of the old damage state.
275 clearDamageForAllSurfaces(root.get());
276 emulateDrawingOneFrame(root.get());
277 EXPECT_TRUE(root->renderSurface()->damageTracker()->currentDamageRect().IsEmpty());
279 // Then, test the actual layer movement.
280 clearDamageForAllSurfaces(root.get());
281 child->setPosition(gfx::PointF(200, 230));
282 emulateDrawingOneFrame(root.get());
284 // Expect damage to be the combination of the previous one and the new one.
285 expectedRect.Union(gfx::RectF(200, 230, 30, 30));
286 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
287 EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
290 TEST_F(DamageTrackerTest, verifyDamageForTransformedLayer)
292 // If a layer is transformed, the damage rect should still enclose the entire
293 // transformed layer.
295 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
296 LayerImpl* child = root->children()[0];
298 gfx::Transform rotation;
299 rotation.Rotate(45);
301 clearDamageForAllSurfaces(root.get());
302 child->setAnchorPoint(gfx::PointF(0.5, 0.5));
303 child->setPosition(gfx::PointF(85, 85));
304 emulateDrawingOneFrame(root.get());
306 // Sanity check that the layer actually moved to (85, 85), damaging its old location and new location.
307 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
308 EXPECT_FLOAT_RECT_EQ(gfx::RectF(85, 85, 45, 45), rootDamageRect);
310 // With the anchor on the layer's center, now we can test the rotation more
311 // intuitively, since it applies about the layer's anchor.
312 clearDamageForAllSurfaces(root.get());
313 child->setTransform(rotation);
314 emulateDrawingOneFrame(root.get());
316 // Since the child layer is square, rotation by 45 degrees about the center should
317 // increase the size of the expected rect by sqrt(2), centered around (100, 100). The
318 // old exposed region should be fully contained in the new region.
319 double expectedWidth = 30 * sqrt(2.0);
320 double expectedPosition = 100 - 0.5 * expectedWidth;
321 gfx::RectF expectedRect(expectedPosition, expectedPosition, expectedWidth, expectedWidth);
322 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
323 EXPECT_FLOAT_RECT_EQ(expectedRect, rootDamageRect);
326 TEST_F(DamageTrackerTest, verifyDamageForPerspectiveClippedLayer)
328 // If a layer has a perspective transform that causes w < 0, then not clipping the
329 // layer can cause an invalid damage rect. This test checks that the w < 0 case is
330 // tracked properly.
332 // The transform is constructed so that if w < 0 clipping is not performed, the
333 // incorrect rect will be very small, specifically: position (500.972504, 498.544617) and size 0.056610 x 2.910767.
334 // Instead, the correctly transformed rect should actually be very huge (i.e. in theory, -infinity on the left),
335 // and positioned so that the right-most bound rect will be approximately 501 units in root surface space.
338 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
339 LayerImpl* child = root->children()[0];
341 gfx::Transform transform;
342 transform.Translate3d(500, 500, 0);
343 transform.ApplyPerspectiveDepth(1);
344 MathUtil::rotateEulerAngles(&transform, 0, 45, 0);
345 transform.Translate3d(-50, -50, 0);
347 // Set up the child
348 child->setPosition(gfx::PointF(0, 0));
349 child->setBounds(gfx::Size(100, 100));
350 child->setContentBounds(gfx::Size(100, 100));
351 child->setTransform(transform);
352 emulateDrawingOneFrame(root.get());
354 // Sanity check that the child layer's bounds would actually get clipped by w < 0,
355 // otherwise this test is not actually testing the intended scenario.
356 gfx::QuadF testQuad(gfx::RectF(gfx::PointF(), gfx::SizeF(100, 100)));
357 bool clipped = false;
358 MathUtil::mapQuad(transform, testQuad, clipped);
359 EXPECT_TRUE(clipped);
361 // Damage the child without moving it.
362 clearDamageForAllSurfaces(root.get());
363 child->setOpacity(0.5);
364 emulateDrawingOneFrame(root.get());
366 // The expected damage should cover the entire root surface (500x500), but we don't
367 // care whether the damage rect was clamped or is larger than the surface for this test.
368 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
369 gfx::RectF damageWeCareAbout = gfx::RectF(gfx::PointF(), gfx::SizeF(500, 500));
370 EXPECT_TRUE(rootDamageRect.Contains(damageWeCareAbout));
373 TEST_F(DamageTrackerTest, verifyDamageForBlurredSurface)
375 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
376 LayerImpl* child = root->children()[0];
378 WebFilterOperations filters;
379 filters.append(WebFilterOperation::createBlurFilter(5));
380 int outsetTop, outsetRight, outsetBottom, outsetLeft;
381 filters.getOutsets(outsetTop, outsetRight, outsetBottom, outsetLeft);
383 // Setting the filter will damage the whole surface.
384 clearDamageForAllSurfaces(root.get());
385 root->setFilters(filters);
386 emulateDrawingOneFrame(root.get());
388 // Setting the update rect should cause the corresponding damage to the surface, blurred based on the size of the blur filter.
389 clearDamageForAllSurfaces(root.get());
390 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
391 emulateDrawingOneFrame(root.get());
393 // Damage position on the surface should be: position of updateRect (10, 11) relative to the child (100, 100), but expanded by the blur outsets.
394 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
395 gfx::RectF expectedDamageRect = gfx::RectF(110, 111, 12, 13);
396 expectedDamageRect.Inset(-outsetLeft, -outsetTop, -outsetRight, -outsetBottom);
397 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
400 TEST_F(DamageTrackerTest, verifyDamageForImageFilter)
402 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
403 LayerImpl* child = root->children()[0];
404 gfx::RectF rootDamageRect, childDamageRect;
406 // Allow us to set damage on child too.
407 child->setDrawsContent(true);
409 skia::RefPtr<SkImageFilter> filter = skia::AdoptRef(new SkBlurImageFilter(SkIntToScalar(2), SkIntToScalar(2)));
410 // Setting the filter will damage the whole surface.
411 clearDamageForAllSurfaces(root.get());
412 child->setFilter(filter);
413 emulateDrawingOneFrame(root.get());
414 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
415 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
416 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 30, 30), rootDamageRect);
417 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
419 // CASE 1: Setting the update rect should damage the whole surface (for now)
420 clearDamageForAllSurfaces(root.get());
421 child->setUpdateRect(gfx::RectF(0, 0, 1, 1));
422 emulateDrawingOneFrame(root.get());
424 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
425 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
426 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 30, 30), rootDamageRect);
427 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
430 TEST_F(DamageTrackerTest, verifyDamageForBackgroundBlurredChild)
432 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
433 LayerImpl* child1 = root->children()[0];
434 LayerImpl* child2 = root->children()[1];
436 // Allow us to set damage on child1 too.
437 child1->setDrawsContent(true);
439 WebFilterOperations filters;
440 filters.append(WebFilterOperation::createBlurFilter(2));
441 int outsetTop, outsetRight, outsetBottom, outsetLeft;
442 filters.getOutsets(outsetTop, outsetRight, outsetBottom, outsetLeft);
444 // Setting the filter will damage the whole surface.
445 clearDamageForAllSurfaces(root.get());
446 child1->setBackgroundFilters(filters);
447 emulateDrawingOneFrame(root.get());
449 // CASE 1: Setting the update rect should cause the corresponding damage to
450 // the surface, blurred based on the size of the child's background blur
451 // filter.
452 clearDamageForAllSurfaces(root.get());
453 root->setUpdateRect(gfx::RectF(297, 297, 2, 2));
454 emulateDrawingOneFrame(root.get());
456 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
457 // Damage position on the surface should be a composition of the damage on the root and on child2.
458 // Damage on the root should be: position of updateRect (297, 297), but expanded by the blur outsets.
459 gfx::RectF expectedDamageRect = gfx::RectF(297, 297, 2, 2);
460 expectedDamageRect.Inset(-outsetLeft, -outsetTop, -outsetRight, -outsetBottom);
461 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
463 // CASE 2: Setting the update rect should cause the corresponding damage to
464 // the surface, blurred based on the size of the child's background blur
465 // filter. Since the damage extends to the right/bottom outside of the
466 // blurred layer, only the left/top should end up expanded.
467 clearDamageForAllSurfaces(root.get());
468 root->setUpdateRect(gfx::RectF(297, 297, 30, 30));
469 emulateDrawingOneFrame(root.get());
471 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
472 // Damage position on the surface should be a composition of the damage on the root and on child2.
473 // Damage on the root should be: position of updateRect (297, 297), but expanded on the left/top
474 // by the blur outsets.
475 expectedDamageRect = gfx::RectF(297, 297, 30, 30);
476 expectedDamageRect.Inset(-outsetLeft, -outsetTop, 0, 0);
477 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
479 // CASE 3: Setting this update rect outside the blurred contentBounds of the blurred
480 // child1 will not cause it to be expanded.
481 clearDamageForAllSurfaces(root.get());
482 root->setUpdateRect(gfx::RectF(30, 30, 2, 2));
483 emulateDrawingOneFrame(root.get());
485 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
486 // Damage on the root should be: position of updateRect (30, 30), not
487 // expanded.
488 expectedDamageRect = gfx::RectF(30, 30, 2, 2);
489 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
491 // CASE 4: Setting this update rect inside the blurred contentBounds but outside the
492 // original contentBounds of the blurred child1 will cause it to be expanded.
493 clearDamageForAllSurfaces(root.get());
494 root->setUpdateRect(gfx::RectF(99, 99, 1, 1));
495 emulateDrawingOneFrame(root.get());
497 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
498 // Damage on the root should be: position of updateRect (99, 99), expanded
499 // by the blurring on child1, but since it is 1 pixel outside the layer, the
500 // expanding should be reduced by 1.
501 expectedDamageRect = gfx::RectF(99, 99, 1, 1);
502 expectedDamageRect.Inset(-outsetLeft + 1, -outsetTop + 1, -outsetRight, -outsetBottom);
503 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
505 // CASE 5: Setting the update rect on child2, which is above child1, will
506 // not get blurred by child1, so it does not need to get expanded.
507 clearDamageForAllSurfaces(root.get());
508 child2->setUpdateRect(gfx::RectF(0, 0, 1, 1));
509 emulateDrawingOneFrame(root.get());
511 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
512 // Damage on child2 should be: position of updateRect offset by the child's position (11, 11), and not expanded by anything.
513 expectedDamageRect = gfx::RectF(11, 11, 1, 1);
514 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
516 // CASE 6: Setting the update rect on child1 will also blur the damage, so
517 // that any pixels needed for the blur are redrawn in the current frame.
518 clearDamageForAllSurfaces(root.get());
519 child1->setUpdateRect(gfx::RectF(0, 0, 1, 1));
520 emulateDrawingOneFrame(root.get());
522 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
523 // Damage on child1 should be: position of updateRect offset by the child's position (100, 100), and expanded by the damage.
524 expectedDamageRect = gfx::RectF(100, 100, 1, 1);
525 expectedDamageRect.Inset(-outsetLeft, -outsetTop, -outsetRight, -outsetBottom);
526 EXPECT_FLOAT_RECT_EQ(expectedDamageRect, rootDamageRect);
529 TEST_F(DamageTrackerTest, verifyDamageForAddingAndRemovingLayer)
531 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
532 LayerImpl* child1 = root->children()[0];
534 // CASE 1: Adding a new layer should cause the appropriate damage.
536 clearDamageForAllSurfaces(root.get());
538 scoped_ptr<LayerImpl> child2 = LayerImpl::create(m_hostImpl.activeTree(), 3);
539 child2->setPosition(gfx::PointF(400, 380));
540 child2->setAnchorPoint(gfx::PointF());
541 child2->setBounds(gfx::Size(6, 8));
542 child2->setContentBounds(gfx::Size(6, 8));
543 child2->setDrawsContent(true);
544 root->addChild(child2.Pass());
546 emulateDrawingOneFrame(root.get());
548 // Sanity check - all 3 layers should be on the same render surface; render surfaces are tested elsewhere.
549 ASSERT_EQ(3u, root->renderSurface()->layerList().size());
551 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
552 EXPECT_FLOAT_RECT_EQ(gfx::RectF(400, 380, 6, 8), rootDamageRect);
554 // CASE 2: If the layer is removed, its entire old layer becomes exposed, not just the
555 // last update rect.
557 // Advance one frame without damage so that we know the damage rect is not leftover from the previous case.
558 clearDamageForAllSurfaces(root.get());
559 emulateDrawingOneFrame(root.get());
560 EXPECT_TRUE(root->renderSurface()->damageTracker()->currentDamageRect().IsEmpty());
562 // Then, test removing child1.
563 root->removeChild(child1);
564 child1 = NULL;
565 emulateDrawingOneFrame(root.get());
566 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
567 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 30, 30), rootDamageRect);
570 TEST_F(DamageTrackerTest, verifyDamageForNewUnchangedLayer)
572 // If child2 is added to the layer tree, but it doesn't have any explicit damage of
573 // its own, it should still indeed damage the target surface.
575 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
577 clearDamageForAllSurfaces(root.get());
579 scoped_ptr<LayerImpl> child2 = LayerImpl::create(m_hostImpl.activeTree(), 3);
580 child2->setPosition(gfx::PointF(400, 380));
581 child2->setAnchorPoint(gfx::PointF());
582 child2->setBounds(gfx::Size(6, 8));
583 child2->setContentBounds(gfx::Size(6, 8));
584 child2->setDrawsContent(true);
585 child2->resetAllChangeTrackingForSubtree();
586 // Sanity check the initial conditions of the test, if these asserts trigger, it
587 // means the test no longer actually covers the intended scenario.
588 ASSERT_FALSE(child2->layerPropertyChanged());
589 ASSERT_TRUE(child2->updateRect().IsEmpty());
590 root->addChild(child2.Pass());
592 emulateDrawingOneFrame(root.get());
594 // Sanity check - all 3 layers should be on the same render surface; render surfaces are tested elsewhere.
595 ASSERT_EQ(3u, root->renderSurface()->layerList().size());
597 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
598 EXPECT_FLOAT_RECT_EQ(gfx::RectF(400, 380, 6, 8), rootDamageRect);
601 TEST_F(DamageTrackerTest, verifyDamageForMultipleLayers)
603 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
604 LayerImpl* child1 = root->children()[0];
606 // In this test we don't want the above tree manipulation to be considered part of the same frame.
607 clearDamageForAllSurfaces(root.get());
609 scoped_ptr<LayerImpl> child2 = LayerImpl::create(m_hostImpl.activeTree(), 3);
610 child2->setPosition(gfx::PointF(400, 380));
611 child2->setAnchorPoint(gfx::PointF());
612 child2->setBounds(gfx::Size(6, 8));
613 child2->setContentBounds(gfx::Size(6, 8));
614 child2->setDrawsContent(true);
615 root->addChild(child2.Pass());
617 LayerImpl* child2 = root->children()[1];
618 emulateDrawingOneFrame(root.get());
620 // Damaging two layers simultaneously should cause combined damage.
621 // - child1 update rect in surface space: gfx::RectF(100, 100, 1, 2);
622 // - child2 update rect in surface space: gfx::RectF(400, 380, 3, 4);
623 clearDamageForAllSurfaces(root.get());
624 child1->setUpdateRect(gfx::RectF(0, 0, 1, 2));
625 child2->setUpdateRect(gfx::RectF(0, 0, 3, 4));
626 emulateDrawingOneFrame(root.get());
627 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
628 EXPECT_FLOAT_RECT_EQ(gfx::RectF(100, 100, 303, 284), rootDamageRect);
631 TEST_F(DamageTrackerTest, verifyDamageForNestedSurfaces)
633 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
634 LayerImpl* child1 = root->children()[0];
635 LayerImpl* child2 = root->children()[1];
636 LayerImpl* grandChild1 = root->children()[0]->children()[0];
637 gfx::RectF childDamageRect;
638 gfx::RectF rootDamageRect;
640 // CASE 1: Damage to a descendant surface should propagate properly to ancestor surface.
642 clearDamageForAllSurfaces(root.get());
643 grandChild1->setOpacity(0.5);
644 emulateDrawingOneFrame(root.get());
645 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
646 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
647 EXPECT_FLOAT_RECT_EQ(gfx::RectF(200, 200, 6, 8), childDamageRect);
648 EXPECT_FLOAT_RECT_EQ(gfx::RectF(300, 300, 6, 8), rootDamageRect);
650 // CASE 2: Same as previous case, but with additional damage elsewhere that should be properly unioned.
651 // - child1 surface damage in root surface space: gfx::RectF(300, 300, 6, 8);
652 // - child2 damage in root surface space: gfx::RectF(11, 11, 18, 18);
653 clearDamageForAllSurfaces(root.get());
654 grandChild1->setOpacity(0.7f);
655 child2->setOpacity(0.7f);
656 emulateDrawingOneFrame(root.get());
657 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
658 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
659 EXPECT_FLOAT_RECT_EQ(gfx::RectF(200, 200, 6, 8), childDamageRect);
660 EXPECT_FLOAT_RECT_EQ(gfx::RectF(11, 11, 295, 297), rootDamageRect);
663 TEST_F(DamageTrackerTest, verifyDamageForSurfaceChangeFromDescendantLayer)
665 // If descendant layer changes and affects the content bounds of the render surface,
666 // then the entire descendant surface should be damaged, and it should damage its
667 // ancestor surface with the old and new surface regions.
669 // This is a tricky case, since only the first grandChild changes, but the entire
670 // surface should be marked dirty.
672 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
673 LayerImpl* child1 = root->children()[0];
674 LayerImpl* grandChild1 = root->children()[0]->children()[0];
675 gfx::RectF childDamageRect;
676 gfx::RectF rootDamageRect;
678 clearDamageForAllSurfaces(root.get());
679 grandChild1->setPosition(gfx::PointF(195, 205));
680 emulateDrawingOneFrame(root.get());
681 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
682 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
684 // The new surface bounds should be damaged entirely, even though only one of the layers changed.
685 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 11, 23), childDamageRect);
687 // Damage to the root surface should be the union of child1's *entire* render surface
688 // (in target space), and its old exposed area (also in target space).
689 EXPECT_FLOAT_RECT_EQ(gfx::RectF(290, 290, 16, 23), rootDamageRect);
692 TEST_F(DamageTrackerTest, verifyDamageForSurfaceChangeFromAncestorLayer)
694 // An ancestor/owning layer changes that affects the position/transform of the render
695 // surface. Note that in this case, the layerPropertyChanged flag already propagates
696 // to the subtree (tested in LayerImpltest), which damages the entire child1
697 // surface, but the damage tracker still needs the correct logic to compute the
698 // exposed region on the root surface.
700 // FIXME: the expectations of this test case should change when we add support for a
701 // unique scissorRect per renderSurface. In that case, the child1 surface
702 // should be completely unchanged, since we are only transforming it, while the
703 // root surface would be damaged appropriately.
705 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
706 LayerImpl* child1 = root->children()[0];
707 gfx::RectF childDamageRect;
708 gfx::RectF rootDamageRect;
710 clearDamageForAllSurfaces(root.get());
711 child1->setPosition(gfx::PointF(50, 50));
712 emulateDrawingOneFrame(root.get());
713 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
714 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
716 // The new surface bounds should be damaged entirely.
717 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 16, 18), childDamageRect);
719 // The entire child1 surface and the old exposed child1 surface should damage the root surface.
720 // - old child1 surface in target space: gfx::RectF(290, 290, 16, 18)
721 // - new child1 surface in target space: gfx::RectF(240, 240, 16, 18)
722 EXPECT_FLOAT_RECT_EQ(gfx::RectF(240, 240, 66, 68), rootDamageRect);
725 TEST_F(DamageTrackerTest, verifyDamageForAddingAndRemovingRenderSurfaces)
727 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
728 LayerImpl* child1 = root->children()[0];
729 gfx::RectF childDamageRect;
730 gfx::RectF rootDamageRect;
732 // CASE 1: If a descendant surface disappears, its entire old area becomes exposed.
734 clearDamageForAllSurfaces(root.get());
735 child1->setOpacity(1);
736 child1->setForceRenderSurface(false);
737 emulateDrawingOneFrame(root.get());
739 // Sanity check that there is only one surface now.
740 ASSERT_FALSE(child1->renderSurface());
741 ASSERT_EQ(4u, root->renderSurface()->layerList().size());
743 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
744 EXPECT_FLOAT_RECT_EQ(gfx::RectF(290, 290, 16, 18), rootDamageRect);
746 // CASE 2: If a descendant surface appears, its entire old area becomes exposed.
748 // Cycle one frame of no change, just to sanity check that the next rect is not because of the old damage state.
749 clearDamageForAllSurfaces(root.get());
750 emulateDrawingOneFrame(root.get());
751 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
752 EXPECT_TRUE(rootDamageRect.IsEmpty());
754 // Then change the tree so that the render surface is added back.
755 clearDamageForAllSurfaces(root.get());
756 child1->setOpacity(0.5);
757 child1->setForceRenderSurface(true);
758 emulateDrawingOneFrame(root.get());
760 // Sanity check that there is a new surface now.
761 ASSERT_TRUE(child1->renderSurface());
762 EXPECT_EQ(3u, root->renderSurface()->layerList().size());
763 EXPECT_EQ(2u, child1->renderSurface()->layerList().size());
765 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
766 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
767 EXPECT_FLOAT_RECT_EQ(gfx::RectF(190, 190, 16, 18), childDamageRect);
768 EXPECT_FLOAT_RECT_EQ(gfx::RectF(290, 290, 16, 18), rootDamageRect);
771 TEST_F(DamageTrackerTest, verifyNoDamageWhenNothingChanged)
773 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
774 LayerImpl* child1 = root->children()[0];
775 gfx::RectF childDamageRect;
776 gfx::RectF rootDamageRect;
778 // CASE 1: If nothing changes, the damage rect should be empty.
780 clearDamageForAllSurfaces(root.get());
781 emulateDrawingOneFrame(root.get());
782 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
783 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
784 EXPECT_TRUE(childDamageRect.IsEmpty());
785 EXPECT_TRUE(rootDamageRect.IsEmpty());
787 // CASE 2: If nothing changes twice in a row, the damage rect should still be empty.
789 clearDamageForAllSurfaces(root.get());
790 emulateDrawingOneFrame(root.get());
791 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
792 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
793 EXPECT_TRUE(childDamageRect.IsEmpty());
794 EXPECT_TRUE(rootDamageRect.IsEmpty());
797 TEST_F(DamageTrackerTest, verifyNoDamageForUpdateRectThatDoesNotDrawContent)
799 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
800 LayerImpl* child1 = root->children()[0];
801 gfx::RectF childDamageRect;
802 gfx::RectF rootDamageRect;
804 // In our specific tree, the update rect of child1 should not cause any damage to any
805 // surface because it does not actually draw content.
806 clearDamageForAllSurfaces(root.get());
807 child1->setUpdateRect(gfx::RectF(0, 0, 1, 2));
808 emulateDrawingOneFrame(root.get());
809 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
810 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
811 EXPECT_TRUE(childDamageRect.IsEmpty());
812 EXPECT_TRUE(rootDamageRect.IsEmpty());
815 TEST_F(DamageTrackerTest, verifyDamageForReplica)
817 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
818 LayerImpl* child1 = root->children()[0];
819 LayerImpl* grandChild1 = child1->children()[0];
820 LayerImpl* grandChild2 = child1->children()[1];
822 // Damage on a surface that has a reflection should cause the target surface to
823 // receive the surface's damage and the surface's reflected damage.
825 // For this test case, we modify grandChild2, and add grandChild3 to extend the bounds
826 // of child1's surface. This way, we can test reflection changes without changing
827 // contentBounds of the surface.
828 grandChild2->setPosition(gfx::PointF(180, 180));
830 scoped_ptr<LayerImpl> grandChild3 = LayerImpl::create(m_hostImpl.activeTree(), 6);
831 grandChild3->setPosition(gfx::PointF(240, 240));
832 grandChild3->setAnchorPoint(gfx::PointF());
833 grandChild3->setBounds(gfx::Size(10, 10));
834 grandChild3->setContentBounds(gfx::Size(10, 10));
835 grandChild3->setDrawsContent(true);
836 child1->addChild(grandChild3.Pass());
838 child1->setOpacity(0.5);
839 emulateDrawingOneFrame(root.get());
841 // CASE 1: adding a reflection about the left edge of grandChild1.
843 clearDamageForAllSurfaces(root.get());
845 scoped_ptr<LayerImpl> grandChild1Replica = LayerImpl::create(m_hostImpl.activeTree(), 7);
846 grandChild1Replica->setPosition(gfx::PointF());
847 grandChild1Replica->setAnchorPoint(gfx::PointF());
848 gfx::Transform reflection;
849 reflection.Scale3d(-1, 1, 1);
850 grandChild1Replica->setTransform(reflection);
851 grandChild1->setReplicaLayer(grandChild1Replica.Pass());
853 emulateDrawingOneFrame(root.get());
855 gfx::RectF grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
856 gfx::RectF childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
857 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
859 // The grandChild surface damage should not include its own replica. The child
860 // surface damage should include the normal and replica surfaces.
861 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 6, 8), grandChildDamageRect);
862 EXPECT_FLOAT_RECT_EQ(gfx::RectF(194, 200, 12, 8), childDamageRect);
863 EXPECT_FLOAT_RECT_EQ(gfx::RectF(294, 300, 12, 8), rootDamageRect);
865 // CASE 2: moving the descendant surface should cause both the original and reflected
866 // areas to be damaged on the target.
867 clearDamageForAllSurfaces(root.get());
868 gfx::Rect oldContentRect = child1->renderSurface()->contentRect();
869 grandChild1->setPosition(gfx::PointF(195, 205));
870 emulateDrawingOneFrame(root.get());
871 ASSERT_EQ(oldContentRect.width(), child1->renderSurface()->contentRect().width());
872 ASSERT_EQ(oldContentRect.height(), child1->renderSurface()->contentRect().height());
874 grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
875 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
876 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
878 // The child surface damage should include normal and replica surfaces for both old and new locations.
879 // - old location in target space: gfx::RectF(194, 200, 12, 8)
880 // - new location in target space: gfx::RectF(189, 205, 12, 8)
881 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 6, 8), grandChildDamageRect);
882 EXPECT_FLOAT_RECT_EQ(gfx::RectF(189, 200, 17, 13), childDamageRect);
883 EXPECT_FLOAT_RECT_EQ(gfx::RectF(289, 300, 17, 13), rootDamageRect);
885 // CASE 3: removing the reflection should cause the entire region including reflection
886 // to damage the target surface.
887 clearDamageForAllSurfaces(root.get());
888 grandChild1->setReplicaLayer(scoped_ptr<LayerImpl>());
889 emulateDrawingOneFrame(root.get());
890 ASSERT_EQ(oldContentRect.width(), child1->renderSurface()->contentRect().width());
891 ASSERT_EQ(oldContentRect.height(), child1->renderSurface()->contentRect().height());
893 EXPECT_FALSE(grandChild1->renderSurface());
894 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
895 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
897 EXPECT_FLOAT_RECT_EQ(gfx::RectF(189, 205, 12, 8), childDamageRect);
898 EXPECT_FLOAT_RECT_EQ(gfx::RectF(289, 305, 12, 8), rootDamageRect);
901 TEST_F(DamageTrackerTest, verifyDamageForMask)
903 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
904 LayerImpl* child = root->children()[0];
906 // In the current implementation of the damage tracker, changes to mask layers should
907 // damage the entire corresponding surface.
909 clearDamageForAllSurfaces(root.get());
911 // Set up the mask layer.
913 scoped_ptr<LayerImpl> maskLayer = LayerImpl::create(m_hostImpl.activeTree(), 3);
914 maskLayer->setPosition(child->position());
915 maskLayer->setAnchorPoint(gfx::PointF());
916 maskLayer->setBounds(child->bounds());
917 maskLayer->setContentBounds(child->bounds());
918 child->setMaskLayer(maskLayer.Pass());
920 LayerImpl* maskLayer = child->maskLayer();
922 // Add opacity and a grandChild so that the render surface persists even after we remove the mask.
923 child->setOpacity(0.5);
925 scoped_ptr<LayerImpl> grandChild = LayerImpl::create(m_hostImpl.activeTree(), 4);
926 grandChild->setPosition(gfx::PointF(2, 2));
927 grandChild->setAnchorPoint(gfx::PointF());
928 grandChild->setBounds(gfx::Size(2, 2));
929 grandChild->setContentBounds(gfx::Size(2, 2));
930 grandChild->setDrawsContent(true);
931 child->addChild(grandChild.Pass());
933 emulateDrawingOneFrame(root.get());
935 // Sanity check that a new surface was created for the child.
936 ASSERT_TRUE(child->renderSurface());
938 // CASE 1: the updateRect on a mask layer should damage the entire target surface.
940 clearDamageForAllSurfaces(root.get());
941 maskLayer->setUpdateRect(gfx::RectF(1, 2, 3, 4));
942 emulateDrawingOneFrame(root.get());
943 gfx::RectF childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
944 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
946 // CASE 2: a property change on the mask layer should damage the entire target surface.
949 // Advance one frame without damage so that we know the damage rect is not leftover from the previous case.
950 clearDamageForAllSurfaces(root.get());
951 emulateDrawingOneFrame(root.get());
952 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
953 EXPECT_TRUE(childDamageRect.IsEmpty());
955 // Then test the property change.
956 clearDamageForAllSurfaces(root.get());
957 maskLayer->setStackingOrderChanged(true);
959 emulateDrawingOneFrame(root.get());
960 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
961 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
963 // CASE 3: removing the mask also damages the entire target surface.
966 // Advance one frame without damage so that we know the damage rect is not leftover from the previous case.
967 clearDamageForAllSurfaces(root.get());
968 emulateDrawingOneFrame(root.get());
969 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
970 EXPECT_TRUE(childDamageRect.IsEmpty());
972 // Then test mask removal.
973 clearDamageForAllSurfaces(root.get());
974 child->setMaskLayer(scoped_ptr<LayerImpl>());
975 ASSERT_TRUE(child->layerPropertyChanged());
976 emulateDrawingOneFrame(root.get());
978 // Sanity check that a render surface still exists.
979 ASSERT_TRUE(child->renderSurface());
981 childDamageRect = child->renderSurface()->damageTracker()->currentDamageRect();
982 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 30, 30), childDamageRect);
985 TEST_F(DamageTrackerTest, verifyDamageForReplicaMask)
987 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
988 LayerImpl* child1 = root->children()[0];
989 LayerImpl* grandChild1 = child1->children()[0];
991 // Changes to a replica's mask should not damage the original surface, because it is
992 // not masked. But it does damage the ancestor target surface.
994 clearDamageForAllSurfaces(root.get());
996 // Create a reflection about the left edge of grandChild1.
998 scoped_ptr<LayerImpl> grandChild1Replica = LayerImpl::create(m_hostImpl.activeTree(), 6);
999 grandChild1Replica->setPosition(gfx::PointF());
1000 grandChild1Replica->setAnchorPoint(gfx::PointF());
1001 gfx::Transform reflection;
1002 reflection.Scale3d(-1, 1, 1);
1003 grandChild1Replica->setTransform(reflection);
1004 grandChild1->setReplicaLayer(grandChild1Replica.Pass());
1006 LayerImpl* grandChild1Replica = grandChild1->replicaLayer();
1008 // Set up the mask layer on the replica layer
1010 scoped_ptr<LayerImpl> replicaMaskLayer = LayerImpl::create(m_hostImpl.activeTree(), 7);
1011 replicaMaskLayer->setPosition(gfx::PointF());
1012 replicaMaskLayer->setAnchorPoint(gfx::PointF());
1013 replicaMaskLayer->setBounds(grandChild1->bounds());
1014 replicaMaskLayer->setContentBounds(grandChild1->bounds());
1015 grandChild1Replica->setMaskLayer(replicaMaskLayer.Pass());
1017 LayerImpl* replicaMaskLayer = grandChild1Replica->maskLayer();
1019 emulateDrawingOneFrame(root.get());
1021 // Sanity check that the appropriate render surfaces were created
1022 ASSERT_TRUE(grandChild1->renderSurface());
1024 // CASE 1: a property change on the mask should damage only the reflected region on the target surface.
1025 clearDamageForAllSurfaces(root.get());
1026 replicaMaskLayer->setStackingOrderChanged(true);
1027 emulateDrawingOneFrame(root.get());
1029 gfx::RectF grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
1030 gfx::RectF childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
1032 EXPECT_TRUE(grandChildDamageRect.IsEmpty());
1033 EXPECT_FLOAT_RECT_EQ(gfx::RectF(194, 200, 6, 8), childDamageRect);
1035 // CASE 2: removing the replica mask damages only the reflected region on the target surface.
1037 clearDamageForAllSurfaces(root.get());
1038 grandChild1Replica->setMaskLayer(scoped_ptr<LayerImpl>());
1039 emulateDrawingOneFrame(root.get());
1041 grandChildDamageRect = grandChild1->renderSurface()->damageTracker()->currentDamageRect();
1042 childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
1044 EXPECT_TRUE(grandChildDamageRect.IsEmpty());
1045 EXPECT_FLOAT_RECT_EQ(gfx::RectF(194, 200, 6, 8), childDamageRect);
1048 TEST_F(DamageTrackerTest, verifyDamageForReplicaMaskWithAnchor)
1050 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithTwoSurfaces();
1051 LayerImpl* child1 = root->children()[0];
1052 LayerImpl* grandChild1 = child1->children()[0];
1054 // Verify that the correct replicaOriginTransform is used for the replicaMask;
1055 clearDamageForAllSurfaces(root.get());
1057 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.
1060 scoped_ptr<LayerImpl> grandChild1Replica = LayerImpl::create(m_hostImpl.activeTree(), 6);
1061 grandChild1Replica->setPosition(gfx::PointF());
1062 grandChild1Replica->setAnchorPoint(gfx::PointF(1, 0)); // This is the anchor being tested.
1063 gfx::Transform reflection;
1064 reflection.Scale3d(-1, 1, 1);
1065 grandChild1Replica->setTransform(reflection);
1066 grandChild1->setReplicaLayer(grandChild1Replica.Pass());
1068 LayerImpl* grandChild1Replica = grandChild1->replicaLayer();
1070 // Set up the mask layer on the replica layer
1072 scoped_ptr<LayerImpl> replicaMaskLayer = LayerImpl::create(m_hostImpl.activeTree(), 7);
1073 replicaMaskLayer->setPosition(gfx::PointF());
1074 replicaMaskLayer->setAnchorPoint(gfx::PointF()); // note, this is not the anchor being tested.
1075 replicaMaskLayer->setBounds(grandChild1->bounds());
1076 replicaMaskLayer->setContentBounds(grandChild1->bounds());
1077 grandChild1Replica->setMaskLayer(replicaMaskLayer.Pass());
1079 LayerImpl* replicaMaskLayer = grandChild1Replica->maskLayer();
1081 emulateDrawingOneFrame(root.get());
1083 // Sanity check that the appropriate render surfaces were created
1084 ASSERT_TRUE(grandChild1->renderSurface());
1086 // A property change on the replicaMask should damage the reflected region on the target surface.
1087 clearDamageForAllSurfaces(root.get());
1088 replicaMaskLayer->setStackingOrderChanged(true);
1090 emulateDrawingOneFrame(root.get());
1092 gfx::RectF childDamageRect = child1->renderSurface()->damageTracker()->currentDamageRect();
1093 EXPECT_FLOAT_RECT_EQ(gfx::RectF(206, 200, 6, 8), childDamageRect);
1096 TEST_F(DamageTrackerTest, verifyDamageWhenForcedFullDamage)
1098 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
1099 LayerImpl* child = root->children()[0];
1101 // Case 1: This test ensures that when the tracker is forced to have full damage, that
1102 // it takes priority over any other partial damage.
1104 clearDamageForAllSurfaces(root.get());
1105 child->setUpdateRect(gfx::RectF(10, 11, 12, 13));
1106 root->renderSurface()->damageTracker()->forceFullDamageNextUpdate();
1107 emulateDrawingOneFrame(root.get());
1108 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1109 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 500, 500), rootDamageRect);
1111 // Case 2: An additional sanity check that forcing full damage works even when nothing
1112 // on the layer tree changed.
1114 clearDamageForAllSurfaces(root.get());
1115 root->renderSurface()->damageTracker()->forceFullDamageNextUpdate();
1116 emulateDrawingOneFrame(root.get());
1117 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1118 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0, 0, 500, 500), rootDamageRect);
1121 TEST_F(DamageTrackerTest, verifyDamageForEmptyLayerList)
1123 // Though it should never happen, its a good idea to verify that the damage tracker
1124 // does not crash when it receives an empty layerList.
1126 scoped_ptr<LayerImpl> root = LayerImpl::create(m_hostImpl.activeTree(), 1);
1127 root->createRenderSurface();
1129 ASSERT_TRUE(root == root->renderTarget());
1130 RenderSurfaceImpl* targetSurface = root->renderSurface();
1131 targetSurface->clearLayerLists();
1132 targetSurface->damageTracker()->updateDamageTrackingState(targetSurface->layerList(), targetSurface->owningLayerId(), false, gfx::Rect(), 0, WebFilterOperations(), 0);
1134 gfx::RectF damageRect = targetSurface->damageTracker()->currentDamageRect();
1135 EXPECT_TRUE(damageRect.IsEmpty());
1138 TEST_F(DamageTrackerTest, verifyDamageAccumulatesUntilReset)
1140 // If damage is not cleared, it should accumulate.
1142 scoped_ptr<LayerImpl> root = createAndSetUpTestTreeWithOneSurface();
1143 LayerImpl* child = root->children()[0];
1145 clearDamageForAllSurfaces(root.get());
1146 child->setUpdateRect(gfx::RectF(10, 11, 1, 2));
1147 emulateDrawingOneFrame(root.get());
1149 // Sanity check damage after the first frame; this isnt the actual test yet.
1150 gfx::RectF rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1151 EXPECT_FLOAT_RECT_EQ(gfx::RectF(110, 111, 1, 2), rootDamageRect);
1153 // New damage, without having cleared the previous damage, should be unioned to the previous one.
1154 child->setUpdateRect(gfx::RectF(20, 25, 1, 2));
1155 emulateDrawingOneFrame(root.get());
1156 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1157 EXPECT_FLOAT_RECT_EQ(gfx::RectF(110, 111, 11, 16), rootDamageRect);
1159 // If we notify the damage tracker that we drew the damaged area, then damage should be emptied.
1160 root->renderSurface()->damageTracker()->didDrawDamagedArea();
1161 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1162 EXPECT_TRUE(rootDamageRect.IsEmpty());
1164 // Damage should remain empty even after one frame, since there's yet no new damage
1165 emulateDrawingOneFrame(root.get());
1166 rootDamageRect = root->renderSurface()->damageTracker()->currentDamageRect();
1167 EXPECT_TRUE(rootDamageRect.IsEmpty());
1170 } // namespace
1171 } // namespace cc