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/location.h"
6 #include "base/thread_task_runner_handle.h"
7 #include "base/time/time.h"
8 #include "cc/debug/lap_timer.h"
9 #include "cc/resources/raster_buffer.h"
10 #include "cc/resources/tile.h"
11 #include "cc/resources/tile_priority.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_pile_impl.h"
19 #include "cc/test/fake_tile_manager.h"
20 #include "cc/test/fake_tile_manager_client.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"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "testing/perf/perf_test.h"
30 #include "ui/gfx/frame_time.h"
35 static const int kTimeLimitMillis
= 2000;
36 static const int kWarmupRuns
= 5;
37 static const int kTimeCheckInterval
= 10;
39 class FakeTileTaskRunnerImpl
: public TileTaskRunner
, public TileTaskClient
{
41 // Overridden from TileTaskRunner:
42 void SetClient(TileTaskRunnerClient
* client
) override
{}
43 void Shutdown() override
{}
44 void ScheduleTasks(TileTaskQueue
* queue
) override
{
45 for (TileTaskQueue::Item::Vector::const_iterator it
= queue
->items
.begin();
46 it
!= queue
->items
.end(); ++it
) {
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();
70 ResourceFormat
GetResourceFormat() override
{
74 // Overridden from TileTaskClient:
75 scoped_ptr
<RasterBuffer
> AcquireBufferForRaster(
76 const Resource
* resource
) override
{
79 void ReleaseBufferForRaster(scoped_ptr
<RasterBuffer
> buffer
) override
{}
82 RasterTask::Vector completed_tasks_
;
84 base::LazyInstance
<FakeTileTaskRunnerImpl
> g_fake_tile_task_runner
=
85 LAZY_INSTANCE_INITIALIZER
;
87 class TileManagerPerfTest
: public testing::Test
{
90 : memory_limit_policy_(ALLOW_ANYTHING
),
93 proxy_(base::ThreadTaskRunnerHandle::Get()),
94 host_impl_(ImplSidePaintingSettings(10000),
96 &shared_bitmap_manager_
,
99 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
100 kTimeCheckInterval
) {}
102 void SetTreePriority(TreePriority tree_priority
) {
103 GlobalStateThatImpactsTilePriority state
;
104 gfx::Size
tile_size(256, 256);
106 state
.soft_memory_limit_in_bytes
= 100 * 1000 * 1000;
107 state
.num_resources_limit
= max_tiles_
;
108 state
.hard_memory_limit_in_bytes
= state
.soft_memory_limit_in_bytes
* 2;
109 state
.memory_limit_policy
= memory_limit_policy_
;
110 state
.tree_priority
= tree_priority
;
112 global_state_
= state
;
113 host_impl_
.resource_pool()->SetResourceUsageLimits(
114 state
.soft_memory_limit_in_bytes
, 0, state
.num_resources_limit
);
115 host_impl_
.tile_manager()->SetGlobalStateForTesting(state
);
118 void SetUp() override
{
119 InitializeRenderer();
120 SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES
);
123 virtual void InitializeRenderer() {
124 host_impl_
.InitializeRenderer(FakeOutputSurface::Create3d().Pass());
125 tile_manager()->SetTileTaskRunnerForTesting(
126 g_fake_tile_task_runner
.Pointer());
129 void SetupDefaultTrees(const gfx::Size
& layer_bounds
) {
130 scoped_refptr
<FakePicturePileImpl
> pending_pile
=
131 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize
, layer_bounds
);
132 scoped_refptr
<FakePicturePileImpl
> active_pile
=
133 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize
, layer_bounds
);
135 SetupTrees(pending_pile
, active_pile
);
138 void ActivateTree() {
139 host_impl_
.ActivateSyncTree();
140 CHECK(!host_impl_
.pending_tree());
141 pending_root_layer_
= NULL
;
142 active_root_layer_
= static_cast<FakePictureLayerImpl
*>(
143 host_impl_
.active_tree()->LayerById(id_
));
146 void SetupDefaultTreesWithFixedTileSize(const gfx::Size
& layer_bounds
,
147 const gfx::Size
& tile_size
) {
148 SetupDefaultTrees(layer_bounds
);
149 pending_root_layer_
->set_fixed_tile_size(tile_size
);
150 active_root_layer_
->set_fixed_tile_size(tile_size
);
153 void SetupTrees(scoped_refptr
<PicturePileImpl
> pending_pile
,
154 scoped_refptr
<PicturePileImpl
> active_pile
) {
155 SetupPendingTree(active_pile
);
157 SetupPendingTree(pending_pile
);
160 void SetupPendingTree(scoped_refptr
<PicturePileImpl
> pile
) {
161 host_impl_
.CreatePendingTree();
162 LayerTreeImpl
* pending_tree
= host_impl_
.pending_tree();
163 // Clear recycled tree.
164 pending_tree
->DetachLayerTree();
166 scoped_ptr
<FakePictureLayerImpl
> pending_layer
=
167 FakePictureLayerImpl::CreateWithRasterSource(pending_tree
, id_
, pile
);
168 pending_layer
->SetDrawsContent(true);
169 pending_layer
->SetHasRenderSurface(true);
170 pending_tree
->SetRootLayer(pending_layer
.Pass());
172 pending_root_layer_
= static_cast<FakePictureLayerImpl
*>(
173 host_impl_
.pending_tree()->LayerById(id_
));
176 void RunRasterQueueConstructTest(const std::string
& test_name
,
178 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
179 SMOOTHNESS_TAKES_PRIORITY
,
180 NEW_CONTENT_TAKES_PRIORITY
};
181 int priority_count
= 0;
183 std::vector
<FakePictureLayerImpl
*> layers
= CreateLayers(layer_count
, 10);
184 bool resourceless_software_draw
= false;
185 for (const auto& layer
: layers
)
186 layer
->UpdateTiles(resourceless_software_draw
);
190 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
191 priorities
[priority_count
], RasterTilePriorityQueue::Type::ALL
));
192 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
194 } while (!timer_
.HasTimeLimitExpired());
196 perf_test::PrintResult("tile_manager_raster_tile_queue_construct",
199 timer_
.LapsPerSecond(),
204 void RunRasterQueueConstructAndIterateTest(const std::string
& test_name
,
207 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
208 SMOOTHNESS_TAKES_PRIORITY
,
209 NEW_CONTENT_TAKES_PRIORITY
};
211 std::vector
<FakePictureLayerImpl
*> layers
= CreateLayers(layer_count
, 100);
212 bool resourceless_software_draw
= false;
213 for (const auto& layer
: layers
)
214 layer
->UpdateTiles(resourceless_software_draw
);
216 int priority_count
= 0;
219 int count
= tile_count
;
220 scoped_ptr
<RasterTilePriorityQueue
> queue(host_impl_
.BuildRasterQueue(
221 priorities
[priority_count
], RasterTilePriorityQueue::Type::ALL
));
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
<FakePictureLayerImpl
*> layers
= CreateLayers(layer_count
, 10);
248 bool resourceless_software_draw
= false;
249 for (const auto& layer
: layers
) {
250 layer
->UpdateTiles(resourceless_software_draw
);
251 for (size_t i
= 0; i
< layer
->num_tilings(); ++i
) {
252 tile_manager()->InitializeTilesWithResourcesForTesting(
253 layer
->tilings()->tiling_at(i
)->AllTilesForTesting());
259 scoped_ptr
<EvictionTilePriorityQueue
> queue(
260 host_impl_
.BuildEvictionQueue(priorities
[priority_count
]));
261 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
263 } while (!timer_
.HasTimeLimitExpired());
265 perf_test::PrintResult("tile_manager_eviction_tile_queue_construct",
268 timer_
.LapsPerSecond(),
273 void RunEvictionQueueConstructAndIterateTest(const std::string
& test_name
,
276 TreePriority priorities
[] = {SAME_PRIORITY_FOR_BOTH_TREES
,
277 SMOOTHNESS_TAKES_PRIORITY
,
278 NEW_CONTENT_TAKES_PRIORITY
};
279 int priority_count
= 0;
281 std::vector
<FakePictureLayerImpl
*> layers
=
282 CreateLayers(layer_count
, tile_count
);
283 bool resourceless_software_draw
= false;
284 for (const auto& layer
: layers
) {
285 layer
->UpdateTiles(resourceless_software_draw
);
286 for (size_t i
= 0; i
< layer
->num_tilings(); ++i
) {
287 tile_manager()->InitializeTilesWithResourcesForTesting(
288 layer
->tilings()->tiling_at(i
)->AllTilesForTesting());
294 int count
= tile_count
;
295 scoped_ptr
<EvictionTilePriorityQueue
> queue(
296 host_impl_
.BuildEvictionQueue(priorities
[priority_count
]));
298 ASSERT_FALSE(queue
->IsEmpty());
299 ASSERT_TRUE(queue
->Top() != NULL
);
302 priority_count
= (priority_count
+ 1) % arraysize(priorities
);
304 } while (!timer_
.HasTimeLimitExpired());
306 perf_test::PrintResult(
307 "tile_manager_eviction_tile_queue_construct_and_iterate",
310 timer_
.LapsPerSecond(),
315 std::vector
<FakePictureLayerImpl
*> CreateLayers(int layer_count
,
316 int tiles_per_layer_count
) {
317 // Compute the width/height required for high res to get
318 // tiles_per_layer_count tiles.
319 float width
= std::sqrt(static_cast<float>(tiles_per_layer_count
));
320 float height
= tiles_per_layer_count
/ width
;
322 // Adjust the width and height to account for the fact that tiles
323 // are bigger than 1x1. Also, account for the fact that that we
324 // will be creating one high res and one low res tiling. That is,
325 // width and height should be smaller by sqrt(1 + low_res_scale).
326 // This gives us _approximately_ correct counts.
327 width
*= settings_
.default_tile_size
.width() /
328 std::sqrt(1 + settings_
.low_res_contents_scale_factor
);
329 height
*= settings_
.default_tile_size
.height() /
330 std::sqrt(1 + settings_
.low_res_contents_scale_factor
);
332 // Ensure that we start with blank trees and no tiles.
333 host_impl_
.ResetTreesForTesting();
334 tile_manager()->FreeResourcesAndCleanUpReleasedTilesForTesting();
336 gfx::Size
layer_bounds(width
, height
);
337 gfx::Size
viewport(width
/ 5, height
/ 5);
338 host_impl_
.SetViewportSize(viewport
);
339 SetupDefaultTreesWithFixedTileSize(layer_bounds
,
340 settings_
.default_tile_size
);
342 std::vector
<FakePictureLayerImpl
*> layers
;
344 // Pending layer counts as one layer.
345 layers
.push_back(pending_root_layer_
);
346 int next_id
= id_
+ 1;
348 // Create the rest of the layers as children of the root layer.
349 scoped_refptr
<FakePicturePileImpl
> pile
=
350 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize
, layer_bounds
);
351 while (static_cast<int>(layers
.size()) < layer_count
) {
352 scoped_ptr
<FakePictureLayerImpl
> layer
=
353 FakePictureLayerImpl::CreateWithRasterSource(
354 host_impl_
.pending_tree(), next_id
, pile
);
355 layer
->SetBounds(layer_bounds
);
356 layer
->SetDrawsContent(true);
357 layers
.push_back(layer
.get());
358 pending_root_layer_
->AddChild(layer
.Pass());
362 bool update_lcd_text
= false;
363 host_impl_
.pending_tree()->UpdateDrawProperties(update_lcd_text
);
364 for (FakePictureLayerImpl
* layer
: layers
)
365 layer
->CreateAllTiles();
370 GlobalStateThatImpactsTilePriority
GlobalStateForTest() {
371 GlobalStateThatImpactsTilePriority state
;
372 gfx::Size tile_size
= settings_
.default_tile_size
;
373 state
.soft_memory_limit_in_bytes
=
375 static_cast<size_t>(tile_size
.width() * tile_size
.height());
376 state
.hard_memory_limit_in_bytes
= state
.soft_memory_limit_in_bytes
;
377 state
.num_resources_limit
= 10000;
378 state
.memory_limit_policy
= ALLOW_ANYTHING
;
379 state
.tree_priority
= SMOOTHNESS_TAKES_PRIORITY
;
383 void RunPrepareTilesTest(const std::string
& test_name
,
385 int approximate_tile_count_per_layer
) {
386 std::vector
<FakePictureLayerImpl
*> layers
=
387 CreateLayers(layer_count
, approximate_tile_count_per_layer
);
389 bool resourceless_software_draw
= false;
391 BeginFrameArgs args
=
392 CreateBeginFrameArgsForTesting(BEGINFRAME_FROM_HERE
);
393 host_impl_
.WillBeginImplFrame(args
);
394 for (const auto& layer
: layers
)
395 layer
->UpdateTiles(resourceless_software_draw
);
397 GlobalStateThatImpactsTilePriority
global_state(GlobalStateForTest());
398 tile_manager()->PrepareTiles(global_state
);
399 tile_manager()->UpdateVisibleTiles(global_state
);
401 host_impl_
.DidFinishImplFrame();
402 } while (!timer_
.HasTimeLimitExpired());
404 perf_test::PrintResult("prepare_tiles", "", test_name
,
405 timer_
.LapsPerSecond(), "runs/s", true);
408 TileManager
* tile_manager() { return host_impl_
.tile_manager(); }
411 GlobalStateThatImpactsTilePriority global_state_
;
413 TestSharedBitmapManager shared_bitmap_manager_
;
414 TestTaskGraphRunner task_graph_runner_
;
415 TileMemoryLimitPolicy memory_limit_policy_
;
418 FakeImplProxy proxy_
;
419 FakeLayerTreeHostImpl host_impl_
;
420 FakePictureLayerImpl
* pending_root_layer_
;
421 FakePictureLayerImpl
* active_root_layer_
;
423 LayerTreeSettings settings_
;
425 static const gfx::Size kDefaultTileSize
;
428 const gfx::Size
TileManagerPerfTest::kDefaultTileSize(100, 100);
430 TEST_F(TileManagerPerfTest
, PrepareTiles
) {
431 RunPrepareTilesTest("2_100", 2, 100);
432 RunPrepareTilesTest("2_500", 2, 500);
433 RunPrepareTilesTest("2_1000", 2, 1000);
434 RunPrepareTilesTest("10_100", 10, 100);
435 RunPrepareTilesTest("10_500", 10, 500);
436 RunPrepareTilesTest("10_1000", 10, 1000);
437 RunPrepareTilesTest("50_100", 100, 100);
438 RunPrepareTilesTest("50_500", 100, 500);
439 RunPrepareTilesTest("50_1000", 100, 1000);
442 TEST_F(TileManagerPerfTest
, RasterTileQueueConstruct
) {
443 RunRasterQueueConstructTest("2", 2);
444 RunRasterQueueConstructTest("10", 10);
445 RunRasterQueueConstructTest("50", 50);
448 TEST_F(TileManagerPerfTest
, RasterTileQueueConstructAndIterate
) {
449 RunRasterQueueConstructAndIterateTest("2_16", 2, 16);
450 RunRasterQueueConstructAndIterateTest("2_32", 2, 32);
451 RunRasterQueueConstructAndIterateTest("2_64", 2, 64);
452 RunRasterQueueConstructAndIterateTest("2_128", 2, 128);
453 RunRasterQueueConstructAndIterateTest("10_16", 10, 16);
454 RunRasterQueueConstructAndIterateTest("10_32", 10, 32);
455 RunRasterQueueConstructAndIterateTest("10_64", 10, 64);
456 RunRasterQueueConstructAndIterateTest("10_128", 10, 128);
457 RunRasterQueueConstructAndIterateTest("50_16", 50, 16);
458 RunRasterQueueConstructAndIterateTest("50_32", 50, 32);
459 RunRasterQueueConstructAndIterateTest("50_64", 50, 64);
460 RunRasterQueueConstructAndIterateTest("50_128", 50, 128);
463 TEST_F(TileManagerPerfTest
, EvictionTileQueueConstruct
) {
464 RunEvictionQueueConstructTest("2", 2);
465 RunEvictionQueueConstructTest("10", 10);
466 RunEvictionQueueConstructTest("50", 50);
469 TEST_F(TileManagerPerfTest
, EvictionTileQueueConstructAndIterate
) {
470 RunEvictionQueueConstructAndIterateTest("2_16", 2, 16);
471 RunEvictionQueueConstructAndIterateTest("2_32", 2, 32);
472 RunEvictionQueueConstructAndIterateTest("2_64", 2, 64);
473 RunEvictionQueueConstructAndIterateTest("2_128", 2, 128);
474 RunEvictionQueueConstructAndIterateTest("10_16", 10, 16);
475 RunEvictionQueueConstructAndIterateTest("10_32", 10, 32);
476 RunEvictionQueueConstructAndIterateTest("10_64", 10, 64);
477 RunEvictionQueueConstructAndIterateTest("10_128", 10, 128);
478 RunEvictionQueueConstructAndIterateTest("50_16", 50, 16);
479 RunEvictionQueueConstructAndIterateTest("50_32", 50, 32);
480 RunEvictionQueueConstructAndIterateTest("50_64", 50, 64);
481 RunEvictionQueueConstructAndIterateTest("50_128", 50, 128);