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