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