Disable flaky UnloadTest.CrossSiteInfiniteBeforeUnloadSync on Mac.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common_unittest.cc
blob591f48c43295c82059076b4a6a8cc86656a386dc
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_content_layer.h"
25 #include "cc/test/fake_content_layer_client.h"
26 #include "cc/test/fake_content_layer_impl.h"
27 #include "cc/test/fake_impl_proxy.h"
28 #include "cc/test/fake_layer_tree_host.h"
29 #include "cc/test/fake_layer_tree_host_impl.h"
30 #include "cc/test/fake_picture_layer.h"
31 #include "cc/test/fake_picture_layer_impl.h"
32 #include "cc/test/geometry_test_utils.h"
33 #include "cc/test/layer_tree_host_common_test.h"
34 #include "cc/test/test_task_graph_runner.h"
35 #include "cc/trees/layer_tree_impl.h"
36 #include "cc/trees/proxy.h"
37 #include "cc/trees/single_thread_proxy.h"
38 #include "testing/gmock/include/gmock/gmock.h"
39 #include "testing/gtest/include/gtest/gtest.h"
40 #include "ui/gfx/geometry/quad_f.h"
41 #include "ui/gfx/geometry/vector2d_conversions.h"
42 #include "ui/gfx/transform.h"
44 namespace cc {
45 namespace {
47 class LayerWithForcedDrawsContent : public Layer {
48 public:
49 LayerWithForcedDrawsContent() {}
51 bool DrawsContent() const override;
53 private:
54 ~LayerWithForcedDrawsContent() override {}
57 bool LayerWithForcedDrawsContent::DrawsContent() const { return true; }
59 class MockContentLayerClient : public ContentLayerClient {
60 public:
61 MockContentLayerClient() {}
62 ~MockContentLayerClient() override {}
63 void PaintContents(SkCanvas* canvas,
64 const gfx::Rect& clip,
65 PaintingControlSetting picture_control) override {}
66 void PaintContentsToDisplayList(
67 DisplayItemList* display_list,
68 const gfx::Rect& clip,
69 PaintingControlSetting picture_control) override {
70 NOTIMPLEMENTED();
72 bool FillsBoundsCompletely() const override { return false; }
75 scoped_refptr<FakePictureLayer> CreateDrawablePictureLayer(
76 ContentLayerClient* delegate) {
77 scoped_refptr<FakePictureLayer> to_return =
78 FakePictureLayer::Create(delegate);
79 to_return->SetIsDrawable(true);
80 return to_return;
83 scoped_refptr<ContentLayer> CreateDrawableContentLayer(
84 ContentLayerClient* delegate) {
85 scoped_refptr<ContentLayer> to_return = ContentLayer::Create(delegate);
86 to_return->SetIsDrawable(true);
87 return to_return;
90 #define EXPECT_CONTENTS_SCALE_EQ(expected, layer) \
91 do { \
92 EXPECT_FLOAT_EQ(expected, layer->contents_scale_x()); \
93 EXPECT_FLOAT_EQ(expected, layer->contents_scale_y()); \
94 } while (false)
96 #define EXPECT_IDEAL_SCALE_EQ(expected, layer) \
97 do { \
98 EXPECT_FLOAT_EQ(expected, layer->draw_properties().ideal_contents_scale); \
99 } while (false)
101 TEST_F(LayerTreeHostCommonTest, TransformsForNoOpLayer) {
102 // Sanity check: For layers positioned at zero, with zero size,
103 // and with identity transforms, then the draw transform,
104 // screen space transform, and the hierarchy passed on to children
105 // layers should also be identity transforms.
107 scoped_refptr<Layer> parent = Layer::Create();
108 scoped_refptr<Layer> child = Layer::Create();
109 scoped_refptr<Layer> grand_child = Layer::Create();
110 parent->AddChild(child);
111 child->AddChild(grand_child);
113 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
114 host->SetRootLayer(parent);
116 gfx::Transform identity_matrix;
117 SetLayerPropertiesForTesting(parent.get(),
118 identity_matrix,
119 gfx::Point3F(),
120 gfx::PointF(),
121 gfx::Size(100, 100),
122 true,
123 false);
124 SetLayerPropertiesForTesting(child.get(),
125 identity_matrix,
126 gfx::Point3F(),
127 gfx::PointF(),
128 gfx::Size(),
129 true,
130 false);
131 SetLayerPropertiesForTesting(grand_child.get(),
132 identity_matrix,
133 gfx::Point3F(),
134 gfx::PointF(),
135 gfx::Size(),
136 true,
137 false);
139 ExecuteCalculateDrawProperties(parent.get());
141 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
142 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
143 child->screen_space_transform());
144 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
145 grand_child->draw_transform());
146 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
147 grand_child->screen_space_transform());
150 TEST_F(LayerTreeHostCommonTest, DoNotSkipLayersWithHandlers) {
151 scoped_refptr<Layer> parent = Layer::Create();
152 scoped_refptr<Layer> child = Layer::Create();
153 scoped_refptr<Layer> grand_child = Layer::Create();
154 parent->AddChild(child);
155 child->AddChild(grand_child);
157 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
158 host->SetRootLayer(parent);
160 gfx::Transform identity_matrix;
161 SetLayerPropertiesForTesting(parent.get(),
162 identity_matrix,
163 gfx::Point3F(),
164 gfx::PointF(),
165 gfx::Size(100, 100),
166 true,
167 false);
168 SetLayerPropertiesForTesting(child.get(),
169 identity_matrix,
170 gfx::Point3F(),
171 gfx::PointF(10, 10),
172 gfx::Size(100, 100),
173 true,
174 false);
175 // This would have previously caused us to skip our subtree, but this would be
176 // wrong; we need up-to-date draw properties to do hit testing on the layers
177 // with handlers.
178 child->SetOpacity(0.f);
179 SetLayerPropertiesForTesting(grand_child.get(),
180 identity_matrix,
181 gfx::Point3F(),
182 gfx::PointF(10, 10),
183 gfx::Size(100, 100),
184 true,
185 false);
186 grand_child->SetTouchEventHandlerRegion(gfx::Rect(0, 0, 100, 100));
188 ExecuteCalculateDrawProperties(parent.get());
190 // Check that we've computed draw properties for the subtree rooted at
191 // |child|.
192 EXPECT_FALSE(child->draw_transform().IsIdentity());
193 EXPECT_FALSE(grand_child->draw_transform().IsIdentity());
196 TEST_F(LayerTreeHostCommonTest, TransformsForSingleLayer) {
197 gfx::Transform identity_matrix;
198 scoped_refptr<Layer> layer = Layer::Create();
200 scoped_refptr<Layer> root = Layer::Create();
201 SetLayerPropertiesForTesting(root.get(),
202 identity_matrix,
203 gfx::Point3F(),
204 gfx::PointF(),
205 gfx::Size(1, 2),
206 true,
207 false);
208 root->AddChild(layer);
210 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
211 host->SetRootLayer(root);
213 // Case 2: Setting the bounds of the layer should not affect either the draw
214 // transform or the screenspace transform.
215 gfx::Transform translation_to_center;
216 translation_to_center.Translate(5.0, 6.0);
217 SetLayerPropertiesForTesting(layer.get(),
218 identity_matrix,
219 gfx::Point3F(),
220 gfx::PointF(),
221 gfx::Size(10, 12),
222 true,
223 false);
224 ExecuteCalculateDrawProperties(root.get());
225 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, layer->draw_transform());
226 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
227 layer->screen_space_transform());
229 // Case 3: The anchor point by itself (without a layer transform) should have
230 // no effect on the transforms.
231 SetLayerPropertiesForTesting(layer.get(),
232 identity_matrix,
233 gfx::Point3F(2.5f, 3.0f, 0.f),
234 gfx::PointF(),
235 gfx::Size(10, 12),
236 true,
237 false);
238 ExecuteCalculateDrawProperties(root.get());
239 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, layer->draw_transform());
240 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
241 layer->screen_space_transform());
243 // Case 4: A change in actual position affects both the draw transform and
244 // screen space transform.
245 gfx::Transform position_transform;
246 position_transform.Translate(0.f, 1.2f);
247 SetLayerPropertiesForTesting(layer.get(),
248 identity_matrix,
249 gfx::Point3F(2.5f, 3.0f, 0.f),
250 gfx::PointF(0.f, 1.2f),
251 gfx::Size(10, 12),
252 true,
253 false);
254 ExecuteCalculateDrawProperties(root.get());
255 EXPECT_TRANSFORMATION_MATRIX_EQ(position_transform, layer->draw_transform());
256 EXPECT_TRANSFORMATION_MATRIX_EQ(position_transform,
257 layer->screen_space_transform());
259 // Case 5: In the correct sequence of transforms, the layer transform should
260 // pre-multiply the translation_to_center. This is easily tested by using a
261 // scale transform, because scale and translation are not commutative.
262 gfx::Transform layer_transform;
263 layer_transform.Scale3d(2.0, 2.0, 1.0);
264 SetLayerPropertiesForTesting(layer.get(),
265 layer_transform,
266 gfx::Point3F(),
267 gfx::PointF(),
268 gfx::Size(10, 12),
269 true,
270 false);
271 ExecuteCalculateDrawProperties(root.get());
272 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_transform, layer->draw_transform());
273 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_transform,
274 layer->screen_space_transform());
276 // Case 6: The layer transform should occur with respect to the anchor point.
277 gfx::Transform translation_to_anchor;
278 translation_to_anchor.Translate(5.0, 0.0);
279 gfx::Transform expected_result =
280 translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
281 SetLayerPropertiesForTesting(layer.get(),
282 layer_transform,
283 gfx::Point3F(5.0f, 0.f, 0.f),
284 gfx::PointF(),
285 gfx::Size(10, 12),
286 true,
287 false);
288 ExecuteCalculateDrawProperties(root.get());
289 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result, layer->draw_transform());
290 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result,
291 layer->screen_space_transform());
293 // Case 7: Verify that position pre-multiplies the layer transform. The
294 // current implementation of CalculateDrawProperties does this implicitly, but
295 // it is still worth testing to detect accidental regressions.
296 expected_result = position_transform * translation_to_anchor *
297 layer_transform * Inverse(translation_to_anchor);
298 SetLayerPropertiesForTesting(layer.get(),
299 layer_transform,
300 gfx::Point3F(5.0f, 0.f, 0.f),
301 gfx::PointF(0.f, 1.2f),
302 gfx::Size(10, 12),
303 true,
304 false);
305 ExecuteCalculateDrawProperties(root.get());
306 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result, layer->draw_transform());
307 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result,
308 layer->screen_space_transform());
311 TEST_F(LayerTreeHostCommonTest, TransformsAboutScrollOffset) {
312 const gfx::ScrollOffset kScrollOffset(50, 100);
313 const gfx::Vector2dF kScrollDelta(2.34f, 5.67f);
314 const gfx::Vector2d kMaxScrollOffset(200, 200);
315 const gfx::PointF kScrollLayerPosition(-kScrollOffset.x(),
316 -kScrollOffset.y());
317 const float kPageScale = 0.888f;
318 const float kDeviceScale = 1.666f;
320 FakeImplProxy proxy;
321 TestSharedBitmapManager shared_bitmap_manager;
322 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
324 gfx::Transform identity_matrix;
325 scoped_ptr<LayerImpl> sublayer_scoped_ptr(
326 LayerImpl::Create(host_impl.active_tree(), 1));
327 LayerImpl* sublayer = sublayer_scoped_ptr.get();
328 sublayer->SetContentsScale(kPageScale * kDeviceScale,
329 kPageScale * kDeviceScale);
330 SetLayerPropertiesForTesting(sublayer, identity_matrix, gfx::Point3F(),
331 gfx::PointF(), gfx::Size(500, 500), true, false,
332 false);
334 scoped_ptr<LayerImpl> scroll_layer_scoped_ptr(
335 LayerImpl::Create(host_impl.active_tree(), 2));
336 LayerImpl* scroll_layer = scroll_layer_scoped_ptr.get();
337 SetLayerPropertiesForTesting(scroll_layer, identity_matrix, gfx::Point3F(),
338 gfx::PointF(), gfx::Size(10, 20), true, false,
339 false);
340 scoped_ptr<LayerImpl> clip_layer_scoped_ptr(
341 LayerImpl::Create(host_impl.active_tree(), 4));
342 LayerImpl* clip_layer = clip_layer_scoped_ptr.get();
344 scroll_layer->SetScrollClipLayer(clip_layer->id());
345 clip_layer->SetBounds(
346 gfx::Size(scroll_layer->bounds().width() + kMaxScrollOffset.x(),
347 scroll_layer->bounds().height() + kMaxScrollOffset.y()));
348 scroll_layer->SetScrollClipLayer(clip_layer->id());
349 scroll_layer->SetScrollDelta(kScrollDelta);
350 gfx::Transform impl_transform;
351 scroll_layer->AddChild(sublayer_scoped_ptr.Pass());
352 LayerImpl* scroll_layer_raw_ptr = scroll_layer_scoped_ptr.get();
353 clip_layer->AddChild(scroll_layer_scoped_ptr.Pass());
354 scroll_layer_raw_ptr->PushScrollOffsetFromMainThread(kScrollOffset);
356 scoped_ptr<LayerImpl> root(LayerImpl::Create(host_impl.active_tree(), 3));
357 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
358 gfx::PointF(), gfx::Size(3, 4), true, false,
359 false);
360 root->AddChild(clip_layer_scoped_ptr.Pass());
361 root->SetHasRenderSurface(true);
363 ExecuteCalculateDrawProperties(
364 root.get(), kDeviceScale, kPageScale, scroll_layer->parent());
365 gfx::Transform expected_transform = identity_matrix;
366 gfx::PointF sub_layer_screen_position = kScrollLayerPosition - kScrollDelta;
367 sub_layer_screen_position.Scale(kPageScale * kDeviceScale);
368 expected_transform.Translate(MathUtil::Round(sub_layer_screen_position.x()),
369 MathUtil::Round(sub_layer_screen_position.y()));
370 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
371 sublayer->draw_transform());
372 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
373 sublayer->screen_space_transform());
375 gfx::Transform arbitrary_translate;
376 const float kTranslateX = 10.6f;
377 const float kTranslateY = 20.6f;
378 arbitrary_translate.Translate(kTranslateX, kTranslateY);
379 SetLayerPropertiesForTesting(scroll_layer, arbitrary_translate,
380 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 20),
381 true, false, false);
382 ExecuteCalculateDrawProperties(
383 root.get(), kDeviceScale, kPageScale, scroll_layer->parent());
384 expected_transform.MakeIdentity();
385 expected_transform.Translate(
386 MathUtil::Round(kTranslateX * kPageScale * kDeviceScale +
387 sub_layer_screen_position.x()),
388 MathUtil::Round(kTranslateY * kPageScale * kDeviceScale +
389 sub_layer_screen_position.y()));
390 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
391 sublayer->draw_transform());
394 TEST_F(LayerTreeHostCommonTest, TransformsForSimpleHierarchy) {
395 gfx::Transform identity_matrix;
396 scoped_refptr<Layer> root = Layer::Create();
397 scoped_refptr<Layer> parent = Layer::Create();
398 scoped_refptr<Layer> child = Layer::Create();
399 scoped_refptr<Layer> grand_child = Layer::Create();
400 root->AddChild(parent);
401 parent->AddChild(child);
402 child->AddChild(grand_child);
404 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
405 host->SetRootLayer(root);
407 // One-time setup of root layer
408 SetLayerPropertiesForTesting(root.get(),
409 identity_matrix,
410 gfx::Point3F(),
411 gfx::PointF(),
412 gfx::Size(1, 2),
413 true,
414 false);
416 // Case 1: parent's anchor point should not affect child or grand_child.
417 SetLayerPropertiesForTesting(parent.get(),
418 identity_matrix,
419 gfx::Point3F(2.5f, 3.0f, 0.f),
420 gfx::PointF(),
421 gfx::Size(10, 12),
422 true,
423 false);
424 SetLayerPropertiesForTesting(child.get(),
425 identity_matrix,
426 gfx::Point3F(),
427 gfx::PointF(),
428 gfx::Size(16, 18),
429 true,
430 false);
431 SetLayerPropertiesForTesting(grand_child.get(),
432 identity_matrix,
433 gfx::Point3F(),
434 gfx::PointF(),
435 gfx::Size(76, 78),
436 true,
437 false);
438 ExecuteCalculateDrawProperties(root.get());
439 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
440 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
441 child->screen_space_transform());
442 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
443 grand_child->draw_transform());
444 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
445 grand_child->screen_space_transform());
447 // Case 2: parent's position affects child and grand_child.
448 gfx::Transform parent_position_transform;
449 parent_position_transform.Translate(0.f, 1.2f);
450 SetLayerPropertiesForTesting(parent.get(),
451 identity_matrix,
452 gfx::Point3F(2.5f, 3.0f, 0.f),
453 gfx::PointF(0.f, 1.2f),
454 gfx::Size(10, 12),
455 true,
456 false);
457 SetLayerPropertiesForTesting(child.get(),
458 identity_matrix,
459 gfx::Point3F(),
460 gfx::PointF(),
461 gfx::Size(16, 18),
462 true,
463 false);
464 SetLayerPropertiesForTesting(grand_child.get(),
465 identity_matrix,
466 gfx::Point3F(),
467 gfx::PointF(),
468 gfx::Size(76, 78),
469 true,
470 false);
471 ExecuteCalculateDrawProperties(root.get());
472 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
473 child->draw_transform());
474 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
475 child->screen_space_transform());
476 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
477 grand_child->draw_transform());
478 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
479 grand_child->screen_space_transform());
481 // Case 3: parent's local transform affects child and grandchild
482 gfx::Transform parent_layer_transform;
483 parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
484 gfx::Transform parent_translation_to_anchor;
485 parent_translation_to_anchor.Translate(2.5, 3.0);
486 gfx::Transform parent_composite_transform =
487 parent_translation_to_anchor * parent_layer_transform *
488 Inverse(parent_translation_to_anchor);
489 SetLayerPropertiesForTesting(parent.get(),
490 parent_layer_transform,
491 gfx::Point3F(2.5f, 3.0f, 0.f),
492 gfx::PointF(),
493 gfx::Size(10, 12),
494 true,
495 false);
496 SetLayerPropertiesForTesting(child.get(),
497 identity_matrix,
498 gfx::Point3F(),
499 gfx::PointF(),
500 gfx::Size(16, 18),
501 true,
502 false);
503 SetLayerPropertiesForTesting(grand_child.get(),
504 identity_matrix,
505 gfx::Point3F(),
506 gfx::PointF(),
507 gfx::Size(76, 78),
508 true,
509 false);
510 ExecuteCalculateDrawProperties(root.get());
511 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
512 child->draw_transform());
513 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
514 child->screen_space_transform());
515 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
516 grand_child->draw_transform());
517 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
518 grand_child->screen_space_transform());
521 TEST_F(LayerTreeHostCommonTest, TransformsForSingleRenderSurface) {
522 scoped_refptr<Layer> root = Layer::Create();
523 scoped_refptr<Layer> parent = Layer::Create();
524 scoped_refptr<Layer> child = Layer::Create();
525 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
526 make_scoped_refptr(new LayerWithForcedDrawsContent());
527 root->AddChild(parent);
528 parent->AddChild(child);
529 child->AddChild(grand_child);
531 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
532 host->SetRootLayer(root);
534 // One-time setup of root layer
535 gfx::Transform identity_matrix;
536 SetLayerPropertiesForTesting(root.get(),
537 identity_matrix,
538 gfx::Point3F(),
539 gfx::PointF(),
540 gfx::Size(1, 2),
541 true,
542 false);
544 // Child is set up so that a new render surface should be created.
545 child->SetOpacity(0.5f);
546 child->SetForceRenderSurface(true);
548 gfx::Transform parent_layer_transform;
549 parent_layer_transform.Scale3d(1.f, 0.9f, 1.f);
550 gfx::Transform parent_translation_to_anchor;
551 parent_translation_to_anchor.Translate(25.0, 30.0);
553 gfx::Transform parent_composite_transform =
554 parent_translation_to_anchor * parent_layer_transform *
555 Inverse(parent_translation_to_anchor);
556 gfx::Vector2dF parent_composite_scale =
557 MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
558 1.f);
559 gfx::Transform surface_sublayer_transform;
560 surface_sublayer_transform.Scale(parent_composite_scale.x(),
561 parent_composite_scale.y());
562 gfx::Transform surface_sublayer_composite_transform =
563 parent_composite_transform * Inverse(surface_sublayer_transform);
565 // Child's render surface should not exist yet.
566 ASSERT_FALSE(child->render_surface());
568 SetLayerPropertiesForTesting(parent.get(),
569 parent_layer_transform,
570 gfx::Point3F(25.0f, 30.0f, 0.f),
571 gfx::PointF(),
572 gfx::Size(100, 120),
573 true,
574 false);
575 SetLayerPropertiesForTesting(child.get(),
576 identity_matrix,
577 gfx::Point3F(),
578 gfx::PointF(),
579 gfx::Size(16, 18),
580 true,
581 false);
582 SetLayerPropertiesForTesting(grand_child.get(),
583 identity_matrix,
584 gfx::Point3F(),
585 gfx::PointF(),
586 gfx::Size(8, 10),
587 true,
588 false);
589 ExecuteCalculateDrawProperties(root.get());
591 // Render surface should have been created now.
592 ASSERT_TRUE(child->render_surface());
593 ASSERT_EQ(child.get(), child->render_target());
595 // The child layer's draw transform should refer to its new render surface.
596 // The screen-space transform, however, should still refer to the root.
597 EXPECT_TRANSFORMATION_MATRIX_EQ(surface_sublayer_transform,
598 child->draw_transform());
599 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
600 child->screen_space_transform());
602 // Because the grand_child is the only drawable content, the child's render
603 // surface will tighten its bounds to the grand_child. The scale at which the
604 // surface's subtree is drawn must be removed from the composite transform.
605 EXPECT_TRANSFORMATION_MATRIX_EQ(
606 surface_sublayer_composite_transform,
607 child->render_target()->render_surface()->draw_transform());
609 // The screen space is the same as the target since the child surface draws
610 // into the root.
611 EXPECT_TRANSFORMATION_MATRIX_EQ(
612 surface_sublayer_composite_transform,
613 child->render_target()->render_surface()->screen_space_transform());
616 TEST_F(LayerTreeHostCommonTest, TransformsForReplica) {
617 scoped_refptr<Layer> root = Layer::Create();
618 scoped_refptr<Layer> parent = Layer::Create();
619 scoped_refptr<Layer> child = Layer::Create();
620 scoped_refptr<Layer> child_replica = Layer::Create();
621 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
622 make_scoped_refptr(new LayerWithForcedDrawsContent());
623 root->AddChild(parent);
624 parent->AddChild(child);
625 child->AddChild(grand_child);
626 child->SetReplicaLayer(child_replica.get());
628 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
629 host->SetRootLayer(root);
631 // One-time setup of root layer
632 gfx::Transform identity_matrix;
633 SetLayerPropertiesForTesting(root.get(),
634 identity_matrix,
635 gfx::Point3F(),
636 gfx::PointF(),
637 gfx::Size(1, 2),
638 true,
639 false);
641 // Child is set up so that a new render surface should be created.
642 child->SetOpacity(0.5f);
644 gfx::Transform parent_layer_transform;
645 parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
646 gfx::Transform parent_translation_to_anchor;
647 parent_translation_to_anchor.Translate(2.5, 3.0);
648 gfx::Transform parent_composite_transform =
649 parent_translation_to_anchor * parent_layer_transform *
650 Inverse(parent_translation_to_anchor);
651 gfx::Transform replica_layer_transform;
652 replica_layer_transform.Scale3d(3.0, 3.0, 1.0);
653 gfx::Vector2dF parent_composite_scale =
654 MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
655 1.f);
656 gfx::Transform surface_sublayer_transform;
657 surface_sublayer_transform.Scale(parent_composite_scale.x(),
658 parent_composite_scale.y());
659 gfx::Transform replica_composite_transform =
660 parent_composite_transform * replica_layer_transform *
661 Inverse(surface_sublayer_transform);
662 child_replica->SetIsDrawable(true);
663 // Child's render surface should not exist yet.
664 ASSERT_FALSE(child->render_surface());
666 SetLayerPropertiesForTesting(parent.get(),
667 parent_layer_transform,
668 gfx::Point3F(2.5f, 3.0f, 0.f),
669 gfx::PointF(),
670 gfx::Size(10, 12),
671 true,
672 false);
673 SetLayerPropertiesForTesting(child.get(),
674 identity_matrix,
675 gfx::Point3F(),
676 gfx::PointF(),
677 gfx::Size(16, 18),
678 true,
679 false);
680 SetLayerPropertiesForTesting(grand_child.get(),
681 identity_matrix,
682 gfx::Point3F(),
683 gfx::PointF(-0.5f, -0.5f),
684 gfx::Size(1, 1),
685 true,
686 false);
687 SetLayerPropertiesForTesting(child_replica.get(),
688 replica_layer_transform,
689 gfx::Point3F(),
690 gfx::PointF(),
691 gfx::Size(),
692 true,
693 false);
694 ExecuteCalculateDrawProperties(root.get());
696 // Render surface should have been created now.
697 ASSERT_TRUE(child->render_surface());
698 ASSERT_EQ(child.get(), child->render_target());
700 EXPECT_TRANSFORMATION_MATRIX_EQ(
701 replica_composite_transform,
702 child->render_target()->render_surface()->replica_draw_transform());
703 EXPECT_TRANSFORMATION_MATRIX_EQ(replica_composite_transform,
704 child->render_target()
705 ->render_surface()
706 ->replica_screen_space_transform());
709 TEST_F(LayerTreeHostCommonTest, TransformsForRenderSurfaceHierarchy) {
710 // This test creates a more complex tree and verifies it all at once. This
711 // covers the following cases:
712 // - layers that are described w.r.t. a render surface: should have draw
713 // transforms described w.r.t. that surface
714 // - A render surface described w.r.t. an ancestor render surface: should
715 // have a draw transform described w.r.t. that ancestor surface
716 // - Replicas of a render surface are described w.r.t. the replica's
717 // transform around its anchor, along with the surface itself.
718 // - Sanity check on recursion: verify transforms of layers described w.r.t.
719 // a render surface that is described w.r.t. an ancestor render surface.
720 // - verifying that each layer has a reference to the correct render surface
721 // and render target values.
723 scoped_refptr<Layer> root = Layer::Create();
724 scoped_refptr<Layer> parent = Layer::Create();
725 scoped_refptr<Layer> render_surface1 = Layer::Create();
726 scoped_refptr<Layer> render_surface2 = Layer::Create();
727 scoped_refptr<Layer> child_of_root = Layer::Create();
728 scoped_refptr<Layer> child_of_rs1 = Layer::Create();
729 scoped_refptr<Layer> child_of_rs2 = Layer::Create();
730 scoped_refptr<Layer> replica_of_rs1 = Layer::Create();
731 scoped_refptr<Layer> replica_of_rs2 = Layer::Create();
732 scoped_refptr<Layer> grand_child_of_root = Layer::Create();
733 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs1 =
734 make_scoped_refptr(new LayerWithForcedDrawsContent());
735 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs2 =
736 make_scoped_refptr(new LayerWithForcedDrawsContent());
737 root->AddChild(parent);
738 parent->AddChild(render_surface1);
739 parent->AddChild(child_of_root);
740 render_surface1->AddChild(child_of_rs1);
741 render_surface1->AddChild(render_surface2);
742 render_surface2->AddChild(child_of_rs2);
743 child_of_root->AddChild(grand_child_of_root);
744 child_of_rs1->AddChild(grand_child_of_rs1);
745 child_of_rs2->AddChild(grand_child_of_rs2);
746 render_surface1->SetReplicaLayer(replica_of_rs1.get());
747 render_surface2->SetReplicaLayer(replica_of_rs2.get());
749 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
750 host->SetRootLayer(root);
752 // In combination with descendant draws content, opacity != 1 forces the layer
753 // to have a new render surface.
754 render_surface1->SetOpacity(0.5f);
755 render_surface2->SetOpacity(0.33f);
757 // One-time setup of root layer
758 gfx::Transform identity_matrix;
759 SetLayerPropertiesForTesting(root.get(),
760 identity_matrix,
761 gfx::Point3F(),
762 gfx::PointF(),
763 gfx::Size(1, 2),
764 true,
765 false);
767 // All layers in the tree are initialized with an anchor at .25 and a size of
768 // (10,10). matrix "A" is the composite layer transform used in all layers,
769 // Matrix "R" is the composite replica transform used in all replica layers.
770 gfx::Transform translation_to_anchor;
771 translation_to_anchor.Translate(2.5, 0.0);
772 gfx::Transform layer_transform;
773 layer_transform.Translate(1.0, 1.0);
774 gfx::Transform replica_layer_transform;
775 replica_layer_transform.Scale3d(-2.0, 5.0, 1.0);
777 gfx::Transform A =
778 translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
779 gfx::Transform R = A * translation_to_anchor * replica_layer_transform *
780 Inverse(translation_to_anchor);
782 gfx::Vector2dF surface1_parent_transform_scale =
783 MathUtil::ComputeTransform2dScaleComponents(A, 1.f);
784 gfx::Transform surface1_sublayer_transform;
785 surface1_sublayer_transform.Scale(surface1_parent_transform_scale.x(),
786 surface1_parent_transform_scale.y());
788 // SS1 = transform given to the subtree of render_surface1
789 gfx::Transform SS1 = surface1_sublayer_transform;
790 // S1 = transform to move from render_surface1 pixels to the layer space of
791 // the owning layer
792 gfx::Transform S1 = Inverse(surface1_sublayer_transform);
794 gfx::Vector2dF surface2_parent_transform_scale =
795 MathUtil::ComputeTransform2dScaleComponents(SS1 * A, 1.f);
796 gfx::Transform surface2_sublayer_transform;
797 surface2_sublayer_transform.Scale(surface2_parent_transform_scale.x(),
798 surface2_parent_transform_scale.y());
800 // SS2 = transform given to the subtree of render_surface2
801 gfx::Transform SS2 = surface2_sublayer_transform;
802 // S2 = transform to move from render_surface2 pixels to the layer space of
803 // the owning layer
804 gfx::Transform S2 = Inverse(surface2_sublayer_transform);
806 SetLayerPropertiesForTesting(parent.get(),
807 layer_transform,
808 gfx::Point3F(2.5f, 0.f, 0.f),
809 gfx::PointF(),
810 gfx::Size(10, 10),
811 true,
812 false);
813 SetLayerPropertiesForTesting(render_surface1.get(),
814 layer_transform,
815 gfx::Point3F(2.5f, 0.f, 0.f),
816 gfx::PointF(),
817 gfx::Size(10, 10),
818 true,
819 false);
820 SetLayerPropertiesForTesting(render_surface2.get(),
821 layer_transform,
822 gfx::Point3F(2.5f, 0.f, 0.f),
823 gfx::PointF(),
824 gfx::Size(10, 10),
825 true,
826 false);
827 SetLayerPropertiesForTesting(child_of_root.get(),
828 layer_transform,
829 gfx::Point3F(2.5f, 0.f, 0.f),
830 gfx::PointF(),
831 gfx::Size(10, 10),
832 true,
833 false);
834 SetLayerPropertiesForTesting(child_of_rs1.get(),
835 layer_transform,
836 gfx::Point3F(2.5f, 0.f, 0.f),
837 gfx::PointF(),
838 gfx::Size(10, 10),
839 true,
840 false);
841 SetLayerPropertiesForTesting(child_of_rs2.get(),
842 layer_transform,
843 gfx::Point3F(2.5f, 0.f, 0.f),
844 gfx::PointF(),
845 gfx::Size(10, 10),
846 true,
847 false);
848 SetLayerPropertiesForTesting(grand_child_of_root.get(),
849 layer_transform,
850 gfx::Point3F(2.5f, 0.f, 0.f),
851 gfx::PointF(),
852 gfx::Size(10, 10),
853 true,
854 false);
855 SetLayerPropertiesForTesting(grand_child_of_rs1.get(),
856 layer_transform,
857 gfx::Point3F(2.5f, 0.f, 0.f),
858 gfx::PointF(),
859 gfx::Size(10, 10),
860 true,
861 false);
862 SetLayerPropertiesForTesting(grand_child_of_rs2.get(),
863 layer_transform,
864 gfx::Point3F(2.5f, 0.f, 0.f),
865 gfx::PointF(),
866 gfx::Size(10, 10),
867 true,
868 false);
869 SetLayerPropertiesForTesting(replica_of_rs1.get(),
870 replica_layer_transform,
871 gfx::Point3F(2.5f, 0.f, 0.f),
872 gfx::PointF(),
873 gfx::Size(),
874 true,
875 false);
876 SetLayerPropertiesForTesting(replica_of_rs2.get(),
877 replica_layer_transform,
878 gfx::Point3F(2.5f, 0.f, 0.f),
879 gfx::PointF(),
880 gfx::Size(),
881 true,
882 false);
884 ExecuteCalculateDrawProperties(root.get());
886 // Only layers that are associated with render surfaces should have an actual
887 // RenderSurface() value.
888 ASSERT_TRUE(root->render_surface());
889 ASSERT_FALSE(child_of_root->render_surface());
890 ASSERT_FALSE(grand_child_of_root->render_surface());
892 ASSERT_TRUE(render_surface1->render_surface());
893 ASSERT_FALSE(child_of_rs1->render_surface());
894 ASSERT_FALSE(grand_child_of_rs1->render_surface());
896 ASSERT_TRUE(render_surface2->render_surface());
897 ASSERT_FALSE(child_of_rs2->render_surface());
898 ASSERT_FALSE(grand_child_of_rs2->render_surface());
900 // Verify all render target accessors
901 EXPECT_EQ(root.get(), parent->render_target());
902 EXPECT_EQ(root.get(), child_of_root->render_target());
903 EXPECT_EQ(root.get(), grand_child_of_root->render_target());
905 EXPECT_EQ(render_surface1.get(), render_surface1->render_target());
906 EXPECT_EQ(render_surface1.get(), child_of_rs1->render_target());
907 EXPECT_EQ(render_surface1.get(), grand_child_of_rs1->render_target());
909 EXPECT_EQ(render_surface2.get(), render_surface2->render_target());
910 EXPECT_EQ(render_surface2.get(), child_of_rs2->render_target());
911 EXPECT_EQ(render_surface2.get(), grand_child_of_rs2->render_target());
913 // Verify layer draw transforms note that draw transforms are described with
914 // respect to the nearest ancestor render surface but screen space transforms
915 // are described with respect to the root.
916 EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->draw_transform());
917 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A, child_of_root->draw_transform());
918 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
919 grand_child_of_root->draw_transform());
921 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1, render_surface1->draw_transform());
922 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A, child_of_rs1->draw_transform());
923 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A * A,
924 grand_child_of_rs1->draw_transform());
926 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2, render_surface2->draw_transform());
927 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A, child_of_rs2->draw_transform());
928 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A * A,
929 grand_child_of_rs2->draw_transform());
931 // Verify layer screen-space transforms
933 EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->screen_space_transform());
934 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A,
935 child_of_root->screen_space_transform());
936 EXPECT_TRANSFORMATION_MATRIX_EQ(
937 A * A * A, grand_child_of_root->screen_space_transform());
939 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A,
940 render_surface1->screen_space_transform());
941 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
942 child_of_rs1->screen_space_transform());
943 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
944 grand_child_of_rs1->screen_space_transform());
946 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
947 render_surface2->screen_space_transform());
948 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
949 child_of_rs2->screen_space_transform());
950 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A * A,
951 grand_child_of_rs2->screen_space_transform());
953 // Verify render surface transforms.
955 // Draw transform of render surface 1 is described with respect to root.
956 EXPECT_TRANSFORMATION_MATRIX_EQ(
957 A * A * S1, render_surface1->render_surface()->draw_transform());
958 EXPECT_TRANSFORMATION_MATRIX_EQ(
959 A * R * S1, render_surface1->render_surface()->replica_draw_transform());
960 EXPECT_TRANSFORMATION_MATRIX_EQ(
961 A * A * S1, render_surface1->render_surface()->screen_space_transform());
962 EXPECT_TRANSFORMATION_MATRIX_EQ(
963 A * R * S1,
964 render_surface1->render_surface()->replica_screen_space_transform());
965 // Draw transform of render surface 2 is described with respect to render
966 // surface 1.
967 EXPECT_TRANSFORMATION_MATRIX_EQ(
968 SS1 * A * S2, render_surface2->render_surface()->draw_transform());
969 EXPECT_TRANSFORMATION_MATRIX_EQ(
970 SS1 * R * S2,
971 render_surface2->render_surface()->replica_draw_transform());
972 EXPECT_TRANSFORMATION_MATRIX_EQ(
973 A * A * A * S2,
974 render_surface2->render_surface()->screen_space_transform());
975 EXPECT_TRANSFORMATION_MATRIX_EQ(
976 A * A * R * S2,
977 render_surface2->render_surface()->replica_screen_space_transform());
979 // Sanity check. If these fail there is probably a bug in the test itself. It
980 // is expected that we correctly set up transforms so that the y-component of
981 // the screen-space transform encodes the "depth" of the layer in the tree.
982 EXPECT_FLOAT_EQ(1.0, parent->screen_space_transform().matrix().get(1, 3));
983 EXPECT_FLOAT_EQ(2.0,
984 child_of_root->screen_space_transform().matrix().get(1, 3));
985 EXPECT_FLOAT_EQ(
986 3.0, grand_child_of_root->screen_space_transform().matrix().get(1, 3));
988 EXPECT_FLOAT_EQ(2.0,
989 render_surface1->screen_space_transform().matrix().get(1, 3));
990 EXPECT_FLOAT_EQ(3.0,
991 child_of_rs1->screen_space_transform().matrix().get(1, 3));
992 EXPECT_FLOAT_EQ(
993 4.0, grand_child_of_rs1->screen_space_transform().matrix().get(1, 3));
995 EXPECT_FLOAT_EQ(3.0,
996 render_surface2->screen_space_transform().matrix().get(1, 3));
997 EXPECT_FLOAT_EQ(4.0,
998 child_of_rs2->screen_space_transform().matrix().get(1, 3));
999 EXPECT_FLOAT_EQ(
1000 5.0, grand_child_of_rs2->screen_space_transform().matrix().get(1, 3));
1003 TEST_F(LayerTreeHostCommonTest, TransformsForFlatteningLayer) {
1004 // For layers that flatten their subtree, there should be an orthographic
1005 // projection (for x and y values) in the middle of the transform sequence.
1006 // Note that the way the code is currently implemented, it is not expected to
1007 // use a canonical orthographic projection.
1009 scoped_refptr<Layer> root = Layer::Create();
1010 scoped_refptr<Layer> child = Layer::Create();
1011 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1012 make_scoped_refptr(new LayerWithForcedDrawsContent());
1013 scoped_refptr<LayerWithForcedDrawsContent> great_grand_child =
1014 make_scoped_refptr(new LayerWithForcedDrawsContent());
1016 gfx::Transform rotation_about_y_axis;
1017 rotation_about_y_axis.RotateAboutYAxis(30.0);
1019 const gfx::Transform identity_matrix;
1020 SetLayerPropertiesForTesting(root.get(),
1021 identity_matrix,
1022 gfx::Point3F(),
1023 gfx::PointF(),
1024 gfx::Size(100, 100),
1025 true,
1026 false);
1027 SetLayerPropertiesForTesting(child.get(),
1028 rotation_about_y_axis,
1029 gfx::Point3F(),
1030 gfx::PointF(),
1031 gfx::Size(10, 10),
1032 true,
1033 false);
1034 SetLayerPropertiesForTesting(grand_child.get(),
1035 rotation_about_y_axis,
1036 gfx::Point3F(),
1037 gfx::PointF(),
1038 gfx::Size(10, 10),
1039 true,
1040 false);
1041 SetLayerPropertiesForTesting(great_grand_child.get(), identity_matrix,
1042 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1043 true, false);
1045 root->AddChild(child);
1046 child->AddChild(grand_child);
1047 grand_child->AddChild(great_grand_child);
1048 child->SetForceRenderSurface(true);
1050 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1051 host->SetRootLayer(root);
1053 // No layers in this test should preserve 3d.
1054 ASSERT_TRUE(root->should_flatten_transform());
1055 ASSERT_TRUE(child->should_flatten_transform());
1056 ASSERT_TRUE(grand_child->should_flatten_transform());
1057 ASSERT_TRUE(great_grand_child->should_flatten_transform());
1059 gfx::Transform expected_child_draw_transform = rotation_about_y_axis;
1060 gfx::Transform expected_child_screen_space_transform = rotation_about_y_axis;
1061 gfx::Transform expected_grand_child_draw_transform =
1062 rotation_about_y_axis; // draws onto child's render surface
1063 gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
1064 flattened_rotation_about_y.FlattenTo2d();
1065 gfx::Transform expected_grand_child_screen_space_transform =
1066 flattened_rotation_about_y * rotation_about_y_axis;
1067 gfx::Transform expected_great_grand_child_draw_transform =
1068 flattened_rotation_about_y;
1069 gfx::Transform expected_great_grand_child_screen_space_transform =
1070 flattened_rotation_about_y * flattened_rotation_about_y;
1072 ExecuteCalculateDrawProperties(root.get());
1074 // The child's draw transform should have been taken by its surface.
1075 ASSERT_TRUE(child->render_surface());
1076 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_draw_transform,
1077 child->render_surface()->draw_transform());
1078 EXPECT_TRANSFORMATION_MATRIX_EQ(
1079 expected_child_screen_space_transform,
1080 child->render_surface()->screen_space_transform());
1081 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1082 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_screen_space_transform,
1083 child->screen_space_transform());
1084 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_draw_transform,
1085 grand_child->draw_transform());
1086 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_screen_space_transform,
1087 grand_child->screen_space_transform());
1088 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_great_grand_child_draw_transform,
1089 great_grand_child->draw_transform());
1090 EXPECT_TRANSFORMATION_MATRIX_EQ(
1091 expected_great_grand_child_screen_space_transform,
1092 great_grand_child->screen_space_transform());
1095 TEST_F(LayerTreeHostCommonTest, TransformsForDegenerateIntermediateLayer) {
1096 // A layer that is empty in one axis, but not the other, was accidentally
1097 // skipping a necessary translation. Without that translation, the coordinate
1098 // space of the layer's draw transform is incorrect.
1100 // Normally this isn't a problem, because the layer wouldn't be drawn anyway,
1101 // but if that layer becomes a render surface, then its draw transform is
1102 // implicitly inherited by the rest of the subtree, which then is positioned
1103 // incorrectly as a result.
1105 scoped_refptr<Layer> root = Layer::Create();
1106 scoped_refptr<Layer> child = Layer::Create();
1107 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1108 make_scoped_refptr(new LayerWithForcedDrawsContent());
1110 // The child height is zero, but has non-zero width that should be accounted
1111 // for while computing draw transforms.
1112 const gfx::Transform identity_matrix;
1113 SetLayerPropertiesForTesting(root.get(),
1114 identity_matrix,
1115 gfx::Point3F(),
1116 gfx::PointF(),
1117 gfx::Size(100, 100),
1118 true,
1119 false);
1120 SetLayerPropertiesForTesting(child.get(),
1121 identity_matrix,
1122 gfx::Point3F(),
1123 gfx::PointF(),
1124 gfx::Size(10, 0),
1125 true,
1126 false);
1127 SetLayerPropertiesForTesting(grand_child.get(),
1128 identity_matrix,
1129 gfx::Point3F(),
1130 gfx::PointF(),
1131 gfx::Size(10, 10),
1132 true,
1133 false);
1135 root->AddChild(child);
1136 child->AddChild(grand_child);
1137 child->SetForceRenderSurface(true);
1139 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1140 host->SetRootLayer(root);
1142 ExecuteCalculateDrawProperties(root.get());
1144 ASSERT_TRUE(child->render_surface());
1145 // This is the real test, the rest are sanity checks.
1146 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1147 child->render_surface()->draw_transform());
1148 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1149 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1150 grand_child->draw_transform());
1153 TEST_F(LayerTreeHostCommonTest, TransformAboveRootLayer) {
1154 // Transformations applied at the root of the tree should be forwarded
1155 // to child layers instead of applied to the root RenderSurface.
1156 const gfx::Transform identity_matrix;
1157 scoped_refptr<LayerWithForcedDrawsContent> root =
1158 new LayerWithForcedDrawsContent;
1159 scoped_refptr<LayerWithForcedDrawsContent> child =
1160 new LayerWithForcedDrawsContent;
1161 child->SetScrollClipLayerId(root->id());
1162 root->AddChild(child);
1164 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1165 host->SetRootLayer(root);
1167 SetLayerPropertiesForTesting(root.get(),
1168 identity_matrix,
1169 gfx::Point3F(),
1170 gfx::PointF(),
1171 gfx::Size(20, 20),
1172 true,
1173 false);
1174 SetLayerPropertiesForTesting(child.get(),
1175 identity_matrix,
1176 gfx::Point3F(),
1177 gfx::PointF(),
1178 gfx::Size(20, 20),
1179 true,
1180 false);
1182 gfx::Transform translate;
1183 translate.Translate(50, 50);
1185 RenderSurfaceLayerList render_surface_layer_list;
1186 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1187 root.get(), root->bounds(), translate, &render_surface_layer_list);
1188 inputs.can_adjust_raster_scales = true;
1189 inputs.property_trees->needs_rebuild = true;
1190 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1191 EXPECT_EQ(translate, root->draw_properties().target_space_transform);
1192 EXPECT_EQ(translate, child->draw_properties().target_space_transform);
1193 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1194 EXPECT_EQ(1.f, root->draw_properties().device_scale_factor);
1195 EXPECT_EQ(1.f, child->draw_properties().device_scale_factor);
1198 gfx::Transform scale;
1199 scale.Scale(2, 2);
1201 RenderSurfaceLayerList render_surface_layer_list;
1202 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1203 root.get(), root->bounds(), scale, &render_surface_layer_list);
1204 inputs.can_adjust_raster_scales = true;
1205 inputs.property_trees->needs_rebuild = true;
1206 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1207 EXPECT_EQ(scale, root->draw_properties().target_space_transform);
1208 EXPECT_EQ(scale, child->draw_properties().target_space_transform);
1209 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1210 EXPECT_EQ(2.f, root->draw_properties().device_scale_factor);
1211 EXPECT_EQ(2.f, child->draw_properties().device_scale_factor);
1214 gfx::Transform rotate;
1215 rotate.Rotate(2);
1217 RenderSurfaceLayerList render_surface_layer_list;
1218 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1219 root.get(), root->bounds(), rotate, &render_surface_layer_list);
1220 inputs.can_adjust_raster_scales = true;
1221 inputs.property_trees->needs_rebuild = true;
1222 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1223 EXPECT_EQ(rotate, root->draw_properties().target_space_transform);
1224 EXPECT_EQ(rotate, child->draw_properties().target_space_transform);
1225 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1226 EXPECT_EQ(1.f, root->draw_properties().device_scale_factor);
1227 EXPECT_EQ(1.f, child->draw_properties().device_scale_factor);
1230 gfx::Transform composite;
1231 composite.ConcatTransform(translate);
1232 composite.ConcatTransform(scale);
1233 composite.ConcatTransform(rotate);
1235 RenderSurfaceLayerList render_surface_layer_list;
1236 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1237 root.get(), root->bounds(), composite, &render_surface_layer_list);
1238 inputs.can_adjust_raster_scales = true;
1239 inputs.property_trees->needs_rebuild = true;
1240 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1241 EXPECT_EQ(composite, root->draw_properties().target_space_transform);
1242 EXPECT_EQ(composite, child->draw_properties().target_space_transform);
1243 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1246 // Verify it composes correctly with device scale.
1247 float device_scale_factor = 1.5f;
1250 RenderSurfaceLayerList render_surface_layer_list;
1251 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1252 root.get(), root->bounds(), translate, &render_surface_layer_list);
1253 inputs.device_scale_factor = device_scale_factor;
1254 inputs.can_adjust_raster_scales = true;
1255 inputs.property_trees->needs_rebuild = true;
1256 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1257 gfx::Transform device_scaled_translate = translate;
1258 device_scaled_translate.Scale(device_scale_factor, device_scale_factor);
1259 EXPECT_EQ(device_scaled_translate,
1260 root->draw_properties().target_space_transform);
1261 EXPECT_EQ(device_scaled_translate,
1262 child->draw_properties().target_space_transform);
1263 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1264 EXPECT_EQ(device_scale_factor, root->draw_properties().device_scale_factor);
1265 EXPECT_EQ(device_scale_factor,
1266 child->draw_properties().device_scale_factor);
1269 // Verify it composes correctly with page scale.
1270 float page_scale_factor = 2.f;
1273 RenderSurfaceLayerList render_surface_layer_list;
1274 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1275 root.get(), root->bounds(), translate, &render_surface_layer_list);
1276 inputs.page_scale_factor = page_scale_factor;
1277 inputs.page_scale_application_layer = root.get();
1278 inputs.can_adjust_raster_scales = true;
1279 inputs.property_trees->needs_rebuild = true;
1280 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1281 gfx::Transform page_scaled_translate = translate;
1282 page_scaled_translate.Scale(page_scale_factor, page_scale_factor);
1283 EXPECT_EQ(translate, root->draw_properties().target_space_transform);
1284 EXPECT_EQ(page_scaled_translate,
1285 child->draw_properties().target_space_transform);
1286 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1287 EXPECT_EQ(1.f, root->draw_properties().device_scale_factor);
1288 EXPECT_EQ(1.f, child->draw_properties().device_scale_factor);
1291 // Verify that it composes correctly with transforms directly on root layer.
1292 root->SetTransform(composite);
1295 RenderSurfaceLayerList render_surface_layer_list;
1296 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1297 root.get(), root->bounds(), composite, &render_surface_layer_list);
1298 inputs.can_adjust_raster_scales = true;
1299 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1300 gfx::Transform compositeSquared = composite;
1301 compositeSquared.ConcatTransform(composite);
1302 EXPECT_TRANSFORMATION_MATRIX_EQ(
1303 compositeSquared, root->draw_properties().target_space_transform);
1304 EXPECT_TRANSFORMATION_MATRIX_EQ(
1305 compositeSquared, child->draw_properties().target_space_transform);
1306 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1310 TEST_F(LayerTreeHostCommonTest,
1311 RenderSurfaceListForRenderSurfaceWithClippedLayer) {
1312 scoped_refptr<Layer> parent = Layer::Create();
1313 scoped_refptr<Layer> render_surface1 = Layer::Create();
1314 scoped_refptr<LayerWithForcedDrawsContent> child =
1315 make_scoped_refptr(new LayerWithForcedDrawsContent());
1317 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1318 host->SetRootLayer(parent);
1320 const gfx::Transform identity_matrix;
1321 SetLayerPropertiesForTesting(parent.get(),
1322 identity_matrix,
1323 gfx::Point3F(),
1324 gfx::PointF(),
1325 gfx::Size(10, 10),
1326 true,
1327 false);
1328 SetLayerPropertiesForTesting(render_surface1.get(),
1329 identity_matrix,
1330 gfx::Point3F(),
1331 gfx::PointF(),
1332 gfx::Size(10, 10),
1333 true,
1334 false);
1335 SetLayerPropertiesForTesting(child.get(),
1336 identity_matrix,
1337 gfx::Point3F(),
1338 gfx::PointF(30.f, 30.f),
1339 gfx::Size(10, 10),
1340 true,
1341 false);
1343 parent->AddChild(render_surface1);
1344 parent->SetMasksToBounds(true);
1345 render_surface1->AddChild(child);
1346 render_surface1->SetForceRenderSurface(true);
1348 RenderSurfaceLayerList render_surface_layer_list;
1349 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1350 parent.get(),
1351 parent->bounds(),
1352 gfx::Transform(),
1353 &render_surface_layer_list);
1354 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1356 // The child layer's content is entirely outside the parent's clip rect, so
1357 // the intermediate render surface should not be listed here, even if it was
1358 // forced to be created. Render surfaces without children or visible content
1359 // are unexpected at draw time (e.g. we might try to create a content texture
1360 // of size 0).
1361 ASSERT_TRUE(parent->render_surface());
1362 EXPECT_EQ(1U, render_surface_layer_list.size());
1365 TEST_F(LayerTreeHostCommonTest, RenderSurfaceListForTransparentChild) {
1366 scoped_refptr<Layer> parent = Layer::Create();
1367 scoped_refptr<Layer> render_surface1 = Layer::Create();
1368 scoped_refptr<LayerWithForcedDrawsContent> child =
1369 make_scoped_refptr(new LayerWithForcedDrawsContent());
1371 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1372 host->SetRootLayer(parent);
1374 const gfx::Transform identity_matrix;
1375 SetLayerPropertiesForTesting(render_surface1.get(),
1376 identity_matrix,
1377 gfx::Point3F(),
1378 gfx::PointF(),
1379 gfx::Size(10, 10),
1380 true,
1381 false);
1382 SetLayerPropertiesForTesting(child.get(),
1383 identity_matrix,
1384 gfx::Point3F(),
1385 gfx::PointF(),
1386 gfx::Size(10, 10),
1387 true,
1388 false);
1390 parent->AddChild(render_surface1);
1391 render_surface1->AddChild(child);
1392 render_surface1->SetForceRenderSurface(true);
1393 render_surface1->SetOpacity(0.f);
1395 RenderSurfaceLayerList render_surface_layer_list;
1396 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1397 parent.get(), parent->bounds(), &render_surface_layer_list);
1398 inputs.can_adjust_raster_scales = true;
1399 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1401 // Since the layer is transparent, render_surface1->render_surface() should
1402 // not have gotten added anywhere. Also, the drawable content rect should not
1403 // have been extended by the children.
1404 ASSERT_TRUE(parent->render_surface());
1405 EXPECT_EQ(0U, parent->render_surface()->layer_list().size());
1406 EXPECT_EQ(1U, render_surface_layer_list.size());
1407 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1408 EXPECT_EQ(gfx::Rect(), parent->drawable_content_rect());
1411 TEST_F(LayerTreeHostCommonTest, RenderSurfaceForBlendMode) {
1412 scoped_refptr<Layer> parent = Layer::Create();
1413 scoped_refptr<LayerWithForcedDrawsContent> child =
1414 make_scoped_refptr(new LayerWithForcedDrawsContent());
1416 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1417 host->SetRootLayer(parent);
1419 const gfx::Transform identity_matrix;
1420 const SkXfermode::Mode blend_mode = SkXfermode::kMultiply_Mode;
1421 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
1422 gfx::PointF(), gfx::Size(10, 10), true, false);
1424 parent->AddChild(child);
1425 child->SetBlendMode(blend_mode);
1427 RenderSurfaceLayerList render_surface_layer_list;
1428 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1429 parent.get(), parent->bounds(), &render_surface_layer_list);
1430 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1432 // Since the child layer has a blend mode other than normal, it should get
1433 // its own render surface. Also, layer's draw_properties should contain the
1434 // default blend mode, since the render surface becomes responsible for
1435 // applying the blend mode.
1436 ASSERT_TRUE(child->render_surface());
1437 EXPECT_EQ(1U, child->render_surface()->layer_list().size());
1438 EXPECT_EQ(SkXfermode::kSrcOver_Mode, child->draw_properties().blend_mode);
1441 TEST_F(LayerTreeHostCommonTest, ForceRenderSurface) {
1442 scoped_refptr<Layer> parent = Layer::Create();
1443 scoped_refptr<Layer> render_surface1 = Layer::Create();
1444 scoped_refptr<LayerWithForcedDrawsContent> child =
1445 make_scoped_refptr(new LayerWithForcedDrawsContent());
1446 render_surface1->SetForceRenderSurface(true);
1448 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1449 host->SetRootLayer(parent);
1451 const gfx::Transform identity_matrix;
1452 SetLayerPropertiesForTesting(parent.get(),
1453 identity_matrix,
1454 gfx::Point3F(),
1455 gfx::PointF(),
1456 gfx::Size(10, 10),
1457 true,
1458 false);
1459 SetLayerPropertiesForTesting(render_surface1.get(),
1460 identity_matrix,
1461 gfx::Point3F(),
1462 gfx::PointF(),
1463 gfx::Size(10, 10),
1464 true,
1465 false);
1466 SetLayerPropertiesForTesting(child.get(),
1467 identity_matrix,
1468 gfx::Point3F(),
1469 gfx::PointF(),
1470 gfx::Size(10, 10),
1471 true,
1472 false);
1474 parent->AddChild(render_surface1);
1475 render_surface1->AddChild(child);
1477 // Sanity check before the actual test
1478 EXPECT_FALSE(parent->render_surface());
1479 EXPECT_FALSE(render_surface1->render_surface());
1482 RenderSurfaceLayerList render_surface_layer_list;
1483 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1484 parent.get(), parent->bounds(), &render_surface_layer_list);
1485 inputs.can_adjust_raster_scales = true;
1486 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1488 // The root layer always creates a render surface
1489 EXPECT_TRUE(parent->render_surface());
1490 EXPECT_TRUE(render_surface1->render_surface());
1491 EXPECT_EQ(2U, render_surface_layer_list.size());
1495 RenderSurfaceLayerList render_surface_layer_list;
1496 render_surface1->SetForceRenderSurface(false);
1497 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1498 parent.get(), parent->bounds(), &render_surface_layer_list);
1499 inputs.can_adjust_raster_scales = true;
1500 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1501 EXPECT_TRUE(parent->render_surface());
1502 EXPECT_FALSE(render_surface1->render_surface());
1503 EXPECT_EQ(1U, render_surface_layer_list.size());
1507 TEST_F(LayerTreeHostCommonTest, RenderSurfacesFlattenScreenSpaceTransform) {
1508 // Render surfaces act as a flattening point for their subtree, so should
1509 // always flatten the target-to-screen space transform seen by descendants.
1511 scoped_refptr<Layer> root = Layer::Create();
1512 scoped_refptr<Layer> parent = Layer::Create();
1513 scoped_refptr<LayerWithForcedDrawsContent> child =
1514 make_scoped_refptr(new LayerWithForcedDrawsContent());
1515 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1516 make_scoped_refptr(new LayerWithForcedDrawsContent());
1518 gfx::Transform rotation_about_y_axis;
1519 rotation_about_y_axis.RotateAboutYAxis(30.0);
1520 // Make |parent| have a render surface.
1521 parent->SetOpacity(0.9f);
1523 const gfx::Transform identity_matrix;
1524 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
1525 gfx::PointF(), gfx::Size(100, 100), true, false);
1526 SetLayerPropertiesForTesting(parent.get(), rotation_about_y_axis,
1527 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1528 true, false);
1529 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
1530 gfx::PointF(), gfx::Size(10, 10), true, false);
1531 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
1532 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1533 true, false);
1535 root->AddChild(parent);
1536 parent->AddChild(child);
1537 child->AddChild(grand_child);
1539 grand_child->SetShouldFlattenTransform(false);
1541 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1542 host->SetRootLayer(root);
1544 // Only grand_child should preserve 3d.
1545 EXPECT_TRUE(root->should_flatten_transform());
1546 EXPECT_TRUE(parent->should_flatten_transform());
1547 EXPECT_TRUE(child->should_flatten_transform());
1548 EXPECT_FALSE(grand_child->should_flatten_transform());
1550 gfx::Transform expected_child_draw_transform = identity_matrix;
1551 gfx::Transform expected_grand_child_draw_transform = identity_matrix;
1553 gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
1554 flattened_rotation_about_y.FlattenTo2d();
1556 ExecuteCalculateDrawProperties(root.get());
1558 EXPECT_TRUE(parent->render_surface());
1559 EXPECT_FALSE(child->render_surface());
1560 EXPECT_FALSE(grand_child->render_surface());
1562 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1563 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1564 grand_child->draw_transform());
1566 // The screen-space transform inherited by |child| and |grand_child| should
1567 // have been flattened at their render target. In particular, the fact that
1568 // |grand_child| happens to preserve 3d shouldn't affect this flattening.
1569 EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
1570 child->screen_space_transform());
1571 EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
1572 grand_child->screen_space_transform());
1575 TEST_F(LayerTreeHostCommonTest, ClipRectCullsRenderSurfaces) {
1576 // The entire subtree of layers that are outside the clip rect should be
1577 // culled away, and should not affect the render_surface_layer_list.
1579 // The test tree is set up as follows:
1580 // - all layers except the leaf_nodes are forced to be a new render surface
1581 // that have something to draw.
1582 // - parent is a large container layer.
1583 // - child has masksToBounds=true to cause clipping.
1584 // - grand_child is positioned outside of the child's bounds
1585 // - great_grand_child is also kept outside child's bounds.
1587 // In this configuration, grand_child and great_grand_child are completely
1588 // outside the clip rect, and they should never get scheduled on the list of
1589 // render surfaces.
1592 const gfx::Transform identity_matrix;
1593 scoped_refptr<Layer> parent = Layer::Create();
1594 scoped_refptr<Layer> child = Layer::Create();
1595 scoped_refptr<Layer> grand_child = Layer::Create();
1596 scoped_refptr<Layer> great_grand_child = Layer::Create();
1597 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
1598 make_scoped_refptr(new LayerWithForcedDrawsContent());
1599 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
1600 make_scoped_refptr(new LayerWithForcedDrawsContent());
1601 parent->AddChild(child);
1602 child->AddChild(grand_child);
1603 grand_child->AddChild(great_grand_child);
1605 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1606 host->SetRootLayer(parent);
1608 // leaf_node1 ensures that parent and child are kept on the
1609 // render_surface_layer_list, even though grand_child and great_grand_child
1610 // should be clipped.
1611 child->AddChild(leaf_node1);
1612 great_grand_child->AddChild(leaf_node2);
1614 SetLayerPropertiesForTesting(parent.get(),
1615 identity_matrix,
1616 gfx::Point3F(),
1617 gfx::PointF(),
1618 gfx::Size(500, 500),
1619 true,
1620 false);
1621 SetLayerPropertiesForTesting(child.get(),
1622 identity_matrix,
1623 gfx::Point3F(),
1624 gfx::PointF(),
1625 gfx::Size(20, 20),
1626 true,
1627 false);
1628 SetLayerPropertiesForTesting(grand_child.get(),
1629 identity_matrix,
1630 gfx::Point3F(),
1631 gfx::PointF(45.f, 45.f),
1632 gfx::Size(10, 10),
1633 true,
1634 false);
1635 SetLayerPropertiesForTesting(great_grand_child.get(),
1636 identity_matrix,
1637 gfx::Point3F(),
1638 gfx::PointF(),
1639 gfx::Size(10, 10),
1640 true,
1641 false);
1642 SetLayerPropertiesForTesting(leaf_node1.get(),
1643 identity_matrix,
1644 gfx::Point3F(),
1645 gfx::PointF(),
1646 gfx::Size(500, 500),
1647 true,
1648 false);
1649 SetLayerPropertiesForTesting(leaf_node2.get(),
1650 identity_matrix,
1651 gfx::Point3F(),
1652 gfx::PointF(),
1653 gfx::Size(20, 20),
1654 true,
1655 false);
1657 child->SetMasksToBounds(true);
1658 child->SetOpacity(0.4f);
1659 child->SetForceRenderSurface(true);
1660 grand_child->SetOpacity(0.5f);
1661 great_grand_child->SetOpacity(0.4f);
1663 RenderSurfaceLayerList render_surface_layer_list;
1664 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1665 parent.get(), parent->bounds(), &render_surface_layer_list);
1666 inputs.can_adjust_raster_scales = true;
1667 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1669 ASSERT_EQ(2U, render_surface_layer_list.size());
1670 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1671 EXPECT_EQ(child->id(), render_surface_layer_list.at(1)->id());
1674 TEST_F(LayerTreeHostCommonTest, ClipRectCullsSurfaceWithoutVisibleContent) {
1675 // When a render surface has a clip rect, it is used to clip the content rect
1676 // of the surface. When the render surface is animating its transforms, then
1677 // the content rect's position in the clip rect is not defined on the main
1678 // thread, and its content rect should not be clipped.
1680 // The test tree is set up as follows:
1681 // - parent is a container layer that masksToBounds=true to cause clipping.
1682 // - child is a render surface, which has a clip rect set to the bounds of
1683 // the parent.
1684 // - grand_child is a render surface, and the only visible content in child.
1685 // It is positioned outside of the clip rect from parent.
1687 // In this configuration, grand_child should be outside the clipped
1688 // content rect of the child, making grand_child not appear in the
1689 // render_surface_layer_list. However, when we place an animation on the
1690 // child, this clipping should be avoided and we should keep the grand_child
1691 // in the render_surface_layer_list.
1693 const gfx::Transform identity_matrix;
1694 scoped_refptr<Layer> parent = Layer::Create();
1695 scoped_refptr<Layer> child = Layer::Create();
1696 scoped_refptr<Layer> grand_child = Layer::Create();
1697 scoped_refptr<LayerWithForcedDrawsContent> leaf_node =
1698 make_scoped_refptr(new LayerWithForcedDrawsContent());
1699 parent->AddChild(child);
1700 child->AddChild(grand_child);
1701 grand_child->AddChild(leaf_node);
1703 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1704 host->SetRootLayer(parent);
1706 SetLayerPropertiesForTesting(parent.get(),
1707 identity_matrix,
1708 gfx::Point3F(),
1709 gfx::PointF(),
1710 gfx::Size(100, 100),
1711 true,
1712 false);
1713 SetLayerPropertiesForTesting(child.get(),
1714 identity_matrix,
1715 gfx::Point3F(),
1716 gfx::PointF(),
1717 gfx::Size(20, 20),
1718 true,
1719 false);
1720 SetLayerPropertiesForTesting(grand_child.get(),
1721 identity_matrix,
1722 gfx::Point3F(),
1723 gfx::PointF(200.f, 200.f),
1724 gfx::Size(10, 10),
1725 true,
1726 false);
1727 SetLayerPropertiesForTesting(leaf_node.get(),
1728 identity_matrix,
1729 gfx::Point3F(),
1730 gfx::PointF(),
1731 gfx::Size(10, 10),
1732 true,
1733 false);
1735 parent->SetMasksToBounds(true);
1736 child->SetOpacity(0.4f);
1737 child->SetForceRenderSurface(true);
1738 grand_child->SetOpacity(0.4f);
1739 grand_child->SetForceRenderSurface(true);
1742 RenderSurfaceLayerList render_surface_layer_list;
1743 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1744 parent.get(), parent->bounds(), &render_surface_layer_list);
1745 inputs.can_adjust_raster_scales = true;
1746 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1748 // Without an animation, we should cull child and grand_child from the
1749 // render_surface_layer_list.
1750 ASSERT_EQ(1U, render_surface_layer_list.size());
1751 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1754 // Now put an animating transform on child.
1755 AddAnimatedTransformToController(
1756 child->layer_animation_controller(), 10.0, 30, 0);
1759 RenderSurfaceLayerList render_surface_layer_list;
1760 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1761 parent.get(), parent->bounds(), &render_surface_layer_list);
1762 inputs.can_adjust_raster_scales = true;
1763 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1765 // With an animating transform, we should keep child and grand_child in the
1766 // render_surface_layer_list.
1767 ASSERT_EQ(3U, render_surface_layer_list.size());
1768 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1769 EXPECT_EQ(child->id(), render_surface_layer_list.at(1)->id());
1770 EXPECT_EQ(grand_child->id(), render_surface_layer_list.at(2)->id());
1774 TEST_F(LayerTreeHostCommonTest, IsClippedIsSetCorrectly) {
1775 // Layer's IsClipped() property is set to true when:
1776 // - the layer clips its subtree, e.g. masks to bounds,
1777 // - the layer is clipped by an ancestor that contributes to the same
1778 // render target,
1779 // - a surface is clipped by an ancestor that contributes to the same
1780 // render target.
1782 // In particular, for a layer that owns a render surface:
1783 // - the render surface inherits any clip from ancestors, and does NOT
1784 // pass that clipped status to the layer itself.
1785 // - but if the layer itself masks to bounds, it is considered clipped
1786 // and propagates the clip to the subtree.
1788 const gfx::Transform identity_matrix;
1789 scoped_refptr<Layer> root = Layer::Create();
1790 scoped_refptr<Layer> parent = Layer::Create();
1791 scoped_refptr<Layer> child1 = Layer::Create();
1792 scoped_refptr<Layer> child2 = Layer::Create();
1793 scoped_refptr<Layer> grand_child = Layer::Create();
1794 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
1795 make_scoped_refptr(new LayerWithForcedDrawsContent());
1796 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
1797 make_scoped_refptr(new LayerWithForcedDrawsContent());
1798 root->AddChild(parent);
1799 parent->AddChild(child1);
1800 parent->AddChild(child2);
1801 child1->AddChild(grand_child);
1802 child2->AddChild(leaf_node2);
1803 grand_child->AddChild(leaf_node1);
1805 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1806 host->SetRootLayer(root);
1808 child2->SetForceRenderSurface(true);
1810 SetLayerPropertiesForTesting(root.get(),
1811 identity_matrix,
1812 gfx::Point3F(),
1813 gfx::PointF(),
1814 gfx::Size(100, 100),
1815 true,
1816 false);
1817 SetLayerPropertiesForTesting(parent.get(),
1818 identity_matrix,
1819 gfx::Point3F(),
1820 gfx::PointF(),
1821 gfx::Size(100, 100),
1822 true,
1823 false);
1824 SetLayerPropertiesForTesting(child1.get(),
1825 identity_matrix,
1826 gfx::Point3F(),
1827 gfx::PointF(),
1828 gfx::Size(100, 100),
1829 true,
1830 false);
1831 SetLayerPropertiesForTesting(child2.get(),
1832 identity_matrix,
1833 gfx::Point3F(),
1834 gfx::PointF(),
1835 gfx::Size(100, 100),
1836 true,
1837 false);
1838 SetLayerPropertiesForTesting(grand_child.get(),
1839 identity_matrix,
1840 gfx::Point3F(),
1841 gfx::PointF(),
1842 gfx::Size(100, 100),
1843 true,
1844 false);
1845 SetLayerPropertiesForTesting(leaf_node1.get(),
1846 identity_matrix,
1847 gfx::Point3F(),
1848 gfx::PointF(),
1849 gfx::Size(100, 100),
1850 true,
1851 false);
1852 SetLayerPropertiesForTesting(leaf_node2.get(),
1853 identity_matrix,
1854 gfx::Point3F(),
1855 gfx::PointF(),
1856 gfx::Size(100, 100),
1857 true,
1858 false);
1860 // Case 1: nothing is clipped except the root render surface.
1862 RenderSurfaceLayerList render_surface_layer_list;
1863 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1864 root.get(), parent->bounds(), &render_surface_layer_list);
1865 inputs.can_adjust_raster_scales = true;
1866 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1868 ASSERT_TRUE(root->render_surface());
1869 ASSERT_TRUE(child2->render_surface());
1871 EXPECT_FALSE(root->is_clipped());
1872 EXPECT_TRUE(root->render_surface()->is_clipped());
1873 EXPECT_FALSE(parent->is_clipped());
1874 EXPECT_FALSE(child1->is_clipped());
1875 EXPECT_FALSE(child2->is_clipped());
1876 EXPECT_FALSE(child2->render_surface()->is_clipped());
1877 EXPECT_FALSE(grand_child->is_clipped());
1878 EXPECT_FALSE(leaf_node1->is_clipped());
1879 EXPECT_FALSE(leaf_node2->is_clipped());
1882 // Case 2: parent masksToBounds, so the parent, child1, and child2's
1883 // surface are clipped. But layers that contribute to child2's surface are
1884 // not clipped explicitly because child2's surface already accounts for
1885 // that clip.
1887 RenderSurfaceLayerList render_surface_layer_list;
1888 parent->SetMasksToBounds(true);
1889 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1890 root.get(), parent->bounds(), &render_surface_layer_list);
1891 inputs.can_adjust_raster_scales = true;
1892 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1894 ASSERT_TRUE(root->render_surface());
1895 ASSERT_TRUE(child2->render_surface());
1897 EXPECT_FALSE(root->is_clipped());
1898 EXPECT_TRUE(root->render_surface()->is_clipped());
1899 EXPECT_TRUE(parent->is_clipped());
1900 EXPECT_TRUE(child1->is_clipped());
1901 EXPECT_FALSE(child2->is_clipped());
1902 EXPECT_TRUE(child2->render_surface()->is_clipped());
1903 EXPECT_TRUE(grand_child->is_clipped());
1904 EXPECT_TRUE(leaf_node1->is_clipped());
1905 EXPECT_FALSE(leaf_node2->is_clipped());
1908 // Case 3: child2 masksToBounds. The layer and subtree are clipped, and
1909 // child2's render surface is not clipped.
1911 RenderSurfaceLayerList render_surface_layer_list;
1912 parent->SetMasksToBounds(false);
1913 child2->SetMasksToBounds(true);
1914 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1915 root.get(), parent->bounds(), &render_surface_layer_list);
1916 inputs.can_adjust_raster_scales = true;
1917 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1919 ASSERT_TRUE(root->render_surface());
1920 ASSERT_TRUE(child2->render_surface());
1922 EXPECT_FALSE(root->is_clipped());
1923 EXPECT_TRUE(root->render_surface()->is_clipped());
1924 EXPECT_FALSE(parent->is_clipped());
1925 EXPECT_FALSE(child1->is_clipped());
1926 EXPECT_TRUE(child2->is_clipped());
1927 EXPECT_FALSE(child2->render_surface()->is_clipped());
1928 EXPECT_FALSE(grand_child->is_clipped());
1929 EXPECT_FALSE(leaf_node1->is_clipped());
1930 EXPECT_TRUE(leaf_node2->is_clipped());
1934 TEST_F(LayerTreeHostCommonTest, DrawableContentRectForLayers) {
1935 // Verify that layers get the appropriate DrawableContentRect when their
1936 // parent masksToBounds is true.
1938 // grand_child1 - completely inside the region; DrawableContentRect should
1939 // be the layer rect expressed in target space.
1940 // grand_child2 - partially clipped but NOT masksToBounds; the clip rect
1941 // will be the intersection of layer bounds and the mask region.
1942 // grand_child3 - partially clipped and masksToBounds; the
1943 // DrawableContentRect will still be the intersection of layer bounds and
1944 // the mask region.
1945 // grand_child4 - outside parent's clip rect; the DrawableContentRect should
1946 // be empty.
1949 const gfx::Transform identity_matrix;
1950 scoped_refptr<Layer> parent = Layer::Create();
1951 scoped_refptr<Layer> child = Layer::Create();
1952 scoped_refptr<Layer> grand_child1 = Layer::Create();
1953 scoped_refptr<Layer> grand_child2 = Layer::Create();
1954 scoped_refptr<Layer> grand_child3 = Layer::Create();
1955 scoped_refptr<Layer> grand_child4 = Layer::Create();
1957 parent->AddChild(child);
1958 child->AddChild(grand_child1);
1959 child->AddChild(grand_child2);
1960 child->AddChild(grand_child3);
1961 child->AddChild(grand_child4);
1963 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1964 host->SetRootLayer(parent);
1966 SetLayerPropertiesForTesting(parent.get(),
1967 identity_matrix,
1968 gfx::Point3F(),
1969 gfx::PointF(),
1970 gfx::Size(500, 500),
1971 true,
1972 false);
1973 SetLayerPropertiesForTesting(child.get(),
1974 identity_matrix,
1975 gfx::Point3F(),
1976 gfx::PointF(),
1977 gfx::Size(20, 20),
1978 true,
1979 false);
1980 SetLayerPropertiesForTesting(grand_child1.get(),
1981 identity_matrix,
1982 gfx::Point3F(),
1983 gfx::PointF(5.f, 5.f),
1984 gfx::Size(10, 10),
1985 true,
1986 false);
1987 SetLayerPropertiesForTesting(grand_child2.get(),
1988 identity_matrix,
1989 gfx::Point3F(),
1990 gfx::PointF(15.f, 15.f),
1991 gfx::Size(10, 10),
1992 true,
1993 false);
1994 SetLayerPropertiesForTesting(grand_child3.get(),
1995 identity_matrix,
1996 gfx::Point3F(),
1997 gfx::PointF(15.f, 15.f),
1998 gfx::Size(10, 10),
1999 true,
2000 false);
2001 SetLayerPropertiesForTesting(grand_child4.get(),
2002 identity_matrix,
2003 gfx::Point3F(),
2004 gfx::PointF(45.f, 45.f),
2005 gfx::Size(10, 10),
2006 true,
2007 false);
2009 child->SetMasksToBounds(true);
2010 grand_child3->SetMasksToBounds(true);
2012 // Force everyone to be a render surface.
2013 child->SetOpacity(0.4f);
2014 grand_child1->SetOpacity(0.5f);
2015 grand_child2->SetOpacity(0.5f);
2016 grand_child3->SetOpacity(0.5f);
2017 grand_child4->SetOpacity(0.5f);
2019 RenderSurfaceLayerList render_surface_layer_list;
2020 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2021 parent.get(), parent->bounds(), &render_surface_layer_list);
2022 inputs.can_adjust_raster_scales = true;
2023 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2025 EXPECT_EQ(gfx::Rect(5, 5, 10, 10), grand_child1->drawable_content_rect());
2026 EXPECT_EQ(gfx::Rect(15, 15, 5, 5), grand_child3->drawable_content_rect());
2027 EXPECT_EQ(gfx::Rect(15, 15, 5, 5), grand_child3->drawable_content_rect());
2028 EXPECT_TRUE(grand_child4->drawable_content_rect().IsEmpty());
2031 TEST_F(LayerTreeHostCommonTest, ClipRectIsPropagatedCorrectlyToSurfaces) {
2032 // Verify that render surfaces (and their layers) get the appropriate
2033 // clip rects when their parent masksToBounds is true.
2035 // Layers that own render surfaces (at least for now) do not inherit any
2036 // clipping; instead the surface will enforce the clip for the entire subtree.
2037 // They may still have a clip rect of their own layer bounds, however, if
2038 // masksToBounds was true.
2039 const gfx::Transform identity_matrix;
2040 scoped_refptr<Layer> parent = Layer::Create();
2041 scoped_refptr<Layer> child = Layer::Create();
2042 scoped_refptr<Layer> grand_child1 = Layer::Create();
2043 scoped_refptr<Layer> grand_child2 = Layer::Create();
2044 scoped_refptr<Layer> grand_child3 = Layer::Create();
2045 scoped_refptr<Layer> grand_child4 = Layer::Create();
2046 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
2047 make_scoped_refptr(new LayerWithForcedDrawsContent());
2048 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
2049 make_scoped_refptr(new LayerWithForcedDrawsContent());
2050 scoped_refptr<LayerWithForcedDrawsContent> leaf_node3 =
2051 make_scoped_refptr(new LayerWithForcedDrawsContent());
2052 scoped_refptr<LayerWithForcedDrawsContent> leaf_node4 =
2053 make_scoped_refptr(new LayerWithForcedDrawsContent());
2055 parent->AddChild(child);
2056 child->AddChild(grand_child1);
2057 child->AddChild(grand_child2);
2058 child->AddChild(grand_child3);
2059 child->AddChild(grand_child4);
2061 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2062 host->SetRootLayer(parent);
2064 // the leaf nodes ensure that these grand_children become render surfaces for
2065 // this test.
2066 grand_child1->AddChild(leaf_node1);
2067 grand_child2->AddChild(leaf_node2);
2068 grand_child3->AddChild(leaf_node3);
2069 grand_child4->AddChild(leaf_node4);
2071 SetLayerPropertiesForTesting(parent.get(),
2072 identity_matrix,
2073 gfx::Point3F(),
2074 gfx::PointF(),
2075 gfx::Size(500, 500),
2076 true,
2077 false);
2078 SetLayerPropertiesForTesting(child.get(),
2079 identity_matrix,
2080 gfx::Point3F(),
2081 gfx::PointF(),
2082 gfx::Size(20, 20),
2083 true,
2084 false);
2085 SetLayerPropertiesForTesting(grand_child1.get(),
2086 identity_matrix,
2087 gfx::Point3F(),
2088 gfx::PointF(5.f, 5.f),
2089 gfx::Size(10, 10),
2090 true,
2091 false);
2092 SetLayerPropertiesForTesting(grand_child2.get(),
2093 identity_matrix,
2094 gfx::Point3F(),
2095 gfx::PointF(15.f, 15.f),
2096 gfx::Size(10, 10),
2097 true,
2098 false);
2099 SetLayerPropertiesForTesting(grand_child3.get(),
2100 identity_matrix,
2101 gfx::Point3F(),
2102 gfx::PointF(15.f, 15.f),
2103 gfx::Size(10, 10),
2104 true,
2105 false);
2106 SetLayerPropertiesForTesting(grand_child4.get(),
2107 identity_matrix,
2108 gfx::Point3F(),
2109 gfx::PointF(45.f, 45.f),
2110 gfx::Size(10, 10),
2111 true,
2112 false);
2113 SetLayerPropertiesForTesting(leaf_node1.get(),
2114 identity_matrix,
2115 gfx::Point3F(),
2116 gfx::PointF(),
2117 gfx::Size(10, 10),
2118 true,
2119 false);
2120 SetLayerPropertiesForTesting(leaf_node2.get(),
2121 identity_matrix,
2122 gfx::Point3F(),
2123 gfx::PointF(),
2124 gfx::Size(10, 10),
2125 true,
2126 false);
2127 SetLayerPropertiesForTesting(leaf_node3.get(),
2128 identity_matrix,
2129 gfx::Point3F(),
2130 gfx::PointF(),
2131 gfx::Size(10, 10),
2132 true,
2133 false);
2134 SetLayerPropertiesForTesting(leaf_node4.get(),
2135 identity_matrix,
2136 gfx::Point3F(),
2137 gfx::PointF(),
2138 gfx::Size(10, 10),
2139 true,
2140 false);
2142 child->SetMasksToBounds(true);
2143 grand_child3->SetMasksToBounds(true);
2144 grand_child4->SetMasksToBounds(true);
2146 // Force everyone to be a render surface.
2147 child->SetOpacity(0.4f);
2148 child->SetForceRenderSurface(true);
2149 grand_child1->SetOpacity(0.5f);
2150 grand_child1->SetForceRenderSurface(true);
2151 grand_child2->SetOpacity(0.5f);
2152 grand_child2->SetForceRenderSurface(true);
2153 grand_child3->SetOpacity(0.5f);
2154 grand_child3->SetForceRenderSurface(true);
2155 grand_child4->SetOpacity(0.5f);
2156 grand_child4->SetForceRenderSurface(true);
2158 RenderSurfaceLayerList render_surface_layer_list;
2159 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2160 parent.get(), parent->bounds(), &render_surface_layer_list);
2161 inputs.can_adjust_raster_scales = true;
2162 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2163 ASSERT_TRUE(grand_child1->render_surface());
2164 ASSERT_TRUE(grand_child2->render_surface());
2165 ASSERT_TRUE(grand_child3->render_surface());
2167 // Surfaces are clipped by their parent, but un-affected by the owning layer's
2168 // masksToBounds.
2169 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2170 grand_child1->render_surface()->clip_rect());
2171 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2172 grand_child2->render_surface()->clip_rect());
2173 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2174 grand_child3->render_surface()->clip_rect());
2177 TEST_F(LayerTreeHostCommonTest, AnimationsForRenderSurfaceHierarchy) {
2178 scoped_refptr<Layer> parent = Layer::Create();
2179 scoped_refptr<Layer> render_surface1 = Layer::Create();
2180 scoped_refptr<Layer> render_surface2 = Layer::Create();
2181 scoped_refptr<Layer> child_of_root = Layer::Create();
2182 scoped_refptr<Layer> child_of_rs1 = Layer::Create();
2183 scoped_refptr<Layer> child_of_rs2 = Layer::Create();
2184 scoped_refptr<Layer> grand_child_of_root = Layer::Create();
2185 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs1 =
2186 make_scoped_refptr(new LayerWithForcedDrawsContent());
2187 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs2 =
2188 make_scoped_refptr(new LayerWithForcedDrawsContent());
2189 parent->AddChild(render_surface1);
2190 parent->AddChild(child_of_root);
2191 render_surface1->AddChild(child_of_rs1);
2192 render_surface1->AddChild(render_surface2);
2193 render_surface2->AddChild(child_of_rs2);
2194 child_of_root->AddChild(grand_child_of_root);
2195 child_of_rs1->AddChild(grand_child_of_rs1);
2196 child_of_rs2->AddChild(grand_child_of_rs2);
2198 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2199 host->SetRootLayer(parent);
2201 // Make our render surfaces.
2202 render_surface1->SetForceRenderSurface(true);
2203 render_surface2->SetForceRenderSurface(true);
2205 gfx::Transform layer_transform;
2206 layer_transform.Translate(1.0, 1.0);
2208 SetLayerPropertiesForTesting(parent.get(),
2209 layer_transform,
2210 gfx::Point3F(0.25f, 0.f, 0.f),
2211 gfx::PointF(2.5f, 0.f),
2212 gfx::Size(10, 10),
2213 true,
2214 false);
2215 SetLayerPropertiesForTesting(render_surface1.get(),
2216 layer_transform,
2217 gfx::Point3F(0.25f, 0.f, 0.f),
2218 gfx::PointF(2.5f, 0.f),
2219 gfx::Size(10, 10),
2220 true,
2221 false);
2222 SetLayerPropertiesForTesting(render_surface2.get(),
2223 layer_transform,
2224 gfx::Point3F(0.25f, 0.f, 0.f),
2225 gfx::PointF(2.5f, 0.f),
2226 gfx::Size(10, 10),
2227 true,
2228 false);
2229 SetLayerPropertiesForTesting(child_of_root.get(),
2230 layer_transform,
2231 gfx::Point3F(0.25f, 0.f, 0.f),
2232 gfx::PointF(2.5f, 0.f),
2233 gfx::Size(10, 10),
2234 true,
2235 false);
2236 SetLayerPropertiesForTesting(child_of_rs1.get(),
2237 layer_transform,
2238 gfx::Point3F(0.25f, 0.f, 0.f),
2239 gfx::PointF(2.5f, 0.f),
2240 gfx::Size(10, 10),
2241 true,
2242 false);
2243 SetLayerPropertiesForTesting(child_of_rs2.get(),
2244 layer_transform,
2245 gfx::Point3F(0.25f, 0.f, 0.f),
2246 gfx::PointF(2.5f, 0.f),
2247 gfx::Size(10, 10),
2248 true,
2249 false);
2250 SetLayerPropertiesForTesting(grand_child_of_root.get(),
2251 layer_transform,
2252 gfx::Point3F(0.25f, 0.f, 0.f),
2253 gfx::PointF(2.5f, 0.f),
2254 gfx::Size(10, 10),
2255 true,
2256 false);
2257 SetLayerPropertiesForTesting(grand_child_of_rs1.get(),
2258 layer_transform,
2259 gfx::Point3F(0.25f, 0.f, 0.f),
2260 gfx::PointF(2.5f, 0.f),
2261 gfx::Size(10, 10),
2262 true,
2263 false);
2264 SetLayerPropertiesForTesting(grand_child_of_rs2.get(),
2265 layer_transform,
2266 gfx::Point3F(0.25f, 0.f, 0.f),
2267 gfx::PointF(2.5f, 0.f),
2268 gfx::Size(10, 10),
2269 true,
2270 false);
2272 // Put an animated opacity on the render surface.
2273 AddOpacityTransitionToController(
2274 render_surface1->layer_animation_controller(), 10.0, 1.f, 0.f, false);
2276 // Also put an animated opacity on a layer without descendants.
2277 AddOpacityTransitionToController(
2278 grand_child_of_root->layer_animation_controller(), 10.0, 1.f, 0.f, false);
2280 // Put a transform animation on the render surface.
2281 AddAnimatedTransformToController(
2282 render_surface2->layer_animation_controller(), 10.0, 30, 0);
2284 // Also put transform animations on grand_child_of_root, and
2285 // grand_child_of_rs2
2286 AddAnimatedTransformToController(
2287 grand_child_of_root->layer_animation_controller(), 10.0, 30, 0);
2288 AddAnimatedTransformToController(
2289 grand_child_of_rs2->layer_animation_controller(), 10.0, 30, 0);
2291 ExecuteCalculateDrawProperties(parent.get());
2293 // Only layers that are associated with render surfaces should have an actual
2294 // RenderSurface() value.
2295 ASSERT_TRUE(parent->render_surface());
2296 ASSERT_FALSE(child_of_root->render_surface());
2297 ASSERT_FALSE(grand_child_of_root->render_surface());
2299 ASSERT_TRUE(render_surface1->render_surface());
2300 ASSERT_FALSE(child_of_rs1->render_surface());
2301 ASSERT_FALSE(grand_child_of_rs1->render_surface());
2303 ASSERT_TRUE(render_surface2->render_surface());
2304 ASSERT_FALSE(child_of_rs2->render_surface());
2305 ASSERT_FALSE(grand_child_of_rs2->render_surface());
2307 // Verify all render target accessors
2308 EXPECT_EQ(parent.get(), parent->render_target());
2309 EXPECT_EQ(parent.get(), child_of_root->render_target());
2310 EXPECT_EQ(parent.get(), grand_child_of_root->render_target());
2312 EXPECT_EQ(render_surface1.get(), render_surface1->render_target());
2313 EXPECT_EQ(render_surface1.get(), child_of_rs1->render_target());
2314 EXPECT_EQ(render_surface1.get(), grand_child_of_rs1->render_target());
2316 EXPECT_EQ(render_surface2.get(), render_surface2->render_target());
2317 EXPECT_EQ(render_surface2.get(), child_of_rs2->render_target());
2318 EXPECT_EQ(render_surface2.get(), grand_child_of_rs2->render_target());
2320 // Verify draw_opacity_is_animating values
2321 EXPECT_FALSE(parent->draw_opacity_is_animating());
2322 EXPECT_FALSE(child_of_root->draw_opacity_is_animating());
2323 EXPECT_TRUE(grand_child_of_root->draw_opacity_is_animating());
2324 EXPECT_FALSE(render_surface1->draw_opacity_is_animating());
2325 EXPECT_TRUE(render_surface1->render_surface()->draw_opacity_is_animating());
2326 EXPECT_FALSE(child_of_rs1->draw_opacity_is_animating());
2327 EXPECT_FALSE(grand_child_of_rs1->draw_opacity_is_animating());
2328 EXPECT_FALSE(render_surface2->draw_opacity_is_animating());
2329 EXPECT_FALSE(render_surface2->render_surface()->draw_opacity_is_animating());
2330 EXPECT_FALSE(child_of_rs2->draw_opacity_is_animating());
2331 EXPECT_FALSE(grand_child_of_rs2->draw_opacity_is_animating());
2333 // Verify draw_transform_is_animating values
2334 EXPECT_FALSE(parent->draw_transform_is_animating());
2335 EXPECT_FALSE(child_of_root->draw_transform_is_animating());
2336 EXPECT_TRUE(grand_child_of_root->draw_transform_is_animating());
2337 EXPECT_FALSE(render_surface1->draw_transform_is_animating());
2338 EXPECT_FALSE(render_surface1->render_surface()
2339 ->target_surface_transforms_are_animating());
2340 EXPECT_FALSE(child_of_rs1->draw_transform_is_animating());
2341 EXPECT_FALSE(grand_child_of_rs1->draw_transform_is_animating());
2342 EXPECT_FALSE(render_surface2->draw_transform_is_animating());
2343 EXPECT_TRUE(render_surface2->render_surface()
2344 ->target_surface_transforms_are_animating());
2345 EXPECT_FALSE(child_of_rs2->draw_transform_is_animating());
2346 EXPECT_TRUE(grand_child_of_rs2->draw_transform_is_animating());
2348 // Verify screen_space_transform_is_animating values
2349 EXPECT_FALSE(parent->screen_space_transform_is_animating());
2350 EXPECT_FALSE(child_of_root->screen_space_transform_is_animating());
2351 EXPECT_TRUE(grand_child_of_root->screen_space_transform_is_animating());
2352 EXPECT_FALSE(render_surface1->screen_space_transform_is_animating());
2353 EXPECT_FALSE(render_surface1->render_surface()
2354 ->screen_space_transforms_are_animating());
2355 EXPECT_FALSE(child_of_rs1->screen_space_transform_is_animating());
2356 EXPECT_FALSE(grand_child_of_rs1->screen_space_transform_is_animating());
2357 EXPECT_TRUE(render_surface2->screen_space_transform_is_animating());
2358 EXPECT_TRUE(render_surface2->render_surface()
2359 ->screen_space_transforms_are_animating());
2360 EXPECT_TRUE(child_of_rs2->screen_space_transform_is_animating());
2361 EXPECT_TRUE(grand_child_of_rs2->screen_space_transform_is_animating());
2363 // Sanity check. If these fail there is probably a bug in the test itself.
2364 // It is expected that we correctly set up transforms so that the y-component
2365 // of the screen-space transform encodes the "depth" of the layer in the tree.
2366 EXPECT_FLOAT_EQ(1.0, parent->screen_space_transform().matrix().get(1, 3));
2367 EXPECT_FLOAT_EQ(2.0,
2368 child_of_root->screen_space_transform().matrix().get(1, 3));
2369 EXPECT_FLOAT_EQ(
2370 3.0, grand_child_of_root->screen_space_transform().matrix().get(1, 3));
2372 EXPECT_FLOAT_EQ(2.0,
2373 render_surface1->screen_space_transform().matrix().get(1, 3));
2374 EXPECT_FLOAT_EQ(3.0,
2375 child_of_rs1->screen_space_transform().matrix().get(1, 3));
2376 EXPECT_FLOAT_EQ(
2377 4.0, grand_child_of_rs1->screen_space_transform().matrix().get(1, 3));
2379 EXPECT_FLOAT_EQ(3.0,
2380 render_surface2->screen_space_transform().matrix().get(1, 3));
2381 EXPECT_FLOAT_EQ(4.0,
2382 child_of_rs2->screen_space_transform().matrix().get(1, 3));
2383 EXPECT_FLOAT_EQ(
2384 5.0, grand_child_of_rs2->screen_space_transform().matrix().get(1, 3));
2387 TEST_F(LayerTreeHostCommonTest, VisibleRectForIdentityTransform) {
2388 // Test the calculateVisibleRect() function works correctly for identity
2389 // transforms.
2391 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2392 gfx::Transform layer_to_surface_transform;
2394 // Case 1: Layer is contained within the surface.
2395 gfx::Rect layer_content_rect = gfx::Rect(10, 10, 30, 30);
2396 gfx::Rect expected = gfx::Rect(10, 10, 30, 30);
2397 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2398 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2399 EXPECT_EQ(expected, actual);
2401 // Case 2: Layer is outside the surface rect.
2402 layer_content_rect = gfx::Rect(120, 120, 30, 30);
2403 actual = LayerTreeHostCommon::CalculateVisibleRect(
2404 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2405 EXPECT_TRUE(actual.IsEmpty());
2407 // Case 3: Layer is partially overlapping the surface rect.
2408 layer_content_rect = gfx::Rect(80, 80, 30, 30);
2409 expected = gfx::Rect(80, 80, 20, 20);
2410 actual = LayerTreeHostCommon::CalculateVisibleRect(
2411 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2412 EXPECT_EQ(expected, actual);
2415 TEST_F(LayerTreeHostCommonTest, VisibleRectForTranslations) {
2416 // Test the calculateVisibleRect() function works correctly for scaling
2417 // transforms.
2419 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2420 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
2421 gfx::Transform layer_to_surface_transform;
2423 // Case 1: Layer is contained within the surface.
2424 layer_to_surface_transform.MakeIdentity();
2425 layer_to_surface_transform.Translate(10.0, 10.0);
2426 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
2427 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2428 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2429 EXPECT_EQ(expected, actual);
2431 // Case 2: Layer is outside the surface rect.
2432 layer_to_surface_transform.MakeIdentity();
2433 layer_to_surface_transform.Translate(120.0, 120.0);
2434 actual = LayerTreeHostCommon::CalculateVisibleRect(
2435 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2436 EXPECT_TRUE(actual.IsEmpty());
2438 // Case 3: Layer is partially overlapping the surface rect.
2439 layer_to_surface_transform.MakeIdentity();
2440 layer_to_surface_transform.Translate(80.0, 80.0);
2441 expected = gfx::Rect(0, 0, 20, 20);
2442 actual = LayerTreeHostCommon::CalculateVisibleRect(
2443 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2444 EXPECT_EQ(expected, actual);
2447 TEST_F(LayerTreeHostCommonTest, VisibleRectFor2DRotations) {
2448 // Test the calculateVisibleRect() function works correctly for rotations
2449 // about z-axis (i.e. 2D rotations). Remember that calculateVisibleRect()
2450 // should return the g in the layer's space.
2452 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2453 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
2454 gfx::Transform layer_to_surface_transform;
2456 // Case 1: Layer is contained within the surface.
2457 layer_to_surface_transform.MakeIdentity();
2458 layer_to_surface_transform.Translate(50.0, 50.0);
2459 layer_to_surface_transform.Rotate(45.0);
2460 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
2461 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2462 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2463 EXPECT_EQ(expected, actual);
2465 // Case 2: Layer is outside the surface rect.
2466 layer_to_surface_transform.MakeIdentity();
2467 layer_to_surface_transform.Translate(-50.0, 0.0);
2468 layer_to_surface_transform.Rotate(45.0);
2469 actual = LayerTreeHostCommon::CalculateVisibleRect(
2470 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2471 EXPECT_TRUE(actual.IsEmpty());
2473 // Case 3: The layer is rotated about its top-left corner. In surface space,
2474 // the layer is oriented diagonally, with the left half outside of the render
2475 // surface. In this case, the g should still be the entire layer
2476 // (remember the g is computed in layer space); both the top-left
2477 // and bottom-right corners of the layer are still visible.
2478 layer_to_surface_transform.MakeIdentity();
2479 layer_to_surface_transform.Rotate(45.0);
2480 expected = gfx::Rect(0, 0, 30, 30);
2481 actual = LayerTreeHostCommon::CalculateVisibleRect(
2482 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2483 EXPECT_EQ(expected, actual);
2485 // Case 4: The layer is rotated about its top-left corner, and translated
2486 // upwards. In surface space, the layer is oriented diagonally, with only the
2487 // top corner of the surface overlapping the layer. In layer space, the render
2488 // surface overlaps the right side of the layer. The g should be
2489 // the layer's right half.
2490 layer_to_surface_transform.MakeIdentity();
2491 layer_to_surface_transform.Translate(0.0, -sqrt(2.0) * 15.0);
2492 layer_to_surface_transform.Rotate(45.0);
2493 expected = gfx::Rect(15, 0, 15, 30); // Right half of layer bounds.
2494 actual = LayerTreeHostCommon::CalculateVisibleRect(
2495 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2496 EXPECT_EQ(expected, actual);
2499 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dOrthographicTransform) {
2500 // Test that the calculateVisibleRect() function works correctly for 3d
2501 // transforms.
2503 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2504 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
2505 gfx::Transform layer_to_surface_transform;
2507 // Case 1: Orthographic projection of a layer rotated about y-axis by 45
2508 // degrees, should be fully contained in the render surface.
2509 layer_to_surface_transform.MakeIdentity();
2510 layer_to_surface_transform.RotateAboutYAxis(45.0);
2511 gfx::Rect expected = gfx::Rect(0, 0, 100, 100);
2512 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2513 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2514 EXPECT_EQ(expected, actual);
2516 // Case 2: Orthographic projection of a layer rotated about y-axis by 45
2517 // degrees, but shifted to the side so only the right-half the layer would be
2518 // visible on the surface.
2519 // 100 is the un-rotated layer width; divided by sqrt(2) is the rotated width.
2520 SkMScalar half_width_of_rotated_layer =
2521 SkDoubleToMScalar((100.0 / sqrt(2.0)) * 0.5);
2522 layer_to_surface_transform.MakeIdentity();
2523 layer_to_surface_transform.Translate(-half_width_of_rotated_layer, 0.0);
2524 layer_to_surface_transform.RotateAboutYAxis(45.0); // Rotates about the left
2525 // edge of the layer.
2526 expected = gfx::Rect(50, 0, 50, 100); // Tight half of the layer.
2527 actual = LayerTreeHostCommon::CalculateVisibleRect(
2528 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2529 EXPECT_EQ(expected, actual);
2532 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveTransform) {
2533 // Test the calculateVisibleRect() function works correctly when the layer has
2534 // a perspective projection onto the target surface.
2536 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2537 gfx::Rect layer_content_rect = gfx::Rect(-50, -50, 200, 200);
2538 gfx::Transform layer_to_surface_transform;
2540 // Case 1: Even though the layer is twice as large as the surface, due to
2541 // perspective foreshortening, the layer will fit fully in the surface when
2542 // its translated more than the perspective amount.
2543 layer_to_surface_transform.MakeIdentity();
2545 // The following sequence of transforms applies the perspective about the
2546 // center of the surface.
2547 layer_to_surface_transform.Translate(50.0, 50.0);
2548 layer_to_surface_transform.ApplyPerspectiveDepth(9.0);
2549 layer_to_surface_transform.Translate(-50.0, -50.0);
2551 // This translate places the layer in front of the surface's projection plane.
2552 layer_to_surface_transform.Translate3d(0.0, 0.0, -27.0);
2554 gfx::Rect expected = gfx::Rect(-50, -50, 200, 200);
2555 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2556 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2557 EXPECT_EQ(expected, actual);
2559 // Case 2: same projection as before, except that the layer is also translated
2560 // to the side, so that only the right half of the layer should be visible.
2562 // Explanation of expected result: The perspective ratio is (z distance
2563 // between layer and camera origin) / (z distance between projection plane and
2564 // camera origin) == ((-27 - 9) / 9) Then, by similar triangles, if we want to
2565 // move a layer by translating -50 units in projected surface units (so that
2566 // only half of it is visible), then we would need to translate by (-36 / 9) *
2567 // -50 == -200 in the layer's units.
2568 layer_to_surface_transform.Translate3d(-200.0, 0.0, 0.0);
2569 expected = gfx::Rect(gfx::Point(50, -50),
2570 gfx::Size(100, 200)); // The right half of the layer's
2571 // bounding rect.
2572 actual = LayerTreeHostCommon::CalculateVisibleRect(
2573 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2574 EXPECT_EQ(expected, actual);
2577 TEST_F(LayerTreeHostCommonTest,
2578 VisibleRectFor3dOrthographicIsNotClippedBehindSurface) {
2579 // There is currently no explicit concept of an orthographic projection plane
2580 // in our code (nor in the CSS spec to my knowledge). Therefore, layers that
2581 // are technically behind the surface in an orthographic world should not be
2582 // clipped when they are flattened to the surface.
2584 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2585 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
2586 gfx::Transform layer_to_surface_transform;
2588 // This sequence of transforms effectively rotates the layer about the y-axis
2589 // at the center of the layer.
2590 layer_to_surface_transform.MakeIdentity();
2591 layer_to_surface_transform.Translate(50.0, 0.0);
2592 layer_to_surface_transform.RotateAboutYAxis(45.0);
2593 layer_to_surface_transform.Translate(-50.0, 0.0);
2595 gfx::Rect expected = gfx::Rect(0, 0, 100, 100);
2596 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2597 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2598 EXPECT_EQ(expected, actual);
2601 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveWhenClippedByW) {
2602 // Test the calculateVisibleRect() function works correctly when projecting a
2603 // surface onto a layer, but the layer is partially behind the camera (not
2604 // just behind the projection plane). In this case, the cartesian coordinates
2605 // may seem to be valid, but actually they are not. The visible rect needs to
2606 // be properly clipped by the w = 0 plane in homogeneous coordinates before
2607 // converting to cartesian coordinates.
2609 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100);
2610 gfx::Rect layer_content_rect = gfx::Rect(-10, -1, 20, 2);
2611 gfx::Transform layer_to_surface_transform;
2613 // The layer is positioned so that the right half of the layer should be in
2614 // front of the camera, while the other half is behind the surface's
2615 // projection plane. The following sequence of transforms applies the
2616 // perspective and rotation about the center of the layer.
2617 layer_to_surface_transform.MakeIdentity();
2618 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2619 layer_to_surface_transform.Translate3d(-2.0, 0.0, 1.0);
2620 layer_to_surface_transform.RotateAboutYAxis(45.0);
2622 // Sanity check that this transform does indeed cause w < 0 when applying the
2623 // transform, otherwise this code is not testing the intended scenario.
2624 bool clipped;
2625 MathUtil::MapQuad(layer_to_surface_transform,
2626 gfx::QuadF(gfx::RectF(layer_content_rect)),
2627 &clipped);
2628 ASSERT_TRUE(clipped);
2630 int expected_x_position = 0;
2631 int expected_width = 10;
2632 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2633 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2634 EXPECT_EQ(expected_x_position, actual.x());
2635 EXPECT_EQ(expected_width, actual.width());
2638 TEST_F(LayerTreeHostCommonTest, VisibleRectForPerspectiveUnprojection) {
2639 // To determine visible rect in layer space, there needs to be an
2640 // un-projection from surface space to layer space. When the original
2641 // transform was a perspective projection that was clipped, it returns a rect
2642 // that encloses the clipped bounds. Un-projecting this new rect may require
2643 // clipping again.
2645 // This sequence of transforms causes one corner of the layer to protrude
2646 // across the w = 0 plane, and should be clipped.
2647 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100);
2648 gfx::Rect layer_content_rect = gfx::Rect(-10, -10, 20, 20);
2649 gfx::Transform layer_to_surface_transform;
2650 layer_to_surface_transform.MakeIdentity();
2651 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2652 layer_to_surface_transform.Translate3d(0.0, 0.0, -5.0);
2653 layer_to_surface_transform.RotateAboutYAxis(45.0);
2654 layer_to_surface_transform.RotateAboutXAxis(80.0);
2656 // Sanity check that un-projection does indeed cause w < 0, otherwise this
2657 // code is not testing the intended scenario.
2658 bool clipped;
2659 gfx::RectF clipped_rect =
2660 MathUtil::MapClippedRect(layer_to_surface_transform, layer_content_rect);
2661 MathUtil::ProjectQuad(
2662 Inverse(layer_to_surface_transform), gfx::QuadF(clipped_rect), &clipped);
2663 ASSERT_TRUE(clipped);
2665 // Only the corner of the layer is not visible on the surface because of being
2666 // clipped. But, the net result of rounding visible region to an axis-aligned
2667 // rect is that the entire layer should still be considered visible.
2668 gfx::Rect expected = gfx::Rect(-10, -10, 20, 20);
2669 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2670 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2671 EXPECT_EQ(expected, actual);
2674 TEST_F(LayerTreeHostCommonTest, DrawableAndVisibleContentRectsForSimpleLayers) {
2675 scoped_refptr<Layer> root = Layer::Create();
2676 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2677 make_scoped_refptr(new LayerWithForcedDrawsContent());
2678 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2679 make_scoped_refptr(new LayerWithForcedDrawsContent());
2680 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2681 make_scoped_refptr(new LayerWithForcedDrawsContent());
2682 root->AddChild(child1);
2683 root->AddChild(child2);
2684 root->AddChild(child3);
2686 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2687 host->SetRootLayer(root);
2689 gfx::Transform identity_matrix;
2690 SetLayerPropertiesForTesting(root.get(),
2691 identity_matrix,
2692 gfx::Point3F(),
2693 gfx::PointF(),
2694 gfx::Size(100, 100),
2695 true,
2696 false);
2697 SetLayerPropertiesForTesting(child1.get(),
2698 identity_matrix,
2699 gfx::Point3F(),
2700 gfx::PointF(),
2701 gfx::Size(50, 50),
2702 true,
2703 false);
2704 SetLayerPropertiesForTesting(child2.get(),
2705 identity_matrix,
2706 gfx::Point3F(),
2707 gfx::PointF(75.f, 75.f),
2708 gfx::Size(50, 50),
2709 true,
2710 false);
2711 SetLayerPropertiesForTesting(child3.get(),
2712 identity_matrix,
2713 gfx::Point3F(),
2714 gfx::PointF(125.f, 125.f),
2715 gfx::Size(50, 50),
2716 true,
2717 false);
2719 ExecuteCalculateDrawProperties(root.get());
2721 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2722 root->render_surface()->DrawableContentRect());
2723 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2725 // Layers that do not draw content should have empty visible_content_rects.
2726 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2728 // layer visible_content_rects are clipped by their target surface.
2729 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
2730 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_content_rect());
2731 EXPECT_TRUE(child3->visible_content_rect().IsEmpty());
2733 // layer drawable_content_rects are not clipped.
2734 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->drawable_content_rect());
2735 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
2736 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
2739 TEST_F(LayerTreeHostCommonTest,
2740 DrawableAndVisibleContentRectsForLayersClippedByLayer) {
2741 scoped_refptr<Layer> root = Layer::Create();
2742 scoped_refptr<Layer> child = Layer::Create();
2743 scoped_refptr<LayerWithForcedDrawsContent> grand_child1 =
2744 make_scoped_refptr(new LayerWithForcedDrawsContent());
2745 scoped_refptr<LayerWithForcedDrawsContent> grand_child2 =
2746 make_scoped_refptr(new LayerWithForcedDrawsContent());
2747 scoped_refptr<LayerWithForcedDrawsContent> grand_child3 =
2748 make_scoped_refptr(new LayerWithForcedDrawsContent());
2749 root->AddChild(child);
2750 child->AddChild(grand_child1);
2751 child->AddChild(grand_child2);
2752 child->AddChild(grand_child3);
2754 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2755 host->SetRootLayer(root);
2757 gfx::Transform identity_matrix;
2758 SetLayerPropertiesForTesting(root.get(),
2759 identity_matrix,
2760 gfx::Point3F(),
2761 gfx::PointF(),
2762 gfx::Size(100, 100),
2763 true,
2764 false);
2765 SetLayerPropertiesForTesting(child.get(),
2766 identity_matrix,
2767 gfx::Point3F(),
2768 gfx::PointF(),
2769 gfx::Size(100, 100),
2770 true,
2771 false);
2772 SetLayerPropertiesForTesting(grand_child1.get(),
2773 identity_matrix,
2774 gfx::Point3F(),
2775 gfx::PointF(5.f, 5.f),
2776 gfx::Size(50, 50),
2777 true,
2778 false);
2779 SetLayerPropertiesForTesting(grand_child2.get(),
2780 identity_matrix,
2781 gfx::Point3F(),
2782 gfx::PointF(75.f, 75.f),
2783 gfx::Size(50, 50),
2784 true,
2785 false);
2786 SetLayerPropertiesForTesting(grand_child3.get(),
2787 identity_matrix,
2788 gfx::Point3F(),
2789 gfx::PointF(125.f, 125.f),
2790 gfx::Size(50, 50),
2791 true,
2792 false);
2794 child->SetMasksToBounds(true);
2795 ExecuteCalculateDrawProperties(root.get());
2797 ASSERT_FALSE(child->render_surface());
2799 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2800 root->render_surface()->DrawableContentRect());
2801 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2803 // Layers that do not draw content should have empty visible content rects.
2804 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2805 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), child->visible_content_rect());
2807 // All grandchild visible content rects should be clipped by child.
2808 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), grand_child1->visible_content_rect());
2809 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), grand_child2->visible_content_rect());
2810 EXPECT_TRUE(grand_child3->visible_content_rect().IsEmpty());
2812 // All grandchild DrawableContentRects should also be clipped by child.
2813 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), grand_child1->drawable_content_rect());
2814 EXPECT_EQ(gfx::Rect(75, 75, 25, 25), grand_child2->drawable_content_rect());
2815 EXPECT_TRUE(grand_child3->drawable_content_rect().IsEmpty());
2818 TEST_F(LayerTreeHostCommonTest, VisibleContentRectWithClippingAndScaling) {
2819 scoped_refptr<Layer> root = Layer::Create();
2820 scoped_refptr<Layer> child = Layer::Create();
2821 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
2822 make_scoped_refptr(new LayerWithForcedDrawsContent());
2823 root->AddChild(child);
2824 child->AddChild(grand_child);
2826 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2827 host->SetRootLayer(root);
2829 gfx::Transform identity_matrix;
2830 gfx::Transform child_scale_matrix;
2831 child_scale_matrix.Scale(0.25f, 0.25f);
2832 gfx::Transform grand_child_scale_matrix;
2833 grand_child_scale_matrix.Scale(0.246f, 0.246f);
2834 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
2835 gfx::PointF(), gfx::Size(100, 100), true, false);
2836 SetLayerPropertiesForTesting(child.get(), child_scale_matrix, gfx::Point3F(),
2837 gfx::PointF(), gfx::Size(10, 10), true, false);
2838 SetLayerPropertiesForTesting(grand_child.get(), grand_child_scale_matrix,
2839 gfx::Point3F(), gfx::PointF(),
2840 gfx::Size(100, 100), true, false);
2842 child->SetMasksToBounds(true);
2843 ExecuteCalculateDrawProperties(root.get());
2845 // The visible rect is expanded to integer coordinates in target space before
2846 // being projected back to layer space, where it is once again expanded to
2847 // integer coordinates.
2848 EXPECT_EQ(gfx::Rect(49, 49), grand_child->visible_rect_from_property_trees());
2851 TEST_F(LayerTreeHostCommonTest,
2852 DrawableAndVisibleContentRectsForLayersInUnclippedRenderSurface) {
2853 scoped_refptr<Layer> root = Layer::Create();
2854 scoped_refptr<Layer> render_surface1 = Layer::Create();
2855 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2856 make_scoped_refptr(new LayerWithForcedDrawsContent());
2857 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2858 make_scoped_refptr(new LayerWithForcedDrawsContent());
2859 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2860 make_scoped_refptr(new LayerWithForcedDrawsContent());
2861 root->AddChild(render_surface1);
2862 render_surface1->AddChild(child1);
2863 render_surface1->AddChild(child2);
2864 render_surface1->AddChild(child3);
2866 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2867 host->SetRootLayer(root);
2869 gfx::Transform identity_matrix;
2870 SetLayerPropertiesForTesting(root.get(),
2871 identity_matrix,
2872 gfx::Point3F(),
2873 gfx::PointF(),
2874 gfx::Size(100, 100),
2875 true,
2876 false);
2877 SetLayerPropertiesForTesting(render_surface1.get(),
2878 identity_matrix,
2879 gfx::Point3F(),
2880 gfx::PointF(),
2881 gfx::Size(3, 4),
2882 true,
2883 false);
2884 SetLayerPropertiesForTesting(child1.get(),
2885 identity_matrix,
2886 gfx::Point3F(),
2887 gfx::PointF(5.f, 5.f),
2888 gfx::Size(50, 50),
2889 true,
2890 false);
2891 SetLayerPropertiesForTesting(child2.get(),
2892 identity_matrix,
2893 gfx::Point3F(),
2894 gfx::PointF(75.f, 75.f),
2895 gfx::Size(50, 50),
2896 true,
2897 false);
2898 SetLayerPropertiesForTesting(child3.get(),
2899 identity_matrix,
2900 gfx::Point3F(),
2901 gfx::PointF(125.f, 125.f),
2902 gfx::Size(50, 50),
2903 true,
2904 false);
2906 render_surface1->SetForceRenderSurface(true);
2907 ExecuteCalculateDrawProperties(root.get());
2909 ASSERT_TRUE(render_surface1->render_surface());
2911 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2912 root->render_surface()->DrawableContentRect());
2913 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2915 // Layers that do not draw content should have empty visible content rects.
2916 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2917 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
2919 // An unclipped surface grows its DrawableContentRect to include all drawable
2920 // regions of the subtree.
2921 EXPECT_EQ(gfx::Rect(5, 5, 170, 170),
2922 render_surface1->render_surface()->DrawableContentRect());
2924 // All layers that draw content into the unclipped surface are also unclipped.
2925 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
2926 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
2927 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
2929 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
2930 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
2931 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
2934 TEST_F(LayerTreeHostCommonTest,
2935 VisibleContentRectsForClippedSurfaceWithEmptyClip) {
2936 scoped_refptr<Layer> root = Layer::Create();
2937 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2938 make_scoped_refptr(new LayerWithForcedDrawsContent());
2939 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2940 make_scoped_refptr(new LayerWithForcedDrawsContent());
2941 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2942 make_scoped_refptr(new LayerWithForcedDrawsContent());
2943 root->AddChild(child1);
2944 root->AddChild(child2);
2945 root->AddChild(child3);
2947 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2948 host->SetRootLayer(root);
2950 gfx::Transform identity_matrix;
2951 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
2952 gfx::PointF(), gfx::Size(100, 100), true, false);
2953 SetLayerPropertiesForTesting(child1.get(), identity_matrix, gfx::Point3F(),
2954 gfx::PointF(5.f, 5.f), gfx::Size(50, 50), true,
2955 false);
2956 SetLayerPropertiesForTesting(child2.get(), identity_matrix, gfx::Point3F(),
2957 gfx::PointF(75.f, 75.f), gfx::Size(50, 50), true,
2958 false);
2959 SetLayerPropertiesForTesting(child3.get(), identity_matrix, gfx::Point3F(),
2960 gfx::PointF(125.f, 125.f), gfx::Size(50, 50),
2961 true, false);
2963 RenderSurfaceLayerList render_surface_layer_list;
2964 // Now set the root render surface an empty clip.
2965 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2966 root.get(), gfx::Size(), &render_surface_layer_list);
2968 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2969 ASSERT_TRUE(root->render_surface());
2970 EXPECT_FALSE(root->is_clipped());
2972 gfx::Rect empty;
2973 EXPECT_EQ(empty, root->render_surface()->clip_rect());
2974 EXPECT_TRUE(root->render_surface()->is_clipped());
2976 // Visible content rect calculation will check if the target surface is
2977 // clipped or not. An empty clip rect does not indicate the render surface
2978 // is unclipped.
2979 EXPECT_EQ(empty, child1->visible_content_rect());
2980 EXPECT_EQ(empty, child2->visible_content_rect());
2981 EXPECT_EQ(empty, child3->visible_content_rect());
2984 TEST_F(LayerTreeHostCommonTest,
2985 DrawableAndVisibleContentRectsForLayersWithUninvertibleTransform) {
2986 scoped_refptr<Layer> root = Layer::Create();
2987 scoped_refptr<LayerWithForcedDrawsContent> child =
2988 make_scoped_refptr(new LayerWithForcedDrawsContent());
2989 root->AddChild(child);
2991 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2992 host->SetRootLayer(root);
2994 // Case 1: a truly degenerate matrix
2995 gfx::Transform identity_matrix;
2996 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
2997 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
2999 SetLayerPropertiesForTesting(root.get(),
3000 identity_matrix,
3001 gfx::Point3F(),
3002 gfx::PointF(),
3003 gfx::Size(100, 100),
3004 true,
3005 false);
3006 SetLayerPropertiesForTesting(child.get(),
3007 uninvertible_matrix,
3008 gfx::Point3F(),
3009 gfx::PointF(5.f, 5.f),
3010 gfx::Size(50, 50),
3011 true,
3012 false);
3014 ExecuteCalculateDrawProperties(root.get());
3016 EXPECT_TRUE(child->visible_content_rect().IsEmpty());
3017 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3019 // Case 2: a matrix with flattened z, uninvertible and not visible according
3020 // to the CSS spec.
3021 uninvertible_matrix.MakeIdentity();
3022 uninvertible_matrix.matrix().set(2, 2, 0.0);
3023 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3025 SetLayerPropertiesForTesting(child.get(),
3026 uninvertible_matrix,
3027 gfx::Point3F(),
3028 gfx::PointF(5.f, 5.f),
3029 gfx::Size(50, 50),
3030 true,
3031 false);
3033 ExecuteCalculateDrawProperties(root.get());
3035 EXPECT_TRUE(child->visible_content_rect().IsEmpty());
3036 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3038 // Case 3: a matrix with flattened z, also uninvertible and not visible.
3039 uninvertible_matrix.MakeIdentity();
3040 uninvertible_matrix.Translate(500.0, 0.0);
3041 uninvertible_matrix.matrix().set(2, 2, 0.0);
3042 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3044 SetLayerPropertiesForTesting(child.get(),
3045 uninvertible_matrix,
3046 gfx::Point3F(),
3047 gfx::PointF(5.f, 5.f),
3048 gfx::Size(50, 50),
3049 true,
3050 false);
3052 ExecuteCalculateDrawProperties(root.get());
3054 EXPECT_TRUE(child->visible_content_rect().IsEmpty());
3055 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3058 TEST_F(LayerTreeHostCommonTest,
3059 SingularTransformDoesNotPreventClearingDrawProperties) {
3060 scoped_refptr<Layer> root = Layer::Create();
3061 scoped_refptr<LayerWithForcedDrawsContent> child =
3062 make_scoped_refptr(new LayerWithForcedDrawsContent());
3063 root->AddChild(child);
3065 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3066 host->SetRootLayer(root);
3068 gfx::Transform identity_matrix;
3069 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
3070 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3072 SetLayerPropertiesForTesting(root.get(),
3073 uninvertible_matrix,
3074 gfx::Point3F(),
3075 gfx::PointF(),
3076 gfx::Size(100, 100),
3077 true,
3078 false);
3079 SetLayerPropertiesForTesting(child.get(),
3080 identity_matrix,
3081 gfx::Point3F(),
3082 gfx::PointF(5.f, 5.f),
3083 gfx::Size(50, 50),
3084 true,
3085 false);
3087 child->draw_properties().sorted_for_recursion = true;
3089 TransformOperations start_transform_operations;
3090 start_transform_operations.AppendScale(1.f, 0.f, 0.f);
3092 TransformOperations end_transform_operations;
3093 end_transform_operations.AppendScale(1.f, 1.f, 0.f);
3095 AddAnimatedTransformToLayer(
3096 root.get(), 10.0, start_transform_operations, end_transform_operations);
3098 EXPECT_TRUE(root->TransformIsAnimating());
3100 ExecuteCalculateDrawProperties(root.get());
3102 EXPECT_FALSE(child->draw_properties().sorted_for_recursion);
3105 TEST_F(LayerTreeHostCommonTest,
3106 SingularNonAnimatingTransformDoesNotPreventClearingDrawProperties) {
3107 scoped_refptr<Layer> root = Layer::Create();
3109 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3110 host->SetRootLayer(root);
3112 gfx::Transform identity_matrix;
3113 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
3114 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3116 SetLayerPropertiesForTesting(root.get(),
3117 uninvertible_matrix,
3118 gfx::Point3F(),
3119 gfx::PointF(),
3120 gfx::Size(100, 100),
3121 true,
3122 false);
3124 root->draw_properties().sorted_for_recursion = true;
3126 EXPECT_FALSE(root->TransformIsAnimating());
3128 ExecuteCalculateDrawProperties(root.get());
3130 EXPECT_FALSE(root->draw_properties().sorted_for_recursion);
3133 TEST_F(LayerTreeHostCommonTest,
3134 DrawableAndVisibleContentRectsForLayersInClippedRenderSurface) {
3135 scoped_refptr<Layer> root = Layer::Create();
3136 scoped_refptr<Layer> render_surface1 = Layer::Create();
3137 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3138 make_scoped_refptr(new LayerWithForcedDrawsContent());
3139 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3140 make_scoped_refptr(new LayerWithForcedDrawsContent());
3141 scoped_refptr<LayerWithForcedDrawsContent> child3 =
3142 make_scoped_refptr(new LayerWithForcedDrawsContent());
3143 root->AddChild(render_surface1);
3144 render_surface1->AddChild(child1);
3145 render_surface1->AddChild(child2);
3146 render_surface1->AddChild(child3);
3148 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3149 host->SetRootLayer(root);
3151 gfx::Transform identity_matrix;
3152 SetLayerPropertiesForTesting(root.get(),
3153 identity_matrix,
3154 gfx::Point3F(),
3155 gfx::PointF(),
3156 gfx::Size(100, 100),
3157 true,
3158 false);
3159 SetLayerPropertiesForTesting(render_surface1.get(),
3160 identity_matrix,
3161 gfx::Point3F(),
3162 gfx::PointF(),
3163 gfx::Size(3, 4),
3164 true,
3165 false);
3166 SetLayerPropertiesForTesting(child1.get(),
3167 identity_matrix,
3168 gfx::Point3F(),
3169 gfx::PointF(5.f, 5.f),
3170 gfx::Size(50, 50),
3171 true,
3172 false);
3173 SetLayerPropertiesForTesting(child2.get(),
3174 identity_matrix,
3175 gfx::Point3F(),
3176 gfx::PointF(75.f, 75.f),
3177 gfx::Size(50, 50),
3178 true,
3179 false);
3180 SetLayerPropertiesForTesting(child3.get(),
3181 identity_matrix,
3182 gfx::Point3F(),
3183 gfx::PointF(125.f, 125.f),
3184 gfx::Size(50, 50),
3185 true,
3186 false);
3188 root->SetMasksToBounds(true);
3189 render_surface1->SetForceRenderSurface(true);
3190 ExecuteCalculateDrawProperties(root.get());
3192 ASSERT_TRUE(render_surface1->render_surface());
3194 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3195 root->render_surface()->DrawableContentRect());
3196 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3198 // Layers that do not draw content should have empty visible content rects.
3199 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3200 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
3202 // A clipped surface grows its DrawableContentRect to include all drawable
3203 // regions of the subtree, but also gets clamped by the ancestor's clip.
3204 EXPECT_EQ(gfx::Rect(5, 5, 95, 95),
3205 render_surface1->render_surface()->DrawableContentRect());
3207 // All layers that draw content into the surface have their visible content
3208 // rect clipped by the surface clip rect.
3209 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3210 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_content_rect());
3211 EXPECT_TRUE(child3->visible_content_rect().IsEmpty());
3213 // But the DrawableContentRects are unclipped.
3214 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3215 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3216 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3219 TEST_F(LayerTreeHostCommonTest,
3220 DrawableAndVisibleContentRectsForSurfaceHierarchy) {
3221 // Check that clipping does not propagate down surfaces.
3222 scoped_refptr<Layer> root = Layer::Create();
3223 scoped_refptr<Layer> render_surface1 = Layer::Create();
3224 scoped_refptr<Layer> render_surface2 = Layer::Create();
3225 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3226 make_scoped_refptr(new LayerWithForcedDrawsContent());
3227 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3228 make_scoped_refptr(new LayerWithForcedDrawsContent());
3229 scoped_refptr<LayerWithForcedDrawsContent> child3 =
3230 make_scoped_refptr(new LayerWithForcedDrawsContent());
3231 root->AddChild(render_surface1);
3232 render_surface1->AddChild(render_surface2);
3233 render_surface2->AddChild(child1);
3234 render_surface2->AddChild(child2);
3235 render_surface2->AddChild(child3);
3237 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3238 host->SetRootLayer(root);
3240 gfx::Transform identity_matrix;
3241 SetLayerPropertiesForTesting(root.get(),
3242 identity_matrix,
3243 gfx::Point3F(),
3244 gfx::PointF(),
3245 gfx::Size(100, 100),
3246 true,
3247 false);
3248 SetLayerPropertiesForTesting(render_surface1.get(),
3249 identity_matrix,
3250 gfx::Point3F(),
3251 gfx::PointF(),
3252 gfx::Size(3, 4),
3253 true,
3254 false);
3255 SetLayerPropertiesForTesting(render_surface2.get(),
3256 identity_matrix,
3257 gfx::Point3F(),
3258 gfx::PointF(),
3259 gfx::Size(7, 13),
3260 true,
3261 false);
3262 SetLayerPropertiesForTesting(child1.get(),
3263 identity_matrix,
3264 gfx::Point3F(),
3265 gfx::PointF(5.f, 5.f),
3266 gfx::Size(50, 50),
3267 true,
3268 false);
3269 SetLayerPropertiesForTesting(child2.get(),
3270 identity_matrix,
3271 gfx::Point3F(),
3272 gfx::PointF(75.f, 75.f),
3273 gfx::Size(50, 50),
3274 true,
3275 false);
3276 SetLayerPropertiesForTesting(child3.get(),
3277 identity_matrix,
3278 gfx::Point3F(),
3279 gfx::PointF(125.f, 125.f),
3280 gfx::Size(50, 50),
3281 true,
3282 false);
3284 root->SetMasksToBounds(true);
3285 render_surface1->SetForceRenderSurface(true);
3286 render_surface2->SetForceRenderSurface(true);
3287 ExecuteCalculateDrawProperties(root.get());
3289 ASSERT_TRUE(render_surface1->render_surface());
3290 ASSERT_TRUE(render_surface2->render_surface());
3292 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3293 root->render_surface()->DrawableContentRect());
3294 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3296 // Layers that do not draw content should have empty visible content rects.
3297 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3298 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
3299 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface2->visible_content_rect());
3301 // A clipped surface grows its DrawableContentRect to include all drawable
3302 // regions of the subtree, but also gets clamped by the ancestor's clip.
3303 EXPECT_EQ(gfx::Rect(5, 5, 95, 95),
3304 render_surface1->render_surface()->DrawableContentRect());
3306 // render_surface1 lives in the "unclipped universe" of render_surface1, and
3307 // is only implicitly clipped by render_surface1's content rect. So,
3308 // render_surface2 grows to enclose all drawable content of its subtree.
3309 EXPECT_EQ(gfx::Rect(5, 5, 170, 170),
3310 render_surface2->render_surface()->DrawableContentRect());
3312 // All layers that draw content into render_surface2 think they are unclipped.
3313 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3314 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
3315 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
3317 // DrawableContentRects are also unclipped.
3318 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3319 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3320 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3323 TEST_F(LayerTreeHostCommonTest,
3324 DrawableAndVisibleContentRectsWithTransformOnUnclippedSurface) {
3325 // Layers that have non-axis aligned bounds (due to transforms) have an
3326 // expanded, axis-aligned DrawableContentRect and visible content rect.
3328 scoped_refptr<Layer> root = Layer::Create();
3329 scoped_refptr<Layer> render_surface1 = Layer::Create();
3330 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3331 make_scoped_refptr(new LayerWithForcedDrawsContent());
3332 root->AddChild(render_surface1);
3333 render_surface1->AddChild(child1);
3335 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3336 host->SetRootLayer(root);
3338 gfx::Transform identity_matrix;
3339 gfx::Transform child_rotation;
3340 child_rotation.Rotate(45.0);
3341 SetLayerPropertiesForTesting(root.get(),
3342 identity_matrix,
3343 gfx::Point3F(),
3344 gfx::PointF(),
3345 gfx::Size(100, 100),
3346 true,
3347 false);
3348 SetLayerPropertiesForTesting(render_surface1.get(),
3349 identity_matrix,
3350 gfx::Point3F(),
3351 gfx::PointF(),
3352 gfx::Size(3, 4),
3353 true,
3354 false);
3355 SetLayerPropertiesForTesting(child1.get(),
3356 child_rotation,
3357 gfx::Point3F(25, 25, 0.f),
3358 gfx::PointF(25.f, 25.f),
3359 gfx::Size(50, 50),
3360 true,
3361 false);
3363 render_surface1->SetForceRenderSurface(true);
3364 ExecuteCalculateDrawProperties(root.get());
3366 ASSERT_TRUE(render_surface1->render_surface());
3368 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3369 root->render_surface()->DrawableContentRect());
3370 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3372 // Layers that do not draw content should have empty visible content rects.
3373 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3374 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
3376 // The unclipped surface grows its DrawableContentRect to include all drawable
3377 // regions of the subtree.
3378 int diagonal_radius = ceil(sqrt(2.0) * 25.0);
3379 gfx::Rect expected_surface_drawable_content =
3380 gfx::Rect(50 - diagonal_radius,
3381 50 - diagonal_radius,
3382 diagonal_radius * 2,
3383 diagonal_radius * 2);
3384 EXPECT_EQ(expected_surface_drawable_content,
3385 render_surface1->render_surface()->DrawableContentRect());
3387 // All layers that draw content into the unclipped surface are also unclipped.
3388 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3389 EXPECT_EQ(expected_surface_drawable_content, child1->drawable_content_rect());
3392 TEST_F(LayerTreeHostCommonTest,
3393 DrawableAndVisibleContentRectsWithTransformOnClippedSurface) {
3394 // Layers that have non-axis aligned bounds (due to transforms) have an
3395 // expanded, axis-aligned DrawableContentRect and visible content rect.
3397 scoped_refptr<Layer> root = Layer::Create();
3398 scoped_refptr<Layer> render_surface1 = Layer::Create();
3399 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3400 make_scoped_refptr(new LayerWithForcedDrawsContent());
3401 root->AddChild(render_surface1);
3402 render_surface1->AddChild(child1);
3404 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3405 host->SetRootLayer(root);
3407 gfx::Transform identity_matrix;
3408 gfx::Transform child_rotation;
3409 child_rotation.Rotate(45.0);
3410 SetLayerPropertiesForTesting(root.get(),
3411 identity_matrix,
3412 gfx::Point3F(),
3413 gfx::PointF(),
3414 gfx::Size(50, 50),
3415 true,
3416 false);
3417 SetLayerPropertiesForTesting(render_surface1.get(),
3418 identity_matrix,
3419 gfx::Point3F(),
3420 gfx::PointF(),
3421 gfx::Size(3, 4),
3422 true,
3423 false);
3425 SetLayerPropertiesForTesting(child1.get(),
3426 child_rotation,
3427 gfx::Point3F(25, 25, 0.f),
3428 gfx::PointF(25.f, 25.f),
3429 gfx::Size(50, 50),
3430 true,
3431 false);
3433 root->SetMasksToBounds(true);
3434 render_surface1->SetForceRenderSurface(true);
3435 ExecuteCalculateDrawProperties(root.get());
3437 ASSERT_TRUE(render_surface1->render_surface());
3439 // The clipped surface clamps the DrawableContentRect that encloses the
3440 // rotated layer.
3441 int diagonal_radius = ceil(sqrt(2.0) * 25.0);
3442 gfx::Rect unclipped_surface_content = gfx::Rect(50 - diagonal_radius,
3443 50 - diagonal_radius,
3444 diagonal_radius * 2,
3445 diagonal_radius * 2);
3446 gfx::Rect expected_surface_drawable_content =
3447 gfx::IntersectRects(unclipped_surface_content, gfx::Rect(0, 0, 50, 50));
3448 EXPECT_EQ(expected_surface_drawable_content,
3449 render_surface1->render_surface()->DrawableContentRect());
3451 // On the clipped surface, only a quarter of the child1 is visible, but when
3452 // rotating it back to child1's content space, the actual enclosing rect ends
3453 // up covering the full left half of child1.
3455 // Given the floating point math, this number is a little bit fuzzy.
3456 EXPECT_EQ(gfx::Rect(0, 0, 26, 50), child1->visible_content_rect());
3458 // The child's DrawableContentRect is unclipped.
3459 EXPECT_EQ(unclipped_surface_content, child1->drawable_content_rect());
3462 TEST_F(LayerTreeHostCommonTest, DrawableAndVisibleContentRectsInHighDPI) {
3463 MockContentLayerClient client;
3465 scoped_refptr<Layer> root = Layer::Create();
3466 scoped_refptr<FakePictureLayer> render_surface1 =
3467 CreateDrawablePictureLayer(&client);
3468 scoped_refptr<FakePictureLayer> render_surface2 =
3469 CreateDrawablePictureLayer(&client);
3470 scoped_refptr<FakePictureLayer> child1 = CreateDrawablePictureLayer(&client);
3471 scoped_refptr<FakePictureLayer> child2 = CreateDrawablePictureLayer(&client);
3472 scoped_refptr<FakePictureLayer> child3 = CreateDrawablePictureLayer(&client);
3473 root->AddChild(render_surface1);
3474 render_surface1->AddChild(render_surface2);
3475 render_surface2->AddChild(child1);
3476 render_surface2->AddChild(child2);
3477 render_surface2->AddChild(child3);
3479 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3480 host->SetRootLayer(root);
3482 gfx::Transform identity_matrix;
3483 SetLayerPropertiesForTesting(root.get(),
3484 identity_matrix,
3485 gfx::Point3F(),
3486 gfx::PointF(),
3487 gfx::Size(100, 100),
3488 true,
3489 false);
3490 SetLayerPropertiesForTesting(render_surface1.get(),
3491 identity_matrix,
3492 gfx::Point3F(),
3493 gfx::PointF(5.f, 5.f),
3494 gfx::Size(3, 4),
3495 true,
3496 false);
3497 SetLayerPropertiesForTesting(render_surface2.get(),
3498 identity_matrix,
3499 gfx::Point3F(),
3500 gfx::PointF(5.f, 5.f),
3501 gfx::Size(7, 13),
3502 true,
3503 false);
3504 SetLayerPropertiesForTesting(child1.get(),
3505 identity_matrix,
3506 gfx::Point3F(),
3507 gfx::PointF(5.f, 5.f),
3508 gfx::Size(50, 50),
3509 true,
3510 false);
3511 SetLayerPropertiesForTesting(child2.get(),
3512 identity_matrix,
3513 gfx::Point3F(),
3514 gfx::PointF(75.f, 75.f),
3515 gfx::Size(50, 50),
3516 true,
3517 false);
3518 SetLayerPropertiesForTesting(child3.get(),
3519 identity_matrix,
3520 gfx::Point3F(),
3521 gfx::PointF(125.f, 125.f),
3522 gfx::Size(50, 50),
3523 true,
3524 false);
3526 float device_scale_factor = 2.f;
3528 root->SetMasksToBounds(true);
3529 render_surface1->SetForceRenderSurface(true);
3530 render_surface2->SetForceRenderSurface(true);
3531 ExecuteCalculateDrawProperties(root.get(), device_scale_factor);
3533 ASSERT_TRUE(render_surface1->render_surface());
3534 ASSERT_TRUE(render_surface2->render_surface());
3536 // drawable_content_rects for all layers and surfaces are scaled by
3537 // device_scale_factor.
3538 EXPECT_EQ(gfx::Rect(0, 0, 200, 200),
3539 root->render_surface()->DrawableContentRect());
3540 EXPECT_EQ(gfx::Rect(0, 0, 200, 200), root->drawable_content_rect());
3541 EXPECT_EQ(gfx::Rect(10, 10, 190, 190),
3542 render_surface1->render_surface()->DrawableContentRect());
3544 // render_surface2 lives in the "unclipped universe" of render_surface1, and
3545 // is only implicitly clipped by render_surface1.
3546 EXPECT_EQ(gfx::Rect(10, 10, 350, 350),
3547 render_surface2->render_surface()->DrawableContentRect());
3549 EXPECT_EQ(gfx::Rect(10, 10, 100, 100), child1->drawable_content_rect());
3550 EXPECT_EQ(gfx::Rect(150, 150, 100, 100), child2->drawable_content_rect());
3551 EXPECT_EQ(gfx::Rect(250, 250, 100, 100), child3->drawable_content_rect());
3553 // The root layer does not actually draw content of its own.
3554 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3556 // All layer visible content rects are not expressed in content space of each
3557 // layer, so they are not scaled by the device_scale_factor.
3558 EXPECT_EQ(gfx::Rect(0, 0, 3, 4), render_surface1->visible_content_rect());
3559 EXPECT_EQ(gfx::Rect(0, 0, 7, 13), render_surface2->visible_content_rect());
3560 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3561 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
3562 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
3565 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithoutPreserves3d) {
3566 // Verify the behavior of back-face culling when there are no preserve-3d
3567 // layers. Note that 3d transforms still apply in this case, but they are
3568 // "flattened" to each parent layer according to current W3C spec.
3570 const gfx::Transform identity_matrix;
3571 scoped_refptr<Layer> parent = Layer::Create();
3572 scoped_refptr<LayerWithForcedDrawsContent> front_facing_child =
3573 make_scoped_refptr(new LayerWithForcedDrawsContent());
3574 scoped_refptr<LayerWithForcedDrawsContent> back_facing_child =
3575 make_scoped_refptr(new LayerWithForcedDrawsContent());
3576 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3577 make_scoped_refptr(new LayerWithForcedDrawsContent());
3578 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3579 make_scoped_refptr(new LayerWithForcedDrawsContent());
3580 scoped_refptr<LayerWithForcedDrawsContent>
3581 front_facing_child_of_front_facing_surface =
3582 make_scoped_refptr(new LayerWithForcedDrawsContent());
3583 scoped_refptr<LayerWithForcedDrawsContent>
3584 back_facing_child_of_front_facing_surface =
3585 make_scoped_refptr(new LayerWithForcedDrawsContent());
3586 scoped_refptr<LayerWithForcedDrawsContent>
3587 front_facing_child_of_back_facing_surface =
3588 make_scoped_refptr(new LayerWithForcedDrawsContent());
3589 scoped_refptr<LayerWithForcedDrawsContent>
3590 back_facing_child_of_back_facing_surface =
3591 make_scoped_refptr(new LayerWithForcedDrawsContent());
3593 parent->AddChild(front_facing_child);
3594 parent->AddChild(back_facing_child);
3595 parent->AddChild(front_facing_surface);
3596 parent->AddChild(back_facing_surface);
3597 front_facing_surface->AddChild(front_facing_child_of_front_facing_surface);
3598 front_facing_surface->AddChild(back_facing_child_of_front_facing_surface);
3599 back_facing_surface->AddChild(front_facing_child_of_back_facing_surface);
3600 back_facing_surface->AddChild(back_facing_child_of_back_facing_surface);
3602 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3603 host->SetRootLayer(parent);
3605 // Nothing is double-sided
3606 front_facing_child->SetDoubleSided(false);
3607 back_facing_child->SetDoubleSided(false);
3608 front_facing_surface->SetDoubleSided(false);
3609 back_facing_surface->SetDoubleSided(false);
3610 front_facing_child_of_front_facing_surface->SetDoubleSided(false);
3611 back_facing_child_of_front_facing_surface->SetDoubleSided(false);
3612 front_facing_child_of_back_facing_surface->SetDoubleSided(false);
3613 back_facing_child_of_back_facing_surface->SetDoubleSided(false);
3615 gfx::Transform backface_matrix;
3616 backface_matrix.Translate(50.0, 50.0);
3617 backface_matrix.RotateAboutYAxis(180.0);
3618 backface_matrix.Translate(-50.0, -50.0);
3620 // Having a descendant and opacity will force these to have render surfaces.
3621 front_facing_surface->SetOpacity(0.5f);
3622 back_facing_surface->SetOpacity(0.5f);
3624 // Nothing preserves 3d. According to current W3C CSS gfx::Transforms spec,
3625 // these layers should blindly use their own local transforms to determine
3626 // back-face culling.
3627 SetLayerPropertiesForTesting(parent.get(),
3628 identity_matrix,
3629 gfx::Point3F(),
3630 gfx::PointF(),
3631 gfx::Size(100, 100),
3632 true,
3633 false);
3634 SetLayerPropertiesForTesting(front_facing_child.get(),
3635 identity_matrix,
3636 gfx::Point3F(),
3637 gfx::PointF(),
3638 gfx::Size(100, 100),
3639 true,
3640 false);
3641 SetLayerPropertiesForTesting(back_facing_child.get(),
3642 backface_matrix,
3643 gfx::Point3F(),
3644 gfx::PointF(),
3645 gfx::Size(100, 100),
3646 true,
3647 false);
3648 SetLayerPropertiesForTesting(front_facing_surface.get(),
3649 identity_matrix,
3650 gfx::Point3F(),
3651 gfx::PointF(),
3652 gfx::Size(100, 100),
3653 true,
3654 false);
3655 SetLayerPropertiesForTesting(back_facing_surface.get(),
3656 backface_matrix,
3657 gfx::Point3F(),
3658 gfx::PointF(),
3659 gfx::Size(100, 100),
3660 true,
3661 false);
3662 SetLayerPropertiesForTesting(front_facing_child_of_front_facing_surface.get(),
3663 identity_matrix,
3664 gfx::Point3F(),
3665 gfx::PointF(),
3666 gfx::Size(100, 100),
3667 true,
3668 false);
3669 SetLayerPropertiesForTesting(back_facing_child_of_front_facing_surface.get(),
3670 backface_matrix,
3671 gfx::Point3F(),
3672 gfx::PointF(),
3673 gfx::Size(100, 100),
3674 true,
3675 false);
3676 SetLayerPropertiesForTesting(front_facing_child_of_back_facing_surface.get(),
3677 identity_matrix,
3678 gfx::Point3F(),
3679 gfx::PointF(),
3680 gfx::Size(100, 100),
3681 true,
3682 false);
3683 SetLayerPropertiesForTesting(back_facing_child_of_back_facing_surface.get(),
3684 backface_matrix,
3685 gfx::Point3F(),
3686 gfx::PointF(),
3687 gfx::Size(100, 100),
3688 true,
3689 false);
3691 RenderSurfaceLayerList render_surface_layer_list;
3692 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
3693 parent.get(), parent->bounds(), &render_surface_layer_list);
3694 inputs.can_adjust_raster_scales = true;
3695 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
3697 // Verify which render surfaces were created.
3698 EXPECT_FALSE(front_facing_child->render_surface());
3699 EXPECT_FALSE(back_facing_child->render_surface());
3700 EXPECT_TRUE(front_facing_surface->render_surface());
3701 EXPECT_TRUE(back_facing_surface->render_surface());
3702 EXPECT_FALSE(front_facing_child_of_front_facing_surface->render_surface());
3703 EXPECT_FALSE(back_facing_child_of_front_facing_surface->render_surface());
3704 EXPECT_FALSE(front_facing_child_of_back_facing_surface->render_surface());
3705 EXPECT_FALSE(back_facing_child_of_back_facing_surface->render_surface());
3707 // Verify the render_surface_layer_list.
3708 ASSERT_EQ(3u, render_surface_layer_list.size());
3709 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
3710 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list.at(1)->id());
3711 // Even though the back facing surface LAYER gets culled, the other
3712 // descendants should still be added, so the SURFACE should not be culled.
3713 EXPECT_EQ(back_facing_surface->id(), render_surface_layer_list.at(2)->id());
3715 // Verify root surface's layer list.
3716 ASSERT_EQ(
3718 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
3719 EXPECT_EQ(front_facing_child->id(),
3720 render_surface_layer_list.at(0)
3721 ->render_surface()
3722 ->layer_list()
3723 .at(0)
3724 ->id());
3725 EXPECT_EQ(front_facing_surface->id(),
3726 render_surface_layer_list.at(0)
3727 ->render_surface()
3728 ->layer_list()
3729 .at(1)
3730 ->id());
3731 EXPECT_EQ(back_facing_surface->id(),
3732 render_surface_layer_list.at(0)
3733 ->render_surface()
3734 ->layer_list()
3735 .at(2)
3736 ->id());
3738 // Verify front_facing_surface's layer list.
3739 ASSERT_EQ(
3741 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
3742 EXPECT_EQ(front_facing_surface->id(),
3743 render_surface_layer_list.at(1)
3744 ->render_surface()
3745 ->layer_list()
3746 .at(0)
3747 ->id());
3748 EXPECT_EQ(front_facing_child_of_front_facing_surface->id(),
3749 render_surface_layer_list.at(1)
3750 ->render_surface()
3751 ->layer_list()
3752 .at(1)
3753 ->id());
3755 // Verify back_facing_surface's layer list; its own layer should be culled
3756 // from the surface list.
3757 ASSERT_EQ(
3759 render_surface_layer_list.at(2)->render_surface()->layer_list().size());
3760 EXPECT_EQ(front_facing_child_of_back_facing_surface->id(),
3761 render_surface_layer_list.at(2)
3762 ->render_surface()
3763 ->layer_list()
3764 .at(0)
3765 ->id());
3768 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithPreserves3d) {
3769 // Verify the behavior of back-face culling when preserves-3d transform style
3770 // is used.
3772 const gfx::Transform identity_matrix;
3773 scoped_refptr<Layer> parent = Layer::Create();
3774 scoped_refptr<LayerWithForcedDrawsContent> front_facing_child =
3775 make_scoped_refptr(new LayerWithForcedDrawsContent());
3776 scoped_refptr<LayerWithForcedDrawsContent> back_facing_child =
3777 make_scoped_refptr(new LayerWithForcedDrawsContent());
3778 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3779 make_scoped_refptr(new LayerWithForcedDrawsContent());
3780 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3781 make_scoped_refptr(new LayerWithForcedDrawsContent());
3782 scoped_refptr<LayerWithForcedDrawsContent>
3783 front_facing_child_of_front_facing_surface =
3784 make_scoped_refptr(new LayerWithForcedDrawsContent());
3785 scoped_refptr<LayerWithForcedDrawsContent>
3786 back_facing_child_of_front_facing_surface =
3787 make_scoped_refptr(new LayerWithForcedDrawsContent());
3788 scoped_refptr<LayerWithForcedDrawsContent>
3789 front_facing_child_of_back_facing_surface =
3790 make_scoped_refptr(new LayerWithForcedDrawsContent());
3791 scoped_refptr<LayerWithForcedDrawsContent>
3792 back_facing_child_of_back_facing_surface =
3793 make_scoped_refptr(new LayerWithForcedDrawsContent());
3794 scoped_refptr<LayerWithForcedDrawsContent> dummy_replica_layer1 =
3795 make_scoped_refptr(new LayerWithForcedDrawsContent());
3796 scoped_refptr<LayerWithForcedDrawsContent> dummy_replica_layer2 =
3797 make_scoped_refptr(new LayerWithForcedDrawsContent());
3799 parent->AddChild(front_facing_child);
3800 parent->AddChild(back_facing_child);
3801 parent->AddChild(front_facing_surface);
3802 parent->AddChild(back_facing_surface);
3803 front_facing_surface->AddChild(front_facing_child_of_front_facing_surface);
3804 front_facing_surface->AddChild(back_facing_child_of_front_facing_surface);
3805 back_facing_surface->AddChild(front_facing_child_of_back_facing_surface);
3806 back_facing_surface->AddChild(back_facing_child_of_back_facing_surface);
3808 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3809 host->SetRootLayer(parent);
3811 // Nothing is double-sided
3812 front_facing_child->SetDoubleSided(false);
3813 back_facing_child->SetDoubleSided(false);
3814 front_facing_surface->SetDoubleSided(false);
3815 back_facing_surface->SetDoubleSided(false);
3816 front_facing_child_of_front_facing_surface->SetDoubleSided(false);
3817 back_facing_child_of_front_facing_surface->SetDoubleSided(false);
3818 front_facing_child_of_back_facing_surface->SetDoubleSided(false);
3819 back_facing_child_of_back_facing_surface->SetDoubleSided(false);
3821 gfx::Transform backface_matrix;
3822 backface_matrix.Translate(50.0, 50.0);
3823 backface_matrix.RotateAboutYAxis(180.0);
3824 backface_matrix.Translate(-50.0, -50.0);
3826 // Opacity will not force creation of render surfaces in this case because of
3827 // the preserve-3d transform style. Instead, an example of when a surface
3828 // would be created with preserve-3d is when there is a replica layer.
3829 front_facing_surface->SetReplicaLayer(dummy_replica_layer1.get());
3830 back_facing_surface->SetReplicaLayer(dummy_replica_layer2.get());
3832 // Each surface creates its own new 3d rendering context (as defined by W3C
3833 // spec). According to current W3C CSS gfx::Transforms spec, layers in a 3d
3834 // rendering context should use the transform with respect to that context.
3835 // This 3d rendering context occurs when (a) parent's transform style is flat
3836 // and (b) the layer's transform style is preserve-3d.
3837 SetLayerPropertiesForTesting(parent.get(),
3838 identity_matrix,
3839 gfx::Point3F(),
3840 gfx::PointF(),
3841 gfx::Size(100, 100),
3842 true,
3843 false); // parent transform style is flat.
3844 SetLayerPropertiesForTesting(front_facing_child.get(),
3845 identity_matrix,
3846 gfx::Point3F(),
3847 gfx::PointF(),
3848 gfx::Size(100, 100),
3849 true,
3850 false);
3851 SetLayerPropertiesForTesting(back_facing_child.get(),
3852 backface_matrix,
3853 gfx::Point3F(),
3854 gfx::PointF(),
3855 gfx::Size(100, 100),
3856 true,
3857 false);
3858 // surface transform style is preserve-3d.
3859 SetLayerPropertiesForTesting(front_facing_surface.get(),
3860 identity_matrix,
3861 gfx::Point3F(),
3862 gfx::PointF(),
3863 gfx::Size(100, 100),
3864 false,
3865 true);
3866 // surface transform style is preserve-3d.
3867 SetLayerPropertiesForTesting(back_facing_surface.get(),
3868 backface_matrix,
3869 gfx::Point3F(),
3870 gfx::PointF(),
3871 gfx::Size(100, 100),
3872 false,
3873 true);
3874 SetLayerPropertiesForTesting(front_facing_child_of_front_facing_surface.get(),
3875 identity_matrix,
3876 gfx::Point3F(),
3877 gfx::PointF(),
3878 gfx::Size(100, 100),
3879 true,
3880 true);
3881 SetLayerPropertiesForTesting(back_facing_child_of_front_facing_surface.get(),
3882 backface_matrix,
3883 gfx::Point3F(),
3884 gfx::PointF(),
3885 gfx::Size(100, 100),
3886 true,
3887 true);
3888 SetLayerPropertiesForTesting(front_facing_child_of_back_facing_surface.get(),
3889 identity_matrix,
3890 gfx::Point3F(),
3891 gfx::PointF(),
3892 gfx::Size(100, 100),
3893 true,
3894 true);
3895 SetLayerPropertiesForTesting(back_facing_child_of_back_facing_surface.get(),
3896 backface_matrix,
3897 gfx::Point3F(),
3898 gfx::PointF(),
3899 gfx::Size(100, 100),
3900 true,
3901 true);
3903 RenderSurfaceLayerList render_surface_layer_list;
3904 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
3905 parent.get(), parent->bounds(), &render_surface_layer_list);
3906 inputs.can_adjust_raster_scales = true;
3907 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
3909 // Verify which render surfaces were created and used.
3910 EXPECT_FALSE(front_facing_child->render_surface());
3911 EXPECT_FALSE(back_facing_child->render_surface());
3912 EXPECT_TRUE(front_facing_surface->render_surface());
3913 EXPECT_NE(back_facing_surface->render_target(), back_facing_surface);
3914 // We expect that a render_surface was created but not used.
3915 EXPECT_TRUE(back_facing_surface->render_surface());
3916 EXPECT_FALSE(front_facing_child_of_front_facing_surface->render_surface());
3917 EXPECT_FALSE(back_facing_child_of_front_facing_surface->render_surface());
3918 EXPECT_FALSE(front_facing_child_of_back_facing_surface->render_surface());
3919 EXPECT_FALSE(back_facing_child_of_back_facing_surface->render_surface());
3921 // Verify the render_surface_layer_list. The back-facing surface should be
3922 // culled.
3923 ASSERT_EQ(2u, render_surface_layer_list.size());
3924 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
3925 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list.at(1)->id());
3927 // Verify root surface's layer list.
3928 ASSERT_EQ(
3930 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
3931 EXPECT_EQ(front_facing_child->id(),
3932 render_surface_layer_list.at(0)
3933 ->render_surface()->layer_list().at(0)->id());
3934 EXPECT_EQ(front_facing_surface->id(),
3935 render_surface_layer_list.at(0)
3936 ->render_surface()->layer_list().at(1)->id());
3938 // Verify front_facing_surface's layer list.
3939 ASSERT_EQ(
3941 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
3942 EXPECT_EQ(front_facing_surface->id(),
3943 render_surface_layer_list.at(1)
3944 ->render_surface()->layer_list().at(0)->id());
3945 EXPECT_EQ(front_facing_child_of_front_facing_surface->id(),
3946 render_surface_layer_list.at(1)
3947 ->render_surface()->layer_list().at(1)->id());
3950 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithAnimatingTransforms) {
3951 // Verify that layers are appropriately culled when their back face is showing
3952 // and they are not double sided, while animations are going on.
3954 // Layers that are animating do not get culled on the main thread, as their
3955 // transforms should be treated as "unknown" so we can not be sure that their
3956 // back face is really showing.
3957 const gfx::Transform identity_matrix;
3958 scoped_refptr<Layer> parent = Layer::Create();
3959 scoped_refptr<LayerWithForcedDrawsContent> child =
3960 make_scoped_refptr(new LayerWithForcedDrawsContent());
3961 scoped_refptr<LayerWithForcedDrawsContent> animating_surface =
3962 make_scoped_refptr(new LayerWithForcedDrawsContent());
3963 scoped_refptr<LayerWithForcedDrawsContent> child_of_animating_surface =
3964 make_scoped_refptr(new LayerWithForcedDrawsContent());
3965 scoped_refptr<LayerWithForcedDrawsContent> animating_child =
3966 make_scoped_refptr(new LayerWithForcedDrawsContent());
3967 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3968 make_scoped_refptr(new LayerWithForcedDrawsContent());
3970 parent->AddChild(child);
3971 parent->AddChild(animating_surface);
3972 animating_surface->AddChild(child_of_animating_surface);
3973 parent->AddChild(animating_child);
3974 parent->AddChild(child2);
3976 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3977 host->SetRootLayer(parent);
3979 // Nothing is double-sided
3980 child->SetDoubleSided(false);
3981 child2->SetDoubleSided(false);
3982 animating_surface->SetDoubleSided(false);
3983 child_of_animating_surface->SetDoubleSided(false);
3984 animating_child->SetDoubleSided(false);
3986 gfx::Transform backface_matrix;
3987 backface_matrix.Translate(50.0, 50.0);
3988 backface_matrix.RotateAboutYAxis(180.0);
3989 backface_matrix.Translate(-50.0, -50.0);
3991 // Make our render surface.
3992 animating_surface->SetForceRenderSurface(true);
3994 // Animate the transform on the render surface.
3995 AddAnimatedTransformToController(
3996 animating_surface->layer_animation_controller(), 10.0, 30, 0);
3997 // This is just an animating layer, not a surface.
3998 AddAnimatedTransformToController(
3999 animating_child->layer_animation_controller(), 10.0, 30, 0);
4001 SetLayerPropertiesForTesting(parent.get(),
4002 identity_matrix,
4003 gfx::Point3F(),
4004 gfx::PointF(),
4005 gfx::Size(100, 100),
4006 true,
4007 false);
4008 SetLayerPropertiesForTesting(child.get(),
4009 backface_matrix,
4010 gfx::Point3F(),
4011 gfx::PointF(),
4012 gfx::Size(100, 100),
4013 true,
4014 false);
4015 SetLayerPropertiesForTesting(animating_surface.get(),
4016 backface_matrix,
4017 gfx::Point3F(),
4018 gfx::PointF(),
4019 gfx::Size(100, 100),
4020 true,
4021 false);
4022 SetLayerPropertiesForTesting(child_of_animating_surface.get(),
4023 backface_matrix,
4024 gfx::Point3F(),
4025 gfx::PointF(),
4026 gfx::Size(100, 100),
4027 true,
4028 false);
4029 SetLayerPropertiesForTesting(animating_child.get(),
4030 backface_matrix,
4031 gfx::Point3F(),
4032 gfx::PointF(),
4033 gfx::Size(100, 100),
4034 true,
4035 false);
4036 SetLayerPropertiesForTesting(child2.get(),
4037 identity_matrix,
4038 gfx::Point3F(),
4039 gfx::PointF(),
4040 gfx::Size(100, 100),
4041 true,
4042 false);
4044 RenderSurfaceLayerList render_surface_layer_list;
4045 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4046 parent.get(), parent->bounds(), &render_surface_layer_list);
4047 inputs.can_adjust_raster_scales = true;
4048 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4050 EXPECT_FALSE(child->render_surface());
4051 EXPECT_TRUE(animating_surface->render_surface());
4052 EXPECT_FALSE(child_of_animating_surface->render_surface());
4053 EXPECT_FALSE(animating_child->render_surface());
4054 EXPECT_FALSE(child2->render_surface());
4056 // Verify that the animating_child and child_of_animating_surface were not
4057 // culled, but that child was.
4058 ASSERT_EQ(2u, render_surface_layer_list.size());
4059 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
4060 EXPECT_EQ(animating_surface->id(), render_surface_layer_list.at(1)->id());
4062 // The non-animating child be culled from the layer list for the parent render
4063 // surface.
4064 ASSERT_EQ(
4066 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
4067 EXPECT_EQ(animating_surface->id(),
4068 render_surface_layer_list.at(0)
4069 ->render_surface()->layer_list().at(0)->id());
4070 EXPECT_EQ(animating_child->id(),
4071 render_surface_layer_list.at(0)
4072 ->render_surface()->layer_list().at(1)->id());
4073 EXPECT_EQ(child2->id(),
4074 render_surface_layer_list.at(0)
4075 ->render_surface()->layer_list().at(2)->id());
4077 ASSERT_EQ(
4079 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
4080 EXPECT_EQ(animating_surface->id(),
4081 render_surface_layer_list.at(1)
4082 ->render_surface()->layer_list().at(0)->id());
4083 EXPECT_EQ(child_of_animating_surface->id(),
4084 render_surface_layer_list.at(1)
4085 ->render_surface()->layer_list().at(1)->id());
4087 EXPECT_FALSE(child2->visible_content_rect().IsEmpty());
4089 // The animating layers should have a visible content rect that represents the
4090 // area of the front face that is within the viewport.
4091 EXPECT_EQ(animating_child->visible_content_rect(),
4092 gfx::Rect(animating_child->content_bounds()));
4093 EXPECT_EQ(animating_surface->visible_content_rect(),
4094 gfx::Rect(animating_surface->content_bounds()));
4095 // And layers in the subtree of the animating layer should have valid visible
4096 // content rects also.
4097 EXPECT_EQ(child_of_animating_surface->visible_content_rect(),
4098 gfx::Rect(child_of_animating_surface->content_bounds()));
4101 TEST_F(LayerTreeHostCommonTest,
4102 BackFaceCullingWithPreserves3dForFlatteningSurface) {
4103 // Verify the behavior of back-face culling for a render surface that is
4104 // created when it flattens its subtree, and its parent has preserves-3d.
4106 const gfx::Transform identity_matrix;
4107 scoped_refptr<Layer> parent = Layer::Create();
4108 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
4109 make_scoped_refptr(new LayerWithForcedDrawsContent());
4110 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
4111 make_scoped_refptr(new LayerWithForcedDrawsContent());
4112 scoped_refptr<LayerWithForcedDrawsContent> child1 =
4113 make_scoped_refptr(new LayerWithForcedDrawsContent());
4114 scoped_refptr<LayerWithForcedDrawsContent> child2 =
4115 make_scoped_refptr(new LayerWithForcedDrawsContent());
4117 parent->AddChild(front_facing_surface);
4118 parent->AddChild(back_facing_surface);
4119 front_facing_surface->AddChild(child1);
4120 back_facing_surface->AddChild(child2);
4122 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4123 host->SetRootLayer(parent);
4125 // RenderSurfaces are not double-sided
4126 front_facing_surface->SetDoubleSided(false);
4127 back_facing_surface->SetDoubleSided(false);
4129 gfx::Transform backface_matrix;
4130 backface_matrix.Translate(50.0, 50.0);
4131 backface_matrix.RotateAboutYAxis(180.0);
4132 backface_matrix.Translate(-50.0, -50.0);
4134 SetLayerPropertiesForTesting(parent.get(),
4135 identity_matrix,
4136 gfx::Point3F(),
4137 gfx::PointF(),
4138 gfx::Size(100, 100),
4139 false,
4140 true); // parent transform style is preserve3d.
4141 SetLayerPropertiesForTesting(front_facing_surface.get(),
4142 identity_matrix,
4143 gfx::Point3F(),
4144 gfx::PointF(),
4145 gfx::Size(100, 100),
4146 true,
4147 true); // surface transform style is flat.
4148 SetLayerPropertiesForTesting(back_facing_surface.get(),
4149 backface_matrix,
4150 gfx::Point3F(),
4151 gfx::PointF(),
4152 gfx::Size(100, 100),
4153 true,
4154 true); // surface transform style is flat.
4155 SetLayerPropertiesForTesting(child1.get(),
4156 identity_matrix,
4157 gfx::Point3F(),
4158 gfx::PointF(),
4159 gfx::Size(100, 100),
4160 true,
4161 false);
4162 SetLayerPropertiesForTesting(child2.get(),
4163 identity_matrix,
4164 gfx::Point3F(),
4165 gfx::PointF(),
4166 gfx::Size(100, 100),
4167 true,
4168 false);
4170 front_facing_surface->Set3dSortingContextId(1);
4171 back_facing_surface->Set3dSortingContextId(1);
4173 RenderSurfaceLayerList render_surface_layer_list;
4174 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4175 parent.get(), parent->bounds(), &render_surface_layer_list);
4176 inputs.can_adjust_raster_scales = true;
4177 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4179 // Verify which render surfaces were created and used.
4180 EXPECT_TRUE(front_facing_surface->render_surface());
4182 // We expect the render surface to have been created, but remain unused.
4183 EXPECT_TRUE(back_facing_surface->render_surface());
4184 EXPECT_NE(back_facing_surface->render_target(),
4185 back_facing_surface); // because it should be culled
4186 EXPECT_FALSE(child1->render_surface());
4187 EXPECT_FALSE(child2->render_surface());
4189 // Verify the render_surface_layer_list. The back-facing surface should be
4190 // culled.
4191 ASSERT_EQ(2u, render_surface_layer_list.size());
4192 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
4193 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list.at(1)->id());
4195 // Verify root surface's layer list.
4196 ASSERT_EQ(
4198 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
4199 EXPECT_EQ(front_facing_surface->id(),
4200 render_surface_layer_list.at(0)
4201 ->render_surface()->layer_list().at(0)->id());
4203 // Verify front_facing_surface's layer list.
4204 ASSERT_EQ(
4206 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
4207 EXPECT_EQ(front_facing_surface->id(),
4208 render_surface_layer_list.at(1)
4209 ->render_surface()->layer_list().at(0)->id());
4210 EXPECT_EQ(child1->id(),
4211 render_surface_layer_list.at(1)
4212 ->render_surface()->layer_list().at(1)->id());
4215 class NoScaleContentLayer : public ContentLayer {
4216 public:
4217 static scoped_refptr<NoScaleContentLayer> Create(ContentLayerClient* client) {
4218 return make_scoped_refptr(new NoScaleContentLayer(client));
4221 void CalculateContentsScale(float ideal_contents_scale,
4222 float* contents_scale_x,
4223 float* contents_scale_y,
4224 gfx::Size* content_bounds) override {
4225 // Skip over the ContentLayer to the base Layer class.
4226 Layer::CalculateContentsScale(ideal_contents_scale,
4227 contents_scale_x,
4228 contents_scale_y,
4229 content_bounds);
4232 protected:
4233 explicit NoScaleContentLayer(ContentLayerClient* client)
4234 : ContentLayer(client) {}
4235 ~NoScaleContentLayer() override {}
4238 scoped_refptr<NoScaleContentLayer> CreateNoScaleDrawableContentLayer(
4239 ContentLayerClient* delegate) {
4240 scoped_refptr<NoScaleContentLayer> to_return =
4241 NoScaleContentLayer::Create(delegate);
4242 to_return->SetIsDrawable(true);
4243 return to_return;
4246 TEST_F(LayerTreeHostCommonTest, LayerTransformsInHighDPI) {
4247 // Verify draw and screen space transforms of layers not in a surface.
4248 MockContentLayerClient delegate;
4249 gfx::Transform identity_matrix;
4251 scoped_refptr<FakePictureLayer> parent =
4252 CreateDrawablePictureLayer(&delegate);
4253 SetLayerPropertiesForTesting(parent.get(),
4254 identity_matrix,
4255 gfx::Point3F(),
4256 gfx::PointF(),
4257 gfx::Size(100, 100),
4258 false,
4259 true);
4261 scoped_refptr<FakePictureLayer> child = CreateDrawablePictureLayer(&delegate);
4262 SetLayerPropertiesForTesting(child.get(),
4263 identity_matrix,
4264 gfx::Point3F(),
4265 gfx::PointF(2.f, 2.f),
4266 gfx::Size(10, 10),
4267 false,
4268 true);
4270 scoped_refptr<FakePictureLayer> child_empty =
4271 CreateDrawablePictureLayer(&delegate);
4272 SetLayerPropertiesForTesting(child_empty.get(),
4273 identity_matrix,
4274 gfx::Point3F(),
4275 gfx::PointF(2.f, 2.f),
4276 gfx::Size(),
4277 false,
4278 true);
4280 parent->AddChild(child);
4281 parent->AddChild(child_empty);
4283 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4284 host->SetRootLayer(parent);
4286 float device_scale_factor = 2.5f;
4287 float page_scale_factor = 1.f;
4289 RenderSurfaceLayerList render_surface_layer_list;
4290 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4291 parent.get(), parent->bounds(), &render_surface_layer_list);
4292 inputs.device_scale_factor = device_scale_factor;
4293 inputs.page_scale_factor = page_scale_factor;
4294 inputs.can_adjust_raster_scales = true;
4295 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4297 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4298 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, child);
4299 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, child_empty);
4301 EXPECT_EQ(1u, render_surface_layer_list.size());
4303 // Verify parent transforms
4304 gfx::Transform expected_parent_transform;
4305 expected_parent_transform.Scale(device_scale_factor * page_scale_factor,
4306 device_scale_factor * page_scale_factor);
4307 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4308 parent->screen_space_transform());
4309 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4310 parent->draw_transform());
4312 // Verify results of transformed parent rects
4313 gfx::RectF parent_content_bounds(parent->content_bounds());
4315 gfx::RectF parent_draw_rect =
4316 MathUtil::MapClippedRect(parent->draw_transform(), parent_content_bounds);
4317 gfx::RectF parent_screen_space_rect = MathUtil::MapClippedRect(
4318 parent->screen_space_transform(), parent_content_bounds);
4320 gfx::RectF expected_parent_draw_rect(parent->bounds());
4321 expected_parent_draw_rect.Scale(device_scale_factor);
4322 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_draw_rect);
4323 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_screen_space_rect);
4325 // Verify child and child_empty transforms. They should match.
4326 gfx::Transform expected_child_transform;
4327 expected_child_transform.Scale(device_scale_factor, device_scale_factor);
4328 expected_child_transform.Translate(child->position().x(),
4329 child->position().y());
4330 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4331 child->draw_transform());
4332 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4333 child->screen_space_transform());
4334 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4335 child_empty->draw_transform());
4336 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4337 child_empty->screen_space_transform());
4339 // Verify results of transformed child and child_empty rects. They should
4340 // match.
4341 gfx::RectF child_content_bounds(child->content_bounds());
4343 gfx::RectF child_draw_rect =
4344 MathUtil::MapClippedRect(child->draw_transform(), child_content_bounds);
4345 gfx::RectF child_screen_space_rect = MathUtil::MapClippedRect(
4346 child->screen_space_transform(), child_content_bounds);
4348 gfx::RectF child_empty_draw_rect = MathUtil::MapClippedRect(
4349 child_empty->draw_transform(), child_content_bounds);
4350 gfx::RectF child_empty_screen_space_rect = MathUtil::MapClippedRect(
4351 child_empty->screen_space_transform(), child_content_bounds);
4353 gfx::RectF expected_child_draw_rect(child->position(), child->bounds());
4354 expected_child_draw_rect.Scale(device_scale_factor);
4355 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_draw_rect);
4356 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_screen_space_rect);
4357 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_empty_draw_rect);
4358 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_empty_screen_space_rect);
4361 TEST_F(LayerTreeHostCommonTest, SurfaceLayerTransformsInHighDPI) {
4362 // Verify draw and screen space transforms of layers in a surface.
4363 MockContentLayerClient delegate;
4364 gfx::Transform identity_matrix;
4366 gfx::Transform perspective_matrix;
4367 perspective_matrix.ApplyPerspectiveDepth(2.0);
4369 gfx::Transform scale_small_matrix;
4370 scale_small_matrix.Scale(SK_MScalar1 / 10.f, SK_MScalar1 / 12.f);
4372 scoped_refptr<Layer> root = Layer::Create();
4374 scoped_refptr<FakePictureLayer> parent =
4375 CreateDrawablePictureLayer(&delegate);
4376 SetLayerPropertiesForTesting(parent.get(),
4377 identity_matrix,
4378 gfx::Point3F(),
4379 gfx::PointF(),
4380 gfx::Size(100, 100),
4381 false,
4382 true);
4384 scoped_refptr<FakePictureLayer> perspective_surface =
4385 CreateDrawablePictureLayer(&delegate);
4386 SetLayerPropertiesForTesting(perspective_surface.get(),
4387 perspective_matrix * scale_small_matrix,
4388 gfx::Point3F(),
4389 gfx::PointF(2.f, 2.f),
4390 gfx::Size(10, 10),
4391 false,
4392 true);
4394 scoped_refptr<FakePictureLayer> scale_surface =
4395 CreateDrawablePictureLayer(&delegate);
4396 SetLayerPropertiesForTesting(scale_surface.get(),
4397 scale_small_matrix,
4398 gfx::Point3F(),
4399 gfx::PointF(2.f, 2.f),
4400 gfx::Size(10, 10),
4401 false,
4402 true);
4404 perspective_surface->SetForceRenderSurface(true);
4405 scale_surface->SetForceRenderSurface(true);
4407 parent->AddChild(perspective_surface);
4408 parent->AddChild(scale_surface);
4409 root->AddChild(parent);
4411 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4412 host->SetRootLayer(root);
4414 float device_scale_factor = 2.5f;
4415 float page_scale_factor = 3.f;
4417 RenderSurfaceLayerList render_surface_layer_list;
4418 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4419 root.get(), parent->bounds(), &render_surface_layer_list);
4420 inputs.device_scale_factor = device_scale_factor;
4421 inputs.page_scale_factor = page_scale_factor;
4422 inputs.page_scale_application_layer = root.get();
4423 inputs.can_adjust_raster_scales = true;
4424 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4426 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4427 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor,
4428 perspective_surface);
4429 // Ideal scale is the max 2d scale component of the combined transform up to
4430 // the nearest render target. Here this includes the layer transform as well
4431 // as the device and page scale factors.
4432 gfx::Transform transform = scale_small_matrix;
4433 transform.Scale(device_scale_factor * page_scale_factor,
4434 device_scale_factor * page_scale_factor);
4435 gfx::Vector2dF scales =
4436 MathUtil::ComputeTransform2dScaleComponents(transform, 0.f);
4437 float max_2d_scale = std::max(scales.x(), scales.y());
4438 EXPECT_IDEAL_SCALE_EQ(max_2d_scale, scale_surface);
4440 // The ideal scale will draw 1:1 with its render target space along
4441 // the larger-scale axis.
4442 gfx::Vector2dF target_space_transform_scales =
4443 MathUtil::ComputeTransform2dScaleComponents(
4444 scale_surface->draw_properties().target_space_transform, 0.f);
4445 EXPECT_FLOAT_EQ(max_2d_scale,
4446 std::max(target_space_transform_scales.x(),
4447 target_space_transform_scales.y()));
4449 EXPECT_EQ(3u, render_surface_layer_list.size());
4451 gfx::Transform expected_parent_draw_transform;
4452 expected_parent_draw_transform.Scale(device_scale_factor * page_scale_factor,
4453 device_scale_factor * page_scale_factor);
4454 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_draw_transform,
4455 parent->draw_transform());
4457 // The scale for the perspective surface is not known, so it is rendered 1:1
4458 // with the screen, and then scaled during drawing.
4459 gfx::Transform expected_perspective_surface_draw_transform;
4460 expected_perspective_surface_draw_transform.Translate(
4461 device_scale_factor * page_scale_factor *
4462 perspective_surface->position().x(),
4463 device_scale_factor * page_scale_factor *
4464 perspective_surface->position().y());
4465 expected_perspective_surface_draw_transform.PreconcatTransform(
4466 perspective_matrix);
4467 expected_perspective_surface_draw_transform.PreconcatTransform(
4468 scale_small_matrix);
4469 gfx::Transform expected_perspective_surface_layer_draw_transform;
4470 expected_perspective_surface_layer_draw_transform.Scale(
4471 device_scale_factor * page_scale_factor,
4472 device_scale_factor * page_scale_factor);
4473 EXPECT_TRANSFORMATION_MATRIX_EQ(
4474 expected_perspective_surface_draw_transform,
4475 perspective_surface->render_surface()->draw_transform());
4476 EXPECT_TRANSFORMATION_MATRIX_EQ(
4477 expected_perspective_surface_layer_draw_transform,
4478 perspective_surface->draw_transform());
4481 // TODO(sohanjg): Remove this test when ContentLayer is removed.
4482 TEST_F(LayerTreeHostCommonTest,
4483 LayerTransformsInHighDPIAccurateScaleZeroChildPosition) {
4484 // Verify draw and screen space transforms of layers not in a surface.
4485 MockContentLayerClient delegate;
4486 gfx::Transform identity_matrix;
4488 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
4489 SetLayerPropertiesForTesting(parent.get(),
4490 identity_matrix,
4491 gfx::Point3F(),
4492 gfx::PointF(),
4493 gfx::Size(133, 133),
4494 false,
4495 true);
4497 scoped_refptr<ContentLayer> child = CreateDrawableContentLayer(&delegate);
4498 SetLayerPropertiesForTesting(child.get(),
4499 identity_matrix,
4500 gfx::Point3F(),
4501 gfx::PointF(),
4502 gfx::Size(13, 13),
4503 false,
4504 true);
4506 scoped_refptr<NoScaleContentLayer> child_no_scale =
4507 CreateNoScaleDrawableContentLayer(&delegate);
4508 SetLayerPropertiesForTesting(child_no_scale.get(),
4509 identity_matrix,
4510 gfx::Point3F(),
4511 gfx::PointF(),
4512 gfx::Size(13, 13),
4513 false,
4514 true);
4516 parent->AddChild(child);
4517 parent->AddChild(child_no_scale);
4519 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4520 host->SetRootLayer(parent);
4522 float device_scale_factor = 1.7f;
4523 float page_scale_factor = 1.f;
4525 RenderSurfaceLayerList render_surface_layer_list;
4526 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4527 parent.get(), parent->bounds(), &render_surface_layer_list);
4528 inputs.device_scale_factor = device_scale_factor;
4529 inputs.page_scale_factor = page_scale_factor;
4530 inputs.page_scale_application_layer = parent.get();
4531 inputs.can_adjust_raster_scales = true;
4532 inputs.verify_property_trees = false;
4533 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4535 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4536 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, child);
4537 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4539 EXPECT_EQ(1u, render_surface_layer_list.size());
4541 // Verify parent transforms
4542 gfx::Transform expected_parent_transform;
4543 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4544 parent->screen_space_transform());
4545 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4546 parent->draw_transform());
4548 // Verify results of transformed parent rects
4549 gfx::RectF parent_content_bounds(parent->content_bounds());
4551 gfx::RectF parent_draw_rect =
4552 MathUtil::MapClippedRect(parent->draw_transform(), parent_content_bounds);
4553 gfx::RectF parent_screen_space_rect = MathUtil::MapClippedRect(
4554 parent->screen_space_transform(), parent_content_bounds);
4556 gfx::RectF expected_parent_draw_rect(parent->bounds());
4557 expected_parent_draw_rect.Scale(device_scale_factor);
4558 expected_parent_draw_rect.set_width(ceil(expected_parent_draw_rect.width()));
4559 expected_parent_draw_rect.set_height(
4560 ceil(expected_parent_draw_rect.height()));
4561 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_draw_rect);
4562 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_screen_space_rect);
4564 // Verify child transforms
4565 gfx::Transform expected_child_transform;
4566 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4567 child->draw_transform());
4568 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4569 child->screen_space_transform());
4571 // Verify results of transformed child rects
4572 gfx::RectF child_content_bounds(child->content_bounds());
4574 gfx::RectF child_draw_rect =
4575 MathUtil::MapClippedRect(child->draw_transform(), child_content_bounds);
4576 gfx::RectF child_screen_space_rect = MathUtil::MapClippedRect(
4577 child->screen_space_transform(), child_content_bounds);
4579 gfx::RectF expected_child_draw_rect(child->bounds());
4580 expected_child_draw_rect.Scale(device_scale_factor);
4581 expected_child_draw_rect.set_width(ceil(expected_child_draw_rect.width()));
4582 expected_child_draw_rect.set_height(ceil(expected_child_draw_rect.height()));
4583 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_draw_rect);
4584 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_screen_space_rect);
4586 // Verify child_no_scale transforms
4587 gfx::Transform expected_child_no_scale_transform = child->draw_transform();
4588 // All transforms operate on content rects. The child's content rect
4589 // incorporates device scale, but the child_no_scale does not; add it here.
4590 expected_child_no_scale_transform.Scale(device_scale_factor,
4591 device_scale_factor);
4592 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_no_scale_transform,
4593 child_no_scale->draw_transform());
4594 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_no_scale_transform,
4595 child_no_scale->screen_space_transform());
4598 // TODO(sohanjg): Remove this test when ContentLayer is removed.
4599 TEST_F(LayerTreeHostCommonTest, ContentsScale) {
4600 MockContentLayerClient delegate;
4601 gfx::Transform identity_matrix;
4603 gfx::Transform parent_scale_matrix;
4604 SkMScalar initial_parent_scale = 1.75;
4605 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4607 gfx::Transform child_scale_matrix;
4608 SkMScalar initial_child_scale = 1.25;
4609 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4611 scoped_refptr<Layer> root = Layer::Create();
4612 root->SetBounds(gfx::Size(100, 100));
4614 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
4615 SetLayerPropertiesForTesting(parent.get(),
4616 parent_scale_matrix,
4617 gfx::Point3F(),
4618 gfx::PointF(),
4619 gfx::Size(100, 100),
4620 false,
4621 true);
4623 scoped_refptr<ContentLayer> child_scale =
4624 CreateDrawableContentLayer(&delegate);
4625 SetLayerPropertiesForTesting(child_scale.get(),
4626 child_scale_matrix,
4627 gfx::Point3F(),
4628 gfx::PointF(2.f, 2.f),
4629 gfx::Size(10, 10),
4630 false,
4631 true);
4633 scoped_refptr<ContentLayer> child_empty =
4634 CreateDrawableContentLayer(&delegate);
4635 SetLayerPropertiesForTesting(child_empty.get(),
4636 child_scale_matrix,
4637 gfx::Point3F(),
4638 gfx::PointF(2.f, 2.f),
4639 gfx::Size(),
4640 false,
4641 true);
4643 scoped_refptr<NoScaleContentLayer> child_no_scale =
4644 CreateNoScaleDrawableContentLayer(&delegate);
4645 SetLayerPropertiesForTesting(child_no_scale.get(),
4646 child_scale_matrix,
4647 gfx::Point3F(),
4648 gfx::PointF(12.f, 12.f),
4649 gfx::Size(10, 10),
4650 false,
4651 true);
4653 root->AddChild(parent);
4655 parent->AddChild(child_scale);
4656 parent->AddChild(child_empty);
4657 parent->AddChild(child_no_scale);
4659 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4660 host->SetRootLayer(root);
4662 float device_scale_factor = 2.5f;
4663 float page_scale_factor = 1.f;
4666 RenderSurfaceLayerList render_surface_layer_list;
4667 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4668 root.get(), root->bounds(), &render_surface_layer_list);
4669 inputs.device_scale_factor = device_scale_factor;
4670 inputs.page_scale_factor = page_scale_factor;
4671 inputs.page_scale_application_layer = root.get();
4672 inputs.can_adjust_raster_scales = true;
4673 inputs.verify_property_trees = false;
4674 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4676 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4677 initial_parent_scale, parent);
4678 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4679 initial_parent_scale * initial_child_scale,
4680 child_scale);
4681 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4682 initial_parent_scale * initial_child_scale,
4683 child_empty);
4684 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4686 // The parent is scaled up and shouldn't need to scale during draw. The
4687 // child that can scale its contents should also not need to scale during
4688 // draw. This shouldn't change if the child has empty bounds. The other
4689 // children should.
4690 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(0, 0));
4691 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(1, 1));
4692 EXPECT_FLOAT_EQ(1.0, child_scale->draw_transform().matrix().get(0, 0));
4693 EXPECT_FLOAT_EQ(1.0, child_scale->draw_transform().matrix().get(1, 1));
4694 EXPECT_FLOAT_EQ(1.0, child_empty->draw_transform().matrix().get(0, 0));
4695 EXPECT_FLOAT_EQ(1.0, child_empty->draw_transform().matrix().get(1, 1));
4696 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4697 initial_parent_scale * initial_child_scale,
4698 child_no_scale->draw_transform().matrix().get(0, 0));
4699 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4700 initial_parent_scale * initial_child_scale,
4701 child_no_scale->draw_transform().matrix().get(1, 1));
4704 // If the device_scale_factor or page_scale_factor changes, then it should be
4705 // updated using the initial transform as the raster scale.
4706 device_scale_factor = 2.25f;
4707 page_scale_factor = 1.25f;
4710 RenderSurfaceLayerList render_surface_layer_list;
4711 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4712 root.get(), root->bounds(), &render_surface_layer_list);
4713 inputs.device_scale_factor = device_scale_factor;
4714 inputs.page_scale_factor = page_scale_factor;
4715 inputs.page_scale_application_layer = root.get();
4716 inputs.can_adjust_raster_scales = true;
4717 inputs.verify_property_trees = false;
4718 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4720 EXPECT_CONTENTS_SCALE_EQ(
4721 device_scale_factor * page_scale_factor * initial_parent_scale, parent);
4722 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4723 initial_parent_scale * initial_child_scale,
4724 child_scale);
4725 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4726 initial_parent_scale * initial_child_scale,
4727 child_empty);
4728 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4731 // If the transform changes, we expect the raster scale to be reset to 1.0.
4732 SkMScalar second_child_scale = 1.75;
4733 child_scale_matrix.Scale(second_child_scale / initial_child_scale,
4734 second_child_scale / initial_child_scale);
4735 child_scale->SetTransform(child_scale_matrix);
4736 child_empty->SetTransform(child_scale_matrix);
4739 RenderSurfaceLayerList render_surface_layer_list;
4740 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4741 root.get(), root->bounds(), &render_surface_layer_list);
4742 inputs.device_scale_factor = device_scale_factor;
4743 inputs.page_scale_factor = page_scale_factor;
4744 inputs.page_scale_application_layer = root.get();
4745 inputs.can_adjust_raster_scales = true;
4746 inputs.verify_property_trees = false;
4747 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4749 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4750 initial_parent_scale,
4751 parent);
4752 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4753 child_scale);
4754 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4755 child_empty);
4756 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4759 // If the device_scale_factor or page_scale_factor changes, then it should be
4760 // updated, but still using 1.0 as the raster scale.
4761 device_scale_factor = 2.75f;
4762 page_scale_factor = 1.75f;
4765 RenderSurfaceLayerList render_surface_layer_list;
4766 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4767 root.get(), root->bounds(), &render_surface_layer_list);
4768 inputs.device_scale_factor = device_scale_factor;
4769 inputs.page_scale_factor = page_scale_factor;
4770 inputs.page_scale_application_layer = root.get();
4771 inputs.can_adjust_raster_scales = true;
4772 inputs.verify_property_trees = false;
4773 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4775 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4776 initial_parent_scale,
4777 parent);
4778 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4779 child_scale);
4780 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4781 child_empty);
4782 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4786 // TODO(sohanjg): Remove this test when ContentLayer is removed.
4787 TEST_F(LayerTreeHostCommonTest,
4788 ContentsScale_LayerTransformsDontAffectContentsScale) {
4789 MockContentLayerClient delegate;
4790 gfx::Transform identity_matrix;
4792 gfx::Transform parent_scale_matrix;
4793 SkMScalar initial_parent_scale = 1.75;
4794 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4796 gfx::Transform child_scale_matrix;
4797 SkMScalar initial_child_scale = 1.25;
4798 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4800 scoped_refptr<Layer> root = Layer::Create();
4801 root->SetBounds(gfx::Size(100, 100));
4803 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
4804 SetLayerPropertiesForTesting(parent.get(),
4805 parent_scale_matrix,
4806 gfx::Point3F(),
4807 gfx::PointF(),
4808 gfx::Size(100, 100),
4809 false,
4810 true);
4812 scoped_refptr<ContentLayer> child_scale =
4813 CreateDrawableContentLayer(&delegate);
4814 SetLayerPropertiesForTesting(child_scale.get(),
4815 child_scale_matrix,
4816 gfx::Point3F(),
4817 gfx::PointF(2.f, 2.f),
4818 gfx::Size(10, 10),
4819 false,
4820 true);
4822 scoped_refptr<ContentLayer> child_empty =
4823 CreateDrawableContentLayer(&delegate);
4824 SetLayerPropertiesForTesting(child_empty.get(),
4825 child_scale_matrix,
4826 gfx::Point3F(),
4827 gfx::PointF(2.f, 2.f),
4828 gfx::Size(),
4829 false,
4830 true);
4832 scoped_refptr<NoScaleContentLayer> child_no_scale =
4833 CreateNoScaleDrawableContentLayer(&delegate);
4834 SetLayerPropertiesForTesting(child_no_scale.get(),
4835 child_scale_matrix,
4836 gfx::Point3F(),
4837 gfx::PointF(12.f, 12.f),
4838 gfx::Size(10, 10),
4839 false,
4840 true);
4842 root->AddChild(parent);
4844 parent->AddChild(child_scale);
4845 parent->AddChild(child_empty);
4846 parent->AddChild(child_no_scale);
4848 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4849 host->SetRootLayer(root);
4851 RenderSurfaceLayerList render_surface_layer_list;
4853 float device_scale_factor = 2.5f;
4854 float page_scale_factor = 1.f;
4856 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4857 root.get(), root->bounds(), &render_surface_layer_list);
4858 inputs.device_scale_factor = device_scale_factor;
4859 inputs.page_scale_factor = page_scale_factor;
4860 inputs.page_scale_application_layer = root.get();
4861 inputs.verify_property_trees = false;
4862 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4864 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4865 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4866 child_scale);
4867 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4868 child_empty);
4869 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4871 // Since the transform scale does not affect contents scale, it should affect
4872 // the draw transform instead.
4873 EXPECT_FLOAT_EQ(initial_parent_scale,
4874 parent->draw_transform().matrix().get(0, 0));
4875 EXPECT_FLOAT_EQ(initial_parent_scale,
4876 parent->draw_transform().matrix().get(1, 1));
4877 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4878 child_scale->draw_transform().matrix().get(0, 0));
4879 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4880 child_scale->draw_transform().matrix().get(1, 1));
4881 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4882 child_empty->draw_transform().matrix().get(0, 0));
4883 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4884 child_empty->draw_transform().matrix().get(1, 1));
4885 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4886 initial_parent_scale * initial_child_scale,
4887 child_no_scale->draw_transform().matrix().get(0, 0));
4888 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4889 initial_parent_scale * initial_child_scale,
4890 child_no_scale->draw_transform().matrix().get(1, 1));
4893 TEST_F(LayerTreeHostCommonTest, SmallIdealScale) {
4894 MockContentLayerClient delegate;
4895 gfx::Transform identity_matrix;
4897 gfx::Transform parent_scale_matrix;
4898 SkMScalar initial_parent_scale = 1.75;
4899 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4901 gfx::Transform child_scale_matrix;
4902 SkMScalar initial_child_scale = 0.25;
4903 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4905 scoped_refptr<Layer> root = Layer::Create();
4906 root->SetBounds(gfx::Size(100, 100));
4908 scoped_refptr<FakePictureLayer> parent =
4909 CreateDrawablePictureLayer(&delegate);
4910 SetLayerPropertiesForTesting(parent.get(),
4911 parent_scale_matrix,
4912 gfx::Point3F(),
4913 gfx::PointF(),
4914 gfx::Size(100, 100),
4915 false,
4916 true);
4918 scoped_refptr<FakePictureLayer> child_scale =
4919 CreateDrawablePictureLayer(&delegate);
4920 SetLayerPropertiesForTesting(child_scale.get(),
4921 child_scale_matrix,
4922 gfx::Point3F(),
4923 gfx::PointF(2.f, 2.f),
4924 gfx::Size(10, 10),
4925 false,
4926 true);
4928 root->AddChild(parent);
4930 parent->AddChild(child_scale);
4932 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4933 host->SetRootLayer(root);
4935 float device_scale_factor = 2.5f;
4936 float page_scale_factor = 0.01f;
4939 RenderSurfaceLayerList render_surface_layer_list;
4940 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4941 root.get(), root->bounds(), &render_surface_layer_list);
4942 inputs.device_scale_factor = device_scale_factor;
4943 inputs.page_scale_factor = page_scale_factor;
4944 inputs.page_scale_application_layer = root.get();
4945 inputs.can_adjust_raster_scales = true;
4946 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4948 // The ideal scale is able to go below 1.
4949 float expected_ideal_scale =
4950 device_scale_factor * page_scale_factor * initial_parent_scale;
4951 EXPECT_LT(expected_ideal_scale, 1.f);
4952 EXPECT_IDEAL_SCALE_EQ(expected_ideal_scale, parent);
4954 expected_ideal_scale = device_scale_factor * page_scale_factor *
4955 initial_parent_scale * initial_child_scale;
4956 EXPECT_LT(expected_ideal_scale, 1.f);
4957 EXPECT_IDEAL_SCALE_EQ(expected_ideal_scale, child_scale);
4961 TEST_F(LayerTreeHostCommonTest, ContentsScaleForSurfaces) {
4962 MockContentLayerClient delegate;
4963 gfx::Transform identity_matrix;
4965 gfx::Transform parent_scale_matrix;
4966 SkMScalar initial_parent_scale = 2.0;
4967 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4969 gfx::Transform child_scale_matrix;
4970 SkMScalar initial_child_scale = 3.0;
4971 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4973 scoped_refptr<Layer> root = Layer::Create();
4974 root->SetBounds(gfx::Size(100, 100));
4976 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
4977 SetLayerPropertiesForTesting(parent.get(),
4978 parent_scale_matrix,
4979 gfx::Point3F(),
4980 gfx::PointF(),
4981 gfx::Size(100, 100),
4982 false,
4983 true);
4985 scoped_refptr<ContentLayer> surface_scale =
4986 CreateDrawableContentLayer(&delegate);
4987 SetLayerPropertiesForTesting(surface_scale.get(),
4988 child_scale_matrix,
4989 gfx::Point3F(),
4990 gfx::PointF(2.f, 2.f),
4991 gfx::Size(10, 10),
4992 false,
4993 true);
4995 scoped_refptr<ContentLayer> surface_scale_child_scale =
4996 CreateDrawableContentLayer(&delegate);
4997 SetLayerPropertiesForTesting(surface_scale_child_scale.get(),
4998 child_scale_matrix,
4999 gfx::Point3F(),
5000 gfx::PointF(),
5001 gfx::Size(10, 10),
5002 false,
5003 true);
5005 scoped_refptr<NoScaleContentLayer> surface_scale_child_no_scale =
5006 CreateNoScaleDrawableContentLayer(&delegate);
5007 SetLayerPropertiesForTesting(surface_scale_child_no_scale.get(),
5008 child_scale_matrix,
5009 gfx::Point3F(),
5010 gfx::PointF(),
5011 gfx::Size(10, 10),
5012 false,
5013 true);
5015 scoped_refptr<NoScaleContentLayer> surface_no_scale =
5016 CreateNoScaleDrawableContentLayer(&delegate);
5017 SetLayerPropertiesForTesting(surface_no_scale.get(),
5018 child_scale_matrix,
5019 gfx::Point3F(),
5020 gfx::PointF(12.f, 12.f),
5021 gfx::Size(10, 10),
5022 false,
5023 true);
5025 scoped_refptr<ContentLayer> surface_no_scale_child_scale =
5026 CreateDrawableContentLayer(&delegate);
5027 SetLayerPropertiesForTesting(surface_no_scale_child_scale.get(),
5028 child_scale_matrix,
5029 gfx::Point3F(),
5030 gfx::PointF(),
5031 gfx::Size(10, 10),
5032 false,
5033 true);
5035 scoped_refptr<NoScaleContentLayer> surface_no_scale_child_no_scale =
5036 CreateNoScaleDrawableContentLayer(&delegate);
5037 SetLayerPropertiesForTesting(surface_no_scale_child_no_scale.get(),
5038 child_scale_matrix,
5039 gfx::Point3F(),
5040 gfx::PointF(),
5041 gfx::Size(10, 10),
5042 false,
5043 true);
5045 root->AddChild(parent);
5047 parent->AddChild(surface_scale);
5048 parent->AddChild(surface_no_scale);
5050 surface_scale->SetForceRenderSurface(true);
5051 surface_scale->AddChild(surface_scale_child_scale);
5052 surface_scale->AddChild(surface_scale_child_no_scale);
5054 surface_no_scale->SetForceRenderSurface(true);
5055 surface_no_scale->AddChild(surface_no_scale_child_scale);
5056 surface_no_scale->AddChild(surface_no_scale_child_no_scale);
5058 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5059 host->SetRootLayer(root);
5061 SkMScalar device_scale_factor = 5;
5062 SkMScalar page_scale_factor = 7;
5064 RenderSurfaceLayerList render_surface_layer_list;
5065 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5066 root.get(), root->bounds(), &render_surface_layer_list);
5067 inputs.device_scale_factor = device_scale_factor;
5068 inputs.page_scale_factor = page_scale_factor;
5069 inputs.page_scale_application_layer = root.get();
5070 inputs.can_adjust_raster_scales = true;
5071 inputs.verify_property_trees = false;
5072 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5074 EXPECT_CONTENTS_SCALE_EQ(
5075 device_scale_factor * page_scale_factor * initial_parent_scale, parent);
5076 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
5077 initial_parent_scale * initial_child_scale,
5078 surface_scale);
5079 EXPECT_CONTENTS_SCALE_EQ(1, surface_no_scale);
5080 EXPECT_CONTENTS_SCALE_EQ(
5081 device_scale_factor * page_scale_factor * initial_parent_scale *
5082 initial_child_scale * initial_child_scale,
5083 surface_scale_child_scale);
5084 EXPECT_CONTENTS_SCALE_EQ(1, surface_scale_child_no_scale);
5085 EXPECT_CONTENTS_SCALE_EQ(
5086 device_scale_factor * page_scale_factor * initial_parent_scale *
5087 initial_child_scale * initial_child_scale,
5088 surface_no_scale_child_scale);
5089 EXPECT_CONTENTS_SCALE_EQ(1, surface_no_scale_child_no_scale);
5091 // The parent is scaled up and shouldn't need to scale during draw.
5092 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(0, 0));
5093 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(1, 1));
5095 // RenderSurfaces should always be 1:1 with their target.
5096 EXPECT_FLOAT_EQ(
5097 1.0,
5098 surface_scale->render_surface()->draw_transform().matrix().get(0, 0));
5099 EXPECT_FLOAT_EQ(
5100 1.0,
5101 surface_scale->render_surface()->draw_transform().matrix().get(1, 1));
5103 // The surface_scale can apply contents scale so the layer shouldn't need to
5104 // scale during draw.
5105 EXPECT_FLOAT_EQ(1.0, surface_scale->draw_transform().matrix().get(0, 0));
5106 EXPECT_FLOAT_EQ(1.0, surface_scale->draw_transform().matrix().get(1, 1));
5108 // The surface_scale_child_scale can apply contents scale so it shouldn't need
5109 // to scale during draw.
5110 EXPECT_FLOAT_EQ(
5111 1.0, surface_scale_child_scale->draw_transform().matrix().get(0, 0));
5112 EXPECT_FLOAT_EQ(
5113 1.0, surface_scale_child_scale->draw_transform().matrix().get(1, 1));
5115 // The surface_scale_child_no_scale can not apply contents scale, so it needs
5116 // to be scaled during draw.
5117 EXPECT_FLOAT_EQ(
5118 device_scale_factor * page_scale_factor * initial_parent_scale *
5119 initial_child_scale * initial_child_scale,
5120 surface_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5121 EXPECT_FLOAT_EQ(
5122 device_scale_factor * page_scale_factor * initial_parent_scale *
5123 initial_child_scale * initial_child_scale,
5124 surface_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5126 // RenderSurfaces should always be 1:1 with their target.
5127 EXPECT_FLOAT_EQ(
5128 1.0,
5129 surface_no_scale->render_surface()->draw_transform().matrix().get(0, 0));
5130 EXPECT_FLOAT_EQ(
5131 1.0,
5132 surface_no_scale->render_surface()->draw_transform().matrix().get(1, 1));
5134 // The surface_no_scale layer can not apply contents scale, so it needs to be
5135 // scaled during draw.
5136 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
5137 initial_parent_scale * initial_child_scale,
5138 surface_no_scale->draw_transform().matrix().get(0, 0));
5139 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
5140 initial_parent_scale * initial_child_scale,
5141 surface_no_scale->draw_transform().matrix().get(1, 1));
5143 // The surface_scale_child_scale can apply contents scale so it shouldn't need
5144 // to scale during draw.
5145 EXPECT_FLOAT_EQ(
5146 1.0, surface_no_scale_child_scale->draw_transform().matrix().get(0, 0));
5147 EXPECT_FLOAT_EQ(
5148 1.0, surface_no_scale_child_scale->draw_transform().matrix().get(1, 1));
5150 // The surface_scale_child_no_scale can not apply contents scale, so it needs
5151 // to be scaled during draw.
5152 EXPECT_FLOAT_EQ(
5153 device_scale_factor * page_scale_factor * initial_parent_scale *
5154 initial_child_scale * initial_child_scale,
5155 surface_no_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5156 EXPECT_FLOAT_EQ(
5157 device_scale_factor * page_scale_factor * initial_parent_scale *
5158 initial_child_scale * initial_child_scale,
5159 surface_no_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5162 // TODO(sohanjg): Remove this test when ContentLayer is removed.
5163 TEST_F(LayerTreeHostCommonTest,
5164 ContentsScaleForSurfaces_LayerTransformsDontAffectContentsScale) {
5165 MockContentLayerClient delegate;
5166 gfx::Transform identity_matrix;
5168 gfx::Transform parent_scale_matrix;
5169 SkMScalar initial_parent_scale = 2.0;
5170 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
5172 gfx::Transform child_scale_matrix;
5173 SkMScalar initial_child_scale = 3.0;
5174 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
5176 scoped_refptr<Layer> root = Layer::Create();
5177 root->SetBounds(gfx::Size(100, 100));
5179 scoped_refptr<ContentLayer> parent = CreateDrawableContentLayer(&delegate);
5180 SetLayerPropertiesForTesting(parent.get(),
5181 parent_scale_matrix,
5182 gfx::Point3F(),
5183 gfx::PointF(),
5184 gfx::Size(100, 100),
5185 false,
5186 true);
5188 scoped_refptr<ContentLayer> surface_scale =
5189 CreateDrawableContentLayer(&delegate);
5190 SetLayerPropertiesForTesting(surface_scale.get(),
5191 child_scale_matrix,
5192 gfx::Point3F(),
5193 gfx::PointF(2.f, 2.f),
5194 gfx::Size(10, 10),
5195 false,
5196 true);
5198 scoped_refptr<ContentLayer> surface_scale_child_scale =
5199 CreateDrawableContentLayer(&delegate);
5200 SetLayerPropertiesForTesting(surface_scale_child_scale.get(),
5201 child_scale_matrix,
5202 gfx::Point3F(),
5203 gfx::PointF(),
5204 gfx::Size(10, 10),
5205 false,
5206 true);
5208 scoped_refptr<NoScaleContentLayer> surface_scale_child_no_scale =
5209 CreateNoScaleDrawableContentLayer(&delegate);
5210 SetLayerPropertiesForTesting(surface_scale_child_no_scale.get(),
5211 child_scale_matrix,
5212 gfx::Point3F(),
5213 gfx::PointF(),
5214 gfx::Size(10, 10),
5215 false,
5216 true);
5218 scoped_refptr<NoScaleContentLayer> surface_no_scale =
5219 CreateNoScaleDrawableContentLayer(&delegate);
5220 SetLayerPropertiesForTesting(surface_no_scale.get(),
5221 child_scale_matrix,
5222 gfx::Point3F(),
5223 gfx::PointF(12.f, 12.f),
5224 gfx::Size(10, 10),
5225 false,
5226 true);
5228 scoped_refptr<ContentLayer> surface_no_scale_child_scale =
5229 CreateDrawableContentLayer(&delegate);
5230 SetLayerPropertiesForTesting(surface_no_scale_child_scale.get(),
5231 child_scale_matrix,
5232 gfx::Point3F(),
5233 gfx::PointF(),
5234 gfx::Size(10, 10),
5235 false,
5236 true);
5238 scoped_refptr<NoScaleContentLayer> surface_no_scale_child_no_scale =
5239 CreateNoScaleDrawableContentLayer(&delegate);
5240 SetLayerPropertiesForTesting(surface_no_scale_child_no_scale.get(),
5241 child_scale_matrix,
5242 gfx::Point3F(),
5243 gfx::PointF(),
5244 gfx::Size(10, 10),
5245 false,
5246 true);
5248 root->AddChild(parent);
5250 parent->AddChild(surface_scale);
5251 parent->AddChild(surface_no_scale);
5253 surface_scale->SetForceRenderSurface(true);
5254 surface_scale->AddChild(surface_scale_child_scale);
5255 surface_scale->AddChild(surface_scale_child_no_scale);
5257 surface_no_scale->SetForceRenderSurface(true);
5258 surface_no_scale->AddChild(surface_no_scale_child_scale);
5259 surface_no_scale->AddChild(surface_no_scale_child_no_scale);
5261 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5262 host->SetRootLayer(root);
5264 RenderSurfaceLayerList render_surface_layer_list;
5266 SkMScalar device_scale_factor = 5.0;
5267 SkMScalar page_scale_factor = 7.0;
5268 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5269 root.get(), root->bounds(), &render_surface_layer_list);
5270 inputs.device_scale_factor = device_scale_factor;
5271 inputs.page_scale_factor = page_scale_factor;
5272 inputs.page_scale_application_layer = root.get();
5273 inputs.verify_property_trees = false;
5274 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5276 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5277 parent);
5278 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5279 surface_scale);
5280 EXPECT_CONTENTS_SCALE_EQ(1.f, surface_no_scale);
5281 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5282 surface_scale_child_scale);
5283 EXPECT_CONTENTS_SCALE_EQ(1.f, surface_scale_child_no_scale);
5284 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5285 surface_no_scale_child_scale);
5286 EXPECT_CONTENTS_SCALE_EQ(1.f, surface_no_scale_child_no_scale);
5288 // The parent is scaled up during draw, since its contents are not scaled by
5289 // the transform hierarchy.
5290 EXPECT_FLOAT_EQ(initial_parent_scale,
5291 parent->draw_transform().matrix().get(0, 0));
5292 EXPECT_FLOAT_EQ(initial_parent_scale,
5293 parent->draw_transform().matrix().get(1, 1));
5295 // The child surface is not scaled up during draw since its subtree is scaled
5296 // by the transform hierarchy.
5297 EXPECT_FLOAT_EQ(
5298 1.f,
5299 surface_scale->render_surface()->draw_transform().matrix().get(0, 0));
5300 EXPECT_FLOAT_EQ(
5301 1.f,
5302 surface_scale->render_surface()->draw_transform().matrix().get(1, 1));
5304 // The surface_scale's RenderSurface is not scaled during draw, so the layer
5305 // needs to be scaled when drawing into its surface.
5306 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
5307 surface_scale->draw_transform().matrix().get(0, 0));
5308 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
5309 surface_scale->draw_transform().matrix().get(1, 1));
5311 // The surface_scale_child_scale is not scaled when drawing into its surface,
5312 // since its content bounds are scaled by the transform hierarchy.
5313 EXPECT_FLOAT_EQ(
5314 initial_child_scale * initial_child_scale * initial_parent_scale,
5315 surface_scale_child_scale->draw_transform().matrix().get(0, 0));
5316 EXPECT_FLOAT_EQ(
5317 initial_child_scale * initial_child_scale * initial_parent_scale,
5318 surface_scale_child_scale->draw_transform().matrix().get(1, 1));
5320 // The surface_scale_child_no_scale is scaled by the device scale, page scale
5321 // and transform hierarchy.
5322 EXPECT_FLOAT_EQ(
5323 device_scale_factor * page_scale_factor * initial_parent_scale *
5324 initial_child_scale * initial_child_scale,
5325 surface_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5326 EXPECT_FLOAT_EQ(
5327 device_scale_factor * page_scale_factor * initial_parent_scale *
5328 initial_child_scale * initial_child_scale,
5329 surface_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5331 // The child surface is not scaled up during draw since its subtree is scaled
5332 // by the transform hierarchy.
5333 EXPECT_FLOAT_EQ(
5334 1.f,
5335 surface_no_scale->render_surface()->draw_transform().matrix().get(0, 0));
5336 EXPECT_FLOAT_EQ(
5337 1.f,
5338 surface_no_scale->render_surface()->draw_transform().matrix().get(1, 1));
5340 // The surface_no_scale layer has a fixed contents scale of 1, so it needs to
5341 // be scaled by the device and page scale factors. Its surface is already
5342 // scaled by the transform hierarchy so those don't need to scale the layer's
5343 // drawing.
5344 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale *
5345 device_scale_factor * page_scale_factor,
5346 surface_no_scale->draw_transform().matrix().get(0, 0));
5347 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale *
5348 device_scale_factor * page_scale_factor,
5349 surface_no_scale->draw_transform().matrix().get(1, 1));
5351 // The surface_no_scale_child_scale has its contents scaled by the page and
5352 // device scale factors, but needs to be scaled by the transform hierarchy
5353 // when drawing.
5354 EXPECT_FLOAT_EQ(
5355 initial_parent_scale * initial_child_scale * initial_child_scale,
5356 surface_no_scale_child_scale->draw_transform().matrix().get(0, 0));
5357 EXPECT_FLOAT_EQ(
5358 initial_parent_scale * initial_child_scale * initial_child_scale,
5359 surface_no_scale_child_scale->draw_transform().matrix().get(1, 1));
5361 // The surface_no_scale_child_no_scale needs to be scaled by the device and
5362 // page scale factors and by any transform heirarchy below its target surface.
5363 EXPECT_FLOAT_EQ(
5364 device_scale_factor * page_scale_factor * initial_parent_scale *
5365 initial_child_scale * initial_child_scale,
5366 surface_no_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5367 EXPECT_FLOAT_EQ(
5368 device_scale_factor * page_scale_factor * initial_parent_scale *
5369 initial_child_scale * initial_child_scale,
5370 surface_no_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5373 TEST_F(LayerTreeHostCommonTest, IdealScaleForAnimatingLayer) {
5374 MockContentLayerClient delegate;
5375 gfx::Transform identity_matrix;
5377 gfx::Transform parent_scale_matrix;
5378 SkMScalar initial_parent_scale = 1.75;
5379 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
5381 gfx::Transform child_scale_matrix;
5382 SkMScalar initial_child_scale = 1.25;
5383 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
5385 scoped_refptr<Layer> root = Layer::Create();
5386 root->SetBounds(gfx::Size(100, 100));
5388 scoped_refptr<FakePictureLayer> parent =
5389 CreateDrawablePictureLayer(&delegate);
5390 SetLayerPropertiesForTesting(parent.get(),
5391 parent_scale_matrix,
5392 gfx::Point3F(),
5393 gfx::PointF(),
5394 gfx::Size(100, 100),
5395 false,
5396 true);
5398 scoped_refptr<FakePictureLayer> child_scale =
5399 CreateDrawablePictureLayer(&delegate);
5400 SetLayerPropertiesForTesting(child_scale.get(),
5401 child_scale_matrix,
5402 gfx::Point3F(),
5403 gfx::PointF(2.f, 2.f),
5404 gfx::Size(10, 10),
5405 false,
5406 true);
5408 root->AddChild(parent);
5410 parent->AddChild(child_scale);
5412 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5413 host->SetRootLayer(root);
5416 RenderSurfaceLayerList render_surface_layer_list;
5417 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5418 root.get(), root->bounds(), &render_surface_layer_list);
5419 inputs.can_adjust_raster_scales = true;
5420 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5422 EXPECT_IDEAL_SCALE_EQ(initial_parent_scale, parent);
5423 // Animating layers compute ideal scale in the same way as when
5424 // they are static.
5425 EXPECT_IDEAL_SCALE_EQ(initial_child_scale * initial_parent_scale,
5426 child_scale);
5430 // TODO(sohanjg): Remove this test when ContentLayer is removed.
5431 TEST_F(LayerTreeHostCommonTest,
5432 ChangeInContentBoundsOrScaleTriggersPushProperties) {
5433 MockContentLayerClient delegate;
5434 scoped_refptr<Layer> root = Layer::Create();
5435 scoped_refptr<Layer> child = CreateDrawableContentLayer(&delegate);
5436 root->AddChild(child);
5438 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5439 host->SetRootLayer(root);
5441 gfx::Transform identity_matrix;
5442 SetLayerPropertiesForTesting(root.get(),
5443 identity_matrix,
5444 gfx::Point3F(),
5445 gfx::PointF(),
5446 gfx::Size(100, 100),
5447 true,
5448 false);
5449 SetLayerPropertiesForTesting(child.get(),
5450 identity_matrix,
5451 gfx::Point3F(),
5452 gfx::PointF(),
5453 gfx::Size(100, 100),
5454 true,
5455 false);
5457 root->reset_needs_push_properties_for_testing();
5458 child->reset_needs_push_properties_for_testing();
5460 gfx::Size device_viewport_size = gfx::Size(100, 100);
5461 RenderSurfaceLayerList render_surface_layer_list;
5462 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5463 root.get(), device_viewport_size, &render_surface_layer_list);
5464 inputs.device_scale_factor = 1.f;
5465 inputs.can_adjust_raster_scales = true;
5466 inputs.verify_property_trees = false;
5468 // This will change both layers' content bounds.
5469 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5470 EXPECT_TRUE(root->needs_push_properties());
5471 EXPECT_TRUE(child->needs_push_properties());
5473 root->reset_needs_push_properties_for_testing();
5474 child->reset_needs_push_properties_for_testing();
5476 // This will change only the child layer's contents scale and content bounds,
5477 // since the root layer is not a ContentsScalingLayer.
5478 inputs.device_scale_factor = 2.f;
5479 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5480 EXPECT_FALSE(root->needs_push_properties());
5481 EXPECT_TRUE(child->needs_push_properties());
5483 root->reset_needs_push_properties_for_testing();
5484 child->reset_needs_push_properties_for_testing();
5486 // This will not change either layer's contents scale or content bounds.
5487 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5488 EXPECT_FALSE(root->needs_push_properties());
5489 EXPECT_FALSE(child->needs_push_properties());
5492 TEST_F(LayerTreeHostCommonTest, RenderSurfaceTransformsInHighDPI) {
5493 MockContentLayerClient delegate;
5494 gfx::Transform identity_matrix;
5496 scoped_refptr<FakePictureLayer> parent =
5497 CreateDrawablePictureLayer(&delegate);
5498 SetLayerPropertiesForTesting(parent.get(),
5499 identity_matrix,
5500 gfx::Point3F(),
5501 gfx::PointF(),
5502 gfx::Size(30, 30),
5503 false,
5504 true);
5506 scoped_refptr<FakePictureLayer> child = CreateDrawablePictureLayer(&delegate);
5507 SetLayerPropertiesForTesting(child.get(),
5508 identity_matrix,
5509 gfx::Point3F(),
5510 gfx::PointF(2.f, 2.f),
5511 gfx::Size(10, 10),
5512 false,
5513 true);
5515 gfx::Transform replica_transform;
5516 replica_transform.Scale(1.0, -1.0);
5517 scoped_refptr<FakePictureLayer> replica =
5518 CreateDrawablePictureLayer(&delegate);
5519 SetLayerPropertiesForTesting(replica.get(),
5520 replica_transform,
5521 gfx::Point3F(),
5522 gfx::PointF(2.f, 2.f),
5523 gfx::Size(10, 10),
5524 false,
5525 true);
5527 // This layer should end up in the same surface as child, with the same draw
5528 // and screen space transforms.
5529 scoped_refptr<FakePictureLayer> duplicate_child_non_owner =
5530 CreateDrawablePictureLayer(&delegate);
5531 SetLayerPropertiesForTesting(duplicate_child_non_owner.get(),
5532 identity_matrix,
5533 gfx::Point3F(),
5534 gfx::PointF(),
5535 gfx::Size(10, 10),
5536 false,
5537 true);
5539 parent->AddChild(child);
5540 child->AddChild(duplicate_child_non_owner);
5541 child->SetReplicaLayer(replica.get());
5543 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5544 host->SetRootLayer(parent);
5546 RenderSurfaceLayerList render_surface_layer_list;
5548 float device_scale_factor = 1.5f;
5549 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5550 parent.get(), parent->bounds(), &render_surface_layer_list);
5551 inputs.device_scale_factor = device_scale_factor;
5552 inputs.can_adjust_raster_scales = true;
5553 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5555 // We should have two render surfaces. The root's render surface and child's
5556 // render surface (it needs one because it has a replica layer).
5557 EXPECT_EQ(2u, render_surface_layer_list.size());
5559 gfx::Transform expected_parent_transform;
5560 expected_parent_transform.Scale(device_scale_factor, device_scale_factor);
5561 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
5562 parent->screen_space_transform());
5563 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
5564 parent->draw_transform());
5566 gfx::Transform expected_draw_transform;
5567 expected_draw_transform.Scale(device_scale_factor, device_scale_factor);
5568 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_draw_transform,
5569 child->draw_transform());
5571 gfx::Transform expected_screen_space_transform;
5572 expected_screen_space_transform.Scale(device_scale_factor,
5573 device_scale_factor);
5574 expected_screen_space_transform.Translate(child->position().x(),
5575 child->position().y());
5576 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_screen_space_transform,
5577 child->screen_space_transform());
5579 gfx::Transform expected_duplicate_child_draw_transform =
5580 child->draw_transform();
5581 EXPECT_TRANSFORMATION_MATRIX_EQ(child->draw_transform(),
5582 duplicate_child_non_owner->draw_transform());
5583 EXPECT_TRANSFORMATION_MATRIX_EQ(
5584 child->screen_space_transform(),
5585 duplicate_child_non_owner->screen_space_transform());
5586 EXPECT_EQ(child->drawable_content_rect(),
5587 duplicate_child_non_owner->drawable_content_rect());
5588 EXPECT_EQ(child->content_bounds(),
5589 duplicate_child_non_owner->content_bounds());
5591 gfx::Transform expected_render_surface_draw_transform;
5592 expected_render_surface_draw_transform.Translate(
5593 device_scale_factor * child->position().x(),
5594 device_scale_factor * child->position().y());
5595 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_render_surface_draw_transform,
5596 child->render_surface()->draw_transform());
5598 gfx::Transform expected_surface_draw_transform;
5599 expected_surface_draw_transform.Translate(device_scale_factor * 2.f,
5600 device_scale_factor * 2.f);
5601 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_surface_draw_transform,
5602 child->render_surface()->draw_transform());
5604 gfx::Transform expected_surface_screen_space_transform;
5605 expected_surface_screen_space_transform.Translate(device_scale_factor * 2.f,
5606 device_scale_factor * 2.f);
5607 EXPECT_TRANSFORMATION_MATRIX_EQ(
5608 expected_surface_screen_space_transform,
5609 child->render_surface()->screen_space_transform());
5611 gfx::Transform expected_replica_draw_transform;
5612 expected_replica_draw_transform.matrix().set(1, 1, -1.0);
5613 expected_replica_draw_transform.matrix().set(0, 3, 6.0);
5614 expected_replica_draw_transform.matrix().set(1, 3, 6.0);
5615 EXPECT_TRANSFORMATION_MATRIX_EQ(
5616 expected_replica_draw_transform,
5617 child->render_surface()->replica_draw_transform());
5619 gfx::Transform expected_replica_screen_space_transform;
5620 expected_replica_screen_space_transform.matrix().set(1, 1, -1.0);
5621 expected_replica_screen_space_transform.matrix().set(0, 3, 6.0);
5622 expected_replica_screen_space_transform.matrix().set(1, 3, 6.0);
5623 EXPECT_TRANSFORMATION_MATRIX_EQ(
5624 expected_replica_screen_space_transform,
5625 child->render_surface()->replica_screen_space_transform());
5626 EXPECT_TRANSFORMATION_MATRIX_EQ(
5627 expected_replica_screen_space_transform,
5628 child->render_surface()->replica_screen_space_transform());
5631 TEST_F(LayerTreeHostCommonTest,
5632 RenderSurfaceTransformsInHighDPIAccurateScaleZeroPosition) {
5633 MockContentLayerClient delegate;
5634 gfx::Transform identity_matrix;
5636 scoped_refptr<FakePictureLayer> parent =
5637 CreateDrawablePictureLayer(&delegate);
5638 SetLayerPropertiesForTesting(parent.get(),
5639 identity_matrix,
5640 gfx::Point3F(),
5641 gfx::PointF(),
5642 gfx::Size(33, 31),
5643 false,
5644 true);
5646 scoped_refptr<FakePictureLayer> child = CreateDrawablePictureLayer(&delegate);
5647 SetLayerPropertiesForTesting(child.get(),
5648 identity_matrix,
5649 gfx::Point3F(),
5650 gfx::PointF(),
5651 gfx::Size(13, 11),
5652 false,
5653 true);
5655 gfx::Transform replica_transform;
5656 replica_transform.Scale(1.0, -1.0);
5657 scoped_refptr<FakePictureLayer> replica =
5658 CreateDrawablePictureLayer(&delegate);
5659 SetLayerPropertiesForTesting(replica.get(),
5660 replica_transform,
5661 gfx::Point3F(),
5662 gfx::PointF(),
5663 gfx::Size(13, 11),
5664 false,
5665 true);
5667 parent->AddChild(child);
5668 child->SetReplicaLayer(replica.get());
5670 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5671 host->SetRootLayer(parent);
5673 float device_scale_factor = 1.7f;
5675 RenderSurfaceLayerList render_surface_layer_list;
5676 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5677 parent.get(), parent->bounds(), &render_surface_layer_list);
5678 inputs.device_scale_factor = device_scale_factor;
5679 inputs.can_adjust_raster_scales = true;
5680 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5682 // We should have two render surfaces. The root's render surface and child's
5683 // render surface (it needs one because it has a replica layer).
5684 EXPECT_EQ(2u, render_surface_layer_list.size());
5686 gfx::Transform identity_transform;
5687 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
5688 child->render_surface()->draw_transform());
5689 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
5690 child->render_surface()->draw_transform());
5691 EXPECT_TRANSFORMATION_MATRIX_EQ(
5692 identity_transform, child->render_surface()->screen_space_transform());
5694 gfx::Transform expected_replica_draw_transform;
5695 expected_replica_draw_transform.matrix().set(1, 1, -1.0);
5696 EXPECT_TRANSFORMATION_MATRIX_EQ(
5697 expected_replica_draw_transform,
5698 child->render_surface()->replica_draw_transform());
5700 gfx::Transform expected_replica_screen_space_transform;
5701 expected_replica_screen_space_transform.matrix().set(1, 1, -1.0);
5702 EXPECT_TRANSFORMATION_MATRIX_EQ(
5703 expected_replica_screen_space_transform,
5704 child->render_surface()->replica_screen_space_transform());
5707 TEST_F(LayerTreeHostCommonTest, SubtreeSearch) {
5708 scoped_refptr<Layer> root = Layer::Create();
5709 scoped_refptr<Layer> child = Layer::Create();
5710 scoped_refptr<Layer> grand_child = Layer::Create();
5711 scoped_refptr<Layer> mask_layer = Layer::Create();
5712 scoped_refptr<Layer> replica_layer = Layer::Create();
5714 grand_child->SetReplicaLayer(replica_layer.get());
5715 child->AddChild(grand_child.get());
5716 child->SetMaskLayer(mask_layer.get());
5717 root->AddChild(child.get());
5719 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5720 host->SetRootLayer(root);
5722 int nonexistent_id = -1;
5723 EXPECT_EQ(root.get(),
5724 LayerTreeHostCommon::FindLayerInSubtree(root.get(), root->id()));
5725 EXPECT_EQ(child.get(),
5726 LayerTreeHostCommon::FindLayerInSubtree(root.get(), child->id()));
5727 EXPECT_EQ(
5728 grand_child.get(),
5729 LayerTreeHostCommon::FindLayerInSubtree(root.get(), grand_child->id()));
5730 EXPECT_EQ(
5731 mask_layer.get(),
5732 LayerTreeHostCommon::FindLayerInSubtree(root.get(), mask_layer->id()));
5733 EXPECT_EQ(
5734 replica_layer.get(),
5735 LayerTreeHostCommon::FindLayerInSubtree(root.get(), replica_layer->id()));
5736 EXPECT_EQ(
5737 0, LayerTreeHostCommon::FindLayerInSubtree(root.get(), nonexistent_id));
5740 TEST_F(LayerTreeHostCommonTest, TransparentChildRenderSurfaceCreation) {
5741 scoped_refptr<Layer> root = Layer::Create();
5742 scoped_refptr<Layer> child = Layer::Create();
5743 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
5744 make_scoped_refptr(new LayerWithForcedDrawsContent());
5746 const gfx::Transform identity_matrix;
5747 SetLayerPropertiesForTesting(root.get(),
5748 identity_matrix,
5749 gfx::Point3F(),
5750 gfx::PointF(),
5751 gfx::Size(100, 100),
5752 true,
5753 false);
5754 SetLayerPropertiesForTesting(child.get(),
5755 identity_matrix,
5756 gfx::Point3F(),
5757 gfx::PointF(),
5758 gfx::Size(10, 10),
5759 true,
5760 false);
5761 SetLayerPropertiesForTesting(grand_child.get(),
5762 identity_matrix,
5763 gfx::Point3F(),
5764 gfx::PointF(),
5765 gfx::Size(10, 10),
5766 true,
5767 false);
5769 root->AddChild(child);
5770 child->AddChild(grand_child);
5771 child->SetOpacity(0.5f);
5773 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5774 host->SetRootLayer(root);
5776 ExecuteCalculateDrawProperties(root.get());
5778 EXPECT_FALSE(child->render_surface());
5781 TEST_F(LayerTreeHostCommonTest, OpacityAnimatingOnPendingTree) {
5782 FakeImplProxy proxy;
5783 TestSharedBitmapManager shared_bitmap_manager;
5784 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
5785 host_impl.CreatePendingTree();
5786 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
5788 const gfx::Transform identity_matrix;
5789 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
5790 gfx::PointF(), gfx::Size(100, 100), true, false,
5791 false);
5792 root->SetDrawsContent(true);
5794 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
5795 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
5796 gfx::PointF(), gfx::Size(50, 50), true, false,
5797 false);
5798 child->SetDrawsContent(true);
5799 child->SetOpacity(0.0f);
5801 // Add opacity animation.
5802 AddOpacityTransitionToController(
5803 child->layer_animation_controller(), 10.0, 0.0f, 1.0f, false);
5805 root->AddChild(child.Pass());
5806 root->SetHasRenderSurface(true);
5808 LayerImplList render_surface_layer_list;
5809 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
5810 root.get(), root->bounds(), &render_surface_layer_list);
5811 inputs.can_adjust_raster_scales = true;
5812 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5814 // We should have one render surface and two layers. The child
5815 // layer should be included even though it is transparent.
5816 ASSERT_EQ(1u, render_surface_layer_list.size());
5817 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
5820 using LCDTextTestParam = std::tr1::tuple<bool, bool, bool>;
5821 class LCDTextTest
5822 : public LayerTreeHostCommonTestBase,
5823 public testing::TestWithParam<LCDTextTestParam> {
5824 public:
5825 LCDTextTest()
5826 : host_impl_(&proxy_, &shared_bitmap_manager_, &task_graph_runner_),
5827 root_(nullptr),
5828 child_(nullptr),
5829 grand_child_(nullptr) {}
5831 protected:
5832 void SetUp() override {
5833 can_use_lcd_text_ = std::tr1::get<0>(GetParam());
5834 layers_always_allowed_lcd_text_ = std::tr1::get<1>(GetParam());
5836 scoped_ptr<LayerImpl> root_ptr =
5837 LayerImpl::Create(host_impl_.active_tree(), 1);
5838 scoped_ptr<LayerImpl> child_ptr =
5839 LayerImpl::Create(host_impl_.active_tree(), 2);
5840 scoped_ptr<LayerImpl> grand_child_ptr =
5841 LayerImpl::Create(host_impl_.active_tree(), 3);
5843 // Stash raw pointers to look at later.
5844 root_ = root_ptr.get();
5845 child_ = child_ptr.get();
5846 grand_child_ = grand_child_ptr.get();
5848 child_->AddChild(grand_child_ptr.Pass());
5849 root_->AddChild(child_ptr.Pass());
5850 host_impl_.active_tree()->SetRootLayer(root_ptr.Pass());
5852 root_->SetContentsOpaque(true);
5853 child_->SetContentsOpaque(true);
5854 grand_child_->SetContentsOpaque(true);
5856 gfx::Transform identity_matrix;
5857 SetLayerPropertiesForTesting(root_, identity_matrix, gfx::Point3F(),
5858 gfx::PointF(), gfx::Size(1, 1), true, false,
5859 true);
5860 SetLayerPropertiesForTesting(child_, identity_matrix, gfx::Point3F(),
5861 gfx::PointF(), gfx::Size(1, 1), true, false,
5862 std::tr1::get<2>(GetParam()));
5863 SetLayerPropertiesForTesting(grand_child_, identity_matrix, gfx::Point3F(),
5864 gfx::PointF(), gfx::Size(1, 1), true, false,
5865 false);
5868 bool can_use_lcd_text_;
5869 bool layers_always_allowed_lcd_text_;
5871 FakeImplProxy proxy_;
5872 TestSharedBitmapManager shared_bitmap_manager_;
5873 TestTaskGraphRunner task_graph_runner_;
5874 FakeLayerTreeHostImpl host_impl_;
5876 LayerImpl* root_;
5877 LayerImpl* child_;
5878 LayerImpl* grand_child_;
5881 TEST_P(LCDTextTest, CanUseLCDText) {
5882 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
5883 bool expect_not_lcd_text = layers_always_allowed_lcd_text_;
5885 // Case 1: Identity transform.
5886 gfx::Transform identity_matrix;
5887 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5888 layers_always_allowed_lcd_text_);
5889 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5890 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5891 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5893 // Case 2: Integral translation.
5894 gfx::Transform integral_translation;
5895 integral_translation.Translate(1.0, 2.0);
5896 child_->SetTransform(integral_translation);
5897 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5898 layers_always_allowed_lcd_text_);
5899 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5900 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5901 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5903 // Case 3: Non-integral translation.
5904 gfx::Transform non_integral_translation;
5905 non_integral_translation.Translate(1.5, 2.5);
5906 child_->SetTransform(non_integral_translation);
5907 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5908 layers_always_allowed_lcd_text_);
5909 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5910 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5911 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5913 // Case 4: Rotation.
5914 gfx::Transform rotation;
5915 rotation.Rotate(10.0);
5916 child_->SetTransform(rotation);
5917 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5918 layers_always_allowed_lcd_text_);
5919 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5920 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5921 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5923 // Case 5: Scale.
5924 gfx::Transform scale;
5925 scale.Scale(2.0, 2.0);
5926 child_->SetTransform(scale);
5927 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5928 layers_always_allowed_lcd_text_);
5929 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5930 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5931 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5933 // Case 6: Skew.
5934 gfx::Transform skew;
5935 skew.SkewX(10.0);
5936 child_->SetTransform(skew);
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_not_lcd_text, grand_child_->can_use_lcd_text());
5943 // Case 7: Translucent.
5944 child_->SetTransform(identity_matrix);
5945 child_->SetOpacity(0.5f);
5946 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5947 layers_always_allowed_lcd_text_);
5948 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5949 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5950 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5952 // Case 8: Sanity check: restore transform and opacity.
5953 child_->SetTransform(identity_matrix);
5954 child_->SetOpacity(1.f);
5955 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5956 layers_always_allowed_lcd_text_);
5957 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5958 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5959 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5961 // Case 9: Non-opaque content.
5962 child_->SetContentsOpaque(false);
5963 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5964 layers_always_allowed_lcd_text_);
5965 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5966 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5967 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5969 // Case 10: Sanity check: restore content opaqueness.
5970 child_->SetContentsOpaque(true);
5971 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5972 layers_always_allowed_lcd_text_);
5973 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5974 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5975 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5978 TEST_P(LCDTextTest, CanUseLCDTextWithAnimation) {
5979 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
5980 bool expect_not_lcd_text = layers_always_allowed_lcd_text_;
5982 // Sanity check: Make sure can_use_lcd_text_ is set on each node.
5983 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5984 layers_always_allowed_lcd_text_);
5985 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5986 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5987 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5989 // Add opacity animation.
5990 child_->SetOpacity(0.9f);
5991 AddOpacityTransitionToController(
5992 child_->layer_animation_controller(), 10.0, 0.9f, 0.1f, false);
5994 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5995 layers_always_allowed_lcd_text_);
5996 // Text LCD should be adjusted while animation is active.
5997 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5998 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5999 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
6002 TEST_P(LCDTextTest, CanUseLCDTextWithAnimationContentsOpaque) {
6003 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
6004 bool expect_not_lcd_text = layers_always_allowed_lcd_text_;
6006 // Sanity check: Make sure can_use_lcd_text_ is set on each node.
6007 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
6008 layers_always_allowed_lcd_text_);
6009 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
6010 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
6011 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
6013 // Mark contents non-opaque within the first animation frame.
6014 child_->SetContentsOpaque(false);
6015 AddOpacityTransitionToController(child_->layer_animation_controller(), 10.0,
6016 0.9f, 0.1f, false);
6018 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
6019 layers_always_allowed_lcd_text_);
6020 // LCD text should be disabled for non-opaque layers even during animations.
6021 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
6022 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
6023 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
6026 INSTANTIATE_TEST_CASE_P(LayerTreeHostCommonTest,
6027 LCDTextTest,
6028 testing::Combine(testing::Bool(),
6029 testing::Bool(),
6030 testing::Bool()));
6032 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_SingleLayer) {
6033 FakeImplProxy proxy;
6034 TestSharedBitmapManager shared_bitmap_manager;
6035 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6036 host_impl.CreatePendingTree();
6037 const gfx::Transform identity_matrix;
6039 scoped_refptr<Layer> root = Layer::Create();
6040 SetLayerPropertiesForTesting(root.get(),
6041 identity_matrix,
6042 gfx::Point3F(),
6043 gfx::PointF(),
6044 gfx::Size(50, 50),
6045 true,
6046 false);
6047 root->SetIsDrawable(true);
6049 scoped_refptr<Layer> child = Layer::Create();
6050 SetLayerPropertiesForTesting(child.get(),
6051 identity_matrix,
6052 gfx::Point3F(),
6053 gfx::PointF(),
6054 gfx::Size(40, 40),
6055 true,
6056 false);
6057 child->SetIsDrawable(true);
6059 scoped_refptr<Layer> grand_child = Layer::Create();
6060 SetLayerPropertiesForTesting(grand_child.get(),
6061 identity_matrix,
6062 gfx::Point3F(),
6063 gfx::PointF(),
6064 gfx::Size(30, 30),
6065 true,
6066 false);
6067 grand_child->SetIsDrawable(true);
6068 grand_child->SetHideLayerAndSubtree(true);
6070 child->AddChild(grand_child);
6071 root->AddChild(child);
6073 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6074 host->SetRootLayer(root);
6076 RenderSurfaceLayerList render_surface_layer_list;
6077 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6078 root.get(), root->bounds(), &render_surface_layer_list);
6079 inputs.can_adjust_raster_scales = true;
6080 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6082 // We should have one render surface and two layers. The grand child has
6083 // hidden itself.
6084 ASSERT_EQ(1u, render_surface_layer_list.size());
6085 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6086 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6087 EXPECT_EQ(child->id(), root->render_surface()->layer_list().at(1)->id());
6090 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_SingleLayerImpl) {
6091 FakeImplProxy proxy;
6092 TestSharedBitmapManager shared_bitmap_manager;
6093 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6094 host_impl.CreatePendingTree();
6095 const gfx::Transform identity_matrix;
6097 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
6098 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
6099 gfx::PointF(), gfx::Size(50, 50), true, false,
6100 false);
6101 root->SetDrawsContent(true);
6103 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
6104 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
6105 gfx::PointF(), gfx::Size(40, 40), true, false,
6106 false);
6107 child->SetDrawsContent(true);
6109 scoped_ptr<LayerImpl> grand_child =
6110 LayerImpl::Create(host_impl.pending_tree(), 3);
6111 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
6112 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6113 true, false, false);
6114 grand_child->SetDrawsContent(true);
6115 grand_child->SetHideLayerAndSubtree(true);
6117 child->AddChild(grand_child.Pass());
6118 root->AddChild(child.Pass());
6119 root->SetHasRenderSurface(true);
6121 LayerImplList render_surface_layer_list;
6122 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6123 root.get(), root->bounds(), &render_surface_layer_list);
6124 inputs.can_adjust_raster_scales = true;
6125 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6127 // We should have one render surface and two layers. The grand child has
6128 // hidden itself.
6129 ASSERT_EQ(1u, render_surface_layer_list.size());
6130 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6131 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id());
6132 EXPECT_EQ(2, root->render_surface()->layer_list().at(1)->id());
6135 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_TwoLayers) {
6136 FakeImplProxy proxy;
6137 TestSharedBitmapManager shared_bitmap_manager;
6138 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6139 host_impl.CreatePendingTree();
6140 const gfx::Transform identity_matrix;
6142 scoped_refptr<Layer> root = Layer::Create();
6143 SetLayerPropertiesForTesting(root.get(),
6144 identity_matrix,
6145 gfx::Point3F(),
6146 gfx::PointF(),
6147 gfx::Size(50, 50),
6148 true,
6149 false);
6150 root->SetIsDrawable(true);
6152 scoped_refptr<Layer> child = Layer::Create();
6153 SetLayerPropertiesForTesting(child.get(),
6154 identity_matrix,
6155 gfx::Point3F(),
6156 gfx::PointF(),
6157 gfx::Size(40, 40),
6158 true,
6159 false);
6160 child->SetIsDrawable(true);
6161 child->SetHideLayerAndSubtree(true);
6163 scoped_refptr<Layer> grand_child = Layer::Create();
6164 SetLayerPropertiesForTesting(grand_child.get(),
6165 identity_matrix,
6166 gfx::Point3F(),
6167 gfx::PointF(),
6168 gfx::Size(30, 30),
6169 true,
6170 false);
6171 grand_child->SetIsDrawable(true);
6173 child->AddChild(grand_child);
6174 root->AddChild(child);
6176 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6177 host->SetRootLayer(root);
6179 RenderSurfaceLayerList render_surface_layer_list;
6180 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6181 root.get(), root->bounds(), &render_surface_layer_list);
6182 inputs.can_adjust_raster_scales = true;
6183 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6185 // We should have one render surface and one layers. The child has
6186 // hidden itself and the grand child.
6187 ASSERT_EQ(1u, render_surface_layer_list.size());
6188 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6189 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6192 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_TwoLayersImpl) {
6193 FakeImplProxy proxy;
6194 TestSharedBitmapManager shared_bitmap_manager;
6195 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6196 host_impl.CreatePendingTree();
6197 const gfx::Transform identity_matrix;
6199 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
6200 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
6201 gfx::PointF(), gfx::Size(50, 50), true, false,
6202 true);
6203 root->SetDrawsContent(true);
6205 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
6206 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
6207 gfx::PointF(), gfx::Size(40, 40), true, false,
6208 false);
6209 child->SetDrawsContent(true);
6210 child->SetHideLayerAndSubtree(true);
6212 scoped_ptr<LayerImpl> grand_child =
6213 LayerImpl::Create(host_impl.pending_tree(), 3);
6214 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
6215 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6216 true, false, false);
6217 grand_child->SetDrawsContent(true);
6219 child->AddChild(grand_child.Pass());
6220 root->AddChild(child.Pass());
6222 LayerImplList render_surface_layer_list;
6223 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6224 root.get(), root->bounds(), &render_surface_layer_list);
6225 inputs.can_adjust_raster_scales = true;
6226 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6228 // We should have one render surface and one layers. The child has
6229 // hidden itself and the grand child.
6230 ASSERT_EQ(1u, render_surface_layer_list.size());
6231 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6232 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id());
6235 void EmptyCopyOutputCallback(scoped_ptr<CopyOutputResult> result) {}
6237 TEST_F(LayerTreeHostCommonTest, SubtreeHiddenWithCopyRequest) {
6238 FakeImplProxy proxy;
6239 TestSharedBitmapManager shared_bitmap_manager;
6240 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6241 host_impl.CreatePendingTree();
6242 const gfx::Transform identity_matrix;
6244 scoped_refptr<Layer> root = Layer::Create();
6245 SetLayerPropertiesForTesting(root.get(),
6246 identity_matrix,
6247 gfx::Point3F(),
6248 gfx::PointF(),
6249 gfx::Size(50, 50),
6250 true,
6251 false);
6252 root->SetIsDrawable(true);
6254 scoped_refptr<Layer> copy_grand_parent = Layer::Create();
6255 SetLayerPropertiesForTesting(copy_grand_parent.get(),
6256 identity_matrix,
6257 gfx::Point3F(),
6258 gfx::PointF(),
6259 gfx::Size(40, 40),
6260 true,
6261 false);
6262 copy_grand_parent->SetIsDrawable(true);
6264 scoped_refptr<Layer> copy_parent = Layer::Create();
6265 SetLayerPropertiesForTesting(copy_parent.get(),
6266 identity_matrix,
6267 gfx::Point3F(),
6268 gfx::PointF(),
6269 gfx::Size(30, 30),
6270 true,
6271 false);
6272 copy_parent->SetIsDrawable(true);
6273 copy_parent->SetForceRenderSurface(true);
6275 scoped_refptr<Layer> copy_layer = Layer::Create();
6276 SetLayerPropertiesForTesting(copy_layer.get(),
6277 identity_matrix,
6278 gfx::Point3F(),
6279 gfx::PointF(),
6280 gfx::Size(20, 20),
6281 true,
6282 false);
6283 copy_layer->SetIsDrawable(true);
6285 scoped_refptr<Layer> copy_child = Layer::Create();
6286 SetLayerPropertiesForTesting(copy_child.get(),
6287 identity_matrix,
6288 gfx::Point3F(),
6289 gfx::PointF(),
6290 gfx::Size(20, 20),
6291 true,
6292 false);
6293 copy_child->SetIsDrawable(true);
6295 scoped_refptr<Layer> copy_grand_parent_sibling_before = Layer::Create();
6296 SetLayerPropertiesForTesting(copy_grand_parent_sibling_before.get(),
6297 identity_matrix,
6298 gfx::Point3F(),
6299 gfx::PointF(),
6300 gfx::Size(40, 40),
6301 true,
6302 false);
6303 copy_grand_parent_sibling_before->SetIsDrawable(true);
6305 scoped_refptr<Layer> copy_grand_parent_sibling_after = Layer::Create();
6306 SetLayerPropertiesForTesting(copy_grand_parent_sibling_after.get(),
6307 identity_matrix,
6308 gfx::Point3F(),
6309 gfx::PointF(),
6310 gfx::Size(40, 40),
6311 true,
6312 false);
6313 copy_grand_parent_sibling_after->SetIsDrawable(true);
6315 copy_layer->AddChild(copy_child);
6316 copy_parent->AddChild(copy_layer);
6317 copy_grand_parent->AddChild(copy_parent);
6318 root->AddChild(copy_grand_parent_sibling_before);
6319 root->AddChild(copy_grand_parent);
6320 root->AddChild(copy_grand_parent_sibling_after);
6322 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6323 host->SetRootLayer(root);
6325 // Hide the copy_grand_parent and its subtree. But make a copy request in that
6326 // hidden subtree on copy_layer.
6327 copy_grand_parent->SetHideLayerAndSubtree(true);
6328 copy_grand_parent_sibling_before->SetHideLayerAndSubtree(true);
6329 copy_grand_parent_sibling_after->SetHideLayerAndSubtree(true);
6330 copy_layer->RequestCopyOfOutput(CopyOutputRequest::CreateRequest(
6331 base::Bind(&EmptyCopyOutputCallback)));
6332 EXPECT_TRUE(copy_layer->HasCopyRequest());
6334 RenderSurfaceLayerList render_surface_layer_list;
6335 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6336 root.get(), root->bounds(), &render_surface_layer_list);
6337 inputs.can_adjust_raster_scales = true;
6338 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6340 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
6341 EXPECT_TRUE(copy_grand_parent->draw_properties().
6342 layer_or_descendant_has_copy_request);
6343 EXPECT_TRUE(copy_parent->draw_properties().
6344 layer_or_descendant_has_copy_request);
6345 EXPECT_TRUE(copy_layer->draw_properties().
6346 layer_or_descendant_has_copy_request);
6347 EXPECT_FALSE(copy_child->draw_properties().
6348 layer_or_descendant_has_copy_request);
6349 EXPECT_FALSE(copy_grand_parent_sibling_before->draw_properties().
6350 layer_or_descendant_has_copy_request);
6351 EXPECT_FALSE(copy_grand_parent_sibling_after->draw_properties().
6352 layer_or_descendant_has_copy_request);
6354 // We should have three render surfaces, one for the root, one for the parent
6355 // since it owns a surface, and one for the copy_layer.
6356 ASSERT_EQ(3u, render_surface_layer_list.size());
6357 EXPECT_EQ(root->id(), render_surface_layer_list.at(0)->id());
6358 EXPECT_EQ(copy_parent->id(), render_surface_layer_list.at(1)->id());
6359 EXPECT_EQ(copy_layer->id(), render_surface_layer_list.at(2)->id());
6361 // The root render surface should have 2 contributing layers. The
6362 // copy_grand_parent is hidden along with its siblings, but the copy_parent
6363 // will appear since something in its subtree needs to be drawn for a copy
6364 // request.
6365 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6366 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6367 EXPECT_EQ(copy_parent->id(),
6368 root->render_surface()->layer_list().at(1)->id());
6370 // Nothing actually draws into the copy parent, so only the copy_layer will
6371 // appear in its list, since it needs to be drawn for the copy request.
6372 ASSERT_EQ(1u, copy_parent->render_surface()->layer_list().size());
6373 EXPECT_EQ(copy_layer->id(),
6374 copy_parent->render_surface()->layer_list().at(0)->id());
6376 // The copy_layer's render surface should have two contributing layers.
6377 ASSERT_EQ(2u, copy_layer->render_surface()->layer_list().size());
6378 EXPECT_EQ(copy_layer->id(),
6379 copy_layer->render_surface()->layer_list().at(0)->id());
6380 EXPECT_EQ(copy_child->id(),
6381 copy_layer->render_surface()->layer_list().at(1)->id());
6384 TEST_F(LayerTreeHostCommonTest, ClippedOutCopyRequest) {
6385 FakeImplProxy proxy;
6386 TestSharedBitmapManager shared_bitmap_manager;
6387 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6388 host_impl.CreatePendingTree();
6389 const gfx::Transform identity_matrix;
6391 scoped_refptr<Layer> root = Layer::Create();
6392 SetLayerPropertiesForTesting(root.get(),
6393 identity_matrix,
6394 gfx::Point3F(),
6395 gfx::PointF(),
6396 gfx::Size(50, 50),
6397 true,
6398 false);
6399 root->SetIsDrawable(true);
6401 scoped_refptr<Layer> copy_parent = Layer::Create();
6402 SetLayerPropertiesForTesting(copy_parent.get(),
6403 identity_matrix,
6404 gfx::Point3F(),
6405 gfx::PointF(),
6406 gfx::Size(),
6407 true,
6408 false);
6409 copy_parent->SetIsDrawable(true);
6410 copy_parent->SetMasksToBounds(true);
6412 scoped_refptr<Layer> copy_layer = Layer::Create();
6413 SetLayerPropertiesForTesting(copy_layer.get(),
6414 identity_matrix,
6415 gfx::Point3F(),
6416 gfx::PointF(),
6417 gfx::Size(30, 30),
6418 true,
6419 false);
6420 copy_layer->SetIsDrawable(true);
6422 scoped_refptr<Layer> copy_child = Layer::Create();
6423 SetLayerPropertiesForTesting(copy_child.get(),
6424 identity_matrix,
6425 gfx::Point3F(),
6426 gfx::PointF(),
6427 gfx::Size(20, 20),
6428 true,
6429 false);
6430 copy_child->SetIsDrawable(true);
6432 copy_layer->AddChild(copy_child);
6433 copy_parent->AddChild(copy_layer);
6434 root->AddChild(copy_parent);
6436 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6437 host->SetRootLayer(root);
6439 copy_layer->RequestCopyOfOutput(CopyOutputRequest::CreateRequest(
6440 base::Bind(&EmptyCopyOutputCallback)));
6441 EXPECT_TRUE(copy_layer->HasCopyRequest());
6443 RenderSurfaceLayerList render_surface_layer_list;
6444 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6445 root.get(), root->bounds(), &render_surface_layer_list);
6446 inputs.can_adjust_raster_scales = true;
6447 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6449 // We should have one render surface, as the others are clipped out.
6450 ASSERT_EQ(1u, render_surface_layer_list.size());
6451 EXPECT_EQ(root->id(), render_surface_layer_list.at(0)->id());
6453 // The root render surface should only have 1 contributing layer, since the
6454 // other layers are empty/clipped away.
6455 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6456 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6459 TEST_F(LayerTreeHostCommonTest, VisibleContentRectInsideSurface) {
6460 FakeImplProxy proxy;
6461 TestSharedBitmapManager shared_bitmap_manager;
6462 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6463 host_impl.CreatePendingTree();
6464 const gfx::Transform identity_matrix;
6466 scoped_refptr<Layer> root = Layer::Create();
6467 SetLayerPropertiesForTesting(root.get(),
6468 identity_matrix,
6469 gfx::Point3F(),
6470 gfx::PointF(),
6471 gfx::Size(50, 50),
6472 true,
6473 false);
6474 root->SetIsDrawable(true);
6476 // The surface is moved slightly outside of the viewport.
6477 scoped_refptr<Layer> surface = Layer::Create();
6478 SetLayerPropertiesForTesting(surface.get(),
6479 identity_matrix,
6480 gfx::Point3F(),
6481 gfx::PointF(-10, -20),
6482 gfx::Size(),
6483 true,
6484 false);
6485 surface->SetForceRenderSurface(true);
6487 scoped_refptr<Layer> surface_child = Layer::Create();
6488 SetLayerPropertiesForTesting(surface_child.get(),
6489 identity_matrix,
6490 gfx::Point3F(),
6491 gfx::PointF(),
6492 gfx::Size(50, 50),
6493 true,
6494 false);
6495 surface_child->SetIsDrawable(true);
6497 surface->AddChild(surface_child);
6498 root->AddChild(surface);
6500 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6501 host->SetRootLayer(root);
6503 RenderSurfaceLayerList render_surface_layer_list;
6504 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6505 root.get(), root->bounds(), &render_surface_layer_list);
6506 inputs.can_adjust_raster_scales = true;
6507 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6509 // The visible_content_rect for the |surface_child| should not be clipped by
6510 // the viewport.
6511 EXPECT_EQ(gfx::Rect(50, 50).ToString(),
6512 surface_child->visible_content_rect().ToString());
6515 TEST_F(LayerTreeHostCommonTest, TransformedClipParent) {
6516 // Ensure that a transform between the layer and its render surface is not a
6517 // problem. Constructs the following layer tree.
6519 // root (a render surface)
6520 // + render_surface
6521 // + clip_parent (scaled)
6522 // + intervening_clipping_layer
6523 // + clip_child
6525 // The render surface should be resized correctly and the clip child should
6526 // inherit the right clip rect.
6527 scoped_refptr<Layer> root = Layer::Create();
6528 scoped_refptr<Layer> render_surface = Layer::Create();
6529 scoped_refptr<Layer> clip_parent = Layer::Create();
6530 scoped_refptr<Layer> intervening = Layer::Create();
6531 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6532 make_scoped_refptr(new LayerWithForcedDrawsContent);
6534 root->AddChild(render_surface);
6535 render_surface->AddChild(clip_parent);
6536 clip_parent->AddChild(intervening);
6537 intervening->AddChild(clip_child);
6539 clip_child->SetClipParent(clip_parent.get());
6541 intervening->SetMasksToBounds(true);
6542 clip_parent->SetMasksToBounds(true);
6544 render_surface->SetForceRenderSurface(true);
6546 gfx::Transform scale_transform;
6547 scale_transform.Scale(2, 2);
6549 gfx::Transform identity_transform;
6551 SetLayerPropertiesForTesting(root.get(),
6552 identity_transform,
6553 gfx::Point3F(),
6554 gfx::PointF(),
6555 gfx::Size(50, 50),
6556 true,
6557 false);
6558 SetLayerPropertiesForTesting(render_surface.get(),
6559 identity_transform,
6560 gfx::Point3F(),
6561 gfx::PointF(),
6562 gfx::Size(10, 10),
6563 true,
6564 false);
6565 SetLayerPropertiesForTesting(clip_parent.get(),
6566 scale_transform,
6567 gfx::Point3F(),
6568 gfx::PointF(1.f, 1.f),
6569 gfx::Size(10, 10),
6570 true,
6571 false);
6572 SetLayerPropertiesForTesting(intervening.get(),
6573 identity_transform,
6574 gfx::Point3F(),
6575 gfx::PointF(1.f, 1.f),
6576 gfx::Size(5, 5),
6577 true,
6578 false);
6579 SetLayerPropertiesForTesting(clip_child.get(),
6580 identity_transform,
6581 gfx::Point3F(),
6582 gfx::PointF(1.f, 1.f),
6583 gfx::Size(10, 10),
6584 true,
6585 false);
6587 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6588 host->SetRootLayer(root);
6590 ExecuteCalculateDrawProperties(root.get());
6592 ASSERT_TRUE(root->render_surface());
6593 ASSERT_TRUE(render_surface->render_surface());
6595 // Ensure that we've inherited our clip parent's clip and weren't affected
6596 // by the intervening clip layer.
6597 ASSERT_EQ(gfx::Rect(1, 1, 20, 20).ToString(),
6598 clip_parent->clip_rect().ToString());
6599 ASSERT_EQ(clip_parent->clip_rect().ToString(),
6600 clip_child->clip_rect().ToString());
6601 ASSERT_EQ(gfx::Rect(3, 3, 10, 10).ToString(),
6602 intervening->clip_rect().ToString());
6604 // Ensure that the render surface reports a content rect that has been grown
6605 // to accomodate for the clip child.
6606 ASSERT_EQ(gfx::Rect(5, 5, 16, 16).ToString(),
6607 render_surface->render_surface()->content_rect().ToString());
6609 // The above check implies the two below, but they nicely demonstrate that
6610 // we've grown, despite the intervening layer's clip.
6611 ASSERT_TRUE(clip_parent->clip_rect().Contains(
6612 render_surface->render_surface()->content_rect()));
6613 ASSERT_FALSE(intervening->clip_rect().Contains(
6614 render_surface->render_surface()->content_rect()));
6617 TEST_F(LayerTreeHostCommonTest, ClipParentWithInterveningRenderSurface) {
6618 // Ensure that intervening render surfaces are not a problem in the basic
6619 // case. In the following tree, both render surfaces should be resized to
6620 // accomodate for the clip child, despite an intervening clip.
6622 // root (a render surface)
6623 // + clip_parent (masks to bounds)
6624 // + render_surface1 (sets opacity)
6625 // + intervening (masks to bounds)
6626 // + render_surface2 (also sets opacity)
6627 // + clip_child
6629 scoped_refptr<Layer> root = Layer::Create();
6630 scoped_refptr<Layer> clip_parent = Layer::Create();
6631 scoped_refptr<Layer> render_surface1 = Layer::Create();
6632 scoped_refptr<Layer> intervening = Layer::Create();
6633 scoped_refptr<Layer> render_surface2 = Layer::Create();
6634 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6635 make_scoped_refptr(new LayerWithForcedDrawsContent);
6637 root->AddChild(clip_parent);
6638 clip_parent->AddChild(render_surface1);
6639 render_surface1->AddChild(intervening);
6640 intervening->AddChild(render_surface2);
6641 render_surface2->AddChild(clip_child);
6643 clip_child->SetClipParent(clip_parent.get());
6645 intervening->SetMasksToBounds(true);
6646 clip_parent->SetMasksToBounds(true);
6648 render_surface1->SetForceRenderSurface(true);
6649 render_surface2->SetForceRenderSurface(true);
6651 gfx::Transform translation_transform;
6652 translation_transform.Translate(2, 2);
6654 gfx::Transform identity_transform;
6655 SetLayerPropertiesForTesting(root.get(),
6656 identity_transform,
6657 gfx::Point3F(),
6658 gfx::PointF(),
6659 gfx::Size(50, 50),
6660 true,
6661 false);
6662 SetLayerPropertiesForTesting(clip_parent.get(),
6663 translation_transform,
6664 gfx::Point3F(),
6665 gfx::PointF(1.f, 1.f),
6666 gfx::Size(40, 40),
6667 true,
6668 false);
6669 SetLayerPropertiesForTesting(render_surface1.get(),
6670 identity_transform,
6671 gfx::Point3F(),
6672 gfx::PointF(),
6673 gfx::Size(10, 10),
6674 true,
6675 false);
6676 SetLayerPropertiesForTesting(intervening.get(),
6677 identity_transform,
6678 gfx::Point3F(),
6679 gfx::PointF(1.f, 1.f),
6680 gfx::Size(5, 5),
6681 true,
6682 false);
6683 SetLayerPropertiesForTesting(render_surface2.get(),
6684 identity_transform,
6685 gfx::Point3F(),
6686 gfx::PointF(),
6687 gfx::Size(10, 10),
6688 true,
6689 false);
6690 SetLayerPropertiesForTesting(clip_child.get(),
6691 identity_transform,
6692 gfx::Point3F(),
6693 gfx::PointF(-10.f, -10.f),
6694 gfx::Size(60, 60),
6695 true,
6696 false);
6698 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6699 host->SetRootLayer(root);
6701 ExecuteCalculateDrawProperties(root.get());
6703 EXPECT_TRUE(root->render_surface());
6704 EXPECT_TRUE(render_surface1->render_surface());
6705 EXPECT_TRUE(render_surface2->render_surface());
6707 // Since the render surfaces could have expanded, they should not clip (their
6708 // bounds would no longer be reliable). We should resort to layer clipping
6709 // in this case.
6710 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6711 render_surface1->render_surface()->clip_rect().ToString());
6712 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
6713 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6714 render_surface2->render_surface()->clip_rect().ToString());
6715 EXPECT_FALSE(render_surface2->render_surface()->is_clipped());
6717 // NB: clip rects are in target space.
6718 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6719 render_surface1->clip_rect().ToString());
6720 EXPECT_TRUE(render_surface1->is_clipped());
6722 // This value is inherited from the clipping ancestor layer, 'intervening'.
6723 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
6724 render_surface2->clip_rect().ToString());
6725 EXPECT_TRUE(render_surface2->is_clipped());
6727 // The content rects of both render surfaces should both have expanded to
6728 // contain the clip child.
6729 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6730 render_surface1->render_surface()->content_rect().ToString());
6731 EXPECT_EQ(gfx::Rect(-1, -1, 40, 40).ToString(),
6732 render_surface2->render_surface()->content_rect().ToString());
6734 // The clip child should have inherited the clip parent's clip (projected to
6735 // the right space, of course), and should have the correctly sized visible
6736 // content rect.
6737 EXPECT_EQ(gfx::Rect(-1, -1, 40, 40).ToString(),
6738 clip_child->clip_rect().ToString());
6739 EXPECT_EQ(gfx::Rect(9, 9, 40, 40).ToString(),
6740 clip_child->visible_content_rect().ToString());
6741 EXPECT_TRUE(clip_child->is_clipped());
6744 TEST_F(LayerTreeHostCommonTest, ClipParentScrolledInterveningLayer) {
6745 // Ensure that intervening render surfaces are not a problem, even if there
6746 // is a scroll involved. Note, we do _not_ have to consider any other sort
6747 // of transform.
6749 // root (a render surface)
6750 // + clip_parent (masks to bounds)
6751 // + render_surface1 (sets opacity)
6752 // + intervening (masks to bounds AND scrolls)
6753 // + render_surface2 (also sets opacity)
6754 // + clip_child
6756 scoped_refptr<Layer> root = Layer::Create();
6757 scoped_refptr<Layer> clip_parent = Layer::Create();
6758 scoped_refptr<Layer> render_surface1 = Layer::Create();
6759 scoped_refptr<Layer> intervening = Layer::Create();
6760 scoped_refptr<Layer> render_surface2 = Layer::Create();
6761 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6762 make_scoped_refptr(new LayerWithForcedDrawsContent);
6764 root->AddChild(clip_parent);
6765 clip_parent->AddChild(render_surface1);
6766 render_surface1->AddChild(intervening);
6767 intervening->AddChild(render_surface2);
6768 render_surface2->AddChild(clip_child);
6770 clip_child->SetClipParent(clip_parent.get());
6772 intervening->SetMasksToBounds(true);
6773 clip_parent->SetMasksToBounds(true);
6774 intervening->SetScrollClipLayerId(clip_parent->id());
6775 intervening->SetScrollOffset(gfx::ScrollOffset(3, 3));
6777 render_surface1->SetForceRenderSurface(true);
6778 render_surface2->SetForceRenderSurface(true);
6780 gfx::Transform translation_transform;
6781 translation_transform.Translate(2, 2);
6783 gfx::Transform identity_transform;
6784 SetLayerPropertiesForTesting(root.get(),
6785 identity_transform,
6786 gfx::Point3F(),
6787 gfx::PointF(),
6788 gfx::Size(50, 50),
6789 true,
6790 false);
6791 SetLayerPropertiesForTesting(clip_parent.get(),
6792 translation_transform,
6793 gfx::Point3F(),
6794 gfx::PointF(1.f, 1.f),
6795 gfx::Size(40, 40),
6796 true,
6797 false);
6798 SetLayerPropertiesForTesting(render_surface1.get(),
6799 identity_transform,
6800 gfx::Point3F(),
6801 gfx::PointF(),
6802 gfx::Size(10, 10),
6803 true,
6804 false);
6805 SetLayerPropertiesForTesting(intervening.get(),
6806 identity_transform,
6807 gfx::Point3F(),
6808 gfx::PointF(1.f, 1.f),
6809 gfx::Size(5, 5),
6810 true,
6811 false);
6812 SetLayerPropertiesForTesting(render_surface2.get(),
6813 identity_transform,
6814 gfx::Point3F(),
6815 gfx::PointF(),
6816 gfx::Size(10, 10),
6817 true,
6818 false);
6819 SetLayerPropertiesForTesting(clip_child.get(),
6820 identity_transform,
6821 gfx::Point3F(),
6822 gfx::PointF(-10.f, -10.f),
6823 gfx::Size(60, 60),
6824 true,
6825 false);
6827 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6828 host->SetRootLayer(root);
6830 ExecuteCalculateDrawProperties(root.get());
6832 EXPECT_TRUE(root->render_surface());
6833 EXPECT_TRUE(render_surface1->render_surface());
6834 EXPECT_TRUE(render_surface2->render_surface());
6836 // Since the render surfaces could have expanded, they should not clip (their
6837 // bounds would no longer be reliable). We should resort to layer clipping
6838 // in this case.
6839 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6840 render_surface1->render_surface()->clip_rect().ToString());
6841 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
6842 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6843 render_surface2->render_surface()->clip_rect().ToString());
6844 EXPECT_FALSE(render_surface2->render_surface()->is_clipped());
6846 // NB: clip rects are in target space.
6847 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6848 render_surface1->clip_rect().ToString());
6849 EXPECT_TRUE(render_surface1->is_clipped());
6851 // This value is inherited from the clipping ancestor layer, 'intervening'.
6852 EXPECT_EQ(gfx::Rect(2, 2, 3, 3).ToString(),
6853 render_surface2->clip_rect().ToString());
6854 EXPECT_TRUE(render_surface2->is_clipped());
6856 // The content rects of both render surfaces should both have expanded to
6857 // contain the clip child.
6858 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6859 render_surface1->render_surface()->content_rect().ToString());
6860 EXPECT_EQ(gfx::Rect(2, 2, 40, 40).ToString(),
6861 render_surface2->render_surface()->content_rect().ToString());
6863 // The clip child should have inherited the clip parent's clip (projected to
6864 // the right space, of course), and should have the correctly sized visible
6865 // content rect.
6866 EXPECT_EQ(gfx::Rect(2, 2, 40, 40).ToString(),
6867 clip_child->clip_rect().ToString());
6868 EXPECT_EQ(gfx::Rect(12, 12, 40, 40).ToString(),
6869 clip_child->visible_content_rect().ToString());
6870 EXPECT_TRUE(clip_child->is_clipped());
6873 TEST_F(LayerTreeHostCommonTest, DescendantsOfClipChildren) {
6874 // Ensures that descendants of the clip child inherit the correct clip.
6876 // root (a render surface)
6877 // + clip_parent (masks to bounds)
6878 // + intervening (masks to bounds)
6879 // + clip_child
6880 // + child
6882 scoped_refptr<Layer> root = Layer::Create();
6883 scoped_refptr<Layer> clip_parent = Layer::Create();
6884 scoped_refptr<Layer> intervening = Layer::Create();
6885 scoped_refptr<Layer> clip_child = Layer::Create();
6886 scoped_refptr<LayerWithForcedDrawsContent> child =
6887 make_scoped_refptr(new LayerWithForcedDrawsContent);
6889 root->AddChild(clip_parent);
6890 clip_parent->AddChild(intervening);
6891 intervening->AddChild(clip_child);
6892 clip_child->AddChild(child);
6894 clip_child->SetClipParent(clip_parent.get());
6896 intervening->SetMasksToBounds(true);
6897 clip_parent->SetMasksToBounds(true);
6899 gfx::Transform identity_transform;
6900 SetLayerPropertiesForTesting(root.get(),
6901 identity_transform,
6902 gfx::Point3F(),
6903 gfx::PointF(),
6904 gfx::Size(50, 50),
6905 true,
6906 false);
6907 SetLayerPropertiesForTesting(clip_parent.get(),
6908 identity_transform,
6909 gfx::Point3F(),
6910 gfx::PointF(),
6911 gfx::Size(40, 40),
6912 true,
6913 false);
6914 SetLayerPropertiesForTesting(intervening.get(),
6915 identity_transform,
6916 gfx::Point3F(),
6917 gfx::PointF(),
6918 gfx::Size(5, 5),
6919 true,
6920 false);
6921 SetLayerPropertiesForTesting(clip_child.get(),
6922 identity_transform,
6923 gfx::Point3F(),
6924 gfx::PointF(),
6925 gfx::Size(60, 60),
6926 true,
6927 false);
6928 SetLayerPropertiesForTesting(child.get(),
6929 identity_transform,
6930 gfx::Point3F(),
6931 gfx::PointF(),
6932 gfx::Size(60, 60),
6933 true,
6934 false);
6936 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6937 host->SetRootLayer(root);
6939 ExecuteCalculateDrawProperties(root.get());
6941 EXPECT_TRUE(root->render_surface());
6943 // Neither the clip child nor its descendant should have inherited the clip
6944 // from |intervening|.
6945 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6946 clip_child->clip_rect().ToString());
6947 EXPECT_TRUE(clip_child->is_clipped());
6948 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6949 child->visible_content_rect().ToString());
6950 EXPECT_TRUE(child->is_clipped());
6953 TEST_F(LayerTreeHostCommonTest,
6954 SurfacesShouldBeUnaffectedByNonDescendantClipChildren) {
6955 // Ensures that non-descendant clip children in the tree do not affect
6956 // render surfaces.
6958 // root (a render surface)
6959 // + clip_parent (masks to bounds)
6960 // + render_surface1
6961 // + clip_child
6962 // + render_surface2
6963 // + non_clip_child
6965 // In this example render_surface2 should be unaffected by clip_child.
6966 scoped_refptr<Layer> root = Layer::Create();
6967 scoped_refptr<Layer> clip_parent = Layer::Create();
6968 scoped_refptr<Layer> render_surface1 = Layer::Create();
6969 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6970 make_scoped_refptr(new LayerWithForcedDrawsContent);
6971 scoped_refptr<Layer> render_surface2 = Layer::Create();
6972 scoped_refptr<LayerWithForcedDrawsContent> non_clip_child =
6973 make_scoped_refptr(new LayerWithForcedDrawsContent);
6975 root->AddChild(clip_parent);
6976 clip_parent->AddChild(render_surface1);
6977 render_surface1->AddChild(clip_child);
6978 clip_parent->AddChild(render_surface2);
6979 render_surface2->AddChild(non_clip_child);
6981 clip_child->SetClipParent(clip_parent.get());
6983 clip_parent->SetMasksToBounds(true);
6984 render_surface1->SetMasksToBounds(true);
6986 gfx::Transform identity_transform;
6987 SetLayerPropertiesForTesting(root.get(),
6988 identity_transform,
6989 gfx::Point3F(),
6990 gfx::PointF(),
6991 gfx::Size(15, 15),
6992 true,
6993 false);
6994 SetLayerPropertiesForTesting(clip_parent.get(),
6995 identity_transform,
6996 gfx::Point3F(),
6997 gfx::PointF(),
6998 gfx::Size(10, 10),
6999 true,
7000 false);
7001 SetLayerPropertiesForTesting(render_surface1.get(),
7002 identity_transform,
7003 gfx::Point3F(),
7004 gfx::PointF(5, 5),
7005 gfx::Size(5, 5),
7006 true,
7007 false);
7008 SetLayerPropertiesForTesting(render_surface2.get(),
7009 identity_transform,
7010 gfx::Point3F(),
7011 gfx::PointF(),
7012 gfx::Size(5, 5),
7013 true,
7014 false);
7015 SetLayerPropertiesForTesting(clip_child.get(),
7016 identity_transform,
7017 gfx::Point3F(),
7018 gfx::PointF(-1, 1),
7019 gfx::Size(10, 10),
7020 true,
7021 false);
7022 SetLayerPropertiesForTesting(non_clip_child.get(),
7023 identity_transform,
7024 gfx::Point3F(),
7025 gfx::PointF(),
7026 gfx::Size(5, 5),
7027 true,
7028 false);
7030 render_surface1->SetForceRenderSurface(true);
7031 render_surface2->SetForceRenderSurface(true);
7033 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7034 host->SetRootLayer(root);
7036 ExecuteCalculateDrawProperties(root.get());
7038 EXPECT_TRUE(root->render_surface());
7039 EXPECT_TRUE(render_surface1->render_surface());
7040 EXPECT_TRUE(render_surface2->render_surface());
7042 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7043 render_surface1->clip_rect().ToString());
7044 EXPECT_TRUE(render_surface1->is_clipped());
7046 // The render surface should not clip (it has unclipped descendants), instead
7047 // it should rely on layer clipping.
7048 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
7049 render_surface1->render_surface()->clip_rect().ToString());
7050 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
7052 // That said, it should have grown to accomodate the unclipped descendant.
7053 EXPECT_EQ(gfx::Rect(-1, 1, 6, 4).ToString(),
7054 render_surface1->render_surface()->content_rect().ToString());
7056 // This render surface should clip. It has no unclipped descendants.
7057 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7058 render_surface2->clip_rect().ToString());
7059 EXPECT_TRUE(render_surface2->render_surface()->is_clipped());
7061 // It also shouldn't have grown to accomodate the clip child.
7062 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7063 render_surface2->render_surface()->content_rect().ToString());
7065 // Sanity check our num_unclipped_descendants values.
7066 EXPECT_EQ(1, render_surface1->num_unclipped_descendants());
7067 EXPECT_EQ(0, render_surface2->num_unclipped_descendants());
7070 TEST_F(LayerTreeHostCommonTest, CanRenderToSeparateSurface) {
7071 FakeImplProxy proxy;
7072 TestSharedBitmapManager shared_bitmap_manager;
7073 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7074 scoped_ptr<LayerImpl> root =
7075 LayerImpl::Create(host_impl.active_tree(), 12345);
7076 scoped_ptr<LayerImpl> child1 =
7077 LayerImpl::Create(host_impl.active_tree(), 123456);
7078 scoped_ptr<LayerImpl> child2 =
7079 LayerImpl::Create(host_impl.active_tree(), 1234567);
7080 scoped_ptr<LayerImpl> child3 =
7081 LayerImpl::Create(host_impl.active_tree(), 12345678);
7083 gfx::Transform identity_matrix;
7084 gfx::Point3F transform_origin;
7085 gfx::PointF position;
7086 gfx::Size bounds(100, 100);
7087 SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
7088 position, bounds, true, false, true);
7089 root->SetDrawsContent(true);
7091 // This layer structure normally forces render surface due to preserves3d
7092 // behavior.
7093 SetLayerPropertiesForTesting(child1.get(), identity_matrix, transform_origin,
7094 position, bounds, false, true, true);
7095 child1->SetDrawsContent(true);
7096 SetLayerPropertiesForTesting(child2.get(), identity_matrix, transform_origin,
7097 position, bounds, true, false, false);
7098 child2->SetDrawsContent(true);
7099 SetLayerPropertiesForTesting(child3.get(), identity_matrix, transform_origin,
7100 position, bounds, true, false, false);
7101 child3->SetDrawsContent(true);
7103 child2->Set3dSortingContextId(1);
7104 child3->Set3dSortingContextId(1);
7106 child2->AddChild(child3.Pass());
7107 child1->AddChild(child2.Pass());
7108 root->AddChild(child1.Pass());
7111 LayerImplList render_surface_layer_list;
7112 FakeLayerTreeHostImpl::RecursiveUpdateNumChildren(root.get());
7113 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7114 root.get(), root->bounds(), &render_surface_layer_list);
7115 inputs.can_render_to_separate_surface = true;
7116 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7118 EXPECT_EQ(2u, render_surface_layer_list.size());
7120 int count_represents_target_render_surface = 0;
7121 int count_represents_contributing_render_surface = 0;
7122 int count_represents_itself = 0;
7123 auto end = LayerIterator<LayerImpl>::End(&render_surface_layer_list);
7124 for (auto it = LayerIterator<LayerImpl>::Begin(&render_surface_layer_list);
7125 it != end; ++it) {
7126 if (it.represents_target_render_surface())
7127 count_represents_target_render_surface++;
7128 if (it.represents_contributing_render_surface())
7129 count_represents_contributing_render_surface++;
7130 if (it.represents_itself())
7131 count_represents_itself++;
7134 // Two render surfaces.
7135 EXPECT_EQ(2, count_represents_target_render_surface);
7136 // Second render surface contributes to root render surface.
7137 EXPECT_EQ(1, count_represents_contributing_render_surface);
7138 // All 4 layers represent itself.
7139 EXPECT_EQ(4, count_represents_itself);
7143 LayerImplList render_surface_layer_list;
7144 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7145 root.get(), root->bounds(), &render_surface_layer_list);
7146 inputs.can_render_to_separate_surface = false;
7147 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7149 EXPECT_EQ(1u, render_surface_layer_list.size());
7151 int count_represents_target_render_surface = 0;
7152 int count_represents_contributing_render_surface = 0;
7153 int count_represents_itself = 0;
7154 auto end = LayerIterator<LayerImpl>::End(&render_surface_layer_list);
7155 for (auto it = LayerIterator<LayerImpl>::Begin(&render_surface_layer_list);
7156 it != end; ++it) {
7157 if (it.represents_target_render_surface())
7158 count_represents_target_render_surface++;
7159 if (it.represents_contributing_render_surface())
7160 count_represents_contributing_render_surface++;
7161 if (it.represents_itself())
7162 count_represents_itself++;
7165 // Only root layer has a render surface.
7166 EXPECT_EQ(1, count_represents_target_render_surface);
7167 // No layer contributes a render surface to root render surface.
7168 EXPECT_EQ(0, count_represents_contributing_render_surface);
7169 // All 4 layers represent itself.
7170 EXPECT_EQ(4, count_represents_itself);
7174 TEST_F(LayerTreeHostCommonTest, DoNotIncludeBackfaceInvisibleSurfaces) {
7175 scoped_refptr<Layer> root = Layer::Create();
7176 scoped_refptr<Layer> render_surface = Layer::Create();
7177 scoped_refptr<LayerWithForcedDrawsContent> child =
7178 make_scoped_refptr(new LayerWithForcedDrawsContent);
7180 root->AddChild(render_surface);
7181 render_surface->AddChild(child);
7183 gfx::Transform identity_transform;
7184 SetLayerPropertiesForTesting(root.get(),
7185 identity_transform,
7186 gfx::Point3F(),
7187 gfx::PointF(),
7188 gfx::Size(50, 50),
7189 true,
7190 false);
7191 SetLayerPropertiesForTesting(render_surface.get(),
7192 identity_transform,
7193 gfx::Point3F(),
7194 gfx::PointF(),
7195 gfx::Size(30, 30),
7196 false,
7197 true);
7198 SetLayerPropertiesForTesting(child.get(),
7199 identity_transform,
7200 gfx::Point3F(),
7201 gfx::PointF(),
7202 gfx::Size(20, 20),
7203 true,
7204 false);
7206 root->SetShouldFlattenTransform(false);
7207 root->Set3dSortingContextId(1);
7208 render_surface->SetDoubleSided(false);
7209 render_surface->SetForceRenderSurface(true);
7211 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7212 host->SetRootLayer(root);
7214 ExecuteCalculateDrawProperties(root.get());
7216 EXPECT_EQ(2u, render_surface_layer_list()->size());
7217 EXPECT_EQ(1u,
7218 render_surface_layer_list()->at(0)
7219 ->render_surface()->layer_list().size());
7220 EXPECT_EQ(1u,
7221 render_surface_layer_list()->at(1)
7222 ->render_surface()->layer_list().size());
7224 gfx::Transform rotation_transform = identity_transform;
7225 rotation_transform.RotateAboutXAxis(180.0);
7227 render_surface->SetTransform(rotation_transform);
7229 ExecuteCalculateDrawProperties(root.get());
7231 EXPECT_EQ(1u, render_surface_layer_list()->size());
7232 EXPECT_EQ(0u,
7233 render_surface_layer_list()->at(0)
7234 ->render_surface()->layer_list().size());
7237 TEST_F(LayerTreeHostCommonTest, ClippedByScrollParent) {
7238 // Checks that the simple case (being clipped by a scroll parent that would
7239 // have been processed before you anyhow) results in the right clips.
7241 // + root
7242 // + scroll_parent_border
7243 // | + scroll_parent_clip
7244 // | + scroll_parent
7245 // + scroll_child
7247 scoped_refptr<Layer> root = Layer::Create();
7248 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7249 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7250 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7251 make_scoped_refptr(new LayerWithForcedDrawsContent);
7252 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7253 make_scoped_refptr(new LayerWithForcedDrawsContent);
7255 root->AddChild(scroll_child);
7257 root->AddChild(scroll_parent_border);
7258 scroll_parent_border->AddChild(scroll_parent_clip);
7259 scroll_parent_clip->AddChild(scroll_parent);
7261 scroll_parent_clip->SetMasksToBounds(true);
7263 scroll_child->SetScrollParent(scroll_parent.get());
7265 gfx::Transform identity_transform;
7266 SetLayerPropertiesForTesting(root.get(),
7267 identity_transform,
7268 gfx::Point3F(),
7269 gfx::PointF(),
7270 gfx::Size(50, 50),
7271 true,
7272 false);
7273 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7274 identity_transform,
7275 gfx::Point3F(),
7276 gfx::PointF(),
7277 gfx::Size(40, 40),
7278 true,
7279 false);
7280 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7281 identity_transform,
7282 gfx::Point3F(),
7283 gfx::PointF(),
7284 gfx::Size(30, 30),
7285 true,
7286 false);
7287 SetLayerPropertiesForTesting(scroll_parent.get(),
7288 identity_transform,
7289 gfx::Point3F(),
7290 gfx::PointF(),
7291 gfx::Size(50, 50),
7292 true,
7293 false);
7294 SetLayerPropertiesForTesting(scroll_child.get(),
7295 identity_transform,
7296 gfx::Point3F(),
7297 gfx::PointF(),
7298 gfx::Size(50, 50),
7299 true,
7300 false);
7302 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7303 host->SetRootLayer(root);
7305 ExecuteCalculateDrawProperties(root.get());
7307 EXPECT_TRUE(root->render_surface());
7309 EXPECT_EQ(gfx::Rect(0, 0, 30, 30).ToString(),
7310 scroll_child->clip_rect().ToString());
7311 EXPECT_TRUE(scroll_child->is_clipped());
7314 TEST_F(LayerTreeHostCommonTest, SingularTransformSubtreesDoNotDraw) {
7315 scoped_refptr<LayerWithForcedDrawsContent> root =
7316 make_scoped_refptr(new LayerWithForcedDrawsContent);
7317 scoped_refptr<LayerWithForcedDrawsContent> parent =
7318 make_scoped_refptr(new LayerWithForcedDrawsContent);
7319 scoped_refptr<LayerWithForcedDrawsContent> child =
7320 make_scoped_refptr(new LayerWithForcedDrawsContent);
7322 root->AddChild(parent);
7323 parent->AddChild(child);
7325 gfx::Transform identity_transform;
7326 SetLayerPropertiesForTesting(root.get(),
7327 identity_transform,
7328 gfx::Point3F(),
7329 gfx::PointF(),
7330 gfx::Size(50, 50),
7331 true,
7332 true);
7333 root->SetForceRenderSurface(true);
7334 SetLayerPropertiesForTesting(parent.get(),
7335 identity_transform,
7336 gfx::Point3F(),
7337 gfx::PointF(),
7338 gfx::Size(30, 30),
7339 true,
7340 true);
7341 parent->SetForceRenderSurface(true);
7342 SetLayerPropertiesForTesting(child.get(),
7343 identity_transform,
7344 gfx::Point3F(),
7345 gfx::PointF(),
7346 gfx::Size(20, 20),
7347 true,
7348 true);
7349 child->SetForceRenderSurface(true);
7351 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7352 host->SetRootLayer(root);
7354 ExecuteCalculateDrawProperties(root.get());
7356 EXPECT_EQ(3u, render_surface_layer_list()->size());
7358 gfx::Transform singular_transform;
7359 singular_transform.Scale3d(
7360 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
7362 child->SetTransform(singular_transform);
7364 ExecuteCalculateDrawProperties(root.get());
7366 EXPECT_EQ(2u, render_surface_layer_list()->size());
7368 // Ensure that the entire subtree under a layer with singular transform does
7369 // not get rendered.
7370 parent->SetTransform(singular_transform);
7371 child->SetTransform(identity_transform);
7373 ExecuteCalculateDrawProperties(root.get());
7375 EXPECT_EQ(1u, render_surface_layer_list()->size());
7378 TEST_F(LayerTreeHostCommonTest, ClippedByOutOfOrderScrollParent) {
7379 // Checks that clipping by a scroll parent that follows you in paint order
7380 // still results in correct clipping.
7382 // + root
7383 // + scroll_child
7384 // + scroll_parent_border
7385 // + scroll_parent_clip
7386 // + scroll_parent
7388 scoped_refptr<Layer> root = Layer::Create();
7389 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7390 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7391 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7392 make_scoped_refptr(new LayerWithForcedDrawsContent);
7393 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7394 make_scoped_refptr(new LayerWithForcedDrawsContent);
7396 root->AddChild(scroll_parent_border);
7397 scroll_parent_border->AddChild(scroll_parent_clip);
7398 scroll_parent_clip->AddChild(scroll_parent);
7400 root->AddChild(scroll_child);
7402 scroll_parent_clip->SetMasksToBounds(true);
7404 scroll_child->SetScrollParent(scroll_parent.get());
7406 gfx::Transform identity_transform;
7407 SetLayerPropertiesForTesting(root.get(),
7408 identity_transform,
7409 gfx::Point3F(),
7410 gfx::PointF(),
7411 gfx::Size(50, 50),
7412 true,
7413 false);
7414 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7415 identity_transform,
7416 gfx::Point3F(),
7417 gfx::PointF(),
7418 gfx::Size(40, 40),
7419 true,
7420 false);
7421 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7422 identity_transform,
7423 gfx::Point3F(),
7424 gfx::PointF(),
7425 gfx::Size(30, 30),
7426 true,
7427 false);
7428 SetLayerPropertiesForTesting(scroll_parent.get(),
7429 identity_transform,
7430 gfx::Point3F(),
7431 gfx::PointF(),
7432 gfx::Size(50, 50),
7433 true,
7434 false);
7435 SetLayerPropertiesForTesting(scroll_child.get(),
7436 identity_transform,
7437 gfx::Point3F(),
7438 gfx::PointF(),
7439 gfx::Size(50, 50),
7440 true,
7441 false);
7443 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7444 host->SetRootLayer(root);
7446 ExecuteCalculateDrawProperties(root.get());
7448 EXPECT_TRUE(root->render_surface());
7450 EXPECT_EQ(gfx::Rect(0, 0, 30, 30).ToString(),
7451 scroll_child->clip_rect().ToString());
7452 EXPECT_TRUE(scroll_child->is_clipped());
7455 TEST_F(LayerTreeHostCommonTest, ClippedByOutOfOrderScrollGrandparent) {
7456 // Checks that clipping by a scroll parent and scroll grandparent that follow
7457 // you in paint order still results in correct clipping.
7459 // + root
7460 // + scroll_child
7461 // + scroll_parent_border
7462 // | + scroll_parent_clip
7463 // | + scroll_parent
7464 // + scroll_grandparent_border
7465 // + scroll_grandparent_clip
7466 // + scroll_grandparent
7468 scoped_refptr<Layer> root = Layer::Create();
7469 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7470 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7471 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7472 make_scoped_refptr(new LayerWithForcedDrawsContent);
7474 scoped_refptr<Layer> scroll_grandparent_border = Layer::Create();
7475 scoped_refptr<Layer> scroll_grandparent_clip = Layer::Create();
7476 scoped_refptr<LayerWithForcedDrawsContent> scroll_grandparent =
7477 make_scoped_refptr(new LayerWithForcedDrawsContent);
7479 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7480 make_scoped_refptr(new LayerWithForcedDrawsContent);
7482 root->AddChild(scroll_child);
7484 root->AddChild(scroll_parent_border);
7485 scroll_parent_border->AddChild(scroll_parent_clip);
7486 scroll_parent_clip->AddChild(scroll_parent);
7488 root->AddChild(scroll_grandparent_border);
7489 scroll_grandparent_border->AddChild(scroll_grandparent_clip);
7490 scroll_grandparent_clip->AddChild(scroll_grandparent);
7492 scroll_parent_clip->SetMasksToBounds(true);
7493 scroll_grandparent_clip->SetMasksToBounds(true);
7495 scroll_child->SetScrollParent(scroll_parent.get());
7496 scroll_parent_border->SetScrollParent(scroll_grandparent.get());
7498 gfx::Transform identity_transform;
7499 SetLayerPropertiesForTesting(root.get(),
7500 identity_transform,
7501 gfx::Point3F(),
7502 gfx::PointF(),
7503 gfx::Size(50, 50),
7504 true,
7505 false);
7506 SetLayerPropertiesForTesting(scroll_grandparent_border.get(),
7507 identity_transform,
7508 gfx::Point3F(),
7509 gfx::PointF(),
7510 gfx::Size(40, 40),
7511 true,
7512 false);
7513 SetLayerPropertiesForTesting(scroll_grandparent_clip.get(),
7514 identity_transform,
7515 gfx::Point3F(),
7516 gfx::PointF(),
7517 gfx::Size(20, 20),
7518 true,
7519 false);
7520 SetLayerPropertiesForTesting(scroll_grandparent.get(),
7521 identity_transform,
7522 gfx::Point3F(),
7523 gfx::PointF(),
7524 gfx::Size(50, 50),
7525 true,
7526 false);
7527 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7528 identity_transform,
7529 gfx::Point3F(),
7530 gfx::PointF(),
7531 gfx::Size(40, 40),
7532 true,
7533 false);
7534 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7535 identity_transform,
7536 gfx::Point3F(),
7537 gfx::PointF(),
7538 gfx::Size(30, 30),
7539 true,
7540 false);
7541 SetLayerPropertiesForTesting(scroll_parent.get(),
7542 identity_transform,
7543 gfx::Point3F(),
7544 gfx::PointF(),
7545 gfx::Size(50, 50),
7546 true,
7547 false);
7548 SetLayerPropertiesForTesting(scroll_child.get(),
7549 identity_transform,
7550 gfx::Point3F(),
7551 gfx::PointF(),
7552 gfx::Size(50, 50),
7553 true,
7554 false);
7556 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7557 host->SetRootLayer(root);
7559 ExecuteCalculateDrawProperties(root.get());
7561 EXPECT_TRUE(root->render_surface());
7563 EXPECT_EQ(gfx::Rect(0, 0, 20, 20).ToString(),
7564 scroll_child->clip_rect().ToString());
7565 EXPECT_TRUE(scroll_child->is_clipped());
7567 // Despite the fact that we visited the above layers out of order to get the
7568 // correct clip, the layer lists should be unaffected.
7569 EXPECT_EQ(3u, root->render_surface()->layer_list().size());
7570 EXPECT_EQ(scroll_child.get(),
7571 root->render_surface()->layer_list().at(0).get());
7572 EXPECT_EQ(scroll_parent.get(),
7573 root->render_surface()->layer_list().at(1).get());
7574 EXPECT_EQ(scroll_grandparent.get(),
7575 root->render_surface()->layer_list().at(2).get());
7578 TEST_F(LayerTreeHostCommonTest, OutOfOrderClippingRequiresRSLLSorting) {
7579 // Ensures that even if we visit layers out of order, we still produce a
7580 // correctly ordered render surface layer list.
7581 // + root
7582 // + scroll_child
7583 // + scroll_parent_border
7584 // + scroll_parent_clip
7585 // + scroll_parent
7586 // + render_surface1
7587 // + scroll_grandparent_border
7588 // + scroll_grandparent_clip
7589 // + scroll_grandparent
7590 // + render_surface2
7592 scoped_refptr<LayerWithForcedDrawsContent> root =
7593 make_scoped_refptr(new LayerWithForcedDrawsContent);
7595 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7596 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7597 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7598 make_scoped_refptr(new LayerWithForcedDrawsContent);
7599 scoped_refptr<LayerWithForcedDrawsContent> render_surface1 =
7600 make_scoped_refptr(new LayerWithForcedDrawsContent);
7602 scoped_refptr<Layer> scroll_grandparent_border = Layer::Create();
7603 scoped_refptr<Layer> scroll_grandparent_clip = Layer::Create();
7604 scoped_refptr<LayerWithForcedDrawsContent> scroll_grandparent =
7605 make_scoped_refptr(new LayerWithForcedDrawsContent);
7606 scoped_refptr<LayerWithForcedDrawsContent> render_surface2 =
7607 make_scoped_refptr(new LayerWithForcedDrawsContent);
7609 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7610 make_scoped_refptr(new LayerWithForcedDrawsContent);
7612 root->AddChild(scroll_child);
7614 root->AddChild(scroll_parent_border);
7615 scroll_parent_border->AddChild(scroll_parent_clip);
7616 scroll_parent_clip->AddChild(scroll_parent);
7617 scroll_parent->AddChild(render_surface2);
7619 root->AddChild(scroll_grandparent_border);
7620 scroll_grandparent_border->AddChild(scroll_grandparent_clip);
7621 scroll_grandparent_clip->AddChild(scroll_grandparent);
7622 scroll_grandparent->AddChild(render_surface1);
7624 scroll_parent_clip->SetMasksToBounds(true);
7625 scroll_grandparent_clip->SetMasksToBounds(true);
7627 scroll_child->SetScrollParent(scroll_parent.get());
7628 scroll_parent_border->SetScrollParent(scroll_grandparent.get());
7630 render_surface1->SetForceRenderSurface(true);
7631 render_surface2->SetForceRenderSurface(true);
7633 gfx::Transform identity_transform;
7634 SetLayerPropertiesForTesting(root.get(),
7635 identity_transform,
7636 gfx::Point3F(),
7637 gfx::PointF(),
7638 gfx::Size(50, 50),
7639 true,
7640 false);
7641 SetLayerPropertiesForTesting(scroll_grandparent_border.get(),
7642 identity_transform,
7643 gfx::Point3F(),
7644 gfx::PointF(),
7645 gfx::Size(40, 40),
7646 true,
7647 false);
7648 SetLayerPropertiesForTesting(scroll_grandparent_clip.get(),
7649 identity_transform,
7650 gfx::Point3F(),
7651 gfx::PointF(),
7652 gfx::Size(20, 20),
7653 true,
7654 false);
7655 SetLayerPropertiesForTesting(scroll_grandparent.get(),
7656 identity_transform,
7657 gfx::Point3F(),
7658 gfx::PointF(),
7659 gfx::Size(50, 50),
7660 true,
7661 false);
7662 SetLayerPropertiesForTesting(render_surface1.get(),
7663 identity_transform,
7664 gfx::Point3F(),
7665 gfx::PointF(),
7666 gfx::Size(50, 50),
7667 true,
7668 false);
7669 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7670 identity_transform,
7671 gfx::Point3F(),
7672 gfx::PointF(),
7673 gfx::Size(40, 40),
7674 true,
7675 false);
7676 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7677 identity_transform,
7678 gfx::Point3F(),
7679 gfx::PointF(),
7680 gfx::Size(30, 30),
7681 true,
7682 false);
7683 SetLayerPropertiesForTesting(scroll_parent.get(),
7684 identity_transform,
7685 gfx::Point3F(),
7686 gfx::PointF(),
7687 gfx::Size(50, 50),
7688 true,
7689 false);
7690 SetLayerPropertiesForTesting(render_surface2.get(),
7691 identity_transform,
7692 gfx::Point3F(),
7693 gfx::PointF(),
7694 gfx::Size(50, 50),
7695 true,
7696 false);
7697 SetLayerPropertiesForTesting(scroll_child.get(),
7698 identity_transform,
7699 gfx::Point3F(),
7700 gfx::PointF(),
7701 gfx::Size(50, 50),
7702 true,
7703 false);
7705 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7706 host->SetRootLayer(root);
7708 RenderSurfaceLayerList render_surface_layer_list;
7709 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
7710 root.get(),
7711 root->bounds(),
7712 identity_transform,
7713 &render_surface_layer_list);
7715 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7717 EXPECT_TRUE(root->render_surface());
7719 EXPECT_EQ(gfx::Rect(0, 0, 20, 20).ToString(),
7720 scroll_child->clip_rect().ToString());
7721 EXPECT_TRUE(scroll_child->is_clipped());
7723 // Despite the fact that we had to process the layers out of order to get the
7724 // right clip, our render_surface_layer_list's order should be unaffected.
7725 EXPECT_EQ(3u, render_surface_layer_list.size());
7726 EXPECT_EQ(root.get(), render_surface_layer_list.at(0));
7727 EXPECT_EQ(render_surface2.get(), render_surface_layer_list.at(1));
7728 EXPECT_EQ(render_surface1.get(), render_surface_layer_list.at(2));
7729 EXPECT_TRUE(render_surface_layer_list.at(0)->render_surface());
7730 EXPECT_TRUE(render_surface_layer_list.at(1)->render_surface());
7731 EXPECT_TRUE(render_surface_layer_list.at(2)->render_surface());
7734 TEST_F(LayerTreeHostCommonTest, FixedPositionWithInterveningRenderSurface) {
7735 // Ensures that when we have a render surface between a fixed position layer
7736 // and its container, we compute the fixed position layer's draw transform
7737 // with respect to that intervening render surface, not with respect to its
7738 // container's render target.
7740 // + root
7741 // + render_surface
7742 // + fixed
7743 // + child
7745 scoped_refptr<Layer> root = Layer::Create();
7746 scoped_refptr<LayerWithForcedDrawsContent> render_surface =
7747 make_scoped_refptr(new LayerWithForcedDrawsContent());
7748 scoped_refptr<LayerWithForcedDrawsContent> fixed =
7749 make_scoped_refptr(new LayerWithForcedDrawsContent());
7750 scoped_refptr<LayerWithForcedDrawsContent> child =
7751 make_scoped_refptr(new LayerWithForcedDrawsContent());
7753 root->AddChild(render_surface);
7754 render_surface->AddChild(fixed);
7755 fixed->AddChild(child);
7757 root->SetIsContainerForFixedPositionLayers(true);
7758 render_surface->SetForceRenderSurface(true);
7760 LayerPositionConstraint constraint;
7761 constraint.set_is_fixed_position(true);
7762 fixed->SetPositionConstraint(constraint);
7764 SetLayerPropertiesForTesting(root.get(), gfx::Transform(), gfx::Point3F(),
7765 gfx::PointF(), gfx::Size(50, 50), true, false);
7766 SetLayerPropertiesForTesting(render_surface.get(), gfx::Transform(),
7767 gfx::Point3F(), gfx::PointF(7.f, 9.f),
7768 gfx::Size(50, 50), true, false);
7769 SetLayerPropertiesForTesting(fixed.get(), gfx::Transform(), gfx::Point3F(),
7770 gfx::PointF(10.f, 15.f), gfx::Size(50, 50), true,
7771 false);
7772 SetLayerPropertiesForTesting(child.get(), gfx::Transform(), gfx::Point3F(),
7773 gfx::PointF(1.f, 2.f), gfx::Size(50, 50), true,
7774 false);
7776 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7777 host->SetRootLayer(root);
7779 ExecuteCalculateDrawProperties(root.get());
7781 gfx::Transform expected_fixed_draw_transform;
7782 expected_fixed_draw_transform.Translate(10.f, 15.f);
7783 EXPECT_EQ(expected_fixed_draw_transform, fixed->draw_transform());
7785 gfx::Transform expected_fixed_screen_space_transform;
7786 expected_fixed_screen_space_transform.Translate(17.f, 24.f);
7787 EXPECT_EQ(expected_fixed_screen_space_transform,
7788 fixed->screen_space_transform());
7790 gfx::Transform expected_child_draw_transform;
7791 expected_child_draw_transform.Translate(11.f, 17.f);
7792 EXPECT_EQ(expected_child_draw_transform, child->draw_transform());
7794 gfx::Transform expected_child_screen_space_transform;
7795 expected_child_screen_space_transform.Translate(18.f, 26.f);
7796 EXPECT_EQ(expected_child_screen_space_transform,
7797 child->screen_space_transform());
7800 TEST_F(LayerTreeHostCommonTest, ScrollCompensationWithRounding) {
7801 // This test verifies that a scrolling layer that gets snapped to
7802 // integer coordinates doesn't move a fixed position child.
7804 // + root
7805 // + container
7806 // + scroller
7807 // + fixed
7809 FakeImplProxy proxy;
7810 TestSharedBitmapManager shared_bitmap_manager;
7811 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7812 host_impl.CreatePendingTree();
7813 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
7814 scoped_ptr<LayerImpl> container =
7815 LayerImpl::Create(host_impl.active_tree(), 2);
7816 LayerImpl* container_layer = container.get();
7817 scoped_ptr<LayerImpl> scroller =
7818 LayerImpl::Create(host_impl.active_tree(), 3);
7819 LayerImpl* scroll_layer = scroller.get();
7820 scoped_ptr<LayerImpl> fixed = LayerImpl::Create(host_impl.active_tree(), 4);
7821 LayerImpl* fixed_layer = fixed.get();
7823 container->SetIsContainerForFixedPositionLayers(true);
7825 LayerPositionConstraint constraint;
7826 constraint.set_is_fixed_position(true);
7827 fixed->SetPositionConstraint(constraint);
7829 scroller->SetScrollClipLayer(container->id());
7831 gfx::Transform identity_transform;
7832 gfx::Transform container_transform;
7833 container_transform.Translate3d(10.0, 20.0, 0.0);
7834 gfx::Vector2dF container_offset = container_transform.To2dTranslation();
7836 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7837 gfx::PointF(), gfx::Size(50, 50), true, false,
7838 true);
7839 SetLayerPropertiesForTesting(container.get(), container_transform,
7840 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
7841 true, false, false);
7842 SetLayerPropertiesForTesting(scroller.get(), identity_transform,
7843 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
7844 true, false, false);
7845 SetLayerPropertiesForTesting(fixed.get(), identity_transform, gfx::Point3F(),
7846 gfx::PointF(), gfx::Size(50, 50), true, false,
7847 false);
7849 scroller->AddChild(fixed.Pass());
7850 container->AddChild(scroller.Pass());
7851 root->AddChild(container.Pass());
7853 // Rounded to integers already.
7855 gfx::Vector2dF scroll_delta(3.0, 5.0);
7856 scroll_layer->SetScrollDelta(scroll_delta);
7858 LayerImplList render_surface_layer_list;
7859 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7860 root.get(), root->bounds(), &render_surface_layer_list);
7861 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7863 EXPECT_TRANSFORMATION_MATRIX_EQ(
7864 container_layer->draw_properties().screen_space_transform,
7865 fixed_layer->draw_properties().screen_space_transform);
7866 EXPECT_VECTOR_EQ(
7867 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7868 container_offset);
7869 EXPECT_VECTOR_EQ(scroll_layer->draw_properties()
7870 .screen_space_transform.To2dTranslation(),
7871 container_offset - scroll_delta);
7874 // Scroll delta requiring rounding.
7876 gfx::Vector2dF scroll_delta(4.1f, 8.1f);
7877 scroll_layer->SetScrollDelta(scroll_delta);
7879 gfx::Vector2dF rounded_scroll_delta(4.f, 8.f);
7881 LayerImplList render_surface_layer_list;
7882 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7883 root.get(), root->bounds(), &render_surface_layer_list);
7884 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7886 EXPECT_TRANSFORMATION_MATRIX_EQ(
7887 container_layer->draw_properties().screen_space_transform,
7888 fixed_layer->draw_properties().screen_space_transform);
7889 EXPECT_VECTOR_EQ(
7890 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7891 container_offset);
7892 EXPECT_VECTOR_EQ(scroll_layer->draw_properties()
7893 .screen_space_transform.To2dTranslation(),
7894 container_offset - rounded_scroll_delta);
7897 // Scale is applied earlier in the tree.
7899 gfx::Transform scaled_container_transform = container_transform;
7900 scaled_container_transform.Scale3d(3.0, 3.0, 1.0);
7901 container_layer->SetTransform(scaled_container_transform);
7903 gfx::Vector2dF scroll_delta(4.5f, 8.5f);
7904 scroll_layer->SetScrollDelta(scroll_delta);
7906 LayerImplList render_surface_layer_list;
7907 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7908 root.get(), root->bounds(), &render_surface_layer_list);
7909 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7911 EXPECT_TRANSFORMATION_MATRIX_EQ(
7912 container_layer->draw_properties().screen_space_transform,
7913 fixed_layer->draw_properties().screen_space_transform);
7914 EXPECT_VECTOR_EQ(
7915 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7916 container_offset);
7918 container_layer->SetTransform(container_transform);
7921 // Scale is applied on the scroll layer itself.
7923 gfx::Transform scale_transform;
7924 scale_transform.Scale3d(3.0, 3.0, 1.0);
7925 scroll_layer->SetTransform(scale_transform);
7927 gfx::Vector2dF scroll_delta(4.5f, 8.5f);
7928 scroll_layer->SetScrollDelta(scroll_delta);
7930 LayerImplList render_surface_layer_list;
7931 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7932 root.get(), root->bounds(), &render_surface_layer_list);
7933 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7935 EXPECT_VECTOR_EQ(
7936 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7937 container_offset);
7939 scroll_layer->SetTransform(identity_transform);
7943 TEST_F(LayerTreeHostCommonTest,
7944 ScrollCompensationMainScrollOffsetFractionalPart) {
7945 // This test verifies that a scrolling layer that has fractional scroll offset
7946 // from main doesn't move a fixed position child.
7948 // + root
7949 // + container
7950 // + scroller
7951 // + fixed
7953 FakeImplProxy proxy;
7954 TestSharedBitmapManager shared_bitmap_manager;
7955 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7956 host_impl.CreatePendingTree();
7957 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
7958 scoped_ptr<LayerImpl> container =
7959 LayerImpl::Create(host_impl.active_tree(), 2);
7960 LayerImpl* container_layer = container.get();
7961 scoped_ptr<LayerImpl> scroller =
7962 LayerImpl::Create(host_impl.active_tree(), 3);
7963 LayerImpl* scroll_layer = scroller.get();
7964 scoped_ptr<LayerImpl> fixed = LayerImpl::Create(host_impl.active_tree(), 4);
7965 LayerImpl* fixed_layer = fixed.get();
7967 container->SetIsContainerForFixedPositionLayers(true);
7969 LayerPositionConstraint constraint;
7970 constraint.set_is_fixed_position(true);
7971 fixed->SetPositionConstraint(constraint);
7973 scroller->SetScrollClipLayer(container->id());
7975 gfx::Transform identity_transform;
7976 gfx::Transform container_transform;
7977 container_transform.Translate3d(10.0, 20.0, 0.0);
7978 gfx::Vector2dF container_offset = container_transform.To2dTranslation();
7980 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7981 gfx::PointF(), gfx::Size(50, 50), true, false,
7982 true);
7983 SetLayerPropertiesForTesting(container.get(), container_transform,
7984 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
7985 true, false, false);
7986 SetLayerPropertiesForTesting(scroller.get(), identity_transform,
7987 gfx::Point3F(), gfx::PointF(0.0, 0.0),
7988 gfx::Size(30, 30), true, false, false);
7990 gfx::ScrollOffset scroll_offset(3.3, 4.2);
7991 gfx::Vector2dF main_scroll_fractional_part(0.3f, 0.2f);
7992 gfx::Vector2dF scroll_delta(0.1f, 0.4f);
7993 // Blink only uses the integer part of the scroll_offset for fixed
7994 // position layer.
7995 SetLayerPropertiesForTesting(fixed.get(), identity_transform, gfx::Point3F(),
7996 gfx::PointF(3.0f, 4.0f), gfx::Size(50, 50), true,
7997 false, false);
7998 scroll_layer->PushScrollOffsetFromMainThread(scroll_offset);
7999 scroll_layer->SetScrollDelta(scroll_delta);
8000 scroll_layer->SetScrollCompensationAdjustment(main_scroll_fractional_part);
8002 scroller->AddChild(fixed.Pass());
8003 container->AddChild(scroller.Pass());
8004 root->AddChild(container.Pass());
8006 LayerImplList render_surface_layer_list;
8007 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
8008 root.get(), root->bounds(), &render_surface_layer_list);
8009 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8011 EXPECT_TRANSFORMATION_MATRIX_EQ(
8012 container_layer->draw_properties().screen_space_transform,
8013 fixed_layer->draw_properties().screen_space_transform);
8014 EXPECT_VECTOR_EQ(
8015 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
8016 container_offset);
8018 gfx::ScrollOffset effective_scroll_offset =
8019 ScrollOffsetWithDelta(scroll_offset, scroll_delta);
8020 gfx::Vector2d rounded_effective_scroll_offset =
8021 ToRoundedVector2d(ScrollOffsetToVector2dF(effective_scroll_offset));
8022 EXPECT_VECTOR_EQ(
8023 scroll_layer->draw_properties().screen_space_transform.To2dTranslation(),
8024 container_offset - rounded_effective_scroll_offset);
8027 class AnimationScaleFactorTrackingLayerImpl : public LayerImpl {
8028 public:
8029 static scoped_ptr<AnimationScaleFactorTrackingLayerImpl> Create(
8030 LayerTreeImpl* tree_impl,
8031 int id) {
8032 return make_scoped_ptr(
8033 new AnimationScaleFactorTrackingLayerImpl(tree_impl, id));
8036 ~AnimationScaleFactorTrackingLayerImpl() override {}
8038 private:
8039 explicit AnimationScaleFactorTrackingLayerImpl(LayerTreeImpl* tree_impl,
8040 int id)
8041 : LayerImpl(tree_impl, id) {
8042 SetDrawsContent(true);
8046 TEST_F(LayerTreeHostCommonTest, MaximumAnimationScaleFactor) {
8047 FakeImplProxy proxy;
8048 TestSharedBitmapManager shared_bitmap_manager;
8049 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8050 gfx::Transform identity_matrix;
8051 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> grand_parent =
8052 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 1);
8053 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> parent =
8054 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 2);
8055 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> child =
8056 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 3);
8057 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> grand_child =
8058 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 4);
8060 AnimationScaleFactorTrackingLayerImpl* parent_raw = parent.get();
8061 AnimationScaleFactorTrackingLayerImpl* child_raw = child.get();
8062 AnimationScaleFactorTrackingLayerImpl* grand_child_raw = grand_child.get();
8064 child->AddChild(grand_child.Pass());
8065 parent->AddChild(child.Pass());
8066 grand_parent->AddChild(parent.Pass());
8068 SetLayerPropertiesForTesting(grand_parent.get(), identity_matrix,
8069 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8070 true, false, true);
8071 SetLayerPropertiesForTesting(parent_raw, identity_matrix, gfx::Point3F(),
8072 gfx::PointF(), gfx::Size(1, 2), true, false,
8073 false);
8074 SetLayerPropertiesForTesting(child_raw, identity_matrix, gfx::Point3F(),
8075 gfx::PointF(), gfx::Size(1, 2), true, false,
8076 false);
8078 SetLayerPropertiesForTesting(grand_child_raw, identity_matrix, gfx::Point3F(),
8079 gfx::PointF(), gfx::Size(1, 2), true, false,
8080 false);
8082 ExecuteCalculateDrawProperties(grand_parent.get());
8084 // No layers have animations.
8085 EXPECT_EQ(0.f,
8086 grand_parent->draw_properties().maximum_animation_contents_scale);
8087 EXPECT_EQ(0.f,
8088 parent_raw->draw_properties().maximum_animation_contents_scale);
8089 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8090 EXPECT_EQ(
8091 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8093 TransformOperations translation;
8094 translation.AppendTranslate(1.f, 2.f, 3.f);
8096 AddAnimatedTransformToLayer(
8097 parent_raw, 1.0, TransformOperations(), translation);
8099 // No layers have scale-affecting animations.
8100 EXPECT_EQ(0.f,
8101 grand_parent->draw_properties().maximum_animation_contents_scale);
8102 EXPECT_EQ(0.f,
8103 parent_raw->draw_properties().maximum_animation_contents_scale);
8104 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8105 EXPECT_EQ(
8106 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8108 TransformOperations scale;
8109 scale.AppendScale(5.f, 4.f, 3.f);
8111 AddAnimatedTransformToLayer(child_raw, 1.0, TransformOperations(), scale);
8112 ExecuteCalculateDrawProperties(grand_parent.get());
8114 // Only |child| has a scale-affecting animation.
8115 EXPECT_EQ(0.f,
8116 grand_parent->draw_properties().maximum_animation_contents_scale);
8117 EXPECT_EQ(0.f,
8118 parent_raw->draw_properties().maximum_animation_contents_scale);
8119 EXPECT_EQ(5.f, child_raw->draw_properties().maximum_animation_contents_scale);
8120 EXPECT_EQ(
8121 5.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8123 AddAnimatedTransformToLayer(
8124 grand_parent.get(), 1.0, TransformOperations(), scale);
8125 ExecuteCalculateDrawProperties(grand_parent.get());
8127 // |grand_parent| and |child| have scale-affecting animations.
8128 EXPECT_EQ(5.f,
8129 grand_parent->draw_properties().maximum_animation_contents_scale);
8130 EXPECT_EQ(5.f,
8131 parent_raw->draw_properties().maximum_animation_contents_scale);
8132 // We don't support combining animated scales from two nodes; 0.f means
8133 // that the maximum scale could not be computed.
8134 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8135 EXPECT_EQ(
8136 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8138 AddAnimatedTransformToLayer(parent_raw, 1.0, TransformOperations(), scale);
8139 ExecuteCalculateDrawProperties(grand_parent.get());
8141 // |grand_parent|, |parent|, and |child| have scale-affecting animations.
8142 EXPECT_EQ(5.f,
8143 grand_parent->draw_properties().maximum_animation_contents_scale);
8144 EXPECT_EQ(0.f,
8145 parent_raw->draw_properties().maximum_animation_contents_scale);
8146 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8147 EXPECT_EQ(
8148 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8150 grand_parent->layer_animation_controller()->AbortAnimations(
8151 Animation::TRANSFORM);
8152 parent_raw->layer_animation_controller()->AbortAnimations(
8153 Animation::TRANSFORM);
8154 child_raw->layer_animation_controller()->AbortAnimations(
8155 Animation::TRANSFORM);
8157 TransformOperations perspective;
8158 perspective.AppendPerspective(10.f);
8160 AddAnimatedTransformToLayer(
8161 child_raw, 1.0, TransformOperations(), perspective);
8162 ExecuteCalculateDrawProperties(grand_parent.get());
8164 // |child| has a scale-affecting animation but computing the maximum of this
8165 // animation is not supported.
8166 EXPECT_EQ(0.f,
8167 grand_parent->draw_properties().maximum_animation_contents_scale);
8168 EXPECT_EQ(0.f,
8169 parent_raw->draw_properties().maximum_animation_contents_scale);
8170 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8171 EXPECT_EQ(
8172 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8174 child_raw->layer_animation_controller()->AbortAnimations(
8175 Animation::TRANSFORM);
8177 gfx::Transform scale_matrix;
8178 scale_matrix.Scale(1.f, 2.f);
8179 grand_parent->SetTransform(scale_matrix);
8180 parent_raw->SetTransform(scale_matrix);
8181 AddAnimatedTransformToLayer(parent_raw, 1.0, TransformOperations(), scale);
8182 ExecuteCalculateDrawProperties(grand_parent.get());
8184 // |grand_parent| and |parent| each have scale 2.f. |parent| has a scale
8185 // animation with maximum scale 5.f.
8186 EXPECT_EQ(0.f,
8187 grand_parent->draw_properties().maximum_animation_contents_scale);
8188 EXPECT_EQ(10.f,
8189 parent_raw->draw_properties().maximum_animation_contents_scale);
8190 EXPECT_EQ(10.f,
8191 child_raw->draw_properties().maximum_animation_contents_scale);
8192 EXPECT_EQ(
8193 10.f,
8194 grand_child_raw->draw_properties().maximum_animation_contents_scale);
8196 gfx::Transform perspective_matrix;
8197 perspective_matrix.ApplyPerspectiveDepth(2.f);
8198 child_raw->SetTransform(perspective_matrix);
8199 ExecuteCalculateDrawProperties(grand_parent.get());
8201 // |child| has a transform that's neither a translation nor a scale.
8202 EXPECT_EQ(0.f,
8203 grand_parent->draw_properties().maximum_animation_contents_scale);
8204 EXPECT_EQ(10.f,
8205 parent_raw->draw_properties().maximum_animation_contents_scale);
8206 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8207 EXPECT_EQ(
8208 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8210 parent_raw->SetTransform(perspective_matrix);
8211 ExecuteCalculateDrawProperties(grand_parent.get());
8213 // |parent| and |child| have transforms that are neither translations nor
8214 // scales.
8215 EXPECT_EQ(0.f,
8216 grand_parent->draw_properties().maximum_animation_contents_scale);
8217 EXPECT_EQ(0.f,
8218 parent_raw->draw_properties().maximum_animation_contents_scale);
8219 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8220 EXPECT_EQ(
8221 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8223 parent_raw->SetTransform(identity_matrix);
8224 child_raw->SetTransform(identity_matrix);
8225 grand_parent->SetTransform(perspective_matrix);
8227 ExecuteCalculateDrawProperties(grand_parent.get());
8229 // |grand_parent| has a transform that's neither a translation nor a scale.
8230 EXPECT_EQ(0.f,
8231 grand_parent->draw_properties().maximum_animation_contents_scale);
8232 EXPECT_EQ(0.f,
8233 parent_raw->draw_properties().maximum_animation_contents_scale);
8234 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8235 EXPECT_EQ(
8236 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8239 static int membership_id(LayerImpl* layer) {
8240 return layer->draw_properties().last_drawn_render_surface_layer_list_id;
8243 static void GatherDrawnLayers(LayerImplList* rsll,
8244 std::set<LayerImpl*>* drawn_layers) {
8245 for (LayerIterator<LayerImpl> it = LayerIterator<LayerImpl>::Begin(rsll),
8246 end = LayerIterator<LayerImpl>::End(rsll);
8247 it != end;
8248 ++it) {
8249 LayerImpl* layer = *it;
8250 if (it.represents_itself())
8251 drawn_layers->insert(layer);
8253 if (!it.represents_contributing_render_surface())
8254 continue;
8256 if (layer->mask_layer())
8257 drawn_layers->insert(layer->mask_layer());
8258 if (layer->replica_layer() && layer->replica_layer()->mask_layer())
8259 drawn_layers->insert(layer->replica_layer()->mask_layer());
8263 TEST_F(LayerTreeHostCommonTest, RenderSurfaceLayerListMembership) {
8264 FakeImplProxy proxy;
8265 TestSharedBitmapManager shared_bitmap_manager;
8266 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8267 gfx::Transform identity_matrix;
8269 scoped_ptr<LayerImpl> grand_parent =
8270 LayerImpl::Create(host_impl.active_tree(), 1);
8271 scoped_ptr<LayerImpl> parent = LayerImpl::Create(host_impl.active_tree(), 3);
8272 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.active_tree(), 5);
8273 scoped_ptr<LayerImpl> grand_child1 =
8274 LayerImpl::Create(host_impl.active_tree(), 7);
8275 scoped_ptr<LayerImpl> grand_child2 =
8276 LayerImpl::Create(host_impl.active_tree(), 9);
8278 LayerImpl* grand_parent_raw = grand_parent.get();
8279 LayerImpl* parent_raw = parent.get();
8280 LayerImpl* child_raw = child.get();
8281 LayerImpl* grand_child1_raw = grand_child1.get();
8282 LayerImpl* grand_child2_raw = grand_child2.get();
8284 child->AddChild(grand_child1.Pass());
8285 child->AddChild(grand_child2.Pass());
8286 parent->AddChild(child.Pass());
8287 grand_parent->AddChild(parent.Pass());
8289 SetLayerPropertiesForTesting(grand_parent_raw, identity_matrix,
8290 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8291 true, false, true);
8292 SetLayerPropertiesForTesting(parent_raw, identity_matrix, gfx::Point3F(),
8293 gfx::PointF(), gfx::Size(1, 2), true, false,
8294 false);
8296 SetLayerPropertiesForTesting(child_raw, identity_matrix, gfx::Point3F(),
8297 gfx::PointF(), gfx::Size(1, 2), true, false,
8298 false);
8300 SetLayerPropertiesForTesting(grand_child1_raw, identity_matrix,
8301 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8302 true, false, false);
8304 SetLayerPropertiesForTesting(grand_child2_raw, identity_matrix,
8305 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8306 true, false, false);
8308 // Start with nothing being drawn.
8309 ExecuteCalculateDrawProperties(grand_parent_raw);
8310 int member_id = render_surface_layer_list_count();
8312 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8313 EXPECT_NE(member_id, membership_id(parent_raw));
8314 EXPECT_NE(member_id, membership_id(child_raw));
8315 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8316 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8318 std::set<LayerImpl*> expected;
8319 std::set<LayerImpl*> actual;
8320 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8321 EXPECT_EQ(expected, actual);
8323 // If we force render surface, but none of the layers are in the layer list,
8324 // then this layer should not appear in RSLL.
8325 grand_child1_raw->SetHasRenderSurface(true);
8327 ExecuteCalculateDrawProperties(grand_parent_raw);
8328 member_id = render_surface_layer_list_count();
8330 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8331 EXPECT_NE(member_id, membership_id(parent_raw));
8332 EXPECT_NE(member_id, membership_id(child_raw));
8333 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8334 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8336 expected.clear();
8337 actual.clear();
8338 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8339 EXPECT_EQ(expected, actual);
8341 // However, if we say that this layer also draws content, it will appear in
8342 // RSLL.
8343 grand_child1_raw->SetDrawsContent(true);
8345 ExecuteCalculateDrawProperties(grand_parent_raw);
8346 member_id = render_surface_layer_list_count();
8348 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8349 EXPECT_NE(member_id, membership_id(parent_raw));
8350 EXPECT_NE(member_id, membership_id(child_raw));
8351 EXPECT_EQ(member_id, membership_id(grand_child1_raw));
8352 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8354 expected.clear();
8355 expected.insert(grand_child1_raw);
8357 actual.clear();
8358 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8359 EXPECT_EQ(expected, actual);
8361 // Now child is forced to have a render surface, and one if its children draws
8362 // content.
8363 grand_child1_raw->SetDrawsContent(false);
8364 grand_child1_raw->SetHasRenderSurface(false);
8365 child_raw->SetHasRenderSurface(true);
8366 grand_child2_raw->SetDrawsContent(true);
8368 ExecuteCalculateDrawProperties(grand_parent_raw);
8369 member_id = render_surface_layer_list_count();
8371 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8372 EXPECT_NE(member_id, membership_id(parent_raw));
8373 EXPECT_NE(member_id, membership_id(child_raw));
8374 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8375 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8377 expected.clear();
8378 expected.insert(grand_child2_raw);
8380 actual.clear();
8381 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8382 EXPECT_EQ(expected, actual);
8384 // Add a mask layer to child.
8385 child_raw->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 6).Pass());
8387 ExecuteCalculateDrawProperties(grand_parent_raw);
8388 member_id = render_surface_layer_list_count();
8390 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8391 EXPECT_NE(member_id, membership_id(parent_raw));
8392 EXPECT_NE(member_id, membership_id(child_raw));
8393 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8394 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8395 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8397 expected.clear();
8398 expected.insert(grand_child2_raw);
8399 expected.insert(child_raw->mask_layer());
8401 expected.clear();
8402 expected.insert(grand_child2_raw);
8403 expected.insert(child_raw->mask_layer());
8405 actual.clear();
8406 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8407 EXPECT_EQ(expected, actual);
8409 // Add replica mask layer.
8410 scoped_ptr<LayerImpl> replica_layer =
8411 LayerImpl::Create(host_impl.active_tree(), 20);
8412 replica_layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 21));
8413 child_raw->SetReplicaLayer(replica_layer.Pass());
8415 ExecuteCalculateDrawProperties(grand_parent_raw);
8416 member_id = render_surface_layer_list_count();
8418 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8419 EXPECT_NE(member_id, membership_id(parent_raw));
8420 EXPECT_NE(member_id, membership_id(child_raw));
8421 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8422 EXPECT_EQ(member_id, membership_id(child_raw->replica_layer()->mask_layer()));
8423 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8424 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8426 expected.clear();
8427 expected.insert(grand_child2_raw);
8428 expected.insert(child_raw->mask_layer());
8429 expected.insert(child_raw->replica_layer()->mask_layer());
8431 actual.clear();
8432 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8433 EXPECT_EQ(expected, actual);
8435 child_raw->TakeReplicaLayer();
8437 // With nothing drawing, we should have no layers.
8438 grand_child2_raw->SetDrawsContent(false);
8440 ExecuteCalculateDrawProperties(grand_parent_raw);
8441 member_id = render_surface_layer_list_count();
8443 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8444 EXPECT_NE(member_id, membership_id(parent_raw));
8445 EXPECT_NE(member_id, membership_id(child_raw));
8446 EXPECT_NE(member_id, membership_id(child_raw->mask_layer()));
8447 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8448 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8450 expected.clear();
8451 actual.clear();
8452 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8453 EXPECT_EQ(expected, actual);
8455 // Child itself draws means that we should have the child and the mask in the
8456 // list.
8457 child_raw->SetDrawsContent(true);
8459 ExecuteCalculateDrawProperties(grand_parent_raw);
8460 member_id = render_surface_layer_list_count();
8462 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8463 EXPECT_NE(member_id, membership_id(parent_raw));
8464 EXPECT_EQ(member_id, membership_id(child_raw));
8465 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8466 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8467 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8469 expected.clear();
8470 expected.insert(child_raw);
8471 expected.insert(child_raw->mask_layer());
8472 actual.clear();
8473 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8474 EXPECT_EQ(expected, actual);
8476 child_raw->TakeMaskLayer();
8478 // Now everyone's a member!
8479 grand_parent_raw->SetDrawsContent(true);
8480 parent_raw->SetDrawsContent(true);
8481 child_raw->SetDrawsContent(true);
8482 grand_child1_raw->SetDrawsContent(true);
8483 grand_child2_raw->SetDrawsContent(true);
8485 ExecuteCalculateDrawProperties(grand_parent_raw);
8486 member_id = render_surface_layer_list_count();
8488 EXPECT_EQ(member_id, membership_id(grand_parent_raw));
8489 EXPECT_EQ(member_id, membership_id(parent_raw));
8490 EXPECT_EQ(member_id, membership_id(child_raw));
8491 EXPECT_EQ(member_id, membership_id(grand_child1_raw));
8492 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8494 expected.clear();
8495 expected.insert(grand_parent_raw);
8496 expected.insert(parent_raw);
8497 expected.insert(child_raw);
8498 expected.insert(grand_child1_raw);
8499 expected.insert(grand_child2_raw);
8501 actual.clear();
8502 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8503 EXPECT_EQ(expected, actual);
8506 TEST_F(LayerTreeHostCommonTest, DrawPropertyScales) {
8507 FakeImplProxy proxy;
8508 TestSharedBitmapManager shared_bitmap_manager;
8509 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8511 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
8512 LayerImpl* root_layer = root.get();
8513 scoped_ptr<LayerImpl> child1 = LayerImpl::Create(host_impl.active_tree(), 2);
8514 LayerImpl* child1_layer = child1.get();
8515 scoped_ptr<LayerImpl> child2 = LayerImpl::Create(host_impl.active_tree(), 3);
8516 LayerImpl* child2_layer = child2.get();
8518 root->AddChild(child1.Pass());
8519 root->AddChild(child2.Pass());
8520 root->SetHasRenderSurface(true);
8522 gfx::Transform identity_matrix, scale_transform_child1,
8523 scale_transform_child2;
8524 scale_transform_child1.Scale(2, 3);
8525 scale_transform_child2.Scale(4, 5);
8527 SetLayerPropertiesForTesting(root_layer, identity_matrix, gfx::Point3F(),
8528 gfx::PointF(), gfx::Size(1, 1), true, false,
8529 true);
8530 SetLayerPropertiesForTesting(child1_layer, scale_transform_child1,
8531 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
8532 false, false);
8534 child1_layer->SetMaskLayer(
8535 LayerImpl::Create(host_impl.active_tree(), 4).Pass());
8537 scoped_ptr<LayerImpl> replica_layer =
8538 LayerImpl::Create(host_impl.active_tree(), 5);
8539 replica_layer->SetHasRenderSurface(true);
8540 replica_layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 6));
8541 child1_layer->SetReplicaLayer(replica_layer.Pass());
8542 child1_layer->SetHasRenderSurface(true);
8544 ExecuteCalculateDrawProperties(root_layer);
8546 TransformOperations scale;
8547 scale.AppendScale(5.f, 8.f, 3.f);
8549 AddAnimatedTransformToLayer(child2_layer, 1.0, TransformOperations(), scale);
8550 SetLayerPropertiesForTesting(child2_layer, scale_transform_child2,
8551 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
8552 false, false);
8554 ExecuteCalculateDrawProperties(root_layer);
8556 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().ideal_contents_scale);
8557 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().ideal_contents_scale);
8558 EXPECT_FLOAT_EQ(
8559 3.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8560 EXPECT_FLOAT_EQ(3.f,
8561 child1_layer->replica_layer()
8562 ->mask_layer()
8563 ->draw_properties()
8564 .ideal_contents_scale);
8565 EXPECT_FLOAT_EQ(5.f, child2_layer->draw_properties().ideal_contents_scale);
8567 EXPECT_FLOAT_EQ(
8568 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8569 EXPECT_FLOAT_EQ(
8570 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8571 EXPECT_FLOAT_EQ(0.f,
8572 child1_layer->mask_layer()
8573 ->draw_properties()
8574 .maximum_animation_contents_scale);
8575 EXPECT_FLOAT_EQ(0.f,
8576 child1_layer->replica_layer()
8577 ->mask_layer()
8578 ->draw_properties()
8579 .maximum_animation_contents_scale);
8580 EXPECT_FLOAT_EQ(
8581 8.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8583 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8584 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().page_scale_factor);
8585 EXPECT_FLOAT_EQ(
8586 1.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8587 EXPECT_FLOAT_EQ(1.f,
8588 child1_layer->replica_layer()
8589 ->mask_layer()
8590 ->draw_properties()
8591 .page_scale_factor);
8592 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().page_scale_factor);
8594 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().device_scale_factor);
8595 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().device_scale_factor);
8596 EXPECT_FLOAT_EQ(
8597 1.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8598 EXPECT_FLOAT_EQ(1.f,
8599 child1_layer->replica_layer()
8600 ->mask_layer()
8601 ->draw_properties()
8602 .device_scale_factor);
8603 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().device_scale_factor);
8605 // Changing page-scale would affect ideal_contents_scale and
8606 // maximum_animation_contents_scale.
8608 float page_scale_factor = 3.f;
8609 float device_scale_factor = 1.0f;
8610 std::vector<LayerImpl*> render_surface_layer_list;
8611 gfx::Size device_viewport_size =
8612 gfx::Size(root_layer->bounds().width() * device_scale_factor,
8613 root_layer->bounds().height() * device_scale_factor);
8614 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
8615 root_layer, device_viewport_size, &render_surface_layer_list);
8617 inputs.page_scale_factor = page_scale_factor;
8618 inputs.can_adjust_raster_scales = true;
8619 inputs.page_scale_application_layer = root_layer;
8620 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8622 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().ideal_contents_scale);
8623 EXPECT_FLOAT_EQ(9.f, child1_layer->draw_properties().ideal_contents_scale);
8624 EXPECT_FLOAT_EQ(
8625 9.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8626 EXPECT_FLOAT_EQ(9.f,
8627 child1_layer->replica_layer()
8628 ->mask_layer()
8629 ->draw_properties()
8630 .ideal_contents_scale);
8631 EXPECT_FLOAT_EQ(15.f, child2_layer->draw_properties().ideal_contents_scale);
8633 EXPECT_FLOAT_EQ(
8634 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8635 EXPECT_FLOAT_EQ(
8636 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8637 EXPECT_FLOAT_EQ(0.f,
8638 child1_layer->mask_layer()
8639 ->draw_properties()
8640 .maximum_animation_contents_scale);
8641 EXPECT_FLOAT_EQ(0.f,
8642 child1_layer->replica_layer()
8643 ->mask_layer()
8644 ->draw_properties()
8645 .maximum_animation_contents_scale);
8646 EXPECT_FLOAT_EQ(
8647 24.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8649 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8650 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().page_scale_factor);
8651 EXPECT_FLOAT_EQ(
8652 3.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8653 EXPECT_FLOAT_EQ(3.f,
8654 child1_layer->replica_layer()
8655 ->mask_layer()
8656 ->draw_properties()
8657 .page_scale_factor);
8658 EXPECT_FLOAT_EQ(3.f, child2_layer->draw_properties().page_scale_factor);
8660 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().device_scale_factor);
8661 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().device_scale_factor);
8662 EXPECT_FLOAT_EQ(
8663 1.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8664 EXPECT_FLOAT_EQ(1.f,
8665 child1_layer->replica_layer()
8666 ->mask_layer()
8667 ->draw_properties()
8668 .device_scale_factor);
8669 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().device_scale_factor);
8671 // Changing device-scale would affect ideal_contents_scale and
8672 // maximum_animation_contents_scale.
8674 device_scale_factor = 4.0f;
8675 inputs.device_scale_factor = device_scale_factor;
8676 inputs.can_adjust_raster_scales = true;
8677 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8679 EXPECT_FLOAT_EQ(4.f, root_layer->draw_properties().ideal_contents_scale);
8680 EXPECT_FLOAT_EQ(36.f, child1_layer->draw_properties().ideal_contents_scale);
8681 EXPECT_FLOAT_EQ(
8682 36.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8683 EXPECT_FLOAT_EQ(36.f,
8684 child1_layer->replica_layer()
8685 ->mask_layer()
8686 ->draw_properties()
8687 .ideal_contents_scale);
8688 EXPECT_FLOAT_EQ(60.f, child2_layer->draw_properties().ideal_contents_scale);
8690 EXPECT_FLOAT_EQ(
8691 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8692 EXPECT_FLOAT_EQ(
8693 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8694 EXPECT_FLOAT_EQ(0.f,
8695 child1_layer->mask_layer()
8696 ->draw_properties()
8697 .maximum_animation_contents_scale);
8698 EXPECT_FLOAT_EQ(0.f,
8699 child1_layer->replica_layer()
8700 ->mask_layer()
8701 ->draw_properties()
8702 .maximum_animation_contents_scale);
8703 EXPECT_FLOAT_EQ(
8704 96.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8706 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8707 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().page_scale_factor);
8708 EXPECT_FLOAT_EQ(
8709 3.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8710 EXPECT_FLOAT_EQ(3.f,
8711 child1_layer->replica_layer()
8712 ->mask_layer()
8713 ->draw_properties()
8714 .page_scale_factor);
8715 EXPECT_FLOAT_EQ(3.f, child2_layer->draw_properties().page_scale_factor);
8717 EXPECT_FLOAT_EQ(4.f, root_layer->draw_properties().device_scale_factor);
8718 EXPECT_FLOAT_EQ(4.f, child1_layer->draw_properties().device_scale_factor);
8719 EXPECT_FLOAT_EQ(
8720 4.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8721 EXPECT_FLOAT_EQ(4.f,
8722 child1_layer->replica_layer()
8723 ->mask_layer()
8724 ->draw_properties()
8725 .device_scale_factor);
8726 EXPECT_FLOAT_EQ(4.f, child2_layer->draw_properties().device_scale_factor);
8729 TEST_F(LayerTreeHostCommonTest, VisibleContentRectInChildRenderSurface) {
8730 scoped_refptr<Layer> root = Layer::Create();
8731 SetLayerPropertiesForTesting(root.get(),
8732 gfx::Transform(),
8733 gfx::Point3F(),
8734 gfx::PointF(),
8735 gfx::Size(768 / 2, 3000),
8736 true,
8737 false);
8738 root->SetIsDrawable(true);
8740 scoped_refptr<Layer> clip = Layer::Create();
8741 SetLayerPropertiesForTesting(clip.get(),
8742 gfx::Transform(),
8743 gfx::Point3F(),
8744 gfx::PointF(),
8745 gfx::Size(768 / 2, 10000),
8746 true,
8747 false);
8748 clip->SetMasksToBounds(true);
8750 scoped_refptr<Layer> content = Layer::Create();
8751 SetLayerPropertiesForTesting(content.get(),
8752 gfx::Transform(),
8753 gfx::Point3F(),
8754 gfx::PointF(),
8755 gfx::Size(768 / 2, 10000),
8756 true,
8757 false);
8758 content->SetIsDrawable(true);
8759 content->SetForceRenderSurface(true);
8761 root->AddChild(clip);
8762 clip->AddChild(content);
8764 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
8765 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
8766 host->SetRootLayer(root);
8768 gfx::Size device_viewport_size(768, 582);
8769 RenderSurfaceLayerList render_surface_layer_list;
8770 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
8771 host->root_layer(), device_viewport_size, &render_surface_layer_list);
8772 inputs.device_scale_factor = 2.f;
8773 inputs.page_scale_factor = 1.f;
8774 inputs.page_scale_application_layer = NULL;
8775 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8777 // Layers in the root render surface have their visible content rect clipped
8778 // by the viewport.
8779 EXPECT_EQ(gfx::Rect(768 / 2, 582 / 2), root->visible_content_rect());
8781 // Layers drawing to a child render surface should still have their visible
8782 // content rect clipped by the viewport.
8783 EXPECT_EQ(gfx::Rect(768 / 2, 582 / 2), content->visible_content_rect());
8786 TEST_F(LayerTreeHostCommonTest, BoundsDeltaAffectVisibleContentRect) {
8787 FakeImplProxy proxy;
8788 TestSharedBitmapManager shared_bitmap_manager;
8789 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8791 // Set two layers: the root layer clips it's child,
8792 // the child draws its content.
8794 gfx::Size root_size = gfx::Size(300, 500);
8796 // Sublayer should be bigger than the root enlarged by bounds_delta.
8797 gfx::Size sublayer_size = gfx::Size(300, 1000);
8799 // Device viewport accomidated the root and the top controls.
8800 gfx::Size device_viewport_size = gfx::Size(300, 600);
8801 gfx::Transform identity_matrix;
8803 host_impl.active_tree()->SetRootLayer(
8804 LayerImpl::Create(host_impl.active_tree(), 1));
8806 LayerImpl* root = host_impl.active_tree()->root_layer();
8807 SetLayerPropertiesForTesting(root,
8808 identity_matrix,
8809 gfx::Point3F(),
8810 gfx::PointF(),
8811 root_size,
8812 false,
8813 false,
8814 true);
8816 root->SetContentBounds(root_size);
8817 root->SetMasksToBounds(true);
8819 root->AddChild(LayerImpl::Create(host_impl.active_tree(), 2));
8821 LayerImpl* sublayer = root->child_at(0);
8822 SetLayerPropertiesForTesting(sublayer,
8823 identity_matrix,
8824 gfx::Point3F(),
8825 gfx::PointF(),
8826 sublayer_size,
8827 false,
8828 false,
8829 false);
8831 sublayer->SetContentBounds(sublayer_size);
8832 sublayer->SetDrawsContent(true);
8834 LayerImplList layer_impl_list;
8835 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
8836 root, device_viewport_size, &layer_impl_list);
8838 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8840 EXPECT_EQ(gfx::Rect(root_size), sublayer->visible_content_rect());
8842 root->SetBoundsDelta(gfx::Vector2dF(0.0, 50.0));
8844 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8846 gfx::Rect affected_by_delta(0, 0, root_size.width(),
8847 root_size.height() + 50);
8848 EXPECT_EQ(affected_by_delta, sublayer->visible_content_rect());
8851 TEST_F(LayerTreeHostCommonTest, VisibleContentRectForAnimatedLayer) {
8852 const gfx::Transform identity_matrix;
8853 scoped_refptr<Layer> root = Layer::Create();
8854 scoped_refptr<LayerWithForcedDrawsContent> animated =
8855 make_scoped_refptr(new LayerWithForcedDrawsContent());
8857 root->AddChild(animated);
8859 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8860 host->SetRootLayer(root);
8862 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
8863 gfx::PointF(), gfx::Size(100, 100), true, false);
8864 SetLayerPropertiesForTesting(animated.get(), identity_matrix, gfx::Point3F(),
8865 gfx::PointF(), gfx::Size(20, 20), true, false);
8867 root->SetMasksToBounds(true);
8868 root->SetForceRenderSurface(true);
8869 animated->SetOpacity(0.f);
8871 AddOpacityTransitionToController(animated->layer_animation_controller(), 10.0,
8872 0.f, 1.f, false);
8874 ExecuteCalculateDrawProperties(root.get());
8876 EXPECT_FALSE(animated->visible_rect_from_property_trees().IsEmpty());
8879 TEST_F(LayerTreeHostCommonTest,
8880 VisibleContentRectForAnimatedLayerWithSingularTransform) {
8881 const gfx::Transform identity_matrix;
8882 scoped_refptr<Layer> root = Layer::Create();
8883 scoped_refptr<Layer> clip = Layer::Create();
8884 scoped_refptr<LayerWithForcedDrawsContent> animated =
8885 make_scoped_refptr(new LayerWithForcedDrawsContent());
8886 scoped_refptr<LayerWithForcedDrawsContent> surface =
8887 make_scoped_refptr(new LayerWithForcedDrawsContent());
8888 scoped_refptr<LayerWithForcedDrawsContent> descendant_of_animation =
8889 make_scoped_refptr(new LayerWithForcedDrawsContent());
8891 root->AddChild(clip);
8892 clip->AddChild(animated);
8893 animated->AddChild(surface);
8894 surface->AddChild(descendant_of_animation);
8896 clip->SetMasksToBounds(true);
8897 surface->SetForceRenderSurface(true);
8899 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8900 host->SetRootLayer(root);
8902 gfx::Transform uninvertible_matrix;
8903 uninvertible_matrix.Scale3d(6.f, 6.f, 0.f);
8905 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
8906 gfx::PointF(), gfx::Size(100, 100), true, false);
8907 SetLayerPropertiesForTesting(clip.get(), identity_matrix, gfx::Point3F(),
8908 gfx::PointF(), gfx::Size(10, 10), true, false);
8909 SetLayerPropertiesForTesting(animated.get(), uninvertible_matrix,
8910 gfx::Point3F(), gfx::PointF(),
8911 gfx::Size(120, 120), true, false);
8912 SetLayerPropertiesForTesting(surface.get(), identity_matrix, gfx::Point3F(),
8913 gfx::PointF(), gfx::Size(100, 100), true, false);
8914 SetLayerPropertiesForTesting(descendant_of_animation.get(), identity_matrix,
8915 gfx::Point3F(), gfx::PointF(),
8916 gfx::Size(200, 200), true, false);
8918 TransformOperations start_transform_operations;
8919 start_transform_operations.AppendMatrix(uninvertible_matrix);
8920 TransformOperations end_transform_operations;
8922 AddAnimatedTransformToLayer(animated.get(), 10.0, start_transform_operations,
8923 end_transform_operations);
8925 ExecuteCalculateDrawProperties(root.get());
8927 // The animated layer has a singular transform and maps to a non-empty rect in
8928 // clipped target space, so is treated as fully visible.
8929 EXPECT_EQ(gfx::Rect(120, 120), animated->visible_rect_from_property_trees());
8931 // The singular transform on |animated| is flattened when inherited by
8932 // |surface|, and this happens to make it invertible.
8933 EXPECT_EQ(gfx::Rect(2, 2), surface->visible_rect_from_property_trees());
8934 EXPECT_EQ(gfx::Rect(2, 2),
8935 descendant_of_animation->visible_rect_from_property_trees());
8937 gfx::Transform zero_matrix;
8938 zero_matrix.Scale3d(0.f, 0.f, 0.f);
8939 SetLayerPropertiesForTesting(animated.get(), zero_matrix, gfx::Point3F(),
8940 gfx::PointF(), gfx::Size(120, 120), true, false);
8942 ExecuteCalculateDrawProperties(root.get());
8944 // The animated layer maps to the empty rect in clipped target space, so is
8945 // treated as having an empty visible rect.
8946 EXPECT_EQ(gfx::Rect(), animated->visible_rect_from_property_trees());
8948 // This time, flattening does not make |animated|'s transform invertible. This
8949 // means the clip cannot be projected into |surface|'s space, so we treat
8950 // |surface| and layers that draw into it as fully visible.
8951 EXPECT_EQ(gfx::Rect(100, 100), surface->visible_rect_from_property_trees());
8952 EXPECT_EQ(gfx::Rect(200, 200),
8953 descendant_of_animation->visible_rect_from_property_trees());
8956 // Verify that having an animated filter (but no current filter, as these
8957 // are mutually exclusive) correctly creates a render surface.
8958 TEST_F(LayerTreeHostCommonTest, AnimatedFilterCreatesRenderSurface) {
8959 scoped_refptr<Layer> root = Layer::Create();
8960 scoped_refptr<Layer> child = Layer::Create();
8961 scoped_refptr<Layer> grandchild = Layer::Create();
8962 root->AddChild(child);
8963 child->AddChild(grandchild);
8965 gfx::Transform identity_transform;
8966 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
8967 gfx::PointF(), gfx::Size(50, 50), true, false);
8968 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
8969 gfx::PointF(), gfx::Size(50, 50), true, false);
8970 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
8971 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
8972 true, false);
8973 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8974 host->SetRootLayer(root);
8976 AddAnimatedFilterToLayer(child.get(), 10.0, 0.1f, 0.2f);
8978 ExecuteCalculateDrawProperties(root.get());
8980 EXPECT_TRUE(root->render_surface());
8981 EXPECT_TRUE(child->render_surface());
8982 EXPECT_FALSE(grandchild->render_surface());
8984 EXPECT_TRUE(root->filters().IsEmpty());
8985 EXPECT_TRUE(child->filters().IsEmpty());
8986 EXPECT_TRUE(grandchild->filters().IsEmpty());
8988 EXPECT_FALSE(root->FilterIsAnimating());
8989 EXPECT_TRUE(child->FilterIsAnimating());
8990 EXPECT_FALSE(grandchild->FilterIsAnimating());
8993 // Ensures that the property tree code accounts for offsets between fixed
8994 // position layers and their respective containers.
8995 TEST_F(LayerTreeHostCommonTest, PropertyTreesAccountForFixedParentOffset) {
8996 scoped_refptr<Layer> root = Layer::Create();
8997 scoped_refptr<Layer> child = Layer::Create();
8998 scoped_refptr<LayerWithForcedDrawsContent> grandchild =
8999 make_scoped_refptr(new LayerWithForcedDrawsContent());
9001 root->AddChild(child);
9002 child->AddChild(grandchild);
9004 gfx::Transform identity_transform;
9005 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
9006 gfx::PointF(), gfx::Size(50, 50), true, false);
9007 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
9008 gfx::PointF(1000, 1000), gfx::Size(50, 50), true,
9009 false);
9010 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
9011 gfx::Point3F(), gfx::PointF(-1000, -1000),
9012 gfx::Size(50, 50), true, false);
9014 root->SetMasksToBounds(true);
9015 root->SetIsContainerForFixedPositionLayers(true);
9016 LayerPositionConstraint constraint;
9017 constraint.set_is_fixed_position(true);
9018 grandchild->SetPositionConstraint(constraint);
9020 root->SetIsContainerForFixedPositionLayers(true);
9022 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9023 host->SetRootLayer(root);
9025 ExecuteCalculateDrawProperties(root.get());
9027 EXPECT_EQ(gfx::Rect(0, 0, 50, 50),
9028 grandchild->visible_rect_from_property_trees());
9031 TEST_F(LayerTreeHostCommonTest, CombineClipsUsingContentTarget) {
9032 // In the following layer tree, the layer |box|'s render target is |surface|.
9033 // |surface| also creates a transform node. We want to combine clips for |box|
9034 // in the space of its target (i.e., |surface|), not its target's target. This
9035 // test ensures that happens.
9037 gfx::Transform rotate;
9038 rotate.Rotate(5);
9039 gfx::Transform identity;
9041 scoped_refptr<Layer> root = Layer::Create();
9042 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9043 gfx::PointF(), gfx::Size(2500, 1500), true,
9044 false);
9046 scoped_refptr<Layer> frame_clip = Layer::Create();
9047 SetLayerPropertiesForTesting(frame_clip.get(), identity, gfx::Point3F(),
9048 gfx::PointF(), gfx::Size(2500, 1500), true,
9049 false);
9050 frame_clip->SetMasksToBounds(true);
9052 scoped_refptr<Layer> rotated = Layer::Create();
9053 SetLayerPropertiesForTesting(rotated.get(), rotate,
9054 gfx::Point3F(1250, 250, 0), gfx::PointF(),
9055 gfx::Size(2500, 500), true, false);
9057 scoped_refptr<Layer> surface = Layer::Create();
9058 SetLayerPropertiesForTesting(surface.get(), rotate, gfx::Point3F(),
9059 gfx::PointF(), gfx::Size(2500, 500), true,
9060 false);
9061 surface->SetOpacity(0.5);
9063 scoped_refptr<LayerWithForcedDrawsContent> container =
9064 make_scoped_refptr(new LayerWithForcedDrawsContent());
9065 SetLayerPropertiesForTesting(container.get(), identity, gfx::Point3F(),
9066 gfx::PointF(), gfx::Size(300, 300), true, false);
9068 scoped_refptr<LayerWithForcedDrawsContent> box =
9069 make_scoped_refptr(new LayerWithForcedDrawsContent());
9070 SetLayerPropertiesForTesting(box.get(), identity, gfx::Point3F(),
9071 gfx::PointF(), gfx::Size(100, 100), true, false);
9073 root->AddChild(frame_clip);
9074 frame_clip->AddChild(rotated);
9075 rotated->AddChild(surface);
9076 surface->AddChild(container);
9077 surface->AddChild(box);
9079 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9080 host->SetRootLayer(root);
9082 ExecuteCalculateDrawProperties(root.get());
9085 TEST_F(LayerTreeHostCommonTest, OnlyApplyFixedPositioningOnce) {
9086 gfx::Transform identity;
9087 gfx::Transform translate_z;
9088 translate_z.Translate3d(0, 0, 10);
9090 scoped_refptr<Layer> root = Layer::Create();
9091 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9092 gfx::PointF(), gfx::Size(800, 800), true, false);
9093 root->SetIsContainerForFixedPositionLayers(true);
9095 scoped_refptr<Layer> frame_clip = Layer::Create();
9096 SetLayerPropertiesForTesting(frame_clip.get(), translate_z, gfx::Point3F(),
9097 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9098 false);
9099 frame_clip->SetMasksToBounds(true);
9101 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9102 make_scoped_refptr(new LayerWithForcedDrawsContent());
9103 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9104 gfx::PointF(), gfx::Size(1000, 1000), true,
9105 false);
9107 LayerPositionConstraint constraint;
9108 constraint.set_is_fixed_position(true);
9109 fixed->SetPositionConstraint(constraint);
9111 root->AddChild(frame_clip);
9112 frame_clip->AddChild(fixed);
9114 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9115 host->SetRootLayer(root);
9117 ExecuteCalculateDrawProperties(root.get());
9119 gfx::Rect expected(0, 0, 100, 100);
9120 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9123 TEST_F(LayerTreeHostCommonTest,
9124 PropertyTreesAccountForScrollCompensationAdjustment) {
9125 gfx::Transform identity;
9126 gfx::Transform translate_z;
9127 translate_z.Translate3d(0, 0, 10);
9129 scoped_refptr<Layer> root = Layer::Create();
9130 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9131 gfx::PointF(), gfx::Size(800, 800), true, false);
9132 root->SetIsContainerForFixedPositionLayers(true);
9134 scoped_refptr<Layer> frame_clip = Layer::Create();
9135 SetLayerPropertiesForTesting(frame_clip.get(), translate_z, gfx::Point3F(),
9136 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9137 false);
9138 frame_clip->SetMasksToBounds(true);
9140 scoped_refptr<LayerWithForcedDrawsContent> scroller =
9141 make_scoped_refptr(new LayerWithForcedDrawsContent());
9142 SetLayerPropertiesForTesting(scroller.get(), identity, gfx::Point3F(),
9143 gfx::PointF(), gfx::Size(1000, 1000), true,
9144 false);
9146 scroller->SetScrollCompensationAdjustment(gfx::Vector2dF(0.3f, 0.7f));
9147 scroller->SetScrollOffset(gfx::ScrollOffset(0.3, 0.7));
9148 scroller->SetScrollClipLayerId(frame_clip->id());
9150 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9151 make_scoped_refptr(new LayerWithForcedDrawsContent());
9152 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9153 gfx::PointF(), gfx::Size(50, 50), true, false);
9155 LayerPositionConstraint constraint;
9156 constraint.set_is_fixed_position(true);
9157 fixed->SetPositionConstraint(constraint);
9159 scoped_refptr<LayerWithForcedDrawsContent> fixed_child =
9160 make_scoped_refptr(new LayerWithForcedDrawsContent());
9161 SetLayerPropertiesForTesting(fixed_child.get(), identity, gfx::Point3F(),
9162 gfx::PointF(), gfx::Size(10, 10), true, false);
9164 fixed_child->SetPositionConstraint(constraint);
9166 root->AddChild(frame_clip);
9167 frame_clip->AddChild(scroller);
9168 scroller->AddChild(fixed);
9169 fixed->AddChild(fixed_child);
9171 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9172 host->SetRootLayer(root);
9174 ExecuteCalculateDrawProperties(root.get());
9176 gfx::Rect expected(0, 0, 50, 50);
9177 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9179 expected = gfx::Rect(0, 0, 10, 10);
9180 EXPECT_EQ(expected, fixed_child->visible_rect_from_property_trees());
9183 TEST_F(LayerTreeHostCommonTest, FixedClipsShouldBeAssociatedWithTheRightNode) {
9184 gfx::Transform identity;
9186 scoped_refptr<Layer> root = Layer::Create();
9187 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9188 gfx::PointF(), gfx::Size(800, 800), true, false);
9189 root->SetIsContainerForFixedPositionLayers(true);
9191 scoped_refptr<Layer> frame_clip = Layer::Create();
9192 SetLayerPropertiesForTesting(frame_clip.get(), identity, gfx::Point3F(),
9193 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9194 false);
9195 frame_clip->SetMasksToBounds(true);
9197 scoped_refptr<LayerWithForcedDrawsContent> scroller =
9198 make_scoped_refptr(new LayerWithForcedDrawsContent());
9199 SetLayerPropertiesForTesting(scroller.get(), identity, gfx::Point3F(),
9200 gfx::PointF(), gfx::Size(1000, 1000), true,
9201 false);
9203 scroller->SetScrollOffset(gfx::ScrollOffset(100, 100));
9204 scroller->SetScrollClipLayerId(frame_clip->id());
9206 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9207 make_scoped_refptr(new LayerWithForcedDrawsContent());
9208 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9209 gfx::PointF(100, 100), gfx::Size(50, 50), true,
9210 false);
9212 LayerPositionConstraint constraint;
9213 constraint.set_is_fixed_position(true);
9214 fixed->SetPositionConstraint(constraint);
9215 fixed->SetForceRenderSurface(true);
9216 fixed->SetMasksToBounds(true);
9218 root->AddChild(frame_clip);
9219 frame_clip->AddChild(scroller);
9220 scroller->AddChild(fixed);
9222 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9223 host->SetRootLayer(root);
9225 ExecuteCalculateDrawProperties(root.get());
9227 gfx::Rect expected(0, 0, 50, 50);
9228 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9231 TEST_F(LayerTreeHostCommonTest, ChangingAxisAlignmentTriggersRebuild) {
9232 gfx::Transform identity;
9233 gfx::Transform translate;
9234 gfx::Transform rotate;
9236 translate.Translate(10, 10);
9237 rotate.Rotate(45);
9239 scoped_refptr<Layer> root = Layer::Create();
9240 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9241 gfx::PointF(), gfx::Size(800, 800), true, false);
9242 root->SetIsContainerForFixedPositionLayers(true);
9244 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9245 host->SetRootLayer(root);
9247 ExecuteCalculateDrawProperties(root.get());
9249 root->SetTransform(translate);
9250 EXPECT_FALSE(host->property_trees()->needs_rebuild);
9252 root->SetTransform(rotate);
9253 EXPECT_TRUE(host->property_trees()->needs_rebuild);
9256 TEST_F(LayerTreeHostCommonTest, ChangeTransformOrigin) {
9257 scoped_refptr<Layer> root = Layer::Create();
9258 scoped_refptr<LayerWithForcedDrawsContent> child =
9259 make_scoped_refptr(new LayerWithForcedDrawsContent());
9260 root->AddChild(child);
9262 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9263 host->SetRootLayer(root);
9265 gfx::Transform identity_matrix;
9266 gfx::Transform scale_matrix;
9267 scale_matrix.Scale(2.f, 2.f);
9268 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
9269 gfx::PointF(), gfx::Size(100, 100), true, false);
9270 SetLayerPropertiesForTesting(child.get(), scale_matrix, gfx::Point3F(),
9271 gfx::PointF(), gfx::Size(10, 10), true, false);
9273 ExecuteCalculateDrawProperties(root.get());
9274 EXPECT_EQ(gfx::Rect(10, 10), child->visible_rect_from_property_trees());
9276 child->SetTransformOrigin(gfx::Point3F(10.f, 10.f, 10.f));
9278 ExecuteCalculateDrawProperties(root.get());
9279 EXPECT_EQ(gfx::Rect(5, 5, 5, 5), child->visible_rect_from_property_trees());
9282 TEST_F(LayerTreeHostCommonTest, UpdateScrollChildPosition) {
9283 scoped_refptr<Layer> root = Layer::Create();
9284 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
9285 make_scoped_refptr(new LayerWithForcedDrawsContent);
9286 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
9287 make_scoped_refptr(new LayerWithForcedDrawsContent);
9289 root->AddChild(scroll_child);
9290 root->AddChild(scroll_parent);
9291 scroll_child->SetScrollParent(scroll_parent.get());
9292 scroll_parent->SetScrollClipLayerId(root->id());
9294 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9295 host->SetRootLayer(root);
9297 gfx::Transform identity_transform;
9298 gfx::Transform scale;
9299 scale.Scale(2.f, 2.f);
9300 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
9301 gfx::PointF(), gfx::Size(50, 50), true, false);
9302 SetLayerPropertiesForTesting(scroll_child.get(), scale, gfx::Point3F(),
9303 gfx::PointF(), gfx::Size(40, 40), true, false);
9304 SetLayerPropertiesForTesting(scroll_parent.get(), identity_transform,
9305 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
9306 true, false);
9308 ExecuteCalculateDrawProperties(root.get());
9309 EXPECT_EQ(gfx::Rect(25, 25),
9310 scroll_child->visible_rect_from_property_trees());
9312 scroll_child->SetPosition(gfx::PointF(0, -10.f));
9313 scroll_parent->SetScrollOffset(gfx::ScrollOffset(0.f, 10.f));
9314 ExecuteCalculateDrawProperties(root.get());
9315 EXPECT_EQ(gfx::Rect(0, 5, 25, 25),
9316 scroll_child->visible_rect_from_property_trees());
9319 static void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
9322 TEST_F(LayerTreeHostCommonTest, SkippingSubtreeMain) {
9323 gfx::Transform identity;
9324 FakeContentLayerClient client;
9325 scoped_refptr<Layer> root = Layer::Create();
9326 scoped_refptr<LayerWithForcedDrawsContent> child =
9327 make_scoped_refptr(new LayerWithForcedDrawsContent());
9328 scoped_refptr<LayerWithForcedDrawsContent> grandchild =
9329 make_scoped_refptr(new LayerWithForcedDrawsContent());
9330 scoped_refptr<FakeContentLayer> greatgrandchild(
9331 FakeContentLayer::Create(&client));
9332 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9333 gfx::PointF(), gfx::Size(100, 100), true, false);
9334 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9335 gfx::PointF(), gfx::Size(10, 10), true, false);
9336 SetLayerPropertiesForTesting(grandchild.get(), identity, gfx::Point3F(),
9337 gfx::PointF(), gfx::Size(10, 10), true, false);
9338 SetLayerPropertiesForTesting(greatgrandchild.get(), identity, gfx::Point3F(),
9339 gfx::PointF(), gfx::Size(10, 10), true, false);
9341 root->AddChild(child);
9342 child->AddChild(grandchild);
9343 grandchild->AddChild(greatgrandchild);
9345 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9346 host->SetRootLayer(root);
9348 // Check the non-skipped case.
9349 ExecuteCalculateDrawProperties(root.get());
9350 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
9352 // Now we will reset the visible rect from property trees for the grandchild,
9353 // and we will configure |child| in several ways that should force the subtree
9354 // to be skipped. The visible content rect for |grandchild| should, therefore,
9355 // remain empty.
9356 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
9357 gfx::Transform singular;
9358 singular.matrix().set(0, 0, 0);
9360 child->SetTransform(singular);
9361 ExecuteCalculateDrawProperties(root.get());
9362 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9363 child->SetTransform(identity);
9365 child->SetHideLayerAndSubtree(true);
9366 ExecuteCalculateDrawProperties(root.get());
9367 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9368 child->SetHideLayerAndSubtree(false);
9370 child->SetOpacity(0.f);
9371 ExecuteCalculateDrawProperties(root.get());
9372 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9374 // Now, even though child has zero opacity, we will configure |grandchild| and
9375 // |greatgrandchild| in several ways that should force the subtree to be
9376 // processed anyhow.
9377 grandchild->SetTouchEventHandlerRegion(Region(gfx::Rect(0, 0, 10, 10)));
9378 ExecuteCalculateDrawProperties(root.get());
9379 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
9380 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
9381 grandchild->SetTouchEventHandlerRegion(Region());
9382 ExecuteCalculateDrawProperties(root.get());
9383 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9384 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
9386 greatgrandchild->RequestCopyOfOutput(
9387 CopyOutputRequest::CreateBitmapRequest(base::Bind(&CopyOutputCallback)));
9388 ExecuteCalculateDrawProperties(root.get());
9389 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
9392 TEST_F(LayerTreeHostCommonTest, SkippingSubtreeImpl) {
9393 FakeImplProxy proxy;
9394 TestSharedBitmapManager shared_bitmap_manager;
9395 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
9397 gfx::Transform identity;
9398 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
9399 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.active_tree(), 2);
9400 scoped_ptr<LayerImpl> grandchild =
9401 LayerImpl::Create(host_impl.active_tree(), 3);
9403 scoped_ptr<FakeContentLayerImpl> greatgrandchild(
9404 FakeContentLayerImpl::Create(host_impl.active_tree(), 4));
9406 child->SetDrawsContent(true);
9407 grandchild->SetDrawsContent(true);
9408 greatgrandchild->SetDrawsContent(true);
9410 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9411 gfx::PointF(), gfx::Size(100, 100), true, false,
9412 true);
9413 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9414 gfx::PointF(), gfx::Size(10, 10), true, false,
9415 false);
9416 SetLayerPropertiesForTesting(grandchild.get(), identity, gfx::Point3F(),
9417 gfx::PointF(), gfx::Size(10, 10), true, false,
9418 false);
9419 SetLayerPropertiesForTesting(greatgrandchild.get(), identity, gfx::Point3F(),
9420 gfx::PointF(), gfx::Size(10, 10), true, false,
9421 true);
9423 LayerImpl* child_ptr = child.get();
9424 LayerImpl* grandchild_ptr = grandchild.get();
9425 LayerImpl* greatgrandchild_ptr = greatgrandchild.get();
9427 grandchild->AddChild(greatgrandchild.Pass());
9428 child->AddChild(grandchild.Pass());
9429 root->AddChild(child.Pass());
9431 // Check the non-skipped case.
9432 ExecuteCalculateDrawProperties(root.get());
9433 EXPECT_EQ(gfx::Rect(10, 10),
9434 grandchild_ptr->visible_rect_from_property_trees());
9436 // Now we will reset the visible rect from property trees for the grandchild,
9437 // and we will configure |child| in several ways that should force the subtree
9438 // to be skipped. The visible content rect for |grandchild| should, therefore,
9439 // remain empty.
9440 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
9441 gfx::Transform singular;
9442 singular.matrix().set(0, 0, 0);
9444 child_ptr->SetTransform(singular);
9445 ExecuteCalculateDrawProperties(root.get());
9446 EXPECT_EQ(gfx::Rect(0, 0),
9447 grandchild_ptr->visible_rect_from_property_trees());
9448 child_ptr->SetTransform(identity);
9450 child_ptr->SetHideLayerAndSubtree(true);
9451 ExecuteCalculateDrawProperties(root.get());
9452 EXPECT_EQ(gfx::Rect(0, 0),
9453 grandchild_ptr->visible_rect_from_property_trees());
9454 child_ptr->SetHideLayerAndSubtree(false);
9456 child_ptr->SetOpacity(0.f);
9457 ExecuteCalculateDrawProperties(root.get());
9458 EXPECT_EQ(gfx::Rect(0, 0),
9459 grandchild_ptr->visible_rect_from_property_trees());
9461 // Now, even though child has zero opacity, we will configure |grandchild| and
9462 // |greatgrandchild| in several ways that should force the subtree to be
9463 // processed anyhow.
9464 grandchild_ptr->SetTouchEventHandlerRegion(Region(gfx::Rect(0, 0, 10, 10)));
9465 ExecuteCalculateDrawProperties(root.get());
9466 EXPECT_EQ(gfx::Rect(10, 10),
9467 grandchild_ptr->visible_rect_from_property_trees());
9468 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
9469 grandchild_ptr->SetTouchEventHandlerRegion(Region());
9470 ExecuteCalculateDrawProperties(root.get());
9471 EXPECT_EQ(gfx::Rect(0, 0),
9472 grandchild_ptr->visible_rect_from_property_trees());
9473 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
9475 ScopedPtrVector<CopyOutputRequest> requests;
9476 requests.push_back(CopyOutputRequest::CreateEmptyRequest());
9478 greatgrandchild_ptr->PassCopyRequests(&requests);
9479 ExecuteCalculateDrawProperties(root.get());
9480 EXPECT_EQ(gfx::Rect(10, 10),
9481 grandchild_ptr->visible_rect_from_property_trees());
9484 TEST_F(LayerTreeHostCommonTest, SkippingLayer) {
9485 gfx::Transform identity;
9486 FakeContentLayerClient client;
9487 scoped_refptr<Layer> root = Layer::Create();
9488 scoped_refptr<LayerWithForcedDrawsContent> child =
9489 make_scoped_refptr(new LayerWithForcedDrawsContent());
9490 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9491 gfx::PointF(), gfx::Size(100, 100), true, false);
9492 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9493 gfx::PointF(), gfx::Size(10, 10), true, false);
9494 root->AddChild(child);
9496 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9497 host->SetRootLayer(root);
9499 ExecuteCalculateDrawProperties(root.get());
9500 EXPECT_EQ(gfx::Rect(10, 10), child->visible_rect_from_property_trees());
9501 child->set_visible_rect_from_property_trees(gfx::Rect());
9503 child->SetHideLayerAndSubtree(true);
9504 ExecuteCalculateDrawProperties(root.get());
9505 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9506 child->SetHideLayerAndSubtree(false);
9508 child->SetBounds(gfx::Size());
9509 ExecuteCalculateDrawProperties(root.get());
9510 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9511 child->SetBounds(gfx::Size(10, 10));
9513 gfx::Transform rotate;
9514 child->SetDoubleSided(false);
9515 rotate.RotateAboutXAxis(180.f);
9516 child->SetTransform(rotate);
9517 ExecuteCalculateDrawProperties(root.get());
9518 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9519 child->SetDoubleSided(true);
9520 child->SetTransform(identity);
9522 child->SetOpacity(0.f);
9523 ExecuteCalculateDrawProperties(root.get());
9524 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9527 TEST_F(LayerTreeHostCommonTest, LayerTreeRebuildTest) {
9528 // Ensure that the treewalk in LayerTreeHostCommom::
9529 // PreCalculateMetaInformation happens when its required.
9530 scoped_refptr<Layer> root = Layer::Create();
9531 scoped_refptr<Layer> parent = Layer::Create();
9532 scoped_refptr<Layer> child = Layer::Create();
9534 root->AddChild(parent);
9535 parent->AddChild(child);
9537 child->SetClipParent(root.get());
9539 gfx::Transform identity;
9541 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9542 gfx::PointF(), gfx::Size(100, 100), true, false);
9543 SetLayerPropertiesForTesting(parent.get(), identity, gfx::Point3F(),
9544 gfx::PointF(), gfx::Size(100, 100), true, false);
9545 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9546 gfx::PointF(), gfx::Size(100, 100), true, false);
9548 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9549 host->SetRootLayer(root);
9551 ExecuteCalculateDrawProperties(root.get());
9552 EXPECT_EQ(parent->draw_properties().num_unclipped_descendants, 1);
9554 // Ensure the dynamic update to input handlers happens.
9555 child->SetHaveWheelEventHandlers(true);
9556 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_input_handler);
9557 ExecuteCalculateDrawProperties(root.get());
9558 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_input_handler);
9560 child->SetHaveWheelEventHandlers(false);
9561 EXPECT_FALSE(root->draw_properties().layer_or_descendant_has_input_handler);
9562 ExecuteCalculateDrawProperties(root.get());
9563 EXPECT_FALSE(root->draw_properties().layer_or_descendant_has_input_handler);
9565 child->RequestCopyOfOutput(
9566 CopyOutputRequest::CreateRequest(base::Bind(&EmptyCopyOutputCallback)));
9567 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
9568 ExecuteCalculateDrawProperties(root.get());
9569 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
9572 TEST_F(LayerTreeHostCommonTest, InputHandlersRecursiveUpdateTest) {
9573 // Ensure that the treewalk in LayertreeHostCommon::
9574 // PreCalculateMetaInformation updates input handlers correctly.
9575 scoped_refptr<Layer> root = Layer::Create();
9576 scoped_refptr<Layer> child = Layer::Create();
9578 root->AddChild(child);
9580 child->SetHaveWheelEventHandlers(true);
9582 gfx::Transform identity;
9584 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9585 gfx::PointF(), gfx::Size(100, 100), true, false);
9586 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9587 gfx::PointF(), gfx::Size(100, 100), true, false);
9589 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9590 host->SetRootLayer(root);
9592 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 0);
9593 ExecuteCalculateDrawProperties(root.get());
9594 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 1);
9595 child->SetHaveWheelEventHandlers(false);
9596 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 0);
9599 } // namespace
9600 } // namespace cc