Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / cc / layers / layer_unittest.cc
blobba8e92d75b95a91e479783596d3eea22433c9d08
1 // Copyright 2011 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/layer.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "cc/animation/keyframed_animation_curve.h"
9 #include "cc/base/math_util.h"
10 #include "cc/layers/layer_impl.h"
11 #include "cc/output/copy_output_request.h"
12 #include "cc/output/copy_output_result.h"
13 #include "cc/test/animation_test_common.h"
14 #include "cc/test/fake_impl_proxy.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/geometry_test_utils.h"
18 #include "cc/test/layer_test_common.h"
19 #include "cc/test/test_gpu_memory_buffer_manager.h"
20 #include "cc/test/test_shared_bitmap_manager.h"
21 #include "cc/test/test_task_graph_runner.h"
22 #include "cc/trees/layer_tree_host.h"
23 #include "cc/trees/single_thread_proxy.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/gfx/transform.h"
28 using ::testing::AnyNumber;
29 using ::testing::AtLeast;
30 using ::testing::Mock;
31 using ::testing::StrictMock;
32 using ::testing::_;
34 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
35 do { \
36 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
37 code_to_test; \
38 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
39 } while (false)
41 namespace cc {
42 namespace {
44 class MockLayerTreeHost : public LayerTreeHost {
45 public:
46 MockLayerTreeHost(LayerTreeHostSingleThreadClient* single_thread_client,
47 LayerTreeHost::InitParams* params)
48 : LayerTreeHost(params) {
49 InitializeSingleThreaded(single_thread_client,
50 base::ThreadTaskRunnerHandle::Get(), nullptr);
53 MOCK_METHOD0(SetNeedsCommit, void());
54 MOCK_METHOD0(SetNeedsUpdateLayers, void());
55 MOCK_METHOD0(SetNeedsFullTreeSync, void());
58 class LayerTest : public testing::Test {
59 public:
60 LayerTest()
61 : host_impl_(&proxy_, &shared_bitmap_manager_, &task_graph_runner_),
62 fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
64 protected:
65 void SetUp() override {
66 LayerTreeHost::InitParams params;
67 LayerTreeSettings settings;
68 params.client = &fake_client_;
69 params.settings = &settings;
70 params.task_graph_runner = &task_graph_runner_;
71 layer_tree_host_.reset(
72 new StrictMock<MockLayerTreeHost>(&fake_client_, &params));
75 void TearDown() override {
76 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
77 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
78 parent_ = nullptr;
79 child1_ = nullptr;
80 child2_ = nullptr;
81 child3_ = nullptr;
82 grand_child1_ = nullptr;
83 grand_child2_ = nullptr;
84 grand_child3_ = nullptr;
86 layer_tree_host_->SetRootLayer(nullptr);
87 layer_tree_host_ = nullptr;
90 void VerifyTestTreeInitialState() const {
91 ASSERT_EQ(3U, parent_->children().size());
92 EXPECT_EQ(child1_, parent_->children()[0]);
93 EXPECT_EQ(child2_, parent_->children()[1]);
94 EXPECT_EQ(child3_, parent_->children()[2]);
95 EXPECT_EQ(parent_.get(), child1_->parent());
96 EXPECT_EQ(parent_.get(), child2_->parent());
97 EXPECT_EQ(parent_.get(), child3_->parent());
99 ASSERT_EQ(2U, child1_->children().size());
100 EXPECT_EQ(grand_child1_, child1_->children()[0]);
101 EXPECT_EQ(grand_child2_, child1_->children()[1]);
102 EXPECT_EQ(child1_.get(), grand_child1_->parent());
103 EXPECT_EQ(child1_.get(), grand_child2_->parent());
105 ASSERT_EQ(1U, child2_->children().size());
106 EXPECT_EQ(grand_child3_, child2_->children()[0]);
107 EXPECT_EQ(child2_.get(), grand_child3_->parent());
109 ASSERT_EQ(0U, child3_->children().size());
112 void CreateSimpleTestTree() {
113 parent_ = Layer::Create(layer_settings_);
114 child1_ = Layer::Create(layer_settings_);
115 child2_ = Layer::Create(layer_settings_);
116 child3_ = Layer::Create(layer_settings_);
117 grand_child1_ = Layer::Create(layer_settings_);
118 grand_child2_ = Layer::Create(layer_settings_);
119 grand_child3_ = Layer::Create(layer_settings_);
121 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
122 layer_tree_host_->SetRootLayer(parent_);
124 parent_->AddChild(child1_);
125 parent_->AddChild(child2_);
126 parent_->AddChild(child3_);
127 child1_->AddChild(grand_child1_);
128 child1_->AddChild(grand_child2_);
129 child2_->AddChild(grand_child3_);
131 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
133 VerifyTestTreeInitialState();
136 FakeImplProxy proxy_;
137 TestSharedBitmapManager shared_bitmap_manager_;
138 TestTaskGraphRunner task_graph_runner_;
139 FakeLayerTreeHostImpl host_impl_;
141 FakeLayerTreeHostClient fake_client_;
142 scoped_ptr<StrictMock<MockLayerTreeHost>> layer_tree_host_;
143 scoped_refptr<Layer> parent_;
144 scoped_refptr<Layer> child1_;
145 scoped_refptr<Layer> child2_;
146 scoped_refptr<Layer> child3_;
147 scoped_refptr<Layer> grand_child1_;
148 scoped_refptr<Layer> grand_child2_;
149 scoped_refptr<Layer> grand_child3_;
151 LayerSettings layer_settings_;
154 TEST_F(LayerTest, BasicCreateAndDestroy) {
155 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
156 ASSERT_TRUE(test_layer.get());
158 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
159 test_layer->SetLayerTreeHost(layer_tree_host_.get());
160 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
162 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
163 test_layer->SetLayerTreeHost(nullptr);
166 TEST_F(LayerTest, AddAndRemoveChild) {
167 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
168 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
170 // Upon creation, layers should not have children or parent.
171 ASSERT_EQ(0U, parent->children().size());
172 EXPECT_FALSE(child->parent());
174 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
175 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->AddChild(child));
177 ASSERT_EQ(1U, parent->children().size());
178 EXPECT_EQ(child.get(), parent->children()[0].get());
179 EXPECT_EQ(parent.get(), child->parent());
180 EXPECT_EQ(parent.get(), child->RootLayer());
182 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child->RemoveFromParent());
185 TEST_F(LayerTest, AddSameChildTwice) {
186 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
188 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
189 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
191 layer_tree_host_->SetRootLayer(parent);
193 ASSERT_EQ(0u, parent->children().size());
195 parent->AddChild(child);
196 ASSERT_EQ(1u, parent->children().size());
197 EXPECT_EQ(parent.get(), child->parent());
199 parent->AddChild(child);
200 ASSERT_EQ(1u, parent->children().size());
201 EXPECT_EQ(parent.get(), child->parent());
204 TEST_F(LayerTest, InsertChild) {
205 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
206 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
207 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
208 scoped_refptr<Layer> child3 = Layer::Create(layer_settings_);
209 scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
211 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
213 ASSERT_EQ(0U, parent->children().size());
215 // Case 1: inserting to empty list.
216 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child3, 0));
217 ASSERT_EQ(1U, parent->children().size());
218 EXPECT_EQ(child3, parent->children()[0]);
219 EXPECT_EQ(parent.get(), child3->parent());
221 // Case 2: inserting to beginning of list
222 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
223 ASSERT_EQ(2U, parent->children().size());
224 EXPECT_EQ(child1, parent->children()[0]);
225 EXPECT_EQ(child3, parent->children()[1]);
226 EXPECT_EQ(parent.get(), child1->parent());
228 // Case 3: inserting to middle of list
229 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
230 ASSERT_EQ(3U, parent->children().size());
231 EXPECT_EQ(child1, parent->children()[0]);
232 EXPECT_EQ(child2, parent->children()[1]);
233 EXPECT_EQ(child3, parent->children()[2]);
234 EXPECT_EQ(parent.get(), child2->parent());
236 // Case 4: inserting to end of list
237 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child4, 3));
239 ASSERT_EQ(4U, parent->children().size());
240 EXPECT_EQ(child1, parent->children()[0]);
241 EXPECT_EQ(child2, parent->children()[1]);
242 EXPECT_EQ(child3, parent->children()[2]);
243 EXPECT_EQ(child4, parent->children()[3]);
244 EXPECT_EQ(parent.get(), child4->parent());
246 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
249 TEST_F(LayerTest, InsertChildPastEndOfList) {
250 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
251 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
252 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
254 ASSERT_EQ(0U, parent->children().size());
256 // insert to an out-of-bounds index
257 parent->InsertChild(child1, 53);
259 ASSERT_EQ(1U, parent->children().size());
260 EXPECT_EQ(child1, parent->children()[0]);
262 // insert another child to out-of-bounds, when list is not already empty.
263 parent->InsertChild(child2, 2459);
265 ASSERT_EQ(2U, parent->children().size());
266 EXPECT_EQ(child1, parent->children()[0]);
267 EXPECT_EQ(child2, parent->children()[1]);
270 TEST_F(LayerTest, InsertSameChildTwice) {
271 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
272 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
273 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
275 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
277 ASSERT_EQ(0U, parent->children().size());
279 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
280 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
282 ASSERT_EQ(2U, parent->children().size());
283 EXPECT_EQ(child1, parent->children()[0]);
284 EXPECT_EQ(child2, parent->children()[1]);
286 // Inserting the same child again should cause the child to be removed and
287 // re-inserted at the new location.
288 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent->InsertChild(child1, 1));
290 // child1 should now be at the end of the list.
291 ASSERT_EQ(2U, parent->children().size());
292 EXPECT_EQ(child2, parent->children()[0]);
293 EXPECT_EQ(child1, parent->children()[1]);
295 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
298 TEST_F(LayerTest, ReplaceChildWithNewChild) {
299 CreateSimpleTestTree();
300 scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
302 EXPECT_FALSE(child4->parent());
304 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
305 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
306 EXPECT_FALSE(parent_->NeedsDisplayForTesting());
307 EXPECT_FALSE(child1_->NeedsDisplayForTesting());
308 EXPECT_FALSE(child2_->NeedsDisplayForTesting());
309 EXPECT_FALSE(child3_->NeedsDisplayForTesting());
310 EXPECT_FALSE(child4->NeedsDisplayForTesting());
312 ASSERT_EQ(static_cast<size_t>(3), parent_->children().size());
313 EXPECT_EQ(child1_, parent_->children()[0]);
314 EXPECT_EQ(child4, parent_->children()[1]);
315 EXPECT_EQ(child3_, parent_->children()[2]);
316 EXPECT_EQ(parent_.get(), child4->parent());
318 EXPECT_FALSE(child2_->parent());
321 TEST_F(LayerTest, ReplaceChildWithNewChildThatHasOtherParent) {
322 CreateSimpleTestTree();
324 // create another simple tree with test_layer and child4.
325 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
326 scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
327 test_layer->AddChild(child4);
328 ASSERT_EQ(1U, test_layer->children().size());
329 EXPECT_EQ(child4, test_layer->children()[0]);
330 EXPECT_EQ(test_layer.get(), child4->parent());
332 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
333 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
335 ASSERT_EQ(3U, parent_->children().size());
336 EXPECT_EQ(child1_, parent_->children()[0]);
337 EXPECT_EQ(child4, parent_->children()[1]);
338 EXPECT_EQ(child3_, parent_->children()[2]);
339 EXPECT_EQ(parent_.get(), child4->parent());
341 // test_layer should no longer have child4,
342 // and child2 should no longer have a parent.
343 ASSERT_EQ(0U, test_layer->children().size());
344 EXPECT_FALSE(child2_->parent());
347 TEST_F(LayerTest, DeleteRemovedScrollParent) {
348 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
349 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
350 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
352 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
354 ASSERT_EQ(0U, parent->children().size());
356 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
357 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
359 ASSERT_EQ(2U, parent->children().size());
360 EXPECT_EQ(child1, parent->children()[0]);
361 EXPECT_EQ(child2, parent->children()[1]);
363 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
365 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child2->RemoveFromParent());
367 child1->reset_needs_push_properties_for_testing();
369 EXPECT_SET_NEEDS_COMMIT(1, child2 = nullptr);
371 EXPECT_TRUE(child1->needs_push_properties());
373 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
376 TEST_F(LayerTest, DeleteRemovedScrollChild) {
377 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
378 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
379 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
381 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
383 ASSERT_EQ(0U, parent->children().size());
385 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
386 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
388 ASSERT_EQ(2U, parent->children().size());
389 EXPECT_EQ(child1, parent->children()[0]);
390 EXPECT_EQ(child2, parent->children()[1]);
392 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
394 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child1->RemoveFromParent());
396 child2->reset_needs_push_properties_for_testing();
398 EXPECT_SET_NEEDS_COMMIT(1, child1 = nullptr);
400 EXPECT_TRUE(child2->needs_push_properties());
402 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
405 TEST_F(LayerTest, ReplaceChildWithSameChild) {
406 CreateSimpleTestTree();
408 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
409 // same child.
410 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
411 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(0);
412 parent_->ReplaceChild(child2_.get(), child2_);
414 VerifyTestTreeInitialState();
417 TEST_F(LayerTest, RemoveAllChildren) {
418 CreateSimpleTestTree();
420 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren());
422 ASSERT_EQ(0U, parent_->children().size());
423 EXPECT_FALSE(child1_->parent());
424 EXPECT_FALSE(child2_->parent());
425 EXPECT_FALSE(child3_->parent());
428 TEST_F(LayerTest, SetChildren) {
429 scoped_refptr<Layer> old_parent = Layer::Create(layer_settings_);
430 scoped_refptr<Layer> new_parent = Layer::Create(layer_settings_);
432 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
433 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
435 LayerList new_children;
436 new_children.push_back(child1);
437 new_children.push_back(child2);
439 // Set up and verify initial test conditions: child1 has a parent, child2 has
440 // no parent.
441 old_parent->AddChild(child1);
442 ASSERT_EQ(0U, new_parent->children().size());
443 EXPECT_EQ(old_parent.get(), child1->parent());
444 EXPECT_FALSE(child2->parent());
446 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
447 1, layer_tree_host_->SetRootLayer(new_parent));
449 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
450 AtLeast(1), new_parent->SetChildren(new_children));
452 ASSERT_EQ(2U, new_parent->children().size());
453 EXPECT_EQ(new_parent.get(), child1->parent());
454 EXPECT_EQ(new_parent.get(), child2->parent());
456 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
459 TEST_F(LayerTest, HasAncestor) {
460 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
461 EXPECT_FALSE(parent->HasAncestor(parent.get()));
463 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
464 parent->AddChild(child);
466 EXPECT_FALSE(child->HasAncestor(child.get()));
467 EXPECT_TRUE(child->HasAncestor(parent.get()));
468 EXPECT_FALSE(parent->HasAncestor(child.get()));
470 scoped_refptr<Layer> child_child = Layer::Create(layer_settings_);
471 child->AddChild(child_child);
473 EXPECT_FALSE(child_child->HasAncestor(child_child.get()));
474 EXPECT_TRUE(child_child->HasAncestor(parent.get()));
475 EXPECT_TRUE(child_child->HasAncestor(child.get()));
476 EXPECT_FALSE(parent->HasAncestor(child.get()));
477 EXPECT_FALSE(parent->HasAncestor(child_child.get()));
480 TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) {
481 CreateSimpleTestTree();
483 // For this test we don't care about SetNeedsFullTreeSync calls.
484 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
486 scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
488 EXPECT_EQ(parent_.get(), parent_->RootLayer());
489 EXPECT_EQ(parent_.get(), child1_->RootLayer());
490 EXPECT_EQ(parent_.get(), child2_->RootLayer());
491 EXPECT_EQ(parent_.get(), child3_->RootLayer());
492 EXPECT_EQ(child4.get(), child4->RootLayer());
493 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
494 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
495 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
497 child1_->RemoveFromParent();
499 // |child1| and its children, grand_child1 and grand_child2 are now on a
500 // separate subtree.
501 EXPECT_EQ(parent_.get(), parent_->RootLayer());
502 EXPECT_EQ(child1_.get(), child1_->RootLayer());
503 EXPECT_EQ(parent_.get(), child2_->RootLayer());
504 EXPECT_EQ(parent_.get(), child3_->RootLayer());
505 EXPECT_EQ(child4.get(), child4->RootLayer());
506 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
507 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
508 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
510 grand_child3_->AddChild(child4);
512 EXPECT_EQ(parent_.get(), parent_->RootLayer());
513 EXPECT_EQ(child1_.get(), child1_->RootLayer());
514 EXPECT_EQ(parent_.get(), child2_->RootLayer());
515 EXPECT_EQ(parent_.get(), child3_->RootLayer());
516 EXPECT_EQ(parent_.get(), child4->RootLayer());
517 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
518 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
519 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
521 child2_->ReplaceChild(grand_child3_.get(), child1_);
523 // |grand_child3| gets orphaned and the child1 subtree gets planted back into
524 // the tree under child2.
525 EXPECT_EQ(parent_.get(), parent_->RootLayer());
526 EXPECT_EQ(parent_.get(), child1_->RootLayer());
527 EXPECT_EQ(parent_.get(), child2_->RootLayer());
528 EXPECT_EQ(parent_.get(), child3_->RootLayer());
529 EXPECT_EQ(grand_child3_.get(), child4->RootLayer());
530 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
531 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
532 EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer());
535 TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) {
536 // The semantics for SetNeedsDisplay which are tested here:
537 // 1. sets NeedsDisplay flag appropriately.
538 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
539 // SetNeedsDisplay.
541 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
542 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
543 1, layer_tree_host_->SetRootLayer(test_layer));
544 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
546 gfx::Size test_bounds = gfx::Size(501, 508);
548 gfx::Rect dirty1 = gfx::Rect(10, 15, 1, 2);
549 gfx::Rect dirty2 = gfx::Rect(20, 25, 3, 4);
550 gfx::Rect out_of_bounds_dirty_rect = gfx::Rect(400, 405, 500, 502);
552 // Before anything, test_layer should not be dirty.
553 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
555 // This is just initialization, but SetNeedsCommit behavior is verified anyway
556 // to avoid warnings.
557 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBounds(test_bounds));
558 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
560 // The real test begins here.
561 test_layer->ResetNeedsDisplayForTesting();
562 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
564 // Case 1: Layer should accept dirty rects that go beyond its bounds.
565 test_layer->ResetNeedsDisplayForTesting();
566 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
567 EXPECT_SET_NEEDS_UPDATE(
568 1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect));
569 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
570 test_layer->ResetNeedsDisplayForTesting();
572 // Case 2: SetNeedsDisplay() without the dirty rect arg.
573 test_layer->ResetNeedsDisplayForTesting();
574 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
575 EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay());
576 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
577 test_layer->ResetNeedsDisplayForTesting();
579 // Case 3: SetNeedsDisplay() with an empty rect.
580 test_layer->ResetNeedsDisplayForTesting();
581 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
582 EXPECT_SET_NEEDS_COMMIT(0, test_layer->SetNeedsDisplayRect(gfx::Rect()));
583 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
585 // Case 4: SetNeedsDisplay() with a non-drawable layer
586 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(false));
587 test_layer->ResetNeedsDisplayForTesting();
588 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
589 EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty1));
590 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
593 TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) {
594 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
595 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
596 1, layer_tree_host_->SetRootLayer(test_layer));
597 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
599 scoped_refptr<Layer> dummy_layer1 = Layer::Create(layer_settings_);
600 scoped_refptr<Layer> dummy_layer2 = Layer::Create(layer_settings_);
602 // sanity check of initial test condition
603 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
605 // Next, test properties that should call SetNeedsCommit (but not
606 // SetNeedsDisplay). All properties need to be set to new values in order for
607 // SetNeedsCommit to be called.
608 EXPECT_SET_NEEDS_COMMIT(
609 1, test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f)));
610 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY));
611 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true));
612 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
613 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode));
614 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true));
615 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true));
616 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
617 // We can use any layer pointer here since we aren't syncing for real.
618 EXPECT_SET_NEEDS_COMMIT(1,
619 test_layer->SetScrollClipLayerId(test_layer->id()));
620 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false));
621 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset(
622 gfx::ScrollOffset(10, 10)));
623 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true));
624 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion(
625 Region(gfx::Rect(1, 1, 2, 2))));
626 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true));
627 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true));
628 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(
629 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
630 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false));
631 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion(
632 gfx::Rect(10, 10)));
633 EXPECT_SET_NEEDS_COMMIT(
635 test_layer->SetDrawCheckerboardForMissingTiles(
636 !test_layer->draw_checkerboard_for_missing_tiles()));
637 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true));
638 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true));
640 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(
641 dummy_layer1.get()));
642 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer(
643 dummy_layer2.get()));
645 // The above tests should not have caused a change to the needs_display flag.
646 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
648 // As layers are removed from the tree, they will cause a tree sync.
649 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
652 TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
653 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
654 scoped_ptr<LayerImpl> impl_layer =
655 LayerImpl::Create(host_impl_.active_tree(), 1);
657 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
658 layer_tree_host_->SetRootLayer(test_layer));
660 test_layer->SetNeedsDisplayRect(gfx::Rect(5, 5));
661 test_layer->PushPropertiesTo(impl_layer.get());
662 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f),
663 impl_layer->update_rect());
665 // The LayerImpl's update_rect() should be accumulated here, since we did not
666 // do anything to clear it.
667 test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
668 test_layer->PushPropertiesTo(impl_layer.get());
669 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f),
670 impl_layer->update_rect());
672 // If we do clear the LayerImpl side, then the next update_rect() should be
673 // fresh without accumulation.
674 impl_layer->ResetAllChangeTrackingForSubtree();
675 test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
676 test_layer->PushPropertiesTo(impl_layer.get());
677 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f),
678 impl_layer->update_rect());
681 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
682 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
683 scoped_ptr<LayerImpl> impl_layer =
684 LayerImpl::Create(host_impl_.active_tree(), 1);
686 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
687 layer_tree_host_->SetRootLayer(test_layer));
689 gfx::Transform transform;
690 transform.Rotate(45.0);
691 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
693 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
695 test_layer->PushPropertiesTo(impl_layer.get());
697 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
700 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
701 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
702 scoped_ptr<LayerImpl> impl_layer =
703 LayerImpl::Create(host_impl_.active_tree(), 1);
705 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
706 layer_tree_host_->SetRootLayer(test_layer));
708 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
710 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
712 test_layer->PushPropertiesTo(impl_layer.get());
714 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
717 TEST_F(LayerTest,
718 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) {
719 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
720 scoped_ptr<LayerImpl> impl_layer =
721 LayerImpl::Create(host_impl_.active_tree(), 1);
723 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
724 layer_tree_host_->SetRootLayer(test_layer));
726 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
727 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
728 registrar.get());
730 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
731 1.0,
733 100);
735 gfx::Transform transform;
736 transform.Rotate(45.0);
737 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
739 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
740 test_layer->PushPropertiesTo(impl_layer.get());
741 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
743 impl_layer->ResetAllChangeTrackingForSubtree();
744 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
745 1.0,
747 100);
748 impl_layer->layer_animation_controller()
749 ->GetAnimation(Animation::TRANSFORM)
750 ->set_is_impl_only(true);
751 transform.Rotate(45.0);
752 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
754 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
755 test_layer->PushPropertiesTo(impl_layer.get());
756 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
759 TEST_F(LayerTest,
760 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) {
761 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
762 scoped_ptr<LayerImpl> impl_layer =
763 LayerImpl::Create(host_impl_.active_tree(), 1);
765 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
766 layer_tree_host_->SetRootLayer(test_layer));
768 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
769 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
770 registrar.get());
772 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
773 1.0,
774 0.3f,
775 0.7f,
776 false);
778 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
780 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
781 test_layer->PushPropertiesTo(impl_layer.get());
782 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
784 impl_layer->ResetAllChangeTrackingForSubtree();
785 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
786 1.0,
787 0.3f,
788 0.7f,
789 false);
790 impl_layer->layer_animation_controller()
791 ->GetAnimation(Animation::OPACITY)
792 ->set_is_impl_only(true);
793 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f));
795 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
796 test_layer->PushPropertiesTo(impl_layer.get());
797 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
800 TEST_F(LayerTest,
801 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) {
802 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
803 scoped_ptr<LayerImpl> impl_layer =
804 LayerImpl::Create(host_impl_.active_tree(), 1);
806 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
807 layer_tree_host_->SetRootLayer(test_layer));
809 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
810 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
811 registrar.get());
813 AddAnimatedFilterToController(
814 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
816 FilterOperations filters;
817 filters.Append(FilterOperation::CreateBlurFilter(2.f));
818 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
820 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
821 test_layer->PushPropertiesTo(impl_layer.get());
822 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
824 impl_layer->ResetAllChangeTrackingForSubtree();
825 AddAnimatedFilterToController(
826 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
827 impl_layer->layer_animation_controller()
828 ->GetAnimation(Animation::FILTER)
829 ->set_is_impl_only(true);
830 filters.Append(FilterOperation::CreateSepiaFilter(0.5f));
831 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
833 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
834 test_layer->PushPropertiesTo(impl_layer.get());
835 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
838 TEST_F(LayerTest, MaskAndReplicaHasParent) {
839 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
840 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
841 scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
842 scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
843 scoped_refptr<Layer> replica_mask = Layer::Create(layer_settings_);
844 scoped_refptr<Layer> mask_replacement = Layer::Create(layer_settings_);
845 scoped_refptr<Layer> replica_replacement = Layer::Create(layer_settings_);
846 scoped_refptr<Layer> replica_mask_replacement =
847 Layer::Create(layer_settings_);
849 parent->AddChild(child);
850 child->SetMaskLayer(mask.get());
851 child->SetReplicaLayer(replica.get());
852 replica->SetMaskLayer(replica_mask.get());
854 EXPECT_EQ(parent.get(), child->parent());
855 EXPECT_EQ(child.get(), mask->parent());
856 EXPECT_EQ(child.get(), replica->parent());
857 EXPECT_EQ(replica.get(), replica_mask->parent());
859 replica->SetMaskLayer(replica_mask_replacement.get());
860 EXPECT_EQ(nullptr, replica_mask->parent());
861 EXPECT_EQ(replica.get(), replica_mask_replacement->parent());
863 child->SetMaskLayer(mask_replacement.get());
864 EXPECT_EQ(nullptr, mask->parent());
865 EXPECT_EQ(child.get(), mask_replacement->parent());
867 child->SetReplicaLayer(replica_replacement.get());
868 EXPECT_EQ(nullptr, replica->parent());
869 EXPECT_EQ(child.get(), replica_replacement->parent());
871 EXPECT_EQ(replica.get(), replica->mask_layer()->parent());
874 TEST_F(LayerTest, CheckTranformIsInvertible) {
875 scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
876 scoped_ptr<LayerImpl> impl_layer =
877 LayerImpl::Create(host_impl_.active_tree(), 1);
878 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
879 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
880 layer_tree_host_->SetRootLayer(layer);
882 EXPECT_TRUE(layer->transform_is_invertible());
884 gfx::Transform singular_transform;
885 singular_transform.Scale3d(
886 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
888 layer->SetTransform(singular_transform);
889 layer->PushPropertiesTo(impl_layer.get());
891 EXPECT_FALSE(layer->transform_is_invertible());
892 EXPECT_FALSE(impl_layer->transform_is_invertible());
894 gfx::Transform rotation_transform;
895 rotation_transform.RotateAboutZAxis(-45.0);
897 layer->SetTransform(rotation_transform);
898 layer->PushPropertiesTo(impl_layer.get());
899 EXPECT_TRUE(layer->transform_is_invertible());
900 EXPECT_TRUE(impl_layer->transform_is_invertible());
902 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
905 TEST_F(LayerTest, TranformIsInvertibleAnimation) {
906 scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
907 scoped_ptr<LayerImpl> impl_layer =
908 LayerImpl::Create(host_impl_.active_tree(), 1);
909 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
910 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
911 layer_tree_host_->SetRootLayer(layer);
913 EXPECT_TRUE(layer->transform_is_invertible());
915 gfx::Transform singular_transform;
916 singular_transform.Scale3d(
917 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
919 layer->SetTransform(singular_transform);
920 layer->PushPropertiesTo(impl_layer.get());
922 EXPECT_FALSE(layer->transform_is_invertible());
923 EXPECT_FALSE(impl_layer->transform_is_invertible());
925 gfx::Transform identity_transform;
927 layer->SetTransform(identity_transform);
928 static_cast<LayerAnimationValueObserver*>(layer.get())
929 ->OnTransformAnimated(singular_transform);
930 layer->PushPropertiesTo(impl_layer.get());
931 EXPECT_FALSE(layer->transform_is_invertible());
932 EXPECT_FALSE(impl_layer->transform_is_invertible());
934 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
937 class LayerTreeHostFactory {
938 public:
939 LayerTreeHostFactory() : client_(FakeLayerTreeHostClient::DIRECT_3D) {}
941 scoped_ptr<LayerTreeHost> Create() { return Create(LayerTreeSettings()); }
943 scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) {
944 LayerTreeHost::InitParams params;
945 params.client = &client_;
946 params.shared_bitmap_manager = &shared_bitmap_manager_;
947 params.task_graph_runner = &task_graph_runner_;
948 params.gpu_memory_buffer_manager = &gpu_memory_buffer_manager_;
949 params.settings = &settings;
950 params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
951 return LayerTreeHost::CreateSingleThreaded(&client_, &params);
954 private:
955 FakeLayerTreeHostClient client_;
956 TestSharedBitmapManager shared_bitmap_manager_;
957 TestTaskGraphRunner task_graph_runner_;
958 TestGpuMemoryBufferManager gpu_memory_buffer_manager_;
961 void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) {
962 EXPECT_EQ(host, layer->layer_tree_host());
964 for (size_t i = 0; i < layer->children().size(); ++i)
965 AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
967 if (layer->mask_layer())
968 AssertLayerTreeHostMatchesForSubtree(layer->mask_layer(), host);
970 if (layer->replica_layer())
971 AssertLayerTreeHostMatchesForSubtree(layer->replica_layer(), host);
974 class LayerLayerTreeHostTest : public testing::Test {
975 public:
976 protected:
977 LayerSettings layer_settings_;
980 TEST_F(LayerLayerTreeHostTest, EnteringTree) {
981 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
982 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
983 scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
984 scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
985 scoped_refptr<Layer> replica_mask = Layer::Create(layer_settings_);
987 // Set up a detached tree of layers. The host pointer should be nil for these
988 // layers.
989 parent->AddChild(child);
990 child->SetMaskLayer(mask.get());
991 child->SetReplicaLayer(replica.get());
992 replica->SetMaskLayer(replica_mask.get());
994 AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
996 LayerTreeHostFactory factory;
997 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
998 // Setting the root layer should set the host pointer for all layers in the
999 // tree.
1000 layer_tree_host->SetRootLayer(parent.get());
1002 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1004 // Clearing the root layer should also clear out the host pointers for all
1005 // layers in the tree.
1006 layer_tree_host->SetRootLayer(nullptr);
1008 AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
1011 TEST_F(LayerLayerTreeHostTest, AddingLayerSubtree) {
1012 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
1013 LayerTreeHostFactory factory;
1014 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1016 layer_tree_host->SetRootLayer(parent.get());
1018 EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get());
1020 // Adding a subtree to a layer already associated with a host should set the
1021 // host pointer on all layers in that subtree.
1022 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
1023 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings_);
1024 child->AddChild(grand_child);
1026 // Masks, replicas, and replica masks should pick up the new host too.
1027 scoped_refptr<Layer> child_mask = Layer::Create(layer_settings_);
1028 child->SetMaskLayer(child_mask.get());
1029 scoped_refptr<Layer> child_replica = Layer::Create(layer_settings_);
1030 child->SetReplicaLayer(child_replica.get());
1031 scoped_refptr<Layer> child_replica_mask = Layer::Create(layer_settings_);
1032 child_replica->SetMaskLayer(child_replica_mask.get());
1034 parent->AddChild(child);
1035 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1037 layer_tree_host->SetRootLayer(nullptr);
1040 TEST_F(LayerLayerTreeHostTest, ChangeHost) {
1041 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
1042 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
1043 scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
1044 scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
1045 scoped_refptr<Layer> replica_mask = Layer::Create(layer_settings_);
1047 // Same setup as the previous test.
1048 parent->AddChild(child);
1049 child->SetMaskLayer(mask.get());
1050 child->SetReplicaLayer(replica.get());
1051 replica->SetMaskLayer(replica_mask.get());
1053 LayerTreeHostFactory factory;
1054 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1055 first_layer_tree_host->SetRootLayer(parent.get());
1057 AssertLayerTreeHostMatchesForSubtree(parent.get(),
1058 first_layer_tree_host.get());
1060 // Now re-root the tree to a new host (simulating what we do on a context lost
1061 // event). This should update the host pointers for all layers in the tree.
1062 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1063 second_layer_tree_host->SetRootLayer(parent.get());
1065 AssertLayerTreeHostMatchesForSubtree(parent.get(),
1066 second_layer_tree_host.get());
1068 second_layer_tree_host->SetRootLayer(nullptr);
1071 TEST_F(LayerLayerTreeHostTest, ChangeHostInSubtree) {
1072 scoped_refptr<Layer> first_parent = Layer::Create(layer_settings_);
1073 scoped_refptr<Layer> first_child = Layer::Create(layer_settings_);
1074 scoped_refptr<Layer> second_parent = Layer::Create(layer_settings_);
1075 scoped_refptr<Layer> second_child = Layer::Create(layer_settings_);
1076 scoped_refptr<Layer> second_grand_child = Layer::Create(layer_settings_);
1078 // First put all children under the first parent and set the first host.
1079 first_parent->AddChild(first_child);
1080 second_child->AddChild(second_grand_child);
1081 first_parent->AddChild(second_child);
1083 LayerTreeHostFactory factory;
1084 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1085 first_layer_tree_host->SetRootLayer(first_parent.get());
1087 AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
1088 first_layer_tree_host.get());
1090 // Now reparent the subtree starting at second_child to a layer in a different
1091 // tree.
1092 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1093 second_layer_tree_host->SetRootLayer(second_parent.get());
1095 second_parent->AddChild(second_child);
1097 // The moved layer and its children should point to the new host.
1098 EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
1099 EXPECT_EQ(second_layer_tree_host.get(),
1100 second_grand_child->layer_tree_host());
1102 // Test over, cleanup time.
1103 first_layer_tree_host->SetRootLayer(nullptr);
1104 second_layer_tree_host->SetRootLayer(nullptr);
1107 TEST_F(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) {
1108 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
1109 scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
1110 scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
1111 scoped_refptr<Layer> mask_child = Layer::Create(layer_settings_);
1112 scoped_refptr<Layer> replica_child = Layer::Create(layer_settings_);
1113 scoped_refptr<Layer> mask_replacement = Layer::Create(layer_settings_);
1114 scoped_refptr<Layer> replica_replacement = Layer::Create(layer_settings_);
1116 parent->SetMaskLayer(mask.get());
1117 parent->SetReplicaLayer(replica.get());
1118 mask->AddChild(mask_child);
1119 replica->AddChild(replica_child);
1121 LayerTreeHostFactory factory;
1122 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1123 layer_tree_host->SetRootLayer(parent.get());
1125 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1127 // Replacing the mask should clear out the old mask's subtree's host pointers.
1128 parent->SetMaskLayer(mask_replacement.get());
1129 EXPECT_EQ(nullptr, mask->layer_tree_host());
1130 EXPECT_EQ(nullptr, mask_child->layer_tree_host());
1132 // Same for replacing a replica layer.
1133 parent->SetReplicaLayer(replica_replacement.get());
1134 EXPECT_EQ(nullptr, replica->layer_tree_host());
1135 EXPECT_EQ(nullptr, replica_child->layer_tree_host());
1137 // Test over, cleanup time.
1138 layer_tree_host->SetRootLayer(nullptr);
1141 TEST_F(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
1142 scoped_refptr<Layer> root = Layer::Create(layer_settings_);
1143 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
1144 root->AddChild(child);
1145 LayerTreeHostFactory factory;
1146 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1147 layer_tree_host->SetRootLayer(root);
1150 static bool AddTestAnimation(Layer* layer) {
1151 scoped_ptr<KeyframedFloatAnimationCurve> curve =
1152 KeyframedFloatAnimationCurve::Create();
1153 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.3f, nullptr));
1154 curve->AddKeyframe(
1155 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 0.7f, nullptr));
1156 scoped_ptr<Animation> animation =
1157 Animation::Create(curve.Pass(), 0, 0, Animation::OPACITY);
1159 return layer->AddAnimation(animation.Pass());
1162 TEST_F(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) {
1163 scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
1165 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1166 // animation should not be accepted.
1167 EXPECT_FALSE(AddTestAnimation(layer.get()));
1169 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
1170 layer->RegisterForAnimations(registrar.get());
1172 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1173 EXPECT_TRUE(AddTestAnimation(layer.get()));
1175 LayerTreeSettings settings;
1176 settings.accelerated_animation_enabled = false;
1177 LayerTreeHostFactory factory;
1178 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings);
1179 layer_tree_host->SetRootLayer(layer);
1180 AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get());
1182 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1183 // animation should be rejected.
1184 EXPECT_FALSE(AddTestAnimation(layer.get()));
1187 TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
1188 LayerTreeHostFactory factory;
1189 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1191 scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
1192 layer_tree_host->SetRootLayer(layer);
1194 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
1195 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
1196 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
1197 layer->SetContentsOpaque(!!contents_opaque);
1198 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
1199 : SK_ColorTRANSPARENT);
1200 layer_tree_host->set_background_color(
1201 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
1203 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
1204 if (contents_opaque) {
1205 EXPECT_EQ(SkColorGetA(safe_color), 255u)
1206 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1207 << host_opaque << "\n";
1208 } else {
1209 EXPECT_NE(SkColorGetA(safe_color), 255u)
1210 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1211 << host_opaque << "\n";
1218 class DrawsContentChangeLayer : public Layer {
1219 public:
1220 static scoped_refptr<DrawsContentChangeLayer> Create(
1221 const LayerSettings& settings) {
1222 return make_scoped_refptr(new DrawsContentChangeLayer(settings));
1225 void SetLayerTreeHost(LayerTreeHost* host) override {
1226 Layer::SetLayerTreeHost(host);
1227 SetFakeDrawsContent(!fake_draws_content_);
1230 bool HasDrawableContent() const override {
1231 return fake_draws_content_ && Layer::HasDrawableContent();
1234 void SetFakeDrawsContent(bool fake_draws_content) {
1235 fake_draws_content_ = fake_draws_content;
1236 UpdateDrawsContent(HasDrawableContent());
1239 private:
1240 explicit DrawsContentChangeLayer(const LayerSettings& settings)
1241 : Layer(settings), fake_draws_content_(false) {}
1242 ~DrawsContentChangeLayer() override {}
1244 bool fake_draws_content_;
1247 TEST_F(LayerTest, DrawsContentChangedInSetLayerTreeHost) {
1248 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings_);
1249 scoped_refptr<DrawsContentChangeLayer> becomes_not_draws_content =
1250 DrawsContentChangeLayer::Create(layer_settings_);
1251 scoped_refptr<DrawsContentChangeLayer> becomes_draws_content =
1252 DrawsContentChangeLayer::Create(layer_settings_);
1253 root_layer->SetIsDrawable(true);
1254 becomes_not_draws_content->SetIsDrawable(true);
1255 becomes_not_draws_content->SetFakeDrawsContent(true);
1256 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1257 root_layer->AddChild(becomes_not_draws_content);
1258 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1260 becomes_draws_content->SetIsDrawable(true);
1261 root_layer->AddChild(becomes_draws_content);
1262 EXPECT_EQ(1, root_layer->NumDescendantsThatDrawContent());
1265 void ReceiveCopyOutputResult(int* result_count,
1266 scoped_ptr<CopyOutputResult> result) {
1267 ++(*result_count);
1270 TEST_F(LayerTest, DedupesCopyOutputRequestsBySource) {
1271 scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
1272 int result_count = 0;
1274 // Create identical requests without the source being set, and expect the
1275 // layer does not abort either one.
1276 scoped_ptr<CopyOutputRequest> request = CopyOutputRequest::CreateRequest(
1277 base::Bind(&ReceiveCopyOutputResult, &result_count));
1278 layer->RequestCopyOfOutput(request.Pass());
1279 EXPECT_EQ(0, result_count);
1280 request = CopyOutputRequest::CreateRequest(
1281 base::Bind(&ReceiveCopyOutputResult, &result_count));
1282 layer->RequestCopyOfOutput(request.Pass());
1283 EXPECT_EQ(0, result_count);
1285 // When the layer is destroyed, expect both requests to be aborted.
1286 layer = nullptr;
1287 EXPECT_EQ(2, result_count);
1289 layer = Layer::Create(layer_settings_);
1290 result_count = 0;
1292 // Create identical requests, but this time the source is being set. Expect
1293 // the first request from |this| source aborts immediately when the second
1294 // request from |this| source is made.
1295 int did_receive_first_result_from_this_source = 0;
1296 request = CopyOutputRequest::CreateRequest(base::Bind(
1297 &ReceiveCopyOutputResult, &did_receive_first_result_from_this_source));
1298 request->set_source(this);
1299 layer->RequestCopyOfOutput(request.Pass());
1300 EXPECT_EQ(0, did_receive_first_result_from_this_source);
1301 // Make a request from a different source.
1302 int did_receive_result_from_different_source = 0;
1303 request = CopyOutputRequest::CreateRequest(base::Bind(
1304 &ReceiveCopyOutputResult, &did_receive_result_from_different_source));
1305 request->set_source(reinterpret_cast<void*>(0xdeadbee0));
1306 layer->RequestCopyOfOutput(request.Pass());
1307 EXPECT_EQ(0, did_receive_result_from_different_source);
1308 // Make a request without specifying the source.
1309 int did_receive_result_from_anonymous_source = 0;
1310 request = CopyOutputRequest::CreateRequest(base::Bind(
1311 &ReceiveCopyOutputResult, &did_receive_result_from_anonymous_source));
1312 layer->RequestCopyOfOutput(request.Pass());
1313 EXPECT_EQ(0, did_receive_result_from_anonymous_source);
1314 // Make the second request from |this| source.
1315 int did_receive_second_result_from_this_source = 0;
1316 request = CopyOutputRequest::CreateRequest(base::Bind(
1317 &ReceiveCopyOutputResult, &did_receive_second_result_from_this_source));
1318 request->set_source(this);
1319 layer->RequestCopyOfOutput(request.Pass()); // First request to be aborted.
1320 EXPECT_EQ(1, did_receive_first_result_from_this_source);
1321 EXPECT_EQ(0, did_receive_result_from_different_source);
1322 EXPECT_EQ(0, did_receive_result_from_anonymous_source);
1323 EXPECT_EQ(0, did_receive_second_result_from_this_source);
1325 // When the layer is destroyed, the other three requests should be aborted.
1326 layer = nullptr;
1327 EXPECT_EQ(1, did_receive_first_result_from_this_source);
1328 EXPECT_EQ(1, did_receive_result_from_different_source);
1329 EXPECT_EQ(1, did_receive_result_from_anonymous_source);
1330 EXPECT_EQ(1, did_receive_second_result_from_this_source);
1333 } // namespace
1334 } // namespace cc