PlzNavigate: Improvements to RFHM commit logic.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common_unittest.cc
blob439d9200a911013def3d661b844531acffee0b08
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/layer_animation_controller.h"
11 #include "cc/animation/transform_operations.h"
12 #include "cc/base/math_util.h"
13 #include "cc/layers/content_layer.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_impl_proxy.h"
25 #include "cc/test/fake_layer_tree_host.h"
26 #include "cc/test/fake_layer_tree_host_impl.h"
27 #include "cc/test/fake_picture_layer.h"
28 #include "cc/test/fake_picture_layer_impl.h"
29 #include "cc/test/geometry_test_utils.h"
30 #include "cc/test/layer_tree_host_common_test.h"
31 #include "cc/test/test_task_graph_runner.h"
32 #include "cc/trees/layer_tree_impl.h"
33 #include "cc/trees/proxy.h"
34 #include "cc/trees/single_thread_proxy.h"
35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h"
37 #include "ui/gfx/geometry/quad_f.h"
38 #include "ui/gfx/geometry/vector2d_conversions.h"
39 #include "ui/gfx/transform.h"
41 namespace cc {
42 namespace {
44 class LayerWithForcedDrawsContent : public Layer {
45 public:
46 LayerWithForcedDrawsContent() {}
48 bool DrawsContent() const override;
50 private:
51 ~LayerWithForcedDrawsContent() override {}
54 bool LayerWithForcedDrawsContent::DrawsContent() const { return true; }
56 class MockContentLayerClient : public ContentLayerClient {
57 public:
58 MockContentLayerClient() {}
59 ~MockContentLayerClient() override {}
60 void PaintContents(SkCanvas* canvas,
61 const gfx::Rect& clip,
62 PaintingControlSetting picture_control) override {}
63 scoped_refptr<DisplayItemList> PaintContentsToDisplayList(
64 const gfx::Rect& clip,
65 PaintingControlSetting picture_control) override {
66 NOTIMPLEMENTED();
67 return DisplayItemList::Create();
69 bool FillsBoundsCompletely() const override { return false; }
72 scoped_refptr<FakePictureLayer> CreateDrawablePictureLayer(
73 ContentLayerClient* delegate) {
74 scoped_refptr<FakePictureLayer> to_return =
75 FakePictureLayer::Create(delegate);
76 to_return->SetIsDrawable(true);
77 return to_return;
80 scoped_refptr<ContentLayer> CreateDrawableContentLayer(
81 ContentLayerClient* delegate) {
82 scoped_refptr<ContentLayer> to_return = ContentLayer::Create(delegate);
83 to_return->SetIsDrawable(true);
84 return to_return;
87 #define EXPECT_CONTENTS_SCALE_EQ(expected, layer) \
88 do { \
89 EXPECT_FLOAT_EQ(expected, layer->contents_scale_x()); \
90 EXPECT_FLOAT_EQ(expected, layer->contents_scale_y()); \
91 } while (false)
93 #define EXPECT_IDEAL_SCALE_EQ(expected, layer) \
94 do { \
95 EXPECT_FLOAT_EQ(expected, layer->draw_properties().ideal_contents_scale); \
96 } while (false)
98 TEST_F(LayerTreeHostCommonTest, TransformsForNoOpLayer) {
99 // Sanity check: For layers positioned at zero, with zero size,
100 // and with identity transforms, then the draw transform,
101 // screen space transform, and the hierarchy passed on to children
102 // layers should also be identity transforms.
104 scoped_refptr<Layer> parent = Layer::Create();
105 scoped_refptr<Layer> child = Layer::Create();
106 scoped_refptr<Layer> grand_child = Layer::Create();
107 parent->AddChild(child);
108 child->AddChild(grand_child);
110 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
111 host->SetRootLayer(parent);
113 gfx::Transform identity_matrix;
114 SetLayerPropertiesForTesting(parent.get(),
115 identity_matrix,
116 gfx::Point3F(),
117 gfx::PointF(),
118 gfx::Size(100, 100),
119 true,
120 false);
121 SetLayerPropertiesForTesting(child.get(),
122 identity_matrix,
123 gfx::Point3F(),
124 gfx::PointF(),
125 gfx::Size(),
126 true,
127 false);
128 SetLayerPropertiesForTesting(grand_child.get(),
129 identity_matrix,
130 gfx::Point3F(),
131 gfx::PointF(),
132 gfx::Size(),
133 true,
134 false);
136 ExecuteCalculateDrawProperties(parent.get());
138 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
139 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
140 child->screen_space_transform());
141 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
142 grand_child->draw_transform());
143 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
144 grand_child->screen_space_transform());
147 TEST_F(LayerTreeHostCommonTest, DoNotSkipLayersWithHandlers) {
148 scoped_refptr<Layer> parent = Layer::Create();
149 scoped_refptr<Layer> child = Layer::Create();
150 scoped_refptr<Layer> grand_child = Layer::Create();
151 parent->AddChild(child);
152 child->AddChild(grand_child);
154 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
155 host->SetRootLayer(parent);
157 gfx::Transform identity_matrix;
158 SetLayerPropertiesForTesting(parent.get(),
159 identity_matrix,
160 gfx::Point3F(),
161 gfx::PointF(),
162 gfx::Size(100, 100),
163 true,
164 false);
165 SetLayerPropertiesForTesting(child.get(),
166 identity_matrix,
167 gfx::Point3F(),
168 gfx::PointF(10, 10),
169 gfx::Size(100, 100),
170 true,
171 false);
172 // This would have previously caused us to skip our subtree, but this would be
173 // wrong; we need up-to-date draw properties to do hit testing on the layers
174 // with handlers.
175 child->SetOpacity(0.f);
176 SetLayerPropertiesForTesting(grand_child.get(),
177 identity_matrix,
178 gfx::Point3F(),
179 gfx::PointF(10, 10),
180 gfx::Size(100, 100),
181 true,
182 false);
183 grand_child->SetTouchEventHandlerRegion(gfx::Rect(0, 0, 100, 100));
185 ExecuteCalculateDrawProperties(parent.get());
187 // Check that we've computed draw properties for the subtree rooted at
188 // |child|.
189 EXPECT_FALSE(child->draw_transform().IsIdentity());
190 EXPECT_FALSE(grand_child->draw_transform().IsIdentity());
193 TEST_F(LayerTreeHostCommonTest, TransformsForSingleLayer) {
194 gfx::Transform identity_matrix;
195 scoped_refptr<Layer> layer = Layer::Create();
197 scoped_refptr<Layer> root = Layer::Create();
198 SetLayerPropertiesForTesting(root.get(),
199 identity_matrix,
200 gfx::Point3F(),
201 gfx::PointF(),
202 gfx::Size(1, 2),
203 true,
204 false);
205 root->AddChild(layer);
207 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
208 host->SetRootLayer(root);
210 // Case 2: Setting the bounds of the layer should not affect either the draw
211 // transform or the screenspace transform.
212 gfx::Transform translation_to_center;
213 translation_to_center.Translate(5.0, 6.0);
214 SetLayerPropertiesForTesting(layer.get(),
215 identity_matrix,
216 gfx::Point3F(),
217 gfx::PointF(),
218 gfx::Size(10, 12),
219 true,
220 false);
221 ExecuteCalculateDrawProperties(root.get());
222 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, layer->draw_transform());
223 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
224 layer->screen_space_transform());
226 // Case 3: The anchor point by itself (without a layer transform) should have
227 // no effect on the transforms.
228 SetLayerPropertiesForTesting(layer.get(),
229 identity_matrix,
230 gfx::Point3F(2.5f, 3.0f, 0.f),
231 gfx::PointF(),
232 gfx::Size(10, 12),
233 true,
234 false);
235 ExecuteCalculateDrawProperties(root.get());
236 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, layer->draw_transform());
237 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
238 layer->screen_space_transform());
240 // Case 4: A change in actual position affects both the draw transform and
241 // screen space transform.
242 gfx::Transform position_transform;
243 position_transform.Translate(0.f, 1.2f);
244 SetLayerPropertiesForTesting(layer.get(),
245 identity_matrix,
246 gfx::Point3F(2.5f, 3.0f, 0.f),
247 gfx::PointF(0.f, 1.2f),
248 gfx::Size(10, 12),
249 true,
250 false);
251 ExecuteCalculateDrawProperties(root.get());
252 EXPECT_TRANSFORMATION_MATRIX_EQ(position_transform, layer->draw_transform());
253 EXPECT_TRANSFORMATION_MATRIX_EQ(position_transform,
254 layer->screen_space_transform());
256 // Case 5: In the correct sequence of transforms, the layer transform should
257 // pre-multiply the translation_to_center. This is easily tested by using a
258 // scale transform, because scale and translation are not commutative.
259 gfx::Transform layer_transform;
260 layer_transform.Scale3d(2.0, 2.0, 1.0);
261 SetLayerPropertiesForTesting(layer.get(),
262 layer_transform,
263 gfx::Point3F(),
264 gfx::PointF(),
265 gfx::Size(10, 12),
266 true,
267 false);
268 ExecuteCalculateDrawProperties(root.get());
269 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_transform, layer->draw_transform());
270 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_transform,
271 layer->screen_space_transform());
273 // Case 6: The layer transform should occur with respect to the anchor point.
274 gfx::Transform translation_to_anchor;
275 translation_to_anchor.Translate(5.0, 0.0);
276 gfx::Transform expected_result =
277 translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
278 SetLayerPropertiesForTesting(layer.get(),
279 layer_transform,
280 gfx::Point3F(5.0f, 0.f, 0.f),
281 gfx::PointF(),
282 gfx::Size(10, 12),
283 true,
284 false);
285 ExecuteCalculateDrawProperties(root.get());
286 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result, layer->draw_transform());
287 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result,
288 layer->screen_space_transform());
290 // Case 7: Verify that position pre-multiplies the layer transform. The
291 // current implementation of CalculateDrawProperties does this implicitly, but
292 // it is still worth testing to detect accidental regressions.
293 expected_result = position_transform * translation_to_anchor *
294 layer_transform * Inverse(translation_to_anchor);
295 SetLayerPropertiesForTesting(layer.get(),
296 layer_transform,
297 gfx::Point3F(5.0f, 0.f, 0.f),
298 gfx::PointF(0.f, 1.2f),
299 gfx::Size(10, 12),
300 true,
301 false);
302 ExecuteCalculateDrawProperties(root.get());
303 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result, layer->draw_transform());
304 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result,
305 layer->screen_space_transform());
308 TEST_F(LayerTreeHostCommonTest, TransformsAboutScrollOffset) {
309 const gfx::ScrollOffset kScrollOffset(50, 100);
310 const gfx::Vector2dF kScrollDelta(2.34f, 5.67f);
311 const gfx::Vector2d kMaxScrollOffset(200, 200);
312 const gfx::PointF kScrollLayerPosition(-kScrollOffset.x(),
313 -kScrollOffset.y());
314 const float kPageScale = 0.888f;
315 const float kDeviceScale = 1.666f;
317 FakeImplProxy proxy;
318 TestSharedBitmapManager shared_bitmap_manager;
319 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
321 gfx::Transform identity_matrix;
322 scoped_ptr<LayerImpl> sublayer_scoped_ptr(
323 LayerImpl::Create(host_impl.active_tree(), 1));
324 LayerImpl* sublayer = sublayer_scoped_ptr.get();
325 sublayer->SetContentsScale(kPageScale * kDeviceScale,
326 kPageScale * kDeviceScale);
327 SetLayerPropertiesForTesting(sublayer, identity_matrix, gfx::Point3F(),
328 gfx::PointF(), gfx::Size(500, 500), true, false,
329 false);
331 scoped_ptr<LayerImpl> scroll_layer_scoped_ptr(
332 LayerImpl::Create(host_impl.active_tree(), 2));
333 LayerImpl* scroll_layer = scroll_layer_scoped_ptr.get();
334 SetLayerPropertiesForTesting(scroll_layer, identity_matrix, gfx::Point3F(),
335 gfx::PointF(), gfx::Size(10, 20), true, false,
336 false);
337 scoped_ptr<LayerImpl> clip_layer_scoped_ptr(
338 LayerImpl::Create(host_impl.active_tree(), 4));
339 LayerImpl* clip_layer = clip_layer_scoped_ptr.get();
341 scroll_layer->SetScrollClipLayer(clip_layer->id());
342 clip_layer->SetBounds(
343 gfx::Size(scroll_layer->bounds().width() + kMaxScrollOffset.x(),
344 scroll_layer->bounds().height() + kMaxScrollOffset.y()));
345 scroll_layer->SetScrollClipLayer(clip_layer->id());
346 scroll_layer->SetScrollDelta(kScrollDelta);
347 gfx::Transform impl_transform;
348 scroll_layer->AddChild(sublayer_scoped_ptr.Pass());
349 LayerImpl* scroll_layer_raw_ptr = scroll_layer_scoped_ptr.get();
350 clip_layer->AddChild(scroll_layer_scoped_ptr.Pass());
351 scroll_layer_raw_ptr->PushScrollOffsetFromMainThread(kScrollOffset);
353 scoped_ptr<LayerImpl> root(LayerImpl::Create(host_impl.active_tree(), 3));
354 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
355 gfx::PointF(), gfx::Size(3, 4), true, false,
356 false);
357 root->AddChild(clip_layer_scoped_ptr.Pass());
358 root->SetHasRenderSurface(true);
360 ExecuteCalculateDrawProperties(
361 root.get(), kDeviceScale, kPageScale, scroll_layer->parent());
362 gfx::Transform expected_transform = identity_matrix;
363 gfx::PointF sub_layer_screen_position = kScrollLayerPosition - kScrollDelta;
364 sub_layer_screen_position.Scale(kPageScale * kDeviceScale);
365 expected_transform.Translate(MathUtil::Round(sub_layer_screen_position.x()),
366 MathUtil::Round(sub_layer_screen_position.y()));
367 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
368 sublayer->draw_transform());
369 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
370 sublayer->screen_space_transform());
372 gfx::Transform arbitrary_translate;
373 const float kTranslateX = 10.6f;
374 const float kTranslateY = 20.6f;
375 arbitrary_translate.Translate(kTranslateX, kTranslateY);
376 SetLayerPropertiesForTesting(scroll_layer, arbitrary_translate,
377 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 20),
378 true, false, false);
379 ExecuteCalculateDrawProperties(
380 root.get(), kDeviceScale, kPageScale, scroll_layer->parent());
381 expected_transform.MakeIdentity();
382 expected_transform.Translate(
383 MathUtil::Round(kTranslateX * kPageScale * kDeviceScale +
384 sub_layer_screen_position.x()),
385 MathUtil::Round(kTranslateY * kPageScale * kDeviceScale +
386 sub_layer_screen_position.y()));
387 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
388 sublayer->draw_transform());
391 TEST_F(LayerTreeHostCommonTest, TransformsForSimpleHierarchy) {
392 gfx::Transform identity_matrix;
393 scoped_refptr<Layer> root = Layer::Create();
394 scoped_refptr<Layer> parent = Layer::Create();
395 scoped_refptr<Layer> child = Layer::Create();
396 scoped_refptr<Layer> grand_child = Layer::Create();
397 root->AddChild(parent);
398 parent->AddChild(child);
399 child->AddChild(grand_child);
401 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
402 host->SetRootLayer(root);
404 // One-time setup of root layer
405 SetLayerPropertiesForTesting(root.get(),
406 identity_matrix,
407 gfx::Point3F(),
408 gfx::PointF(),
409 gfx::Size(1, 2),
410 true,
411 false);
413 // Case 1: parent's anchor point should not affect child or grand_child.
414 SetLayerPropertiesForTesting(parent.get(),
415 identity_matrix,
416 gfx::Point3F(2.5f, 3.0f, 0.f),
417 gfx::PointF(),
418 gfx::Size(10, 12),
419 true,
420 false);
421 SetLayerPropertiesForTesting(child.get(),
422 identity_matrix,
423 gfx::Point3F(),
424 gfx::PointF(),
425 gfx::Size(16, 18),
426 true,
427 false);
428 SetLayerPropertiesForTesting(grand_child.get(),
429 identity_matrix,
430 gfx::Point3F(),
431 gfx::PointF(),
432 gfx::Size(76, 78),
433 true,
434 false);
435 ExecuteCalculateDrawProperties(root.get());
436 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
437 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
438 child->screen_space_transform());
439 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
440 grand_child->draw_transform());
441 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
442 grand_child->screen_space_transform());
444 // Case 2: parent's position affects child and grand_child.
445 gfx::Transform parent_position_transform;
446 parent_position_transform.Translate(0.f, 1.2f);
447 SetLayerPropertiesForTesting(parent.get(),
448 identity_matrix,
449 gfx::Point3F(2.5f, 3.0f, 0.f),
450 gfx::PointF(0.f, 1.2f),
451 gfx::Size(10, 12),
452 true,
453 false);
454 SetLayerPropertiesForTesting(child.get(),
455 identity_matrix,
456 gfx::Point3F(),
457 gfx::PointF(),
458 gfx::Size(16, 18),
459 true,
460 false);
461 SetLayerPropertiesForTesting(grand_child.get(),
462 identity_matrix,
463 gfx::Point3F(),
464 gfx::PointF(),
465 gfx::Size(76, 78),
466 true,
467 false);
468 ExecuteCalculateDrawProperties(root.get());
469 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
470 child->draw_transform());
471 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
472 child->screen_space_transform());
473 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
474 grand_child->draw_transform());
475 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
476 grand_child->screen_space_transform());
478 // Case 3: parent's local transform affects child and grandchild
479 gfx::Transform parent_layer_transform;
480 parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
481 gfx::Transform parent_translation_to_anchor;
482 parent_translation_to_anchor.Translate(2.5, 3.0);
483 gfx::Transform parent_composite_transform =
484 parent_translation_to_anchor * parent_layer_transform *
485 Inverse(parent_translation_to_anchor);
486 SetLayerPropertiesForTesting(parent.get(),
487 parent_layer_transform,
488 gfx::Point3F(2.5f, 3.0f, 0.f),
489 gfx::PointF(),
490 gfx::Size(10, 12),
491 true,
492 false);
493 SetLayerPropertiesForTesting(child.get(),
494 identity_matrix,
495 gfx::Point3F(),
496 gfx::PointF(),
497 gfx::Size(16, 18),
498 true,
499 false);
500 SetLayerPropertiesForTesting(grand_child.get(),
501 identity_matrix,
502 gfx::Point3F(),
503 gfx::PointF(),
504 gfx::Size(76, 78),
505 true,
506 false);
507 ExecuteCalculateDrawProperties(root.get());
508 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
509 child->draw_transform());
510 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
511 child->screen_space_transform());
512 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
513 grand_child->draw_transform());
514 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
515 grand_child->screen_space_transform());
518 TEST_F(LayerTreeHostCommonTest, TransformsForSingleRenderSurface) {
519 scoped_refptr<Layer> root = Layer::Create();
520 scoped_refptr<Layer> parent = Layer::Create();
521 scoped_refptr<Layer> child = Layer::Create();
522 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
523 make_scoped_refptr(new LayerWithForcedDrawsContent());
524 root->AddChild(parent);
525 parent->AddChild(child);
526 child->AddChild(grand_child);
528 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
529 host->SetRootLayer(root);
531 // One-time setup of root layer
532 gfx::Transform identity_matrix;
533 SetLayerPropertiesForTesting(root.get(),
534 identity_matrix,
535 gfx::Point3F(),
536 gfx::PointF(),
537 gfx::Size(1, 2),
538 true,
539 false);
541 // Child is set up so that a new render surface should be created.
542 child->SetOpacity(0.5f);
543 child->SetForceRenderSurface(true);
545 gfx::Transform parent_layer_transform;
546 parent_layer_transform.Scale3d(1.f, 0.9f, 1.f);
547 gfx::Transform parent_translation_to_anchor;
548 parent_translation_to_anchor.Translate(25.0, 30.0);
550 gfx::Transform parent_composite_transform =
551 parent_translation_to_anchor * parent_layer_transform *
552 Inverse(parent_translation_to_anchor);
553 gfx::Vector2dF parent_composite_scale =
554 MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
555 1.f);
556 gfx::Transform surface_sublayer_transform;
557 surface_sublayer_transform.Scale(parent_composite_scale.x(),
558 parent_composite_scale.y());
559 gfx::Transform surface_sublayer_composite_transform =
560 parent_composite_transform * Inverse(surface_sublayer_transform);
562 // Child's render surface should not exist yet.
563 ASSERT_FALSE(child->render_surface());
565 SetLayerPropertiesForTesting(parent.get(),
566 parent_layer_transform,
567 gfx::Point3F(25.0f, 30.0f, 0.f),
568 gfx::PointF(),
569 gfx::Size(100, 120),
570 true,
571 false);
572 SetLayerPropertiesForTesting(child.get(),
573 identity_matrix,
574 gfx::Point3F(),
575 gfx::PointF(),
576 gfx::Size(16, 18),
577 true,
578 false);
579 SetLayerPropertiesForTesting(grand_child.get(),
580 identity_matrix,
581 gfx::Point3F(),
582 gfx::PointF(),
583 gfx::Size(8, 10),
584 true,
585 false);
586 ExecuteCalculateDrawProperties(root.get());
588 // Render surface should have been created now.
589 ASSERT_TRUE(child->render_surface());
590 ASSERT_EQ(child.get(), child->render_target());
592 // The child layer's draw transform should refer to its new render surface.
593 // The screen-space transform, however, should still refer to the root.
594 EXPECT_TRANSFORMATION_MATRIX_EQ(surface_sublayer_transform,
595 child->draw_transform());
596 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
597 child->screen_space_transform());
599 // Because the grand_child is the only drawable content, the child's render
600 // surface will tighten its bounds to the grand_child. The scale at which the
601 // surface's subtree is drawn must be removed from the composite transform.
602 EXPECT_TRANSFORMATION_MATRIX_EQ(
603 surface_sublayer_composite_transform,
604 child->render_target()->render_surface()->draw_transform());
606 // The screen space is the same as the target since the child surface draws
607 // into the root.
608 EXPECT_TRANSFORMATION_MATRIX_EQ(
609 surface_sublayer_composite_transform,
610 child->render_target()->render_surface()->screen_space_transform());
613 TEST_F(LayerTreeHostCommonTest, TransformsForReplica) {
614 scoped_refptr<Layer> root = Layer::Create();
615 scoped_refptr<Layer> parent = Layer::Create();
616 scoped_refptr<Layer> child = Layer::Create();
617 scoped_refptr<Layer> child_replica = Layer::Create();
618 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
619 make_scoped_refptr(new LayerWithForcedDrawsContent());
620 root->AddChild(parent);
621 parent->AddChild(child);
622 child->AddChild(grand_child);
623 child->SetReplicaLayer(child_replica.get());
625 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
626 host->SetRootLayer(root);
628 // One-time setup of root layer
629 gfx::Transform identity_matrix;
630 SetLayerPropertiesForTesting(root.get(),
631 identity_matrix,
632 gfx::Point3F(),
633 gfx::PointF(),
634 gfx::Size(1, 2),
635 true,
636 false);
638 // Child is set up so that a new render surface should be created.
639 child->SetOpacity(0.5f);
641 gfx::Transform parent_layer_transform;
642 parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
643 gfx::Transform parent_translation_to_anchor;
644 parent_translation_to_anchor.Translate(2.5, 3.0);
645 gfx::Transform parent_composite_transform =
646 parent_translation_to_anchor * parent_layer_transform *
647 Inverse(parent_translation_to_anchor);
648 gfx::Transform replica_layer_transform;
649 replica_layer_transform.Scale3d(3.0, 3.0, 1.0);
650 gfx::Vector2dF parent_composite_scale =
651 MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
652 1.f);
653 gfx::Transform surface_sublayer_transform;
654 surface_sublayer_transform.Scale(parent_composite_scale.x(),
655 parent_composite_scale.y());
656 gfx::Transform replica_composite_transform =
657 parent_composite_transform * replica_layer_transform *
658 Inverse(surface_sublayer_transform);
659 child_replica->SetIsDrawable(true);
660 // Child's render surface should not exist yet.
661 ASSERT_FALSE(child->render_surface());
663 SetLayerPropertiesForTesting(parent.get(),
664 parent_layer_transform,
665 gfx::Point3F(2.5f, 3.0f, 0.f),
666 gfx::PointF(),
667 gfx::Size(10, 12),
668 true,
669 false);
670 SetLayerPropertiesForTesting(child.get(),
671 identity_matrix,
672 gfx::Point3F(),
673 gfx::PointF(),
674 gfx::Size(16, 18),
675 true,
676 false);
677 SetLayerPropertiesForTesting(grand_child.get(),
678 identity_matrix,
679 gfx::Point3F(),
680 gfx::PointF(-0.5f, -0.5f),
681 gfx::Size(1, 1),
682 true,
683 false);
684 SetLayerPropertiesForTesting(child_replica.get(),
685 replica_layer_transform,
686 gfx::Point3F(),
687 gfx::PointF(),
688 gfx::Size(),
689 true,
690 false);
691 ExecuteCalculateDrawProperties(root.get());
693 // Render surface should have been created now.
694 ASSERT_TRUE(child->render_surface());
695 ASSERT_EQ(child.get(), child->render_target());
697 EXPECT_TRANSFORMATION_MATRIX_EQ(
698 replica_composite_transform,
699 child->render_target()->render_surface()->replica_draw_transform());
700 EXPECT_TRANSFORMATION_MATRIX_EQ(replica_composite_transform,
701 child->render_target()
702 ->render_surface()
703 ->replica_screen_space_transform());
706 TEST_F(LayerTreeHostCommonTest, TransformsForRenderSurfaceHierarchy) {
707 // This test creates a more complex tree and verifies it all at once. This
708 // covers the following cases:
709 // - layers that are described w.r.t. a render surface: should have draw
710 // transforms described w.r.t. that surface
711 // - A render surface described w.r.t. an ancestor render surface: should
712 // have a draw transform described w.r.t. that ancestor surface
713 // - Replicas of a render surface are described w.r.t. the replica's
714 // transform around its anchor, along with the surface itself.
715 // - Sanity check on recursion: verify transforms of layers described w.r.t.
716 // a render surface that is described w.r.t. an ancestor render surface.
717 // - verifying that each layer has a reference to the correct render surface
718 // and render target values.
720 scoped_refptr<Layer> root = Layer::Create();
721 scoped_refptr<Layer> parent = Layer::Create();
722 scoped_refptr<Layer> render_surface1 = Layer::Create();
723 scoped_refptr<Layer> render_surface2 = Layer::Create();
724 scoped_refptr<Layer> child_of_root = Layer::Create();
725 scoped_refptr<Layer> child_of_rs1 = Layer::Create();
726 scoped_refptr<Layer> child_of_rs2 = Layer::Create();
727 scoped_refptr<Layer> replica_of_rs1 = Layer::Create();
728 scoped_refptr<Layer> replica_of_rs2 = Layer::Create();
729 scoped_refptr<Layer> grand_child_of_root = Layer::Create();
730 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs1 =
731 make_scoped_refptr(new LayerWithForcedDrawsContent());
732 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs2 =
733 make_scoped_refptr(new LayerWithForcedDrawsContent());
734 root->AddChild(parent);
735 parent->AddChild(render_surface1);
736 parent->AddChild(child_of_root);
737 render_surface1->AddChild(child_of_rs1);
738 render_surface1->AddChild(render_surface2);
739 render_surface2->AddChild(child_of_rs2);
740 child_of_root->AddChild(grand_child_of_root);
741 child_of_rs1->AddChild(grand_child_of_rs1);
742 child_of_rs2->AddChild(grand_child_of_rs2);
743 render_surface1->SetReplicaLayer(replica_of_rs1.get());
744 render_surface2->SetReplicaLayer(replica_of_rs2.get());
746 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
747 host->SetRootLayer(root);
749 // In combination with descendant draws content, opacity != 1 forces the layer
750 // to have a new render surface.
751 render_surface1->SetOpacity(0.5f);
752 render_surface2->SetOpacity(0.33f);
754 // One-time setup of root layer
755 gfx::Transform identity_matrix;
756 SetLayerPropertiesForTesting(root.get(),
757 identity_matrix,
758 gfx::Point3F(),
759 gfx::PointF(),
760 gfx::Size(1, 2),
761 true,
762 false);
764 // All layers in the tree are initialized with an anchor at .25 and a size of
765 // (10,10). matrix "A" is the composite layer transform used in all layers,
766 // Matrix "R" is the composite replica transform used in all replica layers.
767 gfx::Transform translation_to_anchor;
768 translation_to_anchor.Translate(2.5, 0.0);
769 gfx::Transform layer_transform;
770 layer_transform.Translate(1.0, 1.0);
771 gfx::Transform replica_layer_transform;
772 replica_layer_transform.Scale3d(-2.0, 5.0, 1.0);
774 gfx::Transform A =
775 translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
776 gfx::Transform R = A * translation_to_anchor * replica_layer_transform *
777 Inverse(translation_to_anchor);
779 gfx::Vector2dF surface1_parent_transform_scale =
780 MathUtil::ComputeTransform2dScaleComponents(A, 1.f);
781 gfx::Transform surface1_sublayer_transform;
782 surface1_sublayer_transform.Scale(surface1_parent_transform_scale.x(),
783 surface1_parent_transform_scale.y());
785 // SS1 = transform given to the subtree of render_surface1
786 gfx::Transform SS1 = surface1_sublayer_transform;
787 // S1 = transform to move from render_surface1 pixels to the layer space of
788 // the owning layer
789 gfx::Transform S1 = Inverse(surface1_sublayer_transform);
791 gfx::Vector2dF surface2_parent_transform_scale =
792 MathUtil::ComputeTransform2dScaleComponents(SS1 * A, 1.f);
793 gfx::Transform surface2_sublayer_transform;
794 surface2_sublayer_transform.Scale(surface2_parent_transform_scale.x(),
795 surface2_parent_transform_scale.y());
797 // SS2 = transform given to the subtree of render_surface2
798 gfx::Transform SS2 = surface2_sublayer_transform;
799 // S2 = transform to move from render_surface2 pixels to the layer space of
800 // the owning layer
801 gfx::Transform S2 = Inverse(surface2_sublayer_transform);
803 SetLayerPropertiesForTesting(parent.get(),
804 layer_transform,
805 gfx::Point3F(2.5f, 0.f, 0.f),
806 gfx::PointF(),
807 gfx::Size(10, 10),
808 true,
809 false);
810 SetLayerPropertiesForTesting(render_surface1.get(),
811 layer_transform,
812 gfx::Point3F(2.5f, 0.f, 0.f),
813 gfx::PointF(),
814 gfx::Size(10, 10),
815 true,
816 false);
817 SetLayerPropertiesForTesting(render_surface2.get(),
818 layer_transform,
819 gfx::Point3F(2.5f, 0.f, 0.f),
820 gfx::PointF(),
821 gfx::Size(10, 10),
822 true,
823 false);
824 SetLayerPropertiesForTesting(child_of_root.get(),
825 layer_transform,
826 gfx::Point3F(2.5f, 0.f, 0.f),
827 gfx::PointF(),
828 gfx::Size(10, 10),
829 true,
830 false);
831 SetLayerPropertiesForTesting(child_of_rs1.get(),
832 layer_transform,
833 gfx::Point3F(2.5f, 0.f, 0.f),
834 gfx::PointF(),
835 gfx::Size(10, 10),
836 true,
837 false);
838 SetLayerPropertiesForTesting(child_of_rs2.get(),
839 layer_transform,
840 gfx::Point3F(2.5f, 0.f, 0.f),
841 gfx::PointF(),
842 gfx::Size(10, 10),
843 true,
844 false);
845 SetLayerPropertiesForTesting(grand_child_of_root.get(),
846 layer_transform,
847 gfx::Point3F(2.5f, 0.f, 0.f),
848 gfx::PointF(),
849 gfx::Size(10, 10),
850 true,
851 false);
852 SetLayerPropertiesForTesting(grand_child_of_rs1.get(),
853 layer_transform,
854 gfx::Point3F(2.5f, 0.f, 0.f),
855 gfx::PointF(),
856 gfx::Size(10, 10),
857 true,
858 false);
859 SetLayerPropertiesForTesting(grand_child_of_rs2.get(),
860 layer_transform,
861 gfx::Point3F(2.5f, 0.f, 0.f),
862 gfx::PointF(),
863 gfx::Size(10, 10),
864 true,
865 false);
866 SetLayerPropertiesForTesting(replica_of_rs1.get(),
867 replica_layer_transform,
868 gfx::Point3F(2.5f, 0.f, 0.f),
869 gfx::PointF(),
870 gfx::Size(),
871 true,
872 false);
873 SetLayerPropertiesForTesting(replica_of_rs2.get(),
874 replica_layer_transform,
875 gfx::Point3F(2.5f, 0.f, 0.f),
876 gfx::PointF(),
877 gfx::Size(),
878 true,
879 false);
881 ExecuteCalculateDrawProperties(root.get());
883 // Only layers that are associated with render surfaces should have an actual
884 // RenderSurface() value.
885 ASSERT_TRUE(root->render_surface());
886 ASSERT_FALSE(child_of_root->render_surface());
887 ASSERT_FALSE(grand_child_of_root->render_surface());
889 ASSERT_TRUE(render_surface1->render_surface());
890 ASSERT_FALSE(child_of_rs1->render_surface());
891 ASSERT_FALSE(grand_child_of_rs1->render_surface());
893 ASSERT_TRUE(render_surface2->render_surface());
894 ASSERT_FALSE(child_of_rs2->render_surface());
895 ASSERT_FALSE(grand_child_of_rs2->render_surface());
897 // Verify all render target accessors
898 EXPECT_EQ(root.get(), parent->render_target());
899 EXPECT_EQ(root.get(), child_of_root->render_target());
900 EXPECT_EQ(root.get(), grand_child_of_root->render_target());
902 EXPECT_EQ(render_surface1.get(), render_surface1->render_target());
903 EXPECT_EQ(render_surface1.get(), child_of_rs1->render_target());
904 EXPECT_EQ(render_surface1.get(), grand_child_of_rs1->render_target());
906 EXPECT_EQ(render_surface2.get(), render_surface2->render_target());
907 EXPECT_EQ(render_surface2.get(), child_of_rs2->render_target());
908 EXPECT_EQ(render_surface2.get(), grand_child_of_rs2->render_target());
910 // Verify layer draw transforms note that draw transforms are described with
911 // respect to the nearest ancestor render surface but screen space transforms
912 // are described with respect to the root.
913 EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->draw_transform());
914 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A, child_of_root->draw_transform());
915 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
916 grand_child_of_root->draw_transform());
918 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1, render_surface1->draw_transform());
919 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A, child_of_rs1->draw_transform());
920 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A * A,
921 grand_child_of_rs1->draw_transform());
923 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2, render_surface2->draw_transform());
924 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A, child_of_rs2->draw_transform());
925 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A * A,
926 grand_child_of_rs2->draw_transform());
928 // Verify layer screen-space transforms
930 EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->screen_space_transform());
931 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A,
932 child_of_root->screen_space_transform());
933 EXPECT_TRANSFORMATION_MATRIX_EQ(
934 A * A * A, grand_child_of_root->screen_space_transform());
936 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A,
937 render_surface1->screen_space_transform());
938 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
939 child_of_rs1->screen_space_transform());
940 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
941 grand_child_of_rs1->screen_space_transform());
943 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
944 render_surface2->screen_space_transform());
945 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
946 child_of_rs2->screen_space_transform());
947 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A * A,
948 grand_child_of_rs2->screen_space_transform());
950 // Verify render surface transforms.
952 // Draw transform of render surface 1 is described with respect to root.
953 EXPECT_TRANSFORMATION_MATRIX_EQ(
954 A * A * S1, render_surface1->render_surface()->draw_transform());
955 EXPECT_TRANSFORMATION_MATRIX_EQ(
956 A * R * S1, render_surface1->render_surface()->replica_draw_transform());
957 EXPECT_TRANSFORMATION_MATRIX_EQ(
958 A * A * S1, render_surface1->render_surface()->screen_space_transform());
959 EXPECT_TRANSFORMATION_MATRIX_EQ(
960 A * R * S1,
961 render_surface1->render_surface()->replica_screen_space_transform());
962 // Draw transform of render surface 2 is described with respect to render
963 // surface 1.
964 EXPECT_TRANSFORMATION_MATRIX_EQ(
965 SS1 * A * S2, render_surface2->render_surface()->draw_transform());
966 EXPECT_TRANSFORMATION_MATRIX_EQ(
967 SS1 * R * S2,
968 render_surface2->render_surface()->replica_draw_transform());
969 EXPECT_TRANSFORMATION_MATRIX_EQ(
970 A * A * A * S2,
971 render_surface2->render_surface()->screen_space_transform());
972 EXPECT_TRANSFORMATION_MATRIX_EQ(
973 A * A * R * S2,
974 render_surface2->render_surface()->replica_screen_space_transform());
976 // Sanity check. If these fail there is probably a bug in the test itself. It
977 // is expected that we correctly set up transforms so that the y-component of
978 // the screen-space transform encodes the "depth" of the layer in the tree.
979 EXPECT_FLOAT_EQ(1.0, parent->screen_space_transform().matrix().get(1, 3));
980 EXPECT_FLOAT_EQ(2.0,
981 child_of_root->screen_space_transform().matrix().get(1, 3));
982 EXPECT_FLOAT_EQ(
983 3.0, grand_child_of_root->screen_space_transform().matrix().get(1, 3));
985 EXPECT_FLOAT_EQ(2.0,
986 render_surface1->screen_space_transform().matrix().get(1, 3));
987 EXPECT_FLOAT_EQ(3.0,
988 child_of_rs1->screen_space_transform().matrix().get(1, 3));
989 EXPECT_FLOAT_EQ(
990 4.0, grand_child_of_rs1->screen_space_transform().matrix().get(1, 3));
992 EXPECT_FLOAT_EQ(3.0,
993 render_surface2->screen_space_transform().matrix().get(1, 3));
994 EXPECT_FLOAT_EQ(4.0,
995 child_of_rs2->screen_space_transform().matrix().get(1, 3));
996 EXPECT_FLOAT_EQ(
997 5.0, grand_child_of_rs2->screen_space_transform().matrix().get(1, 3));
1000 TEST_F(LayerTreeHostCommonTest, TransformsForFlatteningLayer) {
1001 // For layers that flatten their subtree, there should be an orthographic
1002 // projection (for x and y values) in the middle of the transform sequence.
1003 // Note that the way the code is currently implemented, it is not expected to
1004 // use a canonical orthographic projection.
1006 scoped_refptr<Layer> root = Layer::Create();
1007 scoped_refptr<Layer> child = Layer::Create();
1008 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1009 make_scoped_refptr(new LayerWithForcedDrawsContent());
1010 scoped_refptr<LayerWithForcedDrawsContent> great_grand_child =
1011 make_scoped_refptr(new LayerWithForcedDrawsContent());
1013 gfx::Transform rotation_about_y_axis;
1014 rotation_about_y_axis.RotateAboutYAxis(30.0);
1016 const gfx::Transform identity_matrix;
1017 SetLayerPropertiesForTesting(root.get(),
1018 identity_matrix,
1019 gfx::Point3F(),
1020 gfx::PointF(),
1021 gfx::Size(100, 100),
1022 true,
1023 false);
1024 SetLayerPropertiesForTesting(child.get(),
1025 rotation_about_y_axis,
1026 gfx::Point3F(),
1027 gfx::PointF(),
1028 gfx::Size(10, 10),
1029 true,
1030 false);
1031 SetLayerPropertiesForTesting(grand_child.get(),
1032 rotation_about_y_axis,
1033 gfx::Point3F(),
1034 gfx::PointF(),
1035 gfx::Size(10, 10),
1036 true,
1037 false);
1038 SetLayerPropertiesForTesting(great_grand_child.get(), identity_matrix,
1039 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1040 true, false);
1042 root->AddChild(child);
1043 child->AddChild(grand_child);
1044 grand_child->AddChild(great_grand_child);
1045 child->SetForceRenderSurface(true);
1047 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1048 host->SetRootLayer(root);
1050 // No layers in this test should preserve 3d.
1051 ASSERT_TRUE(root->should_flatten_transform());
1052 ASSERT_TRUE(child->should_flatten_transform());
1053 ASSERT_TRUE(grand_child->should_flatten_transform());
1054 ASSERT_TRUE(great_grand_child->should_flatten_transform());
1056 gfx::Transform expected_child_draw_transform = rotation_about_y_axis;
1057 gfx::Transform expected_child_screen_space_transform = rotation_about_y_axis;
1058 gfx::Transform expected_grand_child_draw_transform =
1059 rotation_about_y_axis; // draws onto child's render surface
1060 gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
1061 flattened_rotation_about_y.FlattenTo2d();
1062 gfx::Transform expected_grand_child_screen_space_transform =
1063 flattened_rotation_about_y * rotation_about_y_axis;
1064 gfx::Transform expected_great_grand_child_draw_transform =
1065 flattened_rotation_about_y;
1066 gfx::Transform expected_great_grand_child_screen_space_transform =
1067 flattened_rotation_about_y * flattened_rotation_about_y;
1069 ExecuteCalculateDrawProperties(root.get());
1071 // The child's draw transform should have been taken by its surface.
1072 ASSERT_TRUE(child->render_surface());
1073 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_draw_transform,
1074 child->render_surface()->draw_transform());
1075 EXPECT_TRANSFORMATION_MATRIX_EQ(
1076 expected_child_screen_space_transform,
1077 child->render_surface()->screen_space_transform());
1078 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1079 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_screen_space_transform,
1080 child->screen_space_transform());
1081 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_draw_transform,
1082 grand_child->draw_transform());
1083 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_screen_space_transform,
1084 grand_child->screen_space_transform());
1085 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_great_grand_child_draw_transform,
1086 great_grand_child->draw_transform());
1087 EXPECT_TRANSFORMATION_MATRIX_EQ(
1088 expected_great_grand_child_screen_space_transform,
1089 great_grand_child->screen_space_transform());
1092 TEST_F(LayerTreeHostCommonTest, TransformsForDegenerateIntermediateLayer) {
1093 // A layer that is empty in one axis, but not the other, was accidentally
1094 // skipping a necessary translation. Without that translation, the coordinate
1095 // space of the layer's draw transform is incorrect.
1097 // Normally this isn't a problem, because the layer wouldn't be drawn anyway,
1098 // but if that layer becomes a render surface, then its draw transform is
1099 // implicitly inherited by the rest of the subtree, which then is positioned
1100 // incorrectly as a result.
1102 scoped_refptr<Layer> root = Layer::Create();
1103 scoped_refptr<Layer> child = Layer::Create();
1104 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1105 make_scoped_refptr(new LayerWithForcedDrawsContent());
1107 // The child height is zero, but has non-zero width that should be accounted
1108 // for while computing draw transforms.
1109 const gfx::Transform identity_matrix;
1110 SetLayerPropertiesForTesting(root.get(),
1111 identity_matrix,
1112 gfx::Point3F(),
1113 gfx::PointF(),
1114 gfx::Size(100, 100),
1115 true,
1116 false);
1117 SetLayerPropertiesForTesting(child.get(),
1118 identity_matrix,
1119 gfx::Point3F(),
1120 gfx::PointF(),
1121 gfx::Size(10, 0),
1122 true,
1123 false);
1124 SetLayerPropertiesForTesting(grand_child.get(),
1125 identity_matrix,
1126 gfx::Point3F(),
1127 gfx::PointF(),
1128 gfx::Size(10, 10),
1129 true,
1130 false);
1132 root->AddChild(child);
1133 child->AddChild(grand_child);
1134 child->SetForceRenderSurface(true);
1136 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1137 host->SetRootLayer(root);
1139 ExecuteCalculateDrawProperties(root.get());
1141 ASSERT_TRUE(child->render_surface());
1142 // This is the real test, the rest are sanity checks.
1143 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1144 child->render_surface()->draw_transform());
1145 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1146 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1147 grand_child->draw_transform());
1150 TEST_F(LayerTreeHostCommonTest, TransformAboveRootLayer) {
1151 // Transformations applied at the root of the tree should be forwarded
1152 // to child layers instead of applied to the root RenderSurface.
1153 const gfx::Transform identity_matrix;
1154 scoped_refptr<LayerWithForcedDrawsContent> root =
1155 new LayerWithForcedDrawsContent;
1156 scoped_refptr<LayerWithForcedDrawsContent> child =
1157 new LayerWithForcedDrawsContent;
1158 child->SetScrollClipLayerId(root->id());
1159 root->AddChild(child);
1161 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1162 host->SetRootLayer(root);
1164 SetLayerPropertiesForTesting(root.get(),
1165 identity_matrix,
1166 gfx::Point3F(),
1167 gfx::PointF(),
1168 gfx::Size(20, 20),
1169 true,
1170 false);
1171 SetLayerPropertiesForTesting(child.get(),
1172 identity_matrix,
1173 gfx::Point3F(),
1174 gfx::PointF(),
1175 gfx::Size(20, 20),
1176 true,
1177 false);
1179 gfx::Transform translate;
1180 translate.Translate(50, 50);
1182 RenderSurfaceLayerList render_surface_layer_list;
1183 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1184 root.get(), root->bounds(), translate, &render_surface_layer_list);
1185 inputs.can_adjust_raster_scales = true;
1186 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1187 EXPECT_EQ(translate, root->draw_properties().target_space_transform);
1188 EXPECT_EQ(translate, child->draw_properties().target_space_transform);
1189 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1190 EXPECT_EQ(1.f, root->draw_properties().device_scale_factor);
1191 EXPECT_EQ(1.f, child->draw_properties().device_scale_factor);
1194 gfx::Transform scale;
1195 scale.Scale(2, 2);
1197 RenderSurfaceLayerList render_surface_layer_list;
1198 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1199 root.get(), root->bounds(), scale, &render_surface_layer_list);
1200 inputs.can_adjust_raster_scales = true;
1201 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1202 EXPECT_EQ(scale, root->draw_properties().target_space_transform);
1203 EXPECT_EQ(scale, child->draw_properties().target_space_transform);
1204 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1205 EXPECT_EQ(2.f, root->draw_properties().device_scale_factor);
1206 EXPECT_EQ(2.f, child->draw_properties().device_scale_factor);
1209 gfx::Transform rotate;
1210 rotate.Rotate(2);
1212 RenderSurfaceLayerList render_surface_layer_list;
1213 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1214 root.get(), root->bounds(), rotate, &render_surface_layer_list);
1215 inputs.can_adjust_raster_scales = true;
1216 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1217 EXPECT_EQ(rotate, root->draw_properties().target_space_transform);
1218 EXPECT_EQ(rotate, child->draw_properties().target_space_transform);
1219 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1220 EXPECT_EQ(1.f, root->draw_properties().device_scale_factor);
1221 EXPECT_EQ(1.f, child->draw_properties().device_scale_factor);
1224 gfx::Transform composite;
1225 composite.ConcatTransform(translate);
1226 composite.ConcatTransform(scale);
1227 composite.ConcatTransform(rotate);
1229 RenderSurfaceLayerList render_surface_layer_list;
1230 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1231 root.get(), root->bounds(), composite, &render_surface_layer_list);
1232 inputs.can_adjust_raster_scales = true;
1233 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1234 EXPECT_EQ(composite, root->draw_properties().target_space_transform);
1235 EXPECT_EQ(composite, child->draw_properties().target_space_transform);
1236 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1239 // Verify it composes correctly with device scale.
1240 float device_scale_factor = 1.5f;
1243 RenderSurfaceLayerList render_surface_layer_list;
1244 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1245 root.get(), root->bounds(), translate, &render_surface_layer_list);
1246 inputs.device_scale_factor = device_scale_factor;
1247 inputs.can_adjust_raster_scales = true;
1248 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1249 gfx::Transform device_scaled_translate = translate;
1250 device_scaled_translate.Scale(device_scale_factor, device_scale_factor);
1251 EXPECT_EQ(device_scaled_translate,
1252 root->draw_properties().target_space_transform);
1253 EXPECT_EQ(device_scaled_translate,
1254 child->draw_properties().target_space_transform);
1255 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1256 EXPECT_EQ(device_scale_factor, root->draw_properties().device_scale_factor);
1257 EXPECT_EQ(device_scale_factor,
1258 child->draw_properties().device_scale_factor);
1261 // Verify it composes correctly with page scale.
1262 float page_scale_factor = 2.f;
1265 RenderSurfaceLayerList render_surface_layer_list;
1266 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1267 root.get(), root->bounds(), translate, &render_surface_layer_list);
1268 inputs.page_scale_factor = page_scale_factor;
1269 inputs.page_scale_application_layer = root.get();
1270 inputs.can_adjust_raster_scales = true;
1271 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1272 gfx::Transform page_scaled_translate = translate;
1273 page_scaled_translate.Scale(page_scale_factor, page_scale_factor);
1274 EXPECT_EQ(translate, root->draw_properties().target_space_transform);
1275 EXPECT_EQ(page_scaled_translate,
1276 child->draw_properties().target_space_transform);
1277 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1278 EXPECT_EQ(1.f, root->draw_properties().device_scale_factor);
1279 EXPECT_EQ(1.f, child->draw_properties().device_scale_factor);
1282 // Verify that it composes correctly with transforms directly on root layer.
1283 root->SetTransform(composite);
1286 RenderSurfaceLayerList render_surface_layer_list;
1287 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1288 root.get(), root->bounds(), composite, &render_surface_layer_list);
1289 inputs.can_adjust_raster_scales = true;
1290 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1291 gfx::Transform compositeSquared = composite;
1292 compositeSquared.ConcatTransform(composite);
1293 EXPECT_TRANSFORMATION_MATRIX_EQ(
1294 compositeSquared, root->draw_properties().target_space_transform);
1295 EXPECT_TRANSFORMATION_MATRIX_EQ(
1296 compositeSquared, child->draw_properties().target_space_transform);
1297 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1301 TEST_F(LayerTreeHostCommonTest,
1302 RenderSurfaceListForRenderSurfaceWithClippedLayer) {
1303 scoped_refptr<Layer> parent = Layer::Create();
1304 scoped_refptr<Layer> render_surface1 = Layer::Create();
1305 scoped_refptr<LayerWithForcedDrawsContent> child =
1306 make_scoped_refptr(new LayerWithForcedDrawsContent());
1308 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1309 host->SetRootLayer(parent);
1311 const gfx::Transform identity_matrix;
1312 SetLayerPropertiesForTesting(parent.get(),
1313 identity_matrix,
1314 gfx::Point3F(),
1315 gfx::PointF(),
1316 gfx::Size(10, 10),
1317 true,
1318 false);
1319 SetLayerPropertiesForTesting(render_surface1.get(),
1320 identity_matrix,
1321 gfx::Point3F(),
1322 gfx::PointF(),
1323 gfx::Size(10, 10),
1324 true,
1325 false);
1326 SetLayerPropertiesForTesting(child.get(),
1327 identity_matrix,
1328 gfx::Point3F(),
1329 gfx::PointF(30.f, 30.f),
1330 gfx::Size(10, 10),
1331 true,
1332 false);
1334 parent->AddChild(render_surface1);
1335 parent->SetMasksToBounds(true);
1336 render_surface1->AddChild(child);
1337 render_surface1->SetForceRenderSurface(true);
1339 RenderSurfaceLayerList render_surface_layer_list;
1340 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1341 parent.get(),
1342 parent->bounds(),
1343 gfx::Transform(),
1344 &render_surface_layer_list);
1345 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1347 // The child layer's content is entirely outside the parent's clip rect, so
1348 // the intermediate render surface should not be listed here, even if it was
1349 // forced to be created. Render surfaces without children or visible content
1350 // are unexpected at draw time (e.g. we might try to create a content texture
1351 // of size 0).
1352 ASSERT_TRUE(parent->render_surface());
1353 EXPECT_EQ(1U, render_surface_layer_list.size());
1356 TEST_F(LayerTreeHostCommonTest, RenderSurfaceListForTransparentChild) {
1357 scoped_refptr<Layer> parent = Layer::Create();
1358 scoped_refptr<Layer> render_surface1 = Layer::Create();
1359 scoped_refptr<LayerWithForcedDrawsContent> child =
1360 make_scoped_refptr(new LayerWithForcedDrawsContent());
1362 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1363 host->SetRootLayer(parent);
1365 const gfx::Transform identity_matrix;
1366 SetLayerPropertiesForTesting(render_surface1.get(),
1367 identity_matrix,
1368 gfx::Point3F(),
1369 gfx::PointF(),
1370 gfx::Size(10, 10),
1371 true,
1372 false);
1373 SetLayerPropertiesForTesting(child.get(),
1374 identity_matrix,
1375 gfx::Point3F(),
1376 gfx::PointF(),
1377 gfx::Size(10, 10),
1378 true,
1379 false);
1381 parent->AddChild(render_surface1);
1382 render_surface1->AddChild(child);
1383 render_surface1->SetForceRenderSurface(true);
1384 render_surface1->SetOpacity(0.f);
1386 RenderSurfaceLayerList render_surface_layer_list;
1387 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1388 parent.get(), 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();
1404 scoped_refptr<LayerWithForcedDrawsContent> child =
1405 make_scoped_refptr(new LayerWithForcedDrawsContent());
1407 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1408 host->SetRootLayer(parent);
1410 const gfx::Transform identity_matrix;
1411 const SkXfermode::Mode blend_mode = SkXfermode::kMultiply_Mode;
1412 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
1413 gfx::PointF(), gfx::Size(10, 10), true, false);
1415 parent->AddChild(child);
1416 child->SetBlendMode(blend_mode);
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(1U, child->render_surface()->layer_list().size());
1429 EXPECT_EQ(SkXfermode::kSrcOver_Mode, child->draw_properties().blend_mode);
1432 TEST_F(LayerTreeHostCommonTest, ForceRenderSurface) {
1433 scoped_refptr<Layer> parent = Layer::Create();
1434 scoped_refptr<Layer> render_surface1 = Layer::Create();
1435 scoped_refptr<LayerWithForcedDrawsContent> child =
1436 make_scoped_refptr(new LayerWithForcedDrawsContent());
1437 render_surface1->SetForceRenderSurface(true);
1439 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
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());
1482 EXPECT_EQ(2U, render_surface_layer_list.size());
1486 RenderSurfaceLayerList render_surface_layer_list;
1487 render_surface1->SetForceRenderSurface(false);
1488 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1489 parent.get(), parent->bounds(), &render_surface_layer_list);
1490 inputs.can_adjust_raster_scales = true;
1491 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1492 EXPECT_TRUE(parent->render_surface());
1493 EXPECT_FALSE(render_surface1->render_surface());
1494 EXPECT_EQ(1U, render_surface_layer_list.size());
1498 TEST_F(LayerTreeHostCommonTest, RenderSurfacesFlattenScreenSpaceTransform) {
1499 // Render surfaces act as a flattening point for their subtree, so should
1500 // always flatten the target-to-screen space transform seen by descendants.
1502 scoped_refptr<Layer> root = Layer::Create();
1503 scoped_refptr<Layer> parent = Layer::Create();
1504 scoped_refptr<LayerWithForcedDrawsContent> child =
1505 make_scoped_refptr(new LayerWithForcedDrawsContent());
1506 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1507 make_scoped_refptr(new LayerWithForcedDrawsContent());
1509 gfx::Transform rotation_about_y_axis;
1510 rotation_about_y_axis.RotateAboutYAxis(30.0);
1511 // Make |parent| have a render surface.
1512 parent->SetOpacity(0.9f);
1514 const gfx::Transform identity_matrix;
1515 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
1516 gfx::PointF(), gfx::Size(100, 100), true, false);
1517 SetLayerPropertiesForTesting(parent.get(), rotation_about_y_axis,
1518 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1519 true, false);
1520 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
1521 gfx::PointF(), gfx::Size(10, 10), true, false);
1522 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
1523 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1524 true, false);
1526 root->AddChild(parent);
1527 parent->AddChild(child);
1528 child->AddChild(grand_child);
1530 grand_child->SetShouldFlattenTransform(false);
1532 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1533 host->SetRootLayer(root);
1535 // Only grand_child should preserve 3d.
1536 EXPECT_TRUE(root->should_flatten_transform());
1537 EXPECT_TRUE(parent->should_flatten_transform());
1538 EXPECT_TRUE(child->should_flatten_transform());
1539 EXPECT_FALSE(grand_child->should_flatten_transform());
1541 gfx::Transform expected_child_draw_transform = identity_matrix;
1542 gfx::Transform expected_grand_child_draw_transform = identity_matrix;
1544 gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
1545 flattened_rotation_about_y.FlattenTo2d();
1547 ExecuteCalculateDrawProperties(root.get());
1549 EXPECT_TRUE(parent->render_surface());
1550 EXPECT_FALSE(child->render_surface());
1551 EXPECT_FALSE(grand_child->render_surface());
1553 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1554 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1555 grand_child->draw_transform());
1557 // The screen-space transform inherited by |child| and |grand_child| should
1558 // have been flattened at their render target. In particular, the fact that
1559 // |grand_child| happens to preserve 3d shouldn't affect this flattening.
1560 EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
1561 child->screen_space_transform());
1562 EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
1563 grand_child->screen_space_transform());
1566 TEST_F(LayerTreeHostCommonTest, ClipRectCullsRenderSurfaces) {
1567 // The entire subtree of layers that are outside the clip rect should be
1568 // culled away, and should not affect the render_surface_layer_list.
1570 // The test tree is set up as follows:
1571 // - all layers except the leaf_nodes are forced to be a new render surface
1572 // that have something to draw.
1573 // - parent is a large container layer.
1574 // - child has masksToBounds=true to cause clipping.
1575 // - grand_child is positioned outside of the child's bounds
1576 // - great_grand_child is also kept outside child's bounds.
1578 // In this configuration, grand_child and great_grand_child are completely
1579 // outside the clip rect, and they should never get scheduled on the list of
1580 // render surfaces.
1583 const gfx::Transform identity_matrix;
1584 scoped_refptr<Layer> parent = Layer::Create();
1585 scoped_refptr<Layer> child = Layer::Create();
1586 scoped_refptr<Layer> grand_child = Layer::Create();
1587 scoped_refptr<Layer> great_grand_child = Layer::Create();
1588 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
1589 make_scoped_refptr(new LayerWithForcedDrawsContent());
1590 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
1591 make_scoped_refptr(new LayerWithForcedDrawsContent());
1592 parent->AddChild(child);
1593 child->AddChild(grand_child);
1594 grand_child->AddChild(great_grand_child);
1596 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1597 host->SetRootLayer(parent);
1599 // leaf_node1 ensures that parent and child are kept on the
1600 // render_surface_layer_list, even though grand_child and great_grand_child
1601 // should be clipped.
1602 child->AddChild(leaf_node1);
1603 great_grand_child->AddChild(leaf_node2);
1605 SetLayerPropertiesForTesting(parent.get(),
1606 identity_matrix,
1607 gfx::Point3F(),
1608 gfx::PointF(),
1609 gfx::Size(500, 500),
1610 true,
1611 false);
1612 SetLayerPropertiesForTesting(child.get(),
1613 identity_matrix,
1614 gfx::Point3F(),
1615 gfx::PointF(),
1616 gfx::Size(20, 20),
1617 true,
1618 false);
1619 SetLayerPropertiesForTesting(grand_child.get(),
1620 identity_matrix,
1621 gfx::Point3F(),
1622 gfx::PointF(45.f, 45.f),
1623 gfx::Size(10, 10),
1624 true,
1625 false);
1626 SetLayerPropertiesForTesting(great_grand_child.get(),
1627 identity_matrix,
1628 gfx::Point3F(),
1629 gfx::PointF(),
1630 gfx::Size(10, 10),
1631 true,
1632 false);
1633 SetLayerPropertiesForTesting(leaf_node1.get(),
1634 identity_matrix,
1635 gfx::Point3F(),
1636 gfx::PointF(),
1637 gfx::Size(500, 500),
1638 true,
1639 false);
1640 SetLayerPropertiesForTesting(leaf_node2.get(),
1641 identity_matrix,
1642 gfx::Point3F(),
1643 gfx::PointF(),
1644 gfx::Size(20, 20),
1645 true,
1646 false);
1648 child->SetMasksToBounds(true);
1649 child->SetOpacity(0.4f);
1650 child->SetForceRenderSurface(true);
1651 grand_child->SetOpacity(0.5f);
1652 great_grand_child->SetOpacity(0.4f);
1654 RenderSurfaceLayerList render_surface_layer_list;
1655 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1656 parent.get(), parent->bounds(), &render_surface_layer_list);
1657 inputs.can_adjust_raster_scales = true;
1658 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1660 ASSERT_EQ(2U, render_surface_layer_list.size());
1661 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1662 EXPECT_EQ(child->id(), render_surface_layer_list.at(1)->id());
1665 TEST_F(LayerTreeHostCommonTest, ClipRectCullsSurfaceWithoutVisibleContent) {
1666 // When a render surface has a clip rect, it is used to clip the content rect
1667 // of the surface. When the render surface is animating its transforms, then
1668 // the content rect's position in the clip rect is not defined on the main
1669 // thread, and its content rect should not be clipped.
1671 // The test tree is set up as follows:
1672 // - parent is a container layer that masksToBounds=true to cause clipping.
1673 // - child is a render surface, which has a clip rect set to the bounds of
1674 // the parent.
1675 // - grand_child is a render surface, and the only visible content in child.
1676 // It is positioned outside of the clip rect from parent.
1678 // In this configuration, grand_child should be outside the clipped
1679 // content rect of the child, making grand_child not appear in the
1680 // render_surface_layer_list. However, when we place an animation on the
1681 // child, this clipping should be avoided and we should keep the grand_child
1682 // in the render_surface_layer_list.
1684 const gfx::Transform identity_matrix;
1685 scoped_refptr<Layer> parent = Layer::Create();
1686 scoped_refptr<Layer> child = Layer::Create();
1687 scoped_refptr<Layer> grand_child = Layer::Create();
1688 scoped_refptr<LayerWithForcedDrawsContent> leaf_node =
1689 make_scoped_refptr(new LayerWithForcedDrawsContent());
1690 parent->AddChild(child);
1691 child->AddChild(grand_child);
1692 grand_child->AddChild(leaf_node);
1694 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1695 host->SetRootLayer(parent);
1697 SetLayerPropertiesForTesting(parent.get(),
1698 identity_matrix,
1699 gfx::Point3F(),
1700 gfx::PointF(),
1701 gfx::Size(100, 100),
1702 true,
1703 false);
1704 SetLayerPropertiesForTesting(child.get(),
1705 identity_matrix,
1706 gfx::Point3F(),
1707 gfx::PointF(),
1708 gfx::Size(20, 20),
1709 true,
1710 false);
1711 SetLayerPropertiesForTesting(grand_child.get(),
1712 identity_matrix,
1713 gfx::Point3F(),
1714 gfx::PointF(200.f, 200.f),
1715 gfx::Size(10, 10),
1716 true,
1717 false);
1718 SetLayerPropertiesForTesting(leaf_node.get(),
1719 identity_matrix,
1720 gfx::Point3F(),
1721 gfx::PointF(),
1722 gfx::Size(10, 10),
1723 true,
1724 false);
1726 parent->SetMasksToBounds(true);
1727 child->SetOpacity(0.4f);
1728 child->SetForceRenderSurface(true);
1729 grand_child->SetOpacity(0.4f);
1730 grand_child->SetForceRenderSurface(true);
1733 RenderSurfaceLayerList render_surface_layer_list;
1734 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1735 parent.get(), parent->bounds(), &render_surface_layer_list);
1736 inputs.can_adjust_raster_scales = true;
1737 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1739 // Without an animation, we should cull child and grand_child from the
1740 // render_surface_layer_list.
1741 ASSERT_EQ(1U, render_surface_layer_list.size());
1742 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1745 // Now put an animating transform on child.
1746 AddAnimatedTransformToController(
1747 child->layer_animation_controller(), 10.0, 30, 0);
1750 RenderSurfaceLayerList render_surface_layer_list;
1751 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1752 parent.get(), parent->bounds(), &render_surface_layer_list);
1753 inputs.can_adjust_raster_scales = true;
1754 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1756 // With an animating transform, we should keep child and grand_child in the
1757 // render_surface_layer_list.
1758 ASSERT_EQ(3U, render_surface_layer_list.size());
1759 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1760 EXPECT_EQ(child->id(), render_surface_layer_list.at(1)->id());
1761 EXPECT_EQ(grand_child->id(), render_surface_layer_list.at(2)->id());
1765 TEST_F(LayerTreeHostCommonTest, IsClippedIsSetCorrectly) {
1766 // Layer's IsClipped() property is set to true when:
1767 // - the layer clips its subtree, e.g. masks to bounds,
1768 // - the layer is clipped by an ancestor that contributes to the same
1769 // render target,
1770 // - a surface is clipped by an ancestor that contributes to the same
1771 // render target.
1773 // In particular, for a layer that owns a render surface:
1774 // - the render surface inherits any clip from ancestors, and does NOT
1775 // pass that clipped status to the layer itself.
1776 // - but if the layer itself masks to bounds, it is considered clipped
1777 // and propagates the clip to the subtree.
1779 const gfx::Transform identity_matrix;
1780 scoped_refptr<Layer> root = Layer::Create();
1781 scoped_refptr<Layer> parent = Layer::Create();
1782 scoped_refptr<Layer> child1 = Layer::Create();
1783 scoped_refptr<Layer> child2 = Layer::Create();
1784 scoped_refptr<Layer> grand_child = Layer::Create();
1785 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
1786 make_scoped_refptr(new LayerWithForcedDrawsContent());
1787 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
1788 make_scoped_refptr(new LayerWithForcedDrawsContent());
1789 root->AddChild(parent);
1790 parent->AddChild(child1);
1791 parent->AddChild(child2);
1792 child1->AddChild(grand_child);
1793 child2->AddChild(leaf_node2);
1794 grand_child->AddChild(leaf_node1);
1796 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1797 host->SetRootLayer(root);
1799 child2->SetForceRenderSurface(true);
1801 SetLayerPropertiesForTesting(root.get(),
1802 identity_matrix,
1803 gfx::Point3F(),
1804 gfx::PointF(),
1805 gfx::Size(100, 100),
1806 true,
1807 false);
1808 SetLayerPropertiesForTesting(parent.get(),
1809 identity_matrix,
1810 gfx::Point3F(),
1811 gfx::PointF(),
1812 gfx::Size(100, 100),
1813 true,
1814 false);
1815 SetLayerPropertiesForTesting(child1.get(),
1816 identity_matrix,
1817 gfx::Point3F(),
1818 gfx::PointF(),
1819 gfx::Size(100, 100),
1820 true,
1821 false);
1822 SetLayerPropertiesForTesting(child2.get(),
1823 identity_matrix,
1824 gfx::Point3F(),
1825 gfx::PointF(),
1826 gfx::Size(100, 100),
1827 true,
1828 false);
1829 SetLayerPropertiesForTesting(grand_child.get(),
1830 identity_matrix,
1831 gfx::Point3F(),
1832 gfx::PointF(),
1833 gfx::Size(100, 100),
1834 true,
1835 false);
1836 SetLayerPropertiesForTesting(leaf_node1.get(),
1837 identity_matrix,
1838 gfx::Point3F(),
1839 gfx::PointF(),
1840 gfx::Size(100, 100),
1841 true,
1842 false);
1843 SetLayerPropertiesForTesting(leaf_node2.get(),
1844 identity_matrix,
1845 gfx::Point3F(),
1846 gfx::PointF(),
1847 gfx::Size(100, 100),
1848 true,
1849 false);
1851 // Case 1: nothing is clipped except the root render surface.
1853 RenderSurfaceLayerList render_surface_layer_list;
1854 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1855 root.get(), parent->bounds(), &render_surface_layer_list);
1856 inputs.can_adjust_raster_scales = true;
1857 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1859 ASSERT_TRUE(root->render_surface());
1860 ASSERT_TRUE(child2->render_surface());
1862 EXPECT_FALSE(root->is_clipped());
1863 EXPECT_TRUE(root->render_surface()->is_clipped());
1864 EXPECT_FALSE(parent->is_clipped());
1865 EXPECT_FALSE(child1->is_clipped());
1866 EXPECT_FALSE(child2->is_clipped());
1867 EXPECT_FALSE(child2->render_surface()->is_clipped());
1868 EXPECT_FALSE(grand_child->is_clipped());
1869 EXPECT_FALSE(leaf_node1->is_clipped());
1870 EXPECT_FALSE(leaf_node2->is_clipped());
1873 // Case 2: parent masksToBounds, so the parent, child1, and child2's
1874 // surface are clipped. But layers that contribute to child2's surface are
1875 // not clipped explicitly because child2's surface already accounts for
1876 // that clip.
1878 RenderSurfaceLayerList render_surface_layer_list;
1879 parent->SetMasksToBounds(true);
1880 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1881 root.get(), parent->bounds(), &render_surface_layer_list);
1882 inputs.can_adjust_raster_scales = true;
1883 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1885 ASSERT_TRUE(root->render_surface());
1886 ASSERT_TRUE(child2->render_surface());
1888 EXPECT_FALSE(root->is_clipped());
1889 EXPECT_TRUE(root->render_surface()->is_clipped());
1890 EXPECT_TRUE(parent->is_clipped());
1891 EXPECT_TRUE(child1->is_clipped());
1892 EXPECT_FALSE(child2->is_clipped());
1893 EXPECT_TRUE(child2->render_surface()->is_clipped());
1894 EXPECT_TRUE(grand_child->is_clipped());
1895 EXPECT_TRUE(leaf_node1->is_clipped());
1896 EXPECT_FALSE(leaf_node2->is_clipped());
1899 // Case 3: child2 masksToBounds. The layer and subtree are clipped, and
1900 // child2's render surface is not clipped.
1902 RenderSurfaceLayerList render_surface_layer_list;
1903 parent->SetMasksToBounds(false);
1904 child2->SetMasksToBounds(true);
1905 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1906 root.get(), parent->bounds(), &render_surface_layer_list);
1907 inputs.can_adjust_raster_scales = true;
1908 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1910 ASSERT_TRUE(root->render_surface());
1911 ASSERT_TRUE(child2->render_surface());
1913 EXPECT_FALSE(root->is_clipped());
1914 EXPECT_TRUE(root->render_surface()->is_clipped());
1915 EXPECT_FALSE(parent->is_clipped());
1916 EXPECT_FALSE(child1->is_clipped());
1917 EXPECT_TRUE(child2->is_clipped());
1918 EXPECT_FALSE(child2->render_surface()->is_clipped());
1919 EXPECT_FALSE(grand_child->is_clipped());
1920 EXPECT_FALSE(leaf_node1->is_clipped());
1921 EXPECT_TRUE(leaf_node2->is_clipped());
1925 TEST_F(LayerTreeHostCommonTest, DrawableContentRectForLayers) {
1926 // Verify that layers get the appropriate DrawableContentRect when their
1927 // parent masksToBounds is true.
1929 // grand_child1 - completely inside the region; DrawableContentRect should
1930 // be the layer rect expressed in target space.
1931 // grand_child2 - partially clipped but NOT masksToBounds; the clip rect
1932 // will be the intersection of layer bounds and the mask region.
1933 // grand_child3 - partially clipped and masksToBounds; the
1934 // DrawableContentRect will still be the intersection of layer bounds and
1935 // the mask region.
1936 // grand_child4 - outside parent's clip rect; the DrawableContentRect should
1937 // be empty.
1940 const gfx::Transform identity_matrix;
1941 scoped_refptr<Layer> parent = Layer::Create();
1942 scoped_refptr<Layer> child = Layer::Create();
1943 scoped_refptr<Layer> grand_child1 = Layer::Create();
1944 scoped_refptr<Layer> grand_child2 = Layer::Create();
1945 scoped_refptr<Layer> grand_child3 = Layer::Create();
1946 scoped_refptr<Layer> grand_child4 = Layer::Create();
1948 parent->AddChild(child);
1949 child->AddChild(grand_child1);
1950 child->AddChild(grand_child2);
1951 child->AddChild(grand_child3);
1952 child->AddChild(grand_child4);
1954 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1955 host->SetRootLayer(parent);
1957 SetLayerPropertiesForTesting(parent.get(),
1958 identity_matrix,
1959 gfx::Point3F(),
1960 gfx::PointF(),
1961 gfx::Size(500, 500),
1962 true,
1963 false);
1964 SetLayerPropertiesForTesting(child.get(),
1965 identity_matrix,
1966 gfx::Point3F(),
1967 gfx::PointF(),
1968 gfx::Size(20, 20),
1969 true,
1970 false);
1971 SetLayerPropertiesForTesting(grand_child1.get(),
1972 identity_matrix,
1973 gfx::Point3F(),
1974 gfx::PointF(5.f, 5.f),
1975 gfx::Size(10, 10),
1976 true,
1977 false);
1978 SetLayerPropertiesForTesting(grand_child2.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_child3.get(),
1986 identity_matrix,
1987 gfx::Point3F(),
1988 gfx::PointF(15.f, 15.f),
1989 gfx::Size(10, 10),
1990 true,
1991 false);
1992 SetLayerPropertiesForTesting(grand_child4.get(),
1993 identity_matrix,
1994 gfx::Point3F(),
1995 gfx::PointF(45.f, 45.f),
1996 gfx::Size(10, 10),
1997 true,
1998 false);
2000 child->SetMasksToBounds(true);
2001 grand_child3->SetMasksToBounds(true);
2003 // Force everyone to be a render surface.
2004 child->SetOpacity(0.4f);
2005 grand_child1->SetOpacity(0.5f);
2006 grand_child2->SetOpacity(0.5f);
2007 grand_child3->SetOpacity(0.5f);
2008 grand_child4->SetOpacity(0.5f);
2010 RenderSurfaceLayerList render_surface_layer_list;
2011 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2012 parent.get(), parent->bounds(), &render_surface_layer_list);
2013 inputs.can_adjust_raster_scales = true;
2014 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2016 EXPECT_EQ(gfx::Rect(5, 5, 10, 10), grand_child1->drawable_content_rect());
2017 EXPECT_EQ(gfx::Rect(15, 15, 5, 5), grand_child3->drawable_content_rect());
2018 EXPECT_EQ(gfx::Rect(15, 15, 5, 5), grand_child3->drawable_content_rect());
2019 EXPECT_TRUE(grand_child4->drawable_content_rect().IsEmpty());
2022 TEST_F(LayerTreeHostCommonTest, ClipRectIsPropagatedCorrectlyToSurfaces) {
2023 // Verify that render surfaces (and their layers) get the appropriate
2024 // clip rects when their parent masksToBounds is true.
2026 // Layers that own render surfaces (at least for now) do not inherit any
2027 // clipping; instead the surface will enforce the clip for the entire subtree.
2028 // They may still have a clip rect of their own layer bounds, however, if
2029 // masksToBounds was true.
2030 const gfx::Transform identity_matrix;
2031 scoped_refptr<Layer> parent = Layer::Create();
2032 scoped_refptr<Layer> child = Layer::Create();
2033 scoped_refptr<Layer> grand_child1 = Layer::Create();
2034 scoped_refptr<Layer> grand_child2 = Layer::Create();
2035 scoped_refptr<Layer> grand_child3 = Layer::Create();
2036 scoped_refptr<Layer> grand_child4 = Layer::Create();
2037 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
2038 make_scoped_refptr(new LayerWithForcedDrawsContent());
2039 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
2040 make_scoped_refptr(new LayerWithForcedDrawsContent());
2041 scoped_refptr<LayerWithForcedDrawsContent> leaf_node3 =
2042 make_scoped_refptr(new LayerWithForcedDrawsContent());
2043 scoped_refptr<LayerWithForcedDrawsContent> leaf_node4 =
2044 make_scoped_refptr(new LayerWithForcedDrawsContent());
2046 parent->AddChild(child);
2047 child->AddChild(grand_child1);
2048 child->AddChild(grand_child2);
2049 child->AddChild(grand_child3);
2050 child->AddChild(grand_child4);
2052 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2053 host->SetRootLayer(parent);
2055 // the leaf nodes ensure that these grand_children become render surfaces for
2056 // this test.
2057 grand_child1->AddChild(leaf_node1);
2058 grand_child2->AddChild(leaf_node2);
2059 grand_child3->AddChild(leaf_node3);
2060 grand_child4->AddChild(leaf_node4);
2062 SetLayerPropertiesForTesting(parent.get(),
2063 identity_matrix,
2064 gfx::Point3F(),
2065 gfx::PointF(),
2066 gfx::Size(500, 500),
2067 true,
2068 false);
2069 SetLayerPropertiesForTesting(child.get(),
2070 identity_matrix,
2071 gfx::Point3F(),
2072 gfx::PointF(),
2073 gfx::Size(20, 20),
2074 true,
2075 false);
2076 SetLayerPropertiesForTesting(grand_child1.get(),
2077 identity_matrix,
2078 gfx::Point3F(),
2079 gfx::PointF(5.f, 5.f),
2080 gfx::Size(10, 10),
2081 true,
2082 false);
2083 SetLayerPropertiesForTesting(grand_child2.get(),
2084 identity_matrix,
2085 gfx::Point3F(),
2086 gfx::PointF(15.f, 15.f),
2087 gfx::Size(10, 10),
2088 true,
2089 false);
2090 SetLayerPropertiesForTesting(grand_child3.get(),
2091 identity_matrix,
2092 gfx::Point3F(),
2093 gfx::PointF(15.f, 15.f),
2094 gfx::Size(10, 10),
2095 true,
2096 false);
2097 SetLayerPropertiesForTesting(grand_child4.get(),
2098 identity_matrix,
2099 gfx::Point3F(),
2100 gfx::PointF(45.f, 45.f),
2101 gfx::Size(10, 10),
2102 true,
2103 false);
2104 SetLayerPropertiesForTesting(leaf_node1.get(),
2105 identity_matrix,
2106 gfx::Point3F(),
2107 gfx::PointF(),
2108 gfx::Size(10, 10),
2109 true,
2110 false);
2111 SetLayerPropertiesForTesting(leaf_node2.get(),
2112 identity_matrix,
2113 gfx::Point3F(),
2114 gfx::PointF(),
2115 gfx::Size(10, 10),
2116 true,
2117 false);
2118 SetLayerPropertiesForTesting(leaf_node3.get(),
2119 identity_matrix,
2120 gfx::Point3F(),
2121 gfx::PointF(),
2122 gfx::Size(10, 10),
2123 true,
2124 false);
2125 SetLayerPropertiesForTesting(leaf_node4.get(),
2126 identity_matrix,
2127 gfx::Point3F(),
2128 gfx::PointF(),
2129 gfx::Size(10, 10),
2130 true,
2131 false);
2133 child->SetMasksToBounds(true);
2134 grand_child3->SetMasksToBounds(true);
2135 grand_child4->SetMasksToBounds(true);
2137 // Force everyone to be a render surface.
2138 child->SetOpacity(0.4f);
2139 child->SetForceRenderSurface(true);
2140 grand_child1->SetOpacity(0.5f);
2141 grand_child1->SetForceRenderSurface(true);
2142 grand_child2->SetOpacity(0.5f);
2143 grand_child2->SetForceRenderSurface(true);
2144 grand_child3->SetOpacity(0.5f);
2145 grand_child3->SetForceRenderSurface(true);
2146 grand_child4->SetOpacity(0.5f);
2147 grand_child4->SetForceRenderSurface(true);
2149 RenderSurfaceLayerList render_surface_layer_list;
2150 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2151 parent.get(), parent->bounds(), &render_surface_layer_list);
2152 inputs.can_adjust_raster_scales = true;
2153 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2154 ASSERT_TRUE(grand_child1->render_surface());
2155 ASSERT_TRUE(grand_child2->render_surface());
2156 ASSERT_TRUE(grand_child3->render_surface());
2158 // Surfaces are clipped by their parent, but un-affected by the owning layer's
2159 // masksToBounds.
2160 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2161 grand_child1->render_surface()->clip_rect());
2162 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2163 grand_child2->render_surface()->clip_rect());
2164 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2165 grand_child3->render_surface()->clip_rect());
2168 TEST_F(LayerTreeHostCommonTest, AnimationsForRenderSurfaceHierarchy) {
2169 scoped_refptr<Layer> parent = Layer::Create();
2170 scoped_refptr<Layer> render_surface1 = Layer::Create();
2171 scoped_refptr<Layer> render_surface2 = Layer::Create();
2172 scoped_refptr<Layer> child_of_root = Layer::Create();
2173 scoped_refptr<Layer> child_of_rs1 = Layer::Create();
2174 scoped_refptr<Layer> child_of_rs2 = Layer::Create();
2175 scoped_refptr<Layer> grand_child_of_root = Layer::Create();
2176 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs1 =
2177 make_scoped_refptr(new LayerWithForcedDrawsContent());
2178 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs2 =
2179 make_scoped_refptr(new LayerWithForcedDrawsContent());
2180 parent->AddChild(render_surface1);
2181 parent->AddChild(child_of_root);
2182 render_surface1->AddChild(child_of_rs1);
2183 render_surface1->AddChild(render_surface2);
2184 render_surface2->AddChild(child_of_rs2);
2185 child_of_root->AddChild(grand_child_of_root);
2186 child_of_rs1->AddChild(grand_child_of_rs1);
2187 child_of_rs2->AddChild(grand_child_of_rs2);
2189 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2190 host->SetRootLayer(parent);
2192 // Make our render surfaces.
2193 render_surface1->SetForceRenderSurface(true);
2194 render_surface2->SetForceRenderSurface(true);
2196 gfx::Transform layer_transform;
2197 layer_transform.Translate(1.0, 1.0);
2199 SetLayerPropertiesForTesting(parent.get(),
2200 layer_transform,
2201 gfx::Point3F(0.25f, 0.f, 0.f),
2202 gfx::PointF(2.5f, 0.f),
2203 gfx::Size(10, 10),
2204 true,
2205 false);
2206 SetLayerPropertiesForTesting(render_surface1.get(),
2207 layer_transform,
2208 gfx::Point3F(0.25f, 0.f, 0.f),
2209 gfx::PointF(2.5f, 0.f),
2210 gfx::Size(10, 10),
2211 true,
2212 false);
2213 SetLayerPropertiesForTesting(render_surface2.get(),
2214 layer_transform,
2215 gfx::Point3F(0.25f, 0.f, 0.f),
2216 gfx::PointF(2.5f, 0.f),
2217 gfx::Size(10, 10),
2218 true,
2219 false);
2220 SetLayerPropertiesForTesting(child_of_root.get(),
2221 layer_transform,
2222 gfx::Point3F(0.25f, 0.f, 0.f),
2223 gfx::PointF(2.5f, 0.f),
2224 gfx::Size(10, 10),
2225 true,
2226 false);
2227 SetLayerPropertiesForTesting(child_of_rs1.get(),
2228 layer_transform,
2229 gfx::Point3F(0.25f, 0.f, 0.f),
2230 gfx::PointF(2.5f, 0.f),
2231 gfx::Size(10, 10),
2232 true,
2233 false);
2234 SetLayerPropertiesForTesting(child_of_rs2.get(),
2235 layer_transform,
2236 gfx::Point3F(0.25f, 0.f, 0.f),
2237 gfx::PointF(2.5f, 0.f),
2238 gfx::Size(10, 10),
2239 true,
2240 false);
2241 SetLayerPropertiesForTesting(grand_child_of_root.get(),
2242 layer_transform,
2243 gfx::Point3F(0.25f, 0.f, 0.f),
2244 gfx::PointF(2.5f, 0.f),
2245 gfx::Size(10, 10),
2246 true,
2247 false);
2248 SetLayerPropertiesForTesting(grand_child_of_rs1.get(),
2249 layer_transform,
2250 gfx::Point3F(0.25f, 0.f, 0.f),
2251 gfx::PointF(2.5f, 0.f),
2252 gfx::Size(10, 10),
2253 true,
2254 false);
2255 SetLayerPropertiesForTesting(grand_child_of_rs2.get(),
2256 layer_transform,
2257 gfx::Point3F(0.25f, 0.f, 0.f),
2258 gfx::PointF(2.5f, 0.f),
2259 gfx::Size(10, 10),
2260 true,
2261 false);
2263 // Put an animated opacity on the render surface.
2264 AddOpacityTransitionToController(
2265 render_surface1->layer_animation_controller(), 10.0, 1.f, 0.f, false);
2267 // Also put an animated opacity on a layer without descendants.
2268 AddOpacityTransitionToController(
2269 grand_child_of_root->layer_animation_controller(), 10.0, 1.f, 0.f, false);
2271 // Put a transform animation on the render surface.
2272 AddAnimatedTransformToController(
2273 render_surface2->layer_animation_controller(), 10.0, 30, 0);
2275 // Also put transform animations on grand_child_of_root, and
2276 // grand_child_of_rs2
2277 AddAnimatedTransformToController(
2278 grand_child_of_root->layer_animation_controller(), 10.0, 30, 0);
2279 AddAnimatedTransformToController(
2280 grand_child_of_rs2->layer_animation_controller(), 10.0, 30, 0);
2282 ExecuteCalculateDrawProperties(parent.get());
2284 // Only layers that are associated with render surfaces should have an actual
2285 // RenderSurface() value.
2286 ASSERT_TRUE(parent->render_surface());
2287 ASSERT_FALSE(child_of_root->render_surface());
2288 ASSERT_FALSE(grand_child_of_root->render_surface());
2290 ASSERT_TRUE(render_surface1->render_surface());
2291 ASSERT_FALSE(child_of_rs1->render_surface());
2292 ASSERT_FALSE(grand_child_of_rs1->render_surface());
2294 ASSERT_TRUE(render_surface2->render_surface());
2295 ASSERT_FALSE(child_of_rs2->render_surface());
2296 ASSERT_FALSE(grand_child_of_rs2->render_surface());
2298 // Verify all render target accessors
2299 EXPECT_EQ(parent.get(), parent->render_target());
2300 EXPECT_EQ(parent.get(), child_of_root->render_target());
2301 EXPECT_EQ(parent.get(), grand_child_of_root->render_target());
2303 EXPECT_EQ(render_surface1.get(), render_surface1->render_target());
2304 EXPECT_EQ(render_surface1.get(), child_of_rs1->render_target());
2305 EXPECT_EQ(render_surface1.get(), grand_child_of_rs1->render_target());
2307 EXPECT_EQ(render_surface2.get(), render_surface2->render_target());
2308 EXPECT_EQ(render_surface2.get(), child_of_rs2->render_target());
2309 EXPECT_EQ(render_surface2.get(), grand_child_of_rs2->render_target());
2311 // Verify draw_opacity_is_animating values
2312 EXPECT_FALSE(parent->draw_opacity_is_animating());
2313 EXPECT_FALSE(child_of_root->draw_opacity_is_animating());
2314 EXPECT_TRUE(grand_child_of_root->draw_opacity_is_animating());
2315 EXPECT_FALSE(render_surface1->draw_opacity_is_animating());
2316 EXPECT_TRUE(render_surface1->render_surface()->draw_opacity_is_animating());
2317 EXPECT_FALSE(child_of_rs1->draw_opacity_is_animating());
2318 EXPECT_FALSE(grand_child_of_rs1->draw_opacity_is_animating());
2319 EXPECT_FALSE(render_surface2->draw_opacity_is_animating());
2320 EXPECT_FALSE(render_surface2->render_surface()->draw_opacity_is_animating());
2321 EXPECT_FALSE(child_of_rs2->draw_opacity_is_animating());
2322 EXPECT_FALSE(grand_child_of_rs2->draw_opacity_is_animating());
2324 // Verify draw_transform_is_animating values
2325 EXPECT_FALSE(parent->draw_transform_is_animating());
2326 EXPECT_FALSE(child_of_root->draw_transform_is_animating());
2327 EXPECT_TRUE(grand_child_of_root->draw_transform_is_animating());
2328 EXPECT_FALSE(render_surface1->draw_transform_is_animating());
2329 EXPECT_FALSE(render_surface1->render_surface()
2330 ->target_surface_transforms_are_animating());
2331 EXPECT_FALSE(child_of_rs1->draw_transform_is_animating());
2332 EXPECT_FALSE(grand_child_of_rs1->draw_transform_is_animating());
2333 EXPECT_FALSE(render_surface2->draw_transform_is_animating());
2334 EXPECT_TRUE(render_surface2->render_surface()
2335 ->target_surface_transforms_are_animating());
2336 EXPECT_FALSE(child_of_rs2->draw_transform_is_animating());
2337 EXPECT_TRUE(grand_child_of_rs2->draw_transform_is_animating());
2339 // Verify screen_space_transform_is_animating values
2340 EXPECT_FALSE(parent->screen_space_transform_is_animating());
2341 EXPECT_FALSE(child_of_root->screen_space_transform_is_animating());
2342 EXPECT_TRUE(grand_child_of_root->screen_space_transform_is_animating());
2343 EXPECT_FALSE(render_surface1->screen_space_transform_is_animating());
2344 EXPECT_FALSE(render_surface1->render_surface()
2345 ->screen_space_transforms_are_animating());
2346 EXPECT_FALSE(child_of_rs1->screen_space_transform_is_animating());
2347 EXPECT_FALSE(grand_child_of_rs1->screen_space_transform_is_animating());
2348 EXPECT_TRUE(render_surface2->screen_space_transform_is_animating());
2349 EXPECT_TRUE(render_surface2->render_surface()
2350 ->screen_space_transforms_are_animating());
2351 EXPECT_TRUE(child_of_rs2->screen_space_transform_is_animating());
2352 EXPECT_TRUE(grand_child_of_rs2->screen_space_transform_is_animating());
2354 // Sanity check. If these fail there is probably a bug in the test itself.
2355 // It is expected that we correctly set up transforms so that the y-component
2356 // of the screen-space transform encodes the "depth" of the layer in the tree.
2357 EXPECT_FLOAT_EQ(1.0, parent->screen_space_transform().matrix().get(1, 3));
2358 EXPECT_FLOAT_EQ(2.0,
2359 child_of_root->screen_space_transform().matrix().get(1, 3));
2360 EXPECT_FLOAT_EQ(
2361 3.0, grand_child_of_root->screen_space_transform().matrix().get(1, 3));
2363 EXPECT_FLOAT_EQ(2.0,
2364 render_surface1->screen_space_transform().matrix().get(1, 3));
2365 EXPECT_FLOAT_EQ(3.0,
2366 child_of_rs1->screen_space_transform().matrix().get(1, 3));
2367 EXPECT_FLOAT_EQ(
2368 4.0, grand_child_of_rs1->screen_space_transform().matrix().get(1, 3));
2370 EXPECT_FLOAT_EQ(3.0,
2371 render_surface2->screen_space_transform().matrix().get(1, 3));
2372 EXPECT_FLOAT_EQ(4.0,
2373 child_of_rs2->screen_space_transform().matrix().get(1, 3));
2374 EXPECT_FLOAT_EQ(
2375 5.0, grand_child_of_rs2->screen_space_transform().matrix().get(1, 3));
2378 TEST_F(LayerTreeHostCommonTest, VisibleRectForIdentityTransform) {
2379 // Test the calculateVisibleRect() function works correctly for identity
2380 // transforms.
2382 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2383 gfx::Transform layer_to_surface_transform;
2385 // Case 1: Layer is contained within the surface.
2386 gfx::Rect layer_content_rect = gfx::Rect(10, 10, 30, 30);
2387 gfx::Rect expected = gfx::Rect(10, 10, 30, 30);
2388 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2389 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2390 EXPECT_EQ(expected, actual);
2392 // Case 2: Layer is outside the surface rect.
2393 layer_content_rect = gfx::Rect(120, 120, 30, 30);
2394 actual = LayerTreeHostCommon::CalculateVisibleRect(
2395 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2396 EXPECT_TRUE(actual.IsEmpty());
2398 // Case 3: Layer is partially overlapping the surface rect.
2399 layer_content_rect = gfx::Rect(80, 80, 30, 30);
2400 expected = gfx::Rect(80, 80, 20, 20);
2401 actual = LayerTreeHostCommon::CalculateVisibleRect(
2402 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2403 EXPECT_EQ(expected, actual);
2406 TEST_F(LayerTreeHostCommonTest, VisibleRectForTranslations) {
2407 // Test the calculateVisibleRect() function works correctly for scaling
2408 // transforms.
2410 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2411 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
2412 gfx::Transform layer_to_surface_transform;
2414 // Case 1: Layer is contained within the surface.
2415 layer_to_surface_transform.MakeIdentity();
2416 layer_to_surface_transform.Translate(10.0, 10.0);
2417 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
2418 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2419 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2420 EXPECT_EQ(expected, actual);
2422 // Case 2: Layer is outside the surface rect.
2423 layer_to_surface_transform.MakeIdentity();
2424 layer_to_surface_transform.Translate(120.0, 120.0);
2425 actual = LayerTreeHostCommon::CalculateVisibleRect(
2426 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2427 EXPECT_TRUE(actual.IsEmpty());
2429 // Case 3: Layer is partially overlapping the surface rect.
2430 layer_to_surface_transform.MakeIdentity();
2431 layer_to_surface_transform.Translate(80.0, 80.0);
2432 expected = gfx::Rect(0, 0, 20, 20);
2433 actual = LayerTreeHostCommon::CalculateVisibleRect(
2434 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2435 EXPECT_EQ(expected, actual);
2438 TEST_F(LayerTreeHostCommonTest, VisibleRectFor2DRotations) {
2439 // Test the calculateVisibleRect() function works correctly for rotations
2440 // about z-axis (i.e. 2D rotations). Remember that calculateVisibleRect()
2441 // should return the g in the layer's space.
2443 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2444 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
2445 gfx::Transform layer_to_surface_transform;
2447 // Case 1: Layer is contained within the surface.
2448 layer_to_surface_transform.MakeIdentity();
2449 layer_to_surface_transform.Translate(50.0, 50.0);
2450 layer_to_surface_transform.Rotate(45.0);
2451 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
2452 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2453 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2454 EXPECT_EQ(expected, actual);
2456 // Case 2: Layer is outside the surface rect.
2457 layer_to_surface_transform.MakeIdentity();
2458 layer_to_surface_transform.Translate(-50.0, 0.0);
2459 layer_to_surface_transform.Rotate(45.0);
2460 actual = LayerTreeHostCommon::CalculateVisibleRect(
2461 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2462 EXPECT_TRUE(actual.IsEmpty());
2464 // Case 3: The layer is rotated about its top-left corner. In surface space,
2465 // the layer is oriented diagonally, with the left half outside of the render
2466 // surface. In this case, the g should still be the entire layer
2467 // (remember the g is computed in layer space); both the top-left
2468 // and bottom-right corners of the layer are still visible.
2469 layer_to_surface_transform.MakeIdentity();
2470 layer_to_surface_transform.Rotate(45.0);
2471 expected = gfx::Rect(0, 0, 30, 30);
2472 actual = LayerTreeHostCommon::CalculateVisibleRect(
2473 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2474 EXPECT_EQ(expected, actual);
2476 // Case 4: The layer is rotated about its top-left corner, and translated
2477 // upwards. In surface space, the layer is oriented diagonally, with only the
2478 // top corner of the surface overlapping the layer. In layer space, the render
2479 // surface overlaps the right side of the layer. The g should be
2480 // the layer's right half.
2481 layer_to_surface_transform.MakeIdentity();
2482 layer_to_surface_transform.Translate(0.0, -sqrt(2.0) * 15.0);
2483 layer_to_surface_transform.Rotate(45.0);
2484 expected = gfx::Rect(15, 0, 15, 30); // Right half of layer bounds.
2485 actual = LayerTreeHostCommon::CalculateVisibleRect(
2486 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2487 EXPECT_EQ(expected, actual);
2490 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dOrthographicTransform) {
2491 // Test that the calculateVisibleRect() function works correctly for 3d
2492 // transforms.
2494 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2495 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
2496 gfx::Transform layer_to_surface_transform;
2498 // Case 1: Orthographic projection of a layer rotated about y-axis by 45
2499 // degrees, should be fully contained in the render surface.
2500 layer_to_surface_transform.MakeIdentity();
2501 layer_to_surface_transform.RotateAboutYAxis(45.0);
2502 gfx::Rect expected = gfx::Rect(0, 0, 100, 100);
2503 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2504 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2505 EXPECT_EQ(expected, actual);
2507 // Case 2: Orthographic projection of a layer rotated about y-axis by 45
2508 // degrees, but shifted to the side so only the right-half the layer would be
2509 // visible on the surface.
2510 // 100 is the un-rotated layer width; divided by sqrt(2) is the rotated width.
2511 SkMScalar half_width_of_rotated_layer =
2512 SkDoubleToMScalar((100.0 / sqrt(2.0)) * 0.5);
2513 layer_to_surface_transform.MakeIdentity();
2514 layer_to_surface_transform.Translate(-half_width_of_rotated_layer, 0.0);
2515 layer_to_surface_transform.RotateAboutYAxis(45.0); // Rotates about the left
2516 // edge of the layer.
2517 expected = gfx::Rect(50, 0, 50, 100); // Tight half of the layer.
2518 actual = LayerTreeHostCommon::CalculateVisibleRect(
2519 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2520 EXPECT_EQ(expected, actual);
2523 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveTransform) {
2524 // Test the calculateVisibleRect() function works correctly when the layer has
2525 // a perspective projection onto the target surface.
2527 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2528 gfx::Rect layer_content_rect = gfx::Rect(-50, -50, 200, 200);
2529 gfx::Transform layer_to_surface_transform;
2531 // Case 1: Even though the layer is twice as large as the surface, due to
2532 // perspective foreshortening, the layer will fit fully in the surface when
2533 // its translated more than the perspective amount.
2534 layer_to_surface_transform.MakeIdentity();
2536 // The following sequence of transforms applies the perspective about the
2537 // center of the surface.
2538 layer_to_surface_transform.Translate(50.0, 50.0);
2539 layer_to_surface_transform.ApplyPerspectiveDepth(9.0);
2540 layer_to_surface_transform.Translate(-50.0, -50.0);
2542 // This translate places the layer in front of the surface's projection plane.
2543 layer_to_surface_transform.Translate3d(0.0, 0.0, -27.0);
2545 gfx::Rect expected = gfx::Rect(-50, -50, 200, 200);
2546 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2547 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2548 EXPECT_EQ(expected, actual);
2550 // Case 2: same projection as before, except that the layer is also translated
2551 // to the side, so that only the right half of the layer should be visible.
2553 // Explanation of expected result: The perspective ratio is (z distance
2554 // between layer and camera origin) / (z distance between projection plane and
2555 // camera origin) == ((-27 - 9) / 9) Then, by similar triangles, if we want to
2556 // move a layer by translating -50 units in projected surface units (so that
2557 // only half of it is visible), then we would need to translate by (-36 / 9) *
2558 // -50 == -200 in the layer's units.
2559 layer_to_surface_transform.Translate3d(-200.0, 0.0, 0.0);
2560 expected = gfx::Rect(gfx::Point(50, -50),
2561 gfx::Size(100, 200)); // The right half of the layer's
2562 // bounding rect.
2563 actual = LayerTreeHostCommon::CalculateVisibleRect(
2564 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2565 EXPECT_EQ(expected, actual);
2568 TEST_F(LayerTreeHostCommonTest,
2569 VisibleRectFor3dOrthographicIsNotClippedBehindSurface) {
2570 // There is currently no explicit concept of an orthographic projection plane
2571 // in our code (nor in the CSS spec to my knowledge). Therefore, layers that
2572 // are technically behind the surface in an orthographic world should not be
2573 // clipped when they are flattened to the surface.
2575 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2576 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
2577 gfx::Transform layer_to_surface_transform;
2579 // This sequence of transforms effectively rotates the layer about the y-axis
2580 // at the center of the layer.
2581 layer_to_surface_transform.MakeIdentity();
2582 layer_to_surface_transform.Translate(50.0, 0.0);
2583 layer_to_surface_transform.RotateAboutYAxis(45.0);
2584 layer_to_surface_transform.Translate(-50.0, 0.0);
2586 gfx::Rect expected = gfx::Rect(0, 0, 100, 100);
2587 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2588 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2589 EXPECT_EQ(expected, actual);
2592 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveWhenClippedByW) {
2593 // Test the calculateVisibleRect() function works correctly when projecting a
2594 // surface onto a layer, but the layer is partially behind the camera (not
2595 // just behind the projection plane). In this case, the cartesian coordinates
2596 // may seem to be valid, but actually they are not. The visible rect needs to
2597 // be properly clipped by the w = 0 plane in homogeneous coordinates before
2598 // converting to cartesian coordinates.
2600 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100);
2601 gfx::Rect layer_content_rect = gfx::Rect(-10, -1, 20, 2);
2602 gfx::Transform layer_to_surface_transform;
2604 // The layer is positioned so that the right half of the layer should be in
2605 // front of the camera, while the other half is behind the surface's
2606 // projection plane. The following sequence of transforms applies the
2607 // perspective and rotation about the center of the layer.
2608 layer_to_surface_transform.MakeIdentity();
2609 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2610 layer_to_surface_transform.Translate3d(-2.0, 0.0, 1.0);
2611 layer_to_surface_transform.RotateAboutYAxis(45.0);
2613 // Sanity check that this transform does indeed cause w < 0 when applying the
2614 // transform, otherwise this code is not testing the intended scenario.
2615 bool clipped;
2616 MathUtil::MapQuad(layer_to_surface_transform,
2617 gfx::QuadF(gfx::RectF(layer_content_rect)),
2618 &clipped);
2619 ASSERT_TRUE(clipped);
2621 int expected_x_position = 0;
2622 int expected_width = 10;
2623 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2624 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2625 EXPECT_EQ(expected_x_position, actual.x());
2626 EXPECT_EQ(expected_width, actual.width());
2629 TEST_F(LayerTreeHostCommonTest, VisibleRectForPerspectiveUnprojection) {
2630 // To determine visible rect in layer space, there needs to be an
2631 // un-projection from surface space to layer space. When the original
2632 // transform was a perspective projection that was clipped, it returns a rect
2633 // that encloses the clipped bounds. Un-projecting this new rect may require
2634 // clipping again.
2636 // This sequence of transforms causes one corner of the layer to protrude
2637 // across the w = 0 plane, and should be clipped.
2638 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100);
2639 gfx::Rect layer_content_rect = gfx::Rect(-10, -10, 20, 20);
2640 gfx::Transform layer_to_surface_transform;
2641 layer_to_surface_transform.MakeIdentity();
2642 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2643 layer_to_surface_transform.Translate3d(0.0, 0.0, -5.0);
2644 layer_to_surface_transform.RotateAboutYAxis(45.0);
2645 layer_to_surface_transform.RotateAboutXAxis(80.0);
2647 // Sanity check that un-projection does indeed cause w < 0, otherwise this
2648 // code is not testing the intended scenario.
2649 bool clipped;
2650 gfx::RectF clipped_rect =
2651 MathUtil::MapClippedRect(layer_to_surface_transform, layer_content_rect);
2652 MathUtil::ProjectQuad(
2653 Inverse(layer_to_surface_transform), gfx::QuadF(clipped_rect), &clipped);
2654 ASSERT_TRUE(clipped);
2656 // Only the corner of the layer is not visible on the surface because of being
2657 // clipped. But, the net result of rounding visible region to an axis-aligned
2658 // rect is that the entire layer should still be considered visible.
2659 gfx::Rect expected = gfx::Rect(-10, -10, 20, 20);
2660 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2661 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2662 EXPECT_EQ(expected, actual);
2665 TEST_F(LayerTreeHostCommonTest, DrawableAndVisibleContentRectsForSimpleLayers) {
2666 scoped_refptr<Layer> root = Layer::Create();
2667 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2668 make_scoped_refptr(new LayerWithForcedDrawsContent());
2669 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2670 make_scoped_refptr(new LayerWithForcedDrawsContent());
2671 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2672 make_scoped_refptr(new LayerWithForcedDrawsContent());
2673 root->AddChild(child1);
2674 root->AddChild(child2);
2675 root->AddChild(child3);
2677 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2678 host->SetRootLayer(root);
2680 gfx::Transform identity_matrix;
2681 SetLayerPropertiesForTesting(root.get(),
2682 identity_matrix,
2683 gfx::Point3F(),
2684 gfx::PointF(),
2685 gfx::Size(100, 100),
2686 true,
2687 false);
2688 SetLayerPropertiesForTesting(child1.get(),
2689 identity_matrix,
2690 gfx::Point3F(),
2691 gfx::PointF(),
2692 gfx::Size(50, 50),
2693 true,
2694 false);
2695 SetLayerPropertiesForTesting(child2.get(),
2696 identity_matrix,
2697 gfx::Point3F(),
2698 gfx::PointF(75.f, 75.f),
2699 gfx::Size(50, 50),
2700 true,
2701 false);
2702 SetLayerPropertiesForTesting(child3.get(),
2703 identity_matrix,
2704 gfx::Point3F(),
2705 gfx::PointF(125.f, 125.f),
2706 gfx::Size(50, 50),
2707 true,
2708 false);
2710 ExecuteCalculateDrawProperties(root.get());
2712 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2713 root->render_surface()->DrawableContentRect());
2714 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2716 // Layers that do not draw content should have empty visible_content_rects.
2717 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2719 // layer visible_content_rects are clipped by their target surface.
2720 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
2721 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_content_rect());
2722 EXPECT_TRUE(child3->visible_content_rect().IsEmpty());
2724 // layer drawable_content_rects are not clipped.
2725 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->drawable_content_rect());
2726 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
2727 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
2730 TEST_F(LayerTreeHostCommonTest,
2731 DrawableAndVisibleContentRectsForLayersClippedByLayer) {
2732 scoped_refptr<Layer> root = Layer::Create();
2733 scoped_refptr<Layer> child = Layer::Create();
2734 scoped_refptr<LayerWithForcedDrawsContent> grand_child1 =
2735 make_scoped_refptr(new LayerWithForcedDrawsContent());
2736 scoped_refptr<LayerWithForcedDrawsContent> grand_child2 =
2737 make_scoped_refptr(new LayerWithForcedDrawsContent());
2738 scoped_refptr<LayerWithForcedDrawsContent> grand_child3 =
2739 make_scoped_refptr(new LayerWithForcedDrawsContent());
2740 root->AddChild(child);
2741 child->AddChild(grand_child1);
2742 child->AddChild(grand_child2);
2743 child->AddChild(grand_child3);
2745 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2746 host->SetRootLayer(root);
2748 gfx::Transform identity_matrix;
2749 SetLayerPropertiesForTesting(root.get(),
2750 identity_matrix,
2751 gfx::Point3F(),
2752 gfx::PointF(),
2753 gfx::Size(100, 100),
2754 true,
2755 false);
2756 SetLayerPropertiesForTesting(child.get(),
2757 identity_matrix,
2758 gfx::Point3F(),
2759 gfx::PointF(),
2760 gfx::Size(100, 100),
2761 true,
2762 false);
2763 SetLayerPropertiesForTesting(grand_child1.get(),
2764 identity_matrix,
2765 gfx::Point3F(),
2766 gfx::PointF(5.f, 5.f),
2767 gfx::Size(50, 50),
2768 true,
2769 false);
2770 SetLayerPropertiesForTesting(grand_child2.get(),
2771 identity_matrix,
2772 gfx::Point3F(),
2773 gfx::PointF(75.f, 75.f),
2774 gfx::Size(50, 50),
2775 true,
2776 false);
2777 SetLayerPropertiesForTesting(grand_child3.get(),
2778 identity_matrix,
2779 gfx::Point3F(),
2780 gfx::PointF(125.f, 125.f),
2781 gfx::Size(50, 50),
2782 true,
2783 false);
2785 child->SetMasksToBounds(true);
2786 ExecuteCalculateDrawProperties(root.get());
2788 ASSERT_FALSE(child->render_surface());
2790 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2791 root->render_surface()->DrawableContentRect());
2792 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2794 // Layers that do not draw content should have empty visible content rects.
2795 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2796 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), child->visible_content_rect());
2798 // All grandchild visible content rects should be clipped by child.
2799 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), grand_child1->visible_content_rect());
2800 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), grand_child2->visible_content_rect());
2801 EXPECT_TRUE(grand_child3->visible_content_rect().IsEmpty());
2803 // All grandchild DrawableContentRects should also be clipped by child.
2804 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), grand_child1->drawable_content_rect());
2805 EXPECT_EQ(gfx::Rect(75, 75, 25, 25), grand_child2->drawable_content_rect());
2806 EXPECT_TRUE(grand_child3->drawable_content_rect().IsEmpty());
2809 TEST_F(LayerTreeHostCommonTest, VisibleContentRectWithClippingAndScaling) {
2810 scoped_refptr<Layer> root = Layer::Create();
2811 scoped_refptr<Layer> child = Layer::Create();
2812 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
2813 make_scoped_refptr(new LayerWithForcedDrawsContent());
2814 root->AddChild(child);
2815 child->AddChild(grand_child);
2817 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2818 host->SetRootLayer(root);
2820 gfx::Transform identity_matrix;
2821 gfx::Transform child_scale_matrix;
2822 child_scale_matrix.Scale(0.25f, 0.25f);
2823 gfx::Transform grand_child_scale_matrix;
2824 grand_child_scale_matrix.Scale(0.246f, 0.246f);
2825 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
2826 gfx::PointF(), gfx::Size(100, 100), true, false);
2827 SetLayerPropertiesForTesting(child.get(), child_scale_matrix, gfx::Point3F(),
2828 gfx::PointF(), gfx::Size(10, 10), true, false);
2829 SetLayerPropertiesForTesting(grand_child.get(), grand_child_scale_matrix,
2830 gfx::Point3F(), gfx::PointF(),
2831 gfx::Size(100, 100), true, false);
2833 child->SetMasksToBounds(true);
2834 ExecuteCalculateDrawProperties(root.get());
2836 // The visible rect is expanded to integer coordinates in target space before
2837 // being projected back to layer space, where it is once again expanded to
2838 // integer coordinates.
2839 EXPECT_EQ(gfx::Rect(49, 49), grand_child->visible_rect_from_property_trees());
2842 TEST_F(LayerTreeHostCommonTest,
2843 DrawableAndVisibleContentRectsForLayersInUnclippedRenderSurface) {
2844 scoped_refptr<Layer> root = Layer::Create();
2845 scoped_refptr<Layer> render_surface1 = Layer::Create();
2846 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2847 make_scoped_refptr(new LayerWithForcedDrawsContent());
2848 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2849 make_scoped_refptr(new LayerWithForcedDrawsContent());
2850 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2851 make_scoped_refptr(new LayerWithForcedDrawsContent());
2852 root->AddChild(render_surface1);
2853 render_surface1->AddChild(child1);
2854 render_surface1->AddChild(child2);
2855 render_surface1->AddChild(child3);
2857 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2858 host->SetRootLayer(root);
2860 gfx::Transform identity_matrix;
2861 SetLayerPropertiesForTesting(root.get(),
2862 identity_matrix,
2863 gfx::Point3F(),
2864 gfx::PointF(),
2865 gfx::Size(100, 100),
2866 true,
2867 false);
2868 SetLayerPropertiesForTesting(render_surface1.get(),
2869 identity_matrix,
2870 gfx::Point3F(),
2871 gfx::PointF(),
2872 gfx::Size(3, 4),
2873 true,
2874 false);
2875 SetLayerPropertiesForTesting(child1.get(),
2876 identity_matrix,
2877 gfx::Point3F(),
2878 gfx::PointF(5.f, 5.f),
2879 gfx::Size(50, 50),
2880 true,
2881 false);
2882 SetLayerPropertiesForTesting(child2.get(),
2883 identity_matrix,
2884 gfx::Point3F(),
2885 gfx::PointF(75.f, 75.f),
2886 gfx::Size(50, 50),
2887 true,
2888 false);
2889 SetLayerPropertiesForTesting(child3.get(),
2890 identity_matrix,
2891 gfx::Point3F(),
2892 gfx::PointF(125.f, 125.f),
2893 gfx::Size(50, 50),
2894 true,
2895 false);
2897 render_surface1->SetForceRenderSurface(true);
2898 ExecuteCalculateDrawProperties(root.get());
2900 ASSERT_TRUE(render_surface1->render_surface());
2902 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2903 root->render_surface()->DrawableContentRect());
2904 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2906 // Layers that do not draw content should have empty visible content rects.
2907 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2908 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
2910 // An unclipped surface grows its DrawableContentRect to include all drawable
2911 // regions of the subtree.
2912 EXPECT_EQ(gfx::Rect(5, 5, 170, 170),
2913 render_surface1->render_surface()->DrawableContentRect());
2915 // All layers that draw content into the unclipped surface are also unclipped.
2916 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
2917 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
2918 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
2920 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
2921 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
2922 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
2925 TEST_F(LayerTreeHostCommonTest,
2926 VisibleContentRectsForClippedSurfaceWithEmptyClip) {
2927 scoped_refptr<Layer> root = Layer::Create();
2928 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2929 make_scoped_refptr(new LayerWithForcedDrawsContent());
2930 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2931 make_scoped_refptr(new LayerWithForcedDrawsContent());
2932 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2933 make_scoped_refptr(new LayerWithForcedDrawsContent());
2934 root->AddChild(child1);
2935 root->AddChild(child2);
2936 root->AddChild(child3);
2938 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2939 host->SetRootLayer(root);
2941 gfx::Transform identity_matrix;
2942 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
2943 gfx::PointF(), gfx::Size(100, 100), true, false);
2944 SetLayerPropertiesForTesting(child1.get(), identity_matrix, gfx::Point3F(),
2945 gfx::PointF(5.f, 5.f), gfx::Size(50, 50), true,
2946 false);
2947 SetLayerPropertiesForTesting(child2.get(), identity_matrix, gfx::Point3F(),
2948 gfx::PointF(75.f, 75.f), gfx::Size(50, 50), true,
2949 false);
2950 SetLayerPropertiesForTesting(child3.get(), identity_matrix, gfx::Point3F(),
2951 gfx::PointF(125.f, 125.f), gfx::Size(50, 50),
2952 true, false);
2954 RenderSurfaceLayerList render_surface_layer_list;
2955 // Now set the root render surface an empty clip.
2956 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2957 root.get(), gfx::Size(), &render_surface_layer_list);
2959 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2960 ASSERT_TRUE(root->render_surface());
2961 EXPECT_FALSE(root->is_clipped());
2963 gfx::Rect empty;
2964 EXPECT_EQ(empty, root->render_surface()->clip_rect());
2965 EXPECT_TRUE(root->render_surface()->is_clipped());
2967 // Visible content rect calculation will check if the target surface is
2968 // clipped or not. An empty clip rect does not indicate the render surface
2969 // is unclipped.
2970 EXPECT_EQ(empty, child1->visible_content_rect());
2971 EXPECT_EQ(empty, child2->visible_content_rect());
2972 EXPECT_EQ(empty, child3->visible_content_rect());
2975 TEST_F(LayerTreeHostCommonTest,
2976 DrawableAndVisibleContentRectsForLayersWithUninvertibleTransform) {
2977 scoped_refptr<Layer> root = Layer::Create();
2978 scoped_refptr<LayerWithForcedDrawsContent> child =
2979 make_scoped_refptr(new LayerWithForcedDrawsContent());
2980 root->AddChild(child);
2982 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2983 host->SetRootLayer(root);
2985 // Case 1: a truly degenerate matrix
2986 gfx::Transform identity_matrix;
2987 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
2988 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
2990 SetLayerPropertiesForTesting(root.get(),
2991 identity_matrix,
2992 gfx::Point3F(),
2993 gfx::PointF(),
2994 gfx::Size(100, 100),
2995 true,
2996 false);
2997 SetLayerPropertiesForTesting(child.get(),
2998 uninvertible_matrix,
2999 gfx::Point3F(),
3000 gfx::PointF(5.f, 5.f),
3001 gfx::Size(50, 50),
3002 true,
3003 false);
3005 ExecuteCalculateDrawProperties(root.get());
3007 EXPECT_TRUE(child->visible_content_rect().IsEmpty());
3008 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3010 // Case 2: a matrix with flattened z, uninvertible and not visible according
3011 // to the CSS spec.
3012 uninvertible_matrix.MakeIdentity();
3013 uninvertible_matrix.matrix().set(2, 2, 0.0);
3014 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3016 SetLayerPropertiesForTesting(child.get(),
3017 uninvertible_matrix,
3018 gfx::Point3F(),
3019 gfx::PointF(5.f, 5.f),
3020 gfx::Size(50, 50),
3021 true,
3022 false);
3024 ExecuteCalculateDrawProperties(root.get());
3026 EXPECT_TRUE(child->visible_content_rect().IsEmpty());
3027 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3029 // Case 3: a matrix with flattened z, also uninvertible and not visible.
3030 uninvertible_matrix.MakeIdentity();
3031 uninvertible_matrix.Translate(500.0, 0.0);
3032 uninvertible_matrix.matrix().set(2, 2, 0.0);
3033 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3035 SetLayerPropertiesForTesting(child.get(),
3036 uninvertible_matrix,
3037 gfx::Point3F(),
3038 gfx::PointF(5.f, 5.f),
3039 gfx::Size(50, 50),
3040 true,
3041 false);
3043 ExecuteCalculateDrawProperties(root.get());
3045 EXPECT_TRUE(child->visible_content_rect().IsEmpty());
3046 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3049 TEST_F(LayerTreeHostCommonTest,
3050 SingularTransformDoesNotPreventClearingDrawProperties) {
3051 scoped_refptr<Layer> root = Layer::Create();
3052 scoped_refptr<LayerWithForcedDrawsContent> child =
3053 make_scoped_refptr(new LayerWithForcedDrawsContent());
3054 root->AddChild(child);
3056 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3057 host->SetRootLayer(root);
3059 gfx::Transform identity_matrix;
3060 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
3061 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3063 SetLayerPropertiesForTesting(root.get(),
3064 uninvertible_matrix,
3065 gfx::Point3F(),
3066 gfx::PointF(),
3067 gfx::Size(100, 100),
3068 true,
3069 false);
3070 SetLayerPropertiesForTesting(child.get(),
3071 identity_matrix,
3072 gfx::Point3F(),
3073 gfx::PointF(5.f, 5.f),
3074 gfx::Size(50, 50),
3075 true,
3076 false);
3078 child->draw_properties().sorted_for_recursion = true;
3080 TransformOperations start_transform_operations;
3081 start_transform_operations.AppendScale(1.f, 0.f, 0.f);
3083 TransformOperations end_transform_operations;
3084 end_transform_operations.AppendScale(1.f, 1.f, 0.f);
3086 AddAnimatedTransformToLayer(
3087 root.get(), 10.0, start_transform_operations, end_transform_operations);
3089 EXPECT_TRUE(root->TransformIsAnimating());
3091 ExecuteCalculateDrawProperties(root.get());
3093 EXPECT_FALSE(child->draw_properties().sorted_for_recursion);
3096 TEST_F(LayerTreeHostCommonTest,
3097 SingularNonAnimatingTransformDoesNotPreventClearingDrawProperties) {
3098 scoped_refptr<Layer> root = Layer::Create();
3100 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3101 host->SetRootLayer(root);
3103 gfx::Transform identity_matrix;
3104 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
3105 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3107 SetLayerPropertiesForTesting(root.get(),
3108 uninvertible_matrix,
3109 gfx::Point3F(),
3110 gfx::PointF(),
3111 gfx::Size(100, 100),
3112 true,
3113 false);
3115 root->draw_properties().sorted_for_recursion = true;
3117 EXPECT_FALSE(root->TransformIsAnimating());
3119 ExecuteCalculateDrawProperties(root.get());
3121 EXPECT_FALSE(root->draw_properties().sorted_for_recursion);
3124 TEST_F(LayerTreeHostCommonTest,
3125 DrawableAndVisibleContentRectsForLayersInClippedRenderSurface) {
3126 scoped_refptr<Layer> root = Layer::Create();
3127 scoped_refptr<Layer> render_surface1 = Layer::Create();
3128 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3129 make_scoped_refptr(new LayerWithForcedDrawsContent());
3130 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3131 make_scoped_refptr(new LayerWithForcedDrawsContent());
3132 scoped_refptr<LayerWithForcedDrawsContent> child3 =
3133 make_scoped_refptr(new LayerWithForcedDrawsContent());
3134 root->AddChild(render_surface1);
3135 render_surface1->AddChild(child1);
3136 render_surface1->AddChild(child2);
3137 render_surface1->AddChild(child3);
3139 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3140 host->SetRootLayer(root);
3142 gfx::Transform identity_matrix;
3143 SetLayerPropertiesForTesting(root.get(),
3144 identity_matrix,
3145 gfx::Point3F(),
3146 gfx::PointF(),
3147 gfx::Size(100, 100),
3148 true,
3149 false);
3150 SetLayerPropertiesForTesting(render_surface1.get(),
3151 identity_matrix,
3152 gfx::Point3F(),
3153 gfx::PointF(),
3154 gfx::Size(3, 4),
3155 true,
3156 false);
3157 SetLayerPropertiesForTesting(child1.get(),
3158 identity_matrix,
3159 gfx::Point3F(),
3160 gfx::PointF(5.f, 5.f),
3161 gfx::Size(50, 50),
3162 true,
3163 false);
3164 SetLayerPropertiesForTesting(child2.get(),
3165 identity_matrix,
3166 gfx::Point3F(),
3167 gfx::PointF(75.f, 75.f),
3168 gfx::Size(50, 50),
3169 true,
3170 false);
3171 SetLayerPropertiesForTesting(child3.get(),
3172 identity_matrix,
3173 gfx::Point3F(),
3174 gfx::PointF(125.f, 125.f),
3175 gfx::Size(50, 50),
3176 true,
3177 false);
3179 root->SetMasksToBounds(true);
3180 render_surface1->SetForceRenderSurface(true);
3181 ExecuteCalculateDrawProperties(root.get());
3183 ASSERT_TRUE(render_surface1->render_surface());
3185 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3186 root->render_surface()->DrawableContentRect());
3187 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3189 // Layers that do not draw content should have empty visible content rects.
3190 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3191 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
3193 // A clipped surface grows its DrawableContentRect to include all drawable
3194 // regions of the subtree, but also gets clamped by the ancestor's clip.
3195 EXPECT_EQ(gfx::Rect(5, 5, 95, 95),
3196 render_surface1->render_surface()->DrawableContentRect());
3198 // All layers that draw content into the surface have their visible content
3199 // rect clipped by the surface clip rect.
3200 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3201 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_content_rect());
3202 EXPECT_TRUE(child3->visible_content_rect().IsEmpty());
3204 // But the DrawableContentRects are unclipped.
3205 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3206 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3207 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3210 TEST_F(LayerTreeHostCommonTest,
3211 DrawableAndVisibleContentRectsForSurfaceHierarchy) {
3212 // Check that clipping does not propagate down surfaces.
3213 scoped_refptr<Layer> root = Layer::Create();
3214 scoped_refptr<Layer> render_surface1 = Layer::Create();
3215 scoped_refptr<Layer> render_surface2 = Layer::Create();
3216 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3217 make_scoped_refptr(new LayerWithForcedDrawsContent());
3218 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3219 make_scoped_refptr(new LayerWithForcedDrawsContent());
3220 scoped_refptr<LayerWithForcedDrawsContent> child3 =
3221 make_scoped_refptr(new LayerWithForcedDrawsContent());
3222 root->AddChild(render_surface1);
3223 render_surface1->AddChild(render_surface2);
3224 render_surface2->AddChild(child1);
3225 render_surface2->AddChild(child2);
3226 render_surface2->AddChild(child3);
3228 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3229 host->SetRootLayer(root);
3231 gfx::Transform identity_matrix;
3232 SetLayerPropertiesForTesting(root.get(),
3233 identity_matrix,
3234 gfx::Point3F(),
3235 gfx::PointF(),
3236 gfx::Size(100, 100),
3237 true,
3238 false);
3239 SetLayerPropertiesForTesting(render_surface1.get(),
3240 identity_matrix,
3241 gfx::Point3F(),
3242 gfx::PointF(),
3243 gfx::Size(3, 4),
3244 true,
3245 false);
3246 SetLayerPropertiesForTesting(render_surface2.get(),
3247 identity_matrix,
3248 gfx::Point3F(),
3249 gfx::PointF(),
3250 gfx::Size(7, 13),
3251 true,
3252 false);
3253 SetLayerPropertiesForTesting(child1.get(),
3254 identity_matrix,
3255 gfx::Point3F(),
3256 gfx::PointF(5.f, 5.f),
3257 gfx::Size(50, 50),
3258 true,
3259 false);
3260 SetLayerPropertiesForTesting(child2.get(),
3261 identity_matrix,
3262 gfx::Point3F(),
3263 gfx::PointF(75.f, 75.f),
3264 gfx::Size(50, 50),
3265 true,
3266 false);
3267 SetLayerPropertiesForTesting(child3.get(),
3268 identity_matrix,
3269 gfx::Point3F(),
3270 gfx::PointF(125.f, 125.f),
3271 gfx::Size(50, 50),
3272 true,
3273 false);
3275 root->SetMasksToBounds(true);
3276 render_surface1->SetForceRenderSurface(true);
3277 render_surface2->SetForceRenderSurface(true);
3278 ExecuteCalculateDrawProperties(root.get());
3280 ASSERT_TRUE(render_surface1->render_surface());
3281 ASSERT_TRUE(render_surface2->render_surface());
3283 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3284 root->render_surface()->DrawableContentRect());
3285 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3287 // Layers that do not draw content should have empty visible content rects.
3288 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3289 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
3290 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface2->visible_content_rect());
3292 // A clipped surface grows its DrawableContentRect to include all drawable
3293 // regions of the subtree, but also gets clamped by the ancestor's clip.
3294 EXPECT_EQ(gfx::Rect(5, 5, 95, 95),
3295 render_surface1->render_surface()->DrawableContentRect());
3297 // render_surface1 lives in the "unclipped universe" of render_surface1, and
3298 // is only implicitly clipped by render_surface1's content rect. So,
3299 // render_surface2 grows to enclose all drawable content of its subtree.
3300 EXPECT_EQ(gfx::Rect(5, 5, 170, 170),
3301 render_surface2->render_surface()->DrawableContentRect());
3303 // All layers that draw content into render_surface2 think they are unclipped.
3304 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3305 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
3306 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
3308 // DrawableContentRects are also unclipped.
3309 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3310 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3311 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3314 TEST_F(LayerTreeHostCommonTest,
3315 DrawableAndVisibleContentRectsWithTransformOnUnclippedSurface) {
3316 // Layers that have non-axis aligned bounds (due to transforms) have an
3317 // expanded, axis-aligned DrawableContentRect and visible content rect.
3319 scoped_refptr<Layer> root = Layer::Create();
3320 scoped_refptr<Layer> render_surface1 = Layer::Create();
3321 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3322 make_scoped_refptr(new LayerWithForcedDrawsContent());
3323 root->AddChild(render_surface1);
3324 render_surface1->AddChild(child1);
3326 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3327 host->SetRootLayer(root);
3329 gfx::Transform identity_matrix;
3330 gfx::Transform child_rotation;
3331 child_rotation.Rotate(45.0);
3332 SetLayerPropertiesForTesting(root.get(),
3333 identity_matrix,
3334 gfx::Point3F(),
3335 gfx::PointF(),
3336 gfx::Size(100, 100),
3337 true,
3338 false);
3339 SetLayerPropertiesForTesting(render_surface1.get(),
3340 identity_matrix,
3341 gfx::Point3F(),
3342 gfx::PointF(),
3343 gfx::Size(3, 4),
3344 true,
3345 false);
3346 SetLayerPropertiesForTesting(child1.get(),
3347 child_rotation,
3348 gfx::Point3F(25, 25, 0.f),
3349 gfx::PointF(25.f, 25.f),
3350 gfx::Size(50, 50),
3351 true,
3352 false);
3354 render_surface1->SetForceRenderSurface(true);
3355 ExecuteCalculateDrawProperties(root.get());
3357 ASSERT_TRUE(render_surface1->render_surface());
3359 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3360 root->render_surface()->DrawableContentRect());
3361 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3363 // Layers that do not draw content should have empty visible content rects.
3364 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3365 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
3367 // The unclipped surface grows its DrawableContentRect to include all drawable
3368 // regions of the subtree.
3369 int diagonal_radius = ceil(sqrt(2.0) * 25.0);
3370 gfx::Rect expected_surface_drawable_content =
3371 gfx::Rect(50 - diagonal_radius,
3372 50 - diagonal_radius,
3373 diagonal_radius * 2,
3374 diagonal_radius * 2);
3375 EXPECT_EQ(expected_surface_drawable_content,
3376 render_surface1->render_surface()->DrawableContentRect());
3378 // All layers that draw content into the unclipped surface are also unclipped.
3379 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3380 EXPECT_EQ(expected_surface_drawable_content, child1->drawable_content_rect());
3383 TEST_F(LayerTreeHostCommonTest,
3384 DrawableAndVisibleContentRectsWithTransformOnClippedSurface) {
3385 // Layers that have non-axis aligned bounds (due to transforms) have an
3386 // expanded, axis-aligned DrawableContentRect and visible content rect.
3388 scoped_refptr<Layer> root = Layer::Create();
3389 scoped_refptr<Layer> render_surface1 = Layer::Create();
3390 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3391 make_scoped_refptr(new LayerWithForcedDrawsContent());
3392 root->AddChild(render_surface1);
3393 render_surface1->AddChild(child1);
3395 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3396 host->SetRootLayer(root);
3398 gfx::Transform identity_matrix;
3399 gfx::Transform child_rotation;
3400 child_rotation.Rotate(45.0);
3401 SetLayerPropertiesForTesting(root.get(),
3402 identity_matrix,
3403 gfx::Point3F(),
3404 gfx::PointF(),
3405 gfx::Size(50, 50),
3406 true,
3407 false);
3408 SetLayerPropertiesForTesting(render_surface1.get(),
3409 identity_matrix,
3410 gfx::Point3F(),
3411 gfx::PointF(),
3412 gfx::Size(3, 4),
3413 true,
3414 false);
3416 SetLayerPropertiesForTesting(child1.get(),
3417 child_rotation,
3418 gfx::Point3F(25, 25, 0.f),
3419 gfx::PointF(25.f, 25.f),
3420 gfx::Size(50, 50),
3421 true,
3422 false);
3424 root->SetMasksToBounds(true);
3425 render_surface1->SetForceRenderSurface(true);
3426 ExecuteCalculateDrawProperties(root.get());
3428 ASSERT_TRUE(render_surface1->render_surface());
3430 // The clipped surface clamps the DrawableContentRect that encloses the
3431 // rotated layer.
3432 int diagonal_radius = ceil(sqrt(2.0) * 25.0);
3433 gfx::Rect unclipped_surface_content = gfx::Rect(50 - diagonal_radius,
3434 50 - diagonal_radius,
3435 diagonal_radius * 2,
3436 diagonal_radius * 2);
3437 gfx::Rect expected_surface_drawable_content =
3438 gfx::IntersectRects(unclipped_surface_content, gfx::Rect(0, 0, 50, 50));
3439 EXPECT_EQ(expected_surface_drawable_content,
3440 render_surface1->render_surface()->DrawableContentRect());
3442 // On the clipped surface, only a quarter of the child1 is visible, but when
3443 // rotating it back to child1's content space, the actual enclosing rect ends
3444 // up covering the full left half of child1.
3446 // Given the floating point math, this number is a little bit fuzzy.
3447 EXPECT_EQ(gfx::Rect(0, 0, 26, 50), child1->visible_content_rect());
3449 // The child's DrawableContentRect is unclipped.
3450 EXPECT_EQ(unclipped_surface_content, child1->drawable_content_rect());
3453 TEST_F(LayerTreeHostCommonTest, DrawableAndVisibleContentRectsInHighDPI) {
3454 MockContentLayerClient client;
3456 scoped_refptr<Layer> root = Layer::Create();
3457 scoped_refptr<FakePictureLayer> render_surface1 =
3458 CreateDrawablePictureLayer(&client);
3459 scoped_refptr<FakePictureLayer> render_surface2 =
3460 CreateDrawablePictureLayer(&client);
3461 scoped_refptr<FakePictureLayer> child1 = CreateDrawablePictureLayer(&client);
3462 scoped_refptr<FakePictureLayer> child2 = CreateDrawablePictureLayer(&client);
3463 scoped_refptr<FakePictureLayer> child3 = CreateDrawablePictureLayer(&client);
3464 root->AddChild(render_surface1);
3465 render_surface1->AddChild(render_surface2);
3466 render_surface2->AddChild(child1);
3467 render_surface2->AddChild(child2);
3468 render_surface2->AddChild(child3);
3470 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3471 host->SetRootLayer(root);
3473 gfx::Transform identity_matrix;
3474 SetLayerPropertiesForTesting(root.get(),
3475 identity_matrix,
3476 gfx::Point3F(),
3477 gfx::PointF(),
3478 gfx::Size(100, 100),
3479 true,
3480 false);
3481 SetLayerPropertiesForTesting(render_surface1.get(),
3482 identity_matrix,
3483 gfx::Point3F(),
3484 gfx::PointF(5.f, 5.f),
3485 gfx::Size(3, 4),
3486 true,
3487 false);
3488 SetLayerPropertiesForTesting(render_surface2.get(),
3489 identity_matrix,
3490 gfx::Point3F(),
3491 gfx::PointF(5.f, 5.f),
3492 gfx::Size(7, 13),
3493 true,
3494 false);
3495 SetLayerPropertiesForTesting(child1.get(),
3496 identity_matrix,
3497 gfx::Point3F(),
3498 gfx::PointF(5.f, 5.f),
3499 gfx::Size(50, 50),
3500 true,
3501 false);
3502 SetLayerPropertiesForTesting(child2.get(),
3503 identity_matrix,
3504 gfx::Point3F(),
3505 gfx::PointF(75.f, 75.f),
3506 gfx::Size(50, 50),
3507 true,
3508 false);
3509 SetLayerPropertiesForTesting(child3.get(),
3510 identity_matrix,
3511 gfx::Point3F(),
3512 gfx::PointF(125.f, 125.f),
3513 gfx::Size(50, 50),
3514 true,
3515 false);
3517 float device_scale_factor = 2.f;
3519 root->SetMasksToBounds(true);
3520 render_surface1->SetForceRenderSurface(true);
3521 render_surface2->SetForceRenderSurface(true);
3522 ExecuteCalculateDrawProperties(root.get(), device_scale_factor);
3524 ASSERT_TRUE(render_surface1->render_surface());
3525 ASSERT_TRUE(render_surface2->render_surface());
3527 // drawable_content_rects for all layers and surfaces are scaled by
3528 // device_scale_factor.
3529 EXPECT_EQ(gfx::Rect(0, 0, 200, 200),
3530 root->render_surface()->DrawableContentRect());
3531 EXPECT_EQ(gfx::Rect(0, 0, 200, 200), root->drawable_content_rect());
3532 EXPECT_EQ(gfx::Rect(10, 10, 190, 190),
3533 render_surface1->render_surface()->DrawableContentRect());
3535 // render_surface2 lives in the "unclipped universe" of render_surface1, and
3536 // is only implicitly clipped by render_surface1.
3537 EXPECT_EQ(gfx::Rect(10, 10, 350, 350),
3538 render_surface2->render_surface()->DrawableContentRect());
3540 EXPECT_EQ(gfx::Rect(10, 10, 100, 100), child1->drawable_content_rect());
3541 EXPECT_EQ(gfx::Rect(150, 150, 100, 100), child2->drawable_content_rect());
3542 EXPECT_EQ(gfx::Rect(250, 250, 100, 100), child3->drawable_content_rect());
3544 // The root layer does not actually draw content of its own.
3545 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3547 // All layer visible content rects are not expressed in content space of each
3548 // layer, so they are not scaled by the device_scale_factor.
3549 EXPECT_EQ(gfx::Rect(0, 0, 3, 4), render_surface1->visible_content_rect());
3550 EXPECT_EQ(gfx::Rect(0, 0, 7, 13), render_surface2->visible_content_rect());
3551 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3552 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
3553 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
3556 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithoutPreserves3d) {
3557 // Verify the behavior of back-face culling when there are no preserve-3d
3558 // layers. Note that 3d transforms still apply in this case, but they are
3559 // "flattened" to each parent layer according to current W3C spec.
3561 const gfx::Transform identity_matrix;
3562 scoped_refptr<Layer> parent = Layer::Create();
3563 scoped_refptr<LayerWithForcedDrawsContent> front_facing_child =
3564 make_scoped_refptr(new LayerWithForcedDrawsContent());
3565 scoped_refptr<LayerWithForcedDrawsContent> back_facing_child =
3566 make_scoped_refptr(new LayerWithForcedDrawsContent());
3567 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3568 make_scoped_refptr(new LayerWithForcedDrawsContent());
3569 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3570 make_scoped_refptr(new LayerWithForcedDrawsContent());
3571 scoped_refptr<LayerWithForcedDrawsContent>
3572 front_facing_child_of_front_facing_surface =
3573 make_scoped_refptr(new LayerWithForcedDrawsContent());
3574 scoped_refptr<LayerWithForcedDrawsContent>
3575 back_facing_child_of_front_facing_surface =
3576 make_scoped_refptr(new LayerWithForcedDrawsContent());
3577 scoped_refptr<LayerWithForcedDrawsContent>
3578 front_facing_child_of_back_facing_surface =
3579 make_scoped_refptr(new LayerWithForcedDrawsContent());
3580 scoped_refptr<LayerWithForcedDrawsContent>
3581 back_facing_child_of_back_facing_surface =
3582 make_scoped_refptr(new LayerWithForcedDrawsContent());
3584 parent->AddChild(front_facing_child);
3585 parent->AddChild(back_facing_child);
3586 parent->AddChild(front_facing_surface);
3587 parent->AddChild(back_facing_surface);
3588 front_facing_surface->AddChild(front_facing_child_of_front_facing_surface);
3589 front_facing_surface->AddChild(back_facing_child_of_front_facing_surface);
3590 back_facing_surface->AddChild(front_facing_child_of_back_facing_surface);
3591 back_facing_surface->AddChild(back_facing_child_of_back_facing_surface);
3593 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3594 host->SetRootLayer(parent);
3596 // Nothing is double-sided
3597 front_facing_child->SetDoubleSided(false);
3598 back_facing_child->SetDoubleSided(false);
3599 front_facing_surface->SetDoubleSided(false);
3600 back_facing_surface->SetDoubleSided(false);
3601 front_facing_child_of_front_facing_surface->SetDoubleSided(false);
3602 back_facing_child_of_front_facing_surface->SetDoubleSided(false);
3603 front_facing_child_of_back_facing_surface->SetDoubleSided(false);
3604 back_facing_child_of_back_facing_surface->SetDoubleSided(false);
3606 gfx::Transform backface_matrix;
3607 backface_matrix.Translate(50.0, 50.0);
3608 backface_matrix.RotateAboutYAxis(180.0);
3609 backface_matrix.Translate(-50.0, -50.0);
3611 // Having a descendant and opacity will force these to have render surfaces.
3612 front_facing_surface->SetOpacity(0.5f);
3613 back_facing_surface->SetOpacity(0.5f);
3615 // Nothing preserves 3d. According to current W3C CSS gfx::Transforms spec,
3616 // these layers should blindly use their own local transforms to determine
3617 // back-face culling.
3618 SetLayerPropertiesForTesting(parent.get(),
3619 identity_matrix,
3620 gfx::Point3F(),
3621 gfx::PointF(),
3622 gfx::Size(100, 100),
3623 true,
3624 false);
3625 SetLayerPropertiesForTesting(front_facing_child.get(),
3626 identity_matrix,
3627 gfx::Point3F(),
3628 gfx::PointF(),
3629 gfx::Size(100, 100),
3630 true,
3631 false);
3632 SetLayerPropertiesForTesting(back_facing_child.get(),
3633 backface_matrix,
3634 gfx::Point3F(),
3635 gfx::PointF(),
3636 gfx::Size(100, 100),
3637 true,
3638 false);
3639 SetLayerPropertiesForTesting(front_facing_surface.get(),
3640 identity_matrix,
3641 gfx::Point3F(),
3642 gfx::PointF(),
3643 gfx::Size(100, 100),
3644 true,
3645 false);
3646 SetLayerPropertiesForTesting(back_facing_surface.get(),
3647 backface_matrix,
3648 gfx::Point3F(),
3649 gfx::PointF(),
3650 gfx::Size(100, 100),
3651 true,
3652 false);
3653 SetLayerPropertiesForTesting(front_facing_child_of_front_facing_surface.get(),
3654 identity_matrix,
3655 gfx::Point3F(),
3656 gfx::PointF(),
3657 gfx::Size(100, 100),
3658 true,
3659 false);
3660 SetLayerPropertiesForTesting(back_facing_child_of_front_facing_surface.get(),
3661 backface_matrix,
3662 gfx::Point3F(),
3663 gfx::PointF(),
3664 gfx::Size(100, 100),
3665 true,
3666 false);
3667 SetLayerPropertiesForTesting(front_facing_child_of_back_facing_surface.get(),
3668 identity_matrix,
3669 gfx::Point3F(),
3670 gfx::PointF(),
3671 gfx::Size(100, 100),
3672 true,
3673 false);
3674 SetLayerPropertiesForTesting(back_facing_child_of_back_facing_surface.get(),
3675 backface_matrix,
3676 gfx::Point3F(),
3677 gfx::PointF(),
3678 gfx::Size(100, 100),
3679 true,
3680 false);
3682 RenderSurfaceLayerList render_surface_layer_list;
3683 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
3684 parent.get(), parent->bounds(), &render_surface_layer_list);
3685 inputs.can_adjust_raster_scales = true;
3686 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
3688 // Verify which render surfaces were created.
3689 EXPECT_FALSE(front_facing_child->render_surface());
3690 EXPECT_FALSE(back_facing_child->render_surface());
3691 EXPECT_TRUE(front_facing_surface->render_surface());
3692 EXPECT_TRUE(back_facing_surface->render_surface());
3693 EXPECT_FALSE(front_facing_child_of_front_facing_surface->render_surface());
3694 EXPECT_FALSE(back_facing_child_of_front_facing_surface->render_surface());
3695 EXPECT_FALSE(front_facing_child_of_back_facing_surface->render_surface());
3696 EXPECT_FALSE(back_facing_child_of_back_facing_surface->render_surface());
3698 // Verify the render_surface_layer_list.
3699 ASSERT_EQ(3u, render_surface_layer_list.size());
3700 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
3701 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list.at(1)->id());
3702 // Even though the back facing surface LAYER gets culled, the other
3703 // descendants should still be added, so the SURFACE should not be culled.
3704 EXPECT_EQ(back_facing_surface->id(), render_surface_layer_list.at(2)->id());
3706 // Verify root surface's layer list.
3707 ASSERT_EQ(
3709 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
3710 EXPECT_EQ(front_facing_child->id(),
3711 render_surface_layer_list.at(0)
3712 ->render_surface()
3713 ->layer_list()
3714 .at(0)
3715 ->id());
3716 EXPECT_EQ(front_facing_surface->id(),
3717 render_surface_layer_list.at(0)
3718 ->render_surface()
3719 ->layer_list()
3720 .at(1)
3721 ->id());
3722 EXPECT_EQ(back_facing_surface->id(),
3723 render_surface_layer_list.at(0)
3724 ->render_surface()
3725 ->layer_list()
3726 .at(2)
3727 ->id());
3729 // Verify front_facing_surface's layer list.
3730 ASSERT_EQ(
3732 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
3733 EXPECT_EQ(front_facing_surface->id(),
3734 render_surface_layer_list.at(1)
3735 ->render_surface()
3736 ->layer_list()
3737 .at(0)
3738 ->id());
3739 EXPECT_EQ(front_facing_child_of_front_facing_surface->id(),
3740 render_surface_layer_list.at(1)
3741 ->render_surface()
3742 ->layer_list()
3743 .at(1)
3744 ->id());
3746 // Verify back_facing_surface's layer list; its own layer should be culled
3747 // from the surface list.
3748 ASSERT_EQ(
3750 render_surface_layer_list.at(2)->render_surface()->layer_list().size());
3751 EXPECT_EQ(front_facing_child_of_back_facing_surface->id(),
3752 render_surface_layer_list.at(2)
3753 ->render_surface()
3754 ->layer_list()
3755 .at(0)
3756 ->id());
3759 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithPreserves3d) {
3760 // Verify the behavior of back-face culling when preserves-3d transform style
3761 // is used.
3763 const gfx::Transform identity_matrix;
3764 scoped_refptr<Layer> parent = Layer::Create();
3765 scoped_refptr<LayerWithForcedDrawsContent> front_facing_child =
3766 make_scoped_refptr(new LayerWithForcedDrawsContent());
3767 scoped_refptr<LayerWithForcedDrawsContent> back_facing_child =
3768 make_scoped_refptr(new LayerWithForcedDrawsContent());
3769 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3770 make_scoped_refptr(new LayerWithForcedDrawsContent());
3771 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3772 make_scoped_refptr(new LayerWithForcedDrawsContent());
3773 scoped_refptr<LayerWithForcedDrawsContent>
3774 front_facing_child_of_front_facing_surface =
3775 make_scoped_refptr(new LayerWithForcedDrawsContent());
3776 scoped_refptr<LayerWithForcedDrawsContent>
3777 back_facing_child_of_front_facing_surface =
3778 make_scoped_refptr(new LayerWithForcedDrawsContent());
3779 scoped_refptr<LayerWithForcedDrawsContent>
3780 front_facing_child_of_back_facing_surface =
3781 make_scoped_refptr(new LayerWithForcedDrawsContent());
3782 scoped_refptr<LayerWithForcedDrawsContent>
3783 back_facing_child_of_back_facing_surface =
3784 make_scoped_refptr(new LayerWithForcedDrawsContent());
3785 scoped_refptr<LayerWithForcedDrawsContent> dummy_replica_layer1 =
3786 make_scoped_refptr(new LayerWithForcedDrawsContent());
3787 scoped_refptr<LayerWithForcedDrawsContent> dummy_replica_layer2 =
3788 make_scoped_refptr(new LayerWithForcedDrawsContent());
3790 parent->AddChild(front_facing_child);
3791 parent->AddChild(back_facing_child);
3792 parent->AddChild(front_facing_surface);
3793 parent->AddChild(back_facing_surface);
3794 front_facing_surface->AddChild(front_facing_child_of_front_facing_surface);
3795 front_facing_surface->AddChild(back_facing_child_of_front_facing_surface);
3796 back_facing_surface->AddChild(front_facing_child_of_back_facing_surface);
3797 back_facing_surface->AddChild(back_facing_child_of_back_facing_surface);
3799 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3800 host->SetRootLayer(parent);
3802 // Nothing is double-sided
3803 front_facing_child->SetDoubleSided(false);
3804 back_facing_child->SetDoubleSided(false);
3805 front_facing_surface->SetDoubleSided(false);
3806 back_facing_surface->SetDoubleSided(false);
3807 front_facing_child_of_front_facing_surface->SetDoubleSided(false);
3808 back_facing_child_of_front_facing_surface->SetDoubleSided(false);
3809 front_facing_child_of_back_facing_surface->SetDoubleSided(false);
3810 back_facing_child_of_back_facing_surface->SetDoubleSided(false);
3812 gfx::Transform backface_matrix;
3813 backface_matrix.Translate(50.0, 50.0);
3814 backface_matrix.RotateAboutYAxis(180.0);
3815 backface_matrix.Translate(-50.0, -50.0);
3817 // Opacity will not force creation of render surfaces in this case because of
3818 // the preserve-3d transform style. Instead, an example of when a surface
3819 // would be created with preserve-3d is when there is a replica layer.
3820 front_facing_surface->SetReplicaLayer(dummy_replica_layer1.get());
3821 back_facing_surface->SetReplicaLayer(dummy_replica_layer2.get());
3823 // Each surface creates its own new 3d rendering context (as defined by W3C
3824 // spec). According to current W3C CSS gfx::Transforms spec, layers in a 3d
3825 // rendering context should use the transform with respect to that context.
3826 // This 3d rendering context occurs when (a) parent's transform style is flat
3827 // and (b) the layer's transform style is preserve-3d.
3828 SetLayerPropertiesForTesting(parent.get(),
3829 identity_matrix,
3830 gfx::Point3F(),
3831 gfx::PointF(),
3832 gfx::Size(100, 100),
3833 true,
3834 false); // parent transform style is flat.
3835 SetLayerPropertiesForTesting(front_facing_child.get(),
3836 identity_matrix,
3837 gfx::Point3F(),
3838 gfx::PointF(),
3839 gfx::Size(100, 100),
3840 true,
3841 false);
3842 SetLayerPropertiesForTesting(back_facing_child.get(),
3843 backface_matrix,
3844 gfx::Point3F(),
3845 gfx::PointF(),
3846 gfx::Size(100, 100),
3847 true,
3848 false);
3849 // surface transform style is preserve-3d.
3850 SetLayerPropertiesForTesting(front_facing_surface.get(),
3851 identity_matrix,
3852 gfx::Point3F(),
3853 gfx::PointF(),
3854 gfx::Size(100, 100),
3855 false,
3856 true);
3857 // surface transform style is preserve-3d.
3858 SetLayerPropertiesForTesting(back_facing_surface.get(),
3859 backface_matrix,
3860 gfx::Point3F(),
3861 gfx::PointF(),
3862 gfx::Size(100, 100),
3863 false,
3864 true);
3865 SetLayerPropertiesForTesting(front_facing_child_of_front_facing_surface.get(),
3866 identity_matrix,
3867 gfx::Point3F(),
3868 gfx::PointF(),
3869 gfx::Size(100, 100),
3870 true,
3871 true);
3872 SetLayerPropertiesForTesting(back_facing_child_of_front_facing_surface.get(),
3873 backface_matrix,
3874 gfx::Point3F(),
3875 gfx::PointF(),
3876 gfx::Size(100, 100),
3877 true,
3878 true);
3879 SetLayerPropertiesForTesting(front_facing_child_of_back_facing_surface.get(),
3880 identity_matrix,
3881 gfx::Point3F(),
3882 gfx::PointF(),
3883 gfx::Size(100, 100),
3884 true,
3885 true);
3886 SetLayerPropertiesForTesting(back_facing_child_of_back_facing_surface.get(),
3887 backface_matrix,
3888 gfx::Point3F(),
3889 gfx::PointF(),
3890 gfx::Size(100, 100),
3891 true,
3892 true);
3894 RenderSurfaceLayerList render_surface_layer_list;
3895 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
3896 parent.get(), parent->bounds(), &render_surface_layer_list);
3897 inputs.can_adjust_raster_scales = true;
3898 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
3900 // Verify which render surfaces were created and used.
3901 EXPECT_FALSE(front_facing_child->render_surface());
3902 EXPECT_FALSE(back_facing_child->render_surface());
3903 EXPECT_TRUE(front_facing_surface->render_surface());
3904 EXPECT_NE(back_facing_surface->render_target(), back_facing_surface);
3905 // We expect that a render_surface was created but not used.
3906 EXPECT_TRUE(back_facing_surface->render_surface());
3907 EXPECT_FALSE(front_facing_child_of_front_facing_surface->render_surface());
3908 EXPECT_FALSE(back_facing_child_of_front_facing_surface->render_surface());
3909 EXPECT_FALSE(front_facing_child_of_back_facing_surface->render_surface());
3910 EXPECT_FALSE(back_facing_child_of_back_facing_surface->render_surface());
3912 // Verify the render_surface_layer_list. The back-facing surface should be
3913 // culled.
3914 ASSERT_EQ(2u, render_surface_layer_list.size());
3915 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
3916 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list.at(1)->id());
3918 // Verify root surface's layer list.
3919 ASSERT_EQ(
3921 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
3922 EXPECT_EQ(front_facing_child->id(),
3923 render_surface_layer_list.at(0)
3924 ->render_surface()->layer_list().at(0)->id());
3925 EXPECT_EQ(front_facing_surface->id(),
3926 render_surface_layer_list.at(0)
3927 ->render_surface()->layer_list().at(1)->id());
3929 // Verify front_facing_surface's layer list.
3930 ASSERT_EQ(
3932 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
3933 EXPECT_EQ(front_facing_surface->id(),
3934 render_surface_layer_list.at(1)
3935 ->render_surface()->layer_list().at(0)->id());
3936 EXPECT_EQ(front_facing_child_of_front_facing_surface->id(),
3937 render_surface_layer_list.at(1)
3938 ->render_surface()->layer_list().at(1)->id());
3941 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithAnimatingTransforms) {
3942 // Verify that layers are appropriately culled when their back face is showing
3943 // and they are not double sided, while animations are going on.
3945 // Layers that are animating do not get culled on the main thread, as their
3946 // transforms should be treated as "unknown" so we can not be sure that their
3947 // back face is really showing.
3948 const gfx::Transform identity_matrix;
3949 scoped_refptr<Layer> parent = Layer::Create();
3950 scoped_refptr<LayerWithForcedDrawsContent> child =
3951 make_scoped_refptr(new LayerWithForcedDrawsContent());
3952 scoped_refptr<LayerWithForcedDrawsContent> animating_surface =
3953 make_scoped_refptr(new LayerWithForcedDrawsContent());
3954 scoped_refptr<LayerWithForcedDrawsContent> child_of_animating_surface =
3955 make_scoped_refptr(new LayerWithForcedDrawsContent());
3956 scoped_refptr<LayerWithForcedDrawsContent> animating_child =
3957 make_scoped_refptr(new LayerWithForcedDrawsContent());
3958 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3959 make_scoped_refptr(new LayerWithForcedDrawsContent());
3961 parent->AddChild(child);
3962 parent->AddChild(animating_surface);
3963 animating_surface->AddChild(child_of_animating_surface);
3964 parent->AddChild(animating_child);
3965 parent->AddChild(child2);
3967 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3968 host->SetRootLayer(parent);
3970 // Nothing is double-sided
3971 child->SetDoubleSided(false);
3972 child2->SetDoubleSided(false);
3973 animating_surface->SetDoubleSided(false);
3974 child_of_animating_surface->SetDoubleSided(false);
3975 animating_child->SetDoubleSided(false);
3977 gfx::Transform backface_matrix;
3978 backface_matrix.Translate(50.0, 50.0);
3979 backface_matrix.RotateAboutYAxis(180.0);
3980 backface_matrix.Translate(-50.0, -50.0);
3982 // Make our render surface.
3983 animating_surface->SetForceRenderSurface(true);
3985 // Animate the transform on the render surface.
3986 AddAnimatedTransformToController(
3987 animating_surface->layer_animation_controller(), 10.0, 30, 0);
3988 // This is just an animating layer, not a surface.
3989 AddAnimatedTransformToController(
3990 animating_child->layer_animation_controller(), 10.0, 30, 0);
3992 SetLayerPropertiesForTesting(parent.get(),
3993 identity_matrix,
3994 gfx::Point3F(),
3995 gfx::PointF(),
3996 gfx::Size(100, 100),
3997 true,
3998 false);
3999 SetLayerPropertiesForTesting(child.get(),
4000 backface_matrix,
4001 gfx::Point3F(),
4002 gfx::PointF(),
4003 gfx::Size(100, 100),
4004 true,
4005 false);
4006 SetLayerPropertiesForTesting(animating_surface.get(),
4007 backface_matrix,
4008 gfx::Point3F(),
4009 gfx::PointF(),
4010 gfx::Size(100, 100),
4011 true,
4012 false);
4013 SetLayerPropertiesForTesting(child_of_animating_surface.get(),
4014 backface_matrix,
4015 gfx::Point3F(),
4016 gfx::PointF(),
4017 gfx::Size(100, 100),
4018 true,
4019 false);
4020 SetLayerPropertiesForTesting(animating_child.get(),
4021 backface_matrix,
4022 gfx::Point3F(),
4023 gfx::PointF(),
4024 gfx::Size(100, 100),
4025 true,
4026 false);
4027 SetLayerPropertiesForTesting(child2.get(),
4028 identity_matrix,
4029 gfx::Point3F(),
4030 gfx::PointF(),
4031 gfx::Size(100, 100),
4032 true,
4033 false);
4035 RenderSurfaceLayerList render_surface_layer_list;
4036 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4037 parent.get(), parent->bounds(), &render_surface_layer_list);
4038 inputs.can_adjust_raster_scales = true;
4039 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4041 EXPECT_FALSE(child->render_surface());
4042 EXPECT_TRUE(animating_surface->render_surface());
4043 EXPECT_FALSE(child_of_animating_surface->render_surface());
4044 EXPECT_FALSE(animating_child->render_surface());
4045 EXPECT_FALSE(child2->render_surface());
4047 // Verify that the animating_child and child_of_animating_surface were not
4048 // culled, but that child was.
4049 ASSERT_EQ(2u, render_surface_layer_list.size());
4050 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
4051 EXPECT_EQ(animating_surface->id(), render_surface_layer_list.at(1)->id());
4053 // The non-animating child be culled from the layer list for the parent render
4054 // surface.
4055 ASSERT_EQ(
4057 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
4058 EXPECT_EQ(animating_surface->id(),
4059 render_surface_layer_list.at(0)
4060 ->render_surface()->layer_list().at(0)->id());
4061 EXPECT_EQ(animating_child->id(),
4062 render_surface_layer_list.at(0)
4063 ->render_surface()->layer_list().at(1)->id());
4064 EXPECT_EQ(child2->id(),
4065 render_surface_layer_list.at(0)
4066 ->render_surface()->layer_list().at(2)->id());
4068 ASSERT_EQ(
4070 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
4071 EXPECT_EQ(animating_surface->id(),
4072 render_surface_layer_list.at(1)
4073 ->render_surface()->layer_list().at(0)->id());
4074 EXPECT_EQ(child_of_animating_surface->id(),
4075 render_surface_layer_list.at(1)
4076 ->render_surface()->layer_list().at(1)->id());
4078 EXPECT_FALSE(child2->visible_content_rect().IsEmpty());
4080 // The animating layers should have a visible content rect that represents the
4081 // area of the front face that is within the viewport.
4082 EXPECT_EQ(animating_child->visible_content_rect(),
4083 gfx::Rect(animating_child->content_bounds()));
4084 EXPECT_EQ(animating_surface->visible_content_rect(),
4085 gfx::Rect(animating_surface->content_bounds()));
4086 // And layers in the subtree of the animating layer should have valid visible
4087 // content rects also.
4088 EXPECT_EQ(child_of_animating_surface->visible_content_rect(),
4089 gfx::Rect(child_of_animating_surface->content_bounds()));
4092 TEST_F(LayerTreeHostCommonTest,
4093 BackFaceCullingWithPreserves3dForFlatteningSurface) {
4094 // Verify the behavior of back-face culling for a render surface that is
4095 // created when it flattens its subtree, and its parent has preserves-3d.
4097 const gfx::Transform identity_matrix;
4098 scoped_refptr<Layer> parent = Layer::Create();
4099 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
4100 make_scoped_refptr(new LayerWithForcedDrawsContent());
4101 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
4102 make_scoped_refptr(new LayerWithForcedDrawsContent());
4103 scoped_refptr<LayerWithForcedDrawsContent> child1 =
4104 make_scoped_refptr(new LayerWithForcedDrawsContent());
4105 scoped_refptr<LayerWithForcedDrawsContent> child2 =
4106 make_scoped_refptr(new LayerWithForcedDrawsContent());
4108 parent->AddChild(front_facing_surface);
4109 parent->AddChild(back_facing_surface);
4110 front_facing_surface->AddChild(child1);
4111 back_facing_surface->AddChild(child2);
4113 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4114 host->SetRootLayer(parent);
4116 // RenderSurfaces are not double-sided
4117 front_facing_surface->SetDoubleSided(false);
4118 back_facing_surface->SetDoubleSided(false);
4120 gfx::Transform backface_matrix;
4121 backface_matrix.Translate(50.0, 50.0);
4122 backface_matrix.RotateAboutYAxis(180.0);
4123 backface_matrix.Translate(-50.0, -50.0);
4125 SetLayerPropertiesForTesting(parent.get(),
4126 identity_matrix,
4127 gfx::Point3F(),
4128 gfx::PointF(),
4129 gfx::Size(100, 100),
4130 false,
4131 true); // parent transform style is preserve3d.
4132 SetLayerPropertiesForTesting(front_facing_surface.get(),
4133 identity_matrix,
4134 gfx::Point3F(),
4135 gfx::PointF(),
4136 gfx::Size(100, 100),
4137 true,
4138 true); // surface transform style is flat.
4139 SetLayerPropertiesForTesting(back_facing_surface.get(),
4140 backface_matrix,
4141 gfx::Point3F(),
4142 gfx::PointF(),
4143 gfx::Size(100, 100),
4144 true,
4145 true); // surface transform style is flat.
4146 SetLayerPropertiesForTesting(child1.get(),
4147 identity_matrix,
4148 gfx::Point3F(),
4149 gfx::PointF(),
4150 gfx::Size(100, 100),
4151 true,
4152 false);
4153 SetLayerPropertiesForTesting(child2.get(),
4154 identity_matrix,
4155 gfx::Point3F(),
4156 gfx::PointF(),
4157 gfx::Size(100, 100),
4158 true,
4159 false);
4161 front_facing_surface->Set3dSortingContextId(1);
4162 back_facing_surface->Set3dSortingContextId(1);
4164 RenderSurfaceLayerList render_surface_layer_list;
4165 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4166 parent.get(), parent->bounds(), &render_surface_layer_list);
4167 inputs.can_adjust_raster_scales = true;
4168 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4170 // Verify which render surfaces were created and used.
4171 EXPECT_TRUE(front_facing_surface->render_surface());
4173 // We expect the render surface to have been created, but remain unused.
4174 EXPECT_TRUE(back_facing_surface->render_surface());
4175 EXPECT_NE(back_facing_surface->render_target(),
4176 back_facing_surface); // because it should be culled
4177 EXPECT_FALSE(child1->render_surface());
4178 EXPECT_FALSE(child2->render_surface());
4180 // Verify the render_surface_layer_list. The back-facing surface should be
4181 // culled.
4182 ASSERT_EQ(2u, render_surface_layer_list.size());
4183 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
4184 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list.at(1)->id());
4186 // Verify root surface's layer list.
4187 ASSERT_EQ(
4189 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
4190 EXPECT_EQ(front_facing_surface->id(),
4191 render_surface_layer_list.at(0)
4192 ->render_surface()->layer_list().at(0)->id());
4194 // Verify front_facing_surface's layer list.
4195 ASSERT_EQ(
4197 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
4198 EXPECT_EQ(front_facing_surface->id(),
4199 render_surface_layer_list.at(1)
4200 ->render_surface()->layer_list().at(0)->id());
4201 EXPECT_EQ(child1->id(),
4202 render_surface_layer_list.at(1)
4203 ->render_surface()->layer_list().at(1)->id());
4206 class NoScaleContentLayer : public ContentLayer {
4207 public:
4208 static scoped_refptr<NoScaleContentLayer> Create(ContentLayerClient* client) {
4209 return make_scoped_refptr(new NoScaleContentLayer(client));
4212 void CalculateContentsScale(float ideal_contents_scale,
4213 float* contents_scale_x,
4214 float* contents_scale_y,
4215 gfx::Size* content_bounds) override {
4216 // Skip over the ContentLayer to the base Layer class.
4217 Layer::CalculateContentsScale(ideal_contents_scale,
4218 contents_scale_x,
4219 contents_scale_y,
4220 content_bounds);
4223 protected:
4224 explicit NoScaleContentLayer(ContentLayerClient* client)
4225 : ContentLayer(client) {}
4226 ~NoScaleContentLayer() override {}
4229 scoped_refptr<NoScaleContentLayer> CreateNoScaleDrawableContentLayer(
4230 ContentLayerClient* delegate) {
4231 scoped_refptr<NoScaleContentLayer> to_return =
4232 NoScaleContentLayer::Create(delegate);
4233 to_return->SetIsDrawable(true);
4234 return to_return;
4237 TEST_F(LayerTreeHostCommonTest, LayerTransformsInHighDPI) {
4238 // Verify draw and screen space transforms of layers not in a surface.
4239 MockContentLayerClient delegate;
4240 gfx::Transform identity_matrix;
4242 scoped_refptr<FakePictureLayer> parent =
4243 CreateDrawablePictureLayer(&delegate);
4244 SetLayerPropertiesForTesting(parent.get(),
4245 identity_matrix,
4246 gfx::Point3F(),
4247 gfx::PointF(),
4248 gfx::Size(100, 100),
4249 false,
4250 true);
4252 scoped_refptr<FakePictureLayer> child = CreateDrawablePictureLayer(&delegate);
4253 SetLayerPropertiesForTesting(child.get(),
4254 identity_matrix,
4255 gfx::Point3F(),
4256 gfx::PointF(2.f, 2.f),
4257 gfx::Size(10, 10),
4258 false,
4259 true);
4261 scoped_refptr<FakePictureLayer> child_empty =
4262 CreateDrawablePictureLayer(&delegate);
4263 SetLayerPropertiesForTesting(child_empty.get(),
4264 identity_matrix,
4265 gfx::Point3F(),
4266 gfx::PointF(2.f, 2.f),
4267 gfx::Size(),
4268 false,
4269 true);
4271 parent->AddChild(child);
4272 parent->AddChild(child_empty);
4274 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4275 host->SetRootLayer(parent);
4277 float device_scale_factor = 2.5f;
4278 float page_scale_factor = 1.f;
4280 RenderSurfaceLayerList render_surface_layer_list;
4281 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4282 parent.get(), parent->bounds(), &render_surface_layer_list);
4283 inputs.device_scale_factor = device_scale_factor;
4284 inputs.page_scale_factor = page_scale_factor;
4285 inputs.can_adjust_raster_scales = true;
4286 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4288 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4289 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, child);
4290 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, child_empty);
4292 EXPECT_EQ(1u, render_surface_layer_list.size());
4294 // Verify parent transforms
4295 gfx::Transform expected_parent_transform;
4296 expected_parent_transform.Scale(device_scale_factor * page_scale_factor,
4297 device_scale_factor * page_scale_factor);
4298 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4299 parent->screen_space_transform());
4300 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4301 parent->draw_transform());
4303 // Verify results of transformed parent rects
4304 gfx::RectF parent_content_bounds(parent->content_bounds());
4306 gfx::RectF parent_draw_rect =
4307 MathUtil::MapClippedRect(parent->draw_transform(), parent_content_bounds);
4308 gfx::RectF parent_screen_space_rect = MathUtil::MapClippedRect(
4309 parent->screen_space_transform(), parent_content_bounds);
4311 gfx::RectF expected_parent_draw_rect(parent->bounds());
4312 expected_parent_draw_rect.Scale(device_scale_factor);
4313 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_draw_rect);
4314 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_screen_space_rect);
4316 // Verify child and child_empty transforms. They should match.
4317 gfx::Transform expected_child_transform;
4318 expected_child_transform.Scale(device_scale_factor, device_scale_factor);
4319 expected_child_transform.Translate(child->position().x(),
4320 child->position().y());
4321 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4322 child->draw_transform());
4323 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4324 child->screen_space_transform());
4325 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4326 child_empty->draw_transform());
4327 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4328 child_empty->screen_space_transform());
4330 // Verify results of transformed child and child_empty rects. They should
4331 // match.
4332 gfx::RectF child_content_bounds(child->content_bounds());
4334 gfx::RectF child_draw_rect =
4335 MathUtil::MapClippedRect(child->draw_transform(), child_content_bounds);
4336 gfx::RectF child_screen_space_rect = MathUtil::MapClippedRect(
4337 child->screen_space_transform(), child_content_bounds);
4339 gfx::RectF child_empty_draw_rect = MathUtil::MapClippedRect(
4340 child_empty->draw_transform(), child_content_bounds);
4341 gfx::RectF child_empty_screen_space_rect = MathUtil::MapClippedRect(
4342 child_empty->screen_space_transform(), child_content_bounds);
4344 gfx::RectF expected_child_draw_rect(child->position(), child->bounds());
4345 expected_child_draw_rect.Scale(device_scale_factor);
4346 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_draw_rect);
4347 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_screen_space_rect);
4348 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_empty_draw_rect);
4349 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_empty_screen_space_rect);
4352 TEST_F(LayerTreeHostCommonTest, SurfaceLayerTransformsInHighDPI) {
4353 // Verify draw and screen space transforms of layers in a surface.
4354 MockContentLayerClient delegate;
4355 gfx::Transform identity_matrix;
4357 gfx::Transform perspective_matrix;
4358 perspective_matrix.ApplyPerspectiveDepth(2.0);
4360 gfx::Transform scale_small_matrix;
4361 scale_small_matrix.Scale(SK_MScalar1 / 10.f, SK_MScalar1 / 12.f);
4363 scoped_refptr<Layer> root = Layer::Create();
4365 scoped_refptr<FakePictureLayer> parent =
4366 CreateDrawablePictureLayer(&delegate);
4367 SetLayerPropertiesForTesting(parent.get(),
4368 identity_matrix,
4369 gfx::Point3F(),
4370 gfx::PointF(),
4371 gfx::Size(100, 100),
4372 false,
4373 true);
4375 scoped_refptr<FakePictureLayer> perspective_surface =
4376 CreateDrawablePictureLayer(&delegate);
4377 SetLayerPropertiesForTesting(perspective_surface.get(),
4378 perspective_matrix * scale_small_matrix,
4379 gfx::Point3F(),
4380 gfx::PointF(2.f, 2.f),
4381 gfx::Size(10, 10),
4382 false,
4383 true);
4385 scoped_refptr<FakePictureLayer> scale_surface =
4386 CreateDrawablePictureLayer(&delegate);
4387 SetLayerPropertiesForTesting(scale_surface.get(),
4388 scale_small_matrix,
4389 gfx::Point3F(),
4390 gfx::PointF(2.f, 2.f),
4391 gfx::Size(10, 10),
4392 false,
4393 true);
4395 perspective_surface->SetForceRenderSurface(true);
4396 scale_surface->SetForceRenderSurface(true);
4398 parent->AddChild(perspective_surface);
4399 parent->AddChild(scale_surface);
4400 root->AddChild(parent);
4402 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4403 host->SetRootLayer(root);
4405 float device_scale_factor = 2.5f;
4406 float page_scale_factor = 3.f;
4408 RenderSurfaceLayerList render_surface_layer_list;
4409 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4410 root.get(), parent->bounds(), &render_surface_layer_list);
4411 inputs.device_scale_factor = device_scale_factor;
4412 inputs.page_scale_factor = page_scale_factor;
4413 inputs.page_scale_application_layer = root.get();
4414 inputs.can_adjust_raster_scales = true;
4415 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4417 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4418 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor,
4419 perspective_surface);
4420 // Ideal scale is the max 2d scale component of the combined transform up to
4421 // the nearest render target. Here this includes the layer transform as well
4422 // as the device and page scale factors.
4423 gfx::Transform transform = scale_small_matrix;
4424 transform.Scale(device_scale_factor * page_scale_factor,
4425 device_scale_factor * page_scale_factor);
4426 gfx::Vector2dF scales =
4427 MathUtil::ComputeTransform2dScaleComponents(transform, 0.f);
4428 float max_2d_scale = std::max(scales.x(), scales.y());
4429 EXPECT_IDEAL_SCALE_EQ(max_2d_scale, scale_surface);
4431 // The ideal scale will draw 1:1 with its render target space along
4432 // the larger-scale axis.
4433 gfx::Vector2dF target_space_transform_scales =
4434 MathUtil::ComputeTransform2dScaleComponents(
4435 scale_surface->draw_properties().target_space_transform, 0.f);
4436 EXPECT_FLOAT_EQ(max_2d_scale,
4437 std::max(target_space_transform_scales.x(),
4438 target_space_transform_scales.y()));
4440 EXPECT_EQ(3u, render_surface_layer_list.size());
4442 gfx::Transform expected_parent_draw_transform;
4443 expected_parent_draw_transform.Scale(device_scale_factor * page_scale_factor,
4444 device_scale_factor * page_scale_factor);
4445 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_draw_transform,
4446 parent->draw_transform());
4448 // The scale for the perspective surface is not known, so it is rendered 1:1
4449 // with the screen, and then scaled during drawing.
4450 gfx::Transform expected_perspective_surface_draw_transform;
4451 expected_perspective_surface_draw_transform.Translate(
4452 device_scale_factor * page_scale_factor *
4453 perspective_surface->position().x(),
4454 device_scale_factor * page_scale_factor *
4455 perspective_surface->position().y());
4456 expected_perspective_surface_draw_transform.PreconcatTransform(
4457 perspective_matrix);
4458 expected_perspective_surface_draw_transform.PreconcatTransform(
4459 scale_small_matrix);
4460 gfx::Transform expected_perspective_surface_layer_draw_transform;
4461 expected_perspective_surface_layer_draw_transform.Scale(
4462 device_scale_factor * page_scale_factor,
4463 device_scale_factor * page_scale_factor);
4464 EXPECT_TRANSFORMATION_MATRIX_EQ(
4465 expected_perspective_surface_draw_transform,
4466 perspective_surface->render_surface()->draw_transform());
4467 EXPECT_TRANSFORMATION_MATRIX_EQ(
4468 expected_perspective_surface_layer_draw_transform,
4469 perspective_surface->draw_transform());
4472 // TODO(sohanjg): Remove this test when ContentLayer is removed.
4473 TEST_F(LayerTreeHostCommonTest,
4474 LayerTransformsInHighDPIAccurateScaleZeroChildPosition) {
4475 // Verify draw and screen space transforms of layers not in a surface.
4476 MockContentLayerClient delegate;
4477 gfx::Transform identity_matrix;
4479 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
4480 SetLayerPropertiesForTesting(parent.get(),
4481 identity_matrix,
4482 gfx::Point3F(),
4483 gfx::PointF(),
4484 gfx::Size(133, 133),
4485 false,
4486 true);
4488 scoped_refptr<ContentLayer> child = CreateDrawableContentLayer(&delegate);
4489 SetLayerPropertiesForTesting(child.get(),
4490 identity_matrix,
4491 gfx::Point3F(),
4492 gfx::PointF(),
4493 gfx::Size(13, 13),
4494 false,
4495 true);
4497 scoped_refptr<NoScaleContentLayer> child_no_scale =
4498 CreateNoScaleDrawableContentLayer(&delegate);
4499 SetLayerPropertiesForTesting(child_no_scale.get(),
4500 identity_matrix,
4501 gfx::Point3F(),
4502 gfx::PointF(),
4503 gfx::Size(13, 13),
4504 false,
4505 true);
4507 parent->AddChild(child);
4508 parent->AddChild(child_no_scale);
4510 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4511 host->SetRootLayer(parent);
4513 float device_scale_factor = 1.7f;
4514 float page_scale_factor = 1.f;
4516 RenderSurfaceLayerList render_surface_layer_list;
4517 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4518 parent.get(), parent->bounds(), &render_surface_layer_list);
4519 inputs.device_scale_factor = device_scale_factor;
4520 inputs.page_scale_factor = page_scale_factor;
4521 inputs.page_scale_application_layer = parent.get();
4522 inputs.can_adjust_raster_scales = true;
4523 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4525 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4526 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, child);
4527 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4529 EXPECT_EQ(1u, render_surface_layer_list.size());
4531 // Verify parent transforms
4532 gfx::Transform expected_parent_transform;
4533 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4534 parent->screen_space_transform());
4535 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4536 parent->draw_transform());
4538 // Verify results of transformed parent rects
4539 gfx::RectF parent_content_bounds(parent->content_bounds());
4541 gfx::RectF parent_draw_rect =
4542 MathUtil::MapClippedRect(parent->draw_transform(), parent_content_bounds);
4543 gfx::RectF parent_screen_space_rect = MathUtil::MapClippedRect(
4544 parent->screen_space_transform(), parent_content_bounds);
4546 gfx::RectF expected_parent_draw_rect(parent->bounds());
4547 expected_parent_draw_rect.Scale(device_scale_factor);
4548 expected_parent_draw_rect.set_width(ceil(expected_parent_draw_rect.width()));
4549 expected_parent_draw_rect.set_height(
4550 ceil(expected_parent_draw_rect.height()));
4551 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_draw_rect);
4552 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_screen_space_rect);
4554 // Verify child transforms
4555 gfx::Transform expected_child_transform;
4556 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4557 child->draw_transform());
4558 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4559 child->screen_space_transform());
4561 // Verify results of transformed child rects
4562 gfx::RectF child_content_bounds(child->content_bounds());
4564 gfx::RectF child_draw_rect =
4565 MathUtil::MapClippedRect(child->draw_transform(), child_content_bounds);
4566 gfx::RectF child_screen_space_rect = MathUtil::MapClippedRect(
4567 child->screen_space_transform(), child_content_bounds);
4569 gfx::RectF expected_child_draw_rect(child->bounds());
4570 expected_child_draw_rect.Scale(device_scale_factor);
4571 expected_child_draw_rect.set_width(ceil(expected_child_draw_rect.width()));
4572 expected_child_draw_rect.set_height(ceil(expected_child_draw_rect.height()));
4573 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_draw_rect);
4574 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_screen_space_rect);
4576 // Verify child_no_scale transforms
4577 gfx::Transform expected_child_no_scale_transform = child->draw_transform();
4578 // All transforms operate on content rects. The child's content rect
4579 // incorporates device scale, but the child_no_scale does not; add it here.
4580 expected_child_no_scale_transform.Scale(device_scale_factor,
4581 device_scale_factor);
4582 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_no_scale_transform,
4583 child_no_scale->draw_transform());
4584 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_no_scale_transform,
4585 child_no_scale->screen_space_transform());
4588 // TODO(sohanjg): Remove this test when ContentLayer is removed.
4589 TEST_F(LayerTreeHostCommonTest, ContentsScale) {
4590 MockContentLayerClient delegate;
4591 gfx::Transform identity_matrix;
4593 gfx::Transform parent_scale_matrix;
4594 SkMScalar initial_parent_scale = 1.75;
4595 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4597 gfx::Transform child_scale_matrix;
4598 SkMScalar initial_child_scale = 1.25;
4599 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4601 scoped_refptr<Layer> root = Layer::Create();
4602 root->SetBounds(gfx::Size(100, 100));
4604 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
4605 SetLayerPropertiesForTesting(parent.get(),
4606 parent_scale_matrix,
4607 gfx::Point3F(),
4608 gfx::PointF(),
4609 gfx::Size(100, 100),
4610 false,
4611 true);
4613 scoped_refptr<ContentLayer> child_scale =
4614 CreateDrawableContentLayer(&delegate);
4615 SetLayerPropertiesForTesting(child_scale.get(),
4616 child_scale_matrix,
4617 gfx::Point3F(),
4618 gfx::PointF(2.f, 2.f),
4619 gfx::Size(10, 10),
4620 false,
4621 true);
4623 scoped_refptr<ContentLayer> child_empty =
4624 CreateDrawableContentLayer(&delegate);
4625 SetLayerPropertiesForTesting(child_empty.get(),
4626 child_scale_matrix,
4627 gfx::Point3F(),
4628 gfx::PointF(2.f, 2.f),
4629 gfx::Size(),
4630 false,
4631 true);
4633 scoped_refptr<NoScaleContentLayer> child_no_scale =
4634 CreateNoScaleDrawableContentLayer(&delegate);
4635 SetLayerPropertiesForTesting(child_no_scale.get(),
4636 child_scale_matrix,
4637 gfx::Point3F(),
4638 gfx::PointF(12.f, 12.f),
4639 gfx::Size(10, 10),
4640 false,
4641 true);
4643 root->AddChild(parent);
4645 parent->AddChild(child_scale);
4646 parent->AddChild(child_empty);
4647 parent->AddChild(child_no_scale);
4649 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4650 host->SetRootLayer(root);
4652 float device_scale_factor = 2.5f;
4653 float page_scale_factor = 1.f;
4656 RenderSurfaceLayerList render_surface_layer_list;
4657 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4658 root.get(), root->bounds(), &render_surface_layer_list);
4659 inputs.device_scale_factor = device_scale_factor;
4660 inputs.page_scale_factor = page_scale_factor;
4661 inputs.page_scale_application_layer = root.get();
4662 inputs.can_adjust_raster_scales = true;
4663 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4665 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4666 initial_parent_scale, parent);
4667 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4668 initial_parent_scale * initial_child_scale,
4669 child_scale);
4670 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4671 initial_parent_scale * initial_child_scale,
4672 child_empty);
4673 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4675 // The parent is scaled up and shouldn't need to scale during draw. The
4676 // child that can scale its contents should also not need to scale during
4677 // draw. This shouldn't change if the child has empty bounds. The other
4678 // children should.
4679 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(0, 0));
4680 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(1, 1));
4681 EXPECT_FLOAT_EQ(1.0, child_scale->draw_transform().matrix().get(0, 0));
4682 EXPECT_FLOAT_EQ(1.0, child_scale->draw_transform().matrix().get(1, 1));
4683 EXPECT_FLOAT_EQ(1.0, child_empty->draw_transform().matrix().get(0, 0));
4684 EXPECT_FLOAT_EQ(1.0, child_empty->draw_transform().matrix().get(1, 1));
4685 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4686 initial_parent_scale * initial_child_scale,
4687 child_no_scale->draw_transform().matrix().get(0, 0));
4688 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4689 initial_parent_scale * initial_child_scale,
4690 child_no_scale->draw_transform().matrix().get(1, 1));
4693 // If the device_scale_factor or page_scale_factor changes, then it should be
4694 // updated using the initial transform as the raster scale.
4695 device_scale_factor = 2.25f;
4696 page_scale_factor = 1.25f;
4699 RenderSurfaceLayerList render_surface_layer_list;
4700 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4701 root.get(), root->bounds(), &render_surface_layer_list);
4702 inputs.device_scale_factor = device_scale_factor;
4703 inputs.page_scale_factor = page_scale_factor;
4704 inputs.page_scale_application_layer = root.get();
4705 inputs.can_adjust_raster_scales = true;
4706 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4708 EXPECT_CONTENTS_SCALE_EQ(
4709 device_scale_factor * page_scale_factor * initial_parent_scale, parent);
4710 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4711 initial_parent_scale * initial_child_scale,
4712 child_scale);
4713 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4714 initial_parent_scale * initial_child_scale,
4715 child_empty);
4716 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4719 // If the transform changes, we expect the raster scale to be reset to 1.0.
4720 SkMScalar second_child_scale = 1.75;
4721 child_scale_matrix.Scale(second_child_scale / initial_child_scale,
4722 second_child_scale / initial_child_scale);
4723 child_scale->SetTransform(child_scale_matrix);
4724 child_empty->SetTransform(child_scale_matrix);
4727 RenderSurfaceLayerList render_surface_layer_list;
4728 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4729 root.get(), root->bounds(), &render_surface_layer_list);
4730 inputs.device_scale_factor = device_scale_factor;
4731 inputs.page_scale_factor = page_scale_factor;
4732 inputs.page_scale_application_layer = root.get();
4733 inputs.can_adjust_raster_scales = true;
4734 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4736 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4737 initial_parent_scale,
4738 parent);
4739 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4740 child_scale);
4741 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4742 child_empty);
4743 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4746 // If the device_scale_factor or page_scale_factor changes, then it should be
4747 // updated, but still using 1.0 as the raster scale.
4748 device_scale_factor = 2.75f;
4749 page_scale_factor = 1.75f;
4752 RenderSurfaceLayerList render_surface_layer_list;
4753 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4754 root.get(), root->bounds(), &render_surface_layer_list);
4755 inputs.device_scale_factor = device_scale_factor;
4756 inputs.page_scale_factor = page_scale_factor;
4757 inputs.page_scale_application_layer = root.get();
4758 inputs.can_adjust_raster_scales = true;
4759 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4761 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4762 initial_parent_scale,
4763 parent);
4764 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4765 child_scale);
4766 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4767 child_empty);
4768 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4772 // TODO(sohanjg): Remove this test when ContentLayer is removed.
4773 TEST_F(LayerTreeHostCommonTest,
4774 ContentsScale_LayerTransformsDontAffectContentsScale) {
4775 MockContentLayerClient delegate;
4776 gfx::Transform identity_matrix;
4778 gfx::Transform parent_scale_matrix;
4779 SkMScalar initial_parent_scale = 1.75;
4780 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4782 gfx::Transform child_scale_matrix;
4783 SkMScalar initial_child_scale = 1.25;
4784 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4786 scoped_refptr<Layer> root = Layer::Create();
4787 root->SetBounds(gfx::Size(100, 100));
4789 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
4790 SetLayerPropertiesForTesting(parent.get(),
4791 parent_scale_matrix,
4792 gfx::Point3F(),
4793 gfx::PointF(),
4794 gfx::Size(100, 100),
4795 false,
4796 true);
4798 scoped_refptr<ContentLayer> child_scale =
4799 CreateDrawableContentLayer(&delegate);
4800 SetLayerPropertiesForTesting(child_scale.get(),
4801 child_scale_matrix,
4802 gfx::Point3F(),
4803 gfx::PointF(2.f, 2.f),
4804 gfx::Size(10, 10),
4805 false,
4806 true);
4808 scoped_refptr<ContentLayer> child_empty =
4809 CreateDrawableContentLayer(&delegate);
4810 SetLayerPropertiesForTesting(child_empty.get(),
4811 child_scale_matrix,
4812 gfx::Point3F(),
4813 gfx::PointF(2.f, 2.f),
4814 gfx::Size(),
4815 false,
4816 true);
4818 scoped_refptr<NoScaleContentLayer> child_no_scale =
4819 CreateNoScaleDrawableContentLayer(&delegate);
4820 SetLayerPropertiesForTesting(child_no_scale.get(),
4821 child_scale_matrix,
4822 gfx::Point3F(),
4823 gfx::PointF(12.f, 12.f),
4824 gfx::Size(10, 10),
4825 false,
4826 true);
4828 root->AddChild(parent);
4830 parent->AddChild(child_scale);
4831 parent->AddChild(child_empty);
4832 parent->AddChild(child_no_scale);
4834 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4835 host->SetRootLayer(root);
4837 RenderSurfaceLayerList render_surface_layer_list;
4839 float device_scale_factor = 2.5f;
4840 float page_scale_factor = 1.f;
4842 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4843 root.get(), root->bounds(), &render_surface_layer_list);
4844 inputs.device_scale_factor = device_scale_factor;
4845 inputs.page_scale_factor = page_scale_factor;
4846 inputs.page_scale_application_layer = root.get(),
4847 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4849 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4850 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4851 child_scale);
4852 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4853 child_empty);
4854 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4856 // Since the transform scale does not affect contents scale, it should affect
4857 // the draw transform instead.
4858 EXPECT_FLOAT_EQ(initial_parent_scale,
4859 parent->draw_transform().matrix().get(0, 0));
4860 EXPECT_FLOAT_EQ(initial_parent_scale,
4861 parent->draw_transform().matrix().get(1, 1));
4862 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4863 child_scale->draw_transform().matrix().get(0, 0));
4864 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4865 child_scale->draw_transform().matrix().get(1, 1));
4866 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4867 child_empty->draw_transform().matrix().get(0, 0));
4868 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4869 child_empty->draw_transform().matrix().get(1, 1));
4870 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4871 initial_parent_scale * initial_child_scale,
4872 child_no_scale->draw_transform().matrix().get(0, 0));
4873 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4874 initial_parent_scale * initial_child_scale,
4875 child_no_scale->draw_transform().matrix().get(1, 1));
4878 TEST_F(LayerTreeHostCommonTest, SmallIdealScale) {
4879 MockContentLayerClient delegate;
4880 gfx::Transform identity_matrix;
4882 gfx::Transform parent_scale_matrix;
4883 SkMScalar initial_parent_scale = 1.75;
4884 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4886 gfx::Transform child_scale_matrix;
4887 SkMScalar initial_child_scale = 0.25;
4888 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4890 scoped_refptr<Layer> root = Layer::Create();
4891 root->SetBounds(gfx::Size(100, 100));
4893 scoped_refptr<FakePictureLayer> parent =
4894 CreateDrawablePictureLayer(&delegate);
4895 SetLayerPropertiesForTesting(parent.get(),
4896 parent_scale_matrix,
4897 gfx::Point3F(),
4898 gfx::PointF(),
4899 gfx::Size(100, 100),
4900 false,
4901 true);
4903 scoped_refptr<FakePictureLayer> child_scale =
4904 CreateDrawablePictureLayer(&delegate);
4905 SetLayerPropertiesForTesting(child_scale.get(),
4906 child_scale_matrix,
4907 gfx::Point3F(),
4908 gfx::PointF(2.f, 2.f),
4909 gfx::Size(10, 10),
4910 false,
4911 true);
4913 root->AddChild(parent);
4915 parent->AddChild(child_scale);
4917 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4918 host->SetRootLayer(root);
4920 float device_scale_factor = 2.5f;
4921 float page_scale_factor = 0.01f;
4924 RenderSurfaceLayerList render_surface_layer_list;
4925 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4926 root.get(), root->bounds(), &render_surface_layer_list);
4927 inputs.device_scale_factor = device_scale_factor;
4928 inputs.page_scale_factor = page_scale_factor;
4929 inputs.page_scale_application_layer = root.get();
4930 inputs.can_adjust_raster_scales = true;
4931 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4933 // The ideal scale is able to go below 1.
4934 float expected_ideal_scale =
4935 device_scale_factor * page_scale_factor * initial_parent_scale;
4936 EXPECT_LT(expected_ideal_scale, 1.f);
4937 EXPECT_IDEAL_SCALE_EQ(expected_ideal_scale, parent);
4939 expected_ideal_scale = device_scale_factor * page_scale_factor *
4940 initial_parent_scale * initial_child_scale;
4941 EXPECT_LT(expected_ideal_scale, 1.f);
4942 EXPECT_IDEAL_SCALE_EQ(expected_ideal_scale, child_scale);
4946 TEST_F(LayerTreeHostCommonTest, ContentsScaleForSurfaces) {
4947 MockContentLayerClient delegate;
4948 gfx::Transform identity_matrix;
4950 gfx::Transform parent_scale_matrix;
4951 SkMScalar initial_parent_scale = 2.0;
4952 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4954 gfx::Transform child_scale_matrix;
4955 SkMScalar initial_child_scale = 3.0;
4956 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4958 scoped_refptr<Layer> root = Layer::Create();
4959 root->SetBounds(gfx::Size(100, 100));
4961 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
4962 SetLayerPropertiesForTesting(parent.get(),
4963 parent_scale_matrix,
4964 gfx::Point3F(),
4965 gfx::PointF(),
4966 gfx::Size(100, 100),
4967 false,
4968 true);
4970 scoped_refptr<ContentLayer> surface_scale =
4971 CreateDrawableContentLayer(&delegate);
4972 SetLayerPropertiesForTesting(surface_scale.get(),
4973 child_scale_matrix,
4974 gfx::Point3F(),
4975 gfx::PointF(2.f, 2.f),
4976 gfx::Size(10, 10),
4977 false,
4978 true);
4980 scoped_refptr<ContentLayer> surface_scale_child_scale =
4981 CreateDrawableContentLayer(&delegate);
4982 SetLayerPropertiesForTesting(surface_scale_child_scale.get(),
4983 child_scale_matrix,
4984 gfx::Point3F(),
4985 gfx::PointF(),
4986 gfx::Size(10, 10),
4987 false,
4988 true);
4990 scoped_refptr<NoScaleContentLayer> surface_scale_child_no_scale =
4991 CreateNoScaleDrawableContentLayer(&delegate);
4992 SetLayerPropertiesForTesting(surface_scale_child_no_scale.get(),
4993 child_scale_matrix,
4994 gfx::Point3F(),
4995 gfx::PointF(),
4996 gfx::Size(10, 10),
4997 false,
4998 true);
5000 scoped_refptr<NoScaleContentLayer> surface_no_scale =
5001 CreateNoScaleDrawableContentLayer(&delegate);
5002 SetLayerPropertiesForTesting(surface_no_scale.get(),
5003 child_scale_matrix,
5004 gfx::Point3F(),
5005 gfx::PointF(12.f, 12.f),
5006 gfx::Size(10, 10),
5007 false,
5008 true);
5010 scoped_refptr<ContentLayer> surface_no_scale_child_scale =
5011 CreateDrawableContentLayer(&delegate);
5012 SetLayerPropertiesForTesting(surface_no_scale_child_scale.get(),
5013 child_scale_matrix,
5014 gfx::Point3F(),
5015 gfx::PointF(),
5016 gfx::Size(10, 10),
5017 false,
5018 true);
5020 scoped_refptr<NoScaleContentLayer> surface_no_scale_child_no_scale =
5021 CreateNoScaleDrawableContentLayer(&delegate);
5022 SetLayerPropertiesForTesting(surface_no_scale_child_no_scale.get(),
5023 child_scale_matrix,
5024 gfx::Point3F(),
5025 gfx::PointF(),
5026 gfx::Size(10, 10),
5027 false,
5028 true);
5030 root->AddChild(parent);
5032 parent->AddChild(surface_scale);
5033 parent->AddChild(surface_no_scale);
5035 surface_scale->SetForceRenderSurface(true);
5036 surface_scale->AddChild(surface_scale_child_scale);
5037 surface_scale->AddChild(surface_scale_child_no_scale);
5039 surface_no_scale->SetForceRenderSurface(true);
5040 surface_no_scale->AddChild(surface_no_scale_child_scale);
5041 surface_no_scale->AddChild(surface_no_scale_child_no_scale);
5043 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5044 host->SetRootLayer(root);
5046 SkMScalar device_scale_factor = 5;
5047 SkMScalar page_scale_factor = 7;
5049 RenderSurfaceLayerList render_surface_layer_list;
5050 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5051 root.get(), root->bounds(), &render_surface_layer_list);
5052 inputs.device_scale_factor = device_scale_factor;
5053 inputs.page_scale_factor = page_scale_factor;
5054 inputs.page_scale_application_layer = root.get();
5055 inputs.can_adjust_raster_scales = true;
5056 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5058 EXPECT_CONTENTS_SCALE_EQ(
5059 device_scale_factor * page_scale_factor * initial_parent_scale, parent);
5060 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
5061 initial_parent_scale * initial_child_scale,
5062 surface_scale);
5063 EXPECT_CONTENTS_SCALE_EQ(1, surface_no_scale);
5064 EXPECT_CONTENTS_SCALE_EQ(
5065 device_scale_factor * page_scale_factor * initial_parent_scale *
5066 initial_child_scale * initial_child_scale,
5067 surface_scale_child_scale);
5068 EXPECT_CONTENTS_SCALE_EQ(1, surface_scale_child_no_scale);
5069 EXPECT_CONTENTS_SCALE_EQ(
5070 device_scale_factor * page_scale_factor * initial_parent_scale *
5071 initial_child_scale * initial_child_scale,
5072 surface_no_scale_child_scale);
5073 EXPECT_CONTENTS_SCALE_EQ(1, surface_no_scale_child_no_scale);
5075 // The parent is scaled up and shouldn't need to scale during draw.
5076 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(0, 0));
5077 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(1, 1));
5079 // RenderSurfaces should always be 1:1 with their target.
5080 EXPECT_FLOAT_EQ(
5081 1.0,
5082 surface_scale->render_surface()->draw_transform().matrix().get(0, 0));
5083 EXPECT_FLOAT_EQ(
5084 1.0,
5085 surface_scale->render_surface()->draw_transform().matrix().get(1, 1));
5087 // The surface_scale can apply contents scale so the layer shouldn't need to
5088 // scale during draw.
5089 EXPECT_FLOAT_EQ(1.0, surface_scale->draw_transform().matrix().get(0, 0));
5090 EXPECT_FLOAT_EQ(1.0, surface_scale->draw_transform().matrix().get(1, 1));
5092 // The surface_scale_child_scale can apply contents scale so it shouldn't need
5093 // to scale during draw.
5094 EXPECT_FLOAT_EQ(
5095 1.0, surface_scale_child_scale->draw_transform().matrix().get(0, 0));
5096 EXPECT_FLOAT_EQ(
5097 1.0, surface_scale_child_scale->draw_transform().matrix().get(1, 1));
5099 // The surface_scale_child_no_scale can not apply contents scale, so it needs
5100 // to be scaled during draw.
5101 EXPECT_FLOAT_EQ(
5102 device_scale_factor * page_scale_factor * initial_parent_scale *
5103 initial_child_scale * initial_child_scale,
5104 surface_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5105 EXPECT_FLOAT_EQ(
5106 device_scale_factor * page_scale_factor * initial_parent_scale *
5107 initial_child_scale * initial_child_scale,
5108 surface_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5110 // RenderSurfaces should always be 1:1 with their target.
5111 EXPECT_FLOAT_EQ(
5112 1.0,
5113 surface_no_scale->render_surface()->draw_transform().matrix().get(0, 0));
5114 EXPECT_FLOAT_EQ(
5115 1.0,
5116 surface_no_scale->render_surface()->draw_transform().matrix().get(1, 1));
5118 // The surface_no_scale layer can not apply contents scale, so it needs to be
5119 // scaled during draw.
5120 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
5121 initial_parent_scale * initial_child_scale,
5122 surface_no_scale->draw_transform().matrix().get(0, 0));
5123 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
5124 initial_parent_scale * initial_child_scale,
5125 surface_no_scale->draw_transform().matrix().get(1, 1));
5127 // The surface_scale_child_scale can apply contents scale so it shouldn't need
5128 // to scale during draw.
5129 EXPECT_FLOAT_EQ(
5130 1.0, surface_no_scale_child_scale->draw_transform().matrix().get(0, 0));
5131 EXPECT_FLOAT_EQ(
5132 1.0, surface_no_scale_child_scale->draw_transform().matrix().get(1, 1));
5134 // The surface_scale_child_no_scale can not apply contents scale, so it needs
5135 // to be scaled during draw.
5136 EXPECT_FLOAT_EQ(
5137 device_scale_factor * page_scale_factor * initial_parent_scale *
5138 initial_child_scale * initial_child_scale,
5139 surface_no_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5140 EXPECT_FLOAT_EQ(
5141 device_scale_factor * page_scale_factor * initial_parent_scale *
5142 initial_child_scale * initial_child_scale,
5143 surface_no_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5146 // TODO(sohanjg): Remove this test when ContentLayer is removed.
5147 TEST_F(LayerTreeHostCommonTest,
5148 ContentsScaleForSurfaces_LayerTransformsDontAffectContentsScale) {
5149 MockContentLayerClient delegate;
5150 gfx::Transform identity_matrix;
5152 gfx::Transform parent_scale_matrix;
5153 SkMScalar initial_parent_scale = 2.0;
5154 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
5156 gfx::Transform child_scale_matrix;
5157 SkMScalar initial_child_scale = 3.0;
5158 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
5160 scoped_refptr<Layer> root = Layer::Create();
5161 root->SetBounds(gfx::Size(100, 100));
5163 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
5164 SetLayerPropertiesForTesting(parent.get(),
5165 parent_scale_matrix,
5166 gfx::Point3F(),
5167 gfx::PointF(),
5168 gfx::Size(100, 100),
5169 false,
5170 true);
5172 scoped_refptr<ContentLayer> surface_scale =
5173 CreateDrawableContentLayer(&delegate);
5174 SetLayerPropertiesForTesting(surface_scale.get(),
5175 child_scale_matrix,
5176 gfx::Point3F(),
5177 gfx::PointF(2.f, 2.f),
5178 gfx::Size(10, 10),
5179 false,
5180 true);
5182 scoped_refptr<ContentLayer> surface_scale_child_scale =
5183 CreateDrawableContentLayer(&delegate);
5184 SetLayerPropertiesForTesting(surface_scale_child_scale.get(),
5185 child_scale_matrix,
5186 gfx::Point3F(),
5187 gfx::PointF(),
5188 gfx::Size(10, 10),
5189 false,
5190 true);
5192 scoped_refptr<NoScaleContentLayer> surface_scale_child_no_scale =
5193 CreateNoScaleDrawableContentLayer(&delegate);
5194 SetLayerPropertiesForTesting(surface_scale_child_no_scale.get(),
5195 child_scale_matrix,
5196 gfx::Point3F(),
5197 gfx::PointF(),
5198 gfx::Size(10, 10),
5199 false,
5200 true);
5202 scoped_refptr<NoScaleContentLayer> surface_no_scale =
5203 CreateNoScaleDrawableContentLayer(&delegate);
5204 SetLayerPropertiesForTesting(surface_no_scale.get(),
5205 child_scale_matrix,
5206 gfx::Point3F(),
5207 gfx::PointF(12.f, 12.f),
5208 gfx::Size(10, 10),
5209 false,
5210 true);
5212 scoped_refptr<ContentLayer> surface_no_scale_child_scale =
5213 CreateDrawableContentLayer(&delegate);
5214 SetLayerPropertiesForTesting(surface_no_scale_child_scale.get(),
5215 child_scale_matrix,
5216 gfx::Point3F(),
5217 gfx::PointF(),
5218 gfx::Size(10, 10),
5219 false,
5220 true);
5222 scoped_refptr<NoScaleContentLayer> surface_no_scale_child_no_scale =
5223 CreateNoScaleDrawableContentLayer(&delegate);
5224 SetLayerPropertiesForTesting(surface_no_scale_child_no_scale.get(),
5225 child_scale_matrix,
5226 gfx::Point3F(),
5227 gfx::PointF(),
5228 gfx::Size(10, 10),
5229 false,
5230 true);
5232 root->AddChild(parent);
5234 parent->AddChild(surface_scale);
5235 parent->AddChild(surface_no_scale);
5237 surface_scale->SetForceRenderSurface(true);
5238 surface_scale->AddChild(surface_scale_child_scale);
5239 surface_scale->AddChild(surface_scale_child_no_scale);
5241 surface_no_scale->SetForceRenderSurface(true);
5242 surface_no_scale->AddChild(surface_no_scale_child_scale);
5243 surface_no_scale->AddChild(surface_no_scale_child_no_scale);
5245 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5246 host->SetRootLayer(root);
5248 RenderSurfaceLayerList render_surface_layer_list;
5250 SkMScalar device_scale_factor = 5.0;
5251 SkMScalar page_scale_factor = 7.0;
5252 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5253 root.get(), root->bounds(), &render_surface_layer_list);
5254 inputs.device_scale_factor = device_scale_factor;
5255 inputs.page_scale_factor = page_scale_factor;
5256 inputs.page_scale_application_layer = root.get();
5257 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5259 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5260 parent);
5261 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5262 surface_scale);
5263 EXPECT_CONTENTS_SCALE_EQ(1.f, surface_no_scale);
5264 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5265 surface_scale_child_scale);
5266 EXPECT_CONTENTS_SCALE_EQ(1.f, surface_scale_child_no_scale);
5267 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5268 surface_no_scale_child_scale);
5269 EXPECT_CONTENTS_SCALE_EQ(1.f, surface_no_scale_child_no_scale);
5271 // The parent is scaled up during draw, since its contents are not scaled by
5272 // the transform hierarchy.
5273 EXPECT_FLOAT_EQ(initial_parent_scale,
5274 parent->draw_transform().matrix().get(0, 0));
5275 EXPECT_FLOAT_EQ(initial_parent_scale,
5276 parent->draw_transform().matrix().get(1, 1));
5278 // The child surface is not scaled up during draw since its subtree is scaled
5279 // by the transform hierarchy.
5280 EXPECT_FLOAT_EQ(
5281 1.f,
5282 surface_scale->render_surface()->draw_transform().matrix().get(0, 0));
5283 EXPECT_FLOAT_EQ(
5284 1.f,
5285 surface_scale->render_surface()->draw_transform().matrix().get(1, 1));
5287 // The surface_scale's RenderSurface is not scaled during draw, so the layer
5288 // needs to be scaled when drawing into its surface.
5289 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
5290 surface_scale->draw_transform().matrix().get(0, 0));
5291 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
5292 surface_scale->draw_transform().matrix().get(1, 1));
5294 // The surface_scale_child_scale is not scaled when drawing into its surface,
5295 // since its content bounds are scaled by the transform hierarchy.
5296 EXPECT_FLOAT_EQ(
5297 initial_child_scale * initial_child_scale * initial_parent_scale,
5298 surface_scale_child_scale->draw_transform().matrix().get(0, 0));
5299 EXPECT_FLOAT_EQ(
5300 initial_child_scale * initial_child_scale * initial_parent_scale,
5301 surface_scale_child_scale->draw_transform().matrix().get(1, 1));
5303 // The surface_scale_child_no_scale is scaled by the device scale, page scale
5304 // and transform hierarchy.
5305 EXPECT_FLOAT_EQ(
5306 device_scale_factor * page_scale_factor * initial_parent_scale *
5307 initial_child_scale * initial_child_scale,
5308 surface_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5309 EXPECT_FLOAT_EQ(
5310 device_scale_factor * page_scale_factor * initial_parent_scale *
5311 initial_child_scale * initial_child_scale,
5312 surface_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5314 // The child surface is not scaled up during draw since its subtree is scaled
5315 // by the transform hierarchy.
5316 EXPECT_FLOAT_EQ(
5317 1.f,
5318 surface_no_scale->render_surface()->draw_transform().matrix().get(0, 0));
5319 EXPECT_FLOAT_EQ(
5320 1.f,
5321 surface_no_scale->render_surface()->draw_transform().matrix().get(1, 1));
5323 // The surface_no_scale layer has a fixed contents scale of 1, so it needs to
5324 // be scaled by the device and page scale factors. Its surface is already
5325 // scaled by the transform hierarchy so those don't need to scale the layer's
5326 // drawing.
5327 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale *
5328 device_scale_factor * page_scale_factor,
5329 surface_no_scale->draw_transform().matrix().get(0, 0));
5330 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale *
5331 device_scale_factor * page_scale_factor,
5332 surface_no_scale->draw_transform().matrix().get(1, 1));
5334 // The surface_no_scale_child_scale has its contents scaled by the page and
5335 // device scale factors, but needs to be scaled by the transform hierarchy
5336 // when drawing.
5337 EXPECT_FLOAT_EQ(
5338 initial_parent_scale * initial_child_scale * initial_child_scale,
5339 surface_no_scale_child_scale->draw_transform().matrix().get(0, 0));
5340 EXPECT_FLOAT_EQ(
5341 initial_parent_scale * initial_child_scale * initial_child_scale,
5342 surface_no_scale_child_scale->draw_transform().matrix().get(1, 1));
5344 // The surface_no_scale_child_no_scale needs to be scaled by the device and
5345 // page scale factors and by any transform heirarchy below its target surface.
5346 EXPECT_FLOAT_EQ(
5347 device_scale_factor * page_scale_factor * initial_parent_scale *
5348 initial_child_scale * initial_child_scale,
5349 surface_no_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5350 EXPECT_FLOAT_EQ(
5351 device_scale_factor * page_scale_factor * initial_parent_scale *
5352 initial_child_scale * initial_child_scale,
5353 surface_no_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5356 TEST_F(LayerTreeHostCommonTest, IdealScaleForAnimatingLayer) {
5357 MockContentLayerClient delegate;
5358 gfx::Transform identity_matrix;
5360 gfx::Transform parent_scale_matrix;
5361 SkMScalar initial_parent_scale = 1.75;
5362 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
5364 gfx::Transform child_scale_matrix;
5365 SkMScalar initial_child_scale = 1.25;
5366 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
5368 scoped_refptr<Layer> root = Layer::Create();
5369 root->SetBounds(gfx::Size(100, 100));
5371 scoped_refptr<FakePictureLayer> parent =
5372 CreateDrawablePictureLayer(&delegate);
5373 SetLayerPropertiesForTesting(parent.get(),
5374 parent_scale_matrix,
5375 gfx::Point3F(),
5376 gfx::PointF(),
5377 gfx::Size(100, 100),
5378 false,
5379 true);
5381 scoped_refptr<FakePictureLayer> child_scale =
5382 CreateDrawablePictureLayer(&delegate);
5383 SetLayerPropertiesForTesting(child_scale.get(),
5384 child_scale_matrix,
5385 gfx::Point3F(),
5386 gfx::PointF(2.f, 2.f),
5387 gfx::Size(10, 10),
5388 false,
5389 true);
5391 root->AddChild(parent);
5393 parent->AddChild(child_scale);
5395 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5396 host->SetRootLayer(root);
5399 RenderSurfaceLayerList render_surface_layer_list;
5400 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5401 root.get(), root->bounds(), &render_surface_layer_list);
5402 inputs.can_adjust_raster_scales = true;
5403 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5405 EXPECT_IDEAL_SCALE_EQ(initial_parent_scale, parent);
5406 // Animating layers compute ideal scale in the same way as when
5407 // they are static.
5408 EXPECT_IDEAL_SCALE_EQ(initial_child_scale * initial_parent_scale,
5409 child_scale);
5413 // TODO(sohanjg): Remove this test when ContentLayer is removed.
5414 TEST_F(LayerTreeHostCommonTest,
5415 ChangeInContentBoundsOrScaleTriggersPushProperties) {
5416 MockContentLayerClient delegate;
5417 scoped_refptr<Layer> root = Layer::Create();
5418 scoped_refptr<Layer> child = CreateDrawableContentLayer(&delegate);
5419 root->AddChild(child);
5421 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5422 host->SetRootLayer(root);
5424 gfx::Transform identity_matrix;
5425 SetLayerPropertiesForTesting(root.get(),
5426 identity_matrix,
5427 gfx::Point3F(),
5428 gfx::PointF(),
5429 gfx::Size(100, 100),
5430 true,
5431 false);
5432 SetLayerPropertiesForTesting(child.get(),
5433 identity_matrix,
5434 gfx::Point3F(),
5435 gfx::PointF(),
5436 gfx::Size(100, 100),
5437 true,
5438 false);
5440 root->reset_needs_push_properties_for_testing();
5441 child->reset_needs_push_properties_for_testing();
5443 // This will change both layers' content bounds.
5444 ExecuteCalculateDrawProperties(root.get());
5445 EXPECT_TRUE(root->needs_push_properties());
5446 EXPECT_TRUE(child->needs_push_properties());
5448 root->reset_needs_push_properties_for_testing();
5449 child->reset_needs_push_properties_for_testing();
5451 // This will change only the child layer's contents scale and content bounds,
5452 // since the root layer is not a ContentsScalingLayer.
5453 ExecuteCalculateDrawProperties(root.get(), 2.f);
5454 EXPECT_FALSE(root->needs_push_properties());
5455 EXPECT_TRUE(child->needs_push_properties());
5457 root->reset_needs_push_properties_for_testing();
5458 child->reset_needs_push_properties_for_testing();
5460 // This will not change either layer's contents scale or content bounds.
5461 ExecuteCalculateDrawProperties(root.get(), 2.f);
5462 EXPECT_FALSE(root->needs_push_properties());
5463 EXPECT_FALSE(child->needs_push_properties());
5466 TEST_F(LayerTreeHostCommonTest, RenderSurfaceTransformsInHighDPI) {
5467 MockContentLayerClient delegate;
5468 gfx::Transform identity_matrix;
5470 scoped_refptr<FakePictureLayer> parent =
5471 CreateDrawablePictureLayer(&delegate);
5472 SetLayerPropertiesForTesting(parent.get(),
5473 identity_matrix,
5474 gfx::Point3F(),
5475 gfx::PointF(),
5476 gfx::Size(30, 30),
5477 false,
5478 true);
5480 scoped_refptr<FakePictureLayer> child = CreateDrawablePictureLayer(&delegate);
5481 SetLayerPropertiesForTesting(child.get(),
5482 identity_matrix,
5483 gfx::Point3F(),
5484 gfx::PointF(2.f, 2.f),
5485 gfx::Size(10, 10),
5486 false,
5487 true);
5489 gfx::Transform replica_transform;
5490 replica_transform.Scale(1.0, -1.0);
5491 scoped_refptr<FakePictureLayer> replica =
5492 CreateDrawablePictureLayer(&delegate);
5493 SetLayerPropertiesForTesting(replica.get(),
5494 replica_transform,
5495 gfx::Point3F(),
5496 gfx::PointF(2.f, 2.f),
5497 gfx::Size(10, 10),
5498 false,
5499 true);
5501 // This layer should end up in the same surface as child, with the same draw
5502 // and screen space transforms.
5503 scoped_refptr<FakePictureLayer> duplicate_child_non_owner =
5504 CreateDrawablePictureLayer(&delegate);
5505 SetLayerPropertiesForTesting(duplicate_child_non_owner.get(),
5506 identity_matrix,
5507 gfx::Point3F(),
5508 gfx::PointF(),
5509 gfx::Size(10, 10),
5510 false,
5511 true);
5513 parent->AddChild(child);
5514 child->AddChild(duplicate_child_non_owner);
5515 child->SetReplicaLayer(replica.get());
5517 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5518 host->SetRootLayer(parent);
5520 RenderSurfaceLayerList render_surface_layer_list;
5522 float device_scale_factor = 1.5f;
5523 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5524 parent.get(), parent->bounds(), &render_surface_layer_list);
5525 inputs.device_scale_factor = device_scale_factor;
5526 inputs.can_adjust_raster_scales = true;
5527 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5529 // We should have two render surfaces. The root's render surface and child's
5530 // render surface (it needs one because it has a replica layer).
5531 EXPECT_EQ(2u, render_surface_layer_list.size());
5533 gfx::Transform expected_parent_transform;
5534 expected_parent_transform.Scale(device_scale_factor, device_scale_factor);
5535 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
5536 parent->screen_space_transform());
5537 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
5538 parent->draw_transform());
5540 gfx::Transform expected_draw_transform;
5541 expected_draw_transform.Scale(device_scale_factor, device_scale_factor);
5542 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_draw_transform,
5543 child->draw_transform());
5545 gfx::Transform expected_screen_space_transform;
5546 expected_screen_space_transform.Scale(device_scale_factor,
5547 device_scale_factor);
5548 expected_screen_space_transform.Translate(child->position().x(),
5549 child->position().y());
5550 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_screen_space_transform,
5551 child->screen_space_transform());
5553 gfx::Transform expected_duplicate_child_draw_transform =
5554 child->draw_transform();
5555 EXPECT_TRANSFORMATION_MATRIX_EQ(child->draw_transform(),
5556 duplicate_child_non_owner->draw_transform());
5557 EXPECT_TRANSFORMATION_MATRIX_EQ(
5558 child->screen_space_transform(),
5559 duplicate_child_non_owner->screen_space_transform());
5560 EXPECT_EQ(child->drawable_content_rect(),
5561 duplicate_child_non_owner->drawable_content_rect());
5562 EXPECT_EQ(child->content_bounds(),
5563 duplicate_child_non_owner->content_bounds());
5565 gfx::Transform expected_render_surface_draw_transform;
5566 expected_render_surface_draw_transform.Translate(
5567 device_scale_factor * child->position().x(),
5568 device_scale_factor * child->position().y());
5569 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_render_surface_draw_transform,
5570 child->render_surface()->draw_transform());
5572 gfx::Transform expected_surface_draw_transform;
5573 expected_surface_draw_transform.Translate(device_scale_factor * 2.f,
5574 device_scale_factor * 2.f);
5575 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_surface_draw_transform,
5576 child->render_surface()->draw_transform());
5578 gfx::Transform expected_surface_screen_space_transform;
5579 expected_surface_screen_space_transform.Translate(device_scale_factor * 2.f,
5580 device_scale_factor * 2.f);
5581 EXPECT_TRANSFORMATION_MATRIX_EQ(
5582 expected_surface_screen_space_transform,
5583 child->render_surface()->screen_space_transform());
5585 gfx::Transform expected_replica_draw_transform;
5586 expected_replica_draw_transform.matrix().set(1, 1, -1.0);
5587 expected_replica_draw_transform.matrix().set(0, 3, 6.0);
5588 expected_replica_draw_transform.matrix().set(1, 3, 6.0);
5589 EXPECT_TRANSFORMATION_MATRIX_EQ(
5590 expected_replica_draw_transform,
5591 child->render_surface()->replica_draw_transform());
5593 gfx::Transform expected_replica_screen_space_transform;
5594 expected_replica_screen_space_transform.matrix().set(1, 1, -1.0);
5595 expected_replica_screen_space_transform.matrix().set(0, 3, 6.0);
5596 expected_replica_screen_space_transform.matrix().set(1, 3, 6.0);
5597 EXPECT_TRANSFORMATION_MATRIX_EQ(
5598 expected_replica_screen_space_transform,
5599 child->render_surface()->replica_screen_space_transform());
5600 EXPECT_TRANSFORMATION_MATRIX_EQ(
5601 expected_replica_screen_space_transform,
5602 child->render_surface()->replica_screen_space_transform());
5605 TEST_F(LayerTreeHostCommonTest,
5606 RenderSurfaceTransformsInHighDPIAccurateScaleZeroPosition) {
5607 MockContentLayerClient delegate;
5608 gfx::Transform identity_matrix;
5610 scoped_refptr<FakePictureLayer> parent =
5611 CreateDrawablePictureLayer(&delegate);
5612 SetLayerPropertiesForTesting(parent.get(),
5613 identity_matrix,
5614 gfx::Point3F(),
5615 gfx::PointF(),
5616 gfx::Size(33, 31),
5617 false,
5618 true);
5620 scoped_refptr<FakePictureLayer> child = CreateDrawablePictureLayer(&delegate);
5621 SetLayerPropertiesForTesting(child.get(),
5622 identity_matrix,
5623 gfx::Point3F(),
5624 gfx::PointF(),
5625 gfx::Size(13, 11),
5626 false,
5627 true);
5629 gfx::Transform replica_transform;
5630 replica_transform.Scale(1.0, -1.0);
5631 scoped_refptr<FakePictureLayer> replica =
5632 CreateDrawablePictureLayer(&delegate);
5633 SetLayerPropertiesForTesting(replica.get(),
5634 replica_transform,
5635 gfx::Point3F(),
5636 gfx::PointF(),
5637 gfx::Size(13, 11),
5638 false,
5639 true);
5641 parent->AddChild(child);
5642 child->SetReplicaLayer(replica.get());
5644 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5645 host->SetRootLayer(parent);
5647 float device_scale_factor = 1.7f;
5649 RenderSurfaceLayerList render_surface_layer_list;
5650 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5651 parent.get(), parent->bounds(), &render_surface_layer_list);
5652 inputs.device_scale_factor = device_scale_factor;
5653 inputs.can_adjust_raster_scales = true;
5654 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5656 // We should have two render surfaces. The root's render surface and child's
5657 // render surface (it needs one because it has a replica layer).
5658 EXPECT_EQ(2u, render_surface_layer_list.size());
5660 gfx::Transform identity_transform;
5661 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
5662 child->render_surface()->draw_transform());
5663 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
5664 child->render_surface()->draw_transform());
5665 EXPECT_TRANSFORMATION_MATRIX_EQ(
5666 identity_transform, child->render_surface()->screen_space_transform());
5668 gfx::Transform expected_replica_draw_transform;
5669 expected_replica_draw_transform.matrix().set(1, 1, -1.0);
5670 EXPECT_TRANSFORMATION_MATRIX_EQ(
5671 expected_replica_draw_transform,
5672 child->render_surface()->replica_draw_transform());
5674 gfx::Transform expected_replica_screen_space_transform;
5675 expected_replica_screen_space_transform.matrix().set(1, 1, -1.0);
5676 EXPECT_TRANSFORMATION_MATRIX_EQ(
5677 expected_replica_screen_space_transform,
5678 child->render_surface()->replica_screen_space_transform());
5681 TEST_F(LayerTreeHostCommonTest, SubtreeSearch) {
5682 scoped_refptr<Layer> root = Layer::Create();
5683 scoped_refptr<Layer> child = Layer::Create();
5684 scoped_refptr<Layer> grand_child = Layer::Create();
5685 scoped_refptr<Layer> mask_layer = Layer::Create();
5686 scoped_refptr<Layer> replica_layer = Layer::Create();
5688 grand_child->SetReplicaLayer(replica_layer.get());
5689 child->AddChild(grand_child.get());
5690 child->SetMaskLayer(mask_layer.get());
5691 root->AddChild(child.get());
5693 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5694 host->SetRootLayer(root);
5696 int nonexistent_id = -1;
5697 EXPECT_EQ(root.get(),
5698 LayerTreeHostCommon::FindLayerInSubtree(root.get(), root->id()));
5699 EXPECT_EQ(child.get(),
5700 LayerTreeHostCommon::FindLayerInSubtree(root.get(), child->id()));
5701 EXPECT_EQ(
5702 grand_child.get(),
5703 LayerTreeHostCommon::FindLayerInSubtree(root.get(), grand_child->id()));
5704 EXPECT_EQ(
5705 mask_layer.get(),
5706 LayerTreeHostCommon::FindLayerInSubtree(root.get(), mask_layer->id()));
5707 EXPECT_EQ(
5708 replica_layer.get(),
5709 LayerTreeHostCommon::FindLayerInSubtree(root.get(), replica_layer->id()));
5710 EXPECT_EQ(
5711 0, LayerTreeHostCommon::FindLayerInSubtree(root.get(), nonexistent_id));
5714 TEST_F(LayerTreeHostCommonTest, TransparentChildRenderSurfaceCreation) {
5715 scoped_refptr<Layer> root = Layer::Create();
5716 scoped_refptr<Layer> child = Layer::Create();
5717 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
5718 make_scoped_refptr(new LayerWithForcedDrawsContent());
5720 const gfx::Transform identity_matrix;
5721 SetLayerPropertiesForTesting(root.get(),
5722 identity_matrix,
5723 gfx::Point3F(),
5724 gfx::PointF(),
5725 gfx::Size(100, 100),
5726 true,
5727 false);
5728 SetLayerPropertiesForTesting(child.get(),
5729 identity_matrix,
5730 gfx::Point3F(),
5731 gfx::PointF(),
5732 gfx::Size(10, 10),
5733 true,
5734 false);
5735 SetLayerPropertiesForTesting(grand_child.get(),
5736 identity_matrix,
5737 gfx::Point3F(),
5738 gfx::PointF(),
5739 gfx::Size(10, 10),
5740 true,
5741 false);
5743 root->AddChild(child);
5744 child->AddChild(grand_child);
5745 child->SetOpacity(0.5f);
5747 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5748 host->SetRootLayer(root);
5750 ExecuteCalculateDrawProperties(root.get());
5752 EXPECT_FALSE(child->render_surface());
5755 TEST_F(LayerTreeHostCommonTest, OpacityAnimatingOnPendingTree) {
5756 FakeImplProxy proxy;
5757 TestSharedBitmapManager shared_bitmap_manager;
5758 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
5759 host_impl.CreatePendingTree();
5760 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
5762 const gfx::Transform identity_matrix;
5763 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
5764 gfx::PointF(), gfx::Size(100, 100), true, false,
5765 false);
5766 root->SetDrawsContent(true);
5768 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
5769 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
5770 gfx::PointF(), gfx::Size(50, 50), true, false,
5771 false);
5772 child->SetDrawsContent(true);
5773 child->SetOpacity(0.0f);
5775 // Add opacity animation.
5776 AddOpacityTransitionToController(
5777 child->layer_animation_controller(), 10.0, 0.0f, 1.0f, false);
5779 root->AddChild(child.Pass());
5780 root->SetHasRenderSurface(true);
5782 LayerImplList render_surface_layer_list;
5783 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
5784 root.get(), root->bounds(), &render_surface_layer_list);
5785 inputs.can_adjust_raster_scales = true;
5786 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5788 // We should have one render surface and two layers. The child
5789 // layer should be included even though it is transparent.
5790 ASSERT_EQ(1u, render_surface_layer_list.size());
5791 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
5794 using LCDTextTestParam = std::tr1::tuple<bool, bool, bool>;
5795 class LCDTextTest
5796 : public LayerTreeHostCommonTestBase,
5797 public testing::TestWithParam<LCDTextTestParam> {
5798 public:
5799 LCDTextTest()
5800 : host_impl_(&proxy_, &shared_bitmap_manager_, &task_graph_runner_),
5801 root_(nullptr),
5802 child_(nullptr),
5803 grand_child_(nullptr) {}
5805 protected:
5806 void SetUp() override {
5807 can_use_lcd_text_ = std::tr1::get<0>(GetParam());
5808 layers_always_allowed_lcd_text_ = std::tr1::get<1>(GetParam());
5810 scoped_ptr<LayerImpl> root_ptr =
5811 LayerImpl::Create(host_impl_.active_tree(), 1);
5812 scoped_ptr<LayerImpl> child_ptr =
5813 LayerImpl::Create(host_impl_.active_tree(), 2);
5814 scoped_ptr<LayerImpl> grand_child_ptr =
5815 LayerImpl::Create(host_impl_.active_tree(), 3);
5817 // Stash raw pointers to look at later.
5818 root_ = root_ptr.get();
5819 child_ = child_ptr.get();
5820 grand_child_ = grand_child_ptr.get();
5822 child_->AddChild(grand_child_ptr.Pass());
5823 root_->AddChild(child_ptr.Pass());
5824 host_impl_.active_tree()->SetRootLayer(root_ptr.Pass());
5826 root_->SetContentsOpaque(true);
5827 child_->SetContentsOpaque(true);
5828 grand_child_->SetContentsOpaque(true);
5830 gfx::Transform identity_matrix;
5831 SetLayerPropertiesForTesting(root_, identity_matrix, gfx::Point3F(),
5832 gfx::PointF(), gfx::Size(1, 1), true, false,
5833 true);
5834 SetLayerPropertiesForTesting(child_, identity_matrix, gfx::Point3F(),
5835 gfx::PointF(), gfx::Size(1, 1), true, false,
5836 std::tr1::get<2>(GetParam()));
5837 SetLayerPropertiesForTesting(grand_child_, identity_matrix, gfx::Point3F(),
5838 gfx::PointF(), gfx::Size(1, 1), true, false,
5839 false);
5842 bool can_use_lcd_text_;
5843 bool layers_always_allowed_lcd_text_;
5845 FakeImplProxy proxy_;
5846 TestSharedBitmapManager shared_bitmap_manager_;
5847 TestTaskGraphRunner task_graph_runner_;
5848 FakeLayerTreeHostImpl host_impl_;
5850 LayerImpl* root_;
5851 LayerImpl* child_;
5852 LayerImpl* grand_child_;
5855 TEST_P(LCDTextTest, CanUseLCDText) {
5856 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
5857 bool expect_not_lcd_text = layers_always_allowed_lcd_text_;
5859 // Case 1: Identity transform.
5860 gfx::Transform identity_matrix;
5861 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5862 layers_always_allowed_lcd_text_);
5863 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5864 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5865 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5867 // Case 2: Integral translation.
5868 gfx::Transform integral_translation;
5869 integral_translation.Translate(1.0, 2.0);
5870 child_->SetTransform(integral_translation);
5871 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5872 layers_always_allowed_lcd_text_);
5873 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5874 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5875 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5877 // Case 3: Non-integral translation.
5878 gfx::Transform non_integral_translation;
5879 non_integral_translation.Translate(1.5, 2.5);
5880 child_->SetTransform(non_integral_translation);
5881 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5882 layers_always_allowed_lcd_text_);
5883 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5884 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5885 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5887 // Case 4: Rotation.
5888 gfx::Transform rotation;
5889 rotation.Rotate(10.0);
5890 child_->SetTransform(rotation);
5891 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5892 layers_always_allowed_lcd_text_);
5893 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5894 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5895 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5897 // Case 5: Scale.
5898 gfx::Transform scale;
5899 scale.Scale(2.0, 2.0);
5900 child_->SetTransform(scale);
5901 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5902 layers_always_allowed_lcd_text_);
5903 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5904 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5905 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5907 // Case 6: Skew.
5908 gfx::Transform skew;
5909 skew.SkewX(10.0);
5910 child_->SetTransform(skew);
5911 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5912 layers_always_allowed_lcd_text_);
5913 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5914 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5915 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5917 // Case 7: Translucent.
5918 child_->SetTransform(identity_matrix);
5919 child_->SetOpacity(0.5f);
5920 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5921 layers_always_allowed_lcd_text_);
5922 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5923 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5924 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5926 // Case 8: Sanity check: restore transform and opacity.
5927 child_->SetTransform(identity_matrix);
5928 child_->SetOpacity(1.f);
5929 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5930 layers_always_allowed_lcd_text_);
5931 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5932 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5933 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5935 // Case 9: Non-opaque content.
5936 child_->SetContentsOpaque(false);
5937 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5938 layers_always_allowed_lcd_text_);
5939 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5940 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5941 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5943 // Case 10: Sanity check: restore content opaqueness.
5944 child_->SetContentsOpaque(true);
5945 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5946 layers_always_allowed_lcd_text_);
5947 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5948 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5949 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5952 TEST_P(LCDTextTest, CanUseLCDTextWithAnimation) {
5953 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
5955 // Sanity check: Make sure can_use_lcd_text_ is set on each node.
5956 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5957 layers_always_allowed_lcd_text_);
5958 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5959 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5960 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5962 // Add opacity animation.
5963 child_->SetOpacity(0.9f);
5964 AddOpacityTransitionToController(
5965 child_->layer_animation_controller(), 10.0, 0.9f, 0.1f, false);
5967 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5968 layers_always_allowed_lcd_text_);
5969 // Text AA should not be adjusted while animation is active.
5970 // Make sure LCD text AA setting remains unchanged.
5971 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5972 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5973 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5976 INSTANTIATE_TEST_CASE_P(LayerTreeHostCommonTest,
5977 LCDTextTest,
5978 testing::Combine(testing::Bool(),
5979 testing::Bool(),
5980 testing::Bool()));
5982 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_SingleLayer) {
5983 FakeImplProxy proxy;
5984 TestSharedBitmapManager shared_bitmap_manager;
5985 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
5986 host_impl.CreatePendingTree();
5987 const gfx::Transform identity_matrix;
5989 scoped_refptr<Layer> root = Layer::Create();
5990 SetLayerPropertiesForTesting(root.get(),
5991 identity_matrix,
5992 gfx::Point3F(),
5993 gfx::PointF(),
5994 gfx::Size(50, 50),
5995 true,
5996 false);
5997 root->SetIsDrawable(true);
5999 scoped_refptr<Layer> child = Layer::Create();
6000 SetLayerPropertiesForTesting(child.get(),
6001 identity_matrix,
6002 gfx::Point3F(),
6003 gfx::PointF(),
6004 gfx::Size(40, 40),
6005 true,
6006 false);
6007 child->SetIsDrawable(true);
6009 scoped_refptr<Layer> grand_child = Layer::Create();
6010 SetLayerPropertiesForTesting(grand_child.get(),
6011 identity_matrix,
6012 gfx::Point3F(),
6013 gfx::PointF(),
6014 gfx::Size(30, 30),
6015 true,
6016 false);
6017 grand_child->SetIsDrawable(true);
6018 grand_child->SetHideLayerAndSubtree(true);
6020 child->AddChild(grand_child);
6021 root->AddChild(child);
6023 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6024 host->SetRootLayer(root);
6026 RenderSurfaceLayerList render_surface_layer_list;
6027 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6028 root.get(), root->bounds(), &render_surface_layer_list);
6029 inputs.can_adjust_raster_scales = true;
6030 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6032 // We should have one render surface and two layers. The grand child has
6033 // hidden itself.
6034 ASSERT_EQ(1u, render_surface_layer_list.size());
6035 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6036 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6037 EXPECT_EQ(child->id(), root->render_surface()->layer_list().at(1)->id());
6040 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_SingleLayerImpl) {
6041 FakeImplProxy proxy;
6042 TestSharedBitmapManager shared_bitmap_manager;
6043 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6044 host_impl.CreatePendingTree();
6045 const gfx::Transform identity_matrix;
6047 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
6048 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
6049 gfx::PointF(), gfx::Size(50, 50), true, false,
6050 false);
6051 root->SetDrawsContent(true);
6053 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
6054 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
6055 gfx::PointF(), gfx::Size(40, 40), true, false,
6056 false);
6057 child->SetDrawsContent(true);
6059 scoped_ptr<LayerImpl> grand_child =
6060 LayerImpl::Create(host_impl.pending_tree(), 3);
6061 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
6062 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6063 true, false, false);
6064 grand_child->SetDrawsContent(true);
6065 grand_child->SetHideLayerAndSubtree(true);
6067 child->AddChild(grand_child.Pass());
6068 root->AddChild(child.Pass());
6069 root->SetHasRenderSurface(true);
6071 LayerImplList render_surface_layer_list;
6072 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6073 root.get(), root->bounds(), &render_surface_layer_list);
6074 inputs.can_adjust_raster_scales = true;
6075 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6077 // We should have one render surface and two layers. The grand child has
6078 // hidden itself.
6079 ASSERT_EQ(1u, render_surface_layer_list.size());
6080 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6081 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id());
6082 EXPECT_EQ(2, root->render_surface()->layer_list().at(1)->id());
6085 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_TwoLayers) {
6086 FakeImplProxy proxy;
6087 TestSharedBitmapManager shared_bitmap_manager;
6088 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6089 host_impl.CreatePendingTree();
6090 const gfx::Transform identity_matrix;
6092 scoped_refptr<Layer> root = Layer::Create();
6093 SetLayerPropertiesForTesting(root.get(),
6094 identity_matrix,
6095 gfx::Point3F(),
6096 gfx::PointF(),
6097 gfx::Size(50, 50),
6098 true,
6099 false);
6100 root->SetIsDrawable(true);
6102 scoped_refptr<Layer> child = Layer::Create();
6103 SetLayerPropertiesForTesting(child.get(),
6104 identity_matrix,
6105 gfx::Point3F(),
6106 gfx::PointF(),
6107 gfx::Size(40, 40),
6108 true,
6109 false);
6110 child->SetIsDrawable(true);
6111 child->SetHideLayerAndSubtree(true);
6113 scoped_refptr<Layer> grand_child = Layer::Create();
6114 SetLayerPropertiesForTesting(grand_child.get(),
6115 identity_matrix,
6116 gfx::Point3F(),
6117 gfx::PointF(),
6118 gfx::Size(30, 30),
6119 true,
6120 false);
6121 grand_child->SetIsDrawable(true);
6123 child->AddChild(grand_child);
6124 root->AddChild(child);
6126 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6127 host->SetRootLayer(root);
6129 RenderSurfaceLayerList render_surface_layer_list;
6130 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6131 root.get(), root->bounds(), &render_surface_layer_list);
6132 inputs.can_adjust_raster_scales = true;
6133 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6135 // We should have one render surface and one layers. The child has
6136 // hidden itself and the grand child.
6137 ASSERT_EQ(1u, render_surface_layer_list.size());
6138 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6139 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6142 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_TwoLayersImpl) {
6143 FakeImplProxy proxy;
6144 TestSharedBitmapManager shared_bitmap_manager;
6145 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6146 host_impl.CreatePendingTree();
6147 const gfx::Transform identity_matrix;
6149 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
6150 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
6151 gfx::PointF(), gfx::Size(50, 50), true, false,
6152 true);
6153 root->SetDrawsContent(true);
6155 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
6156 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
6157 gfx::PointF(), gfx::Size(40, 40), true, false,
6158 false);
6159 child->SetDrawsContent(true);
6160 child->SetHideLayerAndSubtree(true);
6162 scoped_ptr<LayerImpl> grand_child =
6163 LayerImpl::Create(host_impl.pending_tree(), 3);
6164 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
6165 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6166 true, false, false);
6167 grand_child->SetDrawsContent(true);
6169 child->AddChild(grand_child.Pass());
6170 root->AddChild(child.Pass());
6172 LayerImplList render_surface_layer_list;
6173 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6174 root.get(), root->bounds(), &render_surface_layer_list);
6175 inputs.can_adjust_raster_scales = true;
6176 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6178 // We should have one render surface and one layers. The child has
6179 // hidden itself and the grand child.
6180 ASSERT_EQ(1u, render_surface_layer_list.size());
6181 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6182 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id());
6185 void EmptyCopyOutputCallback(scoped_ptr<CopyOutputResult> result) {}
6187 TEST_F(LayerTreeHostCommonTest, SubtreeHiddenWithCopyRequest) {
6188 FakeImplProxy proxy;
6189 TestSharedBitmapManager shared_bitmap_manager;
6190 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6191 host_impl.CreatePendingTree();
6192 const gfx::Transform identity_matrix;
6194 scoped_refptr<Layer> root = Layer::Create();
6195 SetLayerPropertiesForTesting(root.get(),
6196 identity_matrix,
6197 gfx::Point3F(),
6198 gfx::PointF(),
6199 gfx::Size(50, 50),
6200 true,
6201 false);
6202 root->SetIsDrawable(true);
6204 scoped_refptr<Layer> copy_grand_parent = Layer::Create();
6205 SetLayerPropertiesForTesting(copy_grand_parent.get(),
6206 identity_matrix,
6207 gfx::Point3F(),
6208 gfx::PointF(),
6209 gfx::Size(40, 40),
6210 true,
6211 false);
6212 copy_grand_parent->SetIsDrawable(true);
6214 scoped_refptr<Layer> copy_parent = Layer::Create();
6215 SetLayerPropertiesForTesting(copy_parent.get(),
6216 identity_matrix,
6217 gfx::Point3F(),
6218 gfx::PointF(),
6219 gfx::Size(30, 30),
6220 true,
6221 false);
6222 copy_parent->SetIsDrawable(true);
6223 copy_parent->SetForceRenderSurface(true);
6225 scoped_refptr<Layer> copy_layer = Layer::Create();
6226 SetLayerPropertiesForTesting(copy_layer.get(),
6227 identity_matrix,
6228 gfx::Point3F(),
6229 gfx::PointF(),
6230 gfx::Size(20, 20),
6231 true,
6232 false);
6233 copy_layer->SetIsDrawable(true);
6235 scoped_refptr<Layer> copy_child = Layer::Create();
6236 SetLayerPropertiesForTesting(copy_child.get(),
6237 identity_matrix,
6238 gfx::Point3F(),
6239 gfx::PointF(),
6240 gfx::Size(20, 20),
6241 true,
6242 false);
6243 copy_child->SetIsDrawable(true);
6245 scoped_refptr<Layer> copy_grand_parent_sibling_before = Layer::Create();
6246 SetLayerPropertiesForTesting(copy_grand_parent_sibling_before.get(),
6247 identity_matrix,
6248 gfx::Point3F(),
6249 gfx::PointF(),
6250 gfx::Size(40, 40),
6251 true,
6252 false);
6253 copy_grand_parent_sibling_before->SetIsDrawable(true);
6255 scoped_refptr<Layer> copy_grand_parent_sibling_after = Layer::Create();
6256 SetLayerPropertiesForTesting(copy_grand_parent_sibling_after.get(),
6257 identity_matrix,
6258 gfx::Point3F(),
6259 gfx::PointF(),
6260 gfx::Size(40, 40),
6261 true,
6262 false);
6263 copy_grand_parent_sibling_after->SetIsDrawable(true);
6265 copy_layer->AddChild(copy_child);
6266 copy_parent->AddChild(copy_layer);
6267 copy_grand_parent->AddChild(copy_parent);
6268 root->AddChild(copy_grand_parent_sibling_before);
6269 root->AddChild(copy_grand_parent);
6270 root->AddChild(copy_grand_parent_sibling_after);
6272 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6273 host->SetRootLayer(root);
6275 // Hide the copy_grand_parent and its subtree. But make a copy request in that
6276 // hidden subtree on copy_layer.
6277 copy_grand_parent->SetHideLayerAndSubtree(true);
6278 copy_grand_parent_sibling_before->SetHideLayerAndSubtree(true);
6279 copy_grand_parent_sibling_after->SetHideLayerAndSubtree(true);
6280 copy_layer->RequestCopyOfOutput(CopyOutputRequest::CreateRequest(
6281 base::Bind(&EmptyCopyOutputCallback)));
6282 EXPECT_TRUE(copy_layer->HasCopyRequest());
6284 RenderSurfaceLayerList render_surface_layer_list;
6285 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6286 root.get(), root->bounds(), &render_surface_layer_list);
6287 inputs.can_adjust_raster_scales = true;
6288 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6290 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
6291 EXPECT_TRUE(copy_grand_parent->draw_properties().
6292 layer_or_descendant_has_copy_request);
6293 EXPECT_TRUE(copy_parent->draw_properties().
6294 layer_or_descendant_has_copy_request);
6295 EXPECT_TRUE(copy_layer->draw_properties().
6296 layer_or_descendant_has_copy_request);
6297 EXPECT_FALSE(copy_child->draw_properties().
6298 layer_or_descendant_has_copy_request);
6299 EXPECT_FALSE(copy_grand_parent_sibling_before->draw_properties().
6300 layer_or_descendant_has_copy_request);
6301 EXPECT_FALSE(copy_grand_parent_sibling_after->draw_properties().
6302 layer_or_descendant_has_copy_request);
6304 // We should have three render surfaces, one for the root, one for the parent
6305 // since it owns a surface, and one for the copy_layer.
6306 ASSERT_EQ(3u, render_surface_layer_list.size());
6307 EXPECT_EQ(root->id(), render_surface_layer_list.at(0)->id());
6308 EXPECT_EQ(copy_parent->id(), render_surface_layer_list.at(1)->id());
6309 EXPECT_EQ(copy_layer->id(), render_surface_layer_list.at(2)->id());
6311 // The root render surface should have 2 contributing layers. The
6312 // copy_grand_parent is hidden along with its siblings, but the copy_parent
6313 // will appear since something in its subtree needs to be drawn for a copy
6314 // request.
6315 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6316 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6317 EXPECT_EQ(copy_parent->id(),
6318 root->render_surface()->layer_list().at(1)->id());
6320 // Nothing actually draws into the copy parent, so only the copy_layer will
6321 // appear in its list, since it needs to be drawn for the copy request.
6322 ASSERT_EQ(1u, copy_parent->render_surface()->layer_list().size());
6323 EXPECT_EQ(copy_layer->id(),
6324 copy_parent->render_surface()->layer_list().at(0)->id());
6326 // The copy_layer's render surface should have two contributing layers.
6327 ASSERT_EQ(2u, copy_layer->render_surface()->layer_list().size());
6328 EXPECT_EQ(copy_layer->id(),
6329 copy_layer->render_surface()->layer_list().at(0)->id());
6330 EXPECT_EQ(copy_child->id(),
6331 copy_layer->render_surface()->layer_list().at(1)->id());
6334 TEST_F(LayerTreeHostCommonTest, ClippedOutCopyRequest) {
6335 FakeImplProxy proxy;
6336 TestSharedBitmapManager shared_bitmap_manager;
6337 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6338 host_impl.CreatePendingTree();
6339 const gfx::Transform identity_matrix;
6341 scoped_refptr<Layer> root = Layer::Create();
6342 SetLayerPropertiesForTesting(root.get(),
6343 identity_matrix,
6344 gfx::Point3F(),
6345 gfx::PointF(),
6346 gfx::Size(50, 50),
6347 true,
6348 false);
6349 root->SetIsDrawable(true);
6351 scoped_refptr<Layer> copy_parent = Layer::Create();
6352 SetLayerPropertiesForTesting(copy_parent.get(),
6353 identity_matrix,
6354 gfx::Point3F(),
6355 gfx::PointF(),
6356 gfx::Size(),
6357 true,
6358 false);
6359 copy_parent->SetIsDrawable(true);
6360 copy_parent->SetMasksToBounds(true);
6362 scoped_refptr<Layer> copy_layer = Layer::Create();
6363 SetLayerPropertiesForTesting(copy_layer.get(),
6364 identity_matrix,
6365 gfx::Point3F(),
6366 gfx::PointF(),
6367 gfx::Size(30, 30),
6368 true,
6369 false);
6370 copy_layer->SetIsDrawable(true);
6372 scoped_refptr<Layer> copy_child = Layer::Create();
6373 SetLayerPropertiesForTesting(copy_child.get(),
6374 identity_matrix,
6375 gfx::Point3F(),
6376 gfx::PointF(),
6377 gfx::Size(20, 20),
6378 true,
6379 false);
6380 copy_child->SetIsDrawable(true);
6382 copy_layer->AddChild(copy_child);
6383 copy_parent->AddChild(copy_layer);
6384 root->AddChild(copy_parent);
6386 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6387 host->SetRootLayer(root);
6389 copy_layer->RequestCopyOfOutput(CopyOutputRequest::CreateRequest(
6390 base::Bind(&EmptyCopyOutputCallback)));
6391 EXPECT_TRUE(copy_layer->HasCopyRequest());
6393 RenderSurfaceLayerList render_surface_layer_list;
6394 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6395 root.get(), root->bounds(), &render_surface_layer_list);
6396 inputs.can_adjust_raster_scales = true;
6397 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6399 // We should have one render surface, as the others are clipped out.
6400 ASSERT_EQ(1u, render_surface_layer_list.size());
6401 EXPECT_EQ(root->id(), render_surface_layer_list.at(0)->id());
6403 // The root render surface should only have 1 contributing layer, since the
6404 // other layers are empty/clipped away.
6405 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6406 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6409 TEST_F(LayerTreeHostCommonTest, VisibleContentRectInsideSurface) {
6410 FakeImplProxy proxy;
6411 TestSharedBitmapManager shared_bitmap_manager;
6412 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6413 host_impl.CreatePendingTree();
6414 const gfx::Transform identity_matrix;
6416 scoped_refptr<Layer> root = Layer::Create();
6417 SetLayerPropertiesForTesting(root.get(),
6418 identity_matrix,
6419 gfx::Point3F(),
6420 gfx::PointF(),
6421 gfx::Size(50, 50),
6422 true,
6423 false);
6424 root->SetIsDrawable(true);
6426 // The surface is moved slightly outside of the viewport.
6427 scoped_refptr<Layer> surface = Layer::Create();
6428 SetLayerPropertiesForTesting(surface.get(),
6429 identity_matrix,
6430 gfx::Point3F(),
6431 gfx::PointF(-10, -20),
6432 gfx::Size(),
6433 true,
6434 false);
6435 surface->SetForceRenderSurface(true);
6437 scoped_refptr<Layer> surface_child = Layer::Create();
6438 SetLayerPropertiesForTesting(surface_child.get(),
6439 identity_matrix,
6440 gfx::Point3F(),
6441 gfx::PointF(),
6442 gfx::Size(50, 50),
6443 true,
6444 false);
6445 surface_child->SetIsDrawable(true);
6447 surface->AddChild(surface_child);
6448 root->AddChild(surface);
6450 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6451 host->SetRootLayer(root);
6453 RenderSurfaceLayerList render_surface_layer_list;
6454 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6455 root.get(), root->bounds(), &render_surface_layer_list);
6456 inputs.can_adjust_raster_scales = true;
6457 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6459 // The visible_content_rect for the |surface_child| should not be clipped by
6460 // the viewport.
6461 EXPECT_EQ(gfx::Rect(50, 50).ToString(),
6462 surface_child->visible_content_rect().ToString());
6465 TEST_F(LayerTreeHostCommonTest, TransformedClipParent) {
6466 // Ensure that a transform between the layer and its render surface is not a
6467 // problem. Constructs the following layer tree.
6469 // root (a render surface)
6470 // + render_surface
6471 // + clip_parent (scaled)
6472 // + intervening_clipping_layer
6473 // + clip_child
6475 // The render surface should be resized correctly and the clip child should
6476 // inherit the right clip rect.
6477 scoped_refptr<Layer> root = Layer::Create();
6478 scoped_refptr<Layer> render_surface = Layer::Create();
6479 scoped_refptr<Layer> clip_parent = Layer::Create();
6480 scoped_refptr<Layer> intervening = Layer::Create();
6481 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6482 make_scoped_refptr(new LayerWithForcedDrawsContent);
6484 root->AddChild(render_surface);
6485 render_surface->AddChild(clip_parent);
6486 clip_parent->AddChild(intervening);
6487 intervening->AddChild(clip_child);
6489 clip_child->SetClipParent(clip_parent.get());
6491 intervening->SetMasksToBounds(true);
6492 clip_parent->SetMasksToBounds(true);
6494 render_surface->SetForceRenderSurface(true);
6496 gfx::Transform scale_transform;
6497 scale_transform.Scale(2, 2);
6499 gfx::Transform identity_transform;
6501 SetLayerPropertiesForTesting(root.get(),
6502 identity_transform,
6503 gfx::Point3F(),
6504 gfx::PointF(),
6505 gfx::Size(50, 50),
6506 true,
6507 false);
6508 SetLayerPropertiesForTesting(render_surface.get(),
6509 identity_transform,
6510 gfx::Point3F(),
6511 gfx::PointF(),
6512 gfx::Size(10, 10),
6513 true,
6514 false);
6515 SetLayerPropertiesForTesting(clip_parent.get(),
6516 scale_transform,
6517 gfx::Point3F(),
6518 gfx::PointF(1.f, 1.f),
6519 gfx::Size(10, 10),
6520 true,
6521 false);
6522 SetLayerPropertiesForTesting(intervening.get(),
6523 identity_transform,
6524 gfx::Point3F(),
6525 gfx::PointF(1.f, 1.f),
6526 gfx::Size(5, 5),
6527 true,
6528 false);
6529 SetLayerPropertiesForTesting(clip_child.get(),
6530 identity_transform,
6531 gfx::Point3F(),
6532 gfx::PointF(1.f, 1.f),
6533 gfx::Size(10, 10),
6534 true,
6535 false);
6537 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6538 host->SetRootLayer(root);
6540 ExecuteCalculateDrawProperties(root.get());
6542 ASSERT_TRUE(root->render_surface());
6543 ASSERT_TRUE(render_surface->render_surface());
6545 // Ensure that we've inherited our clip parent's clip and weren't affected
6546 // by the intervening clip layer.
6547 ASSERT_EQ(gfx::Rect(1, 1, 20, 20).ToString(),
6548 clip_parent->clip_rect().ToString());
6549 ASSERT_EQ(clip_parent->clip_rect().ToString(),
6550 clip_child->clip_rect().ToString());
6551 ASSERT_EQ(gfx::Rect(3, 3, 10, 10).ToString(),
6552 intervening->clip_rect().ToString());
6554 // Ensure that the render surface reports a content rect that has been grown
6555 // to accomodate for the clip child.
6556 ASSERT_EQ(gfx::Rect(5, 5, 16, 16).ToString(),
6557 render_surface->render_surface()->content_rect().ToString());
6559 // The above check implies the two below, but they nicely demonstrate that
6560 // we've grown, despite the intervening layer's clip.
6561 ASSERT_TRUE(clip_parent->clip_rect().Contains(
6562 render_surface->render_surface()->content_rect()));
6563 ASSERT_FALSE(intervening->clip_rect().Contains(
6564 render_surface->render_surface()->content_rect()));
6567 TEST_F(LayerTreeHostCommonTest, ClipParentWithInterveningRenderSurface) {
6568 // Ensure that intervening render surfaces are not a problem in the basic
6569 // case. In the following tree, both render surfaces should be resized to
6570 // accomodate for the clip child, despite an intervening clip.
6572 // root (a render surface)
6573 // + clip_parent (masks to bounds)
6574 // + render_surface1 (sets opacity)
6575 // + intervening (masks to bounds)
6576 // + render_surface2 (also sets opacity)
6577 // + clip_child
6579 scoped_refptr<Layer> root = Layer::Create();
6580 scoped_refptr<Layer> clip_parent = Layer::Create();
6581 scoped_refptr<Layer> render_surface1 = Layer::Create();
6582 scoped_refptr<Layer> intervening = Layer::Create();
6583 scoped_refptr<Layer> render_surface2 = Layer::Create();
6584 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6585 make_scoped_refptr(new LayerWithForcedDrawsContent);
6587 root->AddChild(clip_parent);
6588 clip_parent->AddChild(render_surface1);
6589 render_surface1->AddChild(intervening);
6590 intervening->AddChild(render_surface2);
6591 render_surface2->AddChild(clip_child);
6593 clip_child->SetClipParent(clip_parent.get());
6595 intervening->SetMasksToBounds(true);
6596 clip_parent->SetMasksToBounds(true);
6598 render_surface1->SetForceRenderSurface(true);
6599 render_surface2->SetForceRenderSurface(true);
6601 gfx::Transform translation_transform;
6602 translation_transform.Translate(2, 2);
6604 gfx::Transform identity_transform;
6605 SetLayerPropertiesForTesting(root.get(),
6606 identity_transform,
6607 gfx::Point3F(),
6608 gfx::PointF(),
6609 gfx::Size(50, 50),
6610 true,
6611 false);
6612 SetLayerPropertiesForTesting(clip_parent.get(),
6613 translation_transform,
6614 gfx::Point3F(),
6615 gfx::PointF(1.f, 1.f),
6616 gfx::Size(40, 40),
6617 true,
6618 false);
6619 SetLayerPropertiesForTesting(render_surface1.get(),
6620 identity_transform,
6621 gfx::Point3F(),
6622 gfx::PointF(),
6623 gfx::Size(10, 10),
6624 true,
6625 false);
6626 SetLayerPropertiesForTesting(intervening.get(),
6627 identity_transform,
6628 gfx::Point3F(),
6629 gfx::PointF(1.f, 1.f),
6630 gfx::Size(5, 5),
6631 true,
6632 false);
6633 SetLayerPropertiesForTesting(render_surface2.get(),
6634 identity_transform,
6635 gfx::Point3F(),
6636 gfx::PointF(),
6637 gfx::Size(10, 10),
6638 true,
6639 false);
6640 SetLayerPropertiesForTesting(clip_child.get(),
6641 identity_transform,
6642 gfx::Point3F(),
6643 gfx::PointF(-10.f, -10.f),
6644 gfx::Size(60, 60),
6645 true,
6646 false);
6648 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6649 host->SetRootLayer(root);
6651 ExecuteCalculateDrawProperties(root.get());
6653 EXPECT_TRUE(root->render_surface());
6654 EXPECT_TRUE(render_surface1->render_surface());
6655 EXPECT_TRUE(render_surface2->render_surface());
6657 // Since the render surfaces could have expanded, they should not clip (their
6658 // bounds would no longer be reliable). We should resort to layer clipping
6659 // in this case.
6660 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6661 render_surface1->render_surface()->clip_rect().ToString());
6662 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
6663 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6664 render_surface2->render_surface()->clip_rect().ToString());
6665 EXPECT_FALSE(render_surface2->render_surface()->is_clipped());
6667 // NB: clip rects are in target space.
6668 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6669 render_surface1->clip_rect().ToString());
6670 EXPECT_TRUE(render_surface1->is_clipped());
6672 // This value is inherited from the clipping ancestor layer, 'intervening'.
6673 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
6674 render_surface2->clip_rect().ToString());
6675 EXPECT_TRUE(render_surface2->is_clipped());
6677 // The content rects of both render surfaces should both have expanded to
6678 // contain the clip child.
6679 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6680 render_surface1->render_surface()->content_rect().ToString());
6681 EXPECT_EQ(gfx::Rect(-1, -1, 40, 40).ToString(),
6682 render_surface2->render_surface()->content_rect().ToString());
6684 // The clip child should have inherited the clip parent's clip (projected to
6685 // the right space, of course), and should have the correctly sized visible
6686 // content rect.
6687 EXPECT_EQ(gfx::Rect(-1, -1, 40, 40).ToString(),
6688 clip_child->clip_rect().ToString());
6689 EXPECT_EQ(gfx::Rect(9, 9, 40, 40).ToString(),
6690 clip_child->visible_content_rect().ToString());
6691 EXPECT_TRUE(clip_child->is_clipped());
6694 TEST_F(LayerTreeHostCommonTest, ClipParentScrolledInterveningLayer) {
6695 // Ensure that intervening render surfaces are not a problem, even if there
6696 // is a scroll involved. Note, we do _not_ have to consider any other sort
6697 // of transform.
6699 // root (a render surface)
6700 // + clip_parent (masks to bounds)
6701 // + render_surface1 (sets opacity)
6702 // + intervening (masks to bounds AND scrolls)
6703 // + render_surface2 (also sets opacity)
6704 // + clip_child
6706 scoped_refptr<Layer> root = Layer::Create();
6707 scoped_refptr<Layer> clip_parent = Layer::Create();
6708 scoped_refptr<Layer> render_surface1 = Layer::Create();
6709 scoped_refptr<Layer> intervening = Layer::Create();
6710 scoped_refptr<Layer> render_surface2 = Layer::Create();
6711 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6712 make_scoped_refptr(new LayerWithForcedDrawsContent);
6714 root->AddChild(clip_parent);
6715 clip_parent->AddChild(render_surface1);
6716 render_surface1->AddChild(intervening);
6717 intervening->AddChild(render_surface2);
6718 render_surface2->AddChild(clip_child);
6720 clip_child->SetClipParent(clip_parent.get());
6722 intervening->SetMasksToBounds(true);
6723 clip_parent->SetMasksToBounds(true);
6724 intervening->SetScrollClipLayerId(clip_parent->id());
6725 intervening->SetScrollOffset(gfx::ScrollOffset(3, 3));
6727 render_surface1->SetForceRenderSurface(true);
6728 render_surface2->SetForceRenderSurface(true);
6730 gfx::Transform translation_transform;
6731 translation_transform.Translate(2, 2);
6733 gfx::Transform identity_transform;
6734 SetLayerPropertiesForTesting(root.get(),
6735 identity_transform,
6736 gfx::Point3F(),
6737 gfx::PointF(),
6738 gfx::Size(50, 50),
6739 true,
6740 false);
6741 SetLayerPropertiesForTesting(clip_parent.get(),
6742 translation_transform,
6743 gfx::Point3F(),
6744 gfx::PointF(1.f, 1.f),
6745 gfx::Size(40, 40),
6746 true,
6747 false);
6748 SetLayerPropertiesForTesting(render_surface1.get(),
6749 identity_transform,
6750 gfx::Point3F(),
6751 gfx::PointF(),
6752 gfx::Size(10, 10),
6753 true,
6754 false);
6755 SetLayerPropertiesForTesting(intervening.get(),
6756 identity_transform,
6757 gfx::Point3F(),
6758 gfx::PointF(1.f, 1.f),
6759 gfx::Size(5, 5),
6760 true,
6761 false);
6762 SetLayerPropertiesForTesting(render_surface2.get(),
6763 identity_transform,
6764 gfx::Point3F(),
6765 gfx::PointF(),
6766 gfx::Size(10, 10),
6767 true,
6768 false);
6769 SetLayerPropertiesForTesting(clip_child.get(),
6770 identity_transform,
6771 gfx::Point3F(),
6772 gfx::PointF(-10.f, -10.f),
6773 gfx::Size(60, 60),
6774 true,
6775 false);
6777 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6778 host->SetRootLayer(root);
6780 ExecuteCalculateDrawProperties(root.get());
6782 EXPECT_TRUE(root->render_surface());
6783 EXPECT_TRUE(render_surface1->render_surface());
6784 EXPECT_TRUE(render_surface2->render_surface());
6786 // Since the render surfaces could have expanded, they should not clip (their
6787 // bounds would no longer be reliable). We should resort to layer clipping
6788 // in this case.
6789 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6790 render_surface1->render_surface()->clip_rect().ToString());
6791 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
6792 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6793 render_surface2->render_surface()->clip_rect().ToString());
6794 EXPECT_FALSE(render_surface2->render_surface()->is_clipped());
6796 // NB: clip rects are in target space.
6797 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6798 render_surface1->clip_rect().ToString());
6799 EXPECT_TRUE(render_surface1->is_clipped());
6801 // This value is inherited from the clipping ancestor layer, 'intervening'.
6802 EXPECT_EQ(gfx::Rect(2, 2, 3, 3).ToString(),
6803 render_surface2->clip_rect().ToString());
6804 EXPECT_TRUE(render_surface2->is_clipped());
6806 // The content rects of both render surfaces should both have expanded to
6807 // contain the clip child.
6808 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6809 render_surface1->render_surface()->content_rect().ToString());
6810 EXPECT_EQ(gfx::Rect(2, 2, 40, 40).ToString(),
6811 render_surface2->render_surface()->content_rect().ToString());
6813 // The clip child should have inherited the clip parent's clip (projected to
6814 // the right space, of course), and should have the correctly sized visible
6815 // content rect.
6816 EXPECT_EQ(gfx::Rect(2, 2, 40, 40).ToString(),
6817 clip_child->clip_rect().ToString());
6818 EXPECT_EQ(gfx::Rect(12, 12, 40, 40).ToString(),
6819 clip_child->visible_content_rect().ToString());
6820 EXPECT_TRUE(clip_child->is_clipped());
6823 TEST_F(LayerTreeHostCommonTest, DescendantsOfClipChildren) {
6824 // Ensures that descendants of the clip child inherit the correct clip.
6826 // root (a render surface)
6827 // + clip_parent (masks to bounds)
6828 // + intervening (masks to bounds)
6829 // + clip_child
6830 // + child
6832 scoped_refptr<Layer> root = Layer::Create();
6833 scoped_refptr<Layer> clip_parent = Layer::Create();
6834 scoped_refptr<Layer> intervening = Layer::Create();
6835 scoped_refptr<Layer> clip_child = Layer::Create();
6836 scoped_refptr<LayerWithForcedDrawsContent> child =
6837 make_scoped_refptr(new LayerWithForcedDrawsContent);
6839 root->AddChild(clip_parent);
6840 clip_parent->AddChild(intervening);
6841 intervening->AddChild(clip_child);
6842 clip_child->AddChild(child);
6844 clip_child->SetClipParent(clip_parent.get());
6846 intervening->SetMasksToBounds(true);
6847 clip_parent->SetMasksToBounds(true);
6849 gfx::Transform identity_transform;
6850 SetLayerPropertiesForTesting(root.get(),
6851 identity_transform,
6852 gfx::Point3F(),
6853 gfx::PointF(),
6854 gfx::Size(50, 50),
6855 true,
6856 false);
6857 SetLayerPropertiesForTesting(clip_parent.get(),
6858 identity_transform,
6859 gfx::Point3F(),
6860 gfx::PointF(),
6861 gfx::Size(40, 40),
6862 true,
6863 false);
6864 SetLayerPropertiesForTesting(intervening.get(),
6865 identity_transform,
6866 gfx::Point3F(),
6867 gfx::PointF(),
6868 gfx::Size(5, 5),
6869 true,
6870 false);
6871 SetLayerPropertiesForTesting(clip_child.get(),
6872 identity_transform,
6873 gfx::Point3F(),
6874 gfx::PointF(),
6875 gfx::Size(60, 60),
6876 true,
6877 false);
6878 SetLayerPropertiesForTesting(child.get(),
6879 identity_transform,
6880 gfx::Point3F(),
6881 gfx::PointF(),
6882 gfx::Size(60, 60),
6883 true,
6884 false);
6886 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6887 host->SetRootLayer(root);
6889 ExecuteCalculateDrawProperties(root.get());
6891 EXPECT_TRUE(root->render_surface());
6893 // Neither the clip child nor its descendant should have inherited the clip
6894 // from |intervening|.
6895 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6896 clip_child->clip_rect().ToString());
6897 EXPECT_TRUE(clip_child->is_clipped());
6898 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6899 child->visible_content_rect().ToString());
6900 EXPECT_TRUE(child->is_clipped());
6903 TEST_F(LayerTreeHostCommonTest,
6904 SurfacesShouldBeUnaffectedByNonDescendantClipChildren) {
6905 // Ensures that non-descendant clip children in the tree do not affect
6906 // render surfaces.
6908 // root (a render surface)
6909 // + clip_parent (masks to bounds)
6910 // + render_surface1
6911 // + clip_child
6912 // + render_surface2
6913 // + non_clip_child
6915 // In this example render_surface2 should be unaffected by clip_child.
6916 scoped_refptr<Layer> root = Layer::Create();
6917 scoped_refptr<Layer> clip_parent = Layer::Create();
6918 scoped_refptr<Layer> render_surface1 = Layer::Create();
6919 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6920 make_scoped_refptr(new LayerWithForcedDrawsContent);
6921 scoped_refptr<Layer> render_surface2 = Layer::Create();
6922 scoped_refptr<LayerWithForcedDrawsContent> non_clip_child =
6923 make_scoped_refptr(new LayerWithForcedDrawsContent);
6925 root->AddChild(clip_parent);
6926 clip_parent->AddChild(render_surface1);
6927 render_surface1->AddChild(clip_child);
6928 clip_parent->AddChild(render_surface2);
6929 render_surface2->AddChild(non_clip_child);
6931 clip_child->SetClipParent(clip_parent.get());
6933 clip_parent->SetMasksToBounds(true);
6934 render_surface1->SetMasksToBounds(true);
6936 gfx::Transform identity_transform;
6937 SetLayerPropertiesForTesting(root.get(),
6938 identity_transform,
6939 gfx::Point3F(),
6940 gfx::PointF(),
6941 gfx::Size(15, 15),
6942 true,
6943 false);
6944 SetLayerPropertiesForTesting(clip_parent.get(),
6945 identity_transform,
6946 gfx::Point3F(),
6947 gfx::PointF(),
6948 gfx::Size(10, 10),
6949 true,
6950 false);
6951 SetLayerPropertiesForTesting(render_surface1.get(),
6952 identity_transform,
6953 gfx::Point3F(),
6954 gfx::PointF(5, 5),
6955 gfx::Size(5, 5),
6956 true,
6957 false);
6958 SetLayerPropertiesForTesting(render_surface2.get(),
6959 identity_transform,
6960 gfx::Point3F(),
6961 gfx::PointF(),
6962 gfx::Size(5, 5),
6963 true,
6964 false);
6965 SetLayerPropertiesForTesting(clip_child.get(),
6966 identity_transform,
6967 gfx::Point3F(),
6968 gfx::PointF(-1, 1),
6969 gfx::Size(10, 10),
6970 true,
6971 false);
6972 SetLayerPropertiesForTesting(non_clip_child.get(),
6973 identity_transform,
6974 gfx::Point3F(),
6975 gfx::PointF(),
6976 gfx::Size(5, 5),
6977 true,
6978 false);
6980 render_surface1->SetForceRenderSurface(true);
6981 render_surface2->SetForceRenderSurface(true);
6983 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6984 host->SetRootLayer(root);
6986 ExecuteCalculateDrawProperties(root.get());
6988 EXPECT_TRUE(root->render_surface());
6989 EXPECT_TRUE(render_surface1->render_surface());
6990 EXPECT_TRUE(render_surface2->render_surface());
6992 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
6993 render_surface1->clip_rect().ToString());
6994 EXPECT_TRUE(render_surface1->is_clipped());
6996 // The render surface should not clip (it has unclipped descendants), instead
6997 // it should rely on layer clipping.
6998 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6999 render_surface1->render_surface()->clip_rect().ToString());
7000 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
7002 // That said, it should have grown to accomodate the unclipped descendant.
7003 EXPECT_EQ(gfx::Rect(-1, 1, 6, 4).ToString(),
7004 render_surface1->render_surface()->content_rect().ToString());
7006 // This render surface should clip. It has no unclipped descendants.
7007 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7008 render_surface2->clip_rect().ToString());
7009 EXPECT_TRUE(render_surface2->render_surface()->is_clipped());
7011 // It also shouldn't have grown to accomodate the clip child.
7012 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7013 render_surface2->render_surface()->content_rect().ToString());
7015 // Sanity check our num_unclipped_descendants values.
7016 EXPECT_EQ(1, render_surface1->num_unclipped_descendants());
7017 EXPECT_EQ(0, render_surface2->num_unclipped_descendants());
7020 TEST_F(LayerTreeHostCommonTest, CanRenderToSeparateSurface) {
7021 FakeImplProxy proxy;
7022 TestSharedBitmapManager shared_bitmap_manager;
7023 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7024 scoped_ptr<LayerImpl> root =
7025 LayerImpl::Create(host_impl.active_tree(), 12345);
7026 scoped_ptr<LayerImpl> child1 =
7027 LayerImpl::Create(host_impl.active_tree(), 123456);
7028 scoped_ptr<LayerImpl> child2 =
7029 LayerImpl::Create(host_impl.active_tree(), 1234567);
7030 scoped_ptr<LayerImpl> child3 =
7031 LayerImpl::Create(host_impl.active_tree(), 12345678);
7033 gfx::Transform identity_matrix;
7034 gfx::Point3F transform_origin;
7035 gfx::PointF position;
7036 gfx::Size bounds(100, 100);
7037 SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
7038 position, bounds, true, false, true);
7039 root->SetDrawsContent(true);
7041 // This layer structure normally forces render surface due to preserves3d
7042 // behavior.
7043 SetLayerPropertiesForTesting(child1.get(), identity_matrix, transform_origin,
7044 position, bounds, false, true, true);
7045 child1->SetDrawsContent(true);
7046 SetLayerPropertiesForTesting(child2.get(), identity_matrix, transform_origin,
7047 position, bounds, true, false, false);
7048 child2->SetDrawsContent(true);
7049 SetLayerPropertiesForTesting(child3.get(), identity_matrix, transform_origin,
7050 position, bounds, true, false, false);
7051 child3->SetDrawsContent(true);
7053 child2->Set3dSortingContextId(1);
7054 child3->Set3dSortingContextId(1);
7056 child2->AddChild(child3.Pass());
7057 child1->AddChild(child2.Pass());
7058 root->AddChild(child1.Pass());
7061 LayerImplList render_surface_layer_list;
7062 FakeLayerTreeHostImpl::RecursiveUpdateNumChildren(root.get());
7063 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7064 root.get(), root->bounds(), &render_surface_layer_list);
7065 inputs.can_render_to_separate_surface = true;
7066 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7068 EXPECT_EQ(2u, render_surface_layer_list.size());
7070 int count_represents_target_render_surface = 0;
7071 int count_represents_contributing_render_surface = 0;
7072 int count_represents_itself = 0;
7073 auto end = LayerIterator<LayerImpl>::End(&render_surface_layer_list);
7074 for (auto it = LayerIterator<LayerImpl>::Begin(&render_surface_layer_list);
7075 it != end; ++it) {
7076 if (it.represents_target_render_surface())
7077 count_represents_target_render_surface++;
7078 if (it.represents_contributing_render_surface())
7079 count_represents_contributing_render_surface++;
7080 if (it.represents_itself())
7081 count_represents_itself++;
7084 // Two render surfaces.
7085 EXPECT_EQ(2, count_represents_target_render_surface);
7086 // Second render surface contributes to root render surface.
7087 EXPECT_EQ(1, count_represents_contributing_render_surface);
7088 // All 4 layers represent itself.
7089 EXPECT_EQ(4, count_represents_itself);
7093 LayerImplList render_surface_layer_list;
7094 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7095 root.get(), root->bounds(), &render_surface_layer_list);
7096 inputs.can_render_to_separate_surface = false;
7097 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7099 EXPECT_EQ(1u, render_surface_layer_list.size());
7101 int count_represents_target_render_surface = 0;
7102 int count_represents_contributing_render_surface = 0;
7103 int count_represents_itself = 0;
7104 auto end = LayerIterator<LayerImpl>::End(&render_surface_layer_list);
7105 for (auto it = LayerIterator<LayerImpl>::Begin(&render_surface_layer_list);
7106 it != end; ++it) {
7107 if (it.represents_target_render_surface())
7108 count_represents_target_render_surface++;
7109 if (it.represents_contributing_render_surface())
7110 count_represents_contributing_render_surface++;
7111 if (it.represents_itself())
7112 count_represents_itself++;
7115 // Only root layer has a render surface.
7116 EXPECT_EQ(1, count_represents_target_render_surface);
7117 // No layer contributes a render surface to root render surface.
7118 EXPECT_EQ(0, count_represents_contributing_render_surface);
7119 // All 4 layers represent itself.
7120 EXPECT_EQ(4, count_represents_itself);
7124 TEST_F(LayerTreeHostCommonTest, DoNotIncludeBackfaceInvisibleSurfaces) {
7125 scoped_refptr<Layer> root = Layer::Create();
7126 scoped_refptr<Layer> render_surface = Layer::Create();
7127 scoped_refptr<LayerWithForcedDrawsContent> child =
7128 make_scoped_refptr(new LayerWithForcedDrawsContent);
7130 root->AddChild(render_surface);
7131 render_surface->AddChild(child);
7133 gfx::Transform identity_transform;
7134 SetLayerPropertiesForTesting(root.get(),
7135 identity_transform,
7136 gfx::Point3F(),
7137 gfx::PointF(),
7138 gfx::Size(50, 50),
7139 true,
7140 false);
7141 SetLayerPropertiesForTesting(render_surface.get(),
7142 identity_transform,
7143 gfx::Point3F(),
7144 gfx::PointF(),
7145 gfx::Size(30, 30),
7146 false,
7147 true);
7148 SetLayerPropertiesForTesting(child.get(),
7149 identity_transform,
7150 gfx::Point3F(),
7151 gfx::PointF(),
7152 gfx::Size(20, 20),
7153 true,
7154 false);
7156 root->SetShouldFlattenTransform(false);
7157 root->Set3dSortingContextId(1);
7158 render_surface->SetDoubleSided(false);
7159 render_surface->SetForceRenderSurface(true);
7161 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7162 host->SetRootLayer(root);
7164 ExecuteCalculateDrawProperties(root.get());
7166 EXPECT_EQ(2u, render_surface_layer_list()->size());
7167 EXPECT_EQ(1u,
7168 render_surface_layer_list()->at(0)
7169 ->render_surface()->layer_list().size());
7170 EXPECT_EQ(1u,
7171 render_surface_layer_list()->at(1)
7172 ->render_surface()->layer_list().size());
7174 gfx::Transform rotation_transform = identity_transform;
7175 rotation_transform.RotateAboutXAxis(180.0);
7177 render_surface->SetTransform(rotation_transform);
7179 ExecuteCalculateDrawProperties(root.get());
7181 EXPECT_EQ(1u, render_surface_layer_list()->size());
7182 EXPECT_EQ(0u,
7183 render_surface_layer_list()->at(0)
7184 ->render_surface()->layer_list().size());
7187 TEST_F(LayerTreeHostCommonTest, ClippedByScrollParent) {
7188 // Checks that the simple case (being clipped by a scroll parent that would
7189 // have been processed before you anyhow) results in the right clips.
7191 // + root
7192 // + scroll_parent_border
7193 // | + scroll_parent_clip
7194 // | + scroll_parent
7195 // + scroll_child
7197 scoped_refptr<Layer> root = Layer::Create();
7198 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7199 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7200 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7201 make_scoped_refptr(new LayerWithForcedDrawsContent);
7202 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7203 make_scoped_refptr(new LayerWithForcedDrawsContent);
7205 root->AddChild(scroll_child);
7207 root->AddChild(scroll_parent_border);
7208 scroll_parent_border->AddChild(scroll_parent_clip);
7209 scroll_parent_clip->AddChild(scroll_parent);
7211 scroll_parent_clip->SetMasksToBounds(true);
7213 scroll_child->SetScrollParent(scroll_parent.get());
7215 gfx::Transform identity_transform;
7216 SetLayerPropertiesForTesting(root.get(),
7217 identity_transform,
7218 gfx::Point3F(),
7219 gfx::PointF(),
7220 gfx::Size(50, 50),
7221 true,
7222 false);
7223 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7224 identity_transform,
7225 gfx::Point3F(),
7226 gfx::PointF(),
7227 gfx::Size(40, 40),
7228 true,
7229 false);
7230 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7231 identity_transform,
7232 gfx::Point3F(),
7233 gfx::PointF(),
7234 gfx::Size(30, 30),
7235 true,
7236 false);
7237 SetLayerPropertiesForTesting(scroll_parent.get(),
7238 identity_transform,
7239 gfx::Point3F(),
7240 gfx::PointF(),
7241 gfx::Size(50, 50),
7242 true,
7243 false);
7244 SetLayerPropertiesForTesting(scroll_child.get(),
7245 identity_transform,
7246 gfx::Point3F(),
7247 gfx::PointF(),
7248 gfx::Size(50, 50),
7249 true,
7250 false);
7252 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7253 host->SetRootLayer(root);
7255 ExecuteCalculateDrawProperties(root.get());
7257 EXPECT_TRUE(root->render_surface());
7259 EXPECT_EQ(gfx::Rect(0, 0, 30, 30).ToString(),
7260 scroll_child->clip_rect().ToString());
7261 EXPECT_TRUE(scroll_child->is_clipped());
7264 TEST_F(LayerTreeHostCommonTest, SingularTransformSubtreesDoNotDraw) {
7265 scoped_refptr<LayerWithForcedDrawsContent> root =
7266 make_scoped_refptr(new LayerWithForcedDrawsContent);
7267 scoped_refptr<LayerWithForcedDrawsContent> parent =
7268 make_scoped_refptr(new LayerWithForcedDrawsContent);
7269 scoped_refptr<LayerWithForcedDrawsContent> child =
7270 make_scoped_refptr(new LayerWithForcedDrawsContent);
7272 root->AddChild(parent);
7273 parent->AddChild(child);
7275 gfx::Transform identity_transform;
7276 SetLayerPropertiesForTesting(root.get(),
7277 identity_transform,
7278 gfx::Point3F(),
7279 gfx::PointF(),
7280 gfx::Size(50, 50),
7281 true,
7282 true);
7283 root->SetForceRenderSurface(true);
7284 SetLayerPropertiesForTesting(parent.get(),
7285 identity_transform,
7286 gfx::Point3F(),
7287 gfx::PointF(),
7288 gfx::Size(30, 30),
7289 true,
7290 true);
7291 parent->SetForceRenderSurface(true);
7292 SetLayerPropertiesForTesting(child.get(),
7293 identity_transform,
7294 gfx::Point3F(),
7295 gfx::PointF(),
7296 gfx::Size(20, 20),
7297 true,
7298 true);
7299 child->SetForceRenderSurface(true);
7301 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7302 host->SetRootLayer(root);
7304 ExecuteCalculateDrawProperties(root.get());
7306 EXPECT_EQ(3u, render_surface_layer_list()->size());
7308 gfx::Transform singular_transform;
7309 singular_transform.Scale3d(
7310 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
7312 child->SetTransform(singular_transform);
7314 ExecuteCalculateDrawProperties(root.get());
7316 EXPECT_EQ(2u, render_surface_layer_list()->size());
7318 // Ensure that the entire subtree under a layer with singular transform does
7319 // not get rendered.
7320 parent->SetTransform(singular_transform);
7321 child->SetTransform(identity_transform);
7323 ExecuteCalculateDrawProperties(root.get());
7325 EXPECT_EQ(1u, render_surface_layer_list()->size());
7328 TEST_F(LayerTreeHostCommonTest, ClippedByOutOfOrderScrollParent) {
7329 // Checks that clipping by a scroll parent that follows you in paint order
7330 // still results in correct clipping.
7332 // + root
7333 // + scroll_child
7334 // + scroll_parent_border
7335 // + scroll_parent_clip
7336 // + scroll_parent
7338 scoped_refptr<Layer> root = Layer::Create();
7339 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7340 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7341 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7342 make_scoped_refptr(new LayerWithForcedDrawsContent);
7343 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7344 make_scoped_refptr(new LayerWithForcedDrawsContent);
7346 root->AddChild(scroll_parent_border);
7347 scroll_parent_border->AddChild(scroll_parent_clip);
7348 scroll_parent_clip->AddChild(scroll_parent);
7350 root->AddChild(scroll_child);
7352 scroll_parent_clip->SetMasksToBounds(true);
7354 scroll_child->SetScrollParent(scroll_parent.get());
7356 gfx::Transform identity_transform;
7357 SetLayerPropertiesForTesting(root.get(),
7358 identity_transform,
7359 gfx::Point3F(),
7360 gfx::PointF(),
7361 gfx::Size(50, 50),
7362 true,
7363 false);
7364 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7365 identity_transform,
7366 gfx::Point3F(),
7367 gfx::PointF(),
7368 gfx::Size(40, 40),
7369 true,
7370 false);
7371 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7372 identity_transform,
7373 gfx::Point3F(),
7374 gfx::PointF(),
7375 gfx::Size(30, 30),
7376 true,
7377 false);
7378 SetLayerPropertiesForTesting(scroll_parent.get(),
7379 identity_transform,
7380 gfx::Point3F(),
7381 gfx::PointF(),
7382 gfx::Size(50, 50),
7383 true,
7384 false);
7385 SetLayerPropertiesForTesting(scroll_child.get(),
7386 identity_transform,
7387 gfx::Point3F(),
7388 gfx::PointF(),
7389 gfx::Size(50, 50),
7390 true,
7391 false);
7393 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7394 host->SetRootLayer(root);
7396 ExecuteCalculateDrawProperties(root.get());
7398 EXPECT_TRUE(root->render_surface());
7400 EXPECT_EQ(gfx::Rect(0, 0, 30, 30).ToString(),
7401 scroll_child->clip_rect().ToString());
7402 EXPECT_TRUE(scroll_child->is_clipped());
7405 TEST_F(LayerTreeHostCommonTest, ClippedByOutOfOrderScrollGrandparent) {
7406 // Checks that clipping by a scroll parent and scroll grandparent that follow
7407 // you in paint order still results in correct clipping.
7409 // + root
7410 // + scroll_child
7411 // + scroll_parent_border
7412 // | + scroll_parent_clip
7413 // | + scroll_parent
7414 // + scroll_grandparent_border
7415 // + scroll_grandparent_clip
7416 // + scroll_grandparent
7418 scoped_refptr<Layer> root = Layer::Create();
7419 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7420 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7421 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7422 make_scoped_refptr(new LayerWithForcedDrawsContent);
7424 scoped_refptr<Layer> scroll_grandparent_border = Layer::Create();
7425 scoped_refptr<Layer> scroll_grandparent_clip = Layer::Create();
7426 scoped_refptr<LayerWithForcedDrawsContent> scroll_grandparent =
7427 make_scoped_refptr(new LayerWithForcedDrawsContent);
7429 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7430 make_scoped_refptr(new LayerWithForcedDrawsContent);
7432 root->AddChild(scroll_child);
7434 root->AddChild(scroll_parent_border);
7435 scroll_parent_border->AddChild(scroll_parent_clip);
7436 scroll_parent_clip->AddChild(scroll_parent);
7438 root->AddChild(scroll_grandparent_border);
7439 scroll_grandparent_border->AddChild(scroll_grandparent_clip);
7440 scroll_grandparent_clip->AddChild(scroll_grandparent);
7442 scroll_parent_clip->SetMasksToBounds(true);
7443 scroll_grandparent_clip->SetMasksToBounds(true);
7445 scroll_child->SetScrollParent(scroll_parent.get());
7446 scroll_parent_border->SetScrollParent(scroll_grandparent.get());
7448 gfx::Transform identity_transform;
7449 SetLayerPropertiesForTesting(root.get(),
7450 identity_transform,
7451 gfx::Point3F(),
7452 gfx::PointF(),
7453 gfx::Size(50, 50),
7454 true,
7455 false);
7456 SetLayerPropertiesForTesting(scroll_grandparent_border.get(),
7457 identity_transform,
7458 gfx::Point3F(),
7459 gfx::PointF(),
7460 gfx::Size(40, 40),
7461 true,
7462 false);
7463 SetLayerPropertiesForTesting(scroll_grandparent_clip.get(),
7464 identity_transform,
7465 gfx::Point3F(),
7466 gfx::PointF(),
7467 gfx::Size(20, 20),
7468 true,
7469 false);
7470 SetLayerPropertiesForTesting(scroll_grandparent.get(),
7471 identity_transform,
7472 gfx::Point3F(),
7473 gfx::PointF(),
7474 gfx::Size(50, 50),
7475 true,
7476 false);
7477 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7478 identity_transform,
7479 gfx::Point3F(),
7480 gfx::PointF(),
7481 gfx::Size(40, 40),
7482 true,
7483 false);
7484 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7485 identity_transform,
7486 gfx::Point3F(),
7487 gfx::PointF(),
7488 gfx::Size(30, 30),
7489 true,
7490 false);
7491 SetLayerPropertiesForTesting(scroll_parent.get(),
7492 identity_transform,
7493 gfx::Point3F(),
7494 gfx::PointF(),
7495 gfx::Size(50, 50),
7496 true,
7497 false);
7498 SetLayerPropertiesForTesting(scroll_child.get(),
7499 identity_transform,
7500 gfx::Point3F(),
7501 gfx::PointF(),
7502 gfx::Size(50, 50),
7503 true,
7504 false);
7506 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7507 host->SetRootLayer(root);
7509 ExecuteCalculateDrawProperties(root.get());
7511 EXPECT_TRUE(root->render_surface());
7513 EXPECT_EQ(gfx::Rect(0, 0, 20, 20).ToString(),
7514 scroll_child->clip_rect().ToString());
7515 EXPECT_TRUE(scroll_child->is_clipped());
7517 // Despite the fact that we visited the above layers out of order to get the
7518 // correct clip, the layer lists should be unaffected.
7519 EXPECT_EQ(3u, root->render_surface()->layer_list().size());
7520 EXPECT_EQ(scroll_child.get(),
7521 root->render_surface()->layer_list().at(0).get());
7522 EXPECT_EQ(scroll_parent.get(),
7523 root->render_surface()->layer_list().at(1).get());
7524 EXPECT_EQ(scroll_grandparent.get(),
7525 root->render_surface()->layer_list().at(2).get());
7528 TEST_F(LayerTreeHostCommonTest, OutOfOrderClippingRequiresRSLLSorting) {
7529 // Ensures that even if we visit layers out of order, we still produce a
7530 // correctly ordered render surface layer list.
7531 // + root
7532 // + scroll_child
7533 // + scroll_parent_border
7534 // + scroll_parent_clip
7535 // + scroll_parent
7536 // + render_surface1
7537 // + scroll_grandparent_border
7538 // + scroll_grandparent_clip
7539 // + scroll_grandparent
7540 // + render_surface2
7542 scoped_refptr<LayerWithForcedDrawsContent> root =
7543 make_scoped_refptr(new LayerWithForcedDrawsContent);
7545 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7546 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7547 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7548 make_scoped_refptr(new LayerWithForcedDrawsContent);
7549 scoped_refptr<LayerWithForcedDrawsContent> render_surface1 =
7550 make_scoped_refptr(new LayerWithForcedDrawsContent);
7552 scoped_refptr<Layer> scroll_grandparent_border = Layer::Create();
7553 scoped_refptr<Layer> scroll_grandparent_clip = Layer::Create();
7554 scoped_refptr<LayerWithForcedDrawsContent> scroll_grandparent =
7555 make_scoped_refptr(new LayerWithForcedDrawsContent);
7556 scoped_refptr<LayerWithForcedDrawsContent> render_surface2 =
7557 make_scoped_refptr(new LayerWithForcedDrawsContent);
7559 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7560 make_scoped_refptr(new LayerWithForcedDrawsContent);
7562 root->AddChild(scroll_child);
7564 root->AddChild(scroll_parent_border);
7565 scroll_parent_border->AddChild(scroll_parent_clip);
7566 scroll_parent_clip->AddChild(scroll_parent);
7567 scroll_parent->AddChild(render_surface2);
7569 root->AddChild(scroll_grandparent_border);
7570 scroll_grandparent_border->AddChild(scroll_grandparent_clip);
7571 scroll_grandparent_clip->AddChild(scroll_grandparent);
7572 scroll_grandparent->AddChild(render_surface1);
7574 scroll_parent_clip->SetMasksToBounds(true);
7575 scroll_grandparent_clip->SetMasksToBounds(true);
7577 scroll_child->SetScrollParent(scroll_parent.get());
7578 scroll_parent_border->SetScrollParent(scroll_grandparent.get());
7580 render_surface1->SetForceRenderSurface(true);
7581 render_surface2->SetForceRenderSurface(true);
7583 gfx::Transform identity_transform;
7584 SetLayerPropertiesForTesting(root.get(),
7585 identity_transform,
7586 gfx::Point3F(),
7587 gfx::PointF(),
7588 gfx::Size(50, 50),
7589 true,
7590 false);
7591 SetLayerPropertiesForTesting(scroll_grandparent_border.get(),
7592 identity_transform,
7593 gfx::Point3F(),
7594 gfx::PointF(),
7595 gfx::Size(40, 40),
7596 true,
7597 false);
7598 SetLayerPropertiesForTesting(scroll_grandparent_clip.get(),
7599 identity_transform,
7600 gfx::Point3F(),
7601 gfx::PointF(),
7602 gfx::Size(20, 20),
7603 true,
7604 false);
7605 SetLayerPropertiesForTesting(scroll_grandparent.get(),
7606 identity_transform,
7607 gfx::Point3F(),
7608 gfx::PointF(),
7609 gfx::Size(50, 50),
7610 true,
7611 false);
7612 SetLayerPropertiesForTesting(render_surface1.get(),
7613 identity_transform,
7614 gfx::Point3F(),
7615 gfx::PointF(),
7616 gfx::Size(50, 50),
7617 true,
7618 false);
7619 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7620 identity_transform,
7621 gfx::Point3F(),
7622 gfx::PointF(),
7623 gfx::Size(40, 40),
7624 true,
7625 false);
7626 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7627 identity_transform,
7628 gfx::Point3F(),
7629 gfx::PointF(),
7630 gfx::Size(30, 30),
7631 true,
7632 false);
7633 SetLayerPropertiesForTesting(scroll_parent.get(),
7634 identity_transform,
7635 gfx::Point3F(),
7636 gfx::PointF(),
7637 gfx::Size(50, 50),
7638 true,
7639 false);
7640 SetLayerPropertiesForTesting(render_surface2.get(),
7641 identity_transform,
7642 gfx::Point3F(),
7643 gfx::PointF(),
7644 gfx::Size(50, 50),
7645 true,
7646 false);
7647 SetLayerPropertiesForTesting(scroll_child.get(),
7648 identity_transform,
7649 gfx::Point3F(),
7650 gfx::PointF(),
7651 gfx::Size(50, 50),
7652 true,
7653 false);
7655 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7656 host->SetRootLayer(root);
7658 RenderSurfaceLayerList render_surface_layer_list;
7659 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
7660 root.get(),
7661 root->bounds(),
7662 identity_transform,
7663 &render_surface_layer_list);
7665 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7667 EXPECT_TRUE(root->render_surface());
7669 EXPECT_EQ(gfx::Rect(0, 0, 20, 20).ToString(),
7670 scroll_child->clip_rect().ToString());
7671 EXPECT_TRUE(scroll_child->is_clipped());
7673 // Despite the fact that we had to process the layers out of order to get the
7674 // right clip, our render_surface_layer_list's order should be unaffected.
7675 EXPECT_EQ(3u, render_surface_layer_list.size());
7676 EXPECT_EQ(root.get(), render_surface_layer_list.at(0));
7677 EXPECT_EQ(render_surface2.get(), render_surface_layer_list.at(1));
7678 EXPECT_EQ(render_surface1.get(), render_surface_layer_list.at(2));
7679 EXPECT_TRUE(render_surface_layer_list.at(0)->render_surface());
7680 EXPECT_TRUE(render_surface_layer_list.at(1)->render_surface());
7681 EXPECT_TRUE(render_surface_layer_list.at(2)->render_surface());
7684 TEST_F(LayerTreeHostCommonTest, FixedPositionWithInterveningRenderSurface) {
7685 // Ensures that when we have a render surface between a fixed position layer
7686 // and its container, we compute the fixed position layer's draw transform
7687 // with respect to that intervening render surface, not with respect to its
7688 // container's render target.
7690 // + root
7691 // + render_surface
7692 // + fixed
7694 scoped_refptr<Layer> root = Layer::Create();
7695 scoped_refptr<LayerWithForcedDrawsContent> render_surface =
7696 make_scoped_refptr(new LayerWithForcedDrawsContent());
7697 scoped_refptr<LayerWithForcedDrawsContent> fixed =
7698 make_scoped_refptr(new LayerWithForcedDrawsContent());
7700 root->AddChild(render_surface);
7701 render_surface->AddChild(fixed);
7703 root->SetIsContainerForFixedPositionLayers(true);
7704 render_surface->SetForceRenderSurface(true);
7706 LayerPositionConstraint constraint;
7707 constraint.set_is_fixed_position(true);
7708 fixed->SetPositionConstraint(constraint);
7710 SetLayerPropertiesForTesting(root.get(), gfx::Transform(), gfx::Point3F(),
7711 gfx::PointF(), gfx::Size(50, 50), true, false);
7712 SetLayerPropertiesForTesting(render_surface.get(), gfx::Transform(),
7713 gfx::Point3F(), gfx::PointF(7.f, 9.f),
7714 gfx::Size(50, 50), true, false);
7715 SetLayerPropertiesForTesting(fixed.get(), gfx::Transform(), gfx::Point3F(),
7716 gfx::PointF(10.f, 15.f), gfx::Size(50, 50), true,
7717 false);
7719 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7720 host->SetRootLayer(root);
7722 ExecuteCalculateDrawProperties(root.get());
7724 gfx::Transform expected_draw_transform;
7725 expected_draw_transform.Translate(10.f, 15.f);
7726 EXPECT_EQ(expected_draw_transform, fixed->draw_transform());
7728 gfx::Transform expected_screen_space_transform;
7729 expected_screen_space_transform.Translate(17.f, 24.f);
7730 EXPECT_EQ(expected_screen_space_transform, fixed->screen_space_transform());
7733 TEST_F(LayerTreeHostCommonTest, ScrollCompensationWithRounding) {
7734 // This test verifies that a scrolling layer that gets snapped to
7735 // integer coordinates doesn't move a fixed position child.
7737 // + root
7738 // + container
7739 // + scroller
7740 // + fixed
7742 FakeImplProxy proxy;
7743 TestSharedBitmapManager shared_bitmap_manager;
7744 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7745 host_impl.CreatePendingTree();
7746 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
7747 scoped_ptr<LayerImpl> container =
7748 LayerImpl::Create(host_impl.active_tree(), 2);
7749 LayerImpl* container_layer = container.get();
7750 scoped_ptr<LayerImpl> scroller =
7751 LayerImpl::Create(host_impl.active_tree(), 3);
7752 LayerImpl* scroll_layer = scroller.get();
7753 scoped_ptr<LayerImpl> fixed = LayerImpl::Create(host_impl.active_tree(), 4);
7754 LayerImpl* fixed_layer = fixed.get();
7756 container->SetIsContainerForFixedPositionLayers(true);
7758 LayerPositionConstraint constraint;
7759 constraint.set_is_fixed_position(true);
7760 fixed->SetPositionConstraint(constraint);
7762 scroller->SetScrollClipLayer(container->id());
7764 gfx::Transform identity_transform;
7765 gfx::Transform container_transform;
7766 container_transform.Translate3d(10.0, 20.0, 0.0);
7767 gfx::Vector2dF container_offset = container_transform.To2dTranslation();
7769 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7770 gfx::PointF(), gfx::Size(50, 50), true, false,
7771 true);
7772 SetLayerPropertiesForTesting(container.get(), container_transform,
7773 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
7774 true, false, false);
7775 SetLayerPropertiesForTesting(scroller.get(), identity_transform,
7776 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
7777 true, false, false);
7778 SetLayerPropertiesForTesting(fixed.get(), identity_transform, gfx::Point3F(),
7779 gfx::PointF(), gfx::Size(50, 50), true, false,
7780 false);
7782 scroller->AddChild(fixed.Pass());
7783 container->AddChild(scroller.Pass());
7784 root->AddChild(container.Pass());
7786 // Rounded to integers already.
7788 gfx::Vector2dF scroll_delta(3.0, 5.0);
7789 scroll_layer->SetScrollDelta(scroll_delta);
7791 LayerImplList render_surface_layer_list;
7792 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7793 root.get(), root->bounds(), &render_surface_layer_list);
7794 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7796 EXPECT_TRANSFORMATION_MATRIX_EQ(
7797 container_layer->draw_properties().screen_space_transform,
7798 fixed_layer->draw_properties().screen_space_transform);
7799 EXPECT_VECTOR_EQ(
7800 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7801 container_offset);
7802 EXPECT_VECTOR_EQ(scroll_layer->draw_properties()
7803 .screen_space_transform.To2dTranslation(),
7804 container_offset - scroll_delta);
7807 // Scroll delta requiring rounding.
7809 gfx::Vector2dF scroll_delta(4.1f, 8.1f);
7810 scroll_layer->SetScrollDelta(scroll_delta);
7812 gfx::Vector2dF rounded_scroll_delta(4.f, 8.f);
7814 LayerImplList render_surface_layer_list;
7815 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7816 root.get(), root->bounds(), &render_surface_layer_list);
7817 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7819 EXPECT_TRANSFORMATION_MATRIX_EQ(
7820 container_layer->draw_properties().screen_space_transform,
7821 fixed_layer->draw_properties().screen_space_transform);
7822 EXPECT_VECTOR_EQ(
7823 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7824 container_offset);
7825 EXPECT_VECTOR_EQ(scroll_layer->draw_properties()
7826 .screen_space_transform.To2dTranslation(),
7827 container_offset - rounded_scroll_delta);
7830 // Scale is applied earlier in the tree.
7832 gfx::Transform scaled_container_transform = container_transform;
7833 scaled_container_transform.Scale3d(3.0, 3.0, 1.0);
7834 container_layer->SetTransform(scaled_container_transform);
7836 gfx::Vector2dF scroll_delta(4.5f, 8.5f);
7837 scroll_layer->SetScrollDelta(scroll_delta);
7839 LayerImplList render_surface_layer_list;
7840 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7841 root.get(), root->bounds(), &render_surface_layer_list);
7842 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7844 EXPECT_TRANSFORMATION_MATRIX_EQ(
7845 container_layer->draw_properties().screen_space_transform,
7846 fixed_layer->draw_properties().screen_space_transform);
7847 EXPECT_VECTOR_EQ(
7848 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7849 container_offset);
7851 container_layer->SetTransform(container_transform);
7854 // Scale is applied on the scroll layer itself.
7856 gfx::Transform scale_transform;
7857 scale_transform.Scale3d(3.0, 3.0, 1.0);
7858 scroll_layer->SetTransform(scale_transform);
7860 gfx::Vector2dF scroll_delta(4.5f, 8.5f);
7861 scroll_layer->SetScrollDelta(scroll_delta);
7863 LayerImplList render_surface_layer_list;
7864 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7865 root.get(), root->bounds(), &render_surface_layer_list);
7866 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7868 EXPECT_VECTOR_EQ(
7869 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7870 container_offset);
7872 scroll_layer->SetTransform(identity_transform);
7876 TEST_F(LayerTreeHostCommonTest,
7877 ScrollCompensationMainScrollOffsetFractionalPart) {
7878 // This test verifies that a scrolling layer that has fractional scroll offset
7879 // from main doesn't move a fixed position child.
7881 // + root
7882 // + container
7883 // + scroller
7884 // + fixed
7886 FakeImplProxy proxy;
7887 TestSharedBitmapManager shared_bitmap_manager;
7888 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7889 host_impl.CreatePendingTree();
7890 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
7891 scoped_ptr<LayerImpl> container =
7892 LayerImpl::Create(host_impl.active_tree(), 2);
7893 LayerImpl* container_layer = container.get();
7894 scoped_ptr<LayerImpl> scroller =
7895 LayerImpl::Create(host_impl.active_tree(), 3);
7896 LayerImpl* scroll_layer = scroller.get();
7897 scoped_ptr<LayerImpl> fixed = LayerImpl::Create(host_impl.active_tree(), 4);
7898 LayerImpl* fixed_layer = fixed.get();
7900 container->SetIsContainerForFixedPositionLayers(true);
7902 LayerPositionConstraint constraint;
7903 constraint.set_is_fixed_position(true);
7904 fixed->SetPositionConstraint(constraint);
7906 scroller->SetScrollClipLayer(container->id());
7908 gfx::Transform identity_transform;
7909 gfx::Transform container_transform;
7910 container_transform.Translate3d(10.0, 20.0, 0.0);
7911 gfx::Vector2dF container_offset = container_transform.To2dTranslation();
7913 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7914 gfx::PointF(), gfx::Size(50, 50), true, false,
7915 true);
7916 SetLayerPropertiesForTesting(container.get(), container_transform,
7917 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
7918 true, false, false);
7919 SetLayerPropertiesForTesting(scroller.get(), identity_transform,
7920 gfx::Point3F(), gfx::PointF(0.0, 0.0),
7921 gfx::Size(30, 30), true, false, false);
7923 gfx::ScrollOffset scroll_offset(3.3, 4.2);
7924 gfx::Vector2dF main_scroll_fractional_part(0.3f, 0.2f);
7925 gfx::Vector2dF scroll_delta(0.1f, 0.4f);
7926 // Blink only uses the integer part of the scroll_offset for fixed
7927 // position layer.
7928 SetLayerPropertiesForTesting(fixed.get(), identity_transform, gfx::Point3F(),
7929 gfx::PointF(3.0f, 4.0f), gfx::Size(50, 50), true,
7930 false, false);
7931 scroll_layer->PushScrollOffsetFromMainThread(scroll_offset);
7932 scroll_layer->SetScrollDelta(scroll_delta);
7933 scroll_layer->SetScrollCompensationAdjustment(main_scroll_fractional_part);
7935 scroller->AddChild(fixed.Pass());
7936 container->AddChild(scroller.Pass());
7937 root->AddChild(container.Pass());
7939 LayerImplList render_surface_layer_list;
7940 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7941 root.get(), root->bounds(), &render_surface_layer_list);
7942 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7944 EXPECT_TRANSFORMATION_MATRIX_EQ(
7945 container_layer->draw_properties().screen_space_transform,
7946 fixed_layer->draw_properties().screen_space_transform);
7947 EXPECT_VECTOR_EQ(
7948 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7949 container_offset);
7951 gfx::ScrollOffset effective_scroll_offset =
7952 ScrollOffsetWithDelta(scroll_offset, scroll_delta);
7953 gfx::Vector2d rounded_effective_scroll_offset =
7954 ToRoundedVector2d(ScrollOffsetToVector2dF(effective_scroll_offset));
7955 EXPECT_VECTOR_EQ(
7956 scroll_layer->draw_properties().screen_space_transform.To2dTranslation(),
7957 container_offset - rounded_effective_scroll_offset);
7960 class AnimationScaleFactorTrackingLayerImpl : public LayerImpl {
7961 public:
7962 static scoped_ptr<AnimationScaleFactorTrackingLayerImpl> Create(
7963 LayerTreeImpl* tree_impl,
7964 int id) {
7965 return make_scoped_ptr(
7966 new AnimationScaleFactorTrackingLayerImpl(tree_impl, id));
7969 ~AnimationScaleFactorTrackingLayerImpl() override {}
7971 private:
7972 explicit AnimationScaleFactorTrackingLayerImpl(LayerTreeImpl* tree_impl,
7973 int id)
7974 : LayerImpl(tree_impl, id) {
7975 SetDrawsContent(true);
7979 TEST_F(LayerTreeHostCommonTest, MaximumAnimationScaleFactor) {
7980 FakeImplProxy proxy;
7981 TestSharedBitmapManager shared_bitmap_manager;
7982 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7983 gfx::Transform identity_matrix;
7984 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> grand_parent =
7985 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 1);
7986 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> parent =
7987 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 2);
7988 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> child =
7989 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 3);
7990 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> grand_child =
7991 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 4);
7993 AnimationScaleFactorTrackingLayerImpl* parent_raw = parent.get();
7994 AnimationScaleFactorTrackingLayerImpl* child_raw = child.get();
7995 AnimationScaleFactorTrackingLayerImpl* grand_child_raw = grand_child.get();
7997 child->AddChild(grand_child.Pass());
7998 parent->AddChild(child.Pass());
7999 grand_parent->AddChild(parent.Pass());
8001 SetLayerPropertiesForTesting(grand_parent.get(), identity_matrix,
8002 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8003 true, false, true);
8004 SetLayerPropertiesForTesting(parent_raw, identity_matrix, gfx::Point3F(),
8005 gfx::PointF(), gfx::Size(1, 2), true, false,
8006 false);
8007 SetLayerPropertiesForTesting(child_raw, identity_matrix, gfx::Point3F(),
8008 gfx::PointF(), gfx::Size(1, 2), true, false,
8009 false);
8011 SetLayerPropertiesForTesting(grand_child_raw, identity_matrix, gfx::Point3F(),
8012 gfx::PointF(), gfx::Size(1, 2), true, false,
8013 false);
8015 ExecuteCalculateDrawProperties(grand_parent.get());
8017 // No layers have animations.
8018 EXPECT_EQ(0.f,
8019 grand_parent->draw_properties().maximum_animation_contents_scale);
8020 EXPECT_EQ(0.f,
8021 parent_raw->draw_properties().maximum_animation_contents_scale);
8022 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8023 EXPECT_EQ(
8024 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8026 TransformOperations translation;
8027 translation.AppendTranslate(1.f, 2.f, 3.f);
8029 AddAnimatedTransformToLayer(
8030 parent_raw, 1.0, TransformOperations(), translation);
8032 // No layers have scale-affecting animations.
8033 EXPECT_EQ(0.f,
8034 grand_parent->draw_properties().maximum_animation_contents_scale);
8035 EXPECT_EQ(0.f,
8036 parent_raw->draw_properties().maximum_animation_contents_scale);
8037 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8038 EXPECT_EQ(
8039 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8041 TransformOperations scale;
8042 scale.AppendScale(5.f, 4.f, 3.f);
8044 AddAnimatedTransformToLayer(child_raw, 1.0, TransformOperations(), scale);
8045 ExecuteCalculateDrawProperties(grand_parent.get());
8047 // Only |child| has a scale-affecting animation.
8048 EXPECT_EQ(0.f,
8049 grand_parent->draw_properties().maximum_animation_contents_scale);
8050 EXPECT_EQ(0.f,
8051 parent_raw->draw_properties().maximum_animation_contents_scale);
8052 EXPECT_EQ(5.f, child_raw->draw_properties().maximum_animation_contents_scale);
8053 EXPECT_EQ(
8054 5.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8056 AddAnimatedTransformToLayer(
8057 grand_parent.get(), 1.0, TransformOperations(), scale);
8058 ExecuteCalculateDrawProperties(grand_parent.get());
8060 // |grand_parent| and |child| have scale-affecting animations.
8061 EXPECT_EQ(5.f,
8062 grand_parent->draw_properties().maximum_animation_contents_scale);
8063 EXPECT_EQ(5.f,
8064 parent_raw->draw_properties().maximum_animation_contents_scale);
8065 // We don't support combining animated scales from two nodes; 0.f means
8066 // that the maximum scale could not be computed.
8067 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8068 EXPECT_EQ(
8069 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8071 AddAnimatedTransformToLayer(parent_raw, 1.0, TransformOperations(), scale);
8072 ExecuteCalculateDrawProperties(grand_parent.get());
8074 // |grand_parent|, |parent|, and |child| have scale-affecting animations.
8075 EXPECT_EQ(5.f,
8076 grand_parent->draw_properties().maximum_animation_contents_scale);
8077 EXPECT_EQ(0.f,
8078 parent_raw->draw_properties().maximum_animation_contents_scale);
8079 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8080 EXPECT_EQ(
8081 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8083 grand_parent->layer_animation_controller()->AbortAnimations(
8084 Animation::TRANSFORM);
8085 parent_raw->layer_animation_controller()->AbortAnimations(
8086 Animation::TRANSFORM);
8087 child_raw->layer_animation_controller()->AbortAnimations(
8088 Animation::TRANSFORM);
8090 TransformOperations perspective;
8091 perspective.AppendPerspective(10.f);
8093 AddAnimatedTransformToLayer(
8094 child_raw, 1.0, TransformOperations(), perspective);
8095 ExecuteCalculateDrawProperties(grand_parent.get());
8097 // |child| has a scale-affecting animation but computing the maximum of this
8098 // animation is not supported.
8099 EXPECT_EQ(0.f,
8100 grand_parent->draw_properties().maximum_animation_contents_scale);
8101 EXPECT_EQ(0.f,
8102 parent_raw->draw_properties().maximum_animation_contents_scale);
8103 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8104 EXPECT_EQ(
8105 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8107 child_raw->layer_animation_controller()->AbortAnimations(
8108 Animation::TRANSFORM);
8110 gfx::Transform scale_matrix;
8111 scale_matrix.Scale(1.f, 2.f);
8112 grand_parent->SetTransform(scale_matrix);
8113 parent_raw->SetTransform(scale_matrix);
8114 AddAnimatedTransformToLayer(parent_raw, 1.0, TransformOperations(), scale);
8115 ExecuteCalculateDrawProperties(grand_parent.get());
8117 // |grand_parent| and |parent| each have scale 2.f. |parent| has a scale
8118 // animation with maximum scale 5.f.
8119 EXPECT_EQ(0.f,
8120 grand_parent->draw_properties().maximum_animation_contents_scale);
8121 EXPECT_EQ(10.f,
8122 parent_raw->draw_properties().maximum_animation_contents_scale);
8123 EXPECT_EQ(10.f,
8124 child_raw->draw_properties().maximum_animation_contents_scale);
8125 EXPECT_EQ(
8126 10.f,
8127 grand_child_raw->draw_properties().maximum_animation_contents_scale);
8129 gfx::Transform perspective_matrix;
8130 perspective_matrix.ApplyPerspectiveDepth(2.f);
8131 child_raw->SetTransform(perspective_matrix);
8132 ExecuteCalculateDrawProperties(grand_parent.get());
8134 // |child| has a transform that's neither a translation nor a scale.
8135 EXPECT_EQ(0.f,
8136 grand_parent->draw_properties().maximum_animation_contents_scale);
8137 EXPECT_EQ(10.f,
8138 parent_raw->draw_properties().maximum_animation_contents_scale);
8139 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8140 EXPECT_EQ(
8141 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8143 parent_raw->SetTransform(perspective_matrix);
8144 ExecuteCalculateDrawProperties(grand_parent.get());
8146 // |parent| and |child| have transforms that are neither translations nor
8147 // scales.
8148 EXPECT_EQ(0.f,
8149 grand_parent->draw_properties().maximum_animation_contents_scale);
8150 EXPECT_EQ(0.f,
8151 parent_raw->draw_properties().maximum_animation_contents_scale);
8152 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8153 EXPECT_EQ(
8154 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8156 parent_raw->SetTransform(identity_matrix);
8157 child_raw->SetTransform(identity_matrix);
8158 grand_parent->SetTransform(perspective_matrix);
8160 ExecuteCalculateDrawProperties(grand_parent.get());
8162 // |grand_parent| has a transform that's neither a translation nor a scale.
8163 EXPECT_EQ(0.f,
8164 grand_parent->draw_properties().maximum_animation_contents_scale);
8165 EXPECT_EQ(0.f,
8166 parent_raw->draw_properties().maximum_animation_contents_scale);
8167 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8168 EXPECT_EQ(
8169 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8172 static int membership_id(LayerImpl* layer) {
8173 return layer->draw_properties().last_drawn_render_surface_layer_list_id;
8176 static void GatherDrawnLayers(LayerImplList* rsll,
8177 std::set<LayerImpl*>* drawn_layers) {
8178 for (LayerIterator<LayerImpl> it = LayerIterator<LayerImpl>::Begin(rsll),
8179 end = LayerIterator<LayerImpl>::End(rsll);
8180 it != end;
8181 ++it) {
8182 LayerImpl* layer = *it;
8183 if (it.represents_itself())
8184 drawn_layers->insert(layer);
8186 if (!it.represents_contributing_render_surface())
8187 continue;
8189 if (layer->mask_layer())
8190 drawn_layers->insert(layer->mask_layer());
8191 if (layer->replica_layer() && layer->replica_layer()->mask_layer())
8192 drawn_layers->insert(layer->replica_layer()->mask_layer());
8196 TEST_F(LayerTreeHostCommonTest, RenderSurfaceLayerListMembership) {
8197 FakeImplProxy proxy;
8198 TestSharedBitmapManager shared_bitmap_manager;
8199 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8200 gfx::Transform identity_matrix;
8202 scoped_ptr<LayerImpl> grand_parent =
8203 LayerImpl::Create(host_impl.active_tree(), 1);
8204 scoped_ptr<LayerImpl> parent = LayerImpl::Create(host_impl.active_tree(), 3);
8205 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.active_tree(), 5);
8206 scoped_ptr<LayerImpl> grand_child1 =
8207 LayerImpl::Create(host_impl.active_tree(), 7);
8208 scoped_ptr<LayerImpl> grand_child2 =
8209 LayerImpl::Create(host_impl.active_tree(), 9);
8211 LayerImpl* grand_parent_raw = grand_parent.get();
8212 LayerImpl* parent_raw = parent.get();
8213 LayerImpl* child_raw = child.get();
8214 LayerImpl* grand_child1_raw = grand_child1.get();
8215 LayerImpl* grand_child2_raw = grand_child2.get();
8217 child->AddChild(grand_child1.Pass());
8218 child->AddChild(grand_child2.Pass());
8219 parent->AddChild(child.Pass());
8220 grand_parent->AddChild(parent.Pass());
8222 SetLayerPropertiesForTesting(grand_parent_raw, identity_matrix,
8223 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8224 true, false, true);
8225 SetLayerPropertiesForTesting(parent_raw, identity_matrix, gfx::Point3F(),
8226 gfx::PointF(), gfx::Size(1, 2), true, false,
8227 false);
8229 SetLayerPropertiesForTesting(child_raw, identity_matrix, gfx::Point3F(),
8230 gfx::PointF(), gfx::Size(1, 2), true, false,
8231 false);
8233 SetLayerPropertiesForTesting(grand_child1_raw, identity_matrix,
8234 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8235 true, false, false);
8237 SetLayerPropertiesForTesting(grand_child2_raw, identity_matrix,
8238 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8239 true, false, false);
8241 // Start with nothing being drawn.
8242 ExecuteCalculateDrawProperties(grand_parent_raw);
8243 int member_id = render_surface_layer_list_count();
8245 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8246 EXPECT_NE(member_id, membership_id(parent_raw));
8247 EXPECT_NE(member_id, membership_id(child_raw));
8248 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8249 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8251 std::set<LayerImpl*> expected;
8252 std::set<LayerImpl*> actual;
8253 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8254 EXPECT_EQ(expected, actual);
8256 // If we force render surface, but none of the layers are in the layer list,
8257 // then this layer should not appear in RSLL.
8258 grand_child1_raw->SetHasRenderSurface(true);
8260 ExecuteCalculateDrawProperties(grand_parent_raw);
8261 member_id = render_surface_layer_list_count();
8263 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8264 EXPECT_NE(member_id, membership_id(parent_raw));
8265 EXPECT_NE(member_id, membership_id(child_raw));
8266 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8267 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8269 expected.clear();
8270 actual.clear();
8271 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8272 EXPECT_EQ(expected, actual);
8274 // However, if we say that this layer also draws content, it will appear in
8275 // RSLL.
8276 grand_child1_raw->SetDrawsContent(true);
8278 ExecuteCalculateDrawProperties(grand_parent_raw);
8279 member_id = render_surface_layer_list_count();
8281 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8282 EXPECT_NE(member_id, membership_id(parent_raw));
8283 EXPECT_NE(member_id, membership_id(child_raw));
8284 EXPECT_EQ(member_id, membership_id(grand_child1_raw));
8285 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8287 expected.clear();
8288 expected.insert(grand_child1_raw);
8290 actual.clear();
8291 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8292 EXPECT_EQ(expected, actual);
8294 // Now child is forced to have a render surface, and one if its children draws
8295 // content.
8296 grand_child1_raw->SetDrawsContent(false);
8297 grand_child1_raw->SetHasRenderSurface(false);
8298 child_raw->SetHasRenderSurface(true);
8299 grand_child2_raw->SetDrawsContent(true);
8301 ExecuteCalculateDrawProperties(grand_parent_raw);
8302 member_id = render_surface_layer_list_count();
8304 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8305 EXPECT_NE(member_id, membership_id(parent_raw));
8306 EXPECT_NE(member_id, membership_id(child_raw));
8307 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8308 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8310 expected.clear();
8311 expected.insert(grand_child2_raw);
8313 actual.clear();
8314 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8315 EXPECT_EQ(expected, actual);
8317 // Add a mask layer to child.
8318 child_raw->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 6).Pass());
8320 ExecuteCalculateDrawProperties(grand_parent_raw);
8321 member_id = render_surface_layer_list_count();
8323 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8324 EXPECT_NE(member_id, membership_id(parent_raw));
8325 EXPECT_NE(member_id, membership_id(child_raw));
8326 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8327 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8328 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8330 expected.clear();
8331 expected.insert(grand_child2_raw);
8332 expected.insert(child_raw->mask_layer());
8334 expected.clear();
8335 expected.insert(grand_child2_raw);
8336 expected.insert(child_raw->mask_layer());
8338 actual.clear();
8339 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8340 EXPECT_EQ(expected, actual);
8342 // Add replica mask layer.
8343 scoped_ptr<LayerImpl> replica_layer =
8344 LayerImpl::Create(host_impl.active_tree(), 20);
8345 replica_layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 21));
8346 child_raw->SetReplicaLayer(replica_layer.Pass());
8348 ExecuteCalculateDrawProperties(grand_parent_raw);
8349 member_id = render_surface_layer_list_count();
8351 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8352 EXPECT_NE(member_id, membership_id(parent_raw));
8353 EXPECT_NE(member_id, membership_id(child_raw));
8354 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8355 EXPECT_EQ(member_id, membership_id(child_raw->replica_layer()->mask_layer()));
8356 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8357 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8359 expected.clear();
8360 expected.insert(grand_child2_raw);
8361 expected.insert(child_raw->mask_layer());
8362 expected.insert(child_raw->replica_layer()->mask_layer());
8364 actual.clear();
8365 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8366 EXPECT_EQ(expected, actual);
8368 child_raw->TakeReplicaLayer();
8370 // With nothing drawing, we should have no layers.
8371 grand_child2_raw->SetDrawsContent(false);
8373 ExecuteCalculateDrawProperties(grand_parent_raw);
8374 member_id = render_surface_layer_list_count();
8376 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8377 EXPECT_NE(member_id, membership_id(parent_raw));
8378 EXPECT_NE(member_id, membership_id(child_raw));
8379 EXPECT_NE(member_id, membership_id(child_raw->mask_layer()));
8380 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8381 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8383 expected.clear();
8384 actual.clear();
8385 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8386 EXPECT_EQ(expected, actual);
8388 // Child itself draws means that we should have the child and the mask in the
8389 // list.
8390 child_raw->SetDrawsContent(true);
8392 ExecuteCalculateDrawProperties(grand_parent_raw);
8393 member_id = render_surface_layer_list_count();
8395 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8396 EXPECT_NE(member_id, membership_id(parent_raw));
8397 EXPECT_EQ(member_id, membership_id(child_raw));
8398 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8399 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8400 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8402 expected.clear();
8403 expected.insert(child_raw);
8404 expected.insert(child_raw->mask_layer());
8405 actual.clear();
8406 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8407 EXPECT_EQ(expected, actual);
8409 child_raw->TakeMaskLayer();
8411 // Now everyone's a member!
8412 grand_parent_raw->SetDrawsContent(true);
8413 parent_raw->SetDrawsContent(true);
8414 child_raw->SetDrawsContent(true);
8415 grand_child1_raw->SetDrawsContent(true);
8416 grand_child2_raw->SetDrawsContent(true);
8418 ExecuteCalculateDrawProperties(grand_parent_raw);
8419 member_id = render_surface_layer_list_count();
8421 EXPECT_EQ(member_id, membership_id(grand_parent_raw));
8422 EXPECT_EQ(member_id, membership_id(parent_raw));
8423 EXPECT_EQ(member_id, membership_id(child_raw));
8424 EXPECT_EQ(member_id, membership_id(grand_child1_raw));
8425 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8427 expected.clear();
8428 expected.insert(grand_parent_raw);
8429 expected.insert(parent_raw);
8430 expected.insert(child_raw);
8431 expected.insert(grand_child1_raw);
8432 expected.insert(grand_child2_raw);
8434 actual.clear();
8435 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8436 EXPECT_EQ(expected, actual);
8439 TEST_F(LayerTreeHostCommonTest, DrawPropertyScales) {
8440 FakeImplProxy proxy;
8441 TestSharedBitmapManager shared_bitmap_manager;
8442 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8444 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
8445 LayerImpl* root_layer = root.get();
8446 scoped_ptr<LayerImpl> child1 = LayerImpl::Create(host_impl.active_tree(), 2);
8447 LayerImpl* child1_layer = child1.get();
8448 scoped_ptr<LayerImpl> child2 = LayerImpl::Create(host_impl.active_tree(), 3);
8449 LayerImpl* child2_layer = child2.get();
8451 root->AddChild(child1.Pass());
8452 root->AddChild(child2.Pass());
8453 root->SetHasRenderSurface(true);
8455 gfx::Transform identity_matrix, scale_transform_child1,
8456 scale_transform_child2;
8457 scale_transform_child1.Scale(2, 3);
8458 scale_transform_child2.Scale(4, 5);
8460 SetLayerPropertiesForTesting(root_layer, identity_matrix, gfx::Point3F(),
8461 gfx::PointF(), gfx::Size(1, 1), true, false,
8462 true);
8463 SetLayerPropertiesForTesting(child1_layer, scale_transform_child1,
8464 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
8465 false, false);
8467 child1_layer->SetMaskLayer(
8468 LayerImpl::Create(host_impl.active_tree(), 4).Pass());
8470 scoped_ptr<LayerImpl> replica_layer =
8471 LayerImpl::Create(host_impl.active_tree(), 5);
8472 replica_layer->SetHasRenderSurface(true);
8473 replica_layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 6));
8474 child1_layer->SetReplicaLayer(replica_layer.Pass());
8475 child1_layer->SetHasRenderSurface(true);
8477 ExecuteCalculateDrawProperties(root_layer);
8479 TransformOperations scale;
8480 scale.AppendScale(5.f, 8.f, 3.f);
8482 AddAnimatedTransformToLayer(child2_layer, 1.0, TransformOperations(), scale);
8483 SetLayerPropertiesForTesting(child2_layer, scale_transform_child2,
8484 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
8485 false, false);
8487 ExecuteCalculateDrawProperties(root_layer);
8489 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().ideal_contents_scale);
8490 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().ideal_contents_scale);
8491 EXPECT_FLOAT_EQ(
8492 3.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8493 EXPECT_FLOAT_EQ(3.f,
8494 child1_layer->replica_layer()
8495 ->mask_layer()
8496 ->draw_properties()
8497 .ideal_contents_scale);
8498 EXPECT_FLOAT_EQ(5.f, child2_layer->draw_properties().ideal_contents_scale);
8500 EXPECT_FLOAT_EQ(
8501 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8502 EXPECT_FLOAT_EQ(
8503 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8504 EXPECT_FLOAT_EQ(0.f,
8505 child1_layer->mask_layer()
8506 ->draw_properties()
8507 .maximum_animation_contents_scale);
8508 EXPECT_FLOAT_EQ(0.f,
8509 child1_layer->replica_layer()
8510 ->mask_layer()
8511 ->draw_properties()
8512 .maximum_animation_contents_scale);
8513 EXPECT_FLOAT_EQ(
8514 8.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8516 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8517 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().page_scale_factor);
8518 EXPECT_FLOAT_EQ(
8519 1.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8520 EXPECT_FLOAT_EQ(1.f,
8521 child1_layer->replica_layer()
8522 ->mask_layer()
8523 ->draw_properties()
8524 .page_scale_factor);
8525 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().page_scale_factor);
8527 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().device_scale_factor);
8528 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().device_scale_factor);
8529 EXPECT_FLOAT_EQ(
8530 1.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8531 EXPECT_FLOAT_EQ(1.f,
8532 child1_layer->replica_layer()
8533 ->mask_layer()
8534 ->draw_properties()
8535 .device_scale_factor);
8536 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().device_scale_factor);
8538 // Changing page-scale would affect ideal_contents_scale and
8539 // maximum_animation_contents_scale.
8541 float page_scale_factor = 3.f;
8542 float device_scale_factor = 1.0f;
8543 std::vector<LayerImpl*> render_surface_layer_list;
8544 gfx::Size device_viewport_size =
8545 gfx::Size(root_layer->bounds().width() * device_scale_factor,
8546 root_layer->bounds().height() * device_scale_factor);
8547 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
8548 root_layer, device_viewport_size, &render_surface_layer_list);
8550 inputs.page_scale_factor = page_scale_factor;
8551 inputs.can_adjust_raster_scales = true;
8552 inputs.page_scale_application_layer = root_layer;
8553 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8555 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().ideal_contents_scale);
8556 EXPECT_FLOAT_EQ(9.f, child1_layer->draw_properties().ideal_contents_scale);
8557 EXPECT_FLOAT_EQ(
8558 9.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8559 EXPECT_FLOAT_EQ(9.f,
8560 child1_layer->replica_layer()
8561 ->mask_layer()
8562 ->draw_properties()
8563 .ideal_contents_scale);
8564 EXPECT_FLOAT_EQ(15.f, child2_layer->draw_properties().ideal_contents_scale);
8566 EXPECT_FLOAT_EQ(
8567 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8568 EXPECT_FLOAT_EQ(
8569 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8570 EXPECT_FLOAT_EQ(0.f,
8571 child1_layer->mask_layer()
8572 ->draw_properties()
8573 .maximum_animation_contents_scale);
8574 EXPECT_FLOAT_EQ(0.f,
8575 child1_layer->replica_layer()
8576 ->mask_layer()
8577 ->draw_properties()
8578 .maximum_animation_contents_scale);
8579 EXPECT_FLOAT_EQ(
8580 24.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8582 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8583 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().page_scale_factor);
8584 EXPECT_FLOAT_EQ(
8585 3.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8586 EXPECT_FLOAT_EQ(3.f,
8587 child1_layer->replica_layer()
8588 ->mask_layer()
8589 ->draw_properties()
8590 .page_scale_factor);
8591 EXPECT_FLOAT_EQ(3.f, child2_layer->draw_properties().page_scale_factor);
8593 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().device_scale_factor);
8594 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().device_scale_factor);
8595 EXPECT_FLOAT_EQ(
8596 1.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8597 EXPECT_FLOAT_EQ(1.f,
8598 child1_layer->replica_layer()
8599 ->mask_layer()
8600 ->draw_properties()
8601 .device_scale_factor);
8602 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().device_scale_factor);
8604 // Changing device-scale would affect ideal_contents_scale and
8605 // maximum_animation_contents_scale.
8607 device_scale_factor = 4.0f;
8608 inputs.device_scale_factor = device_scale_factor;
8609 inputs.can_adjust_raster_scales = true;
8610 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8612 EXPECT_FLOAT_EQ(4.f, root_layer->draw_properties().ideal_contents_scale);
8613 EXPECT_FLOAT_EQ(36.f, child1_layer->draw_properties().ideal_contents_scale);
8614 EXPECT_FLOAT_EQ(
8615 36.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8616 EXPECT_FLOAT_EQ(36.f,
8617 child1_layer->replica_layer()
8618 ->mask_layer()
8619 ->draw_properties()
8620 .ideal_contents_scale);
8621 EXPECT_FLOAT_EQ(60.f, child2_layer->draw_properties().ideal_contents_scale);
8623 EXPECT_FLOAT_EQ(
8624 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8625 EXPECT_FLOAT_EQ(
8626 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8627 EXPECT_FLOAT_EQ(0.f,
8628 child1_layer->mask_layer()
8629 ->draw_properties()
8630 .maximum_animation_contents_scale);
8631 EXPECT_FLOAT_EQ(0.f,
8632 child1_layer->replica_layer()
8633 ->mask_layer()
8634 ->draw_properties()
8635 .maximum_animation_contents_scale);
8636 EXPECT_FLOAT_EQ(
8637 96.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8639 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8640 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().page_scale_factor);
8641 EXPECT_FLOAT_EQ(
8642 3.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8643 EXPECT_FLOAT_EQ(3.f,
8644 child1_layer->replica_layer()
8645 ->mask_layer()
8646 ->draw_properties()
8647 .page_scale_factor);
8648 EXPECT_FLOAT_EQ(3.f, child2_layer->draw_properties().page_scale_factor);
8650 EXPECT_FLOAT_EQ(4.f, root_layer->draw_properties().device_scale_factor);
8651 EXPECT_FLOAT_EQ(4.f, child1_layer->draw_properties().device_scale_factor);
8652 EXPECT_FLOAT_EQ(
8653 4.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8654 EXPECT_FLOAT_EQ(4.f,
8655 child1_layer->replica_layer()
8656 ->mask_layer()
8657 ->draw_properties()
8658 .device_scale_factor);
8659 EXPECT_FLOAT_EQ(4.f, child2_layer->draw_properties().device_scale_factor);
8662 TEST_F(LayerTreeHostCommonTest, VisibleContentRectInChildRenderSurface) {
8663 scoped_refptr<Layer> root = Layer::Create();
8664 SetLayerPropertiesForTesting(root.get(),
8665 gfx::Transform(),
8666 gfx::Point3F(),
8667 gfx::PointF(),
8668 gfx::Size(768 / 2, 3000),
8669 true,
8670 false);
8671 root->SetIsDrawable(true);
8673 scoped_refptr<Layer> clip = Layer::Create();
8674 SetLayerPropertiesForTesting(clip.get(),
8675 gfx::Transform(),
8676 gfx::Point3F(),
8677 gfx::PointF(),
8678 gfx::Size(768 / 2, 10000),
8679 true,
8680 false);
8681 clip->SetMasksToBounds(true);
8683 scoped_refptr<Layer> content = Layer::Create();
8684 SetLayerPropertiesForTesting(content.get(),
8685 gfx::Transform(),
8686 gfx::Point3F(),
8687 gfx::PointF(),
8688 gfx::Size(768 / 2, 10000),
8689 true,
8690 false);
8691 content->SetIsDrawable(true);
8692 content->SetForceRenderSurface(true);
8694 root->AddChild(clip);
8695 clip->AddChild(content);
8697 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
8698 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
8699 host->SetRootLayer(root);
8701 gfx::Size device_viewport_size(768, 582);
8702 RenderSurfaceLayerList render_surface_layer_list;
8703 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
8704 host->root_layer(), device_viewport_size, &render_surface_layer_list);
8705 inputs.device_scale_factor = 2.f;
8706 inputs.page_scale_factor = 1.f;
8707 inputs.page_scale_application_layer = NULL;
8708 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8710 // Layers in the root render surface have their visible content rect clipped
8711 // by the viewport.
8712 EXPECT_EQ(gfx::Rect(768 / 2, 582 / 2), root->visible_content_rect());
8714 // Layers drawing to a child render surface should still have their visible
8715 // content rect clipped by the viewport.
8716 EXPECT_EQ(gfx::Rect(768 / 2, 582 / 2), content->visible_content_rect());
8719 TEST_F(LayerTreeHostCommonTest, BoundsDeltaAffectVisibleContentRect) {
8720 FakeImplProxy proxy;
8721 TestSharedBitmapManager shared_bitmap_manager;
8722 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8724 // Set two layers: the root layer clips it's child,
8725 // the child draws its content.
8727 gfx::Size root_size = gfx::Size(300, 500);
8729 // Sublayer should be bigger than the root enlarged by bounds_delta.
8730 gfx::Size sublayer_size = gfx::Size(300, 1000);
8732 // Device viewport accomidated the root and the top controls.
8733 gfx::Size device_viewport_size = gfx::Size(300, 600);
8734 gfx::Transform identity_matrix;
8736 host_impl.active_tree()->SetRootLayer(
8737 LayerImpl::Create(host_impl.active_tree(), 1));
8739 LayerImpl* root = host_impl.active_tree()->root_layer();
8740 SetLayerPropertiesForTesting(root,
8741 identity_matrix,
8742 gfx::Point3F(),
8743 gfx::PointF(),
8744 root_size,
8745 false,
8746 false,
8747 true);
8749 root->SetContentBounds(root_size);
8750 root->SetMasksToBounds(true);
8752 root->AddChild(LayerImpl::Create(host_impl.active_tree(), 2));
8754 LayerImpl* sublayer = root->child_at(0);
8755 SetLayerPropertiesForTesting(sublayer,
8756 identity_matrix,
8757 gfx::Point3F(),
8758 gfx::PointF(),
8759 sublayer_size,
8760 false,
8761 false,
8762 false);
8764 sublayer->SetContentBounds(sublayer_size);
8765 sublayer->SetDrawsContent(true);
8767 LayerImplList layer_impl_list;
8768 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
8769 root, device_viewport_size, &layer_impl_list);
8771 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8773 EXPECT_EQ(gfx::Rect(root_size), sublayer->visible_content_rect());
8775 root->SetBoundsDelta(gfx::Vector2dF(0.0, 50.0));
8777 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8779 gfx::Rect affected_by_delta(0, 0, root_size.width(),
8780 root_size.height() + 50);
8781 EXPECT_EQ(affected_by_delta, sublayer->visible_content_rect());
8784 TEST_F(LayerTreeHostCommonTest, VisibleContentRectForAnimatedLayer) {
8785 const gfx::Transform identity_matrix;
8786 scoped_refptr<Layer> root = Layer::Create();
8787 scoped_refptr<LayerWithForcedDrawsContent> animated =
8788 make_scoped_refptr(new LayerWithForcedDrawsContent());
8790 root->AddChild(animated);
8792 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8793 host->SetRootLayer(root);
8795 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
8796 gfx::PointF(), gfx::Size(100, 100), true, false);
8797 SetLayerPropertiesForTesting(animated.get(), identity_matrix, gfx::Point3F(),
8798 gfx::PointF(), gfx::Size(20, 20), true, false);
8800 root->SetMasksToBounds(true);
8801 root->SetForceRenderSurface(true);
8802 animated->SetOpacity(0.f);
8804 AddOpacityTransitionToController(animated->layer_animation_controller(), 10.0,
8805 0.f, 1.f, false);
8807 ExecuteCalculateDrawProperties(root.get());
8809 EXPECT_FALSE(animated->visible_rect_from_property_trees().IsEmpty());
8812 TEST_F(LayerTreeHostCommonTest,
8813 VisibleContentRectForAnimatedLayerWithSingularTransform) {
8814 const gfx::Transform identity_matrix;
8815 scoped_refptr<Layer> root = Layer::Create();
8816 scoped_refptr<Layer> clip = Layer::Create();
8817 scoped_refptr<LayerWithForcedDrawsContent> animated =
8818 make_scoped_refptr(new LayerWithForcedDrawsContent());
8819 scoped_refptr<LayerWithForcedDrawsContent> surface =
8820 make_scoped_refptr(new LayerWithForcedDrawsContent());
8821 scoped_refptr<LayerWithForcedDrawsContent> descendant_of_animation =
8822 make_scoped_refptr(new LayerWithForcedDrawsContent());
8824 root->AddChild(clip);
8825 clip->AddChild(animated);
8826 animated->AddChild(surface);
8827 surface->AddChild(descendant_of_animation);
8829 clip->SetMasksToBounds(true);
8830 surface->SetForceRenderSurface(true);
8832 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8833 host->SetRootLayer(root);
8835 gfx::Transform uninvertible_matrix;
8836 uninvertible_matrix.Scale3d(6.f, 6.f, 0.f);
8838 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
8839 gfx::PointF(), gfx::Size(100, 100), true, false);
8840 SetLayerPropertiesForTesting(clip.get(), identity_matrix, gfx::Point3F(),
8841 gfx::PointF(), gfx::Size(10, 10), true, false);
8842 SetLayerPropertiesForTesting(animated.get(), uninvertible_matrix,
8843 gfx::Point3F(), gfx::PointF(),
8844 gfx::Size(120, 120), true, false);
8845 SetLayerPropertiesForTesting(surface.get(), identity_matrix, gfx::Point3F(),
8846 gfx::PointF(), gfx::Size(100, 100), true, false);
8847 SetLayerPropertiesForTesting(descendant_of_animation.get(), identity_matrix,
8848 gfx::Point3F(), gfx::PointF(),
8849 gfx::Size(200, 200), true, false);
8851 TransformOperations start_transform_operations;
8852 start_transform_operations.AppendMatrix(uninvertible_matrix);
8853 TransformOperations end_transform_operations;
8855 AddAnimatedTransformToLayer(animated.get(), 10.0, start_transform_operations,
8856 end_transform_operations);
8858 ExecuteCalculateDrawProperties(root.get());
8860 // The animated layer has a singular transform and maps to a non-empty rect in
8861 // clipped target space, so is treated as fully visible.
8862 EXPECT_EQ(gfx::Rect(120, 120), animated->visible_rect_from_property_trees());
8864 // The singular transform on |animated| is flattened when inherited by
8865 // |surface|, and this happens to make it invertible.
8866 EXPECT_EQ(gfx::Rect(2, 2), surface->visible_rect_from_property_trees());
8867 EXPECT_EQ(gfx::Rect(2, 2),
8868 descendant_of_animation->visible_rect_from_property_trees());
8870 gfx::Transform zero_matrix;
8871 zero_matrix.Scale3d(0.f, 0.f, 0.f);
8872 SetLayerPropertiesForTesting(animated.get(), zero_matrix, gfx::Point3F(),
8873 gfx::PointF(), gfx::Size(120, 120), true, false);
8875 ExecuteCalculateDrawProperties(root.get());
8877 // The animated layer maps to the empty rect in clipped target space, so is
8878 // treated as having an empty visible rect.
8879 EXPECT_EQ(gfx::Rect(), animated->visible_rect_from_property_trees());
8881 // This time, flattening does not make |animated|'s transform invertible. This
8882 // means the clip cannot be projected into |surface|'s space, so we treat
8883 // |surface| and layers that draw into it as fully visible.
8884 EXPECT_EQ(gfx::Rect(100, 100), surface->visible_rect_from_property_trees());
8885 EXPECT_EQ(gfx::Rect(200, 200),
8886 descendant_of_animation->visible_rect_from_property_trees());
8889 // Verify that having an animated filter (but no current filter, as these
8890 // are mutually exclusive) correctly creates a render surface.
8891 TEST_F(LayerTreeHostCommonTest, AnimatedFilterCreatesRenderSurface) {
8892 scoped_refptr<Layer> root = Layer::Create();
8893 scoped_refptr<Layer> child = Layer::Create();
8894 scoped_refptr<Layer> grandchild = Layer::Create();
8895 root->AddChild(child);
8896 child->AddChild(grandchild);
8898 gfx::Transform identity_transform;
8899 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
8900 gfx::PointF(), gfx::Size(50, 50), true, false);
8901 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
8902 gfx::PointF(), gfx::Size(50, 50), true, false);
8903 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
8904 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
8905 true, false);
8906 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8907 host->SetRootLayer(root);
8909 AddAnimatedFilterToLayer(child.get(), 10.0, 0.1f, 0.2f);
8911 ExecuteCalculateDrawProperties(root.get());
8913 EXPECT_TRUE(root->render_surface());
8914 EXPECT_TRUE(child->render_surface());
8915 EXPECT_FALSE(grandchild->render_surface());
8917 EXPECT_TRUE(root->filters().IsEmpty());
8918 EXPECT_TRUE(child->filters().IsEmpty());
8919 EXPECT_TRUE(grandchild->filters().IsEmpty());
8921 EXPECT_FALSE(root->FilterIsAnimating());
8922 EXPECT_TRUE(child->FilterIsAnimating());
8923 EXPECT_FALSE(grandchild->FilterIsAnimating());
8926 // Ensures that the property tree code accounts for offsets between fixed
8927 // position layers and their respective containers.
8928 TEST_F(LayerTreeHostCommonTest, PropertyTreesAccountForFixedParentOffset) {
8929 scoped_refptr<Layer> root = Layer::Create();
8930 scoped_refptr<Layer> child = Layer::Create();
8931 scoped_refptr<LayerWithForcedDrawsContent> grandchild =
8932 make_scoped_refptr(new LayerWithForcedDrawsContent());
8934 root->AddChild(child);
8935 child->AddChild(grandchild);
8937 gfx::Transform identity_transform;
8938 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
8939 gfx::PointF(), gfx::Size(50, 50), true, false);
8940 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
8941 gfx::PointF(1000, 1000), gfx::Size(50, 50), true,
8942 false);
8943 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
8944 gfx::Point3F(), gfx::PointF(-1000, -1000),
8945 gfx::Size(50, 50), true, false);
8947 root->SetMasksToBounds(true);
8948 root->SetIsContainerForFixedPositionLayers(true);
8949 LayerPositionConstraint constraint;
8950 constraint.set_is_fixed_position(true);
8951 grandchild->SetPositionConstraint(constraint);
8953 root->SetIsContainerForFixedPositionLayers(true);
8955 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8956 host->SetRootLayer(root);
8958 ExecuteCalculateDrawProperties(root.get());
8960 EXPECT_EQ(gfx::Rect(0, 0, 50, 50),
8961 grandchild->visible_rect_from_property_trees());
8964 TEST_F(LayerTreeHostCommonTest, CombineClipsUsingContentTarget) {
8965 // In the following layer tree, the layer |box|'s render target is |surface|.
8966 // |surface| also creates a transform node. We want to combine clips for |box|
8967 // in the space of its target (i.e., |surface|), not its target's target. This
8968 // test ensures that happens.
8970 gfx::Transform rotate;
8971 rotate.Rotate(5);
8972 gfx::Transform identity;
8974 scoped_refptr<Layer> root = Layer::Create();
8975 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
8976 gfx::PointF(), gfx::Size(2500, 1500), true,
8977 false);
8979 scoped_refptr<Layer> frame_clip = Layer::Create();
8980 SetLayerPropertiesForTesting(frame_clip.get(), identity, gfx::Point3F(),
8981 gfx::PointF(), gfx::Size(2500, 1500), true,
8982 false);
8983 frame_clip->SetMasksToBounds(true);
8985 scoped_refptr<Layer> rotated = Layer::Create();
8986 SetLayerPropertiesForTesting(rotated.get(), rotate,
8987 gfx::Point3F(1250, 250, 0), gfx::PointF(),
8988 gfx::Size(2500, 500), true, false);
8990 scoped_refptr<Layer> surface = Layer::Create();
8991 SetLayerPropertiesForTesting(surface.get(), rotate, gfx::Point3F(),
8992 gfx::PointF(), gfx::Size(2500, 500), true,
8993 false);
8994 surface->SetOpacity(0.5);
8996 scoped_refptr<LayerWithForcedDrawsContent> container =
8997 make_scoped_refptr(new LayerWithForcedDrawsContent());
8998 SetLayerPropertiesForTesting(container.get(), identity, gfx::Point3F(),
8999 gfx::PointF(), gfx::Size(300, 300), true, false);
9001 scoped_refptr<LayerWithForcedDrawsContent> box =
9002 make_scoped_refptr(new LayerWithForcedDrawsContent());
9003 SetLayerPropertiesForTesting(box.get(), identity, gfx::Point3F(),
9004 gfx::PointF(), gfx::Size(100, 100), true, false);
9006 root->AddChild(frame_clip);
9007 frame_clip->AddChild(rotated);
9008 rotated->AddChild(surface);
9009 surface->AddChild(container);
9010 surface->AddChild(box);
9012 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9013 host->SetRootLayer(root);
9015 ExecuteCalculateDrawProperties(root.get());
9018 TEST_F(LayerTreeHostCommonTest, OnlyApplyFixedPositioningOnce) {
9019 gfx::Transform identity;
9020 gfx::Transform translate_z;
9021 translate_z.Translate3d(0, 0, 10);
9023 scoped_refptr<Layer> root = Layer::Create();
9024 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9025 gfx::PointF(), gfx::Size(800, 800), true, false);
9026 root->SetIsContainerForFixedPositionLayers(true);
9028 scoped_refptr<Layer> frame_clip = Layer::Create();
9029 SetLayerPropertiesForTesting(frame_clip.get(), translate_z, gfx::Point3F(),
9030 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9031 false);
9032 frame_clip->SetMasksToBounds(true);
9034 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9035 make_scoped_refptr(new LayerWithForcedDrawsContent());
9036 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9037 gfx::PointF(), gfx::Size(1000, 1000), true,
9038 false);
9040 LayerPositionConstraint constraint;
9041 constraint.set_is_fixed_position(true);
9042 fixed->SetPositionConstraint(constraint);
9044 root->AddChild(frame_clip);
9045 frame_clip->AddChild(fixed);
9047 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9048 host->SetRootLayer(root);
9050 ExecuteCalculateDrawProperties(root.get());
9052 gfx::Rect expected(0, 0, 100, 100);
9053 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9056 TEST_F(LayerTreeHostCommonTest,
9057 PropertyTreesAccountForScrollCompensationAdjustment) {
9058 gfx::Transform identity;
9059 gfx::Transform translate_z;
9060 translate_z.Translate3d(0, 0, 10);
9062 scoped_refptr<Layer> root = Layer::Create();
9063 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9064 gfx::PointF(), gfx::Size(800, 800), true, false);
9065 root->SetIsContainerForFixedPositionLayers(true);
9067 scoped_refptr<Layer> frame_clip = Layer::Create();
9068 SetLayerPropertiesForTesting(frame_clip.get(), translate_z, gfx::Point3F(),
9069 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9070 false);
9071 frame_clip->SetMasksToBounds(true);
9073 scoped_refptr<LayerWithForcedDrawsContent> scroller =
9074 make_scoped_refptr(new LayerWithForcedDrawsContent());
9075 SetLayerPropertiesForTesting(scroller.get(), identity, gfx::Point3F(),
9076 gfx::PointF(), gfx::Size(1000, 1000), true,
9077 false);
9079 scroller->SetScrollCompensationAdjustment(gfx::Vector2dF(0.3f, 0.7f));
9080 scroller->SetScrollOffset(gfx::ScrollOffset(0.3, 0.7));
9081 scroller->SetScrollClipLayerId(frame_clip->id());
9083 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9084 make_scoped_refptr(new LayerWithForcedDrawsContent());
9085 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9086 gfx::PointF(), gfx::Size(50, 50), true, false);
9088 LayerPositionConstraint constraint;
9089 constraint.set_is_fixed_position(true);
9090 fixed->SetPositionConstraint(constraint);
9092 scoped_refptr<LayerWithForcedDrawsContent> fixed_child =
9093 make_scoped_refptr(new LayerWithForcedDrawsContent());
9094 SetLayerPropertiesForTesting(fixed_child.get(), identity, gfx::Point3F(),
9095 gfx::PointF(), gfx::Size(10, 10), true, false);
9097 fixed_child->SetPositionConstraint(constraint);
9099 root->AddChild(frame_clip);
9100 frame_clip->AddChild(scroller);
9101 scroller->AddChild(fixed);
9102 fixed->AddChild(fixed_child);
9104 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9105 host->SetRootLayer(root);
9107 ExecuteCalculateDrawProperties(root.get());
9109 gfx::Rect expected(0, 0, 50, 50);
9110 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9112 expected = gfx::Rect(0, 0, 10, 10);
9113 EXPECT_EQ(expected, fixed_child->visible_rect_from_property_trees());
9116 TEST_F(LayerTreeHostCommonTest, FixedClipsShouldBeAssociatedWithTheRightNode) {
9117 gfx::Transform identity;
9119 scoped_refptr<Layer> root = Layer::Create();
9120 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9121 gfx::PointF(), gfx::Size(800, 800), true, false);
9122 root->SetIsContainerForFixedPositionLayers(true);
9124 scoped_refptr<Layer> frame_clip = Layer::Create();
9125 SetLayerPropertiesForTesting(frame_clip.get(), identity, gfx::Point3F(),
9126 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9127 false);
9128 frame_clip->SetMasksToBounds(true);
9130 scoped_refptr<LayerWithForcedDrawsContent> scroller =
9131 make_scoped_refptr(new LayerWithForcedDrawsContent());
9132 SetLayerPropertiesForTesting(scroller.get(), identity, gfx::Point3F(),
9133 gfx::PointF(), gfx::Size(1000, 1000), true,
9134 false);
9136 scroller->SetScrollOffset(gfx::ScrollOffset(100, 100));
9137 scroller->SetScrollClipLayerId(frame_clip->id());
9139 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9140 make_scoped_refptr(new LayerWithForcedDrawsContent());
9141 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9142 gfx::PointF(100, 100), gfx::Size(50, 50), true,
9143 false);
9145 LayerPositionConstraint constraint;
9146 constraint.set_is_fixed_position(true);
9147 fixed->SetPositionConstraint(constraint);
9148 fixed->SetForceRenderSurface(true);
9149 fixed->SetMasksToBounds(true);
9151 root->AddChild(frame_clip);
9152 frame_clip->AddChild(scroller);
9153 scroller->AddChild(fixed);
9155 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9156 host->SetRootLayer(root);
9158 ExecuteCalculateDrawProperties(root.get());
9160 gfx::Rect expected(0, 0, 50, 50);
9161 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9164 } // namespace
9165 } // namespace cc