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_task_graph_runner.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
{
36 FakeLayerTreeHost::Create(&fake_client_
, &task_graph_runner_
);
37 layer_tree_host_
->SetViewportSize(gfx::Size(10, 10));
40 void TearDown() override
{
41 if (layer_tree_host_
) {
42 layer_tree_host_
->SetRootLayer(nullptr);
43 layer_tree_host_
= nullptr;
47 FakeLayerTreeHostClient fake_client_
;
48 TestTaskGraphRunner task_graph_runner_
;
49 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host_
;
50 LayerSettings layer_settings_
;
53 void SatisfyCallback(SurfaceSequence
* out
, SurfaceSequence in
) {
57 void RequireCallback(SurfaceId
* out_id
,
58 std::set
<SurfaceSequence
>* out
,
65 // Check that one surface can be referenced by multiple LayerTreeHosts, and
66 // each will create its own SurfaceSequence that's satisfied on destruction.
67 TEST_F(SurfaceLayerTest
, MultipleFramesOneSurface
) {
68 SurfaceSequence blank_change
; // Receives sequence if commit doesn't happen.
70 SurfaceId required_id
;
71 std::set
<SurfaceSequence
> required_seq
;
72 scoped_refptr
<SurfaceLayer
> layer(SurfaceLayer::Create(
73 layer_settings_
, base::Bind(&SatisfyCallback
, &blank_change
),
74 base::Bind(&RequireCallback
, &required_id
, &required_seq
)));
75 layer
->SetSurfaceId(SurfaceId(1), 1.f
, gfx::Size(1, 1));
76 layer_tree_host_
->set_surface_id_namespace(1);
77 layer_tree_host_
->SetRootLayer(layer
);
79 scoped_ptr
<FakeLayerTreeHost
> layer_tree_host2
=
80 FakeLayerTreeHost::Create(&fake_client_
, &task_graph_runner_
);
81 scoped_refptr
<SurfaceLayer
> layer2(SurfaceLayer::Create(
82 layer_settings_
, base::Bind(&SatisfyCallback
, &blank_change
),
83 base::Bind(&RequireCallback
, &required_id
, &required_seq
)));
84 layer2
->SetSurfaceId(SurfaceId(1), 1.f
, gfx::Size(1, 1));
85 layer_tree_host2
->set_surface_id_namespace(2);
86 layer_tree_host2
->SetRootLayer(layer2
);
88 // Layers haven't been removed, so no sequence should be satisfied.
89 EXPECT_TRUE(blank_change
.is_null());
91 SurfaceSequence
expected1(1u, 1u);
92 SurfaceSequence
expected2(2u, 1u);
94 layer_tree_host2
->SetRootLayer(nullptr);
95 layer_tree_host2
.reset();
97 // Layer was removed so sequence from second LayerTreeHost should be
99 EXPECT_TRUE(blank_change
== expected2
);
101 // Set of sequences that need to be satisfied should include sequences from
103 EXPECT_TRUE(required_id
== SurfaceId(1));
104 EXPECT_EQ(2u, required_seq
.size());
105 EXPECT_TRUE(required_seq
.count(expected1
));
106 EXPECT_TRUE(required_seq
.count(expected2
));
108 layer_tree_host_
->SetRootLayer(nullptr);
109 layer_tree_host_
.reset();
111 // Layer was removed so sequence from first LayerTreeHost should be
113 EXPECT_TRUE(blank_change
== expected1
);
115 // No more SurfaceSequences should have been generated that need to have be
117 EXPECT_EQ(2u, required_seq
.size());
120 // Check that SurfaceSequence is sent through swap promise.
121 class SurfaceLayerSwapPromise
: public LayerTreeTest
{
123 SurfaceLayerSwapPromise()
124 : commit_count_(0), sequence_was_satisfied_(false) {}
126 void BeginTest() override
{
127 layer_tree_host()->set_surface_id_namespace(1);
128 layer_
= SurfaceLayer::Create(
129 layer_settings(), base::Bind(&SatisfyCallback
, &satisfied_sequence_
),
130 base::Bind(&RequireCallback
, &required_id_
, &required_set_
));
131 layer_
->SetSurfaceId(SurfaceId(1), 1.f
, gfx::Size(1, 1));
133 // Layer hasn't been added to tree so no SurfaceSequence generated yet.
134 EXPECT_EQ(0u, required_set_
.size());
136 layer_tree_host()->SetRootLayer(layer_
);
138 // Should have SurfaceSequence from first tree.
139 SurfaceSequence
expected(1u, 1u);
140 EXPECT_TRUE(required_id_
== SurfaceId(1));
141 EXPECT_EQ(1u, required_set_
.size());
142 EXPECT_TRUE(required_set_
.count(expected
));
144 gfx::Size
bounds(100, 100);
145 layer_tree_host()->SetViewportSize(bounds
);
146 PostSetNeedsCommitToMainThread();
149 void DidCommitAndDrawFrame() override
{
150 base::ThreadTaskRunnerHandle::Get()->PostTask(
151 FROM_HERE
, base::Bind(&SurfaceLayerSwapPromise::ChangeTree
,
152 base::Unretained(this)));
157 switch (commit_count_
) {
159 // Remove SurfaceLayer from tree to cause SwapPromise to be created.
160 blank_layer_
= SolidColorLayer::Create(layer_settings());
161 blank_layer_
->SetIsDrawable(true);
162 blank_layer_
->SetBounds(gfx::Size(10, 10));
163 layer_tree_host()->SetRootLayer(blank_layer_
);
173 void SwapBuffersOnThread(LayerTreeHostImpl
* host_impl
, bool result
) override
{
175 std::vector
<uint32_t>& satisfied
=
176 output_surface()->last_sent_frame().metadata
.satisfies_sequences
;
177 EXPECT_LE(satisfied
.size(), 1u);
178 if (satisfied
.size() == 1) {
179 // Eventually the one SurfaceSequence should be satisfied, but only
180 // after the layer was removed from the tree, and only once.
181 EXPECT_EQ(1u, satisfied
[0]);
182 EXPECT_LE(1, commit_count_
);
183 EXPECT_FALSE(sequence_was_satisfied_
);
184 sequence_was_satisfied_
= true;
189 void AfterTest() override
{
190 EXPECT_TRUE(required_id_
== SurfaceId(1));
191 EXPECT_EQ(1u, required_set_
.size());
192 // Sequence should have been satisfied through Swap, not with the
194 EXPECT_TRUE(satisfied_sequence_
.is_null());
199 bool sequence_was_satisfied_
;
200 scoped_refptr
<SurfaceLayer
> layer_
;
201 scoped_refptr
<Layer
> blank_layer_
;
202 SurfaceSequence satisfied_sequence_
;
204 SurfaceId required_id_
;
205 std::set
<SurfaceSequence
> required_set_
;
208 // TODO(jbauman): Reenable on single thread once http://crbug.com/421923 is
210 MULTI_THREAD_TEST_F(SurfaceLayerSwapPromise
);