cc: Convert LTHCommon tests to LayerImpl
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common_unittest.cc
blob927c6e03cd34d332cf77aab691e770777fde3a03
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/trees/layer_tree_host_common.h"
7 #include <algorithm>
8 #include <set>
10 #include "cc/animation/keyframed_animation_curve.h"
11 #include "cc/animation/layer_animation_controller.h"
12 #include "cc/animation/transform_operations.h"
13 #include "cc/base/math_util.h"
14 #include "cc/layers/content_layer_client.h"
15 #include "cc/layers/layer.h"
16 #include "cc/layers/layer_client.h"
17 #include "cc/layers/layer_impl.h"
18 #include "cc/layers/layer_iterator.h"
19 #include "cc/layers/render_surface.h"
20 #include "cc/layers/render_surface_impl.h"
21 #include "cc/output/copy_output_request.h"
22 #include "cc/output/copy_output_result.h"
23 #include "cc/test/animation_test_common.h"
24 #include "cc/test/fake_content_layer_client.h"
25 #include "cc/test/fake_impl_proxy.h"
26 #include "cc/test/fake_layer_tree_host.h"
27 #include "cc/test/fake_layer_tree_host_impl.h"
28 #include "cc/test/fake_picture_layer.h"
29 #include "cc/test/fake_picture_layer_impl.h"
30 #include "cc/test/geometry_test_utils.h"
31 #include "cc/test/layer_tree_host_common_test.h"
32 #include "cc/test/test_task_graph_runner.h"
33 #include "cc/trees/layer_tree_impl.h"
34 #include "cc/trees/proxy.h"
35 #include "cc/trees/single_thread_proxy.h"
36 #include "testing/gmock/include/gmock/gmock.h"
37 #include "testing/gtest/include/gtest/gtest.h"
38 #include "ui/gfx/geometry/quad_f.h"
39 #include "ui/gfx/geometry/vector2d_conversions.h"
40 #include "ui/gfx/transform.h"
42 namespace cc {
43 namespace {
45 class LayerWithForcedDrawsContent : public Layer {
46 public:
47 explicit LayerWithForcedDrawsContent(const LayerSettings& settings)
48 : Layer(settings) {}
50 bool DrawsContent() const override;
52 private:
53 ~LayerWithForcedDrawsContent() override {}
56 bool LayerWithForcedDrawsContent::DrawsContent() const { return true; }
58 class MockContentLayerClient : public ContentLayerClient {
59 public:
60 MockContentLayerClient() {}
61 ~MockContentLayerClient() override {}
62 void PaintContents(SkCanvas* canvas,
63 const gfx::Rect& clip,
64 PaintingControlSetting picture_control) override {}
65 scoped_refptr<DisplayItemList> PaintContentsToDisplayList(
66 const gfx::Rect& clip,
67 PaintingControlSetting picture_control) override {
68 NOTIMPLEMENTED();
69 return nullptr;
71 bool FillsBoundsCompletely() const override { return false; }
72 size_t GetApproximateUnsharedMemoryUsage() const override { return 0; }
75 scoped_refptr<FakePictureLayer> CreateDrawablePictureLayer(
76 const LayerSettings& settings,
77 ContentLayerClient* delegate) {
78 scoped_refptr<FakePictureLayer> to_return =
79 FakePictureLayer::Create(settings, delegate);
80 to_return->SetIsDrawable(true);
81 return to_return;
84 #define EXPECT_CONTENTS_SCALE_EQ(expected, layer) \
85 do { \
86 EXPECT_FLOAT_EQ(expected, layer->contents_scale_x()); \
87 EXPECT_FLOAT_EQ(expected, layer->contents_scale_y()); \
88 } while (false)
90 #define EXPECT_IDEAL_SCALE_EQ(expected, layer) \
91 do { \
92 EXPECT_FLOAT_EQ(expected, layer->GetIdealContentsScale()); \
93 } while (false)
95 class LayerTreeSettingsScaleContent : public LayerTreeSettings {
96 public:
97 LayerTreeSettingsScaleContent() {
98 layer_transforms_should_scale_layer_contents = true;
102 class LayerTreeHostCommonScalingTest : public LayerTreeHostCommonTest {
103 public:
104 LayerTreeHostCommonScalingTest()
105 : LayerTreeHostCommonTest(LayerTreeSettingsScaleContent()) {}
108 TEST_F(LayerTreeHostCommonTest, TransformsForNoOpLayer) {
109 // Sanity check: For layers positioned at zero, with zero size,
110 // and with identity transforms, then the draw transform,
111 // screen space transform, and the hierarchy passed on to children
112 // layers should also be identity transforms.
114 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
115 scoped_refptr<Layer> child = Layer::Create(layer_settings());
116 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
117 parent->AddChild(child);
118 child->AddChild(grand_child);
120 host()->SetRootLayer(parent);
122 gfx::Transform identity_matrix;
123 SetLayerPropertiesForTesting(parent.get(),
124 identity_matrix,
125 gfx::Point3F(),
126 gfx::PointF(),
127 gfx::Size(100, 100),
128 true,
129 false);
130 SetLayerPropertiesForTesting(child.get(),
131 identity_matrix,
132 gfx::Point3F(),
133 gfx::PointF(),
134 gfx::Size(),
135 true,
136 false);
137 SetLayerPropertiesForTesting(grand_child.get(),
138 identity_matrix,
139 gfx::Point3F(),
140 gfx::PointF(),
141 gfx::Size(),
142 true,
143 false);
145 ExecuteCalculateDrawProperties(parent.get());
147 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
148 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
149 child->screen_space_transform());
150 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
151 grand_child->draw_transform());
152 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
153 grand_child->screen_space_transform());
156 TEST_F(LayerTreeHostCommonTest, DoNotSkipLayersWithHandlers) {
157 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
158 scoped_refptr<Layer> child = Layer::Create(layer_settings());
159 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
160 parent->AddChild(child);
161 child->AddChild(grand_child);
163 host()->SetRootLayer(parent);
165 gfx::Transform identity_matrix;
166 SetLayerPropertiesForTesting(parent.get(),
167 identity_matrix,
168 gfx::Point3F(),
169 gfx::PointF(),
170 gfx::Size(100, 100),
171 true,
172 false);
173 SetLayerPropertiesForTesting(child.get(),
174 identity_matrix,
175 gfx::Point3F(),
176 gfx::PointF(10, 10),
177 gfx::Size(100, 100),
178 true,
179 false);
180 // This would have previously caused us to skip our subtree, but this would be
181 // wrong; we need up-to-date draw properties to do hit testing on the layers
182 // with handlers.
183 child->SetOpacity(0.f);
184 SetLayerPropertiesForTesting(grand_child.get(),
185 identity_matrix,
186 gfx::Point3F(),
187 gfx::PointF(10, 10),
188 gfx::Size(100, 100),
189 true,
190 false);
191 grand_child->SetTouchEventHandlerRegion(gfx::Rect(0, 0, 100, 100));
193 ExecuteCalculateDrawProperties(parent.get());
195 // Check that we've computed draw properties for the subtree rooted at
196 // |child|.
197 EXPECT_FALSE(child->draw_transform().IsIdentity());
198 EXPECT_FALSE(grand_child->draw_transform().IsIdentity());
201 TEST_F(LayerTreeHostCommonTest, TransformsForSingleLayer) {
202 gfx::Transform identity_matrix;
203 scoped_refptr<Layer> layer = Layer::Create(layer_settings());
205 scoped_refptr<Layer> root = Layer::Create(layer_settings());
206 SetLayerPropertiesForTesting(root.get(),
207 identity_matrix,
208 gfx::Point3F(),
209 gfx::PointF(),
210 gfx::Size(1, 2),
211 true,
212 false);
213 root->AddChild(layer);
215 host()->SetRootLayer(root);
217 // Case 2: Setting the bounds of the layer should not affect either the draw
218 // transform or the screenspace transform.
219 gfx::Transform translation_to_center;
220 translation_to_center.Translate(5.0, 6.0);
221 SetLayerPropertiesForTesting(layer.get(),
222 identity_matrix,
223 gfx::Point3F(),
224 gfx::PointF(),
225 gfx::Size(10, 12),
226 true,
227 false);
228 ExecuteCalculateDrawProperties(root.get());
229 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, layer->draw_transform());
230 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
231 layer->screen_space_transform());
233 // Case 3: The anchor point by itself (without a layer transform) should have
234 // no effect on the transforms.
235 SetLayerPropertiesForTesting(layer.get(),
236 identity_matrix,
237 gfx::Point3F(2.5f, 3.0f, 0.f),
238 gfx::PointF(),
239 gfx::Size(10, 12),
240 true,
241 false);
242 ExecuteCalculateDrawProperties(root.get());
243 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, layer->draw_transform());
244 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
245 layer->screen_space_transform());
247 // Case 4: A change in actual position affects both the draw transform and
248 // screen space transform.
249 gfx::Transform position_transform;
250 position_transform.Translate(0.f, 1.2f);
251 SetLayerPropertiesForTesting(layer.get(),
252 identity_matrix,
253 gfx::Point3F(2.5f, 3.0f, 0.f),
254 gfx::PointF(0.f, 1.2f),
255 gfx::Size(10, 12),
256 true,
257 false);
258 ExecuteCalculateDrawProperties(root.get());
259 EXPECT_TRANSFORMATION_MATRIX_EQ(position_transform, layer->draw_transform());
260 EXPECT_TRANSFORMATION_MATRIX_EQ(position_transform,
261 layer->screen_space_transform());
263 // Case 5: In the correct sequence of transforms, the layer transform should
264 // pre-multiply the translation_to_center. This is easily tested by using a
265 // scale transform, because scale and translation are not commutative.
266 gfx::Transform layer_transform;
267 layer_transform.Scale3d(2.0, 2.0, 1.0);
268 SetLayerPropertiesForTesting(layer.get(),
269 layer_transform,
270 gfx::Point3F(),
271 gfx::PointF(),
272 gfx::Size(10, 12),
273 true,
274 false);
275 ExecuteCalculateDrawProperties(root.get());
276 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_transform, layer->draw_transform());
277 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_transform,
278 layer->screen_space_transform());
280 // Case 6: The layer transform should occur with respect to the anchor point.
281 gfx::Transform translation_to_anchor;
282 translation_to_anchor.Translate(5.0, 0.0);
283 gfx::Transform expected_result =
284 translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
285 SetLayerPropertiesForTesting(layer.get(),
286 layer_transform,
287 gfx::Point3F(5.0f, 0.f, 0.f),
288 gfx::PointF(),
289 gfx::Size(10, 12),
290 true,
291 false);
292 ExecuteCalculateDrawProperties(root.get());
293 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result, layer->draw_transform());
294 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result,
295 layer->screen_space_transform());
297 // Case 7: Verify that position pre-multiplies the layer transform. The
298 // current implementation of CalculateDrawProperties does this implicitly, but
299 // it is still worth testing to detect accidental regressions.
300 expected_result = position_transform * translation_to_anchor *
301 layer_transform * Inverse(translation_to_anchor);
302 SetLayerPropertiesForTesting(layer.get(),
303 layer_transform,
304 gfx::Point3F(5.0f, 0.f, 0.f),
305 gfx::PointF(0.f, 1.2f),
306 gfx::Size(10, 12),
307 true,
308 false);
309 ExecuteCalculateDrawProperties(root.get());
310 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result, layer->draw_transform());
311 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result,
312 layer->screen_space_transform());
315 TEST_F(LayerTreeHostCommonTest, TransformsAboutScrollOffset) {
316 const gfx::ScrollOffset kScrollOffset(50, 100);
317 const gfx::Vector2dF kScrollDelta(2.34f, 5.67f);
318 const gfx::Vector2d kMaxScrollOffset(200, 200);
319 const gfx::PointF kScrollLayerPosition(-kScrollOffset.x(),
320 -kScrollOffset.y());
321 const float kPageScale = 0.888f;
322 const float kDeviceScale = 1.666f;
324 FakeImplProxy proxy;
325 TestSharedBitmapManager shared_bitmap_manager;
326 TestTaskGraphRunner task_graph_runner;
327 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
328 &task_graph_runner);
330 gfx::Transform identity_matrix;
331 scoped_ptr<LayerImpl> sublayer_scoped_ptr(
332 LayerImpl::Create(host_impl.active_tree(), 1));
333 LayerImpl* sublayer = sublayer_scoped_ptr.get();
334 SetLayerPropertiesForTesting(sublayer, identity_matrix, gfx::Point3F(),
335 gfx::PointF(), gfx::Size(500, 500), true, false,
336 false);
338 scoped_ptr<LayerImpl> scroll_layer_scoped_ptr(
339 LayerImpl::Create(host_impl.active_tree(), 2));
340 LayerImpl* scroll_layer = scroll_layer_scoped_ptr.get();
341 SetLayerPropertiesForTesting(scroll_layer, identity_matrix, gfx::Point3F(),
342 gfx::PointF(), gfx::Size(10, 20), true, false,
343 false);
344 scoped_ptr<LayerImpl> clip_layer_scoped_ptr(
345 LayerImpl::Create(host_impl.active_tree(), 4));
346 LayerImpl* clip_layer = clip_layer_scoped_ptr.get();
348 scroll_layer->SetScrollClipLayer(clip_layer->id());
349 clip_layer->SetBounds(
350 gfx::Size(scroll_layer->bounds().width() + kMaxScrollOffset.x(),
351 scroll_layer->bounds().height() + kMaxScrollOffset.y()));
352 scroll_layer->SetScrollClipLayer(clip_layer->id());
353 scroll_layer->SetScrollDelta(kScrollDelta);
354 gfx::Transform impl_transform;
355 scroll_layer->AddChild(sublayer_scoped_ptr.Pass());
356 LayerImpl* scroll_layer_raw_ptr = scroll_layer_scoped_ptr.get();
357 clip_layer->AddChild(scroll_layer_scoped_ptr.Pass());
358 scroll_layer_raw_ptr->PushScrollOffsetFromMainThread(kScrollOffset);
360 scoped_ptr<LayerImpl> root(LayerImpl::Create(host_impl.active_tree(), 3));
361 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
362 gfx::PointF(), gfx::Size(3, 4), true, false,
363 false);
364 root->AddChild(clip_layer_scoped_ptr.Pass());
365 root->SetHasRenderSurface(true);
367 ExecuteCalculateDrawProperties(
368 root.get(), kDeviceScale, kPageScale, scroll_layer->parent());
369 gfx::Transform expected_transform = identity_matrix;
370 gfx::PointF sub_layer_screen_position = kScrollLayerPosition - kScrollDelta;
371 expected_transform.Translate(MathUtil::Round(sub_layer_screen_position.x() *
372 kPageScale * kDeviceScale),
373 MathUtil::Round(sub_layer_screen_position.y() *
374 kPageScale * kDeviceScale));
375 expected_transform.Scale(kPageScale * kDeviceScale,
376 kPageScale * kDeviceScale);
377 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
378 sublayer->draw_transform());
379 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
380 sublayer->screen_space_transform());
382 gfx::Transform arbitrary_translate;
383 const float kTranslateX = 10.6f;
384 const float kTranslateY = 20.6f;
385 arbitrary_translate.Translate(kTranslateX, kTranslateY);
386 SetLayerPropertiesForTesting(scroll_layer, arbitrary_translate,
387 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 20),
388 true, false, false);
389 ExecuteCalculateDrawProperties(
390 root.get(), kDeviceScale, kPageScale, scroll_layer->parent());
391 expected_transform.MakeIdentity();
392 expected_transform.Translate(
393 MathUtil::Round(kTranslateX * kPageScale * kDeviceScale +
394 sub_layer_screen_position.x() * kPageScale *
395 kDeviceScale),
396 MathUtil::Round(kTranslateY * kPageScale * kDeviceScale +
397 sub_layer_screen_position.y() * kPageScale *
398 kDeviceScale));
399 expected_transform.Scale(kPageScale * kDeviceScale,
400 kPageScale * kDeviceScale);
401 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
402 sublayer->draw_transform());
405 TEST_F(LayerTreeHostCommonTest, TransformsForSimpleHierarchy) {
406 gfx::Transform identity_matrix;
407 scoped_refptr<Layer> root = Layer::Create(layer_settings());
408 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
409 scoped_refptr<Layer> child = Layer::Create(layer_settings());
410 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
411 root->AddChild(parent);
412 parent->AddChild(child);
413 child->AddChild(grand_child);
415 host()->SetRootLayer(root);
417 // One-time setup of root layer
418 SetLayerPropertiesForTesting(root.get(),
419 identity_matrix,
420 gfx::Point3F(),
421 gfx::PointF(),
422 gfx::Size(1, 2),
423 true,
424 false);
426 // Case 1: parent's anchor point should not affect child or grand_child.
427 SetLayerPropertiesForTesting(parent.get(),
428 identity_matrix,
429 gfx::Point3F(2.5f, 3.0f, 0.f),
430 gfx::PointF(),
431 gfx::Size(10, 12),
432 true,
433 false);
434 SetLayerPropertiesForTesting(child.get(),
435 identity_matrix,
436 gfx::Point3F(),
437 gfx::PointF(),
438 gfx::Size(16, 18),
439 true,
440 false);
441 SetLayerPropertiesForTesting(grand_child.get(),
442 identity_matrix,
443 gfx::Point3F(),
444 gfx::PointF(),
445 gfx::Size(76, 78),
446 true,
447 false);
448 ExecuteCalculateDrawProperties(root.get());
449 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
450 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
451 child->screen_space_transform());
452 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
453 grand_child->draw_transform());
454 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
455 grand_child->screen_space_transform());
457 // Case 2: parent's position affects child and grand_child.
458 gfx::Transform parent_position_transform;
459 parent_position_transform.Translate(0.f, 1.2f);
460 SetLayerPropertiesForTesting(parent.get(),
461 identity_matrix,
462 gfx::Point3F(2.5f, 3.0f, 0.f),
463 gfx::PointF(0.f, 1.2f),
464 gfx::Size(10, 12),
465 true,
466 false);
467 SetLayerPropertiesForTesting(child.get(),
468 identity_matrix,
469 gfx::Point3F(),
470 gfx::PointF(),
471 gfx::Size(16, 18),
472 true,
473 false);
474 SetLayerPropertiesForTesting(grand_child.get(),
475 identity_matrix,
476 gfx::Point3F(),
477 gfx::PointF(),
478 gfx::Size(76, 78),
479 true,
480 false);
481 ExecuteCalculateDrawProperties(root.get());
482 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
483 child->draw_transform());
484 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
485 child->screen_space_transform());
486 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
487 grand_child->draw_transform());
488 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
489 grand_child->screen_space_transform());
491 // Case 3: parent's local transform affects child and grandchild
492 gfx::Transform parent_layer_transform;
493 parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
494 gfx::Transform parent_translation_to_anchor;
495 parent_translation_to_anchor.Translate(2.5, 3.0);
496 gfx::Transform parent_composite_transform =
497 parent_translation_to_anchor * parent_layer_transform *
498 Inverse(parent_translation_to_anchor);
499 SetLayerPropertiesForTesting(parent.get(),
500 parent_layer_transform,
501 gfx::Point3F(2.5f, 3.0f, 0.f),
502 gfx::PointF(),
503 gfx::Size(10, 12),
504 true,
505 false);
506 SetLayerPropertiesForTesting(child.get(),
507 identity_matrix,
508 gfx::Point3F(),
509 gfx::PointF(),
510 gfx::Size(16, 18),
511 true,
512 false);
513 SetLayerPropertiesForTesting(grand_child.get(),
514 identity_matrix,
515 gfx::Point3F(),
516 gfx::PointF(),
517 gfx::Size(76, 78),
518 true,
519 false);
520 ExecuteCalculateDrawProperties(root.get());
521 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
522 child->draw_transform());
523 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
524 child->screen_space_transform());
525 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
526 grand_child->draw_transform());
527 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
528 grand_child->screen_space_transform());
531 TEST_F(LayerTreeHostCommonTest, TransformsForSingleRenderSurface) {
532 scoped_refptr<Layer> root = Layer::Create(layer_settings());
533 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
534 scoped_refptr<Layer> child = Layer::Create(layer_settings());
535 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
536 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
537 root->AddChild(parent);
538 parent->AddChild(child);
539 child->AddChild(grand_child);
541 host()->SetRootLayer(root);
543 // One-time setup of root layer
544 gfx::Transform identity_matrix;
545 SetLayerPropertiesForTesting(root.get(),
546 identity_matrix,
547 gfx::Point3F(),
548 gfx::PointF(),
549 gfx::Size(1, 2),
550 true,
551 false);
553 // Child is set up so that a new render surface should be created.
554 child->SetOpacity(0.5f);
555 child->SetForceRenderSurface(true);
557 gfx::Transform parent_layer_transform;
558 parent_layer_transform.Scale3d(1.f, 0.9f, 1.f);
559 gfx::Transform parent_translation_to_anchor;
560 parent_translation_to_anchor.Translate(25.0, 30.0);
562 gfx::Transform parent_composite_transform =
563 parent_translation_to_anchor * parent_layer_transform *
564 Inverse(parent_translation_to_anchor);
565 gfx::Vector2dF parent_composite_scale =
566 MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
567 1.f);
568 gfx::Transform surface_sublayer_transform;
569 surface_sublayer_transform.Scale(parent_composite_scale.x(),
570 parent_composite_scale.y());
571 gfx::Transform surface_sublayer_composite_transform =
572 parent_composite_transform * Inverse(surface_sublayer_transform);
574 // Child's render surface should not exist yet.
575 ASSERT_FALSE(child->render_surface());
577 SetLayerPropertiesForTesting(parent.get(),
578 parent_layer_transform,
579 gfx::Point3F(25.0f, 30.0f, 0.f),
580 gfx::PointF(),
581 gfx::Size(100, 120),
582 true,
583 false);
584 SetLayerPropertiesForTesting(child.get(),
585 identity_matrix,
586 gfx::Point3F(),
587 gfx::PointF(),
588 gfx::Size(16, 18),
589 true,
590 false);
591 SetLayerPropertiesForTesting(grand_child.get(),
592 identity_matrix,
593 gfx::Point3F(),
594 gfx::PointF(),
595 gfx::Size(8, 10),
596 true,
597 false);
598 ExecuteCalculateDrawProperties(root.get());
600 // Render surface should have been created now.
601 ASSERT_TRUE(child->render_surface());
602 ASSERT_EQ(child.get(), child->render_target());
604 // The child layer's draw transform should refer to its new render surface.
605 // The screen-space transform, however, should still refer to the root.
606 EXPECT_TRANSFORMATION_MATRIX_EQ(surface_sublayer_transform,
607 child->draw_transform());
608 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
609 child->screen_space_transform());
611 // Because the grand_child is the only drawable content, the child's render
612 // surface will tighten its bounds to the grand_child. The scale at which the
613 // surface's subtree is drawn must be removed from the composite transform.
614 EXPECT_TRANSFORMATION_MATRIX_EQ(
615 surface_sublayer_composite_transform,
616 child->render_target()->render_surface()->draw_transform());
618 // The screen space is the same as the target since the child surface draws
619 // into the root.
620 EXPECT_TRANSFORMATION_MATRIX_EQ(
621 surface_sublayer_composite_transform,
622 child->render_target()->render_surface()->screen_space_transform());
625 TEST_F(LayerTreeHostCommonTest, TransformsForReplica) {
626 scoped_refptr<Layer> root = Layer::Create(layer_settings());
627 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
628 scoped_refptr<Layer> child = Layer::Create(layer_settings());
629 scoped_refptr<Layer> child_replica = Layer::Create(layer_settings());
630 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
631 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
632 root->AddChild(parent);
633 parent->AddChild(child);
634 child->AddChild(grand_child);
635 child->SetReplicaLayer(child_replica.get());
637 host()->SetRootLayer(root);
639 // One-time setup of root layer
640 gfx::Transform identity_matrix;
641 SetLayerPropertiesForTesting(root.get(),
642 identity_matrix,
643 gfx::Point3F(),
644 gfx::PointF(),
645 gfx::Size(1, 2),
646 true,
647 false);
649 // Child is set up so that a new render surface should be created.
650 child->SetOpacity(0.5f);
652 gfx::Transform parent_layer_transform;
653 parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
654 gfx::Transform parent_translation_to_anchor;
655 parent_translation_to_anchor.Translate(2.5, 3.0);
656 gfx::Transform parent_composite_transform =
657 parent_translation_to_anchor * parent_layer_transform *
658 Inverse(parent_translation_to_anchor);
659 gfx::Transform replica_layer_transform;
660 replica_layer_transform.Scale3d(3.0, 3.0, 1.0);
661 gfx::Vector2dF parent_composite_scale =
662 MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
663 1.f);
664 gfx::Transform surface_sublayer_transform;
665 surface_sublayer_transform.Scale(parent_composite_scale.x(),
666 parent_composite_scale.y());
667 gfx::Transform replica_composite_transform =
668 parent_composite_transform * replica_layer_transform *
669 Inverse(surface_sublayer_transform);
670 child_replica->SetIsDrawable(true);
671 // Child's render surface should not exist yet.
672 ASSERT_FALSE(child->render_surface());
674 SetLayerPropertiesForTesting(parent.get(),
675 parent_layer_transform,
676 gfx::Point3F(2.5f, 3.0f, 0.f),
677 gfx::PointF(),
678 gfx::Size(10, 12),
679 true,
680 false);
681 SetLayerPropertiesForTesting(child.get(),
682 identity_matrix,
683 gfx::Point3F(),
684 gfx::PointF(),
685 gfx::Size(16, 18),
686 true,
687 false);
688 SetLayerPropertiesForTesting(grand_child.get(),
689 identity_matrix,
690 gfx::Point3F(),
691 gfx::PointF(-0.5f, -0.5f),
692 gfx::Size(1, 1),
693 true,
694 false);
695 SetLayerPropertiesForTesting(child_replica.get(),
696 replica_layer_transform,
697 gfx::Point3F(),
698 gfx::PointF(),
699 gfx::Size(),
700 true,
701 false);
702 ExecuteCalculateDrawProperties(root.get());
704 // Render surface should have been created now.
705 ASSERT_TRUE(child->render_surface());
706 ASSERT_EQ(child.get(), child->render_target());
708 EXPECT_TRANSFORMATION_MATRIX_EQ(
709 replica_composite_transform,
710 child->render_target()->render_surface()->replica_draw_transform());
711 EXPECT_TRANSFORMATION_MATRIX_EQ(replica_composite_transform,
712 child->render_target()
713 ->render_surface()
714 ->replica_screen_space_transform());
717 TEST_F(LayerTreeHostCommonTest, TransformsForRenderSurfaceHierarchy) {
718 // This test creates a more complex tree and verifies it all at once. This
719 // covers the following cases:
720 // - layers that are described w.r.t. a render surface: should have draw
721 // transforms described w.r.t. that surface
722 // - A render surface described w.r.t. an ancestor render surface: should
723 // have a draw transform described w.r.t. that ancestor surface
724 // - Replicas of a render surface are described w.r.t. the replica's
725 // transform around its anchor, along with the surface itself.
726 // - Sanity check on recursion: verify transforms of layers described w.r.t.
727 // a render surface that is described w.r.t. an ancestor render surface.
728 // - verifying that each layer has a reference to the correct render surface
729 // and render target values.
731 scoped_refptr<Layer> root = Layer::Create(layer_settings());
732 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
733 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
734 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
735 scoped_refptr<Layer> child_of_root = Layer::Create(layer_settings());
736 scoped_refptr<Layer> child_of_rs1 = Layer::Create(layer_settings());
737 scoped_refptr<Layer> child_of_rs2 = Layer::Create(layer_settings());
738 scoped_refptr<Layer> replica_of_rs1 = Layer::Create(layer_settings());
739 scoped_refptr<Layer> replica_of_rs2 = Layer::Create(layer_settings());
740 scoped_refptr<Layer> grand_child_of_root = Layer::Create(layer_settings());
741 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs1 =
742 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
743 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs2 =
744 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
745 root->AddChild(parent);
746 parent->AddChild(render_surface1);
747 parent->AddChild(child_of_root);
748 render_surface1->AddChild(child_of_rs1);
749 render_surface1->AddChild(render_surface2);
750 render_surface2->AddChild(child_of_rs2);
751 child_of_root->AddChild(grand_child_of_root);
752 child_of_rs1->AddChild(grand_child_of_rs1);
753 child_of_rs2->AddChild(grand_child_of_rs2);
754 render_surface1->SetReplicaLayer(replica_of_rs1.get());
755 render_surface2->SetReplicaLayer(replica_of_rs2.get());
757 host()->SetRootLayer(root);
759 // In combination with descendant draws content, opacity != 1 forces the layer
760 // to have a new render surface.
761 render_surface1->SetOpacity(0.5f);
762 render_surface2->SetOpacity(0.33f);
764 // One-time setup of root layer
765 gfx::Transform identity_matrix;
766 SetLayerPropertiesForTesting(root.get(),
767 identity_matrix,
768 gfx::Point3F(),
769 gfx::PointF(),
770 gfx::Size(1, 2),
771 true,
772 false);
774 // All layers in the tree are initialized with an anchor at .25 and a size of
775 // (10,10). matrix "A" is the composite layer transform used in all layers,
776 // Matrix "R" is the composite replica transform used in all replica layers.
777 gfx::Transform translation_to_anchor;
778 translation_to_anchor.Translate(2.5, 0.0);
779 gfx::Transform layer_transform;
780 layer_transform.Translate(1.0, 1.0);
781 gfx::Transform replica_layer_transform;
782 replica_layer_transform.Scale3d(-2.0, 5.0, 1.0);
784 gfx::Transform A =
785 translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
786 gfx::Transform R = A * translation_to_anchor * replica_layer_transform *
787 Inverse(translation_to_anchor);
789 gfx::Vector2dF surface1_parent_transform_scale =
790 MathUtil::ComputeTransform2dScaleComponents(A, 1.f);
791 gfx::Transform surface1_sublayer_transform;
792 surface1_sublayer_transform.Scale(surface1_parent_transform_scale.x(),
793 surface1_parent_transform_scale.y());
795 // SS1 = transform given to the subtree of render_surface1
796 gfx::Transform SS1 = surface1_sublayer_transform;
797 // S1 = transform to move from render_surface1 pixels to the layer space of
798 // the owning layer
799 gfx::Transform S1 = Inverse(surface1_sublayer_transform);
801 gfx::Vector2dF surface2_parent_transform_scale =
802 MathUtil::ComputeTransform2dScaleComponents(SS1 * A, 1.f);
803 gfx::Transform surface2_sublayer_transform;
804 surface2_sublayer_transform.Scale(surface2_parent_transform_scale.x(),
805 surface2_parent_transform_scale.y());
807 // SS2 = transform given to the subtree of render_surface2
808 gfx::Transform SS2 = surface2_sublayer_transform;
809 // S2 = transform to move from render_surface2 pixels to the layer space of
810 // the owning layer
811 gfx::Transform S2 = Inverse(surface2_sublayer_transform);
813 SetLayerPropertiesForTesting(parent.get(),
814 layer_transform,
815 gfx::Point3F(2.5f, 0.f, 0.f),
816 gfx::PointF(),
817 gfx::Size(10, 10),
818 true,
819 false);
820 SetLayerPropertiesForTesting(render_surface1.get(),
821 layer_transform,
822 gfx::Point3F(2.5f, 0.f, 0.f),
823 gfx::PointF(),
824 gfx::Size(10, 10),
825 true,
826 false);
827 SetLayerPropertiesForTesting(render_surface2.get(),
828 layer_transform,
829 gfx::Point3F(2.5f, 0.f, 0.f),
830 gfx::PointF(),
831 gfx::Size(10, 10),
832 true,
833 false);
834 SetLayerPropertiesForTesting(child_of_root.get(),
835 layer_transform,
836 gfx::Point3F(2.5f, 0.f, 0.f),
837 gfx::PointF(),
838 gfx::Size(10, 10),
839 true,
840 false);
841 SetLayerPropertiesForTesting(child_of_rs1.get(),
842 layer_transform,
843 gfx::Point3F(2.5f, 0.f, 0.f),
844 gfx::PointF(),
845 gfx::Size(10, 10),
846 true,
847 false);
848 SetLayerPropertiesForTesting(child_of_rs2.get(),
849 layer_transform,
850 gfx::Point3F(2.5f, 0.f, 0.f),
851 gfx::PointF(),
852 gfx::Size(10, 10),
853 true,
854 false);
855 SetLayerPropertiesForTesting(grand_child_of_root.get(),
856 layer_transform,
857 gfx::Point3F(2.5f, 0.f, 0.f),
858 gfx::PointF(),
859 gfx::Size(10, 10),
860 true,
861 false);
862 SetLayerPropertiesForTesting(grand_child_of_rs1.get(),
863 layer_transform,
864 gfx::Point3F(2.5f, 0.f, 0.f),
865 gfx::PointF(),
866 gfx::Size(10, 10),
867 true,
868 false);
869 SetLayerPropertiesForTesting(grand_child_of_rs2.get(),
870 layer_transform,
871 gfx::Point3F(2.5f, 0.f, 0.f),
872 gfx::PointF(),
873 gfx::Size(10, 10),
874 true,
875 false);
876 SetLayerPropertiesForTesting(replica_of_rs1.get(),
877 replica_layer_transform,
878 gfx::Point3F(2.5f, 0.f, 0.f),
879 gfx::PointF(),
880 gfx::Size(),
881 true,
882 false);
883 SetLayerPropertiesForTesting(replica_of_rs2.get(),
884 replica_layer_transform,
885 gfx::Point3F(2.5f, 0.f, 0.f),
886 gfx::PointF(),
887 gfx::Size(),
888 true,
889 false);
891 ExecuteCalculateDrawProperties(root.get());
893 // Only layers that are associated with render surfaces should have an actual
894 // RenderSurface() value.
895 ASSERT_TRUE(root->render_surface());
896 ASSERT_FALSE(child_of_root->render_surface());
897 ASSERT_FALSE(grand_child_of_root->render_surface());
899 ASSERT_TRUE(render_surface1->render_surface());
900 ASSERT_FALSE(child_of_rs1->render_surface());
901 ASSERT_FALSE(grand_child_of_rs1->render_surface());
903 ASSERT_TRUE(render_surface2->render_surface());
904 ASSERT_FALSE(child_of_rs2->render_surface());
905 ASSERT_FALSE(grand_child_of_rs2->render_surface());
907 // Verify all render target accessors
908 EXPECT_EQ(root.get(), parent->render_target());
909 EXPECT_EQ(root.get(), child_of_root->render_target());
910 EXPECT_EQ(root.get(), grand_child_of_root->render_target());
912 EXPECT_EQ(render_surface1.get(), render_surface1->render_target());
913 EXPECT_EQ(render_surface1.get(), child_of_rs1->render_target());
914 EXPECT_EQ(render_surface1.get(), grand_child_of_rs1->render_target());
916 EXPECT_EQ(render_surface2.get(), render_surface2->render_target());
917 EXPECT_EQ(render_surface2.get(), child_of_rs2->render_target());
918 EXPECT_EQ(render_surface2.get(), grand_child_of_rs2->render_target());
920 // Verify layer draw transforms note that draw transforms are described with
921 // respect to the nearest ancestor render surface but screen space transforms
922 // are described with respect to the root.
923 EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->draw_transform());
924 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A, child_of_root->draw_transform());
925 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
926 grand_child_of_root->draw_transform());
928 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1, render_surface1->draw_transform());
929 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A, child_of_rs1->draw_transform());
930 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A * A,
931 grand_child_of_rs1->draw_transform());
933 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2, render_surface2->draw_transform());
934 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A, child_of_rs2->draw_transform());
935 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A * A,
936 grand_child_of_rs2->draw_transform());
938 // Verify layer screen-space transforms
940 EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->screen_space_transform());
941 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A,
942 child_of_root->screen_space_transform());
943 EXPECT_TRANSFORMATION_MATRIX_EQ(
944 A * A * A, grand_child_of_root->screen_space_transform());
946 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A,
947 render_surface1->screen_space_transform());
948 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
949 child_of_rs1->screen_space_transform());
950 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
951 grand_child_of_rs1->screen_space_transform());
953 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
954 render_surface2->screen_space_transform());
955 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
956 child_of_rs2->screen_space_transform());
957 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A * A,
958 grand_child_of_rs2->screen_space_transform());
960 // Verify render surface transforms.
962 // Draw transform of render surface 1 is described with respect to root.
963 EXPECT_TRANSFORMATION_MATRIX_EQ(
964 A * A * S1, render_surface1->render_surface()->draw_transform());
965 EXPECT_TRANSFORMATION_MATRIX_EQ(
966 A * R * S1, render_surface1->render_surface()->replica_draw_transform());
967 EXPECT_TRANSFORMATION_MATRIX_EQ(
968 A * A * S1, render_surface1->render_surface()->screen_space_transform());
969 EXPECT_TRANSFORMATION_MATRIX_EQ(
970 A * R * S1,
971 render_surface1->render_surface()->replica_screen_space_transform());
972 // Draw transform of render surface 2 is described with respect to render
973 // surface 1.
974 EXPECT_TRANSFORMATION_MATRIX_EQ(
975 SS1 * A * S2, render_surface2->render_surface()->draw_transform());
976 EXPECT_TRANSFORMATION_MATRIX_EQ(
977 SS1 * R * S2,
978 render_surface2->render_surface()->replica_draw_transform());
979 EXPECT_TRANSFORMATION_MATRIX_EQ(
980 A * A * A * S2,
981 render_surface2->render_surface()->screen_space_transform());
982 EXPECT_TRANSFORMATION_MATRIX_EQ(
983 A * A * R * S2,
984 render_surface2->render_surface()->replica_screen_space_transform());
986 // Sanity check. If these fail there is probably a bug in the test itself. It
987 // is expected that we correctly set up transforms so that the y-component of
988 // the screen-space transform encodes the "depth" of the layer in the tree.
989 EXPECT_FLOAT_EQ(1.0, parent->screen_space_transform().matrix().get(1, 3));
990 EXPECT_FLOAT_EQ(2.0,
991 child_of_root->screen_space_transform().matrix().get(1, 3));
992 EXPECT_FLOAT_EQ(
993 3.0, grand_child_of_root->screen_space_transform().matrix().get(1, 3));
995 EXPECT_FLOAT_EQ(2.0,
996 render_surface1->screen_space_transform().matrix().get(1, 3));
997 EXPECT_FLOAT_EQ(3.0,
998 child_of_rs1->screen_space_transform().matrix().get(1, 3));
999 EXPECT_FLOAT_EQ(
1000 4.0, grand_child_of_rs1->screen_space_transform().matrix().get(1, 3));
1002 EXPECT_FLOAT_EQ(3.0,
1003 render_surface2->screen_space_transform().matrix().get(1, 3));
1004 EXPECT_FLOAT_EQ(4.0,
1005 child_of_rs2->screen_space_transform().matrix().get(1, 3));
1006 EXPECT_FLOAT_EQ(
1007 5.0, grand_child_of_rs2->screen_space_transform().matrix().get(1, 3));
1010 TEST_F(LayerTreeHostCommonTest, TransformsForFlatteningLayer) {
1011 // For layers that flatten their subtree, there should be an orthographic
1012 // projection (for x and y values) in the middle of the transform sequence.
1013 // Note that the way the code is currently implemented, it is not expected to
1014 // use a canonical orthographic projection.
1016 scoped_refptr<Layer> root = Layer::Create(layer_settings());
1017 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1018 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1019 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1020 scoped_refptr<LayerWithForcedDrawsContent> great_grand_child =
1021 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1023 gfx::Transform rotation_about_y_axis;
1024 rotation_about_y_axis.RotateAboutYAxis(30.0);
1026 const gfx::Transform identity_matrix;
1027 SetLayerPropertiesForTesting(root.get(),
1028 identity_matrix,
1029 gfx::Point3F(),
1030 gfx::PointF(),
1031 gfx::Size(100, 100),
1032 true,
1033 false);
1034 SetLayerPropertiesForTesting(child.get(),
1035 rotation_about_y_axis,
1036 gfx::Point3F(),
1037 gfx::PointF(),
1038 gfx::Size(10, 10),
1039 true,
1040 false);
1041 SetLayerPropertiesForTesting(grand_child.get(),
1042 rotation_about_y_axis,
1043 gfx::Point3F(),
1044 gfx::PointF(),
1045 gfx::Size(10, 10),
1046 true,
1047 false);
1048 SetLayerPropertiesForTesting(great_grand_child.get(), identity_matrix,
1049 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1050 true, false);
1052 root->AddChild(child);
1053 child->AddChild(grand_child);
1054 grand_child->AddChild(great_grand_child);
1055 child->SetForceRenderSurface(true);
1057 host()->SetRootLayer(root);
1059 // No layers in this test should preserve 3d.
1060 ASSERT_TRUE(root->should_flatten_transform());
1061 ASSERT_TRUE(child->should_flatten_transform());
1062 ASSERT_TRUE(grand_child->should_flatten_transform());
1063 ASSERT_TRUE(great_grand_child->should_flatten_transform());
1065 gfx::Transform expected_child_draw_transform = rotation_about_y_axis;
1066 gfx::Transform expected_child_screen_space_transform = rotation_about_y_axis;
1067 gfx::Transform expected_grand_child_draw_transform =
1068 rotation_about_y_axis; // draws onto child's render surface
1069 gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
1070 flattened_rotation_about_y.FlattenTo2d();
1071 gfx::Transform expected_grand_child_screen_space_transform =
1072 flattened_rotation_about_y * rotation_about_y_axis;
1073 gfx::Transform expected_great_grand_child_draw_transform =
1074 flattened_rotation_about_y;
1075 gfx::Transform expected_great_grand_child_screen_space_transform =
1076 flattened_rotation_about_y * flattened_rotation_about_y;
1078 ExecuteCalculateDrawProperties(root.get());
1080 // The child's draw transform should have been taken by its surface.
1081 ASSERT_TRUE(child->render_surface());
1082 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_draw_transform,
1083 child->render_surface()->draw_transform());
1084 EXPECT_TRANSFORMATION_MATRIX_EQ(
1085 expected_child_screen_space_transform,
1086 child->render_surface()->screen_space_transform());
1087 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1088 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_screen_space_transform,
1089 child->screen_space_transform());
1090 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_draw_transform,
1091 grand_child->draw_transform());
1092 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_screen_space_transform,
1093 grand_child->screen_space_transform());
1094 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_great_grand_child_draw_transform,
1095 great_grand_child->draw_transform());
1096 EXPECT_TRANSFORMATION_MATRIX_EQ(
1097 expected_great_grand_child_screen_space_transform,
1098 great_grand_child->screen_space_transform());
1101 TEST_F(LayerTreeHostCommonTest, LayerFullyContainedWithinClipInTargetSpace) {
1102 scoped_refptr<Layer> root = Layer::Create(layer_settings());
1103 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1104 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1105 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1107 gfx::Transform child_transform;
1108 child_transform.Translate(50.0, 50.0);
1109 child_transform.RotateAboutZAxis(30.0);
1111 gfx::Transform grand_child_transform;
1112 grand_child_transform.RotateAboutYAxis(90.0);
1114 const gfx::Transform identity_matrix;
1115 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
1116 gfx::PointF(), gfx::Size(200, 200), true, false);
1117 SetLayerPropertiesForTesting(child.get(), child_transform, gfx::Point3F(),
1118 gfx::PointF(), gfx::Size(10, 10), true, false);
1119 SetLayerPropertiesForTesting(grand_child.get(), grand_child_transform,
1120 gfx::Point3F(), gfx::PointF(),
1121 gfx::Size(100, 100), true, false);
1123 root->AddChild(child);
1124 child->AddChild(grand_child);
1125 grand_child->SetShouldFlattenTransform(false);
1127 host()->SetRootLayer(root);
1129 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
1131 // Mapping grand_child's bounds to target space produces a non-empty rect
1132 // that is fully contained within the target's bounds, so grand_child should
1133 // be considered fully visible.
1134 EXPECT_EQ(gfx::Rect(grand_child->bounds()),
1135 grand_child->visible_rect_from_property_trees());
1138 TEST_F(LayerTreeHostCommonTest, TransformsForDegenerateIntermediateLayer) {
1139 // A layer that is empty in one axis, but not the other, was accidentally
1140 // skipping a necessary translation. Without that translation, the coordinate
1141 // space of the layer's draw transform is incorrect.
1143 // Normally this isn't a problem, because the layer wouldn't be drawn anyway,
1144 // but if that layer becomes a render surface, then its draw transform is
1145 // implicitly inherited by the rest of the subtree, which then is positioned
1146 // incorrectly as a result.
1148 scoped_refptr<Layer> root = Layer::Create(layer_settings());
1149 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1150 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1151 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1153 // The child height is zero, but has non-zero width that should be accounted
1154 // for while computing draw transforms.
1155 const gfx::Transform identity_matrix;
1156 SetLayerPropertiesForTesting(root.get(),
1157 identity_matrix,
1158 gfx::Point3F(),
1159 gfx::PointF(),
1160 gfx::Size(100, 100),
1161 true,
1162 false);
1163 SetLayerPropertiesForTesting(child.get(),
1164 identity_matrix,
1165 gfx::Point3F(),
1166 gfx::PointF(),
1167 gfx::Size(10, 0),
1168 true,
1169 false);
1170 SetLayerPropertiesForTesting(grand_child.get(),
1171 identity_matrix,
1172 gfx::Point3F(),
1173 gfx::PointF(),
1174 gfx::Size(10, 10),
1175 true,
1176 false);
1178 root->AddChild(child);
1179 child->AddChild(grand_child);
1180 child->SetForceRenderSurface(true);
1182 host()->SetRootLayer(root);
1184 ExecuteCalculateDrawProperties(root.get());
1186 ASSERT_TRUE(child->render_surface());
1187 // This is the real test, the rest are sanity checks.
1188 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1189 child->render_surface()->draw_transform());
1190 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1191 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1192 grand_child->draw_transform());
1195 TEST_F(LayerTreeHostCommonTest, TransformAboveRootLayer) {
1196 // Transformations applied at the root of the tree should be forwarded
1197 // to child layers instead of applied to the root RenderSurface.
1198 const gfx::Transform identity_matrix;
1199 scoped_refptr<LayerWithForcedDrawsContent> root =
1200 new LayerWithForcedDrawsContent(layer_settings());
1201 scoped_refptr<LayerWithForcedDrawsContent> child =
1202 new LayerWithForcedDrawsContent(layer_settings());
1203 child->SetScrollClipLayerId(root->id());
1204 root->AddChild(child);
1206 host()->SetRootLayer(root);
1208 SetLayerPropertiesForTesting(root.get(),
1209 identity_matrix,
1210 gfx::Point3F(),
1211 gfx::PointF(),
1212 gfx::Size(20, 20),
1213 true,
1214 false);
1215 SetLayerPropertiesForTesting(child.get(),
1216 identity_matrix,
1217 gfx::Point3F(),
1218 gfx::PointF(),
1219 gfx::Size(20, 20),
1220 true,
1221 false);
1223 gfx::Transform translate;
1224 translate.Translate(50, 50);
1226 RenderSurfaceLayerList render_surface_layer_list;
1227 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1228 root.get(), root->bounds(), translate, &render_surface_layer_list);
1229 inputs.can_adjust_raster_scales = true;
1230 inputs.property_trees->needs_rebuild = true;
1231 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1232 EXPECT_EQ(translate, root->draw_properties().target_space_transform);
1233 EXPECT_EQ(translate, child->draw_properties().target_space_transform);
1234 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1237 gfx::Transform scale;
1238 scale.Scale(2, 2);
1240 RenderSurfaceLayerList render_surface_layer_list;
1241 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1242 root.get(), root->bounds(), scale, &render_surface_layer_list);
1243 inputs.can_adjust_raster_scales = true;
1244 inputs.property_trees->needs_rebuild = true;
1245 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1246 EXPECT_EQ(scale, root->draw_properties().target_space_transform);
1247 EXPECT_EQ(scale, child->draw_properties().target_space_transform);
1248 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1251 gfx::Transform rotate;
1252 rotate.Rotate(2);
1254 RenderSurfaceLayerList render_surface_layer_list;
1255 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1256 root.get(), root->bounds(), rotate, &render_surface_layer_list);
1257 inputs.can_adjust_raster_scales = true;
1258 inputs.property_trees->needs_rebuild = true;
1259 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1260 EXPECT_EQ(rotate, root->draw_properties().target_space_transform);
1261 EXPECT_EQ(rotate, child->draw_properties().target_space_transform);
1262 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1265 gfx::Transform composite;
1266 composite.ConcatTransform(translate);
1267 composite.ConcatTransform(scale);
1268 composite.ConcatTransform(rotate);
1270 RenderSurfaceLayerList render_surface_layer_list;
1271 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1272 root.get(), root->bounds(), composite, &render_surface_layer_list);
1273 inputs.can_adjust_raster_scales = true;
1274 inputs.property_trees->needs_rebuild = true;
1275 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1276 EXPECT_EQ(composite, root->draw_properties().target_space_transform);
1277 EXPECT_EQ(composite, child->draw_properties().target_space_transform);
1278 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1281 // Verify it composes correctly with device scale.
1282 float device_scale_factor = 1.5f;
1285 RenderSurfaceLayerList render_surface_layer_list;
1286 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1287 root.get(), root->bounds(), translate, &render_surface_layer_list);
1288 inputs.device_scale_factor = device_scale_factor;
1289 inputs.can_adjust_raster_scales = true;
1290 inputs.property_trees->needs_rebuild = true;
1291 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1292 gfx::Transform device_scaled_translate = translate;
1293 device_scaled_translate.Scale(device_scale_factor, device_scale_factor);
1294 EXPECT_EQ(device_scaled_translate,
1295 root->draw_properties().target_space_transform);
1296 EXPECT_EQ(device_scaled_translate,
1297 child->draw_properties().target_space_transform);
1298 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1301 // Verify it composes correctly with page scale.
1302 float page_scale_factor = 2.f;
1305 RenderSurfaceLayerList render_surface_layer_list;
1306 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1307 root.get(), root->bounds(), translate, &render_surface_layer_list);
1308 inputs.page_scale_factor = page_scale_factor;
1309 inputs.page_scale_layer = root.get();
1310 inputs.can_adjust_raster_scales = true;
1311 inputs.property_trees->needs_rebuild = true;
1312 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1313 gfx::Transform page_scaled_translate = translate;
1314 page_scaled_translate.Scale(page_scale_factor, page_scale_factor);
1315 EXPECT_EQ(page_scaled_translate,
1316 root->draw_properties().target_space_transform);
1317 EXPECT_EQ(page_scaled_translate,
1318 child->draw_properties().target_space_transform);
1319 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1322 // Verify that it composes correctly with transforms directly on root layer.
1323 root->SetTransform(composite);
1326 RenderSurfaceLayerList render_surface_layer_list;
1327 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1328 root.get(), root->bounds(), composite, &render_surface_layer_list);
1329 inputs.can_adjust_raster_scales = true;
1330 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1331 gfx::Transform compositeSquared = composite;
1332 compositeSquared.ConcatTransform(composite);
1333 EXPECT_TRANSFORMATION_MATRIX_EQ(
1334 compositeSquared, root->draw_properties().target_space_transform);
1335 EXPECT_TRANSFORMATION_MATRIX_EQ(
1336 compositeSquared, child->draw_properties().target_space_transform);
1337 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1341 TEST_F(LayerTreeHostCommonTest,
1342 RenderSurfaceListForRenderSurfaceWithClippedLayer) {
1343 LayerImpl* parent = root_layer();
1344 parent->SetMasksToBounds(true);
1345 LayerImpl* render_surface1 = AddChildToRoot<LayerImpl>();
1346 LayerImpl* child = AddChild<LayerImpl>(render_surface1);
1347 child->SetDrawsContent(true);
1349 const gfx::Transform identity_matrix;
1350 SetLayerPropertiesForTesting(parent, identity_matrix, gfx::Point3F(),
1351 gfx::PointF(), gfx::Size(10, 10), true, false,
1352 true);
1353 SetLayerPropertiesForTesting(render_surface1, identity_matrix, gfx::Point3F(),
1354 gfx::PointF(), gfx::Size(10, 10), true, false,
1355 true);
1356 SetLayerPropertiesForTesting(child, identity_matrix, gfx::Point3F(),
1357 gfx::PointF(30.f, 30.f), gfx::Size(10, 10), true,
1358 false, false);
1360 ExecuteCalculateDrawProperties(parent);
1362 // The child layer's content is entirely outside the parent's clip rect, so
1363 // the intermediate render surface should not be listed here, even if it was
1364 // forced to be created. Render surfaces without children or visible content
1365 // are unexpected at draw time (e.g. we might try to create a content texture
1366 // of size 0).
1367 ASSERT_TRUE(parent->render_surface());
1368 EXPECT_EQ(1U, render_surface_layer_list_impl()->size());
1371 TEST_F(LayerTreeHostCommonTest, RenderSurfaceListForTransparentChild) {
1372 LayerImpl* parent = root_layer();
1373 LayerImpl* render_surface1 = AddChild<LayerImpl>(parent);
1374 LayerImpl* child = AddChild<LayerImpl>(render_surface1);
1375 child->SetDrawsContent(true);
1377 const gfx::Transform identity_matrix;
1378 SetLayerPropertiesForTesting(render_surface1, identity_matrix, gfx::Point3F(),
1379 gfx::PointF(), gfx::Size(10, 10), true, false,
1380 true);
1381 SetLayerPropertiesForTesting(child, identity_matrix, gfx::Point3F(),
1382 gfx::PointF(), gfx::Size(10, 10), true, false,
1383 false);
1384 render_surface1->SetOpacity(0.f);
1386 LayerImplList render_surface_layer_list;
1387 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
1388 parent, parent->bounds(), &render_surface_layer_list);
1389 inputs.can_adjust_raster_scales = true;
1390 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1392 // Since the layer is transparent, render_surface1->render_surface() should
1393 // not have gotten added anywhere. Also, the drawable content rect should not
1394 // have been extended by the children.
1395 ASSERT_TRUE(parent->render_surface());
1396 EXPECT_EQ(0U, parent->render_surface()->layer_list().size());
1397 EXPECT_EQ(1U, render_surface_layer_list.size());
1398 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1399 EXPECT_EQ(gfx::Rect(), parent->drawable_content_rect());
1402 TEST_F(LayerTreeHostCommonTest, RenderSurfaceForBlendMode) {
1403 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1404 scoped_refptr<LayerWithForcedDrawsContent> child =
1405 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1407 host()->SetRootLayer(parent);
1409 const gfx::Transform identity_matrix;
1410 const SkXfermode::Mode blend_mode = SkXfermode::kMultiply_Mode;
1411 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
1412 gfx::PointF(), gfx::Size(10, 10), true, false);
1414 parent->AddChild(child);
1415 child->SetBlendMode(blend_mode);
1416 child->SetOpacity(0.5f);
1418 RenderSurfaceLayerList render_surface_layer_list;
1419 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1420 parent.get(), parent->bounds(), &render_surface_layer_list);
1421 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1423 // Since the child layer has a blend mode other than normal, it should get
1424 // its own render surface. Also, layer's draw_properties should contain the
1425 // default blend mode, since the render surface becomes responsible for
1426 // applying the blend mode.
1427 ASSERT_TRUE(child->render_surface());
1428 EXPECT_EQ(1.0f, child->draw_opacity());
1429 EXPECT_EQ(0.5f, child->render_surface()->draw_opacity());
1430 EXPECT_EQ(SkXfermode::kSrcOver_Mode, child->draw_properties().blend_mode);
1433 TEST_F(LayerTreeHostCommonTest, ForceRenderSurface) {
1434 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1435 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
1436 scoped_refptr<LayerWithForcedDrawsContent> child =
1437 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1438 render_surface1->SetForceRenderSurface(true);
1440 host()->SetRootLayer(parent);
1442 const gfx::Transform identity_matrix;
1443 SetLayerPropertiesForTesting(parent.get(),
1444 identity_matrix,
1445 gfx::Point3F(),
1446 gfx::PointF(),
1447 gfx::Size(10, 10),
1448 true,
1449 false);
1450 SetLayerPropertiesForTesting(render_surface1.get(),
1451 identity_matrix,
1452 gfx::Point3F(),
1453 gfx::PointF(),
1454 gfx::Size(10, 10),
1455 true,
1456 false);
1457 SetLayerPropertiesForTesting(child.get(),
1458 identity_matrix,
1459 gfx::Point3F(),
1460 gfx::PointF(),
1461 gfx::Size(10, 10),
1462 true,
1463 false);
1465 parent->AddChild(render_surface1);
1466 render_surface1->AddChild(child);
1468 // Sanity check before the actual test
1469 EXPECT_FALSE(parent->render_surface());
1470 EXPECT_FALSE(render_surface1->render_surface());
1473 RenderSurfaceLayerList render_surface_layer_list;
1474 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1475 parent.get(), parent->bounds(), &render_surface_layer_list);
1476 inputs.can_adjust_raster_scales = true;
1477 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1479 // The root layer always creates a render surface
1480 EXPECT_TRUE(parent->render_surface());
1481 EXPECT_TRUE(render_surface1->render_surface());
1485 RenderSurfaceLayerList render_surface_layer_list;
1486 render_surface1->SetForceRenderSurface(false);
1487 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1488 parent.get(), parent->bounds(), &render_surface_layer_list);
1489 inputs.can_adjust_raster_scales = true;
1490 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1491 EXPECT_TRUE(parent->render_surface());
1492 EXPECT_FALSE(render_surface1->render_surface());
1496 TEST_F(LayerTreeHostCommonTest, RenderSurfacesFlattenScreenSpaceTransform) {
1497 // Render surfaces act as a flattening point for their subtree, so should
1498 // always flatten the target-to-screen space transform seen by descendants.
1500 scoped_refptr<Layer> root = Layer::Create(layer_settings());
1501 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1502 scoped_refptr<LayerWithForcedDrawsContent> child =
1503 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1504 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1505 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1507 gfx::Transform rotation_about_y_axis;
1508 rotation_about_y_axis.RotateAboutYAxis(30.0);
1509 // Make |parent| have a render surface.
1510 parent->SetOpacity(0.9f);
1512 const gfx::Transform identity_matrix;
1513 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
1514 gfx::PointF(), gfx::Size(100, 100), true, false);
1515 SetLayerPropertiesForTesting(parent.get(), rotation_about_y_axis,
1516 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1517 true, false);
1518 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
1519 gfx::PointF(), gfx::Size(10, 10), true, false);
1520 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
1521 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1522 true, false);
1524 root->AddChild(parent);
1525 parent->AddChild(child);
1526 child->AddChild(grand_child);
1528 grand_child->SetShouldFlattenTransform(false);
1530 host()->SetRootLayer(root);
1532 // Only grand_child should preserve 3d.
1533 EXPECT_TRUE(root->should_flatten_transform());
1534 EXPECT_TRUE(parent->should_flatten_transform());
1535 EXPECT_TRUE(child->should_flatten_transform());
1536 EXPECT_FALSE(grand_child->should_flatten_transform());
1538 gfx::Transform expected_child_draw_transform = identity_matrix;
1539 gfx::Transform expected_grand_child_draw_transform = identity_matrix;
1541 gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
1542 flattened_rotation_about_y.FlattenTo2d();
1544 ExecuteCalculateDrawProperties(root.get());
1546 EXPECT_TRUE(parent->render_surface());
1547 EXPECT_FALSE(child->render_surface());
1548 EXPECT_FALSE(grand_child->render_surface());
1550 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1551 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1552 grand_child->draw_transform());
1554 // The screen-space transform inherited by |child| and |grand_child| should
1555 // have been flattened at their render target. In particular, the fact that
1556 // |grand_child| happens to preserve 3d shouldn't affect this flattening.
1557 EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
1558 child->screen_space_transform());
1559 EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
1560 grand_child->screen_space_transform());
1563 TEST_F(LayerTreeHostCommonTest, ClipRectCullsRenderSurfaces) {
1564 // The entire subtree of layers that are outside the clip rect should be
1565 // culled away, and should not affect the render_surface_layer_list.
1567 // The test tree is set up as follows:
1568 // - all layers except the leaf_nodes are forced to be a new render surface
1569 // that have something to draw.
1570 // - parent is a large container layer.
1571 // - child has masksToBounds=true to cause clipping.
1572 // - grand_child is positioned outside of the child's bounds
1573 // - great_grand_child is also kept outside child's bounds.
1575 // In this configuration, grand_child and great_grand_child are completely
1576 // outside the clip rect, and they should never get scheduled on the list of
1577 // render surfaces.
1580 const gfx::Transform identity_matrix;
1581 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1582 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1583 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
1584 scoped_refptr<Layer> great_grand_child = Layer::Create(layer_settings());
1585 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
1586 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1587 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
1588 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1589 parent->AddChild(child);
1590 child->AddChild(grand_child);
1591 grand_child->AddChild(great_grand_child);
1593 host()->SetRootLayer(parent);
1595 // leaf_node1 ensures that parent and child are kept on the
1596 // render_surface_layer_list, even though grand_child and great_grand_child
1597 // should be clipped.
1598 child->AddChild(leaf_node1);
1599 great_grand_child->AddChild(leaf_node2);
1601 SetLayerPropertiesForTesting(parent.get(),
1602 identity_matrix,
1603 gfx::Point3F(),
1604 gfx::PointF(),
1605 gfx::Size(500, 500),
1606 true,
1607 false);
1608 SetLayerPropertiesForTesting(child.get(),
1609 identity_matrix,
1610 gfx::Point3F(),
1611 gfx::PointF(),
1612 gfx::Size(20, 20),
1613 true,
1614 false);
1615 SetLayerPropertiesForTesting(grand_child.get(),
1616 identity_matrix,
1617 gfx::Point3F(),
1618 gfx::PointF(45.f, 45.f),
1619 gfx::Size(10, 10),
1620 true,
1621 false);
1622 SetLayerPropertiesForTesting(great_grand_child.get(),
1623 identity_matrix,
1624 gfx::Point3F(),
1625 gfx::PointF(),
1626 gfx::Size(10, 10),
1627 true,
1628 false);
1629 SetLayerPropertiesForTesting(leaf_node1.get(),
1630 identity_matrix,
1631 gfx::Point3F(),
1632 gfx::PointF(),
1633 gfx::Size(500, 500),
1634 true,
1635 false);
1636 SetLayerPropertiesForTesting(leaf_node2.get(),
1637 identity_matrix,
1638 gfx::Point3F(),
1639 gfx::PointF(),
1640 gfx::Size(20, 20),
1641 true,
1642 false);
1644 child->SetMasksToBounds(true);
1645 child->SetOpacity(0.4f);
1646 child->SetForceRenderSurface(true);
1647 grand_child->SetOpacity(0.5f);
1648 great_grand_child->SetOpacity(0.4f);
1650 RenderSurfaceLayerList render_surface_layer_list;
1651 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1652 parent.get(), parent->bounds(), &render_surface_layer_list);
1653 inputs.can_adjust_raster_scales = true;
1654 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1656 ASSERT_EQ(2U, render_surface_layer_list.size());
1657 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1658 EXPECT_EQ(child->id(), render_surface_layer_list.at(1)->id());
1661 TEST_F(LayerTreeHostCommonTest, ClipRectCullsSurfaceWithoutVisibleContent) {
1662 // When a render surface has a clip rect, it is used to clip the content rect
1663 // of the surface. When the render surface is animating its transforms, then
1664 // the content rect's position in the clip rect is not defined on the main
1665 // thread, and its content rect should not be clipped.
1667 // The test tree is set up as follows:
1668 // - parent is a container layer that masksToBounds=true to cause clipping.
1669 // - child is a render surface, which has a clip rect set to the bounds of
1670 // the parent.
1671 // - grand_child is a render surface, and the only visible content in child.
1672 // It is positioned outside of the clip rect from parent.
1674 // In this configuration, grand_child should be outside the clipped
1675 // content rect of the child, making grand_child not appear in the
1676 // render_surface_layer_list. However, when we place an animation on the
1677 // child, this clipping should be avoided and we should keep the grand_child
1678 // in the render_surface_layer_list.
1680 const gfx::Transform identity_matrix;
1681 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1682 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1683 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
1684 scoped_refptr<LayerWithForcedDrawsContent> leaf_node =
1685 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1686 parent->AddChild(child);
1687 child->AddChild(grand_child);
1688 grand_child->AddChild(leaf_node);
1690 host()->SetRootLayer(parent);
1692 SetLayerPropertiesForTesting(parent.get(),
1693 identity_matrix,
1694 gfx::Point3F(),
1695 gfx::PointF(),
1696 gfx::Size(100, 100),
1697 true,
1698 false);
1699 SetLayerPropertiesForTesting(child.get(),
1700 identity_matrix,
1701 gfx::Point3F(),
1702 gfx::PointF(),
1703 gfx::Size(20, 20),
1704 true,
1705 false);
1706 SetLayerPropertiesForTesting(grand_child.get(),
1707 identity_matrix,
1708 gfx::Point3F(),
1709 gfx::PointF(200.f, 200.f),
1710 gfx::Size(10, 10),
1711 true,
1712 false);
1713 SetLayerPropertiesForTesting(leaf_node.get(),
1714 identity_matrix,
1715 gfx::Point3F(),
1716 gfx::PointF(),
1717 gfx::Size(10, 10),
1718 true,
1719 false);
1721 parent->SetMasksToBounds(true);
1722 child->SetOpacity(0.4f);
1723 child->SetForceRenderSurface(true);
1724 grand_child->SetOpacity(0.4f);
1725 grand_child->SetForceRenderSurface(true);
1728 RenderSurfaceLayerList render_surface_layer_list;
1729 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1730 parent.get(), parent->bounds(), &render_surface_layer_list);
1731 inputs.can_adjust_raster_scales = true;
1732 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1734 // Without an animation, we should cull child and grand_child from the
1735 // render_surface_layer_list.
1736 ASSERT_EQ(1U, render_surface_layer_list.size());
1737 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1740 // Now put an animating transform on child.
1741 AddAnimatedTransformToController(
1742 child->layer_animation_controller(), 10.0, 30, 0);
1745 RenderSurfaceLayerList render_surface_layer_list;
1746 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1747 parent.get(), parent->bounds(), &render_surface_layer_list);
1748 inputs.can_adjust_raster_scales = true;
1749 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1751 // With an animating transform, we should keep child and grand_child in the
1752 // render_surface_layer_list.
1753 ASSERT_EQ(3U, render_surface_layer_list.size());
1754 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1755 EXPECT_EQ(child->id(), render_surface_layer_list.at(1)->id());
1756 EXPECT_EQ(grand_child->id(), render_surface_layer_list.at(2)->id());
1760 TEST_F(LayerTreeHostCommonTest, IsClippedIsSetCorrectly) {
1761 // Layer's IsClipped() property is set to true when:
1762 // - the layer clips its subtree, e.g. masks to bounds,
1763 // - the layer is clipped by an ancestor that contributes to the same
1764 // render target,
1765 // - a surface is clipped by an ancestor that contributes to the same
1766 // render target.
1768 // In particular, for a layer that owns a render surface:
1769 // - the render surface inherits any clip from ancestors, and does NOT
1770 // pass that clipped status to the layer itself.
1771 // - but if the layer itself masks to bounds, it is considered clipped
1772 // and propagates the clip to the subtree.
1774 const gfx::Transform identity_matrix;
1775 scoped_refptr<Layer> root = Layer::Create(layer_settings());
1776 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1777 scoped_refptr<Layer> child1 = Layer::Create(layer_settings());
1778 scoped_refptr<Layer> child2 = Layer::Create(layer_settings());
1779 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
1780 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
1781 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1782 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
1783 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1784 root->AddChild(parent);
1785 parent->AddChild(child1);
1786 parent->AddChild(child2);
1787 child1->AddChild(grand_child);
1788 child2->AddChild(leaf_node2);
1789 grand_child->AddChild(leaf_node1);
1791 host()->SetRootLayer(root);
1793 child2->SetForceRenderSurface(true);
1795 SetLayerPropertiesForTesting(root.get(),
1796 identity_matrix,
1797 gfx::Point3F(),
1798 gfx::PointF(),
1799 gfx::Size(100, 100),
1800 true,
1801 false);
1802 SetLayerPropertiesForTesting(parent.get(),
1803 identity_matrix,
1804 gfx::Point3F(),
1805 gfx::PointF(),
1806 gfx::Size(100, 100),
1807 true,
1808 false);
1809 SetLayerPropertiesForTesting(child1.get(),
1810 identity_matrix,
1811 gfx::Point3F(),
1812 gfx::PointF(),
1813 gfx::Size(100, 100),
1814 true,
1815 false);
1816 SetLayerPropertiesForTesting(child2.get(),
1817 identity_matrix,
1818 gfx::Point3F(),
1819 gfx::PointF(),
1820 gfx::Size(100, 100),
1821 true,
1822 false);
1823 SetLayerPropertiesForTesting(grand_child.get(),
1824 identity_matrix,
1825 gfx::Point3F(),
1826 gfx::PointF(),
1827 gfx::Size(100, 100),
1828 true,
1829 false);
1830 SetLayerPropertiesForTesting(leaf_node1.get(),
1831 identity_matrix,
1832 gfx::Point3F(),
1833 gfx::PointF(),
1834 gfx::Size(100, 100),
1835 true,
1836 false);
1837 SetLayerPropertiesForTesting(leaf_node2.get(),
1838 identity_matrix,
1839 gfx::Point3F(),
1840 gfx::PointF(),
1841 gfx::Size(100, 100),
1842 true,
1843 false);
1845 // Case 1: nothing is clipped except the root render surface.
1847 RenderSurfaceLayerList render_surface_layer_list;
1848 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1849 root.get(), parent->bounds(), &render_surface_layer_list);
1850 inputs.can_adjust_raster_scales = true;
1851 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1853 ASSERT_TRUE(root->render_surface());
1854 ASSERT_TRUE(child2->render_surface());
1856 EXPECT_FALSE(root->is_clipped());
1857 EXPECT_TRUE(root->render_surface()->is_clipped());
1858 EXPECT_FALSE(parent->is_clipped());
1859 EXPECT_FALSE(child1->is_clipped());
1860 EXPECT_FALSE(child2->is_clipped());
1861 EXPECT_FALSE(child2->render_surface()->is_clipped());
1862 EXPECT_FALSE(grand_child->is_clipped());
1863 EXPECT_FALSE(leaf_node1->is_clipped());
1864 EXPECT_FALSE(leaf_node2->is_clipped());
1867 // Case 2: parent masksToBounds, so the parent, child1, and child2's
1868 // surface are clipped. But layers that contribute to child2's surface are
1869 // not clipped explicitly because child2's surface already accounts for
1870 // that clip.
1872 RenderSurfaceLayerList render_surface_layer_list;
1873 parent->SetMasksToBounds(true);
1874 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1875 root.get(), parent->bounds(), &render_surface_layer_list);
1876 inputs.can_adjust_raster_scales = true;
1877 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1879 ASSERT_TRUE(root->render_surface());
1880 ASSERT_TRUE(child2->render_surface());
1882 EXPECT_FALSE(root->is_clipped());
1883 EXPECT_TRUE(root->render_surface()->is_clipped());
1884 EXPECT_TRUE(parent->is_clipped());
1885 EXPECT_TRUE(child1->is_clipped());
1886 EXPECT_FALSE(child2->is_clipped());
1887 EXPECT_TRUE(child2->render_surface()->is_clipped());
1888 EXPECT_TRUE(grand_child->is_clipped());
1889 EXPECT_TRUE(leaf_node1->is_clipped());
1890 EXPECT_FALSE(leaf_node2->is_clipped());
1893 // Case 3: child2 masksToBounds. The layer and subtree are clipped, and
1894 // child2's render surface is not clipped.
1896 RenderSurfaceLayerList render_surface_layer_list;
1897 parent->SetMasksToBounds(false);
1898 child2->SetMasksToBounds(true);
1899 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1900 root.get(), parent->bounds(), &render_surface_layer_list);
1901 inputs.can_adjust_raster_scales = true;
1902 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1904 ASSERT_TRUE(root->render_surface());
1905 ASSERT_TRUE(child2->render_surface());
1907 EXPECT_FALSE(root->is_clipped());
1908 EXPECT_TRUE(root->render_surface()->is_clipped());
1909 EXPECT_FALSE(parent->is_clipped());
1910 EXPECT_FALSE(child1->is_clipped());
1911 EXPECT_TRUE(child2->is_clipped());
1912 EXPECT_FALSE(child2->render_surface()->is_clipped());
1913 EXPECT_FALSE(grand_child->is_clipped());
1914 EXPECT_FALSE(leaf_node1->is_clipped());
1915 EXPECT_TRUE(leaf_node2->is_clipped());
1919 TEST_F(LayerTreeHostCommonTest, DrawableContentRectForLayers) {
1920 // Verify that layers get the appropriate DrawableContentRect when their
1921 // parent masksToBounds is true.
1923 // grand_child1 - completely inside the region; DrawableContentRect should
1924 // be the layer rect expressed in target space.
1925 // grand_child2 - partially clipped but NOT masksToBounds; the clip rect
1926 // will be the intersection of layer bounds and the mask region.
1927 // grand_child3 - partially clipped and masksToBounds; the
1928 // DrawableContentRect will still be the intersection of layer bounds and
1929 // the mask region.
1930 // grand_child4 - outside parent's clip rect; the DrawableContentRect should
1931 // be empty.
1934 const gfx::Transform identity_matrix;
1935 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1936 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1937 scoped_refptr<Layer> grand_child1 = Layer::Create(layer_settings());
1938 scoped_refptr<Layer> grand_child2 = Layer::Create(layer_settings());
1939 scoped_refptr<Layer> grand_child3 = Layer::Create(layer_settings());
1940 scoped_refptr<Layer> grand_child4 = Layer::Create(layer_settings());
1942 parent->AddChild(child);
1943 child->AddChild(grand_child1);
1944 child->AddChild(grand_child2);
1945 child->AddChild(grand_child3);
1946 child->AddChild(grand_child4);
1948 host()->SetRootLayer(parent);
1950 SetLayerPropertiesForTesting(parent.get(),
1951 identity_matrix,
1952 gfx::Point3F(),
1953 gfx::PointF(),
1954 gfx::Size(500, 500),
1955 true,
1956 false);
1957 SetLayerPropertiesForTesting(child.get(),
1958 identity_matrix,
1959 gfx::Point3F(),
1960 gfx::PointF(),
1961 gfx::Size(20, 20),
1962 true,
1963 false);
1964 SetLayerPropertiesForTesting(grand_child1.get(),
1965 identity_matrix,
1966 gfx::Point3F(),
1967 gfx::PointF(5.f, 5.f),
1968 gfx::Size(10, 10),
1969 true,
1970 false);
1971 SetLayerPropertiesForTesting(grand_child2.get(),
1972 identity_matrix,
1973 gfx::Point3F(),
1974 gfx::PointF(15.f, 15.f),
1975 gfx::Size(10, 10),
1976 true,
1977 false);
1978 SetLayerPropertiesForTesting(grand_child3.get(),
1979 identity_matrix,
1980 gfx::Point3F(),
1981 gfx::PointF(15.f, 15.f),
1982 gfx::Size(10, 10),
1983 true,
1984 false);
1985 SetLayerPropertiesForTesting(grand_child4.get(),
1986 identity_matrix,
1987 gfx::Point3F(),
1988 gfx::PointF(45.f, 45.f),
1989 gfx::Size(10, 10),
1990 true,
1991 false);
1993 child->SetMasksToBounds(true);
1994 grand_child3->SetMasksToBounds(true);
1996 // Force everyone to be a render surface.
1997 child->SetOpacity(0.4f);
1998 grand_child1->SetOpacity(0.5f);
1999 grand_child2->SetOpacity(0.5f);
2000 grand_child3->SetOpacity(0.5f);
2001 grand_child4->SetOpacity(0.5f);
2003 RenderSurfaceLayerList render_surface_layer_list;
2004 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2005 parent.get(), parent->bounds(), &render_surface_layer_list);
2006 inputs.can_adjust_raster_scales = true;
2007 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2009 EXPECT_EQ(gfx::Rect(5, 5, 10, 10), grand_child1->drawable_content_rect());
2010 EXPECT_EQ(gfx::Rect(15, 15, 5, 5), grand_child3->drawable_content_rect());
2011 EXPECT_EQ(gfx::Rect(15, 15, 5, 5), grand_child3->drawable_content_rect());
2012 EXPECT_TRUE(grand_child4->drawable_content_rect().IsEmpty());
2015 TEST_F(LayerTreeHostCommonTest, ClipRectIsPropagatedCorrectlyToSurfaces) {
2016 // Verify that render surfaces (and their layers) get the appropriate
2017 // clip rects when their parent masksToBounds is true.
2019 // Layers that own render surfaces (at least for now) do not inherit any
2020 // clipping; instead the surface will enforce the clip for the entire subtree.
2021 // They may still have a clip rect of their own layer bounds, however, if
2022 // masksToBounds was true.
2023 const gfx::Transform identity_matrix;
2024 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
2025 scoped_refptr<Layer> child = Layer::Create(layer_settings());
2026 scoped_refptr<Layer> grand_child1 = Layer::Create(layer_settings());
2027 scoped_refptr<Layer> grand_child2 = Layer::Create(layer_settings());
2028 scoped_refptr<Layer> grand_child3 = Layer::Create(layer_settings());
2029 scoped_refptr<Layer> grand_child4 = Layer::Create(layer_settings());
2030 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
2031 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2032 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
2033 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2034 scoped_refptr<LayerWithForcedDrawsContent> leaf_node3 =
2035 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2036 scoped_refptr<LayerWithForcedDrawsContent> leaf_node4 =
2037 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2039 parent->AddChild(child);
2040 child->AddChild(grand_child1);
2041 child->AddChild(grand_child2);
2042 child->AddChild(grand_child3);
2043 child->AddChild(grand_child4);
2045 host()->SetRootLayer(parent);
2047 // the leaf nodes ensure that these grand_children become render surfaces for
2048 // this test.
2049 grand_child1->AddChild(leaf_node1);
2050 grand_child2->AddChild(leaf_node2);
2051 grand_child3->AddChild(leaf_node3);
2052 grand_child4->AddChild(leaf_node4);
2054 SetLayerPropertiesForTesting(parent.get(),
2055 identity_matrix,
2056 gfx::Point3F(),
2057 gfx::PointF(),
2058 gfx::Size(500, 500),
2059 true,
2060 false);
2061 SetLayerPropertiesForTesting(child.get(),
2062 identity_matrix,
2063 gfx::Point3F(),
2064 gfx::PointF(),
2065 gfx::Size(20, 20),
2066 true,
2067 false);
2068 SetLayerPropertiesForTesting(grand_child1.get(),
2069 identity_matrix,
2070 gfx::Point3F(),
2071 gfx::PointF(5.f, 5.f),
2072 gfx::Size(10, 10),
2073 true,
2074 false);
2075 SetLayerPropertiesForTesting(grand_child2.get(),
2076 identity_matrix,
2077 gfx::Point3F(),
2078 gfx::PointF(15.f, 15.f),
2079 gfx::Size(10, 10),
2080 true,
2081 false);
2082 SetLayerPropertiesForTesting(grand_child3.get(),
2083 identity_matrix,
2084 gfx::Point3F(),
2085 gfx::PointF(15.f, 15.f),
2086 gfx::Size(10, 10),
2087 true,
2088 false);
2089 SetLayerPropertiesForTesting(grand_child4.get(),
2090 identity_matrix,
2091 gfx::Point3F(),
2092 gfx::PointF(45.f, 45.f),
2093 gfx::Size(10, 10),
2094 true,
2095 false);
2096 SetLayerPropertiesForTesting(leaf_node1.get(),
2097 identity_matrix,
2098 gfx::Point3F(),
2099 gfx::PointF(),
2100 gfx::Size(10, 10),
2101 true,
2102 false);
2103 SetLayerPropertiesForTesting(leaf_node2.get(),
2104 identity_matrix,
2105 gfx::Point3F(),
2106 gfx::PointF(),
2107 gfx::Size(10, 10),
2108 true,
2109 false);
2110 SetLayerPropertiesForTesting(leaf_node3.get(),
2111 identity_matrix,
2112 gfx::Point3F(),
2113 gfx::PointF(),
2114 gfx::Size(10, 10),
2115 true,
2116 false);
2117 SetLayerPropertiesForTesting(leaf_node4.get(),
2118 identity_matrix,
2119 gfx::Point3F(),
2120 gfx::PointF(),
2121 gfx::Size(10, 10),
2122 true,
2123 false);
2125 child->SetMasksToBounds(true);
2126 grand_child3->SetMasksToBounds(true);
2127 grand_child4->SetMasksToBounds(true);
2129 // Force everyone to be a render surface.
2130 child->SetOpacity(0.4f);
2131 child->SetForceRenderSurface(true);
2132 grand_child1->SetOpacity(0.5f);
2133 grand_child1->SetForceRenderSurface(true);
2134 grand_child2->SetOpacity(0.5f);
2135 grand_child2->SetForceRenderSurface(true);
2136 grand_child3->SetOpacity(0.5f);
2137 grand_child3->SetForceRenderSurface(true);
2138 grand_child4->SetOpacity(0.5f);
2139 grand_child4->SetForceRenderSurface(true);
2141 RenderSurfaceLayerList render_surface_layer_list;
2142 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2143 parent.get(), parent->bounds(), &render_surface_layer_list);
2144 inputs.can_adjust_raster_scales = true;
2145 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2146 ASSERT_TRUE(grand_child1->render_surface());
2147 ASSERT_TRUE(grand_child2->render_surface());
2148 ASSERT_TRUE(grand_child3->render_surface());
2150 // Surfaces are clipped by their parent, but un-affected by the owning layer's
2151 // masksToBounds.
2152 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2153 grand_child1->render_surface()->clip_rect());
2154 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2155 grand_child2->render_surface()->clip_rect());
2156 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2157 grand_child3->render_surface()->clip_rect());
2160 TEST_F(LayerTreeHostCommonTest, AnimationsForRenderSurfaceHierarchy) {
2161 LayerImpl* parent = root_layer();
2162 LayerImpl* render_surface1 = AddChildToRoot<LayerImpl>();
2163 LayerImpl* child_of_rs1 = AddChild<LayerImpl>(render_surface1);
2164 LayerImpl* grand_child_of_rs1 = AddChild<LayerImpl>(child_of_rs1);
2165 LayerImpl* render_surface2 = AddChild<LayerImpl>(render_surface1);
2166 LayerImpl* child_of_rs2 = AddChild<LayerImpl>(render_surface2);
2167 LayerImpl* grand_child_of_rs2 = AddChild<LayerImpl>(child_of_rs2);
2168 LayerImpl* child_of_root = AddChildToRoot<LayerImpl>();
2169 LayerImpl* grand_child_of_root = AddChild<LayerImpl>(child_of_root);
2171 grand_child_of_rs1->SetDrawsContent(true);
2172 grand_child_of_rs2->SetDrawsContent(true);
2174 gfx::Transform layer_transform;
2175 layer_transform.Translate(1.0, 1.0);
2177 SetLayerPropertiesForTesting(
2178 parent, layer_transform, gfx::Point3F(0.25f, 0.f, 0.f),
2179 gfx::PointF(2.5f, 0.f), gfx::Size(10, 10), true, false, true);
2180 SetLayerPropertiesForTesting(
2181 render_surface1, layer_transform, gfx::Point3F(0.25f, 0.f, 0.f),
2182 gfx::PointF(2.5f, 0.f), gfx::Size(10, 10), true, false, true);
2183 SetLayerPropertiesForTesting(
2184 render_surface2, layer_transform, gfx::Point3F(0.25f, 0.f, 0.f),
2185 gfx::PointF(2.5f, 0.f), gfx::Size(10, 10), true, false, true);
2186 SetLayerPropertiesForTesting(
2187 child_of_root, layer_transform, gfx::Point3F(0.25f, 0.f, 0.f),
2188 gfx::PointF(2.5f, 0.f), gfx::Size(10, 10), true, false, false);
2189 SetLayerPropertiesForTesting(
2190 child_of_rs1, layer_transform, gfx::Point3F(0.25f, 0.f, 0.f),
2191 gfx::PointF(2.5f, 0.f), gfx::Size(10, 10), true, false, false);
2192 SetLayerPropertiesForTesting(
2193 child_of_rs2, layer_transform, gfx::Point3F(0.25f, 0.f, 0.f),
2194 gfx::PointF(2.5f, 0.f), gfx::Size(10, 10), true, false, false);
2195 SetLayerPropertiesForTesting(
2196 grand_child_of_root, layer_transform, gfx::Point3F(0.25f, 0.f, 0.f),
2197 gfx::PointF(2.5f, 0.f), gfx::Size(10, 10), true, false, false);
2198 SetLayerPropertiesForTesting(
2199 grand_child_of_rs1, layer_transform, gfx::Point3F(0.25f, 0.f, 0.f),
2200 gfx::PointF(2.5f, 0.f), gfx::Size(10, 10), true, false, false);
2201 SetLayerPropertiesForTesting(
2202 grand_child_of_rs2, layer_transform, gfx::Point3F(0.25f, 0.f, 0.f),
2203 gfx::PointF(2.5f, 0.f), gfx::Size(10, 10), true, false, false);
2205 // Put an animated opacity on the render surface.
2206 AddOpacityTransitionToController(
2207 render_surface1->layer_animation_controller(), 10.0, 1.f, 0.f, false);
2209 // Also put an animated opacity on a layer without descendants.
2210 AddOpacityTransitionToController(
2211 grand_child_of_root->layer_animation_controller(), 10.0, 1.f, 0.f, false);
2213 // Put a transform animation on the render surface.
2214 AddAnimatedTransformToController(
2215 render_surface2->layer_animation_controller(), 10.0, 30, 0);
2217 // Also put transform animations on grand_child_of_root, and
2218 // grand_child_of_rs2
2219 AddAnimatedTransformToController(
2220 grand_child_of_root->layer_animation_controller(), 10.0, 30, 0);
2221 AddAnimatedTransformToController(
2222 grand_child_of_rs2->layer_animation_controller(), 10.0, 30, 0);
2224 ExecuteCalculateDrawProperties(parent);
2226 // Only layers that are associated with render surfaces should have an actual
2227 // RenderSurface() value.
2228 ASSERT_TRUE(parent->render_surface());
2229 ASSERT_FALSE(child_of_root->render_surface());
2230 ASSERT_FALSE(grand_child_of_root->render_surface());
2232 ASSERT_TRUE(render_surface1->render_surface());
2233 ASSERT_FALSE(child_of_rs1->render_surface());
2234 ASSERT_FALSE(grand_child_of_rs1->render_surface());
2236 ASSERT_TRUE(render_surface2->render_surface());
2237 ASSERT_FALSE(child_of_rs2->render_surface());
2238 ASSERT_FALSE(grand_child_of_rs2->render_surface());
2240 // Verify all render target accessors
2241 EXPECT_EQ(parent, parent->render_target());
2242 EXPECT_EQ(parent, child_of_root->render_target());
2243 EXPECT_EQ(parent, grand_child_of_root->render_target());
2245 EXPECT_EQ(render_surface1, render_surface1->render_target());
2246 EXPECT_EQ(render_surface1, child_of_rs1->render_target());
2247 EXPECT_EQ(render_surface1, grand_child_of_rs1->render_target());
2249 EXPECT_EQ(render_surface2, render_surface2->render_target());
2250 EXPECT_EQ(render_surface2, child_of_rs2->render_target());
2251 EXPECT_EQ(render_surface2, grand_child_of_rs2->render_target());
2253 // Verify draw_transform_is_animating values
2254 EXPECT_FALSE(parent->draw_transform_is_animating());
2255 EXPECT_FALSE(child_of_root->draw_transform_is_animating());
2256 EXPECT_TRUE(grand_child_of_root->draw_transform_is_animating());
2257 EXPECT_FALSE(render_surface1->draw_transform_is_animating());
2258 EXPECT_FALSE(child_of_rs1->draw_transform_is_animating());
2259 EXPECT_FALSE(grand_child_of_rs1->draw_transform_is_animating());
2260 EXPECT_FALSE(render_surface2->draw_transform_is_animating());
2261 EXPECT_FALSE(child_of_rs2->draw_transform_is_animating());
2262 EXPECT_TRUE(grand_child_of_rs2->draw_transform_is_animating());
2264 // Verify screen_space_transform_is_animating values
2265 EXPECT_FALSE(parent->screen_space_transform_is_animating());
2266 EXPECT_FALSE(child_of_root->screen_space_transform_is_animating());
2267 EXPECT_TRUE(grand_child_of_root->screen_space_transform_is_animating());
2268 EXPECT_FALSE(render_surface1->screen_space_transform_is_animating());
2269 EXPECT_FALSE(child_of_rs1->screen_space_transform_is_animating());
2270 EXPECT_FALSE(grand_child_of_rs1->screen_space_transform_is_animating());
2271 EXPECT_TRUE(render_surface2->screen_space_transform_is_animating());
2272 EXPECT_TRUE(child_of_rs2->screen_space_transform_is_animating());
2273 EXPECT_TRUE(grand_child_of_rs2->screen_space_transform_is_animating());
2275 // Sanity check. If these fail there is probably a bug in the test itself.
2276 // It is expected that we correctly set up transforms so that the y-component
2277 // of the screen-space transform encodes the "depth" of the layer in the tree.
2278 EXPECT_FLOAT_EQ(1.0, parent->screen_space_transform().matrix().get(1, 3));
2279 EXPECT_FLOAT_EQ(2.0,
2280 child_of_root->screen_space_transform().matrix().get(1, 3));
2281 EXPECT_FLOAT_EQ(
2282 3.0, grand_child_of_root->screen_space_transform().matrix().get(1, 3));
2284 EXPECT_FLOAT_EQ(2.0,
2285 render_surface1->screen_space_transform().matrix().get(1, 3));
2286 EXPECT_FLOAT_EQ(3.0,
2287 child_of_rs1->screen_space_transform().matrix().get(1, 3));
2288 EXPECT_FLOAT_EQ(
2289 4.0, grand_child_of_rs1->screen_space_transform().matrix().get(1, 3));
2291 EXPECT_FLOAT_EQ(3.0,
2292 render_surface2->screen_space_transform().matrix().get(1, 3));
2293 EXPECT_FLOAT_EQ(4.0,
2294 child_of_rs2->screen_space_transform().matrix().get(1, 3));
2295 EXPECT_FLOAT_EQ(
2296 5.0, grand_child_of_rs2->screen_space_transform().matrix().get(1, 3));
2299 TEST_F(LayerTreeHostCommonTest,
2300 ScreenSpaceTransformIsAnimatingWithDelayedAnimation) {
2301 LayerImpl* parent = root_layer();
2302 LayerImpl* child = AddChild<LayerImpl>(parent);
2303 LayerImpl* grand_child = AddChild<LayerImpl>(child);
2304 LayerImpl* great_grand_child = AddChild<LayerImpl>(grand_child);
2306 parent->SetDrawsContent(true);
2307 child->SetDrawsContent(true);
2308 grand_child->SetDrawsContent(true);
2309 great_grand_child->SetDrawsContent(true);
2311 gfx::Transform identity;
2313 SetLayerPropertiesForTesting(parent, identity, gfx::Point3F(), gfx::PointF(),
2314 gfx::Size(10, 10), true, false, true);
2315 SetLayerPropertiesForTesting(child, identity, gfx::Point3F(), gfx::PointF(),
2316 gfx::Size(10, 10), true, false, false);
2317 SetLayerPropertiesForTesting(grand_child, identity, gfx::Point3F(),
2318 gfx::PointF(), gfx::Size(10, 10), true, false,
2319 false);
2320 SetLayerPropertiesForTesting(great_grand_child, identity, gfx::Point3F(),
2321 gfx::PointF(), gfx::Size(10, 10), true, false,
2322 false);
2324 // Add a transform animation with a start delay to |grand_child|.
2325 scoped_ptr<Animation> animation = Animation::Create(
2326 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(), 0, 1,
2327 Animation::TRANSFORM);
2328 animation->set_fill_mode(Animation::FILL_MODE_NONE);
2329 animation->set_time_offset(base::TimeDelta::FromMilliseconds(-1000));
2330 grand_child->layer_animation_controller()->AddAnimation(animation.Pass());
2332 ExecuteCalculateDrawProperties(parent);
2334 EXPECT_FALSE(parent->screen_space_transform_is_animating());
2335 EXPECT_FALSE(child->screen_space_transform_is_animating());
2337 EXPECT_FALSE(grand_child->TransformIsAnimating());
2338 EXPECT_TRUE(grand_child->HasPotentiallyRunningTransformAnimation());
2339 EXPECT_TRUE(grand_child->screen_space_transform_is_animating());
2340 EXPECT_TRUE(great_grand_child->screen_space_transform_is_animating());
2343 TEST_F(LayerTreeHostCommonTest, VisibleRectForIdentityTransform) {
2344 // Test the calculateVisibleRect() function works correctly for identity
2345 // transforms.
2347 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2348 gfx::Transform layer_to_surface_transform;
2350 // Case 1: Layer is contained within the surface.
2351 gfx::Rect layer_content_rect = gfx::Rect(10, 10, 30, 30);
2352 gfx::Rect expected = gfx::Rect(10, 10, 30, 30);
2353 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2354 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2355 EXPECT_EQ(expected, actual);
2357 // Case 2: Layer is outside the surface rect.
2358 layer_content_rect = gfx::Rect(120, 120, 30, 30);
2359 actual = LayerTreeHostCommon::CalculateVisibleRect(
2360 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2361 EXPECT_TRUE(actual.IsEmpty());
2363 // Case 3: Layer is partially overlapping the surface rect.
2364 layer_content_rect = gfx::Rect(80, 80, 30, 30);
2365 expected = gfx::Rect(80, 80, 20, 20);
2366 actual = LayerTreeHostCommon::CalculateVisibleRect(
2367 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2368 EXPECT_EQ(expected, actual);
2371 TEST_F(LayerTreeHostCommonTest, VisibleRectForTranslations) {
2372 // Test the calculateVisibleRect() function works correctly for scaling
2373 // transforms.
2375 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2376 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
2377 gfx::Transform layer_to_surface_transform;
2379 // Case 1: Layer is contained within the surface.
2380 layer_to_surface_transform.MakeIdentity();
2381 layer_to_surface_transform.Translate(10.0, 10.0);
2382 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
2383 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2384 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2385 EXPECT_EQ(expected, actual);
2387 // Case 2: Layer is outside the surface rect.
2388 layer_to_surface_transform.MakeIdentity();
2389 layer_to_surface_transform.Translate(120.0, 120.0);
2390 actual = LayerTreeHostCommon::CalculateVisibleRect(
2391 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2392 EXPECT_TRUE(actual.IsEmpty());
2394 // Case 3: Layer is partially overlapping the surface rect.
2395 layer_to_surface_transform.MakeIdentity();
2396 layer_to_surface_transform.Translate(80.0, 80.0);
2397 expected = gfx::Rect(0, 0, 20, 20);
2398 actual = LayerTreeHostCommon::CalculateVisibleRect(
2399 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2400 EXPECT_EQ(expected, actual);
2403 TEST_F(LayerTreeHostCommonTest, VisibleRectFor2DRotations) {
2404 // Test the calculateVisibleRect() function works correctly for rotations
2405 // about z-axis (i.e. 2D rotations). Remember that calculateVisibleRect()
2406 // should return the g in the layer's space.
2408 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2409 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
2410 gfx::Transform layer_to_surface_transform;
2412 // Case 1: Layer is contained within the surface.
2413 layer_to_surface_transform.MakeIdentity();
2414 layer_to_surface_transform.Translate(50.0, 50.0);
2415 layer_to_surface_transform.Rotate(45.0);
2416 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
2417 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2418 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2419 EXPECT_EQ(expected, actual);
2421 // Case 2: Layer is outside the surface rect.
2422 layer_to_surface_transform.MakeIdentity();
2423 layer_to_surface_transform.Translate(-50.0, 0.0);
2424 layer_to_surface_transform.Rotate(45.0);
2425 actual = LayerTreeHostCommon::CalculateVisibleRect(
2426 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2427 EXPECT_TRUE(actual.IsEmpty());
2429 // Case 3: The layer is rotated about its top-left corner. In surface space,
2430 // the layer is oriented diagonally, with the left half outside of the render
2431 // surface. In this case, the g should still be the entire layer
2432 // (remember the g is computed in layer space); both the top-left
2433 // and bottom-right corners of the layer are still visible.
2434 layer_to_surface_transform.MakeIdentity();
2435 layer_to_surface_transform.Rotate(45.0);
2436 expected = gfx::Rect(0, 0, 30, 30);
2437 actual = LayerTreeHostCommon::CalculateVisibleRect(
2438 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2439 EXPECT_EQ(expected, actual);
2441 // Case 4: The layer is rotated about its top-left corner, and translated
2442 // upwards. In surface space, the layer is oriented diagonally, with only the
2443 // top corner of the surface overlapping the layer. In layer space, the render
2444 // surface overlaps the right side of the layer. The g should be
2445 // the layer's right half.
2446 layer_to_surface_transform.MakeIdentity();
2447 layer_to_surface_transform.Translate(0.0, -sqrt(2.0) * 15.0);
2448 layer_to_surface_transform.Rotate(45.0);
2449 expected = gfx::Rect(15, 0, 15, 30); // Right half of layer bounds.
2450 actual = LayerTreeHostCommon::CalculateVisibleRect(
2451 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2452 EXPECT_EQ(expected, actual);
2455 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dOrthographicTransform) {
2456 // Test that the calculateVisibleRect() function works correctly for 3d
2457 // transforms.
2459 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2460 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
2461 gfx::Transform layer_to_surface_transform;
2463 // Case 1: Orthographic projection of a layer rotated about y-axis by 45
2464 // degrees, should be fully contained in the render surface.
2465 layer_to_surface_transform.MakeIdentity();
2466 layer_to_surface_transform.RotateAboutYAxis(45.0);
2467 gfx::Rect expected = gfx::Rect(0, 0, 100, 100);
2468 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2469 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2470 EXPECT_EQ(expected, actual);
2472 // Case 2: Orthographic projection of a layer rotated about y-axis by 45
2473 // degrees, but shifted to the side so only the right-half the layer would be
2474 // visible on the surface.
2475 // 100 is the un-rotated layer width; divided by sqrt(2) is the rotated width.
2476 SkMScalar half_width_of_rotated_layer =
2477 SkDoubleToMScalar((100.0 / sqrt(2.0)) * 0.5);
2478 layer_to_surface_transform.MakeIdentity();
2479 layer_to_surface_transform.Translate(-half_width_of_rotated_layer, 0.0);
2480 layer_to_surface_transform.RotateAboutYAxis(45.0); // Rotates about the left
2481 // edge of the layer.
2482 expected = gfx::Rect(50, 0, 50, 100); // Tight half of the layer.
2483 actual = LayerTreeHostCommon::CalculateVisibleRect(
2484 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2485 EXPECT_EQ(expected, actual);
2488 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveTransform) {
2489 // Test the calculateVisibleRect() function works correctly when the layer has
2490 // a perspective projection onto the target surface.
2492 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2493 gfx::Rect layer_content_rect = gfx::Rect(-50, -50, 200, 200);
2494 gfx::Transform layer_to_surface_transform;
2496 // Case 1: Even though the layer is twice as large as the surface, due to
2497 // perspective foreshortening, the layer will fit fully in the surface when
2498 // its translated more than the perspective amount.
2499 layer_to_surface_transform.MakeIdentity();
2501 // The following sequence of transforms applies the perspective about the
2502 // center of the surface.
2503 layer_to_surface_transform.Translate(50.0, 50.0);
2504 layer_to_surface_transform.ApplyPerspectiveDepth(9.0);
2505 layer_to_surface_transform.Translate(-50.0, -50.0);
2507 // This translate places the layer in front of the surface's projection plane.
2508 layer_to_surface_transform.Translate3d(0.0, 0.0, -27.0);
2510 gfx::Rect expected = gfx::Rect(-50, -50, 200, 200);
2511 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2512 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2513 EXPECT_EQ(expected, actual);
2515 // Case 2: same projection as before, except that the layer is also translated
2516 // to the side, so that only the right half of the layer should be visible.
2518 // Explanation of expected result: The perspective ratio is (z distance
2519 // between layer and camera origin) / (z distance between projection plane and
2520 // camera origin) == ((-27 - 9) / 9) Then, by similar triangles, if we want to
2521 // move a layer by translating -50 units in projected surface units (so that
2522 // only half of it is visible), then we would need to translate by (-36 / 9) *
2523 // -50 == -200 in the layer's units.
2524 layer_to_surface_transform.Translate3d(-200.0, 0.0, 0.0);
2525 expected = gfx::Rect(gfx::Point(50, -50),
2526 gfx::Size(100, 200)); // The right half of the layer's
2527 // bounding rect.
2528 actual = LayerTreeHostCommon::CalculateVisibleRect(
2529 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2530 EXPECT_EQ(expected, actual);
2533 TEST_F(LayerTreeHostCommonTest,
2534 VisibleRectFor3dOrthographicIsNotClippedBehindSurface) {
2535 // There is currently no explicit concept of an orthographic projection plane
2536 // in our code (nor in the CSS spec to my knowledge). Therefore, layers that
2537 // are technically behind the surface in an orthographic world should not be
2538 // clipped when they are flattened to the surface.
2540 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2541 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
2542 gfx::Transform layer_to_surface_transform;
2544 // This sequence of transforms effectively rotates the layer about the y-axis
2545 // at the center of the layer.
2546 layer_to_surface_transform.MakeIdentity();
2547 layer_to_surface_transform.Translate(50.0, 0.0);
2548 layer_to_surface_transform.RotateAboutYAxis(45.0);
2549 layer_to_surface_transform.Translate(-50.0, 0.0);
2551 gfx::Rect expected = gfx::Rect(0, 0, 100, 100);
2552 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2553 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2554 EXPECT_EQ(expected, actual);
2557 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveWhenClippedByW) {
2558 // Test the calculateVisibleRect() function works correctly when projecting a
2559 // surface onto a layer, but the layer is partially behind the camera (not
2560 // just behind the projection plane). In this case, the cartesian coordinates
2561 // may seem to be valid, but actually they are not. The visible rect needs to
2562 // be properly clipped by the w = 0 plane in homogeneous coordinates before
2563 // converting to cartesian coordinates.
2565 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100);
2566 gfx::Rect layer_content_rect = gfx::Rect(-10, -1, 20, 2);
2567 gfx::Transform layer_to_surface_transform;
2569 // The layer is positioned so that the right half of the layer should be in
2570 // front of the camera, while the other half is behind the surface's
2571 // projection plane. The following sequence of transforms applies the
2572 // perspective and rotation about the center of the layer.
2573 layer_to_surface_transform.MakeIdentity();
2574 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2575 layer_to_surface_transform.Translate3d(-2.0, 0.0, 1.0);
2576 layer_to_surface_transform.RotateAboutYAxis(45.0);
2578 // Sanity check that this transform does indeed cause w < 0 when applying the
2579 // transform, otherwise this code is not testing the intended scenario.
2580 bool clipped;
2581 MathUtil::MapQuad(layer_to_surface_transform,
2582 gfx::QuadF(gfx::RectF(layer_content_rect)),
2583 &clipped);
2584 ASSERT_TRUE(clipped);
2586 int expected_x_position = 0;
2587 int expected_width = 10;
2588 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2589 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2590 EXPECT_EQ(expected_x_position, actual.x());
2591 EXPECT_EQ(expected_width, actual.width());
2594 TEST_F(LayerTreeHostCommonTest, VisibleRectForPerspectiveUnprojection) {
2595 // To determine visible rect in layer space, there needs to be an
2596 // un-projection from surface space to layer space. When the original
2597 // transform was a perspective projection that was clipped, it returns a rect
2598 // that encloses the clipped bounds. Un-projecting this new rect may require
2599 // clipping again.
2601 // This sequence of transforms causes one corner of the layer to protrude
2602 // across the w = 0 plane, and should be clipped.
2603 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100);
2604 gfx::Rect layer_content_rect = gfx::Rect(-10, -10, 20, 20);
2605 gfx::Transform layer_to_surface_transform;
2606 layer_to_surface_transform.MakeIdentity();
2607 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2608 layer_to_surface_transform.Translate3d(0.0, 0.0, -5.0);
2609 layer_to_surface_transform.RotateAboutYAxis(45.0);
2610 layer_to_surface_transform.RotateAboutXAxis(80.0);
2612 // Sanity check that un-projection does indeed cause w < 0, otherwise this
2613 // code is not testing the intended scenario.
2614 bool clipped;
2615 gfx::RectF clipped_rect =
2616 MathUtil::MapClippedRect(layer_to_surface_transform, layer_content_rect);
2617 MathUtil::ProjectQuad(
2618 Inverse(layer_to_surface_transform), gfx::QuadF(clipped_rect), &clipped);
2619 ASSERT_TRUE(clipped);
2621 // Only the corner of the layer is not visible on the surface because of being
2622 // clipped. But, the net result of rounding visible region to an axis-aligned
2623 // rect is that the entire layer should still be considered visible.
2624 gfx::Rect expected = gfx::Rect(-10, -10, 20, 20);
2625 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2626 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2627 EXPECT_EQ(expected, actual);
2630 TEST_F(LayerTreeHostCommonTest,
2631 VisibleRectsForPositionedRootLayerClippedByViewport) {
2632 scoped_refptr<Layer> root =
2633 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2634 host()->SetRootLayer(root);
2636 gfx::Transform identity_matrix;
2637 // Root layer is positioned at (60, 70). The default device viewport size
2638 // is (0, 0, 100x100) in target space. So the root layer's visible rect
2639 // will be clipped by the viewport to be (0, 0, 40x30) in layer's space.
2640 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
2641 gfx::PointF(60, 70), gfx::Size(100, 100), true,
2642 false);
2643 ExecuteCalculateDrawProperties(root.get());
2645 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2646 root->render_surface()->DrawableContentRect());
2647 // In target space, not clipped.
2648 EXPECT_EQ(gfx::Rect(60, 70, 100, 100), root->drawable_content_rect());
2649 // In layer space, clipped.
2650 EXPECT_EQ(gfx::Rect(0, 0, 40, 30), root->visible_layer_rect());
2653 TEST_F(LayerTreeHostCommonTest, DrawableAndVisibleContentRectsForSimpleLayers) {
2654 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2655 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2656 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2657 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2658 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2659 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2660 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2661 root->AddChild(child1);
2662 root->AddChild(child2);
2663 root->AddChild(child3);
2665 host()->SetRootLayer(root);
2667 gfx::Transform identity_matrix;
2668 SetLayerPropertiesForTesting(root.get(),
2669 identity_matrix,
2670 gfx::Point3F(),
2671 gfx::PointF(),
2672 gfx::Size(100, 100),
2673 true,
2674 false);
2675 SetLayerPropertiesForTesting(child1.get(),
2676 identity_matrix,
2677 gfx::Point3F(),
2678 gfx::PointF(),
2679 gfx::Size(50, 50),
2680 true,
2681 false);
2682 SetLayerPropertiesForTesting(child2.get(),
2683 identity_matrix,
2684 gfx::Point3F(),
2685 gfx::PointF(75.f, 75.f),
2686 gfx::Size(50, 50),
2687 true,
2688 false);
2689 SetLayerPropertiesForTesting(child3.get(),
2690 identity_matrix,
2691 gfx::Point3F(),
2692 gfx::PointF(125.f, 125.f),
2693 gfx::Size(50, 50),
2694 true,
2695 false);
2697 ExecuteCalculateDrawProperties(root.get());
2699 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2700 root->render_surface()->DrawableContentRect());
2701 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2703 // Layers that do not draw content should have empty visible_layer_rects.
2704 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_layer_rect());
2706 // layer visible_layer_rects are clipped by their target surface.
2707 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_layer_rect());
2708 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_layer_rect());
2709 EXPECT_TRUE(child3->visible_layer_rect().IsEmpty());
2711 // layer drawable_content_rects are not clipped.
2712 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->drawable_content_rect());
2713 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
2714 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
2717 TEST_F(LayerTreeHostCommonTest,
2718 DrawableAndVisibleContentRectsForLayersClippedByLayer) {
2719 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2720 scoped_refptr<Layer> child = Layer::Create(layer_settings());
2721 scoped_refptr<LayerWithForcedDrawsContent> grand_child1 =
2722 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2723 scoped_refptr<LayerWithForcedDrawsContent> grand_child2 =
2724 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2725 scoped_refptr<LayerWithForcedDrawsContent> grand_child3 =
2726 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2727 root->AddChild(child);
2728 child->AddChild(grand_child1);
2729 child->AddChild(grand_child2);
2730 child->AddChild(grand_child3);
2732 host()->SetRootLayer(root);
2734 gfx::Transform identity_matrix;
2735 SetLayerPropertiesForTesting(root.get(),
2736 identity_matrix,
2737 gfx::Point3F(),
2738 gfx::PointF(),
2739 gfx::Size(100, 100),
2740 true,
2741 false);
2742 SetLayerPropertiesForTesting(child.get(),
2743 identity_matrix,
2744 gfx::Point3F(),
2745 gfx::PointF(),
2746 gfx::Size(100, 100),
2747 true,
2748 false);
2749 SetLayerPropertiesForTesting(grand_child1.get(),
2750 identity_matrix,
2751 gfx::Point3F(),
2752 gfx::PointF(5.f, 5.f),
2753 gfx::Size(50, 50),
2754 true,
2755 false);
2756 SetLayerPropertiesForTesting(grand_child2.get(),
2757 identity_matrix,
2758 gfx::Point3F(),
2759 gfx::PointF(75.f, 75.f),
2760 gfx::Size(50, 50),
2761 true,
2762 false);
2763 SetLayerPropertiesForTesting(grand_child3.get(),
2764 identity_matrix,
2765 gfx::Point3F(),
2766 gfx::PointF(125.f, 125.f),
2767 gfx::Size(50, 50),
2768 true,
2769 false);
2771 child->SetMasksToBounds(true);
2772 ExecuteCalculateDrawProperties(root.get());
2774 ASSERT_FALSE(child->render_surface());
2776 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2777 root->render_surface()->DrawableContentRect());
2778 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2780 // Layers that do not draw content should have empty visible content rects.
2781 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_layer_rect());
2782 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), child->visible_layer_rect());
2784 // All grandchild visible content rects should be clipped by child.
2785 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), grand_child1->visible_layer_rect());
2786 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), grand_child2->visible_layer_rect());
2787 EXPECT_TRUE(grand_child3->visible_layer_rect().IsEmpty());
2789 // All grandchild DrawableContentRects should also be clipped by child.
2790 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), grand_child1->drawable_content_rect());
2791 EXPECT_EQ(gfx::Rect(75, 75, 25, 25), grand_child2->drawable_content_rect());
2792 EXPECT_TRUE(grand_child3->drawable_content_rect().IsEmpty());
2795 TEST_F(LayerTreeHostCommonTest, VisibleContentRectWithClippingAndScaling) {
2796 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2797 scoped_refptr<Layer> child = Layer::Create(layer_settings());
2798 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
2799 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2800 root->AddChild(child);
2801 child->AddChild(grand_child);
2803 host()->SetRootLayer(root);
2805 gfx::Transform identity_matrix;
2806 gfx::Transform child_scale_matrix;
2807 child_scale_matrix.Scale(0.25f, 0.25f);
2808 gfx::Transform grand_child_scale_matrix;
2809 grand_child_scale_matrix.Scale(0.246f, 0.246f);
2810 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
2811 gfx::PointF(), gfx::Size(100, 100), true, false);
2812 SetLayerPropertiesForTesting(child.get(), child_scale_matrix, gfx::Point3F(),
2813 gfx::PointF(), gfx::Size(10, 10), true, false);
2814 SetLayerPropertiesForTesting(grand_child.get(), grand_child_scale_matrix,
2815 gfx::Point3F(), gfx::PointF(),
2816 gfx::Size(100, 100), true, false);
2818 child->SetMasksToBounds(true);
2819 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
2821 // The visible rect is expanded to integer coordinates in target space before
2822 // being projected back to layer space, where it is once again expanded to
2823 // integer coordinates.
2824 EXPECT_EQ(gfx::Rect(49, 49), grand_child->visible_rect_from_property_trees());
2827 TEST_F(LayerTreeHostCommonTest,
2828 DrawableAndVisibleContentRectsForLayersInUnclippedRenderSurface) {
2829 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2830 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
2831 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2832 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2833 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2834 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2835 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2836 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2837 root->AddChild(render_surface1);
2838 render_surface1->AddChild(child1);
2839 render_surface1->AddChild(child2);
2840 render_surface1->AddChild(child3);
2842 host()->SetRootLayer(root);
2844 gfx::Transform identity_matrix;
2845 SetLayerPropertiesForTesting(root.get(),
2846 identity_matrix,
2847 gfx::Point3F(),
2848 gfx::PointF(),
2849 gfx::Size(100, 100),
2850 true,
2851 false);
2852 SetLayerPropertiesForTesting(render_surface1.get(),
2853 identity_matrix,
2854 gfx::Point3F(),
2855 gfx::PointF(),
2856 gfx::Size(3, 4),
2857 true,
2858 false);
2859 SetLayerPropertiesForTesting(child1.get(),
2860 identity_matrix,
2861 gfx::Point3F(),
2862 gfx::PointF(5.f, 5.f),
2863 gfx::Size(50, 50),
2864 true,
2865 false);
2866 SetLayerPropertiesForTesting(child2.get(),
2867 identity_matrix,
2868 gfx::Point3F(),
2869 gfx::PointF(75.f, 75.f),
2870 gfx::Size(50, 50),
2871 true,
2872 false);
2873 SetLayerPropertiesForTesting(child3.get(),
2874 identity_matrix,
2875 gfx::Point3F(),
2876 gfx::PointF(125.f, 125.f),
2877 gfx::Size(50, 50),
2878 true,
2879 false);
2881 render_surface1->SetForceRenderSurface(true);
2882 ExecuteCalculateDrawProperties(root.get());
2884 ASSERT_TRUE(render_surface1->render_surface());
2886 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2887 root->render_surface()->DrawableContentRect());
2888 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2890 // Layers that do not draw content should have empty visible content rects.
2891 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_layer_rect());
2892 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_layer_rect());
2894 // An unclipped surface grows its DrawableContentRect to include all drawable
2895 // regions of the subtree.
2896 EXPECT_EQ(gfx::Rect(5, 5, 170, 170),
2897 render_surface1->render_surface()->DrawableContentRect());
2899 // All layers that draw content into the unclipped surface are also unclipped.
2900 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_layer_rect());
2901 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_layer_rect());
2902 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_layer_rect());
2904 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
2905 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
2906 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
2909 TEST_F(LayerTreeHostCommonTest,
2910 VisibleContentRectsForClippedSurfaceWithEmptyClip) {
2911 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2912 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2913 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2914 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2915 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2916 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2917 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2918 root->AddChild(child1);
2919 root->AddChild(child2);
2920 root->AddChild(child3);
2922 host()->SetRootLayer(root);
2924 gfx::Transform identity_matrix;
2925 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
2926 gfx::PointF(), gfx::Size(100, 100), true, false);
2927 SetLayerPropertiesForTesting(child1.get(), identity_matrix, gfx::Point3F(),
2928 gfx::PointF(5.f, 5.f), gfx::Size(50, 50), true,
2929 false);
2930 SetLayerPropertiesForTesting(child2.get(), identity_matrix, gfx::Point3F(),
2931 gfx::PointF(75.f, 75.f), gfx::Size(50, 50), true,
2932 false);
2933 SetLayerPropertiesForTesting(child3.get(), identity_matrix, gfx::Point3F(),
2934 gfx::PointF(125.f, 125.f), gfx::Size(50, 50),
2935 true, false);
2937 RenderSurfaceLayerList render_surface_layer_list;
2938 // Now set the root render surface an empty clip.
2939 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2940 root.get(), gfx::Size(), &render_surface_layer_list);
2942 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2943 ASSERT_TRUE(root->render_surface());
2944 EXPECT_FALSE(root->is_clipped());
2946 gfx::Rect empty;
2947 EXPECT_EQ(empty, root->render_surface()->clip_rect());
2948 EXPECT_TRUE(root->render_surface()->is_clipped());
2950 // Visible content rect calculation will check if the target surface is
2951 // clipped or not. An empty clip rect does not indicate the render surface
2952 // is unclipped.
2953 EXPECT_EQ(empty, child1->visible_layer_rect());
2954 EXPECT_EQ(empty, child2->visible_layer_rect());
2955 EXPECT_EQ(empty, child3->visible_layer_rect());
2958 TEST_F(LayerTreeHostCommonTest,
2959 DrawableAndVisibleContentRectsForLayersWithUninvertibleTransform) {
2960 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2961 scoped_refptr<LayerWithForcedDrawsContent> child =
2962 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2963 root->AddChild(child);
2965 host()->SetRootLayer(root);
2967 // Case 1: a truly degenerate matrix
2968 gfx::Transform identity_matrix;
2969 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
2970 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
2972 SetLayerPropertiesForTesting(root.get(),
2973 identity_matrix,
2974 gfx::Point3F(),
2975 gfx::PointF(),
2976 gfx::Size(100, 100),
2977 true,
2978 false);
2979 SetLayerPropertiesForTesting(child.get(),
2980 uninvertible_matrix,
2981 gfx::Point3F(),
2982 gfx::PointF(5.f, 5.f),
2983 gfx::Size(50, 50),
2984 true,
2985 false);
2987 ExecuteCalculateDrawProperties(root.get());
2989 EXPECT_TRUE(child->visible_layer_rect().IsEmpty());
2990 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
2992 // Case 2: a matrix with flattened z, uninvertible and not visible according
2993 // to the CSS spec.
2994 uninvertible_matrix.MakeIdentity();
2995 uninvertible_matrix.matrix().set(2, 2, 0.0);
2996 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
2998 SetLayerPropertiesForTesting(child.get(),
2999 uninvertible_matrix,
3000 gfx::Point3F(),
3001 gfx::PointF(5.f, 5.f),
3002 gfx::Size(50, 50),
3003 true,
3004 false);
3006 ExecuteCalculateDrawProperties(root.get());
3008 EXPECT_TRUE(child->visible_layer_rect().IsEmpty());
3009 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3011 // Case 3: a matrix with flattened z, also uninvertible and not visible.
3012 uninvertible_matrix.MakeIdentity();
3013 uninvertible_matrix.Translate(500.0, 0.0);
3014 uninvertible_matrix.matrix().set(2, 2, 0.0);
3015 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3017 SetLayerPropertiesForTesting(child.get(),
3018 uninvertible_matrix,
3019 gfx::Point3F(),
3020 gfx::PointF(5.f, 5.f),
3021 gfx::Size(50, 50),
3022 true,
3023 false);
3025 ExecuteCalculateDrawProperties(root.get());
3027 EXPECT_TRUE(child->visible_layer_rect().IsEmpty());
3028 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3031 TEST_F(LayerTreeHostCommonTest,
3032 SingularTransformDoesNotPreventClearingDrawProperties) {
3033 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3034 scoped_refptr<LayerWithForcedDrawsContent> child =
3035 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3036 root->AddChild(child);
3038 host()->SetRootLayer(root);
3040 gfx::Transform identity_matrix;
3041 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
3042 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3044 SetLayerPropertiesForTesting(root.get(),
3045 uninvertible_matrix,
3046 gfx::Point3F(),
3047 gfx::PointF(),
3048 gfx::Size(100, 100),
3049 true,
3050 false);
3051 SetLayerPropertiesForTesting(child.get(),
3052 identity_matrix,
3053 gfx::Point3F(),
3054 gfx::PointF(5.f, 5.f),
3055 gfx::Size(50, 50),
3056 true,
3057 false);
3059 child->set_sorted_for_recursion(true);
3061 TransformOperations start_transform_operations;
3062 start_transform_operations.AppendScale(1.f, 0.f, 0.f);
3064 TransformOperations end_transform_operations;
3065 end_transform_operations.AppendScale(1.f, 1.f, 0.f);
3067 AddAnimatedTransformToLayer(
3068 root.get(), 10.0, start_transform_operations, end_transform_operations);
3070 EXPECT_TRUE(root->TransformIsAnimating());
3072 ExecuteCalculateDrawProperties(root.get());
3074 EXPECT_FALSE(child->sorted_for_recursion());
3077 TEST_F(LayerTreeHostCommonTest,
3078 SingularNonAnimatingTransformDoesNotPreventClearingDrawProperties) {
3079 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3081 host()->SetRootLayer(root);
3083 gfx::Transform identity_matrix;
3084 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
3085 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3087 SetLayerPropertiesForTesting(root.get(),
3088 uninvertible_matrix,
3089 gfx::Point3F(),
3090 gfx::PointF(),
3091 gfx::Size(100, 100),
3092 true,
3093 false);
3095 root->set_sorted_for_recursion(true);
3097 EXPECT_FALSE(root->TransformIsAnimating());
3099 ExecuteCalculateDrawProperties(root.get());
3101 EXPECT_FALSE(root->sorted_for_recursion());
3104 TEST_F(LayerTreeHostCommonTest,
3105 DrawableAndVisibleContentRectsForLayersInClippedRenderSurface) {
3106 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3107 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
3108 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3109 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3110 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3111 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3112 scoped_refptr<LayerWithForcedDrawsContent> child3 =
3113 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3114 root->AddChild(render_surface1);
3115 render_surface1->AddChild(child1);
3116 render_surface1->AddChild(child2);
3117 render_surface1->AddChild(child3);
3119 host()->SetRootLayer(root);
3121 gfx::Transform identity_matrix;
3122 SetLayerPropertiesForTesting(root.get(),
3123 identity_matrix,
3124 gfx::Point3F(),
3125 gfx::PointF(),
3126 gfx::Size(100, 100),
3127 true,
3128 false);
3129 SetLayerPropertiesForTesting(render_surface1.get(),
3130 identity_matrix,
3131 gfx::Point3F(),
3132 gfx::PointF(),
3133 gfx::Size(3, 4),
3134 true,
3135 false);
3136 SetLayerPropertiesForTesting(child1.get(),
3137 identity_matrix,
3138 gfx::Point3F(),
3139 gfx::PointF(5.f, 5.f),
3140 gfx::Size(50, 50),
3141 true,
3142 false);
3143 SetLayerPropertiesForTesting(child2.get(),
3144 identity_matrix,
3145 gfx::Point3F(),
3146 gfx::PointF(75.f, 75.f),
3147 gfx::Size(50, 50),
3148 true,
3149 false);
3150 SetLayerPropertiesForTesting(child3.get(),
3151 identity_matrix,
3152 gfx::Point3F(),
3153 gfx::PointF(125.f, 125.f),
3154 gfx::Size(50, 50),
3155 true,
3156 false);
3158 root->SetMasksToBounds(true);
3159 render_surface1->SetForceRenderSurface(true);
3160 ExecuteCalculateDrawProperties(root.get());
3162 ASSERT_TRUE(render_surface1->render_surface());
3164 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3165 root->render_surface()->DrawableContentRect());
3166 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3168 // Layers that do not draw content should have empty visible content rects.
3169 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_layer_rect());
3170 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_layer_rect());
3172 // A clipped surface grows its DrawableContentRect to include all drawable
3173 // regions of the subtree, but also gets clamped by the ancestor's clip.
3174 EXPECT_EQ(gfx::Rect(5, 5, 95, 95),
3175 render_surface1->render_surface()->DrawableContentRect());
3177 // All layers that draw content into the surface have their visible content
3178 // rect clipped by the surface clip rect.
3179 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_layer_rect());
3180 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_layer_rect());
3181 EXPECT_TRUE(child3->visible_layer_rect().IsEmpty());
3183 // But the DrawableContentRects are unclipped.
3184 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3185 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3186 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3189 TEST_F(LayerTreeHostCommonTest,
3190 DrawableAndVisibleContentRectsForSurfaceHierarchy) {
3191 // Check that clipping does not propagate down surfaces.
3192 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3193 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
3194 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
3195 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3196 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3197 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3198 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3199 scoped_refptr<LayerWithForcedDrawsContent> child3 =
3200 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3201 root->AddChild(render_surface1);
3202 render_surface1->AddChild(render_surface2);
3203 render_surface2->AddChild(child1);
3204 render_surface2->AddChild(child2);
3205 render_surface2->AddChild(child3);
3207 host()->SetRootLayer(root);
3209 gfx::Transform identity_matrix;
3210 SetLayerPropertiesForTesting(root.get(),
3211 identity_matrix,
3212 gfx::Point3F(),
3213 gfx::PointF(),
3214 gfx::Size(100, 100),
3215 true,
3216 false);
3217 SetLayerPropertiesForTesting(render_surface1.get(),
3218 identity_matrix,
3219 gfx::Point3F(),
3220 gfx::PointF(),
3221 gfx::Size(3, 4),
3222 true,
3223 false);
3224 SetLayerPropertiesForTesting(render_surface2.get(),
3225 identity_matrix,
3226 gfx::Point3F(),
3227 gfx::PointF(),
3228 gfx::Size(7, 13),
3229 true,
3230 false);
3231 SetLayerPropertiesForTesting(child1.get(),
3232 identity_matrix,
3233 gfx::Point3F(),
3234 gfx::PointF(5.f, 5.f),
3235 gfx::Size(50, 50),
3236 true,
3237 false);
3238 SetLayerPropertiesForTesting(child2.get(),
3239 identity_matrix,
3240 gfx::Point3F(),
3241 gfx::PointF(75.f, 75.f),
3242 gfx::Size(50, 50),
3243 true,
3244 false);
3245 SetLayerPropertiesForTesting(child3.get(),
3246 identity_matrix,
3247 gfx::Point3F(),
3248 gfx::PointF(125.f, 125.f),
3249 gfx::Size(50, 50),
3250 true,
3251 false);
3253 root->SetMasksToBounds(true);
3254 render_surface1->SetForceRenderSurface(true);
3255 render_surface2->SetForceRenderSurface(true);
3256 ExecuteCalculateDrawProperties(root.get());
3258 ASSERT_TRUE(render_surface1->render_surface());
3259 ASSERT_TRUE(render_surface2->render_surface());
3261 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3262 root->render_surface()->DrawableContentRect());
3263 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3265 // Layers that do not draw content should have empty visible content rects.
3266 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_layer_rect());
3267 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_layer_rect());
3268 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface2->visible_layer_rect());
3270 // A clipped surface grows its DrawableContentRect to include all drawable
3271 // regions of the subtree, but also gets clamped by the ancestor's clip.
3272 EXPECT_EQ(gfx::Rect(5, 5, 95, 95),
3273 render_surface1->render_surface()->DrawableContentRect());
3275 // render_surface1 lives in the "unclipped universe" of render_surface1, and
3276 // is only implicitly clipped by render_surface1's content rect. So,
3277 // render_surface2 grows to enclose all drawable content of its subtree.
3278 EXPECT_EQ(gfx::Rect(5, 5, 170, 170),
3279 render_surface2->render_surface()->DrawableContentRect());
3281 // All layers that draw content into render_surface2 think they are unclipped.
3282 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_layer_rect());
3283 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_layer_rect());
3284 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_layer_rect());
3286 // DrawableContentRects are also unclipped.
3287 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3288 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3289 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3292 TEST_F(LayerTreeHostCommonTest,
3293 DrawableAndVisibleContentRectsWithTransformOnUnclippedSurface) {
3294 // Layers that have non-axis aligned bounds (due to transforms) have an
3295 // expanded, axis-aligned DrawableContentRect and visible content rect.
3297 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3298 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
3299 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3300 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3301 root->AddChild(render_surface1);
3302 render_surface1->AddChild(child1);
3304 host()->SetRootLayer(root);
3306 gfx::Transform identity_matrix;
3307 gfx::Transform child_rotation;
3308 child_rotation.Rotate(45.0);
3309 SetLayerPropertiesForTesting(root.get(),
3310 identity_matrix,
3311 gfx::Point3F(),
3312 gfx::PointF(),
3313 gfx::Size(100, 100),
3314 true,
3315 false);
3316 SetLayerPropertiesForTesting(render_surface1.get(),
3317 identity_matrix,
3318 gfx::Point3F(),
3319 gfx::PointF(),
3320 gfx::Size(3, 4),
3321 true,
3322 false);
3323 SetLayerPropertiesForTesting(child1.get(),
3324 child_rotation,
3325 gfx::Point3F(25, 25, 0.f),
3326 gfx::PointF(25.f, 25.f),
3327 gfx::Size(50, 50),
3328 true,
3329 false);
3331 render_surface1->SetForceRenderSurface(true);
3332 ExecuteCalculateDrawProperties(root.get());
3334 ASSERT_TRUE(render_surface1->render_surface());
3336 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3337 root->render_surface()->DrawableContentRect());
3338 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3340 // Layers that do not draw content should have empty visible content rects.
3341 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_layer_rect());
3342 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_layer_rect());
3344 // The unclipped surface grows its DrawableContentRect to include all drawable
3345 // regions of the subtree.
3346 int diagonal_radius = ceil(sqrt(2.0) * 25.0);
3347 gfx::Rect expected_surface_drawable_content =
3348 gfx::Rect(50 - diagonal_radius,
3349 50 - diagonal_radius,
3350 diagonal_radius * 2,
3351 diagonal_radius * 2);
3352 EXPECT_EQ(expected_surface_drawable_content,
3353 render_surface1->render_surface()->DrawableContentRect());
3355 // All layers that draw content into the unclipped surface are also unclipped.
3356 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_layer_rect());
3357 EXPECT_EQ(expected_surface_drawable_content, child1->drawable_content_rect());
3360 TEST_F(LayerTreeHostCommonTest,
3361 DrawableAndVisibleContentRectsWithTransformOnClippedSurface) {
3362 // Layers that have non-axis aligned bounds (due to transforms) have an
3363 // expanded, axis-aligned DrawableContentRect and visible content rect.
3365 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3366 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
3367 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3368 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3369 root->AddChild(render_surface1);
3370 render_surface1->AddChild(child1);
3372 host()->SetRootLayer(root);
3374 gfx::Transform identity_matrix;
3375 gfx::Transform child_rotation;
3376 child_rotation.Rotate(45.0);
3377 SetLayerPropertiesForTesting(root.get(),
3378 identity_matrix,
3379 gfx::Point3F(),
3380 gfx::PointF(),
3381 gfx::Size(50, 50),
3382 true,
3383 false);
3384 SetLayerPropertiesForTesting(render_surface1.get(),
3385 identity_matrix,
3386 gfx::Point3F(),
3387 gfx::PointF(),
3388 gfx::Size(3, 4),
3389 true,
3390 false);
3392 SetLayerPropertiesForTesting(child1.get(),
3393 child_rotation,
3394 gfx::Point3F(25, 25, 0.f),
3395 gfx::PointF(25.f, 25.f),
3396 gfx::Size(50, 50),
3397 true,
3398 false);
3400 root->SetMasksToBounds(true);
3401 render_surface1->SetForceRenderSurface(true);
3402 ExecuteCalculateDrawProperties(root.get());
3404 ASSERT_TRUE(render_surface1->render_surface());
3406 // The clipped surface clamps the DrawableContentRect that encloses the
3407 // rotated layer.
3408 int diagonal_radius = ceil(sqrt(2.0) * 25.0);
3409 gfx::Rect unclipped_surface_content = gfx::Rect(50 - diagonal_radius,
3410 50 - diagonal_radius,
3411 diagonal_radius * 2,
3412 diagonal_radius * 2);
3413 gfx::Rect expected_surface_drawable_content =
3414 gfx::IntersectRects(unclipped_surface_content, gfx::Rect(0, 0, 50, 50));
3415 EXPECT_EQ(expected_surface_drawable_content,
3416 render_surface1->render_surface()->DrawableContentRect());
3418 // On the clipped surface, only a quarter of the child1 is visible, but when
3419 // rotating it back to child1's content space, the actual enclosing rect ends
3420 // up covering the full left half of child1.
3422 // Given the floating point math, this number is a little bit fuzzy.
3423 EXPECT_EQ(gfx::Rect(0, 0, 26, 50), child1->visible_layer_rect());
3425 // The child's DrawableContentRect is unclipped.
3426 EXPECT_EQ(unclipped_surface_content, child1->drawable_content_rect());
3429 TEST_F(LayerTreeHostCommonTest, DrawableAndVisibleContentRectsInHighDPI) {
3430 MockContentLayerClient client;
3432 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3433 scoped_refptr<FakePictureLayer> render_surface1 =
3434 CreateDrawablePictureLayer(layer_settings(), &client);
3435 scoped_refptr<FakePictureLayer> render_surface2 =
3436 CreateDrawablePictureLayer(layer_settings(), &client);
3437 scoped_refptr<FakePictureLayer> child1 =
3438 CreateDrawablePictureLayer(layer_settings(), &client);
3439 scoped_refptr<FakePictureLayer> child2 =
3440 CreateDrawablePictureLayer(layer_settings(), &client);
3441 scoped_refptr<FakePictureLayer> child3 =
3442 CreateDrawablePictureLayer(layer_settings(), &client);
3443 root->AddChild(render_surface1);
3444 render_surface1->AddChild(render_surface2);
3445 render_surface2->AddChild(child1);
3446 render_surface2->AddChild(child2);
3447 render_surface2->AddChild(child3);
3449 host()->SetRootLayer(root);
3451 gfx::Transform identity_matrix;
3452 SetLayerPropertiesForTesting(root.get(),
3453 identity_matrix,
3454 gfx::Point3F(),
3455 gfx::PointF(),
3456 gfx::Size(100, 100),
3457 true,
3458 false);
3459 SetLayerPropertiesForTesting(render_surface1.get(),
3460 identity_matrix,
3461 gfx::Point3F(),
3462 gfx::PointF(5.f, 5.f),
3463 gfx::Size(3, 4),
3464 true,
3465 false);
3466 SetLayerPropertiesForTesting(render_surface2.get(),
3467 identity_matrix,
3468 gfx::Point3F(),
3469 gfx::PointF(5.f, 5.f),
3470 gfx::Size(7, 13),
3471 true,
3472 false);
3473 SetLayerPropertiesForTesting(child1.get(),
3474 identity_matrix,
3475 gfx::Point3F(),
3476 gfx::PointF(5.f, 5.f),
3477 gfx::Size(50, 50),
3478 true,
3479 false);
3480 SetLayerPropertiesForTesting(child2.get(),
3481 identity_matrix,
3482 gfx::Point3F(),
3483 gfx::PointF(75.f, 75.f),
3484 gfx::Size(50, 50),
3485 true,
3486 false);
3487 SetLayerPropertiesForTesting(child3.get(),
3488 identity_matrix,
3489 gfx::Point3F(),
3490 gfx::PointF(125.f, 125.f),
3491 gfx::Size(50, 50),
3492 true,
3493 false);
3495 float device_scale_factor = 2.f;
3497 root->SetMasksToBounds(true);
3498 render_surface1->SetForceRenderSurface(true);
3499 render_surface2->SetForceRenderSurface(true);
3500 ExecuteCalculateDrawProperties(root.get(), device_scale_factor);
3502 ASSERT_TRUE(render_surface1->render_surface());
3503 ASSERT_TRUE(render_surface2->render_surface());
3505 // drawable_content_rects for all layers and surfaces are scaled by
3506 // device_scale_factor.
3507 EXPECT_EQ(gfx::Rect(0, 0, 200, 200),
3508 root->render_surface()->DrawableContentRect());
3509 EXPECT_EQ(gfx::Rect(0, 0, 200, 200), root->drawable_content_rect());
3510 EXPECT_EQ(gfx::Rect(10, 10, 190, 190),
3511 render_surface1->render_surface()->DrawableContentRect());
3513 // render_surface2 lives in the "unclipped universe" of render_surface1, and
3514 // is only implicitly clipped by render_surface1.
3515 EXPECT_EQ(gfx::Rect(10, 10, 350, 350),
3516 render_surface2->render_surface()->DrawableContentRect());
3518 EXPECT_EQ(gfx::Rect(10, 10, 100, 100), child1->drawable_content_rect());
3519 EXPECT_EQ(gfx::Rect(150, 150, 100, 100), child2->drawable_content_rect());
3520 EXPECT_EQ(gfx::Rect(250, 250, 100, 100), child3->drawable_content_rect());
3522 // The root layer does not actually draw content of its own.
3523 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_layer_rect());
3525 // All layer visible content rects are not expressed in content space of each
3526 // layer, so they are not scaled by the device_scale_factor.
3527 EXPECT_EQ(gfx::Rect(0, 0, 3, 4), render_surface1->visible_layer_rect());
3528 EXPECT_EQ(gfx::Rect(0, 0, 7, 13), render_surface2->visible_layer_rect());
3529 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_layer_rect());
3530 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_layer_rect());
3531 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_layer_rect());
3534 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithoutPreserves3d) {
3535 // Verify the behavior of back-face culling when there are no preserve-3d
3536 // layers. Note that 3d transforms still apply in this case, but they are
3537 // "flattened" to each parent layer according to current W3C spec.
3539 const gfx::Transform identity_matrix;
3540 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
3541 scoped_refptr<LayerWithForcedDrawsContent> front_facing_child =
3542 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3543 scoped_refptr<LayerWithForcedDrawsContent> back_facing_child =
3544 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3545 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3546 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3547 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3548 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3549 scoped_refptr<LayerWithForcedDrawsContent>
3550 front_facing_child_of_front_facing_surface =
3551 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3552 scoped_refptr<LayerWithForcedDrawsContent>
3553 back_facing_child_of_front_facing_surface =
3554 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3555 scoped_refptr<LayerWithForcedDrawsContent>
3556 front_facing_child_of_back_facing_surface =
3557 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3558 scoped_refptr<LayerWithForcedDrawsContent>
3559 back_facing_child_of_back_facing_surface =
3560 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3562 parent->AddChild(front_facing_child);
3563 parent->AddChild(back_facing_child);
3564 parent->AddChild(front_facing_surface);
3565 parent->AddChild(back_facing_surface);
3566 front_facing_surface->AddChild(front_facing_child_of_front_facing_surface);
3567 front_facing_surface->AddChild(back_facing_child_of_front_facing_surface);
3568 back_facing_surface->AddChild(front_facing_child_of_back_facing_surface);
3569 back_facing_surface->AddChild(back_facing_child_of_back_facing_surface);
3571 host()->SetRootLayer(parent);
3573 // Nothing is double-sided
3574 front_facing_child->SetDoubleSided(false);
3575 back_facing_child->SetDoubleSided(false);
3576 front_facing_surface->SetDoubleSided(false);
3577 back_facing_surface->SetDoubleSided(false);
3578 front_facing_child_of_front_facing_surface->SetDoubleSided(false);
3579 back_facing_child_of_front_facing_surface->SetDoubleSided(false);
3580 front_facing_child_of_back_facing_surface->SetDoubleSided(false);
3581 back_facing_child_of_back_facing_surface->SetDoubleSided(false);
3583 gfx::Transform backface_matrix;
3584 backface_matrix.Translate(50.0, 50.0);
3585 backface_matrix.RotateAboutYAxis(180.0);
3586 backface_matrix.Translate(-50.0, -50.0);
3588 // Having a descendant and opacity will force these to have render surfaces.
3589 front_facing_surface->SetOpacity(0.5f);
3590 back_facing_surface->SetOpacity(0.5f);
3592 // Nothing preserves 3d. According to current W3C CSS gfx::Transforms spec,
3593 // these layers should blindly use their own local transforms to determine
3594 // back-face culling.
3595 SetLayerPropertiesForTesting(parent.get(),
3596 identity_matrix,
3597 gfx::Point3F(),
3598 gfx::PointF(),
3599 gfx::Size(100, 100),
3600 true,
3601 false);
3602 SetLayerPropertiesForTesting(front_facing_child.get(),
3603 identity_matrix,
3604 gfx::Point3F(),
3605 gfx::PointF(),
3606 gfx::Size(100, 100),
3607 true,
3608 false);
3609 SetLayerPropertiesForTesting(back_facing_child.get(),
3610 backface_matrix,
3611 gfx::Point3F(),
3612 gfx::PointF(),
3613 gfx::Size(100, 100),
3614 true,
3615 false);
3616 SetLayerPropertiesForTesting(front_facing_surface.get(),
3617 identity_matrix,
3618 gfx::Point3F(),
3619 gfx::PointF(),
3620 gfx::Size(100, 100),
3621 true,
3622 false);
3623 SetLayerPropertiesForTesting(back_facing_surface.get(),
3624 backface_matrix,
3625 gfx::Point3F(),
3626 gfx::PointF(),
3627 gfx::Size(100, 100),
3628 true,
3629 false);
3630 SetLayerPropertiesForTesting(front_facing_child_of_front_facing_surface.get(),
3631 identity_matrix,
3632 gfx::Point3F(),
3633 gfx::PointF(),
3634 gfx::Size(100, 100),
3635 true,
3636 false);
3637 SetLayerPropertiesForTesting(back_facing_child_of_front_facing_surface.get(),
3638 backface_matrix,
3639 gfx::Point3F(),
3640 gfx::PointF(),
3641 gfx::Size(100, 100),
3642 true,
3643 false);
3644 SetLayerPropertiesForTesting(front_facing_child_of_back_facing_surface.get(),
3645 identity_matrix,
3646 gfx::Point3F(),
3647 gfx::PointF(),
3648 gfx::Size(100, 100),
3649 true,
3650 false);
3651 SetLayerPropertiesForTesting(back_facing_child_of_back_facing_surface.get(),
3652 backface_matrix,
3653 gfx::Point3F(),
3654 gfx::PointF(),
3655 gfx::Size(100, 100),
3656 true,
3657 false);
3659 ExecuteCalculateDrawPropertiesWithPropertyTrees(parent.get());
3661 // Verify which render surfaces were created.
3662 EXPECT_FALSE(front_facing_child->render_surface());
3663 EXPECT_FALSE(back_facing_child->render_surface());
3664 EXPECT_TRUE(front_facing_surface->render_surface());
3665 EXPECT_TRUE(back_facing_surface->render_surface());
3666 EXPECT_FALSE(front_facing_child_of_front_facing_surface->render_surface());
3667 EXPECT_FALSE(back_facing_child_of_front_facing_surface->render_surface());
3668 EXPECT_FALSE(front_facing_child_of_back_facing_surface->render_surface());
3669 EXPECT_FALSE(back_facing_child_of_back_facing_surface->render_surface());
3671 EXPECT_EQ(4u, update_layer_list().size());
3672 EXPECT_TRUE(UpdateLayerListContains(front_facing_child->id()));
3673 EXPECT_TRUE(UpdateLayerListContains(front_facing_surface->id()));
3674 EXPECT_TRUE(UpdateLayerListContains(
3675 front_facing_child_of_front_facing_surface->id()));
3676 EXPECT_TRUE(
3677 UpdateLayerListContains(front_facing_child_of_back_facing_surface->id()));
3680 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithPreserves3d) {
3681 // Verify the behavior of back-face culling when preserves-3d transform style
3682 // is used.
3684 const gfx::Transform identity_matrix;
3685 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
3686 scoped_refptr<LayerWithForcedDrawsContent> front_facing_child =
3687 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3688 scoped_refptr<LayerWithForcedDrawsContent> back_facing_child =
3689 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3690 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3691 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3692 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3693 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3694 scoped_refptr<LayerWithForcedDrawsContent>
3695 front_facing_child_of_front_facing_surface =
3696 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3697 scoped_refptr<LayerWithForcedDrawsContent>
3698 back_facing_child_of_front_facing_surface =
3699 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3700 scoped_refptr<LayerWithForcedDrawsContent>
3701 front_facing_child_of_back_facing_surface =
3702 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3703 scoped_refptr<LayerWithForcedDrawsContent>
3704 back_facing_child_of_back_facing_surface =
3705 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3706 scoped_refptr<LayerWithForcedDrawsContent> dummy_replica_layer1 =
3707 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3708 scoped_refptr<LayerWithForcedDrawsContent> dummy_replica_layer2 =
3709 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3711 parent->AddChild(front_facing_child);
3712 parent->AddChild(back_facing_child);
3713 parent->AddChild(front_facing_surface);
3714 parent->AddChild(back_facing_surface);
3715 front_facing_surface->AddChild(front_facing_child_of_front_facing_surface);
3716 front_facing_surface->AddChild(back_facing_child_of_front_facing_surface);
3717 back_facing_surface->AddChild(front_facing_child_of_back_facing_surface);
3718 back_facing_surface->AddChild(back_facing_child_of_back_facing_surface);
3720 host()->SetRootLayer(parent);
3722 // Nothing is double-sided
3723 front_facing_child->SetDoubleSided(false);
3724 back_facing_child->SetDoubleSided(false);
3725 front_facing_surface->SetDoubleSided(false);
3726 back_facing_surface->SetDoubleSided(false);
3727 front_facing_child_of_front_facing_surface->SetDoubleSided(false);
3728 back_facing_child_of_front_facing_surface->SetDoubleSided(false);
3729 front_facing_child_of_back_facing_surface->SetDoubleSided(false);
3730 back_facing_child_of_back_facing_surface->SetDoubleSided(false);
3732 gfx::Transform backface_matrix;
3733 backface_matrix.Translate(50.0, 50.0);
3734 backface_matrix.RotateAboutYAxis(180.0);
3735 backface_matrix.Translate(-50.0, -50.0);
3737 // Opacity will not force creation of render surfaces in this case because of
3738 // the preserve-3d transform style. Instead, an example of when a surface
3739 // would be created with preserve-3d is when there is a replica layer.
3740 front_facing_surface->SetReplicaLayer(dummy_replica_layer1.get());
3741 back_facing_surface->SetReplicaLayer(dummy_replica_layer2.get());
3743 // Each surface creates its own new 3d rendering context (as defined by W3C
3744 // spec). According to current W3C CSS gfx::Transforms spec, layers in a 3d
3745 // rendering context should use the transform with respect to that context.
3746 // This 3d rendering context occurs when (a) parent's transform style is flat
3747 // and (b) the layer's transform style is preserve-3d.
3748 SetLayerPropertiesForTesting(parent.get(),
3749 identity_matrix,
3750 gfx::Point3F(),
3751 gfx::PointF(),
3752 gfx::Size(100, 100),
3753 true,
3754 false); // parent transform style is flat.
3755 SetLayerPropertiesForTesting(front_facing_child.get(),
3756 identity_matrix,
3757 gfx::Point3F(),
3758 gfx::PointF(),
3759 gfx::Size(100, 100),
3760 true,
3761 false);
3762 SetLayerPropertiesForTesting(back_facing_child.get(),
3763 backface_matrix,
3764 gfx::Point3F(),
3765 gfx::PointF(),
3766 gfx::Size(100, 100),
3767 true,
3768 false);
3769 // surface transform style is preserve-3d.
3770 SetLayerPropertiesForTesting(front_facing_surface.get(),
3771 identity_matrix,
3772 gfx::Point3F(),
3773 gfx::PointF(),
3774 gfx::Size(100, 100),
3775 false,
3776 true);
3777 // surface transform style is preserve-3d.
3778 SetLayerPropertiesForTesting(back_facing_surface.get(),
3779 backface_matrix,
3780 gfx::Point3F(),
3781 gfx::PointF(),
3782 gfx::Size(100, 100),
3783 false,
3784 true);
3785 SetLayerPropertiesForTesting(front_facing_child_of_front_facing_surface.get(),
3786 identity_matrix,
3787 gfx::Point3F(),
3788 gfx::PointF(),
3789 gfx::Size(100, 100),
3790 true,
3791 true);
3792 SetLayerPropertiesForTesting(back_facing_child_of_front_facing_surface.get(),
3793 backface_matrix,
3794 gfx::Point3F(),
3795 gfx::PointF(),
3796 gfx::Size(100, 100),
3797 true,
3798 true);
3799 SetLayerPropertiesForTesting(front_facing_child_of_back_facing_surface.get(),
3800 identity_matrix,
3801 gfx::Point3F(),
3802 gfx::PointF(),
3803 gfx::Size(100, 100),
3804 true,
3805 true);
3806 SetLayerPropertiesForTesting(back_facing_child_of_back_facing_surface.get(),
3807 backface_matrix,
3808 gfx::Point3F(),
3809 gfx::PointF(),
3810 gfx::Size(100, 100),
3811 true,
3812 true);
3814 ExecuteCalculateDrawPropertiesWithPropertyTrees(parent.get());
3816 // Verify which render surfaces were created and used.
3817 EXPECT_FALSE(front_facing_child->render_surface());
3818 EXPECT_FALSE(back_facing_child->render_surface());
3819 EXPECT_TRUE(front_facing_surface->render_surface());
3820 EXPECT_NE(back_facing_surface->render_target(), back_facing_surface);
3821 // We expect that a render_surface was created but not used.
3822 EXPECT_TRUE(back_facing_surface->render_surface());
3823 EXPECT_FALSE(front_facing_child_of_front_facing_surface->render_surface());
3824 EXPECT_FALSE(back_facing_child_of_front_facing_surface->render_surface());
3825 EXPECT_FALSE(front_facing_child_of_back_facing_surface->render_surface());
3826 EXPECT_FALSE(back_facing_child_of_back_facing_surface->render_surface());
3828 EXPECT_EQ(3u, update_layer_list().size());
3830 EXPECT_TRUE(UpdateLayerListContains(front_facing_child->id()));
3831 EXPECT_TRUE(UpdateLayerListContains(front_facing_surface->id()));
3832 EXPECT_TRUE(UpdateLayerListContains(
3833 front_facing_child_of_front_facing_surface->id()));
3836 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithAnimatingTransforms) {
3837 // Verify that layers are appropriately culled when their back face is showing
3838 // and they are not double sided, while animations are going on.
3840 // Layers that are animating do not get culled on the main thread, as their
3841 // transforms should be treated as "unknown" so we can not be sure that their
3842 // back face is really showing.
3843 const gfx::Transform identity_matrix;
3844 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
3845 scoped_refptr<LayerWithForcedDrawsContent> child =
3846 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3847 scoped_refptr<LayerWithForcedDrawsContent> animating_surface =
3848 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3849 scoped_refptr<LayerWithForcedDrawsContent> child_of_animating_surface =
3850 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3851 scoped_refptr<LayerWithForcedDrawsContent> animating_child =
3852 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3853 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3854 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3856 parent->AddChild(child);
3857 parent->AddChild(animating_surface);
3858 animating_surface->AddChild(child_of_animating_surface);
3859 parent->AddChild(animating_child);
3860 parent->AddChild(child2);
3862 host()->SetRootLayer(parent);
3864 // Nothing is double-sided
3865 child->SetDoubleSided(false);
3866 child2->SetDoubleSided(false);
3867 animating_surface->SetDoubleSided(false);
3868 child_of_animating_surface->SetDoubleSided(false);
3869 animating_child->SetDoubleSided(false);
3871 gfx::Transform backface_matrix;
3872 backface_matrix.Translate(50.0, 50.0);
3873 backface_matrix.RotateAboutYAxis(180.0);
3874 backface_matrix.Translate(-50.0, -50.0);
3876 // Make our render surface.
3877 animating_surface->SetForceRenderSurface(true);
3879 // Animate the transform on the render surface.
3880 AddAnimatedTransformToController(
3881 animating_surface->layer_animation_controller(), 10.0, 30, 0);
3882 // This is just an animating layer, not a surface.
3883 AddAnimatedTransformToController(
3884 animating_child->layer_animation_controller(), 10.0, 30, 0);
3886 SetLayerPropertiesForTesting(parent.get(),
3887 identity_matrix,
3888 gfx::Point3F(),
3889 gfx::PointF(),
3890 gfx::Size(100, 100),
3891 true,
3892 false);
3893 SetLayerPropertiesForTesting(child.get(),
3894 backface_matrix,
3895 gfx::Point3F(),
3896 gfx::PointF(),
3897 gfx::Size(100, 100),
3898 true,
3899 false);
3900 SetLayerPropertiesForTesting(animating_surface.get(),
3901 backface_matrix,
3902 gfx::Point3F(),
3903 gfx::PointF(),
3904 gfx::Size(100, 100),
3905 true,
3906 false);
3907 SetLayerPropertiesForTesting(child_of_animating_surface.get(),
3908 backface_matrix,
3909 gfx::Point3F(),
3910 gfx::PointF(),
3911 gfx::Size(100, 100),
3912 true,
3913 false);
3914 SetLayerPropertiesForTesting(animating_child.get(),
3915 backface_matrix,
3916 gfx::Point3F(),
3917 gfx::PointF(),
3918 gfx::Size(100, 100),
3919 true,
3920 false);
3921 SetLayerPropertiesForTesting(child2.get(),
3922 identity_matrix,
3923 gfx::Point3F(),
3924 gfx::PointF(),
3925 gfx::Size(100, 100),
3926 true,
3927 false);
3929 RenderSurfaceLayerList render_surface_layer_list;
3930 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
3931 parent.get(), parent->bounds(), &render_surface_layer_list);
3932 inputs.can_adjust_raster_scales = true;
3933 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
3935 EXPECT_FALSE(child->render_surface());
3936 EXPECT_TRUE(animating_surface->render_surface());
3937 EXPECT_FALSE(child_of_animating_surface->render_surface());
3938 EXPECT_FALSE(animating_child->render_surface());
3939 EXPECT_FALSE(child2->render_surface());
3941 ExecuteCalculateDrawPropertiesWithPropertyTrees(parent.get());
3943 EXPECT_EQ(4u, update_layer_list().size());
3945 // The non-animating child be culled from the layer list for the parent render
3946 // surface.
3947 EXPECT_TRUE(UpdateLayerListContains(animating_surface->id()));
3948 EXPECT_TRUE(UpdateLayerListContains(animating_child->id()));
3949 EXPECT_TRUE(UpdateLayerListContains(child2->id()));
3950 EXPECT_TRUE(UpdateLayerListContains(child_of_animating_surface->id()));
3952 EXPECT_FALSE(child2->visible_rect_from_property_trees().IsEmpty());
3954 // The animating layers should have a visible content rect that represents the
3955 // area of the front face that is within the viewport.
3956 EXPECT_EQ(animating_child->visible_rect_from_property_trees(),
3957 gfx::Rect(animating_child->bounds()));
3958 EXPECT_EQ(animating_surface->visible_rect_from_property_trees(),
3959 gfx::Rect(animating_surface->bounds()));
3960 // And layers in the subtree of the animating layer should have valid visible
3961 // content rects also.
3962 EXPECT_EQ(child_of_animating_surface->visible_rect_from_property_trees(),
3963 gfx::Rect(child_of_animating_surface->bounds()));
3966 TEST_F(LayerTreeHostCommonTest,
3967 BackFaceCullingWithPreserves3dForFlatteningSurface) {
3968 // Verify the behavior of back-face culling for a render surface that is
3969 // created when it flattens its subtree, and its parent has preserves-3d.
3971 const gfx::Transform identity_matrix;
3972 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
3973 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3974 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3975 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3976 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3977 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3978 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3979 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3980 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3982 parent->AddChild(front_facing_surface);
3983 parent->AddChild(back_facing_surface);
3984 front_facing_surface->AddChild(child1);
3985 back_facing_surface->AddChild(child2);
3987 host()->SetRootLayer(parent);
3989 // RenderSurfaces are not double-sided
3990 front_facing_surface->SetDoubleSided(false);
3991 back_facing_surface->SetDoubleSided(false);
3993 gfx::Transform backface_matrix;
3994 backface_matrix.Translate(50.0, 50.0);
3995 backface_matrix.RotateAboutYAxis(180.0);
3996 backface_matrix.Translate(-50.0, -50.0);
3998 SetLayerPropertiesForTesting(parent.get(),
3999 identity_matrix,
4000 gfx::Point3F(),
4001 gfx::PointF(),
4002 gfx::Size(100, 100),
4003 false,
4004 true); // parent transform style is preserve3d.
4005 SetLayerPropertiesForTesting(front_facing_surface.get(),
4006 identity_matrix,
4007 gfx::Point3F(),
4008 gfx::PointF(),
4009 gfx::Size(100, 100),
4010 true,
4011 true); // surface transform style is flat.
4012 SetLayerPropertiesForTesting(back_facing_surface.get(),
4013 backface_matrix,
4014 gfx::Point3F(),
4015 gfx::PointF(),
4016 gfx::Size(100, 100),
4017 true,
4018 true); // surface transform style is flat.
4019 SetLayerPropertiesForTesting(child1.get(),
4020 identity_matrix,
4021 gfx::Point3F(),
4022 gfx::PointF(),
4023 gfx::Size(100, 100),
4024 true,
4025 false);
4026 SetLayerPropertiesForTesting(child2.get(),
4027 identity_matrix,
4028 gfx::Point3F(),
4029 gfx::PointF(),
4030 gfx::Size(100, 100),
4031 true,
4032 false);
4034 front_facing_surface->Set3dSortingContextId(1);
4035 back_facing_surface->Set3dSortingContextId(1);
4037 ExecuteCalculateDrawPropertiesWithPropertyTrees(parent.get());
4039 // Verify which render surfaces were created and used.
4040 EXPECT_TRUE(front_facing_surface->render_surface());
4042 // We expect the render surface to have been created, but remain unused.
4043 EXPECT_TRUE(back_facing_surface->render_surface());
4044 EXPECT_NE(back_facing_surface->render_target(),
4045 back_facing_surface); // because it should be culled
4046 EXPECT_FALSE(child1->render_surface());
4047 EXPECT_FALSE(child2->render_surface());
4049 EXPECT_EQ(2u, update_layer_list().size());
4050 EXPECT_TRUE(UpdateLayerListContains(front_facing_surface->id()));
4051 EXPECT_TRUE(UpdateLayerListContains(child1->id()));
4054 TEST_F(LayerTreeHostCommonScalingTest, LayerTransformsInHighDPI) {
4055 // Verify draw and screen space transforms of layers not in a surface.
4056 gfx::Transform identity_matrix;
4058 LayerImpl* parent = root_layer();
4059 SetLayerPropertiesForTesting(parent, identity_matrix, gfx::Point3F(),
4060 gfx::PointF(), gfx::Size(100, 100), false, true,
4061 true);
4063 LayerImpl* child = AddChildToRoot<LayerImpl>();
4064 SetLayerPropertiesForTesting(child, identity_matrix, gfx::Point3F(),
4065 gfx::PointF(2.f, 2.f), gfx::Size(10, 10), false,
4066 true, false);
4068 LayerImpl* child_empty = AddChildToRoot<LayerImpl>();
4069 SetLayerPropertiesForTesting(child_empty, identity_matrix, gfx::Point3F(),
4070 gfx::PointF(2.f, 2.f), gfx::Size(), false, true,
4071 false);
4073 float device_scale_factor = 2.5f;
4074 gfx::Size viewport_size(100, 100);
4075 ExecuteCalculateDrawProperties(parent, device_scale_factor);
4077 EXPECT_IDEAL_SCALE_EQ(device_scale_factor, parent);
4078 EXPECT_IDEAL_SCALE_EQ(device_scale_factor, child);
4079 EXPECT_IDEAL_SCALE_EQ(device_scale_factor, child_empty);
4081 EXPECT_EQ(1u, render_surface_layer_list_impl()->size());
4083 // Verify parent transforms
4084 gfx::Transform expected_parent_transform;
4085 expected_parent_transform.Scale(device_scale_factor, device_scale_factor);
4086 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4087 parent->screen_space_transform());
4088 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4089 parent->draw_transform());
4091 // Verify results of transformed parent rects
4092 gfx::RectF parent_bounds(parent->bounds());
4094 gfx::RectF parent_draw_rect =
4095 MathUtil::MapClippedRect(parent->draw_transform(), parent_bounds);
4096 gfx::RectF parent_screen_space_rect =
4097 MathUtil::MapClippedRect(parent->screen_space_transform(), parent_bounds);
4099 gfx::RectF expected_parent_draw_rect(parent->bounds());
4100 expected_parent_draw_rect.Scale(device_scale_factor);
4101 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_draw_rect);
4102 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_screen_space_rect);
4104 // Verify child and child_empty transforms. They should match.
4105 gfx::Transform expected_child_transform;
4106 expected_child_transform.Scale(device_scale_factor, device_scale_factor);
4107 expected_child_transform.Translate(child->position().x(),
4108 child->position().y());
4109 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4110 child->draw_transform());
4111 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4112 child->screen_space_transform());
4113 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4114 child_empty->draw_transform());
4115 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4116 child_empty->screen_space_transform());
4118 // Verify results of transformed child and child_empty rects. They should
4119 // match.
4120 gfx::RectF child_bounds(child->bounds());
4122 gfx::RectF child_draw_rect =
4123 MathUtil::MapClippedRect(child->draw_transform(), child_bounds);
4124 gfx::RectF child_screen_space_rect =
4125 MathUtil::MapClippedRect(child->screen_space_transform(), child_bounds);
4127 gfx::RectF child_empty_draw_rect =
4128 MathUtil::MapClippedRect(child_empty->draw_transform(), child_bounds);
4129 gfx::RectF child_empty_screen_space_rect = MathUtil::MapClippedRect(
4130 child_empty->screen_space_transform(), child_bounds);
4132 gfx::RectF expected_child_draw_rect(child->position(), child->bounds());
4133 expected_child_draw_rect.Scale(device_scale_factor);
4134 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_draw_rect);
4135 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_screen_space_rect);
4136 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_empty_draw_rect);
4137 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_empty_screen_space_rect);
4140 TEST_F(LayerTreeHostCommonScalingTest, SurfaceLayerTransformsInHighDPI) {
4141 // Verify draw and screen space transforms of layers in a surface.
4142 gfx::Transform identity_matrix;
4143 gfx::Transform perspective_matrix;
4144 perspective_matrix.ApplyPerspectiveDepth(2.0);
4146 gfx::Transform scale_small_matrix;
4147 scale_small_matrix.Scale(SK_MScalar1 / 10.f, SK_MScalar1 / 12.f);
4149 LayerImpl* root = root_layer();
4150 SetLayerPropertiesForTesting(root, identity_matrix, gfx::Point3F(),
4151 gfx::PointF(), gfx::Size(100, 100), false, true,
4152 false);
4153 LayerImpl* parent = AddChildToRoot<LayerImpl>();
4154 SetLayerPropertiesForTesting(parent, identity_matrix, gfx::Point3F(),
4155 gfx::PointF(), gfx::Size(100, 100), false, true,
4156 false);
4158 LayerImpl* perspective_surface = AddChild<LayerImpl>(parent);
4159 SetLayerPropertiesForTesting(perspective_surface,
4160 perspective_matrix * scale_small_matrix,
4161 gfx::Point3F(), gfx::PointF(2.f, 2.f),
4162 gfx::Size(10, 10), false, true, true);
4163 perspective_surface->SetDrawsContent(true);
4165 LayerImpl* scale_surface = AddChild<LayerImpl>(parent);
4166 SetLayerPropertiesForTesting(scale_surface, scale_small_matrix,
4167 gfx::Point3F(), gfx::PointF(2.f, 2.f),
4168 gfx::Size(10, 10), false, true, true);
4169 scale_surface->SetDrawsContent(true);
4171 float device_scale_factor = 2.5f;
4172 float page_scale_factor = 3.f;
4173 ExecuteCalculateDrawProperties(root, device_scale_factor, page_scale_factor,
4174 root);
4176 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4177 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor,
4178 perspective_surface);
4179 // Ideal scale is the max 2d scale component of the combined transform up to
4180 // the nearest render target. Here this includes the layer transform as well
4181 // as the device and page scale factors.
4182 gfx::Transform transform = scale_small_matrix;
4183 transform.Scale(device_scale_factor * page_scale_factor,
4184 device_scale_factor * page_scale_factor);
4185 gfx::Vector2dF scales =
4186 MathUtil::ComputeTransform2dScaleComponents(transform, 0.f);
4187 float max_2d_scale = std::max(scales.x(), scales.y());
4188 EXPECT_IDEAL_SCALE_EQ(max_2d_scale, scale_surface);
4190 // The ideal scale will draw 1:1 with its render target space along
4191 // the larger-scale axis.
4192 gfx::Vector2dF target_space_transform_scales =
4193 MathUtil::ComputeTransform2dScaleComponents(
4194 scale_surface->draw_properties().target_space_transform, 0.f);
4195 EXPECT_FLOAT_EQ(max_2d_scale,
4196 std::max(target_space_transform_scales.x(),
4197 target_space_transform_scales.y()));
4199 EXPECT_EQ(3u, render_surface_layer_list_impl()->size());
4201 gfx::Transform expected_parent_draw_transform;
4202 expected_parent_draw_transform.Scale(device_scale_factor * page_scale_factor,
4203 device_scale_factor * page_scale_factor);
4204 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_draw_transform,
4205 parent->draw_transform());
4207 // The scale for the perspective surface is not known, so it is rendered 1:1
4208 // with the screen, and then scaled during drawing.
4209 gfx::Transform expected_perspective_surface_draw_transform;
4210 expected_perspective_surface_draw_transform.Translate(
4211 device_scale_factor * page_scale_factor *
4212 perspective_surface->position().x(),
4213 device_scale_factor * page_scale_factor *
4214 perspective_surface->position().y());
4215 expected_perspective_surface_draw_transform.PreconcatTransform(
4216 perspective_matrix);
4217 expected_perspective_surface_draw_transform.PreconcatTransform(
4218 scale_small_matrix);
4219 gfx::Transform expected_perspective_surface_layer_draw_transform;
4220 expected_perspective_surface_layer_draw_transform.Scale(
4221 device_scale_factor * page_scale_factor,
4222 device_scale_factor * page_scale_factor);
4223 EXPECT_TRANSFORMATION_MATRIX_EQ(
4224 expected_perspective_surface_draw_transform,
4225 perspective_surface->render_surface()->draw_transform());
4226 EXPECT_TRANSFORMATION_MATRIX_EQ(
4227 expected_perspective_surface_layer_draw_transform,
4228 perspective_surface->draw_transform());
4231 TEST_F(LayerTreeHostCommonScalingTest, SmallIdealScale) {
4232 gfx::Transform parent_scale_matrix;
4233 SkMScalar initial_parent_scale = 1.75;
4234 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4236 gfx::Transform child_scale_matrix;
4237 SkMScalar initial_child_scale = 0.25;
4238 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4240 LayerImpl* root = root_layer();
4241 root->SetBounds(gfx::Size(100, 100));
4243 LayerImpl* parent = AddChildToRoot<LayerImpl>();
4244 SetLayerPropertiesForTesting(parent, parent_scale_matrix, gfx::Point3F(),
4245 gfx::PointF(), gfx::Size(100, 100), false, true,
4246 false);
4248 LayerImpl* child_scale = AddChild<LayerImpl>(parent);
4249 SetLayerPropertiesForTesting(child_scale, child_scale_matrix, gfx::Point3F(),
4250 gfx::PointF(2.f, 2.f), gfx::Size(10, 10), false,
4251 true, false);
4253 float device_scale_factor = 2.5f;
4254 float page_scale_factor = 0.01f;
4257 ExecuteCalculateDrawProperties(root, device_scale_factor, page_scale_factor,
4258 root);
4260 // The ideal scale is able to go below 1.
4261 float expected_ideal_scale =
4262 device_scale_factor * page_scale_factor * initial_parent_scale;
4263 EXPECT_LT(expected_ideal_scale, 1.f);
4264 EXPECT_IDEAL_SCALE_EQ(expected_ideal_scale, parent);
4266 expected_ideal_scale = device_scale_factor * page_scale_factor *
4267 initial_parent_scale * initial_child_scale;
4268 EXPECT_LT(expected_ideal_scale, 1.f);
4269 EXPECT_IDEAL_SCALE_EQ(expected_ideal_scale, child_scale);
4273 TEST_F(LayerTreeHostCommonScalingTest, IdealScaleForAnimatingLayer) {
4274 gfx::Transform parent_scale_matrix;
4275 SkMScalar initial_parent_scale = 1.75;
4276 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4278 gfx::Transform child_scale_matrix;
4279 SkMScalar initial_child_scale = 1.25;
4280 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4282 LayerImpl* root = root_layer();
4283 root->SetBounds(gfx::Size(100, 100));
4285 LayerImpl* parent = AddChildToRoot<LayerImpl>();
4286 SetLayerPropertiesForTesting(parent, parent_scale_matrix, gfx::Point3F(),
4287 gfx::PointF(), gfx::Size(100, 100), false, true,
4288 false);
4290 LayerImpl* child_scale = AddChild<LayerImpl>(parent);
4291 SetLayerPropertiesForTesting(child_scale, child_scale_matrix, gfx::Point3F(),
4292 gfx::PointF(2.f, 2.f), gfx::Size(10, 10), false,
4293 true, false);
4296 ExecuteCalculateDrawProperties(root);
4298 EXPECT_IDEAL_SCALE_EQ(initial_parent_scale, parent);
4299 // Animating layers compute ideal scale in the same way as when
4300 // they are static.
4301 EXPECT_IDEAL_SCALE_EQ(initial_child_scale * initial_parent_scale,
4302 child_scale);
4306 TEST_F(LayerTreeHostCommonTest, RenderSurfaceTransformsInHighDPI) {
4307 gfx::Transform identity_matrix;
4309 LayerImpl* parent = root_layer();
4310 parent->SetDrawsContent(true);
4311 SetLayerPropertiesForTesting(parent, identity_matrix, gfx::Point3F(),
4312 gfx::PointF(), gfx::Size(30, 30), false, true,
4313 true);
4315 LayerImpl* child = AddChildToRoot<LayerImpl>();
4316 child->SetDrawsContent(true);
4317 SetLayerPropertiesForTesting(child, identity_matrix, gfx::Point3F(),
4318 gfx::PointF(2.f, 2.f), gfx::Size(10, 10), false,
4319 true, true);
4321 gfx::Transform replica_transform;
4322 replica_transform.Scale(1.0, -1.0);
4324 scoped_ptr<LayerImpl> replica =
4325 LayerImpl::Create(host_impl()->active_tree(), 7);
4326 SetLayerPropertiesForTesting(replica.get(), replica_transform, gfx::Point3F(),
4327 gfx::PointF(2.f, 2.f), gfx::Size(10, 10), false,
4328 true, false);
4329 child->SetReplicaLayer(replica.Pass());
4331 // This layer should end up in the same surface as child, with the same draw
4332 // and screen space transforms.
4333 LayerImpl* duplicate_child_non_owner = AddChild<LayerImpl>(child);
4334 duplicate_child_non_owner->SetDrawsContent(true);
4335 SetLayerPropertiesForTesting(duplicate_child_non_owner, identity_matrix,
4336 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
4337 false, true, false);
4339 float device_scale_factor = 1.5f;
4340 ExecuteCalculateDrawProperties(parent, device_scale_factor);
4342 // We should have two render surfaces. The root's render surface and child's
4343 // render surface (it needs one because it has a replica layer).
4344 EXPECT_EQ(2u, render_surface_layer_list_impl()->size());
4346 gfx::Transform expected_parent_transform;
4347 expected_parent_transform.Scale(device_scale_factor, device_scale_factor);
4348 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4349 parent->screen_space_transform());
4350 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4351 parent->draw_transform());
4353 gfx::Transform expected_draw_transform;
4354 expected_draw_transform.Scale(device_scale_factor, device_scale_factor);
4355 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_draw_transform,
4356 child->draw_transform());
4358 gfx::Transform expected_screen_space_transform;
4359 expected_screen_space_transform.Scale(device_scale_factor,
4360 device_scale_factor);
4361 expected_screen_space_transform.Translate(child->position().x(),
4362 child->position().y());
4363 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_screen_space_transform,
4364 child->screen_space_transform());
4366 gfx::Transform expected_duplicate_child_draw_transform =
4367 child->draw_transform();
4368 EXPECT_TRANSFORMATION_MATRIX_EQ(child->draw_transform(),
4369 duplicate_child_non_owner->draw_transform());
4370 EXPECT_TRANSFORMATION_MATRIX_EQ(
4371 child->screen_space_transform(),
4372 duplicate_child_non_owner->screen_space_transform());
4373 EXPECT_EQ(child->drawable_content_rect(),
4374 duplicate_child_non_owner->drawable_content_rect());
4375 EXPECT_EQ(child->bounds(), duplicate_child_non_owner->bounds());
4377 gfx::Transform expected_render_surface_draw_transform;
4378 expected_render_surface_draw_transform.Translate(
4379 device_scale_factor * child->position().x(),
4380 device_scale_factor * child->position().y());
4381 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_render_surface_draw_transform,
4382 child->render_surface()->draw_transform());
4384 gfx::Transform expected_surface_draw_transform;
4385 expected_surface_draw_transform.Translate(device_scale_factor * 2.f,
4386 device_scale_factor * 2.f);
4387 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_surface_draw_transform,
4388 child->render_surface()->draw_transform());
4390 gfx::Transform expected_surface_screen_space_transform;
4391 expected_surface_screen_space_transform.Translate(device_scale_factor * 2.f,
4392 device_scale_factor * 2.f);
4393 EXPECT_TRANSFORMATION_MATRIX_EQ(
4394 expected_surface_screen_space_transform,
4395 child->render_surface()->screen_space_transform());
4397 gfx::Transform expected_replica_draw_transform;
4398 expected_replica_draw_transform.matrix().set(1, 1, -1.0);
4399 expected_replica_draw_transform.matrix().set(0, 3, 6.0);
4400 expected_replica_draw_transform.matrix().set(1, 3, 6.0);
4401 EXPECT_TRANSFORMATION_MATRIX_EQ(
4402 expected_replica_draw_transform,
4403 child->render_surface()->replica_draw_transform());
4405 gfx::Transform expected_replica_screen_space_transform;
4406 expected_replica_screen_space_transform.matrix().set(1, 1, -1.0);
4407 expected_replica_screen_space_transform.matrix().set(0, 3, 6.0);
4408 expected_replica_screen_space_transform.matrix().set(1, 3, 6.0);
4409 EXPECT_TRANSFORMATION_MATRIX_EQ(
4410 expected_replica_screen_space_transform,
4411 child->render_surface()->replica_screen_space_transform());
4412 EXPECT_TRANSFORMATION_MATRIX_EQ(
4413 expected_replica_screen_space_transform,
4414 child->render_surface()->replica_screen_space_transform());
4417 TEST_F(LayerTreeHostCommonTest,
4418 RenderSurfaceTransformsInHighDPIAccurateScaleZeroPosition) {
4419 gfx::Transform identity_matrix;
4421 LayerImpl* parent = root_layer();
4422 parent->SetDrawsContent(true);
4423 SetLayerPropertiesForTesting(parent, identity_matrix, gfx::Point3F(),
4424 gfx::PointF(), gfx::Size(33, 31), false, true,
4425 true);
4427 LayerImpl* child = AddChildToRoot<LayerImpl>();
4428 child->SetDrawsContent(true);
4429 SetLayerPropertiesForTesting(child, identity_matrix, gfx::Point3F(),
4430 gfx::PointF(), gfx::Size(13, 11), false, true,
4431 true);
4433 gfx::Transform replica_transform;
4434 replica_transform.Scale(1.0, -1.0);
4435 scoped_ptr<LayerImpl> replica =
4436 LayerImpl::Create(host_impl()->active_tree(), 7);
4437 SetLayerPropertiesForTesting(replica.get(), replica_transform, gfx::Point3F(),
4438 gfx::PointF(), gfx::Size(13, 11), false, true,
4439 false);
4440 child->SetReplicaLayer(replica.Pass());
4442 float device_scale_factor = 1.7f;
4443 ExecuteCalculateDrawProperties(parent, device_scale_factor);
4445 // We should have two render surfaces. The root's render surface and child's
4446 // render surface (it needs one because it has a replica layer).
4447 EXPECT_EQ(2u, render_surface_layer_list_impl()->size());
4449 gfx::Transform identity_transform;
4450 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
4451 child->render_surface()->draw_transform());
4452 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
4453 child->render_surface()->draw_transform());
4454 EXPECT_TRANSFORMATION_MATRIX_EQ(
4455 identity_transform, child->render_surface()->screen_space_transform());
4457 gfx::Transform expected_replica_draw_transform;
4458 expected_replica_draw_transform.matrix().set(1, 1, -1.0);
4459 EXPECT_TRANSFORMATION_MATRIX_EQ(
4460 expected_replica_draw_transform,
4461 child->render_surface()->replica_draw_transform());
4463 gfx::Transform expected_replica_screen_space_transform;
4464 expected_replica_screen_space_transform.matrix().set(1, 1, -1.0);
4465 EXPECT_TRANSFORMATION_MATRIX_EQ(
4466 expected_replica_screen_space_transform,
4467 child->render_surface()->replica_screen_space_transform());
4470 TEST_F(LayerTreeHostCommonTest, SubtreeSearch) {
4471 scoped_refptr<Layer> root = Layer::Create(layer_settings());
4472 scoped_refptr<Layer> child = Layer::Create(layer_settings());
4473 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
4474 scoped_refptr<Layer> mask_layer = Layer::Create(layer_settings());
4475 scoped_refptr<Layer> replica_layer = Layer::Create(layer_settings());
4477 grand_child->SetReplicaLayer(replica_layer.get());
4478 child->AddChild(grand_child.get());
4479 child->SetMaskLayer(mask_layer.get());
4480 root->AddChild(child.get());
4482 host()->SetRootLayer(root);
4484 int nonexistent_id = -1;
4485 EXPECT_EQ(root.get(),
4486 LayerTreeHostCommon::FindLayerInSubtree(root.get(), root->id()));
4487 EXPECT_EQ(child.get(),
4488 LayerTreeHostCommon::FindLayerInSubtree(root.get(), child->id()));
4489 EXPECT_EQ(
4490 grand_child.get(),
4491 LayerTreeHostCommon::FindLayerInSubtree(root.get(), grand_child->id()));
4492 EXPECT_EQ(
4493 mask_layer.get(),
4494 LayerTreeHostCommon::FindLayerInSubtree(root.get(), mask_layer->id()));
4495 EXPECT_EQ(
4496 replica_layer.get(),
4497 LayerTreeHostCommon::FindLayerInSubtree(root.get(), replica_layer->id()));
4498 EXPECT_EQ(
4499 0, LayerTreeHostCommon::FindLayerInSubtree(root.get(), nonexistent_id));
4502 TEST_F(LayerTreeHostCommonTest, TransparentChildRenderSurfaceCreation) {
4503 scoped_refptr<Layer> root = Layer::Create(layer_settings());
4504 scoped_refptr<Layer> child = Layer::Create(layer_settings());
4505 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
4506 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
4508 const gfx::Transform identity_matrix;
4509 SetLayerPropertiesForTesting(root.get(),
4510 identity_matrix,
4511 gfx::Point3F(),
4512 gfx::PointF(),
4513 gfx::Size(100, 100),
4514 true,
4515 false);
4516 SetLayerPropertiesForTesting(child.get(),
4517 identity_matrix,
4518 gfx::Point3F(),
4519 gfx::PointF(),
4520 gfx::Size(10, 10),
4521 true,
4522 false);
4523 SetLayerPropertiesForTesting(grand_child.get(),
4524 identity_matrix,
4525 gfx::Point3F(),
4526 gfx::PointF(),
4527 gfx::Size(10, 10),
4528 true,
4529 false);
4531 root->AddChild(child);
4532 child->AddChild(grand_child);
4533 child->SetOpacity(0.5f);
4535 host()->SetRootLayer(root);
4537 ExecuteCalculateDrawProperties(root.get());
4539 EXPECT_FALSE(child->render_surface());
4542 TEST_F(LayerTreeHostCommonTest, OpacityAnimatingOnPendingTree) {
4543 FakeImplProxy proxy;
4544 TestSharedBitmapManager shared_bitmap_manager;
4545 TestTaskGraphRunner task_graph_runner;
4546 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
4547 &task_graph_runner);
4548 host_impl.CreatePendingTree();
4549 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
4551 const gfx::Transform identity_matrix;
4552 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
4553 gfx::PointF(), gfx::Size(100, 100), true, false,
4554 false);
4555 root->SetDrawsContent(true);
4557 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
4558 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
4559 gfx::PointF(), gfx::Size(50, 50), true, false,
4560 false);
4561 child->SetDrawsContent(true);
4562 child->SetOpacity(0.0f);
4564 // Add opacity animation.
4565 AddOpacityTransitionToController(
4566 child->layer_animation_controller(), 10.0, 0.0f, 1.0f, false);
4568 root->AddChild(child.Pass());
4569 root->SetHasRenderSurface(true);
4571 LayerImplList render_surface_layer_list;
4572 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
4573 root.get(), root->bounds(), &render_surface_layer_list);
4574 inputs.can_adjust_raster_scales = true;
4575 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4577 // We should have one render surface and two layers. The child
4578 // layer should be included even though it is transparent.
4579 ASSERT_EQ(1u, render_surface_layer_list.size());
4580 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
4583 using LCDTextTestParam = std::tr1::tuple<bool, bool, bool>;
4584 class LCDTextTest : public LayerTreeHostCommonTestBase,
4585 public testing::TestWithParam<LCDTextTestParam> {
4586 public:
4587 LCDTextTest()
4588 : LayerTreeHostCommonTestBase(LayerTreeSettings()),
4589 host_impl_(&proxy_, &shared_bitmap_manager_, &task_graph_runner_),
4590 root_(nullptr),
4591 child_(nullptr),
4592 grand_child_(nullptr) {}
4594 protected:
4595 void SetUp() override {
4596 can_use_lcd_text_ = std::tr1::get<0>(GetParam());
4597 layers_always_allowed_lcd_text_ = std::tr1::get<1>(GetParam());
4599 scoped_ptr<LayerImpl> root_ptr =
4600 LayerImpl::Create(host_impl_.active_tree(), 1);
4601 scoped_ptr<LayerImpl> child_ptr =
4602 LayerImpl::Create(host_impl_.active_tree(), 2);
4603 scoped_ptr<LayerImpl> grand_child_ptr =
4604 LayerImpl::Create(host_impl_.active_tree(), 3);
4606 // Stash raw pointers to look at later.
4607 root_ = root_ptr.get();
4608 child_ = child_ptr.get();
4609 grand_child_ = grand_child_ptr.get();
4611 child_->AddChild(grand_child_ptr.Pass());
4612 root_->AddChild(child_ptr.Pass());
4613 host_impl_.active_tree()->SetRootLayer(root_ptr.Pass());
4615 root_->SetContentsOpaque(true);
4616 child_->SetContentsOpaque(true);
4617 grand_child_->SetContentsOpaque(true);
4619 root_->SetDrawsContent(true);
4620 child_->SetDrawsContent(true);
4621 grand_child_->SetDrawsContent(true);
4623 gfx::Transform identity_matrix;
4624 SetLayerPropertiesForTesting(root_, identity_matrix, gfx::Point3F(),
4625 gfx::PointF(), gfx::Size(1, 1), true, false,
4626 true);
4627 SetLayerPropertiesForTesting(child_, identity_matrix, gfx::Point3F(),
4628 gfx::PointF(), gfx::Size(1, 1), true, false,
4629 std::tr1::get<2>(GetParam()));
4630 SetLayerPropertiesForTesting(grand_child_, identity_matrix, gfx::Point3F(),
4631 gfx::PointF(), gfx::Size(1, 1), true, false,
4632 false);
4635 bool can_use_lcd_text_;
4636 bool layers_always_allowed_lcd_text_;
4638 FakeImplProxy proxy_;
4639 TestSharedBitmapManager shared_bitmap_manager_;
4640 TestTaskGraphRunner task_graph_runner_;
4641 FakeLayerTreeHostImpl host_impl_;
4643 LayerImpl* root_;
4644 LayerImpl* child_;
4645 LayerImpl* grand_child_;
4648 TEST_P(LCDTextTest, CanUseLCDText) {
4649 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
4650 bool expect_not_lcd_text = layers_always_allowed_lcd_text_;
4652 // Case 1: Identity transform.
4653 gfx::Transform identity_matrix;
4654 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4655 layers_always_allowed_lcd_text_);
4656 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4657 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
4658 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
4660 // Case 2: Integral translation.
4661 gfx::Transform integral_translation;
4662 integral_translation.Translate(1.0, 2.0);
4663 child_->SetTransform(integral_translation);
4664 child_->layer_tree_impl()->property_trees()->needs_rebuild = true;
4665 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4666 layers_always_allowed_lcd_text_);
4667 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4668 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
4669 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
4671 // Case 3: Non-integral translation.
4672 gfx::Transform non_integral_translation;
4673 non_integral_translation.Translate(1.5, 2.5);
4674 child_->SetTransform(non_integral_translation);
4675 child_->layer_tree_impl()->property_trees()->needs_rebuild = true;
4676 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4677 layers_always_allowed_lcd_text_);
4678 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4679 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
4680 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
4682 // Case 4: Rotation.
4683 gfx::Transform rotation;
4684 rotation.Rotate(10.0);
4685 child_->SetTransform(rotation);
4686 child_->layer_tree_impl()->property_trees()->needs_rebuild = true;
4687 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4688 layers_always_allowed_lcd_text_);
4689 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4690 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
4691 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
4693 // Case 5: Scale.
4694 gfx::Transform scale;
4695 scale.Scale(2.0, 2.0);
4696 child_->SetTransform(scale);
4697 child_->layer_tree_impl()->property_trees()->needs_rebuild = true;
4698 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4699 layers_always_allowed_lcd_text_);
4700 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4701 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
4702 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
4704 // Case 6: Skew.
4705 gfx::Transform skew;
4706 skew.SkewX(10.0);
4707 child_->SetTransform(skew);
4708 child_->layer_tree_impl()->property_trees()->needs_rebuild = true;
4709 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4710 layers_always_allowed_lcd_text_);
4711 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4712 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
4713 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
4715 // Case 7: Translucent.
4716 child_->SetTransform(identity_matrix);
4717 child_->layer_tree_impl()->property_trees()->needs_rebuild = true;
4718 child_->SetOpacity(0.5f);
4719 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4720 layers_always_allowed_lcd_text_);
4721 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4722 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
4723 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
4725 // Case 8: Sanity check: restore transform and opacity.
4726 child_->SetTransform(identity_matrix);
4727 child_->layer_tree_impl()->property_trees()->needs_rebuild = true;
4728 child_->SetOpacity(1.f);
4729 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4730 layers_always_allowed_lcd_text_);
4731 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4732 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
4733 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
4735 // Case 9: Non-opaque content.
4736 child_->SetContentsOpaque(false);
4737 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4738 layers_always_allowed_lcd_text_);
4739 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4740 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
4741 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
4743 // Case 10: Sanity check: restore content opaqueness.
4744 child_->SetContentsOpaque(true);
4745 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4746 layers_always_allowed_lcd_text_);
4747 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4748 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
4749 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
4752 TEST_P(LCDTextTest, CanUseLCDTextWithAnimation) {
4753 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
4754 bool expect_not_lcd_text = layers_always_allowed_lcd_text_;
4756 // Sanity check: Make sure can_use_lcd_text_ is set on each node.
4757 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4758 layers_always_allowed_lcd_text_);
4759 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4760 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
4761 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
4763 // Add opacity animation.
4764 child_->SetOpacity(0.9f);
4765 child_->layer_tree_impl()->property_trees()->needs_rebuild = true;
4766 AddOpacityTransitionToController(
4767 child_->layer_animation_controller(), 10.0, 0.9f, 0.1f, false);
4769 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4770 layers_always_allowed_lcd_text_);
4771 // Text LCD should be adjusted while animation is active.
4772 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4773 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
4774 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
4777 TEST_P(LCDTextTest, CanUseLCDTextWithAnimationContentsOpaque) {
4778 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
4779 bool expect_not_lcd_text = layers_always_allowed_lcd_text_;
4781 // Sanity check: Make sure can_use_lcd_text_ is set on each node.
4782 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4783 layers_always_allowed_lcd_text_);
4784 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4785 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
4786 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
4788 // Mark contents non-opaque within the first animation frame.
4789 child_->SetContentsOpaque(false);
4790 AddOpacityTransitionToController(child_->layer_animation_controller(), 10.0,
4791 0.9f, 0.1f, false);
4793 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
4794 layers_always_allowed_lcd_text_);
4795 // LCD text should be disabled for non-opaque layers even during animations.
4796 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
4797 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
4798 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
4801 INSTANTIATE_TEST_CASE_P(LayerTreeHostCommonTest,
4802 LCDTextTest,
4803 testing::Combine(testing::Bool(),
4804 testing::Bool(),
4805 testing::Bool()));
4807 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_SingleLayerImpl) {
4808 FakeImplProxy proxy;
4809 TestSharedBitmapManager shared_bitmap_manager;
4810 TestTaskGraphRunner task_graph_runner;
4811 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
4812 &task_graph_runner);
4813 host_impl.CreatePendingTree();
4814 const gfx::Transform identity_matrix;
4816 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
4817 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
4818 gfx::PointF(), gfx::Size(50, 50), true, false,
4819 false);
4820 root->SetDrawsContent(true);
4822 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
4823 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
4824 gfx::PointF(), gfx::Size(40, 40), true, false,
4825 false);
4826 child->SetDrawsContent(true);
4828 scoped_ptr<LayerImpl> grand_child =
4829 LayerImpl::Create(host_impl.pending_tree(), 3);
4830 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
4831 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
4832 true, false, false);
4833 grand_child->SetDrawsContent(true);
4834 grand_child->SetHideLayerAndSubtree(true);
4836 child->AddChild(grand_child.Pass());
4837 root->AddChild(child.Pass());
4838 root->SetHasRenderSurface(true);
4840 LayerImplList render_surface_layer_list;
4841 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
4842 root.get(), root->bounds(), &render_surface_layer_list);
4843 inputs.can_adjust_raster_scales = true;
4844 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4846 // We should have one render surface and two layers. The grand child has
4847 // hidden itself.
4848 ASSERT_EQ(1u, render_surface_layer_list.size());
4849 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
4850 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id());
4851 EXPECT_EQ(2, root->render_surface()->layer_list().at(1)->id());
4854 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_TwoLayersImpl) {
4855 FakeImplProxy proxy;
4856 TestSharedBitmapManager shared_bitmap_manager;
4857 TestTaskGraphRunner task_graph_runner;
4858 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
4859 &task_graph_runner);
4860 host_impl.CreatePendingTree();
4861 const gfx::Transform identity_matrix;
4863 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
4864 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
4865 gfx::PointF(), gfx::Size(50, 50), true, false,
4866 true);
4867 root->SetDrawsContent(true);
4869 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
4870 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
4871 gfx::PointF(), gfx::Size(40, 40), true, false,
4872 false);
4873 child->SetDrawsContent(true);
4874 child->SetHideLayerAndSubtree(true);
4876 scoped_ptr<LayerImpl> grand_child =
4877 LayerImpl::Create(host_impl.pending_tree(), 3);
4878 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
4879 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
4880 true, false, false);
4881 grand_child->SetDrawsContent(true);
4883 child->AddChild(grand_child.Pass());
4884 root->AddChild(child.Pass());
4886 LayerImplList render_surface_layer_list;
4887 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
4888 root.get(), root->bounds(), &render_surface_layer_list);
4889 inputs.can_adjust_raster_scales = true;
4890 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4892 // We should have one render surface and one layers. The child has
4893 // hidden itself and the grand child.
4894 ASSERT_EQ(1u, render_surface_layer_list.size());
4895 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
4896 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id());
4899 void EmptyCopyOutputCallback(scoped_ptr<CopyOutputResult> result) {}
4901 TEST_F(LayerTreeHostCommonTest, SubtreeHiddenWithCopyRequest) {
4902 FakeImplProxy proxy;
4903 TestSharedBitmapManager shared_bitmap_manager;
4904 TestTaskGraphRunner task_graph_runner;
4905 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
4906 &task_graph_runner);
4907 host_impl.CreatePendingTree();
4908 const gfx::Transform identity_matrix;
4910 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
4911 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
4912 gfx::PointF(), gfx::Size(50, 50), true, false,
4913 true);
4914 root->SetDrawsContent(true);
4916 scoped_ptr<LayerImpl> copy_grand_parent =
4917 LayerImpl::Create(host_impl.pending_tree(), 2);
4918 SetLayerPropertiesForTesting(copy_grand_parent.get(), identity_matrix,
4919 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
4920 true, false, false);
4921 copy_grand_parent->SetDrawsContent(true);
4922 LayerImpl* copy_grand_parent_layer = copy_grand_parent.get();
4924 scoped_ptr<LayerImpl> copy_parent =
4925 LayerImpl::Create(host_impl.pending_tree(), 3);
4926 SetLayerPropertiesForTesting(copy_parent.get(), identity_matrix,
4927 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
4928 true, false, true);
4929 copy_parent->SetDrawsContent(true);
4930 LayerImpl* copy_parent_layer = copy_parent.get();
4932 scoped_ptr<LayerImpl> copy_request =
4933 LayerImpl::Create(host_impl.pending_tree(), 4);
4934 SetLayerPropertiesForTesting(copy_request.get(), identity_matrix,
4935 gfx::Point3F(), gfx::PointF(), gfx::Size(20, 20),
4936 true, false, true);
4937 copy_request->SetDrawsContent(true);
4938 LayerImpl* copy_layer = copy_request.get();
4940 scoped_ptr<LayerImpl> copy_child =
4941 LayerImpl::Create(host_impl.pending_tree(), 5);
4942 SetLayerPropertiesForTesting(copy_child.get(), identity_matrix,
4943 gfx::Point3F(), gfx::PointF(), gfx::Size(20, 20),
4944 true, false, false);
4945 copy_child->SetDrawsContent(true);
4946 LayerImpl* copy_child_layer = copy_child.get();
4948 scoped_ptr<LayerImpl> copy_grand_parent_sibling_before =
4949 LayerImpl::Create(host_impl.pending_tree(), 6);
4950 SetLayerPropertiesForTesting(copy_grand_parent_sibling_before.get(),
4951 identity_matrix, gfx::Point3F(), gfx::PointF(),
4952 gfx::Size(40, 40), true, false, false);
4953 copy_grand_parent_sibling_before->SetDrawsContent(true);
4954 LayerImpl* copy_grand_parent_sibling_before_layer =
4955 copy_grand_parent_sibling_before.get();
4957 scoped_ptr<LayerImpl> copy_grand_parent_sibling_after =
4958 LayerImpl::Create(host_impl.pending_tree(), 7);
4959 SetLayerPropertiesForTesting(copy_grand_parent_sibling_after.get(),
4960 identity_matrix, gfx::Point3F(), gfx::PointF(),
4961 gfx::Size(40, 40), true, false, false);
4962 copy_grand_parent_sibling_after->SetDrawsContent(true);
4963 LayerImpl* copy_grand_parent_sibling_after_layer =
4964 copy_grand_parent_sibling_after.get();
4966 copy_request->AddChild(copy_child.Pass());
4967 copy_parent->AddChild(copy_request.Pass());
4968 copy_grand_parent->AddChild(copy_parent.Pass());
4969 root->AddChild(copy_grand_parent_sibling_before.Pass());
4970 root->AddChild(copy_grand_parent.Pass());
4971 root->AddChild(copy_grand_parent_sibling_after.Pass());
4973 // Hide the copy_grand_parent and its subtree. But make a copy request in that
4974 // hidden subtree on copy_layer.
4975 copy_grand_parent_layer->SetHideLayerAndSubtree(true);
4976 copy_grand_parent_sibling_before_layer->SetHideLayerAndSubtree(true);
4977 copy_grand_parent_sibling_after_layer->SetHideLayerAndSubtree(true);
4979 ScopedPtrVector<CopyOutputRequest> copy_requests;
4980 copy_requests.push_back(
4981 CopyOutputRequest::CreateRequest(base::Bind(&EmptyCopyOutputCallback)));
4982 copy_layer->PassCopyRequests(&copy_requests);
4983 EXPECT_TRUE(copy_layer->HasCopyRequest());
4985 LayerImplList render_surface_layer_list;
4986 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
4987 root.get(), root->bounds(), &render_surface_layer_list);
4988 inputs.can_adjust_raster_scales = true;
4989 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4991 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
4992 EXPECT_TRUE(copy_grand_parent_layer->draw_properties()
4993 .layer_or_descendant_has_copy_request);
4994 EXPECT_TRUE(copy_parent_layer->draw_properties()
4995 .layer_or_descendant_has_copy_request);
4996 EXPECT_TRUE(
4997 copy_layer->draw_properties().layer_or_descendant_has_copy_request);
4998 EXPECT_FALSE(
4999 copy_child_layer->draw_properties().layer_or_descendant_has_copy_request);
5000 EXPECT_FALSE(copy_grand_parent_sibling_before_layer->draw_properties()
5001 .layer_or_descendant_has_copy_request);
5002 EXPECT_FALSE(copy_grand_parent_sibling_after_layer->draw_properties()
5003 .layer_or_descendant_has_copy_request);
5005 // We should have three render surfaces, one for the root, one for the parent
5006 // since it owns a surface, and one for the copy_layer.
5007 ASSERT_EQ(3u, render_surface_layer_list.size());
5008 EXPECT_EQ(root->id(), render_surface_layer_list.at(0)->id());
5009 EXPECT_EQ(copy_parent_layer->id(), render_surface_layer_list.at(1)->id());
5010 EXPECT_EQ(copy_layer->id(), render_surface_layer_list.at(2)->id());
5012 // The root render surface should have 2 contributing layers. The
5013 // copy_grand_parent is hidden along with its siblings, but the copy_parent
5014 // will appear since something in its subtree needs to be drawn for a copy
5015 // request.
5016 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
5017 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
5018 EXPECT_EQ(copy_parent_layer->id(),
5019 root->render_surface()->layer_list().at(1)->id());
5021 // Nothing actually draws into the copy parent, so only the copy_layer will
5022 // appear in its list, since it needs to be drawn for the copy request.
5023 ASSERT_EQ(1u, copy_parent_layer->render_surface()->layer_list().size());
5024 EXPECT_EQ(copy_layer->id(),
5025 copy_parent_layer->render_surface()->layer_list().at(0)->id());
5027 // The copy_layer's render surface should have two contributing layers.
5028 ASSERT_EQ(2u, copy_layer->render_surface()->layer_list().size());
5029 EXPECT_EQ(copy_layer->id(),
5030 copy_layer->render_surface()->layer_list().at(0)->id());
5031 EXPECT_EQ(copy_child_layer->id(),
5032 copy_layer->render_surface()->layer_list().at(1)->id());
5035 TEST_F(LayerTreeHostCommonTest, ClippedOutCopyRequest) {
5036 FakeImplProxy proxy;
5037 TestSharedBitmapManager shared_bitmap_manager;
5038 TestTaskGraphRunner task_graph_runner;
5039 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
5040 &task_graph_runner);
5041 host_impl.CreatePendingTree();
5042 const gfx::Transform identity_matrix;
5044 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
5045 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
5046 gfx::PointF(), gfx::Size(50, 50), true, false,
5047 true);
5048 root->SetDrawsContent(true);
5050 scoped_ptr<LayerImpl> copy_parent =
5051 LayerImpl::Create(host_impl.pending_tree(), 2);
5052 SetLayerPropertiesForTesting(copy_parent.get(), identity_matrix,
5053 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
5054 false, false);
5055 copy_parent->SetDrawsContent(true);
5056 copy_parent->SetMasksToBounds(true);
5058 scoped_ptr<LayerImpl> copy_layer =
5059 LayerImpl::Create(host_impl.pending_tree(), 3);
5060 SetLayerPropertiesForTesting(copy_layer.get(), identity_matrix,
5061 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
5062 true, false, true);
5063 copy_layer->SetDrawsContent(true);
5065 scoped_ptr<LayerImpl> copy_child =
5066 LayerImpl::Create(host_impl.pending_tree(), 4);
5067 SetLayerPropertiesForTesting(copy_child.get(), identity_matrix,
5068 gfx::Point3F(), gfx::PointF(), gfx::Size(20, 20),
5069 true, false, false);
5070 copy_child->SetDrawsContent(true);
5072 ScopedPtrVector<CopyOutputRequest> copy_requests;
5073 copy_requests.push_back(
5074 CopyOutputRequest::CreateRequest(base::Bind(&EmptyCopyOutputCallback)));
5075 copy_layer->PassCopyRequests(&copy_requests);
5076 EXPECT_TRUE(copy_layer->HasCopyRequest());
5078 copy_layer->AddChild(copy_child.Pass());
5079 copy_parent->AddChild(copy_layer.Pass());
5080 root->AddChild(copy_parent.Pass());
5082 LayerImplList render_surface_layer_list;
5083 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
5084 root.get(), root->bounds(), &render_surface_layer_list);
5085 inputs.can_adjust_raster_scales = true;
5086 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5088 // We should have one render surface, as the others are clipped out.
5089 ASSERT_EQ(1u, render_surface_layer_list.size());
5090 EXPECT_EQ(root->id(), render_surface_layer_list.at(0)->id());
5092 // The root render surface should only have 1 contributing layer, since the
5093 // other layers are empty/clipped away.
5094 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
5095 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
5098 TEST_F(LayerTreeHostCommonTest, VisibleContentRectInsideSurface) {
5099 FakeImplProxy proxy;
5100 TestSharedBitmapManager shared_bitmap_manager;
5101 TestTaskGraphRunner task_graph_runner;
5102 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
5103 &task_graph_runner);
5104 host_impl.CreatePendingTree();
5105 const gfx::Transform identity_matrix;
5107 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5108 SetLayerPropertiesForTesting(root.get(),
5109 identity_matrix,
5110 gfx::Point3F(),
5111 gfx::PointF(),
5112 gfx::Size(50, 50),
5113 true,
5114 false);
5115 root->SetIsDrawable(true);
5117 // The surface is moved slightly outside of the viewport.
5118 scoped_refptr<Layer> surface = Layer::Create(layer_settings());
5119 SetLayerPropertiesForTesting(surface.get(),
5120 identity_matrix,
5121 gfx::Point3F(),
5122 gfx::PointF(-10, -20),
5123 gfx::Size(),
5124 true,
5125 false);
5126 surface->SetForceRenderSurface(true);
5128 scoped_refptr<Layer> surface_child = Layer::Create(layer_settings());
5129 SetLayerPropertiesForTesting(surface_child.get(),
5130 identity_matrix,
5131 gfx::Point3F(),
5132 gfx::PointF(),
5133 gfx::Size(50, 50),
5134 true,
5135 false);
5136 surface_child->SetIsDrawable(true);
5138 surface->AddChild(surface_child);
5139 root->AddChild(surface);
5141 host()->SetRootLayer(root);
5143 RenderSurfaceLayerList render_surface_layer_list;
5144 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5145 root.get(), root->bounds(), &render_surface_layer_list);
5146 inputs.can_adjust_raster_scales = true;
5147 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5149 // The visible_layer_rect for the |surface_child| should not be clipped by
5150 // the viewport.
5151 EXPECT_EQ(gfx::Rect(50, 50).ToString(),
5152 surface_child->visible_layer_rect().ToString());
5155 TEST_F(LayerTreeHostCommonTest, TransformedClipParent) {
5156 // Ensure that a transform between the layer and its render surface is not a
5157 // problem. Constructs the following layer tree.
5159 // root (a render surface)
5160 // + render_surface
5161 // + clip_parent (scaled)
5162 // + intervening_clipping_layer
5163 // + clip_child
5165 // The render surface should be resized correctly and the clip child should
5166 // inherit the right clip rect.
5167 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5168 scoped_refptr<Layer> render_surface = Layer::Create(layer_settings());
5169 scoped_refptr<Layer> clip_parent = Layer::Create(layer_settings());
5170 scoped_refptr<Layer> intervening = Layer::Create(layer_settings());
5171 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
5172 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5174 root->AddChild(render_surface);
5175 render_surface->AddChild(clip_parent);
5176 clip_parent->AddChild(intervening);
5177 intervening->AddChild(clip_child);
5179 clip_child->SetClipParent(clip_parent.get());
5181 intervening->SetMasksToBounds(true);
5182 clip_parent->SetMasksToBounds(true);
5184 render_surface->SetForceRenderSurface(true);
5186 gfx::Transform scale_transform;
5187 scale_transform.Scale(2, 2);
5189 gfx::Transform identity_transform;
5191 SetLayerPropertiesForTesting(root.get(),
5192 identity_transform,
5193 gfx::Point3F(),
5194 gfx::PointF(),
5195 gfx::Size(50, 50),
5196 true,
5197 false);
5198 SetLayerPropertiesForTesting(render_surface.get(),
5199 identity_transform,
5200 gfx::Point3F(),
5201 gfx::PointF(),
5202 gfx::Size(10, 10),
5203 true,
5204 false);
5205 SetLayerPropertiesForTesting(clip_parent.get(),
5206 scale_transform,
5207 gfx::Point3F(),
5208 gfx::PointF(1.f, 1.f),
5209 gfx::Size(10, 10),
5210 true,
5211 false);
5212 SetLayerPropertiesForTesting(intervening.get(),
5213 identity_transform,
5214 gfx::Point3F(),
5215 gfx::PointF(1.f, 1.f),
5216 gfx::Size(5, 5),
5217 true,
5218 false);
5219 SetLayerPropertiesForTesting(clip_child.get(),
5220 identity_transform,
5221 gfx::Point3F(),
5222 gfx::PointF(1.f, 1.f),
5223 gfx::Size(10, 10),
5224 true,
5225 false);
5227 host()->SetRootLayer(root);
5229 ExecuteCalculateDrawProperties(root.get());
5231 ASSERT_TRUE(root->render_surface());
5232 ASSERT_TRUE(render_surface->render_surface());
5234 // Ensure that we've inherited our clip parent's clip and weren't affected
5235 // by the intervening clip layer.
5236 ASSERT_EQ(gfx::Rect(1, 1, 20, 20).ToString(),
5237 clip_parent->clip_rect().ToString());
5238 ASSERT_EQ(clip_parent->clip_rect().ToString(),
5239 clip_child->clip_rect().ToString());
5240 ASSERT_EQ(gfx::Rect(3, 3, 10, 10).ToString(),
5241 intervening->clip_rect().ToString());
5243 // Ensure that the render surface reports a content rect that has been grown
5244 // to accomodate for the clip child.
5245 ASSERT_EQ(gfx::Rect(5, 5, 16, 16).ToString(),
5246 render_surface->render_surface()->content_rect().ToString());
5248 // The above check implies the two below, but they nicely demonstrate that
5249 // we've grown, despite the intervening layer's clip.
5250 ASSERT_TRUE(clip_parent->clip_rect().Contains(
5251 render_surface->render_surface()->content_rect()));
5252 ASSERT_FALSE(intervening->clip_rect().Contains(
5253 render_surface->render_surface()->content_rect()));
5256 TEST_F(LayerTreeHostCommonTest, ClipParentWithInterveningRenderSurface) {
5257 // Ensure that intervening render surfaces are not a problem in the basic
5258 // case. In the following tree, both render surfaces should be resized to
5259 // accomodate for the clip child, despite an intervening clip.
5261 // root (a render surface)
5262 // + clip_parent (masks to bounds)
5263 // + render_surface1 (sets opacity)
5264 // + intervening (masks to bounds)
5265 // + render_surface2 (also sets opacity)
5266 // + clip_child
5268 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5269 scoped_refptr<Layer> clip_parent = Layer::Create(layer_settings());
5270 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
5271 scoped_refptr<Layer> intervening = Layer::Create(layer_settings());
5272 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
5273 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
5274 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5276 root->AddChild(clip_parent);
5277 clip_parent->AddChild(render_surface1);
5278 render_surface1->AddChild(intervening);
5279 intervening->AddChild(render_surface2);
5280 render_surface2->AddChild(clip_child);
5282 clip_child->SetClipParent(clip_parent.get());
5284 intervening->SetMasksToBounds(true);
5285 clip_parent->SetMasksToBounds(true);
5287 render_surface1->SetForceRenderSurface(true);
5288 render_surface2->SetForceRenderSurface(true);
5290 gfx::Transform translation_transform;
5291 translation_transform.Translate(2, 2);
5293 gfx::Transform identity_transform;
5294 SetLayerPropertiesForTesting(root.get(),
5295 identity_transform,
5296 gfx::Point3F(),
5297 gfx::PointF(),
5298 gfx::Size(50, 50),
5299 true,
5300 false);
5301 SetLayerPropertiesForTesting(clip_parent.get(),
5302 translation_transform,
5303 gfx::Point3F(),
5304 gfx::PointF(1.f, 1.f),
5305 gfx::Size(40, 40),
5306 true,
5307 false);
5308 SetLayerPropertiesForTesting(render_surface1.get(),
5309 identity_transform,
5310 gfx::Point3F(),
5311 gfx::PointF(),
5312 gfx::Size(10, 10),
5313 true,
5314 false);
5315 SetLayerPropertiesForTesting(intervening.get(),
5316 identity_transform,
5317 gfx::Point3F(),
5318 gfx::PointF(1.f, 1.f),
5319 gfx::Size(5, 5),
5320 true,
5321 false);
5322 SetLayerPropertiesForTesting(render_surface2.get(),
5323 identity_transform,
5324 gfx::Point3F(),
5325 gfx::PointF(),
5326 gfx::Size(10, 10),
5327 true,
5328 false);
5329 SetLayerPropertiesForTesting(clip_child.get(),
5330 identity_transform,
5331 gfx::Point3F(),
5332 gfx::PointF(-10.f, -10.f),
5333 gfx::Size(60, 60),
5334 true,
5335 false);
5337 host()->SetRootLayer(root);
5339 ExecuteCalculateDrawProperties(root.get());
5341 EXPECT_TRUE(root->render_surface());
5342 EXPECT_TRUE(render_surface1->render_surface());
5343 EXPECT_TRUE(render_surface2->render_surface());
5345 // Since the render surfaces could have expanded, they should not clip (their
5346 // bounds would no longer be reliable). We should resort to layer clipping
5347 // in this case.
5348 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
5349 render_surface1->render_surface()->clip_rect().ToString());
5350 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
5351 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
5352 render_surface2->render_surface()->clip_rect().ToString());
5353 EXPECT_FALSE(render_surface2->render_surface()->is_clipped());
5355 // NB: clip rects are in target space.
5356 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
5357 render_surface1->clip_rect().ToString());
5358 EXPECT_TRUE(render_surface1->is_clipped());
5360 // This value is inherited from the clipping ancestor layer, 'intervening'.
5361 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
5362 render_surface2->clip_rect().ToString());
5363 EXPECT_TRUE(render_surface2->is_clipped());
5365 // The content rects of both render surfaces should both have expanded to
5366 // contain the clip child.
5367 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
5368 render_surface1->render_surface()->content_rect().ToString());
5369 EXPECT_EQ(gfx::Rect(-1, -1, 40, 40).ToString(),
5370 render_surface2->render_surface()->content_rect().ToString());
5372 // The clip child should have inherited the clip parent's clip (projected to
5373 // the right space, of course), and should have the correctly sized visible
5374 // content rect.
5375 EXPECT_EQ(gfx::Rect(-1, -1, 40, 40).ToString(),
5376 clip_child->clip_rect().ToString());
5377 EXPECT_EQ(gfx::Rect(9, 9, 40, 40).ToString(),
5378 clip_child->visible_layer_rect().ToString());
5379 EXPECT_TRUE(clip_child->is_clipped());
5382 TEST_F(LayerTreeHostCommonTest, ClipParentScrolledInterveningLayer) {
5383 // Ensure that intervening render surfaces are not a problem, even if there
5384 // is a scroll involved. Note, we do _not_ have to consider any other sort
5385 // of transform.
5387 // root (a render surface)
5388 // + clip_parent (masks to bounds)
5389 // + render_surface1 (sets opacity)
5390 // + intervening (masks to bounds AND scrolls)
5391 // + render_surface2 (also sets opacity)
5392 // + clip_child
5394 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5395 scoped_refptr<Layer> clip_parent = Layer::Create(layer_settings());
5396 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
5397 scoped_refptr<Layer> intervening = Layer::Create(layer_settings());
5398 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
5399 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
5400 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5402 root->AddChild(clip_parent);
5403 clip_parent->AddChild(render_surface1);
5404 render_surface1->AddChild(intervening);
5405 intervening->AddChild(render_surface2);
5406 render_surface2->AddChild(clip_child);
5408 clip_child->SetClipParent(clip_parent.get());
5410 intervening->SetMasksToBounds(true);
5411 clip_parent->SetMasksToBounds(true);
5412 intervening->SetScrollClipLayerId(clip_parent->id());
5413 intervening->SetScrollOffset(gfx::ScrollOffset(3, 3));
5415 render_surface1->SetForceRenderSurface(true);
5416 render_surface2->SetForceRenderSurface(true);
5418 gfx::Transform translation_transform;
5419 translation_transform.Translate(2, 2);
5421 gfx::Transform identity_transform;
5422 SetLayerPropertiesForTesting(root.get(),
5423 identity_transform,
5424 gfx::Point3F(),
5425 gfx::PointF(),
5426 gfx::Size(50, 50),
5427 true,
5428 false);
5429 SetLayerPropertiesForTesting(clip_parent.get(),
5430 translation_transform,
5431 gfx::Point3F(),
5432 gfx::PointF(1.f, 1.f),
5433 gfx::Size(40, 40),
5434 true,
5435 false);
5436 SetLayerPropertiesForTesting(render_surface1.get(),
5437 identity_transform,
5438 gfx::Point3F(),
5439 gfx::PointF(),
5440 gfx::Size(10, 10),
5441 true,
5442 false);
5443 SetLayerPropertiesForTesting(intervening.get(),
5444 identity_transform,
5445 gfx::Point3F(),
5446 gfx::PointF(1.f, 1.f),
5447 gfx::Size(5, 5),
5448 true,
5449 false);
5450 SetLayerPropertiesForTesting(render_surface2.get(),
5451 identity_transform,
5452 gfx::Point3F(),
5453 gfx::PointF(),
5454 gfx::Size(10, 10),
5455 true,
5456 false);
5457 SetLayerPropertiesForTesting(clip_child.get(),
5458 identity_transform,
5459 gfx::Point3F(),
5460 gfx::PointF(-10.f, -10.f),
5461 gfx::Size(60, 60),
5462 true,
5463 false);
5465 host()->SetRootLayer(root);
5467 ExecuteCalculateDrawProperties(root.get());
5469 EXPECT_TRUE(root->render_surface());
5470 EXPECT_TRUE(render_surface1->render_surface());
5471 EXPECT_TRUE(render_surface2->render_surface());
5473 // Since the render surfaces could have expanded, they should not clip (their
5474 // bounds would no longer be reliable). We should resort to layer clipping
5475 // in this case.
5476 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
5477 render_surface1->render_surface()->clip_rect().ToString());
5478 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
5479 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
5480 render_surface2->render_surface()->clip_rect().ToString());
5481 EXPECT_FALSE(render_surface2->render_surface()->is_clipped());
5483 // NB: clip rects are in target space.
5484 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
5485 render_surface1->clip_rect().ToString());
5486 EXPECT_TRUE(render_surface1->is_clipped());
5488 // This value is inherited from the clipping ancestor layer, 'intervening'.
5489 EXPECT_EQ(gfx::Rect(2, 2, 3, 3).ToString(),
5490 render_surface2->clip_rect().ToString());
5491 EXPECT_TRUE(render_surface2->is_clipped());
5493 // The content rects of both render surfaces should both have expanded to
5494 // contain the clip child.
5495 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
5496 render_surface1->render_surface()->content_rect().ToString());
5497 EXPECT_EQ(gfx::Rect(2, 2, 40, 40).ToString(),
5498 render_surface2->render_surface()->content_rect().ToString());
5500 // The clip child should have inherited the clip parent's clip (projected to
5501 // the right space, of course), and should have the correctly sized visible
5502 // content rect.
5503 EXPECT_EQ(gfx::Rect(2, 2, 40, 40).ToString(),
5504 clip_child->clip_rect().ToString());
5505 EXPECT_EQ(gfx::Rect(12, 12, 40, 40).ToString(),
5506 clip_child->visible_layer_rect().ToString());
5507 EXPECT_TRUE(clip_child->is_clipped());
5510 TEST_F(LayerTreeHostCommonTest, DescendantsOfClipChildren) {
5511 // Ensures that descendants of the clip child inherit the correct clip.
5513 // root (a render surface)
5514 // + clip_parent (masks to bounds)
5515 // + intervening (masks to bounds)
5516 // + clip_child
5517 // + child
5519 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5520 scoped_refptr<Layer> clip_parent = Layer::Create(layer_settings());
5521 scoped_refptr<Layer> intervening = Layer::Create(layer_settings());
5522 scoped_refptr<Layer> clip_child = Layer::Create(layer_settings());
5523 scoped_refptr<LayerWithForcedDrawsContent> child =
5524 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5526 root->AddChild(clip_parent);
5527 clip_parent->AddChild(intervening);
5528 intervening->AddChild(clip_child);
5529 clip_child->AddChild(child);
5531 clip_child->SetClipParent(clip_parent.get());
5533 intervening->SetMasksToBounds(true);
5534 clip_parent->SetMasksToBounds(true);
5536 gfx::Transform identity_transform;
5537 SetLayerPropertiesForTesting(root.get(),
5538 identity_transform,
5539 gfx::Point3F(),
5540 gfx::PointF(),
5541 gfx::Size(50, 50),
5542 true,
5543 false);
5544 SetLayerPropertiesForTesting(clip_parent.get(),
5545 identity_transform,
5546 gfx::Point3F(),
5547 gfx::PointF(),
5548 gfx::Size(40, 40),
5549 true,
5550 false);
5551 SetLayerPropertiesForTesting(intervening.get(),
5552 identity_transform,
5553 gfx::Point3F(),
5554 gfx::PointF(),
5555 gfx::Size(5, 5),
5556 true,
5557 false);
5558 SetLayerPropertiesForTesting(clip_child.get(),
5559 identity_transform,
5560 gfx::Point3F(),
5561 gfx::PointF(),
5562 gfx::Size(60, 60),
5563 true,
5564 false);
5565 SetLayerPropertiesForTesting(child.get(),
5566 identity_transform,
5567 gfx::Point3F(),
5568 gfx::PointF(),
5569 gfx::Size(60, 60),
5570 true,
5571 false);
5573 host()->SetRootLayer(root);
5575 ExecuteCalculateDrawProperties(root.get());
5577 EXPECT_TRUE(root->render_surface());
5579 // Neither the clip child nor its descendant should have inherited the clip
5580 // from |intervening|.
5581 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
5582 clip_child->clip_rect().ToString());
5583 EXPECT_TRUE(clip_child->is_clipped());
5584 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
5585 child->visible_layer_rect().ToString());
5586 EXPECT_TRUE(child->is_clipped());
5589 TEST_F(LayerTreeHostCommonTest,
5590 SurfacesShouldBeUnaffectedByNonDescendantClipChildren) {
5591 // Ensures that non-descendant clip children in the tree do not affect
5592 // render surfaces.
5594 // root (a render surface)
5595 // + clip_parent (masks to bounds)
5596 // + render_surface1
5597 // + clip_child
5598 // + render_surface2
5599 // + non_clip_child
5601 // In this example render_surface2 should be unaffected by clip_child.
5602 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5603 scoped_refptr<Layer> clip_parent = Layer::Create(layer_settings());
5604 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
5605 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
5606 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5607 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
5608 scoped_refptr<LayerWithForcedDrawsContent> non_clip_child =
5609 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5611 root->AddChild(clip_parent);
5612 clip_parent->AddChild(render_surface1);
5613 render_surface1->AddChild(clip_child);
5614 clip_parent->AddChild(render_surface2);
5615 render_surface2->AddChild(non_clip_child);
5617 clip_child->SetClipParent(clip_parent.get());
5619 clip_parent->SetMasksToBounds(true);
5620 render_surface1->SetMasksToBounds(true);
5622 gfx::Transform identity_transform;
5623 SetLayerPropertiesForTesting(root.get(),
5624 identity_transform,
5625 gfx::Point3F(),
5626 gfx::PointF(),
5627 gfx::Size(15, 15),
5628 true,
5629 false);
5630 SetLayerPropertiesForTesting(clip_parent.get(),
5631 identity_transform,
5632 gfx::Point3F(),
5633 gfx::PointF(),
5634 gfx::Size(10, 10),
5635 true,
5636 false);
5637 SetLayerPropertiesForTesting(render_surface1.get(),
5638 identity_transform,
5639 gfx::Point3F(),
5640 gfx::PointF(5, 5),
5641 gfx::Size(5, 5),
5642 true,
5643 false);
5644 SetLayerPropertiesForTesting(render_surface2.get(),
5645 identity_transform,
5646 gfx::Point3F(),
5647 gfx::PointF(),
5648 gfx::Size(5, 5),
5649 true,
5650 false);
5651 SetLayerPropertiesForTesting(clip_child.get(),
5652 identity_transform,
5653 gfx::Point3F(),
5654 gfx::PointF(-1, 1),
5655 gfx::Size(10, 10),
5656 true,
5657 false);
5658 SetLayerPropertiesForTesting(non_clip_child.get(),
5659 identity_transform,
5660 gfx::Point3F(),
5661 gfx::PointF(),
5662 gfx::Size(5, 5),
5663 true,
5664 false);
5666 render_surface1->SetForceRenderSurface(true);
5667 render_surface2->SetForceRenderSurface(true);
5669 host()->SetRootLayer(root);
5671 ExecuteCalculateDrawProperties(root.get());
5673 EXPECT_TRUE(root->render_surface());
5674 EXPECT_TRUE(render_surface1->render_surface());
5675 EXPECT_TRUE(render_surface2->render_surface());
5677 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
5678 render_surface1->clip_rect().ToString());
5679 EXPECT_TRUE(render_surface1->is_clipped());
5681 // The render surface should not clip (it has unclipped descendants), instead
5682 // it should rely on layer clipping.
5683 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
5684 render_surface1->render_surface()->clip_rect().ToString());
5685 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
5687 // That said, it should have grown to accomodate the unclipped descendant.
5688 EXPECT_EQ(gfx::Rect(-1, 1, 6, 4).ToString(),
5689 render_surface1->render_surface()->content_rect().ToString());
5691 // This render surface should clip. It has no unclipped descendants.
5692 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
5693 render_surface2->clip_rect().ToString());
5694 EXPECT_TRUE(render_surface2->render_surface()->is_clipped());
5696 // It also shouldn't have grown to accomodate the clip child.
5697 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
5698 render_surface2->render_surface()->content_rect().ToString());
5700 // Sanity check our num_unclipped_descendants values.
5701 EXPECT_EQ(1u, render_surface1->num_unclipped_descendants());
5702 EXPECT_EQ(0u, render_surface2->num_unclipped_descendants());
5705 TEST_F(LayerTreeHostCommonTest, CanRenderToSeparateSurface) {
5706 FakeImplProxy proxy;
5707 TestSharedBitmapManager shared_bitmap_manager;
5708 TestTaskGraphRunner task_graph_runner;
5709 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
5710 &task_graph_runner);
5711 scoped_ptr<LayerImpl> root =
5712 LayerImpl::Create(host_impl.active_tree(), 12345);
5713 scoped_ptr<LayerImpl> child1 =
5714 LayerImpl::Create(host_impl.active_tree(), 123456);
5715 scoped_ptr<LayerImpl> child2 =
5716 LayerImpl::Create(host_impl.active_tree(), 1234567);
5717 scoped_ptr<LayerImpl> child3 =
5718 LayerImpl::Create(host_impl.active_tree(), 12345678);
5720 gfx::Transform identity_matrix;
5721 gfx::Point3F transform_origin;
5722 gfx::PointF position;
5723 gfx::Size bounds(100, 100);
5724 SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
5725 position, bounds, true, false, true);
5726 root->SetDrawsContent(true);
5728 // This layer structure normally forces render surface due to preserves3d
5729 // behavior.
5730 SetLayerPropertiesForTesting(child1.get(), identity_matrix, transform_origin,
5731 position, bounds, false, true, true);
5732 child1->SetDrawsContent(true);
5733 SetLayerPropertiesForTesting(child2.get(), identity_matrix, transform_origin,
5734 position, bounds, true, false, false);
5735 child2->SetDrawsContent(true);
5736 SetLayerPropertiesForTesting(child3.get(), identity_matrix, transform_origin,
5737 position, bounds, true, false, false);
5738 child3->SetDrawsContent(true);
5740 child2->Set3dSortingContextId(1);
5741 child3->Set3dSortingContextId(1);
5743 child2->AddChild(child3.Pass());
5744 child1->AddChild(child2.Pass());
5745 root->AddChild(child1.Pass());
5748 LayerImplList render_surface_layer_list;
5749 FakeLayerTreeHostImpl::RecursiveUpdateNumChildren(root.get());
5750 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
5751 root.get(), root->bounds(), &render_surface_layer_list);
5752 inputs.can_render_to_separate_surface = true;
5753 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5755 EXPECT_EQ(2u, render_surface_layer_list.size());
5757 int count_represents_target_render_surface = 0;
5758 int count_represents_contributing_render_surface = 0;
5759 int count_represents_itself = 0;
5760 LayerIterator end = LayerIterator::End(&render_surface_layer_list);
5761 for (LayerIterator it = LayerIterator::Begin(&render_surface_layer_list);
5762 it != end; ++it) {
5763 if (it.represents_target_render_surface())
5764 count_represents_target_render_surface++;
5765 if (it.represents_contributing_render_surface())
5766 count_represents_contributing_render_surface++;
5767 if (it.represents_itself())
5768 count_represents_itself++;
5771 // Two render surfaces.
5772 EXPECT_EQ(2, count_represents_target_render_surface);
5773 // Second render surface contributes to root render surface.
5774 EXPECT_EQ(1, count_represents_contributing_render_surface);
5775 // All 4 layers represent itself.
5776 EXPECT_EQ(4, count_represents_itself);
5780 LayerImplList render_surface_layer_list;
5781 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
5782 root.get(), root->bounds(), &render_surface_layer_list);
5783 inputs.can_render_to_separate_surface = false;
5784 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5786 EXPECT_EQ(1u, render_surface_layer_list.size());
5788 int count_represents_target_render_surface = 0;
5789 int count_represents_contributing_render_surface = 0;
5790 int count_represents_itself = 0;
5791 LayerIterator end = LayerIterator::End(&render_surface_layer_list);
5792 for (LayerIterator it = LayerIterator::Begin(&render_surface_layer_list);
5793 it != end; ++it) {
5794 if (it.represents_target_render_surface())
5795 count_represents_target_render_surface++;
5796 if (it.represents_contributing_render_surface())
5797 count_represents_contributing_render_surface++;
5798 if (it.represents_itself())
5799 count_represents_itself++;
5802 // Only root layer has a render surface.
5803 EXPECT_EQ(1, count_represents_target_render_surface);
5804 // No layer contributes a render surface to root render surface.
5805 EXPECT_EQ(0, count_represents_contributing_render_surface);
5806 // All 4 layers represent itself.
5807 EXPECT_EQ(4, count_represents_itself);
5811 TEST_F(LayerTreeHostCommonTest, DoNotIncludeBackfaceInvisibleSurfaces) {
5812 LayerImpl* root = root_layer();
5813 LayerImpl* render_surface = AddChild<LayerImpl>(root);
5814 LayerImpl* child = AddChild<LayerImpl>(render_surface);
5815 child->SetDrawsContent(true);
5817 gfx::Transform identity_transform;
5818 SetLayerPropertiesForTesting(root, identity_transform, gfx::Point3F(),
5819 gfx::PointF(), gfx::Size(50, 50), true, false,
5820 true);
5821 SetLayerPropertiesForTesting(render_surface, identity_transform,
5822 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
5823 false, true, true);
5824 SetLayerPropertiesForTesting(child, identity_transform, gfx::Point3F(),
5825 gfx::PointF(), gfx::Size(20, 20), true, false,
5826 false);
5828 root->SetShouldFlattenTransform(false);
5829 root->Set3dSortingContextId(1);
5830 render_surface->SetDoubleSided(false);
5832 ExecuteCalculateDrawProperties(root);
5834 EXPECT_EQ(2u, render_surface_layer_list_impl()->size());
5835 EXPECT_EQ(1u, render_surface_layer_list_impl()
5836 ->at(0)
5837 ->render_surface()
5838 ->layer_list()
5839 .size());
5840 EXPECT_EQ(1u, render_surface_layer_list_impl()
5841 ->at(1)
5842 ->render_surface()
5843 ->layer_list()
5844 .size());
5846 gfx::Transform rotation_transform = identity_transform;
5847 rotation_transform.RotateAboutXAxis(180.0);
5849 render_surface->SetTransform(rotation_transform);
5851 ExecuteCalculateDrawProperties(root);
5853 EXPECT_EQ(1u, render_surface_layer_list_impl()->size());
5854 EXPECT_EQ(0u, render_surface_layer_list_impl()
5855 ->at(0)
5856 ->render_surface()
5857 ->layer_list()
5858 .size());
5861 TEST_F(LayerTreeHostCommonTest, ClippedByScrollParent) {
5862 // Checks that the simple case (being clipped by a scroll parent that would
5863 // have been processed before you anyhow) results in the right clips.
5865 // + root
5866 // + scroll_parent_border
5867 // | + scroll_parent_clip
5868 // | + scroll_parent
5869 // + scroll_child
5871 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5872 scoped_refptr<Layer> scroll_parent_border = Layer::Create(layer_settings());
5873 scoped_refptr<Layer> scroll_parent_clip = Layer::Create(layer_settings());
5874 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
5875 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5876 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
5877 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5879 root->AddChild(scroll_child);
5881 root->AddChild(scroll_parent_border);
5882 scroll_parent_border->AddChild(scroll_parent_clip);
5883 scroll_parent_clip->AddChild(scroll_parent);
5885 scroll_parent_clip->SetMasksToBounds(true);
5887 scroll_child->SetScrollParent(scroll_parent.get());
5889 gfx::Transform identity_transform;
5890 SetLayerPropertiesForTesting(root.get(),
5891 identity_transform,
5892 gfx::Point3F(),
5893 gfx::PointF(),
5894 gfx::Size(50, 50),
5895 true,
5896 false);
5897 SetLayerPropertiesForTesting(scroll_parent_border.get(),
5898 identity_transform,
5899 gfx::Point3F(),
5900 gfx::PointF(),
5901 gfx::Size(40, 40),
5902 true,
5903 false);
5904 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
5905 identity_transform,
5906 gfx::Point3F(),
5907 gfx::PointF(),
5908 gfx::Size(30, 30),
5909 true,
5910 false);
5911 SetLayerPropertiesForTesting(scroll_parent.get(),
5912 identity_transform,
5913 gfx::Point3F(),
5914 gfx::PointF(),
5915 gfx::Size(50, 50),
5916 true,
5917 false);
5918 SetLayerPropertiesForTesting(scroll_child.get(),
5919 identity_transform,
5920 gfx::Point3F(),
5921 gfx::PointF(),
5922 gfx::Size(50, 50),
5923 true,
5924 false);
5926 host()->SetRootLayer(root);
5928 ExecuteCalculateDrawProperties(root.get());
5930 EXPECT_TRUE(root->render_surface());
5932 EXPECT_EQ(gfx::Rect(0, 0, 30, 30).ToString(),
5933 scroll_child->clip_rect().ToString());
5934 EXPECT_TRUE(scroll_child->is_clipped());
5937 TEST_F(LayerTreeHostCommonTest, SingularTransformSubtreesDoNotDraw) {
5938 scoped_refptr<LayerWithForcedDrawsContent> root =
5939 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5940 scoped_refptr<LayerWithForcedDrawsContent> parent =
5941 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5942 scoped_refptr<LayerWithForcedDrawsContent> child =
5943 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5945 root->AddChild(parent);
5946 parent->AddChild(child);
5948 gfx::Transform identity_transform;
5949 SetLayerPropertiesForTesting(root.get(),
5950 identity_transform,
5951 gfx::Point3F(),
5952 gfx::PointF(),
5953 gfx::Size(50, 50),
5954 true,
5955 true);
5956 root->SetForceRenderSurface(true);
5957 SetLayerPropertiesForTesting(parent.get(),
5958 identity_transform,
5959 gfx::Point3F(),
5960 gfx::PointF(),
5961 gfx::Size(30, 30),
5962 true,
5963 true);
5964 parent->SetForceRenderSurface(true);
5965 SetLayerPropertiesForTesting(child.get(),
5966 identity_transform,
5967 gfx::Point3F(),
5968 gfx::PointF(),
5969 gfx::Size(20, 20),
5970 true,
5971 true);
5972 child->SetForceRenderSurface(true);
5974 host()->SetRootLayer(root);
5976 ExecuteCalculateDrawProperties(root.get());
5978 EXPECT_EQ(3u, render_surface_layer_list()->size());
5980 gfx::Transform singular_transform;
5981 singular_transform.Scale3d(
5982 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
5984 child->SetTransform(singular_transform);
5986 ExecuteCalculateDrawProperties(root.get());
5988 EXPECT_EQ(2u, render_surface_layer_list()->size());
5990 // Ensure that the entire subtree under a layer with singular transform does
5991 // not get rendered.
5992 parent->SetTransform(singular_transform);
5993 child->SetTransform(identity_transform);
5995 ExecuteCalculateDrawProperties(root.get());
5997 EXPECT_EQ(1u, render_surface_layer_list()->size());
6000 TEST_F(LayerTreeHostCommonTest, ClippedByOutOfOrderScrollParent) {
6001 // Checks that clipping by a scroll parent that follows you in paint order
6002 // still results in correct clipping.
6004 // + root
6005 // + scroll_parent_border
6006 // + scroll_parent_clip
6007 // + scroll_parent
6008 // + scroll_child
6010 LayerImpl* root = root_layer();
6011 LayerImpl* scroll_parent_border = AddChild<LayerImpl>(root);
6012 LayerImpl* scroll_parent_clip = AddChild<LayerImpl>(scroll_parent_border);
6013 LayerImpl* scroll_parent = AddChild<LayerImpl>(scroll_parent_clip);
6014 LayerImpl* scroll_child = AddChild<LayerImpl>(root);
6016 scroll_parent->SetDrawsContent(true);
6017 scroll_child->SetDrawsContent(true);
6019 scroll_parent_clip->SetMasksToBounds(true);
6021 scroll_child->SetScrollParent(scroll_parent);
6022 scoped_ptr<std::set<LayerImpl*>> scroll_children(new std::set<LayerImpl*>);
6023 scroll_children->insert(scroll_child);
6024 scroll_parent->SetScrollChildren(scroll_children.release());
6026 gfx::Transform identity_transform;
6027 SetLayerPropertiesForTesting(root, identity_transform, gfx::Point3F(),
6028 gfx::PointF(), gfx::Size(50, 50), true, false,
6029 true);
6030 SetLayerPropertiesForTesting(scroll_parent_border, identity_transform,
6031 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
6032 true, false, false);
6033 SetLayerPropertiesForTesting(scroll_parent_clip, identity_transform,
6034 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6035 true, false, false);
6036 SetLayerPropertiesForTesting(scroll_parent, identity_transform,
6037 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
6038 true, false, false);
6039 SetLayerPropertiesForTesting(scroll_child, identity_transform, gfx::Point3F(),
6040 gfx::PointF(), gfx::Size(50, 50), true, false,
6041 false);
6043 ExecuteCalculateDrawProperties(root);
6045 EXPECT_TRUE(root->render_surface());
6047 EXPECT_EQ(gfx::Rect(0, 0, 30, 30).ToString(),
6048 scroll_child->clip_rect().ToString());
6049 EXPECT_TRUE(scroll_child->is_clipped());
6052 TEST_F(LayerTreeHostCommonTest, ClippedByOutOfOrderScrollGrandparent) {
6053 // Checks that clipping by a scroll parent and scroll grandparent that follow
6054 // you in paint order still results in correct clipping.
6056 // + root
6057 // + scroll_child
6058 // + scroll_parent_border
6059 // | + scroll_parent_clip
6060 // | + scroll_parent
6061 // + scroll_grandparent_border
6062 // + scroll_grandparent_clip
6063 // + scroll_grandparent
6065 LayerImpl* root = root_layer();
6066 LayerImpl* scroll_child = AddChild<LayerImpl>(root);
6067 LayerImpl* scroll_parent_border = AddChild<LayerImpl>(root);
6068 LayerImpl* scroll_parent_clip = AddChild<LayerImpl>(scroll_parent_border);
6069 LayerImpl* scroll_parent = AddChild<LayerImpl>(scroll_parent_clip);
6070 LayerImpl* scroll_grandparent_border = AddChild<LayerImpl>(root);
6071 LayerImpl* scroll_grandparent_clip =
6072 AddChild<LayerImpl>(scroll_grandparent_border);
6073 LayerImpl* scroll_grandparent = AddChild<LayerImpl>(scroll_grandparent_clip);
6075 scroll_parent->SetDrawsContent(true);
6076 scroll_grandparent->SetDrawsContent(true);
6077 scroll_child->SetDrawsContent(true);
6079 scroll_parent_clip->SetMasksToBounds(true);
6080 scroll_grandparent_clip->SetMasksToBounds(true);
6082 scroll_child->SetScrollParent(scroll_parent);
6083 scoped_ptr<std::set<LayerImpl*>> scroll_children(new std::set<LayerImpl*>);
6084 scroll_children->insert(scroll_child);
6085 scroll_parent->SetScrollChildren(scroll_children.release());
6087 scroll_parent_border->SetScrollParent(scroll_grandparent);
6088 scroll_children.reset(new std::set<LayerImpl*>);
6089 scroll_children->insert(scroll_parent_border);
6090 scroll_grandparent->SetScrollChildren(scroll_children.release());
6092 gfx::Transform identity_transform;
6093 SetLayerPropertiesForTesting(root, identity_transform, gfx::Point3F(),
6094 gfx::PointF(), gfx::Size(50, 50), true, false,
6095 true);
6096 SetLayerPropertiesForTesting(scroll_grandparent_border, identity_transform,
6097 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
6098 true, false, false);
6099 SetLayerPropertiesForTesting(scroll_grandparent_clip, identity_transform,
6100 gfx::Point3F(), gfx::PointF(), gfx::Size(20, 20),
6101 true, false, false);
6102 SetLayerPropertiesForTesting(scroll_grandparent, identity_transform,
6103 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
6104 true, false, false);
6105 SetLayerPropertiesForTesting(scroll_parent_border, identity_transform,
6106 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
6107 true, false, false);
6108 SetLayerPropertiesForTesting(scroll_parent_clip, identity_transform,
6109 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6110 true, false, false);
6111 SetLayerPropertiesForTesting(scroll_parent, identity_transform,
6112 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
6113 true, false, false);
6114 SetLayerPropertiesForTesting(scroll_child, identity_transform, gfx::Point3F(),
6115 gfx::PointF(), gfx::Size(50, 50), true, false,
6116 false);
6118 ExecuteCalculateDrawProperties(root);
6120 EXPECT_TRUE(root->render_surface());
6122 EXPECT_EQ(gfx::Rect(0, 0, 20, 20).ToString(),
6123 scroll_child->clip_rect().ToString());
6124 EXPECT_TRUE(scroll_child->is_clipped());
6126 // Despite the fact that we visited the above layers out of order to get the
6127 // correct clip, the layer lists should be unaffected.
6128 EXPECT_EQ(3u, root->render_surface()->layer_list().size());
6129 EXPECT_EQ(scroll_child, root->render_surface()->layer_list().at(0));
6130 EXPECT_EQ(scroll_parent, root->render_surface()->layer_list().at(1));
6131 EXPECT_EQ(scroll_grandparent, root->render_surface()->layer_list().at(2));
6134 TEST_F(LayerTreeHostCommonTest, OutOfOrderClippingRequiresRSLLSorting) {
6135 // Ensures that even if we visit layers out of order, we still produce a
6136 // correctly ordered render surface layer list.
6137 // + root
6138 // + scroll_child
6139 // + scroll_parent_border
6140 // + scroll_parent_clip
6141 // + scroll_parent
6142 // + render_surface2
6143 // + scroll_grandparent_border
6144 // + scroll_grandparent_clip
6145 // + scroll_grandparent
6146 // + render_surface1
6148 LayerImpl* root = root_layer();
6149 root->SetDrawsContent(true);
6151 LayerImpl* scroll_child = AddChild<LayerImpl>(root);
6152 scroll_child->SetDrawsContent(true);
6154 LayerImpl* scroll_parent_border = AddChild<LayerImpl>(root);
6155 LayerImpl* scroll_parent_clip = AddChild<LayerImpl>(scroll_parent_border);
6156 LayerImpl* scroll_parent = AddChild<LayerImpl>(scroll_parent_clip);
6157 LayerImpl* render_surface2 = AddChild<LayerImpl>(scroll_parent);
6158 LayerImpl* scroll_grandparent_border = AddChild<LayerImpl>(root);
6159 LayerImpl* scroll_grandparent_clip =
6160 AddChild<LayerImpl>(scroll_grandparent_border);
6161 LayerImpl* scroll_grandparent = AddChild<LayerImpl>(scroll_grandparent_clip);
6162 LayerImpl* render_surface1 = AddChild<LayerImpl>(scroll_grandparent);
6164 scroll_parent->SetDrawsContent(true);
6165 render_surface1->SetDrawsContent(true);
6166 scroll_grandparent->SetDrawsContent(true);
6167 render_surface2->SetDrawsContent(true);
6169 scroll_parent_clip->SetMasksToBounds(true);
6170 scroll_grandparent_clip->SetMasksToBounds(true);
6172 scroll_child->SetScrollParent(scroll_parent);
6173 scoped_ptr<std::set<LayerImpl*>> scroll_children(new std::set<LayerImpl*>);
6174 scroll_children->insert(scroll_child);
6175 scroll_parent->SetScrollChildren(scroll_children.release());
6177 scroll_parent_border->SetScrollParent(scroll_grandparent);
6178 scroll_children.reset(new std::set<LayerImpl*>);
6179 scroll_children->insert(scroll_parent_border);
6180 scroll_grandparent->SetScrollChildren(scroll_children.release());
6182 gfx::Transform identity_transform;
6183 SetLayerPropertiesForTesting(root, identity_transform, gfx::Point3F(),
6184 gfx::PointF(), gfx::Size(50, 50), true, false,
6185 true);
6186 SetLayerPropertiesForTesting(scroll_grandparent_border, identity_transform,
6187 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
6188 true, false, false);
6189 SetLayerPropertiesForTesting(scroll_grandparent_clip, identity_transform,
6190 gfx::Point3F(), gfx::PointF(), gfx::Size(20, 20),
6191 true, false, false);
6192 SetLayerPropertiesForTesting(scroll_grandparent, identity_transform,
6193 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
6194 true, false, false);
6195 SetLayerPropertiesForTesting(render_surface1, identity_transform,
6196 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
6197 true, false, true);
6198 SetLayerPropertiesForTesting(scroll_parent_border, identity_transform,
6199 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
6200 true, false, false);
6201 SetLayerPropertiesForTesting(scroll_parent_clip, identity_transform,
6202 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6203 true, false, false);
6204 SetLayerPropertiesForTesting(scroll_parent, identity_transform,
6205 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
6206 true, false, false);
6207 SetLayerPropertiesForTesting(render_surface2, identity_transform,
6208 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
6209 true, false, true);
6210 SetLayerPropertiesForTesting(scroll_child, identity_transform, gfx::Point3F(),
6211 gfx::PointF(), gfx::Size(50, 50), true, false,
6212 false);
6214 ExecuteCalculateDrawProperties(root);
6216 EXPECT_TRUE(root->render_surface());
6218 EXPECT_EQ(gfx::Rect(0, 0, 20, 20).ToString(),
6219 scroll_child->clip_rect().ToString());
6220 EXPECT_TRUE(scroll_child->is_clipped());
6222 // Despite the fact that we had to process the layers out of order to get the
6223 // right clip, our render_surface_layer_list's order should be unaffected.
6224 EXPECT_EQ(3u, render_surface_layer_list_impl()->size());
6225 EXPECT_EQ(root, render_surface_layer_list_impl()->at(0));
6226 EXPECT_EQ(render_surface2, render_surface_layer_list_impl()->at(1));
6227 EXPECT_EQ(render_surface1, render_surface_layer_list_impl()->at(2));
6228 EXPECT_TRUE(render_surface_layer_list_impl()->at(0)->render_surface());
6229 EXPECT_TRUE(render_surface_layer_list_impl()->at(1)->render_surface());
6230 EXPECT_TRUE(render_surface_layer_list_impl()->at(2)->render_surface());
6233 TEST_F(LayerTreeHostCommonTest, FixedPositionWithInterveningRenderSurface) {
6234 // Ensures that when we have a render surface between a fixed position layer
6235 // and its container, we compute the fixed position layer's draw transform
6236 // with respect to that intervening render surface, not with respect to its
6237 // container's render target.
6239 // + root
6240 // + render_surface
6241 // + fixed
6242 // + child
6244 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6245 scoped_refptr<LayerWithForcedDrawsContent> render_surface =
6246 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
6247 scoped_refptr<LayerWithForcedDrawsContent> fixed =
6248 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
6249 scoped_refptr<LayerWithForcedDrawsContent> child =
6250 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
6252 root->AddChild(render_surface);
6253 render_surface->AddChild(fixed);
6254 fixed->AddChild(child);
6256 root->SetIsContainerForFixedPositionLayers(true);
6257 render_surface->SetForceRenderSurface(true);
6259 LayerPositionConstraint constraint;
6260 constraint.set_is_fixed_position(true);
6261 fixed->SetPositionConstraint(constraint);
6263 SetLayerPropertiesForTesting(root.get(), gfx::Transform(), gfx::Point3F(),
6264 gfx::PointF(), gfx::Size(50, 50), true, false);
6265 SetLayerPropertiesForTesting(render_surface.get(), gfx::Transform(),
6266 gfx::Point3F(), gfx::PointF(7.f, 9.f),
6267 gfx::Size(50, 50), true, false);
6268 SetLayerPropertiesForTesting(fixed.get(), gfx::Transform(), gfx::Point3F(),
6269 gfx::PointF(10.f, 15.f), gfx::Size(50, 50), true,
6270 false);
6271 SetLayerPropertiesForTesting(child.get(), gfx::Transform(), gfx::Point3F(),
6272 gfx::PointF(1.f, 2.f), gfx::Size(50, 50), true,
6273 false);
6275 host()->SetRootLayer(root);
6277 ExecuteCalculateDrawProperties(root.get());
6279 gfx::Transform expected_fixed_draw_transform;
6280 expected_fixed_draw_transform.Translate(10.f, 15.f);
6281 EXPECT_EQ(expected_fixed_draw_transform, fixed->draw_transform());
6283 gfx::Transform expected_fixed_screen_space_transform;
6284 expected_fixed_screen_space_transform.Translate(17.f, 24.f);
6285 EXPECT_EQ(expected_fixed_screen_space_transform,
6286 fixed->screen_space_transform());
6288 gfx::Transform expected_child_draw_transform;
6289 expected_child_draw_transform.Translate(11.f, 17.f);
6290 EXPECT_EQ(expected_child_draw_transform, child->draw_transform());
6292 gfx::Transform expected_child_screen_space_transform;
6293 expected_child_screen_space_transform.Translate(18.f, 26.f);
6294 EXPECT_EQ(expected_child_screen_space_transform,
6295 child->screen_space_transform());
6298 TEST_F(LayerTreeHostCommonTest, ScrollCompensationWithRounding) {
6299 // This test verifies that a scrolling layer that gets snapped to
6300 // integer coordinates doesn't move a fixed position child.
6302 // + root
6303 // + container
6304 // + scroller
6305 // + fixed
6307 FakeImplProxy proxy;
6308 TestSharedBitmapManager shared_bitmap_manager;
6309 TestTaskGraphRunner task_graph_runner;
6310 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
6311 &task_graph_runner);
6312 host_impl.CreatePendingTree();
6313 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
6314 scoped_ptr<LayerImpl> container =
6315 LayerImpl::Create(host_impl.active_tree(), 2);
6316 LayerImpl* container_layer = container.get();
6317 scoped_ptr<LayerImpl> scroller =
6318 LayerImpl::Create(host_impl.active_tree(), 3);
6319 LayerImpl* scroll_layer = scroller.get();
6320 scoped_ptr<LayerImpl> fixed = LayerImpl::Create(host_impl.active_tree(), 4);
6321 LayerImpl* fixed_layer = fixed.get();
6323 container->SetIsContainerForFixedPositionLayers(true);
6325 LayerPositionConstraint constraint;
6326 constraint.set_is_fixed_position(true);
6327 fixed->SetPositionConstraint(constraint);
6329 scroller->SetScrollClipLayer(container->id());
6331 gfx::Transform identity_transform;
6332 gfx::Transform container_transform;
6333 container_transform.Translate3d(10.0, 20.0, 0.0);
6334 gfx::Vector2dF container_offset = container_transform.To2dTranslation();
6336 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
6337 gfx::PointF(), gfx::Size(50, 50), true, false,
6338 true);
6339 SetLayerPropertiesForTesting(container.get(), container_transform,
6340 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
6341 true, false, false);
6342 SetLayerPropertiesForTesting(scroller.get(), identity_transform,
6343 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6344 true, false, false);
6345 SetLayerPropertiesForTesting(fixed.get(), identity_transform, gfx::Point3F(),
6346 gfx::PointF(), gfx::Size(50, 50), true, false,
6347 false);
6349 scroller->AddChild(fixed.Pass());
6350 container->AddChild(scroller.Pass());
6351 root->AddChild(container.Pass());
6353 // Rounded to integers already.
6355 gfx::Vector2dF scroll_delta(3.0, 5.0);
6356 scroll_layer->SetScrollDelta(scroll_delta);
6358 LayerImplList render_surface_layer_list;
6359 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6360 root.get(), root->bounds(), &render_surface_layer_list);
6361 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6363 EXPECT_TRANSFORMATION_MATRIX_EQ(
6364 container_layer->draw_properties().screen_space_transform,
6365 fixed_layer->draw_properties().screen_space_transform);
6366 EXPECT_VECTOR_EQ(
6367 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
6368 container_offset);
6369 EXPECT_VECTOR_EQ(scroll_layer->draw_properties()
6370 .screen_space_transform.To2dTranslation(),
6371 container_offset - scroll_delta);
6374 // Scroll delta requiring rounding.
6376 gfx::Vector2dF scroll_delta(4.1f, 8.1f);
6377 scroll_layer->SetScrollDelta(scroll_delta);
6379 gfx::Vector2dF rounded_scroll_delta(4.f, 8.f);
6381 LayerImplList render_surface_layer_list;
6382 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6383 root.get(), root->bounds(), &render_surface_layer_list);
6384 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6386 EXPECT_TRANSFORMATION_MATRIX_EQ(
6387 container_layer->draw_properties().screen_space_transform,
6388 fixed_layer->draw_properties().screen_space_transform);
6389 EXPECT_VECTOR_EQ(
6390 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
6391 container_offset);
6392 EXPECT_VECTOR_EQ(scroll_layer->draw_properties()
6393 .screen_space_transform.To2dTranslation(),
6394 container_offset - rounded_scroll_delta);
6397 // Scale is applied earlier in the tree.
6399 gfx::Transform scaled_container_transform = container_transform;
6400 scaled_container_transform.Scale3d(3.0, 3.0, 1.0);
6401 container_layer->SetTransform(scaled_container_transform);
6403 gfx::Vector2dF scroll_delta(4.5f, 8.5f);
6404 scroll_layer->SetScrollDelta(scroll_delta);
6406 LayerImplList render_surface_layer_list;
6407 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6408 root.get(), root->bounds(), &render_surface_layer_list);
6409 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6411 EXPECT_TRANSFORMATION_MATRIX_EQ(
6412 container_layer->draw_properties().screen_space_transform,
6413 fixed_layer->draw_properties().screen_space_transform);
6414 EXPECT_VECTOR_EQ(
6415 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
6416 container_offset);
6418 container_layer->SetTransform(container_transform);
6421 // Scale is applied on the scroll layer itself.
6423 gfx::Transform scale_transform;
6424 scale_transform.Scale3d(3.0, 3.0, 1.0);
6425 scroll_layer->SetTransform(scale_transform);
6427 gfx::Vector2dF scroll_delta(4.5f, 8.5f);
6428 scroll_layer->SetScrollDelta(scroll_delta);
6430 LayerImplList render_surface_layer_list;
6431 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6432 root.get(), root->bounds(), &render_surface_layer_list);
6433 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6435 EXPECT_VECTOR_EQ(
6436 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
6437 container_offset);
6439 scroll_layer->SetTransform(identity_transform);
6443 TEST_F(LayerTreeHostCommonTest,
6444 ScrollCompensationMainScrollOffsetFractionalPart) {
6445 // This test verifies that a scrolling layer that has fractional scroll offset
6446 // from main doesn't move a fixed position child.
6448 // + root
6449 // + container
6450 // + scroller
6451 // + fixed
6453 FakeImplProxy proxy;
6454 TestSharedBitmapManager shared_bitmap_manager;
6455 TestTaskGraphRunner task_graph_runner;
6456 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
6457 &task_graph_runner);
6458 host_impl.CreatePendingTree();
6459 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
6460 scoped_ptr<LayerImpl> container =
6461 LayerImpl::Create(host_impl.active_tree(), 2);
6462 LayerImpl* container_layer = container.get();
6463 scoped_ptr<LayerImpl> scroller =
6464 LayerImpl::Create(host_impl.active_tree(), 3);
6465 LayerImpl* scroll_layer = scroller.get();
6466 scoped_ptr<LayerImpl> fixed = LayerImpl::Create(host_impl.active_tree(), 4);
6467 LayerImpl* fixed_layer = fixed.get();
6469 container->SetIsContainerForFixedPositionLayers(true);
6471 LayerPositionConstraint constraint;
6472 constraint.set_is_fixed_position(true);
6473 fixed->SetPositionConstraint(constraint);
6475 scroller->SetScrollClipLayer(container->id());
6477 gfx::Transform identity_transform;
6478 gfx::Transform container_transform;
6479 container_transform.Translate3d(10.0, 20.0, 0.0);
6480 gfx::Vector2dF container_offset = container_transform.To2dTranslation();
6482 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
6483 gfx::PointF(), gfx::Size(50, 50), true, false,
6484 true);
6485 SetLayerPropertiesForTesting(container.get(), container_transform,
6486 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
6487 true, false, false);
6488 SetLayerPropertiesForTesting(scroller.get(), identity_transform,
6489 gfx::Point3F(), gfx::PointF(0.0, 0.0),
6490 gfx::Size(30, 30), true, false, false);
6492 gfx::ScrollOffset scroll_offset(3.3, 4.2);
6493 gfx::Vector2dF main_scroll_fractional_part(0.3f, 0.2f);
6494 gfx::Vector2dF scroll_delta(0.1f, 0.4f);
6495 // Blink only uses the integer part of the scroll_offset for fixed
6496 // position layer.
6497 SetLayerPropertiesForTesting(fixed.get(), identity_transform, gfx::Point3F(),
6498 gfx::PointF(3.0f, 4.0f), gfx::Size(50, 50), true,
6499 false, false);
6500 scroll_layer->PushScrollOffsetFromMainThread(scroll_offset);
6501 scroll_layer->SetScrollDelta(scroll_delta);
6502 scroll_layer->SetScrollCompensationAdjustment(main_scroll_fractional_part);
6504 scroller->AddChild(fixed.Pass());
6505 container->AddChild(scroller.Pass());
6506 root->AddChild(container.Pass());
6508 LayerImplList render_surface_layer_list;
6509 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6510 root.get(), root->bounds(), &render_surface_layer_list);
6511 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6513 EXPECT_TRANSFORMATION_MATRIX_EQ(
6514 container_layer->draw_properties().screen_space_transform,
6515 fixed_layer->draw_properties().screen_space_transform);
6516 EXPECT_VECTOR_EQ(
6517 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
6518 container_offset);
6520 gfx::ScrollOffset effective_scroll_offset =
6521 ScrollOffsetWithDelta(scroll_offset, scroll_delta);
6522 gfx::Vector2d rounded_effective_scroll_offset =
6523 ToRoundedVector2d(ScrollOffsetToVector2dF(effective_scroll_offset));
6524 EXPECT_VECTOR_EQ(
6525 scroll_layer->draw_properties().screen_space_transform.To2dTranslation(),
6526 container_offset - rounded_effective_scroll_offset);
6529 class AnimationScaleFactorTrackingLayerImpl : public LayerImpl {
6530 public:
6531 static scoped_ptr<AnimationScaleFactorTrackingLayerImpl> Create(
6532 LayerTreeImpl* tree_impl,
6533 int id) {
6534 return make_scoped_ptr(
6535 new AnimationScaleFactorTrackingLayerImpl(tree_impl, id));
6538 ~AnimationScaleFactorTrackingLayerImpl() override {}
6540 private:
6541 explicit AnimationScaleFactorTrackingLayerImpl(LayerTreeImpl* tree_impl,
6542 int id)
6543 : LayerImpl(tree_impl, id) {
6544 SetDrawsContent(true);
6548 TEST_F(LayerTreeHostCommonTest, MaximumAnimationScaleFactor) {
6549 FakeImplProxy proxy;
6550 TestSharedBitmapManager shared_bitmap_manager;
6551 TestTaskGraphRunner task_graph_runner;
6552 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
6553 &task_graph_runner);
6554 gfx::Transform identity_matrix;
6555 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> grand_parent =
6556 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 1);
6557 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> parent =
6558 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 2);
6559 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> child =
6560 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 3);
6561 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> grand_child =
6562 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 4);
6564 AnimationScaleFactorTrackingLayerImpl* parent_raw = parent.get();
6565 AnimationScaleFactorTrackingLayerImpl* child_raw = child.get();
6566 AnimationScaleFactorTrackingLayerImpl* grand_child_raw = grand_child.get();
6568 child->AddChild(grand_child.Pass());
6569 parent->AddChild(child.Pass());
6570 grand_parent->AddChild(parent.Pass());
6572 SetLayerPropertiesForTesting(grand_parent.get(), identity_matrix,
6573 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
6574 true, false, true);
6575 SetLayerPropertiesForTesting(parent_raw, identity_matrix, gfx::Point3F(),
6576 gfx::PointF(), gfx::Size(1, 2), true, false,
6577 false);
6578 SetLayerPropertiesForTesting(child_raw, identity_matrix, gfx::Point3F(),
6579 gfx::PointF(), gfx::Size(1, 2), true, false,
6580 false);
6582 SetLayerPropertiesForTesting(grand_child_raw, identity_matrix, gfx::Point3F(),
6583 gfx::PointF(), gfx::Size(1, 2), true, false,
6584 false);
6586 ExecuteCalculateDrawProperties(grand_parent.get());
6588 // No layers have animations.
6589 EXPECT_EQ(0.f,
6590 grand_parent->draw_properties().maximum_animation_contents_scale);
6591 EXPECT_EQ(0.f,
6592 parent_raw->draw_properties().maximum_animation_contents_scale);
6593 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
6594 EXPECT_EQ(
6595 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
6597 TransformOperations translation;
6598 translation.AppendTranslate(1.f, 2.f, 3.f);
6600 AddAnimatedTransformToLayer(
6601 parent_raw, 1.0, TransformOperations(), translation);
6603 // No layers have scale-affecting animations.
6604 EXPECT_EQ(0.f,
6605 grand_parent->draw_properties().maximum_animation_contents_scale);
6606 EXPECT_EQ(0.f,
6607 parent_raw->draw_properties().maximum_animation_contents_scale);
6608 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
6609 EXPECT_EQ(
6610 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
6612 TransformOperations scale;
6613 scale.AppendScale(5.f, 4.f, 3.f);
6615 AddAnimatedTransformToLayer(child_raw, 1.0, TransformOperations(), scale);
6616 ExecuteCalculateDrawProperties(grand_parent.get());
6618 // Only |child| has a scale-affecting animation.
6619 EXPECT_EQ(0.f,
6620 grand_parent->draw_properties().maximum_animation_contents_scale);
6621 EXPECT_EQ(0.f,
6622 parent_raw->draw_properties().maximum_animation_contents_scale);
6623 EXPECT_EQ(5.f, child_raw->draw_properties().maximum_animation_contents_scale);
6624 EXPECT_EQ(
6625 5.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
6627 AddAnimatedTransformToLayer(
6628 grand_parent.get(), 1.0, TransformOperations(), scale);
6629 ExecuteCalculateDrawProperties(grand_parent.get());
6631 // |grand_parent| and |child| have scale-affecting animations.
6632 EXPECT_EQ(5.f,
6633 grand_parent->draw_properties().maximum_animation_contents_scale);
6634 EXPECT_EQ(5.f,
6635 parent_raw->draw_properties().maximum_animation_contents_scale);
6636 // We don't support combining animated scales from two nodes; 0.f means
6637 // that the maximum scale could not be computed.
6638 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
6639 EXPECT_EQ(
6640 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
6642 AddAnimatedTransformToLayer(parent_raw, 1.0, TransformOperations(), scale);
6643 ExecuteCalculateDrawProperties(grand_parent.get());
6645 // |grand_parent|, |parent|, and |child| have scale-affecting animations.
6646 EXPECT_EQ(5.f,
6647 grand_parent->draw_properties().maximum_animation_contents_scale);
6648 EXPECT_EQ(0.f,
6649 parent_raw->draw_properties().maximum_animation_contents_scale);
6650 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
6651 EXPECT_EQ(
6652 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
6654 grand_parent->layer_animation_controller()->AbortAnimations(
6655 Animation::TRANSFORM);
6656 parent_raw->layer_animation_controller()->AbortAnimations(
6657 Animation::TRANSFORM);
6658 child_raw->layer_animation_controller()->AbortAnimations(
6659 Animation::TRANSFORM);
6661 TransformOperations perspective;
6662 perspective.AppendPerspective(10.f);
6664 AddAnimatedTransformToLayer(
6665 child_raw, 1.0, TransformOperations(), perspective);
6666 ExecuteCalculateDrawProperties(grand_parent.get());
6668 // |child| has a scale-affecting animation but computing the maximum of this
6669 // animation is not supported.
6670 EXPECT_EQ(0.f,
6671 grand_parent->draw_properties().maximum_animation_contents_scale);
6672 EXPECT_EQ(0.f,
6673 parent_raw->draw_properties().maximum_animation_contents_scale);
6674 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
6675 EXPECT_EQ(
6676 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
6678 child_raw->layer_animation_controller()->AbortAnimations(
6679 Animation::TRANSFORM);
6681 gfx::Transform scale_matrix;
6682 scale_matrix.Scale(1.f, 2.f);
6683 grand_parent->SetTransform(scale_matrix);
6684 parent_raw->SetTransform(scale_matrix);
6685 grand_parent->layer_tree_impl()->property_trees()->needs_rebuild = true;
6686 AddAnimatedTransformToLayer(parent_raw, 1.0, TransformOperations(), scale);
6687 ExecuteCalculateDrawProperties(grand_parent.get());
6689 // |grand_parent| and |parent| each have scale 2.f. |parent| has a scale
6690 // animation with maximum scale 5.f.
6691 EXPECT_EQ(0.f,
6692 grand_parent->draw_properties().maximum_animation_contents_scale);
6693 EXPECT_EQ(10.f,
6694 parent_raw->draw_properties().maximum_animation_contents_scale);
6695 EXPECT_EQ(10.f,
6696 child_raw->draw_properties().maximum_animation_contents_scale);
6697 EXPECT_EQ(
6698 10.f,
6699 grand_child_raw->draw_properties().maximum_animation_contents_scale);
6701 gfx::Transform perspective_matrix;
6702 perspective_matrix.ApplyPerspectiveDepth(2.f);
6703 child_raw->SetTransform(perspective_matrix);
6704 grand_parent->layer_tree_impl()->property_trees()->needs_rebuild = true;
6705 ExecuteCalculateDrawProperties(grand_parent.get());
6707 // |child| has a transform that's neither a translation nor a scale.
6708 EXPECT_EQ(0.f,
6709 grand_parent->draw_properties().maximum_animation_contents_scale);
6710 EXPECT_EQ(10.f,
6711 parent_raw->draw_properties().maximum_animation_contents_scale);
6712 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
6713 EXPECT_EQ(
6714 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
6716 parent_raw->SetTransform(perspective_matrix);
6717 grand_parent->layer_tree_impl()->property_trees()->needs_rebuild = true;
6718 ExecuteCalculateDrawProperties(grand_parent.get());
6720 // |parent| and |child| have transforms that are neither translations nor
6721 // scales.
6722 EXPECT_EQ(0.f,
6723 grand_parent->draw_properties().maximum_animation_contents_scale);
6724 EXPECT_EQ(0.f,
6725 parent_raw->draw_properties().maximum_animation_contents_scale);
6726 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
6727 EXPECT_EQ(
6728 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
6730 parent_raw->SetTransform(identity_matrix);
6731 child_raw->SetTransform(identity_matrix);
6732 grand_parent->SetTransform(perspective_matrix);
6733 grand_parent->layer_tree_impl()->property_trees()->needs_rebuild = true;
6735 ExecuteCalculateDrawProperties(grand_parent.get());
6737 // |grand_parent| has a transform that's neither a translation nor a scale.
6738 EXPECT_EQ(0.f,
6739 grand_parent->draw_properties().maximum_animation_contents_scale);
6740 EXPECT_EQ(0.f,
6741 parent_raw->draw_properties().maximum_animation_contents_scale);
6742 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
6743 EXPECT_EQ(
6744 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
6747 static int membership_id(LayerImpl* layer) {
6748 return layer->draw_properties().last_drawn_render_surface_layer_list_id;
6751 static void GatherDrawnLayers(LayerImplList* rsll,
6752 std::set<LayerImpl*>* drawn_layers) {
6753 for (LayerIterator it = LayerIterator::Begin(rsll),
6754 end = LayerIterator::End(rsll);
6755 it != end; ++it) {
6756 LayerImpl* layer = *it;
6757 if (it.represents_itself())
6758 drawn_layers->insert(layer);
6760 if (!it.represents_contributing_render_surface())
6761 continue;
6763 if (layer->mask_layer())
6764 drawn_layers->insert(layer->mask_layer());
6765 if (layer->replica_layer() && layer->replica_layer()->mask_layer())
6766 drawn_layers->insert(layer->replica_layer()->mask_layer());
6770 TEST_F(LayerTreeHostCommonTest, RenderSurfaceLayerListMembership) {
6771 FakeImplProxy proxy;
6772 TestSharedBitmapManager shared_bitmap_manager;
6773 TestTaskGraphRunner task_graph_runner;
6774 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
6775 &task_graph_runner);
6776 gfx::Transform identity_matrix;
6778 scoped_ptr<LayerImpl> grand_parent =
6779 LayerImpl::Create(host_impl.active_tree(), 1);
6780 scoped_ptr<LayerImpl> parent = LayerImpl::Create(host_impl.active_tree(), 3);
6781 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.active_tree(), 5);
6782 scoped_ptr<LayerImpl> grand_child1 =
6783 LayerImpl::Create(host_impl.active_tree(), 7);
6784 scoped_ptr<LayerImpl> grand_child2 =
6785 LayerImpl::Create(host_impl.active_tree(), 9);
6787 LayerImpl* grand_parent_raw = grand_parent.get();
6788 LayerImpl* parent_raw = parent.get();
6789 LayerImpl* child_raw = child.get();
6790 LayerImpl* grand_child1_raw = grand_child1.get();
6791 LayerImpl* grand_child2_raw = grand_child2.get();
6793 child->AddChild(grand_child1.Pass());
6794 child->AddChild(grand_child2.Pass());
6795 parent->AddChild(child.Pass());
6796 grand_parent->AddChild(parent.Pass());
6798 SetLayerPropertiesForTesting(grand_parent_raw, identity_matrix,
6799 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
6800 true, false, true);
6801 SetLayerPropertiesForTesting(parent_raw, identity_matrix, gfx::Point3F(),
6802 gfx::PointF(), gfx::Size(1, 2), true, false,
6803 false);
6805 SetLayerPropertiesForTesting(child_raw, identity_matrix, gfx::Point3F(),
6806 gfx::PointF(), gfx::Size(1, 2), true, false,
6807 false);
6809 SetLayerPropertiesForTesting(grand_child1_raw, identity_matrix,
6810 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
6811 true, false, false);
6813 SetLayerPropertiesForTesting(grand_child2_raw, identity_matrix,
6814 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
6815 true, false, false);
6817 // Start with nothing being drawn.
6818 ExecuteCalculateDrawProperties(grand_parent_raw);
6819 int member_id = render_surface_layer_list_count();
6821 EXPECT_NE(member_id, membership_id(grand_parent_raw));
6822 EXPECT_NE(member_id, membership_id(parent_raw));
6823 EXPECT_NE(member_id, membership_id(child_raw));
6824 EXPECT_NE(member_id, membership_id(grand_child1_raw));
6825 EXPECT_NE(member_id, membership_id(grand_child2_raw));
6827 std::set<LayerImpl*> expected;
6828 std::set<LayerImpl*> actual;
6829 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
6830 EXPECT_EQ(expected, actual);
6832 // If we force render surface, but none of the layers are in the layer list,
6833 // then this layer should not appear in RSLL.
6834 grand_child1_raw->SetHasRenderSurface(true);
6836 ExecuteCalculateDrawProperties(grand_parent_raw);
6837 member_id = render_surface_layer_list_count();
6839 EXPECT_NE(member_id, membership_id(grand_parent_raw));
6840 EXPECT_NE(member_id, membership_id(parent_raw));
6841 EXPECT_NE(member_id, membership_id(child_raw));
6842 EXPECT_NE(member_id, membership_id(grand_child1_raw));
6843 EXPECT_NE(member_id, membership_id(grand_child2_raw));
6845 expected.clear();
6846 actual.clear();
6847 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
6848 EXPECT_EQ(expected, actual);
6850 // However, if we say that this layer also draws content, it will appear in
6851 // RSLL.
6852 grand_child1_raw->SetDrawsContent(true);
6854 ExecuteCalculateDrawProperties(grand_parent_raw);
6855 member_id = render_surface_layer_list_count();
6857 EXPECT_NE(member_id, membership_id(grand_parent_raw));
6858 EXPECT_NE(member_id, membership_id(parent_raw));
6859 EXPECT_NE(member_id, membership_id(child_raw));
6860 EXPECT_EQ(member_id, membership_id(grand_child1_raw));
6861 EXPECT_NE(member_id, membership_id(grand_child2_raw));
6863 expected.clear();
6864 expected.insert(grand_child1_raw);
6866 actual.clear();
6867 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
6868 EXPECT_EQ(expected, actual);
6870 // Now child is forced to have a render surface, and one if its children draws
6871 // content.
6872 grand_child1_raw->SetDrawsContent(false);
6873 grand_child1_raw->SetHasRenderSurface(false);
6874 child_raw->SetHasRenderSurface(true);
6875 grand_child2_raw->SetDrawsContent(true);
6877 ExecuteCalculateDrawProperties(grand_parent_raw);
6878 member_id = render_surface_layer_list_count();
6880 EXPECT_NE(member_id, membership_id(grand_parent_raw));
6881 EXPECT_NE(member_id, membership_id(parent_raw));
6882 EXPECT_NE(member_id, membership_id(child_raw));
6883 EXPECT_NE(member_id, membership_id(grand_child1_raw));
6884 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
6886 expected.clear();
6887 expected.insert(grand_child2_raw);
6889 actual.clear();
6890 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
6891 EXPECT_EQ(expected, actual);
6893 // Add a mask layer to child.
6894 child_raw->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 6).Pass());
6895 child_raw->layer_tree_impl()->property_trees()->needs_rebuild = true;
6897 ExecuteCalculateDrawProperties(grand_parent_raw);
6898 member_id = render_surface_layer_list_count();
6900 EXPECT_NE(member_id, membership_id(grand_parent_raw));
6901 EXPECT_NE(member_id, membership_id(parent_raw));
6902 EXPECT_NE(member_id, membership_id(child_raw));
6903 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
6904 EXPECT_NE(member_id, membership_id(grand_child1_raw));
6905 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
6907 expected.clear();
6908 expected.insert(grand_child2_raw);
6909 expected.insert(child_raw->mask_layer());
6911 expected.clear();
6912 expected.insert(grand_child2_raw);
6913 expected.insert(child_raw->mask_layer());
6915 actual.clear();
6916 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
6917 EXPECT_EQ(expected, actual);
6919 // Add replica mask layer.
6920 scoped_ptr<LayerImpl> replica_layer =
6921 LayerImpl::Create(host_impl.active_tree(), 20);
6922 replica_layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 21));
6923 child_raw->SetReplicaLayer(replica_layer.Pass());
6925 ExecuteCalculateDrawProperties(grand_parent_raw);
6926 member_id = render_surface_layer_list_count();
6928 EXPECT_NE(member_id, membership_id(grand_parent_raw));
6929 EXPECT_NE(member_id, membership_id(parent_raw));
6930 EXPECT_NE(member_id, membership_id(child_raw));
6931 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
6932 EXPECT_EQ(member_id, membership_id(child_raw->replica_layer()->mask_layer()));
6933 EXPECT_NE(member_id, membership_id(grand_child1_raw));
6934 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
6936 expected.clear();
6937 expected.insert(grand_child2_raw);
6938 expected.insert(child_raw->mask_layer());
6939 expected.insert(child_raw->replica_layer()->mask_layer());
6941 actual.clear();
6942 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
6943 EXPECT_EQ(expected, actual);
6945 child_raw->TakeReplicaLayer();
6947 // With nothing drawing, we should have no layers.
6948 grand_child2_raw->SetDrawsContent(false);
6950 ExecuteCalculateDrawProperties(grand_parent_raw);
6951 member_id = render_surface_layer_list_count();
6953 EXPECT_NE(member_id, membership_id(grand_parent_raw));
6954 EXPECT_NE(member_id, membership_id(parent_raw));
6955 EXPECT_NE(member_id, membership_id(child_raw));
6956 EXPECT_NE(member_id, membership_id(child_raw->mask_layer()));
6957 EXPECT_NE(member_id, membership_id(grand_child1_raw));
6958 EXPECT_NE(member_id, membership_id(grand_child2_raw));
6960 expected.clear();
6961 actual.clear();
6962 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
6963 EXPECT_EQ(expected, actual);
6965 // Child itself draws means that we should have the child and the mask in the
6966 // list.
6967 child_raw->SetDrawsContent(true);
6969 ExecuteCalculateDrawProperties(grand_parent_raw);
6970 member_id = render_surface_layer_list_count();
6972 EXPECT_NE(member_id, membership_id(grand_parent_raw));
6973 EXPECT_NE(member_id, membership_id(parent_raw));
6974 EXPECT_EQ(member_id, membership_id(child_raw));
6975 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
6976 EXPECT_NE(member_id, membership_id(grand_child1_raw));
6977 EXPECT_NE(member_id, membership_id(grand_child2_raw));
6979 expected.clear();
6980 expected.insert(child_raw);
6981 expected.insert(child_raw->mask_layer());
6982 actual.clear();
6983 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
6984 EXPECT_EQ(expected, actual);
6986 child_raw->TakeMaskLayer();
6987 child_raw->layer_tree_impl()->property_trees()->needs_rebuild = true;
6989 // Now everyone's a member!
6990 grand_parent_raw->SetDrawsContent(true);
6991 parent_raw->SetDrawsContent(true);
6992 child_raw->SetDrawsContent(true);
6993 grand_child1_raw->SetDrawsContent(true);
6994 grand_child2_raw->SetDrawsContent(true);
6996 ExecuteCalculateDrawProperties(grand_parent_raw);
6997 member_id = render_surface_layer_list_count();
6999 EXPECT_EQ(member_id, membership_id(grand_parent_raw));
7000 EXPECT_EQ(member_id, membership_id(parent_raw));
7001 EXPECT_EQ(member_id, membership_id(child_raw));
7002 EXPECT_EQ(member_id, membership_id(grand_child1_raw));
7003 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
7005 expected.clear();
7006 expected.insert(grand_parent_raw);
7007 expected.insert(parent_raw);
7008 expected.insert(child_raw);
7009 expected.insert(grand_child1_raw);
7010 expected.insert(grand_child2_raw);
7012 actual.clear();
7013 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
7014 EXPECT_EQ(expected, actual);
7017 TEST_F(LayerTreeHostCommonTest, DrawPropertyScales) {
7018 FakeImplProxy proxy;
7019 TestSharedBitmapManager shared_bitmap_manager;
7020 TestTaskGraphRunner task_graph_runner;
7021 LayerTreeSettings settings;
7022 settings.layer_transforms_should_scale_layer_contents = true;
7023 FakeLayerTreeHostImpl host_impl(settings, &proxy, &shared_bitmap_manager,
7024 &task_graph_runner);
7026 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
7027 LayerImpl* root_layer = root.get();
7028 scoped_ptr<LayerImpl> child1 = LayerImpl::Create(host_impl.active_tree(), 2);
7029 LayerImpl* child1_layer = child1.get();
7030 scoped_ptr<LayerImpl> child2 = LayerImpl::Create(host_impl.active_tree(), 3);
7031 LayerImpl* child2_layer = child2.get();
7033 root->AddChild(child1.Pass());
7034 root->AddChild(child2.Pass());
7035 root->SetHasRenderSurface(true);
7037 gfx::Transform identity_matrix, scale_transform_child1,
7038 scale_transform_child2;
7039 scale_transform_child1.Scale(2, 3);
7040 scale_transform_child2.Scale(4, 5);
7042 SetLayerPropertiesForTesting(root_layer, identity_matrix, gfx::Point3F(),
7043 gfx::PointF(), gfx::Size(1, 1), true, false,
7044 true);
7045 SetLayerPropertiesForTesting(child1_layer, scale_transform_child1,
7046 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
7047 false, false);
7049 child1_layer->SetMaskLayer(
7050 LayerImpl::Create(host_impl.active_tree(), 4).Pass());
7052 scoped_ptr<LayerImpl> replica_layer =
7053 LayerImpl::Create(host_impl.active_tree(), 5);
7054 replica_layer->SetHasRenderSurface(true);
7055 replica_layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 6));
7056 child1_layer->SetReplicaLayer(replica_layer.Pass());
7057 child1_layer->SetHasRenderSurface(true);
7059 ExecuteCalculateDrawProperties(root_layer);
7061 TransformOperations scale;
7062 scale.AppendScale(5.f, 8.f, 3.f);
7064 AddAnimatedTransformToLayer(child2_layer, 1.0, TransformOperations(), scale);
7065 SetLayerPropertiesForTesting(child2_layer, scale_transform_child2,
7066 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
7067 false, false);
7069 ExecuteCalculateDrawProperties(root_layer);
7071 EXPECT_FLOAT_EQ(1.f, root_layer->GetIdealContentsScale());
7072 EXPECT_FLOAT_EQ(3.f, child1_layer->GetIdealContentsScale());
7073 EXPECT_FLOAT_EQ(3.f, child1_layer->mask_layer()->GetIdealContentsScale());
7074 EXPECT_FLOAT_EQ(5.f, child2_layer->GetIdealContentsScale());
7076 EXPECT_FLOAT_EQ(
7077 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
7078 EXPECT_FLOAT_EQ(
7079 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
7080 EXPECT_FLOAT_EQ(0.f,
7081 child1_layer->mask_layer()
7082 ->draw_properties()
7083 .maximum_animation_contents_scale);
7084 EXPECT_FLOAT_EQ(0.f,
7085 child1_layer->replica_layer()
7086 ->mask_layer()
7087 ->draw_properties()
7088 .maximum_animation_contents_scale);
7089 EXPECT_FLOAT_EQ(
7090 8.f, child2_layer->draw_properties().maximum_animation_contents_scale);
7092 // Changing page-scale would affect ideal_contents_scale and
7093 // maximum_animation_contents_scale.
7095 float page_scale_factor = 3.f;
7096 float device_scale_factor = 1.0f;
7097 std::vector<LayerImpl*> render_surface_layer_list;
7098 gfx::Size device_viewport_size =
7099 gfx::Size(root_layer->bounds().width() * device_scale_factor,
7100 root_layer->bounds().height() * device_scale_factor);
7101 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7102 root_layer, device_viewport_size, &render_surface_layer_list);
7104 inputs.page_scale_factor = page_scale_factor;
7105 inputs.can_adjust_raster_scales = true;
7106 inputs.page_scale_layer = root_layer;
7107 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7109 EXPECT_FLOAT_EQ(3.f, root_layer->GetIdealContentsScale());
7110 EXPECT_FLOAT_EQ(9.f, child1_layer->GetIdealContentsScale());
7111 EXPECT_FLOAT_EQ(9.f, child1_layer->mask_layer()->GetIdealContentsScale());
7112 EXPECT_FLOAT_EQ(
7113 9.f,
7114 child1_layer->replica_layer()->mask_layer()->GetIdealContentsScale());
7115 EXPECT_FLOAT_EQ(15.f, child2_layer->GetIdealContentsScale());
7117 EXPECT_FLOAT_EQ(
7118 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
7119 EXPECT_FLOAT_EQ(
7120 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
7121 EXPECT_FLOAT_EQ(0.f,
7122 child1_layer->mask_layer()
7123 ->draw_properties()
7124 .maximum_animation_contents_scale);
7125 EXPECT_FLOAT_EQ(0.f,
7126 child1_layer->replica_layer()
7127 ->mask_layer()
7128 ->draw_properties()
7129 .maximum_animation_contents_scale);
7130 EXPECT_FLOAT_EQ(
7131 24.f, child2_layer->draw_properties().maximum_animation_contents_scale);
7133 // Changing device-scale would affect ideal_contents_scale and
7134 // maximum_animation_contents_scale.
7136 device_scale_factor = 4.0f;
7137 inputs.device_scale_factor = device_scale_factor;
7138 inputs.can_adjust_raster_scales = true;
7139 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7141 EXPECT_FLOAT_EQ(12.f, root_layer->GetIdealContentsScale());
7142 EXPECT_FLOAT_EQ(36.f, child1_layer->GetIdealContentsScale());
7143 EXPECT_FLOAT_EQ(36.f, child1_layer->mask_layer()->GetIdealContentsScale());
7144 EXPECT_FLOAT_EQ(
7145 36.f,
7146 child1_layer->replica_layer()->mask_layer()->GetIdealContentsScale());
7147 EXPECT_FLOAT_EQ(60.f, child2_layer->GetIdealContentsScale());
7149 EXPECT_FLOAT_EQ(
7150 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
7151 EXPECT_FLOAT_EQ(
7152 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
7153 EXPECT_FLOAT_EQ(0.f,
7154 child1_layer->mask_layer()
7155 ->draw_properties()
7156 .maximum_animation_contents_scale);
7157 EXPECT_FLOAT_EQ(0.f,
7158 child1_layer->replica_layer()
7159 ->mask_layer()
7160 ->draw_properties()
7161 .maximum_animation_contents_scale);
7162 EXPECT_FLOAT_EQ(
7163 96.f, child2_layer->draw_properties().maximum_animation_contents_scale);
7166 TEST_F(LayerTreeHostCommonTest, VisibleContentRectInChildRenderSurface) {
7167 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7168 SetLayerPropertiesForTesting(root.get(),
7169 gfx::Transform(),
7170 gfx::Point3F(),
7171 gfx::PointF(),
7172 gfx::Size(768 / 2, 3000),
7173 true,
7174 false);
7175 root->SetIsDrawable(true);
7177 scoped_refptr<Layer> clip = Layer::Create(layer_settings());
7178 SetLayerPropertiesForTesting(clip.get(),
7179 gfx::Transform(),
7180 gfx::Point3F(),
7181 gfx::PointF(),
7182 gfx::Size(768 / 2, 10000),
7183 true,
7184 false);
7185 clip->SetMasksToBounds(true);
7187 scoped_refptr<Layer> content = Layer::Create(layer_settings());
7188 SetLayerPropertiesForTesting(content.get(),
7189 gfx::Transform(),
7190 gfx::Point3F(),
7191 gfx::PointF(),
7192 gfx::Size(768 / 2, 10000),
7193 true,
7194 false);
7195 content->SetIsDrawable(true);
7196 content->SetForceRenderSurface(true);
7198 root->AddChild(clip);
7199 clip->AddChild(content);
7201 host()->SetRootLayer(root);
7203 gfx::Size device_viewport_size(768, 582);
7204 RenderSurfaceLayerList render_surface_layer_list;
7205 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
7206 host()->root_layer(), device_viewport_size, &render_surface_layer_list);
7207 inputs.device_scale_factor = 2.f;
7208 inputs.page_scale_factor = 1.f;
7209 inputs.page_scale_layer = NULL;
7210 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7212 // Layers in the root render surface have their visible content rect clipped
7213 // by the viewport.
7214 EXPECT_EQ(gfx::Rect(768 / 2, 582 / 2), root->visible_layer_rect());
7216 // Layers drawing to a child render surface should still have their visible
7217 // content rect clipped by the viewport.
7218 EXPECT_EQ(gfx::Rect(768 / 2, 582 / 2), content->visible_layer_rect());
7221 TEST_F(LayerTreeHostCommonTest, BoundsDeltaAffectVisibleContentRect) {
7222 FakeImplProxy proxy;
7223 TestSharedBitmapManager shared_bitmap_manager;
7224 TestTaskGraphRunner task_graph_runner;
7225 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
7226 &task_graph_runner);
7228 // Set two layers: the root layer clips it's child,
7229 // the child draws its content.
7231 gfx::Size root_size = gfx::Size(300, 500);
7233 // Sublayer should be bigger than the root enlarged by bounds_delta.
7234 gfx::Size sublayer_size = gfx::Size(300, 1000);
7236 // Device viewport accomidated the root and the top controls.
7237 gfx::Size device_viewport_size = gfx::Size(300, 600);
7238 gfx::Transform identity_matrix;
7240 host_impl.SetViewportSize(device_viewport_size);
7241 host_impl.active_tree()->SetRootLayer(
7242 LayerImpl::Create(host_impl.active_tree(), 1));
7244 LayerImpl* root = host_impl.active_tree()->root_layer();
7245 SetLayerPropertiesForTesting(root,
7246 identity_matrix,
7247 gfx::Point3F(),
7248 gfx::PointF(),
7249 root_size,
7250 false,
7251 false,
7252 true);
7253 root->SetMasksToBounds(true);
7255 root->AddChild(LayerImpl::Create(host_impl.active_tree(), 2));
7257 LayerImpl* sublayer = root->child_at(0);
7258 SetLayerPropertiesForTesting(sublayer,
7259 identity_matrix,
7260 gfx::Point3F(),
7261 gfx::PointF(),
7262 sublayer_size,
7263 false,
7264 false,
7265 false);
7266 sublayer->SetDrawsContent(true);
7268 LayerImplList layer_impl_list;
7269 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7270 root, device_viewport_size, &layer_impl_list);
7272 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7274 EXPECT_EQ(gfx::Rect(root_size), sublayer->visible_layer_rect());
7276 root->SetBoundsDelta(gfx::Vector2dF(0.0, 50.0));
7278 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7280 gfx::Rect affected_by_delta(0, 0, root_size.width(),
7281 root_size.height() + 50);
7282 EXPECT_EQ(affected_by_delta, sublayer->visible_layer_rect());
7285 TEST_F(LayerTreeHostCommonTest, NodesAffectedByBoundsDeltaGetUpdated) {
7286 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7287 scoped_refptr<Layer> inner_viewport_container_layer =
7288 Layer::Create(layer_settings());
7289 scoped_refptr<Layer> inner_viewport_scroll_layer =
7290 Layer::Create(layer_settings());
7291 scoped_refptr<Layer> outer_viewport_container_layer =
7292 Layer::Create(layer_settings());
7293 scoped_refptr<Layer> outer_viewport_scroll_layer =
7294 Layer::Create(layer_settings());
7296 root->AddChild(inner_viewport_container_layer);
7297 inner_viewport_container_layer->AddChild(inner_viewport_scroll_layer);
7298 inner_viewport_scroll_layer->AddChild(outer_viewport_container_layer);
7299 outer_viewport_container_layer->AddChild(outer_viewport_scroll_layer);
7301 inner_viewport_scroll_layer->SetScrollClipLayerId(
7302 inner_viewport_container_layer->id());
7303 outer_viewport_scroll_layer->SetScrollClipLayerId(
7304 outer_viewport_container_layer->id());
7306 inner_viewport_scroll_layer->SetIsContainerForFixedPositionLayers(true);
7307 outer_viewport_scroll_layer->SetIsContainerForFixedPositionLayers(true);
7309 host()->SetRootLayer(root);
7310 host()->RegisterViewportLayers(nullptr, root, inner_viewport_scroll_layer,
7311 outer_viewport_scroll_layer);
7313 scoped_refptr<Layer> fixed_to_inner = Layer::Create(layer_settings());
7314 scoped_refptr<Layer> fixed_to_outer = Layer::Create(layer_settings());
7316 inner_viewport_scroll_layer->AddChild(fixed_to_inner);
7317 outer_viewport_scroll_layer->AddChild(fixed_to_outer);
7319 LayerPositionConstraint fixed_to_right;
7320 fixed_to_right.set_is_fixed_position(true);
7321 fixed_to_right.set_is_fixed_to_right_edge(true);
7323 fixed_to_inner->SetPositionConstraint(fixed_to_right);
7324 fixed_to_outer->SetPositionConstraint(fixed_to_right);
7326 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7328 TransformTree& transform_tree = host()->property_trees()->transform_tree;
7329 EXPECT_TRUE(transform_tree.HasNodesAffectedByInnerViewportBoundsDelta());
7330 EXPECT_TRUE(transform_tree.HasNodesAffectedByOuterViewportBoundsDelta());
7332 LayerPositionConstraint fixed_to_left;
7333 fixed_to_left.set_is_fixed_position(true);
7334 fixed_to_inner->SetPositionConstraint(fixed_to_left);
7336 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7337 EXPECT_FALSE(transform_tree.HasNodesAffectedByInnerViewportBoundsDelta());
7338 EXPECT_TRUE(transform_tree.HasNodesAffectedByOuterViewportBoundsDelta());
7340 fixed_to_outer->SetPositionConstraint(fixed_to_left);
7342 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7343 EXPECT_FALSE(transform_tree.HasNodesAffectedByInnerViewportBoundsDelta());
7344 EXPECT_FALSE(transform_tree.HasNodesAffectedByOuterViewportBoundsDelta());
7347 TEST_F(LayerTreeHostCommonTest, VisibleContentRectForAnimatedLayer) {
7348 const gfx::Transform identity_matrix;
7349 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7350 scoped_refptr<LayerWithForcedDrawsContent> animated =
7351 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7353 root->AddChild(animated);
7355 host()->SetRootLayer(root);
7357 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
7358 gfx::PointF(), gfx::Size(100, 100), true, false);
7359 SetLayerPropertiesForTesting(animated.get(), identity_matrix, gfx::Point3F(),
7360 gfx::PointF(), gfx::Size(20, 20), true, false);
7362 root->SetMasksToBounds(true);
7363 root->SetForceRenderSurface(true);
7364 animated->SetOpacity(0.f);
7366 AddOpacityTransitionToController(animated->layer_animation_controller(), 10.0,
7367 0.f, 1.f, false);
7369 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7371 EXPECT_FALSE(animated->visible_rect_from_property_trees().IsEmpty());
7374 TEST_F(LayerTreeHostCommonTest,
7375 VisibleContentRectForAnimatedLayerWithSingularTransform) {
7376 const gfx::Transform identity_matrix;
7377 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7378 scoped_refptr<Layer> clip = Layer::Create(layer_settings());
7379 scoped_refptr<LayerWithForcedDrawsContent> animated =
7380 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7381 scoped_refptr<LayerWithForcedDrawsContent> surface =
7382 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7383 scoped_refptr<LayerWithForcedDrawsContent> descendant_of_animation =
7384 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7386 root->AddChild(clip);
7387 clip->AddChild(animated);
7388 animated->AddChild(surface);
7389 surface->AddChild(descendant_of_animation);
7391 clip->SetMasksToBounds(true);
7392 surface->SetForceRenderSurface(true);
7394 host()->SetRootLayer(root);
7396 gfx::Transform uninvertible_matrix;
7397 uninvertible_matrix.Scale3d(6.f, 6.f, 0.f);
7399 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
7400 gfx::PointF(), gfx::Size(100, 100), true, false);
7401 SetLayerPropertiesForTesting(clip.get(), identity_matrix, gfx::Point3F(),
7402 gfx::PointF(), gfx::Size(10, 10), true, false);
7403 SetLayerPropertiesForTesting(animated.get(), uninvertible_matrix,
7404 gfx::Point3F(), gfx::PointF(),
7405 gfx::Size(120, 120), true, false);
7406 SetLayerPropertiesForTesting(surface.get(), identity_matrix, gfx::Point3F(),
7407 gfx::PointF(), gfx::Size(100, 100), true, false);
7408 SetLayerPropertiesForTesting(descendant_of_animation.get(), identity_matrix,
7409 gfx::Point3F(), gfx::PointF(),
7410 gfx::Size(200, 200), true, false);
7412 TransformOperations start_transform_operations;
7413 start_transform_operations.AppendMatrix(uninvertible_matrix);
7414 TransformOperations end_transform_operations;
7416 AddAnimatedTransformToLayer(animated.get(), 10.0, start_transform_operations,
7417 end_transform_operations);
7419 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7421 // The animated layer has a singular transform and maps to a non-empty rect in
7422 // clipped target space, so is treated as fully visible.
7423 EXPECT_EQ(gfx::Rect(120, 120), animated->visible_rect_from_property_trees());
7425 // The singular transform on |animated| is flattened when inherited by
7426 // |surface|, and this happens to make it invertible.
7427 EXPECT_EQ(gfx::Rect(2, 2), surface->visible_rect_from_property_trees());
7428 EXPECT_EQ(gfx::Rect(2, 2),
7429 descendant_of_animation->visible_rect_from_property_trees());
7431 gfx::Transform zero_matrix;
7432 zero_matrix.Scale3d(0.f, 0.f, 0.f);
7433 SetLayerPropertiesForTesting(animated.get(), zero_matrix, gfx::Point3F(),
7434 gfx::PointF(), gfx::Size(120, 120), true, false);
7436 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7438 // The animated layer maps to the empty rect in clipped target space, so is
7439 // treated as having an empty visible rect.
7440 EXPECT_EQ(gfx::Rect(), animated->visible_rect_from_property_trees());
7442 // This time, flattening does not make |animated|'s transform invertible. This
7443 // means the clip cannot be projected into |surface|'s space, so we treat
7444 // |surface| and layers that draw into it as fully visible.
7445 EXPECT_EQ(gfx::Rect(100, 100), surface->visible_rect_from_property_trees());
7446 EXPECT_EQ(gfx::Rect(200, 200),
7447 descendant_of_animation->visible_rect_from_property_trees());
7450 // Verify that having an animated filter (but no current filter, as these
7451 // are mutually exclusive) correctly creates a render surface.
7452 TEST_F(LayerTreeHostCommonTest, AnimatedFilterCreatesRenderSurface) {
7453 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7454 scoped_refptr<Layer> child = Layer::Create(layer_settings());
7455 scoped_refptr<Layer> grandchild = Layer::Create(layer_settings());
7456 root->AddChild(child);
7457 child->AddChild(grandchild);
7459 gfx::Transform identity_transform;
7460 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7461 gfx::PointF(), gfx::Size(50, 50), true, false);
7462 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
7463 gfx::PointF(), gfx::Size(50, 50), true, false);
7464 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
7465 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
7466 true, false);
7467 host()->SetRootLayer(root);
7469 AddAnimatedFilterToLayer(child.get(), 10.0, 0.1f, 0.2f);
7471 ExecuteCalculateDrawProperties(root.get());
7473 EXPECT_TRUE(root->render_surface());
7474 EXPECT_TRUE(child->render_surface());
7475 EXPECT_FALSE(grandchild->render_surface());
7477 EXPECT_TRUE(root->filters().IsEmpty());
7478 EXPECT_TRUE(child->filters().IsEmpty());
7479 EXPECT_TRUE(grandchild->filters().IsEmpty());
7481 EXPECT_FALSE(root->FilterIsAnimating());
7482 EXPECT_TRUE(child->FilterIsAnimating());
7483 EXPECT_FALSE(grandchild->FilterIsAnimating());
7486 // Verify that having a filter animation with a delayed start time creates a
7487 // render surface.
7488 TEST_F(LayerTreeHostCommonTest, DelayedFilterAnimationCreatesRenderSurface) {
7489 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7490 scoped_refptr<Layer> child = Layer::Create(layer_settings());
7491 scoped_refptr<Layer> grandchild = Layer::Create(layer_settings());
7492 root->AddChild(child);
7493 child->AddChild(grandchild);
7495 gfx::Transform identity_transform;
7496 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7497 gfx::PointF(), gfx::Size(50, 50), true, false);
7498 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
7499 gfx::PointF(), gfx::Size(50, 50), true, false);
7500 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
7501 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
7502 true, false);
7503 host()->SetRootLayer(root);
7505 scoped_ptr<KeyframedFilterAnimationCurve> curve(
7506 KeyframedFilterAnimationCurve::Create());
7507 FilterOperations start_filters;
7508 start_filters.Append(FilterOperation::CreateBrightnessFilter(0.1f));
7509 FilterOperations end_filters;
7510 end_filters.Append(FilterOperation::CreateBrightnessFilter(0.3f));
7511 curve->AddKeyframe(
7512 FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
7513 curve->AddKeyframe(FilterKeyframe::Create(
7514 base::TimeDelta::FromMilliseconds(100), end_filters, nullptr));
7515 scoped_ptr<Animation> animation =
7516 Animation::Create(curve.Pass(), 0, 1, Animation::FILTER);
7517 animation->set_fill_mode(Animation::FILL_MODE_NONE);
7518 animation->set_time_offset(base::TimeDelta::FromMilliseconds(-1000));
7519 child->layer_animation_controller()->AddAnimation(animation.Pass());
7521 ExecuteCalculateDrawProperties(root.get());
7523 EXPECT_TRUE(root->render_surface());
7524 EXPECT_TRUE(child->render_surface());
7525 EXPECT_FALSE(grandchild->render_surface());
7527 EXPECT_TRUE(root->filters().IsEmpty());
7528 EXPECT_TRUE(child->filters().IsEmpty());
7529 EXPECT_TRUE(grandchild->filters().IsEmpty());
7531 EXPECT_FALSE(root->FilterIsAnimating());
7532 EXPECT_FALSE(root->HasPotentiallyRunningFilterAnimation());
7533 EXPECT_FALSE(child->FilterIsAnimating());
7534 EXPECT_TRUE(child->HasPotentiallyRunningFilterAnimation());
7535 EXPECT_FALSE(grandchild->FilterIsAnimating());
7536 EXPECT_FALSE(grandchild->HasPotentiallyRunningFilterAnimation());
7539 // Ensures that the property tree code accounts for offsets between fixed
7540 // position layers and their respective containers.
7541 TEST_F(LayerTreeHostCommonTest, PropertyTreesAccountForFixedParentOffset) {
7542 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7543 scoped_refptr<Layer> child = Layer::Create(layer_settings());
7544 scoped_refptr<LayerWithForcedDrawsContent> grandchild =
7545 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7547 root->AddChild(child);
7548 child->AddChild(grandchild);
7550 gfx::Transform identity_transform;
7551 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7552 gfx::PointF(), gfx::Size(50, 50), true, false);
7553 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
7554 gfx::PointF(1000, 1000), gfx::Size(50, 50), true,
7555 false);
7556 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
7557 gfx::Point3F(), gfx::PointF(-1000, -1000),
7558 gfx::Size(50, 50), true, false);
7560 root->SetMasksToBounds(true);
7561 root->SetIsContainerForFixedPositionLayers(true);
7562 LayerPositionConstraint constraint;
7563 constraint.set_is_fixed_position(true);
7564 grandchild->SetPositionConstraint(constraint);
7566 root->SetIsContainerForFixedPositionLayers(true);
7568 host()->SetRootLayer(root);
7570 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7572 EXPECT_EQ(gfx::Rect(0, 0, 50, 50),
7573 grandchild->visible_rect_from_property_trees());
7576 // Ensures that the property tree code accounts for offsets between fixed
7577 // position containers and their transform tree parents, when a fixed position
7578 // layer's container is its layer tree parent, but this parent doesn't have its
7579 // own transform tree node.
7580 TEST_F(LayerTreeHostCommonTest,
7581 PropertyTreesAccountForFixedParentOffsetWhenContainerIsParent) {
7582 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7583 scoped_refptr<Layer> child = Layer::Create(layer_settings());
7584 scoped_refptr<LayerWithForcedDrawsContent> grandchild =
7585 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7587 root->AddChild(child);
7588 child->AddChild(grandchild);
7590 gfx::Transform identity_transform;
7591 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7592 gfx::PointF(), gfx::Size(50, 50), true, false);
7593 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
7594 gfx::PointF(1000, 1000), gfx::Size(50, 50), true,
7595 false);
7596 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
7597 gfx::Point3F(), gfx::PointF(-1000, -1000),
7598 gfx::Size(50, 50), true, false);
7600 root->SetMasksToBounds(true);
7601 child->SetIsContainerForFixedPositionLayers(true);
7602 LayerPositionConstraint constraint;
7603 constraint.set_is_fixed_position(true);
7604 grandchild->SetPositionConstraint(constraint);
7606 root->SetIsContainerForFixedPositionLayers(true);
7608 host()->SetRootLayer(root);
7610 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7612 EXPECT_EQ(gfx::Rect(0, 0, 50, 50),
7613 grandchild->visible_rect_from_property_trees());
7616 TEST_F(LayerTreeHostCommonTest, CombineClipsUsingContentTarget) {
7617 // In the following layer tree, the layer |box|'s render target is |surface|.
7618 // |surface| also creates a transform node. We want to combine clips for |box|
7619 // in the space of its target (i.e., |surface|), not its target's target. This
7620 // test ensures that happens.
7622 gfx::Transform rotate;
7623 rotate.Rotate(5);
7624 gfx::Transform identity;
7626 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7627 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
7628 gfx::PointF(), gfx::Size(2500, 1500), true,
7629 false);
7631 scoped_refptr<Layer> frame_clip = Layer::Create(layer_settings());
7632 SetLayerPropertiesForTesting(frame_clip.get(), identity, gfx::Point3F(),
7633 gfx::PointF(), gfx::Size(2500, 1500), true,
7634 false);
7635 frame_clip->SetMasksToBounds(true);
7637 scoped_refptr<Layer> rotated = Layer::Create(layer_settings());
7638 SetLayerPropertiesForTesting(rotated.get(), rotate,
7639 gfx::Point3F(1250, 250, 0), gfx::PointF(),
7640 gfx::Size(2500, 500), true, false);
7642 scoped_refptr<Layer> surface = Layer::Create(layer_settings());
7643 SetLayerPropertiesForTesting(surface.get(), rotate, gfx::Point3F(),
7644 gfx::PointF(), gfx::Size(2500, 500), true,
7645 false);
7646 surface->SetOpacity(0.5);
7648 scoped_refptr<LayerWithForcedDrawsContent> container =
7649 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7650 SetLayerPropertiesForTesting(container.get(), identity, gfx::Point3F(),
7651 gfx::PointF(), gfx::Size(300, 300), true, false);
7653 scoped_refptr<LayerWithForcedDrawsContent> box =
7654 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7655 SetLayerPropertiesForTesting(box.get(), identity, gfx::Point3F(),
7656 gfx::PointF(), gfx::Size(100, 100), true, false);
7658 root->AddChild(frame_clip);
7659 frame_clip->AddChild(rotated);
7660 rotated->AddChild(surface);
7661 surface->AddChild(container);
7662 surface->AddChild(box);
7664 host()->SetRootLayer(root);
7666 ExecuteCalculateDrawProperties(root.get());
7669 TEST_F(LayerTreeHostCommonTest, OnlyApplyFixedPositioningOnce) {
7670 gfx::Transform identity;
7671 gfx::Transform translate_z;
7672 translate_z.Translate3d(0, 0, 10);
7674 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7675 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
7676 gfx::PointF(), gfx::Size(800, 800), true, false);
7677 root->SetIsContainerForFixedPositionLayers(true);
7679 scoped_refptr<Layer> frame_clip = Layer::Create(layer_settings());
7680 SetLayerPropertiesForTesting(frame_clip.get(), translate_z, gfx::Point3F(),
7681 gfx::PointF(500, 100), gfx::Size(100, 100), true,
7682 false);
7683 frame_clip->SetMasksToBounds(true);
7685 scoped_refptr<LayerWithForcedDrawsContent> fixed =
7686 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7687 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
7688 gfx::PointF(), gfx::Size(1000, 1000), true,
7689 false);
7691 LayerPositionConstraint constraint;
7692 constraint.set_is_fixed_position(true);
7693 fixed->SetPositionConstraint(constraint);
7695 root->AddChild(frame_clip);
7696 frame_clip->AddChild(fixed);
7698 host()->SetRootLayer(root);
7700 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7702 gfx::Rect expected(0, 0, 100, 100);
7703 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
7706 TEST_F(LayerTreeHostCommonTest,
7707 PropertyTreesAccountForScrollCompensationAdjustment) {
7708 gfx::Transform identity;
7709 gfx::Transform translate_z;
7710 translate_z.Translate3d(0, 0, 10);
7712 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7713 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
7714 gfx::PointF(), gfx::Size(800, 800), true, false);
7715 root->SetIsContainerForFixedPositionLayers(true);
7717 scoped_refptr<Layer> frame_clip = Layer::Create(layer_settings());
7718 SetLayerPropertiesForTesting(frame_clip.get(), translate_z, gfx::Point3F(),
7719 gfx::PointF(500, 100), gfx::Size(100, 100), true,
7720 false);
7721 frame_clip->SetMasksToBounds(true);
7723 scoped_refptr<LayerWithForcedDrawsContent> scroller =
7724 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7725 SetLayerPropertiesForTesting(scroller.get(), identity, gfx::Point3F(),
7726 gfx::PointF(), gfx::Size(1000, 1000), true,
7727 false);
7729 scroller->SetScrollCompensationAdjustment(gfx::Vector2dF(0.3f, 0.7f));
7730 scroller->SetScrollOffset(gfx::ScrollOffset(0.3, 0.7));
7731 scroller->SetScrollClipLayerId(frame_clip->id());
7733 scoped_refptr<LayerWithForcedDrawsContent> fixed =
7734 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7735 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
7736 gfx::PointF(), gfx::Size(50, 50), true, false);
7738 LayerPositionConstraint constraint;
7739 constraint.set_is_fixed_position(true);
7740 fixed->SetPositionConstraint(constraint);
7742 scoped_refptr<LayerWithForcedDrawsContent> fixed_child =
7743 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7744 SetLayerPropertiesForTesting(fixed_child.get(), identity, gfx::Point3F(),
7745 gfx::PointF(), gfx::Size(10, 10), true, false);
7747 fixed_child->SetPositionConstraint(constraint);
7749 root->AddChild(frame_clip);
7750 frame_clip->AddChild(scroller);
7751 scroller->AddChild(fixed);
7752 fixed->AddChild(fixed_child);
7754 host()->SetRootLayer(root);
7756 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7758 gfx::Rect expected(0, 0, 50, 50);
7759 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
7761 expected = gfx::Rect(0, 0, 10, 10);
7762 EXPECT_EQ(expected, fixed_child->visible_rect_from_property_trees());
7765 TEST_F(LayerTreeHostCommonTest, FixedClipsShouldBeAssociatedWithTheRightNode) {
7766 gfx::Transform identity;
7768 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7769 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
7770 gfx::PointF(), gfx::Size(800, 800), true, false);
7771 root->SetIsContainerForFixedPositionLayers(true);
7773 scoped_refptr<Layer> frame_clip = Layer::Create(layer_settings());
7774 SetLayerPropertiesForTesting(frame_clip.get(), identity, gfx::Point3F(),
7775 gfx::PointF(500, 100), gfx::Size(100, 100), true,
7776 false);
7777 frame_clip->SetMasksToBounds(true);
7779 scoped_refptr<LayerWithForcedDrawsContent> scroller =
7780 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7781 SetLayerPropertiesForTesting(scroller.get(), identity, gfx::Point3F(),
7782 gfx::PointF(), gfx::Size(1000, 1000), true,
7783 false);
7785 scroller->SetScrollOffset(gfx::ScrollOffset(100, 100));
7786 scroller->SetScrollClipLayerId(frame_clip->id());
7788 scoped_refptr<LayerWithForcedDrawsContent> fixed =
7789 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7790 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
7791 gfx::PointF(100, 100), gfx::Size(50, 50), true,
7792 false);
7794 LayerPositionConstraint constraint;
7795 constraint.set_is_fixed_position(true);
7796 fixed->SetPositionConstraint(constraint);
7797 fixed->SetForceRenderSurface(true);
7798 fixed->SetMasksToBounds(true);
7800 root->AddChild(frame_clip);
7801 frame_clip->AddChild(scroller);
7802 scroller->AddChild(fixed);
7804 host()->SetRootLayer(root);
7806 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7808 gfx::Rect expected(0, 0, 50, 50);
7809 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
7812 TEST_F(LayerTreeHostCommonTest, ChangingAxisAlignmentTriggersRebuild) {
7813 gfx::Transform identity;
7814 gfx::Transform translate;
7815 gfx::Transform rotate;
7817 translate.Translate(10, 10);
7818 rotate.Rotate(45);
7820 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7821 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
7822 gfx::PointF(), gfx::Size(800, 800), true, false);
7823 root->SetIsContainerForFixedPositionLayers(true);
7825 host()->SetRootLayer(root);
7827 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7828 EXPECT_FALSE(host()->property_trees()->needs_rebuild);
7830 root->SetTransform(translate);
7831 EXPECT_FALSE(host()->property_trees()->needs_rebuild);
7833 root->SetTransform(rotate);
7834 EXPECT_TRUE(host()->property_trees()->needs_rebuild);
7837 TEST_F(LayerTreeHostCommonTest, ChangeTransformOrigin) {
7838 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7839 scoped_refptr<LayerWithForcedDrawsContent> child =
7840 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7841 root->AddChild(child);
7843 host()->SetRootLayer(root);
7845 gfx::Transform identity_matrix;
7846 gfx::Transform scale_matrix;
7847 scale_matrix.Scale(2.f, 2.f);
7848 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
7849 gfx::PointF(), gfx::Size(100, 100), true, false);
7850 SetLayerPropertiesForTesting(child.get(), scale_matrix, gfx::Point3F(),
7851 gfx::PointF(), gfx::Size(10, 10), true, false);
7853 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7854 EXPECT_EQ(gfx::Rect(10, 10), child->visible_rect_from_property_trees());
7856 child->SetTransformOrigin(gfx::Point3F(10.f, 10.f, 10.f));
7858 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7859 EXPECT_EQ(gfx::Rect(5, 5, 5, 5), child->visible_rect_from_property_trees());
7862 TEST_F(LayerTreeHostCommonTest, UpdateScrollChildPosition) {
7863 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7864 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7865 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7866 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7867 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7869 root->AddChild(scroll_child);
7870 root->AddChild(scroll_parent);
7871 scroll_child->SetScrollParent(scroll_parent.get());
7872 scroll_parent->SetScrollClipLayerId(root->id());
7874 host()->SetRootLayer(root);
7876 gfx::Transform identity_transform;
7877 gfx::Transform scale;
7878 scale.Scale(2.f, 2.f);
7879 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7880 gfx::PointF(), gfx::Size(50, 50), true, false);
7881 SetLayerPropertiesForTesting(scroll_child.get(), scale, gfx::Point3F(),
7882 gfx::PointF(), gfx::Size(40, 40), true, false);
7883 SetLayerPropertiesForTesting(scroll_parent.get(), identity_transform,
7884 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
7885 true, false);
7887 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7888 EXPECT_EQ(gfx::Rect(25, 25),
7889 scroll_child->visible_rect_from_property_trees());
7891 scroll_child->SetPosition(gfx::PointF(0, -10.f));
7892 scroll_parent->SetScrollOffset(gfx::ScrollOffset(0.f, 10.f));
7893 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7894 EXPECT_EQ(gfx::Rect(0, 5, 25, 25),
7895 scroll_child->visible_rect_from_property_trees());
7898 static void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
7901 TEST_F(LayerTreeHostCommonTest, SkippingSubtreeMain) {
7902 gfx::Transform identity;
7903 FakeContentLayerClient client;
7904 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7905 scoped_refptr<LayerWithForcedDrawsContent> child =
7906 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7907 scoped_refptr<LayerWithForcedDrawsContent> grandchild =
7908 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7909 scoped_refptr<FakePictureLayer> greatgrandchild(
7910 FakePictureLayer::Create(layer_settings(), &client));
7911 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
7912 gfx::PointF(), gfx::Size(100, 100), true, false);
7913 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
7914 gfx::PointF(), gfx::Size(10, 10), true, false);
7915 SetLayerPropertiesForTesting(grandchild.get(), identity, gfx::Point3F(),
7916 gfx::PointF(), gfx::Size(10, 10), true, false);
7917 SetLayerPropertiesForTesting(greatgrandchild.get(), identity, gfx::Point3F(),
7918 gfx::PointF(), gfx::Size(10, 10), true, false);
7920 root->AddChild(child);
7921 child->AddChild(grandchild);
7922 grandchild->AddChild(greatgrandchild);
7924 host()->SetRootLayer(root);
7926 // Check the non-skipped case.
7927 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7928 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
7930 // Now we will reset the visible rect from property trees for the grandchild,
7931 // and we will configure |child| in several ways that should force the subtree
7932 // to be skipped. The visible content rect for |grandchild| should, therefore,
7933 // remain empty.
7934 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
7935 gfx::Transform singular;
7936 singular.matrix().set(0, 0, 0);
7938 child->SetTransform(singular);
7939 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7940 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
7941 child->SetTransform(identity);
7943 child->SetHideLayerAndSubtree(true);
7944 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7945 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
7946 child->SetHideLayerAndSubtree(false);
7948 gfx::Transform zero_z_scale;
7949 zero_z_scale.Scale3d(1, 1, 0);
7950 child->SetTransform(zero_z_scale);
7952 // Add a transform animation with a start delay. Now, even though |child| has
7953 // a singular transform, the subtree should still get processed.
7954 int animation_id = 0;
7955 scoped_ptr<Animation> animation = Animation::Create(
7956 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
7957 animation_id, 1, Animation::TRANSFORM);
7958 animation->set_fill_mode(Animation::FILL_MODE_NONE);
7959 animation->set_time_offset(base::TimeDelta::FromMilliseconds(-1000));
7960 child->AddAnimation(animation.Pass());
7961 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7962 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
7963 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
7965 child->RemoveAnimation(animation_id);
7966 child->SetTransform(identity);
7967 child->SetOpacity(0.f);
7968 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7969 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
7971 // Now, even though child has zero opacity, we will configure |grandchild| and
7972 // |greatgrandchild| in several ways that should force the subtree to be
7973 // processed anyhow.
7974 grandchild->SetTouchEventHandlerRegion(Region(gfx::Rect(0, 0, 10, 10)));
7975 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7976 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
7977 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
7978 grandchild->SetTouchEventHandlerRegion(Region());
7979 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7980 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
7981 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
7983 greatgrandchild->RequestCopyOfOutput(
7984 CopyOutputRequest::CreateBitmapRequest(base::Bind(&CopyOutputCallback)));
7985 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7986 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
7987 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
7989 // Add an opacity animation with a start delay.
7990 animation_id = 1;
7991 animation = Animation::Create(
7992 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
7993 animation_id, 1, Animation::OPACITY);
7994 animation->set_fill_mode(Animation::FILL_MODE_NONE);
7995 animation->set_time_offset(base::TimeDelta::FromMilliseconds(-1000));
7996 child->AddAnimation(animation.Pass());
7997 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
7998 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
8001 TEST_F(LayerTreeHostCommonTest, SkippingSubtreeImpl) {
8002 FakeImplProxy proxy;
8003 TestSharedBitmapManager shared_bitmap_manager;
8004 TestTaskGraphRunner task_graph_runner;
8005 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
8006 &task_graph_runner);
8008 gfx::Transform identity;
8009 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
8010 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.active_tree(), 2);
8011 scoped_ptr<LayerImpl> grandchild =
8012 LayerImpl::Create(host_impl.active_tree(), 3);
8014 scoped_ptr<FakePictureLayerImpl> greatgrandchild(
8015 FakePictureLayerImpl::Create(host_impl.active_tree(), 4));
8017 child->SetDrawsContent(true);
8018 grandchild->SetDrawsContent(true);
8019 greatgrandchild->SetDrawsContent(true);
8021 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
8022 gfx::PointF(), gfx::Size(100, 100), true, false,
8023 true);
8024 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
8025 gfx::PointF(), gfx::Size(10, 10), true, false,
8026 false);
8027 SetLayerPropertiesForTesting(grandchild.get(), identity, gfx::Point3F(),
8028 gfx::PointF(), gfx::Size(10, 10), true, false,
8029 false);
8030 SetLayerPropertiesForTesting(greatgrandchild.get(), identity, gfx::Point3F(),
8031 gfx::PointF(), gfx::Size(10, 10), true, false,
8032 true);
8034 LayerImpl* child_ptr = child.get();
8035 LayerImpl* grandchild_ptr = grandchild.get();
8036 LayerImpl* greatgrandchild_ptr = greatgrandchild.get();
8038 grandchild->AddChild(greatgrandchild.Pass());
8039 child->AddChild(grandchild.Pass());
8040 root->AddChild(child.Pass());
8042 // Check the non-skipped case.
8043 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8044 EXPECT_EQ(gfx::Rect(10, 10),
8045 grandchild_ptr->visible_rect_from_property_trees());
8047 // Now we will reset the visible rect from property trees for the grandchild,
8048 // and we will configure |child| in several ways that should force the subtree
8049 // to be skipped. The visible content rect for |grandchild| should, therefore,
8050 // remain empty.
8051 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
8052 gfx::Transform singular;
8053 singular.matrix().set(0, 0, 0);
8055 child_ptr->SetTransform(singular);
8056 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8057 EXPECT_EQ(gfx::Rect(0, 0),
8058 grandchild_ptr->visible_rect_from_property_trees());
8059 child_ptr->SetTransform(identity);
8061 child_ptr->SetHideLayerAndSubtree(true);
8062 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8063 EXPECT_EQ(gfx::Rect(0, 0),
8064 grandchild_ptr->visible_rect_from_property_trees());
8065 child_ptr->SetHideLayerAndSubtree(false);
8067 child_ptr->SetOpacity(0.f);
8068 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8069 EXPECT_EQ(gfx::Rect(0, 0),
8070 grandchild_ptr->visible_rect_from_property_trees());
8072 // Now, even though child has zero opacity, we will configure |grandchild| and
8073 // |greatgrandchild| in several ways that should force the subtree to be
8074 // processed anyhow.
8075 grandchild_ptr->SetTouchEventHandlerRegion(Region(gfx::Rect(0, 0, 10, 10)));
8076 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8077 EXPECT_EQ(gfx::Rect(10, 10),
8078 grandchild_ptr->visible_rect_from_property_trees());
8079 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
8080 grandchild_ptr->SetTouchEventHandlerRegion(Region());
8081 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8082 EXPECT_EQ(gfx::Rect(0, 0),
8083 grandchild_ptr->visible_rect_from_property_trees());
8084 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
8086 ScopedPtrVector<CopyOutputRequest> requests;
8087 requests.push_back(CopyOutputRequest::CreateEmptyRequest());
8089 greatgrandchild_ptr->PassCopyRequests(&requests);
8090 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8091 EXPECT_EQ(gfx::Rect(10, 10),
8092 grandchild_ptr->visible_rect_from_property_trees());
8095 TEST_F(LayerTreeHostCommonTest, SkippingLayer) {
8096 gfx::Transform identity;
8097 FakeContentLayerClient client;
8098 scoped_refptr<Layer> root = Layer::Create(layer_settings());
8099 scoped_refptr<LayerWithForcedDrawsContent> child =
8100 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
8101 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
8102 gfx::PointF(), gfx::Size(100, 100), true, false);
8103 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
8104 gfx::PointF(), gfx::Size(10, 10), true, false);
8105 root->AddChild(child);
8107 host()->SetRootLayer(root);
8109 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8110 EXPECT_EQ(gfx::Rect(10, 10), child->visible_rect_from_property_trees());
8111 child->set_visible_rect_from_property_trees(gfx::Rect());
8113 child->SetHideLayerAndSubtree(true);
8114 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8115 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
8116 child->SetHideLayerAndSubtree(false);
8118 child->SetBounds(gfx::Size());
8119 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8120 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
8121 child->SetBounds(gfx::Size(10, 10));
8123 gfx::Transform rotate;
8124 child->SetDoubleSided(false);
8125 rotate.RotateAboutXAxis(180.f);
8126 child->SetTransform(rotate);
8127 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8128 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
8129 child->SetDoubleSided(true);
8130 child->SetTransform(identity);
8132 child->SetOpacity(0.f);
8133 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8134 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
8137 TEST_F(LayerTreeHostCommonTest, LayerTreeRebuildTest) {
8138 // Ensure that the treewalk in LayerTreeHostCommom::
8139 // PreCalculateMetaInformation happens when its required.
8140 scoped_refptr<Layer> root = Layer::Create(layer_settings());
8141 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
8142 scoped_refptr<Layer> child = Layer::Create(layer_settings());
8144 root->AddChild(parent);
8145 parent->AddChild(child);
8147 child->SetClipParent(root.get());
8149 gfx::Transform identity;
8151 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
8152 gfx::PointF(), gfx::Size(100, 100), true, false);
8153 SetLayerPropertiesForTesting(parent.get(), identity, gfx::Point3F(),
8154 gfx::PointF(), gfx::Size(100, 100), true, false);
8155 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
8156 gfx::PointF(), gfx::Size(100, 100), true, false);
8158 host()->SetRootLayer(root);
8160 ExecuteCalculateDrawProperties(root.get());
8161 EXPECT_EQ(parent->draw_properties().num_unclipped_descendants, 1u);
8163 // Ensure the dynamic update to input handlers happens.
8164 child->SetHaveWheelEventHandlers(true);
8165 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_input_handler);
8166 ExecuteCalculateDrawProperties(root.get());
8167 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_input_handler);
8169 child->SetHaveWheelEventHandlers(false);
8170 EXPECT_FALSE(root->draw_properties().layer_or_descendant_has_input_handler);
8171 ExecuteCalculateDrawProperties(root.get());
8172 EXPECT_FALSE(root->draw_properties().layer_or_descendant_has_input_handler);
8174 child->RequestCopyOfOutput(
8175 CopyOutputRequest::CreateRequest(base::Bind(&EmptyCopyOutputCallback)));
8176 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
8177 ExecuteCalculateDrawProperties(root.get());
8178 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
8181 TEST_F(LayerTreeHostCommonTest, InputHandlersRecursiveUpdateTest) {
8182 // Ensure that the treewalk in LayertreeHostCommon::
8183 // PreCalculateMetaInformation updates input handlers correctly.
8184 scoped_refptr<Layer> root = Layer::Create(layer_settings());
8185 scoped_refptr<Layer> child = Layer::Create(layer_settings());
8187 root->AddChild(child);
8189 child->SetHaveWheelEventHandlers(true);
8191 gfx::Transform identity;
8193 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
8194 gfx::PointF(), gfx::Size(100, 100), true, false);
8195 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
8196 gfx::PointF(), gfx::Size(100, 100), true, false);
8198 host()->SetRootLayer(root);
8200 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 0);
8201 ExecuteCalculateDrawProperties(root.get());
8202 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 1);
8203 child->SetHaveWheelEventHandlers(false);
8204 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 0);
8207 TEST_F(LayerTreeHostCommonTest, ResetPropertyTreeIndices) {
8208 gfx::Transform identity;
8209 gfx::Transform translate_z;
8210 translate_z.Translate3d(0, 0, 10);
8212 scoped_refptr<Layer> root = Layer::Create(layer_settings());
8213 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
8214 gfx::PointF(), gfx::Size(800, 800), true, false);
8216 scoped_refptr<Layer> child = Layer::Create(layer_settings());
8217 SetLayerPropertiesForTesting(child.get(), translate_z, gfx::Point3F(),
8218 gfx::PointF(), gfx::Size(100, 100), true, false);
8220 root->AddChild(child);
8222 host()->SetRootLayer(root);
8224 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8225 EXPECT_NE(-1, child->transform_tree_index());
8227 child->RemoveFromParent();
8229 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8230 EXPECT_EQ(-1, child->transform_tree_index());
8233 TEST_F(LayerTreeHostCommonTest, ResetLayerDrawPropertiestest) {
8234 scoped_refptr<Layer> root = Layer::Create(layer_settings());
8235 scoped_refptr<Layer> child = Layer::Create(layer_settings());
8237 root->AddChild(child);
8238 gfx::Transform identity;
8240 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
8241 gfx::PointF(), gfx::Size(100, 100), true, false);
8242 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
8243 gfx::PointF(), gfx::Size(100, 100), true, false);
8245 host()->SetRootLayer(root);
8247 EXPECT_FALSE(root->layer_or_descendant_is_drawn());
8248 EXPECT_FALSE(root->visited());
8249 EXPECT_FALSE(root->sorted_for_recursion());
8250 EXPECT_FALSE(child->layer_or_descendant_is_drawn());
8251 EXPECT_FALSE(child->visited());
8252 EXPECT_FALSE(child->sorted_for_recursion());
8254 root->set_layer_or_descendant_is_drawn(true);
8255 root->set_visited(true);
8256 root->set_sorted_for_recursion(true);
8257 child->set_layer_or_descendant_is_drawn(true);
8258 child->set_visited(true);
8259 child->set_sorted_for_recursion(true);
8261 LayerTreeHostCommon::PreCalculateMetaInformationForTesting(root.get());
8263 EXPECT_FALSE(root->layer_or_descendant_is_drawn());
8264 EXPECT_FALSE(root->visited());
8265 EXPECT_FALSE(root->sorted_for_recursion());
8266 EXPECT_FALSE(child->layer_or_descendant_is_drawn());
8267 EXPECT_FALSE(child->visited());
8268 EXPECT_FALSE(child->sorted_for_recursion());
8271 } // namespace
8272 } // namespace cc