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/layers/picture_layer.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "cc/layers/content_layer_client.h"
9 #include "cc/layers/picture_layer_impl.h"
10 #include "cc/test/fake_layer_tree_host.h"
11 #include "cc/test/fake_picture_layer.h"
12 #include "cc/test/fake_picture_layer_impl.h"
13 #include "cc/test/fake_proxy.h"
14 #include "cc/test/test_shared_bitmap_manager.h"
15 #include "cc/test/test_task_graph_runner.h"
16 #include "cc/trees/single_thread_proxy.h"
17 #include "testing/gtest/include/gtest/gtest.h"
22 class MockContentLayerClient
: public ContentLayerClient
{
24 void PaintContents(SkCanvas
* canvas
,
25 const gfx::Rect
& clip
,
26 PaintingControlSetting picture_control
) override
{}
27 scoped_refptr
<DisplayItemList
> PaintContentsToDisplayList(
28 const gfx::Rect
& clip
,
29 PaintingControlSetting picture_control
) override
{
33 bool FillsBoundsCompletely() const override
{ return false; };
34 size_t GetApproximateUnsharedMemoryUsage() const override
{ return 0; }
37 TEST(PictureLayerTest
, NoTilesIfEmptyBounds
) {
38 MockContentLayerClient client
;
39 scoped_refptr
<PictureLayer
> layer
=
40 PictureLayer::Create(LayerSettings(), &client
);
41 layer
->SetBounds(gfx::Size(10, 10));
43 FakeLayerTreeHostClient
host_client(FakeLayerTreeHostClient::DIRECT_3D
);
44 TestTaskGraphRunner task_graph_runner
;
45 scoped_ptr
<FakeLayerTreeHost
> host
=
46 FakeLayerTreeHost::Create(&host_client
, &task_graph_runner
);
47 host
->SetRootLayer(layer
);
48 layer
->SetIsDrawable(true);
49 layer
->SavePaintProperties();
52 EXPECT_EQ(0, host
->source_frame_number());
53 host
->CommitComplete();
54 EXPECT_EQ(1, host
->source_frame_number());
56 layer
->SetBounds(gfx::Size(0, 0));
57 layer
->SavePaintProperties();
58 // Intentionally skipping Update since it would normally be skipped on
59 // a layer with empty bounds.
63 DebugScopedSetImplThread
impl_thread(&proxy
);
65 TestSharedBitmapManager shared_bitmap_manager
;
66 FakeLayerTreeHostImpl
host_impl(LayerTreeSettings(), &proxy
,
67 &shared_bitmap_manager
, &task_graph_runner
);
68 host_impl
.CreatePendingTree();
69 scoped_ptr
<FakePictureLayerImpl
> layer_impl
=
70 FakePictureLayerImpl::Create(host_impl
.pending_tree(), 1);
72 layer
->PushPropertiesTo(layer_impl
.get());
73 EXPECT_FALSE(layer_impl
->CanHaveTilings());
74 EXPECT_TRUE(layer_impl
->bounds() == gfx::Size(0, 0));
75 EXPECT_EQ(gfx::Size(), layer_impl
->raster_source()->GetSize());
76 EXPECT_FALSE(layer_impl
->raster_source()->HasRecordings());
80 TEST(PictureLayerTest
, SuitableForGpuRasterization
) {
81 MockContentLayerClient client
;
82 scoped_refptr
<PictureLayer
> layer
=
83 PictureLayer::Create(LayerSettings(), &client
);
84 FakeLayerTreeHostClient
host_client(FakeLayerTreeHostClient::DIRECT_3D
);
85 TestTaskGraphRunner task_graph_runner
;
86 scoped_ptr
<FakeLayerTreeHost
> host
=
87 FakeLayerTreeHost::Create(&host_client
, &task_graph_runner
);
88 host
->SetRootLayer(layer
);
89 RecordingSource
* recording_source
= layer
->GetRecordingSourceForTesting();
91 // Layer is suitable for gpu rasterization by default.
92 EXPECT_TRUE(recording_source
->IsSuitableForGpuRasterization());
93 EXPECT_TRUE(layer
->IsSuitableForGpuRasterization());
95 // Veto gpu rasterization.
96 recording_source
->SetUnsuitableForGpuRasterizationForTesting();
97 EXPECT_FALSE(recording_source
->IsSuitableForGpuRasterization());
98 EXPECT_FALSE(layer
->IsSuitableForGpuRasterization());
101 TEST(PictureLayerTest
, UseTileGridSize
) {
102 LayerTreeSettings settings
;
103 settings
.default_tile_grid_size
= gfx::Size(123, 123);
105 MockContentLayerClient client
;
106 scoped_refptr
<PictureLayer
> layer
=
107 PictureLayer::Create(LayerSettings(), &client
);
108 FakeLayerTreeHostClient
host_client(FakeLayerTreeHostClient::DIRECT_3D
);
109 TestTaskGraphRunner task_graph_runner
;
110 scoped_ptr
<FakeLayerTreeHost
> host
=
111 FakeLayerTreeHost::Create(&host_client
, &task_graph_runner
, settings
);
112 host
->SetRootLayer(layer
);
114 // Tile-grid is set according to its setting.
116 layer
->GetRecordingSourceForTesting()->GetTileGridSizeForTesting();
117 EXPECT_EQ(size
.width(), 123);
118 EXPECT_EQ(size
.height(), 123);
121 // PicturePile uses the source frame number as a unit for measuring invalidation
122 // frequency. When a pile moves between compositors, the frame number increases
123 // non-monotonically. This executes that code path under this scenario allowing
124 // for the code to verify correctness with DCHECKs.
125 TEST(PictureLayerTest
, NonMonotonicSourceFrameNumber
) {
126 LayerTreeSettings settings
;
127 settings
.single_thread_proxy_scheduler
= false;
128 settings
.use_zero_copy
= true;
129 settings
.use_one_copy
= false;
131 FakeLayerTreeHostClient
host_client1(FakeLayerTreeHostClient::DIRECT_3D
);
132 FakeLayerTreeHostClient
host_client2(FakeLayerTreeHostClient::DIRECT_3D
);
133 TestSharedBitmapManager shared_bitmap_manager
;
134 TestTaskGraphRunner task_graph_runner
;
136 MockContentLayerClient client
;
137 scoped_refptr
<FakePictureLayer
> layer
=
138 FakePictureLayer::Create(LayerSettings(), &client
);
140 LayerTreeHost::InitParams params
;
141 params
.client
= &host_client1
;
142 params
.shared_bitmap_manager
= &shared_bitmap_manager
;
143 params
.settings
= &settings
;
144 params
.task_graph_runner
= &task_graph_runner
;
145 params
.main_task_runner
= base::ThreadTaskRunnerHandle::Get();
146 scoped_ptr
<LayerTreeHost
> host1
=
147 LayerTreeHost::CreateSingleThreaded(&host_client1
, ¶ms
);
148 host_client1
.SetLayerTreeHost(host1
.get());
150 params
.client
= &host_client2
;
151 scoped_ptr
<LayerTreeHost
> host2
=
152 LayerTreeHost::CreateSingleThreaded(&host_client2
, ¶ms
);
153 host_client2
.SetLayerTreeHost(host2
.get());
155 // The PictureLayer is put in one LayerTreeHost.
156 host1
->SetRootLayer(layer
);
157 // Do a main frame, record the picture layers.
158 EXPECT_EQ(0, layer
->update_count());
159 layer
->SetNeedsDisplay();
160 host1
->Composite(base::TimeTicks::Now());
161 EXPECT_EQ(1, layer
->update_count());
162 EXPECT_EQ(1, host1
->source_frame_number());
164 // The source frame number in |host1| is now higher than host2.
165 layer
->SetNeedsDisplay();
166 host1
->Composite(base::TimeTicks::Now());
167 EXPECT_EQ(2, layer
->update_count());
168 EXPECT_EQ(2, host1
->source_frame_number());
170 // Then moved to another LayerTreeHost.
171 host1
->SetRootLayer(nullptr);
172 host2
->SetRootLayer(layer
);
174 // Do a main frame, record the picture layers. The frame number has changed
175 // non-monotonically.
176 layer
->SetNeedsDisplay();
177 host2
->Composite(base::TimeTicks::Now());
178 EXPECT_EQ(3, layer
->update_count());
179 EXPECT_EQ(1, host2
->source_frame_number());