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/time/time.h"
6 #include "cc/debug/lap_timer.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/fake_tile_manager_client.h"
17 #include "cc/test/impl_side_painting_settings.h"
18 #include "cc/test/test_shared_bitmap_manager.h"
19 #include "cc/test/test_tile_priorities.h"
20 #include "cc/trees/layer_tree_impl.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "testing/perf/perf_test.h"
29 static const int kTimeLimitMillis
= 2000;
30 static const int kWarmupRuns
= 5;
31 static const int kTimeCheckInterval
= 10;
33 class FakeRasterizerImpl
: public Rasterizer
, public RasterizerTaskClient
{
35 // Overridden from Rasterizer:
36 virtual void SetClient(RasterizerClient
* client
) OVERRIDE
{}
37 virtual void Shutdown() OVERRIDE
{}
38 virtual void ScheduleTasks(RasterTaskQueue
* queue
) OVERRIDE
{
39 for (RasterTaskQueue::Item::Vector::const_iterator it
=
41 it
!= queue
->items
.end();
43 RasterTask
* task
= it
->task
;
46 task
->ScheduleOnOriginThread(this);
49 completed_tasks_
.push_back(task
);
52 virtual void CheckForCompletedTasks() OVERRIDE
{
53 for (RasterTask::Vector::iterator it
= completed_tasks_
.begin();
54 it
!= completed_tasks_
.end();
56 RasterTask
* task
= it
->get();
59 task
->CompleteOnOriginThread(this);
62 task
->RunReplyOnOriginThread();
64 completed_tasks_
.clear();
67 // Overridden from RasterizerTaskClient:
68 virtual SkCanvas
* AcquireCanvasForRaster(RasterTask
* task
) OVERRIDE
{
71 virtual void ReleaseCanvasForRaster(RasterTask
* task
) OVERRIDE
{}
74 RasterTask::Vector completed_tasks_
;
76 base::LazyInstance
<FakeRasterizerImpl
> g_fake_rasterizer
=
77 LAZY_INSTANCE_INITIALIZER
;
79 class TileManagerPerfTest
: public testing::Test
{
82 : memory_limit_policy_(ALLOW_ANYTHING
),
85 proxy_(base::MessageLoopProxy::current()),
86 host_impl_(ImplSidePaintingSettings(10000),
88 &shared_bitmap_manager_
),
90 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
91 kTimeCheckInterval
) {}
93 void SetTreePriority(TreePriority tree_priority
) {
94 GlobalStateThatImpactsTilePriority state
;
95 gfx::Size
tile_size(256, 256);
97 state
.soft_memory_limit_in_bytes
= 100 * 1000 * 1000;
98 state
.num_resources_limit
= max_tiles_
;
99 state
.hard_memory_limit_in_bytes
= state
.soft_memory_limit_in_bytes
* 2;
100 state
.memory_limit_policy
= memory_limit_policy_
;
101 state
.tree_priority
= tree_priority
;
103 global_state_
= state
;
104 host_impl_
.resource_pool()->SetResourceUsageLimits(
105 state
.soft_memory_limit_in_bytes
, 0, state
.num_resources_limit
);
106 host_impl_
.tile_manager()->SetGlobalStateForTesting(state
);
109 virtual void SetUp() OVERRIDE
{
110 picture_pile_
= FakePicturePileImpl::CreateInfiniteFilledPile();
111 InitializeRenderer();
112 SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES
);
115 virtual void InitializeRenderer() {
116 host_impl_
.InitializeRenderer(
117 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>());
118 tile_manager()->SetRasterizerForTesting(g_fake_rasterizer
.Pointer());
121 void SetupDefaultTrees(const gfx::Size
& layer_bounds
) {
122 gfx::Size
tile_size(100, 100);
124 scoped_refptr
<FakePicturePileImpl
> pending_pile
=
125 FakePicturePileImpl::CreateFilledPile(tile_size
, layer_bounds
);
126 scoped_refptr
<FakePicturePileImpl
> active_pile
=
127 FakePicturePileImpl::CreateFilledPile(tile_size
, layer_bounds
);
129 SetupTrees(pending_pile
, active_pile
);
132 void ActivateTree() {
133 host_impl_
.ActivateSyncTree();
134 CHECK(!host_impl_
.pending_tree());
135 pending_root_layer_
= NULL
;
136 active_root_layer_
= static_cast<FakePictureLayerImpl
*>(
137 host_impl_
.active_tree()->LayerById(id_
));
140 void SetupDefaultTreesWithFixedTileSize(const gfx::Size
& layer_bounds
,
141 const gfx::Size
& tile_size
) {
142 SetupDefaultTrees(layer_bounds
);
143 pending_root_layer_
->set_fixed_tile_size(tile_size
);
144 active_root_layer_
->set_fixed_tile_size(tile_size
);
147 void SetupTrees(scoped_refptr
<PicturePileImpl
> pending_pile
,
148 scoped_refptr
<PicturePileImpl
> active_pile
) {
149 SetupPendingTree(active_pile
);
151 SetupPendingTree(pending_pile
);
154 void SetupPendingTree(scoped_refptr
<PicturePileImpl
> pile
) {
155 host_impl_
.CreatePendingTree();
156 LayerTreeImpl
* pending_tree
= host_impl_
.pending_tree();
157 // Clear recycled tree.
158 pending_tree
->DetachLayerTree();
160 scoped_ptr
<FakePictureLayerImpl
> pending_layer
=
161 FakePictureLayerImpl::CreateWithPile(pending_tree
, id_
, pile
);
162 pending_layer
->SetDrawsContent(true);
163 pending_tree
->SetRootLayer(pending_layer
.PassAs
<LayerImpl
>());
165 pending_root_layer_
= static_cast<FakePictureLayerImpl
*>(
166 host_impl_
.pending_tree()->LayerById(id_
));
167 pending_root_layer_
->DoPostCommitInitializationIfNeeded();
170 void CreateHighLowResAndSetAllTilesVisible() {
171 // Active layer must get updated first so pending layer can share from it.
172 active_root_layer_
->CreateDefaultTilingsAndTiles();
173 active_root_layer_
->SetAllTilesVisible();
174 pending_root_layer_
->CreateDefaultTilingsAndTiles();
175 pending_root_layer_
->SetAllTilesVisible();
178 void RunRasterQueueConstructTest(const std::string
& test_name
,
180 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
181 SMOOTHNESS_TAKES_PRIORITY
,
182 NEW_CONTENT_TAKES_PRIORITY
};
183 int priority_count
= 0;
185 std::vector
<LayerImpl
*> layers
= CreateLayers(layer_count
, 10);
186 for (unsigned i
= 0; i
< layers
.size(); ++i
)
187 layers
[i
]->UpdateTiles(NULL
);
191 RasterTilePriorityQueue queue
;
192 host_impl_
.BuildRasterQueue(&queue
, priorities
[priority_count
]);
193 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
195 } while (!timer_
.HasTimeLimitExpired());
197 perf_test::PrintResult("tile_manager_raster_tile_queue_construct",
200 timer_
.LapsPerSecond(),
205 void RunRasterQueueConstructAndIterateTest(const std::string
& test_name
,
207 unsigned tile_count
) {
208 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
209 SMOOTHNESS_TAKES_PRIORITY
,
210 NEW_CONTENT_TAKES_PRIORITY
};
212 std::vector
<LayerImpl
*> layers
= CreateLayers(layer_count
, 100);
213 for (unsigned i
= 0; i
< layers
.size(); ++i
)
214 layers
[i
]->UpdateTiles(NULL
);
216 int priority_count
= 0;
219 int count
= tile_count
;
220 RasterTilePriorityQueue queue
;
221 host_impl_
.BuildRasterQueue(&queue
, priorities
[priority_count
]);
223 ASSERT_FALSE(queue
.IsEmpty());
224 ASSERT_TRUE(queue
.Top() != NULL
);
227 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
229 } while (!timer_
.HasTimeLimitExpired());
231 perf_test::PrintResult(
232 "tile_manager_raster_tile_queue_construct_and_iterate",
235 timer_
.LapsPerSecond(),
240 void RunEvictionQueueConstructTest(const std::string
& test_name
,
242 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
243 SMOOTHNESS_TAKES_PRIORITY
,
244 NEW_CONTENT_TAKES_PRIORITY
};
245 int priority_count
= 0;
247 std::vector
<LayerImpl
*> layers
= CreateLayers(layer_count
, 10);
248 for (unsigned i
= 0; i
< layers
.size(); ++i
) {
249 FakePictureLayerImpl
* layer
=
250 static_cast<FakePictureLayerImpl
*>(layers
[i
]);
251 layer
->UpdateTiles(NULL
);
252 for (size_t j
= 0; j
< layer
->GetTilings()->num_tilings(); ++j
) {
253 tile_manager()->InitializeTilesWithResourcesForTesting(
254 layer
->GetTilings()->tiling_at(j
)->AllTilesForTesting());
260 EvictionTilePriorityQueue queue
;
261 host_impl_
.BuildEvictionQueue(&queue
, priorities
[priority_count
]);
262 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
264 } while (!timer_
.HasTimeLimitExpired());
266 perf_test::PrintResult("tile_manager_eviction_tile_queue_construct",
269 timer_
.LapsPerSecond(),
274 void RunEvictionQueueConstructAndIterateTest(const std::string
& test_name
,
276 unsigned tile_count
) {
277 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
278 SMOOTHNESS_TAKES_PRIORITY
,
279 NEW_CONTENT_TAKES_PRIORITY
};
280 int priority_count
= 0;
282 std::vector
<LayerImpl
*> layers
= CreateLayers(layer_count
, tile_count
);
283 for (unsigned i
= 0; i
< layers
.size(); ++i
) {
284 FakePictureLayerImpl
* layer
=
285 static_cast<FakePictureLayerImpl
*>(layers
[i
]);
286 layer
->UpdateTiles(NULL
);
287 for (size_t j
= 0; j
< layer
->GetTilings()->num_tilings(); ++j
) {
288 tile_manager()->InitializeTilesWithResourcesForTesting(
289 layer
->GetTilings()->tiling_at(j
)->AllTilesForTesting());
295 int count
= tile_count
;
296 EvictionTilePriorityQueue queue
;
297 host_impl_
.BuildEvictionQueue(&queue
, priorities
[priority_count
]);
299 ASSERT_FALSE(queue
.IsEmpty());
300 ASSERT_TRUE(queue
.Top() != NULL
);
303 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
305 } while (!timer_
.HasTimeLimitExpired());
307 perf_test::PrintResult(
308 "tile_manager_eviction_tile_queue_construct_and_iterate",
311 timer_
.LapsPerSecond(),
316 std::vector
<LayerImpl
*> CreateLayers(int layer_count
,
317 int tiles_per_layer_count
) {
318 // Compute the width/height required for high res to get
319 // tiles_per_layer_count tiles.
320 float width
= std::sqrt(static_cast<float>(tiles_per_layer_count
));
321 float height
= tiles_per_layer_count
/ width
;
323 // Adjust the width and height to account for the fact that tiles
324 // are bigger than 1x1. Also, account for the fact that that we
325 // will be creating one high res and one low res tiling. That is,
326 // width and height should be smaller by sqrt(1 + low_res_scale).
327 // This gives us _approximately_ correct counts.
328 width
*= settings_
.default_tile_size
.width() /
329 std::sqrt(1 + settings_
.low_res_contents_scale_factor
);
330 height
*= settings_
.default_tile_size
.height() /
331 std::sqrt(1 + settings_
.low_res_contents_scale_factor
);
333 // Ensure that we start with blank trees and no tiles.
334 host_impl_
.ResetTreesForTesting();
335 tile_manager()->CleanUpReleasedTilesForTesting();
337 gfx::Size
layer_bounds(width
, height
);
338 gfx::Size
viewport(width
/ 5, height
/ 5);
339 host_impl_
.SetViewportSize(viewport
);
340 SetupDefaultTreesWithFixedTileSize(layer_bounds
,
341 settings_
.default_tile_size
);
343 active_root_layer_
->CreateDefaultTilingsAndTiles();
344 pending_root_layer_
->CreateDefaultTilingsAndTiles();
346 std::vector
<LayerImpl
*> layers
;
348 // Pending layer counts as one layer.
349 layers
.push_back(pending_root_layer_
);
350 int next_id
= id_
+ 1;
352 // Create the rest of the layers as children of the root layer.
353 while (static_cast<int>(layers
.size()) < layer_count
) {
354 scoped_ptr
<FakePictureLayerImpl
> layer
=
355 FakePictureLayerImpl::CreateWithPile(
356 host_impl_
.pending_tree(), next_id
, picture_pile_
);
357 layer
->SetBounds(layer_bounds
);
358 layers
.push_back(layer
.get());
359 pending_root_layer_
->AddChild(layer
.PassAs
<LayerImpl
>());
361 FakePictureLayerImpl
* fake_layer
=
362 static_cast<FakePictureLayerImpl
*>(layers
.back());
364 fake_layer
->SetDrawsContent(true);
365 fake_layer
->DoPostCommitInitializationIfNeeded();
366 fake_layer
->CreateDefaultTilingsAndTiles();
373 GlobalStateThatImpactsTilePriority
GlobalStateForTest() {
374 GlobalStateThatImpactsTilePriority state
;
375 gfx::Size tile_size
= settings_
.default_tile_size
;
376 state
.soft_memory_limit_in_bytes
=
378 static_cast<size_t>(tile_size
.width() * tile_size
.height());
379 state
.hard_memory_limit_in_bytes
= state
.soft_memory_limit_in_bytes
;
380 state
.num_resources_limit
= 10000;
381 state
.memory_limit_policy
= ALLOW_ANYTHING
;
382 state
.tree_priority
= SMOOTHNESS_TAKES_PRIORITY
;
386 void RunManageTilesTest(const std::string
& test_name
,
388 int approximate_tile_count_per_layer
) {
389 std::vector
<LayerImpl
*> layers
=
390 CreateLayers(layer_count
, approximate_tile_count_per_layer
);
393 host_impl_
.UpdateCurrentFrameTime();
394 for (unsigned i
= 0; i
< layers
.size(); ++i
)
395 layers
[i
]->UpdateTiles(NULL
);
397 GlobalStateThatImpactsTilePriority
global_state(GlobalStateForTest());
398 tile_manager()->ManageTiles(global_state
);
399 tile_manager()->UpdateVisibleTiles();
401 host_impl_
.ResetCurrentFrameTimeForNextFrame();
402 } while (!timer_
.HasTimeLimitExpired());
404 perf_test::PrintResult(
405 "manage_tiles", "", test_name
, timer_
.LapsPerSecond(), "runs/s", true);
408 TileManager
* tile_manager() { return host_impl_
.tile_manager(); }
411 GlobalStateThatImpactsTilePriority global_state_
;
413 TestSharedBitmapManager shared_bitmap_manager_
;
414 TileMemoryLimitPolicy memory_limit_policy_
;
417 FakeImplProxy proxy_
;
418 FakeLayerTreeHostImpl host_impl_
;
419 FakePictureLayerImpl
* pending_root_layer_
;
420 FakePictureLayerImpl
* active_root_layer_
;
422 scoped_refptr
<FakePicturePileImpl
> picture_pile_
;
423 LayerTreeSettings settings_
;
426 TEST_F(TileManagerPerfTest
, ManageTiles
) {
427 RunManageTilesTest("2_100", 2, 100);
428 RunManageTilesTest("2_500", 2, 500);
429 RunManageTilesTest("2_1000", 2, 1000);
430 RunManageTilesTest("10_100", 10, 100);
431 RunManageTilesTest("10_500", 10, 500);
432 RunManageTilesTest("10_1000", 10, 1000);
433 RunManageTilesTest("50_100", 100, 100);
434 RunManageTilesTest("50_500", 100, 500);
435 RunManageTilesTest("50_1000", 100, 1000);
438 TEST_F(TileManagerPerfTest
, RasterTileQueueConstruct
) {
439 RunRasterQueueConstructTest("2", 2);
440 RunRasterQueueConstructTest("10", 10);
441 RunRasterQueueConstructTest("50", 50);
444 TEST_F(TileManagerPerfTest
, RasterTileQueueConstructAndIterate
) {
445 RunRasterQueueConstructAndIterateTest("2_16", 2, 16);
446 RunRasterQueueConstructAndIterateTest("2_32", 2, 32);
447 RunRasterQueueConstructAndIterateTest("2_64", 2, 64);
448 RunRasterQueueConstructAndIterateTest("2_128", 2, 128);
449 RunRasterQueueConstructAndIterateTest("10_16", 10, 16);
450 RunRasterQueueConstructAndIterateTest("10_32", 10, 32);
451 RunRasterQueueConstructAndIterateTest("10_64", 10, 64);
452 RunRasterQueueConstructAndIterateTest("10_128", 10, 128);
453 RunRasterQueueConstructAndIterateTest("50_16", 50, 16);
454 RunRasterQueueConstructAndIterateTest("50_32", 50, 32);
455 RunRasterQueueConstructAndIterateTest("50_64", 50, 64);
456 RunRasterQueueConstructAndIterateTest("50_128", 50, 128);
459 TEST_F(TileManagerPerfTest
, EvictionTileQueueConstruct
) {
460 RunEvictionQueueConstructTest("2", 2);
461 RunEvictionQueueConstructTest("10", 10);
462 RunEvictionQueueConstructTest("50", 50);
465 TEST_F(TileManagerPerfTest
, EvictionTileQueueConstructAndIterate
) {
466 RunEvictionQueueConstructAndIterateTest("2_16", 2, 16);
467 RunEvictionQueueConstructAndIterateTest("2_32", 2, 32);
468 RunEvictionQueueConstructAndIterateTest("2_64", 2, 64);
469 RunEvictionQueueConstructAndIterateTest("2_128", 2, 128);
470 RunEvictionQueueConstructAndIterateTest("10_16", 10, 16);
471 RunEvictionQueueConstructAndIterateTest("10_32", 10, 32);
472 RunEvictionQueueConstructAndIterateTest("10_64", 10, 64);
473 RunEvictionQueueConstructAndIterateTest("10_128", 10, 128);
474 RunEvictionQueueConstructAndIterateTest("50_16", 50, 16);
475 RunEvictionQueueConstructAndIterateTest("50_32", 50, 32);
476 RunEvictionQueueConstructAndIterateTest("50_64", 50, 64);
477 RunEvictionQueueConstructAndIterateTest("50_128", 50, 128);