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/raster_buffer.h"
8 #include "cc/resources/tile.h"
9 #include "cc/resources/tile_priority.h"
10 #include "cc/test/begin_frame_args_test.h"
11 #include "cc/test/fake_impl_proxy.h"
12 #include "cc/test/fake_layer_tree_host_impl.h"
13 #include "cc/test/fake_output_surface.h"
14 #include "cc/test/fake_output_surface_client.h"
15 #include "cc/test/fake_picture_layer_impl.h"
16 #include "cc/test/fake_picture_pile_impl.h"
17 #include "cc/test/fake_tile_manager.h"
18 #include "cc/test/fake_tile_manager_client.h"
19 #include "cc/test/impl_side_painting_settings.h"
20 #include "cc/test/test_shared_bitmap_manager.h"
21 #include "cc/test/test_tile_priorities.h"
22 #include "cc/trees/layer_tree_impl.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "testing/perf/perf_test.h"
27 #include "ui/gfx/frame_time.h"
33 static const int kTimeLimitMillis
= 2000;
34 static const int kWarmupRuns
= 5;
35 static const int kTimeCheckInterval
= 10;
37 class FakeRasterizerImpl
: public Rasterizer
, public RasterizerTaskClient
{
39 // Overridden from Rasterizer:
40 void SetClient(RasterizerClient
* client
) override
{}
41 void Shutdown() override
{}
42 void ScheduleTasks(RasterTaskQueue
* queue
) override
{
43 for (RasterTaskQueue::Item::Vector::const_iterator it
=
45 it
!= queue
->items
.end();
47 RasterTask
* task
= it
->task
;
50 task
->ScheduleOnOriginThread(this);
53 completed_tasks_
.push_back(task
);
56 void CheckForCompletedTasks() override
{
57 for (RasterTask::Vector::iterator it
= completed_tasks_
.begin();
58 it
!= completed_tasks_
.end();
60 RasterTask
* task
= it
->get();
63 task
->CompleteOnOriginThread(this);
66 task
->RunReplyOnOriginThread();
68 completed_tasks_
.clear();
71 // Overridden from RasterizerTaskClient:
72 scoped_ptr
<RasterBuffer
> AcquireBufferForRaster(
73 const Resource
* resource
) override
{
76 void ReleaseBufferForRaster(scoped_ptr
<RasterBuffer
> buffer
) override
{}
79 RasterTask::Vector completed_tasks_
;
81 base::LazyInstance
<FakeRasterizerImpl
> g_fake_rasterizer
=
82 LAZY_INSTANCE_INITIALIZER
;
84 class TileManagerPerfTest
: public testing::Test
{
87 : memory_limit_policy_(ALLOW_ANYTHING
),
90 proxy_(base::MessageLoopProxy::current()),
91 host_impl_(ImplSidePaintingSettings(10000),
93 &shared_bitmap_manager_
),
95 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
96 kTimeCheckInterval
) {}
98 void SetTreePriority(TreePriority tree_priority
) {
99 GlobalStateThatImpactsTilePriority state
;
100 gfx::Size
tile_size(256, 256);
102 state
.soft_memory_limit_in_bytes
= 100 * 1000 * 1000;
103 state
.num_resources_limit
= max_tiles_
;
104 state
.hard_memory_limit_in_bytes
= state
.soft_memory_limit_in_bytes
* 2;
105 state
.memory_limit_policy
= memory_limit_policy_
;
106 state
.tree_priority
= tree_priority
;
108 global_state_
= state
;
109 host_impl_
.resource_pool()->SetResourceUsageLimits(
110 state
.soft_memory_limit_in_bytes
, 0, state
.num_resources_limit
);
111 host_impl_
.tile_manager()->SetGlobalStateForTesting(state
);
114 void SetUp() override
{
115 InitializeRenderer();
116 SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES
);
119 virtual void InitializeRenderer() {
120 host_impl_
.InitializeRenderer(FakeOutputSurface::Create3d().Pass());
121 tile_manager()->SetRasterizerForTesting(g_fake_rasterizer
.Pointer());
124 void SetupDefaultTrees(const gfx::Size
& layer_bounds
) {
125 scoped_refptr
<FakePicturePileImpl
> pending_pile
=
126 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize
, layer_bounds
);
127 scoped_refptr
<FakePicturePileImpl
> active_pile
=
128 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize
, layer_bounds
);
130 SetupTrees(pending_pile
, active_pile
);
133 void ActivateTree() {
134 host_impl_
.ActivateSyncTree();
135 CHECK(!host_impl_
.pending_tree());
136 pending_root_layer_
= NULL
;
137 active_root_layer_
= static_cast<FakePictureLayerImpl
*>(
138 host_impl_
.active_tree()->LayerById(id_
));
141 void SetupDefaultTreesWithFixedTileSize(const gfx::Size
& layer_bounds
,
142 const gfx::Size
& tile_size
) {
143 SetupDefaultTrees(layer_bounds
);
144 pending_root_layer_
->set_fixed_tile_size(tile_size
);
145 active_root_layer_
->set_fixed_tile_size(tile_size
);
148 void SetupTrees(scoped_refptr
<PicturePileImpl
> pending_pile
,
149 scoped_refptr
<PicturePileImpl
> active_pile
) {
150 SetupPendingTree(active_pile
);
152 SetupPendingTree(pending_pile
);
155 void SetupPendingTree(scoped_refptr
<PicturePileImpl
> pile
) {
156 host_impl_
.CreatePendingTree();
157 LayerTreeImpl
* pending_tree
= host_impl_
.pending_tree();
158 // Clear recycled tree.
159 pending_tree
->DetachLayerTree();
161 scoped_ptr
<FakePictureLayerImpl
> pending_layer
=
162 FakePictureLayerImpl::CreateWithRasterSource(pending_tree
, id_
, pile
);
163 pending_layer
->SetDrawsContent(true);
164 pending_tree
->SetRootLayer(pending_layer
.Pass());
166 pending_root_layer_
= static_cast<FakePictureLayerImpl
*>(
167 host_impl_
.pending_tree()->LayerById(id_
));
168 pending_root_layer_
->DoPostCommitInitializationIfNeeded();
171 void CreateHighLowResAndSetAllTilesVisible() {
172 // Active layer must get updated first so pending layer can share from it.
173 active_root_layer_
->CreateDefaultTilingsAndTiles();
174 active_root_layer_
->SetAllTilesVisible();
175 pending_root_layer_
->CreateDefaultTilingsAndTiles();
176 pending_root_layer_
->SetAllTilesVisible();
179 void RunRasterQueueConstructTest(const std::string
& test_name
,
181 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
182 SMOOTHNESS_TAKES_PRIORITY
,
183 NEW_CONTENT_TAKES_PRIORITY
};
184 int priority_count
= 0;
186 std::vector
<LayerImpl
*> layers
= CreateLayers(layer_count
, 10);
187 bool resourceless_software_draw
= false;
188 for (unsigned i
= 0; i
< layers
.size(); ++i
) {
189 layers
[i
]->UpdateTiles(Occlusion(), resourceless_software_draw
);
194 RasterTilePriorityQueue queue
;
195 host_impl_
.BuildRasterQueue(&queue
, priorities
[priority_count
]);
196 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
198 } while (!timer_
.HasTimeLimitExpired());
200 perf_test::PrintResult("tile_manager_raster_tile_queue_construct",
203 timer_
.LapsPerSecond(),
208 void RunRasterQueueConstructAndIterateTest(const std::string
& test_name
,
210 unsigned tile_count
) {
211 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
212 SMOOTHNESS_TAKES_PRIORITY
,
213 NEW_CONTENT_TAKES_PRIORITY
};
215 std::vector
<LayerImpl
*> layers
= CreateLayers(layer_count
, 100);
216 bool resourceless_software_draw
= false;
217 for (unsigned i
= 0; i
< layers
.size(); ++i
) {
218 layers
[i
]->UpdateTiles(Occlusion(), resourceless_software_draw
);
221 int priority_count
= 0;
224 int count
= tile_count
;
225 RasterTilePriorityQueue queue
;
226 host_impl_
.BuildRasterQueue(&queue
, priorities
[priority_count
]);
228 ASSERT_FALSE(queue
.IsEmpty());
229 ASSERT_TRUE(queue
.Top() != NULL
);
232 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
234 } while (!timer_
.HasTimeLimitExpired());
236 perf_test::PrintResult(
237 "tile_manager_raster_tile_queue_construct_and_iterate",
240 timer_
.LapsPerSecond(),
245 void RunEvictionQueueConstructTest(const std::string
& test_name
,
247 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
248 SMOOTHNESS_TAKES_PRIORITY
,
249 NEW_CONTENT_TAKES_PRIORITY
};
250 int priority_count
= 0;
252 std::vector
<LayerImpl
*> layers
= CreateLayers(layer_count
, 10);
253 bool resourceless_software_draw
= false;
254 for (unsigned i
= 0; i
< layers
.size(); ++i
) {
255 FakePictureLayerImpl
* layer
=
256 static_cast<FakePictureLayerImpl
*>(layers
[i
]);
257 layer
->UpdateTiles(Occlusion(), resourceless_software_draw
);
258 for (size_t j
= 0; j
< layer
->GetTilings()->num_tilings(); ++j
) {
259 tile_manager()->InitializeTilesWithResourcesForTesting(
260 layer
->GetTilings()->tiling_at(j
)->AllTilesForTesting());
266 EvictionTilePriorityQueue queue
;
267 host_impl_
.BuildEvictionQueue(&queue
, priorities
[priority_count
]);
268 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
270 } while (!timer_
.HasTimeLimitExpired());
272 perf_test::PrintResult("tile_manager_eviction_tile_queue_construct",
275 timer_
.LapsPerSecond(),
280 void RunEvictionQueueConstructAndIterateTest(const std::string
& test_name
,
282 unsigned tile_count
) {
283 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
284 SMOOTHNESS_TAKES_PRIORITY
,
285 NEW_CONTENT_TAKES_PRIORITY
};
286 int priority_count
= 0;
288 std::vector
<LayerImpl
*> layers
= CreateLayers(layer_count
, tile_count
);
289 bool resourceless_software_draw
= false;
290 for (unsigned i
= 0; i
< layers
.size(); ++i
) {
291 FakePictureLayerImpl
* layer
=
292 static_cast<FakePictureLayerImpl
*>(layers
[i
]);
293 layer
->UpdateTiles(Occlusion(), resourceless_software_draw
);
294 for (size_t j
= 0; j
< layer
->GetTilings()->num_tilings(); ++j
) {
295 tile_manager()->InitializeTilesWithResourcesForTesting(
296 layer
->GetTilings()->tiling_at(j
)->AllTilesForTesting());
302 int count
= tile_count
;
303 EvictionTilePriorityQueue queue
;
304 host_impl_
.BuildEvictionQueue(&queue
, priorities
[priority_count
]);
306 ASSERT_FALSE(queue
.IsEmpty());
307 ASSERT_TRUE(queue
.Top() != NULL
);
310 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
312 } while (!timer_
.HasTimeLimitExpired());
314 perf_test::PrintResult(
315 "tile_manager_eviction_tile_queue_construct_and_iterate",
318 timer_
.LapsPerSecond(),
323 std::vector
<LayerImpl
*> CreateLayers(int layer_count
,
324 int tiles_per_layer_count
) {
325 // Compute the width/height required for high res to get
326 // tiles_per_layer_count tiles.
327 float width
= std::sqrt(static_cast<float>(tiles_per_layer_count
));
328 float height
= tiles_per_layer_count
/ width
;
330 // Adjust the width and height to account for the fact that tiles
331 // are bigger than 1x1. Also, account for the fact that that we
332 // will be creating one high res and one low res tiling. That is,
333 // width and height should be smaller by sqrt(1 + low_res_scale).
334 // This gives us _approximately_ correct counts.
335 width
*= settings_
.default_tile_size
.width() /
336 std::sqrt(1 + settings_
.low_res_contents_scale_factor
);
337 height
*= settings_
.default_tile_size
.height() /
338 std::sqrt(1 + settings_
.low_res_contents_scale_factor
);
340 // Ensure that we start with blank trees and no tiles.
341 host_impl_
.ResetTreesForTesting();
342 tile_manager()->FreeResourcesAndCleanUpReleasedTilesForTesting();
344 gfx::Size
layer_bounds(width
, height
);
345 gfx::Size
viewport(width
/ 5, height
/ 5);
346 host_impl_
.SetViewportSize(viewport
);
347 SetupDefaultTreesWithFixedTileSize(layer_bounds
,
348 settings_
.default_tile_size
);
350 active_root_layer_
->CreateDefaultTilingsAndTiles();
351 pending_root_layer_
->CreateDefaultTilingsAndTiles();
353 std::vector
<LayerImpl
*> layers
;
355 // Pending layer counts as one layer.
356 layers
.push_back(pending_root_layer_
);
357 int next_id
= id_
+ 1;
359 // Create the rest of the layers as children of the root layer.
360 scoped_refptr
<FakePicturePileImpl
> pile
=
361 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize
, layer_bounds
);
362 while (static_cast<int>(layers
.size()) < layer_count
) {
363 scoped_ptr
<FakePictureLayerImpl
> layer
=
364 FakePictureLayerImpl::CreateWithRasterSource(
365 host_impl_
.pending_tree(), next_id
, pile
);
366 layer
->SetBounds(layer_bounds
);
367 layers
.push_back(layer
.get());
368 pending_root_layer_
->AddChild(layer
.Pass());
370 FakePictureLayerImpl
* fake_layer
=
371 static_cast<FakePictureLayerImpl
*>(layers
.back());
373 fake_layer
->SetDrawsContent(true);
374 fake_layer
->DoPostCommitInitializationIfNeeded();
375 fake_layer
->CreateDefaultTilingsAndTiles();
382 GlobalStateThatImpactsTilePriority
GlobalStateForTest() {
383 GlobalStateThatImpactsTilePriority state
;
384 gfx::Size tile_size
= settings_
.default_tile_size
;
385 state
.soft_memory_limit_in_bytes
=
387 static_cast<size_t>(tile_size
.width() * tile_size
.height());
388 state
.hard_memory_limit_in_bytes
= state
.soft_memory_limit_in_bytes
;
389 state
.num_resources_limit
= 10000;
390 state
.memory_limit_policy
= ALLOW_ANYTHING
;
391 state
.tree_priority
= SMOOTHNESS_TAKES_PRIORITY
;
395 void RunManageTilesTest(const std::string
& test_name
,
397 int approximate_tile_count_per_layer
) {
398 std::vector
<LayerImpl
*> layers
=
399 CreateLayers(layer_count
, approximate_tile_count_per_layer
);
401 bool resourceless_software_draw
= false;
403 BeginFrameArgs args
= CreateBeginFrameArgsForTesting();
404 host_impl_
.UpdateCurrentBeginFrameArgs(args
);
405 for (unsigned i
= 0; i
< layers
.size(); ++i
) {
406 layers
[i
]->UpdateTiles(Occlusion(), resourceless_software_draw
);
409 GlobalStateThatImpactsTilePriority
global_state(GlobalStateForTest());
410 tile_manager()->ManageTiles(global_state
);
411 tile_manager()->UpdateVisibleTiles();
413 host_impl_
.ResetCurrentBeginFrameArgsForNextFrame();
414 } while (!timer_
.HasTimeLimitExpired());
416 perf_test::PrintResult(
417 "manage_tiles", "", test_name
, timer_
.LapsPerSecond(), "runs/s", true);
420 TileManager
* tile_manager() { return host_impl_
.tile_manager(); }
423 GlobalStateThatImpactsTilePriority global_state_
;
425 TestSharedBitmapManager shared_bitmap_manager_
;
426 TileMemoryLimitPolicy memory_limit_policy_
;
429 FakeImplProxy proxy_
;
430 FakeLayerTreeHostImpl host_impl_
;
431 FakePictureLayerImpl
* pending_root_layer_
;
432 FakePictureLayerImpl
* active_root_layer_
;
434 LayerTreeSettings settings_
;
436 static const gfx::Size kDefaultTileSize
;
439 const gfx::Size
TileManagerPerfTest::kDefaultTileSize(100, 100);
441 TEST_F(TileManagerPerfTest
, ManageTiles
) {
442 RunManageTilesTest("2_100", 2, 100);
443 RunManageTilesTest("2_500", 2, 500);
444 RunManageTilesTest("2_1000", 2, 1000);
445 RunManageTilesTest("10_100", 10, 100);
446 RunManageTilesTest("10_500", 10, 500);
447 RunManageTilesTest("10_1000", 10, 1000);
448 RunManageTilesTest("50_100", 100, 100);
449 RunManageTilesTest("50_500", 100, 500);
450 RunManageTilesTest("50_1000", 100, 1000);
453 TEST_F(TileManagerPerfTest
, RasterTileQueueConstruct
) {
454 RunRasterQueueConstructTest("2", 2);
455 RunRasterQueueConstructTest("10", 10);
456 RunRasterQueueConstructTest("50", 50);
459 TEST_F(TileManagerPerfTest
, RasterTileQueueConstructAndIterate
) {
460 RunRasterQueueConstructAndIterateTest("2_16", 2, 16);
461 RunRasterQueueConstructAndIterateTest("2_32", 2, 32);
462 RunRasterQueueConstructAndIterateTest("2_64", 2, 64);
463 RunRasterQueueConstructAndIterateTest("2_128", 2, 128);
464 RunRasterQueueConstructAndIterateTest("10_16", 10, 16);
465 RunRasterQueueConstructAndIterateTest("10_32", 10, 32);
466 RunRasterQueueConstructAndIterateTest("10_64", 10, 64);
467 RunRasterQueueConstructAndIterateTest("10_128", 10, 128);
468 RunRasterQueueConstructAndIterateTest("50_16", 50, 16);
469 RunRasterQueueConstructAndIterateTest("50_32", 50, 32);
470 RunRasterQueueConstructAndIterateTest("50_64", 50, 64);
471 RunRasterQueueConstructAndIterateTest("50_128", 50, 128);
474 TEST_F(TileManagerPerfTest
, EvictionTileQueueConstruct
) {
475 RunEvictionQueueConstructTest("2", 2);
476 RunEvictionQueueConstructTest("10", 10);
477 RunEvictionQueueConstructTest("50", 50);
480 TEST_F(TileManagerPerfTest
, EvictionTileQueueConstructAndIterate
) {
481 RunEvictionQueueConstructAndIterateTest("2_16", 2, 16);
482 RunEvictionQueueConstructAndIterateTest("2_32", 2, 32);
483 RunEvictionQueueConstructAndIterateTest("2_64", 2, 64);
484 RunEvictionQueueConstructAndIterateTest("2_128", 2, 128);
485 RunEvictionQueueConstructAndIterateTest("10_16", 10, 16);
486 RunEvictionQueueConstructAndIterateTest("10_32", 10, 32);
487 RunEvictionQueueConstructAndIterateTest("10_64", 10, 64);
488 RunEvictionQueueConstructAndIterateTest("10_128", 10, 128);
489 RunEvictionQueueConstructAndIterateTest("50_16", 50, 16);
490 RunEvictionQueueConstructAndIterateTest("50_32", 50, 32);
491 RunEvictionQueueConstructAndIterateTest("50_64", 50, 64);
492 RunEvictionQueueConstructAndIterateTest("50_128", 50, 128);