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