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