Reland "Add base::TimeDelta::Max()"
[chromium-blink-merge.git] / cc / resources / picture_layer_tiling_unittest.cc
blob5fe1acd1031210a53e1fd61c1f14708e1b35ace2
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.
5 #include "cc/resources/picture_layer_tiling.h"
7 #include <limits>
9 #include "cc/base/math_util.h"
10 #include "cc/resources/picture_layer_tiling_set.h"
11 #include "cc/test/fake_picture_layer_tiling_client.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/gfx/rect_conversions.h"
14 #include "ui/gfx/size_conversions.h"
16 namespace cc {
17 namespace {
19 static gfx::Rect ViewportInLayerSpace(
20 const gfx::Transform& transform,
21 const gfx::Size& device_viewport) {
23 gfx::Transform inverse;
24 if (!transform.GetInverse(&inverse))
25 return gfx::Rect();
27 gfx::RectF viewport_in_layer_space = MathUtil::ProjectClippedRect(
28 inverse, gfx::RectF(gfx::Point(0, 0), device_viewport));
29 return ToEnclosingRect(viewport_in_layer_space);
32 class TestablePictureLayerTiling : public PictureLayerTiling {
33 public:
34 using PictureLayerTiling::SetLiveTilesRect;
35 using PictureLayerTiling::TileAt;
37 static scoped_ptr<TestablePictureLayerTiling> Create(
38 float contents_scale,
39 const gfx::Size& layer_bounds,
40 PictureLayerTilingClient* client) {
41 return make_scoped_ptr(new TestablePictureLayerTiling(
42 contents_scale,
43 layer_bounds,
44 client));
47 using PictureLayerTiling::ComputeSkewport;
49 protected:
50 TestablePictureLayerTiling(float contents_scale,
51 const gfx::Size& layer_bounds,
52 PictureLayerTilingClient* client)
53 : PictureLayerTiling(contents_scale, layer_bounds, client) { }
56 class PictureLayerTilingIteratorTest : public testing::Test {
57 public:
58 PictureLayerTilingIteratorTest() {}
59 virtual ~PictureLayerTilingIteratorTest() {}
61 void Initialize(const gfx::Size& tile_size,
62 float contents_scale,
63 const gfx::Size& layer_bounds) {
64 client_.SetTileSize(tile_size);
65 tiling_ = TestablePictureLayerTiling::Create(contents_scale,
66 layer_bounds,
67 &client_);
70 void SetLiveRectAndVerifyTiles(const gfx::Rect& live_tiles_rect) {
71 tiling_->SetLiveTilesRect(live_tiles_rect);
73 std::vector<Tile*> tiles = tiling_->AllTilesForTesting();
74 for (std::vector<Tile*>::iterator iter = tiles.begin();
75 iter != tiles.end();
76 ++iter) {
77 EXPECT_TRUE(live_tiles_rect.Intersects((*iter)->content_rect()));
81 void VerifyTilesExactlyCoverRect(
82 float rect_scale,
83 const gfx::Rect& request_rect,
84 const gfx::Rect& expect_rect) {
85 EXPECT_TRUE(request_rect.Contains(expect_rect));
87 // Iterators are not valid if this ratio is too large (i.e. the
88 // tiling is too high-res for a low-res destination rect.) This is an
89 // artifact of snapping geometry to integer coordinates and then mapping
90 // back to floating point texture coordinates.
91 float dest_to_contents_scale = tiling_->contents_scale() / rect_scale;
92 ASSERT_LE(dest_to_contents_scale, 2.0);
94 Region remaining = expect_rect;
95 for (PictureLayerTiling::CoverageIterator
96 iter(tiling_.get(), rect_scale, request_rect);
97 iter;
98 ++iter) {
99 // Geometry cannot overlap previous geometry at all
100 gfx::Rect geometry = iter.geometry_rect();
101 EXPECT_TRUE(expect_rect.Contains(geometry));
102 EXPECT_TRUE(remaining.Contains(geometry));
103 remaining.Subtract(geometry);
105 // Sanity check that texture coords are within the texture rect.
106 gfx::RectF texture_rect = iter.texture_rect();
107 EXPECT_GE(texture_rect.x(), 0);
108 EXPECT_GE(texture_rect.y(), 0);
109 EXPECT_LE(texture_rect.right(), client_.TileSize().width());
110 EXPECT_LE(texture_rect.bottom(), client_.TileSize().height());
112 EXPECT_EQ(iter.texture_size(), client_.TileSize());
115 // The entire rect must be filled by geometry from the tiling.
116 EXPECT_TRUE(remaining.IsEmpty());
119 void VerifyTilesExactlyCoverRect(float rect_scale, const gfx::Rect& rect) {
120 VerifyTilesExactlyCoverRect(rect_scale, rect, rect);
123 void VerifyTiles(
124 float rect_scale,
125 const gfx::Rect& rect,
126 base::Callback<void(Tile* tile,
127 const gfx::Rect& geometry_rect)> callback) {
128 VerifyTiles(tiling_.get(),
129 rect_scale,
130 rect,
131 callback);
134 void VerifyTiles(
135 PictureLayerTiling* tiling,
136 float rect_scale,
137 const gfx::Rect& rect,
138 base::Callback<void(Tile* tile,
139 const gfx::Rect& geometry_rect)> callback) {
140 Region remaining = rect;
141 for (PictureLayerTiling::CoverageIterator iter(tiling, rect_scale, rect);
142 iter;
143 ++iter) {
144 remaining.Subtract(iter.geometry_rect());
145 callback.Run(*iter, iter.geometry_rect());
147 EXPECT_TRUE(remaining.IsEmpty());
150 void VerifyTilesCoverNonContainedRect(float rect_scale,
151 const gfx::Rect& dest_rect) {
152 float dest_to_contents_scale = tiling_->contents_scale() / rect_scale;
153 gfx::Rect clamped_rect = gfx::ScaleToEnclosingRect(
154 tiling_->ContentRect(), 1.f / dest_to_contents_scale);
155 clamped_rect.Intersect(dest_rect);
156 VerifyTilesExactlyCoverRect(rect_scale, dest_rect, clamped_rect);
159 void set_max_tiles_for_interest_area(size_t area) {
160 client_.set_max_tiles_for_interest_area(area);
163 protected:
164 FakePictureLayerTilingClient client_;
165 scoped_ptr<TestablePictureLayerTiling> tiling_;
167 private:
168 DISALLOW_COPY_AND_ASSIGN(PictureLayerTilingIteratorTest);
171 TEST_F(PictureLayerTilingIteratorTest, LiveTilesExactlyCoverLiveTileRect) {
172 Initialize(gfx::Size(100, 100), 1, gfx::Size(1099, 801));
173 SetLiveRectAndVerifyTiles(gfx::Rect(100, 100));
174 SetLiveRectAndVerifyTiles(gfx::Rect(101, 99));
175 SetLiveRectAndVerifyTiles(gfx::Rect(1099, 1));
176 SetLiveRectAndVerifyTiles(gfx::Rect(1, 801));
177 SetLiveRectAndVerifyTiles(gfx::Rect(1099, 1));
178 SetLiveRectAndVerifyTiles(gfx::Rect(201, 800));
181 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsNoScale) {
182 Initialize(gfx::Size(100, 100), 1, gfx::Size(1099, 801));
183 VerifyTilesExactlyCoverRect(1, gfx::Rect());
184 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1099, 801));
185 VerifyTilesExactlyCoverRect(1, gfx::Rect(52, 83, 789, 412));
187 // With borders, a size of 3x3 = 1 pixel of content.
188 Initialize(gfx::Size(3, 3), 1, gfx::Size(10, 10));
189 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
190 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
191 VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
192 VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
195 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsTilingScale) {
196 Initialize(gfx::Size(200, 100), 2.0f, gfx::Size(1005, 2010));
197 VerifyTilesExactlyCoverRect(1, gfx::Rect());
198 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
199 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
201 Initialize(gfx::Size(3, 3), 2.0f, gfx::Size(10, 10));
202 VerifyTilesExactlyCoverRect(1, gfx::Rect());
203 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
204 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
205 VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
206 VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
208 Initialize(gfx::Size(100, 200), 0.5f, gfx::Size(1005, 2010));
209 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
210 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
212 Initialize(gfx::Size(150, 250), 0.37f, gfx::Size(1005, 2010));
213 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
214 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
216 Initialize(gfx::Size(312, 123), 0.01f, gfx::Size(1005, 2010));
217 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
218 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
221 TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsBothScale) {
222 Initialize(gfx::Size(50, 50), 4.0f, gfx::Size(800, 600));
223 VerifyTilesExactlyCoverRect(2.0f, gfx::Rect());
224 VerifyTilesExactlyCoverRect(2.0f, gfx::Rect(0, 0, 1600, 1200));
225 VerifyTilesExactlyCoverRect(2.0f, gfx::Rect(512, 365, 253, 182));
227 float scale = 6.7f;
228 gfx::Size bounds(800, 600);
229 gfx::Rect full_rect(gfx::ToCeiledSize(gfx::ScaleSize(bounds, scale)));
230 Initialize(gfx::Size(256, 512), 5.2f, bounds);
231 VerifyTilesExactlyCoverRect(scale, full_rect);
232 VerifyTilesExactlyCoverRect(scale, gfx::Rect(2014, 1579, 867, 1033));
235 TEST_F(PictureLayerTilingIteratorTest, IteratorEmptyRect) {
236 Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(800, 600));
238 gfx::Rect empty;
239 PictureLayerTiling::CoverageIterator iter(tiling_.get(), 1.0f, empty);
240 EXPECT_FALSE(iter);
243 TEST_F(PictureLayerTilingIteratorTest, NonIntersectingRect) {
244 Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(800, 600));
245 gfx::Rect non_intersecting(1000, 1000, 50, 50);
246 PictureLayerTiling::CoverageIterator iter(tiling_.get(), 1, non_intersecting);
247 EXPECT_FALSE(iter);
250 TEST_F(PictureLayerTilingIteratorTest, LayerEdgeTextureCoordinates) {
251 Initialize(gfx::Size(300, 300), 1.0f, gfx::Size(256, 256));
252 // All of these sizes are 256x256, scaled and ceiled.
253 VerifyTilesExactlyCoverRect(1.0f, gfx::Rect(0, 0, 256, 256));
254 VerifyTilesExactlyCoverRect(0.8f, gfx::Rect(0, 0, 205, 205));
255 VerifyTilesExactlyCoverRect(1.2f, gfx::Rect(0, 0, 308, 308));
258 TEST_F(PictureLayerTilingIteratorTest, NonContainedDestRect) {
259 Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(400, 400));
261 // Too large in all dimensions
262 VerifyTilesCoverNonContainedRect(1.0f, gfx::Rect(-1000, -1000, 2000, 2000));
263 VerifyTilesCoverNonContainedRect(1.5f, gfx::Rect(-1000, -1000, 2000, 2000));
264 VerifyTilesCoverNonContainedRect(0.5f, gfx::Rect(-1000, -1000, 2000, 2000));
266 // Partially covering content, but too large
267 VerifyTilesCoverNonContainedRect(1.0f, gfx::Rect(-1000, 100, 2000, 100));
268 VerifyTilesCoverNonContainedRect(1.5f, gfx::Rect(-1000, 100, 2000, 100));
269 VerifyTilesCoverNonContainedRect(0.5f, gfx::Rect(-1000, 100, 2000, 100));
272 TEST(PictureLayerTilingTest, SkewportLimits) {
273 FakePictureLayerTilingClient client;
274 client.set_skewport_extrapolation_limit_in_content_pixels(75);
275 scoped_ptr<TestablePictureLayerTiling> tiling;
277 gfx::Rect viewport(0, 0, 100, 100);
278 gfx::Size layer_bounds(200, 200);
280 client.SetTileSize(gfx::Size(100, 100));
281 tiling = TestablePictureLayerTiling::Create(1.0f, layer_bounds, &client);
283 tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 1.f, 1.0);
285 // Move viewport down 50 pixels in 0.5 seconds.
286 gfx::Rect down_skewport =
287 tiling->ComputeSkewport(1.5, gfx::Rect(0, 50, 100, 100));
289 EXPECT_EQ(0, down_skewport.x());
290 EXPECT_EQ(50, down_skewport.y());
291 EXPECT_EQ(100, down_skewport.width());
292 EXPECT_EQ(175, down_skewport.height());
293 EXPECT_TRUE(down_skewport.Contains(gfx::Rect(0, 50, 100, 100)));
295 // Move viewport down 50 and right 10 pixels.
296 gfx::Rect down_right_skewport =
297 tiling->ComputeSkewport(1.5, gfx::Rect(10, 50, 100, 100));
299 EXPECT_EQ(10, down_right_skewport.x());
300 EXPECT_EQ(50, down_right_skewport.y());
301 EXPECT_EQ(120, down_right_skewport.width());
302 EXPECT_EQ(175, down_right_skewport.height());
303 EXPECT_TRUE(down_right_skewport.Contains(gfx::Rect(10, 50, 100, 100)));
305 // Move viewport left.
306 gfx::Rect left_skewport =
307 tiling->ComputeSkewport(1.5, gfx::Rect(-50, 0, 100, 100));
309 EXPECT_EQ(-125, left_skewport.x());
310 EXPECT_EQ(0, left_skewport.y());
311 EXPECT_EQ(175, left_skewport.width());
312 EXPECT_EQ(100, left_skewport.height());
313 EXPECT_TRUE(left_skewport.Contains(gfx::Rect(-50, 0, 100, 100)));
315 // Expand viewport.
316 gfx::Rect expand_skewport =
317 tiling->ComputeSkewport(1.5, gfx::Rect(-50, -50, 200, 200));
319 // x and y moved by -75 (-50 - 75 = -125).
320 // right side and bottom side moved by 75 [(350 - 125) - (200 - 50) = 75].
321 EXPECT_EQ(-125, expand_skewport.x());
322 EXPECT_EQ(-125, expand_skewport.y());
323 EXPECT_EQ(350, expand_skewport.width());
324 EXPECT_EQ(350, expand_skewport.height());
325 EXPECT_TRUE(expand_skewport.Contains(gfx::Rect(-50, -50, 200, 200)));
327 // Expand the viewport past the limit.
328 gfx::Rect big_expand_skewport =
329 tiling->ComputeSkewport(1.5, gfx::Rect(-500, -500, 1500, 1500));
331 EXPECT_EQ(-575, big_expand_skewport.x());
332 EXPECT_EQ(-575, big_expand_skewport.y());
333 EXPECT_EQ(1650, big_expand_skewport.width());
334 EXPECT_EQ(1650, big_expand_skewport.height());
335 EXPECT_TRUE(big_expand_skewport.Contains(gfx::Rect(-500, -500, 1500, 1500)));
338 TEST(PictureLayerTilingTest, ComputeSkewport) {
339 FakePictureLayerTilingClient client;
340 scoped_ptr<TestablePictureLayerTiling> tiling;
342 gfx::Rect viewport(0, 0, 100, 100);
343 gfx::Size layer_bounds(200, 200);
345 client.SetTileSize(gfx::Size(100, 100));
346 tiling = TestablePictureLayerTiling::Create(1.0f, layer_bounds, &client);
348 tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 1.f, 1.0);
350 // Move viewport down 50 pixels in 0.5 seconds.
351 gfx::Rect down_skewport =
352 tiling->ComputeSkewport(1.5, gfx::Rect(0, 50, 100, 100));
354 EXPECT_EQ(0, down_skewport.x());
355 EXPECT_EQ(50, down_skewport.y());
356 EXPECT_EQ(100, down_skewport.width());
357 EXPECT_EQ(200, down_skewport.height());
359 // Shrink viewport.
360 gfx::Rect shrink_skewport =
361 tiling->ComputeSkewport(1.5, gfx::Rect(25, 25, 50, 50));
363 EXPECT_EQ(25, shrink_skewport.x());
364 EXPECT_EQ(25, shrink_skewport.y());
365 EXPECT_EQ(50, shrink_skewport.width());
366 EXPECT_EQ(50, shrink_skewport.height());
368 // Move viewport down 50 and right 10 pixels.
369 gfx::Rect down_right_skewport =
370 tiling->ComputeSkewport(1.5, gfx::Rect(10, 50, 100, 100));
372 EXPECT_EQ(10, down_right_skewport.x());
373 EXPECT_EQ(50, down_right_skewport.y());
374 EXPECT_EQ(120, down_right_skewport.width());
375 EXPECT_EQ(200, down_right_skewport.height());
377 // Move viewport left.
378 gfx::Rect left_skewport =
379 tiling->ComputeSkewport(1.5, gfx::Rect(-20, 0, 100, 100));
381 EXPECT_EQ(-60, left_skewport.x());
382 EXPECT_EQ(0, left_skewport.y());
383 EXPECT_EQ(140, left_skewport.width());
384 EXPECT_EQ(100, left_skewport.height());
386 // Expand viewport in 0.2 seconds.
387 gfx::Rect expanded_skewport =
388 tiling->ComputeSkewport(1.2, gfx::Rect(-5, -5, 110, 110));
390 EXPECT_EQ(-30, expanded_skewport.x());
391 EXPECT_EQ(-30, expanded_skewport.y());
392 EXPECT_EQ(160, expanded_skewport.width());
393 EXPECT_EQ(160, expanded_skewport.height());
396 TEST(PictureLayerTilingTest, ViewportDistanceWithScale) {
397 FakePictureLayerTilingClient client;
398 scoped_ptr<TestablePictureLayerTiling> tiling;
400 gfx::Rect viewport(0, 0, 100, 100);
401 gfx::Size layer_bounds(200, 200);
403 client.SetTileSize(gfx::Size(10, 10));
405 // Tiling at 0.25 scale: this should create 36 tiles (6x6) of size 10x10.
406 // The reason is that each tile has a one pixel border, so tile at (1, 2)
407 // for instance begins at (8, 16) pixels. So tile at (5, 5) will begin at
408 // (40, 40) and extend right to the end of 200 * 0.25 = 50 edge of the
409 // tiling.
410 tiling = TestablePictureLayerTiling::Create(0.25f, layer_bounds, &client);
411 gfx::Rect viewport_in_content_space =
412 gfx::ToEnclosedRect(gfx::ScaleRect(viewport, 0.25f));
414 tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 1.f, 1.0);
416 // Sanity checks.
417 for (int i = 0; i < 6; ++i) {
418 for (int j = 0; j < 6; ++j) {
419 EXPECT_TRUE(tiling->TileAt(i, j)) << "i: " << i << " j: " << j;
422 for (int i = 0; i < 7; ++i) {
423 EXPECT_FALSE(tiling->TileAt(i, 6)) << "i: " << i;
424 EXPECT_FALSE(tiling->TileAt(6, i)) << "i: " << i;
427 // No movement in the viewport implies that tiles will either be NOW
428 // or EVENTUALLY.
429 bool have_now = false;
430 bool have_eventually = false;
431 for (int i = 0; i < 6; ++i) {
432 for (int j = 0; j < 6; ++j) {
433 Tile* tile = tiling->TileAt(i, j);
434 TilePriority priority = tile->priority(ACTIVE_TREE);
436 if (viewport_in_content_space.Intersects(tile->content_rect())) {
437 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
438 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
439 have_now = true;
440 } else {
441 EXPECT_EQ(TilePriority::EVENTUALLY, priority.priority_bin);
442 EXPECT_GT(priority.distance_to_visible, 0.f);
443 have_eventually = true;
448 EXPECT_TRUE(have_now);
449 EXPECT_TRUE(have_eventually);
451 // Spot check some distances.
452 // Tile at 5, 1 should begin at 41x9 in content space (without borders),
453 // so the distance to a viewport that ends at 25x25 in content space
454 // should be 17 (41 - 25 + 1). In layer space, then that should be
455 // 17 / 0.25 = 68 pixels.
457 // We can verify that the content rect (with borders) is one pixel off
458 // 41,9 8x8 on all sides.
459 EXPECT_EQ(tiling->TileAt(5, 1)->content_rect().ToString(), "40,8 10x10");
461 TilePriority priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
462 EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
464 priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
465 EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
467 priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
468 EXPECT_FLOAT_EQ(40.f, priority.distance_to_visible);
470 // Move the viewport down 40 pixels.
471 viewport = gfx::Rect(0, 40, 100, 100);
472 viewport_in_content_space =
473 gfx::ToEnclosedRect(gfx::ScaleRect(viewport, 0.25f));
474 gfx::Rect skewport = tiling->ComputeSkewport(2.0, viewport_in_content_space);
476 EXPECT_EQ(0, skewport.x());
477 EXPECT_EQ(10, skewport.y());
478 EXPECT_EQ(25, skewport.width());
479 EXPECT_EQ(35, skewport.height());
481 tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 1.f, 2.0);
483 have_now = false;
484 have_eventually = false;
485 bool have_soon = false;
487 // Viewport moved, so we expect to find some NOW tiles, some SOON tiles and
488 // some EVENTUALLY tiles.
489 for (int i = 0; i < 6; ++i) {
490 for (int j = 0; j < 6; ++j) {
491 Tile* tile = tiling->TileAt(i, j);
492 TilePriority priority = tile->priority(ACTIVE_TREE);
494 if (viewport_in_content_space.Intersects(tile->content_rect())) {
495 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
496 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
497 have_now = true;
498 } else if (skewport.Intersects(tile->content_rect())) {
499 EXPECT_EQ(TilePriority::SOON, priority.priority_bin);
500 EXPECT_GT(priority.distance_to_visible, 0.f);
501 have_soon = true;
502 } else {
503 EXPECT_EQ(TilePriority::EVENTUALLY, priority.priority_bin);
504 EXPECT_GT(priority.distance_to_visible, 0.f);
505 have_eventually = true;
510 EXPECT_TRUE(have_now);
511 EXPECT_TRUE(have_soon);
512 EXPECT_TRUE(have_eventually);
514 priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
515 EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
517 priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
518 EXPECT_FLOAT_EQ(28.f, priority.distance_to_visible);
520 priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
521 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
523 // Change the underlying layer scale.
524 tiling->UpdateTilePriorities(ACTIVE_TREE, viewport, 2.0f, 3.0);
526 priority = tiling->TileAt(5, 1)->priority(ACTIVE_TREE);
527 EXPECT_FLOAT_EQ(34.f, priority.distance_to_visible);
529 priority = tiling->TileAt(2, 5)->priority(ACTIVE_TREE);
530 EXPECT_FLOAT_EQ(14.f, priority.distance_to_visible);
532 priority = tiling->TileAt(3, 4)->priority(ACTIVE_TREE);
533 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
536 TEST(PictureLayerTilingTest, ExpandRectEqual) {
537 gfx::Rect in(40, 50, 100, 200);
538 gfx::Rect bounds(-1000, -1000, 10000, 10000);
539 int64 target_area = 100 * 200;
540 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
541 in, target_area, bounds, NULL);
542 EXPECT_EQ(in.ToString(), out.ToString());
545 TEST(PictureLayerTilingTest, ExpandRectSmaller) {
546 gfx::Rect in(40, 50, 100, 200);
547 gfx::Rect bounds(-1000, -1000, 10000, 10000);
548 int64 target_area = 100 * 100;
549 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
550 in, target_area, bounds, NULL);
551 EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
552 EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
553 EXPECT_EQ(out.width() - in.width(), out.height() - in.height());
554 EXPECT_NEAR(100 * 100, out.width() * out.height(), 50);
555 EXPECT_TRUE(bounds.Contains(out));
558 TEST(PictureLayerTilingTest, ExpandRectUnbounded) {
559 gfx::Rect in(40, 50, 100, 200);
560 gfx::Rect bounds(-1000, -1000, 10000, 10000);
561 int64 target_area = 200 * 200;
562 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
563 in, target_area, bounds, NULL);
564 EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
565 EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
566 EXPECT_EQ(out.width() - in.width(), out.height() - in.height());
567 EXPECT_NEAR(200 * 200, out.width() * out.height(), 100);
568 EXPECT_TRUE(bounds.Contains(out));
571 TEST(PictureLayerTilingTest, ExpandRectBoundedSmaller) {
572 gfx::Rect in(40, 50, 100, 200);
573 gfx::Rect bounds(50, 60, 40, 30);
574 int64 target_area = 200 * 200;
575 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
576 in, target_area, bounds, NULL);
577 EXPECT_EQ(bounds.ToString(), out.ToString());
580 TEST(PictureLayerTilingTest, ExpandRectBoundedEqual) {
581 gfx::Rect in(40, 50, 100, 200);
582 gfx::Rect bounds = in;
583 int64 target_area = 200 * 200;
584 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
585 in, target_area, bounds, NULL);
586 EXPECT_EQ(bounds.ToString(), out.ToString());
589 TEST(PictureLayerTilingTest, ExpandRectBoundedSmallerStretchVertical) {
590 gfx::Rect in(40, 50, 100, 200);
591 gfx::Rect bounds(45, 0, 90, 300);
592 int64 target_area = 200 * 200;
593 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
594 in, target_area, bounds, NULL);
595 EXPECT_EQ(bounds.ToString(), out.ToString());
598 TEST(PictureLayerTilingTest, ExpandRectBoundedEqualStretchVertical) {
599 gfx::Rect in(40, 50, 100, 200);
600 gfx::Rect bounds(40, 0, 100, 300);
601 int64 target_area = 200 * 200;
602 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
603 in, target_area, bounds, NULL);
604 EXPECT_EQ(bounds.ToString(), out.ToString());
607 TEST(PictureLayerTilingTest, ExpandRectBoundedSmallerStretchHorizontal) {
608 gfx::Rect in(40, 50, 100, 200);
609 gfx::Rect bounds(0, 55, 180, 190);
610 int64 target_area = 200 * 200;
611 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
612 in, target_area, bounds, NULL);
613 EXPECT_EQ(bounds.ToString(), out.ToString());
616 TEST(PictureLayerTilingTest, ExpandRectBoundedEqualStretchHorizontal) {
617 gfx::Rect in(40, 50, 100, 200);
618 gfx::Rect bounds(0, 50, 180, 200);
619 int64 target_area = 200 * 200;
620 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
621 in, target_area, bounds, NULL);
622 EXPECT_EQ(bounds.ToString(), out.ToString());
625 TEST(PictureLayerTilingTest, ExpandRectBoundedLeft) {
626 gfx::Rect in(40, 50, 100, 200);
627 gfx::Rect bounds(20, -1000, 10000, 10000);
628 int64 target_area = 200 * 200;
629 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
630 in, target_area, bounds, NULL);
631 EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
632 EXPECT_EQ(out.bottom() - in.bottom(), out.right() - in.right());
633 EXPECT_LE(out.width() * out.height(), target_area);
634 EXPECT_GT(out.width() * out.height(),
635 target_area - out.width() - out.height() * 2);
636 EXPECT_TRUE(bounds.Contains(out));
639 TEST(PictureLayerTilingTest, ExpandRectBoundedRight) {
640 gfx::Rect in(40, 50, 100, 200);
641 gfx::Rect bounds(-1000, -1000, 1000+120, 10000);
642 int64 target_area = 200 * 200;
643 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
644 in, target_area, bounds, NULL);
645 EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
646 EXPECT_EQ(out.bottom() - in.bottom(), in.x() - out.x());
647 EXPECT_LE(out.width() * out.height(), target_area);
648 EXPECT_GT(out.width() * out.height(),
649 target_area - out.width() - out.height() * 2);
650 EXPECT_TRUE(bounds.Contains(out));
653 TEST(PictureLayerTilingTest, ExpandRectBoundedTop) {
654 gfx::Rect in(40, 50, 100, 200);
655 gfx::Rect bounds(-1000, 30, 10000, 10000);
656 int64 target_area = 200 * 200;
657 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
658 in, target_area, bounds, NULL);
659 EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
660 EXPECT_EQ(out.right() - in.right(), out.bottom() - in.bottom());
661 EXPECT_LE(out.width() * out.height(), target_area);
662 EXPECT_GT(out.width() * out.height(),
663 target_area - out.width() * 2 - out.height());
664 EXPECT_TRUE(bounds.Contains(out));
667 TEST(PictureLayerTilingTest, ExpandRectBoundedBottom) {
668 gfx::Rect in(40, 50, 100, 200);
669 gfx::Rect bounds(-1000, -1000, 10000, 1000 + 220);
670 int64 target_area = 200 * 200;
671 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
672 in, target_area, bounds, NULL);
673 EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
674 EXPECT_EQ(out.right() - in.right(), in.y() - out.y());
675 EXPECT_LE(out.width() * out.height(), target_area);
676 EXPECT_GT(out.width() * out.height(),
677 target_area - out.width() * 2 - out.height());
678 EXPECT_TRUE(bounds.Contains(out));
681 TEST(PictureLayerTilingTest, ExpandRectSquishedHorizontally) {
682 gfx::Rect in(40, 50, 100, 200);
683 gfx::Rect bounds(0, -4000, 100+40+20, 100000);
684 int64 target_area = 400 * 400;
685 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
686 in, target_area, bounds, NULL);
687 EXPECT_EQ(20, out.right() - in.right());
688 EXPECT_EQ(40, in.x() - out.x());
689 EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
690 EXPECT_LE(out.width() * out.height(), target_area);
691 EXPECT_GT(out.width() * out.height(),
692 target_area - out.width() * 2);
693 EXPECT_TRUE(bounds.Contains(out));
696 TEST(PictureLayerTilingTest, ExpandRectSquishedVertically) {
697 gfx::Rect in(40, 50, 100, 200);
698 gfx::Rect bounds(-4000, 0, 100000, 200+50+30);
699 int64 target_area = 400 * 400;
700 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
701 in, target_area, bounds, NULL);
702 EXPECT_EQ(30, out.bottom() - in.bottom());
703 EXPECT_EQ(50, in.y() - out.y());
704 EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
705 EXPECT_LE(out.width() * out.height(), target_area);
706 EXPECT_GT(out.width() * out.height(),
707 target_area - out.height() * 2);
708 EXPECT_TRUE(bounds.Contains(out));
711 TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsFarAway) {
712 gfx::Rect in(400, 500, 100, 200);
713 gfx::Rect bounds(0, 0, 10, 10);
714 int64 target_area = 400 * 400;
715 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
716 in, target_area, bounds, NULL);
717 EXPECT_TRUE(out.IsEmpty());
720 TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsExpandedFullyCover) {
721 gfx::Rect in(40, 50, 100, 100);
722 gfx::Rect bounds(0, 0, 10, 10);
723 int64 target_area = 400 * 400;
724 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
725 in, target_area, bounds, NULL);
726 EXPECT_EQ(bounds.ToString(), out.ToString());
729 TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsExpandedPartlyCover) {
730 gfx::Rect in(600, 600, 100, 100);
731 gfx::Rect bounds(0, 0, 500, 500);
732 int64 target_area = 400 * 400;
733 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
734 in, target_area, bounds, NULL);
735 EXPECT_EQ(bounds.right(), out.right());
736 EXPECT_EQ(bounds.bottom(), out.bottom());
737 EXPECT_LE(out.width() * out.height(), target_area);
738 EXPECT_GT(out.width() * out.height(),
739 target_area - out.width() - out.height());
740 EXPECT_TRUE(bounds.Contains(out));
743 TEST(PictureLayerTilingTest, EmptyStartingRect) {
744 // If a layer has a non-invertible transform, then the starting rect
745 // for the layer would be empty.
746 gfx::Rect in(40, 40, 0, 0);
747 gfx::Rect bounds(0, 0, 10, 10);
748 int64 target_area = 400 * 400;
749 gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
750 in, target_area, bounds, NULL);
751 EXPECT_TRUE(out.IsEmpty());
754 static void TileExists(bool exists, Tile* tile,
755 const gfx::Rect& geometry_rect) {
756 EXPECT_EQ(exists, tile != NULL) << geometry_rect.ToString();
759 TEST_F(PictureLayerTilingIteratorTest, TilesExist) {
760 gfx::Size layer_bounds(1099, 801);
761 Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
762 VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
763 VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
765 tiling_->UpdateTilePriorities(
766 ACTIVE_TREE,
767 gfx::Rect(layer_bounds), // visible content rect
768 1.f, // current contents scale
769 1.0); // current frame time
770 VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
772 // Make the viewport rect empty. All tiles are killed and become zombies.
773 tiling_->UpdateTilePriorities(ACTIVE_TREE,
774 gfx::Rect(), // visible content rect
775 1.f, // current contents scale
776 2.0); // current frame time
777 VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
780 TEST_F(PictureLayerTilingIteratorTest, TilesExistGiantViewport) {
781 gfx::Size layer_bounds(1099, 801);
782 Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
783 VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
784 VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
786 gfx::Rect giant_rect(-10000000, -10000000, 1000000000, 1000000000);
788 tiling_->UpdateTilePriorities(
789 ACTIVE_TREE,
790 gfx::Rect(layer_bounds), // visible content rect
791 1.f, // current contents scale
792 1.0); // current frame time
793 VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
795 // If the visible content rect is empty, it should still have live tiles.
796 tiling_->UpdateTilePriorities(ACTIVE_TREE,
797 giant_rect, // visible content rect
798 1.f, // current contents scale
799 2.0); // current frame time
800 VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
803 TEST_F(PictureLayerTilingIteratorTest, TilesExistOutsideViewport) {
804 gfx::Size layer_bounds(1099, 801);
805 Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
806 VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
807 VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
809 // This rect does not intersect with the layer, as the layer is outside the
810 // viewport.
811 gfx::Rect viewport_rect(1100, 0, 1000, 1000);
812 EXPECT_FALSE(viewport_rect.Intersects(gfx::Rect(layer_bounds)));
814 tiling_->UpdateTilePriorities(ACTIVE_TREE,
815 viewport_rect, // visible content rect
816 1.f, // current contents scale
817 1.0); // current frame time
818 VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
821 static void TilesIntersectingRectExist(const gfx::Rect& rect,
822 bool intersect_exists,
823 Tile* tile,
824 const gfx::Rect& geometry_rect) {
825 bool intersects = rect.Intersects(geometry_rect);
826 bool expected_exists = intersect_exists ? intersects : !intersects;
827 EXPECT_EQ(expected_exists, tile != NULL)
828 << "Rects intersecting " << rect.ToString() << " should exist. "
829 << "Current tile rect is " << geometry_rect.ToString();
832 TEST_F(PictureLayerTilingIteratorTest,
833 TilesExistLargeViewportAndLayerWithSmallVisibleArea) {
834 gfx::Size layer_bounds(10000, 10000);
835 Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
836 VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
837 VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
839 gfx::Rect visible_rect(8000, 8000, 50, 50);
841 set_max_tiles_for_interest_area(1);
842 tiling_->UpdateTilePriorities(ACTIVE_TREE,
843 visible_rect, // visible content rect
844 1.f, // current contents scale
845 1.0); // current frame time
846 VerifyTiles(1.f,
847 gfx::Rect(layer_bounds),
848 base::Bind(&TilesIntersectingRectExist, visible_rect, true));
851 static void CountExistingTiles(int *count,
852 Tile* tile,
853 const gfx::Rect& geometry_rect) {
854 if (tile != NULL)
855 ++(*count);
858 TEST_F(PictureLayerTilingIteratorTest,
859 TilesExistLargeViewportAndLayerWithLargeVisibleArea) {
860 gfx::Size layer_bounds(10000, 10000);
861 Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
862 VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
863 VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
865 set_max_tiles_for_interest_area(1);
866 tiling_->UpdateTilePriorities(
867 ACTIVE_TREE,
868 gfx::Rect(layer_bounds), // visible content rect
869 1.f, // current contents scale
870 1.0); // current frame time
872 int num_tiles = 0;
873 VerifyTiles(1.f,
874 gfx::Rect(layer_bounds),
875 base::Bind(&CountExistingTiles, &num_tiles));
876 // If we're making a rect the size of one tile, it can only overlap up to 4
877 // tiles depending on its position.
878 EXPECT_LE(num_tiles, 4);
879 VerifyTiles(1.f, gfx::Rect(), base::Bind(&TileExists, false));
882 TEST_F(PictureLayerTilingIteratorTest, AddTilingsToMatchScale) {
883 gfx::Size layer_bounds(1099, 801);
884 gfx::Size tile_size(100, 100);
886 client_.SetTileSize(tile_size);
888 PictureLayerTilingSet active_set(&client_, layer_bounds);
890 active_set.AddTiling(1.f);
892 VerifyTiles(active_set.tiling_at(0),
893 1.f,
894 gfx::Rect(layer_bounds),
895 base::Bind(&TileExists, false));
897 active_set.UpdateTilePriorities(
898 PENDING_TREE,
899 gfx::Rect(layer_bounds), // visible content rect
900 1.f, // current contents scale
901 1.0); // current frame time
903 // The active tiling has tiles now.
904 VerifyTiles(active_set.tiling_at(0),
905 1.f,
906 gfx::Rect(layer_bounds),
907 base::Bind(&TileExists, true));
909 // Add the same tilings to the pending set.
910 PictureLayerTilingSet pending_set(&client_, layer_bounds);
911 Region invalidation;
912 pending_set.SyncTilings(active_set, layer_bounds, invalidation, 0.f);
914 // The pending tiling starts with no tiles.
915 VerifyTiles(pending_set.tiling_at(0),
916 1.f,
917 gfx::Rect(layer_bounds),
918 base::Bind(&TileExists, false));
920 // UpdateTilePriorities on the pending tiling at the same frame time. The
921 // pending tiling should get tiles.
922 pending_set.UpdateTilePriorities(
923 PENDING_TREE,
924 gfx::Rect(layer_bounds), // visible content rect
925 1.f, // current contents scale
926 1.0); // current frame time
928 VerifyTiles(pending_set.tiling_at(0),
929 1.f,
930 gfx::Rect(layer_bounds),
931 base::Bind(&TileExists, true));
934 TEST(UpdateTilePrioritiesTest, VisibleTiles) {
935 // The TilePriority of visible tiles should have zero distance_to_visible
936 // and time_to_visible.
938 FakePictureLayerTilingClient client;
939 scoped_ptr<TestablePictureLayerTiling> tiling;
941 gfx::Size device_viewport(800, 600);
942 gfx::Size last_layer_bounds(200, 200);
943 gfx::Size current_layer_bounds(200, 200);
944 float current_layer_contents_scale = 1.f;
945 gfx::Transform current_screen_transform;
946 double current_frame_time_in_seconds = 1.0;
948 gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
949 current_screen_transform, device_viewport);
951 client.SetTileSize(gfx::Size(100, 100));
952 tiling = TestablePictureLayerTiling::Create(1.0f, // contents_scale
953 current_layer_bounds,
954 &client);
956 tiling->UpdateTilePriorities(ACTIVE_TREE,
957 viewport_in_layer_space,
958 current_layer_contents_scale,
959 current_frame_time_in_seconds);
961 ASSERT_TRUE(tiling->TileAt(0, 0));
962 ASSERT_TRUE(tiling->TileAt(0, 1));
963 ASSERT_TRUE(tiling->TileAt(1, 0));
964 ASSERT_TRUE(tiling->TileAt(1, 1));
966 TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
967 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
968 EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
970 priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
971 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
972 EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
974 priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
975 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
976 EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
978 priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
979 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
980 EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
983 TEST(UpdateTilePrioritiesTest, OffscreenTiles) {
984 // The TilePriority of offscreen tiles (without movement) should have nonzero
985 // distance_to_visible and infinite time_to_visible.
987 FakePictureLayerTilingClient client;
988 scoped_ptr<TestablePictureLayerTiling> tiling;
990 gfx::Size device_viewport(800, 600);
991 gfx::Size last_layer_bounds(200, 200);
992 gfx::Size current_layer_bounds(200, 200);
993 float current_layer_contents_scale = 1.f;
994 gfx::Transform last_screen_transform;
995 gfx::Transform current_screen_transform;
996 double current_frame_time_in_seconds = 1.0;
998 current_screen_transform.Translate(850, 0);
999 last_screen_transform = current_screen_transform;
1001 gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1002 current_screen_transform, device_viewport);
1004 client.SetTileSize(gfx::Size(100, 100));
1005 tiling = TestablePictureLayerTiling::Create(1.0f, // contents_scale
1006 current_layer_bounds,
1007 &client);
1009 tiling->UpdateTilePriorities(ACTIVE_TREE,
1010 viewport_in_layer_space,
1011 current_layer_contents_scale,
1012 current_frame_time_in_seconds);
1014 ASSERT_TRUE(tiling->TileAt(0, 0));
1015 ASSERT_TRUE(tiling->TileAt(0, 1));
1016 ASSERT_TRUE(tiling->TileAt(1, 0));
1017 ASSERT_TRUE(tiling->TileAt(1, 1));
1019 TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1020 EXPECT_GT(priority.distance_to_visible, 0.f);
1021 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1023 priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1024 EXPECT_GT(priority.distance_to_visible, 0.f);
1025 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1027 priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1028 EXPECT_GT(priority.distance_to_visible, 0.f);
1029 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1031 priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1032 EXPECT_GT(priority.distance_to_visible, 0.f);
1033 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1035 // Furthermore, in this scenario tiles on the right hand side should have a
1036 // larger distance to visible.
1037 TilePriority left = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1038 TilePriority right = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1039 EXPECT_GT(right.distance_to_visible, left.distance_to_visible);
1041 left = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1042 right = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1043 EXPECT_GT(right.distance_to_visible, left.distance_to_visible);
1046 TEST(UpdateTilePrioritiesTest, PartiallyOffscreenLayer) {
1047 // Sanity check that a layer with some tiles visible and others offscreen has
1048 // correct TilePriorities for each tile.
1050 FakePictureLayerTilingClient client;
1051 scoped_ptr<TestablePictureLayerTiling> tiling;
1053 gfx::Size device_viewport(800, 600);
1054 gfx::Size last_layer_bounds(200, 200);
1055 gfx::Size current_layer_bounds(200, 200);
1056 float current_layer_contents_scale = 1.f;
1057 gfx::Transform last_screen_transform;
1058 gfx::Transform current_screen_transform;
1059 double current_frame_time_in_seconds = 1.0;
1061 current_screen_transform.Translate(705, 505);
1062 last_screen_transform = current_screen_transform;
1064 gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1065 current_screen_transform, device_viewport);
1067 client.SetTileSize(gfx::Size(100, 100));
1068 tiling = TestablePictureLayerTiling::Create(1.0f, // contents_scale
1069 current_layer_bounds,
1070 &client);
1072 tiling->UpdateTilePriorities(ACTIVE_TREE,
1073 viewport_in_layer_space,
1074 current_layer_contents_scale,
1075 current_frame_time_in_seconds);
1077 ASSERT_TRUE(tiling->TileAt(0, 0));
1078 ASSERT_TRUE(tiling->TileAt(0, 1));
1079 ASSERT_TRUE(tiling->TileAt(1, 0));
1080 ASSERT_TRUE(tiling->TileAt(1, 1));
1082 TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1083 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1084 EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1086 priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1087 EXPECT_GT(priority.distance_to_visible, 0.f);
1088 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1090 priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1091 EXPECT_GT(priority.distance_to_visible, 0.f);
1092 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1094 priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1095 EXPECT_GT(priority.distance_to_visible, 0.f);
1096 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1099 TEST(UpdateTilePrioritiesTest, PartiallyOffscreenRotatedLayer) {
1100 // Each tile of a layer may be affected differently by a transform; Check
1101 // that UpdateTilePriorities correctly accounts for the transform between
1102 // layer space and screen space.
1104 FakePictureLayerTilingClient client;
1105 scoped_ptr<TestablePictureLayerTiling> tiling;
1107 gfx::Size device_viewport(800, 600);
1108 gfx::Size last_layer_bounds(200, 200);
1109 gfx::Size current_layer_bounds(200, 200);
1110 float current_layer_contents_scale = 1.f;
1111 gfx::Transform last_screen_transform;
1112 gfx::Transform current_screen_transform;
1113 double current_frame_time_in_seconds = 1.0;
1115 // A diagonally rotated layer that is partially off the bottom of the screen.
1116 // In this configuration, only the top-left tile would be visible.
1117 current_screen_transform.Translate(600, 750);
1118 current_screen_transform.RotateAboutZAxis(45);
1119 last_screen_transform = current_screen_transform;
1121 gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1122 current_screen_transform, device_viewport);
1124 client.SetTileSize(gfx::Size(100, 100));
1125 tiling = TestablePictureLayerTiling::Create(1.0f, // contents_scale
1126 current_layer_bounds,
1127 &client);
1129 tiling->UpdateTilePriorities(ACTIVE_TREE,
1130 viewport_in_layer_space,
1131 current_layer_contents_scale,
1132 current_frame_time_in_seconds);
1134 ASSERT_TRUE(tiling->TileAt(0, 0));
1135 ASSERT_TRUE(tiling->TileAt(0, 1));
1136 ASSERT_TRUE(tiling->TileAt(1, 0));
1137 ASSERT_TRUE(tiling->TileAt(1, 1));
1139 TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1140 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1141 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1143 priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1144 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1145 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1147 priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1148 EXPECT_GT(priority.distance_to_visible, 0.f);
1149 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1151 priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1152 EXPECT_GT(priority.distance_to_visible, 0.f);
1153 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1155 // Furthermore, in this scenario the bottom-right tile should have the larger
1156 // distance to visible.
1157 TilePriority top_left = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1158 TilePriority top_right = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1159 TilePriority bottom_right = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1160 EXPECT_GT(top_right.distance_to_visible, top_left.distance_to_visible);
1162 EXPECT_EQ(bottom_right.distance_to_visible, top_right.distance_to_visible);
1165 TEST(UpdateTilePrioritiesTest, PerspectiveLayer) {
1166 // Perspective transforms need to take a different code path.
1167 // This test checks tile priorities of a perspective layer.
1169 FakePictureLayerTilingClient client;
1170 scoped_ptr<TestablePictureLayerTiling> tiling;
1172 gfx::Size device_viewport(800, 600);
1173 gfx::Rect visible_layer_rect(0, 0, 0, 0); // offscreen.
1174 gfx::Size last_layer_bounds(200, 200);
1175 gfx::Size current_layer_bounds(200, 200);
1176 float current_layer_contents_scale = 1.f;
1177 gfx::Transform last_screen_transform;
1178 gfx::Transform current_screen_transform;
1179 double current_frame_time_in_seconds = 1.0;
1181 // A 3d perspective layer rotated about its Y axis, translated to almost
1182 // fully offscreen. The left side will appear closer (i.e. larger in 2d) than
1183 // the right side, so the top-left tile will technically be closer than the
1184 // top-right.
1186 // Translate layer to offscreen
1187 current_screen_transform.Translate(400.0, 630.0);
1188 // Apply perspective about the center of the layer
1189 current_screen_transform.Translate(100.0, 100.0);
1190 current_screen_transform.ApplyPerspectiveDepth(100.0);
1191 current_screen_transform.RotateAboutYAxis(10.0);
1192 current_screen_transform.Translate(-100.0, -100.0);
1193 last_screen_transform = current_screen_transform;
1195 // Sanity check that this transform wouldn't cause w<0 clipping.
1196 bool clipped;
1197 MathUtil::MapQuad(current_screen_transform,
1198 gfx::QuadF(gfx::RectF(0, 0, 200, 200)),
1199 &clipped);
1200 ASSERT_FALSE(clipped);
1202 gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1203 current_screen_transform, device_viewport);
1205 client.SetTileSize(gfx::Size(100, 100));
1206 tiling = TestablePictureLayerTiling::Create(1.0f, // contents_scale
1207 current_layer_bounds,
1208 &client);
1210 tiling->UpdateTilePriorities(ACTIVE_TREE,
1211 viewport_in_layer_space,
1212 current_layer_contents_scale,
1213 current_frame_time_in_seconds);
1215 ASSERT_TRUE(tiling->TileAt(0, 0));
1216 ASSERT_TRUE(tiling->TileAt(0, 1));
1217 ASSERT_TRUE(tiling->TileAt(1, 0));
1218 ASSERT_TRUE(tiling->TileAt(1, 1));
1220 // All tiles will have a positive distance_to_visible
1221 // and an infinite time_to_visible.
1222 TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1223 EXPECT_FLOAT_EQ(priority.distance_to_visible, 0.f);
1224 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1226 priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1227 EXPECT_GT(priority.distance_to_visible, 0.f);
1228 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1230 priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1231 EXPECT_FLOAT_EQ(priority.distance_to_visible, 0.f);
1232 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1234 priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1235 EXPECT_GT(priority.distance_to_visible, 0.f);
1236 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1238 // Furthermore, in this scenario the top-left distance_to_visible
1239 // will be smallest, followed by top-right. The bottom layers
1240 // will of course be further than the top layers.
1241 TilePriority top_left = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1242 TilePriority top_right = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1243 TilePriority bottom_left = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1244 TilePriority bottom_right = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1246 EXPECT_GT(bottom_right.distance_to_visible, top_right.distance_to_visible);
1248 EXPECT_GT(bottom_left.distance_to_visible, top_left.distance_to_visible);
1251 TEST(UpdateTilePrioritiesTest, PerspectiveLayerClippedByW) {
1252 // Perspective transforms need to take a different code path.
1253 // This test checks tile priorities of a perspective layer.
1255 FakePictureLayerTilingClient client;
1256 scoped_ptr<TestablePictureLayerTiling> tiling;
1258 gfx::Size device_viewport(800, 600);
1259 gfx::Size last_layer_bounds(200, 200);
1260 gfx::Size current_layer_bounds(200, 200);
1261 float current_layer_contents_scale = 1.f;
1262 gfx::Transform last_screen_transform;
1263 gfx::Transform current_screen_transform;
1264 double current_frame_time_in_seconds = 1.0;
1266 // A 3d perspective layer rotated about its Y axis, translated to almost
1267 // fully offscreen. The left side will appear closer (i.e. larger in 2d) than
1268 // the right side, so the top-left tile will technically be closer than the
1269 // top-right.
1271 // Translate layer to offscreen
1272 current_screen_transform.Translate(400.0, 970.0);
1273 // Apply perspective and rotation about the center of the layer
1274 current_screen_transform.Translate(100.0, 100.0);
1275 current_screen_transform.ApplyPerspectiveDepth(10.0);
1276 current_screen_transform.RotateAboutYAxis(10.0);
1277 current_screen_transform.Translate(-100.0, -100.0);
1278 last_screen_transform = current_screen_transform;
1280 // Sanity check that this transform does cause w<0 clipping for the left side
1281 // of the layer, but not the right side.
1282 bool clipped;
1283 MathUtil::MapQuad(current_screen_transform,
1284 gfx::QuadF(gfx::RectF(0, 0, 100, 200)),
1285 &clipped);
1286 ASSERT_TRUE(clipped);
1288 MathUtil::MapQuad(current_screen_transform,
1289 gfx::QuadF(gfx::RectF(100, 0, 100, 200)),
1290 &clipped);
1291 ASSERT_FALSE(clipped);
1293 gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1294 current_screen_transform, device_viewport);
1296 client.SetTileSize(gfx::Size(100, 100));
1297 tiling = TestablePictureLayerTiling::Create(1.0f, // contents_scale
1298 current_layer_bounds,
1299 &client);
1301 tiling->UpdateTilePriorities(ACTIVE_TREE,
1302 viewport_in_layer_space,
1303 current_layer_contents_scale,
1304 current_frame_time_in_seconds);
1306 ASSERT_TRUE(tiling->TileAt(0, 0));
1307 ASSERT_TRUE(tiling->TileAt(0, 1));
1308 ASSERT_TRUE(tiling->TileAt(1, 0));
1309 ASSERT_TRUE(tiling->TileAt(1, 1));
1311 // Left-side tiles will be clipped by the transform, so we have to assume
1312 // they are visible just in case.
1313 TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1314 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1315 EXPECT_FLOAT_EQ(TilePriority::NOW, priority.priority_bin);
1317 priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1318 EXPECT_GT(priority.distance_to_visible, 0.f);
1319 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1321 // Right-side tiles will have a positive distance_to_visible
1322 // and an infinite time_to_visible.
1323 priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1324 EXPECT_FLOAT_EQ(priority.distance_to_visible, 0.f);
1325 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1327 priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1328 EXPECT_GT(priority.distance_to_visible, 0.f);
1329 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1332 TEST(UpdateTilePrioritiesTest, BasicMotion) {
1333 // Test that time_to_visible is computed correctly when
1334 // there is some motion.
1336 FakePictureLayerTilingClient client;
1337 scoped_ptr<TestablePictureLayerTiling> tiling;
1339 gfx::Size device_viewport(800, 600);
1340 gfx::Rect visible_layer_rect(0, 0, 0, 0);
1341 gfx::Size last_layer_bounds(200, 200);
1342 gfx::Size current_layer_bounds(200, 200);
1343 float last_layer_contents_scale = 1.f;
1344 float current_layer_contents_scale = 1.f;
1345 gfx::Transform last_screen_transform;
1346 gfx::Transform current_screen_transform;
1347 double last_frame_time_in_seconds = 1.0;
1348 double current_frame_time_in_seconds = 2.0;
1350 // Offscreen layer is coming closer to viewport at 1000 pixels per second.
1351 current_screen_transform.Translate(1800, 0);
1352 last_screen_transform.Translate(2800, 0);
1354 gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1355 current_screen_transform, device_viewport);
1357 client.SetTileSize(gfx::Size(100, 100));
1358 tiling = TestablePictureLayerTiling::Create(1.0f, // contents_scale
1359 current_layer_bounds,
1360 &client);
1362 // previous ("last") frame
1363 tiling->UpdateTilePriorities(ACTIVE_TREE,
1364 viewport_in_layer_space,
1365 last_layer_contents_scale,
1366 last_frame_time_in_seconds);
1368 // current frame
1369 tiling->UpdateTilePriorities(ACTIVE_TREE,
1370 viewport_in_layer_space,
1371 current_layer_contents_scale,
1372 current_frame_time_in_seconds);
1374 ASSERT_TRUE(tiling->TileAt(0, 0));
1375 ASSERT_TRUE(tiling->TileAt(0, 1));
1376 ASSERT_TRUE(tiling->TileAt(1, 0));
1377 ASSERT_TRUE(tiling->TileAt(1, 1));
1379 TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1380 EXPECT_GT(priority.distance_to_visible, 0.f);
1381 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1383 priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1384 EXPECT_GT(priority.distance_to_visible, 0.f);
1385 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1387 // time_to_visible for the right hand side layers needs an extra 0.099
1388 // seconds because this tile is 99 pixels further away.
1389 priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1390 EXPECT_GT(priority.distance_to_visible, 0.f);
1391 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1393 priority = tiling->TileAt(1, 1)->priority(ACTIVE_TREE);
1394 EXPECT_GT(priority.distance_to_visible, 0.f);
1395 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1398 TEST(UpdateTilePrioritiesTest, RotationMotion) {
1399 // Each tile of a layer may be affected differently by a transform; Check
1400 // that UpdateTilePriorities correctly accounts for the transform between
1401 // layer space and screen space.
1403 FakePictureLayerTilingClient client;
1404 scoped_ptr<TestablePictureLayerTiling> tiling;
1406 gfx::Size device_viewport(800, 600);
1407 gfx::Rect visible_layer_rect(0, 0, 0, 0); // offscren.
1408 gfx::Size last_layer_bounds(200, 200);
1409 gfx::Size current_layer_bounds(200, 200);
1410 float last_layer_contents_scale = 1.f;
1411 float current_layer_contents_scale = 1.f;
1412 gfx::Transform last_screen_transform;
1413 gfx::Transform current_screen_transform;
1414 double last_frame_time_in_seconds = 1.0;
1415 double current_frame_time_in_seconds = 2.0;
1417 // Rotation motion is set up specifically so that:
1418 // - rotation occurs about the center of the layer
1419 // - the top-left tile becomes visible on rotation
1420 // - the top-right tile will have an infinite time_to_visible
1421 // because it is rotating away from viewport.
1422 // - bottom-left layer will have a positive non-zero time_to_visible
1423 // because it is rotating toward the viewport.
1424 current_screen_transform.Translate(400, 550);
1425 current_screen_transform.RotateAboutZAxis(45);
1427 last_screen_transform.Translate(400, 550);
1429 gfx::Rect viewport_in_layer_space = ViewportInLayerSpace(
1430 current_screen_transform, device_viewport);
1432 client.SetTileSize(gfx::Size(100, 100));
1433 tiling = TestablePictureLayerTiling::Create(1.0f, // contents_scale
1434 current_layer_bounds,
1435 &client);
1437 // previous ("last") frame
1438 tiling->UpdateTilePriorities(ACTIVE_TREE,
1439 viewport_in_layer_space,
1440 last_layer_contents_scale,
1441 last_frame_time_in_seconds);
1443 // current frame
1444 tiling->UpdateTilePriorities(ACTIVE_TREE,
1445 viewport_in_layer_space,
1446 current_layer_contents_scale,
1447 current_frame_time_in_seconds);
1449 ASSERT_TRUE(tiling->TileAt(0, 0));
1450 ASSERT_TRUE(tiling->TileAt(0, 1));
1451 ASSERT_TRUE(tiling->TileAt(1, 0));
1452 ASSERT_TRUE(tiling->TileAt(1, 1));
1454 TilePriority priority = tiling->TileAt(0, 0)->priority(ACTIVE_TREE);
1455 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1456 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1458 priority = tiling->TileAt(0, 1)->priority(ACTIVE_TREE);
1459 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1460 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1462 priority = tiling->TileAt(1, 0)->priority(ACTIVE_TREE);
1463 EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
1464 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1467 } // namespace
1468 } // namespace cc