[Eraser strings] Remove unused Supervised User infobar and corresponding strings
[chromium-blink-merge.git] / cc / trees / layer_tree_host_unittest.cc
blobe5db8f8a244ee62ecdd86294d156ea7c9d3380a6
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 LayerTreeHostTestSetNeedsUpdateInsideLayout : public LayerTreeHostTest {
113 protected:
114 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
116 void Layout() override {
117 // This shouldn't cause a second commit to happen.
118 layer_tree_host()->SetNeedsUpdateLayers();
121 void DidCommit() override {
122 EXPECT_EQ(1, layer_tree_host()->source_frame_number());
123 EndTest();
126 void AfterTest() override {}
129 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsUpdateInsideLayout);
131 // Test if the LTHI receives ReadyToActivate notifications from the TileManager
132 // when no raster tasks get scheduled.
133 class LayerTreeHostTestReadyToActivateEmpty : public LayerTreeHostTest {
134 public:
135 LayerTreeHostTestReadyToActivateEmpty()
136 : did_notify_ready_to_activate_(false),
137 all_tiles_required_for_activation_are_ready_to_draw_(false),
138 required_for_activation_count_(0) {}
140 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
142 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
143 const std::vector<PictureLayerImpl*>& layers =
144 impl->sync_tree()->picture_layers();
145 required_for_activation_count_ = 0;
146 for (const auto& layer : layers) {
147 FakePictureLayerImpl* fake_layer =
148 static_cast<FakePictureLayerImpl*>(layer);
149 required_for_activation_count_ +=
150 fake_layer->CountTilesRequiredForActivation();
154 void NotifyReadyToActivateOnThread(LayerTreeHostImpl* impl) override {
155 did_notify_ready_to_activate_ = true;
156 all_tiles_required_for_activation_are_ready_to_draw_ =
157 impl->tile_manager()->IsReadyToActivate();
158 EndTest();
161 void AfterTest() override {
162 EXPECT_TRUE(did_notify_ready_to_activate_);
163 EXPECT_TRUE(all_tiles_required_for_activation_are_ready_to_draw_);
164 EXPECT_EQ(size_t(0), required_for_activation_count_);
167 protected:
168 bool did_notify_ready_to_activate_;
169 bool all_tiles_required_for_activation_are_ready_to_draw_;
170 size_t required_for_activation_count_;
173 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestReadyToActivateEmpty);
175 // Test if the LTHI receives ReadyToActivate notifications from the TileManager
176 // when some raster tasks flagged as REQUIRED_FOR_ACTIVATION got scheduled.
177 class LayerTreeHostTestReadyToActivateNonEmpty
178 : public LayerTreeHostTestReadyToActivateEmpty {
179 public:
180 void SetupTree() override {
181 client_.set_fill_with_nonsolid_color(true);
182 scoped_refptr<FakePictureLayer> root_layer =
183 FakePictureLayer::Create(layer_settings(), &client_);
184 root_layer->SetBounds(gfx::Size(1024, 1024));
185 root_layer->SetIsDrawable(true);
187 layer_tree_host()->SetRootLayer(root_layer);
188 LayerTreeHostTest::SetupTree();
191 void AfterTest() override {
192 EXPECT_TRUE(did_notify_ready_to_activate_);
193 EXPECT_TRUE(all_tiles_required_for_activation_are_ready_to_draw_);
194 EXPECT_LE(size_t(1), required_for_activation_count_);
197 private:
198 FakeContentLayerClient client_;
201 // Multi-thread only because in single thread the commit goes directly to the
202 // active tree, so notify ready to activate is skipped.
203 MULTI_THREAD_TEST_F(LayerTreeHostTestReadyToActivateNonEmpty);
205 // Test if the LTHI receives ReadyToDraw notifications from the TileManager when
206 // no raster tasks get scheduled.
207 class LayerTreeHostTestReadyToDrawEmpty : public LayerTreeHostTest {
208 public:
209 LayerTreeHostTestReadyToDrawEmpty()
210 : did_notify_ready_to_draw_(false),
211 all_tiles_required_for_draw_are_ready_to_draw_(false),
212 required_for_draw_count_(0) {}
214 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
216 void NotifyReadyToDrawOnThread(LayerTreeHostImpl* impl) override {
217 did_notify_ready_to_draw_ = true;
218 const std::vector<PictureLayerImpl*>& layers =
219 impl->active_tree()->picture_layers();
220 all_tiles_required_for_draw_are_ready_to_draw_ =
221 impl->tile_manager()->IsReadyToDraw();
222 for (const auto& layer : layers) {
223 FakePictureLayerImpl* fake_layer =
224 static_cast<FakePictureLayerImpl*>(layer);
225 required_for_draw_count_ += fake_layer->CountTilesRequiredForDraw();
228 EndTest();
231 void AfterTest() override {
232 EXPECT_TRUE(did_notify_ready_to_draw_);
233 EXPECT_TRUE(all_tiles_required_for_draw_are_ready_to_draw_);
234 EXPECT_EQ(size_t(0), required_for_draw_count_);
237 protected:
238 bool did_notify_ready_to_draw_;
239 bool all_tiles_required_for_draw_are_ready_to_draw_;
240 size_t required_for_draw_count_;
243 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestReadyToDrawEmpty);
245 // Test if the LTHI receives ReadyToDraw notifications from the TileManager when
246 // some raster tasks flagged as REQUIRED_FOR_DRAW got scheduled.
247 class LayerTreeHostTestReadyToDrawNonEmpty
248 : public LayerTreeHostTestReadyToDrawEmpty {
249 public:
250 void SetupTree() override {
251 client_.set_fill_with_nonsolid_color(true);
252 scoped_refptr<FakePictureLayer> root_layer =
253 FakePictureLayer::Create(layer_settings(), &client_);
254 root_layer->SetBounds(gfx::Size(1024, 1024));
255 root_layer->SetIsDrawable(true);
257 layer_tree_host()->SetRootLayer(root_layer);
258 LayerTreeHostTest::SetupTree();
261 void AfterTest() override {
262 EXPECT_TRUE(did_notify_ready_to_draw_);
263 EXPECT_TRUE(all_tiles_required_for_draw_are_ready_to_draw_);
264 EXPECT_LE(size_t(1), required_for_draw_count_);
267 private:
268 FakeContentLayerClient client_;
271 // Note: With this test setup, we only get tiles flagged as REQUIRED_FOR_DRAW in
272 // single threaded mode.
273 SINGLE_THREAD_TEST_F(LayerTreeHostTestReadyToDrawNonEmpty);
275 // This tests if we get the READY_TO_DRAW signal and draw if we become invisible
276 // and then become visible again.
277 class LayerTreeHostTestReadyToDrawVisibility : public LayerTreeHostTest {
278 public:
279 LayerTreeHostTestReadyToDrawVisibility()
280 : LayerTreeHostTest(),
281 toggled_visibility_(false),
282 did_notify_ready_to_draw_(false),
283 did_draw_(false) {}
285 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
287 void SetupTree() override {
288 client_.set_fill_with_nonsolid_color(true);
289 scoped_refptr<FakePictureLayer> root_layer =
290 FakePictureLayer::Create(layer_settings(), &client_);
291 root_layer->SetBounds(gfx::Size(1024, 1024));
292 root_layer->SetIsDrawable(true);
294 layer_tree_host()->SetRootLayer(root_layer);
295 LayerTreeHostTest::SetupTree();
298 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
299 if (!toggled_visibility_) {
301 DebugScopedSetMainThread main(proxy());
302 layer_tree_host()->SetVisible(false);
304 toggled_visibility_ = true;
305 EXPECT_FALSE(host_impl->visible());
309 void NotifyReadyToDrawOnThread(LayerTreeHostImpl* host_impl) override {
310 // Sometimes the worker thread posts NotifyReadyToDraw in the extremely
311 // short duration of time between PrepareTiles and SetVisible(false) so we
312 // might get two NotifyReadyToDraw signals for this test.
313 did_notify_ready_to_draw_ = true;
316 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
317 EXPECT_FALSE(did_draw_);
318 did_draw_ = true;
319 EndTest();
322 void DidFinishImplFrameOnThread(LayerTreeHostImpl* host_impl) override {
323 if (!host_impl->visible()) {
325 DebugScopedSetMainThread main(proxy());
326 layer_tree_host()->SetVisible(true);
328 EXPECT_TRUE(host_impl->visible());
332 void AfterTest() override {
333 EXPECT_TRUE(did_notify_ready_to_draw_);
334 EXPECT_TRUE(did_draw_);
337 private:
338 FakeContentLayerClient client_;
339 bool toggled_visibility_;
340 bool did_notify_ready_to_draw_;
341 bool did_draw_;
344 // Note: With this test setup, we only get tiles flagged as REQUIRED_FOR_DRAW in
345 // single threaded mode.
346 SINGLE_THREAD_TEST_F(LayerTreeHostTestReadyToDrawVisibility);
348 class LayerTreeHostFreeWorkerContextResourcesTest : public LayerTreeHostTest {
349 public:
350 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
351 auto output_surface = make_scoped_ptr(new testing::StrictMock<
352 MockSetWorkerContextShouldAggressivelyFreeResourcesOutputSurface>(
353 delegating_renderer()));
355 // At init, we expect one call to set visibility to true.
356 testing::Expectation visibility_true =
357 EXPECT_CALL(*output_surface,
358 SetWorkerContextShouldAggressivelyFreeResources(false))
359 .Times(1);
361 // After running, we should get exactly one call to
362 // FreeWorkerContextGpuResources.
363 EXPECT_CALL(*output_surface,
364 SetWorkerContextShouldAggressivelyFreeResources(true))
365 .After(visibility_true)
366 .WillOnce(testing::Invoke([this](bool is_visible) { EndTest(); }));
367 return output_surface.Pass();
370 void InitializeSettings(LayerTreeSettings* settings) override {
371 settings->gpu_rasterization_enabled = true;
372 settings->gpu_rasterization_forced = true;
375 void BeginTest() override {
376 // Logic is handled in InitializedRendererOnThread to ensure that our
377 // LTHI is fully set up.
380 void AfterTest() override {
381 // Expectations handled via mock.
384 private:
385 class MockSetWorkerContextShouldAggressivelyFreeResourcesOutputSurface
386 : public FakeOutputSurface {
387 public:
388 ~MockSetWorkerContextShouldAggressivelyFreeResourcesOutputSurface() {}
389 explicit MockSetWorkerContextShouldAggressivelyFreeResourcesOutputSurface(
390 bool delegated_rendering)
391 : FakeOutputSurface(TestContextProvider::Create(),
392 TestContextProvider::Create(),
393 delegated_rendering) {}
394 MOCK_METHOD1(SetWorkerContextShouldAggressivelyFreeResources,
395 void(bool is_visible));
399 // Test if the LTH successfully frees resources on the worker context when
400 // visibility is set to false.
401 class LayerTreeHostFreeWorkerContextResourcesOnInvisible
402 : public LayerTreeHostFreeWorkerContextResourcesTest {
403 public:
404 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
405 bool success) override {
406 PostSetVisibleToMainThread(false);
410 SINGLE_AND_MULTI_THREAD_TEST_F(
411 LayerTreeHostFreeWorkerContextResourcesOnInvisible);
413 // Test if the LTH successfully frees resources on the worker context when
414 // hard memory limit is set to zero.
415 class LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit
416 : public LayerTreeHostFreeWorkerContextResourcesTest {
417 public:
418 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
419 bool success) override {
420 ManagedMemoryPolicy zero_policy(
421 0, gpu::MemoryAllocation::CUTOFF_ALLOW_NOTHING, 0);
422 host_impl->SetMemoryPolicy(zero_policy);
426 SINGLE_AND_MULTI_THREAD_TEST_F(
427 LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit);
429 // Test if the LTH successfully frees resources on the worker context when
430 // hard memory limit is set to zero while using a synchronous compositor (like
431 // Android WebView).
432 class LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimitSynchronous
433 : public LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimit {
434 public:
435 void InitializeSettings(LayerTreeSettings* settings) override {
436 LayerTreeHostFreeWorkerContextResourcesTest::InitializeSettings(settings);
437 settings->use_external_begin_frame_source = true;
438 settings->using_synchronous_renderer_compositor = true;
442 SINGLE_AND_MULTI_THREAD_TEST_F(
443 LayerTreeHostFreeWorkerContextResourcesOnZeroMemoryLimitSynchronous);
445 // Two setNeedsCommits in a row should lead to at least 1 commit and at least 1
446 // draw with frame 0.
447 class LayerTreeHostTestSetNeedsCommit1 : public LayerTreeHostTest {
448 public:
449 LayerTreeHostTestSetNeedsCommit1() : num_commits_(0), num_draws_(0) {}
451 void BeginTest() override {
452 PostSetNeedsCommitToMainThread();
453 PostSetNeedsCommitToMainThread();
456 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
457 num_draws_++;
458 if (!impl->active_tree()->source_frame_number())
459 EndTest();
462 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
463 num_commits_++;
466 void AfterTest() override {
467 EXPECT_LE(1, num_commits_);
468 EXPECT_LE(1, num_draws_);
471 private:
472 int num_commits_;
473 int num_draws_;
476 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsCommit1);
478 // A SetNeedsCommit should lead to 1 commit. Issuing a second commit after that
479 // first committed frame draws should lead to another commit.
480 class LayerTreeHostTestSetNeedsCommit2 : public LayerTreeHostTest {
481 public:
482 LayerTreeHostTestSetNeedsCommit2() : num_commits_(0), num_draws_(0) {}
484 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
486 void DrawLayersOnThread(LayerTreeHostImpl* impl) override { ++num_draws_; }
488 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
489 ++num_commits_;
490 switch (num_commits_) {
491 case 1:
492 PostSetNeedsCommitToMainThread();
493 break;
494 case 2:
495 EndTest();
496 break;
497 default:
498 NOTREACHED();
502 void AfterTest() override {
503 EXPECT_EQ(2, num_commits_);
504 EXPECT_LE(1, num_draws_);
507 private:
508 int num_commits_;
509 int num_draws_;
512 MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsCommit2);
514 // Verify that we pass property values in PushPropertiesTo.
515 class LayerTreeHostTestPushPropertiesTo : public LayerTreeHostTest {
516 protected:
517 void SetupTree() override {
518 scoped_refptr<Layer> root = Layer::Create(layer_settings());
519 root->CreateRenderSurface();
520 root->SetBounds(gfx::Size(10, 10));
521 layer_tree_host()->SetRootLayer(root);
522 LayerTreeHostTest::SetupTree();
525 enum Properties {
526 STARTUP,
527 BOUNDS,
528 HIDE_LAYER_AND_SUBTREE,
529 DRAWS_CONTENT,
530 DONE,
533 void BeginTest() override {
534 index_ = STARTUP;
535 PostSetNeedsCommitToMainThread();
538 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
539 VerifyAfterValues(impl->active_tree()->root_layer());
542 void DidCommitAndDrawFrame() override {
543 SetBeforeValues(layer_tree_host()->root_layer());
544 VerifyBeforeValues(layer_tree_host()->root_layer());
546 ++index_;
547 if (index_ == DONE) {
548 EndTest();
549 return;
552 SetAfterValues(layer_tree_host()->root_layer());
555 void AfterTest() override {}
557 void VerifyBeforeValues(Layer* layer) {
558 EXPECT_EQ(gfx::Size(10, 10).ToString(), layer->bounds().ToString());
559 EXPECT_FALSE(layer->hide_layer_and_subtree());
560 EXPECT_FALSE(layer->DrawsContent());
563 void SetBeforeValues(Layer* layer) {
564 layer->SetBounds(gfx::Size(10, 10));
565 layer->SetHideLayerAndSubtree(false);
566 layer->SetIsDrawable(false);
569 void VerifyAfterValues(LayerImpl* layer) {
570 switch (static_cast<Properties>(index_)) {
571 case STARTUP:
572 case DONE:
573 break;
574 case BOUNDS:
575 EXPECT_EQ(gfx::Size(20, 20).ToString(), layer->bounds().ToString());
576 break;
577 case HIDE_LAYER_AND_SUBTREE:
578 EXPECT_TRUE(layer->hide_layer_and_subtree());
579 break;
580 case DRAWS_CONTENT:
581 EXPECT_TRUE(layer->DrawsContent());
582 break;
586 void SetAfterValues(Layer* layer) {
587 switch (static_cast<Properties>(index_)) {
588 case STARTUP:
589 case DONE:
590 break;
591 case BOUNDS:
592 layer->SetBounds(gfx::Size(20, 20));
593 break;
594 case HIDE_LAYER_AND_SUBTREE:
595 layer->SetHideLayerAndSubtree(true);
596 break;
597 case DRAWS_CONTENT:
598 layer->SetIsDrawable(true);
599 break;
603 int index_;
606 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestPushPropertiesTo);
608 // 1 setNeedsRedraw after the first commit has completed should lead to 1
609 // additional draw.
610 class LayerTreeHostTestSetNeedsRedraw : public LayerTreeHostTest {
611 public:
612 LayerTreeHostTestSetNeedsRedraw() : num_commits_(0), num_draws_(0) {}
614 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
616 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
617 EXPECT_EQ(0, impl->active_tree()->source_frame_number());
618 if (!num_draws_) {
619 // Redraw again to verify that the second redraw doesn't commit.
620 PostSetNeedsRedrawToMainThread();
621 } else {
622 EndTest();
624 num_draws_++;
627 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
628 EXPECT_EQ(0, num_draws_);
629 num_commits_++;
632 void AfterTest() override {
633 EXPECT_GE(2, num_draws_);
634 EXPECT_EQ(1, num_commits_);
637 private:
638 int num_commits_;
639 int num_draws_;
642 MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsRedraw);
644 // After setNeedsRedrawRect(invalid_rect) the final damage_rect
645 // must contain invalid_rect.
646 class LayerTreeHostTestSetNeedsRedrawRect : public LayerTreeHostTest {
647 public:
648 LayerTreeHostTestSetNeedsRedrawRect()
649 : num_draws_(0), bounds_(50, 50), invalid_rect_(10, 10, 20, 20) {}
651 void BeginTest() override {
652 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
653 root_layer_->SetIsDrawable(true);
654 root_layer_->SetBounds(bounds_);
655 layer_tree_host()->SetRootLayer(root_layer_);
656 layer_tree_host()->SetViewportSize(bounds_);
657 PostSetNeedsCommitToMainThread();
660 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
661 LayerTreeHostImpl::FrameData* frame_data,
662 DrawResult draw_result) override {
663 EXPECT_EQ(DRAW_SUCCESS, draw_result);
665 gfx::RectF root_damage_rect;
666 if (!frame_data->render_passes.empty())
667 root_damage_rect = frame_data->render_passes.back()->damage_rect;
669 if (!num_draws_) {
670 // If this is the first frame, expect full frame damage.
671 EXPECT_EQ(root_damage_rect, gfx::Rect(bounds_));
672 } else {
673 // Check that invalid_rect_ is indeed repainted.
674 EXPECT_TRUE(root_damage_rect.Contains(invalid_rect_));
677 return draw_result;
680 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
681 if (!num_draws_) {
682 PostSetNeedsRedrawRectToMainThread(invalid_rect_);
683 } else {
684 EndTest();
686 num_draws_++;
689 void AfterTest() override { EXPECT_EQ(2, num_draws_); }
691 private:
692 int num_draws_;
693 const gfx::Size bounds_;
694 const gfx::Rect invalid_rect_;
695 FakeContentLayerClient client_;
696 scoped_refptr<FakePictureLayer> root_layer_;
699 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsRedrawRect);
701 // Ensure the texture size of the pending and active trees are identical when a
702 // layer is not in the viewport and a resize happens on the viewport
703 class LayerTreeHostTestGpuRasterDeviceSizeChanged : public LayerTreeHostTest {
704 public:
705 LayerTreeHostTestGpuRasterDeviceSizeChanged()
706 : num_draws_(0), bounds_(500, 64), invalid_rect_(10, 10, 20, 20) {}
708 void BeginTest() override {
709 client_.set_fill_with_nonsolid_color(true);
710 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
711 root_layer_->SetIsDrawable(true);
712 gfx::Transform transform;
713 // Translate the layer out of the viewport to force it to not update its
714 // tile size via PushProperties.
715 transform.Translate(10000.0, 10000.0);
716 root_layer_->SetTransform(transform);
717 root_layer_->SetBounds(bounds_);
718 layer_tree_host()->SetRootLayer(root_layer_);
719 layer_tree_host()->SetViewportSize(bounds_);
721 PostSetNeedsCommitToMainThread();
724 void InitializeSettings(LayerTreeSettings* settings) override {
725 settings->gpu_rasterization_enabled = true;
726 settings->gpu_rasterization_forced = true;
729 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
730 // Perform 2 commits.
731 if (!num_draws_) {
732 PostSetNeedsRedrawRectToMainThread(invalid_rect_);
733 } else {
734 EndTest();
736 num_draws_++;
739 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
740 if (num_draws_ == 2) {
741 auto pending_tree = host_impl->pending_tree();
742 auto pending_layer_impl =
743 static_cast<FakePictureLayerImpl*>(pending_tree->root_layer());
744 EXPECT_NE(pending_layer_impl, nullptr);
746 auto active_tree = host_impl->pending_tree();
747 auto active_layer_impl =
748 static_cast<FakePictureLayerImpl*>(active_tree->root_layer());
749 EXPECT_NE(pending_layer_impl, nullptr);
751 auto active_tiling_set = active_layer_impl->picture_layer_tiling_set();
752 auto active_tiling = active_tiling_set->tiling_at(0);
753 auto pending_tiling_set = pending_layer_impl->picture_layer_tiling_set();
754 auto pending_tiling = pending_tiling_set->tiling_at(0);
755 EXPECT_EQ(
756 pending_tiling->TilingDataForTesting().max_texture_size().width(),
757 active_tiling->TilingDataForTesting().max_texture_size().width());
761 void DidCommitAndDrawFrame() override {
762 // On the second commit, resize the viewport.
763 if (num_draws_ == 1) {
764 layer_tree_host()->SetViewportSize(gfx::Size(400, 64));
768 void AfterTest() override {}
770 private:
771 int num_draws_;
772 const gfx::Size bounds_;
773 const gfx::Rect invalid_rect_;
774 FakeContentLayerClient client_;
775 scoped_refptr<FakePictureLayer> root_layer_;
778 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterDeviceSizeChanged);
780 class LayerTreeHostTestNoExtraCommitFromInvalidate : public LayerTreeHostTest {
781 public:
782 void InitializeSettings(LayerTreeSettings* settings) override {
783 settings->layer_transforms_should_scale_layer_contents = true;
786 void SetupTree() override {
787 root_layer_ = Layer::Create(layer_settings());
788 root_layer_->SetBounds(gfx::Size(10, 20));
789 root_layer_->CreateRenderSurface();
791 scaled_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
792 scaled_layer_->SetBounds(gfx::Size(1, 1));
793 root_layer_->AddChild(scaled_layer_);
795 layer_tree_host()->SetRootLayer(root_layer_);
796 LayerTreeHostTest::SetupTree();
799 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
801 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
802 if (host_impl->active_tree()->source_frame_number() == 1)
803 EndTest();
806 void DidCommit() override {
807 switch (layer_tree_host()->source_frame_number()) {
808 case 1:
809 // SetBounds grows the layer and exposes new content.
810 scaled_layer_->SetBounds(gfx::Size(4, 4));
811 break;
812 default:
813 // No extra commits.
814 EXPECT_EQ(2, layer_tree_host()->source_frame_number());
818 void AfterTest() override {
819 EXPECT_EQ(gfx::Size(4, 4).ToString(), scaled_layer_->bounds().ToString());
822 private:
823 FakeContentLayerClient client_;
824 scoped_refptr<Layer> root_layer_;
825 scoped_refptr<Layer> scaled_layer_;
828 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestNoExtraCommitFromInvalidate);
830 class LayerTreeHostTestNoExtraCommitFromScrollbarInvalidate
831 : public LayerTreeHostTest {
832 public:
833 void InitializeSettings(LayerTreeSettings* settings) override {
834 settings->layer_transforms_should_scale_layer_contents = true;
837 void SetupTree() override {
838 root_layer_ = Layer::Create(layer_settings());
839 root_layer_->SetBounds(gfx::Size(10, 20));
840 root_layer_->CreateRenderSurface();
842 bool paint_scrollbar = true;
843 bool has_thumb = false;
844 scrollbar_ = FakePaintedScrollbarLayer::Create(
845 layer_settings(), paint_scrollbar, has_thumb, root_layer_->id());
846 scrollbar_->SetPosition(gfx::Point(0, 10));
847 scrollbar_->SetBounds(gfx::Size(10, 10));
849 root_layer_->AddChild(scrollbar_);
851 layer_tree_host()->SetRootLayer(root_layer_);
852 LayerTreeHostTest::SetupTree();
855 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
857 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
858 if (host_impl->active_tree()->source_frame_number() == 1)
859 EndTest();
862 void DidCommit() override {
863 switch (layer_tree_host()->source_frame_number()) {
864 case 1:
865 // Changing the device scale factor causes a commit. It also changes
866 // the content bounds of |scrollbar_|, which should not generate
867 // a second commit as a result.
868 layer_tree_host()->SetDeviceScaleFactor(4.f);
869 break;
870 default:
871 // No extra commits.
872 EXPECT_EQ(2, layer_tree_host()->source_frame_number());
873 break;
877 void AfterTest() override {
880 private:
881 FakeContentLayerClient client_;
882 scoped_refptr<Layer> root_layer_;
883 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_;
886 SINGLE_AND_MULTI_THREAD_TEST_F(
887 LayerTreeHostTestNoExtraCommitFromScrollbarInvalidate);
889 class LayerTreeHostTestSetNextCommitForcesRedraw : public LayerTreeHostTest {
890 public:
891 LayerTreeHostTestSetNextCommitForcesRedraw()
892 : num_draws_(0), bounds_(50, 50), invalid_rect_(10, 10, 20, 20) {}
894 void BeginTest() override {
895 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
896 root_layer_->SetIsDrawable(true);
897 root_layer_->SetBounds(bounds_);
898 layer_tree_host()->SetRootLayer(root_layer_);
899 layer_tree_host()->SetViewportSize(bounds_);
900 PostSetNeedsCommitToMainThread();
903 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
904 if (num_draws_ == 3)
905 host_impl->SetNeedsRedrawRect(invalid_rect_);
908 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
909 LayerTreeHostImpl::FrameData* frame_data,
910 DrawResult draw_result) override {
911 EXPECT_EQ(DRAW_SUCCESS, draw_result);
913 gfx::RectF root_damage_rect;
914 if (!frame_data->render_passes.empty())
915 root_damage_rect = frame_data->render_passes.back()->damage_rect;
917 switch (num_draws_) {
918 case 0:
919 EXPECT_EQ(gfx::Rect(bounds_), root_damage_rect);
920 break;
921 case 1:
922 case 2:
923 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root_damage_rect);
924 break;
925 case 3:
926 EXPECT_EQ(invalid_rect_, root_damage_rect);
927 break;
928 case 4:
929 EXPECT_EQ(gfx::Rect(bounds_), root_damage_rect);
930 break;
931 default:
932 NOTREACHED();
935 return draw_result;
938 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
939 switch (num_draws_) {
940 case 0:
941 case 1:
942 // Cycle through a couple of empty commits to ensure we're observing the
943 // right behavior
944 PostSetNeedsCommitToMainThread();
945 break;
946 case 2:
947 // Should force full frame damage on the next commit
948 PostSetNextCommitForcesRedrawToMainThread();
949 PostSetNeedsCommitToMainThread();
950 host_impl->BlockNotifyReadyToActivateForTesting(true);
951 break;
952 case 3:
953 host_impl->BlockNotifyReadyToActivateForTesting(false);
954 break;
955 default:
956 EndTest();
957 break;
959 num_draws_++;
962 void AfterTest() override { EXPECT_EQ(5, num_draws_); }
964 private:
965 int num_draws_;
966 const gfx::Size bounds_;
967 const gfx::Rect invalid_rect_;
968 FakeContentLayerClient client_;
969 scoped_refptr<FakePictureLayer> root_layer_;
972 // This test blocks activation which is not supported for single thread mode.
973 MULTI_THREAD_BLOCKNOTIFY_TEST_F(LayerTreeHostTestSetNextCommitForcesRedraw);
975 // Tests that if a layer is not drawn because of some reason in the parent then
976 // its damage is preserved until the next time it is drawn.
977 class LayerTreeHostTestUndrawnLayersDamageLater : public LayerTreeHostTest {
978 public:
979 void InitializeSettings(LayerTreeSettings* settings) override {
980 // If we don't set the minimum contents scale, it's harder to verify whether
981 // the damage we get is correct. For other scale amounts, please see
982 // LayerTreeHostTestDamageWithScale.
983 settings->minimum_contents_scale = 1.f;
986 void SetupTree() override {
987 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
988 root_layer_->SetIsDrawable(true);
989 root_layer_->SetBounds(gfx::Size(50, 50));
990 layer_tree_host()->SetRootLayer(root_layer_);
992 // The initially transparent layer has a larger child layer, which is
993 // not initially drawn because of the this (parent) layer.
994 parent_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
995 parent_layer_->SetBounds(gfx::Size(15, 15));
996 parent_layer_->SetOpacity(0.0f);
997 root_layer_->AddChild(parent_layer_);
999 child_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
1000 child_layer_->SetBounds(gfx::Size(25, 25));
1001 parent_layer_->AddChild(child_layer_);
1003 LayerTreeHostTest::SetupTree();
1006 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1008 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
1009 LayerTreeHostImpl::FrameData* frame_data,
1010 DrawResult draw_result) override {
1011 EXPECT_EQ(DRAW_SUCCESS, draw_result);
1013 gfx::RectF root_damage_rect;
1014 if (!frame_data->render_passes.empty())
1015 root_damage_rect = frame_data->render_passes.back()->damage_rect;
1017 // The first time, the whole view needs be drawn.
1018 // Afterwards, just the opacity of surface_layer1 is changed a few times,
1019 // and each damage should be the bounding box of it and its child. If this
1020 // was working improperly, the damage might not include its childs bounding
1021 // box.
1022 switch (host_impl->active_tree()->source_frame_number()) {
1023 case 0:
1024 EXPECT_EQ(gfx::Rect(root_layer_->bounds()), root_damage_rect);
1025 break;
1026 case 1:
1027 case 2:
1028 case 3:
1029 EXPECT_EQ(gfx::Rect(child_layer_->bounds()), root_damage_rect);
1030 break;
1031 default:
1032 NOTREACHED();
1035 return draw_result;
1038 void DidCommitAndDrawFrame() override {
1039 switch (layer_tree_host()->source_frame_number()) {
1040 case 1:
1041 // Test not owning the surface.
1042 parent_layer_->SetOpacity(1.0f);
1043 break;
1044 case 2:
1045 parent_layer_->SetOpacity(0.0f);
1046 break;
1047 case 3:
1048 // Test owning the surface.
1049 parent_layer_->SetOpacity(0.5f);
1050 parent_layer_->SetForceRenderSurface(true);
1051 break;
1052 case 4:
1053 EndTest();
1054 break;
1055 default:
1056 NOTREACHED();
1060 void AfterTest() override {}
1062 private:
1063 FakeContentLayerClient client_;
1064 scoped_refptr<FakePictureLayer> root_layer_;
1065 scoped_refptr<FakePictureLayer> parent_layer_;
1066 scoped_refptr<FakePictureLayer> child_layer_;
1069 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestUndrawnLayersDamageLater);
1071 // Tests that if a layer is not drawn because of some reason in the parent then
1072 // its damage is preserved until the next time it is drawn.
1073 class LayerTreeHostTestDamageWithScale : public LayerTreeHostTest {
1074 public:
1075 LayerTreeHostTestDamageWithScale() {}
1077 void SetupTree() override {
1078 client_.set_fill_with_nonsolid_color(true);
1080 scoped_ptr<FakePicturePile> pile(
1081 new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
1082 LayerTreeSettings().default_tile_grid_size));
1083 root_layer_ = FakePictureLayer::CreateWithRecordingSource(
1084 layer_settings(), &client_, pile.Pass());
1085 root_layer_->SetBounds(gfx::Size(50, 50));
1087 pile.reset(new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
1088 LayerTreeSettings().default_tile_grid_size));
1089 child_layer_ = FakePictureLayer::CreateWithRecordingSource(
1090 layer_settings(), &client_, pile.Pass());
1091 child_layer_->SetBounds(gfx::Size(25, 25));
1092 child_layer_->SetIsDrawable(true);
1093 child_layer_->SetContentsOpaque(true);
1094 root_layer_->AddChild(child_layer_);
1096 layer_tree_host()->SetRootLayer(root_layer_);
1097 LayerTreeHostTest::SetupTree();
1100 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
1101 // Force the layer to have a tiling at 1.3f scale. Note that if we simply
1102 // add tiling, it will be gone by the time we draw because of aggressive
1103 // cleanup. AddTilingUntilNextDraw ensures that it remains there during
1104 // damage calculation.
1105 FakePictureLayerImpl* child_layer_impl = static_cast<FakePictureLayerImpl*>(
1106 host_impl->active_tree()->LayerById(child_layer_->id()));
1107 child_layer_impl->AddTilingUntilNextDraw(1.3f);
1110 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1112 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
1113 LayerTreeHostImpl::FrameData* frame_data,
1114 DrawResult draw_result) override {
1115 EXPECT_EQ(DRAW_SUCCESS, draw_result);
1117 gfx::RectF root_damage_rect;
1118 if (!frame_data->render_passes.empty())
1119 root_damage_rect = frame_data->render_passes.back()->damage_rect;
1121 // The first time, the whole view needs be drawn.
1122 // Afterwards, just the opacity of surface_layer1 is changed a few times,
1123 // and each damage should be the bounding box of it and its child. If this
1124 // was working improperly, the damage might not include its childs bounding
1125 // box.
1126 switch (host_impl->active_tree()->source_frame_number()) {
1127 case 0:
1128 EXPECT_EQ(gfx::Rect(root_layer_->bounds()), root_damage_rect);
1129 break;
1130 case 1: {
1131 FakePictureLayerImpl* child_layer_impl =
1132 static_cast<FakePictureLayerImpl*>(
1133 host_impl->active_tree()->LayerById(child_layer_->id()));
1134 // We remove tilings pretty aggressively if they are not ideal. Add this
1135 // back in so that we can compare
1136 // child_layer_impl->GetEnclosingRectInTargetSpace to the damage.
1137 child_layer_impl->AddTilingUntilNextDraw(1.3f);
1139 EXPECT_EQ(gfx::Rect(26, 26), root_damage_rect);
1140 EXPECT_EQ(child_layer_impl->GetEnclosingRectInTargetSpace(),
1141 root_damage_rect);
1142 EXPECT_TRUE(child_layer_impl->GetEnclosingRectInTargetSpace().Contains(
1143 gfx::Rect(child_layer_->bounds())));
1144 break;
1146 default:
1147 NOTREACHED();
1150 return draw_result;
1153 void DidCommitAndDrawFrame() override {
1154 switch (layer_tree_host()->source_frame_number()) {
1155 case 1: {
1156 // Test not owning the surface.
1157 child_layer_->SetOpacity(0.5f);
1158 break;
1160 case 2:
1161 EndTest();
1162 break;
1163 default:
1164 NOTREACHED();
1168 void AfterTest() override {}
1170 private:
1171 FakeContentLayerClient client_;
1172 scoped_refptr<Layer> root_layer_;
1173 scoped_refptr<Layer> child_layer_;
1176 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestDamageWithScale);
1178 // This test verifies that properties on the layer tree host are commited
1179 // to the impl side.
1180 class LayerTreeHostTestCommit : public LayerTreeHostTest {
1181 public:
1182 LayerTreeHostTestCommit() {}
1184 void BeginTest() override {
1185 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
1186 layer_tree_host()->set_background_color(SK_ColorGRAY);
1188 PostSetNeedsCommitToMainThread();
1191 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1192 EXPECT_EQ(gfx::Size(20, 20), impl->DrawViewportSize());
1193 EXPECT_EQ(SK_ColorGRAY, impl->active_tree()->background_color());
1195 EndTest();
1198 void AfterTest() override {}
1201 MULTI_THREAD_TEST_F(LayerTreeHostTestCommit);
1203 // This test verifies that LayerTreeHostImpl's current frame time gets
1204 // updated in consecutive frames when it doesn't draw due to tree
1205 // activation failure.
1206 class LayerTreeHostTestFrameTimeUpdatesAfterActivationFails
1207 : public LayerTreeHostTest {
1208 public:
1209 LayerTreeHostTestFrameTimeUpdatesAfterActivationFails()
1210 : frame_count_with_pending_tree_(0) {}
1212 void BeginTest() override {
1213 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
1214 layer_tree_host()->set_background_color(SK_ColorGRAY);
1216 PostSetNeedsCommitToMainThread();
1219 void BeginCommitOnThread(LayerTreeHostImpl* impl) override {
1220 EXPECT_EQ(frame_count_with_pending_tree_, 0);
1221 impl->BlockNotifyReadyToActivateForTesting(true);
1224 void WillBeginImplFrameOnThread(LayerTreeHostImpl* impl,
1225 const BeginFrameArgs& args) override {
1226 if (impl->pending_tree())
1227 frame_count_with_pending_tree_++;
1229 if (frame_count_with_pending_tree_ == 1) {
1230 EXPECT_EQ(first_frame_time_.ToInternalValue(), 0);
1231 first_frame_time_ = impl->CurrentBeginFrameArgs().frame_time;
1232 } else if (frame_count_with_pending_tree_ == 2) {
1233 impl->BlockNotifyReadyToActivateForTesting(false);
1237 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
1238 EXPECT_GT(frame_count_with_pending_tree_, 1);
1239 EXPECT_NE(first_frame_time_.ToInternalValue(), 0);
1240 EXPECT_NE(first_frame_time_.ToInternalValue(),
1241 impl->CurrentBeginFrameArgs().frame_time.ToInternalValue());
1242 EndTest();
1245 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1246 EXPECT_GT(frame_count_with_pending_tree_, 1);
1249 void AfterTest() override {}
1251 private:
1252 int frame_count_with_pending_tree_;
1253 base::TimeTicks first_frame_time_;
1256 // This test blocks activation which is not supported for single thread mode.
1257 MULTI_THREAD_BLOCKNOTIFY_TEST_F(
1258 LayerTreeHostTestFrameTimeUpdatesAfterActivationFails);
1260 // This test verifies that LayerTreeHostImpl's current frame time gets
1261 // updated in consecutive frames when it draws in each frame.
1262 class LayerTreeHostTestFrameTimeUpdatesAfterDraw : public LayerTreeHostTest {
1263 public:
1264 LayerTreeHostTestFrameTimeUpdatesAfterDraw() : frame_(0) {}
1266 void BeginTest() override {
1267 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
1268 layer_tree_host()->set_background_color(SK_ColorGRAY);
1270 PostSetNeedsCommitToMainThread();
1273 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
1274 frame_++;
1275 if (frame_ == 1) {
1276 first_frame_time_ = impl->CurrentBeginFrameArgs().frame_time;
1277 impl->SetNeedsRedraw();
1279 // Since we might use a low-resolution clock on Windows, we need to
1280 // make sure that the clock has incremented past first_frame_time_.
1281 while (first_frame_time_ == base::TimeTicks::Now()) {
1284 return;
1287 EXPECT_NE(first_frame_time_, impl->CurrentBeginFrameArgs().frame_time);
1288 EndTest();
1291 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
1292 // Ensure there isn't a commit between the two draws, to ensure that a
1293 // commit isn't required for updating the current frame time. We can
1294 // only check for this in the multi-threaded case, since in the single-
1295 // threaded case there will always be a commit between consecutive draws.
1296 if (HasImplThread())
1297 EXPECT_EQ(0, frame_);
1300 void AfterTest() override {}
1302 private:
1303 int frame_;
1304 base::TimeTicks first_frame_time_;
1307 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestFrameTimeUpdatesAfterDraw);
1309 // Verifies that StartPageScaleAnimation events propagate correctly
1310 // from LayerTreeHost to LayerTreeHostImpl in the MT compositor.
1311 class LayerTreeHostTestStartPageScaleAnimation : public LayerTreeHostTest {
1312 public:
1313 LayerTreeHostTestStartPageScaleAnimation() {}
1315 void SetupTree() override {
1316 LayerTreeHostTest::SetupTree();
1318 Layer* root_layer = layer_tree_host()->root_layer();
1320 scoped_refptr<FakePictureLayer> layer =
1321 FakePictureLayer::Create(layer_settings(), &client_);
1322 layer->set_always_update_resources(true);
1323 scroll_layer_ = layer;
1325 scroll_layer_->SetBounds(gfx::Size(2 * root_layer->bounds().width(),
1326 2 * root_layer->bounds().height()));
1327 scroll_layer_->SetScrollOffset(gfx::ScrollOffset());
1329 CreateVirtualViewportLayers(root_layer,
1330 scroll_layer_,
1331 root_layer->bounds(),
1332 root_layer->bounds(),
1333 layer_tree_host(),
1334 layer_settings());
1336 layer_tree_host()->SetPageScaleFactorAndLimits(1.f, 0.5f, 2.f);
1339 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1341 void ApplyViewportDeltas(const gfx::Vector2dF& scroll_delta,
1342 const gfx::Vector2dF&,
1343 const gfx::Vector2dF& elastic_overscroll_delta,
1344 float scale,
1345 float) override {
1346 gfx::ScrollOffset offset = scroll_layer_->scroll_offset();
1347 scroll_layer_->SetScrollOffset(ScrollOffsetWithDelta(offset,
1348 scroll_delta));
1349 layer_tree_host()->SetPageScaleFactorAndLimits(scale, 0.5f, 2.f);
1352 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1353 // We get one commit before the first draw, and the animation doesn't happen
1354 // until the second draw.
1355 switch (impl->active_tree()->source_frame_number()) {
1356 case 0:
1357 EXPECT_EQ(1.f, impl->active_tree()->current_page_scale_factor());
1358 // We'll start an animation when we get back to the main thread.
1359 break;
1360 case 1:
1361 EXPECT_EQ(1.f, impl->active_tree()->current_page_scale_factor());
1362 break;
1363 case 2:
1364 EXPECT_EQ(1.25f, impl->active_tree()->current_page_scale_factor());
1365 EndTest();
1366 break;
1367 default:
1368 NOTREACHED();
1372 void DidCommitAndDrawFrame() override {
1373 switch (layer_tree_host()->source_frame_number()) {
1374 case 1:
1375 layer_tree_host()->StartPageScaleAnimation(
1376 gfx::Vector2d(), false, 1.25f, base::TimeDelta());
1377 break;
1381 void AfterTest() override {}
1383 FakeContentLayerClient client_;
1384 scoped_refptr<Layer> scroll_layer_;
1387 // Single thread proxy does not support impl-side page scale changes.
1388 MULTI_THREAD_TEST_F(LayerTreeHostTestStartPageScaleAnimation);
1390 class LayerTreeHostTestSetVisible : public LayerTreeHostTest {
1391 public:
1392 LayerTreeHostTestSetVisible() : num_draws_(0) {}
1394 void BeginTest() override {
1395 PostSetNeedsCommitToMainThread();
1396 PostSetVisibleToMainThread(false);
1397 // This is suppressed while we're invisible.
1398 PostSetNeedsRedrawToMainThread();
1399 // Triggers the redraw.
1400 PostSetVisibleToMainThread(true);
1403 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
1404 EXPECT_TRUE(impl->visible());
1405 ++num_draws_;
1406 EndTest();
1409 void AfterTest() override { EXPECT_EQ(1, num_draws_); }
1411 private:
1412 int num_draws_;
1415 MULTI_THREAD_TEST_F(LayerTreeHostTestSetVisible);
1417 class TestOpacityChangeLayerDelegate : public ContentLayerClient {
1418 public:
1419 TestOpacityChangeLayerDelegate() : test_layer_(0) {}
1421 void SetTestLayer(Layer* test_layer) { test_layer_ = test_layer; }
1423 void PaintContents(SkCanvas* canvas,
1424 const gfx::Rect& clip,
1425 PaintingControlSetting picture_control) override {
1426 // Set layer opacity to 0.
1427 if (test_layer_)
1428 test_layer_->SetOpacity(0.f);
1430 scoped_refptr<DisplayItemList> PaintContentsToDisplayList(
1431 const gfx::Rect& clip,
1432 PaintingControlSetting picture_control) override {
1433 NOTIMPLEMENTED();
1434 return nullptr;
1436 bool FillsBoundsCompletely() const override { return false; }
1437 size_t GetApproximateUnsharedMemoryUsage() const override { return 0; }
1439 private:
1440 Layer* test_layer_;
1443 // Layer opacity change during paint should not prevent compositor resources
1444 // from being updated during commit.
1445 class LayerTreeHostTestOpacityChange : public LayerTreeHostTest {
1446 public:
1447 LayerTreeHostTestOpacityChange() : test_opacity_change_delegate_() {}
1449 void SetupTree() override {
1450 LayerTreeHostTest::SetupTree();
1452 update_check_picture_layer_ = FakePictureLayer::Create(
1453 layer_settings(), &test_opacity_change_delegate_);
1454 test_opacity_change_delegate_.SetTestLayer(
1455 update_check_picture_layer_.get());
1456 layer_tree_host()->root_layer()->AddChild(update_check_picture_layer_);
1459 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1461 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override { EndTest(); }
1463 void AfterTest() override {
1464 // Update() should have been called once.
1465 EXPECT_EQ(1, update_check_picture_layer_->update_count());
1468 private:
1469 TestOpacityChangeLayerDelegate test_opacity_change_delegate_;
1470 scoped_refptr<FakePictureLayer> update_check_picture_layer_;
1473 MULTI_THREAD_TEST_F(LayerTreeHostTestOpacityChange);
1475 class LayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers
1476 : public LayerTreeHostTest {
1477 public:
1478 LayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers() {}
1480 void BeginTest() override {
1481 client_.set_fill_with_nonsolid_color(true);
1482 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
1483 child_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
1485 layer_tree_host()->SetViewportSize(gfx::Size(60, 60));
1486 layer_tree_host()->SetDeviceScaleFactor(1.5);
1487 EXPECT_EQ(gfx::Size(60, 60), layer_tree_host()->device_viewport_size());
1489 root_layer_->AddChild(child_layer_);
1491 root_layer_->SetIsDrawable(true);
1492 root_layer_->SetBounds(gfx::Size(30, 30));
1494 child_layer_->SetIsDrawable(true);
1495 child_layer_->SetPosition(gfx::Point(2, 2));
1496 child_layer_->SetBounds(gfx::Size(10, 10));
1498 layer_tree_host()->SetRootLayer(root_layer_);
1500 PostSetNeedsCommitToMainThread();
1503 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
1504 // Should only do one commit.
1505 EXPECT_EQ(0, impl->active_tree()->source_frame_number());
1506 // Device scale factor should come over to impl.
1507 EXPECT_NEAR(impl->device_scale_factor(), 1.5f, 0.00001f);
1509 // Both layers are on impl.
1510 ASSERT_EQ(1u, impl->active_tree()->root_layer()->children().size());
1512 // Device viewport is scaled.
1513 EXPECT_EQ(gfx::Size(60, 60), impl->DrawViewportSize());
1515 FakePictureLayerImpl* root =
1516 static_cast<FakePictureLayerImpl*>(impl->active_tree()->root_layer());
1517 FakePictureLayerImpl* child = static_cast<FakePictureLayerImpl*>(
1518 impl->active_tree()->root_layer()->children()[0]);
1520 // Positions remain in layout pixels.
1521 EXPECT_EQ(gfx::Point(0, 0), root->position());
1522 EXPECT_EQ(gfx::Point(2, 2), child->position());
1524 // Compute all the layer transforms for the frame.
1525 LayerTreeHostImpl::FrameData frame_data;
1526 impl->PrepareToDraw(&frame_data);
1527 impl->DidDrawAllLayers(frame_data);
1529 const LayerImplList& render_surface_layer_list =
1530 *frame_data.render_surface_layer_list;
1532 // Both layers should be drawing into the root render surface.
1533 ASSERT_EQ(1u, render_surface_layer_list.size());
1534 ASSERT_EQ(root->render_surface(),
1535 render_surface_layer_list[0]->render_surface());
1536 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
1538 // The root render surface is the size of the viewport.
1539 EXPECT_EQ(gfx::Rect(0, 0, 60, 60), root->render_surface()->content_rect());
1541 // The max tiling scale of the child should be scaled.
1542 EXPECT_FLOAT_EQ(1.5f, child->MaximumTilingContentsScale());
1544 gfx::Transform scale_transform;
1545 scale_transform.Scale(impl->device_scale_factor(),
1546 impl->device_scale_factor());
1548 // The root layer is scaled by 2x.
1549 gfx::Transform root_screen_space_transform = scale_transform;
1550 gfx::Transform root_draw_transform = scale_transform;
1552 EXPECT_EQ(root_draw_transform, root->draw_transform());
1553 EXPECT_EQ(root_screen_space_transform, root->screen_space_transform());
1555 // The child is at position 2,2, which is transformed to 3,3 after the scale
1556 gfx::Transform child_transform;
1557 child_transform.Translate(3.f, 3.f);
1558 child_transform.Scale(child->MaximumTilingContentsScale(),
1559 child->MaximumTilingContentsScale());
1561 EXPECT_TRANSFORMATION_MATRIX_EQ(child_transform, child->draw_transform());
1562 EXPECT_TRANSFORMATION_MATRIX_EQ(child_transform,
1563 child->screen_space_transform());
1565 EndTest();
1568 void AfterTest() override {}
1570 private:
1571 FakeContentLayerClient client_;
1572 scoped_refptr<FakePictureLayer> root_layer_;
1573 scoped_refptr<FakePictureLayer> child_layer_;
1576 MULTI_THREAD_TEST_F(LayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers);
1578 class LayerTreeHostTestContinuousInvalidate : public LayerTreeHostTest {
1579 public:
1580 LayerTreeHostTestContinuousInvalidate()
1581 : num_commit_complete_(0), num_draw_layers_(0) {}
1583 void BeginTest() override {
1584 layer_tree_host()->SetViewportSize(gfx::Size(10, 10));
1585 layer_tree_host()->root_layer()->SetBounds(gfx::Size(10, 10));
1587 layer_ = FakePictureLayer::Create(layer_settings(), &client_);
1588 layer_->SetBounds(gfx::Size(10, 10));
1589 layer_->SetPosition(gfx::PointF(0.f, 0.f));
1590 layer_->SetIsDrawable(true);
1591 layer_tree_host()->root_layer()->AddChild(layer_);
1593 PostSetNeedsCommitToMainThread();
1596 void DidCommitAndDrawFrame() override {
1597 if (num_draw_layers_ == 2)
1598 return;
1599 layer_->SetNeedsDisplay();
1602 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
1603 if (num_draw_layers_ == 1)
1604 num_commit_complete_++;
1607 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
1608 num_draw_layers_++;
1609 if (num_draw_layers_ == 2)
1610 EndTest();
1613 void AfterTest() override {
1614 // Check that we didn't commit twice between first and second draw.
1615 EXPECT_EQ(1, num_commit_complete_);
1618 private:
1619 FakeContentLayerClient client_;
1620 scoped_refptr<Layer> layer_;
1621 int num_commit_complete_;
1622 int num_draw_layers_;
1625 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestContinuousInvalidate);
1627 class LayerTreeHostTestDeferCommits : public LayerTreeHostTest {
1628 public:
1629 LayerTreeHostTestDeferCommits()
1630 : num_will_begin_impl_frame_(0),
1631 num_send_begin_main_frame_(0) {}
1633 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1635 void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl,
1636 const BeginFrameArgs& args) override {
1637 num_will_begin_impl_frame_++;
1638 switch (num_will_begin_impl_frame_) {
1639 case 1:
1640 break;
1641 case 2:
1642 case 3:
1643 case 4:
1644 // Post a number of frames to increase the chance that, if there exist
1645 // bugs, an unexpected BeginMainFrame will be issued.
1646 PostSetNeedsCommitToMainThread();
1647 PostSetNeedsRedrawToMainThread();
1648 break;
1649 case 5:
1650 PostSetDeferCommitsToMainThread(false);
1651 break;
1652 default:
1653 // Sometimes |num_will_begin_impl_frame_| will be greater than 5 if the
1654 // main thread is slow to respond.
1655 break;
1659 void ScheduledActionSendBeginMainFrame() override {
1660 num_send_begin_main_frame_++;
1661 switch (num_send_begin_main_frame_) {
1662 case 1:
1663 PostSetDeferCommitsToMainThread(true);
1664 break;
1665 case 2:
1666 EndTest();
1667 break;
1668 default:
1669 NOTREACHED();
1670 break;
1674 void AfterTest() override {
1675 EXPECT_GE(num_will_begin_impl_frame_, 5);
1676 EXPECT_EQ(2, num_send_begin_main_frame_);
1679 private:
1680 int num_will_begin_impl_frame_;
1681 int num_send_begin_main_frame_;
1684 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestDeferCommits);
1686 class LayerTreeHostTestCompositeImmediatelyStateTransitions
1687 : public LayerTreeHostTest {
1688 public:
1689 enum {
1690 kInvalid,
1691 kStartedTest,
1692 kStartedImplFrame,
1693 kStartedMainFrame,
1694 kStartedCommit,
1695 kCompletedCommit,
1696 kCompletedMainFrame,
1697 kCompletedImplFrame,
1700 LayerTreeHostTestCompositeImmediatelyStateTransitions()
1701 : current_state_(kInvalid), current_begin_frame_args_() {}
1703 void InitializeSettings(LayerTreeSettings* settings) override {
1704 settings->single_thread_proxy_scheduler = false;
1705 settings->use_zero_copy = true;
1706 settings->use_one_copy = false;
1709 void BeginTest() override {
1710 current_state_ = kStartedTest;
1711 PostCompositeImmediatelyToMainThread();
1714 void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl,
1715 const BeginFrameArgs& args) override {
1716 EXPECT_EQ(current_state_, kStartedTest);
1717 current_state_ = kStartedImplFrame;
1719 EXPECT_FALSE(current_begin_frame_args_.IsValid());
1720 EXPECT_TRUE(args.IsValid());
1721 current_begin_frame_args_ = args;
1723 void WillBeginMainFrame() override {
1724 EXPECT_EQ(current_state_, kStartedImplFrame);
1725 current_state_ = kStartedMainFrame;
1727 void BeginMainFrame(const BeginFrameArgs& args) override {
1728 EXPECT_EQ(current_state_, kStartedMainFrame);
1729 EXPECT_EQ(args.frame_time, current_begin_frame_args_.frame_time);
1731 void BeginCommitOnThread(LayerTreeHostImpl* host_impl) override {
1732 EXPECT_EQ(current_state_, kStartedMainFrame);
1733 current_state_ = kStartedCommit;
1735 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
1736 EXPECT_EQ(current_state_, kStartedCommit);
1737 current_state_ = kCompletedCommit;
1739 void DidBeginMainFrame() override {
1740 EXPECT_EQ(current_state_, kCompletedCommit);
1741 current_state_ = kCompletedMainFrame;
1743 void DidFinishImplFrameOnThread(LayerTreeHostImpl* host_impl) override {
1744 EXPECT_EQ(current_state_, kCompletedMainFrame);
1745 current_state_ = kCompletedImplFrame;
1746 EndTest();
1748 void AfterTest() override { EXPECT_EQ(current_state_, kCompletedImplFrame); }
1750 private:
1751 int current_state_;
1752 BeginFrameArgs current_begin_frame_args_;
1755 SINGLE_THREAD_TEST_F(LayerTreeHostTestCompositeImmediatelyStateTransitions);
1757 class LayerTreeHostTestLCDChange : public LayerTreeHostTest {
1758 public:
1759 void SetupTree() override {
1760 num_tiles_rastered_ = 0;
1762 scoped_refptr<Layer> root_layer =
1763 PictureLayer::Create(layer_settings(), &client_);
1764 client_.set_fill_with_nonsolid_color(true);
1765 root_layer->SetIsDrawable(true);
1766 root_layer->SetBounds(gfx::Size(10, 10));
1767 root_layer->SetContentsOpaque(true);
1769 layer_tree_host()->SetRootLayer(root_layer);
1771 // The expectations are based on the assumption that the default
1772 // LCD settings are:
1773 EXPECT_TRUE(layer_tree_host()->settings().can_use_lcd_text);
1775 LayerTreeHostTest::SetupTree();
1778 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1780 void DidCommitAndDrawFrame() override {
1781 switch (layer_tree_host()->source_frame_number()) {
1782 case 1:
1783 PostSetNeedsCommitToMainThread();
1784 break;
1785 case 2:
1786 // Change layer opacity that should trigger lcd change.
1787 layer_tree_host()->root_layer()->SetOpacity(.5f);
1788 break;
1789 case 3:
1790 // Change layer opacity that should not trigger lcd change.
1791 layer_tree_host()->root_layer()->SetOpacity(1.f);
1792 break;
1793 case 4:
1794 EndTest();
1795 break;
1799 void NotifyTileStateChangedOnThread(LayerTreeHostImpl* host_impl,
1800 const Tile* tile) override {
1801 ++num_tiles_rastered_;
1804 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
1805 PictureLayerImpl* root_layer =
1806 static_cast<PictureLayerImpl*>(host_impl->active_tree()->root_layer());
1807 bool can_use_lcd_text =
1808 host_impl->active_tree()->root_layer()->can_use_lcd_text();
1809 switch (host_impl->active_tree()->source_frame_number()) {
1810 case 0:
1811 // The first draw.
1812 EXPECT_EQ(1, num_tiles_rastered_);
1813 EXPECT_TRUE(can_use_lcd_text);
1814 EXPECT_TRUE(root_layer->RasterSourceUsesLCDText());
1815 break;
1816 case 1:
1817 // Nothing changed on the layer.
1818 EXPECT_EQ(1, num_tiles_rastered_);
1819 EXPECT_TRUE(can_use_lcd_text);
1820 EXPECT_TRUE(root_layer->RasterSourceUsesLCDText());
1821 break;
1822 case 2:
1823 // LCD text was disabled; it should be re-rastered with LCD text off.
1824 EXPECT_EQ(2, num_tiles_rastered_);
1825 EXPECT_FALSE(can_use_lcd_text);
1826 EXPECT_FALSE(root_layer->RasterSourceUsesLCDText());
1827 break;
1828 case 3:
1829 // LCD text was enabled, but it's sticky and stays off.
1830 EXPECT_EQ(2, num_tiles_rastered_);
1831 EXPECT_TRUE(can_use_lcd_text);
1832 EXPECT_FALSE(root_layer->RasterSourceUsesLCDText());
1833 break;
1837 void AfterTest() override {}
1839 private:
1840 FakeContentLayerClient client_;
1841 int num_tiles_rastered_;
1844 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestLCDChange);
1846 // Verify that the BeginFrame notification is used to initiate rendering.
1847 class LayerTreeHostTestBeginFrameNotification : public LayerTreeHostTest {
1848 public:
1849 void InitializeSettings(LayerTreeSettings* settings) override {
1850 settings->use_external_begin_frame_source = true;
1853 void BeginTest() override {
1854 // This will trigger a SetNeedsBeginFrame which will trigger a
1855 // BeginFrame.
1856 PostSetNeedsCommitToMainThread();
1859 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
1860 LayerTreeHostImpl::FrameData* frame,
1861 DrawResult draw_result) override {
1862 EndTest();
1863 return DRAW_SUCCESS;
1866 void AfterTest() override {}
1868 private:
1869 base::TimeTicks frame_time_;
1872 MULTI_THREAD_TEST_F(LayerTreeHostTestBeginFrameNotification);
1874 class LayerTreeHostTestBeginFrameNotificationShutdownWhileEnabled
1875 : public LayerTreeHostTest {
1876 public:
1877 void InitializeSettings(LayerTreeSettings* settings) override {
1878 settings->use_external_begin_frame_source = true;
1879 settings->using_synchronous_renderer_compositor = true;
1882 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1884 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
1885 // The BeginFrame notification is turned off now but will get enabled
1886 // once we return. End test while it's enabled.
1887 ImplThreadTaskRunner()->PostTask(
1888 FROM_HERE,
1889 base::Bind(&LayerTreeHostTestBeginFrameNotification::EndTest,
1890 base::Unretained(this)));
1893 void AfterTest() override {}
1896 MULTI_THREAD_TEST_F(
1897 LayerTreeHostTestBeginFrameNotificationShutdownWhileEnabled);
1899 class LayerTreeHostTestAbortedCommitDoesntStall : public LayerTreeHostTest {
1900 protected:
1901 LayerTreeHostTestAbortedCommitDoesntStall()
1902 : commit_count_(0), commit_abort_count_(0), commit_complete_count_(0) {}
1904 void InitializeSettings(LayerTreeSettings* settings) override {
1905 settings->use_external_begin_frame_source = true;
1908 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
1910 void DidCommit() override {
1911 commit_count_++;
1912 if (commit_count_ == 4) {
1913 // After two aborted commits, request a real commit now to make sure a
1914 // real commit following an aborted commit will still complete and
1915 // end the test even when the Impl thread is idle.
1916 layer_tree_host()->SetNeedsCommit();
1920 void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl,
1921 CommitEarlyOutReason reason) override {
1922 commit_abort_count_++;
1923 // Initiate another abortable commit.
1924 host_impl->SetNeedsCommit();
1927 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
1928 commit_complete_count_++;
1929 if (commit_complete_count_ == 1) {
1930 // Initiate an abortable commit after the first commit.
1931 host_impl->SetNeedsCommit();
1932 } else {
1933 EndTest();
1937 void AfterTest() override {
1938 EXPECT_EQ(commit_count_, 5);
1939 EXPECT_EQ(commit_abort_count_, 3);
1940 EXPECT_EQ(commit_complete_count_, 2);
1943 int commit_count_;
1944 int commit_abort_count_;
1945 int commit_complete_count_;
1948 class LayerTreeHostTestAbortedCommitDoesntStallSynchronousCompositor
1949 : public LayerTreeHostTestAbortedCommitDoesntStall {
1950 protected:
1951 void InitializeSettings(LayerTreeSettings* settings) override {
1952 LayerTreeHostTestAbortedCommitDoesntStall::InitializeSettings(settings);
1953 settings->using_synchronous_renderer_compositor = true;
1956 void ScheduledActionInvalidateOutputSurface() override {
1957 ImplThreadTaskRunner()->PostTask(
1958 FROM_HERE,
1959 base::Bind(
1960 &LayerTreeHostTestAbortedCommitDoesntStallSynchronousCompositor::
1961 CallOnDraw,
1962 base::Unretained(this)));
1965 void CallOnDraw() {
1966 // Synchronous compositor does not draw unless told to do so by the output
1967 // surface.
1968 output_surface()->client()->OnDraw();
1972 MULTI_THREAD_TEST_F(
1973 LayerTreeHostTestAbortedCommitDoesntStallSynchronousCompositor);
1975 class LayerTreeHostTestAbortedCommitDoesntStallDisabledVsync
1976 : public LayerTreeHostTestAbortedCommitDoesntStall {
1977 void InitializeSettings(LayerTreeSettings* settings) override {
1978 LayerTreeHostTestAbortedCommitDoesntStall::InitializeSettings(settings);
1979 settings->wait_for_beginframe_interval = false;
1980 settings->renderer_settings.disable_display_vsync = true;
1984 MULTI_THREAD_TEST_F(LayerTreeHostTestAbortedCommitDoesntStallDisabledVsync);
1986 class LayerTreeHostTestUninvertibleTransformDoesNotBlockActivation
1987 : public LayerTreeHostTest {
1988 protected:
1989 void SetupTree() override {
1990 LayerTreeHostTest::SetupTree();
1992 scoped_refptr<Layer> layer =
1993 PictureLayer::Create(layer_settings(), &client_);
1994 layer->SetTransform(gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
1995 layer->SetBounds(gfx::Size(10, 10));
1996 layer_tree_host()->root_layer()->AddChild(layer);
1999 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2001 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
2002 EndTest();
2005 void AfterTest() override {}
2007 FakeContentLayerClient client_;
2010 SINGLE_AND_MULTI_THREAD_TEST_F(
2011 LayerTreeHostTestUninvertibleTransformDoesNotBlockActivation);
2013 class LayerTreeHostTestChangeLayerPropertiesInPaintContents
2014 : public LayerTreeHostTest {
2015 public:
2016 class SetBoundsClient : public ContentLayerClient {
2017 public:
2018 SetBoundsClient() : layer_(0) {}
2020 void set_layer(Layer* layer) { layer_ = layer; }
2022 void PaintContents(SkCanvas* canvas,
2023 const gfx::Rect& clip,
2024 PaintingControlSetting picture_control) override {
2025 layer_->SetBounds(gfx::Size(2, 2));
2028 scoped_refptr<DisplayItemList> PaintContentsToDisplayList(
2029 const gfx::Rect& clip,
2030 PaintingControlSetting picture_control) override {
2031 NOTIMPLEMENTED();
2032 return nullptr;
2035 bool FillsBoundsCompletely() const override { return false; }
2036 size_t GetApproximateUnsharedMemoryUsage() const override { return 0; }
2038 private:
2039 Layer* layer_;
2042 LayerTreeHostTestChangeLayerPropertiesInPaintContents() : num_commits_(0) {}
2044 void SetupTree() override {
2045 scoped_refptr<PictureLayer> root_layer =
2046 PictureLayer::Create(layer_settings(), &client_);
2047 root_layer->SetIsDrawable(true);
2048 root_layer->SetBounds(gfx::Size(1, 1));
2049 client_.set_layer(root_layer.get());
2051 layer_tree_host()->SetRootLayer(root_layer);
2052 LayerTreeHostTest::SetupTree();
2055 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2056 void AfterTest() override {}
2058 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
2059 num_commits_++;
2060 if (num_commits_ == 1) {
2061 LayerImpl* root_layer = host_impl->active_tree()->root_layer();
2062 EXPECT_EQ(gfx::Size(1, 1), root_layer->bounds());
2063 } else {
2064 LayerImpl* root_layer = host_impl->active_tree()->root_layer();
2065 EXPECT_EQ(gfx::Size(2, 2), root_layer->bounds());
2066 EndTest();
2070 private:
2071 SetBoundsClient client_;
2072 int num_commits_;
2075 SINGLE_AND_MULTI_THREAD_TEST_F(
2076 LayerTreeHostTestChangeLayerPropertiesInPaintContents);
2078 class MockIOSurfaceWebGraphicsContext3D : public TestWebGraphicsContext3D {
2079 public:
2080 MockIOSurfaceWebGraphicsContext3D() {
2081 test_capabilities_.gpu.iosurface = true;
2082 test_capabilities_.gpu.texture_rectangle = true;
2085 GLuint createTexture() override { return 1; }
2086 MOCK_METHOD1(activeTexture, void(GLenum texture));
2087 MOCK_METHOD2(bindTexture, void(GLenum target,
2088 GLuint texture_id));
2089 MOCK_METHOD3(texParameteri, void(GLenum target,
2090 GLenum pname,
2091 GLint param));
2092 MOCK_METHOD5(texImageIOSurface2DCHROMIUM, void(GLenum target,
2093 GLint width,
2094 GLint height,
2095 GLuint ioSurfaceId,
2096 GLuint plane));
2097 MOCK_METHOD4(drawElements, void(GLenum mode,
2098 GLsizei count,
2099 GLenum type,
2100 GLintptr offset));
2101 MOCK_METHOD1(deleteTexture, void(GLenum texture));
2102 MOCK_METHOD3(produceTextureDirectCHROMIUM,
2103 void(GLuint texture, GLenum target, const GLbyte* mailbox));
2106 class LayerTreeHostTestIOSurfaceDrawing : public LayerTreeHostTest {
2107 protected:
2108 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
2109 scoped_ptr<MockIOSurfaceWebGraphicsContext3D> mock_context_owned(
2110 new MockIOSurfaceWebGraphicsContext3D);
2111 mock_context_ = mock_context_owned.get();
2113 if (delegating_renderer())
2114 return FakeOutputSurface::CreateDelegating3d(mock_context_owned.Pass());
2115 else
2116 return FakeOutputSurface::Create3d(mock_context_owned.Pass());
2119 void SetupTree() override {
2120 LayerTreeHostTest::SetupTree();
2122 layer_tree_host()->root_layer()->SetIsDrawable(false);
2124 io_surface_id_ = 9;
2125 io_surface_size_ = gfx::Size(6, 7);
2127 scoped_refptr<IOSurfaceLayer> io_surface_layer =
2128 IOSurfaceLayer::Create(layer_settings());
2129 io_surface_layer->SetBounds(gfx::Size(10, 10));
2130 io_surface_layer->SetIsDrawable(true);
2131 io_surface_layer->SetContentsOpaque(true);
2132 io_surface_layer->SetIOSurfaceProperties(io_surface_id_, io_surface_size_);
2133 layer_tree_host()->root_layer()->AddChild(io_surface_layer);
2136 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2138 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
2139 EXPECT_EQ(0u, host_impl->resource_provider()->num_resources());
2140 // In WillDraw, the IOSurfaceLayer sets up the io surface texture.
2142 EXPECT_CALL(*mock_context_, activeTexture(_)).Times(0);
2143 EXPECT_CALL(*mock_context_, bindTexture(GL_TEXTURE_RECTANGLE_ARB, 1))
2144 .Times(AtLeast(1));
2145 EXPECT_CALL(*mock_context_,
2146 texParameteri(
2147 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR))
2148 .Times(1);
2149 EXPECT_CALL(*mock_context_,
2150 texParameteri(
2151 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR))
2152 .Times(1);
2153 EXPECT_CALL(*mock_context_,
2154 texParameteri(GL_TEXTURE_RECTANGLE_ARB,
2155 GL_TEXTURE_POOL_CHROMIUM,
2156 GL_TEXTURE_POOL_UNMANAGED_CHROMIUM)).Times(1);
2157 EXPECT_CALL(*mock_context_,
2158 texParameteri(GL_TEXTURE_RECTANGLE_ARB,
2159 GL_TEXTURE_WRAP_S,
2160 GL_CLAMP_TO_EDGE)).Times(1);
2161 EXPECT_CALL(*mock_context_,
2162 texParameteri(GL_TEXTURE_RECTANGLE_ARB,
2163 GL_TEXTURE_WRAP_T,
2164 GL_CLAMP_TO_EDGE)).Times(1);
2166 EXPECT_CALL(*mock_context_,
2167 texImageIOSurface2DCHROMIUM(GL_TEXTURE_RECTANGLE_ARB,
2168 io_surface_size_.width(),
2169 io_surface_size_.height(),
2170 io_surface_id_,
2171 0)).Times(1);
2173 EXPECT_CALL(*mock_context_, bindTexture(_, 0)).Times(AnyNumber());
2176 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
2177 LayerTreeHostImpl::FrameData* frame,
2178 DrawResult draw_result) override {
2179 Mock::VerifyAndClearExpectations(&mock_context_);
2180 ResourceProvider* resource_provider = host_impl->resource_provider();
2181 EXPECT_EQ(1u, resource_provider->num_resources());
2182 CHECK_EQ(1u, frame->render_passes.size());
2183 CHECK_LE(1u, frame->render_passes[0]->quad_list.size());
2184 const DrawQuad* quad = frame->render_passes[0]->quad_list.front();
2185 CHECK_EQ(DrawQuad::IO_SURFACE_CONTENT, quad->material);
2186 const IOSurfaceDrawQuad* io_surface_draw_quad =
2187 IOSurfaceDrawQuad::MaterialCast(quad);
2188 EXPECT_EQ(io_surface_size_, io_surface_draw_quad->io_surface_size);
2189 EXPECT_NE(0u, io_surface_draw_quad->io_surface_resource_id());
2190 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_RECTANGLE_ARB),
2191 resource_provider->TargetForTesting(
2192 io_surface_draw_quad->io_surface_resource_id()));
2194 if (delegating_renderer()) {
2195 // The io surface layer's resource should be sent to the parent.
2196 EXPECT_CALL(*mock_context_, produceTextureDirectCHROMIUM(
2197 _, GL_TEXTURE_RECTANGLE_ARB, _)).Times(1);
2198 } else {
2199 // The io surface layer's texture is drawn.
2200 EXPECT_CALL(*mock_context_, activeTexture(GL_TEXTURE0)).Times(AtLeast(1));
2201 EXPECT_CALL(*mock_context_, drawElements(GL_TRIANGLES, 6, _, _))
2202 .Times(AtLeast(1));
2205 return draw_result;
2208 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
2209 Mock::VerifyAndClearExpectations(&mock_context_);
2211 EXPECT_CALL(*mock_context_, deleteTexture(1)).Times(AtLeast(1));
2212 EndTest();
2215 void AfterTest() override {}
2217 int io_surface_id_;
2218 MockIOSurfaceWebGraphicsContext3D* mock_context_;
2219 gfx::Size io_surface_size_;
2222 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestIOSurfaceDrawing);
2224 class LayerTreeHostTestNumFramesPending : public LayerTreeHostTest {
2225 public:
2226 void BeginTest() override {
2227 frame_ = 0;
2228 PostSetNeedsCommitToMainThread();
2231 // Round 1: commit + draw
2232 // Round 2: commit only (no draw/swap)
2233 // Round 3: draw only (no commit)
2235 void DidCommit() override {
2236 int commit = layer_tree_host()->source_frame_number();
2237 switch (commit) {
2238 case 2:
2239 // Round 2 done.
2240 EXPECT_EQ(1, frame_);
2241 layer_tree_host()->SetNeedsRedraw();
2242 break;
2246 void DidCompleteSwapBuffers() override {
2247 int commit = layer_tree_host()->source_frame_number();
2248 ++frame_;
2249 switch (frame_) {
2250 case 1:
2251 // Round 1 done.
2252 EXPECT_EQ(1, commit);
2253 layer_tree_host()->SetNeedsCommit();
2254 break;
2255 case 2:
2256 // Round 3 done.
2257 EXPECT_EQ(2, commit);
2258 EndTest();
2259 break;
2263 void AfterTest() override {}
2265 protected:
2266 int frame_;
2269 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestNumFramesPending);
2271 class LayerTreeHostTestResourcelessSoftwareDraw : public LayerTreeHostTest {
2272 public:
2273 void SetupTree() override {
2274 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
2275 root_layer_->SetIsDrawable(true);
2276 root_layer_->SetBounds(gfx::Size(50, 50));
2278 parent_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
2279 parent_layer_->SetIsDrawable(true);
2280 parent_layer_->SetBounds(gfx::Size(50, 50));
2281 parent_layer_->SetForceRenderSurface(true);
2283 child_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
2284 child_layer_->SetIsDrawable(true);
2285 child_layer_->SetBounds(gfx::Size(50, 50));
2287 root_layer_->AddChild(parent_layer_);
2288 parent_layer_->AddChild(child_layer_);
2289 layer_tree_host()->SetRootLayer(root_layer_);
2291 LayerTreeHostTest::SetupTree();
2294 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
2295 if (delegating_renderer()) {
2296 return FakeOutputSurface::CreateDelegatingSoftware(
2297 make_scoped_ptr(new SoftwareOutputDevice));
2298 } else {
2299 return FakeOutputSurface::CreateSoftware(
2300 make_scoped_ptr(new SoftwareOutputDevice));
2304 void BeginTest() override {
2305 PostSetNeedsCommitToMainThread();
2306 swap_count_ = 0;
2309 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
2310 LayerTreeHostImpl::FrameData* frame_data,
2311 DrawResult draw_result) override {
2312 if (host_impl->GetDrawMode() == DRAW_MODE_RESOURCELESS_SOFTWARE) {
2313 EXPECT_EQ(1u, frame_data->render_passes.size());
2314 // Has at least 3 quads for each layer.
2315 RenderPass* render_pass = frame_data->render_passes[0];
2316 EXPECT_GE(render_pass->quad_list.size(), 3u);
2317 } else {
2318 EXPECT_EQ(2u, frame_data->render_passes.size());
2320 // At least root layer quad in root render pass.
2321 EXPECT_GE(frame_data->render_passes[0]->quad_list.size(), 1u);
2322 // At least parent and child layer quads in parent render pass.
2323 EXPECT_GE(frame_data->render_passes[1]->quad_list.size(), 2u);
2325 return draw_result;
2328 void SwapBuffersCompleteOnThread(LayerTreeHostImpl* host_impl) override {
2329 swap_count_++;
2330 switch (swap_count_) {
2331 case 1: {
2332 gfx::Transform identity;
2333 gfx::Rect empty_rect;
2334 bool resourceless_software_draw = true;
2335 host_impl->SetExternalDrawConstraints(identity, empty_rect, empty_rect,
2336 empty_rect, identity,
2337 resourceless_software_draw);
2338 host_impl->SetFullRootLayerDamage();
2339 host_impl->SetNeedsRedraw();
2340 break;
2342 case 2:
2343 EndTest();
2344 break;
2345 default:
2346 NOTREACHED();
2350 void AfterTest() override {}
2352 private:
2353 FakeContentLayerClient client_;
2354 scoped_refptr<Layer> root_layer_;
2355 scoped_refptr<Layer> parent_layer_;
2356 scoped_refptr<Layer> child_layer_;
2357 int swap_count_;
2360 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestResourcelessSoftwareDraw);
2362 // Test for UI Resource management.
2363 class LayerTreeHostTestUIResource : public LayerTreeHostTest {
2364 public:
2365 LayerTreeHostTestUIResource() : num_ui_resources_(0) {}
2367 void InitializeSettings(LayerTreeSettings* settings) override {
2368 settings->renderer_settings.texture_id_allocation_chunk_size = 1;
2371 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2373 void DidCommit() override {
2374 int frame = layer_tree_host()->source_frame_number();
2375 switch (frame) {
2376 case 1:
2377 CreateResource();
2378 CreateResource();
2379 PostSetNeedsCommitToMainThread();
2380 break;
2381 case 2:
2382 // Usually ScopedUIResource are deleted from the manager in their
2383 // destructor. Here we just want to test that a direct call to
2384 // DeleteUIResource works.
2385 layer_tree_host()->DeleteUIResource(ui_resources_[0]->id());
2386 PostSetNeedsCommitToMainThread();
2387 break;
2388 case 3:
2389 // DeleteUIResource can be called with an invalid id.
2390 layer_tree_host()->DeleteUIResource(ui_resources_[0]->id());
2391 PostSetNeedsCommitToMainThread();
2392 break;
2393 case 4:
2394 CreateResource();
2395 CreateResource();
2396 PostSetNeedsCommitToMainThread();
2397 break;
2398 case 5:
2399 ClearResources();
2400 EndTest();
2401 break;
2405 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
2406 TestWebGraphicsContext3D* context = TestContext();
2408 int frame = impl->active_tree()->source_frame_number();
2409 switch (frame) {
2410 case 0:
2411 ASSERT_EQ(0u, context->NumTextures());
2412 break;
2413 case 1:
2414 // Created two textures.
2415 ASSERT_EQ(2u, context->NumTextures());
2416 break;
2417 case 2:
2418 // One texture left after one deletion.
2419 ASSERT_EQ(1u, context->NumTextures());
2420 break;
2421 case 3:
2422 // Resource manager state should not change when delete is called on an
2423 // invalid id.
2424 ASSERT_EQ(1u, context->NumTextures());
2425 break;
2426 case 4:
2427 // Creation after deletion: two more creates should total up to
2428 // three textures.
2429 ASSERT_EQ(3u, context->NumTextures());
2430 break;
2434 void AfterTest() override {}
2436 private:
2437 // Must clear all resources before exiting.
2438 void ClearResources() {
2439 for (int i = 0; i < num_ui_resources_; i++)
2440 ui_resources_[i] = nullptr;
2443 void CreateResource() {
2444 ui_resources_[num_ui_resources_++] =
2445 FakeScopedUIResource::Create(layer_tree_host());
2448 scoped_ptr<FakeScopedUIResource> ui_resources_[5];
2449 int num_ui_resources_;
2452 MULTI_THREAD_TEST_F(LayerTreeHostTestUIResource);
2454 class PushPropertiesCountingLayerImpl : public LayerImpl {
2455 public:
2456 static scoped_ptr<PushPropertiesCountingLayerImpl> Create(
2457 LayerTreeImpl* tree_impl, int id) {
2458 return make_scoped_ptr(new PushPropertiesCountingLayerImpl(tree_impl, id));
2461 ~PushPropertiesCountingLayerImpl() override {}
2463 void PushPropertiesTo(LayerImpl* layer) override {
2464 LayerImpl::PushPropertiesTo(layer);
2465 push_properties_count_++;
2466 // Push state to the active tree because we can only access it from there.
2467 static_cast<PushPropertiesCountingLayerImpl*>(
2468 layer)->push_properties_count_ = push_properties_count_;
2471 scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl) override {
2472 return PushPropertiesCountingLayerImpl::Create(tree_impl, id());
2475 size_t push_properties_count() const { return push_properties_count_; }
2476 void reset_push_properties_count() { push_properties_count_ = 0; }
2478 private:
2479 size_t push_properties_count_;
2481 PushPropertiesCountingLayerImpl(LayerTreeImpl* tree_impl, int id)
2482 : LayerImpl(tree_impl, id),
2483 push_properties_count_(0) {
2484 SetBounds(gfx::Size(1, 1));
2488 class PushPropertiesCountingLayer : public Layer {
2489 public:
2490 static scoped_refptr<PushPropertiesCountingLayer> Create(
2491 const LayerSettings& settings) {
2492 return new PushPropertiesCountingLayer(settings);
2495 void PushPropertiesTo(LayerImpl* layer) override {
2496 Layer::PushPropertiesTo(layer);
2497 push_properties_count_++;
2498 if (persist_needs_push_properties_)
2499 needs_push_properties_ = true;
2502 // Something to make this layer push properties, but no other layer.
2503 void MakePushProperties() { SetContentsOpaque(!contents_opaque()); }
2505 scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl) override {
2506 return PushPropertiesCountingLayerImpl::Create(tree_impl, id());
2509 void SetDrawsContent(bool draws_content) { SetIsDrawable(draws_content); }
2511 size_t push_properties_count() const { return push_properties_count_; }
2512 void reset_push_properties_count() { push_properties_count_ = 0; }
2514 void set_persist_needs_push_properties(bool persist) {
2515 persist_needs_push_properties_ = persist;
2518 private:
2519 explicit PushPropertiesCountingLayer(const LayerSettings& settings)
2520 : Layer(settings),
2521 push_properties_count_(0),
2522 persist_needs_push_properties_(false) {
2523 SetBounds(gfx::Size(1, 1));
2525 ~PushPropertiesCountingLayer() override {}
2527 size_t push_properties_count_;
2528 bool persist_needs_push_properties_;
2531 class LayerTreeHostTestLayersPushProperties : public LayerTreeHostTest {
2532 protected:
2533 void BeginTest() override {
2534 num_commits_ = 0;
2535 expected_push_properties_root_ = 0;
2536 expected_push_properties_child_ = 0;
2537 expected_push_properties_grandchild_ = 0;
2538 expected_push_properties_child2_ = 0;
2539 expected_push_properties_other_root_ = 0;
2540 expected_push_properties_leaf_layer_ = 0;
2541 PostSetNeedsCommitToMainThread();
2544 void SetupTree() override {
2545 root_ = PushPropertiesCountingLayer::Create(layer_settings());
2546 root_->CreateRenderSurface();
2547 child_ = PushPropertiesCountingLayer::Create(layer_settings());
2548 child2_ = PushPropertiesCountingLayer::Create(layer_settings());
2549 grandchild_ = PushPropertiesCountingLayer::Create(layer_settings());
2550 leaf_always_pushing_layer_ =
2551 PushPropertiesCountingLayer::Create(layer_settings());
2552 leaf_always_pushing_layer_->set_persist_needs_push_properties(true);
2554 root_->AddChild(child_);
2555 root_->AddChild(child2_);
2556 child_->AddChild(grandchild_);
2557 child2_->AddChild(leaf_always_pushing_layer_);
2559 other_root_ = PushPropertiesCountingLayer::Create(layer_settings());
2560 other_root_->CreateRenderSurface();
2562 // Don't set the root layer here.
2563 LayerTreeHostTest::SetupTree();
2566 void DidCommitAndDrawFrame() override {
2567 EXPECT_EQ(expected_push_properties_root_, root_->push_properties_count())
2568 << "num_commits: " << num_commits_;
2569 EXPECT_EQ(expected_push_properties_child_, child_->push_properties_count())
2570 << "num_commits: " << num_commits_;
2571 EXPECT_EQ(expected_push_properties_grandchild_,
2572 grandchild_->push_properties_count())
2573 << "num_commits: " << num_commits_;
2574 EXPECT_EQ(expected_push_properties_child2_,
2575 child2_->push_properties_count())
2576 << "num_commits: " << num_commits_;
2577 EXPECT_EQ(expected_push_properties_other_root_,
2578 other_root_->push_properties_count())
2579 << "num_commits: " << num_commits_;
2580 EXPECT_EQ(expected_push_properties_leaf_layer_,
2581 leaf_always_pushing_layer_->push_properties_count())
2582 << "num_commits: " << num_commits_;
2584 ++num_commits_;
2586 // The scrollbar layer always needs to be pushed.
2587 if (root_->layer_tree_host()) {
2588 EXPECT_TRUE(root_->descendant_needs_push_properties());
2589 EXPECT_FALSE(root_->needs_push_properties());
2591 if (child2_->layer_tree_host()) {
2592 EXPECT_TRUE(child2_->descendant_needs_push_properties());
2593 EXPECT_FALSE(child2_->needs_push_properties());
2595 if (leaf_always_pushing_layer_->layer_tree_host()) {
2596 EXPECT_FALSE(
2597 leaf_always_pushing_layer_->descendant_needs_push_properties());
2598 EXPECT_TRUE(leaf_always_pushing_layer_->needs_push_properties());
2601 // child_ and grandchild_ don't persist their need to push properties.
2602 if (child_->layer_tree_host()) {
2603 EXPECT_FALSE(child_->descendant_needs_push_properties());
2604 EXPECT_FALSE(child_->needs_push_properties());
2606 if (grandchild_->layer_tree_host()) {
2607 EXPECT_FALSE(grandchild_->descendant_needs_push_properties());
2608 EXPECT_FALSE(grandchild_->needs_push_properties());
2611 if (other_root_->layer_tree_host()) {
2612 EXPECT_FALSE(other_root_->descendant_needs_push_properties());
2613 EXPECT_FALSE(other_root_->needs_push_properties());
2616 switch (num_commits_) {
2617 case 1:
2618 layer_tree_host()->SetRootLayer(root_);
2619 // Layers added to the tree get committed.
2620 ++expected_push_properties_root_;
2621 ++expected_push_properties_child_;
2622 ++expected_push_properties_grandchild_;
2623 ++expected_push_properties_child2_;
2624 break;
2625 case 2:
2626 layer_tree_host()->SetNeedsCommit();
2627 // No layers need commit.
2628 break;
2629 case 3:
2630 layer_tree_host()->SetRootLayer(other_root_);
2631 // Layers added to the tree get committed.
2632 ++expected_push_properties_other_root_;
2633 break;
2634 case 4:
2635 layer_tree_host()->SetRootLayer(root_);
2636 // Layers added to the tree get committed.
2637 ++expected_push_properties_root_;
2638 ++expected_push_properties_child_;
2639 ++expected_push_properties_grandchild_;
2640 ++expected_push_properties_child2_;
2641 break;
2642 case 5:
2643 layer_tree_host()->SetNeedsCommit();
2644 // No layers need commit.
2645 break;
2646 case 6:
2647 child_->RemoveFromParent();
2648 // No layers need commit.
2649 break;
2650 case 7:
2651 root_->AddChild(child_);
2652 // Layers added to the tree get committed.
2653 ++expected_push_properties_child_;
2654 ++expected_push_properties_grandchild_;
2655 break;
2656 case 8:
2657 grandchild_->RemoveFromParent();
2658 // No layers need commit.
2659 break;
2660 case 9:
2661 child_->AddChild(grandchild_);
2662 // Layers added to the tree get committed.
2663 ++expected_push_properties_grandchild_;
2664 break;
2665 case 10:
2666 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
2667 // No layers need commit.
2668 break;
2669 case 11:
2670 layer_tree_host()->SetPageScaleFactorAndLimits(1.f, 0.8f, 1.1f);
2671 // No layers need commit.
2672 break;
2673 case 12:
2674 child_->MakePushProperties();
2675 // The modified layer needs commit
2676 ++expected_push_properties_child_;
2677 break;
2678 case 13:
2679 child2_->MakePushProperties();
2680 // The modified layer needs commit
2681 ++expected_push_properties_child2_;
2682 break;
2683 case 14:
2684 child_->RemoveFromParent();
2685 root_->AddChild(child_);
2686 // Layers added to the tree get committed.
2687 ++expected_push_properties_child_;
2688 ++expected_push_properties_grandchild_;
2689 break;
2690 case 15:
2691 grandchild_->MakePushProperties();
2692 // The modified layer needs commit
2693 ++expected_push_properties_grandchild_;
2694 break;
2695 case 16:
2696 // SetNeedsDisplay does not always set needs commit (so call it
2697 // explicitly), but is a property change.
2698 child_->SetNeedsDisplay();
2699 ++expected_push_properties_child_;
2700 layer_tree_host()->SetNeedsCommit();
2701 break;
2702 case 17:
2703 EndTest();
2704 break;
2707 // The leaf layer always pushes.
2708 if (leaf_always_pushing_layer_->layer_tree_host())
2709 ++expected_push_properties_leaf_layer_;
2712 void AfterTest() override {}
2714 int num_commits_;
2715 FakeContentLayerClient client_;
2716 scoped_refptr<PushPropertiesCountingLayer> root_;
2717 scoped_refptr<PushPropertiesCountingLayer> child_;
2718 scoped_refptr<PushPropertiesCountingLayer> child2_;
2719 scoped_refptr<PushPropertiesCountingLayer> grandchild_;
2720 scoped_refptr<PushPropertiesCountingLayer> other_root_;
2721 scoped_refptr<PushPropertiesCountingLayer> leaf_always_pushing_layer_;
2722 size_t expected_push_properties_root_;
2723 size_t expected_push_properties_child_;
2724 size_t expected_push_properties_child2_;
2725 size_t expected_push_properties_grandchild_;
2726 size_t expected_push_properties_other_root_;
2727 size_t expected_push_properties_leaf_layer_;
2730 MULTI_THREAD_TEST_F(LayerTreeHostTestLayersPushProperties);
2732 class LayerTreeHostTestImplLayersPushProperties
2733 : public LayerTreeHostTestLayersPushProperties {
2734 protected:
2735 void BeginTest() override {
2736 expected_push_properties_root_impl_ = 0;
2737 expected_push_properties_child_impl_ = 0;
2738 expected_push_properties_grandchild_impl_ = 0;
2739 expected_push_properties_child2_impl_ = 0;
2740 expected_push_properties_grandchild2_impl_ = 0;
2741 LayerTreeHostTestLayersPushProperties::BeginTest();
2744 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
2745 // These commits are in response to the changes made in
2746 // LayerTreeHostTestLayersPushProperties::DidCommitAndDrawFrame()
2747 switch (num_commits_) {
2748 case 0:
2749 // Tree hasn't been setup yet don't bother to check anything.
2750 return;
2751 case 1:
2752 // Root gets set up, Everyone is initialized.
2753 ++expected_push_properties_root_impl_;
2754 ++expected_push_properties_child_impl_;
2755 ++expected_push_properties_grandchild_impl_;
2756 ++expected_push_properties_child2_impl_;
2757 ++expected_push_properties_grandchild2_impl_;
2758 break;
2759 case 2:
2760 // Tree doesn't change but the one leaf that always pushes is pushed.
2761 ++expected_push_properties_grandchild2_impl_;
2762 break;
2763 case 3:
2764 // Root is swapped here.
2765 // Clear the expected push properties the tree will be rebuilt.
2766 expected_push_properties_root_impl_ = 0;
2767 expected_push_properties_child_impl_ = 0;
2768 expected_push_properties_grandchild_impl_ = 0;
2769 expected_push_properties_child2_impl_ = 0;
2770 expected_push_properties_grandchild2_impl_ = 0;
2772 // Make sure the new root is pushed.
2773 EXPECT_EQ(1u, static_cast<PushPropertiesCountingLayerImpl*>(
2774 host_impl->RootLayer())->push_properties_count());
2775 return;
2776 case 4:
2777 // Root is swapped back all of the layers in the tree get pushed.
2778 ++expected_push_properties_root_impl_;
2779 ++expected_push_properties_child_impl_;
2780 ++expected_push_properties_grandchild_impl_;
2781 ++expected_push_properties_child2_impl_;
2782 ++expected_push_properties_grandchild2_impl_;
2783 break;
2784 case 5:
2785 // Tree doesn't change but the one leaf that always pushes is pushed.
2786 ++expected_push_properties_grandchild2_impl_;
2787 break;
2788 case 6:
2789 // First child is removed. Structure of the tree changes here so swap
2790 // some of the values. child_impl becomes child2_impl.
2791 expected_push_properties_child_impl_ =
2792 expected_push_properties_child2_impl_;
2793 expected_push_properties_child2_impl_ = 0;
2794 // grandchild_impl becomes grandchild2_impl.
2795 expected_push_properties_grandchild_impl_ =
2796 expected_push_properties_grandchild2_impl_;
2797 expected_push_properties_grandchild2_impl_ = 0;
2799 // grandchild_impl is now the leaf that always pushes. It is pushed.
2800 ++expected_push_properties_grandchild_impl_;
2801 break;
2802 case 7:
2803 // The leaf that always pushes is pushed.
2804 ++expected_push_properties_grandchild_impl_;
2806 // Child is added back. New layers are initialized.
2807 ++expected_push_properties_grandchild2_impl_;
2808 ++expected_push_properties_child2_impl_;
2809 break;
2810 case 8:
2811 // Leaf is removed.
2812 expected_push_properties_grandchild2_impl_ = 0;
2814 // Always pushing.
2815 ++expected_push_properties_grandchild_impl_;
2816 break;
2817 case 9:
2818 // Leaf is added back
2819 ++expected_push_properties_grandchild2_impl_;
2821 // The leaf that always pushes is pushed.
2822 ++expected_push_properties_grandchild_impl_;
2823 break;
2824 case 10:
2825 // The leaf that always pushes is pushed.
2826 ++expected_push_properties_grandchild_impl_;
2827 break;
2828 case 11:
2829 // The leaf that always pushes is pushed.
2830 ++expected_push_properties_grandchild_impl_;
2831 break;
2832 case 12:
2833 // The leaf that always pushes is pushed.
2834 ++expected_push_properties_grandchild_impl_;
2836 // This child position was changed.
2837 ++expected_push_properties_child2_impl_;
2838 break;
2839 case 13:
2840 // The position of this child was changed.
2841 ++expected_push_properties_child_impl_;
2843 // The leaf that always pushes is pushed.
2844 ++expected_push_properties_grandchild_impl_;
2845 break;
2846 case 14:
2847 // Second child is removed from tree. Don't discard counts because
2848 // they are added back before commit.
2850 // The leaf that always pushes is pushed.
2851 ++expected_push_properties_grandchild_impl_;
2853 // Second child added back.
2854 ++expected_push_properties_child2_impl_;
2855 ++expected_push_properties_grandchild2_impl_;
2857 break;
2858 case 15:
2859 // The position of this child was changed.
2860 ++expected_push_properties_grandchild2_impl_;
2862 // The leaf that always pushes is pushed.
2863 ++expected_push_properties_grandchild_impl_;
2864 break;
2865 case 16:
2866 // Second child is invalidated with SetNeedsDisplay
2867 ++expected_push_properties_child2_impl_;
2869 // The leaf that always pushed is pushed.
2870 ++expected_push_properties_grandchild_impl_;
2871 break;
2874 PushPropertiesCountingLayerImpl* root_impl_ = NULL;
2875 PushPropertiesCountingLayerImpl* child_impl_ = NULL;
2876 PushPropertiesCountingLayerImpl* child2_impl_ = NULL;
2877 PushPropertiesCountingLayerImpl* grandchild_impl_ = NULL;
2878 PushPropertiesCountingLayerImpl* leaf_always_pushing_layer_impl_ = NULL;
2880 // Pull the layers that we need from the tree assuming the same structure
2881 // as LayerTreeHostTestLayersPushProperties
2882 root_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
2883 host_impl->RootLayer());
2885 if (root_impl_ && root_impl_->children().size() > 0) {
2886 child_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
2887 root_impl_->children()[0]);
2889 if (child_impl_ && child_impl_->children().size() > 0)
2890 grandchild_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
2891 child_impl_->children()[0]);
2894 if (root_impl_ && root_impl_->children().size() > 1) {
2895 child2_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
2896 root_impl_->children()[1]);
2898 if (child2_impl_ && child2_impl_->children().size() > 0)
2899 leaf_always_pushing_layer_impl_ =
2900 static_cast<PushPropertiesCountingLayerImpl*>(
2901 child2_impl_->children()[0]);
2904 if (root_impl_)
2905 EXPECT_EQ(expected_push_properties_root_impl_,
2906 root_impl_->push_properties_count());
2907 if (child_impl_)
2908 EXPECT_EQ(expected_push_properties_child_impl_,
2909 child_impl_->push_properties_count());
2910 if (grandchild_impl_)
2911 EXPECT_EQ(expected_push_properties_grandchild_impl_,
2912 grandchild_impl_->push_properties_count());
2913 if (child2_impl_)
2914 EXPECT_EQ(expected_push_properties_child2_impl_,
2915 child2_impl_->push_properties_count());
2916 if (leaf_always_pushing_layer_impl_)
2917 EXPECT_EQ(expected_push_properties_grandchild2_impl_,
2918 leaf_always_pushing_layer_impl_->push_properties_count());
2921 size_t expected_push_properties_root_impl_;
2922 size_t expected_push_properties_child_impl_;
2923 size_t expected_push_properties_child2_impl_;
2924 size_t expected_push_properties_grandchild_impl_;
2925 size_t expected_push_properties_grandchild2_impl_;
2928 // In single thread there's no pending tree to push properties from.
2929 MULTI_THREAD_TEST_F(LayerTreeHostTestImplLayersPushProperties);
2931 class LayerTreeHostTestPropertyChangesDuringUpdateArePushed
2932 : public LayerTreeHostTest {
2933 protected:
2934 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2936 void SetupTree() override {
2937 root_ = Layer::Create(layer_settings());
2938 root_->CreateRenderSurface();
2939 root_->SetBounds(gfx::Size(1, 1));
2941 bool paint_scrollbar = true;
2942 bool has_thumb = false;
2943 scrollbar_layer_ = FakePaintedScrollbarLayer::Create(
2944 layer_settings(), paint_scrollbar, has_thumb, root_->id());
2946 root_->AddChild(scrollbar_layer_);
2948 layer_tree_host()->SetRootLayer(root_);
2949 LayerTreeHostTest::SetupTree();
2952 void DidCommitAndDrawFrame() override {
2953 switch (layer_tree_host()->source_frame_number()) {
2954 case 0:
2955 break;
2956 case 1: {
2957 // During update, the ignore_set_needs_commit_ bit is set to true to
2958 // avoid causing a second commit to be scheduled. If a property change
2959 // is made during this, however, it needs to be pushed in the upcoming
2960 // commit.
2961 scoped_ptr<base::AutoReset<bool>> ignore =
2962 scrollbar_layer_->IgnoreSetNeedsCommit();
2964 scrollbar_layer_->SetBounds(gfx::Size(30, 30));
2966 EXPECT_TRUE(scrollbar_layer_->needs_push_properties());
2967 EXPECT_TRUE(root_->descendant_needs_push_properties());
2968 layer_tree_host()->SetNeedsCommit();
2970 scrollbar_layer_->reset_push_properties_count();
2971 EXPECT_EQ(0u, scrollbar_layer_->push_properties_count());
2972 break;
2974 case 2:
2975 EXPECT_EQ(1u, scrollbar_layer_->push_properties_count());
2976 EndTest();
2977 break;
2981 void AfterTest() override {}
2983 scoped_refptr<Layer> root_;
2984 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer_;
2987 MULTI_THREAD_TEST_F(LayerTreeHostTestPropertyChangesDuringUpdateArePushed);
2989 class LayerTreeHostTestSetDrawableCausesCommit : public LayerTreeHostTest {
2990 protected:
2991 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
2993 void SetupTree() override {
2994 root_ = PushPropertiesCountingLayer::Create(layer_settings());
2995 root_->CreateRenderSurface();
2996 child_ = PushPropertiesCountingLayer::Create(layer_settings());
2997 root_->AddChild(child_);
2999 layer_tree_host()->SetRootLayer(root_);
3000 LayerTreeHostTest::SetupTree();
3003 void DidCommitAndDrawFrame() override {
3004 switch (layer_tree_host()->source_frame_number()) {
3005 case 0:
3006 break;
3007 case 1: {
3008 // During update, the ignore_set_needs_commit_ bit is set to true to
3009 // avoid causing a second commit to be scheduled. If a property change
3010 // is made during this, however, it needs to be pushed in the upcoming
3011 // commit.
3012 EXPECT_FALSE(root_->needs_push_properties());
3013 EXPECT_FALSE(child_->needs_push_properties());
3014 EXPECT_EQ(0, root_->NumDescendantsThatDrawContent());
3015 root_->reset_push_properties_count();
3016 child_->reset_push_properties_count();
3017 child_->SetDrawsContent(true);
3018 EXPECT_EQ(1, root_->NumDescendantsThatDrawContent());
3019 EXPECT_EQ(0u, root_->push_properties_count());
3020 EXPECT_EQ(0u, child_->push_properties_count());
3021 EXPECT_TRUE(root_->needs_push_properties());
3022 EXPECT_TRUE(child_->needs_push_properties());
3023 break;
3025 case 2:
3026 EXPECT_EQ(1u, root_->push_properties_count());
3027 EXPECT_EQ(1u, child_->push_properties_count());
3028 EXPECT_FALSE(root_->needs_push_properties());
3029 EXPECT_FALSE(child_->needs_push_properties());
3030 EndTest();
3031 break;
3035 void AfterTest() override {}
3037 scoped_refptr<PushPropertiesCountingLayer> root_;
3038 scoped_refptr<PushPropertiesCountingLayer> child_;
3041 MULTI_THREAD_TEST_F(LayerTreeHostTestSetDrawableCausesCommit);
3043 class LayerTreeHostTestCasePushPropertiesThreeGrandChildren
3044 : public LayerTreeHostTest {
3045 protected:
3046 void BeginTest() override {
3047 expected_push_properties_root_ = 0;
3048 expected_push_properties_child_ = 0;
3049 expected_push_properties_grandchild1_ = 0;
3050 expected_push_properties_grandchild2_ = 0;
3051 expected_push_properties_grandchild3_ = 0;
3052 PostSetNeedsCommitToMainThread();
3055 void SetupTree() override {
3056 root_ = PushPropertiesCountingLayer::Create(layer_settings());
3057 root_->CreateRenderSurface();
3058 child_ = PushPropertiesCountingLayer::Create(layer_settings());
3059 grandchild1_ = PushPropertiesCountingLayer::Create(layer_settings());
3060 grandchild2_ = PushPropertiesCountingLayer::Create(layer_settings());
3061 grandchild3_ = PushPropertiesCountingLayer::Create(layer_settings());
3063 root_->AddChild(child_);
3064 child_->AddChild(grandchild1_);
3065 child_->AddChild(grandchild2_);
3066 child_->AddChild(grandchild3_);
3068 // Don't set the root layer here.
3069 LayerTreeHostTest::SetupTree();
3072 void AfterTest() override {}
3074 FakeContentLayerClient client_;
3075 scoped_refptr<PushPropertiesCountingLayer> root_;
3076 scoped_refptr<PushPropertiesCountingLayer> child_;
3077 scoped_refptr<PushPropertiesCountingLayer> grandchild1_;
3078 scoped_refptr<PushPropertiesCountingLayer> grandchild2_;
3079 scoped_refptr<PushPropertiesCountingLayer> grandchild3_;
3080 size_t expected_push_properties_root_;
3081 size_t expected_push_properties_child_;
3082 size_t expected_push_properties_grandchild1_;
3083 size_t expected_push_properties_grandchild2_;
3084 size_t expected_push_properties_grandchild3_;
3087 class LayerTreeHostTestPushPropertiesAddingToTreeRequiresPush
3088 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3089 protected:
3090 void DidCommitAndDrawFrame() override {
3091 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3092 switch (last_source_frame_number) {
3093 case 0:
3094 EXPECT_FALSE(root_->needs_push_properties());
3095 EXPECT_FALSE(root_->descendant_needs_push_properties());
3096 EXPECT_FALSE(child_->needs_push_properties());
3097 EXPECT_FALSE(child_->descendant_needs_push_properties());
3098 EXPECT_FALSE(grandchild1_->needs_push_properties());
3099 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3100 EXPECT_FALSE(grandchild2_->needs_push_properties());
3101 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3102 EXPECT_FALSE(grandchild3_->needs_push_properties());
3103 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3105 layer_tree_host()->SetRootLayer(root_);
3107 EXPECT_TRUE(root_->needs_push_properties());
3108 EXPECT_TRUE(root_->descendant_needs_push_properties());
3109 EXPECT_TRUE(child_->needs_push_properties());
3110 EXPECT_TRUE(child_->descendant_needs_push_properties());
3111 EXPECT_TRUE(grandchild1_->needs_push_properties());
3112 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3113 EXPECT_TRUE(grandchild2_->needs_push_properties());
3114 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3115 EXPECT_TRUE(grandchild3_->needs_push_properties());
3116 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3117 break;
3118 case 1:
3119 EndTest();
3120 break;
3125 MULTI_THREAD_TEST_F(LayerTreeHostTestPushPropertiesAddingToTreeRequiresPush);
3127 class LayerTreeHostTestPushPropertiesRemovingChildStopsRecursion
3128 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3129 protected:
3130 void DidCommitAndDrawFrame() override {
3131 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3132 switch (last_source_frame_number) {
3133 case 0:
3134 layer_tree_host()->SetRootLayer(root_);
3135 break;
3136 case 1:
3137 EXPECT_FALSE(root_->needs_push_properties());
3138 EXPECT_FALSE(root_->descendant_needs_push_properties());
3139 EXPECT_FALSE(child_->needs_push_properties());
3140 EXPECT_FALSE(child_->descendant_needs_push_properties());
3141 EXPECT_FALSE(grandchild1_->needs_push_properties());
3142 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3143 EXPECT_FALSE(grandchild2_->needs_push_properties());
3144 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3145 EXPECT_FALSE(grandchild3_->needs_push_properties());
3146 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3148 grandchild1_->RemoveFromParent();
3149 grandchild1_->SetPosition(gfx::Point(1, 1));
3151 EXPECT_FALSE(root_->needs_push_properties());
3152 EXPECT_FALSE(root_->descendant_needs_push_properties());
3153 EXPECT_FALSE(child_->needs_push_properties());
3154 EXPECT_FALSE(child_->descendant_needs_push_properties());
3155 EXPECT_FALSE(grandchild2_->needs_push_properties());
3156 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3157 EXPECT_FALSE(grandchild3_->needs_push_properties());
3158 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3160 child_->AddChild(grandchild1_);
3162 EXPECT_FALSE(root_->needs_push_properties());
3163 EXPECT_TRUE(root_->descendant_needs_push_properties());
3164 EXPECT_FALSE(child_->needs_push_properties());
3165 EXPECT_TRUE(child_->descendant_needs_push_properties());
3166 EXPECT_TRUE(grandchild1_->needs_push_properties());
3167 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3168 EXPECT_FALSE(grandchild2_->needs_push_properties());
3169 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3170 EXPECT_FALSE(grandchild3_->needs_push_properties());
3171 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3173 grandchild2_->SetPosition(gfx::Point(1, 1));
3175 EXPECT_FALSE(root_->needs_push_properties());
3176 EXPECT_TRUE(root_->descendant_needs_push_properties());
3177 EXPECT_FALSE(child_->needs_push_properties());
3178 EXPECT_TRUE(child_->descendant_needs_push_properties());
3179 EXPECT_TRUE(grandchild1_->needs_push_properties());
3180 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3181 EXPECT_TRUE(grandchild2_->needs_push_properties());
3182 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3183 EXPECT_FALSE(grandchild3_->needs_push_properties());
3184 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3186 // grandchild2_ will still need a push properties.
3187 grandchild1_->RemoveFromParent();
3189 EXPECT_FALSE(root_->needs_push_properties());
3190 EXPECT_TRUE(root_->descendant_needs_push_properties());
3191 EXPECT_FALSE(child_->needs_push_properties());
3192 EXPECT_TRUE(child_->descendant_needs_push_properties());
3194 // grandchild3_ does not need a push properties, so recursing should
3195 // no longer be needed.
3196 grandchild2_->RemoveFromParent();
3198 EXPECT_FALSE(root_->needs_push_properties());
3199 EXPECT_FALSE(root_->descendant_needs_push_properties());
3200 EXPECT_FALSE(child_->needs_push_properties());
3201 EXPECT_FALSE(child_->descendant_needs_push_properties());
3202 EndTest();
3203 break;
3208 MULTI_THREAD_TEST_F(LayerTreeHostTestPushPropertiesRemovingChildStopsRecursion);
3210 class LayerTreeHostTestPushPropertiesRemovingChildStopsRecursionWithPersistence
3211 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3212 protected:
3213 void DidCommitAndDrawFrame() override {
3214 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3215 switch (last_source_frame_number) {
3216 case 0:
3217 layer_tree_host()->SetRootLayer(root_);
3218 grandchild1_->set_persist_needs_push_properties(true);
3219 grandchild2_->set_persist_needs_push_properties(true);
3220 break;
3221 case 1:
3222 EXPECT_FALSE(root_->needs_push_properties());
3223 EXPECT_TRUE(root_->descendant_needs_push_properties());
3224 EXPECT_FALSE(child_->needs_push_properties());
3225 EXPECT_TRUE(child_->descendant_needs_push_properties());
3226 EXPECT_TRUE(grandchild1_->needs_push_properties());
3227 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3228 EXPECT_TRUE(grandchild2_->needs_push_properties());
3229 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3230 EXPECT_FALSE(grandchild3_->needs_push_properties());
3231 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3233 // grandchild2_ will still need a push properties.
3234 grandchild1_->RemoveFromParent();
3236 EXPECT_FALSE(root_->needs_push_properties());
3237 EXPECT_TRUE(root_->descendant_needs_push_properties());
3238 EXPECT_FALSE(child_->needs_push_properties());
3239 EXPECT_TRUE(child_->descendant_needs_push_properties());
3241 // grandchild3_ does not need a push properties, so recursing should
3242 // no longer be needed.
3243 grandchild2_->RemoveFromParent();
3245 EXPECT_FALSE(root_->needs_push_properties());
3246 EXPECT_FALSE(root_->descendant_needs_push_properties());
3247 EXPECT_FALSE(child_->needs_push_properties());
3248 EXPECT_FALSE(child_->descendant_needs_push_properties());
3249 EndTest();
3250 break;
3255 MULTI_THREAD_TEST_F(
3256 LayerTreeHostTestPushPropertiesRemovingChildStopsRecursionWithPersistence);
3258 class LayerTreeHostTestPushPropertiesSetPropertiesWhileOutsideTree
3259 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3260 protected:
3261 void DidCommitAndDrawFrame() override {
3262 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3263 switch (last_source_frame_number) {
3264 case 0:
3265 layer_tree_host()->SetRootLayer(root_);
3266 break;
3267 case 1:
3268 EXPECT_FALSE(root_->needs_push_properties());
3269 EXPECT_FALSE(root_->descendant_needs_push_properties());
3270 EXPECT_FALSE(child_->needs_push_properties());
3271 EXPECT_FALSE(child_->descendant_needs_push_properties());
3272 EXPECT_FALSE(grandchild1_->needs_push_properties());
3273 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3274 EXPECT_FALSE(grandchild2_->needs_push_properties());
3275 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3276 EXPECT_FALSE(grandchild3_->needs_push_properties());
3277 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3279 // Change grandchildren while their parent is not in the tree.
3280 child_->RemoveFromParent();
3281 grandchild1_->SetPosition(gfx::Point(1, 1));
3282 grandchild2_->SetPosition(gfx::Point(1, 1));
3283 root_->AddChild(child_);
3285 EXPECT_FALSE(root_->needs_push_properties());
3286 EXPECT_TRUE(root_->descendant_needs_push_properties());
3287 EXPECT_TRUE(child_->needs_push_properties());
3288 EXPECT_TRUE(child_->descendant_needs_push_properties());
3289 EXPECT_TRUE(grandchild1_->needs_push_properties());
3290 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3291 EXPECT_TRUE(grandchild2_->needs_push_properties());
3292 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3293 EXPECT_TRUE(grandchild3_->needs_push_properties());
3294 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3296 grandchild1_->RemoveFromParent();
3298 EXPECT_FALSE(root_->needs_push_properties());
3299 EXPECT_TRUE(root_->descendant_needs_push_properties());
3300 EXPECT_TRUE(child_->needs_push_properties());
3301 EXPECT_TRUE(child_->descendant_needs_push_properties());
3303 grandchild2_->RemoveFromParent();
3305 EXPECT_FALSE(root_->needs_push_properties());
3306 EXPECT_TRUE(root_->descendant_needs_push_properties());
3307 EXPECT_TRUE(child_->needs_push_properties());
3308 EXPECT_TRUE(child_->descendant_needs_push_properties());
3310 grandchild3_->RemoveFromParent();
3312 EXPECT_FALSE(root_->needs_push_properties());
3313 EXPECT_TRUE(root_->descendant_needs_push_properties());
3314 EXPECT_TRUE(child_->needs_push_properties());
3315 EXPECT_FALSE(child_->descendant_needs_push_properties());
3317 EndTest();
3318 break;
3323 MULTI_THREAD_TEST_F(
3324 LayerTreeHostTestPushPropertiesSetPropertiesWhileOutsideTree);
3326 class LayerTreeHostTestPushPropertiesSetPropertyInParentThenChild
3327 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3328 protected:
3329 void DidCommitAndDrawFrame() override {
3330 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3331 switch (last_source_frame_number) {
3332 case 0:
3333 layer_tree_host()->SetRootLayer(root_);
3334 break;
3335 case 1:
3336 EXPECT_FALSE(root_->needs_push_properties());
3337 EXPECT_FALSE(root_->descendant_needs_push_properties());
3338 EXPECT_FALSE(child_->needs_push_properties());
3339 EXPECT_FALSE(child_->descendant_needs_push_properties());
3340 EXPECT_FALSE(grandchild1_->needs_push_properties());
3341 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3342 EXPECT_FALSE(grandchild2_->needs_push_properties());
3343 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3344 EXPECT_FALSE(grandchild3_->needs_push_properties());
3345 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3347 child_->SetPosition(gfx::Point(1, 1));
3348 grandchild1_->SetPosition(gfx::Point(1, 1));
3349 grandchild2_->SetPosition(gfx::Point(1, 1));
3351 EXPECT_FALSE(root_->needs_push_properties());
3352 EXPECT_TRUE(root_->descendant_needs_push_properties());
3353 EXPECT_TRUE(child_->needs_push_properties());
3354 EXPECT_TRUE(child_->descendant_needs_push_properties());
3355 EXPECT_TRUE(grandchild1_->needs_push_properties());
3356 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3357 EXPECT_TRUE(grandchild2_->needs_push_properties());
3358 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3359 EXPECT_FALSE(grandchild3_->needs_push_properties());
3360 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3362 grandchild1_->RemoveFromParent();
3364 EXPECT_FALSE(root_->needs_push_properties());
3365 EXPECT_TRUE(root_->descendant_needs_push_properties());
3366 EXPECT_TRUE(child_->needs_push_properties());
3367 EXPECT_TRUE(child_->descendant_needs_push_properties());
3369 grandchild2_->RemoveFromParent();
3371 EXPECT_FALSE(root_->needs_push_properties());
3372 EXPECT_TRUE(root_->descendant_needs_push_properties());
3373 EXPECT_TRUE(child_->needs_push_properties());
3374 EXPECT_FALSE(child_->descendant_needs_push_properties());
3376 child_->RemoveFromParent();
3378 EXPECT_FALSE(root_->needs_push_properties());
3379 EXPECT_FALSE(root_->descendant_needs_push_properties());
3381 EndTest();
3382 break;
3387 MULTI_THREAD_TEST_F(
3388 LayerTreeHostTestPushPropertiesSetPropertyInParentThenChild);
3390 class LayerTreeHostTestPushPropertiesSetPropertyInChildThenParent
3391 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3392 protected:
3393 void DidCommitAndDrawFrame() override {
3394 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3395 switch (last_source_frame_number) {
3396 case 0:
3397 layer_tree_host()->SetRootLayer(root_);
3398 break;
3399 case 1:
3400 EXPECT_FALSE(root_->needs_push_properties());
3401 EXPECT_FALSE(root_->descendant_needs_push_properties());
3402 EXPECT_FALSE(child_->needs_push_properties());
3403 EXPECT_FALSE(child_->descendant_needs_push_properties());
3404 EXPECT_FALSE(grandchild1_->needs_push_properties());
3405 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3406 EXPECT_FALSE(grandchild2_->needs_push_properties());
3407 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3408 EXPECT_FALSE(grandchild3_->needs_push_properties());
3409 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3411 grandchild1_->SetPosition(gfx::Point(1, 1));
3412 grandchild2_->SetPosition(gfx::Point(1, 1));
3413 child_->SetPosition(gfx::Point(1, 1));
3415 EXPECT_FALSE(root_->needs_push_properties());
3416 EXPECT_TRUE(root_->descendant_needs_push_properties());
3417 EXPECT_TRUE(child_->needs_push_properties());
3418 EXPECT_TRUE(child_->descendant_needs_push_properties());
3419 EXPECT_TRUE(grandchild1_->needs_push_properties());
3420 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3421 EXPECT_TRUE(grandchild2_->needs_push_properties());
3422 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3423 EXPECT_FALSE(grandchild3_->needs_push_properties());
3424 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3426 grandchild1_->RemoveFromParent();
3428 EXPECT_FALSE(root_->needs_push_properties());
3429 EXPECT_TRUE(root_->descendant_needs_push_properties());
3430 EXPECT_TRUE(child_->needs_push_properties());
3431 EXPECT_TRUE(child_->descendant_needs_push_properties());
3433 grandchild2_->RemoveFromParent();
3435 EXPECT_FALSE(root_->needs_push_properties());
3436 EXPECT_TRUE(root_->descendant_needs_push_properties());
3437 EXPECT_TRUE(child_->needs_push_properties());
3438 EXPECT_FALSE(child_->descendant_needs_push_properties());
3440 child_->RemoveFromParent();
3442 EXPECT_FALSE(root_->needs_push_properties());
3443 EXPECT_FALSE(root_->descendant_needs_push_properties());
3445 EndTest();
3446 break;
3451 MULTI_THREAD_TEST_F(
3452 LayerTreeHostTestPushPropertiesSetPropertyInChildThenParent);
3454 // This test verifies that the tree activation callback is invoked correctly.
3455 class LayerTreeHostTestTreeActivationCallback : public LayerTreeHostTest {
3456 public:
3457 LayerTreeHostTestTreeActivationCallback()
3458 : num_commits_(0), callback_count_(0) {}
3460 void BeginTest() override {
3461 PostSetNeedsCommitToMainThread();
3464 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
3465 LayerTreeHostImpl::FrameData* frame_data,
3466 DrawResult draw_result) override {
3467 ++num_commits_;
3468 switch (num_commits_) {
3469 case 1:
3470 EXPECT_EQ(0, callback_count_);
3471 callback_count_ = 0;
3472 SetCallback(true);
3473 PostSetNeedsCommitToMainThread();
3474 break;
3475 case 2:
3476 EXPECT_EQ(1, callback_count_);
3477 callback_count_ = 0;
3478 SetCallback(false);
3479 PostSetNeedsCommitToMainThread();
3480 break;
3481 case 3:
3482 EXPECT_EQ(0, callback_count_);
3483 callback_count_ = 0;
3484 EndTest();
3485 break;
3486 default:
3487 ADD_FAILURE() << num_commits_;
3488 EndTest();
3489 break;
3491 return LayerTreeHostTest::PrepareToDrawOnThread(
3492 host_impl, frame_data, draw_result);
3495 void AfterTest() override { EXPECT_EQ(3, num_commits_); }
3497 void SetCallback(bool enable) {
3498 output_surface()->SetTreeActivationCallback(
3499 enable
3500 ? base::Bind(
3501 &LayerTreeHostTestTreeActivationCallback::ActivationCallback,
3502 base::Unretained(this))
3503 : base::Closure());
3506 void ActivationCallback() { ++callback_count_; }
3508 int num_commits_;
3509 int callback_count_;
3512 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestTreeActivationCallback);
3514 class LayerInvalidateCausesDraw : public LayerTreeHostTest {
3515 public:
3516 LayerInvalidateCausesDraw() : num_commits_(0), num_draws_(0) {}
3518 void BeginTest() override {
3519 ASSERT_TRUE(invalidate_layer_)
3520 << "Derived tests must set this in SetupTree";
3522 // One initial commit.
3523 PostSetNeedsCommitToMainThread();
3526 void DidCommitAndDrawFrame() override {
3527 // After commit, invalidate the layer. This should cause a commit.
3528 if (layer_tree_host()->source_frame_number() == 1)
3529 invalidate_layer_->SetNeedsDisplay();
3532 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
3533 num_draws_++;
3534 if (impl->active_tree()->source_frame_number() == 1)
3535 EndTest();
3538 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
3539 num_commits_++;
3542 void AfterTest() override {
3543 EXPECT_GE(2, num_commits_);
3544 EXPECT_GE(2, num_draws_);
3547 protected:
3548 scoped_refptr<Layer> invalidate_layer_;
3550 private:
3551 int num_commits_;
3552 int num_draws_;
3555 // VideoLayer must support being invalidated and then passing that along
3556 // to the compositor thread, even though no resources are updated in
3557 // response to that invalidation.
3558 class LayerTreeHostTestVideoLayerInvalidate : public LayerInvalidateCausesDraw {
3559 public:
3560 void SetupTree() override {
3561 LayerTreeHostTest::SetupTree();
3562 scoped_refptr<VideoLayer> video_layer = VideoLayer::Create(
3563 layer_settings(), &provider_, media::VIDEO_ROTATION_0);
3564 video_layer->SetBounds(gfx::Size(10, 10));
3565 video_layer->SetIsDrawable(true);
3566 layer_tree_host()->root_layer()->AddChild(video_layer);
3568 invalidate_layer_ = video_layer;
3571 private:
3572 FakeVideoFrameProvider provider_;
3575 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestVideoLayerInvalidate);
3577 // IOSurfaceLayer must support being invalidated and then passing that along
3578 // to the compositor thread, even though no resources are updated in
3579 // response to that invalidation.
3580 class LayerTreeHostTestIOSurfaceLayerInvalidate
3581 : public LayerInvalidateCausesDraw {
3582 public:
3583 void SetupTree() override {
3584 LayerTreeHostTest::SetupTree();
3585 scoped_refptr<IOSurfaceLayer> layer =
3586 IOSurfaceLayer::Create(layer_settings());
3587 layer->SetBounds(gfx::Size(10, 10));
3588 uint32_t fake_io_surface_id = 7;
3589 layer->SetIOSurfaceProperties(fake_io_surface_id, layer->bounds());
3590 layer->SetIsDrawable(true);
3591 layer_tree_host()->root_layer()->AddChild(layer);
3593 invalidate_layer_ = layer;
3597 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestIOSurfaceLayerInvalidate);
3599 class LayerTreeHostTestPushHiddenLayer : public LayerTreeHostTest {
3600 protected:
3601 void SetupTree() override {
3602 root_layer_ = Layer::Create(layer_settings());
3603 root_layer_->CreateRenderSurface();
3604 root_layer_->SetPosition(gfx::Point());
3605 root_layer_->SetBounds(gfx::Size(10, 10));
3607 parent_layer_ = SolidColorLayer::Create(layer_settings());
3608 parent_layer_->SetPosition(gfx::Point());
3609 parent_layer_->SetBounds(gfx::Size(10, 10));
3610 parent_layer_->SetIsDrawable(true);
3611 root_layer_->AddChild(parent_layer_);
3613 child_layer_ = SolidColorLayer::Create(layer_settings());
3614 child_layer_->SetPosition(gfx::Point());
3615 child_layer_->SetBounds(gfx::Size(10, 10));
3616 child_layer_->SetIsDrawable(true);
3617 parent_layer_->AddChild(child_layer_);
3619 layer_tree_host()->SetRootLayer(root_layer_);
3620 LayerTreeHostTest::SetupTree();
3623 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
3625 void DidCommitAndDrawFrame() override {
3626 switch (layer_tree_host()->source_frame_number()) {
3627 case 1:
3628 // The layer type used does not need to push properties every frame.
3629 EXPECT_FALSE(child_layer_->needs_push_properties());
3631 // Change the bounds of the child layer, but make it skipped
3632 // by CalculateDrawProperties.
3633 parent_layer_->SetOpacity(0.f);
3634 child_layer_->SetBounds(gfx::Size(5, 5));
3635 break;
3636 case 2:
3637 // The bounds of the child layer were pushed to the impl side.
3638 EXPECT_FALSE(child_layer_->needs_push_properties());
3640 EndTest();
3641 break;
3645 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
3646 LayerImpl* root = impl->active_tree()->root_layer();
3647 LayerImpl* parent = root->children()[0];
3648 LayerImpl* child = parent->children()[0];
3650 switch (impl->active_tree()->source_frame_number()) {
3651 case 1:
3652 EXPECT_EQ(gfx::Size(5, 5).ToString(), child->bounds().ToString());
3653 break;
3657 void AfterTest() override {}
3659 scoped_refptr<Layer> root_layer_;
3660 scoped_refptr<SolidColorLayer> parent_layer_;
3661 scoped_refptr<SolidColorLayer> child_layer_;
3664 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestPushHiddenLayer);
3666 class LayerTreeHostTestUpdateLayerInEmptyViewport : public LayerTreeHostTest {
3667 protected:
3668 void SetupTree() override {
3669 root_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
3670 root_layer_->SetBounds(gfx::Size(10, 10));
3672 layer_tree_host()->SetRootLayer(root_layer_);
3673 LayerTreeHostTest::SetupTree();
3676 void BeginTest() override {
3677 // The viewport is empty, but we still need to update layers on the main
3678 // thread.
3679 layer_tree_host()->SetViewportSize(gfx::Size(0, 0));
3680 PostSetNeedsCommitToMainThread();
3683 void DidCommit() override {
3684 // The layer should be updated even though the viewport is empty, so we
3685 // are capable of drawing it on the impl tree.
3686 EXPECT_GT(root_layer_->update_count(), 0);
3687 EndTest();
3690 void AfterTest() override {}
3692 FakeContentLayerClient client_;
3693 scoped_refptr<FakePictureLayer> root_layer_;
3696 MULTI_THREAD_TEST_F(LayerTreeHostTestUpdateLayerInEmptyViewport);
3698 class LayerTreeHostTestMaxTransferBufferUsageBytes : public LayerTreeHostTest {
3699 protected:
3700 void InitializeSettings(LayerTreeSettings* settings) override {
3701 // Testing async uploads.
3702 settings->use_zero_copy = false;
3703 settings->use_one_copy = false;
3706 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
3707 scoped_refptr<TestContextProvider> context_provider =
3708 TestContextProvider::Create();
3709 context_provider->SetMaxTransferBufferUsageBytes(512 * 512);
3710 if (delegating_renderer())
3711 return FakeOutputSurface::CreateDelegating3d(context_provider);
3712 else
3713 return FakeOutputSurface::Create3d(context_provider);
3716 void SetupTree() override {
3717 client_.set_fill_with_nonsolid_color(true);
3718 scoped_refptr<FakePictureLayer> root_layer =
3719 FakePictureLayer::Create(layer_settings(), &client_);
3720 root_layer->SetBounds(gfx::Size(1024, 1024));
3721 root_layer->SetIsDrawable(true);
3723 layer_tree_host()->SetRootLayer(root_layer);
3724 LayerTreeHostTest::SetupTree();
3727 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
3729 void DidActivateTreeOnThread(LayerTreeHostImpl* impl) override {
3730 TestWebGraphicsContext3D* context = TestContext();
3732 // Expect that the transfer buffer memory used is equal to the
3733 // MaxTransferBufferUsageBytes value set in CreateOutputSurface.
3734 EXPECT_EQ(512 * 512u, context->max_used_transfer_buffer_usage_bytes());
3735 EndTest();
3738 void AfterTest() override {}
3740 private:
3741 FakeContentLayerClient client_;
3744 // Impl-side painting is a multi-threaded compositor feature.
3745 MULTI_THREAD_TEST_F(LayerTreeHostTestMaxTransferBufferUsageBytes);
3747 class LayerTreeHostTestSetMemoryPolicyOnLostOutputSurface
3748 : public LayerTreeHostTest {
3749 protected:
3750 LayerTreeHostTestSetMemoryPolicyOnLostOutputSurface()
3751 : first_output_surface_memory_limit_(4321234),
3752 second_output_surface_memory_limit_(1234321) {}
3754 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
3755 if (!first_context_provider_.get()) {
3756 first_context_provider_ = TestContextProvider::Create();
3757 } else {
3758 EXPECT_FALSE(second_context_provider_.get());
3759 second_context_provider_ = TestContextProvider::Create();
3762 scoped_refptr<TestContextProvider> provider(second_context_provider_.get()
3763 ? second_context_provider_
3764 : first_context_provider_);
3765 scoped_ptr<FakeOutputSurface> output_surface;
3766 if (delegating_renderer())
3767 output_surface = FakeOutputSurface::CreateDelegating3d(provider);
3768 else
3769 output_surface = FakeOutputSurface::Create3d(provider);
3770 output_surface->SetMemoryPolicyToSetAtBind(
3771 make_scoped_ptr(new ManagedMemoryPolicy(
3772 second_context_provider_.get() ? second_output_surface_memory_limit_
3773 : first_output_surface_memory_limit_,
3774 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
3775 ManagedMemoryPolicy::kDefaultNumResourcesLimit)));
3776 return output_surface.Pass();
3779 void SetupTree() override {
3780 root_ = FakePictureLayer::Create(layer_settings(), &client_);
3781 root_->SetBounds(gfx::Size(20, 20));
3782 layer_tree_host()->SetRootLayer(root_);
3783 LayerTreeHostTest::SetupTree();
3786 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
3788 void DidCommitAndDrawFrame() override {
3789 // Lost context sometimes takes two frames to recreate. The third frame
3790 // is sometimes aborted, so wait until the fourth frame to verify that
3791 // the memory has been set, and the fifth frame to end the test.
3792 if (layer_tree_host()->source_frame_number() < 5) {
3793 layer_tree_host()->SetNeedsCommit();
3794 } else if (layer_tree_host()->source_frame_number() == 5) {
3795 EndTest();
3799 void SwapBuffersOnThread(LayerTreeHostImpl* impl, bool result) override {
3800 switch (impl->active_tree()->source_frame_number()) {
3801 case 1:
3802 EXPECT_EQ(first_output_surface_memory_limit_,
3803 impl->memory_allocation_limit_bytes());
3804 // Lose the output surface.
3805 first_context_provider_->TestContext3d()->loseContextCHROMIUM(
3806 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
3807 break;
3808 case 4:
3809 EXPECT_EQ(second_output_surface_memory_limit_,
3810 impl->memory_allocation_limit_bytes());
3811 break;
3815 void AfterTest() override {}
3817 scoped_refptr<TestContextProvider> first_context_provider_;
3818 scoped_refptr<TestContextProvider> second_context_provider_;
3819 size_t first_output_surface_memory_limit_;
3820 size_t second_output_surface_memory_limit_;
3821 FakeContentLayerClient client_;
3822 scoped_refptr<Layer> root_;
3825 SINGLE_AND_MULTI_THREAD_TEST_F(
3826 LayerTreeHostTestSetMemoryPolicyOnLostOutputSurface);
3828 struct TestSwapPromiseResult {
3829 TestSwapPromiseResult()
3830 : did_activate_called(false),
3831 did_swap_called(false),
3832 did_not_swap_called(false),
3833 dtor_called(false),
3834 reason(SwapPromise::COMMIT_FAILS) {}
3836 bool did_activate_called;
3837 bool did_swap_called;
3838 bool did_not_swap_called;
3839 bool dtor_called;
3840 SwapPromise::DidNotSwapReason reason;
3841 base::Lock lock;
3844 class TestSwapPromise : public SwapPromise {
3845 public:
3846 explicit TestSwapPromise(TestSwapPromiseResult* result) : result_(result) {}
3848 ~TestSwapPromise() override {
3849 base::AutoLock lock(result_->lock);
3850 result_->dtor_called = true;
3853 void DidActivate() override {
3854 base::AutoLock lock(result_->lock);
3855 EXPECT_FALSE(result_->did_activate_called);
3856 EXPECT_FALSE(result_->did_swap_called);
3857 EXPECT_FALSE(result_->did_not_swap_called);
3858 result_->did_activate_called = true;
3861 void DidSwap(CompositorFrameMetadata* metadata) override {
3862 base::AutoLock lock(result_->lock);
3863 EXPECT_TRUE(result_->did_activate_called);
3864 EXPECT_FALSE(result_->did_swap_called);
3865 EXPECT_FALSE(result_->did_not_swap_called);
3866 result_->did_swap_called = true;
3869 void DidNotSwap(DidNotSwapReason reason) override {
3870 base::AutoLock lock(result_->lock);
3871 EXPECT_FALSE(result_->did_swap_called);
3872 EXPECT_FALSE(result_->did_not_swap_called);
3873 EXPECT_FALSE(result_->did_activate_called &&
3874 reason != DidNotSwapReason::SWAP_FAILS);
3875 result_->did_not_swap_called = true;
3876 result_->reason = reason;
3879 int64 TraceId() const override { return 0; }
3881 private:
3882 // Not owned.
3883 TestSwapPromiseResult* result_;
3886 class LayerTreeHostTestBreakSwapPromise : public LayerTreeHostTest {
3887 protected:
3888 LayerTreeHostTestBreakSwapPromise()
3889 : commit_count_(0), commit_complete_count_(0) {}
3891 void WillBeginMainFrame() override {
3892 ASSERT_LE(commit_count_, 2);
3893 scoped_ptr<SwapPromise> swap_promise(
3894 new TestSwapPromise(&swap_promise_result_[commit_count_]));
3895 layer_tree_host()->QueueSwapPromise(swap_promise.Pass());
3898 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
3900 void DidCommit() override {
3901 commit_count_++;
3902 if (commit_count_ == 2) {
3903 // This commit will finish.
3904 layer_tree_host()->SetNeedsCommit();
3908 void WillActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
3909 if (host_impl->pending_tree()) {
3910 int frame = host_impl->pending_tree()->source_frame_number();
3911 base::AutoLock lock(swap_promise_result_[frame].lock);
3912 EXPECT_FALSE(swap_promise_result_[frame].did_activate_called);
3913 EXPECT_FALSE(swap_promise_result_[frame].did_swap_called);
3917 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
3918 int frame = host_impl->active_tree()->source_frame_number();
3919 base::AutoLock lock(swap_promise_result_[frame].lock);
3920 EXPECT_TRUE(swap_promise_result_[frame].did_activate_called);
3921 EXPECT_FALSE(swap_promise_result_[frame].did_swap_called);
3924 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
3925 commit_complete_count_++;
3926 if (commit_complete_count_ == 1) {
3927 // This commit will be aborted because no actual update.
3928 PostSetNeedsUpdateLayersToMainThread();
3932 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
3933 int frame = host_impl->active_tree()->source_frame_number();
3934 if (frame == 2) {
3935 EndTest();
3939 void AfterTest() override {
3940 // 3 commits are scheduled. 2 completes. 1 is aborted.
3941 EXPECT_EQ(commit_count_, 3);
3942 EXPECT_EQ(commit_complete_count_, 2);
3945 // The first commit completes and causes swap buffer which finishes
3946 // the promise.
3947 base::AutoLock lock(swap_promise_result_[0].lock);
3948 EXPECT_TRUE(swap_promise_result_[0].did_swap_called);
3949 EXPECT_FALSE(swap_promise_result_[0].did_not_swap_called);
3950 EXPECT_TRUE(swap_promise_result_[0].dtor_called);
3954 // The second commit is aborted since it contains no updates.
3955 base::AutoLock lock(swap_promise_result_[1].lock);
3956 EXPECT_FALSE(swap_promise_result_[1].did_activate_called);
3957 EXPECT_FALSE(swap_promise_result_[1].did_swap_called);
3958 EXPECT_TRUE(swap_promise_result_[1].did_not_swap_called);
3959 EXPECT_EQ(SwapPromise::COMMIT_NO_UPDATE, swap_promise_result_[1].reason);
3960 EXPECT_TRUE(swap_promise_result_[1].dtor_called);
3964 // The last commit completes but it does not cause swap buffer because
3965 // there is no damage in the frame data.
3966 base::AutoLock lock(swap_promise_result_[2].lock);
3967 EXPECT_TRUE(swap_promise_result_[2].did_activate_called);
3968 EXPECT_FALSE(swap_promise_result_[2].did_swap_called);
3969 EXPECT_TRUE(swap_promise_result_[2].did_not_swap_called);
3970 EXPECT_EQ(SwapPromise::SWAP_FAILS, swap_promise_result_[2].reason);
3971 EXPECT_TRUE(swap_promise_result_[2].dtor_called);
3975 int commit_count_;
3976 int commit_complete_count_;
3977 TestSwapPromiseResult swap_promise_result_[3];
3980 MULTI_THREAD_TEST_F(LayerTreeHostTestBreakSwapPromise);
3982 class LayerTreeHostTestKeepSwapPromise : public LayerTreeTest {
3983 public:
3984 LayerTreeHostTestKeepSwapPromise() {}
3986 void BeginTest() override {
3987 layer_ = SolidColorLayer::Create(layer_settings());
3988 layer_->SetIsDrawable(true);
3989 layer_->SetBounds(gfx::Size(10, 10));
3990 layer_tree_host()->SetRootLayer(layer_);
3991 gfx::Size bounds(100, 100);
3992 layer_tree_host()->SetViewportSize(bounds);
3993 PostSetNeedsCommitToMainThread();
3996 void DidCommit() override {
3997 MainThreadTaskRunner()->PostTask(
3998 FROM_HERE, base::Bind(&LayerTreeHostTestKeepSwapPromise::ChangeFrame,
3999 base::Unretained(this)));
4002 void ChangeFrame() {
4003 switch (layer_tree_host()->source_frame_number()) {
4004 case 1:
4005 layer_->SetBounds(gfx::Size(10, 11));
4006 layer_tree_host()->QueueSwapPromise(
4007 make_scoped_ptr(new TestSwapPromise(&swap_promise_result_)));
4008 break;
4009 case 2:
4010 break;
4011 default:
4012 NOTREACHED();
4013 break;
4017 void WillActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4018 if (host_impl->pending_tree()) {
4019 if (host_impl->pending_tree()->source_frame_number() == 1) {
4020 base::AutoLock lock(swap_promise_result_.lock);
4021 EXPECT_FALSE(swap_promise_result_.did_activate_called);
4022 EXPECT_FALSE(swap_promise_result_.did_swap_called);
4023 SetCallback(true);
4024 } else {
4025 SetCallback(false);
4030 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4031 if (host_impl->active_tree()->source_frame_number() == 1) {
4032 base::AutoLock lock(swap_promise_result_.lock);
4033 EXPECT_TRUE(swap_promise_result_.did_activate_called);
4034 EXPECT_FALSE(swap_promise_result_.did_swap_called);
4038 void ActivationCallback() {
4039 // DidActivate needs to happen before the tree activation callback.
4040 base::AutoLock lock(swap_promise_result_.lock);
4041 EXPECT_TRUE(swap_promise_result_.did_activate_called);
4044 void SetCallback(bool enable) {
4045 output_surface()->SetTreeActivationCallback(
4046 enable
4047 ? base::Bind(&LayerTreeHostTestKeepSwapPromise::ActivationCallback,
4048 base::Unretained(this))
4049 : base::Closure());
4052 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
4053 EXPECT_TRUE(result);
4054 if (host_impl->active_tree()->source_frame_number() >= 1) {
4055 // The commit changes layers so it should cause a swap.
4056 base::AutoLock lock(swap_promise_result_.lock);
4057 EXPECT_TRUE(swap_promise_result_.did_swap_called);
4058 EXPECT_FALSE(swap_promise_result_.did_not_swap_called);
4059 EXPECT_TRUE(swap_promise_result_.dtor_called);
4060 EndTest();
4064 void AfterTest() override {}
4066 private:
4067 scoped_refptr<Layer> layer_;
4068 TestSwapPromiseResult swap_promise_result_;
4071 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestKeepSwapPromise);
4073 class LayerTreeHostTestBreakSwapPromiseForVisibility
4074 : public LayerTreeHostTest {
4075 protected:
4076 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
4078 void SetVisibleFalseAndQueueSwapPromise() {
4079 layer_tree_host()->SetVisible(false);
4080 scoped_ptr<SwapPromise> swap_promise(
4081 new TestSwapPromise(&swap_promise_result_));
4082 layer_tree_host()->QueueSwapPromise(swap_promise.Pass());
4085 void ScheduledActionWillSendBeginMainFrame() override {
4086 MainThreadTaskRunner()->PostTask(
4087 FROM_HERE,
4088 base::Bind(&LayerTreeHostTestBreakSwapPromiseForVisibility
4089 ::SetVisibleFalseAndQueueSwapPromise,
4090 base::Unretained(this)));
4093 void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl,
4094 CommitEarlyOutReason reason) override {
4095 EndTest();
4098 void AfterTest() override {
4100 base::AutoLock lock(swap_promise_result_.lock);
4101 EXPECT_FALSE(swap_promise_result_.did_activate_called);
4102 EXPECT_FALSE(swap_promise_result_.did_swap_called);
4103 EXPECT_TRUE(swap_promise_result_.did_not_swap_called);
4104 EXPECT_EQ(SwapPromise::COMMIT_FAILS, swap_promise_result_.reason);
4105 EXPECT_TRUE(swap_promise_result_.dtor_called);
4109 TestSwapPromiseResult swap_promise_result_;
4112 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestBreakSwapPromiseForVisibility);
4114 class LayerTreeHostTestBreakSwapPromiseForContext : public LayerTreeHostTest {
4115 protected:
4116 LayerTreeHostTestBreakSwapPromiseForContext()
4117 : output_surface_lost_triggered_(false) {
4120 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
4122 void LoseOutputSurfaceAndQueueSwapPromise() {
4123 layer_tree_host()->DidLoseOutputSurface();
4124 scoped_ptr<SwapPromise> swap_promise(
4125 new TestSwapPromise(&swap_promise_result_));
4126 layer_tree_host()->QueueSwapPromise(swap_promise.Pass());
4129 void ScheduledActionWillSendBeginMainFrame() override {
4130 if (output_surface_lost_triggered_)
4131 return;
4132 output_surface_lost_triggered_ = true;
4134 MainThreadTaskRunner()->PostTask(
4135 FROM_HERE,
4136 base::Bind(&LayerTreeHostTestBreakSwapPromiseForContext
4137 ::LoseOutputSurfaceAndQueueSwapPromise,
4138 base::Unretained(this)));
4141 void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl,
4142 CommitEarlyOutReason reason) override {
4143 // This is needed so that the impl-thread state matches main-thread state.
4144 host_impl->DidLoseOutputSurface();
4145 EndTest();
4148 void AfterTest() override {
4150 base::AutoLock lock(swap_promise_result_.lock);
4151 EXPECT_FALSE(swap_promise_result_.did_activate_called);
4152 EXPECT_FALSE(swap_promise_result_.did_swap_called);
4153 EXPECT_TRUE(swap_promise_result_.did_not_swap_called);
4154 EXPECT_EQ(SwapPromise::COMMIT_FAILS, swap_promise_result_.reason);
4155 EXPECT_TRUE(swap_promise_result_.dtor_called);
4159 bool output_surface_lost_triggered_;
4160 TestSwapPromiseResult swap_promise_result_;
4163 SINGLE_AND_MULTI_THREAD_TEST_F(
4164 LayerTreeHostTestBreakSwapPromiseForContext);
4166 class SimpleSwapPromiseMonitor : public SwapPromiseMonitor {
4167 public:
4168 SimpleSwapPromiseMonitor(LayerTreeHost* layer_tree_host,
4169 LayerTreeHostImpl* layer_tree_host_impl,
4170 int* set_needs_commit_count,
4171 int* set_needs_redraw_count)
4172 : SwapPromiseMonitor(layer_tree_host, layer_tree_host_impl),
4173 set_needs_commit_count_(set_needs_commit_count) {}
4175 ~SimpleSwapPromiseMonitor() override {}
4177 void OnSetNeedsCommitOnMain() override { (*set_needs_commit_count_)++; }
4179 void OnSetNeedsRedrawOnImpl() override {
4180 ADD_FAILURE() << "Should not get called on main thread.";
4183 void OnForwardScrollUpdateToMainThreadOnImpl() override {
4184 ADD_FAILURE() << "Should not get called on main thread.";
4187 private:
4188 int* set_needs_commit_count_;
4191 class LayerTreeHostTestSimpleSwapPromiseMonitor : public LayerTreeHostTest {
4192 public:
4193 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
4195 void WillBeginMainFrame() override {
4196 if (TestEnded())
4197 return;
4199 int set_needs_commit_count = 0;
4200 int set_needs_redraw_count = 0;
4203 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
4204 new SimpleSwapPromiseMonitor(layer_tree_host(),
4205 NULL,
4206 &set_needs_commit_count,
4207 &set_needs_redraw_count));
4208 layer_tree_host()->SetNeedsCommit();
4209 EXPECT_EQ(1, set_needs_commit_count);
4210 EXPECT_EQ(0, set_needs_redraw_count);
4213 // Now the monitor is destroyed, SetNeedsCommit() is no longer being
4214 // monitored.
4215 layer_tree_host()->SetNeedsCommit();
4216 EXPECT_EQ(1, set_needs_commit_count);
4217 EXPECT_EQ(0, set_needs_redraw_count);
4220 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
4221 new SimpleSwapPromiseMonitor(layer_tree_host(),
4222 NULL,
4223 &set_needs_commit_count,
4224 &set_needs_redraw_count));
4225 layer_tree_host()->SetNeedsUpdateLayers();
4226 EXPECT_EQ(2, set_needs_commit_count);
4227 EXPECT_EQ(0, set_needs_redraw_count);
4231 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
4232 new SimpleSwapPromiseMonitor(layer_tree_host(),
4233 NULL,
4234 &set_needs_commit_count,
4235 &set_needs_redraw_count));
4236 layer_tree_host()->SetNeedsAnimate();
4237 EXPECT_EQ(3, set_needs_commit_count);
4238 EXPECT_EQ(0, set_needs_redraw_count);
4241 EndTest();
4244 void AfterTest() override {}
4247 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSimpleSwapPromiseMonitor);
4249 class LayerTreeHostTestHighResRequiredAfterEvictingUIResources
4250 : public LayerTreeHostTest {
4251 protected:
4252 void SetupTree() override {
4253 LayerTreeHostTest::SetupTree();
4254 ui_resource_ = FakeScopedUIResource::Create(layer_tree_host());
4257 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
4259 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4260 host_impl->EvictAllUIResources();
4261 // Existence of evicted UI resources will trigger NEW_CONTENT_TAKES_PRIORITY
4262 // mode. Active tree should require high-res to draw after entering this
4263 // mode to ensure that high-res tiles are also required for a pending tree
4264 // to be activated.
4265 EXPECT_TRUE(host_impl->RequiresHighResToDraw());
4268 void DidCommit() override {
4269 int frame = layer_tree_host()->source_frame_number();
4270 switch (frame) {
4271 case 1:
4272 PostSetNeedsCommitToMainThread();
4273 break;
4274 case 2:
4275 ui_resource_ = nullptr;
4276 EndTest();
4277 break;
4281 void AfterTest() override {}
4283 FakeContentLayerClient client_;
4284 scoped_ptr<FakeScopedUIResource> ui_resource_;
4287 // This test is flaky, see http://crbug.com/386199
4288 // MULTI_THREAD_TEST_F(LayerTreeHostTestHighResRequiredAfterEvictingUIResources)
4290 class LayerTreeHostTestGpuRasterizationDefault : public LayerTreeHostTest {
4291 protected:
4292 void InitializeSettings(LayerTreeSettings* settings) override {
4293 EXPECT_FALSE(settings->gpu_rasterization_enabled);
4294 EXPECT_FALSE(settings->gpu_rasterization_forced);
4297 void SetupTree() override {
4298 LayerTreeHostTest::SetupTree();
4300 scoped_refptr<PictureLayer> layer =
4301 PictureLayer::Create(layer_settings(), &layer_client_);
4302 layer->SetBounds(gfx::Size(10, 10));
4303 layer->SetIsDrawable(true);
4304 layer_tree_host()->root_layer()->AddChild(layer);
4307 void BeginTest() override {
4308 Layer* root = layer_tree_host()->root_layer();
4309 PictureLayer* layer = static_cast<PictureLayer*>(root->child_at(0));
4310 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4312 // Verify default values.
4313 EXPECT_TRUE(root->IsSuitableForGpuRasterization());
4314 EXPECT_TRUE(layer->IsSuitableForGpuRasterization());
4315 EXPECT_TRUE(recording_source->IsSuitableForGpuRasterization());
4316 EXPECT_FALSE(layer_tree_host()->has_gpu_rasterization_trigger());
4318 // Setting gpu rasterization trigger does not enable gpu rasterization.
4319 layer_tree_host()->SetHasGpuRasterizationTrigger(true);
4320 EXPECT_TRUE(layer_tree_host()->has_gpu_rasterization_trigger());
4322 PostSetNeedsCommitToMainThread();
4325 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
4326 EXPECT_FALSE(host_impl->pending_tree()->use_gpu_rasterization());
4327 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4330 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4331 EXPECT_FALSE(host_impl->active_tree()->use_gpu_rasterization());
4332 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4333 EndTest();
4336 void AfterTest() override {}
4338 FakeContentLayerClient layer_client_;
4341 MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterizationDefault);
4343 class LayerTreeHostTestGpuRasterizationEnabled : public LayerTreeHostTest {
4344 protected:
4345 void InitializeSettings(LayerTreeSettings* settings) override {
4346 EXPECT_FALSE(settings->gpu_rasterization_enabled);
4347 settings->gpu_rasterization_enabled = true;
4350 void SetupTree() override {
4351 LayerTreeHostTest::SetupTree();
4353 scoped_refptr<PictureLayer> layer =
4354 PictureLayer::Create(layer_settings(), &layer_client_);
4355 layer->SetBounds(gfx::Size(10, 10));
4356 layer->SetIsDrawable(true);
4357 layer_tree_host()->root_layer()->AddChild(layer);
4360 void BeginTest() override {
4361 Layer* root = layer_tree_host()->root_layer();
4362 PictureLayer* layer = static_cast<PictureLayer*>(root->child_at(0));
4363 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4365 // Verify default values.
4366 EXPECT_TRUE(root->IsSuitableForGpuRasterization());
4367 EXPECT_TRUE(layer->IsSuitableForGpuRasterization());
4368 EXPECT_TRUE(recording_source->IsSuitableForGpuRasterization());
4369 EXPECT_FALSE(layer_tree_host()->has_gpu_rasterization_trigger());
4371 // Gpu rasterization trigger is relevant.
4372 layer_tree_host()->SetHasGpuRasterizationTrigger(true);
4373 EXPECT_TRUE(layer_tree_host()->has_gpu_rasterization_trigger());
4375 // Content-based veto is relevant as well.
4376 recording_source->SetUnsuitableForGpuRasterizationForTesting();
4377 EXPECT_FALSE(recording_source->IsSuitableForGpuRasterization());
4378 EXPECT_FALSE(layer->IsSuitableForGpuRasterization());
4379 // Veto will take effect when layers are updated.
4380 // The results will be verified after commit is completed below.
4381 // Since we are manually marking picture pile as unsuitable,
4382 // make sure that the layer gets a chance to update.
4383 layer->SetNeedsDisplay();
4384 PostSetNeedsCommitToMainThread();
4387 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
4388 EXPECT_FALSE(host_impl->pending_tree()->use_gpu_rasterization());
4389 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4392 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4393 EXPECT_FALSE(host_impl->active_tree()->use_gpu_rasterization());
4394 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4395 EndTest();
4398 void AfterTest() override {}
4400 FakeContentLayerClient layer_client_;
4403 MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterizationEnabled);
4405 class LayerTreeHostTestGpuRasterizationForced : public LayerTreeHostTest {
4406 protected:
4407 void InitializeSettings(LayerTreeSettings* settings) override {
4408 EXPECT_FALSE(settings->gpu_rasterization_forced);
4409 settings->gpu_rasterization_forced = true;
4412 void SetupTree() override {
4413 LayerTreeHostTest::SetupTree();
4415 scoped_refptr<FakePictureLayer> layer =
4416 FakePictureLayer::Create(layer_settings(), &layer_client_);
4417 layer->SetBounds(gfx::Size(10, 10));
4418 layer->SetIsDrawable(true);
4419 layer_tree_host()->root_layer()->AddChild(layer);
4422 void BeginTest() override {
4423 Layer* root = layer_tree_host()->root_layer();
4424 PictureLayer* layer = static_cast<PictureLayer*>(root->child_at(0));
4425 RecordingSource* recording_source = layer->GetRecordingSourceForTesting();
4427 // Verify default values.
4428 EXPECT_TRUE(root->IsSuitableForGpuRasterization());
4429 EXPECT_TRUE(layer->IsSuitableForGpuRasterization());
4430 EXPECT_TRUE(recording_source->IsSuitableForGpuRasterization());
4431 EXPECT_FALSE(layer_tree_host()->has_gpu_rasterization_trigger());
4433 // With gpu rasterization forced, gpu rasterization trigger is irrelevant.
4434 layer_tree_host()->SetHasGpuRasterizationTrigger(true);
4435 EXPECT_TRUE(layer_tree_host()->has_gpu_rasterization_trigger());
4437 // Content-based veto is irrelevant as well.
4438 recording_source->SetUnsuitableForGpuRasterizationForTesting();
4439 EXPECT_FALSE(recording_source->IsSuitableForGpuRasterization());
4440 EXPECT_FALSE(layer->IsSuitableForGpuRasterization());
4441 // Veto will take effect when layers are updated.
4442 // The results will be verified after commit is completed below.
4443 // Since we are manually marking picture pile as unsuitable,
4444 // make sure that the layer gets a chance to update.
4445 layer->SetNeedsDisplay();
4446 PostSetNeedsCommitToMainThread();
4449 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
4450 EXPECT_TRUE(host_impl->sync_tree()->use_gpu_rasterization());
4451 EXPECT_TRUE(host_impl->use_gpu_rasterization());
4454 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4455 EXPECT_TRUE(host_impl->active_tree()->use_gpu_rasterization());
4456 EXPECT_TRUE(host_impl->use_gpu_rasterization());
4457 EndTest();
4460 void AfterTest() override {}
4462 FakeContentLayerClient layer_client_;
4465 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterizationForced);
4467 class LayerTreeHostTestContinuousPainting : public LayerTreeHostTest {
4468 public:
4469 LayerTreeHostTestContinuousPainting()
4470 : num_commits_(0), num_draws_(0), bounds_(20, 20), child_layer_(NULL) {}
4472 protected:
4473 enum { kExpectedNumCommits = 10 };
4475 void SetupTree() override {
4476 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings());
4477 root_layer->SetBounds(bounds_);
4478 root_layer->CreateRenderSurface();
4480 child_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
4481 child_layer_->SetBounds(bounds_);
4482 child_layer_->SetIsDrawable(true);
4483 root_layer->AddChild(child_layer_);
4485 layer_tree_host()->SetRootLayer(root_layer);
4486 layer_tree_host()->SetViewportSize(bounds_);
4487 LayerTreeHostTest::SetupTree();
4490 void BeginTest() override {
4491 MainThreadTaskRunner()->PostTask(
4492 FROM_HERE,
4493 base::Bind(
4494 &LayerTreeHostTestContinuousPainting::EnableContinuousPainting,
4495 base::Unretained(this)));
4496 // Wait 50x longer than expected.
4497 double milliseconds_per_frame =
4498 1000.0 / layer_tree_host()->settings().renderer_settings.refresh_rate;
4499 MainThreadTaskRunner()->PostDelayedTask(
4500 FROM_HERE,
4501 base::Bind(
4502 &LayerTreeHostTestContinuousPainting::DisableContinuousPainting,
4503 base::Unretained(this)),
4504 base::TimeDelta::FromMilliseconds(50 * kExpectedNumCommits *
4505 milliseconds_per_frame));
4508 void BeginMainFrame(const BeginFrameArgs& args) override {
4509 child_layer_->SetNeedsDisplay();
4512 void AfterTest() override {
4513 EXPECT_LE(kExpectedNumCommits, num_commits_);
4514 EXPECT_LE(kExpectedNumCommits, num_draws_);
4515 EXPECT_LE(kExpectedNumCommits, child_layer_->update_count());
4518 void DrawLayersOnThread(LayerTreeHostImpl* impl) override {
4519 if (++num_draws_ == kExpectedNumCommits)
4520 EndTest();
4523 void CommitCompleteOnThread(LayerTreeHostImpl* impl) override {
4524 ++num_commits_;
4527 private:
4528 void EnableContinuousPainting() {
4529 LayerTreeDebugState debug_state = layer_tree_host()->debug_state();
4530 debug_state.continuous_painting = true;
4531 layer_tree_host()->SetDebugState(debug_state);
4534 void DisableContinuousPainting() {
4535 LayerTreeDebugState debug_state = layer_tree_host()->debug_state();
4536 debug_state.continuous_painting = false;
4537 layer_tree_host()->SetDebugState(debug_state);
4538 EndTest();
4541 int num_commits_;
4542 int num_draws_;
4543 const gfx::Size bounds_;
4544 FakeContentLayerClient client_;
4545 scoped_refptr<FakePictureLayer> child_layer_;
4548 MULTI_THREAD_TEST_F(LayerTreeHostTestContinuousPainting);
4550 class LayerTreeHostTestWillBeginImplFrameHasDidFinishImplFrame
4551 : public LayerTreeHostTest {
4552 public:
4553 enum { kExpectedNumImplFrames = 10 };
4555 LayerTreeHostTestWillBeginImplFrameHasDidFinishImplFrame()
4556 : will_begin_impl_frame_count_(0), did_finish_impl_frame_count_(0) {}
4558 void BeginTest() override {
4559 // Kick off the test with a commit.
4560 PostSetNeedsCommitToMainThread();
4563 void WillBeginImplFrameOnThread(LayerTreeHostImpl* host_impl,
4564 const BeginFrameArgs& args) override {
4565 EXPECT_EQ(will_begin_impl_frame_count_, did_finish_impl_frame_count_);
4566 EXPECT_FALSE(TestEnded());
4567 will_begin_impl_frame_count_++;
4570 void DidFinishImplFrameOnThread(LayerTreeHostImpl* host_impl) override {
4571 did_finish_impl_frame_count_++;
4572 EXPECT_EQ(will_begin_impl_frame_count_, did_finish_impl_frame_count_);
4574 // Request a number of commits to cause multiple impl frames. We expect to
4575 // get one more impl frames than the number of commits requested because
4576 // after a commit it takes one frame to become idle.
4577 if (did_finish_impl_frame_count_ < kExpectedNumImplFrames - 1)
4578 PostSetNeedsCommitToMainThread();
4581 void SendBeginMainFrameNotExpectedSoon() override { EndTest(); }
4583 void AfterTest() override {
4584 EXPECT_GT(will_begin_impl_frame_count_, 0);
4585 EXPECT_GT(did_finish_impl_frame_count_, 0);
4586 EXPECT_EQ(will_begin_impl_frame_count_, did_finish_impl_frame_count_);
4588 // TODO(mithro): Figure out why the multithread version of this test
4589 // sometimes has one more frame then expected. Possibly related to
4590 // http://crbug.com/443185
4591 if (!HasImplThread()) {
4592 EXPECT_EQ(will_begin_impl_frame_count_, kExpectedNumImplFrames);
4593 EXPECT_EQ(did_finish_impl_frame_count_, kExpectedNumImplFrames);
4597 private:
4598 int will_begin_impl_frame_count_;
4599 int did_finish_impl_frame_count_;
4602 SINGLE_AND_MULTI_THREAD_TEST_F(
4603 LayerTreeHostTestWillBeginImplFrameHasDidFinishImplFrame);
4605 class LayerTreeHostTestSendBeginFramesToChildren : public LayerTreeHostTest {
4606 public:
4607 LayerTreeHostTestSendBeginFramesToChildren()
4608 : begin_frame_sent_to_children_(false) {
4611 void BeginTest() override {
4612 // Kick off the test with a commit.
4613 PostSetNeedsCommitToMainThread();
4616 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
4617 begin_frame_sent_to_children_ = true;
4618 EndTest();
4621 void DidBeginMainFrame() override {
4622 // Children requested BeginFrames.
4623 layer_tree_host()->SetChildrenNeedBeginFrames(true);
4626 void AfterTest() override {
4627 // Ensure that BeginFrame message is sent to children during parent
4628 // scheduler handles its BeginFrame.
4629 EXPECT_TRUE(begin_frame_sent_to_children_);
4632 private:
4633 bool begin_frame_sent_to_children_;
4636 SINGLE_THREAD_TEST_F(LayerTreeHostTestSendBeginFramesToChildren);
4638 class LayerTreeHostTestSendBeginFramesToChildrenWithExternalBFS
4639 : public LayerTreeHostTest {
4640 public:
4641 LayerTreeHostTestSendBeginFramesToChildrenWithExternalBFS()
4642 : begin_frame_sent_to_children_(false) {
4645 void InitializeSettings(LayerTreeSettings* settings) override {
4646 settings->use_external_begin_frame_source = true;
4649 void BeginTest() override {
4650 // Kick off the test with a commit.
4651 PostSetNeedsCommitToMainThread();
4654 void SendBeginFramesToChildren(const BeginFrameArgs& args) override {
4655 begin_frame_sent_to_children_ = true;
4656 EndTest();
4659 void DidBeginMainFrame() override {
4660 // Children requested BeginFrames.
4661 layer_tree_host()->SetChildrenNeedBeginFrames(true);
4664 void AfterTest() override {
4665 // Ensure that BeginFrame message is sent to children during parent
4666 // scheduler handles its BeginFrame.
4667 EXPECT_TRUE(begin_frame_sent_to_children_);
4670 private:
4671 bool begin_frame_sent_to_children_;
4674 SINGLE_THREAD_TEST_F(LayerTreeHostTestSendBeginFramesToChildrenWithExternalBFS);
4676 class LayerTreeHostTestActivateOnInvisible : public LayerTreeHostTest {
4677 public:
4678 LayerTreeHostTestActivateOnInvisible()
4679 : activation_count_(0), visible_(true) {}
4681 void BeginTest() override {
4682 // Kick off the test with a commit.
4683 PostSetNeedsCommitToMainThread();
4686 void BeginCommitOnThread(LayerTreeHostImpl* host_impl) override {
4687 // Make sure we don't activate using the notify signal from tile manager.
4688 host_impl->BlockNotifyReadyToActivateForTesting(true);
4691 void DidCommit() override { layer_tree_host()->SetVisible(false); }
4693 void DidSetVisibleOnImplTree(LayerTreeHostImpl* host_impl,
4694 bool visible) override {
4695 visible_ = visible;
4697 // Once invisible, we can go visible again.
4698 if (!visible) {
4699 PostSetVisibleToMainThread(true);
4700 } else {
4701 EXPECT_TRUE(host_impl->RequiresHighResToDraw());
4702 EndTest();
4706 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override {
4707 ++activation_count_;
4708 EXPECT_FALSE(visible_);
4711 void AfterTest() override {
4712 // Ensure we activated even though the signal was blocked.
4713 EXPECT_EQ(1, activation_count_);
4714 EXPECT_TRUE(visible_);
4717 private:
4718 int activation_count_;
4719 bool visible_;
4721 FakeContentLayerClient client_;
4722 scoped_refptr<FakePictureLayer> picture_layer_;
4725 // TODO(vmpstr): Enable with single thread impl-side painting.
4726 // This test blocks activation which is not supported for single thread mode.
4727 MULTI_THREAD_BLOCKNOTIFY_TEST_F(LayerTreeHostTestActivateOnInvisible);
4729 // Do a synchronous composite and assert that the swap promise succeeds.
4730 class LayerTreeHostTestSynchronousCompositeSwapPromise
4731 : public LayerTreeHostTest {
4732 public:
4733 LayerTreeHostTestSynchronousCompositeSwapPromise() : commit_count_(0) {}
4735 void InitializeSettings(LayerTreeSettings* settings) override {
4736 settings->single_thread_proxy_scheduler = false;
4737 settings->use_zero_copy = true;
4738 settings->use_one_copy = false;
4741 void BeginTest() override {
4742 // Successful composite.
4743 scoped_ptr<SwapPromise> swap_promise0(
4744 new TestSwapPromise(&swap_promise_result_[0]));
4745 layer_tree_host()->QueueSwapPromise(swap_promise0.Pass());
4746 layer_tree_host()->Composite(base::TimeTicks::Now());
4748 // Fail to swap (no damage).
4749 scoped_ptr<SwapPromise> swap_promise1(
4750 new TestSwapPromise(&swap_promise_result_[1]));
4751 layer_tree_host()->QueueSwapPromise(swap_promise1.Pass());
4752 layer_tree_host()->SetNeedsCommit();
4753 layer_tree_host()->Composite(base::TimeTicks::Now());
4755 // Fail to draw (not visible).
4756 scoped_ptr<SwapPromise> swap_promise2(
4757 new TestSwapPromise(&swap_promise_result_[2]));
4758 layer_tree_host()->QueueSwapPromise(swap_promise2.Pass());
4759 layer_tree_host()->SetNeedsDisplayOnAllLayers();
4760 layer_tree_host()->SetVisible(false);
4761 layer_tree_host()->Composite(base::TimeTicks::Now());
4763 EndTest();
4766 void DidCommit() override {
4767 commit_count_++;
4768 ASSERT_LE(commit_count_, 3);
4771 void AfterTest() override {
4772 EXPECT_EQ(3, commit_count_);
4774 // Initial swap promise should have succeded.
4776 base::AutoLock lock(swap_promise_result_[0].lock);
4777 EXPECT_TRUE(swap_promise_result_[0].did_swap_called);
4778 EXPECT_FALSE(swap_promise_result_[0].did_not_swap_called);
4779 EXPECT_TRUE(swap_promise_result_[0].dtor_called);
4782 // Second swap promise fails to swap.
4784 base::AutoLock lock(swap_promise_result_[1].lock);
4785 EXPECT_TRUE(swap_promise_result_[1].did_activate_called);
4786 EXPECT_FALSE(swap_promise_result_[1].did_swap_called);
4787 EXPECT_TRUE(swap_promise_result_[1].did_not_swap_called);
4788 EXPECT_EQ(SwapPromise::SWAP_FAILS, swap_promise_result_[1].reason);
4789 EXPECT_TRUE(swap_promise_result_[1].dtor_called);
4792 // Third swap promises also fails to swap (and draw).
4794 base::AutoLock lock(swap_promise_result_[2].lock);
4795 EXPECT_TRUE(swap_promise_result_[2].did_activate_called);
4796 EXPECT_FALSE(swap_promise_result_[2].did_swap_called);
4797 EXPECT_TRUE(swap_promise_result_[2].did_not_swap_called);
4798 EXPECT_EQ(SwapPromise::SWAP_FAILS, swap_promise_result_[2].reason);
4799 EXPECT_TRUE(swap_promise_result_[2].dtor_called);
4803 int commit_count_;
4804 TestSwapPromiseResult swap_promise_result_[3];
4807 SINGLE_THREAD_TEST_F(LayerTreeHostTestSynchronousCompositeSwapPromise);
4809 // Make sure page scale and top control deltas are applied to the client even
4810 // when the LayerTreeHost doesn't have a root layer.
4811 class LayerTreeHostAcceptsDeltasFromImplWithoutRootLayer
4812 : public LayerTreeHostTest {
4813 public:
4814 LayerTreeHostAcceptsDeltasFromImplWithoutRootLayer()
4815 : deltas_sent_to_client_(false) {}
4817 void BeginTest() override {
4818 layer_tree_host()->SetRootLayer(nullptr);
4819 info_.page_scale_delta = 3.14f;
4820 info_.top_controls_delta = 2.73f;
4822 PostSetNeedsCommitToMainThread();
4825 void BeginMainFrame(const BeginFrameArgs& args) override {
4826 EXPECT_EQ(nullptr, layer_tree_host()->root_layer());
4828 layer_tree_host()->ApplyScrollAndScale(&info_);
4829 EndTest();
4832 void ApplyViewportDeltas(const gfx::Vector2dF& inner,
4833 const gfx::Vector2dF& outer,
4834 const gfx::Vector2dF& elastic_overscroll_delta,
4835 float scale_delta,
4836 float top_controls_delta) override {
4837 EXPECT_EQ(info_.page_scale_delta, scale_delta);
4838 EXPECT_EQ(info_.top_controls_delta, top_controls_delta);
4839 deltas_sent_to_client_ = true;
4842 void AfterTest() override {
4843 EXPECT_TRUE(deltas_sent_to_client_);
4846 ScrollAndScaleSet info_;
4847 bool deltas_sent_to_client_;
4850 MULTI_THREAD_TEST_F(LayerTreeHostAcceptsDeltasFromImplWithoutRootLayer);
4852 class LayerTreeHostTestCrispUpAfterPinchEnds : public LayerTreeHostTest {
4853 protected:
4854 LayerTreeHostTestCrispUpAfterPinchEnds()
4855 : playback_allowed_event_(true, true) {}
4857 void SetupTree() override {
4858 frame_ = 1;
4859 posted_ = false;
4860 client_.set_fill_with_nonsolid_color(true);
4862 scoped_refptr<Layer> root = Layer::Create(layer_settings());
4863 root->SetBounds(gfx::Size(500, 500));
4865 scoped_refptr<Layer> pinch = Layer::Create(layer_settings());
4866 pinch->SetBounds(gfx::Size(500, 500));
4867 pinch->SetScrollClipLayerId(root->id());
4868 pinch->SetIsContainerForFixedPositionLayers(true);
4869 root->AddChild(pinch);
4871 scoped_ptr<FakePicturePile> pile(
4872 new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
4873 LayerTreeSettings().default_tile_grid_size));
4874 pile->SetPlaybackAllowedEvent(&playback_allowed_event_);
4875 scoped_refptr<FakePictureLayer> layer =
4876 FakePictureLayer::CreateWithRecordingSource(layer_settings(), &client_,
4877 pile.Pass());
4878 layer->SetBounds(gfx::Size(500, 500));
4879 layer->SetContentsOpaque(true);
4880 // Avoid LCD text on the layer so we don't cause extra commits when we
4881 // pinch.
4882 layer->disable_lcd_text();
4883 pinch->AddChild(layer);
4885 layer_tree_host()->RegisterViewportLayers(NULL, root, pinch, pinch);
4886 layer_tree_host()->SetPageScaleFactorAndLimits(1.f, 1.f, 4.f);
4887 layer_tree_host()->SetRootLayer(root);
4888 LayerTreeHostTest::SetupTree();
4891 // Returns the delta scale of all quads in the frame's root pass from their
4892 // ideal, or 0 if they are not all the same.
4893 float FrameQuadScaleDeltaFromIdeal(LayerTreeHostImpl::FrameData* frame_data) {
4894 if (frame_data->has_no_damage)
4895 return 0.f;
4896 float frame_scale = 0.f;
4897 RenderPass* root_pass = frame_data->render_passes.back();
4898 for (const auto& draw_quad : root_pass->quad_list) {
4899 // Checkerboards mean an incomplete frame.
4900 if (draw_quad->material != DrawQuad::TILED_CONTENT)
4901 return 0.f;
4902 const TileDrawQuad* quad = TileDrawQuad::MaterialCast(draw_quad);
4903 float quad_scale =
4904 quad->tex_coord_rect.width() / static_cast<float>(quad->rect.width());
4905 float transform_scale = SkMScalarToFloat(
4906 quad->shared_quad_state->quad_to_target_transform.matrix().get(0, 0));
4907 float scale = quad_scale / transform_scale;
4908 if (frame_scale != 0.f && frame_scale != scale)
4909 return 0.f;
4910 frame_scale = scale;
4912 return frame_scale;
4915 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
4917 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
4918 LayerTreeHostImpl::FrameData* frame_data,
4919 DrawResult draw_result) override {
4920 float quad_scale_delta = FrameQuadScaleDeltaFromIdeal(frame_data);
4921 switch (frame_) {
4922 case 1:
4923 // Drew at page scale 1 before any pinching.
4924 EXPECT_EQ(1.f, host_impl->active_tree()->current_page_scale_factor());
4925 EXPECT_EQ(1.f, quad_scale_delta);
4926 PostNextAfterDraw(host_impl);
4927 break;
4928 case 2:
4929 if (quad_scale_delta != 1.f)
4930 break;
4931 // Drew at page scale 1.5 after pinching in.
4932 EXPECT_EQ(1.5f, host_impl->active_tree()->current_page_scale_factor());
4933 EXPECT_EQ(1.f, quad_scale_delta);
4934 PostNextAfterDraw(host_impl);
4935 break;
4936 case 3:
4937 // By pinching out, we will create a new tiling and raster it. This may
4938 // cause some additional draws, though we should still be drawing with
4939 // the old 1.5 tiling.
4940 if (frame_data->has_no_damage)
4941 break;
4942 // Drew at page scale 1 with the 1.5 tiling while pinching out.
4943 EXPECT_EQ(1.f, host_impl->active_tree()->current_page_scale_factor());
4944 EXPECT_EQ(1.5f, quad_scale_delta);
4945 // We don't PostNextAfterDraw here, instead we wait for the new tiling
4946 // to finish rastering so we don't get any noise in further steps.
4947 break;
4948 case 4:
4949 // Drew at page scale 1 with the 1.5 tiling after pinching out completed
4950 // while waiting for texture uploads to complete.
4951 EXPECT_EQ(1.f, host_impl->active_tree()->current_page_scale_factor());
4952 // This frame will not have any damage, since it's actually the same as
4953 // the last frame, and should contain no incomplete tiles. We just want
4954 // to make sure we drew here at least once after the pinch ended to be
4955 // sure that drawing after pinch doesn't leave us at the wrong scale
4956 EXPECT_TRUE(frame_data->has_no_damage);
4957 PostNextAfterDraw(host_impl);
4958 break;
4959 case 5:
4960 if (quad_scale_delta != 1.f)
4961 break;
4962 // Drew at scale 1 after texture uploads are done.
4963 EXPECT_EQ(1.f, host_impl->active_tree()->current_page_scale_factor());
4964 EXPECT_EQ(1.f, quad_scale_delta);
4965 EndTest();
4966 break;
4968 return draw_result;
4971 void PostNextAfterDraw(LayerTreeHostImpl* host_impl) {
4972 if (posted_)
4973 return;
4974 posted_ = true;
4975 ImplThreadTaskRunner()->PostDelayedTask(
4976 FROM_HERE, base::Bind(&LayerTreeHostTestCrispUpAfterPinchEnds::Next,
4977 base::Unretained(this), host_impl),
4978 // Use a delay to allow raster/upload to happen in between frames. This
4979 // should cause flakiness if we fail to block raster/upload when
4980 // desired.
4981 base::TimeDelta::FromMilliseconds(16 * 4));
4984 void Next(LayerTreeHostImpl* host_impl) {
4985 ++frame_;
4986 posted_ = false;
4987 switch (frame_) {
4988 case 2:
4989 // Pinch zoom in.
4990 host_impl->PinchGestureBegin();
4991 host_impl->PinchGestureUpdate(1.5f, gfx::Point(100, 100));
4992 host_impl->PinchGestureEnd();
4993 break;
4994 case 3:
4995 // Pinch zoom back to 1.f but don't end it.
4996 host_impl->PinchGestureBegin();
4997 host_impl->PinchGestureUpdate(1.f / 1.5f, gfx::Point(100, 100));
4998 break;
4999 case 4:
5000 // End the pinch, but delay tile production.
5001 playback_allowed_event_.Reset();
5002 host_impl->PinchGestureEnd();
5003 break;
5004 case 5:
5005 // Let tiles complete.
5006 playback_allowed_event_.Signal();
5007 break;
5011 void NotifyTileStateChangedOnThread(LayerTreeHostImpl* host_impl,
5012 const Tile* tile) override {
5013 if (frame_ == 3) {
5014 // On frame 3, we will have a lower res tile complete for the pinch-out
5015 // gesture even though it's not displayed. We wait for it here to prevent
5016 // flakiness.
5017 EXPECT_EQ(0.75f, tile->contents_scale());
5018 PostNextAfterDraw(host_impl);
5020 // On frame_ == 4, we are preventing texture uploads from completing,
5021 // so this verifies they are not completing before frame_ == 5.
5022 // Flaky failures here indicate we're failing to prevent uploads from
5023 // completing.
5024 EXPECT_NE(4, frame_) << tile->contents_scale();
5027 void AfterTest() override {}
5029 FakeContentLayerClient client_;
5030 int frame_;
5031 bool posted_;
5032 base::WaitableEvent playback_allowed_event_;
5035 // This test does pinching on the impl side which is not supported in single
5036 // thread.
5037 MULTI_THREAD_TEST_F(LayerTreeHostTestCrispUpAfterPinchEnds);
5039 class LayerTreeHostTestCrispUpAfterPinchEndsWithOneCopy
5040 : public LayerTreeHostTestCrispUpAfterPinchEnds {
5041 protected:
5042 void InitializeSettings(LayerTreeSettings* settings) override {
5043 settings->use_one_copy = true;
5046 scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface() override {
5047 scoped_ptr<TestWebGraphicsContext3D> context3d =
5048 TestWebGraphicsContext3D::Create();
5049 context3d->set_support_image(true);
5050 context3d->set_support_sync_query(true);
5051 #if defined(OS_MACOSX)
5052 context3d->set_support_texture_rectangle(true);
5053 #endif
5055 if (delegating_renderer())
5056 return FakeOutputSurface::CreateDelegating3d(context3d.Pass());
5057 else
5058 return FakeOutputSurface::Create3d(context3d.Pass());
5062 // This test does pinching on the impl side which is not supported in single
5063 // thread.
5064 MULTI_THREAD_TEST_F(LayerTreeHostTestCrispUpAfterPinchEndsWithOneCopy);
5066 class RasterizeWithGpuRasterizationCreatesResources : public LayerTreeHostTest {
5067 protected:
5068 RasterizeWithGpuRasterizationCreatesResources() {}
5070 void InitializeSettings(LayerTreeSettings* settings) override {
5071 settings->gpu_rasterization_forced = true;
5074 void SetupTree() override {
5075 client_.set_fill_with_nonsolid_color(true);
5077 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5078 root->SetBounds(gfx::Size(500, 500));
5080 scoped_ptr<FakePicturePile> pile(
5081 new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
5082 LayerTreeSettings().default_tile_grid_size));
5083 scoped_refptr<FakePictureLayer> layer =
5084 FakePictureLayer::CreateWithRecordingSource(layer_settings(), &client_,
5085 pile.Pass());
5086 layer->SetBounds(gfx::Size(500, 500));
5087 layer->SetContentsOpaque(true);
5088 root->AddChild(layer);
5090 layer_tree_host()->SetRootLayer(root);
5091 LayerTreeHostTest::SetupTree();
5094 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5096 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5097 LayerTreeHostImpl::FrameData* frame_data,
5098 DrawResult draw_result) override {
5099 EXPECT_NE(0u, host_impl->resource_provider()->num_resources());
5100 EndTest();
5101 return draw_result;
5103 void AfterTest() override {}
5105 FakeContentLayerClient client_;
5108 SINGLE_AND_MULTI_THREAD_TEST_F(RasterizeWithGpuRasterizationCreatesResources);
5110 class GpuRasterizationRasterizesBorderTiles : public LayerTreeHostTest {
5111 protected:
5112 GpuRasterizationRasterizesBorderTiles() : viewport_size_(1024, 2048) {}
5114 void InitializeSettings(LayerTreeSettings* settings) override {
5115 settings->gpu_rasterization_enabled = true;
5116 settings->gpu_rasterization_forced = true;
5119 void SetupTree() override {
5120 client_.set_fill_with_nonsolid_color(true);
5122 scoped_ptr<FakePicturePile> pile(
5123 new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
5124 LayerTreeSettings().default_tile_grid_size));
5125 scoped_refptr<FakePictureLayer> root =
5126 FakePictureLayer::CreateWithRecordingSource(layer_settings(), &client_,
5127 pile.Pass());
5128 root->SetBounds(gfx::Size(10000, 10000));
5129 root->SetContentsOpaque(true);
5131 layer_tree_host()->SetRootLayer(root);
5132 LayerTreeHostTest::SetupTree();
5133 layer_tree_host()->SetViewportSize(viewport_size_);
5136 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5138 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5139 LayerTreeHostImpl::FrameData* frame_data,
5140 DrawResult draw_result) override {
5141 EXPECT_EQ(10u, host_impl->resource_provider()->num_resources());
5142 EndTest();
5143 return draw_result;
5146 void AfterTest() override {}
5148 private:
5149 FakeContentLayerClient client_;
5150 gfx::Size viewport_size_;
5153 SINGLE_AND_MULTI_THREAD_TEST_F(GpuRasterizationRasterizesBorderTiles);
5155 class LayerTreeHostTestContinuousDrawWhenCreatingVisibleTiles
5156 : public LayerTreeHostTest {
5157 protected:
5158 LayerTreeHostTestContinuousDrawWhenCreatingVisibleTiles()
5159 : playback_allowed_event_(true, true) {}
5161 void SetupTree() override {
5162 step_ = 1;
5163 continuous_draws_ = 0;
5164 client_.set_fill_with_nonsolid_color(true);
5166 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5167 root->SetBounds(gfx::Size(500, 500));
5169 scoped_refptr<Layer> pinch = Layer::Create(layer_settings());
5170 pinch->SetBounds(gfx::Size(500, 500));
5171 pinch->SetScrollClipLayerId(root->id());
5172 pinch->SetIsContainerForFixedPositionLayers(true);
5173 root->AddChild(pinch);
5175 scoped_ptr<FakePicturePile> pile(
5176 new FakePicturePile(LayerTreeSettings().minimum_contents_scale,
5177 LayerTreeSettings().default_tile_grid_size));
5178 pile->SetPlaybackAllowedEvent(&playback_allowed_event_);
5179 scoped_refptr<FakePictureLayer> layer =
5180 FakePictureLayer::CreateWithRecordingSource(layer_settings(), &client_,
5181 pile.Pass());
5182 layer->SetBounds(gfx::Size(500, 500));
5183 layer->SetContentsOpaque(true);
5184 // Avoid LCD text on the layer so we don't cause extra commits when we
5185 // pinch.
5186 layer->disable_lcd_text();
5187 pinch->AddChild(layer);
5189 layer_tree_host()->RegisterViewportLayers(NULL, root, pinch, pinch);
5190 layer_tree_host()->SetPageScaleFactorAndLimits(1.f, 1.f, 4.f);
5191 layer_tree_host()->SetRootLayer(root);
5192 LayerTreeHostTest::SetupTree();
5195 // Returns the delta scale of all quads in the frame's root pass from their
5196 // ideal, or 0 if they are not all the same.
5197 float FrameQuadScaleDeltaFromIdeal(LayerTreeHostImpl::FrameData* frame_data) {
5198 if (frame_data->has_no_damage)
5199 return 0.f;
5200 float frame_scale = 0.f;
5201 RenderPass* root_pass = frame_data->render_passes.back();
5202 for (const auto& draw_quad : root_pass->quad_list) {
5203 const TileDrawQuad* quad = TileDrawQuad::MaterialCast(draw_quad);
5204 float quad_scale =
5205 quad->tex_coord_rect.width() / static_cast<float>(quad->rect.width());
5206 float transform_scale = SkMScalarToFloat(
5207 quad->shared_quad_state->quad_to_target_transform.matrix().get(0, 0));
5208 float scale = quad_scale / transform_scale;
5209 if (frame_scale != 0.f && frame_scale != scale)
5210 return 0.f;
5211 frame_scale = scale;
5213 return frame_scale;
5216 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5218 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5219 LayerTreeHostImpl::FrameData* frame_data,
5220 DrawResult draw_result) override {
5221 float quad_scale_delta = FrameQuadScaleDeltaFromIdeal(frame_data);
5222 switch (step_) {
5223 case 1:
5224 // Drew at scale 1 before any pinching.
5225 EXPECT_EQ(1.f, host_impl->active_tree()->current_page_scale_factor());
5226 EXPECT_EQ(1.f, quad_scale_delta);
5227 break;
5228 case 2:
5229 if (quad_scale_delta != 1.f / 1.5f)
5230 break;
5231 // Drew at scale 1 still though the ideal is 1.5.
5232 EXPECT_EQ(1.5f, host_impl->active_tree()->current_page_scale_factor());
5233 EXPECT_EQ(1.f / 1.5f, quad_scale_delta);
5234 break;
5235 case 3:
5236 // Continuous draws are attempted.
5237 EXPECT_EQ(1.5f, host_impl->active_tree()->current_page_scale_factor());
5238 if (!frame_data->has_no_damage)
5239 EXPECT_EQ(1.f / 1.5f, quad_scale_delta);
5240 break;
5241 case 4:
5242 if (quad_scale_delta != 1.f)
5243 break;
5244 // Drew at scale 1.5 when all the tiles completed.
5245 EXPECT_EQ(1.5f, host_impl->active_tree()->current_page_scale_factor());
5246 EXPECT_EQ(1.f, quad_scale_delta);
5247 break;
5248 case 5:
5249 // TODO(danakj): We get more draws before the NotifyReadyToDraw
5250 // because it is asynchronous from the previous draw and happens late.
5251 break;
5252 case 6:
5253 // NotifyReadyToDraw happened. If we were already inside a frame, we may
5254 // try to draw once more.
5255 break;
5256 case 7:
5257 NOTREACHED() << "No draws should happen once we have a complete frame.";
5258 break;
5260 return draw_result;
5263 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
5264 switch (step_) {
5265 case 1:
5266 // Delay tile production.
5267 playback_allowed_event_.Reset();
5268 // Pinch zoom in to cause new tiles to be required.
5269 host_impl->PinchGestureBegin();
5270 host_impl->PinchGestureUpdate(1.5f, gfx::Point(100, 100));
5271 host_impl->PinchGestureEnd();
5272 ++step_;
5273 break;
5274 case 2:
5275 ++step_;
5276 break;
5277 case 3:
5278 // We should continue to try draw while there are incomplete visible
5279 // tiles.
5280 if (++continuous_draws_ > 5) {
5281 // Allow the tiles to complete.
5282 playback_allowed_event_.Signal();
5283 ++step_;
5285 break;
5286 case 4:
5287 ++step_;
5288 break;
5289 case 5:
5290 // Waiting for NotifyReadyToDraw.
5291 break;
5292 case 6:
5293 // NotifyReadyToDraw happened.
5294 ++step_;
5295 break;
5299 void NotifyReadyToDrawOnThread(LayerTreeHostImpl* host_impl) override {
5300 if (step_ == 5) {
5301 ++step_;
5302 // NotifyReadyToDraw has happened, we may draw once more, but should not
5303 // get any more draws after that. End the test after a timeout to watch
5304 // for any extraneous draws.
5305 // TODO(brianderson): We could remove this delay and instead wait until
5306 // the BeginFrameSource decides it doesn't need to send frames anymore,
5307 // or test that it already doesn't here.
5308 EndTestAfterDelayMs(16 * 4);
5312 void NotifyTileStateChangedOnThread(LayerTreeHostImpl* host_impl,
5313 const Tile* tile) override {
5314 // On step_ == 2, we are preventing texture uploads from completing,
5315 // so this verifies they are not completing before step_ == 3.
5316 // Flaky failures here indicate we're failing to prevent uploads from
5317 // completing.
5318 EXPECT_NE(2, step_);
5321 void AfterTest() override { EXPECT_GT(continuous_draws_, 5); }
5323 FakeContentLayerClient client_;
5324 int step_;
5325 int continuous_draws_;
5326 base::WaitableEvent playback_allowed_event_;
5329 MULTI_THREAD_TEST_F(LayerTreeHostTestContinuousDrawWhenCreatingVisibleTiles);
5331 class LayerTreeHostTestOneActivatePerPrepareTiles : public LayerTreeHostTest {
5332 public:
5333 LayerTreeHostTestOneActivatePerPrepareTiles()
5334 : notify_ready_to_activate_count_(0u),
5335 scheduled_prepare_tiles_count_(0) {}
5337 void SetupTree() override {
5338 client_.set_fill_with_nonsolid_color(true);
5339 scoped_refptr<FakePictureLayer> root_layer =
5340 FakePictureLayer::Create(layer_settings(), &client_);
5341 root_layer->SetBounds(gfx::Size(1500, 1500));
5342 root_layer->SetIsDrawable(true);
5344 layer_tree_host()->SetRootLayer(root_layer);
5345 LayerTreeHostTest::SetupTree();
5348 void BeginTest() override {
5349 layer_tree_host()->SetViewportSize(gfx::Size(16, 16));
5350 PostSetNeedsCommitToMainThread();
5353 void InitializedRendererOnThread(LayerTreeHostImpl* host_impl,
5354 bool success) override {
5355 ASSERT_TRUE(success);
5356 host_impl->tile_manager()->SetScheduledRasterTaskLimitForTesting(1);
5359 void NotifyReadyToActivateOnThread(LayerTreeHostImpl* impl) override {
5360 ++notify_ready_to_activate_count_;
5361 EndTestAfterDelayMs(100);
5364 void ScheduledActionPrepareTiles() override {
5365 ++scheduled_prepare_tiles_count_;
5368 void AfterTest() override {
5369 // Expect at most a notification for each scheduled prepare tiles, plus one
5370 // for the initial commit (which doesn't go through scheduled actions).
5371 // The reason this is not an equality is because depending on timing, we
5372 // might get a prepare tiles but not yet get a notification that we're
5373 // ready to activate. The intent of a test is to ensure that we don't
5374 // get more than one notification per prepare tiles, so this is OK.
5375 EXPECT_LE(notify_ready_to_activate_count_,
5376 1u + scheduled_prepare_tiles_count_);
5379 protected:
5380 FakeContentLayerClient client_;
5381 size_t notify_ready_to_activate_count_;
5382 size_t scheduled_prepare_tiles_count_;
5385 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestOneActivatePerPrepareTiles);
5387 class LayerTreeHostTestFrameTimingRequestsSaveTimestamps
5388 : public LayerTreeHostTest {
5389 public:
5390 LayerTreeHostTestFrameTimingRequestsSaveTimestamps()
5391 : check_results_on_commit_(false) {}
5393 void SetupTree() override {
5394 scoped_refptr<FakePictureLayer> root_layer =
5395 FakePictureLayer::Create(layer_settings(), &client_);
5396 root_layer->SetBounds(gfx::Size(200, 200));
5397 root_layer->SetIsDrawable(true);
5399 scoped_refptr<FakePictureLayer> child_layer =
5400 FakePictureLayer::Create(layer_settings(), &client_);
5401 child_layer->SetBounds(gfx::Size(1500, 1500));
5402 child_layer->SetIsDrawable(true);
5404 std::vector<FrameTimingRequest> requests;
5405 requests.push_back(FrameTimingRequest(1, gfx::Rect(0, 0, 100, 100)));
5406 requests.push_back(FrameTimingRequest(2, gfx::Rect(300, 0, 100, 100)));
5407 child_layer->SetFrameTimingRequests(requests);
5409 root_layer->AddChild(child_layer);
5410 layer_tree_host()->SetRootLayer(root_layer);
5411 LayerTreeHostTest::SetupTree();
5414 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5416 void BeginCommitOnThread(LayerTreeHostImpl* host_impl) override {
5417 if (!check_results_on_commit_)
5418 return;
5420 // Since in reality, the events will be read by LayerTreeHost during commit,
5421 // we check the requests here to ensure that they are correct at the next
5422 // commit time (as opposed to checking in DrawLayers for instance).
5423 // TODO(vmpstr): Change this to read things from the main thread when this
5424 // information is propagated to the main thread (not yet implemented).
5425 FrameTimingTracker* tracker = host_impl->frame_timing_tracker();
5427 // Check composite events.
5429 scoped_ptr<FrameTimingTracker::CompositeTimingSet> timing_set =
5430 tracker->GroupCompositeCountsByRectId();
5431 EXPECT_EQ(1u, timing_set->size());
5432 auto rect_1_it = timing_set->find(1);
5433 EXPECT_TRUE(rect_1_it != timing_set->end());
5434 const auto& timing_events = rect_1_it->second;
5435 EXPECT_EQ(1u, timing_events.size());
5436 EXPECT_EQ(host_impl->active_tree()->source_frame_number(),
5437 timing_events[0].frame_id);
5438 EXPECT_GT(timing_events[0].timestamp, base::TimeTicks());
5441 // Check main frame events.
5443 scoped_ptr<FrameTimingTracker::MainFrameTimingSet> timing_set =
5444 tracker->GroupMainFrameCountsByRectId();
5445 EXPECT_EQ(2u, timing_set->size());
5446 auto rect_1_it = timing_set->find(1);
5447 EXPECT_TRUE(rect_1_it != timing_set->end());
5448 const auto& timing_events = rect_1_it->second;
5449 EXPECT_EQ(1u, timing_events.size());
5450 EXPECT_EQ(host_impl->active_tree()->source_frame_number(),
5451 timing_events[0].frame_id);
5452 EXPECT_GT(timing_events[0].timestamp, base::TimeTicks());
5453 EXPECT_GT(timing_events[0].end_time, timing_events[0].timestamp);
5456 EndTest();
5459 void DrawLayersOnThread(LayerTreeHostImpl* host_impl) override {
5460 check_results_on_commit_ = true;
5461 PostSetNeedsCommitToMainThread();
5464 void AfterTest() override {}
5466 private:
5467 FakeContentLayerClient client_;
5468 bool check_results_on_commit_;
5471 // Frame timing is not implemented in single thread proxy.
5472 MULTI_THREAD_TEST_F(LayerTreeHostTestFrameTimingRequestsSaveTimestamps);
5474 class LayerTreeHostTestActivationCausesPrepareTiles : public LayerTreeHostTest {
5475 public:
5476 LayerTreeHostTestActivationCausesPrepareTiles()
5477 : scheduled_prepare_tiles_count_(0) {}
5479 void SetupTree() override {
5480 client_.set_fill_with_nonsolid_color(true);
5481 scoped_refptr<FakePictureLayer> root_layer =
5482 FakePictureLayer::Create(layer_settings(), &client_);
5483 root_layer->SetBounds(gfx::Size(150, 150));
5484 root_layer->SetIsDrawable(true);
5486 layer_tree_host()->SetRootLayer(root_layer);
5487 LayerTreeHostTest::SetupTree();
5490 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5492 void NotifyReadyToActivateOnThread(LayerTreeHostImpl* impl) override {
5493 // Ensure we've already activated.
5494 EXPECT_FALSE(impl->pending_tree());
5496 // After activating, we either need to prepare tiles, or we've already
5497 // called a scheduled prepare tiles. This is done because activation might
5498 // cause us to have to memory available (old active tree is gone), so we
5499 // need to ensure we will get a PrepareTiles call.
5500 if (!impl->prepare_tiles_needed())
5501 EXPECT_GE(scheduled_prepare_tiles_count_, 1);
5502 EndTest();
5505 void ScheduledActionPrepareTiles() override {
5506 ++scheduled_prepare_tiles_count_;
5509 void AfterTest() override {}
5511 protected:
5512 FakeContentLayerClient client_;
5513 int scheduled_prepare_tiles_count_;
5516 // This test is testing activation from a pending tree and doesn't make sense
5517 // with single thread commit-to-active.
5518 MULTI_THREAD_TEST_F(LayerTreeHostTestActivationCausesPrepareTiles);
5520 // This tests an assertion that DidCommit and WillCommit happen in the same
5521 // stack frame with no tasks that run between them. Various embedders of
5522 // cc depend on this logic. ui::Compositor holds a compositor lock between
5523 // these events and the inspector timeline wants begin/end CompositeLayers
5524 // to be properly nested with other begin/end events.
5525 class LayerTreeHostTestNoTasksBetweenWillAndDidCommit
5526 : public LayerTreeHostTest {
5527 public:
5528 LayerTreeHostTestNoTasksBetweenWillAndDidCommit() : did_commit_(false) {}
5530 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5532 void WillCommit() override {
5533 MainThreadTaskRunner()->PostTask(
5534 FROM_HERE, base::Bind(&LayerTreeHostTestNoTasksBetweenWillAndDidCommit::
5535 EndTestShouldRunAfterDidCommit,
5536 base::Unretained(this)));
5539 void EndTestShouldRunAfterDidCommit() {
5540 EXPECT_TRUE(did_commit_);
5541 EndTest();
5544 void DidCommit() override {
5545 EXPECT_FALSE(did_commit_);
5546 did_commit_ = true;
5549 void AfterTest() override { EXPECT_TRUE(did_commit_); }
5551 private:
5552 bool did_commit_;
5555 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestNoTasksBetweenWillAndDidCommit);
5557 class LayerTreeHostTestUpdateCopyRequests : public LayerTreeHostTest {
5558 protected:
5559 void SetupTree() override {
5560 root = Layer::Create(layer_settings());
5561 child = Layer::Create(layer_settings());
5562 root->AddChild(child);
5563 layer_tree_host()->SetRootLayer(root);
5564 LayerTreeHostTest::SetupTree();
5567 static void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {}
5569 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5571 void DidCommit() override {
5572 switch (layer_tree_host()->source_frame_number()) {
5573 case 1:
5574 child->RequestCopyOfOutput(CopyOutputRequest::CreateBitmapRequest(
5575 base::Bind(CopyOutputCallback)));
5576 EXPECT_TRUE(
5577 root->draw_properties().layer_or_descendant_has_copy_request);
5578 break;
5579 case 2:
5580 EXPECT_FALSE(
5581 root->draw_properties().layer_or_descendant_has_copy_request);
5582 EndTest();
5583 break;
5587 void AfterTest() override {}
5589 private:
5590 scoped_refptr<Layer> root;
5591 scoped_refptr<Layer> child;
5594 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestUpdateCopyRequests);
5596 class LayerTreeTestMaskLayerForSurfaceWithClippedLayer : public LayerTreeTest {
5597 protected:
5598 void SetupTree() override {
5599 // The masked layer has bounds 50x50, but it has a child that causes
5600 // the surface bounds to be larger. It also has a parent that clips the
5601 // masked layer and its surface.
5603 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5605 scoped_refptr<Layer> clipping_layer = Layer::Create(layer_settings());
5606 root->AddChild(clipping_layer);
5608 scoped_refptr<FakePictureLayer> content_layer =
5609 FakePictureLayer::Create(layer_settings(), &client_);
5610 clipping_layer->AddChild(content_layer);
5612 scoped_refptr<FakePictureLayer> content_child_layer =
5613 FakePictureLayer::Create(layer_settings(), &client_);
5614 content_layer->AddChild(content_child_layer);
5616 scoped_refptr<FakePictureLayer> mask_layer =
5617 FakePictureLayer::Create(layer_settings(), &client_);
5618 content_layer->SetMaskLayer(mask_layer.get());
5620 gfx::Size root_size(100, 100);
5621 root->SetBounds(root_size);
5623 gfx::Rect clipping_rect(20, 10, 10, 20);
5624 clipping_layer->SetBounds(clipping_rect.size());
5625 clipping_layer->SetPosition(clipping_rect.origin());
5626 clipping_layer->SetMasksToBounds(true);
5628 gfx::Size layer_size(50, 50);
5629 content_layer->SetBounds(layer_size);
5630 content_layer->SetPosition(gfx::Point() - clipping_rect.OffsetFromOrigin());
5632 gfx::Size child_size(50, 50);
5633 content_child_layer->SetBounds(child_size);
5634 content_child_layer->SetPosition(gfx::Point(20, 0));
5636 gfx::Size mask_size(100, 100);
5637 mask_layer->SetBounds(mask_size);
5638 mask_layer->SetIsMask(true);
5640 layer_tree_host()->SetRootLayer(root);
5641 LayerTreeTest::SetupTree();
5644 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5646 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5647 LayerTreeHostImpl::FrameData* frame_data,
5648 DrawResult draw_result) override {
5649 EXPECT_EQ(2u, frame_data->render_passes.size());
5650 RenderPass* root_pass = frame_data->render_passes.back();
5651 EXPECT_EQ(2u, root_pass->quad_list.size());
5653 // There's a solid color quad under everything.
5654 EXPECT_EQ(DrawQuad::SOLID_COLOR, root_pass->quad_list.back()->material);
5656 // The surface is clipped to 10x20.
5657 EXPECT_EQ(DrawQuad::RENDER_PASS, root_pass->quad_list.front()->material);
5658 const RenderPassDrawQuad* render_pass_quad =
5659 RenderPassDrawQuad::MaterialCast(root_pass->quad_list.front());
5660 EXPECT_EQ(gfx::Rect(20, 10, 10, 20).ToString(),
5661 render_pass_quad->rect.ToString());
5662 // The masked layer is 50x50, but the surface size is 10x20. So the texture
5663 // coords in the mask are scaled by 10/50 and 20/50.
5664 // The surface is clipped to (20,10) so the mask texture coords are offset
5665 // by 20/50 and 10/50
5666 EXPECT_EQ(gfx::ScaleRect(gfx::RectF(20.f, 10.f, 10.f, 20.f), 1.f / 50.f)
5667 .ToString(),
5668 render_pass_quad->MaskUVRect().ToString());
5669 EXPECT_EQ(gfx::Vector2dF(10.f / 50.f, 20.f / 50.f).ToString(),
5670 render_pass_quad->mask_uv_scale.ToString());
5671 EndTest();
5672 return draw_result;
5675 void AfterTest() override {}
5677 FakeContentLayerClient client_;
5680 SINGLE_AND_MULTI_THREAD_TEST_F(
5681 LayerTreeTestMaskLayerForSurfaceWithClippedLayer);
5683 class LayerTreeTestMaskLayerWithScaling : public LayerTreeTest {
5684 protected:
5685 void InitializeSettings(LayerTreeSettings* settings) override {
5686 settings->layer_transforms_should_scale_layer_contents = true;
5689 void SetupTree() override {
5690 // Root
5691 // |
5692 // +-- Scaling Layer (adds a 2x scale)
5693 // |
5694 // +-- Content Layer
5695 // +--Mask
5697 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5699 scoped_refptr<Layer> scaling_layer = Layer::Create(layer_settings());
5700 root->AddChild(scaling_layer);
5702 scoped_refptr<FakePictureLayer> content_layer =
5703 FakePictureLayer::Create(layer_settings(), &client_);
5704 scaling_layer->AddChild(content_layer);
5706 scoped_refptr<FakePictureLayer> mask_layer =
5707 FakePictureLayer::Create(layer_settings(), &client_);
5708 content_layer->SetMaskLayer(mask_layer.get());
5710 gfx::Size root_size(100, 100);
5711 root->SetBounds(root_size);
5713 gfx::Size scaling_layer_size(50, 50);
5714 scaling_layer->SetBounds(scaling_layer_size);
5715 gfx::Transform scale;
5716 scale.Scale(2.f, 2.f);
5717 scaling_layer->SetTransform(scale);
5719 content_layer->SetBounds(scaling_layer_size);
5721 mask_layer->SetBounds(scaling_layer_size);
5722 mask_layer->SetIsMask(true);
5724 layer_tree_host()->SetRootLayer(root);
5725 LayerTreeTest::SetupTree();
5728 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5730 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5731 LayerTreeHostImpl::FrameData* frame_data,
5732 DrawResult draw_result) override {
5733 EXPECT_EQ(2u, frame_data->render_passes.size());
5734 RenderPass* root_pass = frame_data->render_passes.back();
5735 EXPECT_EQ(2u, root_pass->quad_list.size());
5737 // There's a solid color quad under everything.
5738 EXPECT_EQ(DrawQuad::SOLID_COLOR, root_pass->quad_list.back()->material);
5740 EXPECT_EQ(DrawQuad::RENDER_PASS, root_pass->quad_list.front()->material);
5741 const RenderPassDrawQuad* render_pass_quad =
5742 RenderPassDrawQuad::MaterialCast(root_pass->quad_list.front());
5743 switch (host_impl->active_tree()->source_frame_number()) {
5744 case 0:
5745 // Check that the tree scaling is correctly taken into account for the
5746 // mask, that should fully map onto the quad.
5747 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(),
5748 render_pass_quad->rect.ToString());
5749 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5750 render_pass_quad->MaskUVRect().ToString());
5751 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5752 render_pass_quad->mask_uv_scale.ToString());
5753 break;
5754 case 1:
5755 // Applying a DSF should change the render surface size, but won't
5756 // affect which part of the mask is used.
5757 EXPECT_EQ(gfx::Rect(0, 0, 200, 200).ToString(),
5758 render_pass_quad->rect.ToString());
5759 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5760 render_pass_quad->MaskUVRect().ToString());
5761 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5762 render_pass_quad->mask_uv_scale.ToString());
5763 EndTest();
5764 break;
5766 return draw_result;
5769 void DidCommit() override {
5770 switch (layer_tree_host()->source_frame_number()) {
5771 case 1:
5772 gfx::Size double_root_size(200, 200);
5773 layer_tree_host()->SetViewportSize(double_root_size);
5774 layer_tree_host()->SetDeviceScaleFactor(2.f);
5775 break;
5779 void AfterTest() override {}
5781 FakeContentLayerClient client_;
5784 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeTestMaskLayerWithScaling);
5786 class LayerTreeTestMaskLayerWithDifferentBounds : public LayerTreeTest {
5787 protected:
5788 void SetupTree() override {
5789 // The mask layer has bounds 100x100 but is attached to a layer with bounds
5790 // 50x50.
5792 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5794 scoped_refptr<FakePictureLayer> content_layer =
5795 FakePictureLayer::Create(layer_settings(), &client_);
5796 root->AddChild(content_layer);
5798 scoped_refptr<FakePictureLayer> mask_layer =
5799 FakePictureLayer::Create(layer_settings(), &client_);
5800 content_layer->SetMaskLayer(mask_layer.get());
5802 gfx::Size root_size(100, 100);
5803 root->SetBounds(root_size);
5805 gfx::Size layer_size(50, 50);
5806 content_layer->SetBounds(layer_size);
5808 gfx::Size mask_size(100, 100);
5809 mask_layer->SetBounds(mask_size);
5810 mask_layer->SetIsMask(true);
5812 layer_tree_host()->SetRootLayer(root);
5813 LayerTreeTest::SetupTree();
5816 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5818 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5819 LayerTreeHostImpl::FrameData* frame_data,
5820 DrawResult draw_result) override {
5821 EXPECT_EQ(2u, frame_data->render_passes.size());
5822 RenderPass* root_pass = frame_data->render_passes.back();
5823 EXPECT_EQ(2u, root_pass->quad_list.size());
5825 // There's a solid color quad under everything.
5826 EXPECT_EQ(DrawQuad::SOLID_COLOR, root_pass->quad_list.back()->material);
5828 EXPECT_EQ(DrawQuad::RENDER_PASS, root_pass->quad_list.front()->material);
5829 const RenderPassDrawQuad* render_pass_quad =
5830 RenderPassDrawQuad::MaterialCast(root_pass->quad_list.front());
5831 switch (host_impl->active_tree()->source_frame_number()) {
5832 case 0:
5833 // Check that the mask fills the surface.
5834 EXPECT_EQ(gfx::Rect(0, 0, 50, 50).ToString(),
5835 render_pass_quad->rect.ToString());
5836 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5837 render_pass_quad->MaskUVRect().ToString());
5838 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5839 render_pass_quad->mask_uv_scale.ToString());
5840 break;
5841 case 1:
5842 // Applying a DSF should change the render surface size, but won't
5843 // affect which part of the mask is used.
5844 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(),
5845 render_pass_quad->rect.ToString());
5846 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5847 render_pass_quad->MaskUVRect().ToString());
5848 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5849 render_pass_quad->mask_uv_scale.ToString());
5850 EndTest();
5851 break;
5853 return draw_result;
5856 void DidCommit() override {
5857 switch (layer_tree_host()->source_frame_number()) {
5858 case 1:
5859 gfx::Size double_root_size(200, 200);
5860 layer_tree_host()->SetViewportSize(double_root_size);
5861 layer_tree_host()->SetDeviceScaleFactor(2.f);
5862 break;
5866 void AfterTest() override {}
5868 FakeContentLayerClient client_;
5871 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeTestMaskLayerWithDifferentBounds);
5873 class LayerTreeTestReflectionMaskLayerWithDifferentBounds
5874 : public LayerTreeTest {
5875 protected:
5876 void SetupTree() override {
5877 // The replica's mask layer has bounds 100x100 but the replica is of a
5878 // layer with bounds 50x50.
5880 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5882 scoped_refptr<FakePictureLayer> content_layer =
5883 FakePictureLayer::Create(layer_settings(), &client_);
5884 root->AddChild(content_layer);
5886 scoped_refptr<Layer> replica_layer = Layer::Create(layer_settings());
5887 content_layer->SetReplicaLayer(replica_layer.get());
5889 scoped_refptr<FakePictureLayer> mask_layer =
5890 FakePictureLayer::Create(layer_settings(), &client_);
5891 replica_layer->SetMaskLayer(mask_layer.get());
5893 gfx::Size root_size(100, 100);
5894 root->SetBounds(root_size);
5896 gfx::Size layer_size(50, 50);
5897 content_layer->SetBounds(layer_size);
5899 gfx::Size mask_size(100, 100);
5900 mask_layer->SetBounds(mask_size);
5901 mask_layer->SetIsMask(true);
5903 layer_tree_host()->SetRootLayer(root);
5904 LayerTreeTest::SetupTree();
5907 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
5909 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
5910 LayerTreeHostImpl::FrameData* frame_data,
5911 DrawResult draw_result) override {
5912 EXPECT_EQ(2u, frame_data->render_passes.size());
5913 RenderPass* root_pass = frame_data->render_passes.back();
5914 EXPECT_EQ(3u, root_pass->quad_list.size());
5916 // There's a solid color quad under everything.
5917 EXPECT_EQ(DrawQuad::SOLID_COLOR, root_pass->quad_list.back()->material);
5919 EXPECT_EQ(DrawQuad::RENDER_PASS,
5920 root_pass->quad_list.ElementAt(1)->material);
5921 const RenderPassDrawQuad* render_pass_quad =
5922 RenderPassDrawQuad::MaterialCast(root_pass->quad_list.ElementAt(1));
5923 switch (host_impl->active_tree()->source_frame_number()) {
5924 case 0:
5925 // Check that the mask fills the surface.
5926 EXPECT_EQ(gfx::Rect(0, 0, 50, 50).ToString(),
5927 render_pass_quad->rect.ToString());
5928 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5929 render_pass_quad->MaskUVRect().ToString());
5930 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5931 render_pass_quad->mask_uv_scale.ToString());
5932 break;
5933 case 1:
5934 // Applying a DSF should change the render surface size, but won't
5935 // affect which part of the mask is used.
5936 EXPECT_EQ(gfx::Rect(0, 0, 100, 100).ToString(),
5937 render_pass_quad->rect.ToString());
5938 EXPECT_EQ(gfx::RectF(0.f, 0.f, 1.f, 1.f).ToString(),
5939 render_pass_quad->MaskUVRect().ToString());
5940 EXPECT_EQ(gfx::Vector2dF(1.f, 1.f).ToString(),
5941 render_pass_quad->mask_uv_scale.ToString());
5942 EndTest();
5943 break;
5945 return draw_result;
5948 void DidCommit() override {
5949 switch (layer_tree_host()->source_frame_number()) {
5950 case 1:
5951 gfx::Size double_root_size(200, 200);
5952 layer_tree_host()->SetViewportSize(double_root_size);
5953 layer_tree_host()->SetDeviceScaleFactor(2.f);
5954 break;
5958 void AfterTest() override {}
5960 FakeContentLayerClient client_;
5963 SINGLE_AND_MULTI_THREAD_TEST_F(
5964 LayerTreeTestReflectionMaskLayerWithDifferentBounds);
5966 class LayerTreeTestReflectionMaskLayerForSurfaceWithUnclippedChild
5967 : public LayerTreeTest {
5968 protected:
5969 void SetupTree() override {
5970 // The replica is of a layer with bounds 50x50, but it has a child that
5971 // causes the surface bounds to be larger.
5973 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5975 scoped_refptr<FakePictureLayer> content_layer =
5976 FakePictureLayer::Create(layer_settings(), &client_);
5977 root->AddChild(content_layer);
5979 content_child_layer_ = FakePictureLayer::Create(layer_settings(), &client_);
5980 content_layer->AddChild(content_child_layer_);
5982 scoped_refptr<Layer> replica_layer = Layer::Create(layer_settings());
5983 content_layer->SetReplicaLayer(replica_layer.get());
5985 scoped_refptr<FakePictureLayer> mask_layer =
5986 FakePictureLayer::Create(layer_settings(), &client_);
5987 replica_layer->SetMaskLayer(mask_layer.get());
5989 gfx::Size root_size(100, 100);
5990 root->SetBounds(root_size);
5992 gfx::Size layer_size(50, 50);
5993 content_layer->SetBounds(layer_size);
5994 content_child_layer_->SetBounds(layer_size);
5995 content_child_layer_->SetPosition(gfx::PointF(50.f, 0.f));
5997 gfx::Size mask_size(100, 100);
5998 mask_layer->SetBounds(mask_size);
5999 mask_layer->SetIsMask(true);
6001 layer_tree_host()->SetRootLayer(root);
6002 LayerTreeTest::SetupTree();
6005 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
6007 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
6008 LayerTreeHostImpl::FrameData* frame_data,
6009 DrawResult draw_result) override {
6010 EXPECT_EQ(2u, frame_data->render_passes.size());
6011 RenderPass* root_pass = frame_data->render_passes.back();
6012 EXPECT_EQ(3u, root_pass->quad_list.size());
6014 // There's a solid color quad under everything.
6015 EXPECT_EQ(DrawQuad::SOLID_COLOR, root_pass->quad_list.back()->material);
6017 EXPECT_EQ(DrawQuad::RENDER_PASS,
6018 root_pass->quad_list.ElementAt(1)->material);
6019 const RenderPassDrawQuad* replica_quad =
6020 RenderPassDrawQuad::MaterialCast(root_pass->quad_list.ElementAt(1));
6021 switch (host_impl->active_tree()->source_frame_number()) {
6022 case 0:
6023 // The surface is 100x50.
6024 // The mask covers the owning layer only.
6025 EXPECT_EQ(gfx::Rect(0, 0, 100, 50).ToString(),
6026 replica_quad->rect.ToString());
6027 EXPECT_EQ(gfx::RectF(0.f, 0.f, 2.f, 1.f).ToString(),
6028 replica_quad->MaskUVRect().ToString());
6029 EXPECT_EQ(gfx::Vector2dF(2.f, 1.f).ToString(),
6030 replica_quad->mask_uv_scale.ToString());
6031 break;
6032 case 1:
6033 // The surface is 100x50 with its origin at (-50, 0).
6034 // The mask covers the owning layer only.
6035 EXPECT_EQ(gfx::Rect(-50, 0, 100, 50).ToString(),
6036 replica_quad->rect.ToString());
6037 EXPECT_EQ(gfx::RectF(-1.f, 0.f, 2.f, 1.f).ToString(),
6038 replica_quad->MaskUVRect().ToString());
6039 EXPECT_EQ(gfx::Vector2dF(2.f, 1.f).ToString(),
6040 replica_quad->mask_uv_scale.ToString());
6041 EndTest();
6042 break;
6044 return draw_result;
6047 void DidCommit() override {
6048 switch (layer_tree_host()->source_frame_number()) {
6049 case 1:
6050 // Move the child to (-50, 0) instead. Now the mask should be moved to
6051 // still cover the layer being replicated.
6052 content_child_layer_->SetPosition(gfx::PointF(-50.f, 0.f));
6053 break;
6057 void AfterTest() override {}
6059 scoped_refptr<FakePictureLayer> content_child_layer_;
6060 FakeContentLayerClient client_;
6063 SINGLE_AND_MULTI_THREAD_TEST_F(
6064 LayerTreeTestReflectionMaskLayerForSurfaceWithUnclippedChild);
6066 class LayerTreeTestPageScaleFlags : public LayerTreeTest {
6067 protected:
6068 void SetupTree() override {
6069 // -root
6070 // -pre page scale
6071 // -page scale
6072 // -page scale child1
6073 // -page scale grandchild
6074 // -page scale child2
6075 // -post page scale
6077 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6078 scoped_refptr<Layer> pre_page_scale = Layer::Create(layer_settings());
6079 scoped_refptr<Layer> page_scale = Layer::Create(layer_settings());
6080 scoped_refptr<Layer> page_scale_child1 = Layer::Create(layer_settings());
6081 scoped_refptr<Layer> page_scale_grandchild =
6082 Layer::Create(layer_settings());
6083 scoped_refptr<Layer> page_scale_child2 = Layer::Create(layer_settings());
6084 scoped_refptr<Layer> post_page_scale = Layer::Create(layer_settings());
6086 root->AddChild(pre_page_scale);
6087 root->AddChild(page_scale);
6088 root->AddChild(post_page_scale);
6090 page_scale->AddChild(page_scale_child1);
6091 page_scale->AddChild(page_scale_child2);
6092 page_scale_child1->AddChild(page_scale_grandchild);
6094 layer_tree_host()->SetRootLayer(root);
6095 LayerTreeTest::SetupTree();
6097 scoped_refptr<Layer> overscroll_elasticity_layer = nullptr;
6098 scoped_refptr<Layer> inner_viewport_scroll_layer = nullptr;
6099 scoped_refptr<Layer> outer_viewport_scroll_layer = nullptr;
6100 layer_tree_host()->RegisterViewportLayers(
6101 overscroll_elasticity_layer, page_scale, inner_viewport_scroll_layer,
6102 outer_viewport_scroll_layer);
6104 affected_by_page_scale_.push_back(page_scale->id());
6105 affected_by_page_scale_.push_back(page_scale_child1->id());
6106 affected_by_page_scale_.push_back(page_scale_child2->id());
6107 affected_by_page_scale_.push_back(page_scale_grandchild->id());
6109 not_affected_by_page_scale_.push_back(root->id());
6110 not_affected_by_page_scale_.push_back(pre_page_scale->id());
6111 not_affected_by_page_scale_.push_back(post_page_scale->id());
6114 void BeginTest() override { PostSetNeedsCommitToMainThread(); }
6116 void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) override {
6117 LayerTreeHostCommon::CallFunctionForSubtree(
6118 host_impl->sync_tree()->root_layer(), [this](LayerImpl* layer) {
6119 const std::vector<int>& list =
6120 layer->IsAffectedByPageScale()
6121 ? this->affected_by_page_scale_
6122 : this->not_affected_by_page_scale_;
6123 EXPECT_TRUE(std::find(list.begin(), list.end(), layer->id()) !=
6124 list.end());
6127 EndTest();
6130 void AfterTest() override {}
6132 std::vector<int> affected_by_page_scale_;
6133 std::vector<int> not_affected_by_page_scale_;
6136 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeTestPageScaleFlags);
6138 } // namespace
6139 } // namespace cc