1 // Copyright 2014 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.
8 #include "base/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "cc/layers/solid_color_layer.h"
12 #include "cc/layers/surface_layer.h"
13 #include "cc/test/fake_impl_proxy.h"
14 #include "cc/test/fake_layer_tree_host.h"
15 #include "cc/test/fake_layer_tree_host_client.h"
16 #include "cc/test/fake_layer_tree_host_impl.h"
17 #include "cc/test/fake_output_surface.h"
18 #include "cc/test/layer_tree_test.h"
19 #include "cc/test/test_shared_bitmap_manager.h"
20 #include "cc/trees/layer_tree_host.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
27 class SurfaceLayerTest
: public testing::Test
{
31 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D
)) {}
34 void SetUp() override
{
35 layer_tree_host_
= FakeLayerTreeHost::Create(&fake_client_
);
36 layer_tree_host_
->SetViewportSize(gfx::Size(10, 10));
39 void TearDown() override
{
40 if (layer_tree_host_
) {
41 layer_tree_host_
->SetRootLayer(nullptr);
42 layer_tree_host_
= nullptr;
46 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host_
;
47 FakeLayerTreeHostClient fake_client_
;
48 TestSharedBitmapManager shared_bitmap_manager_
;
51 void SatisfyCallback(SurfaceSequence
* out
, SurfaceSequence in
) {
55 void RequireCallback(SurfaceId
* out_id
,
56 std::set
<SurfaceSequence
>* out
,
63 // Check that one surface can be referenced by multiple LayerTreeHosts, and
64 // each will create its own SurfaceSequence that's satisfied on destruction.
65 TEST_F(SurfaceLayerTest
, MultipleFramesOneSurface
) {
66 SurfaceSequence blank_change
; // Receives sequence if commit doesn't happen.
68 SurfaceId required_id
;
69 std::set
<SurfaceSequence
> required_seq
;
70 scoped_refptr
<SurfaceLayer
> layer(SurfaceLayer::Create(
71 base::Bind(&SatisfyCallback
, &blank_change
),
72 base::Bind(&RequireCallback
, &required_id
, &required_seq
)));
73 layer
->SetSurfaceId(SurfaceId(1), 1.f
, gfx::Size(1, 1));
74 layer_tree_host_
->set_surface_id_namespace(1);
75 layer_tree_host_
->SetRootLayer(layer
);
77 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host2
=
78 FakeLayerTreeHost::Create(&fake_client_
);
79 scoped_refptr
<SurfaceLayer
> layer2(SurfaceLayer::Create(
80 base::Bind(&SatisfyCallback
, &blank_change
),
81 base::Bind(&RequireCallback
, &required_id
, &required_seq
)));
82 layer2
->SetSurfaceId(SurfaceId(1), 1.f
, gfx::Size(1, 1));
83 layer_tree_host2
->set_surface_id_namespace(2);
84 layer_tree_host2
->SetRootLayer(layer2
);
86 // Layers haven't been removed, so no sequence should be satisfied.
87 EXPECT_TRUE(blank_change
.is_null());
89 SurfaceSequence
expected1(1u, 1u);
90 SurfaceSequence
expected2(2u, 1u);
92 layer_tree_host2
->SetRootLayer(nullptr);
93 layer_tree_host2
.reset();
95 // Layer was removed so sequence from second LayerTreeHost should be
97 EXPECT_TRUE(blank_change
== expected2
);
99 // Set of sequences that need to be satisfied should include sequences from
101 EXPECT_TRUE(required_id
== SurfaceId(1));
102 EXPECT_EQ(2u, required_seq
.size());
103 EXPECT_TRUE(required_seq
.count(expected1
));
104 EXPECT_TRUE(required_seq
.count(expected2
));
106 layer_tree_host_
->SetRootLayer(nullptr);
107 layer_tree_host_
.reset();
109 // Layer was removed so sequence from first LayerTreeHost should be
111 EXPECT_TRUE(blank_change
== expected1
);
113 // No more SurfaceSequences should have been generated that need to have be
115 EXPECT_EQ(2u, required_seq
.size());
118 static void CalcDrawProps(FakeLayerTreeHost
* host
, float device_scale_factor
) {
119 RenderSurfaceLayerList render_surface_layer_list
;
120 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting
inputs(
121 host
->root_layer(), gfx::Size(500, 500), &render_surface_layer_list
);
122 inputs
.device_scale_factor
= device_scale_factor
;
123 LayerTreeHostCommon::CalculateDrawProperties(&inputs
);
126 // Check that setting content scale on the surface works.
127 TEST_F(SurfaceLayerTest
, ScaleSurface
) {
128 SurfaceSequence blank_change
;
129 SurfaceId required_id
;
130 std::set
<SurfaceSequence
> required_seq
;
131 scoped_refptr
<SurfaceLayer
> layer(SurfaceLayer::Create(
132 base::Bind(&SatisfyCallback
, &blank_change
),
133 base::Bind(&RequireCallback
, &required_id
, &required_seq
)));
134 gfx::Size
surface_size(10, 15);
135 layer
->SetSurfaceId(SurfaceId(1), 2.f
, surface_size
);
136 layer
->SetBounds(gfx::Size(25, 45));
137 layer_tree_host_
->SetRootLayer(layer
);
139 CalcDrawProps(layer_tree_host_
.get(), 5.f
);
140 EXPECT_EQ(2.f
, layer
->contents_scale_x());
141 EXPECT_EQ(2.f
, layer
->contents_scale_y());
142 EXPECT_EQ(surface_size
.ToString(), layer
->content_bounds().ToString());
144 layer_tree_host_
->SetRootLayer(nullptr);
145 layer_tree_host_
.reset();
148 // Check that SurfaceSequence is sent through swap promise.
149 class SurfaceLayerSwapPromise
: public LayerTreeTest
{
151 SurfaceLayerSwapPromise()
152 : commit_count_(0), sequence_was_satisfied_(false) {}
154 void BeginTest() override
{
155 layer_tree_host()->set_surface_id_namespace(1);
156 layer_
= SurfaceLayer::Create(
157 base::Bind(&SatisfyCallback
, &satisfied_sequence_
),
158 base::Bind(&RequireCallback
, &required_id_
, &required_set_
));
159 layer_
->SetSurfaceId(SurfaceId(1), 1.f
, gfx::Size(1, 1));
161 // Layer hasn't been added to tree so no SurfaceSequence generated yet.
162 EXPECT_EQ(0u, required_set_
.size());
164 layer_tree_host()->SetRootLayer(layer_
);
166 // Should have SurfaceSequence from first tree.
167 SurfaceSequence
expected(1u, 1u);
168 EXPECT_TRUE(required_id_
== SurfaceId(1));
169 EXPECT_EQ(1u, required_set_
.size());
170 EXPECT_TRUE(required_set_
.count(expected
));
172 gfx::Size
bounds(100, 100);
173 layer_tree_host()->SetViewportSize(bounds
);
174 PostSetNeedsCommitToMainThread();
177 void DidCommitAndDrawFrame() override
{
178 base::ThreadTaskRunnerHandle::Get()->PostTask(
179 FROM_HERE
, base::Bind(&SurfaceLayerSwapPromise::ChangeTree
,
180 base::Unretained(this)));
185 switch (commit_count_
) {
187 // Remove SurfaceLayer from tree to cause SwapPromise to be created.
188 blank_layer_
= SolidColorLayer::Create();
189 blank_layer_
->SetIsDrawable(true);
190 blank_layer_
->SetBounds(gfx::Size(10, 10));
191 layer_tree_host()->SetRootLayer(blank_layer_
);
201 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
203 std::vector
<uint32_t>& satisfied
=
204 output_surface()->last_sent_frame().metadata
.satisfies_sequences
;
205 EXPECT_LE(satisfied
.size(), 1u);
206 if (satisfied
.size() == 1) {
207 // Eventually the one SurfaceSequence should be satisfied, but only
208 // after the layer was removed from the tree, and only once.
209 EXPECT_EQ(1u, satisfied
[0]);
210 EXPECT_LE(1, commit_count_
);
211 EXPECT_FALSE(sequence_was_satisfied_
);
212 sequence_was_satisfied_
= true;
217 void AfterTest() override
{
218 EXPECT_TRUE(required_id_
== SurfaceId(1));
219 EXPECT_EQ(1u, required_set_
.size());
220 // Sequence should have been satisfied through Swap, not with the
222 EXPECT_TRUE(satisfied_sequence_
.is_null());
227 bool sequence_was_satisfied_
;
228 scoped_refptr
<SurfaceLayer
> layer_
;
229 scoped_refptr
<Layer
> blank_layer_
;
230 SurfaceSequence satisfied_sequence_
;
232 SurfaceId required_id_
;
233 std::set
<SurfaceSequence
> required_set_
;
236 // TODO(jbauman): Reenable on single thread once http://crbug.com/421923 is
238 MULTI_THREAD_TEST_F(SurfaceLayerSwapPromise
);