Support swap promises that are pinned to a particular layer tree.
[chromium-blink-merge.git] / cc / layers / surface_layer_unittest.cc
blobe35b88683d81e1a1df7bad71be07623c954a57d9
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.
5 #include <set>
6 #include <vector>
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"
24 namespace cc {
25 namespace {
27 class SurfaceLayerTest : public testing::Test {
28 public:
29 SurfaceLayerTest()
30 : fake_client_(
31 FakeLayerTreeHostClient(FakeLayerTreeHostClient::DIRECT_3D)) {}
33 protected:
34 void SetUp() override {
35 layer_tree_host_ =
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) {
54 *out = in;
57 void RequireCallback(SurfaceId* out_id,
58 std::set<SurfaceSequence>* out,
59 SurfaceId in_id,
60 SurfaceSequence in) {
61 *out_id = in_id;
62 out->insert(in);
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
98 // satisfied.
99 EXPECT_TRUE(blank_change == expected2);
101 // Set of sequences that need to be satisfied should include sequences from
102 // both trees.
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
112 // satisfied.
113 EXPECT_TRUE(blank_change == expected1);
115 // No more SurfaceSequences should have been generated that need to have be
116 // satisfied.
117 EXPECT_EQ(2u, required_seq.size());
120 // Check that SurfaceSequence is sent through swap promise.
121 class SurfaceLayerSwapPromise : public LayerTreeTest {
122 public:
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);
147 blank_layer_ = SolidColorLayer::Create(layer_settings());
148 blank_layer_->SetIsDrawable(true);
149 blank_layer_->SetBounds(gfx::Size(10, 10));
151 PostSetNeedsCommitToMainThread();
154 virtual void ChangeTree() = 0;
156 void DidCommitAndDrawFrame() override {
157 base::ThreadTaskRunnerHandle::Get()->PostTask(
158 FROM_HERE, base::Bind(&SurfaceLayerSwapPromise::ChangeTree,
159 base::Unretained(this)));
162 protected:
163 int commit_count_;
164 bool sequence_was_satisfied_;
165 scoped_refptr<SurfaceLayer> layer_;
166 scoped_refptr<Layer> blank_layer_;
167 SurfaceSequence satisfied_sequence_;
169 SurfaceId required_id_;
170 std::set<SurfaceSequence> required_set_;
173 // Check that SurfaceSequence is sent through swap promise.
174 class SurfaceLayerSwapPromiseWithDraw : public SurfaceLayerSwapPromise {
175 public:
176 SurfaceLayerSwapPromiseWithDraw() : SurfaceLayerSwapPromise() {}
178 void ChangeTree() override {
179 ++commit_count_;
180 switch (commit_count_) {
181 case 1:
182 // Remove SurfaceLayer from tree to cause SwapPromise to be created.
183 layer_tree_host()->SetRootLayer(blank_layer_);
184 break;
185 case 2:
186 break;
187 default:
188 NOTREACHED();
189 break;
193 void SwapBuffersOnThread(LayerTreeHostImpl* host_impl, bool result) override {
194 EXPECT_TRUE(result);
195 std::vector<uint32_t>& satisfied =
196 output_surface()->last_sent_frame().metadata.satisfies_sequences;
197 EXPECT_LE(satisfied.size(), 1u);
198 if (satisfied.size() == 1) {
199 // Eventually the one SurfaceSequence should be satisfied, but only
200 // after the layer was removed from the tree, and only once.
201 EXPECT_EQ(1u, satisfied[0]);
202 EXPECT_LE(1, commit_count_);
203 EXPECT_FALSE(sequence_was_satisfied_);
204 sequence_was_satisfied_ = true;
205 EndTest();
209 void AfterTest() override {
210 EXPECT_TRUE(required_id_ == SurfaceId(1));
211 EXPECT_EQ(1u, required_set_.size());
212 // Sequence should have been satisfied through Swap, not with the
213 // callback.
214 EXPECT_TRUE(satisfied_sequence_.is_null());
218 // TODO(jbauman): Reenable on single thread once http://crbug.com/421923 is
219 // fixed.
220 MULTI_THREAD_TEST_F(SurfaceLayerSwapPromiseWithDraw);
222 // Check that SurfaceSequence is sent through swap promise and resolved when
223 // swap fails.
224 class SurfaceLayerSwapPromiseWithoutDraw : public SurfaceLayerSwapPromise {
225 public:
226 SurfaceLayerSwapPromiseWithoutDraw() : SurfaceLayerSwapPromise() {}
228 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl,
229 LayerTreeHostImpl::FrameData* frame,
230 DrawResult draw_result) override {
231 return DRAW_ABORTED_MISSING_HIGH_RES_CONTENT;
234 void ChangeTree() override {
235 ++commit_count_;
236 switch (commit_count_) {
237 case 1:
238 // Remove SurfaceLayer from tree to cause SwapPromise to be created.
239 layer_tree_host()->SetRootLayer(blank_layer_);
240 break;
241 case 2:
242 layer_tree_host()->SetNeedsCommit();
243 break;
244 default:
245 EndTest();
246 break;
250 void AfterTest() override {
251 EXPECT_TRUE(required_id_ == SurfaceId(1));
252 EXPECT_EQ(1u, required_set_.size());
253 // Sequence should have been satisfied with the callback.
254 EXPECT_TRUE(satisfied_sequence_ == SurfaceSequence(1u, 1u));
258 MULTI_THREAD_TEST_F(SurfaceLayerSwapPromiseWithoutDraw);
260 } // namespace
261 } // namespace cc