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.
8 #include "cc/base/math_util.h"
9 #include "cc/test/fake_output_surface.h"
10 #include "cc/test/fake_output_surface_client.h"
11 #include "cc/test/fake_picture_layer_tiling_client.h"
12 #include "cc/test/fake_picture_pile_impl.h"
13 #include "cc/test/test_context_provider.h"
14 #include "cc/test/test_shared_bitmap_manager.h"
15 #include "cc/tiles/picture_layer_tiling.h"
16 #include "cc/tiles/picture_layer_tiling_set.h"
17 #include "cc/trees/layer_tree_settings.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/gfx/geometry/quad_f.h"
20 #include "ui/gfx/geometry/rect_conversions.h"
21 #include "ui/gfx/geometry/size_conversions.h"
26 static gfx::Rect
ViewportInLayerSpace(
27 const gfx::Transform
& transform
,
28 const gfx::Size
& device_viewport
) {
30 gfx::Transform inverse
;
31 if (!transform
.GetInverse(&inverse
))
34 gfx::RectF viewport_in_layer_space
= MathUtil::ProjectClippedRect(
35 inverse
, gfx::RectF(gfx::Point(0, 0), device_viewport
));
36 return ToEnclosingRect(viewport_in_layer_space
);
39 class TestablePictureLayerTiling
: public PictureLayerTiling
{
41 using PictureLayerTiling::SetLiveTilesRect
;
42 using PictureLayerTiling::TileAt
;
44 static scoped_ptr
<TestablePictureLayerTiling
> Create(
47 scoped_refptr
<RasterSource
> raster_source
,
48 PictureLayerTilingClient
* client
,
49 const LayerTreeSettings
& settings
) {
50 return make_scoped_ptr(new TestablePictureLayerTiling(
51 tree
, contents_scale
, raster_source
, client
,
52 settings
.tiling_interest_area_padding
,
53 settings
.skewport_target_time_in_seconds
,
54 settings
.skewport_extrapolation_limit_in_content_pixels
));
57 gfx::Rect
live_tiles_rect() const { return live_tiles_rect_
; }
59 using PictureLayerTiling::ComputeSkewport
;
60 using PictureLayerTiling::RemoveTileAt
;
63 TestablePictureLayerTiling(WhichTree tree
,
65 scoped_refptr
<RasterSource
> raster_source
,
66 PictureLayerTilingClient
* client
,
67 size_t tiling_interest_area_padding
,
68 float skewport_target_time
,
69 int skewport_extrapolation_limit
)
70 : PictureLayerTiling(tree
,
74 tiling_interest_area_padding
,
76 skewport_extrapolation_limit
) {}
79 class PictureLayerTilingIteratorTest
: public testing::Test
{
81 PictureLayerTilingIteratorTest() {}
82 ~PictureLayerTilingIteratorTest() override
{}
84 void Initialize(const gfx::Size
& tile_size
,
86 const gfx::Size
& layer_bounds
) {
87 client_
.SetTileSize(tile_size
);
88 scoped_refptr
<FakePicturePileImpl
> pile
=
89 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds
);
90 tiling_
= TestablePictureLayerTiling::Create(
91 PENDING_TREE
, contents_scale
, pile
, &client_
, LayerTreeSettings());
92 tiling_
->set_resolution(HIGH_RESOLUTION
);
95 void InitializeActive(const gfx::Size
& tile_size
,
97 const gfx::Size
& layer_bounds
) {
98 client_
.SetTileSize(tile_size
);
99 scoped_refptr
<FakePicturePileImpl
> pile
=
100 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds
);
101 tiling_
= TestablePictureLayerTiling::Create(
102 ACTIVE_TREE
, contents_scale
, pile
, &client_
, LayerTreeSettings());
103 tiling_
->set_resolution(HIGH_RESOLUTION
);
106 void SetLiveRectAndVerifyTiles(const gfx::Rect
& live_tiles_rect
) {
107 tiling_
->SetLiveTilesRect(live_tiles_rect
);
109 std::vector
<Tile
*> tiles
= tiling_
->AllTilesForTesting();
110 for (std::vector
<Tile
*>::iterator iter
= tiles
.begin();
113 EXPECT_TRUE(live_tiles_rect
.Intersects((*iter
)->content_rect()));
117 void VerifyTilesExactlyCoverRect(
119 const gfx::Rect
& request_rect
,
120 const gfx::Rect
& expect_rect
) {
121 EXPECT_TRUE(request_rect
.Contains(expect_rect
));
123 // Iterators are not valid if this ratio is too large (i.e. the
124 // tiling is too high-res for a low-res destination rect.) This is an
125 // artifact of snapping geometry to integer coordinates and then mapping
126 // back to floating point texture coordinates.
127 float dest_to_contents_scale
= tiling_
->contents_scale() / rect_scale
;
128 ASSERT_LE(dest_to_contents_scale
, 2.0);
130 Region remaining
= expect_rect
;
131 for (PictureLayerTiling::CoverageIterator
132 iter(tiling_
.get(), rect_scale
, request_rect
);
135 // Geometry cannot overlap previous geometry at all
136 gfx::Rect geometry
= iter
.geometry_rect();
137 EXPECT_TRUE(expect_rect
.Contains(geometry
));
138 EXPECT_TRUE(remaining
.Contains(geometry
));
139 remaining
.Subtract(geometry
);
141 // Sanity check that texture coords are within the texture rect.
142 gfx::RectF texture_rect
= iter
.texture_rect();
143 EXPECT_GE(texture_rect
.x(), 0);
144 EXPECT_GE(texture_rect
.y(), 0);
145 EXPECT_LE(texture_rect
.right(), client_
.TileSize().width());
146 EXPECT_LE(texture_rect
.bottom(), client_
.TileSize().height());
149 // The entire rect must be filled by geometry from the tiling.
150 EXPECT_TRUE(remaining
.IsEmpty());
153 void VerifyTilesExactlyCoverRect(float rect_scale
, const gfx::Rect
& rect
) {
154 VerifyTilesExactlyCoverRect(rect_scale
, rect
, rect
);
159 const gfx::Rect
& rect
,
160 base::Callback
<void(Tile
* tile
,
161 const gfx::Rect
& geometry_rect
)> callback
) {
162 VerifyTiles(tiling_
.get(),
169 PictureLayerTiling
* tiling
,
171 const gfx::Rect
& rect
,
172 base::Callback
<void(Tile
* tile
,
173 const gfx::Rect
& geometry_rect
)> callback
) {
174 Region remaining
= rect
;
175 for (PictureLayerTiling::CoverageIterator
iter(tiling
, rect_scale
, rect
);
178 remaining
.Subtract(iter
.geometry_rect());
179 callback
.Run(*iter
, iter
.geometry_rect());
181 EXPECT_TRUE(remaining
.IsEmpty());
184 void VerifyTilesCoverNonContainedRect(float rect_scale
,
185 const gfx::Rect
& dest_rect
) {
186 float dest_to_contents_scale
= tiling_
->contents_scale() / rect_scale
;
187 gfx::Rect clamped_rect
= gfx::ScaleToEnclosingRect(
188 gfx::Rect(tiling_
->tiling_size()), 1.f
/ dest_to_contents_scale
);
189 clamped_rect
.Intersect(dest_rect
);
190 VerifyTilesExactlyCoverRect(rect_scale
, dest_rect
, clamped_rect
);
194 FakePictureLayerTilingClient client_
;
195 scoped_ptr
<TestablePictureLayerTiling
> tiling_
;
198 DISALLOW_COPY_AND_ASSIGN(PictureLayerTilingIteratorTest
);
201 TEST_F(PictureLayerTilingIteratorTest
, ResizeDeletesTiles
) {
202 // Verifies that a resize with invalidation for newly exposed pixels will
203 // deletes tiles that intersect that invalidation.
204 gfx::Size
tile_size(100, 100);
205 gfx::Size
original_layer_size(10, 10);
206 InitializeActive(tile_size
, 1.f
, original_layer_size
);
207 SetLiveRectAndVerifyTiles(gfx::Rect(original_layer_size
));
209 // Tiling only has one tile, since its total size is less than one.
210 EXPECT_TRUE(tiling_
->TileAt(0, 0));
212 // Stop creating tiles so that any invalidations are left as holes.
213 gfx::Size
new_layer_size(200, 200);
214 scoped_refptr
<FakePicturePileImpl
> pile
=
215 FakePicturePileImpl::CreateEmptyPileWithDefaultTileSize(new_layer_size
);
217 Region invalidation
=
218 SubtractRegions(gfx::Rect(tile_size
), gfx::Rect(original_layer_size
));
219 tiling_
->SetRasterSourceAndResize(pile
);
220 EXPECT_TRUE(tiling_
->TileAt(0, 0));
221 tiling_
->Invalidate(invalidation
);
222 EXPECT_FALSE(tiling_
->TileAt(0, 0));
225 TEST_F(PictureLayerTilingIteratorTest
, CreateMissingTilesStaysInsideLiveRect
) {
226 // The tiling has three rows and columns.
227 Initialize(gfx::Size(100, 100), 1.f
, gfx::Size(250, 250));
228 EXPECT_EQ(3, tiling_
->TilingDataForTesting().num_tiles_x());
229 EXPECT_EQ(3, tiling_
->TilingDataForTesting().num_tiles_y());
231 // The live tiles rect is at the very edge of the right-most and
232 // bottom-most tiles. Their border pixels would still be inside the live
233 // tiles rect, but the tiles should not exist just for that.
234 int right
= tiling_
->TilingDataForTesting().TileBounds(2, 2).x();
235 int bottom
= tiling_
->TilingDataForTesting().TileBounds(2, 2).y();
237 SetLiveRectAndVerifyTiles(gfx::Rect(right
, bottom
));
238 EXPECT_FALSE(tiling_
->TileAt(2, 0));
239 EXPECT_FALSE(tiling_
->TileAt(2, 1));
240 EXPECT_FALSE(tiling_
->TileAt(2, 2));
241 EXPECT_FALSE(tiling_
->TileAt(1, 2));
242 EXPECT_FALSE(tiling_
->TileAt(0, 2));
244 // Verify CreateMissingTilesInLiveTilesRect respects this.
245 tiling_
->CreateMissingTilesInLiveTilesRect();
246 EXPECT_FALSE(tiling_
->TileAt(2, 0));
247 EXPECT_FALSE(tiling_
->TileAt(2, 1));
248 EXPECT_FALSE(tiling_
->TileAt(2, 2));
249 EXPECT_FALSE(tiling_
->TileAt(1, 2));
250 EXPECT_FALSE(tiling_
->TileAt(0, 2));
253 TEST_F(PictureLayerTilingIteratorTest
, ResizeTilingOverTileBorders
) {
254 // The tiling has four rows and three columns.
255 Initialize(gfx::Size(100, 100), 1.f
, gfx::Size(250, 350));
256 EXPECT_EQ(3, tiling_
->TilingDataForTesting().num_tiles_x());
257 EXPECT_EQ(4, tiling_
->TilingDataForTesting().num_tiles_y());
259 // The live tiles rect covers the whole tiling.
260 SetLiveRectAndVerifyTiles(gfx::Rect(250, 350));
262 // Tiles in the bottom row and right column exist.
263 EXPECT_TRUE(tiling_
->TileAt(2, 0));
264 EXPECT_TRUE(tiling_
->TileAt(2, 1));
265 EXPECT_TRUE(tiling_
->TileAt(2, 2));
266 EXPECT_TRUE(tiling_
->TileAt(2, 3));
267 EXPECT_TRUE(tiling_
->TileAt(1, 3));
268 EXPECT_TRUE(tiling_
->TileAt(0, 3));
270 int right
= tiling_
->TilingDataForTesting().TileBounds(2, 2).x();
271 int bottom
= tiling_
->TilingDataForTesting().TileBounds(2, 3).y();
273 // Shrink the tiling so that the last tile row/column is entirely in the
274 // border pixels of the interior tiles. That row/column is removed.
275 scoped_refptr
<FakePicturePileImpl
> pile
=
276 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
277 gfx::Size(right
+ 1, bottom
+ 1));
278 tiling_
->SetRasterSourceAndResize(pile
);
279 EXPECT_EQ(2, tiling_
->TilingDataForTesting().num_tiles_x());
280 EXPECT_EQ(3, tiling_
->TilingDataForTesting().num_tiles_y());
282 // The live tiles rect was clamped to the pile size.
283 EXPECT_EQ(gfx::Rect(right
+ 1, bottom
+ 1), tiling_
->live_tiles_rect());
285 // Since the row/column is gone, the tiles should be gone too.
286 EXPECT_FALSE(tiling_
->TileAt(2, 0));
287 EXPECT_FALSE(tiling_
->TileAt(2, 1));
288 EXPECT_FALSE(tiling_
->TileAt(2, 2));
289 EXPECT_FALSE(tiling_
->TileAt(2, 3));
290 EXPECT_FALSE(tiling_
->TileAt(1, 3));
291 EXPECT_FALSE(tiling_
->TileAt(0, 3));
293 // Growing outside the current right/bottom tiles border pixels should create
294 // the tiles again, even though the live rect has not changed size.
295 pile
= FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
296 gfx::Size(right
+ 2, bottom
+ 2));
297 tiling_
->SetRasterSourceAndResize(pile
);
298 EXPECT_EQ(3, tiling_
->TilingDataForTesting().num_tiles_x());
299 EXPECT_EQ(4, tiling_
->TilingDataForTesting().num_tiles_y());
302 EXPECT_EQ(gfx::Rect(right
+ 1, bottom
+ 1), tiling_
->live_tiles_rect());
304 // The last row/column tiles are inside the live tiles rect.
305 EXPECT_TRUE(gfx::Rect(right
+ 1, bottom
+ 1).Intersects(
306 tiling_
->TilingDataForTesting().TileBounds(2, 0)));
307 EXPECT_TRUE(gfx::Rect(right
+ 1, bottom
+ 1).Intersects(
308 tiling_
->TilingDataForTesting().TileBounds(0, 3)));
310 EXPECT_TRUE(tiling_
->TileAt(2, 0));
311 EXPECT_TRUE(tiling_
->TileAt(2, 1));
312 EXPECT_TRUE(tiling_
->TileAt(2, 2));
313 EXPECT_TRUE(tiling_
->TileAt(2, 3));
314 EXPECT_TRUE(tiling_
->TileAt(1, 3));
315 EXPECT_TRUE(tiling_
->TileAt(0, 3));
318 TEST_F(PictureLayerTilingIteratorTest
, ResizeLiveTileRectOverTileBorders
) {
319 // The tiling has three rows and columns.
320 Initialize(gfx::Size(100, 100), 1.f
, gfx::Size(250, 350));
321 EXPECT_EQ(3, tiling_
->TilingDataForTesting().num_tiles_x());
322 EXPECT_EQ(4, tiling_
->TilingDataForTesting().num_tiles_y());
324 // The live tiles rect covers the whole tiling.
325 SetLiveRectAndVerifyTiles(gfx::Rect(250, 350));
327 // Tiles in the bottom row and right column exist.
328 EXPECT_TRUE(tiling_
->TileAt(2, 0));
329 EXPECT_TRUE(tiling_
->TileAt(2, 1));
330 EXPECT_TRUE(tiling_
->TileAt(2, 2));
331 EXPECT_TRUE(tiling_
->TileAt(2, 3));
332 EXPECT_TRUE(tiling_
->TileAt(1, 3));
333 EXPECT_TRUE(tiling_
->TileAt(0, 3));
335 // Shrink the live tiles rect to the very edge of the right-most and
336 // bottom-most tiles. Their border pixels would still be inside the live
337 // tiles rect, but the tiles should not exist just for that.
338 int right
= tiling_
->TilingDataForTesting().TileBounds(2, 3).x();
339 int bottom
= tiling_
->TilingDataForTesting().TileBounds(2, 3).y();
341 SetLiveRectAndVerifyTiles(gfx::Rect(right
, bottom
));
342 EXPECT_FALSE(tiling_
->TileAt(2, 0));
343 EXPECT_FALSE(tiling_
->TileAt(2, 1));
344 EXPECT_FALSE(tiling_
->TileAt(2, 2));
345 EXPECT_FALSE(tiling_
->TileAt(2, 3));
346 EXPECT_FALSE(tiling_
->TileAt(1, 3));
347 EXPECT_FALSE(tiling_
->TileAt(0, 3));
349 // Including the bottom row and right column again, should create the tiles.
350 SetLiveRectAndVerifyTiles(gfx::Rect(right
+ 1, bottom
+ 1));
351 EXPECT_TRUE(tiling_
->TileAt(2, 0));
352 EXPECT_TRUE(tiling_
->TileAt(2, 1));
353 EXPECT_TRUE(tiling_
->TileAt(2, 2));
354 EXPECT_TRUE(tiling_
->TileAt(2, 3));
355 EXPECT_TRUE(tiling_
->TileAt(1, 2));
356 EXPECT_TRUE(tiling_
->TileAt(0, 2));
358 // Shrink the live tiles rect to the very edge of the left-most and
359 // top-most tiles. Their border pixels would still be inside the live
360 // tiles rect, but the tiles should not exist just for that.
361 int left
= tiling_
->TilingDataForTesting().TileBounds(0, 0).right();
362 int top
= tiling_
->TilingDataForTesting().TileBounds(0, 0).bottom();
364 SetLiveRectAndVerifyTiles(gfx::Rect(left
, top
, 250 - left
, 350 - top
));
365 EXPECT_FALSE(tiling_
->TileAt(0, 3));
366 EXPECT_FALSE(tiling_
->TileAt(0, 2));
367 EXPECT_FALSE(tiling_
->TileAt(0, 1));
368 EXPECT_FALSE(tiling_
->TileAt(0, 0));
369 EXPECT_FALSE(tiling_
->TileAt(1, 0));
370 EXPECT_FALSE(tiling_
->TileAt(2, 0));
372 // Including the top row and left column again, should create the tiles.
373 SetLiveRectAndVerifyTiles(
374 gfx::Rect(left
- 1, top
- 1, 250 - left
, 350 - top
));
375 EXPECT_TRUE(tiling_
->TileAt(0, 3));
376 EXPECT_TRUE(tiling_
->TileAt(0, 2));
377 EXPECT_TRUE(tiling_
->TileAt(0, 1));
378 EXPECT_TRUE(tiling_
->TileAt(0, 0));
379 EXPECT_TRUE(tiling_
->TileAt(1, 0));
380 EXPECT_TRUE(tiling_
->TileAt(2, 0));
383 TEST_F(PictureLayerTilingIteratorTest
, ResizeLiveTileRectOverSameTiles
) {
384 // The tiling has four rows and three columns.
385 Initialize(gfx::Size(100, 100), 1.f
, gfx::Size(250, 350));
386 EXPECT_EQ(3, tiling_
->TilingDataForTesting().num_tiles_x());
387 EXPECT_EQ(4, tiling_
->TilingDataForTesting().num_tiles_y());
389 // The live tiles rect covers the whole tiling.
390 SetLiveRectAndVerifyTiles(gfx::Rect(250, 350));
393 for (int i
= 0; i
< 3; ++i
) {
394 for (int j
= 0; j
< 4; ++j
)
395 EXPECT_TRUE(tiling_
->TileAt(i
, j
)) << i
<< "," << j
;
398 // Shrink the live tiles rect, but still cover all the tiles.
399 SetLiveRectAndVerifyTiles(gfx::Rect(1, 1, 249, 349));
401 // All tiles still exist.
402 for (int i
= 0; i
< 3; ++i
) {
403 for (int j
= 0; j
< 4; ++j
)
404 EXPECT_TRUE(tiling_
->TileAt(i
, j
)) << i
<< "," << j
;
407 // Grow the live tiles rect, but still cover all the same tiles.
408 SetLiveRectAndVerifyTiles(gfx::Rect(0, 0, 250, 350));
410 // All tiles still exist.
411 for (int i
= 0; i
< 3; ++i
) {
412 for (int j
= 0; j
< 4; ++j
)
413 EXPECT_TRUE(tiling_
->TileAt(i
, j
)) << i
<< "," << j
;
417 TEST_F(PictureLayerTilingIteratorTest
, ResizeOverBorderPixelsDeletesTiles
) {
418 // Verifies that a resize with invalidation for newly exposed pixels will
419 // deletes tiles that intersect that invalidation.
420 gfx::Size
tile_size(100, 100);
421 gfx::Size
original_layer_size(99, 99);
422 InitializeActive(tile_size
, 1.f
, original_layer_size
);
423 SetLiveRectAndVerifyTiles(gfx::Rect(original_layer_size
));
425 // Tiling only has one tile, since its total size is less than one.
426 EXPECT_TRUE(tiling_
->TileAt(0, 0));
428 // Stop creating tiles so that any invalidations are left as holes.
429 scoped_refptr
<FakePicturePileImpl
> pile
=
430 FakePicturePileImpl::CreateEmptyPileWithDefaultTileSize(
431 gfx::Size(200, 200));
432 tiling_
->SetRasterSourceAndResize(pile
);
434 Region invalidation
=
435 SubtractRegions(gfx::Rect(tile_size
), gfx::Rect(original_layer_size
));
436 EXPECT_TRUE(tiling_
->TileAt(0, 0));
437 tiling_
->Invalidate(invalidation
);
438 EXPECT_FALSE(tiling_
->TileAt(0, 0));
440 // The original tile was the same size after resize, but it would include new
442 EXPECT_EQ(gfx::Rect(original_layer_size
),
443 tiling_
->TilingDataForTesting().TileBounds(0, 0));
446 TEST_F(PictureLayerTilingIteratorTest
, LiveTilesExactlyCoverLiveTileRect
) {
447 Initialize(gfx::Size(100, 100), 1.f
, gfx::Size(1099, 801));
448 SetLiveRectAndVerifyTiles(gfx::Rect(100, 100));
449 SetLiveRectAndVerifyTiles(gfx::Rect(101, 99));
450 SetLiveRectAndVerifyTiles(gfx::Rect(1099, 1));
451 SetLiveRectAndVerifyTiles(gfx::Rect(1, 801));
452 SetLiveRectAndVerifyTiles(gfx::Rect(1099, 1));
453 SetLiveRectAndVerifyTiles(gfx::Rect(201, 800));
456 TEST_F(PictureLayerTilingIteratorTest
, IteratorCoversLayerBoundsNoScale
) {
457 Initialize(gfx::Size(100, 100), 1.f
, gfx::Size(1099, 801));
458 VerifyTilesExactlyCoverRect(1, gfx::Rect());
459 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1099, 801));
460 VerifyTilesExactlyCoverRect(1, gfx::Rect(52, 83, 789, 412));
462 // With borders, a size of 3x3 = 1 pixel of content.
463 Initialize(gfx::Size(3, 3), 1.f
, gfx::Size(10, 10));
464 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
465 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
466 VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
467 VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
470 TEST_F(PictureLayerTilingIteratorTest
, IteratorCoversLayerBoundsTilingScale
) {
471 Initialize(gfx::Size(200, 100), 2.0f
, gfx::Size(1005, 2010));
472 VerifyTilesExactlyCoverRect(1, gfx::Rect());
473 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
474 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
476 Initialize(gfx::Size(3, 3), 2.0f
, gfx::Size(10, 10));
477 VerifyTilesExactlyCoverRect(1, gfx::Rect());
478 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
479 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
480 VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
481 VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
483 Initialize(gfx::Size(100, 200), 0.5f
, gfx::Size(1005, 2010));
484 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
485 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
487 Initialize(gfx::Size(150, 250), 0.37f
, gfx::Size(1005, 2010));
488 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
489 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
491 Initialize(gfx::Size(312, 123), 0.01f
, gfx::Size(1005, 2010));
492 VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
493 VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
496 TEST_F(PictureLayerTilingIteratorTest
, IteratorCoversLayerBoundsBothScale
) {
497 Initialize(gfx::Size(50, 50), 4.0f
, gfx::Size(800, 600));
498 VerifyTilesExactlyCoverRect(2.0f
, gfx::Rect());
499 VerifyTilesExactlyCoverRect(2.0f
, gfx::Rect(0, 0, 1600, 1200));
500 VerifyTilesExactlyCoverRect(2.0f
, gfx::Rect(512, 365, 253, 182));
503 gfx::Size
bounds(800, 600);
504 gfx::Rect
full_rect(gfx::ToCeiledSize(gfx::ScaleSize(bounds
, scale
)));
505 Initialize(gfx::Size(256, 512), 5.2f
, bounds
);
506 VerifyTilesExactlyCoverRect(scale
, full_rect
);
507 VerifyTilesExactlyCoverRect(scale
, gfx::Rect(2014, 1579, 867, 1033));
510 TEST_F(PictureLayerTilingIteratorTest
, IteratorEmptyRect
) {
511 Initialize(gfx::Size(100, 100), 1.0f
, gfx::Size(800, 600));
514 PictureLayerTiling::CoverageIterator
iter(tiling_
.get(), 1.0f
, empty
);
518 TEST_F(PictureLayerTilingIteratorTest
, NonIntersectingRect
) {
519 Initialize(gfx::Size(100, 100), 1.0f
, gfx::Size(800, 600));
520 gfx::Rect
non_intersecting(1000, 1000, 50, 50);
521 PictureLayerTiling::CoverageIterator
iter(tiling_
.get(), 1, non_intersecting
);
525 TEST_F(PictureLayerTilingIteratorTest
, LayerEdgeTextureCoordinates
) {
526 Initialize(gfx::Size(300, 300), 1.0f
, gfx::Size(256, 256));
527 // All of these sizes are 256x256, scaled and ceiled.
528 VerifyTilesExactlyCoverRect(1.0f
, gfx::Rect(0, 0, 256, 256));
529 VerifyTilesExactlyCoverRect(0.8f
, gfx::Rect(0, 0, 205, 205));
530 VerifyTilesExactlyCoverRect(1.2f
, gfx::Rect(0, 0, 308, 308));
533 TEST_F(PictureLayerTilingIteratorTest
, NonContainedDestRect
) {
534 Initialize(gfx::Size(100, 100), 1.0f
, gfx::Size(400, 400));
536 // Too large in all dimensions
537 VerifyTilesCoverNonContainedRect(1.0f
, gfx::Rect(-1000, -1000, 2000, 2000));
538 VerifyTilesCoverNonContainedRect(1.5f
, gfx::Rect(-1000, -1000, 2000, 2000));
539 VerifyTilesCoverNonContainedRect(0.5f
, gfx::Rect(-1000, -1000, 2000, 2000));
541 // Partially covering content, but too large
542 VerifyTilesCoverNonContainedRect(1.0f
, gfx::Rect(-1000, 100, 2000, 100));
543 VerifyTilesCoverNonContainedRect(1.5f
, gfx::Rect(-1000, 100, 2000, 100));
544 VerifyTilesCoverNonContainedRect(0.5f
, gfx::Rect(-1000, 100, 2000, 100));
547 TEST(PictureLayerTilingTest
, SkewportLimits
) {
548 FakePictureLayerTilingClient client
;
550 gfx::Rect
viewport(0, 0, 100, 100);
551 gfx::Size
layer_bounds(200, 200);
553 client
.SetTileSize(gfx::Size(100, 100));
554 LayerTreeSettings settings
;
555 settings
.skewport_extrapolation_limit_in_content_pixels
= 75;
557 scoped_refptr
<FakePicturePileImpl
> pile
=
558 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds
);
559 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
560 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
563 tiling
->ComputeTilePriorityRects(viewport
, 1.f
, 1.0, Occlusion());
565 // Move viewport down 50 pixels in 0.5 seconds.
566 gfx::Rect down_skewport
=
567 tiling
->ComputeSkewport(1.5, gfx::Rect(0, 50, 100, 100));
569 EXPECT_EQ(0, down_skewport
.x());
570 EXPECT_EQ(50, down_skewport
.y());
571 EXPECT_EQ(100, down_skewport
.width());
572 EXPECT_EQ(175, down_skewport
.height());
573 EXPECT_TRUE(down_skewport
.Contains(gfx::Rect(0, 50, 100, 100)));
575 // Move viewport down 50 and right 10 pixels.
576 gfx::Rect down_right_skewport
=
577 tiling
->ComputeSkewport(1.5, gfx::Rect(10, 50, 100, 100));
579 EXPECT_EQ(10, down_right_skewport
.x());
580 EXPECT_EQ(50, down_right_skewport
.y());
581 EXPECT_EQ(120, down_right_skewport
.width());
582 EXPECT_EQ(175, down_right_skewport
.height());
583 EXPECT_TRUE(down_right_skewport
.Contains(gfx::Rect(10, 50, 100, 100)));
585 // Move viewport left.
586 gfx::Rect left_skewport
=
587 tiling
->ComputeSkewport(1.5, gfx::Rect(-50, 0, 100, 100));
589 EXPECT_EQ(-125, left_skewport
.x());
590 EXPECT_EQ(0, left_skewport
.y());
591 EXPECT_EQ(175, left_skewport
.width());
592 EXPECT_EQ(100, left_skewport
.height());
593 EXPECT_TRUE(left_skewport
.Contains(gfx::Rect(-50, 0, 100, 100)));
596 gfx::Rect expand_skewport
=
597 tiling
->ComputeSkewport(1.5, gfx::Rect(-50, -50, 200, 200));
599 // x and y moved by -75 (-50 - 75 = -125).
600 // right side and bottom side moved by 75 [(350 - 125) - (200 - 50) = 75].
601 EXPECT_EQ(-125, expand_skewport
.x());
602 EXPECT_EQ(-125, expand_skewport
.y());
603 EXPECT_EQ(350, expand_skewport
.width());
604 EXPECT_EQ(350, expand_skewport
.height());
605 EXPECT_TRUE(expand_skewport
.Contains(gfx::Rect(-50, -50, 200, 200)));
607 // Expand the viewport past the limit in all directions.
608 gfx::Rect big_expand_skewport
=
609 tiling
->ComputeSkewport(1.5, gfx::Rect(-500, -500, 1500, 1500));
611 EXPECT_EQ(-575, big_expand_skewport
.x());
612 EXPECT_EQ(-575, big_expand_skewport
.y());
613 EXPECT_EQ(1650, big_expand_skewport
.width());
614 EXPECT_EQ(1650, big_expand_skewport
.height());
615 EXPECT_TRUE(big_expand_skewport
.Contains(gfx::Rect(-500, -500, 1500, 1500)));
617 // Shrink the skewport in all directions.
618 gfx::Rect shrink_viewport
=
619 tiling
->ComputeSkewport(1.5, gfx::Rect(0, 0, 100, 100));
620 EXPECT_EQ(0, shrink_viewport
.x());
621 EXPECT_EQ(0, shrink_viewport
.y());
622 EXPECT_EQ(100, shrink_viewport
.width());
623 EXPECT_EQ(100, shrink_viewport
.height());
625 // Move the skewport really far in one direction.
626 gfx::Rect move_skewport_far
=
627 tiling
->ComputeSkewport(1.5, gfx::Rect(0, 5000, 100, 100));
628 EXPECT_EQ(0, move_skewport_far
.x());
629 EXPECT_EQ(5000, move_skewport_far
.y());
630 EXPECT_EQ(100, move_skewport_far
.width());
631 EXPECT_EQ(175, move_skewport_far
.height());
632 EXPECT_TRUE(move_skewport_far
.Contains(gfx::Rect(0, 5000, 100, 100)));
635 TEST(PictureLayerTilingTest
, ComputeSkewportExtremeCases
) {
636 FakePictureLayerTilingClient client
;
638 gfx::Size
layer_bounds(200, 200);
639 client
.SetTileSize(gfx::Size(100, 100));
640 LayerTreeSettings settings
;
641 scoped_refptr
<FakePicturePileImpl
> pile
=
642 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds
);
643 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
644 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
647 gfx::Rect
viewport1(-1918, 255860, 4010, 2356);
648 gfx::Rect
viewport2(-7088, -91738, 14212, 8350);
649 gfx::Rect
viewport3(-12730024, -158883296, 24607540, 14454512);
651 tiling
->ComputeTilePriorityRects(viewport1
, 1.f
, time
, Occlusion());
653 EXPECT_TRUE(tiling
->ComputeSkewport(time
, viewport2
).Contains(viewport2
));
654 tiling
->ComputeTilePriorityRects(viewport2
, 1.f
, time
, Occlusion());
656 EXPECT_TRUE(tiling
->ComputeSkewport(time
, viewport3
).Contains(viewport3
));
659 TEST(PictureLayerTilingTest
, ComputeSkewport
) {
660 FakePictureLayerTilingClient client
;
662 gfx::Rect
viewport(0, 0, 100, 100);
663 gfx::Size
layer_bounds(200, 200);
665 client
.SetTileSize(gfx::Size(100, 100));
667 scoped_refptr
<FakePicturePileImpl
> pile
=
668 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds
);
669 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
670 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
671 LayerTreeSettings());
673 tiling
->ComputeTilePriorityRects(viewport
, 1.f
, 1.0, Occlusion());
675 // Move viewport down 50 pixels in 0.5 seconds.
676 gfx::Rect down_skewport
=
677 tiling
->ComputeSkewport(1.5, gfx::Rect(0, 50, 100, 100));
679 EXPECT_EQ(0, down_skewport
.x());
680 EXPECT_EQ(50, down_skewport
.y());
681 EXPECT_EQ(100, down_skewport
.width());
682 EXPECT_EQ(200, down_skewport
.height());
685 gfx::Rect shrink_skewport
=
686 tiling
->ComputeSkewport(1.5, gfx::Rect(25, 25, 50, 50));
688 EXPECT_EQ(25, shrink_skewport
.x());
689 EXPECT_EQ(25, shrink_skewport
.y());
690 EXPECT_EQ(50, shrink_skewport
.width());
691 EXPECT_EQ(50, shrink_skewport
.height());
693 // Move viewport down 50 and right 10 pixels.
694 gfx::Rect down_right_skewport
=
695 tiling
->ComputeSkewport(1.5, gfx::Rect(10, 50, 100, 100));
697 EXPECT_EQ(10, down_right_skewport
.x());
698 EXPECT_EQ(50, down_right_skewport
.y());
699 EXPECT_EQ(120, down_right_skewport
.width());
700 EXPECT_EQ(200, down_right_skewport
.height());
702 // Move viewport left.
703 gfx::Rect left_skewport
=
704 tiling
->ComputeSkewport(1.5, gfx::Rect(-20, 0, 100, 100));
706 EXPECT_EQ(-60, left_skewport
.x());
707 EXPECT_EQ(0, left_skewport
.y());
708 EXPECT_EQ(140, left_skewport
.width());
709 EXPECT_EQ(100, left_skewport
.height());
711 // Expand viewport in 0.2 seconds.
712 gfx::Rect expanded_skewport
=
713 tiling
->ComputeSkewport(1.2, gfx::Rect(-5, -5, 110, 110));
715 EXPECT_EQ(-30, expanded_skewport
.x());
716 EXPECT_EQ(-30, expanded_skewport
.y());
717 EXPECT_EQ(160, expanded_skewport
.width());
718 EXPECT_EQ(160, expanded_skewport
.height());
721 TEST(PictureLayerTilingTest
, SkewportThroughUpdateTilePriorities
) {
722 FakePictureLayerTilingClient client
;
724 gfx::Rect
viewport(0, 0, 100, 100);
725 gfx::Size
layer_bounds(200, 200);
727 client
.SetTileSize(gfx::Size(100, 100));
729 scoped_refptr
<FakePicturePileImpl
> pile
=
730 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds
);
731 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
732 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
733 LayerTreeSettings());
735 tiling
->ComputeTilePriorityRects(viewport
, 1.f
, 1.0, Occlusion());
737 // Move viewport down 50 pixels in 0.5 seconds.
738 gfx::Rect viewport_50
= gfx::Rect(0, 50, 100, 100);
739 gfx::Rect skewport_50
= tiling
->ComputeSkewport(1.5, viewport_50
);
741 EXPECT_EQ(gfx::Rect(0, 50, 100, 200), skewport_50
);
742 tiling
->ComputeTilePriorityRects(viewport_50
, 1.f
, 1.5, Occlusion());
744 gfx::Rect viewport_100
= gfx::Rect(0, 100, 100, 100);
745 gfx::Rect skewport_100
= tiling
->ComputeSkewport(2.0, viewport_100
);
747 EXPECT_EQ(gfx::Rect(0, 100, 100, 200), skewport_100
);
748 tiling
->ComputeTilePriorityRects(viewport_100
, 1.f
, 2.0, Occlusion());
750 // Advance time, but not the viewport.
751 gfx::Rect result
= tiling
->ComputeSkewport(2.5, viewport_100
);
752 // Since the history did advance, we should still get a skewport but a smaller
754 EXPECT_EQ(gfx::Rect(0, 100, 100, 150), result
);
755 tiling
->ComputeTilePriorityRects(viewport_100
, 1.f
, 2.5, Occlusion());
757 // Advance time again.
758 result
= tiling
->ComputeSkewport(3.0, viewport_100
);
759 EXPECT_EQ(viewport_100
, result
);
760 tiling
->ComputeTilePriorityRects(viewport_100
, 1.f
, 3.0, Occlusion());
762 // Ensure we have a skewport.
763 gfx::Rect viewport_150
= gfx::Rect(0, 150, 100, 100);
764 gfx::Rect skewport_150
= tiling
->ComputeSkewport(3.5, viewport_150
);
765 EXPECT_EQ(gfx::Rect(0, 150, 100, 150), skewport_150
);
766 tiling
->ComputeTilePriorityRects(viewport_150
, 1.f
, 3.5, Occlusion());
768 // Advance the viewport, but not the time.
769 gfx::Rect viewport_200
= gfx::Rect(0, 200, 100, 100);
770 gfx::Rect skewport_200
= tiling
->ComputeSkewport(3.5, viewport_200
);
771 EXPECT_EQ(gfx::Rect(0, 200, 100, 300), skewport_200
);
773 // Ensure that continued calls with the same value, produce the same skewport.
774 tiling
->ComputeTilePriorityRects(viewport_150
, 1.f
, 3.5, Occlusion());
775 EXPECT_EQ(gfx::Rect(0, 200, 100, 300), skewport_200
);
776 tiling
->ComputeTilePriorityRects(viewport_150
, 1.f
, 3.5, Occlusion());
777 EXPECT_EQ(gfx::Rect(0, 200, 100, 300), skewport_200
);
779 tiling
->ComputeTilePriorityRects(viewport_200
, 1.f
, 3.5, Occlusion());
781 // This should never happen, but advance the viewport yet again keeping the
783 gfx::Rect viewport_250
= gfx::Rect(0, 250, 100, 100);
784 gfx::Rect skewport_250
= tiling
->ComputeSkewport(3.5, viewport_250
);
785 EXPECT_EQ(viewport_250
, skewport_250
);
786 tiling
->ComputeTilePriorityRects(viewport_250
, 1.f
, 3.5, Occlusion());
789 TEST(PictureLayerTilingTest
, ViewportDistanceWithScale
) {
790 FakePictureLayerTilingClient client
;
792 gfx::Rect
viewport(0, 0, 100, 100);
793 gfx::Size
layer_bounds(1500, 1500);
795 client
.SetTileSize(gfx::Size(10, 10));
796 LayerTreeSettings settings
;
798 // Tiling at 0.25 scale: this should create 47x47 tiles of size 10x10.
799 // The reason is that each tile has a one pixel border, so tile at (1, 2)
800 // for instance begins at (8, 16) pixels. So tile at (46, 46) will begin at
801 // (368, 368) and extend to the end of 1500 * 0.25 = 375 edge of the
803 scoped_refptr
<FakePicturePileImpl
> pile
=
804 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds
);
805 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
806 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 0.25f
, pile
, &client
,
808 tiling
->set_resolution(HIGH_RESOLUTION
);
809 gfx::Rect viewport_in_content_space
=
810 gfx::ScaleToEnclosedRect(viewport
, 0.25f
);
812 tiling
->ComputeTilePriorityRects(viewport
, 1.f
, 1.0, Occlusion());
813 auto prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
815 // Compute the soon border.
816 float inset
= PictureLayerTiling::CalculateSoonBorderDistance(
817 viewport_in_content_space
, 1.0f
/ 0.25f
);
818 gfx::Rect soon_rect_in_content_space
= viewport_in_content_space
;
819 soon_rect_in_content_space
.Inset(-inset
, -inset
);
822 for (int i
= 0; i
< 47; ++i
) {
823 for (int j
= 0; j
< 47; ++j
) {
824 EXPECT_TRUE(tiling
->TileAt(i
, j
)) << "i: " << i
<< " j: " << j
;
827 for (int i
= 0; i
< 47; ++i
) {
828 EXPECT_FALSE(tiling
->TileAt(i
, 47)) << "i: " << i
;
829 EXPECT_FALSE(tiling
->TileAt(47, i
)) << "i: " << i
;
832 // No movement in the viewport implies that tiles will either be NOW
833 // or EVENTUALLY, with the exception of tiles that are between 0 and 312
834 // pixels away from the viewport, which will be in the SOON bin.
835 bool have_now
= false;
836 bool have_eventually
= false;
837 bool have_soon
= false;
838 for (int i
= 0; i
< 47; ++i
) {
839 for (int j
= 0; j
< 47; ++j
) {
840 Tile
* tile
= tiling
->TileAt(i
, j
);
841 PrioritizedTile prioritized_tile
= prioritized_tiles
[tile
];
842 TilePriority priority
= prioritized_tile
.priority();
844 gfx::Rect tile_rect
= tiling
->TilingDataForTesting().TileBounds(i
, j
);
845 if (viewport_in_content_space
.Intersects(tile_rect
)) {
846 EXPECT_EQ(TilePriority::NOW
, priority
.priority_bin
);
847 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
849 } else if (soon_rect_in_content_space
.Intersects(tile_rect
)) {
850 EXPECT_EQ(TilePriority::SOON
, priority
.priority_bin
);
853 EXPECT_EQ(TilePriority::EVENTUALLY
, priority
.priority_bin
);
854 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
855 have_eventually
= true;
860 EXPECT_TRUE(have_now
);
861 EXPECT_TRUE(have_soon
);
862 EXPECT_TRUE(have_eventually
);
864 // Spot check some distances.
865 // Tile at 5, 1 should begin at 41x9 in content space (without borders),
866 // so the distance to a viewport that ends at 25x25 in content space
867 // should be 17 (41 - 25 + 1). In layer space, then that should be
868 // 17 / 0.25 = 68 pixels.
870 // We can verify that the content rect (with borders) is one pixel off
871 // 41,9 8x8 on all sides.
872 EXPECT_EQ(tiling
->TileAt(5, 1)->content_rect().ToString(), "40,8 10x10");
874 TilePriority priority
= prioritized_tiles
[tiling
->TileAt(5, 1)].priority();
875 EXPECT_FLOAT_EQ(68.f
, priority
.distance_to_visible
);
877 priority
= prioritized_tiles
[tiling
->TileAt(2, 5)].priority();
878 EXPECT_FLOAT_EQ(68.f
, priority
.distance_to_visible
);
880 priority
= prioritized_tiles
[tiling
->TileAt(3, 4)].priority();
881 EXPECT_FLOAT_EQ(40.f
, priority
.distance_to_visible
);
883 // Move the viewport down 40 pixels.
884 viewport
= gfx::Rect(0, 40, 100, 100);
885 viewport_in_content_space
= gfx::ScaleToEnclosedRect(viewport
, 0.25f
);
886 gfx::Rect skewport
= tiling
->ComputeSkewport(2.0, viewport_in_content_space
);
888 // Compute the soon border.
889 inset
= PictureLayerTiling::CalculateSoonBorderDistance(
890 viewport_in_content_space
, 1.0f
/ 0.25f
);
891 soon_rect_in_content_space
= viewport_in_content_space
;
892 soon_rect_in_content_space
.Inset(-inset
, -inset
);
894 EXPECT_EQ(0, skewport
.x());
895 EXPECT_EQ(10, skewport
.y());
896 EXPECT_EQ(25, skewport
.width());
897 EXPECT_EQ(35, skewport
.height());
899 tiling
->ComputeTilePriorityRects(viewport
, 1.f
, 2.0, Occlusion());
900 prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
903 have_eventually
= false;
906 // Viewport moved, so we expect to find some NOW tiles, some SOON tiles and
907 // some EVENTUALLY tiles.
908 for (int i
= 0; i
< 47; ++i
) {
909 for (int j
= 0; j
< 47; ++j
) {
910 Tile
* tile
= tiling
->TileAt(i
, j
);
911 TilePriority priority
= prioritized_tiles
[tile
].priority();
913 gfx::Rect tile_rect
= tiling
->TilingDataForTesting().TileBounds(i
, j
);
914 if (viewport_in_content_space
.Intersects(tile_rect
)) {
915 EXPECT_EQ(TilePriority::NOW
, priority
.priority_bin
) << "i: " << i
917 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
) << "i: " << i
920 } else if (skewport
.Intersects(tile_rect
) ||
921 soon_rect_in_content_space
.Intersects(tile_rect
)) {
922 EXPECT_EQ(TilePriority::SOON
, priority
.priority_bin
) << "i: " << i
924 EXPECT_GT(priority
.distance_to_visible
, 0.f
) << "i: " << i
928 EXPECT_EQ(TilePriority::EVENTUALLY
, priority
.priority_bin
)
929 << "i: " << i
<< " j: " << j
;
930 EXPECT_GT(priority
.distance_to_visible
, 0.f
) << "i: " << i
932 have_eventually
= true;
937 EXPECT_TRUE(have_now
);
938 EXPECT_TRUE(have_soon
);
939 EXPECT_TRUE(have_eventually
);
941 priority
= prioritized_tiles
[tiling
->TileAt(5, 1)].priority();
942 EXPECT_FLOAT_EQ(68.f
, priority
.distance_to_visible
);
944 priority
= prioritized_tiles
[tiling
->TileAt(2, 5)].priority();
945 EXPECT_FLOAT_EQ(28.f
, priority
.distance_to_visible
);
947 priority
= prioritized_tiles
[tiling
->TileAt(3, 4)].priority();
948 EXPECT_FLOAT_EQ(4.f
, priority
.distance_to_visible
);
950 // Change the underlying layer scale.
951 tiling
->ComputeTilePriorityRects(viewport
, 2.0f
, 3.0, Occlusion());
952 prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
954 priority
= prioritized_tiles
[tiling
->TileAt(5, 1)].priority();
955 EXPECT_FLOAT_EQ(136.f
, priority
.distance_to_visible
);
957 priority
= prioritized_tiles
[tiling
->TileAt(2, 5)].priority();
958 EXPECT_FLOAT_EQ(56.f
, priority
.distance_to_visible
);
960 priority
= prioritized_tiles
[tiling
->TileAt(3, 4)].priority();
961 EXPECT_FLOAT_EQ(8.f
, priority
.distance_to_visible
);
963 // Test additional scales.
964 tiling
= TestablePictureLayerTiling::Create(ACTIVE_TREE
, 0.2f
, pile
, &client
,
965 LayerTreeSettings());
966 tiling
->set_resolution(HIGH_RESOLUTION
);
967 tiling
->ComputeTilePriorityRects(viewport
, 1.0f
, 4.0, Occlusion());
968 prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
970 priority
= prioritized_tiles
[tiling
->TileAt(5, 1)].priority();
971 EXPECT_FLOAT_EQ(110.f
, priority
.distance_to_visible
);
973 priority
= prioritized_tiles
[tiling
->TileAt(2, 5)].priority();
974 EXPECT_FLOAT_EQ(70.f
, priority
.distance_to_visible
);
976 priority
= prioritized_tiles
[tiling
->TileAt(3, 4)].priority();
977 EXPECT_FLOAT_EQ(60.f
, priority
.distance_to_visible
);
979 tiling
->ComputeTilePriorityRects(viewport
, 0.5f
, 5.0, Occlusion());
980 prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
982 priority
= prioritized_tiles
[tiling
->TileAt(5, 1)].priority();
983 EXPECT_FLOAT_EQ(55.f
, priority
.distance_to_visible
);
985 priority
= prioritized_tiles
[tiling
->TileAt(2, 5)].priority();
986 EXPECT_FLOAT_EQ(35.f
, priority
.distance_to_visible
);
988 priority
= prioritized_tiles
[tiling
->TileAt(3, 4)].priority();
989 EXPECT_FLOAT_EQ(30.f
, priority
.distance_to_visible
);
992 static void TileExists(bool exists
, Tile
* tile
,
993 const gfx::Rect
& geometry_rect
) {
994 EXPECT_EQ(exists
, tile
!= NULL
) << geometry_rect
.ToString();
997 TEST_F(PictureLayerTilingIteratorTest
, TilesExist
) {
998 gfx::Size
layer_bounds(1099, 801);
999 Initialize(gfx::Size(100, 100), 1.f
, layer_bounds
);
1000 VerifyTilesExactlyCoverRect(1.f
, gfx::Rect(layer_bounds
));
1001 VerifyTiles(1.f
, gfx::Rect(layer_bounds
), base::Bind(&TileExists
, false));
1003 tiling_
->ComputeTilePriorityRects(
1004 gfx::Rect(layer_bounds
), // visible content rect
1005 1.f
, // current contents scale
1006 1.0, // current frame time
1008 VerifyTiles(1.f
, gfx::Rect(layer_bounds
), base::Bind(&TileExists
, true));
1010 // Make the viewport rect empty. All tiles are killed and become zombies.
1011 tiling_
->ComputeTilePriorityRects(gfx::Rect(), // visible content rect
1012 1.f
, // current contents scale
1013 2.0, // current frame time
1015 VerifyTiles(1.f
, gfx::Rect(layer_bounds
), base::Bind(&TileExists
, false));
1018 TEST_F(PictureLayerTilingIteratorTest
, TilesExistGiantViewport
) {
1019 gfx::Size
layer_bounds(1099, 801);
1020 Initialize(gfx::Size(100, 100), 1.f
, layer_bounds
);
1021 VerifyTilesExactlyCoverRect(1.f
, gfx::Rect(layer_bounds
));
1022 VerifyTiles(1.f
, gfx::Rect(layer_bounds
), base::Bind(&TileExists
, false));
1024 gfx::Rect
giant_rect(-10000000, -10000000, 1000000000, 1000000000);
1026 tiling_
->ComputeTilePriorityRects(
1027 gfx::Rect(layer_bounds
), // visible content rect
1028 1.f
, // current contents scale
1029 1.0, // current frame time
1031 VerifyTiles(1.f
, gfx::Rect(layer_bounds
), base::Bind(&TileExists
, true));
1033 // If the visible content rect is empty, it should still have live tiles.
1034 tiling_
->ComputeTilePriorityRects(giant_rect
, // visible content rect
1035 1.f
, // current contents scale
1036 2.0, // current frame time
1038 VerifyTiles(1.f
, gfx::Rect(layer_bounds
), base::Bind(&TileExists
, true));
1041 TEST_F(PictureLayerTilingIteratorTest
, TilesExistOutsideViewport
) {
1042 gfx::Size
layer_bounds(1099, 801);
1043 Initialize(gfx::Size(100, 100), 1.f
, layer_bounds
);
1044 VerifyTilesExactlyCoverRect(1.f
, gfx::Rect(layer_bounds
));
1045 VerifyTiles(1.f
, gfx::Rect(layer_bounds
), base::Bind(&TileExists
, false));
1047 // This rect does not intersect with the layer, as the layer is outside the
1049 gfx::Rect
viewport_rect(1100, 0, 1000, 1000);
1050 EXPECT_FALSE(viewport_rect
.Intersects(gfx::Rect(layer_bounds
)));
1052 tiling_
->ComputeTilePriorityRects(viewport_rect
, // visible content rect
1053 1.f
, // current contents scale
1054 1.0, // current frame time
1056 VerifyTiles(1.f
, gfx::Rect(layer_bounds
), base::Bind(&TileExists
, true));
1059 static void TilesIntersectingRectExist(const gfx::Rect
& rect
,
1060 bool intersect_exists
,
1062 const gfx::Rect
& geometry_rect
) {
1063 bool intersects
= rect
.Intersects(geometry_rect
);
1064 bool expected_exists
= intersect_exists
? intersects
: !intersects
;
1065 EXPECT_EQ(expected_exists
, tile
!= NULL
)
1066 << "Rects intersecting " << rect
.ToString() << " should exist. "
1067 << "Current tile rect is " << geometry_rect
.ToString();
1070 TEST_F(PictureLayerTilingIteratorTest
,
1071 TilesExistLargeViewportAndLayerWithSmallVisibleArea
) {
1072 gfx::Size
layer_bounds(10000, 10000);
1073 client_
.SetTileSize(gfx::Size(100, 100));
1074 LayerTreeSettings settings
;
1075 settings
.tiling_interest_area_padding
= 1;
1077 scoped_refptr
<FakePicturePileImpl
> pile
=
1078 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds
);
1079 tiling_
= TestablePictureLayerTiling::Create(PENDING_TREE
, 1.f
, pile
,
1080 &client_
, settings
);
1081 tiling_
->set_resolution(HIGH_RESOLUTION
);
1082 VerifyTilesExactlyCoverRect(1.f
, gfx::Rect(layer_bounds
));
1083 VerifyTiles(1.f
, gfx::Rect(layer_bounds
), base::Bind(&TileExists
, false));
1085 gfx::Rect
visible_rect(8000, 8000, 50, 50);
1087 tiling_
->ComputeTilePriorityRects(visible_rect
, // visible content rect
1088 1.f
, // current contents scale
1089 1.0, // current frame time
1092 gfx::Rect(layer_bounds
),
1093 base::Bind(&TilesIntersectingRectExist
, visible_rect
, true));
1096 TEST(ComputeTilePriorityRectsTest
, VisibleTiles
) {
1097 // The TilePriority of visible tiles should have zero distance_to_visible
1098 // and time_to_visible.
1099 FakePictureLayerTilingClient client
;
1101 gfx::Size
device_viewport(800, 600);
1102 gfx::Size
last_layer_bounds(200, 200);
1103 gfx::Size
current_layer_bounds(200, 200);
1104 float current_layer_contents_scale
= 1.f
;
1105 gfx::Transform current_screen_transform
;
1106 double current_frame_time_in_seconds
= 1.0;
1108 gfx::Rect viewport_in_layer_space
= ViewportInLayerSpace(
1109 current_screen_transform
, device_viewport
);
1111 client
.SetTileSize(gfx::Size(100, 100));
1113 scoped_refptr
<FakePicturePileImpl
> pile
=
1114 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1115 current_layer_bounds
);
1116 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
1117 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
1118 LayerTreeSettings());
1119 tiling
->set_resolution(HIGH_RESOLUTION
);
1121 tiling
->ComputeTilePriorityRects(viewport_in_layer_space
,
1122 current_layer_contents_scale
,
1123 current_frame_time_in_seconds
, Occlusion());
1124 auto prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
1126 ASSERT_TRUE(tiling
->TileAt(0, 0));
1127 ASSERT_TRUE(tiling
->TileAt(0, 1));
1128 ASSERT_TRUE(tiling
->TileAt(1, 0));
1129 ASSERT_TRUE(tiling
->TileAt(1, 1));
1131 TilePriority priority
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1132 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1133 EXPECT_FLOAT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1135 priority
= prioritized_tiles
[tiling
->TileAt(0, 1)].priority();
1136 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1137 EXPECT_FLOAT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1139 priority
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1140 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1141 EXPECT_FLOAT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1143 priority
= prioritized_tiles
[tiling
->TileAt(1, 1)].priority();
1144 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1145 EXPECT_FLOAT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1148 TEST(ComputeTilePriorityRectsTest
, OffscreenTiles
) {
1149 // The TilePriority of offscreen tiles (without movement) should have nonzero
1150 // distance_to_visible and infinite time_to_visible.
1151 FakePictureLayerTilingClient client
;
1153 gfx::Size
device_viewport(800, 600);
1154 gfx::Size
last_layer_bounds(200, 200);
1155 gfx::Size
current_layer_bounds(200, 200);
1156 float current_layer_contents_scale
= 1.f
;
1157 gfx::Transform last_screen_transform
;
1158 gfx::Transform current_screen_transform
;
1159 double current_frame_time_in_seconds
= 1.0;
1161 current_screen_transform
.Translate(850, 0);
1162 last_screen_transform
= current_screen_transform
;
1164 gfx::Rect viewport_in_layer_space
= ViewportInLayerSpace(
1165 current_screen_transform
, device_viewport
);
1167 client
.SetTileSize(gfx::Size(100, 100));
1169 scoped_refptr
<FakePicturePileImpl
> pile
=
1170 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1171 current_layer_bounds
);
1172 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
1173 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
1174 LayerTreeSettings());
1175 tiling
->set_resolution(HIGH_RESOLUTION
);
1177 tiling
->ComputeTilePriorityRects(viewport_in_layer_space
,
1178 current_layer_contents_scale
,
1179 current_frame_time_in_seconds
, Occlusion());
1180 auto prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
1182 ASSERT_TRUE(tiling
->TileAt(0, 0));
1183 ASSERT_TRUE(tiling
->TileAt(0, 1));
1184 ASSERT_TRUE(tiling
->TileAt(1, 0));
1185 ASSERT_TRUE(tiling
->TileAt(1, 1));
1187 TilePriority priority
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1188 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1189 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1191 priority
= prioritized_tiles
[tiling
->TileAt(0, 1)].priority();
1192 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1193 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1195 priority
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1196 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1197 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1199 priority
= prioritized_tiles
[tiling
->TileAt(1, 1)].priority();
1200 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1201 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1203 // Furthermore, in this scenario tiles on the right hand side should have a
1204 // larger distance to visible.
1205 TilePriority left
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1206 TilePriority right
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1207 EXPECT_GT(right
.distance_to_visible
, left
.distance_to_visible
);
1209 left
= prioritized_tiles
[tiling
->TileAt(0, 1)].priority();
1210 right
= prioritized_tiles
[tiling
->TileAt(1, 1)].priority();
1211 EXPECT_GT(right
.distance_to_visible
, left
.distance_to_visible
);
1214 TEST(ComputeTilePriorityRectsTest
, PartiallyOffscreenLayer
) {
1215 // Sanity check that a layer with some tiles visible and others offscreen has
1216 // correct TilePriorities for each tile.
1217 FakePictureLayerTilingClient client
;
1219 gfx::Size
device_viewport(800, 600);
1220 gfx::Size
last_layer_bounds(200, 200);
1221 gfx::Size
current_layer_bounds(200, 200);
1222 float current_layer_contents_scale
= 1.f
;
1223 gfx::Transform last_screen_transform
;
1224 gfx::Transform current_screen_transform
;
1225 double current_frame_time_in_seconds
= 1.0;
1227 current_screen_transform
.Translate(705, 505);
1228 last_screen_transform
= current_screen_transform
;
1230 gfx::Rect viewport_in_layer_space
= ViewportInLayerSpace(
1231 current_screen_transform
, device_viewport
);
1233 client
.SetTileSize(gfx::Size(100, 100));
1235 scoped_refptr
<FakePicturePileImpl
> pile
=
1236 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1237 current_layer_bounds
);
1238 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
1239 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
1240 LayerTreeSettings());
1241 tiling
->set_resolution(HIGH_RESOLUTION
);
1243 tiling
->ComputeTilePriorityRects(viewport_in_layer_space
,
1244 current_layer_contents_scale
,
1245 current_frame_time_in_seconds
, Occlusion());
1246 auto prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
1248 ASSERT_TRUE(tiling
->TileAt(0, 0));
1249 ASSERT_TRUE(tiling
->TileAt(0, 1));
1250 ASSERT_TRUE(tiling
->TileAt(1, 0));
1251 ASSERT_TRUE(tiling
->TileAt(1, 1));
1253 TilePriority priority
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1254 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1255 EXPECT_FLOAT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1257 priority
= prioritized_tiles
[tiling
->TileAt(0, 1)].priority();
1258 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1259 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1261 priority
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1262 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1263 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1265 priority
= prioritized_tiles
[tiling
->TileAt(1, 1)].priority();
1266 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1267 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1270 TEST(ComputeTilePriorityRectsTest
, PartiallyOffscreenRotatedLayer
) {
1271 // Each tile of a layer may be affected differently by a transform; Check
1272 // that ComputeTilePriorityRects correctly accounts for the transform between
1273 // layer space and screen space.
1274 FakePictureLayerTilingClient client
;
1276 gfx::Size
device_viewport(800, 600);
1277 gfx::Size
last_layer_bounds(200, 200);
1278 gfx::Size
current_layer_bounds(200, 200);
1279 float current_layer_contents_scale
= 1.f
;
1280 gfx::Transform last_screen_transform
;
1281 gfx::Transform current_screen_transform
;
1282 double current_frame_time_in_seconds
= 1.0;
1284 // A diagonally rotated layer that is partially off the bottom of the screen.
1285 // In this configuration, only the top-left tile would be visible.
1286 current_screen_transform
.Translate(600, 750);
1287 current_screen_transform
.RotateAboutZAxis(45);
1288 last_screen_transform
= current_screen_transform
;
1290 gfx::Rect viewport_in_layer_space
= ViewportInLayerSpace(
1291 current_screen_transform
, device_viewport
);
1293 client
.SetTileSize(gfx::Size(100, 100));
1295 scoped_refptr
<FakePicturePileImpl
> pile
=
1296 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1297 current_layer_bounds
);
1298 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
1299 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
1300 LayerTreeSettings());
1301 tiling
->set_resolution(HIGH_RESOLUTION
);
1303 tiling
->ComputeTilePriorityRects(viewport_in_layer_space
,
1304 current_layer_contents_scale
,
1305 current_frame_time_in_seconds
, Occlusion());
1306 auto prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
1308 ASSERT_TRUE(tiling
->TileAt(0, 0));
1309 ASSERT_TRUE(tiling
->TileAt(0, 1));
1310 ASSERT_TRUE(tiling
->TileAt(1, 0));
1311 ASSERT_TRUE(tiling
->TileAt(1, 1));
1313 TilePriority priority
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1314 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1315 EXPECT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1317 priority
= prioritized_tiles
[tiling
->TileAt(0, 1)].priority();
1318 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1319 EXPECT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1321 priority
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1322 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1323 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1325 priority
= prioritized_tiles
[tiling
->TileAt(1, 1)].priority();
1326 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1327 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1329 // Furthermore, in this scenario the bottom-right tile should have the larger
1330 // distance to visible.
1331 TilePriority top_left
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1332 TilePriority top_right
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1333 TilePriority bottom_right
=
1334 prioritized_tiles
[tiling
->TileAt(1, 1)].priority();
1335 EXPECT_GT(top_right
.distance_to_visible
, top_left
.distance_to_visible
);
1337 EXPECT_EQ(bottom_right
.distance_to_visible
, top_right
.distance_to_visible
);
1340 TEST(ComputeTilePriorityRectsTest
, PerspectiveLayer
) {
1341 // Perspective transforms need to take a different code path.
1342 // This test checks tile priorities of a perspective layer.
1343 FakePictureLayerTilingClient client
;
1345 gfx::Size
device_viewport(800, 600);
1346 gfx::Rect
visible_layer_rect(0, 0, 0, 0); // offscreen.
1347 gfx::Size
last_layer_bounds(200, 200);
1348 gfx::Size
current_layer_bounds(200, 200);
1349 float current_layer_contents_scale
= 1.f
;
1350 gfx::Transform last_screen_transform
;
1351 gfx::Transform current_screen_transform
;
1352 double current_frame_time_in_seconds
= 1.0;
1354 // A 3d perspective layer rotated about its Y axis, translated to almost
1355 // fully offscreen. The left side will appear closer (i.e. larger in 2d) than
1356 // the right side, so the top-left tile will technically be closer than the
1359 // Translate layer to offscreen
1360 current_screen_transform
.Translate(400.0, 630.0);
1361 // Apply perspective about the center of the layer
1362 current_screen_transform
.Translate(100.0, 100.0);
1363 current_screen_transform
.ApplyPerspectiveDepth(100.0);
1364 current_screen_transform
.RotateAboutYAxis(10.0);
1365 current_screen_transform
.Translate(-100.0, -100.0);
1366 last_screen_transform
= current_screen_transform
;
1368 // Sanity check that this transform wouldn't cause w<0 clipping.
1370 MathUtil::MapQuad(current_screen_transform
,
1371 gfx::QuadF(gfx::RectF(0, 0, 200, 200)),
1373 ASSERT_FALSE(clipped
);
1375 gfx::Rect viewport_in_layer_space
= ViewportInLayerSpace(
1376 current_screen_transform
, device_viewport
);
1378 client
.SetTileSize(gfx::Size(100, 100));
1380 scoped_refptr
<FakePicturePileImpl
> pile
=
1381 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1382 current_layer_bounds
);
1383 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
1384 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
1385 LayerTreeSettings());
1386 tiling
->set_resolution(HIGH_RESOLUTION
);
1388 tiling
->ComputeTilePriorityRects(viewport_in_layer_space
,
1389 current_layer_contents_scale
,
1390 current_frame_time_in_seconds
, Occlusion());
1391 auto prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
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));
1398 // All tiles will have a positive distance_to_visible
1399 // and an infinite time_to_visible.
1400 TilePriority priority
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1401 EXPECT_FLOAT_EQ(priority
.distance_to_visible
, 0.f
);
1402 EXPECT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1404 priority
= prioritized_tiles
[tiling
->TileAt(0, 1)].priority();
1405 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1406 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1408 priority
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1409 EXPECT_FLOAT_EQ(priority
.distance_to_visible
, 0.f
);
1410 EXPECT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1412 priority
= prioritized_tiles
[tiling
->TileAt(1, 1)].priority();
1413 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1414 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1416 // Furthermore, in this scenario the top-left distance_to_visible
1417 // will be smallest, followed by top-right. The bottom layers
1418 // will of course be further than the top layers.
1419 TilePriority top_left
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1420 TilePriority top_right
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1421 TilePriority bottom_left
= prioritized_tiles
[tiling
->TileAt(0, 1)].priority();
1422 TilePriority bottom_right
=
1423 prioritized_tiles
[tiling
->TileAt(1, 1)].priority();
1425 EXPECT_GT(bottom_right
.distance_to_visible
, top_right
.distance_to_visible
);
1427 EXPECT_GT(bottom_left
.distance_to_visible
, top_left
.distance_to_visible
);
1430 TEST(ComputeTilePriorityRectsTest
, PerspectiveLayerClippedByW
) {
1431 // Perspective transforms need to take a different code path.
1432 // This test checks tile priorities of a perspective layer.
1433 FakePictureLayerTilingClient client
;
1435 gfx::Size
device_viewport(800, 600);
1436 gfx::Size
last_layer_bounds(200, 200);
1437 gfx::Size
current_layer_bounds(200, 200);
1438 float current_layer_contents_scale
= 1.f
;
1439 gfx::Transform last_screen_transform
;
1440 gfx::Transform current_screen_transform
;
1441 double current_frame_time_in_seconds
= 1.0;
1443 // A 3d perspective layer rotated about its Y axis, translated to almost
1444 // fully offscreen. The left side will appear closer (i.e. larger in 2d) than
1445 // the right side, so the top-left tile will technically be closer than the
1448 // Translate layer to offscreen
1449 current_screen_transform
.Translate(400.0, 970.0);
1450 // Apply perspective and rotation about the center of the layer
1451 current_screen_transform
.Translate(100.0, 100.0);
1452 current_screen_transform
.ApplyPerspectiveDepth(10.0);
1453 current_screen_transform
.RotateAboutYAxis(10.0);
1454 current_screen_transform
.Translate(-100.0, -100.0);
1455 last_screen_transform
= current_screen_transform
;
1457 // Sanity check that this transform does cause w<0 clipping for the left side
1458 // of the layer, but not the right side.
1460 MathUtil::MapQuad(current_screen_transform
,
1461 gfx::QuadF(gfx::RectF(0, 0, 100, 200)),
1463 ASSERT_TRUE(clipped
);
1465 MathUtil::MapQuad(current_screen_transform
,
1466 gfx::QuadF(gfx::RectF(100, 0, 100, 200)),
1468 ASSERT_FALSE(clipped
);
1470 gfx::Rect viewport_in_layer_space
= ViewportInLayerSpace(
1471 current_screen_transform
, device_viewport
);
1473 client
.SetTileSize(gfx::Size(100, 100));
1475 scoped_refptr
<FakePicturePileImpl
> pile
=
1476 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1477 current_layer_bounds
);
1478 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
1479 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
1480 LayerTreeSettings());
1481 tiling
->set_resolution(HIGH_RESOLUTION
);
1483 tiling
->ComputeTilePriorityRects(viewport_in_layer_space
,
1484 current_layer_contents_scale
,
1485 current_frame_time_in_seconds
, Occlusion());
1486 auto prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
1488 ASSERT_TRUE(tiling
->TileAt(0, 0));
1489 ASSERT_TRUE(tiling
->TileAt(0, 1));
1490 ASSERT_TRUE(tiling
->TileAt(1, 0));
1491 ASSERT_TRUE(tiling
->TileAt(1, 1));
1493 // Left-side tiles will be clipped by the transform, so we have to assume
1494 // they are visible just in case.
1495 TilePriority priority
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1496 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1497 EXPECT_FLOAT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1499 priority
= prioritized_tiles
[tiling
->TileAt(0, 1)].priority();
1500 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1501 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1503 // Right-side tiles will have a positive distance_to_visible
1504 // and an infinite time_to_visible.
1505 priority
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1506 EXPECT_FLOAT_EQ(priority
.distance_to_visible
, 0.f
);
1507 EXPECT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1509 priority
= prioritized_tiles
[tiling
->TileAt(1, 1)].priority();
1510 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1511 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1514 TEST(ComputeTilePriorityRectsTest
, BasicMotion
) {
1515 // Test that time_to_visible is computed correctly when
1516 // there is some motion.
1517 FakePictureLayerTilingClient client
;
1519 gfx::Size
device_viewport(800, 600);
1520 gfx::Rect
visible_layer_rect(0, 0, 0, 0);
1521 gfx::Size
last_layer_bounds(200, 200);
1522 gfx::Size
current_layer_bounds(200, 200);
1523 float last_layer_contents_scale
= 1.f
;
1524 float current_layer_contents_scale
= 1.f
;
1525 gfx::Transform last_screen_transform
;
1526 gfx::Transform current_screen_transform
;
1527 double last_frame_time_in_seconds
= 1.0;
1528 double current_frame_time_in_seconds
= 2.0;
1530 // Offscreen layer is coming closer to viewport at 1000 pixels per second.
1531 current_screen_transform
.Translate(1800, 0);
1532 last_screen_transform
.Translate(2800, 0);
1534 gfx::Rect viewport_in_layer_space
= ViewportInLayerSpace(
1535 current_screen_transform
, device_viewport
);
1537 client
.SetTileSize(gfx::Size(100, 100));
1538 LayerTreeSettings settings
;
1540 scoped_refptr
<FakePicturePileImpl
> pile
=
1541 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1542 current_layer_bounds
);
1543 scoped_ptr
<TestablePictureLayerTiling
> tiling
=
1544 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
1546 tiling
->set_resolution(HIGH_RESOLUTION
);
1548 // previous ("last") frame
1549 tiling
->ComputeTilePriorityRects(viewport_in_layer_space
,
1550 last_layer_contents_scale
,
1551 last_frame_time_in_seconds
, Occlusion());
1554 tiling
->ComputeTilePriorityRects(viewport_in_layer_space
,
1555 current_layer_contents_scale
,
1556 current_frame_time_in_seconds
, Occlusion());
1557 auto prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
1559 ASSERT_TRUE(tiling
->TileAt(0, 0));
1560 ASSERT_TRUE(tiling
->TileAt(0, 1));
1561 ASSERT_TRUE(tiling
->TileAt(1, 0));
1562 ASSERT_TRUE(tiling
->TileAt(1, 1));
1564 TilePriority priority
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1565 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1566 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1568 priority
= prioritized_tiles
[tiling
->TileAt(0, 1)].priority();
1569 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1570 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1572 // time_to_visible for the right hand side layers needs an extra 0.099
1573 // seconds because this tile is 99 pixels further away.
1574 priority
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1575 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1576 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1578 priority
= prioritized_tiles
[tiling
->TileAt(1, 1)].priority();
1579 EXPECT_GT(priority
.distance_to_visible
, 0.f
);
1580 EXPECT_NE(TilePriority::NOW
, priority
.priority_bin
);
1583 TEST(ComputeTilePriorityRectsTest
, RotationMotion
) {
1584 // Each tile of a layer may be affected differently by a transform; Check
1585 // that ComputeTilePriorityRects correctly accounts for the transform between
1586 // layer space and screen space.
1588 FakePictureLayerTilingClient client
;
1589 scoped_ptr
<TestablePictureLayerTiling
> tiling
;
1591 gfx::Size
device_viewport(800, 600);
1592 gfx::Rect
visible_layer_rect(0, 0, 0, 0); // offscren.
1593 gfx::Size
last_layer_bounds(200, 200);
1594 gfx::Size
current_layer_bounds(200, 200);
1595 float last_layer_contents_scale
= 1.f
;
1596 float current_layer_contents_scale
= 1.f
;
1597 gfx::Transform last_screen_transform
;
1598 gfx::Transform current_screen_transform
;
1599 double last_frame_time_in_seconds
= 1.0;
1600 double current_frame_time_in_seconds
= 2.0;
1602 // Rotation motion is set up specifically so that:
1603 // - rotation occurs about the center of the layer
1604 // - the top-left tile becomes visible on rotation
1605 // - the top-right tile will have an infinite time_to_visible
1606 // because it is rotating away from viewport.
1607 // - bottom-left layer will have a positive non-zero time_to_visible
1608 // because it is rotating toward the viewport.
1609 current_screen_transform
.Translate(400, 550);
1610 current_screen_transform
.RotateAboutZAxis(45);
1612 last_screen_transform
.Translate(400, 550);
1614 gfx::Rect viewport_in_layer_space
= ViewportInLayerSpace(
1615 current_screen_transform
, device_viewport
);
1617 client
.SetTileSize(gfx::Size(100, 100));
1619 scoped_refptr
<FakePicturePileImpl
> pile
=
1620 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1621 current_layer_bounds
);
1622 tiling
= TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
, &client
,
1623 LayerTreeSettings());
1624 tiling
->set_resolution(HIGH_RESOLUTION
);
1626 // previous ("last") frame
1627 tiling
->ComputeTilePriorityRects(viewport_in_layer_space
,
1628 last_layer_contents_scale
,
1629 last_frame_time_in_seconds
, Occlusion());
1632 tiling
->ComputeTilePriorityRects(viewport_in_layer_space
,
1633 current_layer_contents_scale
,
1634 current_frame_time_in_seconds
, Occlusion());
1635 auto prioritized_tiles
= tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
1637 ASSERT_TRUE(tiling
->TileAt(0, 0));
1638 ASSERT_TRUE(tiling
->TileAt(0, 1));
1639 ASSERT_TRUE(tiling
->TileAt(1, 0));
1640 ASSERT_TRUE(tiling
->TileAt(1, 1));
1642 TilePriority priority
= prioritized_tiles
[tiling
->TileAt(0, 0)].priority();
1643 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1644 EXPECT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1646 priority
= prioritized_tiles
[tiling
->TileAt(0, 1)].priority();
1647 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1648 EXPECT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1650 priority
= prioritized_tiles
[tiling
->TileAt(1, 0)].priority();
1651 EXPECT_FLOAT_EQ(0.f
, priority
.distance_to_visible
);
1652 EXPECT_EQ(TilePriority::NOW
, priority
.priority_bin
);
1655 TEST(PictureLayerTilingTest
, RecycledTilesCleared
) {
1656 // This test performs the following:
1658 // - Two tilings, one active one recycled with all tiles shared.
1660 // - Viewport moves somewhere far away and active tiling clears tiles.
1661 // - Viewport moves back and a new active tiling tile is created.
1663 // - Recycle tiling does _not_ have the tile in the same location (thus it
1664 // will be shared next time a pending tiling is created).
1666 FakePictureLayerTilingClient active_client
;
1668 active_client
.SetTileSize(gfx::Size(100, 100));
1669 LayerTreeSettings settings
;
1671 scoped_refptr
<FakePicturePileImpl
> pile
=
1672 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1673 gfx::Size(10000, 10000));
1674 scoped_ptr
<TestablePictureLayerTiling
> active_tiling
=
1675 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
,
1676 &active_client
, settings
);
1677 active_tiling
->set_resolution(HIGH_RESOLUTION
);
1678 // Create all tiles on this tiling.
1679 active_tiling
->ComputeTilePriorityRects(gfx::Rect(0, 0, 100, 100), 1.0f
, 1.0f
,
1682 FakePictureLayerTilingClient recycle_client
;
1683 recycle_client
.SetTileSize(gfx::Size(100, 100));
1684 recycle_client
.set_twin_tiling(active_tiling
.get());
1686 pile
= FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1687 gfx::Size(10000, 10000));
1688 scoped_ptr
<TestablePictureLayerTiling
> recycle_tiling
=
1689 TestablePictureLayerTiling::Create(PENDING_TREE
, 1.0f
, pile
,
1690 &recycle_client
, settings
);
1691 recycle_tiling
->set_resolution(HIGH_RESOLUTION
);
1693 // Create all tiles on the second tiling. All tiles should be shared.
1694 recycle_tiling
->ComputeTilePriorityRects(gfx::Rect(0, 0, 100, 100), 1.0f
,
1697 // Set the second tiling as recycled.
1698 active_client
.set_twin_tiling(NULL
);
1699 recycle_client
.set_twin_tiling(NULL
);
1701 EXPECT_TRUE(active_tiling
->TileAt(0, 0));
1702 EXPECT_FALSE(recycle_tiling
->TileAt(0, 0));
1704 // Move the viewport far away from the (0, 0) tile.
1705 active_tiling
->ComputeTilePriorityRects(gfx::Rect(9000, 9000, 100, 100), 1.0f
,
1707 // Ensure the tile was deleted.
1708 EXPECT_FALSE(active_tiling
->TileAt(0, 0));
1709 EXPECT_FALSE(recycle_tiling
->TileAt(0, 0));
1711 // Move the viewport back to (0, 0) tile.
1712 active_tiling
->ComputeTilePriorityRects(gfx::Rect(0, 0, 100, 100), 1.0f
, 3.0,
1715 // Ensure that we now have a tile here on both active.
1716 EXPECT_TRUE(active_tiling
->TileAt(0, 0));
1717 EXPECT_FALSE(recycle_tiling
->TileAt(0, 0));
1720 TEST(PictureLayerTilingTest
, RecycledTilesClearedOnReset
) {
1721 FakePictureLayerTilingClient active_client
;
1722 active_client
.SetTileSize(gfx::Size(100, 100));
1724 scoped_refptr
<FakePicturePileImpl
> pile
=
1725 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1726 gfx::Size(100, 100));
1727 scoped_ptr
<TestablePictureLayerTiling
> active_tiling
=
1728 TestablePictureLayerTiling::Create(ACTIVE_TREE
, 1.0f
, pile
,
1729 &active_client
, LayerTreeSettings());
1730 active_tiling
->set_resolution(HIGH_RESOLUTION
);
1731 // Create all tiles on this tiling.
1732 active_tiling
->ComputeTilePriorityRects(gfx::Rect(0, 0, 100, 100), 1.0f
, 1.0f
,
1735 FakePictureLayerTilingClient recycle_client
;
1736 recycle_client
.SetTileSize(gfx::Size(100, 100));
1737 recycle_client
.set_twin_tiling(active_tiling
.get());
1739 LayerTreeSettings settings
;
1741 pile
= FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1742 gfx::Size(100, 100));
1743 scoped_ptr
<TestablePictureLayerTiling
> recycle_tiling
=
1744 TestablePictureLayerTiling::Create(PENDING_TREE
, 1.0f
, pile
,
1745 &recycle_client
, settings
);
1746 recycle_tiling
->set_resolution(HIGH_RESOLUTION
);
1748 // Create all tiles on the recycle tiling. All tiles should be shared.
1749 recycle_tiling
->ComputeTilePriorityRects(gfx::Rect(0, 0, 100, 100), 1.0f
,
1752 // Set the second tiling as recycled.
1753 active_client
.set_twin_tiling(NULL
);
1754 recycle_client
.set_twin_tiling(NULL
);
1756 EXPECT_TRUE(active_tiling
->TileAt(0, 0));
1757 EXPECT_FALSE(recycle_tiling
->TileAt(0, 0));
1759 // Reset the active tiling. The recycle tiles should be released too.
1760 active_tiling
->Reset();
1761 EXPECT_FALSE(active_tiling
->TileAt(0, 0));
1762 EXPECT_FALSE(recycle_tiling
->TileAt(0, 0));
1765 TEST_F(PictureLayerTilingIteratorTest
, ResizeTilesAndUpdateToCurrent
) {
1766 // The tiling has four rows and three columns.
1767 Initialize(gfx::Size(150, 100), 1.f
, gfx::Size(250, 150));
1768 tiling_
->CreateAllTilesForTesting();
1769 EXPECT_EQ(150, tiling_
->TilingDataForTesting().max_texture_size().width());
1770 EXPECT_EQ(100, tiling_
->TilingDataForTesting().max_texture_size().height());
1771 EXPECT_EQ(4u, tiling_
->AllTilesForTesting().size());
1773 client_
.SetTileSize(gfx::Size(250, 200));
1775 // Tile size in the tiling should still be 150x100.
1776 EXPECT_EQ(150, tiling_
->TilingDataForTesting().max_texture_size().width());
1777 EXPECT_EQ(100, tiling_
->TilingDataForTesting().max_texture_size().height());
1779 // The layer's size isn't changed, but the tile size was.
1780 scoped_refptr
<FakePicturePileImpl
> pile
=
1781 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(
1782 gfx::Size(250, 150));
1783 tiling_
->SetRasterSourceAndResize(pile
);
1785 // Tile size in the tiling should be resized to 250x200.
1786 EXPECT_EQ(250, tiling_
->TilingDataForTesting().max_texture_size().width());
1787 EXPECT_EQ(200, tiling_
->TilingDataForTesting().max_texture_size().height());
1788 EXPECT_EQ(0u, tiling_
->AllTilesForTesting().size());
1791 // This test runs into floating point issues because of big numbers.
1792 TEST_F(PictureLayerTilingIteratorTest
, GiantRect
) {
1793 gfx::Size
tile_size(256, 256);
1794 gfx::Size
layer_size(33554432, 33554432);
1795 bool filled
= false;
1796 float contents_scale
= 1.f
;
1798 client_
.SetTileSize(tile_size
);
1799 scoped_refptr
<FakePicturePileImpl
> pile
= FakePicturePileImpl::CreatePile(
1800 tile_size
, layer_size
, gfx::Rect(), filled
);
1801 tiling_
= TestablePictureLayerTiling::Create(
1802 PENDING_TREE
, contents_scale
, pile
, &client_
, LayerTreeSettings());
1804 gfx::Rect
content_rect(25554432, 25554432, 950, 860);
1805 VerifyTilesExactlyCoverRect(contents_scale
, content_rect
);