Update V8 to version 4.7.42.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest.cc
blobfd9d18b9a00dad4d30154369c93abd09b9899de5
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/layer_tree_host.h"
7 #include <algorithm>
9 #include "base/auto_reset.h"
10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/synchronization/lock.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "cc/animation/timing_function.h"
15 #include "cc/debug/frame_rate_counter.h"
16 #include "cc/layers/content_layer_client.h"
17 #include "cc/layers/io_surface_layer.h"
18 #include "cc/layers/layer_impl.h"
19 #include "cc/layers/painted_scrollbar_layer.h"
20 #include "cc/layers/picture_layer.h"
21 #include "cc/layers/solid_color_layer.h"
22 #include "cc/layers/video_layer.h"
23 #include "cc/output/begin_frame_args.h"
24 #include "cc/output/compositor_frame_ack.h"
25 #include "cc/output/copy_output_request.h"
26 #include "cc/output/copy_output_result.h"
27 #include "cc/output/output_surface.h"
28 #include "cc/output/swap_promise.h"
29 #include "cc/quads/draw_quad.h"
30 #include "cc/quads/io_surface_draw_quad.h"
31 #include "cc/quads/render_pass_draw_quad.h"
32 #include "cc/quads/tile_draw_quad.h"
33 #include "cc/test/fake_content_layer_client.h"
34 #include "cc/test/fake_layer_tree_host_client.h"
35 #include "cc/test/fake_output_surface.h"
36 #include "cc/test/fake_painted_scrollbar_layer.h"
37 #include "cc/test/fake_picture_layer.h"
38 #include "cc/test/fake_picture_layer_impl.h"
39 #include "cc/test/fake_picture_pile.h"
40 #include "cc/test/fake_proxy.h"
41 #include "cc/test/fake_scoped_ui_resource.h"
42 #include "cc/test/fake_video_frame_provider.h"
43 #include "cc/test/geometry_test_utils.h"
44 #include "cc/test/layer_tree_test.h"
45 #include "cc/test/test_shared_bitmap_manager.h"
46 #include "cc/test/test_web_graphics_context_3d.h"
47 #include "cc/trees/layer_tree_host_impl.h"
48 #include "cc/trees/layer_tree_impl.h"
49 #include "cc/trees/single_thread_proxy.h"
50 #include "cc/trees/thread_proxy.h"
51 #include "gpu/GLES2/gl2extchromium.h"
52 #include "skia/ext/refptr.h"
53 #include "testing/gmock/include/gmock/gmock.h"
54 #include "third_party/khronos/GLES2/gl2.h"
55 #include "third_party/khronos/GLES2/gl2ext.h"
56 #include "third_party/skia/include/core/SkPicture.h"
57 #include "ui/gfx/geometry/point_conversions.h"
58 #include "ui/gfx/geometry/size_conversions.h"
59 #include "ui/gfx/geometry/vector2d_conversions.h"
61 using testing::_;
62 using testing::AnyNumber;
63 using testing::AtLeast;
64 using testing::Mock;
66 namespace cc {
67 namespace {
69 class LayerTreeHostTest : public LayerTreeTest {};
71 class LayerTreeHostTestHasImplThreadTest : public LayerTreeHostTest {
72 public:
73 LayerTreeHostTestHasImplThreadTest() : threaded_(false) {}
75 void RunTest(bool threaded, bool delegating_renderer) override {
76 threaded_ = threaded;
77 LayerTreeHostTest::RunTest(threaded, delegating_renderer);
80 void BeginTest() override {
81 EXPECT_EQ(threaded_, HasImplThread());
82 EndTest();
85 void AfterTest() override { EXPECT_EQ(threaded_, HasImplThread()); }
87 private:
88 bool threaded_;
91 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestHasImplThreadTest);
93 class LayerTreeHostTestSetNeedsCommitInsideLayout : public LayerTreeHostTest {
94 protected:
95 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
97 void Layout() override {
98 // This shouldn't cause a second commit to happen.
99 layer_tree_host()->SetNeedsCommit();
102 void DidCommit() override {
103 EXPECT_EQ(1, layer_tree_host()->source_frame_number());
104 EndTest();
107 void AfterTest() override {}
110 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsCommitInsideLayout);
112 class LayerTreeHostTestFrameOrdering : public LayerTreeHostTest {
113 protected:
114 enum MainOrder : int {
115 MAIN_START = 1,
116 MAIN_LAYOUT,
117 MAIN_COMMIT_COMPLETE,
118 MAIN_DID_BEGIN_FRAME,
119 MAIN_END,
122 enum ImplOrder : int {
123 IMPL_START = 1,
124 IMPL_COMMIT,
125 IMPL_COMMIT_COMPLETE,
126 IMPL_ACTIVATE,
127 IMPL_DRAW,
128 IMPL_SWAP,
129 IMPL_END,
132 template <typename T>
133 bool CheckStep(T next, T* var) {
134 int expected = next - 1;
135 EXPECT_EQ(expected, *var);
136 bool correct = expected == *var;
137 *var = next;
138 return correct;
141 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
143 void Layout() override { EXPECT_TRUE(CheckStep(MAIN_LAYOUT, &main_)); }
145 void DidCommit() override {
146 EXPECT_TRUE(CheckStep(MAIN_COMMIT_COMPLETE, &main_));
149 void DidBeginMainFrame() override {
150 EXPECT_TRUE(CheckStep(MAIN_DID_BEGIN_FRAME, &main_));
153 void BeginCommitOnThread(LayerTreeHostImpl* impl) override {
154 EXPECT_TRUE(CheckStep(IMPL_COMMIT, &impl_));
157 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
158 EXPECT_TRUE(CheckStep(IMPL_COMMIT_COMPLETE, &impl_));
161 void WillActivateTreeOnThread(LayerTreeHostImpl* impl) override {
162 EXPECT_TRUE(CheckStep(IMPL_ACTIVATE, &impl_));
165 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
166 EXPECT_TRUE(CheckStep(IMPL_DRAW, &impl_));
169 void SwapBuffersCompleteOnThread(LayerTreeHostImpl* impl) override {
170 EXPECT_TRUE(CheckStep(IMPL_SWAP, &impl_));
172 EndTest();
175 void AfterTest() override {
176 EXPECT_TRUE(CheckStep(MAIN_END, &main_));
177 EXPECT_TRUE(CheckStep(IMPL_END, &impl_));
180 MainOrder main_ = MAIN_START;
181 ImplOrder impl_ = IMPL_START;
184 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestFrameOrdering);
186 class LayerTreeHostTestSetNeedsUpdateInsideLayout : public LayerTreeHostTest {
187 protected:
188 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
190 void Layout() override {
191 // This shouldn't cause a second commit to happen.
192 layer_tree_host()->SetNeedsUpdateLayers();
195 void DidCommit() override {
196 EXPECT_EQ(1, layer_tree_host()->source_frame_number());
197 EndTest();
200 void AfterTest() override {}
203 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsUpdateInsideLayout);
205 // Test if the LTHI receives ReadyToActivate notifications from the TileManager
206 // when no raster tasks get scheduled.
207 class LayerTreeHostTestReadyToActivateEmpty : public LayerTreeHostTest {
208 public:
209 LayerTreeHostTestReadyToActivateEmpty()
210 : did_notify_ready_to_activate_(false),
211 all_tiles_required_for_activation_are_ready_to_draw_(false),
212 required_for_activation_count_(0) {}
214 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
216 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
217 const std::vector<PictureLayerImpl*>& layers =
218 impl->sync_tree()->picture_layers();
219 required_for_activation_count_ = 0;
220 for (const auto& layer : layers) {
221 FakePictureLayerImpl* fake_layer =
222 static_cast<FakePictureLayerImpl*>(layer);
223 required_for_activation_count_ +=
224 fake_layer->CountTilesRequiredForActivation();
228 void NotifyReadyToActivateOnThread(LayerTreeHostImpl* impl) override {
229 did_notify_ready_to_activate_ = true;
230 all_tiles_required_for_activation_are_ready_to_draw_ =
231 impl->tile_manager()->IsReadyToActivate();
232 EndTest();
235 void AfterTest() override {
236 EXPECT_TRUE(did_notify_ready_to_activate_);
237 EXPECT_TRUE(all_tiles_required_for_activation_are_ready_to_draw_);
238 EXPECT_EQ(size_t(0), required_for_activation_count_);
241 protected:
242 bool did_notify_ready_to_activate_;
243 bool all_tiles_required_for_activation_are_ready_to_draw_;
244 size_t required_for_activation_count_;
247 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestReadyToActivateEmpty);
249 // Test if the LTHI receives ReadyToActivate notifications from the TileManager
250 // when some raster tasks flagged as REQUIRED_FOR_ACTIVATION got scheduled.
251 class LayerTreeHostTestReadyToActivateNonEmpty
252 : public LayerTreeHostTestReadyToActivateEmpty {
253 public:
254 void SetupTree() override {
255 client_.set_fill_with_nonsolid_color(true);
256 scoped_refptr<FakePictureLayer> root_layer =
257 FakePictureLayer::Create(layer_settings(), &client_);
258 root_layer->SetBounds(gfx::Size(1024, 1024));
259 root_layer->SetIsDrawable(true);
261 layer_tree_host()->SetRootLayer(root_layer);
262 LayerTreeHostTest::SetupTree();
265 void AfterTest() override {
266 EXPECT_TRUE(did_notify_ready_to_activate_);
267 EXPECT_TRUE(all_tiles_required_for_activation_are_ready_to_draw_);
268 EXPECT_LE(size_t(1), required_for_activation_count_);
271 private:
272 FakeContentLayerClient client_;
275 // Multi-thread only because in single thread the commit goes directly to the
276 // active tree, so notify ready to activate is skipped.
277 MULTI_THREAD_TEST_F(LayerTreeHostTestReadyToActivateNonEmpty);
279 // Test if the LTHI receives ReadyToDraw notifications from the TileManager when
280 // no raster tasks get scheduled.
281 class LayerTreeHostTestReadyToDrawEmpty : public LayerTreeHostTest {
282 public:
283 LayerTreeHostTestReadyToDrawEmpty()
284 : did_notify_ready_to_draw_(false),
285 all_tiles_required_for_draw_are_ready_to_draw_(false),
286 required_for_draw_count_(0) {}
288 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
290 void NotifyReadyToDrawOnThread(LayerTreeHostImpl* impl) override {
291 did_notify_ready_to_draw_ = true;
292 const std::vector<PictureLayerImpl*>& layers =
293 impl->active_tree()->picture_layers();
294 all_tiles_required_for_draw_are_ready_to_draw_ =
295 impl->tile_manager()->IsReadyToDraw();
296 for (const auto& layer : layers) {
297 FakePictureLayerImpl* fake_layer =
298 static_cast<FakePictureLayerImpl*>(layer);
299 required_for_draw_count_ += fake_layer->CountTilesRequiredForDraw();
302 EndTest();
305 void AfterTest() override {
306 EXPECT_TRUE(did_notify_ready_to_draw_);
307 EXPECT_TRUE(all_tiles_required_for_draw_are_ready_to_draw_);
308 EXPECT_EQ(size_t(0), required_for_draw_count_);
311 protected:
312 bool did_notify_ready_to_draw_;
313 bool all_tiles_required_for_draw_are_ready_to_draw_;
314 size_t required_for_draw_count_;
317 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestReadyToDrawEmpty);
319 // Test if the LTHI receives ReadyToDraw notifications from the TileManager when
320 // some raster tasks flagged as REQUIRED_FOR_DRAW got scheduled.
321 class LayerTreeHostTestReadyToDrawNonEmpty
322 : public LayerTreeHostTestReadyToDrawEmpty {
323 public:
324 void SetupTree() override {
325 client_.set_fill_with_nonsolid_color(true);
326 scoped_refptr<FakePictureLayer> root_layer =
327 FakePictureLayer::Create(layer_settings(), &client_);
328 root_layer->SetBounds(gfx::Size(1024, 1024));
329 root_layer->SetIsDrawable(true);
331 layer_tree_host()->SetRootLayer(root_layer);
332 LayerTreeHostTest::SetupTree();
335 void AfterTest() override {
336 EXPECT_TRUE(did_notify_ready_to_draw_);
337 EXPECT_TRUE(all_tiles_required_for_draw_are_ready_to_draw_);
338 EXPECT_LE(size_t(1), required_for_draw_count_);
341 private:
342 FakeContentLayerClient client_;
345 // Note: With this test setup, we only get tiles flagged as REQUIRED_FOR_DRAW in
346 // single threaded mode.
347 SINGLE_THREAD_TEST_F(LayerTreeHostTestReadyToDrawNonEmpty);
349 // This tests if we get the READY_TO_DRAW signal and draw if we become invisible
350 // and then become visible again.
351 class LayerTreeHostTestReadyToDrawVisibility : public LayerTreeHostTest {
352 public:
353 LayerTreeHostTestReadyToDrawVisibility()
354 : LayerTreeHostTest(),
355 toggled_visibility_(false),
356 did_notify_ready_to_draw_(false),
357 did_draw_(false) {}
359 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
361 void SetupTree() override {
362 client_.set_fill_with_nonsolid_color(true);
363 scoped_refptr<FakePictureLayer> root_layer =
364 FakePictureLayer::Create(layer_settings(), &client_);
365 root_layer->SetBounds(gfx::Size(1024, 1024));
366 root_layer->SetIsDrawable(true);
368 layer_tree_host()->SetRootLayer(root_layer);
369 LayerTreeHostTest::SetupTree();
372 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
373 if (!toggled_visibility_) {
375 DebugScopedSetMainThread main(proxy());
376 layer_tree_host()->SetVisible(false);
378 toggled_visibility_ = true;
379 EXPECT_FALSE(host_impl->visible());
383 void NotifyReadyToDrawOnThread(LayerTreeHostImpl* host_impl) override {
384 // Sometimes the worker thread posts NotifyReadyToDraw in the extremely
385 // short duration of time between PrepareTiles and SetVisible(false) so we
386 // might get two NotifyReadyToDraw signals for this test.
387 did_notify_ready_to_draw_ = true;
390 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
391 EXPECT_FALSE(did_draw_);
392 did_draw_ = true;
393 EndTest();
396 void DidFinishImplFrameOnThread(LayerTreeHostImpl* host_impl) override {
397 if (!host_impl->visible()) {
399 DebugScopedSetMainThread main(proxy());
400 layer_tree_host()->SetVisible(true);
402 EXPECT_TRUE(host_impl->visible());
406 void AfterTest() override {
407 EXPECT_TRUE(did_notify_ready_to_draw_);
408 EXPECT_TRUE(did_draw_);
411 private:
412 FakeContentLayerClient client_;
413 bool toggled_visibility_;
414 bool did_notify_ready_to_draw_;
415 bool did_draw_;
418 // Note: With this test setup, we only get tiles flagged as REQUIRED_FOR_DRAW in
419 // single threaded mode.
420 SINGLE_THREAD_TEST_F(LayerTreeHostTestReadyToDrawVisibility);
422 class LayerTreeHostFreeWorkerContextResourcesTest : public LayerTreeHostTest {
423 public:
424 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
425 auto output_surface = make_scoped_ptr(new testing::StrictMock<
426 MockSetWorkerContextShouldAggressivelyFreeResourcesOutputSurface>(
427 delegating_renderer()));
429 // At init, we expect one call to set visibility to true.
430 testing::Expectation visibility_true =
431 EXPECT_CALL(*output_surface,
432 SetWorkerContextShouldAggressivelyFreeResources(false))
433 .Times(1);
435 // After running, we should get exactly one call to
436 // FreeWorkerContextGpuResources.
437 EXPECT_CALL(*output_surface,
438 SetWorkerContextShouldAggressivelyFreeResources(true))
439 .After(visibility_true)
440 .WillOnce(testing::Invoke([this](bool is_visible) { EndTest(); }));
441 return output_surface.Pass();
444 void InitializeSettings(LayerTreeSettings* settings) override {
445 settings->gpu_rasterization_enabled = true;
446 settings->gpu_rasterization_forced = true;
449 void BeginTest() override {
450 // Logic is handled in InitializedRendererOnThread to ensure that our
451 // LTHI is fully set up.
454 void AfterTest() override {
455 // Expectations handled via mock.
458 private:
459 class MockSetWorkerContextShouldAggressivelyFreeResourcesOutputSurface
460 : public FakeOutputSurface {
461 public:
462 ~MockSetWorkerContextShouldAggressivelyFreeResourcesOutputSurface() {}
463 explicit MockSetWorkerContextShouldAggressivelyFreeResourcesOutputSurface(
464 bool delegated_rendering)
465 : FakeOutputSurface(TestContextProvider::Create(),
466 TestContextProvider::Create(),
467 delegated_rendering) {}
468 MOCK_METHOD1(SetWorkerContextShouldAggressivelyFreeResources,
469 void(bool is_visible));
473 // Test if the LTH successfully frees resources on the worker context when
474 // visibility is set to false.
475 class LayerTreeHostFreeWorkerContextResourcesOnInvisible
476 : public LayerTreeHostFreeWorkerContextResourcesTest {
477 public:
478 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
479 bool success) override {
480 PostSetVisibleToMainThread(false);
484 SINGLE_AND_MULTI_THREAD_TEST_F(
485 LayerTreeHostFreeWorkerContextResourcesOnInvisible);
487 // Test if the LTH successfully frees resources on the worker context when
488 // hard memory limit is set to zero.
489 class LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit
490 : public LayerTreeHostFreeWorkerContextResourcesTest {
491 public:
492 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
493 bool success) override {
494 ManagedMemoryPolicy zero_policy(
495 0, gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING, 0);
496 host_impl->SetMemoryPolicy(zero_policy);
500 SINGLE_AND_MULTI_THREAD_TEST_F(
501 LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit);
503 // Test if the LTH successfully frees resources on the worker context when
504 // hard memory limit is set to zero while using a synchronous compositor (like
505 // Android WebView).
506 class LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimitSynchronous
507 : public LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit {
508 public:
509 void InitializeSettings(LayerTreeSettings* settings) override {
510 LayerTreeHostFreeWorkerContextResourcesTest::InitializeSettings(settings);
511 settings->use_external_begin_frame_source = true;
512 settings->using_synchronous_renderer_compositor = true;
516 SINGLE_AND_MULTI_THREAD_TEST_F(
517 LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimitSynchronous);
519 // Two setNeedsCommits in a row should lead to at least 1 commit and at least 1
520 // draw with frame 0.
521 class LayerTreeHostTestSetNeedsCommit1 : public LayerTreeHostTest {
522 public:
523 LayerTreeHostTestSetNeedsCommit1() : num_commits_(0), num_draws_(0) {}
525 void BeginTest() override {
526 PostSetNeedsCommitToMainThread();
527 PostSetNeedsCommitToMainThread();
530 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
531 num_draws_++;
532 if (!impl->active_tree()->source_frame_number())
533 EndTest();
536 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
537 num_commits_++;
540 void AfterTest() override {
541 EXPECT_LE(1, num_commits_);
542 EXPECT_LE(1, num_draws_);
545 private:
546 int num_commits_;
547 int num_draws_;
550 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsCommit1);
552 // A SetNeedsCommit should lead to 1 commit. Issuing a second commit after that
553 // first committed frame draws should lead to another commit.
554 class LayerTreeHostTestSetNeedsCommit2 : public LayerTreeHostTest {
555 public:
556 LayerTreeHostTestSetNeedsCommit2() : num_commits_(0), num_draws_(0) {}
558 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
560 void DrawLayersOnThread(LayerTreeHostImpl* impl) override { ++num_draws_; }
562 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
563 ++num_commits_;
564 switch (num_commits_) {
565 case 1:
566 PostSetNeedsCommitToMainThread();
567 break;
568 case 2:
569 EndTest();
570 break;
571 default:
572 NOTREACHED();
576 void AfterTest() override {
577 EXPECT_EQ(2, num_commits_);
578 EXPECT_LE(1, num_draws_);
581 private:
582 int num_commits_;
583 int num_draws_;
586 MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsCommit2);
588 // Verify that we pass property values in PushPropertiesTo.
589 class LayerTreeHostTestPushPropertiesTo : public LayerTreeHostTest {
590 protected:
591 void SetupTree() override {
592 scoped_refptr<Layer> root = Layer::Create(layer_settings());
593 root->SetBounds(gfx::Size(10, 10));
594 layer_tree_host()->SetRootLayer(root);
595 LayerTreeHostTest::SetupTree();
598 enum Properties {
599 STARTUP,
600 BOUNDS,
601 HIDE_LAYER_AND_SUBTREE,
602 DRAWS_CONTENT,
603 DONE,
606 void BeginTest() override {
607 index_ = STARTUP;
608 PostSetNeedsCommitToMainThread();
611 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
612 VerifyAfterValues(impl->active_tree()->root_layer());
615 void DidCommitAndDrawFrame() override {
616 SetBeforeValues(layer_tree_host()->root_layer());
617 VerifyBeforeValues(layer_tree_host()->root_layer());
619 ++index_;
620 if (index_ == DONE) {
621 EndTest();
622 return;
625 SetAfterValues(layer_tree_host()->root_layer());
628 void AfterTest() override {}
630 void VerifyBeforeValues(Layer* layer) {
631 EXPECT_EQ(gfx::Size(10, 10).ToString(), layer->bounds().ToString());
632 EXPECT_FALSE(layer->hide_layer_and_subtree());
633 EXPECT_FALSE(layer->DrawsContent());
636 void SetBeforeValues(Layer* layer) {
637 layer->SetBounds(gfx::Size(10, 10));
638 layer->SetHideLayerAndSubtree(false);
639 layer->SetIsDrawable(false);
642 void VerifyAfterValues(LayerImpl* layer) {
643 switch (static_cast<Properties>(index_)) {
644 case STARTUP:
645 case DONE:
646 break;
647 case BOUNDS:
648 EXPECT_EQ(gfx::Size(20, 20).ToString(), layer->bounds().ToString());
649 break;
650 case HIDE_LAYER_AND_SUBTREE:
651 EXPECT_TRUE(layer->hide_layer_and_subtree());
652 break;
653 case DRAWS_CONTENT:
654 EXPECT_TRUE(layer->DrawsContent());
655 break;
659 void SetAfterValues(Layer* layer) {
660 switch (static_cast<Properties>(index_)) {
661 case STARTUP:
662 case DONE:
663 break;
664 case BOUNDS:
665 layer->SetBounds(gfx::Size(20, 20));
666 break;
667 case HIDE_LAYER_AND_SUBTREE:
668 layer->SetHideLayerAndSubtree(true);
669 break;
670 case DRAWS_CONTENT:
671 layer->SetIsDrawable(true);
672 break;
676 int index_;
679 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestPushPropertiesTo);
681 // 1 setNeedsRedraw after the first commit has completed should lead to 1
682 // additional draw.
683 class LayerTreeHostTestSetNeedsRedraw : public LayerTreeHostTest {
684 public:
685 LayerTreeHostTestSetNeedsRedraw() : num_commits_(0), num_draws_(0) {}
687 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
689 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
690 EXPECT_EQ(0, impl->active_tree()->source_frame_number());
691 if (!num_draws_) {
692 // Redraw again to verify that the second redraw doesn't commit.
693 PostSetNeedsRedrawToMainThread();
694 } else {
695 EndTest();
697 num_draws_++;
700 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
701 EXPECT_EQ(0, num_draws_);
702 num_commits_++;
705 void AfterTest() override {
706 EXPECT_GE(2, num_draws_);
707 EXPECT_EQ(1, num_commits_);
710 private:
711 int num_commits_;
712 int num_draws_;
715 MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsRedraw);
717 // After setNeedsRedrawRect(invalid_rect) the final damage_rect
718 // must contain invalid_rect.
719 class LayerTreeHostTestSetNeedsRedrawRect : public LayerTreeHostTest {
720 public:
721 LayerTreeHostTestSetNeedsRedrawRect()
722 : num_draws_(0), bounds_(50, 50), invalid_rect_(10, 10, 20, 20) {}
724 void BeginTest() override {
725 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
726 root_layer_->SetIsDrawable(true);
727 root_layer_->SetBounds(bounds_);
728 layer_tree_host()->SetRootLayer(root_layer_);
729 layer_tree_host()->SetViewportSize(bounds_);
730 PostSetNeedsCommitToMainThread();
733 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
734 LayerTreeHostImpl::FrameData* frame_data,
735 DrawResult draw_result) override {
736 EXPECT_EQ(DRAW_SUCCESS, draw_result);
738 gfx::Rect root_damage_rect;
739 if (!frame_data->render_passes.empty())
740 root_damage_rect = frame_data->render_passes.back()->damage_rect;
742 if (!num_draws_) {
743 // If this is the first frame, expect full frame damage.
744 EXPECT_EQ(root_damage_rect, gfx::Rect(bounds_));
745 } else {
746 // Check that invalid_rect_ is indeed repainted.
747 EXPECT_TRUE(root_damage_rect.Contains(invalid_rect_));
750 return draw_result;
753 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
754 if (!num_draws_) {
755 PostSetNeedsRedrawRectToMainThread(invalid_rect_);
756 } else {
757 EndTest();
759 num_draws_++;
762 void AfterTest() override { EXPECT_EQ(2, num_draws_); }
764 private:
765 int num_draws_;
766 const gfx::Size bounds_;
767 const gfx::Rect invalid_rect_;
768 FakeContentLayerClient client_;
769 scoped_refptr<FakePictureLayer> root_layer_;
772 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsRedrawRect);
774 // Ensure the texture size of the pending and active trees are identical when a
775 // layer is not in the viewport and a resize happens on the viewport
776 class LayerTreeHostTestGpuRasterDeviceSizeChanged : public LayerTreeHostTest {
777 public:
778 LayerTreeHostTestGpuRasterDeviceSizeChanged()
779 : num_draws_(0), bounds_(500, 64), invalid_rect_(10, 10, 20, 20) {}
781 void BeginTest() override {
782 client_.set_fill_with_nonsolid_color(true);
783 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
784 root_layer_->SetIsDrawable(true);
785 gfx::Transform transform;
786 // Translate the layer out of the viewport to force it to not update its
787 // tile size via PushProperties.
788 transform.Translate(10000.0, 10000.0);
789 root_layer_->SetTransform(transform);
790 root_layer_->SetBounds(bounds_);
791 layer_tree_host()->SetRootLayer(root_layer_);
792 layer_tree_host()->SetViewportSize(bounds_);
794 PostSetNeedsCommitToMainThread();
797 void InitializeSettings(LayerTreeSettings* settings) override {
798 settings->gpu_rasterization_enabled = true;
799 settings->gpu_rasterization_forced = true;
802 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
803 // Perform 2 commits.
804 if (!num_draws_) {
805 PostSetNeedsRedrawRectToMainThread(invalid_rect_);
806 } else {
807 EndTest();
809 num_draws_++;
812 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
813 if (num_draws_ == 2) {
814 auto pending_tree = host_impl->pending_tree();
815 auto pending_layer_impl =
816 static_cast<FakePictureLayerImpl*>(pending_tree->root_layer());
817 EXPECT_NE(pending_layer_impl, nullptr);
819 auto active_tree = host_impl->pending_tree();
820 auto active_layer_impl =
821 static_cast<FakePictureLayerImpl*>(active_tree->root_layer());
822 EXPECT_NE(pending_layer_impl, nullptr);
824 auto active_tiling_set = active_layer_impl->picture_layer_tiling_set();
825 auto active_tiling = active_tiling_set->tiling_at(0);
826 auto pending_tiling_set = pending_layer_impl->picture_layer_tiling_set();
827 auto pending_tiling = pending_tiling_set->tiling_at(0);
828 EXPECT_EQ(
829 pending_tiling->TilingDataForTesting().max_texture_size().width(),
830 active_tiling->TilingDataForTesting().max_texture_size().width());
834 void DidCommitAndDrawFrame() override {
835 // On the second commit, resize the viewport.
836 if (num_draws_ == 1) {
837 layer_tree_host()->SetViewportSize(gfx::Size(400, 64));
841 void AfterTest() override {}
843 private:
844 int num_draws_;
845 const gfx::Size bounds_;
846 const gfx::Rect invalid_rect_;
847 FakeContentLayerClient client_;
848 scoped_refptr<FakePictureLayer> root_layer_;
851 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterDeviceSizeChanged);
853 class LayerTreeHostTestNoExtraCommitFromInvalidate : public LayerTreeHostTest {
854 public:
855 void InitializeSettings(LayerTreeSettings* settings) override {
856 settings->layer_transforms_should_scale_layer_contents = true;
859 void SetupTree() override {
860 root_layer_ = Layer::Create(layer_settings());
861 root_layer_->SetBounds(gfx::Size(10, 20));
863 scaled_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
864 scaled_layer_->SetBounds(gfx::Size(1, 1));
865 root_layer_->AddChild(scaled_layer_);
867 layer_tree_host()->SetRootLayer(root_layer_);
868 LayerTreeHostTest::SetupTree();
871 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
873 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
874 if (host_impl->active_tree()->source_frame_number() == 1)
875 EndTest();
878 void DidCommit() override {
879 switch (layer_tree_host()->source_frame_number()) {
880 case 1:
881 // SetBounds grows the layer and exposes new content.
882 scaled_layer_->SetBounds(gfx::Size(4, 4));
883 break;
884 default:
885 // No extra commits.
886 EXPECT_EQ(2, layer_tree_host()->source_frame_number());
890 void AfterTest() override {
891 EXPECT_EQ(gfx::Size(4, 4).ToString(), scaled_layer_->bounds().ToString());
894 private:
895 FakeContentLayerClient client_;
896 scoped_refptr<Layer> root_layer_;
897 scoped_refptr<Layer> scaled_layer_;
900 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestNoExtraCommitFromInvalidate);
902 class LayerTreeHostTestNoExtraCommitFromScrollbarInvalidate
903 : public LayerTreeHostTest {
904 public:
905 void InitializeSettings(LayerTreeSettings* settings) override {
906 settings->layer_transforms_should_scale_layer_contents = true;
909 void SetupTree() override {
910 root_layer_ = Layer::Create(layer_settings());
911 root_layer_->SetBounds(gfx::Size(10, 20));
913 bool paint_scrollbar = true;
914 bool has_thumb = false;
915 scrollbar_ = FakePaintedScrollbarLayer::Create(
916 layer_settings(), paint_scrollbar, has_thumb, root_layer_->id());
917 scrollbar_->SetPosition(gfx::Point(0, 10));
918 scrollbar_->SetBounds(gfx::Size(10, 10));
920 root_layer_->AddChild(scrollbar_);
922 layer_tree_host()->SetRootLayer(root_layer_);
923 LayerTreeHostTest::SetupTree();
926 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
928 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
929 if (host_impl->active_tree()->source_frame_number() == 1)
930 EndTest();
933 void DidCommit() override {
934 switch (layer_tree_host()->source_frame_number()) {
935 case 1:
936 // Changing the device scale factor causes a commit. It also changes
937 // the content bounds of |scrollbar_|, which should not generate
938 // a second commit as a result.
939 layer_tree_host()->SetDeviceScaleFactor(4.f);
940 break;
941 default:
942 // No extra commits.
943 EXPECT_EQ(2, layer_tree_host()->source_frame_number());
944 break;
948 void AfterTest() override {
951 private:
952 FakeContentLayerClient client_;
953 scoped_refptr<Layer> root_layer_;
954 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_;
957 SINGLE_AND_MULTI_THREAD_TEST_F(
958 LayerTreeHostTestNoExtraCommitFromScrollbarInvalidate);
960 class LayerTreeHostTestSetNextCommitForcesRedraw : public LayerTreeHostTest {
961 public:
962 LayerTreeHostTestSetNextCommitForcesRedraw()
963 : num_draws_(0), bounds_(50, 50), invalid_rect_(10, 10, 20, 20) {}
965 void BeginTest() override {
966 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
967 root_layer_->SetIsDrawable(true);
968 root_layer_->SetBounds(bounds_);
969 layer_tree_host()->SetRootLayer(root_layer_);
970 layer_tree_host()->SetViewportSize(bounds_);
971 PostSetNeedsCommitToMainThread();
974 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
975 if (num_draws_ == 3)
976 host_impl->SetNeedsRedrawRect(invalid_rect_);
979 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
980 LayerTreeHostImpl::FrameData* frame_data,
981 DrawResult draw_result) override {
982 EXPECT_EQ(DRAW_SUCCESS, draw_result);
984 gfx::Rect root_damage_rect;
985 if (!frame_data->render_passes.empty())
986 root_damage_rect = frame_data->render_passes.back()->damage_rect;
988 switch (num_draws_) {
989 case 0:
990 EXPECT_EQ(gfx::Rect(bounds_), root_damage_rect);
991 break;
992 case 1:
993 case 2:
994 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root_damage_rect);
995 break;
996 case 3:
997 EXPECT_EQ(invalid_rect_, root_damage_rect);
998 break;
999 case 4:
1000 EXPECT_EQ(gfx::Rect(bounds_), root_damage_rect);
1001 break;
1002 default:
1003 NOTREACHED();
1006 return draw_result;
1009 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
1010 switch (num_draws_) {
1011 case 0:
1012 case 1:
1013 // Cycle through a couple of empty commits to ensure we're observing the
1014 // right behavior
1015 PostSetNeedsCommitToMainThread();
1016 break;
1017 case 2:
1018 // Should force full frame damage on the next commit
1019 PostSetNextCommitForcesRedrawToMainThread();
1020 PostSetNeedsCommitToMainThread();
1021 host_impl->BlockNotifyReadyToActivateForTesting(true);
1022 break;
1023 case 3:
1024 host_impl->BlockNotifyReadyToActivateForTesting(false);
1025 break;
1026 default:
1027 EndTest();
1028 break;
1030 num_draws_++;
1033 void AfterTest() override { EXPECT_EQ(5, num_draws_); }
1035 private:
1036 int num_draws_;
1037 const gfx::Size bounds_;
1038 const gfx::Rect invalid_rect_;
1039 FakeContentLayerClient client_;
1040 scoped_refptr<FakePictureLayer> root_layer_;
1043 // This test blocks activation which is not supported for single thread mode.
1044 MULTI_THREAD_BLOCKNOTIFY_TEST_F(LayerTreeHostTestSetNextCommitForcesRedraw);
1046 // Tests that if a layer is not drawn because of some reason in the parent then
1047 // its damage is preserved until the next time it is drawn.
1048 class LayerTreeHostTestUndrawnLayersDamageLater : public LayerTreeHostTest {
1049 public:
1050 void InitializeSettings(LayerTreeSettings* settings) override {
1051 // If we don't set the minimum contents scale, it's harder to verify whether
1052 // the damage we get is correct. For other scale amounts, please see
1053 // LayerTreeHostTestDamageWithScale.
1054 settings->minimum_contents_scale = 1.f;
1057 void SetupTree() override {
1058 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
1059 root_layer_->SetIsDrawable(true);
1060 root_layer_->SetBounds(gfx::Size(50, 50));
1061 layer_tree_host()->SetRootLayer(root_layer_);
1063 // The initially transparent layer has a larger child layer, which is
1064 // not initially drawn because of the this (parent) layer.
1065 parent_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
1066 parent_layer_->SetBounds(gfx::Size(15, 15));
1067 parent_layer_->SetOpacity(0.0f);
1068 root_layer_->AddChild(parent_layer_);
1070 child_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
1071 child_layer_->SetBounds(gfx::Size(25, 25));
1072 parent_layer_->AddChild(child_layer_);
1074 LayerTreeHostTest::SetupTree();
1077 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1079 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
1080 LayerTreeHostImpl::FrameData* frame_data,
1081 DrawResult draw_result) override {
1082 EXPECT_EQ(DRAW_SUCCESS, draw_result);
1084 gfx::Rect root_damage_rect;
1085 if (!frame_data->render_passes.empty())
1086 root_damage_rect = frame_data->render_passes.back()->damage_rect;
1088 // The first time, the whole view needs be drawn.
1089 // Afterwards, just the opacity of surface_layer1 is changed a few times,
1090 // and each damage should be the bounding box of it and its child. If this
1091 // was working improperly, the damage might not include its childs bounding
1092 // box.
1093 switch (host_impl->active_tree()->source_frame_number()) {
1094 case 0:
1095 EXPECT_EQ(gfx::Rect(root_layer_->bounds()), root_damage_rect);
1096 break;
1097 case 1:
1098 case 2:
1099 case 3:
1100 EXPECT_EQ(gfx::Rect(child_layer_->bounds()), root_damage_rect);
1101 break;
1102 default:
1103 NOTREACHED();
1106 return draw_result;
1109 void DidCommitAndDrawFrame() override {
1110 switch (layer_tree_host()->source_frame_number()) {
1111 case 1:
1112 // Test not owning the surface.
1113 parent_layer_->SetOpacity(1.0f);
1114 break;
1115 case 2:
1116 parent_layer_->SetOpacity(0.0f);
1117 break;
1118 case 3:
1119 // Test owning the surface.
1120 parent_layer_->SetOpacity(0.5f);
1121 parent_layer_->SetForceRenderSurface(true);
1122 break;
1123 case 4:
1124 EndTest();
1125 break;
1126 default:
1127 NOTREACHED();
1131 void AfterTest() override {}
1133 private:
1134 FakeContentLayerClient client_;
1135 scoped_refptr<FakePictureLayer> root_layer_;
1136 scoped_refptr<FakePictureLayer> parent_layer_;
1137 scoped_refptr<FakePictureLayer> child_layer_;
1140 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestUndrawnLayersDamageLater);
1142 // Tests that if a layer is not drawn because of some reason in the parent then
1143 // its damage is preserved until the next time it is drawn.
1144 class LayerTreeHostTestDamageWithScale : public LayerTreeHostTest {
1145 public:
1146 LayerTreeHostTestDamageWithScale() {}
1148 void SetupTree() override {
1149 client_.set_fill_with_nonsolid_color(true);
1151 scoped_ptr<FakePicturePile> pile(
1152 new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
1153 LayerTreeSettings().default_tile_grid_size));
1154 root_layer_ = FakePictureLayer::CreateWithRecordingSource(
1155 layer_settings(), &client_, pile.Pass());
1156 root_layer_->SetBounds(gfx::Size(50, 50));
1158 pile.reset(new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
1159 LayerTreeSettings().default_tile_grid_size));
1160 child_layer_ = FakePictureLayer::CreateWithRecordingSource(
1161 layer_settings(), &client_, pile.Pass());
1162 child_layer_->SetBounds(gfx::Size(25, 25));
1163 child_layer_->SetIsDrawable(true);
1164 child_layer_->SetContentsOpaque(true);
1165 root_layer_->AddChild(child_layer_);
1167 layer_tree_host()->SetRootLayer(root_layer_);
1168 LayerTreeHostTest::SetupTree();
1171 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1172 // Force the layer to have a tiling at 1.3f scale. Note that if we simply
1173 // add tiling, it will be gone by the time we draw because of aggressive
1174 // cleanup. AddTilingUntilNextDraw ensures that it remains there during
1175 // damage calculation.
1176 FakePictureLayerImpl* child_layer_impl = static_cast<FakePictureLayerImpl*>(
1177 host_impl->active_tree()->LayerById(child_layer_->id()));
1178 child_layer_impl->AddTilingUntilNextDraw(1.3f);
1181 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1183 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
1184 LayerTreeHostImpl::FrameData* frame_data,
1185 DrawResult draw_result) override {
1186 EXPECT_EQ(DRAW_SUCCESS, draw_result);
1188 gfx::Rect root_damage_rect;
1189 if (!frame_data->render_passes.empty())
1190 root_damage_rect = frame_data->render_passes.back()->damage_rect;
1192 // The first time, the whole view needs be drawn.
1193 // Afterwards, just the opacity of surface_layer1 is changed a few times,
1194 // and each damage should be the bounding box of it and its child. If this
1195 // was working improperly, the damage might not include its childs bounding
1196 // box.
1197 switch (host_impl->active_tree()->source_frame_number()) {
1198 case 0:
1199 EXPECT_EQ(gfx::Rect(root_layer_->bounds()), root_damage_rect);
1200 break;
1201 case 1: {
1202 FakePictureLayerImpl* child_layer_impl =
1203 static_cast<FakePictureLayerImpl*>(
1204 host_impl->active_tree()->LayerById(child_layer_->id()));
1205 // We remove tilings pretty aggressively if they are not ideal. Add this
1206 // back in so that we can compare
1207 // child_layer_impl->GetEnclosingRectInTargetSpace to the damage.
1208 child_layer_impl->AddTilingUntilNextDraw(1.3f);
1210 EXPECT_EQ(gfx::Rect(26, 26), root_damage_rect);
1211 EXPECT_EQ(child_layer_impl->GetEnclosingRectInTargetSpace(),
1212 root_damage_rect);
1213 EXPECT_TRUE(child_layer_impl->GetEnclosingRectInTargetSpace().Contains(
1214 gfx::Rect(child_layer_->bounds())));
1215 break;
1217 default:
1218 NOTREACHED();
1221 return draw_result;
1224 void DidCommitAndDrawFrame() override {
1225 switch (layer_tree_host()->source_frame_number()) {
1226 case 1: {
1227 // Test not owning the surface.
1228 child_layer_->SetOpacity(0.5f);
1229 break;
1231 case 2:
1232 EndTest();
1233 break;
1234 default:
1235 NOTREACHED();
1239 void AfterTest() override {}
1241 private:
1242 FakeContentLayerClient client_;
1243 scoped_refptr<Layer> root_layer_;
1244 scoped_refptr<Layer> child_layer_;
1247 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestDamageWithScale);
1249 // This test verifies that properties on the layer tree host are commited
1250 // to the impl side.
1251 class LayerTreeHostTestCommit : public LayerTreeHostTest {
1252 public:
1253 LayerTreeHostTestCommit() {}
1255 void BeginTest() override {
1256 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
1257 layer_tree_host()->set_background_color(SK_ColorGRAY);
1259 PostSetNeedsCommitToMainThread();
1262 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1263 EXPECT_EQ(gfx::Size(20, 20), impl->DrawViewportSize());
1264 EXPECT_EQ(SK_ColorGRAY, impl->active_tree()->background_color());
1266 EndTest();
1269 void AfterTest() override {}
1272 MULTI_THREAD_TEST_F(LayerTreeHostTestCommit);
1274 // This test verifies that LayerTreeHostImpl's current frame time gets
1275 // updated in consecutive frames when it doesn't draw due to tree
1276 // activation failure.
1277 class LayerTreeHostTestFrameTimeUpdatesAfterActivationFails
1278 : public LayerTreeHostTest {
1279 public:
1280 LayerTreeHostTestFrameTimeUpdatesAfterActivationFails()
1281 : frame_count_with_pending_tree_(0) {}
1283 void BeginTest() override {
1284 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
1285 layer_tree_host()->set_background_color(SK_ColorGRAY);
1287 PostSetNeedsCommitToMainThread();
1290 void BeginCommitOnThread(LayerTreeHostImpl* impl) override {
1291 EXPECT_EQ(frame_count_with_pending_tree_, 0);
1292 impl->BlockNotifyReadyToActivateForTesting(true);
1295 void WillBeginImplFrameOnThread(LayerTreeHostImpl* impl,
1296 const BeginFrameArgs& args) override {
1297 if (impl->pending_tree())
1298 frame_count_with_pending_tree_++;
1300 if (frame_count_with_pending_tree_ == 1) {
1301 EXPECT_EQ(first_frame_time_.ToInternalValue(), 0);
1302 first_frame_time_ = impl->CurrentBeginFrameArgs().frame_time;
1303 } else if (frame_count_with_pending_tree_ == 2) {
1304 impl->BlockNotifyReadyToActivateForTesting(false);
1308 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
1309 EXPECT_GT(frame_count_with_pending_tree_, 1);
1310 EXPECT_NE(first_frame_time_.ToInternalValue(), 0);
1311 EXPECT_NE(first_frame_time_.ToInternalValue(),
1312 impl->CurrentBeginFrameArgs().frame_time.ToInternalValue());
1313 EndTest();
1316 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1317 EXPECT_GT(frame_count_with_pending_tree_, 1);
1320 void AfterTest() override {}
1322 private:
1323 int frame_count_with_pending_tree_;
1324 base::TimeTicks first_frame_time_;
1327 // This test blocks activation which is not supported for single thread mode.
1328 MULTI_THREAD_BLOCKNOTIFY_TEST_F(
1329 LayerTreeHostTestFrameTimeUpdatesAfterActivationFails);
1331 // This test verifies that LayerTreeHostImpl's current frame time gets
1332 // updated in consecutive frames when it draws in each frame.
1333 class LayerTreeHostTestFrameTimeUpdatesAfterDraw : public LayerTreeHostTest {
1334 public:
1335 LayerTreeHostTestFrameTimeUpdatesAfterDraw() : frame_(0) {}
1337 void BeginTest() override {
1338 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
1339 layer_tree_host()->set_background_color(SK_ColorGRAY);
1341 PostSetNeedsCommitToMainThread();
1344 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
1345 frame_++;
1346 if (frame_ == 1) {
1347 first_frame_time_ = impl->CurrentBeginFrameArgs().frame_time;
1348 impl->SetNeedsRedraw();
1350 // Since we might use a low-resolution clock on Windows, we need to
1351 // make sure that the clock has incremented past first_frame_time_.
1352 while (first_frame_time_ == base::TimeTicks::Now()) {
1355 return;
1358 EXPECT_NE(first_frame_time_, impl->CurrentBeginFrameArgs().frame_time);
1359 EndTest();
1362 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
1363 // Ensure there isn't a commit between the two draws, to ensure that a
1364 // commit isn't required for updating the current frame time. We can
1365 // only check for this in the multi-threaded case, since in the single-
1366 // threaded case there will always be a commit between consecutive draws.
1367 if (HasImplThread())
1368 EXPECT_EQ(0, frame_);
1371 void AfterTest() override {}
1373 private:
1374 int frame_;
1375 base::TimeTicks first_frame_time_;
1378 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestFrameTimeUpdatesAfterDraw);
1380 // Verifies that StartPageScaleAnimation events propagate correctly
1381 // from LayerTreeHost to LayerTreeHostImpl in the MT compositor.
1382 class LayerTreeHostTestStartPageScaleAnimation : public LayerTreeHostTest {
1383 public:
1384 LayerTreeHostTestStartPageScaleAnimation() {}
1386 void SetupTree() override {
1387 LayerTreeHostTest::SetupTree();
1389 Layer* root_layer = layer_tree_host()->root_layer();
1391 scoped_refptr<FakePictureLayer> layer =
1392 FakePictureLayer::Create(layer_settings(), &client_);
1393 layer->set_always_update_resources(true);
1394 scroll_layer_ = layer;
1396 scroll_layer_->SetBounds(gfx::Size(2 * root_layer->bounds().width(),
1397 2 * root_layer->bounds().height()));
1398 scroll_layer_->SetScrollOffset(gfx::ScrollOffset());
1400 CreateVirtualViewportLayers(root_layer,
1401 scroll_layer_,
1402 root_layer->bounds(),
1403 root_layer->bounds(),
1404 layer_tree_host(),
1405 layer_settings());
1407 layer_tree_host()->SetPageScaleFactorAndLimits(1.f, 0.5f, 2.f);
1410 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1412 void ApplyViewportDeltas(const gfx::Vector2dF& scroll_delta,
1413 const gfx::Vector2dF&,
1414 const gfx::Vector2dF& elastic_overscroll_delta,
1415 float scale,
1416 float) override {
1417 gfx::ScrollOffset offset = scroll_layer_->scroll_offset();
1418 scroll_layer_->SetScrollOffset(ScrollOffsetWithDelta(offset,
1419 scroll_delta));
1420 layer_tree_host()->SetPageScaleFactorAndLimits(scale, 0.5f, 2.f);
1423 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1424 // We get one commit before the first draw, and the animation doesn't happen
1425 // until the second draw.
1426 switch (impl->active_tree()->source_frame_number()) {
1427 case 0:
1428 EXPECT_EQ(1.f, impl->active_tree()->current_page_scale_factor());
1429 // We'll start an animation when we get back to the main thread.
1430 break;
1431 case 1:
1432 EXPECT_EQ(1.f, impl->active_tree()->current_page_scale_factor());
1433 break;
1434 case 2:
1435 EXPECT_EQ(1.25f, impl->active_tree()->current_page_scale_factor());
1436 EndTest();
1437 break;
1438 default:
1439 NOTREACHED();
1443 void DidCommitAndDrawFrame() override {
1444 switch (layer_tree_host()->source_frame_number()) {
1445 case 1:
1446 layer_tree_host()->StartPageScaleAnimation(
1447 gfx::Vector2d(), false, 1.25f, base::TimeDelta());
1448 break;
1452 void AfterTest() override {}
1454 FakeContentLayerClient client_;
1455 scoped_refptr<Layer> scroll_layer_;
1458 // Single thread proxy does not support impl-side page scale changes.
1459 MULTI_THREAD_TEST_F(LayerTreeHostTestStartPageScaleAnimation);
1461 class LayerTreeHostTestSetVisible : public LayerTreeHostTest {
1462 public:
1463 LayerTreeHostTestSetVisible() : num_draws_(0) {}
1465 void BeginTest() override {
1466 PostSetNeedsCommitToMainThread();
1467 PostSetVisibleToMainThread(false);
1468 // This is suppressed while we're invisible.
1469 PostSetNeedsRedrawToMainThread();
1470 // Triggers the redraw.
1471 PostSetVisibleToMainThread(true);
1474 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
1475 EXPECT_TRUE(impl->visible());
1476 ++num_draws_;
1477 EndTest();
1480 void AfterTest() override { EXPECT_EQ(1, num_draws_); }
1482 private:
1483 int num_draws_;
1486 MULTI_THREAD_TEST_F(LayerTreeHostTestSetVisible);
1488 class TestOpacityChangeLayerDelegate : public ContentLayerClient {
1489 public:
1490 TestOpacityChangeLayerDelegate() : test_layer_(0) {}
1492 void SetTestLayer(Layer* test_layer) { test_layer_ = test_layer; }
1494 void PaintContents(SkCanvas* canvas,
1495 const gfx::Rect& clip,
1496 PaintingControlSetting picture_control) override {
1497 // Set layer opacity to 0.
1498 if (test_layer_)
1499 test_layer_->SetOpacity(0.f);
1501 scoped_refptr<DisplayItemList> PaintContentsToDisplayList(
1502 const gfx::Rect& clip,
1503 PaintingControlSetting picture_control) override {
1504 NOTIMPLEMENTED();
1505 return nullptr;
1507 bool FillsBoundsCompletely() const override { return false; }
1508 size_t GetApproximateUnsharedMemoryUsage() const override { return 0; }
1510 private:
1511 Layer* test_layer_;
1514 // Layer opacity change during paint should not prevent compositor resources
1515 // from being updated during commit.
1516 class LayerTreeHostTestOpacityChange : public LayerTreeHostTest {
1517 public:
1518 LayerTreeHostTestOpacityChange() : test_opacity_change_delegate_() {}
1520 void SetupTree() override {
1521 LayerTreeHostTest::SetupTree();
1523 update_check_picture_layer_ = FakePictureLayer::Create(
1524 layer_settings(), &test_opacity_change_delegate_);
1525 test_opacity_change_delegate_.SetTestLayer(
1526 update_check_picture_layer_.get());
1527 layer_tree_host()->root_layer()->AddChild(update_check_picture_layer_);
1530 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1532 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override { EndTest(); }
1534 void AfterTest() override {
1535 // Update() should have been called once.
1536 EXPECT_EQ(1, update_check_picture_layer_->update_count());
1539 private:
1540 TestOpacityChangeLayerDelegate test_opacity_change_delegate_;
1541 scoped_refptr<FakePictureLayer> update_check_picture_layer_;
1544 MULTI_THREAD_TEST_F(LayerTreeHostTestOpacityChange);
1546 class LayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers
1547 : public LayerTreeHostTest {
1548 public:
1549 LayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers() {}
1551 void BeginTest() override {
1552 client_.set_fill_with_nonsolid_color(true);
1553 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
1554 child_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
1556 layer_tree_host()->SetViewportSize(gfx::Size(60, 60));
1557 layer_tree_host()->SetDeviceScaleFactor(1.5);
1558 EXPECT_EQ(gfx::Size(60, 60), layer_tree_host()->device_viewport_size());
1560 root_layer_->AddChild(child_layer_);
1562 root_layer_->SetIsDrawable(true);
1563 root_layer_->SetBounds(gfx::Size(30, 30));
1565 child_layer_->SetIsDrawable(true);
1566 child_layer_->SetPosition(gfx::Point(2, 2));
1567 child_layer_->SetBounds(gfx::Size(10, 10));
1569 layer_tree_host()->SetRootLayer(root_layer_);
1571 PostSetNeedsCommitToMainThread();
1574 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1575 // Should only do one commit.
1576 EXPECT_EQ(0, impl->active_tree()->source_frame_number());
1577 // Device scale factor should come over to impl.
1578 EXPECT_NEAR(impl->device_scale_factor(), 1.5f, 0.00001f);
1580 // Both layers are on impl.
1581 ASSERT_EQ(1u, impl->active_tree()->root_layer()->children().size());
1583 // Device viewport is scaled.
1584 EXPECT_EQ(gfx::Size(60, 60), impl->DrawViewportSize());
1586 FakePictureLayerImpl* root =
1587 static_cast<FakePictureLayerImpl*>(impl->active_tree()->root_layer());
1588 FakePictureLayerImpl* child = static_cast<FakePictureLayerImpl*>(
1589 impl->active_tree()->root_layer()->children()[0]);
1591 // Positions remain in layout pixels.
1592 EXPECT_EQ(gfx::Point(0, 0), root->position());
1593 EXPECT_EQ(gfx::Point(2, 2), child->position());
1595 // Compute all the layer transforms for the frame.
1596 LayerTreeHostImpl::FrameData frame_data;
1597 impl->PrepareToDraw(&frame_data);
1598 impl->DidDrawAllLayers(frame_data);
1600 const LayerImplList& render_surface_layer_list =
1601 *frame_data.render_surface_layer_list;
1603 // Both layers should be drawing into the root render surface.
1604 ASSERT_EQ(1u, render_surface_layer_list.size());
1605 ASSERT_EQ(root->render_surface(),
1606 render_surface_layer_list[0]->render_surface());
1607 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
1609 // The root render surface is the size of the viewport.
1610 EXPECT_EQ(gfx::Rect(0, 0, 60, 60), root->render_surface()->content_rect());
1612 // The max tiling scale of the child should be scaled.
1613 EXPECT_FLOAT_EQ(1.5f, child->MaximumTilingContentsScale());
1615 gfx::Transform scale_transform;
1616 scale_transform.Scale(impl->device_scale_factor(),
1617 impl->device_scale_factor());
1619 // The root layer is scaled by 2x.
1620 gfx::Transform root_screen_space_transform = scale_transform;
1621 gfx::Transform root_draw_transform = scale_transform;
1623 EXPECT_EQ(root_draw_transform, root->draw_transform());
1624 EXPECT_EQ(root_screen_space_transform, root->screen_space_transform());
1626 // The child is at position 2,2, which is transformed to 3,3 after the scale
1627 gfx::Transform child_transform;
1628 child_transform.Translate(3.f, 3.f);
1629 child_transform.Scale(child->MaximumTilingContentsScale(),
1630 child->MaximumTilingContentsScale());
1632 EXPECT_TRANSFORMATION_MATRIX_EQ(child_transform, child->draw_transform());
1633 EXPECT_TRANSFORMATION_MATRIX_EQ(child_transform,
1634 child->screen_space_transform());
1636 EndTest();
1639 void AfterTest() override {}
1641 private:
1642 FakeContentLayerClient client_;
1643 scoped_refptr<FakePictureLayer> root_layer_;
1644 scoped_refptr<FakePictureLayer> child_layer_;
1647 MULTI_THREAD_TEST_F(LayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers);
1649 class LayerTreeHostTestContinuousInvalidate : public LayerTreeHostTest {
1650 public:
1651 LayerTreeHostTestContinuousInvalidate()
1652 : num_commit_complete_(0), num_draw_layers_(0) {}
1654 void BeginTest() override {
1655 layer_tree_host()->SetViewportSize(gfx::Size(10, 10));
1656 layer_tree_host()->root_layer()->SetBounds(gfx::Size(10, 10));
1658 layer_ = FakePictureLayer::Create(layer_settings(), &client_);
1659 layer_->SetBounds(gfx::Size(10, 10));
1660 layer_->SetPosition(gfx::PointF(0.f, 0.f));
1661 layer_->SetIsDrawable(true);
1662 layer_tree_host()->root_layer()->AddChild(layer_);
1664 PostSetNeedsCommitToMainThread();
1667 void DidCommitAndDrawFrame() override {
1668 if (num_draw_layers_ == 2)
1669 return;
1670 layer_->SetNeedsDisplay();
1673 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
1674 if (num_draw_layers_ == 1)
1675 num_commit_complete_++;
1678 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
1679 num_draw_layers_++;
1680 if (num_draw_layers_ == 2)
1681 EndTest();
1684 void AfterTest() override {
1685 // Check that we didn't commit twice between first and second draw.
1686 EXPECT_EQ(1, num_commit_complete_);
1689 private:
1690 FakeContentLayerClient client_;
1691 scoped_refptr<Layer> layer_;
1692 int num_commit_complete_;
1693 int num_draw_layers_;
1696 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestContinuousInvalidate);
1698 class LayerTreeHostTestDeferCommits : public LayerTreeHostTest {
1699 public:
1700 LayerTreeHostTestDeferCommits()
1701 : num_will_begin_impl_frame_(0),
1702 num_send_begin_main_frame_(0) {}
1704 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1706 void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl,
1707 const BeginFrameArgs& args) override {
1708 num_will_begin_impl_frame_++;
1709 switch (num_will_begin_impl_frame_) {
1710 case 1:
1711 break;
1712 case 2:
1713 case 3:
1714 case 4:
1715 // Post a number of frames to increase the chance that, if there exist
1716 // bugs, an unexpected BeginMainFrame will be issued.
1717 PostSetNeedsCommitToMainThread();
1718 PostSetNeedsRedrawToMainThread();
1719 break;
1720 case 5:
1721 PostSetDeferCommitsToMainThread(false);
1722 break;
1723 default:
1724 // Sometimes |num_will_begin_impl_frame_| will be greater than 5 if the
1725 // main thread is slow to respond.
1726 break;
1730 void ScheduledActionSendBeginMainFrame() override {
1731 num_send_begin_main_frame_++;
1732 switch (num_send_begin_main_frame_) {
1733 case 1:
1734 PostSetDeferCommitsToMainThread(true);
1735 break;
1736 case 2:
1737 EndTest();
1738 break;
1739 default:
1740 NOTREACHED();
1741 break;
1745 void AfterTest() override {
1746 EXPECT_GE(num_will_begin_impl_frame_, 5);
1747 EXPECT_EQ(2, num_send_begin_main_frame_);
1750 private:
1751 int num_will_begin_impl_frame_;
1752 int num_send_begin_main_frame_;
1755 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestDeferCommits);
1757 class LayerTreeHostTestCompositeImmediatelyStateTransitions
1758 : public LayerTreeHostTest {
1759 public:
1760 enum {
1761 kInvalid,
1762 kStartedTest,
1763 kStartedImplFrame,
1764 kStartedMainFrame,
1765 kStartedCommit,
1766 kCompletedCommit,
1767 kCompletedMainFrame,
1768 kCompletedImplFrame,
1771 LayerTreeHostTestCompositeImmediatelyStateTransitions()
1772 : current_state_(kInvalid), current_begin_frame_args_() {}
1774 void InitializeSettings(LayerTreeSettings* settings) override {
1775 settings->single_thread_proxy_scheduler = false;
1776 settings->use_zero_copy = true;
1779 void BeginTest() override {
1780 current_state_ = kStartedTest;
1781 PostCompositeImmediatelyToMainThread();
1784 void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl,
1785 const BeginFrameArgs& args) override {
1786 EXPECT_EQ(current_state_, kStartedTest);
1787 current_state_ = kStartedImplFrame;
1789 EXPECT_FALSE(current_begin_frame_args_.IsValid());
1790 EXPECT_TRUE(args.IsValid());
1791 current_begin_frame_args_ = args;
1793 void WillBeginMainFrame() override {
1794 EXPECT_EQ(current_state_, kStartedImplFrame);
1795 current_state_ = kStartedMainFrame;
1797 void BeginMainFrame(const BeginFrameArgs& args) override {
1798 EXPECT_EQ(current_state_, kStartedMainFrame);
1799 EXPECT_EQ(args.frame_time, current_begin_frame_args_.frame_time);
1801 void BeginCommitOnThread(LayerTreeHostImpl* host_impl) override {
1802 EXPECT_EQ(current_state_, kStartedMainFrame);
1803 current_state_ = kStartedCommit;
1805 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
1806 EXPECT_EQ(current_state_, kStartedCommit);
1807 current_state_ = kCompletedCommit;
1809 void DidBeginMainFrame() override {
1810 EXPECT_EQ(current_state_, kCompletedCommit);
1811 current_state_ = kCompletedMainFrame;
1813 void DidFinishImplFrameOnThread(LayerTreeHostImpl* host_impl) override {
1814 EXPECT_EQ(current_state_, kCompletedMainFrame);
1815 current_state_ = kCompletedImplFrame;
1816 EndTest();
1818 void AfterTest() override { EXPECT_EQ(current_state_, kCompletedImplFrame); }
1820 private:
1821 int current_state_;
1822 BeginFrameArgs current_begin_frame_args_;
1825 SINGLE_THREAD_TEST_F(LayerTreeHostTestCompositeImmediatelyStateTransitions);
1827 class LayerTreeHostTestLCDChange : public LayerTreeHostTest {
1828 public:
1829 void SetupTree() override {
1830 num_tiles_rastered_ = 0;
1832 scoped_refptr<Layer> root_layer =
1833 PictureLayer::Create(layer_settings(), &client_);
1834 client_.set_fill_with_nonsolid_color(true);
1835 root_layer->SetIsDrawable(true);
1836 root_layer->SetBounds(gfx::Size(10, 10));
1837 root_layer->SetContentsOpaque(true);
1839 layer_tree_host()->SetRootLayer(root_layer);
1841 // The expectations are based on the assumption that the default
1842 // LCD settings are:
1843 EXPECT_TRUE(layer_tree_host()->settings().can_use_lcd_text);
1845 LayerTreeHostTest::SetupTree();
1848 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1850 void DidCommitAndDrawFrame() override {
1851 switch (layer_tree_host()->source_frame_number()) {
1852 case 1:
1853 PostSetNeedsCommitToMainThread();
1854 break;
1855 case 2:
1856 // Change layer opacity that should trigger lcd change.
1857 layer_tree_host()->root_layer()->SetOpacity(.5f);
1858 break;
1859 case 3:
1860 // Change layer opacity that should not trigger lcd change.
1861 layer_tree_host()->root_layer()->SetOpacity(1.f);
1862 break;
1863 case 4:
1864 EndTest();
1865 break;
1869 void NotifyTileStateChangedOnThread(LayerTreeHostImpl* host_impl,
1870 const Tile* tile) override {
1871 ++num_tiles_rastered_;
1874 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
1875 PictureLayerImpl* root_layer =
1876 static_cast<PictureLayerImpl*>(host_impl->active_tree()->root_layer());
1877 bool can_use_lcd_text =
1878 host_impl->active_tree()->root_layer()->can_use_lcd_text();
1879 switch (host_impl->active_tree()->source_frame_number()) {
1880 case 0:
1881 // The first draw.
1882 EXPECT_EQ(1, num_tiles_rastered_);
1883 EXPECT_TRUE(can_use_lcd_text);
1884 EXPECT_TRUE(root_layer->RasterSourceUsesLCDText());
1885 break;
1886 case 1:
1887 // Nothing changed on the layer.
1888 EXPECT_EQ(1, num_tiles_rastered_);
1889 EXPECT_TRUE(can_use_lcd_text);
1890 EXPECT_TRUE(root_layer->RasterSourceUsesLCDText());
1891 break;
1892 case 2:
1893 // LCD text was disabled; it should be re-rastered with LCD text off.
1894 EXPECT_EQ(2, num_tiles_rastered_);
1895 EXPECT_FALSE(can_use_lcd_text);
1896 EXPECT_FALSE(root_layer->RasterSourceUsesLCDText());
1897 break;
1898 case 3:
1899 // LCD text was enabled, but it's sticky and stays off.
1900 EXPECT_EQ(2, num_tiles_rastered_);
1901 EXPECT_TRUE(can_use_lcd_text);
1902 EXPECT_FALSE(root_layer->RasterSourceUsesLCDText());
1903 break;
1907 void AfterTest() override {}
1909 private:
1910 FakeContentLayerClient client_;
1911 int num_tiles_rastered_;
1914 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestLCDChange);
1916 // Verify that the BeginFrame notification is used to initiate rendering.
1917 class LayerTreeHostTestBeginFrameNotification : public LayerTreeHostTest {
1918 public:
1919 void InitializeSettings(LayerTreeSettings* settings) override {
1920 settings->use_external_begin_frame_source = true;
1923 void BeginTest() override {
1924 // This will trigger a SetNeedsBeginFrame which will trigger a
1925 // BeginFrame.
1926 PostSetNeedsCommitToMainThread();
1929 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
1930 LayerTreeHostImpl::FrameData* frame,
1931 DrawResult draw_result) override {
1932 EndTest();
1933 return DRAW_SUCCESS;
1936 void AfterTest() override {}
1938 private:
1939 base::TimeTicks frame_time_;
1942 MULTI_THREAD_TEST_F(LayerTreeHostTestBeginFrameNotification);
1944 class LayerTreeHostTestBeginFrameNotificationShutdownWhileEnabled
1945 : public LayerTreeHostTest {
1946 public:
1947 void InitializeSettings(LayerTreeSettings* settings) override {
1948 settings->use_external_begin_frame_source = true;
1949 settings->using_synchronous_renderer_compositor = true;
1952 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1954 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
1955 // The BeginFrame notification is turned off now but will get enabled
1956 // once we return. End test while it's enabled.
1957 ImplThreadTaskRunner()->PostTask(
1958 FROM_HERE,
1959 base::Bind(&LayerTreeHostTestBeginFrameNotification::EndTest,
1960 base::Unretained(this)));
1963 void AfterTest() override {}
1966 MULTI_THREAD_TEST_F(
1967 LayerTreeHostTestBeginFrameNotificationShutdownWhileEnabled);
1969 class LayerTreeHostTestAbortedCommitDoesntStall : public LayerTreeHostTest {
1970 protected:
1971 LayerTreeHostTestAbortedCommitDoesntStall()
1972 : commit_count_(0), commit_abort_count_(0), commit_complete_count_(0) {}
1974 void InitializeSettings(LayerTreeSettings* settings) override {
1975 settings->use_external_begin_frame_source = true;
1978 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1980 void DidCommit() override {
1981 commit_count_++;
1982 if (commit_count_ == 4) {
1983 // After two aborted commits, request a real commit now to make sure a
1984 // real commit following an aborted commit will still complete and
1985 // end the test even when the Impl thread is idle.
1986 layer_tree_host()->SetNeedsCommit();
1990 void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl,
1991 CommitEarlyOutReason reason) override {
1992 commit_abort_count_++;
1993 // Initiate another abortable commit.
1994 host_impl->SetNeedsCommit();
1997 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
1998 commit_complete_count_++;
1999 if (commit_complete_count_ == 1) {
2000 // Initiate an abortable commit after the first commit.
2001 host_impl->SetNeedsCommit();
2002 } else {
2003 EndTest();
2007 void AfterTest() override {
2008 EXPECT_EQ(commit_count_, 5);
2009 EXPECT_EQ(commit_abort_count_, 3);
2010 EXPECT_EQ(commit_complete_count_, 2);
2013 int commit_count_;
2014 int commit_abort_count_;
2015 int commit_complete_count_;
2018 class LayerTreeHostTestAbortedCommitDoesntStallSynchronousCompositor
2019 : public LayerTreeHostTestAbortedCommitDoesntStall {
2020 protected:
2021 void InitializeSettings(LayerTreeSettings* settings) override {
2022 LayerTreeHostTestAbortedCommitDoesntStall::InitializeSettings(settings);
2023 settings->using_synchronous_renderer_compositor = true;
2026 void ScheduledActionInvalidateOutputSurface() override {
2027 // Do not call ImplThreadTaskRunner after the test ended because of the
2028 // possibility of use-after-free due to a race.
2029 if (TestEnded())
2030 return;
2031 ImplThreadTaskRunner()->PostTask(
2032 FROM_HERE,
2033 base::Bind(
2034 &LayerTreeHostTestAbortedCommitDoesntStallSynchronousCompositor::
2035 CallOnDraw,
2036 base::Unretained(this)));
2039 void CallOnDraw() {
2040 // Synchronous compositor does not draw unless told to do so by the output
2041 // surface.
2042 output_surface()->client()->OnDraw();
2046 MULTI_THREAD_TEST_F(
2047 LayerTreeHostTestAbortedCommitDoesntStallSynchronousCompositor);
2049 class LayerTreeHostTestAbortedCommitDoesntStallDisabledVsync
2050 : public LayerTreeHostTestAbortedCommitDoesntStall {
2051 void InitializeSettings(LayerTreeSettings* settings) override {
2052 LayerTreeHostTestAbortedCommitDoesntStall::InitializeSettings(settings);
2053 settings->wait_for_beginframe_interval = false;
2054 settings->renderer_settings.disable_display_vsync = true;
2058 MULTI_THREAD_TEST_F(LayerTreeHostTestAbortedCommitDoesntStallDisabledVsync);
2060 class LayerTreeHostTestUninvertibleTransformDoesNotBlockActivation
2061 : public LayerTreeHostTest {
2062 protected:
2063 void SetupTree() override {
2064 LayerTreeHostTest::SetupTree();
2066 scoped_refptr<Layer> layer =
2067 PictureLayer::Create(layer_settings(), &client_);
2068 layer->SetTransform(gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
2069 layer->SetBounds(gfx::Size(10, 10));
2070 layer_tree_host()->root_layer()->AddChild(layer);
2073 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2075 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
2076 EndTest();
2079 void AfterTest() override {}
2081 FakeContentLayerClient client_;
2084 SINGLE_AND_MULTI_THREAD_TEST_F(
2085 LayerTreeHostTestUninvertibleTransformDoesNotBlockActivation);
2087 class LayerTreeHostTestChangeLayerPropertiesInPaintContents
2088 : public LayerTreeHostTest {
2089 public:
2090 class SetBoundsClient : public ContentLayerClient {
2091 public:
2092 SetBoundsClient() : layer_(0) {}
2094 void set_layer(Layer* layer) { layer_ = layer; }
2096 void PaintContents(SkCanvas* canvas,
2097 const gfx::Rect& clip,
2098 PaintingControlSetting picture_control) override {
2099 layer_->SetBounds(gfx::Size(2, 2));
2102 scoped_refptr<DisplayItemList> PaintContentsToDisplayList(
2103 const gfx::Rect& clip,
2104 PaintingControlSetting picture_control) override {
2105 NOTIMPLEMENTED();
2106 return nullptr;
2109 bool FillsBoundsCompletely() const override { return false; }
2110 size_t GetApproximateUnsharedMemoryUsage() const override { return 0; }
2112 private:
2113 Layer* layer_;
2116 LayerTreeHostTestChangeLayerPropertiesInPaintContents() : num_commits_(0) {}
2118 void SetupTree() override {
2119 scoped_refptr<PictureLayer> root_layer =
2120 PictureLayer::Create(layer_settings(), &client_);
2121 root_layer->SetIsDrawable(true);
2122 root_layer->SetBounds(gfx::Size(1, 1));
2123 client_.set_layer(root_layer.get());
2125 layer_tree_host()->SetRootLayer(root_layer);
2126 LayerTreeHostTest::SetupTree();
2129 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2130 void AfterTest() override {}
2132 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
2133 num_commits_++;
2134 if (num_commits_ == 1) {
2135 LayerImpl* root_layer = host_impl->active_tree()->root_layer();
2136 EXPECT_EQ(gfx::Size(1, 1), root_layer->bounds());
2137 } else {
2138 LayerImpl* root_layer = host_impl->active_tree()->root_layer();
2139 EXPECT_EQ(gfx::Size(2, 2), root_layer->bounds());
2140 EndTest();
2144 private:
2145 SetBoundsClient client_;
2146 int num_commits_;
2149 SINGLE_AND_MULTI_THREAD_TEST_F(
2150 LayerTreeHostTestChangeLayerPropertiesInPaintContents);
2152 class MockIOSurfaceWebGraphicsContext3D : public TestWebGraphicsContext3D {
2153 public:
2154 MockIOSurfaceWebGraphicsContext3D() {
2155 test_capabilities_.gpu.iosurface = true;
2156 test_capabilities_.gpu.texture_rectangle = true;
2159 GLuint createTexture() override { return 1; }
2160 MOCK_METHOD1(activeTexture, void(GLenum texture));
2161 MOCK_METHOD2(bindTexture, void(GLenum target,
2162 GLuint texture_id));
2163 MOCK_METHOD3(texParameteri, void(GLenum target,
2164 GLenum pname,
2165 GLint param));
2166 MOCK_METHOD5(texImageIOSurface2DCHROMIUM, void(GLenum target,
2167 GLint width,
2168 GLint height,
2169 GLuint ioSurfaceId,
2170 GLuint plane));
2171 MOCK_METHOD4(drawElements, void(GLenum mode,
2172 GLsizei count,
2173 GLenum type,
2174 GLintptr offset));
2175 MOCK_METHOD1(deleteTexture, void(GLenum texture));
2176 MOCK_METHOD3(produceTextureDirectCHROMIUM,
2177 void(GLuint texture, GLenum target, const GLbyte* mailbox));
2180 class LayerTreeHostTestIOSurfaceDrawing : public LayerTreeHostTest {
2181 protected:
2182 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
2183 scoped_ptr<MockIOSurfaceWebGraphicsContext3D> mock_context_owned(
2184 new MockIOSurfaceWebGraphicsContext3D);
2185 mock_context_ = mock_context_owned.get();
2187 if (delegating_renderer())
2188 return FakeOutputSurface::CreateDelegating3d(mock_context_owned.Pass());
2189 else
2190 return FakeOutputSurface::Create3d(mock_context_owned.Pass());
2193 void SetupTree() override {
2194 LayerTreeHostTest::SetupTree();
2196 layer_tree_host()->root_layer()->SetIsDrawable(false);
2198 io_surface_id_ = 9;
2199 io_surface_size_ = gfx::Size(6, 7);
2201 scoped_refptr<IOSurfaceLayer> io_surface_layer =
2202 IOSurfaceLayer::Create(layer_settings());
2203 io_surface_layer->SetBounds(gfx::Size(10, 10));
2204 io_surface_layer->SetIsDrawable(true);
2205 io_surface_layer->SetContentsOpaque(true);
2206 io_surface_layer->SetIOSurfaceProperties(io_surface_id_, io_surface_size_);
2207 layer_tree_host()->root_layer()->AddChild(io_surface_layer);
2210 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2212 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
2213 EXPECT_EQ(0u, host_impl->resource_provider()->num_resources());
2214 // In WillDraw, the IOSurfaceLayer sets up the io surface texture.
2216 EXPECT_CALL(*mock_context_, activeTexture(_)).Times(0);
2217 EXPECT_CALL(*mock_context_, bindTexture(GL_TEXTURE_RECTANGLE_ARB, 1))
2218 .Times(AtLeast(1));
2219 EXPECT_CALL(*mock_context_,
2220 texParameteri(
2221 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR))
2222 .Times(1);
2223 EXPECT_CALL(*mock_context_,
2224 texParameteri(
2225 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR))
2226 .Times(1);
2227 EXPECT_CALL(*mock_context_,
2228 texParameteri(GL_TEXTURE_RECTANGLE_ARB,
2229 GL_TEXTURE_POOL_CHROMIUM,
2230 GL_TEXTURE_POOL_UNMANAGED_CHROMIUM)).Times(1);
2231 EXPECT_CALL(*mock_context_,
2232 texParameteri(GL_TEXTURE_RECTANGLE_ARB,
2233 GL_TEXTURE_WRAP_S,
2234 GL_CLAMP_TO_EDGE)).Times(1);
2235 EXPECT_CALL(*mock_context_,
2236 texParameteri(GL_TEXTURE_RECTANGLE_ARB,
2237 GL_TEXTURE_WRAP_T,
2238 GL_CLAMP_TO_EDGE)).Times(1);
2240 EXPECT_CALL(*mock_context_,
2241 texImageIOSurface2DCHROMIUM(GL_TEXTURE_RECTANGLE_ARB,
2242 io_surface_size_.width(),
2243 io_surface_size_.height(),
2244 io_surface_id_,
2245 0)).Times(1);
2247 EXPECT_CALL(*mock_context_, bindTexture(_, 0)).Times(AnyNumber());
2250 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
2251 LayerTreeHostImpl::FrameData* frame,
2252 DrawResult draw_result) override {
2253 Mock::VerifyAndClearExpectations(&mock_context_);
2254 ResourceProvider* resource_provider = host_impl->resource_provider();
2255 EXPECT_EQ(1u, resource_provider->num_resources());
2256 CHECK_EQ(1u, frame->render_passes.size());
2257 CHECK_LE(1u, frame->render_passes[0]->quad_list.size());
2258 const DrawQuad* quad = frame->render_passes[0]->quad_list.front();
2259 CHECK_EQ(DrawQuad::IO_SURFACE_CONTENT, quad->material);
2260 const IOSurfaceDrawQuad* io_surface_draw_quad =
2261 IOSurfaceDrawQuad::MaterialCast(quad);
2262 EXPECT_EQ(io_surface_size_, io_surface_draw_quad->io_surface_size);
2263 EXPECT_NE(0u, io_surface_draw_quad->io_surface_resource_id());
2264 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_RECTANGLE_ARB),
2265 resource_provider->TargetForTesting(
2266 io_surface_draw_quad->io_surface_resource_id()));
2268 if (delegating_renderer()) {
2269 // The io surface layer's resource should be sent to the parent.
2270 EXPECT_CALL(*mock_context_, produceTextureDirectCHROMIUM(
2271 _, GL_TEXTURE_RECTANGLE_ARB, _)).Times(1);
2272 } else {
2273 // The io surface layer's texture is drawn.
2274 EXPECT_CALL(*mock_context_, activeTexture(GL_TEXTURE0)).Times(AtLeast(1));
2275 EXPECT_CALL(*mock_context_, drawElements(GL_TRIANGLES, 6, _, _))
2276 .Times(AtLeast(1));
2279 return draw_result;
2282 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
2283 Mock::VerifyAndClearExpectations(&mock_context_);
2285 EXPECT_CALL(*mock_context_, deleteTexture(1)).Times(AtLeast(1));
2286 EndTest();
2289 void AfterTest() override {}
2291 int io_surface_id_;
2292 MockIOSurfaceWebGraphicsContext3D* mock_context_;
2293 gfx::Size io_surface_size_;
2296 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestIOSurfaceDrawing);
2298 class LayerTreeHostTestNumFramesPending : public LayerTreeHostTest {
2299 public:
2300 void BeginTest() override {
2301 frame_ = 0;
2302 PostSetNeedsCommitToMainThread();
2305 // Round 1: commit + draw
2306 // Round 2: commit only (no draw/swap)
2307 // Round 3: draw only (no commit)
2309 void DidCommit() override {
2310 int commit = layer_tree_host()->source_frame_number();
2311 switch (commit) {
2312 case 2:
2313 // Round 2 done.
2314 EXPECT_EQ(1, frame_);
2315 layer_tree_host()->SetNeedsRedraw();
2316 break;
2320 void DidCompleteSwapBuffers() override {
2321 int commit = layer_tree_host()->source_frame_number();
2322 ++frame_;
2323 switch (frame_) {
2324 case 1:
2325 // Round 1 done.
2326 EXPECT_EQ(1, commit);
2327 layer_tree_host()->SetNeedsCommit();
2328 break;
2329 case 2:
2330 // Round 3 done.
2331 EXPECT_EQ(2, commit);
2332 EndTest();
2333 break;
2337 void AfterTest() override {}
2339 protected:
2340 int frame_;
2343 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestNumFramesPending);
2345 class LayerTreeHostTestResourcelessSoftwareDraw : public LayerTreeHostTest {
2346 public:
2347 void SetupTree() override {
2348 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
2349 root_layer_->SetIsDrawable(true);
2350 root_layer_->SetBounds(gfx::Size(50, 50));
2352 parent_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
2353 parent_layer_->SetIsDrawable(true);
2354 parent_layer_->SetBounds(gfx::Size(50, 50));
2355 parent_layer_->SetForceRenderSurface(true);
2357 child_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
2358 child_layer_->SetIsDrawable(true);
2359 child_layer_->SetBounds(gfx::Size(50, 50));
2361 root_layer_->AddChild(parent_layer_);
2362 parent_layer_->AddChild(child_layer_);
2363 layer_tree_host()->SetRootLayer(root_layer_);
2365 LayerTreeHostTest::SetupTree();
2368 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
2369 if (delegating_renderer()) {
2370 return FakeOutputSurface::CreateDelegatingSoftware(
2371 make_scoped_ptr(new SoftwareOutputDevice));
2372 } else {
2373 return FakeOutputSurface::CreateSoftware(
2374 make_scoped_ptr(new SoftwareOutputDevice));
2378 void BeginTest() override {
2379 PostSetNeedsCommitToMainThread();
2380 swap_count_ = 0;
2383 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
2384 LayerTreeHostImpl::FrameData* frame_data,
2385 DrawResult draw_result) override {
2386 if (host_impl->GetDrawMode() == DRAW_MODE_RESOURCELESS_SOFTWARE) {
2387 EXPECT_EQ(1u, frame_data->render_passes.size());
2388 // Has at least 3 quads for each layer.
2389 RenderPass* render_pass = frame_data->render_passes[0];
2390 EXPECT_GE(render_pass->quad_list.size(), 3u);
2391 } else {
2392 EXPECT_EQ(2u, frame_data->render_passes.size());
2394 // At least root layer quad in root render pass.
2395 EXPECT_GE(frame_data->render_passes[0]->quad_list.size(), 1u);
2396 // At least parent and child layer quads in parent render pass.
2397 EXPECT_GE(frame_data->render_passes[1]->quad_list.size(), 2u);
2399 return draw_result;
2402 void SwapBuffersCompleteOnThread(LayerTreeHostImpl* host_impl) override {
2403 swap_count_++;
2404 switch (swap_count_) {
2405 case 1: {
2406 gfx::Transform identity;
2407 gfx::Rect empty_rect;
2408 bool resourceless_software_draw = true;
2409 host_impl->SetExternalDrawConstraints(identity, empty_rect, empty_rect,
2410 empty_rect, identity,
2411 resourceless_software_draw);
2412 host_impl->SetFullRootLayerDamage();
2413 host_impl->SetNeedsRedraw();
2414 break;
2416 case 2:
2417 EndTest();
2418 break;
2419 default:
2420 NOTREACHED();
2424 void AfterTest() override {}
2426 private:
2427 FakeContentLayerClient client_;
2428 scoped_refptr<Layer> root_layer_;
2429 scoped_refptr<Layer> parent_layer_;
2430 scoped_refptr<Layer> child_layer_;
2431 int swap_count_;
2434 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestResourcelessSoftwareDraw);
2436 // Test for UI Resource management.
2437 class LayerTreeHostTestUIResource : public LayerTreeHostTest {
2438 public:
2439 LayerTreeHostTestUIResource() : num_ui_resources_(0) {}
2441 void InitializeSettings(LayerTreeSettings* settings) override {
2442 settings->renderer_settings.texture_id_allocation_chunk_size = 1;
2445 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2447 void DidCommit() override {
2448 int frame = layer_tree_host()->source_frame_number();
2449 switch (frame) {
2450 case 1:
2451 CreateResource();
2452 CreateResource();
2453 PostSetNeedsCommitToMainThread();
2454 break;
2455 case 2:
2456 // Usually ScopedUIResource are deleted from the manager in their
2457 // destructor. Here we just want to test that a direct call to
2458 // DeleteUIResource works.
2459 layer_tree_host()->DeleteUIResource(ui_resources_[0]->id());
2460 PostSetNeedsCommitToMainThread();
2461 break;
2462 case 3:
2463 // DeleteUIResource can be called with an invalid id.
2464 layer_tree_host()->DeleteUIResource(ui_resources_[0]->id());
2465 PostSetNeedsCommitToMainThread();
2466 break;
2467 case 4:
2468 CreateResource();
2469 CreateResource();
2470 PostSetNeedsCommitToMainThread();
2471 break;
2472 case 5:
2473 ClearResources();
2474 EndTest();
2475 break;
2479 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
2480 TestWebGraphicsContext3D* context = TestContext();
2482 int frame = impl->active_tree()->source_frame_number();
2483 switch (frame) {
2484 case 0:
2485 ASSERT_EQ(0u, context->NumTextures());
2486 break;
2487 case 1:
2488 // Created two textures.
2489 ASSERT_EQ(2u, context->NumTextures());
2490 break;
2491 case 2:
2492 // One texture left after one deletion.
2493 ASSERT_EQ(1u, context->NumTextures());
2494 break;
2495 case 3:
2496 // Resource manager state should not change when delete is called on an
2497 // invalid id.
2498 ASSERT_EQ(1u, context->NumTextures());
2499 break;
2500 case 4:
2501 // Creation after deletion: two more creates should total up to
2502 // three textures.
2503 ASSERT_EQ(3u, context->NumTextures());
2504 break;
2508 void AfterTest() override {}
2510 private:
2511 // Must clear all resources before exiting.
2512 void ClearResources() {
2513 for (int i = 0; i < num_ui_resources_; i++)
2514 ui_resources_[i] = nullptr;
2517 void CreateResource() {
2518 ui_resources_[num_ui_resources_++] =
2519 FakeScopedUIResource::Create(layer_tree_host());
2522 scoped_ptr<FakeScopedUIResource> ui_resources_[5];
2523 int num_ui_resources_;
2526 MULTI_THREAD_TEST_F(LayerTreeHostTestUIResource);
2528 class PushPropertiesCountingLayerImpl : public LayerImpl {
2529 public:
2530 static scoped_ptr<PushPropertiesCountingLayerImpl> Create(
2531 LayerTreeImpl* tree_impl, int id) {
2532 return make_scoped_ptr(new PushPropertiesCountingLayerImpl(tree_impl, id));
2535 ~PushPropertiesCountingLayerImpl() override {}
2537 void PushPropertiesTo(LayerImpl* layer) override {
2538 LayerImpl::PushPropertiesTo(layer);
2539 push_properties_count_++;
2540 // Push state to the active tree because we can only access it from there.
2541 static_cast<PushPropertiesCountingLayerImpl*>(
2542 layer)->push_properties_count_ = push_properties_count_;
2545 scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl) override {
2546 return PushPropertiesCountingLayerImpl::Create(tree_impl, id());
2549 size_t push_properties_count() const { return push_properties_count_; }
2550 void reset_push_properties_count() { push_properties_count_ = 0; }
2552 private:
2553 size_t push_properties_count_;
2555 PushPropertiesCountingLayerImpl(LayerTreeImpl* tree_impl, int id)
2556 : LayerImpl(tree_impl, id),
2557 push_properties_count_(0) {
2558 SetBounds(gfx::Size(1, 1));
2562 class PushPropertiesCountingLayer : public Layer {
2563 public:
2564 static scoped_refptr<PushPropertiesCountingLayer> Create(
2565 const LayerSettings& settings) {
2566 return new PushPropertiesCountingLayer(settings);
2569 void PushPropertiesTo(LayerImpl* layer) override {
2570 Layer::PushPropertiesTo(layer);
2571 push_properties_count_++;
2572 if (persist_needs_push_properties_)
2573 needs_push_properties_ = true;
2576 // Something to make this layer push properties, but no other layer.
2577 void MakePushProperties() { SetContentsOpaque(!contents_opaque()); }
2579 scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl) override {
2580 return PushPropertiesCountingLayerImpl::Create(tree_impl, id());
2583 void SetDrawsContent(bool draws_content) { SetIsDrawable(draws_content); }
2585 size_t push_properties_count() const { return push_properties_count_; }
2586 void reset_push_properties_count() { push_properties_count_ = 0; }
2588 void set_persist_needs_push_properties(bool persist) {
2589 persist_needs_push_properties_ = persist;
2592 private:
2593 explicit PushPropertiesCountingLayer(const LayerSettings& settings)
2594 : Layer(settings),
2595 push_properties_count_(0),
2596 persist_needs_push_properties_(false) {
2597 SetBounds(gfx::Size(1, 1));
2599 ~PushPropertiesCountingLayer() override {}
2601 size_t push_properties_count_;
2602 bool persist_needs_push_properties_;
2605 class LayerTreeHostTestLayersPushProperties : public LayerTreeHostTest {
2606 protected:
2607 void BeginTest() override {
2608 num_commits_ = 0;
2609 expected_push_properties_root_ = 0;
2610 expected_push_properties_child_ = 0;
2611 expected_push_properties_grandchild_ = 0;
2612 expected_push_properties_child2_ = 0;
2613 expected_push_properties_other_root_ = 0;
2614 expected_push_properties_leaf_layer_ = 0;
2615 PostSetNeedsCommitToMainThread();
2618 void SetupTree() override {
2619 root_ = PushPropertiesCountingLayer::Create(layer_settings());
2620 child_ = PushPropertiesCountingLayer::Create(layer_settings());
2621 child2_ = PushPropertiesCountingLayer::Create(layer_settings());
2622 grandchild_ = PushPropertiesCountingLayer::Create(layer_settings());
2623 leaf_always_pushing_layer_ =
2624 PushPropertiesCountingLayer::Create(layer_settings());
2625 leaf_always_pushing_layer_->set_persist_needs_push_properties(true);
2627 root_->AddChild(child_);
2628 root_->AddChild(child2_);
2629 child_->AddChild(grandchild_);
2630 child2_->AddChild(leaf_always_pushing_layer_);
2632 other_root_ = PushPropertiesCountingLayer::Create(layer_settings());
2634 // Don't set the root layer here.
2635 LayerTreeHostTest::SetupTree();
2638 void DidCommitAndDrawFrame() override {
2639 EXPECT_EQ(expected_push_properties_root_, root_->push_properties_count())
2640 << "num_commits: " << num_commits_;
2641 EXPECT_EQ(expected_push_properties_child_, child_->push_properties_count())
2642 << "num_commits: " << num_commits_;
2643 EXPECT_EQ(expected_push_properties_grandchild_,
2644 grandchild_->push_properties_count())
2645 << "num_commits: " << num_commits_;
2646 EXPECT_EQ(expected_push_properties_child2_,
2647 child2_->push_properties_count())
2648 << "num_commits: " << num_commits_;
2649 EXPECT_EQ(expected_push_properties_other_root_,
2650 other_root_->push_properties_count())
2651 << "num_commits: " << num_commits_;
2652 EXPECT_EQ(expected_push_properties_leaf_layer_,
2653 leaf_always_pushing_layer_->push_properties_count())
2654 << "num_commits: " << num_commits_;
2656 ++num_commits_;
2658 // The scrollbar layer always needs to be pushed.
2659 if (root_->layer_tree_host()) {
2660 EXPECT_TRUE(root_->descendant_needs_push_properties());
2661 EXPECT_FALSE(root_->needs_push_properties());
2663 if (child2_->layer_tree_host()) {
2664 EXPECT_TRUE(child2_->descendant_needs_push_properties());
2665 EXPECT_FALSE(child2_->needs_push_properties());
2667 if (leaf_always_pushing_layer_->layer_tree_host()) {
2668 EXPECT_FALSE(
2669 leaf_always_pushing_layer_->descendant_needs_push_properties());
2670 EXPECT_TRUE(leaf_always_pushing_layer_->needs_push_properties());
2673 // child_ and grandchild_ don't persist their need to push properties.
2674 if (child_->layer_tree_host()) {
2675 EXPECT_FALSE(child_->descendant_needs_push_properties());
2676 EXPECT_FALSE(child_->needs_push_properties());
2678 if (grandchild_->layer_tree_host()) {
2679 EXPECT_FALSE(grandchild_->descendant_needs_push_properties());
2680 EXPECT_FALSE(grandchild_->needs_push_properties());
2683 if (other_root_->layer_tree_host()) {
2684 EXPECT_FALSE(other_root_->descendant_needs_push_properties());
2685 EXPECT_FALSE(other_root_->needs_push_properties());
2688 switch (num_commits_) {
2689 case 1:
2690 layer_tree_host()->SetRootLayer(root_);
2691 // Layers added to the tree get committed.
2692 ++expected_push_properties_root_;
2693 ++expected_push_properties_child_;
2694 ++expected_push_properties_grandchild_;
2695 ++expected_push_properties_child2_;
2696 break;
2697 case 2:
2698 layer_tree_host()->SetNeedsCommit();
2699 // No layers need commit.
2700 break;
2701 case 3:
2702 layer_tree_host()->SetRootLayer(other_root_);
2703 // Layers added to the tree get committed.
2704 ++expected_push_properties_other_root_;
2705 break;
2706 case 4:
2707 layer_tree_host()->SetRootLayer(root_);
2708 // Layers added to the tree get committed.
2709 ++expected_push_properties_root_;
2710 ++expected_push_properties_child_;
2711 ++expected_push_properties_grandchild_;
2712 ++expected_push_properties_child2_;
2713 break;
2714 case 5:
2715 layer_tree_host()->SetNeedsCommit();
2716 // No layers need commit.
2717 break;
2718 case 6:
2719 child_->RemoveFromParent();
2720 // No layers need commit.
2721 break;
2722 case 7:
2723 root_->AddChild(child_);
2724 // Layers added to the tree get committed.
2725 ++expected_push_properties_child_;
2726 ++expected_push_properties_grandchild_;
2727 break;
2728 case 8:
2729 grandchild_->RemoveFromParent();
2730 // No layers need commit.
2731 break;
2732 case 9:
2733 child_->AddChild(grandchild_);
2734 // Layers added to the tree get committed.
2735 ++expected_push_properties_grandchild_;
2736 break;
2737 case 10:
2738 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
2739 // No layers need commit.
2740 break;
2741 case 11:
2742 layer_tree_host()->SetPageScaleFactorAndLimits(1.f, 0.8f, 1.1f);
2743 // No layers need commit.
2744 break;
2745 case 12:
2746 child_->MakePushProperties();
2747 // The modified layer needs commit
2748 ++expected_push_properties_child_;
2749 break;
2750 case 13:
2751 child2_->MakePushProperties();
2752 // The modified layer needs commit
2753 ++expected_push_properties_child2_;
2754 break;
2755 case 14:
2756 child_->RemoveFromParent();
2757 root_->AddChild(child_);
2758 // Layers added to the tree get committed.
2759 ++expected_push_properties_child_;
2760 ++expected_push_properties_grandchild_;
2761 break;
2762 case 15:
2763 grandchild_->MakePushProperties();
2764 // The modified layer needs commit
2765 ++expected_push_properties_grandchild_;
2766 break;
2767 case 16:
2768 // SetNeedsDisplay does not always set needs commit (so call it
2769 // explicitly), but is a property change.
2770 child_->SetNeedsDisplay();
2771 ++expected_push_properties_child_;
2772 layer_tree_host()->SetNeedsCommit();
2773 break;
2774 case 17:
2775 EndTest();
2776 break;
2779 // The leaf layer always pushes.
2780 if (leaf_always_pushing_layer_->layer_tree_host())
2781 ++expected_push_properties_leaf_layer_;
2784 void AfterTest() override {}
2786 int num_commits_;
2787 FakeContentLayerClient client_;
2788 scoped_refptr<PushPropertiesCountingLayer> root_;
2789 scoped_refptr<PushPropertiesCountingLayer> child_;
2790 scoped_refptr<PushPropertiesCountingLayer> child2_;
2791 scoped_refptr<PushPropertiesCountingLayer> grandchild_;
2792 scoped_refptr<PushPropertiesCountingLayer> other_root_;
2793 scoped_refptr<PushPropertiesCountingLayer> leaf_always_pushing_layer_;
2794 size_t expected_push_properties_root_;
2795 size_t expected_push_properties_child_;
2796 size_t expected_push_properties_child2_;
2797 size_t expected_push_properties_grandchild_;
2798 size_t expected_push_properties_other_root_;
2799 size_t expected_push_properties_leaf_layer_;
2802 MULTI_THREAD_TEST_F(LayerTreeHostTestLayersPushProperties);
2804 class LayerTreeHostTestImplLayersPushProperties
2805 : public LayerTreeHostTestLayersPushProperties {
2806 protected:
2807 void BeginTest() override {
2808 expected_push_properties_root_impl_ = 0;
2809 expected_push_properties_child_impl_ = 0;
2810 expected_push_properties_grandchild_impl_ = 0;
2811 expected_push_properties_child2_impl_ = 0;
2812 expected_push_properties_grandchild2_impl_ = 0;
2813 LayerTreeHostTestLayersPushProperties::BeginTest();
2816 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
2817 // These commits are in response to the changes made in
2818 // LayerTreeHostTestLayersPushProperties::DidCommitAndDrawFrame()
2819 switch (num_commits_) {
2820 case 0:
2821 // Tree hasn't been setup yet don't bother to check anything.
2822 return;
2823 case 1:
2824 // Root gets set up, Everyone is initialized.
2825 ++expected_push_properties_root_impl_;
2826 ++expected_push_properties_child_impl_;
2827 ++expected_push_properties_grandchild_impl_;
2828 ++expected_push_properties_child2_impl_;
2829 ++expected_push_properties_grandchild2_impl_;
2830 break;
2831 case 2:
2832 // Tree doesn't change but the one leaf that always pushes is pushed.
2833 ++expected_push_properties_grandchild2_impl_;
2834 break;
2835 case 3:
2836 // Root is swapped here.
2837 // Clear the expected push properties the tree will be rebuilt.
2838 expected_push_properties_root_impl_ = 0;
2839 expected_push_properties_child_impl_ = 0;
2840 expected_push_properties_grandchild_impl_ = 0;
2841 expected_push_properties_child2_impl_ = 0;
2842 expected_push_properties_grandchild2_impl_ = 0;
2844 // Make sure the new root is pushed.
2845 EXPECT_EQ(1u, static_cast<PushPropertiesCountingLayerImpl*>(
2846 host_impl->RootLayer())->push_properties_count());
2847 return;
2848 case 4:
2849 // Root is swapped back all of the layers in the tree get pushed.
2850 ++expected_push_properties_root_impl_;
2851 ++expected_push_properties_child_impl_;
2852 ++expected_push_properties_grandchild_impl_;
2853 ++expected_push_properties_child2_impl_;
2854 ++expected_push_properties_grandchild2_impl_;
2855 break;
2856 case 5:
2857 // Tree doesn't change but the one leaf that always pushes is pushed.
2858 ++expected_push_properties_grandchild2_impl_;
2859 break;
2860 case 6:
2861 // First child is removed. Structure of the tree changes here so swap
2862 // some of the values. child_impl becomes child2_impl.
2863 expected_push_properties_child_impl_ =
2864 expected_push_properties_child2_impl_;
2865 expected_push_properties_child2_impl_ = 0;
2866 // grandchild_impl becomes grandchild2_impl.
2867 expected_push_properties_grandchild_impl_ =
2868 expected_push_properties_grandchild2_impl_;
2869 expected_push_properties_grandchild2_impl_ = 0;
2871 // grandchild_impl is now the leaf that always pushes. It is pushed.
2872 ++expected_push_properties_grandchild_impl_;
2873 break;
2874 case 7:
2875 // The leaf that always pushes is pushed.
2876 ++expected_push_properties_grandchild_impl_;
2878 // Child is added back. New layers are initialized.
2879 ++expected_push_properties_grandchild2_impl_;
2880 ++expected_push_properties_child2_impl_;
2881 break;
2882 case 8:
2883 // Leaf is removed.
2884 expected_push_properties_grandchild2_impl_ = 0;
2886 // Always pushing.
2887 ++expected_push_properties_grandchild_impl_;
2888 break;
2889 case 9:
2890 // Leaf is added back
2891 ++expected_push_properties_grandchild2_impl_;
2893 // The leaf that always pushes is pushed.
2894 ++expected_push_properties_grandchild_impl_;
2895 break;
2896 case 10:
2897 // The leaf that always pushes is pushed.
2898 ++expected_push_properties_grandchild_impl_;
2899 break;
2900 case 11:
2901 // The leaf that always pushes is pushed.
2902 ++expected_push_properties_grandchild_impl_;
2903 break;
2904 case 12:
2905 // The leaf that always pushes is pushed.
2906 ++expected_push_properties_grandchild_impl_;
2908 // This child position was changed.
2909 ++expected_push_properties_child2_impl_;
2910 break;
2911 case 13:
2912 // The position of this child was changed.
2913 ++expected_push_properties_child_impl_;
2915 // The leaf that always pushes is pushed.
2916 ++expected_push_properties_grandchild_impl_;
2917 break;
2918 case 14:
2919 // Second child is removed from tree. Don't discard counts because
2920 // they are added back before commit.
2922 // The leaf that always pushes is pushed.
2923 ++expected_push_properties_grandchild_impl_;
2925 // Second child added back.
2926 ++expected_push_properties_child2_impl_;
2927 ++expected_push_properties_grandchild2_impl_;
2929 break;
2930 case 15:
2931 // The position of this child was changed.
2932 ++expected_push_properties_grandchild2_impl_;
2934 // The leaf that always pushes is pushed.
2935 ++expected_push_properties_grandchild_impl_;
2936 break;
2937 case 16:
2938 // Second child is invalidated with SetNeedsDisplay
2939 ++expected_push_properties_child2_impl_;
2941 // The leaf that always pushed is pushed.
2942 ++expected_push_properties_grandchild_impl_;
2943 break;
2946 PushPropertiesCountingLayerImpl* root_impl_ = NULL;
2947 PushPropertiesCountingLayerImpl* child_impl_ = NULL;
2948 PushPropertiesCountingLayerImpl* child2_impl_ = NULL;
2949 PushPropertiesCountingLayerImpl* grandchild_impl_ = NULL;
2950 PushPropertiesCountingLayerImpl* leaf_always_pushing_layer_impl_ = NULL;
2952 // Pull the layers that we need from the tree assuming the same structure
2953 // as LayerTreeHostTestLayersPushProperties
2954 root_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
2955 host_impl->RootLayer());
2957 if (root_impl_ && root_impl_->children().size() > 0) {
2958 child_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
2959 root_impl_->children()[0]);
2961 if (child_impl_ && child_impl_->children().size() > 0)
2962 grandchild_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
2963 child_impl_->children()[0]);
2966 if (root_impl_ && root_impl_->children().size() > 1) {
2967 child2_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
2968 root_impl_->children()[1]);
2970 if (child2_impl_ && child2_impl_->children().size() > 0)
2971 leaf_always_pushing_layer_impl_ =
2972 static_cast<PushPropertiesCountingLayerImpl*>(
2973 child2_impl_->children()[0]);
2976 if (root_impl_)
2977 EXPECT_EQ(expected_push_properties_root_impl_,
2978 root_impl_->push_properties_count());
2979 if (child_impl_)
2980 EXPECT_EQ(expected_push_properties_child_impl_,
2981 child_impl_->push_properties_count());
2982 if (grandchild_impl_)
2983 EXPECT_EQ(expected_push_properties_grandchild_impl_,
2984 grandchild_impl_->push_properties_count());
2985 if (child2_impl_)
2986 EXPECT_EQ(expected_push_properties_child2_impl_,
2987 child2_impl_->push_properties_count());
2988 if (leaf_always_pushing_layer_impl_)
2989 EXPECT_EQ(expected_push_properties_grandchild2_impl_,
2990 leaf_always_pushing_layer_impl_->push_properties_count());
2993 size_t expected_push_properties_root_impl_;
2994 size_t expected_push_properties_child_impl_;
2995 size_t expected_push_properties_child2_impl_;
2996 size_t expected_push_properties_grandchild_impl_;
2997 size_t expected_push_properties_grandchild2_impl_;
3000 // In single thread there's no pending tree to push properties from.
3001 MULTI_THREAD_TEST_F(LayerTreeHostTestImplLayersPushProperties);
3003 class LayerTreeHostTestPropertyChangesDuringUpdateArePushed
3004 : public LayerTreeHostTest {
3005 protected:
3006 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
3008 void SetupTree() override {
3009 root_ = Layer::Create(layer_settings());
3010 root_->SetBounds(gfx::Size(1, 1));
3012 bool paint_scrollbar = true;
3013 bool has_thumb = false;
3014 scrollbar_layer_ = FakePaintedScrollbarLayer::Create(
3015 layer_settings(), paint_scrollbar, has_thumb, root_->id());
3017 root_->AddChild(scrollbar_layer_);
3019 layer_tree_host()->SetRootLayer(root_);
3020 LayerTreeHostTest::SetupTree();
3023 void DidCommitAndDrawFrame() override {
3024 switch (layer_tree_host()->source_frame_number()) {
3025 case 0:
3026 break;
3027 case 1: {
3028 // During update, the ignore_set_needs_commit_ bit is set to true to
3029 // avoid causing a second commit to be scheduled. If a property change
3030 // is made during this, however, it needs to be pushed in the upcoming
3031 // commit.
3032 scoped_ptr<base::AutoReset<bool>> ignore =
3033 scrollbar_layer_->IgnoreSetNeedsCommit();
3035 scrollbar_layer_->SetBounds(gfx::Size(30, 30));
3037 EXPECT_TRUE(scrollbar_layer_->needs_push_properties());
3038 EXPECT_TRUE(root_->descendant_needs_push_properties());
3039 layer_tree_host()->SetNeedsCommit();
3041 scrollbar_layer_->reset_push_properties_count();
3042 EXPECT_EQ(0u, scrollbar_layer_->push_properties_count());
3043 break;
3045 case 2:
3046 EXPECT_EQ(1u, scrollbar_layer_->push_properties_count());
3047 EndTest();
3048 break;
3052 void AfterTest() override {}
3054 scoped_refptr<Layer> root_;
3055 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer_;
3058 MULTI_THREAD_TEST_F(LayerTreeHostTestPropertyChangesDuringUpdateArePushed);
3060 class LayerTreeHostTestSetDrawableCausesCommit : public LayerTreeHostTest {
3061 protected:
3062 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
3064 void SetupTree() override {
3065 root_ = PushPropertiesCountingLayer::Create(layer_settings());
3066 child_ = PushPropertiesCountingLayer::Create(layer_settings());
3067 root_->AddChild(child_);
3069 layer_tree_host()->SetRootLayer(root_);
3070 LayerTreeHostTest::SetupTree();
3073 void DidCommitAndDrawFrame() override {
3074 switch (layer_tree_host()->source_frame_number()) {
3075 case 0:
3076 break;
3077 case 1: {
3078 // During update, the ignore_set_needs_commit_ bit is set to true to
3079 // avoid causing a second commit to be scheduled. If a property change
3080 // is made during this, however, it needs to be pushed in the upcoming
3081 // commit.
3082 EXPECT_FALSE(root_->needs_push_properties());
3083 EXPECT_FALSE(child_->needs_push_properties());
3084 EXPECT_EQ(0, root_->NumDescendantsThatDrawContent());
3085 root_->reset_push_properties_count();
3086 child_->reset_push_properties_count();
3087 child_->SetDrawsContent(true);
3088 EXPECT_EQ(1, root_->NumDescendantsThatDrawContent());
3089 EXPECT_EQ(0u, root_->push_properties_count());
3090 EXPECT_EQ(0u, child_->push_properties_count());
3091 EXPECT_TRUE(root_->needs_push_properties());
3092 EXPECT_TRUE(child_->needs_push_properties());
3093 break;
3095 case 2:
3096 EXPECT_EQ(1u, root_->push_properties_count());
3097 EXPECT_EQ(1u, child_->push_properties_count());
3098 EXPECT_FALSE(root_->needs_push_properties());
3099 EXPECT_FALSE(child_->needs_push_properties());
3100 EndTest();
3101 break;
3105 void AfterTest() override {}
3107 scoped_refptr<PushPropertiesCountingLayer> root_;
3108 scoped_refptr<PushPropertiesCountingLayer> child_;
3111 MULTI_THREAD_TEST_F(LayerTreeHostTestSetDrawableCausesCommit);
3113 class LayerTreeHostTestCasePushPropertiesThreeGrandChildren
3114 : public LayerTreeHostTest {
3115 protected:
3116 void BeginTest() override {
3117 expected_push_properties_root_ = 0;
3118 expected_push_properties_child_ = 0;
3119 expected_push_properties_grandchild1_ = 0;
3120 expected_push_properties_grandchild2_ = 0;
3121 expected_push_properties_grandchild3_ = 0;
3122 PostSetNeedsCommitToMainThread();
3125 void SetupTree() override {
3126 root_ = PushPropertiesCountingLayer::Create(layer_settings());
3127 child_ = PushPropertiesCountingLayer::Create(layer_settings());
3128 grandchild1_ = PushPropertiesCountingLayer::Create(layer_settings());
3129 grandchild2_ = PushPropertiesCountingLayer::Create(layer_settings());
3130 grandchild3_ = PushPropertiesCountingLayer::Create(layer_settings());
3132 root_->AddChild(child_);
3133 child_->AddChild(grandchild1_);
3134 child_->AddChild(grandchild2_);
3135 child_->AddChild(grandchild3_);
3137 // Don't set the root layer here.
3138 LayerTreeHostTest::SetupTree();
3141 void AfterTest() override {}
3143 FakeContentLayerClient client_;
3144 scoped_refptr<PushPropertiesCountingLayer> root_;
3145 scoped_refptr<PushPropertiesCountingLayer> child_;
3146 scoped_refptr<PushPropertiesCountingLayer> grandchild1_;
3147 scoped_refptr<PushPropertiesCountingLayer> grandchild2_;
3148 scoped_refptr<PushPropertiesCountingLayer> grandchild3_;
3149 size_t expected_push_properties_root_;
3150 size_t expected_push_properties_child_;
3151 size_t expected_push_properties_grandchild1_;
3152 size_t expected_push_properties_grandchild2_;
3153 size_t expected_push_properties_grandchild3_;
3156 class LayerTreeHostTestPushPropertiesAddingToTreeRequiresPush
3157 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3158 protected:
3159 void DidCommitAndDrawFrame() override {
3160 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3161 switch (last_source_frame_number) {
3162 case 0:
3163 EXPECT_FALSE(root_->needs_push_properties());
3164 EXPECT_FALSE(root_->descendant_needs_push_properties());
3165 EXPECT_FALSE(child_->needs_push_properties());
3166 EXPECT_FALSE(child_->descendant_needs_push_properties());
3167 EXPECT_FALSE(grandchild1_->needs_push_properties());
3168 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3169 EXPECT_FALSE(grandchild2_->needs_push_properties());
3170 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3171 EXPECT_FALSE(grandchild3_->needs_push_properties());
3172 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3174 layer_tree_host()->SetRootLayer(root_);
3176 EXPECT_TRUE(root_->needs_push_properties());
3177 EXPECT_TRUE(root_->descendant_needs_push_properties());
3178 EXPECT_TRUE(child_->needs_push_properties());
3179 EXPECT_TRUE(child_->descendant_needs_push_properties());
3180 EXPECT_TRUE(grandchild1_->needs_push_properties());
3181 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3182 EXPECT_TRUE(grandchild2_->needs_push_properties());
3183 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3184 EXPECT_TRUE(grandchild3_->needs_push_properties());
3185 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3186 break;
3187 case 1:
3188 EndTest();
3189 break;
3194 MULTI_THREAD_TEST_F(LayerTreeHostTestPushPropertiesAddingToTreeRequiresPush);
3196 class LayerTreeHostTestPushPropertiesRemovingChildStopsRecursion
3197 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3198 protected:
3199 void DidCommitAndDrawFrame() override {
3200 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3201 switch (last_source_frame_number) {
3202 case 0:
3203 layer_tree_host()->SetRootLayer(root_);
3204 break;
3205 case 1:
3206 EXPECT_FALSE(root_->needs_push_properties());
3207 EXPECT_FALSE(root_->descendant_needs_push_properties());
3208 EXPECT_FALSE(child_->needs_push_properties());
3209 EXPECT_FALSE(child_->descendant_needs_push_properties());
3210 EXPECT_FALSE(grandchild1_->needs_push_properties());
3211 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3212 EXPECT_FALSE(grandchild2_->needs_push_properties());
3213 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3214 EXPECT_FALSE(grandchild3_->needs_push_properties());
3215 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3217 grandchild1_->RemoveFromParent();
3218 grandchild1_->SetPosition(gfx::Point(1, 1));
3220 EXPECT_FALSE(root_->needs_push_properties());
3221 EXPECT_FALSE(root_->descendant_needs_push_properties());
3222 EXPECT_FALSE(child_->needs_push_properties());
3223 EXPECT_FALSE(child_->descendant_needs_push_properties());
3224 EXPECT_FALSE(grandchild2_->needs_push_properties());
3225 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3226 EXPECT_FALSE(grandchild3_->needs_push_properties());
3227 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3229 child_->AddChild(grandchild1_);
3231 EXPECT_FALSE(root_->needs_push_properties());
3232 EXPECT_TRUE(root_->descendant_needs_push_properties());
3233 EXPECT_FALSE(child_->needs_push_properties());
3234 EXPECT_TRUE(child_->descendant_needs_push_properties());
3235 EXPECT_TRUE(grandchild1_->needs_push_properties());
3236 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3237 EXPECT_FALSE(grandchild2_->needs_push_properties());
3238 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3239 EXPECT_FALSE(grandchild3_->needs_push_properties());
3240 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3242 grandchild2_->SetPosition(gfx::Point(1, 1));
3244 EXPECT_FALSE(root_->needs_push_properties());
3245 EXPECT_TRUE(root_->descendant_needs_push_properties());
3246 EXPECT_FALSE(child_->needs_push_properties());
3247 EXPECT_TRUE(child_->descendant_needs_push_properties());
3248 EXPECT_TRUE(grandchild1_->needs_push_properties());
3249 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3250 EXPECT_TRUE(grandchild2_->needs_push_properties());
3251 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3252 EXPECT_FALSE(grandchild3_->needs_push_properties());
3253 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3255 // grandchild2_ will still need a push properties.
3256 grandchild1_->RemoveFromParent();
3258 EXPECT_FALSE(root_->needs_push_properties());
3259 EXPECT_TRUE(root_->descendant_needs_push_properties());
3260 EXPECT_FALSE(child_->needs_push_properties());
3261 EXPECT_TRUE(child_->descendant_needs_push_properties());
3263 // grandchild3_ does not need a push properties, so recursing should
3264 // no longer be needed.
3265 grandchild2_->RemoveFromParent();
3267 EXPECT_FALSE(root_->needs_push_properties());
3268 EXPECT_FALSE(root_->descendant_needs_push_properties());
3269 EXPECT_FALSE(child_->needs_push_properties());
3270 EXPECT_FALSE(child_->descendant_needs_push_properties());
3271 EndTest();
3272 break;
3277 MULTI_THREAD_TEST_F(LayerTreeHostTestPushPropertiesRemovingChildStopsRecursion);
3279 class LayerTreeHostTestPushPropertiesRemovingChildStopsRecursionWithPersistence
3280 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3281 protected:
3282 void DidCommitAndDrawFrame() override {
3283 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3284 switch (last_source_frame_number) {
3285 case 0:
3286 layer_tree_host()->SetRootLayer(root_);
3287 grandchild1_->set_persist_needs_push_properties(true);
3288 grandchild2_->set_persist_needs_push_properties(true);
3289 break;
3290 case 1:
3291 EXPECT_FALSE(root_->needs_push_properties());
3292 EXPECT_TRUE(root_->descendant_needs_push_properties());
3293 EXPECT_FALSE(child_->needs_push_properties());
3294 EXPECT_TRUE(child_->descendant_needs_push_properties());
3295 EXPECT_TRUE(grandchild1_->needs_push_properties());
3296 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3297 EXPECT_TRUE(grandchild2_->needs_push_properties());
3298 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3299 EXPECT_FALSE(grandchild3_->needs_push_properties());
3300 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3302 // grandchild2_ will still need a push properties.
3303 grandchild1_->RemoveFromParent();
3305 EXPECT_FALSE(root_->needs_push_properties());
3306 EXPECT_TRUE(root_->descendant_needs_push_properties());
3307 EXPECT_FALSE(child_->needs_push_properties());
3308 EXPECT_TRUE(child_->descendant_needs_push_properties());
3310 // grandchild3_ does not need a push properties, so recursing should
3311 // no longer be needed.
3312 grandchild2_->RemoveFromParent();
3314 EXPECT_FALSE(root_->needs_push_properties());
3315 EXPECT_FALSE(root_->descendant_needs_push_properties());
3316 EXPECT_FALSE(child_->needs_push_properties());
3317 EXPECT_FALSE(child_->descendant_needs_push_properties());
3318 EndTest();
3319 break;
3324 MULTI_THREAD_TEST_F(
3325 LayerTreeHostTestPushPropertiesRemovingChildStopsRecursionWithPersistence);
3327 class LayerTreeHostTestPushPropertiesSetPropertiesWhileOutsideTree
3328 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3329 protected:
3330 void DidCommitAndDrawFrame() override {
3331 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3332 switch (last_source_frame_number) {
3333 case 0:
3334 layer_tree_host()->SetRootLayer(root_);
3335 break;
3336 case 1:
3337 EXPECT_FALSE(root_->needs_push_properties());
3338 EXPECT_FALSE(root_->descendant_needs_push_properties());
3339 EXPECT_FALSE(child_->needs_push_properties());
3340 EXPECT_FALSE(child_->descendant_needs_push_properties());
3341 EXPECT_FALSE(grandchild1_->needs_push_properties());
3342 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3343 EXPECT_FALSE(grandchild2_->needs_push_properties());
3344 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3345 EXPECT_FALSE(grandchild3_->needs_push_properties());
3346 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3348 // Change grandchildren while their parent is not in the tree.
3349 child_->RemoveFromParent();
3350 grandchild1_->SetPosition(gfx::Point(1, 1));
3351 grandchild2_->SetPosition(gfx::Point(1, 1));
3352 root_->AddChild(child_);
3354 EXPECT_FALSE(root_->needs_push_properties());
3355 EXPECT_TRUE(root_->descendant_needs_push_properties());
3356 EXPECT_TRUE(child_->needs_push_properties());
3357 EXPECT_TRUE(child_->descendant_needs_push_properties());
3358 EXPECT_TRUE(grandchild1_->needs_push_properties());
3359 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3360 EXPECT_TRUE(grandchild2_->needs_push_properties());
3361 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3362 EXPECT_TRUE(grandchild3_->needs_push_properties());
3363 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3365 grandchild1_->RemoveFromParent();
3367 EXPECT_FALSE(root_->needs_push_properties());
3368 EXPECT_TRUE(root_->descendant_needs_push_properties());
3369 EXPECT_TRUE(child_->needs_push_properties());
3370 EXPECT_TRUE(child_->descendant_needs_push_properties());
3372 grandchild2_->RemoveFromParent();
3374 EXPECT_FALSE(root_->needs_push_properties());
3375 EXPECT_TRUE(root_->descendant_needs_push_properties());
3376 EXPECT_TRUE(child_->needs_push_properties());
3377 EXPECT_TRUE(child_->descendant_needs_push_properties());
3379 grandchild3_->RemoveFromParent();
3381 EXPECT_FALSE(root_->needs_push_properties());
3382 EXPECT_TRUE(root_->descendant_needs_push_properties());
3383 EXPECT_TRUE(child_->needs_push_properties());
3384 EXPECT_FALSE(child_->descendant_needs_push_properties());
3386 EndTest();
3387 break;
3392 MULTI_THREAD_TEST_F(
3393 LayerTreeHostTestPushPropertiesSetPropertiesWhileOutsideTree);
3395 class LayerTreeHostTestPushPropertiesSetPropertyInParentThenChild
3396 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3397 protected:
3398 void DidCommitAndDrawFrame() override {
3399 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3400 switch (last_source_frame_number) {
3401 case 0:
3402 layer_tree_host()->SetRootLayer(root_);
3403 break;
3404 case 1:
3405 EXPECT_FALSE(root_->needs_push_properties());
3406 EXPECT_FALSE(root_->descendant_needs_push_properties());
3407 EXPECT_FALSE(child_->needs_push_properties());
3408 EXPECT_FALSE(child_->descendant_needs_push_properties());
3409 EXPECT_FALSE(grandchild1_->needs_push_properties());
3410 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3411 EXPECT_FALSE(grandchild2_->needs_push_properties());
3412 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3413 EXPECT_FALSE(grandchild3_->needs_push_properties());
3414 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3416 child_->SetPosition(gfx::Point(1, 1));
3417 grandchild1_->SetPosition(gfx::Point(1, 1));
3418 grandchild2_->SetPosition(gfx::Point(1, 1));
3420 EXPECT_FALSE(root_->needs_push_properties());
3421 EXPECT_TRUE(root_->descendant_needs_push_properties());
3422 EXPECT_TRUE(child_->needs_push_properties());
3423 EXPECT_TRUE(child_->descendant_needs_push_properties());
3424 EXPECT_TRUE(grandchild1_->needs_push_properties());
3425 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3426 EXPECT_TRUE(grandchild2_->needs_push_properties());
3427 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3428 EXPECT_FALSE(grandchild3_->needs_push_properties());
3429 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3431 grandchild1_->RemoveFromParent();
3433 EXPECT_FALSE(root_->needs_push_properties());
3434 EXPECT_TRUE(root_->descendant_needs_push_properties());
3435 EXPECT_TRUE(child_->needs_push_properties());
3436 EXPECT_TRUE(child_->descendant_needs_push_properties());
3438 grandchild2_->RemoveFromParent();
3440 EXPECT_FALSE(root_->needs_push_properties());
3441 EXPECT_TRUE(root_->descendant_needs_push_properties());
3442 EXPECT_TRUE(child_->needs_push_properties());
3443 EXPECT_FALSE(child_->descendant_needs_push_properties());
3445 child_->RemoveFromParent();
3447 EXPECT_FALSE(root_->needs_push_properties());
3448 EXPECT_FALSE(root_->descendant_needs_push_properties());
3450 EndTest();
3451 break;
3456 MULTI_THREAD_TEST_F(
3457 LayerTreeHostTestPushPropertiesSetPropertyInParentThenChild);
3459 class LayerTreeHostTestPushPropertiesSetPropertyInChildThenParent
3460 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3461 protected:
3462 void DidCommitAndDrawFrame() override {
3463 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3464 switch (last_source_frame_number) {
3465 case 0:
3466 layer_tree_host()->SetRootLayer(root_);
3467 break;
3468 case 1:
3469 EXPECT_FALSE(root_->needs_push_properties());
3470 EXPECT_FALSE(root_->descendant_needs_push_properties());
3471 EXPECT_FALSE(child_->needs_push_properties());
3472 EXPECT_FALSE(child_->descendant_needs_push_properties());
3473 EXPECT_FALSE(grandchild1_->needs_push_properties());
3474 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3475 EXPECT_FALSE(grandchild2_->needs_push_properties());
3476 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3477 EXPECT_FALSE(grandchild3_->needs_push_properties());
3478 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3480 grandchild1_->SetPosition(gfx::Point(1, 1));
3481 grandchild2_->SetPosition(gfx::Point(1, 1));
3482 child_->SetPosition(gfx::Point(1, 1));
3484 EXPECT_FALSE(root_->needs_push_properties());
3485 EXPECT_TRUE(root_->descendant_needs_push_properties());
3486 EXPECT_TRUE(child_->needs_push_properties());
3487 EXPECT_TRUE(child_->descendant_needs_push_properties());
3488 EXPECT_TRUE(grandchild1_->needs_push_properties());
3489 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3490 EXPECT_TRUE(grandchild2_->needs_push_properties());
3491 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3492 EXPECT_FALSE(grandchild3_->needs_push_properties());
3493 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3495 grandchild1_->RemoveFromParent();
3497 EXPECT_FALSE(root_->needs_push_properties());
3498 EXPECT_TRUE(root_->descendant_needs_push_properties());
3499 EXPECT_TRUE(child_->needs_push_properties());
3500 EXPECT_TRUE(child_->descendant_needs_push_properties());
3502 grandchild2_->RemoveFromParent();
3504 EXPECT_FALSE(root_->needs_push_properties());
3505 EXPECT_TRUE(root_->descendant_needs_push_properties());
3506 EXPECT_TRUE(child_->needs_push_properties());
3507 EXPECT_FALSE(child_->descendant_needs_push_properties());
3509 child_->RemoveFromParent();
3511 EXPECT_FALSE(root_->needs_push_properties());
3512 EXPECT_FALSE(root_->descendant_needs_push_properties());
3514 EndTest();
3515 break;
3520 MULTI_THREAD_TEST_F(
3521 LayerTreeHostTestPushPropertiesSetPropertyInChildThenParent);
3523 // This test verifies that the tree activation callback is invoked correctly.
3524 class LayerTreeHostTestTreeActivationCallback : public LayerTreeHostTest {
3525 public:
3526 LayerTreeHostTestTreeActivationCallback()
3527 : num_commits_(0), callback_count_(0) {}
3529 void BeginTest() override {
3530 PostSetNeedsCommitToMainThread();
3533 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
3534 LayerTreeHostImpl::FrameData* frame_data,
3535 DrawResult draw_result) override {
3536 ++num_commits_;
3537 switch (num_commits_) {
3538 case 1:
3539 EXPECT_EQ(0, callback_count_);
3540 callback_count_ = 0;
3541 SetCallback(true);
3542 PostSetNeedsCommitToMainThread();
3543 break;
3544 case 2:
3545 EXPECT_EQ(1, callback_count_);
3546 callback_count_ = 0;
3547 SetCallback(false);
3548 PostSetNeedsCommitToMainThread();
3549 break;
3550 case 3:
3551 EXPECT_EQ(0, callback_count_);
3552 callback_count_ = 0;
3553 EndTest();
3554 break;
3555 default:
3556 ADD_FAILURE() << num_commits_;
3557 EndTest();
3558 break;
3560 return LayerTreeHostTest::PrepareToDrawOnThread(
3561 host_impl, frame_data, draw_result);
3564 void AfterTest() override { EXPECT_EQ(3, num_commits_); }
3566 void SetCallback(bool enable) {
3567 output_surface()->SetTreeActivationCallback(
3568 enable
3569 ? base::Bind(
3570 &LayerTreeHostTestTreeActivationCallback::ActivationCallback,
3571 base::Unretained(this))
3572 : base::Closure());
3575 void ActivationCallback() { ++callback_count_; }
3577 int num_commits_;
3578 int callback_count_;
3581 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestTreeActivationCallback);
3583 class LayerInvalidateCausesDraw : public LayerTreeHostTest {
3584 public:
3585 LayerInvalidateCausesDraw() : num_commits_(0), num_draws_(0) {}
3587 void BeginTest() override {
3588 ASSERT_TRUE(invalidate_layer_)
3589 << "Derived tests must set this in SetupTree";
3591 // One initial commit.
3592 PostSetNeedsCommitToMainThread();
3595 void DidCommitAndDrawFrame() override {
3596 // After commit, invalidate the layer. This should cause a commit.
3597 if (layer_tree_host()->source_frame_number() == 1)
3598 invalidate_layer_->SetNeedsDisplay();
3601 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
3602 num_draws_++;
3603 if (impl->active_tree()->source_frame_number() == 1)
3604 EndTest();
3607 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
3608 num_commits_++;
3611 void AfterTest() override {
3612 EXPECT_GE(2, num_commits_);
3613 EXPECT_GE(2, num_draws_);
3616 protected:
3617 scoped_refptr<Layer> invalidate_layer_;
3619 private:
3620 int num_commits_;
3621 int num_draws_;
3624 // VideoLayer must support being invalidated and then passing that along
3625 // to the compositor thread, even though no resources are updated in
3626 // response to that invalidation.
3627 class LayerTreeHostTestVideoLayerInvalidate : public LayerInvalidateCausesDraw {
3628 public:
3629 void SetupTree() override {
3630 LayerTreeHostTest::SetupTree();
3631 scoped_refptr<VideoLayer> video_layer = VideoLayer::Create(
3632 layer_settings(), &provider_, media::VIDEO_ROTATION_0);
3633 video_layer->SetBounds(gfx::Size(10, 10));
3634 video_layer->SetIsDrawable(true);
3635 layer_tree_host()->root_layer()->AddChild(video_layer);
3637 invalidate_layer_ = video_layer;
3640 private:
3641 FakeVideoFrameProvider provider_;
3644 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestVideoLayerInvalidate);
3646 // IOSurfaceLayer must support being invalidated and then passing that along
3647 // to the compositor thread, even though no resources are updated in
3648 // response to that invalidation.
3649 class LayerTreeHostTestIOSurfaceLayerInvalidate
3650 : public LayerInvalidateCausesDraw {
3651 public:
3652 void SetupTree() override {
3653 LayerTreeHostTest::SetupTree();
3654 scoped_refptr<IOSurfaceLayer> layer =
3655 IOSurfaceLayer::Create(layer_settings());
3656 layer->SetBounds(gfx::Size(10, 10));
3657 uint32_t fake_io_surface_id = 7;
3658 layer->SetIOSurfaceProperties(fake_io_surface_id, layer->bounds());
3659 layer->SetIsDrawable(true);
3660 layer_tree_host()->root_layer()->AddChild(layer);
3662 invalidate_layer_ = layer;
3666 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestIOSurfaceLayerInvalidate);
3668 class LayerTreeHostTestPushHiddenLayer : public LayerTreeHostTest {
3669 protected:
3670 void SetupTree() override {
3671 root_layer_ = Layer::Create(layer_settings());
3672 root_layer_->SetPosition(gfx::Point());
3673 root_layer_->SetBounds(gfx::Size(10, 10));
3675 parent_layer_ = SolidColorLayer::Create(layer_settings());
3676 parent_layer_->SetPosition(gfx::Point());
3677 parent_layer_->SetBounds(gfx::Size(10, 10));
3678 parent_layer_->SetIsDrawable(true);
3679 root_layer_->AddChild(parent_layer_);
3681 child_layer_ = SolidColorLayer::Create(layer_settings());
3682 child_layer_->SetPosition(gfx::Point());
3683 child_layer_->SetBounds(gfx::Size(10, 10));
3684 child_layer_->SetIsDrawable(true);
3685 parent_layer_->AddChild(child_layer_);
3687 layer_tree_host()->SetRootLayer(root_layer_);
3688 LayerTreeHostTest::SetupTree();
3691 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
3693 void DidCommitAndDrawFrame() override {
3694 switch (layer_tree_host()->source_frame_number()) {
3695 case 1:
3696 // The layer type used does not need to push properties every frame.
3697 EXPECT_FALSE(child_layer_->needs_push_properties());
3699 // Change the bounds of the child layer, but make it skipped
3700 // by CalculateDrawProperties.
3701 parent_layer_->SetOpacity(0.f);
3702 child_layer_->SetBounds(gfx::Size(5, 5));
3703 break;
3704 case 2:
3705 // The bounds of the child layer were pushed to the impl side.
3706 EXPECT_FALSE(child_layer_->needs_push_properties());
3708 EndTest();
3709 break;
3713 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
3714 LayerImpl* root = impl->active_tree()->root_layer();
3715 LayerImpl* parent = root->children()[0];
3716 LayerImpl* child = parent->children()[0];
3718 switch (impl->active_tree()->source_frame_number()) {
3719 case 1:
3720 EXPECT_EQ(gfx::Size(5, 5).ToString(), child->bounds().ToString());
3721 break;
3725 void AfterTest() override {}
3727 scoped_refptr<Layer> root_layer_;
3728 scoped_refptr<SolidColorLayer> parent_layer_;
3729 scoped_refptr<SolidColorLayer> child_layer_;
3732 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestPushHiddenLayer);
3734 class LayerTreeHostTestUpdateLayerInEmptyViewport : public LayerTreeHostTest {
3735 protected:
3736 void SetupTree() override {
3737 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
3738 root_layer_->SetBounds(gfx::Size(10, 10));
3740 layer_tree_host()->SetRootLayer(root_layer_);
3741 LayerTreeHostTest::SetupTree();
3744 void BeginTest() override {
3745 // The viewport is empty, but we still need to update layers on the main
3746 // thread.
3747 layer_tree_host()->SetViewportSize(gfx::Size(0, 0));
3748 PostSetNeedsCommitToMainThread();
3751 void DidCommit() override {
3752 // The layer should be updated even though the viewport is empty, so we
3753 // are capable of drawing it on the impl tree.
3754 EXPECT_GT(root_layer_->update_count(), 0);
3755 EndTest();
3758 void AfterTest() override {}
3760 FakeContentLayerClient client_;
3761 scoped_refptr<FakePictureLayer> root_layer_;
3764 MULTI_THREAD_TEST_F(LayerTreeHostTestUpdateLayerInEmptyViewport);
3766 class LayerTreeHostTestSetMemoryPolicyOnLostOutputSurface
3767 : public LayerTreeHostTest {
3768 protected:
3769 LayerTreeHostTestSetMemoryPolicyOnLostOutputSurface()
3770 : first_output_surface_memory_limit_(4321234),
3771 second_output_surface_memory_limit_(1234321) {}
3773 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
3774 if (!first_context_provider_.get()) {
3775 first_context_provider_ = TestContextProvider::Create();
3776 } else {
3777 EXPECT_FALSE(second_context_provider_.get());
3778 second_context_provider_ = TestContextProvider::Create();
3781 scoped_refptr<TestContextProvider> provider(second_context_provider_.get()
3782 ? second_context_provider_
3783 : first_context_provider_);
3784 scoped_ptr<FakeOutputSurface> output_surface;
3785 if (delegating_renderer())
3786 output_surface = FakeOutputSurface::CreateDelegating3d(provider);
3787 else
3788 output_surface = FakeOutputSurface::Create3d(provider);
3789 output_surface->SetMemoryPolicyToSetAtBind(
3790 make_scoped_ptr(new ManagedMemoryPolicy(
3791 second_context_provider_.get() ? second_output_surface_memory_limit_
3792 : first_output_surface_memory_limit_,
3793 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
3794 ManagedMemoryPolicy::kDefaultNumResourcesLimit)));
3795 return output_surface.Pass();
3798 void SetupTree() override {
3799 root_ = FakePictureLayer::Create(layer_settings(), &client_);
3800 root_->SetBounds(gfx::Size(20, 20));
3801 layer_tree_host()->SetRootLayer(root_);
3802 LayerTreeHostTest::SetupTree();
3805 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
3807 void DidCommitAndDrawFrame() override {
3808 // Lost context sometimes takes two frames to recreate. The third frame
3809 // is sometimes aborted, so wait until the fourth frame to verify that
3810 // the memory has been set, and the fifth frame to end the test.
3811 if (layer_tree_host()->source_frame_number() < 5) {
3812 layer_tree_host()->SetNeedsCommit();
3813 } else if (layer_tree_host()->source_frame_number() == 5) {
3814 EndTest();
3818 void SwapBuffersOnThread(LayerTreeHostImpl* impl, bool result) override {
3819 switch (impl->active_tree()->source_frame_number()) {
3820 case 1:
3821 EXPECT_EQ(first_output_surface_memory_limit_,
3822 impl->memory_allocation_limit_bytes());
3823 // Lose the output surface.
3824 first_context_provider_->TestContext3d()->loseContextCHROMIUM(
3825 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
3826 break;
3827 case 4:
3828 EXPECT_EQ(second_output_surface_memory_limit_,
3829 impl->memory_allocation_limit_bytes());
3830 break;
3834 void AfterTest() override {}
3836 scoped_refptr<TestContextProvider> first_context_provider_;
3837 scoped_refptr<TestContextProvider> second_context_provider_;
3838 size_t first_output_surface_memory_limit_;
3839 size_t second_output_surface_memory_limit_;
3840 FakeContentLayerClient client_;
3841 scoped_refptr<Layer> root_;
3844 SINGLE_AND_MULTI_THREAD_TEST_F(
3845 LayerTreeHostTestSetMemoryPolicyOnLostOutputSurface);
3847 struct TestSwapPromiseResult {
3848 TestSwapPromiseResult()
3849 : did_activate_called(false),
3850 did_swap_called(false),
3851 did_not_swap_called(false),
3852 dtor_called(false),
3853 reason(SwapPromise::COMMIT_FAILS) {}
3855 bool did_activate_called;
3856 bool did_swap_called;
3857 bool did_not_swap_called;
3858 bool dtor_called;
3859 SwapPromise::DidNotSwapReason reason;
3860 base::Lock lock;
3863 class TestSwapPromise : public SwapPromise {
3864 public:
3865 explicit TestSwapPromise(TestSwapPromiseResult* result) : result_(result) {}
3867 ~TestSwapPromise() override {
3868 base::AutoLock lock(result_->lock);
3869 result_->dtor_called = true;
3872 void DidActivate() override {
3873 base::AutoLock lock(result_->lock);
3874 EXPECT_FALSE(result_->did_activate_called);
3875 EXPECT_FALSE(result_->did_swap_called);
3876 EXPECT_FALSE(result_->did_not_swap_called);
3877 result_->did_activate_called = true;
3880 void DidSwap(CompositorFrameMetadata* metadata) override {
3881 base::AutoLock lock(result_->lock);
3882 EXPECT_FALSE(result_->did_swap_called);
3883 EXPECT_FALSE(result_->did_not_swap_called);
3884 result_->did_swap_called = true;
3887 void DidNotSwap(DidNotSwapReason reason) override {
3888 base::AutoLock lock(result_->lock);
3889 EXPECT_FALSE(result_->did_swap_called);
3890 EXPECT_FALSE(result_->did_not_swap_called);
3891 EXPECT_FALSE(result_->did_activate_called &&
3892 reason != DidNotSwapReason::SWAP_FAILS);
3893 result_->did_not_swap_called = true;
3894 result_->reason = reason;
3897 int64 TraceId() const override { return 0; }
3899 private:
3900 // Not owned.
3901 TestSwapPromiseResult* result_;
3904 class PinnedLayerTreeSwapPromise : public LayerTreeHostTest {
3905 protected:
3906 void BeginTest() override {
3907 PostSetNextCommitForcesRedrawToMainThread();
3908 PostSetNeedsCommitToMainThread();
3911 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
3912 int frame = host_impl->active_tree()->source_frame_number();
3913 if (frame == -1) {
3914 host_impl->active_tree()->QueuePinnedSwapPromise(make_scoped_ptr(
3915 new TestSwapPromise(&pinned_active_swap_promise_result_)));
3916 host_impl->pending_tree()->QueueSwapPromise(
3917 make_scoped_ptr(new TestSwapPromise(&pending_swap_promise_result_)));
3918 host_impl->active_tree()->QueueSwapPromise(
3919 make_scoped_ptr(new TestSwapPromise(&active_swap_promise_result_)));
3923 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
3924 EndTest();
3927 void AfterTest() override {
3928 // The pending swap promise should activate and swap.
3929 EXPECT_TRUE(pending_swap_promise_result_.did_activate_called);
3930 EXPECT_TRUE(pending_swap_promise_result_.did_swap_called);
3932 // The active swap promise should fail to swap (it is cancelled by
3933 // the activation of a new frame).
3934 EXPECT_FALSE(active_swap_promise_result_.did_activate_called);
3935 EXPECT_FALSE(active_swap_promise_result_.did_swap_called);
3936 EXPECT_TRUE(active_swap_promise_result_.did_not_swap_called);
3937 EXPECT_EQ(active_swap_promise_result_.reason, SwapPromise::SWAP_FAILS);
3939 // The pinned active swap promise should not activate, but should swap.
3940 EXPECT_FALSE(pinned_active_swap_promise_result_.did_activate_called);
3941 EXPECT_TRUE(pinned_active_swap_promise_result_.did_swap_called);
3944 TestSwapPromiseResult pending_swap_promise_result_;
3945 TestSwapPromiseResult active_swap_promise_result_;
3946 TestSwapPromiseResult pinned_active_swap_promise_result_;
3949 MULTI_THREAD_TEST_F(PinnedLayerTreeSwapPromise);
3951 class LayerTreeHostTestBreakSwapPromise : public LayerTreeHostTest {
3952 protected:
3953 LayerTreeHostTestBreakSwapPromise()
3954 : commit_count_(0), commit_complete_count_(0) {}
3956 void WillBeginMainFrame() override {
3957 ASSERT_LE(commit_count_, 2);
3958 scoped_ptr<SwapPromise> swap_promise(
3959 new TestSwapPromise(&swap_promise_result_[commit_count_]));
3960 layer_tree_host()->QueueSwapPromise(swap_promise.Pass());
3963 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
3965 void DidCommit() override {
3966 commit_count_++;
3967 if (commit_count_ == 2) {
3968 // This commit will finish.
3969 layer_tree_host()->SetNeedsCommit();
3973 void WillActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
3974 if (host_impl->pending_tree()) {
3975 int frame = host_impl->pending_tree()->source_frame_number();
3976 base::AutoLock lock(swap_promise_result_[frame].lock);
3977 EXPECT_FALSE(swap_promise_result_[frame].did_activate_called);
3978 EXPECT_FALSE(swap_promise_result_[frame].did_swap_called);
3982 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
3983 int frame = host_impl->active_tree()->source_frame_number();
3984 base::AutoLock lock(swap_promise_result_[frame].lock);
3985 EXPECT_TRUE(swap_promise_result_[frame].did_activate_called);
3986 EXPECT_FALSE(swap_promise_result_[frame].did_swap_called);
3989 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
3990 commit_complete_count_++;
3991 if (commit_complete_count_ == 1) {
3992 // This commit will be aborted because no actual update.
3993 PostSetNeedsUpdateLayersToMainThread();
3997 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
3998 int frame = host_impl->active_tree()->source_frame_number();
3999 if (frame == 2) {
4000 EndTest();
4004 void AfterTest() override {
4005 // 3 commits are scheduled. 2 completes. 1 is aborted.
4006 EXPECT_EQ(commit_count_, 3);
4007 EXPECT_EQ(commit_complete_count_, 2);
4010 // The first commit completes and causes swap buffer which finishes
4011 // the promise.
4012 base::AutoLock lock(swap_promise_result_[0].lock);
4013 EXPECT_TRUE(swap_promise_result_[0].did_swap_called);
4014 EXPECT_FALSE(swap_promise_result_[0].did_not_swap_called);
4015 EXPECT_TRUE(swap_promise_result_[0].dtor_called);
4019 // The second commit is aborted since it contains no updates.
4020 base::AutoLock lock(swap_promise_result_[1].lock);
4021 EXPECT_FALSE(swap_promise_result_[1].did_activate_called);
4022 EXPECT_FALSE(swap_promise_result_[1].did_swap_called);
4023 EXPECT_TRUE(swap_promise_result_[1].did_not_swap_called);
4024 EXPECT_EQ(SwapPromise::COMMIT_NO_UPDATE, swap_promise_result_[1].reason);
4025 EXPECT_TRUE(swap_promise_result_[1].dtor_called);
4029 // The last commit completes but it does not cause swap buffer because
4030 // there is no damage in the frame data.
4031 base::AutoLock lock(swap_promise_result_[2].lock);
4032 EXPECT_TRUE(swap_promise_result_[2].did_activate_called);
4033 EXPECT_FALSE(swap_promise_result_[2].did_swap_called);
4034 EXPECT_TRUE(swap_promise_result_[2].did_not_swap_called);
4035 EXPECT_EQ(SwapPromise::SWAP_FAILS, swap_promise_result_[2].reason);
4036 EXPECT_TRUE(swap_promise_result_[2].dtor_called);
4040 int commit_count_;
4041 int commit_complete_count_;
4042 TestSwapPromiseResult swap_promise_result_[3];
4045 MULTI_THREAD_TEST_F(LayerTreeHostTestBreakSwapPromise);
4047 class LayerTreeHostTestKeepSwapPromise : public LayerTreeTest {
4048 public:
4049 LayerTreeHostTestKeepSwapPromise() {}
4051 void BeginTest() override {
4052 layer_ = SolidColorLayer::Create(layer_settings());
4053 layer_->SetIsDrawable(true);
4054 layer_->SetBounds(gfx::Size(10, 10));
4055 layer_tree_host()->SetRootLayer(layer_);
4056 gfx::Size bounds(100, 100);
4057 layer_tree_host()->SetViewportSize(bounds);
4058 PostSetNeedsCommitToMainThread();
4061 void DidCommit() override {
4062 MainThreadTaskRunner()->PostTask(
4063 FROM_HERE, base::Bind(&LayerTreeHostTestKeepSwapPromise::ChangeFrame,
4064 base::Unretained(this)));
4067 void ChangeFrame() {
4068 switch (layer_tree_host()->source_frame_number()) {
4069 case 1:
4070 layer_->SetBounds(gfx::Size(10, 11));
4071 layer_tree_host()->QueueSwapPromise(
4072 make_scoped_ptr(new TestSwapPromise(&swap_promise_result_)));
4073 break;
4074 case 2:
4075 break;
4076 default:
4077 NOTREACHED();
4078 break;
4082 void WillActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4083 if (host_impl->pending_tree()) {
4084 if (host_impl->pending_tree()->source_frame_number() == 1) {
4085 base::AutoLock lock(swap_promise_result_.lock);
4086 EXPECT_FALSE(swap_promise_result_.did_activate_called);
4087 EXPECT_FALSE(swap_promise_result_.did_swap_called);
4088 SetCallback(true);
4089 } else {
4090 SetCallback(false);
4095 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4096 if (host_impl->active_tree()->source_frame_number() == 1) {
4097 base::AutoLock lock(swap_promise_result_.lock);
4098 EXPECT_TRUE(swap_promise_result_.did_activate_called);
4099 EXPECT_FALSE(swap_promise_result_.did_swap_called);
4103 void ActivationCallback() {
4104 // DidActivate needs to happen before the tree activation callback.
4105 base::AutoLock lock(swap_promise_result_.lock);
4106 EXPECT_TRUE(swap_promise_result_.did_activate_called);
4109 void SetCallback(bool enable) {
4110 output_surface()->SetTreeActivationCallback(
4111 enable
4112 ? base::Bind(&LayerTreeHostTestKeepSwapPromise::ActivationCallback,
4113 base::Unretained(this))
4114 : base::Closure());
4117 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
4118 EXPECT_TRUE(result);
4119 if (host_impl->active_tree()->source_frame_number() >= 1) {
4120 // The commit changes layers so it should cause a swap.
4121 base::AutoLock lock(swap_promise_result_.lock);
4122 EXPECT_TRUE(swap_promise_result_.did_swap_called);
4123 EXPECT_FALSE(swap_promise_result_.did_not_swap_called);
4124 EXPECT_TRUE(swap_promise_result_.dtor_called);
4125 EndTest();
4129 void AfterTest() override {}
4131 private:
4132 scoped_refptr<Layer> layer_;
4133 TestSwapPromiseResult swap_promise_result_;
4136 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestKeepSwapPromise);
4138 class LayerTreeHostTestBreakSwapPromiseForVisibility
4139 : public LayerTreeHostTest {
4140 protected:
4141 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
4143 void SetVisibleFalseAndQueueSwapPromise() {
4144 layer_tree_host()->SetVisible(false);
4145 scoped_ptr<SwapPromise> swap_promise(
4146 new TestSwapPromise(&swap_promise_result_));
4147 layer_tree_host()->QueueSwapPromise(swap_promise.Pass());
4150 void ScheduledActionWillSendBeginMainFrame() override {
4151 MainThreadTaskRunner()->PostTask(
4152 FROM_HERE,
4153 base::Bind(&LayerTreeHostTestBreakSwapPromiseForVisibility
4154 ::SetVisibleFalseAndQueueSwapPromise,
4155 base::Unretained(this)));
4158 void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl,
4159 CommitEarlyOutReason reason) override {
4160 EndTest();
4163 void AfterTest() override {
4165 base::AutoLock lock(swap_promise_result_.lock);
4166 EXPECT_FALSE(swap_promise_result_.did_activate_called);
4167 EXPECT_FALSE(swap_promise_result_.did_swap_called);
4168 EXPECT_TRUE(swap_promise_result_.did_not_swap_called);
4169 EXPECT_EQ(SwapPromise::COMMIT_FAILS, swap_promise_result_.reason);
4170 EXPECT_TRUE(swap_promise_result_.dtor_called);
4174 TestSwapPromiseResult swap_promise_result_;
4177 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestBreakSwapPromiseForVisibility);
4179 class LayerTreeHostTestBreakSwapPromiseForContext : public LayerTreeHostTest {
4180 protected:
4181 LayerTreeHostTestBreakSwapPromiseForContext()
4182 : output_surface_lost_triggered_(false) {
4185 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
4187 void LoseOutputSurfaceAndQueueSwapPromise() {
4188 layer_tree_host()->DidLoseOutputSurface();
4189 scoped_ptr<SwapPromise> swap_promise(
4190 new TestSwapPromise(&swap_promise_result_));
4191 layer_tree_host()->QueueSwapPromise(swap_promise.Pass());
4194 void ScheduledActionWillSendBeginMainFrame() override {
4195 if (output_surface_lost_triggered_)
4196 return;
4197 output_surface_lost_triggered_ = true;
4199 MainThreadTaskRunner()->PostTask(
4200 FROM_HERE,
4201 base::Bind(&LayerTreeHostTestBreakSwapPromiseForContext
4202 ::LoseOutputSurfaceAndQueueSwapPromise,
4203 base::Unretained(this)));
4206 void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl,
4207 CommitEarlyOutReason reason) override {
4208 // This is needed so that the impl-thread state matches main-thread state.
4209 host_impl->DidLoseOutputSurface();
4210 EndTest();
4213 void AfterTest() override {
4215 base::AutoLock lock(swap_promise_result_.lock);
4216 EXPECT_FALSE(swap_promise_result_.did_activate_called);
4217 EXPECT_FALSE(swap_promise_result_.did_swap_called);
4218 EXPECT_TRUE(swap_promise_result_.did_not_swap_called);
4219 EXPECT_EQ(SwapPromise::COMMIT_FAILS, swap_promise_result_.reason);
4220 EXPECT_TRUE(swap_promise_result_.dtor_called);
4224 bool output_surface_lost_triggered_;
4225 TestSwapPromiseResult swap_promise_result_;
4228 SINGLE_AND_MULTI_THREAD_TEST_F(
4229 LayerTreeHostTestBreakSwapPromiseForContext);
4231 class SimpleSwapPromiseMonitor : public SwapPromiseMonitor {
4232 public:
4233 SimpleSwapPromiseMonitor(LayerTreeHost* layer_tree_host,
4234 LayerTreeHostImpl* layer_tree_host_impl,
4235 int* set_needs_commit_count,
4236 int* set_needs_redraw_count)
4237 : SwapPromiseMonitor(layer_tree_host, layer_tree_host_impl),
4238 set_needs_commit_count_(set_needs_commit_count) {}
4240 ~SimpleSwapPromiseMonitor() override {}
4242 void OnSetNeedsCommitOnMain() override { (*set_needs_commit_count_)++; }
4244 void OnSetNeedsRedrawOnImpl() override {
4245 ADD_FAILURE() << "Should not get called on main thread.";
4248 void OnForwardScrollUpdateToMainThreadOnImpl() override {
4249 ADD_FAILURE() << "Should not get called on main thread.";
4252 private:
4253 int* set_needs_commit_count_;
4256 class LayerTreeHostTestSimpleSwapPromiseMonitor : public LayerTreeHostTest {
4257 public:
4258 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
4260 void WillBeginMainFrame() override {
4261 if (TestEnded())
4262 return;
4264 int set_needs_commit_count = 0;
4265 int set_needs_redraw_count = 0;
4268 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
4269 new SimpleSwapPromiseMonitor(layer_tree_host(),
4270 NULL,
4271 &set_needs_commit_count,
4272 &set_needs_redraw_count));
4273 layer_tree_host()->SetNeedsCommit();
4274 EXPECT_EQ(1, set_needs_commit_count);
4275 EXPECT_EQ(0, set_needs_redraw_count);
4278 // Now the monitor is destroyed, SetNeedsCommit() is no longer being
4279 // monitored.
4280 layer_tree_host()->SetNeedsCommit();
4281 EXPECT_EQ(1, set_needs_commit_count);
4282 EXPECT_EQ(0, set_needs_redraw_count);
4285 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
4286 new SimpleSwapPromiseMonitor(layer_tree_host(),
4287 NULL,
4288 &set_needs_commit_count,
4289 &set_needs_redraw_count));
4290 layer_tree_host()->SetNeedsUpdateLayers();
4291 EXPECT_EQ(2, set_needs_commit_count);
4292 EXPECT_EQ(0, set_needs_redraw_count);
4296 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
4297 new SimpleSwapPromiseMonitor(layer_tree_host(),
4298 NULL,
4299 &set_needs_commit_count,
4300 &set_needs_redraw_count));
4301 layer_tree_host()->SetNeedsAnimate();
4302 EXPECT_EQ(3, set_needs_commit_count);
4303 EXPECT_EQ(0, set_needs_redraw_count);
4306 EndTest();
4309 void AfterTest() override {}
4312 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSimpleSwapPromiseMonitor);
4314 class LayerTreeHostTestHighResRequiredAfterEvictingUIResources
4315 : public LayerTreeHostTest {
4316 protected:
4317 void SetupTree() override {
4318 LayerTreeHostTest::SetupTree();
4319 ui_resource_ = FakeScopedUIResource::Create(layer_tree_host());
4322 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
4324 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4325 host_impl->EvictAllUIResources();
4326 // Existence of evicted UI resources will trigger NEW_CONTENT_TAKES_PRIORITY
4327 // mode. Active tree should require high-res to draw after entering this
4328 // mode to ensure that high-res tiles are also required for a pending tree
4329 // to be activated.
4330 EXPECT_TRUE(host_impl->RequiresHighResToDraw());
4333 void DidCommit() override {
4334 int frame = layer_tree_host()->source_frame_number();
4335 switch (frame) {
4336 case 1:
4337 PostSetNeedsCommitToMainThread();
4338 break;
4339 case 2:
4340 ui_resource_ = nullptr;
4341 EndTest();
4342 break;
4346 void AfterTest() override {}
4348 FakeContentLayerClient client_;
4349 scoped_ptr<FakeScopedUIResource> ui_resource_;
4352 // This test is flaky, see http://crbug.com/386199
4353 // MULTI_THREAD_TEST_F(LayerTreeHostTestHighResRequiredAfterEvictingUIResources)
4355 class LayerTreeHostTestGpuRasterizationDefault : public LayerTreeHostTest {
4356 protected:
4357 void InitializeSettings(LayerTreeSettings* settings) override {
4358 EXPECT_FALSE(settings->gpu_rasterization_enabled);
4359 EXPECT_FALSE(settings->gpu_rasterization_forced);
4362 void SetupTree() override {
4363 LayerTreeHostTest::SetupTree();
4365 scoped_refptr<PictureLayer> layer =
4366 PictureLayer::Create(layer_settings(), &layer_client_);
4367 layer->SetBounds(gfx::Size(10, 10));
4368 layer->SetIsDrawable(true);
4369 layer_tree_host()->root_layer()->AddChild(layer);
4372 void BeginTest() override {
4373 Layer* root = layer_tree_host()->root_layer();
4374 PictureLayer* layer = static_cast<PictureLayer*>(root->child_at(0));
4375 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4377 // Verify default values.
4378 EXPECT_TRUE(root->IsSuitableForGpuRasterization());
4379 EXPECT_TRUE(layer->IsSuitableForGpuRasterization());
4380 EXPECT_TRUE(recording_source->IsSuitableForGpuRasterization());
4381 EXPECT_FALSE(layer_tree_host()->has_gpu_rasterization_trigger());
4383 // Setting gpu rasterization trigger does not enable gpu rasterization.
4384 layer_tree_host()->SetHasGpuRasterizationTrigger(true);
4385 EXPECT_TRUE(layer_tree_host()->has_gpu_rasterization_trigger());
4387 PostSetNeedsCommitToMainThread();
4390 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
4391 EXPECT_FALSE(host_impl->pending_tree()->use_gpu_rasterization());
4392 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4395 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4396 EXPECT_FALSE(host_impl->active_tree()->use_gpu_rasterization());
4397 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4398 EndTest();
4401 void AfterTest() override {}
4403 FakeContentLayerClient layer_client_;
4406 MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterizationDefault);
4408 class LayerTreeHostTestGpuRasterizationEnabled : public LayerTreeHostTest {
4409 protected:
4410 void InitializeSettings(LayerTreeSettings* settings) override {
4411 EXPECT_FALSE(settings->gpu_rasterization_enabled);
4412 settings->gpu_rasterization_enabled = true;
4415 void SetupTree() override {
4416 LayerTreeHostTest::SetupTree();
4418 scoped_refptr<PictureLayer> layer =
4419 PictureLayer::Create(layer_settings(), &layer_client_);
4420 layer->SetBounds(gfx::Size(10, 10));
4421 layer->SetIsDrawable(true);
4422 layer_tree_host()->root_layer()->AddChild(layer);
4425 void BeginTest() override {
4426 Layer* root = layer_tree_host()->root_layer();
4427 PictureLayer* layer = static_cast<PictureLayer*>(root->child_at(0));
4428 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4430 // Verify default values.
4431 EXPECT_TRUE(root->IsSuitableForGpuRasterization());
4432 EXPECT_TRUE(layer->IsSuitableForGpuRasterization());
4433 EXPECT_TRUE(recording_source->IsSuitableForGpuRasterization());
4434 EXPECT_FALSE(layer_tree_host()->has_gpu_rasterization_trigger());
4436 // Gpu rasterization trigger is relevant.
4437 layer_tree_host()->SetHasGpuRasterizationTrigger(true);
4438 EXPECT_TRUE(layer_tree_host()->has_gpu_rasterization_trigger());
4440 // Content-based veto is relevant as well.
4441 recording_source->SetUnsuitableForGpuRasterizationForTesting();
4442 EXPECT_FALSE(recording_source->IsSuitableForGpuRasterization());
4443 EXPECT_FALSE(layer->IsSuitableForGpuRasterization());
4444 // Veto will take effect when layers are updated.
4445 // The results will be verified after commit is completed below.
4446 // Since we are manually marking picture pile as unsuitable,
4447 // make sure that the layer gets a chance to update.
4448 layer->SetNeedsDisplay();
4449 PostSetNeedsCommitToMainThread();
4452 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
4453 EXPECT_FALSE(host_impl->pending_tree()->use_gpu_rasterization());
4454 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4457 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4458 EXPECT_FALSE(host_impl->active_tree()->use_gpu_rasterization());
4459 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4460 EndTest();
4463 void AfterTest() override {}
4465 FakeContentLayerClient layer_client_;
4468 MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterizationEnabled);
4470 class LayerTreeHostTestGpuRasterizationForced : public LayerTreeHostTest {
4471 protected:
4472 void InitializeSettings(LayerTreeSettings* settings) override {
4473 EXPECT_FALSE(settings->gpu_rasterization_forced);
4474 settings->gpu_rasterization_forced = true;
4477 void SetupTree() override {
4478 LayerTreeHostTest::SetupTree();
4480 scoped_refptr<FakePictureLayer> layer =
4481 FakePictureLayer::Create(layer_settings(), &layer_client_);
4482 layer->SetBounds(gfx::Size(10, 10));
4483 layer->SetIsDrawable(true);
4484 layer_tree_host()->root_layer()->AddChild(layer);
4487 void BeginTest() override {
4488 Layer* root = layer_tree_host()->root_layer();
4489 PictureLayer* layer = static_cast<PictureLayer*>(root->child_at(0));
4490 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4492 // Verify default values.
4493 EXPECT_TRUE(root->IsSuitableForGpuRasterization());
4494 EXPECT_TRUE(layer->IsSuitableForGpuRasterization());
4495 EXPECT_TRUE(recording_source->IsSuitableForGpuRasterization());
4496 EXPECT_FALSE(layer_tree_host()->has_gpu_rasterization_trigger());
4498 // With gpu rasterization forced, gpu rasterization trigger is irrelevant.
4499 layer_tree_host()->SetHasGpuRasterizationTrigger(true);
4500 EXPECT_TRUE(layer_tree_host()->has_gpu_rasterization_trigger());
4502 // Content-based veto is irrelevant as well.
4503 recording_source->SetUnsuitableForGpuRasterizationForTesting();
4504 EXPECT_FALSE(recording_source->IsSuitableForGpuRasterization());
4505 EXPECT_FALSE(layer->IsSuitableForGpuRasterization());
4506 // Veto will take effect when layers are updated.
4507 // The results will be verified after commit is completed below.
4508 // Since we are manually marking picture pile as unsuitable,
4509 // make sure that the layer gets a chance to update.
4510 layer->SetNeedsDisplay();
4511 PostSetNeedsCommitToMainThread();
4514 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
4515 EXPECT_TRUE(host_impl->sync_tree()->use_gpu_rasterization());
4516 EXPECT_TRUE(host_impl->use_gpu_rasterization());
4519 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4520 EXPECT_TRUE(host_impl->active_tree()->use_gpu_rasterization());
4521 EXPECT_TRUE(host_impl->use_gpu_rasterization());
4522 EndTest();
4525 void AfterTest() override {}
4527 FakeContentLayerClient layer_client_;
4530 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterizationForced);
4532 class LayerTreeHostTestContinuousPainting : public LayerTreeHostTest {
4533 public:
4534 LayerTreeHostTestContinuousPainting()
4535 : num_commits_(0), num_draws_(0), bounds_(20, 20), child_layer_(NULL) {}
4537 protected:
4538 enum { kExpectedNumCommits = 10 };
4540 void SetupTree() override {
4541 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings());
4542 root_layer->SetBounds(bounds_);
4544 child_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
4545 child_layer_->SetBounds(bounds_);
4546 child_layer_->SetIsDrawable(true);
4547 root_layer->AddChild(child_layer_);
4549 layer_tree_host()->SetRootLayer(root_layer);
4550 layer_tree_host()->SetViewportSize(bounds_);
4551 LayerTreeHostTest::SetupTree();
4554 void BeginTest() override {
4555 MainThreadTaskRunner()->PostTask(
4556 FROM_HERE,
4557 base::Bind(
4558 &LayerTreeHostTestContinuousPainting::EnableContinuousPainting,
4559 base::Unretained(this)));
4560 // Wait 50x longer than expected.
4561 double milliseconds_per_frame =
4562 1000.0 / layer_tree_host()->settings().renderer_settings.refresh_rate;
4563 MainThreadTaskRunner()->PostDelayedTask(
4564 FROM_HERE,
4565 base::Bind(
4566 &LayerTreeHostTestContinuousPainting::DisableContinuousPainting,
4567 base::Unretained(this)),
4568 base::TimeDelta::FromMilliseconds(50 * kExpectedNumCommits *
4569 milliseconds_per_frame));
4572 void BeginMainFrame(const BeginFrameArgs& args) override {
4573 child_layer_->SetNeedsDisplay();
4576 void AfterTest() override {
4577 EXPECT_LE(kExpectedNumCommits, num_commits_);
4578 EXPECT_LE(kExpectedNumCommits, num_draws_);
4579 EXPECT_LE(kExpectedNumCommits, child_layer_->update_count());
4582 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
4583 if (++num_draws_ == kExpectedNumCommits)
4584 EndTest();
4587 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
4588 ++num_commits_;
4591 private:
4592 void EnableContinuousPainting() {
4593 LayerTreeDebugState debug_state = layer_tree_host()->debug_state();
4594 debug_state.continuous_painting = true;
4595 layer_tree_host()->SetDebugState(debug_state);
4598 void DisableContinuousPainting() {
4599 LayerTreeDebugState debug_state = layer_tree_host()->debug_state();
4600 debug_state.continuous_painting = false;
4601 layer_tree_host()->SetDebugState(debug_state);
4602 EndTest();
4605 int num_commits_;
4606 int num_draws_;
4607 const gfx::Size bounds_;
4608 FakeContentLayerClient client_;
4609 scoped_refptr<FakePictureLayer> child_layer_;
4612 MULTI_THREAD_TEST_F(LayerTreeHostTestContinuousPainting);
4614 class LayerTreeHostTestWillBeginImplFrameHasDidFinishImplFrame
4615 : public LayerTreeHostTest {
4616 public:
4617 enum { kExpectedNumImplFrames = 10 };
4619 LayerTreeHostTestWillBeginImplFrameHasDidFinishImplFrame()
4620 : will_begin_impl_frame_count_(0), did_finish_impl_frame_count_(0) {}
4622 void BeginTest() override {
4623 // Kick off the test with a commit.
4624 PostSetNeedsCommitToMainThread();
4627 void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl,
4628 const BeginFrameArgs& args) override {
4629 EXPECT_EQ(will_begin_impl_frame_count_, did_finish_impl_frame_count_);
4630 EXPECT_FALSE(TestEnded());
4631 will_begin_impl_frame_count_++;
4634 void DidFinishImplFrameOnThread(LayerTreeHostImpl* host_impl) override {
4635 did_finish_impl_frame_count_++;
4636 EXPECT_EQ(will_begin_impl_frame_count_, did_finish_impl_frame_count_);
4638 // Request a number of commits to cause multiple impl frames. We expect to
4639 // get one more impl frames than the number of commits requested because
4640 // after a commit it takes one frame to become idle.
4641 if (did_finish_impl_frame_count_ < kExpectedNumImplFrames - 1)
4642 PostSetNeedsCommitToMainThread();
4645 void SendBeginMainFrameNotExpectedSoon() override { EndTest(); }
4647 void AfterTest() override {
4648 EXPECT_GT(will_begin_impl_frame_count_, 0);
4649 EXPECT_GT(did_finish_impl_frame_count_, 0);
4650 EXPECT_EQ(will_begin_impl_frame_count_, did_finish_impl_frame_count_);
4652 // TODO(mithro): Figure out why the multithread version of this test
4653 // sometimes has one more frame then expected. Possibly related to
4654 // http://crbug.com/443185
4655 if (!HasImplThread()) {
4656 EXPECT_EQ(will_begin_impl_frame_count_, kExpectedNumImplFrames);
4657 EXPECT_EQ(did_finish_impl_frame_count_, kExpectedNumImplFrames);
4661 private:
4662 int will_begin_impl_frame_count_;
4663 int did_finish_impl_frame_count_;
4666 SINGLE_AND_MULTI_THREAD_TEST_F(
4667 LayerTreeHostTestWillBeginImplFrameHasDidFinishImplFrame);
4669 class LayerTreeHostTestSendBeginFramesToChildren : public LayerTreeHostTest {
4670 public:
4671 LayerTreeHostTestSendBeginFramesToChildren()
4672 : begin_frame_sent_to_children_(false) {
4675 void BeginTest() override {
4676 // Kick off the test with a commit.
4677 PostSetNeedsCommitToMainThread();
4680 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
4681 begin_frame_sent_to_children_ = true;
4682 EndTest();
4685 void DidBeginMainFrame() override {
4686 // Children requested BeginFrames.
4687 layer_tree_host()->SetChildrenNeedBeginFrames(true);
4690 void AfterTest() override {
4691 // Ensure that BeginFrame message is sent to children during parent
4692 // scheduler handles its BeginFrame.
4693 EXPECT_TRUE(begin_frame_sent_to_children_);
4696 private:
4697 bool begin_frame_sent_to_children_;
4700 SINGLE_THREAD_TEST_F(LayerTreeHostTestSendBeginFramesToChildren);
4702 class LayerTreeHostTestSendBeginFramesToChildrenWithExternalBFS
4703 : public LayerTreeHostTest {
4704 public:
4705 LayerTreeHostTestSendBeginFramesToChildrenWithExternalBFS()
4706 : begin_frame_sent_to_children_(false) {
4709 void InitializeSettings(LayerTreeSettings* settings) override {
4710 settings->use_external_begin_frame_source = true;
4713 void BeginTest() override {
4714 // Kick off the test with a commit.
4715 PostSetNeedsCommitToMainThread();
4718 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
4719 begin_frame_sent_to_children_ = true;
4720 EndTest();
4723 void DidBeginMainFrame() override {
4724 // Children requested BeginFrames.
4725 layer_tree_host()->SetChildrenNeedBeginFrames(true);
4728 void AfterTest() override {
4729 // Ensure that BeginFrame message is sent to children during parent
4730 // scheduler handles its BeginFrame.
4731 EXPECT_TRUE(begin_frame_sent_to_children_);
4734 private:
4735 bool begin_frame_sent_to_children_;
4738 SINGLE_THREAD_TEST_F(LayerTreeHostTestSendBeginFramesToChildrenWithExternalBFS);
4740 class LayerTreeHostTestActivateOnInvisible : public LayerTreeHostTest {
4741 public:
4742 LayerTreeHostTestActivateOnInvisible()
4743 : activation_count_(0), visible_(true) {}
4745 void BeginTest() override {
4746 // Kick off the test with a commit.
4747 PostSetNeedsCommitToMainThread();
4750 void BeginCommitOnThread(LayerTreeHostImpl* host_impl) override {
4751 // Make sure we don't activate using the notify signal from tile manager.
4752 host_impl->BlockNotifyReadyToActivateForTesting(true);
4755 void DidCommit() override { layer_tree_host()->SetVisible(false); }
4757 void DidSetVisibleOnImplTree(LayerTreeHostImpl* host_impl,
4758 bool visible) override {
4759 visible_ = visible;
4761 // Once invisible, we can go visible again.
4762 if (!visible) {
4763 PostSetVisibleToMainThread(true);
4764 } else {
4765 EXPECT_TRUE(host_impl->RequiresHighResToDraw());
4766 EndTest();
4770 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4771 ++activation_count_;
4772 EXPECT_FALSE(visible_);
4775 void AfterTest() override {
4776 // Ensure we activated even though the signal was blocked.
4777 EXPECT_EQ(1, activation_count_);
4778 EXPECT_TRUE(visible_);
4781 private:
4782 int activation_count_;
4783 bool visible_;
4785 FakeContentLayerClient client_;
4786 scoped_refptr<FakePictureLayer> picture_layer_;
4789 // TODO(vmpstr): Enable with single thread impl-side painting.
4790 // This test blocks activation which is not supported for single thread mode.
4791 MULTI_THREAD_BLOCKNOTIFY_TEST_F(LayerTreeHostTestActivateOnInvisible);
4793 // Do a synchronous composite and assert that the swap promise succeeds.
4794 class LayerTreeHostTestSynchronousCompositeSwapPromise
4795 : public LayerTreeHostTest {
4796 public:
4797 LayerTreeHostTestSynchronousCompositeSwapPromise() : commit_count_(0) {}
4799 void InitializeSettings(LayerTreeSettings* settings) override {
4800 settings->single_thread_proxy_scheduler = false;
4801 settings->use_zero_copy = true;
4804 void BeginTest() override {
4805 // Successful composite.
4806 scoped_ptr<SwapPromise> swap_promise0(
4807 new TestSwapPromise(&swap_promise_result_[0]));
4808 layer_tree_host()->QueueSwapPromise(swap_promise0.Pass());
4809 layer_tree_host()->Composite(base::TimeTicks::Now());
4811 // Fail to swap (no damage).
4812 scoped_ptr<SwapPromise> swap_promise1(
4813 new TestSwapPromise(&swap_promise_result_[1]));
4814 layer_tree_host()->QueueSwapPromise(swap_promise1.Pass());
4815 layer_tree_host()->SetNeedsCommit();
4816 layer_tree_host()->Composite(base::TimeTicks::Now());
4818 // Fail to draw (not visible).
4819 scoped_ptr<SwapPromise> swap_promise2(
4820 new TestSwapPromise(&swap_promise_result_[2]));
4821 layer_tree_host()->QueueSwapPromise(swap_promise2.Pass());
4822 layer_tree_host()->SetNeedsDisplayOnAllLayers();
4823 layer_tree_host()->SetVisible(false);
4824 layer_tree_host()->Composite(base::TimeTicks::Now());
4826 EndTest();
4829 void DidCommit() override {
4830 commit_count_++;
4831 ASSERT_LE(commit_count_, 3);
4834 void AfterTest() override {
4835 EXPECT_EQ(3, commit_count_);
4837 // Initial swap promise should have succeded.
4839 base::AutoLock lock(swap_promise_result_[0].lock);
4840 EXPECT_TRUE(swap_promise_result_[0].did_swap_called);
4841 EXPECT_FALSE(swap_promise_result_[0].did_not_swap_called);
4842 EXPECT_TRUE(swap_promise_result_[0].dtor_called);
4845 // Second swap promise fails to swap.
4847 base::AutoLock lock(swap_promise_result_[1].lock);
4848 EXPECT_TRUE(swap_promise_result_[1].did_activate_called);
4849 EXPECT_FALSE(swap_promise_result_[1].did_swap_called);
4850 EXPECT_TRUE(swap_promise_result_[1].did_not_swap_called);
4851 EXPECT_EQ(SwapPromise::SWAP_FAILS, swap_promise_result_[1].reason);
4852 EXPECT_TRUE(swap_promise_result_[1].dtor_called);
4855 // Third swap promises also fails to swap (and draw).
4857 base::AutoLock lock(swap_promise_result_[2].lock);
4858 EXPECT_TRUE(swap_promise_result_[2].did_activate_called);
4859 EXPECT_FALSE(swap_promise_result_[2].did_swap_called);
4860 EXPECT_TRUE(swap_promise_result_[2].did_not_swap_called);
4861 EXPECT_EQ(SwapPromise::SWAP_FAILS, swap_promise_result_[2].reason);
4862 EXPECT_TRUE(swap_promise_result_[2].dtor_called);
4866 int commit_count_;
4867 TestSwapPromiseResult swap_promise_result_[3];
4870 SINGLE_THREAD_TEST_F(LayerTreeHostTestSynchronousCompositeSwapPromise);
4872 // Make sure page scale and top control deltas are applied to the client even
4873 // when the LayerTreeHost doesn't have a root layer.
4874 class LayerTreeHostAcceptsDeltasFromImplWithoutRootLayer
4875 : public LayerTreeHostTest {
4876 public:
4877 LayerTreeHostAcceptsDeltasFromImplWithoutRootLayer()
4878 : deltas_sent_to_client_(false) {}
4880 void BeginTest() override {
4881 layer_tree_host()->SetRootLayer(nullptr);
4882 info_.page_scale_delta = 3.14f;
4883 info_.top_controls_delta = 2.73f;
4885 PostSetNeedsCommitToMainThread();
4888 void BeginMainFrame(const BeginFrameArgs& args) override {
4889 EXPECT_EQ(nullptr, layer_tree_host()->root_layer());
4891 layer_tree_host()->ApplyScrollAndScale(&info_);
4892 EndTest();
4895 void ApplyViewportDeltas(const gfx::Vector2dF& inner,
4896 const gfx::Vector2dF& outer,
4897 const gfx::Vector2dF& elastic_overscroll_delta,
4898 float scale_delta,
4899 float top_controls_delta) override {
4900 EXPECT_EQ(info_.page_scale_delta, scale_delta);
4901 EXPECT_EQ(info_.top_controls_delta, top_controls_delta);
4902 deltas_sent_to_client_ = true;
4905 void AfterTest() override {
4906 EXPECT_TRUE(deltas_sent_to_client_);
4909 ScrollAndScaleSet info_;
4910 bool deltas_sent_to_client_;
4913 MULTI_THREAD_TEST_F(LayerTreeHostAcceptsDeltasFromImplWithoutRootLayer);
4915 class LayerTreeHostTestCrispUpAfterPinchEnds : public LayerTreeHostTest {
4916 protected:
4917 LayerTreeHostTestCrispUpAfterPinchEnds()
4918 : playback_allowed_event_(true, true) {}
4920 void SetupTree() override {
4921 frame_ = 1;
4922 posted_ = false;
4923 client_.set_fill_with_nonsolid_color(true);
4925 scoped_refptr<Layer> root = Layer::Create(layer_settings());
4926 root->SetBounds(gfx::Size(500, 500));
4928 scoped_refptr<Layer> pinch = Layer::Create(layer_settings());
4929 pinch->SetBounds(gfx::Size(500, 500));
4930 pinch->SetScrollClipLayerId(root->id());
4931 pinch->SetIsContainerForFixedPositionLayers(true);
4932 root->AddChild(pinch);
4934 scoped_ptr<FakePicturePile> pile(
4935 new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
4936 LayerTreeSettings().default_tile_grid_size));
4937 pile->SetPlaybackAllowedEvent(&playback_allowed_event_);
4938 scoped_refptr<FakePictureLayer> layer =
4939 FakePictureLayer::CreateWithRecordingSource(layer_settings(), &client_,
4940 pile.Pass());
4941 layer->SetBounds(gfx::Size(500, 500));
4942 layer->SetContentsOpaque(true);
4943 // Avoid LCD text on the layer so we don't cause extra commits when we
4944 // pinch.
4945 pinch->AddChild(layer);
4947 layer_tree_host()->RegisterViewportLayers(NULL, root, pinch, pinch);
4948 layer_tree_host()->SetPageScaleFactorAndLimits(1.f, 1.f, 4.f);
4949 layer_tree_host()->SetRootLayer(root);
4950 LayerTreeHostTest::SetupTree();
4953 // Returns the delta scale of all quads in the frame's root pass from their
4954 // ideal, or 0 if they are not all the same.
4955 float FrameQuadScaleDeltaFromIdeal(LayerTreeHostImpl::FrameData* frame_data) {
4956 if (frame_data->has_no_damage)
4957 return 0.f;
4958 float frame_scale = 0.f;
4959 RenderPass* root_pass = frame_data->render_passes.back();
4960 for (const auto& draw_quad : root_pass->quad_list) {
4961 // Checkerboards mean an incomplete frame.
4962 if (draw_quad->material != DrawQuad::TILED_CONTENT)
4963 return 0.f;
4964 const TileDrawQuad* quad = TileDrawQuad::MaterialCast(draw_quad);
4965 float quad_scale =
4966 quad->tex_coord_rect.width() / static_cast<float>(quad->rect.width());
4967 float transform_scale = SkMScalarToFloat(
4968 quad->shared_quad_state->quad_to_target_transform.matrix().get(0, 0));
4969 float scale = quad_scale / transform_scale;
4970 if (frame_scale != 0.f && frame_scale != scale)
4971 return 0.f;
4972 frame_scale = scale;
4974 return frame_scale;
4977 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
4979 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
4980 LayerTreeHostImpl::FrameData* frame_data,
4981 DrawResult draw_result) override {
4982 float quad_scale_delta = FrameQuadScaleDeltaFromIdeal(frame_data);
4983 switch (frame_) {
4984 case 1:
4985 // Drew at page scale 1 before any pinching.
4986 EXPECT_EQ(1.f, host_impl->active_tree()->current_page_scale_factor());
4987 EXPECT_EQ(1.f, quad_scale_delta);
4988 PostNextAfterDraw(host_impl);
4989 break;
4990 case 2:
4991 if (quad_scale_delta != 1.f)
4992 break;
4993 // Drew at page scale 1.5 after pinching in.
4994 EXPECT_EQ(1.5f, host_impl->active_tree()->current_page_scale_factor());
4995 EXPECT_EQ(1.f, quad_scale_delta);
4996 PostNextAfterDraw(host_impl);
4997 break;
4998 case 3:
4999 // By pinching out, we will create a new tiling and raster it. This may
5000 // cause some additional draws, though we should still be drawing with
5001 // the old 1.5 tiling.
5002 if (frame_data->has_no_damage)
5003 break;
5004 // Drew at page scale 1 with the 1.5 tiling while pinching out.
5005 EXPECT_EQ(1.f, host_impl->active_tree()->current_page_scale_factor());
5006 EXPECT_EQ(1.5f, quad_scale_delta);
5007 // We don't PostNextAfterDraw here, instead we wait for the new tiling
5008 // to finish rastering so we don't get any noise in further steps.
5009 break;
5010 case 4:
5011 // Drew at page scale 1 with the 1.5 tiling after pinching out completed
5012 // while waiting for texture uploads to complete.
5013 EXPECT_EQ(1.f, host_impl->active_tree()->current_page_scale_factor());
5014 // This frame will not have any damage, since it's actually the same as
5015 // the last frame, and should contain no incomplete tiles. We just want
5016 // to make sure we drew here at least once after the pinch ended to be
5017 // sure that drawing after pinch doesn't leave us at the wrong scale
5018 EXPECT_TRUE(frame_data->has_no_damage);
5019 PostNextAfterDraw(host_impl);
5020 break;
5021 case 5:
5022 if (quad_scale_delta != 1.f)
5023 break;
5024 // Drew at scale 1 after texture uploads are done.
5025 EXPECT_EQ(1.f, host_impl->active_tree()->current_page_scale_factor());
5026 EXPECT_EQ(1.f, quad_scale_delta);
5027 EndTest();
5028 break;
5030 return draw_result;
5033 void PostNextAfterDraw(LayerTreeHostImpl* host_impl) {
5034 if (posted_)
5035 return;
5036 posted_ = true;
5037 ImplThreadTaskRunner()->PostDelayedTask(
5038 FROM_HERE, base::Bind(&LayerTreeHostTestCrispUpAfterPinchEnds::Next,
5039 base::Unretained(this), host_impl),
5040 // Use a delay to allow raster/upload to happen in between frames. This
5041 // should cause flakiness if we fail to block raster/upload when
5042 // desired.
5043 base::TimeDelta::FromMilliseconds(16 * 4));
5046 void Next(LayerTreeHostImpl* host_impl) {
5047 ++frame_;
5048 posted_ = false;
5049 switch (frame_) {
5050 case 2:
5051 // Pinch zoom in.
5052 host_impl->PinchGestureBegin();
5053 host_impl->PinchGestureUpdate(1.5f, gfx::Point(100, 100));
5054 host_impl->PinchGestureEnd();
5055 break;
5056 case 3:
5057 // Pinch zoom back to 1.f but don't end it.
5058 host_impl->PinchGestureBegin();
5059 host_impl->PinchGestureUpdate(1.f / 1.5f, gfx::Point(100, 100));
5060 break;
5061 case 4:
5062 // End the pinch, but delay tile production.
5063 playback_allowed_event_.Reset();
5064 host_impl->PinchGestureEnd();
5065 break;
5066 case 5:
5067 // Let tiles complete.
5068 playback_allowed_event_.Signal();
5069 break;
5073 void NotifyTileStateChangedOnThread(LayerTreeHostImpl* host_impl,
5074 const Tile* tile) override {
5075 if (frame_ == 3) {
5076 // On frame 3, we will have a lower res tile complete for the pinch-out
5077 // gesture even though it's not displayed. We wait for it here to prevent
5078 // flakiness.
5079 EXPECT_EQ(0.75f, tile->contents_scale());
5080 PostNextAfterDraw(host_impl);
5082 // On frame_ == 4, we are preventing texture uploads from completing,
5083 // so this verifies they are not completing before frame_ == 5.
5084 // Flaky failures here indicate we're failing to prevent uploads from
5085 // completing.
5086 EXPECT_NE(4, frame_) << tile->contents_scale();
5089 void AfterTest() override {}
5091 FakeContentLayerClient client_;
5092 int frame_;
5093 bool posted_;
5094 base::WaitableEvent playback_allowed_event_;
5097 // This test does pinching on the impl side which is not supported in single
5098 // thread.
5099 MULTI_THREAD_TEST_F(LayerTreeHostTestCrispUpAfterPinchEnds);
5101 class LayerTreeHostTestCrispUpAfterPinchEndsWithOneCopy
5102 : public LayerTreeHostTestCrispUpAfterPinchEnds {
5103 protected:
5104 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
5105 scoped_ptr<TestWebGraphicsContext3D> context3d =
5106 TestWebGraphicsContext3D::Create();
5107 context3d->set_support_image(true);
5108 context3d->set_support_sync_query(true);
5109 #if defined(OS_MACOSX)
5110 context3d->set_support_texture_rectangle(true);
5111 #endif
5113 if (delegating_renderer())
5114 return FakeOutputSurface::CreateDelegating3d(context3d.Pass());
5115 else
5116 return FakeOutputSurface::Create3d(context3d.Pass());
5120 // This test does pinching on the impl side which is not supported in single
5121 // thread.
5122 MULTI_THREAD_TEST_F(LayerTreeHostTestCrispUpAfterPinchEndsWithOneCopy);
5124 class RasterizeWithGpuRasterizationCreatesResources : public LayerTreeHostTest {
5125 protected:
5126 RasterizeWithGpuRasterizationCreatesResources() {}
5128 void InitializeSettings(LayerTreeSettings* settings) override {
5129 settings->gpu_rasterization_forced = true;
5132 void SetupTree() override {
5133 client_.set_fill_with_nonsolid_color(true);
5135 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5136 root->SetBounds(gfx::Size(500, 500));
5138 scoped_ptr<FakePicturePile> pile(
5139 new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
5140 LayerTreeSettings().default_tile_grid_size));
5141 scoped_refptr<FakePictureLayer> layer =
5142 FakePictureLayer::CreateWithRecordingSource(layer_settings(), &client_,
5143 pile.Pass());
5144 layer->SetBounds(gfx::Size(500, 500));
5145 layer->SetContentsOpaque(true);
5146 root->AddChild(layer);
5148 layer_tree_host()->SetRootLayer(root);
5149 LayerTreeHostTest::SetupTree();
5152 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5154 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5155 LayerTreeHostImpl::FrameData* frame_data,
5156 DrawResult draw_result) override {
5157 EXPECT_NE(0u, host_impl->resource_provider()->num_resources());
5158 EndTest();
5159 return draw_result;
5161 void AfterTest() override {}
5163 FakeContentLayerClient client_;
5166 SINGLE_AND_MULTI_THREAD_TEST_F(RasterizeWithGpuRasterizationCreatesResources);
5168 class GpuRasterizationRasterizesBorderTiles : public LayerTreeHostTest {
5169 protected:
5170 GpuRasterizationRasterizesBorderTiles() : viewport_size_(1024, 2048) {}
5172 void InitializeSettings(LayerTreeSettings* settings) override {
5173 settings->gpu_rasterization_enabled = true;
5174 settings->gpu_rasterization_forced = true;
5177 void SetupTree() override {
5178 client_.set_fill_with_nonsolid_color(true);
5180 scoped_ptr<FakePicturePile> pile(
5181 new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
5182 LayerTreeSettings().default_tile_grid_size));
5183 scoped_refptr<FakePictureLayer> root =
5184 FakePictureLayer::CreateWithRecordingSource(layer_settings(), &client_,
5185 pile.Pass());
5186 root->SetBounds(gfx::Size(10000, 10000));
5187 root->SetContentsOpaque(true);
5189 layer_tree_host()->SetRootLayer(root);
5190 LayerTreeHostTest::SetupTree();
5191 layer_tree_host()->SetViewportSize(viewport_size_);
5194 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5196 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5197 LayerTreeHostImpl::FrameData* frame_data,
5198 DrawResult draw_result) override {
5199 EXPECT_EQ(10u, host_impl->resource_provider()->num_resources());
5200 EndTest();
5201 return draw_result;
5204 void AfterTest() override {}
5206 private:
5207 FakeContentLayerClient client_;
5208 gfx::Size viewport_size_;
5211 SINGLE_AND_MULTI_THREAD_TEST_F(GpuRasterizationRasterizesBorderTiles);
5213 class LayerTreeHostTestContinuousDrawWhenCreatingVisibleTiles
5214 : public LayerTreeHostTest {
5215 protected:
5216 LayerTreeHostTestContinuousDrawWhenCreatingVisibleTiles()
5217 : playback_allowed_event_(true, true) {}
5219 void SetupTree() override {
5220 step_ = 1;
5221 continuous_draws_ = 0;
5222 client_.set_fill_with_nonsolid_color(true);
5224 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5225 root->SetBounds(gfx::Size(500, 500));
5227 scoped_refptr<Layer> pinch = Layer::Create(layer_settings());
5228 pinch->SetBounds(gfx::Size(500, 500));
5229 pinch->SetScrollClipLayerId(root->id());
5230 pinch->SetIsContainerForFixedPositionLayers(true);
5231 root->AddChild(pinch);
5233 scoped_ptr<FakePicturePile> pile(
5234 new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
5235 LayerTreeSettings().default_tile_grid_size));
5236 pile->SetPlaybackAllowedEvent(&playback_allowed_event_);
5237 scoped_refptr<FakePictureLayer> layer =
5238 FakePictureLayer::CreateWithRecordingSource(layer_settings(), &client_,
5239 pile.Pass());
5240 layer->SetBounds(gfx::Size(500, 500));
5241 layer->SetContentsOpaque(true);
5242 // Avoid LCD text on the layer so we don't cause extra commits when we
5243 // pinch.
5244 pinch->AddChild(layer);
5246 layer_tree_host()->RegisterViewportLayers(NULL, root, pinch, pinch);
5247 layer_tree_host()->SetPageScaleFactorAndLimits(1.f, 1.f, 4.f);
5248 layer_tree_host()->SetRootLayer(root);
5249 LayerTreeHostTest::SetupTree();
5252 // Returns the delta scale of all quads in the frame's root pass from their
5253 // ideal, or 0 if they are not all the same.
5254 float FrameQuadScaleDeltaFromIdeal(LayerTreeHostImpl::FrameData* frame_data) {
5255 if (frame_data->has_no_damage)
5256 return 0.f;
5257 float frame_scale = 0.f;
5258 RenderPass* root_pass = frame_data->render_passes.back();
5259 for (const auto& draw_quad : root_pass->quad_list) {
5260 const TileDrawQuad* quad = TileDrawQuad::MaterialCast(draw_quad);
5261 float quad_scale =
5262 quad->tex_coord_rect.width() / static_cast<float>(quad->rect.width());
5263 float transform_scale = SkMScalarToFloat(
5264 quad->shared_quad_state->quad_to_target_transform.matrix().get(0, 0));
5265 float scale = quad_scale / transform_scale;
5266 if (frame_scale != 0.f && frame_scale != scale)
5267 return 0.f;
5268 frame_scale = scale;
5270 return frame_scale;
5273 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5275 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5276 LayerTreeHostImpl::FrameData* frame_data,
5277 DrawResult draw_result) override {
5278 float quad_scale_delta = FrameQuadScaleDeltaFromIdeal(frame_data);
5279 switch (step_) {
5280 case 1:
5281 // Drew at scale 1 before any pinching.
5282 EXPECT_EQ(1.f, host_impl->active_tree()->current_page_scale_factor());
5283 EXPECT_EQ(1.f, quad_scale_delta);
5284 break;
5285 case 2:
5286 if (quad_scale_delta != 1.f / 1.5f)
5287 break;
5288 // Drew at scale 1 still though the ideal is 1.5.
5289 EXPECT_EQ(1.5f, host_impl->active_tree()->current_page_scale_factor());
5290 EXPECT_EQ(1.f / 1.5f, quad_scale_delta);
5291 break;
5292 case 3:
5293 // Continuous draws are attempted.
5294 EXPECT_EQ(1.5f, host_impl->active_tree()->current_page_scale_factor());
5295 if (!frame_data->has_no_damage)
5296 EXPECT_EQ(1.f / 1.5f, quad_scale_delta);
5297 break;
5298 case 4:
5299 if (quad_scale_delta != 1.f)
5300 break;
5301 // Drew at scale 1.5 when all the tiles completed.
5302 EXPECT_EQ(1.5f, host_impl->active_tree()->current_page_scale_factor());
5303 EXPECT_EQ(1.f, quad_scale_delta);
5304 break;
5305 case 5:
5306 // TODO(danakj): We get more draws before the NotifyReadyToDraw
5307 // because it is asynchronous from the previous draw and happens late.
5308 break;
5309 case 6:
5310 // NotifyReadyToDraw happened. If we were already inside a frame, we may
5311 // try to draw once more.
5312 break;
5313 case 7:
5314 NOTREACHED() << "No draws should happen once we have a complete frame.";
5315 break;
5317 return draw_result;
5320 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
5321 switch (step_) {
5322 case 1:
5323 // Delay tile production.
5324 playback_allowed_event_.Reset();
5325 // Pinch zoom in to cause new tiles to be required.
5326 host_impl->PinchGestureBegin();
5327 host_impl->PinchGestureUpdate(1.5f, gfx::Point(100, 100));
5328 host_impl->PinchGestureEnd();
5329 ++step_;
5330 break;
5331 case 2:
5332 ++step_;
5333 break;
5334 case 3:
5335 // We should continue to try draw while there are incomplete visible
5336 // tiles.
5337 if (++continuous_draws_ > 5) {
5338 // Allow the tiles to complete.
5339 playback_allowed_event_.Signal();
5340 ++step_;
5342 break;
5343 case 4:
5344 ++step_;
5345 break;
5346 case 5:
5347 // Waiting for NotifyReadyToDraw.
5348 break;
5349 case 6:
5350 // NotifyReadyToDraw happened.
5351 ++step_;
5352 break;
5356 void NotifyReadyToDrawOnThread(LayerTreeHostImpl* host_impl) override {
5357 if (step_ == 5) {
5358 ++step_;
5359 // NotifyReadyToDraw has happened, we may draw once more, but should not
5360 // get any more draws after that. End the test after a timeout to watch
5361 // for any extraneous draws.
5362 // TODO(brianderson): We could remove this delay and instead wait until
5363 // the BeginFrameSource decides it doesn't need to send frames anymore,
5364 // or test that it already doesn't here.
5365 EndTestAfterDelayMs(16 * 4);
5369 void NotifyTileStateChangedOnThread(LayerTreeHostImpl* host_impl,
5370 const Tile* tile) override {
5371 // On step_ == 2, we are preventing texture uploads from completing,
5372 // so this verifies they are not completing before step_ == 3.
5373 // Flaky failures here indicate we're failing to prevent uploads from
5374 // completing.
5375 EXPECT_NE(2, step_);
5378 void AfterTest() override { EXPECT_GT(continuous_draws_, 5); }
5380 FakeContentLayerClient client_;
5381 int step_;
5382 int continuous_draws_;
5383 base::WaitableEvent playback_allowed_event_;
5386 MULTI_THREAD_TEST_F(LayerTreeHostTestContinuousDrawWhenCreatingVisibleTiles);
5388 class LayerTreeHostTestOneActivatePerPrepareTiles : public LayerTreeHostTest {
5389 public:
5390 LayerTreeHostTestOneActivatePerPrepareTiles()
5391 : notify_ready_to_activate_count_(0u),
5392 scheduled_prepare_tiles_count_(0) {}
5394 void SetupTree() override {
5395 client_.set_fill_with_nonsolid_color(true);
5396 scoped_refptr<FakePictureLayer> root_layer =
5397 FakePictureLayer::Create(layer_settings(), &client_);
5398 root_layer->SetBounds(gfx::Size(1500, 1500));
5399 root_layer->SetIsDrawable(true);
5401 layer_tree_host()->SetRootLayer(root_layer);
5402 LayerTreeHostTest::SetupTree();
5405 void BeginTest() override {
5406 layer_tree_host()->SetViewportSize(gfx::Size(16, 16));
5407 PostSetNeedsCommitToMainThread();
5410 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
5411 bool success) override {
5412 ASSERT_TRUE(success);
5413 host_impl->tile_manager()->SetScheduledRasterTaskLimitForTesting(1);
5416 void NotifyReadyToActivateOnThread(LayerTreeHostImpl* impl) override {
5417 ++notify_ready_to_activate_count_;
5418 EndTestAfterDelayMs(100);
5421 void ScheduledActionPrepareTiles() override {
5422 ++scheduled_prepare_tiles_count_;
5425 void AfterTest() override {
5426 // Expect at most a notification for each scheduled prepare tiles, plus one
5427 // for the initial commit (which doesn't go through scheduled actions).
5428 // The reason this is not an equality is because depending on timing, we
5429 // might get a prepare tiles but not yet get a notification that we're
5430 // ready to activate. The intent of a test is to ensure that we don't
5431 // get more than one notification per prepare tiles, so this is OK.
5432 EXPECT_LE(notify_ready_to_activate_count_,
5433 1u + scheduled_prepare_tiles_count_);
5436 protected:
5437 FakeContentLayerClient client_;
5438 size_t notify_ready_to_activate_count_;
5439 size_t scheduled_prepare_tiles_count_;
5442 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestOneActivatePerPrepareTiles);
5444 class LayerTreeHostTestFrameTimingRequestsSaveTimestamps
5445 : public LayerTreeHostTest {
5446 public:
5447 LayerTreeHostTestFrameTimingRequestsSaveTimestamps()
5448 : check_results_on_commit_(false) {}
5450 void SetupTree() override {
5451 scoped_refptr<FakePictureLayer> root_layer =
5452 FakePictureLayer::Create(layer_settings(), &client_);
5453 root_layer->SetBounds(gfx::Size(200, 200));
5454 root_layer->SetIsDrawable(true);
5456 scoped_refptr<FakePictureLayer> child_layer =
5457 FakePictureLayer::Create(layer_settings(), &client_);
5458 child_layer->SetBounds(gfx::Size(1500, 1500));
5459 child_layer->SetIsDrawable(true);
5461 std::vector<FrameTimingRequest> requests;
5462 requests.push_back(FrameTimingRequest(1, gfx::Rect(0, 0, 100, 100)));
5463 requests.push_back(FrameTimingRequest(2, gfx::Rect(300, 0, 100, 100)));
5464 child_layer->SetFrameTimingRequests(requests);
5466 root_layer->AddChild(child_layer);
5467 layer_tree_host()->SetRootLayer(root_layer);
5468 LayerTreeHostTest::SetupTree();
5471 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5473 void BeginCommitOnThread(LayerTreeHostImpl* host_impl) override {
5474 if (!check_results_on_commit_)
5475 return;
5477 // Since in reality, the events will be read by LayerTreeHost during commit,
5478 // we check the requests here to ensure that they are correct at the next
5479 // commit time (as opposed to checking in DrawLayers for instance).
5480 // TODO(vmpstr): Change this to read things from the main thread when this
5481 // information is propagated to the main thread (not yet implemented).
5482 FrameTimingTracker* tracker = host_impl->frame_timing_tracker();
5484 // Check composite events.
5486 scoped_ptr<FrameTimingTracker::CompositeTimingSet> timing_set =
5487 tracker->GroupCompositeCountsByRectId();
5488 EXPECT_EQ(1u, timing_set->size());
5489 auto rect_1_it = timing_set->find(1);
5490 EXPECT_TRUE(rect_1_it != timing_set->end());
5491 const auto& timing_events = rect_1_it->second;
5492 EXPECT_EQ(1u, timing_events.size());
5493 EXPECT_EQ(host_impl->active_tree()->source_frame_number(),
5494 timing_events[0].frame_id);
5495 EXPECT_GT(timing_events[0].timestamp, base::TimeTicks());
5498 // Check main frame events.
5500 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> timing_set =
5501 tracker->GroupMainFrameCountsByRectId();
5502 EXPECT_EQ(2u, timing_set->size());
5503 auto rect_1_it = timing_set->find(1);
5504 EXPECT_TRUE(rect_1_it != timing_set->end());
5505 const auto& timing_events = rect_1_it->second;
5506 EXPECT_EQ(1u, timing_events.size());
5507 EXPECT_EQ(host_impl->active_tree()->source_frame_number(),
5508 timing_events[0].frame_id);
5509 EXPECT_GT(timing_events[0].timestamp, base::TimeTicks());
5510 EXPECT_GT(timing_events[0].end_time, timing_events[0].timestamp);
5513 EndTest();
5516 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
5517 check_results_on_commit_ = true;
5518 PostSetNeedsCommitToMainThread();
5521 void AfterTest() override {}
5523 private:
5524 FakeContentLayerClient client_;
5525 bool check_results_on_commit_;
5528 // Frame timing is not implemented in single thread proxy.
5529 MULTI_THREAD_TEST_F(LayerTreeHostTestFrameTimingRequestsSaveTimestamps);
5531 class LayerTreeHostTestActivationCausesPrepareTiles : public LayerTreeHostTest {
5532 public:
5533 LayerTreeHostTestActivationCausesPrepareTiles()
5534 : scheduled_prepare_tiles_count_(0) {}
5536 void SetupTree() override {
5537 client_.set_fill_with_nonsolid_color(true);
5538 scoped_refptr<FakePictureLayer> root_layer =
5539 FakePictureLayer::Create(layer_settings(), &client_);
5540 root_layer->SetBounds(gfx::Size(150, 150));
5541 root_layer->SetIsDrawable(true);
5543 layer_tree_host()->SetRootLayer(root_layer);
5544 LayerTreeHostTest::SetupTree();
5547 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5549 void NotifyReadyToActivateOnThread(LayerTreeHostImpl* impl) override {
5550 // Ensure we've already activated.
5551 EXPECT_FALSE(impl->pending_tree());
5553 // After activating, we either need to prepare tiles, or we've already
5554 // called a scheduled prepare tiles. This is done because activation might
5555 // cause us to have to memory available (old active tree is gone), so we
5556 // need to ensure we will get a PrepareTiles call.
5557 if (!impl->prepare_tiles_needed())
5558 EXPECT_GE(scheduled_prepare_tiles_count_, 1);
5559 EndTest();
5562 void ScheduledActionPrepareTiles() override {
5563 ++scheduled_prepare_tiles_count_;
5566 void AfterTest() override {}
5568 protected:
5569 FakeContentLayerClient client_;
5570 int scheduled_prepare_tiles_count_;
5573 // This test is testing activation from a pending tree and doesn't make sense
5574 // with single thread commit-to-active.
5575 MULTI_THREAD_TEST_F(LayerTreeHostTestActivationCausesPrepareTiles);
5577 // This tests an assertion that DidCommit and WillCommit happen in the same
5578 // stack frame with no tasks that run between them. Various embedders of
5579 // cc depend on this logic. ui::Compositor holds a compositor lock between
5580 // these events and the inspector timeline wants begin/end CompositeLayers
5581 // to be properly nested with other begin/end events.
5582 class LayerTreeHostTestNoTasksBetweenWillAndDidCommit
5583 : public LayerTreeHostTest {
5584 public:
5585 LayerTreeHostTestNoTasksBetweenWillAndDidCommit() : did_commit_(false) {}
5587 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5589 void WillCommit() override {
5590 MainThreadTaskRunner()->PostTask(
5591 FROM_HERE, base::Bind(&LayerTreeHostTestNoTasksBetweenWillAndDidCommit::
5592 EndTestShouldRunAfterDidCommit,
5593 base::Unretained(this)));
5596 void EndTestShouldRunAfterDidCommit() {
5597 EXPECT_TRUE(did_commit_);
5598 EndTest();
5601 void DidCommit() override {
5602 EXPECT_FALSE(did_commit_);
5603 did_commit_ = true;
5606 void AfterTest() override { EXPECT_TRUE(did_commit_); }
5608 private:
5609 bool did_commit_;
5612 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestNoTasksBetweenWillAndDidCommit);
5614 class LayerTreeHostTestUpdateCopyRequests : public LayerTreeHostTest {
5615 protected:
5616 void SetupTree() override {
5617 root = Layer::Create(layer_settings());
5618 child = Layer::Create(layer_settings());
5619 root->AddChild(child);
5620 layer_tree_host()->SetRootLayer(root);
5621 LayerTreeHostTest::SetupTree();
5624 static void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {}
5626 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5628 void DidCommit() override {
5629 switch (layer_tree_host()->source_frame_number()) {
5630 case 1:
5631 child->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
5632 base::Bind(CopyOutputCallback)));
5633 EXPECT_GT(root->num_layer_or_descendants_with_copy_request(), 0);
5634 break;
5635 case 2:
5636 EXPECT_EQ(root->num_layer_or_descendants_with_copy_request(), 0);
5637 EndTest();
5638 break;
5642 void AfterTest() override {}
5644 private:
5645 scoped_refptr<Layer> root;
5646 scoped_refptr<Layer> child;
5649 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestUpdateCopyRequests);
5651 class LayerTreeTestMaskLayerForSurfaceWithClippedLayer : public LayerTreeTest {
5652 protected:
5653 void SetupTree() override {
5654 // The masked layer has bounds 50x50, but it has a child that causes
5655 // the surface bounds to be larger. It also has a parent that clips the
5656 // masked layer and its surface.
5658 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5660 scoped_refptr<Layer> clipping_layer = Layer::Create(layer_settings());
5661 root->AddChild(clipping_layer);
5663 scoped_refptr<FakePictureLayer> content_layer =
5664 FakePictureLayer::Create(layer_settings(), &client_);
5665 clipping_layer->AddChild(content_layer);
5667 scoped_refptr<FakePictureLayer> content_child_layer =
5668 FakePictureLayer::Create(layer_settings(), &client_);
5669 content_layer->AddChild(content_child_layer);
5671 scoped_refptr<FakePictureLayer> mask_layer =
5672 FakePictureLayer::Create(layer_settings(), &client_);
5673 content_layer->SetMaskLayer(mask_layer.get());
5675 gfx::Size root_size(100, 100);
5676 root->SetBounds(root_size);
5678 gfx::Rect clipping_rect(20, 10, 10, 20);
5679 clipping_layer->SetBounds(clipping_rect.size());
5680 clipping_layer->SetPosition(clipping_rect.origin());
5681 clipping_layer->SetMasksToBounds(true);
5683 gfx::Size layer_size(50, 50);
5684 content_layer->SetBounds(layer_size);
5685 content_layer->SetPosition(gfx::Point() - clipping_rect.OffsetFromOrigin());
5687 gfx::Size child_size(50, 50);
5688 content_child_layer->SetBounds(child_size);
5689 content_child_layer->SetPosition(gfx::Point(20, 0));
5691 gfx::Size mask_size(100, 100);
5692 mask_layer->SetBounds(mask_size);
5693 mask_layer->SetIsMask(true);
5695 layer_tree_host()->SetRootLayer(root);
5696 LayerTreeTest::SetupTree();
5699 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5701 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5702 LayerTreeHostImpl::FrameData* frame_data,
5703 DrawResult draw_result) override {
5704 EXPECT_EQ(2u, frame_data->render_passes.size());
5705 RenderPass* root_pass = frame_data->render_passes.back();
5706 EXPECT_EQ(2u, root_pass->quad_list.size());
5708 // There's a solid color quad under everything.
5709 EXPECT_EQ(DrawQuad::SOLID_COLOR, root_pass->quad_list.back()->material);
5711 // The surface is clipped to 10x20.
5712 EXPECT_EQ(DrawQuad::RENDER_PASS, root_pass->quad_list.front()->material);
5713 const RenderPassDrawQuad* render_pass_quad =
5714 RenderPassDrawQuad::MaterialCast(root_pass->quad_list.front());
5715 EXPECT_EQ(gfx::Rect(20, 10, 10, 20).ToString(),
5716 render_pass_quad->rect.ToString());
5717 // The masked layer is 50x50, but the surface size is 10x20. So the texture
5718 // coords in the mask are scaled by 10/50 and 20/50.
5719 // The surface is clipped to (20,10) so the mask texture coords are offset
5720 // by 20/50 and 10/50
5721 EXPECT_EQ(gfx::ScaleRect(gfx::RectF(20.f, 10.f, 10.f, 20.f), 1.f / 50.f)
5722 .ToString(),
5723 render_pass_quad->MaskUVRect().ToString());
5724 EXPECT_EQ(gfx::Vector2dF(10.f / 50.f, 20.f / 50.f).ToString(),
5725 render_pass_quad->mask_uv_scale.ToString());
5726 EndTest();
5727 return draw_result;
5730 void AfterTest() override {}
5732 FakeContentLayerClient client_;
5735 SINGLE_AND_MULTI_THREAD_TEST_F(
5736 LayerTreeTestMaskLayerForSurfaceWithClippedLayer);
5738 class LayerTreeTestMaskLayerWithScaling : public LayerTreeTest {
5739 protected:
5740 void InitializeSettings(LayerTreeSettings* settings) override {
5741 settings->layer_transforms_should_scale_layer_contents = true;
5744 void SetupTree() override {
5745 // Root
5746 // |
5747 // +-- Scaling Layer (adds a 2x scale)
5748 // |
5749 // +-- Content Layer
5750 // +--Mask
5752 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5754 scoped_refptr<Layer> scaling_layer = Layer::Create(layer_settings());
5755 root->AddChild(scaling_layer);
5757 scoped_refptr<FakePictureLayer> content_layer =
5758 FakePictureLayer::Create(layer_settings(), &client_);
5759 scaling_layer->AddChild(content_layer);
5761 scoped_refptr<FakePictureLayer> mask_layer =
5762 FakePictureLayer::Create(layer_settings(), &client_);
5763 content_layer->SetMaskLayer(mask_layer.get());
5765 gfx::Size root_size(100, 100);
5766 root->SetBounds(root_size);
5768 gfx::Size scaling_layer_size(50, 50);
5769 scaling_layer->SetBounds(scaling_layer_size);
5770 gfx::Transform scale;
5771 scale.Scale(2.f, 2.f);
5772 scaling_layer->SetTransform(scale);
5774 content_layer->SetBounds(scaling_layer_size);
5776 mask_layer->SetBounds(scaling_layer_size);
5777 mask_layer->SetIsMask(true);
5779 layer_tree_host()->SetRootLayer(root);
5780 LayerTreeTest::SetupTree();
5783 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5785 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5786 LayerTreeHostImpl::FrameData* frame_data,
5787 DrawResult draw_result) override {
5788 EXPECT_EQ(2u, frame_data->render_passes.size());
5789 RenderPass* root_pass = frame_data->render_passes.back();
5790 EXPECT_EQ(2u, root_pass->quad_list.size());
5792 // There's a solid color quad under everything.
5793 EXPECT_EQ(DrawQuad::SOLID_COLOR, root_pass->quad_list.back()->material);
5795 EXPECT_EQ(DrawQuad::RENDER_PASS, root_pass->quad_list.front()->material);
5796 const RenderPassDrawQuad* render_pass_quad =
5797 RenderPassDrawQuad::MaterialCast(root_pass->quad_list.front());
5798 switch (host_impl->active_tree()->source_frame_number()) {
5799 case 0:
5800 // Check that the tree scaling is correctly taken into account for the
5801 // mask, that should fully map onto the quad.
5802 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(),
5803 render_pass_quad->rect.ToString());
5804 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5805 render_pass_quad->MaskUVRect().ToString());
5806 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5807 render_pass_quad->mask_uv_scale.ToString());
5808 break;
5809 case 1:
5810 // Applying a DSF should change the render surface size, but won't
5811 // affect which part of the mask is used.
5812 EXPECT_EQ(gfx::Rect(0, 0, 200, 200).ToString(),
5813 render_pass_quad->rect.ToString());
5814 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5815 render_pass_quad->MaskUVRect().ToString());
5816 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5817 render_pass_quad->mask_uv_scale.ToString());
5818 EndTest();
5819 break;
5821 return draw_result;
5824 void DidCommit() override {
5825 switch (layer_tree_host()->source_frame_number()) {
5826 case 1:
5827 gfx::Size double_root_size(200, 200);
5828 layer_tree_host()->SetViewportSize(double_root_size);
5829 layer_tree_host()->SetDeviceScaleFactor(2.f);
5830 break;
5834 void AfterTest() override {}
5836 FakeContentLayerClient client_;
5839 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeTestMaskLayerWithScaling);
5841 class LayerTreeTestMaskLayerWithDifferentBounds : public LayerTreeTest {
5842 protected:
5843 void SetupTree() override {
5844 // The mask layer has bounds 100x100 but is attached to a layer with bounds
5845 // 50x50.
5847 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5849 scoped_refptr<FakePictureLayer> content_layer =
5850 FakePictureLayer::Create(layer_settings(), &client_);
5851 root->AddChild(content_layer);
5853 scoped_refptr<FakePictureLayer> mask_layer =
5854 FakePictureLayer::Create(layer_settings(), &client_);
5855 content_layer->SetMaskLayer(mask_layer.get());
5857 gfx::Size root_size(100, 100);
5858 root->SetBounds(root_size);
5860 gfx::Size layer_size(50, 50);
5861 content_layer->SetBounds(layer_size);
5863 gfx::Size mask_size(100, 100);
5864 mask_layer->SetBounds(mask_size);
5865 mask_layer->SetIsMask(true);
5867 layer_tree_host()->SetRootLayer(root);
5868 LayerTreeTest::SetupTree();
5871 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5873 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5874 LayerTreeHostImpl::FrameData* frame_data,
5875 DrawResult draw_result) override {
5876 EXPECT_EQ(2u, frame_data->render_passes.size());
5877 RenderPass* root_pass = frame_data->render_passes.back();
5878 EXPECT_EQ(2u, root_pass->quad_list.size());
5880 // There's a solid color quad under everything.
5881 EXPECT_EQ(DrawQuad::SOLID_COLOR, root_pass->quad_list.back()->material);
5883 EXPECT_EQ(DrawQuad::RENDER_PASS, root_pass->quad_list.front()->material);
5884 const RenderPassDrawQuad* render_pass_quad =
5885 RenderPassDrawQuad::MaterialCast(root_pass->quad_list.front());
5886 switch (host_impl->active_tree()->source_frame_number()) {
5887 case 0:
5888 // Check that the mask fills the surface.
5889 EXPECT_EQ(gfx::Rect(0, 0, 50, 50).ToString(),
5890 render_pass_quad->rect.ToString());
5891 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5892 render_pass_quad->MaskUVRect().ToString());
5893 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5894 render_pass_quad->mask_uv_scale.ToString());
5895 break;
5896 case 1:
5897 // Applying a DSF should change the render surface size, but won't
5898 // affect which part of the mask is used.
5899 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(),
5900 render_pass_quad->rect.ToString());
5901 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5902 render_pass_quad->MaskUVRect().ToString());
5903 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5904 render_pass_quad->mask_uv_scale.ToString());
5905 EndTest();
5906 break;
5908 return draw_result;
5911 void DidCommit() override {
5912 switch (layer_tree_host()->source_frame_number()) {
5913 case 1:
5914 gfx::Size double_root_size(200, 200);
5915 layer_tree_host()->SetViewportSize(double_root_size);
5916 layer_tree_host()->SetDeviceScaleFactor(2.f);
5917 break;
5921 void AfterTest() override {}
5923 FakeContentLayerClient client_;
5926 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeTestMaskLayerWithDifferentBounds);
5928 class LayerTreeTestReflectionMaskLayerWithDifferentBounds
5929 : public LayerTreeTest {
5930 protected:
5931 void SetupTree() override {
5932 // The replica's mask layer has bounds 100x100 but the replica is of a
5933 // layer with bounds 50x50.
5935 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5937 scoped_refptr<FakePictureLayer> content_layer =
5938 FakePictureLayer::Create(layer_settings(), &client_);
5939 root->AddChild(content_layer);
5941 scoped_refptr<Layer> replica_layer = Layer::Create(layer_settings());
5942 content_layer->SetReplicaLayer(replica_layer.get());
5944 scoped_refptr<FakePictureLayer> mask_layer =
5945 FakePictureLayer::Create(layer_settings(), &client_);
5946 replica_layer->SetMaskLayer(mask_layer.get());
5948 gfx::Size root_size(100, 100);
5949 root->SetBounds(root_size);
5951 gfx::Size layer_size(50, 50);
5952 content_layer->SetBounds(layer_size);
5954 gfx::Size mask_size(100, 100);
5955 mask_layer->SetBounds(mask_size);
5956 mask_layer->SetIsMask(true);
5958 layer_tree_host()->SetRootLayer(root);
5959 LayerTreeTest::SetupTree();
5962 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5964 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5965 LayerTreeHostImpl::FrameData* frame_data,
5966 DrawResult draw_result) override {
5967 EXPECT_EQ(2u, frame_data->render_passes.size());
5968 RenderPass* root_pass = frame_data->render_passes.back();
5969 EXPECT_EQ(3u, root_pass->quad_list.size());
5971 // There's a solid color quad under everything.
5972 EXPECT_EQ(DrawQuad::SOLID_COLOR, root_pass->quad_list.back()->material);
5974 EXPECT_EQ(DrawQuad::RENDER_PASS,
5975 root_pass->quad_list.ElementAt(1)->material);
5976 const RenderPassDrawQuad* render_pass_quad =
5977 RenderPassDrawQuad::MaterialCast(root_pass->quad_list.ElementAt(1));
5978 switch (host_impl->active_tree()->source_frame_number()) {
5979 case 0:
5980 // Check that the mask fills the surface.
5981 EXPECT_EQ(gfx::Rect(0, 0, 50, 50).ToString(),
5982 render_pass_quad->rect.ToString());
5983 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5984 render_pass_quad->MaskUVRect().ToString());
5985 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5986 render_pass_quad->mask_uv_scale.ToString());
5987 break;
5988 case 1:
5989 // Applying a DSF should change the render surface size, but won't
5990 // affect which part of the mask is used.
5991 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(),
5992 render_pass_quad->rect.ToString());
5993 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5994 render_pass_quad->MaskUVRect().ToString());
5995 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5996 render_pass_quad->mask_uv_scale.ToString());
5997 EndTest();
5998 break;
6000 return draw_result;
6003 void DidCommit() override {
6004 switch (layer_tree_host()->source_frame_number()) {
6005 case 1:
6006 gfx::Size double_root_size(200, 200);
6007 layer_tree_host()->SetViewportSize(double_root_size);
6008 layer_tree_host()->SetDeviceScaleFactor(2.f);
6009 break;
6013 void AfterTest() override {}
6015 FakeContentLayerClient client_;
6018 SINGLE_AND_MULTI_THREAD_TEST_F(
6019 LayerTreeTestReflectionMaskLayerWithDifferentBounds);
6021 class LayerTreeTestReflectionMaskLayerForSurfaceWithUnclippedChild
6022 : public LayerTreeTest {
6023 protected:
6024 void SetupTree() override {
6025 // The replica is of a layer with bounds 50x50, but it has a child that
6026 // causes the surface bounds to be larger.
6028 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6030 scoped_refptr<FakePictureLayer> content_layer =
6031 FakePictureLayer::Create(layer_settings(), &client_);
6032 root->AddChild(content_layer);
6034 content_child_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
6035 content_layer->AddChild(content_child_layer_);
6037 scoped_refptr<Layer> replica_layer = Layer::Create(layer_settings());
6038 content_layer->SetReplicaLayer(replica_layer.get());
6040 scoped_refptr<FakePictureLayer> mask_layer =
6041 FakePictureLayer::Create(layer_settings(), &client_);
6042 replica_layer->SetMaskLayer(mask_layer.get());
6044 gfx::Size root_size(100, 100);
6045 root->SetBounds(root_size);
6047 gfx::Size layer_size(50, 50);
6048 content_layer->SetBounds(layer_size);
6049 content_child_layer_->SetBounds(layer_size);
6050 content_child_layer_->SetPosition(gfx::PointF(50.f, 0.f));
6052 gfx::Size mask_size(100, 100);
6053 mask_layer->SetBounds(mask_size);
6054 mask_layer->SetIsMask(true);
6056 layer_tree_host()->SetRootLayer(root);
6057 LayerTreeTest::SetupTree();
6060 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
6062 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
6063 LayerTreeHostImpl::FrameData* frame_data,
6064 DrawResult draw_result) override {
6065 EXPECT_EQ(2u, frame_data->render_passes.size());
6066 RenderPass* root_pass = frame_data->render_passes.back();
6067 EXPECT_EQ(3u, root_pass->quad_list.size());
6069 // There's a solid color quad under everything.
6070 EXPECT_EQ(DrawQuad::SOLID_COLOR, root_pass->quad_list.back()->material);
6072 EXPECT_EQ(DrawQuad::RENDER_PASS,
6073 root_pass->quad_list.ElementAt(1)->material);
6074 const RenderPassDrawQuad* replica_quad =
6075 RenderPassDrawQuad::MaterialCast(root_pass->quad_list.ElementAt(1));
6076 switch (host_impl->active_tree()->source_frame_number()) {
6077 case 0:
6078 // The surface is 100x50.
6079 // The mask covers the owning layer only.
6080 EXPECT_EQ(gfx::Rect(0, 0, 100, 50).ToString(),
6081 replica_quad->rect.ToString());
6082 EXPECT_EQ(gfx::RectF(0.f, 0.f, 2.f, 1.f).ToString(),
6083 replica_quad->MaskUVRect().ToString());
6084 EXPECT_EQ(gfx::Vector2dF(2.f, 1.f).ToString(),
6085 replica_quad->mask_uv_scale.ToString());
6086 break;
6087 case 1:
6088 // The surface is 100x50 with its origin at (-50, 0).
6089 // The mask covers the owning layer only.
6090 EXPECT_EQ(gfx::Rect(-50, 0, 100, 50).ToString(),
6091 replica_quad->rect.ToString());
6092 EXPECT_EQ(gfx::RectF(-1.f, 0.f, 2.f, 1.f).ToString(),
6093 replica_quad->MaskUVRect().ToString());
6094 EXPECT_EQ(gfx::Vector2dF(2.f, 1.f).ToString(),
6095 replica_quad->mask_uv_scale.ToString());
6096 EndTest();
6097 break;
6099 return draw_result;
6102 void DidCommit() override {
6103 switch (layer_tree_host()->source_frame_number()) {
6104 case 1:
6105 // Move the child to (-50, 0) instead. Now the mask should be moved to
6106 // still cover the layer being replicated.
6107 content_child_layer_->SetPosition(gfx::PointF(-50.f, 0.f));
6108 break;
6112 void AfterTest() override {}
6114 scoped_refptr<FakePictureLayer> content_child_layer_;
6115 FakeContentLayerClient client_;
6118 SINGLE_AND_MULTI_THREAD_TEST_F(
6119 LayerTreeTestReflectionMaskLayerForSurfaceWithUnclippedChild);
6121 class LayerTreeTestPageScaleFlags : public LayerTreeTest {
6122 protected:
6123 void SetupTree() override {
6124 // -root
6125 // -pre page scale
6126 // -page scale
6127 // -page scale child1
6128 // -page scale grandchild
6129 // -page scale child2
6130 // -post page scale
6132 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6133 scoped_refptr<Layer> pre_page_scale = Layer::Create(layer_settings());
6134 scoped_refptr<Layer> page_scale = Layer::Create(layer_settings());
6135 scoped_refptr<Layer> page_scale_child1 = Layer::Create(layer_settings());
6136 scoped_refptr<Layer> page_scale_grandchild =
6137 Layer::Create(layer_settings());
6138 scoped_refptr<Layer> page_scale_child2 = Layer::Create(layer_settings());
6139 scoped_refptr<Layer> post_page_scale = Layer::Create(layer_settings());
6141 root->AddChild(pre_page_scale);
6142 root->AddChild(page_scale);
6143 root->AddChild(post_page_scale);
6145 page_scale->AddChild(page_scale_child1);
6146 page_scale->AddChild(page_scale_child2);
6147 page_scale_child1->AddChild(page_scale_grandchild);
6149 layer_tree_host()->SetRootLayer(root);
6150 LayerTreeTest::SetupTree();
6152 scoped_refptr<Layer> overscroll_elasticity_layer = nullptr;
6153 scoped_refptr<Layer> inner_viewport_scroll_layer = nullptr;
6154 scoped_refptr<Layer> outer_viewport_scroll_layer = nullptr;
6155 layer_tree_host()->RegisterViewportLayers(
6156 overscroll_elasticity_layer, page_scale, inner_viewport_scroll_layer,
6157 outer_viewport_scroll_layer);
6159 affected_by_page_scale_.push_back(page_scale->id());
6160 affected_by_page_scale_.push_back(page_scale_child1->id());
6161 affected_by_page_scale_.push_back(page_scale_child2->id());
6162 affected_by_page_scale_.push_back(page_scale_grandchild->id());
6164 not_affected_by_page_scale_.push_back(root->id());
6165 not_affected_by_page_scale_.push_back(pre_page_scale->id());
6166 not_affected_by_page_scale_.push_back(post_page_scale->id());
6169 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
6171 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
6172 LayerTreeHostCommon::CallFunctionForSubtree(
6173 host_impl->sync_tree()->root_layer(), [this](LayerImpl* layer) {
6174 const std::vector<int>& list =
6175 layer->IsAffectedByPageScale()
6176 ? this->affected_by_page_scale_
6177 : this->not_affected_by_page_scale_;
6178 EXPECT_TRUE(std::find(list.begin(), list.end(), layer->id()) !=
6179 list.end());
6182 EndTest();
6185 void AfterTest() override {}
6187 std::vector<int> affected_by_page_scale_;
6188 std::vector<int> not_affected_by_page_scale_;
6191 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeTestPageScaleFlags);
6193 class LayerTreeHostScrollingAndScalingUpdatesLayers : public LayerTreeHostTest {
6194 public:
6195 LayerTreeHostScrollingAndScalingUpdatesLayers()
6196 : requested_update_layers_(false), commit_count_(0) {}
6198 void SetupTree() override {
6199 LayerTreeHostTest::SetupTree();
6200 Layer* root_layer = layer_tree_host()->root_layer();
6201 scoped_refptr<Layer> scroll_layer = Layer::Create(layer_settings());
6202 CreateVirtualViewportLayers(root_layer, scroll_layer, root_layer->bounds(),
6203 root_layer->bounds(), layer_tree_host(),
6204 layer_settings());
6207 void BeginTest() override {
6208 LayerTreeHostCommon::ScrollUpdateInfo scroll;
6209 scroll.layer_id = layer_tree_host()->root_layer()->id();
6210 scroll.scroll_delta = gfx::Vector2d(0, 33);
6211 scroll_info_.scrolls.push_back(scroll);
6213 scale_info_.page_scale_delta = 2.71f;
6215 PostSetNeedsCommitToMainThread();
6218 void BeginMainFrame(const BeginFrameArgs& args) override {
6219 switch (commit_count_) {
6220 case 0:
6221 requested_update_layers_ = false;
6222 layer_tree_host()->ApplyScrollAndScale(&no_op_info_);
6223 EXPECT_FALSE(requested_update_layers_);
6224 break;
6225 case 1:
6226 requested_update_layers_ = false;
6227 layer_tree_host()->ApplyScrollAndScale(&scale_info_);
6228 EXPECT_TRUE(requested_update_layers_);
6229 break;
6230 case 2:
6231 requested_update_layers_ = false;
6232 layer_tree_host()->ApplyScrollAndScale(&scroll_info_);
6233 EXPECT_TRUE(requested_update_layers_);
6234 EndTest();
6235 break;
6236 default:
6237 NOTREACHED();
6241 void DidSetNeedsUpdateLayers() override { requested_update_layers_ = true; }
6243 void DidCommit() override {
6244 if (++commit_count_ < 3)
6245 PostSetNeedsCommitToMainThread();
6248 void AfterTest() override {}
6250 ScrollAndScaleSet scroll_info_;
6251 ScrollAndScaleSet scale_info_;
6252 ScrollAndScaleSet no_op_info_;
6253 bool requested_update_layers_;
6254 int commit_count_;
6257 MULTI_THREAD_TEST_F(LayerTreeHostScrollingAndScalingUpdatesLayers);
6259 } // namespace
6260 } // namespace cc