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