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