picture_layer_impl_unittest.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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 <utility>
8
9#include "cc/debug/test_web_graphics_context_3d.h"
10#include "cc/layers/append_quads_data.h"
11#include "cc/layers/picture_layer.h"
12#include "cc/test/fake_content_layer_client.h"
13#include "cc/test/fake_impl_proxy.h"
14#include "cc/test/fake_layer_tree_host_impl.h"
15#include "cc/test/fake_output_surface.h"
16#include "cc/test/fake_picture_layer_impl.h"
17#include "cc/test/fake_picture_pile_impl.h"
18#include "cc/test/geometry_test_utils.h"
19#include "cc/test/impl_side_painting_settings.h"
20#include "cc/test/mock_quad_culler.h"
21#include "cc/trees/layer_tree_impl.h"
22#include "testing/gtest/include/gtest/gtest.h"
23#include "third_party/skia/include/core/SkBitmapDevice.h"
24#include "ui/gfx/rect_conversions.h"
25
26namespace cc {
27namespace {
28
29class MockCanvas : public SkCanvas {
30 public:
31  explicit MockCanvas(SkBaseDevice* device) : SkCanvas(device) {}
32
33  virtual void drawRect(const SkRect& rect, const SkPaint& paint) OVERRIDE {
34    // Capture calls before SkCanvas quickReject() kicks in.
35    rects_.push_back(rect);
36  }
37
38  std::vector<SkRect> rects_;
39};
40
41class PictureLayerImplTest : public testing::Test {
42 public:
43  PictureLayerImplTest()
44      : host_impl_(ImplSidePaintingSettings(), &proxy_),
45        id_(7) {
46    host_impl_.InitializeRenderer(CreateFakeOutputSurface());
47  }
48
49  virtual ~PictureLayerImplTest() {
50  }
51
52  void SetupDefaultTrees(gfx::Size layer_bounds) {
53    gfx::Size tile_size(100, 100);
54
55    scoped_refptr<FakePicturePileImpl> pending_pile =
56        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
57    scoped_refptr<FakePicturePileImpl> active_pile =
58        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
59
60    SetupTrees(pending_pile, active_pile);
61  }
62
63  void SetupTrees(
64      scoped_refptr<PicturePileImpl> pending_pile,
65      scoped_refptr<PicturePileImpl> active_pile) {
66    SetupPendingTree(active_pile);
67    host_impl_.ActivatePendingTree();
68
69    active_layer_ = static_cast<FakePictureLayerImpl*>(
70        host_impl_.active_tree()->LayerById(id_));
71
72    SetupPendingTree(pending_pile);
73  }
74
75  void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
76    active_layer_->AddTiling(2.3f);
77    active_layer_->AddTiling(1.0f);
78    active_layer_->AddTiling(0.5f);
79    for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
80      active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
81    pending_layer_->set_invalidation(invalidation);
82    for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
83      pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
84  }
85
86  void SetupPendingTree(
87      scoped_refptr<PicturePileImpl> pile) {
88    host_impl_.CreatePendingTree();
89    LayerTreeImpl* pending_tree = host_impl_.pending_tree();
90    // Clear recycled tree.
91    pending_tree->DetachLayerTree();
92
93    scoped_ptr<FakePictureLayerImpl> pending_layer =
94        FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
95    pending_layer->SetDrawsContent(true);
96    pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
97
98    pending_layer_ = static_cast<FakePictureLayerImpl*>(
99        host_impl_.pending_tree()->LayerById(id_));
100    pending_layer_->DoPostCommitInitializationIfNeeded();
101  }
102
103  static void VerifyAllTilesExistAndHavePile(
104      const PictureLayerTiling* tiling,
105      PicturePileImpl* pile) {
106    for (PictureLayerTiling::CoverageIterator
107             iter(tiling, tiling->contents_scale(), tiling->ContentRect());
108         iter;
109         ++iter) {
110      EXPECT_TRUE(*iter);
111      EXPECT_EQ(pile, iter->picture_pile());
112    }
113  }
114
115  void SetContentsScaleOnBothLayers(float contents_scale,
116                                    float device_scale_factor,
117                                    float page_scale_factor,
118                                    bool animating_transform) {
119    float result_scale_x, result_scale_y;
120    gfx::Size result_bounds;
121    pending_layer_->CalculateContentsScale(
122        contents_scale,
123        device_scale_factor,
124        page_scale_factor,
125        animating_transform,
126        &result_scale_x,
127        &result_scale_y,
128        &result_bounds);
129    active_layer_->CalculateContentsScale(
130        contents_scale,
131        device_scale_factor,
132        page_scale_factor,
133        animating_transform,
134        &result_scale_x,
135        &result_scale_y,
136        &result_bounds);
137  }
138
139  void ResetTilingsAndRasterScales() {
140    pending_layer_->DidLoseOutputSurface();
141    active_layer_->DidLoseOutputSurface();
142  }
143
144 protected:
145  void TestTileGridAlignmentCommon() {
146    // Layer to span 4 raster tiles in x and in y
147    ImplSidePaintingSettings settings;
148    gfx::Size layer_size(
149        settings.default_tile_size.width() * 7 / 2,
150        settings.default_tile_size.height() * 7 / 2);
151
152    scoped_refptr<FakePicturePileImpl> pending_pile =
153        FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
154    scoped_refptr<FakePicturePileImpl> active_pile =
155        FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
156
157    SetupTrees(pending_pile, active_pile);
158
159    float result_scale_x, result_scale_y;
160    gfx::Size result_bounds;
161    active_layer_->CalculateContentsScale(
162        1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
163
164    // Add 1x1 rects at the centers of each tile, then re-record pile contents
165    active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
166    std::vector<Tile*> tiles =
167        active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
168    EXPECT_EQ(16u, tiles.size());
169    std::vector<SkRect> rects;
170    std::vector<Tile*>::const_iterator tile_iter;
171    for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
172      gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
173      gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
174      active_pile->add_draw_rect(rect);
175      rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
176    }
177    // Force re-record with newly injected content
178    active_pile->RemoveRecordingAt(0, 0);
179    active_pile->AddRecordingAt(0, 0);
180
181    SkBitmap store;
182    store.setConfig(SkBitmap::kNo_Config, 1000, 1000);
183    SkBitmapDevice device(store);
184
185    std::vector<SkRect>::const_iterator rect_iter = rects.begin();
186    for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
187      MockCanvas mock_canvas(&device);
188      active_pile->RasterDirect(
189          &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
190
191      // This test verifies that when drawing the contents of a specific tile
192      // at content scale 1.0, the playback canvas never receives content from
193      // neighboring tiles which indicates that the tile grid embedded in
194      // SkPicture is perfectly aligned with the compositor's tiles.
195      EXPECT_EQ(1u, mock_canvas.rects_.size());
196      EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]);
197      rect_iter++;
198    }
199  }
200
201  FakeImplProxy proxy_;
202  FakeLayerTreeHostImpl host_impl_;
203  int id_;
204  FakePictureLayerImpl* pending_layer_;
205  FakePictureLayerImpl* active_layer_;
206
207 private:
208  DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
209};
210
211TEST_F(PictureLayerImplTest, TileGridAlignment) {
212  host_impl_.SetDeviceScaleFactor(1.f);
213  TestTileGridAlignmentCommon();
214}
215
216TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
217  host_impl_.SetDeviceScaleFactor(2.f);
218  TestTileGridAlignmentCommon();
219}
220
221TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
222  gfx::Size tile_size(100, 100);
223  gfx::Size layer_bounds(400, 400);
224
225  scoped_refptr<FakePicturePileImpl> pending_pile =
226      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
227  scoped_refptr<FakePicturePileImpl> active_pile =
228      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
229
230  SetupTrees(pending_pile, active_pile);
231
232  Region invalidation;
233  AddDefaultTilingsWithInvalidation(invalidation);
234
235  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
236            active_layer_->tilings()->num_tilings());
237
238  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
239  EXPECT_GT(tilings->num_tilings(), 0u);
240  for (size_t i = 0; i < tilings->num_tilings(); ++i)
241    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
242}
243
244TEST_F(PictureLayerImplTest, SuppressUpdateTilePriorities) {
245  base::TimeTicks time_ticks;
246  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
247
248  gfx::Size tile_size(100, 100);
249  gfx::Size layer_bounds(400, 400);
250
251  scoped_refptr<FakePicturePileImpl> pending_pile =
252      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
253  scoped_refptr<FakePicturePileImpl> active_pile =
254      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
255
256  SetupTrees(pending_pile, active_pile);
257
258  Region invalidation;
259  AddDefaultTilingsWithInvalidation(invalidation);
260  EXPECT_TRUE(host_impl_.manage_tiles_needed());
261  active_layer_->UpdateTilePriorities();
262  host_impl_.ManageTiles();
263  EXPECT_FALSE(host_impl_.manage_tiles_needed());
264
265  time_ticks += base::TimeDelta::FromMilliseconds(200);
266  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
267
268  // Setting this boolean should cause an early out in UpdateTilePriorities.
269  bool valid_for_tile_management = false;
270  host_impl_.SetExternalDrawConstraints(gfx::Transform(),
271                                        gfx::Rect(layer_bounds),
272                                        gfx::Rect(layer_bounds),
273                                        valid_for_tile_management);
274  active_layer_->UpdateTilePriorities();
275  EXPECT_FALSE(host_impl_.manage_tiles_needed());
276
277  time_ticks += base::TimeDelta::FromMilliseconds(200);
278  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
279
280  valid_for_tile_management = true;
281  host_impl_.SetExternalDrawConstraints(gfx::Transform(),
282                                        gfx::Rect(layer_bounds),
283                                        gfx::Rect(layer_bounds),
284                                        valid_for_tile_management);
285  active_layer_->UpdateTilePriorities();
286  EXPECT_TRUE(host_impl_.manage_tiles_needed());
287}
288
289TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
290  gfx::Size tile_size(100, 100);
291  gfx::Size layer_bounds(400, 400);
292  gfx::Rect layer_invalidation(150, 200, 30, 180);
293
294  scoped_refptr<FakePicturePileImpl> pending_pile =
295      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
296  scoped_refptr<FakePicturePileImpl> active_pile =
297      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
298
299  SetupTrees(pending_pile, active_pile);
300
301  Region invalidation(layer_invalidation);
302  AddDefaultTilingsWithInvalidation(invalidation);
303
304  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
305  EXPECT_GT(tilings->num_tilings(), 0u);
306  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
307    const PictureLayerTiling* tiling = tilings->tiling_at(i);
308    gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
309        layer_invalidation,
310        tiling->contents_scale());
311    for (PictureLayerTiling::CoverageIterator
312             iter(tiling,
313                  tiling->contents_scale(),
314                  tiling->ContentRect());
315         iter;
316         ++iter) {
317      EXPECT_TRUE(*iter);
318      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
319      if (iter.geometry_rect().Intersects(content_invalidation))
320        EXPECT_EQ(pending_pile, iter->picture_pile());
321      else
322        EXPECT_EQ(active_pile, iter->picture_pile());
323    }
324  }
325}
326
327TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
328  gfx::Size tile_size(90, 80);
329  gfx::Size layer_bounds(300, 500);
330
331  scoped_refptr<FakePicturePileImpl> pending_pile =
332      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
333  scoped_refptr<FakePicturePileImpl> active_pile =
334      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
335
336  SetupTrees(pending_pile, active_pile);
337
338  Region invalidation((gfx::Rect(layer_bounds)));
339  AddDefaultTilingsWithInvalidation(invalidation);
340
341  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
342            active_layer_->tilings()->num_tilings());
343
344  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
345  EXPECT_GT(tilings->num_tilings(), 0u);
346  for (size_t i = 0; i < tilings->num_tilings(); ++i)
347    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
348}
349
350TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
351  gfx::Size tile_size(90, 80);
352  gfx::Size active_layer_bounds(300, 500);
353  gfx::Size pending_layer_bounds(400, 800);
354
355  scoped_refptr<FakePicturePileImpl> pending_pile =
356      FakePicturePileImpl::CreateFilledPile(tile_size,
357                                                pending_layer_bounds);
358  scoped_refptr<FakePicturePileImpl> active_pile =
359      FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
360
361  SetupTrees(pending_pile, active_pile);
362  pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
363
364  Region invalidation;
365  AddDefaultTilingsWithInvalidation(invalidation);
366
367  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
368  EXPECT_GT(tilings->num_tilings(), 0u);
369  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
370    const PictureLayerTiling* tiling = tilings->tiling_at(i);
371    gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
372        gfx::Rect(active_layer_bounds),
373        tiling->contents_scale());
374    for (PictureLayerTiling::CoverageIterator
375             iter(tiling,
376                  tiling->contents_scale(),
377                  tiling->ContentRect());
378         iter;
379         ++iter) {
380      EXPECT_TRUE(*iter);
381      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
382      std::vector<Tile*> active_tiles =
383          active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
384      std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
385      if (iter.geometry_rect().right() >= active_content_bounds.width() ||
386          iter.geometry_rect().bottom() >= active_content_bounds.height() ||
387          active_tiles[0]->content_rect().size() !=
388              pending_tiles[0]->content_rect().size()) {
389        EXPECT_EQ(pending_pile, iter->picture_pile());
390      } else {
391        EXPECT_EQ(active_pile, iter->picture_pile());
392      }
393    }
394  }
395}
396
397TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
398  gfx::Size tile_size(400, 400);
399  gfx::Size layer_bounds(1300, 1900);
400
401  scoped_refptr<FakePicturePileImpl> pending_pile =
402      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
403  scoped_refptr<FakePicturePileImpl> active_pile =
404      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
405
406  // Fill in some of active pile, but more of pending pile.
407  int hole_count = 0;
408  for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
409    for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
410      if ((x + y) % 2) {
411        pending_pile->AddRecordingAt(x, y);
412        active_pile->AddRecordingAt(x, y);
413      } else {
414        hole_count++;
415        if (hole_count % 2)
416          pending_pile->AddRecordingAt(x, y);
417      }
418    }
419  }
420
421  SetupTrees(pending_pile, active_pile);
422  Region invalidation;
423  AddDefaultTilingsWithInvalidation(invalidation);
424
425  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
426  EXPECT_GT(tilings->num_tilings(), 0u);
427  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
428    const PictureLayerTiling* tiling = tilings->tiling_at(i);
429
430    for (PictureLayerTiling::CoverageIterator
431             iter(tiling,
432                  tiling->contents_scale(),
433                  tiling->ContentRect());
434         iter;
435         ++iter) {
436      EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
437      // Ensure there is a recording for this tile.
438      gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
439          iter.full_tile_geometry_rect(), 1.f / tiling->contents_scale());
440      layer_rect.Intersect(gfx::Rect(layer_bounds));
441
442      bool in_pending = pending_pile->recorded_region().Contains(layer_rect);
443      bool in_active = active_pile->recorded_region().Contains(layer_rect);
444
445      if (in_pending && !in_active)
446        EXPECT_EQ(pending_pile, iter->picture_pile());
447      else if (in_active)
448        EXPECT_EQ(active_pile, iter->picture_pile());
449      else
450        EXPECT_FALSE(*iter);
451    }
452  }
453}
454
455TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
456  gfx::Size tile_size(400, 400);
457  gfx::Size layer_bounds(1300, 1900);
458
459  scoped_refptr<FakePicturePileImpl> pending_pile =
460      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
461  scoped_refptr<FakePicturePileImpl> active_pile =
462      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
463
464  float result_scale_x, result_scale_y;
465  gfx::Size result_bounds;
466
467  SetupTrees(pending_pile, active_pile);
468
469  pending_layer_->CalculateContentsScale(
470      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
471
472  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
473}
474
475TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
476  gfx::Size tile_size(400, 400);
477  gfx::Size layer_bounds(1300, 1900);
478
479  scoped_refptr<FakePicturePileImpl> pending_pile =
480      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
481  scoped_refptr<FakePicturePileImpl> active_pile =
482      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
483
484  float result_scale_x, result_scale_y;
485  gfx::Size result_bounds;
486
487  SetupTrees(pending_pile, active_pile);
488  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
489
490  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
491  EXPECT_LT(low_res_factor, 1.f);
492
493  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
494                                         1.7f,  // device scale
495                                         3.2f,  // page cale
496                                         false,
497                                         &result_scale_x,
498                                         &result_scale_y,
499                                         &result_bounds);
500  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
501  EXPECT_FLOAT_EQ(
502      1.3f,
503      pending_layer_->tilings()->tiling_at(0)->contents_scale());
504  EXPECT_FLOAT_EQ(
505      1.3f * low_res_factor,
506      pending_layer_->tilings()->tiling_at(1)->contents_scale());
507
508  // If we change the layer's CSS scale factor, then we should not get new
509  // tilings.
510  pending_layer_->CalculateContentsScale(1.8f,  // ideal contents scale
511                                         1.7f,  // device scale
512                                         3.2f,  // page cale
513                                         false,
514                                         &result_scale_x,
515                                         &result_scale_y,
516                                         &result_bounds);
517  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
518  EXPECT_FLOAT_EQ(
519      1.3f,
520      pending_layer_->tilings()->tiling_at(0)->contents_scale());
521  EXPECT_FLOAT_EQ(
522      1.3f * low_res_factor,
523      pending_layer_->tilings()->tiling_at(1)->contents_scale());
524
525  // If we change the page scale factor, then we should get new tilings.
526  pending_layer_->CalculateContentsScale(1.8f,  // ideal contents scale
527                                         1.7f,  // device scale
528                                         2.2f,  // page cale
529                                         false,
530                                         &result_scale_x,
531                                         &result_scale_y,
532                                         &result_bounds);
533  ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
534  EXPECT_FLOAT_EQ(
535      1.8f,
536      pending_layer_->tilings()->tiling_at(0)->contents_scale());
537  EXPECT_FLOAT_EQ(
538      1.8f * low_res_factor,
539      pending_layer_->tilings()->tiling_at(2)->contents_scale());
540
541  // If we change the device scale factor, then we should get new tilings.
542  pending_layer_->CalculateContentsScale(1.9f,  // ideal contents scale
543                                         1.4f,  // device scale
544                                         2.2f,  // page cale
545                                         false,
546                                         &result_scale_x,
547                                         &result_scale_y,
548                                         &result_bounds);
549  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
550  EXPECT_FLOAT_EQ(
551      1.9f,
552      pending_layer_->tilings()->tiling_at(0)->contents_scale());
553  EXPECT_FLOAT_EQ(
554      1.9f * low_res_factor,
555      pending_layer_->tilings()->tiling_at(3)->contents_scale());
556
557  // If we change the device scale factor, but end up at the same total scale
558  // factor somehow, then we don't get new tilings.
559  pending_layer_->CalculateContentsScale(1.9f,  // ideal contents scale
560                                         2.2f,  // device scale
561                                         1.4f,  // page cale
562                                         false,
563                                         &result_scale_x,
564                                         &result_scale_y,
565                                         &result_bounds);
566  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
567  EXPECT_FLOAT_EQ(
568      1.9f,
569      pending_layer_->tilings()->tiling_at(0)->contents_scale());
570  EXPECT_FLOAT_EQ(
571      1.9f * low_res_factor,
572      pending_layer_->tilings()->tiling_at(3)->contents_scale());
573}
574
575TEST_F(PictureLayerImplTest, CleanUpTilings) {
576  gfx::Size tile_size(400, 400);
577  gfx::Size layer_bounds(1300, 1900);
578
579  scoped_refptr<FakePicturePileImpl> pending_pile =
580      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
581  scoped_refptr<FakePicturePileImpl> active_pile =
582      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
583
584  float result_scale_x, result_scale_y;
585  gfx::Size result_bounds;
586  std::vector<PictureLayerTiling*> used_tilings;
587
588  SetupTrees(pending_pile, active_pile);
589  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
590
591  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
592  EXPECT_LT(low_res_factor, 1.f);
593
594  float device_scale = 1.7f;
595  float page_scale = 3.2f;
596
597  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false);
598  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
599
600  // We only have ideal tilings, so they aren't removed.
601  used_tilings.clear();
602  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
603  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
604
605  // Changing the ideal but not creating new tilings.
606  SetContentsScaleOnBothLayers(1.5f, device_scale, page_scale, false);
607  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
608
609  // The tilings are still our target scale, so they aren't removed.
610  used_tilings.clear();
611  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
612  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
613
614  // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
615  page_scale = 1.2f;
616  SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, false);
617  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
618  EXPECT_FLOAT_EQ(
619      1.f,
620      active_layer_->tilings()->tiling_at(1)->contents_scale());
621  EXPECT_FLOAT_EQ(
622      1.f * low_res_factor,
623      active_layer_->tilings()->tiling_at(3)->contents_scale());
624
625  // Mark the non-ideal tilings as used. They won't be removed.
626  used_tilings.clear();
627  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
628  used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
629  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
630  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
631
632  // Now move the ideal scale to 0.5. Our target stays 1.2.
633  SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, false);
634
635  // The high resolution tiling is between target and ideal, so is not
636  // removed.  The low res tiling for the old ideal=1.0 scale is removed.
637  used_tilings.clear();
638  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
639  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
640
641  // Now move the ideal scale to 1.0. Our target stays 1.2.
642  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false);
643
644  // All the tilings are between are target and the ideal, so they are not
645  // removed.
646  used_tilings.clear();
647  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
648  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
649
650  // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
651  active_layer_->CalculateContentsScale(1.1f,
652                                        device_scale,
653                                        page_scale,
654                                        false,
655                                        &result_scale_x,
656                                        &result_scale_y,
657                                        &result_bounds);
658
659  // Because the pending layer's ideal scale is still 1.0, our tilings fall
660  // in the range [1.0,1.2] and are kept.
661  used_tilings.clear();
662  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
663  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
664
665  // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
666  // 1.2 still.
667  pending_layer_->CalculateContentsScale(1.1f,
668                                         device_scale,
669                                         page_scale,
670                                         false,
671                                         &result_scale_x,
672                                         &result_scale_y,
673                                         &result_bounds);
674
675  // Our 1.0 tiling now falls outside the range between our ideal scale and our
676  // target raster scale. But it is in our used tilings set, so nothing is
677  // deleted.
678  used_tilings.clear();
679  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
680  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
681  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
682
683  // If we remove it from our used tilings set, it is outside the range to keep
684  // so it is deleted.
685  used_tilings.clear();
686  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
687  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
688}
689
690#define EXPECT_BOTH_EQ(expression, x)         \
691  do {                                        \
692    EXPECT_EQ(pending_layer_->expression, x); \
693    EXPECT_EQ(active_layer_->expression, x);  \
694  } while (false)
695
696TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
697  // Make sure this layer covers multiple tiles, since otherwise low
698  // res won't get created because it is too small.
699  gfx::Size tile_size(host_impl_.settings().default_tile_size);
700  SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
701  // Avoid max untiled layer size heuristics via fixed tile size.
702  pending_layer_->set_fixed_tile_size(tile_size);
703  active_layer_->set_fixed_tile_size(tile_size);
704
705  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
706  float contents_scale = 1.f;
707  float device_scale = 1.f;
708  float page_scale = 1.f;
709  bool animating_transform = true;
710
711  // Animating, so don't create low res even if there isn't one already.
712  SetContentsScaleOnBothLayers(
713      contents_scale, device_scale, page_scale, animating_transform);
714  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
715  EXPECT_BOTH_EQ(num_tilings(), 1u);
716
717  // Stop animating, low res gets created.
718  animating_transform = false;
719  SetContentsScaleOnBothLayers(
720      contents_scale, device_scale, page_scale, animating_transform);
721  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
722  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
723  EXPECT_BOTH_EQ(num_tilings(), 2u);
724
725  // Page scale animation, new high res, but not new low res because animating.
726  contents_scale = 4.f;
727  page_scale = 4.f;
728  animating_transform = true;
729  SetContentsScaleOnBothLayers(
730      contents_scale, device_scale, page_scale, animating_transform);
731  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
732  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
733  EXPECT_BOTH_EQ(num_tilings(), 3u);
734
735  // Stop animating, new low res gets created for final page scale.
736  animating_transform = false;
737  SetContentsScaleOnBothLayers(
738      contents_scale, device_scale, page_scale, animating_transform);
739  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
740  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 4.f * low_res_factor);
741  EXPECT_BOTH_EQ(num_tilings(), 4u);
742}
743
744TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
745  gfx::Size tile_size(host_impl_.settings().default_tile_size);
746  SetupDefaultTrees(tile_size);
747
748  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
749  float device_scale = 1.f;
750  float page_scale = 1.f;
751  bool animating_transform = false;
752
753  // Contents exactly fit on one tile at scale 1, no low res.
754  float contents_scale = 1.f;
755  SetContentsScaleOnBothLayers(
756      contents_scale, device_scale, page_scale, animating_transform);
757  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
758  EXPECT_BOTH_EQ(num_tilings(), 1u);
759
760  ResetTilingsAndRasterScales();
761
762  // Contents that are smaller than one tile, no low res.
763  contents_scale = 0.123f;
764  SetContentsScaleOnBothLayers(
765      contents_scale, device_scale, page_scale, animating_transform);
766  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
767  EXPECT_BOTH_EQ(num_tilings(), 1u);
768
769  ResetTilingsAndRasterScales();
770
771  // Any content bounds that would create more than one tile will
772  // generate a low res tiling.
773  contents_scale = 2.5f;
774  SetContentsScaleOnBothLayers(
775      contents_scale, device_scale, page_scale, animating_transform);
776  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
777  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
778                 contents_scale * low_res_factor);
779  EXPECT_BOTH_EQ(num_tilings(), 2u);
780
781  ResetTilingsAndRasterScales();
782
783  // Mask layers dont create low res since they always fit on one tile.
784  pending_layer_->SetIsMask(true);
785  active_layer_->SetIsMask(true);
786  SetContentsScaleOnBothLayers(
787      contents_scale, device_scale, page_scale, animating_transform);
788  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
789  EXPECT_BOTH_EQ(num_tilings(), 1u);
790}
791
792TEST_F(PictureLayerImplTest, DidLoseOutputSurface) {
793  gfx::Size tile_size(400, 400);
794  gfx::Size layer_bounds(1300, 1900);
795
796  scoped_refptr<FakePicturePileImpl> pending_pile =
797      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
798  scoped_refptr<FakePicturePileImpl> active_pile =
799      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
800
801  float result_scale_x, result_scale_y;
802  gfx::Size result_bounds;
803
804  SetupTrees(pending_pile, active_pile);
805  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
806
807  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
808                                         2.7f,  // device scale
809                                         3.2f,  // page cale
810                                         false,
811                                         &result_scale_x,
812                                         &result_scale_y,
813                                         &result_bounds);
814  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
815
816  // All tilings should be removed when losing output surface.
817  active_layer_->DidLoseOutputSurface();
818  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
819  pending_layer_->DidLoseOutputSurface();
820  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
821
822  // This should create new tilings.
823  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
824                                         2.7f,  // device scale
825                                         3.2f,  // page cale
826                                         false,
827                                         &result_scale_x,
828                                         &result_scale_y,
829                                         &result_bounds);
830  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
831}
832
833TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
834  // The default max tile size is larger than 400x400.
835  gfx::Size tile_size(400, 400);
836  gfx::Size layer_bounds(5000, 5000);
837
838  scoped_refptr<FakePicturePileImpl> pending_pile =
839      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
840  scoped_refptr<FakePicturePileImpl> active_pile =
841      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
842
843  float result_scale_x, result_scale_y;
844  gfx::Size result_bounds;
845
846  SetupTrees(pending_pile, active_pile);
847  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
848
849  pending_layer_->CalculateContentsScale(
850      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
851  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
852
853  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
854
855  // The default value.
856  EXPECT_EQ(gfx::Size(256, 256).ToString(),
857            host_impl_.settings().default_tile_size.ToString());
858
859  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
860  EXPECT_EQ(gfx::Size(256, 256).ToString(),
861            tile->content_rect().size().ToString());
862
863  pending_layer_->DidLoseOutputSurface();
864
865  // Change the max texture size on the output surface context.
866  scoped_ptr<TestWebGraphicsContext3D> context =
867      TestWebGraphicsContext3D::Create();
868  context->set_max_texture_size(140);
869  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
870      context.Pass()).PassAs<OutputSurface>());
871
872  pending_layer_->CalculateContentsScale(
873      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
874  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
875
876  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
877
878  // Verify the tiles are not larger than the context's max texture size.
879  tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
880  EXPECT_GE(140, tile->content_rect().width());
881  EXPECT_GE(140, tile->content_rect().height());
882}
883
884TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
885  // The default max tile size is larger than 400x400.
886  gfx::Size tile_size(400, 400);
887  gfx::Size layer_bounds(500, 500);
888
889  scoped_refptr<FakePicturePileImpl> pending_pile =
890      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
891  scoped_refptr<FakePicturePileImpl> active_pile =
892      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
893
894  float result_scale_x, result_scale_y;
895  gfx::Size result_bounds;
896
897  SetupTrees(pending_pile, active_pile);
898  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
899
900  pending_layer_->CalculateContentsScale(
901      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
902  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
903
904  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
905
906  // The default value. The layer is smaller than this.
907  EXPECT_EQ(gfx::Size(512, 512).ToString(),
908            host_impl_.settings().max_untiled_layer_size.ToString());
909
910  // There should be a single tile since the layer is small.
911  PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
912  EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
913
914  pending_layer_->DidLoseOutputSurface();
915
916  // Change the max texture size on the output surface context.
917  scoped_ptr<TestWebGraphicsContext3D> context =
918      TestWebGraphicsContext3D::Create();
919  context->set_max_texture_size(140);
920  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
921      context.Pass()).PassAs<OutputSurface>());
922
923  pending_layer_->CalculateContentsScale(
924      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
925  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
926
927  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
928
929  // There should be more than one tile since the max texture size won't cover
930  // the layer.
931  high_res_tiling = pending_layer_->tilings()->tiling_at(0);
932  EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
933
934  // Verify the tiles are not larger than the context's max texture size.
935  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
936  EXPECT_GE(140, tile->content_rect().width());
937  EXPECT_GE(140, tile->content_rect().height());
938}
939
940TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
941  MockQuadCuller quad_culler;
942
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  SetupTrees(pending_pile, active_pile);
952
953  active_layer_->SetContentBounds(layer_bounds);
954  active_layer_->draw_properties().visible_content_rect =
955      gfx::Rect(layer_bounds);
956
957  gfx::Rect layer_invalidation(150, 200, 30, 180);
958  Region invalidation(layer_invalidation);
959  AddDefaultTilingsWithInvalidation(invalidation);
960
961  AppendQuadsData data;
962  active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
963  active_layer_->AppendQuads(&quad_culler, &data);
964  active_layer_->DidDraw(NULL);
965
966  ASSERT_EQ(1U, quad_culler.quad_list().size());
967  EXPECT_EQ(DrawQuad::PICTURE_CONTENT, quad_culler.quad_list()[0]->material);
968}
969
970TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
971  gfx::Size tile_size(100, 100);
972  gfx::Size layer_bounds(1000, 1000);
973
974  scoped_refptr<FakePicturePileImpl> pending_pile =
975      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
976  // Layers with entirely empty piles can't get tilings.
977  pending_pile->AddRecordingAt(0, 0);
978
979  SetupPendingTree(pending_pile);
980
981  ASSERT_TRUE(pending_layer_->CanHaveTilings());
982  pending_layer_->AddTiling(1.0f);
983  pending_layer_->AddTiling(2.0f);
984
985  // It should be safe to call this (and MarkVisibleResourcesAsRequired)
986  // on a layer with no recordings.
987  host_impl_.pending_tree()->UpdateDrawProperties();
988  pending_layer_->MarkVisibleResourcesAsRequired();
989}
990
991TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
992  gfx::Size tile_size(100, 100);
993  gfx::Size layer_bounds(200, 100);
994
995  scoped_refptr<FakePicturePileImpl> pending_pile =
996      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
997  SetupPendingTree(pending_pile);
998
999  pending_layer_->set_fixed_tile_size(tile_size);
1000  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1001  PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1002  host_impl_.pending_tree()->UpdateDrawProperties();
1003  EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1004
1005  // Fake set priorities.
1006  int tile_count = 0;
1007  for (PictureLayerTiling::CoverageIterator iter(
1008           tiling,
1009           pending_layer_->contents_scale_x(),
1010           gfx::Rect(pending_layer_->visible_content_rect()));
1011       iter;
1012       ++iter) {
1013    if (!*iter)
1014      continue;
1015    Tile* tile = *iter;
1016    TilePriority priority;
1017    priority.resolution = HIGH_RESOLUTION;
1018    if (++tile_count % 2) {
1019      priority.time_to_visible_in_seconds = 0.f;
1020      priority.distance_to_visible_in_pixels = 0.f;
1021    } else {
1022      priority.time_to_visible_in_seconds = 1.f;
1023      priority.distance_to_visible_in_pixels = 1.f;
1024    }
1025    tile->SetPriority(PENDING_TREE, priority);
1026  }
1027
1028  pending_layer_->MarkVisibleResourcesAsRequired();
1029
1030  int num_visible = 0;
1031  int num_offscreen = 0;
1032
1033  for (PictureLayerTiling::CoverageIterator iter(
1034           tiling,
1035           pending_layer_->contents_scale_x(),
1036           gfx::Rect(pending_layer_->visible_content_rect()));
1037       iter;
1038       ++iter) {
1039    if (!*iter)
1040      continue;
1041    const Tile* tile = *iter;
1042    if (tile->priority(PENDING_TREE).distance_to_visible_in_pixels == 0.f) {
1043      EXPECT_TRUE(tile->required_for_activation());
1044      num_visible++;
1045    } else {
1046      EXPECT_FALSE(tile->required_for_activation());
1047      num_offscreen++;
1048    }
1049  }
1050
1051  EXPECT_GT(num_visible, 0);
1052  EXPECT_GT(num_offscreen, 0);
1053}
1054
1055TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1056  gfx::Size tile_size(100, 100);
1057  gfx::Size layer_bounds(400, 400);
1058  scoped_refptr<FakePicturePileImpl> pending_pile =
1059      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1060
1061  host_impl_.CreatePendingTree();
1062  LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1063
1064  scoped_ptr<FakePictureLayerImpl> pending_layer =
1065      FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1066  pending_layer->SetDrawsContent(true);
1067  pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1068
1069  pending_layer_ = static_cast<FakePictureLayerImpl*>(
1070      host_impl_.pending_tree()->LayerById(id_));
1071
1072  // Set some state on the pending layer, make sure it is not clobbered
1073  // by a sync from the active layer.  This could happen because if the
1074  // pending layer has not been post-commit initialized it will attempt
1075  // to sync from the active layer.
1076  bool default_lcd_text_setting = pending_layer_->is_using_lcd_text();
1077  pending_layer_->force_set_lcd_text(!default_lcd_text_setting);
1078  EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1079
1080  host_impl_.ActivatePendingTree();
1081
1082  active_layer_ = static_cast<FakePictureLayerImpl*>(
1083      host_impl_.active_tree()->LayerById(id_));
1084
1085  EXPECT_EQ(0u, active_layer_->num_tilings());
1086  EXPECT_EQ(!default_lcd_text_setting, active_layer_->is_using_lcd_text());
1087  EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1088}
1089
1090}  // namespace
1091}  // namespace cc
1092