c72d19c2943d245c346a10d1bb2326e55153053f
[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 <algorithm>
8 #include <limits>
9 #include <set>
10 #include <utility>
11
12 #include "cc/base/math_util.h"
13 #include "cc/layers/append_quads_data.h"
14 #include "cc/layers/picture_layer.h"
15 #include "cc/quads/draw_quad.h"
16 #include "cc/quads/tile_draw_quad.h"
17 #include "cc/test/begin_frame_args_test.h"
18 #include "cc/test/fake_content_layer_client.h"
19 #include "cc/test/fake_impl_proxy.h"
20 #include "cc/test/fake_layer_tree_host_impl.h"
21 #include "cc/test/fake_output_surface.h"
22 #include "cc/test/fake_picture_layer_impl.h"
23 #include "cc/test/fake_picture_pile_impl.h"
24 #include "cc/test/geometry_test_utils.h"
25 #include "cc/test/impl_side_painting_settings.h"
26 #include "cc/test/layer_test_common.h"
27 #include "cc/test/test_shared_bitmap_manager.h"
28 #include "cc/test/test_web_graphics_context_3d.h"
29 #include "cc/trees/layer_tree_impl.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "ui/gfx/rect_conversions.h"
32 #include "ui/gfx/size_conversions.h"
33
34 namespace cc {
35 namespace {
36
37 class MockCanvas : public SkCanvas {
38  public:
39   explicit MockCanvas(int w, int h) : SkCanvas(w, h) {}
40
41   virtual void drawRect(const SkRect& rect, const SkPaint& paint) OVERRIDE {
42     // Capture calls before SkCanvas quickReject() kicks in.
43     rects_.push_back(rect);
44   }
45
46   std::vector<SkRect> rects_;
47 };
48
49 class NoLowResTilingsSettings : public ImplSidePaintingSettings {};
50
51 class LowResTilingsSettings : public ImplSidePaintingSettings {
52  public:
53   LowResTilingsSettings() { create_low_res_tiling = true; }
54 };
55
56 class PictureLayerImplTest : public testing::Test {
57  public:
58   PictureLayerImplTest()
59       : proxy_(base::MessageLoopProxy::current()),
60         host_impl_(LowResTilingsSettings(), &proxy_, &shared_bitmap_manager_),
61         id_(7),
62         pending_layer_(NULL),
63         old_pending_layer_(NULL),
64         active_layer_(NULL) {}
65
66   explicit PictureLayerImplTest(const LayerTreeSettings& settings)
67       : proxy_(base::MessageLoopProxy::current()),
68         host_impl_(settings, &proxy_, &shared_bitmap_manager_),
69         id_(7) {}
70
71   virtual ~PictureLayerImplTest() {
72   }
73
74   virtual void SetUp() OVERRIDE {
75     InitializeRenderer();
76   }
77
78   virtual void InitializeRenderer() {
79     host_impl_.InitializeRenderer(
80         FakeOutputSurface::Create3d().PassAs<OutputSurface>());
81   }
82
83   void SetupDefaultTrees(const gfx::Size& layer_bounds) {
84     gfx::Size tile_size(100, 100);
85
86     scoped_refptr<FakePicturePileImpl> pending_pile =
87         FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
88     scoped_refptr<FakePicturePileImpl> active_pile =
89         FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
90
91     SetupTrees(pending_pile, active_pile);
92   }
93
94   void ActivateTree() {
95     host_impl_.ActivateSyncTree();
96     CHECK(!host_impl_.pending_tree());
97     CHECK(host_impl_.recycle_tree());
98     old_pending_layer_ = pending_layer_;
99     pending_layer_ = NULL;
100     active_layer_ = static_cast<FakePictureLayerImpl*>(
101         host_impl_.active_tree()->LayerById(id_));
102   }
103
104   void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
105                                           const gfx::Size& tile_size) {
106     SetupDefaultTrees(layer_bounds);
107     pending_layer_->set_fixed_tile_size(tile_size);
108     active_layer_->set_fixed_tile_size(tile_size);
109   }
110
111   void SetupTrees(
112       scoped_refptr<PicturePileImpl> pending_pile,
113       scoped_refptr<PicturePileImpl> active_pile) {
114     SetupPendingTree(active_pile);
115     ActivateTree();
116     SetupPendingTree(pending_pile);
117   }
118
119   void CreateHighLowResAndSetAllTilesVisible() {
120     // Active layer must get updated first so pending layer can share from it.
121     active_layer_->CreateDefaultTilingsAndTiles();
122     active_layer_->SetAllTilesVisible();
123     pending_layer_->CreateDefaultTilingsAndTiles();
124     pending_layer_->SetAllTilesVisible();
125   }
126
127   void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
128     active_layer_->AddTiling(2.3f);
129     active_layer_->AddTiling(1.0f);
130     active_layer_->AddTiling(0.5f);
131     for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
132       active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
133     pending_layer_->set_invalidation(invalidation);
134     for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
135       pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
136   }
137
138   void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
139     host_impl_.CreatePendingTree();
140     host_impl_.pending_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
141     LayerTreeImpl* pending_tree = host_impl_.pending_tree();
142     // Clear recycled tree.
143     pending_tree->DetachLayerTree();
144
145     scoped_ptr<FakePictureLayerImpl> pending_layer =
146         FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
147     pending_layer->SetDrawsContent(true);
148     pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
149
150     pending_layer_ = static_cast<FakePictureLayerImpl*>(
151         host_impl_.pending_tree()->LayerById(id_));
152     pending_layer_->DoPostCommitInitializationIfNeeded();
153   }
154
155   void SetupDrawPropertiesAndUpdateTiles(FakePictureLayerImpl* layer,
156                                          float ideal_contents_scale,
157                                          float device_scale_factor,
158                                          float page_scale_factor,
159                                          float maximum_animation_contents_scale,
160                                          bool animating_transform_to_screen) {
161     layer->draw_properties().ideal_contents_scale = ideal_contents_scale;
162     layer->draw_properties().device_scale_factor = device_scale_factor;
163     layer->draw_properties().page_scale_factor = page_scale_factor;
164     layer->draw_properties().maximum_animation_contents_scale =
165         maximum_animation_contents_scale;
166     layer->draw_properties().screen_space_transform_is_animating =
167         animating_transform_to_screen;
168     layer->UpdateTiles(Occlusion());
169   }
170   static void VerifyAllTilesExistAndHavePile(
171       const PictureLayerTiling* tiling,
172       PicturePileImpl* pile) {
173     for (PictureLayerTiling::CoverageIterator iter(
174              tiling,
175              tiling->contents_scale(),
176              gfx::Rect(tiling->tiling_size()));
177          iter;
178          ++iter) {
179       EXPECT_TRUE(*iter);
180       EXPECT_EQ(pile, iter->picture_pile());
181     }
182   }
183
184   void SetContentsScaleOnBothLayers(float contents_scale,
185                                     float device_scale_factor,
186                                     float page_scale_factor,
187                                     float maximum_animation_contents_scale,
188                                     bool animating_transform) {
189     SetupDrawPropertiesAndUpdateTiles(pending_layer_,
190                                       contents_scale,
191                                       device_scale_factor,
192                                       page_scale_factor,
193                                       maximum_animation_contents_scale,
194                                       animating_transform);
195
196     SetupDrawPropertiesAndUpdateTiles(active_layer_,
197                                       contents_scale,
198                                       device_scale_factor,
199                                       page_scale_factor,
200                                       maximum_animation_contents_scale,
201                                       animating_transform);
202   }
203
204   void ResetTilingsAndRasterScales() {
205     pending_layer_->ReleaseResources();
206     active_layer_->ReleaseResources();
207   }
208
209   void AssertAllTilesRequired(PictureLayerTiling* tiling) {
210     std::vector<Tile*> tiles = tiling->AllTilesForTesting();
211     for (size_t i = 0; i < tiles.size(); ++i)
212       EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
213     EXPECT_GT(tiles.size(), 0u);
214   }
215
216   void AssertNoTilesRequired(PictureLayerTiling* tiling) {
217     std::vector<Tile*> tiles = tiling->AllTilesForTesting();
218     for (size_t i = 0; i < tiles.size(); ++i)
219       EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
220     EXPECT_GT(tiles.size(), 0u);
221   }
222
223  protected:
224   void TestTileGridAlignmentCommon() {
225     // Layer to span 4 raster tiles in x and in y
226     ImplSidePaintingSettings settings;
227     gfx::Size layer_size(
228         settings.default_tile_size.width() * 7 / 2,
229         settings.default_tile_size.height() * 7 / 2);
230
231     scoped_refptr<FakePicturePileImpl> pending_pile =
232         FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
233     scoped_refptr<FakePicturePileImpl> active_pile =
234         FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
235
236     SetupTrees(pending_pile, active_pile);
237
238     SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
239
240     // Add 1x1 rects at the centers of each tile, then re-record pile contents
241     active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
242     std::vector<Tile*> tiles =
243         active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
244     EXPECT_EQ(16u, tiles.size());
245     std::vector<SkRect> rects;
246     std::vector<Tile*>::const_iterator tile_iter;
247     for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
248       gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
249       gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
250       active_pile->add_draw_rect(rect);
251       rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
252     }
253     // Force re-record with newly injected content
254     active_pile->RemoveRecordingAt(0, 0);
255     active_pile->AddRecordingAt(0, 0);
256
257     std::vector<SkRect>::const_iterator rect_iter = rects.begin();
258     for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
259       MockCanvas mock_canvas(1000, 1000);
260       active_pile->RasterDirect(
261           &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
262
263       // This test verifies that when drawing the contents of a specific tile
264       // at content scale 1.0, the playback canvas never receives content from
265       // neighboring tiles which indicates that the tile grid embedded in
266       // SkPicture is perfectly aligned with the compositor's tiles.
267       EXPECT_EQ(1u, mock_canvas.rects_.size());
268       EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]);
269       rect_iter++;
270     }
271   }
272
273   void TestQuadsForSolidColor(bool test_for_solid);
274
275   FakeImplProxy proxy_;
276   TestSharedBitmapManager shared_bitmap_manager_;
277   FakeLayerTreeHostImpl host_impl_;
278   int id_;
279   FakePictureLayerImpl* pending_layer_;
280   FakePictureLayerImpl* old_pending_layer_;
281   FakePictureLayerImpl* active_layer_;
282
283  private:
284   DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
285 };
286
287 TEST_F(PictureLayerImplTest, TileGridAlignment) {
288   host_impl_.SetDeviceScaleFactor(1.f);
289   TestTileGridAlignmentCommon();
290 }
291
292 TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
293   host_impl_.SetDeviceScaleFactor(2.f);
294   TestTileGridAlignmentCommon();
295 }
296
297 TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
298   gfx::Size tile_size(100, 100);
299   gfx::Size layer_bounds(400, 400);
300
301   scoped_refptr<FakePicturePileImpl> pending_pile =
302       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
303   scoped_refptr<FakePicturePileImpl> active_pile =
304       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
305
306   SetupTrees(pending_pile, active_pile);
307
308   Region invalidation;
309   AddDefaultTilingsWithInvalidation(invalidation);
310
311   EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
312             active_layer_->tilings()->num_tilings());
313
314   const PictureLayerTilingSet* tilings = pending_layer_->tilings();
315   EXPECT_GT(tilings->num_tilings(), 0u);
316   for (size_t i = 0; i < tilings->num_tilings(); ++i)
317     VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
318 }
319
320 TEST_F(PictureLayerImplTest, ExternalViewportRectForPrioritizingTiles) {
321   base::TimeTicks time_ticks;
322   time_ticks += base::TimeDelta::FromMilliseconds(1);
323   host_impl_.SetCurrentBeginFrameArgs(
324       CreateBeginFrameArgsForTesting(time_ticks));
325   gfx::Size tile_size(100, 100);
326   gfx::Size layer_bounds(400, 400);
327
328   scoped_refptr<FakePicturePileImpl> pending_pile =
329       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
330   scoped_refptr<FakePicturePileImpl> active_pile =
331       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
332
333   SetupTrees(pending_pile, active_pile);
334
335   Region invalidation;
336   AddDefaultTilingsWithInvalidation(invalidation);
337   SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
338
339   time_ticks += base::TimeDelta::FromMilliseconds(200);
340   host_impl_.SetCurrentBeginFrameArgs(
341       CreateBeginFrameArgsForTesting(time_ticks));
342
343   // Update tiles with viewport for tile priority as (0, 0, 100, 100) and the
344   // identify transform for tile priority.
345   bool resourceless_software_draw = false;
346   gfx::Rect viewport = gfx::Rect(layer_bounds),
347             viewport_rect_for_tile_priority = gfx::Rect(0, 0, 100, 100);
348   gfx::Transform transform, transform_for_tile_priority;
349
350   host_impl_.SetExternalDrawConstraints(transform,
351                                         viewport,
352                                         viewport,
353                                         viewport_rect_for_tile_priority,
354                                         transform_for_tile_priority,
355                                         resourceless_software_draw);
356   active_layer_->draw_properties().visible_content_rect = viewport;
357   active_layer_->draw_properties().screen_space_transform = transform;
358   active_layer_->UpdateTiles(Occlusion());
359
360   gfx::Rect viewport_rect_for_tile_priority_in_view_space =
361       viewport_rect_for_tile_priority;
362
363   // Verify the viewport rect for tile priority is used in picture layer impl.
364   EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
365             viewport_rect_for_tile_priority_in_view_space);
366
367   // Verify the viewport rect for tile priority is used in picture layer tiling.
368   PictureLayerTilingSet* tilings = active_layer_->tilings();
369   for (size_t i = 0; i < tilings->num_tilings(); i++) {
370     PictureLayerTiling* tiling = tilings->tiling_at(i);
371     EXPECT_EQ(
372         tiling->GetCurrentVisibleRectForTesting(),
373         gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
374                                   tiling->contents_scale()));
375   }
376
377   // Update tiles with viewport for tile priority as (200, 200, 100, 100) in
378   // screen space and the transform for tile priority is translated and
379   // rotated. The actual viewport for tile priority used by PictureLayerImpl
380   // should be (200, 200, 100, 100) applied with the said transform.
381   time_ticks += base::TimeDelta::FromMilliseconds(200);
382   host_impl_.SetCurrentBeginFrameArgs(
383       CreateBeginFrameArgsForTesting(time_ticks));
384
385   viewport_rect_for_tile_priority = gfx::Rect(200, 200, 100, 100);
386   transform_for_tile_priority.Translate(100, 100);
387   transform_for_tile_priority.Rotate(45);
388   host_impl_.SetExternalDrawConstraints(transform,
389                                         viewport,
390                                         viewport,
391                                         viewport_rect_for_tile_priority,
392                                         transform_for_tile_priority,
393                                         resourceless_software_draw);
394   active_layer_->draw_properties().visible_content_rect = viewport;
395   active_layer_->draw_properties().screen_space_transform = transform;
396   active_layer_->UpdateTiles(Occlusion());
397
398   gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
399   bool success = transform_for_tile_priority.GetInverse(&screen_to_view);
400   EXPECT_TRUE(success);
401
402   // Note that we don't clip this to the layer bounds, since it is expected that
403   // the rect will sometimes be outside of the layer bounds. If we clip to
404   // bounds, then tile priorities will end up being incorrect in cases of fully
405   // offscreen layer.
406   viewport_rect_for_tile_priority_in_view_space =
407       gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
408           screen_to_view, viewport_rect_for_tile_priority));
409
410   // Verify the viewport rect for tile priority is used in PictureLayerImpl.
411   EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
412             viewport_rect_for_tile_priority_in_view_space);
413
414   tilings = active_layer_->tilings();
415   for (size_t i = 0; i < tilings->num_tilings(); i++) {
416     PictureLayerTiling* tiling = tilings->tiling_at(i);
417     EXPECT_EQ(
418         tiling->GetCurrentVisibleRectForTesting(),
419         gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
420                                   tiling->contents_scale()));
421   }
422 }
423
424 TEST_F(PictureLayerImplTest,
425        ResourcelessSoftwareDrawHasValidViewportForTilePriority) {
426   base::TimeTicks time_ticks;
427   time_ticks += base::TimeDelta::FromMilliseconds(1);
428   host_impl_.SetCurrentBeginFrameArgs(
429       CreateBeginFrameArgsForTesting(time_ticks));
430
431   gfx::Size tile_size(100, 100);
432   gfx::Size layer_bounds(400, 400);
433
434   scoped_refptr<FakePicturePileImpl> pending_pile =
435       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
436   scoped_refptr<FakePicturePileImpl> active_pile =
437       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
438
439   SetupTrees(pending_pile, active_pile);
440
441   Region invalidation;
442   AddDefaultTilingsWithInvalidation(invalidation);
443   SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
444
445   // UpdateTiles with valid viewport. Should update tile viewport.
446   bool resourceless_software_draw = false;
447   gfx::Rect viewport = gfx::Rect(layer_bounds);
448   gfx::Transform transform;
449   host_impl_.SetExternalDrawConstraints(transform,
450                                         viewport,
451                                         viewport,
452                                         viewport,
453                                         transform,
454                                         resourceless_software_draw);
455   active_layer_->draw_properties().visible_content_rect = viewport;
456   active_layer_->draw_properties().screen_space_transform = transform;
457   active_layer_->UpdateTiles(Occlusion());
458
459   gfx::Rect visible_rect_for_tile_priority =
460       active_layer_->visible_rect_for_tile_priority();
461   EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
462   gfx::Rect viewport_rect_for_tile_priority =
463       active_layer_->viewport_rect_for_tile_priority();
464   EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
465   gfx::Transform screen_space_transform_for_tile_priority =
466       active_layer_->screen_space_transform_for_tile_priority();
467
468   // PictureLayerImpl does not make a special case for
469   // resource_less_software_draw, so the tile viewport and matrix should be
470   // respected.
471   time_ticks += base::TimeDelta::FromMilliseconds(200);
472   host_impl_.SetCurrentBeginFrameArgs(
473       CreateBeginFrameArgsForTesting(time_ticks));
474   resourceless_software_draw = true;
475   viewport = gfx::ScaleToEnclosingRect(viewport, 2);
476   transform.Translate(1.f, 1.f);
477   active_layer_->draw_properties().visible_content_rect = viewport;
478   active_layer_->draw_properties().screen_space_transform = transform;
479   host_impl_.SetExternalDrawConstraints(transform,
480                                         viewport,
481                                         viewport,
482                                         viewport,
483                                         transform,
484                                         resourceless_software_draw);
485   active_layer_->UpdateTiles(Occlusion());
486
487   visible_rect_for_tile_priority =
488       gfx::ScaleToEnclosingRect(visible_rect_for_tile_priority, 2);
489   viewport_rect_for_tile_priority =
490       gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority, 2);
491   screen_space_transform_for_tile_priority = transform;
492   EXPECT_RECT_EQ(visible_rect_for_tile_priority,
493                  active_layer_->visible_rect_for_tile_priority());
494   EXPECT_RECT_EQ(viewport_rect_for_tile_priority,
495                  active_layer_->viewport_rect_for_tile_priority());
496   EXPECT_TRANSFORMATION_MATRIX_EQ(
497       screen_space_transform_for_tile_priority,
498       active_layer_->screen_space_transform_for_tile_priority());
499 }
500
501 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
502   gfx::Size tile_size(100, 100);
503   gfx::Size layer_bounds(400, 400);
504   gfx::Rect layer_invalidation(150, 200, 30, 180);
505
506   scoped_refptr<FakePicturePileImpl> pending_pile =
507       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
508   scoped_refptr<FakePicturePileImpl> active_pile =
509       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
510
511   SetupTrees(pending_pile, active_pile);
512
513   Region invalidation(layer_invalidation);
514   AddDefaultTilingsWithInvalidation(invalidation);
515
516   const PictureLayerTilingSet* tilings = pending_layer_->tilings();
517   EXPECT_GT(tilings->num_tilings(), 0u);
518   for (size_t i = 0; i < tilings->num_tilings(); ++i) {
519     const PictureLayerTiling* tiling = tilings->tiling_at(i);
520     gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
521         layer_invalidation,
522         tiling->contents_scale());
523     for (PictureLayerTiling::CoverageIterator iter(
524              tiling,
525              tiling->contents_scale(),
526              gfx::Rect(tiling->tiling_size()));
527          iter;
528          ++iter) {
529       EXPECT_TRUE(*iter);
530       EXPECT_FALSE(iter.geometry_rect().IsEmpty());
531       if (iter.geometry_rect().Intersects(content_invalidation))
532         EXPECT_EQ(pending_pile.get(), iter->picture_pile());
533       else
534         EXPECT_EQ(active_pile.get(), iter->picture_pile());
535     }
536   }
537 }
538
539 TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
540   gfx::Size tile_size(90, 80);
541   gfx::Size layer_bounds(300, 500);
542
543   scoped_refptr<FakePicturePileImpl> pending_pile =
544       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
545   scoped_refptr<FakePicturePileImpl> active_pile =
546       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
547
548   SetupTrees(pending_pile, active_pile);
549
550   Region invalidation((gfx::Rect(layer_bounds)));
551   AddDefaultTilingsWithInvalidation(invalidation);
552
553   EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
554             active_layer_->tilings()->num_tilings());
555
556   const PictureLayerTilingSet* tilings = pending_layer_->tilings();
557   EXPECT_GT(tilings->num_tilings(), 0u);
558   for (size_t i = 0; i < tilings->num_tilings(); ++i)
559     VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
560 }
561
562 TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
563   gfx::Size tile_size(90, 80);
564   gfx::Size active_layer_bounds(300, 500);
565   gfx::Size pending_layer_bounds(400, 800);
566
567   scoped_refptr<FakePicturePileImpl> pending_pile =
568       FakePicturePileImpl::CreateFilledPile(tile_size,
569                                                 pending_layer_bounds);
570   scoped_refptr<FakePicturePileImpl> active_pile =
571       FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
572
573   SetupTrees(pending_pile, active_pile);
574   pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
575
576   Region invalidation;
577   AddDefaultTilingsWithInvalidation(invalidation);
578
579   const PictureLayerTilingSet* tilings = pending_layer_->tilings();
580   EXPECT_GT(tilings->num_tilings(), 0u);
581   for (size_t i = 0; i < tilings->num_tilings(); ++i) {
582     const PictureLayerTiling* tiling = tilings->tiling_at(i);
583     gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
584         gfx::Rect(active_layer_bounds),
585         tiling->contents_scale());
586     for (PictureLayerTiling::CoverageIterator iter(
587              tiling,
588              tiling->contents_scale(),
589              gfx::Rect(tiling->tiling_size()));
590          iter;
591          ++iter) {
592       EXPECT_TRUE(*iter);
593       EXPECT_FALSE(iter.geometry_rect().IsEmpty());
594       std::vector<Tile*> active_tiles =
595           active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
596       std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
597       if (iter.geometry_rect().right() >= active_content_bounds.width() ||
598           iter.geometry_rect().bottom() >= active_content_bounds.height() ||
599           active_tiles[0]->content_rect().size() !=
600               pending_tiles[0]->content_rect().size()) {
601         EXPECT_EQ(pending_pile.get(), iter->picture_pile());
602       } else {
603         EXPECT_EQ(active_pile.get(), iter->picture_pile());
604       }
605     }
606   }
607 }
608
609 TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
610   gfx::Size tile_size(400, 400);
611   gfx::Size layer_bounds(1300, 1900);
612
613   scoped_refptr<FakePicturePileImpl> pending_pile =
614       FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
615   scoped_refptr<FakePicturePileImpl> active_pile =
616       FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
617
618   // Fill in some of active pile, but more of pending pile.
619   int hole_count = 0;
620   for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
621     for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
622       if ((x + y) % 2) {
623         pending_pile->AddRecordingAt(x, y);
624         active_pile->AddRecordingAt(x, y);
625       } else {
626         hole_count++;
627         if (hole_count % 2)
628           pending_pile->AddRecordingAt(x, y);
629       }
630     }
631   }
632
633   SetupTrees(pending_pile, active_pile);
634   Region invalidation;
635   AddDefaultTilingsWithInvalidation(invalidation);
636
637   const PictureLayerTilingSet* tilings = pending_layer_->tilings();
638   EXPECT_GT(tilings->num_tilings(), 0u);
639   for (size_t i = 0; i < tilings->num_tilings(); ++i) {
640     const PictureLayerTiling* tiling = tilings->tiling_at(i);
641
642     for (PictureLayerTiling::CoverageIterator iter(
643              tiling,
644              tiling->contents_scale(),
645              gfx::Rect(tiling->tiling_size()));
646          iter;
647          ++iter) {
648       EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
649       // Ensure there is a recording for this tile.
650       bool in_pending = pending_pile->CanRaster(tiling->contents_scale(),
651                                                 iter.full_tile_geometry_rect());
652       bool in_active = active_pile->CanRaster(tiling->contents_scale(),
653                                               iter.full_tile_geometry_rect());
654
655       if (in_pending && !in_active)
656         EXPECT_EQ(pending_pile.get(), iter->picture_pile());
657       else if (in_active)
658         EXPECT_EQ(active_pile.get(), iter->picture_pile());
659       else
660         EXPECT_FALSE(*iter);
661     }
662   }
663 }
664
665 TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
666   gfx::Size tile_size(400, 400);
667   gfx::Size layer_bounds(1300, 1900);
668
669   scoped_refptr<FakePicturePileImpl> pending_pile =
670       FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
671   scoped_refptr<FakePicturePileImpl> active_pile =
672       FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
673
674   SetupTrees(pending_pile, active_pile);
675
676   SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
677
678   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
679 }
680
681 TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
682   gfx::Size tile_size(400, 400);
683   gfx::Size layer_bounds(1300, 1900);
684
685   scoped_refptr<FakePicturePileImpl> pending_pile =
686       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
687   scoped_refptr<FakePicturePileImpl> active_pile =
688       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
689
690   SetupTrees(pending_pile, active_pile);
691   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
692
693   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
694   EXPECT_LT(low_res_factor, 1.f);
695
696   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
697                                     6.f,  // ideal contents scale
698                                     3.f,  // device scale
699                                     2.f,  // page scale
700                                     1.f,  // maximum animation scale
701                                     false);
702   ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
703   EXPECT_FLOAT_EQ(6.f,
704                   pending_layer_->tilings()->tiling_at(0)->contents_scale());
705   EXPECT_FLOAT_EQ(6.f * low_res_factor,
706                   pending_layer_->tilings()->tiling_at(1)->contents_scale());
707
708   // If we change the page scale factor, then we should get new tilings.
709   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
710                                     6.6f,  // ideal contents scale
711                                     3.f,   // device scale
712                                     2.2f,  // page scale
713                                     1.f,   // maximum animation scale
714                                     false);
715   ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
716   EXPECT_FLOAT_EQ(6.6f,
717                   pending_layer_->tilings()->tiling_at(0)->contents_scale());
718   EXPECT_FLOAT_EQ(6.6f * low_res_factor,
719                   pending_layer_->tilings()->tiling_at(2)->contents_scale());
720
721   // If we change the device scale factor, then we should get new tilings.
722   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
723                                     7.26f,  // ideal contents scale
724                                     3.3f,   // device scale
725                                     2.2f,   // page scale
726                                     1.f,    // maximum animation scale
727                                     false);
728   ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
729   EXPECT_FLOAT_EQ(7.26f,
730                   pending_layer_->tilings()->tiling_at(0)->contents_scale());
731   EXPECT_FLOAT_EQ(7.26f * low_res_factor,
732                   pending_layer_->tilings()->tiling_at(3)->contents_scale());
733
734   // If we change the device scale factor, but end up at the same total scale
735   // factor somehow, then we don't get new tilings.
736   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
737                                     7.26f,  // ideal contents scale
738                                     2.2f,   // device scale
739                                     3.3f,   // page scale
740                                     1.f,    // maximum animation scale
741                                     false);
742   ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
743   EXPECT_FLOAT_EQ(7.26f,
744                   pending_layer_->tilings()->tiling_at(0)->contents_scale());
745   EXPECT_FLOAT_EQ(7.26f * low_res_factor,
746                   pending_layer_->tilings()->tiling_at(3)->contents_scale());
747 }
748
749 TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
750   // This test makes sure that if a layer can have tilings, then a commit makes
751   // it not able to have tilings (empty size), and then a future commit that
752   // makes it valid again should be able to create tilings.
753   gfx::Size tile_size(400, 400);
754   gfx::Size layer_bounds(1300, 1900);
755
756   scoped_refptr<FakePicturePileImpl> empty_pile =
757       FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
758   scoped_refptr<FakePicturePileImpl> valid_pile =
759       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
760
761   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
762   EXPECT_LT(low_res_factor, 1.f);
763
764   float high_res_scale = 1.3f;
765   float low_res_scale = high_res_scale * low_res_factor;
766   float device_scale = 1.7f;
767   float page_scale = 3.2f;
768   float maximum_animation_scale = 1.f;
769
770   SetupPendingTree(valid_pile);
771   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
772                                     high_res_scale,
773                                     device_scale,
774                                     page_scale,
775                                     maximum_animation_scale,
776                                     false);
777   ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
778   EXPECT_FLOAT_EQ(high_res_scale,
779                   pending_layer_->HighResTiling()->contents_scale());
780   EXPECT_FLOAT_EQ(low_res_scale,
781                   pending_layer_->LowResTiling()->contents_scale());
782
783   ActivateTree();
784   SetupPendingTree(empty_pile);
785   EXPECT_FALSE(pending_layer_->CanHaveTilings());
786   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
787                                     high_res_scale,
788                                     device_scale,
789                                     page_scale,
790                                     maximum_animation_scale,
791                                     false);
792   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
793   ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
794
795   ActivateTree();
796   EXPECT_FALSE(active_layer_->CanHaveTilings());
797   SetupDrawPropertiesAndUpdateTiles(active_layer_,
798                                     high_res_scale,
799                                     device_scale,
800                                     page_scale,
801                                     maximum_animation_scale,
802                                     false);
803   ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
804
805   SetupPendingTree(valid_pile);
806   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
807                                     high_res_scale,
808                                     device_scale,
809                                     page_scale,
810                                     maximum_animation_scale,
811                                     false);
812   ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
813   ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
814   EXPECT_FLOAT_EQ(high_res_scale,
815                   pending_layer_->HighResTiling()->contents_scale());
816   EXPECT_FLOAT_EQ(low_res_scale,
817                   pending_layer_->LowResTiling()->contents_scale());
818 }
819
820 TEST_F(PictureLayerImplTest, ZoomOutCrash) {
821   gfx::Size tile_size(400, 400);
822   gfx::Size layer_bounds(1300, 1900);
823
824   // Set up the high and low res tilings before pinch zoom.
825   scoped_refptr<FakePicturePileImpl> pending_pile =
826       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
827   scoped_refptr<FakePicturePileImpl> active_pile =
828       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
829
830   SetupTrees(pending_pile, active_pile);
831   EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
832   SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, false);
833   host_impl_.PinchGestureBegin();
834   SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
835   SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
836   EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
837 }
838
839 TEST_F(PictureLayerImplTest, PinchGestureTilings) {
840   gfx::Size tile_size(400, 400);
841   gfx::Size layer_bounds(1300, 1900);
842
843   scoped_refptr<FakePicturePileImpl> pending_pile =
844       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
845   scoped_refptr<FakePicturePileImpl> active_pile =
846       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
847
848   // Set up the high and low res tilings before pinch zoom.
849   SetupTrees(pending_pile, active_pile);
850   EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
851   SetContentsScaleOnBothLayers(2.0f, 1.0f, 1.0f, 1.0f, false);
852   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
853   EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
854   EXPECT_FLOAT_EQ(2.0f,
855                   active_layer_->tilings()->tiling_at(0)->contents_scale());
856   EXPECT_FLOAT_EQ(2.0f * low_res_factor,
857                   active_layer_->tilings()->tiling_at(1)->contents_scale());
858
859   // Start a pinch gesture.
860   host_impl_.PinchGestureBegin();
861
862   // Zoom out by a small amount. We should create a tiling at half
863   // the scale (2/kMaxScaleRatioDuringPinch).
864   SetContentsScaleOnBothLayers(1.8f, 1.0f, 0.9f, 1.0f, false);
865   EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
866   EXPECT_FLOAT_EQ(2.0f,
867                   active_layer_->tilings()->tiling_at(0)->contents_scale());
868   EXPECT_FLOAT_EQ(1.0f,
869                   active_layer_->tilings()->tiling_at(1)->contents_scale());
870   EXPECT_FLOAT_EQ(2.0f * low_res_factor,
871                   active_layer_->tilings()->tiling_at(2)->contents_scale());
872
873   // Zoom out further, close to our low-res scale factor. We should
874   // use that tiling as high-res, and not create a new tiling.
875   SetContentsScaleOnBothLayers(
876       low_res_factor, 1.0f, low_res_factor / 2.0f, 1.0f, false);
877   EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
878
879   // Zoom in a lot now. Since we increase by increments of
880   // kMaxScaleRatioDuringPinch, this will first use 1.0, then 2.0
881   // and then finally create a new tiling at 4.0.
882   SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
883   EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
884   SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
885   EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
886   SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
887   EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
888   EXPECT_FLOAT_EQ(4.0f,
889                   active_layer_->tilings()->tiling_at(0)->contents_scale());
890 }
891
892 TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
893   gfx::Size tile_size(300, 300);
894   gfx::Size layer_bounds(2600, 3800);
895
896   scoped_refptr<FakePicturePileImpl> pending_pile =
897       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
898   scoped_refptr<FakePicturePileImpl> active_pile =
899       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
900
901   // Set up the high and low res tilings before pinch zoom.
902   SetupTrees(pending_pile, active_pile);
903   EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
904   SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f, 1.0f, false);
905   EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
906   EXPECT_FLOAT_EQ(0.24f,
907                   active_layer_->tilings()->tiling_at(0)->contents_scale());
908   EXPECT_FLOAT_EQ(0.0625f,
909                   active_layer_->tilings()->tiling_at(1)->contents_scale());
910
911   // Start a pinch gesture.
912   host_impl_.PinchGestureBegin();
913
914   // Zoom out by a small amount. We should create a tiling at half
915   // the scale (1/kMaxScaleRatioDuringPinch).
916   SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f, 1.0f, false);
917   EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
918   EXPECT_FLOAT_EQ(0.24f,
919                   active_layer_->tilings()->tiling_at(0)->contents_scale());
920   EXPECT_FLOAT_EQ(0.12f,
921                   active_layer_->tilings()->tiling_at(1)->contents_scale());
922   EXPECT_FLOAT_EQ(0.0625,
923                   active_layer_->tilings()->tiling_at(2)->contents_scale());
924
925   // Zoom out further, close to our low-res scale factor. We should
926   // use that tiling as high-res, and not create a new tiling.
927   SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f, 1.0f, false);
928   EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
929
930   // Zoom in. 0.125(desired_scale) should be snapped to 0.12 during zoom-in
931   // because 0.125(desired_scale) is within the ratio(1.2)
932   SetContentsScaleOnBothLayers(0.5f, 1.0f, 0.5f, 1.0f, false);
933   EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
934 }
935
936 TEST_F(PictureLayerImplTest, CleanUpTilings) {
937   gfx::Size tile_size(400, 400);
938   gfx::Size layer_bounds(1300, 1900);
939
940   scoped_refptr<FakePicturePileImpl> pending_pile =
941       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
942   scoped_refptr<FakePicturePileImpl> active_pile =
943       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
944
945   std::vector<PictureLayerTiling*> used_tilings;
946
947   SetupTrees(pending_pile, active_pile);
948   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
949
950   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
951   EXPECT_LT(low_res_factor, 1.f);
952
953   float device_scale = 1.7f;
954   float page_scale = 3.2f;
955   float scale = 1.f;
956
957   SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
958   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
959
960   // We only have ideal tilings, so they aren't removed.
961   used_tilings.clear();
962   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
963   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
964
965   host_impl_.PinchGestureBegin();
966
967   // Changing the ideal but not creating new tilings.
968   scale *= 1.5f;
969   page_scale *= 1.5f;
970   SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
971   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
972
973   // The tilings are still our target scale, so they aren't removed.
974   used_tilings.clear();
975   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
976   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
977
978   host_impl_.PinchGestureEnd();
979
980   // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
981   scale /= 4.f;
982   page_scale /= 4.f;
983   SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
984   ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
985   EXPECT_FLOAT_EQ(
986       1.f,
987       active_layer_->tilings()->tiling_at(1)->contents_scale());
988   EXPECT_FLOAT_EQ(
989       1.f * low_res_factor,
990       active_layer_->tilings()->tiling_at(3)->contents_scale());
991
992   // Mark the non-ideal tilings as used. They won't be removed.
993   used_tilings.clear();
994   used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
995   used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
996   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
997   ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
998
999   // Now move the ideal scale to 0.5. Our target stays 1.2.
1000   SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
1001
1002   // The high resolution tiling is between target and ideal, so is not
1003   // removed.  The low res tiling for the old ideal=1.0 scale is removed.
1004   used_tilings.clear();
1005   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1006   ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1007
1008   // Now move the ideal scale to 1.0. Our target stays 1.2.
1009   SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
1010
1011   // All the tilings are between are target and the ideal, so they are not
1012   // removed.
1013   used_tilings.clear();
1014   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1015   ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1016
1017   // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
1018   SetupDrawPropertiesAndUpdateTiles(
1019       active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1020
1021   // Because the pending layer's ideal scale is still 1.0, our tilings fall
1022   // in the range [1.0,1.2] and are kept.
1023   used_tilings.clear();
1024   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1025   ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1026
1027   // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
1028   // 1.2 still.
1029   SetupDrawPropertiesAndUpdateTiles(
1030       pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1031
1032   // Our 1.0 tiling now falls outside the range between our ideal scale and our
1033   // target raster scale. But it is in our used tilings set, so nothing is
1034   // deleted.
1035   used_tilings.clear();
1036   used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1037   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1038   ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1039
1040   // If we remove it from our used tilings set, it is outside the range to keep
1041   // so it is deleted.
1042   used_tilings.clear();
1043   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1044   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1045 }
1046
1047 #define EXPECT_BOTH_EQ(expression, x)         \
1048   do {                                        \
1049     EXPECT_EQ(x, pending_layer_->expression); \
1050     EXPECT_EQ(x, active_layer_->expression);  \
1051   } while (false)
1052
1053 #define EXPECT_BOTH_NE(expression, x)         \
1054   do {                                        \
1055     EXPECT_NE(x, pending_layer_->expression); \
1056     EXPECT_NE(x, active_layer_->expression);  \
1057   } while (false)
1058
1059 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
1060   // Make sure this layer covers multiple tiles, since otherwise low
1061   // res won't get created because it is too small.
1062   gfx::Size tile_size(host_impl_.settings().default_tile_size);
1063   SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
1064   // Avoid max untiled layer size heuristics via fixed tile size.
1065   pending_layer_->set_fixed_tile_size(tile_size);
1066   active_layer_->set_fixed_tile_size(tile_size);
1067
1068   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1069   float contents_scale = 1.f;
1070   float device_scale = 1.f;
1071   float page_scale = 1.f;
1072   float maximum_animation_scale = 1.f;
1073   bool animating_transform = true;
1074
1075   // Animating, so don't create low res even if there isn't one already.
1076   SetContentsScaleOnBothLayers(contents_scale,
1077                                device_scale,
1078                                page_scale,
1079                                maximum_animation_scale,
1080                                animating_transform);
1081   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1082   EXPECT_BOTH_EQ(num_tilings(), 1u);
1083
1084   // Stop animating, low res gets created.
1085   animating_transform = false;
1086   SetContentsScaleOnBothLayers(contents_scale,
1087                                device_scale,
1088                                page_scale,
1089                                maximum_animation_scale,
1090                                animating_transform);
1091   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1092   EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1093   EXPECT_BOTH_EQ(num_tilings(), 2u);
1094
1095   // Page scale animation, new high res, but not new low res because animating.
1096   contents_scale = 2.f;
1097   page_scale = 2.f;
1098   animating_transform = true;
1099   SetContentsScaleOnBothLayers(contents_scale,
1100                                device_scale,
1101                                page_scale,
1102                                maximum_animation_scale,
1103                                animating_transform);
1104   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1105   EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1106   EXPECT_BOTH_EQ(num_tilings(), 3u);
1107
1108   // Stop animating, new low res gets created for final page scale.
1109   animating_transform = false;
1110   SetContentsScaleOnBothLayers(contents_scale,
1111                                device_scale,
1112                                page_scale,
1113                                maximum_animation_scale,
1114                                animating_transform);
1115   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1116   EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1117   EXPECT_BOTH_EQ(num_tilings(), 4u);
1118 }
1119
1120 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1121   gfx::Size tile_size(host_impl_.settings().default_tile_size);
1122   SetupDefaultTrees(tile_size);
1123
1124   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1125   float device_scale = 1.f;
1126   float page_scale = 1.f;
1127   float maximum_animation_scale = 1.f;
1128   bool animating_transform = false;
1129
1130   // Contents exactly fit on one tile at scale 1, no low res.
1131   float contents_scale = 1.f;
1132   SetContentsScaleOnBothLayers(contents_scale,
1133                                device_scale,
1134                                page_scale,
1135                                maximum_animation_scale,
1136                                animating_transform);
1137   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1138   EXPECT_BOTH_EQ(num_tilings(), 1u);
1139
1140   ResetTilingsAndRasterScales();
1141
1142   // Contents that are smaller than one tile, no low res.
1143   contents_scale = 0.123f;
1144   SetContentsScaleOnBothLayers(contents_scale,
1145                                device_scale,
1146                                page_scale,
1147                                maximum_animation_scale,
1148                                animating_transform);
1149   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1150   EXPECT_BOTH_EQ(num_tilings(), 1u);
1151
1152   ResetTilingsAndRasterScales();
1153
1154   // Any content bounds that would create more than one tile will
1155   // generate a low res tiling.
1156   contents_scale = 2.5f;
1157   SetContentsScaleOnBothLayers(contents_scale,
1158                                device_scale,
1159                                page_scale,
1160                                maximum_animation_scale,
1161                                animating_transform);
1162   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1163   EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1164                  contents_scale * low_res_factor);
1165   EXPECT_BOTH_EQ(num_tilings(), 2u);
1166
1167   ResetTilingsAndRasterScales();
1168
1169   // Mask layers dont create low res since they always fit on one tile.
1170   pending_layer_->pile()->set_is_mask(true);
1171   active_layer_->pile()->set_is_mask(true);
1172   SetContentsScaleOnBothLayers(contents_scale,
1173                                device_scale,
1174                                page_scale,
1175                                maximum_animation_scale,
1176                                animating_transform);
1177   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1178   EXPECT_BOTH_EQ(num_tilings(), 1u);
1179 }
1180
1181 TEST_F(PictureLayerImplTest, HugeMasksDontGetTiles) {
1182   gfx::Size tile_size(100, 100);
1183
1184   scoped_refptr<FakePicturePileImpl> valid_pile =
1185       FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1000, 1000));
1186   valid_pile->set_is_mask(true);
1187   SetupPendingTree(valid_pile);
1188
1189   SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1190   EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1191   EXPECT_EQ(1u, pending_layer_->num_tilings());
1192
1193   pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1194   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1195       pending_layer_->HighResTiling()->AllTilesForTesting());
1196
1197   ActivateTree();
1198
1199   // Mask layers have a tiling with a single tile in it.
1200   EXPECT_EQ(1u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1201   // The mask resource exists.
1202   EXPECT_NE(0u, active_layer_->ContentsResourceId());
1203
1204   // Resize larger than the max texture size.
1205   int max_texture_size = host_impl_.GetRendererCapabilities().max_texture_size;
1206   scoped_refptr<FakePicturePileImpl> huge_pile =
1207       FakePicturePileImpl::CreateFilledPile(
1208           tile_size, gfx::Size(max_texture_size + 1, 10));
1209   huge_pile->set_is_mask(true);
1210   SetupPendingTree(huge_pile);
1211
1212   SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1213   EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1214   EXPECT_EQ(1u, pending_layer_->num_tilings());
1215
1216   pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1217   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1218       pending_layer_->HighResTiling()->AllTilesForTesting());
1219
1220   ActivateTree();
1221
1222   // Mask layers have a tiling, but there should be no tiles in it.
1223   EXPECT_EQ(0u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1224   // The mask resource is empty.
1225   EXPECT_EQ(0u, active_layer_->ContentsResourceId());
1226 }
1227
1228 TEST_F(PictureLayerImplTest, ReleaseResources) {
1229   gfx::Size tile_size(400, 400);
1230   gfx::Size layer_bounds(1300, 1900);
1231
1232   scoped_refptr<FakePicturePileImpl> pending_pile =
1233       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1234   scoped_refptr<FakePicturePileImpl> active_pile =
1235       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1236
1237   SetupTrees(pending_pile, active_pile);
1238   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1239
1240   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1241                                     1.3f,  // ideal contents scale
1242                                     2.7f,  // device scale
1243                                     3.2f,  // page scale
1244                                     1.f,   // maximum animation scale
1245                                     false);
1246   EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1247
1248   // All tilings should be removed when losing output surface.
1249   active_layer_->ReleaseResources();
1250   EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1251   pending_layer_->ReleaseResources();
1252   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1253
1254   // This should create new tilings.
1255   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1256                                     1.3f,  // ideal contents scale
1257                                     2.7f,  // device scale
1258                                     3.2f,  // page scale
1259                                     1.f,   // maximum animation scale
1260                                     false);
1261   EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1262 }
1263
1264 TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1265   // The default max tile size is larger than 400x400.
1266   gfx::Size tile_size(400, 400);
1267   gfx::Size layer_bounds(5000, 5000);
1268
1269   scoped_refptr<FakePicturePileImpl> pending_pile =
1270       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1271   scoped_refptr<FakePicturePileImpl> active_pile =
1272       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1273
1274   SetupTrees(pending_pile, active_pile);
1275   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1276
1277   SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1278   ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1279
1280   pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1281
1282   // The default value.
1283   EXPECT_EQ(gfx::Size(256, 256).ToString(),
1284             host_impl_.settings().default_tile_size.ToString());
1285
1286   Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1287   EXPECT_EQ(gfx::Size(256, 256).ToString(),
1288             tile->content_rect().size().ToString());
1289
1290   pending_layer_->ReleaseResources();
1291
1292   // Change the max texture size on the output surface context.
1293   scoped_ptr<TestWebGraphicsContext3D> context =
1294       TestWebGraphicsContext3D::Create();
1295   context->set_max_texture_size(140);
1296   host_impl_.DidLoseOutputSurface();
1297   host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1298       context.Pass()).PassAs<OutputSurface>());
1299
1300   SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1301   ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1302
1303   pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1304
1305   // Verify the tiles are not larger than the context's max texture size.
1306   tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1307   EXPECT_GE(140, tile->content_rect().width());
1308   EXPECT_GE(140, tile->content_rect().height());
1309 }
1310
1311 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1312   // The default max tile size is larger than 400x400.
1313   gfx::Size tile_size(400, 400);
1314   gfx::Size layer_bounds(500, 500);
1315
1316   scoped_refptr<FakePicturePileImpl> pending_pile =
1317       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1318   scoped_refptr<FakePicturePileImpl> active_pile =
1319       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1320
1321   SetupTrees(pending_pile, active_pile);
1322   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1323
1324   SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1325   ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1326
1327   pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1328
1329   // The default value. The layer is smaller than this.
1330   EXPECT_EQ(gfx::Size(512, 512).ToString(),
1331             host_impl_.settings().max_untiled_layer_size.ToString());
1332
1333   // There should be a single tile since the layer is small.
1334   PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1335   EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1336
1337   pending_layer_->ReleaseResources();
1338
1339   // Change the max texture size on the output surface context.
1340   scoped_ptr<TestWebGraphicsContext3D> context =
1341       TestWebGraphicsContext3D::Create();
1342   context->set_max_texture_size(140);
1343   host_impl_.DidLoseOutputSurface();
1344   host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1345       context.Pass()).PassAs<OutputSurface>());
1346
1347   SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1348   ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1349
1350   pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1351
1352   // There should be more than one tile since the max texture size won't cover
1353   // the layer.
1354   high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1355   EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1356
1357   // Verify the tiles are not larger than the context's max texture size.
1358   Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1359   EXPECT_GE(140, tile->content_rect().width());
1360   EXPECT_GE(140, tile->content_rect().height());
1361 }
1362
1363 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1364   MockOcclusionTracker<LayerImpl> occlusion_tracker;
1365   scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1366
1367   gfx::Size tile_size(400, 400);
1368   gfx::Size layer_bounds(1300, 1900);
1369
1370   scoped_refptr<FakePicturePileImpl> pending_pile =
1371       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1372   scoped_refptr<FakePicturePileImpl> active_pile =
1373       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1374
1375   SetupTrees(pending_pile, active_pile);
1376
1377   active_layer_->draw_properties().visible_content_rect =
1378       gfx::Rect(layer_bounds);
1379
1380   gfx::Rect layer_invalidation(150, 200, 30, 180);
1381   Region invalidation(layer_invalidation);
1382   AddDefaultTilingsWithInvalidation(invalidation);
1383
1384   AppendQuadsData data;
1385   active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
1386   active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1387   active_layer_->DidDraw(NULL);
1388
1389   ASSERT_EQ(1U, render_pass->quad_list.size());
1390   EXPECT_EQ(DrawQuad::PICTURE_CONTENT,
1391             render_pass->quad_list.front()->material);
1392 }
1393
1394 TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
1395   gfx::Size tile_size(100, 100);
1396   gfx::Size layer_bounds(1000, 1000);
1397
1398   scoped_refptr<FakePicturePileImpl> pending_pile =
1399       FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1400   // Layers with entirely empty piles can't get tilings.
1401   pending_pile->AddRecordingAt(0, 0);
1402
1403   SetupPendingTree(pending_pile);
1404
1405   ASSERT_TRUE(pending_layer_->CanHaveTilings());
1406   pending_layer_->AddTiling(1.0f);
1407   pending_layer_->AddTiling(2.0f);
1408
1409   // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1410   // on a layer with no recordings.
1411   host_impl_.pending_tree()->UpdateDrawProperties();
1412   pending_layer_->MarkVisibleResourcesAsRequired();
1413 }
1414
1415 TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1416   gfx::Size tile_size(100, 100);
1417   gfx::Size layer_bounds(200, 200);
1418
1419   scoped_refptr<FakePicturePileImpl> pending_pile =
1420       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1421   SetupPendingTree(pending_pile);
1422
1423   pending_layer_->set_fixed_tile_size(tile_size);
1424   ASSERT_TRUE(pending_layer_->CanHaveTilings());
1425   PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1426   host_impl_.pending_tree()->UpdateDrawProperties();
1427   EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1428
1429   pending_layer_->draw_properties().visible_content_rect =
1430       gfx::Rect(0, 0, 100, 200);
1431
1432   // Fake set priorities.
1433   for (PictureLayerTiling::CoverageIterator iter(
1434            tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1435        iter;
1436        ++iter) {
1437     if (!*iter)
1438       continue;
1439     Tile* tile = *iter;
1440     TilePriority priority;
1441     priority.resolution = HIGH_RESOLUTION;
1442     gfx::Rect tile_bounds = iter.geometry_rect();
1443     if (pending_layer_->visible_content_rect().Intersects(tile_bounds)) {
1444       priority.priority_bin = TilePriority::NOW;
1445       priority.distance_to_visible = 0.f;
1446     } else {
1447       priority.priority_bin = TilePriority::SOON;
1448       priority.distance_to_visible = 1.f;
1449     }
1450     tile->SetPriority(PENDING_TREE, priority);
1451   }
1452
1453   pending_layer_->MarkVisibleResourcesAsRequired();
1454
1455   int num_visible = 0;
1456   int num_offscreen = 0;
1457
1458   for (PictureLayerTiling::CoverageIterator iter(
1459            tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1460        iter;
1461        ++iter) {
1462     if (!*iter)
1463       continue;
1464     const Tile* tile = *iter;
1465     if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1466       EXPECT_TRUE(tile->required_for_activation());
1467       num_visible++;
1468     } else {
1469       EXPECT_FALSE(tile->required_for_activation());
1470       num_offscreen++;
1471     }
1472   }
1473
1474   EXPECT_GT(num_visible, 0);
1475   EXPECT_GT(num_offscreen, 0);
1476 }
1477
1478 TEST_F(PictureLayerImplTest, TileOutsideOfViewportForTilePriorityNotRequired) {
1479   base::TimeTicks time_ticks;
1480   time_ticks += base::TimeDelta::FromMilliseconds(1);
1481   host_impl_.SetCurrentBeginFrameArgs(
1482       CreateBeginFrameArgsForTesting(time_ticks));
1483
1484   gfx::Size tile_size(100, 100);
1485   gfx::Size layer_bounds(400, 400);
1486   gfx::Rect external_viewport_for_tile_priority(400, 200);
1487   gfx::Rect visible_content_rect(200, 400);
1488
1489   scoped_refptr<FakePicturePileImpl> active_pile =
1490       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1491   scoped_refptr<FakePicturePileImpl> pending_pile =
1492       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1493   SetupTrees(pending_pile, active_pile);
1494
1495   active_layer_->set_fixed_tile_size(tile_size);
1496   pending_layer_->set_fixed_tile_size(tile_size);
1497   ASSERT_TRUE(pending_layer_->CanHaveTilings());
1498   PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1499
1500   // Set external viewport for tile priority.
1501   gfx::Rect viewport = gfx::Rect(layer_bounds);
1502   gfx::Transform transform;
1503   gfx::Transform transform_for_tile_priority;
1504   bool resourceless_software_draw = false;
1505   host_impl_.SetExternalDrawConstraints(transform,
1506                                         viewport,
1507                                         viewport,
1508                                         external_viewport_for_tile_priority,
1509                                         transform_for_tile_priority,
1510                                         resourceless_software_draw);
1511   host_impl_.pending_tree()->UpdateDrawProperties();
1512
1513   // Set visible content rect that is different from
1514   // external_viewport_for_tile_priority.
1515   pending_layer_->draw_properties().visible_content_rect = visible_content_rect;
1516   time_ticks += base::TimeDelta::FromMilliseconds(200);
1517   host_impl_.SetCurrentBeginFrameArgs(
1518       CreateBeginFrameArgsForTesting(time_ticks));
1519   pending_layer_->UpdateTiles(Occlusion());
1520
1521   pending_layer_->MarkVisibleResourcesAsRequired();
1522
1523   // Intersect the two rects. Any tile outside should not be required for
1524   // activation.
1525   gfx::Rect viewport_for_tile_priority =
1526       pending_layer_->GetViewportForTilePriorityInContentSpace();
1527   viewport_for_tile_priority.Intersect(pending_layer_->visible_content_rect());
1528
1529   int num_inside = 0;
1530   int num_outside = 0;
1531   for (PictureLayerTiling::CoverageIterator iter(
1532            tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1533        iter;
1534        ++iter) {
1535     if (!*iter)
1536       continue;
1537     Tile* tile = *iter;
1538     if (viewport_for_tile_priority.Intersects(iter.geometry_rect())) {
1539       num_inside++;
1540       // Mark everything in viewport for tile priority as ready to draw.
1541       ManagedTileState::TileVersion& tile_version =
1542           tile->GetTileVersionForTesting(
1543               tile->DetermineRasterModeForTree(PENDING_TREE));
1544       tile_version.SetSolidColorForTesting(SK_ColorRED);
1545     } else {
1546       num_outside++;
1547       EXPECT_FALSE(tile->required_for_activation());
1548     }
1549   }
1550
1551   EXPECT_GT(num_inside, 0);
1552   EXPECT_GT(num_outside, 0);
1553
1554   // Activate and draw active layer.
1555   host_impl_.ActivateSyncTree();
1556   host_impl_.active_tree()->UpdateDrawProperties();
1557   active_layer_->draw_properties().visible_content_rect = visible_content_rect;
1558
1559   MockOcclusionTracker<LayerImpl> occlusion_tracker;
1560   scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1561   AppendQuadsData data;
1562   active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1563   active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1564   active_layer_->DidDraw(NULL);
1565
1566   // All tiles in activation rect is ready to draw.
1567   EXPECT_EQ(0u, data.num_missing_tiles);
1568   EXPECT_EQ(0u, data.num_incomplete_tiles);
1569 }
1570
1571 TEST_F(PictureLayerImplTest, HighResTileIsComplete) {
1572   base::TimeTicks time_ticks;
1573   time_ticks += base::TimeDelta::FromMilliseconds(1);
1574   host_impl_.SetCurrentBeginFrameArgs(
1575       CreateBeginFrameArgsForTesting(time_ticks));
1576
1577   gfx::Size tile_size(100, 100);
1578   gfx::Size layer_bounds(200, 200);
1579
1580   host_impl_.SetViewportSize(layer_bounds);
1581
1582   scoped_refptr<FakePicturePileImpl> pending_pile =
1583       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1584   SetupPendingTree(pending_pile);
1585   ActivateTree();
1586
1587   // All high res tiles have resources.
1588   active_layer_->set_fixed_tile_size(tile_size);
1589   host_impl_.active_tree()->UpdateDrawProperties();
1590   std::vector<Tile*> tiles =
1591       active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1592   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
1593
1594   MockOcclusionTracker<LayerImpl> occlusion_tracker;
1595   scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1596   AppendQuadsData data;
1597   active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1598   active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1599   active_layer_->DidDraw(NULL);
1600
1601   // All high res tiles drew, nothing was incomplete.
1602   EXPECT_EQ(9u, render_pass->quad_list.size());
1603   EXPECT_EQ(0u, data.num_missing_tiles);
1604   EXPECT_EQ(0u, data.num_incomplete_tiles);
1605 }
1606
1607 TEST_F(PictureLayerImplTest, LowResTileIsIncomplete) {
1608   base::TimeTicks time_ticks;
1609   time_ticks += base::TimeDelta::FromMilliseconds(1);
1610   host_impl_.SetCurrentBeginFrameArgs(
1611       CreateBeginFrameArgsForTesting(time_ticks));
1612
1613   gfx::Size tile_size(100, 100);
1614   gfx::Size layer_bounds(200, 200);
1615
1616   host_impl_.SetViewportSize(layer_bounds);
1617
1618   scoped_refptr<FakePicturePileImpl> pending_pile =
1619       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1620   SetupPendingTree(pending_pile);
1621   ActivateTree();
1622
1623   // All high res tiles have resources except one.
1624   active_layer_->set_fixed_tile_size(tile_size);
1625   host_impl_.active_tree()->UpdateDrawProperties();
1626   std::vector<Tile*> high_tiles =
1627       active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1628   high_tiles.erase(high_tiles.begin());
1629   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1630
1631   // All low res tiles have resources.
1632   std::vector<Tile*> low_tiles =
1633       active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1634   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1635
1636   MockOcclusionTracker<LayerImpl> occlusion_tracker;
1637   scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1638   AppendQuadsData data;
1639   active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1640   active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1641   active_layer_->DidDraw(NULL);
1642
1643   // The missing high res tile was replaced by a low res tile.
1644   EXPECT_EQ(9u, render_pass->quad_list.size());
1645   EXPECT_EQ(0u, data.num_missing_tiles);
1646   EXPECT_EQ(1u, data.num_incomplete_tiles);
1647 }
1648
1649 TEST_F(PictureLayerImplTest,
1650        HighResAndIdealResTileIsCompleteWhenRasterScaleIsNotIdeal) {
1651   base::TimeTicks time_ticks;
1652   time_ticks += base::TimeDelta::FromMilliseconds(1);
1653   host_impl_.SetCurrentBeginFrameArgs(
1654       CreateBeginFrameArgsForTesting(time_ticks));
1655
1656   gfx::Size tile_size(100, 100);
1657   gfx::Size layer_bounds(200, 200);
1658
1659   host_impl_.SetViewportSize(layer_bounds);
1660
1661   scoped_refptr<FakePicturePileImpl> pending_pile =
1662       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1663   scoped_refptr<FakePicturePileImpl> active_pile =
1664       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1665   SetupTrees(pending_pile, active_pile);
1666
1667   active_layer_->set_fixed_tile_size(tile_size);
1668
1669   active_layer_->draw_properties().visible_content_rect =
1670       gfx::Rect(layer_bounds);
1671   SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1672
1673   // One ideal tile exists, this will get used when drawing.
1674   std::vector<Tile*> ideal_tiles;
1675   EXPECT_EQ(2.f, active_layer_->HighResTiling()->contents_scale());
1676   ideal_tiles.push_back(active_layer_->HighResTiling()->TileAt(0, 0));
1677   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1678       ideal_tiles);
1679
1680   // Due to layer scale throttling, the raster contents scale is changed to 1,
1681   // while the ideal is still 2.
1682   SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
1683   SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1684
1685   EXPECT_EQ(1.f, active_layer_->HighResTiling()->contents_scale());
1686   EXPECT_EQ(1.f, active_layer_->raster_contents_scale());
1687   EXPECT_EQ(2.f, active_layer_->ideal_contents_scale());
1688
1689   // Both tilings still exist.
1690   EXPECT_EQ(2.f, active_layer_->tilings()->tiling_at(0)->contents_scale());
1691   EXPECT_EQ(1.f, active_layer_->tilings()->tiling_at(1)->contents_scale());
1692
1693   // All high res tiles have resources.
1694   std::vector<Tile*> high_tiles =
1695       active_layer_->HighResTiling()->AllTilesForTesting();
1696   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1697
1698   MockOcclusionTracker<LayerImpl> occlusion_tracker;
1699   scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1700   AppendQuadsData data;
1701   active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1702   active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1703   active_layer_->DidDraw(NULL);
1704
1705   // All high res tiles drew, and the one ideal res tile drew.
1706   ASSERT_GT(render_pass->quad_list.size(), 9u);
1707   EXPECT_EQ(gfx::SizeF(99.f, 99.f),
1708             TileDrawQuad::MaterialCast(render_pass->quad_list.front())
1709                 ->tex_coord_rect.size());
1710   EXPECT_EQ(gfx::SizeF(49.5f, 49.5f),
1711             TileDrawQuad::MaterialCast(render_pass->quad_list.ElementAt(1))
1712                 ->tex_coord_rect.size());
1713
1714   // Neither the high res nor the ideal tiles were considered as incomplete.
1715   EXPECT_EQ(0u, data.num_missing_tiles);
1716   EXPECT_EQ(0u, data.num_incomplete_tiles);
1717 }
1718
1719 TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
1720   gfx::Size layer_bounds(400, 400);
1721   gfx::Size tile_size(100, 100);
1722   SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1723
1724   // No tiles shared.
1725   pending_layer_->set_invalidation(gfx::Rect(layer_bounds));
1726
1727   CreateHighLowResAndSetAllTilesVisible();
1728
1729   active_layer_->SetAllTilesReady();
1730
1731   // No shared tiles and all active tiles ready, so pending can only
1732   // activate with all high res tiles.
1733   pending_layer_->MarkVisibleResourcesAsRequired();
1734   AssertAllTilesRequired(pending_layer_->HighResTiling());
1735   AssertNoTilesRequired(pending_layer_->LowResTiling());
1736 }
1737
1738 TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
1739   gfx::Size layer_bounds(400, 400);
1740   gfx::Size tile_size(100, 100);
1741   SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1742
1743   // All tiles shared (no invalidation).
1744   CreateHighLowResAndSetAllTilesVisible();
1745
1746   // Verify active tree not ready.
1747   Tile* some_active_tile =
1748       active_layer_->HighResTiling()->AllTilesForTesting()[0];
1749   EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1750
1751   // When high res are required, even if the active tree is not ready,
1752   // the high res tiles must be ready.
1753   host_impl_.active_tree()->SetRequiresHighResToDraw();
1754   pending_layer_->MarkVisibleResourcesAsRequired();
1755   AssertAllTilesRequired(pending_layer_->HighResTiling());
1756   AssertNoTilesRequired(pending_layer_->LowResTiling());
1757 }
1758
1759 TEST_F(PictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
1760   gfx::Size layer_bounds(400, 400);
1761   gfx::Size tile_size(100, 100);
1762   SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1763
1764   CreateHighLowResAndSetAllTilesVisible();
1765
1766   Tile* some_active_tile =
1767       active_layer_->HighResTiling()->AllTilesForTesting()[0];
1768   EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1769
1770   // All tiles shared (no invalidation), so even though the active tree's
1771   // tiles aren't ready, there is nothing required.
1772   pending_layer_->MarkVisibleResourcesAsRequired();
1773   AssertNoTilesRequired(pending_layer_->HighResTiling());
1774   AssertNoTilesRequired(pending_layer_->LowResTiling());
1775 }
1776
1777 TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
1778   gfx::Size layer_bounds(400, 400);
1779   gfx::Size tile_size(100, 100);
1780   scoped_refptr<FakePicturePileImpl> pending_pile =
1781       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1782   // This pile will create tilings, but has no recordings so will not create any
1783   // tiles.  This is attempting to simulate scrolling past the end of recorded
1784   // content on the active layer, where the recordings are so far away that
1785   // no tiles are created.
1786   scoped_refptr<FakePicturePileImpl> active_pile =
1787       FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1788           tile_size, layer_bounds);
1789   SetupTrees(pending_pile, active_pile);
1790   pending_layer_->set_fixed_tile_size(tile_size);
1791   active_layer_->set_fixed_tile_size(tile_size);
1792
1793   CreateHighLowResAndSetAllTilesVisible();
1794
1795   // Active layer has tilings, but no tiles due to missing recordings.
1796   EXPECT_TRUE(active_layer_->CanHaveTilings());
1797   EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
1798   EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
1799
1800   // Since the active layer has no tiles at all, the pending layer doesn't
1801   // need content in order to activate.
1802   pending_layer_->MarkVisibleResourcesAsRequired();
1803   AssertNoTilesRequired(pending_layer_->HighResTiling());
1804   AssertNoTilesRequired(pending_layer_->LowResTiling());
1805 }
1806
1807 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
1808   gfx::Size layer_bounds(400, 400);
1809   gfx::Size tile_size(100, 100);
1810   scoped_refptr<FakePicturePileImpl> pending_pile =
1811       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1812   scoped_refptr<FakePicturePileImpl> active_pile =
1813       FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1814   SetupTrees(pending_pile, active_pile);
1815   pending_layer_->set_fixed_tile_size(tile_size);
1816   active_layer_->set_fixed_tile_size(tile_size);
1817
1818   CreateHighLowResAndSetAllTilesVisible();
1819
1820   // Active layer can't have tiles.
1821   EXPECT_FALSE(active_layer_->CanHaveTilings());
1822
1823   // All high res tiles required.  This should be considered identical
1824   // to the case where there is no active layer, to avoid flashing content.
1825   // This can happen if a layer exists for a while and switches from
1826   // not being able to have content to having content.
1827   pending_layer_->MarkVisibleResourcesAsRequired();
1828   AssertAllTilesRequired(pending_layer_->HighResTiling());
1829   AssertNoTilesRequired(pending_layer_->LowResTiling());
1830 }
1831
1832 TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
1833   gfx::Size layer_bounds(200, 200);
1834   gfx::Size tile_size(100, 100);
1835   SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1836
1837   gfx::Size pending_layer_bounds(400, 400);
1838   pending_layer_->SetBounds(pending_layer_bounds);
1839
1840   CreateHighLowResAndSetAllTilesVisible();
1841
1842   active_layer_->SetAllTilesReady();
1843
1844   // Since the active layer has different bounds, the pending layer needs all
1845   // high res tiles in order to activate.
1846   pending_layer_->MarkVisibleResourcesAsRequired();
1847   AssertAllTilesRequired(pending_layer_->HighResTiling());
1848   AssertNoTilesRequired(pending_layer_->LowResTiling());
1849 }
1850
1851 TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1852   gfx::Size tile_size(100, 100);
1853   gfx::Size layer_bounds(400, 400);
1854   scoped_refptr<FakePicturePileImpl> pending_pile =
1855       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1856
1857   host_impl_.CreatePendingTree();
1858   LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1859
1860   scoped_ptr<FakePictureLayerImpl> pending_layer =
1861       FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1862   pending_layer->SetDrawsContent(true);
1863   pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1864
1865   pending_layer_ = static_cast<FakePictureLayerImpl*>(
1866       host_impl_.pending_tree()->LayerById(id_));
1867
1868   // Set some state on the pending layer, make sure it is not clobbered
1869   // by a sync from the active layer.  This could happen because if the
1870   // pending layer has not been post-commit initialized it will attempt
1871   // to sync from the active layer.
1872   float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
1873   pending_layer_->set_raster_page_scale(raster_page_scale);
1874   EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1875
1876   host_impl_.ActivateSyncTree();
1877
1878   active_layer_ = static_cast<FakePictureLayerImpl*>(
1879       host_impl_.active_tree()->LayerById(id_));
1880
1881   EXPECT_EQ(0u, active_layer_->num_tilings());
1882   EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
1883   EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1884 }
1885
1886 TEST_F(PictureLayerImplTest, ShareTilesOnSync) {
1887   SetupDefaultTrees(gfx::Size(1500, 1500));
1888   AddDefaultTilingsWithInvalidation(gfx::Rect());
1889
1890   host_impl_.ActivateSyncTree();
1891   host_impl_.CreatePendingTree();
1892   active_layer_ = static_cast<FakePictureLayerImpl*>(
1893       host_impl_.active_tree()->LayerById(id_));
1894
1895   // Force the active tree to sync to the pending tree "post-commit".
1896   pending_layer_->DoPostCommitInitializationIfNeeded();
1897
1898   // Both invalidations should drop tiles from the pending tree.
1899   EXPECT_EQ(3u, active_layer_->num_tilings());
1900   EXPECT_EQ(3u, pending_layer_->num_tilings());
1901   for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1902     PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1903     PictureLayerTiling* pending_tiling =
1904         pending_layer_->tilings()->tiling_at(i);
1905
1906     ASSERT_TRUE(active_tiling);
1907     ASSERT_TRUE(pending_tiling);
1908
1909     EXPECT_TRUE(active_tiling->TileAt(0, 0));
1910     EXPECT_TRUE(active_tiling->TileAt(1, 0));
1911     EXPECT_TRUE(active_tiling->TileAt(0, 1));
1912     EXPECT_TRUE(active_tiling->TileAt(1, 1));
1913
1914     EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1915     EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1916     EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1917     EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1918
1919     EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1920     EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
1921     EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1922     EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
1923     EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1924     EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
1925     EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1926     EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
1927   }
1928 }
1929
1930 TEST_F(PictureLayerImplTest, ShareInvalidActiveTreeTilesOnSync) {
1931   SetupDefaultTrees(gfx::Size(1500, 1500));
1932   AddDefaultTilingsWithInvalidation(gfx::Rect(0, 0, 1, 1));
1933
1934   // This activates the 0,0,1,1 invalidation.
1935   host_impl_.ActivateSyncTree();
1936   host_impl_.CreatePendingTree();
1937   active_layer_ = static_cast<FakePictureLayerImpl*>(
1938       host_impl_.active_tree()->LayerById(id_));
1939
1940   // Force the active tree to sync to the pending tree "post-commit".
1941   pending_layer_->DoPostCommitInitializationIfNeeded();
1942
1943   // The active tree invalidation was handled by the active tiles, so they
1944   // can be shared with the pending tree.
1945   EXPECT_EQ(3u, active_layer_->num_tilings());
1946   EXPECT_EQ(3u, pending_layer_->num_tilings());
1947   for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1948     PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1949     PictureLayerTiling* pending_tiling =
1950         pending_layer_->tilings()->tiling_at(i);
1951
1952     ASSERT_TRUE(active_tiling);
1953     ASSERT_TRUE(pending_tiling);
1954
1955     EXPECT_TRUE(active_tiling->TileAt(0, 0));
1956     EXPECT_TRUE(active_tiling->TileAt(1, 0));
1957     EXPECT_TRUE(active_tiling->TileAt(0, 1));
1958     EXPECT_TRUE(active_tiling->TileAt(1, 1));
1959
1960     EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1961     EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1962     EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1963     EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1964
1965     EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1966     EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
1967     EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1968     EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
1969     EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1970     EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
1971     EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1972     EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
1973   }
1974 }
1975
1976 TEST_F(PictureLayerImplTest, RemoveInvalidPendingTreeTilesOnSync) {
1977   SetupDefaultTrees(gfx::Size(1500, 1500));
1978   AddDefaultTilingsWithInvalidation(gfx::Rect());
1979
1980   host_impl_.ActivateSyncTree();
1981   host_impl_.CreatePendingTree();
1982   active_layer_ = static_cast<FakePictureLayerImpl*>(
1983       host_impl_.active_tree()->LayerById(id_));
1984
1985   // Set some invalidation on the pending tree "during commit". We should
1986   // replace raster tiles that touch this.
1987   pending_layer_->set_invalidation(gfx::Rect(1, 1));
1988
1989   // Force the active tree to sync to the pending tree "post-commit".
1990   pending_layer_->DoPostCommitInitializationIfNeeded();
1991
1992   // The pending tree invalidation means tiles can not be shared with the
1993   // active tree.
1994   EXPECT_EQ(3u, active_layer_->num_tilings());
1995   EXPECT_EQ(3u, pending_layer_->num_tilings());
1996   for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1997     PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1998     PictureLayerTiling* pending_tiling =
1999         pending_layer_->tilings()->tiling_at(i);
2000
2001     ASSERT_TRUE(active_tiling);
2002     ASSERT_TRUE(pending_tiling);
2003
2004     EXPECT_TRUE(active_tiling->TileAt(0, 0));
2005     EXPECT_TRUE(active_tiling->TileAt(1, 0));
2006     EXPECT_TRUE(active_tiling->TileAt(0, 1));
2007     EXPECT_TRUE(active_tiling->TileAt(1, 1));
2008
2009     EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2010     EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2011     EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2012     EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2013
2014     EXPECT_NE(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2015     EXPECT_FALSE(active_tiling->TileAt(0, 0)->is_shared());
2016     EXPECT_FALSE(pending_tiling->TileAt(0, 0)->is_shared());
2017     EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2018     EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2019     EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2020     EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2021     EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2022     EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2023   }
2024 }
2025
2026 TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
2027   SetupDefaultTrees(gfx::Size(10, 10));
2028   host_impl_.active_tree()->UpdateDrawProperties();
2029   EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2030
2031   // Contrived unit test of a real crash. A layer is transparent during a
2032   // context loss, and later becomes opaque, causing active layer SyncTiling to
2033   // be called.
2034   float new_scale = 1.f;
2035   active_layer_->ReleaseResources();
2036   pending_layer_->ReleaseResources();
2037   EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(new_scale));
2038   pending_layer_->AddTiling(new_scale);
2039   EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(new_scale));
2040
2041   // UpdateDrawProperties early-outs if the tree doesn't need it.  It is also
2042   // responsible for calling ManageTilings.  These checks verify that
2043   // ReleaseResources has set needs update draw properties so that the
2044   // new tiling gets the appropriate resolution set in ManageTilings.
2045   EXPECT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2046   host_impl_.active_tree()->UpdateDrawProperties();
2047   PictureLayerTiling* high_res =
2048       active_layer_->tilings()->TilingAtScale(new_scale);
2049   ASSERT_TRUE(!!high_res);
2050   EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
2051 }
2052
2053 TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
2054   SetupDefaultTrees(gfx::Size(10, 10));
2055
2056   const float kScale = 1.f;
2057   pending_layer_->AddTiling(kScale);
2058   EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2059   EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2060
2061   // Gpu rasterization is disabled by default.
2062   EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2063   // Toggling the gpu rasterization clears all tilings on both trees.
2064   host_impl_.SetUseGpuRasterization(true);
2065   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2066   EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2067
2068   // Make sure that we can still add tiling to the pending layer,
2069   // that gets synced to the active layer.
2070   pending_layer_->AddTiling(kScale);
2071   EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2072   EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2073
2074   // Toggling the gpu rasterization clears all tilings on both trees.
2075   EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2076   host_impl_.SetUseGpuRasterization(false);
2077   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2078   EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2079 }
2080
2081 TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
2082   SetupDefaultTrees(gfx::Size(10, 10));
2083   host_impl_.active_tree()->UpdateDrawProperties();
2084   EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2085
2086   SetupDrawPropertiesAndUpdateTiles(
2087       active_layer_, 0.5f, 0.5f, 0.5f, 0.5f, false);
2088   active_layer_->tilings()->RemoveAllTilings();
2089   PictureLayerTiling* tiling = active_layer_->tilings()->AddTiling(0.5f);
2090   active_layer_->tilings()->AddTiling(1.5f);
2091   active_layer_->tilings()->AddTiling(0.25f);
2092   tiling->set_resolution(HIGH_RESOLUTION);
2093
2094   // Sanity checks.
2095   ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
2096   ASSERT_EQ(tiling, active_layer_->tilings()->TilingAtScale(0.5f));
2097
2098   // Now, set the bounds to be 1x1 (so that minimum contents scale becomes
2099   // 1.0f). Note that we should also ensure that the pending layer needs post
2100   // commit initialization, since this is what would happen during commit. In
2101   // other words we want the pending layer to sync from the active layer.
2102   pending_layer_->SetBounds(gfx::Size(1, 1));
2103   pending_layer_->SetNeedsPostCommitInitialization();
2104   pending_layer_->set_twin_layer(NULL);
2105   active_layer_->set_twin_layer(NULL);
2106   EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
2107
2108   // Update the draw properties: sync from active tree should happen here.
2109   host_impl_.pending_tree()->UpdateDrawProperties();
2110
2111   // Another sanity check.
2112   ASSERT_EQ(1.f, pending_layer_->MinimumContentsScale());
2113
2114   // Now we should've synced 1.5f tiling, since that's the only one that doesn't
2115   // violate minimum contents scale. At the same time, we should've created a
2116   // new high res tiling at scale 1.0f.
2117   EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
2118   ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.0f));
2119   EXPECT_EQ(HIGH_RESOLUTION,
2120             pending_layer_->tilings()->TilingAtScale(1.0f)->resolution());
2121   ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.5f));
2122   EXPECT_EQ(NON_IDEAL_RESOLUTION,
2123             pending_layer_->tilings()->TilingAtScale(1.5f)->resolution());
2124 }
2125
2126 TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
2127   gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
2128   gfx::Size layer_bounds(default_tile_size.width() * 4,
2129                          default_tile_size.height() * 4);
2130
2131   SetupDefaultTrees(layer_bounds);
2132   EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2133   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2134   SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2135   // Should have a low-res and a high-res tiling.
2136   ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
2137
2138   ResetTilingsAndRasterScales();
2139
2140   host_impl_.SetUseGpuRasterization(true);
2141   EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2142   SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2143
2144   // Should only have the high-res tiling.
2145   ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
2146 }
2147
2148 TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
2149   // Set up layers with tilings.
2150   SetupDefaultTrees(gfx::Size(10, 10));
2151   SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
2152   pending_layer_->PushPropertiesTo(active_layer_);
2153   EXPECT_TRUE(pending_layer_->DrawsContent());
2154   EXPECT_TRUE(pending_layer_->CanHaveTilings());
2155   EXPECT_GE(pending_layer_->num_tilings(), 0u);
2156   EXPECT_GE(active_layer_->num_tilings(), 0u);
2157
2158   // Set content to false, which should make CanHaveTilings return false.
2159   pending_layer_->SetDrawsContent(false);
2160   EXPECT_FALSE(pending_layer_->DrawsContent());
2161   EXPECT_FALSE(pending_layer_->CanHaveTilings());
2162
2163   // No tilings should be pushed to active layer.
2164   pending_layer_->PushPropertiesTo(active_layer_);
2165   EXPECT_EQ(0u, active_layer_->num_tilings());
2166 }
2167
2168 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
2169   SetupDefaultTrees(gfx::Size(10, 10));
2170   host_impl_.PinchGestureBegin();
2171   float high_res_scale = 2.3f;
2172   SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2173
2174   ASSERT_GE(pending_layer_->num_tilings(), 0u);
2175   EXPECT_FLOAT_EQ(high_res_scale,
2176                   pending_layer_->HighResTiling()->contents_scale());
2177 }
2178
2179 TEST_F(PictureLayerImplTest, FirstTilingTooSmall) {
2180   SetupDefaultTrees(gfx::Size(10, 10));
2181   host_impl_.PinchGestureBegin();
2182   float high_res_scale = 0.0001f;
2183   EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale);
2184
2185   SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2186
2187   ASSERT_GE(pending_layer_->num_tilings(), 0u);
2188   EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2189                   pending_layer_->HighResTiling()->contents_scale());
2190 }
2191
2192 TEST_F(PictureLayerImplTest, PinchingTooSmall) {
2193   SetupDefaultTrees(gfx::Size(10, 10));
2194
2195   float contents_scale = 0.15f;
2196   SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
2197
2198   ASSERT_GE(pending_layer_->num_tilings(), 0u);
2199   EXPECT_FLOAT_EQ(contents_scale,
2200                   pending_layer_->HighResTiling()->contents_scale());
2201
2202   host_impl_.PinchGestureBegin();
2203
2204   float page_scale = 0.0001f;
2205   EXPECT_LT(page_scale * contents_scale,
2206             pending_layer_->MinimumContentsScale());
2207
2208   SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, 1.f, false);
2209   ASSERT_GE(pending_layer_->num_tilings(), 0u);
2210   EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2211                   pending_layer_->HighResTiling()->contents_scale());
2212 }
2213
2214 class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
2215  public:
2216   virtual void InitializeRenderer() OVERRIDE {
2217     bool delegated_rendering = false;
2218     host_impl_.InitializeRenderer(
2219         FakeOutputSurface::CreateDeferredGL(
2220             scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
2221             delegated_rendering).PassAs<OutputSurface>());
2222   }
2223
2224   virtual void SetUp() OVERRIDE {
2225     PictureLayerImplTest::SetUp();
2226
2227     // Create some default active and pending trees.
2228     gfx::Size tile_size(100, 100);
2229     gfx::Size layer_bounds(400, 400);
2230
2231     scoped_refptr<FakePicturePileImpl> pending_pile =
2232         FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2233     scoped_refptr<FakePicturePileImpl> active_pile =
2234         FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2235
2236     SetupTrees(pending_pile, active_pile);
2237   }
2238 };
2239
2240 // This test is really a LayerTreeHostImpl test, in that it makes sure
2241 // that trees need update draw properties after deferred initialization.
2242 // However, this is also a regression test for PictureLayerImpl in that
2243 // not having this update will cause a crash.
2244 TEST_F(DeferredInitPictureLayerImplTest, PreventUpdateTilesDuringLostContext) {
2245   host_impl_.pending_tree()->UpdateDrawProperties();
2246   host_impl_.active_tree()->UpdateDrawProperties();
2247   EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
2248   EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2249
2250   FakeOutputSurface* fake_output_surface =
2251       static_cast<FakeOutputSurface*>(host_impl_.output_surface());
2252   ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
2253       TestContextProvider::Create()));
2254
2255   // These will crash PictureLayerImpl if this is not true.
2256   ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
2257   ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2258   host_impl_.active_tree()->UpdateDrawProperties();
2259 }
2260
2261 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
2262   gfx::Size layer_bounds(100, 100);
2263   gfx::Size viewport_size(1000, 1000);
2264   SetupDefaultTrees(layer_bounds);
2265   host_impl_.SetViewportSize(viewport_size);
2266
2267   float contents_scale = 1.f;
2268   float device_scale = 1.3f;
2269   float page_scale = 1.4f;
2270   float maximum_animation_scale = 1.f;
2271   bool animating_transform = false;
2272
2273   SetContentsScaleOnBothLayers(contents_scale,
2274                                device_scale,
2275                                page_scale,
2276                                maximum_animation_scale,
2277                                animating_transform);
2278   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2279
2280   // Since we're CPU-rasterizing, starting an animation should cause tiling
2281   // resolution to get set to the maximum animation scale factor.
2282   animating_transform = true;
2283   maximum_animation_scale = 3.f;
2284   contents_scale = 2.f;
2285
2286   SetContentsScaleOnBothLayers(contents_scale,
2287                                device_scale,
2288                                page_scale,
2289                                maximum_animation_scale,
2290                                animating_transform);
2291   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2292
2293   // Further changes to scale during the animation should not cause a new
2294   // high-res tiling to get created.
2295   contents_scale = 4.f;
2296   maximum_animation_scale = 5.f;
2297
2298   SetContentsScaleOnBothLayers(contents_scale,
2299                                device_scale,
2300                                page_scale,
2301                                maximum_animation_scale,
2302                                animating_transform);
2303   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2304
2305   // Once we stop animating, a new high-res tiling should be created.
2306   animating_transform = false;
2307
2308   SetContentsScaleOnBothLayers(contents_scale,
2309                                device_scale,
2310                                page_scale,
2311                                maximum_animation_scale,
2312                                animating_transform);
2313   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2314
2315   // When animating with an unknown maximum animation scale factor, a new
2316   // high-res tiling should be created at the animation's initial scale.
2317   animating_transform = true;
2318   contents_scale = 2.f;
2319   maximum_animation_scale = 0.f;
2320
2321   SetContentsScaleOnBothLayers(contents_scale,
2322                                device_scale,
2323                                page_scale,
2324                                maximum_animation_scale,
2325                                animating_transform);
2326   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2327
2328   // Further changes to scale during the animation should not cause a new
2329   // high-res tiling to get created.
2330   contents_scale = 3.f;
2331
2332   SetContentsScaleOnBothLayers(contents_scale,
2333                                device_scale,
2334                                page_scale,
2335                                maximum_animation_scale,
2336                                animating_transform);
2337   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2338
2339   // Once we stop animating, a new high-res tiling should be created.
2340   animating_transform = false;
2341   contents_scale = 4.f;
2342
2343   SetContentsScaleOnBothLayers(contents_scale,
2344                                device_scale,
2345                                page_scale,
2346                                maximum_animation_scale,
2347                                animating_transform);
2348   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2349
2350   // When animating with a maxmium animation scale factor that is so large
2351   // that the layer grows larger than the viewport at this scale, a new
2352   // high-res tiling should get created at the animation's initial scale, not
2353   // at its maximum scale.
2354   animating_transform = true;
2355   contents_scale = 2.f;
2356   maximum_animation_scale = 11.f;
2357
2358   SetContentsScaleOnBothLayers(contents_scale,
2359                                device_scale,
2360                                page_scale,
2361                                maximum_animation_scale,
2362                                animating_transform);
2363   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2364
2365   // Once we stop animating, a new high-res tiling should be created.
2366   animating_transform = false;
2367   contents_scale = 11.f;
2368
2369   SetContentsScaleOnBothLayers(contents_scale,
2370                                device_scale,
2371                                page_scale,
2372                                maximum_animation_scale,
2373                                animating_transform);
2374   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2375
2376   // When animating with a maxmium animation scale factor that is so large
2377   // that the layer grows larger than the viewport at this scale, and where
2378   // the intial source scale is < 1, a new high-res tiling should get created
2379   // at source scale 1.
2380   animating_transform = true;
2381   contents_scale = 0.1f;
2382   maximum_animation_scale = 11.f;
2383
2384   SetContentsScaleOnBothLayers(contents_scale,
2385                                device_scale,
2386                                page_scale,
2387                                maximum_animation_scale,
2388                                animating_transform);
2389   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2390
2391   // Once we stop animating, a new high-res tiling should be created.
2392   animating_transform = false;
2393   contents_scale = 11.f;
2394
2395   SetContentsScaleOnBothLayers(contents_scale,
2396                                device_scale,
2397                                page_scale,
2398                                maximum_animation_scale,
2399                                animating_transform);
2400   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2401 }
2402
2403 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForGpuRasterization) {
2404   gfx::Size layer_bounds(100, 100);
2405   gfx::Size viewport_size(1000, 1000);
2406   SetupDefaultTrees(layer_bounds);
2407   host_impl_.SetViewportSize(viewport_size);
2408   host_impl_.SetUseGpuRasterization(true);
2409
2410   float contents_scale = 1.f;
2411   float device_scale = 1.3f;
2412   float page_scale = 1.4f;
2413   float maximum_animation_scale = 1.f;
2414   bool animating_transform = false;
2415
2416   SetContentsScaleOnBothLayers(contents_scale,
2417                                device_scale,
2418                                page_scale,
2419                                maximum_animation_scale,
2420                                animating_transform);
2421   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2422
2423   // Since we're GPU-rasterizing, starting an animation should cause tiling
2424   // resolution to get set to the current contents scale.
2425   animating_transform = true;
2426   contents_scale = 2.f;
2427   maximum_animation_scale = 4.f;
2428
2429   SetContentsScaleOnBothLayers(contents_scale,
2430                                device_scale,
2431                                page_scale,
2432                                maximum_animation_scale,
2433                                animating_transform);
2434   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2435
2436   // Further changes to scale during the animation should cause a new high-res
2437   // tiling to get created.
2438   contents_scale = 3.f;
2439
2440   SetContentsScaleOnBothLayers(contents_scale,
2441                                device_scale,
2442                                page_scale,
2443                                maximum_animation_scale,
2444                                animating_transform);
2445   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2446
2447   // Since we're re-rasterizing during the animation, scales smaller than 1
2448   // should be respected.
2449   contents_scale = 0.25f;
2450
2451   SetContentsScaleOnBothLayers(contents_scale,
2452                                device_scale,
2453                                page_scale,
2454                                maximum_animation_scale,
2455                                animating_transform);
2456   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 0.25f);
2457
2458   // Once we stop animating, a new high-res tiling should be created.
2459   contents_scale = 4.f;
2460   animating_transform = false;
2461
2462   SetContentsScaleOnBothLayers(contents_scale,
2463                                device_scale,
2464                                page_scale,
2465                                maximum_animation_scale,
2466                                animating_transform);
2467   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2468
2469   static_cast<FakePicturePileImpl*>(pending_layer_->pile())->set_has_text(true);
2470   static_cast<FakePicturePileImpl*>(active_layer_->pile())->set_has_text(true);
2471
2472   // Since we're GPU-rasterizing but have text, starting an animation should
2473   // cause tiling resolution to get set to the maximum animation scale.
2474   animating_transform = true;
2475   contents_scale = 2.f;
2476   maximum_animation_scale = 3.f;
2477
2478   SetContentsScaleOnBothLayers(contents_scale,
2479                                device_scale,
2480                                page_scale,
2481                                maximum_animation_scale,
2482                                animating_transform);
2483   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2484
2485   // Further changes to scale during the animation should not cause a new
2486   // high-res tiling to get created.
2487   contents_scale = 4.f;
2488   maximum_animation_scale = 5.f;
2489
2490   SetContentsScaleOnBothLayers(contents_scale,
2491                                device_scale,
2492                                page_scale,
2493                                maximum_animation_scale,
2494                                animating_transform);
2495   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2496
2497   // Once we stop animating, a new high-res tiling should be created.
2498   animating_transform = false;
2499
2500   SetContentsScaleOnBothLayers(contents_scale,
2501                                device_scale,
2502                                page_scale,
2503                                maximum_animation_scale,
2504                                animating_transform);
2505   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2506 }
2507
2508 TEST_F(PictureLayerImplTest, LayerRasterTileIterator) {
2509   base::TimeTicks time_ticks;
2510   time_ticks += base::TimeDelta::FromMilliseconds(1);
2511   host_impl_.SetCurrentBeginFrameArgs(
2512       CreateBeginFrameArgsForTesting(time_ticks));
2513
2514   gfx::Size tile_size(100, 100);
2515   gfx::Size layer_bounds(1000, 1000);
2516
2517   scoped_refptr<FakePicturePileImpl> pending_pile =
2518       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2519
2520   SetupPendingTree(pending_pile);
2521
2522   ASSERT_TRUE(pending_layer_->CanHaveTilings());
2523
2524   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2525
2526   // Empty iterator
2527   PictureLayerImpl::LayerRasterTileIterator it;
2528   EXPECT_FALSE(it);
2529
2530   // No tilings.
2531   it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2532   EXPECT_FALSE(it);
2533
2534   pending_layer_->AddTiling(low_res_factor);
2535   pending_layer_->AddTiling(0.3f);
2536   pending_layer_->AddTiling(0.7f);
2537   PictureLayerTiling* high_res_tiling = pending_layer_->AddTiling(1.0f);
2538   pending_layer_->AddTiling(2.0f);
2539
2540   host_impl_.SetViewportSize(gfx::Size(500, 500));
2541   host_impl_.pending_tree()->UpdateDrawProperties();
2542
2543   std::set<Tile*> unique_tiles;
2544   bool reached_prepaint = false;
2545   size_t non_ideal_tile_count = 0u;
2546   size_t low_res_tile_count = 0u;
2547   size_t high_res_tile_count = 0u;
2548   for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2549        it;
2550        ++it) {
2551     Tile* tile = *it;
2552     TilePriority priority = tile->priority(PENDING_TREE);
2553
2554     EXPECT_TRUE(tile);
2555
2556     // Non-high res tiles only get visible tiles. Also, prepaint should only
2557     // come at the end of the iteration.
2558     if (priority.resolution != HIGH_RESOLUTION)
2559       EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2560     else if (reached_prepaint)
2561       EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2562     else
2563       reached_prepaint = priority.priority_bin != TilePriority::NOW;
2564
2565     non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2566     low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2567     high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2568
2569     unique_tiles.insert(tile);
2570   }
2571
2572   EXPECT_TRUE(reached_prepaint);
2573   EXPECT_EQ(0u, non_ideal_tile_count);
2574   EXPECT_EQ(1u, low_res_tile_count);
2575   EXPECT_EQ(16u, high_res_tile_count);
2576   EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2577             unique_tiles.size());
2578
2579   // No NOW tiles.
2580   time_ticks += base::TimeDelta::FromMilliseconds(200);
2581   host_impl_.SetCurrentBeginFrameArgs(
2582       CreateBeginFrameArgsForTesting(time_ticks));
2583
2584   pending_layer_->draw_properties().visible_content_rect =
2585       gfx::Rect(1100, 1100, 500, 500);
2586   pending_layer_->UpdateTiles(Occlusion());
2587
2588   unique_tiles.clear();
2589   high_res_tile_count = 0u;
2590   for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2591        it;
2592        ++it) {
2593     Tile* tile = *it;
2594     TilePriority priority = tile->priority(PENDING_TREE);
2595
2596     EXPECT_TRUE(tile);
2597
2598     // Non-high res tiles only get visible tiles.
2599     EXPECT_EQ(HIGH_RESOLUTION, priority.resolution);
2600     EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2601
2602     high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2603
2604     unique_tiles.insert(tile);
2605   }
2606
2607   EXPECT_EQ(16u, high_res_tile_count);
2608   EXPECT_EQ(high_res_tile_count, unique_tiles.size());
2609
2610   time_ticks += base::TimeDelta::FromMilliseconds(200);
2611   host_impl_.SetCurrentBeginFrameArgs(
2612       CreateBeginFrameArgsForTesting(time_ticks));
2613
2614   pending_layer_->draw_properties().visible_content_rect =
2615       gfx::Rect(0, 0, 500, 500);
2616   pending_layer_->UpdateTiles(Occlusion());
2617
2618   std::vector<Tile*> high_res_tiles = high_res_tiling->AllTilesForTesting();
2619   for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
2620        tile_it != high_res_tiles.end();
2621        ++tile_it) {
2622     Tile* tile = *tile_it;
2623     ManagedTileState::TileVersion& tile_version =
2624         tile->GetTileVersionForTesting(
2625             tile->DetermineRasterModeForTree(ACTIVE_TREE));
2626     tile_version.SetSolidColorForTesting(SK_ColorRED);
2627   }
2628
2629   non_ideal_tile_count = 0;
2630   low_res_tile_count = 0;
2631   high_res_tile_count = 0;
2632   for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2633        it;
2634        ++it) {
2635     Tile* tile = *it;
2636     TilePriority priority = tile->priority(PENDING_TREE);
2637
2638     EXPECT_TRUE(tile);
2639
2640     non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2641     low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2642     high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2643   }
2644
2645   EXPECT_EQ(0u, non_ideal_tile_count);
2646   EXPECT_EQ(1u, low_res_tile_count);
2647   EXPECT_EQ(0u, high_res_tile_count);
2648 }
2649
2650 TEST_F(PictureLayerImplTest, LayerEvictionTileIterator) {
2651   gfx::Size tile_size(100, 100);
2652   gfx::Size layer_bounds(1000, 1000);
2653
2654   scoped_refptr<FakePicturePileImpl> pending_pile =
2655       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2656
2657   SetupPendingTree(pending_pile);
2658
2659   ASSERT_TRUE(pending_layer_->CanHaveTilings());
2660
2661   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2662
2663   std::vector<PictureLayerTiling*> tilings;
2664   tilings.push_back(pending_layer_->AddTiling(low_res_factor));
2665   tilings.push_back(pending_layer_->AddTiling(0.3f));
2666   tilings.push_back(pending_layer_->AddTiling(0.7f));
2667   tilings.push_back(pending_layer_->AddTiling(1.0f));
2668   tilings.push_back(pending_layer_->AddTiling(2.0f));
2669
2670   host_impl_.SetViewportSize(gfx::Size(500, 500));
2671   host_impl_.pending_tree()->UpdateDrawProperties();
2672
2673   std::vector<Tile*> all_tiles;
2674   for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
2675            tilings.begin();
2676        tiling_iterator != tilings.end();
2677        ++tiling_iterator) {
2678     std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
2679     std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
2680   }
2681
2682   std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
2683
2684   bool mark_required = false;
2685   size_t number_of_marked_tiles = 0u;
2686   size_t number_of_unmarked_tiles = 0u;
2687   for (size_t i = 0; i < tilings.size(); ++i) {
2688     PictureLayerTiling* tiling = tilings.at(i);
2689     for (PictureLayerTiling::CoverageIterator iter(
2690              tiling,
2691              pending_layer_->contents_scale_x(),
2692              pending_layer_->visible_content_rect());
2693          iter;
2694          ++iter) {
2695       if (mark_required) {
2696         number_of_marked_tiles++;
2697         iter->MarkRequiredForActivation();
2698       } else {
2699         number_of_unmarked_tiles++;
2700       }
2701       mark_required = !mark_required;
2702     }
2703   }
2704
2705   // Sanity checks.
2706   EXPECT_EQ(91u, all_tiles.size());
2707   EXPECT_EQ(91u, all_tiles_set.size());
2708   EXPECT_GT(number_of_marked_tiles, 1u);
2709   EXPECT_GT(number_of_unmarked_tiles, 1u);
2710
2711   // Empty iterator.
2712   PictureLayerImpl::LayerEvictionTileIterator it;
2713   EXPECT_FALSE(it);
2714
2715   // Tiles don't have resources yet.
2716   it = PictureLayerImpl::LayerEvictionTileIterator(
2717       pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2718   EXPECT_FALSE(it);
2719
2720   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
2721
2722   std::set<Tile*> unique_tiles;
2723   float expected_scales[] = {2.0f, 0.3f, 0.7f, low_res_factor, 1.0f};
2724   size_t scale_index = 0;
2725   bool reached_visible = false;
2726   Tile* last_tile = NULL;
2727   for (it = PictureLayerImpl::LayerEvictionTileIterator(
2728            pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2729        it;
2730        ++it) {
2731     Tile* tile = *it;
2732     if (!last_tile)
2733       last_tile = tile;
2734
2735     EXPECT_TRUE(tile);
2736
2737     TilePriority priority = tile->priority(PENDING_TREE);
2738
2739     if (priority.priority_bin == TilePriority::NOW) {
2740       reached_visible = true;
2741       last_tile = tile;
2742       break;
2743     }
2744
2745     EXPECT_FALSE(tile->required_for_activation());
2746
2747     while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2748            std::numeric_limits<float>::epsilon()) {
2749       ++scale_index;
2750       ASSERT_LT(scale_index, arraysize(expected_scales));
2751     }
2752
2753     EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2754     unique_tiles.insert(tile);
2755
2756     // If the tile is the same rough bin as last tile (same activation, bin, and
2757     // scale), then distance should be decreasing.
2758     if (tile->required_for_activation() ==
2759             last_tile->required_for_activation() &&
2760         priority.priority_bin ==
2761             last_tile->priority(PENDING_TREE).priority_bin &&
2762         std::abs(tile->contents_scale() - last_tile->contents_scale()) <
2763             std::numeric_limits<float>::epsilon()) {
2764       EXPECT_LE(priority.distance_to_visible,
2765                 last_tile->priority(PENDING_TREE).distance_to_visible);
2766     }
2767
2768     last_tile = tile;
2769   }
2770
2771   EXPECT_TRUE(reached_visible);
2772   EXPECT_EQ(65u, unique_tiles.size());
2773
2774   scale_index = 0;
2775   bool reached_required = false;
2776   for (; it; ++it) {
2777     Tile* tile = *it;
2778     EXPECT_TRUE(tile);
2779
2780     TilePriority priority = tile->priority(PENDING_TREE);
2781     EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2782
2783     if (reached_required) {
2784       EXPECT_TRUE(tile->required_for_activation());
2785     } else if (tile->required_for_activation()) {
2786       reached_required = true;
2787       scale_index = 0;
2788     }
2789
2790     while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2791            std::numeric_limits<float>::epsilon()) {
2792       ++scale_index;
2793       ASSERT_LT(scale_index, arraysize(expected_scales));
2794     }
2795
2796     EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2797     unique_tiles.insert(tile);
2798   }
2799
2800   EXPECT_TRUE(reached_required);
2801   EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
2802 }
2803
2804 TEST_F(PictureLayerImplTest, Occlusion) {
2805   gfx::Size tile_size(102, 102);
2806   gfx::Size layer_bounds(1000, 1000);
2807   gfx::Size viewport_size(1000, 1000);
2808
2809   LayerTestCommon::LayerImplTest impl;
2810
2811   scoped_refptr<FakePicturePileImpl> pending_pile =
2812       FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
2813   SetupPendingTree(pending_pile);
2814   pending_layer_->SetBounds(layer_bounds);
2815   ActivateTree();
2816   active_layer_->set_fixed_tile_size(tile_size);
2817
2818   host_impl_.SetViewportSize(viewport_size);
2819   host_impl_.active_tree()->UpdateDrawProperties();
2820
2821   std::vector<Tile*> tiles =
2822       active_layer_->HighResTiling()->AllTilesForTesting();
2823   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
2824
2825   {
2826     SCOPED_TRACE("No occlusion");
2827     gfx::Rect occluded;
2828     impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2829
2830     LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
2831                                                  gfx::Rect(layer_bounds));
2832     EXPECT_EQ(100u, impl.quad_list().size());
2833   }
2834
2835   {
2836     SCOPED_TRACE("Full occlusion");
2837     gfx::Rect occluded(active_layer_->visible_content_rect());
2838     impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2839
2840     LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
2841     EXPECT_EQ(impl.quad_list().size(), 0u);
2842   }
2843
2844   {
2845     SCOPED_TRACE("Partial occlusion");
2846     gfx::Rect occluded(150, 0, 200, 1000);
2847     impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2848
2849     size_t partially_occluded_count = 0;
2850     LayerTestCommon::VerifyQuadsAreOccluded(
2851         impl.quad_list(), occluded, &partially_occluded_count);
2852     // The layer outputs one quad, which is partially occluded.
2853     EXPECT_EQ(100u - 10u, impl.quad_list().size());
2854     EXPECT_EQ(10u + 10u, partially_occluded_count);
2855   }
2856 }
2857
2858 TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
2859   gfx::Size tile_size(host_impl_.settings().default_tile_size);
2860   SetupDefaultTrees(tile_size);
2861
2862   float contents_scale = 2.f;
2863   float device_scale = 1.f;
2864   float page_scale = 1.f;
2865   float maximum_animation_scale = 1.f;
2866   bool animating_transform = false;
2867
2868   SetContentsScaleOnBothLayers(contents_scale,
2869                                device_scale,
2870                                page_scale,
2871                                maximum_animation_scale,
2872                                animating_transform);
2873   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2874
2875   // Changing the source scale without being in an animation will cause
2876   // the layer to reset its source scale to 1.f.
2877   contents_scale = 3.f;
2878
2879   SetContentsScaleOnBothLayers(contents_scale,
2880                                device_scale,
2881                                page_scale,
2882                                maximum_animation_scale,
2883                                animating_transform);
2884   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2885
2886   // Further changes to the source scale will no longer be reflected in the
2887   // contents scale.
2888   contents_scale = 0.5f;
2889
2890   SetContentsScaleOnBothLayers(contents_scale,
2891                                device_scale,
2892                                page_scale,
2893                                maximum_animation_scale,
2894                                animating_transform);
2895   EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2896 }
2897
2898 TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
2899   gfx::Size tile_size(100, 100);
2900   gfx::Size layer_bounds(1000, 1000);
2901
2902   SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2903
2904   // Make sure some tiles are not shared.
2905   pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2906
2907   CreateHighLowResAndSetAllTilesVisible();
2908   active_layer_->SetAllTilesReady();
2909   pending_layer_->MarkVisibleResourcesAsRequired();
2910
2911   // All pending layer tiles required are not ready.
2912   EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2913
2914   // Initialize all low-res tiles.
2915   pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
2916
2917   // Low-res tiles should not be enough.
2918   EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2919
2920   // Initialize remaining tiles.
2921   pending_layer_->SetAllTilesReady();
2922
2923   EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2924 }
2925
2926 TEST_F(PictureLayerImplTest, HighResReadyToDrawEnoughToActivate) {
2927   gfx::Size tile_size(100, 100);
2928   gfx::Size layer_bounds(1000, 1000);
2929
2930   SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2931
2932   // Make sure some tiles are not shared.
2933   pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2934
2935   CreateHighLowResAndSetAllTilesVisible();
2936   active_layer_->SetAllTilesReady();
2937   pending_layer_->MarkVisibleResourcesAsRequired();
2938
2939   // All pending layer tiles required are not ready.
2940   EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2941
2942   // Initialize all high-res tiles.
2943   pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
2944
2945   // High-res tiles should be enough, since they cover everything visible.
2946   EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2947 }
2948
2949 TEST_F(PictureLayerImplTest,
2950        SharedActiveHighResReadyAndPendingLowResReadyNotEnoughToActivate) {
2951   gfx::Size tile_size(100, 100);
2952   gfx::Size layer_bounds(1000, 1000);
2953
2954   SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2955
2956   // Make sure some tiles are not shared.
2957   pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2958
2959   CreateHighLowResAndSetAllTilesVisible();
2960
2961   // Initialize all high-res tiles in the active layer.
2962   active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
2963   // And all the low-res tiles in the pending layer.
2964   pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
2965
2966   pending_layer_->MarkVisibleResourcesAsRequired();
2967
2968   // The unshared high-res tiles are not ready, so we cannot activate.
2969   EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2970
2971   // When the unshared pending high-res tiles are ready, we can activate.
2972   pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
2973   EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2974 }
2975
2976 TEST_F(PictureLayerImplTest, SharedActiveHighResReadyNotEnoughToActivate) {
2977   gfx::Size tile_size(100, 100);
2978   gfx::Size layer_bounds(1000, 1000);
2979
2980   SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2981
2982   // Make sure some tiles are not shared.
2983   pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2984
2985   CreateHighLowResAndSetAllTilesVisible();
2986
2987   // Initialize all high-res tiles in the active layer.
2988   active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
2989
2990   pending_layer_->MarkVisibleResourcesAsRequired();
2991
2992   // The unshared high-res tiles are not ready, so we cannot activate.
2993   EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2994
2995   // When the unshared pending high-res tiles are ready, we can activate.
2996   pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
2997   EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2998 }
2999
3000 class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
3001  public:
3002   NoLowResPictureLayerImplTest()
3003       : PictureLayerImplTest(NoLowResTilingsSettings()) {}
3004 };
3005
3006 TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
3007   gfx::Size tile_size(400, 400);
3008   gfx::Size layer_bounds(1300, 1900);
3009
3010   scoped_refptr<FakePicturePileImpl> pending_pile =
3011       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3012   scoped_refptr<FakePicturePileImpl> active_pile =
3013       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3014
3015   SetupTrees(pending_pile, active_pile);
3016   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3017
3018   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3019   EXPECT_LT(low_res_factor, 1.f);
3020
3021   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3022                                     6.f,  // ideal contents scale
3023                                     3.f,  // device scale
3024                                     2.f,  // page scale
3025                                     1.f,  // maximum animation scale
3026                                     false);
3027   ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3028   EXPECT_FLOAT_EQ(6.f,
3029                   pending_layer_->tilings()->tiling_at(0)->contents_scale());
3030
3031   // If we change the page scale factor, then we should get new tilings.
3032   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3033                                     6.6f,  // ideal contents scale
3034                                     3.f,   // device scale
3035                                     2.2f,  // page scale
3036                                     1.f,   // maximum animation scale
3037                                     false);
3038   ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
3039   EXPECT_FLOAT_EQ(6.6f,
3040                   pending_layer_->tilings()->tiling_at(0)->contents_scale());
3041
3042   // If we change the device scale factor, then we should get new tilings.
3043   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3044                                     7.26f,  // ideal contents scale
3045                                     3.3f,   // device scale
3046                                     2.2f,   // page scale
3047                                     1.f,    // maximum animation scale
3048                                     false);
3049   ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
3050   EXPECT_FLOAT_EQ(7.26f,
3051                   pending_layer_->tilings()->tiling_at(0)->contents_scale());
3052
3053   // If we change the device scale factor, but end up at the same total scale
3054   // factor somehow, then we don't get new tilings.
3055   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3056                                     7.26f,  // ideal contents scale
3057                                     2.2f,   // device scale
3058                                     3.3f,   // page scale
3059                                     1.f,    // maximum animation scale
3060                                     false);
3061   ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
3062   EXPECT_FLOAT_EQ(7.26f,
3063                   pending_layer_->tilings()->tiling_at(0)->contents_scale());
3064 }
3065
3066 TEST_F(NoLowResPictureLayerImplTest, MarkRequiredNullTiles) {
3067   gfx::Size tile_size(100, 100);
3068   gfx::Size layer_bounds(1000, 1000);
3069
3070   scoped_refptr<FakePicturePileImpl> pending_pile =
3071       FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
3072   // Layers with entirely empty piles can't get tilings.
3073   pending_pile->AddRecordingAt(0, 0);
3074
3075   SetupPendingTree(pending_pile);
3076
3077   ASSERT_TRUE(pending_layer_->CanHaveTilings());
3078   pending_layer_->AddTiling(1.0f);
3079   pending_layer_->AddTiling(2.0f);
3080
3081   // It should be safe to call this (and MarkVisibleResourcesAsRequired)
3082   // on a layer with no recordings.
3083   host_impl_.pending_tree()->UpdateDrawProperties();
3084   pending_layer_->MarkVisibleResourcesAsRequired();
3085 }
3086
3087 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
3088   gfx::Size layer_bounds(400, 400);
3089   gfx::Size tile_size(100, 100);
3090   SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3091
3092   CreateHighLowResAndSetAllTilesVisible();
3093
3094   Tile* some_active_tile =
3095       active_layer_->HighResTiling()->AllTilesForTesting()[0];
3096   EXPECT_FALSE(some_active_tile->IsReadyToDraw());
3097
3098   // All tiles shared (no invalidation), so even though the active tree's
3099   // tiles aren't ready, there is nothing required.
3100   pending_layer_->MarkVisibleResourcesAsRequired();
3101   AssertNoTilesRequired(pending_layer_->HighResTiling());
3102   if (host_impl_.settings().create_low_res_tiling) {
3103     AssertNoTilesRequired(pending_layer_->LowResTiling());
3104   }
3105 }
3106
3107 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
3108   gfx::Size layer_bounds(400, 400);
3109   gfx::Size tile_size(100, 100);
3110   scoped_refptr<FakePicturePileImpl> pending_pile =
3111       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3112   // This pile will create tilings, but has no recordings so will not create any
3113   // tiles.  This is attempting to simulate scrolling past the end of recorded
3114   // content on the active layer, where the recordings are so far away that
3115   // no tiles are created.
3116   scoped_refptr<FakePicturePileImpl> active_pile =
3117       FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
3118           tile_size, layer_bounds);
3119   SetupTrees(pending_pile, active_pile);
3120   pending_layer_->set_fixed_tile_size(tile_size);
3121   active_layer_->set_fixed_tile_size(tile_size);
3122
3123   CreateHighLowResAndSetAllTilesVisible();
3124
3125   // Active layer has tilings, but no tiles due to missing recordings.
3126   EXPECT_TRUE(active_layer_->CanHaveTilings());
3127   EXPECT_EQ(active_layer_->tilings()->num_tilings(),
3128             host_impl_.settings().create_low_res_tiling ? 2u : 1u);
3129   EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
3130
3131   // Since the active layer has no tiles at all, the pending layer doesn't
3132   // need content in order to activate.
3133   pending_layer_->MarkVisibleResourcesAsRequired();
3134   AssertNoTilesRequired(pending_layer_->HighResTiling());
3135   if (host_impl_.settings().create_low_res_tiling)
3136     AssertNoTilesRequired(pending_layer_->LowResTiling());
3137 }
3138
3139 TEST_F(NoLowResPictureLayerImplTest,
3140        ResourcelessSoftwareDrawHasValidViewportForTilePriority) {
3141   base::TimeTicks time_ticks;
3142   time_ticks += base::TimeDelta::FromMilliseconds(1);
3143   host_impl_.SetCurrentBeginFrameArgs(
3144       CreateBeginFrameArgsForTesting(time_ticks));
3145
3146   gfx::Size tile_size(100, 100);
3147   gfx::Size layer_bounds(400, 400);
3148
3149   scoped_refptr<FakePicturePileImpl> pending_pile =
3150       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3151   scoped_refptr<FakePicturePileImpl> active_pile =
3152       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3153
3154   SetupTrees(pending_pile, active_pile);
3155
3156   Region invalidation;
3157   AddDefaultTilingsWithInvalidation(invalidation);
3158   SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
3159
3160   // UpdateTiles with valid viewport. Should update tile viewport.
3161   bool resourceless_software_draw = false;
3162   gfx::Rect viewport = gfx::Rect(layer_bounds);
3163   gfx::Transform transform;
3164   host_impl_.SetExternalDrawConstraints(transform,
3165                                         viewport,
3166                                         viewport,
3167                                         viewport,
3168                                         transform,
3169                                         resourceless_software_draw);
3170   active_layer_->draw_properties().visible_content_rect = viewport;
3171   active_layer_->draw_properties().screen_space_transform = transform;
3172   active_layer_->UpdateTiles(Occlusion());
3173
3174   gfx::Rect visible_rect_for_tile_priority =
3175       active_layer_->visible_rect_for_tile_priority();
3176   EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
3177   gfx::Rect viewport_rect_for_tile_priority =
3178       active_layer_->viewport_rect_for_tile_priority();
3179   EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
3180   gfx::Transform screen_space_transform_for_tile_priority =
3181       active_layer_->screen_space_transform_for_tile_priority();
3182
3183   // PictureLayerImpl does not make a special case for
3184   // resource_less_software_draw, so the tile viewport and matrix should be
3185   // respected.
3186   time_ticks += base::TimeDelta::FromMilliseconds(200);
3187   host_impl_.SetCurrentBeginFrameArgs(
3188       CreateBeginFrameArgsForTesting(time_ticks));
3189   resourceless_software_draw = true;
3190   viewport = gfx::ScaleToEnclosingRect(viewport, 2);
3191   transform.Translate(1.f, 1.f);
3192   active_layer_->draw_properties().visible_content_rect = viewport;
3193   active_layer_->draw_properties().screen_space_transform = transform;
3194   host_impl_.SetExternalDrawConstraints(transform,
3195                                         viewport,
3196                                         viewport,
3197                                         viewport,
3198                                         transform,
3199                                         resourceless_software_draw);
3200   active_layer_->UpdateTiles(Occlusion());
3201
3202   visible_rect_for_tile_priority =
3203       gfx::ScaleToEnclosingRect(visible_rect_for_tile_priority, 2);
3204   viewport_rect_for_tile_priority =
3205       gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority, 2);
3206   screen_space_transform_for_tile_priority = transform;
3207
3208   EXPECT_RECT_EQ(visible_rect_for_tile_priority,
3209                  active_layer_->visible_rect_for_tile_priority());
3210   EXPECT_RECT_EQ(viewport_rect_for_tile_priority,
3211                  active_layer_->viewport_rect_for_tile_priority());
3212   EXPECT_TRANSFORMATION_MATRIX_EQ(
3213       screen_space_transform_for_tile_priority,
3214       active_layer_->screen_space_transform_for_tile_priority());
3215 }
3216
3217 TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
3218   gfx::Size tile_size(400, 400);
3219   gfx::Size layer_bounds(1300, 1900);
3220
3221   scoped_refptr<FakePicturePileImpl> pending_pile =
3222       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3223   scoped_refptr<FakePicturePileImpl> active_pile =
3224       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3225
3226   std::vector<PictureLayerTiling*> used_tilings;
3227
3228   SetupTrees(pending_pile, active_pile);
3229   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3230
3231   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3232   EXPECT_LT(low_res_factor, 1.f);
3233
3234   float device_scale = 1.7f;
3235   float page_scale = 3.2f;
3236   float scale = 1.f;
3237
3238   SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3239   ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3240
3241   // We only have ideal tilings, so they aren't removed.
3242   used_tilings.clear();
3243   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3244   ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3245
3246   host_impl_.PinchGestureBegin();
3247
3248   // Changing the ideal but not creating new tilings.
3249   scale *= 1.5f;
3250   page_scale *= 1.5f;
3251   SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3252   ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3253
3254   // The tilings are still our target scale, so they aren't removed.
3255   used_tilings.clear();
3256   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3257   ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3258
3259   host_impl_.PinchGestureEnd();
3260
3261   // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
3262   scale /= 4.f;
3263   page_scale /= 4.f;
3264   SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
3265   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3266   EXPECT_FLOAT_EQ(1.f,
3267                   active_layer_->tilings()->tiling_at(1)->contents_scale());
3268
3269   // Mark the non-ideal tilings as used. They won't be removed.
3270   used_tilings.clear();
3271   used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3272   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3273   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3274
3275   // Now move the ideal scale to 0.5. Our target stays 1.2.
3276   SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
3277
3278   // The high resolution tiling is between target and ideal, so is not
3279   // removed.  The low res tiling for the old ideal=1.0 scale is removed.
3280   used_tilings.clear();
3281   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3282   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3283
3284   // Now move the ideal scale to 1.0. Our target stays 1.2.
3285   SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
3286
3287   // All the tilings are between are target and the ideal, so they are not
3288   // removed.
3289   used_tilings.clear();
3290   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3291   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3292
3293   // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
3294   SetupDrawPropertiesAndUpdateTiles(
3295       active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3296
3297   // Because the pending layer's ideal scale is still 1.0, our tilings fall
3298   // in the range [1.0,1.2] and are kept.
3299   used_tilings.clear();
3300   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3301   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3302
3303   // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
3304   // 1.2 still.
3305   SetupDrawPropertiesAndUpdateTiles(
3306       pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3307
3308   // Our 1.0 tiling now falls outside the range between our ideal scale and our
3309   // target raster scale. But it is in our used tilings set, so nothing is
3310   // deleted.
3311   used_tilings.clear();
3312   used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3313   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3314   ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3315
3316   // If we remove it from our used tilings set, it is outside the range to keep
3317   // so it is deleted.
3318   used_tilings.clear();
3319   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3320   ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3321 }
3322
3323 TEST_F(PictureLayerImplTest, ScaleCollision) {
3324   gfx::Size tile_size(400, 400);
3325   gfx::Size layer_bounds(1300, 1900);
3326
3327   scoped_refptr<FakePicturePileImpl> pending_pile =
3328       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3329   scoped_refptr<FakePicturePileImpl> active_pile =
3330       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3331
3332   std::vector<PictureLayerTiling*> used_tilings;
3333
3334   SetupTrees(pending_pile, active_pile);
3335
3336   float pending_contents_scale = 1.f;
3337   float active_contents_scale = 2.f;
3338   float device_scale_factor = 1.f;
3339   float page_scale_factor = 1.f;
3340   float maximum_animation_contents_scale = 1.f;
3341   bool animating_transform = false;
3342
3343   EXPECT_TRUE(host_impl_.settings().create_low_res_tiling);
3344   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3345   EXPECT_LT(low_res_factor, 1.f);
3346
3347   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3348                                     pending_contents_scale,
3349                                     device_scale_factor,
3350                                     page_scale_factor,
3351                                     maximum_animation_contents_scale,
3352                                     animating_transform);
3353   SetupDrawPropertiesAndUpdateTiles(active_layer_,
3354                                     active_contents_scale,
3355                                     device_scale_factor,
3356                                     page_scale_factor,
3357                                     maximum_animation_contents_scale,
3358                                     animating_transform);
3359
3360   ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
3361   ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3362
3363   EXPECT_EQ(active_contents_scale,
3364             pending_layer_->tilings()->tiling_at(0)->contents_scale());
3365   EXPECT_EQ(pending_contents_scale,
3366             pending_layer_->tilings()->tiling_at(1)->contents_scale());
3367   EXPECT_EQ(active_contents_scale * low_res_factor,
3368             pending_layer_->tilings()->tiling_at(2)->contents_scale());
3369   EXPECT_EQ(pending_contents_scale * low_res_factor,
3370             pending_layer_->tilings()->tiling_at(3)->contents_scale());
3371
3372   EXPECT_EQ(active_contents_scale,
3373             active_layer_->tilings()->tiling_at(0)->contents_scale());
3374   EXPECT_EQ(pending_contents_scale,
3375             active_layer_->tilings()->tiling_at(1)->contents_scale());
3376   EXPECT_EQ(active_contents_scale * low_res_factor,
3377             active_layer_->tilings()->tiling_at(2)->contents_scale());
3378   EXPECT_EQ(pending_contents_scale * low_res_factor,
3379             active_layer_->tilings()->tiling_at(3)->contents_scale());
3380
3381   // The unused low res tiling from the pending tree must be kept or we may add
3382   // it again on the active tree and collide with the pending tree.
3383   used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3384   active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3385   ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3386
3387   EXPECT_EQ(active_contents_scale,
3388             active_layer_->tilings()->tiling_at(0)->contents_scale());
3389   EXPECT_EQ(pending_contents_scale,
3390             active_layer_->tilings()->tiling_at(1)->contents_scale());
3391   EXPECT_EQ(active_contents_scale * low_res_factor,
3392             active_layer_->tilings()->tiling_at(2)->contents_scale());
3393   EXPECT_EQ(pending_contents_scale * low_res_factor,
3394             active_layer_->tilings()->tiling_at(3)->contents_scale());
3395 }
3396
3397 TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
3398   gfx::Size tile_size(400, 400);
3399   gfx::Size layer_bounds(1300, 1900);
3400
3401   scoped_refptr<FakePicturePileImpl> pending_pile =
3402       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3403   scoped_refptr<FakePicturePileImpl> active_pile =
3404       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3405
3406   SetupTrees(pending_pile, active_pile);
3407   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3408
3409   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3410                                     1.3f,  // ideal contents scale
3411                                     2.7f,  // device scale
3412                                     3.2f,  // page scale
3413                                     1.f,   // maximum animation scale
3414                                     false);
3415   EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3416
3417   // All tilings should be removed when losing output surface.
3418   active_layer_->ReleaseResources();
3419   EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
3420   pending_layer_->ReleaseResources();
3421   EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3422
3423   // This should create new tilings.
3424   SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3425                                     1.3f,  // ideal contents scale
3426                                     2.7f,  // device scale
3427                                     3.2f,  // page scale
3428                                     1.f,   // maximum animation scale
3429                                     false);
3430   EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3431 }
3432
3433 TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3434   MockOcclusionTracker<LayerImpl> occlusion_tracker;
3435   scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3436
3437   gfx::Size tile_size(400, 400);
3438   gfx::Size layer_bounds(1000, 2000);
3439
3440   scoped_refptr<FakePicturePileImpl> pending_pile =
3441       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3442   scoped_refptr<FakePicturePileImpl> active_pile =
3443       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3444
3445   SetupTrees(pending_pile, active_pile);
3446
3447   SetupDrawPropertiesAndUpdateTiles(pending_layer_, 2.5f, 1.f, 1.f, 1.f, false);
3448   host_impl_.pending_tree()->UpdateDrawProperties();
3449
3450   active_layer_->draw_properties().visible_content_rect =
3451       gfx::Rect(layer_bounds);
3452   host_impl_.active_tree()->UpdateDrawProperties();
3453
3454   float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3455   gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3456   scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3457                               SK_MScalar1 / max_contents_scale);
3458
3459   AppendQuadsData data;
3460   active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3461
3462   // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3463   EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
3464   // The content_to_target_transform should be scaled by the
3465   // MaximumTilingContentsScale on the layer.
3466   EXPECT_EQ(scaled_draw_transform.ToString(),
3467             render_pass->shared_quad_state_list[0]
3468                 ->content_to_target_transform.ToString());
3469   // The content_bounds should be scaled by the
3470   // MaximumTilingContentsScale on the layer.
3471   EXPECT_EQ(gfx::Size(2500u, 5000u).ToString(),
3472             render_pass->shared_quad_state_list[0]->content_bounds.ToString());
3473   // The visible_content_rect should be scaled by the
3474   // MaximumTilingContentsScale on the layer.
3475   EXPECT_EQ(
3476       gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3477       render_pass->shared_quad_state_list[0]->visible_content_rect.ToString());
3478 }
3479
3480 TEST_F(PictureLayerImplTest, UpdateTilesForMasksWithNoVisibleContent) {
3481   gfx::Size tile_size(400, 400);
3482   gfx::Size bounds(100000, 100);
3483
3484   host_impl_.CreatePendingTree();
3485
3486   scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_.pending_tree(), 1);
3487
3488   scoped_ptr<FakePictureLayerImpl> layer_with_mask =
3489       FakePictureLayerImpl::Create(host_impl_.pending_tree(), 2);
3490
3491   layer_with_mask->SetBounds(bounds);
3492   layer_with_mask->SetContentBounds(bounds);
3493
3494   scoped_refptr<FakePicturePileImpl> pending_pile =
3495       FakePicturePileImpl::CreateFilledPile(tile_size, bounds);
3496   pending_pile->set_is_mask(true);
3497   scoped_ptr<FakePictureLayerImpl> mask = FakePictureLayerImpl::CreateWithPile(
3498       host_impl_.pending_tree(), 3, pending_pile);
3499
3500   mask->SetBounds(bounds);
3501   mask->SetContentBounds(bounds);
3502   mask->SetDrawsContent(true);
3503
3504   FakePictureLayerImpl* pending_mask_content = mask.get();
3505   layer_with_mask->SetMaskLayer(mask.PassAs<LayerImpl>());
3506
3507   scoped_ptr<FakePictureLayerImpl> child_of_layer_with_mask =
3508       FakePictureLayerImpl::Create(host_impl_.pending_tree(), 4);
3509
3510   child_of_layer_with_mask->SetBounds(bounds);
3511   child_of_layer_with_mask->SetContentBounds(bounds);
3512   child_of_layer_with_mask->SetDrawsContent(true);
3513
3514   layer_with_mask->AddChild(child_of_layer_with_mask.PassAs<LayerImpl>());
3515
3516   root->AddChild(layer_with_mask.PassAs<LayerImpl>());
3517
3518   host_impl_.pending_tree()->SetRootLayer(root.Pass());
3519
3520   EXPECT_FALSE(pending_mask_content->tilings());
3521   host_impl_.pending_tree()->UpdateDrawProperties();
3522   EXPECT_NE(0u, pending_mask_content->num_tilings());
3523 }
3524
3525 class PictureLayerImplTestWithDelegatingRenderer : public PictureLayerImplTest {
3526  public:
3527   PictureLayerImplTestWithDelegatingRenderer() : PictureLayerImplTest() {}
3528
3529   virtual void InitializeRenderer() OVERRIDE {
3530     host_impl_.InitializeRenderer(
3531         FakeOutputSurface::CreateDelegating3d().PassAs<OutputSurface>());
3532   }
3533 };
3534
3535 TEST_F(PictureLayerImplTestWithDelegatingRenderer,
3536        DelegatingRendererWithTileOOM) {
3537   // This test is added for crbug.com/402321, where quad should be produced when
3538   // raster on demand is not allowed and tile is OOM.
3539   gfx::Size tile_size = host_impl_.settings().default_tile_size;
3540   gfx::Size layer_bounds(1000, 1000);
3541
3542   // Create tiles.
3543   scoped_refptr<FakePicturePileImpl> pending_pile =
3544       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3545   SetupPendingTree(pending_pile);
3546   pending_layer_->SetBounds(layer_bounds);
3547   host_impl_.SetViewportSize(layer_bounds);
3548   ActivateTree();
3549   host_impl_.active_tree()->UpdateDrawProperties();
3550   std::vector<Tile*> tiles =
3551       active_layer_->HighResTiling()->AllTilesForTesting();
3552   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3553
3554   // Force tiles after max_tiles to be OOM. TileManager uses
3555   // GlobalStateThatImpactsTilesPriority from LayerTreeHostImpl, and we cannot
3556   // directly set state to host_impl_, so we set policy that would change the
3557   // state. We also need to update tree priority separately.
3558   GlobalStateThatImpactsTilePriority state;
3559   size_t max_tiles = 1;
3560   size_t memory_limit = max_tiles * 4 * tile_size.width() * tile_size.height();
3561   size_t resource_limit = max_tiles;
3562   ManagedMemoryPolicy policy(memory_limit,
3563                              gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
3564                              resource_limit);
3565   host_impl_.SetMemoryPolicy(policy);
3566   host_impl_.SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
3567   host_impl_.ManageTiles();
3568
3569   MockOcclusionTracker<LayerImpl> occlusion_tracker;
3570   scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3571   AppendQuadsData data;
3572   active_layer_->WillDraw(DRAW_MODE_HARDWARE, NULL);
3573   active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3574   active_layer_->DidDraw(NULL);
3575
3576   // Even when OOM, quads should be produced, and should be different material
3577   // from quads with resource.
3578   EXPECT_LT(max_tiles, render_pass->quad_list.size());
3579   EXPECT_EQ(DrawQuad::Material::TILED_CONTENT,
3580             render_pass->quad_list.front()->material);
3581   EXPECT_EQ(DrawQuad::Material::SOLID_COLOR,
3582             render_pass->quad_list.back()->material);
3583 }
3584
3585 class OcclusionTrackingSettings : public LowResTilingsSettings {
3586  public:
3587   OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
3588 };
3589
3590 class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
3591  public:
3592   OcclusionTrackingPictureLayerImplTest()
3593       : PictureLayerImplTest(OcclusionTrackingSettings()) {}
3594
3595   void VerifyEvictionConsidersOcclusion(
3596       PictureLayerImpl* layer,
3597       size_t expected_occluded_tile_count[NUM_TREE_PRIORITIES]) {
3598     for (int priority_count = 0; priority_count < NUM_TREE_PRIORITIES;
3599          ++priority_count) {
3600       TreePriority tree_priority = static_cast<TreePriority>(priority_count);
3601       size_t occluded_tile_count = 0u;
3602       Tile* last_tile = NULL;
3603
3604       for (PictureLayerImpl::LayerEvictionTileIterator it =
3605                PictureLayerImpl::LayerEvictionTileIterator(layer,
3606                                                            tree_priority);
3607            it;
3608            ++it) {
3609         Tile* tile = *it;
3610         if (!last_tile)
3611           last_tile = tile;
3612
3613         // The only way we will encounter an occluded tile after an unoccluded
3614         // tile is if the priorty bin decreased, the tile is required for
3615         // activation, or the scale changed.
3616         bool tile_is_occluded =
3617             tile->is_occluded_for_tree_priority(tree_priority);
3618         if (tile_is_occluded) {
3619           occluded_tile_count++;
3620
3621           bool last_tile_is_occluded =
3622               last_tile->is_occluded_for_tree_priority(tree_priority);
3623           if (!last_tile_is_occluded) {
3624             TilePriority::PriorityBin tile_priority_bin =
3625                 tile->priority_for_tree_priority(tree_priority).priority_bin;
3626             TilePriority::PriorityBin last_tile_priority_bin =
3627                 last_tile->priority_for_tree_priority(tree_priority)
3628                     .priority_bin;
3629
3630             EXPECT_TRUE(
3631                 (tile_priority_bin < last_tile_priority_bin) ||
3632                 tile->required_for_activation() ||
3633                 (tile->contents_scale() != last_tile->contents_scale()));
3634           }
3635         }
3636         last_tile = tile;
3637       }
3638       EXPECT_EQ(expected_occluded_tile_count[priority_count],
3639                 occluded_tile_count);
3640     }
3641   }
3642 };
3643
3644 TEST_F(OcclusionTrackingPictureLayerImplTest,
3645        OccludedTilesSkippedDuringRasterization) {
3646   base::TimeTicks time_ticks;
3647   time_ticks += base::TimeDelta::FromMilliseconds(1);
3648   host_impl_.SetCurrentBeginFrameArgs(
3649       CreateBeginFrameArgsForTesting(time_ticks));
3650
3651   gfx::Size tile_size(102, 102);
3652   gfx::Size layer_bounds(1000, 1000);
3653   gfx::Size viewport_size(500, 500);
3654   gfx::Point occluding_layer_position(310, 0);
3655
3656   scoped_refptr<FakePicturePileImpl> pending_pile =
3657       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3658   SetupPendingTree(pending_pile);
3659   pending_layer_->set_fixed_tile_size(tile_size);
3660
3661   host_impl_.SetViewportSize(viewport_size);
3662   host_impl_.pending_tree()->UpdateDrawProperties();
3663
3664   // No occlusion.
3665   int unoccluded_tile_count = 0;
3666   for (PictureLayerImpl::LayerRasterTileIterator it =
3667            PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3668        it;
3669        ++it) {
3670     Tile* tile = *it;
3671
3672     // Occluded tiles should not be iterated over.
3673     EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3674
3675     // Some tiles may not be visible (i.e. outside the viewport). The rest are
3676     // visible and at least partially unoccluded, verified by the above expect.
3677     bool tile_is_visible =
3678         tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3679     if (tile_is_visible)
3680       unoccluded_tile_count++;
3681   }
3682   EXPECT_EQ(unoccluded_tile_count, 25 + 4);
3683
3684   // Partial occlusion.
3685   pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3686   LayerImpl* layer1 = pending_layer_->children()[0];
3687   layer1->SetBounds(layer_bounds);
3688   layer1->SetContentBounds(layer_bounds);
3689   layer1->SetDrawsContent(true);
3690   layer1->SetContentsOpaque(true);
3691   layer1->SetPosition(occluding_layer_position);
3692
3693   time_ticks += base::TimeDelta::FromMilliseconds(200);
3694   host_impl_.SetCurrentBeginFrameArgs(
3695       CreateBeginFrameArgsForTesting(time_ticks));
3696   host_impl_.pending_tree()->UpdateDrawProperties();
3697
3698   unoccluded_tile_count = 0;
3699   for (PictureLayerImpl::LayerRasterTileIterator it =
3700            PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3701        it;
3702        ++it) {
3703     Tile* tile = *it;
3704
3705     EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3706
3707     bool tile_is_visible =
3708         tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3709     if (tile_is_visible)
3710       unoccluded_tile_count++;
3711   }
3712   EXPECT_EQ(unoccluded_tile_count, 20 + 2);
3713
3714   // Full occlusion.
3715   layer1->SetPosition(gfx::Point(0, 0));
3716
3717   time_ticks += base::TimeDelta::FromMilliseconds(200);
3718   host_impl_.SetCurrentBeginFrameArgs(
3719       CreateBeginFrameArgsForTesting(time_ticks));
3720   host_impl_.pending_tree()->UpdateDrawProperties();
3721
3722   unoccluded_tile_count = 0;
3723   for (PictureLayerImpl::LayerRasterTileIterator it =
3724            PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3725        it;
3726        ++it) {
3727     Tile* tile = *it;
3728
3729     EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3730
3731     bool tile_is_visible =
3732         tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3733     if (tile_is_visible)
3734       unoccluded_tile_count++;
3735   }
3736   EXPECT_EQ(unoccluded_tile_count, 0);
3737 }
3738
3739 TEST_F(OcclusionTrackingPictureLayerImplTest,
3740        OccludedTilesNotMarkedAsRequired) {
3741   base::TimeTicks time_ticks;
3742   time_ticks += base::TimeDelta::FromMilliseconds(1);
3743   host_impl_.SetCurrentBeginFrameArgs(
3744       CreateBeginFrameArgsForTesting(time_ticks));
3745
3746   gfx::Size tile_size(102, 102);
3747   gfx::Size layer_bounds(1000, 1000);
3748   gfx::Size viewport_size(500, 500);
3749   gfx::Point occluding_layer_position(310, 0);
3750
3751   scoped_refptr<FakePicturePileImpl> pending_pile =
3752       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3753   SetupPendingTree(pending_pile);
3754   pending_layer_->set_fixed_tile_size(tile_size);
3755
3756   host_impl_.SetViewportSize(viewport_size);
3757   host_impl_.pending_tree()->UpdateDrawProperties();
3758
3759   // No occlusion.
3760   int occluded_tile_count = 0;
3761   for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3762     PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3763
3764     occluded_tile_count = 0;
3765     for (PictureLayerTiling::CoverageIterator iter(
3766              tiling,
3767              pending_layer_->contents_scale_x(),
3768              gfx::Rect(layer_bounds));
3769          iter;
3770          ++iter) {
3771       if (!*iter)
3772         continue;
3773       const Tile* tile = *iter;
3774
3775       // Fully occluded tiles are not required for activation.
3776       if (tile->is_occluded(PENDING_TREE)) {
3777         EXPECT_FALSE(tile->required_for_activation());
3778         occluded_tile_count++;
3779       }
3780     }
3781     EXPECT_EQ(occluded_tile_count, 0);
3782   }
3783
3784   // Partial occlusion.
3785   pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3786   LayerImpl* layer1 = pending_layer_->children()[0];
3787   layer1->SetBounds(layer_bounds);
3788   layer1->SetContentBounds(layer_bounds);
3789   layer1->SetDrawsContent(true);
3790   layer1->SetContentsOpaque(true);
3791   layer1->SetPosition(occluding_layer_position);
3792
3793   time_ticks += base::TimeDelta::FromMilliseconds(200);
3794   host_impl_.SetCurrentBeginFrameArgs(
3795       CreateBeginFrameArgsForTesting(time_ticks));
3796   host_impl_.pending_tree()->UpdateDrawProperties();
3797
3798   for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3799     PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3800
3801     occluded_tile_count = 0;
3802     for (PictureLayerTiling::CoverageIterator iter(
3803              tiling,
3804              pending_layer_->contents_scale_x(),
3805              gfx::Rect(layer_bounds));
3806          iter;
3807          ++iter) {
3808       if (!*iter)
3809         continue;
3810       const Tile* tile = *iter;
3811
3812       if (tile->is_occluded(PENDING_TREE)) {
3813         EXPECT_FALSE(tile->required_for_activation());
3814         occluded_tile_count++;
3815       }
3816     }
3817     switch (i) {
3818       case 0:
3819         EXPECT_EQ(occluded_tile_count, 5);
3820         break;
3821       case 1:
3822         EXPECT_EQ(occluded_tile_count, 2);
3823         break;
3824       default:
3825         NOTREACHED();
3826     }
3827   }
3828
3829   // Full occlusion.
3830   layer1->SetPosition(gfx::PointF(0, 0));
3831
3832   time_ticks += base::TimeDelta::FromMilliseconds(200);
3833   host_impl_.SetCurrentBeginFrameArgs(
3834       CreateBeginFrameArgsForTesting(time_ticks));
3835   host_impl_.pending_tree()->UpdateDrawProperties();
3836
3837   for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3838     PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3839
3840     occluded_tile_count = 0;
3841     for (PictureLayerTiling::CoverageIterator iter(
3842              tiling,
3843              pending_layer_->contents_scale_x(),
3844              gfx::Rect(layer_bounds));
3845          iter;
3846          ++iter) {
3847       if (!*iter)
3848         continue;
3849       const Tile* tile = *iter;
3850
3851       if (tile->is_occluded(PENDING_TREE)) {
3852         EXPECT_FALSE(tile->required_for_activation());
3853         occluded_tile_count++;
3854       }
3855     }
3856     switch (i) {
3857       case 0:
3858         EXPECT_EQ(occluded_tile_count, 25);
3859         break;
3860       case 1:
3861         EXPECT_EQ(occluded_tile_count, 4);
3862         break;
3863       default:
3864         NOTREACHED();
3865     }
3866   }
3867 }
3868
3869 TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
3870   gfx::Size tile_size(102, 102);
3871   gfx::Size layer_bounds(1000, 1000);
3872   gfx::Size viewport_size(500, 500);
3873   gfx::Point occluding_layer_position(310, 0);
3874
3875   scoped_refptr<FakePicturePileImpl> pending_pile =
3876       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3877   SetupPendingTree(pending_pile);
3878   pending_layer_->set_fixed_tile_size(tile_size);
3879
3880   ASSERT_TRUE(pending_layer_->CanHaveTilings());
3881
3882   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3883
3884   std::vector<PictureLayerTiling*> tilings;
3885   tilings.push_back(pending_layer_->AddTiling(low_res_factor));
3886   tilings.push_back(pending_layer_->AddTiling(0.3f));
3887   tilings.push_back(pending_layer_->AddTiling(0.7f));
3888   tilings.push_back(pending_layer_->AddTiling(1.0f));
3889   tilings.push_back(pending_layer_->AddTiling(2.0f));
3890
3891   pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3892   LayerImpl* layer1 = pending_layer_->children()[0];
3893   layer1->SetBounds(layer_bounds);
3894   layer1->SetContentBounds(layer_bounds);
3895   layer1->SetDrawsContent(true);
3896   layer1->SetContentsOpaque(true);
3897   layer1->SetPosition(occluding_layer_position);
3898
3899   host_impl_.SetViewportSize(viewport_size);
3900   host_impl_.pending_tree()->UpdateDrawProperties();
3901
3902   int tiling_count = 0;
3903   int occluded_tile_count = 0;
3904   for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
3905            tilings.begin();
3906        tiling_iterator != tilings.end();
3907        ++tiling_iterator) {
3908     std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
3909
3910     occluded_tile_count = 0;
3911     for (size_t i = 0; i < tiles.size(); ++i) {
3912       if (tiles[i]->is_occluded(PENDING_TREE)) {
3913         gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3914             tiles[i]->content_rect(), 1.0f / tiles[i]->contents_scale());
3915         EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
3916         occluded_tile_count++;
3917       }
3918     }
3919     switch (tiling_count) {
3920       case 0:
3921       case 1:
3922         EXPECT_EQ(occluded_tile_count, 2);
3923         break;
3924       case 2:
3925         EXPECT_EQ(occluded_tile_count, 4);
3926         break;
3927       case 3:
3928         EXPECT_EQ(occluded_tile_count, 5);
3929         break;
3930       case 4:
3931         EXPECT_EQ(occluded_tile_count, 30);
3932         break;
3933       default:
3934         NOTREACHED();
3935     }
3936
3937     tiling_count++;
3938   }
3939
3940   EXPECT_EQ(tiling_count, 5);
3941 }
3942
3943 TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
3944   gfx::Size tile_size(102, 102);
3945   gfx::Size layer_bounds(1000, 1000);
3946   gfx::Size viewport_size(1000, 1000);
3947   gfx::Point occluding_layer_position(310, 0);
3948   gfx::Rect invalidation_rect(230, 230, 102, 102);
3949
3950   scoped_refptr<FakePicturePileImpl> pending_pile =
3951       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3952   scoped_refptr<FakePicturePileImpl> active_pile =
3953       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3954   SetupTrees(pending_pile, active_pile);
3955
3956   // Partially occlude the active layer.
3957   active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
3958   LayerImpl* layer1 = active_layer_->children()[0];
3959   layer1->SetBounds(layer_bounds);
3960   layer1->SetContentBounds(layer_bounds);
3961   layer1->SetDrawsContent(true);
3962   layer1->SetContentsOpaque(true);
3963   layer1->SetPosition(occluding_layer_position);
3964
3965   // Partially invalidate the pending layer.
3966   pending_layer_->set_invalidation(invalidation_rect);
3967
3968   host_impl_.SetViewportSize(viewport_size);
3969
3970   active_layer_->CreateDefaultTilingsAndTiles();
3971   pending_layer_->CreateDefaultTilingsAndTiles();
3972
3973   for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3974     PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3975
3976     for (PictureLayerTiling::CoverageIterator iter(
3977              tiling,
3978              pending_layer_->contents_scale_x(),
3979              gfx::Rect(layer_bounds));
3980          iter;
3981          ++iter) {
3982       if (!*iter)
3983         continue;
3984       const Tile* tile = *iter;
3985
3986       // All tiles are unoccluded on the pending tree.
3987       EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3988
3989       Tile* twin_tile =
3990           pending_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
3991       gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3992           tile->content_rect(), 1.0f / tile->contents_scale());
3993
3994       if (scaled_content_rect.Intersects(invalidation_rect)) {
3995         // Tiles inside the invalidation rect are only on the pending tree.
3996         EXPECT_NE(tile, twin_tile);
3997
3998         // Unshared tiles should be unoccluded on the active tree by default.
3999         EXPECT_FALSE(tile->is_occluded(ACTIVE_TREE));
4000       } else {
4001         // Tiles outside the invalidation rect are shared between both trees.
4002         EXPECT_EQ(tile, twin_tile);
4003         // Shared tiles are occluded on the active tree iff they lie beneath the
4004         // occluding layer.
4005         EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4006                   scaled_content_rect.x() >= occluding_layer_position.x());
4007       }
4008     }
4009   }
4010
4011   for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4012     PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4013
4014     for (PictureLayerTiling::CoverageIterator iter(
4015              tiling,
4016              active_layer_->contents_scale_x(),
4017              gfx::Rect(layer_bounds));
4018          iter;
4019          ++iter) {
4020       if (!*iter)
4021         continue;
4022       const Tile* tile = *iter;
4023
4024       Tile* twin_tile =
4025           active_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
4026       gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4027           tile->content_rect(), 1.0f / tile->contents_scale());
4028
4029       // Since we've already checked the shared tiles, only consider tiles in
4030       // the invalidation rect.
4031       if (scaled_content_rect.Intersects(invalidation_rect)) {
4032         // Tiles inside the invalidation rect are only on the active tree.
4033         EXPECT_NE(tile, twin_tile);
4034
4035         // Unshared tiles should be unoccluded on the pending tree by default.
4036         EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4037
4038         // Unshared tiles are occluded on the active tree iff they lie beneath
4039         // the occluding layer.
4040         EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4041                   scaled_content_rect.x() >= occluding_layer_position.x());
4042       }
4043     }
4044   }
4045 }
4046
4047 TEST_F(OcclusionTrackingPictureLayerImplTest,
4048        OccludedTilesConsideredDuringEviction) {
4049   gfx::Size tile_size(102, 102);
4050   gfx::Size layer_bounds(1000, 1000);
4051   gfx::Size viewport_size(500, 500);
4052   gfx::Point pending_occluding_layer_position(310, 0);
4053   gfx::Point active_occluding_layer_position(0, 310);
4054   gfx::Rect invalidation_rect(230, 230, 102, 102);
4055
4056   scoped_refptr<FakePicturePileImpl> pending_pile =
4057       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4058   scoped_refptr<FakePicturePileImpl> active_pile =
4059       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4060   SetupTrees(pending_pile, active_pile);
4061
4062   pending_layer_->set_fixed_tile_size(tile_size);
4063   active_layer_->set_fixed_tile_size(tile_size);
4064
4065   float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
4066
4067   std::vector<PictureLayerTiling*> tilings;
4068   tilings.push_back(pending_layer_->AddTiling(low_res_factor));
4069   tilings.push_back(pending_layer_->AddTiling(0.3f));
4070   tilings.push_back(pending_layer_->AddTiling(0.7f));
4071   tilings.push_back(pending_layer_->AddTiling(1.0f));
4072   tilings.push_back(pending_layer_->AddTiling(2.0f));
4073
4074   EXPECT_EQ(5u, pending_layer_->num_tilings());
4075   EXPECT_EQ(5u, active_layer_->num_tilings());
4076
4077   // Partially occlude the pending layer.
4078   pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4079   LayerImpl* pending_occluding_layer = pending_layer_->children()[0];
4080   pending_occluding_layer->SetBounds(layer_bounds);
4081   pending_occluding_layer->SetContentBounds(layer_bounds);
4082   pending_occluding_layer->SetDrawsContent(true);
4083   pending_occluding_layer->SetContentsOpaque(true);
4084   pending_occluding_layer->SetPosition(pending_occluding_layer_position);
4085
4086   // Partially occlude the active layer.
4087   active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
4088   LayerImpl* active_occluding_layer = active_layer_->children()[0];
4089   active_occluding_layer->SetBounds(layer_bounds);
4090   active_occluding_layer->SetContentBounds(layer_bounds);
4091   active_occluding_layer->SetDrawsContent(true);
4092   active_occluding_layer->SetContentsOpaque(true);
4093   active_occluding_layer->SetPosition(active_occluding_layer_position);
4094
4095   // Partially invalidate the pending layer. Tiles inside the invalidation rect
4096   // are not shared between trees.
4097   pending_layer_->set_invalidation(invalidation_rect);
4098
4099   host_impl_.SetViewportSize(viewport_size);
4100   host_impl_.active_tree()->UpdateDrawProperties();
4101   host_impl_.pending_tree()->UpdateDrawProperties();
4102
4103   // The expected number of occluded tiles on each of the 5 tilings for each of
4104   // the 3 tree priorities.
4105   size_t expected_occluded_tile_count_on_both[] = {9u, 1u, 1u, 1u, 1u};
4106   size_t expected_occluded_tile_count_on_active[] = {30u, 5u, 4u, 2u, 2u};
4107   size_t expected_occluded_tile_count_on_pending[] = {30u, 5u, 4u, 2u, 2u};
4108
4109   // The total expected number of occluded tiles on all tilings for each of the
4110   // 3 tree priorities.
4111   size_t total_expected_occluded_tile_count[] = {13u, 43u, 43u};
4112
4113   ASSERT_EQ(arraysize(total_expected_occluded_tile_count), NUM_TREE_PRIORITIES);
4114
4115   // Verify number of occluded tiles on the pending layer for each tiling.
4116   for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4117     PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4118     tiling->CreateAllTilesForTesting();
4119
4120     size_t occluded_tile_count_on_pending = 0u;
4121     size_t occluded_tile_count_on_active = 0u;
4122     size_t occluded_tile_count_on_both = 0u;
4123     for (PictureLayerTiling::CoverageIterator iter(
4124              tiling,
4125              pending_layer_->contents_scale_x(),
4126              gfx::Rect(layer_bounds));
4127          iter;
4128          ++iter) {
4129       Tile* tile = *iter;
4130
4131       if (tile->is_occluded(PENDING_TREE))
4132         occluded_tile_count_on_pending++;
4133       if (tile->is_occluded(ACTIVE_TREE))
4134         occluded_tile_count_on_active++;
4135       if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4136         occluded_tile_count_on_both++;
4137     }
4138     EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4139               occluded_tile_count_on_pending)
4140         << i;
4141     EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4142               occluded_tile_count_on_active)
4143         << i;
4144     EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4145               occluded_tile_count_on_both)
4146         << i;
4147   }
4148
4149   // Verify number of occluded tiles on the active layer for each tiling.
4150   for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4151     PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4152     tiling->CreateAllTilesForTesting();
4153
4154     size_t occluded_tile_count_on_pending = 0u;
4155     size_t occluded_tile_count_on_active = 0u;
4156     size_t occluded_tile_count_on_both = 0u;
4157     for (PictureLayerTiling::CoverageIterator iter(
4158              tiling,
4159              pending_layer_->contents_scale_x(),
4160              gfx::Rect(layer_bounds));
4161          iter;
4162          ++iter) {
4163       Tile* tile = *iter;
4164
4165       if (tile->is_occluded(PENDING_TREE))
4166         occluded_tile_count_on_pending++;
4167       if (tile->is_occluded(ACTIVE_TREE))
4168         occluded_tile_count_on_active++;
4169       if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4170         occluded_tile_count_on_both++;
4171     }
4172     EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4173               occluded_tile_count_on_pending)
4174         << i;
4175     EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4176               occluded_tile_count_on_active)
4177         << i;
4178     EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4179               occluded_tile_count_on_both)
4180         << i;
4181   }
4182
4183   std::vector<Tile*> all_tiles;
4184   for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
4185            tilings.begin();
4186        tiling_iterator != tilings.end();
4187        ++tiling_iterator) {
4188     std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
4189     std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
4190   }
4191
4192   host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
4193
4194   VerifyEvictionConsidersOcclusion(pending_layer_,
4195                                    total_expected_occluded_tile_count);
4196   VerifyEvictionConsidersOcclusion(active_layer_,
4197                                    total_expected_occluded_tile_count);
4198 }
4199
4200 TEST_F(PictureLayerImplTest, RecycledTwinLayer) {
4201   gfx::Size tile_size(102, 102);
4202   gfx::Size layer_bounds(1000, 1000);
4203
4204   scoped_refptr<FakePicturePileImpl> pile =
4205       FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4206   SetupPendingTree(pile);
4207   EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4208
4209   ActivateTree();
4210   EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4211   EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4212
4213   SetupPendingTree(pile);
4214   EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4215   EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4216
4217   ActivateTree();
4218   EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4219   EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4220
4221   host_impl_.ResetRecycleTreeForTesting();
4222   EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4223 }
4224
4225 void PictureLayerImplTest::TestQuadsForSolidColor(bool test_for_solid) {
4226   base::TimeTicks time_ticks;
4227   time_ticks += base::TimeDelta::FromMilliseconds(1);
4228   host_impl_.SetCurrentBeginFrameArgs(
4229       CreateBeginFrameArgsForTesting(time_ticks));
4230
4231   gfx::Size tile_size(100, 100);
4232   gfx::Size layer_bounds(200, 200);
4233   gfx::Rect layer_rect(layer_bounds);
4234
4235   FakeContentLayerClient client;
4236   scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4237   FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4238   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4239   host->SetRootLayer(layer);
4240   PicturePile* pile = layer->GetPicturePileForTesting();
4241
4242   host_impl_.SetViewportSize(layer_bounds);
4243
4244   int frame_number = 0;
4245   FakeRenderingStatsInstrumentation stats_instrumentation;
4246
4247   client.set_fill_with_nonsolid_color(!test_for_solid);
4248
4249   Region invalidation(layer_rect);
4250   pile->UpdateAndExpandInvalidation(&client,
4251                                     &invalidation,
4252                                     SK_ColorWHITE,
4253                                     false,
4254                                     false,
4255                                     layer_bounds,
4256                                     layer_rect,
4257                                     frame_number++,
4258                                     Picture::RECORD_NORMALLY,
4259                                     &stats_instrumentation);
4260
4261   scoped_refptr<PicturePileImpl> pending_pile =
4262       PicturePileImpl::CreateFromOther(pile);
4263
4264   SetupPendingTree(pending_pile);
4265   ActivateTree();
4266
4267   if (test_for_solid) {
4268     EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4269   } else {
4270     ASSERT_TRUE(active_layer_->tilings());
4271     active_layer_->set_fixed_tile_size(tile_size);
4272     host_impl_.active_tree()->UpdateDrawProperties();
4273     ASSERT_GT(active_layer_->tilings()->num_tilings(), 0u);
4274     std::vector<Tile*> tiles =
4275         active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
4276     EXPECT_FALSE(tiles.empty());
4277     host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4278   }
4279
4280   MockOcclusionTracker<LayerImpl> occlusion_tracker;
4281   scoped_ptr<RenderPass> render_pass = RenderPass::Create();
4282   AppendQuadsData data;
4283   active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
4284   active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
4285   active_layer_->DidDraw(NULL);
4286
4287   DrawQuad::Material expected = test_for_solid
4288                                     ? DrawQuad::Material::SOLID_COLOR
4289                                     : DrawQuad::Material::TILED_CONTENT;
4290   EXPECT_EQ(expected, render_pass->quad_list.front()->material);
4291 }
4292
4293 TEST_F(PictureLayerImplTest, DrawSolidQuads) {
4294   TestQuadsForSolidColor(true);
4295 }
4296
4297 TEST_F(PictureLayerImplTest, DrawNonSolidQuads) {
4298   TestQuadsForSolidColor(false);
4299 }
4300
4301 TEST_F(PictureLayerImplTest, NonSolidToSolidNoTilings) {
4302   base::TimeTicks time_ticks;
4303   time_ticks += base::TimeDelta::FromMilliseconds(1);
4304   host_impl_.SetCurrentBeginFrameArgs(
4305       CreateBeginFrameArgsForTesting(time_ticks));
4306
4307   gfx::Size tile_size(100, 100);
4308   gfx::Size layer_bounds(200, 200);
4309   gfx::Rect layer_rect(layer_bounds);
4310
4311   FakeContentLayerClient client;
4312   scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4313   FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4314   scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4315   host->SetRootLayer(layer);
4316   PicturePile* pile = layer->GetPicturePileForTesting();
4317
4318   host_impl_.SetViewportSize(layer_bounds);
4319
4320   int frame_number = 0;
4321   FakeRenderingStatsInstrumentation stats_instrumentation;
4322
4323   client.set_fill_with_nonsolid_color(true);
4324
4325   Region invalidation1(layer_rect);
4326   pile->UpdateAndExpandInvalidation(&client,
4327                                     &invalidation1,
4328                                     SK_ColorWHITE,
4329                                     false,
4330                                     false,
4331                                     layer_bounds,
4332                                     layer_rect,
4333                                     frame_number++,
4334                                     Picture::RECORD_NORMALLY,
4335                                     &stats_instrumentation);
4336
4337   scoped_refptr<PicturePileImpl> pending_pile1 =
4338       PicturePileImpl::CreateFromOther(pile);
4339
4340   SetupPendingTree(pending_pile1);
4341   ActivateTree();
4342   host_impl_.active_tree()->UpdateDrawProperties();
4343
4344   // We've started with a solid layer that contains some tilings.
4345   ASSERT_TRUE(active_layer_->tilings());
4346   EXPECT_NE(0u, active_layer_->tilings()->num_tilings());
4347
4348   client.set_fill_with_nonsolid_color(false);
4349
4350   Region invalidation2(layer_rect);
4351   pile->UpdateAndExpandInvalidation(&client,
4352                                     &invalidation2,
4353                                     SK_ColorWHITE,
4354                                     false,
4355                                     false,
4356                                     layer_bounds,
4357                                     layer_rect,
4358                                     frame_number++,
4359                                     Picture::RECORD_NORMALLY,
4360                                     &stats_instrumentation);
4361
4362   scoped_refptr<PicturePileImpl> pending_pile2 =
4363       PicturePileImpl::CreateFromOther(pile);
4364
4365   SetupPendingTree(pending_pile2);
4366   ActivateTree();
4367
4368   // We've switched to a solid color, so we should end up with no tilings.
4369   ASSERT_TRUE(active_layer_->tilings());
4370   EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4371 }
4372
4373 }  // namespace
4374 }  // namespace cc