1 // Copyright 2012 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/layer_tree_host.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/time/time.h"
11 #include "cc/layers/solid_color_layer.h"
12 #include "cc/test/fake_content_layer_client.h"
13 #include "cc/test/fake_painted_scrollbar_layer.h"
14 #include "cc/test/fake_picture_layer.h"
15 #include "cc/test/layer_tree_test.h"
16 #include "cc/trees/damage_tracker.h"
17 #include "cc/trees/layer_tree_impl.h"
22 // These tests deal with damage tracking.
23 class LayerTreeHostDamageTest
: public LayerTreeTest
{};
25 // LayerTreeHost::SetNeedsRedraw should damage the whole viewport.
26 class LayerTreeHostDamageTestSetNeedsRedraw
27 : public LayerTreeHostDamageTest
{
28 void SetupTree() override
{
30 scoped_refptr
<FakePictureLayer
> root
=
31 FakePictureLayer::Create(layer_settings(), &client_
);
32 root
->SetBounds(gfx::Size(10, 10));
34 layer_tree_host()->SetRootLayer(root
);
35 LayerTreeHostDamageTest::SetupTree();
38 void BeginTest() override
{
40 PostSetNeedsCommitToMainThread();
43 void DidCommitAndDrawFrame() override
{
44 switch (layer_tree_host()->source_frame_number()) {
46 layer_tree_host()->SetNeedsRedraw();
51 DrawResult
PrepareToDrawOnThread(LayerTreeHostImpl
* impl
,
52 LayerTreeHostImpl::FrameData
* frame_data
,
53 DrawResult draw_result
) override
{
54 EXPECT_EQ(DRAW_SUCCESS
, draw_result
);
56 RenderSurfaceImpl
* root_surface
=
57 impl
->active_tree()->root_layer()->render_surface();
58 gfx::RectF root_damage
=
59 root_surface
->damage_tracker()->current_damage_rect();
61 switch (draw_count_
) {
63 // The first frame has full damage.
64 EXPECT_EQ(gfx::RectF(10.f
, 10.f
).ToString(), root_damage
.ToString());
67 // The second frame has full damage.
68 EXPECT_EQ(gfx::RectF(10.f
, 10.f
).ToString(), root_damage
.ToString());
79 void AfterTest() override
{}
82 FakeContentLayerClient client_
;
85 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDamageTestSetNeedsRedraw
);
87 // LayerTreeHost::SetViewportSize should damage the whole viewport.
88 class LayerTreeHostDamageTestSetViewportSize
89 : public LayerTreeHostDamageTest
{
90 void SetupTree() override
{
92 scoped_refptr
<FakePictureLayer
> root
=
93 FakePictureLayer::Create(layer_settings(), &client_
);
94 root
->SetBounds(gfx::Size(10, 10));
96 layer_tree_host()->SetRootLayer(root
);
97 LayerTreeHostDamageTest::SetupTree();
100 void BeginTest() override
{
102 PostSetNeedsCommitToMainThread();
105 void DidCommitAndDrawFrame() override
{
106 switch (layer_tree_host()->source_frame_number()) {
108 layer_tree_host()->SetViewportSize(gfx::Size(15, 15));
113 DrawResult
PrepareToDrawOnThread(LayerTreeHostImpl
* impl
,
114 LayerTreeHostImpl::FrameData
* frame_data
,
115 DrawResult draw_result
) override
{
116 EXPECT_EQ(DRAW_SUCCESS
, draw_result
);
118 RenderSurfaceImpl
* root_surface
=
119 impl
->active_tree()->root_layer()->render_surface();
120 gfx::RectF root_damage
=
121 root_surface
->damage_tracker()->current_damage_rect();
123 switch (draw_count_
) {
125 // The first frame has full damage.
126 EXPECT_EQ(gfx::RectF(10.f
, 10.f
).ToString(), root_damage
.ToString());
129 // The second frame has full damage.
130 EXPECT_EQ(gfx::RectF(15.f
, 15.f
).ToString(), root_damage
.ToString());
141 void AfterTest() override
{}
144 FakeContentLayerClient client_
;
147 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDamageTestSetViewportSize
);
149 class LayerTreeHostDamageTestNoDamageDoesNotSwap
150 : public LayerTreeHostDamageTest
{
151 void BeginTest() override
{
152 expect_swap_and_succeed_
= 0;
154 did_swap_and_succeed_
= 0;
155 PostSetNeedsCommitToMainThread();
158 void SetupTree() override
{
159 scoped_refptr
<FakePictureLayer
> root
=
160 FakePictureLayer::Create(layer_settings(), &client_
);
161 root
->SetBounds(gfx::Size(10, 10));
163 // Most of the layer isn't visible.
164 content_
= FakePictureLayer::Create(layer_settings(), &client_
);
165 content_
->SetBounds(gfx::Size(2000, 100));
166 root
->AddChild(content_
);
168 layer_tree_host()->SetRootLayer(root
);
169 LayerTreeHostDamageTest::SetupTree();
172 DrawResult
PrepareToDrawOnThread(LayerTreeHostImpl
* host_impl
,
173 LayerTreeHostImpl::FrameData
* frame_data
,
174 DrawResult draw_result
) override
{
175 EXPECT_EQ(DRAW_SUCCESS
, draw_result
);
177 int source_frame
= host_impl
->active_tree()->source_frame_number();
178 switch (source_frame
) {
180 // The first frame has damage, so we should draw and swap.
181 ++expect_swap_and_succeed_
;
184 // The second frame has no damage, so we should not draw and swap.
187 // The third frame has damage again, so we should draw and swap.
188 ++expect_swap_and_succeed_
;
191 // The fourth frame has no visible damage, so we should not draw and
199 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
202 ++did_swap_and_succeed_
;
203 EXPECT_EQ(expect_swap_and_succeed_
, did_swap_and_succeed_
);
206 void DidCommit() override
{
207 int next_frame
= layer_tree_host()->source_frame_number();
208 switch (next_frame
) {
210 layer_tree_host()->SetNeedsCommit();
213 // Cause visible damage.
214 content_
->SetNeedsDisplayRect(
215 gfx::Rect(layer_tree_host()->device_viewport_size()));
218 // Cause non-visible damage.
219 content_
->SetNeedsDisplayRect(gfx::Rect(1990, 1990, 10, 10));
220 layer_tree_host()->SetNeedsCommit();
225 void AfterTest() override
{
226 EXPECT_EQ(4, did_swaps_
);
227 EXPECT_EQ(2, expect_swap_and_succeed_
);
228 EXPECT_EQ(expect_swap_and_succeed_
, did_swap_and_succeed_
);
231 FakeContentLayerClient client_
;
232 scoped_refptr
<FakePictureLayer
> content_
;
233 int expect_swap_and_succeed_
;
235 int did_swap_and_succeed_
;
238 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDamageTestNoDamageDoesNotSwap
);
240 class LayerTreeHostDamageTestForcedFullDamage
: public LayerTreeHostDamageTest
{
241 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
243 void SetupTree() override
{
244 root_
= FakePictureLayer::Create(layer_settings(), &client_
);
245 child_
= FakePictureLayer::Create(layer_settings(), &client_
);
247 root_
->SetBounds(gfx::Size(500, 500));
248 child_
->SetPosition(gfx::Point(100, 100));
249 child_
->SetBounds(gfx::Size(30, 30));
251 root_
->AddChild(child_
);
252 layer_tree_host()->SetRootLayer(root_
);
253 LayerTreeHostDamageTest::SetupTree();
256 DrawResult
PrepareToDrawOnThread(LayerTreeHostImpl
* host_impl
,
257 LayerTreeHostImpl::FrameData
* frame_data
,
258 DrawResult draw_result
) override
{
259 EXPECT_EQ(DRAW_SUCCESS
, draw_result
);
261 RenderSurfaceImpl
* root_surface
=
262 host_impl
->active_tree()->root_layer()->render_surface();
263 gfx::RectF root_damage
=
264 root_surface
->damage_tracker()->current_damage_rect();
265 root_damage
.Intersect(root_surface
->content_rect());
267 int source_frame
= host_impl
->active_tree()->source_frame_number();
268 switch (source_frame
) {
270 // The first frame draws and clears any damage.
271 EXPECT_EQ(gfx::RectF(root_surface
->content_rect()).ToString(),
272 root_damage
.ToString());
273 EXPECT_FALSE(frame_data
->has_no_damage
);
276 // If we get a frame without damage then we don't draw.
277 EXPECT_EQ(gfx::RectF().ToString(), root_damage
.ToString());
278 EXPECT_TRUE(frame_data
->has_no_damage
);
280 // Then we set full damage for the next frame.
281 host_impl
->SetFullRootLayerDamage();
284 // The whole frame should be damaged as requested.
285 EXPECT_EQ(gfx::RectF(root_surface
->content_rect()).ToString(),
286 root_damage
.ToString());
287 EXPECT_FALSE(frame_data
->has_no_damage
);
289 // Just a part of the next frame should be damaged.
290 child_damage_rect_
= gfx::Rect(10, 11, 12, 13);
293 // The update rect in the child should be damaged and the damaged area
294 // should match the invalidation.
295 EXPECT_EQ(gfx::RectF(100+10, 100+11, 12, 13).ToString(),
296 root_damage
.ToString());
297 EXPECT_FALSE(frame_data
->has_no_damage
);
299 // If we damage part of the frame, but also damage the full
300 // frame, then the whole frame should be damaged.
301 child_damage_rect_
= gfx::Rect(10, 11, 12, 13);
302 host_impl
->SetFullRootLayerDamage();
305 // The whole frame is damaged.
306 EXPECT_EQ(gfx::RectF(root_surface
->content_rect()).ToString(),
307 root_damage
.ToString());
308 EXPECT_FALSE(frame_data
->has_no_damage
);
316 void DidCommitAndDrawFrame() override
{
318 layer_tree_host()->SetNeedsCommit();
320 if (!child_damage_rect_
.IsEmpty()) {
321 child_
->SetNeedsDisplayRect(child_damage_rect_
);
322 child_damage_rect_
= gfx::Rect();
326 void AfterTest() override
{}
328 FakeContentLayerClient client_
;
329 scoped_refptr
<FakePictureLayer
> root_
;
330 scoped_refptr
<FakePictureLayer
> child_
;
331 gfx::Rect child_damage_rect_
;
334 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostDamageTestForcedFullDamage
);
336 class LayerTreeHostScrollbarDamageTest
: public LayerTreeHostDamageTest
{
337 void SetupTree() override
{
338 scoped_refptr
<Layer
> root_layer
= Layer::Create(layer_settings());
339 root_layer
->SetBounds(gfx::Size(400, 400));
340 root_layer
->SetMasksToBounds(true);
341 layer_tree_host()->SetRootLayer(root_layer
);
343 scoped_refptr
<Layer
> scroll_clip_layer
= Layer::Create(layer_settings());
344 scoped_refptr
<Layer
> content_layer
=
345 FakePictureLayer::Create(layer_settings(), &client_
);
346 content_layer
->SetScrollClipLayerId(scroll_clip_layer
->id());
347 content_layer
->SetScrollOffset(gfx::ScrollOffset(10, 20));
348 content_layer
->SetBounds(gfx::Size(100, 200));
349 content_layer
->SetIsDrawable(true);
350 scroll_clip_layer
->SetBounds(
351 gfx::Size(content_layer
->bounds().width() - 30,
352 content_layer
->bounds().height() - 50));
353 scroll_clip_layer
->AddChild(content_layer
);
354 root_layer
->AddChild(scroll_clip_layer
);
356 scoped_refptr
<Layer
> scrollbar_layer
= FakePaintedScrollbarLayer::Create(
357 layer_settings(), false, true, content_layer
->id());
358 scrollbar_layer
->SetPosition(gfx::Point(300, 300));
359 scrollbar_layer
->SetBounds(gfx::Size(10, 100));
360 scrollbar_layer
->ToScrollbarLayer()->SetClipLayer(scroll_clip_layer
->id());
361 scrollbar_layer
->ToScrollbarLayer()->SetScrollLayer(content_layer
->id());
362 root_layer
->AddChild(scrollbar_layer
);
364 gfx::RectF
content_rect(content_layer
->position(),
365 content_layer
->bounds());
366 gfx::RectF
scrollbar_rect(scrollbar_layer
->position(),
367 scrollbar_layer
->bounds());
368 EXPECT_FALSE(content_rect
.Intersects(scrollbar_rect
));
370 LayerTreeHostDamageTest::SetupTree();
374 FakeContentLayerClient client_
;
377 class LayerTreeHostDamageTestScrollbarDoesDamage
378 : public LayerTreeHostScrollbarDamageTest
{
379 void BeginTest() override
{
381 PostSetNeedsCommitToMainThread();
384 DrawResult
PrepareToDrawOnThread(LayerTreeHostImpl
* host_impl
,
385 LayerTreeHostImpl::FrameData
* frame_data
,
386 DrawResult draw_result
) override
{
387 EXPECT_EQ(DRAW_SUCCESS
, draw_result
);
388 RenderSurfaceImpl
* root_surface
=
389 host_impl
->active_tree()->root_layer()->render_surface();
390 gfx::RectF root_damage
=
391 root_surface
->damage_tracker()->current_damage_rect();
392 root_damage
.Intersect(root_surface
->content_rect());
393 switch (did_swaps_
) {
395 // The first frame has damage, so we should draw and swap.
398 // The second frame should not damage the scrollbars.
399 EXPECT_FALSE(root_damage
.Intersects(gfx::Rect(300, 300, 10, 100)));
402 // The third frame should damage the scrollbars.
403 EXPECT_TRUE(root_damage
.Contains(gfx::Rect(300, 300, 10, 100)));
406 // The fourth frame should damage the scrollbars.
407 EXPECT_TRUE(root_damage
.Contains(gfx::Rect(300, 300, 10, 100)));
414 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
417 LayerImpl
* root
= host_impl
->active_tree()->root_layer();
418 LayerImpl
* scroll_clip_layer
= root
->children()[0];
419 LayerImpl
* scroll_layer
= scroll_clip_layer
->children()[0];
420 switch (did_swaps_
) {
422 // Test that modifying the position of the content layer (not
423 // scrolling) won't damage the scrollbar.
424 scroll_layer
->SetPosition(gfx::Point(1, 1));
425 scroll_layer
->PushScrollOffsetFromMainThread(
426 scroll_layer
->BaseScrollOffset());
427 host_impl
->SetNeedsRedraw();
430 scroll_layer
->ScrollBy(gfx::Vector2dF(10.f
, 10.f
));
431 host_impl
->SetNeedsRedraw();
434 // We will resize the content layer, on the main thread.
435 MainThreadTaskRunner()->PostTask(
438 &LayerTreeHostDamageTestScrollbarDoesDamage::ResizeScrollLayer
,
439 base::Unretained(this)));
444 void ResizeScrollLayer() {
445 EXPECT_EQ(3, did_swaps_
);
446 Layer
* root
= layer_tree_host()->root_layer();
447 Layer
* scroll_clip_layer
= root
->child_at(0);
448 Layer
* scroll_layer
= scroll_clip_layer
->child_at(0);
449 scroll_layer
->SetBounds(
450 gfx::Size(root
->bounds().width() + 60, root
->bounds().height() + 100));
453 void AfterTest() override
{ EXPECT_EQ(4, did_swaps_
); }
458 MULTI_THREAD_TEST_F(LayerTreeHostDamageTestScrollbarDoesDamage
);
460 class LayerTreeHostDamageTestScrollbarCommitDoesNoDamage
461 : public LayerTreeHostScrollbarDamageTest
{
462 void BeginTest() override
{
464 PostSetNeedsCommitToMainThread();
467 DrawResult
PrepareToDrawOnThread(LayerTreeHostImpl
* host_impl
,
468 LayerTreeHostImpl::FrameData
* frame_data
,
469 DrawResult draw_result
) override
{
470 EXPECT_EQ(DRAW_SUCCESS
, draw_result
);
471 RenderSurfaceImpl
* root_surface
=
472 host_impl
->active_tree()->root_layer()->render_surface();
473 gfx::RectF root_damage
=
474 root_surface
->damage_tracker()->current_damage_rect();
475 root_damage
.Intersect(root_surface
->content_rect());
476 int frame
= host_impl
->active_tree()->source_frame_number();
477 switch (did_swaps_
) {
479 // The first frame has damage, so we should draw and swap.
483 // The second frame has scrolled, so the scrollbar should be damaged.
485 EXPECT_TRUE(root_damage
.Contains(gfx::Rect(300, 300, 10, 100)));
488 // The third frame (after the commit) has no changes, so it shouldn't.
490 EXPECT_FALSE(root_damage
.Intersects(gfx::Rect(300, 300, 10, 100)));
499 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
502 LayerImpl
* root
= host_impl
->active_tree()->root_layer();
503 LayerImpl
* scroll_clip_layer
= root
->children()[0];
504 LayerImpl
* scroll_layer
= scroll_clip_layer
->children()[0];
505 switch (did_swaps_
) {
507 // Scroll on the thread. This should damage the scrollbar for the
508 // next draw on the thread.
509 scroll_layer
->ScrollBy(gfx::Vector2dF(10.f
, 10.f
));
510 host_impl
->SetNeedsRedraw();
513 // Forcibly send the scroll to the main thread.
514 PostSetNeedsCommitToMainThread();
517 // First swap after second commit.
526 void AfterTest() override
{ EXPECT_EQ(3, did_swaps_
); }
531 MULTI_THREAD_TEST_F(LayerTreeHostDamageTestScrollbarCommitDoesNoDamage
);