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/resources/resource_update_queue.h"
11 #include "cc/test/fake_layer_tree_host.h"
12 #include "cc/test/fake_picture_layer.h"
13 #include "cc/test/fake_picture_layer_impl.h"
14 #include "cc/test/fake_proxy.h"
15 #include "cc/test/impl_side_painting_settings.h"
16 #include "cc/trees/occlusion_tracker.h"
17 #include "cc/trees/single_thread_proxy.h"
18 #include "testing/gtest/include/gtest/gtest.h"
23 class MockContentLayerClient
: public ContentLayerClient
{
25 void PaintContents(SkCanvas
* canvas
,
26 const gfx::Rect
& clip
,
27 PaintingControlSetting picture_control
) override
{}
28 void PaintContentsToDisplayList(
29 DisplayItemList
* display_list
,
30 const gfx::Rect
& clip
,
31 PaintingControlSetting picture_control
) override
{
34 bool FillsBoundsCompletely() const override
{ return false; };
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 scoped_ptr
<FakeLayerTreeHost
> host
= FakeLayerTreeHost::Create(&host_client
);
45 host
->SetRootLayer(layer
);
46 layer
->SetIsDrawable(true);
47 layer
->SavePaintProperties();
49 OcclusionTracker
<Layer
> occlusion(gfx::Rect(0, 0, 1000, 1000));
50 scoped_ptr
<ResourceUpdateQueue
> queue(new ResourceUpdateQueue
);
51 layer
->Update(queue
.get(), &occlusion
);
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(ImplSidePaintingSettings(), &proxy
,
68 &shared_bitmap_manager
, nullptr);
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 MockContentLayerClient client
;
83 scoped_refptr
<PictureLayer
> layer
=
84 PictureLayer::Create(LayerSettings(), &client
);
85 FakeLayerTreeHostClient
host_client(FakeLayerTreeHostClient::DIRECT_3D
);
86 scoped_ptr
<FakeLayerTreeHost
> host
= FakeLayerTreeHost::Create(&host_client
);
87 host
->SetRootLayer(layer
);
88 RecordingSource
* recording_source
= layer
->GetRecordingSourceForTesting();
90 // Layer is suitable for gpu rasterization by default.
91 EXPECT_TRUE(recording_source
->IsSuitableForGpuRasterization());
92 EXPECT_TRUE(layer
->IsSuitableForGpuRasterization());
94 // Veto gpu rasterization.
95 recording_source
->SetUnsuitableForGpuRasterizationForTesting();
96 EXPECT_FALSE(recording_source
->IsSuitableForGpuRasterization());
97 EXPECT_FALSE(layer
->IsSuitableForGpuRasterization());
100 TEST(PictureLayerTest
, UseTileGridSize
) {
101 LayerTreeSettings settings
;
102 settings
.default_tile_grid_size
= gfx::Size(123, 123);
104 MockContentLayerClient client
;
105 scoped_refptr
<PictureLayer
> layer
=
106 PictureLayer::Create(LayerSettings(), &client
);
107 FakeLayerTreeHostClient
host_client(FakeLayerTreeHostClient::DIRECT_3D
);
108 scoped_ptr
<FakeLayerTreeHost
> host
=
109 FakeLayerTreeHost::Create(&host_client
, settings
);
110 host
->SetRootLayer(layer
);
112 // Tile-grid is set according to its setting.
114 layer
->GetRecordingSourceForTesting()->GetTileGridSizeForTesting();
115 EXPECT_EQ(size
.width(), 123);
116 EXPECT_EQ(size
.height(), 123);
119 // PicturePile uses the source frame number as a unit for measuring invalidation
120 // frequency. When a pile moves between compositors, the frame number increases
121 // non-monotonically. This executes that code path under this scenario allowing
122 // for the code to verify correctness with DCHECKs.
123 TEST(PictureLayerTest
, NonMonotonicSourceFrameNumber
) {
124 LayerTreeSettings settings
;
125 settings
.single_thread_proxy_scheduler
= false;
127 FakeLayerTreeHostClient
host_client1(FakeLayerTreeHostClient::DIRECT_3D
);
128 FakeLayerTreeHostClient
host_client2(FakeLayerTreeHostClient::DIRECT_3D
);
129 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager(
130 new TestSharedBitmapManager());
132 MockContentLayerClient client
;
133 scoped_refptr
<FakePictureLayer
> layer
=
134 FakePictureLayer::Create(LayerSettings(), &client
);
136 LayerTreeHost::InitParams params
;
137 params
.client
= &host_client1
;
138 params
.shared_bitmap_manager
= shared_bitmap_manager
.get();
139 params
.settings
= &settings
;
140 params
.main_task_runner
= base::ThreadTaskRunnerHandle::Get();
141 scoped_ptr
<LayerTreeHost
> host1
=
142 LayerTreeHost::CreateSingleThreaded(&host_client1
, ¶ms
);
143 host_client1
.SetLayerTreeHost(host1
.get());
145 params
.client
= &host_client2
;
146 scoped_ptr
<LayerTreeHost
> host2
=
147 LayerTreeHost::CreateSingleThreaded(&host_client2
, ¶ms
);
148 host_client2
.SetLayerTreeHost(host2
.get());
150 // The PictureLayer is put in one LayerTreeHost.
151 host1
->SetRootLayer(layer
);
152 // Do a main frame, record the picture layers.
153 EXPECT_EQ(0u, layer
->update_count());
154 layer
->SetNeedsDisplay();
155 host1
->Composite(base::TimeTicks::Now());
156 EXPECT_EQ(1u, layer
->update_count());
157 EXPECT_EQ(1, host1
->source_frame_number());
159 // The source frame number in |host1| is now higher than host2.
160 layer
->SetNeedsDisplay();
161 host1
->Composite(base::TimeTicks::Now());
162 EXPECT_EQ(2u, layer
->update_count());
163 EXPECT_EQ(2, host1
->source_frame_number());
165 // Then moved to another LayerTreeHost.
166 host1
->SetRootLayer(nullptr);
167 host2
->SetRootLayer(layer
);
169 // Do a main frame, record the picture layers. The frame number has changed
170 // non-monotonically.
171 layer
->SetNeedsDisplay();
172 host2
->Composite(base::TimeTicks::Now());
173 EXPECT_EQ(3u, layer
->update_count());
174 EXPECT_EQ(1, host2
->source_frame_number());