picture_layer_impl_unittest.cc revision 3551c9c881056c480085172ff9840cab31610854
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/SkDevice.h"
24#include "ui/gfx/rect_conversions.h"
25
26namespace cc {
27namespace {
28
29class MockCanvas : public SkCanvas {
30 public:
31  explicit MockCanvas(SkDevice* 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    SkDevice 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, ClonePartialInvalidation) {
245  gfx::Size tile_size(100, 100);
246  gfx::Size layer_bounds(400, 400);
247  gfx::Rect layer_invalidation(150, 200, 30, 180);
248
249  scoped_refptr<FakePicturePileImpl> pending_pile =
250      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
251  scoped_refptr<FakePicturePileImpl> active_pile =
252      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
253
254  SetupTrees(pending_pile, active_pile);
255
256  Region invalidation(layer_invalidation);
257  AddDefaultTilingsWithInvalidation(invalidation);
258
259  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
260  EXPECT_GT(tilings->num_tilings(), 0u);
261  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
262    const PictureLayerTiling* tiling = tilings->tiling_at(i);
263    gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
264        layer_invalidation,
265        tiling->contents_scale());
266    for (PictureLayerTiling::CoverageIterator
267             iter(tiling,
268                  tiling->contents_scale(),
269                  tiling->ContentRect());
270         iter;
271         ++iter) {
272      EXPECT_TRUE(*iter);
273      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
274      if (iter.geometry_rect().Intersects(content_invalidation))
275        EXPECT_EQ(pending_pile, iter->picture_pile());
276      else
277        EXPECT_EQ(active_pile, iter->picture_pile());
278    }
279  }
280}
281
282TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
283  gfx::Size tile_size(90, 80);
284  gfx::Size layer_bounds(300, 500);
285
286  scoped_refptr<FakePicturePileImpl> pending_pile =
287      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
288  scoped_refptr<FakePicturePileImpl> active_pile =
289      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
290
291  SetupTrees(pending_pile, active_pile);
292
293  Region invalidation((gfx::Rect(layer_bounds)));
294  AddDefaultTilingsWithInvalidation(invalidation);
295
296  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
297            active_layer_->tilings()->num_tilings());
298
299  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
300  EXPECT_GT(tilings->num_tilings(), 0u);
301  for (size_t i = 0; i < tilings->num_tilings(); ++i)
302    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
303}
304
305TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
306  gfx::Size tile_size(90, 80);
307  gfx::Size active_layer_bounds(300, 500);
308  gfx::Size pending_layer_bounds(400, 800);
309
310  scoped_refptr<FakePicturePileImpl> pending_pile =
311      FakePicturePileImpl::CreateFilledPile(tile_size,
312                                                pending_layer_bounds);
313  scoped_refptr<FakePicturePileImpl> active_pile =
314      FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
315
316  SetupTrees(pending_pile, active_pile);
317  pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
318
319  Region invalidation;
320  AddDefaultTilingsWithInvalidation(invalidation);
321
322  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
323  EXPECT_GT(tilings->num_tilings(), 0u);
324  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
325    const PictureLayerTiling* tiling = tilings->tiling_at(i);
326    gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
327        gfx::Rect(active_layer_bounds),
328        tiling->contents_scale());
329    for (PictureLayerTiling::CoverageIterator
330             iter(tiling,
331                  tiling->contents_scale(),
332                  tiling->ContentRect());
333         iter;
334         ++iter) {
335      EXPECT_TRUE(*iter);
336      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
337      std::vector<Tile*> active_tiles =
338          active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
339      std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
340      if (iter.geometry_rect().right() >= active_content_bounds.width() ||
341          iter.geometry_rect().bottom() >= active_content_bounds.height() ||
342          active_tiles[0]->content_rect().size() !=
343              pending_tiles[0]->content_rect().size()) {
344        EXPECT_EQ(pending_pile, iter->picture_pile());
345      } else {
346        EXPECT_EQ(active_pile, iter->picture_pile());
347      }
348    }
349  }
350}
351
352TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
353  gfx::Size tile_size(400, 400);
354  gfx::Size layer_bounds(1300, 1900);
355
356  scoped_refptr<FakePicturePileImpl> pending_pile =
357      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
358  scoped_refptr<FakePicturePileImpl> active_pile =
359      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
360
361  // Fill in some of active pile, but more of pending pile.
362  int hole_count = 0;
363  for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
364    for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
365      if ((x + y) % 2) {
366        pending_pile->AddRecordingAt(x, y);
367        active_pile->AddRecordingAt(x, y);
368      } else {
369        hole_count++;
370        if (hole_count % 2)
371          pending_pile->AddRecordingAt(x, y);
372      }
373    }
374  }
375
376  SetupTrees(pending_pile, active_pile);
377  Region invalidation;
378  AddDefaultTilingsWithInvalidation(invalidation);
379
380  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
381  EXPECT_GT(tilings->num_tilings(), 0u);
382  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
383    const PictureLayerTiling* tiling = tilings->tiling_at(i);
384
385    for (PictureLayerTiling::CoverageIterator
386             iter(tiling,
387                  tiling->contents_scale(),
388                  tiling->ContentRect());
389         iter;
390         ++iter) {
391      EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
392      // Ensure there is a recording for this tile.
393      gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
394          iter.full_tile_geometry_rect(), 1.f / tiling->contents_scale());
395      layer_rect.Intersect(gfx::Rect(layer_bounds));
396
397      bool in_pending = pending_pile->recorded_region().Contains(layer_rect);
398      bool in_active = active_pile->recorded_region().Contains(layer_rect);
399
400      if (in_pending && !in_active)
401        EXPECT_EQ(pending_pile, iter->picture_pile());
402      else if (in_active)
403        EXPECT_EQ(active_pile, iter->picture_pile());
404      else
405        EXPECT_FALSE(*iter);
406    }
407  }
408}
409
410TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
411  gfx::Size tile_size(400, 400);
412  gfx::Size layer_bounds(1300, 1900);
413
414  scoped_refptr<FakePicturePileImpl> pending_pile =
415      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
416  scoped_refptr<FakePicturePileImpl> active_pile =
417      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
418
419  float result_scale_x, result_scale_y;
420  gfx::Size result_bounds;
421
422  SetupTrees(pending_pile, active_pile);
423
424  pending_layer_->CalculateContentsScale(
425      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
426
427  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
428}
429
430TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
431  gfx::Size tile_size(400, 400);
432  gfx::Size layer_bounds(1300, 1900);
433
434  scoped_refptr<FakePicturePileImpl> pending_pile =
435      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
436  scoped_refptr<FakePicturePileImpl> active_pile =
437      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
438
439  float result_scale_x, result_scale_y;
440  gfx::Size result_bounds;
441
442  SetupTrees(pending_pile, active_pile);
443  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
444
445  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
446  EXPECT_LT(low_res_factor, 1.f);
447
448  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
449                                         1.7f,  // device scale
450                                         3.2f,  // page cale
451                                         false,
452                                         &result_scale_x,
453                                         &result_scale_y,
454                                         &result_bounds);
455  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
456  EXPECT_FLOAT_EQ(
457      1.3f,
458      pending_layer_->tilings()->tiling_at(0)->contents_scale());
459  EXPECT_FLOAT_EQ(
460      1.3f * low_res_factor,
461      pending_layer_->tilings()->tiling_at(1)->contents_scale());
462
463  // If we change the layer's CSS scale factor, then we should not get new
464  // tilings.
465  pending_layer_->CalculateContentsScale(1.8f,  // ideal contents scale
466                                         1.7f,  // device scale
467                                         3.2f,  // page cale
468                                         false,
469                                         &result_scale_x,
470                                         &result_scale_y,
471                                         &result_bounds);
472  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
473  EXPECT_FLOAT_EQ(
474      1.3f,
475      pending_layer_->tilings()->tiling_at(0)->contents_scale());
476  EXPECT_FLOAT_EQ(
477      1.3f * low_res_factor,
478      pending_layer_->tilings()->tiling_at(1)->contents_scale());
479
480  // If we change the page scale factor, then we should get new tilings.
481  pending_layer_->CalculateContentsScale(1.8f,  // ideal contents scale
482                                         1.7f,  // device scale
483                                         2.2f,  // page cale
484                                         false,
485                                         &result_scale_x,
486                                         &result_scale_y,
487                                         &result_bounds);
488  ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
489  EXPECT_FLOAT_EQ(
490      1.8f,
491      pending_layer_->tilings()->tiling_at(0)->contents_scale());
492  EXPECT_FLOAT_EQ(
493      1.8f * low_res_factor,
494      pending_layer_->tilings()->tiling_at(2)->contents_scale());
495
496  // If we change the device scale factor, then we should get new tilings.
497  pending_layer_->CalculateContentsScale(1.9f,  // ideal contents scale
498                                         1.4f,  // device scale
499                                         2.2f,  // page cale
500                                         false,
501                                         &result_scale_x,
502                                         &result_scale_y,
503                                         &result_bounds);
504  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
505  EXPECT_FLOAT_EQ(
506      1.9f,
507      pending_layer_->tilings()->tiling_at(0)->contents_scale());
508  EXPECT_FLOAT_EQ(
509      1.9f * low_res_factor,
510      pending_layer_->tilings()->tiling_at(3)->contents_scale());
511
512  // If we change the device scale factor, but end up at the same total scale
513  // factor somehow, then we don't get new tilings.
514  pending_layer_->CalculateContentsScale(1.9f,  // ideal contents scale
515                                         2.2f,  // device scale
516                                         1.4f,  // page cale
517                                         false,
518                                         &result_scale_x,
519                                         &result_scale_y,
520                                         &result_bounds);
521  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
522  EXPECT_FLOAT_EQ(
523      1.9f,
524      pending_layer_->tilings()->tiling_at(0)->contents_scale());
525  EXPECT_FLOAT_EQ(
526      1.9f * low_res_factor,
527      pending_layer_->tilings()->tiling_at(3)->contents_scale());
528}
529
530TEST_F(PictureLayerImplTest, CleanUpTilings) {
531  gfx::Size tile_size(400, 400);
532  gfx::Size layer_bounds(1300, 1900);
533
534  scoped_refptr<FakePicturePileImpl> pending_pile =
535      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
536  scoped_refptr<FakePicturePileImpl> active_pile =
537      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
538
539  float result_scale_x, result_scale_y;
540  gfx::Size result_bounds;
541  std::vector<PictureLayerTiling*> used_tilings;
542
543  SetupTrees(pending_pile, active_pile);
544  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
545
546  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
547  EXPECT_LT(low_res_factor, 1.f);
548
549  float device_scale = 1.7f;
550  float page_scale = 3.2f;
551
552  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false);
553  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
554
555  // We only have ideal tilings, so they aren't removed.
556  used_tilings.clear();
557  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
558  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
559
560  // Changing the ideal but not creating new tilings.
561  SetContentsScaleOnBothLayers(1.5f, device_scale, page_scale, false);
562  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
563
564  // The tilings are still our target scale, so they aren't removed.
565  used_tilings.clear();
566  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
567  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
568
569  // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
570  page_scale = 1.2f;
571  SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, false);
572  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
573  EXPECT_FLOAT_EQ(
574      1.f,
575      active_layer_->tilings()->tiling_at(1)->contents_scale());
576  EXPECT_FLOAT_EQ(
577      1.f * low_res_factor,
578      active_layer_->tilings()->tiling_at(3)->contents_scale());
579
580  // Mark the non-ideal tilings as used. They won't be removed.
581  used_tilings.clear();
582  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
583  used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
584  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
585  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
586
587  // Now move the ideal scale to 0.5. Our target stays 1.2.
588  SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, false);
589
590  // The high resolution tiling is between target and ideal, so is not
591  // removed.  The low res tiling for the old ideal=1.0 scale is removed.
592  used_tilings.clear();
593  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
594  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
595
596  // Now move the ideal scale to 1.0. Our target stays 1.2.
597  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false);
598
599  // All the tilings are between are target and the ideal, so they are not
600  // removed.
601  used_tilings.clear();
602  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
603  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
604
605  // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
606  active_layer_->CalculateContentsScale(1.1f,
607                                        device_scale,
608                                        page_scale,
609                                        false,
610                                        &result_scale_x,
611                                        &result_scale_y,
612                                        &result_bounds);
613
614  // Because the pending layer's ideal scale is still 1.0, our tilings fall
615  // in the range [1.0,1.2] and are kept.
616  used_tilings.clear();
617  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
618  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
619
620  // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
621  // 1.2 still.
622  pending_layer_->CalculateContentsScale(1.1f,
623                                         device_scale,
624                                         page_scale,
625                                         false,
626                                         &result_scale_x,
627                                         &result_scale_y,
628                                         &result_bounds);
629
630  // Our 1.0 tiling now falls outside the range between our ideal scale and our
631  // target raster scale. But it is in our used tilings set, so nothing is
632  // deleted.
633  used_tilings.clear();
634  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
635  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
636  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
637
638  // If we remove it from our used tilings set, it is outside the range to keep
639  // so it is deleted.
640  used_tilings.clear();
641  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
642  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
643}
644
645#define EXPECT_BOTH_EQ(expression, x)         \
646  do {                                        \
647    EXPECT_EQ(pending_layer_->expression, x); \
648    EXPECT_EQ(active_layer_->expression, x);  \
649  } while (false)
650
651TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
652  // Make sure this layer covers multiple tiles, since otherwise low
653  // res won't get created because it is too small.
654  gfx::Size tile_size(host_impl_.settings().default_tile_size);
655  SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
656  // Avoid max untiled layer size heuristics via fixed tile size.
657  pending_layer_->set_fixed_tile_size(tile_size);
658  active_layer_->set_fixed_tile_size(tile_size);
659
660  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
661  float contents_scale = 1.f;
662  float device_scale = 1.f;
663  float page_scale = 1.f;
664  bool animating_transform = true;
665
666  // Animating, so don't create low res even if there isn't one already.
667  SetContentsScaleOnBothLayers(
668      contents_scale, device_scale, page_scale, animating_transform);
669  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
670  EXPECT_BOTH_EQ(num_tilings(), 1u);
671
672  // Stop animating, low res gets created.
673  animating_transform = false;
674  SetContentsScaleOnBothLayers(
675      contents_scale, device_scale, page_scale, animating_transform);
676  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
677  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
678  EXPECT_BOTH_EQ(num_tilings(), 2u);
679
680  // Page scale animation, new high res, but not new low res because animating.
681  contents_scale = 4.f;
682  page_scale = 4.f;
683  animating_transform = true;
684  SetContentsScaleOnBothLayers(
685      contents_scale, device_scale, page_scale, animating_transform);
686  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
687  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
688  EXPECT_BOTH_EQ(num_tilings(), 3u);
689
690  // Stop animating, new low res gets created for final page scale.
691  animating_transform = false;
692  SetContentsScaleOnBothLayers(
693      contents_scale, device_scale, page_scale, animating_transform);
694  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
695  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 4.f * low_res_factor);
696  EXPECT_BOTH_EQ(num_tilings(), 4u);
697}
698
699TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
700  gfx::Size tile_size(host_impl_.settings().default_tile_size);
701  SetupDefaultTrees(tile_size);
702
703  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
704  float device_scale = 1.f;
705  float page_scale = 1.f;
706  bool animating_transform = false;
707
708  // Contents exactly fit on one tile at scale 1, no low res.
709  float contents_scale = 1.f;
710  SetContentsScaleOnBothLayers(
711      contents_scale, device_scale, page_scale, animating_transform);
712  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
713  EXPECT_BOTH_EQ(num_tilings(), 1u);
714
715  ResetTilingsAndRasterScales();
716
717  // Contents that are smaller than one tile, no low res.
718  contents_scale = 0.123f;
719  SetContentsScaleOnBothLayers(
720      contents_scale, device_scale, page_scale, animating_transform);
721  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
722  EXPECT_BOTH_EQ(num_tilings(), 1u);
723
724  ResetTilingsAndRasterScales();
725
726  // Any content bounds that would create more than one tile will
727  // generate a low res tiling.
728  contents_scale = 2.5f;
729  SetContentsScaleOnBothLayers(
730      contents_scale, device_scale, page_scale, animating_transform);
731  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
732  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
733                 contents_scale * low_res_factor);
734  EXPECT_BOTH_EQ(num_tilings(), 2u);
735
736  ResetTilingsAndRasterScales();
737
738  // Mask layers dont create low res since they always fit on one tile.
739  pending_layer_->SetIsMask(true);
740  active_layer_->SetIsMask(true);
741  SetContentsScaleOnBothLayers(
742      contents_scale, device_scale, page_scale, animating_transform);
743  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
744  EXPECT_BOTH_EQ(num_tilings(), 1u);
745}
746
747TEST_F(PictureLayerImplTest, DidLoseOutputSurface) {
748  gfx::Size tile_size(400, 400);
749  gfx::Size layer_bounds(1300, 1900);
750
751  scoped_refptr<FakePicturePileImpl> pending_pile =
752      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
753  scoped_refptr<FakePicturePileImpl> active_pile =
754      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
755
756  float result_scale_x, result_scale_y;
757  gfx::Size result_bounds;
758
759  SetupTrees(pending_pile, active_pile);
760  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
761
762  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
763                                         2.7f,  // device scale
764                                         3.2f,  // page cale
765                                         false,
766                                         &result_scale_x,
767                                         &result_scale_y,
768                                         &result_bounds);
769  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
770
771  // All tilings should be removed when losing output surface.
772  active_layer_->DidLoseOutputSurface();
773  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
774  pending_layer_->DidLoseOutputSurface();
775  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
776
777  // This should create new tilings.
778  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
779                                         2.7f,  // device scale
780                                         3.2f,  // page cale
781                                         false,
782                                         &result_scale_x,
783                                         &result_scale_y,
784                                         &result_bounds);
785  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
786}
787
788TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
789  // The default max tile size is larger than 400x400.
790  gfx::Size tile_size(400, 400);
791  gfx::Size layer_bounds(5000, 5000);
792
793  scoped_refptr<FakePicturePileImpl> pending_pile =
794      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
795  scoped_refptr<FakePicturePileImpl> active_pile =
796      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
797
798  float result_scale_x, result_scale_y;
799  gfx::Size result_bounds;
800
801  SetupTrees(pending_pile, active_pile);
802  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
803
804  pending_layer_->CalculateContentsScale(
805      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
806  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
807
808  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
809
810  // The default value.
811  EXPECT_EQ(gfx::Size(256, 256).ToString(),
812            host_impl_.settings().default_tile_size.ToString());
813
814  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
815  EXPECT_EQ(gfx::Size(256, 256).ToString(),
816            tile->content_rect().size().ToString());
817
818  pending_layer_->DidLoseOutputSurface();
819
820  // Change the max texture size on the output surface context.
821  scoped_ptr<TestWebGraphicsContext3D> context =
822      TestWebGraphicsContext3D::Create();
823  context->set_max_texture_size(140);
824  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
825      context.Pass()).PassAs<OutputSurface>());
826
827  pending_layer_->CalculateContentsScale(
828      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
829  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
830
831  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
832
833  // Verify the tiles are not larger than the context's max texture size.
834  tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
835  EXPECT_GE(140, tile->content_rect().width());
836  EXPECT_GE(140, tile->content_rect().height());
837}
838
839TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
840  // The default max tile size is larger than 400x400.
841  gfx::Size tile_size(400, 400);
842  gfx::Size layer_bounds(500, 500);
843
844  scoped_refptr<FakePicturePileImpl> pending_pile =
845      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
846  scoped_refptr<FakePicturePileImpl> active_pile =
847      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
848
849  float result_scale_x, result_scale_y;
850  gfx::Size result_bounds;
851
852  SetupTrees(pending_pile, active_pile);
853  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
854
855  pending_layer_->CalculateContentsScale(
856      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
857  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
858
859  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
860
861  // The default value. The layer is smaller than this.
862  EXPECT_EQ(gfx::Size(512, 512).ToString(),
863            host_impl_.settings().max_untiled_layer_size.ToString());
864
865  // There should be a single tile since the layer is small.
866  PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
867  EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
868
869  pending_layer_->DidLoseOutputSurface();
870
871  // Change the max texture size on the output surface context.
872  scoped_ptr<TestWebGraphicsContext3D> context =
873      TestWebGraphicsContext3D::Create();
874  context->set_max_texture_size(140);
875  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
876      context.Pass()).PassAs<OutputSurface>());
877
878  pending_layer_->CalculateContentsScale(
879      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
880  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
881
882  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
883
884  // There should be more than one tile since the max texture size won't cover
885  // the layer.
886  high_res_tiling = pending_layer_->tilings()->tiling_at(0);
887  EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
888
889  // Verify the tiles are not larger than the context's max texture size.
890  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
891  EXPECT_GE(140, tile->content_rect().width());
892  EXPECT_GE(140, tile->content_rect().height());
893}
894
895TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
896  MockQuadCuller quad_culler;
897
898  gfx::Size tile_size(400, 400);
899  gfx::Size layer_bounds(1300, 1900);
900
901  scoped_refptr<FakePicturePileImpl> pending_pile =
902      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
903  scoped_refptr<FakePicturePileImpl> active_pile =
904      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
905
906  SetupTrees(pending_pile, active_pile);
907
908  active_layer_->SetContentBounds(layer_bounds);
909  active_layer_->draw_properties().visible_content_rect =
910      gfx::Rect(layer_bounds);
911
912  gfx::Rect layer_invalidation(150, 200, 30, 180);
913  Region invalidation(layer_invalidation);
914  AddDefaultTilingsWithInvalidation(invalidation);
915
916  AppendQuadsData data;
917  active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
918  active_layer_->AppendQuads(&quad_culler, &data);
919  active_layer_->DidDraw(NULL);
920
921  ASSERT_EQ(1U, quad_culler.quad_list().size());
922  EXPECT_EQ(DrawQuad::PICTURE_CONTENT, quad_culler.quad_list()[0]->material);
923}
924
925TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
926  gfx::Size tile_size(100, 100);
927  gfx::Size layer_bounds(1000, 1000);
928
929  scoped_refptr<FakePicturePileImpl> pending_pile =
930      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
931  // Layers with entirely empty piles can't get tilings.
932  pending_pile->AddRecordingAt(0, 0);
933
934  SetupPendingTree(pending_pile);
935
936  ASSERT_TRUE(pending_layer_->CanHaveTilings());
937  pending_layer_->AddTiling(1.0f);
938  pending_layer_->AddTiling(2.0f);
939
940  // It should be safe to call this (and MarkVisibleResourcesAsRequired)
941  // on a layer with no recordings.
942  host_impl_.pending_tree()->UpdateDrawProperties();
943  pending_layer_->MarkVisibleResourcesAsRequired();
944}
945
946TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
947  gfx::Size tile_size(100, 100);
948  gfx::Size layer_bounds(200, 100);
949
950  scoped_refptr<FakePicturePileImpl> pending_pile =
951      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
952  SetupPendingTree(pending_pile);
953
954  pending_layer_->set_fixed_tile_size(tile_size);
955  ASSERT_TRUE(pending_layer_->CanHaveTilings());
956  PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
957  host_impl_.pending_tree()->UpdateDrawProperties();
958  EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
959
960  // Fake set priorities.
961  int tile_count = 0;
962  for (PictureLayerTiling::CoverageIterator iter(
963           tiling,
964           pending_layer_->contents_scale_x(),
965           gfx::Rect(pending_layer_->visible_content_rect()));
966       iter;
967       ++iter) {
968    if (!*iter)
969      continue;
970    Tile* tile = *iter;
971    TilePriority priority;
972    priority.resolution = HIGH_RESOLUTION;
973    if (++tile_count % 2) {
974      priority.time_to_visible_in_seconds = 0.f;
975      priority.distance_to_visible_in_pixels = 0.f;
976    } else {
977      priority.time_to_visible_in_seconds = 1.f;
978      priority.distance_to_visible_in_pixels = 1.f;
979    }
980    tile->SetPriority(PENDING_TREE, priority);
981  }
982
983  pending_layer_->MarkVisibleResourcesAsRequired();
984
985  int num_visible = 0;
986  int num_offscreen = 0;
987
988  for (PictureLayerTiling::CoverageIterator iter(
989           tiling,
990           pending_layer_->contents_scale_x(),
991           gfx::Rect(pending_layer_->visible_content_rect()));
992       iter;
993       ++iter) {
994    if (!*iter)
995      continue;
996    const Tile* tile = *iter;
997    if (tile->priority(PENDING_TREE).distance_to_visible_in_pixels == 0.f) {
998      EXPECT_TRUE(tile->required_for_activation());
999      num_visible++;
1000    } else {
1001      EXPECT_FALSE(tile->required_for_activation());
1002      num_offscreen++;
1003    }
1004  }
1005
1006  EXPECT_GT(num_visible, 0);
1007  EXPECT_GT(num_offscreen, 0);
1008}
1009
1010TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1011  gfx::Size tile_size(100, 100);
1012  gfx::Size layer_bounds(400, 400);
1013  scoped_refptr<FakePicturePileImpl> pending_pile =
1014      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1015
1016  host_impl_.CreatePendingTree();
1017  LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1018
1019  scoped_ptr<FakePictureLayerImpl> pending_layer =
1020      FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1021  pending_layer->SetDrawsContent(true);
1022  pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1023
1024  pending_layer_ = static_cast<FakePictureLayerImpl*>(
1025      host_impl_.pending_tree()->LayerById(id_));
1026
1027  // Set some state on the pending layer, make sure it is not clobbered
1028  // by a sync from the active layer.  This could happen because if the
1029  // pending layer has not been post-commit initialized it will attempt
1030  // to sync from the active layer.
1031  bool default_lcd_text_setting = pending_layer_->is_using_lcd_text();
1032  pending_layer_->force_set_lcd_text(!default_lcd_text_setting);
1033  EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1034
1035  host_impl_.ActivatePendingTree();
1036
1037  active_layer_ = static_cast<FakePictureLayerImpl*>(
1038      host_impl_.active_tree()->LayerById(id_));
1039
1040  EXPECT_EQ(0u, active_layer_->num_tilings());
1041  EXPECT_EQ(!default_lcd_text_setting, active_layer_->is_using_lcd_text());
1042  EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1043}
1044
1045}  // namespace
1046}  // namespace cc
1047