picture_layer_impl_unittest.cc revision f2477e01787aa58f445919b809d89e252beef54f
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/test/test_web_graphics_context_3d.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_), id_(7) {}
45
46  explicit PictureLayerImplTest(const LayerTreeSettings& settings)
47      : host_impl_(settings, &proxy_), id_(7) {}
48
49  virtual ~PictureLayerImplTest() {
50  }
51
52  virtual void SetUp() OVERRIDE {
53    InitializeRenderer();
54  }
55
56  virtual void InitializeRenderer() {
57    host_impl_.InitializeRenderer(CreateFakeOutputSurface());
58  }
59
60  void SetupDefaultTrees(gfx::Size layer_bounds) {
61    gfx::Size tile_size(100, 100);
62
63    scoped_refptr<FakePicturePileImpl> pending_pile =
64        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
65    scoped_refptr<FakePicturePileImpl> active_pile =
66        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
67
68    SetupTrees(pending_pile, active_pile);
69  }
70
71  void ActivateTree() {
72    host_impl_.ActivatePendingTree();
73    CHECK(!host_impl_.pending_tree());
74    pending_layer_ = NULL;
75    active_layer_ = static_cast<FakePictureLayerImpl*>(
76        host_impl_.active_tree()->LayerById(id_));
77  }
78
79  void SetupTrees(
80      scoped_refptr<PicturePileImpl> pending_pile,
81      scoped_refptr<PicturePileImpl> active_pile) {
82    SetupPendingTree(active_pile);
83    ActivateTree();
84    SetupPendingTree(pending_pile);
85  }
86
87  void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
88    active_layer_->AddTiling(2.3f);
89    active_layer_->AddTiling(1.0f);
90    active_layer_->AddTiling(0.5f);
91    for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
92      active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
93    pending_layer_->set_invalidation(invalidation);
94    for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
95      pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
96  }
97
98  void SetupPendingTree(
99      scoped_refptr<PicturePileImpl> pile) {
100    host_impl_.CreatePendingTree();
101    LayerTreeImpl* pending_tree = host_impl_.pending_tree();
102    // Clear recycled tree.
103    pending_tree->DetachLayerTree();
104
105    scoped_ptr<FakePictureLayerImpl> pending_layer =
106        FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
107    pending_layer->SetDrawsContent(true);
108    pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
109
110    pending_layer_ = static_cast<FakePictureLayerImpl*>(
111        host_impl_.pending_tree()->LayerById(id_));
112    pending_layer_->DoPostCommitInitializationIfNeeded();
113  }
114
115  static void VerifyAllTilesExistAndHavePile(
116      const PictureLayerTiling* tiling,
117      PicturePileImpl* pile) {
118    for (PictureLayerTiling::CoverageIterator
119             iter(tiling, tiling->contents_scale(), tiling->ContentRect());
120         iter;
121         ++iter) {
122      EXPECT_TRUE(*iter);
123      EXPECT_EQ(pile, iter->picture_pile());
124    }
125  }
126
127  void SetContentsScaleOnBothLayers(float contents_scale,
128                                    float device_scale_factor,
129                                    float page_scale_factor,
130                                    bool animating_transform) {
131    float result_scale_x, result_scale_y;
132    gfx::Size result_bounds;
133    pending_layer_->CalculateContentsScale(
134        contents_scale,
135        device_scale_factor,
136        page_scale_factor,
137        animating_transform,
138        &result_scale_x,
139        &result_scale_y,
140        &result_bounds);
141    active_layer_->CalculateContentsScale(
142        contents_scale,
143        device_scale_factor,
144        page_scale_factor,
145        animating_transform,
146        &result_scale_x,
147        &result_scale_y,
148        &result_bounds);
149  }
150
151  void ResetTilingsAndRasterScales() {
152    pending_layer_->DidLoseOutputSurface();
153    active_layer_->DidLoseOutputSurface();
154  }
155
156 protected:
157  void TestTileGridAlignmentCommon() {
158    // Layer to span 4 raster tiles in x and in y
159    ImplSidePaintingSettings settings;
160    gfx::Size layer_size(
161        settings.default_tile_size.width() * 7 / 2,
162        settings.default_tile_size.height() * 7 / 2);
163
164    scoped_refptr<FakePicturePileImpl> pending_pile =
165        FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
166    scoped_refptr<FakePicturePileImpl> active_pile =
167        FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
168
169    SetupTrees(pending_pile, active_pile);
170
171    float result_scale_x, result_scale_y;
172    gfx::Size result_bounds;
173    active_layer_->CalculateContentsScale(
174        1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
175
176    // Add 1x1 rects at the centers of each tile, then re-record pile contents
177    active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
178    std::vector<Tile*> tiles =
179        active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
180    EXPECT_EQ(16u, tiles.size());
181    std::vector<SkRect> rects;
182    std::vector<Tile*>::const_iterator tile_iter;
183    for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
184      gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
185      gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
186      active_pile->add_draw_rect(rect);
187      rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
188    }
189    // Force re-record with newly injected content
190    active_pile->RemoveRecordingAt(0, 0);
191    active_pile->AddRecordingAt(0, 0);
192
193    SkBitmap store;
194    store.setConfig(SkBitmap::kNo_Config, 1000, 1000);
195    SkBitmapDevice device(store);
196
197    std::vector<SkRect>::const_iterator rect_iter = rects.begin();
198    for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
199      MockCanvas mock_canvas(&device);
200      active_pile->RasterDirect(
201          &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
202
203      // This test verifies that when drawing the contents of a specific tile
204      // at content scale 1.0, the playback canvas never receives content from
205      // neighboring tiles which indicates that the tile grid embedded in
206      // SkPicture is perfectly aligned with the compositor's tiles.
207      EXPECT_EQ(1u, mock_canvas.rects_.size());
208      EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]);
209      rect_iter++;
210    }
211  }
212
213  FakeImplProxy proxy_;
214  FakeLayerTreeHostImpl host_impl_;
215  int id_;
216  FakePictureLayerImpl* pending_layer_;
217  FakePictureLayerImpl* active_layer_;
218
219 private:
220  DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
221};
222
223TEST_F(PictureLayerImplTest, TileGridAlignment) {
224  host_impl_.SetDeviceScaleFactor(1.f);
225  TestTileGridAlignmentCommon();
226}
227
228TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
229  host_impl_.SetDeviceScaleFactor(2.f);
230  TestTileGridAlignmentCommon();
231}
232
233TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
234  gfx::Size tile_size(100, 100);
235  gfx::Size layer_bounds(400, 400);
236
237  scoped_refptr<FakePicturePileImpl> pending_pile =
238      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
239  scoped_refptr<FakePicturePileImpl> active_pile =
240      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
241
242  SetupTrees(pending_pile, active_pile);
243
244  Region invalidation;
245  AddDefaultTilingsWithInvalidation(invalidation);
246
247  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
248            active_layer_->tilings()->num_tilings());
249
250  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
251  EXPECT_GT(tilings->num_tilings(), 0u);
252  for (size_t i = 0; i < tilings->num_tilings(); ++i)
253    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
254}
255
256TEST_F(PictureLayerImplTest, SuppressUpdateTilePriorities) {
257  base::TimeTicks time_ticks;
258  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
259
260  gfx::Size tile_size(100, 100);
261  gfx::Size layer_bounds(400, 400);
262
263  scoped_refptr<FakePicturePileImpl> pending_pile =
264      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
265  scoped_refptr<FakePicturePileImpl> active_pile =
266      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
267
268  SetupTrees(pending_pile, active_pile);
269
270  Region invalidation;
271  AddDefaultTilingsWithInvalidation(invalidation);
272  float dummy_contents_scale_x;
273  float dummy_contents_scale_y;
274  gfx::Size dummy_content_bounds;
275  active_layer_->CalculateContentsScale(1.f,
276                                        1.f,
277                                        1.f,
278                                        false,
279                                        &dummy_contents_scale_x,
280                                        &dummy_contents_scale_y,
281                                        &dummy_content_bounds);
282
283  EXPECT_TRUE(host_impl_.manage_tiles_needed());
284  active_layer_->UpdateTilePriorities();
285  host_impl_.ManageTiles();
286  EXPECT_FALSE(host_impl_.manage_tiles_needed());
287
288  time_ticks += base::TimeDelta::FromMilliseconds(200);
289  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
290
291  // Setting this boolean should cause an early out in UpdateTilePriorities.
292  bool valid_for_tile_management = false;
293  host_impl_.SetExternalDrawConstraints(gfx::Transform(),
294                                        gfx::Rect(layer_bounds),
295                                        gfx::Rect(layer_bounds),
296                                        valid_for_tile_management);
297  active_layer_->UpdateTilePriorities();
298  EXPECT_FALSE(host_impl_.manage_tiles_needed());
299
300  time_ticks += base::TimeDelta::FromMilliseconds(200);
301  host_impl_.SetCurrentFrameTimeTicks(time_ticks);
302
303  valid_for_tile_management = true;
304  host_impl_.SetExternalDrawConstraints(gfx::Transform(),
305                                        gfx::Rect(layer_bounds),
306                                        gfx::Rect(layer_bounds),
307                                        valid_for_tile_management);
308  active_layer_->UpdateTilePriorities();
309  EXPECT_TRUE(host_impl_.manage_tiles_needed());
310}
311
312TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
313  gfx::Size tile_size(100, 100);
314  gfx::Size layer_bounds(400, 400);
315  gfx::Rect layer_invalidation(150, 200, 30, 180);
316
317  scoped_refptr<FakePicturePileImpl> pending_pile =
318      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
319  scoped_refptr<FakePicturePileImpl> active_pile =
320      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
321
322  SetupTrees(pending_pile, active_pile);
323
324  Region invalidation(layer_invalidation);
325  AddDefaultTilingsWithInvalidation(invalidation);
326
327  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
328  EXPECT_GT(tilings->num_tilings(), 0u);
329  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
330    const PictureLayerTiling* tiling = tilings->tiling_at(i);
331    gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
332        layer_invalidation,
333        tiling->contents_scale());
334    for (PictureLayerTiling::CoverageIterator
335             iter(tiling,
336                  tiling->contents_scale(),
337                  tiling->ContentRect());
338         iter;
339         ++iter) {
340      EXPECT_TRUE(*iter);
341      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
342      if (iter.geometry_rect().Intersects(content_invalidation))
343        EXPECT_EQ(pending_pile, iter->picture_pile());
344      else
345        EXPECT_EQ(active_pile, iter->picture_pile());
346    }
347  }
348}
349
350TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
351  gfx::Size tile_size(90, 80);
352  gfx::Size layer_bounds(300, 500);
353
354  scoped_refptr<FakePicturePileImpl> pending_pile =
355      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
356  scoped_refptr<FakePicturePileImpl> active_pile =
357      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
358
359  SetupTrees(pending_pile, active_pile);
360
361  Region invalidation((gfx::Rect(layer_bounds)));
362  AddDefaultTilingsWithInvalidation(invalidation);
363
364  EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
365            active_layer_->tilings()->num_tilings());
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    VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
371}
372
373TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
374  gfx::Size tile_size(90, 80);
375  gfx::Size active_layer_bounds(300, 500);
376  gfx::Size pending_layer_bounds(400, 800);
377
378  scoped_refptr<FakePicturePileImpl> pending_pile =
379      FakePicturePileImpl::CreateFilledPile(tile_size,
380                                                pending_layer_bounds);
381  scoped_refptr<FakePicturePileImpl> active_pile =
382      FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
383
384  SetupTrees(pending_pile, active_pile);
385  pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
386
387  Region invalidation;
388  AddDefaultTilingsWithInvalidation(invalidation);
389
390  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
391  EXPECT_GT(tilings->num_tilings(), 0u);
392  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
393    const PictureLayerTiling* tiling = tilings->tiling_at(i);
394    gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
395        gfx::Rect(active_layer_bounds),
396        tiling->contents_scale());
397    for (PictureLayerTiling::CoverageIterator
398             iter(tiling,
399                  tiling->contents_scale(),
400                  tiling->ContentRect());
401         iter;
402         ++iter) {
403      EXPECT_TRUE(*iter);
404      EXPECT_FALSE(iter.geometry_rect().IsEmpty());
405      std::vector<Tile*> active_tiles =
406          active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
407      std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
408      if (iter.geometry_rect().right() >= active_content_bounds.width() ||
409          iter.geometry_rect().bottom() >= active_content_bounds.height() ||
410          active_tiles[0]->content_rect().size() !=
411              pending_tiles[0]->content_rect().size()) {
412        EXPECT_EQ(pending_pile, iter->picture_pile());
413      } else {
414        EXPECT_EQ(active_pile, iter->picture_pile());
415      }
416    }
417  }
418}
419
420TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
421  gfx::Size tile_size(400, 400);
422  gfx::Size layer_bounds(1300, 1900);
423
424  scoped_refptr<FakePicturePileImpl> pending_pile =
425      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
426  scoped_refptr<FakePicturePileImpl> active_pile =
427      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
428
429  // Fill in some of active pile, but more of pending pile.
430  int hole_count = 0;
431  for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
432    for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
433      if ((x + y) % 2) {
434        pending_pile->AddRecordingAt(x, y);
435        active_pile->AddRecordingAt(x, y);
436      } else {
437        hole_count++;
438        if (hole_count % 2)
439          pending_pile->AddRecordingAt(x, y);
440      }
441    }
442  }
443
444  SetupTrees(pending_pile, active_pile);
445  Region invalidation;
446  AddDefaultTilingsWithInvalidation(invalidation);
447
448  const PictureLayerTilingSet* tilings = pending_layer_->tilings();
449  EXPECT_GT(tilings->num_tilings(), 0u);
450  for (size_t i = 0; i < tilings->num_tilings(); ++i) {
451    const PictureLayerTiling* tiling = tilings->tiling_at(i);
452
453    for (PictureLayerTiling::CoverageIterator
454             iter(tiling,
455                  tiling->contents_scale(),
456                  tiling->ContentRect());
457         iter;
458         ++iter) {
459      EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
460      // Ensure there is a recording for this tile.
461      gfx::Rect layer_rect = gfx::ScaleToEnclosingRect(
462          iter.full_tile_geometry_rect(), 1.f / tiling->contents_scale());
463      layer_rect.Intersect(gfx::Rect(layer_bounds));
464
465      bool in_pending = pending_pile->recorded_region().Contains(layer_rect);
466      bool in_active = active_pile->recorded_region().Contains(layer_rect);
467
468      if (in_pending && !in_active)
469        EXPECT_EQ(pending_pile, iter->picture_pile());
470      else if (in_active)
471        EXPECT_EQ(active_pile, iter->picture_pile());
472      else
473        EXPECT_FALSE(*iter);
474    }
475  }
476}
477
478TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
479  gfx::Size tile_size(400, 400);
480  gfx::Size layer_bounds(1300, 1900);
481
482  scoped_refptr<FakePicturePileImpl> pending_pile =
483      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
484  scoped_refptr<FakePicturePileImpl> active_pile =
485      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
486
487  float result_scale_x, result_scale_y;
488  gfx::Size result_bounds;
489
490  SetupTrees(pending_pile, active_pile);
491
492  pending_layer_->CalculateContentsScale(
493      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
494
495  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
496}
497
498TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
499  gfx::Size tile_size(400, 400);
500  gfx::Size layer_bounds(1300, 1900);
501
502  scoped_refptr<FakePicturePileImpl> pending_pile =
503      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
504  scoped_refptr<FakePicturePileImpl> active_pile =
505      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
506
507  float result_scale_x, result_scale_y;
508  gfx::Size result_bounds;
509
510  SetupTrees(pending_pile, active_pile);
511  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
512
513  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
514  EXPECT_LT(low_res_factor, 1.f);
515
516  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
517                                         1.7f,  // device scale
518                                         3.2f,  // page cale
519                                         false,
520                                         &result_scale_x,
521                                         &result_scale_y,
522                                         &result_bounds);
523  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
524  EXPECT_FLOAT_EQ(
525      1.3f,
526      pending_layer_->tilings()->tiling_at(0)->contents_scale());
527  EXPECT_FLOAT_EQ(
528      1.3f * low_res_factor,
529      pending_layer_->tilings()->tiling_at(1)->contents_scale());
530
531  // If we change the layer's CSS scale factor, then we should not get new
532  // tilings.
533  pending_layer_->CalculateContentsScale(1.8f,  // ideal contents scale
534                                         1.7f,  // device scale
535                                         3.2f,  // page cale
536                                         false,
537                                         &result_scale_x,
538                                         &result_scale_y,
539                                         &result_bounds);
540  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
541  EXPECT_FLOAT_EQ(
542      1.3f,
543      pending_layer_->tilings()->tiling_at(0)->contents_scale());
544  EXPECT_FLOAT_EQ(
545      1.3f * low_res_factor,
546      pending_layer_->tilings()->tiling_at(1)->contents_scale());
547
548  // If we change the page scale factor, then we should get new tilings.
549  pending_layer_->CalculateContentsScale(1.8f,  // ideal contents scale
550                                         1.7f,  // device scale
551                                         2.2f,  // page cale
552                                         false,
553                                         &result_scale_x,
554                                         &result_scale_y,
555                                         &result_bounds);
556  ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
557  EXPECT_FLOAT_EQ(
558      1.8f,
559      pending_layer_->tilings()->tiling_at(0)->contents_scale());
560  EXPECT_FLOAT_EQ(
561      1.8f * low_res_factor,
562      pending_layer_->tilings()->tiling_at(2)->contents_scale());
563
564  // If we change the device scale factor, then we should get new tilings.
565  pending_layer_->CalculateContentsScale(1.9f,  // ideal contents scale
566                                         1.4f,  // device scale
567                                         2.2f,  // page cale
568                                         false,
569                                         &result_scale_x,
570                                         &result_scale_y,
571                                         &result_bounds);
572  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
573  EXPECT_FLOAT_EQ(
574      1.9f,
575      pending_layer_->tilings()->tiling_at(0)->contents_scale());
576  EXPECT_FLOAT_EQ(
577      1.9f * low_res_factor,
578      pending_layer_->tilings()->tiling_at(3)->contents_scale());
579
580  // If we change the device scale factor, but end up at the same total scale
581  // factor somehow, then we don't get new tilings.
582  pending_layer_->CalculateContentsScale(1.9f,  // ideal contents scale
583                                         2.2f,  // device scale
584                                         1.4f,  // page cale
585                                         false,
586                                         &result_scale_x,
587                                         &result_scale_y,
588                                         &result_bounds);
589  ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
590  EXPECT_FLOAT_EQ(
591      1.9f,
592      pending_layer_->tilings()->tiling_at(0)->contents_scale());
593  EXPECT_FLOAT_EQ(
594      1.9f * low_res_factor,
595      pending_layer_->tilings()->tiling_at(3)->contents_scale());
596}
597
598TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
599  // This test makes sure that if a layer can have tilings, then a commit makes
600  // it not able to have tilings (empty size), and then a future commit that
601  // makes it valid again should be able to create tilings.
602  gfx::Size tile_size(400, 400);
603  gfx::Size layer_bounds(1300, 1900);
604
605  scoped_refptr<FakePicturePileImpl> empty_pile =
606      FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1000, 0));
607  scoped_refptr<FakePicturePileImpl> valid_pile =
608      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
609
610  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
611  EXPECT_LT(low_res_factor, 1.f);
612
613  float high_res_scale = 1.3f;
614  float low_res_scale = high_res_scale * low_res_factor;
615  float device_scale = 1.7f;
616  float page_scale = 3.2f;
617  float result_scale_x, result_scale_y;
618  gfx::Size result_bounds;
619
620  SetupPendingTree(valid_pile);
621  pending_layer_->CalculateContentsScale(high_res_scale,
622                                         device_scale,
623                                         page_scale,
624                                         false,
625                                         &result_scale_x,
626                                         &result_scale_y,
627                                         &result_bounds);
628  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
629  EXPECT_FLOAT_EQ(high_res_scale,
630                  pending_layer_->HighResTiling()->contents_scale());
631  EXPECT_FLOAT_EQ(low_res_scale,
632                  pending_layer_->LowResTiling()->contents_scale());
633
634  ActivateTree();
635  SetupPendingTree(empty_pile);
636  pending_layer_->CalculateContentsScale(high_res_scale,
637                                         device_scale,
638                                         page_scale,
639                                         false,
640                                         &result_scale_x,
641                                         &result_scale_y,
642                                         &result_bounds);
643  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
644  ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
645
646  ActivateTree();
647  active_layer_->CalculateContentsScale(high_res_scale,
648                                        device_scale,
649                                        page_scale,
650                                        false,
651                                        &result_scale_x,
652                                        &result_scale_y,
653                                        &result_bounds);
654  ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
655
656  SetupPendingTree(valid_pile);
657  pending_layer_->CalculateContentsScale(high_res_scale,
658                                         device_scale,
659                                         page_scale,
660                                         false,
661                                         &result_scale_x,
662                                         &result_scale_y,
663                                         &result_bounds);
664  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
665  ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
666  EXPECT_FLOAT_EQ(high_res_scale,
667                  pending_layer_->HighResTiling()->contents_scale());
668  EXPECT_FLOAT_EQ(low_res_scale,
669                  pending_layer_->LowResTiling()->contents_scale());
670}
671
672TEST_F(PictureLayerImplTest, ZoomOutCrash) {
673  gfx::Size tile_size(400, 400);
674  gfx::Size layer_bounds(1300, 1900);
675
676  // Set up the high and low res tilings before pinch zoom.
677  scoped_refptr<FakePicturePileImpl> pending_pile =
678      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
679  scoped_refptr<FakePicturePileImpl> active_pile =
680      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
681
682  SetupTrees(pending_pile, active_pile);
683  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
684  SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, false);
685  host_impl_.PinchGestureBegin();
686  SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, false);
687  SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, false);
688  EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
689}
690
691TEST_F(PictureLayerImplTest, PinchGestureTilings) {
692  gfx::Size tile_size(400, 400);
693  gfx::Size layer_bounds(1300, 1900);
694
695  scoped_refptr<FakePicturePileImpl> pending_pile =
696      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
697  scoped_refptr<FakePicturePileImpl> active_pile =
698      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
699
700  // Set up the high and low res tilings before pinch zoom.
701  SetupTrees(pending_pile, active_pile);
702  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
703  SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, false);
704  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
705  EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
706  EXPECT_FLOAT_EQ(
707      1.0f,
708      active_layer_->tilings()->tiling_at(0)->contents_scale());
709  EXPECT_FLOAT_EQ(
710      1.0f * low_res_factor,
711      active_layer_->tilings()->tiling_at(1)->contents_scale());
712
713  // Start a pinch gesture.
714  host_impl_.PinchGestureBegin();
715
716  // Zoom out by a small amount. We should create a tiling at half
717  // the scale (1/kMaxScaleRatioDuringPinch).
718  SetContentsScaleOnBothLayers(0.90f, 1.0f, 0.9f, false);
719  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
720  EXPECT_FLOAT_EQ(
721      1.0f,
722      active_layer_->tilings()->tiling_at(0)->contents_scale());
723  EXPECT_FLOAT_EQ(
724      0.5f,
725      active_layer_->tilings()->tiling_at(1)->contents_scale());
726  EXPECT_FLOAT_EQ(
727      1.0f * low_res_factor,
728      active_layer_->tilings()->tiling_at(2)->contents_scale());
729
730  // Zoom out further, close to our low-res scale factor. We should
731  // use that tiling as high-res, and not create a new tiling.
732  SetContentsScaleOnBothLayers(low_res_factor, 1.0f, low_res_factor, false);
733  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
734
735  // Zoom in a lot now. Since we increase by increments of
736  // kMaxScaleRatioDuringPinch, this will first use 0.5, then 1.0
737  // and then finally create a new tiling at 2.0.
738  SetContentsScaleOnBothLayers(2.1f, 1.0f, 2.1f, false);
739  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
740  SetContentsScaleOnBothLayers(2.1f, 1.0f, 2.1f, false);
741  EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
742  SetContentsScaleOnBothLayers(2.1f, 1.0f, 2.1f, false);
743  EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
744  EXPECT_FLOAT_EQ(
745      2.0f,
746      active_layer_->tilings()->tiling_at(0)->contents_scale());
747}
748
749TEST_F(PictureLayerImplTest, CleanUpTilings) {
750  gfx::Size tile_size(400, 400);
751  gfx::Size layer_bounds(1300, 1900);
752
753  scoped_refptr<FakePicturePileImpl> pending_pile =
754      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
755  scoped_refptr<FakePicturePileImpl> active_pile =
756      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
757
758  float result_scale_x, result_scale_y;
759  gfx::Size result_bounds;
760  std::vector<PictureLayerTiling*> used_tilings;
761
762  SetupTrees(pending_pile, active_pile);
763  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
764
765  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
766  EXPECT_LT(low_res_factor, 1.f);
767
768  float device_scale = 1.7f;
769  float page_scale = 3.2f;
770
771  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false);
772  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
773
774  // We only have ideal tilings, so they aren't removed.
775  used_tilings.clear();
776  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
777  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
778
779  // Changing the ideal but not creating new tilings.
780  SetContentsScaleOnBothLayers(1.5f, device_scale, page_scale, false);
781  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
782
783  // The tilings are still our target scale, so they aren't removed.
784  used_tilings.clear();
785  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
786  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
787
788  // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
789  page_scale = 1.2f;
790  SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, false);
791  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
792  EXPECT_FLOAT_EQ(
793      1.f,
794      active_layer_->tilings()->tiling_at(1)->contents_scale());
795  EXPECT_FLOAT_EQ(
796      1.f * low_res_factor,
797      active_layer_->tilings()->tiling_at(3)->contents_scale());
798
799  // Mark the non-ideal tilings as used. They won't be removed.
800  used_tilings.clear();
801  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
802  used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
803  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
804  ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
805
806  // Now move the ideal scale to 0.5. Our target stays 1.2.
807  SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, false);
808
809  // The high resolution tiling is between target and ideal, so is not
810  // removed.  The low res tiling for the old ideal=1.0 scale is removed.
811  used_tilings.clear();
812  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
813  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
814
815  // Now move the ideal scale to 1.0. Our target stays 1.2.
816  SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false);
817
818  // All the tilings are between are target and the ideal, so they are not
819  // removed.
820  used_tilings.clear();
821  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
822  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
823
824  // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
825  active_layer_->CalculateContentsScale(1.1f,
826                                        device_scale,
827                                        page_scale,
828                                        false,
829                                        &result_scale_x,
830                                        &result_scale_y,
831                                        &result_bounds);
832
833  // Because the pending layer's ideal scale is still 1.0, our tilings fall
834  // in the range [1.0,1.2] and are kept.
835  used_tilings.clear();
836  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
837  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
838
839  // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
840  // 1.2 still.
841  pending_layer_->CalculateContentsScale(1.1f,
842                                         device_scale,
843                                         page_scale,
844                                         false,
845                                         &result_scale_x,
846                                         &result_scale_y,
847                                         &result_bounds);
848
849  // Our 1.0 tiling now falls outside the range between our ideal scale and our
850  // target raster scale. But it is in our used tilings set, so nothing is
851  // deleted.
852  used_tilings.clear();
853  used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
854  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
855  ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
856
857  // If we remove it from our used tilings set, it is outside the range to keep
858  // so it is deleted.
859  used_tilings.clear();
860  active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
861  ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
862}
863
864#define EXPECT_BOTH_EQ(expression, x)         \
865  do {                                        \
866    EXPECT_EQ(pending_layer_->expression, x); \
867    EXPECT_EQ(active_layer_->expression, x);  \
868  } while (false)
869
870TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
871  // Make sure this layer covers multiple tiles, since otherwise low
872  // res won't get created because it is too small.
873  gfx::Size tile_size(host_impl_.settings().default_tile_size);
874  SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
875  // Avoid max untiled layer size heuristics via fixed tile size.
876  pending_layer_->set_fixed_tile_size(tile_size);
877  active_layer_->set_fixed_tile_size(tile_size);
878
879  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
880  float contents_scale = 1.f;
881  float device_scale = 1.f;
882  float page_scale = 1.f;
883  bool animating_transform = true;
884
885  // Animating, so don't create low res even if there isn't one already.
886  SetContentsScaleOnBothLayers(
887      contents_scale, device_scale, page_scale, animating_transform);
888  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
889  EXPECT_BOTH_EQ(num_tilings(), 1u);
890
891  // Stop animating, low res gets created.
892  animating_transform = false;
893  SetContentsScaleOnBothLayers(
894      contents_scale, device_scale, page_scale, animating_transform);
895  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
896  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
897  EXPECT_BOTH_EQ(num_tilings(), 2u);
898
899  // Page scale animation, new high res, but not new low res because animating.
900  contents_scale = 2.f;
901  page_scale = 2.f;
902  animating_transform = true;
903  SetContentsScaleOnBothLayers(
904      contents_scale, device_scale, page_scale, animating_transform);
905  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
906  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
907  EXPECT_BOTH_EQ(num_tilings(), 3u);
908
909  // Stop animating, new low res gets created for final page scale.
910  animating_transform = false;
911  SetContentsScaleOnBothLayers(
912      contents_scale, device_scale, page_scale, animating_transform);
913  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
914  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
915  EXPECT_BOTH_EQ(num_tilings(), 4u);
916}
917
918TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
919  gfx::Size tile_size(host_impl_.settings().default_tile_size);
920  SetupDefaultTrees(tile_size);
921
922  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
923  float device_scale = 1.f;
924  float page_scale = 1.f;
925  bool animating_transform = false;
926
927  // Contents exactly fit on one tile at scale 1, no low res.
928  float contents_scale = 1.f;
929  SetContentsScaleOnBothLayers(
930      contents_scale, device_scale, page_scale, animating_transform);
931  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
932  EXPECT_BOTH_EQ(num_tilings(), 1u);
933
934  ResetTilingsAndRasterScales();
935
936  // Contents that are smaller than one tile, no low res.
937  contents_scale = 0.123f;
938  SetContentsScaleOnBothLayers(
939      contents_scale, device_scale, page_scale, animating_transform);
940  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
941  EXPECT_BOTH_EQ(num_tilings(), 1u);
942
943  ResetTilingsAndRasterScales();
944
945  // Any content bounds that would create more than one tile will
946  // generate a low res tiling.
947  contents_scale = 2.5f;
948  SetContentsScaleOnBothLayers(
949      contents_scale, device_scale, page_scale, animating_transform);
950  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
951  EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
952                 contents_scale * low_res_factor);
953  EXPECT_BOTH_EQ(num_tilings(), 2u);
954
955  ResetTilingsAndRasterScales();
956
957  // Mask layers dont create low res since they always fit on one tile.
958  pending_layer_->SetIsMask(true);
959  active_layer_->SetIsMask(true);
960  SetContentsScaleOnBothLayers(
961      contents_scale, device_scale, page_scale, animating_transform);
962  EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
963  EXPECT_BOTH_EQ(num_tilings(), 1u);
964}
965
966TEST_F(PictureLayerImplTest, DidLoseOutputSurface) {
967  gfx::Size tile_size(400, 400);
968  gfx::Size layer_bounds(1300, 1900);
969
970  scoped_refptr<FakePicturePileImpl> pending_pile =
971      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
972  scoped_refptr<FakePicturePileImpl> active_pile =
973      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
974
975  float result_scale_x, result_scale_y;
976  gfx::Size result_bounds;
977
978  SetupTrees(pending_pile, active_pile);
979  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
980
981  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
982                                         2.7f,  // device scale
983                                         3.2f,  // page cale
984                                         false,
985                                         &result_scale_x,
986                                         &result_scale_y,
987                                         &result_bounds);
988  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
989
990  // All tilings should be removed when losing output surface.
991  active_layer_->DidLoseOutputSurface();
992  EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
993  pending_layer_->DidLoseOutputSurface();
994  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
995
996  // This should create new tilings.
997  pending_layer_->CalculateContentsScale(1.3f,  // ideal contents scale
998                                         2.7f,  // device scale
999                                         3.2f,  // page cale
1000                                         false,
1001                                         &result_scale_x,
1002                                         &result_scale_y,
1003                                         &result_bounds);
1004  EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1005}
1006
1007TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1008  // The default max tile size is larger than 400x400.
1009  gfx::Size tile_size(400, 400);
1010  gfx::Size layer_bounds(5000, 5000);
1011
1012  scoped_refptr<FakePicturePileImpl> pending_pile =
1013      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1014  scoped_refptr<FakePicturePileImpl> active_pile =
1015      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1016
1017  float result_scale_x, result_scale_y;
1018  gfx::Size result_bounds;
1019
1020  SetupTrees(pending_pile, active_pile);
1021  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1022
1023  pending_layer_->CalculateContentsScale(
1024      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
1025  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1026
1027  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1028
1029  // The default value.
1030  EXPECT_EQ(gfx::Size(256, 256).ToString(),
1031            host_impl_.settings().default_tile_size.ToString());
1032
1033  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1034  EXPECT_EQ(gfx::Size(256, 256).ToString(),
1035            tile->content_rect().size().ToString());
1036
1037  pending_layer_->DidLoseOutputSurface();
1038
1039  // Change the max texture size on the output surface context.
1040  scoped_ptr<TestWebGraphicsContext3D> context =
1041      TestWebGraphicsContext3D::Create();
1042  context->set_max_texture_size(140);
1043  host_impl_.DidLoseOutputSurface();
1044  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1045      context.Pass()).PassAs<OutputSurface>());
1046
1047  pending_layer_->CalculateContentsScale(
1048      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
1049  ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1050
1051  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1052
1053  // Verify the tiles are not larger than the context's max texture size.
1054  tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1055  EXPECT_GE(140, tile->content_rect().width());
1056  EXPECT_GE(140, tile->content_rect().height());
1057}
1058
1059TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1060  // The default max tile size is larger than 400x400.
1061  gfx::Size tile_size(400, 400);
1062  gfx::Size layer_bounds(500, 500);
1063
1064  scoped_refptr<FakePicturePileImpl> pending_pile =
1065      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1066  scoped_refptr<FakePicturePileImpl> active_pile =
1067      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1068
1069  float result_scale_x, result_scale_y;
1070  gfx::Size result_bounds;
1071
1072  SetupTrees(pending_pile, active_pile);
1073  EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1074
1075  pending_layer_->CalculateContentsScale(
1076      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
1077  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1078
1079  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1080
1081  // The default value. The layer is smaller than this.
1082  EXPECT_EQ(gfx::Size(512, 512).ToString(),
1083            host_impl_.settings().max_untiled_layer_size.ToString());
1084
1085  // There should be a single tile since the layer is small.
1086  PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1087  EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1088
1089  pending_layer_->DidLoseOutputSurface();
1090
1091  // Change the max texture size on the output surface context.
1092  scoped_ptr<TestWebGraphicsContext3D> context =
1093      TestWebGraphicsContext3D::Create();
1094  context->set_max_texture_size(140);
1095  host_impl_.DidLoseOutputSurface();
1096  host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1097      context.Pass()).PassAs<OutputSurface>());
1098
1099  pending_layer_->CalculateContentsScale(
1100      1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds);
1101  ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1102
1103  pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1104
1105  // There should be more than one tile since the max texture size won't cover
1106  // the layer.
1107  high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1108  EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1109
1110  // Verify the tiles are not larger than the context's max texture size.
1111  Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1112  EXPECT_GE(140, tile->content_rect().width());
1113  EXPECT_GE(140, tile->content_rect().height());
1114}
1115
1116TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1117  MockQuadCuller quad_culler;
1118
1119  gfx::Size tile_size(400, 400);
1120  gfx::Size layer_bounds(1300, 1900);
1121
1122  scoped_refptr<FakePicturePileImpl> pending_pile =
1123      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1124  scoped_refptr<FakePicturePileImpl> active_pile =
1125      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1126
1127  SetupTrees(pending_pile, active_pile);
1128
1129  active_layer_->SetContentBounds(layer_bounds);
1130  active_layer_->draw_properties().visible_content_rect =
1131      gfx::Rect(layer_bounds);
1132
1133  gfx::Rect layer_invalidation(150, 200, 30, 180);
1134  Region invalidation(layer_invalidation);
1135  AddDefaultTilingsWithInvalidation(invalidation);
1136
1137  AppendQuadsData data;
1138  active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
1139  active_layer_->AppendQuads(&quad_culler, &data);
1140  active_layer_->DidDraw(NULL);
1141
1142  ASSERT_EQ(1U, quad_culler.quad_list().size());
1143  EXPECT_EQ(DrawQuad::PICTURE_CONTENT, quad_culler.quad_list()[0]->material);
1144}
1145
1146TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
1147  gfx::Size tile_size(100, 100);
1148  gfx::Size layer_bounds(1000, 1000);
1149
1150  scoped_refptr<FakePicturePileImpl> pending_pile =
1151      FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1152  // Layers with entirely empty piles can't get tilings.
1153  pending_pile->AddRecordingAt(0, 0);
1154
1155  SetupPendingTree(pending_pile);
1156
1157  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1158  pending_layer_->AddTiling(1.0f);
1159  pending_layer_->AddTiling(2.0f);
1160
1161  // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1162  // on a layer with no recordings.
1163  host_impl_.pending_tree()->UpdateDrawProperties();
1164  pending_layer_->MarkVisibleResourcesAsRequired();
1165}
1166
1167TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1168  gfx::Size tile_size(100, 100);
1169  gfx::Size layer_bounds(200, 100);
1170
1171  scoped_refptr<FakePicturePileImpl> pending_pile =
1172      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1173  SetupPendingTree(pending_pile);
1174
1175  pending_layer_->set_fixed_tile_size(tile_size);
1176  ASSERT_TRUE(pending_layer_->CanHaveTilings());
1177  PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1178  host_impl_.pending_tree()->UpdateDrawProperties();
1179  EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1180
1181  // Fake set priorities.
1182  int tile_count = 0;
1183  for (PictureLayerTiling::CoverageIterator iter(
1184           tiling,
1185           pending_layer_->contents_scale_x(),
1186           gfx::Rect(pending_layer_->visible_content_rect()));
1187       iter;
1188       ++iter) {
1189    if (!*iter)
1190      continue;
1191    Tile* tile = *iter;
1192    TilePriority priority;
1193    priority.resolution = HIGH_RESOLUTION;
1194    if (++tile_count % 2) {
1195      priority.time_to_visible_in_seconds = 0.f;
1196      priority.distance_to_visible_in_pixels = 0.f;
1197    } else {
1198      priority.time_to_visible_in_seconds = 1.f;
1199      priority.distance_to_visible_in_pixels = 1.f;
1200    }
1201    tile->SetPriority(PENDING_TREE, priority);
1202  }
1203
1204  pending_layer_->MarkVisibleResourcesAsRequired();
1205
1206  int num_visible = 0;
1207  int num_offscreen = 0;
1208
1209  for (PictureLayerTiling::CoverageIterator iter(
1210           tiling,
1211           pending_layer_->contents_scale_x(),
1212           gfx::Rect(pending_layer_->visible_content_rect()));
1213       iter;
1214       ++iter) {
1215    if (!*iter)
1216      continue;
1217    const Tile* tile = *iter;
1218    if (tile->priority(PENDING_TREE).distance_to_visible_in_pixels == 0.f) {
1219      EXPECT_TRUE(tile->required_for_activation());
1220      num_visible++;
1221    } else {
1222      EXPECT_FALSE(tile->required_for_activation());
1223      num_offscreen++;
1224    }
1225  }
1226
1227  EXPECT_GT(num_visible, 0);
1228  EXPECT_GT(num_offscreen, 0);
1229}
1230
1231TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1232  gfx::Size tile_size(100, 100);
1233  gfx::Size layer_bounds(400, 400);
1234  scoped_refptr<FakePicturePileImpl> pending_pile =
1235      FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1236
1237  host_impl_.CreatePendingTree();
1238  LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1239
1240  scoped_ptr<FakePictureLayerImpl> pending_layer =
1241      FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1242  pending_layer->SetDrawsContent(true);
1243  pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1244
1245  pending_layer_ = static_cast<FakePictureLayerImpl*>(
1246      host_impl_.pending_tree()->LayerById(id_));
1247
1248  // Set some state on the pending layer, make sure it is not clobbered
1249  // by a sync from the active layer.  This could happen because if the
1250  // pending layer has not been post-commit initialized it will attempt
1251  // to sync from the active layer.
1252  bool default_lcd_text_setting = pending_layer_->is_using_lcd_text();
1253  pending_layer_->force_set_lcd_text(!default_lcd_text_setting);
1254  EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1255
1256  host_impl_.ActivatePendingTree();
1257
1258  active_layer_ = static_cast<FakePictureLayerImpl*>(
1259      host_impl_.active_tree()->LayerById(id_));
1260
1261  EXPECT_EQ(0u, active_layer_->num_tilings());
1262  EXPECT_EQ(!default_lcd_text_setting, active_layer_->is_using_lcd_text());
1263  EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1264}
1265
1266TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
1267  SetupDefaultTrees(gfx::Size(10, 10));
1268  host_impl_.active_tree()->UpdateDrawProperties();
1269  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1270
1271  // Contrived unit test of a real crash. A layer is transparent during a
1272  // context loss, and later becomes opaque, causing active layer SyncTiling to
1273  // be called.
1274  const float tile_scale = 2.f;
1275  active_layer_->DidLoseOutputSurface();
1276  EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(tile_scale));
1277  pending_layer_->AddTiling(2.f);
1278  EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(tile_scale));
1279}
1280
1281TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
1282  // Set up layers with tilings.
1283  SetupDefaultTrees(gfx::Size(10, 10));
1284  SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, false);
1285  pending_layer_->PushPropertiesTo(active_layer_);
1286  EXPECT_TRUE(pending_layer_->DrawsContent());
1287  EXPECT_TRUE(pending_layer_->CanHaveTilings());
1288  EXPECT_GE(pending_layer_->num_tilings(), 0u);
1289  EXPECT_GE(active_layer_->num_tilings(), 0u);
1290
1291  // Set content to false, which should make CanHaveTilings return false.
1292  pending_layer_->SetDrawsContent(false);
1293  EXPECT_FALSE(pending_layer_->DrawsContent());
1294  EXPECT_FALSE(pending_layer_->CanHaveTilings());
1295
1296  // No tilings should be pushed to active layer.
1297  pending_layer_->PushPropertiesTo(active_layer_);
1298  EXPECT_EQ(0u, active_layer_->num_tilings());
1299}
1300
1301class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
1302 public:
1303  DeferredInitPictureLayerImplTest()
1304      : PictureLayerImplTest(ImplSidePaintingSettings()) {}
1305
1306  virtual void InitializeRenderer() OVERRIDE {
1307    host_impl_.InitializeRenderer(FakeOutputSurface::CreateDeferredGL(
1308        scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice))
1309                                      .PassAs<OutputSurface>());
1310  }
1311
1312  virtual void SetUp() OVERRIDE {
1313    PictureLayerImplTest::SetUp();
1314
1315    // Create some default active and pending trees.
1316    gfx::Size tile_size(100, 100);
1317    gfx::Size layer_bounds(400, 400);
1318
1319    scoped_refptr<FakePicturePileImpl> pending_pile =
1320        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1321    scoped_refptr<FakePicturePileImpl> active_pile =
1322        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1323
1324    SetupTrees(pending_pile, active_pile);
1325  }
1326};
1327
1328// This test is really a LayerTreeHostImpl test, in that it makes sure
1329// that trees need update draw properties after deferred initialization.
1330// However, this is also a regression test for PictureLayerImpl in that
1331// not having this update will cause a crash.
1332TEST_F(DeferredInitPictureLayerImplTest,
1333       PreventUpdateTilePrioritiesDuringLostContext) {
1334  host_impl_.pending_tree()->UpdateDrawProperties();
1335  host_impl_.active_tree()->UpdateDrawProperties();
1336  EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
1337  EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1338
1339  FakeOutputSurface* fake_output_surface =
1340      static_cast<FakeOutputSurface*>(host_impl_.output_surface());
1341  ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
1342      TestContextProvider::Create(), NULL));
1343
1344  // These will crash PictureLayerImpl if this is not true.
1345  ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
1346  ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1347  host_impl_.active_tree()->UpdateDrawProperties();
1348}
1349
1350}  // namespace
1351}  // namespace cc
1352