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