Add Apps.AppListSearchQueryLength UMA histogram.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common_unittest.cc
blobbcd437f9cbb87274e5f846e1dc7b342bf6b09f2b
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_;
5981 // Sanity check: Make sure can_use_lcd_text_ is set on each node.
5982 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5983 layers_always_allowed_lcd_text_);
5984 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5985 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5986 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5988 // Add opacity animation.
5989 child_->SetOpacity(0.9f);
5990 AddOpacityTransitionToController(
5991 child_->layer_animation_controller(), 10.0, 0.9f, 0.1f, false);
5993 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5994 layers_always_allowed_lcd_text_);
5995 // Text AA should not be adjusted while animation is active.
5996 // Make sure LCD text AA setting remains unchanged.
5997 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5998 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5999 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
6002 INSTANTIATE_TEST_CASE_P(LayerTreeHostCommonTest,
6003 LCDTextTest,
6004 testing::Combine(testing::Bool(),
6005 testing::Bool(),
6006 testing::Bool()));
6008 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_SingleLayer) {
6009 FakeImplProxy proxy;
6010 TestSharedBitmapManager shared_bitmap_manager;
6011 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6012 host_impl.CreatePendingTree();
6013 const gfx::Transform identity_matrix;
6015 scoped_refptr<Layer> root = Layer::Create();
6016 SetLayerPropertiesForTesting(root.get(),
6017 identity_matrix,
6018 gfx::Point3F(),
6019 gfx::PointF(),
6020 gfx::Size(50, 50),
6021 true,
6022 false);
6023 root->SetIsDrawable(true);
6025 scoped_refptr<Layer> child = Layer::Create();
6026 SetLayerPropertiesForTesting(child.get(),
6027 identity_matrix,
6028 gfx::Point3F(),
6029 gfx::PointF(),
6030 gfx::Size(40, 40),
6031 true,
6032 false);
6033 child->SetIsDrawable(true);
6035 scoped_refptr<Layer> grand_child = Layer::Create();
6036 SetLayerPropertiesForTesting(grand_child.get(),
6037 identity_matrix,
6038 gfx::Point3F(),
6039 gfx::PointF(),
6040 gfx::Size(30, 30),
6041 true,
6042 false);
6043 grand_child->SetIsDrawable(true);
6044 grand_child->SetHideLayerAndSubtree(true);
6046 child->AddChild(grand_child);
6047 root->AddChild(child);
6049 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6050 host->SetRootLayer(root);
6052 RenderSurfaceLayerList render_surface_layer_list;
6053 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6054 root.get(), root->bounds(), &render_surface_layer_list);
6055 inputs.can_adjust_raster_scales = true;
6056 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6058 // We should have one render surface and two layers. The grand child has
6059 // hidden itself.
6060 ASSERT_EQ(1u, render_surface_layer_list.size());
6061 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6062 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6063 EXPECT_EQ(child->id(), root->render_surface()->layer_list().at(1)->id());
6066 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_SingleLayerImpl) {
6067 FakeImplProxy proxy;
6068 TestSharedBitmapManager shared_bitmap_manager;
6069 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6070 host_impl.CreatePendingTree();
6071 const gfx::Transform identity_matrix;
6073 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
6074 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
6075 gfx::PointF(), gfx::Size(50, 50), true, false,
6076 false);
6077 root->SetDrawsContent(true);
6079 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
6080 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
6081 gfx::PointF(), gfx::Size(40, 40), true, false,
6082 false);
6083 child->SetDrawsContent(true);
6085 scoped_ptr<LayerImpl> grand_child =
6086 LayerImpl::Create(host_impl.pending_tree(), 3);
6087 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
6088 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6089 true, false, false);
6090 grand_child->SetDrawsContent(true);
6091 grand_child->SetHideLayerAndSubtree(true);
6093 child->AddChild(grand_child.Pass());
6094 root->AddChild(child.Pass());
6095 root->SetHasRenderSurface(true);
6097 LayerImplList render_surface_layer_list;
6098 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6099 root.get(), root->bounds(), &render_surface_layer_list);
6100 inputs.can_adjust_raster_scales = true;
6101 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6103 // We should have one render surface and two layers. The grand child has
6104 // hidden itself.
6105 ASSERT_EQ(1u, render_surface_layer_list.size());
6106 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6107 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id());
6108 EXPECT_EQ(2, root->render_surface()->layer_list().at(1)->id());
6111 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_TwoLayers) {
6112 FakeImplProxy proxy;
6113 TestSharedBitmapManager shared_bitmap_manager;
6114 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6115 host_impl.CreatePendingTree();
6116 const gfx::Transform identity_matrix;
6118 scoped_refptr<Layer> root = Layer::Create();
6119 SetLayerPropertiesForTesting(root.get(),
6120 identity_matrix,
6121 gfx::Point3F(),
6122 gfx::PointF(),
6123 gfx::Size(50, 50),
6124 true,
6125 false);
6126 root->SetIsDrawable(true);
6128 scoped_refptr<Layer> child = Layer::Create();
6129 SetLayerPropertiesForTesting(child.get(),
6130 identity_matrix,
6131 gfx::Point3F(),
6132 gfx::PointF(),
6133 gfx::Size(40, 40),
6134 true,
6135 false);
6136 child->SetIsDrawable(true);
6137 child->SetHideLayerAndSubtree(true);
6139 scoped_refptr<Layer> grand_child = Layer::Create();
6140 SetLayerPropertiesForTesting(grand_child.get(),
6141 identity_matrix,
6142 gfx::Point3F(),
6143 gfx::PointF(),
6144 gfx::Size(30, 30),
6145 true,
6146 false);
6147 grand_child->SetIsDrawable(true);
6149 child->AddChild(grand_child);
6150 root->AddChild(child);
6152 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6153 host->SetRootLayer(root);
6155 RenderSurfaceLayerList render_surface_layer_list;
6156 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6157 root.get(), root->bounds(), &render_surface_layer_list);
6158 inputs.can_adjust_raster_scales = true;
6159 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6161 // We should have one render surface and one layers. The child has
6162 // hidden itself and the grand child.
6163 ASSERT_EQ(1u, render_surface_layer_list.size());
6164 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6165 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6168 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_TwoLayersImpl) {
6169 FakeImplProxy proxy;
6170 TestSharedBitmapManager shared_bitmap_manager;
6171 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6172 host_impl.CreatePendingTree();
6173 const gfx::Transform identity_matrix;
6175 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
6176 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
6177 gfx::PointF(), gfx::Size(50, 50), true, false,
6178 true);
6179 root->SetDrawsContent(true);
6181 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
6182 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
6183 gfx::PointF(), gfx::Size(40, 40), true, false,
6184 false);
6185 child->SetDrawsContent(true);
6186 child->SetHideLayerAndSubtree(true);
6188 scoped_ptr<LayerImpl> grand_child =
6189 LayerImpl::Create(host_impl.pending_tree(), 3);
6190 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
6191 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6192 true, false, false);
6193 grand_child->SetDrawsContent(true);
6195 child->AddChild(grand_child.Pass());
6196 root->AddChild(child.Pass());
6198 LayerImplList render_surface_layer_list;
6199 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6200 root.get(), root->bounds(), &render_surface_layer_list);
6201 inputs.can_adjust_raster_scales = true;
6202 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6204 // We should have one render surface and one layers. The child has
6205 // hidden itself and the grand child.
6206 ASSERT_EQ(1u, render_surface_layer_list.size());
6207 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6208 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id());
6211 void EmptyCopyOutputCallback(scoped_ptr<CopyOutputResult> result) {}
6213 TEST_F(LayerTreeHostCommonTest, SubtreeHiddenWithCopyRequest) {
6214 FakeImplProxy proxy;
6215 TestSharedBitmapManager shared_bitmap_manager;
6216 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6217 host_impl.CreatePendingTree();
6218 const gfx::Transform identity_matrix;
6220 scoped_refptr<Layer> root = Layer::Create();
6221 SetLayerPropertiesForTesting(root.get(),
6222 identity_matrix,
6223 gfx::Point3F(),
6224 gfx::PointF(),
6225 gfx::Size(50, 50),
6226 true,
6227 false);
6228 root->SetIsDrawable(true);
6230 scoped_refptr<Layer> copy_grand_parent = Layer::Create();
6231 SetLayerPropertiesForTesting(copy_grand_parent.get(),
6232 identity_matrix,
6233 gfx::Point3F(),
6234 gfx::PointF(),
6235 gfx::Size(40, 40),
6236 true,
6237 false);
6238 copy_grand_parent->SetIsDrawable(true);
6240 scoped_refptr<Layer> copy_parent = Layer::Create();
6241 SetLayerPropertiesForTesting(copy_parent.get(),
6242 identity_matrix,
6243 gfx::Point3F(),
6244 gfx::PointF(),
6245 gfx::Size(30, 30),
6246 true,
6247 false);
6248 copy_parent->SetIsDrawable(true);
6249 copy_parent->SetForceRenderSurface(true);
6251 scoped_refptr<Layer> copy_layer = Layer::Create();
6252 SetLayerPropertiesForTesting(copy_layer.get(),
6253 identity_matrix,
6254 gfx::Point3F(),
6255 gfx::PointF(),
6256 gfx::Size(20, 20),
6257 true,
6258 false);
6259 copy_layer->SetIsDrawable(true);
6261 scoped_refptr<Layer> copy_child = Layer::Create();
6262 SetLayerPropertiesForTesting(copy_child.get(),
6263 identity_matrix,
6264 gfx::Point3F(),
6265 gfx::PointF(),
6266 gfx::Size(20, 20),
6267 true,
6268 false);
6269 copy_child->SetIsDrawable(true);
6271 scoped_refptr<Layer> copy_grand_parent_sibling_before = Layer::Create();
6272 SetLayerPropertiesForTesting(copy_grand_parent_sibling_before.get(),
6273 identity_matrix,
6274 gfx::Point3F(),
6275 gfx::PointF(),
6276 gfx::Size(40, 40),
6277 true,
6278 false);
6279 copy_grand_parent_sibling_before->SetIsDrawable(true);
6281 scoped_refptr<Layer> copy_grand_parent_sibling_after = Layer::Create();
6282 SetLayerPropertiesForTesting(copy_grand_parent_sibling_after.get(),
6283 identity_matrix,
6284 gfx::Point3F(),
6285 gfx::PointF(),
6286 gfx::Size(40, 40),
6287 true,
6288 false);
6289 copy_grand_parent_sibling_after->SetIsDrawable(true);
6291 copy_layer->AddChild(copy_child);
6292 copy_parent->AddChild(copy_layer);
6293 copy_grand_parent->AddChild(copy_parent);
6294 root->AddChild(copy_grand_parent_sibling_before);
6295 root->AddChild(copy_grand_parent);
6296 root->AddChild(copy_grand_parent_sibling_after);
6298 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6299 host->SetRootLayer(root);
6301 // Hide the copy_grand_parent and its subtree. But make a copy request in that
6302 // hidden subtree on copy_layer.
6303 copy_grand_parent->SetHideLayerAndSubtree(true);
6304 copy_grand_parent_sibling_before->SetHideLayerAndSubtree(true);
6305 copy_grand_parent_sibling_after->SetHideLayerAndSubtree(true);
6306 copy_layer->RequestCopyOfOutput(CopyOutputRequest::CreateRequest(
6307 base::Bind(&EmptyCopyOutputCallback)));
6308 EXPECT_TRUE(copy_layer->HasCopyRequest());
6310 RenderSurfaceLayerList render_surface_layer_list;
6311 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6312 root.get(), root->bounds(), &render_surface_layer_list);
6313 inputs.can_adjust_raster_scales = true;
6314 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6316 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
6317 EXPECT_TRUE(copy_grand_parent->draw_properties().
6318 layer_or_descendant_has_copy_request);
6319 EXPECT_TRUE(copy_parent->draw_properties().
6320 layer_or_descendant_has_copy_request);
6321 EXPECT_TRUE(copy_layer->draw_properties().
6322 layer_or_descendant_has_copy_request);
6323 EXPECT_FALSE(copy_child->draw_properties().
6324 layer_or_descendant_has_copy_request);
6325 EXPECT_FALSE(copy_grand_parent_sibling_before->draw_properties().
6326 layer_or_descendant_has_copy_request);
6327 EXPECT_FALSE(copy_grand_parent_sibling_after->draw_properties().
6328 layer_or_descendant_has_copy_request);
6330 // We should have three render surfaces, one for the root, one for the parent
6331 // since it owns a surface, and one for the copy_layer.
6332 ASSERT_EQ(3u, render_surface_layer_list.size());
6333 EXPECT_EQ(root->id(), render_surface_layer_list.at(0)->id());
6334 EXPECT_EQ(copy_parent->id(), render_surface_layer_list.at(1)->id());
6335 EXPECT_EQ(copy_layer->id(), render_surface_layer_list.at(2)->id());
6337 // The root render surface should have 2 contributing layers. The
6338 // copy_grand_parent is hidden along with its siblings, but the copy_parent
6339 // will appear since something in its subtree needs to be drawn for a copy
6340 // request.
6341 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6342 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6343 EXPECT_EQ(copy_parent->id(),
6344 root->render_surface()->layer_list().at(1)->id());
6346 // Nothing actually draws into the copy parent, so only the copy_layer will
6347 // appear in its list, since it needs to be drawn for the copy request.
6348 ASSERT_EQ(1u, copy_parent->render_surface()->layer_list().size());
6349 EXPECT_EQ(copy_layer->id(),
6350 copy_parent->render_surface()->layer_list().at(0)->id());
6352 // The copy_layer's render surface should have two contributing layers.
6353 ASSERT_EQ(2u, copy_layer->render_surface()->layer_list().size());
6354 EXPECT_EQ(copy_layer->id(),
6355 copy_layer->render_surface()->layer_list().at(0)->id());
6356 EXPECT_EQ(copy_child->id(),
6357 copy_layer->render_surface()->layer_list().at(1)->id());
6360 TEST_F(LayerTreeHostCommonTest, ClippedOutCopyRequest) {
6361 FakeImplProxy proxy;
6362 TestSharedBitmapManager shared_bitmap_manager;
6363 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6364 host_impl.CreatePendingTree();
6365 const gfx::Transform identity_matrix;
6367 scoped_refptr<Layer> root = Layer::Create();
6368 SetLayerPropertiesForTesting(root.get(),
6369 identity_matrix,
6370 gfx::Point3F(),
6371 gfx::PointF(),
6372 gfx::Size(50, 50),
6373 true,
6374 false);
6375 root->SetIsDrawable(true);
6377 scoped_refptr<Layer> copy_parent = Layer::Create();
6378 SetLayerPropertiesForTesting(copy_parent.get(),
6379 identity_matrix,
6380 gfx::Point3F(),
6381 gfx::PointF(),
6382 gfx::Size(),
6383 true,
6384 false);
6385 copy_parent->SetIsDrawable(true);
6386 copy_parent->SetMasksToBounds(true);
6388 scoped_refptr<Layer> copy_layer = Layer::Create();
6389 SetLayerPropertiesForTesting(copy_layer.get(),
6390 identity_matrix,
6391 gfx::Point3F(),
6392 gfx::PointF(),
6393 gfx::Size(30, 30),
6394 true,
6395 false);
6396 copy_layer->SetIsDrawable(true);
6398 scoped_refptr<Layer> copy_child = Layer::Create();
6399 SetLayerPropertiesForTesting(copy_child.get(),
6400 identity_matrix,
6401 gfx::Point3F(),
6402 gfx::PointF(),
6403 gfx::Size(20, 20),
6404 true,
6405 false);
6406 copy_child->SetIsDrawable(true);
6408 copy_layer->AddChild(copy_child);
6409 copy_parent->AddChild(copy_layer);
6410 root->AddChild(copy_parent);
6412 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6413 host->SetRootLayer(root);
6415 copy_layer->RequestCopyOfOutput(CopyOutputRequest::CreateRequest(
6416 base::Bind(&EmptyCopyOutputCallback)));
6417 EXPECT_TRUE(copy_layer->HasCopyRequest());
6419 RenderSurfaceLayerList render_surface_layer_list;
6420 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6421 root.get(), root->bounds(), &render_surface_layer_list);
6422 inputs.can_adjust_raster_scales = true;
6423 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6425 // We should have one render surface, as the others are clipped out.
6426 ASSERT_EQ(1u, render_surface_layer_list.size());
6427 EXPECT_EQ(root->id(), render_surface_layer_list.at(0)->id());
6429 // The root render surface should only have 1 contributing layer, since the
6430 // other layers are empty/clipped away.
6431 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6432 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6435 TEST_F(LayerTreeHostCommonTest, VisibleContentRectInsideSurface) {
6436 FakeImplProxy proxy;
6437 TestSharedBitmapManager shared_bitmap_manager;
6438 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6439 host_impl.CreatePendingTree();
6440 const gfx::Transform identity_matrix;
6442 scoped_refptr<Layer> root = Layer::Create();
6443 SetLayerPropertiesForTesting(root.get(),
6444 identity_matrix,
6445 gfx::Point3F(),
6446 gfx::PointF(),
6447 gfx::Size(50, 50),
6448 true,
6449 false);
6450 root->SetIsDrawable(true);
6452 // The surface is moved slightly outside of the viewport.
6453 scoped_refptr<Layer> surface = Layer::Create();
6454 SetLayerPropertiesForTesting(surface.get(),
6455 identity_matrix,
6456 gfx::Point3F(),
6457 gfx::PointF(-10, -20),
6458 gfx::Size(),
6459 true,
6460 false);
6461 surface->SetForceRenderSurface(true);
6463 scoped_refptr<Layer> surface_child = Layer::Create();
6464 SetLayerPropertiesForTesting(surface_child.get(),
6465 identity_matrix,
6466 gfx::Point3F(),
6467 gfx::PointF(),
6468 gfx::Size(50, 50),
6469 true,
6470 false);
6471 surface_child->SetIsDrawable(true);
6473 surface->AddChild(surface_child);
6474 root->AddChild(surface);
6476 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6477 host->SetRootLayer(root);
6479 RenderSurfaceLayerList render_surface_layer_list;
6480 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6481 root.get(), root->bounds(), &render_surface_layer_list);
6482 inputs.can_adjust_raster_scales = true;
6483 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6485 // The visible_content_rect for the |surface_child| should not be clipped by
6486 // the viewport.
6487 EXPECT_EQ(gfx::Rect(50, 50).ToString(),
6488 surface_child->visible_content_rect().ToString());
6491 TEST_F(LayerTreeHostCommonTest, TransformedClipParent) {
6492 // Ensure that a transform between the layer and its render surface is not a
6493 // problem. Constructs the following layer tree.
6495 // root (a render surface)
6496 // + render_surface
6497 // + clip_parent (scaled)
6498 // + intervening_clipping_layer
6499 // + clip_child
6501 // The render surface should be resized correctly and the clip child should
6502 // inherit the right clip rect.
6503 scoped_refptr<Layer> root = Layer::Create();
6504 scoped_refptr<Layer> render_surface = Layer::Create();
6505 scoped_refptr<Layer> clip_parent = Layer::Create();
6506 scoped_refptr<Layer> intervening = Layer::Create();
6507 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6508 make_scoped_refptr(new LayerWithForcedDrawsContent);
6510 root->AddChild(render_surface);
6511 render_surface->AddChild(clip_parent);
6512 clip_parent->AddChild(intervening);
6513 intervening->AddChild(clip_child);
6515 clip_child->SetClipParent(clip_parent.get());
6517 intervening->SetMasksToBounds(true);
6518 clip_parent->SetMasksToBounds(true);
6520 render_surface->SetForceRenderSurface(true);
6522 gfx::Transform scale_transform;
6523 scale_transform.Scale(2, 2);
6525 gfx::Transform identity_transform;
6527 SetLayerPropertiesForTesting(root.get(),
6528 identity_transform,
6529 gfx::Point3F(),
6530 gfx::PointF(),
6531 gfx::Size(50, 50),
6532 true,
6533 false);
6534 SetLayerPropertiesForTesting(render_surface.get(),
6535 identity_transform,
6536 gfx::Point3F(),
6537 gfx::PointF(),
6538 gfx::Size(10, 10),
6539 true,
6540 false);
6541 SetLayerPropertiesForTesting(clip_parent.get(),
6542 scale_transform,
6543 gfx::Point3F(),
6544 gfx::PointF(1.f, 1.f),
6545 gfx::Size(10, 10),
6546 true,
6547 false);
6548 SetLayerPropertiesForTesting(intervening.get(),
6549 identity_transform,
6550 gfx::Point3F(),
6551 gfx::PointF(1.f, 1.f),
6552 gfx::Size(5, 5),
6553 true,
6554 false);
6555 SetLayerPropertiesForTesting(clip_child.get(),
6556 identity_transform,
6557 gfx::Point3F(),
6558 gfx::PointF(1.f, 1.f),
6559 gfx::Size(10, 10),
6560 true,
6561 false);
6563 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6564 host->SetRootLayer(root);
6566 ExecuteCalculateDrawProperties(root.get());
6568 ASSERT_TRUE(root->render_surface());
6569 ASSERT_TRUE(render_surface->render_surface());
6571 // Ensure that we've inherited our clip parent's clip and weren't affected
6572 // by the intervening clip layer.
6573 ASSERT_EQ(gfx::Rect(1, 1, 20, 20).ToString(),
6574 clip_parent->clip_rect().ToString());
6575 ASSERT_EQ(clip_parent->clip_rect().ToString(),
6576 clip_child->clip_rect().ToString());
6577 ASSERT_EQ(gfx::Rect(3, 3, 10, 10).ToString(),
6578 intervening->clip_rect().ToString());
6580 // Ensure that the render surface reports a content rect that has been grown
6581 // to accomodate for the clip child.
6582 ASSERT_EQ(gfx::Rect(5, 5, 16, 16).ToString(),
6583 render_surface->render_surface()->content_rect().ToString());
6585 // The above check implies the two below, but they nicely demonstrate that
6586 // we've grown, despite the intervening layer's clip.
6587 ASSERT_TRUE(clip_parent->clip_rect().Contains(
6588 render_surface->render_surface()->content_rect()));
6589 ASSERT_FALSE(intervening->clip_rect().Contains(
6590 render_surface->render_surface()->content_rect()));
6593 TEST_F(LayerTreeHostCommonTest, ClipParentWithInterveningRenderSurface) {
6594 // Ensure that intervening render surfaces are not a problem in the basic
6595 // case. In the following tree, both render surfaces should be resized to
6596 // accomodate for the clip child, despite an intervening clip.
6598 // root (a render surface)
6599 // + clip_parent (masks to bounds)
6600 // + render_surface1 (sets opacity)
6601 // + intervening (masks to bounds)
6602 // + render_surface2 (also sets opacity)
6603 // + clip_child
6605 scoped_refptr<Layer> root = Layer::Create();
6606 scoped_refptr<Layer> clip_parent = Layer::Create();
6607 scoped_refptr<Layer> render_surface1 = Layer::Create();
6608 scoped_refptr<Layer> intervening = Layer::Create();
6609 scoped_refptr<Layer> render_surface2 = Layer::Create();
6610 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6611 make_scoped_refptr(new LayerWithForcedDrawsContent);
6613 root->AddChild(clip_parent);
6614 clip_parent->AddChild(render_surface1);
6615 render_surface1->AddChild(intervening);
6616 intervening->AddChild(render_surface2);
6617 render_surface2->AddChild(clip_child);
6619 clip_child->SetClipParent(clip_parent.get());
6621 intervening->SetMasksToBounds(true);
6622 clip_parent->SetMasksToBounds(true);
6624 render_surface1->SetForceRenderSurface(true);
6625 render_surface2->SetForceRenderSurface(true);
6627 gfx::Transform translation_transform;
6628 translation_transform.Translate(2, 2);
6630 gfx::Transform identity_transform;
6631 SetLayerPropertiesForTesting(root.get(),
6632 identity_transform,
6633 gfx::Point3F(),
6634 gfx::PointF(),
6635 gfx::Size(50, 50),
6636 true,
6637 false);
6638 SetLayerPropertiesForTesting(clip_parent.get(),
6639 translation_transform,
6640 gfx::Point3F(),
6641 gfx::PointF(1.f, 1.f),
6642 gfx::Size(40, 40),
6643 true,
6644 false);
6645 SetLayerPropertiesForTesting(render_surface1.get(),
6646 identity_transform,
6647 gfx::Point3F(),
6648 gfx::PointF(),
6649 gfx::Size(10, 10),
6650 true,
6651 false);
6652 SetLayerPropertiesForTesting(intervening.get(),
6653 identity_transform,
6654 gfx::Point3F(),
6655 gfx::PointF(1.f, 1.f),
6656 gfx::Size(5, 5),
6657 true,
6658 false);
6659 SetLayerPropertiesForTesting(render_surface2.get(),
6660 identity_transform,
6661 gfx::Point3F(),
6662 gfx::PointF(),
6663 gfx::Size(10, 10),
6664 true,
6665 false);
6666 SetLayerPropertiesForTesting(clip_child.get(),
6667 identity_transform,
6668 gfx::Point3F(),
6669 gfx::PointF(-10.f, -10.f),
6670 gfx::Size(60, 60),
6671 true,
6672 false);
6674 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6675 host->SetRootLayer(root);
6677 ExecuteCalculateDrawProperties(root.get());
6679 EXPECT_TRUE(root->render_surface());
6680 EXPECT_TRUE(render_surface1->render_surface());
6681 EXPECT_TRUE(render_surface2->render_surface());
6683 // Since the render surfaces could have expanded, they should not clip (their
6684 // bounds would no longer be reliable). We should resort to layer clipping
6685 // in this case.
6686 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6687 render_surface1->render_surface()->clip_rect().ToString());
6688 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
6689 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6690 render_surface2->render_surface()->clip_rect().ToString());
6691 EXPECT_FALSE(render_surface2->render_surface()->is_clipped());
6693 // NB: clip rects are in target space.
6694 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6695 render_surface1->clip_rect().ToString());
6696 EXPECT_TRUE(render_surface1->is_clipped());
6698 // This value is inherited from the clipping ancestor layer, 'intervening'.
6699 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
6700 render_surface2->clip_rect().ToString());
6701 EXPECT_TRUE(render_surface2->is_clipped());
6703 // The content rects of both render surfaces should both have expanded to
6704 // contain the clip child.
6705 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6706 render_surface1->render_surface()->content_rect().ToString());
6707 EXPECT_EQ(gfx::Rect(-1, -1, 40, 40).ToString(),
6708 render_surface2->render_surface()->content_rect().ToString());
6710 // The clip child should have inherited the clip parent's clip (projected to
6711 // the right space, of course), and should have the correctly sized visible
6712 // content rect.
6713 EXPECT_EQ(gfx::Rect(-1, -1, 40, 40).ToString(),
6714 clip_child->clip_rect().ToString());
6715 EXPECT_EQ(gfx::Rect(9, 9, 40, 40).ToString(),
6716 clip_child->visible_content_rect().ToString());
6717 EXPECT_TRUE(clip_child->is_clipped());
6720 TEST_F(LayerTreeHostCommonTest, ClipParentScrolledInterveningLayer) {
6721 // Ensure that intervening render surfaces are not a problem, even if there
6722 // is a scroll involved. Note, we do _not_ have to consider any other sort
6723 // of transform.
6725 // root (a render surface)
6726 // + clip_parent (masks to bounds)
6727 // + render_surface1 (sets opacity)
6728 // + intervening (masks to bounds AND scrolls)
6729 // + render_surface2 (also sets opacity)
6730 // + clip_child
6732 scoped_refptr<Layer> root = Layer::Create();
6733 scoped_refptr<Layer> clip_parent = Layer::Create();
6734 scoped_refptr<Layer> render_surface1 = Layer::Create();
6735 scoped_refptr<Layer> intervening = Layer::Create();
6736 scoped_refptr<Layer> render_surface2 = Layer::Create();
6737 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6738 make_scoped_refptr(new LayerWithForcedDrawsContent);
6740 root->AddChild(clip_parent);
6741 clip_parent->AddChild(render_surface1);
6742 render_surface1->AddChild(intervening);
6743 intervening->AddChild(render_surface2);
6744 render_surface2->AddChild(clip_child);
6746 clip_child->SetClipParent(clip_parent.get());
6748 intervening->SetMasksToBounds(true);
6749 clip_parent->SetMasksToBounds(true);
6750 intervening->SetScrollClipLayerId(clip_parent->id());
6751 intervening->SetScrollOffset(gfx::ScrollOffset(3, 3));
6753 render_surface1->SetForceRenderSurface(true);
6754 render_surface2->SetForceRenderSurface(true);
6756 gfx::Transform translation_transform;
6757 translation_transform.Translate(2, 2);
6759 gfx::Transform identity_transform;
6760 SetLayerPropertiesForTesting(root.get(),
6761 identity_transform,
6762 gfx::Point3F(),
6763 gfx::PointF(),
6764 gfx::Size(50, 50),
6765 true,
6766 false);
6767 SetLayerPropertiesForTesting(clip_parent.get(),
6768 translation_transform,
6769 gfx::Point3F(),
6770 gfx::PointF(1.f, 1.f),
6771 gfx::Size(40, 40),
6772 true,
6773 false);
6774 SetLayerPropertiesForTesting(render_surface1.get(),
6775 identity_transform,
6776 gfx::Point3F(),
6777 gfx::PointF(),
6778 gfx::Size(10, 10),
6779 true,
6780 false);
6781 SetLayerPropertiesForTesting(intervening.get(),
6782 identity_transform,
6783 gfx::Point3F(),
6784 gfx::PointF(1.f, 1.f),
6785 gfx::Size(5, 5),
6786 true,
6787 false);
6788 SetLayerPropertiesForTesting(render_surface2.get(),
6789 identity_transform,
6790 gfx::Point3F(),
6791 gfx::PointF(),
6792 gfx::Size(10, 10),
6793 true,
6794 false);
6795 SetLayerPropertiesForTesting(clip_child.get(),
6796 identity_transform,
6797 gfx::Point3F(),
6798 gfx::PointF(-10.f, -10.f),
6799 gfx::Size(60, 60),
6800 true,
6801 false);
6803 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6804 host->SetRootLayer(root);
6806 ExecuteCalculateDrawProperties(root.get());
6808 EXPECT_TRUE(root->render_surface());
6809 EXPECT_TRUE(render_surface1->render_surface());
6810 EXPECT_TRUE(render_surface2->render_surface());
6812 // Since the render surfaces could have expanded, they should not clip (their
6813 // bounds would no longer be reliable). We should resort to layer clipping
6814 // in this case.
6815 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6816 render_surface1->render_surface()->clip_rect().ToString());
6817 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
6818 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6819 render_surface2->render_surface()->clip_rect().ToString());
6820 EXPECT_FALSE(render_surface2->render_surface()->is_clipped());
6822 // NB: clip rects are in target space.
6823 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6824 render_surface1->clip_rect().ToString());
6825 EXPECT_TRUE(render_surface1->is_clipped());
6827 // This value is inherited from the clipping ancestor layer, 'intervening'.
6828 EXPECT_EQ(gfx::Rect(2, 2, 3, 3).ToString(),
6829 render_surface2->clip_rect().ToString());
6830 EXPECT_TRUE(render_surface2->is_clipped());
6832 // The content rects of both render surfaces should both have expanded to
6833 // contain the clip child.
6834 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6835 render_surface1->render_surface()->content_rect().ToString());
6836 EXPECT_EQ(gfx::Rect(2, 2, 40, 40).ToString(),
6837 render_surface2->render_surface()->content_rect().ToString());
6839 // The clip child should have inherited the clip parent's clip (projected to
6840 // the right space, of course), and should have the correctly sized visible
6841 // content rect.
6842 EXPECT_EQ(gfx::Rect(2, 2, 40, 40).ToString(),
6843 clip_child->clip_rect().ToString());
6844 EXPECT_EQ(gfx::Rect(12, 12, 40, 40).ToString(),
6845 clip_child->visible_content_rect().ToString());
6846 EXPECT_TRUE(clip_child->is_clipped());
6849 TEST_F(LayerTreeHostCommonTest, DescendantsOfClipChildren) {
6850 // Ensures that descendants of the clip child inherit the correct clip.
6852 // root (a render surface)
6853 // + clip_parent (masks to bounds)
6854 // + intervening (masks to bounds)
6855 // + clip_child
6856 // + child
6858 scoped_refptr<Layer> root = Layer::Create();
6859 scoped_refptr<Layer> clip_parent = Layer::Create();
6860 scoped_refptr<Layer> intervening = Layer::Create();
6861 scoped_refptr<Layer> clip_child = Layer::Create();
6862 scoped_refptr<LayerWithForcedDrawsContent> child =
6863 make_scoped_refptr(new LayerWithForcedDrawsContent);
6865 root->AddChild(clip_parent);
6866 clip_parent->AddChild(intervening);
6867 intervening->AddChild(clip_child);
6868 clip_child->AddChild(child);
6870 clip_child->SetClipParent(clip_parent.get());
6872 intervening->SetMasksToBounds(true);
6873 clip_parent->SetMasksToBounds(true);
6875 gfx::Transform identity_transform;
6876 SetLayerPropertiesForTesting(root.get(),
6877 identity_transform,
6878 gfx::Point3F(),
6879 gfx::PointF(),
6880 gfx::Size(50, 50),
6881 true,
6882 false);
6883 SetLayerPropertiesForTesting(clip_parent.get(),
6884 identity_transform,
6885 gfx::Point3F(),
6886 gfx::PointF(),
6887 gfx::Size(40, 40),
6888 true,
6889 false);
6890 SetLayerPropertiesForTesting(intervening.get(),
6891 identity_transform,
6892 gfx::Point3F(),
6893 gfx::PointF(),
6894 gfx::Size(5, 5),
6895 true,
6896 false);
6897 SetLayerPropertiesForTesting(clip_child.get(),
6898 identity_transform,
6899 gfx::Point3F(),
6900 gfx::PointF(),
6901 gfx::Size(60, 60),
6902 true,
6903 false);
6904 SetLayerPropertiesForTesting(child.get(),
6905 identity_transform,
6906 gfx::Point3F(),
6907 gfx::PointF(),
6908 gfx::Size(60, 60),
6909 true,
6910 false);
6912 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6913 host->SetRootLayer(root);
6915 ExecuteCalculateDrawProperties(root.get());
6917 EXPECT_TRUE(root->render_surface());
6919 // Neither the clip child nor its descendant should have inherited the clip
6920 // from |intervening|.
6921 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6922 clip_child->clip_rect().ToString());
6923 EXPECT_TRUE(clip_child->is_clipped());
6924 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6925 child->visible_content_rect().ToString());
6926 EXPECT_TRUE(child->is_clipped());
6929 TEST_F(LayerTreeHostCommonTest,
6930 SurfacesShouldBeUnaffectedByNonDescendantClipChildren) {
6931 // Ensures that non-descendant clip children in the tree do not affect
6932 // render surfaces.
6934 // root (a render surface)
6935 // + clip_parent (masks to bounds)
6936 // + render_surface1
6937 // + clip_child
6938 // + render_surface2
6939 // + non_clip_child
6941 // In this example render_surface2 should be unaffected by clip_child.
6942 scoped_refptr<Layer> root = Layer::Create();
6943 scoped_refptr<Layer> clip_parent = Layer::Create();
6944 scoped_refptr<Layer> render_surface1 = Layer::Create();
6945 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6946 make_scoped_refptr(new LayerWithForcedDrawsContent);
6947 scoped_refptr<Layer> render_surface2 = Layer::Create();
6948 scoped_refptr<LayerWithForcedDrawsContent> non_clip_child =
6949 make_scoped_refptr(new LayerWithForcedDrawsContent);
6951 root->AddChild(clip_parent);
6952 clip_parent->AddChild(render_surface1);
6953 render_surface1->AddChild(clip_child);
6954 clip_parent->AddChild(render_surface2);
6955 render_surface2->AddChild(non_clip_child);
6957 clip_child->SetClipParent(clip_parent.get());
6959 clip_parent->SetMasksToBounds(true);
6960 render_surface1->SetMasksToBounds(true);
6962 gfx::Transform identity_transform;
6963 SetLayerPropertiesForTesting(root.get(),
6964 identity_transform,
6965 gfx::Point3F(),
6966 gfx::PointF(),
6967 gfx::Size(15, 15),
6968 true,
6969 false);
6970 SetLayerPropertiesForTesting(clip_parent.get(),
6971 identity_transform,
6972 gfx::Point3F(),
6973 gfx::PointF(),
6974 gfx::Size(10, 10),
6975 true,
6976 false);
6977 SetLayerPropertiesForTesting(render_surface1.get(),
6978 identity_transform,
6979 gfx::Point3F(),
6980 gfx::PointF(5, 5),
6981 gfx::Size(5, 5),
6982 true,
6983 false);
6984 SetLayerPropertiesForTesting(render_surface2.get(),
6985 identity_transform,
6986 gfx::Point3F(),
6987 gfx::PointF(),
6988 gfx::Size(5, 5),
6989 true,
6990 false);
6991 SetLayerPropertiesForTesting(clip_child.get(),
6992 identity_transform,
6993 gfx::Point3F(),
6994 gfx::PointF(-1, 1),
6995 gfx::Size(10, 10),
6996 true,
6997 false);
6998 SetLayerPropertiesForTesting(non_clip_child.get(),
6999 identity_transform,
7000 gfx::Point3F(),
7001 gfx::PointF(),
7002 gfx::Size(5, 5),
7003 true,
7004 false);
7006 render_surface1->SetForceRenderSurface(true);
7007 render_surface2->SetForceRenderSurface(true);
7009 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7010 host->SetRootLayer(root);
7012 ExecuteCalculateDrawProperties(root.get());
7014 EXPECT_TRUE(root->render_surface());
7015 EXPECT_TRUE(render_surface1->render_surface());
7016 EXPECT_TRUE(render_surface2->render_surface());
7018 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7019 render_surface1->clip_rect().ToString());
7020 EXPECT_TRUE(render_surface1->is_clipped());
7022 // The render surface should not clip (it has unclipped descendants), instead
7023 // it should rely on layer clipping.
7024 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
7025 render_surface1->render_surface()->clip_rect().ToString());
7026 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
7028 // That said, it should have grown to accomodate the unclipped descendant.
7029 EXPECT_EQ(gfx::Rect(-1, 1, 6, 4).ToString(),
7030 render_surface1->render_surface()->content_rect().ToString());
7032 // This render surface should clip. It has no unclipped descendants.
7033 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7034 render_surface2->clip_rect().ToString());
7035 EXPECT_TRUE(render_surface2->render_surface()->is_clipped());
7037 // It also shouldn't have grown to accomodate the clip child.
7038 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7039 render_surface2->render_surface()->content_rect().ToString());
7041 // Sanity check our num_unclipped_descendants values.
7042 EXPECT_EQ(1, render_surface1->num_unclipped_descendants());
7043 EXPECT_EQ(0, render_surface2->num_unclipped_descendants());
7046 TEST_F(LayerTreeHostCommonTest, CanRenderToSeparateSurface) {
7047 FakeImplProxy proxy;
7048 TestSharedBitmapManager shared_bitmap_manager;
7049 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7050 scoped_ptr<LayerImpl> root =
7051 LayerImpl::Create(host_impl.active_tree(), 12345);
7052 scoped_ptr<LayerImpl> child1 =
7053 LayerImpl::Create(host_impl.active_tree(), 123456);
7054 scoped_ptr<LayerImpl> child2 =
7055 LayerImpl::Create(host_impl.active_tree(), 1234567);
7056 scoped_ptr<LayerImpl> child3 =
7057 LayerImpl::Create(host_impl.active_tree(), 12345678);
7059 gfx::Transform identity_matrix;
7060 gfx::Point3F transform_origin;
7061 gfx::PointF position;
7062 gfx::Size bounds(100, 100);
7063 SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
7064 position, bounds, true, false, true);
7065 root->SetDrawsContent(true);
7067 // This layer structure normally forces render surface due to preserves3d
7068 // behavior.
7069 SetLayerPropertiesForTesting(child1.get(), identity_matrix, transform_origin,
7070 position, bounds, false, true, true);
7071 child1->SetDrawsContent(true);
7072 SetLayerPropertiesForTesting(child2.get(), identity_matrix, transform_origin,
7073 position, bounds, true, false, false);
7074 child2->SetDrawsContent(true);
7075 SetLayerPropertiesForTesting(child3.get(), identity_matrix, transform_origin,
7076 position, bounds, true, false, false);
7077 child3->SetDrawsContent(true);
7079 child2->Set3dSortingContextId(1);
7080 child3->Set3dSortingContextId(1);
7082 child2->AddChild(child3.Pass());
7083 child1->AddChild(child2.Pass());
7084 root->AddChild(child1.Pass());
7087 LayerImplList render_surface_layer_list;
7088 FakeLayerTreeHostImpl::RecursiveUpdateNumChildren(root.get());
7089 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7090 root.get(), root->bounds(), &render_surface_layer_list);
7091 inputs.can_render_to_separate_surface = true;
7092 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7094 EXPECT_EQ(2u, render_surface_layer_list.size());
7096 int count_represents_target_render_surface = 0;
7097 int count_represents_contributing_render_surface = 0;
7098 int count_represents_itself = 0;
7099 auto end = LayerIterator<LayerImpl>::End(&render_surface_layer_list);
7100 for (auto it = LayerIterator<LayerImpl>::Begin(&render_surface_layer_list);
7101 it != end; ++it) {
7102 if (it.represents_target_render_surface())
7103 count_represents_target_render_surface++;
7104 if (it.represents_contributing_render_surface())
7105 count_represents_contributing_render_surface++;
7106 if (it.represents_itself())
7107 count_represents_itself++;
7110 // Two render surfaces.
7111 EXPECT_EQ(2, count_represents_target_render_surface);
7112 // Second render surface contributes to root render surface.
7113 EXPECT_EQ(1, count_represents_contributing_render_surface);
7114 // All 4 layers represent itself.
7115 EXPECT_EQ(4, count_represents_itself);
7119 LayerImplList render_surface_layer_list;
7120 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7121 root.get(), root->bounds(), &render_surface_layer_list);
7122 inputs.can_render_to_separate_surface = false;
7123 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7125 EXPECT_EQ(1u, render_surface_layer_list.size());
7127 int count_represents_target_render_surface = 0;
7128 int count_represents_contributing_render_surface = 0;
7129 int count_represents_itself = 0;
7130 auto end = LayerIterator<LayerImpl>::End(&render_surface_layer_list);
7131 for (auto it = LayerIterator<LayerImpl>::Begin(&render_surface_layer_list);
7132 it != end; ++it) {
7133 if (it.represents_target_render_surface())
7134 count_represents_target_render_surface++;
7135 if (it.represents_contributing_render_surface())
7136 count_represents_contributing_render_surface++;
7137 if (it.represents_itself())
7138 count_represents_itself++;
7141 // Only root layer has a render surface.
7142 EXPECT_EQ(1, count_represents_target_render_surface);
7143 // No layer contributes a render surface to root render surface.
7144 EXPECT_EQ(0, count_represents_contributing_render_surface);
7145 // All 4 layers represent itself.
7146 EXPECT_EQ(4, count_represents_itself);
7150 TEST_F(LayerTreeHostCommonTest, DoNotIncludeBackfaceInvisibleSurfaces) {
7151 scoped_refptr<Layer> root = Layer::Create();
7152 scoped_refptr<Layer> render_surface = Layer::Create();
7153 scoped_refptr<LayerWithForcedDrawsContent> child =
7154 make_scoped_refptr(new LayerWithForcedDrawsContent);
7156 root->AddChild(render_surface);
7157 render_surface->AddChild(child);
7159 gfx::Transform identity_transform;
7160 SetLayerPropertiesForTesting(root.get(),
7161 identity_transform,
7162 gfx::Point3F(),
7163 gfx::PointF(),
7164 gfx::Size(50, 50),
7165 true,
7166 false);
7167 SetLayerPropertiesForTesting(render_surface.get(),
7168 identity_transform,
7169 gfx::Point3F(),
7170 gfx::PointF(),
7171 gfx::Size(30, 30),
7172 false,
7173 true);
7174 SetLayerPropertiesForTesting(child.get(),
7175 identity_transform,
7176 gfx::Point3F(),
7177 gfx::PointF(),
7178 gfx::Size(20, 20),
7179 true,
7180 false);
7182 root->SetShouldFlattenTransform(false);
7183 root->Set3dSortingContextId(1);
7184 render_surface->SetDoubleSided(false);
7185 render_surface->SetForceRenderSurface(true);
7187 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7188 host->SetRootLayer(root);
7190 ExecuteCalculateDrawProperties(root.get());
7192 EXPECT_EQ(2u, render_surface_layer_list()->size());
7193 EXPECT_EQ(1u,
7194 render_surface_layer_list()->at(0)
7195 ->render_surface()->layer_list().size());
7196 EXPECT_EQ(1u,
7197 render_surface_layer_list()->at(1)
7198 ->render_surface()->layer_list().size());
7200 gfx::Transform rotation_transform = identity_transform;
7201 rotation_transform.RotateAboutXAxis(180.0);
7203 render_surface->SetTransform(rotation_transform);
7205 ExecuteCalculateDrawProperties(root.get());
7207 EXPECT_EQ(1u, render_surface_layer_list()->size());
7208 EXPECT_EQ(0u,
7209 render_surface_layer_list()->at(0)
7210 ->render_surface()->layer_list().size());
7213 TEST_F(LayerTreeHostCommonTest, ClippedByScrollParent) {
7214 // Checks that the simple case (being clipped by a scroll parent that would
7215 // have been processed before you anyhow) results in the right clips.
7217 // + root
7218 // + scroll_parent_border
7219 // | + scroll_parent_clip
7220 // | + scroll_parent
7221 // + scroll_child
7223 scoped_refptr<Layer> root = Layer::Create();
7224 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7225 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7226 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7227 make_scoped_refptr(new LayerWithForcedDrawsContent);
7228 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7229 make_scoped_refptr(new LayerWithForcedDrawsContent);
7231 root->AddChild(scroll_child);
7233 root->AddChild(scroll_parent_border);
7234 scroll_parent_border->AddChild(scroll_parent_clip);
7235 scroll_parent_clip->AddChild(scroll_parent);
7237 scroll_parent_clip->SetMasksToBounds(true);
7239 scroll_child->SetScrollParent(scroll_parent.get());
7241 gfx::Transform identity_transform;
7242 SetLayerPropertiesForTesting(root.get(),
7243 identity_transform,
7244 gfx::Point3F(),
7245 gfx::PointF(),
7246 gfx::Size(50, 50),
7247 true,
7248 false);
7249 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7250 identity_transform,
7251 gfx::Point3F(),
7252 gfx::PointF(),
7253 gfx::Size(40, 40),
7254 true,
7255 false);
7256 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7257 identity_transform,
7258 gfx::Point3F(),
7259 gfx::PointF(),
7260 gfx::Size(30, 30),
7261 true,
7262 false);
7263 SetLayerPropertiesForTesting(scroll_parent.get(),
7264 identity_transform,
7265 gfx::Point3F(),
7266 gfx::PointF(),
7267 gfx::Size(50, 50),
7268 true,
7269 false);
7270 SetLayerPropertiesForTesting(scroll_child.get(),
7271 identity_transform,
7272 gfx::Point3F(),
7273 gfx::PointF(),
7274 gfx::Size(50, 50),
7275 true,
7276 false);
7278 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7279 host->SetRootLayer(root);
7281 ExecuteCalculateDrawProperties(root.get());
7283 EXPECT_TRUE(root->render_surface());
7285 EXPECT_EQ(gfx::Rect(0, 0, 30, 30).ToString(),
7286 scroll_child->clip_rect().ToString());
7287 EXPECT_TRUE(scroll_child->is_clipped());
7290 TEST_F(LayerTreeHostCommonTest, SingularTransformSubtreesDoNotDraw) {
7291 scoped_refptr<LayerWithForcedDrawsContent> root =
7292 make_scoped_refptr(new LayerWithForcedDrawsContent);
7293 scoped_refptr<LayerWithForcedDrawsContent> parent =
7294 make_scoped_refptr(new LayerWithForcedDrawsContent);
7295 scoped_refptr<LayerWithForcedDrawsContent> child =
7296 make_scoped_refptr(new LayerWithForcedDrawsContent);
7298 root->AddChild(parent);
7299 parent->AddChild(child);
7301 gfx::Transform identity_transform;
7302 SetLayerPropertiesForTesting(root.get(),
7303 identity_transform,
7304 gfx::Point3F(),
7305 gfx::PointF(),
7306 gfx::Size(50, 50),
7307 true,
7308 true);
7309 root->SetForceRenderSurface(true);
7310 SetLayerPropertiesForTesting(parent.get(),
7311 identity_transform,
7312 gfx::Point3F(),
7313 gfx::PointF(),
7314 gfx::Size(30, 30),
7315 true,
7316 true);
7317 parent->SetForceRenderSurface(true);
7318 SetLayerPropertiesForTesting(child.get(),
7319 identity_transform,
7320 gfx::Point3F(),
7321 gfx::PointF(),
7322 gfx::Size(20, 20),
7323 true,
7324 true);
7325 child->SetForceRenderSurface(true);
7327 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7328 host->SetRootLayer(root);
7330 ExecuteCalculateDrawProperties(root.get());
7332 EXPECT_EQ(3u, render_surface_layer_list()->size());
7334 gfx::Transform singular_transform;
7335 singular_transform.Scale3d(
7336 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
7338 child->SetTransform(singular_transform);
7340 ExecuteCalculateDrawProperties(root.get());
7342 EXPECT_EQ(2u, render_surface_layer_list()->size());
7344 // Ensure that the entire subtree under a layer with singular transform does
7345 // not get rendered.
7346 parent->SetTransform(singular_transform);
7347 child->SetTransform(identity_transform);
7349 ExecuteCalculateDrawProperties(root.get());
7351 EXPECT_EQ(1u, render_surface_layer_list()->size());
7354 TEST_F(LayerTreeHostCommonTest, ClippedByOutOfOrderScrollParent) {
7355 // Checks that clipping by a scroll parent that follows you in paint order
7356 // still results in correct clipping.
7358 // + root
7359 // + scroll_child
7360 // + scroll_parent_border
7361 // + scroll_parent_clip
7362 // + scroll_parent
7364 scoped_refptr<Layer> root = Layer::Create();
7365 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7366 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7367 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7368 make_scoped_refptr(new LayerWithForcedDrawsContent);
7369 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7370 make_scoped_refptr(new LayerWithForcedDrawsContent);
7372 root->AddChild(scroll_parent_border);
7373 scroll_parent_border->AddChild(scroll_parent_clip);
7374 scroll_parent_clip->AddChild(scroll_parent);
7376 root->AddChild(scroll_child);
7378 scroll_parent_clip->SetMasksToBounds(true);
7380 scroll_child->SetScrollParent(scroll_parent.get());
7382 gfx::Transform identity_transform;
7383 SetLayerPropertiesForTesting(root.get(),
7384 identity_transform,
7385 gfx::Point3F(),
7386 gfx::PointF(),
7387 gfx::Size(50, 50),
7388 true,
7389 false);
7390 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7391 identity_transform,
7392 gfx::Point3F(),
7393 gfx::PointF(),
7394 gfx::Size(40, 40),
7395 true,
7396 false);
7397 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7398 identity_transform,
7399 gfx::Point3F(),
7400 gfx::PointF(),
7401 gfx::Size(30, 30),
7402 true,
7403 false);
7404 SetLayerPropertiesForTesting(scroll_parent.get(),
7405 identity_transform,
7406 gfx::Point3F(),
7407 gfx::PointF(),
7408 gfx::Size(50, 50),
7409 true,
7410 false);
7411 SetLayerPropertiesForTesting(scroll_child.get(),
7412 identity_transform,
7413 gfx::Point3F(),
7414 gfx::PointF(),
7415 gfx::Size(50, 50),
7416 true,
7417 false);
7419 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7420 host->SetRootLayer(root);
7422 ExecuteCalculateDrawProperties(root.get());
7424 EXPECT_TRUE(root->render_surface());
7426 EXPECT_EQ(gfx::Rect(0, 0, 30, 30).ToString(),
7427 scroll_child->clip_rect().ToString());
7428 EXPECT_TRUE(scroll_child->is_clipped());
7431 TEST_F(LayerTreeHostCommonTest, ClippedByOutOfOrderScrollGrandparent) {
7432 // Checks that clipping by a scroll parent and scroll grandparent that follow
7433 // you in paint order still results in correct clipping.
7435 // + root
7436 // + scroll_child
7437 // + scroll_parent_border
7438 // | + scroll_parent_clip
7439 // | + scroll_parent
7440 // + scroll_grandparent_border
7441 // + scroll_grandparent_clip
7442 // + scroll_grandparent
7444 scoped_refptr<Layer> root = Layer::Create();
7445 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7446 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7447 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7448 make_scoped_refptr(new LayerWithForcedDrawsContent);
7450 scoped_refptr<Layer> scroll_grandparent_border = Layer::Create();
7451 scoped_refptr<Layer> scroll_grandparent_clip = Layer::Create();
7452 scoped_refptr<LayerWithForcedDrawsContent> scroll_grandparent =
7453 make_scoped_refptr(new LayerWithForcedDrawsContent);
7455 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7456 make_scoped_refptr(new LayerWithForcedDrawsContent);
7458 root->AddChild(scroll_child);
7460 root->AddChild(scroll_parent_border);
7461 scroll_parent_border->AddChild(scroll_parent_clip);
7462 scroll_parent_clip->AddChild(scroll_parent);
7464 root->AddChild(scroll_grandparent_border);
7465 scroll_grandparent_border->AddChild(scroll_grandparent_clip);
7466 scroll_grandparent_clip->AddChild(scroll_grandparent);
7468 scroll_parent_clip->SetMasksToBounds(true);
7469 scroll_grandparent_clip->SetMasksToBounds(true);
7471 scroll_child->SetScrollParent(scroll_parent.get());
7472 scroll_parent_border->SetScrollParent(scroll_grandparent.get());
7474 gfx::Transform identity_transform;
7475 SetLayerPropertiesForTesting(root.get(),
7476 identity_transform,
7477 gfx::Point3F(),
7478 gfx::PointF(),
7479 gfx::Size(50, 50),
7480 true,
7481 false);
7482 SetLayerPropertiesForTesting(scroll_grandparent_border.get(),
7483 identity_transform,
7484 gfx::Point3F(),
7485 gfx::PointF(),
7486 gfx::Size(40, 40),
7487 true,
7488 false);
7489 SetLayerPropertiesForTesting(scroll_grandparent_clip.get(),
7490 identity_transform,
7491 gfx::Point3F(),
7492 gfx::PointF(),
7493 gfx::Size(20, 20),
7494 true,
7495 false);
7496 SetLayerPropertiesForTesting(scroll_grandparent.get(),
7497 identity_transform,
7498 gfx::Point3F(),
7499 gfx::PointF(),
7500 gfx::Size(50, 50),
7501 true,
7502 false);
7503 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7504 identity_transform,
7505 gfx::Point3F(),
7506 gfx::PointF(),
7507 gfx::Size(40, 40),
7508 true,
7509 false);
7510 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7511 identity_transform,
7512 gfx::Point3F(),
7513 gfx::PointF(),
7514 gfx::Size(30, 30),
7515 true,
7516 false);
7517 SetLayerPropertiesForTesting(scroll_parent.get(),
7518 identity_transform,
7519 gfx::Point3F(),
7520 gfx::PointF(),
7521 gfx::Size(50, 50),
7522 true,
7523 false);
7524 SetLayerPropertiesForTesting(scroll_child.get(),
7525 identity_transform,
7526 gfx::Point3F(),
7527 gfx::PointF(),
7528 gfx::Size(50, 50),
7529 true,
7530 false);
7532 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7533 host->SetRootLayer(root);
7535 ExecuteCalculateDrawProperties(root.get());
7537 EXPECT_TRUE(root->render_surface());
7539 EXPECT_EQ(gfx::Rect(0, 0, 20, 20).ToString(),
7540 scroll_child->clip_rect().ToString());
7541 EXPECT_TRUE(scroll_child->is_clipped());
7543 // Despite the fact that we visited the above layers out of order to get the
7544 // correct clip, the layer lists should be unaffected.
7545 EXPECT_EQ(3u, root->render_surface()->layer_list().size());
7546 EXPECT_EQ(scroll_child.get(),
7547 root->render_surface()->layer_list().at(0).get());
7548 EXPECT_EQ(scroll_parent.get(),
7549 root->render_surface()->layer_list().at(1).get());
7550 EXPECT_EQ(scroll_grandparent.get(),
7551 root->render_surface()->layer_list().at(2).get());
7554 TEST_F(LayerTreeHostCommonTest, OutOfOrderClippingRequiresRSLLSorting) {
7555 // Ensures that even if we visit layers out of order, we still produce a
7556 // correctly ordered render surface layer list.
7557 // + root
7558 // + scroll_child
7559 // + scroll_parent_border
7560 // + scroll_parent_clip
7561 // + scroll_parent
7562 // + render_surface1
7563 // + scroll_grandparent_border
7564 // + scroll_grandparent_clip
7565 // + scroll_grandparent
7566 // + render_surface2
7568 scoped_refptr<LayerWithForcedDrawsContent> root =
7569 make_scoped_refptr(new LayerWithForcedDrawsContent);
7571 scoped_refptr<Layer> scroll_parent_border = Layer::Create();
7572 scoped_refptr<Layer> scroll_parent_clip = Layer::Create();
7573 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7574 make_scoped_refptr(new LayerWithForcedDrawsContent);
7575 scoped_refptr<LayerWithForcedDrawsContent> render_surface1 =
7576 make_scoped_refptr(new LayerWithForcedDrawsContent);
7578 scoped_refptr<Layer> scroll_grandparent_border = Layer::Create();
7579 scoped_refptr<Layer> scroll_grandparent_clip = Layer::Create();
7580 scoped_refptr<LayerWithForcedDrawsContent> scroll_grandparent =
7581 make_scoped_refptr(new LayerWithForcedDrawsContent);
7582 scoped_refptr<LayerWithForcedDrawsContent> render_surface2 =
7583 make_scoped_refptr(new LayerWithForcedDrawsContent);
7585 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7586 make_scoped_refptr(new LayerWithForcedDrawsContent);
7588 root->AddChild(scroll_child);
7590 root->AddChild(scroll_parent_border);
7591 scroll_parent_border->AddChild(scroll_parent_clip);
7592 scroll_parent_clip->AddChild(scroll_parent);
7593 scroll_parent->AddChild(render_surface2);
7595 root->AddChild(scroll_grandparent_border);
7596 scroll_grandparent_border->AddChild(scroll_grandparent_clip);
7597 scroll_grandparent_clip->AddChild(scroll_grandparent);
7598 scroll_grandparent->AddChild(render_surface1);
7600 scroll_parent_clip->SetMasksToBounds(true);
7601 scroll_grandparent_clip->SetMasksToBounds(true);
7603 scroll_child->SetScrollParent(scroll_parent.get());
7604 scroll_parent_border->SetScrollParent(scroll_grandparent.get());
7606 render_surface1->SetForceRenderSurface(true);
7607 render_surface2->SetForceRenderSurface(true);
7609 gfx::Transform identity_transform;
7610 SetLayerPropertiesForTesting(root.get(),
7611 identity_transform,
7612 gfx::Point3F(),
7613 gfx::PointF(),
7614 gfx::Size(50, 50),
7615 true,
7616 false);
7617 SetLayerPropertiesForTesting(scroll_grandparent_border.get(),
7618 identity_transform,
7619 gfx::Point3F(),
7620 gfx::PointF(),
7621 gfx::Size(40, 40),
7622 true,
7623 false);
7624 SetLayerPropertiesForTesting(scroll_grandparent_clip.get(),
7625 identity_transform,
7626 gfx::Point3F(),
7627 gfx::PointF(),
7628 gfx::Size(20, 20),
7629 true,
7630 false);
7631 SetLayerPropertiesForTesting(scroll_grandparent.get(),
7632 identity_transform,
7633 gfx::Point3F(),
7634 gfx::PointF(),
7635 gfx::Size(50, 50),
7636 true,
7637 false);
7638 SetLayerPropertiesForTesting(render_surface1.get(),
7639 identity_transform,
7640 gfx::Point3F(),
7641 gfx::PointF(),
7642 gfx::Size(50, 50),
7643 true,
7644 false);
7645 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7646 identity_transform,
7647 gfx::Point3F(),
7648 gfx::PointF(),
7649 gfx::Size(40, 40),
7650 true,
7651 false);
7652 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7653 identity_transform,
7654 gfx::Point3F(),
7655 gfx::PointF(),
7656 gfx::Size(30, 30),
7657 true,
7658 false);
7659 SetLayerPropertiesForTesting(scroll_parent.get(),
7660 identity_transform,
7661 gfx::Point3F(),
7662 gfx::PointF(),
7663 gfx::Size(50, 50),
7664 true,
7665 false);
7666 SetLayerPropertiesForTesting(render_surface2.get(),
7667 identity_transform,
7668 gfx::Point3F(),
7669 gfx::PointF(),
7670 gfx::Size(50, 50),
7671 true,
7672 false);
7673 SetLayerPropertiesForTesting(scroll_child.get(),
7674 identity_transform,
7675 gfx::Point3F(),
7676 gfx::PointF(),
7677 gfx::Size(50, 50),
7678 true,
7679 false);
7681 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7682 host->SetRootLayer(root);
7684 RenderSurfaceLayerList render_surface_layer_list;
7685 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
7686 root.get(),
7687 root->bounds(),
7688 identity_transform,
7689 &render_surface_layer_list);
7691 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7693 EXPECT_TRUE(root->render_surface());
7695 EXPECT_EQ(gfx::Rect(0, 0, 20, 20).ToString(),
7696 scroll_child->clip_rect().ToString());
7697 EXPECT_TRUE(scroll_child->is_clipped());
7699 // Despite the fact that we had to process the layers out of order to get the
7700 // right clip, our render_surface_layer_list's order should be unaffected.
7701 EXPECT_EQ(3u, render_surface_layer_list.size());
7702 EXPECT_EQ(root.get(), render_surface_layer_list.at(0));
7703 EXPECT_EQ(render_surface2.get(), render_surface_layer_list.at(1));
7704 EXPECT_EQ(render_surface1.get(), render_surface_layer_list.at(2));
7705 EXPECT_TRUE(render_surface_layer_list.at(0)->render_surface());
7706 EXPECT_TRUE(render_surface_layer_list.at(1)->render_surface());
7707 EXPECT_TRUE(render_surface_layer_list.at(2)->render_surface());
7710 TEST_F(LayerTreeHostCommonTest, FixedPositionWithInterveningRenderSurface) {
7711 // Ensures that when we have a render surface between a fixed position layer
7712 // and its container, we compute the fixed position layer's draw transform
7713 // with respect to that intervening render surface, not with respect to its
7714 // container's render target.
7716 // + root
7717 // + render_surface
7718 // + fixed
7719 // + child
7721 scoped_refptr<Layer> root = Layer::Create();
7722 scoped_refptr<LayerWithForcedDrawsContent> render_surface =
7723 make_scoped_refptr(new LayerWithForcedDrawsContent());
7724 scoped_refptr<LayerWithForcedDrawsContent> fixed =
7725 make_scoped_refptr(new LayerWithForcedDrawsContent());
7726 scoped_refptr<LayerWithForcedDrawsContent> child =
7727 make_scoped_refptr(new LayerWithForcedDrawsContent());
7729 root->AddChild(render_surface);
7730 render_surface->AddChild(fixed);
7731 fixed->AddChild(child);
7733 root->SetIsContainerForFixedPositionLayers(true);
7734 render_surface->SetForceRenderSurface(true);
7736 LayerPositionConstraint constraint;
7737 constraint.set_is_fixed_position(true);
7738 fixed->SetPositionConstraint(constraint);
7740 SetLayerPropertiesForTesting(root.get(), gfx::Transform(), gfx::Point3F(),
7741 gfx::PointF(), gfx::Size(50, 50), true, false);
7742 SetLayerPropertiesForTesting(render_surface.get(), gfx::Transform(),
7743 gfx::Point3F(), gfx::PointF(7.f, 9.f),
7744 gfx::Size(50, 50), true, false);
7745 SetLayerPropertiesForTesting(fixed.get(), gfx::Transform(), gfx::Point3F(),
7746 gfx::PointF(10.f, 15.f), gfx::Size(50, 50), true,
7747 false);
7748 SetLayerPropertiesForTesting(child.get(), gfx::Transform(), gfx::Point3F(),
7749 gfx::PointF(1.f, 2.f), gfx::Size(50, 50), true,
7750 false);
7752 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7753 host->SetRootLayer(root);
7755 ExecuteCalculateDrawProperties(root.get());
7757 gfx::Transform expected_fixed_draw_transform;
7758 expected_fixed_draw_transform.Translate(10.f, 15.f);
7759 EXPECT_EQ(expected_fixed_draw_transform, fixed->draw_transform());
7761 gfx::Transform expected_fixed_screen_space_transform;
7762 expected_fixed_screen_space_transform.Translate(17.f, 24.f);
7763 EXPECT_EQ(expected_fixed_screen_space_transform,
7764 fixed->screen_space_transform());
7766 gfx::Transform expected_child_draw_transform;
7767 expected_child_draw_transform.Translate(11.f, 17.f);
7768 EXPECT_EQ(expected_child_draw_transform, child->draw_transform());
7770 gfx::Transform expected_child_screen_space_transform;
7771 expected_child_screen_space_transform.Translate(18.f, 26.f);
7772 EXPECT_EQ(expected_child_screen_space_transform,
7773 child->screen_space_transform());
7776 TEST_F(LayerTreeHostCommonTest, ScrollCompensationWithRounding) {
7777 // This test verifies that a scrolling layer that gets snapped to
7778 // integer coordinates doesn't move a fixed position child.
7780 // + root
7781 // + container
7782 // + scroller
7783 // + fixed
7785 FakeImplProxy proxy;
7786 TestSharedBitmapManager shared_bitmap_manager;
7787 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7788 host_impl.CreatePendingTree();
7789 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
7790 scoped_ptr<LayerImpl> container =
7791 LayerImpl::Create(host_impl.active_tree(), 2);
7792 LayerImpl* container_layer = container.get();
7793 scoped_ptr<LayerImpl> scroller =
7794 LayerImpl::Create(host_impl.active_tree(), 3);
7795 LayerImpl* scroll_layer = scroller.get();
7796 scoped_ptr<LayerImpl> fixed = LayerImpl::Create(host_impl.active_tree(), 4);
7797 LayerImpl* fixed_layer = fixed.get();
7799 container->SetIsContainerForFixedPositionLayers(true);
7801 LayerPositionConstraint constraint;
7802 constraint.set_is_fixed_position(true);
7803 fixed->SetPositionConstraint(constraint);
7805 scroller->SetScrollClipLayer(container->id());
7807 gfx::Transform identity_transform;
7808 gfx::Transform container_transform;
7809 container_transform.Translate3d(10.0, 20.0, 0.0);
7810 gfx::Vector2dF container_offset = container_transform.To2dTranslation();
7812 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7813 gfx::PointF(), gfx::Size(50, 50), true, false,
7814 true);
7815 SetLayerPropertiesForTesting(container.get(), container_transform,
7816 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
7817 true, false, false);
7818 SetLayerPropertiesForTesting(scroller.get(), identity_transform,
7819 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
7820 true, false, false);
7821 SetLayerPropertiesForTesting(fixed.get(), identity_transform, gfx::Point3F(),
7822 gfx::PointF(), gfx::Size(50, 50), true, false,
7823 false);
7825 scroller->AddChild(fixed.Pass());
7826 container->AddChild(scroller.Pass());
7827 root->AddChild(container.Pass());
7829 // Rounded to integers already.
7831 gfx::Vector2dF scroll_delta(3.0, 5.0);
7832 scroll_layer->SetScrollDelta(scroll_delta);
7834 LayerImplList render_surface_layer_list;
7835 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7836 root.get(), root->bounds(), &render_surface_layer_list);
7837 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7839 EXPECT_TRANSFORMATION_MATRIX_EQ(
7840 container_layer->draw_properties().screen_space_transform,
7841 fixed_layer->draw_properties().screen_space_transform);
7842 EXPECT_VECTOR_EQ(
7843 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7844 container_offset);
7845 EXPECT_VECTOR_EQ(scroll_layer->draw_properties()
7846 .screen_space_transform.To2dTranslation(),
7847 container_offset - scroll_delta);
7850 // Scroll delta requiring rounding.
7852 gfx::Vector2dF scroll_delta(4.1f, 8.1f);
7853 scroll_layer->SetScrollDelta(scroll_delta);
7855 gfx::Vector2dF rounded_scroll_delta(4.f, 8.f);
7857 LayerImplList render_surface_layer_list;
7858 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7859 root.get(), root->bounds(), &render_surface_layer_list);
7860 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7862 EXPECT_TRANSFORMATION_MATRIX_EQ(
7863 container_layer->draw_properties().screen_space_transform,
7864 fixed_layer->draw_properties().screen_space_transform);
7865 EXPECT_VECTOR_EQ(
7866 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7867 container_offset);
7868 EXPECT_VECTOR_EQ(scroll_layer->draw_properties()
7869 .screen_space_transform.To2dTranslation(),
7870 container_offset - rounded_scroll_delta);
7873 // Scale is applied earlier in the tree.
7875 gfx::Transform scaled_container_transform = container_transform;
7876 scaled_container_transform.Scale3d(3.0, 3.0, 1.0);
7877 container_layer->SetTransform(scaled_container_transform);
7879 gfx::Vector2dF scroll_delta(4.5f, 8.5f);
7880 scroll_layer->SetScrollDelta(scroll_delta);
7882 LayerImplList render_surface_layer_list;
7883 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7884 root.get(), root->bounds(), &render_surface_layer_list);
7885 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7887 EXPECT_TRANSFORMATION_MATRIX_EQ(
7888 container_layer->draw_properties().screen_space_transform,
7889 fixed_layer->draw_properties().screen_space_transform);
7890 EXPECT_VECTOR_EQ(
7891 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7892 container_offset);
7894 container_layer->SetTransform(container_transform);
7897 // Scale is applied on the scroll layer itself.
7899 gfx::Transform scale_transform;
7900 scale_transform.Scale3d(3.0, 3.0, 1.0);
7901 scroll_layer->SetTransform(scale_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_VECTOR_EQ(
7912 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7913 container_offset);
7915 scroll_layer->SetTransform(identity_transform);
7919 TEST_F(LayerTreeHostCommonTest,
7920 ScrollCompensationMainScrollOffsetFractionalPart) {
7921 // This test verifies that a scrolling layer that has fractional scroll offset
7922 // from main doesn't move a fixed position child.
7924 // + root
7925 // + container
7926 // + scroller
7927 // + fixed
7929 FakeImplProxy proxy;
7930 TestSharedBitmapManager shared_bitmap_manager;
7931 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7932 host_impl.CreatePendingTree();
7933 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
7934 scoped_ptr<LayerImpl> container =
7935 LayerImpl::Create(host_impl.active_tree(), 2);
7936 LayerImpl* container_layer = container.get();
7937 scoped_ptr<LayerImpl> scroller =
7938 LayerImpl::Create(host_impl.active_tree(), 3);
7939 LayerImpl* scroll_layer = scroller.get();
7940 scoped_ptr<LayerImpl> fixed = LayerImpl::Create(host_impl.active_tree(), 4);
7941 LayerImpl* fixed_layer = fixed.get();
7943 container->SetIsContainerForFixedPositionLayers(true);
7945 LayerPositionConstraint constraint;
7946 constraint.set_is_fixed_position(true);
7947 fixed->SetPositionConstraint(constraint);
7949 scroller->SetScrollClipLayer(container->id());
7951 gfx::Transform identity_transform;
7952 gfx::Transform container_transform;
7953 container_transform.Translate3d(10.0, 20.0, 0.0);
7954 gfx::Vector2dF container_offset = container_transform.To2dTranslation();
7956 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7957 gfx::PointF(), gfx::Size(50, 50), true, false,
7958 true);
7959 SetLayerPropertiesForTesting(container.get(), container_transform,
7960 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
7961 true, false, false);
7962 SetLayerPropertiesForTesting(scroller.get(), identity_transform,
7963 gfx::Point3F(), gfx::PointF(0.0, 0.0),
7964 gfx::Size(30, 30), true, false, false);
7966 gfx::ScrollOffset scroll_offset(3.3, 4.2);
7967 gfx::Vector2dF main_scroll_fractional_part(0.3f, 0.2f);
7968 gfx::Vector2dF scroll_delta(0.1f, 0.4f);
7969 // Blink only uses the integer part of the scroll_offset for fixed
7970 // position layer.
7971 SetLayerPropertiesForTesting(fixed.get(), identity_transform, gfx::Point3F(),
7972 gfx::PointF(3.0f, 4.0f), gfx::Size(50, 50), true,
7973 false, false);
7974 scroll_layer->PushScrollOffsetFromMainThread(scroll_offset);
7975 scroll_layer->SetScrollDelta(scroll_delta);
7976 scroll_layer->SetScrollCompensationAdjustment(main_scroll_fractional_part);
7978 scroller->AddChild(fixed.Pass());
7979 container->AddChild(scroller.Pass());
7980 root->AddChild(container.Pass());
7982 LayerImplList render_surface_layer_list;
7983 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7984 root.get(), root->bounds(), &render_surface_layer_list);
7985 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7987 EXPECT_TRANSFORMATION_MATRIX_EQ(
7988 container_layer->draw_properties().screen_space_transform,
7989 fixed_layer->draw_properties().screen_space_transform);
7990 EXPECT_VECTOR_EQ(
7991 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7992 container_offset);
7994 gfx::ScrollOffset effective_scroll_offset =
7995 ScrollOffsetWithDelta(scroll_offset, scroll_delta);
7996 gfx::Vector2d rounded_effective_scroll_offset =
7997 ToRoundedVector2d(ScrollOffsetToVector2dF(effective_scroll_offset));
7998 EXPECT_VECTOR_EQ(
7999 scroll_layer->draw_properties().screen_space_transform.To2dTranslation(),
8000 container_offset - rounded_effective_scroll_offset);
8003 class AnimationScaleFactorTrackingLayerImpl : public LayerImpl {
8004 public:
8005 static scoped_ptr<AnimationScaleFactorTrackingLayerImpl> Create(
8006 LayerTreeImpl* tree_impl,
8007 int id) {
8008 return make_scoped_ptr(
8009 new AnimationScaleFactorTrackingLayerImpl(tree_impl, id));
8012 ~AnimationScaleFactorTrackingLayerImpl() override {}
8014 private:
8015 explicit AnimationScaleFactorTrackingLayerImpl(LayerTreeImpl* tree_impl,
8016 int id)
8017 : LayerImpl(tree_impl, id) {
8018 SetDrawsContent(true);
8022 TEST_F(LayerTreeHostCommonTest, MaximumAnimationScaleFactor) {
8023 FakeImplProxy proxy;
8024 TestSharedBitmapManager shared_bitmap_manager;
8025 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8026 gfx::Transform identity_matrix;
8027 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> grand_parent =
8028 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 1);
8029 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> parent =
8030 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 2);
8031 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> child =
8032 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 3);
8033 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> grand_child =
8034 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 4);
8036 AnimationScaleFactorTrackingLayerImpl* parent_raw = parent.get();
8037 AnimationScaleFactorTrackingLayerImpl* child_raw = child.get();
8038 AnimationScaleFactorTrackingLayerImpl* grand_child_raw = grand_child.get();
8040 child->AddChild(grand_child.Pass());
8041 parent->AddChild(child.Pass());
8042 grand_parent->AddChild(parent.Pass());
8044 SetLayerPropertiesForTesting(grand_parent.get(), identity_matrix,
8045 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8046 true, false, true);
8047 SetLayerPropertiesForTesting(parent_raw, identity_matrix, gfx::Point3F(),
8048 gfx::PointF(), gfx::Size(1, 2), true, false,
8049 false);
8050 SetLayerPropertiesForTesting(child_raw, identity_matrix, gfx::Point3F(),
8051 gfx::PointF(), gfx::Size(1, 2), true, false,
8052 false);
8054 SetLayerPropertiesForTesting(grand_child_raw, identity_matrix, gfx::Point3F(),
8055 gfx::PointF(), gfx::Size(1, 2), true, false,
8056 false);
8058 ExecuteCalculateDrawProperties(grand_parent.get());
8060 // No layers have animations.
8061 EXPECT_EQ(0.f,
8062 grand_parent->draw_properties().maximum_animation_contents_scale);
8063 EXPECT_EQ(0.f,
8064 parent_raw->draw_properties().maximum_animation_contents_scale);
8065 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8066 EXPECT_EQ(
8067 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8069 TransformOperations translation;
8070 translation.AppendTranslate(1.f, 2.f, 3.f);
8072 AddAnimatedTransformToLayer(
8073 parent_raw, 1.0, TransformOperations(), translation);
8075 // No layers have scale-affecting animations.
8076 EXPECT_EQ(0.f,
8077 grand_parent->draw_properties().maximum_animation_contents_scale);
8078 EXPECT_EQ(0.f,
8079 parent_raw->draw_properties().maximum_animation_contents_scale);
8080 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8081 EXPECT_EQ(
8082 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8084 TransformOperations scale;
8085 scale.AppendScale(5.f, 4.f, 3.f);
8087 AddAnimatedTransformToLayer(child_raw, 1.0, TransformOperations(), scale);
8088 ExecuteCalculateDrawProperties(grand_parent.get());
8090 // Only |child| has a scale-affecting animation.
8091 EXPECT_EQ(0.f,
8092 grand_parent->draw_properties().maximum_animation_contents_scale);
8093 EXPECT_EQ(0.f,
8094 parent_raw->draw_properties().maximum_animation_contents_scale);
8095 EXPECT_EQ(5.f, child_raw->draw_properties().maximum_animation_contents_scale);
8096 EXPECT_EQ(
8097 5.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8099 AddAnimatedTransformToLayer(
8100 grand_parent.get(), 1.0, TransformOperations(), scale);
8101 ExecuteCalculateDrawProperties(grand_parent.get());
8103 // |grand_parent| and |child| have scale-affecting animations.
8104 EXPECT_EQ(5.f,
8105 grand_parent->draw_properties().maximum_animation_contents_scale);
8106 EXPECT_EQ(5.f,
8107 parent_raw->draw_properties().maximum_animation_contents_scale);
8108 // We don't support combining animated scales from two nodes; 0.f means
8109 // that the maximum scale could not be computed.
8110 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8111 EXPECT_EQ(
8112 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8114 AddAnimatedTransformToLayer(parent_raw, 1.0, TransformOperations(), scale);
8115 ExecuteCalculateDrawProperties(grand_parent.get());
8117 // |grand_parent|, |parent|, and |child| have scale-affecting animations.
8118 EXPECT_EQ(5.f,
8119 grand_parent->draw_properties().maximum_animation_contents_scale);
8120 EXPECT_EQ(0.f,
8121 parent_raw->draw_properties().maximum_animation_contents_scale);
8122 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8123 EXPECT_EQ(
8124 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8126 grand_parent->layer_animation_controller()->AbortAnimations(
8127 Animation::TRANSFORM);
8128 parent_raw->layer_animation_controller()->AbortAnimations(
8129 Animation::TRANSFORM);
8130 child_raw->layer_animation_controller()->AbortAnimations(
8131 Animation::TRANSFORM);
8133 TransformOperations perspective;
8134 perspective.AppendPerspective(10.f);
8136 AddAnimatedTransformToLayer(
8137 child_raw, 1.0, TransformOperations(), perspective);
8138 ExecuteCalculateDrawProperties(grand_parent.get());
8140 // |child| has a scale-affecting animation but computing the maximum of this
8141 // animation is not supported.
8142 EXPECT_EQ(0.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 child_raw->layer_animation_controller()->AbortAnimations(
8151 Animation::TRANSFORM);
8153 gfx::Transform scale_matrix;
8154 scale_matrix.Scale(1.f, 2.f);
8155 grand_parent->SetTransform(scale_matrix);
8156 parent_raw->SetTransform(scale_matrix);
8157 AddAnimatedTransformToLayer(parent_raw, 1.0, TransformOperations(), scale);
8158 ExecuteCalculateDrawProperties(grand_parent.get());
8160 // |grand_parent| and |parent| each have scale 2.f. |parent| has a scale
8161 // animation with maximum scale 5.f.
8162 EXPECT_EQ(0.f,
8163 grand_parent->draw_properties().maximum_animation_contents_scale);
8164 EXPECT_EQ(10.f,
8165 parent_raw->draw_properties().maximum_animation_contents_scale);
8166 EXPECT_EQ(10.f,
8167 child_raw->draw_properties().maximum_animation_contents_scale);
8168 EXPECT_EQ(
8169 10.f,
8170 grand_child_raw->draw_properties().maximum_animation_contents_scale);
8172 gfx::Transform perspective_matrix;
8173 perspective_matrix.ApplyPerspectiveDepth(2.f);
8174 child_raw->SetTransform(perspective_matrix);
8175 ExecuteCalculateDrawProperties(grand_parent.get());
8177 // |child| has a transform that's neither a translation nor a scale.
8178 EXPECT_EQ(0.f,
8179 grand_parent->draw_properties().maximum_animation_contents_scale);
8180 EXPECT_EQ(10.f,
8181 parent_raw->draw_properties().maximum_animation_contents_scale);
8182 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8183 EXPECT_EQ(
8184 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8186 parent_raw->SetTransform(perspective_matrix);
8187 ExecuteCalculateDrawProperties(grand_parent.get());
8189 // |parent| and |child| have transforms that are neither translations nor
8190 // scales.
8191 EXPECT_EQ(0.f,
8192 grand_parent->draw_properties().maximum_animation_contents_scale);
8193 EXPECT_EQ(0.f,
8194 parent_raw->draw_properties().maximum_animation_contents_scale);
8195 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8196 EXPECT_EQ(
8197 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8199 parent_raw->SetTransform(identity_matrix);
8200 child_raw->SetTransform(identity_matrix);
8201 grand_parent->SetTransform(perspective_matrix);
8203 ExecuteCalculateDrawProperties(grand_parent.get());
8205 // |grand_parent| has a transform that's neither a translation nor a scale.
8206 EXPECT_EQ(0.f,
8207 grand_parent->draw_properties().maximum_animation_contents_scale);
8208 EXPECT_EQ(0.f,
8209 parent_raw->draw_properties().maximum_animation_contents_scale);
8210 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8211 EXPECT_EQ(
8212 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8215 static int membership_id(LayerImpl* layer) {
8216 return layer->draw_properties().last_drawn_render_surface_layer_list_id;
8219 static void GatherDrawnLayers(LayerImplList* rsll,
8220 std::set<LayerImpl*>* drawn_layers) {
8221 for (LayerIterator<LayerImpl> it = LayerIterator<LayerImpl>::Begin(rsll),
8222 end = LayerIterator<LayerImpl>::End(rsll);
8223 it != end;
8224 ++it) {
8225 LayerImpl* layer = *it;
8226 if (it.represents_itself())
8227 drawn_layers->insert(layer);
8229 if (!it.represents_contributing_render_surface())
8230 continue;
8232 if (layer->mask_layer())
8233 drawn_layers->insert(layer->mask_layer());
8234 if (layer->replica_layer() && layer->replica_layer()->mask_layer())
8235 drawn_layers->insert(layer->replica_layer()->mask_layer());
8239 TEST_F(LayerTreeHostCommonTest, RenderSurfaceLayerListMembership) {
8240 FakeImplProxy proxy;
8241 TestSharedBitmapManager shared_bitmap_manager;
8242 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8243 gfx::Transform identity_matrix;
8245 scoped_ptr<LayerImpl> grand_parent =
8246 LayerImpl::Create(host_impl.active_tree(), 1);
8247 scoped_ptr<LayerImpl> parent = LayerImpl::Create(host_impl.active_tree(), 3);
8248 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.active_tree(), 5);
8249 scoped_ptr<LayerImpl> grand_child1 =
8250 LayerImpl::Create(host_impl.active_tree(), 7);
8251 scoped_ptr<LayerImpl> grand_child2 =
8252 LayerImpl::Create(host_impl.active_tree(), 9);
8254 LayerImpl* grand_parent_raw = grand_parent.get();
8255 LayerImpl* parent_raw = parent.get();
8256 LayerImpl* child_raw = child.get();
8257 LayerImpl* grand_child1_raw = grand_child1.get();
8258 LayerImpl* grand_child2_raw = grand_child2.get();
8260 child->AddChild(grand_child1.Pass());
8261 child->AddChild(grand_child2.Pass());
8262 parent->AddChild(child.Pass());
8263 grand_parent->AddChild(parent.Pass());
8265 SetLayerPropertiesForTesting(grand_parent_raw, identity_matrix,
8266 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8267 true, false, true);
8268 SetLayerPropertiesForTesting(parent_raw, identity_matrix, gfx::Point3F(),
8269 gfx::PointF(), gfx::Size(1, 2), true, false,
8270 false);
8272 SetLayerPropertiesForTesting(child_raw, identity_matrix, gfx::Point3F(),
8273 gfx::PointF(), gfx::Size(1, 2), true, false,
8274 false);
8276 SetLayerPropertiesForTesting(grand_child1_raw, identity_matrix,
8277 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8278 true, false, false);
8280 SetLayerPropertiesForTesting(grand_child2_raw, identity_matrix,
8281 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8282 true, false, false);
8284 // Start with nothing being drawn.
8285 ExecuteCalculateDrawProperties(grand_parent_raw);
8286 int member_id = render_surface_layer_list_count();
8288 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8289 EXPECT_NE(member_id, membership_id(parent_raw));
8290 EXPECT_NE(member_id, membership_id(child_raw));
8291 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8292 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8294 std::set<LayerImpl*> expected;
8295 std::set<LayerImpl*> actual;
8296 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8297 EXPECT_EQ(expected, actual);
8299 // If we force render surface, but none of the layers are in the layer list,
8300 // then this layer should not appear in RSLL.
8301 grand_child1_raw->SetHasRenderSurface(true);
8303 ExecuteCalculateDrawProperties(grand_parent_raw);
8304 member_id = render_surface_layer_list_count();
8306 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8307 EXPECT_NE(member_id, membership_id(parent_raw));
8308 EXPECT_NE(member_id, membership_id(child_raw));
8309 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8310 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8312 expected.clear();
8313 actual.clear();
8314 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8315 EXPECT_EQ(expected, actual);
8317 // However, if we say that this layer also draws content, it will appear in
8318 // RSLL.
8319 grand_child1_raw->SetDrawsContent(true);
8321 ExecuteCalculateDrawProperties(grand_parent_raw);
8322 member_id = render_surface_layer_list_count();
8324 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8325 EXPECT_NE(member_id, membership_id(parent_raw));
8326 EXPECT_NE(member_id, membership_id(child_raw));
8327 EXPECT_EQ(member_id, membership_id(grand_child1_raw));
8328 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8330 expected.clear();
8331 expected.insert(grand_child1_raw);
8333 actual.clear();
8334 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8335 EXPECT_EQ(expected, actual);
8337 // Now child is forced to have a render surface, and one if its children draws
8338 // content.
8339 grand_child1_raw->SetDrawsContent(false);
8340 grand_child1_raw->SetHasRenderSurface(false);
8341 child_raw->SetHasRenderSurface(true);
8342 grand_child2_raw->SetDrawsContent(true);
8344 ExecuteCalculateDrawProperties(grand_parent_raw);
8345 member_id = render_surface_layer_list_count();
8347 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8348 EXPECT_NE(member_id, membership_id(parent_raw));
8349 EXPECT_NE(member_id, membership_id(child_raw));
8350 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8351 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8353 expected.clear();
8354 expected.insert(grand_child2_raw);
8356 actual.clear();
8357 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8358 EXPECT_EQ(expected, actual);
8360 // Add a mask layer to child.
8361 child_raw->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 6).Pass());
8363 ExecuteCalculateDrawProperties(grand_parent_raw);
8364 member_id = render_surface_layer_list_count();
8366 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8367 EXPECT_NE(member_id, membership_id(parent_raw));
8368 EXPECT_NE(member_id, membership_id(child_raw));
8369 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8370 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8371 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8373 expected.clear();
8374 expected.insert(grand_child2_raw);
8375 expected.insert(child_raw->mask_layer());
8377 expected.clear();
8378 expected.insert(grand_child2_raw);
8379 expected.insert(child_raw->mask_layer());
8381 actual.clear();
8382 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8383 EXPECT_EQ(expected, actual);
8385 // Add replica mask layer.
8386 scoped_ptr<LayerImpl> replica_layer =
8387 LayerImpl::Create(host_impl.active_tree(), 20);
8388 replica_layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 21));
8389 child_raw->SetReplicaLayer(replica_layer.Pass());
8391 ExecuteCalculateDrawProperties(grand_parent_raw);
8392 member_id = render_surface_layer_list_count();
8394 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8395 EXPECT_NE(member_id, membership_id(parent_raw));
8396 EXPECT_NE(member_id, membership_id(child_raw));
8397 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8398 EXPECT_EQ(member_id, membership_id(child_raw->replica_layer()->mask_layer()));
8399 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8400 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8402 expected.clear();
8403 expected.insert(grand_child2_raw);
8404 expected.insert(child_raw->mask_layer());
8405 expected.insert(child_raw->replica_layer()->mask_layer());
8407 actual.clear();
8408 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8409 EXPECT_EQ(expected, actual);
8411 child_raw->TakeReplicaLayer();
8413 // With nothing drawing, we should have no layers.
8414 grand_child2_raw->SetDrawsContent(false);
8416 ExecuteCalculateDrawProperties(grand_parent_raw);
8417 member_id = render_surface_layer_list_count();
8419 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8420 EXPECT_NE(member_id, membership_id(parent_raw));
8421 EXPECT_NE(member_id, membership_id(child_raw));
8422 EXPECT_NE(member_id, membership_id(child_raw->mask_layer()));
8423 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8424 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8426 expected.clear();
8427 actual.clear();
8428 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8429 EXPECT_EQ(expected, actual);
8431 // Child itself draws means that we should have the child and the mask in the
8432 // list.
8433 child_raw->SetDrawsContent(true);
8435 ExecuteCalculateDrawProperties(grand_parent_raw);
8436 member_id = render_surface_layer_list_count();
8438 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8439 EXPECT_NE(member_id, membership_id(parent_raw));
8440 EXPECT_EQ(member_id, membership_id(child_raw));
8441 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8442 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8443 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8445 expected.clear();
8446 expected.insert(child_raw);
8447 expected.insert(child_raw->mask_layer());
8448 actual.clear();
8449 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8450 EXPECT_EQ(expected, actual);
8452 child_raw->TakeMaskLayer();
8454 // Now everyone's a member!
8455 grand_parent_raw->SetDrawsContent(true);
8456 parent_raw->SetDrawsContent(true);
8457 child_raw->SetDrawsContent(true);
8458 grand_child1_raw->SetDrawsContent(true);
8459 grand_child2_raw->SetDrawsContent(true);
8461 ExecuteCalculateDrawProperties(grand_parent_raw);
8462 member_id = render_surface_layer_list_count();
8464 EXPECT_EQ(member_id, membership_id(grand_parent_raw));
8465 EXPECT_EQ(member_id, membership_id(parent_raw));
8466 EXPECT_EQ(member_id, membership_id(child_raw));
8467 EXPECT_EQ(member_id, membership_id(grand_child1_raw));
8468 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8470 expected.clear();
8471 expected.insert(grand_parent_raw);
8472 expected.insert(parent_raw);
8473 expected.insert(child_raw);
8474 expected.insert(grand_child1_raw);
8475 expected.insert(grand_child2_raw);
8477 actual.clear();
8478 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8479 EXPECT_EQ(expected, actual);
8482 TEST_F(LayerTreeHostCommonTest, DrawPropertyScales) {
8483 FakeImplProxy proxy;
8484 TestSharedBitmapManager shared_bitmap_manager;
8485 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8487 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
8488 LayerImpl* root_layer = root.get();
8489 scoped_ptr<LayerImpl> child1 = LayerImpl::Create(host_impl.active_tree(), 2);
8490 LayerImpl* child1_layer = child1.get();
8491 scoped_ptr<LayerImpl> child2 = LayerImpl::Create(host_impl.active_tree(), 3);
8492 LayerImpl* child2_layer = child2.get();
8494 root->AddChild(child1.Pass());
8495 root->AddChild(child2.Pass());
8496 root->SetHasRenderSurface(true);
8498 gfx::Transform identity_matrix, scale_transform_child1,
8499 scale_transform_child2;
8500 scale_transform_child1.Scale(2, 3);
8501 scale_transform_child2.Scale(4, 5);
8503 SetLayerPropertiesForTesting(root_layer, identity_matrix, gfx::Point3F(),
8504 gfx::PointF(), gfx::Size(1, 1), true, false,
8505 true);
8506 SetLayerPropertiesForTesting(child1_layer, scale_transform_child1,
8507 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
8508 false, false);
8510 child1_layer->SetMaskLayer(
8511 LayerImpl::Create(host_impl.active_tree(), 4).Pass());
8513 scoped_ptr<LayerImpl> replica_layer =
8514 LayerImpl::Create(host_impl.active_tree(), 5);
8515 replica_layer->SetHasRenderSurface(true);
8516 replica_layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 6));
8517 child1_layer->SetReplicaLayer(replica_layer.Pass());
8518 child1_layer->SetHasRenderSurface(true);
8520 ExecuteCalculateDrawProperties(root_layer);
8522 TransformOperations scale;
8523 scale.AppendScale(5.f, 8.f, 3.f);
8525 AddAnimatedTransformToLayer(child2_layer, 1.0, TransformOperations(), scale);
8526 SetLayerPropertiesForTesting(child2_layer, scale_transform_child2,
8527 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
8528 false, false);
8530 ExecuteCalculateDrawProperties(root_layer);
8532 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().ideal_contents_scale);
8533 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().ideal_contents_scale);
8534 EXPECT_FLOAT_EQ(
8535 3.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8536 EXPECT_FLOAT_EQ(3.f,
8537 child1_layer->replica_layer()
8538 ->mask_layer()
8539 ->draw_properties()
8540 .ideal_contents_scale);
8541 EXPECT_FLOAT_EQ(5.f, child2_layer->draw_properties().ideal_contents_scale);
8543 EXPECT_FLOAT_EQ(
8544 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8545 EXPECT_FLOAT_EQ(
8546 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8547 EXPECT_FLOAT_EQ(0.f,
8548 child1_layer->mask_layer()
8549 ->draw_properties()
8550 .maximum_animation_contents_scale);
8551 EXPECT_FLOAT_EQ(0.f,
8552 child1_layer->replica_layer()
8553 ->mask_layer()
8554 ->draw_properties()
8555 .maximum_animation_contents_scale);
8556 EXPECT_FLOAT_EQ(
8557 8.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8559 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8560 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().page_scale_factor);
8561 EXPECT_FLOAT_EQ(
8562 1.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8563 EXPECT_FLOAT_EQ(1.f,
8564 child1_layer->replica_layer()
8565 ->mask_layer()
8566 ->draw_properties()
8567 .page_scale_factor);
8568 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().page_scale_factor);
8570 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().device_scale_factor);
8571 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().device_scale_factor);
8572 EXPECT_FLOAT_EQ(
8573 1.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8574 EXPECT_FLOAT_EQ(1.f,
8575 child1_layer->replica_layer()
8576 ->mask_layer()
8577 ->draw_properties()
8578 .device_scale_factor);
8579 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().device_scale_factor);
8581 // Changing page-scale would affect ideal_contents_scale and
8582 // maximum_animation_contents_scale.
8584 float page_scale_factor = 3.f;
8585 float device_scale_factor = 1.0f;
8586 std::vector<LayerImpl*> render_surface_layer_list;
8587 gfx::Size device_viewport_size =
8588 gfx::Size(root_layer->bounds().width() * device_scale_factor,
8589 root_layer->bounds().height() * device_scale_factor);
8590 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
8591 root_layer, device_viewport_size, &render_surface_layer_list);
8593 inputs.page_scale_factor = page_scale_factor;
8594 inputs.can_adjust_raster_scales = true;
8595 inputs.page_scale_application_layer = root_layer;
8596 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8598 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().ideal_contents_scale);
8599 EXPECT_FLOAT_EQ(9.f, child1_layer->draw_properties().ideal_contents_scale);
8600 EXPECT_FLOAT_EQ(
8601 9.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8602 EXPECT_FLOAT_EQ(9.f,
8603 child1_layer->replica_layer()
8604 ->mask_layer()
8605 ->draw_properties()
8606 .ideal_contents_scale);
8607 EXPECT_FLOAT_EQ(15.f, child2_layer->draw_properties().ideal_contents_scale);
8609 EXPECT_FLOAT_EQ(
8610 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8611 EXPECT_FLOAT_EQ(
8612 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8613 EXPECT_FLOAT_EQ(0.f,
8614 child1_layer->mask_layer()
8615 ->draw_properties()
8616 .maximum_animation_contents_scale);
8617 EXPECT_FLOAT_EQ(0.f,
8618 child1_layer->replica_layer()
8619 ->mask_layer()
8620 ->draw_properties()
8621 .maximum_animation_contents_scale);
8622 EXPECT_FLOAT_EQ(
8623 24.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8625 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8626 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().page_scale_factor);
8627 EXPECT_FLOAT_EQ(
8628 3.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8629 EXPECT_FLOAT_EQ(3.f,
8630 child1_layer->replica_layer()
8631 ->mask_layer()
8632 ->draw_properties()
8633 .page_scale_factor);
8634 EXPECT_FLOAT_EQ(3.f, child2_layer->draw_properties().page_scale_factor);
8636 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().device_scale_factor);
8637 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().device_scale_factor);
8638 EXPECT_FLOAT_EQ(
8639 1.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8640 EXPECT_FLOAT_EQ(1.f,
8641 child1_layer->replica_layer()
8642 ->mask_layer()
8643 ->draw_properties()
8644 .device_scale_factor);
8645 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().device_scale_factor);
8647 // Changing device-scale would affect ideal_contents_scale and
8648 // maximum_animation_contents_scale.
8650 device_scale_factor = 4.0f;
8651 inputs.device_scale_factor = device_scale_factor;
8652 inputs.can_adjust_raster_scales = true;
8653 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8655 EXPECT_FLOAT_EQ(4.f, root_layer->draw_properties().ideal_contents_scale);
8656 EXPECT_FLOAT_EQ(36.f, child1_layer->draw_properties().ideal_contents_scale);
8657 EXPECT_FLOAT_EQ(
8658 36.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8659 EXPECT_FLOAT_EQ(36.f,
8660 child1_layer->replica_layer()
8661 ->mask_layer()
8662 ->draw_properties()
8663 .ideal_contents_scale);
8664 EXPECT_FLOAT_EQ(60.f, child2_layer->draw_properties().ideal_contents_scale);
8666 EXPECT_FLOAT_EQ(
8667 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8668 EXPECT_FLOAT_EQ(
8669 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8670 EXPECT_FLOAT_EQ(0.f,
8671 child1_layer->mask_layer()
8672 ->draw_properties()
8673 .maximum_animation_contents_scale);
8674 EXPECT_FLOAT_EQ(0.f,
8675 child1_layer->replica_layer()
8676 ->mask_layer()
8677 ->draw_properties()
8678 .maximum_animation_contents_scale);
8679 EXPECT_FLOAT_EQ(
8680 96.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8682 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8683 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().page_scale_factor);
8684 EXPECT_FLOAT_EQ(
8685 3.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8686 EXPECT_FLOAT_EQ(3.f,
8687 child1_layer->replica_layer()
8688 ->mask_layer()
8689 ->draw_properties()
8690 .page_scale_factor);
8691 EXPECT_FLOAT_EQ(3.f, child2_layer->draw_properties().page_scale_factor);
8693 EXPECT_FLOAT_EQ(4.f, root_layer->draw_properties().device_scale_factor);
8694 EXPECT_FLOAT_EQ(4.f, child1_layer->draw_properties().device_scale_factor);
8695 EXPECT_FLOAT_EQ(
8696 4.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8697 EXPECT_FLOAT_EQ(4.f,
8698 child1_layer->replica_layer()
8699 ->mask_layer()
8700 ->draw_properties()
8701 .device_scale_factor);
8702 EXPECT_FLOAT_EQ(4.f, child2_layer->draw_properties().device_scale_factor);
8705 TEST_F(LayerTreeHostCommonTest, VisibleContentRectInChildRenderSurface) {
8706 scoped_refptr<Layer> root = Layer::Create();
8707 SetLayerPropertiesForTesting(root.get(),
8708 gfx::Transform(),
8709 gfx::Point3F(),
8710 gfx::PointF(),
8711 gfx::Size(768 / 2, 3000),
8712 true,
8713 false);
8714 root->SetIsDrawable(true);
8716 scoped_refptr<Layer> clip = Layer::Create();
8717 SetLayerPropertiesForTesting(clip.get(),
8718 gfx::Transform(),
8719 gfx::Point3F(),
8720 gfx::PointF(),
8721 gfx::Size(768 / 2, 10000),
8722 true,
8723 false);
8724 clip->SetMasksToBounds(true);
8726 scoped_refptr<Layer> content = Layer::Create();
8727 SetLayerPropertiesForTesting(content.get(),
8728 gfx::Transform(),
8729 gfx::Point3F(),
8730 gfx::PointF(),
8731 gfx::Size(768 / 2, 10000),
8732 true,
8733 false);
8734 content->SetIsDrawable(true);
8735 content->SetForceRenderSurface(true);
8737 root->AddChild(clip);
8738 clip->AddChild(content);
8740 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
8741 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
8742 host->SetRootLayer(root);
8744 gfx::Size device_viewport_size(768, 582);
8745 RenderSurfaceLayerList render_surface_layer_list;
8746 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
8747 host->root_layer(), device_viewport_size, &render_surface_layer_list);
8748 inputs.device_scale_factor = 2.f;
8749 inputs.page_scale_factor = 1.f;
8750 inputs.page_scale_application_layer = NULL;
8751 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8753 // Layers in the root render surface have their visible content rect clipped
8754 // by the viewport.
8755 EXPECT_EQ(gfx::Rect(768 / 2, 582 / 2), root->visible_content_rect());
8757 // Layers drawing to a child render surface should still have their visible
8758 // content rect clipped by the viewport.
8759 EXPECT_EQ(gfx::Rect(768 / 2, 582 / 2), content->visible_content_rect());
8762 TEST_F(LayerTreeHostCommonTest, BoundsDeltaAffectVisibleContentRect) {
8763 FakeImplProxy proxy;
8764 TestSharedBitmapManager shared_bitmap_manager;
8765 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8767 // Set two layers: the root layer clips it's child,
8768 // the child draws its content.
8770 gfx::Size root_size = gfx::Size(300, 500);
8772 // Sublayer should be bigger than the root enlarged by bounds_delta.
8773 gfx::Size sublayer_size = gfx::Size(300, 1000);
8775 // Device viewport accomidated the root and the top controls.
8776 gfx::Size device_viewport_size = gfx::Size(300, 600);
8777 gfx::Transform identity_matrix;
8779 host_impl.active_tree()->SetRootLayer(
8780 LayerImpl::Create(host_impl.active_tree(), 1));
8782 LayerImpl* root = host_impl.active_tree()->root_layer();
8783 SetLayerPropertiesForTesting(root,
8784 identity_matrix,
8785 gfx::Point3F(),
8786 gfx::PointF(),
8787 root_size,
8788 false,
8789 false,
8790 true);
8792 root->SetContentBounds(root_size);
8793 root->SetMasksToBounds(true);
8795 root->AddChild(LayerImpl::Create(host_impl.active_tree(), 2));
8797 LayerImpl* sublayer = root->child_at(0);
8798 SetLayerPropertiesForTesting(sublayer,
8799 identity_matrix,
8800 gfx::Point3F(),
8801 gfx::PointF(),
8802 sublayer_size,
8803 false,
8804 false,
8805 false);
8807 sublayer->SetContentBounds(sublayer_size);
8808 sublayer->SetDrawsContent(true);
8810 LayerImplList layer_impl_list;
8811 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
8812 root, device_viewport_size, &layer_impl_list);
8814 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8816 EXPECT_EQ(gfx::Rect(root_size), sublayer->visible_content_rect());
8818 root->SetBoundsDelta(gfx::Vector2dF(0.0, 50.0));
8820 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8822 gfx::Rect affected_by_delta(0, 0, root_size.width(),
8823 root_size.height() + 50);
8824 EXPECT_EQ(affected_by_delta, sublayer->visible_content_rect());
8827 TEST_F(LayerTreeHostCommonTest, VisibleContentRectForAnimatedLayer) {
8828 const gfx::Transform identity_matrix;
8829 scoped_refptr<Layer> root = Layer::Create();
8830 scoped_refptr<LayerWithForcedDrawsContent> animated =
8831 make_scoped_refptr(new LayerWithForcedDrawsContent());
8833 root->AddChild(animated);
8835 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8836 host->SetRootLayer(root);
8838 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
8839 gfx::PointF(), gfx::Size(100, 100), true, false);
8840 SetLayerPropertiesForTesting(animated.get(), identity_matrix, gfx::Point3F(),
8841 gfx::PointF(), gfx::Size(20, 20), true, false);
8843 root->SetMasksToBounds(true);
8844 root->SetForceRenderSurface(true);
8845 animated->SetOpacity(0.f);
8847 AddOpacityTransitionToController(animated->layer_animation_controller(), 10.0,
8848 0.f, 1.f, false);
8850 ExecuteCalculateDrawProperties(root.get());
8852 EXPECT_FALSE(animated->visible_rect_from_property_trees().IsEmpty());
8855 TEST_F(LayerTreeHostCommonTest,
8856 VisibleContentRectForAnimatedLayerWithSingularTransform) {
8857 const gfx::Transform identity_matrix;
8858 scoped_refptr<Layer> root = Layer::Create();
8859 scoped_refptr<Layer> clip = Layer::Create();
8860 scoped_refptr<LayerWithForcedDrawsContent> animated =
8861 make_scoped_refptr(new LayerWithForcedDrawsContent());
8862 scoped_refptr<LayerWithForcedDrawsContent> surface =
8863 make_scoped_refptr(new LayerWithForcedDrawsContent());
8864 scoped_refptr<LayerWithForcedDrawsContent> descendant_of_animation =
8865 make_scoped_refptr(new LayerWithForcedDrawsContent());
8867 root->AddChild(clip);
8868 clip->AddChild(animated);
8869 animated->AddChild(surface);
8870 surface->AddChild(descendant_of_animation);
8872 clip->SetMasksToBounds(true);
8873 surface->SetForceRenderSurface(true);
8875 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8876 host->SetRootLayer(root);
8878 gfx::Transform uninvertible_matrix;
8879 uninvertible_matrix.Scale3d(6.f, 6.f, 0.f);
8881 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
8882 gfx::PointF(), gfx::Size(100, 100), true, false);
8883 SetLayerPropertiesForTesting(clip.get(), identity_matrix, gfx::Point3F(),
8884 gfx::PointF(), gfx::Size(10, 10), true, false);
8885 SetLayerPropertiesForTesting(animated.get(), uninvertible_matrix,
8886 gfx::Point3F(), gfx::PointF(),
8887 gfx::Size(120, 120), true, false);
8888 SetLayerPropertiesForTesting(surface.get(), identity_matrix, gfx::Point3F(),
8889 gfx::PointF(), gfx::Size(100, 100), true, false);
8890 SetLayerPropertiesForTesting(descendant_of_animation.get(), identity_matrix,
8891 gfx::Point3F(), gfx::PointF(),
8892 gfx::Size(200, 200), true, false);
8894 TransformOperations start_transform_operations;
8895 start_transform_operations.AppendMatrix(uninvertible_matrix);
8896 TransformOperations end_transform_operations;
8898 AddAnimatedTransformToLayer(animated.get(), 10.0, start_transform_operations,
8899 end_transform_operations);
8901 ExecuteCalculateDrawProperties(root.get());
8903 // The animated layer has a singular transform and maps to a non-empty rect in
8904 // clipped target space, so is treated as fully visible.
8905 EXPECT_EQ(gfx::Rect(120, 120), animated->visible_rect_from_property_trees());
8907 // The singular transform on |animated| is flattened when inherited by
8908 // |surface|, and this happens to make it invertible.
8909 EXPECT_EQ(gfx::Rect(2, 2), surface->visible_rect_from_property_trees());
8910 EXPECT_EQ(gfx::Rect(2, 2),
8911 descendant_of_animation->visible_rect_from_property_trees());
8913 gfx::Transform zero_matrix;
8914 zero_matrix.Scale3d(0.f, 0.f, 0.f);
8915 SetLayerPropertiesForTesting(animated.get(), zero_matrix, gfx::Point3F(),
8916 gfx::PointF(), gfx::Size(120, 120), true, false);
8918 ExecuteCalculateDrawProperties(root.get());
8920 // The animated layer maps to the empty rect in clipped target space, so is
8921 // treated as having an empty visible rect.
8922 EXPECT_EQ(gfx::Rect(), animated->visible_rect_from_property_trees());
8924 // This time, flattening does not make |animated|'s transform invertible. This
8925 // means the clip cannot be projected into |surface|'s space, so we treat
8926 // |surface| and layers that draw into it as fully visible.
8927 EXPECT_EQ(gfx::Rect(100, 100), surface->visible_rect_from_property_trees());
8928 EXPECT_EQ(gfx::Rect(200, 200),
8929 descendant_of_animation->visible_rect_from_property_trees());
8932 // Verify that having an animated filter (but no current filter, as these
8933 // are mutually exclusive) correctly creates a render surface.
8934 TEST_F(LayerTreeHostCommonTest, AnimatedFilterCreatesRenderSurface) {
8935 scoped_refptr<Layer> root = Layer::Create();
8936 scoped_refptr<Layer> child = Layer::Create();
8937 scoped_refptr<Layer> grandchild = Layer::Create();
8938 root->AddChild(child);
8939 child->AddChild(grandchild);
8941 gfx::Transform identity_transform;
8942 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
8943 gfx::PointF(), gfx::Size(50, 50), true, false);
8944 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
8945 gfx::PointF(), gfx::Size(50, 50), true, false);
8946 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
8947 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
8948 true, false);
8949 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8950 host->SetRootLayer(root);
8952 AddAnimatedFilterToLayer(child.get(), 10.0, 0.1f, 0.2f);
8954 ExecuteCalculateDrawProperties(root.get());
8956 EXPECT_TRUE(root->render_surface());
8957 EXPECT_TRUE(child->render_surface());
8958 EXPECT_FALSE(grandchild->render_surface());
8960 EXPECT_TRUE(root->filters().IsEmpty());
8961 EXPECT_TRUE(child->filters().IsEmpty());
8962 EXPECT_TRUE(grandchild->filters().IsEmpty());
8964 EXPECT_FALSE(root->FilterIsAnimating());
8965 EXPECT_TRUE(child->FilterIsAnimating());
8966 EXPECT_FALSE(grandchild->FilterIsAnimating());
8969 // Ensures that the property tree code accounts for offsets between fixed
8970 // position layers and their respective containers.
8971 TEST_F(LayerTreeHostCommonTest, PropertyTreesAccountForFixedParentOffset) {
8972 scoped_refptr<Layer> root = Layer::Create();
8973 scoped_refptr<Layer> child = Layer::Create();
8974 scoped_refptr<LayerWithForcedDrawsContent> grandchild =
8975 make_scoped_refptr(new LayerWithForcedDrawsContent());
8977 root->AddChild(child);
8978 child->AddChild(grandchild);
8980 gfx::Transform identity_transform;
8981 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
8982 gfx::PointF(), gfx::Size(50, 50), true, false);
8983 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
8984 gfx::PointF(1000, 1000), gfx::Size(50, 50), true,
8985 false);
8986 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
8987 gfx::Point3F(), gfx::PointF(-1000, -1000),
8988 gfx::Size(50, 50), true, false);
8990 root->SetMasksToBounds(true);
8991 root->SetIsContainerForFixedPositionLayers(true);
8992 LayerPositionConstraint constraint;
8993 constraint.set_is_fixed_position(true);
8994 grandchild->SetPositionConstraint(constraint);
8996 root->SetIsContainerForFixedPositionLayers(true);
8998 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8999 host->SetRootLayer(root);
9001 ExecuteCalculateDrawProperties(root.get());
9003 EXPECT_EQ(gfx::Rect(0, 0, 50, 50),
9004 grandchild->visible_rect_from_property_trees());
9007 TEST_F(LayerTreeHostCommonTest, CombineClipsUsingContentTarget) {
9008 // In the following layer tree, the layer |box|'s render target is |surface|.
9009 // |surface| also creates a transform node. We want to combine clips for |box|
9010 // in the space of its target (i.e., |surface|), not its target's target. This
9011 // test ensures that happens.
9013 gfx::Transform rotate;
9014 rotate.Rotate(5);
9015 gfx::Transform identity;
9017 scoped_refptr<Layer> root = Layer::Create();
9018 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9019 gfx::PointF(), gfx::Size(2500, 1500), true,
9020 false);
9022 scoped_refptr<Layer> frame_clip = Layer::Create();
9023 SetLayerPropertiesForTesting(frame_clip.get(), identity, gfx::Point3F(),
9024 gfx::PointF(), gfx::Size(2500, 1500), true,
9025 false);
9026 frame_clip->SetMasksToBounds(true);
9028 scoped_refptr<Layer> rotated = Layer::Create();
9029 SetLayerPropertiesForTesting(rotated.get(), rotate,
9030 gfx::Point3F(1250, 250, 0), gfx::PointF(),
9031 gfx::Size(2500, 500), true, false);
9033 scoped_refptr<Layer> surface = Layer::Create();
9034 SetLayerPropertiesForTesting(surface.get(), rotate, gfx::Point3F(),
9035 gfx::PointF(), gfx::Size(2500, 500), true,
9036 false);
9037 surface->SetOpacity(0.5);
9039 scoped_refptr<LayerWithForcedDrawsContent> container =
9040 make_scoped_refptr(new LayerWithForcedDrawsContent());
9041 SetLayerPropertiesForTesting(container.get(), identity, gfx::Point3F(),
9042 gfx::PointF(), gfx::Size(300, 300), true, false);
9044 scoped_refptr<LayerWithForcedDrawsContent> box =
9045 make_scoped_refptr(new LayerWithForcedDrawsContent());
9046 SetLayerPropertiesForTesting(box.get(), identity, gfx::Point3F(),
9047 gfx::PointF(), gfx::Size(100, 100), true, false);
9049 root->AddChild(frame_clip);
9050 frame_clip->AddChild(rotated);
9051 rotated->AddChild(surface);
9052 surface->AddChild(container);
9053 surface->AddChild(box);
9055 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9056 host->SetRootLayer(root);
9058 ExecuteCalculateDrawProperties(root.get());
9061 TEST_F(LayerTreeHostCommonTest, OnlyApplyFixedPositioningOnce) {
9062 gfx::Transform identity;
9063 gfx::Transform translate_z;
9064 translate_z.Translate3d(0, 0, 10);
9066 scoped_refptr<Layer> root = Layer::Create();
9067 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9068 gfx::PointF(), gfx::Size(800, 800), true, false);
9069 root->SetIsContainerForFixedPositionLayers(true);
9071 scoped_refptr<Layer> frame_clip = Layer::Create();
9072 SetLayerPropertiesForTesting(frame_clip.get(), translate_z, gfx::Point3F(),
9073 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9074 false);
9075 frame_clip->SetMasksToBounds(true);
9077 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9078 make_scoped_refptr(new LayerWithForcedDrawsContent());
9079 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9080 gfx::PointF(), gfx::Size(1000, 1000), true,
9081 false);
9083 LayerPositionConstraint constraint;
9084 constraint.set_is_fixed_position(true);
9085 fixed->SetPositionConstraint(constraint);
9087 root->AddChild(frame_clip);
9088 frame_clip->AddChild(fixed);
9090 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9091 host->SetRootLayer(root);
9093 ExecuteCalculateDrawProperties(root.get());
9095 gfx::Rect expected(0, 0, 100, 100);
9096 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9099 TEST_F(LayerTreeHostCommonTest,
9100 PropertyTreesAccountForScrollCompensationAdjustment) {
9101 gfx::Transform identity;
9102 gfx::Transform translate_z;
9103 translate_z.Translate3d(0, 0, 10);
9105 scoped_refptr<Layer> root = Layer::Create();
9106 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9107 gfx::PointF(), gfx::Size(800, 800), true, false);
9108 root->SetIsContainerForFixedPositionLayers(true);
9110 scoped_refptr<Layer> frame_clip = Layer::Create();
9111 SetLayerPropertiesForTesting(frame_clip.get(), translate_z, gfx::Point3F(),
9112 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9113 false);
9114 frame_clip->SetMasksToBounds(true);
9116 scoped_refptr<LayerWithForcedDrawsContent> scroller =
9117 make_scoped_refptr(new LayerWithForcedDrawsContent());
9118 SetLayerPropertiesForTesting(scroller.get(), identity, gfx::Point3F(),
9119 gfx::PointF(), gfx::Size(1000, 1000), true,
9120 false);
9122 scroller->SetScrollCompensationAdjustment(gfx::Vector2dF(0.3f, 0.7f));
9123 scroller->SetScrollOffset(gfx::ScrollOffset(0.3, 0.7));
9124 scroller->SetScrollClipLayerId(frame_clip->id());
9126 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9127 make_scoped_refptr(new LayerWithForcedDrawsContent());
9128 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9129 gfx::PointF(), gfx::Size(50, 50), true, false);
9131 LayerPositionConstraint constraint;
9132 constraint.set_is_fixed_position(true);
9133 fixed->SetPositionConstraint(constraint);
9135 scoped_refptr<LayerWithForcedDrawsContent> fixed_child =
9136 make_scoped_refptr(new LayerWithForcedDrawsContent());
9137 SetLayerPropertiesForTesting(fixed_child.get(), identity, gfx::Point3F(),
9138 gfx::PointF(), gfx::Size(10, 10), true, false);
9140 fixed_child->SetPositionConstraint(constraint);
9142 root->AddChild(frame_clip);
9143 frame_clip->AddChild(scroller);
9144 scroller->AddChild(fixed);
9145 fixed->AddChild(fixed_child);
9147 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9148 host->SetRootLayer(root);
9150 ExecuteCalculateDrawProperties(root.get());
9152 gfx::Rect expected(0, 0, 50, 50);
9153 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9155 expected = gfx::Rect(0, 0, 10, 10);
9156 EXPECT_EQ(expected, fixed_child->visible_rect_from_property_trees());
9159 TEST_F(LayerTreeHostCommonTest, FixedClipsShouldBeAssociatedWithTheRightNode) {
9160 gfx::Transform identity;
9162 scoped_refptr<Layer> root = Layer::Create();
9163 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9164 gfx::PointF(), gfx::Size(800, 800), true, false);
9165 root->SetIsContainerForFixedPositionLayers(true);
9167 scoped_refptr<Layer> frame_clip = Layer::Create();
9168 SetLayerPropertiesForTesting(frame_clip.get(), identity, gfx::Point3F(),
9169 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9170 false);
9171 frame_clip->SetMasksToBounds(true);
9173 scoped_refptr<LayerWithForcedDrawsContent> scroller =
9174 make_scoped_refptr(new LayerWithForcedDrawsContent());
9175 SetLayerPropertiesForTesting(scroller.get(), identity, gfx::Point3F(),
9176 gfx::PointF(), gfx::Size(1000, 1000), true,
9177 false);
9179 scroller->SetScrollOffset(gfx::ScrollOffset(100, 100));
9180 scroller->SetScrollClipLayerId(frame_clip->id());
9182 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9183 make_scoped_refptr(new LayerWithForcedDrawsContent());
9184 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9185 gfx::PointF(100, 100), gfx::Size(50, 50), true,
9186 false);
9188 LayerPositionConstraint constraint;
9189 constraint.set_is_fixed_position(true);
9190 fixed->SetPositionConstraint(constraint);
9191 fixed->SetForceRenderSurface(true);
9192 fixed->SetMasksToBounds(true);
9194 root->AddChild(frame_clip);
9195 frame_clip->AddChild(scroller);
9196 scroller->AddChild(fixed);
9198 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9199 host->SetRootLayer(root);
9201 ExecuteCalculateDrawProperties(root.get());
9203 gfx::Rect expected(0, 0, 50, 50);
9204 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9207 TEST_F(LayerTreeHostCommonTest, ChangingAxisAlignmentTriggersRebuild) {
9208 gfx::Transform identity;
9209 gfx::Transform translate;
9210 gfx::Transform rotate;
9212 translate.Translate(10, 10);
9213 rotate.Rotate(45);
9215 scoped_refptr<Layer> root = Layer::Create();
9216 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9217 gfx::PointF(), gfx::Size(800, 800), true, false);
9218 root->SetIsContainerForFixedPositionLayers(true);
9220 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9221 host->SetRootLayer(root);
9223 ExecuteCalculateDrawProperties(root.get());
9225 root->SetTransform(translate);
9226 EXPECT_FALSE(host->property_trees()->needs_rebuild);
9228 root->SetTransform(rotate);
9229 EXPECT_TRUE(host->property_trees()->needs_rebuild);
9232 TEST_F(LayerTreeHostCommonTest, ChangeTransformOrigin) {
9233 scoped_refptr<Layer> root = Layer::Create();
9234 scoped_refptr<LayerWithForcedDrawsContent> child =
9235 make_scoped_refptr(new LayerWithForcedDrawsContent());
9236 root->AddChild(child);
9238 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9239 host->SetRootLayer(root);
9241 gfx::Transform identity_matrix;
9242 gfx::Transform scale_matrix;
9243 scale_matrix.Scale(2.f, 2.f);
9244 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
9245 gfx::PointF(), gfx::Size(100, 100), true, false);
9246 SetLayerPropertiesForTesting(child.get(), scale_matrix, gfx::Point3F(),
9247 gfx::PointF(), gfx::Size(10, 10), true, false);
9249 ExecuteCalculateDrawProperties(root.get());
9250 EXPECT_EQ(gfx::Rect(10, 10), child->visible_rect_from_property_trees());
9252 child->SetTransformOrigin(gfx::Point3F(10.f, 10.f, 10.f));
9254 ExecuteCalculateDrawProperties(root.get());
9255 EXPECT_EQ(gfx::Rect(5, 5, 5, 5), child->visible_rect_from_property_trees());
9258 TEST_F(LayerTreeHostCommonTest, UpdateScrollChildPosition) {
9259 scoped_refptr<Layer> root = Layer::Create();
9260 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
9261 make_scoped_refptr(new LayerWithForcedDrawsContent);
9262 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
9263 make_scoped_refptr(new LayerWithForcedDrawsContent);
9265 root->AddChild(scroll_child);
9266 root->AddChild(scroll_parent);
9267 scroll_child->SetScrollParent(scroll_parent.get());
9268 scroll_parent->SetScrollClipLayerId(root->id());
9270 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9271 host->SetRootLayer(root);
9273 gfx::Transform identity_transform;
9274 gfx::Transform scale;
9275 scale.Scale(2.f, 2.f);
9276 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
9277 gfx::PointF(), gfx::Size(50, 50), true, false);
9278 SetLayerPropertiesForTesting(scroll_child.get(), scale, gfx::Point3F(),
9279 gfx::PointF(), gfx::Size(40, 40), true, false);
9280 SetLayerPropertiesForTesting(scroll_parent.get(), identity_transform,
9281 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
9282 true, false);
9284 ExecuteCalculateDrawProperties(root.get());
9285 EXPECT_EQ(gfx::Rect(25, 25),
9286 scroll_child->visible_rect_from_property_trees());
9288 scroll_child->SetPosition(gfx::PointF(0, -10.f));
9289 scroll_parent->SetScrollOffset(gfx::ScrollOffset(0.f, 10.f));
9290 ExecuteCalculateDrawProperties(root.get());
9291 EXPECT_EQ(gfx::Rect(0, 5, 25, 25),
9292 scroll_child->visible_rect_from_property_trees());
9295 static void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
9298 TEST_F(LayerTreeHostCommonTest, SkippingSubtreeMain) {
9299 gfx::Transform identity;
9300 FakeContentLayerClient client;
9301 scoped_refptr<Layer> root = Layer::Create();
9302 scoped_refptr<LayerWithForcedDrawsContent> child =
9303 make_scoped_refptr(new LayerWithForcedDrawsContent());
9304 scoped_refptr<LayerWithForcedDrawsContent> grandchild =
9305 make_scoped_refptr(new LayerWithForcedDrawsContent());
9306 scoped_refptr<FakeContentLayer> greatgrandchild(
9307 FakeContentLayer::Create(&client));
9308 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9309 gfx::PointF(), gfx::Size(100, 100), true, false);
9310 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9311 gfx::PointF(), gfx::Size(10, 10), true, false);
9312 SetLayerPropertiesForTesting(grandchild.get(), identity, gfx::Point3F(),
9313 gfx::PointF(), gfx::Size(10, 10), true, false);
9314 SetLayerPropertiesForTesting(greatgrandchild.get(), identity, gfx::Point3F(),
9315 gfx::PointF(), gfx::Size(10, 10), true, false);
9317 root->AddChild(child);
9318 child->AddChild(grandchild);
9319 grandchild->AddChild(greatgrandchild);
9321 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9322 host->SetRootLayer(root);
9324 // Check the non-skipped case.
9325 ExecuteCalculateDrawProperties(root.get());
9326 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
9328 // Now we will reset the visible rect from property trees for the grandchild,
9329 // and we will configure |child| in several ways that should force the subtree
9330 // to be skipped. The visible content rect for |grandchild| should, therefore,
9331 // remain empty.
9332 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
9333 gfx::Transform singular;
9334 singular.matrix().set(0, 0, 0);
9336 child->SetTransform(singular);
9337 ExecuteCalculateDrawProperties(root.get());
9338 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9339 child->SetTransform(identity);
9341 child->SetHideLayerAndSubtree(true);
9342 ExecuteCalculateDrawProperties(root.get());
9343 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9344 child->SetHideLayerAndSubtree(false);
9346 child->SetOpacity(0.f);
9347 ExecuteCalculateDrawProperties(root.get());
9348 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9350 // Now, even though child has zero opacity, we will configure |grandchild| and
9351 // |greatgrandchild| in several ways that should force the subtree to be
9352 // processed anyhow.
9353 grandchild->SetTouchEventHandlerRegion(Region(gfx::Rect(0, 0, 10, 10)));
9354 ExecuteCalculateDrawProperties(root.get());
9355 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
9356 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
9357 grandchild->SetTouchEventHandlerRegion(Region());
9358 ExecuteCalculateDrawProperties(root.get());
9359 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9360 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
9362 greatgrandchild->RequestCopyOfOutput(
9363 CopyOutputRequest::CreateBitmapRequest(base::Bind(&CopyOutputCallback)));
9364 ExecuteCalculateDrawProperties(root.get());
9365 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
9368 TEST_F(LayerTreeHostCommonTest, SkippingSubtreeImpl) {
9369 FakeImplProxy proxy;
9370 TestSharedBitmapManager shared_bitmap_manager;
9371 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
9373 gfx::Transform identity;
9374 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
9375 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.active_tree(), 2);
9376 scoped_ptr<LayerImpl> grandchild =
9377 LayerImpl::Create(host_impl.active_tree(), 3);
9379 scoped_ptr<FakeContentLayerImpl> greatgrandchild(
9380 FakeContentLayerImpl::Create(host_impl.active_tree(), 4));
9382 child->SetDrawsContent(true);
9383 grandchild->SetDrawsContent(true);
9384 greatgrandchild->SetDrawsContent(true);
9386 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9387 gfx::PointF(), gfx::Size(100, 100), true, false,
9388 true);
9389 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9390 gfx::PointF(), gfx::Size(10, 10), true, false,
9391 false);
9392 SetLayerPropertiesForTesting(grandchild.get(), identity, gfx::Point3F(),
9393 gfx::PointF(), gfx::Size(10, 10), true, false,
9394 false);
9395 SetLayerPropertiesForTesting(greatgrandchild.get(), identity, gfx::Point3F(),
9396 gfx::PointF(), gfx::Size(10, 10), true, false,
9397 true);
9399 LayerImpl* child_ptr = child.get();
9400 LayerImpl* grandchild_ptr = grandchild.get();
9401 LayerImpl* greatgrandchild_ptr = greatgrandchild.get();
9403 grandchild->AddChild(greatgrandchild.Pass());
9404 child->AddChild(grandchild.Pass());
9405 root->AddChild(child.Pass());
9407 // Check the non-skipped case.
9408 ExecuteCalculateDrawProperties(root.get());
9409 EXPECT_EQ(gfx::Rect(10, 10),
9410 grandchild_ptr->visible_rect_from_property_trees());
9412 // Now we will reset the visible rect from property trees for the grandchild,
9413 // and we will configure |child| in several ways that should force the subtree
9414 // to be skipped. The visible content rect for |grandchild| should, therefore,
9415 // remain empty.
9416 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
9417 gfx::Transform singular;
9418 singular.matrix().set(0, 0, 0);
9420 child_ptr->SetTransform(singular);
9421 ExecuteCalculateDrawProperties(root.get());
9422 EXPECT_EQ(gfx::Rect(0, 0),
9423 grandchild_ptr->visible_rect_from_property_trees());
9424 child_ptr->SetTransform(identity);
9426 child_ptr->SetHideLayerAndSubtree(true);
9427 ExecuteCalculateDrawProperties(root.get());
9428 EXPECT_EQ(gfx::Rect(0, 0),
9429 grandchild_ptr->visible_rect_from_property_trees());
9430 child_ptr->SetHideLayerAndSubtree(false);
9432 child_ptr->SetOpacity(0.f);
9433 ExecuteCalculateDrawProperties(root.get());
9434 EXPECT_EQ(gfx::Rect(0, 0),
9435 grandchild_ptr->visible_rect_from_property_trees());
9437 // Now, even though child has zero opacity, we will configure |grandchild| and
9438 // |greatgrandchild| in several ways that should force the subtree to be
9439 // processed anyhow.
9440 grandchild_ptr->SetTouchEventHandlerRegion(Region(gfx::Rect(0, 0, 10, 10)));
9441 ExecuteCalculateDrawProperties(root.get());
9442 EXPECT_EQ(gfx::Rect(10, 10),
9443 grandchild_ptr->visible_rect_from_property_trees());
9444 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
9445 grandchild_ptr->SetTouchEventHandlerRegion(Region());
9446 ExecuteCalculateDrawProperties(root.get());
9447 EXPECT_EQ(gfx::Rect(0, 0),
9448 grandchild_ptr->visible_rect_from_property_trees());
9449 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
9451 ScopedPtrVector<CopyOutputRequest> requests;
9452 requests.push_back(CopyOutputRequest::CreateEmptyRequest());
9454 greatgrandchild_ptr->PassCopyRequests(&requests);
9455 ExecuteCalculateDrawProperties(root.get());
9456 EXPECT_EQ(gfx::Rect(10, 10),
9457 grandchild_ptr->visible_rect_from_property_trees());
9460 TEST_F(LayerTreeHostCommonTest, SkippingLayer) {
9461 gfx::Transform identity;
9462 FakeContentLayerClient client;
9463 scoped_refptr<Layer> root = Layer::Create();
9464 scoped_refptr<LayerWithForcedDrawsContent> child =
9465 make_scoped_refptr(new LayerWithForcedDrawsContent());
9466 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9467 gfx::PointF(), gfx::Size(100, 100), true, false);
9468 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9469 gfx::PointF(), gfx::Size(10, 10), true, false);
9470 root->AddChild(child);
9472 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9473 host->SetRootLayer(root);
9475 ExecuteCalculateDrawProperties(root.get());
9476 EXPECT_EQ(gfx::Rect(10, 10), child->visible_rect_from_property_trees());
9477 child->set_visible_rect_from_property_trees(gfx::Rect());
9479 child->SetHideLayerAndSubtree(true);
9480 ExecuteCalculateDrawProperties(root.get());
9481 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9482 child->SetHideLayerAndSubtree(false);
9484 child->SetBounds(gfx::Size());
9485 ExecuteCalculateDrawProperties(root.get());
9486 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9487 child->SetBounds(gfx::Size(10, 10));
9489 gfx::Transform rotate;
9490 child->SetDoubleSided(false);
9491 rotate.RotateAboutXAxis(180.f);
9492 child->SetTransform(rotate);
9493 ExecuteCalculateDrawProperties(root.get());
9494 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9495 child->SetDoubleSided(true);
9496 child->SetTransform(identity);
9498 child->SetOpacity(0.f);
9499 ExecuteCalculateDrawProperties(root.get());
9500 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9503 TEST_F(LayerTreeHostCommonTest, LayerTreeRebuildTest) {
9504 // Ensure that the treewalk in LayerTreeHostCommom::
9505 // PreCalculateMetaInformation happens when its required.
9506 scoped_refptr<Layer> root = Layer::Create();
9507 scoped_refptr<Layer> parent = Layer::Create();
9508 scoped_refptr<Layer> child = Layer::Create();
9510 root->AddChild(parent);
9511 parent->AddChild(child);
9513 child->SetClipParent(root.get());
9515 gfx::Transform identity;
9517 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9518 gfx::PointF(), gfx::Size(100, 100), true, false);
9519 SetLayerPropertiesForTesting(parent.get(), identity, gfx::Point3F(),
9520 gfx::PointF(), gfx::Size(100, 100), true, false);
9521 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9522 gfx::PointF(), gfx::Size(100, 100), true, false);
9524 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9525 host->SetRootLayer(root);
9527 ExecuteCalculateDrawProperties(root.get());
9528 EXPECT_EQ(parent->draw_properties().num_unclipped_descendants, 1);
9530 // Ensure the dynamic update to input handlers happens.
9531 child->SetHaveWheelEventHandlers(true);
9532 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_input_handler);
9533 ExecuteCalculateDrawProperties(root.get());
9534 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_input_handler);
9536 child->SetHaveWheelEventHandlers(false);
9537 EXPECT_FALSE(root->draw_properties().layer_or_descendant_has_input_handler);
9538 ExecuteCalculateDrawProperties(root.get());
9539 EXPECT_FALSE(root->draw_properties().layer_or_descendant_has_input_handler);
9541 child->RequestCopyOfOutput(
9542 CopyOutputRequest::CreateRequest(base::Bind(&EmptyCopyOutputCallback)));
9543 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
9544 ExecuteCalculateDrawProperties(root.get());
9545 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
9548 TEST_F(LayerTreeHostCommonTest, InputHandlersRecursiveUpdateTest) {
9549 // Ensure that the treewalk in LayertreeHostCommon::
9550 // PreCalculateMetaInformation updates input handlers correctly.
9551 scoped_refptr<Layer> root = Layer::Create();
9552 scoped_refptr<Layer> child = Layer::Create();
9554 root->AddChild(child);
9556 child->SetHaveWheelEventHandlers(true);
9558 gfx::Transform identity;
9560 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9561 gfx::PointF(), gfx::Size(100, 100), true, false);
9562 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9563 gfx::PointF(), gfx::Size(100, 100), true, false);
9565 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9566 host->SetRootLayer(root);
9568 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 0);
9569 ExecuteCalculateDrawProperties(root.get());
9570 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 1);
9571 child->SetHaveWheelEventHandlers(false);
9572 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 0);
9575 } // namespace
9576 } // namespace cc