Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / cc / resources / picture_layer_tiling_unittest.cc
1 // Copyright 2012 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/resources/picture_layer_tiling.h"
6
7 #include <limits>
8 #include <set>
9
10 #include "cc/base/math_util.h"
11 #include "cc/resources/picture_layer_tiling_set.h"
12 #include "cc/test/fake_output_surface.h"
13 #include "cc/test/fake_output_surface_client.h"
14 #include "cc/test/fake_picture_layer_tiling_client.h"
15 #include "cc/test/test_context_provider.h"
16 #include "cc/test/test_shared_bitmap_manager.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/gfx/geometry/quad_f.h"
19 #include "ui/gfx/geometry/rect_conversions.h"
20 #include "ui/gfx/geometry/size_conversions.h"
21
22 namespace cc {
23 namespace {
24
25 static gfx::Rect ViewportInLayerSpace(
26     const gfx::Transform& transform,
27     const gfx::Size& device_viewport) {
28
29   gfx::Transform inverse;
30   if (!transform.GetInverse(&inverse))
31     return gfx::Rect();
32
33   gfx::RectF viewport_in_layer_space = MathUtil::ProjectClippedRect(
34       inverse, gfx::RectF(gfx::Point(0, 0), device_viewport));
35   return ToEnclosingRect(viewport_in_layer_space);
36 }
37
38 static void UpdateAllTilePriorities(PictureLayerTilingSet* set,
39                                     WhichTree tree,
40                                     const gfx::Rect& visible_layer_rect,
41                                     float layer_contents_scale,
42                                     double current_frame_time_in_seconds) {
43   for (size_t i = 0; i < set->num_tilings(); ++i) {
44     set->tiling_at(i)->ComputeTilePriorityRects(tree,
45                                                 visible_layer_rect,
46                                                 layer_contents_scale,
47                                                 current_frame_time_in_seconds,
48                                                 Occlusion());
49   }
50 }
51
52 class TestablePictureLayerTiling : public PictureLayerTiling {
53  public:
54   using PictureLayerTiling::SetLiveTilesRect;
55   using PictureLayerTiling::TileAt;
56
57   static scoped_ptr<TestablePictureLayerTiling> Create(
58       float contents_scale,
59       const gfx::Size& layer_bounds,
60       PictureLayerTilingClient* client) {
61     return make_scoped_ptr(new TestablePictureLayerTiling(
62         contents_scale,
63         layer_bounds,
64         client));
65   }
66
67   gfx::Rect live_tiles_rect() const { return live_tiles_rect_; }
68   bool eviction_tiles_cache_valid() const {
69     return eviction_tiles_cache_valid_;
70   }
71
72   using PictureLayerTiling::ComputeSkewport;
73   using PictureLayerTiling::RemoveTileAt;
74
75  protected:
76   TestablePictureLayerTiling(float contents_scale,
77                              const gfx::Size& layer_bounds,
78                              PictureLayerTilingClient* client)
79       : PictureLayerTiling(contents_scale, layer_bounds, client) { }
80 };
81
82 class PictureLayerTilingIteratorTest : public testing::Test {
83  public:
84   PictureLayerTilingIteratorTest() {}
85   virtual ~PictureLayerTilingIteratorTest() {}
86
87   void Initialize(const gfx::Size& tile_size,
88                   float contents_scale,
89                   const gfx::Size& layer_bounds) {
90     client_.SetTileSize(tile_size);
91     client_.set_tree(PENDING_TREE);
92     tiling_ = TestablePictureLayerTiling::Create(contents_scale,
93                                                  layer_bounds,
94                                                  &client_);
95   }
96
97   void SetLiveRectAndVerifyTiles(const gfx::Rect& live_tiles_rect) {
98     tiling_->SetLiveTilesRect(live_tiles_rect);
99
100     std::vector<Tile*> tiles = tiling_->AllTilesForTesting();
101     for (std::vector<Tile*>::iterator iter = tiles.begin();
102          iter != tiles.end();
103          ++iter) {
104       EXPECT_TRUE(live_tiles_rect.Intersects((*iter)->content_rect()));
105     }
106   }
107
108   void VerifyTilesExactlyCoverRect(
109       float rect_scale,
110       const gfx::Rect& request_rect,
111       const gfx::Rect& expect_rect) {
112     EXPECT_TRUE(request_rect.Contains(expect_rect));
113
114     // Iterators are not valid if this ratio is too large (i.e. the
115     // tiling is too high-res for a low-res destination rect.)  This is an
116     // artifact of snapping geometry to integer coordinates and then mapping
117     // back to floating point texture coordinates.
118     float dest_to_contents_scale = tiling_->contents_scale() / rect_scale;
119     ASSERT_LE(dest_to_contents_scale, 2.0);
120
121     Region remaining = expect_rect;
122     for (PictureLayerTiling::CoverageIterator
123              iter(tiling_.get(), rect_scale, request_rect);
124          iter;
125          ++iter) {
126       // Geometry cannot overlap previous geometry at all
127       gfx::Rect geometry = iter.geometry_rect();
128       EXPECT_TRUE(expect_rect.Contains(geometry));
129       EXPECT_TRUE(remaining.Contains(geometry));
130       remaining.Subtract(geometry);
131
132       // Sanity check that texture coords are within the texture rect.
133       gfx::RectF texture_rect = iter.texture_rect();
134       EXPECT_GE(texture_rect.x(), 0);
135       EXPECT_GE(texture_rect.y(), 0);
136       EXPECT_LE(texture_rect.right(), client_.TileSize().width());
137       EXPECT_LE(texture_rect.bottom(), client_.TileSize().height());
138
139       EXPECT_EQ(iter.texture_size(), client_.TileSize());
140     }
141
142     // The entire rect must be filled by geometry from the tiling.
143     EXPECT_TRUE(remaining.IsEmpty());
144   }
145
146   void VerifyTilesExactlyCoverRect(float rect_scale, const gfx::Rect& rect) {
147     VerifyTilesExactlyCoverRect(rect_scale, rect, rect);
148   }
149
150   void VerifyTiles(
151       float rect_scale,
152       const gfx::Rect& rect,
153       base::Callback<void(Tile* tile,
154                           const gfx::Rect& geometry_rect)> callback) {
155     VerifyTiles(tiling_.get(),
156                 rect_scale,
157                 rect,
158                 callback);
159   }
160
161   void VerifyTiles(
162       PictureLayerTiling* tiling,
163       float rect_scale,
164       const gfx::Rect& rect,
165       base::Callback<void(Tile* tile,
166                           const gfx::Rect& geometry_rect)> callback) {
167     Region remaining = rect;
168     for (PictureLayerTiling::CoverageIterator iter(tiling, rect_scale, rect);
169          iter;
170          ++iter) {
171       remaining.Subtract(iter.geometry_rect());
172       callback.Run(*iter, iter.geometry_rect());
173     }
174     EXPECT_TRUE(remaining.IsEmpty());
175   }
176
177   void VerifyTilesCoverNonContainedRect(float rect_scale,
178                                         const gfx::Rect& dest_rect) {
179     float dest_to_contents_scale = tiling_->contents_scale() / rect_scale;
180     gfx::Rect clamped_rect = gfx::ScaleToEnclosingRect(
181         gfx::Rect(tiling_->tiling_size()), 1.f / dest_to_contents_scale);
182     clamped_rect.Intersect(dest_rect);
183     VerifyTilesExactlyCoverRect(rect_scale, dest_rect, clamped_rect);
184   }
185
186   void set_max_tiles_for_interest_area(size_t area) {
187     client_.set_max_tiles_for_interest_area(area);
188   }
189
190  protected:
191   FakePictureLayerTilingClient client_;
192   scoped_ptr<TestablePictureLayerTiling> tiling_;
193
194  private:
195   DISALLOW_COPY_AND_ASSIGN(PictureLayerTilingIteratorTest);
196 };
197
198 TEST_F(PictureLayerTilingIteratorTest, ResizeDeletesTiles) {
199   // Verifies that a resize with invalidation for newly exposed pixels will
200   // deletes tiles that intersect that invalidation.
201   gfx::Size tile_size(100, 100);
202   gfx::Size original_layer_size(10, 10);
203   Initialize(tile_size, 1.f, original_layer_size);
204   SetLiveRectAndVerifyTiles(gfx::Rect(original_layer_size));
205
206   // Tiling only has one tile, since its total size is less than one.
207   EXPECT_TRUE(tiling_->TileAt(0, 0));
208
209   // Stop creating tiles so that any invalidations are left as holes.
210   client_.set_allow_create_tile(false);
211
212   Region invalidation =
213       SubtractRegions(gfx::Rect(tile_size), gfx::Rect(original_layer_size));
214   tiling_->UpdateTilesToCurrentPile(invalidation, gfx::Size(200, 200));
215   EXPECT_FALSE(tiling_->TileAt(0, 0));
216 }
217
218 TEST_F(PictureLayerTilingIteratorTest, CreateMissingTilesStaysInsideLiveRect) {
219   // The tiling has three rows and columns.
220   Initialize(gfx::Size(100, 100), 1, gfx::Size(250, 250));
221   EXPECT_EQ(3, tiling_->TilingDataForTesting().num_tiles_x());
222   EXPECT_EQ(3, tiling_->TilingDataForTesting().num_tiles_y());
223
224   // The live tiles rect is at the very edge of the right-most and
225   // bottom-most tiles. Their border pixels would still be inside the live
226   // tiles rect, but the tiles should not exist just for that.
227   int right = tiling_->TilingDataForTesting().TileBounds(2, 2).x();
228   int bottom = tiling_->TilingDataForTesting().TileBounds(2, 2).y();
229
230   SetLiveRectAndVerifyTiles(gfx::Rect(right, bottom));
231   EXPECT_FALSE(tiling_->TileAt(2, 0));
232   EXPECT_FALSE(tiling_->TileAt(2, 1));
233   EXPECT_FALSE(tiling_->TileAt(2, 2));
234   EXPECT_FALSE(tiling_->TileAt(1, 2));
235   EXPECT_FALSE(tiling_->TileAt(0, 2));
236
237   // Verify CreateMissingTilesInLiveTilesRect respects this.
238   tiling_->CreateMissingTilesInLiveTilesRect();
239   EXPECT_FALSE(tiling_->TileAt(2, 0));
240   EXPECT_FALSE(tiling_->TileAt(2, 1));
241   EXPECT_FALSE(tiling_->TileAt(2, 2));
242   EXPECT_FALSE(tiling_->TileAt(1, 2));
243   EXPECT_FALSE(tiling_->TileAt(0, 2));
244 }
245
246 TEST_F(PictureLayerTilingIteratorTest, ResizeTilingOverTileBorders) {
247   // The tiling has four rows and three columns.
248   Initialize(gfx::Size(100, 100), 1, gfx::Size(250, 350));
249   EXPECT_EQ(3, tiling_->TilingDataForTesting().num_tiles_x());
250   EXPECT_EQ(4, tiling_->TilingDataForTesting().num_tiles_y());
251
252   // The live tiles rect covers the whole tiling.
253   SetLiveRectAndVerifyTiles(gfx::Rect(250, 350));
254
255   // Tiles in the bottom row and right column exist.
256   EXPECT_TRUE(tiling_->TileAt(2, 0));
257   EXPECT_TRUE(tiling_->TileAt(2, 1));
258   EXPECT_TRUE(tiling_->TileAt(2, 2));
259   EXPECT_TRUE(tiling_->TileAt(2, 3));
260   EXPECT_TRUE(tiling_->TileAt(1, 3));
261   EXPECT_TRUE(tiling_->TileAt(0, 3));
262
263   int right = tiling_->TilingDataForTesting().TileBounds(2, 2).x();
264   int bottom = tiling_->TilingDataForTesting().TileBounds(2, 3).y();
265
266   // Shrink the tiling so that the last tile row/column is entirely in the
267   // border pixels of the interior tiles. That row/column is removed.
268   Region invalidation;
269   tiling_->UpdateTilesToCurrentPile(invalidation,
270                                     gfx::Size(right + 1, bottom + 1));
271   EXPECT_EQ(2, tiling_->TilingDataForTesting().num_tiles_x());
272   EXPECT_EQ(3, tiling_->TilingDataForTesting().num_tiles_y());
273
274   // The live tiles rect was clamped to the pile size.
275   EXPECT_EQ(gfx::Rect(right + 1, bottom + 1), tiling_->live_tiles_rect());
276
277   // Since the row/column is gone, the tiles should be gone too.
278   EXPECT_FALSE(tiling_->TileAt(2, 0));
279   EXPECT_FALSE(tiling_->TileAt(2, 1));
280   EXPECT_FALSE(tiling_->TileAt(2, 2));
281   EXPECT_FALSE(tiling_->TileAt(2, 3));
282   EXPECT_FALSE(tiling_->TileAt(1, 3));
283   EXPECT_FALSE(tiling_->TileAt(0, 3));
284
285   // Growing outside the current right/bottom tiles border pixels should create
286   // the tiles again, even though the live rect has not changed size.
287   tiling_->UpdateTilesToCurrentPile(invalidation,
288                                     gfx::Size(right + 2, bottom + 2));
289   EXPECT_EQ(3, tiling_->TilingDataForTesting().num_tiles_x());
290   EXPECT_EQ(4, tiling_->TilingDataForTesting().num_tiles_y());
291
292   // Not changed.
293   EXPECT_EQ(gfx::Rect(right + 1, bottom + 1), tiling_->live_tiles_rect());
294
295   // The last row/column tiles are inside the live tiles rect.
296   EXPECT_TRUE(gfx::Rect(right + 1, bottom + 1).Intersects(
297       tiling_->TilingDataForTesting().TileBounds(2, 0)));
298   EXPECT_TRUE(gfx::Rect(right + 1, bottom + 1).Intersects(
299       tiling_->TilingDataForTesting().TileBounds(0, 3)));
300
301   EXPECT_TRUE(tiling_->TileAt(2, 0));
302   EXPECT_TRUE(tiling_->TileAt(2, 1));
303   EXPECT_TRUE(tiling_->TileAt(2, 2));
304   EXPECT_TRUE(tiling_->TileAt(2, 3));
305   EXPECT_TRUE(tiling_->TileAt(1, 3));
306   EXPECT_TRUE(tiling_->TileAt(0, 3));
307 }
308
309 TEST_F(PictureLayerTilingIteratorTest, ResizeLiveTileRectOverTileBorders) {
310   // The tiling has three rows and columns.
311   Initialize(gfx::Size(100, 100), 1, gfx::Size(250, 350));
312   EXPECT_EQ(3, tiling_->TilingDataForTesting().num_tiles_x());
313   EXPECT_EQ(4, tiling_->TilingDataForTesting().num_tiles_y());
314
315   // The live tiles rect covers the whole tiling.
316   SetLiveRectAndVerifyTiles(gfx::Rect(250, 350));
317
318   // Tiles in the bottom row and right column exist.
319   EXPECT_TRUE(tiling_->TileAt(2, 0));
320   EXPECT_TRUE(tiling_->TileAt(2, 1));
321   EXPECT_TRUE(tiling_->TileAt(2, 2));
322   EXPECT_TRUE(tiling_->TileAt(2, 3));
323   EXPECT_TRUE(tiling_->TileAt(1, 3));
324   EXPECT_TRUE(tiling_->TileAt(0, 3));
325
326   // Shrink the live tiles rect to the very edge of the right-most and
327   // bottom-most tiles. Their border pixels would still be inside the live
328   // tiles rect, but the tiles should not exist just for that.
329   int right = tiling_->TilingDataForTesting().TileBounds(2, 3).x();
330   int bottom = tiling_->TilingDataForTesting().TileBounds(2, 3).y();
331
332   SetLiveRectAndVerifyTiles(gfx::Rect(right, bottom));
333   EXPECT_FALSE(tiling_->TileAt(2, 0));
334   EXPECT_FALSE(tiling_->TileAt(2, 1));
335   EXPECT_FALSE(tiling_->TileAt(2, 2));
336   EXPECT_FALSE(tiling_->TileAt(2, 3));
337   EXPECT_FALSE(tiling_->TileAt(1, 3));
338   EXPECT_FALSE(tiling_->TileAt(0, 3));
339
340   // Including the bottom row and right column again, should create the tiles.
341   SetLiveRectAndVerifyTiles(gfx::Rect(right + 1, bottom + 1));
342   EXPECT_TRUE(tiling_->TileAt(2, 0));
343   EXPECT_TRUE(tiling_->TileAt(2, 1));
344   EXPECT_TRUE(tiling_->TileAt(2, 2));
345   EXPECT_TRUE(tiling_->TileAt(2, 3));
346   EXPECT_TRUE(tiling_->TileAt(1, 2));
347   EXPECT_TRUE(tiling_->TileAt(0, 2));
348
349   // Shrink the live tiles rect to the very edge of the left-most and
350   // top-most tiles. Their border pixels would still be inside the live
351   // tiles rect, but the tiles should not exist just for that.
352   int left = tiling_->TilingDataForTesting().TileBounds(0, 0).right();
353   int top = tiling_->TilingDataForTesting().TileBounds(0, 0).bottom();
354
355   SetLiveRectAndVerifyTiles(gfx::Rect(left, top, 250 - left, 350 - top));
356   EXPECT_FALSE(tiling_->TileAt(0, 3));
357   EXPECT_FALSE(tiling_->TileAt(0, 2));
358   EXPECT_FALSE(tiling_->TileAt(0, 1));
359   EXPECT_FALSE(tiling_->TileAt(0, 0));
360   EXPECT_FALSE(tiling_->TileAt(1, 0));
361   EXPECT_FALSE(tiling_->TileAt(2, 0));
362
363   // Including the top row and left column again, should create the tiles.
364   SetLiveRectAndVerifyTiles(
365       gfx::Rect(left - 1, top - 1, 250 - left, 350 - top));
366   EXPECT_TRUE(tiling_->TileAt(0, 3));
367   EXPECT_TRUE(tiling_->TileAt(0, 2));
368   EXPECT_TRUE(tiling_->TileAt(0, 1));
369   EXPECT_TRUE(tiling_->TileAt(0, 0));
370   EXPECT_TRUE(tiling_->TileAt(1, 0));
371   EXPECT_TRUE(tiling_->TileAt(2, 0));
372 }
373
374 TEST_F(PictureLayerTilingIteratorTest, ResizeLiveTileRectOverSameTiles) {
375   // The tiling has four rows and three columns.
376   Initialize(gfx::Size(100, 100), 1, gfx::Size(250, 350));
377   EXPECT_EQ(3, tiling_->TilingDataForTesting().num_tiles_x());
378   EXPECT_EQ(4, tiling_->TilingDataForTesting().num_tiles_y());
379
380   // The live tiles rect covers the whole tiling.
381   SetLiveRectAndVerifyTiles(gfx::Rect(250, 350));
382
383   // All tiles exist.
384   for (int i = 0; i < 3; ++i) {
385     for (int j = 0; j < 4; ++j)
386       EXPECT_TRUE(tiling_->TileAt(i, j)) << i << "," << j;
387   }
388
389   // Shrink the live tiles rect, but still cover all the tiles.
390   SetLiveRectAndVerifyTiles(gfx::Rect(1, 1, 249, 349));
391
392   // All tiles still exist.
393   for (int i = 0; i < 3; ++i) {
394     for (int j = 0; j < 4; ++j)
395       EXPECT_TRUE(tiling_->TileAt(i, j)) << i << "," << j;
396   }
397
398   // Grow the live tiles rect, but still cover all the same tiles.
399   SetLiveRectAndVerifyTiles(gfx::Rect(0, 0, 250, 350));
400
401   // All tiles still exist.
402   for (int i = 0; i < 3; ++i) {
403     for (int j = 0; j < 4; ++j)
404       EXPECT_TRUE(tiling_->TileAt(i, j)) << i << "," << j;
405   }
406 }
407
408 TEST_F(PictureLayerTilingIteratorTest, ResizeOverBorderPixelsDeletesTiles) {
409   // Verifies that a resize with invalidation for newly exposed pixels will
410   // deletes tiles that intersect that invalidation.
411   gfx::Size tile_size(100, 100);
412   gfx::Size original_layer_size(99, 99);
413   Initialize(tile_size, 1.f, original_layer_size);
414   SetLiveRectAndVerifyTiles(gfx::Rect(original_layer_size));
415
416   // Tiling only has one tile, since its total size is less than one.
417   EXPECT_TRUE(tiling_->TileAt(0, 0));
418
419   // Stop creating tiles so that any invalidations are left as holes.
420   client_.set_allow_create_tile(false);
421
422   Region invalidation =
423       SubtractRegions(gfx::Rect(tile_size), gfx::Rect(original_layer_size));
424   tiling_->UpdateTilesToCurrentPile(invalidation, gfx::Size(200, 200));
425   EXPECT_FALSE(tiling_->TileAt(0, 0));
426
427   // The original tile was the same size after resize, but it would include new
428   // border pixels.
429   EXPECT_EQ(gfx::Rect(original_layer_size),
430             tiling_->TilingDataForTesting().TileBounds(0, 0));
431 }
432
433 TEST_F(PictureLayerTilingIteratorTest, LiveTilesExactlyCoverLiveTileRect) {
434   Initialize(gfx::Size(100, 100), 1, gfx::Size(1099, 801));
435   SetLiveRectAndVerifyTiles(gfx::Rect(100, 100));
436   SetLiveRectAndVerifyTiles(gfx::Rect(101, 99));
437   SetLiveRectAndVerifyTiles(gfx::Rect(1099, 1));
438   SetLiveRectAndVerifyTiles(gfx::Rect(1, 801));
439   SetLiveRectAndVerifyTiles(gfx::Rect(1099, 1));
440   SetLiveRectAndVerifyTiles(gfx::Rect(201, 800));
441 }
442
443 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsNoScale) {
444   Initialize(gfx::Size(100, 100), 1, gfx::Size(1099, 801));
445   VerifyTilesExactlyCoverRect(1, gfx::Rect());
446   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1099, 801));
447   VerifyTilesExactlyCoverRect(1, gfx::Rect(52, 83, 789, 412));
448
449   // With borders, a size of 3x3 = 1 pixel of content.
450   Initialize(gfx::Size(3, 3), 1, gfx::Size(10, 10));
451   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
452   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
453   VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
454   VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
455 }
456
457 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsTilingScale) {
458   Initialize(gfx::Size(200, 100), 2.0f, gfx::Size(1005, 2010));
459   VerifyTilesExactlyCoverRect(1, gfx::Rect());
460   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
461   VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
462
463   Initialize(gfx::Size(3, 3), 2.0f, gfx::Size(10, 10));
464   VerifyTilesExactlyCoverRect(1, gfx::Rect());
465   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
466   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
467   VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
468   VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
469
470   Initialize(gfx::Size(100, 200), 0.5f, gfx::Size(1005, 2010));
471   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
472   VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
473
474   Initialize(gfx::Size(150, 250), 0.37f, gfx::Size(1005, 2010));
475   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
476   VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
477
478   Initialize(gfx::Size(312, 123), 0.01f, gfx::Size(1005, 2010));
479   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
480   VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
481 }
482
483 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsBothScale) {
484   Initialize(gfx::Size(50, 50), 4.0f, gfx::Size(800, 600));
485   VerifyTilesExactlyCoverRect(2.0f, gfx::Rect());
486   VerifyTilesExactlyCoverRect(2.0f, gfx::Rect(0, 0, 1600, 1200));
487   VerifyTilesExactlyCoverRect(2.0f, gfx::Rect(512, 365, 253, 182));
488
489   float scale = 6.7f;
490   gfx::Size bounds(800, 600);
491   gfx::Rect full_rect(gfx::ToCeiledSize(gfx::ScaleSize(bounds, scale)));
492   Initialize(gfx::Size(256, 512), 5.2f, bounds);
493   VerifyTilesExactlyCoverRect(scale, full_rect);
494   VerifyTilesExactlyCoverRect(scale, gfx::Rect(2014, 1579, 867, 1033));
495 }
496
497 TEST_F(PictureLayerTilingIteratorTest, IteratorEmptyRect) {
498   Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(800, 600));
499
500   gfx::Rect empty;
501   PictureLayerTiling::CoverageIterator iter(tiling_.get(), 1.0f, empty);
502   EXPECT_FALSE(iter);
503 }
504
505 TEST_F(PictureLayerTilingIteratorTest, NonIntersectingRect) {
506   Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(800, 600));
507   gfx::Rect non_intersecting(1000, 1000, 50, 50);
508   PictureLayerTiling::CoverageIterator iter(tiling_.get(), 1, non_intersecting);
509   EXPECT_FALSE(iter);
510 }
511
512 TEST_F(PictureLayerTilingIteratorTest, LayerEdgeTextureCoordinates) {
513   Initialize(gfx::Size(300, 300), 1.0f, gfx::Size(256, 256));
514   // All of these sizes are 256x256, scaled and ceiled.
515   VerifyTilesExactlyCoverRect(1.0f, gfx::Rect(0, 0, 256, 256));
516   VerifyTilesExactlyCoverRect(0.8f, gfx::Rect(0, 0, 205, 205));
517   VerifyTilesExactlyCoverRect(1.2f, gfx::Rect(0, 0, 308, 308));
518 }
519
520 TEST_F(PictureLayerTilingIteratorTest, NonContainedDestRect) {
521   Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(400, 400));
522
523   // Too large in all dimensions
524   VerifyTilesCoverNonContainedRect(1.0f, gfx::Rect(-1000, -1000, 2000, 2000));
525   VerifyTilesCoverNonContainedRect(1.5f, gfx::Rect(-1000, -1000, 2000, 2000));
526   VerifyTilesCoverNonContainedRect(0.5f, gfx::Rect(-1000, -1000, 2000, 2000));
527
528   // Partially covering content, but too large
529   VerifyTilesCoverNonContainedRect(1.0f, gfx::Rect(-1000, 100, 2000, 100));
530   VerifyTilesCoverNonContainedRect(1.5f, gfx::Rect(-1000, 100, 2000, 100));
531   VerifyTilesCoverNonContainedRect(0.5f, gfx::Rect(-1000, 100, 2000, 100));
532 }
533
534 TEST(PictureLayerTilingTest, SkewportLimits) {
535   FakePictureLayerTilingClient client;
536   client.set_skewport_extrapolation_limit_in_content_pixels(75);
537   client.set_tree(ACTIVE_TREE);
538   scoped_ptr<TestablePictureLayerTiling> tiling;
539
540   gfx::Rect viewport(0, 0, 100, 100);
541   gfx::Size layer_bounds(200, 200);
542
543   client.SetTileSize(gfx::Size(100, 100));
544   tiling = TestablePictureLayerTiling::Create(1.0f, layer_bounds, &client);
545
546   tiling->ComputeTilePriorityRects(
547       ACTIVE_TREE, viewport, 1.f, 1.0, Occlusion());
548
549   // Move viewport down 50 pixels in 0.5 seconds.
550   gfx::Rect down_skewport =
551       tiling->ComputeSkewport(1.5, gfx::Rect(0, 50, 100, 100));
552
553   EXPECT_EQ(0, down_skewport.x());
554   EXPECT_EQ(50, down_skewport.y());
555   EXPECT_EQ(100, down_skewport.width());
556   EXPECT_EQ(175, down_skewport.height());
557   EXPECT_TRUE(down_skewport.Contains(gfx::Rect(0, 50, 100, 100)));
558
559   // Move viewport down 50 and right 10 pixels.
560   gfx::Rect down_right_skewport =
561       tiling->ComputeSkewport(1.5, gfx::Rect(10, 50, 100, 100));
562
563   EXPECT_EQ(10, down_right_skewport.x());
564   EXPECT_EQ(50, down_right_skewport.y());
565   EXPECT_EQ(120, down_right_skewport.width());
566   EXPECT_EQ(175, down_right_skewport.height());
567   EXPECT_TRUE(down_right_skewport.Contains(gfx::Rect(10, 50, 100, 100)));
568
569   // Move viewport left.
570   gfx::Rect left_skewport =
571       tiling->ComputeSkewport(1.5, gfx::Rect(-50, 0, 100, 100));
572
573   EXPECT_EQ(-125, left_skewport.x());
574   EXPECT_EQ(0, left_skewport.y());
575   EXPECT_EQ(175, left_skewport.width());
576   EXPECT_EQ(100, left_skewport.height());
577   EXPECT_TRUE(left_skewport.Contains(gfx::Rect(-50, 0, 100, 100)));
578
579   // Expand viewport.
580   gfx::Rect expand_skewport =
581       tiling->ComputeSkewport(1.5, gfx::Rect(-50, -50, 200, 200));
582
583   // x and y moved by -75 (-50 - 75 = -125).
584   // right side and bottom side moved by 75 [(350 - 125) - (200 - 50) = 75].
585   EXPECT_EQ(-125, expand_skewport.x());
586   EXPECT_EQ(-125, expand_skewport.y());
587   EXPECT_EQ(350, expand_skewport.width());
588   EXPECT_EQ(350, expand_skewport.height());
589   EXPECT_TRUE(expand_skewport.Contains(gfx::Rect(-50, -50, 200, 200)));
590
591   // Expand the viewport past the limit.
592   gfx::Rect big_expand_skewport =
593       tiling->ComputeSkewport(1.5, gfx::Rect(-500, -500, 1500, 1500));
594
595   EXPECT_EQ(-575, big_expand_skewport.x());
596   EXPECT_EQ(-575, big_expand_skewport.y());
597   EXPECT_EQ(1650, big_expand_skewport.width());
598   EXPECT_EQ(1650, big_expand_skewport.height());
599   EXPECT_TRUE(big_expand_skewport.Contains(gfx::Rect(-500, -500, 1500, 1500)));
600 }
601
602 TEST(PictureLayerTilingTest, ComputeSkewport) {
603   FakePictureLayerTilingClient client;
604   scoped_ptr<TestablePictureLayerTiling> tiling;
605
606   gfx::Rect viewport(0, 0, 100, 100);
607   gfx::Size layer_bounds(200, 200);
608
609   client.SetTileSize(gfx::Size(100, 100));
610   client.set_tree(ACTIVE_TREE);
611   tiling = TestablePictureLayerTiling::Create(1.0f, layer_bounds, &client);
612
613   tiling->ComputeTilePriorityRects(
614       ACTIVE_TREE, viewport, 1.f, 1.0, Occlusion());
615
616   // Move viewport down 50 pixels in 0.5 seconds.
617   gfx::Rect down_skewport =
618       tiling->ComputeSkewport(1.5, gfx::Rect(0, 50, 100, 100));
619
620   EXPECT_EQ(0, down_skewport.x());
621   EXPECT_EQ(50, down_skewport.y());
622   EXPECT_EQ(100, down_skewport.width());
623   EXPECT_EQ(200, down_skewport.height());
624
625   // Shrink viewport.
626   gfx::Rect shrink_skewport =
627       tiling->ComputeSkewport(1.5, gfx::Rect(25, 25, 50, 50));
628
629   EXPECT_EQ(25, shrink_skewport.x());
630   EXPECT_EQ(25, shrink_skewport.y());
631   EXPECT_EQ(50, shrink_skewport.width());
632   EXPECT_EQ(50, shrink_skewport.height());
633
634   // Move viewport down 50 and right 10 pixels.
635   gfx::Rect down_right_skewport =
636       tiling->ComputeSkewport(1.5, gfx::Rect(10, 50, 100, 100));
637
638   EXPECT_EQ(10, down_right_skewport.x());
639   EXPECT_EQ(50, down_right_skewport.y());
640   EXPECT_EQ(120, down_right_skewport.width());
641   EXPECT_EQ(200, down_right_skewport.height());
642
643   // Move viewport left.
644   gfx::Rect left_skewport =
645       tiling->ComputeSkewport(1.5, gfx::Rect(-20, 0, 100, 100));
646
647   EXPECT_EQ(-60, left_skewport.x());
648   EXPECT_EQ(0, left_skewport.y());
649   EXPECT_EQ(140, left_skewport.width());
650   EXPECT_EQ(100, left_skewport.height());
651
652   // Expand viewport in 0.2 seconds.
653   gfx::Rect expanded_skewport =
654       tiling->ComputeSkewport(1.2, gfx::Rect(-5, -5, 110, 110));
655
656   EXPECT_EQ(-30, expanded_skewport.x());
657   EXPECT_EQ(-30, expanded_skewport.y());
658   EXPECT_EQ(160, expanded_skewport.width());
659   EXPECT_EQ(160, expanded_skewport.height());
660 }
661
662 TEST(PictureLayerTilingTest, ViewportDistanceWithScale) {
663   FakePictureLayerTilingClient client;
664   scoped_ptr<TestablePictureLayerTiling> tiling;
665
666   gfx::Rect viewport(0, 0, 100, 100);
667   gfx::Size layer_bounds(1500, 1500);
668
669   client.SetTileSize(gfx::Size(10, 10));
670   client.set_tree(ACTIVE_TREE);
671
672   // Tiling at 0.25 scale: this should create 47x47 tiles of size 10x10.
673   // The reason is that each tile has a one pixel border, so tile at (1, 2)
674   // for instance begins at (8, 16) pixels. So tile at (46, 46) will begin at
675   // (368, 368) and extend to the end of 1500 * 0.25 = 375 edge of the
676   // tiling.
677   tiling = TestablePictureLayerTiling::Create(0.25f, layer_bounds, &client);
678   gfx::Rect viewport_in_content_space =
679       gfx::ToEnclosedRect(gfx::ScaleRect(viewport, 0.25f));
680
681   tiling->ComputeTilePriorityRects(
682       ACTIVE_TREE, viewport, 1.f, 1.0, Occlusion());
683   tiling->UpdateAllTilePrioritiesForTesting();
684
685   gfx::Rect soon_rect = viewport;
686   soon_rect.Inset(-312.f, -312.f, -312.f, -312.f);
687   gfx::Rect soon_rect_in_content_space =
688       gfx::ToEnclosedRect(gfx::ScaleRect(soon_rect, 0.25f));
689
690   // Sanity checks.
691   for (int i = 0; i < 47; ++i) {
692     for (int j = 0; j < 47; ++j) {
693       EXPECT_TRUE(tiling->TileAt(i, j)) << "i: " << i << " j: " << j;
694     }
695   }
696   for (int i = 0; i < 47; ++i) {
697     EXPECT_FALSE(tiling->TileAt(i, 47)) << "i: " << i;
698     EXPECT_FALSE(tiling->TileAt(47, i)) << "i: " << i;
699   }
700
701   // No movement in the viewport implies that tiles will either be NOW
702   // or EVENTUALLY, with the exception of tiles that are between 0 and 312
703   // pixels away from the viewport, which will be in the SOON bin.
704   bool have_now = false;
705   bool have_eventually = false;
706   bool have_soon = false;
707   for (int i = 0; i < 47; ++i) {
708     for (int j = 0; j < 47; ++j) {
709       Tile* tile = tiling->TileAt(i, j);
710       TilePriority priority = tile->priority(ACTIVE_TREE);
711
712       gfx::Rect tile_rect = tiling->TilingDataForTesting().TileBounds(i, j);
713       if (viewport_in_content_space.Intersects(tile_rect)) {
714         EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
715         EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
716         have_now = true;
717       } else if (soon_rect_in_content_space.Intersects(tile_rect)) {
718         EXPECT_EQ(TilePriority::SOON, priority.priority_bin);
719         have_soon = true;
720       } else {
721         EXPECT_EQ(TilePriority::EVENTUALLY, priority.priority_bin);
722         EXPECT_GT(priority.distance_to_visible, 0.f);
723         have_eventually = true;
724       }
725     }
726   }
727
728   EXPECT_TRUE(have_now);
729   EXPECT_TRUE(have_soon);
730   EXPECT_TRUE(have_eventually);
731
732   // Spot check some distances.
733   // Tile at 5, 1 should begin at 41x9 in content space (without borders),
734   // so the distance to a viewport that ends at 25x25 in content space
735   // should be 17 (41 - 25 + 1). In layer space, then that should be
736   // 17 / 0.25 = 68 pixels.
737
738   // We can verify that the content rect (with borders) is one pixel off
739   // 41,9 8x8 on all sides.
740   EXPECT_EQ(tiling->TileAt(5, 1)->content_rect().ToString(), "40,8 10x10");
741
742   TilePriority priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
743   EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
744
745   priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
746   EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
747
748   priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
749   EXPECT_FLOAT_EQ(40.f, priority.distance_to_visible);
750
751   // Move the viewport down 40 pixels.
752   viewport = gfx::Rect(0, 40, 100, 100);
753   viewport_in_content_space =
754       gfx::ToEnclosedRect(gfx::ScaleRect(viewport, 0.25f));
755   gfx::Rect skewport = tiling->ComputeSkewport(2.0, viewport_in_content_space);
756
757   soon_rect = viewport;
758   soon_rect.Inset(-312.f, -312.f, -312.f, -312.f);
759   soon_rect_in_content_space =
760       gfx::ToEnclosedRect(gfx::ScaleRect(soon_rect, 0.25f));
761
762   EXPECT_EQ(0, skewport.x());
763   EXPECT_EQ(10, skewport.y());
764   EXPECT_EQ(25, skewport.width());
765   EXPECT_EQ(35, skewport.height());
766
767   tiling->ComputeTilePriorityRects(
768       ACTIVE_TREE, viewport, 1.f, 2.0, Occlusion());
769   tiling->UpdateAllTilePrioritiesForTesting();
770
771   have_now = false;
772   have_eventually = false;
773   have_soon = false;
774
775   // Viewport moved, so we expect to find some NOW tiles, some SOON tiles and
776   // some EVENTUALLY tiles.
777   for (int i = 0; i < 47; ++i) {
778     for (int j = 0; j < 47; ++j) {
779       Tile* tile = tiling->TileAt(i, j);
780       TilePriority priority = tile->priority(ACTIVE_TREE);
781
782       gfx::Rect tile_rect = tiling->TilingDataForTesting().TileBounds(i, j);
783       if (viewport_in_content_space.Intersects(tile_rect)) {
784         EXPECT_EQ(TilePriority::NOW, priority.priority_bin) << "i: " << i
785                                                             << " j: " << j;
786         EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible) << "i: " << i
787                                                            << " j: " << j;
788         have_now = true;
789       } else if (skewport.Intersects(tile_rect) ||
790                  soon_rect_in_content_space.Intersects(tile_rect)) {
791         EXPECT_EQ(TilePriority::SOON, priority.priority_bin) << "i: " << i
792                                                              << " j: " << j;
793         EXPECT_GT(priority.distance_to_visible, 0.f) << "i: " << i
794                                                      << " j: " << j;
795         have_soon = true;
796       } else {
797         EXPECT_EQ(TilePriority::EVENTUALLY, priority.priority_bin)
798             << "i: " << i << " j: " << j;
799         EXPECT_GT(priority.distance_to_visible, 0.f) << "i: " << i
800                                                      << " j: " << j;
801         have_eventually = true;
802       }
803     }
804   }
805
806   EXPECT_TRUE(have_now);
807   EXPECT_TRUE(have_soon);
808   EXPECT_TRUE(have_eventually);
809
810   priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
811   EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
812
813   priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
814   EXPECT_FLOAT_EQ(28.f, priority.distance_to_visible);
815
816   priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
817   EXPECT_FLOAT_EQ(4.f, priority.distance_to_visible);
818
819   // Change the underlying layer scale.
820   tiling->ComputeTilePriorityRects(
821       ACTIVE_TREE, viewport, 2.0f, 3.0, Occlusion());
822   tiling->UpdateAllTilePrioritiesForTesting();
823
824   priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
825   EXPECT_FLOAT_EQ(136.f, priority.distance_to_visible);
826
827   priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
828   EXPECT_FLOAT_EQ(56.f, priority.distance_to_visible);
829
830   priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
831   EXPECT_FLOAT_EQ(8.f, priority.distance_to_visible);
832
833   // Test additional scales.
834   tiling = TestablePictureLayerTiling::Create(0.2f, layer_bounds, &client);
835   tiling->ComputeTilePriorityRects(
836       ACTIVE_TREE, viewport, 1.0f, 4.0, Occlusion());
837   tiling->UpdateAllTilePrioritiesForTesting();
838
839   priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
840   EXPECT_FLOAT_EQ(110.f, priority.distance_to_visible);
841
842   priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
843   EXPECT_FLOAT_EQ(70.f, priority.distance_to_visible);
844
845   priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
846   EXPECT_FLOAT_EQ(60.f, priority.distance_to_visible);
847
848   tiling->ComputeTilePriorityRects(
849       ACTIVE_TREE, viewport, 0.5f, 5.0, Occlusion());
850   tiling->UpdateAllTilePrioritiesForTesting();
851
852   priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
853   EXPECT_FLOAT_EQ(55.f, priority.distance_to_visible);
854
855   priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
856   EXPECT_FLOAT_EQ(35.f, priority.distance_to_visible);
857
858   priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
859   EXPECT_FLOAT_EQ(30.f, priority.distance_to_visible);
860 }
861
862 TEST(PictureLayerTilingTest, ExpandRectEqual) {
863   gfx::Rect in(40, 50, 100, 200);
864   gfx::Rect bounds(-1000, -1000, 10000, 10000);
865   int64 target_area = 100 * 200;
866   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
867       in, target_area, bounds, NULL);
868   EXPECT_EQ(in.ToString(), out.ToString());
869 }
870
871 TEST(PictureLayerTilingTest, ExpandRectSmaller) {
872   gfx::Rect in(40, 50, 100, 200);
873   gfx::Rect bounds(-1000, -1000, 10000, 10000);
874   int64 target_area = 100 * 100;
875   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
876       in, target_area, bounds, NULL);
877   EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
878   EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
879   EXPECT_EQ(out.width() - in.width(), out.height() - in.height());
880
881   // |in| represents the visible rect, and |out| represents the eventually rect.
882   // If the eventually rect doesn't contain the visible rect, we will start
883   // losing tiles.
884   EXPECT_TRUE(out.Contains(in));
885   EXPECT_TRUE(bounds.Contains(out));
886 }
887
888 TEST(PictureLayerTilingTest, ExpandRectUnbounded) {
889   gfx::Rect in(40, 50, 100, 200);
890   gfx::Rect bounds(-1000, -1000, 10000, 10000);
891   int64 target_area = 200 * 200;
892   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
893       in, target_area, bounds, NULL);
894   EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
895   EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
896   EXPECT_EQ(out.width() - in.width(), out.height() - in.height());
897   EXPECT_NEAR(200 * 200, out.width() * out.height(), 100);
898   EXPECT_TRUE(bounds.Contains(out));
899 }
900
901 TEST(PictureLayerTilingTest, ExpandRectBoundedSmaller) {
902   gfx::Rect in(40, 50, 100, 200);
903   gfx::Rect bounds(50, 60, 40, 30);
904   int64 target_area = 200 * 200;
905   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
906       in, target_area, bounds, NULL);
907   EXPECT_EQ(bounds.ToString(), out.ToString());
908 }
909
910 TEST(PictureLayerTilingTest, ExpandRectBoundedEqual) {
911   gfx::Rect in(40, 50, 100, 200);
912   gfx::Rect bounds = in;
913   int64 target_area = 200 * 200;
914   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
915       in, target_area, bounds, NULL);
916   EXPECT_EQ(bounds.ToString(), out.ToString());
917 }
918
919 TEST(PictureLayerTilingTest, ExpandRectBoundedSmallerStretchVertical) {
920   gfx::Rect in(40, 50, 100, 200);
921   gfx::Rect bounds(45, 0, 90, 300);
922   int64 target_area = 200 * 200;
923   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
924       in, target_area, bounds, NULL);
925   EXPECT_EQ(bounds.ToString(), out.ToString());
926 }
927
928 TEST(PictureLayerTilingTest, ExpandRectBoundedEqualStretchVertical) {
929   gfx::Rect in(40, 50, 100, 200);
930   gfx::Rect bounds(40, 0, 100, 300);
931   int64 target_area = 200 * 200;
932   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
933       in, target_area, bounds, NULL);
934   EXPECT_EQ(bounds.ToString(), out.ToString());
935 }
936
937 TEST(PictureLayerTilingTest, ExpandRectBoundedSmallerStretchHorizontal) {
938   gfx::Rect in(40, 50, 100, 200);
939   gfx::Rect bounds(0, 55, 180, 190);
940   int64 target_area = 200 * 200;
941   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
942       in, target_area, bounds, NULL);
943   EXPECT_EQ(bounds.ToString(), out.ToString());
944 }
945
946 TEST(PictureLayerTilingTest, ExpandRectBoundedEqualStretchHorizontal) {
947   gfx::Rect in(40, 50, 100, 200);
948   gfx::Rect bounds(0, 50, 180, 200);
949   int64 target_area = 200 * 200;
950   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
951       in, target_area, bounds, NULL);
952   EXPECT_EQ(bounds.ToString(), out.ToString());
953 }
954
955 TEST(PictureLayerTilingTest, ExpandRectBoundedLeft) {
956   gfx::Rect in(40, 50, 100, 200);
957   gfx::Rect bounds(20, -1000, 10000, 10000);
958   int64 target_area = 200 * 200;
959   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
960       in, target_area, bounds, NULL);
961   EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
962   EXPECT_EQ(out.bottom() - in.bottom(), out.right() - in.right());
963   EXPECT_LE(out.width() * out.height(), target_area);
964   EXPECT_GT(out.width() * out.height(),
965             target_area - out.width() - out.height() * 2);
966   EXPECT_TRUE(bounds.Contains(out));
967 }
968
969 TEST(PictureLayerTilingTest, ExpandRectBoundedRight) {
970   gfx::Rect in(40, 50, 100, 200);
971   gfx::Rect bounds(-1000, -1000, 1000+120, 10000);
972   int64 target_area = 200 * 200;
973   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
974       in, target_area, bounds, NULL);
975   EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
976   EXPECT_EQ(out.bottom() - in.bottom(), in.x() - out.x());
977   EXPECT_LE(out.width() * out.height(), target_area);
978   EXPECT_GT(out.width() * out.height(),
979             target_area - out.width() - out.height() * 2);
980   EXPECT_TRUE(bounds.Contains(out));
981 }
982
983 TEST(PictureLayerTilingTest, ExpandRectBoundedTop) {
984   gfx::Rect in(40, 50, 100, 200);
985   gfx::Rect bounds(-1000, 30, 10000, 10000);
986   int64 target_area = 200 * 200;
987   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
988       in, target_area, bounds, NULL);
989   EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
990   EXPECT_EQ(out.right() - in.right(), out.bottom() - in.bottom());
991   EXPECT_LE(out.width() * out.height(), target_area);
992   EXPECT_GT(out.width() * out.height(),
993             target_area - out.width() * 2 - out.height());
994   EXPECT_TRUE(bounds.Contains(out));
995 }
996
997 TEST(PictureLayerTilingTest, ExpandRectBoundedBottom) {
998   gfx::Rect in(40, 50, 100, 200);
999   gfx::Rect bounds(-1000, -1000, 10000, 1000 + 220);
1000   int64 target_area = 200 * 200;
1001   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
1002       in, target_area, bounds, NULL);
1003   EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
1004   EXPECT_EQ(out.right() - in.right(), in.y() - out.y());
1005   EXPECT_LE(out.width() * out.height(), target_area);
1006   EXPECT_GT(out.width() * out.height(),
1007             target_area - out.width() * 2 - out.height());
1008   EXPECT_TRUE(bounds.Contains(out));
1009 }
1010
1011 TEST(PictureLayerTilingTest, ExpandRectSquishedHorizontally) {
1012   gfx::Rect in(40, 50, 100, 200);
1013   gfx::Rect bounds(0, -4000, 100+40+20, 100000);
1014   int64 target_area = 400 * 400;
1015   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
1016       in, target_area, bounds, NULL);
1017   EXPECT_EQ(20, out.right() - in.right());
1018   EXPECT_EQ(40, in.x() - out.x());
1019   EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
1020   EXPECT_LE(out.width() * out.height(), target_area);
1021   EXPECT_GT(out.width() * out.height(),
1022             target_area - out.width() * 2);
1023   EXPECT_TRUE(bounds.Contains(out));
1024 }
1025
1026 TEST(PictureLayerTilingTest, ExpandRectSquishedVertically) {
1027   gfx::Rect in(40, 50, 100, 200);
1028   gfx::Rect bounds(-4000, 0, 100000, 200+50+30);
1029   int64 target_area = 400 * 400;
1030   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
1031       in, target_area, bounds, NULL);
1032   EXPECT_EQ(30, out.bottom() - in.bottom());
1033   EXPECT_EQ(50, in.y() - out.y());
1034   EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
1035   EXPECT_LE(out.width() * out.height(), target_area);
1036   EXPECT_GT(out.width() * out.height(),
1037             target_area - out.height() * 2);
1038   EXPECT_TRUE(bounds.Contains(out));
1039 }
1040
1041 TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsFarAway) {
1042   gfx::Rect in(400, 500, 100, 200);
1043   gfx::Rect bounds(0, 0, 10, 10);
1044   int64 target_area = 400 * 400;
1045   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
1046       in, target_area, bounds, NULL);
1047   EXPECT_TRUE(out.IsEmpty());
1048 }
1049
1050 TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsExpandedFullyCover) {
1051   gfx::Rect in(40, 50, 100, 100);
1052   gfx::Rect bounds(0, 0, 10, 10);
1053   int64 target_area = 400 * 400;
1054   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
1055       in, target_area, bounds, NULL);
1056   EXPECT_EQ(bounds.ToString(), out.ToString());
1057 }
1058
1059 TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsExpandedPartlyCover) {
1060   gfx::Rect in(600, 600, 100, 100);
1061   gfx::Rect bounds(0, 0, 500, 500);
1062   int64 target_area = 400 * 400;
1063   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
1064       in, target_area, bounds, NULL);
1065   EXPECT_EQ(bounds.right(), out.right());
1066   EXPECT_EQ(bounds.bottom(), out.bottom());
1067   EXPECT_LE(out.width() * out.height(), target_area);
1068   EXPECT_GT(out.width() * out.height(),
1069             target_area - out.width() - out.height());
1070   EXPECT_TRUE(bounds.Contains(out));
1071 }
1072
1073 TEST(PictureLayerTilingTest, EmptyStartingRect) {
1074   // If a layer has a non-invertible transform, then the starting rect
1075   // for the layer would be empty.
1076   gfx::Rect in(40, 40, 0, 0);
1077   gfx::Rect bounds(0, 0, 10, 10);
1078   int64 target_area = 400 * 400;
1079   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
1080       in, target_area, bounds, NULL);
1081   EXPECT_TRUE(out.IsEmpty());
1082 }
1083
1084 TEST(PictureLayerTilingTest, TilingRasterTileIteratorStaticViewport) {
1085   FakePictureLayerTilingClient client;
1086   scoped_ptr<TestablePictureLayerTiling> tiling;
1087
1088   gfx::Rect viewport(50, 50, 100, 100);
1089   gfx::Size layer_bounds(800, 800);
1090
1091   gfx::Rect soon_rect = viewport;
1092   soon_rect.Inset(-312.f, -312.f, -312.f, -312.f);
1093
1094   client.SetTileSize(gfx::Size(30, 30));
1095   client.set_tree(ACTIVE_TREE);
1096
1097   tiling = TestablePictureLayerTiling::Create(1.0f, layer_bounds, &client);
1098   tiling->ComputeTilePriorityRects(
1099       ACTIVE_TREE, viewport, 1.0f, 1.0, Occlusion());
1100   tiling->UpdateAllTilePrioritiesForTesting();
1101
1102   PictureLayerTiling::TilingRasterTileIterator empty_iterator;
1103   EXPECT_FALSE(empty_iterator);
1104
1105   std::vector<Tile*> all_tiles = tiling->AllTilesForTesting();
1106
1107   // Sanity check.
1108   EXPECT_EQ(841u, all_tiles.size());
1109
1110   // The explanation of each iteration is as follows:
1111   // 1. First iteration tests that we can get all of the tiles correctly.
1112   // 2. Second iteration ensures that we can get all of the tiles again (first
1113   //    iteration didn't change any tiles), as well set all tiles to be ready to
1114   //    draw.
1115   // 3. Third iteration ensures that no tiles are returned, since they were all
1116   //    marked as ready to draw.
1117   for (int i = 0; i < 3; ++i) {
1118     PictureLayerTiling::TilingRasterTileIterator it(tiling.get());
1119
1120     // There are 3 bins in TilePriority.
1121     bool have_tiles[3] = {};
1122
1123     // On the third iteration, we should get no tiles since everything was
1124     // marked as ready to draw.
1125     if (i == 2) {
1126       EXPECT_FALSE(it);
1127       continue;
1128     }
1129
1130     EXPECT_TRUE(it);
1131     std::set<Tile*> unique_tiles;
1132     unique_tiles.insert(*it);
1133     Tile* last_tile = *it;
1134     have_tiles[last_tile->priority(ACTIVE_TREE).priority_bin] = true;
1135
1136     // On the second iteration, mark everything as ready to draw (solid color).
1137     if (i == 1) {
1138       ManagedTileState::DrawInfo& draw_info = last_tile->draw_info();
1139       draw_info.SetSolidColorForTesting(SK_ColorRED);
1140     }
1141     ++it;
1142     int eventually_bin_order_correct_count = 0;
1143     int eventually_bin_order_incorrect_count = 0;
1144     while (it) {
1145       Tile* new_tile = *it;
1146       ++it;
1147       unique_tiles.insert(new_tile);
1148
1149       TilePriority last_priority = last_tile->priority(ACTIVE_TREE);
1150       TilePriority new_priority = new_tile->priority(ACTIVE_TREE);
1151       EXPECT_LE(last_priority.priority_bin, new_priority.priority_bin);
1152       if (last_priority.priority_bin == new_priority.priority_bin) {
1153         if (last_priority.priority_bin == TilePriority::EVENTUALLY) {
1154           bool order_correct = last_priority.distance_to_visible <=
1155                                new_priority.distance_to_visible;
1156           eventually_bin_order_correct_count += order_correct;
1157           eventually_bin_order_incorrect_count += !order_correct;
1158         } else if (!soon_rect.Intersects(new_tile->content_rect()) &&
1159                    !soon_rect.Intersects(last_tile->content_rect())) {
1160           EXPECT_LE(last_priority.distance_to_visible,
1161                     new_priority.distance_to_visible);
1162           EXPECT_EQ(TilePriority::NOW, new_priority.priority_bin);
1163         } else if (new_priority.distance_to_visible > 0.f) {
1164           EXPECT_EQ(TilePriority::SOON, new_priority.priority_bin);
1165         }
1166       }
1167       have_tiles[new_priority.priority_bin] = true;
1168
1169       last_tile = new_tile;
1170
1171       // On the second iteration, mark everything as ready to draw (solid
1172       // color).
1173       if (i == 1) {
1174         ManagedTileState::DrawInfo& draw_info = last_tile->draw_info();
1175         draw_info.SetSolidColorForTesting(SK_ColorRED);
1176       }
1177     }
1178
1179     EXPECT_GT(eventually_bin_order_correct_count,
1180               eventually_bin_order_incorrect_count);
1181
1182     // We should have now and eventually tiles, as well as soon tiles from
1183     // the border region.
1184     EXPECT_TRUE(have_tiles[TilePriority::NOW]);
1185     EXPECT_TRUE(have_tiles[TilePriority::SOON]);
1186     EXPECT_TRUE(have_tiles[TilePriority::EVENTUALLY]);
1187
1188     EXPECT_EQ(unique_tiles.size(), all_tiles.size());
1189   }
1190 }
1191
1192 TEST(PictureLayerTilingTest, TilingRasterTileIteratorMovingViewport) {
1193   FakePictureLayerTilingClient client;
1194   scoped_ptr<TestablePictureLayerTiling> tiling;
1195
1196   gfx::Rect viewport(50, 0, 100, 100);
1197   gfx::Rect moved_viewport(50, 0, 100, 500);
1198   gfx::Size layer_bounds(1000, 1000);
1199
1200   client.SetTileSize(gfx::Size(30, 30));
1201   client.set_tree(ACTIVE_TREE);
1202
1203   tiling = TestablePictureLayerTiling::Create(1.f, layer_bounds, &client);
1204   tiling->ComputeTilePriorityRects(
1205       ACTIVE_TREE, viewport, 1.0f, 1.0, Occlusion());
1206   tiling->ComputeTilePriorityRects(
1207       ACTIVE_TREE, moved_viewport, 1.0f, 2.0, Occlusion());
1208   tiling->UpdateAllTilePrioritiesForTesting();
1209
1210   gfx::Rect soon_rect = moved_viewport;
1211   soon_rect.Inset(-312.f, -312.f, -312.f, -312.f);
1212
1213   // There are 3 bins in TilePriority.
1214   bool have_tiles[3] = {};
1215   Tile* last_tile = NULL;
1216   int eventually_bin_order_correct_count = 0;
1217   int eventually_bin_order_incorrect_count = 0;
1218   for (PictureLayerTiling::TilingRasterTileIterator it(tiling.get()); it;
1219        ++it) {
1220     if (!last_tile)
1221       last_tile = *it;
1222
1223     Tile* new_tile = *it;
1224
1225     TilePriority last_priority = last_tile->priority(ACTIVE_TREE);
1226     TilePriority new_priority = new_tile->priority(ACTIVE_TREE);
1227
1228     have_tiles[new_priority.priority_bin] = true;
1229
1230     EXPECT_LE(last_priority.priority_bin, new_priority.priority_bin);
1231     if (last_priority.priority_bin == new_priority.priority_bin) {
1232       if (last_priority.priority_bin == TilePriority::EVENTUALLY) {
1233         bool order_correct = last_priority.distance_to_visible <=
1234                              new_priority.distance_to_visible;
1235         eventually_bin_order_correct_count += order_correct;
1236         eventually_bin_order_incorrect_count += !order_correct;
1237       } else if (!soon_rect.Intersects(new_tile->content_rect()) &&
1238                  !soon_rect.Intersects(last_tile->content_rect())) {
1239         EXPECT_LE(last_priority.distance_to_visible,
1240                   new_priority.distance_to_visible);
1241       } else if (new_priority.distance_to_visible > 0.f) {
1242         EXPECT_EQ(TilePriority::SOON, new_priority.priority_bin);
1243       }
1244     }
1245     last_tile = new_tile;
1246   }
1247
1248   EXPECT_GT(eventually_bin_order_correct_count,
1249             eventually_bin_order_incorrect_count);
1250
1251   EXPECT_TRUE(have_tiles[TilePriority::NOW]);
1252   EXPECT_TRUE(have_tiles[TilePriority::SOON]);
1253   EXPECT_TRUE(have_tiles[TilePriority::EVENTUALLY]);
1254 }
1255
1256 static void TileExists(bool exists, Tile* tile,
1257                        const gfx::Rect& geometry_rect) {
1258   EXPECT_EQ(exists, tile != NULL) << geometry_rect.ToString();
1259 }
1260
1261 TEST(PictureLayerTilingTest, TilingEvictionTileIteratorStaticViewport) {
1262   FakeOutputSurfaceClient output_surface_client;
1263   scoped_ptr<FakeOutputSurface> output_surface = FakeOutputSurface::Create3d();
1264   CHECK(output_surface->BindToClient(&output_surface_client));
1265   TestSharedBitmapManager shared_bitmap_manager;
1266   scoped_ptr<ResourceProvider> resource_provider = ResourceProvider::Create(
1267       output_surface.get(), &shared_bitmap_manager, NULL, NULL, 0, false, 1);
1268
1269   FakePictureLayerTilingClient client(resource_provider.get());
1270   scoped_ptr<TestablePictureLayerTiling> tiling;
1271
1272   gfx::Rect viewport(50, 50, 100, 100);
1273   gfx::Size layer_bounds(2000, 2000);
1274
1275   client.SetTileSize(gfx::Size(30, 30));
1276   client.set_tree(ACTIVE_TREE);
1277
1278   tiling = TestablePictureLayerTiling::Create(1.0f, layer_bounds, &client);
1279   tiling->ComputeTilePriorityRects(
1280       ACTIVE_TREE, viewport, 1.0f, 1.0, Occlusion());
1281   tiling->UpdateAllTilePrioritiesForTesting();
1282
1283   PictureLayerTiling::TilingRasterTileIterator empty_iterator;
1284   EXPECT_FALSE(empty_iterator);
1285
1286   std::vector<Tile*> all_tiles = tiling->AllTilesForTesting();
1287
1288   PictureLayerTiling::TilingEvictionTileIterator it(
1289       tiling.get(), SMOOTHNESS_TAKES_PRIORITY, PictureLayerTiling::NOW);
1290
1291   // Tiles don't have resources to evict.
1292   EXPECT_FALSE(it);
1293
1294   // Sanity check.
1295   EXPECT_EQ(5184u, all_tiles.size());
1296
1297   client.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
1298
1299   std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
1300
1301   std::set<Tile*> eviction_tiles;
1302
1303   it = PictureLayerTiling::TilingEvictionTileIterator(
1304       tiling.get(), SMOOTHNESS_TAKES_PRIORITY, PictureLayerTiling::EVENTUALLY);
1305   EXPECT_TRUE(it);
1306   for (; it; ++it) {
1307     Tile* tile = *it;
1308     EXPECT_TRUE(tile);
1309     EXPECT_EQ(TilePriority::EVENTUALLY,
1310               tile->priority(ACTIVE_TREE).priority_bin);
1311     EXPECT_FALSE(tile->required_for_activation());
1312     eviction_tiles.insert(tile);
1313   }
1314
1315   it = PictureLayerTiling::TilingEvictionTileIterator(
1316       tiling.get(), SMOOTHNESS_TAKES_PRIORITY, PictureLayerTiling::SOON);
1317   EXPECT_TRUE(it);
1318   for (; it; ++it) {
1319     Tile* tile = *it;
1320     EXPECT_TRUE(tile);
1321     EXPECT_EQ(TilePriority::SOON, tile->priority(ACTIVE_TREE).priority_bin);
1322     EXPECT_FALSE(tile->required_for_activation());
1323     eviction_tiles.insert(tile);
1324   }
1325
1326   it = PictureLayerTiling::TilingEvictionTileIterator(
1327       tiling.get(), SMOOTHNESS_TAKES_PRIORITY, PictureLayerTiling::NOW);
1328   EXPECT_TRUE(it);
1329   for (; it; ++it) {
1330     Tile* tile = *it;
1331     EXPECT_TRUE(tile);
1332     EXPECT_EQ(TilePriority::NOW, tile->priority(ACTIVE_TREE).priority_bin);
1333     EXPECT_FALSE(tile->required_for_activation());
1334     eviction_tiles.insert(tile);
1335   }
1336
1337   it = PictureLayerTiling::TilingEvictionTileIterator(
1338       tiling.get(),
1339       SMOOTHNESS_TAKES_PRIORITY,
1340       PictureLayerTiling::NOW_AND_REQUIRED_FOR_ACTIVATION);
1341   EXPECT_FALSE(it);
1342
1343   EXPECT_GT(all_tiles_set.size(), 0u);
1344   EXPECT_EQ(all_tiles_set, eviction_tiles);
1345
1346   EXPECT_TRUE(tiling->eviction_tiles_cache_valid());
1347   tiling->RemoveTileAt(0, 0, nullptr);
1348   EXPECT_FALSE(tiling->eviction_tiles_cache_valid());
1349
1350   it = PictureLayerTiling::TilingEvictionTileIterator(
1351       tiling.get(), SMOOTHNESS_TAKES_PRIORITY,
1352       PictureLayerTiling::NOW_AND_REQUIRED_FOR_ACTIVATION);
1353   EXPECT_TRUE(tiling->eviction_tiles_cache_valid());
1354   tiling->Reset();
1355   EXPECT_FALSE(tiling->eviction_tiles_cache_valid());
1356 }
1357
1358 TEST_F(PictureLayerTilingIteratorTest, TilesExist) {
1359   gfx::Size layer_bounds(1099, 801);
1360   Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
1361   VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
1362   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
1363
1364   client_.set_tree(ACTIVE_TREE);
1365   tiling_->ComputeTilePriorityRects(
1366       ACTIVE_TREE,
1367       gfx::Rect(layer_bounds),  // visible content rect
1368       1.f,                      // current contents scale
1369       1.0,                      // current frame time
1370       Occlusion());
1371   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
1372
1373   // Make the viewport rect empty. All tiles are killed and become zombies.
1374   tiling_->ComputeTilePriorityRects(ACTIVE_TREE,
1375                                     gfx::Rect(),  // visible content rect
1376                                     1.f,          // current contents scale
1377                                     2.0,          // current frame time
1378                                     Occlusion());
1379   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
1380 }
1381
1382 TEST_F(PictureLayerTilingIteratorTest, TilesExistGiantViewport) {
1383   gfx::Size layer_bounds(1099, 801);
1384   Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
1385   VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
1386   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
1387
1388   gfx::Rect giant_rect(-10000000, -10000000, 1000000000, 1000000000);
1389
1390   client_.set_tree(ACTIVE_TREE);
1391   tiling_->ComputeTilePriorityRects(
1392       ACTIVE_TREE,
1393       gfx::Rect(layer_bounds),  // visible content rect
1394       1.f,                      // current contents scale
1395       1.0,                      // current frame time
1396       Occlusion());
1397   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
1398
1399   // If the visible content rect is empty, it should still have live tiles.
1400   tiling_->ComputeTilePriorityRects(ACTIVE_TREE,
1401                                     giant_rect,  // visible content rect
1402                                     1.f,         // current contents scale
1403                                     2.0,         // current frame time
1404                                     Occlusion());
1405   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
1406 }
1407
1408 TEST_F(PictureLayerTilingIteratorTest, TilesExistOutsideViewport) {
1409   gfx::Size layer_bounds(1099, 801);
1410   Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
1411   VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
1412   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
1413
1414   // This rect does not intersect with the layer, as the layer is outside the
1415   // viewport.
1416   gfx::Rect viewport_rect(1100, 0, 1000, 1000);
1417   EXPECT_FALSE(viewport_rect.Intersects(gfx::Rect(layer_bounds)));
1418
1419   client_.set_tree(ACTIVE_TREE);
1420   tiling_->ComputeTilePriorityRects(ACTIVE_TREE,
1421                                     viewport_rect,  // visible content rect
1422                                     1.f,            // current contents scale
1423                                     1.0,            // current frame time
1424                                     Occlusion());
1425   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
1426 }
1427
1428 static void TilesIntersectingRectExist(const gfx::Rect& rect,
1429                                        bool intersect_exists,
1430                                        Tile* tile,
1431                                        const gfx::Rect& geometry_rect) {
1432   bool intersects = rect.Intersects(geometry_rect);
1433   bool expected_exists = intersect_exists ? intersects : !intersects;
1434   EXPECT_EQ(expected_exists, tile != NULL)
1435       << "Rects intersecting " << rect.ToString() << " should exist. "
1436       << "Current tile rect is " << geometry_rect.ToString();
1437 }
1438
1439 TEST_F(PictureLayerTilingIteratorTest,
1440        TilesExistLargeViewportAndLayerWithSmallVisibleArea) {
1441   gfx::Size layer_bounds(10000, 10000);
1442   Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
1443   VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
1444   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
1445
1446   gfx::Rect visible_rect(8000, 8000, 50, 50);
1447
1448   client_.set_tree(ACTIVE_TREE);
1449   set_max_tiles_for_interest_area(1);
1450   tiling_->ComputeTilePriorityRects(ACTIVE_TREE,
1451                                     visible_rect,  // visible content rect
1452                                     1.f,           // current contents scale
1453                                     1.0,           // current frame time
1454                                     Occlusion());
1455   VerifyTiles(1.f,
1456               gfx::Rect(layer_bounds),
1457               base::Bind(&TilesIntersectingRectExist, visible_rect, true));
1458 }
1459
1460 TEST_F(PictureLayerTilingIteratorTest, AddTilingsToMatchScale) {
1461   gfx::Size layer_bounds(1099, 801);
1462   gfx::Size tile_size(100, 100);
1463
1464   client_.SetTileSize(tile_size);
1465   client_.set_tree(PENDING_TREE);
1466
1467   PictureLayerTilingSet active_set(&client_);
1468
1469   active_set.AddTiling(1.f, layer_bounds);
1470
1471   VerifyTiles(active_set.tiling_at(0),
1472               1.f,
1473               gfx::Rect(layer_bounds),
1474               base::Bind(&TileExists, false));
1475
1476   UpdateAllTilePriorities(&active_set,
1477                           PENDING_TREE,
1478                           gfx::Rect(layer_bounds),  // visible content rect
1479                           1.f,                      // current contents scale
1480                           1.0);                     // current frame time
1481
1482   // The active tiling has tiles now.
1483   VerifyTiles(active_set.tiling_at(0),
1484               1.f,
1485               gfx::Rect(layer_bounds),
1486               base::Bind(&TileExists, true));
1487
1488   // Add the same tilings to the pending set.
1489   PictureLayerTilingSet pending_set(&client_);
1490   Region invalidation;
1491   pending_set.SyncTilings(active_set, layer_bounds, invalidation, 0.f);
1492
1493   // The pending tiling starts with no tiles.
1494   VerifyTiles(pending_set.tiling_at(0),
1495               1.f,
1496               gfx::Rect(layer_bounds),
1497               base::Bind(&TileExists, false));
1498
1499   // ComputeTilePriorityRects on the pending tiling at the same frame time. The
1500   // pending tiling should get tiles.
1501   UpdateAllTilePriorities(&pending_set,
1502                           PENDING_TREE,
1503                           gfx::Rect(layer_bounds),  // visible content rect
1504                           1.f,                      // current contents scale
1505                           1.0);                     // current frame time
1506
1507   VerifyTiles(pending_set.tiling_at(0),
1508               1.f,
1509               gfx::Rect(layer_bounds),
1510               base::Bind(&TileExists, true));
1511 }
1512
1513 TEST(ComputeTilePriorityRectsTest, VisibleTiles) {
1514   // The TilePriority of visible tiles should have zero distance_to_visible
1515   // and time_to_visible.
1516
1517   FakePictureLayerTilingClient client;
1518   scoped_ptr<TestablePictureLayerTiling> tiling;
1519
1520   gfx::Size device_viewport(800, 600);
1521   gfx::Size last_layer_bounds(200, 200);
1522   gfx::Size current_layer_bounds(200, 200);
1523   float current_layer_contents_scale = 1.f;
1524   gfx::Transform current_screen_transform;
1525   double current_frame_time_in_seconds = 1.0;
1526
1527   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1528       current_screen_transform, device_viewport);
1529
1530   client.SetTileSize(gfx::Size(100, 100));
1531   client.set_tree(ACTIVE_TREE);
1532   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1533                                               current_layer_bounds,
1534                                               &client);
1535
1536   tiling->ComputeTilePriorityRects(ACTIVE_TREE,
1537                                    viewport_in_layer_space,
1538                                    current_layer_contents_scale,
1539                                    current_frame_time_in_seconds,
1540                                    Occlusion());
1541   tiling->UpdateAllTilePrioritiesForTesting();
1542
1543   ASSERT_TRUE(tiling->TileAt(0, 0));
1544   ASSERT_TRUE(tiling->TileAt(0, 1));
1545   ASSERT_TRUE(tiling->TileAt(1, 0));
1546   ASSERT_TRUE(tiling->TileAt(1, 1));
1547
1548   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1549   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1550   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1551
1552   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1553   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1554   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1555
1556   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1557   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1558   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1559
1560   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1561   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1562   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1563 }
1564
1565 TEST(ComputeTilePriorityRectsTest, OffscreenTiles) {
1566   // The TilePriority of offscreen tiles (without movement) should have nonzero
1567   // distance_to_visible and infinite time_to_visible.
1568
1569   FakePictureLayerTilingClient client;
1570   scoped_ptr<TestablePictureLayerTiling> tiling;
1571
1572   gfx::Size device_viewport(800, 600);
1573   gfx::Size last_layer_bounds(200, 200);
1574   gfx::Size current_layer_bounds(200, 200);
1575   float current_layer_contents_scale = 1.f;
1576   gfx::Transform last_screen_transform;
1577   gfx::Transform current_screen_transform;
1578   double current_frame_time_in_seconds = 1.0;
1579
1580   current_screen_transform.Translate(850, 0);
1581   last_screen_transform = current_screen_transform;
1582
1583   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1584       current_screen_transform, device_viewport);
1585
1586   client.SetTileSize(gfx::Size(100, 100));
1587   client.set_tree(ACTIVE_TREE);
1588   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1589                                               current_layer_bounds,
1590                                               &client);
1591
1592   tiling->ComputeTilePriorityRects(ACTIVE_TREE,
1593                                    viewport_in_layer_space,
1594                                    current_layer_contents_scale,
1595                                    current_frame_time_in_seconds,
1596                                    Occlusion());
1597   tiling->UpdateAllTilePrioritiesForTesting();
1598
1599   ASSERT_TRUE(tiling->TileAt(0, 0));
1600   ASSERT_TRUE(tiling->TileAt(0, 1));
1601   ASSERT_TRUE(tiling->TileAt(1, 0));
1602   ASSERT_TRUE(tiling->TileAt(1, 1));
1603
1604   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1605   EXPECT_GT(priority.distance_to_visible, 0.f);
1606   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1607
1608   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1609   EXPECT_GT(priority.distance_to_visible, 0.f);
1610   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1611
1612   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1613   EXPECT_GT(priority.distance_to_visible, 0.f);
1614   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1615
1616   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1617   EXPECT_GT(priority.distance_to_visible, 0.f);
1618   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1619
1620   // Furthermore, in this scenario tiles on the right hand side should have a
1621   // larger distance to visible.
1622   TilePriority left = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1623   TilePriority right = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1624   EXPECT_GT(right.distance_to_visible, left.distance_to_visible);
1625
1626   left = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1627   right = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1628   EXPECT_GT(right.distance_to_visible, left.distance_to_visible);
1629 }
1630
1631 TEST(ComputeTilePriorityRectsTest, PartiallyOffscreenLayer) {
1632   // Sanity check that a layer with some tiles visible and others offscreen has
1633   // correct TilePriorities for each tile.
1634
1635   FakePictureLayerTilingClient client;
1636   scoped_ptr<TestablePictureLayerTiling> tiling;
1637
1638   gfx::Size device_viewport(800, 600);
1639   gfx::Size last_layer_bounds(200, 200);
1640   gfx::Size current_layer_bounds(200, 200);
1641   float current_layer_contents_scale = 1.f;
1642   gfx::Transform last_screen_transform;
1643   gfx::Transform current_screen_transform;
1644   double current_frame_time_in_seconds = 1.0;
1645
1646   current_screen_transform.Translate(705, 505);
1647   last_screen_transform = current_screen_transform;
1648
1649   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1650       current_screen_transform, device_viewport);
1651
1652   client.SetTileSize(gfx::Size(100, 100));
1653   client.set_tree(ACTIVE_TREE);
1654   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1655                                               current_layer_bounds,
1656                                               &client);
1657
1658   tiling->ComputeTilePriorityRects(ACTIVE_TREE,
1659                                    viewport_in_layer_space,
1660                                    current_layer_contents_scale,
1661                                    current_frame_time_in_seconds,
1662                                    Occlusion());
1663   tiling->UpdateAllTilePrioritiesForTesting();
1664
1665   ASSERT_TRUE(tiling->TileAt(0, 0));
1666   ASSERT_TRUE(tiling->TileAt(0, 1));
1667   ASSERT_TRUE(tiling->TileAt(1, 0));
1668   ASSERT_TRUE(tiling->TileAt(1, 1));
1669
1670   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1671   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1672   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1673
1674   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1675   EXPECT_GT(priority.distance_to_visible, 0.f);
1676   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1677
1678   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1679   EXPECT_GT(priority.distance_to_visible, 0.f);
1680   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1681
1682   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1683   EXPECT_GT(priority.distance_to_visible, 0.f);
1684   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1685 }
1686
1687 TEST(ComputeTilePriorityRectsTest, PartiallyOffscreenRotatedLayer) {
1688   // Each tile of a layer may be affected differently by a transform; Check
1689   // that ComputeTilePriorityRects correctly accounts for the transform between
1690   // layer space and screen space.
1691
1692   FakePictureLayerTilingClient client;
1693   scoped_ptr<TestablePictureLayerTiling> tiling;
1694
1695   gfx::Size device_viewport(800, 600);
1696   gfx::Size last_layer_bounds(200, 200);
1697   gfx::Size current_layer_bounds(200, 200);
1698   float current_layer_contents_scale = 1.f;
1699   gfx::Transform last_screen_transform;
1700   gfx::Transform current_screen_transform;
1701   double current_frame_time_in_seconds = 1.0;
1702
1703   // A diagonally rotated layer that is partially off the bottom of the screen.
1704   // In this configuration, only the top-left tile would be visible.
1705   current_screen_transform.Translate(600, 750);
1706   current_screen_transform.RotateAboutZAxis(45);
1707   last_screen_transform = current_screen_transform;
1708
1709   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1710       current_screen_transform, device_viewport);
1711
1712   client.SetTileSize(gfx::Size(100, 100));
1713   client.set_tree(ACTIVE_TREE);
1714   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1715                                               current_layer_bounds,
1716                                               &client);
1717
1718   tiling->ComputeTilePriorityRects(ACTIVE_TREE,
1719                                    viewport_in_layer_space,
1720                                    current_layer_contents_scale,
1721                                    current_frame_time_in_seconds,
1722                                    Occlusion());
1723   tiling->UpdateAllTilePrioritiesForTesting();
1724
1725   ASSERT_TRUE(tiling->TileAt(0, 0));
1726   ASSERT_TRUE(tiling->TileAt(0, 1));
1727   ASSERT_TRUE(tiling->TileAt(1, 0));
1728   ASSERT_TRUE(tiling->TileAt(1, 1));
1729
1730   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1731   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1732   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1733
1734   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1735   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1736   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1737
1738   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1739   EXPECT_GT(priority.distance_to_visible, 0.f);
1740   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1741
1742   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1743   EXPECT_GT(priority.distance_to_visible, 0.f);
1744   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1745
1746   // Furthermore, in this scenario the bottom-right tile should have the larger
1747   // distance to visible.
1748   TilePriority top_left = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1749   TilePriority top_right = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1750   TilePriority bottom_right = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1751   EXPECT_GT(top_right.distance_to_visible, top_left.distance_to_visible);
1752
1753   EXPECT_EQ(bottom_right.distance_to_visible, top_right.distance_to_visible);
1754 }
1755
1756 TEST(ComputeTilePriorityRectsTest, PerspectiveLayer) {
1757   // Perspective transforms need to take a different code path.
1758   // This test checks tile priorities of a perspective layer.
1759
1760   FakePictureLayerTilingClient client;
1761   scoped_ptr<TestablePictureLayerTiling> tiling;
1762
1763   gfx::Size device_viewport(800, 600);
1764   gfx::Rect visible_layer_rect(0, 0, 0, 0);  // offscreen.
1765   gfx::Size last_layer_bounds(200, 200);
1766   gfx::Size current_layer_bounds(200, 200);
1767   float current_layer_contents_scale = 1.f;
1768   gfx::Transform last_screen_transform;
1769   gfx::Transform current_screen_transform;
1770   double current_frame_time_in_seconds = 1.0;
1771
1772   // A 3d perspective layer rotated about its Y axis, translated to almost
1773   // fully offscreen. The left side will appear closer (i.e. larger in 2d) than
1774   // the right side, so the top-left tile will technically be closer than the
1775   // top-right.
1776
1777   // Translate layer to offscreen
1778   current_screen_transform.Translate(400.0, 630.0);
1779   // Apply perspective about the center of the layer
1780   current_screen_transform.Translate(100.0, 100.0);
1781   current_screen_transform.ApplyPerspectiveDepth(100.0);
1782   current_screen_transform.RotateAboutYAxis(10.0);
1783   current_screen_transform.Translate(-100.0, -100.0);
1784   last_screen_transform = current_screen_transform;
1785
1786   // Sanity check that this transform wouldn't cause w<0 clipping.
1787   bool clipped;
1788   MathUtil::MapQuad(current_screen_transform,
1789                     gfx::QuadF(gfx::RectF(0, 0, 200, 200)),
1790                     &clipped);
1791   ASSERT_FALSE(clipped);
1792
1793   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1794       current_screen_transform, device_viewport);
1795
1796   client.SetTileSize(gfx::Size(100, 100));
1797   client.set_tree(ACTIVE_TREE);
1798   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1799                                               current_layer_bounds,
1800                                               &client);
1801
1802   tiling->ComputeTilePriorityRects(ACTIVE_TREE,
1803                                    viewport_in_layer_space,
1804                                    current_layer_contents_scale,
1805                                    current_frame_time_in_seconds,
1806                                    Occlusion());
1807   tiling->UpdateAllTilePrioritiesForTesting();
1808
1809   ASSERT_TRUE(tiling->TileAt(0, 0));
1810   ASSERT_TRUE(tiling->TileAt(0, 1));
1811   ASSERT_TRUE(tiling->TileAt(1, 0));
1812   ASSERT_TRUE(tiling->TileAt(1, 1));
1813
1814   // All tiles will have a positive distance_to_visible
1815   // and an infinite time_to_visible.
1816   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1817   EXPECT_FLOAT_EQ(priority.distance_to_visible, 0.f);
1818   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1819
1820   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1821   EXPECT_GT(priority.distance_to_visible, 0.f);
1822   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1823
1824   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1825   EXPECT_FLOAT_EQ(priority.distance_to_visible, 0.f);
1826   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1827
1828   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1829   EXPECT_GT(priority.distance_to_visible, 0.f);
1830   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1831
1832   // Furthermore, in this scenario the top-left distance_to_visible
1833   // will be smallest, followed by top-right. The bottom layers
1834   // will of course be further than the top layers.
1835   TilePriority top_left = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1836   TilePriority top_right = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1837   TilePriority bottom_left = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1838   TilePriority bottom_right = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1839
1840   EXPECT_GT(bottom_right.distance_to_visible, top_right.distance_to_visible);
1841
1842   EXPECT_GT(bottom_left.distance_to_visible, top_left.distance_to_visible);
1843 }
1844
1845 TEST(ComputeTilePriorityRectsTest, PerspectiveLayerClippedByW) {
1846   // Perspective transforms need to take a different code path.
1847   // This test checks tile priorities of a perspective layer.
1848
1849   FakePictureLayerTilingClient client;
1850   scoped_ptr<TestablePictureLayerTiling> tiling;
1851
1852   gfx::Size device_viewport(800, 600);
1853   gfx::Size last_layer_bounds(200, 200);
1854   gfx::Size current_layer_bounds(200, 200);
1855   float current_layer_contents_scale = 1.f;
1856   gfx::Transform last_screen_transform;
1857   gfx::Transform current_screen_transform;
1858   double current_frame_time_in_seconds = 1.0;
1859
1860   // A 3d perspective layer rotated about its Y axis, translated to almost
1861   // fully offscreen. The left side will appear closer (i.e. larger in 2d) than
1862   // the right side, so the top-left tile will technically be closer than the
1863   // top-right.
1864
1865   // Translate layer to offscreen
1866   current_screen_transform.Translate(400.0, 970.0);
1867   // Apply perspective and rotation about the center of the layer
1868   current_screen_transform.Translate(100.0, 100.0);
1869   current_screen_transform.ApplyPerspectiveDepth(10.0);
1870   current_screen_transform.RotateAboutYAxis(10.0);
1871   current_screen_transform.Translate(-100.0, -100.0);
1872   last_screen_transform = current_screen_transform;
1873
1874   // Sanity check that this transform does cause w<0 clipping for the left side
1875   // of the layer, but not the right side.
1876   bool clipped;
1877   MathUtil::MapQuad(current_screen_transform,
1878                     gfx::QuadF(gfx::RectF(0, 0, 100, 200)),
1879                     &clipped);
1880   ASSERT_TRUE(clipped);
1881
1882   MathUtil::MapQuad(current_screen_transform,
1883                     gfx::QuadF(gfx::RectF(100, 0, 100, 200)),
1884                     &clipped);
1885   ASSERT_FALSE(clipped);
1886
1887   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1888       current_screen_transform, device_viewport);
1889
1890   client.SetTileSize(gfx::Size(100, 100));
1891   client.set_tree(ACTIVE_TREE);
1892   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1893                                               current_layer_bounds,
1894                                               &client);
1895
1896   tiling->ComputeTilePriorityRects(ACTIVE_TREE,
1897                                    viewport_in_layer_space,
1898                                    current_layer_contents_scale,
1899                                    current_frame_time_in_seconds,
1900                                    Occlusion());
1901   tiling->UpdateAllTilePrioritiesForTesting();
1902
1903   ASSERT_TRUE(tiling->TileAt(0, 0));
1904   ASSERT_TRUE(tiling->TileAt(0, 1));
1905   ASSERT_TRUE(tiling->TileAt(1, 0));
1906   ASSERT_TRUE(tiling->TileAt(1, 1));
1907
1908   // Left-side tiles will be clipped by the transform, so we have to assume
1909   // they are visible just in case.
1910   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1911   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1912   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1913
1914   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1915   EXPECT_GT(priority.distance_to_visible, 0.f);
1916   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1917
1918   // Right-side tiles will have a positive distance_to_visible
1919   // and an infinite time_to_visible.
1920   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1921   EXPECT_FLOAT_EQ(priority.distance_to_visible, 0.f);
1922   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1923
1924   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1925   EXPECT_GT(priority.distance_to_visible, 0.f);
1926   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1927 }
1928
1929 TEST(ComputeTilePriorityRectsTest, BasicMotion) {
1930   // Test that time_to_visible is computed correctly when
1931   // there is some motion.
1932
1933   FakePictureLayerTilingClient client;
1934   scoped_ptr<TestablePictureLayerTiling> tiling;
1935
1936   gfx::Size device_viewport(800, 600);
1937   gfx::Rect visible_layer_rect(0, 0, 0, 0);
1938   gfx::Size last_layer_bounds(200, 200);
1939   gfx::Size current_layer_bounds(200, 200);
1940   float last_layer_contents_scale = 1.f;
1941   float current_layer_contents_scale = 1.f;
1942   gfx::Transform last_screen_transform;
1943   gfx::Transform current_screen_transform;
1944   double last_frame_time_in_seconds = 1.0;
1945   double current_frame_time_in_seconds = 2.0;
1946
1947   // Offscreen layer is coming closer to viewport at 1000 pixels per second.
1948   current_screen_transform.Translate(1800, 0);
1949   last_screen_transform.Translate(2800, 0);
1950
1951   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1952       current_screen_transform, device_viewport);
1953
1954   client.SetTileSize(gfx::Size(100, 100));
1955   client.set_tree(ACTIVE_TREE);
1956   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1957                                               current_layer_bounds,
1958                                               &client);
1959
1960   // previous ("last") frame
1961   tiling->ComputeTilePriorityRects(ACTIVE_TREE,
1962                                    viewport_in_layer_space,
1963                                    last_layer_contents_scale,
1964                                    last_frame_time_in_seconds,
1965                                    Occlusion());
1966
1967   // current frame
1968   tiling->ComputeTilePriorityRects(ACTIVE_TREE,
1969                                    viewport_in_layer_space,
1970                                    current_layer_contents_scale,
1971                                    current_frame_time_in_seconds,
1972                                    Occlusion());
1973   tiling->UpdateAllTilePrioritiesForTesting();
1974
1975   ASSERT_TRUE(tiling->TileAt(0, 0));
1976   ASSERT_TRUE(tiling->TileAt(0, 1));
1977   ASSERT_TRUE(tiling->TileAt(1, 0));
1978   ASSERT_TRUE(tiling->TileAt(1, 1));
1979
1980   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1981   EXPECT_GT(priority.distance_to_visible, 0.f);
1982   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1983
1984   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1985   EXPECT_GT(priority.distance_to_visible, 0.f);
1986   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1987
1988   // time_to_visible for the right hand side layers needs an extra 0.099
1989   // seconds because this tile is 99 pixels further away.
1990   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1991   EXPECT_GT(priority.distance_to_visible, 0.f);
1992   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1993
1994   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1995   EXPECT_GT(priority.distance_to_visible, 0.f);
1996   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1997 }
1998
1999 TEST(ComputeTilePriorityRectsTest, RotationMotion) {
2000   // Each tile of a layer may be affected differently by a transform; Check
2001   // that ComputeTilePriorityRects correctly accounts for the transform between
2002   // layer space and screen space.
2003
2004   FakePictureLayerTilingClient client;
2005   scoped_ptr<TestablePictureLayerTiling> tiling;
2006
2007   gfx::Size device_viewport(800, 600);
2008   gfx::Rect visible_layer_rect(0, 0, 0, 0);  // offscren.
2009   gfx::Size last_layer_bounds(200, 200);
2010   gfx::Size current_layer_bounds(200, 200);
2011   float last_layer_contents_scale = 1.f;
2012   float current_layer_contents_scale = 1.f;
2013   gfx::Transform last_screen_transform;
2014   gfx::Transform current_screen_transform;
2015   double last_frame_time_in_seconds = 1.0;
2016   double current_frame_time_in_seconds = 2.0;
2017
2018   // Rotation motion is set up specifically so that:
2019   //  - rotation occurs about the center of the layer
2020   //  - the top-left tile becomes visible on rotation
2021   //  - the top-right tile will have an infinite time_to_visible
2022   //    because it is rotating away from viewport.
2023   //  - bottom-left layer will have a positive non-zero time_to_visible
2024   //    because it is rotating toward the viewport.
2025   current_screen_transform.Translate(400, 550);
2026   current_screen_transform.RotateAboutZAxis(45);
2027
2028   last_screen_transform.Translate(400, 550);
2029
2030   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
2031       current_screen_transform, device_viewport);
2032
2033   client.SetTileSize(gfx::Size(100, 100));
2034   client.set_tree(ACTIVE_TREE);
2035   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
2036                                               current_layer_bounds,
2037                                               &client);
2038
2039   // previous ("last") frame
2040   tiling->ComputeTilePriorityRects(ACTIVE_TREE,
2041                                    viewport_in_layer_space,
2042                                    last_layer_contents_scale,
2043                                    last_frame_time_in_seconds,
2044                                    Occlusion());
2045
2046   // current frame
2047   tiling->ComputeTilePriorityRects(ACTIVE_TREE,
2048                                    viewport_in_layer_space,
2049                                    current_layer_contents_scale,
2050                                    current_frame_time_in_seconds,
2051                                    Occlusion());
2052   tiling->UpdateAllTilePrioritiesForTesting();
2053
2054   ASSERT_TRUE(tiling->TileAt(0, 0));
2055   ASSERT_TRUE(tiling->TileAt(0, 1));
2056   ASSERT_TRUE(tiling->TileAt(1, 0));
2057   ASSERT_TRUE(tiling->TileAt(1, 1));
2058
2059   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
2060   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
2061   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2062
2063   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
2064   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
2065   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2066
2067   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
2068   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
2069   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2070 }
2071
2072 TEST(PictureLayerTilingTest, RecycledTilesCleared) {
2073   // This test performs the following:
2074   // Setup:
2075   // - Two tilings, one active one recycled with all tiles shared.
2076   // Procedure:
2077   // - Viewport moves somewhere far away and active tiling clears tiles.
2078   // - Viewport moves back and a new active tiling tile is created.
2079   // Result:
2080   // - Recycle tiling does _not_ have the tile in the same location (thus it
2081   //   will be shared next time a pending tiling is created).
2082
2083   FakePictureLayerTilingClient active_client;
2084   scoped_ptr<TestablePictureLayerTiling> active_tiling;
2085
2086   active_client.SetTileSize(gfx::Size(100, 100));
2087   active_client.set_tree(ACTIVE_TREE);
2088   active_client.set_max_tiles_for_interest_area(10);
2089   active_tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
2090                                                      gfx::Size(10000, 10000),
2091                                                      &active_client);
2092   // Create all tiles on this tiling.
2093   active_tiling->ComputeTilePriorityRects(
2094       ACTIVE_TREE, gfx::Rect(0, 0, 100, 100), 1.0f, 1.0f, Occlusion());
2095
2096   FakePictureLayerTilingClient recycle_client;
2097   recycle_client.SetTileSize(gfx::Size(100, 100));
2098   recycle_client.set_tree(PENDING_TREE);
2099   recycle_client.set_twin_tiling(active_tiling.get());
2100   recycle_client.set_max_tiles_for_interest_area(10);
2101
2102   scoped_ptr<TestablePictureLayerTiling> recycle_tiling;
2103   recycle_tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
2104                                                       gfx::Size(10000, 10000),
2105                                                       &recycle_client);
2106
2107   // Create all tiles on the second tiling. All tiles should be shared.
2108   recycle_tiling->ComputeTilePriorityRects(
2109       PENDING_TREE, gfx::Rect(0, 0, 100, 100), 1.0f, 1.0f, Occlusion());
2110
2111   // Set the second tiling as recycled.
2112   active_client.set_twin_tiling(NULL);
2113   active_client.set_recycled_twin_tiling(recycle_tiling.get());
2114   recycle_client.set_twin_tiling(NULL);
2115
2116   // Verify that tiles exist and are shared.
2117   EXPECT_TRUE(active_tiling->TileAt(0, 0));
2118   EXPECT_TRUE(recycle_tiling->TileAt(0, 0));
2119   EXPECT_EQ(active_tiling->TileAt(0, 0), recycle_tiling->TileAt(0, 0));
2120
2121   // Move the viewport far away from the (0, 0) tile.
2122   active_tiling->ComputeTilePriorityRects(
2123       ACTIVE_TREE, gfx::Rect(9000, 9000, 100, 100), 1.0f, 2.0, Occlusion());
2124   // Ensure the tile was deleted on both tilings.
2125   EXPECT_FALSE(active_tiling->TileAt(0, 0));
2126   EXPECT_FALSE(recycle_tiling->TileAt(0, 0));
2127
2128   // Move the viewport back to (0, 0) tile.
2129   active_tiling->ComputeTilePriorityRects(
2130       ACTIVE_TREE, gfx::Rect(0, 0, 100, 100), 1.0f, 3.0, Occlusion());
2131
2132   // Ensure that we now have a tile here, but the recycle tiling does not.
2133   EXPECT_TRUE(active_tiling->TileAt(0, 0));
2134   EXPECT_FALSE(recycle_tiling->TileAt(0, 0));
2135 }
2136
2137 TEST(PictureLayerTilingTest, RecycledTilesClearedOnReset) {
2138   FakePictureLayerTilingClient active_client;
2139   scoped_ptr<TestablePictureLayerTiling> active_tiling;
2140
2141   active_client.SetTileSize(gfx::Size(100, 100));
2142   active_client.set_tree(ACTIVE_TREE);
2143   active_tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
2144                                                      gfx::Size(100, 100),
2145                                                      &active_client);
2146   // Create all tiles on this tiling.
2147   active_tiling->ComputeTilePriorityRects(
2148       ACTIVE_TREE, gfx::Rect(0, 0, 100, 100), 1.0f, 1.0f, Occlusion());
2149
2150   FakePictureLayerTilingClient recycle_client;
2151   recycle_client.SetTileSize(gfx::Size(100, 100));
2152   recycle_client.set_tree(PENDING_TREE);
2153   recycle_client.set_twin_tiling(active_tiling.get());
2154   recycle_client.set_max_tiles_for_interest_area(10);
2155
2156   scoped_ptr<TestablePictureLayerTiling> recycle_tiling;
2157   recycle_tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
2158                                                       gfx::Size(100, 100),
2159                                                       &recycle_client);
2160
2161   // Create all tiles on the recycle tiling. All tiles should be shared.
2162   recycle_tiling->ComputeTilePriorityRects(
2163       PENDING_TREE, gfx::Rect(0, 0, 100, 100), 1.0f, 1.0f, Occlusion());
2164
2165   // Set the second tiling as recycled.
2166   active_client.set_twin_tiling(NULL);
2167   active_client.set_recycled_twin_tiling(recycle_tiling.get());
2168   recycle_client.set_twin_tiling(NULL);
2169
2170   // Verify that tiles exist and are shared.
2171   EXPECT_TRUE(active_tiling->TileAt(0, 0));
2172   EXPECT_TRUE(recycle_tiling->TileAt(0, 0));
2173   EXPECT_EQ(active_tiling->TileAt(0, 0), recycle_tiling->TileAt(0, 0));
2174
2175   // Reset the active tiling. The recycle tiles should be released too.
2176   active_tiling->Reset();
2177   EXPECT_FALSE(active_tiling->TileAt(0, 0));
2178   EXPECT_FALSE(recycle_tiling->TileAt(0, 0));
2179 }
2180
2181 }  // namespace
2182 }  // namespace cc