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 "cc/animation/keyframed_animation_curve.h"
8 #include "cc/base/math_util.h"
9 #include "cc/layers/layer_impl.h"
10 #include "cc/output/copy_output_request.h"
11 #include "cc/output/copy_output_result.h"
12 #include "cc/resources/layer_painter.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
;
34 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
36 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
38 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
44 class MockLayerTreeHost
: public LayerTreeHost
{
46 explicit MockLayerTreeHost(FakeLayerTreeHostClient
* client
)
47 : LayerTreeHost(client
, nullptr, nullptr, nullptr, LayerTreeSettings()) {
48 InitializeSingleThreaded(client
,
49 base::MessageLoopProxy::current(),
53 MOCK_METHOD0(SetNeedsCommit
, void());
54 MOCK_METHOD0(SetNeedsUpdateLayers
, void());
55 MOCK_METHOD0(SetNeedsFullTreeSync
, void());
58 class MockLayerPainter
: public LayerPainter
{
60 void Paint(SkCanvas
* canvas
, const gfx::Rect
& content_rect
) override
{}
63 class LayerTest
: public testing::Test
{
66 : host_impl_(&proxy_
, &shared_bitmap_manager_
, &task_graph_runner_
),
67 fake_client_(FakeLayerTreeHostClient::DIRECT_3D
) {}
70 void SetUp() override
{
71 layer_tree_host_
.reset(new StrictMock
<MockLayerTreeHost
>(&fake_client_
));
74 void TearDown() override
{
75 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
76 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AnyNumber());
81 grand_child1_
= nullptr;
82 grand_child2_
= nullptr;
83 grand_child3_
= nullptr;
85 layer_tree_host_
->SetRootLayer(nullptr);
86 layer_tree_host_
= nullptr;
89 void VerifyTestTreeInitialState() const {
90 ASSERT_EQ(3U, parent_
->children().size());
91 EXPECT_EQ(child1_
, parent_
->children()[0]);
92 EXPECT_EQ(child2_
, parent_
->children()[1]);
93 EXPECT_EQ(child3_
, parent_
->children()[2]);
94 EXPECT_EQ(parent_
.get(), child1_
->parent());
95 EXPECT_EQ(parent_
.get(), child2_
->parent());
96 EXPECT_EQ(parent_
.get(), child3_
->parent());
98 ASSERT_EQ(2U, child1_
->children().size());
99 EXPECT_EQ(grand_child1_
, child1_
->children()[0]);
100 EXPECT_EQ(grand_child2_
, child1_
->children()[1]);
101 EXPECT_EQ(child1_
.get(), grand_child1_
->parent());
102 EXPECT_EQ(child1_
.get(), grand_child2_
->parent());
104 ASSERT_EQ(1U, child2_
->children().size());
105 EXPECT_EQ(grand_child3_
, child2_
->children()[0]);
106 EXPECT_EQ(child2_
.get(), grand_child3_
->parent());
108 ASSERT_EQ(0U, child3_
->children().size());
111 void CreateSimpleTestTree() {
112 parent_
= Layer::Create();
113 child1_
= Layer::Create();
114 child2_
= Layer::Create();
115 child3_
= Layer::Create();
116 grand_child1_
= Layer::Create();
117 grand_child2_
= Layer::Create();
118 grand_child3_
= Layer::Create();
120 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AnyNumber());
121 layer_tree_host_
->SetRootLayer(parent_
);
123 parent_
->AddChild(child1_
);
124 parent_
->AddChild(child2_
);
125 parent_
->AddChild(child3_
);
126 child1_
->AddChild(grand_child1_
);
127 child1_
->AddChild(grand_child2_
);
128 child2_
->AddChild(grand_child3_
);
130 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
132 VerifyTestTreeInitialState();
135 FakeImplProxy proxy_
;
136 TestSharedBitmapManager shared_bitmap_manager_
;
137 TestTaskGraphRunner task_graph_runner_
;
138 FakeLayerTreeHostImpl host_impl_
;
140 FakeLayerTreeHostClient fake_client_
;
141 scoped_ptr
<StrictMock
<MockLayerTreeHost
>> layer_tree_host_
;
142 scoped_refptr
<Layer
> parent_
;
143 scoped_refptr
<Layer
> child1_
;
144 scoped_refptr
<Layer
> child2_
;
145 scoped_refptr
<Layer
> child3_
;
146 scoped_refptr
<Layer
> grand_child1_
;
147 scoped_refptr
<Layer
> grand_child2_
;
148 scoped_refptr
<Layer
> grand_child3_
;
151 TEST_F(LayerTest
, BasicCreateAndDestroy
) {
152 scoped_refptr
<Layer
> test_layer
= Layer::Create();
153 ASSERT_TRUE(test_layer
.get());
155 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(0);
156 test_layer
->SetLayerTreeHost(layer_tree_host_
.get());
157 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
159 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(0);
160 test_layer
->SetLayerTreeHost(nullptr);
163 TEST_F(LayerTest
, AddAndRemoveChild
) {
164 scoped_refptr
<Layer
> parent
= Layer::Create();
165 scoped_refptr
<Layer
> child
= Layer::Create();
167 // Upon creation, layers should not have children or parent.
168 ASSERT_EQ(0U, parent
->children().size());
169 EXPECT_FALSE(child
->parent());
171 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
172 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->AddChild(child
));
174 ASSERT_EQ(1U, parent
->children().size());
175 EXPECT_EQ(child
.get(), parent
->children()[0].get());
176 EXPECT_EQ(parent
.get(), child
->parent());
177 EXPECT_EQ(parent
.get(), child
->RootLayer());
179 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child
->RemoveFromParent());
182 TEST_F(LayerTest
, AddSameChildTwice
) {
183 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AtLeast(1));
185 scoped_refptr
<Layer
> parent
= Layer::Create();
186 scoped_refptr
<Layer
> child
= Layer::Create();
188 layer_tree_host_
->SetRootLayer(parent
);
190 ASSERT_EQ(0u, parent
->children().size());
192 parent
->AddChild(child
);
193 ASSERT_EQ(1u, parent
->children().size());
194 EXPECT_EQ(parent
.get(), child
->parent());
196 parent
->AddChild(child
);
197 ASSERT_EQ(1u, parent
->children().size());
198 EXPECT_EQ(parent
.get(), child
->parent());
201 TEST_F(LayerTest
, InsertChild
) {
202 scoped_refptr
<Layer
> parent
= Layer::Create();
203 scoped_refptr
<Layer
> child1
= Layer::Create();
204 scoped_refptr
<Layer
> child2
= Layer::Create();
205 scoped_refptr
<Layer
> child3
= Layer::Create();
206 scoped_refptr
<Layer
> child4
= Layer::Create();
208 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
210 ASSERT_EQ(0U, parent
->children().size());
212 // Case 1: inserting to empty list.
213 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child3
, 0));
214 ASSERT_EQ(1U, parent
->children().size());
215 EXPECT_EQ(child3
, parent
->children()[0]);
216 EXPECT_EQ(parent
.get(), child3
->parent());
218 // Case 2: inserting to beginning of list
219 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child1
, 0));
220 ASSERT_EQ(2U, parent
->children().size());
221 EXPECT_EQ(child1
, parent
->children()[0]);
222 EXPECT_EQ(child3
, parent
->children()[1]);
223 EXPECT_EQ(parent
.get(), child1
->parent());
225 // Case 3: inserting to middle of list
226 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child2
, 1));
227 ASSERT_EQ(3U, parent
->children().size());
228 EXPECT_EQ(child1
, parent
->children()[0]);
229 EXPECT_EQ(child2
, parent
->children()[1]);
230 EXPECT_EQ(child3
, parent
->children()[2]);
231 EXPECT_EQ(parent
.get(), child2
->parent());
233 // Case 4: inserting to end of list
234 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child4
, 3));
236 ASSERT_EQ(4U, parent
->children().size());
237 EXPECT_EQ(child1
, parent
->children()[0]);
238 EXPECT_EQ(child2
, parent
->children()[1]);
239 EXPECT_EQ(child3
, parent
->children()[2]);
240 EXPECT_EQ(child4
, parent
->children()[3]);
241 EXPECT_EQ(parent
.get(), child4
->parent());
243 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(nullptr));
246 TEST_F(LayerTest
, InsertChildPastEndOfList
) {
247 scoped_refptr
<Layer
> parent
= Layer::Create();
248 scoped_refptr
<Layer
> child1
= Layer::Create();
249 scoped_refptr
<Layer
> child2
= Layer::Create();
251 ASSERT_EQ(0U, parent
->children().size());
253 // insert to an out-of-bounds index
254 parent
->InsertChild(child1
, 53);
256 ASSERT_EQ(1U, parent
->children().size());
257 EXPECT_EQ(child1
, parent
->children()[0]);
259 // insert another child to out-of-bounds, when list is not already empty.
260 parent
->InsertChild(child2
, 2459);
262 ASSERT_EQ(2U, parent
->children().size());
263 EXPECT_EQ(child1
, parent
->children()[0]);
264 EXPECT_EQ(child2
, parent
->children()[1]);
267 TEST_F(LayerTest
, InsertSameChildTwice
) {
268 scoped_refptr
<Layer
> parent
= Layer::Create();
269 scoped_refptr
<Layer
> child1
= Layer::Create();
270 scoped_refptr
<Layer
> child2
= Layer::Create();
272 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
274 ASSERT_EQ(0U, parent
->children().size());
276 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child1
, 0));
277 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child2
, 1));
279 ASSERT_EQ(2U, parent
->children().size());
280 EXPECT_EQ(child1
, parent
->children()[0]);
281 EXPECT_EQ(child2
, parent
->children()[1]);
283 // Inserting the same child again should cause the child to be removed and
284 // re-inserted at the new location.
285 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent
->InsertChild(child1
, 1));
287 // child1 should now be at the end of the list.
288 ASSERT_EQ(2U, parent
->children().size());
289 EXPECT_EQ(child2
, parent
->children()[0]);
290 EXPECT_EQ(child1
, parent
->children()[1]);
292 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(nullptr));
295 TEST_F(LayerTest
, ReplaceChildWithNewChild
) {
296 CreateSimpleTestTree();
297 scoped_refptr
<Layer
> child4
= Layer::Create();
299 EXPECT_FALSE(child4
->parent());
301 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
302 AtLeast(1), parent_
->ReplaceChild(child2_
.get(), child4
));
303 EXPECT_FALSE(parent_
->NeedsDisplayForTesting());
304 EXPECT_FALSE(child1_
->NeedsDisplayForTesting());
305 EXPECT_FALSE(child2_
->NeedsDisplayForTesting());
306 EXPECT_FALSE(child3_
->NeedsDisplayForTesting());
307 EXPECT_FALSE(child4
->NeedsDisplayForTesting());
309 ASSERT_EQ(static_cast<size_t>(3), parent_
->children().size());
310 EXPECT_EQ(child1_
, parent_
->children()[0]);
311 EXPECT_EQ(child4
, parent_
->children()[1]);
312 EXPECT_EQ(child3_
, parent_
->children()[2]);
313 EXPECT_EQ(parent_
.get(), child4
->parent());
315 EXPECT_FALSE(child2_
->parent());
318 TEST_F(LayerTest
, ReplaceChildWithNewChildThatHasOtherParent
) {
319 CreateSimpleTestTree();
321 // create another simple tree with test_layer and child4.
322 scoped_refptr
<Layer
> test_layer
= Layer::Create();
323 scoped_refptr
<Layer
> child4
= Layer::Create();
324 test_layer
->AddChild(child4
);
325 ASSERT_EQ(1U, test_layer
->children().size());
326 EXPECT_EQ(child4
, test_layer
->children()[0]);
327 EXPECT_EQ(test_layer
.get(), child4
->parent());
329 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
330 AtLeast(1), parent_
->ReplaceChild(child2_
.get(), child4
));
332 ASSERT_EQ(3U, parent_
->children().size());
333 EXPECT_EQ(child1_
, parent_
->children()[0]);
334 EXPECT_EQ(child4
, parent_
->children()[1]);
335 EXPECT_EQ(child3_
, parent_
->children()[2]);
336 EXPECT_EQ(parent_
.get(), child4
->parent());
338 // test_layer should no longer have child4,
339 // and child2 should no longer have a parent.
340 ASSERT_EQ(0U, test_layer
->children().size());
341 EXPECT_FALSE(child2_
->parent());
344 TEST_F(LayerTest
, DeleteRemovedScrollParent
) {
345 scoped_refptr
<Layer
> parent
= Layer::Create();
346 scoped_refptr
<Layer
> child1
= Layer::Create();
347 scoped_refptr
<Layer
> child2
= Layer::Create();
349 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
351 ASSERT_EQ(0U, parent
->children().size());
353 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child1
, 0));
354 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child2
, 1));
356 ASSERT_EQ(2U, parent
->children().size());
357 EXPECT_EQ(child1
, parent
->children()[0]);
358 EXPECT_EQ(child2
, parent
->children()[1]);
360 EXPECT_SET_NEEDS_COMMIT(2, child1
->SetScrollParent(child2
.get()));
362 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child2
->RemoveFromParent());
364 child1
->reset_needs_push_properties_for_testing();
366 EXPECT_SET_NEEDS_COMMIT(1, child2
= nullptr);
368 EXPECT_TRUE(child1
->needs_push_properties());
370 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(nullptr));
373 TEST_F(LayerTest
, DeleteRemovedScrollChild
) {
374 scoped_refptr
<Layer
> parent
= Layer::Create();
375 scoped_refptr
<Layer
> child1
= Layer::Create();
376 scoped_refptr
<Layer
> child2
= Layer::Create();
378 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
380 ASSERT_EQ(0U, parent
->children().size());
382 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child1
, 0));
383 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child2
, 1));
385 ASSERT_EQ(2U, parent
->children().size());
386 EXPECT_EQ(child1
, parent
->children()[0]);
387 EXPECT_EQ(child2
, parent
->children()[1]);
389 EXPECT_SET_NEEDS_COMMIT(2, child1
->SetScrollParent(child2
.get()));
391 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child1
->RemoveFromParent());
393 child2
->reset_needs_push_properties_for_testing();
395 EXPECT_SET_NEEDS_COMMIT(1, child1
= nullptr);
397 EXPECT_TRUE(child2
->needs_push_properties());
399 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(nullptr));
402 TEST_F(LayerTest
, ReplaceChildWithSameChild
) {
403 CreateSimpleTestTree();
405 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
407 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(0);
408 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(0);
409 parent_
->ReplaceChild(child2_
.get(), child2_
);
411 VerifyTestTreeInitialState();
414 TEST_F(LayerTest
, RemoveAllChildren
) {
415 CreateSimpleTestTree();
417 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_
->RemoveAllChildren());
419 ASSERT_EQ(0U, parent_
->children().size());
420 EXPECT_FALSE(child1_
->parent());
421 EXPECT_FALSE(child2_
->parent());
422 EXPECT_FALSE(child3_
->parent());
425 TEST_F(LayerTest
, SetChildren
) {
426 scoped_refptr
<Layer
> old_parent
= Layer::Create();
427 scoped_refptr
<Layer
> new_parent
= Layer::Create();
429 scoped_refptr
<Layer
> child1
= Layer::Create();
430 scoped_refptr
<Layer
> child2
= Layer::Create();
432 LayerList new_children
;
433 new_children
.push_back(child1
);
434 new_children
.push_back(child2
);
436 // Set up and verify initial test conditions: child1 has a parent, child2 has
438 old_parent
->AddChild(child1
);
439 ASSERT_EQ(0U, new_parent
->children().size());
440 EXPECT_EQ(old_parent
.get(), child1
->parent());
441 EXPECT_FALSE(child2
->parent());
443 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
444 1, layer_tree_host_
->SetRootLayer(new_parent
));
446 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
447 AtLeast(1), new_parent
->SetChildren(new_children
));
449 ASSERT_EQ(2U, new_parent
->children().size());
450 EXPECT_EQ(new_parent
.get(), child1
->parent());
451 EXPECT_EQ(new_parent
.get(), child2
->parent());
453 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(nullptr));
456 TEST_F(LayerTest
, HasAncestor
) {
457 scoped_refptr
<Layer
> parent
= Layer::Create();
458 EXPECT_FALSE(parent
->HasAncestor(parent
.get()));
460 scoped_refptr
<Layer
> child
= Layer::Create();
461 parent
->AddChild(child
);
463 EXPECT_FALSE(child
->HasAncestor(child
.get()));
464 EXPECT_TRUE(child
->HasAncestor(parent
.get()));
465 EXPECT_FALSE(parent
->HasAncestor(child
.get()));
467 scoped_refptr
<Layer
> child_child
= Layer::Create();
468 child
->AddChild(child_child
);
470 EXPECT_FALSE(child_child
->HasAncestor(child_child
.get()));
471 EXPECT_TRUE(child_child
->HasAncestor(parent
.get()));
472 EXPECT_TRUE(child_child
->HasAncestor(child
.get()));
473 EXPECT_FALSE(parent
->HasAncestor(child
.get()));
474 EXPECT_FALSE(parent
->HasAncestor(child_child
.get()));
477 TEST_F(LayerTest
, GetRootLayerAfterTreeManipulations
) {
478 CreateSimpleTestTree();
480 // For this test we don't care about SetNeedsFullTreeSync calls.
481 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AnyNumber());
483 scoped_refptr
<Layer
> child4
= Layer::Create();
485 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
486 EXPECT_EQ(parent_
.get(), child1_
->RootLayer());
487 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
488 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
489 EXPECT_EQ(child4
.get(), child4
->RootLayer());
490 EXPECT_EQ(parent_
.get(), grand_child1_
->RootLayer());
491 EXPECT_EQ(parent_
.get(), grand_child2_
->RootLayer());
492 EXPECT_EQ(parent_
.get(), grand_child3_
->RootLayer());
494 child1_
->RemoveFromParent();
496 // |child1| and its children, grand_child1 and grand_child2 are now on a
498 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
499 EXPECT_EQ(child1_
.get(), child1_
->RootLayer());
500 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
501 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
502 EXPECT_EQ(child4
.get(), child4
->RootLayer());
503 EXPECT_EQ(child1_
.get(), grand_child1_
->RootLayer());
504 EXPECT_EQ(child1_
.get(), grand_child2_
->RootLayer());
505 EXPECT_EQ(parent_
.get(), grand_child3_
->RootLayer());
507 grand_child3_
->AddChild(child4
);
509 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
510 EXPECT_EQ(child1_
.get(), child1_
->RootLayer());
511 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
512 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
513 EXPECT_EQ(parent_
.get(), child4
->RootLayer());
514 EXPECT_EQ(child1_
.get(), grand_child1_
->RootLayer());
515 EXPECT_EQ(child1_
.get(), grand_child2_
->RootLayer());
516 EXPECT_EQ(parent_
.get(), grand_child3_
->RootLayer());
518 child2_
->ReplaceChild(grand_child3_
.get(), child1_
);
520 // |grand_child3| gets orphaned and the child1 subtree gets planted back into
521 // the tree under child2.
522 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
523 EXPECT_EQ(parent_
.get(), child1_
->RootLayer());
524 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
525 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
526 EXPECT_EQ(grand_child3_
.get(), child4
->RootLayer());
527 EXPECT_EQ(parent_
.get(), grand_child1_
->RootLayer());
528 EXPECT_EQ(parent_
.get(), grand_child2_
->RootLayer());
529 EXPECT_EQ(grand_child3_
.get(), grand_child3_
->RootLayer());
532 TEST_F(LayerTest
, CheckSetNeedsDisplayCausesCorrectBehavior
) {
533 // The semantics for SetNeedsDisplay which are tested here:
534 // 1. sets NeedsDisplay flag appropriately.
535 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
538 scoped_refptr
<Layer
> test_layer
= Layer::Create();
539 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
540 1, layer_tree_host_
->SetRootLayer(test_layer
));
541 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsDrawable(true));
543 gfx::Size test_bounds
= gfx::Size(501, 508);
545 gfx::Rect dirty1
= gfx::Rect(10, 15, 1, 2);
546 gfx::Rect dirty2
= gfx::Rect(20, 25, 3, 4);
547 gfx::Rect out_of_bounds_dirty_rect
= gfx::Rect(400, 405, 500, 502);
549 // Before anything, test_layer should not be dirty.
550 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
552 // This is just initialization, but SetNeedsCommit behavior is verified anyway
553 // to avoid warnings.
554 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetBounds(test_bounds
));
555 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
557 // The real test begins here.
558 test_layer
->ResetNeedsDisplayForTesting();
559 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
561 // Case 1: Layer should accept dirty rects that go beyond its bounds.
562 test_layer
->ResetNeedsDisplayForTesting();
563 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
564 EXPECT_SET_NEEDS_UPDATE(
565 1, test_layer
->SetNeedsDisplayRect(out_of_bounds_dirty_rect
));
566 EXPECT_TRUE(test_layer
->NeedsDisplayForTesting());
567 test_layer
->ResetNeedsDisplayForTesting();
569 // Case 2: SetNeedsDisplay() without the dirty rect arg.
570 test_layer
->ResetNeedsDisplayForTesting();
571 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
572 EXPECT_SET_NEEDS_UPDATE(1, test_layer
->SetNeedsDisplay());
573 EXPECT_TRUE(test_layer
->NeedsDisplayForTesting());
574 test_layer
->ResetNeedsDisplayForTesting();
576 // Case 3: SetNeedsDisplay() with an empty rect.
577 test_layer
->ResetNeedsDisplayForTesting();
578 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
579 EXPECT_SET_NEEDS_COMMIT(0, test_layer
->SetNeedsDisplayRect(gfx::Rect()));
580 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
582 // Case 4: SetNeedsDisplay() with a non-drawable layer
583 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsDrawable(false));
584 test_layer
->ResetNeedsDisplayForTesting();
585 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
586 EXPECT_SET_NEEDS_UPDATE(0, test_layer
->SetNeedsDisplayRect(dirty1
));
587 EXPECT_TRUE(test_layer
->NeedsDisplayForTesting());
590 TEST_F(LayerTest
, CheckPropertyChangeCausesCorrectBehavior
) {
591 scoped_refptr
<Layer
> test_layer
= Layer::Create();
592 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
593 1, layer_tree_host_
->SetRootLayer(test_layer
));
594 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsDrawable(true));
596 scoped_refptr
<Layer
> dummy_layer1
= Layer::Create();
597 scoped_refptr
<Layer
> dummy_layer2
= Layer::Create();
599 // sanity check of initial test condition
600 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
602 // Next, test properties that should call SetNeedsCommit (but not
603 // SetNeedsDisplay). All properties need to be set to new values in order for
604 // SetNeedsCommit to be called.
605 EXPECT_SET_NEEDS_COMMIT(
606 1, test_layer
->SetTransformOrigin(gfx::Point3F(1.23f
, 4.56f
, 0.f
)));
607 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetBackgroundColor(SK_ColorLTGRAY
));
608 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetMasksToBounds(true));
609 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.5f
));
610 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetBlendMode(SkXfermode::kHue_Mode
));
611 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsRootForIsolatedGroup(true));
612 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetContentsOpaque(true));
613 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetPosition(gfx::PointF(4.f
, 9.f
)));
614 // We can use any layer pointer here since we aren't syncing for real.
615 EXPECT_SET_NEEDS_COMMIT(1,
616 test_layer
->SetScrollClipLayerId(test_layer
->id()));
617 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetUserScrollable(true, false));
618 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetScrollOffset(
619 gfx::ScrollOffset(10, 10)));
620 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetShouldScrollOnMainThread(true));
621 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetNonFastScrollableRegion(
622 Region(gfx::Rect(1, 1, 2, 2))));
623 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetHaveWheelEventHandlers(true));
624 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetHaveScrollEventHandlers(true));
625 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(
626 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
627 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetDoubleSided(false));
628 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTouchEventHandlerRegion(
630 EXPECT_SET_NEEDS_COMMIT(
632 test_layer
->SetDrawCheckerboardForMissingTiles(
633 !test_layer
->draw_checkerboard_for_missing_tiles()));
634 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetForceRenderSurface(true));
635 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetHideLayerAndSubtree(true));
637 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer
->SetMaskLayer(
638 dummy_layer1
.get()));
639 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer
->SetReplicaLayer(
640 dummy_layer2
.get()));
642 // The above tests should not have caused a change to the needs_display flag.
643 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
645 // As layers are removed from the tree, they will cause a tree sync.
646 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times((AnyNumber()));
649 TEST_F(LayerTest
, PushPropertiesAccumulatesUpdateRect
) {
650 scoped_refptr
<Layer
> test_layer
= Layer::Create();
651 scoped_ptr
<LayerImpl
> impl_layer
=
652 LayerImpl::Create(host_impl_
.active_tree(), 1);
654 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
655 layer_tree_host_
->SetRootLayer(test_layer
));
657 test_layer
->SetNeedsDisplayRect(gfx::Rect(5, 5));
658 test_layer
->PushPropertiesTo(impl_layer
.get());
659 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f
, 0.f
, 5.f
, 5.f
),
660 impl_layer
->update_rect());
662 // The LayerImpl's update_rect() should be accumulated here, since we did not
663 // do anything to clear it.
664 test_layer
->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
665 test_layer
->PushPropertiesTo(impl_layer
.get());
666 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f
, 0.f
, 15.f
, 15.f
),
667 impl_layer
->update_rect());
669 // If we do clear the LayerImpl side, then the next update_rect() should be
670 // fresh without accumulation.
671 impl_layer
->ResetAllChangeTrackingForSubtree();
672 test_layer
->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
673 test_layer
->PushPropertiesTo(impl_layer
.get());
674 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f
, 10.f
, 5.f
, 5.f
),
675 impl_layer
->update_rect());
678 TEST_F(LayerTest
, PushPropertiesCausesLayerPropertyChangedForTransform
) {
679 scoped_refptr
<Layer
> test_layer
= Layer::Create();
680 scoped_ptr
<LayerImpl
> impl_layer
=
681 LayerImpl::Create(host_impl_
.active_tree(), 1);
683 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
684 layer_tree_host_
->SetRootLayer(test_layer
));
686 gfx::Transform transform
;
687 transform
.Rotate(45.0);
688 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(transform
));
690 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
692 test_layer
->PushPropertiesTo(impl_layer
.get());
694 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
697 TEST_F(LayerTest
, PushPropertiesCausesLayerPropertyChangedForOpacity
) {
698 scoped_refptr
<Layer
> test_layer
= Layer::Create();
699 scoped_ptr
<LayerImpl
> impl_layer
=
700 LayerImpl::Create(host_impl_
.active_tree(), 1);
702 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
703 layer_tree_host_
->SetRootLayer(test_layer
));
705 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.5f
));
707 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
709 test_layer
->PushPropertiesTo(impl_layer
.get());
711 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
715 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim
) {
716 scoped_refptr
<Layer
> test_layer
= Layer::Create();
717 scoped_ptr
<LayerImpl
> impl_layer
=
718 LayerImpl::Create(host_impl_
.active_tree(), 1);
720 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
721 layer_tree_host_
->SetRootLayer(test_layer
));
723 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
724 impl_layer
->layer_animation_controller()->SetAnimationRegistrar(
727 AddAnimatedTransformToController(impl_layer
->layer_animation_controller(),
732 gfx::Transform transform
;
733 transform
.Rotate(45.0);
734 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(transform
));
736 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
737 test_layer
->PushPropertiesTo(impl_layer
.get());
738 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
740 impl_layer
->ResetAllChangeTrackingForSubtree();
741 AddAnimatedTransformToController(impl_layer
->layer_animation_controller(),
745 impl_layer
->layer_animation_controller()
746 ->GetAnimation(Animation::TRANSFORM
)
747 ->set_is_impl_only(true);
748 transform
.Rotate(45.0);
749 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(transform
));
751 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
752 test_layer
->PushPropertiesTo(impl_layer
.get());
753 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
757 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim
) {
758 scoped_refptr
<Layer
> test_layer
= Layer::Create();
759 scoped_ptr
<LayerImpl
> impl_layer
=
760 LayerImpl::Create(host_impl_
.active_tree(), 1);
762 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
763 layer_tree_host_
->SetRootLayer(test_layer
));
765 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
766 impl_layer
->layer_animation_controller()->SetAnimationRegistrar(
769 AddOpacityTransitionToController(impl_layer
->layer_animation_controller(),
775 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.5f
));
777 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
778 test_layer
->PushPropertiesTo(impl_layer
.get());
779 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
781 impl_layer
->ResetAllChangeTrackingForSubtree();
782 AddOpacityTransitionToController(impl_layer
->layer_animation_controller(),
787 impl_layer
->layer_animation_controller()
788 ->GetAnimation(Animation::OPACITY
)
789 ->set_is_impl_only(true);
790 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.75f
));
792 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
793 test_layer
->PushPropertiesTo(impl_layer
.get());
794 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
798 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim
) {
799 scoped_refptr
<Layer
> test_layer
= Layer::Create();
800 scoped_ptr
<LayerImpl
> impl_layer
=
801 LayerImpl::Create(host_impl_
.active_tree(), 1);
803 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
804 layer_tree_host_
->SetRootLayer(test_layer
));
806 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
807 impl_layer
->layer_animation_controller()->SetAnimationRegistrar(
810 AddAnimatedFilterToController(
811 impl_layer
->layer_animation_controller(), 1.0, 1.f
, 2.f
);
813 FilterOperations filters
;
814 filters
.Append(FilterOperation::CreateBlurFilter(2.f
));
815 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetFilters(filters
));
817 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
818 test_layer
->PushPropertiesTo(impl_layer
.get());
819 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
821 impl_layer
->ResetAllChangeTrackingForSubtree();
822 AddAnimatedFilterToController(
823 impl_layer
->layer_animation_controller(), 1.0, 1.f
, 2.f
);
824 impl_layer
->layer_animation_controller()
825 ->GetAnimation(Animation::FILTER
)
826 ->set_is_impl_only(true);
827 filters
.Append(FilterOperation::CreateSepiaFilter(0.5f
));
828 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetFilters(filters
));
830 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
831 test_layer
->PushPropertiesTo(impl_layer
.get());
832 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
835 TEST_F(LayerTest
, MaskAndReplicaHasParent
) {
836 scoped_refptr
<Layer
> parent
= Layer::Create();
837 scoped_refptr
<Layer
> child
= Layer::Create();
838 scoped_refptr
<Layer
> mask
= Layer::Create();
839 scoped_refptr
<Layer
> replica
= Layer::Create();
840 scoped_refptr
<Layer
> replica_mask
= Layer::Create();
841 scoped_refptr
<Layer
> mask_replacement
= Layer::Create();
842 scoped_refptr
<Layer
> replica_replacement
= Layer::Create();
843 scoped_refptr
<Layer
> replica_mask_replacement
= Layer::Create();
845 parent
->AddChild(child
);
846 child
->SetMaskLayer(mask
.get());
847 child
->SetReplicaLayer(replica
.get());
848 replica
->SetMaskLayer(replica_mask
.get());
850 EXPECT_EQ(parent
.get(), child
->parent());
851 EXPECT_EQ(child
.get(), mask
->parent());
852 EXPECT_EQ(child
.get(), replica
->parent());
853 EXPECT_EQ(replica
.get(), replica_mask
->parent());
855 replica
->SetMaskLayer(replica_mask_replacement
.get());
856 EXPECT_EQ(nullptr, replica_mask
->parent());
857 EXPECT_EQ(replica
.get(), replica_mask_replacement
->parent());
859 child
->SetMaskLayer(mask_replacement
.get());
860 EXPECT_EQ(nullptr, mask
->parent());
861 EXPECT_EQ(child
.get(), mask_replacement
->parent());
863 child
->SetReplicaLayer(replica_replacement
.get());
864 EXPECT_EQ(nullptr, replica
->parent());
865 EXPECT_EQ(child
.get(), replica_replacement
->parent());
867 EXPECT_EQ(replica
.get(), replica
->mask_layer()->parent());
870 TEST_F(LayerTest
, CheckTranformIsInvertible
) {
871 scoped_refptr
<Layer
> layer
= Layer::Create();
872 scoped_ptr
<LayerImpl
> impl_layer
=
873 LayerImpl::Create(host_impl_
.active_tree(), 1);
874 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(1);
875 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(AnyNumber());
876 layer_tree_host_
->SetRootLayer(layer
);
878 EXPECT_TRUE(layer
->transform_is_invertible());
880 gfx::Transform singular_transform
;
881 singular_transform
.Scale3d(
882 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
884 layer
->SetTransform(singular_transform
);
885 layer
->PushPropertiesTo(impl_layer
.get());
887 EXPECT_FALSE(layer
->transform_is_invertible());
888 EXPECT_FALSE(impl_layer
->transform_is_invertible());
890 gfx::Transform rotation_transform
;
891 rotation_transform
.RotateAboutZAxis(-45.0);
893 layer
->SetTransform(rotation_transform
);
894 layer
->PushPropertiesTo(impl_layer
.get());
895 EXPECT_TRUE(layer
->transform_is_invertible());
896 EXPECT_TRUE(impl_layer
->transform_is_invertible());
898 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
901 TEST_F(LayerTest
, TranformIsInvertibleAnimation
) {
902 scoped_refptr
<Layer
> layer
= Layer::Create();
903 scoped_ptr
<LayerImpl
> impl_layer
=
904 LayerImpl::Create(host_impl_
.active_tree(), 1);
905 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(1);
906 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(AnyNumber());
907 layer_tree_host_
->SetRootLayer(layer
);
909 EXPECT_TRUE(layer
->transform_is_invertible());
911 gfx::Transform singular_transform
;
912 singular_transform
.Scale3d(
913 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
915 layer
->SetTransform(singular_transform
);
916 layer
->PushPropertiesTo(impl_layer
.get());
918 EXPECT_FALSE(layer
->transform_is_invertible());
919 EXPECT_FALSE(impl_layer
->transform_is_invertible());
921 gfx::Transform identity_transform
;
923 layer
->SetTransform(identity_transform
);
924 static_cast<LayerAnimationValueObserver
*>(layer
.get())
925 ->OnTransformAnimated(singular_transform
);
926 layer
->PushPropertiesTo(impl_layer
.get());
927 EXPECT_FALSE(layer
->transform_is_invertible());
928 EXPECT_FALSE(impl_layer
->transform_is_invertible());
930 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
933 class LayerTreeHostFactory
{
935 LayerTreeHostFactory()
936 : client_(FakeLayerTreeHostClient::DIRECT_3D
),
937 shared_bitmap_manager_(new TestSharedBitmapManager
),
938 gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager
) {}
940 scoped_ptr
<LayerTreeHost
> Create() {
941 return LayerTreeHost::CreateSingleThreaded(
942 &client_
, &client_
, shared_bitmap_manager_
.get(),
943 gpu_memory_buffer_manager_
.get(), nullptr, LayerTreeSettings(),
944 base::MessageLoopProxy::current(), nullptr);
947 scoped_ptr
<LayerTreeHost
> Create(LayerTreeSettings settings
) {
948 return LayerTreeHost::CreateSingleThreaded(
949 &client_
, &client_
, shared_bitmap_manager_
.get(),
950 gpu_memory_buffer_manager_
.get(), nullptr, settings
,
951 base::MessageLoopProxy::current(), nullptr);
955 FakeLayerTreeHostClient client_
;
956 scoped_ptr
<TestSharedBitmapManager
> shared_bitmap_manager_
;
957 scoped_ptr
<TestGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
960 void AssertLayerTreeHostMatchesForSubtree(Layer
* layer
, LayerTreeHost
* host
) {
961 EXPECT_EQ(host
, layer
->layer_tree_host());
963 for (size_t i
= 0; i
< layer
->children().size(); ++i
)
964 AssertLayerTreeHostMatchesForSubtree(layer
->children()[i
].get(), host
);
966 if (layer
->mask_layer())
967 AssertLayerTreeHostMatchesForSubtree(layer
->mask_layer(), host
);
969 if (layer
->replica_layer())
970 AssertLayerTreeHostMatchesForSubtree(layer
->replica_layer(), host
);
973 TEST(LayerLayerTreeHostTest
, EnteringTree
) {
974 scoped_refptr
<Layer
> parent
= Layer::Create();
975 scoped_refptr
<Layer
> child
= Layer::Create();
976 scoped_refptr
<Layer
> mask
= Layer::Create();
977 scoped_refptr
<Layer
> replica
= Layer::Create();
978 scoped_refptr
<Layer
> replica_mask
= Layer::Create();
980 // Set up a detached tree of layers. The host pointer should be nil for these
982 parent
->AddChild(child
);
983 child
->SetMaskLayer(mask
.get());
984 child
->SetReplicaLayer(replica
.get());
985 replica
->SetMaskLayer(replica_mask
.get());
987 AssertLayerTreeHostMatchesForSubtree(parent
.get(), nullptr);
989 LayerTreeHostFactory factory
;
990 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
991 // Setting the root layer should set the host pointer for all layers in the
993 layer_tree_host
->SetRootLayer(parent
.get());
995 AssertLayerTreeHostMatchesForSubtree(parent
.get(), layer_tree_host
.get());
997 // Clearing the root layer should also clear out the host pointers for all
998 // layers in the tree.
999 layer_tree_host
->SetRootLayer(nullptr);
1001 AssertLayerTreeHostMatchesForSubtree(parent
.get(), nullptr);
1004 TEST(LayerLayerTreeHostTest
, AddingLayerSubtree
) {
1005 scoped_refptr
<Layer
> parent
= Layer::Create();
1006 LayerTreeHostFactory factory
;
1007 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
1009 layer_tree_host
->SetRootLayer(parent
.get());
1011 EXPECT_EQ(parent
->layer_tree_host(), layer_tree_host
.get());
1013 // Adding a subtree to a layer already associated with a host should set the
1014 // host pointer on all layers in that subtree.
1015 scoped_refptr
<Layer
> child
= Layer::Create();
1016 scoped_refptr
<Layer
> grand_child
= Layer::Create();
1017 child
->AddChild(grand_child
);
1019 // Masks, replicas, and replica masks should pick up the new host too.
1020 scoped_refptr
<Layer
> child_mask
= Layer::Create();
1021 child
->SetMaskLayer(child_mask
.get());
1022 scoped_refptr
<Layer
> child_replica
= Layer::Create();
1023 child
->SetReplicaLayer(child_replica
.get());
1024 scoped_refptr
<Layer
> child_replica_mask
= Layer::Create();
1025 child_replica
->SetMaskLayer(child_replica_mask
.get());
1027 parent
->AddChild(child
);
1028 AssertLayerTreeHostMatchesForSubtree(parent
.get(), layer_tree_host
.get());
1030 layer_tree_host
->SetRootLayer(nullptr);
1033 TEST(LayerLayerTreeHostTest
, ChangeHost
) {
1034 scoped_refptr
<Layer
> parent
= Layer::Create();
1035 scoped_refptr
<Layer
> child
= Layer::Create();
1036 scoped_refptr
<Layer
> mask
= Layer::Create();
1037 scoped_refptr
<Layer
> replica
= Layer::Create();
1038 scoped_refptr
<Layer
> replica_mask
= Layer::Create();
1040 // Same setup as the previous test.
1041 parent
->AddChild(child
);
1042 child
->SetMaskLayer(mask
.get());
1043 child
->SetReplicaLayer(replica
.get());
1044 replica
->SetMaskLayer(replica_mask
.get());
1046 LayerTreeHostFactory factory
;
1047 scoped_ptr
<LayerTreeHost
> first_layer_tree_host
= factory
.Create();
1048 first_layer_tree_host
->SetRootLayer(parent
.get());
1050 AssertLayerTreeHostMatchesForSubtree(parent
.get(),
1051 first_layer_tree_host
.get());
1053 // Now re-root the tree to a new host (simulating what we do on a context lost
1054 // event). This should update the host pointers for all layers in the tree.
1055 scoped_ptr
<LayerTreeHost
> second_layer_tree_host
= factory
.Create();
1056 second_layer_tree_host
->SetRootLayer(parent
.get());
1058 AssertLayerTreeHostMatchesForSubtree(parent
.get(),
1059 second_layer_tree_host
.get());
1061 second_layer_tree_host
->SetRootLayer(nullptr);
1064 TEST(LayerLayerTreeHostTest
, ChangeHostInSubtree
) {
1065 scoped_refptr
<Layer
> first_parent
= Layer::Create();
1066 scoped_refptr
<Layer
> first_child
= Layer::Create();
1067 scoped_refptr
<Layer
> second_parent
= Layer::Create();
1068 scoped_refptr
<Layer
> second_child
= Layer::Create();
1069 scoped_refptr
<Layer
> second_grand_child
= Layer::Create();
1071 // First put all children under the first parent and set the first host.
1072 first_parent
->AddChild(first_child
);
1073 second_child
->AddChild(second_grand_child
);
1074 first_parent
->AddChild(second_child
);
1076 LayerTreeHostFactory factory
;
1077 scoped_ptr
<LayerTreeHost
> first_layer_tree_host
= factory
.Create();
1078 first_layer_tree_host
->SetRootLayer(first_parent
.get());
1080 AssertLayerTreeHostMatchesForSubtree(first_parent
.get(),
1081 first_layer_tree_host
.get());
1083 // Now reparent the subtree starting at second_child to a layer in a different
1085 scoped_ptr
<LayerTreeHost
> second_layer_tree_host
= factory
.Create();
1086 second_layer_tree_host
->SetRootLayer(second_parent
.get());
1088 second_parent
->AddChild(second_child
);
1090 // The moved layer and its children should point to the new host.
1091 EXPECT_EQ(second_layer_tree_host
.get(), second_child
->layer_tree_host());
1092 EXPECT_EQ(second_layer_tree_host
.get(),
1093 second_grand_child
->layer_tree_host());
1095 // Test over, cleanup time.
1096 first_layer_tree_host
->SetRootLayer(nullptr);
1097 second_layer_tree_host
->SetRootLayer(nullptr);
1100 TEST(LayerLayerTreeHostTest
, ReplaceMaskAndReplicaLayer
) {
1101 scoped_refptr
<Layer
> parent
= Layer::Create();
1102 scoped_refptr
<Layer
> mask
= Layer::Create();
1103 scoped_refptr
<Layer
> replica
= Layer::Create();
1104 scoped_refptr
<Layer
> mask_child
= Layer::Create();
1105 scoped_refptr
<Layer
> replica_child
= Layer::Create();
1106 scoped_refptr
<Layer
> mask_replacement
= Layer::Create();
1107 scoped_refptr
<Layer
> replica_replacement
= Layer::Create();
1109 parent
->SetMaskLayer(mask
.get());
1110 parent
->SetReplicaLayer(replica
.get());
1111 mask
->AddChild(mask_child
);
1112 replica
->AddChild(replica_child
);
1114 LayerTreeHostFactory factory
;
1115 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
1116 layer_tree_host
->SetRootLayer(parent
.get());
1118 AssertLayerTreeHostMatchesForSubtree(parent
.get(), layer_tree_host
.get());
1120 // Replacing the mask should clear out the old mask's subtree's host pointers.
1121 parent
->SetMaskLayer(mask_replacement
.get());
1122 EXPECT_EQ(nullptr, mask
->layer_tree_host());
1123 EXPECT_EQ(nullptr, mask_child
->layer_tree_host());
1125 // Same for replacing a replica layer.
1126 parent
->SetReplicaLayer(replica_replacement
.get());
1127 EXPECT_EQ(nullptr, replica
->layer_tree_host());
1128 EXPECT_EQ(nullptr, replica_child
->layer_tree_host());
1130 // Test over, cleanup time.
1131 layer_tree_host
->SetRootLayer(nullptr);
1134 TEST(LayerLayerTreeHostTest
, DestroyHostWithNonNullRootLayer
) {
1135 scoped_refptr
<Layer
> root
= Layer::Create();
1136 scoped_refptr
<Layer
> child
= Layer::Create();
1137 root
->AddChild(child
);
1138 LayerTreeHostFactory factory
;
1139 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
1140 layer_tree_host
->SetRootLayer(root
);
1143 static bool AddTestAnimation(Layer
* layer
) {
1144 scoped_ptr
<KeyframedFloatAnimationCurve
> curve
=
1145 KeyframedFloatAnimationCurve::Create();
1146 curve
->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.3f
, nullptr));
1148 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 0.7f
, nullptr));
1149 scoped_ptr
<Animation
> animation
=
1150 Animation::Create(curve
.Pass(), 0, 0, Animation::OPACITY
);
1152 return layer
->AddAnimation(animation
.Pass());
1155 TEST(LayerLayerTreeHostTest
, ShouldNotAddAnimationWithoutAnimationRegistrar
) {
1156 scoped_refptr
<Layer
> layer
= Layer::Create();
1158 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1159 // animation should not be accepted.
1160 EXPECT_FALSE(AddTestAnimation(layer
.get()));
1162 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
1163 layer
->layer_animation_controller()->SetAnimationRegistrar(registrar
.get());
1165 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1166 EXPECT_TRUE(AddTestAnimation(layer
.get()));
1168 LayerTreeSettings settings
;
1169 settings
.accelerated_animation_enabled
= false;
1170 LayerTreeHostFactory factory
;
1171 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create(settings
);
1172 layer_tree_host
->SetRootLayer(layer
);
1173 AssertLayerTreeHostMatchesForSubtree(layer
.get(), layer_tree_host
.get());
1175 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1176 // animation should be rejected.
1177 EXPECT_FALSE(AddTestAnimation(layer
.get()));
1180 TEST_F(LayerTest
, SafeOpaqueBackgroundColor
) {
1181 LayerTreeHostFactory factory
;
1182 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
1184 scoped_refptr
<Layer
> layer
= Layer::Create();
1185 layer_tree_host
->SetRootLayer(layer
);
1187 for (int contents_opaque
= 0; contents_opaque
< 2; ++contents_opaque
) {
1188 for (int layer_opaque
= 0; layer_opaque
< 2; ++layer_opaque
) {
1189 for (int host_opaque
= 0; host_opaque
< 2; ++host_opaque
) {
1190 layer
->SetContentsOpaque(!!contents_opaque
);
1191 layer
->SetBackgroundColor(layer_opaque
? SK_ColorRED
1192 : SK_ColorTRANSPARENT
);
1193 layer_tree_host
->set_background_color(
1194 host_opaque
? SK_ColorRED
: SK_ColorTRANSPARENT
);
1196 SkColor safe_color
= layer
->SafeOpaqueBackgroundColor();
1197 if (contents_opaque
) {
1198 EXPECT_EQ(SkColorGetA(safe_color
), 255u)
1199 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
1200 << host_opaque
<< "\n";
1202 EXPECT_NE(SkColorGetA(safe_color
), 255u)
1203 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
1204 << host_opaque
<< "\n";
1211 class DrawsContentChangeLayer
: public Layer
{
1213 static scoped_refptr
<DrawsContentChangeLayer
> Create() {
1214 return make_scoped_refptr(new DrawsContentChangeLayer());
1217 void SetLayerTreeHost(LayerTreeHost
* host
) override
{
1218 Layer::SetLayerTreeHost(host
);
1219 SetFakeDrawsContent(!fake_draws_content_
);
1222 bool HasDrawableContent() const override
{
1223 return fake_draws_content_
&& Layer::HasDrawableContent();
1226 void SetFakeDrawsContent(bool fake_draws_content
) {
1227 fake_draws_content_
= fake_draws_content
;
1228 UpdateDrawsContent(HasDrawableContent());
1232 DrawsContentChangeLayer() : Layer(), fake_draws_content_(false) {}
1233 ~DrawsContentChangeLayer() override
{}
1235 bool fake_draws_content_
;
1238 TEST_F(LayerTest
, DrawsContentChangedInSetLayerTreeHost
) {
1239 scoped_refptr
<Layer
> root_layer
= Layer::Create();
1240 scoped_refptr
<DrawsContentChangeLayer
> becomes_not_draws_content
=
1241 DrawsContentChangeLayer::Create();
1242 scoped_refptr
<DrawsContentChangeLayer
> becomes_draws_content
=
1243 DrawsContentChangeLayer::Create();
1244 root_layer
->SetIsDrawable(true);
1245 becomes_not_draws_content
->SetIsDrawable(true);
1246 becomes_not_draws_content
->SetFakeDrawsContent(true);
1247 EXPECT_EQ(0, root_layer
->NumDescendantsThatDrawContent());
1248 root_layer
->AddChild(becomes_not_draws_content
);
1249 EXPECT_EQ(0, root_layer
->NumDescendantsThatDrawContent());
1251 becomes_draws_content
->SetIsDrawable(true);
1252 root_layer
->AddChild(becomes_draws_content
);
1253 EXPECT_EQ(1, root_layer
->NumDescendantsThatDrawContent());
1256 void ReceiveCopyOutputResult(int* result_count
,
1257 scoped_ptr
<CopyOutputResult
> result
) {
1261 TEST_F(LayerTest
, DedupesCopyOutputRequestsBySource
) {
1262 scoped_refptr
<Layer
> layer
= Layer::Create();
1263 int result_count
= 0;
1265 // Create identical requests without the source being set, and expect the
1266 // layer does not abort either one.
1267 scoped_ptr
<CopyOutputRequest
> request
= CopyOutputRequest::CreateRequest(
1268 base::Bind(&ReceiveCopyOutputResult
, &result_count
));
1269 layer
->RequestCopyOfOutput(request
.Pass());
1270 EXPECT_EQ(0, result_count
);
1271 request
= CopyOutputRequest::CreateRequest(
1272 base::Bind(&ReceiveCopyOutputResult
, &result_count
));
1273 layer
->RequestCopyOfOutput(request
.Pass());
1274 EXPECT_EQ(0, result_count
);
1276 // When the layer is destroyed, expect both requests to be aborted.
1278 EXPECT_EQ(2, result_count
);
1280 layer
= Layer::Create();
1283 // Create identical requests, but this time the source is being set. Expect
1284 // the first request from |this| source aborts immediately when the second
1285 // request from |this| source is made.
1286 int did_receive_first_result_from_this_source
= 0;
1287 request
= CopyOutputRequest::CreateRequest(base::Bind(
1288 &ReceiveCopyOutputResult
, &did_receive_first_result_from_this_source
));
1289 request
->set_source(this);
1290 layer
->RequestCopyOfOutput(request
.Pass());
1291 EXPECT_EQ(0, did_receive_first_result_from_this_source
);
1292 // Make a request from a different source.
1293 int did_receive_result_from_different_source
= 0;
1294 request
= CopyOutputRequest::CreateRequest(base::Bind(
1295 &ReceiveCopyOutputResult
, &did_receive_result_from_different_source
));
1296 request
->set_source(reinterpret_cast<void*>(0xdeadbee0));
1297 layer
->RequestCopyOfOutput(request
.Pass());
1298 EXPECT_EQ(0, did_receive_result_from_different_source
);
1299 // Make a request without specifying the source.
1300 int did_receive_result_from_anonymous_source
= 0;
1301 request
= CopyOutputRequest::CreateRequest(base::Bind(
1302 &ReceiveCopyOutputResult
, &did_receive_result_from_anonymous_source
));
1303 layer
->RequestCopyOfOutput(request
.Pass());
1304 EXPECT_EQ(0, did_receive_result_from_anonymous_source
);
1305 // Make the second request from |this| source.
1306 int did_receive_second_result_from_this_source
= 0;
1307 request
= CopyOutputRequest::CreateRequest(base::Bind(
1308 &ReceiveCopyOutputResult
, &did_receive_second_result_from_this_source
));
1309 request
->set_source(this);
1310 layer
->RequestCopyOfOutput(request
.Pass()); // First request to be aborted.
1311 EXPECT_EQ(1, did_receive_first_result_from_this_source
);
1312 EXPECT_EQ(0, did_receive_result_from_different_source
);
1313 EXPECT_EQ(0, did_receive_result_from_anonymous_source
);
1314 EXPECT_EQ(0, did_receive_second_result_from_this_source
);
1316 // When the layer is destroyed, the other three requests should be aborted.
1318 EXPECT_EQ(1, did_receive_first_result_from_this_source
);
1319 EXPECT_EQ(1, did_receive_result_from_different_source
);
1320 EXPECT_EQ(1, did_receive_result_from_anonymous_source
);
1321 EXPECT_EQ(1, did_receive_second_result_from_this_source
);