1 // Copyright 2013 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 "base/thread_task_runner_handle.h"
6 #include "cc/resources/eviction_tile_priority_queue.h"
7 #include "cc/resources/raster_tile_priority_queue.h"
8 #include "cc/resources/resource_pool.h"
9 #include "cc/resources/tile.h"
10 #include "cc/resources/tile_priority.h"
11 #include "cc/resources/tiling_set_raster_queue_all.h"
12 #include "cc/test/begin_frame_args_test.h"
13 #include "cc/test/fake_impl_proxy.h"
14 #include "cc/test/fake_layer_tree_host_impl.h"
15 #include "cc/test/fake_output_surface.h"
16 #include "cc/test/fake_output_surface_client.h"
17 #include "cc/test/fake_picture_layer_impl.h"
18 #include "cc/test/fake_picture_layer_tiling_client.h"
19 #include "cc/test/fake_picture_pile_impl.h"
20 #include "cc/test/fake_tile_manager.h"
21 #include "cc/test/impl_side_painting_settings.h"
22 #include "cc/test/test_shared_bitmap_manager.h"
23 #include "cc/test/test_task_graph_runner.h"
24 #include "cc/test/test_tile_priorities.h"
25 #include "cc/trees/layer_tree_impl.h"
26 #include "testing/gtest/include/gtest/gtest.h"
31 class LowResTilingsSettings
: public ImplSidePaintingSettings
{
33 LowResTilingsSettings() {
34 tiling_interest_area_viewport_multiplier
= 10000;
35 create_low_res_tiling
= true;
39 class TileManagerTilePriorityQueueTest
: public testing::Test
{
41 TileManagerTilePriorityQueueTest()
42 : memory_limit_policy_(ALLOW_ANYTHING
),
44 ready_to_activate_(false),
46 proxy_(base::ThreadTaskRunnerHandle::Get()),
47 host_impl_(LowResTilingsSettings(),
49 &shared_bitmap_manager_
,
50 &task_graph_runner_
) {}
52 void SetTreePriority(TreePriority tree_priority
) {
53 GlobalStateThatImpactsTilePriority state
;
54 gfx::Size
tile_size(256, 256);
56 state
.soft_memory_limit_in_bytes
= 100 * 1000 * 1000;
57 state
.num_resources_limit
= max_tiles_
;
58 state
.hard_memory_limit_in_bytes
= state
.soft_memory_limit_in_bytes
* 2;
59 state
.memory_limit_policy
= memory_limit_policy_
;
60 state
.tree_priority
= tree_priority
;
62 global_state_
= state
;
63 host_impl_
.resource_pool()->SetResourceUsageLimits(
64 state
.soft_memory_limit_in_bytes
,
65 state
.soft_memory_limit_in_bytes
,
66 state
.num_resources_limit
);
67 host_impl_
.tile_manager()->SetGlobalStateForTesting(state
);
70 void SetUp() override
{
72 SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES
);
75 virtual void InitializeRenderer() {
76 host_impl_
.InitializeRenderer(FakeOutputSurface::Create3d());
79 void SetupDefaultTrees(const gfx::Size
& layer_bounds
) {
80 gfx::Size
tile_size(100, 100);
82 scoped_refptr
<FakePicturePileImpl
> pending_pile
=
83 FakePicturePileImpl::CreateFilledPile(tile_size
, layer_bounds
);
84 scoped_refptr
<FakePicturePileImpl
> active_pile
=
85 FakePicturePileImpl::CreateFilledPile(tile_size
, layer_bounds
);
87 SetupTrees(pending_pile
, active_pile
);
91 host_impl_
.ActivateSyncTree();
92 CHECK(!host_impl_
.pending_tree());
93 pending_layer_
= NULL
;
94 active_layer_
= static_cast<FakePictureLayerImpl
*>(
95 host_impl_
.active_tree()->LayerById(id_
));
98 void SetupDefaultTreesWithFixedTileSize(const gfx::Size
& layer_bounds
,
99 const gfx::Size
& tile_size
) {
100 SetupDefaultTrees(layer_bounds
);
101 pending_layer_
->set_fixed_tile_size(tile_size
);
102 active_layer_
->set_fixed_tile_size(tile_size
);
105 void SetupTrees(scoped_refptr
<PicturePileImpl
> pending_pile
,
106 scoped_refptr
<PicturePileImpl
> active_pile
) {
107 SetupPendingTree(active_pile
);
109 SetupPendingTree(pending_pile
);
112 void SetupPendingTree(scoped_refptr
<PicturePileImpl
> pile
) {
113 host_impl_
.CreatePendingTree();
114 LayerTreeImpl
* pending_tree
= host_impl_
.pending_tree();
116 // Steal from the recycled tree.
117 scoped_ptr
<LayerImpl
> old_pending_root
= pending_tree
->DetachLayerTree();
118 DCHECK_IMPLIES(old_pending_root
, old_pending_root
->id() == id_
);
120 scoped_ptr
<FakePictureLayerImpl
> pending_layer
;
121 if (old_pending_root
) {
123 static_cast<FakePictureLayerImpl
*>(old_pending_root
.release()));
124 pending_layer
->SetRasterSourceOnPending(pile
, Region());
127 FakePictureLayerImpl::CreateWithRasterSource(pending_tree
, id_
, pile
);
128 pending_layer
->SetDrawsContent(true);
129 pending_layer
->SetHasRenderSurface(true);
131 // The bounds() just mirror the pile size.
132 pending_layer
->SetBounds(pending_layer
->raster_source()->GetSize());
133 pending_tree
->SetRootLayer(pending_layer
.Pass());
135 pending_layer_
= static_cast<FakePictureLayerImpl
*>(
136 host_impl_
.pending_tree()->LayerById(id_
));
138 // Add tilings/tiles for the layer.
139 bool update_lcd_text
= false;
140 host_impl_
.pending_tree()->UpdateDrawProperties(update_lcd_text
);
143 TileManager
* tile_manager() { return host_impl_
.tile_manager(); }
146 GlobalStateThatImpactsTilePriority global_state_
;
148 TestSharedBitmapManager shared_bitmap_manager_
;
149 TestTaskGraphRunner task_graph_runner_
;
150 TileMemoryLimitPolicy memory_limit_policy_
;
152 bool ready_to_activate_
;
154 FakeImplProxy proxy_
;
155 FakeLayerTreeHostImpl host_impl_
;
156 FakePictureLayerImpl
* pending_layer_
;
157 FakePictureLayerImpl
* active_layer_
;
160 TEST_F(TileManagerTilePriorityQueueTest
, RasterTilePriorityQueue
) {
161 const gfx::Size
layer_bounds(1000, 1000);
162 host_impl_
.SetViewportSize(layer_bounds
);
163 SetupDefaultTrees(layer_bounds
);
165 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
166 SAME_PRIORITY_FOR_BOTH_TREES
, RasterTilePriorityQueue::Type::ALL
));
167 EXPECT_FALSE(queue
->IsEmpty());
169 size_t tile_count
= 0;
170 std::set
<Tile
*> all_tiles
;
171 while (!queue
->IsEmpty()) {
172 EXPECT_TRUE(queue
->Top().tile());
173 all_tiles
.insert(queue
->Top().tile());
178 EXPECT_EQ(tile_count
, all_tiles
.size());
179 EXPECT_EQ(16u, tile_count
);
181 // Sanity check, all tiles should be visible.
182 std::set
<Tile
*> smoothness_tiles
;
183 queue
= host_impl_
.BuildRasterQueue(SMOOTHNESS_TAKES_PRIORITY
,
184 RasterTilePriorityQueue::Type::ALL
);
185 bool had_low_res
= false;
186 while (!queue
->IsEmpty()) {
187 PrioritizedTile prioritized_tile
= queue
->Top();
188 EXPECT_TRUE(prioritized_tile
.tile());
189 EXPECT_EQ(TilePriority::NOW
, prioritized_tile
.priority().priority_bin
);
190 if (prioritized_tile
.priority().resolution
== LOW_RESOLUTION
)
193 smoothness_tiles
.insert(prioritized_tile
.tile());
196 EXPECT_EQ(all_tiles
, smoothness_tiles
);
197 EXPECT_TRUE(had_low_res
);
199 // Check that everything is required for activation.
200 queue
= host_impl_
.BuildRasterQueue(
201 SMOOTHNESS_TAKES_PRIORITY
,
202 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION
);
203 std::set
<Tile
*> required_for_activation_tiles
;
204 while (!queue
->IsEmpty()) {
205 PrioritizedTile prioritized_tile
= queue
->Top();
206 EXPECT_TRUE(prioritized_tile
.tile()->required_for_activation());
207 required_for_activation_tiles
.insert(prioritized_tile
.tile());
210 EXPECT_EQ(all_tiles
, required_for_activation_tiles
);
212 // Check that everything is required for draw.
213 queue
= host_impl_
.BuildRasterQueue(
214 SMOOTHNESS_TAKES_PRIORITY
,
215 RasterTilePriorityQueue::Type::REQUIRED_FOR_DRAW
);
216 std::set
<Tile
*> required_for_draw_tiles
;
217 while (!queue
->IsEmpty()) {
218 PrioritizedTile prioritized_tile
= queue
->Top();
219 EXPECT_TRUE(prioritized_tile
.tile()->required_for_draw());
220 required_for_draw_tiles
.insert(prioritized_tile
.tile());
223 EXPECT_EQ(all_tiles
, required_for_draw_tiles
);
225 Region
invalidation(gfx::Rect(0, 0, 500, 500));
227 // Invalidate the pending tree.
228 pending_layer_
->set_invalidation(invalidation
);
229 pending_layer_
->HighResTiling()->Invalidate(invalidation
);
230 pending_layer_
->LowResTiling()->Invalidate(invalidation
);
232 // Renew all of the tile priorities.
233 gfx::Rect
viewport(50, 50, 100, 100);
234 pending_layer_
->HighResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
236 pending_layer_
->LowResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
238 active_layer_
->HighResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
240 active_layer_
->LowResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
243 // Populate all tiles directly from the tilings.
245 std::set
<Tile
*> high_res_tiles
;
246 std::vector
<Tile
*> pending_high_res_tiles
=
247 pending_layer_
->HighResTiling()->AllTilesForTesting();
248 for (size_t i
= 0; i
< pending_high_res_tiles
.size(); ++i
) {
249 all_tiles
.insert(pending_high_res_tiles
[i
]);
250 high_res_tiles
.insert(pending_high_res_tiles
[i
]);
253 std::vector
<Tile
*> pending_low_res_tiles
=
254 pending_layer_
->LowResTiling()->AllTilesForTesting();
255 for (size_t i
= 0; i
< pending_low_res_tiles
.size(); ++i
)
256 all_tiles
.insert(pending_low_res_tiles
[i
]);
258 std::vector
<Tile
*> active_high_res_tiles
=
259 active_layer_
->HighResTiling()->AllTilesForTesting();
260 for (size_t i
= 0; i
< active_high_res_tiles
.size(); ++i
) {
261 all_tiles
.insert(active_high_res_tiles
[i
]);
262 high_res_tiles
.insert(active_high_res_tiles
[i
]);
265 std::vector
<Tile
*> active_low_res_tiles
=
266 active_layer_
->LowResTiling()->AllTilesForTesting();
267 for (size_t i
= 0; i
< active_low_res_tiles
.size(); ++i
)
268 all_tiles
.insert(active_low_res_tiles
[i
]);
270 PrioritizedTile last_tile
;
271 smoothness_tiles
.clear();
273 size_t correct_order_tiles
= 0u;
274 // Here we expect to get increasing ACTIVE_TREE priority_bin.
275 queue
= host_impl_
.BuildRasterQueue(SMOOTHNESS_TAKES_PRIORITY
,
276 RasterTilePriorityQueue::Type::ALL
);
277 std::set
<Tile
*> expected_required_for_draw_tiles
;
278 std::set
<Tile
*> expected_required_for_activation_tiles
;
279 while (!queue
->IsEmpty()) {
280 PrioritizedTile prioritized_tile
= queue
->Top();
281 EXPECT_TRUE(prioritized_tile
.tile());
283 if (!last_tile
.tile())
284 last_tile
= prioritized_tile
;
286 EXPECT_LE(last_tile
.priority().priority_bin
,
287 prioritized_tile
.priority().priority_bin
);
288 bool skip_updating_last_tile
= false;
289 if (last_tile
.priority().priority_bin
==
290 prioritized_tile
.priority().priority_bin
) {
291 correct_order_tiles
+= last_tile
.priority().distance_to_visible
<=
292 prioritized_tile
.priority().distance_to_visible
;
293 } else if (prioritized_tile
.priority().priority_bin
== TilePriority::NOW
) {
294 // Since we'd return pending tree now tiles before the eventually tiles on
295 // the active tree, update the value.
296 ++correct_order_tiles
;
297 skip_updating_last_tile
= true;
300 if (prioritized_tile
.priority().priority_bin
== TilePriority::NOW
&&
301 last_tile
.priority().resolution
!=
302 prioritized_tile
.priority().resolution
) {
303 // Low resolution should come first.
304 EXPECT_EQ(LOW_RESOLUTION
, last_tile
.priority().resolution
);
307 if (!skip_updating_last_tile
)
308 last_tile
= prioritized_tile
;
310 smoothness_tiles
.insert(prioritized_tile
.tile());
311 if (prioritized_tile
.tile()->required_for_draw())
312 expected_required_for_draw_tiles
.insert(prioritized_tile
.tile());
313 if (prioritized_tile
.tile()->required_for_activation())
314 expected_required_for_activation_tiles
.insert(prioritized_tile
.tile());
318 EXPECT_EQ(tile_count
, smoothness_tiles
.size());
319 EXPECT_EQ(all_tiles
, smoothness_tiles
);
320 // Since we don't guarantee increasing distance due to spiral iterator, we
321 // should check that we're _mostly_ right.
322 EXPECT_GT(correct_order_tiles
, 3 * tile_count
/ 4);
324 // Check that we have consistent required_for_activation tiles.
325 queue
= host_impl_
.BuildRasterQueue(
326 SMOOTHNESS_TAKES_PRIORITY
,
327 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION
);
328 required_for_activation_tiles
.clear();
329 while (!queue
->IsEmpty()) {
330 PrioritizedTile prioritized_tile
= queue
->Top();
331 EXPECT_TRUE(prioritized_tile
.tile()->required_for_activation());
332 required_for_activation_tiles
.insert(prioritized_tile
.tile());
335 EXPECT_EQ(expected_required_for_activation_tiles
,
336 required_for_activation_tiles
);
337 EXPECT_NE(all_tiles
, required_for_activation_tiles
);
339 // Check that we have consistent required_for_draw tiles.
340 queue
= host_impl_
.BuildRasterQueue(
341 SMOOTHNESS_TAKES_PRIORITY
,
342 RasterTilePriorityQueue::Type::REQUIRED_FOR_DRAW
);
343 required_for_draw_tiles
.clear();
344 while (!queue
->IsEmpty()) {
345 PrioritizedTile prioritized_tile
= queue
->Top();
346 EXPECT_TRUE(prioritized_tile
.tile()->required_for_draw());
347 required_for_draw_tiles
.insert(prioritized_tile
.tile());
350 EXPECT_EQ(expected_required_for_draw_tiles
, required_for_draw_tiles
);
351 EXPECT_NE(all_tiles
, required_for_draw_tiles
);
353 std::set
<Tile
*> new_content_tiles
;
354 last_tile
= PrioritizedTile();
355 size_t increasing_distance_tiles
= 0u;
356 // Here we expect to get increasing PENDING_TREE priority_bin.
357 queue
= host_impl_
.BuildRasterQueue(NEW_CONTENT_TAKES_PRIORITY
,
358 RasterTilePriorityQueue::Type::ALL
);
360 while (!queue
->IsEmpty()) {
361 PrioritizedTile prioritized_tile
= queue
->Top();
362 EXPECT_TRUE(prioritized_tile
.tile());
364 if (!last_tile
.tile())
365 last_tile
= prioritized_tile
;
367 EXPECT_LE(last_tile
.priority().priority_bin
,
368 prioritized_tile
.priority().priority_bin
);
369 if (last_tile
.priority().priority_bin
==
370 prioritized_tile
.priority().priority_bin
) {
371 increasing_distance_tiles
+=
372 last_tile
.priority().distance_to_visible
<=
373 prioritized_tile
.priority().distance_to_visible
;
376 if (prioritized_tile
.priority().priority_bin
== TilePriority::NOW
&&
377 last_tile
.priority().resolution
!=
378 prioritized_tile
.priority().resolution
) {
379 // High resolution should come first.
380 EXPECT_EQ(HIGH_RESOLUTION
, last_tile
.priority().resolution
);
383 last_tile
= prioritized_tile
;
384 new_content_tiles
.insert(prioritized_tile
.tile());
389 EXPECT_EQ(tile_count
, new_content_tiles
.size());
390 EXPECT_EQ(high_res_tiles
, new_content_tiles
);
391 // Since we don't guarantee increasing distance due to spiral iterator, we
392 // should check that we're _mostly_ right.
393 EXPECT_GE(increasing_distance_tiles
, 3 * tile_count
/ 4);
395 // Check that we have consistent required_for_activation tiles.
396 queue
= host_impl_
.BuildRasterQueue(
397 NEW_CONTENT_TAKES_PRIORITY
,
398 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION
);
399 required_for_activation_tiles
.clear();
400 while (!queue
->IsEmpty()) {
401 PrioritizedTile prioritized_tile
= queue
->Top();
402 EXPECT_TRUE(prioritized_tile
.tile()->required_for_activation());
403 required_for_activation_tiles
.insert(prioritized_tile
.tile());
406 EXPECT_EQ(expected_required_for_activation_tiles
,
407 required_for_activation_tiles
);
408 EXPECT_NE(new_content_tiles
, required_for_activation_tiles
);
410 // Check that we have consistent required_for_draw tiles.
411 queue
= host_impl_
.BuildRasterQueue(
412 NEW_CONTENT_TAKES_PRIORITY
,
413 RasterTilePriorityQueue::Type::REQUIRED_FOR_DRAW
);
414 required_for_draw_tiles
.clear();
415 while (!queue
->IsEmpty()) {
416 PrioritizedTile prioritized_tile
= queue
->Top();
417 EXPECT_TRUE(prioritized_tile
.tile()->required_for_draw());
418 required_for_draw_tiles
.insert(prioritized_tile
.tile());
421 EXPECT_EQ(expected_required_for_draw_tiles
, required_for_draw_tiles
);
422 EXPECT_NE(new_content_tiles
, required_for_draw_tiles
);
425 TEST_F(TileManagerTilePriorityQueueTest
,
426 RasterTilePriorityQueueHighNonIdealTilings
) {
427 const gfx::Size
layer_bounds(1000, 1000);
428 const gfx::Size
viewport(800, 800);
429 host_impl_
.SetViewportSize(viewport
);
430 SetupDefaultTrees(layer_bounds
);
432 pending_layer_
->tilings()->AddTiling(1.5f
, pending_layer_
->raster_source());
433 active_layer_
->tilings()->AddTiling(1.5f
, active_layer_
->raster_source());
434 pending_layer_
->tilings()->AddTiling(1.7f
, pending_layer_
->raster_source());
435 active_layer_
->tilings()->AddTiling(1.7f
, active_layer_
->raster_source());
437 pending_layer_
->tilings()->UpdateTilePriorities(gfx::Rect(viewport
), 1.f
, 5.0,
439 active_layer_
->tilings()->UpdateTilePriorities(gfx::Rect(viewport
), 1.f
, 5.0,
442 std::set
<Tile
*> all_expected_tiles
;
443 for (size_t i
= 0; i
< pending_layer_
->num_tilings(); ++i
) {
444 PictureLayerTiling
* tiling
= pending_layer_
->tilings()->tiling_at(i
);
445 if (tiling
->contents_scale() == 1.f
) {
446 tiling
->set_resolution(HIGH_RESOLUTION
);
447 const auto& all_tiles
= tiling
->AllTilesForTesting();
448 all_expected_tiles
.insert(all_tiles
.begin(), all_tiles
.end());
450 tiling
->set_resolution(NON_IDEAL_RESOLUTION
);
454 for (size_t i
= 0; i
< active_layer_
->num_tilings(); ++i
) {
455 PictureLayerTiling
* tiling
= active_layer_
->tilings()->tiling_at(i
);
456 if (tiling
->contents_scale() == 1.5f
) {
457 tiling
->set_resolution(HIGH_RESOLUTION
);
458 const auto& all_tiles
= tiling
->AllTilesForTesting();
459 all_expected_tiles
.insert(all_tiles
.begin(), all_tiles
.end());
461 tiling
->set_resolution(NON_IDEAL_RESOLUTION
);
462 // Non ideal tilings with a high res pending twin have to be processed
463 // because of possible activation tiles.
464 if (tiling
->contents_scale() == 1.f
) {
465 tiling
->UpdateAndGetAllPrioritizedTilesForTesting();
466 const auto& all_tiles
= tiling
->AllTilesForTesting();
467 for (auto* tile
: all_tiles
)
468 EXPECT_TRUE(tile
->required_for_activation());
469 all_expected_tiles
.insert(all_tiles
.begin(), all_tiles
.end());
474 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
475 SMOOTHNESS_TAKES_PRIORITY
, RasterTilePriorityQueue::Type::ALL
));
476 EXPECT_FALSE(queue
->IsEmpty());
478 size_t tile_count
= 0;
479 std::set
<Tile
*> all_actual_tiles
;
480 while (!queue
->IsEmpty()) {
481 EXPECT_TRUE(queue
->Top().tile());
482 all_actual_tiles
.insert(queue
->Top().tile());
487 EXPECT_EQ(tile_count
, all_actual_tiles
.size());
488 EXPECT_EQ(all_expected_tiles
.size(), all_actual_tiles
.size());
489 EXPECT_EQ(all_expected_tiles
, all_actual_tiles
);
492 TEST_F(TileManagerTilePriorityQueueTest
, RasterTilePriorityQueueInvalidation
) {
493 const gfx::Size
layer_bounds(1000, 1000);
494 host_impl_
.SetViewportSize(gfx::Size(500, 500));
495 SetupDefaultTrees(layer_bounds
);
497 // Use a tile's content rect as an invalidation. We should inset it a bit to
498 // ensure that border math doesn't invalidate neighbouring tiles.
499 gfx::Rect invalidation
=
500 active_layer_
->HighResTiling()->TileAt(1, 0)->content_rect();
501 invalidation
.Inset(2, 2);
503 pending_layer_
->set_invalidation(invalidation
);
504 pending_layer_
->HighResTiling()->Invalidate(invalidation
);
505 pending_layer_
->HighResTiling()->CreateMissingTilesInLiveTilesRect();
506 pending_layer_
->LowResTiling()->Invalidate(invalidation
);
507 pending_layer_
->LowResTiling()->CreateMissingTilesInLiveTilesRect();
509 // Sanity checks: Tile at 0, 0 not exist on the pending tree (it's not
510 // invalidated). Tile 1, 0 should exist on both.
511 EXPECT_FALSE(pending_layer_
->HighResTiling()->TileAt(0, 0));
512 EXPECT_TRUE(active_layer_
->HighResTiling()->TileAt(0, 0));
513 EXPECT_TRUE(pending_layer_
->HighResTiling()->TileAt(1, 0));
514 EXPECT_TRUE(active_layer_
->HighResTiling()->TileAt(1, 0));
515 EXPECT_NE(pending_layer_
->HighResTiling()->TileAt(1, 0),
516 active_layer_
->HighResTiling()->TileAt(1, 0));
518 std::set
<Tile
*> expected_now_tiles
;
519 std::set
<Tile
*> expected_required_for_draw_tiles
;
520 std::set
<Tile
*> expected_required_for_activation_tiles
;
521 for (int i
= 0; i
<= 1; ++i
) {
522 for (int j
= 0; j
<= 1; ++j
) {
523 bool have_pending_tile
= false;
524 if (pending_layer_
->HighResTiling()->TileAt(i
, j
)) {
525 expected_now_tiles
.insert(
526 pending_layer_
->HighResTiling()->TileAt(i
, j
));
527 expected_required_for_activation_tiles
.insert(
528 pending_layer_
->HighResTiling()->TileAt(i
, j
));
529 have_pending_tile
= true;
531 Tile
* active_tile
= active_layer_
->HighResTiling()->TileAt(i
, j
);
532 EXPECT_TRUE(active_tile
);
533 expected_now_tiles
.insert(active_tile
);
534 expected_required_for_draw_tiles
.insert(active_tile
);
535 if (!have_pending_tile
)
536 expected_required_for_activation_tiles
.insert(active_tile
);
539 // Expect 3 shared tiles and 1 unshared tile in total.
540 EXPECT_EQ(5u, expected_now_tiles
.size());
541 // Expect 4 tiles for each draw and activation, but not all the same.
542 EXPECT_EQ(4u, expected_required_for_activation_tiles
.size());
543 EXPECT_EQ(4u, expected_required_for_draw_tiles
.size());
544 EXPECT_NE(expected_required_for_draw_tiles
,
545 expected_required_for_activation_tiles
);
547 std::set
<Tile
*> expected_all_tiles
;
548 for (int i
= 0; i
<= 3; ++i
) {
549 for (int j
= 0; j
<= 3; ++j
) {
550 if (pending_layer_
->HighResTiling()->TileAt(i
, j
))
551 expected_all_tiles
.insert(
552 pending_layer_
->HighResTiling()->TileAt(i
, j
));
553 EXPECT_TRUE(active_layer_
->HighResTiling()->TileAt(i
, j
));
554 expected_all_tiles
.insert(active_layer_
->HighResTiling()->TileAt(i
, j
));
557 // Expect 15 shared tiles and 1 unshared tile.
558 EXPECT_EQ(17u, expected_all_tiles
.size());
560 // The actual test will now build different queues and verify that the queues
561 // return the same information as computed manually above.
562 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
563 SAME_PRIORITY_FOR_BOTH_TREES
, RasterTilePriorityQueue::Type::ALL
));
564 std::set
<Tile
*> actual_now_tiles
;
565 std::set
<Tile
*> actual_all_tiles
;
566 while (!queue
->IsEmpty()) {
567 PrioritizedTile prioritized_tile
= queue
->Top();
569 if (prioritized_tile
.priority().priority_bin
== TilePriority::NOW
)
570 actual_now_tiles
.insert(prioritized_tile
.tile());
571 actual_all_tiles
.insert(prioritized_tile
.tile());
573 EXPECT_EQ(expected_now_tiles
, actual_now_tiles
);
574 EXPECT_EQ(expected_all_tiles
, actual_all_tiles
);
576 queue
= host_impl_
.BuildRasterQueue(
577 SAME_PRIORITY_FOR_BOTH_TREES
,
578 RasterTilePriorityQueue::Type::REQUIRED_FOR_DRAW
);
579 std::set
<Tile
*> actual_required_for_draw_tiles
;
580 while (!queue
->IsEmpty()) {
581 PrioritizedTile prioritized_tile
= queue
->Top();
583 actual_required_for_draw_tiles
.insert(prioritized_tile
.tile());
585 EXPECT_EQ(expected_required_for_draw_tiles
, actual_required_for_draw_tiles
);
587 queue
= host_impl_
.BuildRasterQueue(
588 SAME_PRIORITY_FOR_BOTH_TREES
,
589 RasterTilePriorityQueue::Type::REQUIRED_FOR_ACTIVATION
);
590 std::set
<Tile
*> actual_required_for_activation_tiles
;
591 while (!queue
->IsEmpty()) {
592 Tile
* tile
= queue
->Top().tile();
594 actual_required_for_activation_tiles
.insert(tile
);
596 EXPECT_EQ(expected_required_for_activation_tiles
,
597 actual_required_for_activation_tiles
);
600 TEST_F(TileManagerTilePriorityQueueTest
, ActivationComesBeforeEventually
) {
601 base::TimeTicks time_ticks
;
602 time_ticks
+= base::TimeDelta::FromMilliseconds(1);
603 host_impl_
.SetCurrentBeginFrameArgs(
604 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, time_ticks
));
606 gfx::Size
layer_bounds(1000, 1000);
607 SetupDefaultTrees(layer_bounds
);
609 // Create a pending child layer.
610 gfx::Size
tile_size(256, 256);
611 scoped_refptr
<FakePicturePileImpl
> pending_pile
=
612 FakePicturePileImpl::CreateFilledPile(tile_size
, layer_bounds
);
613 scoped_ptr
<FakePictureLayerImpl
> pending_child
=
614 FakePictureLayerImpl::CreateWithRasterSource(host_impl_
.pending_tree(),
615 id_
+ 1, pending_pile
);
616 FakePictureLayerImpl
* pending_child_raw
= pending_child
.get();
617 pending_child_raw
->SetDrawsContent(true);
618 pending_layer_
->AddChild(pending_child
.Pass());
620 // Set a small viewport, so we have soon and eventually tiles.
621 host_impl_
.SetViewportSize(gfx::Size(200, 200));
622 time_ticks
+= base::TimeDelta::FromMilliseconds(1);
623 host_impl_
.SetCurrentBeginFrameArgs(
624 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, time_ticks
));
625 bool update_lcd_text
= false;
626 host_impl_
.pending_tree()->UpdateDrawProperties(update_lcd_text
);
628 host_impl_
.SetRequiresHighResToDraw();
629 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
630 SMOOTHNESS_TAKES_PRIORITY
, RasterTilePriorityQueue::Type::ALL
));
631 EXPECT_FALSE(queue
->IsEmpty());
633 // Get all the tiles that are NOW or SOON and make sure they are ready to
635 std::vector
<Tile
*> all_tiles
;
636 while (!queue
->IsEmpty()) {
637 PrioritizedTile prioritized_tile
= queue
->Top();
638 if (prioritized_tile
.priority().priority_bin
>= TilePriority::EVENTUALLY
)
641 all_tiles
.push_back(prioritized_tile
.tile());
645 tile_manager()->InitializeTilesWithResourcesForTesting(
646 std::vector
<Tile
*>(all_tiles
.begin(), all_tiles
.end()));
648 // Ensure we can activate.
649 EXPECT_TRUE(tile_manager()->IsReadyToActivate());
652 TEST_F(TileManagerTilePriorityQueueTest
, EvictionTilePriorityQueue
) {
653 const gfx::Size
layer_bounds(1000, 1000);
654 host_impl_
.SetViewportSize(layer_bounds
);
655 SetupDefaultTrees(layer_bounds
);
657 scoped_ptr
<EvictionTilePriorityQueue
> empty_queue(
658 host_impl_
.BuildEvictionQueue(SAME_PRIORITY_FOR_BOTH_TREES
));
659 EXPECT_TRUE(empty_queue
->IsEmpty());
660 std::set
<Tile
*> all_tiles
;
661 size_t tile_count
= 0;
663 scoped_ptr
<RasterTilePriorityQueue
> raster_queue(host_impl_
.BuildRasterQueue(
664 SAME_PRIORITY_FOR_BOTH_TREES
, RasterTilePriorityQueue::Type::ALL
));
665 while (!raster_queue
->IsEmpty()) {
667 EXPECT_TRUE(raster_queue
->Top().tile());
668 all_tiles
.insert(raster_queue
->Top().tile());
672 EXPECT_EQ(tile_count
, all_tiles
.size());
673 EXPECT_EQ(16u, tile_count
);
675 tile_manager()->InitializeTilesWithResourcesForTesting(
676 std::vector
<Tile
*>(all_tiles
.begin(), all_tiles
.end()));
678 scoped_ptr
<EvictionTilePriorityQueue
> queue(
679 host_impl_
.BuildEvictionQueue(SMOOTHNESS_TAKES_PRIORITY
));
680 EXPECT_FALSE(queue
->IsEmpty());
682 // Sanity check, all tiles should be visible.
683 std::set
<Tile
*> smoothness_tiles
;
684 while (!queue
->IsEmpty()) {
685 PrioritizedTile prioritized_tile
= queue
->Top();
686 EXPECT_TRUE(prioritized_tile
.tile());
687 EXPECT_EQ(TilePriority::NOW
, prioritized_tile
.priority().priority_bin
);
688 EXPECT_TRUE(prioritized_tile
.tile()->draw_info().has_resource());
689 smoothness_tiles
.insert(prioritized_tile
.tile());
692 EXPECT_EQ(all_tiles
, smoothness_tiles
);
694 tile_manager()->ReleaseTileResourcesForTesting(
695 std::vector
<Tile
*>(all_tiles
.begin(), all_tiles
.end()));
697 Region
invalidation(gfx::Rect(0, 0, 500, 500));
699 // Invalidate the pending tree.
700 pending_layer_
->set_invalidation(invalidation
);
701 pending_layer_
->HighResTiling()->Invalidate(invalidation
);
702 pending_layer_
->HighResTiling()->CreateMissingTilesInLiveTilesRect();
703 pending_layer_
->LowResTiling()->Invalidate(invalidation
);
704 pending_layer_
->LowResTiling()->CreateMissingTilesInLiveTilesRect();
706 // Renew all of the tile priorities.
707 gfx::Rect
viewport(50, 50, 100, 100);
708 pending_layer_
->HighResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
710 pending_layer_
->LowResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
712 active_layer_
->HighResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
714 active_layer_
->LowResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
717 // Populate all tiles directly from the tilings.
719 std::vector
<Tile
*> pending_high_res_tiles
=
720 pending_layer_
->HighResTiling()->AllTilesForTesting();
721 for (size_t i
= 0; i
< pending_high_res_tiles
.size(); ++i
)
722 all_tiles
.insert(pending_high_res_tiles
[i
]);
724 std::vector
<Tile
*> pending_low_res_tiles
=
725 pending_layer_
->LowResTiling()->AllTilesForTesting();
726 for (size_t i
= 0; i
< pending_low_res_tiles
.size(); ++i
)
727 all_tiles
.insert(pending_low_res_tiles
[i
]);
729 std::vector
<Tile
*> active_high_res_tiles
=
730 active_layer_
->HighResTiling()->AllTilesForTesting();
731 for (size_t i
= 0; i
< active_high_res_tiles
.size(); ++i
)
732 all_tiles
.insert(active_high_res_tiles
[i
]);
734 std::vector
<Tile
*> active_low_res_tiles
=
735 active_layer_
->LowResTiling()->AllTilesForTesting();
736 for (size_t i
= 0; i
< active_low_res_tiles
.size(); ++i
)
737 all_tiles
.insert(active_low_res_tiles
[i
]);
739 tile_manager()->InitializeTilesWithResourcesForTesting(
740 std::vector
<Tile
*>(all_tiles
.begin(), all_tiles
.end()));
742 PrioritizedTile last_tile
;
743 smoothness_tiles
.clear();
745 // Here we expect to get increasing combined priority_bin.
746 queue
= host_impl_
.BuildEvictionQueue(SMOOTHNESS_TAKES_PRIORITY
);
747 int distance_increasing
= 0;
748 int distance_decreasing
= 0;
749 while (!queue
->IsEmpty()) {
750 PrioritizedTile prioritized_tile
= queue
->Top();
751 Tile
* tile
= prioritized_tile
.tile();
753 EXPECT_TRUE(tile
->draw_info().has_resource());
755 if (!last_tile
.tile())
756 last_tile
= prioritized_tile
;
758 const TilePriority
& last_priority
= last_tile
.priority();
759 const TilePriority
& priority
= prioritized_tile
.priority();
761 EXPECT_GE(last_priority
.priority_bin
, priority
.priority_bin
);
762 if (last_priority
.priority_bin
== priority
.priority_bin
) {
763 EXPECT_LE(last_tile
.tile()->required_for_activation(),
764 tile
->required_for_activation());
765 if (last_tile
.tile()->required_for_activation() ==
766 tile
->required_for_activation()) {
767 if (last_priority
.distance_to_visible
>= priority
.distance_to_visible
)
768 ++distance_decreasing
;
770 ++distance_increasing
;
774 last_tile
= prioritized_tile
;
776 smoothness_tiles
.insert(tile
);
780 // Ensure that the distance is decreasing many more times than increasing.
781 EXPECT_EQ(3, distance_increasing
);
782 EXPECT_EQ(17, distance_decreasing
);
783 EXPECT_EQ(tile_count
, smoothness_tiles
.size());
784 EXPECT_EQ(all_tiles
, smoothness_tiles
);
786 std::set
<Tile
*> new_content_tiles
;
787 last_tile
= PrioritizedTile();
788 // Again, we expect to get increasing combined priority_bin.
789 queue
= host_impl_
.BuildEvictionQueue(NEW_CONTENT_TAKES_PRIORITY
);
790 distance_decreasing
= 0;
791 distance_increasing
= 0;
792 while (!queue
->IsEmpty()) {
793 PrioritizedTile prioritized_tile
= queue
->Top();
794 Tile
* tile
= prioritized_tile
.tile();
797 if (!last_tile
.tile())
798 last_tile
= prioritized_tile
;
800 const TilePriority
& last_priority
= last_tile
.priority();
801 const TilePriority
& priority
= prioritized_tile
.priority();
803 EXPECT_GE(last_priority
.priority_bin
, priority
.priority_bin
);
804 if (last_priority
.priority_bin
== priority
.priority_bin
) {
805 EXPECT_LE(last_tile
.tile()->required_for_activation(),
806 tile
->required_for_activation());
807 if (last_tile
.tile()->required_for_activation() ==
808 tile
->required_for_activation()) {
809 if (last_priority
.distance_to_visible
>= priority
.distance_to_visible
)
810 ++distance_decreasing
;
812 ++distance_increasing
;
816 last_tile
= prioritized_tile
;
817 new_content_tiles
.insert(tile
);
821 // Ensure that the distance is decreasing many more times than increasing.
822 EXPECT_EQ(3, distance_increasing
);
823 EXPECT_EQ(17, distance_decreasing
);
824 EXPECT_EQ(tile_count
, new_content_tiles
.size());
825 EXPECT_EQ(all_tiles
, new_content_tiles
);
828 TEST_F(TileManagerTilePriorityQueueTest
,
829 EvictionTilePriorityQueueWithOcclusion
) {
830 base::TimeTicks time_ticks
;
831 time_ticks
+= base::TimeDelta::FromMilliseconds(1);
832 host_impl_
.SetCurrentBeginFrameArgs(
833 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, time_ticks
));
835 gfx::Size
tile_size(102, 102);
836 gfx::Size
layer_bounds(1000, 1000);
838 host_impl_
.SetViewportSize(layer_bounds
);
840 scoped_refptr
<FakePicturePileImpl
> pending_pile
=
841 FakePicturePileImpl::CreateFilledPile(tile_size
, layer_bounds
);
842 SetupPendingTree(pending_pile
);
844 scoped_ptr
<FakePictureLayerImpl
> pending_child
=
845 FakePictureLayerImpl::CreateWithRasterSource(host_impl_
.pending_tree(), 2,
847 pending_layer_
->AddChild(pending_child
.Pass());
849 FakePictureLayerImpl
* pending_child_layer
=
850 static_cast<FakePictureLayerImpl
*>(pending_layer_
->children()[0]);
851 pending_child_layer
->SetDrawsContent(true);
853 time_ticks
+= base::TimeDelta::FromMilliseconds(1);
854 host_impl_
.SetCurrentBeginFrameArgs(
855 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, time_ticks
));
856 bool update_lcd_text
= false;
857 host_impl_
.pending_tree()->UpdateDrawProperties(update_lcd_text
);
860 SetupPendingTree(pending_pile
);
862 FakePictureLayerImpl
* active_child_layer
=
863 static_cast<FakePictureLayerImpl
*>(active_layer_
->children()[0]);
865 std::set
<Tile
*> all_tiles
;
866 size_t tile_count
= 0;
867 scoped_ptr
<RasterTilePriorityQueue
> raster_queue(host_impl_
.BuildRasterQueue(
868 SAME_PRIORITY_FOR_BOTH_TREES
, RasterTilePriorityQueue::Type::ALL
));
869 while (!raster_queue
->IsEmpty()) {
871 EXPECT_TRUE(raster_queue
->Top().tile());
872 all_tiles
.insert(raster_queue
->Top().tile());
875 EXPECT_EQ(tile_count
, all_tiles
.size());
876 EXPECT_EQ(32u, tile_count
);
878 // Renew all of the tile priorities.
879 gfx::Rect
viewport(layer_bounds
);
880 pending_layer_
->HighResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
882 pending_layer_
->LowResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
884 pending_child_layer
->HighResTiling()->ComputeTilePriorityRects(
885 viewport
, 1.0f
, 1.0, Occlusion());
886 pending_child_layer
->LowResTiling()->ComputeTilePriorityRects(
887 viewport
, 1.0f
, 1.0, Occlusion());
889 active_layer_
->HighResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
891 active_layer_
->LowResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
893 active_child_layer
->HighResTiling()->ComputeTilePriorityRects(
894 viewport
, 1.0f
, 1.0, Occlusion());
895 active_child_layer
->LowResTiling()->ComputeTilePriorityRects(
896 viewport
, 1.0f
, 1.0, Occlusion());
898 // Populate all tiles directly from the tilings.
900 std::vector
<Tile
*> pending_high_res_tiles
=
901 pending_layer_
->HighResTiling()->AllTilesForTesting();
902 all_tiles
.insert(pending_high_res_tiles
.begin(),
903 pending_high_res_tiles
.end());
905 std::vector
<Tile
*> pending_low_res_tiles
=
906 pending_layer_
->LowResTiling()->AllTilesForTesting();
907 all_tiles
.insert(pending_low_res_tiles
.begin(), pending_low_res_tiles
.end());
909 // Set all tiles on the pending_child_layer as occluded on the pending tree.
910 std::vector
<Tile
*> pending_child_high_res_tiles
=
911 pending_child_layer
->HighResTiling()->AllTilesForTesting();
912 pending_child_layer
->HighResTiling()->SetAllTilesOccludedForTesting();
913 active_child_layer
->HighResTiling()->SetAllTilesOccludedForTesting();
914 all_tiles
.insert(pending_child_high_res_tiles
.begin(),
915 pending_child_high_res_tiles
.end());
917 std::vector
<Tile
*> pending_child_low_res_tiles
=
918 pending_child_layer
->LowResTiling()->AllTilesForTesting();
919 pending_child_layer
->LowResTiling()->SetAllTilesOccludedForTesting();
920 active_child_layer
->LowResTiling()->SetAllTilesOccludedForTesting();
921 all_tiles
.insert(pending_child_low_res_tiles
.begin(),
922 pending_child_low_res_tiles
.end());
924 tile_manager()->InitializeTilesWithResourcesForTesting(
925 std::vector
<Tile
*>(all_tiles
.begin(), all_tiles
.end()));
927 // Verify occlusion is considered by EvictionTilePriorityQueue.
928 TreePriority tree_priority
= NEW_CONTENT_TAKES_PRIORITY
;
929 size_t occluded_count
= 0u;
930 PrioritizedTile last_tile
;
931 scoped_ptr
<EvictionTilePriorityQueue
> queue(
932 host_impl_
.BuildEvictionQueue(tree_priority
));
933 while (!queue
->IsEmpty()) {
934 PrioritizedTile prioritized_tile
= queue
->Top();
935 if (!last_tile
.tile())
936 last_tile
= prioritized_tile
;
938 bool tile_is_occluded
= prioritized_tile
.is_occluded();
940 // The only way we will encounter an occluded tile after an unoccluded
941 // tile is if the priorty bin decreased, the tile is required for
942 // activation, or the scale changed.
943 if (tile_is_occluded
) {
946 bool last_tile_is_occluded
= last_tile
.is_occluded();
947 if (!last_tile_is_occluded
) {
948 TilePriority::PriorityBin tile_priority_bin
=
949 prioritized_tile
.priority().priority_bin
;
950 TilePriority::PriorityBin last_tile_priority_bin
=
951 last_tile
.priority().priority_bin
;
953 EXPECT_TRUE((tile_priority_bin
< last_tile_priority_bin
) ||
954 prioritized_tile
.tile()->required_for_activation() ||
955 (prioritized_tile
.tile()->contents_scale() !=
956 last_tile
.tile()->contents_scale()));
959 last_tile
= prioritized_tile
;
962 size_t expected_occluded_count
=
963 pending_child_high_res_tiles
.size() + pending_child_low_res_tiles
.size();
964 EXPECT_EQ(expected_occluded_count
, occluded_count
);
967 TEST_F(TileManagerTilePriorityQueueTest
,
968 EvictionTilePriorityQueueWithTransparentLayer
) {
969 base::TimeTicks time_ticks
;
970 time_ticks
+= base::TimeDelta::FromMilliseconds(1);
971 host_impl_
.SetCurrentBeginFrameArgs(
972 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, time_ticks
));
974 gfx::Size
tile_size(102, 102);
975 gfx::Size
layer_bounds(1000, 1000);
977 scoped_refptr
<FakePicturePileImpl
> pending_pile
=
978 FakePicturePileImpl::CreateFilledPile(tile_size
, layer_bounds
);
979 SetupPendingTree(pending_pile
);
981 scoped_ptr
<FakePictureLayerImpl
> pending_child
=
982 FakePictureLayerImpl::CreateWithRasterSource(host_impl_
.pending_tree(), 2,
984 FakePictureLayerImpl
* pending_child_layer
= pending_child
.get();
985 pending_layer_
->AddChild(pending_child
.Pass());
987 // Create a fully transparent child layer so that its tile priorities are not
988 // considered to be valid.
989 pending_child_layer
->SetDrawsContent(true);
991 time_ticks
+= base::TimeDelta::FromMilliseconds(1);
992 host_impl_
.SetCurrentBeginFrameArgs(
993 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, time_ticks
));
994 bool update_lcd_text
= false;
995 host_impl_
.pending_tree()->UpdateDrawProperties(update_lcd_text
);
997 pending_child_layer
->SetOpacity(0.0);
999 time_ticks
+= base::TimeDelta::FromMilliseconds(1);
1000 host_impl_
.SetCurrentBeginFrameArgs(
1001 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
, time_ticks
));
1002 host_impl_
.pending_tree()->UpdateDrawProperties(update_lcd_text
);
1004 // Renew all of the tile priorities.
1005 gfx::Rect
viewport(layer_bounds
);
1006 pending_layer_
->HighResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
1008 pending_layer_
->LowResTiling()->ComputeTilePriorityRects(viewport
, 1.0f
, 1.0,
1010 pending_child_layer
->HighResTiling()->ComputeTilePriorityRects(
1011 viewport
, 1.0f
, 1.0, Occlusion());
1012 pending_child_layer
->LowResTiling()->ComputeTilePriorityRects(
1013 viewport
, 1.0f
, 1.0, Occlusion());
1015 // Populate all tiles directly from the tilings.
1016 std::set
<Tile
*> all_pending_tiles
;
1017 std::vector
<Tile
*> pending_high_res_tiles
=
1018 pending_layer_
->HighResTiling()->AllTilesForTesting();
1019 all_pending_tiles
.insert(pending_high_res_tiles
.begin(),
1020 pending_high_res_tiles
.end());
1021 EXPECT_EQ(16u, pending_high_res_tiles
.size());
1023 std::vector
<Tile
*> pending_low_res_tiles
=
1024 pending_layer_
->LowResTiling()->AllTilesForTesting();
1025 all_pending_tiles
.insert(pending_low_res_tiles
.begin(),
1026 pending_low_res_tiles
.end());
1027 EXPECT_EQ(1u, pending_low_res_tiles
.size());
1029 std::set
<Tile
*> all_pending_child_tiles
;
1030 std::vector
<Tile
*> pending_child_high_res_tiles
=
1031 pending_child_layer
->HighResTiling()->AllTilesForTesting();
1032 all_pending_child_tiles
.insert(pending_child_high_res_tiles
.begin(),
1033 pending_child_high_res_tiles
.end());
1034 EXPECT_EQ(16u, pending_child_high_res_tiles
.size());
1036 std::vector
<Tile
*> pending_child_low_res_tiles
=
1037 pending_child_layer
->LowResTiling()->AllTilesForTesting();
1038 all_pending_child_tiles
.insert(pending_child_low_res_tiles
.begin(),
1039 pending_child_low_res_tiles
.end());
1040 EXPECT_EQ(1u, pending_child_low_res_tiles
.size());
1042 std::set
<Tile
*> all_tiles
= all_pending_tiles
;
1043 all_tiles
.insert(all_pending_child_tiles
.begin(),
1044 all_pending_child_tiles
.end());
1046 tile_manager()->InitializeTilesWithResourcesForTesting(
1047 std::vector
<Tile
*>(all_tiles
.begin(), all_tiles
.end()));
1049 EXPECT_TRUE(pending_layer_
->HasValidTilePriorities());
1050 EXPECT_FALSE(pending_child_layer
->HasValidTilePriorities());
1052 // Verify that eviction queue returns tiles also from layers without valid
1053 // tile priorities and that the tile priority bin of those tiles is (at most)
1055 TreePriority tree_priority
= NEW_CONTENT_TAKES_PRIORITY
;
1056 std::set
<Tile
*> new_content_tiles
;
1057 size_t tile_count
= 0;
1058 scoped_ptr
<EvictionTilePriorityQueue
> queue(
1059 host_impl_
.BuildEvictionQueue(tree_priority
));
1060 while (!queue
->IsEmpty()) {
1061 PrioritizedTile prioritized_tile
= queue
->Top();
1062 Tile
* tile
= prioritized_tile
.tile();
1063 const TilePriority
& pending_priority
= prioritized_tile
.priority();
1064 EXPECT_NE(std::numeric_limits
<float>::infinity(),
1065 pending_priority
.distance_to_visible
);
1066 if (all_pending_child_tiles
.find(tile
) != all_pending_child_tiles
.end())
1067 EXPECT_EQ(TilePriority::EVENTUALLY
, pending_priority
.priority_bin
);
1069 EXPECT_EQ(TilePriority::NOW
, pending_priority
.priority_bin
);
1070 new_content_tiles
.insert(tile
);
1074 EXPECT_EQ(tile_count
, new_content_tiles
.size());
1075 EXPECT_EQ(all_tiles
, new_content_tiles
);
1078 TEST_F(TileManagerTilePriorityQueueTest
, RasterTilePriorityQueueEmptyLayers
) {
1079 const gfx::Size
layer_bounds(1000, 1000);
1080 host_impl_
.SetViewportSize(layer_bounds
);
1081 SetupDefaultTrees(layer_bounds
);
1083 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
1084 SAME_PRIORITY_FOR_BOTH_TREES
, RasterTilePriorityQueue::Type::ALL
));
1085 EXPECT_FALSE(queue
->IsEmpty());
1087 size_t tile_count
= 0;
1088 std::set
<Tile
*> all_tiles
;
1089 while (!queue
->IsEmpty()) {
1090 EXPECT_TRUE(queue
->Top().tile());
1091 all_tiles
.insert(queue
->Top().tile());
1096 EXPECT_EQ(tile_count
, all_tiles
.size());
1097 EXPECT_EQ(16u, tile_count
);
1099 for (int i
= 1; i
< 10; ++i
) {
1100 scoped_ptr
<FakePictureLayerImpl
> pending_layer
=
1101 FakePictureLayerImpl::Create(host_impl_
.pending_tree(), id_
+ i
);
1102 pending_layer
->SetDrawsContent(true);
1103 pending_layer
->set_has_valid_tile_priorities(true);
1104 pending_layer_
->AddChild(pending_layer
.Pass());
1107 queue
= host_impl_
.BuildRasterQueue(SAME_PRIORITY_FOR_BOTH_TREES
,
1108 RasterTilePriorityQueue::Type::ALL
);
1109 EXPECT_FALSE(queue
->IsEmpty());
1113 while (!queue
->IsEmpty()) {
1114 EXPECT_TRUE(queue
->Top().tile());
1115 all_tiles
.insert(queue
->Top().tile());
1119 EXPECT_EQ(tile_count
, all_tiles
.size());
1120 EXPECT_EQ(16u, tile_count
);
1123 TEST_F(TileManagerTilePriorityQueueTest
, EvictionTilePriorityQueueEmptyLayers
) {
1124 const gfx::Size
layer_bounds(1000, 1000);
1125 host_impl_
.SetViewportSize(layer_bounds
);
1126 SetupDefaultTrees(layer_bounds
);
1128 scoped_ptr
<RasterTilePriorityQueue
> raster_queue(host_impl_
.BuildRasterQueue(
1129 SAME_PRIORITY_FOR_BOTH_TREES
, RasterTilePriorityQueue::Type::ALL
));
1130 EXPECT_FALSE(raster_queue
->IsEmpty());
1132 size_t tile_count
= 0;
1133 std::set
<Tile
*> all_tiles
;
1134 while (!raster_queue
->IsEmpty()) {
1135 EXPECT_TRUE(raster_queue
->Top().tile());
1136 all_tiles
.insert(raster_queue
->Top().tile());
1138 raster_queue
->Pop();
1140 EXPECT_EQ(tile_count
, all_tiles
.size());
1141 EXPECT_EQ(16u, tile_count
);
1143 std::vector
<Tile
*> tiles(all_tiles
.begin(), all_tiles
.end());
1144 host_impl_
.tile_manager()->InitializeTilesWithResourcesForTesting(tiles
);
1146 for (int i
= 1; i
< 10; ++i
) {
1147 scoped_ptr
<FakePictureLayerImpl
> pending_layer
=
1148 FakePictureLayerImpl::Create(host_impl_
.pending_tree(), id_
+ i
);
1149 pending_layer
->SetDrawsContent(true);
1150 pending_layer
->set_has_valid_tile_priorities(true);
1151 pending_layer_
->AddChild(pending_layer
.Pass());
1154 scoped_ptr
<EvictionTilePriorityQueue
> queue(
1155 host_impl_
.BuildEvictionQueue(SAME_PRIORITY_FOR_BOTH_TREES
));
1156 EXPECT_FALSE(queue
->IsEmpty());
1160 while (!queue
->IsEmpty()) {
1161 EXPECT_TRUE(queue
->Top().tile());
1162 all_tiles
.insert(queue
->Top().tile());
1166 EXPECT_EQ(tile_count
, all_tiles
.size());
1167 EXPECT_EQ(16u, tile_count
);
1170 TEST_F(TileManagerTilePriorityQueueTest
,
1171 RasterTilePriorityQueueStaticViewport
) {
1172 FakePictureLayerTilingClient client
;
1174 gfx::Rect
viewport(50, 50, 500, 500);
1175 gfx::Size
layer_bounds(1600, 1600);
1177 float inset
= PictureLayerTiling::CalculateSoonBorderDistance(viewport
, 1.0f
);
1178 gfx::Rect soon_rect
= viewport
;
1179 soon_rect
.Inset(-inset
, -inset
);
1181 client
.SetTileSize(gfx::Size(30, 30));
1182 LayerTreeSettings settings
;
1183 settings
.tiling_interest_area_viewport_multiplier
= 10000;
1185 scoped_ptr
<PictureLayerTilingSet
> tiling_set
= PictureLayerTilingSet::Create(
1186 ACTIVE_TREE
, &client
, settings
.tiling_interest_area_viewport_multiplier
,
1187 settings
.skewport_target_time_in_seconds
,
1188 settings
.skewport_extrapolation_limit_in_content_pixels
);
1190 scoped_refptr
<FakePicturePileImpl
> pile
=
1191 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds
);
1192 PictureLayerTiling
* tiling
= tiling_set
->AddTiling(1.0f
, pile
);
1193 tiling
->set_resolution(HIGH_RESOLUTION
);
1195 tiling_set
->UpdateTilePriorities(viewport
, 1.0f
, 1.0, Occlusion(), true);
1196 std::vector
<Tile
*> all_tiles
= tiling
->AllTilesForTesting();
1198 EXPECT_EQ(3364u, all_tiles
.size());
1200 // The explanation of each iteration is as follows:
1201 // 1. First iteration tests that we can get all of the tiles correctly.
1202 // 2. Second iteration ensures that we can get all of the tiles again (first
1203 // iteration didn't change any tiles), as well set all tiles to be ready to
1205 // 3. Third iteration ensures that no tiles are returned, since they were all
1206 // marked as ready to draw.
1207 for (int i
= 0; i
< 3; ++i
) {
1208 scoped_ptr
<TilingSetRasterQueueAll
> queue(
1209 new TilingSetRasterQueueAll(tiling_set
.get(), false));
1211 // There are 3 bins in TilePriority.
1212 bool have_tiles
[3] = {};
1214 // On the third iteration, we should get no tiles since everything was
1215 // marked as ready to draw.
1217 EXPECT_TRUE(queue
->IsEmpty());
1221 EXPECT_FALSE(queue
->IsEmpty());
1222 std::set
<Tile
*> unique_tiles
;
1223 unique_tiles
.insert(queue
->Top().tile());
1224 PrioritizedTile last_tile
= queue
->Top();
1225 have_tiles
[last_tile
.priority().priority_bin
] = true;
1227 // On the second iteration, mark everything as ready to draw (solid color).
1229 TileDrawInfo
& draw_info
= last_tile
.tile()->draw_info();
1230 draw_info
.SetSolidColorForTesting(SK_ColorRED
);
1233 int eventually_bin_order_correct_count
= 0;
1234 int eventually_bin_order_incorrect_count
= 0;
1235 while (!queue
->IsEmpty()) {
1236 PrioritizedTile new_tile
= queue
->Top();
1238 unique_tiles
.insert(new_tile
.tile());
1240 TilePriority last_priority
= last_tile
.priority();
1241 TilePriority new_priority
= new_tile
.priority();
1242 EXPECT_LE(last_priority
.priority_bin
, new_priority
.priority_bin
);
1243 if (last_priority
.priority_bin
== new_priority
.priority_bin
) {
1244 if (last_priority
.priority_bin
== TilePriority::EVENTUALLY
) {
1245 bool order_correct
= last_priority
.distance_to_visible
<=
1246 new_priority
.distance_to_visible
;
1247 eventually_bin_order_correct_count
+= order_correct
;
1248 eventually_bin_order_incorrect_count
+= !order_correct
;
1249 } else if (!soon_rect
.Intersects(new_tile
.tile()->content_rect()) &&
1250 !soon_rect
.Intersects(last_tile
.tile()->content_rect())) {
1251 EXPECT_LE(last_priority
.distance_to_visible
,
1252 new_priority
.distance_to_visible
);
1253 EXPECT_EQ(TilePriority::NOW
, new_priority
.priority_bin
);
1254 } else if (new_priority
.distance_to_visible
> 0.f
) {
1255 EXPECT_EQ(TilePriority::SOON
, new_priority
.priority_bin
);
1258 have_tiles
[new_priority
.priority_bin
] = true;
1260 last_tile
= new_tile
;
1262 // On the second iteration, mark everything as ready to draw (solid
1265 TileDrawInfo
& draw_info
= last_tile
.tile()->draw_info();
1266 draw_info
.SetSolidColorForTesting(SK_ColorRED
);
1270 EXPECT_GT(eventually_bin_order_correct_count
,
1271 eventually_bin_order_incorrect_count
);
1273 // We should have now and eventually tiles, as well as soon tiles from
1274 // the border region.
1275 EXPECT_TRUE(have_tiles
[TilePriority::NOW
]);
1276 EXPECT_TRUE(have_tiles
[TilePriority::SOON
]);
1277 EXPECT_TRUE(have_tiles
[TilePriority::EVENTUALLY
]);
1279 EXPECT_EQ(unique_tiles
.size(), all_tiles
.size());
1283 TEST_F(TileManagerTilePriorityQueueTest
,
1284 RasterTilePriorityQueueMovingViewport
) {
1285 FakePictureLayerTilingClient client
;
1287 gfx::Rect
viewport(50, 0, 100, 100);
1288 gfx::Rect
moved_viewport(50, 0, 100, 500);
1289 gfx::Size
layer_bounds(1000, 1000);
1291 client
.SetTileSize(gfx::Size(30, 30));
1292 LayerTreeSettings settings
;
1294 scoped_ptr
<PictureLayerTilingSet
> tiling_set
= PictureLayerTilingSet::Create(
1295 ACTIVE_TREE
, &client
, settings
.tiling_interest_area_viewport_multiplier
,
1296 settings
.skewport_target_time_in_seconds
,
1297 settings
.skewport_extrapolation_limit_in_content_pixels
);
1299 scoped_refptr
<FakePicturePileImpl
> pile
=
1300 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds
);
1301 PictureLayerTiling
* tiling
= tiling_set
->AddTiling(1.0f
, pile
);
1302 tiling
->set_resolution(HIGH_RESOLUTION
);
1304 tiling_set
->UpdateTilePriorities(viewport
, 1.0f
, 1.0, Occlusion(), true);
1305 tiling_set
->UpdateTilePriorities(moved_viewport
, 1.0f
, 2.0, Occlusion(),
1309 PictureLayerTiling::CalculateSoonBorderDistance(moved_viewport
, 1.0f
);
1310 gfx::Rect soon_rect
= moved_viewport
;
1311 soon_rect
.Inset(-inset
, -inset
);
1313 // There are 3 bins in TilePriority.
1314 bool have_tiles
[3] = {};
1315 PrioritizedTile last_tile
;
1316 int eventually_bin_order_correct_count
= 0;
1317 int eventually_bin_order_incorrect_count
= 0;
1318 scoped_ptr
<TilingSetRasterQueueAll
> queue(
1319 new TilingSetRasterQueueAll(tiling_set
.get(), false));
1320 for (; !queue
->IsEmpty(); queue
->Pop()) {
1321 if (!last_tile
.tile())
1322 last_tile
= queue
->Top();
1324 const PrioritizedTile
& new_tile
= queue
->Top();
1326 TilePriority last_priority
= last_tile
.priority();
1327 TilePriority new_priority
= new_tile
.priority();
1329 have_tiles
[new_priority
.priority_bin
] = true;
1331 EXPECT_LE(last_priority
.priority_bin
, new_priority
.priority_bin
);
1332 if (last_priority
.priority_bin
== new_priority
.priority_bin
) {
1333 if (last_priority
.priority_bin
== TilePriority::EVENTUALLY
) {
1334 bool order_correct
= last_priority
.distance_to_visible
<=
1335 new_priority
.distance_to_visible
;
1336 eventually_bin_order_correct_count
+= order_correct
;
1337 eventually_bin_order_incorrect_count
+= !order_correct
;
1338 } else if (!soon_rect
.Intersects(new_tile
.tile()->content_rect()) &&
1339 !soon_rect
.Intersects(last_tile
.tile()->content_rect())) {
1340 EXPECT_LE(last_priority
.distance_to_visible
,
1341 new_priority
.distance_to_visible
);
1342 } else if (new_priority
.distance_to_visible
> 0.f
) {
1343 EXPECT_EQ(TilePriority::SOON
, new_priority
.priority_bin
);
1346 last_tile
= new_tile
;
1349 EXPECT_GT(eventually_bin_order_correct_count
,
1350 eventually_bin_order_incorrect_count
);
1352 EXPECT_TRUE(have_tiles
[TilePriority::NOW
]);
1353 EXPECT_TRUE(have_tiles
[TilePriority::SOON
]);
1354 EXPECT_TRUE(have_tiles
[TilePriority::EVENTUALLY
]);
1357 TEST_F(TileManagerTilePriorityQueueTest
, SetIsLikelyToRequireADraw
) {
1358 const gfx::Size
layer_bounds(1000, 1000);
1359 host_impl_
.SetViewportSize(layer_bounds
);
1360 SetupDefaultTrees(layer_bounds
);
1362 // Verify that the queue has a required for draw tile at Top.
1363 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
1364 SAME_PRIORITY_FOR_BOTH_TREES
, RasterTilePriorityQueue::Type::ALL
));
1365 EXPECT_FALSE(queue
->IsEmpty());
1366 EXPECT_TRUE(queue
->Top().tile()->required_for_draw());
1368 EXPECT_FALSE(host_impl_
.is_likely_to_require_a_draw());
1369 host_impl_
.tile_manager()->PrepareTiles(host_impl_
.global_tile_state());
1370 EXPECT_TRUE(host_impl_
.is_likely_to_require_a_draw());
1373 TEST_F(TileManagerTilePriorityQueueTest
,
1374 SetIsLikelyToRequireADrawOnZeroMemoryBudget
) {
1375 const gfx::Size
layer_bounds(1000, 1000);
1376 host_impl_
.SetViewportSize(layer_bounds
);
1377 SetupDefaultTrees(layer_bounds
);
1379 // Verify that the queue has a required for draw tile at Top.
1380 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
1381 SAME_PRIORITY_FOR_BOTH_TREES
, RasterTilePriorityQueue::Type::ALL
));
1382 EXPECT_FALSE(queue
->IsEmpty());
1383 EXPECT_TRUE(queue
->Top().tile()->required_for_draw());
1385 ManagedMemoryPolicy policy
= host_impl_
.ActualManagedMemoryPolicy();
1386 policy
.bytes_limit_when_visible
= 0;
1387 host_impl_
.SetMemoryPolicy(policy
);
1389 EXPECT_FALSE(host_impl_
.is_likely_to_require_a_draw());
1390 host_impl_
.tile_manager()->PrepareTiles(host_impl_
.global_tile_state());
1391 EXPECT_FALSE(host_impl_
.is_likely_to_require_a_draw());
1394 TEST_F(TileManagerTilePriorityQueueTest
,
1395 SetIsLikelyToRequireADrawOnLimitedMemoryBudget
) {
1396 const gfx::Size
layer_bounds(1000, 1000);
1397 host_impl_
.SetViewportSize(layer_bounds
);
1398 SetupDefaultTrees(layer_bounds
);
1400 // Verify that the queue has a required for draw tile at Top.
1401 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
1402 SAME_PRIORITY_FOR_BOTH_TREES
, RasterTilePriorityQueue::Type::ALL
));
1403 EXPECT_FALSE(queue
->IsEmpty());
1404 EXPECT_TRUE(queue
->Top().tile()->required_for_draw());
1405 EXPECT_EQ(gfx::Size(256, 256), queue
->Top().tile()->desired_texture_size());
1406 EXPECT_EQ(RGBA_8888
, host_impl_
.resource_provider()->best_texture_format());
1408 ManagedMemoryPolicy policy
= host_impl_
.ActualManagedMemoryPolicy();
1409 policy
.bytes_limit_when_visible
=
1410 Resource::MemorySizeBytes(gfx::Size(256, 256), RGBA_8888
);
1411 host_impl_
.SetMemoryPolicy(policy
);
1413 EXPECT_FALSE(host_impl_
.is_likely_to_require_a_draw());
1414 host_impl_
.tile_manager()->PrepareTiles(host_impl_
.global_tile_state());
1415 EXPECT_TRUE(host_impl_
.is_likely_to_require_a_draw());
1417 scoped_ptr
<ScopedResource
> resource
=
1418 host_impl_
.resource_pool()->AcquireResource(gfx::Size(256, 256),
1421 host_impl_
.tile_manager()->CheckIfMoreTilesNeedToBePreparedForTesting();
1422 EXPECT_FALSE(host_impl_
.is_likely_to_require_a_draw());
1424 host_impl_
.resource_pool()->ReleaseResource(resource
.Pass());