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