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