Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / cc / resources / tile_manager_unittest.cc
blob49a93d3d1296ee2a8bd46f7d6df8032247d337e6
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 "cc/resources/eviction_tile_priority_queue.h"
6 #include "cc/resources/raster_tile_priority_queue.h"
7 #include "cc/resources/tile.h"
8 #include "cc/resources/tile_priority.h"
9 #include "cc/test/fake_impl_proxy.h"
10 #include "cc/test/fake_layer_tree_host_impl.h"
11 #include "cc/test/fake_output_surface.h"
12 #include "cc/test/fake_output_surface_client.h"
13 #include "cc/test/fake_picture_layer_impl.h"
14 #include "cc/test/fake_picture_pile_impl.h"
15 #include "cc/test/fake_tile_manager.h"
16 #include "cc/test/impl_side_painting_settings.h"
17 #include "cc/test/test_shared_bitmap_manager.h"
18 #include "cc/test/test_tile_priorities.h"
19 #include "cc/trees/layer_tree_impl.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace cc {
23 namespace {
25 class LowResTilingsSettings : public ImplSidePaintingSettings {
26 public:
27 LowResTilingsSettings() { create_low_res_tiling = true; }
30 class TileManagerTilePriorityQueueTest : public testing::Test {
31 public:
32 TileManagerTilePriorityQueueTest()
33 : memory_limit_policy_(ALLOW_ANYTHING),
34 max_tiles_(10000),
35 ready_to_activate_(false),
36 id_(7),
37 proxy_(base::MessageLoopProxy::current()),
38 host_impl_(LowResTilingsSettings(), &proxy_, &shared_bitmap_manager_) {}
40 void SetTreePriority(TreePriority tree_priority) {
41 GlobalStateThatImpactsTilePriority state;
42 gfx::Size tile_size(256, 256);
44 state.soft_memory_limit_in_bytes = 100 * 1000 * 1000;
45 state.num_resources_limit = max_tiles_;
46 state.hard_memory_limit_in_bytes = state.soft_memory_limit_in_bytes * 2;
47 state.memory_limit_policy = memory_limit_policy_;
48 state.tree_priority = tree_priority;
50 global_state_ = state;
51 host_impl_.resource_pool()->SetResourceUsageLimits(
52 state.soft_memory_limit_in_bytes,
53 state.soft_memory_limit_in_bytes,
54 state.num_resources_limit);
55 host_impl_.tile_manager()->SetGlobalStateForTesting(state);
58 void SetUp() override {
59 InitializeRenderer();
60 SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
63 virtual void InitializeRenderer() {
64 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d());
67 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
68 gfx::Size tile_size(100, 100);
70 scoped_refptr<FakePicturePileImpl> pending_pile =
71 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
72 scoped_refptr<FakePicturePileImpl> active_pile =
73 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
75 SetupTrees(pending_pile, active_pile);
78 void ActivateTree() {
79 host_impl_.ActivateSyncTree();
80 CHECK(!host_impl_.pending_tree());
81 pending_layer_ = NULL;
82 active_layer_ = static_cast<FakePictureLayerImpl*>(
83 host_impl_.active_tree()->LayerById(id_));
86 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
87 const gfx::Size& tile_size) {
88 SetupDefaultTrees(layer_bounds);
89 pending_layer_->set_fixed_tile_size(tile_size);
90 active_layer_->set_fixed_tile_size(tile_size);
93 void SetupTrees(scoped_refptr<PicturePileImpl> pending_pile,
94 scoped_refptr<PicturePileImpl> active_pile) {
95 SetupPendingTree(active_pile);
96 ActivateTree();
97 SetupPendingTree(pending_pile);
100 void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
101 host_impl_.CreatePendingTree();
102 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
104 // Steal from the recycled tree.
105 scoped_ptr<LayerImpl> old_pending_root = pending_tree->DetachLayerTree();
106 DCHECK_IMPLIES(old_pending_root, old_pending_root->id() == id_);
108 scoped_ptr<FakePictureLayerImpl> pending_layer;
109 if (old_pending_root) {
110 pending_layer.reset(
111 static_cast<FakePictureLayerImpl*>(old_pending_root.release()));
112 pending_layer->SetRasterSource(pile);
113 } else {
114 pending_layer =
115 FakePictureLayerImpl::CreateWithRasterSource(pending_tree, id_, pile);
116 pending_layer->SetDrawsContent(true);
118 // The bounds() just mirror the pile size.
119 pending_layer->SetBounds(pending_layer->raster_source()->GetSize());
120 pending_tree->SetRootLayer(pending_layer.Pass());
122 pending_layer_ = static_cast<FakePictureLayerImpl*>(
123 host_impl_.pending_tree()->LayerById(id_));
124 pending_layer_->DoPostCommitInitializationIfNeeded();
127 void CreateHighLowResAndSetAllTilesVisible() {
128 // Active layer must get updated first so pending layer can share from it.
129 active_layer_->CreateDefaultTilingsAndTiles();
130 active_layer_->SetAllTilesVisible();
131 pending_layer_->CreateDefaultTilingsAndTiles();
132 pending_layer_->SetAllTilesVisible();
135 TileManager* tile_manager() { return host_impl_.tile_manager(); }
137 protected:
138 GlobalStateThatImpactsTilePriority global_state_;
140 TestSharedBitmapManager shared_bitmap_manager_;
141 TileMemoryLimitPolicy memory_limit_policy_;
142 int max_tiles_;
143 bool ready_to_activate_;
144 int id_;
145 FakeImplProxy proxy_;
146 FakeLayerTreeHostImpl host_impl_;
147 FakePictureLayerImpl* pending_layer_;
148 FakePictureLayerImpl* active_layer_;
151 TEST_F(TileManagerTilePriorityQueueTest, RasterTilePriorityQueue) {
152 SetupDefaultTrees(gfx::Size(1000, 1000));
154 active_layer_->CreateDefaultTilingsAndTiles();
155 pending_layer_->CreateDefaultTilingsAndTiles();
157 RasterTilePriorityQueue queue;
158 host_impl_.BuildRasterQueue(&queue, SAME_PRIORITY_FOR_BOTH_TREES);
159 EXPECT_FALSE(queue.IsEmpty());
161 size_t tile_count = 0;
162 std::set<Tile*> all_tiles;
163 while (!queue.IsEmpty()) {
164 EXPECT_TRUE(queue.Top());
165 all_tiles.insert(queue.Top());
166 ++tile_count;
167 queue.Pop();
170 EXPECT_EQ(tile_count, all_tiles.size());
171 EXPECT_EQ(16u, tile_count);
173 // Sanity check, all tiles should be visible.
174 std::set<Tile*> smoothness_tiles;
175 queue.Reset();
176 host_impl_.BuildRasterQueue(&queue, SMOOTHNESS_TAKES_PRIORITY);
177 bool had_low_res = false;
178 while (!queue.IsEmpty()) {
179 Tile* tile = queue.Top();
180 EXPECT_TRUE(tile);
181 EXPECT_EQ(TilePriority::NOW, tile->priority(ACTIVE_TREE).priority_bin);
182 EXPECT_EQ(TilePriority::NOW, tile->priority(PENDING_TREE).priority_bin);
183 if (tile->priority(ACTIVE_TREE).resolution == LOW_RESOLUTION)
184 had_low_res = true;
185 else
186 smoothness_tiles.insert(tile);
187 queue.Pop();
189 EXPECT_EQ(all_tiles, smoothness_tiles);
190 EXPECT_TRUE(had_low_res);
192 Region invalidation(gfx::Rect(0, 0, 500, 500));
194 // Invalidate the pending tree.
195 pending_layer_->set_invalidation(invalidation);
196 pending_layer_->HighResTiling()->UpdateTilesToCurrentRasterSource(
197 pending_layer_->raster_source(), invalidation, gfx::Size(1000, 1000));
198 pending_layer_->LowResTiling()->UpdateTilesToCurrentRasterSource(
199 pending_layer_->raster_source(), invalidation, gfx::Size(1000, 1000));
201 active_layer_->ResetAllTilesPriorities();
202 pending_layer_->ResetAllTilesPriorities();
204 // Renew all of the tile priorities.
205 gfx::Rect viewport(50, 50, 100, 100);
206 pending_layer_->HighResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
207 Occlusion());
208 pending_layer_->LowResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
209 Occlusion());
210 active_layer_->HighResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
211 Occlusion());
212 active_layer_->LowResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
213 Occlusion());
215 // Populate all tiles directly from the tilings.
216 all_tiles.clear();
217 std::set<Tile*> high_res_tiles;
218 std::vector<Tile*> pending_high_res_tiles =
219 pending_layer_->HighResTiling()->AllTilesForTesting();
220 for (size_t i = 0; i < pending_high_res_tiles.size(); ++i) {
221 all_tiles.insert(pending_high_res_tiles[i]);
222 high_res_tiles.insert(pending_high_res_tiles[i]);
225 std::vector<Tile*> pending_low_res_tiles =
226 pending_layer_->LowResTiling()->AllTilesForTesting();
227 for (size_t i = 0; i < pending_low_res_tiles.size(); ++i)
228 all_tiles.insert(pending_low_res_tiles[i]);
230 std::vector<Tile*> active_high_res_tiles =
231 active_layer_->HighResTiling()->AllTilesForTesting();
232 for (size_t i = 0; i < active_high_res_tiles.size(); ++i) {
233 all_tiles.insert(active_high_res_tiles[i]);
234 high_res_tiles.insert(active_high_res_tiles[i]);
237 std::vector<Tile*> active_low_res_tiles =
238 active_layer_->LowResTiling()->AllTilesForTesting();
239 for (size_t i = 0; i < active_low_res_tiles.size(); ++i)
240 all_tiles.insert(active_low_res_tiles[i]);
242 Tile* last_tile = NULL;
243 smoothness_tiles.clear();
244 tile_count = 0;
245 size_t correct_order_tiles = 0u;
246 // Here we expect to get increasing ACTIVE_TREE priority_bin.
247 queue.Reset();
248 host_impl_.BuildRasterQueue(&queue, SMOOTHNESS_TAKES_PRIORITY);
249 while (!queue.IsEmpty()) {
250 Tile* tile = queue.Top();
251 EXPECT_TRUE(tile);
253 if (!last_tile)
254 last_tile = tile;
256 EXPECT_LE(last_tile->priority(ACTIVE_TREE).priority_bin,
257 tile->priority(ACTIVE_TREE).priority_bin);
258 bool skip_updating_last_tile = false;
259 if (last_tile->priority(ACTIVE_TREE).priority_bin ==
260 tile->priority(ACTIVE_TREE).priority_bin) {
261 correct_order_tiles +=
262 last_tile->priority(ACTIVE_TREE).distance_to_visible <=
263 tile->priority(ACTIVE_TREE).distance_to_visible;
264 } else if (tile->priority(ACTIVE_TREE).priority_bin ==
265 TilePriority::EVENTUALLY &&
266 tile->priority(PENDING_TREE).priority_bin == TilePriority::NOW) {
267 // Since we'd return pending tree now tiles before the eventually tiles on
268 // the active tree, update the value.
269 ++correct_order_tiles;
270 skip_updating_last_tile = true;
273 if (tile->priority(ACTIVE_TREE).priority_bin == TilePriority::NOW &&
274 last_tile->priority(ACTIVE_TREE).resolution !=
275 tile->priority(ACTIVE_TREE).resolution) {
276 // Low resolution should come first.
277 EXPECT_EQ(LOW_RESOLUTION, last_tile->priority(ACTIVE_TREE).resolution);
280 if (!skip_updating_last_tile)
281 last_tile = tile;
282 ++tile_count;
283 smoothness_tiles.insert(tile);
284 queue.Pop();
287 EXPECT_EQ(tile_count, smoothness_tiles.size());
288 EXPECT_EQ(all_tiles, smoothness_tiles);
289 // Since we don't guarantee increasing distance due to spiral iterator, we
290 // should check that we're _mostly_ right.
291 EXPECT_GT(correct_order_tiles, 3 * tile_count / 4);
293 std::set<Tile*> new_content_tiles;
294 last_tile = NULL;
295 size_t increasing_distance_tiles = 0u;
296 // Here we expect to get increasing PENDING_TREE priority_bin.
297 queue.Reset();
298 host_impl_.BuildRasterQueue(&queue, NEW_CONTENT_TAKES_PRIORITY);
299 tile_count = 0;
300 while (!queue.IsEmpty()) {
301 Tile* tile = queue.Top();
302 EXPECT_TRUE(tile);
304 if (!last_tile)
305 last_tile = tile;
307 EXPECT_LE(last_tile->priority(PENDING_TREE).priority_bin,
308 tile->priority(PENDING_TREE).priority_bin);
309 if (last_tile->priority(PENDING_TREE).priority_bin ==
310 tile->priority(PENDING_TREE).priority_bin) {
311 increasing_distance_tiles +=
312 last_tile->priority(PENDING_TREE).distance_to_visible <=
313 tile->priority(PENDING_TREE).distance_to_visible;
316 if (tile->priority(PENDING_TREE).priority_bin == TilePriority::NOW &&
317 last_tile->priority(PENDING_TREE).resolution !=
318 tile->priority(PENDING_TREE).resolution) {
319 // High resolution should come first.
320 EXPECT_EQ(HIGH_RESOLUTION, last_tile->priority(PENDING_TREE).resolution);
323 last_tile = tile;
324 new_content_tiles.insert(tile);
325 ++tile_count;
326 queue.Pop();
329 EXPECT_EQ(tile_count, new_content_tiles.size());
330 EXPECT_EQ(high_res_tiles, new_content_tiles);
331 // Since we don't guarantee increasing distance due to spiral iterator, we
332 // should check that we're _mostly_ right.
333 EXPECT_GE(increasing_distance_tiles, 3 * tile_count / 4);
336 TEST_F(TileManagerTilePriorityQueueTest, ActivationComesBeforeEventually) {
337 SetupDefaultTrees(gfx::Size(1000, 1000));
339 active_layer_->CreateDefaultTilingsAndTiles();
340 pending_layer_->CreateDefaultTilingsAndTiles();
342 // Create a pending child layer.
343 gfx::Size tile_size(256, 256);
344 scoped_refptr<FakePicturePileImpl> pending_pile =
345 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1000, 1000));
346 scoped_ptr<FakePictureLayerImpl> pending_child =
347 FakePictureLayerImpl::CreateWithRasterSource(host_impl_.pending_tree(),
348 id_ + 1, pending_pile);
349 pending_layer_->AddChild(pending_child.Pass());
350 FakePictureLayerImpl* pending_child_raw = static_cast<FakePictureLayerImpl*>(
351 host_impl_.pending_tree()->LayerById(id_ + 1));
352 ASSERT_TRUE(pending_child_raw);
354 pending_child_raw->SetDrawsContent(true);
355 pending_child_raw->DoPostCommitInitializationIfNeeded();
356 pending_child_raw->CreateDefaultTilingsAndTiles();
357 ASSERT_TRUE(pending_child_raw->HighResTiling());
359 // Set a small viewport, so we have soon and eventually tiles.
360 gfx::Rect viewport(200, 200);
361 active_layer_->draw_properties().visible_content_rect = viewport;
362 active_layer_->UpdateTiles(Occlusion(), false);
363 pending_layer_->draw_properties().visible_content_rect = viewport;
364 pending_layer_->UpdateTiles(Occlusion(), false);
365 pending_child_raw->draw_properties().visible_content_rect = viewport;
366 pending_child_raw->UpdateTiles(Occlusion(), false);
368 RasterTilePriorityQueue queue;
369 host_impl_.SetRequiresHighResToDraw();
370 host_impl_.BuildRasterQueue(&queue, SMOOTHNESS_TAKES_PRIORITY);
371 EXPECT_FALSE(queue.IsEmpty());
373 // Get all the tiles that are NOW or SOON and make sure they are ready to
374 // draw.
375 std::vector<Tile*> all_tiles;
376 while (!queue.IsEmpty()) {
377 Tile* tile = queue.Top();
378 if (tile->combined_priority().priority_bin >= TilePriority::EVENTUALLY)
379 break;
381 all_tiles.push_back(tile);
382 queue.Pop();
385 tile_manager()->InitializeTilesWithResourcesForTesting(
386 std::vector<Tile*>(all_tiles.begin(), all_tiles.end()));
388 // Ensure we can activate.
389 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
390 EXPECT_TRUE(pending_child_raw->AllTilesRequiredForActivationAreReadyToDraw());
393 TEST_F(TileManagerTilePriorityQueueTest, EvictionTilePriorityQueue) {
394 SetupDefaultTrees(gfx::Size(1000, 1000));
396 active_layer_->CreateDefaultTilingsAndTiles();
397 pending_layer_->CreateDefaultTilingsAndTiles();
399 EvictionTilePriorityQueue empty_queue;
400 host_impl_.BuildEvictionQueue(&empty_queue, SAME_PRIORITY_FOR_BOTH_TREES);
401 EXPECT_TRUE(empty_queue.IsEmpty());
402 std::set<Tile*> all_tiles;
403 size_t tile_count = 0;
405 RasterTilePriorityQueue raster_queue;
406 host_impl_.BuildRasterQueue(&raster_queue, SAME_PRIORITY_FOR_BOTH_TREES);
407 while (!raster_queue.IsEmpty()) {
408 ++tile_count;
409 EXPECT_TRUE(raster_queue.Top());
410 all_tiles.insert(raster_queue.Top());
411 raster_queue.Pop();
414 EXPECT_EQ(tile_count, all_tiles.size());
415 EXPECT_EQ(16u, tile_count);
417 tile_manager()->InitializeTilesWithResourcesForTesting(
418 std::vector<Tile*>(all_tiles.begin(), all_tiles.end()));
420 EvictionTilePriorityQueue queue;
421 host_impl_.BuildEvictionQueue(&queue, SMOOTHNESS_TAKES_PRIORITY);
422 EXPECT_FALSE(queue.IsEmpty());
424 // Sanity check, all tiles should be visible.
425 std::set<Tile*> smoothness_tiles;
426 while (!queue.IsEmpty()) {
427 Tile* tile = queue.Top();
428 EXPECT_TRUE(tile);
429 EXPECT_EQ(TilePriority::NOW, tile->priority(ACTIVE_TREE).priority_bin);
430 EXPECT_EQ(TilePriority::NOW, tile->priority(PENDING_TREE).priority_bin);
431 EXPECT_TRUE(tile->HasResources());
432 smoothness_tiles.insert(tile);
433 queue.Pop();
435 EXPECT_EQ(all_tiles, smoothness_tiles);
437 tile_manager()->ReleaseTileResourcesForTesting(
438 std::vector<Tile*>(all_tiles.begin(), all_tiles.end()));
440 Region invalidation(gfx::Rect(0, 0, 500, 500));
442 // Invalidate the pending tree.
443 pending_layer_->set_invalidation(invalidation);
444 pending_layer_->HighResTiling()->UpdateTilesToCurrentRasterSource(
445 pending_layer_->raster_source(), invalidation, gfx::Size(1000, 1000));
446 pending_layer_->LowResTiling()->UpdateTilesToCurrentRasterSource(
447 pending_layer_->raster_source(), invalidation, gfx::Size(1000, 1000));
449 active_layer_->ResetAllTilesPriorities();
450 pending_layer_->ResetAllTilesPriorities();
452 // Renew all of the tile priorities.
453 gfx::Rect viewport(50, 50, 100, 100);
454 pending_layer_->HighResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
455 Occlusion());
456 pending_layer_->LowResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
457 Occlusion());
458 active_layer_->HighResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
459 Occlusion());
460 active_layer_->LowResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
461 Occlusion());
463 // Populate all tiles directly from the tilings.
464 all_tiles.clear();
465 std::vector<Tile*> pending_high_res_tiles =
466 pending_layer_->HighResTiling()->AllTilesForTesting();
467 for (size_t i = 0; i < pending_high_res_tiles.size(); ++i)
468 all_tiles.insert(pending_high_res_tiles[i]);
470 std::vector<Tile*> pending_low_res_tiles =
471 pending_layer_->LowResTiling()->AllTilesForTesting();
472 for (size_t i = 0; i < pending_low_res_tiles.size(); ++i)
473 all_tiles.insert(pending_low_res_tiles[i]);
475 std::vector<Tile*> active_high_res_tiles =
476 active_layer_->HighResTiling()->AllTilesForTesting();
477 for (size_t i = 0; i < active_high_res_tiles.size(); ++i)
478 all_tiles.insert(active_high_res_tiles[i]);
480 std::vector<Tile*> active_low_res_tiles =
481 active_layer_->LowResTiling()->AllTilesForTesting();
482 for (size_t i = 0; i < active_low_res_tiles.size(); ++i)
483 all_tiles.insert(active_low_res_tiles[i]);
485 tile_manager()->InitializeTilesWithResourcesForTesting(
486 std::vector<Tile*>(all_tiles.begin(), all_tiles.end()));
488 Tile* last_tile = NULL;
489 smoothness_tiles.clear();
490 tile_count = 0;
491 // Here we expect to get increasing ACTIVE_TREE priority_bin.
492 queue.Reset();
493 host_impl_.BuildEvictionQueue(&queue, SMOOTHNESS_TAKES_PRIORITY);
494 while (!queue.IsEmpty()) {
495 Tile* tile = queue.Top();
496 EXPECT_TRUE(tile);
497 EXPECT_TRUE(tile->HasResources());
499 if (!last_tile)
500 last_tile = tile;
502 EXPECT_GE(last_tile->priority(ACTIVE_TREE).priority_bin,
503 tile->priority(ACTIVE_TREE).priority_bin);
504 if (last_tile->priority(ACTIVE_TREE).priority_bin ==
505 tile->priority(ACTIVE_TREE).priority_bin) {
506 EXPECT_LE(last_tile->required_for_activation(),
507 tile->required_for_activation());
508 if (last_tile->required_for_activation() ==
509 tile->required_for_activation()) {
510 EXPECT_GE(last_tile->priority(ACTIVE_TREE).distance_to_visible,
511 tile->priority(ACTIVE_TREE).distance_to_visible);
515 last_tile = tile;
516 ++tile_count;
517 smoothness_tiles.insert(tile);
518 queue.Pop();
521 EXPECT_EQ(tile_count, smoothness_tiles.size());
522 EXPECT_EQ(all_tiles, smoothness_tiles);
524 std::set<Tile*> new_content_tiles;
525 last_tile = NULL;
526 // Here we expect to get increasing PENDING_TREE priority_bin.
527 queue.Reset();
528 host_impl_.BuildEvictionQueue(&queue, NEW_CONTENT_TAKES_PRIORITY);
529 while (!queue.IsEmpty()) {
530 Tile* tile = queue.Top();
531 EXPECT_TRUE(tile);
533 if (!last_tile)
534 last_tile = tile;
536 EXPECT_GE(last_tile->priority(PENDING_TREE).priority_bin,
537 tile->priority(PENDING_TREE).priority_bin);
538 if (last_tile->priority(PENDING_TREE).priority_bin ==
539 tile->priority(PENDING_TREE).priority_bin) {
540 EXPECT_LE(last_tile->required_for_activation(),
541 tile->required_for_activation());
542 if (last_tile->required_for_activation() ==
543 tile->required_for_activation()) {
544 EXPECT_GE(last_tile->priority(PENDING_TREE).distance_to_visible,
545 tile->priority(PENDING_TREE).distance_to_visible);
549 last_tile = tile;
550 new_content_tiles.insert(tile);
551 queue.Pop();
554 EXPECT_EQ(tile_count, new_content_tiles.size());
555 EXPECT_EQ(all_tiles, new_content_tiles);
558 TEST_F(TileManagerTilePriorityQueueTest,
559 EvictionTilePriorityQueueWithOcclusion) {
560 gfx::Size tile_size(102, 102);
561 gfx::Size layer_bounds(1000, 1000);
563 scoped_refptr<FakePicturePileImpl> pending_pile =
564 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
565 SetupPendingTree(pending_pile);
566 pending_layer_->CreateDefaultTilingsAndTiles();
568 scoped_ptr<FakePictureLayerImpl> pending_child =
569 FakePictureLayerImpl::CreateWithRasterSource(host_impl_.pending_tree(), 2,
570 pending_pile);
571 pending_layer_->AddChild(pending_child.Pass());
573 FakePictureLayerImpl* pending_child_layer =
574 static_cast<FakePictureLayerImpl*>(pending_layer_->children()[0]);
575 pending_child_layer->SetDrawsContent(true);
576 pending_child_layer->DoPostCommitInitializationIfNeeded();
577 pending_child_layer->CreateDefaultTilingsAndTiles();
579 std::set<Tile*> all_tiles;
580 size_t tile_count = 0;
581 RasterTilePriorityQueue raster_queue;
582 host_impl_.BuildRasterQueue(&raster_queue, SAME_PRIORITY_FOR_BOTH_TREES);
583 while (!raster_queue.IsEmpty()) {
584 ++tile_count;
585 EXPECT_TRUE(raster_queue.Top());
586 all_tiles.insert(raster_queue.Top());
587 raster_queue.Pop();
589 EXPECT_EQ(tile_count, all_tiles.size());
590 EXPECT_EQ(32u, tile_count);
592 pending_layer_->ResetAllTilesPriorities();
594 // Renew all of the tile priorities.
595 gfx::Rect viewport(layer_bounds);
596 pending_layer_->HighResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
597 Occlusion());
598 pending_layer_->LowResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
599 Occlusion());
600 pending_child_layer->HighResTiling()->ComputeTilePriorityRects(
601 viewport, 1.0f, 1.0, Occlusion());
602 pending_child_layer->LowResTiling()->ComputeTilePriorityRects(
603 viewport, 1.0f, 1.0, Occlusion());
605 // Populate all tiles directly from the tilings.
606 all_tiles.clear();
607 std::vector<Tile*> pending_high_res_tiles =
608 pending_layer_->HighResTiling()->AllTilesForTesting();
609 all_tiles.insert(pending_high_res_tiles.begin(),
610 pending_high_res_tiles.end());
612 std::vector<Tile*> pending_low_res_tiles =
613 pending_layer_->LowResTiling()->AllTilesForTesting();
614 all_tiles.insert(pending_low_res_tiles.begin(), pending_low_res_tiles.end());
616 // Set all tiles on the pending_child_layer as occluded on the pending tree.
617 std::vector<Tile*> pending_child_high_res_tiles =
618 pending_child_layer->HighResTiling()->AllTilesForTesting();
619 pending_child_layer->HighResTiling()->SetAllTilesOccludedForTesting();
620 all_tiles.insert(pending_child_high_res_tiles.begin(),
621 pending_child_high_res_tiles.end());
623 std::vector<Tile*> pending_child_low_res_tiles =
624 pending_child_layer->LowResTiling()->AllTilesForTesting();
625 pending_child_layer->LowResTiling()->SetAllTilesOccludedForTesting();
626 all_tiles.insert(pending_child_low_res_tiles.begin(),
627 pending_child_low_res_tiles.end());
629 tile_manager()->InitializeTilesWithResourcesForTesting(
630 std::vector<Tile*>(all_tiles.begin(), all_tiles.end()));
632 // Verify occlusion is considered by EvictionTilePriorityQueue.
633 TreePriority tree_priority = NEW_CONTENT_TAKES_PRIORITY;
634 size_t occluded_count = 0u;
635 Tile* last_tile = NULL;
636 EvictionTilePriorityQueue queue;
637 host_impl_.BuildEvictionQueue(&queue, tree_priority);
638 while (!queue.IsEmpty()) {
639 Tile* tile = queue.Top();
640 if (!last_tile)
641 last_tile = tile;
643 bool tile_is_occluded = tile->is_occluded_for_tree_priority(tree_priority);
645 // The only way we will encounter an occluded tile after an unoccluded
646 // tile is if the priorty bin decreased, the tile is required for
647 // activation, or the scale changed.
648 if (tile_is_occluded) {
649 occluded_count++;
651 bool last_tile_is_occluded =
652 last_tile->is_occluded_for_tree_priority(tree_priority);
653 if (!last_tile_is_occluded) {
654 TilePriority::PriorityBin tile_priority_bin =
655 tile->priority_for_tree_priority(tree_priority).priority_bin;
656 TilePriority::PriorityBin last_tile_priority_bin =
657 last_tile->priority_for_tree_priority(tree_priority).priority_bin;
659 EXPECT_TRUE((tile_priority_bin < last_tile_priority_bin) ||
660 tile->required_for_activation() ||
661 (tile->contents_scale() != last_tile->contents_scale()));
664 last_tile = tile;
665 queue.Pop();
667 size_t expected_occluded_count =
668 pending_child_high_res_tiles.size() + pending_child_low_res_tiles.size();
669 EXPECT_EQ(expected_occluded_count, occluded_count);
672 TEST_F(TileManagerTilePriorityQueueTest,
673 EvictionTilePriorityQueueWithTransparentLayer) {
674 gfx::Size tile_size(102, 102);
675 gfx::Size layer_bounds(1000, 1000);
677 scoped_refptr<FakePicturePileImpl> pending_pile =
678 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
679 SetupPendingTree(pending_pile);
680 pending_layer_->CreateDefaultTilingsAndTiles();
682 scoped_ptr<FakePictureLayerImpl> pending_child =
683 FakePictureLayerImpl::CreateWithRasterSource(host_impl_.pending_tree(), 2,
684 pending_pile);
685 pending_layer_->AddChild(pending_child.Pass());
687 // Create a fully transparent child layer so that its tile priorities are not
688 // considered to be valid.
689 FakePictureLayerImpl* pending_child_layer =
690 static_cast<FakePictureLayerImpl*>(pending_layer_->children()[0]);
691 pending_child_layer->SetDrawsContent(true);
692 pending_child_layer->CreateDefaultTilingsAndTiles();
693 pending_child_layer->SetOpacity(0.0);
694 pending_child_layer->layer_tree_impl()->UpdateDrawProperties();
695 pending_child_layer->DoPostCommitInitializationIfNeeded();
697 // Renew all of the tile priorities.
698 gfx::Rect viewport(layer_bounds);
699 pending_layer_->HighResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
700 Occlusion());
701 pending_layer_->LowResTiling()->ComputeTilePriorityRects(viewport, 1.0f, 1.0,
702 Occlusion());
703 pending_child_layer->HighResTiling()->ComputeTilePriorityRects(
704 viewport, 1.0f, 1.0, Occlusion());
705 pending_child_layer->LowResTiling()->ComputeTilePriorityRects(
706 viewport, 1.0f, 1.0, Occlusion());
708 // Populate all tiles directly from the tilings.
709 std::set<Tile*> all_pending_tiles;
710 std::vector<Tile*> pending_high_res_tiles =
711 pending_layer_->HighResTiling()->AllTilesForTesting();
712 all_pending_tiles.insert(pending_high_res_tiles.begin(),
713 pending_high_res_tiles.end());
714 EXPECT_EQ(16u, pending_high_res_tiles.size());
716 std::vector<Tile*> pending_low_res_tiles =
717 pending_layer_->LowResTiling()->AllTilesForTesting();
718 all_pending_tiles.insert(pending_low_res_tiles.begin(),
719 pending_low_res_tiles.end());
720 EXPECT_EQ(1u, pending_low_res_tiles.size());
722 std::set<Tile*> all_pending_child_tiles;
723 std::vector<Tile*> pending_child_high_res_tiles =
724 pending_child_layer->HighResTiling()->AllTilesForTesting();
725 all_pending_child_tiles.insert(pending_child_high_res_tiles.begin(),
726 pending_child_high_res_tiles.end());
727 EXPECT_EQ(16u, pending_child_high_res_tiles.size());
729 std::vector<Tile*> pending_child_low_res_tiles =
730 pending_child_layer->LowResTiling()->AllTilesForTesting();
731 all_pending_child_tiles.insert(pending_child_low_res_tiles.begin(),
732 pending_child_low_res_tiles.end());
733 EXPECT_EQ(1u, pending_child_low_res_tiles.size());
735 std::set<Tile*> all_tiles = all_pending_tiles;
736 all_tiles.insert(all_pending_child_tiles.begin(),
737 all_pending_child_tiles.end());
739 tile_manager()->InitializeTilesWithResourcesForTesting(
740 std::vector<Tile*>(all_tiles.begin(), all_tiles.end()));
742 EXPECT_TRUE(pending_layer_->HasValidTilePriorities());
743 EXPECT_FALSE(pending_child_layer->HasValidTilePriorities());
745 // Verify that eviction queue returns tiles also from layers without valid
746 // tile priorities and that the tile priority bin of those tiles is (at most)
747 // EVENTUALLY.
748 TreePriority tree_priority = NEW_CONTENT_TAKES_PRIORITY;
749 std::set<Tile*> new_content_tiles;
750 size_t tile_count = 0;
751 EvictionTilePriorityQueue queue;
752 host_impl_.BuildEvictionQueue(&queue, tree_priority);
753 while (!queue.IsEmpty()) {
754 Tile* tile = queue.Top();
755 const TilePriority& pending_priority = tile->priority(PENDING_TREE);
756 EXPECT_NE(std::numeric_limits<float>::infinity(),
757 pending_priority.distance_to_visible);
758 if (all_pending_child_tiles.find(tile) != all_pending_child_tiles.end())
759 EXPECT_EQ(TilePriority::EVENTUALLY, pending_priority.priority_bin);
760 else
761 EXPECT_EQ(TilePriority::NOW, pending_priority.priority_bin);
762 new_content_tiles.insert(tile);
763 ++tile_count;
764 queue.Pop();
766 EXPECT_EQ(tile_count, new_content_tiles.size());
767 EXPECT_EQ(all_tiles, new_content_tiles);
770 TEST_F(TileManagerTilePriorityQueueTest, RasterTilePriorityQueueEmptyLayers) {
771 SetupDefaultTrees(gfx::Size(1000, 1000));
773 active_layer_->CreateDefaultTilingsAndTiles();
774 pending_layer_->CreateDefaultTilingsAndTiles();
776 RasterTilePriorityQueue queue;
777 host_impl_.BuildRasterQueue(&queue, SAME_PRIORITY_FOR_BOTH_TREES);
778 EXPECT_FALSE(queue.IsEmpty());
780 size_t tile_count = 0;
781 std::set<Tile*> all_tiles;
782 while (!queue.IsEmpty()) {
783 EXPECT_TRUE(queue.Top());
784 all_tiles.insert(queue.Top());
785 ++tile_count;
786 queue.Pop();
789 EXPECT_EQ(tile_count, all_tiles.size());
790 EXPECT_EQ(16u, tile_count);
792 queue.Reset();
793 for (int i = 1; i < 10; ++i) {
794 scoped_ptr<FakePictureLayerImpl> pending_layer =
795 FakePictureLayerImpl::Create(host_impl_.pending_tree(), id_ + i);
796 pending_layer->SetDrawsContent(true);
797 pending_layer->DoPostCommitInitializationIfNeeded();
798 pending_layer->set_has_valid_tile_priorities(true);
799 pending_layer_->AddChild(pending_layer.Pass());
802 host_impl_.BuildRasterQueue(&queue, SAME_PRIORITY_FOR_BOTH_TREES);
803 EXPECT_FALSE(queue.IsEmpty());
805 tile_count = 0;
806 all_tiles.clear();
807 while (!queue.IsEmpty()) {
808 EXPECT_TRUE(queue.Top());
809 all_tiles.insert(queue.Top());
810 ++tile_count;
811 queue.Pop();
813 EXPECT_EQ(tile_count, all_tiles.size());
814 EXPECT_EQ(16u, tile_count);
817 TEST_F(TileManagerTilePriorityQueueTest, EvictionTilePriorityQueueEmptyLayers) {
818 SetupDefaultTrees(gfx::Size(1000, 1000));
820 active_layer_->CreateDefaultTilingsAndTiles();
821 pending_layer_->CreateDefaultTilingsAndTiles();
823 RasterTilePriorityQueue raster_queue;
824 host_impl_.BuildRasterQueue(&raster_queue, SAME_PRIORITY_FOR_BOTH_TREES);
825 EXPECT_FALSE(raster_queue.IsEmpty());
827 size_t tile_count = 0;
828 std::set<Tile*> all_tiles;
829 while (!raster_queue.IsEmpty()) {
830 EXPECT_TRUE(raster_queue.Top());
831 all_tiles.insert(raster_queue.Top());
832 ++tile_count;
833 raster_queue.Pop();
835 EXPECT_EQ(tile_count, all_tiles.size());
836 EXPECT_EQ(16u, tile_count);
838 std::vector<Tile*> tiles(all_tiles.begin(), all_tiles.end());
839 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
841 EvictionTilePriorityQueue queue;
842 for (int i = 1; i < 10; ++i) {
843 scoped_ptr<FakePictureLayerImpl> pending_layer =
844 FakePictureLayerImpl::Create(host_impl_.pending_tree(), id_ + i);
845 pending_layer->SetDrawsContent(true);
846 pending_layer->DoPostCommitInitializationIfNeeded();
847 pending_layer->set_has_valid_tile_priorities(true);
848 pending_layer_->AddChild(pending_layer.Pass());
851 host_impl_.BuildEvictionQueue(&queue, SAME_PRIORITY_FOR_BOTH_TREES);
852 EXPECT_FALSE(queue.IsEmpty());
854 tile_count = 0;
855 all_tiles.clear();
856 while (!queue.IsEmpty()) {
857 EXPECT_TRUE(queue.Top());
858 all_tiles.insert(queue.Top());
859 ++tile_count;
860 queue.Pop();
862 EXPECT_EQ(tile_count, all_tiles.size());
863 EXPECT_EQ(16u, tile_count);
866 } // namespace
867 } // namespace cc