picture_layer_impl_unittest.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "cc/layers/picture_layer_impl.h"
6
7#include <algorithm>
8#include <limits>
9#include <set>
10#include <utility>
11
12#include "cc/layers/append_quads_data.h"
13#include "cc/layers/picture_layer.h"
14#include "cc/test/fake_content_layer_client.h"
15#include "cc/test/fake_impl_proxy.h"
16#include "cc/test/fake_layer_tree_host_impl.h"
17#include "cc/test/fake_output_surface.h"
18#include "cc/test/fake_picture_layer_impl.h"
19#include "cc/test/fake_picture_pile_impl.h"
20#include "cc/test/geometry_test_utils.h"
21#include "cc/test/impl_side_painting_settings.h"
22#include "cc/test/layer_test_common.h"
23#include "cc/test/mock_quad_culler.h"
24#include "cc/test/test_shared_bitmap_manager.h"
25#include "cc/test/test_web_graphics_context_3d.h"
26#include "cc/trees/layer_tree_impl.h"
27#include "testing/gtest/include/gtest/gtest.h"
28#include "ui/gfx/rect_conversions.h"
29
30namespace cc {
31namespace {
32
33class MockCanvas : public SkCanvas {
34 public:
35  explicit MockCanvas(int w, int h) : SkCanvas(w, h) {}
36
37  virtual void drawRect(const SkRect& rect, const SkPaint& paint) OVERRIDE {
38    // Capture calls before SkCanvas quickReject() kicks in.
39    rects_.push_back(rect);
40  }
41
42  std::vector<SkRect> rects_;
43};
44
45class PictureLayerImplTest : public testing::Test {
46 public:
47  PictureLayerImplTest()
48      : proxy_(base::MessageLoopProxy::current()),
49        host_impl_(ImplSidePaintingSettings(),
50                   &proxy_,
51                   &shared_bitmap_manager_),
52        id_(7) {}
53
54  explicit PictureLayerImplTest(const LayerTreeSettings& settings)
55      : proxy_(base::MessageLoopProxy::current()),
56        host_impl_(settings, &proxy_, &shared_bitmap_manager_),
57        id_(7) {}
58
59  virtual ~PictureLayerImplTest() {
60  }
61
62  virtual void SetUp() OVERRIDE {
63    InitializeRenderer();
64  }
65
66  virtual void InitializeRenderer() {
67    host_impl_.InitializeRenderer(
68        FakeOutputSurface::Create3d().PassAs<OutputSurface>());
69  }
70
71  void SetupDefaultTrees(const gfx::Size& layer_bounds) {
72    gfx::Size tile_size(100, 100);
73
74    scoped_refptr<FakePicturePileImpl> pending_pile =
75        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
76    scoped_refptr<FakePicturePileImpl> active_pile =
77        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
78
79    SetupTrees(pending_pile, active_pile);
80  }
81
82  void ActivateTree() {
83    host_impl_.ActivatePendingTree();
84    CHECK(!host_impl_.pending_tree());
85    pending_layer_ = NULL;
86    active_layer_ = static_cast<FakePictureLayerImpl*>(
87        host_impl_.active_tree()->LayerById(id_));
88  }
89
90  void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
91                                          const gfx::Size& tile_size) {
92    SetupDefaultTrees(layer_bounds);
93    pending_layer_->set_fixed_tile_size(tile_size);
94    active_layer_->set_fixed_tile_size(tile_size);
95  }
96
97  void SetupTrees(
98      scoped_refptr<PicturePileImpl> pending_pile,
99      scoped_refptr<PicturePileImpl> active_pile) {
100    SetupPendingTree(active_pile);
101    ActivateTree();
102    SetupPendingTree(pending_pile);
103  }
104
105  void CreateHighLowResAndSetAllTilesVisible() {
106    // Active layer must get updated first so pending layer can share from it.
107    active_layer_->CreateDefaultTilingsAndTiles();
108    active_layer_->SetAllTilesVisible();
109    pending_layer_->CreateDefaultTilingsAndTiles();
110    pending_layer_->SetAllTilesVisible();
111  }
112
113  void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
114    active_layer_->AddTiling(2.3f);
115    active_layer_->AddTiling(1.0f);
116    active_layer_->AddTiling(0.5f);
117    for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
118      active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
119    pending_layer_->set_invalidation(invalidation);
120    for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
121      pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
122  }
123
124  void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
125    host_impl_.CreatePendingTree();
126    LayerTreeImpl* pending_tree = host_impl_.pending_tree();
127    // Clear recycled tree.
128    pending_tree->DetachLayerTree();
129
130    scoped_ptr<FakePictureLayerImpl> pending_layer =
131        FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
132    pending_layer->SetDrawsContent(true);
133    pending_layer->SetAnchorPoint(gfx::PointF());
134    pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
135
136    pending_layer_ = static_cast<FakePictureLayerImpl*>(
137        host_impl_.pending_tree()->LayerById(id_));
138    pending_layer_->DoPostCommitInitializationIfNeeded();
139  }
140
141  static void VerifyAllTilesExistAndHavePile(
142      const PictureLayerTiling* tiling,
143      PicturePileImpl* pile) {
144    for (PictureLayerTiling::CoverageIterator iter(
145             tiling, tiling->contents_scale(), tiling->TilingRect());
146         iter;
147         ++iter) {
148      EXPECT_TRUE(*iter);
149      EXPECT_EQ(pile, iter->picture_pile());
150    }
151  }
152
153  void SetContentsScaleOnBothLayers(float contents_scale,
154                                    float device_scale_factor,
155                                    float page_scale_factor,
156                                    float maximum_animation_contents_scale,
157                                    bool animating_transform) {
158    float result_scale_x, result_scale_y;
159    gfx::Size result_bounds;
160    pending_layer_->CalculateContentsScale(contents_scale,
161                                           device_scale_factor,
162                                           page_scale_factor,
163                                           maximum_animation_contents_scale,
164                                           animating_transform,
165                                           &result_scale_x,
166                                           &result_scale_y,
167                                           &result_bounds);
168    active_layer_->CalculateContentsScale(contents_scale,
169                                          device_scale_factor,
170                                          page_scale_factor,
171                                          maximum_animation_contents_scale,
172                                          animating_transform,
173                                          &result_scale_x,
174                                          &result_scale_y,
175                                          &result_bounds);
176  }
177
178  void ResetTilingsAndRasterScales() {
179    pending_layer_->ReleaseResources();
180    active_layer_->ReleaseResources();
181  }
182
183  void AssertAllTilesRequired(PictureLayerTiling* tiling) {
184    std::vector<Tile*> tiles = tiling->AllTilesForTesting();
185    for (size_t i = 0; i < tiles.size(); ++i)
186      EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
187    EXPECT_GT(tiles.size(), 0u);
188  }
189
190  void AssertNoTilesRequired(PictureLayerTiling* tiling) {
191    std::vector<Tile*> tiles = tiling->AllTilesForTesting();
192    for (size_t i = 0; i < tiles.size(); ++i)
193      EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
194    EXPECT_GT(tiles.size(), 0u);
195  }
196
197 protected:
198  void TestTileGridAlignmentCommon() {
199    // Layer to span 4 raster tiles in x and in y
200    ImplSidePaintingSettings settings;
201    gfx::Size layer_size(
202        settings.default_tile_size.width() * 7 / 2,
203        settings.default_tile_size.height() * 7 / 2);
204
205    scoped_refptr<FakePicturePileImpl> pending_pile =
206        FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
207    scoped_refptr<FakePicturePileImpl> active_pile =
208        FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
209
210    SetupTrees(pending_pile, active_pile);
211
212    float result_scale_x, result_scale_y;
213    gfx::Size result_bounds;
214    active_layer_->CalculateContentsScale(1.f,
215                                          1.f,
216                                          1.f,
217                                          1.f,
218                                          false,
219                                          &result_scale_x,
220                                          &result_scale_y,
221                                          &result_bounds);
222
223    // Add 1x1 rects at the centers of each tile, then re-record pile contents
224    active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
225    std::vector<Tile*> tiles =
226        active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
227    EXPECT_EQ(16u, tiles.size());
228    std::vector<SkRect> rects;
229    std::vector<Tile*>::const_iterator tile_iter;
230    for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
231      gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
232      gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
233      active_pile->add_draw_rect(rect);
234      rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
235    }
236    // Force re-record with newly injected content
237    active_pile->RemoveRecordingAt(0, 0);
238    active_pile->AddRecordingAt(0, 0);
239
240    std::vector<SkRect>::const_iterator rect_iter = rects.begin();
241    for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
242      MockCanvas mock_canvas(1000, 1000);
243      active_pile->RasterDirect(
244          &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
245
246      // This test verifies that when drawing the contents of a specific tile
247      // at content scale 1.0, the playback canvas never receives content from
248      // neighboring tiles which indicates that the tile grid embedded in
249      // SkPicture is perfectly aligned with the compositor's tiles.
250      EXPECT_EQ(1u, mock_canvas.rects_.size());
251      EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]);
252      rect_iter++;
253    }
254  }
255
256  FakeImplProxy proxy_;
257  TestSharedBitmapManager shared_bitmap_manager_;
258  FakeLayerTreeHostImpl host_impl_;
259  int id_;
260  FakePictureLayerImpl* pending_layer_;
261  FakePictureLayerImpl* active_layer_;
262
263 private:
264  DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
265};
266
267TEST_F(PictureLayerImplTest, TileGridAlignment) {
268  host_impl_.SetDeviceScaleFactor(1.f);
269  TestTileGridAlignmentCommon();
270}
271
272TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
273  host_impl_.SetDeviceScaleFactor(2.f);
274  TestTileGridAlignmentCommon();
275}
276
277TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
278  gfx::Size tile_size(100, 100);
279  gfx::Size layer_bounds(400, 400);
280
281  scoped_refptr<FakePicturePileImpl> pending_pile =
282      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
283  scoped_refptr<FakePicturePileImpl> active_pile =
284      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
285
286  SetupTrees(pending_pile, active_pile);
287
288  Region invalidation;
289  AddDefaultTilingsWithInvalidation(invalidation);
290
291  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
292            active_layer_->tilings()->num_tilings());
293
294  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
295  EXPECT_GT(tilings->num_tilings(), 0u);
296  for (size_t i = 0; i < tilings->num_tilings(); ++i)
297    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
298}
299
300TEST_F(PictureLayerImplTest, TileManagerRegisterUnregister) {
301  gfx::Size tile_size(100, 100);
302  gfx::Size layer_bounds(400, 400);
303
304  scoped_refptr<FakePicturePileImpl> pending_pile =
305      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
306  scoped_refptr<FakePicturePileImpl> active_pile =
307      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
308
309  SetupTrees(pending_pile, active_pile);
310
311  std::vector<TileManager::PairedPictureLayer> paired_layers;
312  host_impl_.tile_manager()->GetPairedPictureLayers(&paired_layers);
313  EXPECT_EQ(0u, paired_layers.size());
314
315  // Update tile priorities will force the layer to register itself.
316  float dummy_contents_scale_x;
317  float dummy_contents_scale_y;
318  gfx::Size dummy_content_bounds;
319  active_layer_->CalculateContentsScale(1.f,
320                                        1.f,
321                                        1.f,
322                                        1.f,
323                                        false,
324                                        &dummy_contents_scale_x,
325                                        &dummy_contents_scale_y,
326                                        &dummy_content_bounds);
327  active_layer_->UpdateTilePriorities();
328  host_impl_.pending_tree()->UpdateDrawProperties();
329  pending_layer_->CalculateContentsScale(1.f,
330                                         1.f,
331                                         1.f,
332                                         1.f,
333                                         false,
334                                         &dummy_contents_scale_x,
335                                         &dummy_contents_scale_y,
336                                         &dummy_content_bounds);
337  pending_layer_->UpdateTilePriorities();
338
339  host_impl_.tile_manager()->GetPairedPictureLayers(&paired_layers);
340  EXPECT_EQ(1u, paired_layers.size());
341  EXPECT_EQ(active_layer_, paired_layers[0].active_layer);
342  EXPECT_EQ(pending_layer_, paired_layers[0].pending_layer);
343
344  // Destroy and recreate tile manager.
345  host_impl_.DidLoseOutputSurface();
346  scoped_ptr<TestWebGraphicsContext3D> context =
347      TestWebGraphicsContext3D::Create();
348  host_impl_.InitializeRenderer(
349      FakeOutputSurface::Create3d(context.Pass()).PassAs<OutputSurface>());
350
351  host_impl_.tile_manager()->GetPairedPictureLayers(&paired_layers);
352  EXPECT_EQ(0u, paired_layers.size());
353
354  active_layer_->CalculateContentsScale(1.f,
355                                        1.f,
356                                        1.f,
357                                        1.f,
358                                        false,
359                                        &dummy_contents_scale_x,
360                                        &dummy_contents_scale_y,
361                                        &dummy_content_bounds);
362  active_layer_->UpdateTilePriorities();
363  host_impl_.pending_tree()->UpdateDrawProperties();
364  pending_layer_->CalculateContentsScale(1.f,
365                                         1.f,
366                                         1.f,
367                                         1.f,
368                                         false,
369                                         &dummy_contents_scale_x,
370                                         &dummy_contents_scale_y,
371                                         &dummy_content_bounds);
372  pending_layer_->UpdateTilePriorities();
373
374  host_impl_.tile_manager()->GetPairedPictureLayers(&paired_layers);
375  EXPECT_EQ(1u, paired_layers.size());
376  EXPECT_EQ(active_layer_, paired_layers[0].active_layer);
377  EXPECT_EQ(pending_layer_, paired_layers[0].pending_layer);
378}
379
380TEST_F(PictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
381  base::TimeTicks time_ticks;
382  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
383
384  gfx::Size tile_size(100, 100);
385  gfx::Size layer_bounds(400, 400);
386
387  scoped_refptr<FakePicturePileImpl> pending_pile =
388      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
389  scoped_refptr<FakePicturePileImpl> active_pile =
390      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
391
392  SetupTrees(pending_pile, active_pile);
393
394  Region invalidation;
395  AddDefaultTilingsWithInvalidation(invalidation);
396  float dummy_contents_scale_x;
397  float dummy_contents_scale_y;
398  gfx::Size dummy_content_bounds;
399  active_layer_->CalculateContentsScale(1.f,
400                                        1.f,
401                                        1.f,
402                                        1.f,
403                                        false,
404                                        &dummy_contents_scale_x,
405                                        &dummy_contents_scale_y,
406                                        &dummy_content_bounds);
407
408  // UpdateTilePriorities with valid viewport. Should update tile viewport.
409  bool valid_for_tile_management = true;
410  gfx::Rect viewport = gfx::Rect(layer_bounds);
411  gfx::Transform transform;
412  host_impl_.SetExternalDrawConstraints(
413      transform, viewport, viewport, valid_for_tile_management);
414  active_layer_->draw_properties().visible_content_rect = viewport;
415  active_layer_->draw_properties().screen_space_transform = transform;
416  active_layer_->UpdateTilePriorities();
417
418  gfx::Rect visible_rect_for_tile_priority =
419      active_layer_->visible_rect_for_tile_priority();
420  EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
421  gfx::Size viewport_size_for_tile_priority =
422      active_layer_->viewport_size_for_tile_priority();
423  EXPECT_FALSE(viewport_size_for_tile_priority.IsEmpty());
424  gfx::Transform screen_space_transform_for_tile_priority =
425      active_layer_->screen_space_transform_for_tile_priority();
426
427  // Expand viewport and set it as invalid for prioritizing tiles.
428  // Should not update tile viewport.
429  time_ticks += base::TimeDelta::FromMilliseconds(200);
430  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
431  valid_for_tile_management = false;
432  viewport = gfx::ScaleToEnclosingRect(viewport, 2);
433  transform.Translate(1.f, 1.f);
434  active_layer_->draw_properties().visible_content_rect = viewport;
435  active_layer_->draw_properties().screen_space_transform = transform;
436  host_impl_.SetExternalDrawConstraints(
437      transform, viewport, viewport, valid_for_tile_management);
438  active_layer_->UpdateTilePriorities();
439
440  EXPECT_RECT_EQ(visible_rect_for_tile_priority,
441                 active_layer_->visible_rect_for_tile_priority());
442  EXPECT_SIZE_EQ(viewport_size_for_tile_priority,
443                 active_layer_->viewport_size_for_tile_priority());
444  EXPECT_TRANSFORMATION_MATRIX_EQ(
445      screen_space_transform_for_tile_priority,
446      active_layer_->screen_space_transform_for_tile_priority());
447
448  // Keep expanded viewport but mark it valid. Should update tile viewport.
449  time_ticks += base::TimeDelta::FromMilliseconds(200);
450  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
451  valid_for_tile_management = true;
452  host_impl_.SetExternalDrawConstraints(
453      transform, viewport, viewport, valid_for_tile_management);
454  active_layer_->UpdateTilePriorities();
455
456  EXPECT_FALSE(visible_rect_for_tile_priority ==
457               active_layer_->visible_rect_for_tile_priority());
458  EXPECT_FALSE(viewport_size_for_tile_priority ==
459               active_layer_->viewport_size_for_tile_priority());
460  EXPECT_FALSE(screen_space_transform_for_tile_priority ==
461               active_layer_->screen_space_transform_for_tile_priority());
462}
463
464TEST_F(PictureLayerImplTest, InvalidViewportAfterReleaseResources) {
465  base::TimeTicks time_ticks;
466  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
467
468  gfx::Size tile_size(100, 100);
469  gfx::Size layer_bounds(400, 400);
470
471  scoped_refptr<FakePicturePileImpl> pending_pile =
472      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
473  scoped_refptr<FakePicturePileImpl> active_pile =
474      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
475
476  SetupTrees(pending_pile, active_pile);
477
478  Region invalidation;
479  AddDefaultTilingsWithInvalidation(invalidation);
480
481  bool valid_for_tile_management = false;
482  gfx::Rect viewport = gfx::Rect(layer_bounds);
483  host_impl_.SetExternalDrawConstraints(
484      gfx::Transform(), viewport, viewport, valid_for_tile_management);
485  ResetTilingsAndRasterScales();
486  host_impl_.pending_tree()->UpdateDrawProperties();
487  host_impl_.active_tree()->UpdateDrawProperties();
488  EXPECT_TRUE(active_layer_->HighResTiling());
489
490  size_t num_tilings = active_layer_->num_tilings();
491  active_layer_->UpdateTilePriorities();
492  pending_layer_->AddTiling(0.5f);
493  EXPECT_EQ(num_tilings + 1, active_layer_->num_tilings());
494}
495
496TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
497  gfx::Size tile_size(100, 100);
498  gfx::Size layer_bounds(400, 400);
499  gfx::Rect layer_invalidation(150, 200, 30, 180);
500
501  scoped_refptr<FakePicturePileImpl> pending_pile =
502      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
503  scoped_refptr<FakePicturePileImpl> active_pile =
504      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
505
506  SetupTrees(pending_pile, active_pile);
507
508  Region invalidation(layer_invalidation);
509  AddDefaultTilingsWithInvalidation(invalidation);
510
511  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
512  EXPECT_GT(tilings->num_tilings(), 0u);
513  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
514    const PictureLayerTiling* tiling = tilings->tiling_at(i);
515    gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
516        layer_invalidation,
517        tiling->contents_scale());
518    for (PictureLayerTiling::CoverageIterator iter(
519             tiling, tiling->contents_scale(), tiling->TilingRect());
520         iter;
521         ++iter) {
522      EXPECT_TRUE(*iter);
523      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
524      if (iter.geometry_rect().Intersects(content_invalidation))
525        EXPECT_EQ(pending_pile, iter->picture_pile());
526      else
527        EXPECT_EQ(active_pile, iter->picture_pile());
528    }
529  }
530}
531
532TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
533  gfx::Size tile_size(90, 80);
534  gfx::Size layer_bounds(300, 500);
535
536  scoped_refptr<FakePicturePileImpl> pending_pile =
537      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
538  scoped_refptr<FakePicturePileImpl> active_pile =
539      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
540
541  SetupTrees(pending_pile, active_pile);
542
543  Region invalidation((gfx::Rect(layer_bounds)));
544  AddDefaultTilingsWithInvalidation(invalidation);
545
546  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
547            active_layer_->tilings()->num_tilings());
548
549  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
550  EXPECT_GT(tilings->num_tilings(), 0u);
551  for (size_t i = 0; i < tilings->num_tilings(); ++i)
552    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
553}
554
555TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
556  gfx::Size tile_size(90, 80);
557  gfx::Size active_layer_bounds(300, 500);
558  gfx::Size pending_layer_bounds(400, 800);
559
560  scoped_refptr<FakePicturePileImpl> pending_pile =
561      FakePicturePileImpl::CreateFilledPile(tile_size,
562                                                pending_layer_bounds);
563  scoped_refptr<FakePicturePileImpl> active_pile =
564      FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
565
566  SetupTrees(pending_pile, active_pile);
567  pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
568
569  Region invalidation;
570  AddDefaultTilingsWithInvalidation(invalidation);
571
572  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
573  EXPECT_GT(tilings->num_tilings(), 0u);
574  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
575    const PictureLayerTiling* tiling = tilings->tiling_at(i);
576    gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
577        gfx::Rect(active_layer_bounds),
578        tiling->contents_scale());
579    for (PictureLayerTiling::CoverageIterator iter(
580             tiling, tiling->contents_scale(), tiling->TilingRect());
581         iter;
582         ++iter) {
583      EXPECT_TRUE(*iter);
584      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
585      std::vector<Tile*> active_tiles =
586          active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
587      std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
588      if (iter.geometry_rect().right() >= active_content_bounds.width() ||
589          iter.geometry_rect().bottom() >= active_content_bounds.height() ||
590          active_tiles[0]->content_rect().size() !=
591              pending_tiles[0]->content_rect().size()) {
592        EXPECT_EQ(pending_pile, iter->picture_pile());
593      } else {
594        EXPECT_EQ(active_pile, iter->picture_pile());
595      }
596    }
597  }
598}
599
600TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
601  gfx::Size tile_size(400, 400);
602  gfx::Size layer_bounds(1300, 1900);
603
604  scoped_refptr<FakePicturePileImpl> pending_pile =
605      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
606  scoped_refptr<FakePicturePileImpl> active_pile =
607      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
608
609  // Fill in some of active pile, but more of pending pile.
610  int hole_count = 0;
611  for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
612    for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
613      if ((x + y) % 2) {
614        pending_pile->AddRecordingAt(x, y);
615        active_pile->AddRecordingAt(x, y);
616      } else {
617        hole_count++;
618        if (hole_count % 2)
619          pending_pile->AddRecordingAt(x, y);
620      }
621    }
622  }
623
624  SetupTrees(pending_pile, active_pile);
625  Region invalidation;
626  AddDefaultTilingsWithInvalidation(invalidation);
627
628  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
629  EXPECT_GT(tilings->num_tilings(), 0u);
630  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
631    const PictureLayerTiling* tiling = tilings->tiling_at(i);
632
633    for (PictureLayerTiling::CoverageIterator iter(
634             tiling, tiling->contents_scale(), tiling->TilingRect());
635         iter;
636         ++iter) {
637      EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
638      // Ensure there is a recording for this tile.
639      bool in_pending = pending_pile->CanRaster(tiling->contents_scale(),
640                                                iter.full_tile_geometry_rect());
641      bool in_active = active_pile->CanRaster(tiling->contents_scale(),
642                                              iter.full_tile_geometry_rect());
643
644      if (in_pending && !in_active)
645        EXPECT_EQ(pending_pile, iter->picture_pile());
646      else if (in_active)
647        EXPECT_EQ(active_pile, iter->picture_pile());
648      else
649        EXPECT_FALSE(*iter);
650    }
651  }
652}
653
654TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
655  gfx::Size tile_size(400, 400);
656  gfx::Size layer_bounds(1300, 1900);
657
658  scoped_refptr<FakePicturePileImpl> pending_pile =
659      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
660  scoped_refptr<FakePicturePileImpl> active_pile =
661      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
662
663  float result_scale_x, result_scale_y;
664  gfx::Size result_bounds;
665
666  SetupTrees(pending_pile, active_pile);
667
668  pending_layer_->CalculateContentsScale(1.f,
669                                         1.f,
670                                         1.f,
671                                         1.f,
672                                         false,
673                                         &result_scale_x,
674                                         &result_scale_y,
675                                         &result_bounds);
676
677  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
678}
679
680TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
681  gfx::Size tile_size(400, 400);
682  gfx::Size layer_bounds(1300, 1900);
683
684  scoped_refptr<FakePicturePileImpl> pending_pile =
685      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
686  scoped_refptr<FakePicturePileImpl> active_pile =
687      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
688
689  float result_scale_x, result_scale_y;
690  gfx::Size result_bounds;
691
692  SetupTrees(pending_pile, active_pile);
693  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
694
695  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
696  EXPECT_LT(low_res_factor, 1.f);
697
698  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
699                                         1.7f,  // device scale
700                                         3.2f,  // page scale
701                                         1.f,   // maximum animation scale
702                                         false,
703                                         &result_scale_x,
704                                         &result_scale_y,
705                                         &result_bounds);
706  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
707  EXPECT_FLOAT_EQ(
708      1.3f,
709      pending_layer_->tilings()->tiling_at(0)->contents_scale());
710  EXPECT_FLOAT_EQ(
711      1.3f * low_res_factor,
712      pending_layer_->tilings()->tiling_at(1)->contents_scale());
713
714  // If we change the layer's CSS scale factor, then we should not get new
715  // tilings.
716  pending_layer_->CalculateContentsScale(1.8f,  // ideal contents scale
717                                         1.7f,  // device scale
718                                         3.2f,  // page scale
719                                         1.f,   // maximum animation scale
720                                         false,
721                                         &result_scale_x,
722                                         &result_scale_y,
723                                         &result_bounds);
724  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
725  EXPECT_FLOAT_EQ(
726      1.3f,
727      pending_layer_->tilings()->tiling_at(0)->contents_scale());
728  EXPECT_FLOAT_EQ(
729      1.3f * low_res_factor,
730      pending_layer_->tilings()->tiling_at(1)->contents_scale());
731
732  // If we change the page scale factor, then we should get new tilings.
733  pending_layer_->CalculateContentsScale(1.8f,  // ideal contents scale
734                                         1.7f,  // device scale
735                                         2.2f,  // page scale
736                                         1.f,   // maximum animation scale
737                                         false,
738                                         &result_scale_x,
739                                         &result_scale_y,
740                                         &result_bounds);
741  ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
742  EXPECT_FLOAT_EQ(
743      1.8f,
744      pending_layer_->tilings()->tiling_at(0)->contents_scale());
745  EXPECT_FLOAT_EQ(
746      1.8f * low_res_factor,
747      pending_layer_->tilings()->tiling_at(2)->contents_scale());
748
749  // If we change the device scale factor, then we should get new tilings.
750  pending_layer_->CalculateContentsScale(1.9f,  // ideal contents scale
751                                         1.4f,  // device scale
752                                         2.2f,  // page scale
753                                         1.f,   // maximum animation scale
754                                         false,
755                                         &result_scale_x,
756                                         &result_scale_y,
757                                         &result_bounds);
758  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
759  EXPECT_FLOAT_EQ(
760      1.9f,
761      pending_layer_->tilings()->tiling_at(0)->contents_scale());
762  EXPECT_FLOAT_EQ(
763      1.9f * low_res_factor,
764      pending_layer_->tilings()->tiling_at(3)->contents_scale());
765
766  // If we change the device scale factor, but end up at the same total scale
767  // factor somehow, then we don't get new tilings.
768  pending_layer_->CalculateContentsScale(1.9f,  // ideal contents scale
769                                         2.2f,  // device scale
770                                         1.4f,  // page scale
771                                         1.f,   // maximum animation scale
772                                         false,
773                                         &result_scale_x,
774                                         &result_scale_y,
775                                         &result_bounds);
776  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
777  EXPECT_FLOAT_EQ(
778      1.9f,
779      pending_layer_->tilings()->tiling_at(0)->contents_scale());
780  EXPECT_FLOAT_EQ(
781      1.9f * low_res_factor,
782      pending_layer_->tilings()->tiling_at(3)->contents_scale());
783}
784
785TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
786  // This test makes sure that if a layer can have tilings, then a commit makes
787  // it not able to have tilings (empty size), and then a future commit that
788  // makes it valid again should be able to create tilings.
789  gfx::Size tile_size(400, 400);
790  gfx::Size layer_bounds(1300, 1900);
791
792  scoped_refptr<FakePicturePileImpl> empty_pile =
793      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
794  scoped_refptr<FakePicturePileImpl> valid_pile =
795      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
796
797  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
798  EXPECT_LT(low_res_factor, 1.f);
799
800  float high_res_scale = 1.3f;
801  float low_res_scale = high_res_scale * low_res_factor;
802  float device_scale = 1.7f;
803  float page_scale = 3.2f;
804  float maximum_animation_scale = 1.f;
805  float result_scale_x, result_scale_y;
806  gfx::Size result_bounds;
807
808  SetupPendingTree(valid_pile);
809  pending_layer_->CalculateContentsScale(high_res_scale,
810                                         device_scale,
811                                         page_scale,
812                                         maximum_animation_scale,
813                                         false,
814                                         &result_scale_x,
815                                         &result_scale_y,
816                                         &result_bounds);
817  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
818  EXPECT_FLOAT_EQ(high_res_scale,
819                  pending_layer_->HighResTiling()->contents_scale());
820  EXPECT_FLOAT_EQ(low_res_scale,
821                  pending_layer_->LowResTiling()->contents_scale());
822
823  ActivateTree();
824  SetupPendingTree(empty_pile);
825  pending_layer_->CalculateContentsScale(high_res_scale,
826                                         device_scale,
827                                         page_scale,
828                                         maximum_animation_scale,
829                                         false,
830                                         &result_scale_x,
831                                         &result_scale_y,
832                                         &result_bounds);
833  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
834  ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
835
836  ActivateTree();
837  active_layer_->CalculateContentsScale(high_res_scale,
838                                        device_scale,
839                                        page_scale,
840                                        maximum_animation_scale,
841                                        false,
842                                        &result_scale_x,
843                                        &result_scale_y,
844                                        &result_bounds);
845  ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
846
847  SetupPendingTree(valid_pile);
848  pending_layer_->CalculateContentsScale(high_res_scale,
849                                         device_scale,
850                                         page_scale,
851                                         maximum_animation_scale,
852                                         false,
853                                         &result_scale_x,
854                                         &result_scale_y,
855                                         &result_bounds);
856  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
857  ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
858  EXPECT_FLOAT_EQ(high_res_scale,
859                  pending_layer_->HighResTiling()->contents_scale());
860  EXPECT_FLOAT_EQ(low_res_scale,
861                  pending_layer_->LowResTiling()->contents_scale());
862}
863
864TEST_F(PictureLayerImplTest, ZoomOutCrash) {
865  gfx::Size tile_size(400, 400);
866  gfx::Size layer_bounds(1300, 1900);
867
868  // Set up the high and low res tilings before pinch zoom.
869  scoped_refptr<FakePicturePileImpl> pending_pile =
870      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
871  scoped_refptr<FakePicturePileImpl> active_pile =
872      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
873
874  SetupTrees(pending_pile, active_pile);
875  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
876  SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, false);
877  host_impl_.PinchGestureBegin();
878  SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
879  SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
880  EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
881}
882
883TEST_F(PictureLayerImplTest, PinchGestureTilings) {
884  gfx::Size tile_size(400, 400);
885  gfx::Size layer_bounds(1300, 1900);
886
887  scoped_refptr<FakePicturePileImpl> pending_pile =
888      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
889  scoped_refptr<FakePicturePileImpl> active_pile =
890      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
891
892  // Set up the high and low res tilings before pinch zoom.
893  SetupTrees(pending_pile, active_pile);
894  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
895  SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
896  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
897  EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
898  EXPECT_FLOAT_EQ(
899      1.0f,
900      active_layer_->tilings()->tiling_at(0)->contents_scale());
901  EXPECT_FLOAT_EQ(
902      1.0f * low_res_factor,
903      active_layer_->tilings()->tiling_at(1)->contents_scale());
904
905  // Start a pinch gesture.
906  host_impl_.PinchGestureBegin();
907
908  // Zoom out by a small amount. We should create a tiling at half
909  // the scale (1/kMaxScaleRatioDuringPinch).
910  SetContentsScaleOnBothLayers(0.90f, 1.0f, 0.9f, 1.0f, false);
911  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
912  EXPECT_FLOAT_EQ(
913      1.0f,
914      active_layer_->tilings()->tiling_at(0)->contents_scale());
915  EXPECT_FLOAT_EQ(
916      0.5f,
917      active_layer_->tilings()->tiling_at(1)->contents_scale());
918  EXPECT_FLOAT_EQ(
919      1.0f * low_res_factor,
920      active_layer_->tilings()->tiling_at(2)->contents_scale());
921
922  // Zoom out further, close to our low-res scale factor. We should
923  // use that tiling as high-res, and not create a new tiling.
924  SetContentsScaleOnBothLayers(
925      low_res_factor, 1.0f, low_res_factor, 1.0f, false);
926  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
927
928  // Zoom in a lot now. Since we increase by increments of
929  // kMaxScaleRatioDuringPinch, this will first use 0.5, then 1.0
930  // and then finally create a new tiling at 2.0.
931  SetContentsScaleOnBothLayers(2.1f, 1.0f, 2.1f, 1.f, false);
932  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
933  SetContentsScaleOnBothLayers(2.1f, 1.0f, 2.1f, 1.f, false);
934  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
935  SetContentsScaleOnBothLayers(2.1f, 1.0f, 2.1f, 1.f, false);
936  EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
937  EXPECT_FLOAT_EQ(
938      2.0f,
939      active_layer_->tilings()->tiling_at(0)->contents_scale());
940}
941
942TEST_F(PictureLayerImplTest, CleanUpTilings) {
943  gfx::Size tile_size(400, 400);
944  gfx::Size layer_bounds(1300, 1900);
945
946  scoped_refptr<FakePicturePileImpl> pending_pile =
947      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
948  scoped_refptr<FakePicturePileImpl> active_pile =
949      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
950
951  float result_scale_x, result_scale_y;
952  gfx::Size result_bounds;
953  std::vector<PictureLayerTiling*> used_tilings;
954
955  SetupTrees(pending_pile, active_pile);
956  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
957
958  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
959  EXPECT_LT(low_res_factor, 1.f);
960
961  float device_scale = 1.7f;
962  float page_scale = 3.2f;
963
964  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
965  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
966
967  // We only have ideal tilings, so they aren't removed.
968  used_tilings.clear();
969  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
970  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
971
972  // Changing the ideal but not creating new tilings.
973  SetContentsScaleOnBothLayers(1.5f, device_scale, page_scale, 1.f, false);
974  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
975
976  // The tilings are still our target scale, so they aren't removed.
977  used_tilings.clear();
978  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
979  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
980
981  // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
982  page_scale = 1.2f;
983  SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
984  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
985  EXPECT_FLOAT_EQ(
986      1.f,
987      active_layer_->tilings()->tiling_at(1)->contents_scale());
988  EXPECT_FLOAT_EQ(
989      1.f * low_res_factor,
990      active_layer_->tilings()->tiling_at(3)->contents_scale());
991
992  // Mark the non-ideal tilings as used. They won't be removed.
993  used_tilings.clear();
994  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
995  used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
996  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
997  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
998
999  // Now move the ideal scale to 0.5. Our target stays 1.2.
1000  SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
1001
1002  // The high resolution tiling is between target and ideal, so is not
1003  // removed.  The low res tiling for the old ideal=1.0 scale is removed.
1004  used_tilings.clear();
1005  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1006  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1007
1008  // Now move the ideal scale to 1.0. Our target stays 1.2.
1009  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
1010
1011  // All the tilings are between are target and the ideal, so they are not
1012  // removed.
1013  used_tilings.clear();
1014  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1015  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1016
1017  // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
1018  active_layer_->CalculateContentsScale(1.1f,
1019                                        device_scale,
1020                                        page_scale,
1021                                        1.f,
1022                                        false,
1023                                        &result_scale_x,
1024                                        &result_scale_y,
1025                                        &result_bounds);
1026
1027  // Because the pending layer's ideal scale is still 1.0, our tilings fall
1028  // in the range [1.0,1.2] and are kept.
1029  used_tilings.clear();
1030  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1031  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1032
1033  // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
1034  // 1.2 still.
1035  pending_layer_->CalculateContentsScale(1.1f,
1036                                         device_scale,
1037                                         page_scale,
1038                                         1.f,
1039                                         false,
1040                                         &result_scale_x,
1041                                         &result_scale_y,
1042                                         &result_bounds);
1043
1044  // Our 1.0 tiling now falls outside the range between our ideal scale and our
1045  // target raster scale. But it is in our used tilings set, so nothing is
1046  // deleted.
1047  used_tilings.clear();
1048  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1049  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1050  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1051
1052  // If we remove it from our used tilings set, it is outside the range to keep
1053  // so it is deleted.
1054  used_tilings.clear();
1055  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1056  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1057}
1058
1059#define EXPECT_BOTH_EQ(expression, x)         \
1060  do {                                        \
1061    EXPECT_EQ(pending_layer_->expression, x); \
1062    EXPECT_EQ(active_layer_->expression, x);  \
1063  } while (false)
1064
1065TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
1066  // Make sure this layer covers multiple tiles, since otherwise low
1067  // res won't get created because it is too small.
1068  gfx::Size tile_size(host_impl_.settings().default_tile_size);
1069  SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
1070  // Avoid max untiled layer size heuristics via fixed tile size.
1071  pending_layer_->set_fixed_tile_size(tile_size);
1072  active_layer_->set_fixed_tile_size(tile_size);
1073
1074  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1075  float contents_scale = 1.f;
1076  float device_scale = 1.f;
1077  float page_scale = 1.f;
1078  float maximum_animation_scale = 1.f;
1079  bool animating_transform = true;
1080
1081  // Animating, so don't create low res even if there isn't one already.
1082  SetContentsScaleOnBothLayers(contents_scale,
1083                               device_scale,
1084                               page_scale,
1085                               maximum_animation_scale,
1086                               animating_transform);
1087  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1088  EXPECT_BOTH_EQ(num_tilings(), 1u);
1089
1090  // Stop animating, low res gets created.
1091  animating_transform = false;
1092  SetContentsScaleOnBothLayers(contents_scale,
1093                               device_scale,
1094                               page_scale,
1095                               maximum_animation_scale,
1096                               animating_transform);
1097  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1098  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1099  EXPECT_BOTH_EQ(num_tilings(), 2u);
1100
1101  // Page scale animation, new high res, but not new low res because animating.
1102  contents_scale = 2.f;
1103  page_scale = 2.f;
1104  animating_transform = true;
1105  SetContentsScaleOnBothLayers(contents_scale,
1106                               device_scale,
1107                               page_scale,
1108                               maximum_animation_scale,
1109                               animating_transform);
1110  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1111  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1112  EXPECT_BOTH_EQ(num_tilings(), 3u);
1113
1114  // Stop animating, new low res gets created for final page scale.
1115  animating_transform = false;
1116  SetContentsScaleOnBothLayers(contents_scale,
1117                               device_scale,
1118                               page_scale,
1119                               maximum_animation_scale,
1120                               animating_transform);
1121  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1122  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1123  EXPECT_BOTH_EQ(num_tilings(), 4u);
1124}
1125
1126TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1127  gfx::Size tile_size(host_impl_.settings().default_tile_size);
1128  SetupDefaultTrees(tile_size);
1129
1130  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1131  float device_scale = 1.f;
1132  float page_scale = 1.f;
1133  float maximum_animation_scale = 1.f;
1134  bool animating_transform = false;
1135
1136  // Contents exactly fit on one tile at scale 1, no low res.
1137  float contents_scale = 1.f;
1138  SetContentsScaleOnBothLayers(contents_scale,
1139                               device_scale,
1140                               page_scale,
1141                               maximum_animation_scale,
1142                               animating_transform);
1143  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1144  EXPECT_BOTH_EQ(num_tilings(), 1u);
1145
1146  ResetTilingsAndRasterScales();
1147
1148  // Contents that are smaller than one tile, no low res.
1149  contents_scale = 0.123f;
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  // Any content bounds that would create more than one tile will
1161  // generate a low res tiling.
1162  contents_scale = 2.5f;
1163  SetContentsScaleOnBothLayers(contents_scale,
1164                               device_scale,
1165                               page_scale,
1166                               maximum_animation_scale,
1167                               animating_transform);
1168  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1169  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1170                 contents_scale * low_res_factor);
1171  EXPECT_BOTH_EQ(num_tilings(), 2u);
1172
1173  ResetTilingsAndRasterScales();
1174
1175  // Mask layers dont create low res since they always fit on one tile.
1176  pending_layer_->SetIsMask(true);
1177  active_layer_->SetIsMask(true);
1178  SetContentsScaleOnBothLayers(contents_scale,
1179                               device_scale,
1180                               page_scale,
1181                               maximum_animation_scale,
1182                               animating_transform);
1183  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1184  EXPECT_BOTH_EQ(num_tilings(), 1u);
1185}
1186
1187TEST_F(PictureLayerImplTest, ReleaseResources) {
1188  gfx::Size tile_size(400, 400);
1189  gfx::Size layer_bounds(1300, 1900);
1190
1191  scoped_refptr<FakePicturePileImpl> pending_pile =
1192      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1193  scoped_refptr<FakePicturePileImpl> active_pile =
1194      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1195
1196  float result_scale_x, result_scale_y;
1197  gfx::Size result_bounds;
1198
1199  SetupTrees(pending_pile, active_pile);
1200  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1201
1202  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
1203                                         2.7f,  // device scale
1204                                         3.2f,  // page scale
1205                                         1.f,   // maximum animation scale
1206                                         false,
1207                                         &result_scale_x,
1208                                         &result_scale_y,
1209                                         &result_bounds);
1210  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1211
1212  // All tilings should be removed when losing output surface.
1213  active_layer_->ReleaseResources();
1214  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1215  pending_layer_->ReleaseResources();
1216  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1217
1218  // This should create new tilings.
1219  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
1220                                         2.7f,  // device scale
1221                                         3.2f,  // page scale
1222                                         1.f,   // maximum animation scale
1223                                         false,
1224                                         &result_scale_x,
1225                                         &result_scale_y,
1226                                         &result_bounds);
1227  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1228}
1229
1230TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1231  // The default max tile size is larger than 400x400.
1232  gfx::Size tile_size(400, 400);
1233  gfx::Size layer_bounds(5000, 5000);
1234
1235  scoped_refptr<FakePicturePileImpl> pending_pile =
1236      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1237  scoped_refptr<FakePicturePileImpl> active_pile =
1238      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1239
1240  float result_scale_x, result_scale_y;
1241  gfx::Size result_bounds;
1242
1243  SetupTrees(pending_pile, active_pile);
1244  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1245
1246  pending_layer_->CalculateContentsScale(1.f,
1247                                         1.f,
1248                                         1.f,
1249                                         1.f,
1250                                         false,
1251                                         &result_scale_x,
1252                                         &result_scale_y,
1253                                         &result_bounds);
1254  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1255
1256  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1257
1258  // The default value.
1259  EXPECT_EQ(gfx::Size(256, 256).ToString(),
1260            host_impl_.settings().default_tile_size.ToString());
1261
1262  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1263  EXPECT_EQ(gfx::Size(256, 256).ToString(),
1264            tile->content_rect().size().ToString());
1265
1266  pending_layer_->ReleaseResources();
1267
1268  // Change the max texture size on the output surface context.
1269  scoped_ptr<TestWebGraphicsContext3D> context =
1270      TestWebGraphicsContext3D::Create();
1271  context->set_max_texture_size(140);
1272  host_impl_.DidLoseOutputSurface();
1273  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1274      context.Pass()).PassAs<OutputSurface>());
1275
1276  pending_layer_->CalculateContentsScale(1.f,
1277                                         1.f,
1278                                         1.f,
1279                                         1.f,
1280                                         false,
1281                                         &result_scale_x,
1282                                         &result_scale_y,
1283                                         &result_bounds);
1284  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1285
1286  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1287
1288  // Verify the tiles are not larger than the context's max texture size.
1289  tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1290  EXPECT_GE(140, tile->content_rect().width());
1291  EXPECT_GE(140, tile->content_rect().height());
1292}
1293
1294TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1295  // The default max tile size is larger than 400x400.
1296  gfx::Size tile_size(400, 400);
1297  gfx::Size layer_bounds(500, 500);
1298
1299  scoped_refptr<FakePicturePileImpl> pending_pile =
1300      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1301  scoped_refptr<FakePicturePileImpl> active_pile =
1302      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1303
1304  float result_scale_x, result_scale_y;
1305  gfx::Size result_bounds;
1306
1307  SetupTrees(pending_pile, active_pile);
1308  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1309
1310  pending_layer_->CalculateContentsScale(1.f,
1311                                         1.f,
1312                                         1.f,
1313                                         1.f,
1314                                         false,
1315                                         &result_scale_x,
1316                                         &result_scale_y,
1317                                         &result_bounds);
1318  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1319
1320  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1321
1322  // The default value. The layer is smaller than this.
1323  EXPECT_EQ(gfx::Size(512, 512).ToString(),
1324            host_impl_.settings().max_untiled_layer_size.ToString());
1325
1326  // There should be a single tile since the layer is small.
1327  PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1328  EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1329
1330  pending_layer_->ReleaseResources();
1331
1332  // Change the max texture size on the output surface context.
1333  scoped_ptr<TestWebGraphicsContext3D> context =
1334      TestWebGraphicsContext3D::Create();
1335  context->set_max_texture_size(140);
1336  host_impl_.DidLoseOutputSurface();
1337  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1338      context.Pass()).PassAs<OutputSurface>());
1339
1340  pending_layer_->CalculateContentsScale(1.f,
1341                                         1.f,
1342                                         1.f,
1343                                         1.f,
1344                                         false,
1345                                         &result_scale_x,
1346                                         &result_scale_y,
1347                                         &result_bounds);
1348  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1349
1350  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1351
1352  // There should be more than one tile since the max texture size won't cover
1353  // the layer.
1354  high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1355  EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1356
1357  // Verify the tiles are not larger than the context's max texture size.
1358  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1359  EXPECT_GE(140, tile->content_rect().width());
1360  EXPECT_GE(140, tile->content_rect().height());
1361}
1362
1363TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1364  MockQuadCuller quad_culler;
1365
1366  gfx::Size tile_size(400, 400);
1367  gfx::Size layer_bounds(1300, 1900);
1368
1369  scoped_refptr<FakePicturePileImpl> pending_pile =
1370      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1371  scoped_refptr<FakePicturePileImpl> active_pile =
1372      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1373
1374  SetupTrees(pending_pile, active_pile);
1375
1376  active_layer_->SetContentBounds(layer_bounds);
1377  active_layer_->draw_properties().visible_content_rect =
1378      gfx::Rect(layer_bounds);
1379
1380  gfx::Rect layer_invalidation(150, 200, 30, 180);
1381  Region invalidation(layer_invalidation);
1382  AddDefaultTilingsWithInvalidation(invalidation);
1383
1384  AppendQuadsData data;
1385  active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
1386  active_layer_->AppendQuads(&quad_culler, &data);
1387  active_layer_->DidDraw(NULL);
1388
1389  ASSERT_EQ(1U, quad_culler.quad_list().size());
1390  EXPECT_EQ(DrawQuad::PICTURE_CONTENT, quad_culler.quad_list()[0]->material);
1391}
1392
1393TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
1394  gfx::Size tile_size(100, 100);
1395  gfx::Size layer_bounds(1000, 1000);
1396
1397  scoped_refptr<FakePicturePileImpl> pending_pile =
1398      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1399  // Layers with entirely empty piles can't get tilings.
1400  pending_pile->AddRecordingAt(0, 0);
1401
1402  SetupPendingTree(pending_pile);
1403
1404  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1405  pending_layer_->AddTiling(1.0f);
1406  pending_layer_->AddTiling(2.0f);
1407
1408  // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1409  // on a layer with no recordings.
1410  host_impl_.pending_tree()->UpdateDrawProperties();
1411  pending_layer_->MarkVisibleResourcesAsRequired();
1412}
1413
1414TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1415  gfx::Size tile_size(100, 100);
1416  gfx::Size layer_bounds(200, 200);
1417
1418  scoped_refptr<FakePicturePileImpl> pending_pile =
1419      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1420  SetupPendingTree(pending_pile);
1421
1422  pending_layer_->set_fixed_tile_size(tile_size);
1423  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1424  PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1425  host_impl_.pending_tree()->UpdateDrawProperties();
1426  EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1427
1428  pending_layer_->draw_properties().visible_content_rect =
1429      gfx::Rect(0, 0, 100, 200);
1430
1431  // Fake set priorities.
1432  for (PictureLayerTiling::CoverageIterator iter(
1433           tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1434       iter;
1435       ++iter) {
1436    if (!*iter)
1437      continue;
1438    Tile* tile = *iter;
1439    TilePriority priority;
1440    priority.resolution = HIGH_RESOLUTION;
1441    gfx::Rect tile_bounds = iter.geometry_rect();
1442    if (pending_layer_->visible_content_rect().Intersects(tile_bounds)) {
1443      priority.priority_bin = TilePriority::NOW;
1444      priority.distance_to_visible = 0.f;
1445    } else {
1446      priority.priority_bin = TilePriority::SOON;
1447      priority.distance_to_visible = 1.f;
1448    }
1449    tile->SetPriority(PENDING_TREE, priority);
1450  }
1451
1452  pending_layer_->MarkVisibleResourcesAsRequired();
1453
1454  int num_visible = 0;
1455  int num_offscreen = 0;
1456
1457  for (PictureLayerTiling::CoverageIterator iter(
1458           tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1459       iter;
1460       ++iter) {
1461    if (!*iter)
1462      continue;
1463    const Tile* tile = *iter;
1464    if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1465      EXPECT_TRUE(tile->required_for_activation());
1466      num_visible++;
1467    } else {
1468      EXPECT_FALSE(tile->required_for_activation());
1469      num_offscreen++;
1470    }
1471  }
1472
1473  EXPECT_GT(num_visible, 0);
1474  EXPECT_GT(num_offscreen, 0);
1475}
1476
1477TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
1478  gfx::Size layer_bounds(400, 400);
1479  gfx::Size tile_size(100, 100);
1480  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1481
1482  // No tiles shared.
1483  pending_layer_->set_invalidation(gfx::Rect(layer_bounds));
1484
1485  CreateHighLowResAndSetAllTilesVisible();
1486
1487  active_layer_->SetAllTilesReady();
1488
1489  // No shared tiles and all active tiles ready, so pending can only
1490  // activate with all high res tiles.
1491  pending_layer_->MarkVisibleResourcesAsRequired();
1492  AssertAllTilesRequired(pending_layer_->HighResTiling());
1493  AssertNoTilesRequired(pending_layer_->LowResTiling());
1494}
1495
1496TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
1497  gfx::Size layer_bounds(400, 400);
1498  gfx::Size tile_size(100, 100);
1499  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1500
1501  // All tiles shared (no invalidation).
1502  CreateHighLowResAndSetAllTilesVisible();
1503
1504  // Verify active tree not ready.
1505  Tile* some_active_tile =
1506      active_layer_->HighResTiling()->AllTilesForTesting()[0];
1507  EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1508
1509  // When high res are required, even if the active tree is not ready,
1510  // the high res tiles must be ready.
1511  host_impl_.active_tree()->SetRequiresHighResToDraw();
1512  pending_layer_->MarkVisibleResourcesAsRequired();
1513  AssertAllTilesRequired(pending_layer_->HighResTiling());
1514  AssertNoTilesRequired(pending_layer_->LowResTiling());
1515}
1516
1517TEST_F(PictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
1518  gfx::Size layer_bounds(400, 400);
1519  gfx::Size tile_size(100, 100);
1520  SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1521
1522  CreateHighLowResAndSetAllTilesVisible();
1523
1524  Tile* some_active_tile =
1525      active_layer_->HighResTiling()->AllTilesForTesting()[0];
1526  EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1527
1528  // All tiles shared (no invalidation), so even though the active tree's
1529  // tiles aren't ready, there is nothing required.
1530  pending_layer_->MarkVisibleResourcesAsRequired();
1531  AssertNoTilesRequired(pending_layer_->HighResTiling());
1532  AssertNoTilesRequired(pending_layer_->LowResTiling());
1533}
1534
1535TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
1536  gfx::Size layer_bounds(400, 400);
1537  gfx::Size tile_size(100, 100);
1538  scoped_refptr<FakePicturePileImpl> pending_pile =
1539      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1540  // This pile will create tilings, but has no recordings so will not create any
1541  // tiles.  This is attempting to simulate scrolling past the end of recorded
1542  // content on the active layer, where the recordings are so far away that
1543  // no tiles are created.
1544  scoped_refptr<FakePicturePileImpl> active_pile =
1545      FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1546          tile_size, layer_bounds);
1547  SetupTrees(pending_pile, active_pile);
1548  pending_layer_->set_fixed_tile_size(tile_size);
1549  active_layer_->set_fixed_tile_size(tile_size);
1550
1551  CreateHighLowResAndSetAllTilesVisible();
1552
1553  // Active layer has tilings, but no tiles due to missing recordings.
1554  EXPECT_TRUE(active_layer_->CanHaveTilings());
1555  EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
1556  EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
1557
1558  // Since the active layer has no tiles at all, the pending layer doesn't
1559  // need content in order to activate.
1560  pending_layer_->MarkVisibleResourcesAsRequired();
1561  AssertNoTilesRequired(pending_layer_->HighResTiling());
1562  AssertNoTilesRequired(pending_layer_->LowResTiling());
1563}
1564
1565TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
1566  gfx::Size layer_bounds(400, 400);
1567  gfx::Size tile_size(100, 100);
1568  scoped_refptr<FakePicturePileImpl> pending_pile =
1569      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1570  scoped_refptr<FakePicturePileImpl> active_pile =
1571      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1572  SetupTrees(pending_pile, active_pile);
1573  pending_layer_->set_fixed_tile_size(tile_size);
1574  active_layer_->set_fixed_tile_size(tile_size);
1575
1576  CreateHighLowResAndSetAllTilesVisible();
1577
1578  // Active layer can't have tiles.
1579  EXPECT_FALSE(active_layer_->CanHaveTilings());
1580
1581  // All high res tiles required.  This should be considered identical
1582  // to the case where there is no active layer, to avoid flashing content.
1583  // This can happen if a layer exists for a while and switches from
1584  // not being able to have content to having content.
1585  pending_layer_->MarkVisibleResourcesAsRequired();
1586  AssertAllTilesRequired(pending_layer_->HighResTiling());
1587  AssertNoTilesRequired(pending_layer_->LowResTiling());
1588}
1589
1590TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1591  gfx::Size tile_size(100, 100);
1592  gfx::Size layer_bounds(400, 400);
1593  scoped_refptr<FakePicturePileImpl> pending_pile =
1594      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1595
1596  host_impl_.CreatePendingTree();
1597  LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1598
1599  scoped_ptr<FakePictureLayerImpl> pending_layer =
1600      FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1601  pending_layer->SetDrawsContent(true);
1602  pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1603
1604  pending_layer_ = static_cast<FakePictureLayerImpl*>(
1605      host_impl_.pending_tree()->LayerById(id_));
1606
1607  // Set some state on the pending layer, make sure it is not clobbered
1608  // by a sync from the active layer.  This could happen because if the
1609  // pending layer has not been post-commit initialized it will attempt
1610  // to sync from the active layer.
1611  bool default_lcd_text_setting = pending_layer_->is_using_lcd_text();
1612  pending_layer_->force_set_lcd_text(!default_lcd_text_setting);
1613  EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1614
1615  host_impl_.ActivatePendingTree();
1616
1617  active_layer_ = static_cast<FakePictureLayerImpl*>(
1618      host_impl_.active_tree()->LayerById(id_));
1619
1620  EXPECT_EQ(0u, active_layer_->num_tilings());
1621  EXPECT_EQ(!default_lcd_text_setting, active_layer_->is_using_lcd_text());
1622  EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1623}
1624
1625TEST_F(PictureLayerImplTest, RemoveInvalidTilesOnActivation) {
1626  SetupDefaultTrees(gfx::Size(1500, 1500));
1627  AddDefaultTilingsWithInvalidation(gfx::Rect(0, 0, 1, 1));
1628
1629  FakePictureLayerImpl* recycled_layer = pending_layer_;
1630  host_impl_.ActivatePendingTree();
1631
1632  active_layer_ = static_cast<FakePictureLayerImpl*>(
1633      host_impl_.active_tree()->LayerById(id_));
1634
1635  EXPECT_EQ(3u, active_layer_->num_tilings());
1636  EXPECT_EQ(3u, recycled_layer->num_tilings());
1637  EXPECT_FALSE(host_impl_.pending_tree());
1638  for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1639    PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1640    PictureLayerTiling* recycled_tiling =
1641        recycled_layer->tilings()->tiling_at(i);
1642
1643    ASSERT_TRUE(active_tiling);
1644    ASSERT_TRUE(recycled_tiling);
1645
1646    EXPECT_TRUE(active_tiling->TileAt(0, 0));
1647    EXPECT_TRUE(active_tiling->TileAt(1, 0));
1648    EXPECT_TRUE(active_tiling->TileAt(0, 1));
1649    EXPECT_TRUE(active_tiling->TileAt(1, 1));
1650
1651    EXPECT_FALSE(recycled_tiling->TileAt(0, 0));
1652    EXPECT_TRUE(recycled_tiling->TileAt(1, 0));
1653    EXPECT_TRUE(recycled_tiling->TileAt(0, 1));
1654    EXPECT_TRUE(recycled_tiling->TileAt(1, 1));
1655
1656    EXPECT_EQ(active_tiling->TileAt(1, 0), recycled_tiling->TileAt(1, 0));
1657    EXPECT_EQ(active_tiling->TileAt(0, 1), recycled_tiling->TileAt(0, 1));
1658    EXPECT_EQ(active_tiling->TileAt(1, 1), recycled_tiling->TileAt(1, 1));
1659  }
1660}
1661
1662TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
1663  SetupDefaultTrees(gfx::Size(10, 10));
1664  host_impl_.active_tree()->UpdateDrawProperties();
1665  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1666
1667  // Contrived unit test of a real crash. A layer is transparent during a
1668  // context loss, and later becomes opaque, causing active layer SyncTiling to
1669  // be called.
1670  float new_scale = 1.f;
1671  active_layer_->ReleaseResources();
1672  pending_layer_->ReleaseResources();
1673  EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(new_scale));
1674  pending_layer_->AddTiling(new_scale);
1675  EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(new_scale));
1676
1677  // UpdateDrawProperties early-outs if the tree doesn't need it.  It is also
1678  // responsible for calling ManageTilings.  These checks verify that
1679  // ReleaseResources has set needs update draw properties so that the
1680  // new tiling gets the appropriate resolution set in ManageTilings.
1681  EXPECT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1682  host_impl_.active_tree()->UpdateDrawProperties();
1683  PictureLayerTiling* high_res =
1684      active_layer_->tilings()->TilingAtScale(new_scale);
1685  ASSERT_TRUE(!!high_res);
1686  EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
1687}
1688
1689TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
1690  gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
1691  gfx::Size layer_bounds(default_tile_size.width() * 4,
1692                         default_tile_size.height() * 4);
1693  float result_scale_x, result_scale_y;
1694  gfx::Size result_bounds;
1695
1696  SetupDefaultTrees(layer_bounds);
1697  EXPECT_FALSE(pending_layer_->ShouldUseGpuRasterization());
1698  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1699  pending_layer_->CalculateContentsScale(1.f,
1700                                         1.f,
1701                                         1.f,
1702                                         1.f,
1703                                         false,
1704                                         &result_scale_x,
1705                                         &result_scale_y,
1706                                         &result_bounds);
1707  // Should have a low-res and a high-res tiling.
1708  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1709
1710  pending_layer_->SetUseGpuRasterization(true);
1711  EXPECT_TRUE(pending_layer_->ShouldUseGpuRasterization());
1712  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1713  pending_layer_->CalculateContentsScale(1.f,
1714                                         1.f,
1715                                         1.f,
1716                                         1.f,
1717                                         false,
1718                                         &result_scale_x,
1719                                         &result_scale_y,
1720                                         &result_bounds);
1721  // Should only have the high-res tiling.
1722  ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
1723}
1724
1725TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
1726  // Set up layers with tilings.
1727  SetupDefaultTrees(gfx::Size(10, 10));
1728  SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
1729  pending_layer_->PushPropertiesTo(active_layer_);
1730  EXPECT_TRUE(pending_layer_->DrawsContent());
1731  EXPECT_TRUE(pending_layer_->CanHaveTilings());
1732  EXPECT_GE(pending_layer_->num_tilings(), 0u);
1733  EXPECT_GE(active_layer_->num_tilings(), 0u);
1734
1735  // Set content to false, which should make CanHaveTilings return false.
1736  pending_layer_->SetDrawsContent(false);
1737  EXPECT_FALSE(pending_layer_->DrawsContent());
1738  EXPECT_FALSE(pending_layer_->CanHaveTilings());
1739
1740  // No tilings should be pushed to active layer.
1741  pending_layer_->PushPropertiesTo(active_layer_);
1742  EXPECT_EQ(0u, active_layer_->num_tilings());
1743}
1744
1745TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
1746  SetupDefaultTrees(gfx::Size(10, 10));
1747  host_impl_.PinchGestureBegin();
1748  float high_res_scale = 2.3f;
1749  SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
1750
1751  ASSERT_GE(pending_layer_->num_tilings(), 0u);
1752  EXPECT_FLOAT_EQ(high_res_scale,
1753                  pending_layer_->HighResTiling()->contents_scale());
1754}
1755
1756TEST_F(PictureLayerImplTest, FirstTilingTooSmall) {
1757  SetupDefaultTrees(gfx::Size(10, 10));
1758  host_impl_.PinchGestureBegin();
1759  float high_res_scale = 0.0001f;
1760  EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale);
1761
1762  SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
1763
1764  ASSERT_GE(pending_layer_->num_tilings(), 0u);
1765  EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
1766                  pending_layer_->HighResTiling()->contents_scale());
1767}
1768
1769TEST_F(PictureLayerImplTest, PinchingTooSmall) {
1770  SetupDefaultTrees(gfx::Size(10, 10));
1771
1772  float contents_scale = 0.15f;
1773  SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
1774
1775  ASSERT_GE(pending_layer_->num_tilings(), 0u);
1776  EXPECT_FLOAT_EQ(contents_scale,
1777                  pending_layer_->HighResTiling()->contents_scale());
1778
1779  host_impl_.PinchGestureBegin();
1780
1781  float page_scale = 0.0001f;
1782  EXPECT_LT(page_scale * contents_scale,
1783            pending_layer_->MinimumContentsScale());
1784
1785  SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, 1.f, false);
1786  ASSERT_GE(pending_layer_->num_tilings(), 0u);
1787  EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
1788                  pending_layer_->HighResTiling()->contents_scale());
1789}
1790
1791class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
1792 public:
1793  virtual void InitializeRenderer() OVERRIDE {
1794    host_impl_.InitializeRenderer(FakeOutputSurface::CreateDeferredGL(
1795        scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice))
1796                                      .PassAs<OutputSurface>());
1797  }
1798
1799  virtual void SetUp() OVERRIDE {
1800    PictureLayerImplTest::SetUp();
1801
1802    // Create some default active and pending trees.
1803    gfx::Size tile_size(100, 100);
1804    gfx::Size layer_bounds(400, 400);
1805
1806    scoped_refptr<FakePicturePileImpl> pending_pile =
1807        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1808    scoped_refptr<FakePicturePileImpl> active_pile =
1809        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1810
1811    SetupTrees(pending_pile, active_pile);
1812  }
1813};
1814
1815// This test is really a LayerTreeHostImpl test, in that it makes sure
1816// that trees need update draw properties after deferred initialization.
1817// However, this is also a regression test for PictureLayerImpl in that
1818// not having this update will cause a crash.
1819TEST_F(DeferredInitPictureLayerImplTest,
1820       PreventUpdateTilePrioritiesDuringLostContext) {
1821  host_impl_.pending_tree()->UpdateDrawProperties();
1822  host_impl_.active_tree()->UpdateDrawProperties();
1823  EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
1824  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1825
1826  FakeOutputSurface* fake_output_surface =
1827      static_cast<FakeOutputSurface*>(host_impl_.output_surface());
1828  ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
1829      TestContextProvider::Create(), NULL));
1830
1831  // These will crash PictureLayerImpl if this is not true.
1832  ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
1833  ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1834  host_impl_.active_tree()->UpdateDrawProperties();
1835}
1836
1837TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
1838  gfx::Size tile_size(host_impl_.settings().default_tile_size);
1839  SetupDefaultTrees(tile_size);
1840
1841  float contents_scale = 1.f;
1842  float device_scale = 1.3f;
1843  float page_scale = 1.4f;
1844  float maximum_animation_scale = 1.f;
1845  bool animating_transform = false;
1846
1847  SetContentsScaleOnBothLayers(contents_scale,
1848                               device_scale,
1849                               page_scale,
1850                               maximum_animation_scale,
1851                               animating_transform);
1852  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1853
1854  // Since we're CPU-rasterizing, starting an animation should cause tiling
1855  // resolution to get set to the maximum animation scale factor.
1856  animating_transform = true;
1857  maximum_animation_scale = 3.f;
1858  contents_scale = 2.f;
1859
1860  SetContentsScaleOnBothLayers(contents_scale,
1861                               device_scale,
1862                               page_scale,
1863                               maximum_animation_scale,
1864                               animating_transform);
1865  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
1866
1867  // Further changes to scale during the animation should not cause a new
1868  // high-res tiling to get created.
1869  contents_scale = 4.f;
1870  maximum_animation_scale = 5.f;
1871
1872  SetContentsScaleOnBothLayers(contents_scale,
1873                               device_scale,
1874                               page_scale,
1875                               maximum_animation_scale,
1876                               animating_transform);
1877  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
1878
1879  // Once we stop animating, a new high-res tiling should be created.
1880  animating_transform = false;
1881
1882  SetContentsScaleOnBothLayers(contents_scale,
1883                               device_scale,
1884                               page_scale,
1885                               maximum_animation_scale,
1886                               animating_transform);
1887  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
1888
1889  // When animating with an unknown maximum animation scale factor, a new
1890  // high-res tiling should be created at the animation's initial scale.
1891  animating_transform = true;
1892  contents_scale = 2.f;
1893  maximum_animation_scale = 0.f;
1894
1895  SetContentsScaleOnBothLayers(contents_scale,
1896                               device_scale,
1897                               page_scale,
1898                               maximum_animation_scale,
1899                               animating_transform);
1900  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1901
1902  // Further changes to scale during the animation should not cause a new
1903  // high-res tiling to get created.
1904  contents_scale = 3.f;
1905
1906  SetContentsScaleOnBothLayers(contents_scale,
1907                               device_scale,
1908                               page_scale,
1909                               maximum_animation_scale,
1910                               animating_transform);
1911  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1912
1913  // Once we stop animating, a new high-res tiling should be created.
1914  animating_transform = false;
1915  contents_scale = 4.f;
1916
1917  SetContentsScaleOnBothLayers(contents_scale,
1918                               device_scale,
1919                               page_scale,
1920                               maximum_animation_scale,
1921                               animating_transform);
1922  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
1923}
1924
1925TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForGpuRasterization) {
1926  gfx::Size tile_size(host_impl_.settings().default_tile_size);
1927  SetupDefaultTrees(tile_size);
1928  pending_layer_->SetUseGpuRasterization(true);
1929  active_layer_->SetUseGpuRasterization(true);
1930
1931  float contents_scale = 1.f;
1932  float device_scale = 1.f;
1933  float page_scale = 1.f;
1934  float maximum_animation_scale = 1.f;
1935  bool animating_transform = false;
1936
1937  SetContentsScaleOnBothLayers(contents_scale,
1938                               device_scale,
1939                               page_scale,
1940                               maximum_animation_scale,
1941                               animating_transform);
1942  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1943
1944  // Changing contents scale during an animation should cause tiling resolution
1945  // to change, since we're GPU-rasterizing. The maximum animation scale should
1946  // not have any effect.
1947  animating_transform = true;
1948  contents_scale = 2.f;
1949  maximum_animation_scale = 4.f;
1950
1951  SetContentsScaleOnBothLayers(contents_scale,
1952                               device_scale,
1953                               page_scale,
1954                               maximum_animation_scale,
1955                               animating_transform);
1956  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1957
1958  // Since we're re-rasterizing during the animation, scales smaller than 1
1959  // should be respected.
1960  contents_scale = 0.5f;
1961  SetContentsScaleOnBothLayers(contents_scale,
1962                               device_scale,
1963                               page_scale,
1964                               maximum_animation_scale,
1965                               animating_transform);
1966  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 0.5f);
1967
1968  // Tiling resolution should also update once we stop animating.
1969  contents_scale = 4.f;
1970  animating_transform = false;
1971  SetContentsScaleOnBothLayers(contents_scale,
1972                               device_scale,
1973                               page_scale,
1974                               maximum_animation_scale,
1975                               animating_transform);
1976  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
1977}
1978
1979TEST_F(PictureLayerImplTest, LayerRasterTileIterator) {
1980  gfx::Size tile_size(100, 100);
1981  gfx::Size layer_bounds(1000, 1000);
1982
1983  scoped_refptr<FakePicturePileImpl> pending_pile =
1984      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1985
1986  SetupPendingTree(pending_pile);
1987
1988  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1989
1990  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1991
1992  // Empty iterator
1993  PictureLayerImpl::LayerRasterTileIterator it;
1994  EXPECT_FALSE(it);
1995
1996  // No tilings.
1997  it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
1998  EXPECT_FALSE(it);
1999
2000  pending_layer_->AddTiling(low_res_factor);
2001  pending_layer_->AddTiling(0.3f);
2002  pending_layer_->AddTiling(0.7f);
2003  PictureLayerTiling* high_res_tiling = pending_layer_->AddTiling(1.0f);
2004  pending_layer_->AddTiling(2.0f);
2005
2006  host_impl_.SetViewportSize(gfx::Size(500, 500));
2007  host_impl_.pending_tree()->UpdateDrawProperties();
2008
2009  std::set<Tile*> unique_tiles;
2010  bool reached_prepaint = false;
2011  size_t non_ideal_tile_count = 0u;
2012  size_t low_res_tile_count = 0u;
2013  size_t high_res_tile_count = 0u;
2014  for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2015       it;
2016       ++it) {
2017    Tile* tile = *it;
2018    TilePriority priority = tile->priority(PENDING_TREE);
2019
2020    EXPECT_TRUE(tile);
2021
2022    // Non-high res tiles only get visible tiles. Also, prepaint should only
2023    // come at the end of the iteration.
2024    if (priority.resolution != HIGH_RESOLUTION)
2025      EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2026    else if (reached_prepaint)
2027      EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2028    else
2029      reached_prepaint = priority.priority_bin != TilePriority::NOW;
2030
2031    non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2032    low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2033    high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2034
2035    unique_tiles.insert(tile);
2036  }
2037
2038  EXPECT_TRUE(reached_prepaint);
2039  EXPECT_EQ(0u, non_ideal_tile_count);
2040  EXPECT_EQ(1u, low_res_tile_count);
2041  EXPECT_EQ(16u, high_res_tile_count);
2042  EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2043            unique_tiles.size());
2044
2045  std::vector<Tile*> high_res_tiles = high_res_tiling->AllTilesForTesting();
2046  for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
2047       tile_it != high_res_tiles.end();
2048       ++tile_it) {
2049    Tile* tile = *tile_it;
2050    ManagedTileState::TileVersion& tile_version =
2051        tile->GetTileVersionForTesting(
2052            tile->DetermineRasterModeForTree(ACTIVE_TREE));
2053    tile_version.SetSolidColorForTesting(SK_ColorRED);
2054  }
2055
2056  non_ideal_tile_count = 0;
2057  low_res_tile_count = 0;
2058  high_res_tile_count = 0;
2059  for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2060       it;
2061       ++it) {
2062    Tile* tile = *it;
2063    TilePriority priority = tile->priority(PENDING_TREE);
2064
2065    EXPECT_TRUE(tile);
2066
2067    non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2068    low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2069    high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2070  }
2071
2072  EXPECT_EQ(0u, non_ideal_tile_count);
2073  EXPECT_EQ(1u, low_res_tile_count);
2074  EXPECT_EQ(0u, high_res_tile_count);
2075}
2076
2077TEST_F(PictureLayerImplTest, LayerEvictionTileIterator) {
2078  gfx::Size tile_size(100, 100);
2079  gfx::Size layer_bounds(1000, 1000);
2080
2081  scoped_refptr<FakePicturePileImpl> pending_pile =
2082      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2083
2084  SetupPendingTree(pending_pile);
2085
2086  ASSERT_TRUE(pending_layer_->CanHaveTilings());
2087
2088  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2089
2090  std::vector<PictureLayerTiling*> tilings;
2091  tilings.push_back(pending_layer_->AddTiling(low_res_factor));
2092  tilings.push_back(pending_layer_->AddTiling(0.3f));
2093  tilings.push_back(pending_layer_->AddTiling(0.7f));
2094  tilings.push_back(pending_layer_->AddTiling(1.0f));
2095  tilings.push_back(pending_layer_->AddTiling(2.0f));
2096
2097  host_impl_.SetViewportSize(gfx::Size(500, 500));
2098  host_impl_.pending_tree()->UpdateDrawProperties();
2099
2100  std::vector<Tile*> all_tiles;
2101  for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
2102           tilings.begin();
2103       tiling_iterator != tilings.end();
2104       ++tiling_iterator) {
2105    std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
2106    std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
2107  }
2108
2109  std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
2110
2111  bool mark_required = false;
2112  for (std::vector<Tile*>::iterator it = all_tiles.begin();
2113       it != all_tiles.end();
2114       ++it) {
2115    Tile* tile = *it;
2116    if (mark_required)
2117      tile->MarkRequiredForActivation();
2118    mark_required = !mark_required;
2119  }
2120
2121  // Sanity checks.
2122  EXPECT_EQ(91u, all_tiles.size());
2123  EXPECT_EQ(91u, all_tiles_set.size());
2124
2125  // Empty iterator.
2126  PictureLayerImpl::LayerEvictionTileIterator it;
2127  EXPECT_FALSE(it);
2128
2129  // Tiles don't have resources yet.
2130  it = PictureLayerImpl::LayerEvictionTileIterator(
2131      pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2132  EXPECT_FALSE(it);
2133
2134  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
2135
2136  std::set<Tile*> unique_tiles;
2137  float expected_scales[] = {2.0f, 0.3f, 0.7f, low_res_factor, 1.0f};
2138  size_t scale_index = 0;
2139  bool reached_visible = false;
2140  bool reached_required = false;
2141  Tile* last_tile = NULL;
2142  for (it = PictureLayerImpl::LayerEvictionTileIterator(
2143           pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2144       it;
2145       ++it) {
2146    Tile* tile = *it;
2147    if (!last_tile)
2148      last_tile = tile;
2149
2150    EXPECT_TRUE(tile);
2151
2152    TilePriority priority = tile->priority(PENDING_TREE);
2153
2154    if (priority.priority_bin == TilePriority::NOW) {
2155      reached_visible = true;
2156      last_tile = tile;
2157      break;
2158    }
2159
2160    if (reached_required) {
2161      EXPECT_TRUE(tile->required_for_activation());
2162    } else if (tile->required_for_activation()) {
2163      reached_required = true;
2164      scale_index = 0;
2165    }
2166
2167    while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2168           std::numeric_limits<float>::epsilon()) {
2169      ++scale_index;
2170      ASSERT_LT(scale_index, arraysize(expected_scales));
2171    }
2172
2173    EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2174    unique_tiles.insert(tile);
2175
2176    // If the tile is the same rough bin as last tile (same activation, bin, and
2177    // scale), then distance should be decreasing.
2178    if (tile->required_for_activation() ==
2179            last_tile->required_for_activation() &&
2180        priority.priority_bin ==
2181            last_tile->priority(PENDING_TREE).priority_bin &&
2182        std::abs(tile->contents_scale() - last_tile->contents_scale()) <
2183            std::numeric_limits<float>::epsilon()) {
2184      EXPECT_LE(priority.distance_to_visible,
2185                last_tile->priority(PENDING_TREE).distance_to_visible);
2186    }
2187
2188    last_tile = tile;
2189  }
2190
2191  EXPECT_TRUE(reached_visible);
2192  EXPECT_TRUE(reached_required);
2193  EXPECT_EQ(65u, unique_tiles.size());
2194
2195  scale_index = 0;
2196  reached_required = false;
2197  for (; it; ++it) {
2198    Tile* tile = *it;
2199    EXPECT_TRUE(tile);
2200
2201    TilePriority priority = tile->priority(PENDING_TREE);
2202    EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2203
2204    if (reached_required) {
2205      EXPECT_TRUE(tile->required_for_activation());
2206    } else if (tile->required_for_activation()) {
2207      reached_required = true;
2208      scale_index = 0;
2209    }
2210
2211    while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2212           std::numeric_limits<float>::epsilon()) {
2213      ++scale_index;
2214      ASSERT_LT(scale_index, arraysize(expected_scales));
2215    }
2216
2217    EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2218    unique_tiles.insert(tile);
2219  }
2220
2221  EXPECT_TRUE(reached_required);
2222  EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
2223}
2224
2225TEST_F(PictureLayerImplTest, Occlusion) {
2226  gfx::Size tile_size(102, 102);
2227  gfx::Size layer_bounds(1000, 1000);
2228  gfx::Size viewport_size(1000, 1000);
2229
2230  LayerTestCommon::LayerImplTest impl;
2231
2232  scoped_refptr<FakePicturePileImpl> pending_pile =
2233      FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
2234  SetupPendingTree(pending_pile);
2235  pending_layer_->SetBounds(layer_bounds);
2236  ActivateTree();
2237  active_layer_->set_fixed_tile_size(tile_size);
2238
2239  host_impl_.SetViewportSize(viewport_size);
2240  host_impl_.active_tree()->UpdateDrawProperties();
2241
2242  std::vector<Tile*> tiles =
2243      active_layer_->HighResTiling()->AllTilesForTesting();
2244  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
2245
2246  {
2247    SCOPED_TRACE("No occlusion");
2248    gfx::Rect occluded;
2249    impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2250
2251    LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
2252                                                 gfx::Rect(layer_bounds));
2253    EXPECT_EQ(100u, impl.quad_list().size());
2254  }
2255
2256  {
2257    SCOPED_TRACE("Full occlusion");
2258    gfx::Rect occluded(active_layer_->visible_content_rect());
2259    impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2260
2261    LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
2262    EXPECT_EQ(impl.quad_list().size(), 0u);
2263  }
2264
2265  {
2266    SCOPED_TRACE("Partial occlusion");
2267    gfx::Rect occluded(150, 0, 200, 1000);
2268    impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2269
2270    size_t partially_occluded_count = 0;
2271    LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
2272        impl.quad_list(),
2273        gfx::Rect(layer_bounds),
2274        occluded,
2275        &partially_occluded_count);
2276    // The layer outputs one quad, which is partially occluded.
2277    EXPECT_EQ(100u - 10u, impl.quad_list().size());
2278    EXPECT_EQ(10u + 10u, partially_occluded_count);
2279  }
2280}
2281
2282}  // namespace
2283}  // namespace cc
2284