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