1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/trees/layer_tree_host.h"
7 #include "cc/test/fake_content_layer_client.h"
8 #include "cc/test/fake_picture_layer.h"
9 #include "cc/test/fake_picture_layer_impl.h"
10 #include "cc/test/layer_tree_test.h"
11 #include "cc/trees/layer_tree_impl.h"
16 // These tests deal with picture layers.
17 class LayerTreeHostPictureTest
: public LayerTreeTest
{
19 void SetupTreeWithSinglePictureLayer(const gfx::Size
& size
) {
20 scoped_refptr
<Layer
> root
= Layer::Create();
21 root
->SetBounds(size
);
23 root_picture_layer_
= FakePictureLayer::Create(&client_
);
24 root_picture_layer_
->SetBounds(size
);
25 root
->AddChild(root_picture_layer_
);
27 layer_tree_host()->SetRootLayer(root
);
30 scoped_refptr
<FakePictureLayer
> root_picture_layer_
;
31 FakeContentLayerClient client_
;
34 class LayerTreeHostPictureTestTwinLayer
35 : public LayerTreeHostPictureTest
{
36 void SetupTree() override
{
37 SetupTreeWithSinglePictureLayer(gfx::Size(1, 1));
40 void BeginTest() override
{
42 PostSetNeedsCommitToMainThread();
45 void DidCommit() override
{
46 switch (layer_tree_host()->source_frame_number()) {
48 // Drop the picture layer from the tree.
49 layer_tree_host()->root_layer()->children()[0]->RemoveFromParent();
52 // Add a new picture layer.
53 scoped_refptr
<FakePictureLayer
> picture
=
54 FakePictureLayer::Create(&client_
);
55 layer_tree_host()->root_layer()->AddChild(picture
);
60 void WillActivateTreeOnThread(LayerTreeHostImpl
* impl
) override
{
61 LayerImpl
* pending_root_impl
= impl
->pending_tree()->root_layer();
62 LayerImpl
* active_root_impl
= impl
->active_tree()->root_layer();
64 if (pending_root_impl
->children().empty()) {
65 EXPECT_EQ(2, activates_
);
69 FakePictureLayerImpl
* pending_picture_impl
=
70 static_cast<FakePictureLayerImpl
*>(pending_root_impl
->children()[0]);
72 if (!active_root_impl
) {
73 EXPECT_EQ(0, activates_
);
74 EXPECT_EQ(nullptr, pending_picture_impl
->GetPendingOrActiveTwinLayer());
78 if (active_root_impl
->children().empty()) {
79 EXPECT_EQ(3, activates_
);
80 EXPECT_EQ(nullptr, pending_picture_impl
->GetPendingOrActiveTwinLayer());
84 FakePictureLayerImpl
* active_picture_impl
=
85 static_cast<FakePictureLayerImpl
*>(active_root_impl
->children()[0]);
87 // After the first activation, when we commit again, we'll have a pending
88 // and active layer. Then we recreate a picture layer in the 4th activate
89 // and the next commit will have a pending and active twin again.
90 EXPECT_TRUE(activates_
== 1 || activates_
== 4);
92 EXPECT_EQ(pending_picture_impl
,
93 active_picture_impl
->GetPendingOrActiveTwinLayer());
94 EXPECT_EQ(active_picture_impl
,
95 pending_picture_impl
->GetPendingOrActiveTwinLayer());
96 EXPECT_EQ(nullptr, active_picture_impl
->GetRecycledTwinLayer());
99 void DidActivateTreeOnThread(LayerTreeHostImpl
* impl
) override
{
100 LayerImpl
* active_root_impl
= impl
->active_tree()->root_layer();
101 LayerImpl
* recycle_root_impl
= impl
->recycle_tree()->root_layer();
103 if (active_root_impl
->children().empty()) {
104 EXPECT_EQ(2, activates_
);
106 FakePictureLayerImpl
* active_picture_impl
=
107 static_cast<FakePictureLayerImpl
*>(active_root_impl
->children()[0]);
108 FakePictureLayerImpl
* recycle_picture_impl
=
109 static_cast<FakePictureLayerImpl
*>(recycle_root_impl
->children()[0]);
111 EXPECT_EQ(nullptr, active_picture_impl
->GetPendingOrActiveTwinLayer());
112 EXPECT_EQ(recycle_picture_impl
,
113 active_picture_impl
->GetRecycledTwinLayer());
118 PostSetNeedsCommitToMainThread();
123 void AfterTest() override
{}
128 SINGLE_AND_MULTI_THREAD_IMPL_TEST_F(LayerTreeHostPictureTestTwinLayer
);
130 class LayerTreeHostPictureTestResizeViewportWithGpuRaster
131 : public LayerTreeHostPictureTest
{
132 void InitializeSettings(LayerTreeSettings
* settings
) override
{
133 settings
->gpu_rasterization_forced
= true;
136 void SetupTree() override
{
137 scoped_refptr
<Layer
> root
= Layer::Create();
138 root
->SetBounds(gfx::Size(768, 960));
140 client_
.set_fill_with_nonsolid_color(true);
141 picture_
= FakePictureLayer::Create(&client_
);
142 picture_
->SetBounds(gfx::Size(768, 960));
143 root
->AddChild(picture_
);
145 layer_tree_host()->SetRootLayer(root
);
146 LayerTreeHostPictureTest::SetupTree();
149 void BeginTest() override
{ PostSetNeedsCommitToMainThread(); }
151 void WillActivateTreeOnThread(LayerTreeHostImpl
* impl
) override
{
152 LayerImpl
* child
= impl
->sync_tree()->root_layer()->children()[0];
153 FakePictureLayerImpl
* picture_impl
=
154 static_cast<FakePictureLayerImpl
*>(child
);
155 gfx::Size tile_size
=
156 picture_impl
->HighResTiling()->TileAt(0, 0)->content_rect().size();
158 switch (impl
->sync_tree()->source_frame_number()) {
160 tile_size_
= tile_size
;
161 // GPU Raster picks a tile size based on the viewport size.
162 EXPECT_EQ(gfx::Size(768, 256), tile_size
);
165 // When the viewport changed size, the new frame's tiles should change
167 EXPECT_NE(gfx::Size(768, 256), tile_size
);
171 void DidCommit() override
{
172 switch (layer_tree_host()->source_frame_number()) {
174 // Change the picture layer's size along with the viewport, so it will
175 // consider picking a new tile size.
176 picture_
->SetBounds(gfx::Size(768, 1056));
177 layer_tree_host()->SetViewportSize(gfx::Size(768, 1056));
184 void AfterTest() override
{}
186 gfx::Size tile_size_
;
187 FakeContentLayerClient client_
;
188 scoped_refptr
<FakePictureLayer
> picture_
;
191 SINGLE_AND_MULTI_THREAD_IMPL_TEST_F(
192 LayerTreeHostPictureTestResizeViewportWithGpuRaster
);