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/trees/damage_tracker.h"
7 #include "cc/base/math_util.h"
8 #include "cc/layers/layer_impl.h"
9 #include "cc/output/filter_operation.h"
10 #include "cc/output/filter_operations.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 "cc/test/test_shared_bitmap_manager.h"
15 #include "cc/test/test_task_graph_runner.h"
16 #include "cc/trees/layer_tree_host_common.h"
17 #include "cc/trees/layer_tree_impl.h"
18 #include "cc/trees/single_thread_proxy.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
21 #include "ui/gfx/geometry/quad_f.h"
22 #include "ui/gfx/geometry/rect_conversions.h"
27 void ExecuteCalculateDrawProperties(LayerImpl
* root
,
28 LayerImplList
* render_surface_layer_list
) {
29 // Sanity check: The test itself should create the root layer's render
30 // surface, so that the surface (and its damage tracker) can
31 // persist across multiple calls to this function.
32 ASSERT_TRUE(root
->render_surface());
33 ASSERT_FALSE(render_surface_layer_list
->size());
35 FakeLayerTreeHostImpl::RecursiveUpdateNumChildren(root
);
36 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting
inputs(
37 root
, root
->bounds(), render_surface_layer_list
);
38 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
41 void ClearDamageForAllSurfaces(LayerImpl
* layer
) {
42 if (layer
->render_surface())
43 layer
->render_surface()->damage_tracker()->DidDrawDamagedArea();
45 // Recursively clear damage for any existing surface.
46 for (size_t i
= 0; i
< layer
->children().size(); ++i
)
47 ClearDamageForAllSurfaces(layer
->children()[i
]);
50 void EmulateDrawingOneFrame(LayerImpl
* root
) {
51 root
->layer_tree_impl()->property_trees()->needs_rebuild
= true;
52 // This emulates only steps that are relevant to testing the damage tracker:
53 // 1. computing the render passes and layerlists
54 // 2. updating all damage trackers in the correct order
55 // 3. resetting all update_rects and property_changed flags for all layers
58 LayerImplList render_surface_layer_list
;
59 ExecuteCalculateDrawProperties(root
, &render_surface_layer_list
);
61 // Iterate back-to-front, so that damage correctly propagates from descendant
62 // surfaces to ancestors.
63 size_t render_surface_layer_list_size
= render_surface_layer_list
.size();
64 for (size_t i
= 0; i
< render_surface_layer_list_size
; ++i
) {
65 size_t index
= render_surface_layer_list_size
- 1 - i
;
66 RenderSurfaceImpl
* target_surface
=
67 render_surface_layer_list
[index
]->render_surface();
68 target_surface
->damage_tracker()->UpdateDamageTrackingState(
69 target_surface
->layer_list(), target_surface
->OwningLayerId(),
70 target_surface
->SurfacePropertyChangedOnlyFromDescendant(),
71 target_surface
->content_rect(),
72 render_surface_layer_list
[index
]->mask_layer(),
73 render_surface_layer_list
[index
]->filters());
76 root
->ResetAllChangeTrackingForSubtree();
79 class DamageTrackerTest
: public testing::Test
{
82 : host_impl_(&proxy_
, &shared_bitmap_manager_
, &task_graph_runner_
) {}
84 scoped_ptr
<LayerImpl
> CreateTestTreeWithOneSurface() {
85 scoped_ptr
<LayerImpl
> root
=
86 LayerImpl::Create(host_impl_
.active_tree(), 1);
87 scoped_ptr
<LayerImpl
> child
=
88 LayerImpl::Create(host_impl_
.active_tree(), 2);
90 root
->SetPosition(gfx::PointF());
91 root
->SetBounds(gfx::Size(500, 500));
92 root
->SetContentBounds(gfx::Size(500, 500));
93 root
->SetDrawsContent(true);
94 root
->SetHasRenderSurface(true);
95 root
->render_surface()->SetContentRect(gfx::Rect(0, 0, 500, 500));
97 child
->SetPosition(gfx::PointF(100.f
, 100.f
));
98 child
->SetBounds(gfx::Size(30, 30));
99 child
->SetContentBounds(gfx::Size(30, 30));
100 child
->SetDrawsContent(true);
101 root
->AddChild(child
.Pass());
106 scoped_ptr
<LayerImpl
> CreateTestTreeWithTwoSurfaces() {
107 // This test tree has two render surfaces: one for the root, and one for
108 // child1. Additionally, the root has a second child layer, and child1 has
109 // two children of its own.
111 scoped_ptr
<LayerImpl
> root
=
112 LayerImpl::Create(host_impl_
.active_tree(), 1);
113 scoped_ptr
<LayerImpl
> child1
=
114 LayerImpl::Create(host_impl_
.active_tree(), 2);
115 scoped_ptr
<LayerImpl
> child2
=
116 LayerImpl::Create(host_impl_
.active_tree(), 3);
117 scoped_ptr
<LayerImpl
> grand_child1
=
118 LayerImpl::Create(host_impl_
.active_tree(), 4);
119 scoped_ptr
<LayerImpl
> grand_child2
=
120 LayerImpl::Create(host_impl_
.active_tree(), 5);
122 root
->SetPosition(gfx::PointF());
123 root
->SetBounds(gfx::Size(500, 500));
124 root
->SetContentBounds(gfx::Size(500, 500));
125 root
->SetDrawsContent(true);
126 root
->SetHasRenderSurface(true);
127 root
->render_surface()->SetContentRect(gfx::Rect(0, 0, 500, 500));
129 child1
->SetPosition(gfx::PointF(100.f
, 100.f
));
130 child1
->SetBounds(gfx::Size(30, 30));
131 child1
->SetContentBounds(gfx::Size(30, 30));
132 // With a child that draws_content, opacity will cause the layer to create
133 // its own RenderSurface. This layer does not draw, but is intended to
134 // create its own RenderSurface.
135 child1
->SetDrawsContent(false);
136 child1
->SetHasRenderSurface(true);
138 child2
->SetPosition(gfx::PointF(11.f
, 11.f
));
139 child2
->SetBounds(gfx::Size(18, 18));
140 child2
->SetContentBounds(gfx::Size(18, 18));
141 child2
->SetDrawsContent(true);
143 grand_child1
->SetPosition(gfx::PointF(200.f
, 200.f
));
144 grand_child1
->SetBounds(gfx::Size(6, 8));
145 grand_child1
->SetContentBounds(gfx::Size(6, 8));
146 grand_child1
->SetDrawsContent(true);
148 grand_child2
->SetPosition(gfx::PointF(190.f
, 190.f
));
149 grand_child2
->SetBounds(gfx::Size(6, 8));
150 grand_child2
->SetContentBounds(gfx::Size(6, 8));
151 grand_child2
->SetDrawsContent(true);
153 child1
->AddChild(grand_child1
.Pass());
154 child1
->AddChild(grand_child2
.Pass());
155 root
->AddChild(child1
.Pass());
156 root
->AddChild(child2
.Pass());
161 scoped_ptr
<LayerImpl
> CreateAndSetUpTestTreeWithOneSurface() {
162 scoped_ptr
<LayerImpl
> root
= CreateTestTreeWithOneSurface();
164 // Setup includes going past the first frame which always damages
165 // everything, so that we can actually perform specific tests.
166 EmulateDrawingOneFrame(root
.get());
171 scoped_ptr
<LayerImpl
> CreateAndSetUpTestTreeWithTwoSurfaces() {
172 scoped_ptr
<LayerImpl
> root
= CreateTestTreeWithTwoSurfaces();
174 // Setup includes going past the first frame which always damages
175 // everything, so that we can actually perform specific tests.
176 EmulateDrawingOneFrame(root
.get());
182 FakeImplProxy proxy_
;
183 TestSharedBitmapManager shared_bitmap_manager_
;
184 TestTaskGraphRunner task_graph_runner_
;
185 FakeLayerTreeHostImpl host_impl_
;
188 TEST_F(DamageTrackerTest
, SanityCheckTestTreeWithOneSurface
) {
189 // Sanity check that the simple test tree will actually produce the expected
190 // render surfaces and layer lists.
192 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
194 EXPECT_EQ(2u, root
->render_surface()->layer_list().size());
195 EXPECT_EQ(1, root
->render_surface()->layer_list()[0]->id());
196 EXPECT_EQ(2, root
->render_surface()->layer_list()[1]->id());
198 gfx::Rect root_damage_rect
=
199 root
->render_surface()->damage_tracker()->current_damage_rect();
201 EXPECT_EQ(gfx::Rect(500, 500).ToString(), root_damage_rect
.ToString());
204 TEST_F(DamageTrackerTest
, SanityCheckTestTreeWithTwoSurfaces
) {
205 // Sanity check that the complex test tree will actually produce the expected
206 // render surfaces and layer lists.
208 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
210 LayerImpl
* child1
= root
->children()[0];
211 LayerImpl
* child2
= root
->children()[1];
212 gfx::Rect child_damage_rect
=
213 child1
->render_surface()->damage_tracker()->current_damage_rect();
214 gfx::Rect root_damage_rect
=
215 root
->render_surface()->damage_tracker()->current_damage_rect();
217 ASSERT_TRUE(child1
->render_surface());
218 EXPECT_FALSE(child2
->render_surface());
219 EXPECT_EQ(3u, root
->render_surface()->layer_list().size());
220 EXPECT_EQ(2u, child1
->render_surface()->layer_list().size());
222 // The render surface for child1 only has a content_rect that encloses
223 // grand_child1 and grand_child2, because child1 does not draw content.
224 EXPECT_EQ(gfx::Rect(190, 190, 16, 18).ToString(),
225 child_damage_rect
.ToString());
226 EXPECT_EQ(gfx::Rect(500, 500).ToString(), root_damage_rect
.ToString());
229 TEST_F(DamageTrackerTest
, VerifyDamageForUpdateRects
) {
230 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
231 LayerImpl
* child
= root
->children()[0];
233 // CASE 1: Setting the update rect should cause the corresponding damage to
235 ClearDamageForAllSurfaces(root
.get());
236 child
->SetUpdateRect(gfx::Rect(10, 11, 12, 13));
237 EmulateDrawingOneFrame(root
.get());
239 // Damage position on the surface should be: position of update_rect (10, 11)
240 // relative to the child (100, 100).
241 gfx::Rect root_damage_rect
=
242 root
->render_surface()->damage_tracker()->current_damage_rect();
243 EXPECT_EQ(gfx::Rect(110, 111, 12, 13).ToString(),
244 root_damage_rect
.ToString());
246 // CASE 2: The same update rect twice in a row still produces the same
248 ClearDamageForAllSurfaces(root
.get());
249 child
->SetUpdateRect(gfx::Rect(10, 11, 12, 13));
250 EmulateDrawingOneFrame(root
.get());
252 root
->render_surface()->damage_tracker()->current_damage_rect();
253 EXPECT_EQ(gfx::Rect(110, 111, 12, 13).ToString(),
254 root_damage_rect
.ToString());
256 // CASE 3: Setting a different update rect should cause damage on the new
257 // update region, but no additional exposed old region.
258 ClearDamageForAllSurfaces(root
.get());
259 child
->SetUpdateRect(gfx::Rect(20, 25, 1, 2));
260 EmulateDrawingOneFrame(root
.get());
262 // Damage position on the surface should be: position of update_rect (20, 25)
263 // relative to the child (100, 100).
265 root
->render_surface()->damage_tracker()->current_damage_rect();
266 EXPECT_EQ(gfx::Rect(120, 125, 1, 2).ToString(), root_damage_rect
.ToString());
269 TEST_F(DamageTrackerTest
, VerifyDamageForLayerDamageRects
) {
270 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
271 LayerImpl
* child
= root
->children()[0];
273 // CASE 1: Adding the layer damage rect should cause the corresponding damage
275 ClearDamageForAllSurfaces(root
.get());
276 child
->AddDamageRect(gfx::RectF(10.f
, 11.f
, 12.f
, 13.f
));
277 EmulateDrawingOneFrame(root
.get());
279 // Damage position on the surface should be: position of layer damage_rect
280 // (10, 11) relative to the child (100, 100).
281 gfx::Rect root_damage_rect
=
282 root
->render_surface()->damage_tracker()->current_damage_rect();
283 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(110, 111, 12, 13)));
285 // CASE 2: The same layer damage rect twice in a row still produces the same
287 ClearDamageForAllSurfaces(root
.get());
288 child
->AddDamageRect(gfx::RectF(10.f
, 11.f
, 12.f
, 13.f
));
289 EmulateDrawingOneFrame(root
.get());
291 root
->render_surface()->damage_tracker()->current_damage_rect();
292 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(110, 111, 12, 13)));
294 // CASE 3: Adding a different layer damage rect should cause damage on the
295 // new damaged region, but no additional exposed old region.
296 ClearDamageForAllSurfaces(root
.get());
297 child
->AddDamageRect(gfx::RectF(20.f
, 25.f
, 1.f
, 2.f
));
298 EmulateDrawingOneFrame(root
.get());
300 // Damage position on the surface should be: position of layer damage_rect
301 // (20, 25) relative to the child (100, 100).
303 root
->render_surface()->damage_tracker()->current_damage_rect();
304 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(120, 125, 1, 2)));
306 // CASE 4: Adding multiple layer damage rects should cause a unified
307 // damage on root damage rect.
308 ClearDamageForAllSurfaces(root
.get());
309 child
->AddDamageRect(gfx::RectF(20.f
, 25.f
, 1.f
, 2.f
));
310 child
->AddDamageRect(gfx::RectF(10.f
, 15.f
, 3.f
, 4.f
));
311 EmulateDrawingOneFrame(root
.get());
313 // Damage position on the surface should be: position of layer damage_rect
314 // (20, 25) relative to the child (100, 100).
316 root
->render_surface()->damage_tracker()->current_damage_rect();
317 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(120, 125, 1, 2)));
318 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(110, 115, 3, 4)));
321 TEST_F(DamageTrackerTest
, VerifyDamageForLayerUpdateAndDamageRects
) {
322 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
323 LayerImpl
* child
= root
->children()[0];
325 // CASE 1: Adding the layer damage rect and update rect should cause the
326 // corresponding damage to the surface.
327 ClearDamageForAllSurfaces(root
.get());
328 child
->AddDamageRect(gfx::RectF(5.f
, 6.f
, 12.f
, 13.f
));
329 child
->SetUpdateRect(gfx::Rect(15, 16, 14, 10));
330 EmulateDrawingOneFrame(root
.get());
332 // Damage position on the surface should be: position of unified layer
333 // damage_rect and update rect (5, 6)
334 // relative to the child (100, 100).
335 gfx::Rect root_damage_rect
=
336 root
->render_surface()->damage_tracker()->current_damage_rect();
337 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(105, 106, 24, 20)));
339 // CASE 2: The same layer damage rect and update rect twice in a row still
340 // produces the same damage.
341 ClearDamageForAllSurfaces(root
.get());
342 child
->AddDamageRect(gfx::RectF(10.f
, 11.f
, 12.f
, 13.f
));
343 child
->SetUpdateRect(gfx::Rect(10, 11, 14, 15));
344 EmulateDrawingOneFrame(root
.get());
346 root
->render_surface()->damage_tracker()->current_damage_rect();
347 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(110, 111, 14, 15)));
349 // CASE 3: Adding a different layer damage rect and update rect should cause
350 // damage on the new damaged region, but no additional exposed old region.
351 ClearDamageForAllSurfaces(root
.get());
352 child
->AddDamageRect(gfx::RectF(20.f
, 25.f
, 2.f
, 3.f
));
353 child
->SetUpdateRect(gfx::Rect(5, 10, 7, 8));
354 EmulateDrawingOneFrame(root
.get());
356 // Damage position on the surface should be: position of unified layer damage
357 // rect and update rect (5, 10) relative to the child (100, 100).
359 root
->render_surface()->damage_tracker()->current_damage_rect();
360 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(105, 110, 17, 18)));
363 TEST_F(DamageTrackerTest
, VerifyDamageForPropertyChanges
) {
364 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
365 LayerImpl
* child
= root
->children()[0];
367 // CASE 1: The layer's property changed flag takes priority over update rect.
369 ClearDamageForAllSurfaces(root
.get());
370 child
->SetUpdateRect(gfx::Rect(10, 11, 12, 13));
371 child
->SetOpacity(0.5f
);
372 EmulateDrawingOneFrame(root
.get());
374 // Sanity check - we should not have accidentally created a separate render
375 // surface for the translucent layer.
376 ASSERT_FALSE(child
->render_surface());
377 ASSERT_EQ(2u, root
->render_surface()->layer_list().size());
379 // Damage should be the entire child layer in target_surface space.
380 gfx::Rect expected_rect
= gfx::Rect(100, 100, 30, 30);
381 gfx::Rect root_damage_rect
=
382 root
->render_surface()->damage_tracker()->current_damage_rect();
383 EXPECT_EQ(expected_rect
.ToString(), root_damage_rect
.ToString());
385 // CASE 2: If a layer moves due to property change, it damages both the new
386 // location and the old (exposed) location. The old location is the
387 // entire old layer, not just the update_rect.
389 // Cycle one frame of no change, just to sanity check that the next rect is
390 // not because of the old damage state.
391 ClearDamageForAllSurfaces(root
.get());
392 EmulateDrawingOneFrame(root
.get());
394 root
->render_surface()->damage_tracker()->current_damage_rect();
395 EXPECT_TRUE(root_damage_rect
.IsEmpty());
397 // Then, test the actual layer movement.
398 ClearDamageForAllSurfaces(root
.get());
399 child
->SetPosition(gfx::PointF(200.f
, 230.f
));
400 EmulateDrawingOneFrame(root
.get());
402 // Expect damage to be the combination of the previous one and the new one.
403 expected_rect
.Union(gfx::Rect(200, 230, 30, 30));
405 root
->render_surface()->damage_tracker()->current_damage_rect();
406 EXPECT_FLOAT_RECT_EQ(expected_rect
, root_damage_rect
);
409 TEST_F(DamageTrackerTest
, VerifyDamageForTransformedLayer
) {
410 // If a layer is transformed, the damage rect should still enclose the entire
411 // transformed layer.
413 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
414 LayerImpl
* child
= root
->children()[0];
416 gfx::Transform rotation
;
417 rotation
.Rotate(45.0);
419 ClearDamageForAllSurfaces(root
.get());
420 child
->SetTransformOrigin(gfx::Point3F(
421 child
->bounds().width() * 0.5f
, child
->bounds().height() * 0.5f
, 0.f
));
422 child
->SetPosition(gfx::PointF(85.f
, 85.f
));
423 EmulateDrawingOneFrame(root
.get());
425 // Sanity check that the layer actually moved to (85, 85), damaging its old
426 // location and new location.
427 gfx::Rect root_damage_rect
=
428 root
->render_surface()->damage_tracker()->current_damage_rect();
429 EXPECT_EQ(gfx::Rect(85, 85, 45, 45).ToString(), root_damage_rect
.ToString());
431 // With the anchor on the layer's center, now we can test the rotation more
432 // intuitively, since it applies about the layer's anchor.
433 ClearDamageForAllSurfaces(root
.get());
434 child
->SetTransform(rotation
);
435 EmulateDrawingOneFrame(root
.get());
437 // Since the child layer is square, rotation by 45 degrees about the center
438 // should increase the size of the expected rect by sqrt(2), centered around
439 // (100, 100). The old exposed region should be fully contained in the new
441 float expected_width
= 30.f
* sqrt(2.f
);
442 float expected_position
= 100.f
- 0.5f
* expected_width
;
443 gfx::Rect expected_rect
= gfx::ToEnclosingRect(gfx::RectF(
444 expected_position
, expected_position
, expected_width
, expected_width
));
446 root
->render_surface()->damage_tracker()->current_damage_rect();
447 EXPECT_EQ(expected_rect
.ToString(), root_damage_rect
.ToString());
450 TEST_F(DamageTrackerTest
, VerifyDamageForPerspectiveClippedLayer
) {
451 // If a layer has a perspective transform that causes w < 0, then not
452 // clipping the layer can cause an invalid damage rect. This test checks that
453 // the w < 0 case is tracked properly.
455 // The transform is constructed so that if w < 0 clipping is not performed,
456 // the incorrect rect will be very small, specifically: position (500.972504,
457 // 498.544617) and size 0.056610 x 2.910767. Instead, the correctly
458 // transformed rect should actually be very huge (i.e. in theory, -infinity
459 // on the left), and positioned so that the right-most bound rect will be
460 // approximately 501 units in root surface space.
463 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
464 LayerImpl
* child
= root
->children()[0];
466 gfx::Transform transform
;
467 transform
.Translate3d(500.0, 500.0, 0.0);
468 transform
.ApplyPerspectiveDepth(1.0);
469 transform
.RotateAboutYAxis(45.0);
470 transform
.Translate3d(-50.0, -50.0, 0.0);
473 child
->SetPosition(gfx::PointF(0.f
, 0.f
));
474 child
->SetBounds(gfx::Size(100, 100));
475 child
->SetContentBounds(gfx::Size(100, 100));
476 child
->SetTransform(transform
);
477 EmulateDrawingOneFrame(root
.get());
479 // Sanity check that the child layer's bounds would actually get clipped by
480 // w < 0, otherwise this test is not actually testing the intended scenario.
481 gfx::QuadF
test_quad(gfx::RectF(gfx::PointF(), gfx::SizeF(100.f
, 100.f
)));
482 bool clipped
= false;
483 MathUtil::MapQuad(transform
, test_quad
, &clipped
);
484 EXPECT_TRUE(clipped
);
486 // Damage the child without moving it.
487 ClearDamageForAllSurfaces(root
.get());
488 child
->SetOpacity(0.5f
);
489 EmulateDrawingOneFrame(root
.get());
491 // The expected damage should cover the entire root surface (500x500), but we
492 // don't care whether the damage rect was clamped or is larger than the
493 // surface for this test.
494 gfx::Rect root_damage_rect
=
495 root
->render_surface()->damage_tracker()->current_damage_rect();
496 gfx::Rect damage_we_care_about
= gfx::Rect(gfx::Size(500, 500));
497 EXPECT_TRUE(root_damage_rect
.Contains(damage_we_care_about
));
500 TEST_F(DamageTrackerTest
, VerifyDamageForBlurredSurface
) {
501 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
502 LayerImpl
* surface
= root
->children()[0];
503 LayerImpl
* child
= surface
->children()[0];
505 FilterOperations filters
;
506 filters
.Append(FilterOperation::CreateBlurFilter(5.f
));
507 int outset_top
, outset_right
, outset_bottom
, outset_left
;
508 filters
.GetOutsets(&outset_top
, &outset_right
, &outset_bottom
, &outset_left
);
510 // Setting the filter will damage the whole surface.
511 ClearDamageForAllSurfaces(root
.get());
512 surface
->SetFilters(filters
);
513 EmulateDrawingOneFrame(root
.get());
515 // Setting the update rect should cause the corresponding damage to the
516 // surface, blurred based on the size of the blur filter.
517 ClearDamageForAllSurfaces(root
.get());
518 child
->SetUpdateRect(gfx::Rect(1, 2, 3, 4));
519 EmulateDrawingOneFrame(root
.get());
521 // Damage position on the surface should be: position of update_rect (1, 2)
522 // relative to the child (300, 300), but expanded by the blur outsets.
523 gfx::Rect root_damage_rect
=
524 root
->render_surface()->damage_tracker()->current_damage_rect();
525 gfx::Rect expected_damage_rect
= gfx::Rect(301, 302, 3, 4);
527 expected_damage_rect
.Inset(-outset_left
,
531 EXPECT_EQ(expected_damage_rect
.ToString(), root_damage_rect
.ToString());
534 TEST_F(DamageTrackerTest
, VerifyDamageForImageFilter
) {
535 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
536 LayerImpl
* child
= root
->children()[0];
537 gfx::Rect root_damage_rect
, child_damage_rect
;
539 // Allow us to set damage on child too.
540 child
->SetDrawsContent(true);
542 skia::RefPtr
<SkImageFilter
> filter
= skia::AdoptRef(
543 SkBlurImageFilter::Create(SkIntToScalar(2), SkIntToScalar(2)));
544 FilterOperations filters
;
545 filters
.Append(FilterOperation::CreateReferenceFilter(filter
));
547 // Setting the filter will damage the whole surface.
548 ClearDamageForAllSurfaces(root
.get());
549 child
->SetHasRenderSurface(true);
550 child
->SetFilters(filters
);
551 EmulateDrawingOneFrame(root
.get());
553 root
->render_surface()->damage_tracker()->current_damage_rect();
555 child
->render_surface()->damage_tracker()->current_damage_rect();
556 EXPECT_EQ(gfx::Rect(100, 100, 30, 30).ToString(),
557 root_damage_rect
.ToString());
558 EXPECT_EQ(gfx::Rect(30, 30).ToString(), child_damage_rect
.ToString());
560 // CASE 1: Setting the update rect should damage the whole surface (for now)
561 ClearDamageForAllSurfaces(root
.get());
562 child
->SetUpdateRect(gfx::Rect(1, 1));
563 EmulateDrawingOneFrame(root
.get());
566 root
->render_surface()->damage_tracker()->current_damage_rect();
568 child
->render_surface()->damage_tracker()->current_damage_rect();
569 EXPECT_EQ(gfx::Rect(100, 100, 30, 30).ToString(),
570 root_damage_rect
.ToString());
571 EXPECT_EQ(gfx::Rect(30.f
, 30.f
).ToString(), child_damage_rect
.ToString());
574 TEST_F(DamageTrackerTest
, VerifyDamageForBackgroundBlurredChild
) {
575 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
576 LayerImpl
* child1
= root
->children()[0];
577 LayerImpl
* child2
= root
->children()[1];
579 // Allow us to set damage on child1 too.
580 child1
->SetDrawsContent(true);
582 FilterOperations filters
;
583 filters
.Append(FilterOperation::CreateBlurFilter(2.f
));
584 int outset_top
, outset_right
, outset_bottom
, outset_left
;
585 filters
.GetOutsets(&outset_top
, &outset_right
, &outset_bottom
, &outset_left
);
587 // Setting the filter will damage the whole surface.
588 ClearDamageForAllSurfaces(root
.get());
589 child1
->SetBackgroundFilters(filters
);
590 EmulateDrawingOneFrame(root
.get());
592 // CASE 1: Setting the update rect should cause the corresponding damage to
593 // the surface, blurred based on the size of the child's background
595 ClearDamageForAllSurfaces(root
.get());
596 root
->SetUpdateRect(gfx::Rect(297, 297, 2, 2));
597 EmulateDrawingOneFrame(root
.get());
599 gfx::Rect root_damage_rect
=
600 root
->render_surface()->damage_tracker()->current_damage_rect();
601 // Damage position on the surface should be a composition of the damage on
602 // the root and on child2. Damage on the root should be: position of
603 // update_rect (297, 297), but expanded by the blur outsets.
604 gfx::Rect expected_damage_rect
= gfx::Rect(297, 297, 2, 2);
606 expected_damage_rect
.Inset(-outset_left
,
610 EXPECT_EQ(expected_damage_rect
.ToString(), root_damage_rect
.ToString());
612 // CASE 2: Setting the update rect should cause the corresponding damage to
613 // the surface, blurred based on the size of the child's background
614 // blur filter. Since the damage extends to the right/bottom outside
615 // of the blurred layer, only the left/top should end up expanded.
616 ClearDamageForAllSurfaces(root
.get());
617 root
->SetUpdateRect(gfx::Rect(297, 297, 30, 30));
618 EmulateDrawingOneFrame(root
.get());
621 root
->render_surface()->damage_tracker()->current_damage_rect();
622 // Damage position on the surface should be a composition of the damage on
623 // the root and on child2. Damage on the root should be: position of
624 // update_rect (297, 297), but expanded on the left/top by the blur outsets.
625 expected_damage_rect
= gfx::Rect(297, 297, 30, 30);
627 expected_damage_rect
.Inset(-outset_left
,
631 EXPECT_EQ(expected_damage_rect
.ToString(), root_damage_rect
.ToString());
633 // CASE 3: Setting this update rect outside the blurred content_bounds of the
634 // blurred child1 will not cause it to be expanded.
635 ClearDamageForAllSurfaces(root
.get());
636 root
->SetUpdateRect(gfx::Rect(30, 30, 2, 2));
637 EmulateDrawingOneFrame(root
.get());
640 root
->render_surface()->damage_tracker()->current_damage_rect();
641 // Damage on the root should be: position of update_rect (30, 30), not
643 expected_damage_rect
= gfx::Rect(30, 30, 2, 2);
645 EXPECT_EQ(expected_damage_rect
.ToString(), root_damage_rect
.ToString());
647 // CASE 4: Setting this update rect inside the blurred content_bounds but
648 // outside the original content_bounds of the blurred child1 will
649 // cause it to be expanded.
650 ClearDamageForAllSurfaces(root
.get());
651 root
->SetUpdateRect(gfx::Rect(99, 99, 1, 1));
652 EmulateDrawingOneFrame(root
.get());
655 root
->render_surface()->damage_tracker()->current_damage_rect();
656 // Damage on the root should be: position of update_rect (99, 99), expanded by
657 // the blurring on child1, but since it is 1 pixel outside the layer, the
658 // expanding should be reduced by 1.
659 expected_damage_rect
= gfx::Rect(99, 99, 1, 1);
661 expected_damage_rect
.Inset(-outset_left
+ 1,
665 EXPECT_EQ(expected_damage_rect
.ToString(), root_damage_rect
.ToString());
667 // CASE 5: Setting the update rect on child2, which is above child1, will
668 // not get blurred by child1, so it does not need to get expanded.
669 ClearDamageForAllSurfaces(root
.get());
670 child2
->SetUpdateRect(gfx::Rect(1, 1));
671 EmulateDrawingOneFrame(root
.get());
674 root
->render_surface()->damage_tracker()->current_damage_rect();
675 // Damage on child2 should be: position of update_rect offset by the child's
676 // position (11, 11), and not expanded by anything.
677 expected_damage_rect
= gfx::Rect(11, 11, 1, 1);
679 EXPECT_EQ(expected_damage_rect
.ToString(), root_damage_rect
.ToString());
681 // CASE 6: Setting the update rect on child1 will also blur the damage, so
682 // that any pixels needed for the blur are redrawn in the current
684 ClearDamageForAllSurfaces(root
.get());
685 child1
->SetUpdateRect(gfx::Rect(1, 1));
686 EmulateDrawingOneFrame(root
.get());
689 root
->render_surface()->damage_tracker()->current_damage_rect();
690 // Damage on child1 should be: position of update_rect offset by the child's
691 // position (100, 100), and expanded by the damage.
692 expected_damage_rect
= gfx::Rect(100, 100, 1, 1);
694 expected_damage_rect
.Inset(-outset_left
,
698 EXPECT_EQ(expected_damage_rect
.ToString(), root_damage_rect
.ToString());
701 TEST_F(DamageTrackerTest
, VerifyDamageForAddingAndRemovingLayer
) {
702 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
703 LayerImpl
* child1
= root
->children()[0];
705 // CASE 1: Adding a new layer should cause the appropriate damage.
707 ClearDamageForAllSurfaces(root
.get());
709 scoped_ptr
<LayerImpl
> child2
=
710 LayerImpl::Create(host_impl_
.active_tree(), 3);
711 child2
->SetPosition(gfx::PointF(400.f
, 380.f
));
712 child2
->SetBounds(gfx::Size(6, 8));
713 child2
->SetContentBounds(gfx::Size(6, 8));
714 child2
->SetDrawsContent(true);
715 root
->AddChild(child2
.Pass());
717 EmulateDrawingOneFrame(root
.get());
719 // Sanity check - all 3 layers should be on the same render surface; render
720 // surfaces are tested elsewhere.
721 ASSERT_EQ(3u, root
->render_surface()->layer_list().size());
723 gfx::Rect root_damage_rect
=
724 root
->render_surface()->damage_tracker()->current_damage_rect();
725 EXPECT_EQ(gfx::Rect(400, 380, 6, 8).ToString(), root_damage_rect
.ToString());
727 // CASE 2: If the layer is removed, its entire old layer becomes exposed, not
728 // just the last update rect.
730 // Advance one frame without damage so that we know the damage rect is not
731 // leftover from the previous case.
732 ClearDamageForAllSurfaces(root
.get());
733 EmulateDrawingOneFrame(root
.get());
736 root
->render_surface()->damage_tracker()->current_damage_rect();
737 EXPECT_TRUE(root_damage_rect
.IsEmpty());
739 // Then, test removing child1.
740 root
->RemoveChild(child1
);
742 EmulateDrawingOneFrame(root
.get());
745 root
->render_surface()->damage_tracker()->current_damage_rect();
746 EXPECT_EQ(gfx::Rect(100, 100, 30, 30).ToString(),
747 root_damage_rect
.ToString());
750 TEST_F(DamageTrackerTest
, VerifyDamageForNewUnchangedLayer
) {
751 // If child2 is added to the layer tree, but it doesn't have any explicit
752 // damage of its own, it should still indeed damage the target surface.
754 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
756 ClearDamageForAllSurfaces(root
.get());
758 scoped_ptr
<LayerImpl
> child2
=
759 LayerImpl::Create(host_impl_
.active_tree(), 3);
760 child2
->SetPosition(gfx::PointF(400.f
, 380.f
));
761 child2
->SetBounds(gfx::Size(6, 8));
762 child2
->SetContentBounds(gfx::Size(6, 8));
763 child2
->SetDrawsContent(true);
764 child2
->ResetAllChangeTrackingForSubtree();
765 // Sanity check the initial conditions of the test, if these asserts
766 // trigger, it means the test no longer actually covers the intended
768 ASSERT_FALSE(child2
->LayerPropertyChanged());
769 ASSERT_TRUE(child2
->update_rect().IsEmpty());
770 root
->AddChild(child2
.Pass());
772 EmulateDrawingOneFrame(root
.get());
774 // Sanity check - all 3 layers should be on the same render surface; render
775 // surfaces are tested elsewhere.
776 ASSERT_EQ(3u, root
->render_surface()->layer_list().size());
778 gfx::Rect root_damage_rect
=
779 root
->render_surface()->damage_tracker()->current_damage_rect();
780 EXPECT_EQ(gfx::Rect(400, 380, 6, 8).ToString(), root_damage_rect
.ToString());
783 TEST_F(DamageTrackerTest
, VerifyDamageForMultipleLayers
) {
784 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
785 LayerImpl
* child1
= root
->children()[0];
787 // In this test we don't want the above tree manipulation to be considered
788 // part of the same frame.
789 ClearDamageForAllSurfaces(root
.get());
791 scoped_ptr
<LayerImpl
> child2
=
792 LayerImpl::Create(host_impl_
.active_tree(), 3);
793 child2
->SetPosition(gfx::PointF(400.f
, 380.f
));
794 child2
->SetBounds(gfx::Size(6, 8));
795 child2
->SetContentBounds(gfx::Size(6, 8));
796 child2
->SetDrawsContent(true);
797 root
->AddChild(child2
.Pass());
799 LayerImpl
* child2
= root
->children()[1];
800 EmulateDrawingOneFrame(root
.get());
802 // Damaging two layers simultaneously should cause combined damage.
803 // - child1 update rect in surface space: gfx::Rect(100, 100, 1, 2);
804 // - child2 update rect in surface space: gfx::Rect(400, 380, 3, 4);
805 ClearDamageForAllSurfaces(root
.get());
806 child1
->SetUpdateRect(gfx::Rect(1, 2));
807 child2
->SetUpdateRect(gfx::Rect(3, 4));
808 EmulateDrawingOneFrame(root
.get());
809 gfx::Rect root_damage_rect
=
810 root
->render_surface()->damage_tracker()->current_damage_rect();
811 EXPECT_EQ(gfx::Rect(100, 100, 303, 284).ToString(),
812 root_damage_rect
.ToString());
815 TEST_F(DamageTrackerTest
, VerifyDamageForNestedSurfaces
) {
816 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
817 LayerImpl
* child1
= root
->children()[0];
818 LayerImpl
* child2
= root
->children()[1];
819 LayerImpl
* grand_child1
= root
->children()[0]->children()[0];
820 gfx::Rect child_damage_rect
;
821 gfx::Rect root_damage_rect
;
823 // CASE 1: Damage to a descendant surface should propagate properly to
825 ClearDamageForAllSurfaces(root
.get());
826 grand_child1
->SetOpacity(0.5f
);
827 EmulateDrawingOneFrame(root
.get());
829 child1
->render_surface()->damage_tracker()->current_damage_rect();
831 root
->render_surface()->damage_tracker()->current_damage_rect();
832 EXPECT_EQ(gfx::Rect(200, 200, 6, 8).ToString(), child_damage_rect
.ToString());
833 EXPECT_EQ(gfx::Rect(300, 300, 6, 8).ToString(), root_damage_rect
.ToString());
835 // CASE 2: Same as previous case, but with additional damage elsewhere that
836 // should be properly unioned.
837 // - child1 surface damage in root surface space:
838 // gfx::Rect(300, 300, 6, 8);
839 // - child2 damage in root surface space:
840 // gfx::Rect(11, 11, 18, 18);
841 ClearDamageForAllSurfaces(root
.get());
842 grand_child1
->SetOpacity(0.7f
);
843 child2
->SetOpacity(0.7f
);
844 EmulateDrawingOneFrame(root
.get());
846 child1
->render_surface()->damage_tracker()->current_damage_rect();
848 root
->render_surface()->damage_tracker()->current_damage_rect();
849 EXPECT_EQ(gfx::Rect(200, 200, 6, 8).ToString(), child_damage_rect
.ToString());
850 EXPECT_EQ(gfx::Rect(11, 11, 295, 297).ToString(),
851 root_damage_rect
.ToString());
854 TEST_F(DamageTrackerTest
, VerifyDamageForSurfaceChangeFromDescendantLayer
) {
855 // If descendant layer changes and affects the content bounds of the render
856 // surface, then the entire descendant surface should be damaged, and it
857 // should damage its ancestor surface with the old and new surface regions.
859 // This is a tricky case, since only the first grand_child changes, but the
860 // entire surface should be marked dirty.
862 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
863 LayerImpl
* child1
= root
->children()[0];
864 LayerImpl
* grand_child1
= root
->children()[0]->children()[0];
865 gfx::Rect child_damage_rect
;
866 gfx::Rect root_damage_rect
;
868 ClearDamageForAllSurfaces(root
.get());
869 grand_child1
->SetPosition(gfx::PointF(195.f
, 205.f
));
870 EmulateDrawingOneFrame(root
.get());
872 child1
->render_surface()->damage_tracker()->current_damage_rect();
874 root
->render_surface()->damage_tracker()->current_damage_rect();
876 // The new surface bounds should be damaged entirely, even though only one of
877 // the layers changed.
878 EXPECT_EQ(gfx::Rect(190, 190, 11, 23).ToString(),
879 child_damage_rect
.ToString());
881 // Damage to the root surface should be the union of child1's *entire* render
882 // surface (in target space), and its old exposed area (also in target
884 EXPECT_EQ(gfx::Rect(290, 290, 16, 23).ToString(),
885 root_damage_rect
.ToString());
888 TEST_F(DamageTrackerTest
, VerifyDamageForSurfaceChangeFromAncestorLayer
) {
889 // An ancestor/owning layer changes that affects the position/transform of
890 // the render surface. Note that in this case, the layer_property_changed flag
891 // already propagates to the subtree (tested in LayerImpltest), which damages
892 // the entire child1 surface, but the damage tracker still needs the correct
893 // logic to compute the exposed region on the root surface.
895 // TODO(shawnsingh): the expectations of this test case should change when we
896 // add support for a unique scissor_rect per RenderSurface. In that case, the
897 // child1 surface should be completely unchanged, since we are only
898 // transforming it, while the root surface would be damaged appropriately.
900 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
901 LayerImpl
* child1
= root
->children()[0];
902 gfx::Rect child_damage_rect
;
903 gfx::Rect root_damage_rect
;
905 ClearDamageForAllSurfaces(root
.get());
906 child1
->SetPosition(gfx::PointF(50.f
, 50.f
));
907 EmulateDrawingOneFrame(root
.get());
909 child1
->render_surface()->damage_tracker()->current_damage_rect();
911 root
->render_surface()->damage_tracker()->current_damage_rect();
913 // The new surface bounds should be damaged entirely.
914 EXPECT_EQ(gfx::Rect(190, 190, 16, 18).ToString(),
915 child_damage_rect
.ToString());
917 // The entire child1 surface and the old exposed child1 surface should damage
919 // - old child1 surface in target space: gfx::Rect(290, 290, 16, 18)
920 // - new child1 surface in target space: gfx::Rect(240, 240, 16, 18)
921 EXPECT_EQ(gfx::Rect(240, 240, 66, 68).ToString(),
922 root_damage_rect
.ToString());
925 TEST_F(DamageTrackerTest
, VerifyDamageForAddingAndRemovingRenderSurfaces
) {
926 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
927 LayerImpl
* child1
= root
->children()[0];
928 gfx::Rect child_damage_rect
;
929 gfx::Rect root_damage_rect
;
931 // CASE 1: If a descendant surface disappears, its entire old area becomes
933 ClearDamageForAllSurfaces(root
.get());
934 child1
->SetHasRenderSurface(false);
935 EmulateDrawingOneFrame(root
.get());
937 // Sanity check that there is only one surface now.
938 ASSERT_FALSE(child1
->render_surface());
939 ASSERT_EQ(4u, root
->render_surface()->layer_list().size());
942 root
->render_surface()->damage_tracker()->current_damage_rect();
943 EXPECT_EQ(gfx::Rect(290, 290, 16, 18).ToString(),
944 root_damage_rect
.ToString());
946 // CASE 2: If a descendant surface appears, its entire old area becomes
949 // Cycle one frame of no change, just to sanity check that the next rect is
950 // not because of the old damage state.
951 ClearDamageForAllSurfaces(root
.get());
952 EmulateDrawingOneFrame(root
.get());
954 root
->render_surface()->damage_tracker()->current_damage_rect();
955 EXPECT_TRUE(root_damage_rect
.IsEmpty());
957 // Then change the tree so that the render surface is added back.
958 ClearDamageForAllSurfaces(root
.get());
959 child1
->SetHasRenderSurface(true);
961 EmulateDrawingOneFrame(root
.get());
963 // Sanity check that there is a new surface now.
964 ASSERT_TRUE(child1
->render_surface());
965 EXPECT_EQ(3u, root
->render_surface()->layer_list().size());
966 EXPECT_EQ(2u, child1
->render_surface()->layer_list().size());
969 child1
->render_surface()->damage_tracker()->current_damage_rect();
971 root
->render_surface()->damage_tracker()->current_damage_rect();
972 EXPECT_EQ(gfx::Rect(190, 190, 16, 18).ToString(),
973 child_damage_rect
.ToString());
974 EXPECT_EQ(gfx::Rect(290, 290, 16, 18).ToString(),
975 root_damage_rect
.ToString());
978 TEST_F(DamageTrackerTest
, VerifyNoDamageWhenNothingChanged
) {
979 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
980 LayerImpl
* child1
= root
->children()[0];
981 gfx::Rect child_damage_rect
;
982 gfx::Rect root_damage_rect
;
984 // CASE 1: If nothing changes, the damage rect should be empty.
986 ClearDamageForAllSurfaces(root
.get());
987 EmulateDrawingOneFrame(root
.get());
989 child1
->render_surface()->damage_tracker()->current_damage_rect();
991 root
->render_surface()->damage_tracker()->current_damage_rect();
992 EXPECT_TRUE(child_damage_rect
.IsEmpty());
993 EXPECT_TRUE(root_damage_rect
.IsEmpty());
995 // CASE 2: If nothing changes twice in a row, the damage rect should still be
998 ClearDamageForAllSurfaces(root
.get());
999 EmulateDrawingOneFrame(root
.get());
1001 child1
->render_surface()->damage_tracker()->current_damage_rect();
1003 root
->render_surface()->damage_tracker()->current_damage_rect();
1004 EXPECT_TRUE(child_damage_rect
.IsEmpty());
1005 EXPECT_TRUE(root_damage_rect
.IsEmpty());
1008 TEST_F(DamageTrackerTest
, VerifyNoDamageForUpdateRectThatDoesNotDrawContent
) {
1009 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
1010 LayerImpl
* child1
= root
->children()[0];
1011 gfx::Rect child_damage_rect
;
1012 gfx::Rect root_damage_rect
;
1014 // In our specific tree, the update rect of child1 should not cause any
1015 // damage to any surface because it does not actually draw content.
1016 ClearDamageForAllSurfaces(root
.get());
1017 child1
->SetUpdateRect(gfx::Rect(1, 2));
1018 EmulateDrawingOneFrame(root
.get());
1020 child1
->render_surface()->damage_tracker()->current_damage_rect();
1022 root
->render_surface()->damage_tracker()->current_damage_rect();
1023 EXPECT_TRUE(child_damage_rect
.IsEmpty());
1024 EXPECT_TRUE(root_damage_rect
.IsEmpty());
1027 TEST_F(DamageTrackerTest
, VerifyDamageForReplica
) {
1028 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
1029 LayerImpl
* child1
= root
->children()[0];
1030 LayerImpl
* grand_child1
= child1
->children()[0];
1031 LayerImpl
* grand_child2
= child1
->children()[1];
1033 // Damage on a surface that has a reflection should cause the target surface
1034 // to receive the surface's damage and the surface's reflected damage.
1036 // For this test case, we modify grand_child2, and add grand_child3 to extend
1037 // the bounds of child1's surface. This way, we can test reflection changes
1038 // without changing content_bounds of the surface.
1039 grand_child2
->SetPosition(gfx::PointF(180.f
, 180.f
));
1041 scoped_ptr
<LayerImpl
> grand_child3
=
1042 LayerImpl::Create(host_impl_
.active_tree(), 6);
1043 grand_child3
->SetPosition(gfx::PointF(240.f
, 240.f
));
1044 grand_child3
->SetBounds(gfx::Size(10, 10));
1045 grand_child3
->SetContentBounds(gfx::Size(10, 10));
1046 grand_child3
->SetDrawsContent(true);
1047 child1
->AddChild(grand_child3
.Pass());
1049 child1
->SetOpacity(0.5f
);
1050 EmulateDrawingOneFrame(root
.get());
1052 // CASE 1: adding a reflection about the left edge of grand_child1.
1054 ClearDamageForAllSurfaces(root
.get());
1056 scoped_ptr
<LayerImpl
> grand_child1_replica
=
1057 LayerImpl::Create(host_impl_
.active_tree(), 7);
1058 grand_child1_replica
->SetPosition(gfx::PointF());
1059 gfx::Transform reflection
;
1060 reflection
.Scale3d(-1.0, 1.0, 1.0);
1061 grand_child1_replica
->SetTransform(reflection
);
1062 grand_child1
->SetReplicaLayer(grand_child1_replica
.Pass());
1063 grand_child1
->SetHasRenderSurface(true);
1065 EmulateDrawingOneFrame(root
.get());
1067 gfx::Rect grand_child_damage_rect
=
1068 grand_child1
->render_surface()->damage_tracker()->current_damage_rect();
1069 gfx::Rect child_damage_rect
=
1070 child1
->render_surface()->damage_tracker()->current_damage_rect();
1071 gfx::Rect root_damage_rect
=
1072 root
->render_surface()->damage_tracker()->current_damage_rect();
1074 // The grand_child surface damage should not include its own replica. The
1075 // child surface damage should include the normal and replica surfaces.
1076 EXPECT_EQ(gfx::Rect(6, 8).ToString(), grand_child_damage_rect
.ToString());
1077 EXPECT_EQ(gfx::Rect(194, 200, 12, 8).ToString(),
1078 child_damage_rect
.ToString());
1079 EXPECT_EQ(gfx::Rect(294, 300, 12, 8).ToString(), root_damage_rect
.ToString());
1081 // CASE 2: moving the descendant surface should cause both the original and
1082 // reflected areas to be damaged on the target.
1083 ClearDamageForAllSurfaces(root
.get());
1084 gfx::Rect old_content_rect
= child1
->render_surface()->content_rect();
1085 grand_child1
->SetPosition(gfx::PointF(195.f
, 205.f
));
1086 EmulateDrawingOneFrame(root
.get());
1087 ASSERT_EQ(old_content_rect
.width(),
1088 child1
->render_surface()->content_rect().width());
1089 ASSERT_EQ(old_content_rect
.height(),
1090 child1
->render_surface()->content_rect().height());
1092 grand_child_damage_rect
=
1093 grand_child1
->render_surface()->
1094 damage_tracker()->current_damage_rect();
1096 child1
->render_surface()->damage_tracker()->current_damage_rect();
1098 root
->render_surface()->damage_tracker()->current_damage_rect();
1100 // The child surface damage should include normal and replica surfaces for
1101 // both old and new locations.
1102 // - old location in target space: gfx::Rect(194, 200, 12, 8)
1103 // - new location in target space: gfx::Rect(189, 205, 12, 8)
1104 EXPECT_EQ(gfx::Rect(6, 8).ToString(), grand_child_damage_rect
.ToString());
1105 EXPECT_EQ(gfx::Rect(189, 200, 17, 13).ToString(),
1106 child_damage_rect
.ToString());
1107 EXPECT_EQ(gfx::Rect(289, 300, 17, 13).ToString(),
1108 root_damage_rect
.ToString());
1110 // CASE 3: removing the reflection should cause the entire region including
1111 // reflection to damage the target surface.
1112 ClearDamageForAllSurfaces(root
.get());
1113 grand_child1
->SetReplicaLayer(nullptr);
1114 grand_child1
->SetHasRenderSurface(false);
1115 EmulateDrawingOneFrame(root
.get());
1116 ASSERT_EQ(old_content_rect
.width(),
1117 child1
->render_surface()->content_rect().width());
1118 ASSERT_EQ(old_content_rect
.height(),
1119 child1
->render_surface()->content_rect().height());
1121 EXPECT_FALSE(grand_child1
->render_surface());
1123 child1
->render_surface()->damage_tracker()->current_damage_rect();
1125 root
->render_surface()->damage_tracker()->current_damage_rect();
1127 EXPECT_EQ(gfx::Rect(189, 205, 12, 8).ToString(),
1128 child_damage_rect
.ToString());
1129 EXPECT_EQ(gfx::Rect(289, 305, 12, 8).ToString(), root_damage_rect
.ToString());
1132 TEST_F(DamageTrackerTest
, VerifyDamageForMask
) {
1133 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
1134 LayerImpl
* child
= root
->children()[0];
1136 // In the current implementation of the damage tracker, changes to mask
1137 // layers should damage the entire corresponding surface.
1139 ClearDamageForAllSurfaces(root
.get());
1141 // Set up the mask layer.
1143 scoped_ptr
<LayerImpl
> mask_layer
=
1144 LayerImpl::Create(host_impl_
.active_tree(), 3);
1145 mask_layer
->SetPosition(child
->position());
1146 mask_layer
->SetBounds(child
->bounds());
1147 mask_layer
->SetContentBounds(child
->bounds());
1148 child
->SetMaskLayer(mask_layer
.Pass());
1149 child
->SetHasRenderSurface(true);
1151 LayerImpl
* mask_layer
= child
->mask_layer();
1153 // Add opacity and a grand_child so that the render surface persists even
1154 // after we remove the mask.
1156 scoped_ptr
<LayerImpl
> grand_child
=
1157 LayerImpl::Create(host_impl_
.active_tree(), 4);
1158 grand_child
->SetPosition(gfx::PointF(2.f
, 2.f
));
1159 grand_child
->SetBounds(gfx::Size(2, 2));
1160 grand_child
->SetContentBounds(gfx::Size(2, 2));
1161 grand_child
->SetDrawsContent(true);
1162 child
->AddChild(grand_child
.Pass());
1164 EmulateDrawingOneFrame(root
.get());
1166 // CASE 1: the update_rect on a mask layer should damage the entire target
1168 ClearDamageForAllSurfaces(root
.get());
1169 mask_layer
->SetUpdateRect(gfx::Rect(1, 2, 3, 4));
1170 EmulateDrawingOneFrame(root
.get());
1171 gfx::Rect child_damage_rect
=
1172 child
->render_surface()->damage_tracker()->current_damage_rect();
1173 EXPECT_EQ(gfx::Rect(30, 30).ToString(), child_damage_rect
.ToString());
1175 // CASE 2: a property change on the mask layer should damage the entire
1178 // Advance one frame without damage so that we know the damage rect is not
1179 // leftover from the previous case.
1180 ClearDamageForAllSurfaces(root
.get());
1181 EmulateDrawingOneFrame(root
.get());
1183 child
->render_surface()->damage_tracker()->current_damage_rect();
1184 EXPECT_TRUE(child_damage_rect
.IsEmpty());
1186 // Then test the property change.
1187 ClearDamageForAllSurfaces(root
.get());
1188 mask_layer
->SetStackingOrderChanged(true);
1190 EmulateDrawingOneFrame(root
.get());
1192 child
->render_surface()->damage_tracker()->current_damage_rect();
1193 EXPECT_EQ(gfx::Rect(30, 30).ToString(), child_damage_rect
.ToString());
1195 // CASE 3: removing the mask also damages the entire target surface.
1198 // Advance one frame without damage so that we know the damage rect is not
1199 // leftover from the previous case.
1200 ClearDamageForAllSurfaces(root
.get());
1201 EmulateDrawingOneFrame(root
.get());
1203 child
->render_surface()->damage_tracker()->current_damage_rect();
1204 EXPECT_TRUE(child_damage_rect
.IsEmpty());
1206 // Then test mask removal.
1207 ClearDamageForAllSurfaces(root
.get());
1208 child
->SetMaskLayer(nullptr);
1209 ASSERT_TRUE(child
->LayerPropertyChanged());
1210 EmulateDrawingOneFrame(root
.get());
1212 // Sanity check that a render surface still exists.
1213 ASSERT_TRUE(child
->render_surface());
1216 child
->render_surface()->damage_tracker()->current_damage_rect();
1217 EXPECT_EQ(gfx::Rect(30, 30).ToString(), child_damage_rect
.ToString());
1220 TEST_F(DamageTrackerTest
, VerifyDamageForReplicaMask
) {
1221 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
1222 LayerImpl
* child1
= root
->children()[0];
1223 LayerImpl
* grand_child1
= child1
->children()[0];
1225 // Changes to a replica's mask should not damage the original surface,
1226 // because it is not masked. But it does damage the ancestor target surface.
1228 ClearDamageForAllSurfaces(root
.get());
1230 // Create a reflection about the left edge of grand_child1.
1232 scoped_ptr
<LayerImpl
> grand_child1_replica
=
1233 LayerImpl::Create(host_impl_
.active_tree(), 6);
1234 grand_child1_replica
->SetPosition(gfx::PointF());
1235 gfx::Transform reflection
;
1236 reflection
.Scale3d(-1.0, 1.0, 1.0);
1237 grand_child1_replica
->SetTransform(reflection
);
1238 grand_child1
->SetReplicaLayer(grand_child1_replica
.Pass());
1239 grand_child1
->SetHasRenderSurface(true);
1241 LayerImpl
* grand_child1_replica
= grand_child1
->replica_layer();
1243 // Set up the mask layer on the replica layer
1245 scoped_ptr
<LayerImpl
> replica_mask_layer
=
1246 LayerImpl::Create(host_impl_
.active_tree(), 7);
1247 replica_mask_layer
->SetPosition(gfx::PointF());
1248 replica_mask_layer
->SetBounds(grand_child1
->bounds());
1249 replica_mask_layer
->SetContentBounds(grand_child1
->bounds());
1250 grand_child1_replica
->SetMaskLayer(replica_mask_layer
.Pass());
1252 LayerImpl
* replica_mask_layer
= grand_child1_replica
->mask_layer();
1254 EmulateDrawingOneFrame(root
.get());
1256 // Sanity check that the appropriate render surfaces were created
1257 ASSERT_TRUE(grand_child1
->render_surface());
1259 // CASE 1: a property change on the mask should damage only the reflected
1260 // region on the target surface.
1261 ClearDamageForAllSurfaces(root
.get());
1262 replica_mask_layer
->SetStackingOrderChanged(true);
1263 EmulateDrawingOneFrame(root
.get());
1265 gfx::Rect grand_child_damage_rect
=
1266 grand_child1
->render_surface()->damage_tracker()->current_damage_rect();
1267 gfx::Rect child_damage_rect
=
1268 child1
->render_surface()->damage_tracker()->current_damage_rect();
1270 EXPECT_TRUE(grand_child_damage_rect
.IsEmpty());
1271 EXPECT_EQ(gfx::Rect(194, 200, 6, 8).ToString(), child_damage_rect
.ToString());
1273 // CASE 2: removing the replica mask damages only the reflected region on the
1276 ClearDamageForAllSurfaces(root
.get());
1277 grand_child1_replica
->SetMaskLayer(nullptr);
1278 EmulateDrawingOneFrame(root
.get());
1280 grand_child_damage_rect
=
1281 grand_child1
->render_surface()->damage_tracker()->
1282 current_damage_rect();
1284 child1
->render_surface()->damage_tracker()->current_damage_rect();
1286 EXPECT_TRUE(grand_child_damage_rect
.IsEmpty());
1287 EXPECT_EQ(gfx::Rect(194, 200, 6, 8).ToString(), child_damage_rect
.ToString());
1290 TEST_F(DamageTrackerTest
, VerifyDamageForReplicaMaskWithTransformOrigin
) {
1291 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
1292 LayerImpl
* child1
= root
->children()[0];
1293 LayerImpl
* grand_child1
= child1
->children()[0];
1295 // Verify that the correct replica_origin_transform is used for the
1297 ClearDamageForAllSurfaces(root
.get());
1299 // This is not actually the transform origin point being tested, but by
1301 // expected to be the same as the replica's anchor point.
1302 grand_child1
->SetTransformOrigin(
1303 gfx::Point3F(grand_child1
->bounds().width(), 0.f
, 0.f
));
1306 scoped_ptr
<LayerImpl
> grand_child1_replica
=
1307 LayerImpl::Create(host_impl_
.active_tree(), 6);
1308 grand_child1_replica
->SetPosition(gfx::PointF());
1310 // This is the anchor being tested.
1311 grand_child1_replica
->SetTransformOrigin(
1312 gfx::Point3F(grand_child1
->bounds().width(), 0.f
, 0.f
));
1313 gfx::Transform reflection
;
1314 reflection
.Scale3d(-1.0, 1.0, 1.0);
1315 grand_child1_replica
->SetTransform(reflection
);
1316 grand_child1
->SetReplicaLayer(grand_child1_replica
.Pass());
1317 grand_child1
->SetHasRenderSurface(true);
1319 LayerImpl
* grand_child1_replica
= grand_child1
->replica_layer();
1321 // Set up the mask layer on the replica layer
1323 scoped_ptr
<LayerImpl
> replica_mask_layer
=
1324 LayerImpl::Create(host_impl_
.active_tree(), 7);
1325 replica_mask_layer
->SetPosition(gfx::PointF());
1326 // Note: this is not the transform origin being tested.
1327 replica_mask_layer
->SetBounds(grand_child1
->bounds());
1328 replica_mask_layer
->SetContentBounds(grand_child1
->bounds());
1329 grand_child1_replica
->SetMaskLayer(replica_mask_layer
.Pass());
1331 LayerImpl
* replica_mask_layer
= grand_child1_replica
->mask_layer();
1333 EmulateDrawingOneFrame(root
.get());
1335 // Sanity check that the appropriate render surfaces were created
1336 ASSERT_TRUE(grand_child1
->render_surface());
1338 // A property change on the replica_mask should damage the reflected region on
1339 // the target surface.
1340 ClearDamageForAllSurfaces(root
.get());
1341 replica_mask_layer
->SetStackingOrderChanged(true);
1343 EmulateDrawingOneFrame(root
.get());
1345 gfx::Rect child_damage_rect
=
1346 child1
->render_surface()->damage_tracker()->current_damage_rect();
1347 EXPECT_EQ(gfx::Rect(206, 200, 6, 8).ToString(), child_damage_rect
.ToString());
1350 TEST_F(DamageTrackerTest
, DamageWhenAddedExternally
) {
1351 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
1352 LayerImpl
* child
= root
->children()[0];
1354 // Case 1: This test ensures that when the tracker is given damage, that
1355 // it is included with any other partial damage.
1357 ClearDamageForAllSurfaces(root
.get());
1358 child
->SetUpdateRect(gfx::Rect(10, 11, 12, 13));
1359 root
->render_surface()->damage_tracker()->AddDamageNextUpdate(
1360 gfx::Rect(15, 16, 32, 33));
1361 EmulateDrawingOneFrame(root
.get());
1362 gfx::Rect root_damage_rect
=
1363 root
->render_surface()->damage_tracker()->current_damage_rect();
1364 EXPECT_EQ(gfx::UnionRects(gfx::Rect(15, 16, 32, 33),
1365 gfx::Rect(100 + 10, 100 + 11, 12, 13)).ToString(),
1366 root_damage_rect
.ToString());
1368 // Case 2: An additional sanity check that adding damage works even when
1369 // nothing on the layer tree changed.
1371 ClearDamageForAllSurfaces(root
.get());
1372 root
->render_surface()->damage_tracker()->AddDamageNextUpdate(
1373 gfx::Rect(30, 31, 14, 15));
1374 EmulateDrawingOneFrame(root
.get());
1376 root
->render_surface()->damage_tracker()->current_damage_rect();
1377 EXPECT_EQ(gfx::Rect(30, 31, 14, 15).ToString(), root_damage_rect
.ToString());
1380 TEST_F(DamageTrackerTest
, VerifyDamageForEmptyLayerList
) {
1381 // Though it should never happen, its a good idea to verify that the damage
1382 // tracker does not crash when it receives an empty layer_list.
1384 scoped_ptr
<LayerImpl
> root
= LayerImpl::Create(host_impl_
.active_tree(), 1);
1385 root
->SetHasRenderSurface(true);
1386 root
->draw_properties().render_target
= root
.get();
1388 ASSERT_TRUE(root
== root
->render_target());
1389 RenderSurfaceImpl
* target_surface
= root
->render_surface();
1391 LayerImplList empty_list
;
1392 target_surface
->damage_tracker()->UpdateDamageTrackingState(
1394 target_surface
->OwningLayerId(),
1398 FilterOperations());
1400 gfx::Rect damage_rect
=
1401 target_surface
->damage_tracker()->current_damage_rect();
1402 EXPECT_TRUE(damage_rect
.IsEmpty());
1405 TEST_F(DamageTrackerTest
, VerifyDamageAccumulatesUntilReset
) {
1406 // If damage is not cleared, it should accumulate.
1408 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
1409 LayerImpl
* child
= root
->children()[0];
1411 ClearDamageForAllSurfaces(root
.get());
1412 child
->SetUpdateRect(gfx::Rect(10.f
, 11.f
, 1.f
, 2.f
));
1413 EmulateDrawingOneFrame(root
.get());
1415 // Sanity check damage after the first frame; this isnt the actual test yet.
1416 gfx::Rect root_damage_rect
=
1417 root
->render_surface()->damage_tracker()->current_damage_rect();
1418 EXPECT_EQ(gfx::Rect(110, 111, 1, 2).ToString(), root_damage_rect
.ToString());
1420 // New damage, without having cleared the previous damage, should be unioned
1421 // to the previous one.
1422 child
->SetUpdateRect(gfx::Rect(20, 25, 1, 2));
1423 EmulateDrawingOneFrame(root
.get());
1425 root
->render_surface()->damage_tracker()->current_damage_rect();
1426 EXPECT_EQ(gfx::Rect(110, 111, 11, 16).ToString(),
1427 root_damage_rect
.ToString());
1429 // If we notify the damage tracker that we drew the damaged area, then damage
1430 // should be emptied.
1431 root
->render_surface()->damage_tracker()->DidDrawDamagedArea();
1433 root
->render_surface()->damage_tracker()->current_damage_rect();
1434 EXPECT_TRUE(root_damage_rect
.IsEmpty());
1436 // Damage should remain empty even after one frame, since there's yet no new
1438 EmulateDrawingOneFrame(root
.get());
1440 root
->render_surface()->damage_tracker()->current_damage_rect();
1441 EXPECT_TRUE(root_damage_rect
.IsEmpty());
1444 TEST_F(DamageTrackerTest
, HugeDamageRect
) {
1445 // This number is so large that we start losting floating point accuracy.
1446 const int kBigNumber
= 900000000;
1447 // Walk over a range to find floating point inaccuracy boundaries that move
1448 // toward the wrong direction.
1449 const int kRange
= 5000;
1451 for (int i
= 0; i
< kRange
; ++i
) {
1452 scoped_ptr
<LayerImpl
> root
= CreateTestTreeWithOneSurface();
1453 LayerImpl
* child
= root
->children()[0];
1455 gfx::Transform transform
;
1456 transform
.Translate(-kBigNumber
, -kBigNumber
);
1458 // The child layer covers (0, 0, i, i) of the viewport,
1459 // but has a huge negative position.
1460 child
->SetPosition(gfx::PointF());
1461 child
->SetBounds(gfx::Size(kBigNumber
+ i
, kBigNumber
+ i
));
1462 child
->SetContentBounds(gfx::Size(kBigNumber
+ i
, kBigNumber
+ i
));
1463 child
->SetTransform(transform
);
1464 EmulateDrawingOneFrame(root
.get());
1466 // The expected damage should cover the visible part of the child layer,
1467 // which is (0, 0, i, i) in the viewport.
1468 gfx::Rect root_damage_rect
=
1469 root
->render_surface()->damage_tracker()->current_damage_rect();
1470 gfx::Rect damage_we_care_about
= gfx::Rect(i
, i
);
1471 EXPECT_LE(damage_we_care_about
.right(), root_damage_rect
.right());
1472 EXPECT_LE(damage_we_care_about
.bottom(), root_damage_rect
.bottom());