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