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/trees/layer_tree_host_common.h"
16 #include "cc/trees/single_thread_proxy.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
19 #include "ui/gfx/geometry/quad_f.h"
20 #include "ui/gfx/geometry/rect_conversions.h"
25 void ExecuteCalculateDrawProperties(LayerImpl
* root
,
26 LayerImplList
& render_surface_layer_list
) {
27 // Sanity check: The test itself should create the root layer's render
28 // surface, so that the surface (and its damage tracker) can
29 // persist across multiple calls to this function.
30 ASSERT_TRUE(root
->render_surface());
31 ASSERT_FALSE(render_surface_layer_list
.size());
33 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting
inputs(
34 root
, root
->bounds(), &render_surface_layer_list
);
35 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
38 void ClearDamageForAllSurfaces(LayerImpl
* layer
) {
39 if (layer
->render_surface())
40 layer
->render_surface()->damage_tracker()->DidDrawDamagedArea();
42 // Recursively clear damage for any existing surface.
43 for (size_t i
= 0; i
< layer
->children().size(); ++i
)
44 ClearDamageForAllSurfaces(layer
->children()[i
]);
47 void EmulateDrawingOneFrame(LayerImpl
* root
) {
48 // This emulates only steps that are relevant to testing the damage tracker:
49 // 1. computing the render passes and layerlists
50 // 2. updating all damage trackers in the correct order
51 // 3. resetting all update_rects and property_changed flags for all layers
54 LayerImplList render_surface_layer_list
;
55 ExecuteCalculateDrawProperties(root
, render_surface_layer_list
);
57 // Iterate back-to-front, so that damage correctly propagates from descendant
58 // surfaces to ancestors.
59 for (int i
= render_surface_layer_list
.size() - 1; i
>= 0; --i
) {
60 RenderSurfaceImpl
* target_surface
=
61 render_surface_layer_list
[i
]->render_surface();
62 target_surface
->damage_tracker()->UpdateDamageTrackingState(
63 target_surface
->layer_list(),
64 target_surface
->OwningLayerId(),
65 target_surface
->SurfacePropertyChangedOnlyFromDescendant(),
66 target_surface
->content_rect(),
67 render_surface_layer_list
[i
]->mask_layer(),
68 render_surface_layer_list
[i
]->filters());
71 root
->ResetAllChangeTrackingForSubtree();
74 class DamageTrackerTest
: public testing::Test
{
76 DamageTrackerTest() : host_impl_(&proxy_
, &shared_bitmap_manager_
) {}
78 scoped_ptr
<LayerImpl
> CreateTestTreeWithOneSurface() {
79 scoped_ptr
<LayerImpl
> root
=
80 LayerImpl::Create(host_impl_
.active_tree(), 1);
81 scoped_ptr
<LayerImpl
> child
=
82 LayerImpl::Create(host_impl_
.active_tree(), 2);
84 root
->SetPosition(gfx::PointF());
85 root
->SetAnchorPoint(gfx::PointF());
86 root
->SetBounds(gfx::Size(500, 500));
87 root
->SetContentBounds(gfx::Size(500, 500));
88 root
->SetDrawsContent(true);
89 root
->CreateRenderSurface();
90 root
->render_surface()->SetContentRect(gfx::Rect(0, 0, 500, 500));
92 child
->SetPosition(gfx::PointF(100.f
, 100.f
));
93 child
->SetAnchorPoint(gfx::PointF());
94 child
->SetBounds(gfx::Size(30, 30));
95 child
->SetContentBounds(gfx::Size(30, 30));
96 child
->SetDrawsContent(true);
97 root
->AddChild(child
.Pass());
102 scoped_ptr
<LayerImpl
> CreateTestTreeWithTwoSurfaces() {
103 // This test tree has two render surfaces: one for the root, and one for
104 // child1. Additionally, the root has a second child layer, and child1 has
105 // two children of its own.
107 scoped_ptr
<LayerImpl
> root
=
108 LayerImpl::Create(host_impl_
.active_tree(), 1);
109 scoped_ptr
<LayerImpl
> child1
=
110 LayerImpl::Create(host_impl_
.active_tree(), 2);
111 scoped_ptr
<LayerImpl
> child2
=
112 LayerImpl::Create(host_impl_
.active_tree(), 3);
113 scoped_ptr
<LayerImpl
> grand_child1
=
114 LayerImpl::Create(host_impl_
.active_tree(), 4);
115 scoped_ptr
<LayerImpl
> grand_child2
=
116 LayerImpl::Create(host_impl_
.active_tree(), 5);
118 root
->SetPosition(gfx::PointF());
119 root
->SetAnchorPoint(gfx::PointF());
120 root
->SetBounds(gfx::Size(500, 500));
121 root
->SetContentBounds(gfx::Size(500, 500));
122 root
->SetDrawsContent(true);
123 root
->CreateRenderSurface();
124 root
->render_surface()->SetContentRect(gfx::Rect(0, 0, 500, 500));
126 child1
->SetPosition(gfx::PointF(100.f
, 100.f
));
127 child1
->SetAnchorPoint(gfx::PointF());
128 child1
->SetBounds(gfx::Size(30, 30));
129 child1
->SetContentBounds(gfx::Size(30, 30));
130 // With a child that draws_content, opacity will cause the layer to create
131 // its own RenderSurface. This layer does not draw, but is intended to
132 // create its own RenderSurface. TODO: setting opacity and
133 // ForceRenderSurface may be redundant here.
134 child1
->SetOpacity(0.5f
);
135 child1
->SetDrawsContent(false);
136 child1
->SetForceRenderSurface(true);
138 child2
->SetPosition(gfx::PointF(11.f
, 11.f
));
139 child2
->SetAnchorPoint(gfx::PointF());
140 child2
->SetBounds(gfx::Size(18, 18));
141 child2
->SetContentBounds(gfx::Size(18, 18));
142 child2
->SetDrawsContent(true);
144 grand_child1
->SetPosition(gfx::PointF(200.f
, 200.f
));
145 grand_child1
->SetAnchorPoint(gfx::PointF());
146 grand_child1
->SetBounds(gfx::Size(6, 8));
147 grand_child1
->SetContentBounds(gfx::Size(6, 8));
148 grand_child1
->SetDrawsContent(true);
150 grand_child2
->SetPosition(gfx::PointF(190.f
, 190.f
));
151 grand_child2
->SetAnchorPoint(gfx::PointF());
152 grand_child2
->SetBounds(gfx::Size(6, 8));
153 grand_child2
->SetContentBounds(gfx::Size(6, 8));
154 grand_child2
->SetDrawsContent(true);
156 child1
->AddChild(grand_child1
.Pass());
157 child1
->AddChild(grand_child2
.Pass());
158 root
->AddChild(child1
.Pass());
159 root
->AddChild(child2
.Pass());
164 scoped_ptr
<LayerImpl
> CreateAndSetUpTestTreeWithOneSurface() {
165 scoped_ptr
<LayerImpl
> root
= CreateTestTreeWithOneSurface();
167 // Setup includes going past the first frame which always damages
168 // everything, so that we can actually perform specific tests.
169 EmulateDrawingOneFrame(root
.get());
174 scoped_ptr
<LayerImpl
> CreateAndSetUpTestTreeWithTwoSurfaces() {
175 scoped_ptr
<LayerImpl
> root
= CreateTestTreeWithTwoSurfaces();
177 // Setup includes going past the first frame which always damages
178 // everything, so that we can actually perform specific tests.
179 EmulateDrawingOneFrame(root
.get());
185 FakeImplProxy proxy_
;
186 TestSharedBitmapManager shared_bitmap_manager_
;
187 FakeLayerTreeHostImpl host_impl_
;
190 TEST_F(DamageTrackerTest
, SanityCheckTestTreeWithOneSurface
) {
191 // Sanity check that the simple test tree will actually produce the expected
192 // render surfaces and layer lists.
194 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
196 EXPECT_EQ(2u, root
->render_surface()->layer_list().size());
197 EXPECT_EQ(1, root
->render_surface()->layer_list()[0]->id());
198 EXPECT_EQ(2, root
->render_surface()->layer_list()[1]->id());
200 gfx::Rect root_damage_rect
=
201 root
->render_surface()->damage_tracker()->current_damage_rect();
203 EXPECT_EQ(gfx::Rect(500, 500).ToString(), root_damage_rect
.ToString());
206 TEST_F(DamageTrackerTest
, SanityCheckTestTreeWithTwoSurfaces
) {
207 // Sanity check that the complex test tree will actually produce the expected
208 // render surfaces and layer lists.
210 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
212 LayerImpl
* child1
= root
->children()[0];
213 LayerImpl
* child2
= root
->children()[1];
214 gfx::Rect child_damage_rect
=
215 child1
->render_surface()->damage_tracker()->current_damage_rect();
216 gfx::Rect root_damage_rect
=
217 root
->render_surface()->damage_tracker()->current_damage_rect();
219 ASSERT_TRUE(child1
->render_surface());
220 EXPECT_FALSE(child2
->render_surface());
221 EXPECT_EQ(3u, root
->render_surface()->layer_list().size());
222 EXPECT_EQ(2u, child1
->render_surface()->layer_list().size());
224 // The render surface for child1 only has a content_rect that encloses
225 // grand_child1 and grand_child2, because child1 does not draw content.
226 EXPECT_EQ(gfx::Rect(190, 190, 16, 18).ToString(),
227 child_damage_rect
.ToString());
228 EXPECT_EQ(gfx::Rect(500, 500).ToString(), root_damage_rect
.ToString());
231 TEST_F(DamageTrackerTest
, VerifyDamageForUpdateRects
) {
232 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
233 LayerImpl
* child
= root
->children()[0];
235 // CASE 1: Setting the update rect should cause the corresponding damage to
237 ClearDamageForAllSurfaces(root
.get());
238 child
->SetUpdateRect(gfx::RectF(10.f
, 11.f
, 12.f
, 13.f
));
239 EmulateDrawingOneFrame(root
.get());
241 // Damage position on the surface should be: position of update_rect (10, 11)
242 // relative to the child (100, 100).
243 gfx::Rect root_damage_rect
=
244 root
->render_surface()->damage_tracker()->current_damage_rect();
245 EXPECT_EQ(gfx::Rect(110, 111, 12, 13).ToString(),
246 root_damage_rect
.ToString());
248 // CASE 2: The same update rect twice in a row still produces the same
250 ClearDamageForAllSurfaces(root
.get());
251 child
->SetUpdateRect(gfx::RectF(10.f
, 11.f
, 12.f
, 13.f
));
252 EmulateDrawingOneFrame(root
.get());
254 root
->render_surface()->damage_tracker()->current_damage_rect();
255 EXPECT_EQ(gfx::Rect(110, 111, 12, 13).ToString(),
256 root_damage_rect
.ToString());
258 // CASE 3: Setting a different update rect should cause damage on the new
259 // update region, but no additional exposed old region.
260 ClearDamageForAllSurfaces(root
.get());
261 child
->SetUpdateRect(gfx::RectF(20.f
, 25.f
, 1.f
, 2.f
));
262 EmulateDrawingOneFrame(root
.get());
264 // Damage position on the surface should be: position of update_rect (20, 25)
265 // relative to the child (100, 100).
267 root
->render_surface()->damage_tracker()->current_damage_rect();
268 EXPECT_EQ(gfx::Rect(120, 125, 1, 2).ToString(), root_damage_rect
.ToString());
271 TEST_F(DamageTrackerTest
, VerifyDamageForLayerDamageRects
) {
272 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
273 LayerImpl
* child
= root
->children()[0];
275 // CASE 1: Adding the layer damage rect should cause the corresponding damage
277 ClearDamageForAllSurfaces(root
.get());
278 child
->AddDamageRect(gfx::RectF(10.f
, 11.f
, 12.f
, 13.f
));
279 EmulateDrawingOneFrame(root
.get());
281 // Damage position on the surface should be: position of layer damage_rect
282 // (10, 11) relative to the child (100, 100).
283 gfx::Rect root_damage_rect
=
284 root
->render_surface()->damage_tracker()->current_damage_rect();
285 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(110, 111, 12, 13)));
287 // CASE 2: The same layer damage rect twice in a row still produces the same
289 ClearDamageForAllSurfaces(root
.get());
290 child
->AddDamageRect(gfx::RectF(10.f
, 11.f
, 12.f
, 13.f
));
291 EmulateDrawingOneFrame(root
.get());
293 root
->render_surface()->damage_tracker()->current_damage_rect();
294 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(110, 111, 12, 13)));
296 // CASE 3: Adding a different layer damage rect should cause damage on the
297 // new damaged region, but no additional exposed old region.
298 ClearDamageForAllSurfaces(root
.get());
299 child
->AddDamageRect(gfx::RectF(20.f
, 25.f
, 1.f
, 2.f
));
300 EmulateDrawingOneFrame(root
.get());
302 // Damage position on the surface should be: position of layer damage_rect
303 // (20, 25) relative to the child (100, 100).
305 root
->render_surface()->damage_tracker()->current_damage_rect();
306 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(120, 125, 1, 2)));
308 // CASE 4: Adding multiple layer damage rects should cause a unified
309 // damage on root damage rect.
310 ClearDamageForAllSurfaces(root
.get());
311 child
->AddDamageRect(gfx::RectF(20.f
, 25.f
, 1.f
, 2.f
));
312 child
->AddDamageRect(gfx::RectF(10.f
, 15.f
, 3.f
, 4.f
));
313 EmulateDrawingOneFrame(root
.get());
315 // Damage position on the surface should be: position of layer damage_rect
316 // (20, 25) relative to the child (100, 100).
318 root
->render_surface()->damage_tracker()->current_damage_rect();
319 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(120, 125, 1, 2)));
320 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(110, 115, 3, 4)));
323 TEST_F(DamageTrackerTest
, VerifyDamageForLayerUpdateAndDamageRects
) {
324 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
325 LayerImpl
* child
= root
->children()[0];
327 // CASE 1: Adding the layer damage rect and update rect should cause the
328 // corresponding damage to the surface.
329 ClearDamageForAllSurfaces(root
.get());
330 child
->AddDamageRect(gfx::RectF(5.f
, 6.f
, 12.f
, 13.f
));
331 child
->SetUpdateRect(gfx::RectF(15.f
, 16.f
, 14.f
, 10.f
));
332 EmulateDrawingOneFrame(root
.get());
334 // Damage position on the surface should be: position of unified layer
335 // damage_rect and update rect (5, 6)
336 // relative to the child (100, 100).
337 gfx::Rect root_damage_rect
=
338 root
->render_surface()->damage_tracker()->current_damage_rect();
339 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(105, 106, 24, 20)));
341 // CASE 2: The same layer damage rect and update rect twice in a row still
342 // produces the same damage.
343 ClearDamageForAllSurfaces(root
.get());
344 child
->AddDamageRect(gfx::RectF(10.f
, 11.f
, 12.f
, 13.f
));
345 child
->SetUpdateRect(gfx::RectF(10.f
, 11.f
, 14.f
, 15.f
));
346 EmulateDrawingOneFrame(root
.get());
348 root
->render_surface()->damage_tracker()->current_damage_rect();
349 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(110, 111, 14, 15)));
351 // CASE 3: Adding a different layer damage rect and update rect should cause
352 // damage on the new damaged region, but no additional exposed old region.
353 ClearDamageForAllSurfaces(root
.get());
354 child
->AddDamageRect(gfx::RectF(20.f
, 25.f
, 2.f
, 3.f
));
355 child
->SetUpdateRect(gfx::RectF(5.f
, 10.f
, 7.f
, 8.f
));
356 EmulateDrawingOneFrame(root
.get());
358 // Damage position on the surface should be: position of unified layer damage
359 // rect and update rect (5, 10) relative to the child (100, 100).
361 root
->render_surface()->damage_tracker()->current_damage_rect();
362 EXPECT_EQ(true, root_damage_rect
.Contains(gfx::Rect(105, 110, 17, 18)));
365 TEST_F(DamageTrackerTest
, VerifyDamageForPropertyChanges
) {
366 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
367 LayerImpl
* child
= root
->children()[0];
369 // CASE 1: The layer's property changed flag takes priority over update rect.
371 ClearDamageForAllSurfaces(root
.get());
372 child
->SetUpdateRect(gfx::RectF(10.f
, 11.f
, 12.f
, 13.f
));
373 child
->SetOpacity(0.5f
);
374 EmulateDrawingOneFrame(root
.get());
376 // Sanity check - we should not have accidentally created a separate render
377 // surface for the translucent layer.
378 ASSERT_FALSE(child
->render_surface());
379 ASSERT_EQ(2u, root
->render_surface()->layer_list().size());
381 // Damage should be the entire child layer in target_surface space.
382 gfx::Rect expected_rect
= gfx::Rect(100, 100, 30, 30);
383 gfx::Rect root_damage_rect
=
384 root
->render_surface()->damage_tracker()->current_damage_rect();
385 EXPECT_EQ(expected_rect
.ToString(), root_damage_rect
.ToString());
387 // CASE 2: If a layer moves due to property change, it damages both the new
388 // location and the old (exposed) location. The old location is the
389 // entire old layer, not just the update_rect.
391 // Cycle one frame of no change, just to sanity check that the next rect is
392 // not because of the old damage state.
393 ClearDamageForAllSurfaces(root
.get());
394 EmulateDrawingOneFrame(root
.get());
396 root
->render_surface()->damage_tracker()->current_damage_rect();
397 EXPECT_TRUE(root_damage_rect
.IsEmpty());
399 // Then, test the actual layer movement.
400 ClearDamageForAllSurfaces(root
.get());
401 child
->SetPosition(gfx::PointF(200.f
, 230.f
));
402 EmulateDrawingOneFrame(root
.get());
404 // Expect damage to be the combination of the previous one and the new one.
405 expected_rect
.Union(gfx::Rect(200, 230, 30, 30));
407 root
->render_surface()->damage_tracker()->current_damage_rect();
408 EXPECT_FLOAT_RECT_EQ(expected_rect
, root_damage_rect
);
411 TEST_F(DamageTrackerTest
, VerifyDamageForTransformedLayer
) {
412 // If a layer is transformed, the damage rect should still enclose the entire
413 // transformed layer.
415 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
416 LayerImpl
* child
= root
->children()[0];
418 gfx::Transform rotation
;
419 rotation
.Rotate(45.0);
421 ClearDamageForAllSurfaces(root
.get());
422 child
->SetAnchorPoint(gfx::PointF(0.5f
, 0.5f
));
423 child
->SetPosition(gfx::PointF(85.f
, 85.f
));
424 EmulateDrawingOneFrame(root
.get());
426 // Sanity check that the layer actually moved to (85, 85), damaging its old
427 // location and new location.
428 gfx::Rect root_damage_rect
=
429 root
->render_surface()->damage_tracker()->current_damage_rect();
430 EXPECT_EQ(gfx::Rect(85, 85, 45, 45).ToString(), root_damage_rect
.ToString());
432 // With the anchor on the layer's center, now we can test the rotation more
433 // intuitively, since it applies about the layer's anchor.
434 ClearDamageForAllSurfaces(root
.get());
435 child
->SetTransform(rotation
);
436 EmulateDrawingOneFrame(root
.get());
438 // Since the child layer is square, rotation by 45 degrees about the center
439 // should increase the size of the expected rect by sqrt(2), centered around
440 // (100, 100). The old exposed region should be fully contained in the new
442 float expected_width
= 30.f
* sqrt(2.f
);
443 float expected_position
= 100.f
- 0.5f
* expected_width
;
444 gfx::Rect expected_rect
= gfx::ToEnclosingRect(gfx::RectF(
445 expected_position
, expected_position
, expected_width
, expected_width
));
447 root
->render_surface()->damage_tracker()->current_damage_rect();
448 EXPECT_EQ(expected_rect
.ToString(), root_damage_rect
.ToString());
451 TEST_F(DamageTrackerTest
, VerifyDamageForPerspectiveClippedLayer
) {
452 // If a layer has a perspective transform that causes w < 0, then not
453 // clipping the layer can cause an invalid damage rect. This test checks that
454 // the w < 0 case is tracked properly.
456 // The transform is constructed so that if w < 0 clipping is not performed,
457 // the incorrect rect will be very small, specifically: position (500.972504,
458 // 498.544617) and size 0.056610 x 2.910767. Instead, the correctly
459 // transformed rect should actually be very huge (i.e. in theory, -infinity
460 // on the left), and positioned so that the right-most bound rect will be
461 // approximately 501 units in root surface space.
464 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
465 LayerImpl
* child
= root
->children()[0];
467 gfx::Transform transform
;
468 transform
.Translate3d(500.0, 500.0, 0.0);
469 transform
.ApplyPerspectiveDepth(1.0);
470 transform
.RotateAboutYAxis(45.0);
471 transform
.Translate3d(-50.0, -50.0, 0.0);
474 child
->SetPosition(gfx::PointF(0.f
, 0.f
));
475 child
->SetBounds(gfx::Size(100, 100));
476 child
->SetContentBounds(gfx::Size(100, 100));
477 child
->SetTransform(transform
);
478 EmulateDrawingOneFrame(root
.get());
480 // Sanity check that the child layer's bounds would actually get clipped by
481 // w < 0, otherwise this test is not actually testing the intended scenario.
482 gfx::QuadF
test_quad(gfx::RectF(gfx::PointF(), gfx::SizeF(100.f
, 100.f
)));
483 bool clipped
= false;
484 MathUtil::MapQuad(transform
, test_quad
, &clipped
);
485 EXPECT_TRUE(clipped
);
487 // Damage the child without moving it.
488 ClearDamageForAllSurfaces(root
.get());
489 child
->SetOpacity(0.5f
);
490 EmulateDrawingOneFrame(root
.get());
492 // The expected damage should cover the entire root surface (500x500), but we
493 // don't care whether the damage rect was clamped or is larger than the
494 // surface for this test.
495 gfx::Rect root_damage_rect
=
496 root
->render_surface()->damage_tracker()->current_damage_rect();
497 gfx::Rect damage_we_care_about
= gfx::Rect(gfx::Size(500, 500));
498 EXPECT_TRUE(root_damage_rect
.Contains(damage_we_care_about
));
501 TEST_F(DamageTrackerTest
, VerifyDamageForBlurredSurface
) {
502 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
503 LayerImpl
* surface
= root
->children()[0];
504 LayerImpl
* child
= surface
->children()[0];
506 FilterOperations filters
;
507 filters
.Append(FilterOperation::CreateBlurFilter(5.f
));
508 int outset_top
, outset_right
, outset_bottom
, outset_left
;
509 filters
.GetOutsets(&outset_top
, &outset_right
, &outset_bottom
, &outset_left
);
511 // Setting the filter will damage the whole surface.
512 ClearDamageForAllSurfaces(root
.get());
513 surface
->SetFilters(filters
);
514 EmulateDrawingOneFrame(root
.get());
516 // Setting the update rect should cause the corresponding damage to the
517 // surface, blurred based on the size of the blur filter.
518 ClearDamageForAllSurfaces(root
.get());
519 child
->SetUpdateRect(gfx::RectF(1.f
, 2.f
, 3.f
, 4.f
));
520 EmulateDrawingOneFrame(root
.get());
522 // Damage position on the surface should be: position of update_rect (1, 2)
523 // relative to the child (300, 300), but expanded by the blur outsets.
524 gfx::Rect root_damage_rect
=
525 root
->render_surface()->damage_tracker()->current_damage_rect();
526 gfx::Rect expected_damage_rect
= gfx::Rect(301, 302, 3, 4);
528 expected_damage_rect
.Inset(-outset_left
,
532 EXPECT_EQ(expected_damage_rect
.ToString(), root_damage_rect
.ToString());
535 TEST_F(DamageTrackerTest
, VerifyDamageForImageFilter
) {
536 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
537 LayerImpl
* child
= root
->children()[0];
538 gfx::Rect root_damage_rect
, child_damage_rect
;
540 // Allow us to set damage on child too.
541 child
->SetDrawsContent(true);
543 skia::RefPtr
<SkImageFilter
> filter
= skia::AdoptRef(
544 SkBlurImageFilter::Create(SkIntToScalar(2), SkIntToScalar(2)));
545 FilterOperations filters
;
546 filters
.Append(FilterOperation::CreateReferenceFilter(filter
));
548 // Setting the filter will damage the whole surface.
549 ClearDamageForAllSurfaces(root
.get());
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::RectF(1.f
, 1.f
));
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::RectF(297.f
, 297.f
, 2.f
, 2.f
));
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::RectF(297.f
, 297.f
, 30.f
, 30.f
));
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::RectF(30.f
, 30.f
, 2.f
, 2.f
));
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::RectF(99.f
, 99.f
, 1.f
, 1.f
));
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::RectF(0.f
, 0.f
, 1.f
, 1.f
));
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::RectF(0.f
, 0.f
, 1.f
, 1.f
));
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
->SetAnchorPoint(gfx::PointF());
713 child2
->SetBounds(gfx::Size(6, 8));
714 child2
->SetContentBounds(gfx::Size(6, 8));
715 child2
->SetDrawsContent(true);
716 root
->AddChild(child2
.Pass());
718 EmulateDrawingOneFrame(root
.get());
720 // Sanity check - all 3 layers should be on the same render surface; render
721 // surfaces are tested elsewhere.
722 ASSERT_EQ(3u, root
->render_surface()->layer_list().size());
724 gfx::Rect root_damage_rect
=
725 root
->render_surface()->damage_tracker()->current_damage_rect();
726 EXPECT_EQ(gfx::Rect(400, 380, 6, 8).ToString(), root_damage_rect
.ToString());
728 // CASE 2: If the layer is removed, its entire old layer becomes exposed, not
729 // just the last update rect.
731 // Advance one frame without damage so that we know the damage rect is not
732 // leftover from the previous case.
733 ClearDamageForAllSurfaces(root
.get());
734 EmulateDrawingOneFrame(root
.get());
737 root
->render_surface()->damage_tracker()->current_damage_rect();
738 EXPECT_TRUE(root_damage_rect
.IsEmpty());
740 // Then, test removing child1.
741 root
->RemoveChild(child1
);
743 EmulateDrawingOneFrame(root
.get());
746 root
->render_surface()->damage_tracker()->current_damage_rect();
747 EXPECT_EQ(gfx::Rect(100, 100, 30, 30).ToString(),
748 root_damage_rect
.ToString());
751 TEST_F(DamageTrackerTest
, VerifyDamageForNewUnchangedLayer
) {
752 // If child2 is added to the layer tree, but it doesn't have any explicit
753 // damage of its own, it should still indeed damage the target surface.
755 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
757 ClearDamageForAllSurfaces(root
.get());
759 scoped_ptr
<LayerImpl
> child2
=
760 LayerImpl::Create(host_impl_
.active_tree(), 3);
761 child2
->SetPosition(gfx::PointF(400.f
, 380.f
));
762 child2
->SetAnchorPoint(gfx::PointF());
763 child2
->SetBounds(gfx::Size(6, 8));
764 child2
->SetContentBounds(gfx::Size(6, 8));
765 child2
->SetDrawsContent(true);
766 child2
->ResetAllChangeTrackingForSubtree();
767 // Sanity check the initial conditions of the test, if these asserts
768 // trigger, it means the test no longer actually covers the intended
770 ASSERT_FALSE(child2
->LayerPropertyChanged());
771 ASSERT_TRUE(child2
->update_rect().IsEmpty());
772 root
->AddChild(child2
.Pass());
774 EmulateDrawingOneFrame(root
.get());
776 // Sanity check - all 3 layers should be on the same render surface; render
777 // surfaces are tested elsewhere.
778 ASSERT_EQ(3u, root
->render_surface()->layer_list().size());
780 gfx::Rect root_damage_rect
=
781 root
->render_surface()->damage_tracker()->current_damage_rect();
782 EXPECT_EQ(gfx::Rect(400, 380, 6, 8).ToString(), root_damage_rect
.ToString());
785 TEST_F(DamageTrackerTest
, VerifyDamageForMultipleLayers
) {
786 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
787 LayerImpl
* child1
= root
->children()[0];
789 // In this test we don't want the above tree manipulation to be considered
790 // part of the same frame.
791 ClearDamageForAllSurfaces(root
.get());
793 scoped_ptr
<LayerImpl
> child2
=
794 LayerImpl::Create(host_impl_
.active_tree(), 3);
795 child2
->SetPosition(gfx::PointF(400.f
, 380.f
));
796 child2
->SetAnchorPoint(gfx::PointF());
797 child2
->SetBounds(gfx::Size(6, 8));
798 child2
->SetContentBounds(gfx::Size(6, 8));
799 child2
->SetDrawsContent(true);
800 root
->AddChild(child2
.Pass());
802 LayerImpl
* child2
= root
->children()[1];
803 EmulateDrawingOneFrame(root
.get());
805 // Damaging two layers simultaneously should cause combined damage.
806 // - child1 update rect in surface space: gfx::Rect(100, 100, 1, 2);
807 // - child2 update rect in surface space: gfx::Rect(400, 380, 3, 4);
808 ClearDamageForAllSurfaces(root
.get());
809 child1
->SetUpdateRect(gfx::RectF(0.f
, 0.f
, 1.f
, 2.f
));
810 child2
->SetUpdateRect(gfx::RectF(0.f
, 0.f
, 3.f
, 4.f
));
811 EmulateDrawingOneFrame(root
.get());
812 gfx::Rect root_damage_rect
=
813 root
->render_surface()->damage_tracker()->current_damage_rect();
814 EXPECT_EQ(gfx::Rect(100, 100, 303, 284).ToString(),
815 root_damage_rect
.ToString());
818 TEST_F(DamageTrackerTest
, VerifyDamageForNestedSurfaces
) {
819 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
820 LayerImpl
* child1
= root
->children()[0];
821 LayerImpl
* child2
= root
->children()[1];
822 LayerImpl
* grand_child1
= root
->children()[0]->children()[0];
823 gfx::Rect child_damage_rect
;
824 gfx::Rect root_damage_rect
;
826 // CASE 1: Damage to a descendant surface should propagate properly to
828 ClearDamageForAllSurfaces(root
.get());
829 grand_child1
->SetOpacity(0.5f
);
830 EmulateDrawingOneFrame(root
.get());
832 child1
->render_surface()->damage_tracker()->current_damage_rect();
834 root
->render_surface()->damage_tracker()->current_damage_rect();
835 EXPECT_EQ(gfx::Rect(200, 200, 6, 8).ToString(), child_damage_rect
.ToString());
836 EXPECT_EQ(gfx::Rect(300, 300, 6, 8).ToString(), root_damage_rect
.ToString());
838 // CASE 2: Same as previous case, but with additional damage elsewhere that
839 // should be properly unioned.
840 // - child1 surface damage in root surface space:
841 // gfx::Rect(300, 300, 6, 8);
842 // - child2 damage in root surface space:
843 // gfx::Rect(11, 11, 18, 18);
844 ClearDamageForAllSurfaces(root
.get());
845 grand_child1
->SetOpacity(0.7f
);
846 child2
->SetOpacity(0.7f
);
847 EmulateDrawingOneFrame(root
.get());
849 child1
->render_surface()->damage_tracker()->current_damage_rect();
851 root
->render_surface()->damage_tracker()->current_damage_rect();
852 EXPECT_EQ(gfx::Rect(200, 200, 6, 8).ToString(), child_damage_rect
.ToString());
853 EXPECT_EQ(gfx::Rect(11, 11, 295, 297).ToString(),
854 root_damage_rect
.ToString());
857 TEST_F(DamageTrackerTest
, VerifyDamageForSurfaceChangeFromDescendantLayer
) {
858 // If descendant layer changes and affects the content bounds of the render
859 // surface, then the entire descendant surface should be damaged, and it
860 // should damage its ancestor surface with the old and new surface regions.
862 // This is a tricky case, since only the first grand_child changes, but the
863 // entire surface should be marked dirty.
865 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
866 LayerImpl
* child1
= root
->children()[0];
867 LayerImpl
* grand_child1
= root
->children()[0]->children()[0];
868 gfx::Rect child_damage_rect
;
869 gfx::Rect root_damage_rect
;
871 ClearDamageForAllSurfaces(root
.get());
872 grand_child1
->SetPosition(gfx::PointF(195.f
, 205.f
));
873 EmulateDrawingOneFrame(root
.get());
875 child1
->render_surface()->damage_tracker()->current_damage_rect();
877 root
->render_surface()->damage_tracker()->current_damage_rect();
879 // The new surface bounds should be damaged entirely, even though only one of
880 // the layers changed.
881 EXPECT_EQ(gfx::Rect(190, 190, 11, 23).ToString(),
882 child_damage_rect
.ToString());
884 // Damage to the root surface should be the union of child1's *entire* render
885 // surface (in target space), and its old exposed area (also in target
887 EXPECT_EQ(gfx::Rect(290, 290, 16, 23).ToString(),
888 root_damage_rect
.ToString());
891 TEST_F(DamageTrackerTest
, VerifyDamageForSurfaceChangeFromAncestorLayer
) {
892 // An ancestor/owning layer changes that affects the position/transform of
893 // the render surface. Note that in this case, the layer_property_changed flag
894 // already propagates to the subtree (tested in LayerImpltest), which damages
895 // the entire child1 surface, but the damage tracker still needs the correct
896 // logic to compute the exposed region on the root surface.
898 // TODO(shawnsingh): the expectations of this test case should change when we
899 // add support for a unique scissor_rect per RenderSurface. In that case, the
900 // child1 surface should be completely unchanged, since we are only
901 // transforming it, while the root surface would be damaged appropriately.
903 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
904 LayerImpl
* child1
= root
->children()[0];
905 gfx::Rect child_damage_rect
;
906 gfx::Rect root_damage_rect
;
908 ClearDamageForAllSurfaces(root
.get());
909 child1
->SetPosition(gfx::PointF(50.f
, 50.f
));
910 EmulateDrawingOneFrame(root
.get());
912 child1
->render_surface()->damage_tracker()->current_damage_rect();
914 root
->render_surface()->damage_tracker()->current_damage_rect();
916 // The new surface bounds should be damaged entirely.
917 EXPECT_EQ(gfx::Rect(190, 190, 16, 18).ToString(),
918 child_damage_rect
.ToString());
920 // The entire child1 surface and the old exposed child1 surface should damage
922 // - old child1 surface in target space: gfx::Rect(290, 290, 16, 18)
923 // - new child1 surface in target space: gfx::Rect(240, 240, 16, 18)
924 EXPECT_EQ(gfx::Rect(240, 240, 66, 68).ToString(),
925 root_damage_rect
.ToString());
928 TEST_F(DamageTrackerTest
, VerifyDamageForAddingAndRemovingRenderSurfaces
) {
929 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
930 LayerImpl
* child1
= root
->children()[0];
931 gfx::Rect child_damage_rect
;
932 gfx::Rect root_damage_rect
;
934 // CASE 1: If a descendant surface disappears, its entire old area becomes
936 ClearDamageForAllSurfaces(root
.get());
937 child1
->SetOpacity(1.f
);
938 child1
->SetForceRenderSurface(false);
939 EmulateDrawingOneFrame(root
.get());
941 // Sanity check that there is only one surface now.
942 ASSERT_FALSE(child1
->render_surface());
943 ASSERT_EQ(4u, root
->render_surface()->layer_list().size());
946 root
->render_surface()->damage_tracker()->current_damage_rect();
947 EXPECT_EQ(gfx::Rect(290, 290, 16, 18).ToString(),
948 root_damage_rect
.ToString());
950 // CASE 2: If a descendant surface appears, its entire old area becomes
953 // Cycle one frame of no change, just to sanity check that the next rect is
954 // not because of the old damage state.
955 ClearDamageForAllSurfaces(root
.get());
956 EmulateDrawingOneFrame(root
.get());
958 root
->render_surface()->damage_tracker()->current_damage_rect();
959 EXPECT_TRUE(root_damage_rect
.IsEmpty());
961 // Then change the tree so that the render surface is added back.
962 ClearDamageForAllSurfaces(root
.get());
963 child1
->SetOpacity(0.5f
);
964 child1
->SetForceRenderSurface(true);
965 EmulateDrawingOneFrame(root
.get());
967 // Sanity check that there is a new surface now.
968 ASSERT_TRUE(child1
->render_surface());
969 EXPECT_EQ(3u, root
->render_surface()->layer_list().size());
970 EXPECT_EQ(2u, child1
->render_surface()->layer_list().size());
973 child1
->render_surface()->damage_tracker()->current_damage_rect();
975 root
->render_surface()->damage_tracker()->current_damage_rect();
976 EXPECT_EQ(gfx::Rect(190, 190, 16, 18).ToString(),
977 child_damage_rect
.ToString());
978 EXPECT_EQ(gfx::Rect(290, 290, 16, 18).ToString(),
979 root_damage_rect
.ToString());
982 TEST_F(DamageTrackerTest
, VerifyNoDamageWhenNothingChanged
) {
983 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
984 LayerImpl
* child1
= root
->children()[0];
985 gfx::Rect child_damage_rect
;
986 gfx::Rect root_damage_rect
;
988 // CASE 1: If nothing changes, the damage rect should be empty.
990 ClearDamageForAllSurfaces(root
.get());
991 EmulateDrawingOneFrame(root
.get());
993 child1
->render_surface()->damage_tracker()->current_damage_rect();
995 root
->render_surface()->damage_tracker()->current_damage_rect();
996 EXPECT_TRUE(child_damage_rect
.IsEmpty());
997 EXPECT_TRUE(root_damage_rect
.IsEmpty());
999 // CASE 2: If nothing changes twice in a row, the damage rect should still be
1002 ClearDamageForAllSurfaces(root
.get());
1003 EmulateDrawingOneFrame(root
.get());
1005 child1
->render_surface()->damage_tracker()->current_damage_rect();
1007 root
->render_surface()->damage_tracker()->current_damage_rect();
1008 EXPECT_TRUE(child_damage_rect
.IsEmpty());
1009 EXPECT_TRUE(root_damage_rect
.IsEmpty());
1012 TEST_F(DamageTrackerTest
, VerifyNoDamageForUpdateRectThatDoesNotDrawContent
) {
1013 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
1014 LayerImpl
* child1
= root
->children()[0];
1015 gfx::Rect child_damage_rect
;
1016 gfx::Rect root_damage_rect
;
1018 // In our specific tree, the update rect of child1 should not cause any
1019 // damage to any surface because it does not actually draw content.
1020 ClearDamageForAllSurfaces(root
.get());
1021 child1
->SetUpdateRect(gfx::RectF(0.f
, 0.f
, 1.f
, 2.f
));
1022 EmulateDrawingOneFrame(root
.get());
1024 child1
->render_surface()->damage_tracker()->current_damage_rect();
1026 root
->render_surface()->damage_tracker()->current_damage_rect();
1027 EXPECT_TRUE(child_damage_rect
.IsEmpty());
1028 EXPECT_TRUE(root_damage_rect
.IsEmpty());
1031 TEST_F(DamageTrackerTest
, VerifyDamageForReplica
) {
1032 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
1033 LayerImpl
* child1
= root
->children()[0];
1034 LayerImpl
* grand_child1
= child1
->children()[0];
1035 LayerImpl
* grand_child2
= child1
->children()[1];
1037 // Damage on a surface that has a reflection should cause the target surface
1038 // to receive the surface's damage and the surface's reflected damage.
1040 // For this test case, we modify grand_child2, and add grand_child3 to extend
1041 // the bounds of child1's surface. This way, we can test reflection changes
1042 // without changing content_bounds of the surface.
1043 grand_child2
->SetPosition(gfx::PointF(180.f
, 180.f
));
1045 scoped_ptr
<LayerImpl
> grand_child3
=
1046 LayerImpl::Create(host_impl_
.active_tree(), 6);
1047 grand_child3
->SetPosition(gfx::PointF(240.f
, 240.f
));
1048 grand_child3
->SetAnchorPoint(gfx::PointF());
1049 grand_child3
->SetBounds(gfx::Size(10, 10));
1050 grand_child3
->SetContentBounds(gfx::Size(10, 10));
1051 grand_child3
->SetDrawsContent(true);
1052 child1
->AddChild(grand_child3
.Pass());
1054 child1
->SetOpacity(0.5f
);
1055 EmulateDrawingOneFrame(root
.get());
1057 // CASE 1: adding a reflection about the left edge of grand_child1.
1059 ClearDamageForAllSurfaces(root
.get());
1061 scoped_ptr
<LayerImpl
> grand_child1_replica
=
1062 LayerImpl::Create(host_impl_
.active_tree(), 7);
1063 grand_child1_replica
->SetPosition(gfx::PointF());
1064 grand_child1_replica
->SetAnchorPoint(gfx::PointF());
1065 gfx::Transform reflection
;
1066 reflection
.Scale3d(-1.0, 1.0, 1.0);
1067 grand_child1_replica
->SetTransform(reflection
);
1068 grand_child1
->SetReplicaLayer(grand_child1_replica
.Pass());
1070 EmulateDrawingOneFrame(root
.get());
1072 gfx::Rect grand_child_damage_rect
=
1073 grand_child1
->render_surface()->damage_tracker()->current_damage_rect();
1074 gfx::Rect child_damage_rect
=
1075 child1
->render_surface()->damage_tracker()->current_damage_rect();
1076 gfx::Rect root_damage_rect
=
1077 root
->render_surface()->damage_tracker()->current_damage_rect();
1079 // The grand_child surface damage should not include its own replica. The
1080 // child surface damage should include the normal and replica surfaces.
1081 EXPECT_EQ(gfx::Rect(6, 8).ToString(), grand_child_damage_rect
.ToString());
1082 EXPECT_EQ(gfx::Rect(194, 200, 12, 8).ToString(),
1083 child_damage_rect
.ToString());
1084 EXPECT_EQ(gfx::Rect(294, 300, 12, 8).ToString(), root_damage_rect
.ToString());
1086 // CASE 2: moving the descendant surface should cause both the original and
1087 // reflected areas to be damaged on the target.
1088 ClearDamageForAllSurfaces(root
.get());
1089 gfx::Rect old_content_rect
= child1
->render_surface()->content_rect();
1090 grand_child1
->SetPosition(gfx::PointF(195.f
, 205.f
));
1091 EmulateDrawingOneFrame(root
.get());
1092 ASSERT_EQ(old_content_rect
.width(),
1093 child1
->render_surface()->content_rect().width());
1094 ASSERT_EQ(old_content_rect
.height(),
1095 child1
->render_surface()->content_rect().height());
1097 grand_child_damage_rect
=
1098 grand_child1
->render_surface()->
1099 damage_tracker()->current_damage_rect();
1101 child1
->render_surface()->damage_tracker()->current_damage_rect();
1103 root
->render_surface()->damage_tracker()->current_damage_rect();
1105 // The child surface damage should include normal and replica surfaces for
1106 // both old and new locations.
1107 // - old location in target space: gfx::Rect(194, 200, 12, 8)
1108 // - new location in target space: gfx::Rect(189, 205, 12, 8)
1109 EXPECT_EQ(gfx::Rect(6, 8).ToString(), grand_child_damage_rect
.ToString());
1110 EXPECT_EQ(gfx::Rect(189, 200, 17, 13).ToString(),
1111 child_damage_rect
.ToString());
1112 EXPECT_EQ(gfx::Rect(289, 300, 17, 13).ToString(),
1113 root_damage_rect
.ToString());
1115 // CASE 3: removing the reflection should cause the entire region including
1116 // reflection to damage the target surface.
1117 ClearDamageForAllSurfaces(root
.get());
1118 grand_child1
->SetReplicaLayer(scoped_ptr
<LayerImpl
>());
1119 EmulateDrawingOneFrame(root
.get());
1120 ASSERT_EQ(old_content_rect
.width(),
1121 child1
->render_surface()->content_rect().width());
1122 ASSERT_EQ(old_content_rect
.height(),
1123 child1
->render_surface()->content_rect().height());
1125 EXPECT_FALSE(grand_child1
->render_surface());
1127 child1
->render_surface()->damage_tracker()->current_damage_rect();
1129 root
->render_surface()->damage_tracker()->current_damage_rect();
1131 EXPECT_EQ(gfx::Rect(189, 205, 12, 8).ToString(),
1132 child_damage_rect
.ToString());
1133 EXPECT_EQ(gfx::Rect(289, 305, 12, 8).ToString(), root_damage_rect
.ToString());
1136 TEST_F(DamageTrackerTest
, VerifyDamageForMask
) {
1137 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
1138 LayerImpl
* child
= root
->children()[0];
1140 // In the current implementation of the damage tracker, changes to mask
1141 // layers should damage the entire corresponding surface.
1143 ClearDamageForAllSurfaces(root
.get());
1145 // Set up the mask layer.
1147 scoped_ptr
<LayerImpl
> mask_layer
=
1148 LayerImpl::Create(host_impl_
.active_tree(), 3);
1149 mask_layer
->SetPosition(child
->position());
1150 mask_layer
->SetAnchorPoint(gfx::PointF());
1151 mask_layer
->SetBounds(child
->bounds());
1152 mask_layer
->SetContentBounds(child
->bounds());
1153 child
->SetMaskLayer(mask_layer
.Pass());
1155 LayerImpl
* mask_layer
= child
->mask_layer();
1157 // Add opacity and a grand_child so that the render surface persists even
1158 // after we remove the mask.
1159 child
->SetOpacity(0.5f
);
1161 scoped_ptr
<LayerImpl
> grand_child
=
1162 LayerImpl::Create(host_impl_
.active_tree(), 4);
1163 grand_child
->SetPosition(gfx::PointF(2.f
, 2.f
));
1164 grand_child
->SetAnchorPoint(gfx::PointF());
1165 grand_child
->SetBounds(gfx::Size(2, 2));
1166 grand_child
->SetContentBounds(gfx::Size(2, 2));
1167 grand_child
->SetDrawsContent(true);
1168 child
->AddChild(grand_child
.Pass());
1170 EmulateDrawingOneFrame(root
.get());
1172 // Sanity check that a new surface was created for the child.
1173 ASSERT_TRUE(child
->render_surface());
1175 // CASE 1: the update_rect on a mask layer should damage the entire target
1177 ClearDamageForAllSurfaces(root
.get());
1178 mask_layer
->SetUpdateRect(gfx::RectF(1.f
, 2.f
, 3.f
, 4.f
));
1179 EmulateDrawingOneFrame(root
.get());
1180 gfx::Rect child_damage_rect
=
1181 child
->render_surface()->damage_tracker()->current_damage_rect();
1182 EXPECT_EQ(gfx::Rect(30, 30).ToString(), child_damage_rect
.ToString());
1184 // CASE 2: a property change on the mask layer should damage the entire
1187 // Advance one frame without damage so that we know the damage rect is not
1188 // leftover from the previous case.
1189 ClearDamageForAllSurfaces(root
.get());
1190 EmulateDrawingOneFrame(root
.get());
1192 child
->render_surface()->damage_tracker()->current_damage_rect();
1193 EXPECT_TRUE(child_damage_rect
.IsEmpty());
1195 // Then test the property change.
1196 ClearDamageForAllSurfaces(root
.get());
1197 mask_layer
->SetStackingOrderChanged(true);
1199 EmulateDrawingOneFrame(root
.get());
1201 child
->render_surface()->damage_tracker()->current_damage_rect();
1202 EXPECT_EQ(gfx::Rect(30, 30).ToString(), child_damage_rect
.ToString());
1204 // CASE 3: removing the mask also damages the entire target surface.
1207 // Advance one frame without damage so that we know the damage rect is not
1208 // leftover from the previous case.
1209 ClearDamageForAllSurfaces(root
.get());
1210 EmulateDrawingOneFrame(root
.get());
1212 child
->render_surface()->damage_tracker()->current_damage_rect();
1213 EXPECT_TRUE(child_damage_rect
.IsEmpty());
1215 // Then test mask removal.
1216 ClearDamageForAllSurfaces(root
.get());
1217 child
->SetMaskLayer(scoped_ptr
<LayerImpl
>());
1218 ASSERT_TRUE(child
->LayerPropertyChanged());
1219 EmulateDrawingOneFrame(root
.get());
1221 // Sanity check that a render surface still exists.
1222 ASSERT_TRUE(child
->render_surface());
1225 child
->render_surface()->damage_tracker()->current_damage_rect();
1226 EXPECT_EQ(gfx::Rect(30, 30).ToString(), child_damage_rect
.ToString());
1229 TEST_F(DamageTrackerTest
, VerifyDamageForReplicaMask
) {
1230 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
1231 LayerImpl
* child1
= root
->children()[0];
1232 LayerImpl
* grand_child1
= child1
->children()[0];
1234 // Changes to a replica's mask should not damage the original surface,
1235 // because it is not masked. But it does damage the ancestor target surface.
1237 ClearDamageForAllSurfaces(root
.get());
1239 // Create a reflection about the left edge of grand_child1.
1241 scoped_ptr
<LayerImpl
> grand_child1_replica
=
1242 LayerImpl::Create(host_impl_
.active_tree(), 6);
1243 grand_child1_replica
->SetPosition(gfx::PointF());
1244 grand_child1_replica
->SetAnchorPoint(gfx::PointF());
1245 gfx::Transform reflection
;
1246 reflection
.Scale3d(-1.0, 1.0, 1.0);
1247 grand_child1_replica
->SetTransform(reflection
);
1248 grand_child1
->SetReplicaLayer(grand_child1_replica
.Pass());
1250 LayerImpl
* grand_child1_replica
= grand_child1
->replica_layer();
1252 // Set up the mask layer on the replica layer
1254 scoped_ptr
<LayerImpl
> replica_mask_layer
=
1255 LayerImpl::Create(host_impl_
.active_tree(), 7);
1256 replica_mask_layer
->SetPosition(gfx::PointF());
1257 replica_mask_layer
->SetAnchorPoint(gfx::PointF());
1258 replica_mask_layer
->SetBounds(grand_child1
->bounds());
1259 replica_mask_layer
->SetContentBounds(grand_child1
->bounds());
1260 grand_child1_replica
->SetMaskLayer(replica_mask_layer
.Pass());
1262 LayerImpl
* replica_mask_layer
= grand_child1_replica
->mask_layer();
1264 EmulateDrawingOneFrame(root
.get());
1266 // Sanity check that the appropriate render surfaces were created
1267 ASSERT_TRUE(grand_child1
->render_surface());
1269 // CASE 1: a property change on the mask should damage only the reflected
1270 // region on the target surface.
1271 ClearDamageForAllSurfaces(root
.get());
1272 replica_mask_layer
->SetStackingOrderChanged(true);
1273 EmulateDrawingOneFrame(root
.get());
1275 gfx::Rect grand_child_damage_rect
=
1276 grand_child1
->render_surface()->damage_tracker()->current_damage_rect();
1277 gfx::Rect child_damage_rect
=
1278 child1
->render_surface()->damage_tracker()->current_damage_rect();
1280 EXPECT_TRUE(grand_child_damage_rect
.IsEmpty());
1281 EXPECT_EQ(gfx::Rect(194, 200, 6, 8).ToString(), child_damage_rect
.ToString());
1283 // CASE 2: removing the replica mask damages only the reflected region on the
1286 ClearDamageForAllSurfaces(root
.get());
1287 grand_child1_replica
->SetMaskLayer(scoped_ptr
<LayerImpl
>());
1288 EmulateDrawingOneFrame(root
.get());
1290 grand_child_damage_rect
=
1291 grand_child1
->render_surface()->damage_tracker()->
1292 current_damage_rect();
1294 child1
->render_surface()->damage_tracker()->current_damage_rect();
1296 EXPECT_TRUE(grand_child_damage_rect
.IsEmpty());
1297 EXPECT_EQ(gfx::Rect(194, 200, 6, 8).ToString(), child_damage_rect
.ToString());
1300 TEST_F(DamageTrackerTest
, VerifyDamageForReplicaMaskWithAnchor
) {
1301 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithTwoSurfaces();
1302 LayerImpl
* child1
= root
->children()[0];
1303 LayerImpl
* grand_child1
= child1
->children()[0];
1305 // Verify that the correct replica_origin_transform is used for the
1307 ClearDamageForAllSurfaces(root
.get());
1309 // This is not actually the anchor point being tested, but by convention its
1310 // expected to be the same as the replica's anchor point.
1311 grand_child1
->SetAnchorPoint(gfx::PointF(1.f
, 0.f
));
1314 scoped_ptr
<LayerImpl
> grand_child1_replica
=
1315 LayerImpl::Create(host_impl_
.active_tree(), 6);
1316 grand_child1_replica
->SetPosition(gfx::PointF());
1318 // This is the anchor being tested.
1319 grand_child1_replica
->SetAnchorPoint(gfx::PointF(1.f
, 0.f
));
1320 gfx::Transform reflection
;
1321 reflection
.Scale3d(-1.0, 1.0, 1.0);
1322 grand_child1_replica
->SetTransform(reflection
);
1323 grand_child1
->SetReplicaLayer(grand_child1_replica
.Pass());
1325 LayerImpl
* grand_child1_replica
= grand_child1
->replica_layer();
1327 // Set up the mask layer on the replica layer
1329 scoped_ptr
<LayerImpl
> replica_mask_layer
=
1330 LayerImpl::Create(host_impl_
.active_tree(), 7);
1331 replica_mask_layer
->SetPosition(gfx::PointF());
1332 // Note: this is not the anchor being tested.
1333 replica_mask_layer
->SetAnchorPoint(gfx::PointF());
1334 replica_mask_layer
->SetBounds(grand_child1
->bounds());
1335 replica_mask_layer
->SetContentBounds(grand_child1
->bounds());
1336 grand_child1_replica
->SetMaskLayer(replica_mask_layer
.Pass());
1338 LayerImpl
* replica_mask_layer
= grand_child1_replica
->mask_layer();
1340 EmulateDrawingOneFrame(root
.get());
1342 // Sanity check that the appropriate render surfaces were created
1343 ASSERT_TRUE(grand_child1
->render_surface());
1345 // A property change on the replica_mask should damage the reflected region on
1346 // the target surface.
1347 ClearDamageForAllSurfaces(root
.get());
1348 replica_mask_layer
->SetStackingOrderChanged(true);
1350 EmulateDrawingOneFrame(root
.get());
1352 gfx::Rect child_damage_rect
=
1353 child1
->render_surface()->damage_tracker()->current_damage_rect();
1354 EXPECT_EQ(gfx::Rect(206, 200, 6, 8).ToString(), child_damage_rect
.ToString());
1357 TEST_F(DamageTrackerTest
, DamageWhenAddedExternally
) {
1358 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
1359 LayerImpl
* child
= root
->children()[0];
1361 // Case 1: This test ensures that when the tracker is given damage, that
1362 // it is included with any other partial damage.
1364 ClearDamageForAllSurfaces(root
.get());
1365 child
->SetUpdateRect(gfx::RectF(10, 11, 12, 13));
1366 root
->render_surface()->damage_tracker()->AddDamageNextUpdate(
1367 gfx::Rect(15, 16, 32, 33));
1368 EmulateDrawingOneFrame(root
.get());
1369 gfx::Rect root_damage_rect
=
1370 root
->render_surface()->damage_tracker()->current_damage_rect();
1371 EXPECT_EQ(gfx::UnionRects(gfx::Rect(15, 16, 32, 33),
1372 gfx::Rect(100 + 10, 100 + 11, 12, 13)).ToString(),
1373 root_damage_rect
.ToString());
1375 // Case 2: An additional sanity check that adding damage works even when
1376 // nothing on the layer tree changed.
1378 ClearDamageForAllSurfaces(root
.get());
1379 root
->render_surface()->damage_tracker()->AddDamageNextUpdate(
1380 gfx::Rect(30, 31, 14, 15));
1381 EmulateDrawingOneFrame(root
.get());
1383 root
->render_surface()->damage_tracker()->current_damage_rect();
1384 EXPECT_EQ(gfx::Rect(30, 31, 14, 15).ToString(), root_damage_rect
.ToString());
1387 TEST_F(DamageTrackerTest
, VerifyDamageForEmptyLayerList
) {
1388 // Though it should never happen, its a good idea to verify that the damage
1389 // tracker does not crash when it receives an empty layer_list.
1391 scoped_ptr
<LayerImpl
> root
= LayerImpl::Create(host_impl_
.active_tree(), 1);
1392 root
->CreateRenderSurface();
1394 ASSERT_TRUE(root
== root
->render_target());
1395 RenderSurfaceImpl
* target_surface
= root
->render_surface();
1397 LayerImplList empty_list
;
1398 target_surface
->damage_tracker()->UpdateDamageTrackingState(
1400 target_surface
->OwningLayerId(),
1404 FilterOperations());
1406 gfx::Rect damage_rect
=
1407 target_surface
->damage_tracker()->current_damage_rect();
1408 EXPECT_TRUE(damage_rect
.IsEmpty());
1411 TEST_F(DamageTrackerTest
, VerifyDamageAccumulatesUntilReset
) {
1412 // If damage is not cleared, it should accumulate.
1414 scoped_ptr
<LayerImpl
> root
= CreateAndSetUpTestTreeWithOneSurface();
1415 LayerImpl
* child
= root
->children()[0];
1417 ClearDamageForAllSurfaces(root
.get());
1418 child
->SetUpdateRect(gfx::Rect(10.f
, 11.f
, 1.f
, 2.f
));
1419 EmulateDrawingOneFrame(root
.get());
1421 // Sanity check damage after the first frame; this isnt the actual test yet.
1422 gfx::Rect root_damage_rect
=
1423 root
->render_surface()->damage_tracker()->current_damage_rect();
1424 EXPECT_EQ(gfx::Rect(110, 111, 1, 2).ToString(), root_damage_rect
.ToString());
1426 // New damage, without having cleared the previous damage, should be unioned
1427 // to the previous one.
1428 child
->SetUpdateRect(gfx::RectF(20.f
, 25.f
, 1.f
, 2.f
));
1429 EmulateDrawingOneFrame(root
.get());
1431 root
->render_surface()->damage_tracker()->current_damage_rect();
1432 EXPECT_EQ(gfx::Rect(110, 111, 11, 16).ToString(),
1433 root_damage_rect
.ToString());
1435 // If we notify the damage tracker that we drew the damaged area, then damage
1436 // should be emptied.
1437 root
->render_surface()->damage_tracker()->DidDrawDamagedArea();
1439 root
->render_surface()->damage_tracker()->current_damage_rect();
1440 EXPECT_TRUE(root_damage_rect
.IsEmpty());
1442 // Damage should remain empty even after one frame, since there's yet no new
1444 EmulateDrawingOneFrame(root
.get());
1446 root
->render_surface()->damage_tracker()->current_damage_rect();
1447 EXPECT_TRUE(root_damage_rect
.IsEmpty());
1450 TEST_F(DamageTrackerTest
, HugeDamageRect
) {
1451 // This number is so large that we start losting floating point accuracy.
1452 const int kBigNumber
= 900000000;
1453 // Walk over a range to find floating point inaccuracy boundaries that move
1454 // toward the wrong direction.
1455 const int kRange
= 5000;
1457 for (int i
= 0; i
< kRange
; ++i
) {
1458 scoped_ptr
<LayerImpl
> root
= CreateTestTreeWithOneSurface();
1459 LayerImpl
* child
= root
->children()[0];
1461 gfx::Transform transform
;
1462 transform
.Translate(-kBigNumber
, -kBigNumber
);
1464 // The child layer covers (0, 0, i, i) of the viewport,
1465 // but has a huge negative position.
1466 child
->SetPosition(gfx::PointF());
1467 child
->SetBounds(gfx::Size(kBigNumber
+ i
, kBigNumber
+ i
));
1468 child
->SetContentBounds(gfx::Size(kBigNumber
+ i
, kBigNumber
+ i
));
1469 child
->SetTransform(transform
);
1470 EmulateDrawingOneFrame(root
.get());
1472 // The expected damage should cover the visible part of the child layer,
1473 // which is (0, 0, i, i) in the viewport.
1474 gfx::Rect root_damage_rect
=
1475 root
->render_surface()->damage_tracker()->current_damage_rect();
1476 gfx::Rect damage_we_care_about
= gfx::Rect(i
, i
);
1477 EXPECT_LE(damage_we_care_about
.right(), root_damage_rect
.right());
1478 EXPECT_LE(damage_we_care_about
.bottom(), root_damage_rect
.bottom());