837f64710d8a0c4a2975d676cad73bedcd7b6033
[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 "testing/gtest/include/gtest/gtest.h"
17 #include "ui/gfx/rect_conversions.h"
18 #include "ui/gfx/size_conversions.h"
19
20 namespace cc {
21 namespace {
22
23 static gfx::Rect ViewportInLayerSpace(
24     const gfx::Transform& transform,
25     const gfx::Size& device_viewport) {
26
27   gfx::Transform inverse;
28   if (!transform.GetInverse(&inverse))
29     return gfx::Rect();
30
31   gfx::RectF viewport_in_layer_space = MathUtil::ProjectClippedRect(
32       inverse, gfx::RectF(gfx::Point(0, 0), device_viewport));
33   return ToEnclosingRect(viewport_in_layer_space);
34 }
35
36 class TestablePictureLayerTiling : public PictureLayerTiling {
37  public:
38   using PictureLayerTiling::SetLiveTilesRect;
39   using PictureLayerTiling::TileAt;
40
41   static scoped_ptr<TestablePictureLayerTiling> Create(
42       float contents_scale,
43       const gfx::Size& layer_bounds,
44       PictureLayerTilingClient* client) {
45     return make_scoped_ptr(new TestablePictureLayerTiling(
46         contents_scale,
47         layer_bounds,
48         client));
49   }
50
51   using PictureLayerTiling::ComputeSkewport;
52
53  protected:
54   TestablePictureLayerTiling(float contents_scale,
55                              const gfx::Size& layer_bounds,
56                              PictureLayerTilingClient* client)
57       : PictureLayerTiling(contents_scale, layer_bounds, client) { }
58 };
59
60 class PictureLayerTilingIteratorTest : public testing::Test {
61  public:
62   PictureLayerTilingIteratorTest() {}
63   virtual ~PictureLayerTilingIteratorTest() {}
64
65   void Initialize(const gfx::Size& tile_size,
66                   float contents_scale,
67                   const gfx::Size& layer_bounds) {
68     client_.SetTileSize(tile_size);
69     tiling_ = TestablePictureLayerTiling::Create(contents_scale,
70                                                  layer_bounds,
71                                                  &client_);
72   }
73
74   void SetLiveRectAndVerifyTiles(const gfx::Rect& live_tiles_rect) {
75     tiling_->SetLiveTilesRect(live_tiles_rect);
76
77     std::vector<Tile*> tiles = tiling_->AllTilesForTesting();
78     for (std::vector<Tile*>::iterator iter = tiles.begin();
79          iter != tiles.end();
80          ++iter) {
81       EXPECT_TRUE(live_tiles_rect.Intersects((*iter)->content_rect()));
82     }
83   }
84
85   void VerifyTilesExactlyCoverRect(
86       float rect_scale,
87       const gfx::Rect& request_rect,
88       const gfx::Rect& expect_rect) {
89     EXPECT_TRUE(request_rect.Contains(expect_rect));
90
91     // Iterators are not valid if this ratio is too large (i.e. the
92     // tiling is too high-res for a low-res destination rect.)  This is an
93     // artifact of snapping geometry to integer coordinates and then mapping
94     // back to floating point texture coordinates.
95     float dest_to_contents_scale = tiling_->contents_scale() / rect_scale;
96     ASSERT_LE(dest_to_contents_scale, 2.0);
97
98     Region remaining = expect_rect;
99     for (PictureLayerTiling::CoverageIterator
100              iter(tiling_.get(), rect_scale, request_rect);
101          iter;
102          ++iter) {
103       // Geometry cannot overlap previous geometry at all
104       gfx::Rect geometry = iter.geometry_rect();
105       EXPECT_TRUE(expect_rect.Contains(geometry));
106       EXPECT_TRUE(remaining.Contains(geometry));
107       remaining.Subtract(geometry);
108
109       // Sanity check that texture coords are within the texture rect.
110       gfx::RectF texture_rect = iter.texture_rect();
111       EXPECT_GE(texture_rect.x(), 0);
112       EXPECT_GE(texture_rect.y(), 0);
113       EXPECT_LE(texture_rect.right(), client_.TileSize().width());
114       EXPECT_LE(texture_rect.bottom(), client_.TileSize().height());
115
116       EXPECT_EQ(iter.texture_size(), client_.TileSize());
117     }
118
119     // The entire rect must be filled by geometry from the tiling.
120     EXPECT_TRUE(remaining.IsEmpty());
121   }
122
123   void VerifyTilesExactlyCoverRect(float rect_scale, const gfx::Rect& rect) {
124     VerifyTilesExactlyCoverRect(rect_scale, rect, rect);
125   }
126
127   void VerifyTiles(
128       float rect_scale,
129       const gfx::Rect& rect,
130       base::Callback<void(Tile* tile,
131                           const gfx::Rect& geometry_rect)> callback) {
132     VerifyTiles(tiling_.get(),
133                 rect_scale,
134                 rect,
135                 callback);
136   }
137
138   void VerifyTiles(
139       PictureLayerTiling* tiling,
140       float rect_scale,
141       const gfx::Rect& rect,
142       base::Callback<void(Tile* tile,
143                           const gfx::Rect& geometry_rect)> callback) {
144     Region remaining = rect;
145     for (PictureLayerTiling::CoverageIterator iter(tiling, rect_scale, rect);
146          iter;
147          ++iter) {
148       remaining.Subtract(iter.geometry_rect());
149       callback.Run(*iter, iter.geometry_rect());
150     }
151     EXPECT_TRUE(remaining.IsEmpty());
152   }
153
154   void VerifyTilesCoverNonContainedRect(float rect_scale,
155                                         const gfx::Rect& dest_rect) {
156     float dest_to_contents_scale = tiling_->contents_scale() / rect_scale;
157     gfx::Rect clamped_rect = gfx::ScaleToEnclosingRect(
158         tiling_->ContentRect(), 1.f / dest_to_contents_scale);
159     clamped_rect.Intersect(dest_rect);
160     VerifyTilesExactlyCoverRect(rect_scale, dest_rect, clamped_rect);
161   }
162
163   void set_max_tiles_for_interest_area(size_t area) {
164     client_.set_max_tiles_for_interest_area(area);
165   }
166
167  protected:
168   FakePictureLayerTilingClient client_;
169   scoped_ptr<TestablePictureLayerTiling> tiling_;
170
171  private:
172   DISALLOW_COPY_AND_ASSIGN(PictureLayerTilingIteratorTest);
173 };
174
175 TEST_F(PictureLayerTilingIteratorTest, LiveTilesExactlyCoverLiveTileRect) {
176   Initialize(gfx::Size(100, 100), 1, gfx::Size(1099, 801));
177   SetLiveRectAndVerifyTiles(gfx::Rect(100, 100));
178   SetLiveRectAndVerifyTiles(gfx::Rect(101, 99));
179   SetLiveRectAndVerifyTiles(gfx::Rect(1099, 1));
180   SetLiveRectAndVerifyTiles(gfx::Rect(1, 801));
181   SetLiveRectAndVerifyTiles(gfx::Rect(1099, 1));
182   SetLiveRectAndVerifyTiles(gfx::Rect(201, 800));
183 }
184
185 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsNoScale) {
186   Initialize(gfx::Size(100, 100), 1, gfx::Size(1099, 801));
187   VerifyTilesExactlyCoverRect(1, gfx::Rect());
188   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1099, 801));
189   VerifyTilesExactlyCoverRect(1, gfx::Rect(52, 83, 789, 412));
190
191   // With borders, a size of 3x3 = 1 pixel of content.
192   Initialize(gfx::Size(3, 3), 1, gfx::Size(10, 10));
193   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
194   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
195   VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
196   VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
197 }
198
199 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsTilingScale) {
200   Initialize(gfx::Size(200, 100), 2.0f, gfx::Size(1005, 2010));
201   VerifyTilesExactlyCoverRect(1, gfx::Rect());
202   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
203   VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
204
205   Initialize(gfx::Size(3, 3), 2.0f, gfx::Size(10, 10));
206   VerifyTilesExactlyCoverRect(1, gfx::Rect());
207   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
208   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
209   VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
210   VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
211
212   Initialize(gfx::Size(100, 200), 0.5f, gfx::Size(1005, 2010));
213   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
214   VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
215
216   Initialize(gfx::Size(150, 250), 0.37f, gfx::Size(1005, 2010));
217   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
218   VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
219
220   Initialize(gfx::Size(312, 123), 0.01f, gfx::Size(1005, 2010));
221   VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
222   VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
223 }
224
225 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsBothScale) {
226   Initialize(gfx::Size(50, 50), 4.0f, gfx::Size(800, 600));
227   VerifyTilesExactlyCoverRect(2.0f, gfx::Rect());
228   VerifyTilesExactlyCoverRect(2.0f, gfx::Rect(0, 0, 1600, 1200));
229   VerifyTilesExactlyCoverRect(2.0f, gfx::Rect(512, 365, 253, 182));
230
231   float scale = 6.7f;
232   gfx::Size bounds(800, 600);
233   gfx::Rect full_rect(gfx::ToCeiledSize(gfx::ScaleSize(bounds, scale)));
234   Initialize(gfx::Size(256, 512), 5.2f, bounds);
235   VerifyTilesExactlyCoverRect(scale, full_rect);
236   VerifyTilesExactlyCoverRect(scale, gfx::Rect(2014, 1579, 867, 1033));
237 }
238
239 TEST_F(PictureLayerTilingIteratorTest, IteratorEmptyRect) {
240   Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(800, 600));
241
242   gfx::Rect empty;
243   PictureLayerTiling::CoverageIterator iter(tiling_.get(), 1.0f, empty);
244   EXPECT_FALSE(iter);
245 }
246
247 TEST_F(PictureLayerTilingIteratorTest, NonIntersectingRect) {
248   Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(800, 600));
249   gfx::Rect non_intersecting(1000, 1000, 50, 50);
250   PictureLayerTiling::CoverageIterator iter(tiling_.get(), 1, non_intersecting);
251   EXPECT_FALSE(iter);
252 }
253
254 TEST_F(PictureLayerTilingIteratorTest, LayerEdgeTextureCoordinates) {
255   Initialize(gfx::Size(300, 300), 1.0f, gfx::Size(256, 256));
256   // All of these sizes are 256x256, scaled and ceiled.
257   VerifyTilesExactlyCoverRect(1.0f, gfx::Rect(0, 0, 256, 256));
258   VerifyTilesExactlyCoverRect(0.8f, gfx::Rect(0, 0, 205, 205));
259   VerifyTilesExactlyCoverRect(1.2f, gfx::Rect(0, 0, 308, 308));
260 }
261
262 TEST_F(PictureLayerTilingIteratorTest, NonContainedDestRect) {
263   Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(400, 400));
264
265   // Too large in all dimensions
266   VerifyTilesCoverNonContainedRect(1.0f, gfx::Rect(-1000, -1000, 2000, 2000));
267   VerifyTilesCoverNonContainedRect(1.5f, gfx::Rect(-1000, -1000, 2000, 2000));
268   VerifyTilesCoverNonContainedRect(0.5f, gfx::Rect(-1000, -1000, 2000, 2000));
269
270   // Partially covering content, but too large
271   VerifyTilesCoverNonContainedRect(1.0f, gfx::Rect(-1000, 100, 2000, 100));
272   VerifyTilesCoverNonContainedRect(1.5f, gfx::Rect(-1000, 100, 2000, 100));
273   VerifyTilesCoverNonContainedRect(0.5f, gfx::Rect(-1000, 100, 2000, 100));
274 }
275
276 TEST(PictureLayerTilingTest, SkewportLimits) {
277   FakePictureLayerTilingClient client;
278   client.set_skewport_extrapolation_limit_in_content_pixels(75);
279   scoped_ptr<TestablePictureLayerTiling> tiling;
280
281   gfx::Rect viewport(0, 0, 100, 100);
282   gfx::Size layer_bounds(200, 200);
283
284   client.SetTileSize(gfx::Size(100, 100));
285   tiling = TestablePictureLayerTiling::Create(1.0f, layer_bounds, &client);
286
287   tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 1.f, 1.0);
288
289   // Move viewport down 50 pixels in 0.5 seconds.
290   gfx::Rect down_skewport =
291       tiling->ComputeSkewport(1.5, gfx::Rect(0, 50, 100, 100));
292
293   EXPECT_EQ(0, down_skewport.x());
294   EXPECT_EQ(50, down_skewport.y());
295   EXPECT_EQ(100, down_skewport.width());
296   EXPECT_EQ(175, down_skewport.height());
297   EXPECT_TRUE(down_skewport.Contains(gfx::Rect(0, 50, 100, 100)));
298
299   // Move viewport down 50 and right 10 pixels.
300   gfx::Rect down_right_skewport =
301       tiling->ComputeSkewport(1.5, gfx::Rect(10, 50, 100, 100));
302
303   EXPECT_EQ(10, down_right_skewport.x());
304   EXPECT_EQ(50, down_right_skewport.y());
305   EXPECT_EQ(120, down_right_skewport.width());
306   EXPECT_EQ(175, down_right_skewport.height());
307   EXPECT_TRUE(down_right_skewport.Contains(gfx::Rect(10, 50, 100, 100)));
308
309   // Move viewport left.
310   gfx::Rect left_skewport =
311       tiling->ComputeSkewport(1.5, gfx::Rect(-50, 0, 100, 100));
312
313   EXPECT_EQ(-125, left_skewport.x());
314   EXPECT_EQ(0, left_skewport.y());
315   EXPECT_EQ(175, left_skewport.width());
316   EXPECT_EQ(100, left_skewport.height());
317   EXPECT_TRUE(left_skewport.Contains(gfx::Rect(-50, 0, 100, 100)));
318
319   // Expand viewport.
320   gfx::Rect expand_skewport =
321       tiling->ComputeSkewport(1.5, gfx::Rect(-50, -50, 200, 200));
322
323   // x and y moved by -75 (-50 - 75 = -125).
324   // right side and bottom side moved by 75 [(350 - 125) - (200 - 50) = 75].
325   EXPECT_EQ(-125, expand_skewport.x());
326   EXPECT_EQ(-125, expand_skewport.y());
327   EXPECT_EQ(350, expand_skewport.width());
328   EXPECT_EQ(350, expand_skewport.height());
329   EXPECT_TRUE(expand_skewport.Contains(gfx::Rect(-50, -50, 200, 200)));
330
331   // Expand the viewport past the limit.
332   gfx::Rect big_expand_skewport =
333       tiling->ComputeSkewport(1.5, gfx::Rect(-500, -500, 1500, 1500));
334
335   EXPECT_EQ(-575, big_expand_skewport.x());
336   EXPECT_EQ(-575, big_expand_skewport.y());
337   EXPECT_EQ(1650, big_expand_skewport.width());
338   EXPECT_EQ(1650, big_expand_skewport.height());
339   EXPECT_TRUE(big_expand_skewport.Contains(gfx::Rect(-500, -500, 1500, 1500)));
340 }
341
342 TEST(PictureLayerTilingTest, ComputeSkewport) {
343   FakePictureLayerTilingClient client;
344   scoped_ptr<TestablePictureLayerTiling> tiling;
345
346   gfx::Rect viewport(0, 0, 100, 100);
347   gfx::Size layer_bounds(200, 200);
348
349   client.SetTileSize(gfx::Size(100, 100));
350   tiling = TestablePictureLayerTiling::Create(1.0f, layer_bounds, &client);
351
352   tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 1.f, 1.0);
353
354   // Move viewport down 50 pixels in 0.5 seconds.
355   gfx::Rect down_skewport =
356       tiling->ComputeSkewport(1.5, gfx::Rect(0, 50, 100, 100));
357
358   EXPECT_EQ(0, down_skewport.x());
359   EXPECT_EQ(50, down_skewport.y());
360   EXPECT_EQ(100, down_skewport.width());
361   EXPECT_EQ(200, down_skewport.height());
362
363   // Shrink viewport.
364   gfx::Rect shrink_skewport =
365       tiling->ComputeSkewport(1.5, gfx::Rect(25, 25, 50, 50));
366
367   EXPECT_EQ(25, shrink_skewport.x());
368   EXPECT_EQ(25, shrink_skewport.y());
369   EXPECT_EQ(50, shrink_skewport.width());
370   EXPECT_EQ(50, shrink_skewport.height());
371
372   // Move viewport down 50 and right 10 pixels.
373   gfx::Rect down_right_skewport =
374       tiling->ComputeSkewport(1.5, gfx::Rect(10, 50, 100, 100));
375
376   EXPECT_EQ(10, down_right_skewport.x());
377   EXPECT_EQ(50, down_right_skewport.y());
378   EXPECT_EQ(120, down_right_skewport.width());
379   EXPECT_EQ(200, down_right_skewport.height());
380
381   // Move viewport left.
382   gfx::Rect left_skewport =
383       tiling->ComputeSkewport(1.5, gfx::Rect(-20, 0, 100, 100));
384
385   EXPECT_EQ(-60, left_skewport.x());
386   EXPECT_EQ(0, left_skewport.y());
387   EXPECT_EQ(140, left_skewport.width());
388   EXPECT_EQ(100, left_skewport.height());
389
390   // Expand viewport in 0.2 seconds.
391   gfx::Rect expanded_skewport =
392       tiling->ComputeSkewport(1.2, gfx::Rect(-5, -5, 110, 110));
393
394   EXPECT_EQ(-30, expanded_skewport.x());
395   EXPECT_EQ(-30, expanded_skewport.y());
396   EXPECT_EQ(160, expanded_skewport.width());
397   EXPECT_EQ(160, expanded_skewport.height());
398 }
399
400 TEST(PictureLayerTilingTest, ViewportDistanceWithScale) {
401   FakePictureLayerTilingClient client;
402   scoped_ptr<TestablePictureLayerTiling> tiling;
403
404   gfx::Rect viewport(0, 0, 100, 100);
405   gfx::Size layer_bounds(200, 200);
406
407   client.SetTileSize(gfx::Size(10, 10));
408
409   // Tiling at 0.25 scale: this should create 36 tiles (6x6) of size 10x10.
410   // The reason is that each tile has a one pixel border, so tile at (1, 2)
411   // for instance begins at (8, 16) pixels. So tile at (5, 5) will begin at
412   // (40, 40) and extend right to the end of 200 * 0.25 = 50 edge of the
413   // tiling.
414   tiling = TestablePictureLayerTiling::Create(0.25f, layer_bounds, &client);
415   gfx::Rect viewport_in_content_space =
416       gfx::ToEnclosedRect(gfx::ScaleRect(viewport, 0.25f));
417
418   tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 1.f, 1.0);
419
420   // Sanity checks.
421   for (int i = 0; i < 6; ++i) {
422     for (int j = 0; j < 6; ++j) {
423       EXPECT_TRUE(tiling->TileAt(i, j)) << "i: " << i << " j: " << j;
424     }
425   }
426   for (int i = 0; i < 7; ++i) {
427     EXPECT_FALSE(tiling->TileAt(i, 6)) << "i: " << i;
428     EXPECT_FALSE(tiling->TileAt(6, i)) << "i: " << i;
429   }
430
431   // No movement in the viewport implies that tiles will either be NOW
432   // or EVENTUALLY.
433   bool have_now = false;
434   bool have_eventually = false;
435   for (int i = 0; i < 6; ++i) {
436     for (int j = 0; j < 6; ++j) {
437       Tile* tile = tiling->TileAt(i, j);
438       TilePriority priority = tile->priority(ACTIVE_TREE);
439
440       if (viewport_in_content_space.Intersects(tile->content_rect())) {
441         EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
442         EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
443         have_now = true;
444       } else {
445         EXPECT_EQ(TilePriority::EVENTUALLY, priority.priority_bin);
446         EXPECT_GT(priority.distance_to_visible, 0.f);
447         have_eventually = true;
448       }
449     }
450   }
451
452   EXPECT_TRUE(have_now);
453   EXPECT_TRUE(have_eventually);
454
455   // Spot check some distances.
456   // Tile at 5, 1 should begin at 41x9 in content space (without borders),
457   // so the distance to a viewport that ends at 25x25 in content space
458   // should be 17 (41 - 25 + 1). In layer space, then that should be
459   // 17 / 0.25 = 68 pixels.
460
461   // We can verify that the content rect (with borders) is one pixel off
462   // 41,9 8x8 on all sides.
463   EXPECT_EQ(tiling->TileAt(5, 1)->content_rect().ToString(), "40,8 10x10");
464
465   TilePriority priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
466   EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
467
468   priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
469   EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
470
471   priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
472   EXPECT_FLOAT_EQ(40.f, priority.distance_to_visible);
473
474   // Move the viewport down 40 pixels.
475   viewport = gfx::Rect(0, 40, 100, 100);
476   viewport_in_content_space =
477       gfx::ToEnclosedRect(gfx::ScaleRect(viewport, 0.25f));
478   gfx::Rect skewport = tiling->ComputeSkewport(2.0, viewport_in_content_space);
479
480   EXPECT_EQ(0, skewport.x());
481   EXPECT_EQ(10, skewport.y());
482   EXPECT_EQ(25, skewport.width());
483   EXPECT_EQ(35, skewport.height());
484
485   tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 1.f, 2.0);
486
487   have_now = false;
488   have_eventually = false;
489   bool have_soon = false;
490
491   // Viewport moved, so we expect to find some NOW tiles, some SOON tiles and
492   // some EVENTUALLY tiles.
493   for (int i = 0; i < 6; ++i) {
494     for (int j = 0; j < 6; ++j) {
495       Tile* tile = tiling->TileAt(i, j);
496       TilePriority priority = tile->priority(ACTIVE_TREE);
497
498       if (viewport_in_content_space.Intersects(tile->content_rect())) {
499         EXPECT_EQ(TilePriority::NOW, priority.priority_bin) << "i: " << i
500                                                             << " j: " << j;
501         EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible) << "i: " << i
502                                                            << " j: " << j;
503         have_now = true;
504       } else if (skewport.Intersects(tile->content_rect())) {
505         EXPECT_EQ(TilePriority::SOON, priority.priority_bin) << "i: " << i
506                                                              << " j: " << j;
507         EXPECT_GT(priority.distance_to_visible, 0.f) << "i: " << i
508                                                      << " j: " << j;
509         have_soon = true;
510       } else {
511         EXPECT_EQ(TilePriority::EVENTUALLY, priority.priority_bin)
512             << "i: " << i << " j: " << j;
513         EXPECT_GT(priority.distance_to_visible, 0.f) << "i: " << i
514                                                      << " j: " << j;
515         have_eventually = true;
516       }
517     }
518   }
519
520   EXPECT_TRUE(have_now);
521   EXPECT_TRUE(have_soon);
522   EXPECT_TRUE(have_eventually);
523
524   priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
525   EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
526
527   priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
528   EXPECT_FLOAT_EQ(28.f, priority.distance_to_visible);
529
530   priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
531   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
532
533   // Change the underlying layer scale.
534   tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 2.0f, 3.0);
535
536   priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
537   EXPECT_FLOAT_EQ(34.f, priority.distance_to_visible);
538
539   priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
540   EXPECT_FLOAT_EQ(14.f, priority.distance_to_visible);
541
542   priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
543   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
544 }
545
546 TEST(PictureLayerTilingTest, ExpandRectEqual) {
547   gfx::Rect in(40, 50, 100, 200);
548   gfx::Rect bounds(-1000, -1000, 10000, 10000);
549   int64 target_area = 100 * 200;
550   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
551       in, target_area, bounds, NULL);
552   EXPECT_EQ(in.ToString(), out.ToString());
553 }
554
555 TEST(PictureLayerTilingTest, ExpandRectSmaller) {
556   gfx::Rect in(40, 50, 100, 200);
557   gfx::Rect bounds(-1000, -1000, 10000, 10000);
558   int64 target_area = 100 * 100;
559   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
560       in, target_area, bounds, NULL);
561   EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
562   EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
563   EXPECT_EQ(out.width() - in.width(), out.height() - in.height());
564   EXPECT_NEAR(100 * 100, out.width() * out.height(), 50);
565   EXPECT_TRUE(bounds.Contains(out));
566 }
567
568 TEST(PictureLayerTilingTest, ExpandRectUnbounded) {
569   gfx::Rect in(40, 50, 100, 200);
570   gfx::Rect bounds(-1000, -1000, 10000, 10000);
571   int64 target_area = 200 * 200;
572   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
573       in, target_area, bounds, NULL);
574   EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
575   EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
576   EXPECT_EQ(out.width() - in.width(), out.height() - in.height());
577   EXPECT_NEAR(200 * 200, out.width() * out.height(), 100);
578   EXPECT_TRUE(bounds.Contains(out));
579 }
580
581 TEST(PictureLayerTilingTest, ExpandRectBoundedSmaller) {
582   gfx::Rect in(40, 50, 100, 200);
583   gfx::Rect bounds(50, 60, 40, 30);
584   int64 target_area = 200 * 200;
585   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
586       in, target_area, bounds, NULL);
587   EXPECT_EQ(bounds.ToString(), out.ToString());
588 }
589
590 TEST(PictureLayerTilingTest, ExpandRectBoundedEqual) {
591   gfx::Rect in(40, 50, 100, 200);
592   gfx::Rect bounds = in;
593   int64 target_area = 200 * 200;
594   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
595       in, target_area, bounds, NULL);
596   EXPECT_EQ(bounds.ToString(), out.ToString());
597 }
598
599 TEST(PictureLayerTilingTest, ExpandRectBoundedSmallerStretchVertical) {
600   gfx::Rect in(40, 50, 100, 200);
601   gfx::Rect bounds(45, 0, 90, 300);
602   int64 target_area = 200 * 200;
603   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
604       in, target_area, bounds, NULL);
605   EXPECT_EQ(bounds.ToString(), out.ToString());
606 }
607
608 TEST(PictureLayerTilingTest, ExpandRectBoundedEqualStretchVertical) {
609   gfx::Rect in(40, 50, 100, 200);
610   gfx::Rect bounds(40, 0, 100, 300);
611   int64 target_area = 200 * 200;
612   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
613       in, target_area, bounds, NULL);
614   EXPECT_EQ(bounds.ToString(), out.ToString());
615 }
616
617 TEST(PictureLayerTilingTest, ExpandRectBoundedSmallerStretchHorizontal) {
618   gfx::Rect in(40, 50, 100, 200);
619   gfx::Rect bounds(0, 55, 180, 190);
620   int64 target_area = 200 * 200;
621   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
622       in, target_area, bounds, NULL);
623   EXPECT_EQ(bounds.ToString(), out.ToString());
624 }
625
626 TEST(PictureLayerTilingTest, ExpandRectBoundedEqualStretchHorizontal) {
627   gfx::Rect in(40, 50, 100, 200);
628   gfx::Rect bounds(0, 50, 180, 200);
629   int64 target_area = 200 * 200;
630   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
631       in, target_area, bounds, NULL);
632   EXPECT_EQ(bounds.ToString(), out.ToString());
633 }
634
635 TEST(PictureLayerTilingTest, ExpandRectBoundedLeft) {
636   gfx::Rect in(40, 50, 100, 200);
637   gfx::Rect bounds(20, -1000, 10000, 10000);
638   int64 target_area = 200 * 200;
639   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
640       in, target_area, bounds, NULL);
641   EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
642   EXPECT_EQ(out.bottom() - in.bottom(), out.right() - in.right());
643   EXPECT_LE(out.width() * out.height(), target_area);
644   EXPECT_GT(out.width() * out.height(),
645             target_area - out.width() - out.height() * 2);
646   EXPECT_TRUE(bounds.Contains(out));
647 }
648
649 TEST(PictureLayerTilingTest, ExpandRectBoundedRight) {
650   gfx::Rect in(40, 50, 100, 200);
651   gfx::Rect bounds(-1000, -1000, 1000+120, 10000);
652   int64 target_area = 200 * 200;
653   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
654       in, target_area, bounds, NULL);
655   EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
656   EXPECT_EQ(out.bottom() - in.bottom(), in.x() - out.x());
657   EXPECT_LE(out.width() * out.height(), target_area);
658   EXPECT_GT(out.width() * out.height(),
659             target_area - out.width() - out.height() * 2);
660   EXPECT_TRUE(bounds.Contains(out));
661 }
662
663 TEST(PictureLayerTilingTest, ExpandRectBoundedTop) {
664   gfx::Rect in(40, 50, 100, 200);
665   gfx::Rect bounds(-1000, 30, 10000, 10000);
666   int64 target_area = 200 * 200;
667   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
668       in, target_area, bounds, NULL);
669   EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
670   EXPECT_EQ(out.right() - in.right(), out.bottom() - in.bottom());
671   EXPECT_LE(out.width() * out.height(), target_area);
672   EXPECT_GT(out.width() * out.height(),
673             target_area - out.width() * 2 - out.height());
674   EXPECT_TRUE(bounds.Contains(out));
675 }
676
677 TEST(PictureLayerTilingTest, ExpandRectBoundedBottom) {
678   gfx::Rect in(40, 50, 100, 200);
679   gfx::Rect bounds(-1000, -1000, 10000, 1000 + 220);
680   int64 target_area = 200 * 200;
681   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
682       in, target_area, bounds, NULL);
683   EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
684   EXPECT_EQ(out.right() - in.right(), in.y() - out.y());
685   EXPECT_LE(out.width() * out.height(), target_area);
686   EXPECT_GT(out.width() * out.height(),
687             target_area - out.width() * 2 - out.height());
688   EXPECT_TRUE(bounds.Contains(out));
689 }
690
691 TEST(PictureLayerTilingTest, ExpandRectSquishedHorizontally) {
692   gfx::Rect in(40, 50, 100, 200);
693   gfx::Rect bounds(0, -4000, 100+40+20, 100000);
694   int64 target_area = 400 * 400;
695   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
696       in, target_area, bounds, NULL);
697   EXPECT_EQ(20, out.right() - in.right());
698   EXPECT_EQ(40, in.x() - out.x());
699   EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
700   EXPECT_LE(out.width() * out.height(), target_area);
701   EXPECT_GT(out.width() * out.height(),
702             target_area - out.width() * 2);
703   EXPECT_TRUE(bounds.Contains(out));
704 }
705
706 TEST(PictureLayerTilingTest, ExpandRectSquishedVertically) {
707   gfx::Rect in(40, 50, 100, 200);
708   gfx::Rect bounds(-4000, 0, 100000, 200+50+30);
709   int64 target_area = 400 * 400;
710   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
711       in, target_area, bounds, NULL);
712   EXPECT_EQ(30, out.bottom() - in.bottom());
713   EXPECT_EQ(50, in.y() - out.y());
714   EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
715   EXPECT_LE(out.width() * out.height(), target_area);
716   EXPECT_GT(out.width() * out.height(),
717             target_area - out.height() * 2);
718   EXPECT_TRUE(bounds.Contains(out));
719 }
720
721 TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsFarAway) {
722   gfx::Rect in(400, 500, 100, 200);
723   gfx::Rect bounds(0, 0, 10, 10);
724   int64 target_area = 400 * 400;
725   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
726       in, target_area, bounds, NULL);
727   EXPECT_TRUE(out.IsEmpty());
728 }
729
730 TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsExpandedFullyCover) {
731   gfx::Rect in(40, 50, 100, 100);
732   gfx::Rect bounds(0, 0, 10, 10);
733   int64 target_area = 400 * 400;
734   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
735       in, target_area, bounds, NULL);
736   EXPECT_EQ(bounds.ToString(), out.ToString());
737 }
738
739 TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsExpandedPartlyCover) {
740   gfx::Rect in(600, 600, 100, 100);
741   gfx::Rect bounds(0, 0, 500, 500);
742   int64 target_area = 400 * 400;
743   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
744       in, target_area, bounds, NULL);
745   EXPECT_EQ(bounds.right(), out.right());
746   EXPECT_EQ(bounds.bottom(), out.bottom());
747   EXPECT_LE(out.width() * out.height(), target_area);
748   EXPECT_GT(out.width() * out.height(),
749             target_area - out.width() - out.height());
750   EXPECT_TRUE(bounds.Contains(out));
751 }
752
753 TEST(PictureLayerTilingTest, EmptyStartingRect) {
754   // If a layer has a non-invertible transform, then the starting rect
755   // for the layer would be empty.
756   gfx::Rect in(40, 40, 0, 0);
757   gfx::Rect bounds(0, 0, 10, 10);
758   int64 target_area = 400 * 400;
759   gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
760       in, target_area, bounds, NULL);
761   EXPECT_TRUE(out.IsEmpty());
762 }
763
764 TEST(PictureLayerTilingTest, TilingRasterTileIteratorStaticViewport) {
765   FakePictureLayerTilingClient client;
766   scoped_ptr<TestablePictureLayerTiling> tiling;
767
768   gfx::Rect viewport(50, 50, 100, 100);
769   gfx::Size layer_bounds(200, 200);
770
771   client.SetTileSize(gfx::Size(30, 30));
772
773   tiling = TestablePictureLayerTiling::Create(1.0f, layer_bounds, &client);
774   tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 1.0f, 1.0);
775
776   PictureLayerTiling::TilingRasterTileIterator empty_iterator;
777   EXPECT_FALSE(empty_iterator);
778
779   std::vector<Tile*> all_tiles = tiling->AllTilesForTesting();
780
781   // Sanity check.
782   EXPECT_EQ(64u, all_tiles.size());
783
784   // The explanation of each iteration is as follows:
785   // 1. First iteration tests that we can get all of the tiles correctly.
786   // 2. Second iteration ensures that we can get all of the tiles again (first
787   //    iteration didn't change any tiles), as well set all tiles to be ready to
788   //    draw.
789   // 3. Third iteration ensures that no tiles are returned, since they were all
790   //    marked as ready to draw.
791   for (int i = 0; i < 3; ++i) {
792     PictureLayerTiling::TilingRasterTileIterator it(tiling.get(), ACTIVE_TREE);
793
794     // There are 3 bins in TilePriority.
795     bool have_tiles[3] = {};
796
797     // On the third iteration, we should get no tiles since everything was
798     // marked as ready to draw.
799     if (i == 2) {
800       EXPECT_FALSE(it);
801       continue;
802     }
803
804     EXPECT_TRUE(it);
805     std::set<Tile*> unique_tiles;
806     unique_tiles.insert(*it);
807     Tile* last_tile = *it;
808     have_tiles[last_tile->priority(ACTIVE_TREE).priority_bin] = true;
809
810     // On the second iteration, mark everything as ready to draw (solid color).
811     if (i == 1) {
812       ManagedTileState::TileVersion& tile_version =
813           last_tile->GetTileVersionForTesting(
814               last_tile->DetermineRasterModeForTree(ACTIVE_TREE));
815       tile_version.SetSolidColorForTesting(SK_ColorRED);
816     }
817     ++it;
818     int eventually_bin_order_correct_count = 0;
819     int eventually_bin_order_incorrect_count = 0;
820     while (it) {
821       Tile* new_tile = *it;
822       ++it;
823       unique_tiles.insert(new_tile);
824
825       TilePriority last_priority = last_tile->priority(ACTIVE_TREE);
826       TilePriority new_priority = new_tile->priority(ACTIVE_TREE);
827       EXPECT_LE(last_priority.priority_bin, new_priority.priority_bin);
828       if (last_priority.priority_bin == new_priority.priority_bin) {
829         if (last_priority.priority_bin == TilePriority::EVENTUALLY) {
830           bool order_correct = last_priority.distance_to_visible <=
831                                new_priority.distance_to_visible;
832           eventually_bin_order_correct_count += order_correct;
833           eventually_bin_order_incorrect_count += !order_correct;
834         } else {
835           EXPECT_LE(last_priority.distance_to_visible,
836                     new_priority.distance_to_visible);
837         }
838       }
839       have_tiles[new_priority.priority_bin] = true;
840
841       last_tile = new_tile;
842
843       // On the second iteration, mark everything as ready to draw (solid
844       // color).
845       if (i == 1) {
846         ManagedTileState::TileVersion& tile_version =
847             last_tile->GetTileVersionForTesting(
848                 last_tile->DetermineRasterModeForTree(ACTIVE_TREE));
849         tile_version.SetSolidColorForTesting(SK_ColorRED);
850       }
851     }
852
853     EXPECT_GT(eventually_bin_order_correct_count,
854               eventually_bin_order_incorrect_count);
855
856     // We should have now and eventually tiles, but not soon tiles because the
857     // viewport is static.
858     EXPECT_TRUE(have_tiles[TilePriority::NOW]);
859     EXPECT_FALSE(have_tiles[TilePriority::SOON]);
860     EXPECT_TRUE(have_tiles[TilePriority::EVENTUALLY]);
861
862     EXPECT_EQ(unique_tiles.size(), all_tiles.size());
863   }
864 }
865
866 TEST(PictureLayerTilingTest, TilingRasterTileIteratorMovingViewport) {
867   FakePictureLayerTilingClient client;
868   scoped_ptr<TestablePictureLayerTiling> tiling;
869
870   gfx::Rect viewport(50, 0, 100, 100);
871   gfx::Rect moved_viewport(50, 0, 100, 250);
872   gfx::Size layer_bounds(500, 500);
873
874   client.SetTileSize(gfx::Size(30, 30));
875
876   tiling = TestablePictureLayerTiling::Create(1.f, layer_bounds, &client);
877   tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 1.0f, 1.0);
878   tiling->UpdateTilePriorities(ACTIVE_TREE, moved_viewport, 1.0f, 2.0);
879
880   // There are 3 bins in TilePriority.
881   bool have_tiles[3] = {};
882   Tile* last_tile = NULL;
883   int eventually_bin_order_correct_count = 0;
884   int eventually_bin_order_incorrect_count = 0;
885   for (PictureLayerTiling::TilingRasterTileIterator it(tiling.get(),
886                                                        ACTIVE_TREE);
887        it;
888        ++it) {
889     if (!last_tile)
890       last_tile = *it;
891
892     Tile* new_tile = *it;
893
894     TilePriority last_priority = last_tile->priority(ACTIVE_TREE);
895     TilePriority new_priority = new_tile->priority(ACTIVE_TREE);
896
897     have_tiles[new_priority.priority_bin] = true;
898
899     EXPECT_LE(last_priority.priority_bin, new_priority.priority_bin);
900     if (last_priority.priority_bin == new_priority.priority_bin) {
901       if (last_priority.priority_bin == TilePriority::EVENTUALLY) {
902         bool order_correct = last_priority.distance_to_visible <=
903                              new_priority.distance_to_visible;
904         eventually_bin_order_correct_count += order_correct;
905         eventually_bin_order_incorrect_count += !order_correct;
906       } else {
907         EXPECT_LE(last_priority.distance_to_visible,
908                   new_priority.distance_to_visible);
909       }
910     }
911     last_tile = new_tile;
912   }
913
914   EXPECT_GT(eventually_bin_order_correct_count,
915             eventually_bin_order_incorrect_count);
916
917   EXPECT_TRUE(have_tiles[TilePriority::NOW]);
918   EXPECT_TRUE(have_tiles[TilePriority::SOON]);
919   EXPECT_TRUE(have_tiles[TilePriority::EVENTUALLY]);
920 }
921
922 static void TileExists(bool exists, Tile* tile,
923                        const gfx::Rect& geometry_rect) {
924   EXPECT_EQ(exists, tile != NULL) << geometry_rect.ToString();
925 }
926
927 TEST_F(PictureLayerTilingIteratorTest, TilesExist) {
928   gfx::Size layer_bounds(1099, 801);
929   Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
930   VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
931   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
932
933   tiling_->UpdateTilePriorities(
934       ACTIVE_TREE,
935       gfx::Rect(layer_bounds),  // visible content rect
936       1.f,                      // current contents scale
937       1.0);                     // current frame time
938   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
939
940   // Make the viewport rect empty. All tiles are killed and become zombies.
941   tiling_->UpdateTilePriorities(ACTIVE_TREE,
942                                 gfx::Rect(),  // visible content rect
943                                 1.f,          // current contents scale
944                                 2.0);         // current frame time
945   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
946 }
947
948 TEST_F(PictureLayerTilingIteratorTest, TilesExistGiantViewport) {
949   gfx::Size layer_bounds(1099, 801);
950   Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
951   VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
952   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
953
954   gfx::Rect giant_rect(-10000000, -10000000, 1000000000, 1000000000);
955
956   tiling_->UpdateTilePriorities(
957       ACTIVE_TREE,
958       gfx::Rect(layer_bounds),  // visible content rect
959       1.f,                      // current contents scale
960       1.0);                     // current frame time
961   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
962
963   // If the visible content rect is empty, it should still have live tiles.
964   tiling_->UpdateTilePriorities(ACTIVE_TREE,
965                                 giant_rect,  // visible content rect
966                                 1.f,         // current contents scale
967                                 2.0);        // current frame time
968   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
969 }
970
971 TEST_F(PictureLayerTilingIteratorTest, TilesExistOutsideViewport) {
972   gfx::Size layer_bounds(1099, 801);
973   Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
974   VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
975   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
976
977   // This rect does not intersect with the layer, as the layer is outside the
978   // viewport.
979   gfx::Rect viewport_rect(1100, 0, 1000, 1000);
980   EXPECT_FALSE(viewport_rect.Intersects(gfx::Rect(layer_bounds)));
981
982   tiling_->UpdateTilePriorities(ACTIVE_TREE,
983                                 viewport_rect,  // visible content rect
984                                 1.f,            // current contents scale
985                                 1.0);           // current frame time
986   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
987 }
988
989 static void TilesIntersectingRectExist(const gfx::Rect& rect,
990                                        bool intersect_exists,
991                                        Tile* tile,
992                                        const gfx::Rect& geometry_rect) {
993   bool intersects = rect.Intersects(geometry_rect);
994   bool expected_exists = intersect_exists ? intersects : !intersects;
995   EXPECT_EQ(expected_exists, tile != NULL)
996       << "Rects intersecting " << rect.ToString() << " should exist. "
997       << "Current tile rect is " << geometry_rect.ToString();
998 }
999
1000 TEST_F(PictureLayerTilingIteratorTest,
1001        TilesExistLargeViewportAndLayerWithSmallVisibleArea) {
1002   gfx::Size layer_bounds(10000, 10000);
1003   Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
1004   VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
1005   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
1006
1007   gfx::Rect visible_rect(8000, 8000, 50, 50);
1008
1009   set_max_tiles_for_interest_area(1);
1010   tiling_->UpdateTilePriorities(ACTIVE_TREE,
1011                                 visible_rect,  // visible content rect
1012                                 1.f,           // current contents scale
1013                                 1.0);          // current frame time
1014   VerifyTiles(1.f,
1015               gfx::Rect(layer_bounds),
1016               base::Bind(&TilesIntersectingRectExist, visible_rect, true));
1017 }
1018
1019 static void CountExistingTiles(int *count,
1020                                Tile* tile,
1021                                const gfx::Rect& geometry_rect) {
1022   if (tile != NULL)
1023     ++(*count);
1024 }
1025
1026 TEST_F(PictureLayerTilingIteratorTest,
1027        TilesExistLargeViewportAndLayerWithLargeVisibleArea) {
1028   gfx::Size layer_bounds(10000, 10000);
1029   Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
1030   VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
1031   VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
1032
1033   set_max_tiles_for_interest_area(1);
1034   tiling_->UpdateTilePriorities(
1035       ACTIVE_TREE,
1036       gfx::Rect(layer_bounds),  // visible content rect
1037       1.f,                      // current contents scale
1038       1.0);                     // current frame time
1039
1040   int num_tiles = 0;
1041   VerifyTiles(1.f,
1042               gfx::Rect(layer_bounds),
1043               base::Bind(&CountExistingTiles, &num_tiles));
1044   // If we're making a rect the size of one tile, it can only overlap up to 4
1045   // tiles depending on its position.
1046   EXPECT_LE(num_tiles, 4);
1047   VerifyTiles(1.f, gfx::Rect(), base::Bind(&TileExists, false));
1048 }
1049
1050 TEST_F(PictureLayerTilingIteratorTest, AddTilingsToMatchScale) {
1051   gfx::Size layer_bounds(1099, 801);
1052   gfx::Size tile_size(100, 100);
1053
1054   client_.SetTileSize(tile_size);
1055
1056   PictureLayerTilingSet active_set(&client_, layer_bounds);
1057
1058   active_set.AddTiling(1.f);
1059
1060   VerifyTiles(active_set.tiling_at(0),
1061               1.f,
1062               gfx::Rect(layer_bounds),
1063               base::Bind(&TileExists, false));
1064
1065   active_set.UpdateTilePriorities(
1066       PENDING_TREE,
1067       gfx::Rect(layer_bounds),  // visible content rect
1068       1.f,                      // current contents scale
1069       1.0);                     // current frame time
1070
1071   // The active tiling has tiles now.
1072   VerifyTiles(active_set.tiling_at(0),
1073               1.f,
1074               gfx::Rect(layer_bounds),
1075               base::Bind(&TileExists, true));
1076
1077   // Add the same tilings to the pending set.
1078   PictureLayerTilingSet pending_set(&client_, layer_bounds);
1079   Region invalidation;
1080   pending_set.SyncTilings(active_set, layer_bounds, invalidation, 0.f);
1081
1082   // The pending tiling starts with no tiles.
1083   VerifyTiles(pending_set.tiling_at(0),
1084               1.f,
1085               gfx::Rect(layer_bounds),
1086               base::Bind(&TileExists, false));
1087
1088   // UpdateTilePriorities on the pending tiling at the same frame time. The
1089   // pending tiling should get tiles.
1090   pending_set.UpdateTilePriorities(
1091       PENDING_TREE,
1092       gfx::Rect(layer_bounds),  // visible content rect
1093       1.f,                      // current contents scale
1094       1.0);                     // current frame time
1095
1096   VerifyTiles(pending_set.tiling_at(0),
1097               1.f,
1098               gfx::Rect(layer_bounds),
1099               base::Bind(&TileExists, true));
1100 }
1101
1102 TEST(UpdateTilePrioritiesTest, VisibleTiles) {
1103   // The TilePriority of visible tiles should have zero distance_to_visible
1104   // and time_to_visible.
1105
1106   FakePictureLayerTilingClient client;
1107   scoped_ptr<TestablePictureLayerTiling> tiling;
1108
1109   gfx::Size device_viewport(800, 600);
1110   gfx::Size last_layer_bounds(200, 200);
1111   gfx::Size current_layer_bounds(200, 200);
1112   float current_layer_contents_scale = 1.f;
1113   gfx::Transform current_screen_transform;
1114   double current_frame_time_in_seconds = 1.0;
1115
1116   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1117       current_screen_transform, device_viewport);
1118
1119   client.SetTileSize(gfx::Size(100, 100));
1120   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1121                                               current_layer_bounds,
1122                                               &client);
1123
1124   tiling->UpdateTilePriorities(ACTIVE_TREE,
1125                                viewport_in_layer_space,
1126                                current_layer_contents_scale,
1127                                current_frame_time_in_seconds);
1128
1129   ASSERT_TRUE(tiling->TileAt(0, 0));
1130   ASSERT_TRUE(tiling->TileAt(0, 1));
1131   ASSERT_TRUE(tiling->TileAt(1, 0));
1132   ASSERT_TRUE(tiling->TileAt(1, 1));
1133
1134   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1135   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1136   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1137
1138   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1139   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1140   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1141
1142   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1143   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1144   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1145
1146   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1147   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1148   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1149 }
1150
1151 TEST(UpdateTilePrioritiesTest, OffscreenTiles) {
1152   // The TilePriority of offscreen tiles (without movement) should have nonzero
1153   // distance_to_visible and infinite time_to_visible.
1154
1155   FakePictureLayerTilingClient client;
1156   scoped_ptr<TestablePictureLayerTiling> tiling;
1157
1158   gfx::Size device_viewport(800, 600);
1159   gfx::Size last_layer_bounds(200, 200);
1160   gfx::Size current_layer_bounds(200, 200);
1161   float current_layer_contents_scale = 1.f;
1162   gfx::Transform last_screen_transform;
1163   gfx::Transform current_screen_transform;
1164   double current_frame_time_in_seconds = 1.0;
1165
1166   current_screen_transform.Translate(850, 0);
1167   last_screen_transform = current_screen_transform;
1168
1169   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1170       current_screen_transform, device_viewport);
1171
1172   client.SetTileSize(gfx::Size(100, 100));
1173   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1174                                               current_layer_bounds,
1175                                               &client);
1176
1177   tiling->UpdateTilePriorities(ACTIVE_TREE,
1178                                viewport_in_layer_space,
1179                                current_layer_contents_scale,
1180                                current_frame_time_in_seconds);
1181
1182   ASSERT_TRUE(tiling->TileAt(0, 0));
1183   ASSERT_TRUE(tiling->TileAt(0, 1));
1184   ASSERT_TRUE(tiling->TileAt(1, 0));
1185   ASSERT_TRUE(tiling->TileAt(1, 1));
1186
1187   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1188   EXPECT_GT(priority.distance_to_visible, 0.f);
1189   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1190
1191   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1192   EXPECT_GT(priority.distance_to_visible, 0.f);
1193   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1194
1195   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1196   EXPECT_GT(priority.distance_to_visible, 0.f);
1197   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1198
1199   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1200   EXPECT_GT(priority.distance_to_visible, 0.f);
1201   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1202
1203   // Furthermore, in this scenario tiles on the right hand side should have a
1204   // larger distance to visible.
1205   TilePriority left = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1206   TilePriority right = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1207   EXPECT_GT(right.distance_to_visible, left.distance_to_visible);
1208
1209   left = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1210   right = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1211   EXPECT_GT(right.distance_to_visible, left.distance_to_visible);
1212 }
1213
1214 TEST(UpdateTilePrioritiesTest, PartiallyOffscreenLayer) {
1215   // Sanity check that a layer with some tiles visible and others offscreen has
1216   // correct TilePriorities for each tile.
1217
1218   FakePictureLayerTilingClient client;
1219   scoped_ptr<TestablePictureLayerTiling> tiling;
1220
1221   gfx::Size device_viewport(800, 600);
1222   gfx::Size last_layer_bounds(200, 200);
1223   gfx::Size current_layer_bounds(200, 200);
1224   float current_layer_contents_scale = 1.f;
1225   gfx::Transform last_screen_transform;
1226   gfx::Transform current_screen_transform;
1227   double current_frame_time_in_seconds = 1.0;
1228
1229   current_screen_transform.Translate(705, 505);
1230   last_screen_transform = current_screen_transform;
1231
1232   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1233       current_screen_transform, device_viewport);
1234
1235   client.SetTileSize(gfx::Size(100, 100));
1236   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1237                                               current_layer_bounds,
1238                                               &client);
1239
1240   tiling->UpdateTilePriorities(ACTIVE_TREE,
1241                                viewport_in_layer_space,
1242                                current_layer_contents_scale,
1243                                current_frame_time_in_seconds);
1244
1245   ASSERT_TRUE(tiling->TileAt(0, 0));
1246   ASSERT_TRUE(tiling->TileAt(0, 1));
1247   ASSERT_TRUE(tiling->TileAt(1, 0));
1248   ASSERT_TRUE(tiling->TileAt(1, 1));
1249
1250   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1251   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1252   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1253
1254   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1255   EXPECT_GT(priority.distance_to_visible, 0.f);
1256   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1257
1258   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1259   EXPECT_GT(priority.distance_to_visible, 0.f);
1260   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1261
1262   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1263   EXPECT_GT(priority.distance_to_visible, 0.f);
1264   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1265 }
1266
1267 TEST(UpdateTilePrioritiesTest, PartiallyOffscreenRotatedLayer) {
1268   // Each tile of a layer may be affected differently by a transform; Check
1269   // that UpdateTilePriorities correctly accounts for the transform between
1270   // layer space and screen space.
1271
1272   FakePictureLayerTilingClient client;
1273   scoped_ptr<TestablePictureLayerTiling> tiling;
1274
1275   gfx::Size device_viewport(800, 600);
1276   gfx::Size last_layer_bounds(200, 200);
1277   gfx::Size current_layer_bounds(200, 200);
1278   float current_layer_contents_scale = 1.f;
1279   gfx::Transform last_screen_transform;
1280   gfx::Transform current_screen_transform;
1281   double current_frame_time_in_seconds = 1.0;
1282
1283   // A diagonally rotated layer that is partially off the bottom of the screen.
1284   // In this configuration, only the top-left tile would be visible.
1285   current_screen_transform.Translate(600, 750);
1286   current_screen_transform.RotateAboutZAxis(45);
1287   last_screen_transform = current_screen_transform;
1288
1289   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1290       current_screen_transform, device_viewport);
1291
1292   client.SetTileSize(gfx::Size(100, 100));
1293   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1294                                               current_layer_bounds,
1295                                               &client);
1296
1297   tiling->UpdateTilePriorities(ACTIVE_TREE,
1298                                viewport_in_layer_space,
1299                                current_layer_contents_scale,
1300                                current_frame_time_in_seconds);
1301
1302   ASSERT_TRUE(tiling->TileAt(0, 0));
1303   ASSERT_TRUE(tiling->TileAt(0, 1));
1304   ASSERT_TRUE(tiling->TileAt(1, 0));
1305   ASSERT_TRUE(tiling->TileAt(1, 1));
1306
1307   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1308   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1309   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1310
1311   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1312   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1313   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1314
1315   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1316   EXPECT_GT(priority.distance_to_visible, 0.f);
1317   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1318
1319   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1320   EXPECT_GT(priority.distance_to_visible, 0.f);
1321   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1322
1323   // Furthermore, in this scenario the bottom-right tile should have the larger
1324   // distance to visible.
1325   TilePriority top_left = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1326   TilePriority top_right = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1327   TilePriority bottom_right = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1328   EXPECT_GT(top_right.distance_to_visible, top_left.distance_to_visible);
1329
1330   EXPECT_EQ(bottom_right.distance_to_visible, top_right.distance_to_visible);
1331 }
1332
1333 TEST(UpdateTilePrioritiesTest, PerspectiveLayer) {
1334   // Perspective transforms need to take a different code path.
1335   // This test checks tile priorities of a perspective layer.
1336
1337   FakePictureLayerTilingClient client;
1338   scoped_ptr<TestablePictureLayerTiling> tiling;
1339
1340   gfx::Size device_viewport(800, 600);
1341   gfx::Rect visible_layer_rect(0, 0, 0, 0);  // offscreen.
1342   gfx::Size last_layer_bounds(200, 200);
1343   gfx::Size current_layer_bounds(200, 200);
1344   float current_layer_contents_scale = 1.f;
1345   gfx::Transform last_screen_transform;
1346   gfx::Transform current_screen_transform;
1347   double current_frame_time_in_seconds = 1.0;
1348
1349   // A 3d perspective layer rotated about its Y axis, translated to almost
1350   // fully offscreen. The left side will appear closer (i.e. larger in 2d) than
1351   // the right side, so the top-left tile will technically be closer than the
1352   // top-right.
1353
1354   // Translate layer to offscreen
1355   current_screen_transform.Translate(400.0, 630.0);
1356   // Apply perspective about the center of the layer
1357   current_screen_transform.Translate(100.0, 100.0);
1358   current_screen_transform.ApplyPerspectiveDepth(100.0);
1359   current_screen_transform.RotateAboutYAxis(10.0);
1360   current_screen_transform.Translate(-100.0, -100.0);
1361   last_screen_transform = current_screen_transform;
1362
1363   // Sanity check that this transform wouldn't cause w<0 clipping.
1364   bool clipped;
1365   MathUtil::MapQuad(current_screen_transform,
1366                     gfx::QuadF(gfx::RectF(0, 0, 200, 200)),
1367                     &clipped);
1368   ASSERT_FALSE(clipped);
1369
1370   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1371       current_screen_transform, device_viewport);
1372
1373   client.SetTileSize(gfx::Size(100, 100));
1374   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1375                                               current_layer_bounds,
1376                                               &client);
1377
1378   tiling->UpdateTilePriorities(ACTIVE_TREE,
1379                                viewport_in_layer_space,
1380                                current_layer_contents_scale,
1381                                current_frame_time_in_seconds);
1382
1383   ASSERT_TRUE(tiling->TileAt(0, 0));
1384   ASSERT_TRUE(tiling->TileAt(0, 1));
1385   ASSERT_TRUE(tiling->TileAt(1, 0));
1386   ASSERT_TRUE(tiling->TileAt(1, 1));
1387
1388   // All tiles will have a positive distance_to_visible
1389   // and an infinite time_to_visible.
1390   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1391   EXPECT_FLOAT_EQ(priority.distance_to_visible, 0.f);
1392   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1393
1394   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1395   EXPECT_GT(priority.distance_to_visible, 0.f);
1396   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1397
1398   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1399   EXPECT_FLOAT_EQ(priority.distance_to_visible, 0.f);
1400   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1401
1402   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1403   EXPECT_GT(priority.distance_to_visible, 0.f);
1404   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1405
1406   // Furthermore, in this scenario the top-left distance_to_visible
1407   // will be smallest, followed by top-right. The bottom layers
1408   // will of course be further than the top layers.
1409   TilePriority top_left = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1410   TilePriority top_right = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1411   TilePriority bottom_left = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1412   TilePriority bottom_right = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1413
1414   EXPECT_GT(bottom_right.distance_to_visible, top_right.distance_to_visible);
1415
1416   EXPECT_GT(bottom_left.distance_to_visible, top_left.distance_to_visible);
1417 }
1418
1419 TEST(UpdateTilePrioritiesTest, PerspectiveLayerClippedByW) {
1420   // Perspective transforms need to take a different code path.
1421   // This test checks tile priorities of a perspective layer.
1422
1423   FakePictureLayerTilingClient client;
1424   scoped_ptr<TestablePictureLayerTiling> tiling;
1425
1426   gfx::Size device_viewport(800, 600);
1427   gfx::Size last_layer_bounds(200, 200);
1428   gfx::Size current_layer_bounds(200, 200);
1429   float current_layer_contents_scale = 1.f;
1430   gfx::Transform last_screen_transform;
1431   gfx::Transform current_screen_transform;
1432   double current_frame_time_in_seconds = 1.0;
1433
1434   // A 3d perspective layer rotated about its Y axis, translated to almost
1435   // fully offscreen. The left side will appear closer (i.e. larger in 2d) than
1436   // the right side, so the top-left tile will technically be closer than the
1437   // top-right.
1438
1439   // Translate layer to offscreen
1440   current_screen_transform.Translate(400.0, 970.0);
1441   // Apply perspective and rotation about the center of the layer
1442   current_screen_transform.Translate(100.0, 100.0);
1443   current_screen_transform.ApplyPerspectiveDepth(10.0);
1444   current_screen_transform.RotateAboutYAxis(10.0);
1445   current_screen_transform.Translate(-100.0, -100.0);
1446   last_screen_transform = current_screen_transform;
1447
1448   // Sanity check that this transform does cause w<0 clipping for the left side
1449   // of the layer, but not the right side.
1450   bool clipped;
1451   MathUtil::MapQuad(current_screen_transform,
1452                     gfx::QuadF(gfx::RectF(0, 0, 100, 200)),
1453                     &clipped);
1454   ASSERT_TRUE(clipped);
1455
1456   MathUtil::MapQuad(current_screen_transform,
1457                     gfx::QuadF(gfx::RectF(100, 0, 100, 200)),
1458                     &clipped);
1459   ASSERT_FALSE(clipped);
1460
1461   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1462       current_screen_transform, device_viewport);
1463
1464   client.SetTileSize(gfx::Size(100, 100));
1465   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1466                                               current_layer_bounds,
1467                                               &client);
1468
1469   tiling->UpdateTilePriorities(ACTIVE_TREE,
1470                                viewport_in_layer_space,
1471                                current_layer_contents_scale,
1472                                current_frame_time_in_seconds);
1473
1474   ASSERT_TRUE(tiling->TileAt(0, 0));
1475   ASSERT_TRUE(tiling->TileAt(0, 1));
1476   ASSERT_TRUE(tiling->TileAt(1, 0));
1477   ASSERT_TRUE(tiling->TileAt(1, 1));
1478
1479   // Left-side tiles will be clipped by the transform, so we have to assume
1480   // they are visible just in case.
1481   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1482   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1483   EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1484
1485   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1486   EXPECT_GT(priority.distance_to_visible, 0.f);
1487   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1488
1489   // Right-side tiles will have a positive distance_to_visible
1490   // and an infinite time_to_visible.
1491   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1492   EXPECT_FLOAT_EQ(priority.distance_to_visible, 0.f);
1493   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1494
1495   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1496   EXPECT_GT(priority.distance_to_visible, 0.f);
1497   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1498 }
1499
1500 TEST(UpdateTilePrioritiesTest, BasicMotion) {
1501   // Test that time_to_visible is computed correctly when
1502   // there is some motion.
1503
1504   FakePictureLayerTilingClient client;
1505   scoped_ptr<TestablePictureLayerTiling> tiling;
1506
1507   gfx::Size device_viewport(800, 600);
1508   gfx::Rect visible_layer_rect(0, 0, 0, 0);
1509   gfx::Size last_layer_bounds(200, 200);
1510   gfx::Size current_layer_bounds(200, 200);
1511   float last_layer_contents_scale = 1.f;
1512   float current_layer_contents_scale = 1.f;
1513   gfx::Transform last_screen_transform;
1514   gfx::Transform current_screen_transform;
1515   double last_frame_time_in_seconds = 1.0;
1516   double current_frame_time_in_seconds = 2.0;
1517
1518   // Offscreen layer is coming closer to viewport at 1000 pixels per second.
1519   current_screen_transform.Translate(1800, 0);
1520   last_screen_transform.Translate(2800, 0);
1521
1522   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1523       current_screen_transform, device_viewport);
1524
1525   client.SetTileSize(gfx::Size(100, 100));
1526   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1527                                               current_layer_bounds,
1528                                               &client);
1529
1530   // previous ("last") frame
1531   tiling->UpdateTilePriorities(ACTIVE_TREE,
1532                                viewport_in_layer_space,
1533                                last_layer_contents_scale,
1534                                last_frame_time_in_seconds);
1535
1536   // current frame
1537   tiling->UpdateTilePriorities(ACTIVE_TREE,
1538                                viewport_in_layer_space,
1539                                current_layer_contents_scale,
1540                                current_frame_time_in_seconds);
1541
1542   ASSERT_TRUE(tiling->TileAt(0, 0));
1543   ASSERT_TRUE(tiling->TileAt(0, 1));
1544   ASSERT_TRUE(tiling->TileAt(1, 0));
1545   ASSERT_TRUE(tiling->TileAt(1, 1));
1546
1547   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1548   EXPECT_GT(priority.distance_to_visible, 0.f);
1549   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1550
1551   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1552   EXPECT_GT(priority.distance_to_visible, 0.f);
1553   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1554
1555   // time_to_visible for the right hand side layers needs an extra 0.099
1556   // seconds because this tile is 99 pixels further away.
1557   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1558   EXPECT_GT(priority.distance_to_visible, 0.f);
1559   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1560
1561   priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1562   EXPECT_GT(priority.distance_to_visible, 0.f);
1563   EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1564 }
1565
1566 TEST(UpdateTilePrioritiesTest, RotationMotion) {
1567   // Each tile of a layer may be affected differently by a transform; Check
1568   // that UpdateTilePriorities correctly accounts for the transform between
1569   // layer space and screen space.
1570
1571   FakePictureLayerTilingClient client;
1572   scoped_ptr<TestablePictureLayerTiling> tiling;
1573
1574   gfx::Size device_viewport(800, 600);
1575   gfx::Rect visible_layer_rect(0, 0, 0, 0);  // offscren.
1576   gfx::Size last_layer_bounds(200, 200);
1577   gfx::Size current_layer_bounds(200, 200);
1578   float last_layer_contents_scale = 1.f;
1579   float current_layer_contents_scale = 1.f;
1580   gfx::Transform last_screen_transform;
1581   gfx::Transform current_screen_transform;
1582   double last_frame_time_in_seconds = 1.0;
1583   double current_frame_time_in_seconds = 2.0;
1584
1585   // Rotation motion is set up specifically so that:
1586   //  - rotation occurs about the center of the layer
1587   //  - the top-left tile becomes visible on rotation
1588   //  - the top-right tile will have an infinite time_to_visible
1589   //    because it is rotating away from viewport.
1590   //  - bottom-left layer will have a positive non-zero time_to_visible
1591   //    because it is rotating toward the viewport.
1592   current_screen_transform.Translate(400, 550);
1593   current_screen_transform.RotateAboutZAxis(45);
1594
1595   last_screen_transform.Translate(400, 550);
1596
1597   gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1598       current_screen_transform, device_viewport);
1599
1600   client.SetTileSize(gfx::Size(100, 100));
1601   tiling = TestablePictureLayerTiling::Create(1.0f,  // contents_scale
1602                                               current_layer_bounds,
1603                                               &client);
1604
1605   // previous ("last") frame
1606   tiling->UpdateTilePriorities(ACTIVE_TREE,
1607                                viewport_in_layer_space,
1608                                last_layer_contents_scale,
1609                                last_frame_time_in_seconds);
1610
1611   // current frame
1612   tiling->UpdateTilePriorities(ACTIVE_TREE,
1613                                viewport_in_layer_space,
1614                                current_layer_contents_scale,
1615                                current_frame_time_in_seconds);
1616
1617   ASSERT_TRUE(tiling->TileAt(0, 0));
1618   ASSERT_TRUE(tiling->TileAt(0, 1));
1619   ASSERT_TRUE(tiling->TileAt(1, 0));
1620   ASSERT_TRUE(tiling->TileAt(1, 1));
1621
1622   TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1623   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1624   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1625
1626   priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1627   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1628   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1629
1630   priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1631   EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1632   EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1633 }
1634
1635 }  // namespace
1636 }  // namespace cc