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