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/raster/raster_buffer.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/test_shared_bitmap_manager.h"
20 #include "cc/test/test_task_graph_runner.h"
21 #include "cc/test/test_tile_priorities.h"
22 #include "cc/tiles/tile.h"
23 #include "cc/tiles/tile_priority.h"
24 #include "cc/trees/layer_tree_impl.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 #include "testing/perf/perf_test.h"
32 static const int kTimeLimitMillis
= 2000;
33 static const int kWarmupRuns
= 5;
34 static const int kTimeCheckInterval
= 10;
36 class FakeTileTaskRunnerImpl
: public TileTaskRunner
, public TileTaskClient
{
38 // Overridden from TileTaskRunner:
39 void SetClient(TileTaskRunnerClient
* client
) override
{}
40 void Shutdown() override
{}
41 void ScheduleTasks(TileTaskQueue
* queue
) override
{
42 for (TileTaskQueue::Item::Vector::const_iterator it
= queue
->items
.begin();
43 it
!= queue
->items
.end(); ++it
) {
44 RasterTask
* task
= it
->task
;
47 task
->ScheduleOnOriginThread(this);
50 completed_tasks_
.push_back(task
);
53 void CheckForCompletedTasks() override
{
54 for (RasterTask::Vector::iterator it
= completed_tasks_
.begin();
55 it
!= completed_tasks_
.end();
57 RasterTask
* task
= it
->get();
60 task
->CompleteOnOriginThread(this);
63 completed_tasks_
.clear();
65 ResourceFormat
GetResourceFormat() const override
{ return RGBA_8888
; }
66 bool GetResourceRequiresSwizzle() const override
{
67 return !PlatformColor::SameComponentOrder(GetResourceFormat());
70 // Overridden from TileTaskClient:
71 scoped_ptr
<RasterBuffer
> AcquireBufferForRaster(
72 const Resource
* resource
,
73 uint64_t new_content_id
,
74 uint64_t previous_content_id
) 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::ThreadTaskRunnerHandle::Get()),
92 host_impl_(LayerTreeSettings(),
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
, 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().tile());
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().tile());
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
);
388 bool resourceless_software_draw
= false;
390 host_impl_
.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
391 for (const auto& layer
: layers
)
392 layer
->UpdateTiles(resourceless_software_draw
);
394 GlobalStateThatImpactsTilePriority
global_state(GlobalStateForTest());
395 tile_manager()->PrepareTiles(global_state
);
396 tile_manager()->Flush();
398 } while (!timer_
.HasTimeLimitExpired());
400 perf_test::PrintResult("prepare_tiles", "", test_name
,
401 timer_
.LapsPerSecond(), "runs/s", true);
404 TileManager
* tile_manager() { return host_impl_
.tile_manager(); }
407 GlobalStateThatImpactsTilePriority global_state_
;
409 TestSharedBitmapManager shared_bitmap_manager_
;
410 TestTaskGraphRunner task_graph_runner_
;
411 TileMemoryLimitPolicy memory_limit_policy_
;
414 FakeImplProxy proxy_
;
415 FakeLayerTreeHostImpl host_impl_
;
416 FakePictureLayerImpl
* pending_root_layer_
;
417 FakePictureLayerImpl
* active_root_layer_
;
419 LayerTreeSettings settings_
;
421 static const gfx::Size kDefaultTileSize
;
424 const gfx::Size
TileManagerPerfTest::kDefaultTileSize(100, 100);
426 TEST_F(TileManagerPerfTest
, PrepareTiles
) {
427 RunPrepareTilesTest("2_100", 2, 100);
428 RunPrepareTilesTest("2_500", 2, 500);
429 RunPrepareTilesTest("2_1000", 2, 1000);
430 RunPrepareTilesTest("10_100", 10, 100);
431 RunPrepareTilesTest("10_500", 10, 500);
432 RunPrepareTilesTest("10_1000", 10, 1000);
433 RunPrepareTilesTest("50_100", 100, 100);
434 RunPrepareTilesTest("50_500", 100, 500);
435 RunPrepareTilesTest("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);