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