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