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