Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: cc/trees/layer_tree_host_unittest_serialization.cc

Issue 2494623002: cc: Remove client/engine LayerTreeHostInProcess. (Closed)
Patch Set: .. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/trees/layer_tree_host_unittest_remote_server.cc ('k') | cc/trees/proxy_main.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include "cc/trees/layer_tree_host.h"
6
7 #include <memory>
8
9 #include "base/memory/ptr_util.h"
10 #include "cc/animation/animation_host.h"
11 #include "cc/layers/empty_content_layer_client.h"
12 #include "cc/layers/heads_up_display_layer.h"
13 #include "cc/layers/layer.h"
14 #include "cc/proto/layer.pb.h"
15 #include "cc/proto/layer_tree_host.pb.h"
16 #include "cc/test/fake_image_serialization_processor.h"
17 #include "cc/test/fake_layer_tree_host.h"
18 #include "cc/test/fake_layer_tree_host_client.h"
19 #include "cc/test/fake_picture_layer.h"
20 #include "cc/test/fake_recording_source.h"
21 #include "cc/test/layer_tree_test.h"
22 #include "cc/test/skia_common.h"
23 #include "cc/test/test_task_graph_runner.h"
24 #include "cc/trees/layer_tree.h"
25 #include "cc/trees/layer_tree_host_common.h"
26 #include "cc/trees/layer_tree_settings.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "third_party/skia/include/core/SkColor.h"
29 #include "ui/gfx/geometry/point.h"
30 #include "ui/gfx/geometry/size.h"
31 #include "ui/gfx/geometry/vector2d_f.h"
32
33 namespace cc {
34
35 namespace {
36 std::unique_ptr<FakeRecordingSource> CreateRecordingSource(
37 const gfx::Rect& viewport) {
38 gfx::Rect layer_rect(viewport.right(), viewport.bottom());
39 std::unique_ptr<FakeRecordingSource> recording_source =
40 FakeRecordingSource::CreateRecordingSource(viewport, layer_rect.size());
41 return recording_source;
42 }
43
44 scoped_refptr<FakePictureLayer> CreatePictureLayer() {
45 gfx::Rect recorded_viewport(0, 0, 256, 256);
46
47 std::unique_ptr<FakeRecordingSource> recording_source =
48 CreateRecordingSource(recorded_viewport);
49 recording_source->SetDisplayListUsesCachedPicture(false);
50
51 SkPaint simple_paint;
52 simple_paint.setColor(SkColorSetARGB(255, 12, 23, 34));
53 recording_source->add_draw_rect_with_paint(gfx::Rect(0, 0, 256, 256),
54 simple_paint);
55 recording_source->SetGenerateDiscardableImagesMetadata(true);
56 recording_source->Rerecord();
57
58 ContentLayerClient* client = EmptyContentLayerClient::GetInstance();
59 return FakePictureLayer::CreateWithRecordingSource(
60 client, std::move(recording_source));
61 }
62 } // namespace
63
64 class LayerTreeHostSerializationTest : public testing::Test {
65 public:
66 LayerTreeHostSerializationTest()
67 : image_serialization_processor_(
68 base::MakeUnique<FakeImageSerializationProcessor>()) {}
69
70 protected:
71 void SetUp() override {
72 LayerTreeSettings settings;
73 animation_host_src_ = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
74 layer_tree_host_src_ = FakeLayerTreeHost::Create(
75 &client_src_, &task_graph_runner_src_, animation_host_src_.get(),
76 settings, CompositorMode::SINGLE_THREADED,
77 image_serialization_processor_.get());
78 animation_host_dst_ = AnimationHost::CreateForTesting(ThreadInstance::MAIN);
79 layer_tree_host_dst_ = FakeLayerTreeHost::Create(
80 &client_dst_, &task_graph_runner_dst_, animation_host_dst_.get(),
81 settings, CompositorMode::SINGLE_THREADED,
82 image_serialization_processor_.get());
83 layer_tree_host_src_->InitializePictureCacheForTesting();
84 layer_tree_host_dst_->InitializePictureCacheForTesting();
85 }
86
87 void TearDown() override {
88 // Need to reset |in_paint_layer_contents_| to tear down.
89
90 layer_tree_host_src_->GetLayerTree()->in_paint_layer_contents_ = false;
91 layer_tree_host_dst_->GetLayerTree()->in_paint_layer_contents_ = false;
92
93 // Need to reset LayerTreeHost pointers before tear down.
94 layer_tree_host_src_ = nullptr;
95 layer_tree_host_dst_ = nullptr;
96 }
97
98 void VerifyHostHasAllExpectedLayersInTree(Layer* root_layer) {
99 LayerTreeHostCommon::CallFunctionForEveryLayer(
100 root_layer->GetLayerTree(), [root_layer](Layer* layer) {
101 DCHECK(layer->GetLayerTreeHostForTesting());
102 EXPECT_EQ(layer, layer->GetLayerTree()->LayerById(layer->id()));
103 });
104 }
105
106 void VerifySerializationAndDeserialization() {
107 proto::LayerTreeHost proto;
108 LayerTree* layer_tree_src = layer_tree_host_src_->GetLayerTree();
109 LayerTree* layer_tree_dst = layer_tree_host_dst_->GetLayerTree();
110
111 std::unordered_set<Layer*> layers_that_should_push_properties_src =
112 layer_tree_src->LayersThatShouldPushProperties();
113 std::vector<std::unique_ptr<SwapPromise>> swap_promises;
114 layer_tree_host_src_->ToProtobufForCommit(&proto, &swap_promises);
115 layer_tree_host_dst_->FromProtobufForCommit(proto);
116
117 EXPECT_EQ(layer_tree_src->needs_full_tree_sync_,
118 layer_tree_dst->needs_full_tree_sync_);
119 EXPECT_EQ(layer_tree_src->needs_meta_info_recomputation_,
120 layer_tree_dst->needs_meta_info_recomputation_);
121 EXPECT_EQ(layer_tree_host_src_->source_frame_number_,
122 layer_tree_host_dst_->source_frame_number_);
123 EXPECT_EQ(layer_tree_host_src_->root_layer()->id(),
124 layer_tree_host_dst_->root_layer()->id());
125 EXPECT_EQ(layer_tree_host_dst_.get(),
126 layer_tree_dst->inputs_.root_layer->GetLayerTreeHostForTesting());
127 EXPECT_EQ(layer_tree_src->inputs_.root_layer->double_sided(),
128 layer_tree_dst->inputs_.root_layer->double_sided());
129 EXPECT_EQ(layer_tree_src->inputs_.device_viewport_size,
130 layer_tree_dst->inputs_.device_viewport_size);
131 EXPECT_EQ(layer_tree_src->inputs_.device_scale_factor,
132 layer_tree_dst->inputs_.device_scale_factor);
133 EXPECT_EQ(layer_tree_src->inputs_.painted_device_scale_factor,
134 layer_tree_dst->inputs_.painted_device_scale_factor);
135 EXPECT_EQ(layer_tree_src->inputs_.page_scale_factor,
136 layer_tree_dst->inputs_.page_scale_factor);
137 EXPECT_EQ(layer_tree_src->inputs_.min_page_scale_factor,
138 layer_tree_dst->inputs_.min_page_scale_factor);
139 EXPECT_EQ(layer_tree_src->inputs_.max_page_scale_factor,
140 layer_tree_dst->inputs_.max_page_scale_factor);
141 EXPECT_EQ(layer_tree_src->elastic_overscroll_,
142 layer_tree_dst->elastic_overscroll_);
143 EXPECT_EQ(layer_tree_host_src_->has_gpu_rasterization_trigger_,
144 layer_tree_host_dst_->has_gpu_rasterization_trigger_);
145 EXPECT_EQ(layer_tree_host_src_->content_is_suitable_for_gpu_rasterization_,
146 layer_tree_host_dst_->content_is_suitable_for_gpu_rasterization_);
147 EXPECT_EQ(layer_tree_src->inputs_.background_color,
148 layer_tree_dst->inputs_.background_color);
149 EXPECT_EQ(layer_tree_src->inputs_.has_transparent_background,
150 layer_tree_dst->inputs_.has_transparent_background);
151 EXPECT_EQ(layer_tree_src->in_paint_layer_contents(),
152 layer_tree_dst->in_paint_layer_contents());
153 EXPECT_EQ(layer_tree_host_src_->id_, layer_tree_host_dst_->id_);
154 EXPECT_EQ(layer_tree_host_src_->next_commit_forces_redraw_,
155 layer_tree_host_dst_->next_commit_forces_redraw_);
156 for (auto* layer : layers_that_should_push_properties_src) {
157 EXPECT_TRUE(layer_tree_dst->LayerNeedsPushPropertiesForTesting(
158 layer_tree_dst->LayerById(layer->id())));
159 }
160
161 if (layer_tree_src->hud_layer_) {
162 EXPECT_EQ(layer_tree_src->hud_layer_->id(),
163 layer_tree_dst->hud_layer_->id());
164 // The HUD layer member is a HeadsUpDisplayLayer instead of Layer, so
165 // inspect the proto to see if it contains the the right layer type.
166 bool found_hud_layer_type = false;
167 for (int i = 0; i < proto.layer_tree().root_layer().children_size();
168 ++i) {
169 if (proto.layer_tree().root_layer().children(i).id() ==
170 layer_tree_src->hud_layer_->id()) {
171 EXPECT_EQ(proto::LayerNode::HEADS_UP_DISPLAY_LAYER,
172 proto.layer_tree().root_layer().children(i).type());
173 found_hud_layer_type = true;
174 break;
175 }
176 }
177 EXPECT_TRUE(found_hud_layer_type);
178 } else {
179 EXPECT_FALSE(layer_tree_dst->hud_layer_);
180 }
181 if (layer_tree_src->overscroll_elasticity_layer()) {
182 EXPECT_EQ(layer_tree_src->overscroll_elasticity_layer()->id(),
183 layer_tree_dst->overscroll_elasticity_layer()->id());
184 } else {
185 EXPECT_FALSE(layer_tree_dst->overscroll_elasticity_layer());
186 }
187 if (layer_tree_src->page_scale_layer()) {
188 EXPECT_EQ(layer_tree_src->page_scale_layer()->id(),
189 layer_tree_dst->page_scale_layer()->id());
190 } else {
191 EXPECT_FALSE(layer_tree_dst->page_scale_layer());
192 }
193 if (layer_tree_src->inner_viewport_scroll_layer()) {
194 EXPECT_EQ(layer_tree_src->inner_viewport_scroll_layer()->id(),
195 layer_tree_dst->inner_viewport_scroll_layer()->id());
196 } else {
197 EXPECT_FALSE(layer_tree_dst->inner_viewport_scroll_layer());
198 }
199 if (layer_tree_src->outer_viewport_scroll_layer()) {
200 EXPECT_EQ(layer_tree_src->outer_viewport_scroll_layer()->id(),
201 layer_tree_dst->outer_viewport_scroll_layer()->id());
202 } else {
203 EXPECT_FALSE(layer_tree_dst->outer_viewport_scroll_layer());
204 }
205 EXPECT_EQ(layer_tree_src->inputs_.selection,
206 layer_tree_dst->inputs_.selection);
207 EXPECT_EQ(layer_tree_src->property_trees_, layer_tree_dst->property_trees_);
208
209 // All layers must have a property tree index that matches PropertyTrees.
210 if (layer_tree_dst->property_trees_.sequence_number) {
211 int seq_num = layer_tree_dst->property_trees_.sequence_number;
212 LayerTreeHostCommon::CallFunctionForEveryLayer(
213 layer_tree_host_dst_->GetLayerTree(), [seq_num](Layer* layer) {
214 EXPECT_EQ(seq_num, layer->property_tree_sequence_number());
215 });
216 }
217 }
218
219 void RunAllMembersChangedTest() {
220 LayerTree* layer_tree_src = layer_tree_host_src_->GetLayerTree();
221
222 layer_tree_src->needs_full_tree_sync_ =
223 !layer_tree_src->needs_full_tree_sync_;
224 layer_tree_src->needs_meta_info_recomputation_ =
225 !layer_tree_src->needs_meta_info_recomputation_;
226 layer_tree_host_src_->source_frame_number_ *= 3;
227
228 // Just fake setup a layer for both source and dest.
229 scoped_refptr<Layer> root_layer_src = Layer::Create();
230 layer_tree_host_src_->SetRootLayer(root_layer_src);
231 layer_tree_host_dst_->SetRootLayer(Layer::Create());
232 root_layer_src->SetDoubleSided(!root_layer_src->double_sided());
233
234 layer_tree_src->inputs_.device_viewport_size = gfx::Size(3, 14);
235 layer_tree_src->inputs_.device_scale_factor =
236 layer_tree_src->inputs_.device_scale_factor * 3 + 1;
237 layer_tree_src->inputs_.painted_device_scale_factor =
238 layer_tree_src->inputs_.painted_device_scale_factor * 3 + 1;
239 layer_tree_src->inputs_.page_scale_factor =
240 layer_tree_src->inputs_.page_scale_factor * 3 + 1;
241 layer_tree_src->inputs_.min_page_scale_factor =
242 layer_tree_src->inputs_.min_page_scale_factor * 3 + 1;
243 layer_tree_src->inputs_.max_page_scale_factor =
244 layer_tree_src->inputs_.max_page_scale_factor * 3 + 1;
245 layer_tree_src->elastic_overscroll_ = gfx::Vector2dF(3, 14);
246 layer_tree_host_src_->has_gpu_rasterization_trigger_ =
247 !layer_tree_host_src_->has_gpu_rasterization_trigger_;
248 layer_tree_host_src_->content_is_suitable_for_gpu_rasterization_ =
249 !layer_tree_host_src_->content_is_suitable_for_gpu_rasterization_;
250 layer_tree_src->inputs_.background_color = SK_ColorMAGENTA;
251 layer_tree_src->inputs_.has_transparent_background =
252 !layer_tree_src->inputs_.has_transparent_background;
253 layer_tree_host_src_->id_ = layer_tree_host_src_->id_ * 3 + 1;
254 layer_tree_host_src_->next_commit_forces_redraw_ =
255 !layer_tree_host_src_->next_commit_forces_redraw_;
256
257 layer_tree_src->hud_layer_ = HeadsUpDisplayLayer::Create();
258 root_layer_src->AddChild(layer_tree_src->hud_layer_);
259
260 scoped_refptr<Layer> overscroll_elasticity_layer = Layer::Create();
261 scoped_refptr<Layer> page_scale_layer = Layer::Create();
262 scoped_refptr<Layer> inner_viewport_scroll_layer = Layer::Create();
263 scoped_refptr<Layer> outer_viewport_scroll_layer = Layer::Create();
264
265 root_layer_src->AddChild(overscroll_elasticity_layer);
266 root_layer_src->AddChild(page_scale_layer);
267 root_layer_src->AddChild(inner_viewport_scroll_layer);
268 root_layer_src->AddChild(outer_viewport_scroll_layer);
269
270 layer_tree_src->RegisterViewportLayers(
271 overscroll_elasticity_layer, page_scale_layer,
272 inner_viewport_scroll_layer, outer_viewport_scroll_layer);
273
274 // Set in_paint_layer_contents_ only after all calls to AddChild() have
275 // finished to ensure it's allowed to do so at that time.
276 layer_tree_src->in_paint_layer_contents_ =
277 !layer_tree_src->in_paint_layer_contents();
278
279 LayerSelectionBound sel_bound;
280 sel_bound.edge_top = gfx::Point(14, 3);
281 LayerSelection selection;
282 selection.start = sel_bound;
283 layer_tree_src->inputs_.selection = selection;
284
285 layer_tree_src->property_trees_.sequence_number =
286 layer_tree_src->property_trees_.sequence_number * 3 + 1;
287
288 VerifySerializationAndDeserialization();
289 }
290
291 void RunLayersChangedTest() {
292 // Just fake setup a layer for both source and dest.
293 scoped_refptr<Layer> root_layer_src = Layer::Create();
294 layer_tree_host_src_->SetRootLayer(root_layer_src);
295 layer_tree_host_dst_->SetRootLayer(Layer::Create());
296 root_layer_src->SetDoubleSided(!root_layer_src->double_sided());
297
298 // No HUD layer or |overscroll_elasticity_layer_|, or the inner/outer
299 // viewport scroll layers.
300 scoped_refptr<Layer> overscroll_elasticity_layer = Layer::Create();
301 layer_tree_host_src_->GetLayerTree()->inputs_.overscroll_elasticity_layer =
302 overscroll_elasticity_layer;
303 root_layer_src->AddChild(overscroll_elasticity_layer);
304
305 VerifySerializationAndDeserialization();
306 }
307
308 void RunLayersChangedMultipleSerializations() {
309 // Just fake setup a layer for both source and dest.
310 scoped_refptr<Layer> root_layer_src = Layer::Create();
311 layer_tree_host_src_->SetRootLayer(root_layer_src);
312 layer_tree_host_dst_->SetRootLayer(Layer::Create());
313 root_layer_src->SetDoubleSided(!root_layer_src->double_sided());
314
315 LayerTree* layer_tree_src = layer_tree_host_src_->GetLayerTree();
316
317 // Ensure that all the layers work correctly for multiple rounds of
318 // serialization and deserialization.
319 layer_tree_src->hud_layer_ = HeadsUpDisplayLayer::Create();
320 root_layer_src->AddChild(layer_tree_src->hud_layer_);
321
322 scoped_refptr<Layer> overscroll_elasticity_layer = Layer::Create();
323 scoped_refptr<Layer> page_scale_layer = Layer::Create();
324 scoped_refptr<Layer> inner_viewport_scroll_layer = Layer::Create();
325 scoped_refptr<Layer> outer_viewport_scroll_layer = Layer::Create();
326
327 root_layer_src->AddChild(overscroll_elasticity_layer);
328 root_layer_src->AddChild(page_scale_layer);
329 root_layer_src->AddChild(inner_viewport_scroll_layer);
330 root_layer_src->AddChild(outer_viewport_scroll_layer);
331 layer_tree_host_src_->GetLayerTree()->RegisterViewportLayers(
332 overscroll_elasticity_layer, page_scale_layer,
333 inner_viewport_scroll_layer, outer_viewport_scroll_layer);
334
335 VerifySerializationAndDeserialization();
336 VerifySerializationAndDeserialization();
337
338 layer_tree_src->hud_layer_ = nullptr;
339 VerifySerializationAndDeserialization();
340 layer_tree_host_src_->GetLayerTree()->inputs_.overscroll_elasticity_layer =
341 nullptr;
342 VerifySerializationAndDeserialization();
343 layer_tree_host_src_->GetLayerTree()->inputs_.page_scale_layer = nullptr;
344 VerifySerializationAndDeserialization();
345 layer_tree_host_src_->GetLayerTree()->inputs_.inner_viewport_scroll_layer =
346 nullptr;
347 VerifySerializationAndDeserialization();
348 layer_tree_host_src_->GetLayerTree()->inputs_.outer_viewport_scroll_layer =
349 nullptr;
350 VerifySerializationAndDeserialization();
351 }
352
353 void RunPictureLayerMultipleSerializationsTest() {
354 // Just fake setup a layer for both source and dest.
355 scoped_refptr<Layer> root_layer_src = Layer::Create();
356 layer_tree_host_src_->SetRootLayer(root_layer_src);
357 layer_tree_host_dst_->SetRootLayer(Layer::Create());
358
359 // Ensure that a PictureLayer work correctly for multiple rounds of
360 // serialization and deserialization.
361 scoped_refptr<FakePictureLayer> picture_layer_src = CreatePictureLayer();
362 root_layer_src->AddChild(picture_layer_src);
363 picture_layer_src->SetBounds(gfx::Size(10, 10));
364 picture_layer_src->SetIsDrawable(true);
365 picture_layer_src->SavePaintProperties();
366 picture_layer_src->Update();
367 picture_layer_src->SavePaintProperties();
368 VerifySerializationAndDeserialization();
369 ASSERT_EQ(1U, layer_tree_host_dst_->root_layer()->children().size());
370 PictureLayer* picture_layer_dst = reinterpret_cast<PictureLayer*>(
371 layer_tree_host_dst_->root_layer()->child_at(0));
372
373 RecordingSource* recording_source_src =
374 picture_layer_src->GetRecordingSourceForTesting();
375 RecordingSource* recording_source_dst =
376 picture_layer_dst->GetRecordingSourceForTesting();
377 EXPECT_EQ(recording_source_src->GetSize(), recording_source_dst->GetSize());
378 EXPECT_TRUE(AreDisplayListDrawingResultsSame(
379 gfx::Rect(recording_source_src->GetSize()),
380 picture_layer_src->GetDisplayItemList(),
381 picture_layer_dst->GetDisplayItemList()));
382
383 VerifySerializationAndDeserialization();
384 }
385
386 void RunAddAndRemoveNodeFromLayerTree() {
387 /* Testing serialization when the tree hierarchy changes like this:
388 root root
389 / \ / \
390 a b => a c
391 \ \
392 c d
393 */
394 scoped_refptr<Layer> layer_src_root = Layer::Create();
395 layer_tree_host_src_->SetRootLayer(layer_src_root);
396
397 scoped_refptr<Layer> layer_src_a = Layer::Create();
398 scoped_refptr<Layer> layer_src_b = Layer::Create();
399 scoped_refptr<Layer> layer_src_c = Layer::Create();
400 scoped_refptr<Layer> layer_src_d = Layer::Create();
401
402 layer_src_root->AddChild(layer_src_a);
403 layer_src_root->AddChild(layer_src_b);
404 layer_src_b->AddChild(layer_src_c);
405
406 VerifySerializationAndDeserialization();
407 VerifyHostHasAllExpectedLayersInTree(layer_tree_host_dst_->root_layer());
408
409 // Now change the Layer Hierarchy
410 layer_src_c->RemoveFromParent();
411 layer_src_b->RemoveFromParent();
412 layer_src_root->AddChild(layer_src_c);
413 layer_src_c->AddChild(layer_src_d);
414
415 VerifySerializationAndDeserialization();
416 VerifyHostHasAllExpectedLayersInTree(layer_tree_host_dst_->root_layer());
417 }
418
419 private:
420 std::unique_ptr<ImageSerializationProcessor> image_serialization_processor_;
421
422 TestTaskGraphRunner task_graph_runner_src_;
423 FakeLayerTreeHostClient client_src_;
424 std::unique_ptr<AnimationHost> animation_host_src_;
425 std::unique_ptr<FakeLayerTreeHost> layer_tree_host_src_;
426
427 TestTaskGraphRunner task_graph_runner_dst_;
428 FakeLayerTreeHostClient client_dst_;
429 std::unique_ptr<AnimationHost> animation_host_dst_;
430 std::unique_ptr<FakeLayerTreeHost> layer_tree_host_dst_;
431 };
432
433 TEST_F(LayerTreeHostSerializationTest, AllMembersChanged) {
434 RunAllMembersChangedTest();
435 }
436
437 TEST_F(LayerTreeHostSerializationTest, LayersChanged) {
438 RunLayersChangedTest();
439 }
440
441 TEST_F(LayerTreeHostSerializationTest, LayersChangedMultipleSerializations) {
442 RunLayersChangedMultipleSerializations();
443 }
444
445 TEST_F(LayerTreeHostSerializationTest, AddAndRemoveNodeFromLayerTree) {
446 RunAddAndRemoveNodeFromLayerTree();
447 }
448
449 TEST_F(LayerTreeHostSerializationTest, PictureLayerMultipleSerializations) {
450 RunPictureLayerMultipleSerializationsTest();
451 }
452
453 } // namespace cc
OLDNEW
« no previous file with comments | « cc/trees/layer_tree_host_unittest_remote_server.cc ('k') | cc/trees/proxy_main.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698