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  maximum_animation_scale = 2.f;
1116  animating_transform = true;
1117  SetContentsScaleOnBothLayers(contents_scale,
1118                               device_scale,
1119                               page_scale,
1120                               maximum_animation_scale,
1121                               animating_transform);
1122  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1123  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1124  EXPECT_BOTH_EQ(num_tilings(), 3u);
1125
1126  // Stop animating, new low res gets created for final page scale.
1127  animating_transform = false;
1128  SetContentsScaleOnBothLayers(contents_scale,
1129                               device_scale,
1130                               page_scale,
1131                               maximum_animation_scale,
1132                               animating_transform);
1133  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1134  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1135  EXPECT_BOTH_EQ(num_tilings(), 4u);
1136}
1137
1138TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1139  gfx::Size tile_size(host_impl_.settings().default_tile_size);
1140  SetupDefaultTrees(tile_size);
1141
1142  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1143  float device_scale = 1.f;
1144  float page_scale = 1.f;
1145  float maximum_animation_scale = 1.f;
1146  bool animating_transform = false;
1147
1148  // Contents exactly fit on one tile at scale 1, no low res.
1149  float contents_scale = 1.f;
1150  SetContentsScaleOnBothLayers(contents_scale,
1151                               device_scale,
1152                               page_scale,
1153                               maximum_animation_scale,
1154                               animating_transform);
1155  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1156  EXPECT_BOTH_EQ(num_tilings(), 1u);
1157
1158  ResetTilingsAndRasterScales();
1159
1160  // Contents that are smaller than one tile, no low res.
1161  contents_scale = 0.123f;
1162  SetContentsScaleOnBothLayers(contents_scale,
1163                               device_scale,
1164                               page_scale,
1165                               maximum_animation_scale,
1166                               animating_transform);
1167  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1168  EXPECT_BOTH_EQ(num_tilings(), 1u);
1169
1170  ResetTilingsAndRasterScales();
1171
1172  // Any content bounds that would create more than one tile will
1173  // generate a low res tiling.
1174  contents_scale = 2.5f;
1175  SetContentsScaleOnBothLayers(contents_scale,
1176                               device_scale,
1177                               page_scale,
1178                               maximum_animation_scale,
1179                               animating_transform);
1180  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1181  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1182                 contents_scale * low_res_factor);
1183  EXPECT_BOTH_EQ(num_tilings(), 2u);
1184
1185  ResetTilingsAndRasterScales();
1186
1187  // Mask layers dont create low res since they always fit on one tile.
1188  pending_layer_->pile()->set_is_mask(true);
1189  active_layer_->pile()->set_is_mask(true);
1190  SetContentsScaleOnBothLayers(contents_scale,
1191                               device_scale,
1192                               page_scale,
1193                               maximum_animation_scale,
1194                               animating_transform);
1195  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1196  EXPECT_BOTH_EQ(num_tilings(), 1u);
1197}
1198
1199TEST_F(PictureLayerImplTest, HugeMasksDontGetTiles) {
1200  gfx::Size tile_size(100, 100);
1201
1202  scoped_refptr<FakePicturePileImpl> valid_pile =
1203      FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1000, 1000));
1204  valid_pile->set_is_mask(true);
1205  SetupPendingTree(valid_pile);
1206
1207  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1208  EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1209  EXPECT_EQ(1u, pending_layer_->num_tilings());
1210
1211  pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1212  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1213      pending_layer_->HighResTiling()->AllTilesForTesting());
1214
1215  ActivateTree();
1216
1217  // Mask layers have a tiling with a single tile in it.
1218  EXPECT_EQ(1u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1219  // The mask resource exists.
1220  EXPECT_NE(0u, active_layer_->ContentsResourceId());
1221
1222  // Resize larger than the max texture size.
1223  int max_texture_size = host_impl_.GetRendererCapabilities().max_texture_size;
1224  scoped_refptr<FakePicturePileImpl> huge_pile =
1225      FakePicturePileImpl::CreateFilledPile(
1226          tile_size, gfx::Size(max_texture_size + 1, 10));
1227  huge_pile->set_is_mask(true);
1228  SetupPendingTree(huge_pile);
1229
1230  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1231  EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1232  EXPECT_EQ(1u, pending_layer_->num_tilings());
1233
1234  pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1235  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1236      pending_layer_->HighResTiling()->AllTilesForTesting());
1237
1238  ActivateTree();
1239
1240  // Mask layers have a tiling, but there should be no tiles in it.
1241  EXPECT_EQ(0u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1242  // The mask resource is empty.
1243  EXPECT_EQ(0u, active_layer_->ContentsResourceId());
1244}
1245
1246TEST_F(PictureLayerImplTest, ReleaseResources) {
1247  gfx::Size tile_size(400, 400);
1248  gfx::Size layer_bounds(1300, 1900);
1249
1250  scoped_refptr<FakePicturePileImpl> pending_pile =
1251      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1252  scoped_refptr<FakePicturePileImpl> active_pile =
1253      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1254
1255  SetupTrees(pending_pile, active_pile);
1256  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1257
1258  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1259                                    1.3f,  // ideal contents scale
1260                                    2.7f,  // device scale
1261                                    3.2f,  // page scale
1262                                    1.f,   // maximum animation scale
1263                                    false);
1264  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1265
1266  // All tilings should be removed when losing output surface.
1267  active_layer_->ReleaseResources();
1268  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1269  pending_layer_->ReleaseResources();
1270  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1271
1272  // This should create new tilings.
1273  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1274                                    1.3f,  // ideal contents scale
1275                                    2.7f,  // device scale
1276                                    3.2f,  // page scale
1277                                    1.f,   // maximum animation scale
1278                                    false);
1279  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1280}
1281
1282TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1283  // The default max tile size is larger than 400x400.
1284  gfx::Size tile_size(400, 400);
1285  gfx::Size layer_bounds(5000, 5000);
1286
1287  scoped_refptr<FakePicturePileImpl> pending_pile =
1288      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1289  scoped_refptr<FakePicturePileImpl> active_pile =
1290      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1291
1292  SetupTrees(pending_pile, active_pile);
1293  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1294
1295  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1296  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1297
1298  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1299
1300  // The default value.
1301  EXPECT_EQ(gfx::Size(256, 256).ToString(),
1302            host_impl_.settings().default_tile_size.ToString());
1303
1304  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1305  EXPECT_EQ(gfx::Size(256, 256).ToString(),
1306            tile->content_rect().size().ToString());
1307
1308  pending_layer_->ReleaseResources();
1309
1310  // Change the max texture size on the output surface context.
1311  scoped_ptr<TestWebGraphicsContext3D> context =
1312      TestWebGraphicsContext3D::Create();
1313  context->set_max_texture_size(140);
1314  host_impl_.DidLoseOutputSurface();
1315  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1316      context.Pass()).PassAs<OutputSurface>());
1317
1318  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1319  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1320
1321  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1322
1323  // Verify the tiles are not larger than the context's max texture size.
1324  tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1325  EXPECT_GE(140, tile->content_rect().width());
1326  EXPECT_GE(140, tile->content_rect().height());
1327}
1328
1329TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1330  // The default max tile size is larger than 400x400.
1331  gfx::Size tile_size(400, 400);
1332  gfx::Size layer_bounds(500, 500);
1333
1334  scoped_refptr<FakePicturePileImpl> pending_pile =
1335      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1336  scoped_refptr<FakePicturePileImpl> active_pile =
1337      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1338
1339  SetupTrees(pending_pile, active_pile);
1340  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1341
1342  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1343  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1344
1345  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1346
1347  // The default value. The layer is smaller than this.
1348  EXPECT_EQ(gfx::Size(512, 512).ToString(),
1349            host_impl_.settings().max_untiled_layer_size.ToString());
1350
1351  // There should be a single tile since the layer is small.
1352  PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1353  EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1354
1355  pending_layer_->ReleaseResources();
1356
1357  // Change the max texture size on the output surface context.
1358  scoped_ptr<TestWebGraphicsContext3D> context =
1359      TestWebGraphicsContext3D::Create();
1360  context->set_max_texture_size(140);
1361  host_impl_.DidLoseOutputSurface();
1362  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1363      context.Pass()).PassAs<OutputSurface>());
1364
1365  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1366  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1367
1368  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1369
1370  // There should be more than one tile since the max texture size won't cover
1371  // the layer.
1372  high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1373  EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1374
1375  // Verify the tiles are not larger than the context's max texture size.
1376  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1377  EXPECT_GE(140, tile->content_rect().width());
1378  EXPECT_GE(140, tile->content_rect().height());
1379}
1380
1381TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1382  MockOcclusionTracker<LayerImpl> occlusion_tracker;
1383  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1384
1385  gfx::Size tile_size(400, 400);
1386  gfx::Size layer_bounds(1300, 1900);
1387
1388  scoped_refptr<FakePicturePileImpl> pending_pile =
1389      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1390  scoped_refptr<FakePicturePileImpl> active_pile =
1391      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1392
1393  SetupTrees(pending_pile, active_pile);
1394
1395  active_layer_->draw_properties().visible_content_rect =
1396      gfx::Rect(layer_bounds);
1397
1398  gfx::Rect layer_invalidation(150, 200, 30, 180);
1399  Region invalidation(layer_invalidation);
1400  AddDefaultTilingsWithInvalidation(invalidation);
1401
1402  AppendQuadsData data;
1403  active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
1404  active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1405  active_layer_->DidDraw(NULL);
1406
1407  ASSERT_EQ(1U, render_pass->quad_list.size());
1408  EXPECT_EQ(DrawQuad::PICTURE_CONTENT,
1409            render_pass->quad_list.front()->material);
1410}
1411
1412TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
1413  gfx::Size tile_size(100, 100);
1414  gfx::Size layer_bounds(1000, 1000);
1415
1416  scoped_refptr<FakePicturePileImpl> pending_pile =
1417      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1418  // Layers with entirely empty piles can't get tilings.
1419  pending_pile->AddRecordingAt(0, 0);
1420
1421  SetupPendingTree(pending_pile);
1422
1423  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1424  pending_layer_->AddTiling(1.0f);
1425  pending_layer_->AddTiling(2.0f);
1426
1427  // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1428  // on a layer with no recordings.
1429  host_impl_.pending_tree()->UpdateDrawProperties();
1430  pending_layer_->MarkVisibleResourcesAsRequired();
1431}
1432
1433TEST_F(PictureLayerImplTest, TileScalesWithSolidColorPile) {
1434  gfx::Size layer_bounds(200, 200);
1435  gfx::Size tile_size(host_impl_.settings().default_tile_size);
1436  scoped_refptr<FakePicturePileImpl> pending_pile =
1437      FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1438          tile_size, layer_bounds);
1439  scoped_refptr<FakePicturePileImpl> active_pile =
1440      FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1441          tile_size, layer_bounds);
1442
1443  pending_pile->set_is_solid_color(false);
1444  active_pile->set_is_solid_color(true);
1445  SetupTrees(pending_pile, active_pile);
1446  // Solid color layer should not have tilings.
1447  ASSERT_FALSE(active_layer_->CanHaveTilings());
1448
1449  // Update properties with solid color pile should not allow tilings at any
1450  // scale.
1451  host_impl_.active_tree()->UpdateDrawProperties();
1452  EXPECT_FALSE(active_layer_->CanHaveTilings());
1453  EXPECT_EQ(0.f, active_layer_->ideal_contents_scale());
1454
1455  // Push non-solid-color pending pile makes active layer can have tilings.
1456  active_layer_->UpdatePile(pending_pile);
1457  ASSERT_TRUE(active_layer_->CanHaveTilings());
1458
1459  // Update properties with non-solid color pile should allow tilings.
1460  host_impl_.active_tree()->UpdateDrawProperties();
1461  EXPECT_TRUE(active_layer_->CanHaveTilings());
1462  EXPECT_GT(active_layer_->ideal_contents_scale(), 0.f);
1463}
1464
1465TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1466  gfx::Size tile_size(100, 100);
1467  gfx::Size layer_bounds(200, 200);
1468
1469  scoped_refptr<FakePicturePileImpl> pending_pile =
1470      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1471  SetupPendingTree(pending_pile);
1472
1473  gfx::Transform transform;
1474  gfx::Transform transform_for_tile_priority;
1475  bool resourceless_software_draw = false;
1476  gfx::Rect viewport(0, 0, 100, 200);
1477  host_impl_.SetExternalDrawConstraints(transform,
1478                                        viewport,
1479                                        viewport,
1480                                        viewport,
1481                                        transform,
1482                                        resourceless_software_draw);
1483
1484  pending_layer_->set_fixed_tile_size(tile_size);
1485  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1486  PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1487  host_impl_.pending_tree()->UpdateDrawProperties();
1488  EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1489  EXPECT_EQ(viewport, pending_layer_->visible_rect_for_tile_priority());
1490
1491  // Fake set priorities.
1492  for (PictureLayerTiling::CoverageIterator iter(
1493           tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1494       iter;
1495       ++iter) {
1496    if (!*iter)
1497      continue;
1498    Tile* tile = *iter;
1499    TilePriority priority;
1500    priority.resolution = HIGH_RESOLUTION;
1501    gfx::Rect tile_bounds = iter.geometry_rect();
1502    if (pending_layer_->visible_rect_for_tile_priority().Intersects(
1503            tile_bounds)) {
1504      priority.priority_bin = TilePriority::NOW;
1505      priority.distance_to_visible = 0.f;
1506    } else {
1507      priority.priority_bin = TilePriority::SOON;
1508      priority.distance_to_visible = 1.f;
1509    }
1510    tile->SetPriority(PENDING_TREE, priority);
1511  }
1512
1513  pending_layer_->MarkVisibleResourcesAsRequired();
1514
1515  int num_visible = 0;
1516  int num_offscreen = 0;
1517
1518  for (PictureLayerTiling::CoverageIterator iter(
1519           tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1520       iter;
1521       ++iter) {
1522    if (!*iter)
1523      continue;
1524    const Tile* tile = *iter;
1525    if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1526      EXPECT_TRUE(tile->required_for_activation());
1527      num_visible++;
1528    } else {
1529      EXPECT_FALSE(tile->required_for_activation());
1530      num_offscreen++;
1531    }
1532  }
1533
1534  EXPECT_GT(num_visible, 0);
1535  EXPECT_GT(num_offscreen, 0);
1536}
1537
1538TEST_F(PictureLayerImplTest, TileOutsideOfViewportForTilePriorityNotRequired) {
1539  base::TimeTicks time_ticks;
1540  time_ticks += base::TimeDelta::FromMilliseconds(1);
1541  host_impl_.SetCurrentBeginFrameArgs(
1542      CreateBeginFrameArgsForTesting(time_ticks));
1543
1544  gfx::Size tile_size(100, 100);
1545  gfx::Size layer_bounds(400, 400);
1546  gfx::Rect external_viewport_for_tile_priority(400, 200);
1547  gfx::Rect visible_content_rect(200, 400);
1548
1549  scoped_refptr<FakePicturePileImpl> active_pile =
1550      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1551  scoped_refptr<FakePicturePileImpl> pending_pile =
1552      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1553  SetupTrees(pending_pile, active_pile);
1554
1555  active_layer_->set_fixed_tile_size(tile_size);
1556  pending_layer_->set_fixed_tile_size(tile_size);
1557  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1558  PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1559
1560  // Set external viewport for tile priority.
1561  gfx::Rect viewport = gfx::Rect(layer_bounds);
1562  gfx::Transform transform;
1563  gfx::Transform transform_for_tile_priority;
1564  bool resourceless_software_draw = false;
1565  host_impl_.SetExternalDrawConstraints(transform,
1566                                        viewport,
1567                                        viewport,
1568                                        external_viewport_for_tile_priority,
1569                                        transform_for_tile_priority,
1570                                        resourceless_software_draw);
1571  host_impl_.pending_tree()->UpdateDrawProperties();
1572
1573  // Set visible content rect that is different from
1574  // external_viewport_for_tile_priority.
1575  pending_layer_->draw_properties().visible_content_rect = visible_content_rect;
1576  time_ticks += base::TimeDelta::FromMilliseconds(200);
1577  host_impl_.SetCurrentBeginFrameArgs(
1578      CreateBeginFrameArgsForTesting(time_ticks));
1579  pending_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
1580
1581  pending_layer_->MarkVisibleResourcesAsRequired();
1582
1583  // Intersect the two rects. Any tile outside should not be required for
1584  // activation.
1585  gfx::Rect viewport_for_tile_priority =
1586      pending_layer_->GetViewportForTilePriorityInContentSpace();
1587  viewport_for_tile_priority.Intersect(pending_layer_->visible_content_rect());
1588
1589  int num_inside = 0;
1590  int num_outside = 0;
1591  for (PictureLayerTiling::CoverageIterator iter(
1592           tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1593       iter;
1594       ++iter) {
1595    if (!*iter)
1596      continue;
1597    Tile* tile = *iter;
1598    if (viewport_for_tile_priority.Intersects(iter.geometry_rect())) {
1599      num_inside++;
1600      // Mark everything in viewport for tile priority as ready to draw.
1601      ManagedTileState::TileVersion& tile_version =
1602          tile->GetTileVersionForTesting(
1603              tile->DetermineRasterModeForTree(PENDING_TREE));
1604      tile_version.SetSolidColorForTesting(SK_ColorRED);
1605    } else {
1606      num_outside++;
1607      EXPECT_FALSE(tile->required_for_activation());
1608    }
1609  }
1610
1611  EXPECT_GT(num_inside, 0);
1612  EXPECT_GT(num_outside, 0);
1613
1614  // Activate and draw active layer.
1615  host_impl_.ActivateSyncTree();
1616  host_impl_.active_tree()->UpdateDrawProperties();
1617  active_layer_->draw_properties().visible_content_rect = visible_content_rect;
1618
1619  MockOcclusionTracker<LayerImpl> occlusion_tracker;
1620  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1621  AppendQuadsData data;
1622  active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1623  active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1624  active_layer_->DidDraw(NULL);
1625
1626  // All tiles in activation rect is ready to draw.
1627  EXPECT_EQ(0u, data.num_missing_tiles);
1628  EXPECT_EQ(0u, data.num_incomplete_tiles);
1629}
1630
1631TEST_F(PictureLayerImplTest, HighResTileIsComplete) {
1632  base::TimeTicks time_ticks;
1633  time_ticks += base::TimeDelta::FromMilliseconds(1);
1634  host_impl_.SetCurrentBeginFrameArgs(
1635      CreateBeginFrameArgsForTesting(time_ticks));
1636
1637  gfx::Size tile_size(100, 100);
1638  gfx::Size layer_bounds(200, 200);
1639
1640  host_impl_.SetViewportSize(layer_bounds);
1641
1642  scoped_refptr<FakePicturePileImpl> pending_pile =
1643      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1644  SetupPendingTree(pending_pile);
1645  ActivateTree();
1646
1647  // All high res tiles have resources.
1648  active_layer_->set_fixed_tile_size(tile_size);
1649  host_impl_.active_tree()->UpdateDrawProperties();
1650  std::vector<Tile*> tiles =
1651      active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1652  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
1653
1654  MockOcclusionTracker<LayerImpl> occlusion_tracker;
1655  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1656  AppendQuadsData data;
1657  active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1658  active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1659  active_layer_->DidDraw(NULL);
1660
1661  // All high res tiles drew, nothing was incomplete.
1662  EXPECT_EQ(9u, render_pass->quad_list.size());
1663  EXPECT_EQ(0u, data.num_missing_tiles);
1664  EXPECT_EQ(0u, data.num_incomplete_tiles);
1665}
1666
1667TEST_F(PictureLayerImplTest, LowResTileIsIncomplete) {
1668  base::TimeTicks time_ticks;
1669  time_ticks += base::TimeDelta::FromMilliseconds(1);
1670  host_impl_.SetCurrentBeginFrameArgs(
1671      CreateBeginFrameArgsForTesting(time_ticks));
1672
1673  gfx::Size tile_size(100, 100);
1674  gfx::Size layer_bounds(200, 200);
1675
1676  host_impl_.SetViewportSize(layer_bounds);
1677
1678  scoped_refptr<FakePicturePileImpl> pending_pile =
1679      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1680  SetupPendingTree(pending_pile);
1681  ActivateTree();
1682
1683  // All high res tiles have resources except one.
1684  active_layer_->set_fixed_tile_size(tile_size);
1685  host_impl_.active_tree()->UpdateDrawProperties();
1686  std::vector<Tile*> high_tiles =
1687      active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1688  high_tiles.erase(high_tiles.begin());
1689  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1690
1691  // All low res tiles have resources.
1692  std::vector<Tile*> low_tiles =
1693      active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1694  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1695
1696  MockOcclusionTracker<LayerImpl> occlusion_tracker;
1697  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1698  AppendQuadsData data;
1699  active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1700  active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1701  active_layer_->DidDraw(NULL);
1702
1703  // The missing high res tile was replaced by a low res tile.
1704  EXPECT_EQ(9u, render_pass->quad_list.size());
1705  EXPECT_EQ(0u, data.num_missing_tiles);
1706  EXPECT_EQ(1u, data.num_incomplete_tiles);
1707}
1708
1709TEST_F(PictureLayerImplTest,
1710       HighResAndIdealResTileIsCompleteWhenRasterScaleIsNotIdeal) {
1711  base::TimeTicks time_ticks;
1712  time_ticks += base::TimeDelta::FromMilliseconds(1);
1713  host_impl_.SetCurrentBeginFrameArgs(
1714      CreateBeginFrameArgsForTesting(time_ticks));
1715
1716  gfx::Size tile_size(100, 100);
1717  gfx::Size layer_bounds(200, 200);
1718
1719  host_impl_.SetViewportSize(layer_bounds);
1720
1721  scoped_refptr<FakePicturePileImpl> pending_pile =
1722      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1723  scoped_refptr<FakePicturePileImpl> active_pile =
1724      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1725  SetupTrees(pending_pile, active_pile);
1726
1727  active_layer_->set_fixed_tile_size(tile_size);
1728
1729  active_layer_->draw_properties().visible_content_rect =
1730      gfx::Rect(layer_bounds);
1731  SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1732
1733  // One ideal tile exists, this will get used when drawing.
1734  std::vector<Tile*> ideal_tiles;
1735  EXPECT_EQ(2.f, active_layer_->HighResTiling()->contents_scale());
1736  ideal_tiles.push_back(active_layer_->HighResTiling()->TileAt(0, 0));
1737  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1738      ideal_tiles);
1739
1740  // Due to layer scale throttling, the raster contents scale is changed to 1,
1741  // while the ideal is still 2.
1742  SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
1743  SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1744
1745  EXPECT_EQ(1.f, active_layer_->HighResTiling()->contents_scale());
1746  EXPECT_EQ(1.f, active_layer_->raster_contents_scale());
1747  EXPECT_EQ(2.f, active_layer_->ideal_contents_scale());
1748
1749  // Both tilings still exist.
1750  EXPECT_EQ(2.f, active_layer_->tilings()->tiling_at(0)->contents_scale());
1751  EXPECT_EQ(1.f, active_layer_->tilings()->tiling_at(1)->contents_scale());
1752
1753  // All high res tiles have resources.
1754  std::vector<Tile*> high_tiles =
1755      active_layer_->HighResTiling()->AllTilesForTesting();
1756  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1757
1758  MockOcclusionTracker<LayerImpl> occlusion_tracker;
1759  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1760  AppendQuadsData data;
1761  active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1762  active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1763  active_layer_->DidDraw(NULL);
1764
1765  // All high res tiles drew, and the one ideal res tile drew.
1766  ASSERT_GT(render_pass->quad_list.size(), 9u);
1767  EXPECT_EQ(gfx::SizeF(99.f, 99.f),
1768            TileDrawQuad::MaterialCast(render_pass->quad_list.front())
1769                ->tex_coord_rect.size());
1770  EXPECT_EQ(gfx::SizeF(49.5f, 49.5f),
1771            TileDrawQuad::MaterialCast(render_pass->quad_list.ElementAt(1))
1772                ->tex_coord_rect.size());
1773
1774  // Neither the high res nor the ideal tiles were considered as incomplete.
1775  EXPECT_EQ(0u, data.num_missing_tiles);
1776  EXPECT_EQ(0u, data.num_incomplete_tiles);
1777}
1778
1779TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
1780  gfx::Size layer_bounds(400, 400);
1781  gfx::Size tile_size(100, 100);
1782  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1783
1784  // No tiles shared.
1785  pending_layer_->set_invalidation(gfx::Rect(layer_bounds));
1786
1787  CreateHighLowResAndSetAllTilesVisible();
1788
1789  active_layer_->SetAllTilesReady();
1790
1791  // No shared tiles and all active tiles ready, so pending can only
1792  // activate with all high res tiles.
1793  pending_layer_->MarkVisibleResourcesAsRequired();
1794  AssertAllTilesRequired(pending_layer_->HighResTiling());
1795  AssertNoTilesRequired(pending_layer_->LowResTiling());
1796}
1797
1798TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
1799  gfx::Size layer_bounds(400, 400);
1800  gfx::Size tile_size(100, 100);
1801  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1802
1803  // All tiles shared (no invalidation).
1804  CreateHighLowResAndSetAllTilesVisible();
1805
1806  // Verify active tree not ready.
1807  Tile* some_active_tile =
1808      active_layer_->HighResTiling()->AllTilesForTesting()[0];
1809  EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1810
1811  // When high res are required, even if the active tree is not ready,
1812  // the high res tiles must be ready.
1813  host_impl_.active_tree()->SetRequiresHighResToDraw();
1814  pending_layer_->MarkVisibleResourcesAsRequired();
1815  AssertAllTilesRequired(pending_layer_->HighResTiling());
1816  AssertNoTilesRequired(pending_layer_->LowResTiling());
1817}
1818
1819TEST_F(PictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
1820  gfx::Size layer_bounds(400, 400);
1821  gfx::Size tile_size(100, 100);
1822  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1823
1824  CreateHighLowResAndSetAllTilesVisible();
1825
1826  Tile* some_active_tile =
1827      active_layer_->HighResTiling()->AllTilesForTesting()[0];
1828  EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1829
1830  // All tiles shared (no invalidation), so even though the active tree's
1831  // tiles aren't ready, there is nothing required.
1832  pending_layer_->MarkVisibleResourcesAsRequired();
1833  AssertNoTilesRequired(pending_layer_->HighResTiling());
1834  AssertNoTilesRequired(pending_layer_->LowResTiling());
1835}
1836
1837TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
1838  gfx::Size layer_bounds(400, 400);
1839  gfx::Size tile_size(100, 100);
1840  scoped_refptr<FakePicturePileImpl> pending_pile =
1841      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1842  // This pile will create tilings, but has no recordings so will not create any
1843  // tiles.  This is attempting to simulate scrolling past the end of recorded
1844  // content on the active layer, where the recordings are so far away that
1845  // no tiles are created.
1846  scoped_refptr<FakePicturePileImpl> active_pile =
1847      FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1848          tile_size, layer_bounds);
1849  SetupTrees(pending_pile, active_pile);
1850  pending_layer_->set_fixed_tile_size(tile_size);
1851  active_layer_->set_fixed_tile_size(tile_size);
1852
1853  CreateHighLowResAndSetAllTilesVisible();
1854
1855  // Active layer has tilings, but no tiles due to missing recordings.
1856  EXPECT_TRUE(active_layer_->CanHaveTilings());
1857  EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
1858  EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
1859
1860  // Since the active layer has no tiles at all, the pending layer doesn't
1861  // need content in order to activate.
1862  pending_layer_->MarkVisibleResourcesAsRequired();
1863  AssertNoTilesRequired(pending_layer_->HighResTiling());
1864  AssertNoTilesRequired(pending_layer_->LowResTiling());
1865}
1866
1867TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
1868  gfx::Size layer_bounds(400, 400);
1869  gfx::Size tile_size(100, 100);
1870  scoped_refptr<FakePicturePileImpl> pending_pile =
1871      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1872  scoped_refptr<FakePicturePileImpl> active_pile =
1873      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1874  SetupTrees(pending_pile, active_pile);
1875  pending_layer_->set_fixed_tile_size(tile_size);
1876  active_layer_->set_fixed_tile_size(tile_size);
1877
1878  CreateHighLowResAndSetAllTilesVisible();
1879
1880  // Active layer can't have tiles.
1881  EXPECT_FALSE(active_layer_->CanHaveTilings());
1882
1883  // All high res tiles required.  This should be considered identical
1884  // to the case where there is no active layer, to avoid flashing content.
1885  // This can happen if a layer exists for a while and switches from
1886  // not being able to have content to having content.
1887  pending_layer_->MarkVisibleResourcesAsRequired();
1888  AssertAllTilesRequired(pending_layer_->HighResTiling());
1889  AssertNoTilesRequired(pending_layer_->LowResTiling());
1890}
1891
1892TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
1893  gfx::Size layer_bounds(200, 200);
1894  gfx::Size tile_size(100, 100);
1895  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1896
1897  gfx::Size pending_layer_bounds(400, 400);
1898  pending_layer_->SetBounds(pending_layer_bounds);
1899
1900  CreateHighLowResAndSetAllTilesVisible();
1901
1902  active_layer_->SetAllTilesReady();
1903
1904  // Since the active layer has different bounds, the pending layer needs all
1905  // high res tiles in order to activate.
1906  pending_layer_->MarkVisibleResourcesAsRequired();
1907  AssertAllTilesRequired(pending_layer_->HighResTiling());
1908  AssertNoTilesRequired(pending_layer_->LowResTiling());
1909}
1910
1911TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1912  gfx::Size tile_size(100, 100);
1913  gfx::Size layer_bounds(400, 400);
1914  scoped_refptr<FakePicturePileImpl> pending_pile =
1915      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1916
1917  host_impl_.CreatePendingTree();
1918  LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1919
1920  scoped_ptr<FakePictureLayerImpl> pending_layer =
1921      FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1922  pending_layer->SetDrawsContent(true);
1923  pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1924
1925  pending_layer_ = static_cast<FakePictureLayerImpl*>(
1926      host_impl_.pending_tree()->LayerById(id_));
1927
1928  // Set some state on the pending layer, make sure it is not clobbered
1929  // by a sync from the active layer.  This could happen because if the
1930  // pending layer has not been post-commit initialized it will attempt
1931  // to sync from the active layer.
1932  float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
1933  pending_layer_->set_raster_page_scale(raster_page_scale);
1934  EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1935
1936  host_impl_.ActivateSyncTree();
1937
1938  active_layer_ = static_cast<FakePictureLayerImpl*>(
1939      host_impl_.active_tree()->LayerById(id_));
1940
1941  EXPECT_EQ(0u, active_layer_->num_tilings());
1942  EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
1943  EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1944}
1945
1946TEST_F(PictureLayerImplTest, ShareTilesOnSync) {
1947  SetupDefaultTrees(gfx::Size(1500, 1500));
1948  AddDefaultTilingsWithInvalidation(gfx::Rect());
1949
1950  host_impl_.ActivateSyncTree();
1951  host_impl_.CreatePendingTree();
1952  active_layer_ = static_cast<FakePictureLayerImpl*>(
1953      host_impl_.active_tree()->LayerById(id_));
1954
1955  // Force the active tree to sync to the pending tree "post-commit".
1956  pending_layer_->DoPostCommitInitializationIfNeeded();
1957
1958  // Both invalidations should drop tiles from the pending tree.
1959  EXPECT_EQ(3u, active_layer_->num_tilings());
1960  EXPECT_EQ(3u, pending_layer_->num_tilings());
1961  for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1962    PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1963    PictureLayerTiling* pending_tiling =
1964        pending_layer_->tilings()->tiling_at(i);
1965
1966    ASSERT_TRUE(active_tiling);
1967    ASSERT_TRUE(pending_tiling);
1968
1969    EXPECT_TRUE(active_tiling->TileAt(0, 0));
1970    EXPECT_TRUE(active_tiling->TileAt(1, 0));
1971    EXPECT_TRUE(active_tiling->TileAt(0, 1));
1972    EXPECT_TRUE(active_tiling->TileAt(1, 1));
1973
1974    EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1975    EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1976    EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1977    EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1978
1979    EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1980    EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
1981    EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1982    EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
1983    EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1984    EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
1985    EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1986    EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
1987  }
1988}
1989
1990TEST_F(PictureLayerImplTest, ShareInvalidActiveTreeTilesOnSync) {
1991  SetupDefaultTrees(gfx::Size(1500, 1500));
1992  AddDefaultTilingsWithInvalidation(gfx::Rect(0, 0, 1, 1));
1993
1994  // This activates the 0,0,1,1 invalidation.
1995  host_impl_.ActivateSyncTree();
1996  host_impl_.CreatePendingTree();
1997  active_layer_ = static_cast<FakePictureLayerImpl*>(
1998      host_impl_.active_tree()->LayerById(id_));
1999
2000  // Force the active tree to sync to the pending tree "post-commit".
2001  pending_layer_->DoPostCommitInitializationIfNeeded();
2002
2003  // The active tree invalidation was handled by the active tiles, so they
2004  // can be shared with the pending tree.
2005  EXPECT_EQ(3u, active_layer_->num_tilings());
2006  EXPECT_EQ(3u, pending_layer_->num_tilings());
2007  for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
2008    PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
2009    PictureLayerTiling* pending_tiling =
2010        pending_layer_->tilings()->tiling_at(i);
2011
2012    ASSERT_TRUE(active_tiling);
2013    ASSERT_TRUE(pending_tiling);
2014
2015    EXPECT_TRUE(active_tiling->TileAt(0, 0));
2016    EXPECT_TRUE(active_tiling->TileAt(1, 0));
2017    EXPECT_TRUE(active_tiling->TileAt(0, 1));
2018    EXPECT_TRUE(active_tiling->TileAt(1, 1));
2019
2020    EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2021    EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2022    EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2023    EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2024
2025    EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2026    EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
2027    EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2028    EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2029    EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2030    EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
2031    EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2032    EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2033  }
2034}
2035
2036TEST_F(PictureLayerImplTest, RemoveInvalidPendingTreeTilesOnSync) {
2037  SetupDefaultTrees(gfx::Size(1500, 1500));
2038  AddDefaultTilingsWithInvalidation(gfx::Rect());
2039
2040  host_impl_.ActivateSyncTree();
2041  host_impl_.CreatePendingTree();
2042  active_layer_ = static_cast<FakePictureLayerImpl*>(
2043      host_impl_.active_tree()->LayerById(id_));
2044
2045  // Set some invalidation on the pending tree "during commit". We should
2046  // replace raster tiles that touch this.
2047  pending_layer_->set_invalidation(gfx::Rect(1, 1));
2048
2049  // Force the active tree to sync to the pending tree "post-commit".
2050  pending_layer_->DoPostCommitInitializationIfNeeded();
2051
2052  // The pending tree invalidation means tiles can not be shared with the
2053  // active tree.
2054  EXPECT_EQ(3u, active_layer_->num_tilings());
2055  EXPECT_EQ(3u, pending_layer_->num_tilings());
2056  for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
2057    PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
2058    PictureLayerTiling* pending_tiling =
2059        pending_layer_->tilings()->tiling_at(i);
2060
2061    ASSERT_TRUE(active_tiling);
2062    ASSERT_TRUE(pending_tiling);
2063
2064    EXPECT_TRUE(active_tiling->TileAt(0, 0));
2065    EXPECT_TRUE(active_tiling->TileAt(1, 0));
2066    EXPECT_TRUE(active_tiling->TileAt(0, 1));
2067    EXPECT_TRUE(active_tiling->TileAt(1, 1));
2068
2069    EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2070    EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2071    EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2072    EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2073
2074    EXPECT_NE(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2075    EXPECT_FALSE(active_tiling->TileAt(0, 0)->is_shared());
2076    EXPECT_FALSE(pending_tiling->TileAt(0, 0)->is_shared());
2077    EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2078    EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2079    EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2080    EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2081    EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2082    EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2083  }
2084}
2085
2086TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
2087  SetupDefaultTrees(gfx::Size(10, 10));
2088  host_impl_.active_tree()->UpdateDrawProperties();
2089  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2090
2091  // Contrived unit test of a real crash. A layer is transparent during a
2092  // context loss, and later becomes opaque, causing active layer SyncTiling to
2093  // be called.
2094  float new_scale = 1.f;
2095  active_layer_->ReleaseResources();
2096  pending_layer_->ReleaseResources();
2097  EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(new_scale));
2098  pending_layer_->AddTiling(new_scale);
2099  EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(new_scale));
2100
2101  // UpdateDrawProperties early-outs if the tree doesn't need it.  It is also
2102  // responsible for calling ManageTilings.  These checks verify that
2103  // ReleaseResources has set needs update draw properties so that the
2104  // new tiling gets the appropriate resolution set in ManageTilings.
2105  EXPECT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2106  host_impl_.active_tree()->UpdateDrawProperties();
2107  PictureLayerTiling* high_res =
2108      active_layer_->tilings()->TilingAtScale(new_scale);
2109  ASSERT_TRUE(!!high_res);
2110  EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
2111}
2112
2113TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
2114  SetupDefaultTrees(gfx::Size(10, 10));
2115
2116  const float kScale = 1.f;
2117  pending_layer_->AddTiling(kScale);
2118  EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2119  EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2120
2121  // Gpu rasterization is disabled by default.
2122  EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2123  // Toggling the gpu rasterization clears all tilings on both trees.
2124  host_impl_.SetUseGpuRasterization(true);
2125  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2126  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2127
2128  // Make sure that we can still add tiling to the pending layer,
2129  // that gets synced to the active layer.
2130  pending_layer_->AddTiling(kScale);
2131  EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2132  EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2133
2134  // Toggling the gpu rasterization clears all tilings on both trees.
2135  EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2136  host_impl_.SetUseGpuRasterization(false);
2137  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2138  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2139}
2140
2141TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
2142  SetupDefaultTrees(gfx::Size(10, 10));
2143  host_impl_.active_tree()->UpdateDrawProperties();
2144  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2145
2146  SetupDrawPropertiesAndUpdateTiles(
2147      active_layer_, 0.5f, 0.5f, 0.5f, 0.5f, false);
2148  active_layer_->tilings()->RemoveAllTilings();
2149  PictureLayerTiling* tiling = active_layer_->tilings()->AddTiling(0.5f);
2150  active_layer_->tilings()->AddTiling(1.5f);
2151  active_layer_->tilings()->AddTiling(0.25f);
2152  tiling->set_resolution(HIGH_RESOLUTION);
2153
2154  // Sanity checks.
2155  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
2156  ASSERT_EQ(tiling, active_layer_->tilings()->TilingAtScale(0.5f));
2157
2158  // Now, set the bounds to be 1x1 (so that minimum contents scale becomes
2159  // 1.0f). Note that we should also ensure that the pending layer needs post
2160  // commit initialization, since this is what would happen during commit. In
2161  // other words we want the pending layer to sync from the active layer.
2162  pending_layer_->SetBounds(gfx::Size(1, 1));
2163  pending_layer_->SetNeedsPostCommitInitialization();
2164  pending_layer_->set_twin_layer(NULL);
2165  active_layer_->set_twin_layer(NULL);
2166  EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
2167
2168  // Update the draw properties: sync from active tree should happen here.
2169  host_impl_.pending_tree()->UpdateDrawProperties();
2170
2171  // Another sanity check.
2172  ASSERT_EQ(1.f, pending_layer_->MinimumContentsScale());
2173
2174  // Now we should've synced 1.5f tiling, since that's the only one that doesn't
2175  // violate minimum contents scale. At the same time, we should've created a
2176  // new high res tiling at scale 1.0f.
2177  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
2178  ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.0f));
2179  EXPECT_EQ(HIGH_RESOLUTION,
2180            pending_layer_->tilings()->TilingAtScale(1.0f)->resolution());
2181  ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.5f));
2182  EXPECT_EQ(NON_IDEAL_RESOLUTION,
2183            pending_layer_->tilings()->TilingAtScale(1.5f)->resolution());
2184}
2185
2186TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
2187  gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
2188  gfx::Size layer_bounds(default_tile_size.width() * 4,
2189                         default_tile_size.height() * 4);
2190
2191  SetupDefaultTrees(layer_bounds);
2192  EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2193  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2194  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2195  // Should have a low-res and a high-res tiling.
2196  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
2197
2198  ResetTilingsAndRasterScales();
2199
2200  host_impl_.SetUseGpuRasterization(true);
2201  EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2202  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2203
2204  // Should only have the high-res tiling.
2205  ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
2206}
2207
2208TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
2209  // Set up layers with tilings.
2210  SetupDefaultTrees(gfx::Size(10, 10));
2211  SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
2212  pending_layer_->PushPropertiesTo(active_layer_);
2213  EXPECT_TRUE(pending_layer_->DrawsContent());
2214  EXPECT_TRUE(pending_layer_->CanHaveTilings());
2215  EXPECT_GE(pending_layer_->num_tilings(), 0u);
2216  EXPECT_GE(active_layer_->num_tilings(), 0u);
2217
2218  // Set content to false, which should make CanHaveTilings return false.
2219  pending_layer_->SetDrawsContent(false);
2220  EXPECT_FALSE(pending_layer_->DrawsContent());
2221  EXPECT_FALSE(pending_layer_->CanHaveTilings());
2222
2223  // No tilings should be pushed to active layer.
2224  pending_layer_->PushPropertiesTo(active_layer_);
2225  EXPECT_EQ(0u, active_layer_->num_tilings());
2226}
2227
2228TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
2229  SetupDefaultTrees(gfx::Size(10, 10));
2230  host_impl_.PinchGestureBegin();
2231  float high_res_scale = 2.3f;
2232  SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2233
2234  ASSERT_GE(pending_layer_->num_tilings(), 0u);
2235  EXPECT_FLOAT_EQ(high_res_scale,
2236                  pending_layer_->HighResTiling()->contents_scale());
2237}
2238
2239TEST_F(PictureLayerImplTest, FirstTilingTooSmall) {
2240  SetupDefaultTrees(gfx::Size(10, 10));
2241  host_impl_.PinchGestureBegin();
2242  float high_res_scale = 0.0001f;
2243  EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale);
2244
2245  SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2246
2247  ASSERT_GE(pending_layer_->num_tilings(), 0u);
2248  EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2249                  pending_layer_->HighResTiling()->contents_scale());
2250}
2251
2252TEST_F(PictureLayerImplTest, PinchingTooSmall) {
2253  SetupDefaultTrees(gfx::Size(10, 10));
2254
2255  float contents_scale = 0.15f;
2256  SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
2257
2258  ASSERT_GE(pending_layer_->num_tilings(), 0u);
2259  EXPECT_FLOAT_EQ(contents_scale,
2260                  pending_layer_->HighResTiling()->contents_scale());
2261
2262  host_impl_.PinchGestureBegin();
2263
2264  float page_scale = 0.0001f;
2265  EXPECT_LT(page_scale * contents_scale,
2266            pending_layer_->MinimumContentsScale());
2267
2268  SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, 1.f, false);
2269  ASSERT_GE(pending_layer_->num_tilings(), 0u);
2270  EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2271                  pending_layer_->HighResTiling()->contents_scale());
2272}
2273
2274class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
2275 public:
2276  virtual void InitializeRenderer() OVERRIDE {
2277    bool delegated_rendering = false;
2278    host_impl_.InitializeRenderer(
2279        FakeOutputSurface::CreateDeferredGL(
2280            scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
2281            delegated_rendering).PassAs<OutputSurface>());
2282  }
2283
2284  virtual void SetUp() OVERRIDE {
2285    PictureLayerImplTest::SetUp();
2286
2287    // Create some default active and pending trees.
2288    gfx::Size tile_size(100, 100);
2289    gfx::Size layer_bounds(400, 400);
2290
2291    scoped_refptr<FakePicturePileImpl> pending_pile =
2292        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2293    scoped_refptr<FakePicturePileImpl> active_pile =
2294        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2295
2296    SetupTrees(pending_pile, active_pile);
2297  }
2298};
2299
2300// This test is really a LayerTreeHostImpl test, in that it makes sure
2301// that trees need update draw properties after deferred initialization.
2302// However, this is also a regression test for PictureLayerImpl in that
2303// not having this update will cause a crash.
2304TEST_F(DeferredInitPictureLayerImplTest, PreventUpdateTilesDuringLostContext) {
2305  host_impl_.pending_tree()->UpdateDrawProperties();
2306  host_impl_.active_tree()->UpdateDrawProperties();
2307  EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
2308  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2309
2310  FakeOutputSurface* fake_output_surface =
2311      static_cast<FakeOutputSurface*>(host_impl_.output_surface());
2312  ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
2313      TestContextProvider::Create()));
2314
2315  // These will crash PictureLayerImpl if this is not true.
2316  ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
2317  ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2318  host_impl_.active_tree()->UpdateDrawProperties();
2319}
2320
2321TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
2322  gfx::Size layer_bounds(100, 100);
2323  gfx::Size viewport_size(1000, 1000);
2324  SetupDefaultTrees(layer_bounds);
2325  host_impl_.SetViewportSize(viewport_size);
2326
2327  float contents_scale = 1.f;
2328  float device_scale = 1.3f;
2329  float page_scale = 1.4f;
2330  float maximum_animation_scale = 1.f;
2331  bool animating_transform = false;
2332
2333  SetContentsScaleOnBothLayers(contents_scale,
2334                               device_scale,
2335                               page_scale,
2336                               maximum_animation_scale,
2337                               animating_transform);
2338  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2339
2340  // Since we're CPU-rasterizing, starting an animation should cause tiling
2341  // resolution to get set to the maximum animation scale factor.
2342  animating_transform = true;
2343  maximum_animation_scale = 3.f;
2344  contents_scale = 2.f;
2345
2346  SetContentsScaleOnBothLayers(contents_scale,
2347                               device_scale,
2348                               page_scale,
2349                               maximum_animation_scale,
2350                               animating_transform);
2351  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2352
2353  // Further changes to scale during the animation should not cause a new
2354  // high-res tiling to get created.
2355  contents_scale = 4.f;
2356  maximum_animation_scale = 5.f;
2357
2358  SetContentsScaleOnBothLayers(contents_scale,
2359                               device_scale,
2360                               page_scale,
2361                               maximum_animation_scale,
2362                               animating_transform);
2363  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2364
2365  // Once we stop animating, a new high-res tiling should be created.
2366  animating_transform = false;
2367
2368  SetContentsScaleOnBothLayers(contents_scale,
2369                               device_scale,
2370                               page_scale,
2371                               maximum_animation_scale,
2372                               animating_transform);
2373  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2374
2375  // When animating with an unknown maximum animation scale factor, a new
2376  // high-res tiling should be created at a source scale of 1.
2377  animating_transform = true;
2378  contents_scale = 2.f;
2379  maximum_animation_scale = 0.f;
2380
2381  SetContentsScaleOnBothLayers(contents_scale,
2382                               device_scale,
2383                               page_scale,
2384                               maximum_animation_scale,
2385                               animating_transform);
2386  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2387
2388  // Further changes to scale during the animation should not cause a new
2389  // high-res tiling to get created.
2390  contents_scale = 3.f;
2391
2392  SetContentsScaleOnBothLayers(contents_scale,
2393                               device_scale,
2394                               page_scale,
2395                               maximum_animation_scale,
2396                               animating_transform);
2397  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2398
2399  // Once we stop animating, a new high-res tiling should be created.
2400  animating_transform = false;
2401  contents_scale = 4.f;
2402
2403  SetContentsScaleOnBothLayers(contents_scale,
2404                               device_scale,
2405                               page_scale,
2406                               maximum_animation_scale,
2407                               animating_transform);
2408  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2409
2410  // When animating with a maxmium animation scale factor that is so large
2411  // that the layer grows larger than the viewport at this scale, a new
2412  // high-res tiling should get created at a source scale of 1, not at its
2413  // maximum scale.
2414  animating_transform = true;
2415  contents_scale = 2.f;
2416  maximum_animation_scale = 11.f;
2417
2418  SetContentsScaleOnBothLayers(contents_scale,
2419                               device_scale,
2420                               page_scale,
2421                               maximum_animation_scale,
2422                               animating_transform);
2423  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2424
2425  // Once we stop animating, a new high-res tiling should be created.
2426  animating_transform = false;
2427  contents_scale = 11.f;
2428
2429  SetContentsScaleOnBothLayers(contents_scale,
2430                               device_scale,
2431                               page_scale,
2432                               maximum_animation_scale,
2433                               animating_transform);
2434  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2435
2436  // When animating with a maxmium animation scale factor that is so large
2437  // that the layer grows larger than the viewport at this scale, and where
2438  // the intial source scale is < 1, a new high-res tiling should get created
2439  // at source scale 1.
2440  animating_transform = true;
2441  contents_scale = 0.1f;
2442  maximum_animation_scale = 11.f;
2443
2444  SetContentsScaleOnBothLayers(contents_scale,
2445                               device_scale,
2446                               page_scale,
2447                               maximum_animation_scale,
2448                               animating_transform);
2449  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2450
2451  // Once we stop animating, a new high-res tiling should be created.
2452  animating_transform = false;
2453  contents_scale = 12.f;
2454
2455  SetContentsScaleOnBothLayers(contents_scale,
2456                               device_scale,
2457                               page_scale,
2458                               maximum_animation_scale,
2459                               animating_transform);
2460  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 12.f);
2461
2462  // When animating toward a smaller scale, but that is still so large that the
2463  // layer grows larger than the viewport at this scale, a new high-res tiling
2464  // should get created at source scale 1.
2465  animating_transform = true;
2466  contents_scale = 11.f;
2467  maximum_animation_scale = 11.f;
2468
2469  SetContentsScaleOnBothLayers(contents_scale,
2470                               device_scale,
2471                               page_scale,
2472                               maximum_animation_scale,
2473                               animating_transform);
2474  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2475
2476  // Once we stop animating, a new high-res tiling should be created.
2477  animating_transform = false;
2478  contents_scale = 11.f;
2479
2480  SetContentsScaleOnBothLayers(contents_scale,
2481                               device_scale,
2482                               page_scale,
2483                               maximum_animation_scale,
2484                               animating_transform);
2485  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2486}
2487
2488TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForGpuRasterization) {
2489  gfx::Size layer_bounds(100, 100);
2490  gfx::Size viewport_size(1000, 1000);
2491  SetupDefaultTrees(layer_bounds);
2492  host_impl_.SetViewportSize(viewport_size);
2493  host_impl_.SetUseGpuRasterization(true);
2494
2495  float contents_scale = 1.f;
2496  float device_scale = 1.3f;
2497  float page_scale = 1.4f;
2498  float maximum_animation_scale = 1.f;
2499  bool animating_transform = false;
2500
2501  SetContentsScaleOnBothLayers(contents_scale,
2502                               device_scale,
2503                               page_scale,
2504                               maximum_animation_scale,
2505                               animating_transform);
2506  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2507
2508  // Since we're GPU-rasterizing, starting an animation should cause tiling
2509  // resolution to get set to the current contents scale.
2510  animating_transform = true;
2511  contents_scale = 2.f;
2512  maximum_animation_scale = 4.f;
2513
2514  SetContentsScaleOnBothLayers(contents_scale,
2515                               device_scale,
2516                               page_scale,
2517                               maximum_animation_scale,
2518                               animating_transform);
2519  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2520
2521  // Further changes to scale during the animation should cause a new high-res
2522  // tiling to get created.
2523  contents_scale = 3.f;
2524
2525  SetContentsScaleOnBothLayers(contents_scale,
2526                               device_scale,
2527                               page_scale,
2528                               maximum_animation_scale,
2529                               animating_transform);
2530  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2531
2532  // Since we're re-rasterizing during the animation, scales smaller than 1
2533  // should be respected.
2534  contents_scale = 0.25f;
2535
2536  SetContentsScaleOnBothLayers(contents_scale,
2537                               device_scale,
2538                               page_scale,
2539                               maximum_animation_scale,
2540                               animating_transform);
2541  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 0.25f);
2542
2543  // Once we stop animating, a new high-res tiling should be created.
2544  contents_scale = 4.f;
2545  animating_transform = false;
2546
2547  SetContentsScaleOnBothLayers(contents_scale,
2548                               device_scale,
2549                               page_scale,
2550                               maximum_animation_scale,
2551                               animating_transform);
2552  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2553
2554  static_cast<FakePicturePileImpl*>(pending_layer_->pile())->set_has_text(true);
2555  static_cast<FakePicturePileImpl*>(active_layer_->pile())->set_has_text(true);
2556
2557  // Since we're GPU-rasterizing but have text, starting an animation should
2558  // cause tiling resolution to get set to the maximum animation scale.
2559  animating_transform = true;
2560  contents_scale = 2.f;
2561  maximum_animation_scale = 3.f;
2562
2563  SetContentsScaleOnBothLayers(contents_scale,
2564                               device_scale,
2565                               page_scale,
2566                               maximum_animation_scale,
2567                               animating_transform);
2568  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2569
2570  // Further changes to scale during the animation should not cause a new
2571  // high-res tiling to get created.
2572  contents_scale = 4.f;
2573  maximum_animation_scale = 5.f;
2574
2575  SetContentsScaleOnBothLayers(contents_scale,
2576                               device_scale,
2577                               page_scale,
2578                               maximum_animation_scale,
2579                               animating_transform);
2580  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2581
2582  // Once we stop animating, a new high-res tiling should be created.
2583  animating_transform = false;
2584
2585  SetContentsScaleOnBothLayers(contents_scale,
2586                               device_scale,
2587                               page_scale,
2588                               maximum_animation_scale,
2589                               animating_transform);
2590  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2591}
2592
2593TEST_F(PictureLayerImplTest, LayerRasterTileIterator) {
2594  base::TimeTicks time_ticks;
2595  time_ticks += base::TimeDelta::FromMilliseconds(1);
2596  host_impl_.SetCurrentBeginFrameArgs(
2597      CreateBeginFrameArgsForTesting(time_ticks));
2598
2599  gfx::Size tile_size(100, 100);
2600  gfx::Size layer_bounds(1000, 1000);
2601
2602  scoped_refptr<FakePicturePileImpl> pending_pile =
2603      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2604
2605  SetupPendingTree(pending_pile);
2606
2607  ASSERT_TRUE(pending_layer_->CanHaveTilings());
2608
2609  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2610
2611  // Empty iterator
2612  PictureLayerImpl::LayerRasterTileIterator it;
2613  EXPECT_FALSE(it);
2614
2615  // No tilings.
2616  it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2617  EXPECT_FALSE(it);
2618
2619  pending_layer_->AddTiling(low_res_factor);
2620  pending_layer_->AddTiling(0.3f);
2621  pending_layer_->AddTiling(0.7f);
2622  PictureLayerTiling* high_res_tiling = pending_layer_->AddTiling(1.0f);
2623  pending_layer_->AddTiling(2.0f);
2624
2625  host_impl_.SetViewportSize(gfx::Size(500, 500));
2626  host_impl_.pending_tree()->UpdateDrawProperties();
2627
2628  std::set<Tile*> unique_tiles;
2629  bool reached_prepaint = false;
2630  size_t non_ideal_tile_count = 0u;
2631  size_t low_res_tile_count = 0u;
2632  size_t high_res_tile_count = 0u;
2633  for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2634       it;
2635       ++it) {
2636    Tile* tile = *it;
2637    TilePriority priority = tile->priority(PENDING_TREE);
2638
2639    EXPECT_TRUE(tile);
2640
2641    // Non-high res tiles only get visible tiles. Also, prepaint should only
2642    // come at the end of the iteration.
2643    if (priority.resolution != HIGH_RESOLUTION)
2644      EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2645    else if (reached_prepaint)
2646      EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2647    else
2648      reached_prepaint = priority.priority_bin != TilePriority::NOW;
2649
2650    non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2651    low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2652    high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2653
2654    unique_tiles.insert(tile);
2655  }
2656
2657  EXPECT_TRUE(reached_prepaint);
2658  EXPECT_EQ(0u, non_ideal_tile_count);
2659  EXPECT_EQ(1u, low_res_tile_count);
2660  EXPECT_EQ(16u, high_res_tile_count);
2661  EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2662            unique_tiles.size());
2663
2664  // No NOW tiles.
2665  time_ticks += base::TimeDelta::FromMilliseconds(200);
2666  host_impl_.SetCurrentBeginFrameArgs(
2667      CreateBeginFrameArgsForTesting(time_ticks));
2668
2669  pending_layer_->draw_properties().visible_content_rect =
2670      gfx::Rect(1100, 1100, 500, 500);
2671  bool resourceless_software_draw = false;
2672  pending_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
2673
2674  unique_tiles.clear();
2675  high_res_tile_count = 0u;
2676  for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2677       it;
2678       ++it) {
2679    Tile* tile = *it;
2680    TilePriority priority = tile->priority(PENDING_TREE);
2681
2682    EXPECT_TRUE(tile);
2683
2684    // Non-high res tiles only get visible tiles.
2685    EXPECT_EQ(HIGH_RESOLUTION, priority.resolution);
2686    EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2687
2688    high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2689
2690    unique_tiles.insert(tile);
2691  }
2692
2693  EXPECT_EQ(16u, high_res_tile_count);
2694  EXPECT_EQ(high_res_tile_count, unique_tiles.size());
2695
2696  time_ticks += base::TimeDelta::FromMilliseconds(200);
2697  host_impl_.SetCurrentBeginFrameArgs(
2698      CreateBeginFrameArgsForTesting(time_ticks));
2699
2700  pending_layer_->draw_properties().visible_content_rect =
2701      gfx::Rect(0, 0, 500, 500);
2702  pending_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
2703
2704  std::vector<Tile*> high_res_tiles = high_res_tiling->AllTilesForTesting();
2705  for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
2706       tile_it != high_res_tiles.end();
2707       ++tile_it) {
2708    Tile* tile = *tile_it;
2709    ManagedTileState::TileVersion& tile_version =
2710        tile->GetTileVersionForTesting(
2711            tile->DetermineRasterModeForTree(ACTIVE_TREE));
2712    tile_version.SetSolidColorForTesting(SK_ColorRED);
2713  }
2714
2715  non_ideal_tile_count = 0;
2716  low_res_tile_count = 0;
2717  high_res_tile_count = 0;
2718  for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2719       it;
2720       ++it) {
2721    Tile* tile = *it;
2722    TilePriority priority = tile->priority(PENDING_TREE);
2723
2724    EXPECT_TRUE(tile);
2725
2726    non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2727    low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2728    high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2729  }
2730
2731  EXPECT_EQ(0u, non_ideal_tile_count);
2732  EXPECT_EQ(1u, low_res_tile_count);
2733  EXPECT_EQ(0u, high_res_tile_count);
2734}
2735
2736TEST_F(PictureLayerImplTest, LayerEvictionTileIterator) {
2737  gfx::Size tile_size(100, 100);
2738  gfx::Size layer_bounds(1000, 1000);
2739
2740  scoped_refptr<FakePicturePileImpl> pending_pile =
2741      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2742
2743  SetupPendingTree(pending_pile);
2744
2745  ASSERT_TRUE(pending_layer_->CanHaveTilings());
2746
2747  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2748
2749  std::vector<PictureLayerTiling*> tilings;
2750  tilings.push_back(pending_layer_->AddTiling(low_res_factor));
2751  tilings.push_back(pending_layer_->AddTiling(0.3f));
2752  tilings.push_back(pending_layer_->AddTiling(0.7f));
2753  tilings.push_back(pending_layer_->AddTiling(1.0f));
2754  tilings.push_back(pending_layer_->AddTiling(2.0f));
2755
2756  host_impl_.SetViewportSize(gfx::Size(500, 500));
2757  host_impl_.pending_tree()->UpdateDrawProperties();
2758
2759  std::vector<Tile*> all_tiles;
2760  for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
2761           tilings.begin();
2762       tiling_iterator != tilings.end();
2763       ++tiling_iterator) {
2764    std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
2765    std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
2766  }
2767
2768  std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
2769
2770  bool mark_required = false;
2771  size_t number_of_marked_tiles = 0u;
2772  size_t number_of_unmarked_tiles = 0u;
2773  for (size_t i = 0; i < tilings.size(); ++i) {
2774    PictureLayerTiling* tiling = tilings.at(i);
2775    for (PictureLayerTiling::CoverageIterator iter(
2776             tiling,
2777             pending_layer_->contents_scale_x(),
2778             pending_layer_->visible_content_rect());
2779         iter;
2780         ++iter) {
2781      if (mark_required) {
2782        number_of_marked_tiles++;
2783        iter->MarkRequiredForActivation();
2784      } else {
2785        number_of_unmarked_tiles++;
2786      }
2787      mark_required = !mark_required;
2788    }
2789  }
2790
2791  // Sanity checks.
2792  EXPECT_EQ(91u, all_tiles.size());
2793  EXPECT_EQ(91u, all_tiles_set.size());
2794  EXPECT_GT(number_of_marked_tiles, 1u);
2795  EXPECT_GT(number_of_unmarked_tiles, 1u);
2796
2797  // Empty iterator.
2798  PictureLayerImpl::LayerEvictionTileIterator it;
2799  EXPECT_FALSE(it);
2800
2801  // Tiles don't have resources yet.
2802  it = PictureLayerImpl::LayerEvictionTileIterator(
2803      pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2804  EXPECT_FALSE(it);
2805
2806  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
2807
2808  std::set<Tile*> unique_tiles;
2809  float expected_scales[] = {2.0f, 0.3f, 0.7f, low_res_factor, 1.0f};
2810  size_t scale_index = 0;
2811  bool reached_visible = false;
2812  Tile* last_tile = NULL;
2813  for (it = PictureLayerImpl::LayerEvictionTileIterator(
2814           pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2815       it;
2816       ++it) {
2817    Tile* tile = *it;
2818    if (!last_tile)
2819      last_tile = tile;
2820
2821    EXPECT_TRUE(tile);
2822
2823    TilePriority priority = tile->priority(PENDING_TREE);
2824
2825    if (priority.priority_bin == TilePriority::NOW) {
2826      reached_visible = true;
2827      last_tile = tile;
2828      break;
2829    }
2830
2831    EXPECT_FALSE(tile->required_for_activation());
2832
2833    while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2834           std::numeric_limits<float>::epsilon()) {
2835      ++scale_index;
2836      ASSERT_LT(scale_index, arraysize(expected_scales));
2837    }
2838
2839    EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2840    unique_tiles.insert(tile);
2841
2842    // If the tile is the same rough bin as last tile (same activation, bin, and
2843    // scale), then distance should be decreasing.
2844    if (tile->required_for_activation() ==
2845            last_tile->required_for_activation() &&
2846        priority.priority_bin ==
2847            last_tile->priority(PENDING_TREE).priority_bin &&
2848        std::abs(tile->contents_scale() - last_tile->contents_scale()) <
2849            std::numeric_limits<float>::epsilon()) {
2850      EXPECT_LE(priority.distance_to_visible,
2851                last_tile->priority(PENDING_TREE).distance_to_visible);
2852    }
2853
2854    last_tile = tile;
2855  }
2856
2857  EXPECT_TRUE(reached_visible);
2858  EXPECT_EQ(65u, unique_tiles.size());
2859
2860  scale_index = 0;
2861  bool reached_required = false;
2862  for (; it; ++it) {
2863    Tile* tile = *it;
2864    EXPECT_TRUE(tile);
2865
2866    TilePriority priority = tile->priority(PENDING_TREE);
2867    EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2868
2869    if (reached_required) {
2870      EXPECT_TRUE(tile->required_for_activation());
2871    } else if (tile->required_for_activation()) {
2872      reached_required = true;
2873      scale_index = 0;
2874    }
2875
2876    while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2877           std::numeric_limits<float>::epsilon()) {
2878      ++scale_index;
2879      ASSERT_LT(scale_index, arraysize(expected_scales));
2880    }
2881
2882    EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2883    unique_tiles.insert(tile);
2884  }
2885
2886  EXPECT_TRUE(reached_required);
2887  EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
2888}
2889
2890TEST_F(PictureLayerImplTest, Occlusion) {
2891  gfx::Size tile_size(102, 102);
2892  gfx::Size layer_bounds(1000, 1000);
2893  gfx::Size viewport_size(1000, 1000);
2894
2895  LayerTestCommon::LayerImplTest impl;
2896
2897  scoped_refptr<FakePicturePileImpl> pending_pile =
2898      FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
2899  SetupPendingTree(pending_pile);
2900  pending_layer_->SetBounds(layer_bounds);
2901  ActivateTree();
2902  active_layer_->set_fixed_tile_size(tile_size);
2903
2904  host_impl_.SetViewportSize(viewport_size);
2905  host_impl_.active_tree()->UpdateDrawProperties();
2906
2907  std::vector<Tile*> tiles =
2908      active_layer_->HighResTiling()->AllTilesForTesting();
2909  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
2910
2911  {
2912    SCOPED_TRACE("No occlusion");
2913    gfx::Rect occluded;
2914    impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2915
2916    LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
2917                                                 gfx::Rect(layer_bounds));
2918    EXPECT_EQ(100u, impl.quad_list().size());
2919  }
2920
2921  {
2922    SCOPED_TRACE("Full occlusion");
2923    gfx::Rect occluded(active_layer_->visible_content_rect());
2924    impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2925
2926    LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
2927    EXPECT_EQ(impl.quad_list().size(), 0u);
2928  }
2929
2930  {
2931    SCOPED_TRACE("Partial occlusion");
2932    gfx::Rect occluded(150, 0, 200, 1000);
2933    impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2934
2935    size_t partially_occluded_count = 0;
2936    LayerTestCommon::VerifyQuadsAreOccluded(
2937        impl.quad_list(), occluded, &partially_occluded_count);
2938    // The layer outputs one quad, which is partially occluded.
2939    EXPECT_EQ(100u - 10u, impl.quad_list().size());
2940    EXPECT_EQ(10u + 10u, partially_occluded_count);
2941  }
2942}
2943
2944TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
2945  gfx::Size tile_size(host_impl_.settings().default_tile_size);
2946  SetupDefaultTrees(tile_size);
2947
2948  float contents_scale = 2.f;
2949  float device_scale = 1.f;
2950  float page_scale = 1.f;
2951  float maximum_animation_scale = 1.f;
2952  bool animating_transform = false;
2953
2954  SetContentsScaleOnBothLayers(contents_scale,
2955                               device_scale,
2956                               page_scale,
2957                               maximum_animation_scale,
2958                               animating_transform);
2959  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2960
2961  // Changing the source scale without being in an animation will cause
2962  // the layer to reset its source scale to 1.f.
2963  contents_scale = 3.f;
2964
2965  SetContentsScaleOnBothLayers(contents_scale,
2966                               device_scale,
2967                               page_scale,
2968                               maximum_animation_scale,
2969                               animating_transform);
2970  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2971
2972  // Further changes to the source scale will no longer be reflected in the
2973  // contents scale.
2974  contents_scale = 0.5f;
2975
2976  SetContentsScaleOnBothLayers(contents_scale,
2977                               device_scale,
2978                               page_scale,
2979                               maximum_animation_scale,
2980                               animating_transform);
2981  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2982}
2983
2984TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
2985  gfx::Size tile_size(100, 100);
2986  gfx::Size layer_bounds(1000, 1000);
2987
2988  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2989
2990  // Make sure some tiles are not shared.
2991  pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2992
2993  CreateHighLowResAndSetAllTilesVisible();
2994  active_layer_->SetAllTilesReady();
2995  pending_layer_->MarkVisibleResourcesAsRequired();
2996
2997  // All pending layer tiles required are not ready.
2998  EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2999
3000  // Initialize all low-res tiles.
3001  pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
3002
3003  // Low-res tiles should not be enough.
3004  EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3005
3006  // Initialize remaining tiles.
3007  pending_layer_->SetAllTilesReady();
3008
3009  EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3010}
3011
3012TEST_F(PictureLayerImplTest, HighResReadyToDrawEnoughToActivate) {
3013  gfx::Size tile_size(100, 100);
3014  gfx::Size layer_bounds(1000, 1000);
3015
3016  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3017
3018  // Make sure some tiles are not shared.
3019  pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
3020
3021  CreateHighLowResAndSetAllTilesVisible();
3022  active_layer_->SetAllTilesReady();
3023  pending_layer_->MarkVisibleResourcesAsRequired();
3024
3025  // All pending layer tiles required are not ready.
3026  EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3027
3028  // Initialize all high-res tiles.
3029  pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3030
3031  // High-res tiles should be enough, since they cover everything visible.
3032  EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3033}
3034
3035TEST_F(PictureLayerImplTest,
3036       SharedActiveHighResReadyAndPendingLowResReadyNotEnoughToActivate) {
3037  gfx::Size tile_size(100, 100);
3038  gfx::Size layer_bounds(1000, 1000);
3039
3040  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3041
3042  // Make sure some tiles are not shared.
3043  pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
3044
3045  CreateHighLowResAndSetAllTilesVisible();
3046
3047  // Initialize all high-res tiles in the active layer.
3048  active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3049  // And all the low-res tiles in the pending layer.
3050  pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
3051
3052  pending_layer_->MarkVisibleResourcesAsRequired();
3053
3054  // The unshared high-res tiles are not ready, so we cannot activate.
3055  EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3056
3057  // When the unshared pending high-res tiles are ready, we can activate.
3058  pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3059  EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3060}
3061
3062TEST_F(PictureLayerImplTest, SharedActiveHighResReadyNotEnoughToActivate) {
3063  gfx::Size tile_size(100, 100);
3064  gfx::Size layer_bounds(1000, 1000);
3065
3066  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3067
3068  // Make sure some tiles are not shared.
3069  pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
3070
3071  CreateHighLowResAndSetAllTilesVisible();
3072
3073  // Initialize all high-res tiles in the active layer.
3074  active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3075
3076  pending_layer_->MarkVisibleResourcesAsRequired();
3077
3078  // The unshared high-res tiles are not ready, so we cannot activate.
3079  EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3080
3081  // When the unshared pending high-res tiles are ready, we can activate.
3082  pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3083  EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3084}
3085
3086class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
3087 public:
3088  NoLowResPictureLayerImplTest()
3089      : PictureLayerImplTest(NoLowResTilingsSettings()) {}
3090};
3091
3092TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
3093  gfx::Size tile_size(400, 400);
3094  gfx::Size layer_bounds(1300, 1900);
3095
3096  scoped_refptr<FakePicturePileImpl> pending_pile =
3097      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3098  scoped_refptr<FakePicturePileImpl> active_pile =
3099      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3100
3101  SetupTrees(pending_pile, active_pile);
3102  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3103
3104  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3105  EXPECT_LT(low_res_factor, 1.f);
3106
3107  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3108                                    6.f,  // ideal contents scale
3109                                    3.f,  // device scale
3110                                    2.f,  // page scale
3111                                    1.f,  // maximum animation scale
3112                                    false);
3113  ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3114  EXPECT_FLOAT_EQ(6.f,
3115                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
3116
3117  // If we change the page scale factor, then we should get new tilings.
3118  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3119                                    6.6f,  // ideal contents scale
3120                                    3.f,   // device scale
3121                                    2.2f,  // page scale
3122                                    1.f,   // maximum animation scale
3123                                    false);
3124  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
3125  EXPECT_FLOAT_EQ(6.6f,
3126                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
3127
3128  // If we change the device scale factor, then we should get new tilings.
3129  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3130                                    7.26f,  // ideal contents scale
3131                                    3.3f,   // device scale
3132                                    2.2f,   // page scale
3133                                    1.f,    // maximum animation scale
3134                                    false);
3135  ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
3136  EXPECT_FLOAT_EQ(7.26f,
3137                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
3138
3139  // If we change the device scale factor, but end up at the same total scale
3140  // factor somehow, then we don't get new tilings.
3141  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3142                                    7.26f,  // ideal contents scale
3143                                    2.2f,   // device scale
3144                                    3.3f,   // page scale
3145                                    1.f,    // maximum animation scale
3146                                    false);
3147  ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
3148  EXPECT_FLOAT_EQ(7.26f,
3149                  pending_layer_->tilings()->tiling_at(0)->contents_scale());
3150}
3151
3152TEST_F(NoLowResPictureLayerImplTest, MarkRequiredNullTiles) {
3153  gfx::Size tile_size(100, 100);
3154  gfx::Size layer_bounds(1000, 1000);
3155
3156  scoped_refptr<FakePicturePileImpl> pending_pile =
3157      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
3158  // Layers with entirely empty piles can't get tilings.
3159  pending_pile->AddRecordingAt(0, 0);
3160
3161  SetupPendingTree(pending_pile);
3162
3163  ASSERT_TRUE(pending_layer_->CanHaveTilings());
3164  pending_layer_->AddTiling(1.0f);
3165  pending_layer_->AddTiling(2.0f);
3166
3167  // It should be safe to call this (and MarkVisibleResourcesAsRequired)
3168  // on a layer with no recordings.
3169  host_impl_.pending_tree()->UpdateDrawProperties();
3170  pending_layer_->MarkVisibleResourcesAsRequired();
3171}
3172
3173TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
3174  gfx::Size layer_bounds(400, 400);
3175  gfx::Size tile_size(100, 100);
3176  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3177
3178  CreateHighLowResAndSetAllTilesVisible();
3179
3180  Tile* some_active_tile =
3181      active_layer_->HighResTiling()->AllTilesForTesting()[0];
3182  EXPECT_FALSE(some_active_tile->IsReadyToDraw());
3183
3184  // All tiles shared (no invalidation), so even though the active tree's
3185  // tiles aren't ready, there is nothing required.
3186  pending_layer_->MarkVisibleResourcesAsRequired();
3187  AssertNoTilesRequired(pending_layer_->HighResTiling());
3188  if (host_impl_.settings().create_low_res_tiling) {
3189    AssertNoTilesRequired(pending_layer_->LowResTiling());
3190  }
3191}
3192
3193TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
3194  gfx::Size layer_bounds(400, 400);
3195  gfx::Size tile_size(100, 100);
3196  scoped_refptr<FakePicturePileImpl> pending_pile =
3197      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3198  // This pile will create tilings, but has no recordings so will not create any
3199  // tiles.  This is attempting to simulate scrolling past the end of recorded
3200  // content on the active layer, where the recordings are so far away that
3201  // no tiles are created.
3202  scoped_refptr<FakePicturePileImpl> active_pile =
3203      FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
3204          tile_size, layer_bounds);
3205  SetupTrees(pending_pile, active_pile);
3206  pending_layer_->set_fixed_tile_size(tile_size);
3207  active_layer_->set_fixed_tile_size(tile_size);
3208
3209  CreateHighLowResAndSetAllTilesVisible();
3210
3211  // Active layer has tilings, but no tiles due to missing recordings.
3212  EXPECT_TRUE(active_layer_->CanHaveTilings());
3213  EXPECT_EQ(active_layer_->tilings()->num_tilings(),
3214            host_impl_.settings().create_low_res_tiling ? 2u : 1u);
3215  EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
3216
3217  // Since the active layer has no tiles at all, the pending layer doesn't
3218  // need content in order to activate.
3219  pending_layer_->MarkVisibleResourcesAsRequired();
3220  AssertNoTilesRequired(pending_layer_->HighResTiling());
3221  if (host_impl_.settings().create_low_res_tiling)
3222    AssertNoTilesRequired(pending_layer_->LowResTiling());
3223}
3224
3225TEST_F(NoLowResPictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
3226  base::TimeTicks time_ticks;
3227  time_ticks += base::TimeDelta::FromMilliseconds(1);
3228  host_impl_.SetCurrentBeginFrameArgs(
3229      CreateBeginFrameArgsForTesting(time_ticks));
3230
3231  gfx::Size tile_size(100, 100);
3232  gfx::Size layer_bounds(400, 400);
3233
3234  scoped_refptr<FakePicturePileImpl> pending_pile =
3235      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3236  scoped_refptr<FakePicturePileImpl> active_pile =
3237      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3238
3239  SetupTrees(pending_pile, active_pile);
3240
3241  Region invalidation;
3242  AddDefaultTilingsWithInvalidation(invalidation);
3243  SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
3244
3245  // UpdateTiles with valid viewport. Should update tile viewport.
3246  // Note viewport is considered invalid if and only if in resourceless
3247  // software draw.
3248  bool resourceless_software_draw = false;
3249  gfx::Rect viewport = gfx::Rect(layer_bounds);
3250  gfx::Transform transform;
3251  host_impl_.SetExternalDrawConstraints(transform,
3252                                        viewport,
3253                                        viewport,
3254                                        viewport,
3255                                        transform,
3256                                        resourceless_software_draw);
3257  active_layer_->draw_properties().visible_content_rect = viewport;
3258  active_layer_->draw_properties().screen_space_transform = transform;
3259  active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
3260
3261  gfx::Rect visible_rect_for_tile_priority =
3262      active_layer_->visible_rect_for_tile_priority();
3263  EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
3264  gfx::Rect viewport_rect_for_tile_priority =
3265      active_layer_->viewport_rect_for_tile_priority();
3266  EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
3267  gfx::Transform screen_space_transform_for_tile_priority =
3268      active_layer_->screen_space_transform_for_tile_priority();
3269
3270  // Expand viewport and set it as invalid for prioritizing tiles.
3271  // Should update viewport and transform, but not update visible rect.
3272  time_ticks += base::TimeDelta::FromMilliseconds(200);
3273  host_impl_.SetCurrentBeginFrameArgs(
3274      CreateBeginFrameArgsForTesting(time_ticks));
3275  resourceless_software_draw = true;
3276  viewport = gfx::ScaleToEnclosingRect(viewport, 2);
3277  transform.Translate(1.f, 1.f);
3278  active_layer_->draw_properties().visible_content_rect = viewport;
3279  active_layer_->draw_properties().screen_space_transform = transform;
3280  host_impl_.SetExternalDrawConstraints(transform,
3281                                        viewport,
3282                                        viewport,
3283                                        viewport,
3284                                        transform,
3285                                        resourceless_software_draw);
3286  active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
3287
3288  // Viewport and transform for tile priority are updated.
3289  EXPECT_EQ(viewport, active_layer_->viewport_rect_for_tile_priority());
3290  EXPECT_TRANSFORMATION_MATRIX_EQ(
3291      transform, active_layer_->screen_space_transform_for_tile_priority());
3292  // Visible rect for tile priority retains old value.
3293  EXPECT_EQ(visible_rect_for_tile_priority,
3294            active_layer_->visible_rect_for_tile_priority());
3295
3296  // Keep expanded viewport but mark it valid. Should update tile viewport.
3297  time_ticks += base::TimeDelta::FromMilliseconds(200);
3298  host_impl_.SetCurrentBeginFrameArgs(
3299      CreateBeginFrameArgsForTesting(time_ticks));
3300  resourceless_software_draw = false;
3301  host_impl_.SetExternalDrawConstraints(transform,
3302                                        viewport,
3303                                        viewport,
3304                                        viewport,
3305                                        transform,
3306                                        resourceless_software_draw);
3307  active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
3308
3309  EXPECT_TRANSFORMATION_MATRIX_EQ(
3310      transform, active_layer_->screen_space_transform_for_tile_priority());
3311  EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
3312
3313  // Match the reverse translate in |transform|.
3314  EXPECT_EQ(viewport - gfx::Vector2d(1, 1),
3315            active_layer_->viewport_rect_for_tile_priority());
3316}
3317
3318TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
3319  gfx::Size tile_size(400, 400);
3320  gfx::Size layer_bounds(1300, 1900);
3321
3322  scoped_refptr<FakePicturePileImpl> pending_pile =
3323      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3324  scoped_refptr<FakePicturePileImpl> active_pile =
3325      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3326
3327  std::vector<PictureLayerTiling*> used_tilings;
3328
3329  SetupTrees(pending_pile, active_pile);
3330  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3331
3332  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3333  EXPECT_LT(low_res_factor, 1.f);
3334
3335  float device_scale = 1.7f;
3336  float page_scale = 3.2f;
3337  float scale = 1.f;
3338
3339  SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3340  ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3341
3342  // We only have ideal tilings, so they aren't removed.
3343  used_tilings.clear();
3344  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3345  ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3346
3347  host_impl_.PinchGestureBegin();
3348
3349  // Changing the ideal but not creating new tilings.
3350  scale *= 1.5f;
3351  page_scale *= 1.5f;
3352  SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3353  ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3354
3355  // The tilings are still our target scale, so they aren't removed.
3356  used_tilings.clear();
3357  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3358  ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3359
3360  host_impl_.PinchGestureEnd();
3361
3362  // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
3363  scale /= 4.f;
3364  page_scale /= 4.f;
3365  SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
3366  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3367  EXPECT_FLOAT_EQ(1.f,
3368                  active_layer_->tilings()->tiling_at(1)->contents_scale());
3369
3370  // Mark the non-ideal tilings as used. They won't be removed.
3371  used_tilings.clear();
3372  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3373  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3374  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3375
3376  // Now move the ideal scale to 0.5. Our target stays 1.2.
3377  SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
3378
3379  // The high resolution tiling is between target and ideal, so is not
3380  // removed.  The low res tiling for the old ideal=1.0 scale is removed.
3381  used_tilings.clear();
3382  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3383  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3384
3385  // Now move the ideal scale to 1.0. Our target stays 1.2.
3386  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
3387
3388  // All the tilings are between are target and the ideal, so they are not
3389  // removed.
3390  used_tilings.clear();
3391  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3392  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3393
3394  // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
3395  SetupDrawPropertiesAndUpdateTiles(
3396      active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3397
3398  // Because the pending layer's ideal scale is still 1.0, our tilings fall
3399  // in the range [1.0,1.2] and are kept.
3400  used_tilings.clear();
3401  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3402  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3403
3404  // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
3405  // 1.2 still.
3406  SetupDrawPropertiesAndUpdateTiles(
3407      pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3408
3409  // Our 1.0 tiling now falls outside the range between our ideal scale and our
3410  // target raster scale. But it is in our used tilings set, so nothing is
3411  // deleted.
3412  used_tilings.clear();
3413  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3414  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3415  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3416
3417  // If we remove it from our used tilings set, it is outside the range to keep
3418  // so it is deleted.
3419  used_tilings.clear();
3420  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3421  ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3422}
3423
3424TEST_F(PictureLayerImplTest, ScaleCollision) {
3425  gfx::Size tile_size(400, 400);
3426  gfx::Size layer_bounds(1300, 1900);
3427
3428  scoped_refptr<FakePicturePileImpl> pending_pile =
3429      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3430  scoped_refptr<FakePicturePileImpl> active_pile =
3431      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3432
3433  std::vector<PictureLayerTiling*> used_tilings;
3434
3435  SetupTrees(pending_pile, active_pile);
3436
3437  float pending_contents_scale = 1.f;
3438  float active_contents_scale = 2.f;
3439  float device_scale_factor = 1.f;
3440  float page_scale_factor = 1.f;
3441  float maximum_animation_contents_scale = 1.f;
3442  bool animating_transform = false;
3443
3444  EXPECT_TRUE(host_impl_.settings().create_low_res_tiling);
3445  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3446  EXPECT_LT(low_res_factor, 1.f);
3447
3448  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3449                                    pending_contents_scale,
3450                                    device_scale_factor,
3451                                    page_scale_factor,
3452                                    maximum_animation_contents_scale,
3453                                    animating_transform);
3454  SetupDrawPropertiesAndUpdateTiles(active_layer_,
3455                                    active_contents_scale,
3456                                    device_scale_factor,
3457                                    page_scale_factor,
3458                                    maximum_animation_contents_scale,
3459                                    animating_transform);
3460
3461  ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
3462  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3463
3464  EXPECT_EQ(active_contents_scale,
3465            pending_layer_->tilings()->tiling_at(0)->contents_scale());
3466  EXPECT_EQ(pending_contents_scale,
3467            pending_layer_->tilings()->tiling_at(1)->contents_scale());
3468  EXPECT_EQ(active_contents_scale * low_res_factor,
3469            pending_layer_->tilings()->tiling_at(2)->contents_scale());
3470  EXPECT_EQ(pending_contents_scale * low_res_factor,
3471            pending_layer_->tilings()->tiling_at(3)->contents_scale());
3472
3473  EXPECT_EQ(active_contents_scale,
3474            active_layer_->tilings()->tiling_at(0)->contents_scale());
3475  EXPECT_EQ(pending_contents_scale,
3476            active_layer_->tilings()->tiling_at(1)->contents_scale());
3477  EXPECT_EQ(active_contents_scale * low_res_factor,
3478            active_layer_->tilings()->tiling_at(2)->contents_scale());
3479  EXPECT_EQ(pending_contents_scale * low_res_factor,
3480            active_layer_->tilings()->tiling_at(3)->contents_scale());
3481
3482  // The unused low res tiling from the pending tree must be kept or we may add
3483  // it again on the active tree and collide with the pending tree.
3484  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3485  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3486  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3487
3488  EXPECT_EQ(active_contents_scale,
3489            active_layer_->tilings()->tiling_at(0)->contents_scale());
3490  EXPECT_EQ(pending_contents_scale,
3491            active_layer_->tilings()->tiling_at(1)->contents_scale());
3492  EXPECT_EQ(active_contents_scale * low_res_factor,
3493            active_layer_->tilings()->tiling_at(2)->contents_scale());
3494  EXPECT_EQ(pending_contents_scale * low_res_factor,
3495            active_layer_->tilings()->tiling_at(3)->contents_scale());
3496}
3497
3498TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
3499  gfx::Size tile_size(400, 400);
3500  gfx::Size layer_bounds(1300, 1900);
3501
3502  scoped_refptr<FakePicturePileImpl> pending_pile =
3503      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3504  scoped_refptr<FakePicturePileImpl> active_pile =
3505      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3506
3507  SetupTrees(pending_pile, active_pile);
3508  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3509
3510  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3511                                    1.3f,  // ideal contents scale
3512                                    2.7f,  // device scale
3513                                    3.2f,  // page scale
3514                                    1.f,   // maximum animation scale
3515                                    false);
3516  EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3517
3518  // All tilings should be removed when losing output surface.
3519  active_layer_->ReleaseResources();
3520  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
3521  pending_layer_->ReleaseResources();
3522  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3523
3524  // This should create new tilings.
3525  SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3526                                    1.3f,  // ideal contents scale
3527                                    2.7f,  // device scale
3528                                    3.2f,  // page scale
3529                                    1.f,   // maximum animation scale
3530                                    false);
3531  EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3532}
3533
3534TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3535  MockOcclusionTracker<LayerImpl> occlusion_tracker;
3536  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3537
3538  gfx::Size tile_size(400, 400);
3539  gfx::Size layer_bounds(1000, 2000);
3540
3541  scoped_refptr<FakePicturePileImpl> pending_pile =
3542      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3543  scoped_refptr<FakePicturePileImpl> active_pile =
3544      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3545
3546  SetupTrees(pending_pile, active_pile);
3547
3548  SetupDrawPropertiesAndUpdateTiles(pending_layer_, 2.5f, 1.f, 1.f, 1.f, false);
3549  host_impl_.pending_tree()->UpdateDrawProperties();
3550
3551  active_layer_->draw_properties().visible_content_rect =
3552      gfx::Rect(layer_bounds);
3553  host_impl_.active_tree()->UpdateDrawProperties();
3554
3555  float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3556  gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3557  scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3558                              SK_MScalar1 / max_contents_scale);
3559
3560  AppendQuadsData data;
3561  active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3562
3563  // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3564  EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
3565  // The content_to_target_transform should be scaled by the
3566  // MaximumTilingContentsScale on the layer.
3567  EXPECT_EQ(scaled_draw_transform.ToString(),
3568            render_pass->shared_quad_state_list[0]
3569                ->content_to_target_transform.ToString());
3570  // The content_bounds should be scaled by the
3571  // MaximumTilingContentsScale on the layer.
3572  EXPECT_EQ(gfx::Size(2500u, 5000u).ToString(),
3573            render_pass->shared_quad_state_list[0]->content_bounds.ToString());
3574  // The visible_content_rect should be scaled by the
3575  // MaximumTilingContentsScale on the layer.
3576  EXPECT_EQ(
3577      gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3578      render_pass->shared_quad_state_list[0]->visible_content_rect.ToString());
3579}
3580
3581TEST_F(PictureLayerImplTest, UpdateTilesForMasksWithNoVisibleContent) {
3582  gfx::Size tile_size(400, 400);
3583  gfx::Size bounds(100000, 100);
3584
3585  host_impl_.CreatePendingTree();
3586
3587  scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_.pending_tree(), 1);
3588
3589  scoped_ptr<FakePictureLayerImpl> layer_with_mask =
3590      FakePictureLayerImpl::Create(host_impl_.pending_tree(), 2);
3591
3592  layer_with_mask->SetBounds(bounds);
3593  layer_with_mask->SetContentBounds(bounds);
3594
3595  scoped_refptr<FakePicturePileImpl> pending_pile =
3596      FakePicturePileImpl::CreateFilledPile(tile_size, bounds);
3597  pending_pile->set_is_mask(true);
3598  scoped_ptr<FakePictureLayerImpl> mask = FakePictureLayerImpl::CreateWithPile(
3599      host_impl_.pending_tree(), 3, pending_pile);
3600
3601  mask->SetBounds(bounds);
3602  mask->SetContentBounds(bounds);
3603  mask->SetDrawsContent(true);
3604
3605  FakePictureLayerImpl* pending_mask_content = mask.get();
3606  layer_with_mask->SetMaskLayer(mask.PassAs<LayerImpl>());
3607
3608  scoped_ptr<FakePictureLayerImpl> child_of_layer_with_mask =
3609      FakePictureLayerImpl::Create(host_impl_.pending_tree(), 4);
3610
3611  child_of_layer_with_mask->SetBounds(bounds);
3612  child_of_layer_with_mask->SetContentBounds(bounds);
3613  child_of_layer_with_mask->SetDrawsContent(true);
3614
3615  layer_with_mask->AddChild(child_of_layer_with_mask.PassAs<LayerImpl>());
3616
3617  root->AddChild(layer_with_mask.PassAs<LayerImpl>());
3618
3619  host_impl_.pending_tree()->SetRootLayer(root.Pass());
3620
3621  EXPECT_FALSE(pending_mask_content->tilings());
3622  host_impl_.pending_tree()->UpdateDrawProperties();
3623  EXPECT_NE(0u, pending_mask_content->num_tilings());
3624}
3625
3626class PictureLayerImplTestWithDelegatingRenderer : public PictureLayerImplTest {
3627 public:
3628  PictureLayerImplTestWithDelegatingRenderer() : PictureLayerImplTest() {}
3629
3630  virtual void InitializeRenderer() OVERRIDE {
3631    host_impl_.InitializeRenderer(
3632        FakeOutputSurface::CreateDelegating3d().PassAs<OutputSurface>());
3633  }
3634};
3635
3636TEST_F(PictureLayerImplTestWithDelegatingRenderer,
3637       DelegatingRendererWithTileOOM) {
3638  // This test is added for crbug.com/402321, where quad should be produced when
3639  // raster on demand is not allowed and tile is OOM.
3640  gfx::Size tile_size = host_impl_.settings().default_tile_size;
3641  gfx::Size layer_bounds(1000, 1000);
3642
3643  // Create tiles.
3644  scoped_refptr<FakePicturePileImpl> pending_pile =
3645      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3646  SetupPendingTree(pending_pile);
3647  pending_layer_->SetBounds(layer_bounds);
3648  host_impl_.SetViewportSize(layer_bounds);
3649  ActivateTree();
3650  host_impl_.active_tree()->UpdateDrawProperties();
3651  std::vector<Tile*> tiles =
3652      active_layer_->HighResTiling()->AllTilesForTesting();
3653  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3654
3655  // Force tiles after max_tiles to be OOM. TileManager uses
3656  // GlobalStateThatImpactsTilesPriority from LayerTreeHostImpl, and we cannot
3657  // directly set state to host_impl_, so we set policy that would change the
3658  // state. We also need to update tree priority separately.
3659  GlobalStateThatImpactsTilePriority state;
3660  size_t max_tiles = 1;
3661  size_t memory_limit = max_tiles * 4 * tile_size.width() * tile_size.height();
3662  size_t resource_limit = max_tiles;
3663  ManagedMemoryPolicy policy(memory_limit,
3664                             gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
3665                             resource_limit);
3666  host_impl_.SetMemoryPolicy(policy);
3667  host_impl_.SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
3668  host_impl_.ManageTiles();
3669
3670  MockOcclusionTracker<LayerImpl> occlusion_tracker;
3671  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3672  AppendQuadsData data;
3673  active_layer_->WillDraw(DRAW_MODE_HARDWARE, NULL);
3674  active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3675  active_layer_->DidDraw(NULL);
3676
3677  // Even when OOM, quads should be produced, and should be different material
3678  // from quads with resource.
3679  EXPECT_LT(max_tiles, render_pass->quad_list.size());
3680  EXPECT_EQ(DrawQuad::Material::TILED_CONTENT,
3681            render_pass->quad_list.front()->material);
3682  EXPECT_EQ(DrawQuad::Material::SOLID_COLOR,
3683            render_pass->quad_list.back()->material);
3684}
3685
3686class OcclusionTrackingSettings : public LowResTilingsSettings {
3687 public:
3688  OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
3689};
3690
3691class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
3692 public:
3693  OcclusionTrackingPictureLayerImplTest()
3694      : PictureLayerImplTest(OcclusionTrackingSettings()) {}
3695
3696  void VerifyEvictionConsidersOcclusion(
3697      PictureLayerImpl* layer,
3698      size_t expected_occluded_tile_count[NUM_TREE_PRIORITIES]) {
3699    for (int priority_count = 0; priority_count < NUM_TREE_PRIORITIES;
3700         ++priority_count) {
3701      TreePriority tree_priority = static_cast<TreePriority>(priority_count);
3702      size_t occluded_tile_count = 0u;
3703      Tile* last_tile = NULL;
3704
3705      for (PictureLayerImpl::LayerEvictionTileIterator it =
3706               PictureLayerImpl::LayerEvictionTileIterator(layer,
3707                                                           tree_priority);
3708           it;
3709           ++it) {
3710        Tile* tile = *it;
3711        if (!last_tile)
3712          last_tile = tile;
3713
3714        // The only way we will encounter an occluded tile after an unoccluded
3715        // tile is if the priorty bin decreased, the tile is required for
3716        // activation, or the scale changed.
3717        bool tile_is_occluded =
3718            tile->is_occluded_for_tree_priority(tree_priority);
3719        if (tile_is_occluded) {
3720          occluded_tile_count++;
3721
3722          bool last_tile_is_occluded =
3723              last_tile->is_occluded_for_tree_priority(tree_priority);
3724          if (!last_tile_is_occluded) {
3725            TilePriority::PriorityBin tile_priority_bin =
3726                tile->priority_for_tree_priority(tree_priority).priority_bin;
3727            TilePriority::PriorityBin last_tile_priority_bin =
3728                last_tile->priority_for_tree_priority(tree_priority)
3729                    .priority_bin;
3730
3731            EXPECT_TRUE(
3732                (tile_priority_bin < last_tile_priority_bin) ||
3733                tile->required_for_activation() ||
3734                (tile->contents_scale() != last_tile->contents_scale()));
3735          }
3736        }
3737        last_tile = tile;
3738      }
3739      EXPECT_EQ(expected_occluded_tile_count[priority_count],
3740                occluded_tile_count);
3741    }
3742  }
3743};
3744
3745TEST_F(OcclusionTrackingPictureLayerImplTest,
3746       OccludedTilesSkippedDuringRasterization) {
3747  base::TimeTicks time_ticks;
3748  time_ticks += base::TimeDelta::FromMilliseconds(1);
3749  host_impl_.SetCurrentBeginFrameArgs(
3750      CreateBeginFrameArgsForTesting(time_ticks));
3751
3752  gfx::Size tile_size(102, 102);
3753  gfx::Size layer_bounds(1000, 1000);
3754  gfx::Size viewport_size(500, 500);
3755  gfx::Point occluding_layer_position(310, 0);
3756
3757  scoped_refptr<FakePicturePileImpl> pending_pile =
3758      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3759  SetupPendingTree(pending_pile);
3760  pending_layer_->set_fixed_tile_size(tile_size);
3761
3762  host_impl_.SetViewportSize(viewport_size);
3763  host_impl_.pending_tree()->UpdateDrawProperties();
3764
3765  // No occlusion.
3766  int unoccluded_tile_count = 0;
3767  for (PictureLayerImpl::LayerRasterTileIterator it =
3768           PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3769       it;
3770       ++it) {
3771    Tile* tile = *it;
3772
3773    // Occluded tiles should not be iterated over.
3774    EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3775
3776    // Some tiles may not be visible (i.e. outside the viewport). The rest are
3777    // visible and at least partially unoccluded, verified by the above expect.
3778    bool tile_is_visible =
3779        tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3780    if (tile_is_visible)
3781      unoccluded_tile_count++;
3782  }
3783  EXPECT_EQ(unoccluded_tile_count, 25 + 4);
3784
3785  // Partial occlusion.
3786  pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3787  LayerImpl* layer1 = pending_layer_->children()[0];
3788  layer1->SetBounds(layer_bounds);
3789  layer1->SetContentBounds(layer_bounds);
3790  layer1->SetDrawsContent(true);
3791  layer1->SetContentsOpaque(true);
3792  layer1->SetPosition(occluding_layer_position);
3793
3794  time_ticks += base::TimeDelta::FromMilliseconds(200);
3795  host_impl_.SetCurrentBeginFrameArgs(
3796      CreateBeginFrameArgsForTesting(time_ticks));
3797  host_impl_.pending_tree()->UpdateDrawProperties();
3798
3799  unoccluded_tile_count = 0;
3800  for (PictureLayerImpl::LayerRasterTileIterator it =
3801           PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3802       it;
3803       ++it) {
3804    Tile* tile = *it;
3805
3806    EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3807
3808    bool tile_is_visible =
3809        tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3810    if (tile_is_visible)
3811      unoccluded_tile_count++;
3812  }
3813  EXPECT_EQ(unoccluded_tile_count, 20 + 2);
3814
3815  // Full occlusion.
3816  layer1->SetPosition(gfx::Point(0, 0));
3817
3818  time_ticks += base::TimeDelta::FromMilliseconds(200);
3819  host_impl_.SetCurrentBeginFrameArgs(
3820      CreateBeginFrameArgsForTesting(time_ticks));
3821  host_impl_.pending_tree()->UpdateDrawProperties();
3822
3823  unoccluded_tile_count = 0;
3824  for (PictureLayerImpl::LayerRasterTileIterator it =
3825           PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3826       it;
3827       ++it) {
3828    Tile* tile = *it;
3829
3830    EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3831
3832    bool tile_is_visible =
3833        tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3834    if (tile_is_visible)
3835      unoccluded_tile_count++;
3836  }
3837  EXPECT_EQ(unoccluded_tile_count, 0);
3838}
3839
3840TEST_F(OcclusionTrackingPictureLayerImplTest,
3841       OccludedTilesNotMarkedAsRequired) {
3842  base::TimeTicks time_ticks;
3843  time_ticks += base::TimeDelta::FromMilliseconds(1);
3844  host_impl_.SetCurrentBeginFrameArgs(
3845      CreateBeginFrameArgsForTesting(time_ticks));
3846
3847  gfx::Size tile_size(102, 102);
3848  gfx::Size layer_bounds(1000, 1000);
3849  gfx::Size viewport_size(500, 500);
3850  gfx::Point occluding_layer_position(310, 0);
3851
3852  scoped_refptr<FakePicturePileImpl> pending_pile =
3853      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3854  SetupPendingTree(pending_pile);
3855  pending_layer_->set_fixed_tile_size(tile_size);
3856
3857  host_impl_.SetViewportSize(viewport_size);
3858  host_impl_.pending_tree()->UpdateDrawProperties();
3859
3860  // No occlusion.
3861  int occluded_tile_count = 0;
3862  for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3863    PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3864
3865    occluded_tile_count = 0;
3866    for (PictureLayerTiling::CoverageIterator iter(
3867             tiling,
3868             pending_layer_->contents_scale_x(),
3869             gfx::Rect(layer_bounds));
3870         iter;
3871         ++iter) {
3872      if (!*iter)
3873        continue;
3874      const Tile* tile = *iter;
3875
3876      // Fully occluded tiles are not required for activation.
3877      if (tile->is_occluded(PENDING_TREE)) {
3878        EXPECT_FALSE(tile->required_for_activation());
3879        occluded_tile_count++;
3880      }
3881    }
3882    EXPECT_EQ(occluded_tile_count, 0);
3883  }
3884
3885  // Partial occlusion.
3886  pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3887  LayerImpl* layer1 = pending_layer_->children()[0];
3888  layer1->SetBounds(layer_bounds);
3889  layer1->SetContentBounds(layer_bounds);
3890  layer1->SetDrawsContent(true);
3891  layer1->SetContentsOpaque(true);
3892  layer1->SetPosition(occluding_layer_position);
3893
3894  time_ticks += base::TimeDelta::FromMilliseconds(200);
3895  host_impl_.SetCurrentBeginFrameArgs(
3896      CreateBeginFrameArgsForTesting(time_ticks));
3897  host_impl_.pending_tree()->UpdateDrawProperties();
3898
3899  for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3900    PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3901
3902    occluded_tile_count = 0;
3903    for (PictureLayerTiling::CoverageIterator iter(
3904             tiling,
3905             pending_layer_->contents_scale_x(),
3906             gfx::Rect(layer_bounds));
3907         iter;
3908         ++iter) {
3909      if (!*iter)
3910        continue;
3911      const Tile* tile = *iter;
3912
3913      if (tile->is_occluded(PENDING_TREE)) {
3914        EXPECT_FALSE(tile->required_for_activation());
3915        occluded_tile_count++;
3916      }
3917    }
3918    switch (i) {
3919      case 0:
3920        EXPECT_EQ(occluded_tile_count, 5);
3921        break;
3922      case 1:
3923        EXPECT_EQ(occluded_tile_count, 2);
3924        break;
3925      default:
3926        NOTREACHED();
3927    }
3928  }
3929
3930  // Full occlusion.
3931  layer1->SetPosition(gfx::PointF(0, 0));
3932
3933  time_ticks += base::TimeDelta::FromMilliseconds(200);
3934  host_impl_.SetCurrentBeginFrameArgs(
3935      CreateBeginFrameArgsForTesting(time_ticks));
3936  host_impl_.pending_tree()->UpdateDrawProperties();
3937
3938  for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3939    PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3940
3941    occluded_tile_count = 0;
3942    for (PictureLayerTiling::CoverageIterator iter(
3943             tiling,
3944             pending_layer_->contents_scale_x(),
3945             gfx::Rect(layer_bounds));
3946         iter;
3947         ++iter) {
3948      if (!*iter)
3949        continue;
3950      const Tile* tile = *iter;
3951
3952      if (tile->is_occluded(PENDING_TREE)) {
3953        EXPECT_FALSE(tile->required_for_activation());
3954        occluded_tile_count++;
3955      }
3956    }
3957    switch (i) {
3958      case 0:
3959        EXPECT_EQ(occluded_tile_count, 25);
3960        break;
3961      case 1:
3962        EXPECT_EQ(occluded_tile_count, 4);
3963        break;
3964      default:
3965        NOTREACHED();
3966    }
3967  }
3968}
3969
3970TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
3971  gfx::Size tile_size(102, 102);
3972  gfx::Size layer_bounds(1000, 1000);
3973  gfx::Size viewport_size(500, 500);
3974  gfx::Point occluding_layer_position(310, 0);
3975
3976  scoped_refptr<FakePicturePileImpl> pending_pile =
3977      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3978  SetupPendingTree(pending_pile);
3979  pending_layer_->set_fixed_tile_size(tile_size);
3980
3981  ASSERT_TRUE(pending_layer_->CanHaveTilings());
3982
3983  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3984
3985  std::vector<PictureLayerTiling*> tilings;
3986  tilings.push_back(pending_layer_->AddTiling(low_res_factor));
3987  tilings.push_back(pending_layer_->AddTiling(0.3f));
3988  tilings.push_back(pending_layer_->AddTiling(0.7f));
3989  tilings.push_back(pending_layer_->AddTiling(1.0f));
3990  tilings.push_back(pending_layer_->AddTiling(2.0f));
3991
3992  pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3993  LayerImpl* layer1 = pending_layer_->children()[0];
3994  layer1->SetBounds(layer_bounds);
3995  layer1->SetContentBounds(layer_bounds);
3996  layer1->SetDrawsContent(true);
3997  layer1->SetContentsOpaque(true);
3998  layer1->SetPosition(occluding_layer_position);
3999
4000  host_impl_.SetViewportSize(viewport_size);
4001  host_impl_.pending_tree()->UpdateDrawProperties();
4002
4003  int tiling_count = 0;
4004  int occluded_tile_count = 0;
4005  for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
4006           tilings.begin();
4007       tiling_iterator != tilings.end();
4008       ++tiling_iterator) {
4009    std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
4010
4011    occluded_tile_count = 0;
4012    for (size_t i = 0; i < tiles.size(); ++i) {
4013      if (tiles[i]->is_occluded(PENDING_TREE)) {
4014        gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4015            tiles[i]->content_rect(), 1.0f / tiles[i]->contents_scale());
4016        EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
4017        occluded_tile_count++;
4018      }
4019    }
4020    switch (tiling_count) {
4021      case 0:
4022      case 1:
4023        EXPECT_EQ(occluded_tile_count, 2);
4024        break;
4025      case 2:
4026        EXPECT_EQ(occluded_tile_count, 4);
4027        break;
4028      case 3:
4029        EXPECT_EQ(occluded_tile_count, 5);
4030        break;
4031      case 4:
4032        EXPECT_EQ(occluded_tile_count, 30);
4033        break;
4034      default:
4035        NOTREACHED();
4036    }
4037
4038    tiling_count++;
4039  }
4040
4041  EXPECT_EQ(tiling_count, 5);
4042}
4043
4044TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
4045  gfx::Size tile_size(102, 102);
4046  gfx::Size layer_bounds(1000, 1000);
4047  gfx::Size viewport_size(1000, 1000);
4048  gfx::Point occluding_layer_position(310, 0);
4049  gfx::Rect invalidation_rect(230, 230, 102, 102);
4050
4051  scoped_refptr<FakePicturePileImpl> pending_pile =
4052      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4053  scoped_refptr<FakePicturePileImpl> active_pile =
4054      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4055  SetupTrees(pending_pile, active_pile);
4056
4057  // Partially occlude the active layer.
4058  active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
4059  LayerImpl* layer1 = active_layer_->children()[0];
4060  layer1->SetBounds(layer_bounds);
4061  layer1->SetContentBounds(layer_bounds);
4062  layer1->SetDrawsContent(true);
4063  layer1->SetContentsOpaque(true);
4064  layer1->SetPosition(occluding_layer_position);
4065
4066  // Partially invalidate the pending layer.
4067  pending_layer_->set_invalidation(invalidation_rect);
4068
4069  host_impl_.SetViewportSize(viewport_size);
4070
4071  active_layer_->CreateDefaultTilingsAndTiles();
4072  pending_layer_->CreateDefaultTilingsAndTiles();
4073
4074  for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4075    PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4076
4077    for (PictureLayerTiling::CoverageIterator iter(
4078             tiling,
4079             pending_layer_->contents_scale_x(),
4080             gfx::Rect(layer_bounds));
4081         iter;
4082         ++iter) {
4083      if (!*iter)
4084        continue;
4085      const Tile* tile = *iter;
4086
4087      // All tiles are unoccluded on the pending tree.
4088      EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4089
4090      Tile* twin_tile =
4091          pending_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
4092      gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4093          tile->content_rect(), 1.0f / tile->contents_scale());
4094
4095      if (scaled_content_rect.Intersects(invalidation_rect)) {
4096        // Tiles inside the invalidation rect are only on the pending tree.
4097        EXPECT_NE(tile, twin_tile);
4098
4099        // Unshared tiles should be unoccluded on the active tree by default.
4100        EXPECT_FALSE(tile->is_occluded(ACTIVE_TREE));
4101      } else {
4102        // Tiles outside the invalidation rect are shared between both trees.
4103        EXPECT_EQ(tile, twin_tile);
4104        // Shared tiles are occluded on the active tree iff they lie beneath the
4105        // occluding layer.
4106        EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4107                  scaled_content_rect.x() >= occluding_layer_position.x());
4108      }
4109    }
4110  }
4111
4112  for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4113    PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4114
4115    for (PictureLayerTiling::CoverageIterator iter(
4116             tiling,
4117             active_layer_->contents_scale_x(),
4118             gfx::Rect(layer_bounds));
4119         iter;
4120         ++iter) {
4121      if (!*iter)
4122        continue;
4123      const Tile* tile = *iter;
4124
4125      Tile* twin_tile =
4126          active_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
4127      gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4128          tile->content_rect(), 1.0f / tile->contents_scale());
4129
4130      // Since we've already checked the shared tiles, only consider tiles in
4131      // the invalidation rect.
4132      if (scaled_content_rect.Intersects(invalidation_rect)) {
4133        // Tiles inside the invalidation rect are only on the active tree.
4134        EXPECT_NE(tile, twin_tile);
4135
4136        // Unshared tiles should be unoccluded on the pending tree by default.
4137        EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4138
4139        // Unshared tiles are occluded on the active tree iff they lie beneath
4140        // the occluding layer.
4141        EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4142                  scaled_content_rect.x() >= occluding_layer_position.x());
4143      }
4144    }
4145  }
4146}
4147
4148TEST_F(OcclusionTrackingPictureLayerImplTest,
4149       OccludedTilesConsideredDuringEviction) {
4150  gfx::Size tile_size(102, 102);
4151  gfx::Size layer_bounds(1000, 1000);
4152  gfx::Size viewport_size(500, 500);
4153  gfx::Point pending_occluding_layer_position(310, 0);
4154  gfx::Point active_occluding_layer_position(0, 310);
4155  gfx::Rect invalidation_rect(230, 230, 102, 102);
4156
4157  scoped_refptr<FakePicturePileImpl> pending_pile =
4158      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4159  scoped_refptr<FakePicturePileImpl> active_pile =
4160      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4161  SetupTrees(pending_pile, active_pile);
4162
4163  pending_layer_->set_fixed_tile_size(tile_size);
4164  active_layer_->set_fixed_tile_size(tile_size);
4165
4166  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
4167
4168  std::vector<PictureLayerTiling*> tilings;
4169  tilings.push_back(pending_layer_->AddTiling(low_res_factor));
4170  tilings.push_back(pending_layer_->AddTiling(0.3f));
4171  tilings.push_back(pending_layer_->AddTiling(0.7f));
4172  tilings.push_back(pending_layer_->AddTiling(1.0f));
4173  tilings.push_back(pending_layer_->AddTiling(2.0f));
4174
4175  EXPECT_EQ(5u, pending_layer_->num_tilings());
4176  EXPECT_EQ(5u, active_layer_->num_tilings());
4177
4178  // Partially occlude the pending layer.
4179  pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4180  LayerImpl* pending_occluding_layer = pending_layer_->children()[0];
4181  pending_occluding_layer->SetBounds(layer_bounds);
4182  pending_occluding_layer->SetContentBounds(layer_bounds);
4183  pending_occluding_layer->SetDrawsContent(true);
4184  pending_occluding_layer->SetContentsOpaque(true);
4185  pending_occluding_layer->SetPosition(pending_occluding_layer_position);
4186
4187  // Partially occlude the active layer.
4188  active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
4189  LayerImpl* active_occluding_layer = active_layer_->children()[0];
4190  active_occluding_layer->SetBounds(layer_bounds);
4191  active_occluding_layer->SetContentBounds(layer_bounds);
4192  active_occluding_layer->SetDrawsContent(true);
4193  active_occluding_layer->SetContentsOpaque(true);
4194  active_occluding_layer->SetPosition(active_occluding_layer_position);
4195
4196  // Partially invalidate the pending layer. Tiles inside the invalidation rect
4197  // are not shared between trees.
4198  pending_layer_->set_invalidation(invalidation_rect);
4199
4200  host_impl_.SetViewportSize(viewport_size);
4201  host_impl_.active_tree()->UpdateDrawProperties();
4202  host_impl_.pending_tree()->UpdateDrawProperties();
4203
4204  // The expected number of occluded tiles on each of the 5 tilings for each of
4205  // the 3 tree priorities.
4206  size_t expected_occluded_tile_count_on_both[] = {9u, 1u, 1u, 1u, 1u};
4207  size_t expected_occluded_tile_count_on_active[] = {30u, 5u, 4u, 2u, 2u};
4208  size_t expected_occluded_tile_count_on_pending[] = {30u, 5u, 4u, 2u, 2u};
4209
4210  // The total expected number of occluded tiles on all tilings for each of the
4211  // 3 tree priorities.
4212  size_t total_expected_occluded_tile_count[] = {13u, 43u, 43u};
4213
4214  ASSERT_EQ(arraysize(total_expected_occluded_tile_count), NUM_TREE_PRIORITIES);
4215
4216  // Verify number of occluded tiles on the pending layer for each tiling.
4217  for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4218    PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4219    tiling->CreateAllTilesForTesting();
4220
4221    size_t occluded_tile_count_on_pending = 0u;
4222    size_t occluded_tile_count_on_active = 0u;
4223    size_t occluded_tile_count_on_both = 0u;
4224    for (PictureLayerTiling::CoverageIterator iter(
4225             tiling,
4226             pending_layer_->contents_scale_x(),
4227             gfx::Rect(layer_bounds));
4228         iter;
4229         ++iter) {
4230      Tile* tile = *iter;
4231
4232      if (tile->is_occluded(PENDING_TREE))
4233        occluded_tile_count_on_pending++;
4234      if (tile->is_occluded(ACTIVE_TREE))
4235        occluded_tile_count_on_active++;
4236      if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4237        occluded_tile_count_on_both++;
4238    }
4239    EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4240              occluded_tile_count_on_pending)
4241        << i;
4242    EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4243              occluded_tile_count_on_active)
4244        << i;
4245    EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4246              occluded_tile_count_on_both)
4247        << i;
4248  }
4249
4250  // Verify number of occluded tiles on the active layer for each tiling.
4251  for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4252    PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4253    tiling->CreateAllTilesForTesting();
4254
4255    size_t occluded_tile_count_on_pending = 0u;
4256    size_t occluded_tile_count_on_active = 0u;
4257    size_t occluded_tile_count_on_both = 0u;
4258    for (PictureLayerTiling::CoverageIterator iter(
4259             tiling,
4260             pending_layer_->contents_scale_x(),
4261             gfx::Rect(layer_bounds));
4262         iter;
4263         ++iter) {
4264      Tile* tile = *iter;
4265
4266      if (tile->is_occluded(PENDING_TREE))
4267        occluded_tile_count_on_pending++;
4268      if (tile->is_occluded(ACTIVE_TREE))
4269        occluded_tile_count_on_active++;
4270      if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4271        occluded_tile_count_on_both++;
4272    }
4273    EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4274              occluded_tile_count_on_pending)
4275        << i;
4276    EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4277              occluded_tile_count_on_active)
4278        << i;
4279    EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4280              occluded_tile_count_on_both)
4281        << i;
4282  }
4283
4284  std::vector<Tile*> all_tiles;
4285  for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
4286           tilings.begin();
4287       tiling_iterator != tilings.end();
4288       ++tiling_iterator) {
4289    std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
4290    std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
4291  }
4292
4293  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
4294
4295  VerifyEvictionConsidersOcclusion(pending_layer_,
4296                                   total_expected_occluded_tile_count);
4297  VerifyEvictionConsidersOcclusion(active_layer_,
4298                                   total_expected_occluded_tile_count);
4299}
4300
4301TEST_F(PictureLayerImplTest, RecycledTwinLayer) {
4302  gfx::Size tile_size(102, 102);
4303  gfx::Size layer_bounds(1000, 1000);
4304
4305  scoped_refptr<FakePicturePileImpl> pile =
4306      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4307  SetupPendingTree(pile);
4308  EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4309
4310  ActivateTree();
4311  EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4312  EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4313
4314  SetupPendingTree(pile);
4315  EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4316  EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4317
4318  ActivateTree();
4319  EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4320  EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4321
4322  host_impl_.ResetRecycleTreeForTesting();
4323  EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4324}
4325
4326void PictureLayerImplTest::TestQuadsForSolidColor(bool test_for_solid) {
4327  base::TimeTicks time_ticks;
4328  time_ticks += base::TimeDelta::FromMilliseconds(1);
4329  host_impl_.SetCurrentBeginFrameArgs(
4330      CreateBeginFrameArgsForTesting(time_ticks));
4331
4332  gfx::Size tile_size(100, 100);
4333  gfx::Size layer_bounds(200, 200);
4334  gfx::Rect layer_rect(layer_bounds);
4335
4336  FakeContentLayerClient client;
4337  scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4338  FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4339  scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4340  host->SetRootLayer(layer);
4341  PicturePile* pile = layer->GetPicturePileForTesting();
4342
4343  host_impl_.SetViewportSize(layer_bounds);
4344
4345  int frame_number = 0;
4346  FakeRenderingStatsInstrumentation stats_instrumentation;
4347
4348  client.set_fill_with_nonsolid_color(!test_for_solid);
4349
4350  Region invalidation(layer_rect);
4351  pile->UpdateAndExpandInvalidation(&client,
4352                                    &invalidation,
4353                                    SK_ColorWHITE,
4354                                    false,
4355                                    false,
4356                                    layer_bounds,
4357                                    layer_rect,
4358                                    frame_number++,
4359                                    Picture::RECORD_NORMALLY,
4360                                    &stats_instrumentation);
4361
4362  scoped_refptr<PicturePileImpl> pending_pile =
4363      PicturePileImpl::CreateFromOther(pile);
4364
4365  SetupPendingTree(pending_pile);
4366  ActivateTree();
4367
4368  active_layer_->set_fixed_tile_size(tile_size);
4369  host_impl_.active_tree()->UpdateDrawProperties();
4370  if (test_for_solid) {
4371    EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4372  } else {
4373    ASSERT_TRUE(active_layer_->tilings());
4374    ASSERT_GT(active_layer_->tilings()->num_tilings(), 0u);
4375    std::vector<Tile*> tiles =
4376        active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
4377    EXPECT_FALSE(tiles.empty());
4378    host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4379  }
4380
4381  MockOcclusionTracker<LayerImpl> occlusion_tracker;
4382  scoped_ptr<RenderPass> render_pass = RenderPass::Create();
4383  AppendQuadsData data;
4384  active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
4385  active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
4386  active_layer_->DidDraw(NULL);
4387
4388  DrawQuad::Material expected = test_for_solid
4389                                    ? DrawQuad::Material::SOLID_COLOR
4390                                    : DrawQuad::Material::TILED_CONTENT;
4391  EXPECT_EQ(expected, render_pass->quad_list.front()->material);
4392}
4393
4394TEST_F(PictureLayerImplTest, DrawSolidQuads) {
4395  TestQuadsForSolidColor(true);
4396}
4397
4398TEST_F(PictureLayerImplTest, DrawNonSolidQuads) {
4399  TestQuadsForSolidColor(false);
4400}
4401
4402TEST_F(PictureLayerImplTest, NonSolidToSolidNoTilings) {
4403  base::TimeTicks time_ticks;
4404  time_ticks += base::TimeDelta::FromMilliseconds(1);
4405  host_impl_.SetCurrentBeginFrameArgs(
4406      CreateBeginFrameArgsForTesting(time_ticks));
4407
4408  gfx::Size tile_size(100, 100);
4409  gfx::Size layer_bounds(200, 200);
4410  gfx::Rect layer_rect(layer_bounds);
4411
4412  FakeContentLayerClient client;
4413  scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4414  FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4415  scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4416  host->SetRootLayer(layer);
4417  PicturePile* pile = layer->GetPicturePileForTesting();
4418
4419  host_impl_.SetViewportSize(layer_bounds);
4420
4421  int frame_number = 0;
4422  FakeRenderingStatsInstrumentation stats_instrumentation;
4423
4424  client.set_fill_with_nonsolid_color(true);
4425
4426  Region invalidation1(layer_rect);
4427  pile->UpdateAndExpandInvalidation(&client,
4428                                    &invalidation1,
4429                                    SK_ColorWHITE,
4430                                    false,
4431                                    false,
4432                                    layer_bounds,
4433                                    layer_rect,
4434                                    frame_number++,
4435                                    Picture::RECORD_NORMALLY,
4436                                    &stats_instrumentation);
4437
4438  scoped_refptr<PicturePileImpl> pending_pile1 =
4439      PicturePileImpl::CreateFromOther(pile);
4440
4441  SetupPendingTree(pending_pile1);
4442  ActivateTree();
4443  host_impl_.active_tree()->UpdateDrawProperties();
4444
4445  // We've started with a solid layer that contains some tilings.
4446  ASSERT_TRUE(active_layer_->tilings());
4447  EXPECT_NE(0u, active_layer_->tilings()->num_tilings());
4448
4449  client.set_fill_with_nonsolid_color(false);
4450
4451  Region invalidation2(layer_rect);
4452  pile->UpdateAndExpandInvalidation(&client,
4453                                    &invalidation2,
4454                                    SK_ColorWHITE,
4455                                    false,
4456                                    false,
4457                                    layer_bounds,
4458                                    layer_rect,
4459                                    frame_number++,
4460                                    Picture::RECORD_NORMALLY,
4461                                    &stats_instrumentation);
4462
4463  scoped_refptr<PicturePileImpl> pending_pile2 =
4464      PicturePileImpl::CreateFromOther(pile);
4465
4466  SetupPendingTree(pending_pile2);
4467  ActivateTree();
4468
4469  // We've switched to a solid color, so we should end up with no tilings.
4470  ASSERT_TRUE(active_layer_->tilings());
4471  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4472}
4473
4474}  // namespace
4475}  // namespace cc
4476