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"
9 #include "base/auto_reset.h"
10 #include "base/synchronization/lock.h"
11 #include "cc/animation/timing_function.h"
12 #include "cc/base/swap_promise.h"
13 #include "cc/debug/frame_rate_counter.h"
14 #include "cc/layers/content_layer.h"
15 #include "cc/layers/content_layer_client.h"
16 #include "cc/layers/io_surface_layer.h"
17 #include "cc/layers/layer_impl.h"
18 #include "cc/layers/painted_scrollbar_layer.h"
19 #include "cc/layers/picture_layer.h"
20 #include "cc/layers/solid_color_layer.h"
21 #include "cc/layers/video_layer.h"
22 #include "cc/output/begin_frame_args.h"
23 #include "cc/output/compositor_frame_ack.h"
24 #include "cc/output/copy_output_request.h"
25 #include "cc/output/copy_output_result.h"
26 #include "cc/output/output_surface.h"
27 #include "cc/quads/draw_quad.h"
28 #include "cc/quads/io_surface_draw_quad.h"
29 #include "cc/resources/prioritized_resource.h"
30 #include "cc/resources/prioritized_resource_manager.h"
31 #include "cc/resources/resource_update_queue.h"
32 #include "cc/test/fake_content_layer.h"
33 #include "cc/test/fake_content_layer_client.h"
34 #include "cc/test/fake_content_layer_impl.h"
35 #include "cc/test/fake_layer_tree_host_client.h"
36 #include "cc/test/fake_output_surface.h"
37 #include "cc/test/fake_painted_scrollbar_layer.h"
38 #include "cc/test/fake_picture_layer.h"
39 #include "cc/test/fake_picture_layer_impl.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/frame_time.h"
58 #include "ui/gfx/point_conversions.h"
59 #include "ui/gfx/size_conversions.h"
60 #include "ui/gfx/vector2d_conversions.h"
63 using testing::AnyNumber;
64 using testing::AtLeast;
70 class LayerTreeHostTest : public LayerTreeTest {};
72 // Two setNeedsCommits in a row should lead to at least 1 commit and at least 1
74 class LayerTreeHostTestSetNeedsCommit1 : public LayerTreeHostTest {
76 LayerTreeHostTestSetNeedsCommit1() : num_commits_(0), num_draws_(0) {}
78 virtual void BeginTest() OVERRIDE {
79 PostSetNeedsCommitToMainThread();
80 PostSetNeedsCommitToMainThread();
83 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
85 if (!impl->active_tree()->source_frame_number())
89 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
93 virtual void AfterTest() OVERRIDE {
94 EXPECT_LE(1, num_commits_);
95 EXPECT_LE(1, num_draws_);
103 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsCommit1);
105 // A SetNeedsCommit should lead to 1 commit. Issuing a second commit after that
106 // first committed frame draws should lead to another commit.
107 class LayerTreeHostTestSetNeedsCommit2 : public LayerTreeHostTest {
109 LayerTreeHostTestSetNeedsCommit2() : num_commits_(0), num_draws_(0) {}
111 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
113 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
117 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
119 switch (num_commits_) {
121 PostSetNeedsCommitToMainThread();
131 virtual void AfterTest() OVERRIDE {
132 EXPECT_EQ(2, num_commits_);
133 EXPECT_LE(1, num_draws_);
141 MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsCommit2);
143 // Verify that we pass property values in PushPropertiesTo.
144 class LayerTreeHostTestPushPropertiesTo : public LayerTreeHostTest {
146 virtual void SetupTree() OVERRIDE {
147 scoped_refptr<Layer> root = Layer::Create();
148 root->SetBounds(gfx::Size(10, 10));
149 layer_tree_host()->SetRootLayer(root);
150 LayerTreeHostTest::SetupTree();
156 HIDE_LAYER_AND_SUBTREE,
161 virtual void BeginTest() OVERRIDE {
163 PostSetNeedsCommitToMainThread();
166 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
167 VerifyAfterValues(impl->active_tree()->root_layer());
170 virtual void DidCommitAndDrawFrame() OVERRIDE {
171 SetBeforeValues(layer_tree_host()->root_layer());
172 VerifyBeforeValues(layer_tree_host()->root_layer());
175 if (index_ == DONE) {
180 SetAfterValues(layer_tree_host()->root_layer());
183 virtual void AfterTest() OVERRIDE {}
185 void VerifyBeforeValues(Layer* layer) {
186 EXPECT_EQ(gfx::Size(10, 10).ToString(), layer->bounds().ToString());
187 EXPECT_FALSE(layer->hide_layer_and_subtree());
188 EXPECT_FALSE(layer->DrawsContent());
191 void SetBeforeValues(Layer* layer) {
192 layer->SetBounds(gfx::Size(10, 10));
193 layer->SetHideLayerAndSubtree(false);
194 layer->SetIsDrawable(false);
197 void VerifyAfterValues(LayerImpl* layer) {
198 switch (static_cast<Properties>(index_)) {
203 EXPECT_EQ(gfx::Size(20, 20).ToString(), layer->bounds().ToString());
205 case HIDE_LAYER_AND_SUBTREE:
206 EXPECT_TRUE(layer->hide_layer_and_subtree());
209 EXPECT_TRUE(layer->DrawsContent());
214 void SetAfterValues(Layer* layer) {
215 switch (static_cast<Properties>(index_)) {
220 layer->SetBounds(gfx::Size(20, 20));
222 case HIDE_LAYER_AND_SUBTREE:
223 layer->SetHideLayerAndSubtree(true);
226 layer->SetIsDrawable(true);
234 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestPushPropertiesTo);
236 // 1 setNeedsRedraw after the first commit has completed should lead to 1
238 class LayerTreeHostTestSetNeedsRedraw : public LayerTreeHostTest {
240 LayerTreeHostTestSetNeedsRedraw() : num_commits_(0), num_draws_(0) {}
242 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
244 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
245 EXPECT_EQ(0, impl->active_tree()->source_frame_number());
247 // Redraw again to verify that the second redraw doesn't commit.
248 PostSetNeedsRedrawToMainThread();
255 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
256 EXPECT_EQ(0, num_draws_);
260 virtual void AfterTest() OVERRIDE {
261 EXPECT_GE(2, num_draws_);
262 EXPECT_EQ(1, num_commits_);
270 MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsRedraw);
272 // After setNeedsRedrawRect(invalid_rect) the final damage_rect
273 // must contain invalid_rect.
274 class LayerTreeHostTestSetNeedsRedrawRect : public LayerTreeHostTest {
276 LayerTreeHostTestSetNeedsRedrawRect()
279 invalid_rect_(10, 10, 20, 20),
280 root_layer_(ContentLayer::Create(&client_)) {}
282 virtual void BeginTest() OVERRIDE {
283 root_layer_->SetIsDrawable(true);
284 root_layer_->SetBounds(bounds_);
285 layer_tree_host()->SetRootLayer(root_layer_);
286 layer_tree_host()->SetViewportSize(bounds_);
287 PostSetNeedsCommitToMainThread();
290 virtual DrawResult PrepareToDrawOnThread(
291 LayerTreeHostImpl* host_impl,
292 LayerTreeHostImpl::FrameData* frame_data,
293 DrawResult draw_result) OVERRIDE {
294 EXPECT_EQ(DRAW_SUCCESS, draw_result);
296 gfx::RectF root_damage_rect;
297 if (!frame_data->render_passes.empty())
298 root_damage_rect = frame_data->render_passes.back()->damage_rect;
301 // If this is the first frame, expect full frame damage.
302 EXPECT_RECT_EQ(root_damage_rect, gfx::Rect(bounds_));
304 // Check that invalid_rect_ is indeed repainted.
305 EXPECT_TRUE(root_damage_rect.Contains(invalid_rect_));
311 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
313 PostSetNeedsRedrawRectToMainThread(invalid_rect_);
320 virtual void AfterTest() OVERRIDE { EXPECT_EQ(2, num_draws_); }
324 const gfx::Size bounds_;
325 const gfx::Rect invalid_rect_;
326 FakeContentLayerClient client_;
327 scoped_refptr<ContentLayer> root_layer_;
330 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSetNeedsRedrawRect);
332 class LayerTreeHostTestNoExtraCommitFromInvalidate : public LayerTreeHostTest {
334 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
335 settings->layer_transforms_should_scale_layer_contents = true;
338 virtual void SetupTree() OVERRIDE {
339 root_layer_ = Layer::Create();
340 root_layer_->SetBounds(gfx::Size(10, 20));
342 scaled_layer_ = FakeContentLayer::Create(&client_);
343 scaled_layer_->SetBounds(gfx::Size(1, 1));
344 root_layer_->AddChild(scaled_layer_);
346 layer_tree_host()->SetRootLayer(root_layer_);
347 LayerTreeHostTest::SetupTree();
350 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
352 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
353 if (host_impl->active_tree()->source_frame_number() == 1)
357 virtual void DidCommit() OVERRIDE {
358 switch (layer_tree_host()->source_frame_number()) {
360 // Changing the device scale factor causes a commit. It also changes
361 // the content bounds of |scaled_layer_|, which should not generate
362 // a second commit as a result.
363 layer_tree_host()->SetDeviceScaleFactor(4.f);
367 EXPECT_EQ(2, layer_tree_host()->source_frame_number());
371 virtual void AfterTest() OVERRIDE {
372 EXPECT_EQ(gfx::Size(4, 4).ToString(),
373 scaled_layer_->content_bounds().ToString());
377 FakeContentLayerClient client_;
378 scoped_refptr<Layer> root_layer_;
379 scoped_refptr<FakeContentLayer> scaled_layer_;
382 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestNoExtraCommitFromInvalidate);
384 class LayerTreeHostTestNoExtraCommitFromScrollbarInvalidate
385 : public LayerTreeHostTest {
387 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
388 settings->layer_transforms_should_scale_layer_contents = true;
391 virtual void SetupTree() OVERRIDE {
392 root_layer_ = Layer::Create();
393 root_layer_->SetBounds(gfx::Size(10, 20));
395 bool paint_scrollbar = true;
396 bool has_thumb = false;
397 scrollbar_ = FakePaintedScrollbarLayer::Create(
398 paint_scrollbar, has_thumb, root_layer_->id());
399 scrollbar_->SetPosition(gfx::Point(0, 10));
400 scrollbar_->SetBounds(gfx::Size(10, 10));
402 root_layer_->AddChild(scrollbar_);
404 layer_tree_host()->SetRootLayer(root_layer_);
405 LayerTreeHostTest::SetupTree();
408 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
410 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
411 if (host_impl->active_tree()->source_frame_number() == 1)
415 virtual void DidCommit() OVERRIDE {
416 switch (layer_tree_host()->source_frame_number()) {
418 // Changing the device scale factor causes a commit. It also changes
419 // the content bounds of |scrollbar_|, which should not generate
420 // a second commit as a result.
421 layer_tree_host()->SetDeviceScaleFactor(4.f);
425 EXPECT_EQ(2, layer_tree_host()->source_frame_number());
429 virtual void AfterTest() OVERRIDE {
430 EXPECT_EQ(gfx::Size(40, 40).ToString(),
431 scrollbar_->content_bounds().ToString());
435 FakeContentLayerClient client_;
436 scoped_refptr<Layer> root_layer_;
437 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_;
440 SINGLE_AND_MULTI_THREAD_TEST_F(
441 LayerTreeHostTestNoExtraCommitFromScrollbarInvalidate);
443 class LayerTreeHostTestSetNextCommitForcesRedraw : public LayerTreeHostTest {
445 LayerTreeHostTestSetNextCommitForcesRedraw()
448 invalid_rect_(10, 10, 20, 20),
449 root_layer_(ContentLayer::Create(&client_)) {}
451 virtual void BeginTest() OVERRIDE {
452 root_layer_->SetIsDrawable(true);
453 root_layer_->SetBounds(bounds_);
454 layer_tree_host()->SetRootLayer(root_layer_);
455 layer_tree_host()->SetViewportSize(bounds_);
456 PostSetNeedsCommitToMainThread();
459 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
460 if (num_draws_ == 3 && host_impl->settings().impl_side_painting)
461 host_impl->SetNeedsRedrawRect(invalid_rect_);
464 virtual DrawResult PrepareToDrawOnThread(
465 LayerTreeHostImpl* host_impl,
466 LayerTreeHostImpl::FrameData* frame_data,
467 DrawResult draw_result) OVERRIDE {
468 EXPECT_EQ(DRAW_SUCCESS, draw_result);
470 gfx::RectF root_damage_rect;
471 if (!frame_data->render_passes.empty())
472 root_damage_rect = frame_data->render_passes.back()->damage_rect;
474 switch (num_draws_) {
476 EXPECT_RECT_EQ(gfx::Rect(bounds_), root_damage_rect);
480 EXPECT_RECT_EQ(gfx::Rect(0, 0, 0, 0), root_damage_rect);
483 EXPECT_RECT_EQ(invalid_rect_, root_damage_rect);
486 EXPECT_RECT_EQ(gfx::Rect(bounds_), root_damage_rect);
495 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
496 switch (num_draws_) {
499 // Cycle through a couple of empty commits to ensure we're observing the
501 PostSetNeedsCommitToMainThread();
504 // Should force full frame damage on the next commit
505 PostSetNextCommitForcesRedrawToMainThread();
506 PostSetNeedsCommitToMainThread();
507 if (host_impl->settings().impl_side_painting)
508 host_impl->BlockNotifyReadyToActivateForTesting(true);
513 host_impl->BlockNotifyReadyToActivateForTesting(false);
522 virtual void AfterTest() OVERRIDE { EXPECT_EQ(5, num_draws_); }
526 const gfx::Size bounds_;
527 const gfx::Rect invalid_rect_;
528 FakeContentLayerClient client_;
529 scoped_refptr<ContentLayer> root_layer_;
532 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSetNextCommitForcesRedraw);
534 // Tests that if a layer is not drawn because of some reason in the parent then
535 // its damage is preserved until the next time it is drawn.
536 class LayerTreeHostTestUndrawnLayersDamageLater : public LayerTreeHostTest {
538 LayerTreeHostTestUndrawnLayersDamageLater()
539 : root_layer_(ContentLayer::Create(&client_)) {}
541 virtual void SetupTree() OVERRIDE {
542 root_layer_->SetIsDrawable(true);
543 root_layer_->SetBounds(gfx::Size(50, 50));
544 layer_tree_host()->SetRootLayer(root_layer_);
546 // The initially transparent layer has a larger child layer, which is
547 // not initially drawn because of the this (parent) layer.
548 parent_layer_ = FakeContentLayer::Create(&client_);
549 parent_layer_->SetBounds(gfx::Size(15, 15));
550 parent_layer_->SetOpacity(0.0f);
551 root_layer_->AddChild(parent_layer_);
553 child_layer_ = FakeContentLayer::Create(&client_);
554 child_layer_->SetBounds(gfx::Size(25, 25));
555 parent_layer_->AddChild(child_layer_);
557 LayerTreeHostTest::SetupTree();
560 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
562 virtual DrawResult PrepareToDrawOnThread(
563 LayerTreeHostImpl* host_impl,
564 LayerTreeHostImpl::FrameData* frame_data,
565 DrawResult draw_result) OVERRIDE {
566 EXPECT_EQ(DRAW_SUCCESS, draw_result);
568 gfx::RectF root_damage_rect;
569 if (!frame_data->render_passes.empty())
570 root_damage_rect = frame_data->render_passes.back()->damage_rect;
572 // The first time, the whole view needs be drawn.
573 // Afterwards, just the opacity of surface_layer1 is changed a few times,
574 // and each damage should be the bounding box of it and its child. If this
575 // was working improperly, the damage might not include its childs bounding
577 switch (host_impl->active_tree()->source_frame_number()) {
579 EXPECT_RECT_EQ(gfx::Rect(root_layer_->bounds()), root_damage_rect);
584 EXPECT_RECT_EQ(gfx::Rect(child_layer_->bounds()), root_damage_rect);
593 virtual void DidCommitAndDrawFrame() OVERRIDE {
594 switch (layer_tree_host()->source_frame_number()) {
596 // Test not owning the surface.
597 parent_layer_->SetOpacity(1.0f);
600 parent_layer_->SetOpacity(0.0f);
603 // Test owning the surface.
604 parent_layer_->SetOpacity(0.5f);
605 parent_layer_->SetForceRenderSurface(true);
615 virtual void AfterTest() OVERRIDE {}
618 FakeContentLayerClient client_;
619 scoped_refptr<ContentLayer> root_layer_;
620 scoped_refptr<FakeContentLayer> parent_layer_;
621 scoped_refptr<FakeContentLayer> child_layer_;
624 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestUndrawnLayersDamageLater);
626 // Tests that if a layer is not drawn because of some reason in the parent,
627 // causing its content bounds to not be computed, then when it is later drawn,
628 // its content bounds get pushed.
629 class LayerTreeHostTestUndrawnLayersPushContentBoundsLater
630 : public LayerTreeHostTest {
632 LayerTreeHostTestUndrawnLayersPushContentBoundsLater()
633 : root_layer_(Layer::Create()) {}
635 virtual void SetupTree() OVERRIDE {
636 root_layer_->SetIsDrawable(true);
637 root_layer_->SetBounds(gfx::Size(20, 20));
638 layer_tree_host()->SetRootLayer(root_layer_);
640 parent_layer_ = Layer::Create();
641 parent_layer_->SetBounds(gfx::Size(20, 20));
642 parent_layer_->SetOpacity(0.0f);
643 root_layer_->AddChild(parent_layer_);
645 child_layer_ = Layer::Create();
646 child_layer_->SetBounds(gfx::Size(15, 15));
647 parent_layer_->AddChild(child_layer_);
649 LayerTreeHostTest::SetupTree();
652 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
654 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
655 LayerImpl* root = host_impl->active_tree()->root_layer();
656 LayerImpl* parent = root->children()[0];
657 LayerImpl* child = parent->children()[0];
659 switch (host_impl->active_tree()->source_frame_number()) {
661 EXPECT_EQ(0.f, parent->opacity());
662 EXPECT_EQ(gfx::SizeF(), child->content_bounds());
665 EXPECT_EQ(1.f, parent->opacity());
666 EXPECT_EQ(gfx::SizeF(15.f, 15.f), child->content_bounds());
674 virtual void DidCommit() OVERRIDE {
675 switch (layer_tree_host()->source_frame_number()) {
677 parent_layer_->SetOpacity(1.0f);
686 virtual void AfterTest() OVERRIDE {}
689 scoped_refptr<Layer> root_layer_;
690 scoped_refptr<Layer> parent_layer_;
691 scoped_refptr<Layer> child_layer_;
694 SINGLE_AND_MULTI_THREAD_TEST_F(
695 LayerTreeHostTestUndrawnLayersPushContentBoundsLater);
697 // This test verifies that properties on the layer tree host are commited
699 class LayerTreeHostTestCommit : public LayerTreeHostTest {
701 LayerTreeHostTestCommit() {}
703 virtual void BeginTest() OVERRIDE {
704 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
705 layer_tree_host()->set_background_color(SK_ColorGRAY);
707 PostSetNeedsCommitToMainThread();
710 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
711 EXPECT_EQ(gfx::Size(20, 20), impl->DrawViewportSize());
712 EXPECT_EQ(SK_ColorGRAY, impl->active_tree()->background_color());
717 virtual void AfterTest() OVERRIDE {}
720 MULTI_THREAD_TEST_F(LayerTreeHostTestCommit);
722 // This test verifies that LayerTreeHostImpl's current frame time gets
723 // updated in consecutive frames when it doesn't draw due to tree
724 // activation failure.
725 class LayerTreeHostTestFrameTimeUpdatesAfterActivationFails
726 : public LayerTreeHostTest {
728 LayerTreeHostTestFrameTimeUpdatesAfterActivationFails()
729 : frame_count_with_pending_tree_(0) {}
731 virtual void BeginTest() OVERRIDE {
732 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
733 layer_tree_host()->set_background_color(SK_ColorGRAY);
735 PostSetNeedsCommitToMainThread();
738 virtual void BeginCommitOnThread(LayerTreeHostImpl* impl) OVERRIDE {
739 EXPECT_EQ(frame_count_with_pending_tree_, 0);
740 impl->BlockNotifyReadyToActivateForTesting(true);
743 virtual void WillBeginImplFrameOnThread(LayerTreeHostImpl* impl,
744 const BeginFrameArgs& args) OVERRIDE {
745 if (impl->pending_tree())
746 frame_count_with_pending_tree_++;
748 if (frame_count_with_pending_tree_ == 1) {
749 EXPECT_EQ(first_frame_time_.ToInternalValue(), 0);
750 first_frame_time_ = impl->CurrentBeginFrameArgs().frame_time;
751 } else if (frame_count_with_pending_tree_ == 2) {
752 impl->BlockNotifyReadyToActivateForTesting(false);
756 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
757 if (frame_count_with_pending_tree_ > 1) {
758 EXPECT_NE(first_frame_time_.ToInternalValue(), 0);
759 EXPECT_NE(first_frame_time_.ToInternalValue(),
760 impl->CurrentBeginFrameArgs().frame_time.ToInternalValue());
765 EXPECT_FALSE(impl->settings().impl_side_painting);
768 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
769 if (impl->settings().impl_side_painting)
770 EXPECT_NE(frame_count_with_pending_tree_, 1);
773 virtual void AfterTest() OVERRIDE {}
776 int frame_count_with_pending_tree_;
777 base::TimeTicks first_frame_time_;
780 SINGLE_AND_MULTI_THREAD_TEST_F(
781 LayerTreeHostTestFrameTimeUpdatesAfterActivationFails);
783 // This test verifies that LayerTreeHostImpl's current frame time gets
784 // updated in consecutive frames when it draws in each frame.
785 class LayerTreeHostTestFrameTimeUpdatesAfterDraw : public LayerTreeHostTest {
787 LayerTreeHostTestFrameTimeUpdatesAfterDraw() : frame_(0) {}
789 virtual void BeginTest() OVERRIDE {
790 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
791 layer_tree_host()->set_background_color(SK_ColorGRAY);
793 PostSetNeedsCommitToMainThread();
796 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
799 first_frame_time_ = impl->CurrentBeginFrameArgs().frame_time;
800 impl->SetNeedsRedraw();
802 // Since we might use a low-resolution clock on Windows, we need to
803 // make sure that the clock has incremented past first_frame_time_.
804 while (first_frame_time_ == gfx::FrameTime::Now()) {
810 EXPECT_NE(first_frame_time_, impl->CurrentBeginFrameArgs().frame_time);
814 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
815 // Ensure there isn't a commit between the two draws, to ensure that a
816 // commit isn't required for updating the current frame time. We can
817 // only check for this in the multi-threaded case, since in the single-
818 // threaded case there will always be a commit between consecutive draws.
820 EXPECT_EQ(0, frame_);
823 virtual void AfterTest() OVERRIDE {}
827 base::TimeTicks first_frame_time_;
830 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestFrameTimeUpdatesAfterDraw);
832 // Verifies that StartPageScaleAnimation events propagate correctly
833 // from LayerTreeHost to LayerTreeHostImpl in the MT compositor.
834 class LayerTreeHostTestStartPageScaleAnimation : public LayerTreeHostTest {
836 LayerTreeHostTestStartPageScaleAnimation() {}
838 virtual void SetupTree() OVERRIDE {
839 LayerTreeHostTest::SetupTree();
841 if (layer_tree_host()->settings().impl_side_painting) {
842 scoped_refptr<FakePictureLayer> layer =
843 FakePictureLayer::Create(&client_);
844 layer->set_always_update_resources(true);
845 scroll_layer_ = layer;
847 scroll_layer_ = FakeContentLayer::Create(&client_);
850 Layer* root_layer = layer_tree_host()->root_layer();
851 scroll_layer_->SetScrollClipLayerId(root_layer->id());
852 scroll_layer_->SetIsContainerForFixedPositionLayers(true);
853 scroll_layer_->SetBounds(gfx::Size(2 * root_layer->bounds().width(),
854 2 * root_layer->bounds().height()));
855 scroll_layer_->SetScrollOffset(gfx::Vector2d());
856 layer_tree_host()->root_layer()->AddChild(scroll_layer_);
857 // This test requires the page_scale and inner viewport layers to be
859 layer_tree_host()->RegisterViewportLayers(
860 root_layer, scroll_layer_.get(), NULL);
861 layer_tree_host()->SetPageScaleFactorAndLimits(1.f, 0.5f, 2.f);
864 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
866 virtual void ApplyViewportDeltas(const gfx::Vector2d& scroll_delta,
869 gfx::Vector2d offset = scroll_layer_->scroll_offset();
870 scroll_layer_->SetScrollOffset(offset + scroll_delta);
871 layer_tree_host()->SetPageScaleFactorAndLimits(scale, 0.5f, 2.f);
874 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
875 // We get one commit before the first draw, and the animation doesn't happen
876 // until the second draw.
877 switch (impl->active_tree()->source_frame_number()) {
879 EXPECT_EQ(1.f, impl->active_tree()->page_scale_factor());
880 // We'll start an animation when we get back to the main thread.
883 EXPECT_EQ(1.f, impl->active_tree()->page_scale_factor());
886 EXPECT_EQ(1.25f, impl->active_tree()->page_scale_factor());
894 virtual void DidCommitAndDrawFrame() OVERRIDE {
895 switch (layer_tree_host()->source_frame_number()) {
897 layer_tree_host()->StartPageScaleAnimation(
898 gfx::Vector2d(), false, 1.25f, base::TimeDelta());
903 virtual void AfterTest() OVERRIDE {}
905 FakeContentLayerClient client_;
906 scoped_refptr<Layer> scroll_layer_;
909 MULTI_THREAD_TEST_F(LayerTreeHostTestStartPageScaleAnimation);
911 class LayerTreeHostTestSetVisible : public LayerTreeHostTest {
913 LayerTreeHostTestSetVisible() : num_draws_(0) {}
915 virtual void BeginTest() OVERRIDE {
916 PostSetNeedsCommitToMainThread();
917 PostSetVisibleToMainThread(false);
918 // This is suppressed while we're invisible.
919 PostSetNeedsRedrawToMainThread();
920 // Triggers the redraw.
921 PostSetVisibleToMainThread(true);
924 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
925 EXPECT_TRUE(impl->visible());
930 virtual void AfterTest() OVERRIDE { EXPECT_EQ(1, num_draws_); }
936 MULTI_THREAD_TEST_F(LayerTreeHostTestSetVisible);
938 class TestOpacityChangeLayerDelegate : public ContentLayerClient {
940 TestOpacityChangeLayerDelegate() : test_layer_(0) {}
942 void SetTestLayer(Layer* test_layer) { test_layer_ = test_layer; }
944 virtual void PaintContents(
946 const gfx::Rect& clip,
947 ContentLayerClient::GraphicsContextStatus gc_status) OVERRIDE {
948 // Set layer opacity to 0.
950 test_layer_->SetOpacity(0.f);
952 virtual void DidChangeLayerCanUseLCDText() OVERRIDE {}
953 virtual bool FillsBoundsCompletely() const OVERRIDE { return false; }
959 class ContentLayerWithUpdateTracking : public ContentLayer {
961 static scoped_refptr<ContentLayerWithUpdateTracking> Create(
962 ContentLayerClient* client) {
963 return make_scoped_refptr(new ContentLayerWithUpdateTracking(client));
966 int PaintContentsCount() { return paint_contents_count_; }
967 void ResetPaintContentsCount() { paint_contents_count_ = 0; }
969 virtual bool Update(ResourceUpdateQueue* queue,
970 const OcclusionTracker<Layer>* occlusion) OVERRIDE {
971 bool updated = ContentLayer::Update(queue, occlusion);
972 paint_contents_count_++;
977 explicit ContentLayerWithUpdateTracking(ContentLayerClient* client)
978 : ContentLayer(client), paint_contents_count_(0) {
979 SetBounds(gfx::Size(10, 10));
982 virtual ~ContentLayerWithUpdateTracking() {}
984 int paint_contents_count_;
987 // Layer opacity change during paint should not prevent compositor resources
988 // from being updated during commit.
989 class LayerTreeHostTestOpacityChange : public LayerTreeHostTest {
991 LayerTreeHostTestOpacityChange()
992 : test_opacity_change_delegate_(),
993 update_check_layer_(ContentLayerWithUpdateTracking::Create(
994 &test_opacity_change_delegate_)) {
995 test_opacity_change_delegate_.SetTestLayer(update_check_layer_.get());
998 virtual void BeginTest() OVERRIDE {
999 layer_tree_host()->SetViewportSize(gfx::Size(10, 10));
1000 layer_tree_host()->root_layer()->AddChild(update_check_layer_);
1002 PostSetNeedsCommitToMainThread();
1005 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1009 virtual void AfterTest() OVERRIDE {
1010 // Update() should have been called once.
1011 EXPECT_EQ(1, update_check_layer_->PaintContentsCount());
1015 TestOpacityChangeLayerDelegate test_opacity_change_delegate_;
1016 scoped_refptr<ContentLayerWithUpdateTracking> update_check_layer_;
1019 MULTI_THREAD_TEST_F(LayerTreeHostTestOpacityChange);
1021 class NoScaleContentLayer : public ContentLayer {
1023 static scoped_refptr<NoScaleContentLayer> Create(ContentLayerClient* client) {
1024 return make_scoped_refptr(new NoScaleContentLayer(client));
1027 virtual void CalculateContentsScale(float ideal_contents_scale,
1028 float* contents_scale_x,
1029 float* contents_scale_y,
1030 gfx::Size* contentBounds) OVERRIDE {
1031 // Skip over the ContentLayer's method to the base Layer class.
1032 Layer::CalculateContentsScale(ideal_contents_scale,
1039 explicit NoScaleContentLayer(ContentLayerClient* client)
1040 : ContentLayer(client) {}
1041 virtual ~NoScaleContentLayer() {}
1044 class LayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers
1045 : public LayerTreeHostTest {
1047 LayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers()
1048 : root_layer_(NoScaleContentLayer::Create(&client_)),
1049 child_layer_(ContentLayer::Create(&client_)) {}
1051 virtual void BeginTest() OVERRIDE {
1052 layer_tree_host()->SetViewportSize(gfx::Size(60, 60));
1053 layer_tree_host()->SetDeviceScaleFactor(1.5);
1054 EXPECT_EQ(gfx::Size(60, 60), layer_tree_host()->device_viewport_size());
1056 root_layer_->AddChild(child_layer_);
1058 root_layer_->SetIsDrawable(true);
1059 root_layer_->SetBounds(gfx::Size(30, 30));
1061 child_layer_->SetIsDrawable(true);
1062 child_layer_->SetPosition(gfx::Point(2, 2));
1063 child_layer_->SetBounds(gfx::Size(10, 10));
1065 layer_tree_host()->SetRootLayer(root_layer_);
1067 PostSetNeedsCommitToMainThread();
1070 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1071 // Should only do one commit.
1072 EXPECT_EQ(0, impl->active_tree()->source_frame_number());
1073 // Device scale factor should come over to impl.
1074 EXPECT_NEAR(impl->device_scale_factor(), 1.5f, 0.00001f);
1076 // Both layers are on impl.
1077 ASSERT_EQ(1u, impl->active_tree()->root_layer()->children().size());
1079 // Device viewport is scaled.
1080 EXPECT_EQ(gfx::Size(60, 60), impl->DrawViewportSize());
1082 LayerImpl* root = impl->active_tree()->root_layer();
1083 LayerImpl* child = impl->active_tree()->root_layer()->children()[0];
1085 // Positions remain in layout pixels.
1086 EXPECT_EQ(gfx::Point(0, 0), root->position());
1087 EXPECT_EQ(gfx::Point(2, 2), child->position());
1089 // Compute all the layer transforms for the frame.
1090 LayerTreeHostImpl::FrameData frame_data;
1091 impl->PrepareToDraw(&frame_data);
1092 impl->DidDrawAllLayers(frame_data);
1094 const LayerImplList& render_surface_layer_list =
1095 *frame_data.render_surface_layer_list;
1097 // Both layers should be drawing into the root render surface.
1098 ASSERT_EQ(1u, render_surface_layer_list.size());
1099 ASSERT_EQ(root->render_surface(),
1100 render_surface_layer_list[0]->render_surface());
1101 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
1103 // The root render surface is the size of the viewport.
1104 EXPECT_RECT_EQ(gfx::Rect(0, 0, 60, 60),
1105 root->render_surface()->content_rect());
1107 // The content bounds of the child should be scaled.
1108 gfx::Size child_bounds_scaled =
1109 gfx::ToCeiledSize(gfx::ScaleSize(child->bounds(), 1.5));
1110 EXPECT_EQ(child_bounds_scaled, child->content_bounds());
1112 gfx::Transform scale_transform;
1113 scale_transform.Scale(impl->device_scale_factor(),
1114 impl->device_scale_factor());
1116 // The root layer is scaled by 2x.
1117 gfx::Transform root_screen_space_transform = scale_transform;
1118 gfx::Transform root_draw_transform = scale_transform;
1120 EXPECT_EQ(root_draw_transform, root->draw_transform());
1121 EXPECT_EQ(root_screen_space_transform, root->screen_space_transform());
1123 // The child is at position 2,2, which is transformed to 3,3 after the scale
1124 gfx::Transform child_screen_space_transform;
1125 child_screen_space_transform.Translate(3.f, 3.f);
1126 gfx::Transform child_draw_transform = child_screen_space_transform;
1128 EXPECT_TRANSFORMATION_MATRIX_EQ(child_draw_transform,
1129 child->draw_transform());
1130 EXPECT_TRANSFORMATION_MATRIX_EQ(child_screen_space_transform,
1131 child->screen_space_transform());
1136 virtual void AfterTest() OVERRIDE {}
1139 FakeContentLayerClient client_;
1140 scoped_refptr<NoScaleContentLayer> root_layer_;
1141 scoped_refptr<ContentLayer> child_layer_;
1144 MULTI_THREAD_TEST_F(LayerTreeHostTestDeviceScaleFactorScalesViewportAndLayers);
1146 // Verify atomicity of commits and reuse of textures.
1147 class LayerTreeHostTestDirectRendererAtomicCommit : public LayerTreeHostTest {
1149 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
1150 settings->texture_id_allocation_chunk_size = 1;
1151 // Make sure partial texture updates are turned off.
1152 settings->max_partial_texture_updates = 0;
1153 // Linear fade animator prevents scrollbars from drawing immediately.
1154 settings->scrollbar_animator = LayerTreeSettings::NoAnimator;
1157 virtual void SetupTree() OVERRIDE {
1158 layer_ = FakeContentLayer::Create(&client_);
1159 layer_->SetBounds(gfx::Size(10, 20));
1161 bool paint_scrollbar = true;
1162 bool has_thumb = false;
1163 scrollbar_ = FakePaintedScrollbarLayer::Create(
1164 paint_scrollbar, has_thumb, layer_->id());
1165 scrollbar_->SetPosition(gfx::Point(0, 10));
1166 scrollbar_->SetBounds(gfx::Size(10, 10));
1168 layer_->AddChild(scrollbar_);
1170 layer_tree_host()->SetRootLayer(layer_);
1171 LayerTreeHostTest::SetupTree();
1174 virtual void BeginTest() OVERRIDE {
1176 PostSetNeedsCommitToMainThread();
1179 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1180 ASSERT_EQ(0u, layer_tree_host()->settings().max_partial_texture_updates);
1182 TestWebGraphicsContext3D* context = TestContext();
1184 switch (impl->active_tree()->source_frame_number()) {
1186 // Number of textures should be one for each layer
1187 ASSERT_EQ(2u, context->NumTextures());
1188 // Number of textures used for commit should be one for each layer.
1189 EXPECT_EQ(2u, context->NumUsedTextures());
1190 // Verify that used texture is correct.
1191 EXPECT_TRUE(context->UsedTexture(context->TextureAt(0)));
1192 EXPECT_TRUE(context->UsedTexture(context->TextureAt(1)));
1194 context->ResetUsedTextures();
1197 // Number of textures should be one for scrollbar layer since it was
1198 // requested and deleted on the impl-thread, and double for the content
1199 // layer since its first texture is used by impl thread and cannot by
1201 ASSERT_EQ(3u, context->NumTextures());
1202 // Number of textures used for commit should be one for each layer.
1203 EXPECT_EQ(2u, context->NumUsedTextures());
1204 // First textures should not have been used.
1205 EXPECT_FALSE(context->UsedTexture(context->TextureAt(0)));
1206 EXPECT_TRUE(context->UsedTexture(context->TextureAt(1)));
1207 // New textures should have been used.
1208 EXPECT_TRUE(context->UsedTexture(context->TextureAt(2)));
1209 context->ResetUsedTextures();
1220 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1221 TestWebGraphicsContext3D* context = TestContext();
1223 if (drew_frame_ == impl->active_tree()->source_frame_number()) {
1224 EXPECT_EQ(0u, context->NumUsedTextures()) << "For frame " << drew_frame_;
1227 drew_frame_ = impl->active_tree()->source_frame_number();
1229 // We draw/ship one texture each frame for each layer.
1230 EXPECT_EQ(2u, context->NumUsedTextures());
1231 context->ResetUsedTextures();
1234 PostSetNeedsCommitToMainThread();
1237 virtual void Layout() OVERRIDE {
1238 layer_->SetNeedsDisplay();
1239 scrollbar_->SetNeedsDisplay();
1242 virtual void AfterTest() OVERRIDE {}
1245 FakeContentLayerClient client_;
1246 scoped_refptr<FakeContentLayer> layer_;
1247 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_;
1251 MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(
1252 LayerTreeHostTestDirectRendererAtomicCommit);
1254 class LayerTreeHostTestDelegatingRendererAtomicCommit
1255 : public LayerTreeHostTestDirectRendererAtomicCommit {
1257 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1258 ASSERT_EQ(0u, layer_tree_host()->settings().max_partial_texture_updates);
1260 TestWebGraphicsContext3D* context = TestContext();
1262 switch (impl->active_tree()->source_frame_number()) {
1264 // Number of textures should be one for each layer
1265 ASSERT_EQ(2u, context->NumTextures());
1266 // Number of textures used for commit should be one for each layer.
1267 EXPECT_EQ(2u, context->NumUsedTextures());
1268 // Verify that used texture is correct.
1269 EXPECT_TRUE(context->UsedTexture(context->TextureAt(0)));
1270 EXPECT_TRUE(context->UsedTexture(context->TextureAt(1)));
1271 context->ResetUsedTextures();
1274 // Number of textures should be doubled as the first context layer
1275 // texture is being used by the impl-thread and cannot be used for
1276 // update. The scrollbar behavior is different direct renderer because
1277 // UI resource deletion with delegating renderer occurs after tree
1279 ASSERT_EQ(4u, context->NumTextures());
1280 // Number of textures used for commit should still be
1281 // one for each layer.
1282 EXPECT_EQ(2u, context->NumUsedTextures());
1283 // First textures should not have been used.
1284 EXPECT_FALSE(context->UsedTexture(context->TextureAt(0)));
1285 EXPECT_FALSE(context->UsedTexture(context->TextureAt(1)));
1286 // New textures should have been used.
1287 EXPECT_TRUE(context->UsedTexture(context->TextureAt(2)));
1288 EXPECT_TRUE(context->UsedTexture(context->TextureAt(3)));
1289 context->ResetUsedTextures();
1301 MULTI_THREAD_DELEGATING_RENDERER_NOIMPL_TEST_F(
1302 LayerTreeHostTestDelegatingRendererAtomicCommit);
1304 static void SetLayerPropertiesForTesting(Layer* layer,
1306 const gfx::Transform& transform,
1307 const gfx::Point3F& transform_origin,
1308 const gfx::PointF& position,
1309 const gfx::Size& bounds,
1311 layer->RemoveAllChildren();
1313 parent->AddChild(layer);
1314 layer->SetTransform(transform);
1315 layer->SetTransformOrigin(transform_origin);
1316 layer->SetPosition(position);
1317 layer->SetBounds(bounds);
1318 layer->SetContentsOpaque(opaque);
1321 class LayerTreeHostTestAtomicCommitWithPartialUpdate
1322 : public LayerTreeHostTest {
1324 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
1325 settings->texture_id_allocation_chunk_size = 1;
1326 // Allow one partial texture update.
1327 settings->max_partial_texture_updates = 1;
1328 // No partial updates when impl side painting is enabled.
1329 settings->impl_side_painting = false;
1332 virtual void SetupTree() OVERRIDE {
1333 parent_ = FakeContentLayer::Create(&client_);
1334 parent_->SetBounds(gfx::Size(10, 20));
1336 child_ = FakeContentLayer::Create(&client_);
1337 child_->SetPosition(gfx::Point(0, 10));
1338 child_->SetBounds(gfx::Size(3, 10));
1340 parent_->AddChild(child_);
1342 layer_tree_host()->SetRootLayer(parent_);
1343 LayerTreeHostTest::SetupTree();
1346 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
1348 virtual void DidCommitAndDrawFrame() OVERRIDE {
1349 switch (layer_tree_host()->source_frame_number()) {
1351 parent_->SetNeedsDisplay();
1352 child_->SetNeedsDisplay();
1355 // Damage part of layers.
1356 parent_->SetNeedsDisplayRect(gfx::RectF(0.f, 0.f, 5.f, 5.f));
1357 child_->SetNeedsDisplayRect(gfx::RectF(0.f, 0.f, 5.f, 5.f));
1360 child_->SetNeedsDisplay();
1361 layer_tree_host()->SetViewportSize(gfx::Size(10, 10));
1364 layer_tree_host()->SetViewportSize(gfx::Size(10, 20));
1370 NOTREACHED() << layer_tree_host()->source_frame_number();
1375 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1376 ASSERT_EQ(1u, layer_tree_host()->settings().max_partial_texture_updates);
1378 TestWebGraphicsContext3D* context = TestContext();
1380 switch (impl->active_tree()->source_frame_number()) {
1382 // Number of textures should be one for each layer.
1383 ASSERT_EQ(2u, context->NumTextures());
1384 // Number of textures used for commit should be one for each layer.
1385 EXPECT_EQ(2u, context->NumUsedTextures());
1386 // Verify that used textures are correct.
1387 EXPECT_TRUE(context->UsedTexture(context->TextureAt(0)));
1388 EXPECT_TRUE(context->UsedTexture(context->TextureAt(1)));
1389 context->ResetUsedTextures();
1392 if (HasImplThread()) {
1393 // Number of textures should be two for each content layer.
1394 ASSERT_EQ(4u, context->NumTextures());
1396 // In single thread we can always do partial updates, so the limit has
1398 ASSERT_EQ(2u, context->NumTextures());
1400 // Number of textures used for commit should be one for each content
1402 EXPECT_EQ(2u, context->NumUsedTextures());
1404 if (HasImplThread()) {
1405 // First content textures should not have been used.
1406 EXPECT_FALSE(context->UsedTexture(context->TextureAt(0)));
1407 EXPECT_FALSE(context->UsedTexture(context->TextureAt(1)));
1408 // New textures should have been used.
1409 EXPECT_TRUE(context->UsedTexture(context->TextureAt(2)));
1410 EXPECT_TRUE(context->UsedTexture(context->TextureAt(3)));
1412 // In single thread we can always do partial updates, so the limit has
1414 EXPECT_TRUE(context->UsedTexture(context->TextureAt(0)));
1415 EXPECT_TRUE(context->UsedTexture(context->TextureAt(1)));
1418 context->ResetUsedTextures();
1421 if (HasImplThread()) {
1422 // Number of textures should be two for each content layer.
1423 ASSERT_EQ(4u, context->NumTextures());
1425 // In single thread we can always do partial updates, so the limit has
1427 ASSERT_EQ(2u, context->NumTextures());
1429 // Number of textures used for commit should be one for each content
1431 EXPECT_EQ(2u, context->NumUsedTextures());
1433 if (HasImplThread()) {
1434 // One content layer does a partial update also.
1435 EXPECT_TRUE(context->UsedTexture(context->TextureAt(2)));
1436 EXPECT_FALSE(context->UsedTexture(context->TextureAt(3)));
1438 // In single thread we can always do partial updates, so the limit has
1440 EXPECT_TRUE(context->UsedTexture(context->TextureAt(0)));
1441 EXPECT_TRUE(context->UsedTexture(context->TextureAt(1)));
1444 context->ResetUsedTextures();
1447 // No textures should be used for commit.
1448 EXPECT_EQ(0u, context->NumUsedTextures());
1450 context->ResetUsedTextures();
1453 // Number of textures used for commit should be one, for the
1455 EXPECT_EQ(1u, context->NumUsedTextures());
1457 context->ResetUsedTextures();
1465 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1466 EXPECT_LT(impl->active_tree()->source_frame_number(), 5);
1468 TestWebGraphicsContext3D* context = TestContext();
1470 // Number of textures used for drawing should one per layer except for
1471 // frame 3 where the viewport only contains one layer.
1472 if (impl->active_tree()->source_frame_number() == 3) {
1473 EXPECT_EQ(1u, context->NumUsedTextures());
1475 EXPECT_EQ(2u, context->NumUsedTextures())
1476 << "For frame " << impl->active_tree()->source_frame_number();
1479 context->ResetUsedTextures();
1482 virtual void AfterTest() OVERRIDE {}
1485 FakeContentLayerClient client_;
1486 scoped_refptr<FakeContentLayer> parent_;
1487 scoped_refptr<FakeContentLayer> child_;
1490 // Partial updates are not possible with a delegating renderer.
1491 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
1492 LayerTreeHostTestAtomicCommitWithPartialUpdate);
1494 class LayerTreeHostTestSurfaceNotAllocatedForLayersOutsideMemoryLimit
1495 : public LayerTreeHostTest {
1497 virtual void SetupTree() OVERRIDE {
1498 root_layer_ = FakeContentLayer::Create(&client_);
1499 root_layer_->SetBounds(gfx::Size(100, 100));
1501 surface_layer1_ = FakeContentLayer::Create(&client_);
1502 surface_layer1_->SetBounds(gfx::Size(100, 100));
1503 surface_layer1_->SetForceRenderSurface(true);
1504 surface_layer1_->SetOpacity(0.5f);
1505 root_layer_->AddChild(surface_layer1_);
1507 surface_layer2_ = FakeContentLayer::Create(&client_);
1508 surface_layer2_->SetBounds(gfx::Size(100, 100));
1509 surface_layer2_->SetForceRenderSurface(true);
1510 surface_layer2_->SetOpacity(0.5f);
1511 surface_layer1_->AddChild(surface_layer2_);
1513 replica_layer1_ = FakeContentLayer::Create(&client_);
1514 surface_layer1_->SetReplicaLayer(replica_layer1_.get());
1516 replica_layer2_ = FakeContentLayer::Create(&client_);
1517 surface_layer2_->SetReplicaLayer(replica_layer2_.get());
1519 layer_tree_host()->SetRootLayer(root_layer_);
1520 LayerTreeHostTest::SetupTree();
1523 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
1525 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
1526 Renderer* renderer = host_impl->renderer();
1527 RenderPassId surface1_render_pass_id = host_impl->active_tree()
1531 ->GetRenderPassId();
1532 RenderPassId surface2_render_pass_id = host_impl->active_tree()
1537 ->GetRenderPassId();
1539 switch (host_impl->active_tree()->source_frame_number()) {
1542 renderer->HasAllocatedResourcesForTesting(surface1_render_pass_id));
1544 renderer->HasAllocatedResourcesForTesting(surface2_render_pass_id));
1546 // Reduce the memory limit to only fit the root layer and one render
1547 // surface. This prevents any contents drawing into surfaces
1548 // from being allocated.
1549 host_impl->SetMemoryPolicy(ManagedMemoryPolicy(100 * 100 * 4 * 2));
1553 renderer->HasAllocatedResourcesForTesting(surface1_render_pass_id));
1555 renderer->HasAllocatedResourcesForTesting(surface2_render_pass_id));
1562 virtual void DidCommitAndDrawFrame() OVERRIDE {
1563 if (layer_tree_host()->source_frame_number() < 2)
1564 root_layer_->SetNeedsDisplay();
1567 virtual void AfterTest() OVERRIDE {
1568 EXPECT_LE(2u, root_layer_->update_count());
1569 EXPECT_LE(2u, surface_layer1_->update_count());
1570 EXPECT_LE(2u, surface_layer2_->update_count());
1573 FakeContentLayerClient client_;
1574 scoped_refptr<FakeContentLayer> root_layer_;
1575 scoped_refptr<FakeContentLayer> surface_layer1_;
1576 scoped_refptr<FakeContentLayer> replica_layer1_;
1577 scoped_refptr<FakeContentLayer> surface_layer2_;
1578 scoped_refptr<FakeContentLayer> replica_layer2_;
1581 // Surfaces don't exist with a delegated renderer.
1582 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_NOIMPL_TEST_F(
1583 LayerTreeHostTestSurfaceNotAllocatedForLayersOutsideMemoryLimit);
1585 class EvictionTestLayer : public Layer {
1587 static scoped_refptr<EvictionTestLayer> Create() {
1588 return make_scoped_refptr(new EvictionTestLayer());
1591 virtual bool Update(ResourceUpdateQueue*,
1592 const OcclusionTracker<Layer>*) OVERRIDE;
1593 virtual bool DrawsContent() const OVERRIDE { return true; }
1595 virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl)
1597 virtual void PushPropertiesTo(LayerImpl* impl) OVERRIDE;
1598 virtual void SetTexturePriorities(const PriorityCalculator&) OVERRIDE;
1600 bool HaveBackingTexture() const {
1601 return texture_.get() ? texture_->have_backing_texture() : false;
1605 EvictionTestLayer() : Layer() {}
1606 virtual ~EvictionTestLayer() {}
1608 void CreateTextureIfNeeded() {
1611 texture_ = PrioritizedResource::Create(
1612 layer_tree_host()->contents_texture_manager());
1613 texture_->SetDimensions(gfx::Size(10, 10), RGBA_8888);
1614 bitmap_.allocN32Pixels(10, 10);
1617 scoped_ptr<PrioritizedResource> texture_;
1621 class EvictionTestLayerImpl : public LayerImpl {
1623 static scoped_ptr<EvictionTestLayerImpl> Create(LayerTreeImpl* tree_impl,
1625 return make_scoped_ptr(new EvictionTestLayerImpl(tree_impl, id));
1627 virtual ~EvictionTestLayerImpl() {}
1629 virtual void AppendQuads(RenderPass* render_pass,
1630 const OcclusionTracker<LayerImpl>& occlusion_tracker,
1631 AppendQuadsData* append_quads_data) OVERRIDE {
1632 ASSERT_TRUE(has_texture_);
1633 ASSERT_NE(0u, layer_tree_impl()->resource_provider()->num_resources());
1636 void SetHasTexture(bool has_texture) { has_texture_ = has_texture; }
1639 EvictionTestLayerImpl(LayerTreeImpl* tree_impl, int id)
1640 : LayerImpl(tree_impl, id), has_texture_(false) {}
1645 void EvictionTestLayer::SetTexturePriorities(const PriorityCalculator&) {
1646 CreateTextureIfNeeded();
1649 texture_->set_request_priority(PriorityCalculator::UIPriority(true));
1652 bool EvictionTestLayer::Update(ResourceUpdateQueue* queue,
1653 const OcclusionTracker<Layer>* occlusion) {
1654 CreateTextureIfNeeded();
1658 gfx::Rect full_rect(0, 0, 10, 10);
1659 ResourceUpdate upload = ResourceUpdate::Create(
1660 texture_.get(), &bitmap_, full_rect, full_rect, gfx::Vector2d());
1661 queue->AppendFullUpload(upload);
1665 scoped_ptr<LayerImpl> EvictionTestLayer::CreateLayerImpl(
1666 LayerTreeImpl* tree_impl) {
1667 return EvictionTestLayerImpl::Create(tree_impl, layer_id_)
1668 .PassAs<LayerImpl>();
1671 void EvictionTestLayer::PushPropertiesTo(LayerImpl* layer_impl) {
1672 Layer::PushPropertiesTo(layer_impl);
1674 EvictionTestLayerImpl* test_layer_impl =
1675 static_cast<EvictionTestLayerImpl*>(layer_impl);
1676 test_layer_impl->SetHasTexture(texture_->have_backing_texture());
1679 class LayerTreeHostTestEvictTextures : public LayerTreeHostTest {
1681 LayerTreeHostTestEvictTextures()
1682 : layer_(EvictionTestLayer::Create()),
1683 impl_for_evict_textures_(0),
1686 virtual void BeginTest() OVERRIDE {
1687 layer_tree_host()->SetRootLayer(layer_);
1688 layer_tree_host()->SetViewportSize(gfx::Size(10, 20));
1690 gfx::Transform identity_matrix;
1691 SetLayerPropertiesForTesting(layer_.get(),
1694 gfx::Point3F(0.f, 0.f, 0.f),
1695 gfx::PointF(0.f, 0.f),
1699 PostSetNeedsCommitToMainThread();
1702 void PostEvictTextures() {
1703 ImplThreadTaskRunner()->PostTask(
1705 base::Bind(&LayerTreeHostTestEvictTextures::EvictTexturesOnImplThread,
1706 base::Unretained(this)));
1709 void EvictTexturesOnImplThread() {
1710 DCHECK(impl_for_evict_textures_);
1711 impl_for_evict_textures_->EvictTexturesForTesting();
1714 // Commit 1: Just commit and draw normally, then post an eviction at the end
1715 // that will trigger a commit.
1716 // Commit 2: Triggered by the eviction, let it go through and then set
1718 // Commit 3: Triggered by the setNeedsCommit. In Layout(), post an eviction
1719 // task, which will be handled before the commit. Don't set needsCommit, it
1720 // should have been posted. A frame should not be drawn (note,
1721 // didCommitAndDrawFrame may be called anyway).
1722 // Commit 4: Triggered by the eviction, let it go through and then set
1724 // Commit 5: Triggered by the setNeedsCommit, post an eviction task in
1725 // Layout(), a frame should not be drawn but a commit will be posted.
1726 // Commit 6: Triggered by the eviction, post an eviction task in
1727 // Layout(), which will be a noop, letting the commit (which recreates the
1728 // textures) go through and draw a frame, then end the test.
1730 // Commits 1+2 test the eviction recovery path where eviction happens outside
1731 // of the beginFrame/commit pair.
1732 // Commits 3+4 test the eviction recovery path where eviction happens inside
1733 // the beginFrame/commit pair.
1734 // Commits 5+6 test the path where an eviction happens during the eviction
1736 virtual void DidCommit() OVERRIDE {
1737 switch (num_commits_) {
1739 EXPECT_TRUE(layer_->HaveBackingTexture());
1740 PostEvictTextures();
1743 EXPECT_TRUE(layer_->HaveBackingTexture());
1744 layer_tree_host()->SetNeedsCommit();
1749 EXPECT_TRUE(layer_->HaveBackingTexture());
1750 layer_tree_host()->SetNeedsCommit();
1755 EXPECT_TRUE(layer_->HaveBackingTexture());
1764 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1765 impl_for_evict_textures_ = impl;
1768 virtual void Layout() OVERRIDE {
1770 switch (num_commits_) {
1775 PostEvictTextures();
1778 // We couldn't check in didCommitAndDrawFrame on commit 3,
1780 EXPECT_FALSE(layer_->HaveBackingTexture());
1783 PostEvictTextures();
1786 // We couldn't check in didCommitAndDrawFrame on commit 5,
1788 EXPECT_FALSE(layer_->HaveBackingTexture());
1789 PostEvictTextures();
1797 virtual void AfterTest() OVERRIDE {}
1800 FakeContentLayerClient client_;
1801 scoped_refptr<EvictionTestLayer> layer_;
1802 LayerTreeHostImpl* impl_for_evict_textures_;
1806 MULTI_THREAD_NOIMPL_TEST_F(LayerTreeHostTestEvictTextures);
1808 class LayerTreeHostTestContinuousInvalidate : public LayerTreeHostTest {
1810 LayerTreeHostTestContinuousInvalidate()
1811 : num_commit_complete_(0), num_draw_layers_(0) {}
1813 virtual void BeginTest() OVERRIDE {
1814 layer_tree_host()->SetViewportSize(gfx::Size(10, 10));
1815 layer_tree_host()->root_layer()->SetBounds(gfx::Size(10, 10));
1817 content_layer_ = ContentLayer::Create(&client_);
1818 content_layer_->SetBounds(gfx::Size(10, 10));
1819 content_layer_->SetPosition(gfx::PointF(0.f, 0.f));
1820 content_layer_->SetIsDrawable(true);
1821 layer_tree_host()->root_layer()->AddChild(content_layer_);
1823 PostSetNeedsCommitToMainThread();
1826 virtual void DidCommitAndDrawFrame() OVERRIDE {
1827 if (num_draw_layers_ == 2)
1829 content_layer_->SetNeedsDisplay();
1832 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1833 if (num_draw_layers_ == 1)
1834 num_commit_complete_++;
1837 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
1839 if (num_draw_layers_ == 2)
1843 virtual void AfterTest() OVERRIDE {
1844 // Check that we didn't commit twice between first and second draw.
1845 EXPECT_EQ(1, num_commit_complete_);
1849 FakeContentLayerClient client_;
1850 scoped_refptr<Layer> content_layer_;
1851 int num_commit_complete_;
1852 int num_draw_layers_;
1855 MULTI_THREAD_NOIMPL_TEST_F(LayerTreeHostTestContinuousInvalidate);
1857 class LayerTreeHostTestDeferCommits : public LayerTreeHostTest {
1859 LayerTreeHostTestDeferCommits()
1860 : num_commits_deferred_(0), num_complete_commits_(0) {}
1862 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
1864 virtual void DidDeferCommit() OVERRIDE {
1865 num_commits_deferred_++;
1866 layer_tree_host()->SetDeferCommits(false);
1869 virtual void DidCommit() OVERRIDE {
1870 num_complete_commits_++;
1871 switch (num_complete_commits_) {
1873 EXPECT_EQ(0, num_commits_deferred_);
1874 layer_tree_host()->SetDeferCommits(true);
1875 PostSetNeedsCommitToMainThread();
1886 virtual void AfterTest() OVERRIDE {
1887 EXPECT_EQ(1, num_commits_deferred_);
1888 EXPECT_EQ(2, num_complete_commits_);
1892 int num_commits_deferred_;
1893 int num_complete_commits_;
1896 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestDeferCommits);
1898 class LayerTreeHostWithProxy : public LayerTreeHost {
1900 LayerTreeHostWithProxy(FakeLayerTreeHostClient* client,
1901 const LayerTreeSettings& settings,
1902 scoped_ptr<FakeProxy> proxy)
1903 : LayerTreeHost(client, NULL, settings) {
1904 proxy->SetLayerTreeHost(this);
1905 client->SetLayerTreeHost(this);
1906 InitializeForTesting(proxy.PassAs<Proxy>());
1910 TEST(LayerTreeHostTest, LimitPartialUpdates) {
1911 // When partial updates are not allowed, max updates should be 0.
1913 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
1915 scoped_ptr<FakeProxy> proxy(new FakeProxy);
1916 proxy->GetRendererCapabilities().allow_partial_texture_updates = false;
1917 proxy->SetMaxPartialTextureUpdates(5);
1919 LayerTreeSettings settings;
1920 settings.max_partial_texture_updates = 10;
1922 LayerTreeHostWithProxy host(&client, settings, proxy.Pass());
1923 host.OnCreateAndInitializeOutputSurfaceAttempted(true);
1925 EXPECT_EQ(0u, host.MaxPartialTextureUpdates());
1928 // When partial updates are allowed,
1929 // max updates should be limited by the proxy.
1931 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
1933 scoped_ptr<FakeProxy> proxy(new FakeProxy);
1934 proxy->GetRendererCapabilities().allow_partial_texture_updates = true;
1935 proxy->SetMaxPartialTextureUpdates(5);
1937 LayerTreeSettings settings;
1938 settings.max_partial_texture_updates = 10;
1940 LayerTreeHostWithProxy host(&client, settings, proxy.Pass());
1941 host.OnCreateAndInitializeOutputSurfaceAttempted(true);
1943 EXPECT_EQ(5u, host.MaxPartialTextureUpdates());
1946 // When partial updates are allowed,
1947 // max updates should also be limited by the settings.
1949 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
1951 scoped_ptr<FakeProxy> proxy(new FakeProxy);
1952 proxy->GetRendererCapabilities().allow_partial_texture_updates = true;
1953 proxy->SetMaxPartialTextureUpdates(20);
1955 LayerTreeSettings settings;
1956 settings.max_partial_texture_updates = 10;
1958 LayerTreeHostWithProxy host(&client, settings, proxy.Pass());
1959 host.OnCreateAndInitializeOutputSurfaceAttempted(true);
1961 EXPECT_EQ(10u, host.MaxPartialTextureUpdates());
1965 TEST(LayerTreeHostTest, PartialUpdatesWithGLRenderer) {
1966 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
1968 LayerTreeSettings settings;
1969 settings.max_partial_texture_updates = 4;
1970 settings.single_thread_proxy_scheduler = false;
1972 scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
1973 new TestSharedBitmapManager());
1974 scoped_ptr<LayerTreeHost> host =
1975 LayerTreeHost::CreateSingleThreaded(&client,
1977 shared_bitmap_manager.get(),
1979 base::MessageLoopProxy::current());
1980 client.SetLayerTreeHost(host.get());
1981 host->Composite(base::TimeTicks::Now());
1983 EXPECT_EQ(4u, host->settings().max_partial_texture_updates);
1986 TEST(LayerTreeHostTest, PartialUpdatesWithSoftwareRenderer) {
1987 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_SOFTWARE);
1989 LayerTreeSettings settings;
1990 settings.max_partial_texture_updates = 4;
1991 settings.single_thread_proxy_scheduler = false;
1993 scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
1994 new TestSharedBitmapManager());
1995 scoped_ptr<LayerTreeHost> host =
1996 LayerTreeHost::CreateSingleThreaded(&client,
1998 shared_bitmap_manager.get(),
2000 base::MessageLoopProxy::current());
2001 client.SetLayerTreeHost(host.get());
2002 host->Composite(base::TimeTicks::Now());
2004 EXPECT_EQ(4u, host->settings().max_partial_texture_updates);
2007 TEST(LayerTreeHostTest, PartialUpdatesWithDelegatingRendererAndGLContent) {
2008 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DELEGATED_3D);
2010 LayerTreeSettings settings;
2011 settings.max_partial_texture_updates = 4;
2012 settings.single_thread_proxy_scheduler = false;
2014 scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
2015 new TestSharedBitmapManager());
2016 scoped_ptr<LayerTreeHost> host =
2017 LayerTreeHost::CreateSingleThreaded(&client,
2019 shared_bitmap_manager.get(),
2021 base::MessageLoopProxy::current());
2022 client.SetLayerTreeHost(host.get());
2023 host->Composite(base::TimeTicks::Now());
2025 EXPECT_EQ(0u, host->MaxPartialTextureUpdates());
2028 TEST(LayerTreeHostTest,
2029 PartialUpdatesWithDelegatingRendererAndSoftwareContent) {
2030 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DELEGATED_SOFTWARE);
2032 LayerTreeSettings settings;
2033 settings.max_partial_texture_updates = 4;
2034 settings.single_thread_proxy_scheduler = false;
2036 scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
2037 new TestSharedBitmapManager());
2038 scoped_ptr<LayerTreeHost> host =
2039 LayerTreeHost::CreateSingleThreaded(&client,
2041 shared_bitmap_manager.get(),
2043 base::MessageLoopProxy::current());
2044 client.SetLayerTreeHost(host.get());
2045 host->Composite(base::TimeTicks::Now());
2047 EXPECT_EQ(0u, host->MaxPartialTextureUpdates());
2050 class LayerTreeHostTestShutdownWithOnlySomeResourcesEvicted
2051 : public LayerTreeHostTest {
2053 LayerTreeHostTestShutdownWithOnlySomeResourcesEvicted()
2054 : root_layer_(FakeContentLayer::Create(&client_)),
2055 child_layer1_(FakeContentLayer::Create(&client_)),
2056 child_layer2_(FakeContentLayer::Create(&client_)),
2059 virtual void BeginTest() OVERRIDE {
2060 layer_tree_host()->SetViewportSize(gfx::Size(100, 100));
2061 root_layer_->SetBounds(gfx::Size(100, 100));
2062 child_layer1_->SetBounds(gfx::Size(100, 100));
2063 child_layer2_->SetBounds(gfx::Size(100, 100));
2064 root_layer_->AddChild(child_layer1_);
2065 root_layer_->AddChild(child_layer2_);
2066 layer_tree_host()->SetRootLayer(root_layer_);
2067 PostSetNeedsCommitToMainThread();
2070 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl* host_impl,
2071 bool visible) OVERRIDE {
2073 // One backing should remain unevicted.
2075 100u * 100u * 4u * 1u,
2076 layer_tree_host()->contents_texture_manager()->MemoryUseBytes());
2079 0u, layer_tree_host()->contents_texture_manager()->MemoryUseBytes());
2082 // Make sure that contents textures are marked as having been
2084 EXPECT_TRUE(host_impl->active_tree()->ContentsTexturesPurged());
2085 // End the test in this state.
2089 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
2091 switch (num_commits_) {
2093 // All three backings should have memory.
2095 100u * 100u * 4u * 3u,
2096 layer_tree_host()->contents_texture_manager()->MemoryUseBytes());
2097 // Set a new policy that will kick out 1 of the 3 resources.
2098 // Because a resource was evicted, a commit will be kicked off.
2099 host_impl->SetMemoryPolicy(
2100 ManagedMemoryPolicy(100 * 100 * 4 * 2,
2101 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
2105 // Only two backings should have memory.
2107 100u * 100u * 4u * 2u,
2108 layer_tree_host()->contents_texture_manager()->MemoryUseBytes());
2109 // Become backgrounded, which will cause 1 more resource to be
2111 PostSetVisibleToMainThread(false);
2114 // No further commits should happen because this is not visible
2121 virtual void AfterTest() OVERRIDE {}
2124 FakeContentLayerClient client_;
2125 scoped_refptr<FakeContentLayer> root_layer_;
2126 scoped_refptr<FakeContentLayer> child_layer1_;
2127 scoped_refptr<FakeContentLayer> child_layer2_;
2131 SINGLE_AND_MULTI_THREAD_NOIMPL_TEST_F(
2132 LayerTreeHostTestShutdownWithOnlySomeResourcesEvicted);
2134 class LayerTreeHostTestLCDNotification : public LayerTreeHostTest {
2136 class NotificationClient : public ContentLayerClient {
2138 NotificationClient()
2139 : layer_(0), paint_count_(0), lcd_notification_count_(0) {}
2141 void set_layer(Layer* layer) { layer_ = layer; }
2142 int paint_count() const { return paint_count_; }
2143 int lcd_notification_count() const { return lcd_notification_count_; }
2145 virtual void PaintContents(
2147 const gfx::Rect& clip,
2148 ContentLayerClient::GraphicsContextStatus gc_status) OVERRIDE {
2151 virtual void DidChangeLayerCanUseLCDText() OVERRIDE {
2152 ++lcd_notification_count_;
2153 layer_->SetNeedsDisplay();
2155 virtual bool FillsBoundsCompletely() const OVERRIDE { return false; }
2160 int lcd_notification_count_;
2163 virtual void SetupTree() OVERRIDE {
2164 scoped_refptr<Layer> root_layer;
2165 if (layer_tree_host()->settings().impl_side_painting)
2166 root_layer = PictureLayer::Create(&client_);
2168 root_layer = ContentLayer::Create(&client_);
2169 root_layer->SetIsDrawable(true);
2170 root_layer->SetBounds(gfx::Size(1, 1));
2172 layer_tree_host()->SetRootLayer(root_layer);
2173 client_.set_layer(root_layer.get());
2175 // The expecations are based on the assumption that the default
2176 // LCD settings are:
2177 EXPECT_TRUE(layer_tree_host()->settings().can_use_lcd_text);
2178 EXPECT_FALSE(root_layer->can_use_lcd_text());
2180 LayerTreeHostTest::SetupTree();
2183 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2184 virtual void AfterTest() OVERRIDE {}
2186 virtual void DidCommit() OVERRIDE {
2187 switch (layer_tree_host()->source_frame_number()) {
2189 // The first update consists of one LCD notification and one paint.
2190 EXPECT_EQ(1, client_.lcd_notification_count());
2191 EXPECT_EQ(1, client_.paint_count());
2192 // LCD text must have been enabled on the layer.
2193 EXPECT_TRUE(layer_tree_host()->root_layer()->can_use_lcd_text());
2194 PostSetNeedsCommitToMainThread();
2197 // Since nothing changed on layer, there should be no notification
2198 // or paint on the second update.
2199 EXPECT_EQ(1, client_.lcd_notification_count());
2200 EXPECT_EQ(1, client_.paint_count());
2201 // LCD text must not have changed.
2202 EXPECT_TRUE(layer_tree_host()->root_layer()->can_use_lcd_text());
2203 // Change layer opacity that should trigger lcd notification.
2204 layer_tree_host()->root_layer()->SetOpacity(.5f);
2205 // No need to request a commit - setting opacity will do it.
2208 // Verify that there is no extra commit due to layer invalidation.
2209 EXPECT_EQ(3, layer_tree_host()->source_frame_number());
2210 // LCD notification count should have incremented due to
2211 // change in layer opacity.
2212 EXPECT_EQ(2, client_.lcd_notification_count());
2213 // Paint count should be incremented due to invalidation.
2214 EXPECT_EQ(2, client_.paint_count());
2215 // LCD text must have been disabled on the layer due to opacity.
2216 EXPECT_FALSE(layer_tree_host()->root_layer()->can_use_lcd_text());
2223 NotificationClient client_;
2226 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestLCDNotification);
2228 // Verify that the BeginFrame notification is used to initiate rendering.
2229 class LayerTreeHostTestBeginFrameNotification : public LayerTreeHostTest {
2231 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
2232 settings->begin_frame_scheduling_enabled = true;
2235 virtual void BeginTest() OVERRIDE {
2236 // This will trigger a SetNeedsBeginFrame which will trigger a
2238 PostSetNeedsCommitToMainThread();
2241 virtual DrawResult PrepareToDrawOnThread(
2242 LayerTreeHostImpl* host_impl,
2243 LayerTreeHostImpl::FrameData* frame,
2244 DrawResult draw_result) OVERRIDE {
2246 return DRAW_SUCCESS;
2249 virtual void AfterTest() OVERRIDE {}
2252 base::TimeTicks frame_time_;
2255 MULTI_THREAD_TEST_F(LayerTreeHostTestBeginFrameNotification);
2257 class LayerTreeHostTestBeginFrameNotificationShutdownWhileEnabled
2258 : public LayerTreeHostTest {
2260 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
2261 settings->begin_frame_scheduling_enabled = true;
2262 settings->using_synchronous_renderer_compositor = true;
2265 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2267 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
2268 // The BeginFrame notification is turned off now but will get enabled
2269 // once we return. End test while it's enabled.
2270 ImplThreadTaskRunner()->PostTask(
2272 base::Bind(&LayerTreeHostTestBeginFrameNotification::EndTest,
2273 base::Unretained(this)));
2276 virtual void AfterTest() OVERRIDE {}
2279 MULTI_THREAD_TEST_F(
2280 LayerTreeHostTestBeginFrameNotificationShutdownWhileEnabled);
2282 class LayerTreeHostTestAbortedCommitDoesntStall : public LayerTreeHostTest {
2284 LayerTreeHostTestAbortedCommitDoesntStall()
2285 : commit_count_(0), commit_abort_count_(0), commit_complete_count_(0) {}
2287 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
2288 settings->begin_frame_scheduling_enabled = true;
2291 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2293 virtual void DidCommit() OVERRIDE {
2295 if (commit_count_ == 4) {
2296 // After two aborted commits, request a real commit now to make sure a
2297 // real commit following an aborted commit will still complete and
2298 // end the test even when the Impl thread is idle.
2299 layer_tree_host()->SetNeedsCommit();
2303 virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl,
2304 bool did_handle) OVERRIDE {
2305 commit_abort_count_++;
2306 // Initiate another abortable commit.
2307 host_impl->SetNeedsCommit();
2310 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
2311 commit_complete_count_++;
2312 if (commit_complete_count_ == 1) {
2313 // Initiate an abortable commit after the first commit.
2314 host_impl->SetNeedsCommit();
2320 virtual void AfterTest() OVERRIDE {
2321 EXPECT_EQ(commit_count_, 5);
2322 EXPECT_EQ(commit_abort_count_, 3);
2323 EXPECT_EQ(commit_complete_count_, 2);
2327 int commit_abort_count_;
2328 int commit_complete_count_;
2331 class LayerTreeHostTestAbortedCommitDoesntStallSynchronousCompositor
2332 : public LayerTreeHostTestAbortedCommitDoesntStall {
2333 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
2334 LayerTreeHostTestAbortedCommitDoesntStall::InitializeSettings(settings);
2335 settings->using_synchronous_renderer_compositor = true;
2339 MULTI_THREAD_TEST_F(
2340 LayerTreeHostTestAbortedCommitDoesntStallSynchronousCompositor);
2342 class LayerTreeHostTestAbortedCommitDoesntStallDisabledVsync
2343 : public LayerTreeHostTestAbortedCommitDoesntStall {
2344 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
2345 LayerTreeHostTestAbortedCommitDoesntStall::InitializeSettings(settings);
2346 settings->throttle_frame_production = false;
2350 MULTI_THREAD_TEST_F(LayerTreeHostTestAbortedCommitDoesntStallDisabledVsync);
2352 class LayerTreeHostTestUninvertibleTransformDoesNotBlockActivation
2353 : public LayerTreeHostTest {
2355 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
2356 settings->impl_side_painting = true;
2359 virtual void SetupTree() OVERRIDE {
2360 LayerTreeHostTest::SetupTree();
2362 scoped_refptr<Layer> layer = PictureLayer::Create(&client_);
2363 layer->SetTransform(gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
2364 layer->SetBounds(gfx::Size(10, 10));
2365 layer_tree_host()->root_layer()->AddChild(layer);
2368 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2370 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
2374 virtual void AfterTest() OVERRIDE {}
2376 FakeContentLayerClient client_;
2379 MULTI_THREAD_TEST_F(
2380 LayerTreeHostTestUninvertibleTransformDoesNotBlockActivation);
2382 class LayerTreeHostTestChangeLayerPropertiesInPaintContents
2383 : public LayerTreeHostTest {
2385 class SetBoundsClient : public ContentLayerClient {
2387 SetBoundsClient() : layer_(0) {}
2389 void set_layer(Layer* layer) { layer_ = layer; }
2391 virtual void PaintContents(
2393 const gfx::Rect& clip,
2394 ContentLayerClient::GraphicsContextStatus gc_status) OVERRIDE {
2395 layer_->SetBounds(gfx::Size(2, 2));
2398 virtual void DidChangeLayerCanUseLCDText() OVERRIDE {}
2400 virtual bool FillsBoundsCompletely() const OVERRIDE { return false; }
2406 LayerTreeHostTestChangeLayerPropertiesInPaintContents() : num_commits_(0) {}
2408 virtual void SetupTree() OVERRIDE {
2409 scoped_refptr<ContentLayer> root_layer = ContentLayer::Create(&client_);
2410 root_layer->SetIsDrawable(true);
2411 root_layer->SetBounds(gfx::Size(1, 1));
2413 layer_tree_host()->SetRootLayer(root_layer);
2414 client_.set_layer(root_layer.get());
2416 LayerTreeHostTest::SetupTree();
2419 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2420 virtual void AfterTest() OVERRIDE {}
2422 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
2424 if (num_commits_ == 1) {
2425 LayerImpl* root_layer = host_impl->active_tree()->root_layer();
2426 EXPECT_SIZE_EQ(gfx::Size(1, 1), root_layer->bounds());
2428 LayerImpl* root_layer = host_impl->active_tree()->root_layer();
2429 EXPECT_SIZE_EQ(gfx::Size(2, 2), root_layer->bounds());
2435 SetBoundsClient client_;
2439 SINGLE_THREAD_TEST_F(LayerTreeHostTestChangeLayerPropertiesInPaintContents);
2441 class MockIOSurfaceWebGraphicsContext3D : public TestWebGraphicsContext3D {
2443 MockIOSurfaceWebGraphicsContext3D() {
2444 test_capabilities_.gpu.iosurface = true;
2445 test_capabilities_.gpu.texture_rectangle = true;
2448 virtual GLuint createTexture() OVERRIDE {
2451 MOCK_METHOD1(activeTexture, void(GLenum texture));
2452 MOCK_METHOD2(bindTexture, void(GLenum target,
2453 GLuint texture_id));
2454 MOCK_METHOD3(texParameteri, void(GLenum target,
2457 MOCK_METHOD5(texImageIOSurface2DCHROMIUM, void(GLenum target,
2462 MOCK_METHOD4(drawElements, void(GLenum mode,
2466 MOCK_METHOD1(deleteTexture, void(GLenum texture));
2467 MOCK_METHOD2(produceTextureCHROMIUM,
2468 void(GLenum target, const GLbyte* mailbox));
2471 class LayerTreeHostTestIOSurfaceDrawing : public LayerTreeHostTest {
2473 virtual scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface(bool fallback)
2475 scoped_ptr<MockIOSurfaceWebGraphicsContext3D> mock_context_owned(
2476 new MockIOSurfaceWebGraphicsContext3D);
2477 mock_context_ = mock_context_owned.get();
2479 if (delegating_renderer()) {
2480 return FakeOutputSurface::CreateDelegating3d(
2481 mock_context_owned.PassAs<TestWebGraphicsContext3D>());
2483 return FakeOutputSurface::Create3d(
2484 mock_context_owned.PassAs<TestWebGraphicsContext3D>());
2488 virtual void SetupTree() OVERRIDE {
2489 LayerTreeHostTest::SetupTree();
2491 layer_tree_host()->root_layer()->SetIsDrawable(false);
2494 io_surface_size_ = gfx::Size(6, 7);
2496 scoped_refptr<IOSurfaceLayer> io_surface_layer = IOSurfaceLayer::Create();
2497 io_surface_layer->SetBounds(gfx::Size(10, 10));
2498 io_surface_layer->SetIsDrawable(true);
2499 io_surface_layer->SetContentsOpaque(true);
2500 io_surface_layer->SetIOSurfaceProperties(io_surface_id_, io_surface_size_);
2501 layer_tree_host()->root_layer()->AddChild(io_surface_layer);
2504 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2506 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
2507 EXPECT_EQ(0u, host_impl->resource_provider()->num_resources());
2508 // In WillDraw, the IOSurfaceLayer sets up the io surface texture.
2510 EXPECT_CALL(*mock_context_, activeTexture(_)).Times(0);
2511 EXPECT_CALL(*mock_context_, bindTexture(GL_TEXTURE_RECTANGLE_ARB, 1))
2513 EXPECT_CALL(*mock_context_,
2515 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR))
2517 EXPECT_CALL(*mock_context_,
2519 GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR))
2521 EXPECT_CALL(*mock_context_,
2522 texParameteri(GL_TEXTURE_RECTANGLE_ARB,
2523 GL_TEXTURE_POOL_CHROMIUM,
2524 GL_TEXTURE_POOL_UNMANAGED_CHROMIUM)).Times(1);
2525 EXPECT_CALL(*mock_context_,
2526 texParameteri(GL_TEXTURE_RECTANGLE_ARB,
2528 GL_CLAMP_TO_EDGE)).Times(1);
2529 EXPECT_CALL(*mock_context_,
2530 texParameteri(GL_TEXTURE_RECTANGLE_ARB,
2532 GL_CLAMP_TO_EDGE)).Times(1);
2534 EXPECT_CALL(*mock_context_,
2535 texImageIOSurface2DCHROMIUM(GL_TEXTURE_RECTANGLE_ARB,
2536 io_surface_size_.width(),
2537 io_surface_size_.height(),
2541 EXPECT_CALL(*mock_context_, bindTexture(_, 0)).Times(AnyNumber());
2544 virtual DrawResult PrepareToDrawOnThread(
2545 LayerTreeHostImpl* host_impl,
2546 LayerTreeHostImpl::FrameData* frame,
2547 DrawResult draw_result) OVERRIDE {
2548 Mock::VerifyAndClearExpectations(&mock_context_);
2549 ResourceProvider* resource_provider = host_impl->resource_provider();
2550 EXPECT_EQ(1u, resource_provider->num_resources());
2551 CHECK_EQ(1u, frame->render_passes.size());
2552 CHECK_LE(1u, frame->render_passes[0]->quad_list.size());
2553 const DrawQuad* quad = frame->render_passes[0]->quad_list.front();
2554 CHECK_EQ(DrawQuad::IO_SURFACE_CONTENT, quad->material);
2555 const IOSurfaceDrawQuad* io_surface_draw_quad =
2556 IOSurfaceDrawQuad::MaterialCast(quad);
2557 EXPECT_SIZE_EQ(io_surface_size_, io_surface_draw_quad->io_surface_size);
2558 EXPECT_NE(0u, io_surface_draw_quad->io_surface_resource_id);
2559 EXPECT_EQ(static_cast<GLenum>(GL_TEXTURE_RECTANGLE_ARB),
2560 resource_provider->TargetForTesting(
2561 io_surface_draw_quad->io_surface_resource_id));
2563 EXPECT_CALL(*mock_context_, bindTexture(GL_TEXTURE_RECTANGLE_ARB, 1))
2565 if (delegating_renderer()) {
2566 // The io surface layer's resource should be sent to the parent.
2567 EXPECT_CALL(*mock_context_,
2568 produceTextureCHROMIUM(GL_TEXTURE_RECTANGLE_ARB, _)).Times(1);
2570 // The io surface layer's texture is drawn.
2571 EXPECT_CALL(*mock_context_, activeTexture(GL_TEXTURE0)).Times(AtLeast(1));
2572 EXPECT_CALL(*mock_context_, drawElements(GL_TRIANGLES, 6, _, _))
2579 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
2580 Mock::VerifyAndClearExpectations(&mock_context_);
2582 EXPECT_CALL(*mock_context_, deleteTexture(1)).Times(AtLeast(1));
2586 virtual void AfterTest() OVERRIDE {}
2589 MockIOSurfaceWebGraphicsContext3D* mock_context_;
2590 gfx::Size io_surface_size_;
2593 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestIOSurfaceDrawing);
2595 class LayerTreeHostTestNumFramesPending : public LayerTreeHostTest {
2597 virtual void BeginTest() OVERRIDE {
2599 PostSetNeedsCommitToMainThread();
2602 // Round 1: commit + draw
2603 // Round 2: commit only (no draw/swap)
2604 // Round 3: draw only (no commit)
2606 virtual void DidCommit() OVERRIDE {
2607 int commit = layer_tree_host()->source_frame_number();
2611 EXPECT_EQ(1, frame_);
2612 layer_tree_host()->SetNeedsRedraw();
2617 virtual void DidCompleteSwapBuffers() OVERRIDE {
2618 int commit = layer_tree_host()->source_frame_number();
2623 EXPECT_EQ(1, commit);
2624 layer_tree_host()->SetNeedsCommit();
2628 EXPECT_EQ(2, commit);
2634 virtual void AfterTest() OVERRIDE {}
2640 // Flaky on all platforms: http://crbug.com/327498
2641 TEST_F(LayerTreeHostTestNumFramesPending, DISABLED_DelegatingRenderer) {
2642 RunTest(true, true, true);
2645 TEST_F(LayerTreeHostTestNumFramesPending, DISABLED_GLRenderer) {
2646 RunTest(true, false, true);
2649 class LayerTreeHostTestDeferredInitialize : public LayerTreeHostTest {
2651 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
2652 // PictureLayer can only be used with impl side painting enabled.
2653 settings->impl_side_painting = true;
2656 virtual void SetupTree() OVERRIDE {
2657 layer_ = FakePictureLayer::Create(&client_);
2658 // Force commits to not be aborted so new frames get drawn, otherwise
2659 // the renderer gets deferred initialized but nothing new needs drawing.
2660 layer_->set_always_update_resources(true);
2661 layer_tree_host()->SetRootLayer(layer_);
2662 LayerTreeHostTest::SetupTree();
2665 virtual void BeginTest() OVERRIDE {
2666 did_initialize_gl_ = false;
2667 did_release_gl_ = false;
2668 last_source_frame_number_drawn_ = -1; // Never drawn.
2669 PostSetNeedsCommitToMainThread();
2672 virtual scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface(bool fallback)
2674 scoped_ptr<TestWebGraphicsContext3D> context3d(
2675 TestWebGraphicsContext3D::Create());
2677 return FakeOutputSurface::CreateDeferredGL(
2678 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
2679 delegating_renderer());
2682 virtual void DrawLayersOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
2683 ASSERT_TRUE(host_impl->RootLayer());
2684 FakePictureLayerImpl* layer_impl =
2685 static_cast<FakePictureLayerImpl*>(host_impl->RootLayer());
2687 // The same frame can be draw multiple times if new visible tiles are
2688 // rasterized. But we want to make sure we only post DeferredInitialize
2689 // and ReleaseGL once, so early out if the same frame is drawn again.
2690 if (last_source_frame_number_drawn_ ==
2691 host_impl->active_tree()->source_frame_number())
2694 last_source_frame_number_drawn_ =
2695 host_impl->active_tree()->source_frame_number();
2697 if (!did_initialize_gl_) {
2698 EXPECT_LE(1u, layer_impl->append_quads_count());
2699 ImplThreadTaskRunner()->PostTask(
2702 &LayerTreeHostTestDeferredInitialize::DeferredInitializeAndRedraw,
2703 base::Unretained(this),
2704 base::Unretained(host_impl)));
2705 } else if (did_initialize_gl_ && !did_release_gl_) {
2706 EXPECT_LE(2u, layer_impl->append_quads_count());
2707 ImplThreadTaskRunner()->PostTask(
2709 base::Bind(&LayerTreeHostTestDeferredInitialize::ReleaseGLAndRedraw,
2710 base::Unretained(this),
2711 base::Unretained(host_impl)));
2712 } else if (did_initialize_gl_ && did_release_gl_) {
2713 EXPECT_LE(3u, layer_impl->append_quads_count());
2718 void DeferredInitializeAndRedraw(LayerTreeHostImpl* host_impl) {
2719 EXPECT_FALSE(did_initialize_gl_);
2720 // SetAndInitializeContext3D calls SetNeedsCommit.
2721 FakeOutputSurface* fake_output_surface =
2722 static_cast<FakeOutputSurface*>(host_impl->output_surface());
2723 scoped_refptr<TestContextProvider> context_provider =
2724 TestContextProvider::Create(); // Not bound to thread.
2726 fake_output_surface->InitializeAndSetContext3d(context_provider));
2727 did_initialize_gl_ = true;
2730 void ReleaseGLAndRedraw(LayerTreeHostImpl* host_impl) {
2731 EXPECT_TRUE(did_initialize_gl_);
2732 EXPECT_FALSE(did_release_gl_);
2733 // ReleaseGL calls SetNeedsCommit.
2734 static_cast<FakeOutputSurface*>(host_impl->output_surface())->ReleaseGL();
2735 did_release_gl_ = true;
2738 virtual void SwapBuffersOnThread(LayerTreeHostImpl* host_impl,
2739 bool result) OVERRIDE {
2740 ASSERT_TRUE(result);
2741 DelegatedFrameData* delegated_frame_data =
2742 output_surface()->last_sent_frame().delegated_frame_data.get();
2743 if (!delegated_frame_data)
2746 // Return all resources immediately.
2747 TransferableResourceArray resources_to_return =
2748 output_surface()->resources_held_by_parent();
2750 CompositorFrameAck ack;
2751 for (size_t i = 0; i < resources_to_return.size(); ++i)
2752 output_surface()->ReturnResource(resources_to_return[i].id, &ack);
2753 host_impl->ReclaimResources(&ack);
2756 virtual void AfterTest() OVERRIDE {
2757 EXPECT_TRUE(did_initialize_gl_);
2758 EXPECT_TRUE(did_release_gl_);
2762 FakeContentLayerClient client_;
2763 scoped_refptr<FakePictureLayer> layer_;
2764 bool did_initialize_gl_;
2765 bool did_release_gl_;
2766 int last_source_frame_number_drawn_;
2769 MULTI_THREAD_TEST_F(LayerTreeHostTestDeferredInitialize);
2771 class LayerTreeHostTestDeferredInitializeWithGpuRasterization
2772 : public LayerTreeHostTestDeferredInitialize {
2773 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
2774 // PictureLayer can only be used with impl side painting enabled.
2775 settings->impl_side_painting = true;
2776 settings->gpu_rasterization_enabled = true;
2777 settings->gpu_rasterization_forced = true;
2781 MULTI_THREAD_TEST_F(LayerTreeHostTestDeferredInitializeWithGpuRasterization);
2783 // Test for UI Resource management.
2784 class LayerTreeHostTestUIResource : public LayerTreeHostTest {
2786 LayerTreeHostTestUIResource() : num_ui_resources_(0) {}
2788 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
2789 settings->texture_id_allocation_chunk_size = 1;
2792 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
2794 virtual void DidCommit() OVERRIDE {
2795 int frame = layer_tree_host()->source_frame_number();
2800 PostSetNeedsCommitToMainThread();
2803 // Usually ScopedUIResource are deleted from the manager in their
2804 // destructor. Here we just want to test that a direct call to
2805 // DeleteUIResource works.
2806 layer_tree_host()->DeleteUIResource(ui_resources_[0]->id());
2807 PostSetNeedsCommitToMainThread();
2810 // DeleteUIResource can be called with an invalid id.
2811 layer_tree_host()->DeleteUIResource(ui_resources_[0]->id());
2812 PostSetNeedsCommitToMainThread();
2817 PostSetNeedsCommitToMainThread();
2826 void PerformTest(LayerTreeHostImpl* impl) {
2827 TestWebGraphicsContext3D* context = TestContext();
2829 int frame = impl->active_tree()->source_frame_number();
2832 ASSERT_EQ(0u, context->NumTextures());
2835 // Created two textures.
2836 ASSERT_EQ(2u, context->NumTextures());
2839 // One texture left after one deletion.
2840 ASSERT_EQ(1u, context->NumTextures());
2843 // Resource manager state should not change when delete is called on an
2845 ASSERT_EQ(1u, context->NumTextures());
2848 // Creation after deletion: two more creates should total up to
2850 ASSERT_EQ(3u, context->NumTextures());
2855 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
2856 if (!layer_tree_host()->settings().impl_side_painting)
2860 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
2861 if (layer_tree_host()->settings().impl_side_painting)
2865 virtual void AfterTest() OVERRIDE {}
2868 // Must clear all resources before exiting.
2869 void ClearResources() {
2870 for (int i = 0; i < num_ui_resources_; i++)
2871 ui_resources_[i].reset();
2874 void CreateResource() {
2875 ui_resources_[num_ui_resources_++] =
2876 FakeScopedUIResource::Create(layer_tree_host());
2879 scoped_ptr<FakeScopedUIResource> ui_resources_[5];
2880 int num_ui_resources_;
2883 MULTI_THREAD_TEST_F(LayerTreeHostTestUIResource);
2885 class PushPropertiesCountingLayerImpl : public LayerImpl {
2887 static scoped_ptr<PushPropertiesCountingLayerImpl> Create(
2888 LayerTreeImpl* tree_impl, int id) {
2889 return make_scoped_ptr(new PushPropertiesCountingLayerImpl(tree_impl, id));
2892 virtual ~PushPropertiesCountingLayerImpl() {}
2894 virtual void PushPropertiesTo(LayerImpl* layer) OVERRIDE {
2895 LayerImpl::PushPropertiesTo(layer);
2896 push_properties_count_++;
2897 // Push state to the active tree because we can only access it from there.
2898 static_cast<PushPropertiesCountingLayerImpl*>(
2899 layer)->push_properties_count_ = push_properties_count_;
2902 virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl)
2904 return PushPropertiesCountingLayerImpl::Create(tree_impl, id()).
2905 PassAs<LayerImpl>();
2908 size_t push_properties_count() const { return push_properties_count_; }
2909 void reset_push_properties_count() { push_properties_count_ = 0; }
2912 size_t push_properties_count_;
2914 PushPropertiesCountingLayerImpl(LayerTreeImpl* tree_impl, int id)
2915 : LayerImpl(tree_impl, id),
2916 push_properties_count_(0) {
2917 SetBounds(gfx::Size(1, 1));
2921 class PushPropertiesCountingLayer : public Layer {
2923 static scoped_refptr<PushPropertiesCountingLayer> Create() {
2924 return new PushPropertiesCountingLayer();
2927 virtual void PushPropertiesTo(LayerImpl* layer) OVERRIDE {
2928 Layer::PushPropertiesTo(layer);
2929 push_properties_count_++;
2930 if (persist_needs_push_properties_)
2931 needs_push_properties_ = true;
2934 virtual scoped_ptr<LayerImpl> CreateLayerImpl(LayerTreeImpl* tree_impl)
2936 return PushPropertiesCountingLayerImpl::Create(tree_impl, id()).
2937 PassAs<LayerImpl>();
2940 void SetDrawsContent(bool draws_content) { SetIsDrawable(draws_content); }
2942 size_t push_properties_count() const { return push_properties_count_; }
2943 void reset_push_properties_count() { push_properties_count_ = 0; }
2945 void set_persist_needs_push_properties(bool persist) {
2946 persist_needs_push_properties_ = persist;
2950 PushPropertiesCountingLayer()
2951 : push_properties_count_(0), persist_needs_push_properties_(false) {
2952 SetBounds(gfx::Size(1, 1));
2954 virtual ~PushPropertiesCountingLayer() {}
2956 size_t push_properties_count_;
2957 bool persist_needs_push_properties_;
2960 class LayerTreeHostTestLayersPushProperties : public LayerTreeHostTest {
2962 virtual void BeginTest() OVERRIDE {
2964 expected_push_properties_root_ = 0;
2965 expected_push_properties_child_ = 0;
2966 expected_push_properties_grandchild_ = 0;
2967 expected_push_properties_child2_ = 0;
2968 expected_push_properties_other_root_ = 0;
2969 expected_push_properties_leaf_layer_ = 0;
2970 PostSetNeedsCommitToMainThread();
2973 virtual void SetupTree() OVERRIDE {
2974 root_ = PushPropertiesCountingLayer::Create();
2975 child_ = PushPropertiesCountingLayer::Create();
2976 child2_ = PushPropertiesCountingLayer::Create();
2977 grandchild_ = PushPropertiesCountingLayer::Create();
2978 leaf_always_pushing_layer_ = PushPropertiesCountingLayer::Create();
2979 leaf_always_pushing_layer_->set_persist_needs_push_properties(true);
2981 root_->AddChild(child_);
2982 root_->AddChild(child2_);
2983 child_->AddChild(grandchild_);
2984 child2_->AddChild(leaf_always_pushing_layer_);
2986 other_root_ = PushPropertiesCountingLayer::Create();
2988 // Don't set the root layer here.
2989 LayerTreeHostTest::SetupTree();
2992 virtual void DidCommitAndDrawFrame() OVERRIDE {
2995 EXPECT_EQ(expected_push_properties_root_, root_->push_properties_count());
2996 EXPECT_EQ(expected_push_properties_child_, child_->push_properties_count());
2997 EXPECT_EQ(expected_push_properties_grandchild_,
2998 grandchild_->push_properties_count());
2999 EXPECT_EQ(expected_push_properties_child2_,
3000 child2_->push_properties_count());
3001 EXPECT_EQ(expected_push_properties_other_root_,
3002 other_root_->push_properties_count());
3003 EXPECT_EQ(expected_push_properties_leaf_layer_,
3004 leaf_always_pushing_layer_->push_properties_count());
3006 // The scrollbar layer always needs to be pushed.
3007 if (root_->layer_tree_host()) {
3008 EXPECT_TRUE(root_->descendant_needs_push_properties());
3009 EXPECT_FALSE(root_->needs_push_properties());
3011 if (child2_->layer_tree_host()) {
3012 EXPECT_TRUE(child2_->descendant_needs_push_properties());
3013 EXPECT_FALSE(child2_->needs_push_properties());
3015 if (leaf_always_pushing_layer_->layer_tree_host()) {
3017 leaf_always_pushing_layer_->descendant_needs_push_properties());
3018 EXPECT_TRUE(leaf_always_pushing_layer_->needs_push_properties());
3021 // child_ and grandchild_ don't persist their need to push properties.
3022 if (child_->layer_tree_host()) {
3023 EXPECT_FALSE(child_->descendant_needs_push_properties());
3024 EXPECT_FALSE(child_->needs_push_properties());
3026 if (grandchild_->layer_tree_host()) {
3027 EXPECT_FALSE(grandchild_->descendant_needs_push_properties());
3028 EXPECT_FALSE(grandchild_->needs_push_properties());
3031 if (other_root_->layer_tree_host()) {
3032 EXPECT_FALSE(other_root_->descendant_needs_push_properties());
3033 EXPECT_FALSE(other_root_->needs_push_properties());
3036 switch (num_commits_) {
3038 layer_tree_host()->SetRootLayer(root_);
3039 // Layers added to the tree get committed.
3040 ++expected_push_properties_root_;
3041 ++expected_push_properties_child_;
3042 ++expected_push_properties_grandchild_;
3043 ++expected_push_properties_child2_;
3046 layer_tree_host()->SetNeedsCommit();
3047 // No layers need commit.
3050 layer_tree_host()->SetRootLayer(other_root_);
3051 // Layers added to the tree get committed.
3052 ++expected_push_properties_other_root_;
3055 layer_tree_host()->SetRootLayer(root_);
3056 // Layers added to the tree get committed.
3057 ++expected_push_properties_root_;
3058 ++expected_push_properties_child_;
3059 ++expected_push_properties_grandchild_;
3060 ++expected_push_properties_child2_;
3063 layer_tree_host()->SetNeedsCommit();
3064 // No layers need commit.
3067 child_->RemoveFromParent();
3068 // No layers need commit.
3071 root_->AddChild(child_);
3072 // Layers added to the tree get committed.
3073 ++expected_push_properties_child_;
3074 ++expected_push_properties_grandchild_;
3077 grandchild_->RemoveFromParent();
3078 // No layers need commit.
3081 child_->AddChild(grandchild_);
3082 // Layers added to the tree get committed.
3083 ++expected_push_properties_grandchild_;
3086 layer_tree_host()->SetViewportSize(gfx::Size(20, 20));
3087 // No layers need commit.
3090 layer_tree_host()->SetPageScaleFactorAndLimits(1.f, 0.8f, 1.1f);
3091 // No layers need commit.
3094 child_->SetPosition(gfx::Point(1, 1));
3095 // The modified layer needs commit
3096 ++expected_push_properties_child_;
3099 child2_->SetPosition(gfx::Point(1, 1));
3100 // The modified layer needs commit
3101 ++expected_push_properties_child2_;
3104 child_->RemoveFromParent();
3105 root_->AddChild(child_);
3106 // Layers added to the tree get committed.
3107 ++expected_push_properties_child_;
3108 ++expected_push_properties_grandchild_;
3111 grandchild_->SetPosition(gfx::Point(1, 1));
3112 // The modified layer needs commit
3113 ++expected_push_properties_grandchild_;
3116 // SetNeedsDisplay does not always set needs commit (so call it
3117 // explicitly), but is a property change.
3118 child_->SetNeedsDisplay();
3119 ++expected_push_properties_child_;
3120 layer_tree_host()->SetNeedsCommit();
3127 // The leaf layer always pushes.
3128 if (leaf_always_pushing_layer_->layer_tree_host())
3129 ++expected_push_properties_leaf_layer_;
3132 virtual void AfterTest() OVERRIDE {}
3135 FakeContentLayerClient client_;
3136 scoped_refptr<PushPropertiesCountingLayer> root_;
3137 scoped_refptr<PushPropertiesCountingLayer> child_;
3138 scoped_refptr<PushPropertiesCountingLayer> child2_;
3139 scoped_refptr<PushPropertiesCountingLayer> grandchild_;
3140 scoped_refptr<PushPropertiesCountingLayer> other_root_;
3141 scoped_refptr<PushPropertiesCountingLayer> leaf_always_pushing_layer_;
3142 size_t expected_push_properties_root_;
3143 size_t expected_push_properties_child_;
3144 size_t expected_push_properties_child2_;
3145 size_t expected_push_properties_grandchild_;
3146 size_t expected_push_properties_other_root_;
3147 size_t expected_push_properties_leaf_layer_;
3150 MULTI_THREAD_TEST_F(LayerTreeHostTestLayersPushProperties);
3152 class LayerTreeHostTestImplLayersPushProperties
3153 : public LayerTreeHostTestLayersPushProperties {
3155 virtual void BeginTest() OVERRIDE {
3156 expected_push_properties_root_impl_ = 0;
3157 expected_push_properties_child_impl_ = 0;
3158 expected_push_properties_grandchild_impl_ = 0;
3159 expected_push_properties_child2_impl_ = 0;
3160 expected_push_properties_grandchild2_impl_ = 0;
3161 LayerTreeHostTestLayersPushProperties::BeginTest();
3164 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
3165 // These commits are in response to the changes made in
3166 // LayerTreeHostTestLayersPushProperties::DidCommitAndDrawFrame()
3167 switch (num_commits_) {
3169 // Tree hasn't been setup yet don't bother to check anything.
3172 // Root gets set up, Everyone is initialized.
3173 ++expected_push_properties_root_impl_;
3174 ++expected_push_properties_child_impl_;
3175 ++expected_push_properties_grandchild_impl_;
3176 ++expected_push_properties_child2_impl_;
3177 ++expected_push_properties_grandchild2_impl_;
3180 // Tree doesn't change but the one leaf that always pushes is pushed.
3181 ++expected_push_properties_grandchild2_impl_;
3184 // Root is swapped here.
3185 // Clear the expected push properties the tree will be rebuilt.
3186 expected_push_properties_root_impl_ = 0;
3187 expected_push_properties_child_impl_ = 0;
3188 expected_push_properties_grandchild_impl_ = 0;
3189 expected_push_properties_child2_impl_ = 0;
3190 expected_push_properties_grandchild2_impl_ = 0;
3192 // Make sure the new root is pushed.
3193 EXPECT_EQ(1u, static_cast<PushPropertiesCountingLayerImpl*>(
3194 host_impl->RootLayer())->push_properties_count());
3197 // Root is swapped back all of the layers in the tree get pushed.
3198 ++expected_push_properties_root_impl_;
3199 ++expected_push_properties_child_impl_;
3200 ++expected_push_properties_grandchild_impl_;
3201 ++expected_push_properties_child2_impl_;
3202 ++expected_push_properties_grandchild2_impl_;
3205 // Tree doesn't change but the one leaf that always pushes is pushed.
3206 ++expected_push_properties_grandchild2_impl_;
3209 // First child is removed. Structure of the tree changes here so swap
3210 // some of the values. child_impl becomes child2_impl.
3211 expected_push_properties_child_impl_ =
3212 expected_push_properties_child2_impl_;
3213 expected_push_properties_child2_impl_ = 0;
3214 // grandchild_impl becomes grandchild2_impl.
3215 expected_push_properties_grandchild_impl_ =
3216 expected_push_properties_grandchild2_impl_;
3217 expected_push_properties_grandchild2_impl_ = 0;
3219 // grandchild_impl is now the leaf that always pushes. It is pushed.
3220 ++expected_push_properties_grandchild_impl_;
3223 // The leaf that always pushes is pushed.
3224 ++expected_push_properties_grandchild_impl_;
3226 // Child is added back. New layers are initialized.
3227 ++expected_push_properties_grandchild2_impl_;
3228 ++expected_push_properties_child2_impl_;
3232 expected_push_properties_grandchild2_impl_ = 0;
3235 ++expected_push_properties_grandchild_impl_;
3238 // Leaf is added back
3239 ++expected_push_properties_grandchild2_impl_;
3241 // The leaf that always pushes is pushed.
3242 ++expected_push_properties_grandchild_impl_;
3245 // The leaf that always pushes is pushed.
3246 ++expected_push_properties_grandchild_impl_;
3249 // The leaf that always pushes is pushed.
3250 ++expected_push_properties_grandchild_impl_;
3253 // The leaf that always pushes is pushed.
3254 ++expected_push_properties_grandchild_impl_;
3256 // This child position was changed.
3257 ++expected_push_properties_child2_impl_;
3260 // The position of this child was changed.
3261 ++expected_push_properties_child_impl_;
3263 // The leaf that always pushes is pushed.
3264 ++expected_push_properties_grandchild_impl_;
3267 // Second child is removed from tree. Don't discard counts because
3268 // they are added back before commit.
3270 // The leaf that always pushes is pushed.
3271 ++expected_push_properties_grandchild_impl_;
3273 // Second child added back.
3274 ++expected_push_properties_child2_impl_;
3275 ++expected_push_properties_grandchild2_impl_;
3279 // The position of this child was changed.
3280 ++expected_push_properties_grandchild2_impl_;
3282 // The leaf that always pushes is pushed.
3283 ++expected_push_properties_grandchild_impl_;
3286 // Second child is invalidated with SetNeedsDisplay
3287 ++expected_push_properties_child2_impl_;
3289 // The leaf that always pushed is pushed.
3290 ++expected_push_properties_grandchild_impl_;
3294 PushPropertiesCountingLayerImpl* root_impl_ = NULL;
3295 PushPropertiesCountingLayerImpl* child_impl_ = NULL;
3296 PushPropertiesCountingLayerImpl* child2_impl_ = NULL;
3297 PushPropertiesCountingLayerImpl* grandchild_impl_ = NULL;
3298 PushPropertiesCountingLayerImpl* leaf_always_pushing_layer_impl_ = NULL;
3300 // Pull the layers that we need from the tree assuming the same structure
3301 // as LayerTreeHostTestLayersPushProperties
3302 root_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
3303 host_impl->RootLayer());
3305 if (root_impl_ && root_impl_->children().size() > 0) {
3306 child_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
3307 root_impl_->children()[0]);
3309 if (child_impl_ && child_impl_->children().size() > 0)
3310 grandchild_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
3311 child_impl_->children()[0]);
3314 if (root_impl_ && root_impl_->children().size() > 1) {
3315 child2_impl_ = static_cast<PushPropertiesCountingLayerImpl*>(
3316 root_impl_->children()[1]);
3318 if (child2_impl_ && child2_impl_->children().size() > 0)
3319 leaf_always_pushing_layer_impl_ =
3320 static_cast<PushPropertiesCountingLayerImpl*>(
3321 child2_impl_->children()[0]);
3325 EXPECT_EQ(expected_push_properties_root_impl_,
3326 root_impl_->push_properties_count());
3328 EXPECT_EQ(expected_push_properties_child_impl_,
3329 child_impl_->push_properties_count());
3330 if (grandchild_impl_)
3331 EXPECT_EQ(expected_push_properties_grandchild_impl_,
3332 grandchild_impl_->push_properties_count());
3334 EXPECT_EQ(expected_push_properties_child2_impl_,
3335 child2_impl_->push_properties_count());
3336 if (leaf_always_pushing_layer_impl_)
3337 EXPECT_EQ(expected_push_properties_grandchild2_impl_,
3338 leaf_always_pushing_layer_impl_->push_properties_count());
3341 size_t expected_push_properties_root_impl_;
3342 size_t expected_push_properties_child_impl_;
3343 size_t expected_push_properties_child2_impl_;
3344 size_t expected_push_properties_grandchild_impl_;
3345 size_t expected_push_properties_grandchild2_impl_;
3348 TEST_F(LayerTreeHostTestImplLayersPushProperties, DelegatingRenderer) {
3349 RunTestWithImplSidePainting();
3352 class LayerTreeHostTestPropertyChangesDuringUpdateArePushed
3353 : public LayerTreeHostTest {
3355 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
3357 virtual void SetupTree() OVERRIDE {
3358 root_ = Layer::Create();
3359 root_->SetBounds(gfx::Size(1, 1));
3361 bool paint_scrollbar = true;
3362 bool has_thumb = false;
3363 scrollbar_layer_ = FakePaintedScrollbarLayer::Create(
3364 paint_scrollbar, has_thumb, root_->id());
3366 root_->AddChild(scrollbar_layer_);
3368 layer_tree_host()->SetRootLayer(root_);
3369 LayerTreeHostTest::SetupTree();
3372 virtual void DidCommitAndDrawFrame() OVERRIDE {
3373 switch (layer_tree_host()->source_frame_number()) {
3377 // During update, the ignore_set_needs_commit_ bit is set to true to
3378 // avoid causing a second commit to be scheduled. If a property change
3379 // is made during this, however, it needs to be pushed in the upcoming
3381 scoped_ptr<base::AutoReset<bool> > ignore =
3382 scrollbar_layer_->IgnoreSetNeedsCommit();
3384 scrollbar_layer_->SetBounds(gfx::Size(30, 30));
3386 EXPECT_TRUE(scrollbar_layer_->needs_push_properties());
3387 EXPECT_TRUE(root_->descendant_needs_push_properties());
3388 layer_tree_host()->SetNeedsCommit();
3390 scrollbar_layer_->reset_push_properties_count();
3391 EXPECT_EQ(0u, scrollbar_layer_->push_properties_count());
3395 EXPECT_EQ(1u, scrollbar_layer_->push_properties_count());
3401 virtual void AfterTest() OVERRIDE {}
3403 scoped_refptr<Layer> root_;
3404 scoped_refptr<FakePaintedScrollbarLayer> scrollbar_layer_;
3407 MULTI_THREAD_TEST_F(LayerTreeHostTestPropertyChangesDuringUpdateArePushed);
3409 class LayerTreeHostTestSetDrawableCausesCommit : public LayerTreeHostTest {
3411 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
3413 virtual void SetupTree() OVERRIDE {
3414 root_ = PushPropertiesCountingLayer::Create();
3415 child_ = PushPropertiesCountingLayer::Create();
3416 root_->AddChild(child_);
3418 layer_tree_host()->SetRootLayer(root_);
3419 LayerTreeHostTest::SetupTree();
3422 virtual void DidCommitAndDrawFrame() OVERRIDE {
3423 switch (layer_tree_host()->source_frame_number()) {
3427 // During update, the ignore_set_needs_commit_ bit is set to true to
3428 // avoid causing a second commit to be scheduled. If a property change
3429 // is made during this, however, it needs to be pushed in the upcoming
3431 EXPECT_FALSE(root_->needs_push_properties());
3432 EXPECT_FALSE(child_->needs_push_properties());
3433 EXPECT_EQ(0, root_->NumDescendantsThatDrawContent());
3434 root_->reset_push_properties_count();
3435 child_->reset_push_properties_count();
3436 child_->SetDrawsContent(true);
3437 EXPECT_EQ(1, root_->NumDescendantsThatDrawContent());
3438 EXPECT_EQ(0u, root_->push_properties_count());
3439 EXPECT_EQ(0u, child_->push_properties_count());
3440 EXPECT_TRUE(root_->needs_push_properties());
3441 EXPECT_TRUE(child_->needs_push_properties());
3445 EXPECT_EQ(1u, root_->push_properties_count());
3446 EXPECT_EQ(1u, child_->push_properties_count());
3447 EXPECT_FALSE(root_->needs_push_properties());
3448 EXPECT_FALSE(child_->needs_push_properties());
3454 virtual void AfterTest() OVERRIDE {}
3456 scoped_refptr<PushPropertiesCountingLayer> root_;
3457 scoped_refptr<PushPropertiesCountingLayer> child_;
3460 MULTI_THREAD_TEST_F(LayerTreeHostTestSetDrawableCausesCommit);
3462 class LayerTreeHostTestCasePushPropertiesThreeGrandChildren
3463 : public LayerTreeHostTest {
3465 virtual void BeginTest() OVERRIDE {
3466 expected_push_properties_root_ = 0;
3467 expected_push_properties_child_ = 0;
3468 expected_push_properties_grandchild1_ = 0;
3469 expected_push_properties_grandchild2_ = 0;
3470 expected_push_properties_grandchild3_ = 0;
3471 PostSetNeedsCommitToMainThread();
3474 virtual void SetupTree() OVERRIDE {
3475 root_ = PushPropertiesCountingLayer::Create();
3476 child_ = PushPropertiesCountingLayer::Create();
3477 grandchild1_ = PushPropertiesCountingLayer::Create();
3478 grandchild2_ = PushPropertiesCountingLayer::Create();
3479 grandchild3_ = PushPropertiesCountingLayer::Create();
3481 root_->AddChild(child_);
3482 child_->AddChild(grandchild1_);
3483 child_->AddChild(grandchild2_);
3484 child_->AddChild(grandchild3_);
3486 // Don't set the root layer here.
3487 LayerTreeHostTest::SetupTree();
3490 virtual void AfterTest() OVERRIDE {}
3492 FakeContentLayerClient client_;
3493 scoped_refptr<PushPropertiesCountingLayer> root_;
3494 scoped_refptr<PushPropertiesCountingLayer> child_;
3495 scoped_refptr<PushPropertiesCountingLayer> grandchild1_;
3496 scoped_refptr<PushPropertiesCountingLayer> grandchild2_;
3497 scoped_refptr<PushPropertiesCountingLayer> grandchild3_;
3498 size_t expected_push_properties_root_;
3499 size_t expected_push_properties_child_;
3500 size_t expected_push_properties_grandchild1_;
3501 size_t expected_push_properties_grandchild2_;
3502 size_t expected_push_properties_grandchild3_;
3505 class LayerTreeHostTestPushPropertiesAddingToTreeRequiresPush
3506 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3508 virtual void DidCommitAndDrawFrame() OVERRIDE {
3509 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3510 switch (last_source_frame_number) {
3512 EXPECT_FALSE(root_->needs_push_properties());
3513 EXPECT_FALSE(root_->descendant_needs_push_properties());
3514 EXPECT_FALSE(child_->needs_push_properties());
3515 EXPECT_FALSE(child_->descendant_needs_push_properties());
3516 EXPECT_FALSE(grandchild1_->needs_push_properties());
3517 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3518 EXPECT_FALSE(grandchild2_->needs_push_properties());
3519 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3520 EXPECT_FALSE(grandchild3_->needs_push_properties());
3521 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3523 layer_tree_host()->SetRootLayer(root_);
3525 EXPECT_TRUE(root_->needs_push_properties());
3526 EXPECT_TRUE(root_->descendant_needs_push_properties());
3527 EXPECT_TRUE(child_->needs_push_properties());
3528 EXPECT_TRUE(child_->descendant_needs_push_properties());
3529 EXPECT_TRUE(grandchild1_->needs_push_properties());
3530 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3531 EXPECT_TRUE(grandchild2_->needs_push_properties());
3532 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3533 EXPECT_TRUE(grandchild3_->needs_push_properties());
3534 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3543 MULTI_THREAD_TEST_F(LayerTreeHostTestPushPropertiesAddingToTreeRequiresPush);
3545 class LayerTreeHostTestPushPropertiesRemovingChildStopsRecursion
3546 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3548 virtual void DidCommitAndDrawFrame() OVERRIDE {
3549 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3550 switch (last_source_frame_number) {
3552 layer_tree_host()->SetRootLayer(root_);
3555 EXPECT_FALSE(root_->needs_push_properties());
3556 EXPECT_FALSE(root_->descendant_needs_push_properties());
3557 EXPECT_FALSE(child_->needs_push_properties());
3558 EXPECT_FALSE(child_->descendant_needs_push_properties());
3559 EXPECT_FALSE(grandchild1_->needs_push_properties());
3560 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3561 EXPECT_FALSE(grandchild2_->needs_push_properties());
3562 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3563 EXPECT_FALSE(grandchild3_->needs_push_properties());
3564 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3566 grandchild1_->RemoveFromParent();
3567 grandchild1_->SetPosition(gfx::Point(1, 1));
3569 EXPECT_FALSE(root_->needs_push_properties());
3570 EXPECT_FALSE(root_->descendant_needs_push_properties());
3571 EXPECT_FALSE(child_->needs_push_properties());
3572 EXPECT_FALSE(child_->descendant_needs_push_properties());
3573 EXPECT_FALSE(grandchild2_->needs_push_properties());
3574 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3575 EXPECT_FALSE(grandchild3_->needs_push_properties());
3576 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3578 child_->AddChild(grandchild1_);
3580 EXPECT_FALSE(root_->needs_push_properties());
3581 EXPECT_TRUE(root_->descendant_needs_push_properties());
3582 EXPECT_FALSE(child_->needs_push_properties());
3583 EXPECT_TRUE(child_->descendant_needs_push_properties());
3584 EXPECT_TRUE(grandchild1_->needs_push_properties());
3585 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3586 EXPECT_FALSE(grandchild2_->needs_push_properties());
3587 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3588 EXPECT_FALSE(grandchild3_->needs_push_properties());
3589 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3591 grandchild2_->SetPosition(gfx::Point(1, 1));
3593 EXPECT_FALSE(root_->needs_push_properties());
3594 EXPECT_TRUE(root_->descendant_needs_push_properties());
3595 EXPECT_FALSE(child_->needs_push_properties());
3596 EXPECT_TRUE(child_->descendant_needs_push_properties());
3597 EXPECT_TRUE(grandchild1_->needs_push_properties());
3598 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3599 EXPECT_TRUE(grandchild2_->needs_push_properties());
3600 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3601 EXPECT_FALSE(grandchild3_->needs_push_properties());
3602 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3604 // grandchild2_ will still need a push properties.
3605 grandchild1_->RemoveFromParent();
3607 EXPECT_FALSE(root_->needs_push_properties());
3608 EXPECT_TRUE(root_->descendant_needs_push_properties());
3609 EXPECT_FALSE(child_->needs_push_properties());
3610 EXPECT_TRUE(child_->descendant_needs_push_properties());
3612 // grandchild3_ does not need a push properties, so recursing should
3613 // no longer be needed.
3614 grandchild2_->RemoveFromParent();
3616 EXPECT_FALSE(root_->needs_push_properties());
3617 EXPECT_FALSE(root_->descendant_needs_push_properties());
3618 EXPECT_FALSE(child_->needs_push_properties());
3619 EXPECT_FALSE(child_->descendant_needs_push_properties());
3626 MULTI_THREAD_TEST_F(LayerTreeHostTestPushPropertiesRemovingChildStopsRecursion);
3628 class LayerTreeHostTestPushPropertiesRemovingChildStopsRecursionWithPersistence
3629 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3631 virtual void DidCommitAndDrawFrame() OVERRIDE {
3632 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3633 switch (last_source_frame_number) {
3635 layer_tree_host()->SetRootLayer(root_);
3636 grandchild1_->set_persist_needs_push_properties(true);
3637 grandchild2_->set_persist_needs_push_properties(true);
3640 EXPECT_FALSE(root_->needs_push_properties());
3641 EXPECT_TRUE(root_->descendant_needs_push_properties());
3642 EXPECT_FALSE(child_->needs_push_properties());
3643 EXPECT_TRUE(child_->descendant_needs_push_properties());
3644 EXPECT_TRUE(grandchild1_->needs_push_properties());
3645 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3646 EXPECT_TRUE(grandchild2_->needs_push_properties());
3647 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3648 EXPECT_FALSE(grandchild3_->needs_push_properties());
3649 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3651 // grandchild2_ will still need a push properties.
3652 grandchild1_->RemoveFromParent();
3654 EXPECT_FALSE(root_->needs_push_properties());
3655 EXPECT_TRUE(root_->descendant_needs_push_properties());
3656 EXPECT_FALSE(child_->needs_push_properties());
3657 EXPECT_TRUE(child_->descendant_needs_push_properties());
3659 // grandchild3_ does not need a push properties, so recursing should
3660 // no longer be needed.
3661 grandchild2_->RemoveFromParent();
3663 EXPECT_FALSE(root_->needs_push_properties());
3664 EXPECT_FALSE(root_->descendant_needs_push_properties());
3665 EXPECT_FALSE(child_->needs_push_properties());
3666 EXPECT_FALSE(child_->descendant_needs_push_properties());
3673 MULTI_THREAD_TEST_F(
3674 LayerTreeHostTestPushPropertiesRemovingChildStopsRecursionWithPersistence);
3676 class LayerTreeHostTestPushPropertiesSetPropertiesWhileOutsideTree
3677 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3679 virtual void DidCommitAndDrawFrame() OVERRIDE {
3680 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3681 switch (last_source_frame_number) {
3683 layer_tree_host()->SetRootLayer(root_);
3686 EXPECT_FALSE(root_->needs_push_properties());
3687 EXPECT_FALSE(root_->descendant_needs_push_properties());
3688 EXPECT_FALSE(child_->needs_push_properties());
3689 EXPECT_FALSE(child_->descendant_needs_push_properties());
3690 EXPECT_FALSE(grandchild1_->needs_push_properties());
3691 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3692 EXPECT_FALSE(grandchild2_->needs_push_properties());
3693 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3694 EXPECT_FALSE(grandchild3_->needs_push_properties());
3695 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3697 // Change grandchildren while their parent is not in the tree.
3698 child_->RemoveFromParent();
3699 grandchild1_->SetPosition(gfx::Point(1, 1));
3700 grandchild2_->SetPosition(gfx::Point(1, 1));
3701 root_->AddChild(child_);
3703 EXPECT_FALSE(root_->needs_push_properties());
3704 EXPECT_TRUE(root_->descendant_needs_push_properties());
3705 EXPECT_TRUE(child_->needs_push_properties());
3706 EXPECT_TRUE(child_->descendant_needs_push_properties());
3707 EXPECT_TRUE(grandchild1_->needs_push_properties());
3708 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3709 EXPECT_TRUE(grandchild2_->needs_push_properties());
3710 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3711 EXPECT_TRUE(grandchild3_->needs_push_properties());
3712 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3714 grandchild1_->RemoveFromParent();
3716 EXPECT_FALSE(root_->needs_push_properties());
3717 EXPECT_TRUE(root_->descendant_needs_push_properties());
3718 EXPECT_TRUE(child_->needs_push_properties());
3719 EXPECT_TRUE(child_->descendant_needs_push_properties());
3721 grandchild2_->RemoveFromParent();
3723 EXPECT_FALSE(root_->needs_push_properties());
3724 EXPECT_TRUE(root_->descendant_needs_push_properties());
3725 EXPECT_TRUE(child_->needs_push_properties());
3726 EXPECT_TRUE(child_->descendant_needs_push_properties());
3728 grandchild3_->RemoveFromParent();
3730 EXPECT_FALSE(root_->needs_push_properties());
3731 EXPECT_TRUE(root_->descendant_needs_push_properties());
3732 EXPECT_TRUE(child_->needs_push_properties());
3733 EXPECT_FALSE(child_->descendant_needs_push_properties());
3741 MULTI_THREAD_TEST_F(
3742 LayerTreeHostTestPushPropertiesSetPropertiesWhileOutsideTree);
3744 class LayerTreeHostTestPushPropertiesSetPropertyInParentThenChild
3745 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3747 virtual void DidCommitAndDrawFrame() OVERRIDE {
3748 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3749 switch (last_source_frame_number) {
3751 layer_tree_host()->SetRootLayer(root_);
3754 EXPECT_FALSE(root_->needs_push_properties());
3755 EXPECT_FALSE(root_->descendant_needs_push_properties());
3756 EXPECT_FALSE(child_->needs_push_properties());
3757 EXPECT_FALSE(child_->descendant_needs_push_properties());
3758 EXPECT_FALSE(grandchild1_->needs_push_properties());
3759 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3760 EXPECT_FALSE(grandchild2_->needs_push_properties());
3761 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3762 EXPECT_FALSE(grandchild3_->needs_push_properties());
3763 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3765 child_->SetPosition(gfx::Point(1, 1));
3766 grandchild1_->SetPosition(gfx::Point(1, 1));
3767 grandchild2_->SetPosition(gfx::Point(1, 1));
3769 EXPECT_FALSE(root_->needs_push_properties());
3770 EXPECT_TRUE(root_->descendant_needs_push_properties());
3771 EXPECT_TRUE(child_->needs_push_properties());
3772 EXPECT_TRUE(child_->descendant_needs_push_properties());
3773 EXPECT_TRUE(grandchild1_->needs_push_properties());
3774 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3775 EXPECT_TRUE(grandchild2_->needs_push_properties());
3776 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3777 EXPECT_FALSE(grandchild3_->needs_push_properties());
3778 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3780 grandchild1_->RemoveFromParent();
3782 EXPECT_FALSE(root_->needs_push_properties());
3783 EXPECT_TRUE(root_->descendant_needs_push_properties());
3784 EXPECT_TRUE(child_->needs_push_properties());
3785 EXPECT_TRUE(child_->descendant_needs_push_properties());
3787 grandchild2_->RemoveFromParent();
3789 EXPECT_FALSE(root_->needs_push_properties());
3790 EXPECT_TRUE(root_->descendant_needs_push_properties());
3791 EXPECT_TRUE(child_->needs_push_properties());
3792 EXPECT_FALSE(child_->descendant_needs_push_properties());
3794 child_->RemoveFromParent();
3796 EXPECT_FALSE(root_->needs_push_properties());
3797 EXPECT_FALSE(root_->descendant_needs_push_properties());
3805 MULTI_THREAD_TEST_F(
3806 LayerTreeHostTestPushPropertiesSetPropertyInParentThenChild);
3808 class LayerTreeHostTestPushPropertiesSetPropertyInChildThenParent
3809 : public LayerTreeHostTestCasePushPropertiesThreeGrandChildren {
3811 virtual void DidCommitAndDrawFrame() OVERRIDE {
3812 int last_source_frame_number = layer_tree_host()->source_frame_number() - 1;
3813 switch (last_source_frame_number) {
3815 layer_tree_host()->SetRootLayer(root_);
3818 EXPECT_FALSE(root_->needs_push_properties());
3819 EXPECT_FALSE(root_->descendant_needs_push_properties());
3820 EXPECT_FALSE(child_->needs_push_properties());
3821 EXPECT_FALSE(child_->descendant_needs_push_properties());
3822 EXPECT_FALSE(grandchild1_->needs_push_properties());
3823 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3824 EXPECT_FALSE(grandchild2_->needs_push_properties());
3825 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3826 EXPECT_FALSE(grandchild3_->needs_push_properties());
3827 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3829 grandchild1_->SetPosition(gfx::Point(1, 1));
3830 grandchild2_->SetPosition(gfx::Point(1, 1));
3831 child_->SetPosition(gfx::Point(1, 1));
3833 EXPECT_FALSE(root_->needs_push_properties());
3834 EXPECT_TRUE(root_->descendant_needs_push_properties());
3835 EXPECT_TRUE(child_->needs_push_properties());
3836 EXPECT_TRUE(child_->descendant_needs_push_properties());
3837 EXPECT_TRUE(grandchild1_->needs_push_properties());
3838 EXPECT_FALSE(grandchild1_->descendant_needs_push_properties());
3839 EXPECT_TRUE(grandchild2_->needs_push_properties());
3840 EXPECT_FALSE(grandchild2_->descendant_needs_push_properties());
3841 EXPECT_FALSE(grandchild3_->needs_push_properties());
3842 EXPECT_FALSE(grandchild3_->descendant_needs_push_properties());
3844 grandchild1_->RemoveFromParent();
3846 EXPECT_FALSE(root_->needs_push_properties());
3847 EXPECT_TRUE(root_->descendant_needs_push_properties());
3848 EXPECT_TRUE(child_->needs_push_properties());
3849 EXPECT_TRUE(child_->descendant_needs_push_properties());
3851 grandchild2_->RemoveFromParent();
3853 EXPECT_FALSE(root_->needs_push_properties());
3854 EXPECT_TRUE(root_->descendant_needs_push_properties());
3855 EXPECT_TRUE(child_->needs_push_properties());
3856 EXPECT_FALSE(child_->descendant_needs_push_properties());
3858 child_->RemoveFromParent();
3860 EXPECT_FALSE(root_->needs_push_properties());
3861 EXPECT_FALSE(root_->descendant_needs_push_properties());
3869 MULTI_THREAD_TEST_F(
3870 LayerTreeHostTestPushPropertiesSetPropertyInChildThenParent);
3872 // This test verifies that the tree activation callback is invoked correctly.
3873 class LayerTreeHostTestTreeActivationCallback : public LayerTreeHostTest {
3875 LayerTreeHostTestTreeActivationCallback()
3876 : num_commits_(0), callback_count_(0) {}
3878 virtual void BeginTest() OVERRIDE {
3879 EXPECT_TRUE(HasImplThread());
3880 PostSetNeedsCommitToMainThread();
3883 virtual DrawResult PrepareToDrawOnThread(
3884 LayerTreeHostImpl* host_impl,
3885 LayerTreeHostImpl::FrameData* frame_data,
3886 DrawResult draw_result) OVERRIDE {
3888 switch (num_commits_) {
3890 EXPECT_EQ(0, callback_count_);
3891 callback_count_ = 0;
3893 PostSetNeedsCommitToMainThread();
3896 EXPECT_EQ(1, callback_count_);
3897 callback_count_ = 0;
3899 PostSetNeedsCommitToMainThread();
3902 EXPECT_EQ(0, callback_count_);
3903 callback_count_ = 0;
3907 ADD_FAILURE() << num_commits_;
3911 return LayerTreeHostTest::PrepareToDrawOnThread(
3912 host_impl, frame_data, draw_result);
3915 virtual void AfterTest() OVERRIDE { EXPECT_EQ(3, num_commits_); }
3917 void SetCallback(bool enable) {
3918 output_surface()->SetTreeActivationCallback(
3921 &LayerTreeHostTestTreeActivationCallback::ActivationCallback,
3922 base::Unretained(this))
3926 void ActivationCallback() { ++callback_count_; }
3929 int callback_count_;
3932 TEST_F(LayerTreeHostTestTreeActivationCallback, DirectRenderer) {
3933 RunTest(true, false, true);
3936 TEST_F(LayerTreeHostTestTreeActivationCallback, DelegatingRenderer) {
3937 RunTest(true, true, true);
3940 class LayerInvalidateCausesDraw : public LayerTreeHostTest {
3942 LayerInvalidateCausesDraw() : num_commits_(0), num_draws_(0) {}
3944 virtual void BeginTest() OVERRIDE {
3945 ASSERT_TRUE(!!invalidate_layer_.get())
3946 << "Derived tests must set this in SetupTree";
3948 // One initial commit.
3949 PostSetNeedsCommitToMainThread();
3952 virtual void DidCommitAndDrawFrame() OVERRIDE {
3953 // After commit, invalidate the layer. This should cause a commit.
3954 if (layer_tree_host()->source_frame_number() == 1)
3955 invalidate_layer_->SetNeedsDisplay();
3958 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
3960 if (impl->active_tree()->source_frame_number() == 1)
3964 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
3968 virtual void AfterTest() OVERRIDE {
3969 EXPECT_GE(2, num_commits_);
3970 EXPECT_GE(2, num_draws_);
3974 scoped_refptr<Layer> invalidate_layer_;
3981 // VideoLayer must support being invalidated and then passing that along
3982 // to the compositor thread, even though no resources are updated in
3983 // response to that invalidation.
3984 class LayerTreeHostTestVideoLayerInvalidate : public LayerInvalidateCausesDraw {
3986 virtual void SetupTree() OVERRIDE {
3987 LayerTreeHostTest::SetupTree();
3988 scoped_refptr<VideoLayer> video_layer =
3989 VideoLayer::Create(&provider_, media::VIDEO_ROTATION_0);
3990 video_layer->SetBounds(gfx::Size(10, 10));
3991 video_layer->SetIsDrawable(true);
3992 layer_tree_host()->root_layer()->AddChild(video_layer);
3994 invalidate_layer_ = video_layer;
3998 FakeVideoFrameProvider provider_;
4001 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestVideoLayerInvalidate);
4003 // IOSurfaceLayer must support being invalidated and then passing that along
4004 // to the compositor thread, even though no resources are updated in
4005 // response to that invalidation.
4006 class LayerTreeHostTestIOSurfaceLayerInvalidate
4007 : public LayerInvalidateCausesDraw {
4009 virtual void SetupTree() OVERRIDE {
4010 LayerTreeHostTest::SetupTree();
4011 scoped_refptr<IOSurfaceLayer> layer = IOSurfaceLayer::Create();
4012 layer->SetBounds(gfx::Size(10, 10));
4013 uint32_t fake_io_surface_id = 7;
4014 layer->SetIOSurfaceProperties(fake_io_surface_id, layer->bounds());
4015 layer->SetIsDrawable(true);
4016 layer_tree_host()->root_layer()->AddChild(layer);
4018 invalidate_layer_ = layer;
4022 // TODO(danakj): IOSurface layer can not be transported. crbug.com/239335
4023 SINGLE_AND_MULTI_THREAD_DIRECT_RENDERER_TEST_F(
4024 LayerTreeHostTestIOSurfaceLayerInvalidate);
4026 class LayerTreeHostTestPushHiddenLayer : public LayerTreeHostTest {
4028 virtual void SetupTree() OVERRIDE {
4029 root_layer_ = Layer::Create();
4030 root_layer_->SetPosition(gfx::Point());
4031 root_layer_->SetBounds(gfx::Size(10, 10));
4033 parent_layer_ = SolidColorLayer::Create();
4034 parent_layer_->SetPosition(gfx::Point());
4035 parent_layer_->SetBounds(gfx::Size(10, 10));
4036 parent_layer_->SetIsDrawable(true);
4037 root_layer_->AddChild(parent_layer_);
4039 child_layer_ = SolidColorLayer::Create();
4040 child_layer_->SetPosition(gfx::Point());
4041 child_layer_->SetBounds(gfx::Size(10, 10));
4042 child_layer_->SetIsDrawable(true);
4043 parent_layer_->AddChild(child_layer_);
4045 layer_tree_host()->SetRootLayer(root_layer_);
4046 LayerTreeHostTest::SetupTree();
4049 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
4051 virtual void DidCommitAndDrawFrame() OVERRIDE {
4052 switch (layer_tree_host()->source_frame_number()) {
4054 // The layer type used does not need to push properties every frame.
4055 EXPECT_FALSE(child_layer_->needs_push_properties());
4057 // Change the bounds of the child layer, but make it skipped
4058 // by CalculateDrawProperties.
4059 parent_layer_->SetOpacity(0.f);
4060 child_layer_->SetBounds(gfx::Size(5, 5));
4063 // The bounds of the child layer were pushed to the impl side.
4064 EXPECT_FALSE(child_layer_->needs_push_properties());
4071 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
4072 LayerImpl* root = impl->active_tree()->root_layer();
4073 LayerImpl* parent = root->children()[0];
4074 LayerImpl* child = parent->children()[0];
4076 switch (impl->active_tree()->source_frame_number()) {
4078 EXPECT_EQ(gfx::Size(5, 5).ToString(), child->bounds().ToString());
4083 virtual void AfterTest() OVERRIDE {}
4085 scoped_refptr<Layer> root_layer_;
4086 scoped_refptr<SolidColorLayer> parent_layer_;
4087 scoped_refptr<SolidColorLayer> child_layer_;
4090 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestPushHiddenLayer);
4092 class LayerTreeHostTestUpdateLayerInEmptyViewport : public LayerTreeHostTest {
4094 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
4095 settings->impl_side_painting = true;
4098 virtual void SetupTree() OVERRIDE {
4099 root_layer_ = FakePictureLayer::Create(&client_);
4100 root_layer_->SetBounds(gfx::Size(10, 10));
4102 layer_tree_host()->SetRootLayer(root_layer_);
4103 LayerTreeHostTest::SetupTree();
4106 virtual void BeginTest() OVERRIDE {
4107 // The viewport is empty, but we still need to update layers on the main
4109 layer_tree_host()->SetViewportSize(gfx::Size(0, 0));
4110 PostSetNeedsCommitToMainThread();
4113 virtual void DidCommit() OVERRIDE {
4114 // The layer should be updated even though the viewport is empty, so we
4115 // are capable of drawing it on the impl tree.
4116 EXPECT_GT(root_layer_->update_count(), 0u);
4120 virtual void AfterTest() OVERRIDE {}
4122 FakeContentLayerClient client_;
4123 scoped_refptr<FakePictureLayer> root_layer_;
4126 MULTI_THREAD_TEST_F(LayerTreeHostTestUpdateLayerInEmptyViewport);
4128 class LayerTreeHostTestAbortEvictedTextures : public LayerTreeHostTest {
4130 LayerTreeHostTestAbortEvictedTextures()
4131 : num_will_begin_main_frames_(0), num_impl_commits_(0) {}
4134 virtual void SetupTree() OVERRIDE {
4135 scoped_refptr<SolidColorLayer> root_layer = SolidColorLayer::Create();
4136 root_layer->SetBounds(gfx::Size(200, 200));
4137 root_layer->SetIsDrawable(true);
4139 layer_tree_host()->SetRootLayer(root_layer);
4140 LayerTreeHostTest::SetupTree();
4143 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
4145 virtual void WillBeginMainFrame() OVERRIDE {
4146 num_will_begin_main_frames_++;
4147 switch (num_will_begin_main_frames_) {
4149 // Send a redraw to the compositor thread. This will (wrongly) be
4150 // ignored unless aborting resets the texture state.
4151 layer_tree_host()->SetNeedsRedraw();
4156 virtual void BeginCommitOnThread(LayerTreeHostImpl* impl) OVERRIDE {
4157 num_impl_commits_++;
4160 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
4161 switch (impl->SourceAnimationFrameNumber()) {
4163 // Prevent draws until commit.
4164 impl->active_tree()->SetContentsTexturesPurged();
4165 EXPECT_FALSE(impl->CanDraw());
4166 // Trigger an abortable commit.
4167 impl->SetNeedsCommit();
4175 virtual void AfterTest() OVERRIDE {
4176 // Ensure that the commit was truly aborted.
4177 EXPECT_EQ(2, num_will_begin_main_frames_);
4178 EXPECT_EQ(1, num_impl_commits_);
4182 int num_will_begin_main_frames_;
4183 int num_impl_commits_;
4186 // Commits can only be aborted when using the thread proxy.
4187 MULTI_THREAD_TEST_F(LayerTreeHostTestAbortEvictedTextures);
4189 class LayerTreeHostTestMaxTransferBufferUsageBytes : public LayerTreeHostTest {
4191 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
4192 settings->impl_side_painting = true;
4195 virtual scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface(bool fallback)
4197 scoped_refptr<TestContextProvider> context_provider =
4198 TestContextProvider::Create();
4199 context_provider->SetMaxTransferBufferUsageBytes(1024 * 1024);
4200 if (delegating_renderer())
4201 return FakeOutputSurface::CreateDelegating3d(context_provider);
4203 return FakeOutputSurface::Create3d(context_provider);
4206 virtual void SetupTree() OVERRIDE {
4207 client_.set_fill_with_nonsolid_color(true);
4208 scoped_refptr<FakePictureLayer> root_layer =
4209 FakePictureLayer::Create(&client_);
4210 root_layer->SetBounds(gfx::Size(6000, 6000));
4211 root_layer->SetIsDrawable(true);
4213 layer_tree_host()->SetRootLayer(root_layer);
4214 LayerTreeHostTest::SetupTree();
4217 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
4219 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* impl) OVERRIDE {
4220 TestWebGraphicsContext3D* context = TestContext();
4222 // Expect that the transfer buffer memory used is equal to the
4223 // MaxTransferBufferUsageBytes value set in CreateOutputSurface.
4224 EXPECT_EQ(1024 * 1024u, context->max_used_transfer_buffer_usage_bytes());
4228 virtual void AfterTest() OVERRIDE {}
4231 FakeContentLayerClient client_;
4234 // Impl-side painting is a multi-threaded compositor feature.
4235 MULTI_THREAD_TEST_F(LayerTreeHostTestMaxTransferBufferUsageBytes);
4237 // Test ensuring that memory limits are sent to the prioritized resource
4239 class LayerTreeHostTestMemoryLimits : public LayerTreeHostTest {
4241 LayerTreeHostTestMemoryLimits() : num_commits_(0) {}
4243 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
4245 virtual void WillCommit() OVERRIDE {
4246 // Some commits are aborted, so increment number of attempted commits here.
4250 virtual void DidCommit() OVERRIDE {
4251 switch (num_commits_) {
4253 // Verify default values.
4254 EXPECT_EQ(PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
4256 ->contents_texture_manager()
4257 ->MaxMemoryLimitBytes());
4258 EXPECT_EQ(PriorityCalculator::AllowEverythingCutoff(),
4260 ->contents_texture_manager()
4261 ->ExternalPriorityCutoff());
4262 PostSetNeedsCommitToMainThread();
4265 // The values should remain the same until the commit after the policy
4267 EXPECT_EQ(PrioritizedResourceManager::DefaultMemoryAllocationLimit(),
4269 ->contents_texture_manager()
4270 ->MaxMemoryLimitBytes());
4271 EXPECT_EQ(PriorityCalculator::AllowEverythingCutoff(),
4273 ->contents_texture_manager()
4274 ->ExternalPriorityCutoff());
4277 // Verify values were correctly passed.
4278 EXPECT_EQ(16u * 1024u * 1024u,
4280 ->contents_texture_manager()
4281 ->MaxMemoryLimitBytes());
4282 EXPECT_EQ(PriorityCalculator::AllowVisibleAndNearbyCutoff(),
4284 ->contents_texture_manager()
4285 ->ExternalPriorityCutoff());
4289 // Make sure no extra commits happen.
4295 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
4296 switch (num_commits_) {
4300 // This will trigger a commit because the priority cutoff has changed.
4301 impl->SetMemoryPolicy(ManagedMemoryPolicy(
4302 16u * 1024u * 1024u,
4303 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
4307 // This will not trigger a commit because the priority cutoff has not
4308 // changed, and there is already enough memory for all allocations.
4309 impl->SetMemoryPolicy(ManagedMemoryPolicy(
4310 32u * 1024u * 1024u,
4311 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
4320 virtual void AfterTest() OVERRIDE {}
4326 SINGLE_AND_MULTI_THREAD_NOIMPL_TEST_F(LayerTreeHostTestMemoryLimits);
4330 class LayerTreeHostTestSetMemoryPolicyOnLostOutputSurface
4331 : public LayerTreeHostTest {
4333 LayerTreeHostTestSetMemoryPolicyOnLostOutputSurface()
4334 : first_output_surface_memory_limit_(4321234),
4335 second_output_surface_memory_limit_(1234321) {}
4337 virtual scoped_ptr<FakeOutputSurface> CreateFakeOutputSurface(bool fallback)
4339 if (!first_context_provider_.get()) {
4340 first_context_provider_ = TestContextProvider::Create();
4342 EXPECT_FALSE(second_context_provider_.get());
4343 second_context_provider_ = TestContextProvider::Create();
4346 scoped_refptr<TestContextProvider> provider(second_context_provider_.get()
4347 ? second_context_provider_
4348 : first_context_provider_);
4349 scoped_ptr<FakeOutputSurface> output_surface;
4350 if (delegating_renderer())
4351 output_surface = FakeOutputSurface::CreateDelegating3d(provider);
4353 output_surface = FakeOutputSurface::Create3d(provider);
4354 output_surface->SetMemoryPolicyToSetAtBind(
4355 make_scoped_ptr(new ManagedMemoryPolicy(
4356 second_context_provider_.get() ? second_output_surface_memory_limit_
4357 : first_output_surface_memory_limit_,
4358 gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE,
4359 ManagedMemoryPolicy::kDefaultNumResourcesLimit)));
4360 return output_surface.Pass();
4363 virtual void SetupTree() OVERRIDE {
4364 root_ = FakeContentLayer::Create(&client_);
4365 root_->SetBounds(gfx::Size(20, 20));
4366 layer_tree_host()->SetRootLayer(root_);
4367 LayerTreeHostTest::SetupTree();
4370 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
4372 virtual void DidCommitAndDrawFrame() OVERRIDE {
4373 // Lost context sometimes takes two frames to recreate. The third frame
4374 // is sometimes aborted, so wait until the fourth frame to verify that
4375 // the memory has been set, and the fifth frame to end the test.
4376 if (layer_tree_host()->source_frame_number() < 5) {
4377 layer_tree_host()->SetNeedsCommit();
4378 } else if (layer_tree_host()->source_frame_number() == 5) {
4383 virtual void SwapBuffersOnThread(LayerTreeHostImpl* impl,
4384 bool result) OVERRIDE {
4385 switch (impl->active_tree()->source_frame_number()) {
4387 EXPECT_EQ(first_output_surface_memory_limit_,
4388 impl->memory_allocation_limit_bytes());
4389 // Lose the output surface.
4390 first_context_provider_->TestContext3d()->loseContextCHROMIUM(
4391 GL_GUILTY_CONTEXT_RESET_ARB, GL_INNOCENT_CONTEXT_RESET_ARB);
4394 EXPECT_EQ(second_output_surface_memory_limit_,
4395 impl->memory_allocation_limit_bytes());
4400 virtual void AfterTest() OVERRIDE {}
4402 scoped_refptr<TestContextProvider> first_context_provider_;
4403 scoped_refptr<TestContextProvider> second_context_provider_;
4404 size_t first_output_surface_memory_limit_;
4405 size_t second_output_surface_memory_limit_;
4406 FakeContentLayerClient client_;
4407 scoped_refptr<FakeContentLayer> root_;
4410 // No output to copy for delegated renderers.
4411 SINGLE_AND_MULTI_THREAD_TEST_F(
4412 LayerTreeHostTestSetMemoryPolicyOnLostOutputSurface);
4414 struct TestSwapPromiseResult {
4415 TestSwapPromiseResult()
4416 : did_swap_called(false),
4417 did_not_swap_called(false),
4419 reason(SwapPromise::DID_NOT_SWAP_UNKNOWN) {}
4421 bool did_swap_called;
4422 bool did_not_swap_called;
4424 SwapPromise::DidNotSwapReason reason;
4428 class TestSwapPromise : public SwapPromise {
4430 explicit TestSwapPromise(TestSwapPromiseResult* result) : result_(result) {}
4432 virtual ~TestSwapPromise() {
4433 base::AutoLock lock(result_->lock);
4434 result_->dtor_called = true;
4437 virtual void DidSwap(CompositorFrameMetadata* metadata) OVERRIDE {
4438 base::AutoLock lock(result_->lock);
4439 EXPECT_FALSE(result_->did_swap_called);
4440 EXPECT_FALSE(result_->did_not_swap_called);
4441 result_->did_swap_called = true;
4444 virtual void DidNotSwap(DidNotSwapReason reason) OVERRIDE {
4445 base::AutoLock lock(result_->lock);
4446 EXPECT_FALSE(result_->did_swap_called);
4447 EXPECT_FALSE(result_->did_not_swap_called);
4448 result_->did_not_swap_called = true;
4449 result_->reason = reason;
4452 virtual int64 TraceId() const OVERRIDE { return 0; }
4456 TestSwapPromiseResult* result_;
4459 class LayerTreeHostTestBreakSwapPromise : public LayerTreeHostTest {
4461 LayerTreeHostTestBreakSwapPromise()
4462 : commit_count_(0), commit_complete_count_(0) {}
4464 virtual void WillBeginMainFrame() OVERRIDE {
4465 ASSERT_LE(commit_count_, 2);
4466 scoped_ptr<SwapPromise> swap_promise(
4467 new TestSwapPromise(&swap_promise_result_[commit_count_]));
4468 layer_tree_host()->QueueSwapPromise(swap_promise.Pass());
4471 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
4473 virtual void DidCommit() OVERRIDE {
4475 if (commit_count_ == 2) {
4476 // This commit will finish.
4477 layer_tree_host()->SetNeedsCommit();
4481 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
4482 commit_complete_count_++;
4483 if (commit_complete_count_ == 1) {
4484 // This commit will be aborted because no actual update.
4485 PostSetNeedsUpdateLayersToMainThread();
4491 virtual void AfterTest() OVERRIDE {
4492 // 3 commits are scheduled. 2 completes. 1 is aborted.
4493 EXPECT_EQ(commit_count_, 3);
4494 EXPECT_EQ(commit_complete_count_, 2);
4497 // The first commit completes and causes swap buffer which finishes
4499 base::AutoLock lock(swap_promise_result_[0].lock);
4500 EXPECT_TRUE(swap_promise_result_[0].did_swap_called);
4501 EXPECT_FALSE(swap_promise_result_[0].did_not_swap_called);
4502 EXPECT_TRUE(swap_promise_result_[0].dtor_called);
4506 // The second commit is aborted since it contains no updates.
4507 base::AutoLock lock(swap_promise_result_[1].lock);
4508 EXPECT_FALSE(swap_promise_result_[1].did_swap_called);
4509 EXPECT_TRUE(swap_promise_result_[1].did_not_swap_called);
4510 EXPECT_EQ(SwapPromise::COMMIT_NO_UPDATE, swap_promise_result_[1].reason);
4511 EXPECT_TRUE(swap_promise_result_[1].dtor_called);
4515 // The last commit completes but it does not cause swap buffer because
4516 // there is no damage in the frame data.
4517 base::AutoLock lock(swap_promise_result_[2].lock);
4518 EXPECT_FALSE(swap_promise_result_[2].did_swap_called);
4519 EXPECT_TRUE(swap_promise_result_[2].did_not_swap_called);
4520 EXPECT_EQ(SwapPromise::SWAP_FAILS, swap_promise_result_[2].reason);
4521 EXPECT_TRUE(swap_promise_result_[2].dtor_called);
4526 int commit_complete_count_;
4527 TestSwapPromiseResult swap_promise_result_[3];
4530 MULTI_THREAD_TEST_F(LayerTreeHostTestBreakSwapPromise);
4532 class LayerTreeHostTestBreakSwapPromiseForVisibilityAbortedCommit
4533 : public LayerTreeHostTest {
4535 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
4537 virtual void DidCommit() OVERRIDE {
4538 layer_tree_host()->SetDeferCommits(true);
4539 layer_tree_host()->SetNeedsCommit();
4542 virtual void DidDeferCommit() OVERRIDE {
4543 layer_tree_host()->SetVisible(false);
4544 scoped_ptr<SwapPromise> swap_promise(
4545 new TestSwapPromise(&swap_promise_result_));
4546 layer_tree_host()->QueueSwapPromise(swap_promise.Pass());
4547 layer_tree_host()->SetDeferCommits(false);
4550 virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl,
4551 bool did_handle) OVERRIDE {
4555 virtual void AfterTest() OVERRIDE {
4557 base::AutoLock lock(swap_promise_result_.lock);
4558 EXPECT_FALSE(swap_promise_result_.did_swap_called);
4559 EXPECT_TRUE(swap_promise_result_.did_not_swap_called);
4560 EXPECT_EQ(SwapPromise::COMMIT_FAILS, swap_promise_result_.reason);
4561 EXPECT_TRUE(swap_promise_result_.dtor_called);
4565 TestSwapPromiseResult swap_promise_result_;
4568 SINGLE_AND_MULTI_THREAD_TEST_F(
4569 LayerTreeHostTestBreakSwapPromiseForVisibilityAbortedCommit);
4571 class LayerTreeHostTestBreakSwapPromiseForContextAbortedCommit
4572 : public LayerTreeHostTest {
4574 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
4576 virtual void DidCommit() OVERRIDE {
4579 layer_tree_host()->SetDeferCommits(true);
4580 layer_tree_host()->SetNeedsCommit();
4583 virtual void DidDeferCommit() OVERRIDE {
4584 layer_tree_host()->DidLoseOutputSurface();
4585 scoped_ptr<SwapPromise> swap_promise(
4586 new TestSwapPromise(&swap_promise_result_));
4587 layer_tree_host()->QueueSwapPromise(swap_promise.Pass());
4588 layer_tree_host()->SetDeferCommits(false);
4591 virtual void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl,
4592 bool did_handle) OVERRIDE {
4594 // This lets the test finally commit and exit.
4595 MainThreadTaskRunner()->PostTask(
4597 base::Bind(&LayerTreeHostTestBreakSwapPromiseForContextAbortedCommit::
4599 base::Unretained(this)));
4602 void FindOutputSurface() {
4603 layer_tree_host()->OnCreateAndInitializeOutputSurfaceAttempted(true);
4606 virtual void AfterTest() OVERRIDE {
4608 base::AutoLock lock(swap_promise_result_.lock);
4609 EXPECT_FALSE(swap_promise_result_.did_swap_called);
4610 EXPECT_TRUE(swap_promise_result_.did_not_swap_called);
4611 EXPECT_EQ(SwapPromise::COMMIT_FAILS, swap_promise_result_.reason);
4612 EXPECT_TRUE(swap_promise_result_.dtor_called);
4616 TestSwapPromiseResult swap_promise_result_;
4619 SINGLE_AND_MULTI_THREAD_TEST_F(
4620 LayerTreeHostTestBreakSwapPromiseForContextAbortedCommit);
4622 class SimpleSwapPromiseMonitor : public SwapPromiseMonitor {
4624 SimpleSwapPromiseMonitor(LayerTreeHost* layer_tree_host,
4625 LayerTreeHostImpl* layer_tree_host_impl,
4626 int* set_needs_commit_count,
4627 int* set_needs_redraw_count)
4628 : SwapPromiseMonitor(layer_tree_host, layer_tree_host_impl),
4629 set_needs_commit_count_(set_needs_commit_count) {}
4631 virtual ~SimpleSwapPromiseMonitor() {}
4633 virtual void OnSetNeedsCommitOnMain() OVERRIDE {
4634 (*set_needs_commit_count_)++;
4637 virtual void OnSetNeedsRedrawOnImpl() OVERRIDE {
4638 ADD_FAILURE() << "Should not get called on main thread.";
4641 virtual void OnForwardScrollUpdateToMainThreadOnImpl() OVERRIDE {
4642 ADD_FAILURE() << "Should not get called on main thread.";
4646 int* set_needs_commit_count_;
4649 class LayerTreeHostTestSimpleSwapPromiseMonitor : public LayerTreeHostTest {
4651 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
4653 virtual void WillBeginMainFrame() OVERRIDE {
4657 int set_needs_commit_count = 0;
4658 int set_needs_redraw_count = 0;
4661 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
4662 new SimpleSwapPromiseMonitor(layer_tree_host(),
4664 &set_needs_commit_count,
4665 &set_needs_redraw_count));
4666 layer_tree_host()->SetNeedsCommit();
4667 EXPECT_EQ(1, set_needs_commit_count);
4668 EXPECT_EQ(0, set_needs_redraw_count);
4671 // Now the monitor is destroyed, SetNeedsCommit() is no longer being
4673 layer_tree_host()->SetNeedsCommit();
4674 EXPECT_EQ(1, set_needs_commit_count);
4675 EXPECT_EQ(0, set_needs_redraw_count);
4678 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
4679 new SimpleSwapPromiseMonitor(layer_tree_host(),
4681 &set_needs_commit_count,
4682 &set_needs_redraw_count));
4683 layer_tree_host()->SetNeedsUpdateLayers();
4684 EXPECT_EQ(2, set_needs_commit_count);
4685 EXPECT_EQ(0, set_needs_redraw_count);
4689 scoped_ptr<SimpleSwapPromiseMonitor> swap_promise_monitor(
4690 new SimpleSwapPromiseMonitor(layer_tree_host(),
4692 &set_needs_commit_count,
4693 &set_needs_redraw_count));
4694 layer_tree_host()->SetNeedsAnimate();
4695 EXPECT_EQ(3, set_needs_commit_count);
4696 EXPECT_EQ(0, set_needs_redraw_count);
4702 virtual void AfterTest() OVERRIDE {}
4705 SINGLE_AND_MULTI_THREAD_TEST_F(LayerTreeHostTestSimpleSwapPromiseMonitor);
4707 class LayerTreeHostTestHighResRequiredAfterEvictingUIResources
4708 : public LayerTreeHostTest {
4710 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
4711 settings->impl_side_painting = true;
4714 virtual void SetupTree() OVERRIDE {
4715 LayerTreeHostTest::SetupTree();
4716 ui_resource_ = FakeScopedUIResource::Create(layer_tree_host());
4719 virtual void BeginTest() OVERRIDE { PostSetNeedsCommitToMainThread(); }
4721 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
4722 host_impl->EvictAllUIResources();
4723 // Existence of evicted UI resources will trigger NEW_CONTENT_TAKES_PRIORITY
4724 // mode. Active tree should require high-res to draw after entering this
4725 // mode to ensure that high-res tiles are also required for a pending tree
4727 EXPECT_TRUE(host_impl->active_tree()->RequiresHighResToDraw());
4730 virtual void DidCommit() OVERRIDE {
4731 int frame = layer_tree_host()->source_frame_number();
4734 PostSetNeedsCommitToMainThread();
4737 ui_resource_.reset();
4743 virtual void AfterTest() OVERRIDE {}
4745 FakeContentLayerClient client_;
4746 scoped_ptr<FakeScopedUIResource> ui_resource_;
4749 // This test is flaky, see http://crbug.com/386199
4750 // MULTI_THREAD_TEST_F(LayerTreeHostTestHighResRequiredAfterEvictingUIResources)
4752 class LayerTreeHostTestGpuRasterizationDefault : public LayerTreeHostTest {
4754 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
4755 settings->impl_side_painting = true;
4757 EXPECT_FALSE(settings->gpu_rasterization_enabled);
4758 EXPECT_FALSE(settings->gpu_rasterization_forced);
4761 virtual void SetupTree() OVERRIDE {
4762 LayerTreeHostTest::SetupTree();
4764 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&layer_client_);
4765 layer->SetBounds(gfx::Size(10, 10));
4766 layer->SetIsDrawable(true);
4767 layer_tree_host()->root_layer()->AddChild(layer);
4770 virtual void BeginTest() OVERRIDE {
4771 Layer* root = layer_tree_host()->root_layer();
4772 PictureLayer* layer = static_cast<PictureLayer*>(root->child_at(0));
4773 PicturePile* pile = layer->GetPicturePileForTesting();
4775 // Verify default values.
4776 EXPECT_TRUE(root->IsSuitableForGpuRasterization());
4777 EXPECT_TRUE(layer->IsSuitableForGpuRasterization());
4778 EXPECT_TRUE(pile->is_suitable_for_gpu_rasterization());
4779 EXPECT_FALSE(layer_tree_host()->has_gpu_rasterization_trigger());
4780 EXPECT_FALSE(layer_tree_host()->UseGpuRasterization());
4782 // Setting gpu rasterization trigger does not enable gpu rasterization.
4783 layer_tree_host()->SetHasGpuRasterizationTrigger(true);
4784 EXPECT_TRUE(layer_tree_host()->has_gpu_rasterization_trigger());
4785 EXPECT_FALSE(layer_tree_host()->UseGpuRasterization());
4787 PostSetNeedsCommitToMainThread();
4790 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
4791 EXPECT_FALSE(host_impl->pending_tree()->use_gpu_rasterization());
4792 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4795 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
4796 EXPECT_FALSE(host_impl->active_tree()->use_gpu_rasterization());
4797 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4801 virtual void AfterTest() OVERRIDE {}
4803 FakeContentLayerClient layer_client_;
4806 MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterizationDefault);
4808 class LayerTreeHostTestGpuRasterizationEnabled : public LayerTreeHostTest {
4810 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
4811 settings->impl_side_painting = true;
4813 EXPECT_FALSE(settings->gpu_rasterization_enabled);
4814 settings->gpu_rasterization_enabled = true;
4817 virtual void SetupTree() OVERRIDE {
4818 LayerTreeHostTest::SetupTree();
4820 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&layer_client_);
4821 layer->SetBounds(gfx::Size(10, 10));
4822 layer->SetIsDrawable(true);
4823 layer_tree_host()->root_layer()->AddChild(layer);
4826 virtual void BeginTest() OVERRIDE {
4827 Layer* root = layer_tree_host()->root_layer();
4828 PictureLayer* layer = static_cast<PictureLayer*>(root->child_at(0));
4829 PicturePile* pile = layer->GetPicturePileForTesting();
4831 // Verify default values.
4832 EXPECT_TRUE(root->IsSuitableForGpuRasterization());
4833 EXPECT_TRUE(layer->IsSuitableForGpuRasterization());
4834 EXPECT_TRUE(pile->is_suitable_for_gpu_rasterization());
4835 EXPECT_FALSE(layer_tree_host()->has_gpu_rasterization_trigger());
4836 EXPECT_FALSE(layer_tree_host()->UseGpuRasterization());
4838 // Gpu rasterization trigger is relevant.
4839 layer_tree_host()->SetHasGpuRasterizationTrigger(true);
4840 EXPECT_TRUE(layer_tree_host()->has_gpu_rasterization_trigger());
4841 EXPECT_TRUE(layer_tree_host()->UseGpuRasterization());
4843 // Content-based veto is relevant as well.
4844 pile->SetUnsuitableForGpuRasterizationForTesting();
4845 EXPECT_FALSE(pile->is_suitable_for_gpu_rasterization());
4846 EXPECT_FALSE(layer->IsSuitableForGpuRasterization());
4847 // Veto will take effect when layers are updated.
4848 // The results will be verified after commit is completed below.
4849 // Since we are manually marking picture pile as unsuitable,
4850 // make sure that the layer gets a chance to update.
4851 layer->SetNeedsDisplay();
4852 PostSetNeedsCommitToMainThread();
4855 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
4856 EXPECT_FALSE(host_impl->pending_tree()->use_gpu_rasterization());
4857 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4860 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
4861 EXPECT_FALSE(host_impl->active_tree()->use_gpu_rasterization());
4862 EXPECT_FALSE(host_impl->use_gpu_rasterization());
4866 virtual void AfterTest() OVERRIDE {}
4868 FakeContentLayerClient layer_client_;
4871 MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterizationEnabled);
4873 class LayerTreeHostTestGpuRasterizationForced : public LayerTreeHostTest {
4875 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
4876 settings->impl_side_painting = true;
4878 EXPECT_FALSE(settings->gpu_rasterization_forced);
4879 settings->gpu_rasterization_forced = true;
4882 virtual void SetupTree() OVERRIDE {
4883 LayerTreeHostTest::SetupTree();
4885 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&layer_client_);
4886 layer->SetBounds(gfx::Size(10, 10));
4887 layer->SetIsDrawable(true);
4888 layer_tree_host()->root_layer()->AddChild(layer);
4891 virtual void BeginTest() OVERRIDE {
4892 Layer* root = layer_tree_host()->root_layer();
4893 PictureLayer* layer = static_cast<PictureLayer*>(root->child_at(0));
4894 PicturePile* pile = layer->GetPicturePileForTesting();
4896 // Verify default values.
4897 EXPECT_TRUE(root->IsSuitableForGpuRasterization());
4898 EXPECT_TRUE(layer->IsSuitableForGpuRasterization());
4899 EXPECT_TRUE(pile->is_suitable_for_gpu_rasterization());
4900 EXPECT_FALSE(layer_tree_host()->has_gpu_rasterization_trigger());
4902 // With gpu rasterization forced, gpu rasterization trigger is irrelevant.
4903 EXPECT_TRUE(layer_tree_host()->UseGpuRasterization());
4904 layer_tree_host()->SetHasGpuRasterizationTrigger(true);
4905 EXPECT_TRUE(layer_tree_host()->has_gpu_rasterization_trigger());
4906 EXPECT_TRUE(layer_tree_host()->UseGpuRasterization());
4908 // Content-based veto is irrelevant as well.
4909 pile->SetUnsuitableForGpuRasterizationForTesting();
4910 EXPECT_FALSE(pile->is_suitable_for_gpu_rasterization());
4911 EXPECT_FALSE(layer->IsSuitableForGpuRasterization());
4912 // Veto will take effect when layers are updated.
4913 // The results will be verified after commit is completed below.
4914 // Since we are manually marking picture pile as unsuitable,
4915 // make sure that the layer gets a chance to update.
4916 layer->SetNeedsDisplay();
4917 PostSetNeedsCommitToMainThread();
4920 virtual void CommitCompleteOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
4921 EXPECT_TRUE(host_impl->pending_tree()->use_gpu_rasterization());
4922 EXPECT_TRUE(host_impl->use_gpu_rasterization());
4925 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
4926 EXPECT_TRUE(host_impl->active_tree()->use_gpu_rasterization());
4927 EXPECT_TRUE(host_impl->use_gpu_rasterization());
4931 virtual void AfterTest() OVERRIDE {}
4933 FakeContentLayerClient layer_client_;
4936 MULTI_THREAD_TEST_F(LayerTreeHostTestGpuRasterizationForced);
4938 class LayerTreeHostTestContinuousPainting : public LayerTreeHostTest {
4940 LayerTreeHostTestContinuousPainting()
4941 : num_commits_(0), num_draws_(0), bounds_(20, 20), child_layer_(NULL) {}
4944 enum { kExpectedNumCommits = 10 };
4946 virtual void SetupTree() OVERRIDE {
4947 scoped_refptr<Layer> root_layer = Layer::Create();
4948 root_layer->SetBounds(bounds_);
4950 if (layer_tree_host()->settings().impl_side_painting) {
4951 picture_layer_ = FakePictureLayer::Create(&client_);
4952 child_layer_ = picture_layer_.get();
4954 content_layer_ = ContentLayerWithUpdateTracking::Create(&client_);
4955 child_layer_ = content_layer_.get();
4957 child_layer_->SetBounds(bounds_);
4958 child_layer_->SetIsDrawable(true);
4959 root_layer->AddChild(child_layer_);
4961 layer_tree_host()->SetRootLayer(root_layer);
4962 layer_tree_host()->SetViewportSize(bounds_);
4963 LayerTreeHostTest::SetupTree();
4966 virtual void BeginTest() OVERRIDE {
4967 MainThreadTaskRunner()->PostTask(
4970 &LayerTreeHostTestContinuousPainting::EnableContinuousPainting,
4971 base::Unretained(this)));
4972 // Wait 50x longer than expected.
4973 double milliseconds_per_frame =
4974 1000.0 / layer_tree_host()->settings().refresh_rate;
4975 MainThreadTaskRunner()->PostDelayedTask(
4978 &LayerTreeHostTestContinuousPainting::DisableContinuousPainting,
4979 base::Unretained(this)),
4980 base::TimeDelta::FromMilliseconds(50 * kExpectedNumCommits *
4981 milliseconds_per_frame));
4984 virtual void BeginMainFrame(const BeginFrameArgs& args) OVERRIDE {
4985 child_layer_->SetNeedsDisplay();
4988 virtual void AfterTest() OVERRIDE {
4989 EXPECT_LE(kExpectedNumCommits, num_commits_);
4990 EXPECT_LE(kExpectedNumCommits, num_draws_);
4991 int update_count = content_layer_.get()
4992 ? content_layer_->PaintContentsCount()
4993 : picture_layer_->update_count();
4994 EXPECT_LE(kExpectedNumCommits, update_count);
4997 virtual void DrawLayersOnThread(LayerTreeHostImpl* impl) OVERRIDE {
4998 if (++num_draws_ == kExpectedNumCommits)
5002 virtual void CommitCompleteOnThread(LayerTreeHostImpl* impl) OVERRIDE {
5007 void EnableContinuousPainting() {
5008 LayerTreeDebugState debug_state = layer_tree_host()->debug_state();
5009 debug_state.continuous_painting = true;
5010 layer_tree_host()->SetDebugState(debug_state);
5013 void DisableContinuousPainting() {
5014 LayerTreeDebugState debug_state = layer_tree_host()->debug_state();
5015 debug_state.continuous_painting = false;
5016 layer_tree_host()->SetDebugState(debug_state);
5022 const gfx::Size bounds_;
5023 FakeContentLayerClient client_;
5024 scoped_refptr<ContentLayerWithUpdateTracking> content_layer_;
5025 scoped_refptr<FakePictureLayer> picture_layer_;
5026 Layer* child_layer_;
5029 MULTI_THREAD_TEST_F(LayerTreeHostTestContinuousPainting);
5031 class LayerTreeHostTestInvisibleDoesntActivate : public LayerTreeHostTest {
5033 LayerTreeHostTestInvisibleDoesntActivate() : activation_count_(0) {}
5035 virtual void InitializeSettings(LayerTreeSettings* settings) OVERRIDE {
5036 settings->impl_side_painting = true;
5039 virtual void SetupTree() OVERRIDE {
5040 scoped_refptr<Layer> root_layer = Layer::Create();
5041 root_layer->SetBounds(gfx::Size(1000, 1000));
5043 // Set up a non-solid layer with a bunch of tiles.
5044 client_.set_fill_with_nonsolid_color(true);
5045 picture_layer_ = FakePictureLayer::Create(&client_);
5046 picture_layer_->SetBounds(gfx::Size(1000, 1000));
5047 picture_layer_->SetIsDrawable(true);
5048 picture_layer_->SetNeedsDisplayRect(gfx::Rect(1000, 1000));
5049 root_layer->AddChild(picture_layer_.get());
5051 layer_tree_host()->SetRootLayer(root_layer);
5052 LayerTreeHostTest::SetupTree();
5055 virtual void BeginTest() OVERRIDE {
5056 // Kick off the test with a commit.
5057 PostSetNeedsCommitToMainThread();
5060 virtual void BeginCommitOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
5061 // Make sure we don't activate before going invisible.
5062 host_impl->BlockNotifyReadyToActivateForTesting(true);
5065 virtual void DidCommit() OVERRIDE { layer_tree_host()->SetVisible(false); }
5067 virtual void DidSetVisibleOnImplTree(LayerTreeHostImpl* host_impl,
5068 bool visible) OVERRIDE {
5069 // Once invisible, we can go visible again.
5071 // Allow activation from now on.
5072 host_impl->BlockNotifyReadyToActivateForTesting(false);
5073 PostSetVisibleToMainThread(true);
5077 virtual void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) OVERRIDE {
5078 ++activation_count_;
5079 std::vector<Tile*> tiles = host_impl->tile_manager()->AllTilesForTesting();
5080 EXPECT_GT(tiles.size(), 0u);
5081 // When activating, ensure that all tiles are ready to draw with a mode
5082 // other than rasterize on demand.
5083 int resource_tiles_count = 0;
5084 for (std::vector<Tile*>::iterator it = tiles.begin(); it != tiles.end();
5087 const ManagedTileState::TileVersion& tile_version =
5088 tile->GetTileVersionForDrawing();
5089 EXPECT_TRUE(tile_version.IsReadyToDraw());
5090 EXPECT_NE(ManagedTileState::TileVersion::PICTURE_PILE_MODE,
5091 tile_version.mode());
5092 resource_tiles_count +=
5093 tile_version.mode() == ManagedTileState::TileVersion::RESOURCE_MODE;
5095 EXPECT_GT(resource_tiles_count, 0);
5100 virtual void AfterTest() OVERRIDE {
5101 // Double check that we activated once.
5102 EXPECT_EQ(1, activation_count_);
5106 int activation_count_;
5108 FakeContentLayerClient client_;
5109 scoped_refptr<FakePictureLayer> picture_layer_;
5112 // TODO(vmpstr): Enable with single thread impl-side painting.
5113 MULTI_THREAD_TEST_F(LayerTreeHostTestInvisibleDoesntActivate);