Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / cc / tiles / tile_manager_perftest.cc
blob6a855669f0952140cc32e79ac2dfeaa703ea566f
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"
29 namespace cc {
30 namespace {
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 {
37 public:
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;
46 task->WillSchedule();
47 task->ScheduleOnOriginThread(this);
48 task->DidSchedule();
50 completed_tasks_.push_back(task);
53 void CheckForCompletedTasks() override {
54 for (RasterTask::Vector::iterator it = completed_tasks_.begin();
55 it != completed_tasks_.end();
56 ++it) {
57 RasterTask* task = it->get();
59 task->WillComplete();
60 task->CompleteOnOriginThread(this);
61 task->DidComplete();
63 task->RunReplyOnOriginThread();
65 completed_tasks_.clear();
67 ResourceFormat GetResourceFormat() const override { return RGBA_8888; }
68 bool GetResourceRequiresSwizzle() const override {
69 return !PlatformColor::SameComponentOrder(GetResourceFormat());
72 // Overridden from TileTaskClient:
73 scoped_ptr<RasterBuffer> AcquireBufferForRaster(
74 const Resource* resource,
75 uint64_t new_content_id,
76 uint64_t previous_content_id) override {
77 return nullptr;
79 void ReleaseBufferForRaster(scoped_ptr<RasterBuffer> buffer) override {}
81 private:
82 RasterTask::Vector completed_tasks_;
84 base::LazyInstance<FakeTileTaskRunnerImpl> g_fake_tile_task_runner =
85 LAZY_INSTANCE_INITIALIZER;
87 class TileManagerPerfTest : public testing::Test {
88 public:
89 TileManagerPerfTest()
90 : memory_limit_policy_(ALLOW_ANYTHING),
91 max_tiles_(10000),
92 id_(7),
93 proxy_(base::ThreadTaskRunnerHandle::Get()),
94 host_impl_(LayerTreeSettings(),
95 &proxy_,
96 &shared_bitmap_manager_,
97 &task_graph_runner_),
98 timer_(kWarmupRuns,
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, 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);
156 ActivateTree();
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,
177 int layer_count) {
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);
188 timer_.Reset();
189 do {
190 scoped_ptr<RasterTilePriorityQueue> queue(host_impl_.BuildRasterQueue(
191 priorities[priority_count], RasterTilePriorityQueue::Type::ALL));
192 priority_count = (priority_count + 1) % arraysize(priorities);
193 timer_.NextLap();
194 } while (!timer_.HasTimeLimitExpired());
196 perf_test::PrintResult("tile_manager_raster_tile_queue_construct",
198 test_name,
199 timer_.LapsPerSecond(),
200 "runs/s",
201 true);
204 void RunRasterQueueConstructAndIterateTest(const std::string& test_name,
205 int layer_count,
206 int tile_count) {
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;
217 timer_.Reset();
218 do {
219 int count = tile_count;
220 scoped_ptr<RasterTilePriorityQueue> queue(host_impl_.BuildRasterQueue(
221 priorities[priority_count], RasterTilePriorityQueue::Type::ALL));
222 while (count--) {
223 ASSERT_FALSE(queue->IsEmpty());
224 ASSERT_TRUE(queue->Top().tile());
225 queue->Pop();
227 priority_count = (priority_count + 1) % arraysize(priorities);
228 timer_.NextLap();
229 } while (!timer_.HasTimeLimitExpired());
231 perf_test::PrintResult(
232 "tile_manager_raster_tile_queue_construct_and_iterate",
234 test_name,
235 timer_.LapsPerSecond(),
236 "runs/s",
237 true);
240 void RunEvictionQueueConstructTest(const std::string& test_name,
241 int layer_count) {
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());
257 timer_.Reset();
258 do {
259 scoped_ptr<EvictionTilePriorityQueue> queue(
260 host_impl_.BuildEvictionQueue(priorities[priority_count]));
261 priority_count = (priority_count + 1) % arraysize(priorities);
262 timer_.NextLap();
263 } while (!timer_.HasTimeLimitExpired());
265 perf_test::PrintResult("tile_manager_eviction_tile_queue_construct",
267 test_name,
268 timer_.LapsPerSecond(),
269 "runs/s",
270 true);
273 void RunEvictionQueueConstructAndIterateTest(const std::string& test_name,
274 int layer_count,
275 int tile_count) {
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());
292 timer_.Reset();
293 do {
294 int count = tile_count;
295 scoped_ptr<EvictionTilePriorityQueue> queue(
296 host_impl_.BuildEvictionQueue(priorities[priority_count]));
297 while (count--) {
298 ASSERT_FALSE(queue->IsEmpty());
299 ASSERT_TRUE(queue->Top().tile());
300 queue->Pop();
302 priority_count = (priority_count + 1) % arraysize(priorities);
303 timer_.NextLap();
304 } while (!timer_.HasTimeLimitExpired());
306 perf_test::PrintResult(
307 "tile_manager_eviction_tile_queue_construct_and_iterate",
309 test_name,
310 timer_.LapsPerSecond(),
311 "runs/s",
312 true);
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());
359 ++next_id;
362 bool update_lcd_text = false;
363 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
364 for (FakePictureLayerImpl* layer : layers)
365 layer->CreateAllTiles();
367 return layers;
370 GlobalStateThatImpactsTilePriority GlobalStateForTest() {
371 GlobalStateThatImpactsTilePriority state;
372 gfx::Size tile_size = settings_.default_tile_size;
373 state.soft_memory_limit_in_bytes =
374 10000u * 4u *
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;
380 return state;
383 void RunPrepareTilesTest(const std::string& test_name,
384 int layer_count,
385 int approximate_tile_count_per_layer) {
386 std::vector<FakePictureLayerImpl*> layers =
387 CreateLayers(layer_count, approximate_tile_count_per_layer);
389 timer_.Reset();
390 bool resourceless_software_draw = false;
391 do {
392 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
393 for (const auto& layer : layers)
394 layer->UpdateTiles(resourceless_software_draw);
396 GlobalStateThatImpactsTilePriority global_state(GlobalStateForTest());
397 tile_manager()->PrepareTiles(global_state);
398 tile_manager()->Flush();
399 timer_.NextLap();
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(); }
408 protected:
409 GlobalStateThatImpactsTilePriority global_state_;
411 TestSharedBitmapManager shared_bitmap_manager_;
412 TestTaskGraphRunner task_graph_runner_;
413 TileMemoryLimitPolicy memory_limit_policy_;
414 int max_tiles_;
415 int id_;
416 FakeImplProxy proxy_;
417 FakeLayerTreeHostImpl host_impl_;
418 FakePictureLayerImpl* pending_root_layer_;
419 FakePictureLayerImpl* active_root_layer_;
420 LapTimer timer_;
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);
482 } // namespace
483 } // namespace cc