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/resources/layer_painter.h"
14 #include "cc/test/animation_test_common.h"
15 #include "cc/test/fake_impl_proxy.h"
16 #include "cc/test/fake_layer_tree_host_client.h"
17 #include "cc/test/fake_layer_tree_host_impl.h"
18 #include "cc/test/geometry_test_utils.h"
19 #include "cc/test/layer_test_common.h"
20 #include "cc/test/test_gpu_memory_buffer_manager.h"
21 #include "cc/test/test_shared_bitmap_manager.h"
22 #include "cc/test/test_task_graph_runner.h"
23 #include "cc/trees/layer_tree_host.h"
24 #include "cc/trees/single_thread_proxy.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "ui/gfx/transform.h"
29 using ::testing::AnyNumber
;
30 using ::testing::AtLeast
;
31 using ::testing::Mock
;
32 using ::testing::StrictMock
;
35 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
37 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
39 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
45 class MockLayerTreeHost
: public LayerTreeHost
{
47 MockLayerTreeHost(LayerTreeHostSingleThreadClient
* single_thread_client
,
48 LayerTreeHost::InitParams
* params
)
49 : LayerTreeHost(params
) {
50 InitializeSingleThreaded(single_thread_client
,
51 base::ThreadTaskRunnerHandle::Get(), nullptr);
54 MOCK_METHOD0(SetNeedsCommit
, void());
55 MOCK_METHOD0(SetNeedsUpdateLayers
, void());
56 MOCK_METHOD0(SetNeedsFullTreeSync
, void());
59 class MockLayerPainter
: public LayerPainter
{
61 void Paint(SkCanvas
* canvas
, const gfx::Rect
& content_rect
) override
{}
64 class LayerTest
: public testing::Test
{
67 : host_impl_(&proxy_
, &shared_bitmap_manager_
, &task_graph_runner_
),
68 fake_client_(FakeLayerTreeHostClient::DIRECT_3D
) {}
71 void SetUp() override
{
72 LayerTreeHost::InitParams params
;
73 LayerTreeSettings settings
;
74 params
.client
= &fake_client_
;
75 params
.settings
= &settings
;
76 layer_tree_host_
.reset(
77 new StrictMock
<MockLayerTreeHost
>(&fake_client_
, ¶ms
));
80 void TearDown() override
{
81 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
82 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AnyNumber());
87 grand_child1_
= nullptr;
88 grand_child2_
= nullptr;
89 grand_child3_
= nullptr;
91 layer_tree_host_
->SetRootLayer(nullptr);
92 layer_tree_host_
= nullptr;
95 void VerifyTestTreeInitialState() const {
96 ASSERT_EQ(3U, parent_
->children().size());
97 EXPECT_EQ(child1_
, parent_
->children()[0]);
98 EXPECT_EQ(child2_
, parent_
->children()[1]);
99 EXPECT_EQ(child3_
, parent_
->children()[2]);
100 EXPECT_EQ(parent_
.get(), child1_
->parent());
101 EXPECT_EQ(parent_
.get(), child2_
->parent());
102 EXPECT_EQ(parent_
.get(), child3_
->parent());
104 ASSERT_EQ(2U, child1_
->children().size());
105 EXPECT_EQ(grand_child1_
, child1_
->children()[0]);
106 EXPECT_EQ(grand_child2_
, child1_
->children()[1]);
107 EXPECT_EQ(child1_
.get(), grand_child1_
->parent());
108 EXPECT_EQ(child1_
.get(), grand_child2_
->parent());
110 ASSERT_EQ(1U, child2_
->children().size());
111 EXPECT_EQ(grand_child3_
, child2_
->children()[0]);
112 EXPECT_EQ(child2_
.get(), grand_child3_
->parent());
114 ASSERT_EQ(0U, child3_
->children().size());
117 void CreateSimpleTestTree() {
118 parent_
= Layer::Create();
119 child1_
= Layer::Create();
120 child2_
= Layer::Create();
121 child3_
= Layer::Create();
122 grand_child1_
= Layer::Create();
123 grand_child2_
= Layer::Create();
124 grand_child3_
= Layer::Create();
126 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AnyNumber());
127 layer_tree_host_
->SetRootLayer(parent_
);
129 parent_
->AddChild(child1_
);
130 parent_
->AddChild(child2_
);
131 parent_
->AddChild(child3_
);
132 child1_
->AddChild(grand_child1_
);
133 child1_
->AddChild(grand_child2_
);
134 child2_
->AddChild(grand_child3_
);
136 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
138 VerifyTestTreeInitialState();
141 FakeImplProxy proxy_
;
142 TestSharedBitmapManager shared_bitmap_manager_
;
143 TestTaskGraphRunner task_graph_runner_
;
144 FakeLayerTreeHostImpl host_impl_
;
146 FakeLayerTreeHostClient fake_client_
;
147 scoped_ptr
<StrictMock
<MockLayerTreeHost
>> layer_tree_host_
;
148 scoped_refptr
<Layer
> parent_
;
149 scoped_refptr
<Layer
> child1_
;
150 scoped_refptr
<Layer
> child2_
;
151 scoped_refptr
<Layer
> child3_
;
152 scoped_refptr
<Layer
> grand_child1_
;
153 scoped_refptr
<Layer
> grand_child2_
;
154 scoped_refptr
<Layer
> grand_child3_
;
157 TEST_F(LayerTest
, BasicCreateAndDestroy
) {
158 scoped_refptr
<Layer
> test_layer
= Layer::Create();
159 ASSERT_TRUE(test_layer
.get());
161 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(0);
162 test_layer
->SetLayerTreeHost(layer_tree_host_
.get());
163 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
165 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(0);
166 test_layer
->SetLayerTreeHost(nullptr);
169 TEST_F(LayerTest
, AddAndRemoveChild
) {
170 scoped_refptr
<Layer
> parent
= Layer::Create();
171 scoped_refptr
<Layer
> child
= Layer::Create();
173 // Upon creation, layers should not have children or parent.
174 ASSERT_EQ(0U, parent
->children().size());
175 EXPECT_FALSE(child
->parent());
177 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
178 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->AddChild(child
));
180 ASSERT_EQ(1U, parent
->children().size());
181 EXPECT_EQ(child
.get(), parent
->children()[0].get());
182 EXPECT_EQ(parent
.get(), child
->parent());
183 EXPECT_EQ(parent
.get(), child
->RootLayer());
185 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child
->RemoveFromParent());
188 TEST_F(LayerTest
, AddSameChildTwice
) {
189 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AtLeast(1));
191 scoped_refptr
<Layer
> parent
= Layer::Create();
192 scoped_refptr
<Layer
> child
= Layer::Create();
194 layer_tree_host_
->SetRootLayer(parent
);
196 ASSERT_EQ(0u, parent
->children().size());
198 parent
->AddChild(child
);
199 ASSERT_EQ(1u, parent
->children().size());
200 EXPECT_EQ(parent
.get(), child
->parent());
202 parent
->AddChild(child
);
203 ASSERT_EQ(1u, parent
->children().size());
204 EXPECT_EQ(parent
.get(), child
->parent());
207 TEST_F(LayerTest
, InsertChild
) {
208 scoped_refptr
<Layer
> parent
= Layer::Create();
209 scoped_refptr
<Layer
> child1
= Layer::Create();
210 scoped_refptr
<Layer
> child2
= Layer::Create();
211 scoped_refptr
<Layer
> child3
= Layer::Create();
212 scoped_refptr
<Layer
> child4
= Layer::Create();
214 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
216 ASSERT_EQ(0U, parent
->children().size());
218 // Case 1: inserting to empty list.
219 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child3
, 0));
220 ASSERT_EQ(1U, parent
->children().size());
221 EXPECT_EQ(child3
, parent
->children()[0]);
222 EXPECT_EQ(parent
.get(), child3
->parent());
224 // Case 2: inserting to beginning of list
225 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child1
, 0));
226 ASSERT_EQ(2U, parent
->children().size());
227 EXPECT_EQ(child1
, parent
->children()[0]);
228 EXPECT_EQ(child3
, parent
->children()[1]);
229 EXPECT_EQ(parent
.get(), child1
->parent());
231 // Case 3: inserting to middle of list
232 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child2
, 1));
233 ASSERT_EQ(3U, parent
->children().size());
234 EXPECT_EQ(child1
, parent
->children()[0]);
235 EXPECT_EQ(child2
, parent
->children()[1]);
236 EXPECT_EQ(child3
, parent
->children()[2]);
237 EXPECT_EQ(parent
.get(), child2
->parent());
239 // Case 4: inserting to end of list
240 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child4
, 3));
242 ASSERT_EQ(4U, parent
->children().size());
243 EXPECT_EQ(child1
, parent
->children()[0]);
244 EXPECT_EQ(child2
, parent
->children()[1]);
245 EXPECT_EQ(child3
, parent
->children()[2]);
246 EXPECT_EQ(child4
, parent
->children()[3]);
247 EXPECT_EQ(parent
.get(), child4
->parent());
249 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(nullptr));
252 TEST_F(LayerTest
, InsertChildPastEndOfList
) {
253 scoped_refptr
<Layer
> parent
= Layer::Create();
254 scoped_refptr
<Layer
> child1
= Layer::Create();
255 scoped_refptr
<Layer
> child2
= Layer::Create();
257 ASSERT_EQ(0U, parent
->children().size());
259 // insert to an out-of-bounds index
260 parent
->InsertChild(child1
, 53);
262 ASSERT_EQ(1U, parent
->children().size());
263 EXPECT_EQ(child1
, parent
->children()[0]);
265 // insert another child to out-of-bounds, when list is not already empty.
266 parent
->InsertChild(child2
, 2459);
268 ASSERT_EQ(2U, parent
->children().size());
269 EXPECT_EQ(child1
, parent
->children()[0]);
270 EXPECT_EQ(child2
, parent
->children()[1]);
273 TEST_F(LayerTest
, InsertSameChildTwice
) {
274 scoped_refptr
<Layer
> parent
= Layer::Create();
275 scoped_refptr
<Layer
> child1
= Layer::Create();
276 scoped_refptr
<Layer
> child2
= Layer::Create();
278 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
280 ASSERT_EQ(0U, parent
->children().size());
282 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child1
, 0));
283 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child2
, 1));
285 ASSERT_EQ(2U, parent
->children().size());
286 EXPECT_EQ(child1
, parent
->children()[0]);
287 EXPECT_EQ(child2
, parent
->children()[1]);
289 // Inserting the same child again should cause the child to be removed and
290 // re-inserted at the new location.
291 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent
->InsertChild(child1
, 1));
293 // child1 should now be at the end of the list.
294 ASSERT_EQ(2U, parent
->children().size());
295 EXPECT_EQ(child2
, parent
->children()[0]);
296 EXPECT_EQ(child1
, parent
->children()[1]);
298 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(nullptr));
301 TEST_F(LayerTest
, ReplaceChildWithNewChild
) {
302 CreateSimpleTestTree();
303 scoped_refptr
<Layer
> child4
= Layer::Create();
305 EXPECT_FALSE(child4
->parent());
307 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
308 AtLeast(1), parent_
->ReplaceChild(child2_
.get(), child4
));
309 EXPECT_FALSE(parent_
->NeedsDisplayForTesting());
310 EXPECT_FALSE(child1_
->NeedsDisplayForTesting());
311 EXPECT_FALSE(child2_
->NeedsDisplayForTesting());
312 EXPECT_FALSE(child3_
->NeedsDisplayForTesting());
313 EXPECT_FALSE(child4
->NeedsDisplayForTesting());
315 ASSERT_EQ(static_cast<size_t>(3), parent_
->children().size());
316 EXPECT_EQ(child1_
, parent_
->children()[0]);
317 EXPECT_EQ(child4
, parent_
->children()[1]);
318 EXPECT_EQ(child3_
, parent_
->children()[2]);
319 EXPECT_EQ(parent_
.get(), child4
->parent());
321 EXPECT_FALSE(child2_
->parent());
324 TEST_F(LayerTest
, ReplaceChildWithNewChildThatHasOtherParent
) {
325 CreateSimpleTestTree();
327 // create another simple tree with test_layer and child4.
328 scoped_refptr
<Layer
> test_layer
= Layer::Create();
329 scoped_refptr
<Layer
> child4
= Layer::Create();
330 test_layer
->AddChild(child4
);
331 ASSERT_EQ(1U, test_layer
->children().size());
332 EXPECT_EQ(child4
, test_layer
->children()[0]);
333 EXPECT_EQ(test_layer
.get(), child4
->parent());
335 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
336 AtLeast(1), parent_
->ReplaceChild(child2_
.get(), child4
));
338 ASSERT_EQ(3U, parent_
->children().size());
339 EXPECT_EQ(child1_
, parent_
->children()[0]);
340 EXPECT_EQ(child4
, parent_
->children()[1]);
341 EXPECT_EQ(child3_
, parent_
->children()[2]);
342 EXPECT_EQ(parent_
.get(), child4
->parent());
344 // test_layer should no longer have child4,
345 // and child2 should no longer have a parent.
346 ASSERT_EQ(0U, test_layer
->children().size());
347 EXPECT_FALSE(child2_
->parent());
350 TEST_F(LayerTest
, DeleteRemovedScrollParent
) {
351 scoped_refptr
<Layer
> parent
= Layer::Create();
352 scoped_refptr
<Layer
> child1
= Layer::Create();
353 scoped_refptr
<Layer
> child2
= Layer::Create();
355 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
357 ASSERT_EQ(0U, parent
->children().size());
359 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child1
, 0));
360 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child2
, 1));
362 ASSERT_EQ(2U, parent
->children().size());
363 EXPECT_EQ(child1
, parent
->children()[0]);
364 EXPECT_EQ(child2
, parent
->children()[1]);
366 EXPECT_SET_NEEDS_COMMIT(2, child1
->SetScrollParent(child2
.get()));
368 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child2
->RemoveFromParent());
370 child1
->reset_needs_push_properties_for_testing();
372 EXPECT_SET_NEEDS_COMMIT(1, child2
= nullptr);
374 EXPECT_TRUE(child1
->needs_push_properties());
376 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(nullptr));
379 TEST_F(LayerTest
, DeleteRemovedScrollChild
) {
380 scoped_refptr
<Layer
> parent
= Layer::Create();
381 scoped_refptr
<Layer
> child1
= Layer::Create();
382 scoped_refptr
<Layer
> child2
= Layer::Create();
384 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
386 ASSERT_EQ(0U, parent
->children().size());
388 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child1
, 0));
389 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child2
, 1));
391 ASSERT_EQ(2U, parent
->children().size());
392 EXPECT_EQ(child1
, parent
->children()[0]);
393 EXPECT_EQ(child2
, parent
->children()[1]);
395 EXPECT_SET_NEEDS_COMMIT(2, child1
->SetScrollParent(child2
.get()));
397 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child1
->RemoveFromParent());
399 child2
->reset_needs_push_properties_for_testing();
401 EXPECT_SET_NEEDS_COMMIT(1, child1
= nullptr);
403 EXPECT_TRUE(child2
->needs_push_properties());
405 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(nullptr));
408 TEST_F(LayerTest
, ReplaceChildWithSameChild
) {
409 CreateSimpleTestTree();
411 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
413 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(0);
414 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(0);
415 parent_
->ReplaceChild(child2_
.get(), child2_
);
417 VerifyTestTreeInitialState();
420 TEST_F(LayerTest
, RemoveAllChildren
) {
421 CreateSimpleTestTree();
423 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_
->RemoveAllChildren());
425 ASSERT_EQ(0U, parent_
->children().size());
426 EXPECT_FALSE(child1_
->parent());
427 EXPECT_FALSE(child2_
->parent());
428 EXPECT_FALSE(child3_
->parent());
431 TEST_F(LayerTest
, SetChildren
) {
432 scoped_refptr
<Layer
> old_parent
= Layer::Create();
433 scoped_refptr
<Layer
> new_parent
= Layer::Create();
435 scoped_refptr
<Layer
> child1
= Layer::Create();
436 scoped_refptr
<Layer
> child2
= Layer::Create();
438 LayerList new_children
;
439 new_children
.push_back(child1
);
440 new_children
.push_back(child2
);
442 // Set up and verify initial test conditions: child1 has a parent, child2 has
444 old_parent
->AddChild(child1
);
445 ASSERT_EQ(0U, new_parent
->children().size());
446 EXPECT_EQ(old_parent
.get(), child1
->parent());
447 EXPECT_FALSE(child2
->parent());
449 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
450 1, layer_tree_host_
->SetRootLayer(new_parent
));
452 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
453 AtLeast(1), new_parent
->SetChildren(new_children
));
455 ASSERT_EQ(2U, new_parent
->children().size());
456 EXPECT_EQ(new_parent
.get(), child1
->parent());
457 EXPECT_EQ(new_parent
.get(), child2
->parent());
459 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(nullptr));
462 TEST_F(LayerTest
, HasAncestor
) {
463 scoped_refptr
<Layer
> parent
= Layer::Create();
464 EXPECT_FALSE(parent
->HasAncestor(parent
.get()));
466 scoped_refptr
<Layer
> child
= Layer::Create();
467 parent
->AddChild(child
);
469 EXPECT_FALSE(child
->HasAncestor(child
.get()));
470 EXPECT_TRUE(child
->HasAncestor(parent
.get()));
471 EXPECT_FALSE(parent
->HasAncestor(child
.get()));
473 scoped_refptr
<Layer
> child_child
= Layer::Create();
474 child
->AddChild(child_child
);
476 EXPECT_FALSE(child_child
->HasAncestor(child_child
.get()));
477 EXPECT_TRUE(child_child
->HasAncestor(parent
.get()));
478 EXPECT_TRUE(child_child
->HasAncestor(child
.get()));
479 EXPECT_FALSE(parent
->HasAncestor(child
.get()));
480 EXPECT_FALSE(parent
->HasAncestor(child_child
.get()));
483 TEST_F(LayerTest
, GetRootLayerAfterTreeManipulations
) {
484 CreateSimpleTestTree();
486 // For this test we don't care about SetNeedsFullTreeSync calls.
487 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AnyNumber());
489 scoped_refptr
<Layer
> child4
= Layer::Create();
491 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
492 EXPECT_EQ(parent_
.get(), child1_
->RootLayer());
493 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
494 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
495 EXPECT_EQ(child4
.get(), child4
->RootLayer());
496 EXPECT_EQ(parent_
.get(), grand_child1_
->RootLayer());
497 EXPECT_EQ(parent_
.get(), grand_child2_
->RootLayer());
498 EXPECT_EQ(parent_
.get(), grand_child3_
->RootLayer());
500 child1_
->RemoveFromParent();
502 // |child1| and its children, grand_child1 and grand_child2 are now on a
504 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
505 EXPECT_EQ(child1_
.get(), child1_
->RootLayer());
506 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
507 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
508 EXPECT_EQ(child4
.get(), child4
->RootLayer());
509 EXPECT_EQ(child1_
.get(), grand_child1_
->RootLayer());
510 EXPECT_EQ(child1_
.get(), grand_child2_
->RootLayer());
511 EXPECT_EQ(parent_
.get(), grand_child3_
->RootLayer());
513 grand_child3_
->AddChild(child4
);
515 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
516 EXPECT_EQ(child1_
.get(), child1_
->RootLayer());
517 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
518 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
519 EXPECT_EQ(parent_
.get(), child4
->RootLayer());
520 EXPECT_EQ(child1_
.get(), grand_child1_
->RootLayer());
521 EXPECT_EQ(child1_
.get(), grand_child2_
->RootLayer());
522 EXPECT_EQ(parent_
.get(), grand_child3_
->RootLayer());
524 child2_
->ReplaceChild(grand_child3_
.get(), child1_
);
526 // |grand_child3| gets orphaned and the child1 subtree gets planted back into
527 // the tree under child2.
528 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
529 EXPECT_EQ(parent_
.get(), child1_
->RootLayer());
530 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
531 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
532 EXPECT_EQ(grand_child3_
.get(), child4
->RootLayer());
533 EXPECT_EQ(parent_
.get(), grand_child1_
->RootLayer());
534 EXPECT_EQ(parent_
.get(), grand_child2_
->RootLayer());
535 EXPECT_EQ(grand_child3_
.get(), grand_child3_
->RootLayer());
538 TEST_F(LayerTest
, CheckSetNeedsDisplayCausesCorrectBehavior
) {
539 // The semantics for SetNeedsDisplay which are tested here:
540 // 1. sets NeedsDisplay flag appropriately.
541 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
544 scoped_refptr
<Layer
> test_layer
= Layer::Create();
545 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
546 1, layer_tree_host_
->SetRootLayer(test_layer
));
547 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsDrawable(true));
549 gfx::Size test_bounds
= gfx::Size(501, 508);
551 gfx::Rect dirty1
= gfx::Rect(10, 15, 1, 2);
552 gfx::Rect dirty2
= gfx::Rect(20, 25, 3, 4);
553 gfx::Rect out_of_bounds_dirty_rect
= gfx::Rect(400, 405, 500, 502);
555 // Before anything, test_layer should not be dirty.
556 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
558 // This is just initialization, but SetNeedsCommit behavior is verified anyway
559 // to avoid warnings.
560 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetBounds(test_bounds
));
561 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
563 // The real test begins here.
564 test_layer
->ResetNeedsDisplayForTesting();
565 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
567 // Case 1: Layer should accept dirty rects that go beyond its bounds.
568 test_layer
->ResetNeedsDisplayForTesting();
569 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
570 EXPECT_SET_NEEDS_UPDATE(
571 1, test_layer
->SetNeedsDisplayRect(out_of_bounds_dirty_rect
));
572 EXPECT_TRUE(test_layer
->NeedsDisplayForTesting());
573 test_layer
->ResetNeedsDisplayForTesting();
575 // Case 2: SetNeedsDisplay() without the dirty rect arg.
576 test_layer
->ResetNeedsDisplayForTesting();
577 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
578 EXPECT_SET_NEEDS_UPDATE(1, test_layer
->SetNeedsDisplay());
579 EXPECT_TRUE(test_layer
->NeedsDisplayForTesting());
580 test_layer
->ResetNeedsDisplayForTesting();
582 // Case 3: SetNeedsDisplay() with an empty rect.
583 test_layer
->ResetNeedsDisplayForTesting();
584 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
585 EXPECT_SET_NEEDS_COMMIT(0, test_layer
->SetNeedsDisplayRect(gfx::Rect()));
586 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
588 // Case 4: SetNeedsDisplay() with a non-drawable layer
589 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsDrawable(false));
590 test_layer
->ResetNeedsDisplayForTesting();
591 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
592 EXPECT_SET_NEEDS_UPDATE(0, test_layer
->SetNeedsDisplayRect(dirty1
));
593 EXPECT_TRUE(test_layer
->NeedsDisplayForTesting());
596 TEST_F(LayerTest
, CheckPropertyChangeCausesCorrectBehavior
) {
597 scoped_refptr
<Layer
> test_layer
= Layer::Create();
598 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
599 1, layer_tree_host_
->SetRootLayer(test_layer
));
600 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsDrawable(true));
602 scoped_refptr
<Layer
> dummy_layer1
= Layer::Create();
603 scoped_refptr
<Layer
> dummy_layer2
= Layer::Create();
605 // sanity check of initial test condition
606 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
608 // Next, test properties that should call SetNeedsCommit (but not
609 // SetNeedsDisplay). All properties need to be set to new values in order for
610 // SetNeedsCommit to be called.
611 EXPECT_SET_NEEDS_COMMIT(
612 1, test_layer
->SetTransformOrigin(gfx::Point3F(1.23f
, 4.56f
, 0.f
)));
613 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetBackgroundColor(SK_ColorLTGRAY
));
614 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetMasksToBounds(true));
615 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.5f
));
616 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetBlendMode(SkXfermode::kHue_Mode
));
617 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsRootForIsolatedGroup(true));
618 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetContentsOpaque(true));
619 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetPosition(gfx::PointF(4.f
, 9.f
)));
620 // We can use any layer pointer here since we aren't syncing for real.
621 EXPECT_SET_NEEDS_COMMIT(1,
622 test_layer
->SetScrollClipLayerId(test_layer
->id()));
623 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetUserScrollable(true, false));
624 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetScrollOffset(
625 gfx::ScrollOffset(10, 10)));
626 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetShouldScrollOnMainThread(true));
627 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetNonFastScrollableRegion(
628 Region(gfx::Rect(1, 1, 2, 2))));
629 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetHaveWheelEventHandlers(true));
630 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetHaveScrollEventHandlers(true));
631 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(
632 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
633 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetDoubleSided(false));
634 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTouchEventHandlerRegion(
636 EXPECT_SET_NEEDS_COMMIT(
638 test_layer
->SetDrawCheckerboardForMissingTiles(
639 !test_layer
->draw_checkerboard_for_missing_tiles()));
640 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetForceRenderSurface(true));
641 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetHideLayerAndSubtree(true));
643 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer
->SetMaskLayer(
644 dummy_layer1
.get()));
645 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer
->SetReplicaLayer(
646 dummy_layer2
.get()));
648 // The above tests should not have caused a change to the needs_display flag.
649 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
651 // As layers are removed from the tree, they will cause a tree sync.
652 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times((AnyNumber()));
655 TEST_F(LayerTest
, PushPropertiesAccumulatesUpdateRect
) {
656 scoped_refptr
<Layer
> test_layer
= Layer::Create();
657 scoped_ptr
<LayerImpl
> impl_layer
=
658 LayerImpl::Create(host_impl_
.active_tree(), 1);
660 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
661 layer_tree_host_
->SetRootLayer(test_layer
));
663 test_layer
->SetNeedsDisplayRect(gfx::Rect(5, 5));
664 test_layer
->PushPropertiesTo(impl_layer
.get());
665 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f
, 0.f
, 5.f
, 5.f
),
666 impl_layer
->update_rect());
668 // The LayerImpl's update_rect() should be accumulated here, since we did not
669 // do anything to clear it.
670 test_layer
->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
671 test_layer
->PushPropertiesTo(impl_layer
.get());
672 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f
, 0.f
, 15.f
, 15.f
),
673 impl_layer
->update_rect());
675 // If we do clear the LayerImpl side, then the next update_rect() should be
676 // fresh without accumulation.
677 impl_layer
->ResetAllChangeTrackingForSubtree();
678 test_layer
->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
679 test_layer
->PushPropertiesTo(impl_layer
.get());
680 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f
, 10.f
, 5.f
, 5.f
),
681 impl_layer
->update_rect());
684 TEST_F(LayerTest
, PushPropertiesCausesLayerPropertyChangedForTransform
) {
685 scoped_refptr
<Layer
> test_layer
= Layer::Create();
686 scoped_ptr
<LayerImpl
> impl_layer
=
687 LayerImpl::Create(host_impl_
.active_tree(), 1);
689 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
690 layer_tree_host_
->SetRootLayer(test_layer
));
692 gfx::Transform transform
;
693 transform
.Rotate(45.0);
694 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(transform
));
696 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
698 test_layer
->PushPropertiesTo(impl_layer
.get());
700 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
703 TEST_F(LayerTest
, PushPropertiesCausesLayerPropertyChangedForOpacity
) {
704 scoped_refptr
<Layer
> test_layer
= Layer::Create();
705 scoped_ptr
<LayerImpl
> impl_layer
=
706 LayerImpl::Create(host_impl_
.active_tree(), 1);
708 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
709 layer_tree_host_
->SetRootLayer(test_layer
));
711 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.5f
));
713 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
715 test_layer
->PushPropertiesTo(impl_layer
.get());
717 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
721 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim
) {
722 scoped_refptr
<Layer
> test_layer
= Layer::Create();
723 scoped_ptr
<LayerImpl
> impl_layer
=
724 LayerImpl::Create(host_impl_
.active_tree(), 1);
726 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
727 layer_tree_host_
->SetRootLayer(test_layer
));
729 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
730 impl_layer
->layer_animation_controller()->SetAnimationRegistrar(
733 AddAnimatedTransformToController(impl_layer
->layer_animation_controller(),
738 gfx::Transform transform
;
739 transform
.Rotate(45.0);
740 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(transform
));
742 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
743 test_layer
->PushPropertiesTo(impl_layer
.get());
744 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
746 impl_layer
->ResetAllChangeTrackingForSubtree();
747 AddAnimatedTransformToController(impl_layer
->layer_animation_controller(),
751 impl_layer
->layer_animation_controller()
752 ->GetAnimation(Animation::TRANSFORM
)
753 ->set_is_impl_only(true);
754 transform
.Rotate(45.0);
755 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(transform
));
757 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
758 test_layer
->PushPropertiesTo(impl_layer
.get());
759 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
763 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim
) {
764 scoped_refptr
<Layer
> test_layer
= Layer::Create();
765 scoped_ptr
<LayerImpl
> impl_layer
=
766 LayerImpl::Create(host_impl_
.active_tree(), 1);
768 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
769 layer_tree_host_
->SetRootLayer(test_layer
));
771 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
772 impl_layer
->layer_animation_controller()->SetAnimationRegistrar(
775 AddOpacityTransitionToController(impl_layer
->layer_animation_controller(),
781 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.5f
));
783 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
784 test_layer
->PushPropertiesTo(impl_layer
.get());
785 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
787 impl_layer
->ResetAllChangeTrackingForSubtree();
788 AddOpacityTransitionToController(impl_layer
->layer_animation_controller(),
793 impl_layer
->layer_animation_controller()
794 ->GetAnimation(Animation::OPACITY
)
795 ->set_is_impl_only(true);
796 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.75f
));
798 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
799 test_layer
->PushPropertiesTo(impl_layer
.get());
800 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
804 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim
) {
805 scoped_refptr
<Layer
> test_layer
= Layer::Create();
806 scoped_ptr
<LayerImpl
> impl_layer
=
807 LayerImpl::Create(host_impl_
.active_tree(), 1);
809 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
810 layer_tree_host_
->SetRootLayer(test_layer
));
812 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
813 impl_layer
->layer_animation_controller()->SetAnimationRegistrar(
816 AddAnimatedFilterToController(
817 impl_layer
->layer_animation_controller(), 1.0, 1.f
, 2.f
);
819 FilterOperations filters
;
820 filters
.Append(FilterOperation::CreateBlurFilter(2.f
));
821 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetFilters(filters
));
823 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
824 test_layer
->PushPropertiesTo(impl_layer
.get());
825 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
827 impl_layer
->ResetAllChangeTrackingForSubtree();
828 AddAnimatedFilterToController(
829 impl_layer
->layer_animation_controller(), 1.0, 1.f
, 2.f
);
830 impl_layer
->layer_animation_controller()
831 ->GetAnimation(Animation::FILTER
)
832 ->set_is_impl_only(true);
833 filters
.Append(FilterOperation::CreateSepiaFilter(0.5f
));
834 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetFilters(filters
));
836 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
837 test_layer
->PushPropertiesTo(impl_layer
.get());
838 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
841 TEST_F(LayerTest
, MaskAndReplicaHasParent
) {
842 scoped_refptr
<Layer
> parent
= Layer::Create();
843 scoped_refptr
<Layer
> child
= Layer::Create();
844 scoped_refptr
<Layer
> mask
= Layer::Create();
845 scoped_refptr
<Layer
> replica
= Layer::Create();
846 scoped_refptr
<Layer
> replica_mask
= Layer::Create();
847 scoped_refptr
<Layer
> mask_replacement
= Layer::Create();
848 scoped_refptr
<Layer
> replica_replacement
= Layer::Create();
849 scoped_refptr
<Layer
> replica_mask_replacement
= Layer::Create();
851 parent
->AddChild(child
);
852 child
->SetMaskLayer(mask
.get());
853 child
->SetReplicaLayer(replica
.get());
854 replica
->SetMaskLayer(replica_mask
.get());
856 EXPECT_EQ(parent
.get(), child
->parent());
857 EXPECT_EQ(child
.get(), mask
->parent());
858 EXPECT_EQ(child
.get(), replica
->parent());
859 EXPECT_EQ(replica
.get(), replica_mask
->parent());
861 replica
->SetMaskLayer(replica_mask_replacement
.get());
862 EXPECT_EQ(nullptr, replica_mask
->parent());
863 EXPECT_EQ(replica
.get(), replica_mask_replacement
->parent());
865 child
->SetMaskLayer(mask_replacement
.get());
866 EXPECT_EQ(nullptr, mask
->parent());
867 EXPECT_EQ(child
.get(), mask_replacement
->parent());
869 child
->SetReplicaLayer(replica_replacement
.get());
870 EXPECT_EQ(nullptr, replica
->parent());
871 EXPECT_EQ(child
.get(), replica_replacement
->parent());
873 EXPECT_EQ(replica
.get(), replica
->mask_layer()->parent());
876 TEST_F(LayerTest
, CheckTranformIsInvertible
) {
877 scoped_refptr
<Layer
> layer
= Layer::Create();
878 scoped_ptr
<LayerImpl
> impl_layer
=
879 LayerImpl::Create(host_impl_
.active_tree(), 1);
880 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(1);
881 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(AnyNumber());
882 layer_tree_host_
->SetRootLayer(layer
);
884 EXPECT_TRUE(layer
->transform_is_invertible());
886 gfx::Transform singular_transform
;
887 singular_transform
.Scale3d(
888 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
890 layer
->SetTransform(singular_transform
);
891 layer
->PushPropertiesTo(impl_layer
.get());
893 EXPECT_FALSE(layer
->transform_is_invertible());
894 EXPECT_FALSE(impl_layer
->transform_is_invertible());
896 gfx::Transform rotation_transform
;
897 rotation_transform
.RotateAboutZAxis(-45.0);
899 layer
->SetTransform(rotation_transform
);
900 layer
->PushPropertiesTo(impl_layer
.get());
901 EXPECT_TRUE(layer
->transform_is_invertible());
902 EXPECT_TRUE(impl_layer
->transform_is_invertible());
904 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
907 TEST_F(LayerTest
, TranformIsInvertibleAnimation
) {
908 scoped_refptr
<Layer
> layer
= Layer::Create();
909 scoped_ptr
<LayerImpl
> impl_layer
=
910 LayerImpl::Create(host_impl_
.active_tree(), 1);
911 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(1);
912 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(AnyNumber());
913 layer_tree_host_
->SetRootLayer(layer
);
915 EXPECT_TRUE(layer
->transform_is_invertible());
917 gfx::Transform singular_transform
;
918 singular_transform
.Scale3d(
919 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
921 layer
->SetTransform(singular_transform
);
922 layer
->PushPropertiesTo(impl_layer
.get());
924 EXPECT_FALSE(layer
->transform_is_invertible());
925 EXPECT_FALSE(impl_layer
->transform_is_invertible());
927 gfx::Transform identity_transform
;
929 layer
->SetTransform(identity_transform
);
930 static_cast<LayerAnimationValueObserver
*>(layer
.get())
931 ->OnTransformAnimated(singular_transform
);
932 layer
->PushPropertiesTo(impl_layer
.get());
933 EXPECT_FALSE(layer
->transform_is_invertible());
934 EXPECT_FALSE(impl_layer
->transform_is_invertible());
936 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
939 class LayerTreeHostFactory
{
941 LayerTreeHostFactory()
942 : client_(FakeLayerTreeHostClient::DIRECT_3D
),
943 shared_bitmap_manager_(new TestSharedBitmapManager
),
944 gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager
) {}
946 scoped_ptr
<LayerTreeHost
> Create() { return Create(LayerTreeSettings()); }
948 scoped_ptr
<LayerTreeHost
> Create(LayerTreeSettings settings
) {
949 LayerTreeHost::InitParams params
;
950 params
.client
= &client_
;
951 params
.shared_bitmap_manager
= shared_bitmap_manager_
.get();
952 params
.gpu_memory_buffer_manager
= gpu_memory_buffer_manager_
.get();
953 params
.settings
= &settings
;
954 params
.main_task_runner
= base::ThreadTaskRunnerHandle::Get();
955 return LayerTreeHost::CreateSingleThreaded(&client_
, ¶ms
);
959 FakeLayerTreeHostClient client_
;
960 scoped_ptr
<TestSharedBitmapManager
> shared_bitmap_manager_
;
961 scoped_ptr
<TestGpuMemoryBufferManager
> gpu_memory_buffer_manager_
;
964 void AssertLayerTreeHostMatchesForSubtree(Layer
* layer
, LayerTreeHost
* host
) {
965 EXPECT_EQ(host
, layer
->layer_tree_host());
967 for (size_t i
= 0; i
< layer
->children().size(); ++i
)
968 AssertLayerTreeHostMatchesForSubtree(layer
->children()[i
].get(), host
);
970 if (layer
->mask_layer())
971 AssertLayerTreeHostMatchesForSubtree(layer
->mask_layer(), host
);
973 if (layer
->replica_layer())
974 AssertLayerTreeHostMatchesForSubtree(layer
->replica_layer(), host
);
977 TEST(LayerLayerTreeHostTest
, EnteringTree
) {
978 scoped_refptr
<Layer
> parent
= Layer::Create();
979 scoped_refptr
<Layer
> child
= Layer::Create();
980 scoped_refptr
<Layer
> mask
= Layer::Create();
981 scoped_refptr
<Layer
> replica
= Layer::Create();
982 scoped_refptr
<Layer
> replica_mask
= Layer::Create();
984 // Set up a detached tree of layers. The host pointer should be nil for these
986 parent
->AddChild(child
);
987 child
->SetMaskLayer(mask
.get());
988 child
->SetReplicaLayer(replica
.get());
989 replica
->SetMaskLayer(replica_mask
.get());
991 AssertLayerTreeHostMatchesForSubtree(parent
.get(), nullptr);
993 LayerTreeHostFactory factory
;
994 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
995 // Setting the root layer should set the host pointer for all layers in the
997 layer_tree_host
->SetRootLayer(parent
.get());
999 AssertLayerTreeHostMatchesForSubtree(parent
.get(), layer_tree_host
.get());
1001 // Clearing the root layer should also clear out the host pointers for all
1002 // layers in the tree.
1003 layer_tree_host
->SetRootLayer(nullptr);
1005 AssertLayerTreeHostMatchesForSubtree(parent
.get(), nullptr);
1008 TEST(LayerLayerTreeHostTest
, AddingLayerSubtree
) {
1009 scoped_refptr
<Layer
> parent
= Layer::Create();
1010 LayerTreeHostFactory factory
;
1011 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
1013 layer_tree_host
->SetRootLayer(parent
.get());
1015 EXPECT_EQ(parent
->layer_tree_host(), layer_tree_host
.get());
1017 // Adding a subtree to a layer already associated with a host should set the
1018 // host pointer on all layers in that subtree.
1019 scoped_refptr
<Layer
> child
= Layer::Create();
1020 scoped_refptr
<Layer
> grand_child
= Layer::Create();
1021 child
->AddChild(grand_child
);
1023 // Masks, replicas, and replica masks should pick up the new host too.
1024 scoped_refptr
<Layer
> child_mask
= Layer::Create();
1025 child
->SetMaskLayer(child_mask
.get());
1026 scoped_refptr
<Layer
> child_replica
= Layer::Create();
1027 child
->SetReplicaLayer(child_replica
.get());
1028 scoped_refptr
<Layer
> child_replica_mask
= Layer::Create();
1029 child_replica
->SetMaskLayer(child_replica_mask
.get());
1031 parent
->AddChild(child
);
1032 AssertLayerTreeHostMatchesForSubtree(parent
.get(), layer_tree_host
.get());
1034 layer_tree_host
->SetRootLayer(nullptr);
1037 TEST(LayerLayerTreeHostTest
, ChangeHost
) {
1038 scoped_refptr
<Layer
> parent
= Layer::Create();
1039 scoped_refptr
<Layer
> child
= Layer::Create();
1040 scoped_refptr
<Layer
> mask
= Layer::Create();
1041 scoped_refptr
<Layer
> replica
= Layer::Create();
1042 scoped_refptr
<Layer
> replica_mask
= Layer::Create();
1044 // Same setup as the previous test.
1045 parent
->AddChild(child
);
1046 child
->SetMaskLayer(mask
.get());
1047 child
->SetReplicaLayer(replica
.get());
1048 replica
->SetMaskLayer(replica_mask
.get());
1050 LayerTreeHostFactory factory
;
1051 scoped_ptr
<LayerTreeHost
> first_layer_tree_host
= factory
.Create();
1052 first_layer_tree_host
->SetRootLayer(parent
.get());
1054 AssertLayerTreeHostMatchesForSubtree(parent
.get(),
1055 first_layer_tree_host
.get());
1057 // Now re-root the tree to a new host (simulating what we do on a context lost
1058 // event). This should update the host pointers for all layers in the tree.
1059 scoped_ptr
<LayerTreeHost
> second_layer_tree_host
= factory
.Create();
1060 second_layer_tree_host
->SetRootLayer(parent
.get());
1062 AssertLayerTreeHostMatchesForSubtree(parent
.get(),
1063 second_layer_tree_host
.get());
1065 second_layer_tree_host
->SetRootLayer(nullptr);
1068 TEST(LayerLayerTreeHostTest
, ChangeHostInSubtree
) {
1069 scoped_refptr
<Layer
> first_parent
= Layer::Create();
1070 scoped_refptr
<Layer
> first_child
= Layer::Create();
1071 scoped_refptr
<Layer
> second_parent
= Layer::Create();
1072 scoped_refptr
<Layer
> second_child
= Layer::Create();
1073 scoped_refptr
<Layer
> second_grand_child
= Layer::Create();
1075 // First put all children under the first parent and set the first host.
1076 first_parent
->AddChild(first_child
);
1077 second_child
->AddChild(second_grand_child
);
1078 first_parent
->AddChild(second_child
);
1080 LayerTreeHostFactory factory
;
1081 scoped_ptr
<LayerTreeHost
> first_layer_tree_host
= factory
.Create();
1082 first_layer_tree_host
->SetRootLayer(first_parent
.get());
1084 AssertLayerTreeHostMatchesForSubtree(first_parent
.get(),
1085 first_layer_tree_host
.get());
1087 // Now reparent the subtree starting at second_child to a layer in a different
1089 scoped_ptr
<LayerTreeHost
> second_layer_tree_host
= factory
.Create();
1090 second_layer_tree_host
->SetRootLayer(second_parent
.get());
1092 second_parent
->AddChild(second_child
);
1094 // The moved layer and its children should point to the new host.
1095 EXPECT_EQ(second_layer_tree_host
.get(), second_child
->layer_tree_host());
1096 EXPECT_EQ(second_layer_tree_host
.get(),
1097 second_grand_child
->layer_tree_host());
1099 // Test over, cleanup time.
1100 first_layer_tree_host
->SetRootLayer(nullptr);
1101 second_layer_tree_host
->SetRootLayer(nullptr);
1104 TEST(LayerLayerTreeHostTest
, ReplaceMaskAndReplicaLayer
) {
1105 scoped_refptr
<Layer
> parent
= Layer::Create();
1106 scoped_refptr
<Layer
> mask
= Layer::Create();
1107 scoped_refptr
<Layer
> replica
= Layer::Create();
1108 scoped_refptr
<Layer
> mask_child
= Layer::Create();
1109 scoped_refptr
<Layer
> replica_child
= Layer::Create();
1110 scoped_refptr
<Layer
> mask_replacement
= Layer::Create();
1111 scoped_refptr
<Layer
> replica_replacement
= Layer::Create();
1113 parent
->SetMaskLayer(mask
.get());
1114 parent
->SetReplicaLayer(replica
.get());
1115 mask
->AddChild(mask_child
);
1116 replica
->AddChild(replica_child
);
1118 LayerTreeHostFactory factory
;
1119 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
1120 layer_tree_host
->SetRootLayer(parent
.get());
1122 AssertLayerTreeHostMatchesForSubtree(parent
.get(), layer_tree_host
.get());
1124 // Replacing the mask should clear out the old mask's subtree's host pointers.
1125 parent
->SetMaskLayer(mask_replacement
.get());
1126 EXPECT_EQ(nullptr, mask
->layer_tree_host());
1127 EXPECT_EQ(nullptr, mask_child
->layer_tree_host());
1129 // Same for replacing a replica layer.
1130 parent
->SetReplicaLayer(replica_replacement
.get());
1131 EXPECT_EQ(nullptr, replica
->layer_tree_host());
1132 EXPECT_EQ(nullptr, replica_child
->layer_tree_host());
1134 // Test over, cleanup time.
1135 layer_tree_host
->SetRootLayer(nullptr);
1138 TEST(LayerLayerTreeHostTest
, DestroyHostWithNonNullRootLayer
) {
1139 scoped_refptr
<Layer
> root
= Layer::Create();
1140 scoped_refptr
<Layer
> child
= Layer::Create();
1141 root
->AddChild(child
);
1142 LayerTreeHostFactory factory
;
1143 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
1144 layer_tree_host
->SetRootLayer(root
);
1147 static bool AddTestAnimation(Layer
* layer
) {
1148 scoped_ptr
<KeyframedFloatAnimationCurve
> curve
=
1149 KeyframedFloatAnimationCurve::Create();
1150 curve
->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.3f
, nullptr));
1152 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 0.7f
, nullptr));
1153 scoped_ptr
<Animation
> animation
=
1154 Animation::Create(curve
.Pass(), 0, 0, Animation::OPACITY
);
1156 return layer
->AddAnimation(animation
.Pass());
1159 TEST(LayerLayerTreeHostTest
, ShouldNotAddAnimationWithoutAnimationRegistrar
) {
1160 scoped_refptr
<Layer
> layer
= Layer::Create();
1162 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1163 // animation should not be accepted.
1164 EXPECT_FALSE(AddTestAnimation(layer
.get()));
1166 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
1167 layer
->layer_animation_controller()->SetAnimationRegistrar(registrar
.get());
1169 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1170 EXPECT_TRUE(AddTestAnimation(layer
.get()));
1172 LayerTreeSettings settings
;
1173 settings
.accelerated_animation_enabled
= false;
1174 LayerTreeHostFactory factory
;
1175 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create(settings
);
1176 layer_tree_host
->SetRootLayer(layer
);
1177 AssertLayerTreeHostMatchesForSubtree(layer
.get(), layer_tree_host
.get());
1179 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1180 // animation should be rejected.
1181 EXPECT_FALSE(AddTestAnimation(layer
.get()));
1184 TEST_F(LayerTest
, SafeOpaqueBackgroundColor
) {
1185 LayerTreeHostFactory factory
;
1186 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
1188 scoped_refptr
<Layer
> layer
= Layer::Create();
1189 layer_tree_host
->SetRootLayer(layer
);
1191 for (int contents_opaque
= 0; contents_opaque
< 2; ++contents_opaque
) {
1192 for (int layer_opaque
= 0; layer_opaque
< 2; ++layer_opaque
) {
1193 for (int host_opaque
= 0; host_opaque
< 2; ++host_opaque
) {
1194 layer
->SetContentsOpaque(!!contents_opaque
);
1195 layer
->SetBackgroundColor(layer_opaque
? SK_ColorRED
1196 : SK_ColorTRANSPARENT
);
1197 layer_tree_host
->set_background_color(
1198 host_opaque
? SK_ColorRED
: SK_ColorTRANSPARENT
);
1200 SkColor safe_color
= layer
->SafeOpaqueBackgroundColor();
1201 if (contents_opaque
) {
1202 EXPECT_EQ(SkColorGetA(safe_color
), 255u)
1203 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
1204 << host_opaque
<< "\n";
1206 EXPECT_NE(SkColorGetA(safe_color
), 255u)
1207 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
1208 << host_opaque
<< "\n";
1215 class DrawsContentChangeLayer
: public Layer
{
1217 static scoped_refptr
<DrawsContentChangeLayer
> Create() {
1218 return make_scoped_refptr(new DrawsContentChangeLayer());
1221 void SetLayerTreeHost(LayerTreeHost
* host
) override
{
1222 Layer::SetLayerTreeHost(host
);
1223 SetFakeDrawsContent(!fake_draws_content_
);
1226 bool HasDrawableContent() const override
{
1227 return fake_draws_content_
&& Layer::HasDrawableContent();
1230 void SetFakeDrawsContent(bool fake_draws_content
) {
1231 fake_draws_content_
= fake_draws_content
;
1232 UpdateDrawsContent(HasDrawableContent());
1236 DrawsContentChangeLayer() : Layer(), fake_draws_content_(false) {}
1237 ~DrawsContentChangeLayer() override
{}
1239 bool fake_draws_content_
;
1242 TEST_F(LayerTest
, DrawsContentChangedInSetLayerTreeHost
) {
1243 scoped_refptr
<Layer
> root_layer
= Layer::Create();
1244 scoped_refptr
<DrawsContentChangeLayer
> becomes_not_draws_content
=
1245 DrawsContentChangeLayer::Create();
1246 scoped_refptr
<DrawsContentChangeLayer
> becomes_draws_content
=
1247 DrawsContentChangeLayer::Create();
1248 root_layer
->SetIsDrawable(true);
1249 becomes_not_draws_content
->SetIsDrawable(true);
1250 becomes_not_draws_content
->SetFakeDrawsContent(true);
1251 EXPECT_EQ(0, root_layer
->NumDescendantsThatDrawContent());
1252 root_layer
->AddChild(becomes_not_draws_content
);
1253 EXPECT_EQ(0, root_layer
->NumDescendantsThatDrawContent());
1255 becomes_draws_content
->SetIsDrawable(true);
1256 root_layer
->AddChild(becomes_draws_content
);
1257 EXPECT_EQ(1, root_layer
->NumDescendantsThatDrawContent());
1260 void ReceiveCopyOutputResult(int* result_count
,
1261 scoped_ptr
<CopyOutputResult
> result
) {
1265 TEST_F(LayerTest
, DedupesCopyOutputRequestsBySource
) {
1266 scoped_refptr
<Layer
> layer
= Layer::Create();
1267 int result_count
= 0;
1269 // Create identical requests without the source being set, and expect the
1270 // layer does not abort either one.
1271 scoped_ptr
<CopyOutputRequest
> request
= CopyOutputRequest::CreateRequest(
1272 base::Bind(&ReceiveCopyOutputResult
, &result_count
));
1273 layer
->RequestCopyOfOutput(request
.Pass());
1274 EXPECT_EQ(0, result_count
);
1275 request
= CopyOutputRequest::CreateRequest(
1276 base::Bind(&ReceiveCopyOutputResult
, &result_count
));
1277 layer
->RequestCopyOfOutput(request
.Pass());
1278 EXPECT_EQ(0, result_count
);
1280 // When the layer is destroyed, expect both requests to be aborted.
1282 EXPECT_EQ(2, result_count
);
1284 layer
= Layer::Create();
1287 // Create identical requests, but this time the source is being set. Expect
1288 // the first request from |this| source aborts immediately when the second
1289 // request from |this| source is made.
1290 int did_receive_first_result_from_this_source
= 0;
1291 request
= CopyOutputRequest::CreateRequest(base::Bind(
1292 &ReceiveCopyOutputResult
, &did_receive_first_result_from_this_source
));
1293 request
->set_source(this);
1294 layer
->RequestCopyOfOutput(request
.Pass());
1295 EXPECT_EQ(0, did_receive_first_result_from_this_source
);
1296 // Make a request from a different source.
1297 int did_receive_result_from_different_source
= 0;
1298 request
= CopyOutputRequest::CreateRequest(base::Bind(
1299 &ReceiveCopyOutputResult
, &did_receive_result_from_different_source
));
1300 request
->set_source(reinterpret_cast<void*>(0xdeadbee0));
1301 layer
->RequestCopyOfOutput(request
.Pass());
1302 EXPECT_EQ(0, did_receive_result_from_different_source
);
1303 // Make a request without specifying the source.
1304 int did_receive_result_from_anonymous_source
= 0;
1305 request
= CopyOutputRequest::CreateRequest(base::Bind(
1306 &ReceiveCopyOutputResult
, &did_receive_result_from_anonymous_source
));
1307 layer
->RequestCopyOfOutput(request
.Pass());
1308 EXPECT_EQ(0, did_receive_result_from_anonymous_source
);
1309 // Make the second request from |this| source.
1310 int did_receive_second_result_from_this_source
= 0;
1311 request
= CopyOutputRequest::CreateRequest(base::Bind(
1312 &ReceiveCopyOutputResult
, &did_receive_second_result_from_this_source
));
1313 request
->set_source(this);
1314 layer
->RequestCopyOfOutput(request
.Pass()); // First request to be aborted.
1315 EXPECT_EQ(1, did_receive_first_result_from_this_source
);
1316 EXPECT_EQ(0, did_receive_result_from_different_source
);
1317 EXPECT_EQ(0, did_receive_result_from_anonymous_source
);
1318 EXPECT_EQ(0, did_receive_second_result_from_this_source
);
1320 // When the layer is destroyed, the other three requests should be aborted.
1322 EXPECT_EQ(1, did_receive_first_result_from_this_source
);
1323 EXPECT_EQ(1, did_receive_result_from_different_source
);
1324 EXPECT_EQ(1, did_receive_result_from_anonymous_source
);
1325 EXPECT_EQ(1, did_receive_second_result_from_this_source
);