Preserve viewport scrolling layer when a fling starts
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest_damage.cc
blobfc5cacf087e335156f55c3ea9579dee4913dd305
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"
7 #include "base/bind.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"
19 namespace cc {
20 namespace {
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 {
29 // Viewport is 10x10.
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 {
39 draw_count_ = 0;
40 PostSetNeedsCommitToMainThread();
43 void DidCommitAndDrawFrame() override {
44 switch (layer_tree_host()->source_frame_number()) {
45 case 1:
46 layer_tree_host()->SetNeedsRedraw();
47 break;
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_) {
62 case 0:
63 // The first frame has full damage.
64 EXPECT_EQ(gfx::RectF(10.f, 10.f).ToString(), root_damage.ToString());
65 break;
66 case 1:
67 // The second frame has full damage.
68 EXPECT_EQ(gfx::RectF(10.f, 10.f).ToString(), root_damage.ToString());
69 EndTest();
70 break;
71 case 2:
72 NOTREACHED();
75 ++draw_count_;
76 return draw_result;
79 void AfterTest() override {}
81 int draw_count_;
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 {
91 // Viewport is 10x10.
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 {
101 draw_count_ = 0;
102 PostSetNeedsCommitToMainThread();
105 void DidCommitAndDrawFrame() override {
106 switch (layer_tree_host()->source_frame_number()) {
107 case 1:
108 layer_tree_host()->SetViewportSize(gfx::Size(15, 15));
109 break;
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_) {
124 case 0:
125 // The first frame has full damage.
126 EXPECT_EQ(gfx::RectF(10.f, 10.f).ToString(), root_damage.ToString());
127 break;
128 case 1:
129 // The second frame has full damage.
130 EXPECT_EQ(gfx::RectF(15.f, 15.f).ToString(), root_damage.ToString());
131 EndTest();
132 break;
133 case 2:
134 NOTREACHED();
137 ++draw_count_;
138 return draw_result;
141 void AfterTest() override {}
143 int draw_count_;
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;
153 did_swaps_ = 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) {
179 case 0:
180 // The first frame has damage, so we should draw and swap.
181 ++expect_swap_and_succeed_;
182 break;
183 case 1:
184 // The second frame has no damage, so we should not draw and swap.
185 break;
186 case 2:
187 // The third frame has damage again, so we should draw and swap.
188 ++expect_swap_and_succeed_;
189 break;
190 case 3:
191 // The fourth frame has no visible damage, so we should not draw and
192 // swap.
193 EndTest();
194 break;
196 return draw_result;
199 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
200 ++did_swaps_;
201 if (result)
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) {
209 case 1:
210 layer_tree_host()->SetNeedsCommit();
211 break;
212 case 2:
213 // Cause visible damage.
214 content_->SetNeedsDisplayRect(
215 gfx::Rect(layer_tree_host()->device_viewport_size()));
216 break;
217 case 3:
218 // Cause non-visible damage.
219 content_->SetNeedsDisplayRect(gfx::Rect(1990, 1990, 10, 10));
220 layer_tree_host()->SetNeedsCommit();
221 break;
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_;
234 int did_swaps_;
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) {
269 case 0:
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);
274 break;
275 case 1:
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();
282 break;
283 case 2:
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);
291 break;
292 case 3:
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();
303 break;
304 case 4:
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);
310 EndTest();
311 break;
313 return draw_result;
316 void DidCommitAndDrawFrame() override {
317 if (!TestEnded())
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();
373 private:
374 FakeContentLayerClient client_;
377 class LayerTreeHostDamageTestScrollbarDoesDamage
378 : public LayerTreeHostScrollbarDamageTest {
379 void BeginTest() override {
380 did_swaps_ = 0;
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_) {
394 case 0:
395 // The first frame has damage, so we should draw and swap.
396 break;
397 case 1:
398 // The second frame should not damage the scrollbars.
399 EXPECT_FALSE(root_damage.Intersects(gfx::Rect(300, 300, 10, 100)));
400 break;
401 case 2:
402 // The third frame should damage the scrollbars.
403 EXPECT_TRUE(root_damage.Contains(gfx::Rect(300, 300, 10, 100)));
404 break;
405 case 3:
406 // The fourth frame should damage the scrollbars.
407 EXPECT_TRUE(root_damage.Contains(gfx::Rect(300, 300, 10, 100)));
408 EndTest();
409 break;
411 return draw_result;
414 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
415 ++did_swaps_;
416 EXPECT_TRUE(result);
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_) {
421 case 1:
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();
428 break;
429 case 2:
430 scroll_layer->ScrollBy(gfx::Vector2dF(10.f, 10.f));
431 host_impl->SetNeedsRedraw();
432 break;
433 case 3:
434 // We will resize the content layer, on the main thread.
435 MainThreadTaskRunner()->PostTask(
436 FROM_HERE,
437 base::Bind(
438 &LayerTreeHostDamageTestScrollbarDoesDamage::ResizeScrollLayer,
439 base::Unretained(this)));
440 break;
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_); }
455 int did_swaps_;
458 MULTI_THREAD_TEST_F(LayerTreeHostDamageTestScrollbarDoesDamage);
460 class LayerTreeHostDamageTestScrollbarCommitDoesNoDamage
461 : public LayerTreeHostScrollbarDamageTest {
462 void BeginTest() override {
463 did_swaps_ = 0;
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_) {
478 case 0:
479 // The first frame has damage, so we should draw and swap.
480 EXPECT_EQ(0, frame);
481 break;
482 case 1:
483 // The second frame has scrolled, so the scrollbar should be damaged.
484 EXPECT_EQ(0, frame);
485 EXPECT_TRUE(root_damage.Contains(gfx::Rect(300, 300, 10, 100)));
486 break;
487 case 2:
488 // The third frame (after the commit) has no changes, so it shouldn't.
489 EXPECT_EQ(1, frame);
490 EXPECT_FALSE(root_damage.Intersects(gfx::Rect(300, 300, 10, 100)));
491 break;
492 default:
493 NOTREACHED();
494 break;
496 return draw_result;
499 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
500 ++did_swaps_;
501 EXPECT_TRUE(result);
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_) {
506 case 1:
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();
511 break;
512 case 2:
513 // Forcibly send the scroll to the main thread.
514 PostSetNeedsCommitToMainThread();
515 break;
516 case 3:
517 // First swap after second commit.
518 EndTest();
519 break;
520 default:
521 NOTREACHED();
522 break;
526 void AfterTest() override { EXPECT_EQ(3, did_swaps_); }
528 int did_swaps_;
531 MULTI_THREAD_TEST_F(LayerTreeHostDamageTestScrollbarCommitDoesNoDamage);
533 } // namespace
534 } // namespace cc