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/playback/display_item_list_settings.h"
11 #include "cc/test/fake_display_list_recording_source.h"
12 #include "cc/test/fake_layer_tree_host.h"
13 #include "cc/test/fake_picture_layer.h"
14 #include "cc/test/fake_picture_layer_impl.h"
15 #include "cc/test/fake_proxy.h"
16 #include "cc/test/test_shared_bitmap_manager.h"
17 #include "cc/test/test_task_graph_runner.h"
18 #include "cc/trees/single_thread_proxy.h"
19 #include "testing/gtest/include/gtest/gtest.h"
24 class MockContentLayerClient
: public ContentLayerClient
{
26 void PaintContents(SkCanvas
* canvas
,
27 const gfx::Rect
& clip
,
28 PaintingControlSetting picture_control
) override
{}
29 scoped_refptr
<DisplayItemList
> PaintContentsToDisplayList(
30 const gfx::Rect
& clip
,
31 PaintingControlSetting picture_control
) override
{
32 return DisplayItemList::Create(clip
, DisplayItemListSettings());
34 bool FillsBoundsCompletely() const override
{ return false; };
35 size_t GetApproximateUnsharedMemoryUsage() const override
{ return 0; }
38 TEST(PictureLayerTest
, NoTilesIfEmptyBounds
) {
39 MockContentLayerClient client
;
40 scoped_refptr
<PictureLayer
> layer
=
41 PictureLayer::Create(LayerSettings(), &client
);
42 layer
->SetBounds(gfx::Size(10, 10));
44 FakeLayerTreeHostClient
host_client(FakeLayerTreeHostClient::DIRECT_3D
);
45 TestTaskGraphRunner task_graph_runner
;
46 scoped_ptr
<FakeLayerTreeHost
> host
=
47 FakeLayerTreeHost::Create(&host_client
, &task_graph_runner
);
48 host
->SetRootLayer(layer
);
49 layer
->SetIsDrawable(true);
50 layer
->SavePaintProperties();
53 EXPECT_EQ(0, host
->source_frame_number());
54 host
->CommitComplete();
55 EXPECT_EQ(1, host
->source_frame_number());
57 layer
->SetBounds(gfx::Size(0, 0));
58 layer
->SavePaintProperties();
59 // Intentionally skipping Update since it would normally be skipped on
60 // a layer with empty bounds.
64 DebugScopedSetImplThread
impl_thread(&proxy
);
66 TestSharedBitmapManager shared_bitmap_manager
;
67 FakeLayerTreeHostImpl
host_impl(LayerTreeSettings(), &proxy
,
68 &shared_bitmap_manager
, &task_graph_runner
);
69 host_impl
.CreatePendingTree();
70 scoped_ptr
<FakePictureLayerImpl
> layer_impl
=
71 FakePictureLayerImpl::Create(host_impl
.pending_tree(), 1);
73 layer
->PushPropertiesTo(layer_impl
.get());
74 EXPECT_FALSE(layer_impl
->CanHaveTilings());
75 EXPECT_TRUE(layer_impl
->bounds() == gfx::Size(0, 0));
76 EXPECT_EQ(gfx::Size(), layer_impl
->raster_source()->GetSize());
77 EXPECT_FALSE(layer_impl
->raster_source()->HasRecordings());
81 TEST(PictureLayerTest
, SuitableForGpuRasterization
) {
82 scoped_ptr
<FakeDisplayListRecordingSource
> recording_source_owned(
83 new FakeDisplayListRecordingSource(gfx::Size(100, 100)));
84 FakeDisplayListRecordingSource
* recording_source
=
85 recording_source_owned
.get();
87 MockContentLayerClient client
;
88 scoped_refptr
<FakePictureLayer
> layer
=
89 FakePictureLayer::CreateWithRecordingSource(
90 LayerSettings(), &client
, recording_source_owned
.Pass());
92 FakeLayerTreeHostClient
host_client(FakeLayerTreeHostClient::DIRECT_3D
);
93 TestTaskGraphRunner task_graph_runner
;
94 scoped_ptr
<FakeLayerTreeHost
> host
=
95 FakeLayerTreeHost::Create(&host_client
, &task_graph_runner
);
96 host
->SetRootLayer(layer
);
98 // Update layers to initialize the recording source.
99 gfx::Size
layer_bounds(200, 200);
100 gfx::Rect
layer_rect(layer_bounds
);
101 Region
invalidation(layer_rect
);
102 recording_source
->UpdateAndExpandInvalidation(
103 &client
, &invalidation
, layer_bounds
, layer_rect
, 1,
104 RecordingSource::RECORD_NORMALLY
);
106 // Layer is suitable for gpu rasterization by default.
107 EXPECT_TRUE(recording_source
->IsSuitableForGpuRasterization());
108 EXPECT_TRUE(layer
->IsSuitableForGpuRasterization());
110 // Veto gpu rasterization.
111 recording_source
->SetUnsuitableForGpuRasterization();
112 EXPECT_FALSE(recording_source
->IsSuitableForGpuRasterization());
113 EXPECT_FALSE(layer
->IsSuitableForGpuRasterization());
116 // PicturePile uses the source frame number as a unit for measuring invalidation
117 // frequency. When a pile moves between compositors, the frame number increases
118 // non-monotonically. This executes that code path under this scenario allowing
119 // for the code to verify correctness with DCHECKs.
120 TEST(PictureLayerTest
, NonMonotonicSourceFrameNumber
) {
121 LayerTreeSettings settings
;
122 settings
.single_thread_proxy_scheduler
= false;
123 settings
.use_zero_copy
= true;
125 FakeLayerTreeHostClient
host_client1(FakeLayerTreeHostClient::DIRECT_3D
);
126 FakeLayerTreeHostClient
host_client2(FakeLayerTreeHostClient::DIRECT_3D
);
127 TestSharedBitmapManager shared_bitmap_manager
;
128 TestTaskGraphRunner task_graph_runner
;
130 MockContentLayerClient client
;
131 scoped_refptr
<FakePictureLayer
> layer
=
132 FakePictureLayer::Create(LayerSettings(), &client
);
134 LayerTreeHost::InitParams params
;
135 params
.client
= &host_client1
;
136 params
.shared_bitmap_manager
= &shared_bitmap_manager
;
137 params
.settings
= &settings
;
138 params
.task_graph_runner
= &task_graph_runner
;
139 params
.main_task_runner
= base::ThreadTaskRunnerHandle::Get();
140 scoped_ptr
<LayerTreeHost
> host1
=
141 LayerTreeHost::CreateSingleThreaded(&host_client1
, ¶ms
);
142 host_client1
.SetLayerTreeHost(host1
.get());
144 params
.client
= &host_client2
;
145 scoped_ptr
<LayerTreeHost
> host2
=
146 LayerTreeHost::CreateSingleThreaded(&host_client2
, ¶ms
);
147 host_client2
.SetLayerTreeHost(host2
.get());
149 // The PictureLayer is put in one LayerTreeHost.
150 host1
->SetRootLayer(layer
);
151 // Do a main frame, record the picture layers.
152 EXPECT_EQ(0, layer
->update_count());
153 layer
->SetNeedsDisplay();
154 host1
->Composite(base::TimeTicks::Now());
155 EXPECT_EQ(1, layer
->update_count());
156 EXPECT_EQ(1, host1
->source_frame_number());
158 // The source frame number in |host1| is now higher than host2.
159 layer
->SetNeedsDisplay();
160 host1
->Composite(base::TimeTicks::Now());
161 EXPECT_EQ(2, layer
->update_count());
162 EXPECT_EQ(2, host1
->source_frame_number());
164 // Then moved to another LayerTreeHost.
165 host1
->SetRootLayer(nullptr);
166 host2
->SetRootLayer(layer
);
168 // Do a main frame, record the picture layers. The frame number has changed
169 // non-monotonically.
170 layer
->SetNeedsDisplay();
171 host2
->Composite(base::TimeTicks::Now());
172 EXPECT_EQ(3, layer
->update_count());
173 EXPECT_EQ(1, host2
->source_frame_number());