1// Copyright 2013 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/layers/picture_layer_impl.h"
6
7#include <algorithm>
8#include <limits>
9#include <set>
10#include <utility>
11
12#include "cc/base/math_util.h"
13#include "cc/layers/append_quads_data.h"
14#include "cc/layers/picture_layer.h"
15#include "cc/test/fake_content_layer_client.h"
16#include "cc/test/fake_impl_proxy.h"
17#include "cc/test/fake_layer_tree_host_impl.h"
18#include "cc/test/fake_output_surface.h"
19#include "cc/test/fake_picture_layer_impl.h"
20#include "cc/test/fake_picture_pile_impl.h"
21#include "cc/test/geometry_test_utils.h"
22#include "cc/test/impl_side_painting_settings.h"
23#include "cc/test/layer_test_common.h"
24#include "cc/test/mock_quad_culler.h"
25#include "cc/test/test_shared_bitmap_manager.h"
26#include "cc/test/test_web_graphics_context_3d.h"
27#include "cc/trees/layer_tree_impl.h"
28#include "testing/gtest/include/gtest/gtest.h"
29#include "ui/gfx/rect_conversions.h"
30#include "ui/gfx/size_conversions.h"
31
32namespace cc {
33namespace {
34
35class MockCanvas : public SkCanvas {
36 public:
37  explicit MockCanvas(int w, int h) : SkCanvas(w, h) {}
38
39  virtual void drawRect(const SkRect& rect, const SkPaint& paint) OVERRIDE {
40    // Capture calls before SkCanvas quickReject() kicks in.
41    rects_.push_back(rect);
42  }
43
44  std::vector<SkRect> rects_;
45};
46
47class PictureLayerImplTest : public testing::Test {
48 public:
49  PictureLayerImplTest()
50      : proxy_(base::MessageLoopProxy::current()),
51        host_impl_(ImplSidePaintingSettings(),
52                   &proxy_,
53                   &shared_bitmap_manager_),
54        id_(7),
55        pending_layer_(NULL),
56        old_pending_layer_(NULL),
57        active_layer_(NULL) {}
58
59  explicit PictureLayerImplTest(const LayerTreeSettings& settings)
60      : proxy_(base::MessageLoopProxy::current()),
61        host_impl_(settings, &proxy_, &shared_bitmap_manager_),
62        id_(7) {}
63
64  virtual ~PictureLayerImplTest() {
65  }
66
67  virtual void SetUp() OVERRIDE {
68    InitializeRenderer();
69  }
70
71  virtual void InitializeRenderer() {
72    host_impl_.InitializeRenderer(
73        FakeOutputSurface::Create3d().PassAs<OutputSurface>());
74  }
75
76  void SetupDefaultTrees(const gfx::Size& layer_bounds) {
77    gfx::Size tile_size(100, 100);
78
79    scoped_refptr<FakePicturePileImpl> pending_pile =
80        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
81    scoped_refptr<FakePicturePileImpl> active_pile =
82        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
83
84    SetupTrees(pending_pile, active_pile);
85  }
86
87  void ActivateTree() {
88    host_impl_.ActivatePendingTree();
89    CHECK(!host_impl_.pending_tree());
90    CHECK(host_impl_.recycle_tree());
91    old_pending_layer_ = pending_layer_;
92    pending_layer_ = NULL;
93    active_layer_ = static_cast<FakePictureLayerImpl*>(
94        host_impl_.active_tree()->LayerById(id_));
95  }
96
97  void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
98                                          const gfx::Size& tile_size) {
99    SetupDefaultTrees(layer_bounds);
100    pending_layer_->set_fixed_tile_size(tile_size);
101    active_layer_->set_fixed_tile_size(tile_size);
102  }
103
104  void SetupTrees(
105      scoped_refptr<PicturePileImpl> pending_pile,
106      scoped_refptr<PicturePileImpl> active_pile) {
107    SetupPendingTree(active_pile);
108    ActivateTree();
109    SetupPendingTree(pending_pile);
110    host_impl_.pending_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
111    host_impl_.active_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
112  }
113
114  void CreateHighLowResAndSetAllTilesVisible() {
115    // Active layer must get updated first so pending layer can share from it.
116    active_layer_->CreateDefaultTilingsAndTiles();
117    active_layer_->SetAllTilesVisible();
118    pending_layer_->CreateDefaultTilingsAndTiles();
119    pending_layer_->SetAllTilesVisible();
120  }
121
122  void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
123    active_layer_->AddTiling(2.3f);
124    active_layer_->AddTiling(1.0f);
125    active_layer_->AddTiling(0.5f);
126    for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
127      active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
128    pending_layer_->set_invalidation(invalidation);
129    for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
130      pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
131  }
132
133  void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
134    host_impl_.CreatePendingTree();
135    LayerTreeImpl* pending_tree = host_impl_.pending_tree();
136    // Clear recycled tree.
137    pending_tree->DetachLayerTree();
138
139    scoped_ptr<FakePictureLayerImpl> pending_layer =
140        FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
141    pending_layer->SetDrawsContent(true);
142    pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
143
144    pending_layer_ = static_cast<FakePictureLayerImpl*>(
145        host_impl_.pending_tree()->LayerById(id_));
146    pending_layer_->DoPostCommitInitializationIfNeeded();
147  }
148
149  void SetupDrawPropertiesAndUpdateTiles(FakePictureLayerImpl* layer,
150                                         float ideal_contents_scale,
151                                         float device_scale_factor,
152                                         float page_scale_factor,
153                                         float maximum_animation_contents_scale,
154                                         bool animating_transform_to_screen) {
155    layer->draw_properties().ideal_contents_scale = ideal_contents_scale;
156    layer->draw_properties().device_scale_factor = device_scale_factor;
157    layer->draw_properties().page_scale_factor = page_scale_factor;
158    layer->draw_properties().maximum_animation_contents_scale =
159        maximum_animation_contents_scale;
160    layer->draw_properties().screen_space_transform_is_animating =
161        animating_transform_to_screen;
162    layer->UpdateTiles();
163  }
164  static void VerifyAllTilesExistAndHavePile(
165      const PictureLayerTiling* tiling,
166      PicturePileImpl* pile) {
167    for (PictureLayerTiling::CoverageIterator iter(
168             tiling, tiling->contents_scale(), tiling->TilingRect());
169         iter;
170         ++iter) {
171      EXPECT_TRUE(*iter);
172      EXPECT_EQ(pile, iter->picture_pile());
173    }
174  }
175
176  void SetContentsScaleOnBothLayers(float contents_scale,
177                                    float device_scale_factor,
178                                    float page_scale_factor,
179                                    float maximum_animation_contents_scale,
180                                    bool animating_transform) {
181    SetupDrawPropertiesAndUpdateTiles(pending_layer_,
182                                      contents_scale,
183                                      device_scale_factor,
184                                      page_scale_factor,
185                                      maximum_animation_contents_scale,
186                                      animating_transform);
187
188    SetupDrawPropertiesAndUpdateTiles(active_layer_,
189                                      contents_scale,
190                                      device_scale_factor,
191                                      page_scale_factor,
192                                      maximum_animation_contents_scale,
193                                      animating_transform);
194  }
195
196  void ResetTilingsAndRasterScales() {
197    pending_layer_->ReleaseResources();
198    active_layer_->ReleaseResources();
199  }
200
201  void AssertAllTilesRequired(PictureLayerTiling* tiling) {
202    std::vector<Tile*> tiles = tiling->AllTilesForTesting();
203    for (size_t i = 0; i < tiles.size(); ++i)
204      EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
205    EXPECT_GT(tiles.size(), 0u);
206  }
207
208  void AssertNoTilesRequired(PictureLayerTiling* tiling) {
209    std::vector<Tile*> tiles = tiling->AllTilesForTesting();
210    for (size_t i = 0; i < tiles.size(); ++i)
211      EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
212    EXPECT_GT(tiles.size(), 0u);
213  }
214
215 protected:
216  void TestTileGridAlignmentCommon() {
217    // Layer to span 4 raster tiles in x and in y
218    ImplSidePaintingSettings settings;
219    gfx::Size layer_size(
220        settings.default_tile_size.width() * 7 / 2,
221        settings.default_tile_size.height() * 7 / 2);
222
223    scoped_refptr<FakePicturePileImpl> pending_pile =
224        FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
225    scoped_refptr<FakePicturePileImpl> active_pile =
226        FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
227
228    SetupTrees(pending_pile, active_pile);
229
230    SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
231
232    // Add 1x1 rects at the centers of each tile, then re-record pile contents
233    active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
234    std::vector<Tile*> tiles =
235        active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
236    EXPECT_EQ(16u, tiles.size());
237    std::vector<SkRect> rects;
238    std::vector<Tile*>::const_iterator tile_iter;
239    for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
240      gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
241      gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
242      active_pile->add_draw_rect(rect);
243      rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
244    }
245    // Force re-record with newly injected content
246    active_pile->RemoveRecordingAt(0, 0);
247    active_pile->AddRecordingAt(0, 0);
248
249    std::vector<SkRect>::const_iterator rect_iter = rects.begin();
250    for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
251      MockCanvas mock_canvas(1000, 1000);
252      active_pile->RasterDirect(
253          &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
254
255      // This test verifies that when drawing the contents of a specific tile
256      // at content scale 1.0, the playback canvas never receives content from
257      // neighboring tiles which indicates that the tile grid embedded in
258      // SkPicture is perfectly aligned with the compositor's tiles.
259      EXPECT_EQ(1u, mock_canvas.rects_.size());
260      EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]);
261      rect_iter++;
262    }
263  }
264
265  FakeImplProxy proxy_;
266  TestSharedBitmapManager shared_bitmap_manager_;
267  FakeLayerTreeHostImpl host_impl_;
268  int id_;
269  FakePictureLayerImpl* pending_layer_;
270  FakePictureLayerImpl* old_pending_layer_;
271  FakePictureLayerImpl* active_layer_;
272
273 private:
274  DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
275};
276
277TEST_F(PictureLayerImplTest, TileGridAlignment) {
278  host_impl_.SetDeviceScaleFactor(1.f);
279  TestTileGridAlignmentCommon();
280}
281
282TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
283  host_impl_.SetDeviceScaleFactor(2.f);
284  TestTileGridAlignmentCommon();
285}
286
287TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
288  gfx::Size tile_size(100, 100);
289  gfx::Size layer_bounds(400, 400);
290
291  scoped_refptr<FakePicturePileImpl> pending_pile =
292      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
293  scoped_refptr<FakePicturePileImpl> active_pile =
294      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
295
296  SetupTrees(pending_pile, active_pile);
297
298  Region invalidation;
299  AddDefaultTilingsWithInvalidation(invalidation);
300
301  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
302            active_layer_->tilings()->num_tilings());
303
304  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
305  EXPECT_GT(tilings->num_tilings(), 0u);
306  for (size_t i = 0; i < tilings->num_tilings(); ++i)
307    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
308}
309
310TEST_F(PictureLayerImplTest, ExternalViewportRectForPrioritizingTiles) {
311  base::TimeTicks time_ticks;
312  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
313  gfx::Size tile_size(100, 100);
314  gfx::Size layer_bounds(400, 400);
315
316  scoped_refptr<FakePicturePileImpl> pending_pile =
317      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
318  scoped_refptr<FakePicturePileImpl> active_pile =
319      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
320
321  SetupTrees(pending_pile, active_pile);
322
323  Region invalidation;
324  AddDefaultTilingsWithInvalidation(invalidation);
325  SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
326
327  // Update tiles with viewport for tile priority as (0, 0, 100, 100) and the
328  // identify transform for tile priority.
329  bool resourceless_software_draw = false;
330  gfx::Rect viewport = gfx::Rect(layer_bounds),
331            viewport_rect_for_tile_priority = gfx::Rect(0, 0, 100, 100);
332  gfx::Transform transform, transform_for_tile_priority;
333
334  host_impl_.SetExternalDrawConstraints(transform,
335                                        viewport,
336                                        viewport,
337                                        viewport_rect_for_tile_priority,
338                                        transform_for_tile_priority,
339                                        resourceless_software_draw);
340  active_layer_->draw_properties().visible_content_rect = viewport;
341  active_layer_->draw_properties().screen_space_transform = transform;
342  active_layer_->UpdateTiles();
343
344  gfx::Rect viewport_rect_for_tile_priority_in_view_space =
345      viewport_rect_for_tile_priority;
346
347  // Verify the viewport rect for tile priority is used in picture layer impl.
348  EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
349            viewport_rect_for_tile_priority_in_view_space);
350
351  // Verify the viewport rect for tile priority is used in picture layer tiling.
352  PictureLayerTilingSet* tilings = active_layer_->tilings();
353  for (size_t i = 0; i < tilings->num_tilings(); i++) {
354    PictureLayerTiling* tiling = tilings->tiling_at(i);
355    EXPECT_EQ(
356        tiling->GetCurrentVisibleRectForTesting(),
357        gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
358                                  tiling->contents_scale()));
359  }
360
361  // Update tiles with viewport for tile priority as (200, 200, 100, 100) in
362  // screen space and the transform for tile priority is translated and
363  // rotated. The actual viewport for tile priority used by PictureLayerImpl
364  // should be (200, 200, 100, 100) applied with the said transform.
365  time_ticks += base::TimeDelta::FromMilliseconds(200);
366  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
367
368  viewport_rect_for_tile_priority = gfx::Rect(200, 200, 100, 100);
369  transform_for_tile_priority.Translate(100, 100);
370  transform_for_tile_priority.Rotate(45);
371  host_impl_.SetExternalDrawConstraints(transform,
372                                        viewport,
373                                        viewport,
374                                        viewport_rect_for_tile_priority,
375                                        transform_for_tile_priority,
376                                        resourceless_software_draw);
377  active_layer_->draw_properties().visible_content_rect = viewport;
378  active_layer_->draw_properties().screen_space_transform = transform;
379  active_layer_->UpdateTiles();
380
381  gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
382  bool success = transform_for_tile_priority.GetInverse(&screen_to_view);
383  EXPECT_TRUE(success);
384
385  viewport_rect_for_tile_priority_in_view_space =
386      gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
387          screen_to_view, viewport_rect_for_tile_priority));
388
389  // Verify the viewport rect for tile priority is used in PictureLayerImpl.
390  EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
391            viewport_rect_for_tile_priority_in_view_space);
392
393  // Interset viewport_rect_for_tile_priority_in_view_space with the layer
394  // bounds and the result should be used in PictureLayerTiling.
395  viewport_rect_for_tile_priority_in_view_space.Intersect(
396      gfx::Rect(layer_bounds));
397  tilings = active_layer_->tilings();
398  for (size_t i = 0; i < tilings->num_tilings(); i++) {
399    PictureLayerTiling* tiling = tilings->tiling_at(i);
400    EXPECT_EQ(
401        tiling->GetCurrentVisibleRectForTesting(),
402        gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
403                                  tiling->contents_scale()));
404  }
405}
406
407TEST_F(PictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
408  base::TimeTicks time_ticks;
409  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
410
411  gfx::Size tile_size(100, 100);
412  gfx::Size layer_bounds(400, 400);
413
414  scoped_refptr<FakePicturePileImpl> pending_pile =
415      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
416  scoped_refptr<FakePicturePileImpl> active_pile =
417      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
418
419  SetupTrees(pending_pile, active_pile);
420
421  Region invalidation;
422  AddDefaultTilingsWithInvalidation(invalidation);
423  SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
424
425  // UpdateTiles with valid viewport. Should update tile viewport.
426  // Note viewport is considered invalid if and only if in resourceless
427  // software draw.
428  bool resourceless_software_draw = false;
429  gfx::Rect viewport = gfx::Rect(layer_bounds);
430  gfx::Transform transform;
431  host_impl_.SetExternalDrawConstraints(transform,
432                                        viewport,
433                                        viewport,
434                                        viewport,
435                                        transform,
436                                        resourceless_software_draw);
437  active_layer_->draw_properties().visible_content_rect = viewport;
438  active_layer_->draw_properties().screen_space_transform = transform;
439  active_layer_->UpdateTiles();
440
441  gfx::Rect visible_rect_for_tile_priority =
442      active_layer_->visible_rect_for_tile_priority();
443  EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
444  gfx::Rect viewport_rect_for_tile_priority =
445      active_layer_->viewport_rect_for_tile_priority();
446  EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
447  gfx::Transform screen_space_transform_for_tile_priority =
448      active_layer_->screen_space_transform_for_tile_priority();
449
450  // Expand viewport and set it as invalid for prioritizing tiles.
451  // Should not update tile viewport.
452  time_ticks += base::TimeDelta::FromMilliseconds(200);
453  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
454  resourceless_software_draw = true;
455  viewport = gfx::ScaleToEnclosingRect(viewport, 2);
456  transform.Translate(1.f, 1.f);
457  active_layer_->draw_properties().visible_content_rect = viewport;
458  active_layer_->draw_properties().screen_space_transform = transform;
459  host_impl_.SetExternalDrawConstraints(transform,
460                                        viewport,
461                                        viewport,
462                                        viewport,
463                                        transform,
464                                        resourceless_software_draw);
465  active_layer_->UpdateTiles();
466
467  EXPECT_RECT_EQ(visible_rect_for_tile_priority,
468                 active_layer_->visible_rect_for_tile_priority());
469  EXPECT_RECT_EQ(viewport_rect_for_tile_priority,
470                 active_layer_->viewport_rect_for_tile_priority());
471  EXPECT_TRANSFORMATION_MATRIX_EQ(
472      screen_space_transform_for_tile_priority,
473      active_layer_->screen_space_transform_for_tile_priority());
474
475  // Keep expanded viewport but mark it valid. Should update tile viewport.
476  time_ticks += base::TimeDelta::FromMilliseconds(200);
477  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
478  resourceless_software_draw = false;
479  host_impl_.SetExternalDrawConstraints(transform,
480                                        viewport,
481                                        viewport,
482                                        viewport,
483                                        transform,
484                                        resourceless_software_draw);
485  active_layer_->UpdateTiles();
486
487  EXPECT_FALSE(visible_rect_for_tile_priority ==
488               active_layer_->visible_rect_for_tile_priority());
489  EXPECT_FALSE(viewport_rect_for_tile_priority ==
490               active_layer_->viewport_rect_for_tile_priority());
491  EXPECT_FALSE(screen_space_transform_for_tile_priority ==
492               active_layer_->screen_space_transform_for_tile_priority());
493}
494
495TEST_F(PictureLayerImplTest, InvalidViewportAfterReleaseResources) {
496  base::TimeTicks time_ticks;
497  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
498
499  gfx::Size tile_size(100, 100);
500  gfx::Size layer_bounds(400, 400);
501
502  scoped_refptr<FakePicturePileImpl> pending_pile =
503      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
504  scoped_refptr<FakePicturePileImpl> active_pile =
505      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
506
507  SetupTrees(pending_pile, active_pile);
508
509  Region invalidation;
510  AddDefaultTilingsWithInvalidation(invalidation);
511
512  bool resourceless_software_draw = true;
513  gfx::Rect viewport = gfx::Rect(layer_bounds);
514  gfx::Transform identity = gfx::Transform();
515  host_impl_.SetExternalDrawConstraints(identity,
516                                        viewport,
517                                        viewport,
518                                        viewport,
519                                        identity,
520                                        resourceless_software_draw);
521  ResetTilingsAndRasterScales();
522  host_impl_.pending_tree()->UpdateDrawProperties();
523  host_impl_.active_tree()->UpdateDrawProperties();
524  EXPECT_TRUE(active_layer_->HighResTiling());
525
526  size_t num_tilings = active_layer_->num_tilings();
527  active_layer_->UpdateTiles();
528  pending_layer_->AddTiling(0.5f);
529  EXPECT_EQ(num_tilings + 1, active_layer_->num_tilings());
530}
531
532TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
533  gfx::Size tile_size(100, 100);
534  gfx::Size layer_bounds(400, 400);
535  gfx::Rect layer_invalidation(150, 200, 30, 180);
536
537  scoped_refptr<FakePicturePileImpl> pending_pile =
538      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
539  scoped_refptr<FakePicturePileImpl> active_pile =
540      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
541
542  SetupTrees(pending_pile, active_pile);
543
544  Region invalidation(layer_invalidation);
545  AddDefaultTilingsWithInvalidation(invalidation);
546
547  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
548  EXPECT_GT(tilings->num_tilings(), 0u);
549  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
550    const PictureLayerTiling* tiling = tilings->tiling_at(i);
551    gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
552        layer_invalidation,
553        tiling->contents_scale());
554    for (PictureLayerTiling::CoverageIterator iter(
555             tiling, tiling->contents_scale(), tiling->TilingRect());
556         iter;
557         ++iter) {
558      EXPECT_TRUE(*iter);
559      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
560      if (iter.geometry_rect().Intersects(content_invalidation))
561        EXPECT_EQ(pending_pile, iter->picture_pile());
562      else
563        EXPECT_EQ(active_pile, iter->picture_pile());
564    }
565  }
566}
567
568TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
569  gfx::Size tile_size(90, 80);
570  gfx::Size layer_bounds(300, 500);
571
572  scoped_refptr<FakePicturePileImpl> pending_pile =
573      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
574  scoped_refptr<FakePicturePileImpl> active_pile =
575      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
576
577  SetupTrees(pending_pile, active_pile);
578
579  Region invalidation((gfx::Rect(layer_bounds)));
580  AddDefaultTilingsWithInvalidation(invalidation);
581
582  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
583            active_layer_->tilings()->num_tilings());
584
585  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
586  EXPECT_GT(tilings->num_tilings(), 0u);
587  for (size_t i = 0; i < tilings->num_tilings(); ++i)
588    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
589}
590
591TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
592  gfx::Size tile_size(90, 80);
593  gfx::Size active_layer_bounds(300, 500);
594  gfx::Size pending_layer_bounds(400, 800);
595
596  scoped_refptr<FakePicturePileImpl> pending_pile =
597      FakePicturePileImpl::CreateFilledPile(tile_size,
598                                                pending_layer_bounds);
599  scoped_refptr<FakePicturePileImpl> active_pile =
600      FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
601
602  SetupTrees(pending_pile, active_pile);
603  pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
604
605  Region invalidation;
606  AddDefaultTilingsWithInvalidation(invalidation);
607
608  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
609  EXPECT_GT(tilings->num_tilings(), 0u);
610  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
611    const PictureLayerTiling* tiling = tilings->tiling_at(i);
612    gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
613        gfx::Rect(active_layer_bounds),
614        tiling->contents_scale());
615    for (PictureLayerTiling::CoverageIterator iter(
616             tiling, tiling->contents_scale(), tiling->TilingRect());
617         iter;
618         ++iter) {
619      EXPECT_TRUE(*iter);
620      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
621      std::vector<Tile*> active_tiles =
622          active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
623      std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
624      if (iter.geometry_rect().right() >= active_content_bounds.width() ||
625          iter.geometry_rect().bottom() >= active_content_bounds.height() ||
626          active_tiles[0]->content_rect().size() !=
627              pending_tiles[0]->content_rect().size()) {
628        EXPECT_EQ(pending_pile, iter->picture_pile());
629      } else {
630        EXPECT_EQ(active_pile, iter->picture_pile());
631      }
632    }
633  }
634}
635
636TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
637  gfx::Size tile_size(400, 400);
638  gfx::Size layer_bounds(1300, 1900);
639
640  scoped_refptr<FakePicturePileImpl> pending_pile =
641      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
642  scoped_refptr<FakePicturePileImpl> active_pile =
643      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
644
645  // Fill in some of active pile, but more of pending pile.
646  int hole_count = 0;
647  for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
648    for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
649      if ((x + y) % 2) {
650        pending_pile->AddRecordingAt(x, y);
651        active_pile->AddRecordingAt(x, y);
652      } else {
653        hole_count++;
654        if (hole_count % 2)
655          pending_pile->AddRecordingAt(x, y);
656      }
657    }
658  }
659
660  SetupTrees(pending_pile, active_pile);
661  Region invalidation;
662  AddDefaultTilingsWithInvalidation(invalidation);
663
664  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
665  EXPECT_GT(tilings->num_tilings(), 0u);
666  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
667    const PictureLayerTiling* tiling = tilings->tiling_at(i);
668
669    for (PictureLayerTiling::CoverageIterator iter(
670             tiling, tiling->contents_scale(), tiling->TilingRect());
671         iter;
672         ++iter) {
673      EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
674      // Ensure there is a recording for this tile.
675      bool in_pending = pending_pile->CanRaster(tiling->contents_scale(),
676                                                iter.full_tile_geometry_rect());
677      bool in_active = active_pile->CanRaster(tiling->contents_scale(),
678                                              iter.full_tile_geometry_rect());
679
680      if (in_pending && !in_active)
681        EXPECT_EQ(pending_pile, iter->picture_pile());
682      else if (in_active)
683        EXPECT_EQ(active_pile, iter->picture_pile());
684      else
685        EXPECT_FALSE(*iter);
686    }
687  }
688}
689
690TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
691  gfx::Size tile_size(400, 400);
692  gfx::Size layer_bounds(1300, 1900);
693
694  scoped_refptr<FakePicturePileImpl> pending_pile =
695      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
696  scoped_refptr<FakePicturePileImpl> active_pile =
697      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
698
699  SetupTrees(pending_pile, active_pile);
700
701  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
702
703  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
704}
705
706TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
707  gfx::Size tile_size(400, 400);
708  gfx::Size layer_bounds(1300, 1900);
709
710  scoped_refptr<FakePicturePileImpl> pending_pile =
711      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
712  scoped_refptr<FakePicturePileImpl> active_pile =
713      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
714
715  SetupTrees(pending_pile, active_pile);
716  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
717
718  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
719  EXPECT_LT(low_res_factor, 1.f);
720
721  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
722                                    6.f,  // ideal contents scale
723                                    3.f,  // device scale
724                                    2.f,  // page scale
725                                    1.f,  // maximum animation scale
726                                    false);
727  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
728  EXPECT_FLOAT_EQ(6.f,
729                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
730  EXPECT_FLOAT_EQ(6.f * low_res_factor,
731                  pending_layer_->tilings()->tiling_at(1)->contents_scale());
732
733  // If we change the page scale factor, then we should get new tilings.
734  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
735                                    6.6f,  // ideal contents scale
736                                    3.f,   // device scale
737                                    2.2f,  // page scale
738                                    1.f,   // maximum animation scale
739                                    false);
740  ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
741  EXPECT_FLOAT_EQ(6.6f,
742                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
743  EXPECT_FLOAT_EQ(6.6f * low_res_factor,
744                  pending_layer_->tilings()->tiling_at(2)->contents_scale());
745
746  // If we change the device scale factor, then we should get new tilings.
747  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
748                                    7.26f,  // ideal contents scale
749                                    3.3f,   // device scale
750                                    2.2f,   // page scale
751                                    1.f,    // maximum animation scale
752                                    false);
753  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
754  EXPECT_FLOAT_EQ(7.26f,
755                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
756  EXPECT_FLOAT_EQ(7.26f * low_res_factor,
757                  pending_layer_->tilings()->tiling_at(3)->contents_scale());
758
759  // If we change the device scale factor, but end up at the same total scale
760  // factor somehow, then we don't get new tilings.
761  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
762                                    7.26f,  // ideal contents scale
763                                    2.2f,   // device scale
764                                    3.3f,   // page scale
765                                    1.f,    // maximum animation scale
766                                    false);
767  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
768  EXPECT_FLOAT_EQ(7.26f,
769                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
770  EXPECT_FLOAT_EQ(7.26f * low_res_factor,
771                  pending_layer_->tilings()->tiling_at(3)->contents_scale());
772}
773
774TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
775  // This test makes sure that if a layer can have tilings, then a commit makes
776  // it not able to have tilings (empty size), and then a future commit that
777  // makes it valid again should be able to create tilings.
778  gfx::Size tile_size(400, 400);
779  gfx::Size layer_bounds(1300, 1900);
780
781  scoped_refptr<FakePicturePileImpl> empty_pile =
782      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
783  scoped_refptr<FakePicturePileImpl> valid_pile =
784      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
785
786  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
787  EXPECT_LT(low_res_factor, 1.f);
788
789  float high_res_scale = 1.3f;
790  float low_res_scale = high_res_scale * low_res_factor;
791  float device_scale = 1.7f;
792  float page_scale = 3.2f;
793  float maximum_animation_scale = 1.f;
794
795  SetupPendingTree(valid_pile);
796  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
797                                    high_res_scale,
798                                    device_scale,
799                                    page_scale,
800                                    maximum_animation_scale,
801                                    false);
802  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
803  EXPECT_FLOAT_EQ(high_res_scale,
804                  pending_layer_->HighResTiling()->contents_scale());
805  EXPECT_FLOAT_EQ(low_res_scale,
806                  pending_layer_->LowResTiling()->contents_scale());
807
808  ActivateTree();
809  SetupPendingTree(empty_pile);
810  EXPECT_FALSE(pending_layer_->CanHaveTilings());
811  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
812                                    high_res_scale,
813                                    device_scale,
814                                    page_scale,
815                                    maximum_animation_scale,
816                                    false);
817  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
818  ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
819
820  ActivateTree();
821  EXPECT_FALSE(active_layer_->CanHaveTilings());
822  SetupDrawPropertiesAndUpdateTiles(active_layer_,
823                                    high_res_scale,
824                                    device_scale,
825                                    page_scale,
826                                    maximum_animation_scale,
827                                    false);
828  ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
829
830  SetupPendingTree(valid_pile);
831  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
832                                    high_res_scale,
833                                    device_scale,
834                                    page_scale,
835                                    maximum_animation_scale,
836                                    false);
837  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
838  ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
839  EXPECT_FLOAT_EQ(high_res_scale,
840                  pending_layer_->HighResTiling()->contents_scale());
841  EXPECT_FLOAT_EQ(low_res_scale,
842                  pending_layer_->LowResTiling()->contents_scale());
843}
844
845TEST_F(PictureLayerImplTest, ZoomOutCrash) {
846  gfx::Size tile_size(400, 400);
847  gfx::Size layer_bounds(1300, 1900);
848
849  // Set up the high and low res tilings before pinch zoom.
850  scoped_refptr<FakePicturePileImpl> pending_pile =
851      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
852  scoped_refptr<FakePicturePileImpl> active_pile =
853      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
854
855  SetupTrees(pending_pile, active_pile);
856  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
857  SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, false);
858  host_impl_.PinchGestureBegin();
859  SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
860  SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
861  EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
862}
863
864TEST_F(PictureLayerImplTest, PinchGestureTilings) {
865  gfx::Size tile_size(400, 400);
866  gfx::Size layer_bounds(1300, 1900);
867
868  scoped_refptr<FakePicturePileImpl> pending_pile =
869      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
870  scoped_refptr<FakePicturePileImpl> active_pile =
871      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
872
873  // Set up the high and low res tilings before pinch zoom.
874  SetupTrees(pending_pile, active_pile);
875  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
876  SetContentsScaleOnBothLayers(2.0f, 1.0f, 1.0f, 1.0f, false);
877  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
878  EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
879  EXPECT_FLOAT_EQ(2.0f,
880                  active_layer_->tilings()->tiling_at(0)->contents_scale());
881  EXPECT_FLOAT_EQ(2.0f * low_res_factor,
882                  active_layer_->tilings()->tiling_at(1)->contents_scale());
883
884  // Start a pinch gesture.
885  host_impl_.PinchGestureBegin();
886
887  // Zoom out by a small amount. We should create a tiling at half
888  // the scale (2/kMaxScaleRatioDuringPinch).
889  SetContentsScaleOnBothLayers(1.8f, 1.0f, 0.9f, 1.0f, false);
890  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
891  EXPECT_FLOAT_EQ(2.0f,
892                  active_layer_->tilings()->tiling_at(0)->contents_scale());
893  EXPECT_FLOAT_EQ(1.0f,
894                  active_layer_->tilings()->tiling_at(1)->contents_scale());
895  EXPECT_FLOAT_EQ(2.0f * low_res_factor,
896                  active_layer_->tilings()->tiling_at(2)->contents_scale());
897
898  // Zoom out further, close to our low-res scale factor. We should
899  // use that tiling as high-res, and not create a new tiling.
900  SetContentsScaleOnBothLayers(
901      low_res_factor, 1.0f, low_res_factor / 2.0f, 1.0f, false);
902  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
903
904  // Zoom in a lot now. Since we increase by increments of
905  // kMaxScaleRatioDuringPinch, this will first use 1.0, then 2.0
906  // and then finally create a new tiling at 4.0.
907  SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
908  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
909  SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
910  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
911  SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
912  EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
913  EXPECT_FLOAT_EQ(4.0f,
914                  active_layer_->tilings()->tiling_at(0)->contents_scale());
915}
916
917TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
918  gfx::Size tile_size(300, 300);
919  gfx::Size layer_bounds(2600, 3800);
920
921  scoped_refptr<FakePicturePileImpl> pending_pile =
922      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
923  scoped_refptr<FakePicturePileImpl> active_pile =
924      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
925
926  // Set up the high and low res tilings before pinch zoom.
927  SetupTrees(pending_pile, active_pile);
928  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
929  SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f, 1.0f, false);
930  EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
931  EXPECT_FLOAT_EQ(0.24f,
932                  active_layer_->tilings()->tiling_at(0)->contents_scale());
933  EXPECT_FLOAT_EQ(0.0625f,
934                  active_layer_->tilings()->tiling_at(1)->contents_scale());
935
936  // Start a pinch gesture.
937  host_impl_.PinchGestureBegin();
938
939  // Zoom out by a small amount. We should create a tiling at half
940  // the scale (1/kMaxScaleRatioDuringPinch).
941  SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f, 1.0f, false);
942  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
943  EXPECT_FLOAT_EQ(0.24f,
944                  active_layer_->tilings()->tiling_at(0)->contents_scale());
945  EXPECT_FLOAT_EQ(0.12f,
946                  active_layer_->tilings()->tiling_at(1)->contents_scale());
947  EXPECT_FLOAT_EQ(0.0625,
948                  active_layer_->tilings()->tiling_at(2)->contents_scale());
949
950  // Zoom out further, close to our low-res scale factor. We should
951  // use that tiling as high-res, and not create a new tiling.
952  SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f, 1.0f, false);
953  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
954
955  // Zoom in. 0.125(desired_scale) should be snapped to 0.12 during zoom-in
956  // because 0.125(desired_scale) is within the ratio(1.2)
957  SetContentsScaleOnBothLayers(0.5f, 1.0f, 0.5f, 1.0f, false);
958  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
959}
960
961TEST_F(PictureLayerImplTest, CleanUpTilings) {
962  gfx::Size tile_size(400, 400);
963  gfx::Size layer_bounds(1300, 1900);
964
965  scoped_refptr<FakePicturePileImpl> pending_pile =
966      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
967  scoped_refptr<FakePicturePileImpl> active_pile =
968      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
969
970  std::vector<PictureLayerTiling*> used_tilings;
971
972  SetupTrees(pending_pile, active_pile);
973  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
974
975  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
976  EXPECT_LT(low_res_factor, 1.f);
977
978  float device_scale = 1.7f;
979  float page_scale = 3.2f;
980  float scale = 1.f;
981
982  SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
983  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
984
985  // We only have ideal tilings, so they aren't removed.
986  used_tilings.clear();
987  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
988  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
989
990  host_impl_.PinchGestureBegin();
991
992  // Changing the ideal but not creating new tilings.
993  scale *= 1.5f;
994  page_scale *= 1.5f;
995  SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
996  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
997
998  // The tilings are still our target scale, so they aren't removed.
999  used_tilings.clear();
1000  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1001  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1002
1003  host_impl_.PinchGestureEnd();
1004
1005  // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
1006  scale /= 4.f;
1007  page_scale /= 4.f;
1008  SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
1009  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1010  EXPECT_FLOAT_EQ(
1011      1.f,
1012      active_layer_->tilings()->tiling_at(1)->contents_scale());
1013  EXPECT_FLOAT_EQ(
1014      1.f * low_res_factor,
1015      active_layer_->tilings()->tiling_at(3)->contents_scale());
1016
1017  // Mark the non-ideal tilings as used. They won't be removed.
1018  used_tilings.clear();
1019  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1020  used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
1021  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1022  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1023
1024  // Now move the ideal scale to 0.5. Our target stays 1.2.
1025  SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
1026
1027  // The high resolution tiling is between target and ideal, so is not
1028  // removed.  The low res tiling for the old ideal=1.0 scale is removed.
1029  used_tilings.clear();
1030  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1031  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1032
1033  // Now move the ideal scale to 1.0. Our target stays 1.2.
1034  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
1035
1036  // All the tilings are between are target and the ideal, so they are not
1037  // removed.
1038  used_tilings.clear();
1039  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1040  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1041
1042  // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
1043  SetupDrawPropertiesAndUpdateTiles(
1044      active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1045
1046  // Because the pending layer's ideal scale is still 1.0, our tilings fall
1047  // in the range [1.0,1.2] and are kept.
1048  used_tilings.clear();
1049  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1050  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1051
1052  // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
1053  // 1.2 still.
1054  SetupDrawPropertiesAndUpdateTiles(
1055      pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1056
1057  // Our 1.0 tiling now falls outside the range between our ideal scale and our
1058  // target raster scale. But it is in our used tilings set, so nothing is
1059  // deleted.
1060  used_tilings.clear();
1061  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1062  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1063  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1064
1065  // If we remove it from our used tilings set, it is outside the range to keep
1066  // so it is deleted.
1067  used_tilings.clear();
1068  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1069  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1070}
1071
1072#define EXPECT_BOTH_EQ(expression, x)         \
1073  do {                                        \
1074    EXPECT_EQ(x, pending_layer_->expression); \
1075    EXPECT_EQ(x, active_layer_->expression);  \
1076  } while (false)
1077
1078TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
1079  // Make sure this layer covers multiple tiles, since otherwise low
1080  // res won't get created because it is too small.
1081  gfx::Size tile_size(host_impl_.settings().default_tile_size);
1082  SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
1083  // Avoid max untiled layer size heuristics via fixed tile size.
1084  pending_layer_->set_fixed_tile_size(tile_size);
1085  active_layer_->set_fixed_tile_size(tile_size);
1086
1087  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1088  float contents_scale = 1.f;
1089  float device_scale = 1.f;
1090  float page_scale = 1.f;
1091  float maximum_animation_scale = 1.f;
1092  bool animating_transform = true;
1093
1094  // Animating, so don't create low res even if there isn't one already.
1095  SetContentsScaleOnBothLayers(contents_scale,
1096                               device_scale,
1097                               page_scale,
1098                               maximum_animation_scale,
1099                               animating_transform);
1100  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1101  EXPECT_BOTH_EQ(num_tilings(), 1u);
1102
1103  // Stop animating, low res gets created.
1104  animating_transform = false;
1105  SetContentsScaleOnBothLayers(contents_scale,
1106                               device_scale,
1107                               page_scale,
1108                               maximum_animation_scale,
1109                               animating_transform);
1110  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1111  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1112  EXPECT_BOTH_EQ(num_tilings(), 2u);
1113
1114  // Page scale animation, new high res, but not new low res because animating.
1115  contents_scale = 2.f;
1116  page_scale = 2.f;
1117  animating_transform = true;
1118  SetContentsScaleOnBothLayers(contents_scale,
1119                               device_scale,
1120                               page_scale,
1121                               maximum_animation_scale,
1122                               animating_transform);
1123  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1124  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1125  EXPECT_BOTH_EQ(num_tilings(), 3u);
1126
1127  // Stop animating, new low res gets created for final page scale.
1128  animating_transform = false;
1129  SetContentsScaleOnBothLayers(contents_scale,
1130                               device_scale,
1131                               page_scale,
1132                               maximum_animation_scale,
1133                               animating_transform);
1134  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1135  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1136  EXPECT_BOTH_EQ(num_tilings(), 4u);
1137}
1138
1139TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1140  gfx::Size tile_size(host_impl_.settings().default_tile_size);
1141  SetupDefaultTrees(tile_size);
1142
1143  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1144  float device_scale = 1.f;
1145  float page_scale = 1.f;
1146  float maximum_animation_scale = 1.f;
1147  bool animating_transform = false;
1148
1149  // Contents exactly fit on one tile at scale 1, no low res.
1150  float contents_scale = 1.f;
1151  SetContentsScaleOnBothLayers(contents_scale,
1152                               device_scale,
1153                               page_scale,
1154                               maximum_animation_scale,
1155                               animating_transform);
1156  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1157  EXPECT_BOTH_EQ(num_tilings(), 1u);
1158
1159  ResetTilingsAndRasterScales();
1160
1161  // Contents that are smaller than one tile, no low res.
1162  contents_scale = 0.123f;
1163  SetContentsScaleOnBothLayers(contents_scale,
1164                               device_scale,
1165                               page_scale,
1166                               maximum_animation_scale,
1167                               animating_transform);
1168  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1169  EXPECT_BOTH_EQ(num_tilings(), 1u);
1170
1171  ResetTilingsAndRasterScales();
1172
1173  // Any content bounds that would create more than one tile will
1174  // generate a low res tiling.
1175  contents_scale = 2.5f;
1176  SetContentsScaleOnBothLayers(contents_scale,
1177                               device_scale,
1178                               page_scale,
1179                               maximum_animation_scale,
1180                               animating_transform);
1181  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1182  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1183                 contents_scale * low_res_factor);
1184  EXPECT_BOTH_EQ(num_tilings(), 2u);
1185
1186  ResetTilingsAndRasterScales();
1187
1188  // Mask layers dont create low res since they always fit on one tile.
1189  pending_layer_->SetIsMask(true);
1190  active_layer_->SetIsMask(true);
1191  SetContentsScaleOnBothLayers(contents_scale,
1192                               device_scale,
1193                               page_scale,
1194                               maximum_animation_scale,
1195                               animating_transform);
1196  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1197  EXPECT_BOTH_EQ(num_tilings(), 1u);
1198}
1199
1200TEST_F(PictureLayerImplTest, ReleaseResources) {
1201  gfx::Size tile_size(400, 400);
1202  gfx::Size layer_bounds(1300, 1900);
1203
1204  scoped_refptr<FakePicturePileImpl> pending_pile =
1205      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1206  scoped_refptr<FakePicturePileImpl> active_pile =
1207      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1208
1209  SetupTrees(pending_pile, active_pile);
1210  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1211
1212  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1213                                    1.3f,  // ideal contents scale
1214                                    2.7f,  // device scale
1215                                    3.2f,  // page scale
1216                                    1.f,   // maximum animation scale
1217                                    false);
1218  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1219
1220  // All tilings should be removed when losing output surface.
1221  active_layer_->ReleaseResources();
1222  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1223  pending_layer_->ReleaseResources();
1224  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1225
1226  // This should create new tilings.
1227  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1228                                    1.3f,  // ideal contents scale
1229                                    2.7f,  // device scale
1230                                    3.2f,  // page scale
1231                                    1.f,   // maximum animation scale
1232                                    false);
1233  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1234}
1235
1236TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1237  // The default max tile size is larger than 400x400.
1238  gfx::Size tile_size(400, 400);
1239  gfx::Size layer_bounds(5000, 5000);
1240
1241  scoped_refptr<FakePicturePileImpl> pending_pile =
1242      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1243  scoped_refptr<FakePicturePileImpl> active_pile =
1244      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1245
1246  SetupTrees(pending_pile, active_pile);
1247  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1248
1249  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1250  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1251
1252  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1253
1254  // The default value.
1255  EXPECT_EQ(gfx::Size(256, 256).ToString(),
1256            host_impl_.settings().default_tile_size.ToString());
1257
1258  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1259  EXPECT_EQ(gfx::Size(256, 256).ToString(),
1260            tile->content_rect().size().ToString());
1261
1262  pending_layer_->ReleaseResources();
1263
1264  // Change the max texture size on the output surface context.
1265  scoped_ptr<TestWebGraphicsContext3D> context =
1266      TestWebGraphicsContext3D::Create();
1267  context->set_max_texture_size(140);
1268  host_impl_.DidLoseOutputSurface();
1269  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1270      context.Pass()).PassAs<OutputSurface>());
1271
1272  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1273  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1274
1275  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1276
1277  // Verify the tiles are not larger than the context's max texture size.
1278  tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1279  EXPECT_GE(140, tile->content_rect().width());
1280  EXPECT_GE(140, tile->content_rect().height());
1281}
1282
1283TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1284  // The default max tile size is larger than 400x400.
1285  gfx::Size tile_size(400, 400);
1286  gfx::Size layer_bounds(500, 500);
1287
1288  scoped_refptr<FakePicturePileImpl> pending_pile =
1289      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1290  scoped_refptr<FakePicturePileImpl> active_pile =
1291      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1292
1293  SetupTrees(pending_pile, active_pile);
1294  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1295
1296  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1297  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1298
1299  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1300
1301  // The default value. The layer is smaller than this.
1302  EXPECT_EQ(gfx::Size(512, 512).ToString(),
1303            host_impl_.settings().max_untiled_layer_size.ToString());
1304
1305  // There should be a single tile since the layer is small.
1306  PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1307  EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1308
1309  pending_layer_->ReleaseResources();
1310
1311  // Change the max texture size on the output surface context.
1312  scoped_ptr<TestWebGraphicsContext3D> context =
1313      TestWebGraphicsContext3D::Create();
1314  context->set_max_texture_size(140);
1315  host_impl_.DidLoseOutputSurface();
1316  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1317      context.Pass()).PassAs<OutputSurface>());
1318
1319  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1320  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1321
1322  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1323
1324  // There should be more than one tile since the max texture size won't cover
1325  // the layer.
1326  high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1327  EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1328
1329  // Verify the tiles are not larger than the context's max texture size.
1330  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1331  EXPECT_GE(140, tile->content_rect().width());
1332  EXPECT_GE(140, tile->content_rect().height());
1333}
1334
1335TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1336  MockOcclusionTracker<LayerImpl> occlusion_tracker;
1337  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1338  MockQuadCuller quad_culler(render_pass.get(), &occlusion_tracker);
1339
1340  gfx::Size tile_size(400, 400);
1341  gfx::Size layer_bounds(1300, 1900);
1342
1343  scoped_refptr<FakePicturePileImpl> pending_pile =
1344      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1345  scoped_refptr<FakePicturePileImpl> active_pile =
1346      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1347
1348  SetupTrees(pending_pile, active_pile);
1349
1350  active_layer_->draw_properties().visible_content_rect =
1351      gfx::Rect(layer_bounds);
1352
1353  gfx::Rect layer_invalidation(150, 200, 30, 180);
1354  Region invalidation(layer_invalidation);
1355  AddDefaultTilingsWithInvalidation(invalidation);
1356
1357  AppendQuadsData data;
1358  active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
1359  active_layer_->AppendQuads(&quad_culler, &data);
1360  active_layer_->DidDraw(NULL);
1361
1362  ASSERT_EQ(1U, quad_culler.quad_list().size());
1363  EXPECT_EQ(DrawQuad::PICTURE_CONTENT, quad_culler.quad_list()[0]->material);
1364}
1365
1366TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
1367  gfx::Size tile_size(100, 100);
1368  gfx::Size layer_bounds(1000, 1000);
1369
1370  scoped_refptr<FakePicturePileImpl> pending_pile =
1371      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1372  // Layers with entirely empty piles can't get tilings.
1373  pending_pile->AddRecordingAt(0, 0);
1374
1375  SetupPendingTree(pending_pile);
1376
1377  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1378  pending_layer_->AddTiling(1.0f);
1379  pending_layer_->AddTiling(2.0f);
1380
1381  // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1382  // on a layer with no recordings.
1383  host_impl_.pending_tree()->UpdateDrawProperties();
1384  pending_layer_->MarkVisibleResourcesAsRequired();
1385}
1386
1387TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1388  gfx::Size tile_size(100, 100);
1389  gfx::Size layer_bounds(200, 200);
1390
1391  scoped_refptr<FakePicturePileImpl> pending_pile =
1392      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1393  SetupPendingTree(pending_pile);
1394
1395  pending_layer_->set_fixed_tile_size(tile_size);
1396  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1397  PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1398  host_impl_.pending_tree()->UpdateDrawProperties();
1399  EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1400
1401  pending_layer_->draw_properties().visible_content_rect =
1402      gfx::Rect(0, 0, 100, 200);
1403
1404  // Fake set priorities.
1405  for (PictureLayerTiling::CoverageIterator iter(
1406           tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1407       iter;
1408       ++iter) {
1409    if (!*iter)
1410      continue;
1411    Tile* tile = *iter;
1412    TilePriority priority;
1413    priority.resolution = HIGH_RESOLUTION;
1414    gfx::Rect tile_bounds = iter.geometry_rect();
1415    if (pending_layer_->visible_content_rect().Intersects(tile_bounds)) {
1416      priority.priority_bin = TilePriority::NOW;
1417      priority.distance_to_visible = 0.f;
1418    } else {
1419      priority.priority_bin = TilePriority::SOON;
1420      priority.distance_to_visible = 1.f;
1421    }
1422    tile->SetPriority(PENDING_TREE, priority);
1423  }
1424
1425  pending_layer_->MarkVisibleResourcesAsRequired();
1426
1427  int num_visible = 0;
1428  int num_offscreen = 0;
1429
1430  for (PictureLayerTiling::CoverageIterator iter(
1431           tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1432       iter;
1433       ++iter) {
1434    if (!*iter)
1435      continue;
1436    const Tile* tile = *iter;
1437    if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1438      EXPECT_TRUE(tile->required_for_activation());
1439      num_visible++;
1440    } else {
1441      EXPECT_FALSE(tile->required_for_activation());
1442      num_offscreen++;
1443    }
1444  }
1445
1446  EXPECT_GT(num_visible, 0);
1447  EXPECT_GT(num_offscreen, 0);
1448}
1449
1450TEST_F(PictureLayerImplTest, TileOutsideOfViewportForTilePriorityNotRequired) {
1451  base::TimeTicks time_ticks;
1452  time_ticks += base::TimeDelta::FromMilliseconds(1);
1453  host_impl_.SetCurrentBeginFrameArgs(
1454      CreateBeginFrameArgsForTesting(time_ticks));
1455
1456  gfx::Size tile_size(100, 100);
1457  gfx::Size layer_bounds(400, 400);
1458  gfx::Rect external_viewport_for_tile_priority(400, 200);
1459  gfx::Rect visible_content_rect(200, 400);
1460
1461  scoped_refptr<FakePicturePileImpl> active_pile =
1462      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1463  scoped_refptr<FakePicturePileImpl> pending_pile =
1464      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1465  SetupTrees(pending_pile, active_pile);
1466
1467  active_layer_->set_fixed_tile_size(tile_size);
1468  pending_layer_->set_fixed_tile_size(tile_size);
1469  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1470  PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1471
1472  // Set external viewport for tile priority.
1473  gfx::Rect viewport = gfx::Rect(layer_bounds);
1474  gfx::Transform transform;
1475  gfx::Transform transform_for_tile_priority;
1476  bool resourceless_software_draw = false;
1477  host_impl_.SetExternalDrawConstraints(transform,
1478                                        viewport,
1479                                        viewport,
1480                                        external_viewport_for_tile_priority,
1481                                        transform_for_tile_priority,
1482                                        resourceless_software_draw);
1483  host_impl_.pending_tree()->UpdateDrawProperties();
1484
1485  // Set visible content rect that is different from
1486  // external_viewport_for_tile_priority.
1487  pending_layer_->draw_properties().visible_content_rect = visible_content_rect;
1488  time_ticks += base::TimeDelta::FromMilliseconds(200);
1489  host_impl_.SetCurrentBeginFrameArgs(
1490      CreateBeginFrameArgsForTesting(time_ticks));
1491  pending_layer_->UpdateTiles(NULL);
1492
1493  pending_layer_->MarkVisibleResourcesAsRequired();
1494
1495  // Intersect the two rects. Any tile outside should not be required for
1496  // activation.
1497  gfx::Rect viewport_for_tile_priority =
1498      pending_layer_->GetViewportForTilePriorityInContentSpace();
1499  viewport_for_tile_priority.Intersect(pending_layer_->visible_content_rect());
1500
1501  int num_inside = 0;
1502  int num_outside = 0;
1503  for (PictureLayerTiling::CoverageIterator iter(
1504           tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1505       iter;
1506       ++iter) {
1507    if (!*iter)
1508      continue;
1509    Tile* tile = *iter;
1510    if (viewport_for_tile_priority.Intersects(iter.geometry_rect())) {
1511      num_inside++;
1512      // Mark everything in viewport for tile priority as ready to draw.
1513      ManagedTileState::TileVersion& tile_version =
1514          tile->GetTileVersionForTesting(
1515              tile->DetermineRasterModeForTree(PENDING_TREE));
1516      tile_version.SetSolidColorForTesting(SK_ColorRED);
1517    } else {
1518      num_outside++;
1519      EXPECT_FALSE(tile->required_for_activation());
1520    }
1521  }
1522
1523  EXPECT_GT(num_inside, 0);
1524  EXPECT_GT(num_outside, 0);
1525
1526  // Activate and draw active layer.
1527  host_impl_.ActivateSyncTree();
1528  host_impl_.active_tree()->UpdateDrawProperties();
1529  active_layer_->draw_properties().visible_content_rect = visible_content_rect;
1530
1531  MockOcclusionTracker<LayerImpl> occlusion_tracker;
1532  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1533  AppendQuadsData data;
1534  active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1535  active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1536  active_layer_->DidDraw(NULL);
1537
1538  // All tiles in activation rect is ready to draw.
1539  EXPECT_EQ(0u, data.num_missing_tiles);
1540  EXPECT_EQ(0u, data.num_incomplete_tiles);
1541}
1542
1543TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
1544  gfx::Size layer_bounds(400, 400);
1545  gfx::Size tile_size(100, 100);
1546  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1547
1548  // No tiles shared.
1549  pending_layer_->set_invalidation(gfx::Rect(layer_bounds));
1550
1551  CreateHighLowResAndSetAllTilesVisible();
1552
1553  active_layer_->SetAllTilesReady();
1554
1555  // No shared tiles and all active tiles ready, so pending can only
1556  // activate with all high res tiles.
1557  pending_layer_->MarkVisibleResourcesAsRequired();
1558  AssertAllTilesRequired(pending_layer_->HighResTiling());
1559  AssertNoTilesRequired(pending_layer_->LowResTiling());
1560}
1561
1562TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
1563  gfx::Size layer_bounds(400, 400);
1564  gfx::Size tile_size(100, 100);
1565  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1566
1567  // All tiles shared (no invalidation).
1568  CreateHighLowResAndSetAllTilesVisible();
1569
1570  // Verify active tree not ready.
1571  Tile* some_active_tile =
1572      active_layer_->HighResTiling()->AllTilesForTesting()[0];
1573  EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1574
1575  // When high res are required, even if the active tree is not ready,
1576  // the high res tiles must be ready.
1577  host_impl_.active_tree()->SetRequiresHighResToDraw();
1578  pending_layer_->MarkVisibleResourcesAsRequired();
1579  AssertAllTilesRequired(pending_layer_->HighResTiling());
1580  AssertNoTilesRequired(pending_layer_->LowResTiling());
1581}
1582
1583TEST_F(PictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
1584  gfx::Size layer_bounds(400, 400);
1585  gfx::Size tile_size(100, 100);
1586  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1587
1588  CreateHighLowResAndSetAllTilesVisible();
1589
1590  Tile* some_active_tile =
1591      active_layer_->HighResTiling()->AllTilesForTesting()[0];
1592  EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1593
1594  // All tiles shared (no invalidation), so even though the active tree's
1595  // tiles aren't ready, there is nothing required.
1596  pending_layer_->MarkVisibleResourcesAsRequired();
1597  AssertNoTilesRequired(pending_layer_->HighResTiling());
1598  AssertNoTilesRequired(pending_layer_->LowResTiling());
1599}
1600
1601TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
1602  gfx::Size layer_bounds(400, 400);
1603  gfx::Size tile_size(100, 100);
1604  scoped_refptr<FakePicturePileImpl> pending_pile =
1605      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1606  // This pile will create tilings, but has no recordings so will not create any
1607  // tiles.  This is attempting to simulate scrolling past the end of recorded
1608  // content on the active layer, where the recordings are so far away that
1609  // no tiles are created.
1610  scoped_refptr<FakePicturePileImpl> active_pile =
1611      FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1612          tile_size, layer_bounds);
1613  SetupTrees(pending_pile, active_pile);
1614  pending_layer_->set_fixed_tile_size(tile_size);
1615  active_layer_->set_fixed_tile_size(tile_size);
1616
1617  CreateHighLowResAndSetAllTilesVisible();
1618
1619  // Active layer has tilings, but no tiles due to missing recordings.
1620  EXPECT_TRUE(active_layer_->CanHaveTilings());
1621  EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
1622  EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
1623
1624  // Since the active layer has no tiles at all, the pending layer doesn't
1625  // need content in order to activate.
1626  pending_layer_->MarkVisibleResourcesAsRequired();
1627  AssertNoTilesRequired(pending_layer_->HighResTiling());
1628  AssertNoTilesRequired(pending_layer_->LowResTiling());
1629}
1630
1631TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
1632  gfx::Size layer_bounds(400, 400);
1633  gfx::Size tile_size(100, 100);
1634  scoped_refptr<FakePicturePileImpl> pending_pile =
1635      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1636  scoped_refptr<FakePicturePileImpl> active_pile =
1637      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1638  SetupTrees(pending_pile, active_pile);
1639  pending_layer_->set_fixed_tile_size(tile_size);
1640  active_layer_->set_fixed_tile_size(tile_size);
1641
1642  CreateHighLowResAndSetAllTilesVisible();
1643
1644  // Active layer can't have tiles.
1645  EXPECT_FALSE(active_layer_->CanHaveTilings());
1646
1647  // All high res tiles required.  This should be considered identical
1648  // to the case where there is no active layer, to avoid flashing content.
1649  // This can happen if a layer exists for a while and switches from
1650  // not being able to have content to having content.
1651  pending_layer_->MarkVisibleResourcesAsRequired();
1652  AssertAllTilesRequired(pending_layer_->HighResTiling());
1653  AssertNoTilesRequired(pending_layer_->LowResTiling());
1654}
1655
1656TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
1657  gfx::Size layer_bounds(200, 200);
1658  gfx::Size tile_size(100, 100);
1659  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1660
1661  gfx::Size pending_layer_bounds(400, 400);
1662  pending_layer_->SetBounds(pending_layer_bounds);
1663
1664  CreateHighLowResAndSetAllTilesVisible();
1665
1666  active_layer_->SetAllTilesReady();
1667
1668  // Since the active layer has different bounds, the pending layer needs all
1669  // high res tiles in order to activate.
1670  pending_layer_->MarkVisibleResourcesAsRequired();
1671  AssertAllTilesRequired(pending_layer_->HighResTiling());
1672  AssertNoTilesRequired(pending_layer_->LowResTiling());
1673}
1674
1675TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1676  gfx::Size tile_size(100, 100);
1677  gfx::Size layer_bounds(400, 400);
1678  scoped_refptr<FakePicturePileImpl> pending_pile =
1679      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1680
1681  host_impl_.CreatePendingTree();
1682  LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1683
1684  scoped_ptr<FakePictureLayerImpl> pending_layer =
1685      FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1686  pending_layer->SetDrawsContent(true);
1687  pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1688
1689  pending_layer_ = static_cast<FakePictureLayerImpl*>(
1690      host_impl_.pending_tree()->LayerById(id_));
1691
1692  // Set some state on the pending layer, make sure it is not clobbered
1693  // by a sync from the active layer.  This could happen because if the
1694  // pending layer has not been post-commit initialized it will attempt
1695  // to sync from the active layer.
1696  float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
1697  pending_layer_->set_raster_page_scale(raster_page_scale);
1698  EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1699
1700  host_impl_.ActivatePendingTree();
1701
1702  active_layer_ = static_cast<FakePictureLayerImpl*>(
1703      host_impl_.active_tree()->LayerById(id_));
1704
1705  EXPECT_EQ(0u, active_layer_->num_tilings());
1706  EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
1707  EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1708}
1709
1710TEST_F(PictureLayerImplTest, RemoveInvalidTilesOnActivation) {
1711  SetupDefaultTrees(gfx::Size(1500, 1500));
1712  AddDefaultTilingsWithInvalidation(gfx::Rect(0, 0, 1, 1));
1713
1714  FakePictureLayerImpl* recycled_layer = pending_layer_;
1715  host_impl_.ActivatePendingTree();
1716
1717  active_layer_ = static_cast<FakePictureLayerImpl*>(
1718      host_impl_.active_tree()->LayerById(id_));
1719
1720  EXPECT_EQ(3u, active_layer_->num_tilings());
1721  EXPECT_EQ(3u, recycled_layer->num_tilings());
1722  EXPECT_FALSE(host_impl_.pending_tree());
1723  for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1724    PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1725    PictureLayerTiling* recycled_tiling =
1726        recycled_layer->tilings()->tiling_at(i);
1727
1728    ASSERT_TRUE(active_tiling);
1729    ASSERT_TRUE(recycled_tiling);
1730
1731    EXPECT_TRUE(active_tiling->TileAt(0, 0));
1732    EXPECT_TRUE(active_tiling->TileAt(1, 0));
1733    EXPECT_TRUE(active_tiling->TileAt(0, 1));
1734    EXPECT_TRUE(active_tiling->TileAt(1, 1));
1735
1736    EXPECT_FALSE(recycled_tiling->TileAt(0, 0));
1737    EXPECT_TRUE(recycled_tiling->TileAt(1, 0));
1738    EXPECT_TRUE(recycled_tiling->TileAt(0, 1));
1739    EXPECT_TRUE(recycled_tiling->TileAt(1, 1));
1740
1741    EXPECT_EQ(active_tiling->TileAt(1, 0), recycled_tiling->TileAt(1, 0));
1742    EXPECT_EQ(active_tiling->TileAt(0, 1), recycled_tiling->TileAt(0, 1));
1743    EXPECT_EQ(active_tiling->TileAt(1, 1), recycled_tiling->TileAt(1, 1));
1744  }
1745}
1746
1747TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
1748  SetupDefaultTrees(gfx::Size(10, 10));
1749  host_impl_.active_tree()->UpdateDrawProperties();
1750  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1751
1752  // Contrived unit test of a real crash. A layer is transparent during a
1753  // context loss, and later becomes opaque, causing active layer SyncTiling to
1754  // be called.
1755  float new_scale = 1.f;
1756  active_layer_->ReleaseResources();
1757  pending_layer_->ReleaseResources();
1758  EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(new_scale));
1759  pending_layer_->AddTiling(new_scale);
1760  EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(new_scale));
1761
1762  // UpdateDrawProperties early-outs if the tree doesn't need it.  It is also
1763  // responsible for calling ManageTilings.  These checks verify that
1764  // ReleaseResources has set needs update draw properties so that the
1765  // new tiling gets the appropriate resolution set in ManageTilings.
1766  EXPECT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1767  host_impl_.active_tree()->UpdateDrawProperties();
1768  PictureLayerTiling* high_res =
1769      active_layer_->tilings()->TilingAtScale(new_scale);
1770  ASSERT_TRUE(!!high_res);
1771  EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
1772}
1773
1774TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
1775  SetupDefaultTrees(gfx::Size(10, 10));
1776
1777  const float kScale = 1.f;
1778  pending_layer_->AddTiling(kScale);
1779  EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
1780  EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
1781
1782  // Gpu rasterization is disabled by default.
1783  EXPECT_FALSE(host_impl_.use_gpu_rasterization());
1784  // Toggling the gpu rasterization clears all tilings on both trees.
1785  host_impl_.SetUseGpuRasterization(true);
1786  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1787  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1788
1789  // Make sure that we can still add tiling to the pending layer,
1790  // that gets synced to the active layer.
1791  pending_layer_->AddTiling(kScale);
1792  EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
1793  EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
1794
1795  // Toggling the gpu rasterization clears all tilings on both trees.
1796  EXPECT_TRUE(host_impl_.use_gpu_rasterization());
1797  host_impl_.SetUseGpuRasterization(false);
1798  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1799  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1800}
1801
1802TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
1803  SetupDefaultTrees(gfx::Size(10, 10));
1804  host_impl_.active_tree()->UpdateDrawProperties();
1805  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1806
1807  SetupDrawPropertiesAndUpdateTiles(
1808      active_layer_, 0.5f, 0.5f, 0.5f, 0.5f, false);
1809  active_layer_->tilings()->RemoveAllTilings();
1810  PictureLayerTiling* tiling = active_layer_->tilings()->AddTiling(0.5f);
1811  active_layer_->tilings()->AddTiling(1.5f);
1812  active_layer_->tilings()->AddTiling(0.25f);
1813  tiling->set_resolution(HIGH_RESOLUTION);
1814
1815  // Sanity checks.
1816  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1817  ASSERT_EQ(tiling, active_layer_->tilings()->TilingAtScale(0.5f));
1818
1819  // Now, set the bounds to be 1x1 (so that minimum contents scale becomes
1820  // 1.0f). Note that we should also ensure that the pending layer needs post
1821  // commit initialization, since this is what would happen during commit. In
1822  // other words we want the pending layer to sync from the active layer.
1823  pending_layer_->SetBounds(gfx::Size(1, 1));
1824  pending_layer_->SetNeedsPostCommitInitialization();
1825  pending_layer_->set_twin_layer(NULL);
1826  active_layer_->set_twin_layer(NULL);
1827  EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1828
1829  // Update the draw properties: sync from active tree should happen here.
1830  host_impl_.pending_tree()->UpdateDrawProperties();
1831
1832  // Another sanity check.
1833  ASSERT_EQ(1.f, pending_layer_->MinimumContentsScale());
1834
1835  // Now we should've synced 1.5f tiling, since that's the only one that doesn't
1836  // violate minimum contents scale. At the same time, we should've created a
1837  // new high res tiling at scale 1.0f.
1838  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1839  ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.0f));
1840  EXPECT_EQ(HIGH_RESOLUTION,
1841            pending_layer_->tilings()->TilingAtScale(1.0f)->resolution());
1842  ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.5f));
1843  EXPECT_EQ(NON_IDEAL_RESOLUTION,
1844            pending_layer_->tilings()->TilingAtScale(1.5f)->resolution());
1845}
1846
1847TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
1848  gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
1849  gfx::Size layer_bounds(default_tile_size.width() * 4,
1850                         default_tile_size.height() * 4);
1851
1852  SetupDefaultTrees(layer_bounds);
1853  EXPECT_FALSE(host_impl_.use_gpu_rasterization());
1854  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1855  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1856  // Should have a low-res and a high-res tiling.
1857  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1858
1859  ResetTilingsAndRasterScales();
1860
1861  host_impl_.SetUseGpuRasterization(true);
1862  EXPECT_TRUE(host_impl_.use_gpu_rasterization());
1863  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1864
1865  // Should only have the high-res tiling.
1866  ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
1867}
1868
1869TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
1870  // Set up layers with tilings.
1871  SetupDefaultTrees(gfx::Size(10, 10));
1872  SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
1873  pending_layer_->PushPropertiesTo(active_layer_);
1874  EXPECT_TRUE(pending_layer_->DrawsContent());
1875  EXPECT_TRUE(pending_layer_->CanHaveTilings());
1876  EXPECT_GE(pending_layer_->num_tilings(), 0u);
1877  EXPECT_GE(active_layer_->num_tilings(), 0u);
1878
1879  // Set content to false, which should make CanHaveTilings return false.
1880  pending_layer_->SetDrawsContent(false);
1881  EXPECT_FALSE(pending_layer_->DrawsContent());
1882  EXPECT_FALSE(pending_layer_->CanHaveTilings());
1883
1884  // No tilings should be pushed to active layer.
1885  pending_layer_->PushPropertiesTo(active_layer_);
1886  EXPECT_EQ(0u, active_layer_->num_tilings());
1887}
1888
1889TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
1890  SetupDefaultTrees(gfx::Size(10, 10));
1891  host_impl_.PinchGestureBegin();
1892  float high_res_scale = 2.3f;
1893  SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
1894
1895  ASSERT_GE(pending_layer_->num_tilings(), 0u);
1896  EXPECT_FLOAT_EQ(high_res_scale,
1897                  pending_layer_->HighResTiling()->contents_scale());
1898}
1899
1900TEST_F(PictureLayerImplTest, FirstTilingTooSmall) {
1901  SetupDefaultTrees(gfx::Size(10, 10));
1902  host_impl_.PinchGestureBegin();
1903  float high_res_scale = 0.0001f;
1904  EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale);
1905
1906  SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
1907
1908  ASSERT_GE(pending_layer_->num_tilings(), 0u);
1909  EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
1910                  pending_layer_->HighResTiling()->contents_scale());
1911}
1912
1913TEST_F(PictureLayerImplTest, PinchingTooSmall) {
1914  SetupDefaultTrees(gfx::Size(10, 10));
1915
1916  float contents_scale = 0.15f;
1917  SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
1918
1919  ASSERT_GE(pending_layer_->num_tilings(), 0u);
1920  EXPECT_FLOAT_EQ(contents_scale,
1921                  pending_layer_->HighResTiling()->contents_scale());
1922
1923  host_impl_.PinchGestureBegin();
1924
1925  float page_scale = 0.0001f;
1926  EXPECT_LT(page_scale * contents_scale,
1927            pending_layer_->MinimumContentsScale());
1928
1929  SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, 1.f, false);
1930  ASSERT_GE(pending_layer_->num_tilings(), 0u);
1931  EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
1932                  pending_layer_->HighResTiling()->contents_scale());
1933}
1934
1935class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
1936 public:
1937  virtual void InitializeRenderer() OVERRIDE {
1938    bool delegated_rendering = false;
1939    host_impl_.InitializeRenderer(
1940        FakeOutputSurface::CreateDeferredGL(
1941            scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
1942            delegated_rendering).PassAs<OutputSurface>());
1943  }
1944
1945  virtual void SetUp() OVERRIDE {
1946    PictureLayerImplTest::SetUp();
1947
1948    // Create some default active and pending trees.
1949    gfx::Size tile_size(100, 100);
1950    gfx::Size layer_bounds(400, 400);
1951
1952    scoped_refptr<FakePicturePileImpl> pending_pile =
1953        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1954    scoped_refptr<FakePicturePileImpl> active_pile =
1955        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1956
1957    SetupTrees(pending_pile, active_pile);
1958  }
1959};
1960
1961// This test is really a LayerTreeHostImpl test, in that it makes sure
1962// that trees need update draw properties after deferred initialization.
1963// However, this is also a regression test for PictureLayerImpl in that
1964// not having this update will cause a crash.
1965TEST_F(DeferredInitPictureLayerImplTest, PreventUpdateTilesDuringLostContext) {
1966  host_impl_.pending_tree()->UpdateDrawProperties();
1967  host_impl_.active_tree()->UpdateDrawProperties();
1968  EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
1969  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1970
1971  FakeOutputSurface* fake_output_surface =
1972      static_cast<FakeOutputSurface*>(host_impl_.output_surface());
1973  ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
1974      TestContextProvider::Create()));
1975
1976  // These will crash PictureLayerImpl if this is not true.
1977  ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
1978  ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1979  host_impl_.active_tree()->UpdateDrawProperties();
1980}
1981
1982TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
1983  gfx::Size layer_bounds(100, 100);
1984  gfx::Size viewport_size(1000, 1000);
1985  SetupDefaultTrees(layer_bounds);
1986  host_impl_.SetViewportSize(viewport_size);
1987
1988  float contents_scale = 1.f;
1989  float device_scale = 1.3f;
1990  float page_scale = 1.4f;
1991  float maximum_animation_scale = 1.f;
1992  bool animating_transform = false;
1993
1994  SetContentsScaleOnBothLayers(contents_scale,
1995                               device_scale,
1996                               page_scale,
1997                               maximum_animation_scale,
1998                               animating_transform);
1999  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2000
2001  // Since we're CPU-rasterizing, starting an animation should cause tiling
2002  // resolution to get set to the maximum animation scale factor.
2003  animating_transform = true;
2004  maximum_animation_scale = 3.f;
2005  contents_scale = 2.f;
2006
2007  SetContentsScaleOnBothLayers(contents_scale,
2008                               device_scale,
2009                               page_scale,
2010                               maximum_animation_scale,
2011                               animating_transform);
2012  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2013
2014  // Further changes to scale during the animation should not cause a new
2015  // high-res tiling to get created.
2016  contents_scale = 4.f;
2017  maximum_animation_scale = 5.f;
2018
2019  SetContentsScaleOnBothLayers(contents_scale,
2020                               device_scale,
2021                               page_scale,
2022                               maximum_animation_scale,
2023                               animating_transform);
2024  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2025
2026  // Once we stop animating, a new high-res tiling should be created.
2027  animating_transform = false;
2028
2029  SetContentsScaleOnBothLayers(contents_scale,
2030                               device_scale,
2031                               page_scale,
2032                               maximum_animation_scale,
2033                               animating_transform);
2034  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2035
2036  // When animating with an unknown maximum animation scale factor, a new
2037  // high-res tiling should be created at the animation's initial scale.
2038  animating_transform = true;
2039  contents_scale = 2.f;
2040  maximum_animation_scale = 0.f;
2041
2042  SetContentsScaleOnBothLayers(contents_scale,
2043                               device_scale,
2044                               page_scale,
2045                               maximum_animation_scale,
2046                               animating_transform);
2047  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2048
2049  // Further changes to scale during the animation should not cause a new
2050  // high-res tiling to get created.
2051  contents_scale = 3.f;
2052
2053  SetContentsScaleOnBothLayers(contents_scale,
2054                               device_scale,
2055                               page_scale,
2056                               maximum_animation_scale,
2057                               animating_transform);
2058  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2059
2060  // Once we stop animating, a new high-res tiling should be created.
2061  animating_transform = false;
2062  contents_scale = 4.f;
2063
2064  SetContentsScaleOnBothLayers(contents_scale,
2065                               device_scale,
2066                               page_scale,
2067                               maximum_animation_scale,
2068                               animating_transform);
2069  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2070
2071  // When animating with a maxmium animation scale factor that is so large
2072  // that the layer grows larger than the viewport at this scale, a new
2073  // high-res tiling should get created at the animation's initial scale, not
2074  // at its maximum scale.
2075  animating_transform = true;
2076  contents_scale = 2.f;
2077  maximum_animation_scale = 11.f;
2078
2079  SetContentsScaleOnBothLayers(contents_scale,
2080                               device_scale,
2081                               page_scale,
2082                               maximum_animation_scale,
2083                               animating_transform);
2084  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2085
2086  // Once we stop animating, a new high-res tiling should be created.
2087  animating_transform = false;
2088  contents_scale = 11.f;
2089
2090  SetContentsScaleOnBothLayers(contents_scale,
2091                               device_scale,
2092                               page_scale,
2093                               maximum_animation_scale,
2094                               animating_transform);
2095  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2096
2097  // When animating with a maxmium animation scale factor that is so large
2098  // that the layer grows larger than the viewport at this scale, and where
2099  // the intial source scale is < 1, a new high-res tiling should get created
2100  // at source scale 1.
2101  animating_transform = true;
2102  contents_scale = 0.1f;
2103  maximum_animation_scale = 11.f;
2104
2105  SetContentsScaleOnBothLayers(contents_scale,
2106                               device_scale,
2107                               page_scale,
2108                               maximum_animation_scale,
2109                               animating_transform);
2110  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2111
2112  // Once we stop animating, a new high-res tiling should be created.
2113  animating_transform = false;
2114  contents_scale = 11.f;
2115
2116  SetContentsScaleOnBothLayers(contents_scale,
2117                               device_scale,
2118                               page_scale,
2119                               maximum_animation_scale,
2120                               animating_transform);
2121  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2122}
2123
2124TEST_F(PictureLayerImplTest, LayerRasterTileIterator) {
2125  gfx::Size tile_size(100, 100);
2126  gfx::Size layer_bounds(1000, 1000);
2127
2128  scoped_refptr<FakePicturePileImpl> pending_pile =
2129      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2130
2131  SetupPendingTree(pending_pile);
2132
2133  ASSERT_TRUE(pending_layer_->CanHaveTilings());
2134
2135  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2136
2137  // Empty iterator
2138  PictureLayerImpl::LayerRasterTileIterator it;
2139  EXPECT_FALSE(it);
2140
2141  // No tilings.
2142  it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2143  EXPECT_FALSE(it);
2144
2145  pending_layer_->AddTiling(low_res_factor);
2146  pending_layer_->AddTiling(0.3f);
2147  pending_layer_->AddTiling(0.7f);
2148  PictureLayerTiling* high_res_tiling = pending_layer_->AddTiling(1.0f);
2149  pending_layer_->AddTiling(2.0f);
2150
2151  host_impl_.SetViewportSize(gfx::Size(500, 500));
2152  host_impl_.pending_tree()->UpdateDrawProperties();
2153
2154  std::set<Tile*> unique_tiles;
2155  bool reached_prepaint = false;
2156  size_t non_ideal_tile_count = 0u;
2157  size_t low_res_tile_count = 0u;
2158  size_t high_res_tile_count = 0u;
2159  for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2160       it;
2161       ++it) {
2162    Tile* tile = *it;
2163    TilePriority priority = tile->priority(PENDING_TREE);
2164
2165    EXPECT_TRUE(tile);
2166
2167    // Non-high res tiles only get visible tiles. Also, prepaint should only
2168    // come at the end of the iteration.
2169    if (priority.resolution != HIGH_RESOLUTION)
2170      EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2171    else if (reached_prepaint)
2172      EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2173    else
2174      reached_prepaint = priority.priority_bin != TilePriority::NOW;
2175
2176    non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2177    low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2178    high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2179
2180    unique_tiles.insert(tile);
2181  }
2182
2183  EXPECT_TRUE(reached_prepaint);
2184  EXPECT_EQ(0u, non_ideal_tile_count);
2185  EXPECT_EQ(1u, low_res_tile_count);
2186  EXPECT_EQ(16u, high_res_tile_count);
2187  EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2188            unique_tiles.size());
2189
2190  std::vector<Tile*> high_res_tiles = high_res_tiling->AllTilesForTesting();
2191  for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
2192       tile_it != high_res_tiles.end();
2193       ++tile_it) {
2194    Tile* tile = *tile_it;
2195    ManagedTileState::TileVersion& tile_version =
2196        tile->GetTileVersionForTesting(
2197            tile->DetermineRasterModeForTree(ACTIVE_TREE));
2198    tile_version.SetSolidColorForTesting(SK_ColorRED);
2199  }
2200
2201  non_ideal_tile_count = 0;
2202  low_res_tile_count = 0;
2203  high_res_tile_count = 0;
2204  for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2205       it;
2206       ++it) {
2207    Tile* tile = *it;
2208    TilePriority priority = tile->priority(PENDING_TREE);
2209
2210    EXPECT_TRUE(tile);
2211
2212    non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2213    low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2214    high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2215  }
2216
2217  EXPECT_EQ(0u, non_ideal_tile_count);
2218  EXPECT_EQ(1u, low_res_tile_count);
2219  EXPECT_EQ(0u, high_res_tile_count);
2220}
2221
2222TEST_F(PictureLayerImplTest, LayerEvictionTileIterator) {
2223  gfx::Size tile_size(100, 100);
2224  gfx::Size layer_bounds(1000, 1000);
2225
2226  scoped_refptr<FakePicturePileImpl> pending_pile =
2227      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2228
2229  SetupPendingTree(pending_pile);
2230
2231  ASSERT_TRUE(pending_layer_->CanHaveTilings());
2232
2233  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2234
2235  std::vector<PictureLayerTiling*> tilings;
2236  tilings.push_back(pending_layer_->AddTiling(low_res_factor));
2237  tilings.push_back(pending_layer_->AddTiling(0.3f));
2238  tilings.push_back(pending_layer_->AddTiling(0.7f));
2239  tilings.push_back(pending_layer_->AddTiling(1.0f));
2240  tilings.push_back(pending_layer_->AddTiling(2.0f));
2241
2242  host_impl_.SetViewportSize(gfx::Size(500, 500));
2243  host_impl_.pending_tree()->UpdateDrawProperties();
2244
2245  std::vector<Tile*> all_tiles;
2246  for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
2247           tilings.begin();
2248       tiling_iterator != tilings.end();
2249       ++tiling_iterator) {
2250    std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
2251    std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
2252  }
2253
2254  std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
2255
2256  bool mark_required = false;
2257  for (std::vector<Tile*>::iterator it = all_tiles.begin();
2258       it != all_tiles.end();
2259       ++it) {
2260    Tile* tile = *it;
2261    if (mark_required)
2262      tile->MarkRequiredForActivation();
2263    mark_required = !mark_required;
2264  }
2265
2266  // Sanity checks.
2267  EXPECT_EQ(91u, all_tiles.size());
2268  EXPECT_EQ(91u, all_tiles_set.size());
2269
2270  // Empty iterator.
2271  PictureLayerImpl::LayerEvictionTileIterator it;
2272  EXPECT_FALSE(it);
2273
2274  // Tiles don't have resources yet.
2275  it = PictureLayerImpl::LayerEvictionTileIterator(
2276      pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2277  EXPECT_FALSE(it);
2278
2279  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
2280
2281  std::set<Tile*> unique_tiles;
2282  float expected_scales[] = {2.0f, 0.3f, 0.7f, low_res_factor, 1.0f};
2283  size_t scale_index = 0;
2284  bool reached_visible = false;
2285  bool reached_required = false;
2286  Tile* last_tile = NULL;
2287  for (it = PictureLayerImpl::LayerEvictionTileIterator(
2288           pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2289       it;
2290       ++it) {
2291    Tile* tile = *it;
2292    if (!last_tile)
2293      last_tile = tile;
2294
2295    EXPECT_TRUE(tile);
2296
2297    TilePriority priority = tile->priority(PENDING_TREE);
2298
2299    if (priority.priority_bin == TilePriority::NOW) {
2300      reached_visible = true;
2301      last_tile = tile;
2302      break;
2303    }
2304
2305    if (reached_required) {
2306      EXPECT_TRUE(tile->required_for_activation());
2307    } else if (tile->required_for_activation()) {
2308      reached_required = true;
2309      scale_index = 0;
2310    }
2311
2312    while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2313           std::numeric_limits<float>::epsilon()) {
2314      ++scale_index;
2315      ASSERT_LT(scale_index, arraysize(expected_scales));
2316    }
2317
2318    EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2319    unique_tiles.insert(tile);
2320
2321    // If the tile is the same rough bin as last tile (same activation, bin, and
2322    // scale), then distance should be decreasing.
2323    if (tile->required_for_activation() ==
2324            last_tile->required_for_activation() &&
2325        priority.priority_bin ==
2326            last_tile->priority(PENDING_TREE).priority_bin &&
2327        std::abs(tile->contents_scale() - last_tile->contents_scale()) <
2328            std::numeric_limits<float>::epsilon()) {
2329      EXPECT_LE(priority.distance_to_visible,
2330                last_tile->priority(PENDING_TREE).distance_to_visible);
2331    }
2332
2333    last_tile = tile;
2334  }
2335
2336  EXPECT_TRUE(reached_visible);
2337  EXPECT_TRUE(reached_required);
2338  EXPECT_EQ(65u, unique_tiles.size());
2339
2340  scale_index = 0;
2341  reached_required = false;
2342  for (; it; ++it) {
2343    Tile* tile = *it;
2344    EXPECT_TRUE(tile);
2345
2346    TilePriority priority = tile->priority(PENDING_TREE);
2347    EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2348
2349    if (reached_required) {
2350      EXPECT_TRUE(tile->required_for_activation());
2351    } else if (tile->required_for_activation()) {
2352      reached_required = true;
2353      scale_index = 0;
2354    }
2355
2356    while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2357           std::numeric_limits<float>::epsilon()) {
2358      ++scale_index;
2359      ASSERT_LT(scale_index, arraysize(expected_scales));
2360    }
2361
2362    EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2363    unique_tiles.insert(tile);
2364  }
2365
2366  EXPECT_TRUE(reached_required);
2367  EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
2368}
2369
2370TEST_F(PictureLayerImplTest, Occlusion) {
2371  gfx::Size tile_size(102, 102);
2372  gfx::Size layer_bounds(1000, 1000);
2373  gfx::Size viewport_size(1000, 1000);
2374
2375  LayerTestCommon::LayerImplTest impl;
2376
2377  scoped_refptr<FakePicturePileImpl> pending_pile =
2378      FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
2379  SetupPendingTree(pending_pile);
2380  pending_layer_->SetBounds(layer_bounds);
2381  ActivateTree();
2382  active_layer_->set_fixed_tile_size(tile_size);
2383
2384  host_impl_.SetViewportSize(viewport_size);
2385  host_impl_.active_tree()->UpdateDrawProperties();
2386
2387  std::vector<Tile*> tiles =
2388      active_layer_->HighResTiling()->AllTilesForTesting();
2389  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
2390
2391  {
2392    SCOPED_TRACE("No occlusion");
2393    gfx::Rect occluded;
2394    impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2395
2396    LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
2397                                                 gfx::Rect(layer_bounds));
2398    EXPECT_EQ(100u, impl.quad_list().size());
2399  }
2400
2401  {
2402    SCOPED_TRACE("Full occlusion");
2403    gfx::Rect occluded(active_layer_->visible_content_rect());
2404    impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2405
2406    LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
2407    EXPECT_EQ(impl.quad_list().size(), 0u);
2408  }
2409
2410  {
2411    SCOPED_TRACE("Partial occlusion");
2412    gfx::Rect occluded(150, 0, 200, 1000);
2413    impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2414
2415    size_t partially_occluded_count = 0;
2416    LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
2417        impl.quad_list(),
2418        gfx::Rect(layer_bounds),
2419        occluded,
2420        &partially_occluded_count);
2421    // The layer outputs one quad, which is partially occluded.
2422    EXPECT_EQ(100u - 10u, impl.quad_list().size());
2423    EXPECT_EQ(10u + 10u, partially_occluded_count);
2424  }
2425}
2426
2427TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
2428  gfx::Size tile_size(host_impl_.settings().default_tile_size);
2429  SetupDefaultTrees(tile_size);
2430
2431  float contents_scale = 2.f;
2432  float device_scale = 1.f;
2433  float page_scale = 1.f;
2434  float maximum_animation_scale = 1.f;
2435  bool animating_transform = false;
2436
2437  SetContentsScaleOnBothLayers(contents_scale,
2438                               device_scale,
2439                               page_scale,
2440                               maximum_animation_scale,
2441                               animating_transform);
2442  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2443
2444  // Changing the source scale without being in an animation will cause
2445  // the layer to reset its source scale to 1.f.
2446  contents_scale = 3.f;
2447
2448  SetContentsScaleOnBothLayers(contents_scale,
2449                               device_scale,
2450                               page_scale,
2451                               maximum_animation_scale,
2452                               animating_transform);
2453  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2454
2455  // Further changes to the source scale will no longer be reflected in the
2456  // contents scale.
2457  contents_scale = 0.5f;
2458
2459  SetContentsScaleOnBothLayers(contents_scale,
2460                               device_scale,
2461                               page_scale,
2462                               maximum_animation_scale,
2463                               animating_transform);
2464  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2465}
2466
2467TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
2468  gfx::Size tile_size(100, 100);
2469  gfx::Size layer_bounds(1000, 1000);
2470
2471  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2472
2473  // Make sure some tiles are not shared.
2474  pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2475
2476  CreateHighLowResAndSetAllTilesVisible();
2477  active_layer_->SetAllTilesReady();
2478  pending_layer_->MarkVisibleResourcesAsRequired();
2479
2480  // All pending layer tiles required are not ready.
2481  EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2482
2483  // Initialize all low-res tiles.
2484  pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
2485
2486  // Low-res tiles should not be enough.
2487  EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2488
2489  // Initialize remaining tiles.
2490  pending_layer_->SetAllTilesReady();
2491
2492  EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2493}
2494
2495TEST_F(PictureLayerImplTest, HighResReadyToDrawNotEnoughToActivate) {
2496  gfx::Size tile_size(100, 100);
2497  gfx::Size layer_bounds(1000, 1000);
2498
2499  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2500
2501  // Make sure some tiles are not shared.
2502  pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2503
2504  CreateHighLowResAndSetAllTilesVisible();
2505  active_layer_->SetAllTilesReady();
2506  pending_layer_->MarkVisibleResourcesAsRequired();
2507
2508  // All pending layer tiles required are not ready.
2509  EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2510
2511  // Initialize all high-res tiles.
2512  pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
2513
2514  // High-res tiles should not be enough.
2515  EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2516
2517  // Initialize remaining tiles.
2518  pending_layer_->SetAllTilesReady();
2519
2520  EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2521}
2522
2523class NoLowResTilingsSettings : public ImplSidePaintingSettings {
2524 public:
2525  NoLowResTilingsSettings() { create_low_res_tiling = false; }
2526};
2527
2528class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
2529 public:
2530  NoLowResPictureLayerImplTest()
2531      : PictureLayerImplTest(NoLowResTilingsSettings()) {}
2532};
2533
2534TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
2535  gfx::Size tile_size(400, 400);
2536  gfx::Size layer_bounds(1300, 1900);
2537
2538  scoped_refptr<FakePicturePileImpl> pending_pile =
2539      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2540  scoped_refptr<FakePicturePileImpl> active_pile =
2541      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2542
2543  SetupTrees(pending_pile, active_pile);
2544  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2545
2546  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2547  EXPECT_LT(low_res_factor, 1.f);
2548
2549  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2550                                    6.f,  // ideal contents scale
2551                                    3.f,  // device scale
2552                                    2.f,  // page scale
2553                                    1.f,  // maximum animation scale
2554                                    false);
2555  ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
2556  EXPECT_FLOAT_EQ(6.f,
2557                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
2558
2559  // If we change the page scale factor, then we should get new tilings.
2560  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2561                                    6.6f,  // ideal contents scale
2562                                    3.f,   // device scale
2563                                    2.2f,  // page scale
2564                                    1.f,   // maximum animation scale
2565                                    false);
2566  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
2567  EXPECT_FLOAT_EQ(6.6f,
2568                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
2569
2570  // If we change the device scale factor, then we should get new tilings.
2571  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2572                                    7.26f,  // ideal contents scale
2573                                    3.3f,   // device scale
2574                                    2.2f,   // page scale
2575                                    1.f,    // maximum animation scale
2576                                    false);
2577  ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
2578  EXPECT_FLOAT_EQ(7.26f,
2579                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
2580
2581  // If we change the device scale factor, but end up at the same total scale
2582  // factor somehow, then we don't get new tilings.
2583  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2584                                    7.26f,  // ideal contents scale
2585                                    2.2f,   // device scale
2586                                    3.3f,   // page scale
2587                                    1.f,    // maximum animation scale
2588                                    false);
2589  ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
2590  EXPECT_FLOAT_EQ(7.26f,
2591                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
2592}
2593
2594TEST_F(NoLowResPictureLayerImplTest, MarkRequiredNullTiles) {
2595  gfx::Size tile_size(100, 100);
2596  gfx::Size layer_bounds(1000, 1000);
2597
2598  scoped_refptr<FakePicturePileImpl> pending_pile =
2599      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
2600  // Layers with entirely empty piles can't get tilings.
2601  pending_pile->AddRecordingAt(0, 0);
2602
2603  SetupPendingTree(pending_pile);
2604
2605  ASSERT_TRUE(pending_layer_->CanHaveTilings());
2606  pending_layer_->AddTiling(1.0f);
2607  pending_layer_->AddTiling(2.0f);
2608
2609  // It should be safe to call this (and MarkVisibleResourcesAsRequired)
2610  // on a layer with no recordings.
2611  host_impl_.pending_tree()->UpdateDrawProperties();
2612  pending_layer_->MarkVisibleResourcesAsRequired();
2613}
2614
2615TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
2616  gfx::Size layer_bounds(400, 400);
2617  gfx::Size tile_size(100, 100);
2618  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2619
2620  CreateHighLowResAndSetAllTilesVisible();
2621
2622  Tile* some_active_tile =
2623      active_layer_->HighResTiling()->AllTilesForTesting()[0];
2624  EXPECT_FALSE(some_active_tile->IsReadyToDraw());
2625
2626  // All tiles shared (no invalidation), so even though the active tree's
2627  // tiles aren't ready, there is nothing required.
2628  pending_layer_->MarkVisibleResourcesAsRequired();
2629  AssertNoTilesRequired(pending_layer_->HighResTiling());
2630  if (host_impl_.settings().create_low_res_tiling) {
2631    AssertNoTilesRequired(pending_layer_->LowResTiling());
2632  }
2633}
2634
2635TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
2636  gfx::Size layer_bounds(400, 400);
2637  gfx::Size tile_size(100, 100);
2638  scoped_refptr<FakePicturePileImpl> pending_pile =
2639      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2640  // This pile will create tilings, but has no recordings so will not create any
2641  // tiles.  This is attempting to simulate scrolling past the end of recorded
2642  // content on the active layer, where the recordings are so far away that
2643  // no tiles are created.
2644  scoped_refptr<FakePicturePileImpl> active_pile =
2645      FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
2646          tile_size, layer_bounds);
2647  SetupTrees(pending_pile, active_pile);
2648  pending_layer_->set_fixed_tile_size(tile_size);
2649  active_layer_->set_fixed_tile_size(tile_size);
2650
2651  CreateHighLowResAndSetAllTilesVisible();
2652
2653  // Active layer has tilings, but no tiles due to missing recordings.
2654  EXPECT_TRUE(active_layer_->CanHaveTilings());
2655  EXPECT_EQ(active_layer_->tilings()->num_tilings(),
2656            host_impl_.settings().create_low_res_tiling ? 2u : 1u);
2657  EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
2658
2659  // Since the active layer has no tiles at all, the pending layer doesn't
2660  // need content in order to activate.
2661  pending_layer_->MarkVisibleResourcesAsRequired();
2662  AssertNoTilesRequired(pending_layer_->HighResTiling());
2663  if (host_impl_.settings().create_low_res_tiling)
2664    AssertNoTilesRequired(pending_layer_->LowResTiling());
2665}
2666
2667TEST_F(NoLowResPictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
2668  base::TimeTicks time_ticks;
2669  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2670
2671  gfx::Size tile_size(100, 100);
2672  gfx::Size layer_bounds(400, 400);
2673
2674  scoped_refptr<FakePicturePileImpl> pending_pile =
2675      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2676  scoped_refptr<FakePicturePileImpl> active_pile =
2677      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2678
2679  SetupTrees(pending_pile, active_pile);
2680
2681  Region invalidation;
2682  AddDefaultTilingsWithInvalidation(invalidation);
2683  SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
2684
2685  // UpdateTiles with valid viewport. Should update tile viewport.
2686  // Note viewport is considered invalid if and only if in resourceless
2687  // software draw.
2688  bool resourceless_software_draw = false;
2689  gfx::Rect viewport = gfx::Rect(layer_bounds);
2690  gfx::Transform transform;
2691  host_impl_.SetExternalDrawConstraints(transform,
2692                                        viewport,
2693                                        viewport,
2694                                        viewport,
2695                                        transform,
2696                                        resourceless_software_draw);
2697  active_layer_->draw_properties().visible_content_rect = viewport;
2698  active_layer_->draw_properties().screen_space_transform = transform;
2699  active_layer_->UpdateTiles();
2700
2701  gfx::Rect visible_rect_for_tile_priority =
2702      active_layer_->visible_rect_for_tile_priority();
2703  EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
2704  gfx::Rect viewport_rect_for_tile_priority =
2705      active_layer_->viewport_rect_for_tile_priority();
2706  EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
2707  gfx::Transform screen_space_transform_for_tile_priority =
2708      active_layer_->screen_space_transform_for_tile_priority();
2709
2710  // Expand viewport and set it as invalid for prioritizing tiles.
2711  // Should not update tile viewport.
2712  time_ticks += base::TimeDelta::FromMilliseconds(200);
2713  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2714  resourceless_software_draw = true;
2715  viewport = gfx::ScaleToEnclosingRect(viewport, 2);
2716  transform.Translate(1.f, 1.f);
2717  active_layer_->draw_properties().visible_content_rect = viewport;
2718  active_layer_->draw_properties().screen_space_transform = transform;
2719  host_impl_.SetExternalDrawConstraints(transform,
2720                                        viewport,
2721                                        viewport,
2722                                        viewport,
2723                                        transform,
2724                                        resourceless_software_draw);
2725  active_layer_->UpdateTiles();
2726
2727  EXPECT_RECT_EQ(visible_rect_for_tile_priority,
2728                 active_layer_->visible_rect_for_tile_priority());
2729  EXPECT_RECT_EQ(viewport_rect_for_tile_priority,
2730                 active_layer_->viewport_rect_for_tile_priority());
2731  EXPECT_TRANSFORMATION_MATRIX_EQ(
2732      screen_space_transform_for_tile_priority,
2733      active_layer_->screen_space_transform_for_tile_priority());
2734
2735  // Keep expanded viewport but mark it valid. Should update tile viewport.
2736  time_ticks += base::TimeDelta::FromMilliseconds(200);
2737  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2738  resourceless_software_draw = false;
2739  host_impl_.SetExternalDrawConstraints(transform,
2740                                        viewport,
2741                                        viewport,
2742                                        viewport,
2743                                        transform,
2744                                        resourceless_software_draw);
2745  active_layer_->UpdateTiles();
2746
2747  EXPECT_FALSE(visible_rect_for_tile_priority ==
2748               active_layer_->visible_rect_for_tile_priority());
2749  EXPECT_FALSE(viewport_rect_for_tile_priority ==
2750               active_layer_->viewport_rect_for_tile_priority());
2751  EXPECT_FALSE(screen_space_transform_for_tile_priority ==
2752               active_layer_->screen_space_transform_for_tile_priority());
2753}
2754
2755TEST_F(NoLowResPictureLayerImplTest, InvalidViewportAfterReleaseResources) {
2756  base::TimeTicks time_ticks;
2757  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2758
2759  gfx::Size tile_size(100, 100);
2760  gfx::Size layer_bounds(400, 400);
2761
2762  scoped_refptr<FakePicturePileImpl> pending_pile =
2763      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2764  scoped_refptr<FakePicturePileImpl> active_pile =
2765      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2766
2767  SetupTrees(pending_pile, active_pile);
2768
2769  Region invalidation;
2770  AddDefaultTilingsWithInvalidation(invalidation);
2771
2772  bool resourceless_software_draw = true;
2773  gfx::Rect viewport = gfx::Rect(layer_bounds);
2774  gfx::Transform identity = gfx::Transform();
2775  host_impl_.SetExternalDrawConstraints(identity,
2776                                        viewport,
2777                                        viewport,
2778                                        viewport,
2779                                        identity,
2780                                        resourceless_software_draw);
2781  ResetTilingsAndRasterScales();
2782  host_impl_.pending_tree()->UpdateDrawProperties();
2783  host_impl_.active_tree()->UpdateDrawProperties();
2784  EXPECT_TRUE(active_layer_->HighResTiling());
2785
2786  size_t num_tilings = active_layer_->num_tilings();
2787  active_layer_->UpdateTiles();
2788  pending_layer_->AddTiling(0.5f);
2789  EXPECT_EQ(num_tilings + 1, active_layer_->num_tilings());
2790}
2791
2792TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
2793  gfx::Size tile_size(400, 400);
2794  gfx::Size layer_bounds(1300, 1900);
2795
2796  scoped_refptr<FakePicturePileImpl> pending_pile =
2797      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2798  scoped_refptr<FakePicturePileImpl> active_pile =
2799      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2800
2801  std::vector<PictureLayerTiling*> used_tilings;
2802
2803  SetupTrees(pending_pile, active_pile);
2804  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2805
2806  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2807  EXPECT_LT(low_res_factor, 1.f);
2808
2809  float device_scale = 1.7f;
2810  float page_scale = 3.2f;
2811  float scale = 1.f;
2812
2813  SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
2814  ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2815
2816  // We only have ideal tilings, so they aren't removed.
2817  used_tilings.clear();
2818  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2819  ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2820
2821  host_impl_.PinchGestureBegin();
2822
2823  // Changing the ideal but not creating new tilings.
2824  scale *= 1.5f;
2825  page_scale *= 1.5f;
2826  SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
2827  ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2828
2829  // The tilings are still our target scale, so they aren't removed.
2830  used_tilings.clear();
2831  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2832  ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2833
2834  host_impl_.PinchGestureEnd();
2835
2836  // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
2837  scale /= 4.f;
2838  page_scale /= 4.f;
2839  SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
2840  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2841  EXPECT_FLOAT_EQ(1.f,
2842                  active_layer_->tilings()->tiling_at(1)->contents_scale());
2843
2844  // Mark the non-ideal tilings as used. They won't be removed.
2845  used_tilings.clear();
2846  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
2847  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2848  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2849
2850  // Now move the ideal scale to 0.5. Our target stays 1.2.
2851  SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
2852
2853  // The high resolution tiling is between target and ideal, so is not
2854  // removed.  The low res tiling for the old ideal=1.0 scale is removed.
2855  used_tilings.clear();
2856  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2857  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2858
2859  // Now move the ideal scale to 1.0. Our target stays 1.2.
2860  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
2861
2862  // All the tilings are between are target and the ideal, so they are not
2863  // removed.
2864  used_tilings.clear();
2865  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2866  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2867
2868  // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
2869  SetupDrawPropertiesAndUpdateTiles(
2870      active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
2871
2872  // Because the pending layer's ideal scale is still 1.0, our tilings fall
2873  // in the range [1.0,1.2] and are kept.
2874  used_tilings.clear();
2875  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2876  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2877
2878  // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
2879  // 1.2 still.
2880  SetupDrawPropertiesAndUpdateTiles(
2881      pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
2882
2883  // Our 1.0 tiling now falls outside the range between our ideal scale and our
2884  // target raster scale. But it is in our used tilings set, so nothing is
2885  // deleted.
2886  used_tilings.clear();
2887  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
2888  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2889  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2890
2891  // If we remove it from our used tilings set, it is outside the range to keep
2892  // so it is deleted.
2893  used_tilings.clear();
2894  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2895  ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2896}
2897
2898TEST_F(PictureLayerImplTest, ScaleCollision) {
2899  gfx::Size tile_size(400, 400);
2900  gfx::Size layer_bounds(1300, 1900);
2901
2902  scoped_refptr<FakePicturePileImpl> pending_pile =
2903      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2904  scoped_refptr<FakePicturePileImpl> active_pile =
2905      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2906
2907  std::vector<PictureLayerTiling*> used_tilings;
2908
2909  SetupTrees(pending_pile, active_pile);
2910
2911  float pending_contents_scale = 1.f;
2912  float active_contents_scale = 2.f;
2913  float device_scale_factor = 1.f;
2914  float page_scale_factor = 1.f;
2915  float maximum_animation_contents_scale = 1.f;
2916  bool animating_transform = false;
2917
2918  EXPECT_TRUE(host_impl_.settings().create_low_res_tiling);
2919  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2920  EXPECT_LT(low_res_factor, 1.f);
2921
2922  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2923                                    pending_contents_scale,
2924                                    device_scale_factor,
2925                                    page_scale_factor,
2926                                    maximum_animation_contents_scale,
2927                                    animating_transform);
2928  SetupDrawPropertiesAndUpdateTiles(active_layer_,
2929                                    active_contents_scale,
2930                                    device_scale_factor,
2931                                    page_scale_factor,
2932                                    maximum_animation_contents_scale,
2933                                    animating_transform);
2934
2935  ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
2936  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
2937
2938  EXPECT_EQ(active_contents_scale,
2939            pending_layer_->tilings()->tiling_at(0)->contents_scale());
2940  EXPECT_EQ(pending_contents_scale,
2941            pending_layer_->tilings()->tiling_at(1)->contents_scale());
2942  EXPECT_EQ(active_contents_scale * low_res_factor,
2943            pending_layer_->tilings()->tiling_at(2)->contents_scale());
2944  EXPECT_EQ(pending_contents_scale * low_res_factor,
2945            pending_layer_->tilings()->tiling_at(3)->contents_scale());
2946
2947  EXPECT_EQ(active_contents_scale,
2948            active_layer_->tilings()->tiling_at(0)->contents_scale());
2949  EXPECT_EQ(pending_contents_scale,
2950            active_layer_->tilings()->tiling_at(1)->contents_scale());
2951  EXPECT_EQ(active_contents_scale * low_res_factor,
2952            active_layer_->tilings()->tiling_at(2)->contents_scale());
2953  EXPECT_EQ(pending_contents_scale * low_res_factor,
2954            active_layer_->tilings()->tiling_at(3)->contents_scale());
2955
2956  // The unused low res tiling from the pending tree must be kept or we may add
2957  // it again on the active tree and collide with the pending tree.
2958  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
2959  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2960  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
2961
2962  EXPECT_EQ(active_contents_scale,
2963            active_layer_->tilings()->tiling_at(0)->contents_scale());
2964  EXPECT_EQ(pending_contents_scale,
2965            active_layer_->tilings()->tiling_at(1)->contents_scale());
2966  EXPECT_EQ(active_contents_scale * low_res_factor,
2967            active_layer_->tilings()->tiling_at(2)->contents_scale());
2968  EXPECT_EQ(pending_contents_scale * low_res_factor,
2969            active_layer_->tilings()->tiling_at(3)->contents_scale());
2970}
2971
2972TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
2973  gfx::Size tile_size(400, 400);
2974  gfx::Size layer_bounds(1300, 1900);
2975
2976  scoped_refptr<FakePicturePileImpl> pending_pile =
2977      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2978  scoped_refptr<FakePicturePileImpl> active_pile =
2979      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2980
2981  SetupTrees(pending_pile, active_pile);
2982  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2983
2984  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2985                                    1.3f,  // ideal contents scale
2986                                    2.7f,  // device scale
2987                                    3.2f,  // page scale
2988                                    1.f,   // maximum animation scale
2989                                    false);
2990  EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2991
2992  // All tilings should be removed when losing output surface.
2993  active_layer_->ReleaseResources();
2994  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2995  pending_layer_->ReleaseResources();
2996  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2997
2998  // This should create new tilings.
2999  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3000                                    1.3f,  // ideal contents scale
3001                                    2.7f,  // device scale
3002                                    3.2f,  // page scale
3003                                    1.f,   // maximum animation scale
3004                                    false);
3005  EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3006}
3007
3008TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3009  MockOcclusionTracker<LayerImpl> occlusion_tracker;
3010  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3011  MockQuadCuller quad_culler(render_pass.get(), &occlusion_tracker);
3012
3013  gfx::Size tile_size(400, 400);
3014  gfx::Size layer_bounds(1000, 2000);
3015
3016  scoped_refptr<FakePicturePileImpl> pending_pile =
3017      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3018  scoped_refptr<FakePicturePileImpl> active_pile =
3019      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3020
3021  SetupTrees(pending_pile, active_pile);
3022
3023  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 2.5f, 1.f, 1.f, 1.f, false);
3024  host_impl_.pending_tree()->UpdateDrawProperties();
3025
3026  active_layer_->draw_properties().visible_content_rect =
3027      gfx::Rect(layer_bounds);
3028  host_impl_.active_tree()->UpdateDrawProperties();
3029
3030  float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3031  gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3032  scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3033                              SK_MScalar1 / max_contents_scale);
3034
3035  AppendQuadsData data;
3036  active_layer_->AppendQuads(&quad_culler, &data);
3037
3038  // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3039  EXPECT_EQ(1u, quad_culler.shared_quad_state_list().size());
3040  // The content_to_target_transform should be scaled by the
3041  // MaximumTilingContentsScale on the layer.
3042  EXPECT_EQ(scaled_draw_transform.ToString(),
3043            quad_culler.shared_quad_state_list()[0]
3044                ->content_to_target_transform.ToString());
3045  // The content_bounds should be scaled by the
3046  // MaximumTilingContentsScale on the layer.
3047  EXPECT_EQ(gfx::Size(2500u, 5000u).ToString(),
3048            quad_culler.shared_quad_state_list()[0]->content_bounds.ToString());
3049  // The visible_content_rect should be scaled by the
3050  // MaximumTilingContentsScale on the layer.
3051  EXPECT_EQ(
3052      gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3053      quad_culler.shared_quad_state_list()[0]->visible_content_rect.ToString());
3054}
3055
3056TEST_F(PictureLayerImplTest, UpdateTilesForMasksWithNoVisibleContent) {
3057  gfx::Size tile_size(400, 400);
3058  gfx::Size bounds(100000, 100);
3059
3060  host_impl_.CreatePendingTree();
3061
3062  scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_.pending_tree(), 1);
3063
3064  scoped_ptr<FakePictureLayerImpl> layer_with_mask =
3065      FakePictureLayerImpl::Create(host_impl_.pending_tree(), 2);
3066
3067  layer_with_mask->SetBounds(bounds);
3068  layer_with_mask->SetContentBounds(bounds);
3069
3070  scoped_refptr<FakePicturePileImpl> pending_pile =
3071      FakePicturePileImpl::CreateFilledPile(tile_size, bounds);
3072  scoped_ptr<FakePictureLayerImpl> mask = FakePictureLayerImpl::CreateWithPile(
3073      host_impl_.pending_tree(), 3, pending_pile);
3074
3075  mask->SetIsMask(true);
3076  mask->SetBounds(bounds);
3077  mask->SetContentBounds(bounds);
3078  mask->SetDrawsContent(true);
3079
3080  FakePictureLayerImpl* pending_mask_content = mask.get();
3081  layer_with_mask->SetMaskLayer(mask.PassAs<LayerImpl>());
3082
3083  scoped_ptr<FakePictureLayerImpl> child_of_layer_with_mask =
3084      FakePictureLayerImpl::Create(host_impl_.pending_tree(), 4);
3085
3086  child_of_layer_with_mask->SetBounds(bounds);
3087  child_of_layer_with_mask->SetContentBounds(bounds);
3088  child_of_layer_with_mask->SetDrawsContent(true);
3089
3090  layer_with_mask->AddChild(child_of_layer_with_mask.PassAs<LayerImpl>());
3091
3092  root->AddChild(layer_with_mask.PassAs<LayerImpl>());
3093
3094  host_impl_.pending_tree()->SetRootLayer(root.Pass());
3095
3096  EXPECT_FALSE(pending_mask_content->tilings());
3097  host_impl_.pending_tree()->UpdateDrawProperties();
3098  EXPECT_NE(0u, pending_mask_content->num_tilings());
3099}
3100
3101}  // namespace
3102}  // namespace cc
3103