Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common_unittest.cc
blob7e3778f0866e9658e59fecf5057bc653f69aca1d
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 explicit LayerWithForcedDrawsContent(const LayerSettings& settings)
50 : Layer(settings) {}
52 bool DrawsContent() const override;
54 private:
55 ~LayerWithForcedDrawsContent() override {}
58 bool LayerWithForcedDrawsContent::DrawsContent() const { return true; }
60 class MockContentLayerClient : public ContentLayerClient {
61 public:
62 MockContentLayerClient() {}
63 ~MockContentLayerClient() override {}
64 void PaintContents(SkCanvas* canvas,
65 const gfx::Rect& clip,
66 PaintingControlSetting picture_control) override {}
67 void PaintContentsToDisplayList(
68 DisplayItemList* display_list,
69 const gfx::Rect& clip,
70 PaintingControlSetting picture_control) override {
71 NOTIMPLEMENTED();
73 bool FillsBoundsCompletely() const override { return false; }
76 scoped_refptr<FakePictureLayer> CreateDrawablePictureLayer(
77 const LayerSettings& settings,
78 ContentLayerClient* delegate) {
79 scoped_refptr<FakePictureLayer> to_return =
80 FakePictureLayer::Create(settings, delegate);
81 to_return->SetIsDrawable(true);
82 return to_return;
85 scoped_refptr<ContentLayer> CreateDrawableContentLayer(
86 const LayerSettings& settings,
87 ContentLayerClient* delegate) {
88 scoped_refptr<ContentLayer> to_return =
89 ContentLayer::Create(settings, delegate);
90 to_return->SetIsDrawable(true);
91 return to_return;
94 #define EXPECT_CONTENTS_SCALE_EQ(expected, layer) \
95 do { \
96 EXPECT_FLOAT_EQ(expected, layer->contents_scale_x()); \
97 EXPECT_FLOAT_EQ(expected, layer->contents_scale_y()); \
98 } while (false)
100 #define EXPECT_IDEAL_SCALE_EQ(expected, layer) \
101 do { \
102 EXPECT_FLOAT_EQ(expected, layer->draw_properties().ideal_contents_scale); \
103 } while (false)
105 TEST_F(LayerTreeHostCommonTest, TransformsForNoOpLayer) {
106 // Sanity check: For layers positioned at zero, with zero size,
107 // and with identity transforms, then the draw transform,
108 // screen space transform, and the hierarchy passed on to children
109 // layers should also be identity transforms.
111 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
112 scoped_refptr<Layer> child = Layer::Create(layer_settings());
113 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
114 parent->AddChild(child);
115 child->AddChild(grand_child);
117 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
118 host->SetRootLayer(parent);
120 gfx::Transform identity_matrix;
121 SetLayerPropertiesForTesting(parent.get(),
122 identity_matrix,
123 gfx::Point3F(),
124 gfx::PointF(),
125 gfx::Size(100, 100),
126 true,
127 false);
128 SetLayerPropertiesForTesting(child.get(),
129 identity_matrix,
130 gfx::Point3F(),
131 gfx::PointF(),
132 gfx::Size(),
133 true,
134 false);
135 SetLayerPropertiesForTesting(grand_child.get(),
136 identity_matrix,
137 gfx::Point3F(),
138 gfx::PointF(),
139 gfx::Size(),
140 true,
141 false);
143 ExecuteCalculateDrawProperties(parent.get());
145 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
146 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
147 child->screen_space_transform());
148 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
149 grand_child->draw_transform());
150 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
151 grand_child->screen_space_transform());
154 TEST_F(LayerTreeHostCommonTest, DoNotSkipLayersWithHandlers) {
155 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
156 scoped_refptr<Layer> child = Layer::Create(layer_settings());
157 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
158 parent->AddChild(child);
159 child->AddChild(grand_child);
161 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
162 host->SetRootLayer(parent);
164 gfx::Transform identity_matrix;
165 SetLayerPropertiesForTesting(parent.get(),
166 identity_matrix,
167 gfx::Point3F(),
168 gfx::PointF(),
169 gfx::Size(100, 100),
170 true,
171 false);
172 SetLayerPropertiesForTesting(child.get(),
173 identity_matrix,
174 gfx::Point3F(),
175 gfx::PointF(10, 10),
176 gfx::Size(100, 100),
177 true,
178 false);
179 // This would have previously caused us to skip our subtree, but this would be
180 // wrong; we need up-to-date draw properties to do hit testing on the layers
181 // with handlers.
182 child->SetOpacity(0.f);
183 SetLayerPropertiesForTesting(grand_child.get(),
184 identity_matrix,
185 gfx::Point3F(),
186 gfx::PointF(10, 10),
187 gfx::Size(100, 100),
188 true,
189 false);
190 grand_child->SetTouchEventHandlerRegion(gfx::Rect(0, 0, 100, 100));
192 ExecuteCalculateDrawProperties(parent.get());
194 // Check that we've computed draw properties for the subtree rooted at
195 // |child|.
196 EXPECT_FALSE(child->draw_transform().IsIdentity());
197 EXPECT_FALSE(grand_child->draw_transform().IsIdentity());
200 TEST_F(LayerTreeHostCommonTest, TransformsForSingleLayer) {
201 gfx::Transform identity_matrix;
202 scoped_refptr<Layer> layer = Layer::Create(layer_settings());
204 scoped_refptr<Layer> root = Layer::Create(layer_settings());
205 SetLayerPropertiesForTesting(root.get(),
206 identity_matrix,
207 gfx::Point3F(),
208 gfx::PointF(),
209 gfx::Size(1, 2),
210 true,
211 false);
212 root->AddChild(layer);
214 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
215 host->SetRootLayer(root);
217 // Case 2: Setting the bounds of the layer should not affect either the draw
218 // transform or the screenspace transform.
219 gfx::Transform translation_to_center;
220 translation_to_center.Translate(5.0, 6.0);
221 SetLayerPropertiesForTesting(layer.get(),
222 identity_matrix,
223 gfx::Point3F(),
224 gfx::PointF(),
225 gfx::Size(10, 12),
226 true,
227 false);
228 ExecuteCalculateDrawProperties(root.get());
229 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, layer->draw_transform());
230 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
231 layer->screen_space_transform());
233 // Case 3: The anchor point by itself (without a layer transform) should have
234 // no effect on the transforms.
235 SetLayerPropertiesForTesting(layer.get(),
236 identity_matrix,
237 gfx::Point3F(2.5f, 3.0f, 0.f),
238 gfx::PointF(),
239 gfx::Size(10, 12),
240 true,
241 false);
242 ExecuteCalculateDrawProperties(root.get());
243 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, layer->draw_transform());
244 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
245 layer->screen_space_transform());
247 // Case 4: A change in actual position affects both the draw transform and
248 // screen space transform.
249 gfx::Transform position_transform;
250 position_transform.Translate(0.f, 1.2f);
251 SetLayerPropertiesForTesting(layer.get(),
252 identity_matrix,
253 gfx::Point3F(2.5f, 3.0f, 0.f),
254 gfx::PointF(0.f, 1.2f),
255 gfx::Size(10, 12),
256 true,
257 false);
258 ExecuteCalculateDrawProperties(root.get());
259 EXPECT_TRANSFORMATION_MATRIX_EQ(position_transform, layer->draw_transform());
260 EXPECT_TRANSFORMATION_MATRIX_EQ(position_transform,
261 layer->screen_space_transform());
263 // Case 5: In the correct sequence of transforms, the layer transform should
264 // pre-multiply the translation_to_center. This is easily tested by using a
265 // scale transform, because scale and translation are not commutative.
266 gfx::Transform layer_transform;
267 layer_transform.Scale3d(2.0, 2.0, 1.0);
268 SetLayerPropertiesForTesting(layer.get(),
269 layer_transform,
270 gfx::Point3F(),
271 gfx::PointF(),
272 gfx::Size(10, 12),
273 true,
274 false);
275 ExecuteCalculateDrawProperties(root.get());
276 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_transform, layer->draw_transform());
277 EXPECT_TRANSFORMATION_MATRIX_EQ(layer_transform,
278 layer->screen_space_transform());
280 // Case 6: The layer transform should occur with respect to the anchor point.
281 gfx::Transform translation_to_anchor;
282 translation_to_anchor.Translate(5.0, 0.0);
283 gfx::Transform expected_result =
284 translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
285 SetLayerPropertiesForTesting(layer.get(),
286 layer_transform,
287 gfx::Point3F(5.0f, 0.f, 0.f),
288 gfx::PointF(),
289 gfx::Size(10, 12),
290 true,
291 false);
292 ExecuteCalculateDrawProperties(root.get());
293 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result, layer->draw_transform());
294 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result,
295 layer->screen_space_transform());
297 // Case 7: Verify that position pre-multiplies the layer transform. The
298 // current implementation of CalculateDrawProperties does this implicitly, but
299 // it is still worth testing to detect accidental regressions.
300 expected_result = position_transform * translation_to_anchor *
301 layer_transform * Inverse(translation_to_anchor);
302 SetLayerPropertiesForTesting(layer.get(),
303 layer_transform,
304 gfx::Point3F(5.0f, 0.f, 0.f),
305 gfx::PointF(0.f, 1.2f),
306 gfx::Size(10, 12),
307 true,
308 false);
309 ExecuteCalculateDrawProperties(root.get());
310 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result, layer->draw_transform());
311 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_result,
312 layer->screen_space_transform());
315 TEST_F(LayerTreeHostCommonTest, TransformsAboutScrollOffset) {
316 const gfx::ScrollOffset kScrollOffset(50, 100);
317 const gfx::Vector2dF kScrollDelta(2.34f, 5.67f);
318 const gfx::Vector2d kMaxScrollOffset(200, 200);
319 const gfx::PointF kScrollLayerPosition(-kScrollOffset.x(),
320 -kScrollOffset.y());
321 const float kPageScale = 0.888f;
322 const float kDeviceScale = 1.666f;
324 FakeImplProxy proxy;
325 TestSharedBitmapManager shared_bitmap_manager;
326 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
328 gfx::Transform identity_matrix;
329 scoped_ptr<LayerImpl> sublayer_scoped_ptr(
330 LayerImpl::Create(host_impl.active_tree(), 1));
331 LayerImpl* sublayer = sublayer_scoped_ptr.get();
332 sublayer->SetContentsScale(kPageScale * kDeviceScale,
333 kPageScale * kDeviceScale);
334 SetLayerPropertiesForTesting(sublayer, identity_matrix, gfx::Point3F(),
335 gfx::PointF(), gfx::Size(500, 500), true, false,
336 false);
338 scoped_ptr<LayerImpl> scroll_layer_scoped_ptr(
339 LayerImpl::Create(host_impl.active_tree(), 2));
340 LayerImpl* scroll_layer = scroll_layer_scoped_ptr.get();
341 SetLayerPropertiesForTesting(scroll_layer, identity_matrix, gfx::Point3F(),
342 gfx::PointF(), gfx::Size(10, 20), true, false,
343 false);
344 scoped_ptr<LayerImpl> clip_layer_scoped_ptr(
345 LayerImpl::Create(host_impl.active_tree(), 4));
346 LayerImpl* clip_layer = clip_layer_scoped_ptr.get();
348 scroll_layer->SetScrollClipLayer(clip_layer->id());
349 clip_layer->SetBounds(
350 gfx::Size(scroll_layer->bounds().width() + kMaxScrollOffset.x(),
351 scroll_layer->bounds().height() + kMaxScrollOffset.y()));
352 scroll_layer->SetScrollClipLayer(clip_layer->id());
353 scroll_layer->SetScrollDelta(kScrollDelta);
354 gfx::Transform impl_transform;
355 scroll_layer->AddChild(sublayer_scoped_ptr.Pass());
356 LayerImpl* scroll_layer_raw_ptr = scroll_layer_scoped_ptr.get();
357 clip_layer->AddChild(scroll_layer_scoped_ptr.Pass());
358 scroll_layer_raw_ptr->PushScrollOffsetFromMainThread(kScrollOffset);
360 scoped_ptr<LayerImpl> root(LayerImpl::Create(host_impl.active_tree(), 3));
361 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
362 gfx::PointF(), gfx::Size(3, 4), true, false,
363 false);
364 root->AddChild(clip_layer_scoped_ptr.Pass());
365 root->SetHasRenderSurface(true);
367 ExecuteCalculateDrawProperties(
368 root.get(), kDeviceScale, kPageScale, scroll_layer->parent());
369 gfx::Transform expected_transform = identity_matrix;
370 gfx::PointF sub_layer_screen_position = kScrollLayerPosition - kScrollDelta;
371 sub_layer_screen_position.Scale(kPageScale * kDeviceScale);
372 expected_transform.Translate(MathUtil::Round(sub_layer_screen_position.x()),
373 MathUtil::Round(sub_layer_screen_position.y()));
374 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
375 sublayer->draw_transform());
376 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
377 sublayer->screen_space_transform());
379 gfx::Transform arbitrary_translate;
380 const float kTranslateX = 10.6f;
381 const float kTranslateY = 20.6f;
382 arbitrary_translate.Translate(kTranslateX, kTranslateY);
383 SetLayerPropertiesForTesting(scroll_layer, arbitrary_translate,
384 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 20),
385 true, false, false);
386 ExecuteCalculateDrawProperties(
387 root.get(), kDeviceScale, kPageScale, scroll_layer->parent());
388 expected_transform.MakeIdentity();
389 expected_transform.Translate(
390 MathUtil::Round(kTranslateX * kPageScale * kDeviceScale +
391 sub_layer_screen_position.x()),
392 MathUtil::Round(kTranslateY * kPageScale * kDeviceScale +
393 sub_layer_screen_position.y()));
394 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_transform,
395 sublayer->draw_transform());
398 TEST_F(LayerTreeHostCommonTest, TransformsForSimpleHierarchy) {
399 gfx::Transform identity_matrix;
400 scoped_refptr<Layer> root = Layer::Create(layer_settings());
401 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
402 scoped_refptr<Layer> child = Layer::Create(layer_settings());
403 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
404 root->AddChild(parent);
405 parent->AddChild(child);
406 child->AddChild(grand_child);
408 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
409 host->SetRootLayer(root);
411 // One-time setup of root layer
412 SetLayerPropertiesForTesting(root.get(),
413 identity_matrix,
414 gfx::Point3F(),
415 gfx::PointF(),
416 gfx::Size(1, 2),
417 true,
418 false);
420 // Case 1: parent's anchor point should not affect child or grand_child.
421 SetLayerPropertiesForTesting(parent.get(),
422 identity_matrix,
423 gfx::Point3F(2.5f, 3.0f, 0.f),
424 gfx::PointF(),
425 gfx::Size(10, 12),
426 true,
427 false);
428 SetLayerPropertiesForTesting(child.get(),
429 identity_matrix,
430 gfx::Point3F(),
431 gfx::PointF(),
432 gfx::Size(16, 18),
433 true,
434 false);
435 SetLayerPropertiesForTesting(grand_child.get(),
436 identity_matrix,
437 gfx::Point3F(),
438 gfx::PointF(),
439 gfx::Size(76, 78),
440 true,
441 false);
442 ExecuteCalculateDrawProperties(root.get());
443 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
444 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
445 child->screen_space_transform());
446 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
447 grand_child->draw_transform());
448 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
449 grand_child->screen_space_transform());
451 // Case 2: parent's position affects child and grand_child.
452 gfx::Transform parent_position_transform;
453 parent_position_transform.Translate(0.f, 1.2f);
454 SetLayerPropertiesForTesting(parent.get(),
455 identity_matrix,
456 gfx::Point3F(2.5f, 3.0f, 0.f),
457 gfx::PointF(0.f, 1.2f),
458 gfx::Size(10, 12),
459 true,
460 false);
461 SetLayerPropertiesForTesting(child.get(),
462 identity_matrix,
463 gfx::Point3F(),
464 gfx::PointF(),
465 gfx::Size(16, 18),
466 true,
467 false);
468 SetLayerPropertiesForTesting(grand_child.get(),
469 identity_matrix,
470 gfx::Point3F(),
471 gfx::PointF(),
472 gfx::Size(76, 78),
473 true,
474 false);
475 ExecuteCalculateDrawProperties(root.get());
476 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
477 child->draw_transform());
478 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
479 child->screen_space_transform());
480 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
481 grand_child->draw_transform());
482 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_position_transform,
483 grand_child->screen_space_transform());
485 // Case 3: parent's local transform affects child and grandchild
486 gfx::Transform parent_layer_transform;
487 parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
488 gfx::Transform parent_translation_to_anchor;
489 parent_translation_to_anchor.Translate(2.5, 3.0);
490 gfx::Transform parent_composite_transform =
491 parent_translation_to_anchor * parent_layer_transform *
492 Inverse(parent_translation_to_anchor);
493 SetLayerPropertiesForTesting(parent.get(),
494 parent_layer_transform,
495 gfx::Point3F(2.5f, 3.0f, 0.f),
496 gfx::PointF(),
497 gfx::Size(10, 12),
498 true,
499 false);
500 SetLayerPropertiesForTesting(child.get(),
501 identity_matrix,
502 gfx::Point3F(),
503 gfx::PointF(),
504 gfx::Size(16, 18),
505 true,
506 false);
507 SetLayerPropertiesForTesting(grand_child.get(),
508 identity_matrix,
509 gfx::Point3F(),
510 gfx::PointF(),
511 gfx::Size(76, 78),
512 true,
513 false);
514 ExecuteCalculateDrawProperties(root.get());
515 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
516 child->draw_transform());
517 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
518 child->screen_space_transform());
519 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
520 grand_child->draw_transform());
521 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
522 grand_child->screen_space_transform());
525 TEST_F(LayerTreeHostCommonTest, TransformsForSingleRenderSurface) {
526 scoped_refptr<Layer> root = Layer::Create(layer_settings());
527 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
528 scoped_refptr<Layer> child = Layer::Create(layer_settings());
529 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
530 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
531 root->AddChild(parent);
532 parent->AddChild(child);
533 child->AddChild(grand_child);
535 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
536 host->SetRootLayer(root);
538 // One-time setup of root layer
539 gfx::Transform identity_matrix;
540 SetLayerPropertiesForTesting(root.get(),
541 identity_matrix,
542 gfx::Point3F(),
543 gfx::PointF(),
544 gfx::Size(1, 2),
545 true,
546 false);
548 // Child is set up so that a new render surface should be created.
549 child->SetOpacity(0.5f);
550 child->SetForceRenderSurface(true);
552 gfx::Transform parent_layer_transform;
553 parent_layer_transform.Scale3d(1.f, 0.9f, 1.f);
554 gfx::Transform parent_translation_to_anchor;
555 parent_translation_to_anchor.Translate(25.0, 30.0);
557 gfx::Transform parent_composite_transform =
558 parent_translation_to_anchor * parent_layer_transform *
559 Inverse(parent_translation_to_anchor);
560 gfx::Vector2dF parent_composite_scale =
561 MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
562 1.f);
563 gfx::Transform surface_sublayer_transform;
564 surface_sublayer_transform.Scale(parent_composite_scale.x(),
565 parent_composite_scale.y());
566 gfx::Transform surface_sublayer_composite_transform =
567 parent_composite_transform * Inverse(surface_sublayer_transform);
569 // Child's render surface should not exist yet.
570 ASSERT_FALSE(child->render_surface());
572 SetLayerPropertiesForTesting(parent.get(),
573 parent_layer_transform,
574 gfx::Point3F(25.0f, 30.0f, 0.f),
575 gfx::PointF(),
576 gfx::Size(100, 120),
577 true,
578 false);
579 SetLayerPropertiesForTesting(child.get(),
580 identity_matrix,
581 gfx::Point3F(),
582 gfx::PointF(),
583 gfx::Size(16, 18),
584 true,
585 false);
586 SetLayerPropertiesForTesting(grand_child.get(),
587 identity_matrix,
588 gfx::Point3F(),
589 gfx::PointF(),
590 gfx::Size(8, 10),
591 true,
592 false);
593 ExecuteCalculateDrawProperties(root.get());
595 // Render surface should have been created now.
596 ASSERT_TRUE(child->render_surface());
597 ASSERT_EQ(child.get(), child->render_target());
599 // The child layer's draw transform should refer to its new render surface.
600 // The screen-space transform, however, should still refer to the root.
601 EXPECT_TRANSFORMATION_MATRIX_EQ(surface_sublayer_transform,
602 child->draw_transform());
603 EXPECT_TRANSFORMATION_MATRIX_EQ(parent_composite_transform,
604 child->screen_space_transform());
606 // Because the grand_child is the only drawable content, the child's render
607 // surface will tighten its bounds to the grand_child. The scale at which the
608 // surface's subtree is drawn must be removed from the composite transform.
609 EXPECT_TRANSFORMATION_MATRIX_EQ(
610 surface_sublayer_composite_transform,
611 child->render_target()->render_surface()->draw_transform());
613 // The screen space is the same as the target since the child surface draws
614 // into the root.
615 EXPECT_TRANSFORMATION_MATRIX_EQ(
616 surface_sublayer_composite_transform,
617 child->render_target()->render_surface()->screen_space_transform());
620 TEST_F(LayerTreeHostCommonTest, TransformsForReplica) {
621 scoped_refptr<Layer> root = Layer::Create(layer_settings());
622 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
623 scoped_refptr<Layer> child = Layer::Create(layer_settings());
624 scoped_refptr<Layer> child_replica = Layer::Create(layer_settings());
625 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
626 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
627 root->AddChild(parent);
628 parent->AddChild(child);
629 child->AddChild(grand_child);
630 child->SetReplicaLayer(child_replica.get());
632 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
633 host->SetRootLayer(root);
635 // One-time setup of root layer
636 gfx::Transform identity_matrix;
637 SetLayerPropertiesForTesting(root.get(),
638 identity_matrix,
639 gfx::Point3F(),
640 gfx::PointF(),
641 gfx::Size(1, 2),
642 true,
643 false);
645 // Child is set up so that a new render surface should be created.
646 child->SetOpacity(0.5f);
648 gfx::Transform parent_layer_transform;
649 parent_layer_transform.Scale3d(2.0, 2.0, 1.0);
650 gfx::Transform parent_translation_to_anchor;
651 parent_translation_to_anchor.Translate(2.5, 3.0);
652 gfx::Transform parent_composite_transform =
653 parent_translation_to_anchor * parent_layer_transform *
654 Inverse(parent_translation_to_anchor);
655 gfx::Transform replica_layer_transform;
656 replica_layer_transform.Scale3d(3.0, 3.0, 1.0);
657 gfx::Vector2dF parent_composite_scale =
658 MathUtil::ComputeTransform2dScaleComponents(parent_composite_transform,
659 1.f);
660 gfx::Transform surface_sublayer_transform;
661 surface_sublayer_transform.Scale(parent_composite_scale.x(),
662 parent_composite_scale.y());
663 gfx::Transform replica_composite_transform =
664 parent_composite_transform * replica_layer_transform *
665 Inverse(surface_sublayer_transform);
666 child_replica->SetIsDrawable(true);
667 // Child's render surface should not exist yet.
668 ASSERT_FALSE(child->render_surface());
670 SetLayerPropertiesForTesting(parent.get(),
671 parent_layer_transform,
672 gfx::Point3F(2.5f, 3.0f, 0.f),
673 gfx::PointF(),
674 gfx::Size(10, 12),
675 true,
676 false);
677 SetLayerPropertiesForTesting(child.get(),
678 identity_matrix,
679 gfx::Point3F(),
680 gfx::PointF(),
681 gfx::Size(16, 18),
682 true,
683 false);
684 SetLayerPropertiesForTesting(grand_child.get(),
685 identity_matrix,
686 gfx::Point3F(),
687 gfx::PointF(-0.5f, -0.5f),
688 gfx::Size(1, 1),
689 true,
690 false);
691 SetLayerPropertiesForTesting(child_replica.get(),
692 replica_layer_transform,
693 gfx::Point3F(),
694 gfx::PointF(),
695 gfx::Size(),
696 true,
697 false);
698 ExecuteCalculateDrawProperties(root.get());
700 // Render surface should have been created now.
701 ASSERT_TRUE(child->render_surface());
702 ASSERT_EQ(child.get(), child->render_target());
704 EXPECT_TRANSFORMATION_MATRIX_EQ(
705 replica_composite_transform,
706 child->render_target()->render_surface()->replica_draw_transform());
707 EXPECT_TRANSFORMATION_MATRIX_EQ(replica_composite_transform,
708 child->render_target()
709 ->render_surface()
710 ->replica_screen_space_transform());
713 TEST_F(LayerTreeHostCommonTest, TransformsForRenderSurfaceHierarchy) {
714 // This test creates a more complex tree and verifies it all at once. This
715 // covers the following cases:
716 // - layers that are described w.r.t. a render surface: should have draw
717 // transforms described w.r.t. that surface
718 // - A render surface described w.r.t. an ancestor render surface: should
719 // have a draw transform described w.r.t. that ancestor surface
720 // - Replicas of a render surface are described w.r.t. the replica's
721 // transform around its anchor, along with the surface itself.
722 // - Sanity check on recursion: verify transforms of layers described w.r.t.
723 // a render surface that is described w.r.t. an ancestor render surface.
724 // - verifying that each layer has a reference to the correct render surface
725 // and render target values.
727 scoped_refptr<Layer> root = Layer::Create(layer_settings());
728 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
729 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
730 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
731 scoped_refptr<Layer> child_of_root = Layer::Create(layer_settings());
732 scoped_refptr<Layer> child_of_rs1 = Layer::Create(layer_settings());
733 scoped_refptr<Layer> child_of_rs2 = Layer::Create(layer_settings());
734 scoped_refptr<Layer> replica_of_rs1 = Layer::Create(layer_settings());
735 scoped_refptr<Layer> replica_of_rs2 = Layer::Create(layer_settings());
736 scoped_refptr<Layer> grand_child_of_root = Layer::Create(layer_settings());
737 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs1 =
738 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
739 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs2 =
740 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
741 root->AddChild(parent);
742 parent->AddChild(render_surface1);
743 parent->AddChild(child_of_root);
744 render_surface1->AddChild(child_of_rs1);
745 render_surface1->AddChild(render_surface2);
746 render_surface2->AddChild(child_of_rs2);
747 child_of_root->AddChild(grand_child_of_root);
748 child_of_rs1->AddChild(grand_child_of_rs1);
749 child_of_rs2->AddChild(grand_child_of_rs2);
750 render_surface1->SetReplicaLayer(replica_of_rs1.get());
751 render_surface2->SetReplicaLayer(replica_of_rs2.get());
753 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
754 host->SetRootLayer(root);
756 // In combination with descendant draws content, opacity != 1 forces the layer
757 // to have a new render surface.
758 render_surface1->SetOpacity(0.5f);
759 render_surface2->SetOpacity(0.33f);
761 // One-time setup of root layer
762 gfx::Transform identity_matrix;
763 SetLayerPropertiesForTesting(root.get(),
764 identity_matrix,
765 gfx::Point3F(),
766 gfx::PointF(),
767 gfx::Size(1, 2),
768 true,
769 false);
771 // All layers in the tree are initialized with an anchor at .25 and a size of
772 // (10,10). matrix "A" is the composite layer transform used in all layers,
773 // Matrix "R" is the composite replica transform used in all replica layers.
774 gfx::Transform translation_to_anchor;
775 translation_to_anchor.Translate(2.5, 0.0);
776 gfx::Transform layer_transform;
777 layer_transform.Translate(1.0, 1.0);
778 gfx::Transform replica_layer_transform;
779 replica_layer_transform.Scale3d(-2.0, 5.0, 1.0);
781 gfx::Transform A =
782 translation_to_anchor * layer_transform * Inverse(translation_to_anchor);
783 gfx::Transform R = A * translation_to_anchor * replica_layer_transform *
784 Inverse(translation_to_anchor);
786 gfx::Vector2dF surface1_parent_transform_scale =
787 MathUtil::ComputeTransform2dScaleComponents(A, 1.f);
788 gfx::Transform surface1_sublayer_transform;
789 surface1_sublayer_transform.Scale(surface1_parent_transform_scale.x(),
790 surface1_parent_transform_scale.y());
792 // SS1 = transform given to the subtree of render_surface1
793 gfx::Transform SS1 = surface1_sublayer_transform;
794 // S1 = transform to move from render_surface1 pixels to the layer space of
795 // the owning layer
796 gfx::Transform S1 = Inverse(surface1_sublayer_transform);
798 gfx::Vector2dF surface2_parent_transform_scale =
799 MathUtil::ComputeTransform2dScaleComponents(SS1 * A, 1.f);
800 gfx::Transform surface2_sublayer_transform;
801 surface2_sublayer_transform.Scale(surface2_parent_transform_scale.x(),
802 surface2_parent_transform_scale.y());
804 // SS2 = transform given to the subtree of render_surface2
805 gfx::Transform SS2 = surface2_sublayer_transform;
806 // S2 = transform to move from render_surface2 pixels to the layer space of
807 // the owning layer
808 gfx::Transform S2 = Inverse(surface2_sublayer_transform);
810 SetLayerPropertiesForTesting(parent.get(),
811 layer_transform,
812 gfx::Point3F(2.5f, 0.f, 0.f),
813 gfx::PointF(),
814 gfx::Size(10, 10),
815 true,
816 false);
817 SetLayerPropertiesForTesting(render_surface1.get(),
818 layer_transform,
819 gfx::Point3F(2.5f, 0.f, 0.f),
820 gfx::PointF(),
821 gfx::Size(10, 10),
822 true,
823 false);
824 SetLayerPropertiesForTesting(render_surface2.get(),
825 layer_transform,
826 gfx::Point3F(2.5f, 0.f, 0.f),
827 gfx::PointF(),
828 gfx::Size(10, 10),
829 true,
830 false);
831 SetLayerPropertiesForTesting(child_of_root.get(),
832 layer_transform,
833 gfx::Point3F(2.5f, 0.f, 0.f),
834 gfx::PointF(),
835 gfx::Size(10, 10),
836 true,
837 false);
838 SetLayerPropertiesForTesting(child_of_rs1.get(),
839 layer_transform,
840 gfx::Point3F(2.5f, 0.f, 0.f),
841 gfx::PointF(),
842 gfx::Size(10, 10),
843 true,
844 false);
845 SetLayerPropertiesForTesting(child_of_rs2.get(),
846 layer_transform,
847 gfx::Point3F(2.5f, 0.f, 0.f),
848 gfx::PointF(),
849 gfx::Size(10, 10),
850 true,
851 false);
852 SetLayerPropertiesForTesting(grand_child_of_root.get(),
853 layer_transform,
854 gfx::Point3F(2.5f, 0.f, 0.f),
855 gfx::PointF(),
856 gfx::Size(10, 10),
857 true,
858 false);
859 SetLayerPropertiesForTesting(grand_child_of_rs1.get(),
860 layer_transform,
861 gfx::Point3F(2.5f, 0.f, 0.f),
862 gfx::PointF(),
863 gfx::Size(10, 10),
864 true,
865 false);
866 SetLayerPropertiesForTesting(grand_child_of_rs2.get(),
867 layer_transform,
868 gfx::Point3F(2.5f, 0.f, 0.f),
869 gfx::PointF(),
870 gfx::Size(10, 10),
871 true,
872 false);
873 SetLayerPropertiesForTesting(replica_of_rs1.get(),
874 replica_layer_transform,
875 gfx::Point3F(2.5f, 0.f, 0.f),
876 gfx::PointF(),
877 gfx::Size(),
878 true,
879 false);
880 SetLayerPropertiesForTesting(replica_of_rs2.get(),
881 replica_layer_transform,
882 gfx::Point3F(2.5f, 0.f, 0.f),
883 gfx::PointF(),
884 gfx::Size(),
885 true,
886 false);
888 ExecuteCalculateDrawProperties(root.get());
890 // Only layers that are associated with render surfaces should have an actual
891 // RenderSurface() value.
892 ASSERT_TRUE(root->render_surface());
893 ASSERT_FALSE(child_of_root->render_surface());
894 ASSERT_FALSE(grand_child_of_root->render_surface());
896 ASSERT_TRUE(render_surface1->render_surface());
897 ASSERT_FALSE(child_of_rs1->render_surface());
898 ASSERT_FALSE(grand_child_of_rs1->render_surface());
900 ASSERT_TRUE(render_surface2->render_surface());
901 ASSERT_FALSE(child_of_rs2->render_surface());
902 ASSERT_FALSE(grand_child_of_rs2->render_surface());
904 // Verify all render target accessors
905 EXPECT_EQ(root.get(), parent->render_target());
906 EXPECT_EQ(root.get(), child_of_root->render_target());
907 EXPECT_EQ(root.get(), grand_child_of_root->render_target());
909 EXPECT_EQ(render_surface1.get(), render_surface1->render_target());
910 EXPECT_EQ(render_surface1.get(), child_of_rs1->render_target());
911 EXPECT_EQ(render_surface1.get(), grand_child_of_rs1->render_target());
913 EXPECT_EQ(render_surface2.get(), render_surface2->render_target());
914 EXPECT_EQ(render_surface2.get(), child_of_rs2->render_target());
915 EXPECT_EQ(render_surface2.get(), grand_child_of_rs2->render_target());
917 // Verify layer draw transforms note that draw transforms are described with
918 // respect to the nearest ancestor render surface but screen space transforms
919 // are described with respect to the root.
920 EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->draw_transform());
921 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A, child_of_root->draw_transform());
922 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
923 grand_child_of_root->draw_transform());
925 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1, render_surface1->draw_transform());
926 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A, child_of_rs1->draw_transform());
927 EXPECT_TRANSFORMATION_MATRIX_EQ(SS1 * A * A,
928 grand_child_of_rs1->draw_transform());
930 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2, render_surface2->draw_transform());
931 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A, child_of_rs2->draw_transform());
932 EXPECT_TRANSFORMATION_MATRIX_EQ(SS2 * A * A,
933 grand_child_of_rs2->draw_transform());
935 // Verify layer screen-space transforms
937 EXPECT_TRANSFORMATION_MATRIX_EQ(A, parent->screen_space_transform());
938 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A,
939 child_of_root->screen_space_transform());
940 EXPECT_TRANSFORMATION_MATRIX_EQ(
941 A * A * A, grand_child_of_root->screen_space_transform());
943 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A,
944 render_surface1->screen_space_transform());
945 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
946 child_of_rs1->screen_space_transform());
947 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
948 grand_child_of_rs1->screen_space_transform());
950 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A,
951 render_surface2->screen_space_transform());
952 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A,
953 child_of_rs2->screen_space_transform());
954 EXPECT_TRANSFORMATION_MATRIX_EQ(A * A * A * A * A,
955 grand_child_of_rs2->screen_space_transform());
957 // Verify render surface transforms.
959 // Draw transform of render surface 1 is described with respect to root.
960 EXPECT_TRANSFORMATION_MATRIX_EQ(
961 A * A * S1, render_surface1->render_surface()->draw_transform());
962 EXPECT_TRANSFORMATION_MATRIX_EQ(
963 A * R * S1, render_surface1->render_surface()->replica_draw_transform());
964 EXPECT_TRANSFORMATION_MATRIX_EQ(
965 A * A * S1, render_surface1->render_surface()->screen_space_transform());
966 EXPECT_TRANSFORMATION_MATRIX_EQ(
967 A * R * S1,
968 render_surface1->render_surface()->replica_screen_space_transform());
969 // Draw transform of render surface 2 is described with respect to render
970 // surface 1.
971 EXPECT_TRANSFORMATION_MATRIX_EQ(
972 SS1 * A * S2, render_surface2->render_surface()->draw_transform());
973 EXPECT_TRANSFORMATION_MATRIX_EQ(
974 SS1 * R * S2,
975 render_surface2->render_surface()->replica_draw_transform());
976 EXPECT_TRANSFORMATION_MATRIX_EQ(
977 A * A * A * S2,
978 render_surface2->render_surface()->screen_space_transform());
979 EXPECT_TRANSFORMATION_MATRIX_EQ(
980 A * A * R * S2,
981 render_surface2->render_surface()->replica_screen_space_transform());
983 // Sanity check. If these fail there is probably a bug in the test itself. It
984 // is expected that we correctly set up transforms so that the y-component of
985 // the screen-space transform encodes the "depth" of the layer in the tree.
986 EXPECT_FLOAT_EQ(1.0, parent->screen_space_transform().matrix().get(1, 3));
987 EXPECT_FLOAT_EQ(2.0,
988 child_of_root->screen_space_transform().matrix().get(1, 3));
989 EXPECT_FLOAT_EQ(
990 3.0, grand_child_of_root->screen_space_transform().matrix().get(1, 3));
992 EXPECT_FLOAT_EQ(2.0,
993 render_surface1->screen_space_transform().matrix().get(1, 3));
994 EXPECT_FLOAT_EQ(3.0,
995 child_of_rs1->screen_space_transform().matrix().get(1, 3));
996 EXPECT_FLOAT_EQ(
997 4.0, grand_child_of_rs1->screen_space_transform().matrix().get(1, 3));
999 EXPECT_FLOAT_EQ(3.0,
1000 render_surface2->screen_space_transform().matrix().get(1, 3));
1001 EXPECT_FLOAT_EQ(4.0,
1002 child_of_rs2->screen_space_transform().matrix().get(1, 3));
1003 EXPECT_FLOAT_EQ(
1004 5.0, grand_child_of_rs2->screen_space_transform().matrix().get(1, 3));
1007 TEST_F(LayerTreeHostCommonTest, TransformsForFlatteningLayer) {
1008 // For layers that flatten their subtree, there should be an orthographic
1009 // projection (for x and y values) in the middle of the transform sequence.
1010 // Note that the way the code is currently implemented, it is not expected to
1011 // use a canonical orthographic projection.
1013 scoped_refptr<Layer> root = Layer::Create(layer_settings());
1014 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1015 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1016 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1017 scoped_refptr<LayerWithForcedDrawsContent> great_grand_child =
1018 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1020 gfx::Transform rotation_about_y_axis;
1021 rotation_about_y_axis.RotateAboutYAxis(30.0);
1023 const gfx::Transform identity_matrix;
1024 SetLayerPropertiesForTesting(root.get(),
1025 identity_matrix,
1026 gfx::Point3F(),
1027 gfx::PointF(),
1028 gfx::Size(100, 100),
1029 true,
1030 false);
1031 SetLayerPropertiesForTesting(child.get(),
1032 rotation_about_y_axis,
1033 gfx::Point3F(),
1034 gfx::PointF(),
1035 gfx::Size(10, 10),
1036 true,
1037 false);
1038 SetLayerPropertiesForTesting(grand_child.get(),
1039 rotation_about_y_axis,
1040 gfx::Point3F(),
1041 gfx::PointF(),
1042 gfx::Size(10, 10),
1043 true,
1044 false);
1045 SetLayerPropertiesForTesting(great_grand_child.get(), identity_matrix,
1046 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1047 true, false);
1049 root->AddChild(child);
1050 child->AddChild(grand_child);
1051 grand_child->AddChild(great_grand_child);
1052 child->SetForceRenderSurface(true);
1054 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1055 host->SetRootLayer(root);
1057 // No layers in this test should preserve 3d.
1058 ASSERT_TRUE(root->should_flatten_transform());
1059 ASSERT_TRUE(child->should_flatten_transform());
1060 ASSERT_TRUE(grand_child->should_flatten_transform());
1061 ASSERT_TRUE(great_grand_child->should_flatten_transform());
1063 gfx::Transform expected_child_draw_transform = rotation_about_y_axis;
1064 gfx::Transform expected_child_screen_space_transform = rotation_about_y_axis;
1065 gfx::Transform expected_grand_child_draw_transform =
1066 rotation_about_y_axis; // draws onto child's render surface
1067 gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
1068 flattened_rotation_about_y.FlattenTo2d();
1069 gfx::Transform expected_grand_child_screen_space_transform =
1070 flattened_rotation_about_y * rotation_about_y_axis;
1071 gfx::Transform expected_great_grand_child_draw_transform =
1072 flattened_rotation_about_y;
1073 gfx::Transform expected_great_grand_child_screen_space_transform =
1074 flattened_rotation_about_y * flattened_rotation_about_y;
1076 ExecuteCalculateDrawProperties(root.get());
1078 // The child's draw transform should have been taken by its surface.
1079 ASSERT_TRUE(child->render_surface());
1080 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_draw_transform,
1081 child->render_surface()->draw_transform());
1082 EXPECT_TRANSFORMATION_MATRIX_EQ(
1083 expected_child_screen_space_transform,
1084 child->render_surface()->screen_space_transform());
1085 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1086 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_screen_space_transform,
1087 child->screen_space_transform());
1088 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_draw_transform,
1089 grand_child->draw_transform());
1090 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_grand_child_screen_space_transform,
1091 grand_child->screen_space_transform());
1092 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_great_grand_child_draw_transform,
1093 great_grand_child->draw_transform());
1094 EXPECT_TRANSFORMATION_MATRIX_EQ(
1095 expected_great_grand_child_screen_space_transform,
1096 great_grand_child->screen_space_transform());
1099 TEST_F(LayerTreeHostCommonTest, TransformsForDegenerateIntermediateLayer) {
1100 // A layer that is empty in one axis, but not the other, was accidentally
1101 // skipping a necessary translation. Without that translation, the coordinate
1102 // space of the layer's draw transform is incorrect.
1104 // Normally this isn't a problem, because the layer wouldn't be drawn anyway,
1105 // but if that layer becomes a render surface, then its draw transform is
1106 // implicitly inherited by the rest of the subtree, which then is positioned
1107 // incorrectly as a result.
1109 scoped_refptr<Layer> root = Layer::Create(layer_settings());
1110 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1111 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1112 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1114 // The child height is zero, but has non-zero width that should be accounted
1115 // for while computing draw transforms.
1116 const gfx::Transform identity_matrix;
1117 SetLayerPropertiesForTesting(root.get(),
1118 identity_matrix,
1119 gfx::Point3F(),
1120 gfx::PointF(),
1121 gfx::Size(100, 100),
1122 true,
1123 false);
1124 SetLayerPropertiesForTesting(child.get(),
1125 identity_matrix,
1126 gfx::Point3F(),
1127 gfx::PointF(),
1128 gfx::Size(10, 0),
1129 true,
1130 false);
1131 SetLayerPropertiesForTesting(grand_child.get(),
1132 identity_matrix,
1133 gfx::Point3F(),
1134 gfx::PointF(),
1135 gfx::Size(10, 10),
1136 true,
1137 false);
1139 root->AddChild(child);
1140 child->AddChild(grand_child);
1141 child->SetForceRenderSurface(true);
1143 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1144 host->SetRootLayer(root);
1146 ExecuteCalculateDrawProperties(root.get());
1148 ASSERT_TRUE(child->render_surface());
1149 // This is the real test, the rest are sanity checks.
1150 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1151 child->render_surface()->draw_transform());
1152 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1153 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1154 grand_child->draw_transform());
1157 TEST_F(LayerTreeHostCommonTest, TransformAboveRootLayer) {
1158 // Transformations applied at the root of the tree should be forwarded
1159 // to child layers instead of applied to the root RenderSurface.
1160 const gfx::Transform identity_matrix;
1161 scoped_refptr<LayerWithForcedDrawsContent> root =
1162 new LayerWithForcedDrawsContent(layer_settings());
1163 scoped_refptr<LayerWithForcedDrawsContent> child =
1164 new LayerWithForcedDrawsContent(layer_settings());
1165 child->SetScrollClipLayerId(root->id());
1166 root->AddChild(child);
1168 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1169 host->SetRootLayer(root);
1171 SetLayerPropertiesForTesting(root.get(),
1172 identity_matrix,
1173 gfx::Point3F(),
1174 gfx::PointF(),
1175 gfx::Size(20, 20),
1176 true,
1177 false);
1178 SetLayerPropertiesForTesting(child.get(),
1179 identity_matrix,
1180 gfx::Point3F(),
1181 gfx::PointF(),
1182 gfx::Size(20, 20),
1183 true,
1184 false);
1186 gfx::Transform translate;
1187 translate.Translate(50, 50);
1189 RenderSurfaceLayerList render_surface_layer_list;
1190 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1191 root.get(), root->bounds(), translate, &render_surface_layer_list);
1192 inputs.can_adjust_raster_scales = true;
1193 inputs.property_trees->needs_rebuild = true;
1194 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1195 EXPECT_EQ(translate, root->draw_properties().target_space_transform);
1196 EXPECT_EQ(translate, child->draw_properties().target_space_transform);
1197 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1198 EXPECT_EQ(1.f, root->draw_properties().device_scale_factor);
1199 EXPECT_EQ(1.f, child->draw_properties().device_scale_factor);
1202 gfx::Transform scale;
1203 scale.Scale(2, 2);
1205 RenderSurfaceLayerList render_surface_layer_list;
1206 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1207 root.get(), root->bounds(), scale, &render_surface_layer_list);
1208 inputs.can_adjust_raster_scales = true;
1209 inputs.property_trees->needs_rebuild = true;
1210 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1211 EXPECT_EQ(scale, root->draw_properties().target_space_transform);
1212 EXPECT_EQ(scale, child->draw_properties().target_space_transform);
1213 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1214 EXPECT_EQ(2.f, root->draw_properties().device_scale_factor);
1215 EXPECT_EQ(2.f, child->draw_properties().device_scale_factor);
1218 gfx::Transform rotate;
1219 rotate.Rotate(2);
1221 RenderSurfaceLayerList render_surface_layer_list;
1222 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1223 root.get(), root->bounds(), rotate, &render_surface_layer_list);
1224 inputs.can_adjust_raster_scales = true;
1225 inputs.property_trees->needs_rebuild = true;
1226 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1227 EXPECT_EQ(rotate, root->draw_properties().target_space_transform);
1228 EXPECT_EQ(rotate, child->draw_properties().target_space_transform);
1229 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1230 EXPECT_EQ(1.f, root->draw_properties().device_scale_factor);
1231 EXPECT_EQ(1.f, child->draw_properties().device_scale_factor);
1234 gfx::Transform composite;
1235 composite.ConcatTransform(translate);
1236 composite.ConcatTransform(scale);
1237 composite.ConcatTransform(rotate);
1239 RenderSurfaceLayerList render_surface_layer_list;
1240 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1241 root.get(), root->bounds(), composite, &render_surface_layer_list);
1242 inputs.can_adjust_raster_scales = true;
1243 inputs.property_trees->needs_rebuild = true;
1244 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1245 EXPECT_EQ(composite, root->draw_properties().target_space_transform);
1246 EXPECT_EQ(composite, child->draw_properties().target_space_transform);
1247 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1250 // Verify it composes correctly with device scale.
1251 float device_scale_factor = 1.5f;
1254 RenderSurfaceLayerList render_surface_layer_list;
1255 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1256 root.get(), root->bounds(), translate, &render_surface_layer_list);
1257 inputs.device_scale_factor = device_scale_factor;
1258 inputs.can_adjust_raster_scales = true;
1259 inputs.property_trees->needs_rebuild = true;
1260 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1261 gfx::Transform device_scaled_translate = translate;
1262 device_scaled_translate.Scale(device_scale_factor, device_scale_factor);
1263 EXPECT_EQ(device_scaled_translate,
1264 root->draw_properties().target_space_transform);
1265 EXPECT_EQ(device_scaled_translate,
1266 child->draw_properties().target_space_transform);
1267 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1268 EXPECT_EQ(device_scale_factor, root->draw_properties().device_scale_factor);
1269 EXPECT_EQ(device_scale_factor,
1270 child->draw_properties().device_scale_factor);
1273 // Verify it composes correctly with page scale.
1274 float page_scale_factor = 2.f;
1277 RenderSurfaceLayerList render_surface_layer_list;
1278 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1279 root.get(), root->bounds(), translate, &render_surface_layer_list);
1280 inputs.page_scale_factor = page_scale_factor;
1281 inputs.page_scale_layer = root.get();
1282 inputs.can_adjust_raster_scales = true;
1283 inputs.property_trees->needs_rebuild = true;
1284 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1285 gfx::Transform page_scaled_translate = translate;
1286 page_scaled_translate.Scale(page_scale_factor, page_scale_factor);
1287 EXPECT_EQ(page_scaled_translate,
1288 root->draw_properties().target_space_transform);
1289 EXPECT_EQ(page_scaled_translate,
1290 child->draw_properties().target_space_transform);
1291 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1292 EXPECT_EQ(1.f, root->draw_properties().device_scale_factor);
1293 EXPECT_EQ(1.f, child->draw_properties().device_scale_factor);
1296 // Verify that it composes correctly with transforms directly on root layer.
1297 root->SetTransform(composite);
1300 RenderSurfaceLayerList render_surface_layer_list;
1301 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1302 root.get(), root->bounds(), composite, &render_surface_layer_list);
1303 inputs.can_adjust_raster_scales = true;
1304 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1305 gfx::Transform compositeSquared = composite;
1306 compositeSquared.ConcatTransform(composite);
1307 EXPECT_TRANSFORMATION_MATRIX_EQ(
1308 compositeSquared, root->draw_properties().target_space_transform);
1309 EXPECT_TRANSFORMATION_MATRIX_EQ(
1310 compositeSquared, child->draw_properties().target_space_transform);
1311 EXPECT_EQ(identity_matrix, root->render_surface()->draw_transform());
1315 TEST_F(LayerTreeHostCommonTest,
1316 RenderSurfaceListForRenderSurfaceWithClippedLayer) {
1317 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1318 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
1319 scoped_refptr<LayerWithForcedDrawsContent> child =
1320 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1322 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1323 host->SetRootLayer(parent);
1325 const gfx::Transform identity_matrix;
1326 SetLayerPropertiesForTesting(parent.get(),
1327 identity_matrix,
1328 gfx::Point3F(),
1329 gfx::PointF(),
1330 gfx::Size(10, 10),
1331 true,
1332 false);
1333 SetLayerPropertiesForTesting(render_surface1.get(),
1334 identity_matrix,
1335 gfx::Point3F(),
1336 gfx::PointF(),
1337 gfx::Size(10, 10),
1338 true,
1339 false);
1340 SetLayerPropertiesForTesting(child.get(),
1341 identity_matrix,
1342 gfx::Point3F(),
1343 gfx::PointF(30.f, 30.f),
1344 gfx::Size(10, 10),
1345 true,
1346 false);
1348 parent->AddChild(render_surface1);
1349 parent->SetMasksToBounds(true);
1350 render_surface1->AddChild(child);
1351 render_surface1->SetForceRenderSurface(true);
1353 RenderSurfaceLayerList render_surface_layer_list;
1354 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1355 parent.get(),
1356 parent->bounds(),
1357 gfx::Transform(),
1358 &render_surface_layer_list);
1359 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1361 // The child layer's content is entirely outside the parent's clip rect, so
1362 // the intermediate render surface should not be listed here, even if it was
1363 // forced to be created. Render surfaces without children or visible content
1364 // are unexpected at draw time (e.g. we might try to create a content texture
1365 // of size 0).
1366 ASSERT_TRUE(parent->render_surface());
1367 EXPECT_EQ(1U, render_surface_layer_list.size());
1370 TEST_F(LayerTreeHostCommonTest, RenderSurfaceListForTransparentChild) {
1371 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1372 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
1373 scoped_refptr<LayerWithForcedDrawsContent> child =
1374 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1376 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1377 host->SetRootLayer(parent);
1379 const gfx::Transform identity_matrix;
1380 SetLayerPropertiesForTesting(render_surface1.get(),
1381 identity_matrix,
1382 gfx::Point3F(),
1383 gfx::PointF(),
1384 gfx::Size(10, 10),
1385 true,
1386 false);
1387 SetLayerPropertiesForTesting(child.get(),
1388 identity_matrix,
1389 gfx::Point3F(),
1390 gfx::PointF(),
1391 gfx::Size(10, 10),
1392 true,
1393 false);
1395 parent->AddChild(render_surface1);
1396 render_surface1->AddChild(child);
1397 render_surface1->SetForceRenderSurface(true);
1398 render_surface1->SetOpacity(0.f);
1400 RenderSurfaceLayerList render_surface_layer_list;
1401 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1402 parent.get(), parent->bounds(), &render_surface_layer_list);
1403 inputs.can_adjust_raster_scales = true;
1404 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1406 // Since the layer is transparent, render_surface1->render_surface() should
1407 // not have gotten added anywhere. Also, the drawable content rect should not
1408 // have been extended by the children.
1409 ASSERT_TRUE(parent->render_surface());
1410 EXPECT_EQ(0U, parent->render_surface()->layer_list().size());
1411 EXPECT_EQ(1U, render_surface_layer_list.size());
1412 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1413 EXPECT_EQ(gfx::Rect(), parent->drawable_content_rect());
1416 TEST_F(LayerTreeHostCommonTest, RenderSurfaceForBlendMode) {
1417 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1418 scoped_refptr<LayerWithForcedDrawsContent> child =
1419 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1421 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1422 host->SetRootLayer(parent);
1424 const gfx::Transform identity_matrix;
1425 const SkXfermode::Mode blend_mode = SkXfermode::kMultiply_Mode;
1426 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
1427 gfx::PointF(), gfx::Size(10, 10), true, false);
1429 parent->AddChild(child);
1430 child->SetBlendMode(blend_mode);
1432 RenderSurfaceLayerList render_surface_layer_list;
1433 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1434 parent.get(), parent->bounds(), &render_surface_layer_list);
1435 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1437 // Since the child layer has a blend mode other than normal, it should get
1438 // its own render surface. Also, layer's draw_properties should contain the
1439 // default blend mode, since the render surface becomes responsible for
1440 // applying the blend mode.
1441 ASSERT_TRUE(child->render_surface());
1442 EXPECT_EQ(1U, child->render_surface()->layer_list().size());
1443 EXPECT_EQ(SkXfermode::kSrcOver_Mode, child->draw_properties().blend_mode);
1446 TEST_F(LayerTreeHostCommonTest, ForceRenderSurface) {
1447 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1448 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
1449 scoped_refptr<LayerWithForcedDrawsContent> child =
1450 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1451 render_surface1->SetForceRenderSurface(true);
1453 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1454 host->SetRootLayer(parent);
1456 const gfx::Transform identity_matrix;
1457 SetLayerPropertiesForTesting(parent.get(),
1458 identity_matrix,
1459 gfx::Point3F(),
1460 gfx::PointF(),
1461 gfx::Size(10, 10),
1462 true,
1463 false);
1464 SetLayerPropertiesForTesting(render_surface1.get(),
1465 identity_matrix,
1466 gfx::Point3F(),
1467 gfx::PointF(),
1468 gfx::Size(10, 10),
1469 true,
1470 false);
1471 SetLayerPropertiesForTesting(child.get(),
1472 identity_matrix,
1473 gfx::Point3F(),
1474 gfx::PointF(),
1475 gfx::Size(10, 10),
1476 true,
1477 false);
1479 parent->AddChild(render_surface1);
1480 render_surface1->AddChild(child);
1482 // Sanity check before the actual test
1483 EXPECT_FALSE(parent->render_surface());
1484 EXPECT_FALSE(render_surface1->render_surface());
1487 RenderSurfaceLayerList render_surface_layer_list;
1488 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1489 parent.get(), parent->bounds(), &render_surface_layer_list);
1490 inputs.can_adjust_raster_scales = true;
1491 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1493 // The root layer always creates a render surface
1494 EXPECT_TRUE(parent->render_surface());
1495 EXPECT_TRUE(render_surface1->render_surface());
1496 EXPECT_EQ(2U, render_surface_layer_list.size());
1500 RenderSurfaceLayerList render_surface_layer_list;
1501 render_surface1->SetForceRenderSurface(false);
1502 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1503 parent.get(), parent->bounds(), &render_surface_layer_list);
1504 inputs.can_adjust_raster_scales = true;
1505 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1506 EXPECT_TRUE(parent->render_surface());
1507 EXPECT_FALSE(render_surface1->render_surface());
1508 EXPECT_EQ(1U, render_surface_layer_list.size());
1512 TEST_F(LayerTreeHostCommonTest, RenderSurfacesFlattenScreenSpaceTransform) {
1513 // Render surfaces act as a flattening point for their subtree, so should
1514 // always flatten the target-to-screen space transform seen by descendants.
1516 scoped_refptr<Layer> root = Layer::Create(layer_settings());
1517 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1518 scoped_refptr<LayerWithForcedDrawsContent> child =
1519 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1520 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
1521 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1523 gfx::Transform rotation_about_y_axis;
1524 rotation_about_y_axis.RotateAboutYAxis(30.0);
1525 // Make |parent| have a render surface.
1526 parent->SetOpacity(0.9f);
1528 const gfx::Transform identity_matrix;
1529 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
1530 gfx::PointF(), gfx::Size(100, 100), true, false);
1531 SetLayerPropertiesForTesting(parent.get(), rotation_about_y_axis,
1532 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1533 true, false);
1534 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
1535 gfx::PointF(), gfx::Size(10, 10), true, false);
1536 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
1537 gfx::Point3F(), gfx::PointF(), gfx::Size(10, 10),
1538 true, false);
1540 root->AddChild(parent);
1541 parent->AddChild(child);
1542 child->AddChild(grand_child);
1544 grand_child->SetShouldFlattenTransform(false);
1546 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1547 host->SetRootLayer(root);
1549 // Only grand_child should preserve 3d.
1550 EXPECT_TRUE(root->should_flatten_transform());
1551 EXPECT_TRUE(parent->should_flatten_transform());
1552 EXPECT_TRUE(child->should_flatten_transform());
1553 EXPECT_FALSE(grand_child->should_flatten_transform());
1555 gfx::Transform expected_child_draw_transform = identity_matrix;
1556 gfx::Transform expected_grand_child_draw_transform = identity_matrix;
1558 gfx::Transform flattened_rotation_about_y = rotation_about_y_axis;
1559 flattened_rotation_about_y.FlattenTo2d();
1561 ExecuteCalculateDrawProperties(root.get());
1563 EXPECT_TRUE(parent->render_surface());
1564 EXPECT_FALSE(child->render_surface());
1565 EXPECT_FALSE(grand_child->render_surface());
1567 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix, child->draw_transform());
1568 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_matrix,
1569 grand_child->draw_transform());
1571 // The screen-space transform inherited by |child| and |grand_child| should
1572 // have been flattened at their render target. In particular, the fact that
1573 // |grand_child| happens to preserve 3d shouldn't affect this flattening.
1574 EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
1575 child->screen_space_transform());
1576 EXPECT_TRANSFORMATION_MATRIX_EQ(flattened_rotation_about_y,
1577 grand_child->screen_space_transform());
1580 TEST_F(LayerTreeHostCommonTest, ClipRectCullsRenderSurfaces) {
1581 // The entire subtree of layers that are outside the clip rect should be
1582 // culled away, and should not affect the render_surface_layer_list.
1584 // The test tree is set up as follows:
1585 // - all layers except the leaf_nodes are forced to be a new render surface
1586 // that have something to draw.
1587 // - parent is a large container layer.
1588 // - child has masksToBounds=true to cause clipping.
1589 // - grand_child is positioned outside of the child's bounds
1590 // - great_grand_child is also kept outside child's bounds.
1592 // In this configuration, grand_child and great_grand_child are completely
1593 // outside the clip rect, and they should never get scheduled on the list of
1594 // render surfaces.
1597 const gfx::Transform identity_matrix;
1598 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1599 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1600 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
1601 scoped_refptr<Layer> great_grand_child = Layer::Create(layer_settings());
1602 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
1603 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1604 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
1605 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1606 parent->AddChild(child);
1607 child->AddChild(grand_child);
1608 grand_child->AddChild(great_grand_child);
1610 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1611 host->SetRootLayer(parent);
1613 // leaf_node1 ensures that parent and child are kept on the
1614 // render_surface_layer_list, even though grand_child and great_grand_child
1615 // should be clipped.
1616 child->AddChild(leaf_node1);
1617 great_grand_child->AddChild(leaf_node2);
1619 SetLayerPropertiesForTesting(parent.get(),
1620 identity_matrix,
1621 gfx::Point3F(),
1622 gfx::PointF(),
1623 gfx::Size(500, 500),
1624 true,
1625 false);
1626 SetLayerPropertiesForTesting(child.get(),
1627 identity_matrix,
1628 gfx::Point3F(),
1629 gfx::PointF(),
1630 gfx::Size(20, 20),
1631 true,
1632 false);
1633 SetLayerPropertiesForTesting(grand_child.get(),
1634 identity_matrix,
1635 gfx::Point3F(),
1636 gfx::PointF(45.f, 45.f),
1637 gfx::Size(10, 10),
1638 true,
1639 false);
1640 SetLayerPropertiesForTesting(great_grand_child.get(),
1641 identity_matrix,
1642 gfx::Point3F(),
1643 gfx::PointF(),
1644 gfx::Size(10, 10),
1645 true,
1646 false);
1647 SetLayerPropertiesForTesting(leaf_node1.get(),
1648 identity_matrix,
1649 gfx::Point3F(),
1650 gfx::PointF(),
1651 gfx::Size(500, 500),
1652 true,
1653 false);
1654 SetLayerPropertiesForTesting(leaf_node2.get(),
1655 identity_matrix,
1656 gfx::Point3F(),
1657 gfx::PointF(),
1658 gfx::Size(20, 20),
1659 true,
1660 false);
1662 child->SetMasksToBounds(true);
1663 child->SetOpacity(0.4f);
1664 child->SetForceRenderSurface(true);
1665 grand_child->SetOpacity(0.5f);
1666 great_grand_child->SetOpacity(0.4f);
1668 RenderSurfaceLayerList render_surface_layer_list;
1669 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1670 parent.get(), parent->bounds(), &render_surface_layer_list);
1671 inputs.can_adjust_raster_scales = true;
1672 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1674 ASSERT_EQ(2U, render_surface_layer_list.size());
1675 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1676 EXPECT_EQ(child->id(), render_surface_layer_list.at(1)->id());
1679 TEST_F(LayerTreeHostCommonTest, ClipRectCullsSurfaceWithoutVisibleContent) {
1680 // When a render surface has a clip rect, it is used to clip the content rect
1681 // of the surface. When the render surface is animating its transforms, then
1682 // the content rect's position in the clip rect is not defined on the main
1683 // thread, and its content rect should not be clipped.
1685 // The test tree is set up as follows:
1686 // - parent is a container layer that masksToBounds=true to cause clipping.
1687 // - child is a render surface, which has a clip rect set to the bounds of
1688 // the parent.
1689 // - grand_child is a render surface, and the only visible content in child.
1690 // It is positioned outside of the clip rect from parent.
1692 // In this configuration, grand_child should be outside the clipped
1693 // content rect of the child, making grand_child not appear in the
1694 // render_surface_layer_list. However, when we place an animation on the
1695 // child, this clipping should be avoided and we should keep the grand_child
1696 // in the render_surface_layer_list.
1698 const gfx::Transform identity_matrix;
1699 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1700 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1701 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
1702 scoped_refptr<LayerWithForcedDrawsContent> leaf_node =
1703 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1704 parent->AddChild(child);
1705 child->AddChild(grand_child);
1706 grand_child->AddChild(leaf_node);
1708 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1709 host->SetRootLayer(parent);
1711 SetLayerPropertiesForTesting(parent.get(),
1712 identity_matrix,
1713 gfx::Point3F(),
1714 gfx::PointF(),
1715 gfx::Size(100, 100),
1716 true,
1717 false);
1718 SetLayerPropertiesForTesting(child.get(),
1719 identity_matrix,
1720 gfx::Point3F(),
1721 gfx::PointF(),
1722 gfx::Size(20, 20),
1723 true,
1724 false);
1725 SetLayerPropertiesForTesting(grand_child.get(),
1726 identity_matrix,
1727 gfx::Point3F(),
1728 gfx::PointF(200.f, 200.f),
1729 gfx::Size(10, 10),
1730 true,
1731 false);
1732 SetLayerPropertiesForTesting(leaf_node.get(),
1733 identity_matrix,
1734 gfx::Point3F(),
1735 gfx::PointF(),
1736 gfx::Size(10, 10),
1737 true,
1738 false);
1740 parent->SetMasksToBounds(true);
1741 child->SetOpacity(0.4f);
1742 child->SetForceRenderSurface(true);
1743 grand_child->SetOpacity(0.4f);
1744 grand_child->SetForceRenderSurface(true);
1747 RenderSurfaceLayerList render_surface_layer_list;
1748 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1749 parent.get(), parent->bounds(), &render_surface_layer_list);
1750 inputs.can_adjust_raster_scales = true;
1751 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1753 // Without an animation, we should cull child and grand_child from the
1754 // render_surface_layer_list.
1755 ASSERT_EQ(1U, render_surface_layer_list.size());
1756 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1759 // Now put an animating transform on child.
1760 AddAnimatedTransformToController(
1761 child->layer_animation_controller(), 10.0, 30, 0);
1764 RenderSurfaceLayerList render_surface_layer_list;
1765 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1766 parent.get(), parent->bounds(), &render_surface_layer_list);
1767 inputs.can_adjust_raster_scales = true;
1768 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1770 // With an animating transform, we should keep child and grand_child in the
1771 // render_surface_layer_list.
1772 ASSERT_EQ(3U, render_surface_layer_list.size());
1773 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
1774 EXPECT_EQ(child->id(), render_surface_layer_list.at(1)->id());
1775 EXPECT_EQ(grand_child->id(), render_surface_layer_list.at(2)->id());
1779 TEST_F(LayerTreeHostCommonTest, IsClippedIsSetCorrectly) {
1780 // Layer's IsClipped() property is set to true when:
1781 // - the layer clips its subtree, e.g. masks to bounds,
1782 // - the layer is clipped by an ancestor that contributes to the same
1783 // render target,
1784 // - a surface is clipped by an ancestor that contributes to the same
1785 // render target.
1787 // In particular, for a layer that owns a render surface:
1788 // - the render surface inherits any clip from ancestors, and does NOT
1789 // pass that clipped status to the layer itself.
1790 // - but if the layer itself masks to bounds, it is considered clipped
1791 // and propagates the clip to the subtree.
1793 const gfx::Transform identity_matrix;
1794 scoped_refptr<Layer> root = Layer::Create(layer_settings());
1795 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1796 scoped_refptr<Layer> child1 = Layer::Create(layer_settings());
1797 scoped_refptr<Layer> child2 = Layer::Create(layer_settings());
1798 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
1799 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
1800 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1801 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
1802 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
1803 root->AddChild(parent);
1804 parent->AddChild(child1);
1805 parent->AddChild(child2);
1806 child1->AddChild(grand_child);
1807 child2->AddChild(leaf_node2);
1808 grand_child->AddChild(leaf_node1);
1810 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1811 host->SetRootLayer(root);
1813 child2->SetForceRenderSurface(true);
1815 SetLayerPropertiesForTesting(root.get(),
1816 identity_matrix,
1817 gfx::Point3F(),
1818 gfx::PointF(),
1819 gfx::Size(100, 100),
1820 true,
1821 false);
1822 SetLayerPropertiesForTesting(parent.get(),
1823 identity_matrix,
1824 gfx::Point3F(),
1825 gfx::PointF(),
1826 gfx::Size(100, 100),
1827 true,
1828 false);
1829 SetLayerPropertiesForTesting(child1.get(),
1830 identity_matrix,
1831 gfx::Point3F(),
1832 gfx::PointF(),
1833 gfx::Size(100, 100),
1834 true,
1835 false);
1836 SetLayerPropertiesForTesting(child2.get(),
1837 identity_matrix,
1838 gfx::Point3F(),
1839 gfx::PointF(),
1840 gfx::Size(100, 100),
1841 true,
1842 false);
1843 SetLayerPropertiesForTesting(grand_child.get(),
1844 identity_matrix,
1845 gfx::Point3F(),
1846 gfx::PointF(),
1847 gfx::Size(100, 100),
1848 true,
1849 false);
1850 SetLayerPropertiesForTesting(leaf_node1.get(),
1851 identity_matrix,
1852 gfx::Point3F(),
1853 gfx::PointF(),
1854 gfx::Size(100, 100),
1855 true,
1856 false);
1857 SetLayerPropertiesForTesting(leaf_node2.get(),
1858 identity_matrix,
1859 gfx::Point3F(),
1860 gfx::PointF(),
1861 gfx::Size(100, 100),
1862 true,
1863 false);
1865 // Case 1: nothing is clipped except the root render surface.
1867 RenderSurfaceLayerList render_surface_layer_list;
1868 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1869 root.get(), parent->bounds(), &render_surface_layer_list);
1870 inputs.can_adjust_raster_scales = true;
1871 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1873 ASSERT_TRUE(root->render_surface());
1874 ASSERT_TRUE(child2->render_surface());
1876 EXPECT_FALSE(root->is_clipped());
1877 EXPECT_TRUE(root->render_surface()->is_clipped());
1878 EXPECT_FALSE(parent->is_clipped());
1879 EXPECT_FALSE(child1->is_clipped());
1880 EXPECT_FALSE(child2->is_clipped());
1881 EXPECT_FALSE(child2->render_surface()->is_clipped());
1882 EXPECT_FALSE(grand_child->is_clipped());
1883 EXPECT_FALSE(leaf_node1->is_clipped());
1884 EXPECT_FALSE(leaf_node2->is_clipped());
1887 // Case 2: parent masksToBounds, so the parent, child1, and child2's
1888 // surface are clipped. But layers that contribute to child2's surface are
1889 // not clipped explicitly because child2's surface already accounts for
1890 // that clip.
1892 RenderSurfaceLayerList render_surface_layer_list;
1893 parent->SetMasksToBounds(true);
1894 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1895 root.get(), parent->bounds(), &render_surface_layer_list);
1896 inputs.can_adjust_raster_scales = true;
1897 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1899 ASSERT_TRUE(root->render_surface());
1900 ASSERT_TRUE(child2->render_surface());
1902 EXPECT_FALSE(root->is_clipped());
1903 EXPECT_TRUE(root->render_surface()->is_clipped());
1904 EXPECT_TRUE(parent->is_clipped());
1905 EXPECT_TRUE(child1->is_clipped());
1906 EXPECT_FALSE(child2->is_clipped());
1907 EXPECT_TRUE(child2->render_surface()->is_clipped());
1908 EXPECT_TRUE(grand_child->is_clipped());
1909 EXPECT_TRUE(leaf_node1->is_clipped());
1910 EXPECT_FALSE(leaf_node2->is_clipped());
1913 // Case 3: child2 masksToBounds. The layer and subtree are clipped, and
1914 // child2's render surface is not clipped.
1916 RenderSurfaceLayerList render_surface_layer_list;
1917 parent->SetMasksToBounds(false);
1918 child2->SetMasksToBounds(true);
1919 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
1920 root.get(), parent->bounds(), &render_surface_layer_list);
1921 inputs.can_adjust_raster_scales = true;
1922 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
1924 ASSERT_TRUE(root->render_surface());
1925 ASSERT_TRUE(child2->render_surface());
1927 EXPECT_FALSE(root->is_clipped());
1928 EXPECT_TRUE(root->render_surface()->is_clipped());
1929 EXPECT_FALSE(parent->is_clipped());
1930 EXPECT_FALSE(child1->is_clipped());
1931 EXPECT_TRUE(child2->is_clipped());
1932 EXPECT_FALSE(child2->render_surface()->is_clipped());
1933 EXPECT_FALSE(grand_child->is_clipped());
1934 EXPECT_FALSE(leaf_node1->is_clipped());
1935 EXPECT_TRUE(leaf_node2->is_clipped());
1939 TEST_F(LayerTreeHostCommonTest, DrawableContentRectForLayers) {
1940 // Verify that layers get the appropriate DrawableContentRect when their
1941 // parent masksToBounds is true.
1943 // grand_child1 - completely inside the region; DrawableContentRect should
1944 // be the layer rect expressed in target space.
1945 // grand_child2 - partially clipped but NOT masksToBounds; the clip rect
1946 // will be the intersection of layer bounds and the mask region.
1947 // grand_child3 - partially clipped and masksToBounds; the
1948 // DrawableContentRect will still be the intersection of layer bounds and
1949 // the mask region.
1950 // grand_child4 - outside parent's clip rect; the DrawableContentRect should
1951 // be empty.
1954 const gfx::Transform identity_matrix;
1955 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
1956 scoped_refptr<Layer> child = Layer::Create(layer_settings());
1957 scoped_refptr<Layer> grand_child1 = Layer::Create(layer_settings());
1958 scoped_refptr<Layer> grand_child2 = Layer::Create(layer_settings());
1959 scoped_refptr<Layer> grand_child3 = Layer::Create(layer_settings());
1960 scoped_refptr<Layer> grand_child4 = Layer::Create(layer_settings());
1962 parent->AddChild(child);
1963 child->AddChild(grand_child1);
1964 child->AddChild(grand_child2);
1965 child->AddChild(grand_child3);
1966 child->AddChild(grand_child4);
1968 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
1969 host->SetRootLayer(parent);
1971 SetLayerPropertiesForTesting(parent.get(),
1972 identity_matrix,
1973 gfx::Point3F(),
1974 gfx::PointF(),
1975 gfx::Size(500, 500),
1976 true,
1977 false);
1978 SetLayerPropertiesForTesting(child.get(),
1979 identity_matrix,
1980 gfx::Point3F(),
1981 gfx::PointF(),
1982 gfx::Size(20, 20),
1983 true,
1984 false);
1985 SetLayerPropertiesForTesting(grand_child1.get(),
1986 identity_matrix,
1987 gfx::Point3F(),
1988 gfx::PointF(5.f, 5.f),
1989 gfx::Size(10, 10),
1990 true,
1991 false);
1992 SetLayerPropertiesForTesting(grand_child2.get(),
1993 identity_matrix,
1994 gfx::Point3F(),
1995 gfx::PointF(15.f, 15.f),
1996 gfx::Size(10, 10),
1997 true,
1998 false);
1999 SetLayerPropertiesForTesting(grand_child3.get(),
2000 identity_matrix,
2001 gfx::Point3F(),
2002 gfx::PointF(15.f, 15.f),
2003 gfx::Size(10, 10),
2004 true,
2005 false);
2006 SetLayerPropertiesForTesting(grand_child4.get(),
2007 identity_matrix,
2008 gfx::Point3F(),
2009 gfx::PointF(45.f, 45.f),
2010 gfx::Size(10, 10),
2011 true,
2012 false);
2014 child->SetMasksToBounds(true);
2015 grand_child3->SetMasksToBounds(true);
2017 // Force everyone to be a render surface.
2018 child->SetOpacity(0.4f);
2019 grand_child1->SetOpacity(0.5f);
2020 grand_child2->SetOpacity(0.5f);
2021 grand_child3->SetOpacity(0.5f);
2022 grand_child4->SetOpacity(0.5f);
2024 RenderSurfaceLayerList render_surface_layer_list;
2025 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2026 parent.get(), parent->bounds(), &render_surface_layer_list);
2027 inputs.can_adjust_raster_scales = true;
2028 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2030 EXPECT_EQ(gfx::Rect(5, 5, 10, 10), grand_child1->drawable_content_rect());
2031 EXPECT_EQ(gfx::Rect(15, 15, 5, 5), grand_child3->drawable_content_rect());
2032 EXPECT_EQ(gfx::Rect(15, 15, 5, 5), grand_child3->drawable_content_rect());
2033 EXPECT_TRUE(grand_child4->drawable_content_rect().IsEmpty());
2036 TEST_F(LayerTreeHostCommonTest, ClipRectIsPropagatedCorrectlyToSurfaces) {
2037 // Verify that render surfaces (and their layers) get the appropriate
2038 // clip rects when their parent masksToBounds is true.
2040 // Layers that own render surfaces (at least for now) do not inherit any
2041 // clipping; instead the surface will enforce the clip for the entire subtree.
2042 // They may still have a clip rect of their own layer bounds, however, if
2043 // masksToBounds was true.
2044 const gfx::Transform identity_matrix;
2045 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
2046 scoped_refptr<Layer> child = Layer::Create(layer_settings());
2047 scoped_refptr<Layer> grand_child1 = Layer::Create(layer_settings());
2048 scoped_refptr<Layer> grand_child2 = Layer::Create(layer_settings());
2049 scoped_refptr<Layer> grand_child3 = Layer::Create(layer_settings());
2050 scoped_refptr<Layer> grand_child4 = Layer::Create(layer_settings());
2051 scoped_refptr<LayerWithForcedDrawsContent> leaf_node1 =
2052 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2053 scoped_refptr<LayerWithForcedDrawsContent> leaf_node2 =
2054 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2055 scoped_refptr<LayerWithForcedDrawsContent> leaf_node3 =
2056 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2057 scoped_refptr<LayerWithForcedDrawsContent> leaf_node4 =
2058 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2060 parent->AddChild(child);
2061 child->AddChild(grand_child1);
2062 child->AddChild(grand_child2);
2063 child->AddChild(grand_child3);
2064 child->AddChild(grand_child4);
2066 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2067 host->SetRootLayer(parent);
2069 // the leaf nodes ensure that these grand_children become render surfaces for
2070 // this test.
2071 grand_child1->AddChild(leaf_node1);
2072 grand_child2->AddChild(leaf_node2);
2073 grand_child3->AddChild(leaf_node3);
2074 grand_child4->AddChild(leaf_node4);
2076 SetLayerPropertiesForTesting(parent.get(),
2077 identity_matrix,
2078 gfx::Point3F(),
2079 gfx::PointF(),
2080 gfx::Size(500, 500),
2081 true,
2082 false);
2083 SetLayerPropertiesForTesting(child.get(),
2084 identity_matrix,
2085 gfx::Point3F(),
2086 gfx::PointF(),
2087 gfx::Size(20, 20),
2088 true,
2089 false);
2090 SetLayerPropertiesForTesting(grand_child1.get(),
2091 identity_matrix,
2092 gfx::Point3F(),
2093 gfx::PointF(5.f, 5.f),
2094 gfx::Size(10, 10),
2095 true,
2096 false);
2097 SetLayerPropertiesForTesting(grand_child2.get(),
2098 identity_matrix,
2099 gfx::Point3F(),
2100 gfx::PointF(15.f, 15.f),
2101 gfx::Size(10, 10),
2102 true,
2103 false);
2104 SetLayerPropertiesForTesting(grand_child3.get(),
2105 identity_matrix,
2106 gfx::Point3F(),
2107 gfx::PointF(15.f, 15.f),
2108 gfx::Size(10, 10),
2109 true,
2110 false);
2111 SetLayerPropertiesForTesting(grand_child4.get(),
2112 identity_matrix,
2113 gfx::Point3F(),
2114 gfx::PointF(45.f, 45.f),
2115 gfx::Size(10, 10),
2116 true,
2117 false);
2118 SetLayerPropertiesForTesting(leaf_node1.get(),
2119 identity_matrix,
2120 gfx::Point3F(),
2121 gfx::PointF(),
2122 gfx::Size(10, 10),
2123 true,
2124 false);
2125 SetLayerPropertiesForTesting(leaf_node2.get(),
2126 identity_matrix,
2127 gfx::Point3F(),
2128 gfx::PointF(),
2129 gfx::Size(10, 10),
2130 true,
2131 false);
2132 SetLayerPropertiesForTesting(leaf_node3.get(),
2133 identity_matrix,
2134 gfx::Point3F(),
2135 gfx::PointF(),
2136 gfx::Size(10, 10),
2137 true,
2138 false);
2139 SetLayerPropertiesForTesting(leaf_node4.get(),
2140 identity_matrix,
2141 gfx::Point3F(),
2142 gfx::PointF(),
2143 gfx::Size(10, 10),
2144 true,
2145 false);
2147 child->SetMasksToBounds(true);
2148 grand_child3->SetMasksToBounds(true);
2149 grand_child4->SetMasksToBounds(true);
2151 // Force everyone to be a render surface.
2152 child->SetOpacity(0.4f);
2153 child->SetForceRenderSurface(true);
2154 grand_child1->SetOpacity(0.5f);
2155 grand_child1->SetForceRenderSurface(true);
2156 grand_child2->SetOpacity(0.5f);
2157 grand_child2->SetForceRenderSurface(true);
2158 grand_child3->SetOpacity(0.5f);
2159 grand_child3->SetForceRenderSurface(true);
2160 grand_child4->SetOpacity(0.5f);
2161 grand_child4->SetForceRenderSurface(true);
2163 RenderSurfaceLayerList render_surface_layer_list;
2164 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2165 parent.get(), parent->bounds(), &render_surface_layer_list);
2166 inputs.can_adjust_raster_scales = true;
2167 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2168 ASSERT_TRUE(grand_child1->render_surface());
2169 ASSERT_TRUE(grand_child2->render_surface());
2170 ASSERT_TRUE(grand_child3->render_surface());
2172 // Surfaces are clipped by their parent, but un-affected by the owning layer's
2173 // masksToBounds.
2174 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2175 grand_child1->render_surface()->clip_rect());
2176 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2177 grand_child2->render_surface()->clip_rect());
2178 EXPECT_EQ(gfx::Rect(0, 0, 20, 20),
2179 grand_child3->render_surface()->clip_rect());
2182 TEST_F(LayerTreeHostCommonTest, AnimationsForRenderSurfaceHierarchy) {
2183 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
2184 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
2185 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
2186 scoped_refptr<Layer> child_of_root = Layer::Create(layer_settings());
2187 scoped_refptr<Layer> child_of_rs1 = Layer::Create(layer_settings());
2188 scoped_refptr<Layer> child_of_rs2 = Layer::Create(layer_settings());
2189 scoped_refptr<Layer> grand_child_of_root = Layer::Create(layer_settings());
2190 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs1 =
2191 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2192 scoped_refptr<LayerWithForcedDrawsContent> grand_child_of_rs2 =
2193 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2194 parent->AddChild(render_surface1);
2195 parent->AddChild(child_of_root);
2196 render_surface1->AddChild(child_of_rs1);
2197 render_surface1->AddChild(render_surface2);
2198 render_surface2->AddChild(child_of_rs2);
2199 child_of_root->AddChild(grand_child_of_root);
2200 child_of_rs1->AddChild(grand_child_of_rs1);
2201 child_of_rs2->AddChild(grand_child_of_rs2);
2203 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2204 host->SetRootLayer(parent);
2206 // Make our render surfaces.
2207 render_surface1->SetForceRenderSurface(true);
2208 render_surface2->SetForceRenderSurface(true);
2210 gfx::Transform layer_transform;
2211 layer_transform.Translate(1.0, 1.0);
2213 SetLayerPropertiesForTesting(parent.get(),
2214 layer_transform,
2215 gfx::Point3F(0.25f, 0.f, 0.f),
2216 gfx::PointF(2.5f, 0.f),
2217 gfx::Size(10, 10),
2218 true,
2219 false);
2220 SetLayerPropertiesForTesting(render_surface1.get(),
2221 layer_transform,
2222 gfx::Point3F(0.25f, 0.f, 0.f),
2223 gfx::PointF(2.5f, 0.f),
2224 gfx::Size(10, 10),
2225 true,
2226 false);
2227 SetLayerPropertiesForTesting(render_surface2.get(),
2228 layer_transform,
2229 gfx::Point3F(0.25f, 0.f, 0.f),
2230 gfx::PointF(2.5f, 0.f),
2231 gfx::Size(10, 10),
2232 true,
2233 false);
2234 SetLayerPropertiesForTesting(child_of_root.get(),
2235 layer_transform,
2236 gfx::Point3F(0.25f, 0.f, 0.f),
2237 gfx::PointF(2.5f, 0.f),
2238 gfx::Size(10, 10),
2239 true,
2240 false);
2241 SetLayerPropertiesForTesting(child_of_rs1.get(),
2242 layer_transform,
2243 gfx::Point3F(0.25f, 0.f, 0.f),
2244 gfx::PointF(2.5f, 0.f),
2245 gfx::Size(10, 10),
2246 true,
2247 false);
2248 SetLayerPropertiesForTesting(child_of_rs2.get(),
2249 layer_transform,
2250 gfx::Point3F(0.25f, 0.f, 0.f),
2251 gfx::PointF(2.5f, 0.f),
2252 gfx::Size(10, 10),
2253 true,
2254 false);
2255 SetLayerPropertiesForTesting(grand_child_of_root.get(),
2256 layer_transform,
2257 gfx::Point3F(0.25f, 0.f, 0.f),
2258 gfx::PointF(2.5f, 0.f),
2259 gfx::Size(10, 10),
2260 true,
2261 false);
2262 SetLayerPropertiesForTesting(grand_child_of_rs1.get(),
2263 layer_transform,
2264 gfx::Point3F(0.25f, 0.f, 0.f),
2265 gfx::PointF(2.5f, 0.f),
2266 gfx::Size(10, 10),
2267 true,
2268 false);
2269 SetLayerPropertiesForTesting(grand_child_of_rs2.get(),
2270 layer_transform,
2271 gfx::Point3F(0.25f, 0.f, 0.f),
2272 gfx::PointF(2.5f, 0.f),
2273 gfx::Size(10, 10),
2274 true,
2275 false);
2277 // Put an animated opacity on the render surface.
2278 AddOpacityTransitionToController(
2279 render_surface1->layer_animation_controller(), 10.0, 1.f, 0.f, false);
2281 // Also put an animated opacity on a layer without descendants.
2282 AddOpacityTransitionToController(
2283 grand_child_of_root->layer_animation_controller(), 10.0, 1.f, 0.f, false);
2285 // Put a transform animation on the render surface.
2286 AddAnimatedTransformToController(
2287 render_surface2->layer_animation_controller(), 10.0, 30, 0);
2289 // Also put transform animations on grand_child_of_root, and
2290 // grand_child_of_rs2
2291 AddAnimatedTransformToController(
2292 grand_child_of_root->layer_animation_controller(), 10.0, 30, 0);
2293 AddAnimatedTransformToController(
2294 grand_child_of_rs2->layer_animation_controller(), 10.0, 30, 0);
2296 ExecuteCalculateDrawProperties(parent.get());
2298 // Only layers that are associated with render surfaces should have an actual
2299 // RenderSurface() value.
2300 ASSERT_TRUE(parent->render_surface());
2301 ASSERT_FALSE(child_of_root->render_surface());
2302 ASSERT_FALSE(grand_child_of_root->render_surface());
2304 ASSERT_TRUE(render_surface1->render_surface());
2305 ASSERT_FALSE(child_of_rs1->render_surface());
2306 ASSERT_FALSE(grand_child_of_rs1->render_surface());
2308 ASSERT_TRUE(render_surface2->render_surface());
2309 ASSERT_FALSE(child_of_rs2->render_surface());
2310 ASSERT_FALSE(grand_child_of_rs2->render_surface());
2312 // Verify all render target accessors
2313 EXPECT_EQ(parent.get(), parent->render_target());
2314 EXPECT_EQ(parent.get(), child_of_root->render_target());
2315 EXPECT_EQ(parent.get(), grand_child_of_root->render_target());
2317 EXPECT_EQ(render_surface1.get(), render_surface1->render_target());
2318 EXPECT_EQ(render_surface1.get(), child_of_rs1->render_target());
2319 EXPECT_EQ(render_surface1.get(), grand_child_of_rs1->render_target());
2321 EXPECT_EQ(render_surface2.get(), render_surface2->render_target());
2322 EXPECT_EQ(render_surface2.get(), child_of_rs2->render_target());
2323 EXPECT_EQ(render_surface2.get(), grand_child_of_rs2->render_target());
2325 // Verify draw_opacity_is_animating values
2326 EXPECT_FALSE(parent->draw_opacity_is_animating());
2327 EXPECT_FALSE(child_of_root->draw_opacity_is_animating());
2328 EXPECT_TRUE(grand_child_of_root->draw_opacity_is_animating());
2329 EXPECT_FALSE(render_surface1->draw_opacity_is_animating());
2330 EXPECT_TRUE(render_surface1->render_surface()->draw_opacity_is_animating());
2331 EXPECT_FALSE(child_of_rs1->draw_opacity_is_animating());
2332 EXPECT_FALSE(grand_child_of_rs1->draw_opacity_is_animating());
2333 EXPECT_FALSE(render_surface2->draw_opacity_is_animating());
2334 EXPECT_FALSE(render_surface2->render_surface()->draw_opacity_is_animating());
2335 EXPECT_FALSE(child_of_rs2->draw_opacity_is_animating());
2336 EXPECT_FALSE(grand_child_of_rs2->draw_opacity_is_animating());
2338 // Verify draw_transform_is_animating values
2339 EXPECT_FALSE(parent->draw_transform_is_animating());
2340 EXPECT_FALSE(child_of_root->draw_transform_is_animating());
2341 EXPECT_TRUE(grand_child_of_root->draw_transform_is_animating());
2342 EXPECT_FALSE(render_surface1->draw_transform_is_animating());
2343 EXPECT_FALSE(render_surface1->render_surface()
2344 ->target_surface_transforms_are_animating());
2345 EXPECT_FALSE(child_of_rs1->draw_transform_is_animating());
2346 EXPECT_FALSE(grand_child_of_rs1->draw_transform_is_animating());
2347 EXPECT_FALSE(render_surface2->draw_transform_is_animating());
2348 EXPECT_TRUE(render_surface2->render_surface()
2349 ->target_surface_transforms_are_animating());
2350 EXPECT_FALSE(child_of_rs2->draw_transform_is_animating());
2351 EXPECT_TRUE(grand_child_of_rs2->draw_transform_is_animating());
2353 // Verify screen_space_transform_is_animating values
2354 EXPECT_FALSE(parent->screen_space_transform_is_animating());
2355 EXPECT_FALSE(child_of_root->screen_space_transform_is_animating());
2356 EXPECT_TRUE(grand_child_of_root->screen_space_transform_is_animating());
2357 EXPECT_FALSE(render_surface1->screen_space_transform_is_animating());
2358 EXPECT_FALSE(render_surface1->render_surface()
2359 ->screen_space_transforms_are_animating());
2360 EXPECT_FALSE(child_of_rs1->screen_space_transform_is_animating());
2361 EXPECT_FALSE(grand_child_of_rs1->screen_space_transform_is_animating());
2362 EXPECT_TRUE(render_surface2->screen_space_transform_is_animating());
2363 EXPECT_TRUE(render_surface2->render_surface()
2364 ->screen_space_transforms_are_animating());
2365 EXPECT_TRUE(child_of_rs2->screen_space_transform_is_animating());
2366 EXPECT_TRUE(grand_child_of_rs2->screen_space_transform_is_animating());
2368 // Sanity check. If these fail there is probably a bug in the test itself.
2369 // It is expected that we correctly set up transforms so that the y-component
2370 // of the screen-space transform encodes the "depth" of the layer in the tree.
2371 EXPECT_FLOAT_EQ(1.0, parent->screen_space_transform().matrix().get(1, 3));
2372 EXPECT_FLOAT_EQ(2.0,
2373 child_of_root->screen_space_transform().matrix().get(1, 3));
2374 EXPECT_FLOAT_EQ(
2375 3.0, grand_child_of_root->screen_space_transform().matrix().get(1, 3));
2377 EXPECT_FLOAT_EQ(2.0,
2378 render_surface1->screen_space_transform().matrix().get(1, 3));
2379 EXPECT_FLOAT_EQ(3.0,
2380 child_of_rs1->screen_space_transform().matrix().get(1, 3));
2381 EXPECT_FLOAT_EQ(
2382 4.0, grand_child_of_rs1->screen_space_transform().matrix().get(1, 3));
2384 EXPECT_FLOAT_EQ(3.0,
2385 render_surface2->screen_space_transform().matrix().get(1, 3));
2386 EXPECT_FLOAT_EQ(4.0,
2387 child_of_rs2->screen_space_transform().matrix().get(1, 3));
2388 EXPECT_FLOAT_EQ(
2389 5.0, grand_child_of_rs2->screen_space_transform().matrix().get(1, 3));
2392 TEST_F(LayerTreeHostCommonTest, VisibleRectForIdentityTransform) {
2393 // Test the calculateVisibleRect() function works correctly for identity
2394 // transforms.
2396 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2397 gfx::Transform layer_to_surface_transform;
2399 // Case 1: Layer is contained within the surface.
2400 gfx::Rect layer_content_rect = gfx::Rect(10, 10, 30, 30);
2401 gfx::Rect expected = gfx::Rect(10, 10, 30, 30);
2402 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2403 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2404 EXPECT_EQ(expected, actual);
2406 // Case 2: Layer is outside the surface rect.
2407 layer_content_rect = gfx::Rect(120, 120, 30, 30);
2408 actual = LayerTreeHostCommon::CalculateVisibleRect(
2409 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2410 EXPECT_TRUE(actual.IsEmpty());
2412 // Case 3: Layer is partially overlapping the surface rect.
2413 layer_content_rect = gfx::Rect(80, 80, 30, 30);
2414 expected = gfx::Rect(80, 80, 20, 20);
2415 actual = LayerTreeHostCommon::CalculateVisibleRect(
2416 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2417 EXPECT_EQ(expected, actual);
2420 TEST_F(LayerTreeHostCommonTest, VisibleRectForTranslations) {
2421 // Test the calculateVisibleRect() function works correctly for scaling
2422 // transforms.
2424 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2425 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
2426 gfx::Transform layer_to_surface_transform;
2428 // Case 1: Layer is contained within the surface.
2429 layer_to_surface_transform.MakeIdentity();
2430 layer_to_surface_transform.Translate(10.0, 10.0);
2431 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
2432 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2433 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2434 EXPECT_EQ(expected, actual);
2436 // Case 2: Layer is outside the surface rect.
2437 layer_to_surface_transform.MakeIdentity();
2438 layer_to_surface_transform.Translate(120.0, 120.0);
2439 actual = LayerTreeHostCommon::CalculateVisibleRect(
2440 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2441 EXPECT_TRUE(actual.IsEmpty());
2443 // Case 3: Layer is partially overlapping the surface rect.
2444 layer_to_surface_transform.MakeIdentity();
2445 layer_to_surface_transform.Translate(80.0, 80.0);
2446 expected = gfx::Rect(0, 0, 20, 20);
2447 actual = LayerTreeHostCommon::CalculateVisibleRect(
2448 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2449 EXPECT_EQ(expected, actual);
2452 TEST_F(LayerTreeHostCommonTest, VisibleRectFor2DRotations) {
2453 // Test the calculateVisibleRect() function works correctly for rotations
2454 // about z-axis (i.e. 2D rotations). Remember that calculateVisibleRect()
2455 // should return the g in the layer's space.
2457 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2458 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 30, 30);
2459 gfx::Transform layer_to_surface_transform;
2461 // Case 1: Layer is contained within the surface.
2462 layer_to_surface_transform.MakeIdentity();
2463 layer_to_surface_transform.Translate(50.0, 50.0);
2464 layer_to_surface_transform.Rotate(45.0);
2465 gfx::Rect expected = gfx::Rect(0, 0, 30, 30);
2466 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2467 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2468 EXPECT_EQ(expected, actual);
2470 // Case 2: Layer is outside the surface rect.
2471 layer_to_surface_transform.MakeIdentity();
2472 layer_to_surface_transform.Translate(-50.0, 0.0);
2473 layer_to_surface_transform.Rotate(45.0);
2474 actual = LayerTreeHostCommon::CalculateVisibleRect(
2475 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2476 EXPECT_TRUE(actual.IsEmpty());
2478 // Case 3: The layer is rotated about its top-left corner. In surface space,
2479 // the layer is oriented diagonally, with the left half outside of the render
2480 // surface. In this case, the g should still be the entire layer
2481 // (remember the g is computed in layer space); both the top-left
2482 // and bottom-right corners of the layer are still visible.
2483 layer_to_surface_transform.MakeIdentity();
2484 layer_to_surface_transform.Rotate(45.0);
2485 expected = gfx::Rect(0, 0, 30, 30);
2486 actual = LayerTreeHostCommon::CalculateVisibleRect(
2487 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2488 EXPECT_EQ(expected, actual);
2490 // Case 4: The layer is rotated about its top-left corner, and translated
2491 // upwards. In surface space, the layer is oriented diagonally, with only the
2492 // top corner of the surface overlapping the layer. In layer space, the render
2493 // surface overlaps the right side of the layer. The g should be
2494 // the layer's right half.
2495 layer_to_surface_transform.MakeIdentity();
2496 layer_to_surface_transform.Translate(0.0, -sqrt(2.0) * 15.0);
2497 layer_to_surface_transform.Rotate(45.0);
2498 expected = gfx::Rect(15, 0, 15, 30); // Right half of layer bounds.
2499 actual = LayerTreeHostCommon::CalculateVisibleRect(
2500 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2501 EXPECT_EQ(expected, actual);
2504 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dOrthographicTransform) {
2505 // Test that the calculateVisibleRect() function works correctly for 3d
2506 // transforms.
2508 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2509 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
2510 gfx::Transform layer_to_surface_transform;
2512 // Case 1: Orthographic projection of a layer rotated about y-axis by 45
2513 // degrees, should be fully contained in the render surface.
2514 layer_to_surface_transform.MakeIdentity();
2515 layer_to_surface_transform.RotateAboutYAxis(45.0);
2516 gfx::Rect expected = gfx::Rect(0, 0, 100, 100);
2517 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2518 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2519 EXPECT_EQ(expected, actual);
2521 // Case 2: Orthographic projection of a layer rotated about y-axis by 45
2522 // degrees, but shifted to the side so only the right-half the layer would be
2523 // visible on the surface.
2524 // 100 is the un-rotated layer width; divided by sqrt(2) is the rotated width.
2525 SkMScalar half_width_of_rotated_layer =
2526 SkDoubleToMScalar((100.0 / sqrt(2.0)) * 0.5);
2527 layer_to_surface_transform.MakeIdentity();
2528 layer_to_surface_transform.Translate(-half_width_of_rotated_layer, 0.0);
2529 layer_to_surface_transform.RotateAboutYAxis(45.0); // Rotates about the left
2530 // edge of the layer.
2531 expected = gfx::Rect(50, 0, 50, 100); // Tight half of the layer.
2532 actual = LayerTreeHostCommon::CalculateVisibleRect(
2533 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2534 EXPECT_EQ(expected, actual);
2537 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveTransform) {
2538 // Test the calculateVisibleRect() function works correctly when the layer has
2539 // a perspective projection onto the target surface.
2541 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2542 gfx::Rect layer_content_rect = gfx::Rect(-50, -50, 200, 200);
2543 gfx::Transform layer_to_surface_transform;
2545 // Case 1: Even though the layer is twice as large as the surface, due to
2546 // perspective foreshortening, the layer will fit fully in the surface when
2547 // its translated more than the perspective amount.
2548 layer_to_surface_transform.MakeIdentity();
2550 // The following sequence of transforms applies the perspective about the
2551 // center of the surface.
2552 layer_to_surface_transform.Translate(50.0, 50.0);
2553 layer_to_surface_transform.ApplyPerspectiveDepth(9.0);
2554 layer_to_surface_transform.Translate(-50.0, -50.0);
2556 // This translate places the layer in front of the surface's projection plane.
2557 layer_to_surface_transform.Translate3d(0.0, 0.0, -27.0);
2559 gfx::Rect expected = gfx::Rect(-50, -50, 200, 200);
2560 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2561 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2562 EXPECT_EQ(expected, actual);
2564 // Case 2: same projection as before, except that the layer is also translated
2565 // to the side, so that only the right half of the layer should be visible.
2567 // Explanation of expected result: The perspective ratio is (z distance
2568 // between layer and camera origin) / (z distance between projection plane and
2569 // camera origin) == ((-27 - 9) / 9) Then, by similar triangles, if we want to
2570 // move a layer by translating -50 units in projected surface units (so that
2571 // only half of it is visible), then we would need to translate by (-36 / 9) *
2572 // -50 == -200 in the layer's units.
2573 layer_to_surface_transform.Translate3d(-200.0, 0.0, 0.0);
2574 expected = gfx::Rect(gfx::Point(50, -50),
2575 gfx::Size(100, 200)); // The right half of the layer's
2576 // bounding rect.
2577 actual = LayerTreeHostCommon::CalculateVisibleRect(
2578 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2579 EXPECT_EQ(expected, actual);
2582 TEST_F(LayerTreeHostCommonTest,
2583 VisibleRectFor3dOrthographicIsNotClippedBehindSurface) {
2584 // There is currently no explicit concept of an orthographic projection plane
2585 // in our code (nor in the CSS spec to my knowledge). Therefore, layers that
2586 // are technically behind the surface in an orthographic world should not be
2587 // clipped when they are flattened to the surface.
2589 gfx::Rect target_surface_rect = gfx::Rect(0, 0, 100, 100);
2590 gfx::Rect layer_content_rect = gfx::Rect(0, 0, 100, 100);
2591 gfx::Transform layer_to_surface_transform;
2593 // This sequence of transforms effectively rotates the layer about the y-axis
2594 // at the center of the layer.
2595 layer_to_surface_transform.MakeIdentity();
2596 layer_to_surface_transform.Translate(50.0, 0.0);
2597 layer_to_surface_transform.RotateAboutYAxis(45.0);
2598 layer_to_surface_transform.Translate(-50.0, 0.0);
2600 gfx::Rect expected = gfx::Rect(0, 0, 100, 100);
2601 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2602 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2603 EXPECT_EQ(expected, actual);
2606 TEST_F(LayerTreeHostCommonTest, VisibleRectFor3dPerspectiveWhenClippedByW) {
2607 // Test the calculateVisibleRect() function works correctly when projecting a
2608 // surface onto a layer, but the layer is partially behind the camera (not
2609 // just behind the projection plane). In this case, the cartesian coordinates
2610 // may seem to be valid, but actually they are not. The visible rect needs to
2611 // be properly clipped by the w = 0 plane in homogeneous coordinates before
2612 // converting to cartesian coordinates.
2614 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100);
2615 gfx::Rect layer_content_rect = gfx::Rect(-10, -1, 20, 2);
2616 gfx::Transform layer_to_surface_transform;
2618 // The layer is positioned so that the right half of the layer should be in
2619 // front of the camera, while the other half is behind the surface's
2620 // projection plane. The following sequence of transforms applies the
2621 // perspective and rotation about the center of the layer.
2622 layer_to_surface_transform.MakeIdentity();
2623 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2624 layer_to_surface_transform.Translate3d(-2.0, 0.0, 1.0);
2625 layer_to_surface_transform.RotateAboutYAxis(45.0);
2627 // Sanity check that this transform does indeed cause w < 0 when applying the
2628 // transform, otherwise this code is not testing the intended scenario.
2629 bool clipped;
2630 MathUtil::MapQuad(layer_to_surface_transform,
2631 gfx::QuadF(gfx::RectF(layer_content_rect)),
2632 &clipped);
2633 ASSERT_TRUE(clipped);
2635 int expected_x_position = 0;
2636 int expected_width = 10;
2637 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2638 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2639 EXPECT_EQ(expected_x_position, actual.x());
2640 EXPECT_EQ(expected_width, actual.width());
2643 TEST_F(LayerTreeHostCommonTest, VisibleRectForPerspectiveUnprojection) {
2644 // To determine visible rect in layer space, there needs to be an
2645 // un-projection from surface space to layer space. When the original
2646 // transform was a perspective projection that was clipped, it returns a rect
2647 // that encloses the clipped bounds. Un-projecting this new rect may require
2648 // clipping again.
2650 // This sequence of transforms causes one corner of the layer to protrude
2651 // across the w = 0 plane, and should be clipped.
2652 gfx::Rect target_surface_rect = gfx::Rect(-50, -50, 100, 100);
2653 gfx::Rect layer_content_rect = gfx::Rect(-10, -10, 20, 20);
2654 gfx::Transform layer_to_surface_transform;
2655 layer_to_surface_transform.MakeIdentity();
2656 layer_to_surface_transform.ApplyPerspectiveDepth(1.0);
2657 layer_to_surface_transform.Translate3d(0.0, 0.0, -5.0);
2658 layer_to_surface_transform.RotateAboutYAxis(45.0);
2659 layer_to_surface_transform.RotateAboutXAxis(80.0);
2661 // Sanity check that un-projection does indeed cause w < 0, otherwise this
2662 // code is not testing the intended scenario.
2663 bool clipped;
2664 gfx::RectF clipped_rect =
2665 MathUtil::MapClippedRect(layer_to_surface_transform, layer_content_rect);
2666 MathUtil::ProjectQuad(
2667 Inverse(layer_to_surface_transform), gfx::QuadF(clipped_rect), &clipped);
2668 ASSERT_TRUE(clipped);
2670 // Only the corner of the layer is not visible on the surface because of being
2671 // clipped. But, the net result of rounding visible region to an axis-aligned
2672 // rect is that the entire layer should still be considered visible.
2673 gfx::Rect expected = gfx::Rect(-10, -10, 20, 20);
2674 gfx::Rect actual = LayerTreeHostCommon::CalculateVisibleRect(
2675 target_surface_rect, layer_content_rect, layer_to_surface_transform);
2676 EXPECT_EQ(expected, actual);
2679 TEST_F(LayerTreeHostCommonTest, DrawableAndVisibleContentRectsForSimpleLayers) {
2680 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2681 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2682 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2683 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2684 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2685 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2686 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2687 root->AddChild(child1);
2688 root->AddChild(child2);
2689 root->AddChild(child3);
2691 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2692 host->SetRootLayer(root);
2694 gfx::Transform identity_matrix;
2695 SetLayerPropertiesForTesting(root.get(),
2696 identity_matrix,
2697 gfx::Point3F(),
2698 gfx::PointF(),
2699 gfx::Size(100, 100),
2700 true,
2701 false);
2702 SetLayerPropertiesForTesting(child1.get(),
2703 identity_matrix,
2704 gfx::Point3F(),
2705 gfx::PointF(),
2706 gfx::Size(50, 50),
2707 true,
2708 false);
2709 SetLayerPropertiesForTesting(child2.get(),
2710 identity_matrix,
2711 gfx::Point3F(),
2712 gfx::PointF(75.f, 75.f),
2713 gfx::Size(50, 50),
2714 true,
2715 false);
2716 SetLayerPropertiesForTesting(child3.get(),
2717 identity_matrix,
2718 gfx::Point3F(),
2719 gfx::PointF(125.f, 125.f),
2720 gfx::Size(50, 50),
2721 true,
2722 false);
2724 ExecuteCalculateDrawProperties(root.get());
2726 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2727 root->render_surface()->DrawableContentRect());
2728 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2730 // Layers that do not draw content should have empty visible_content_rects.
2731 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2733 // layer visible_content_rects are clipped by their target surface.
2734 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
2735 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_content_rect());
2736 EXPECT_TRUE(child3->visible_content_rect().IsEmpty());
2738 // layer drawable_content_rects are not clipped.
2739 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->drawable_content_rect());
2740 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
2741 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
2744 TEST_F(LayerTreeHostCommonTest,
2745 DrawableAndVisibleContentRectsForLayersClippedByLayer) {
2746 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2747 scoped_refptr<Layer> child = Layer::Create(layer_settings());
2748 scoped_refptr<LayerWithForcedDrawsContent> grand_child1 =
2749 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2750 scoped_refptr<LayerWithForcedDrawsContent> grand_child2 =
2751 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2752 scoped_refptr<LayerWithForcedDrawsContent> grand_child3 =
2753 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2754 root->AddChild(child);
2755 child->AddChild(grand_child1);
2756 child->AddChild(grand_child2);
2757 child->AddChild(grand_child3);
2759 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2760 host->SetRootLayer(root);
2762 gfx::Transform identity_matrix;
2763 SetLayerPropertiesForTesting(root.get(),
2764 identity_matrix,
2765 gfx::Point3F(),
2766 gfx::PointF(),
2767 gfx::Size(100, 100),
2768 true,
2769 false);
2770 SetLayerPropertiesForTesting(child.get(),
2771 identity_matrix,
2772 gfx::Point3F(),
2773 gfx::PointF(),
2774 gfx::Size(100, 100),
2775 true,
2776 false);
2777 SetLayerPropertiesForTesting(grand_child1.get(),
2778 identity_matrix,
2779 gfx::Point3F(),
2780 gfx::PointF(5.f, 5.f),
2781 gfx::Size(50, 50),
2782 true,
2783 false);
2784 SetLayerPropertiesForTesting(grand_child2.get(),
2785 identity_matrix,
2786 gfx::Point3F(),
2787 gfx::PointF(75.f, 75.f),
2788 gfx::Size(50, 50),
2789 true,
2790 false);
2791 SetLayerPropertiesForTesting(grand_child3.get(),
2792 identity_matrix,
2793 gfx::Point3F(),
2794 gfx::PointF(125.f, 125.f),
2795 gfx::Size(50, 50),
2796 true,
2797 false);
2799 child->SetMasksToBounds(true);
2800 ExecuteCalculateDrawProperties(root.get());
2802 ASSERT_FALSE(child->render_surface());
2804 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2805 root->render_surface()->DrawableContentRect());
2806 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2808 // Layers that do not draw content should have empty visible content rects.
2809 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2810 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), child->visible_content_rect());
2812 // All grandchild visible content rects should be clipped by child.
2813 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), grand_child1->visible_content_rect());
2814 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), grand_child2->visible_content_rect());
2815 EXPECT_TRUE(grand_child3->visible_content_rect().IsEmpty());
2817 // All grandchild DrawableContentRects should also be clipped by child.
2818 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), grand_child1->drawable_content_rect());
2819 EXPECT_EQ(gfx::Rect(75, 75, 25, 25), grand_child2->drawable_content_rect());
2820 EXPECT_TRUE(grand_child3->drawable_content_rect().IsEmpty());
2823 TEST_F(LayerTreeHostCommonTest, VisibleContentRectWithClippingAndScaling) {
2824 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2825 scoped_refptr<Layer> child = Layer::Create(layer_settings());
2826 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
2827 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2828 root->AddChild(child);
2829 child->AddChild(grand_child);
2831 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2832 host->SetRootLayer(root);
2834 gfx::Transform identity_matrix;
2835 gfx::Transform child_scale_matrix;
2836 child_scale_matrix.Scale(0.25f, 0.25f);
2837 gfx::Transform grand_child_scale_matrix;
2838 grand_child_scale_matrix.Scale(0.246f, 0.246f);
2839 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
2840 gfx::PointF(), gfx::Size(100, 100), true, false);
2841 SetLayerPropertiesForTesting(child.get(), child_scale_matrix, gfx::Point3F(),
2842 gfx::PointF(), gfx::Size(10, 10), true, false);
2843 SetLayerPropertiesForTesting(grand_child.get(), grand_child_scale_matrix,
2844 gfx::Point3F(), gfx::PointF(),
2845 gfx::Size(100, 100), true, false);
2847 child->SetMasksToBounds(true);
2848 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
2850 // The visible rect is expanded to integer coordinates in target space before
2851 // being projected back to layer space, where it is once again expanded to
2852 // integer coordinates.
2853 EXPECT_EQ(gfx::Rect(49, 49), grand_child->visible_rect_from_property_trees());
2856 TEST_F(LayerTreeHostCommonTest,
2857 DrawableAndVisibleContentRectsForLayersInUnclippedRenderSurface) {
2858 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2859 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
2860 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2861 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2862 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2863 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2864 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2865 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2866 root->AddChild(render_surface1);
2867 render_surface1->AddChild(child1);
2868 render_surface1->AddChild(child2);
2869 render_surface1->AddChild(child3);
2871 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2872 host->SetRootLayer(root);
2874 gfx::Transform identity_matrix;
2875 SetLayerPropertiesForTesting(root.get(),
2876 identity_matrix,
2877 gfx::Point3F(),
2878 gfx::PointF(),
2879 gfx::Size(100, 100),
2880 true,
2881 false);
2882 SetLayerPropertiesForTesting(render_surface1.get(),
2883 identity_matrix,
2884 gfx::Point3F(),
2885 gfx::PointF(),
2886 gfx::Size(3, 4),
2887 true,
2888 false);
2889 SetLayerPropertiesForTesting(child1.get(),
2890 identity_matrix,
2891 gfx::Point3F(),
2892 gfx::PointF(5.f, 5.f),
2893 gfx::Size(50, 50),
2894 true,
2895 false);
2896 SetLayerPropertiesForTesting(child2.get(),
2897 identity_matrix,
2898 gfx::Point3F(),
2899 gfx::PointF(75.f, 75.f),
2900 gfx::Size(50, 50),
2901 true,
2902 false);
2903 SetLayerPropertiesForTesting(child3.get(),
2904 identity_matrix,
2905 gfx::Point3F(),
2906 gfx::PointF(125.f, 125.f),
2907 gfx::Size(50, 50),
2908 true,
2909 false);
2911 render_surface1->SetForceRenderSurface(true);
2912 ExecuteCalculateDrawProperties(root.get());
2914 ASSERT_TRUE(render_surface1->render_surface());
2916 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
2917 root->render_surface()->DrawableContentRect());
2918 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
2920 // Layers that do not draw content should have empty visible content rects.
2921 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
2922 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
2924 // An unclipped surface grows its DrawableContentRect to include all drawable
2925 // regions of the subtree.
2926 EXPECT_EQ(gfx::Rect(5, 5, 170, 170),
2927 render_surface1->render_surface()->DrawableContentRect());
2929 // All layers that draw content into the unclipped surface are also unclipped.
2930 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
2931 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
2932 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
2934 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
2935 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
2936 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
2939 TEST_F(LayerTreeHostCommonTest,
2940 VisibleContentRectsForClippedSurfaceWithEmptyClip) {
2941 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2942 scoped_refptr<LayerWithForcedDrawsContent> child1 =
2943 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2944 scoped_refptr<LayerWithForcedDrawsContent> child2 =
2945 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2946 scoped_refptr<LayerWithForcedDrawsContent> child3 =
2947 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2948 root->AddChild(child1);
2949 root->AddChild(child2);
2950 root->AddChild(child3);
2952 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2953 host->SetRootLayer(root);
2955 gfx::Transform identity_matrix;
2956 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
2957 gfx::PointF(), gfx::Size(100, 100), true, false);
2958 SetLayerPropertiesForTesting(child1.get(), identity_matrix, gfx::Point3F(),
2959 gfx::PointF(5.f, 5.f), gfx::Size(50, 50), true,
2960 false);
2961 SetLayerPropertiesForTesting(child2.get(), identity_matrix, gfx::Point3F(),
2962 gfx::PointF(75.f, 75.f), gfx::Size(50, 50), true,
2963 false);
2964 SetLayerPropertiesForTesting(child3.get(), identity_matrix, gfx::Point3F(),
2965 gfx::PointF(125.f, 125.f), gfx::Size(50, 50),
2966 true, false);
2968 RenderSurfaceLayerList render_surface_layer_list;
2969 // Now set the root render surface an empty clip.
2970 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
2971 root.get(), gfx::Size(), &render_surface_layer_list);
2973 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
2974 ASSERT_TRUE(root->render_surface());
2975 EXPECT_FALSE(root->is_clipped());
2977 gfx::Rect empty;
2978 EXPECT_EQ(empty, root->render_surface()->clip_rect());
2979 EXPECT_TRUE(root->render_surface()->is_clipped());
2981 // Visible content rect calculation will check if the target surface is
2982 // clipped or not. An empty clip rect does not indicate the render surface
2983 // is unclipped.
2984 EXPECT_EQ(empty, child1->visible_content_rect());
2985 EXPECT_EQ(empty, child2->visible_content_rect());
2986 EXPECT_EQ(empty, child3->visible_content_rect());
2989 TEST_F(LayerTreeHostCommonTest,
2990 DrawableAndVisibleContentRectsForLayersWithUninvertibleTransform) {
2991 scoped_refptr<Layer> root = Layer::Create(layer_settings());
2992 scoped_refptr<LayerWithForcedDrawsContent> child =
2993 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
2994 root->AddChild(child);
2996 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
2997 host->SetRootLayer(root);
2999 // Case 1: a truly degenerate matrix
3000 gfx::Transform identity_matrix;
3001 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
3002 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3004 SetLayerPropertiesForTesting(root.get(),
3005 identity_matrix,
3006 gfx::Point3F(),
3007 gfx::PointF(),
3008 gfx::Size(100, 100),
3009 true,
3010 false);
3011 SetLayerPropertiesForTesting(child.get(),
3012 uninvertible_matrix,
3013 gfx::Point3F(),
3014 gfx::PointF(5.f, 5.f),
3015 gfx::Size(50, 50),
3016 true,
3017 false);
3019 ExecuteCalculateDrawProperties(root.get());
3021 EXPECT_TRUE(child->visible_content_rect().IsEmpty());
3022 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3024 // Case 2: a matrix with flattened z, uninvertible and not visible according
3025 // to the CSS spec.
3026 uninvertible_matrix.MakeIdentity();
3027 uninvertible_matrix.matrix().set(2, 2, 0.0);
3028 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3030 SetLayerPropertiesForTesting(child.get(),
3031 uninvertible_matrix,
3032 gfx::Point3F(),
3033 gfx::PointF(5.f, 5.f),
3034 gfx::Size(50, 50),
3035 true,
3036 false);
3038 ExecuteCalculateDrawProperties(root.get());
3040 EXPECT_TRUE(child->visible_content_rect().IsEmpty());
3041 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3043 // Case 3: a matrix with flattened z, also uninvertible and not visible.
3044 uninvertible_matrix.MakeIdentity();
3045 uninvertible_matrix.Translate(500.0, 0.0);
3046 uninvertible_matrix.matrix().set(2, 2, 0.0);
3047 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3049 SetLayerPropertiesForTesting(child.get(),
3050 uninvertible_matrix,
3051 gfx::Point3F(),
3052 gfx::PointF(5.f, 5.f),
3053 gfx::Size(50, 50),
3054 true,
3055 false);
3057 ExecuteCalculateDrawProperties(root.get());
3059 EXPECT_TRUE(child->visible_content_rect().IsEmpty());
3060 EXPECT_TRUE(child->drawable_content_rect().IsEmpty());
3063 TEST_F(LayerTreeHostCommonTest,
3064 SingularTransformDoesNotPreventClearingDrawProperties) {
3065 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3066 scoped_refptr<LayerWithForcedDrawsContent> child =
3067 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3068 root->AddChild(child);
3070 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3071 host->SetRootLayer(root);
3073 gfx::Transform identity_matrix;
3074 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
3075 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3077 SetLayerPropertiesForTesting(root.get(),
3078 uninvertible_matrix,
3079 gfx::Point3F(),
3080 gfx::PointF(),
3081 gfx::Size(100, 100),
3082 true,
3083 false);
3084 SetLayerPropertiesForTesting(child.get(),
3085 identity_matrix,
3086 gfx::Point3F(),
3087 gfx::PointF(5.f, 5.f),
3088 gfx::Size(50, 50),
3089 true,
3090 false);
3092 child->set_sorted_for_recursion(true);
3094 TransformOperations start_transform_operations;
3095 start_transform_operations.AppendScale(1.f, 0.f, 0.f);
3097 TransformOperations end_transform_operations;
3098 end_transform_operations.AppendScale(1.f, 1.f, 0.f);
3100 AddAnimatedTransformToLayer(
3101 root.get(), 10.0, start_transform_operations, end_transform_operations);
3103 EXPECT_TRUE(root->TransformIsAnimating());
3105 ExecuteCalculateDrawProperties(root.get());
3107 EXPECT_FALSE(child->sorted_for_recursion());
3110 TEST_F(LayerTreeHostCommonTest,
3111 SingularNonAnimatingTransformDoesNotPreventClearingDrawProperties) {
3112 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3114 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3115 host->SetRootLayer(root);
3117 gfx::Transform identity_matrix;
3118 gfx::Transform uninvertible_matrix(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
3119 ASSERT_FALSE(uninvertible_matrix.IsInvertible());
3121 SetLayerPropertiesForTesting(root.get(),
3122 uninvertible_matrix,
3123 gfx::Point3F(),
3124 gfx::PointF(),
3125 gfx::Size(100, 100),
3126 true,
3127 false);
3129 root->set_sorted_for_recursion(true);
3131 EXPECT_FALSE(root->TransformIsAnimating());
3133 ExecuteCalculateDrawProperties(root.get());
3135 EXPECT_FALSE(root->sorted_for_recursion());
3138 TEST_F(LayerTreeHostCommonTest,
3139 DrawableAndVisibleContentRectsForLayersInClippedRenderSurface) {
3140 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3141 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
3142 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3143 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3144 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3145 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3146 scoped_refptr<LayerWithForcedDrawsContent> child3 =
3147 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3148 root->AddChild(render_surface1);
3149 render_surface1->AddChild(child1);
3150 render_surface1->AddChild(child2);
3151 render_surface1->AddChild(child3);
3153 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3154 host->SetRootLayer(root);
3156 gfx::Transform identity_matrix;
3157 SetLayerPropertiesForTesting(root.get(),
3158 identity_matrix,
3159 gfx::Point3F(),
3160 gfx::PointF(),
3161 gfx::Size(100, 100),
3162 true,
3163 false);
3164 SetLayerPropertiesForTesting(render_surface1.get(),
3165 identity_matrix,
3166 gfx::Point3F(),
3167 gfx::PointF(),
3168 gfx::Size(3, 4),
3169 true,
3170 false);
3171 SetLayerPropertiesForTesting(child1.get(),
3172 identity_matrix,
3173 gfx::Point3F(),
3174 gfx::PointF(5.f, 5.f),
3175 gfx::Size(50, 50),
3176 true,
3177 false);
3178 SetLayerPropertiesForTesting(child2.get(),
3179 identity_matrix,
3180 gfx::Point3F(),
3181 gfx::PointF(75.f, 75.f),
3182 gfx::Size(50, 50),
3183 true,
3184 false);
3185 SetLayerPropertiesForTesting(child3.get(),
3186 identity_matrix,
3187 gfx::Point3F(),
3188 gfx::PointF(125.f, 125.f),
3189 gfx::Size(50, 50),
3190 true,
3191 false);
3193 root->SetMasksToBounds(true);
3194 render_surface1->SetForceRenderSurface(true);
3195 ExecuteCalculateDrawProperties(root.get());
3197 ASSERT_TRUE(render_surface1->render_surface());
3199 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3200 root->render_surface()->DrawableContentRect());
3201 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3203 // Layers that do not draw content should have empty visible content rects.
3204 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3205 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
3207 // A clipped surface grows its DrawableContentRect to include all drawable
3208 // regions of the subtree, but also gets clamped by the ancestor's clip.
3209 EXPECT_EQ(gfx::Rect(5, 5, 95, 95),
3210 render_surface1->render_surface()->DrawableContentRect());
3212 // All layers that draw content into the surface have their visible content
3213 // rect clipped by the surface clip rect.
3214 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3215 EXPECT_EQ(gfx::Rect(0, 0, 25, 25), child2->visible_content_rect());
3216 EXPECT_TRUE(child3->visible_content_rect().IsEmpty());
3218 // But the DrawableContentRects are unclipped.
3219 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3220 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3221 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3224 TEST_F(LayerTreeHostCommonTest,
3225 DrawableAndVisibleContentRectsForSurfaceHierarchy) {
3226 // Check that clipping does not propagate down surfaces.
3227 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3228 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
3229 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
3230 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3231 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3232 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3233 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3234 scoped_refptr<LayerWithForcedDrawsContent> child3 =
3235 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3236 root->AddChild(render_surface1);
3237 render_surface1->AddChild(render_surface2);
3238 render_surface2->AddChild(child1);
3239 render_surface2->AddChild(child2);
3240 render_surface2->AddChild(child3);
3242 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3243 host->SetRootLayer(root);
3245 gfx::Transform identity_matrix;
3246 SetLayerPropertiesForTesting(root.get(),
3247 identity_matrix,
3248 gfx::Point3F(),
3249 gfx::PointF(),
3250 gfx::Size(100, 100),
3251 true,
3252 false);
3253 SetLayerPropertiesForTesting(render_surface1.get(),
3254 identity_matrix,
3255 gfx::Point3F(),
3256 gfx::PointF(),
3257 gfx::Size(3, 4),
3258 true,
3259 false);
3260 SetLayerPropertiesForTesting(render_surface2.get(),
3261 identity_matrix,
3262 gfx::Point3F(),
3263 gfx::PointF(),
3264 gfx::Size(7, 13),
3265 true,
3266 false);
3267 SetLayerPropertiesForTesting(child1.get(),
3268 identity_matrix,
3269 gfx::Point3F(),
3270 gfx::PointF(5.f, 5.f),
3271 gfx::Size(50, 50),
3272 true,
3273 false);
3274 SetLayerPropertiesForTesting(child2.get(),
3275 identity_matrix,
3276 gfx::Point3F(),
3277 gfx::PointF(75.f, 75.f),
3278 gfx::Size(50, 50),
3279 true,
3280 false);
3281 SetLayerPropertiesForTesting(child3.get(),
3282 identity_matrix,
3283 gfx::Point3F(),
3284 gfx::PointF(125.f, 125.f),
3285 gfx::Size(50, 50),
3286 true,
3287 false);
3289 root->SetMasksToBounds(true);
3290 render_surface1->SetForceRenderSurface(true);
3291 render_surface2->SetForceRenderSurface(true);
3292 ExecuteCalculateDrawProperties(root.get());
3294 ASSERT_TRUE(render_surface1->render_surface());
3295 ASSERT_TRUE(render_surface2->render_surface());
3297 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3298 root->render_surface()->DrawableContentRect());
3299 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3301 // Layers that do not draw content should have empty visible content rects.
3302 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3303 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
3304 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface2->visible_content_rect());
3306 // A clipped surface grows its DrawableContentRect to include all drawable
3307 // regions of the subtree, but also gets clamped by the ancestor's clip.
3308 EXPECT_EQ(gfx::Rect(5, 5, 95, 95),
3309 render_surface1->render_surface()->DrawableContentRect());
3311 // render_surface1 lives in the "unclipped universe" of render_surface1, and
3312 // is only implicitly clipped by render_surface1's content rect. So,
3313 // render_surface2 grows to enclose all drawable content of its subtree.
3314 EXPECT_EQ(gfx::Rect(5, 5, 170, 170),
3315 render_surface2->render_surface()->DrawableContentRect());
3317 // All layers that draw content into render_surface2 think they are unclipped.
3318 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3319 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
3320 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
3322 // DrawableContentRects are also unclipped.
3323 EXPECT_EQ(gfx::Rect(5, 5, 50, 50), child1->drawable_content_rect());
3324 EXPECT_EQ(gfx::Rect(75, 75, 50, 50), child2->drawable_content_rect());
3325 EXPECT_EQ(gfx::Rect(125, 125, 50, 50), child3->drawable_content_rect());
3328 TEST_F(LayerTreeHostCommonTest,
3329 DrawableAndVisibleContentRectsWithTransformOnUnclippedSurface) {
3330 // Layers that have non-axis aligned bounds (due to transforms) have an
3331 // expanded, axis-aligned DrawableContentRect and visible content rect.
3333 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3334 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
3335 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3336 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3337 root->AddChild(render_surface1);
3338 render_surface1->AddChild(child1);
3340 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3341 host->SetRootLayer(root);
3343 gfx::Transform identity_matrix;
3344 gfx::Transform child_rotation;
3345 child_rotation.Rotate(45.0);
3346 SetLayerPropertiesForTesting(root.get(),
3347 identity_matrix,
3348 gfx::Point3F(),
3349 gfx::PointF(),
3350 gfx::Size(100, 100),
3351 true,
3352 false);
3353 SetLayerPropertiesForTesting(render_surface1.get(),
3354 identity_matrix,
3355 gfx::Point3F(),
3356 gfx::PointF(),
3357 gfx::Size(3, 4),
3358 true,
3359 false);
3360 SetLayerPropertiesForTesting(child1.get(),
3361 child_rotation,
3362 gfx::Point3F(25, 25, 0.f),
3363 gfx::PointF(25.f, 25.f),
3364 gfx::Size(50, 50),
3365 true,
3366 false);
3368 render_surface1->SetForceRenderSurface(true);
3369 ExecuteCalculateDrawProperties(root.get());
3371 ASSERT_TRUE(render_surface1->render_surface());
3373 EXPECT_EQ(gfx::Rect(0, 0, 100, 100),
3374 root->render_surface()->DrawableContentRect());
3375 EXPECT_EQ(gfx::Rect(0, 0, 100, 100), root->drawable_content_rect());
3377 // Layers that do not draw content should have empty visible content rects.
3378 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3379 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), render_surface1->visible_content_rect());
3381 // The unclipped surface grows its DrawableContentRect to include all drawable
3382 // regions of the subtree.
3383 int diagonal_radius = ceil(sqrt(2.0) * 25.0);
3384 gfx::Rect expected_surface_drawable_content =
3385 gfx::Rect(50 - diagonal_radius,
3386 50 - diagonal_radius,
3387 diagonal_radius * 2,
3388 diagonal_radius * 2);
3389 EXPECT_EQ(expected_surface_drawable_content,
3390 render_surface1->render_surface()->DrawableContentRect());
3392 // All layers that draw content into the unclipped surface are also unclipped.
3393 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3394 EXPECT_EQ(expected_surface_drawable_content, child1->drawable_content_rect());
3397 TEST_F(LayerTreeHostCommonTest,
3398 DrawableAndVisibleContentRectsWithTransformOnClippedSurface) {
3399 // Layers that have non-axis aligned bounds (due to transforms) have an
3400 // expanded, axis-aligned DrawableContentRect and visible content rect.
3402 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3403 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
3404 scoped_refptr<LayerWithForcedDrawsContent> child1 =
3405 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3406 root->AddChild(render_surface1);
3407 render_surface1->AddChild(child1);
3409 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3410 host->SetRootLayer(root);
3412 gfx::Transform identity_matrix;
3413 gfx::Transform child_rotation;
3414 child_rotation.Rotate(45.0);
3415 SetLayerPropertiesForTesting(root.get(),
3416 identity_matrix,
3417 gfx::Point3F(),
3418 gfx::PointF(),
3419 gfx::Size(50, 50),
3420 true,
3421 false);
3422 SetLayerPropertiesForTesting(render_surface1.get(),
3423 identity_matrix,
3424 gfx::Point3F(),
3425 gfx::PointF(),
3426 gfx::Size(3, 4),
3427 true,
3428 false);
3430 SetLayerPropertiesForTesting(child1.get(),
3431 child_rotation,
3432 gfx::Point3F(25, 25, 0.f),
3433 gfx::PointF(25.f, 25.f),
3434 gfx::Size(50, 50),
3435 true,
3436 false);
3438 root->SetMasksToBounds(true);
3439 render_surface1->SetForceRenderSurface(true);
3440 ExecuteCalculateDrawProperties(root.get());
3442 ASSERT_TRUE(render_surface1->render_surface());
3444 // The clipped surface clamps the DrawableContentRect that encloses the
3445 // rotated layer.
3446 int diagonal_radius = ceil(sqrt(2.0) * 25.0);
3447 gfx::Rect unclipped_surface_content = gfx::Rect(50 - diagonal_radius,
3448 50 - diagonal_radius,
3449 diagonal_radius * 2,
3450 diagonal_radius * 2);
3451 gfx::Rect expected_surface_drawable_content =
3452 gfx::IntersectRects(unclipped_surface_content, gfx::Rect(0, 0, 50, 50));
3453 EXPECT_EQ(expected_surface_drawable_content,
3454 render_surface1->render_surface()->DrawableContentRect());
3456 // On the clipped surface, only a quarter of the child1 is visible, but when
3457 // rotating it back to child1's content space, the actual enclosing rect ends
3458 // up covering the full left half of child1.
3460 // Given the floating point math, this number is a little bit fuzzy.
3461 EXPECT_EQ(gfx::Rect(0, 0, 26, 50), child1->visible_content_rect());
3463 // The child's DrawableContentRect is unclipped.
3464 EXPECT_EQ(unclipped_surface_content, child1->drawable_content_rect());
3467 TEST_F(LayerTreeHostCommonTest, DrawableAndVisibleContentRectsInHighDPI) {
3468 MockContentLayerClient client;
3470 scoped_refptr<Layer> root = Layer::Create(layer_settings());
3471 scoped_refptr<FakePictureLayer> render_surface1 =
3472 CreateDrawablePictureLayer(layer_settings(), &client);
3473 scoped_refptr<FakePictureLayer> render_surface2 =
3474 CreateDrawablePictureLayer(layer_settings(), &client);
3475 scoped_refptr<FakePictureLayer> child1 =
3476 CreateDrawablePictureLayer(layer_settings(), &client);
3477 scoped_refptr<FakePictureLayer> child2 =
3478 CreateDrawablePictureLayer(layer_settings(), &client);
3479 scoped_refptr<FakePictureLayer> child3 =
3480 CreateDrawablePictureLayer(layer_settings(), &client);
3481 root->AddChild(render_surface1);
3482 render_surface1->AddChild(render_surface2);
3483 render_surface2->AddChild(child1);
3484 render_surface2->AddChild(child2);
3485 render_surface2->AddChild(child3);
3487 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3488 host->SetRootLayer(root);
3490 gfx::Transform identity_matrix;
3491 SetLayerPropertiesForTesting(root.get(),
3492 identity_matrix,
3493 gfx::Point3F(),
3494 gfx::PointF(),
3495 gfx::Size(100, 100),
3496 true,
3497 false);
3498 SetLayerPropertiesForTesting(render_surface1.get(),
3499 identity_matrix,
3500 gfx::Point3F(),
3501 gfx::PointF(5.f, 5.f),
3502 gfx::Size(3, 4),
3503 true,
3504 false);
3505 SetLayerPropertiesForTesting(render_surface2.get(),
3506 identity_matrix,
3507 gfx::Point3F(),
3508 gfx::PointF(5.f, 5.f),
3509 gfx::Size(7, 13),
3510 true,
3511 false);
3512 SetLayerPropertiesForTesting(child1.get(),
3513 identity_matrix,
3514 gfx::Point3F(),
3515 gfx::PointF(5.f, 5.f),
3516 gfx::Size(50, 50),
3517 true,
3518 false);
3519 SetLayerPropertiesForTesting(child2.get(),
3520 identity_matrix,
3521 gfx::Point3F(),
3522 gfx::PointF(75.f, 75.f),
3523 gfx::Size(50, 50),
3524 true,
3525 false);
3526 SetLayerPropertiesForTesting(child3.get(),
3527 identity_matrix,
3528 gfx::Point3F(),
3529 gfx::PointF(125.f, 125.f),
3530 gfx::Size(50, 50),
3531 true,
3532 false);
3534 float device_scale_factor = 2.f;
3536 root->SetMasksToBounds(true);
3537 render_surface1->SetForceRenderSurface(true);
3538 render_surface2->SetForceRenderSurface(true);
3539 ExecuteCalculateDrawProperties(root.get(), device_scale_factor);
3541 ASSERT_TRUE(render_surface1->render_surface());
3542 ASSERT_TRUE(render_surface2->render_surface());
3544 // drawable_content_rects for all layers and surfaces are scaled by
3545 // device_scale_factor.
3546 EXPECT_EQ(gfx::Rect(0, 0, 200, 200),
3547 root->render_surface()->DrawableContentRect());
3548 EXPECT_EQ(gfx::Rect(0, 0, 200, 200), root->drawable_content_rect());
3549 EXPECT_EQ(gfx::Rect(10, 10, 190, 190),
3550 render_surface1->render_surface()->DrawableContentRect());
3552 // render_surface2 lives in the "unclipped universe" of render_surface1, and
3553 // is only implicitly clipped by render_surface1.
3554 EXPECT_EQ(gfx::Rect(10, 10, 350, 350),
3555 render_surface2->render_surface()->DrawableContentRect());
3557 EXPECT_EQ(gfx::Rect(10, 10, 100, 100), child1->drawable_content_rect());
3558 EXPECT_EQ(gfx::Rect(150, 150, 100, 100), child2->drawable_content_rect());
3559 EXPECT_EQ(gfx::Rect(250, 250, 100, 100), child3->drawable_content_rect());
3561 // The root layer does not actually draw content of its own.
3562 EXPECT_EQ(gfx::Rect(0, 0, 0, 0), root->visible_content_rect());
3564 // All layer visible content rects are not expressed in content space of each
3565 // layer, so they are not scaled by the device_scale_factor.
3566 EXPECT_EQ(gfx::Rect(0, 0, 3, 4), render_surface1->visible_content_rect());
3567 EXPECT_EQ(gfx::Rect(0, 0, 7, 13), render_surface2->visible_content_rect());
3568 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child1->visible_content_rect());
3569 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child2->visible_content_rect());
3570 EXPECT_EQ(gfx::Rect(0, 0, 50, 50), child3->visible_content_rect());
3573 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithoutPreserves3d) {
3574 // Verify the behavior of back-face culling when there are no preserve-3d
3575 // layers. Note that 3d transforms still apply in this case, but they are
3576 // "flattened" to each parent layer according to current W3C spec.
3578 const gfx::Transform identity_matrix;
3579 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
3580 scoped_refptr<LayerWithForcedDrawsContent> front_facing_child =
3581 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3582 scoped_refptr<LayerWithForcedDrawsContent> back_facing_child =
3583 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3584 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3585 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3586 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3587 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3588 scoped_refptr<LayerWithForcedDrawsContent>
3589 front_facing_child_of_front_facing_surface =
3590 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3591 scoped_refptr<LayerWithForcedDrawsContent>
3592 back_facing_child_of_front_facing_surface =
3593 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3594 scoped_refptr<LayerWithForcedDrawsContent>
3595 front_facing_child_of_back_facing_surface =
3596 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3597 scoped_refptr<LayerWithForcedDrawsContent>
3598 back_facing_child_of_back_facing_surface =
3599 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3601 parent->AddChild(front_facing_child);
3602 parent->AddChild(back_facing_child);
3603 parent->AddChild(front_facing_surface);
3604 parent->AddChild(back_facing_surface);
3605 front_facing_surface->AddChild(front_facing_child_of_front_facing_surface);
3606 front_facing_surface->AddChild(back_facing_child_of_front_facing_surface);
3607 back_facing_surface->AddChild(front_facing_child_of_back_facing_surface);
3608 back_facing_surface->AddChild(back_facing_child_of_back_facing_surface);
3610 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3611 host->SetRootLayer(parent);
3613 // Nothing is double-sided
3614 front_facing_child->SetDoubleSided(false);
3615 back_facing_child->SetDoubleSided(false);
3616 front_facing_surface->SetDoubleSided(false);
3617 back_facing_surface->SetDoubleSided(false);
3618 front_facing_child_of_front_facing_surface->SetDoubleSided(false);
3619 back_facing_child_of_front_facing_surface->SetDoubleSided(false);
3620 front_facing_child_of_back_facing_surface->SetDoubleSided(false);
3621 back_facing_child_of_back_facing_surface->SetDoubleSided(false);
3623 gfx::Transform backface_matrix;
3624 backface_matrix.Translate(50.0, 50.0);
3625 backface_matrix.RotateAboutYAxis(180.0);
3626 backface_matrix.Translate(-50.0, -50.0);
3628 // Having a descendant and opacity will force these to have render surfaces.
3629 front_facing_surface->SetOpacity(0.5f);
3630 back_facing_surface->SetOpacity(0.5f);
3632 // Nothing preserves 3d. According to current W3C CSS gfx::Transforms spec,
3633 // these layers should blindly use their own local transforms to determine
3634 // back-face culling.
3635 SetLayerPropertiesForTesting(parent.get(),
3636 identity_matrix,
3637 gfx::Point3F(),
3638 gfx::PointF(),
3639 gfx::Size(100, 100),
3640 true,
3641 false);
3642 SetLayerPropertiesForTesting(front_facing_child.get(),
3643 identity_matrix,
3644 gfx::Point3F(),
3645 gfx::PointF(),
3646 gfx::Size(100, 100),
3647 true,
3648 false);
3649 SetLayerPropertiesForTesting(back_facing_child.get(),
3650 backface_matrix,
3651 gfx::Point3F(),
3652 gfx::PointF(),
3653 gfx::Size(100, 100),
3654 true,
3655 false);
3656 SetLayerPropertiesForTesting(front_facing_surface.get(),
3657 identity_matrix,
3658 gfx::Point3F(),
3659 gfx::PointF(),
3660 gfx::Size(100, 100),
3661 true,
3662 false);
3663 SetLayerPropertiesForTesting(back_facing_surface.get(),
3664 backface_matrix,
3665 gfx::Point3F(),
3666 gfx::PointF(),
3667 gfx::Size(100, 100),
3668 true,
3669 false);
3670 SetLayerPropertiesForTesting(front_facing_child_of_front_facing_surface.get(),
3671 identity_matrix,
3672 gfx::Point3F(),
3673 gfx::PointF(),
3674 gfx::Size(100, 100),
3675 true,
3676 false);
3677 SetLayerPropertiesForTesting(back_facing_child_of_front_facing_surface.get(),
3678 backface_matrix,
3679 gfx::Point3F(),
3680 gfx::PointF(),
3681 gfx::Size(100, 100),
3682 true,
3683 false);
3684 SetLayerPropertiesForTesting(front_facing_child_of_back_facing_surface.get(),
3685 identity_matrix,
3686 gfx::Point3F(),
3687 gfx::PointF(),
3688 gfx::Size(100, 100),
3689 true,
3690 false);
3691 SetLayerPropertiesForTesting(back_facing_child_of_back_facing_surface.get(),
3692 backface_matrix,
3693 gfx::Point3F(),
3694 gfx::PointF(),
3695 gfx::Size(100, 100),
3696 true,
3697 false);
3699 RenderSurfaceLayerList render_surface_layer_list;
3700 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
3701 parent.get(), parent->bounds(), &render_surface_layer_list);
3702 inputs.can_adjust_raster_scales = true;
3703 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
3705 // Verify which render surfaces were created.
3706 EXPECT_FALSE(front_facing_child->render_surface());
3707 EXPECT_FALSE(back_facing_child->render_surface());
3708 EXPECT_TRUE(front_facing_surface->render_surface());
3709 EXPECT_TRUE(back_facing_surface->render_surface());
3710 EXPECT_FALSE(front_facing_child_of_front_facing_surface->render_surface());
3711 EXPECT_FALSE(back_facing_child_of_front_facing_surface->render_surface());
3712 EXPECT_FALSE(front_facing_child_of_back_facing_surface->render_surface());
3713 EXPECT_FALSE(back_facing_child_of_back_facing_surface->render_surface());
3715 // Verify the render_surface_layer_list.
3716 ASSERT_EQ(3u, render_surface_layer_list.size());
3717 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
3718 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list.at(1)->id());
3719 // Even though the back facing surface LAYER gets culled, the other
3720 // descendants should still be added, so the SURFACE should not be culled.
3721 EXPECT_EQ(back_facing_surface->id(), render_surface_layer_list.at(2)->id());
3723 // Verify root surface's layer list.
3724 ASSERT_EQ(
3726 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
3727 EXPECT_EQ(front_facing_child->id(),
3728 render_surface_layer_list.at(0)
3729 ->render_surface()
3730 ->layer_list()
3731 .at(0)
3732 ->id());
3733 EXPECT_EQ(front_facing_surface->id(),
3734 render_surface_layer_list.at(0)
3735 ->render_surface()
3736 ->layer_list()
3737 .at(1)
3738 ->id());
3739 EXPECT_EQ(back_facing_surface->id(),
3740 render_surface_layer_list.at(0)
3741 ->render_surface()
3742 ->layer_list()
3743 .at(2)
3744 ->id());
3746 // Verify front_facing_surface's layer list.
3747 ASSERT_EQ(
3749 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
3750 EXPECT_EQ(front_facing_surface->id(),
3751 render_surface_layer_list.at(1)
3752 ->render_surface()
3753 ->layer_list()
3754 .at(0)
3755 ->id());
3756 EXPECT_EQ(front_facing_child_of_front_facing_surface->id(),
3757 render_surface_layer_list.at(1)
3758 ->render_surface()
3759 ->layer_list()
3760 .at(1)
3761 ->id());
3763 // Verify back_facing_surface's layer list; its own layer should be culled
3764 // from the surface list.
3765 ASSERT_EQ(
3767 render_surface_layer_list.at(2)->render_surface()->layer_list().size());
3768 EXPECT_EQ(front_facing_child_of_back_facing_surface->id(),
3769 render_surface_layer_list.at(2)
3770 ->render_surface()
3771 ->layer_list()
3772 .at(0)
3773 ->id());
3776 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithPreserves3d) {
3777 // Verify the behavior of back-face culling when preserves-3d transform style
3778 // is used.
3780 const gfx::Transform identity_matrix;
3781 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
3782 scoped_refptr<LayerWithForcedDrawsContent> front_facing_child =
3783 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3784 scoped_refptr<LayerWithForcedDrawsContent> back_facing_child =
3785 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3786 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
3787 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3788 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
3789 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3790 scoped_refptr<LayerWithForcedDrawsContent>
3791 front_facing_child_of_front_facing_surface =
3792 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3793 scoped_refptr<LayerWithForcedDrawsContent>
3794 back_facing_child_of_front_facing_surface =
3795 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3796 scoped_refptr<LayerWithForcedDrawsContent>
3797 front_facing_child_of_back_facing_surface =
3798 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3799 scoped_refptr<LayerWithForcedDrawsContent>
3800 back_facing_child_of_back_facing_surface =
3801 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3802 scoped_refptr<LayerWithForcedDrawsContent> dummy_replica_layer1 =
3803 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3804 scoped_refptr<LayerWithForcedDrawsContent> dummy_replica_layer2 =
3805 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3807 parent->AddChild(front_facing_child);
3808 parent->AddChild(back_facing_child);
3809 parent->AddChild(front_facing_surface);
3810 parent->AddChild(back_facing_surface);
3811 front_facing_surface->AddChild(front_facing_child_of_front_facing_surface);
3812 front_facing_surface->AddChild(back_facing_child_of_front_facing_surface);
3813 back_facing_surface->AddChild(front_facing_child_of_back_facing_surface);
3814 back_facing_surface->AddChild(back_facing_child_of_back_facing_surface);
3816 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3817 host->SetRootLayer(parent);
3819 // Nothing is double-sided
3820 front_facing_child->SetDoubleSided(false);
3821 back_facing_child->SetDoubleSided(false);
3822 front_facing_surface->SetDoubleSided(false);
3823 back_facing_surface->SetDoubleSided(false);
3824 front_facing_child_of_front_facing_surface->SetDoubleSided(false);
3825 back_facing_child_of_front_facing_surface->SetDoubleSided(false);
3826 front_facing_child_of_back_facing_surface->SetDoubleSided(false);
3827 back_facing_child_of_back_facing_surface->SetDoubleSided(false);
3829 gfx::Transform backface_matrix;
3830 backface_matrix.Translate(50.0, 50.0);
3831 backface_matrix.RotateAboutYAxis(180.0);
3832 backface_matrix.Translate(-50.0, -50.0);
3834 // Opacity will not force creation of render surfaces in this case because of
3835 // the preserve-3d transform style. Instead, an example of when a surface
3836 // would be created with preserve-3d is when there is a replica layer.
3837 front_facing_surface->SetReplicaLayer(dummy_replica_layer1.get());
3838 back_facing_surface->SetReplicaLayer(dummy_replica_layer2.get());
3840 // Each surface creates its own new 3d rendering context (as defined by W3C
3841 // spec). According to current W3C CSS gfx::Transforms spec, layers in a 3d
3842 // rendering context should use the transform with respect to that context.
3843 // This 3d rendering context occurs when (a) parent's transform style is flat
3844 // and (b) the layer's transform style is preserve-3d.
3845 SetLayerPropertiesForTesting(parent.get(),
3846 identity_matrix,
3847 gfx::Point3F(),
3848 gfx::PointF(),
3849 gfx::Size(100, 100),
3850 true,
3851 false); // parent transform style is flat.
3852 SetLayerPropertiesForTesting(front_facing_child.get(),
3853 identity_matrix,
3854 gfx::Point3F(),
3855 gfx::PointF(),
3856 gfx::Size(100, 100),
3857 true,
3858 false);
3859 SetLayerPropertiesForTesting(back_facing_child.get(),
3860 backface_matrix,
3861 gfx::Point3F(),
3862 gfx::PointF(),
3863 gfx::Size(100, 100),
3864 true,
3865 false);
3866 // surface transform style is preserve-3d.
3867 SetLayerPropertiesForTesting(front_facing_surface.get(),
3868 identity_matrix,
3869 gfx::Point3F(),
3870 gfx::PointF(),
3871 gfx::Size(100, 100),
3872 false,
3873 true);
3874 // surface transform style is preserve-3d.
3875 SetLayerPropertiesForTesting(back_facing_surface.get(),
3876 backface_matrix,
3877 gfx::Point3F(),
3878 gfx::PointF(),
3879 gfx::Size(100, 100),
3880 false,
3881 true);
3882 SetLayerPropertiesForTesting(front_facing_child_of_front_facing_surface.get(),
3883 identity_matrix,
3884 gfx::Point3F(),
3885 gfx::PointF(),
3886 gfx::Size(100, 100),
3887 true,
3888 true);
3889 SetLayerPropertiesForTesting(back_facing_child_of_front_facing_surface.get(),
3890 backface_matrix,
3891 gfx::Point3F(),
3892 gfx::PointF(),
3893 gfx::Size(100, 100),
3894 true,
3895 true);
3896 SetLayerPropertiesForTesting(front_facing_child_of_back_facing_surface.get(),
3897 identity_matrix,
3898 gfx::Point3F(),
3899 gfx::PointF(),
3900 gfx::Size(100, 100),
3901 true,
3902 true);
3903 SetLayerPropertiesForTesting(back_facing_child_of_back_facing_surface.get(),
3904 backface_matrix,
3905 gfx::Point3F(),
3906 gfx::PointF(),
3907 gfx::Size(100, 100),
3908 true,
3909 true);
3911 RenderSurfaceLayerList render_surface_layer_list;
3912 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
3913 parent.get(), parent->bounds(), &render_surface_layer_list);
3914 inputs.can_adjust_raster_scales = true;
3915 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
3917 // Verify which render surfaces were created and used.
3918 EXPECT_FALSE(front_facing_child->render_surface());
3919 EXPECT_FALSE(back_facing_child->render_surface());
3920 EXPECT_TRUE(front_facing_surface->render_surface());
3921 EXPECT_NE(back_facing_surface->render_target(), back_facing_surface);
3922 // We expect that a render_surface was created but not used.
3923 EXPECT_TRUE(back_facing_surface->render_surface());
3924 EXPECT_FALSE(front_facing_child_of_front_facing_surface->render_surface());
3925 EXPECT_FALSE(back_facing_child_of_front_facing_surface->render_surface());
3926 EXPECT_FALSE(front_facing_child_of_back_facing_surface->render_surface());
3927 EXPECT_FALSE(back_facing_child_of_back_facing_surface->render_surface());
3929 // Verify the render_surface_layer_list. The back-facing surface should be
3930 // culled.
3931 ASSERT_EQ(2u, render_surface_layer_list.size());
3932 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
3933 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list.at(1)->id());
3935 // Verify root surface's layer list.
3936 ASSERT_EQ(
3938 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
3939 EXPECT_EQ(front_facing_child->id(),
3940 render_surface_layer_list.at(0)
3941 ->render_surface()->layer_list().at(0)->id());
3942 EXPECT_EQ(front_facing_surface->id(),
3943 render_surface_layer_list.at(0)
3944 ->render_surface()->layer_list().at(1)->id());
3946 // Verify front_facing_surface's layer list.
3947 ASSERT_EQ(
3949 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
3950 EXPECT_EQ(front_facing_surface->id(),
3951 render_surface_layer_list.at(1)
3952 ->render_surface()->layer_list().at(0)->id());
3953 EXPECT_EQ(front_facing_child_of_front_facing_surface->id(),
3954 render_surface_layer_list.at(1)
3955 ->render_surface()->layer_list().at(1)->id());
3958 TEST_F(LayerTreeHostCommonTest, BackFaceCullingWithAnimatingTransforms) {
3959 // Verify that layers are appropriately culled when their back face is showing
3960 // and they are not double sided, while animations are going on.
3962 // Layers that are animating do not get culled on the main thread, as their
3963 // transforms should be treated as "unknown" so we can not be sure that their
3964 // back face is really showing.
3965 const gfx::Transform identity_matrix;
3966 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
3967 scoped_refptr<LayerWithForcedDrawsContent> child =
3968 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3969 scoped_refptr<LayerWithForcedDrawsContent> animating_surface =
3970 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3971 scoped_refptr<LayerWithForcedDrawsContent> child_of_animating_surface =
3972 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3973 scoped_refptr<LayerWithForcedDrawsContent> animating_child =
3974 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3975 scoped_refptr<LayerWithForcedDrawsContent> child2 =
3976 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
3978 parent->AddChild(child);
3979 parent->AddChild(animating_surface);
3980 animating_surface->AddChild(child_of_animating_surface);
3981 parent->AddChild(animating_child);
3982 parent->AddChild(child2);
3984 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
3985 host->SetRootLayer(parent);
3987 // Nothing is double-sided
3988 child->SetDoubleSided(false);
3989 child2->SetDoubleSided(false);
3990 animating_surface->SetDoubleSided(false);
3991 child_of_animating_surface->SetDoubleSided(false);
3992 animating_child->SetDoubleSided(false);
3994 gfx::Transform backface_matrix;
3995 backface_matrix.Translate(50.0, 50.0);
3996 backface_matrix.RotateAboutYAxis(180.0);
3997 backface_matrix.Translate(-50.0, -50.0);
3999 // Make our render surface.
4000 animating_surface->SetForceRenderSurface(true);
4002 // Animate the transform on the render surface.
4003 AddAnimatedTransformToController(
4004 animating_surface->layer_animation_controller(), 10.0, 30, 0);
4005 // This is just an animating layer, not a surface.
4006 AddAnimatedTransformToController(
4007 animating_child->layer_animation_controller(), 10.0, 30, 0);
4009 SetLayerPropertiesForTesting(parent.get(),
4010 identity_matrix,
4011 gfx::Point3F(),
4012 gfx::PointF(),
4013 gfx::Size(100, 100),
4014 true,
4015 false);
4016 SetLayerPropertiesForTesting(child.get(),
4017 backface_matrix,
4018 gfx::Point3F(),
4019 gfx::PointF(),
4020 gfx::Size(100, 100),
4021 true,
4022 false);
4023 SetLayerPropertiesForTesting(animating_surface.get(),
4024 backface_matrix,
4025 gfx::Point3F(),
4026 gfx::PointF(),
4027 gfx::Size(100, 100),
4028 true,
4029 false);
4030 SetLayerPropertiesForTesting(child_of_animating_surface.get(),
4031 backface_matrix,
4032 gfx::Point3F(),
4033 gfx::PointF(),
4034 gfx::Size(100, 100),
4035 true,
4036 false);
4037 SetLayerPropertiesForTesting(animating_child.get(),
4038 backface_matrix,
4039 gfx::Point3F(),
4040 gfx::PointF(),
4041 gfx::Size(100, 100),
4042 true,
4043 false);
4044 SetLayerPropertiesForTesting(child2.get(),
4045 identity_matrix,
4046 gfx::Point3F(),
4047 gfx::PointF(),
4048 gfx::Size(100, 100),
4049 true,
4050 false);
4052 RenderSurfaceLayerList render_surface_layer_list;
4053 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4054 parent.get(), parent->bounds(), &render_surface_layer_list);
4055 inputs.can_adjust_raster_scales = true;
4056 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4058 EXPECT_FALSE(child->render_surface());
4059 EXPECT_TRUE(animating_surface->render_surface());
4060 EXPECT_FALSE(child_of_animating_surface->render_surface());
4061 EXPECT_FALSE(animating_child->render_surface());
4062 EXPECT_FALSE(child2->render_surface());
4064 // Verify that the animating_child and child_of_animating_surface were not
4065 // culled, but that child was.
4066 ASSERT_EQ(2u, render_surface_layer_list.size());
4067 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
4068 EXPECT_EQ(animating_surface->id(), render_surface_layer_list.at(1)->id());
4070 // The non-animating child be culled from the layer list for the parent render
4071 // surface.
4072 ASSERT_EQ(
4074 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
4075 EXPECT_EQ(animating_surface->id(),
4076 render_surface_layer_list.at(0)
4077 ->render_surface()->layer_list().at(0)->id());
4078 EXPECT_EQ(animating_child->id(),
4079 render_surface_layer_list.at(0)
4080 ->render_surface()->layer_list().at(1)->id());
4081 EXPECT_EQ(child2->id(),
4082 render_surface_layer_list.at(0)
4083 ->render_surface()->layer_list().at(2)->id());
4085 ASSERT_EQ(
4087 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
4088 EXPECT_EQ(animating_surface->id(),
4089 render_surface_layer_list.at(1)
4090 ->render_surface()->layer_list().at(0)->id());
4091 EXPECT_EQ(child_of_animating_surface->id(),
4092 render_surface_layer_list.at(1)
4093 ->render_surface()->layer_list().at(1)->id());
4095 EXPECT_FALSE(child2->visible_content_rect().IsEmpty());
4097 // The animating layers should have a visible content rect that represents the
4098 // area of the front face that is within the viewport.
4099 EXPECT_EQ(animating_child->visible_content_rect(),
4100 gfx::Rect(animating_child->content_bounds()));
4101 EXPECT_EQ(animating_surface->visible_content_rect(),
4102 gfx::Rect(animating_surface->content_bounds()));
4103 // And layers in the subtree of the animating layer should have valid visible
4104 // content rects also.
4105 EXPECT_EQ(child_of_animating_surface->visible_content_rect(),
4106 gfx::Rect(child_of_animating_surface->content_bounds()));
4109 TEST_F(LayerTreeHostCommonTest,
4110 BackFaceCullingWithPreserves3dForFlatteningSurface) {
4111 // Verify the behavior of back-face culling for a render surface that is
4112 // created when it flattens its subtree, and its parent has preserves-3d.
4114 const gfx::Transform identity_matrix;
4115 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
4116 scoped_refptr<LayerWithForcedDrawsContent> front_facing_surface =
4117 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
4118 scoped_refptr<LayerWithForcedDrawsContent> back_facing_surface =
4119 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
4120 scoped_refptr<LayerWithForcedDrawsContent> child1 =
4121 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
4122 scoped_refptr<LayerWithForcedDrawsContent> child2 =
4123 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
4125 parent->AddChild(front_facing_surface);
4126 parent->AddChild(back_facing_surface);
4127 front_facing_surface->AddChild(child1);
4128 back_facing_surface->AddChild(child2);
4130 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4131 host->SetRootLayer(parent);
4133 // RenderSurfaces are not double-sided
4134 front_facing_surface->SetDoubleSided(false);
4135 back_facing_surface->SetDoubleSided(false);
4137 gfx::Transform backface_matrix;
4138 backface_matrix.Translate(50.0, 50.0);
4139 backface_matrix.RotateAboutYAxis(180.0);
4140 backface_matrix.Translate(-50.0, -50.0);
4142 SetLayerPropertiesForTesting(parent.get(),
4143 identity_matrix,
4144 gfx::Point3F(),
4145 gfx::PointF(),
4146 gfx::Size(100, 100),
4147 false,
4148 true); // parent transform style is preserve3d.
4149 SetLayerPropertiesForTesting(front_facing_surface.get(),
4150 identity_matrix,
4151 gfx::Point3F(),
4152 gfx::PointF(),
4153 gfx::Size(100, 100),
4154 true,
4155 true); // surface transform style is flat.
4156 SetLayerPropertiesForTesting(back_facing_surface.get(),
4157 backface_matrix,
4158 gfx::Point3F(),
4159 gfx::PointF(),
4160 gfx::Size(100, 100),
4161 true,
4162 true); // surface transform style is flat.
4163 SetLayerPropertiesForTesting(child1.get(),
4164 identity_matrix,
4165 gfx::Point3F(),
4166 gfx::PointF(),
4167 gfx::Size(100, 100),
4168 true,
4169 false);
4170 SetLayerPropertiesForTesting(child2.get(),
4171 identity_matrix,
4172 gfx::Point3F(),
4173 gfx::PointF(),
4174 gfx::Size(100, 100),
4175 true,
4176 false);
4178 front_facing_surface->Set3dSortingContextId(1);
4179 back_facing_surface->Set3dSortingContextId(1);
4181 RenderSurfaceLayerList render_surface_layer_list;
4182 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4183 parent.get(), parent->bounds(), &render_surface_layer_list);
4184 inputs.can_adjust_raster_scales = true;
4185 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4187 // Verify which render surfaces were created and used.
4188 EXPECT_TRUE(front_facing_surface->render_surface());
4190 // We expect the render surface to have been created, but remain unused.
4191 EXPECT_TRUE(back_facing_surface->render_surface());
4192 EXPECT_NE(back_facing_surface->render_target(),
4193 back_facing_surface); // because it should be culled
4194 EXPECT_FALSE(child1->render_surface());
4195 EXPECT_FALSE(child2->render_surface());
4197 // Verify the render_surface_layer_list. The back-facing surface should be
4198 // culled.
4199 ASSERT_EQ(2u, render_surface_layer_list.size());
4200 EXPECT_EQ(parent->id(), render_surface_layer_list.at(0)->id());
4201 EXPECT_EQ(front_facing_surface->id(), render_surface_layer_list.at(1)->id());
4203 // Verify root surface's layer list.
4204 ASSERT_EQ(
4206 render_surface_layer_list.at(0)->render_surface()->layer_list().size());
4207 EXPECT_EQ(front_facing_surface->id(),
4208 render_surface_layer_list.at(0)
4209 ->render_surface()->layer_list().at(0)->id());
4211 // Verify front_facing_surface's layer list.
4212 ASSERT_EQ(
4214 render_surface_layer_list.at(1)->render_surface()->layer_list().size());
4215 EXPECT_EQ(front_facing_surface->id(),
4216 render_surface_layer_list.at(1)
4217 ->render_surface()->layer_list().at(0)->id());
4218 EXPECT_EQ(child1->id(),
4219 render_surface_layer_list.at(1)
4220 ->render_surface()->layer_list().at(1)->id());
4223 class NoScaleContentLayer : public ContentLayer {
4224 public:
4225 static scoped_refptr<NoScaleContentLayer> Create(
4226 const LayerSettings& settings,
4227 ContentLayerClient* client) {
4228 return make_scoped_refptr(new NoScaleContentLayer(settings, client));
4231 void CalculateContentsScale(float ideal_contents_scale,
4232 float* contents_scale_x,
4233 float* contents_scale_y,
4234 gfx::Size* content_bounds) override {
4235 // Skip over the ContentLayer to the base Layer class.
4236 Layer::CalculateContentsScale(ideal_contents_scale,
4237 contents_scale_x,
4238 contents_scale_y,
4239 content_bounds);
4242 protected:
4243 NoScaleContentLayer(const LayerSettings& settings, ContentLayerClient* client)
4244 : ContentLayer(settings, client) {}
4245 ~NoScaleContentLayer() override {}
4248 scoped_refptr<NoScaleContentLayer> CreateNoScaleDrawableContentLayer(
4249 const LayerSettings& settings,
4250 ContentLayerClient* delegate) {
4251 scoped_refptr<NoScaleContentLayer> to_return =
4252 NoScaleContentLayer::Create(settings, delegate);
4253 to_return->SetIsDrawable(true);
4254 return to_return;
4257 TEST_F(LayerTreeHostCommonTest, LayerTransformsInHighDPI) {
4258 // Verify draw and screen space transforms of layers not in a surface.
4259 MockContentLayerClient delegate;
4260 gfx::Transform identity_matrix;
4262 scoped_refptr<FakePictureLayer> parent =
4263 CreateDrawablePictureLayer(layer_settings(), &delegate);
4264 SetLayerPropertiesForTesting(parent.get(),
4265 identity_matrix,
4266 gfx::Point3F(),
4267 gfx::PointF(),
4268 gfx::Size(100, 100),
4269 false,
4270 true);
4272 scoped_refptr<FakePictureLayer> child =
4273 CreateDrawablePictureLayer(layer_settings(), &delegate);
4274 SetLayerPropertiesForTesting(child.get(),
4275 identity_matrix,
4276 gfx::Point3F(),
4277 gfx::PointF(2.f, 2.f),
4278 gfx::Size(10, 10),
4279 false,
4280 true);
4282 scoped_refptr<FakePictureLayer> child_empty =
4283 CreateDrawablePictureLayer(layer_settings(), &delegate);
4284 SetLayerPropertiesForTesting(child_empty.get(),
4285 identity_matrix,
4286 gfx::Point3F(),
4287 gfx::PointF(2.f, 2.f),
4288 gfx::Size(),
4289 false,
4290 true);
4292 parent->AddChild(child);
4293 parent->AddChild(child_empty);
4295 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4296 host->SetRootLayer(parent);
4298 float device_scale_factor = 2.5f;
4299 float page_scale_factor = 1.f;
4301 RenderSurfaceLayerList render_surface_layer_list;
4302 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4303 parent.get(), parent->bounds(), &render_surface_layer_list);
4304 inputs.device_scale_factor = device_scale_factor;
4305 inputs.page_scale_factor = page_scale_factor;
4306 inputs.can_adjust_raster_scales = true;
4307 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4309 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4310 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, child);
4311 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, child_empty);
4313 EXPECT_EQ(1u, render_surface_layer_list.size());
4315 // Verify parent transforms
4316 gfx::Transform expected_parent_transform;
4317 expected_parent_transform.Scale(device_scale_factor * page_scale_factor,
4318 device_scale_factor * page_scale_factor);
4319 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4320 parent->screen_space_transform());
4321 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4322 parent->draw_transform());
4324 // Verify results of transformed parent rects
4325 gfx::RectF parent_content_bounds(parent->content_bounds());
4327 gfx::RectF parent_draw_rect =
4328 MathUtil::MapClippedRect(parent->draw_transform(), parent_content_bounds);
4329 gfx::RectF parent_screen_space_rect = MathUtil::MapClippedRect(
4330 parent->screen_space_transform(), parent_content_bounds);
4332 gfx::RectF expected_parent_draw_rect(parent->bounds());
4333 expected_parent_draw_rect.Scale(device_scale_factor);
4334 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_draw_rect);
4335 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_screen_space_rect);
4337 // Verify child and child_empty transforms. They should match.
4338 gfx::Transform expected_child_transform;
4339 expected_child_transform.Scale(device_scale_factor, device_scale_factor);
4340 expected_child_transform.Translate(child->position().x(),
4341 child->position().y());
4342 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4343 child->draw_transform());
4344 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4345 child->screen_space_transform());
4346 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4347 child_empty->draw_transform());
4348 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4349 child_empty->screen_space_transform());
4351 // Verify results of transformed child and child_empty rects. They should
4352 // match.
4353 gfx::RectF child_content_bounds(child->content_bounds());
4355 gfx::RectF child_draw_rect =
4356 MathUtil::MapClippedRect(child->draw_transform(), child_content_bounds);
4357 gfx::RectF child_screen_space_rect = MathUtil::MapClippedRect(
4358 child->screen_space_transform(), child_content_bounds);
4360 gfx::RectF child_empty_draw_rect = MathUtil::MapClippedRect(
4361 child_empty->draw_transform(), child_content_bounds);
4362 gfx::RectF child_empty_screen_space_rect = MathUtil::MapClippedRect(
4363 child_empty->screen_space_transform(), child_content_bounds);
4365 gfx::RectF expected_child_draw_rect(child->position(), child->bounds());
4366 expected_child_draw_rect.Scale(device_scale_factor);
4367 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_draw_rect);
4368 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_screen_space_rect);
4369 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_empty_draw_rect);
4370 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_empty_screen_space_rect);
4373 TEST_F(LayerTreeHostCommonTest, SurfaceLayerTransformsInHighDPI) {
4374 // Verify draw and screen space transforms of layers in a surface.
4375 MockContentLayerClient delegate;
4376 gfx::Transform identity_matrix;
4378 gfx::Transform perspective_matrix;
4379 perspective_matrix.ApplyPerspectiveDepth(2.0);
4381 gfx::Transform scale_small_matrix;
4382 scale_small_matrix.Scale(SK_MScalar1 / 10.f, SK_MScalar1 / 12.f);
4384 scoped_refptr<Layer> root = Layer::Create(layer_settings());
4386 scoped_refptr<FakePictureLayer> parent =
4387 CreateDrawablePictureLayer(layer_settings(), &delegate);
4388 SetLayerPropertiesForTesting(parent.get(),
4389 identity_matrix,
4390 gfx::Point3F(),
4391 gfx::PointF(),
4392 gfx::Size(100, 100),
4393 false,
4394 true);
4396 scoped_refptr<FakePictureLayer> perspective_surface =
4397 CreateDrawablePictureLayer(layer_settings(), &delegate);
4398 SetLayerPropertiesForTesting(perspective_surface.get(),
4399 perspective_matrix * scale_small_matrix,
4400 gfx::Point3F(),
4401 gfx::PointF(2.f, 2.f),
4402 gfx::Size(10, 10),
4403 false,
4404 true);
4406 scoped_refptr<FakePictureLayer> scale_surface =
4407 CreateDrawablePictureLayer(layer_settings(), &delegate);
4408 SetLayerPropertiesForTesting(scale_surface.get(),
4409 scale_small_matrix,
4410 gfx::Point3F(),
4411 gfx::PointF(2.f, 2.f),
4412 gfx::Size(10, 10),
4413 false,
4414 true);
4416 perspective_surface->SetForceRenderSurface(true);
4417 scale_surface->SetForceRenderSurface(true);
4419 parent->AddChild(perspective_surface);
4420 parent->AddChild(scale_surface);
4421 root->AddChild(parent);
4423 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4424 host->SetRootLayer(root);
4426 float device_scale_factor = 2.5f;
4427 float page_scale_factor = 3.f;
4429 RenderSurfaceLayerList render_surface_layer_list;
4430 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4431 root.get(), parent->bounds(), &render_surface_layer_list);
4432 inputs.device_scale_factor = device_scale_factor;
4433 inputs.page_scale_factor = page_scale_factor;
4434 inputs.page_scale_layer = root.get();
4435 inputs.can_adjust_raster_scales = true;
4436 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4438 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4439 EXPECT_IDEAL_SCALE_EQ(device_scale_factor * page_scale_factor,
4440 perspective_surface);
4441 // Ideal scale is the max 2d scale component of the combined transform up to
4442 // the nearest render target. Here this includes the layer transform as well
4443 // as the device and page scale factors.
4444 gfx::Transform transform = scale_small_matrix;
4445 transform.Scale(device_scale_factor * page_scale_factor,
4446 device_scale_factor * page_scale_factor);
4447 gfx::Vector2dF scales =
4448 MathUtil::ComputeTransform2dScaleComponents(transform, 0.f);
4449 float max_2d_scale = std::max(scales.x(), scales.y());
4450 EXPECT_IDEAL_SCALE_EQ(max_2d_scale, scale_surface);
4452 // The ideal scale will draw 1:1 with its render target space along
4453 // the larger-scale axis.
4454 gfx::Vector2dF target_space_transform_scales =
4455 MathUtil::ComputeTransform2dScaleComponents(
4456 scale_surface->draw_properties().target_space_transform, 0.f);
4457 EXPECT_FLOAT_EQ(max_2d_scale,
4458 std::max(target_space_transform_scales.x(),
4459 target_space_transform_scales.y()));
4461 EXPECT_EQ(3u, render_surface_layer_list.size());
4463 gfx::Transform expected_parent_draw_transform;
4464 expected_parent_draw_transform.Scale(device_scale_factor * page_scale_factor,
4465 device_scale_factor * page_scale_factor);
4466 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_draw_transform,
4467 parent->draw_transform());
4469 // The scale for the perspective surface is not known, so it is rendered 1:1
4470 // with the screen, and then scaled during drawing.
4471 gfx::Transform expected_perspective_surface_draw_transform;
4472 expected_perspective_surface_draw_transform.Translate(
4473 device_scale_factor * page_scale_factor *
4474 perspective_surface->position().x(),
4475 device_scale_factor * page_scale_factor *
4476 perspective_surface->position().y());
4477 expected_perspective_surface_draw_transform.PreconcatTransform(
4478 perspective_matrix);
4479 expected_perspective_surface_draw_transform.PreconcatTransform(
4480 scale_small_matrix);
4481 gfx::Transform expected_perspective_surface_layer_draw_transform;
4482 expected_perspective_surface_layer_draw_transform.Scale(
4483 device_scale_factor * page_scale_factor,
4484 device_scale_factor * page_scale_factor);
4485 EXPECT_TRANSFORMATION_MATRIX_EQ(
4486 expected_perspective_surface_draw_transform,
4487 perspective_surface->render_surface()->draw_transform());
4488 EXPECT_TRANSFORMATION_MATRIX_EQ(
4489 expected_perspective_surface_layer_draw_transform,
4490 perspective_surface->draw_transform());
4493 // TODO(sohanjg): Remove this test when ContentLayer is removed.
4494 TEST_F(LayerTreeHostCommonTest,
4495 LayerTransformsInHighDPIAccurateScaleZeroChildPosition) {
4496 // Verify draw and screen space transforms of layers not in a surface.
4497 MockContentLayerClient delegate;
4498 gfx::Transform identity_matrix;
4500 scoped_refptr<ContentLayer> parent =
4501 CreateDrawableContentLayer(layer_settings(), &delegate);
4502 SetLayerPropertiesForTesting(parent.get(),
4503 identity_matrix,
4504 gfx::Point3F(),
4505 gfx::PointF(),
4506 gfx::Size(133, 133),
4507 false,
4508 true);
4510 scoped_refptr<ContentLayer> child =
4511 CreateDrawableContentLayer(layer_settings(), &delegate);
4512 SetLayerPropertiesForTesting(child.get(),
4513 identity_matrix,
4514 gfx::Point3F(),
4515 gfx::PointF(),
4516 gfx::Size(13, 13),
4517 false,
4518 true);
4520 scoped_refptr<NoScaleContentLayer> child_no_scale =
4521 CreateNoScaleDrawableContentLayer(layer_settings(), &delegate);
4522 SetLayerPropertiesForTesting(child_no_scale.get(),
4523 identity_matrix,
4524 gfx::Point3F(),
4525 gfx::PointF(),
4526 gfx::Size(13, 13),
4527 false,
4528 true);
4530 parent->AddChild(child);
4531 parent->AddChild(child_no_scale);
4533 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4534 host->SetRootLayer(parent);
4536 float device_scale_factor = 1.7f;
4537 float page_scale_factor = 1.f;
4539 RenderSurfaceLayerList render_surface_layer_list;
4540 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4541 parent.get(), parent->bounds(), &render_surface_layer_list);
4542 inputs.device_scale_factor = device_scale_factor;
4543 inputs.page_scale_factor = page_scale_factor;
4544 inputs.page_scale_layer = parent.get();
4545 inputs.can_adjust_raster_scales = true;
4546 inputs.verify_property_trees = false;
4547 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4549 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4550 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, child);
4551 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4553 EXPECT_EQ(1u, render_surface_layer_list.size());
4555 // Verify parent transforms
4556 gfx::Transform expected_parent_transform;
4557 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4558 parent->screen_space_transform());
4559 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
4560 parent->draw_transform());
4562 // Verify results of transformed parent rects
4563 gfx::RectF parent_content_bounds(parent->content_bounds());
4565 gfx::RectF parent_draw_rect =
4566 MathUtil::MapClippedRect(parent->draw_transform(), parent_content_bounds);
4567 gfx::RectF parent_screen_space_rect = MathUtil::MapClippedRect(
4568 parent->screen_space_transform(), parent_content_bounds);
4570 gfx::RectF expected_parent_draw_rect(parent->bounds());
4571 expected_parent_draw_rect.Scale(device_scale_factor);
4572 expected_parent_draw_rect.set_width(ceil(expected_parent_draw_rect.width()));
4573 expected_parent_draw_rect.set_height(
4574 ceil(expected_parent_draw_rect.height()));
4575 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_draw_rect);
4576 EXPECT_FLOAT_RECT_EQ(expected_parent_draw_rect, parent_screen_space_rect);
4578 // Verify child transforms
4579 gfx::Transform expected_child_transform;
4580 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4581 child->draw_transform());
4582 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_transform,
4583 child->screen_space_transform());
4585 // Verify results of transformed child rects
4586 gfx::RectF child_content_bounds(child->content_bounds());
4588 gfx::RectF child_draw_rect =
4589 MathUtil::MapClippedRect(child->draw_transform(), child_content_bounds);
4590 gfx::RectF child_screen_space_rect = MathUtil::MapClippedRect(
4591 child->screen_space_transform(), child_content_bounds);
4593 gfx::RectF expected_child_draw_rect(child->bounds());
4594 expected_child_draw_rect.Scale(device_scale_factor);
4595 expected_child_draw_rect.set_width(ceil(expected_child_draw_rect.width()));
4596 expected_child_draw_rect.set_height(ceil(expected_child_draw_rect.height()));
4597 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_draw_rect);
4598 EXPECT_FLOAT_RECT_EQ(expected_child_draw_rect, child_screen_space_rect);
4600 // Verify child_no_scale transforms
4601 gfx::Transform expected_child_no_scale_transform = child->draw_transform();
4602 // All transforms operate on content rects. The child's content rect
4603 // incorporates device scale, but the child_no_scale does not; add it here.
4604 expected_child_no_scale_transform.Scale(device_scale_factor,
4605 device_scale_factor);
4606 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_no_scale_transform,
4607 child_no_scale->draw_transform());
4608 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_child_no_scale_transform,
4609 child_no_scale->screen_space_transform());
4612 // TODO(sohanjg): Remove this test when ContentLayer is removed.
4613 TEST_F(LayerTreeHostCommonTest, ContentsScale) {
4614 MockContentLayerClient delegate;
4615 gfx::Transform identity_matrix;
4617 gfx::Transform parent_scale_matrix;
4618 SkMScalar initial_parent_scale = 1.75;
4619 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4621 gfx::Transform child_scale_matrix;
4622 SkMScalar initial_child_scale = 1.25;
4623 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4625 scoped_refptr<Layer> root = Layer::Create(layer_settings());
4626 root->SetBounds(gfx::Size(100, 100));
4628 scoped_refptr<ContentLayer> parent =
4629 CreateDrawableContentLayer(layer_settings(), &delegate);
4630 SetLayerPropertiesForTesting(parent.get(),
4631 parent_scale_matrix,
4632 gfx::Point3F(),
4633 gfx::PointF(),
4634 gfx::Size(100, 100),
4635 false,
4636 true);
4638 scoped_refptr<ContentLayer> child_scale =
4639 CreateDrawableContentLayer(layer_settings(), &delegate);
4640 SetLayerPropertiesForTesting(child_scale.get(),
4641 child_scale_matrix,
4642 gfx::Point3F(),
4643 gfx::PointF(2.f, 2.f),
4644 gfx::Size(10, 10),
4645 false,
4646 true);
4648 scoped_refptr<ContentLayer> child_empty =
4649 CreateDrawableContentLayer(layer_settings(), &delegate);
4650 SetLayerPropertiesForTesting(child_empty.get(),
4651 child_scale_matrix,
4652 gfx::Point3F(),
4653 gfx::PointF(2.f, 2.f),
4654 gfx::Size(),
4655 false,
4656 true);
4658 scoped_refptr<NoScaleContentLayer> child_no_scale =
4659 CreateNoScaleDrawableContentLayer(layer_settings(), &delegate);
4660 SetLayerPropertiesForTesting(child_no_scale.get(),
4661 child_scale_matrix,
4662 gfx::Point3F(),
4663 gfx::PointF(12.f, 12.f),
4664 gfx::Size(10, 10),
4665 false,
4666 true);
4668 root->AddChild(parent);
4670 parent->AddChild(child_scale);
4671 parent->AddChild(child_empty);
4672 parent->AddChild(child_no_scale);
4674 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4675 host->SetRootLayer(root);
4677 float device_scale_factor = 2.5f;
4678 float page_scale_factor = 1.f;
4681 RenderSurfaceLayerList render_surface_layer_list;
4682 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4683 root.get(), root->bounds(), &render_surface_layer_list);
4684 inputs.device_scale_factor = device_scale_factor;
4685 inputs.page_scale_factor = page_scale_factor;
4686 inputs.page_scale_layer = root.get();
4687 inputs.can_adjust_raster_scales = true;
4688 inputs.verify_property_trees = false;
4689 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4691 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4692 initial_parent_scale, parent);
4693 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4694 initial_parent_scale * initial_child_scale,
4695 child_scale);
4696 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4697 initial_parent_scale * initial_child_scale,
4698 child_empty);
4699 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4701 // The parent is scaled up and shouldn't need to scale during draw. The
4702 // child that can scale its contents should also not need to scale during
4703 // draw. This shouldn't change if the child has empty bounds. The other
4704 // children should.
4705 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(0, 0));
4706 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(1, 1));
4707 EXPECT_FLOAT_EQ(1.0, child_scale->draw_transform().matrix().get(0, 0));
4708 EXPECT_FLOAT_EQ(1.0, child_scale->draw_transform().matrix().get(1, 1));
4709 EXPECT_FLOAT_EQ(1.0, child_empty->draw_transform().matrix().get(0, 0));
4710 EXPECT_FLOAT_EQ(1.0, child_empty->draw_transform().matrix().get(1, 1));
4711 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4712 initial_parent_scale * initial_child_scale,
4713 child_no_scale->draw_transform().matrix().get(0, 0));
4714 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4715 initial_parent_scale * initial_child_scale,
4716 child_no_scale->draw_transform().matrix().get(1, 1));
4719 // If the device_scale_factor or page_scale_factor changes, then it should be
4720 // updated using the initial transform as the raster scale.
4721 device_scale_factor = 2.25f;
4722 page_scale_factor = 1.25f;
4725 RenderSurfaceLayerList render_surface_layer_list;
4726 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4727 root.get(), root->bounds(), &render_surface_layer_list);
4728 inputs.device_scale_factor = device_scale_factor;
4729 inputs.page_scale_factor = page_scale_factor;
4730 inputs.page_scale_layer = root.get();
4731 inputs.can_adjust_raster_scales = true;
4732 inputs.verify_property_trees = false;
4733 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4735 EXPECT_CONTENTS_SCALE_EQ(
4736 device_scale_factor * page_scale_factor * initial_parent_scale, parent);
4737 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4738 initial_parent_scale * initial_child_scale,
4739 child_scale);
4740 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4741 initial_parent_scale * initial_child_scale,
4742 child_empty);
4743 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4746 // If the transform changes, we expect the raster scale to be reset to 1.0.
4747 SkMScalar second_child_scale = 1.75;
4748 child_scale_matrix.Scale(second_child_scale / initial_child_scale,
4749 second_child_scale / initial_child_scale);
4750 child_scale->SetTransform(child_scale_matrix);
4751 child_empty->SetTransform(child_scale_matrix);
4754 RenderSurfaceLayerList render_surface_layer_list;
4755 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4756 root.get(), root->bounds(), &render_surface_layer_list);
4757 inputs.device_scale_factor = device_scale_factor;
4758 inputs.page_scale_factor = page_scale_factor;
4759 inputs.page_scale_layer = root.get();
4760 inputs.can_adjust_raster_scales = true;
4761 inputs.verify_property_trees = false;
4762 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4764 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4765 initial_parent_scale,
4766 parent);
4767 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4768 child_scale);
4769 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4770 child_empty);
4771 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4774 // If the device_scale_factor or page_scale_factor changes, then it should be
4775 // updated, but still using 1.0 as the raster scale.
4776 device_scale_factor = 2.75f;
4777 page_scale_factor = 1.75f;
4780 RenderSurfaceLayerList render_surface_layer_list;
4781 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4782 root.get(), root->bounds(), &render_surface_layer_list);
4783 inputs.device_scale_factor = device_scale_factor;
4784 inputs.page_scale_factor = page_scale_factor;
4785 inputs.page_scale_layer = root.get();
4786 inputs.can_adjust_raster_scales = true;
4787 inputs.verify_property_trees = false;
4788 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4790 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
4791 initial_parent_scale,
4792 parent);
4793 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4794 child_scale);
4795 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4796 child_empty);
4797 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4801 // TODO(sohanjg): Remove this test when ContentLayer is removed.
4802 TEST_F(LayerTreeHostCommonTest,
4803 ContentsScale_LayerTransformsDontAffectContentsScale) {
4804 MockContentLayerClient delegate;
4805 gfx::Transform identity_matrix;
4807 gfx::Transform parent_scale_matrix;
4808 SkMScalar initial_parent_scale = 1.75;
4809 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4811 gfx::Transform child_scale_matrix;
4812 SkMScalar initial_child_scale = 1.25;
4813 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4815 scoped_refptr<Layer> root = Layer::Create(layer_settings());
4816 root->SetBounds(gfx::Size(100, 100));
4818 scoped_refptr<ContentLayer> parent =
4819 CreateDrawableContentLayer(layer_settings(), &delegate);
4820 SetLayerPropertiesForTesting(parent.get(),
4821 parent_scale_matrix,
4822 gfx::Point3F(),
4823 gfx::PointF(),
4824 gfx::Size(100, 100),
4825 false,
4826 true);
4828 scoped_refptr<ContentLayer> child_scale =
4829 CreateDrawableContentLayer(layer_settings(), &delegate);
4830 SetLayerPropertiesForTesting(child_scale.get(),
4831 child_scale_matrix,
4832 gfx::Point3F(),
4833 gfx::PointF(2.f, 2.f),
4834 gfx::Size(10, 10),
4835 false,
4836 true);
4838 scoped_refptr<ContentLayer> child_empty =
4839 CreateDrawableContentLayer(layer_settings(), &delegate);
4840 SetLayerPropertiesForTesting(child_empty.get(),
4841 child_scale_matrix,
4842 gfx::Point3F(),
4843 gfx::PointF(2.f, 2.f),
4844 gfx::Size(),
4845 false,
4846 true);
4848 scoped_refptr<NoScaleContentLayer> child_no_scale =
4849 CreateNoScaleDrawableContentLayer(layer_settings(), &delegate);
4850 SetLayerPropertiesForTesting(child_no_scale.get(),
4851 child_scale_matrix,
4852 gfx::Point3F(),
4853 gfx::PointF(12.f, 12.f),
4854 gfx::Size(10, 10),
4855 false,
4856 true);
4858 root->AddChild(parent);
4860 parent->AddChild(child_scale);
4861 parent->AddChild(child_empty);
4862 parent->AddChild(child_no_scale);
4864 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4865 host->SetRootLayer(root);
4867 RenderSurfaceLayerList render_surface_layer_list;
4869 float device_scale_factor = 2.5f;
4870 float page_scale_factor = 1.f;
4872 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4873 root.get(), root->bounds(), &render_surface_layer_list);
4874 inputs.device_scale_factor = device_scale_factor;
4875 inputs.page_scale_factor = page_scale_factor;
4876 inputs.page_scale_layer = root.get();
4877 inputs.verify_property_trees = false;
4878 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4880 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor, parent);
4881 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4882 child_scale);
4883 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
4884 child_empty);
4885 EXPECT_CONTENTS_SCALE_EQ(1, child_no_scale);
4887 // Since the transform scale does not affect contents scale, it should affect
4888 // the draw transform instead.
4889 EXPECT_FLOAT_EQ(initial_parent_scale,
4890 parent->draw_transform().matrix().get(0, 0));
4891 EXPECT_FLOAT_EQ(initial_parent_scale,
4892 parent->draw_transform().matrix().get(1, 1));
4893 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4894 child_scale->draw_transform().matrix().get(0, 0));
4895 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4896 child_scale->draw_transform().matrix().get(1, 1));
4897 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4898 child_empty->draw_transform().matrix().get(0, 0));
4899 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
4900 child_empty->draw_transform().matrix().get(1, 1));
4901 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4902 initial_parent_scale * initial_child_scale,
4903 child_no_scale->draw_transform().matrix().get(0, 0));
4904 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
4905 initial_parent_scale * initial_child_scale,
4906 child_no_scale->draw_transform().matrix().get(1, 1));
4909 TEST_F(LayerTreeHostCommonTest, SmallIdealScale) {
4910 MockContentLayerClient delegate;
4911 gfx::Transform identity_matrix;
4913 gfx::Transform parent_scale_matrix;
4914 SkMScalar initial_parent_scale = 1.75;
4915 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4917 gfx::Transform child_scale_matrix;
4918 SkMScalar initial_child_scale = 0.25;
4919 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4921 scoped_refptr<Layer> root = Layer::Create(layer_settings());
4922 root->SetBounds(gfx::Size(100, 100));
4924 scoped_refptr<FakePictureLayer> parent =
4925 CreateDrawablePictureLayer(layer_settings(), &delegate);
4926 SetLayerPropertiesForTesting(parent.get(),
4927 parent_scale_matrix,
4928 gfx::Point3F(),
4929 gfx::PointF(),
4930 gfx::Size(100, 100),
4931 false,
4932 true);
4934 scoped_refptr<FakePictureLayer> child_scale =
4935 CreateDrawablePictureLayer(layer_settings(), &delegate);
4936 SetLayerPropertiesForTesting(child_scale.get(),
4937 child_scale_matrix,
4938 gfx::Point3F(),
4939 gfx::PointF(2.f, 2.f),
4940 gfx::Size(10, 10),
4941 false,
4942 true);
4944 root->AddChild(parent);
4946 parent->AddChild(child_scale);
4948 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
4949 host->SetRootLayer(root);
4951 float device_scale_factor = 2.5f;
4952 float page_scale_factor = 0.01f;
4955 RenderSurfaceLayerList render_surface_layer_list;
4956 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
4957 root.get(), root->bounds(), &render_surface_layer_list);
4958 inputs.device_scale_factor = device_scale_factor;
4959 inputs.page_scale_factor = page_scale_factor;
4960 inputs.page_scale_layer = root.get();
4961 inputs.can_adjust_raster_scales = true;
4962 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
4964 // The ideal scale is able to go below 1.
4965 float expected_ideal_scale =
4966 device_scale_factor * page_scale_factor * initial_parent_scale;
4967 EXPECT_LT(expected_ideal_scale, 1.f);
4968 EXPECT_IDEAL_SCALE_EQ(expected_ideal_scale, parent);
4970 expected_ideal_scale = device_scale_factor * page_scale_factor *
4971 initial_parent_scale * initial_child_scale;
4972 EXPECT_LT(expected_ideal_scale, 1.f);
4973 EXPECT_IDEAL_SCALE_EQ(expected_ideal_scale, child_scale);
4977 TEST_F(LayerTreeHostCommonTest, ContentsScaleForSurfaces) {
4978 MockContentLayerClient delegate;
4979 gfx::Transform identity_matrix;
4981 gfx::Transform parent_scale_matrix;
4982 SkMScalar initial_parent_scale = 2.0;
4983 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
4985 gfx::Transform child_scale_matrix;
4986 SkMScalar initial_child_scale = 3.0;
4987 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
4989 scoped_refptr<Layer> root = Layer::Create(layer_settings());
4990 root->SetBounds(gfx::Size(100, 100));
4992 scoped_refptr<ContentLayer> parent =
4993 CreateDrawableContentLayer(layer_settings(), &delegate);
4994 SetLayerPropertiesForTesting(parent.get(),
4995 parent_scale_matrix,
4996 gfx::Point3F(),
4997 gfx::PointF(),
4998 gfx::Size(100, 100),
4999 false,
5000 true);
5002 scoped_refptr<ContentLayer> surface_scale =
5003 CreateDrawableContentLayer(layer_settings(), &delegate);
5004 SetLayerPropertiesForTesting(surface_scale.get(),
5005 child_scale_matrix,
5006 gfx::Point3F(),
5007 gfx::PointF(2.f, 2.f),
5008 gfx::Size(10, 10),
5009 false,
5010 true);
5012 scoped_refptr<ContentLayer> surface_scale_child_scale =
5013 CreateDrawableContentLayer(layer_settings(), &delegate);
5014 SetLayerPropertiesForTesting(surface_scale_child_scale.get(),
5015 child_scale_matrix,
5016 gfx::Point3F(),
5017 gfx::PointF(),
5018 gfx::Size(10, 10),
5019 false,
5020 true);
5022 scoped_refptr<NoScaleContentLayer> surface_scale_child_no_scale =
5023 CreateNoScaleDrawableContentLayer(layer_settings(), &delegate);
5024 SetLayerPropertiesForTesting(surface_scale_child_no_scale.get(),
5025 child_scale_matrix,
5026 gfx::Point3F(),
5027 gfx::PointF(),
5028 gfx::Size(10, 10),
5029 false,
5030 true);
5032 scoped_refptr<NoScaleContentLayer> surface_no_scale =
5033 CreateNoScaleDrawableContentLayer(layer_settings(), &delegate);
5034 SetLayerPropertiesForTesting(surface_no_scale.get(),
5035 child_scale_matrix,
5036 gfx::Point3F(),
5037 gfx::PointF(12.f, 12.f),
5038 gfx::Size(10, 10),
5039 false,
5040 true);
5042 scoped_refptr<ContentLayer> surface_no_scale_child_scale =
5043 CreateDrawableContentLayer(layer_settings(), &delegate);
5044 SetLayerPropertiesForTesting(surface_no_scale_child_scale.get(),
5045 child_scale_matrix,
5046 gfx::Point3F(),
5047 gfx::PointF(),
5048 gfx::Size(10, 10),
5049 false,
5050 true);
5052 scoped_refptr<NoScaleContentLayer> surface_no_scale_child_no_scale =
5053 CreateNoScaleDrawableContentLayer(layer_settings(), &delegate);
5054 SetLayerPropertiesForTesting(surface_no_scale_child_no_scale.get(),
5055 child_scale_matrix,
5056 gfx::Point3F(),
5057 gfx::PointF(),
5058 gfx::Size(10, 10),
5059 false,
5060 true);
5062 root->AddChild(parent);
5064 parent->AddChild(surface_scale);
5065 parent->AddChild(surface_no_scale);
5067 surface_scale->SetForceRenderSurface(true);
5068 surface_scale->AddChild(surface_scale_child_scale);
5069 surface_scale->AddChild(surface_scale_child_no_scale);
5071 surface_no_scale->SetForceRenderSurface(true);
5072 surface_no_scale->AddChild(surface_no_scale_child_scale);
5073 surface_no_scale->AddChild(surface_no_scale_child_no_scale);
5075 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5076 host->SetRootLayer(root);
5078 SkMScalar device_scale_factor = 5;
5079 SkMScalar page_scale_factor = 7;
5081 RenderSurfaceLayerList render_surface_layer_list;
5082 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5083 root.get(), root->bounds(), &render_surface_layer_list);
5084 inputs.device_scale_factor = device_scale_factor;
5085 inputs.page_scale_factor = page_scale_factor;
5086 inputs.page_scale_layer = root.get();
5087 inputs.can_adjust_raster_scales = true;
5088 inputs.verify_property_trees = false;
5089 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5091 EXPECT_CONTENTS_SCALE_EQ(
5092 device_scale_factor * page_scale_factor * initial_parent_scale, parent);
5093 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor *
5094 initial_parent_scale * initial_child_scale,
5095 surface_scale);
5096 EXPECT_CONTENTS_SCALE_EQ(1, surface_no_scale);
5097 EXPECT_CONTENTS_SCALE_EQ(
5098 device_scale_factor * page_scale_factor * initial_parent_scale *
5099 initial_child_scale * initial_child_scale,
5100 surface_scale_child_scale);
5101 EXPECT_CONTENTS_SCALE_EQ(1, surface_scale_child_no_scale);
5102 EXPECT_CONTENTS_SCALE_EQ(
5103 device_scale_factor * page_scale_factor * initial_parent_scale *
5104 initial_child_scale * initial_child_scale,
5105 surface_no_scale_child_scale);
5106 EXPECT_CONTENTS_SCALE_EQ(1, surface_no_scale_child_no_scale);
5108 // The parent is scaled up and shouldn't need to scale during draw.
5109 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(0, 0));
5110 EXPECT_FLOAT_EQ(1.0, parent->draw_transform().matrix().get(1, 1));
5112 // RenderSurfaces should always be 1:1 with their target.
5113 EXPECT_FLOAT_EQ(
5114 1.0,
5115 surface_scale->render_surface()->draw_transform().matrix().get(0, 0));
5116 EXPECT_FLOAT_EQ(
5117 1.0,
5118 surface_scale->render_surface()->draw_transform().matrix().get(1, 1));
5120 // The surface_scale can apply contents scale so the layer shouldn't need to
5121 // scale during draw.
5122 EXPECT_FLOAT_EQ(1.0, surface_scale->draw_transform().matrix().get(0, 0));
5123 EXPECT_FLOAT_EQ(1.0, surface_scale->draw_transform().matrix().get(1, 1));
5125 // The surface_scale_child_scale can apply contents scale so it shouldn't need
5126 // to scale during draw.
5127 EXPECT_FLOAT_EQ(
5128 1.0, surface_scale_child_scale->draw_transform().matrix().get(0, 0));
5129 EXPECT_FLOAT_EQ(
5130 1.0, surface_scale_child_scale->draw_transform().matrix().get(1, 1));
5132 // The surface_scale_child_no_scale can not apply contents scale, so it needs
5133 // to be scaled during draw.
5134 EXPECT_FLOAT_EQ(
5135 device_scale_factor * page_scale_factor * initial_parent_scale *
5136 initial_child_scale * initial_child_scale,
5137 surface_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5138 EXPECT_FLOAT_EQ(
5139 device_scale_factor * page_scale_factor * initial_parent_scale *
5140 initial_child_scale * initial_child_scale,
5141 surface_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5143 // RenderSurfaces should always be 1:1 with their target.
5144 EXPECT_FLOAT_EQ(
5145 1.0,
5146 surface_no_scale->render_surface()->draw_transform().matrix().get(0, 0));
5147 EXPECT_FLOAT_EQ(
5148 1.0,
5149 surface_no_scale->render_surface()->draw_transform().matrix().get(1, 1));
5151 // The surface_no_scale layer can not apply contents scale, so it needs to be
5152 // scaled during draw.
5153 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
5154 initial_parent_scale * initial_child_scale,
5155 surface_no_scale->draw_transform().matrix().get(0, 0));
5156 EXPECT_FLOAT_EQ(device_scale_factor * page_scale_factor *
5157 initial_parent_scale * initial_child_scale,
5158 surface_no_scale->draw_transform().matrix().get(1, 1));
5160 // The surface_scale_child_scale can apply contents scale so it shouldn't need
5161 // to scale during draw.
5162 EXPECT_FLOAT_EQ(
5163 1.0, surface_no_scale_child_scale->draw_transform().matrix().get(0, 0));
5164 EXPECT_FLOAT_EQ(
5165 1.0, surface_no_scale_child_scale->draw_transform().matrix().get(1, 1));
5167 // The surface_scale_child_no_scale can not apply contents scale, so it needs
5168 // to be scaled during draw.
5169 EXPECT_FLOAT_EQ(
5170 device_scale_factor * page_scale_factor * initial_parent_scale *
5171 initial_child_scale * initial_child_scale,
5172 surface_no_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5173 EXPECT_FLOAT_EQ(
5174 device_scale_factor * page_scale_factor * initial_parent_scale *
5175 initial_child_scale * initial_child_scale,
5176 surface_no_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5179 // TODO(sohanjg): Remove this test when ContentLayer is removed.
5180 TEST_F(LayerTreeHostCommonTest,
5181 ContentsScaleForSurfaces_LayerTransformsDontAffectContentsScale) {
5182 MockContentLayerClient delegate;
5183 gfx::Transform identity_matrix;
5185 gfx::Transform parent_scale_matrix;
5186 SkMScalar initial_parent_scale = 2.0;
5187 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
5189 gfx::Transform child_scale_matrix;
5190 SkMScalar initial_child_scale = 3.0;
5191 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
5193 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5194 root->SetBounds(gfx::Size(100, 100));
5196 scoped_refptr<ContentLayer> parent =
5197 CreateDrawableContentLayer(layer_settings(), &delegate);
5198 SetLayerPropertiesForTesting(parent.get(),
5199 parent_scale_matrix,
5200 gfx::Point3F(),
5201 gfx::PointF(),
5202 gfx::Size(100, 100),
5203 false,
5204 true);
5206 scoped_refptr<ContentLayer> surface_scale =
5207 CreateDrawableContentLayer(layer_settings(), &delegate);
5208 SetLayerPropertiesForTesting(surface_scale.get(),
5209 child_scale_matrix,
5210 gfx::Point3F(),
5211 gfx::PointF(2.f, 2.f),
5212 gfx::Size(10, 10),
5213 false,
5214 true);
5216 scoped_refptr<ContentLayer> surface_scale_child_scale =
5217 CreateDrawableContentLayer(layer_settings(), &delegate);
5218 SetLayerPropertiesForTesting(surface_scale_child_scale.get(),
5219 child_scale_matrix,
5220 gfx::Point3F(),
5221 gfx::PointF(),
5222 gfx::Size(10, 10),
5223 false,
5224 true);
5226 scoped_refptr<NoScaleContentLayer> surface_scale_child_no_scale =
5227 CreateNoScaleDrawableContentLayer(layer_settings(), &delegate);
5228 SetLayerPropertiesForTesting(surface_scale_child_no_scale.get(),
5229 child_scale_matrix,
5230 gfx::Point3F(),
5231 gfx::PointF(),
5232 gfx::Size(10, 10),
5233 false,
5234 true);
5236 scoped_refptr<NoScaleContentLayer> surface_no_scale =
5237 CreateNoScaleDrawableContentLayer(layer_settings(), &delegate);
5238 SetLayerPropertiesForTesting(surface_no_scale.get(),
5239 child_scale_matrix,
5240 gfx::Point3F(),
5241 gfx::PointF(12.f, 12.f),
5242 gfx::Size(10, 10),
5243 false,
5244 true);
5246 scoped_refptr<ContentLayer> surface_no_scale_child_scale =
5247 CreateDrawableContentLayer(layer_settings(), &delegate);
5248 SetLayerPropertiesForTesting(surface_no_scale_child_scale.get(),
5249 child_scale_matrix,
5250 gfx::Point3F(),
5251 gfx::PointF(),
5252 gfx::Size(10, 10),
5253 false,
5254 true);
5256 scoped_refptr<NoScaleContentLayer> surface_no_scale_child_no_scale =
5257 CreateNoScaleDrawableContentLayer(layer_settings(), &delegate);
5258 SetLayerPropertiesForTesting(surface_no_scale_child_no_scale.get(),
5259 child_scale_matrix,
5260 gfx::Point3F(),
5261 gfx::PointF(),
5262 gfx::Size(10, 10),
5263 false,
5264 true);
5266 root->AddChild(parent);
5268 parent->AddChild(surface_scale);
5269 parent->AddChild(surface_no_scale);
5271 surface_scale->SetForceRenderSurface(true);
5272 surface_scale->AddChild(surface_scale_child_scale);
5273 surface_scale->AddChild(surface_scale_child_no_scale);
5275 surface_no_scale->SetForceRenderSurface(true);
5276 surface_no_scale->AddChild(surface_no_scale_child_scale);
5277 surface_no_scale->AddChild(surface_no_scale_child_no_scale);
5279 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5280 host->SetRootLayer(root);
5282 RenderSurfaceLayerList render_surface_layer_list;
5284 SkMScalar device_scale_factor = 5.0;
5285 SkMScalar page_scale_factor = 7.0;
5286 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5287 root.get(), root->bounds(), &render_surface_layer_list);
5288 inputs.device_scale_factor = device_scale_factor;
5289 inputs.page_scale_factor = page_scale_factor;
5290 inputs.page_scale_layer = root.get();
5291 inputs.verify_property_trees = false;
5292 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5294 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5295 parent);
5296 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5297 surface_scale);
5298 EXPECT_CONTENTS_SCALE_EQ(1.f, surface_no_scale);
5299 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5300 surface_scale_child_scale);
5301 EXPECT_CONTENTS_SCALE_EQ(1.f, surface_scale_child_no_scale);
5302 EXPECT_CONTENTS_SCALE_EQ(device_scale_factor * page_scale_factor,
5303 surface_no_scale_child_scale);
5304 EXPECT_CONTENTS_SCALE_EQ(1.f, surface_no_scale_child_no_scale);
5306 // The parent is scaled up during draw, since its contents are not scaled by
5307 // the transform hierarchy.
5308 EXPECT_FLOAT_EQ(initial_parent_scale,
5309 parent->draw_transform().matrix().get(0, 0));
5310 EXPECT_FLOAT_EQ(initial_parent_scale,
5311 parent->draw_transform().matrix().get(1, 1));
5313 // The child surface is not scaled up during draw since its subtree is scaled
5314 // by the transform hierarchy.
5315 EXPECT_FLOAT_EQ(
5316 1.f,
5317 surface_scale->render_surface()->draw_transform().matrix().get(0, 0));
5318 EXPECT_FLOAT_EQ(
5319 1.f,
5320 surface_scale->render_surface()->draw_transform().matrix().get(1, 1));
5322 // The surface_scale's RenderSurface is not scaled during draw, so the layer
5323 // needs to be scaled when drawing into its surface.
5324 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
5325 surface_scale->draw_transform().matrix().get(0, 0));
5326 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale,
5327 surface_scale->draw_transform().matrix().get(1, 1));
5329 // The surface_scale_child_scale is not scaled when drawing into its surface,
5330 // since its content bounds are scaled by the transform hierarchy.
5331 EXPECT_FLOAT_EQ(
5332 initial_child_scale * initial_child_scale * initial_parent_scale,
5333 surface_scale_child_scale->draw_transform().matrix().get(0, 0));
5334 EXPECT_FLOAT_EQ(
5335 initial_child_scale * initial_child_scale * initial_parent_scale,
5336 surface_scale_child_scale->draw_transform().matrix().get(1, 1));
5338 // The surface_scale_child_no_scale is scaled by the device scale, page scale
5339 // and transform hierarchy.
5340 EXPECT_FLOAT_EQ(
5341 device_scale_factor * page_scale_factor * initial_parent_scale *
5342 initial_child_scale * initial_child_scale,
5343 surface_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5344 EXPECT_FLOAT_EQ(
5345 device_scale_factor * page_scale_factor * initial_parent_scale *
5346 initial_child_scale * initial_child_scale,
5347 surface_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5349 // The child surface is not scaled up during draw since its subtree is scaled
5350 // by the transform hierarchy.
5351 EXPECT_FLOAT_EQ(
5352 1.f,
5353 surface_no_scale->render_surface()->draw_transform().matrix().get(0, 0));
5354 EXPECT_FLOAT_EQ(
5355 1.f,
5356 surface_no_scale->render_surface()->draw_transform().matrix().get(1, 1));
5358 // The surface_no_scale layer has a fixed contents scale of 1, so it needs to
5359 // be scaled by the device and page scale factors. Its surface is already
5360 // scaled by the transform hierarchy so those don't need to scale the layer's
5361 // drawing.
5362 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale *
5363 device_scale_factor * page_scale_factor,
5364 surface_no_scale->draw_transform().matrix().get(0, 0));
5365 EXPECT_FLOAT_EQ(initial_parent_scale * initial_child_scale *
5366 device_scale_factor * page_scale_factor,
5367 surface_no_scale->draw_transform().matrix().get(1, 1));
5369 // The surface_no_scale_child_scale has its contents scaled by the page and
5370 // device scale factors, but needs to be scaled by the transform hierarchy
5371 // when drawing.
5372 EXPECT_FLOAT_EQ(
5373 initial_parent_scale * initial_child_scale * initial_child_scale,
5374 surface_no_scale_child_scale->draw_transform().matrix().get(0, 0));
5375 EXPECT_FLOAT_EQ(
5376 initial_parent_scale * initial_child_scale * initial_child_scale,
5377 surface_no_scale_child_scale->draw_transform().matrix().get(1, 1));
5379 // The surface_no_scale_child_no_scale needs to be scaled by the device and
5380 // page scale factors and by any transform heirarchy below its target surface.
5381 EXPECT_FLOAT_EQ(
5382 device_scale_factor * page_scale_factor * initial_parent_scale *
5383 initial_child_scale * initial_child_scale,
5384 surface_no_scale_child_no_scale->draw_transform().matrix().get(0, 0));
5385 EXPECT_FLOAT_EQ(
5386 device_scale_factor * page_scale_factor * initial_parent_scale *
5387 initial_child_scale * initial_child_scale,
5388 surface_no_scale_child_no_scale->draw_transform().matrix().get(1, 1));
5391 TEST_F(LayerTreeHostCommonTest, IdealScaleForAnimatingLayer) {
5392 MockContentLayerClient delegate;
5393 gfx::Transform identity_matrix;
5395 gfx::Transform parent_scale_matrix;
5396 SkMScalar initial_parent_scale = 1.75;
5397 parent_scale_matrix.Scale(initial_parent_scale, initial_parent_scale);
5399 gfx::Transform child_scale_matrix;
5400 SkMScalar initial_child_scale = 1.25;
5401 child_scale_matrix.Scale(initial_child_scale, initial_child_scale);
5403 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5404 root->SetBounds(gfx::Size(100, 100));
5406 scoped_refptr<FakePictureLayer> parent =
5407 CreateDrawablePictureLayer(layer_settings(), &delegate);
5408 SetLayerPropertiesForTesting(parent.get(),
5409 parent_scale_matrix,
5410 gfx::Point3F(),
5411 gfx::PointF(),
5412 gfx::Size(100, 100),
5413 false,
5414 true);
5416 scoped_refptr<FakePictureLayer> child_scale =
5417 CreateDrawablePictureLayer(layer_settings(), &delegate);
5418 SetLayerPropertiesForTesting(child_scale.get(),
5419 child_scale_matrix,
5420 gfx::Point3F(),
5421 gfx::PointF(2.f, 2.f),
5422 gfx::Size(10, 10),
5423 false,
5424 true);
5426 root->AddChild(parent);
5428 parent->AddChild(child_scale);
5430 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5431 host->SetRootLayer(root);
5434 RenderSurfaceLayerList render_surface_layer_list;
5435 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5436 root.get(), root->bounds(), &render_surface_layer_list);
5437 inputs.can_adjust_raster_scales = true;
5438 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5440 EXPECT_IDEAL_SCALE_EQ(initial_parent_scale, parent);
5441 // Animating layers compute ideal scale in the same way as when
5442 // they are static.
5443 EXPECT_IDEAL_SCALE_EQ(initial_child_scale * initial_parent_scale,
5444 child_scale);
5448 // TODO(sohanjg): Remove this test when ContentLayer is removed.
5449 TEST_F(LayerTreeHostCommonTest,
5450 ChangeInContentBoundsOrScaleTriggersPushProperties) {
5451 MockContentLayerClient delegate;
5452 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5453 scoped_refptr<Layer> child =
5454 CreateDrawableContentLayer(layer_settings(), &delegate);
5455 root->AddChild(child);
5457 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5458 host->SetRootLayer(root);
5460 gfx::Transform identity_matrix;
5461 SetLayerPropertiesForTesting(root.get(),
5462 identity_matrix,
5463 gfx::Point3F(),
5464 gfx::PointF(),
5465 gfx::Size(100, 100),
5466 true,
5467 false);
5468 SetLayerPropertiesForTesting(child.get(),
5469 identity_matrix,
5470 gfx::Point3F(),
5471 gfx::PointF(),
5472 gfx::Size(100, 100),
5473 true,
5474 false);
5476 root->reset_needs_push_properties_for_testing();
5477 child->reset_needs_push_properties_for_testing();
5479 gfx::Size device_viewport_size = gfx::Size(100, 100);
5480 RenderSurfaceLayerList render_surface_layer_list;
5481 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5482 root.get(), device_viewport_size, &render_surface_layer_list);
5483 inputs.device_scale_factor = 1.f;
5484 inputs.can_adjust_raster_scales = true;
5485 inputs.verify_property_trees = false;
5487 // This will change both layers' content bounds.
5488 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5489 EXPECT_TRUE(root->needs_push_properties());
5490 EXPECT_TRUE(child->needs_push_properties());
5492 root->reset_needs_push_properties_for_testing();
5493 child->reset_needs_push_properties_for_testing();
5495 // This will change only the child layer's contents scale and content bounds,
5496 // since the root layer is not a ContentsScalingLayer.
5497 inputs.device_scale_factor = 2.f;
5498 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5499 EXPECT_FALSE(root->needs_push_properties());
5500 EXPECT_TRUE(child->needs_push_properties());
5502 root->reset_needs_push_properties_for_testing();
5503 child->reset_needs_push_properties_for_testing();
5505 // This will not change either layer's contents scale or content bounds.
5506 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5507 EXPECT_FALSE(root->needs_push_properties());
5508 EXPECT_FALSE(child->needs_push_properties());
5511 TEST_F(LayerTreeHostCommonTest, RenderSurfaceTransformsInHighDPI) {
5512 MockContentLayerClient delegate;
5513 gfx::Transform identity_matrix;
5515 scoped_refptr<FakePictureLayer> parent =
5516 CreateDrawablePictureLayer(layer_settings(), &delegate);
5517 SetLayerPropertiesForTesting(parent.get(),
5518 identity_matrix,
5519 gfx::Point3F(),
5520 gfx::PointF(),
5521 gfx::Size(30, 30),
5522 false,
5523 true);
5525 scoped_refptr<FakePictureLayer> child =
5526 CreateDrawablePictureLayer(layer_settings(), &delegate);
5527 SetLayerPropertiesForTesting(child.get(),
5528 identity_matrix,
5529 gfx::Point3F(),
5530 gfx::PointF(2.f, 2.f),
5531 gfx::Size(10, 10),
5532 false,
5533 true);
5535 gfx::Transform replica_transform;
5536 replica_transform.Scale(1.0, -1.0);
5537 scoped_refptr<FakePictureLayer> replica =
5538 CreateDrawablePictureLayer(layer_settings(), &delegate);
5539 SetLayerPropertiesForTesting(replica.get(),
5540 replica_transform,
5541 gfx::Point3F(),
5542 gfx::PointF(2.f, 2.f),
5543 gfx::Size(10, 10),
5544 false,
5545 true);
5547 // This layer should end up in the same surface as child, with the same draw
5548 // and screen space transforms.
5549 scoped_refptr<FakePictureLayer> duplicate_child_non_owner =
5550 CreateDrawablePictureLayer(layer_settings(), &delegate);
5551 SetLayerPropertiesForTesting(duplicate_child_non_owner.get(),
5552 identity_matrix,
5553 gfx::Point3F(),
5554 gfx::PointF(),
5555 gfx::Size(10, 10),
5556 false,
5557 true);
5559 parent->AddChild(child);
5560 child->AddChild(duplicate_child_non_owner);
5561 child->SetReplicaLayer(replica.get());
5563 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5564 host->SetRootLayer(parent);
5566 RenderSurfaceLayerList render_surface_layer_list;
5568 float device_scale_factor = 1.5f;
5569 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5570 parent.get(), parent->bounds(), &render_surface_layer_list);
5571 inputs.device_scale_factor = device_scale_factor;
5572 inputs.can_adjust_raster_scales = true;
5573 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5575 // We should have two render surfaces. The root's render surface and child's
5576 // render surface (it needs one because it has a replica layer).
5577 EXPECT_EQ(2u, render_surface_layer_list.size());
5579 gfx::Transform expected_parent_transform;
5580 expected_parent_transform.Scale(device_scale_factor, device_scale_factor);
5581 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
5582 parent->screen_space_transform());
5583 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_parent_transform,
5584 parent->draw_transform());
5586 gfx::Transform expected_draw_transform;
5587 expected_draw_transform.Scale(device_scale_factor, device_scale_factor);
5588 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_draw_transform,
5589 child->draw_transform());
5591 gfx::Transform expected_screen_space_transform;
5592 expected_screen_space_transform.Scale(device_scale_factor,
5593 device_scale_factor);
5594 expected_screen_space_transform.Translate(child->position().x(),
5595 child->position().y());
5596 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_screen_space_transform,
5597 child->screen_space_transform());
5599 gfx::Transform expected_duplicate_child_draw_transform =
5600 child->draw_transform();
5601 EXPECT_TRANSFORMATION_MATRIX_EQ(child->draw_transform(),
5602 duplicate_child_non_owner->draw_transform());
5603 EXPECT_TRANSFORMATION_MATRIX_EQ(
5604 child->screen_space_transform(),
5605 duplicate_child_non_owner->screen_space_transform());
5606 EXPECT_EQ(child->drawable_content_rect(),
5607 duplicate_child_non_owner->drawable_content_rect());
5608 EXPECT_EQ(child->content_bounds(),
5609 duplicate_child_non_owner->content_bounds());
5611 gfx::Transform expected_render_surface_draw_transform;
5612 expected_render_surface_draw_transform.Translate(
5613 device_scale_factor * child->position().x(),
5614 device_scale_factor * child->position().y());
5615 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_render_surface_draw_transform,
5616 child->render_surface()->draw_transform());
5618 gfx::Transform expected_surface_draw_transform;
5619 expected_surface_draw_transform.Translate(device_scale_factor * 2.f,
5620 device_scale_factor * 2.f);
5621 EXPECT_TRANSFORMATION_MATRIX_EQ(expected_surface_draw_transform,
5622 child->render_surface()->draw_transform());
5624 gfx::Transform expected_surface_screen_space_transform;
5625 expected_surface_screen_space_transform.Translate(device_scale_factor * 2.f,
5626 device_scale_factor * 2.f);
5627 EXPECT_TRANSFORMATION_MATRIX_EQ(
5628 expected_surface_screen_space_transform,
5629 child->render_surface()->screen_space_transform());
5631 gfx::Transform expected_replica_draw_transform;
5632 expected_replica_draw_transform.matrix().set(1, 1, -1.0);
5633 expected_replica_draw_transform.matrix().set(0, 3, 6.0);
5634 expected_replica_draw_transform.matrix().set(1, 3, 6.0);
5635 EXPECT_TRANSFORMATION_MATRIX_EQ(
5636 expected_replica_draw_transform,
5637 child->render_surface()->replica_draw_transform());
5639 gfx::Transform expected_replica_screen_space_transform;
5640 expected_replica_screen_space_transform.matrix().set(1, 1, -1.0);
5641 expected_replica_screen_space_transform.matrix().set(0, 3, 6.0);
5642 expected_replica_screen_space_transform.matrix().set(1, 3, 6.0);
5643 EXPECT_TRANSFORMATION_MATRIX_EQ(
5644 expected_replica_screen_space_transform,
5645 child->render_surface()->replica_screen_space_transform());
5646 EXPECT_TRANSFORMATION_MATRIX_EQ(
5647 expected_replica_screen_space_transform,
5648 child->render_surface()->replica_screen_space_transform());
5651 TEST_F(LayerTreeHostCommonTest,
5652 RenderSurfaceTransformsInHighDPIAccurateScaleZeroPosition) {
5653 MockContentLayerClient delegate;
5654 gfx::Transform identity_matrix;
5656 scoped_refptr<FakePictureLayer> parent =
5657 CreateDrawablePictureLayer(layer_settings(), &delegate);
5658 SetLayerPropertiesForTesting(parent.get(),
5659 identity_matrix,
5660 gfx::Point3F(),
5661 gfx::PointF(),
5662 gfx::Size(33, 31),
5663 false,
5664 true);
5666 scoped_refptr<FakePictureLayer> child =
5667 CreateDrawablePictureLayer(layer_settings(), &delegate);
5668 SetLayerPropertiesForTesting(child.get(),
5669 identity_matrix,
5670 gfx::Point3F(),
5671 gfx::PointF(),
5672 gfx::Size(13, 11),
5673 false,
5674 true);
5676 gfx::Transform replica_transform;
5677 replica_transform.Scale(1.0, -1.0);
5678 scoped_refptr<FakePictureLayer> replica =
5679 CreateDrawablePictureLayer(layer_settings(), &delegate);
5680 SetLayerPropertiesForTesting(replica.get(),
5681 replica_transform,
5682 gfx::Point3F(),
5683 gfx::PointF(),
5684 gfx::Size(13, 11),
5685 false,
5686 true);
5688 parent->AddChild(child);
5689 child->SetReplicaLayer(replica.get());
5691 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5692 host->SetRootLayer(parent);
5694 float device_scale_factor = 1.7f;
5696 RenderSurfaceLayerList render_surface_layer_list;
5697 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
5698 parent.get(), parent->bounds(), &render_surface_layer_list);
5699 inputs.device_scale_factor = device_scale_factor;
5700 inputs.can_adjust_raster_scales = true;
5701 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5703 // We should have two render surfaces. The root's render surface and child's
5704 // render surface (it needs one because it has a replica layer).
5705 EXPECT_EQ(2u, render_surface_layer_list.size());
5707 gfx::Transform identity_transform;
5708 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
5709 child->render_surface()->draw_transform());
5710 EXPECT_TRANSFORMATION_MATRIX_EQ(identity_transform,
5711 child->render_surface()->draw_transform());
5712 EXPECT_TRANSFORMATION_MATRIX_EQ(
5713 identity_transform, child->render_surface()->screen_space_transform());
5715 gfx::Transform expected_replica_draw_transform;
5716 expected_replica_draw_transform.matrix().set(1, 1, -1.0);
5717 EXPECT_TRANSFORMATION_MATRIX_EQ(
5718 expected_replica_draw_transform,
5719 child->render_surface()->replica_draw_transform());
5721 gfx::Transform expected_replica_screen_space_transform;
5722 expected_replica_screen_space_transform.matrix().set(1, 1, -1.0);
5723 EXPECT_TRANSFORMATION_MATRIX_EQ(
5724 expected_replica_screen_space_transform,
5725 child->render_surface()->replica_screen_space_transform());
5728 TEST_F(LayerTreeHostCommonTest, SubtreeSearch) {
5729 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5730 scoped_refptr<Layer> child = Layer::Create(layer_settings());
5731 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
5732 scoped_refptr<Layer> mask_layer = Layer::Create(layer_settings());
5733 scoped_refptr<Layer> replica_layer = Layer::Create(layer_settings());
5735 grand_child->SetReplicaLayer(replica_layer.get());
5736 child->AddChild(grand_child.get());
5737 child->SetMaskLayer(mask_layer.get());
5738 root->AddChild(child.get());
5740 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5741 host->SetRootLayer(root);
5743 int nonexistent_id = -1;
5744 EXPECT_EQ(root.get(),
5745 LayerTreeHostCommon::FindLayerInSubtree(root.get(), root->id()));
5746 EXPECT_EQ(child.get(),
5747 LayerTreeHostCommon::FindLayerInSubtree(root.get(), child->id()));
5748 EXPECT_EQ(
5749 grand_child.get(),
5750 LayerTreeHostCommon::FindLayerInSubtree(root.get(), grand_child->id()));
5751 EXPECT_EQ(
5752 mask_layer.get(),
5753 LayerTreeHostCommon::FindLayerInSubtree(root.get(), mask_layer->id()));
5754 EXPECT_EQ(
5755 replica_layer.get(),
5756 LayerTreeHostCommon::FindLayerInSubtree(root.get(), replica_layer->id()));
5757 EXPECT_EQ(
5758 0, LayerTreeHostCommon::FindLayerInSubtree(root.get(), nonexistent_id));
5761 TEST_F(LayerTreeHostCommonTest, TransparentChildRenderSurfaceCreation) {
5762 scoped_refptr<Layer> root = Layer::Create(layer_settings());
5763 scoped_refptr<Layer> child = Layer::Create(layer_settings());
5764 scoped_refptr<LayerWithForcedDrawsContent> grand_child =
5765 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
5767 const gfx::Transform identity_matrix;
5768 SetLayerPropertiesForTesting(root.get(),
5769 identity_matrix,
5770 gfx::Point3F(),
5771 gfx::PointF(),
5772 gfx::Size(100, 100),
5773 true,
5774 false);
5775 SetLayerPropertiesForTesting(child.get(),
5776 identity_matrix,
5777 gfx::Point3F(),
5778 gfx::PointF(),
5779 gfx::Size(10, 10),
5780 true,
5781 false);
5782 SetLayerPropertiesForTesting(grand_child.get(),
5783 identity_matrix,
5784 gfx::Point3F(),
5785 gfx::PointF(),
5786 gfx::Size(10, 10),
5787 true,
5788 false);
5790 root->AddChild(child);
5791 child->AddChild(grand_child);
5792 child->SetOpacity(0.5f);
5794 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
5795 host->SetRootLayer(root);
5797 ExecuteCalculateDrawProperties(root.get());
5799 EXPECT_FALSE(child->render_surface());
5802 TEST_F(LayerTreeHostCommonTest, OpacityAnimatingOnPendingTree) {
5803 FakeImplProxy proxy;
5804 TestSharedBitmapManager shared_bitmap_manager;
5805 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
5806 host_impl.CreatePendingTree();
5807 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
5809 const gfx::Transform identity_matrix;
5810 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
5811 gfx::PointF(), gfx::Size(100, 100), true, false,
5812 false);
5813 root->SetDrawsContent(true);
5815 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
5816 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
5817 gfx::PointF(), gfx::Size(50, 50), true, false,
5818 false);
5819 child->SetDrawsContent(true);
5820 child->SetOpacity(0.0f);
5822 // Add opacity animation.
5823 AddOpacityTransitionToController(
5824 child->layer_animation_controller(), 10.0, 0.0f, 1.0f, false);
5826 root->AddChild(child.Pass());
5827 root->SetHasRenderSurface(true);
5829 LayerImplList render_surface_layer_list;
5830 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
5831 root.get(), root->bounds(), &render_surface_layer_list);
5832 inputs.can_adjust_raster_scales = true;
5833 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
5835 // We should have one render surface and two layers. The child
5836 // layer should be included even though it is transparent.
5837 ASSERT_EQ(1u, render_surface_layer_list.size());
5838 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
5841 using LCDTextTestParam = std::tr1::tuple<bool, bool, bool>;
5842 class LCDTextTest
5843 : public LayerTreeHostCommonTestBase,
5844 public testing::TestWithParam<LCDTextTestParam> {
5845 public:
5846 LCDTextTest()
5847 : host_impl_(&proxy_, &shared_bitmap_manager_, &task_graph_runner_),
5848 root_(nullptr),
5849 child_(nullptr),
5850 grand_child_(nullptr) {}
5852 protected:
5853 void SetUp() override {
5854 can_use_lcd_text_ = std::tr1::get<0>(GetParam());
5855 layers_always_allowed_lcd_text_ = std::tr1::get<1>(GetParam());
5857 scoped_ptr<LayerImpl> root_ptr =
5858 LayerImpl::Create(host_impl_.active_tree(), 1);
5859 scoped_ptr<LayerImpl> child_ptr =
5860 LayerImpl::Create(host_impl_.active_tree(), 2);
5861 scoped_ptr<LayerImpl> grand_child_ptr =
5862 LayerImpl::Create(host_impl_.active_tree(), 3);
5864 // Stash raw pointers to look at later.
5865 root_ = root_ptr.get();
5866 child_ = child_ptr.get();
5867 grand_child_ = grand_child_ptr.get();
5869 child_->AddChild(grand_child_ptr.Pass());
5870 root_->AddChild(child_ptr.Pass());
5871 host_impl_.active_tree()->SetRootLayer(root_ptr.Pass());
5873 root_->SetContentsOpaque(true);
5874 child_->SetContentsOpaque(true);
5875 grand_child_->SetContentsOpaque(true);
5877 gfx::Transform identity_matrix;
5878 SetLayerPropertiesForTesting(root_, identity_matrix, gfx::Point3F(),
5879 gfx::PointF(), gfx::Size(1, 1), true, false,
5880 true);
5881 SetLayerPropertiesForTesting(child_, identity_matrix, gfx::Point3F(),
5882 gfx::PointF(), gfx::Size(1, 1), true, false,
5883 std::tr1::get<2>(GetParam()));
5884 SetLayerPropertiesForTesting(grand_child_, identity_matrix, gfx::Point3F(),
5885 gfx::PointF(), gfx::Size(1, 1), true, false,
5886 false);
5889 bool can_use_lcd_text_;
5890 bool layers_always_allowed_lcd_text_;
5892 FakeImplProxy proxy_;
5893 TestSharedBitmapManager shared_bitmap_manager_;
5894 TestTaskGraphRunner task_graph_runner_;
5895 FakeLayerTreeHostImpl host_impl_;
5897 LayerImpl* root_;
5898 LayerImpl* child_;
5899 LayerImpl* grand_child_;
5902 TEST_P(LCDTextTest, CanUseLCDText) {
5903 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
5904 bool expect_not_lcd_text = layers_always_allowed_lcd_text_;
5906 // Case 1: Identity transform.
5907 gfx::Transform identity_matrix;
5908 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5909 layers_always_allowed_lcd_text_);
5910 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5911 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5912 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5914 // Case 2: Integral translation.
5915 gfx::Transform integral_translation;
5916 integral_translation.Translate(1.0, 2.0);
5917 child_->SetTransform(integral_translation);
5918 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5919 layers_always_allowed_lcd_text_);
5920 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5921 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5922 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5924 // Case 3: Non-integral translation.
5925 gfx::Transform non_integral_translation;
5926 non_integral_translation.Translate(1.5, 2.5);
5927 child_->SetTransform(non_integral_translation);
5928 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5929 layers_always_allowed_lcd_text_);
5930 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5931 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5932 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5934 // Case 4: Rotation.
5935 gfx::Transform rotation;
5936 rotation.Rotate(10.0);
5937 child_->SetTransform(rotation);
5938 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5939 layers_always_allowed_lcd_text_);
5940 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5941 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5942 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5944 // Case 5: Scale.
5945 gfx::Transform scale;
5946 scale.Scale(2.0, 2.0);
5947 child_->SetTransform(scale);
5948 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5949 layers_always_allowed_lcd_text_);
5950 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5951 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5952 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5954 // Case 6: Skew.
5955 gfx::Transform skew;
5956 skew.SkewX(10.0);
5957 child_->SetTransform(skew);
5958 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5959 layers_always_allowed_lcd_text_);
5960 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5961 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5962 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5964 // Case 7: Translucent.
5965 child_->SetTransform(identity_matrix);
5966 child_->SetOpacity(0.5f);
5967 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5968 layers_always_allowed_lcd_text_);
5969 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5970 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5971 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
5973 // Case 8: Sanity check: restore transform and opacity.
5974 child_->SetTransform(identity_matrix);
5975 child_->SetOpacity(1.f);
5976 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5977 layers_always_allowed_lcd_text_);
5978 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5979 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5980 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5982 // Case 9: Non-opaque content.
5983 child_->SetContentsOpaque(false);
5984 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5985 layers_always_allowed_lcd_text_);
5986 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5987 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
5988 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5990 // Case 10: Sanity check: restore content opaqueness.
5991 child_->SetContentsOpaque(true);
5992 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
5993 layers_always_allowed_lcd_text_);
5994 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
5995 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
5996 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
5999 TEST_P(LCDTextTest, CanUseLCDTextWithAnimation) {
6000 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
6001 bool expect_not_lcd_text = layers_always_allowed_lcd_text_;
6003 // Sanity check: Make sure can_use_lcd_text_ is set on each node.
6004 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
6005 layers_always_allowed_lcd_text_);
6006 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
6007 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
6008 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
6010 // Add opacity animation.
6011 child_->SetOpacity(0.9f);
6012 AddOpacityTransitionToController(
6013 child_->layer_animation_controller(), 10.0, 0.9f, 0.1f, false);
6015 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
6016 layers_always_allowed_lcd_text_);
6017 // Text LCD should be adjusted while animation is active.
6018 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
6019 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
6020 EXPECT_EQ(expect_not_lcd_text, grand_child_->can_use_lcd_text());
6023 TEST_P(LCDTextTest, CanUseLCDTextWithAnimationContentsOpaque) {
6024 bool expect_lcd_text = can_use_lcd_text_ || layers_always_allowed_lcd_text_;
6025 bool expect_not_lcd_text = layers_always_allowed_lcd_text_;
6027 // Sanity check: Make sure can_use_lcd_text_ is set on each node.
6028 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
6029 layers_always_allowed_lcd_text_);
6030 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
6031 EXPECT_EQ(expect_lcd_text, child_->can_use_lcd_text());
6032 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
6034 // Mark contents non-opaque within the first animation frame.
6035 child_->SetContentsOpaque(false);
6036 AddOpacityTransitionToController(child_->layer_animation_controller(), 10.0,
6037 0.9f, 0.1f, false);
6039 ExecuteCalculateDrawProperties(root_, 1.f, 1.f, NULL, can_use_lcd_text_,
6040 layers_always_allowed_lcd_text_);
6041 // LCD text should be disabled for non-opaque layers even during animations.
6042 EXPECT_EQ(expect_lcd_text, root_->can_use_lcd_text());
6043 EXPECT_EQ(expect_not_lcd_text, child_->can_use_lcd_text());
6044 EXPECT_EQ(expect_lcd_text, grand_child_->can_use_lcd_text());
6047 INSTANTIATE_TEST_CASE_P(LayerTreeHostCommonTest,
6048 LCDTextTest,
6049 testing::Combine(testing::Bool(),
6050 testing::Bool(),
6051 testing::Bool()));
6053 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_SingleLayer) {
6054 FakeImplProxy proxy;
6055 TestSharedBitmapManager shared_bitmap_manager;
6056 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6057 host_impl.CreatePendingTree();
6058 const gfx::Transform identity_matrix;
6060 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6061 SetLayerPropertiesForTesting(root.get(),
6062 identity_matrix,
6063 gfx::Point3F(),
6064 gfx::PointF(),
6065 gfx::Size(50, 50),
6066 true,
6067 false);
6068 root->SetIsDrawable(true);
6070 scoped_refptr<Layer> child = Layer::Create(layer_settings());
6071 SetLayerPropertiesForTesting(child.get(),
6072 identity_matrix,
6073 gfx::Point3F(),
6074 gfx::PointF(),
6075 gfx::Size(40, 40),
6076 true,
6077 false);
6078 child->SetIsDrawable(true);
6080 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
6081 SetLayerPropertiesForTesting(grand_child.get(),
6082 identity_matrix,
6083 gfx::Point3F(),
6084 gfx::PointF(),
6085 gfx::Size(30, 30),
6086 true,
6087 false);
6088 grand_child->SetIsDrawable(true);
6089 grand_child->SetHideLayerAndSubtree(true);
6091 child->AddChild(grand_child);
6092 root->AddChild(child);
6094 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6095 host->SetRootLayer(root);
6097 RenderSurfaceLayerList render_surface_layer_list;
6098 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting 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(root->id(), root->render_surface()->layer_list().at(0)->id());
6108 EXPECT_EQ(child->id(), root->render_surface()->layer_list().at(1)->id());
6111 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_SingleLayerImpl) {
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_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
6119 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
6120 gfx::PointF(), gfx::Size(50, 50), true, false,
6121 false);
6122 root->SetDrawsContent(true);
6124 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
6125 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
6126 gfx::PointF(), gfx::Size(40, 40), true, false,
6127 false);
6128 child->SetDrawsContent(true);
6130 scoped_ptr<LayerImpl> grand_child =
6131 LayerImpl::Create(host_impl.pending_tree(), 3);
6132 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
6133 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6134 true, false, false);
6135 grand_child->SetDrawsContent(true);
6136 grand_child->SetHideLayerAndSubtree(true);
6138 child->AddChild(grand_child.Pass());
6139 root->AddChild(child.Pass());
6140 root->SetHasRenderSurface(true);
6142 LayerImplList render_surface_layer_list;
6143 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6144 root.get(), root->bounds(), &render_surface_layer_list);
6145 inputs.can_adjust_raster_scales = true;
6146 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6148 // We should have one render surface and two layers. The grand child has
6149 // hidden itself.
6150 ASSERT_EQ(1u, render_surface_layer_list.size());
6151 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6152 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id());
6153 EXPECT_EQ(2, root->render_surface()->layer_list().at(1)->id());
6156 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_TwoLayers) {
6157 FakeImplProxy proxy;
6158 TestSharedBitmapManager shared_bitmap_manager;
6159 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6160 host_impl.CreatePendingTree();
6161 const gfx::Transform identity_matrix;
6163 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6164 SetLayerPropertiesForTesting(root.get(),
6165 identity_matrix,
6166 gfx::Point3F(),
6167 gfx::PointF(),
6168 gfx::Size(50, 50),
6169 true,
6170 false);
6171 root->SetIsDrawable(true);
6173 scoped_refptr<Layer> child = Layer::Create(layer_settings());
6174 SetLayerPropertiesForTesting(child.get(),
6175 identity_matrix,
6176 gfx::Point3F(),
6177 gfx::PointF(),
6178 gfx::Size(40, 40),
6179 true,
6180 false);
6181 child->SetIsDrawable(true);
6182 child->SetHideLayerAndSubtree(true);
6184 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings());
6185 SetLayerPropertiesForTesting(grand_child.get(),
6186 identity_matrix,
6187 gfx::Point3F(),
6188 gfx::PointF(),
6189 gfx::Size(30, 30),
6190 true,
6191 false);
6192 grand_child->SetIsDrawable(true);
6194 child->AddChild(grand_child);
6195 root->AddChild(child);
6197 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6198 host->SetRootLayer(root);
6200 RenderSurfaceLayerList render_surface_layer_list;
6201 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6202 root.get(), root->bounds(), &render_surface_layer_list);
6203 inputs.can_adjust_raster_scales = true;
6204 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6206 // We should have one render surface and one layers. The child has
6207 // hidden itself and the grand child.
6208 ASSERT_EQ(1u, render_surface_layer_list.size());
6209 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6210 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6213 TEST_F(LayerTreeHostCommonTest, SubtreeHidden_TwoLayersImpl) {
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_ptr<LayerImpl> root = LayerImpl::Create(host_impl.pending_tree(), 1);
6221 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
6222 gfx::PointF(), gfx::Size(50, 50), true, false,
6223 true);
6224 root->SetDrawsContent(true);
6226 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.pending_tree(), 2);
6227 SetLayerPropertiesForTesting(child.get(), identity_matrix, gfx::Point3F(),
6228 gfx::PointF(), gfx::Size(40, 40), true, false,
6229 false);
6230 child->SetDrawsContent(true);
6231 child->SetHideLayerAndSubtree(true);
6233 scoped_ptr<LayerImpl> grand_child =
6234 LayerImpl::Create(host_impl.pending_tree(), 3);
6235 SetLayerPropertiesForTesting(grand_child.get(), identity_matrix,
6236 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
6237 true, false, false);
6238 grand_child->SetDrawsContent(true);
6240 child->AddChild(grand_child.Pass());
6241 root->AddChild(child.Pass());
6243 LayerImplList render_surface_layer_list;
6244 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
6245 root.get(), root->bounds(), &render_surface_layer_list);
6246 inputs.can_adjust_raster_scales = true;
6247 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6249 // We should have one render surface and one layers. The child has
6250 // hidden itself and the grand child.
6251 ASSERT_EQ(1u, render_surface_layer_list.size());
6252 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6253 EXPECT_EQ(1, root->render_surface()->layer_list().at(0)->id());
6256 void EmptyCopyOutputCallback(scoped_ptr<CopyOutputResult> result) {}
6258 TEST_F(LayerTreeHostCommonTest, SubtreeHiddenWithCopyRequest) {
6259 FakeImplProxy proxy;
6260 TestSharedBitmapManager shared_bitmap_manager;
6261 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6262 host_impl.CreatePendingTree();
6263 const gfx::Transform identity_matrix;
6265 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6266 SetLayerPropertiesForTesting(root.get(),
6267 identity_matrix,
6268 gfx::Point3F(),
6269 gfx::PointF(),
6270 gfx::Size(50, 50),
6271 true,
6272 false);
6273 root->SetIsDrawable(true);
6275 scoped_refptr<Layer> copy_grand_parent = Layer::Create(layer_settings());
6276 SetLayerPropertiesForTesting(copy_grand_parent.get(),
6277 identity_matrix,
6278 gfx::Point3F(),
6279 gfx::PointF(),
6280 gfx::Size(40, 40),
6281 true,
6282 false);
6283 copy_grand_parent->SetIsDrawable(true);
6285 scoped_refptr<Layer> copy_parent = Layer::Create(layer_settings());
6286 SetLayerPropertiesForTesting(copy_parent.get(),
6287 identity_matrix,
6288 gfx::Point3F(),
6289 gfx::PointF(),
6290 gfx::Size(30, 30),
6291 true,
6292 false);
6293 copy_parent->SetIsDrawable(true);
6294 copy_parent->SetForceRenderSurface(true);
6296 scoped_refptr<Layer> copy_layer = Layer::Create(layer_settings());
6297 SetLayerPropertiesForTesting(copy_layer.get(),
6298 identity_matrix,
6299 gfx::Point3F(),
6300 gfx::PointF(),
6301 gfx::Size(20, 20),
6302 true,
6303 false);
6304 copy_layer->SetIsDrawable(true);
6306 scoped_refptr<Layer> copy_child = Layer::Create(layer_settings());
6307 SetLayerPropertiesForTesting(copy_child.get(),
6308 identity_matrix,
6309 gfx::Point3F(),
6310 gfx::PointF(),
6311 gfx::Size(20, 20),
6312 true,
6313 false);
6314 copy_child->SetIsDrawable(true);
6316 scoped_refptr<Layer> copy_grand_parent_sibling_before =
6317 Layer::Create(layer_settings());
6318 SetLayerPropertiesForTesting(copy_grand_parent_sibling_before.get(),
6319 identity_matrix,
6320 gfx::Point3F(),
6321 gfx::PointF(),
6322 gfx::Size(40, 40),
6323 true,
6324 false);
6325 copy_grand_parent_sibling_before->SetIsDrawable(true);
6327 scoped_refptr<Layer> copy_grand_parent_sibling_after =
6328 Layer::Create(layer_settings());
6329 SetLayerPropertiesForTesting(copy_grand_parent_sibling_after.get(),
6330 identity_matrix,
6331 gfx::Point3F(),
6332 gfx::PointF(),
6333 gfx::Size(40, 40),
6334 true,
6335 false);
6336 copy_grand_parent_sibling_after->SetIsDrawable(true);
6338 copy_layer->AddChild(copy_child);
6339 copy_parent->AddChild(copy_layer);
6340 copy_grand_parent->AddChild(copy_parent);
6341 root->AddChild(copy_grand_parent_sibling_before);
6342 root->AddChild(copy_grand_parent);
6343 root->AddChild(copy_grand_parent_sibling_after);
6345 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6346 host->SetRootLayer(root);
6348 // Hide the copy_grand_parent and its subtree. But make a copy request in that
6349 // hidden subtree on copy_layer.
6350 copy_grand_parent->SetHideLayerAndSubtree(true);
6351 copy_grand_parent_sibling_before->SetHideLayerAndSubtree(true);
6352 copy_grand_parent_sibling_after->SetHideLayerAndSubtree(true);
6353 copy_layer->RequestCopyOfOutput(CopyOutputRequest::CreateRequest(
6354 base::Bind(&EmptyCopyOutputCallback)));
6355 EXPECT_TRUE(copy_layer->HasCopyRequest());
6357 RenderSurfaceLayerList render_surface_layer_list;
6358 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6359 root.get(), root->bounds(), &render_surface_layer_list);
6360 inputs.can_adjust_raster_scales = true;
6361 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6363 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
6364 EXPECT_TRUE(copy_grand_parent->draw_properties().
6365 layer_or_descendant_has_copy_request);
6366 EXPECT_TRUE(copy_parent->draw_properties().
6367 layer_or_descendant_has_copy_request);
6368 EXPECT_TRUE(copy_layer->draw_properties().
6369 layer_or_descendant_has_copy_request);
6370 EXPECT_FALSE(copy_child->draw_properties().
6371 layer_or_descendant_has_copy_request);
6372 EXPECT_FALSE(copy_grand_parent_sibling_before->draw_properties().
6373 layer_or_descendant_has_copy_request);
6374 EXPECT_FALSE(copy_grand_parent_sibling_after->draw_properties().
6375 layer_or_descendant_has_copy_request);
6377 // We should have three render surfaces, one for the root, one for the parent
6378 // since it owns a surface, and one for the copy_layer.
6379 ASSERT_EQ(3u, render_surface_layer_list.size());
6380 EXPECT_EQ(root->id(), render_surface_layer_list.at(0)->id());
6381 EXPECT_EQ(copy_parent->id(), render_surface_layer_list.at(1)->id());
6382 EXPECT_EQ(copy_layer->id(), render_surface_layer_list.at(2)->id());
6384 // The root render surface should have 2 contributing layers. The
6385 // copy_grand_parent is hidden along with its siblings, but the copy_parent
6386 // will appear since something in its subtree needs to be drawn for a copy
6387 // request.
6388 ASSERT_EQ(2u, root->render_surface()->layer_list().size());
6389 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6390 EXPECT_EQ(copy_parent->id(),
6391 root->render_surface()->layer_list().at(1)->id());
6393 // Nothing actually draws into the copy parent, so only the copy_layer will
6394 // appear in its list, since it needs to be drawn for the copy request.
6395 ASSERT_EQ(1u, copy_parent->render_surface()->layer_list().size());
6396 EXPECT_EQ(copy_layer->id(),
6397 copy_parent->render_surface()->layer_list().at(0)->id());
6399 // The copy_layer's render surface should have two contributing layers.
6400 ASSERT_EQ(2u, copy_layer->render_surface()->layer_list().size());
6401 EXPECT_EQ(copy_layer->id(),
6402 copy_layer->render_surface()->layer_list().at(0)->id());
6403 EXPECT_EQ(copy_child->id(),
6404 copy_layer->render_surface()->layer_list().at(1)->id());
6407 TEST_F(LayerTreeHostCommonTest, ClippedOutCopyRequest) {
6408 FakeImplProxy proxy;
6409 TestSharedBitmapManager shared_bitmap_manager;
6410 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6411 host_impl.CreatePendingTree();
6412 const gfx::Transform identity_matrix;
6414 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6415 SetLayerPropertiesForTesting(root.get(),
6416 identity_matrix,
6417 gfx::Point3F(),
6418 gfx::PointF(),
6419 gfx::Size(50, 50),
6420 true,
6421 false);
6422 root->SetIsDrawable(true);
6424 scoped_refptr<Layer> copy_parent = Layer::Create(layer_settings());
6425 SetLayerPropertiesForTesting(copy_parent.get(),
6426 identity_matrix,
6427 gfx::Point3F(),
6428 gfx::PointF(),
6429 gfx::Size(),
6430 true,
6431 false);
6432 copy_parent->SetIsDrawable(true);
6433 copy_parent->SetMasksToBounds(true);
6435 scoped_refptr<Layer> copy_layer = Layer::Create(layer_settings());
6436 SetLayerPropertiesForTesting(copy_layer.get(),
6437 identity_matrix,
6438 gfx::Point3F(),
6439 gfx::PointF(),
6440 gfx::Size(30, 30),
6441 true,
6442 false);
6443 copy_layer->SetIsDrawable(true);
6445 scoped_refptr<Layer> copy_child = Layer::Create(layer_settings());
6446 SetLayerPropertiesForTesting(copy_child.get(),
6447 identity_matrix,
6448 gfx::Point3F(),
6449 gfx::PointF(),
6450 gfx::Size(20, 20),
6451 true,
6452 false);
6453 copy_child->SetIsDrawable(true);
6455 copy_layer->AddChild(copy_child);
6456 copy_parent->AddChild(copy_layer);
6457 root->AddChild(copy_parent);
6459 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6460 host->SetRootLayer(root);
6462 copy_layer->RequestCopyOfOutput(CopyOutputRequest::CreateRequest(
6463 base::Bind(&EmptyCopyOutputCallback)));
6464 EXPECT_TRUE(copy_layer->HasCopyRequest());
6466 RenderSurfaceLayerList render_surface_layer_list;
6467 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6468 root.get(), root->bounds(), &render_surface_layer_list);
6469 inputs.can_adjust_raster_scales = true;
6470 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6472 // We should have one render surface, as the others are clipped out.
6473 ASSERT_EQ(1u, render_surface_layer_list.size());
6474 EXPECT_EQ(root->id(), render_surface_layer_list.at(0)->id());
6476 // The root render surface should only have 1 contributing layer, since the
6477 // other layers are empty/clipped away.
6478 ASSERT_EQ(1u, root->render_surface()->layer_list().size());
6479 EXPECT_EQ(root->id(), root->render_surface()->layer_list().at(0)->id());
6482 TEST_F(LayerTreeHostCommonTest, VisibleContentRectInsideSurface) {
6483 FakeImplProxy proxy;
6484 TestSharedBitmapManager shared_bitmap_manager;
6485 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
6486 host_impl.CreatePendingTree();
6487 const gfx::Transform identity_matrix;
6489 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6490 SetLayerPropertiesForTesting(root.get(),
6491 identity_matrix,
6492 gfx::Point3F(),
6493 gfx::PointF(),
6494 gfx::Size(50, 50),
6495 true,
6496 false);
6497 root->SetIsDrawable(true);
6499 // The surface is moved slightly outside of the viewport.
6500 scoped_refptr<Layer> surface = Layer::Create(layer_settings());
6501 SetLayerPropertiesForTesting(surface.get(),
6502 identity_matrix,
6503 gfx::Point3F(),
6504 gfx::PointF(-10, -20),
6505 gfx::Size(),
6506 true,
6507 false);
6508 surface->SetForceRenderSurface(true);
6510 scoped_refptr<Layer> surface_child = Layer::Create(layer_settings());
6511 SetLayerPropertiesForTesting(surface_child.get(),
6512 identity_matrix,
6513 gfx::Point3F(),
6514 gfx::PointF(),
6515 gfx::Size(50, 50),
6516 true,
6517 false);
6518 surface_child->SetIsDrawable(true);
6520 surface->AddChild(surface_child);
6521 root->AddChild(surface);
6523 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6524 host->SetRootLayer(root);
6526 RenderSurfaceLayerList render_surface_layer_list;
6527 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
6528 root.get(), root->bounds(), &render_surface_layer_list);
6529 inputs.can_adjust_raster_scales = true;
6530 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
6532 // The visible_content_rect for the |surface_child| should not be clipped by
6533 // the viewport.
6534 EXPECT_EQ(gfx::Rect(50, 50).ToString(),
6535 surface_child->visible_content_rect().ToString());
6538 TEST_F(LayerTreeHostCommonTest, TransformedClipParent) {
6539 // Ensure that a transform between the layer and its render surface is not a
6540 // problem. Constructs the following layer tree.
6542 // root (a render surface)
6543 // + render_surface
6544 // + clip_parent (scaled)
6545 // + intervening_clipping_layer
6546 // + clip_child
6548 // The render surface should be resized correctly and the clip child should
6549 // inherit the right clip rect.
6550 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6551 scoped_refptr<Layer> render_surface = Layer::Create(layer_settings());
6552 scoped_refptr<Layer> clip_parent = Layer::Create(layer_settings());
6553 scoped_refptr<Layer> intervening = Layer::Create(layer_settings());
6554 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6555 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
6557 root->AddChild(render_surface);
6558 render_surface->AddChild(clip_parent);
6559 clip_parent->AddChild(intervening);
6560 intervening->AddChild(clip_child);
6562 clip_child->SetClipParent(clip_parent.get());
6564 intervening->SetMasksToBounds(true);
6565 clip_parent->SetMasksToBounds(true);
6567 render_surface->SetForceRenderSurface(true);
6569 gfx::Transform scale_transform;
6570 scale_transform.Scale(2, 2);
6572 gfx::Transform identity_transform;
6574 SetLayerPropertiesForTesting(root.get(),
6575 identity_transform,
6576 gfx::Point3F(),
6577 gfx::PointF(),
6578 gfx::Size(50, 50),
6579 true,
6580 false);
6581 SetLayerPropertiesForTesting(render_surface.get(),
6582 identity_transform,
6583 gfx::Point3F(),
6584 gfx::PointF(),
6585 gfx::Size(10, 10),
6586 true,
6587 false);
6588 SetLayerPropertiesForTesting(clip_parent.get(),
6589 scale_transform,
6590 gfx::Point3F(),
6591 gfx::PointF(1.f, 1.f),
6592 gfx::Size(10, 10),
6593 true,
6594 false);
6595 SetLayerPropertiesForTesting(intervening.get(),
6596 identity_transform,
6597 gfx::Point3F(),
6598 gfx::PointF(1.f, 1.f),
6599 gfx::Size(5, 5),
6600 true,
6601 false);
6602 SetLayerPropertiesForTesting(clip_child.get(),
6603 identity_transform,
6604 gfx::Point3F(),
6605 gfx::PointF(1.f, 1.f),
6606 gfx::Size(10, 10),
6607 true,
6608 false);
6610 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6611 host->SetRootLayer(root);
6613 ExecuteCalculateDrawProperties(root.get());
6615 ASSERT_TRUE(root->render_surface());
6616 ASSERT_TRUE(render_surface->render_surface());
6618 // Ensure that we've inherited our clip parent's clip and weren't affected
6619 // by the intervening clip layer.
6620 ASSERT_EQ(gfx::Rect(1, 1, 20, 20).ToString(),
6621 clip_parent->clip_rect().ToString());
6622 ASSERT_EQ(clip_parent->clip_rect().ToString(),
6623 clip_child->clip_rect().ToString());
6624 ASSERT_EQ(gfx::Rect(3, 3, 10, 10).ToString(),
6625 intervening->clip_rect().ToString());
6627 // Ensure that the render surface reports a content rect that has been grown
6628 // to accomodate for the clip child.
6629 ASSERT_EQ(gfx::Rect(5, 5, 16, 16).ToString(),
6630 render_surface->render_surface()->content_rect().ToString());
6632 // The above check implies the two below, but they nicely demonstrate that
6633 // we've grown, despite the intervening layer's clip.
6634 ASSERT_TRUE(clip_parent->clip_rect().Contains(
6635 render_surface->render_surface()->content_rect()));
6636 ASSERT_FALSE(intervening->clip_rect().Contains(
6637 render_surface->render_surface()->content_rect()));
6640 TEST_F(LayerTreeHostCommonTest, ClipParentWithInterveningRenderSurface) {
6641 // Ensure that intervening render surfaces are not a problem in the basic
6642 // case. In the following tree, both render surfaces should be resized to
6643 // accomodate for the clip child, despite an intervening clip.
6645 // root (a render surface)
6646 // + clip_parent (masks to bounds)
6647 // + render_surface1 (sets opacity)
6648 // + intervening (masks to bounds)
6649 // + render_surface2 (also sets opacity)
6650 // + clip_child
6652 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6653 scoped_refptr<Layer> clip_parent = Layer::Create(layer_settings());
6654 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
6655 scoped_refptr<Layer> intervening = Layer::Create(layer_settings());
6656 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
6657 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6658 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
6660 root->AddChild(clip_parent);
6661 clip_parent->AddChild(render_surface1);
6662 render_surface1->AddChild(intervening);
6663 intervening->AddChild(render_surface2);
6664 render_surface2->AddChild(clip_child);
6666 clip_child->SetClipParent(clip_parent.get());
6668 intervening->SetMasksToBounds(true);
6669 clip_parent->SetMasksToBounds(true);
6671 render_surface1->SetForceRenderSurface(true);
6672 render_surface2->SetForceRenderSurface(true);
6674 gfx::Transform translation_transform;
6675 translation_transform.Translate(2, 2);
6677 gfx::Transform identity_transform;
6678 SetLayerPropertiesForTesting(root.get(),
6679 identity_transform,
6680 gfx::Point3F(),
6681 gfx::PointF(),
6682 gfx::Size(50, 50),
6683 true,
6684 false);
6685 SetLayerPropertiesForTesting(clip_parent.get(),
6686 translation_transform,
6687 gfx::Point3F(),
6688 gfx::PointF(1.f, 1.f),
6689 gfx::Size(40, 40),
6690 true,
6691 false);
6692 SetLayerPropertiesForTesting(render_surface1.get(),
6693 identity_transform,
6694 gfx::Point3F(),
6695 gfx::PointF(),
6696 gfx::Size(10, 10),
6697 true,
6698 false);
6699 SetLayerPropertiesForTesting(intervening.get(),
6700 identity_transform,
6701 gfx::Point3F(),
6702 gfx::PointF(1.f, 1.f),
6703 gfx::Size(5, 5),
6704 true,
6705 false);
6706 SetLayerPropertiesForTesting(render_surface2.get(),
6707 identity_transform,
6708 gfx::Point3F(),
6709 gfx::PointF(),
6710 gfx::Size(10, 10),
6711 true,
6712 false);
6713 SetLayerPropertiesForTesting(clip_child.get(),
6714 identity_transform,
6715 gfx::Point3F(),
6716 gfx::PointF(-10.f, -10.f),
6717 gfx::Size(60, 60),
6718 true,
6719 false);
6721 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6722 host->SetRootLayer(root);
6724 ExecuteCalculateDrawProperties(root.get());
6726 EXPECT_TRUE(root->render_surface());
6727 EXPECT_TRUE(render_surface1->render_surface());
6728 EXPECT_TRUE(render_surface2->render_surface());
6730 // Since the render surfaces could have expanded, they should not clip (their
6731 // bounds would no longer be reliable). We should resort to layer clipping
6732 // in this case.
6733 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6734 render_surface1->render_surface()->clip_rect().ToString());
6735 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
6736 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6737 render_surface2->render_surface()->clip_rect().ToString());
6738 EXPECT_FALSE(render_surface2->render_surface()->is_clipped());
6740 // NB: clip rects are in target space.
6741 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6742 render_surface1->clip_rect().ToString());
6743 EXPECT_TRUE(render_surface1->is_clipped());
6745 // This value is inherited from the clipping ancestor layer, 'intervening'.
6746 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
6747 render_surface2->clip_rect().ToString());
6748 EXPECT_TRUE(render_surface2->is_clipped());
6750 // The content rects of both render surfaces should both have expanded to
6751 // contain the clip child.
6752 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6753 render_surface1->render_surface()->content_rect().ToString());
6754 EXPECT_EQ(gfx::Rect(-1, -1, 40, 40).ToString(),
6755 render_surface2->render_surface()->content_rect().ToString());
6757 // The clip child should have inherited the clip parent's clip (projected to
6758 // the right space, of course), and should have the correctly sized visible
6759 // content rect.
6760 EXPECT_EQ(gfx::Rect(-1, -1, 40, 40).ToString(),
6761 clip_child->clip_rect().ToString());
6762 EXPECT_EQ(gfx::Rect(9, 9, 40, 40).ToString(),
6763 clip_child->visible_content_rect().ToString());
6764 EXPECT_TRUE(clip_child->is_clipped());
6767 TEST_F(LayerTreeHostCommonTest, ClipParentScrolledInterveningLayer) {
6768 // Ensure that intervening render surfaces are not a problem, even if there
6769 // is a scroll involved. Note, we do _not_ have to consider any other sort
6770 // of transform.
6772 // root (a render surface)
6773 // + clip_parent (masks to bounds)
6774 // + render_surface1 (sets opacity)
6775 // + intervening (masks to bounds AND scrolls)
6776 // + render_surface2 (also sets opacity)
6777 // + clip_child
6779 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6780 scoped_refptr<Layer> clip_parent = Layer::Create(layer_settings());
6781 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
6782 scoped_refptr<Layer> intervening = Layer::Create(layer_settings());
6783 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
6784 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6785 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
6787 root->AddChild(clip_parent);
6788 clip_parent->AddChild(render_surface1);
6789 render_surface1->AddChild(intervening);
6790 intervening->AddChild(render_surface2);
6791 render_surface2->AddChild(clip_child);
6793 clip_child->SetClipParent(clip_parent.get());
6795 intervening->SetMasksToBounds(true);
6796 clip_parent->SetMasksToBounds(true);
6797 intervening->SetScrollClipLayerId(clip_parent->id());
6798 intervening->SetScrollOffset(gfx::ScrollOffset(3, 3));
6800 render_surface1->SetForceRenderSurface(true);
6801 render_surface2->SetForceRenderSurface(true);
6803 gfx::Transform translation_transform;
6804 translation_transform.Translate(2, 2);
6806 gfx::Transform identity_transform;
6807 SetLayerPropertiesForTesting(root.get(),
6808 identity_transform,
6809 gfx::Point3F(),
6810 gfx::PointF(),
6811 gfx::Size(50, 50),
6812 true,
6813 false);
6814 SetLayerPropertiesForTesting(clip_parent.get(),
6815 translation_transform,
6816 gfx::Point3F(),
6817 gfx::PointF(1.f, 1.f),
6818 gfx::Size(40, 40),
6819 true,
6820 false);
6821 SetLayerPropertiesForTesting(render_surface1.get(),
6822 identity_transform,
6823 gfx::Point3F(),
6824 gfx::PointF(),
6825 gfx::Size(10, 10),
6826 true,
6827 false);
6828 SetLayerPropertiesForTesting(intervening.get(),
6829 identity_transform,
6830 gfx::Point3F(),
6831 gfx::PointF(1.f, 1.f),
6832 gfx::Size(5, 5),
6833 true,
6834 false);
6835 SetLayerPropertiesForTesting(render_surface2.get(),
6836 identity_transform,
6837 gfx::Point3F(),
6838 gfx::PointF(),
6839 gfx::Size(10, 10),
6840 true,
6841 false);
6842 SetLayerPropertiesForTesting(clip_child.get(),
6843 identity_transform,
6844 gfx::Point3F(),
6845 gfx::PointF(-10.f, -10.f),
6846 gfx::Size(60, 60),
6847 true,
6848 false);
6850 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6851 host->SetRootLayer(root);
6853 ExecuteCalculateDrawProperties(root.get());
6855 EXPECT_TRUE(root->render_surface());
6856 EXPECT_TRUE(render_surface1->render_surface());
6857 EXPECT_TRUE(render_surface2->render_surface());
6859 // Since the render surfaces could have expanded, they should not clip (their
6860 // bounds would no longer be reliable). We should resort to layer clipping
6861 // in this case.
6862 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6863 render_surface1->render_surface()->clip_rect().ToString());
6864 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
6865 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
6866 render_surface2->render_surface()->clip_rect().ToString());
6867 EXPECT_FALSE(render_surface2->render_surface()->is_clipped());
6869 // NB: clip rects are in target space.
6870 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6871 render_surface1->clip_rect().ToString());
6872 EXPECT_TRUE(render_surface1->is_clipped());
6874 // This value is inherited from the clipping ancestor layer, 'intervening'.
6875 EXPECT_EQ(gfx::Rect(2, 2, 3, 3).ToString(),
6876 render_surface2->clip_rect().ToString());
6877 EXPECT_TRUE(render_surface2->is_clipped());
6879 // The content rects of both render surfaces should both have expanded to
6880 // contain the clip child.
6881 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6882 render_surface1->render_surface()->content_rect().ToString());
6883 EXPECT_EQ(gfx::Rect(2, 2, 40, 40).ToString(),
6884 render_surface2->render_surface()->content_rect().ToString());
6886 // The clip child should have inherited the clip parent's clip (projected to
6887 // the right space, of course), and should have the correctly sized visible
6888 // content rect.
6889 EXPECT_EQ(gfx::Rect(2, 2, 40, 40).ToString(),
6890 clip_child->clip_rect().ToString());
6891 EXPECT_EQ(gfx::Rect(12, 12, 40, 40).ToString(),
6892 clip_child->visible_content_rect().ToString());
6893 EXPECT_TRUE(clip_child->is_clipped());
6896 TEST_F(LayerTreeHostCommonTest, DescendantsOfClipChildren) {
6897 // Ensures that descendants of the clip child inherit the correct clip.
6899 // root (a render surface)
6900 // + clip_parent (masks to bounds)
6901 // + intervening (masks to bounds)
6902 // + clip_child
6903 // + child
6905 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6906 scoped_refptr<Layer> clip_parent = Layer::Create(layer_settings());
6907 scoped_refptr<Layer> intervening = Layer::Create(layer_settings());
6908 scoped_refptr<Layer> clip_child = Layer::Create(layer_settings());
6909 scoped_refptr<LayerWithForcedDrawsContent> child =
6910 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
6912 root->AddChild(clip_parent);
6913 clip_parent->AddChild(intervening);
6914 intervening->AddChild(clip_child);
6915 clip_child->AddChild(child);
6917 clip_child->SetClipParent(clip_parent.get());
6919 intervening->SetMasksToBounds(true);
6920 clip_parent->SetMasksToBounds(true);
6922 gfx::Transform identity_transform;
6923 SetLayerPropertiesForTesting(root.get(),
6924 identity_transform,
6925 gfx::Point3F(),
6926 gfx::PointF(),
6927 gfx::Size(50, 50),
6928 true,
6929 false);
6930 SetLayerPropertiesForTesting(clip_parent.get(),
6931 identity_transform,
6932 gfx::Point3F(),
6933 gfx::PointF(),
6934 gfx::Size(40, 40),
6935 true,
6936 false);
6937 SetLayerPropertiesForTesting(intervening.get(),
6938 identity_transform,
6939 gfx::Point3F(),
6940 gfx::PointF(),
6941 gfx::Size(5, 5),
6942 true,
6943 false);
6944 SetLayerPropertiesForTesting(clip_child.get(),
6945 identity_transform,
6946 gfx::Point3F(),
6947 gfx::PointF(),
6948 gfx::Size(60, 60),
6949 true,
6950 false);
6951 SetLayerPropertiesForTesting(child.get(),
6952 identity_transform,
6953 gfx::Point3F(),
6954 gfx::PointF(),
6955 gfx::Size(60, 60),
6956 true,
6957 false);
6959 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
6960 host->SetRootLayer(root);
6962 ExecuteCalculateDrawProperties(root.get());
6964 EXPECT_TRUE(root->render_surface());
6966 // Neither the clip child nor its descendant should have inherited the clip
6967 // from |intervening|.
6968 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6969 clip_child->clip_rect().ToString());
6970 EXPECT_TRUE(clip_child->is_clipped());
6971 EXPECT_EQ(gfx::Rect(0, 0, 40, 40).ToString(),
6972 child->visible_content_rect().ToString());
6973 EXPECT_TRUE(child->is_clipped());
6976 TEST_F(LayerTreeHostCommonTest,
6977 SurfacesShouldBeUnaffectedByNonDescendantClipChildren) {
6978 // Ensures that non-descendant clip children in the tree do not affect
6979 // render surfaces.
6981 // root (a render surface)
6982 // + clip_parent (masks to bounds)
6983 // + render_surface1
6984 // + clip_child
6985 // + render_surface2
6986 // + non_clip_child
6988 // In this example render_surface2 should be unaffected by clip_child.
6989 scoped_refptr<Layer> root = Layer::Create(layer_settings());
6990 scoped_refptr<Layer> clip_parent = Layer::Create(layer_settings());
6991 scoped_refptr<Layer> render_surface1 = Layer::Create(layer_settings());
6992 scoped_refptr<LayerWithForcedDrawsContent> clip_child =
6993 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
6994 scoped_refptr<Layer> render_surface2 = Layer::Create(layer_settings());
6995 scoped_refptr<LayerWithForcedDrawsContent> non_clip_child =
6996 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
6998 root->AddChild(clip_parent);
6999 clip_parent->AddChild(render_surface1);
7000 render_surface1->AddChild(clip_child);
7001 clip_parent->AddChild(render_surface2);
7002 render_surface2->AddChild(non_clip_child);
7004 clip_child->SetClipParent(clip_parent.get());
7006 clip_parent->SetMasksToBounds(true);
7007 render_surface1->SetMasksToBounds(true);
7009 gfx::Transform identity_transform;
7010 SetLayerPropertiesForTesting(root.get(),
7011 identity_transform,
7012 gfx::Point3F(),
7013 gfx::PointF(),
7014 gfx::Size(15, 15),
7015 true,
7016 false);
7017 SetLayerPropertiesForTesting(clip_parent.get(),
7018 identity_transform,
7019 gfx::Point3F(),
7020 gfx::PointF(),
7021 gfx::Size(10, 10),
7022 true,
7023 false);
7024 SetLayerPropertiesForTesting(render_surface1.get(),
7025 identity_transform,
7026 gfx::Point3F(),
7027 gfx::PointF(5, 5),
7028 gfx::Size(5, 5),
7029 true,
7030 false);
7031 SetLayerPropertiesForTesting(render_surface2.get(),
7032 identity_transform,
7033 gfx::Point3F(),
7034 gfx::PointF(),
7035 gfx::Size(5, 5),
7036 true,
7037 false);
7038 SetLayerPropertiesForTesting(clip_child.get(),
7039 identity_transform,
7040 gfx::Point3F(),
7041 gfx::PointF(-1, 1),
7042 gfx::Size(10, 10),
7043 true,
7044 false);
7045 SetLayerPropertiesForTesting(non_clip_child.get(),
7046 identity_transform,
7047 gfx::Point3F(),
7048 gfx::PointF(),
7049 gfx::Size(5, 5),
7050 true,
7051 false);
7053 render_surface1->SetForceRenderSurface(true);
7054 render_surface2->SetForceRenderSurface(true);
7056 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7057 host->SetRootLayer(root);
7059 ExecuteCalculateDrawProperties(root.get());
7061 EXPECT_TRUE(root->render_surface());
7062 EXPECT_TRUE(render_surface1->render_surface());
7063 EXPECT_TRUE(render_surface2->render_surface());
7065 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7066 render_surface1->clip_rect().ToString());
7067 EXPECT_TRUE(render_surface1->is_clipped());
7069 // The render surface should not clip (it has unclipped descendants), instead
7070 // it should rely on layer clipping.
7071 EXPECT_EQ(gfx::Rect(0, 0, 0, 0).ToString(),
7072 render_surface1->render_surface()->clip_rect().ToString());
7073 EXPECT_FALSE(render_surface1->render_surface()->is_clipped());
7075 // That said, it should have grown to accomodate the unclipped descendant.
7076 EXPECT_EQ(gfx::Rect(-1, 1, 6, 4).ToString(),
7077 render_surface1->render_surface()->content_rect().ToString());
7079 // This render surface should clip. It has no unclipped descendants.
7080 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7081 render_surface2->clip_rect().ToString());
7082 EXPECT_TRUE(render_surface2->render_surface()->is_clipped());
7084 // It also shouldn't have grown to accomodate the clip child.
7085 EXPECT_EQ(gfx::Rect(0, 0, 5, 5).ToString(),
7086 render_surface2->render_surface()->content_rect().ToString());
7088 // Sanity check our num_unclipped_descendants values.
7089 EXPECT_EQ(1, render_surface1->num_unclipped_descendants());
7090 EXPECT_EQ(0, render_surface2->num_unclipped_descendants());
7093 TEST_F(LayerTreeHostCommonTest, CanRenderToSeparateSurface) {
7094 FakeImplProxy proxy;
7095 TestSharedBitmapManager shared_bitmap_manager;
7096 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7097 scoped_ptr<LayerImpl> root =
7098 LayerImpl::Create(host_impl.active_tree(), 12345);
7099 scoped_ptr<LayerImpl> child1 =
7100 LayerImpl::Create(host_impl.active_tree(), 123456);
7101 scoped_ptr<LayerImpl> child2 =
7102 LayerImpl::Create(host_impl.active_tree(), 1234567);
7103 scoped_ptr<LayerImpl> child3 =
7104 LayerImpl::Create(host_impl.active_tree(), 12345678);
7106 gfx::Transform identity_matrix;
7107 gfx::Point3F transform_origin;
7108 gfx::PointF position;
7109 gfx::Size bounds(100, 100);
7110 SetLayerPropertiesForTesting(root.get(), identity_matrix, transform_origin,
7111 position, bounds, true, false, true);
7112 root->SetDrawsContent(true);
7114 // This layer structure normally forces render surface due to preserves3d
7115 // behavior.
7116 SetLayerPropertiesForTesting(child1.get(), identity_matrix, transform_origin,
7117 position, bounds, false, true, true);
7118 child1->SetDrawsContent(true);
7119 SetLayerPropertiesForTesting(child2.get(), identity_matrix, transform_origin,
7120 position, bounds, true, false, false);
7121 child2->SetDrawsContent(true);
7122 SetLayerPropertiesForTesting(child3.get(), identity_matrix, transform_origin,
7123 position, bounds, true, false, false);
7124 child3->SetDrawsContent(true);
7126 child2->Set3dSortingContextId(1);
7127 child3->Set3dSortingContextId(1);
7129 child2->AddChild(child3.Pass());
7130 child1->AddChild(child2.Pass());
7131 root->AddChild(child1.Pass());
7134 LayerImplList render_surface_layer_list;
7135 FakeLayerTreeHostImpl::RecursiveUpdateNumChildren(root.get());
7136 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7137 root.get(), root->bounds(), &render_surface_layer_list);
7138 inputs.can_render_to_separate_surface = true;
7139 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7141 EXPECT_EQ(2u, render_surface_layer_list.size());
7143 int count_represents_target_render_surface = 0;
7144 int count_represents_contributing_render_surface = 0;
7145 int count_represents_itself = 0;
7146 auto end = LayerIterator<LayerImpl>::End(&render_surface_layer_list);
7147 for (auto it = LayerIterator<LayerImpl>::Begin(&render_surface_layer_list);
7148 it != end; ++it) {
7149 if (it.represents_target_render_surface())
7150 count_represents_target_render_surface++;
7151 if (it.represents_contributing_render_surface())
7152 count_represents_contributing_render_surface++;
7153 if (it.represents_itself())
7154 count_represents_itself++;
7157 // Two render surfaces.
7158 EXPECT_EQ(2, count_represents_target_render_surface);
7159 // Second render surface contributes to root render surface.
7160 EXPECT_EQ(1, count_represents_contributing_render_surface);
7161 // All 4 layers represent itself.
7162 EXPECT_EQ(4, count_represents_itself);
7166 LayerImplList render_surface_layer_list;
7167 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7168 root.get(), root->bounds(), &render_surface_layer_list);
7169 inputs.can_render_to_separate_surface = false;
7170 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7172 EXPECT_EQ(1u, render_surface_layer_list.size());
7174 int count_represents_target_render_surface = 0;
7175 int count_represents_contributing_render_surface = 0;
7176 int count_represents_itself = 0;
7177 auto end = LayerIterator<LayerImpl>::End(&render_surface_layer_list);
7178 for (auto it = LayerIterator<LayerImpl>::Begin(&render_surface_layer_list);
7179 it != end; ++it) {
7180 if (it.represents_target_render_surface())
7181 count_represents_target_render_surface++;
7182 if (it.represents_contributing_render_surface())
7183 count_represents_contributing_render_surface++;
7184 if (it.represents_itself())
7185 count_represents_itself++;
7188 // Only root layer has a render surface.
7189 EXPECT_EQ(1, count_represents_target_render_surface);
7190 // No layer contributes a render surface to root render surface.
7191 EXPECT_EQ(0, count_represents_contributing_render_surface);
7192 // All 4 layers represent itself.
7193 EXPECT_EQ(4, count_represents_itself);
7197 TEST_F(LayerTreeHostCommonTest, DoNotIncludeBackfaceInvisibleSurfaces) {
7198 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7199 scoped_refptr<Layer> render_surface = Layer::Create(layer_settings());
7200 scoped_refptr<LayerWithForcedDrawsContent> child =
7201 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7203 root->AddChild(render_surface);
7204 render_surface->AddChild(child);
7206 gfx::Transform identity_transform;
7207 SetLayerPropertiesForTesting(root.get(),
7208 identity_transform,
7209 gfx::Point3F(),
7210 gfx::PointF(),
7211 gfx::Size(50, 50),
7212 true,
7213 false);
7214 SetLayerPropertiesForTesting(render_surface.get(),
7215 identity_transform,
7216 gfx::Point3F(),
7217 gfx::PointF(),
7218 gfx::Size(30, 30),
7219 false,
7220 true);
7221 SetLayerPropertiesForTesting(child.get(),
7222 identity_transform,
7223 gfx::Point3F(),
7224 gfx::PointF(),
7225 gfx::Size(20, 20),
7226 true,
7227 false);
7229 root->SetShouldFlattenTransform(false);
7230 root->Set3dSortingContextId(1);
7231 render_surface->SetDoubleSided(false);
7232 render_surface->SetForceRenderSurface(true);
7234 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7235 host->SetRootLayer(root);
7237 ExecuteCalculateDrawProperties(root.get());
7239 EXPECT_EQ(2u, render_surface_layer_list()->size());
7240 EXPECT_EQ(1u,
7241 render_surface_layer_list()->at(0)
7242 ->render_surface()->layer_list().size());
7243 EXPECT_EQ(1u,
7244 render_surface_layer_list()->at(1)
7245 ->render_surface()->layer_list().size());
7247 gfx::Transform rotation_transform = identity_transform;
7248 rotation_transform.RotateAboutXAxis(180.0);
7250 render_surface->SetTransform(rotation_transform);
7252 ExecuteCalculateDrawProperties(root.get());
7254 EXPECT_EQ(1u, render_surface_layer_list()->size());
7255 EXPECT_EQ(0u,
7256 render_surface_layer_list()->at(0)
7257 ->render_surface()->layer_list().size());
7260 TEST_F(LayerTreeHostCommonTest, ClippedByScrollParent) {
7261 // Checks that the simple case (being clipped by a scroll parent that would
7262 // have been processed before you anyhow) results in the right clips.
7264 // + root
7265 // + scroll_parent_border
7266 // | + scroll_parent_clip
7267 // | + scroll_parent
7268 // + scroll_child
7270 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7271 scoped_refptr<Layer> scroll_parent_border = Layer::Create(layer_settings());
7272 scoped_refptr<Layer> scroll_parent_clip = Layer::Create(layer_settings());
7273 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7274 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7275 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7276 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7278 root->AddChild(scroll_child);
7280 root->AddChild(scroll_parent_border);
7281 scroll_parent_border->AddChild(scroll_parent_clip);
7282 scroll_parent_clip->AddChild(scroll_parent);
7284 scroll_parent_clip->SetMasksToBounds(true);
7286 scroll_child->SetScrollParent(scroll_parent.get());
7288 gfx::Transform identity_transform;
7289 SetLayerPropertiesForTesting(root.get(),
7290 identity_transform,
7291 gfx::Point3F(),
7292 gfx::PointF(),
7293 gfx::Size(50, 50),
7294 true,
7295 false);
7296 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7297 identity_transform,
7298 gfx::Point3F(),
7299 gfx::PointF(),
7300 gfx::Size(40, 40),
7301 true,
7302 false);
7303 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7304 identity_transform,
7305 gfx::Point3F(),
7306 gfx::PointF(),
7307 gfx::Size(30, 30),
7308 true,
7309 false);
7310 SetLayerPropertiesForTesting(scroll_parent.get(),
7311 identity_transform,
7312 gfx::Point3F(),
7313 gfx::PointF(),
7314 gfx::Size(50, 50),
7315 true,
7316 false);
7317 SetLayerPropertiesForTesting(scroll_child.get(),
7318 identity_transform,
7319 gfx::Point3F(),
7320 gfx::PointF(),
7321 gfx::Size(50, 50),
7322 true,
7323 false);
7325 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7326 host->SetRootLayer(root);
7328 ExecuteCalculateDrawProperties(root.get());
7330 EXPECT_TRUE(root->render_surface());
7332 EXPECT_EQ(gfx::Rect(0, 0, 30, 30).ToString(),
7333 scroll_child->clip_rect().ToString());
7334 EXPECT_TRUE(scroll_child->is_clipped());
7337 TEST_F(LayerTreeHostCommonTest, SingularTransformSubtreesDoNotDraw) {
7338 scoped_refptr<LayerWithForcedDrawsContent> root =
7339 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7340 scoped_refptr<LayerWithForcedDrawsContent> parent =
7341 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7342 scoped_refptr<LayerWithForcedDrawsContent> child =
7343 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7345 root->AddChild(parent);
7346 parent->AddChild(child);
7348 gfx::Transform identity_transform;
7349 SetLayerPropertiesForTesting(root.get(),
7350 identity_transform,
7351 gfx::Point3F(),
7352 gfx::PointF(),
7353 gfx::Size(50, 50),
7354 true,
7355 true);
7356 root->SetForceRenderSurface(true);
7357 SetLayerPropertiesForTesting(parent.get(),
7358 identity_transform,
7359 gfx::Point3F(),
7360 gfx::PointF(),
7361 gfx::Size(30, 30),
7362 true,
7363 true);
7364 parent->SetForceRenderSurface(true);
7365 SetLayerPropertiesForTesting(child.get(),
7366 identity_transform,
7367 gfx::Point3F(),
7368 gfx::PointF(),
7369 gfx::Size(20, 20),
7370 true,
7371 true);
7372 child->SetForceRenderSurface(true);
7374 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7375 host->SetRootLayer(root);
7377 ExecuteCalculateDrawProperties(root.get());
7379 EXPECT_EQ(3u, render_surface_layer_list()->size());
7381 gfx::Transform singular_transform;
7382 singular_transform.Scale3d(
7383 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
7385 child->SetTransform(singular_transform);
7387 ExecuteCalculateDrawProperties(root.get());
7389 EXPECT_EQ(2u, render_surface_layer_list()->size());
7391 // Ensure that the entire subtree under a layer with singular transform does
7392 // not get rendered.
7393 parent->SetTransform(singular_transform);
7394 child->SetTransform(identity_transform);
7396 ExecuteCalculateDrawProperties(root.get());
7398 EXPECT_EQ(1u, render_surface_layer_list()->size());
7401 TEST_F(LayerTreeHostCommonTest, ClippedByOutOfOrderScrollParent) {
7402 // Checks that clipping by a scroll parent that follows you in paint order
7403 // still results in correct clipping.
7405 // + root
7406 // + scroll_child
7407 // + scroll_parent_border
7408 // + scroll_parent_clip
7409 // + scroll_parent
7411 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7412 scoped_refptr<Layer> scroll_parent_border = Layer::Create(layer_settings());
7413 scoped_refptr<Layer> scroll_parent_clip = Layer::Create(layer_settings());
7414 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7415 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7416 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7417 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7419 root->AddChild(scroll_parent_border);
7420 scroll_parent_border->AddChild(scroll_parent_clip);
7421 scroll_parent_clip->AddChild(scroll_parent);
7423 root->AddChild(scroll_child);
7425 scroll_parent_clip->SetMasksToBounds(true);
7427 scroll_child->SetScrollParent(scroll_parent.get());
7429 gfx::Transform identity_transform;
7430 SetLayerPropertiesForTesting(root.get(),
7431 identity_transform,
7432 gfx::Point3F(),
7433 gfx::PointF(),
7434 gfx::Size(50, 50),
7435 true,
7436 false);
7437 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7438 identity_transform,
7439 gfx::Point3F(),
7440 gfx::PointF(),
7441 gfx::Size(40, 40),
7442 true,
7443 false);
7444 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7445 identity_transform,
7446 gfx::Point3F(),
7447 gfx::PointF(),
7448 gfx::Size(30, 30),
7449 true,
7450 false);
7451 SetLayerPropertiesForTesting(scroll_parent.get(),
7452 identity_transform,
7453 gfx::Point3F(),
7454 gfx::PointF(),
7455 gfx::Size(50, 50),
7456 true,
7457 false);
7458 SetLayerPropertiesForTesting(scroll_child.get(),
7459 identity_transform,
7460 gfx::Point3F(),
7461 gfx::PointF(),
7462 gfx::Size(50, 50),
7463 true,
7464 false);
7466 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7467 host->SetRootLayer(root);
7469 ExecuteCalculateDrawProperties(root.get());
7471 EXPECT_TRUE(root->render_surface());
7473 EXPECT_EQ(gfx::Rect(0, 0, 30, 30).ToString(),
7474 scroll_child->clip_rect().ToString());
7475 EXPECT_TRUE(scroll_child->is_clipped());
7478 TEST_F(LayerTreeHostCommonTest, ClippedByOutOfOrderScrollGrandparent) {
7479 // Checks that clipping by a scroll parent and scroll grandparent that follow
7480 // you in paint order still results in correct clipping.
7482 // + root
7483 // + scroll_child
7484 // + scroll_parent_border
7485 // | + scroll_parent_clip
7486 // | + scroll_parent
7487 // + scroll_grandparent_border
7488 // + scroll_grandparent_clip
7489 // + scroll_grandparent
7491 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7492 scoped_refptr<Layer> scroll_parent_border = Layer::Create(layer_settings());
7493 scoped_refptr<Layer> scroll_parent_clip = Layer::Create(layer_settings());
7494 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7495 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7497 scoped_refptr<Layer> scroll_grandparent_border =
7498 Layer::Create(layer_settings());
7499 scoped_refptr<Layer> scroll_grandparent_clip =
7500 Layer::Create(layer_settings());
7501 scoped_refptr<LayerWithForcedDrawsContent> scroll_grandparent =
7502 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7504 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7505 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7507 root->AddChild(scroll_child);
7509 root->AddChild(scroll_parent_border);
7510 scroll_parent_border->AddChild(scroll_parent_clip);
7511 scroll_parent_clip->AddChild(scroll_parent);
7513 root->AddChild(scroll_grandparent_border);
7514 scroll_grandparent_border->AddChild(scroll_grandparent_clip);
7515 scroll_grandparent_clip->AddChild(scroll_grandparent);
7517 scroll_parent_clip->SetMasksToBounds(true);
7518 scroll_grandparent_clip->SetMasksToBounds(true);
7520 scroll_child->SetScrollParent(scroll_parent.get());
7521 scroll_parent_border->SetScrollParent(scroll_grandparent.get());
7523 gfx::Transform identity_transform;
7524 SetLayerPropertiesForTesting(root.get(),
7525 identity_transform,
7526 gfx::Point3F(),
7527 gfx::PointF(),
7528 gfx::Size(50, 50),
7529 true,
7530 false);
7531 SetLayerPropertiesForTesting(scroll_grandparent_border.get(),
7532 identity_transform,
7533 gfx::Point3F(),
7534 gfx::PointF(),
7535 gfx::Size(40, 40),
7536 true,
7537 false);
7538 SetLayerPropertiesForTesting(scroll_grandparent_clip.get(),
7539 identity_transform,
7540 gfx::Point3F(),
7541 gfx::PointF(),
7542 gfx::Size(20, 20),
7543 true,
7544 false);
7545 SetLayerPropertiesForTesting(scroll_grandparent.get(),
7546 identity_transform,
7547 gfx::Point3F(),
7548 gfx::PointF(),
7549 gfx::Size(50, 50),
7550 true,
7551 false);
7552 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7553 identity_transform,
7554 gfx::Point3F(),
7555 gfx::PointF(),
7556 gfx::Size(40, 40),
7557 true,
7558 false);
7559 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7560 identity_transform,
7561 gfx::Point3F(),
7562 gfx::PointF(),
7563 gfx::Size(30, 30),
7564 true,
7565 false);
7566 SetLayerPropertiesForTesting(scroll_parent.get(),
7567 identity_transform,
7568 gfx::Point3F(),
7569 gfx::PointF(),
7570 gfx::Size(50, 50),
7571 true,
7572 false);
7573 SetLayerPropertiesForTesting(scroll_child.get(),
7574 identity_transform,
7575 gfx::Point3F(),
7576 gfx::PointF(),
7577 gfx::Size(50, 50),
7578 true,
7579 false);
7581 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7582 host->SetRootLayer(root);
7584 ExecuteCalculateDrawProperties(root.get());
7586 EXPECT_TRUE(root->render_surface());
7588 EXPECT_EQ(gfx::Rect(0, 0, 20, 20).ToString(),
7589 scroll_child->clip_rect().ToString());
7590 EXPECT_TRUE(scroll_child->is_clipped());
7592 // Despite the fact that we visited the above layers out of order to get the
7593 // correct clip, the layer lists should be unaffected.
7594 EXPECT_EQ(3u, root->render_surface()->layer_list().size());
7595 EXPECT_EQ(scroll_child.get(),
7596 root->render_surface()->layer_list().at(0).get());
7597 EXPECT_EQ(scroll_parent.get(),
7598 root->render_surface()->layer_list().at(1).get());
7599 EXPECT_EQ(scroll_grandparent.get(),
7600 root->render_surface()->layer_list().at(2).get());
7603 TEST_F(LayerTreeHostCommonTest, OutOfOrderClippingRequiresRSLLSorting) {
7604 // Ensures that even if we visit layers out of order, we still produce a
7605 // correctly ordered render surface layer list.
7606 // + root
7607 // + scroll_child
7608 // + scroll_parent_border
7609 // + scroll_parent_clip
7610 // + scroll_parent
7611 // + render_surface1
7612 // + scroll_grandparent_border
7613 // + scroll_grandparent_clip
7614 // + scroll_grandparent
7615 // + render_surface2
7617 scoped_refptr<LayerWithForcedDrawsContent> root =
7618 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7620 scoped_refptr<Layer> scroll_parent_border = Layer::Create(layer_settings());
7621 scoped_refptr<Layer> scroll_parent_clip = Layer::Create(layer_settings());
7622 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
7623 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7624 scoped_refptr<LayerWithForcedDrawsContent> render_surface1 =
7625 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7627 scoped_refptr<Layer> scroll_grandparent_border =
7628 Layer::Create(layer_settings());
7629 scoped_refptr<Layer> scroll_grandparent_clip =
7630 Layer::Create(layer_settings());
7631 scoped_refptr<LayerWithForcedDrawsContent> scroll_grandparent =
7632 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7633 scoped_refptr<LayerWithForcedDrawsContent> render_surface2 =
7634 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7636 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
7637 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7639 root->AddChild(scroll_child);
7641 root->AddChild(scroll_parent_border);
7642 scroll_parent_border->AddChild(scroll_parent_clip);
7643 scroll_parent_clip->AddChild(scroll_parent);
7644 scroll_parent->AddChild(render_surface2);
7646 root->AddChild(scroll_grandparent_border);
7647 scroll_grandparent_border->AddChild(scroll_grandparent_clip);
7648 scroll_grandparent_clip->AddChild(scroll_grandparent);
7649 scroll_grandparent->AddChild(render_surface1);
7651 scroll_parent_clip->SetMasksToBounds(true);
7652 scroll_grandparent_clip->SetMasksToBounds(true);
7654 scroll_child->SetScrollParent(scroll_parent.get());
7655 scroll_parent_border->SetScrollParent(scroll_grandparent.get());
7657 render_surface1->SetForceRenderSurface(true);
7658 render_surface2->SetForceRenderSurface(true);
7660 gfx::Transform identity_transform;
7661 SetLayerPropertiesForTesting(root.get(),
7662 identity_transform,
7663 gfx::Point3F(),
7664 gfx::PointF(),
7665 gfx::Size(50, 50),
7666 true,
7667 false);
7668 SetLayerPropertiesForTesting(scroll_grandparent_border.get(),
7669 identity_transform,
7670 gfx::Point3F(),
7671 gfx::PointF(),
7672 gfx::Size(40, 40),
7673 true,
7674 false);
7675 SetLayerPropertiesForTesting(scroll_grandparent_clip.get(),
7676 identity_transform,
7677 gfx::Point3F(),
7678 gfx::PointF(),
7679 gfx::Size(20, 20),
7680 true,
7681 false);
7682 SetLayerPropertiesForTesting(scroll_grandparent.get(),
7683 identity_transform,
7684 gfx::Point3F(),
7685 gfx::PointF(),
7686 gfx::Size(50, 50),
7687 true,
7688 false);
7689 SetLayerPropertiesForTesting(render_surface1.get(),
7690 identity_transform,
7691 gfx::Point3F(),
7692 gfx::PointF(),
7693 gfx::Size(50, 50),
7694 true,
7695 false);
7696 SetLayerPropertiesForTesting(scroll_parent_border.get(),
7697 identity_transform,
7698 gfx::Point3F(),
7699 gfx::PointF(),
7700 gfx::Size(40, 40),
7701 true,
7702 false);
7703 SetLayerPropertiesForTesting(scroll_parent_clip.get(),
7704 identity_transform,
7705 gfx::Point3F(),
7706 gfx::PointF(),
7707 gfx::Size(30, 30),
7708 true,
7709 false);
7710 SetLayerPropertiesForTesting(scroll_parent.get(),
7711 identity_transform,
7712 gfx::Point3F(),
7713 gfx::PointF(),
7714 gfx::Size(50, 50),
7715 true,
7716 false);
7717 SetLayerPropertiesForTesting(render_surface2.get(),
7718 identity_transform,
7719 gfx::Point3F(),
7720 gfx::PointF(),
7721 gfx::Size(50, 50),
7722 true,
7723 false);
7724 SetLayerPropertiesForTesting(scroll_child.get(),
7725 identity_transform,
7726 gfx::Point3F(),
7727 gfx::PointF(),
7728 gfx::Size(50, 50),
7729 true,
7730 false);
7732 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7733 host->SetRootLayer(root);
7735 RenderSurfaceLayerList render_surface_layer_list;
7736 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
7737 root.get(),
7738 root->bounds(),
7739 identity_transform,
7740 &render_surface_layer_list);
7742 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7744 EXPECT_TRUE(root->render_surface());
7746 EXPECT_EQ(gfx::Rect(0, 0, 20, 20).ToString(),
7747 scroll_child->clip_rect().ToString());
7748 EXPECT_TRUE(scroll_child->is_clipped());
7750 // Despite the fact that we had to process the layers out of order to get the
7751 // right clip, our render_surface_layer_list's order should be unaffected.
7752 EXPECT_EQ(3u, render_surface_layer_list.size());
7753 EXPECT_EQ(root.get(), render_surface_layer_list.at(0));
7754 EXPECT_EQ(render_surface2.get(), render_surface_layer_list.at(1));
7755 EXPECT_EQ(render_surface1.get(), render_surface_layer_list.at(2));
7756 EXPECT_TRUE(render_surface_layer_list.at(0)->render_surface());
7757 EXPECT_TRUE(render_surface_layer_list.at(1)->render_surface());
7758 EXPECT_TRUE(render_surface_layer_list.at(2)->render_surface());
7761 TEST_F(LayerTreeHostCommonTest, FixedPositionWithInterveningRenderSurface) {
7762 // Ensures that when we have a render surface between a fixed position layer
7763 // and its container, we compute the fixed position layer's draw transform
7764 // with respect to that intervening render surface, not with respect to its
7765 // container's render target.
7767 // + root
7768 // + render_surface
7769 // + fixed
7770 // + child
7772 scoped_refptr<Layer> root = Layer::Create(layer_settings());
7773 scoped_refptr<LayerWithForcedDrawsContent> render_surface =
7774 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7775 scoped_refptr<LayerWithForcedDrawsContent> fixed =
7776 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7777 scoped_refptr<LayerWithForcedDrawsContent> child =
7778 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
7780 root->AddChild(render_surface);
7781 render_surface->AddChild(fixed);
7782 fixed->AddChild(child);
7784 root->SetIsContainerForFixedPositionLayers(true);
7785 render_surface->SetForceRenderSurface(true);
7787 LayerPositionConstraint constraint;
7788 constraint.set_is_fixed_position(true);
7789 fixed->SetPositionConstraint(constraint);
7791 SetLayerPropertiesForTesting(root.get(), gfx::Transform(), gfx::Point3F(),
7792 gfx::PointF(), gfx::Size(50, 50), true, false);
7793 SetLayerPropertiesForTesting(render_surface.get(), gfx::Transform(),
7794 gfx::Point3F(), gfx::PointF(7.f, 9.f),
7795 gfx::Size(50, 50), true, false);
7796 SetLayerPropertiesForTesting(fixed.get(), gfx::Transform(), gfx::Point3F(),
7797 gfx::PointF(10.f, 15.f), gfx::Size(50, 50), true,
7798 false);
7799 SetLayerPropertiesForTesting(child.get(), gfx::Transform(), gfx::Point3F(),
7800 gfx::PointF(1.f, 2.f), gfx::Size(50, 50), true,
7801 false);
7803 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
7804 host->SetRootLayer(root);
7806 ExecuteCalculateDrawProperties(root.get());
7808 gfx::Transform expected_fixed_draw_transform;
7809 expected_fixed_draw_transform.Translate(10.f, 15.f);
7810 EXPECT_EQ(expected_fixed_draw_transform, fixed->draw_transform());
7812 gfx::Transform expected_fixed_screen_space_transform;
7813 expected_fixed_screen_space_transform.Translate(17.f, 24.f);
7814 EXPECT_EQ(expected_fixed_screen_space_transform,
7815 fixed->screen_space_transform());
7817 gfx::Transform expected_child_draw_transform;
7818 expected_child_draw_transform.Translate(11.f, 17.f);
7819 EXPECT_EQ(expected_child_draw_transform, child->draw_transform());
7821 gfx::Transform expected_child_screen_space_transform;
7822 expected_child_screen_space_transform.Translate(18.f, 26.f);
7823 EXPECT_EQ(expected_child_screen_space_transform,
7824 child->screen_space_transform());
7827 TEST_F(LayerTreeHostCommonTest, ScrollCompensationWithRounding) {
7828 // This test verifies that a scrolling layer that gets snapped to
7829 // integer coordinates doesn't move a fixed position child.
7831 // + root
7832 // + container
7833 // + scroller
7834 // + fixed
7836 FakeImplProxy proxy;
7837 TestSharedBitmapManager shared_bitmap_manager;
7838 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7839 host_impl.CreatePendingTree();
7840 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
7841 scoped_ptr<LayerImpl> container =
7842 LayerImpl::Create(host_impl.active_tree(), 2);
7843 LayerImpl* container_layer = container.get();
7844 scoped_ptr<LayerImpl> scroller =
7845 LayerImpl::Create(host_impl.active_tree(), 3);
7846 LayerImpl* scroll_layer = scroller.get();
7847 scoped_ptr<LayerImpl> fixed = LayerImpl::Create(host_impl.active_tree(), 4);
7848 LayerImpl* fixed_layer = fixed.get();
7850 container->SetIsContainerForFixedPositionLayers(true);
7852 LayerPositionConstraint constraint;
7853 constraint.set_is_fixed_position(true);
7854 fixed->SetPositionConstraint(constraint);
7856 scroller->SetScrollClipLayer(container->id());
7858 gfx::Transform identity_transform;
7859 gfx::Transform container_transform;
7860 container_transform.Translate3d(10.0, 20.0, 0.0);
7861 gfx::Vector2dF container_offset = container_transform.To2dTranslation();
7863 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
7864 gfx::PointF(), gfx::Size(50, 50), true, false,
7865 true);
7866 SetLayerPropertiesForTesting(container.get(), container_transform,
7867 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
7868 true, false, false);
7869 SetLayerPropertiesForTesting(scroller.get(), identity_transform,
7870 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
7871 true, false, false);
7872 SetLayerPropertiesForTesting(fixed.get(), identity_transform, gfx::Point3F(),
7873 gfx::PointF(), gfx::Size(50, 50), true, false,
7874 false);
7876 scroller->AddChild(fixed.Pass());
7877 container->AddChild(scroller.Pass());
7878 root->AddChild(container.Pass());
7880 // Rounded to integers already.
7882 gfx::Vector2dF scroll_delta(3.0, 5.0);
7883 scroll_layer->SetScrollDelta(scroll_delta);
7885 LayerImplList render_surface_layer_list;
7886 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7887 root.get(), root->bounds(), &render_surface_layer_list);
7888 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7890 EXPECT_TRANSFORMATION_MATRIX_EQ(
7891 container_layer->draw_properties().screen_space_transform,
7892 fixed_layer->draw_properties().screen_space_transform);
7893 EXPECT_VECTOR_EQ(
7894 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7895 container_offset);
7896 EXPECT_VECTOR_EQ(scroll_layer->draw_properties()
7897 .screen_space_transform.To2dTranslation(),
7898 container_offset - scroll_delta);
7901 // Scroll delta requiring rounding.
7903 gfx::Vector2dF scroll_delta(4.1f, 8.1f);
7904 scroll_layer->SetScrollDelta(scroll_delta);
7906 gfx::Vector2dF rounded_scroll_delta(4.f, 8.f);
7908 LayerImplList render_surface_layer_list;
7909 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7910 root.get(), root->bounds(), &render_surface_layer_list);
7911 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7913 EXPECT_TRANSFORMATION_MATRIX_EQ(
7914 container_layer->draw_properties().screen_space_transform,
7915 fixed_layer->draw_properties().screen_space_transform);
7916 EXPECT_VECTOR_EQ(
7917 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7918 container_offset);
7919 EXPECT_VECTOR_EQ(scroll_layer->draw_properties()
7920 .screen_space_transform.To2dTranslation(),
7921 container_offset - rounded_scroll_delta);
7924 // Scale is applied earlier in the tree.
7926 gfx::Transform scaled_container_transform = container_transform;
7927 scaled_container_transform.Scale3d(3.0, 3.0, 1.0);
7928 container_layer->SetTransform(scaled_container_transform);
7930 gfx::Vector2dF scroll_delta(4.5f, 8.5f);
7931 scroll_layer->SetScrollDelta(scroll_delta);
7933 LayerImplList render_surface_layer_list;
7934 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7935 root.get(), root->bounds(), &render_surface_layer_list);
7936 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7938 EXPECT_TRANSFORMATION_MATRIX_EQ(
7939 container_layer->draw_properties().screen_space_transform,
7940 fixed_layer->draw_properties().screen_space_transform);
7941 EXPECT_VECTOR_EQ(
7942 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7943 container_offset);
7945 container_layer->SetTransform(container_transform);
7948 // Scale is applied on the scroll layer itself.
7950 gfx::Transform scale_transform;
7951 scale_transform.Scale3d(3.0, 3.0, 1.0);
7952 scroll_layer->SetTransform(scale_transform);
7954 gfx::Vector2dF scroll_delta(4.5f, 8.5f);
7955 scroll_layer->SetScrollDelta(scroll_delta);
7957 LayerImplList render_surface_layer_list;
7958 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
7959 root.get(), root->bounds(), &render_surface_layer_list);
7960 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
7962 EXPECT_VECTOR_EQ(
7963 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
7964 container_offset);
7966 scroll_layer->SetTransform(identity_transform);
7970 TEST_F(LayerTreeHostCommonTest,
7971 ScrollCompensationMainScrollOffsetFractionalPart) {
7972 // This test verifies that a scrolling layer that has fractional scroll offset
7973 // from main doesn't move a fixed position child.
7975 // + root
7976 // + container
7977 // + scroller
7978 // + fixed
7980 FakeImplProxy proxy;
7981 TestSharedBitmapManager shared_bitmap_manager;
7982 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
7983 host_impl.CreatePendingTree();
7984 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
7985 scoped_ptr<LayerImpl> container =
7986 LayerImpl::Create(host_impl.active_tree(), 2);
7987 LayerImpl* container_layer = container.get();
7988 scoped_ptr<LayerImpl> scroller =
7989 LayerImpl::Create(host_impl.active_tree(), 3);
7990 LayerImpl* scroll_layer = scroller.get();
7991 scoped_ptr<LayerImpl> fixed = LayerImpl::Create(host_impl.active_tree(), 4);
7992 LayerImpl* fixed_layer = fixed.get();
7994 container->SetIsContainerForFixedPositionLayers(true);
7996 LayerPositionConstraint constraint;
7997 constraint.set_is_fixed_position(true);
7998 fixed->SetPositionConstraint(constraint);
8000 scroller->SetScrollClipLayer(container->id());
8002 gfx::Transform identity_transform;
8003 gfx::Transform container_transform;
8004 container_transform.Translate3d(10.0, 20.0, 0.0);
8005 gfx::Vector2dF container_offset = container_transform.To2dTranslation();
8007 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
8008 gfx::PointF(), gfx::Size(50, 50), true, false,
8009 true);
8010 SetLayerPropertiesForTesting(container.get(), container_transform,
8011 gfx::Point3F(), gfx::PointF(), gfx::Size(40, 40),
8012 true, false, false);
8013 SetLayerPropertiesForTesting(scroller.get(), identity_transform,
8014 gfx::Point3F(), gfx::PointF(0.0, 0.0),
8015 gfx::Size(30, 30), true, false, false);
8017 gfx::ScrollOffset scroll_offset(3.3, 4.2);
8018 gfx::Vector2dF main_scroll_fractional_part(0.3f, 0.2f);
8019 gfx::Vector2dF scroll_delta(0.1f, 0.4f);
8020 // Blink only uses the integer part of the scroll_offset for fixed
8021 // position layer.
8022 SetLayerPropertiesForTesting(fixed.get(), identity_transform, gfx::Point3F(),
8023 gfx::PointF(3.0f, 4.0f), gfx::Size(50, 50), true,
8024 false, false);
8025 scroll_layer->PushScrollOffsetFromMainThread(scroll_offset);
8026 scroll_layer->SetScrollDelta(scroll_delta);
8027 scroll_layer->SetScrollCompensationAdjustment(main_scroll_fractional_part);
8029 scroller->AddChild(fixed.Pass());
8030 container->AddChild(scroller.Pass());
8031 root->AddChild(container.Pass());
8033 LayerImplList render_surface_layer_list;
8034 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
8035 root.get(), root->bounds(), &render_surface_layer_list);
8036 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8038 EXPECT_TRANSFORMATION_MATRIX_EQ(
8039 container_layer->draw_properties().screen_space_transform,
8040 fixed_layer->draw_properties().screen_space_transform);
8041 EXPECT_VECTOR_EQ(
8042 fixed_layer->draw_properties().screen_space_transform.To2dTranslation(),
8043 container_offset);
8045 gfx::ScrollOffset effective_scroll_offset =
8046 ScrollOffsetWithDelta(scroll_offset, scroll_delta);
8047 gfx::Vector2d rounded_effective_scroll_offset =
8048 ToRoundedVector2d(ScrollOffsetToVector2dF(effective_scroll_offset));
8049 EXPECT_VECTOR_EQ(
8050 scroll_layer->draw_properties().screen_space_transform.To2dTranslation(),
8051 container_offset - rounded_effective_scroll_offset);
8054 class AnimationScaleFactorTrackingLayerImpl : public LayerImpl {
8055 public:
8056 static scoped_ptr<AnimationScaleFactorTrackingLayerImpl> Create(
8057 LayerTreeImpl* tree_impl,
8058 int id) {
8059 return make_scoped_ptr(
8060 new AnimationScaleFactorTrackingLayerImpl(tree_impl, id));
8063 ~AnimationScaleFactorTrackingLayerImpl() override {}
8065 private:
8066 explicit AnimationScaleFactorTrackingLayerImpl(LayerTreeImpl* tree_impl,
8067 int id)
8068 : LayerImpl(tree_impl, id) {
8069 SetDrawsContent(true);
8073 TEST_F(LayerTreeHostCommonTest, MaximumAnimationScaleFactor) {
8074 FakeImplProxy proxy;
8075 TestSharedBitmapManager shared_bitmap_manager;
8076 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8077 gfx::Transform identity_matrix;
8078 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> grand_parent =
8079 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 1);
8080 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> parent =
8081 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 2);
8082 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> child =
8083 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 3);
8084 scoped_ptr<AnimationScaleFactorTrackingLayerImpl> grand_child =
8085 AnimationScaleFactorTrackingLayerImpl::Create(host_impl.active_tree(), 4);
8087 AnimationScaleFactorTrackingLayerImpl* parent_raw = parent.get();
8088 AnimationScaleFactorTrackingLayerImpl* child_raw = child.get();
8089 AnimationScaleFactorTrackingLayerImpl* grand_child_raw = grand_child.get();
8091 child->AddChild(grand_child.Pass());
8092 parent->AddChild(child.Pass());
8093 grand_parent->AddChild(parent.Pass());
8095 SetLayerPropertiesForTesting(grand_parent.get(), identity_matrix,
8096 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8097 true, false, true);
8098 SetLayerPropertiesForTesting(parent_raw, identity_matrix, gfx::Point3F(),
8099 gfx::PointF(), gfx::Size(1, 2), true, false,
8100 false);
8101 SetLayerPropertiesForTesting(child_raw, identity_matrix, gfx::Point3F(),
8102 gfx::PointF(), gfx::Size(1, 2), true, false,
8103 false);
8105 SetLayerPropertiesForTesting(grand_child_raw, identity_matrix, gfx::Point3F(),
8106 gfx::PointF(), gfx::Size(1, 2), true, false,
8107 false);
8109 ExecuteCalculateDrawProperties(grand_parent.get());
8111 // No layers have animations.
8112 EXPECT_EQ(0.f,
8113 grand_parent->draw_properties().maximum_animation_contents_scale);
8114 EXPECT_EQ(0.f,
8115 parent_raw->draw_properties().maximum_animation_contents_scale);
8116 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8117 EXPECT_EQ(
8118 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8120 TransformOperations translation;
8121 translation.AppendTranslate(1.f, 2.f, 3.f);
8123 AddAnimatedTransformToLayer(
8124 parent_raw, 1.0, TransformOperations(), translation);
8126 // No layers have scale-affecting animations.
8127 EXPECT_EQ(0.f,
8128 grand_parent->draw_properties().maximum_animation_contents_scale);
8129 EXPECT_EQ(0.f,
8130 parent_raw->draw_properties().maximum_animation_contents_scale);
8131 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8132 EXPECT_EQ(
8133 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8135 TransformOperations scale;
8136 scale.AppendScale(5.f, 4.f, 3.f);
8138 AddAnimatedTransformToLayer(child_raw, 1.0, TransformOperations(), scale);
8139 ExecuteCalculateDrawProperties(grand_parent.get());
8141 // Only |child| has a scale-affecting animation.
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(5.f, child_raw->draw_properties().maximum_animation_contents_scale);
8147 EXPECT_EQ(
8148 5.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8150 AddAnimatedTransformToLayer(
8151 grand_parent.get(), 1.0, TransformOperations(), scale);
8152 ExecuteCalculateDrawProperties(grand_parent.get());
8154 // |grand_parent| and |child| have scale-affecting animations.
8155 EXPECT_EQ(5.f,
8156 grand_parent->draw_properties().maximum_animation_contents_scale);
8157 EXPECT_EQ(5.f,
8158 parent_raw->draw_properties().maximum_animation_contents_scale);
8159 // We don't support combining animated scales from two nodes; 0.f means
8160 // that the maximum scale could not be computed.
8161 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8162 EXPECT_EQ(
8163 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8165 AddAnimatedTransformToLayer(parent_raw, 1.0, TransformOperations(), scale);
8166 ExecuteCalculateDrawProperties(grand_parent.get());
8168 // |grand_parent|, |parent|, and |child| have scale-affecting animations.
8169 EXPECT_EQ(5.f,
8170 grand_parent->draw_properties().maximum_animation_contents_scale);
8171 EXPECT_EQ(0.f,
8172 parent_raw->draw_properties().maximum_animation_contents_scale);
8173 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8174 EXPECT_EQ(
8175 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8177 grand_parent->layer_animation_controller()->AbortAnimations(
8178 Animation::TRANSFORM);
8179 parent_raw->layer_animation_controller()->AbortAnimations(
8180 Animation::TRANSFORM);
8181 child_raw->layer_animation_controller()->AbortAnimations(
8182 Animation::TRANSFORM);
8184 TransformOperations perspective;
8185 perspective.AppendPerspective(10.f);
8187 AddAnimatedTransformToLayer(
8188 child_raw, 1.0, TransformOperations(), perspective);
8189 ExecuteCalculateDrawProperties(grand_parent.get());
8191 // |child| has a scale-affecting animation but computing the maximum of this
8192 // animation is not supported.
8193 EXPECT_EQ(0.f,
8194 grand_parent->draw_properties().maximum_animation_contents_scale);
8195 EXPECT_EQ(0.f,
8196 parent_raw->draw_properties().maximum_animation_contents_scale);
8197 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8198 EXPECT_EQ(
8199 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8201 child_raw->layer_animation_controller()->AbortAnimations(
8202 Animation::TRANSFORM);
8204 gfx::Transform scale_matrix;
8205 scale_matrix.Scale(1.f, 2.f);
8206 grand_parent->SetTransform(scale_matrix);
8207 parent_raw->SetTransform(scale_matrix);
8208 AddAnimatedTransformToLayer(parent_raw, 1.0, TransformOperations(), scale);
8209 ExecuteCalculateDrawProperties(grand_parent.get());
8211 // |grand_parent| and |parent| each have scale 2.f. |parent| has a scale
8212 // animation with maximum scale 5.f.
8213 EXPECT_EQ(0.f,
8214 grand_parent->draw_properties().maximum_animation_contents_scale);
8215 EXPECT_EQ(10.f,
8216 parent_raw->draw_properties().maximum_animation_contents_scale);
8217 EXPECT_EQ(10.f,
8218 child_raw->draw_properties().maximum_animation_contents_scale);
8219 EXPECT_EQ(
8220 10.f,
8221 grand_child_raw->draw_properties().maximum_animation_contents_scale);
8223 gfx::Transform perspective_matrix;
8224 perspective_matrix.ApplyPerspectiveDepth(2.f);
8225 child_raw->SetTransform(perspective_matrix);
8226 ExecuteCalculateDrawProperties(grand_parent.get());
8228 // |child| has a transform that's neither a translation nor a scale.
8229 EXPECT_EQ(0.f,
8230 grand_parent->draw_properties().maximum_animation_contents_scale);
8231 EXPECT_EQ(10.f,
8232 parent_raw->draw_properties().maximum_animation_contents_scale);
8233 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8234 EXPECT_EQ(
8235 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8237 parent_raw->SetTransform(perspective_matrix);
8238 ExecuteCalculateDrawProperties(grand_parent.get());
8240 // |parent| and |child| have transforms that are neither translations nor
8241 // scales.
8242 EXPECT_EQ(0.f,
8243 grand_parent->draw_properties().maximum_animation_contents_scale);
8244 EXPECT_EQ(0.f,
8245 parent_raw->draw_properties().maximum_animation_contents_scale);
8246 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8247 EXPECT_EQ(
8248 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8250 parent_raw->SetTransform(identity_matrix);
8251 child_raw->SetTransform(identity_matrix);
8252 grand_parent->SetTransform(perspective_matrix);
8254 ExecuteCalculateDrawProperties(grand_parent.get());
8256 // |grand_parent| has a transform that's neither a translation nor a scale.
8257 EXPECT_EQ(0.f,
8258 grand_parent->draw_properties().maximum_animation_contents_scale);
8259 EXPECT_EQ(0.f,
8260 parent_raw->draw_properties().maximum_animation_contents_scale);
8261 EXPECT_EQ(0.f, child_raw->draw_properties().maximum_animation_contents_scale);
8262 EXPECT_EQ(
8263 0.f, grand_child_raw->draw_properties().maximum_animation_contents_scale);
8266 static int membership_id(LayerImpl* layer) {
8267 return layer->draw_properties().last_drawn_render_surface_layer_list_id;
8270 static void GatherDrawnLayers(LayerImplList* rsll,
8271 std::set<LayerImpl*>* drawn_layers) {
8272 for (LayerIterator<LayerImpl> it = LayerIterator<LayerImpl>::Begin(rsll),
8273 end = LayerIterator<LayerImpl>::End(rsll);
8274 it != end;
8275 ++it) {
8276 LayerImpl* layer = *it;
8277 if (it.represents_itself())
8278 drawn_layers->insert(layer);
8280 if (!it.represents_contributing_render_surface())
8281 continue;
8283 if (layer->mask_layer())
8284 drawn_layers->insert(layer->mask_layer());
8285 if (layer->replica_layer() && layer->replica_layer()->mask_layer())
8286 drawn_layers->insert(layer->replica_layer()->mask_layer());
8290 TEST_F(LayerTreeHostCommonTest, RenderSurfaceLayerListMembership) {
8291 FakeImplProxy proxy;
8292 TestSharedBitmapManager shared_bitmap_manager;
8293 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8294 gfx::Transform identity_matrix;
8296 scoped_ptr<LayerImpl> grand_parent =
8297 LayerImpl::Create(host_impl.active_tree(), 1);
8298 scoped_ptr<LayerImpl> parent = LayerImpl::Create(host_impl.active_tree(), 3);
8299 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.active_tree(), 5);
8300 scoped_ptr<LayerImpl> grand_child1 =
8301 LayerImpl::Create(host_impl.active_tree(), 7);
8302 scoped_ptr<LayerImpl> grand_child2 =
8303 LayerImpl::Create(host_impl.active_tree(), 9);
8305 LayerImpl* grand_parent_raw = grand_parent.get();
8306 LayerImpl* parent_raw = parent.get();
8307 LayerImpl* child_raw = child.get();
8308 LayerImpl* grand_child1_raw = grand_child1.get();
8309 LayerImpl* grand_child2_raw = grand_child2.get();
8311 child->AddChild(grand_child1.Pass());
8312 child->AddChild(grand_child2.Pass());
8313 parent->AddChild(child.Pass());
8314 grand_parent->AddChild(parent.Pass());
8316 SetLayerPropertiesForTesting(grand_parent_raw, identity_matrix,
8317 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8318 true, false, true);
8319 SetLayerPropertiesForTesting(parent_raw, identity_matrix, gfx::Point3F(),
8320 gfx::PointF(), gfx::Size(1, 2), true, false,
8321 false);
8323 SetLayerPropertiesForTesting(child_raw, identity_matrix, gfx::Point3F(),
8324 gfx::PointF(), gfx::Size(1, 2), true, false,
8325 false);
8327 SetLayerPropertiesForTesting(grand_child1_raw, identity_matrix,
8328 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8329 true, false, false);
8331 SetLayerPropertiesForTesting(grand_child2_raw, identity_matrix,
8332 gfx::Point3F(), gfx::PointF(), gfx::Size(1, 2),
8333 true, false, false);
8335 // Start with nothing being drawn.
8336 ExecuteCalculateDrawProperties(grand_parent_raw);
8337 int member_id = render_surface_layer_list_count();
8339 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8340 EXPECT_NE(member_id, membership_id(parent_raw));
8341 EXPECT_NE(member_id, membership_id(child_raw));
8342 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8343 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8345 std::set<LayerImpl*> expected;
8346 std::set<LayerImpl*> actual;
8347 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8348 EXPECT_EQ(expected, actual);
8350 // If we force render surface, but none of the layers are in the layer list,
8351 // then this layer should not appear in RSLL.
8352 grand_child1_raw->SetHasRenderSurface(true);
8354 ExecuteCalculateDrawProperties(grand_parent_raw);
8355 member_id = render_surface_layer_list_count();
8357 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8358 EXPECT_NE(member_id, membership_id(parent_raw));
8359 EXPECT_NE(member_id, membership_id(child_raw));
8360 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8361 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8363 expected.clear();
8364 actual.clear();
8365 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8366 EXPECT_EQ(expected, actual);
8368 // However, if we say that this layer also draws content, it will appear in
8369 // RSLL.
8370 grand_child1_raw->SetDrawsContent(true);
8372 ExecuteCalculateDrawProperties(grand_parent_raw);
8373 member_id = render_surface_layer_list_count();
8375 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8376 EXPECT_NE(member_id, membership_id(parent_raw));
8377 EXPECT_NE(member_id, membership_id(child_raw));
8378 EXPECT_EQ(member_id, membership_id(grand_child1_raw));
8379 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8381 expected.clear();
8382 expected.insert(grand_child1_raw);
8384 actual.clear();
8385 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8386 EXPECT_EQ(expected, actual);
8388 // Now child is forced to have a render surface, and one if its children draws
8389 // content.
8390 grand_child1_raw->SetDrawsContent(false);
8391 grand_child1_raw->SetHasRenderSurface(false);
8392 child_raw->SetHasRenderSurface(true);
8393 grand_child2_raw->SetDrawsContent(true);
8395 ExecuteCalculateDrawProperties(grand_parent_raw);
8396 member_id = render_surface_layer_list_count();
8398 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8399 EXPECT_NE(member_id, membership_id(parent_raw));
8400 EXPECT_NE(member_id, membership_id(child_raw));
8401 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8402 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8404 expected.clear();
8405 expected.insert(grand_child2_raw);
8407 actual.clear();
8408 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8409 EXPECT_EQ(expected, actual);
8411 // Add a mask layer to child.
8412 child_raw->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 6).Pass());
8414 ExecuteCalculateDrawProperties(grand_parent_raw);
8415 member_id = render_surface_layer_list_count();
8417 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8418 EXPECT_NE(member_id, membership_id(parent_raw));
8419 EXPECT_NE(member_id, membership_id(child_raw));
8420 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8421 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8422 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8424 expected.clear();
8425 expected.insert(grand_child2_raw);
8426 expected.insert(child_raw->mask_layer());
8428 expected.clear();
8429 expected.insert(grand_child2_raw);
8430 expected.insert(child_raw->mask_layer());
8432 actual.clear();
8433 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8434 EXPECT_EQ(expected, actual);
8436 // Add replica mask layer.
8437 scoped_ptr<LayerImpl> replica_layer =
8438 LayerImpl::Create(host_impl.active_tree(), 20);
8439 replica_layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 21));
8440 child_raw->SetReplicaLayer(replica_layer.Pass());
8442 ExecuteCalculateDrawProperties(grand_parent_raw);
8443 member_id = render_surface_layer_list_count();
8445 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8446 EXPECT_NE(member_id, membership_id(parent_raw));
8447 EXPECT_NE(member_id, membership_id(child_raw));
8448 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8449 EXPECT_EQ(member_id, membership_id(child_raw->replica_layer()->mask_layer()));
8450 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8451 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8453 expected.clear();
8454 expected.insert(grand_child2_raw);
8455 expected.insert(child_raw->mask_layer());
8456 expected.insert(child_raw->replica_layer()->mask_layer());
8458 actual.clear();
8459 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8460 EXPECT_EQ(expected, actual);
8462 child_raw->TakeReplicaLayer();
8464 // With nothing drawing, we should have no layers.
8465 grand_child2_raw->SetDrawsContent(false);
8467 ExecuteCalculateDrawProperties(grand_parent_raw);
8468 member_id = render_surface_layer_list_count();
8470 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8471 EXPECT_NE(member_id, membership_id(parent_raw));
8472 EXPECT_NE(member_id, membership_id(child_raw));
8473 EXPECT_NE(member_id, membership_id(child_raw->mask_layer()));
8474 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8475 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8477 expected.clear();
8478 actual.clear();
8479 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8480 EXPECT_EQ(expected, actual);
8482 // Child itself draws means that we should have the child and the mask in the
8483 // list.
8484 child_raw->SetDrawsContent(true);
8486 ExecuteCalculateDrawProperties(grand_parent_raw);
8487 member_id = render_surface_layer_list_count();
8489 EXPECT_NE(member_id, membership_id(grand_parent_raw));
8490 EXPECT_NE(member_id, membership_id(parent_raw));
8491 EXPECT_EQ(member_id, membership_id(child_raw));
8492 EXPECT_EQ(member_id, membership_id(child_raw->mask_layer()));
8493 EXPECT_NE(member_id, membership_id(grand_child1_raw));
8494 EXPECT_NE(member_id, membership_id(grand_child2_raw));
8496 expected.clear();
8497 expected.insert(child_raw);
8498 expected.insert(child_raw->mask_layer());
8499 actual.clear();
8500 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8501 EXPECT_EQ(expected, actual);
8503 child_raw->TakeMaskLayer();
8505 // Now everyone's a member!
8506 grand_parent_raw->SetDrawsContent(true);
8507 parent_raw->SetDrawsContent(true);
8508 child_raw->SetDrawsContent(true);
8509 grand_child1_raw->SetDrawsContent(true);
8510 grand_child2_raw->SetDrawsContent(true);
8512 ExecuteCalculateDrawProperties(grand_parent_raw);
8513 member_id = render_surface_layer_list_count();
8515 EXPECT_EQ(member_id, membership_id(grand_parent_raw));
8516 EXPECT_EQ(member_id, membership_id(parent_raw));
8517 EXPECT_EQ(member_id, membership_id(child_raw));
8518 EXPECT_EQ(member_id, membership_id(grand_child1_raw));
8519 EXPECT_EQ(member_id, membership_id(grand_child2_raw));
8521 expected.clear();
8522 expected.insert(grand_parent_raw);
8523 expected.insert(parent_raw);
8524 expected.insert(child_raw);
8525 expected.insert(grand_child1_raw);
8526 expected.insert(grand_child2_raw);
8528 actual.clear();
8529 GatherDrawnLayers(render_surface_layer_list_impl(), &actual);
8530 EXPECT_EQ(expected, actual);
8533 TEST_F(LayerTreeHostCommonTest, DrawPropertyScales) {
8534 FakeImplProxy proxy;
8535 TestSharedBitmapManager shared_bitmap_manager;
8536 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8538 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
8539 LayerImpl* root_layer = root.get();
8540 scoped_ptr<LayerImpl> child1 = LayerImpl::Create(host_impl.active_tree(), 2);
8541 LayerImpl* child1_layer = child1.get();
8542 scoped_ptr<LayerImpl> child2 = LayerImpl::Create(host_impl.active_tree(), 3);
8543 LayerImpl* child2_layer = child2.get();
8545 root->AddChild(child1.Pass());
8546 root->AddChild(child2.Pass());
8547 root->SetHasRenderSurface(true);
8549 gfx::Transform identity_matrix, scale_transform_child1,
8550 scale_transform_child2;
8551 scale_transform_child1.Scale(2, 3);
8552 scale_transform_child2.Scale(4, 5);
8554 SetLayerPropertiesForTesting(root_layer, identity_matrix, gfx::Point3F(),
8555 gfx::PointF(), gfx::Size(1, 1), true, false,
8556 true);
8557 SetLayerPropertiesForTesting(child1_layer, scale_transform_child1,
8558 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
8559 false, false);
8561 child1_layer->SetMaskLayer(
8562 LayerImpl::Create(host_impl.active_tree(), 4).Pass());
8564 scoped_ptr<LayerImpl> replica_layer =
8565 LayerImpl::Create(host_impl.active_tree(), 5);
8566 replica_layer->SetHasRenderSurface(true);
8567 replica_layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 6));
8568 child1_layer->SetReplicaLayer(replica_layer.Pass());
8569 child1_layer->SetHasRenderSurface(true);
8571 ExecuteCalculateDrawProperties(root_layer);
8573 TransformOperations scale;
8574 scale.AppendScale(5.f, 8.f, 3.f);
8576 AddAnimatedTransformToLayer(child2_layer, 1.0, TransformOperations(), scale);
8577 SetLayerPropertiesForTesting(child2_layer, scale_transform_child2,
8578 gfx::Point3F(), gfx::PointF(), gfx::Size(), true,
8579 false, false);
8581 ExecuteCalculateDrawProperties(root_layer);
8583 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().ideal_contents_scale);
8584 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().ideal_contents_scale);
8585 EXPECT_FLOAT_EQ(
8586 3.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8587 EXPECT_FLOAT_EQ(3.f,
8588 child1_layer->replica_layer()
8589 ->mask_layer()
8590 ->draw_properties()
8591 .ideal_contents_scale);
8592 EXPECT_FLOAT_EQ(5.f, child2_layer->draw_properties().ideal_contents_scale);
8594 EXPECT_FLOAT_EQ(
8595 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8596 EXPECT_FLOAT_EQ(
8597 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8598 EXPECT_FLOAT_EQ(0.f,
8599 child1_layer->mask_layer()
8600 ->draw_properties()
8601 .maximum_animation_contents_scale);
8602 EXPECT_FLOAT_EQ(0.f,
8603 child1_layer->replica_layer()
8604 ->mask_layer()
8605 ->draw_properties()
8606 .maximum_animation_contents_scale);
8607 EXPECT_FLOAT_EQ(
8608 8.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8610 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8611 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().page_scale_factor);
8612 EXPECT_FLOAT_EQ(
8613 1.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8614 EXPECT_FLOAT_EQ(1.f,
8615 child1_layer->replica_layer()
8616 ->mask_layer()
8617 ->draw_properties()
8618 .page_scale_factor);
8619 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().page_scale_factor);
8621 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().device_scale_factor);
8622 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().device_scale_factor);
8623 EXPECT_FLOAT_EQ(
8624 1.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8625 EXPECT_FLOAT_EQ(1.f,
8626 child1_layer->replica_layer()
8627 ->mask_layer()
8628 ->draw_properties()
8629 .device_scale_factor);
8630 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().device_scale_factor);
8632 // Changing page-scale would affect ideal_contents_scale and
8633 // maximum_animation_contents_scale.
8635 float page_scale_factor = 3.f;
8636 float device_scale_factor = 1.0f;
8637 std::vector<LayerImpl*> render_surface_layer_list;
8638 gfx::Size device_viewport_size =
8639 gfx::Size(root_layer->bounds().width() * device_scale_factor,
8640 root_layer->bounds().height() * device_scale_factor);
8641 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
8642 root_layer, device_viewport_size, &render_surface_layer_list);
8644 inputs.page_scale_factor = page_scale_factor;
8645 inputs.can_adjust_raster_scales = true;
8646 inputs.page_scale_layer = root_layer;
8647 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8649 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().ideal_contents_scale);
8650 EXPECT_FLOAT_EQ(9.f, child1_layer->draw_properties().ideal_contents_scale);
8651 EXPECT_FLOAT_EQ(
8652 9.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8653 EXPECT_FLOAT_EQ(9.f,
8654 child1_layer->replica_layer()
8655 ->mask_layer()
8656 ->draw_properties()
8657 .ideal_contents_scale);
8658 EXPECT_FLOAT_EQ(15.f, child2_layer->draw_properties().ideal_contents_scale);
8660 EXPECT_FLOAT_EQ(
8661 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8662 EXPECT_FLOAT_EQ(
8663 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8664 EXPECT_FLOAT_EQ(0.f,
8665 child1_layer->mask_layer()
8666 ->draw_properties()
8667 .maximum_animation_contents_scale);
8668 EXPECT_FLOAT_EQ(0.f,
8669 child1_layer->replica_layer()
8670 ->mask_layer()
8671 ->draw_properties()
8672 .maximum_animation_contents_scale);
8673 EXPECT_FLOAT_EQ(
8674 24.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8676 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8677 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().page_scale_factor);
8678 EXPECT_FLOAT_EQ(
8679 3.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8680 EXPECT_FLOAT_EQ(3.f,
8681 child1_layer->replica_layer()
8682 ->mask_layer()
8683 ->draw_properties()
8684 .page_scale_factor);
8685 EXPECT_FLOAT_EQ(3.f, child2_layer->draw_properties().page_scale_factor);
8687 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().device_scale_factor);
8688 EXPECT_FLOAT_EQ(1.f, child1_layer->draw_properties().device_scale_factor);
8689 EXPECT_FLOAT_EQ(
8690 1.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8691 EXPECT_FLOAT_EQ(1.f,
8692 child1_layer->replica_layer()
8693 ->mask_layer()
8694 ->draw_properties()
8695 .device_scale_factor);
8696 EXPECT_FLOAT_EQ(1.f, child2_layer->draw_properties().device_scale_factor);
8698 // Changing device-scale would affect ideal_contents_scale and
8699 // maximum_animation_contents_scale.
8701 device_scale_factor = 4.0f;
8702 inputs.device_scale_factor = device_scale_factor;
8703 inputs.can_adjust_raster_scales = true;
8704 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8706 EXPECT_FLOAT_EQ(4.f, root_layer->draw_properties().ideal_contents_scale);
8707 EXPECT_FLOAT_EQ(36.f, child1_layer->draw_properties().ideal_contents_scale);
8708 EXPECT_FLOAT_EQ(
8709 36.f, child1_layer->mask_layer()->draw_properties().ideal_contents_scale);
8710 EXPECT_FLOAT_EQ(36.f,
8711 child1_layer->replica_layer()
8712 ->mask_layer()
8713 ->draw_properties()
8714 .ideal_contents_scale);
8715 EXPECT_FLOAT_EQ(60.f, child2_layer->draw_properties().ideal_contents_scale);
8717 EXPECT_FLOAT_EQ(
8718 0.f, root_layer->draw_properties().maximum_animation_contents_scale);
8719 EXPECT_FLOAT_EQ(
8720 0.f, child1_layer->draw_properties().maximum_animation_contents_scale);
8721 EXPECT_FLOAT_EQ(0.f,
8722 child1_layer->mask_layer()
8723 ->draw_properties()
8724 .maximum_animation_contents_scale);
8725 EXPECT_FLOAT_EQ(0.f,
8726 child1_layer->replica_layer()
8727 ->mask_layer()
8728 ->draw_properties()
8729 .maximum_animation_contents_scale);
8730 EXPECT_FLOAT_EQ(
8731 96.f, child2_layer->draw_properties().maximum_animation_contents_scale);
8733 EXPECT_FLOAT_EQ(1.f, root_layer->draw_properties().page_scale_factor);
8734 EXPECT_FLOAT_EQ(3.f, child1_layer->draw_properties().page_scale_factor);
8735 EXPECT_FLOAT_EQ(
8736 3.f, child1_layer->mask_layer()->draw_properties().page_scale_factor);
8737 EXPECT_FLOAT_EQ(3.f,
8738 child1_layer->replica_layer()
8739 ->mask_layer()
8740 ->draw_properties()
8741 .page_scale_factor);
8742 EXPECT_FLOAT_EQ(3.f, child2_layer->draw_properties().page_scale_factor);
8744 EXPECT_FLOAT_EQ(4.f, root_layer->draw_properties().device_scale_factor);
8745 EXPECT_FLOAT_EQ(4.f, child1_layer->draw_properties().device_scale_factor);
8746 EXPECT_FLOAT_EQ(
8747 4.f, child1_layer->mask_layer()->draw_properties().device_scale_factor);
8748 EXPECT_FLOAT_EQ(4.f,
8749 child1_layer->replica_layer()
8750 ->mask_layer()
8751 ->draw_properties()
8752 .device_scale_factor);
8753 EXPECT_FLOAT_EQ(4.f, child2_layer->draw_properties().device_scale_factor);
8756 TEST_F(LayerTreeHostCommonTest, VisibleContentRectInChildRenderSurface) {
8757 scoped_refptr<Layer> root = Layer::Create(layer_settings());
8758 SetLayerPropertiesForTesting(root.get(),
8759 gfx::Transform(),
8760 gfx::Point3F(),
8761 gfx::PointF(),
8762 gfx::Size(768 / 2, 3000),
8763 true,
8764 false);
8765 root->SetIsDrawable(true);
8767 scoped_refptr<Layer> clip = Layer::Create(layer_settings());
8768 SetLayerPropertiesForTesting(clip.get(),
8769 gfx::Transform(),
8770 gfx::Point3F(),
8771 gfx::PointF(),
8772 gfx::Size(768 / 2, 10000),
8773 true,
8774 false);
8775 clip->SetMasksToBounds(true);
8777 scoped_refptr<Layer> content = Layer::Create(layer_settings());
8778 SetLayerPropertiesForTesting(content.get(),
8779 gfx::Transform(),
8780 gfx::Point3F(),
8781 gfx::PointF(),
8782 gfx::Size(768 / 2, 10000),
8783 true,
8784 false);
8785 content->SetIsDrawable(true);
8786 content->SetForceRenderSurface(true);
8788 root->AddChild(clip);
8789 clip->AddChild(content);
8791 FakeLayerTreeHostClient client(FakeLayerTreeHostClient::DIRECT_3D);
8792 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&client);
8793 host->SetRootLayer(root);
8795 gfx::Size device_viewport_size(768, 582);
8796 RenderSurfaceLayerList render_surface_layer_list;
8797 LayerTreeHostCommon::CalcDrawPropsMainInputsForTesting inputs(
8798 host->root_layer(), device_viewport_size, &render_surface_layer_list);
8799 inputs.device_scale_factor = 2.f;
8800 inputs.page_scale_factor = 1.f;
8801 inputs.page_scale_layer = NULL;
8802 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8804 // Layers in the root render surface have their visible content rect clipped
8805 // by the viewport.
8806 EXPECT_EQ(gfx::Rect(768 / 2, 582 / 2), root->visible_content_rect());
8808 // Layers drawing to a child render surface should still have their visible
8809 // content rect clipped by the viewport.
8810 EXPECT_EQ(gfx::Rect(768 / 2, 582 / 2), content->visible_content_rect());
8813 TEST_F(LayerTreeHostCommonTest, BoundsDeltaAffectVisibleContentRect) {
8814 FakeImplProxy proxy;
8815 TestSharedBitmapManager shared_bitmap_manager;
8816 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
8818 // Set two layers: the root layer clips it's child,
8819 // the child draws its content.
8821 gfx::Size root_size = gfx::Size(300, 500);
8823 // Sublayer should be bigger than the root enlarged by bounds_delta.
8824 gfx::Size sublayer_size = gfx::Size(300, 1000);
8826 // Device viewport accomidated the root and the top controls.
8827 gfx::Size device_viewport_size = gfx::Size(300, 600);
8828 gfx::Transform identity_matrix;
8830 host_impl.active_tree()->SetRootLayer(
8831 LayerImpl::Create(host_impl.active_tree(), 1));
8833 LayerImpl* root = host_impl.active_tree()->root_layer();
8834 SetLayerPropertiesForTesting(root,
8835 identity_matrix,
8836 gfx::Point3F(),
8837 gfx::PointF(),
8838 root_size,
8839 false,
8840 false,
8841 true);
8843 root->SetContentBounds(root_size);
8844 root->SetMasksToBounds(true);
8846 root->AddChild(LayerImpl::Create(host_impl.active_tree(), 2));
8848 LayerImpl* sublayer = root->child_at(0);
8849 SetLayerPropertiesForTesting(sublayer,
8850 identity_matrix,
8851 gfx::Point3F(),
8852 gfx::PointF(),
8853 sublayer_size,
8854 false,
8855 false,
8856 false);
8858 sublayer->SetContentBounds(sublayer_size);
8859 sublayer->SetDrawsContent(true);
8861 LayerImplList layer_impl_list;
8862 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting inputs(
8863 root, device_viewport_size, &layer_impl_list);
8865 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8867 EXPECT_EQ(gfx::Rect(root_size), sublayer->visible_content_rect());
8869 root->SetBoundsDelta(gfx::Vector2dF(0.0, 50.0));
8871 LayerTreeHostCommon::CalculateDrawProperties(&inputs);
8873 gfx::Rect affected_by_delta(0, 0, root_size.width(),
8874 root_size.height() + 50);
8875 EXPECT_EQ(affected_by_delta, sublayer->visible_content_rect());
8878 TEST_F(LayerTreeHostCommonTest, VisibleContentRectForAnimatedLayer) {
8879 const gfx::Transform identity_matrix;
8880 scoped_refptr<Layer> root = Layer::Create(layer_settings());
8881 scoped_refptr<LayerWithForcedDrawsContent> animated =
8882 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
8884 root->AddChild(animated);
8886 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8887 host->SetRootLayer(root);
8889 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
8890 gfx::PointF(), gfx::Size(100, 100), true, false);
8891 SetLayerPropertiesForTesting(animated.get(), identity_matrix, gfx::Point3F(),
8892 gfx::PointF(), gfx::Size(20, 20), true, false);
8894 root->SetMasksToBounds(true);
8895 root->SetForceRenderSurface(true);
8896 animated->SetOpacity(0.f);
8898 AddOpacityTransitionToController(animated->layer_animation_controller(), 10.0,
8899 0.f, 1.f, false);
8901 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8903 EXPECT_FALSE(animated->visible_rect_from_property_trees().IsEmpty());
8906 TEST_F(LayerTreeHostCommonTest,
8907 VisibleContentRectForAnimatedLayerWithSingularTransform) {
8908 const gfx::Transform identity_matrix;
8909 scoped_refptr<Layer> root = Layer::Create(layer_settings());
8910 scoped_refptr<Layer> clip = Layer::Create(layer_settings());
8911 scoped_refptr<LayerWithForcedDrawsContent> animated =
8912 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
8913 scoped_refptr<LayerWithForcedDrawsContent> surface =
8914 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
8915 scoped_refptr<LayerWithForcedDrawsContent> descendant_of_animation =
8916 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
8918 root->AddChild(clip);
8919 clip->AddChild(animated);
8920 animated->AddChild(surface);
8921 surface->AddChild(descendant_of_animation);
8923 clip->SetMasksToBounds(true);
8924 surface->SetForceRenderSurface(true);
8926 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
8927 host->SetRootLayer(root);
8929 gfx::Transform uninvertible_matrix;
8930 uninvertible_matrix.Scale3d(6.f, 6.f, 0.f);
8932 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
8933 gfx::PointF(), gfx::Size(100, 100), true, false);
8934 SetLayerPropertiesForTesting(clip.get(), identity_matrix, gfx::Point3F(),
8935 gfx::PointF(), gfx::Size(10, 10), true, false);
8936 SetLayerPropertiesForTesting(animated.get(), uninvertible_matrix,
8937 gfx::Point3F(), gfx::PointF(),
8938 gfx::Size(120, 120), true, false);
8939 SetLayerPropertiesForTesting(surface.get(), identity_matrix, gfx::Point3F(),
8940 gfx::PointF(), gfx::Size(100, 100), true, false);
8941 SetLayerPropertiesForTesting(descendant_of_animation.get(), identity_matrix,
8942 gfx::Point3F(), gfx::PointF(),
8943 gfx::Size(200, 200), true, false);
8945 TransformOperations start_transform_operations;
8946 start_transform_operations.AppendMatrix(uninvertible_matrix);
8947 TransformOperations end_transform_operations;
8949 AddAnimatedTransformToLayer(animated.get(), 10.0, start_transform_operations,
8950 end_transform_operations);
8952 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8954 // The animated layer has a singular transform and maps to a non-empty rect in
8955 // clipped target space, so is treated as fully visible.
8956 EXPECT_EQ(gfx::Rect(120, 120), animated->visible_rect_from_property_trees());
8958 // The singular transform on |animated| is flattened when inherited by
8959 // |surface|, and this happens to make it invertible.
8960 EXPECT_EQ(gfx::Rect(2, 2), surface->visible_rect_from_property_trees());
8961 EXPECT_EQ(gfx::Rect(2, 2),
8962 descendant_of_animation->visible_rect_from_property_trees());
8964 gfx::Transform zero_matrix;
8965 zero_matrix.Scale3d(0.f, 0.f, 0.f);
8966 SetLayerPropertiesForTesting(animated.get(), zero_matrix, gfx::Point3F(),
8967 gfx::PointF(), gfx::Size(120, 120), true, false);
8969 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
8971 // The animated layer maps to the empty rect in clipped target space, so is
8972 // treated as having an empty visible rect.
8973 EXPECT_EQ(gfx::Rect(), animated->visible_rect_from_property_trees());
8975 // This time, flattening does not make |animated|'s transform invertible. This
8976 // means the clip cannot be projected into |surface|'s space, so we treat
8977 // |surface| and layers that draw into it as fully visible.
8978 EXPECT_EQ(gfx::Rect(100, 100), surface->visible_rect_from_property_trees());
8979 EXPECT_EQ(gfx::Rect(200, 200),
8980 descendant_of_animation->visible_rect_from_property_trees());
8983 // Verify that having an animated filter (but no current filter, as these
8984 // are mutually exclusive) correctly creates a render surface.
8985 TEST_F(LayerTreeHostCommonTest, AnimatedFilterCreatesRenderSurface) {
8986 scoped_refptr<Layer> root = Layer::Create(layer_settings());
8987 scoped_refptr<Layer> child = Layer::Create(layer_settings());
8988 scoped_refptr<Layer> grandchild = Layer::Create(layer_settings());
8989 root->AddChild(child);
8990 child->AddChild(grandchild);
8992 gfx::Transform identity_transform;
8993 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
8994 gfx::PointF(), gfx::Size(50, 50), true, false);
8995 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
8996 gfx::PointF(), gfx::Size(50, 50), true, false);
8997 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
8998 gfx::Point3F(), gfx::PointF(), gfx::Size(50, 50),
8999 true, false);
9000 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9001 host->SetRootLayer(root);
9003 AddAnimatedFilterToLayer(child.get(), 10.0, 0.1f, 0.2f);
9005 ExecuteCalculateDrawProperties(root.get());
9007 EXPECT_TRUE(root->render_surface());
9008 EXPECT_TRUE(child->render_surface());
9009 EXPECT_FALSE(grandchild->render_surface());
9011 EXPECT_TRUE(root->filters().IsEmpty());
9012 EXPECT_TRUE(child->filters().IsEmpty());
9013 EXPECT_TRUE(grandchild->filters().IsEmpty());
9015 EXPECT_FALSE(root->FilterIsAnimating());
9016 EXPECT_TRUE(child->FilterIsAnimating());
9017 EXPECT_FALSE(grandchild->FilterIsAnimating());
9020 // Ensures that the property tree code accounts for offsets between fixed
9021 // position layers and their respective containers.
9022 TEST_F(LayerTreeHostCommonTest, PropertyTreesAccountForFixedParentOffset) {
9023 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9024 scoped_refptr<Layer> child = Layer::Create(layer_settings());
9025 scoped_refptr<LayerWithForcedDrawsContent> grandchild =
9026 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9028 root->AddChild(child);
9029 child->AddChild(grandchild);
9031 gfx::Transform identity_transform;
9032 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
9033 gfx::PointF(), gfx::Size(50, 50), true, false);
9034 SetLayerPropertiesForTesting(child.get(), identity_transform, gfx::Point3F(),
9035 gfx::PointF(1000, 1000), gfx::Size(50, 50), true,
9036 false);
9037 SetLayerPropertiesForTesting(grandchild.get(), identity_transform,
9038 gfx::Point3F(), gfx::PointF(-1000, -1000),
9039 gfx::Size(50, 50), true, false);
9041 root->SetMasksToBounds(true);
9042 root->SetIsContainerForFixedPositionLayers(true);
9043 LayerPositionConstraint constraint;
9044 constraint.set_is_fixed_position(true);
9045 grandchild->SetPositionConstraint(constraint);
9047 root->SetIsContainerForFixedPositionLayers(true);
9049 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9050 host->SetRootLayer(root);
9052 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9054 EXPECT_EQ(gfx::Rect(0, 0, 50, 50),
9055 grandchild->visible_rect_from_property_trees());
9058 TEST_F(LayerTreeHostCommonTest, CombineClipsUsingContentTarget) {
9059 // In the following layer tree, the layer |box|'s render target is |surface|.
9060 // |surface| also creates a transform node. We want to combine clips for |box|
9061 // in the space of its target (i.e., |surface|), not its target's target. This
9062 // test ensures that happens.
9064 gfx::Transform rotate;
9065 rotate.Rotate(5);
9066 gfx::Transform identity;
9068 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9069 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9070 gfx::PointF(), gfx::Size(2500, 1500), true,
9071 false);
9073 scoped_refptr<Layer> frame_clip = Layer::Create(layer_settings());
9074 SetLayerPropertiesForTesting(frame_clip.get(), identity, gfx::Point3F(),
9075 gfx::PointF(), gfx::Size(2500, 1500), true,
9076 false);
9077 frame_clip->SetMasksToBounds(true);
9079 scoped_refptr<Layer> rotated = Layer::Create(layer_settings());
9080 SetLayerPropertiesForTesting(rotated.get(), rotate,
9081 gfx::Point3F(1250, 250, 0), gfx::PointF(),
9082 gfx::Size(2500, 500), true, false);
9084 scoped_refptr<Layer> surface = Layer::Create(layer_settings());
9085 SetLayerPropertiesForTesting(surface.get(), rotate, gfx::Point3F(),
9086 gfx::PointF(), gfx::Size(2500, 500), true,
9087 false);
9088 surface->SetOpacity(0.5);
9090 scoped_refptr<LayerWithForcedDrawsContent> container =
9091 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9092 SetLayerPropertiesForTesting(container.get(), identity, gfx::Point3F(),
9093 gfx::PointF(), gfx::Size(300, 300), true, false);
9095 scoped_refptr<LayerWithForcedDrawsContent> box =
9096 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9097 SetLayerPropertiesForTesting(box.get(), identity, gfx::Point3F(),
9098 gfx::PointF(), gfx::Size(100, 100), true, false);
9100 root->AddChild(frame_clip);
9101 frame_clip->AddChild(rotated);
9102 rotated->AddChild(surface);
9103 surface->AddChild(container);
9104 surface->AddChild(box);
9106 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9107 host->SetRootLayer(root);
9109 ExecuteCalculateDrawProperties(root.get());
9112 TEST_F(LayerTreeHostCommonTest, OnlyApplyFixedPositioningOnce) {
9113 gfx::Transform identity;
9114 gfx::Transform translate_z;
9115 translate_z.Translate3d(0, 0, 10);
9117 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9118 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9119 gfx::PointF(), gfx::Size(800, 800), true, false);
9120 root->SetIsContainerForFixedPositionLayers(true);
9122 scoped_refptr<Layer> frame_clip = Layer::Create(layer_settings());
9123 SetLayerPropertiesForTesting(frame_clip.get(), translate_z, gfx::Point3F(),
9124 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9125 false);
9126 frame_clip->SetMasksToBounds(true);
9128 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9129 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9130 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9131 gfx::PointF(), gfx::Size(1000, 1000), true,
9132 false);
9134 LayerPositionConstraint constraint;
9135 constraint.set_is_fixed_position(true);
9136 fixed->SetPositionConstraint(constraint);
9138 root->AddChild(frame_clip);
9139 frame_clip->AddChild(fixed);
9141 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9142 host->SetRootLayer(root);
9144 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9146 gfx::Rect expected(0, 0, 100, 100);
9147 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9150 TEST_F(LayerTreeHostCommonTest,
9151 PropertyTreesAccountForScrollCompensationAdjustment) {
9152 gfx::Transform identity;
9153 gfx::Transform translate_z;
9154 translate_z.Translate3d(0, 0, 10);
9156 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9157 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9158 gfx::PointF(), gfx::Size(800, 800), true, false);
9159 root->SetIsContainerForFixedPositionLayers(true);
9161 scoped_refptr<Layer> frame_clip = Layer::Create(layer_settings());
9162 SetLayerPropertiesForTesting(frame_clip.get(), translate_z, gfx::Point3F(),
9163 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9164 false);
9165 frame_clip->SetMasksToBounds(true);
9167 scoped_refptr<LayerWithForcedDrawsContent> scroller =
9168 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9169 SetLayerPropertiesForTesting(scroller.get(), identity, gfx::Point3F(),
9170 gfx::PointF(), gfx::Size(1000, 1000), true,
9171 false);
9173 scroller->SetScrollCompensationAdjustment(gfx::Vector2dF(0.3f, 0.7f));
9174 scroller->SetScrollOffset(gfx::ScrollOffset(0.3, 0.7));
9175 scroller->SetScrollClipLayerId(frame_clip->id());
9177 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9178 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9179 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9180 gfx::PointF(), gfx::Size(50, 50), true, false);
9182 LayerPositionConstraint constraint;
9183 constraint.set_is_fixed_position(true);
9184 fixed->SetPositionConstraint(constraint);
9186 scoped_refptr<LayerWithForcedDrawsContent> fixed_child =
9187 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9188 SetLayerPropertiesForTesting(fixed_child.get(), identity, gfx::Point3F(),
9189 gfx::PointF(), gfx::Size(10, 10), true, false);
9191 fixed_child->SetPositionConstraint(constraint);
9193 root->AddChild(frame_clip);
9194 frame_clip->AddChild(scroller);
9195 scroller->AddChild(fixed);
9196 fixed->AddChild(fixed_child);
9198 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9199 host->SetRootLayer(root);
9201 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9203 gfx::Rect expected(0, 0, 50, 50);
9204 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9206 expected = gfx::Rect(0, 0, 10, 10);
9207 EXPECT_EQ(expected, fixed_child->visible_rect_from_property_trees());
9210 TEST_F(LayerTreeHostCommonTest, FixedClipsShouldBeAssociatedWithTheRightNode) {
9211 gfx::Transform identity;
9213 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9214 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9215 gfx::PointF(), gfx::Size(800, 800), true, false);
9216 root->SetIsContainerForFixedPositionLayers(true);
9218 scoped_refptr<Layer> frame_clip = Layer::Create(layer_settings());
9219 SetLayerPropertiesForTesting(frame_clip.get(), identity, gfx::Point3F(),
9220 gfx::PointF(500, 100), gfx::Size(100, 100), true,
9221 false);
9222 frame_clip->SetMasksToBounds(true);
9224 scoped_refptr<LayerWithForcedDrawsContent> scroller =
9225 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9226 SetLayerPropertiesForTesting(scroller.get(), identity, gfx::Point3F(),
9227 gfx::PointF(), gfx::Size(1000, 1000), true,
9228 false);
9230 scroller->SetScrollOffset(gfx::ScrollOffset(100, 100));
9231 scroller->SetScrollClipLayerId(frame_clip->id());
9233 scoped_refptr<LayerWithForcedDrawsContent> fixed =
9234 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9235 SetLayerPropertiesForTesting(fixed.get(), identity, gfx::Point3F(),
9236 gfx::PointF(100, 100), gfx::Size(50, 50), true,
9237 false);
9239 LayerPositionConstraint constraint;
9240 constraint.set_is_fixed_position(true);
9241 fixed->SetPositionConstraint(constraint);
9242 fixed->SetForceRenderSurface(true);
9243 fixed->SetMasksToBounds(true);
9245 root->AddChild(frame_clip);
9246 frame_clip->AddChild(scroller);
9247 scroller->AddChild(fixed);
9249 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9250 host->SetRootLayer(root);
9252 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9254 gfx::Rect expected(0, 0, 50, 50);
9255 EXPECT_EQ(expected, fixed->visible_rect_from_property_trees());
9258 TEST_F(LayerTreeHostCommonTest, ChangingAxisAlignmentTriggersRebuild) {
9259 gfx::Transform identity;
9260 gfx::Transform translate;
9261 gfx::Transform rotate;
9263 translate.Translate(10, 10);
9264 rotate.Rotate(45);
9266 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9267 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9268 gfx::PointF(), gfx::Size(800, 800), true, false);
9269 root->SetIsContainerForFixedPositionLayers(true);
9271 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9272 host->SetRootLayer(root);
9274 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9275 EXPECT_FALSE(host->property_trees()->needs_rebuild);
9277 root->SetTransform(translate);
9278 EXPECT_FALSE(host->property_trees()->needs_rebuild);
9280 root->SetTransform(rotate);
9281 EXPECT_TRUE(host->property_trees()->needs_rebuild);
9284 TEST_F(LayerTreeHostCommonTest, ChangeTransformOrigin) {
9285 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9286 scoped_refptr<LayerWithForcedDrawsContent> child =
9287 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9288 root->AddChild(child);
9290 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9291 host->SetRootLayer(root);
9293 gfx::Transform identity_matrix;
9294 gfx::Transform scale_matrix;
9295 scale_matrix.Scale(2.f, 2.f);
9296 SetLayerPropertiesForTesting(root.get(), identity_matrix, gfx::Point3F(),
9297 gfx::PointF(), gfx::Size(100, 100), true, false);
9298 SetLayerPropertiesForTesting(child.get(), scale_matrix, gfx::Point3F(),
9299 gfx::PointF(), gfx::Size(10, 10), true, false);
9301 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9302 EXPECT_EQ(gfx::Rect(10, 10), child->visible_rect_from_property_trees());
9304 child->SetTransformOrigin(gfx::Point3F(10.f, 10.f, 10.f));
9306 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9307 EXPECT_EQ(gfx::Rect(5, 5, 5, 5), child->visible_rect_from_property_trees());
9310 TEST_F(LayerTreeHostCommonTest, UpdateScrollChildPosition) {
9311 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9312 scoped_refptr<LayerWithForcedDrawsContent> scroll_parent =
9313 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9314 scoped_refptr<LayerWithForcedDrawsContent> scroll_child =
9315 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9317 root->AddChild(scroll_child);
9318 root->AddChild(scroll_parent);
9319 scroll_child->SetScrollParent(scroll_parent.get());
9320 scroll_parent->SetScrollClipLayerId(root->id());
9322 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9323 host->SetRootLayer(root);
9325 gfx::Transform identity_transform;
9326 gfx::Transform scale;
9327 scale.Scale(2.f, 2.f);
9328 SetLayerPropertiesForTesting(root.get(), identity_transform, gfx::Point3F(),
9329 gfx::PointF(), gfx::Size(50, 50), true, false);
9330 SetLayerPropertiesForTesting(scroll_child.get(), scale, gfx::Point3F(),
9331 gfx::PointF(), gfx::Size(40, 40), true, false);
9332 SetLayerPropertiesForTesting(scroll_parent.get(), identity_transform,
9333 gfx::Point3F(), gfx::PointF(), gfx::Size(30, 30),
9334 true, false);
9336 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9337 EXPECT_EQ(gfx::Rect(25, 25),
9338 scroll_child->visible_rect_from_property_trees());
9340 scroll_child->SetPosition(gfx::PointF(0, -10.f));
9341 scroll_parent->SetScrollOffset(gfx::ScrollOffset(0.f, 10.f));
9342 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9343 EXPECT_EQ(gfx::Rect(0, 5, 25, 25),
9344 scroll_child->visible_rect_from_property_trees());
9347 static void CopyOutputCallback(scoped_ptr<CopyOutputResult> result) {
9350 TEST_F(LayerTreeHostCommonTest, SkippingSubtreeMain) {
9351 gfx::Transform identity;
9352 FakeContentLayerClient client;
9353 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9354 scoped_refptr<LayerWithForcedDrawsContent> child =
9355 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9356 scoped_refptr<LayerWithForcedDrawsContent> grandchild =
9357 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9358 scoped_refptr<FakeContentLayer> greatgrandchild(
9359 FakeContentLayer::Create(layer_settings(), &client));
9360 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9361 gfx::PointF(), gfx::Size(100, 100), true, false);
9362 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9363 gfx::PointF(), gfx::Size(10, 10), true, false);
9364 SetLayerPropertiesForTesting(grandchild.get(), identity, gfx::Point3F(),
9365 gfx::PointF(), gfx::Size(10, 10), true, false);
9366 SetLayerPropertiesForTesting(greatgrandchild.get(), identity, gfx::Point3F(),
9367 gfx::PointF(), gfx::Size(10, 10), true, false);
9369 root->AddChild(child);
9370 child->AddChild(grandchild);
9371 grandchild->AddChild(greatgrandchild);
9373 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9374 host->SetRootLayer(root);
9376 // Check the non-skipped case.
9377 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9378 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
9380 // Now we will reset the visible rect from property trees for the grandchild,
9381 // and we will configure |child| in several ways that should force the subtree
9382 // to be skipped. The visible content rect for |grandchild| should, therefore,
9383 // remain empty.
9384 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
9385 gfx::Transform singular;
9386 singular.matrix().set(0, 0, 0);
9388 child->SetTransform(singular);
9389 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9390 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9391 child->SetTransform(identity);
9393 child->SetHideLayerAndSubtree(true);
9394 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9395 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9396 child->SetHideLayerAndSubtree(false);
9398 child->SetOpacity(0.f);
9399 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9400 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9402 // Now, even though child has zero opacity, we will configure |grandchild| and
9403 // |greatgrandchild| in several ways that should force the subtree to be
9404 // processed anyhow.
9405 grandchild->SetTouchEventHandlerRegion(Region(gfx::Rect(0, 0, 10, 10)));
9406 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9407 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
9408 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
9409 grandchild->SetTouchEventHandlerRegion(Region());
9410 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9411 EXPECT_EQ(gfx::Rect(0, 0), grandchild->visible_rect_from_property_trees());
9412 grandchild->set_visible_rect_from_property_trees(gfx::Rect());
9414 greatgrandchild->RequestCopyOfOutput(
9415 CopyOutputRequest::CreateBitmapRequest(base::Bind(&CopyOutputCallback)));
9416 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9417 EXPECT_EQ(gfx::Rect(10, 10), grandchild->visible_rect_from_property_trees());
9420 TEST_F(LayerTreeHostCommonTest, SkippingSubtreeImpl) {
9421 FakeImplProxy proxy;
9422 TestSharedBitmapManager shared_bitmap_manager;
9423 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager, nullptr);
9425 gfx::Transform identity;
9426 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
9427 scoped_ptr<LayerImpl> child = LayerImpl::Create(host_impl.active_tree(), 2);
9428 scoped_ptr<LayerImpl> grandchild =
9429 LayerImpl::Create(host_impl.active_tree(), 3);
9431 scoped_ptr<FakeContentLayerImpl> greatgrandchild(
9432 FakeContentLayerImpl::Create(host_impl.active_tree(), 4));
9434 child->SetDrawsContent(true);
9435 grandchild->SetDrawsContent(true);
9436 greatgrandchild->SetDrawsContent(true);
9438 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9439 gfx::PointF(), gfx::Size(100, 100), true, false,
9440 true);
9441 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9442 gfx::PointF(), gfx::Size(10, 10), true, false,
9443 false);
9444 SetLayerPropertiesForTesting(grandchild.get(), identity, gfx::Point3F(),
9445 gfx::PointF(), gfx::Size(10, 10), true, false,
9446 false);
9447 SetLayerPropertiesForTesting(greatgrandchild.get(), identity, gfx::Point3F(),
9448 gfx::PointF(), gfx::Size(10, 10), true, false,
9449 true);
9451 LayerImpl* child_ptr = child.get();
9452 LayerImpl* grandchild_ptr = grandchild.get();
9453 LayerImpl* greatgrandchild_ptr = greatgrandchild.get();
9455 grandchild->AddChild(greatgrandchild.Pass());
9456 child->AddChild(grandchild.Pass());
9457 root->AddChild(child.Pass());
9459 // Check the non-skipped case.
9460 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9461 EXPECT_EQ(gfx::Rect(10, 10),
9462 grandchild_ptr->visible_rect_from_property_trees());
9464 // Now we will reset the visible rect from property trees for the grandchild,
9465 // and we will configure |child| in several ways that should force the subtree
9466 // to be skipped. The visible content rect for |grandchild| should, therefore,
9467 // remain empty.
9468 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
9469 gfx::Transform singular;
9470 singular.matrix().set(0, 0, 0);
9472 child_ptr->SetTransform(singular);
9473 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9474 EXPECT_EQ(gfx::Rect(0, 0),
9475 grandchild_ptr->visible_rect_from_property_trees());
9476 child_ptr->SetTransform(identity);
9478 child_ptr->SetHideLayerAndSubtree(true);
9479 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9480 EXPECT_EQ(gfx::Rect(0, 0),
9481 grandchild_ptr->visible_rect_from_property_trees());
9482 child_ptr->SetHideLayerAndSubtree(false);
9484 child_ptr->SetOpacity(0.f);
9485 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9486 EXPECT_EQ(gfx::Rect(0, 0),
9487 grandchild_ptr->visible_rect_from_property_trees());
9489 // Now, even though child has zero opacity, we will configure |grandchild| and
9490 // |greatgrandchild| in several ways that should force the subtree to be
9491 // processed anyhow.
9492 grandchild_ptr->SetTouchEventHandlerRegion(Region(gfx::Rect(0, 0, 10, 10)));
9493 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9494 EXPECT_EQ(gfx::Rect(10, 10),
9495 grandchild_ptr->visible_rect_from_property_trees());
9496 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
9497 grandchild_ptr->SetTouchEventHandlerRegion(Region());
9498 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9499 EXPECT_EQ(gfx::Rect(0, 0),
9500 grandchild_ptr->visible_rect_from_property_trees());
9501 grandchild_ptr->set_visible_rect_from_property_trees(gfx::Rect());
9503 ScopedPtrVector<CopyOutputRequest> requests;
9504 requests.push_back(CopyOutputRequest::CreateEmptyRequest());
9506 greatgrandchild_ptr->PassCopyRequests(&requests);
9507 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9508 EXPECT_EQ(gfx::Rect(10, 10),
9509 grandchild_ptr->visible_rect_from_property_trees());
9512 TEST_F(LayerTreeHostCommonTest, SkippingLayer) {
9513 gfx::Transform identity;
9514 FakeContentLayerClient client;
9515 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9516 scoped_refptr<LayerWithForcedDrawsContent> child =
9517 make_scoped_refptr(new LayerWithForcedDrawsContent(layer_settings()));
9518 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9519 gfx::PointF(), gfx::Size(100, 100), true, false);
9520 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9521 gfx::PointF(), gfx::Size(10, 10), true, false);
9522 root->AddChild(child);
9524 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9525 host->SetRootLayer(root);
9527 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9528 EXPECT_EQ(gfx::Rect(10, 10), child->visible_rect_from_property_trees());
9529 child->set_visible_rect_from_property_trees(gfx::Rect());
9531 child->SetHideLayerAndSubtree(true);
9532 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9533 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9534 child->SetHideLayerAndSubtree(false);
9536 child->SetBounds(gfx::Size());
9537 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9538 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9539 child->SetBounds(gfx::Size(10, 10));
9541 gfx::Transform rotate;
9542 child->SetDoubleSided(false);
9543 rotate.RotateAboutXAxis(180.f);
9544 child->SetTransform(rotate);
9545 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9546 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9547 child->SetDoubleSided(true);
9548 child->SetTransform(identity);
9550 child->SetOpacity(0.f);
9551 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9552 EXPECT_EQ(gfx::Rect(0, 0), child->visible_rect_from_property_trees());
9555 TEST_F(LayerTreeHostCommonTest, LayerTreeRebuildTest) {
9556 // Ensure that the treewalk in LayerTreeHostCommom::
9557 // PreCalculateMetaInformation happens when its required.
9558 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9559 scoped_refptr<Layer> parent = Layer::Create(layer_settings());
9560 scoped_refptr<Layer> child = Layer::Create(layer_settings());
9562 root->AddChild(parent);
9563 parent->AddChild(child);
9565 child->SetClipParent(root.get());
9567 gfx::Transform identity;
9569 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9570 gfx::PointF(), gfx::Size(100, 100), true, false);
9571 SetLayerPropertiesForTesting(parent.get(), identity, gfx::Point3F(),
9572 gfx::PointF(), gfx::Size(100, 100), true, false);
9573 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9574 gfx::PointF(), gfx::Size(100, 100), true, false);
9576 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9577 host->SetRootLayer(root);
9579 ExecuteCalculateDrawProperties(root.get());
9580 EXPECT_EQ(parent->draw_properties().num_unclipped_descendants, 1);
9582 // Ensure the dynamic update to input handlers happens.
9583 child->SetHaveWheelEventHandlers(true);
9584 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_input_handler);
9585 ExecuteCalculateDrawProperties(root.get());
9586 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_input_handler);
9588 child->SetHaveWheelEventHandlers(false);
9589 EXPECT_FALSE(root->draw_properties().layer_or_descendant_has_input_handler);
9590 ExecuteCalculateDrawProperties(root.get());
9591 EXPECT_FALSE(root->draw_properties().layer_or_descendant_has_input_handler);
9593 child->RequestCopyOfOutput(
9594 CopyOutputRequest::CreateRequest(base::Bind(&EmptyCopyOutputCallback)));
9595 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
9596 ExecuteCalculateDrawProperties(root.get());
9597 EXPECT_TRUE(root->draw_properties().layer_or_descendant_has_copy_request);
9600 TEST_F(LayerTreeHostCommonTest, InputHandlersRecursiveUpdateTest) {
9601 // Ensure that the treewalk in LayertreeHostCommon::
9602 // PreCalculateMetaInformation updates input handlers correctly.
9603 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9604 scoped_refptr<Layer> child = Layer::Create(layer_settings());
9606 root->AddChild(child);
9608 child->SetHaveWheelEventHandlers(true);
9610 gfx::Transform identity;
9612 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9613 gfx::PointF(), gfx::Size(100, 100), true, false);
9614 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9615 gfx::PointF(), gfx::Size(100, 100), true, false);
9617 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9618 host->SetRootLayer(root);
9620 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 0);
9621 ExecuteCalculateDrawProperties(root.get());
9622 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 1);
9623 child->SetHaveWheelEventHandlers(false);
9624 EXPECT_EQ(root->num_layer_or_descendants_with_input_handler(), 0);
9627 TEST_F(LayerTreeHostCommonTest, ResetPropertyTreeIndices) {
9628 gfx::Transform identity;
9629 gfx::Transform translate_z;
9630 translate_z.Translate3d(0, 0, 10);
9632 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9633 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9634 gfx::PointF(), gfx::Size(800, 800), true, false);
9636 scoped_refptr<Layer> child = Layer::Create(layer_settings());
9637 SetLayerPropertiesForTesting(child.get(), translate_z, gfx::Point3F(),
9638 gfx::PointF(), gfx::Size(100, 100), true, false);
9640 root->AddChild(child);
9642 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9643 host->SetRootLayer(root);
9645 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9646 EXPECT_NE(-1, child->transform_tree_index());
9648 child->RemoveFromParent();
9650 ExecuteCalculateDrawPropertiesWithPropertyTrees(root.get());
9651 EXPECT_EQ(-1, child->transform_tree_index());
9654 TEST_F(LayerTreeHostCommonTest, ResetLayerDrawPropertiestest) {
9655 scoped_refptr<Layer> root = Layer::Create(layer_settings());
9656 scoped_refptr<Layer> child = Layer::Create(layer_settings());
9658 root->AddChild(child);
9659 gfx::Transform identity;
9661 SetLayerPropertiesForTesting(root.get(), identity, gfx::Point3F(),
9662 gfx::PointF(), gfx::Size(100, 100), true, false);
9663 SetLayerPropertiesForTesting(child.get(), identity, gfx::Point3F(),
9664 gfx::PointF(), gfx::Size(100, 100), true, false);
9666 scoped_ptr<FakeLayerTreeHost> host(CreateFakeLayerTreeHost());
9667 host->SetRootLayer(root);
9669 EXPECT_FALSE(root->layer_or_descendant_is_drawn());
9670 EXPECT_FALSE(root->visited());
9671 EXPECT_FALSE(root->sorted_for_recursion());
9672 EXPECT_FALSE(child->layer_or_descendant_is_drawn());
9673 EXPECT_FALSE(child->visited());
9674 EXPECT_FALSE(child->sorted_for_recursion());
9676 root->set_layer_or_descendant_is_drawn(true);
9677 root->set_visited(true);
9678 root->set_sorted_for_recursion(true);
9679 child->set_layer_or_descendant_is_drawn(true);
9680 child->set_visited(true);
9681 child->set_sorted_for_recursion(true);
9683 LayerTreeHostCommon::PreCalculateMetaInformationForTesting(root.get());
9685 EXPECT_FALSE(root->layer_or_descendant_is_drawn());
9686 EXPECT_FALSE(root->visited());
9687 EXPECT_FALSE(root->sorted_for_recursion());
9688 EXPECT_FALSE(child->layer_or_descendant_is_drawn());
9689 EXPECT_FALSE(child->visited());
9690 EXPECT_FALSE(child->sorted_for_recursion());
9693 } // namespace
9694 } // namespace cc