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_task_graph_runner.h"
22 #include "cc/test/test_tile_priorities.h"
23 #include "cc/trees/layer_tree_impl.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "testing/perf/perf_test.h"
28 #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 FakeTileTaskRunnerImpl
: public TileTaskRunner
, public TileTaskClient
{
39 // Overridden from TileTaskRunner:
40 void SetClient(TileTaskRunnerClient
* client
) override
{}
41 void Shutdown() override
{}
42 void ScheduleTasks(TileTaskQueue
* queue
) override
{
43 for (TileTaskQueue::Item::Vector::const_iterator it
= queue
->items
.begin();
44 it
!= queue
->items
.end(); ++it
) {
45 RasterTask
* task
= it
->task
;
48 task
->ScheduleOnOriginThread(this);
51 completed_tasks_
.push_back(task
);
54 void CheckForCompletedTasks() override
{
55 for (RasterTask::Vector::iterator it
= completed_tasks_
.begin();
56 it
!= completed_tasks_
.end();
58 RasterTask
* task
= it
->get();
61 task
->CompleteOnOriginThread(this);
64 task
->RunReplyOnOriginThread();
66 completed_tasks_
.clear();
68 ResourceFormat
GetResourceFormat() override
{
72 // Overridden from TileTaskClient:
73 scoped_ptr
<RasterBuffer
> AcquireBufferForRaster(
74 const Resource
* resource
) override
{
77 void ReleaseBufferForRaster(scoped_ptr
<RasterBuffer
> buffer
) override
{}
80 RasterTask::Vector completed_tasks_
;
82 base::LazyInstance
<FakeTileTaskRunnerImpl
> g_fake_tile_task_runner
=
83 LAZY_INSTANCE_INITIALIZER
;
85 class TileManagerPerfTest
: public testing::Test
{
88 : memory_limit_policy_(ALLOW_ANYTHING
),
91 proxy_(base::MessageLoopProxy::current()),
92 host_impl_(ImplSidePaintingSettings(10000),
94 &shared_bitmap_manager_
,
97 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
98 kTimeCheckInterval
) {}
100 void SetTreePriority(TreePriority tree_priority
) {
101 GlobalStateThatImpactsTilePriority state
;
102 gfx::Size
tile_size(256, 256);
104 state
.soft_memory_limit_in_bytes
= 100 * 1000 * 1000;
105 state
.num_resources_limit
= max_tiles_
;
106 state
.hard_memory_limit_in_bytes
= state
.soft_memory_limit_in_bytes
* 2;
107 state
.memory_limit_policy
= memory_limit_policy_
;
108 state
.tree_priority
= tree_priority
;
110 global_state_
= state
;
111 host_impl_
.resource_pool()->SetResourceUsageLimits(
112 state
.soft_memory_limit_in_bytes
, 0, state
.num_resources_limit
);
113 host_impl_
.tile_manager()->SetGlobalStateForTesting(state
);
116 void SetUp() override
{
117 InitializeRenderer();
118 SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES
);
121 virtual void InitializeRenderer() {
122 host_impl_
.InitializeRenderer(FakeOutputSurface::Create3d().Pass());
123 tile_manager()->SetTileTaskRunnerForTesting(
124 g_fake_tile_task_runner
.Pointer());
127 void SetupDefaultTrees(const gfx::Size
& layer_bounds
) {
128 scoped_refptr
<FakePicturePileImpl
> pending_pile
=
129 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize
, layer_bounds
);
130 scoped_refptr
<FakePicturePileImpl
> active_pile
=
131 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize
, layer_bounds
);
133 SetupTrees(pending_pile
, active_pile
);
136 void ActivateTree() {
137 host_impl_
.ActivateSyncTree();
138 CHECK(!host_impl_
.pending_tree());
139 pending_root_layer_
= NULL
;
140 active_root_layer_
= static_cast<FakePictureLayerImpl
*>(
141 host_impl_
.active_tree()->LayerById(id_
));
144 void SetupDefaultTreesWithFixedTileSize(const gfx::Size
& layer_bounds
,
145 const gfx::Size
& tile_size
) {
146 SetupDefaultTrees(layer_bounds
);
147 pending_root_layer_
->set_fixed_tile_size(tile_size
);
148 active_root_layer_
->set_fixed_tile_size(tile_size
);
151 void SetupTrees(scoped_refptr
<PicturePileImpl
> pending_pile
,
152 scoped_refptr
<PicturePileImpl
> active_pile
) {
153 SetupPendingTree(active_pile
);
155 SetupPendingTree(pending_pile
);
158 void SetupPendingTree(scoped_refptr
<PicturePileImpl
> pile
) {
159 host_impl_
.CreatePendingTree();
160 LayerTreeImpl
* pending_tree
= host_impl_
.pending_tree();
161 // Clear recycled tree.
162 pending_tree
->DetachLayerTree();
164 scoped_ptr
<FakePictureLayerImpl
> pending_layer
=
165 FakePictureLayerImpl::CreateWithRasterSource(pending_tree
, id_
, pile
);
166 pending_layer
->SetDrawsContent(true);
167 pending_layer
->SetHasRenderSurface(true);
168 pending_tree
->SetRootLayer(pending_layer
.Pass());
170 pending_root_layer_
= static_cast<FakePictureLayerImpl
*>(
171 host_impl_
.pending_tree()->LayerById(id_
));
174 void RunRasterQueueConstructTest(const std::string
& test_name
,
176 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
177 SMOOTHNESS_TAKES_PRIORITY
,
178 NEW_CONTENT_TAKES_PRIORITY
};
179 int priority_count
= 0;
181 std::vector
<FakePictureLayerImpl
*> layers
= CreateLayers(layer_count
, 10);
182 bool resourceless_software_draw
= false;
183 for (const auto& layer
: layers
)
184 layer
->UpdateTiles(resourceless_software_draw
);
188 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
189 priorities
[priority_count
], RasterTilePriorityQueue::Type::ALL
));
190 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
192 } while (!timer_
.HasTimeLimitExpired());
194 perf_test::PrintResult("tile_manager_raster_tile_queue_construct",
197 timer_
.LapsPerSecond(),
202 void RunRasterQueueConstructAndIterateTest(const std::string
& test_name
,
205 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
206 SMOOTHNESS_TAKES_PRIORITY
,
207 NEW_CONTENT_TAKES_PRIORITY
};
209 std::vector
<FakePictureLayerImpl
*> layers
= CreateLayers(layer_count
, 100);
210 bool resourceless_software_draw
= false;
211 for (const auto& layer
: layers
)
212 layer
->UpdateTiles(resourceless_software_draw
);
214 int priority_count
= 0;
217 int count
= tile_count
;
218 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
219 priorities
[priority_count
], RasterTilePriorityQueue::Type::ALL
));
221 ASSERT_FALSE(queue
->IsEmpty());
222 ASSERT_TRUE(queue
->Top() != NULL
);
225 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
227 } while (!timer_
.HasTimeLimitExpired());
229 perf_test::PrintResult(
230 "tile_manager_raster_tile_queue_construct_and_iterate",
233 timer_
.LapsPerSecond(),
238 void RunEvictionQueueConstructTest(const std::string
& test_name
,
240 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
241 SMOOTHNESS_TAKES_PRIORITY
,
242 NEW_CONTENT_TAKES_PRIORITY
};
243 int priority_count
= 0;
245 std::vector
<FakePictureLayerImpl
*> layers
= CreateLayers(layer_count
, 10);
246 bool resourceless_software_draw
= false;
247 for (const auto& layer
: layers
) {
248 layer
->UpdateTiles(resourceless_software_draw
);
249 for (size_t i
= 0; i
< layer
->num_tilings(); ++i
) {
250 tile_manager()->InitializeTilesWithResourcesForTesting(
251 layer
->tilings()->tiling_at(i
)->AllTilesForTesting());
257 scoped_ptr
<EvictionTilePriorityQueue
> queue(
258 host_impl_
.BuildEvictionQueue(priorities
[priority_count
]));
259 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
261 } while (!timer_
.HasTimeLimitExpired());
263 perf_test::PrintResult("tile_manager_eviction_tile_queue_construct",
266 timer_
.LapsPerSecond(),
271 void RunEvictionQueueConstructAndIterateTest(const std::string
& test_name
,
274 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
275 SMOOTHNESS_TAKES_PRIORITY
,
276 NEW_CONTENT_TAKES_PRIORITY
};
277 int priority_count
= 0;
279 std::vector
<FakePictureLayerImpl
*> layers
=
280 CreateLayers(layer_count
, tile_count
);
281 bool resourceless_software_draw
= false;
282 for (const auto& layer
: layers
) {
283 layer
->UpdateTiles(resourceless_software_draw
);
284 for (size_t i
= 0; i
< layer
->num_tilings(); ++i
) {
285 tile_manager()->InitializeTilesWithResourcesForTesting(
286 layer
->tilings()->tiling_at(i
)->AllTilesForTesting());
292 int count
= tile_count
;
293 scoped_ptr
<EvictionTilePriorityQueue
> queue(
294 host_impl_
.BuildEvictionQueue(priorities
[priority_count
]));
296 ASSERT_FALSE(queue
->IsEmpty());
297 ASSERT_TRUE(queue
->Top() != NULL
);
300 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
302 } while (!timer_
.HasTimeLimitExpired());
304 perf_test::PrintResult(
305 "tile_manager_eviction_tile_queue_construct_and_iterate",
308 timer_
.LapsPerSecond(),
313 std::vector
<FakePictureLayerImpl
*> CreateLayers(int layer_count
,
314 int tiles_per_layer_count
) {
315 // Compute the width/height required for high res to get
316 // tiles_per_layer_count tiles.
317 float width
= std::sqrt(static_cast<float>(tiles_per_layer_count
));
318 float height
= tiles_per_layer_count
/ width
;
320 // Adjust the width and height to account for the fact that tiles
321 // are bigger than 1x1. Also, account for the fact that that we
322 // will be creating one high res and one low res tiling. That is,
323 // width and height should be smaller by sqrt(1 + low_res_scale).
324 // This gives us _approximately_ correct counts.
325 width
*= settings_
.default_tile_size
.width() /
326 std::sqrt(1 + settings_
.low_res_contents_scale_factor
);
327 height
*= settings_
.default_tile_size
.height() /
328 std::sqrt(1 + settings_
.low_res_contents_scale_factor
);
330 // Ensure that we start with blank trees and no tiles.
331 host_impl_
.ResetTreesForTesting();
332 tile_manager()->FreeResourcesAndCleanUpReleasedTilesForTesting();
334 gfx::Size
layer_bounds(width
, height
);
335 gfx::Size
viewport(width
/ 5, height
/ 5);
336 host_impl_
.SetViewportSize(viewport
);
337 SetupDefaultTreesWithFixedTileSize(layer_bounds
,
338 settings_
.default_tile_size
);
340 std::vector
<FakePictureLayerImpl
*> layers
;
342 // Pending layer counts as one layer.
343 layers
.push_back(pending_root_layer_
);
344 int next_id
= id_
+ 1;
346 // Create the rest of the layers as children of the root layer.
347 scoped_refptr
<FakePicturePileImpl
> pile
=
348 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize
, layer_bounds
);
349 while (static_cast<int>(layers
.size()) < layer_count
) {
350 scoped_ptr
<FakePictureLayerImpl
> layer
=
351 FakePictureLayerImpl::CreateWithRasterSource(
352 host_impl_
.pending_tree(), next_id
, pile
);
353 layer
->SetBounds(layer_bounds
);
354 layer
->SetDrawsContent(true);
355 layers
.push_back(layer
.get());
356 pending_root_layer_
->AddChild(layer
.Pass());
360 bool update_lcd_text
= false;
361 host_impl_
.pending_tree()->UpdateDrawProperties(update_lcd_text
);
362 for (FakePictureLayerImpl
* layer
: layers
)
363 layer
->CreateAllTiles();
368 GlobalStateThatImpactsTilePriority
GlobalStateForTest() {
369 GlobalStateThatImpactsTilePriority state
;
370 gfx::Size tile_size
= settings_
.default_tile_size
;
371 state
.soft_memory_limit_in_bytes
=
373 static_cast<size_t>(tile_size
.width() * tile_size
.height());
374 state
.hard_memory_limit_in_bytes
= state
.soft_memory_limit_in_bytes
;
375 state
.num_resources_limit
= 10000;
376 state
.memory_limit_policy
= ALLOW_ANYTHING
;
377 state
.tree_priority
= SMOOTHNESS_TAKES_PRIORITY
;
381 void RunPrepareTilesTest(const std::string
& test_name
,
383 int approximate_tile_count_per_layer
) {
384 std::vector
<FakePictureLayerImpl
*> layers
=
385 CreateLayers(layer_count
, approximate_tile_count_per_layer
);
387 bool resourceless_software_draw
= false;
389 BeginFrameArgs args
=
390 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
);
391 host_impl_
.UpdateCurrentBeginFrameArgs(args
);
392 for (const auto& layer
: layers
)
393 layer
->UpdateTiles(resourceless_software_draw
);
395 GlobalStateThatImpactsTilePriority
global_state(GlobalStateForTest());
396 tile_manager()->PrepareTiles(global_state
);
397 tile_manager()->UpdateVisibleTiles(global_state
);
399 host_impl_
.ResetCurrentBeginFrameArgsForNextFrame();
400 } while (!timer_
.HasTimeLimitExpired());
402 perf_test::PrintResult("prepare_tiles", "", test_name
,
403 timer_
.LapsPerSecond(), "runs/s", true);
406 TileManager
* tile_manager() { return host_impl_
.tile_manager(); }
409 GlobalStateThatImpactsTilePriority global_state_
;
411 TestSharedBitmapManager shared_bitmap_manager_
;
412 TestTaskGraphRunner task_graph_runner_
;
413 TileMemoryLimitPolicy memory_limit_policy_
;
416 FakeImplProxy proxy_
;
417 FakeLayerTreeHostImpl host_impl_
;
418 FakePictureLayerImpl
* pending_root_layer_
;
419 FakePictureLayerImpl
* active_root_layer_
;
421 LayerTreeSettings settings_
;
423 static const gfx::Size kDefaultTileSize
;
426 const gfx::Size
TileManagerPerfTest::kDefaultTileSize(100, 100);
428 TEST_F(TileManagerPerfTest
, PrepareTiles
) {
429 RunPrepareTilesTest("2_100", 2, 100);
430 RunPrepareTilesTest("2_500", 2, 500);
431 RunPrepareTilesTest("2_1000", 2, 1000);
432 RunPrepareTilesTest("10_100", 10, 100);
433 RunPrepareTilesTest("10_500", 10, 500);
434 RunPrepareTilesTest("10_1000", 10, 1000);
435 RunPrepareTilesTest("50_100", 100, 100);
436 RunPrepareTilesTest("50_500", 100, 500);
437 RunPrepareTilesTest("50_1000", 100, 1000);
440 TEST_F(TileManagerPerfTest
, RasterTileQueueConstruct
) {
441 RunRasterQueueConstructTest("2", 2);
442 RunRasterQueueConstructTest("10", 10);
443 RunRasterQueueConstructTest("50", 50);
446 TEST_F(TileManagerPerfTest
, RasterTileQueueConstructAndIterate
) {
447 RunRasterQueueConstructAndIterateTest("2_16", 2, 16);
448 RunRasterQueueConstructAndIterateTest("2_32", 2, 32);
449 RunRasterQueueConstructAndIterateTest("2_64", 2, 64);
450 RunRasterQueueConstructAndIterateTest("2_128", 2, 128);
451 RunRasterQueueConstructAndIterateTest("10_16", 10, 16);
452 RunRasterQueueConstructAndIterateTest("10_32", 10, 32);
453 RunRasterQueueConstructAndIterateTest("10_64", 10, 64);
454 RunRasterQueueConstructAndIterateTest("10_128", 10, 128);
455 RunRasterQueueConstructAndIterateTest("50_16", 50, 16);
456 RunRasterQueueConstructAndIterateTest("50_32", 50, 32);
457 RunRasterQueueConstructAndIterateTest("50_64", 50, 64);
458 RunRasterQueueConstructAndIterateTest("50_128", 50, 128);
461 TEST_F(TileManagerPerfTest
, EvictionTileQueueConstruct
) {
462 RunEvictionQueueConstructTest("2", 2);
463 RunEvictionQueueConstructTest("10", 10);
464 RunEvictionQueueConstructTest("50", 50);
467 TEST_F(TileManagerPerfTest
, EvictionTileQueueConstructAndIterate
) {
468 RunEvictionQueueConstructAndIterateTest("2_16", 2, 16);
469 RunEvictionQueueConstructAndIterateTest("2_32", 2, 32);
470 RunEvictionQueueConstructAndIterateTest("2_64", 2, 64);
471 RunEvictionQueueConstructAndIterateTest("2_128", 2, 128);
472 RunEvictionQueueConstructAndIterateTest("10_16", 10, 16);
473 RunEvictionQueueConstructAndIterateTest("10_32", 10, 32);
474 RunEvictionQueueConstructAndIterateTest("10_64", 10, 64);
475 RunEvictionQueueConstructAndIterateTest("10_128", 10, 128);
476 RunEvictionQueueConstructAndIterateTest("50_16", 50, 16);
477 RunEvictionQueueConstructAndIterateTest("50_32", 50, 32);
478 RunEvictionQueueConstructAndIterateTest("50_64", 50, 64);
479 RunEvictionQueueConstructAndIterateTest("50_128", 50, 128);