Roll src/third_party/WebKit 095aee4:2f230fd (svn 197498:197508)
[chromium-blink-merge.git] / cc / tiles / tile_manager_perftest.cc
blobd9b340d650ef7995b084923dd3c55993f3f7b0ec
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 LayerTreeSettingsWithLargeInterestArea : public LayerTreeSettings {
88 public:
89 LayerTreeSettingsWithLargeInterestArea() {
90 tiling_interest_area_viewport_multiplier = 10000;
94 class TileManagerPerfTest : public testing::Test {
95 public:
96 TileManagerPerfTest()
97 : memory_limit_policy_(ALLOW_ANYTHING),
98 max_tiles_(10000),
99 id_(7),
100 proxy_(base::ThreadTaskRunnerHandle::Get()),
101 host_impl_(LayerTreeSettingsWithLargeInterestArea(),
102 &proxy_,
103 &shared_bitmap_manager_,
104 &task_graph_runner_),
105 timer_(kWarmupRuns,
106 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
107 kTimeCheckInterval) {}
109 void SetTreePriority(TreePriority tree_priority) {
110 GlobalStateThatImpactsTilePriority state;
111 gfx::Size tile_size(256, 256);
113 state.soft_memory_limit_in_bytes = 100 * 1000 * 1000;
114 state.num_resources_limit = max_tiles_;
115 state.hard_memory_limit_in_bytes = state.soft_memory_limit_in_bytes * 2;
116 state.memory_limit_policy = memory_limit_policy_;
117 state.tree_priority = tree_priority;
119 global_state_ = state;
120 host_impl_.resource_pool()->SetResourceUsageLimits(
121 state.soft_memory_limit_in_bytes, 0, state.num_resources_limit);
122 host_impl_.tile_manager()->SetGlobalStateForTesting(state);
125 void SetUp() override {
126 InitializeRenderer();
127 SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
130 virtual void InitializeRenderer() {
131 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d().Pass());
132 tile_manager()->SetTileTaskRunnerForTesting(
133 g_fake_tile_task_runner.Pointer());
136 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
137 scoped_refptr<FakePicturePileImpl> pending_pile =
138 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize, layer_bounds);
139 scoped_refptr<FakePicturePileImpl> active_pile =
140 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize, layer_bounds);
142 SetupTrees(pending_pile, active_pile);
145 void ActivateTree() {
146 host_impl_.ActivateSyncTree();
147 CHECK(!host_impl_.pending_tree());
148 pending_root_layer_ = NULL;
149 active_root_layer_ = static_cast<FakePictureLayerImpl*>(
150 host_impl_.active_tree()->LayerById(id_));
153 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
154 const gfx::Size& tile_size) {
155 SetupDefaultTrees(layer_bounds);
156 pending_root_layer_->set_fixed_tile_size(tile_size);
157 active_root_layer_->set_fixed_tile_size(tile_size);
160 void SetupTrees(scoped_refptr<PicturePileImpl> pending_pile,
161 scoped_refptr<PicturePileImpl> active_pile) {
162 SetupPendingTree(active_pile);
163 ActivateTree();
164 SetupPendingTree(pending_pile);
167 void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
168 host_impl_.CreatePendingTree();
169 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
170 // Clear recycled tree.
171 pending_tree->DetachLayerTree();
173 scoped_ptr<FakePictureLayerImpl> pending_layer =
174 FakePictureLayerImpl::CreateWithRasterSource(pending_tree, id_, pile);
175 pending_layer->SetDrawsContent(true);
176 pending_layer->SetHasRenderSurface(true);
177 pending_tree->SetRootLayer(pending_layer.Pass());
179 pending_root_layer_ = static_cast<FakePictureLayerImpl*>(
180 host_impl_.pending_tree()->LayerById(id_));
183 void RunRasterQueueConstructTest(const std::string& test_name,
184 int layer_count) {
185 TreePriority priorities[] = {SAME_PRIORITY_FOR_BOTH_TREES,
186 SMOOTHNESS_TAKES_PRIORITY,
187 NEW_CONTENT_TAKES_PRIORITY};
188 int priority_count = 0;
190 std::vector<FakePictureLayerImpl*> layers = CreateLayers(layer_count, 10);
191 bool resourceless_software_draw = false;
192 for (const auto& layer : layers)
193 layer->UpdateTiles(resourceless_software_draw);
195 timer_.Reset();
196 do {
197 scoped_ptr<RasterTilePriorityQueue> queue(host_impl_.BuildRasterQueue(
198 priorities[priority_count], RasterTilePriorityQueue::Type::ALL));
199 priority_count = (priority_count + 1) % arraysize(priorities);
200 timer_.NextLap();
201 } while (!timer_.HasTimeLimitExpired());
203 perf_test::PrintResult("tile_manager_raster_tile_queue_construct",
205 test_name,
206 timer_.LapsPerSecond(),
207 "runs/s",
208 true);
211 void RunRasterQueueConstructAndIterateTest(const std::string& test_name,
212 int layer_count,
213 int tile_count) {
214 TreePriority priorities[] = {SAME_PRIORITY_FOR_BOTH_TREES,
215 SMOOTHNESS_TAKES_PRIORITY,
216 NEW_CONTENT_TAKES_PRIORITY};
218 std::vector<FakePictureLayerImpl*> layers = CreateLayers(layer_count, 100);
219 bool resourceless_software_draw = false;
220 for (const auto& layer : layers)
221 layer->UpdateTiles(resourceless_software_draw);
223 int priority_count = 0;
224 timer_.Reset();
225 do {
226 int count = tile_count;
227 scoped_ptr<RasterTilePriorityQueue> queue(host_impl_.BuildRasterQueue(
228 priorities[priority_count], RasterTilePriorityQueue::Type::ALL));
229 while (count--) {
230 ASSERT_FALSE(queue->IsEmpty());
231 ASSERT_TRUE(queue->Top().tile());
232 queue->Pop();
234 priority_count = (priority_count + 1) % arraysize(priorities);
235 timer_.NextLap();
236 } while (!timer_.HasTimeLimitExpired());
238 perf_test::PrintResult(
239 "tile_manager_raster_tile_queue_construct_and_iterate",
241 test_name,
242 timer_.LapsPerSecond(),
243 "runs/s",
244 true);
247 void RunEvictionQueueConstructTest(const std::string& test_name,
248 int layer_count) {
249 TreePriority priorities[] = {SAME_PRIORITY_FOR_BOTH_TREES,
250 SMOOTHNESS_TAKES_PRIORITY,
251 NEW_CONTENT_TAKES_PRIORITY};
252 int priority_count = 0;
254 std::vector<FakePictureLayerImpl*> layers = CreateLayers(layer_count, 10);
255 bool resourceless_software_draw = false;
256 for (const auto& layer : layers) {
257 layer->UpdateTiles(resourceless_software_draw);
258 for (size_t i = 0; i < layer->num_tilings(); ++i) {
259 tile_manager()->InitializeTilesWithResourcesForTesting(
260 layer->tilings()->tiling_at(i)->AllTilesForTesting());
264 timer_.Reset();
265 do {
266 scoped_ptr<EvictionTilePriorityQueue> queue(
267 host_impl_.BuildEvictionQueue(priorities[priority_count]));
268 priority_count = (priority_count + 1) % arraysize(priorities);
269 timer_.NextLap();
270 } while (!timer_.HasTimeLimitExpired());
272 perf_test::PrintResult("tile_manager_eviction_tile_queue_construct",
274 test_name,
275 timer_.LapsPerSecond(),
276 "runs/s",
277 true);
280 void RunEvictionQueueConstructAndIterateTest(const std::string& test_name,
281 int layer_count,
282 int 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<FakePictureLayerImpl*> layers =
289 CreateLayers(layer_count, tile_count);
290 bool resourceless_software_draw = false;
291 for (const auto& layer : layers) {
292 layer->UpdateTiles(resourceless_software_draw);
293 for (size_t i = 0; i < layer->num_tilings(); ++i) {
294 tile_manager()->InitializeTilesWithResourcesForTesting(
295 layer->tilings()->tiling_at(i)->AllTilesForTesting());
299 timer_.Reset();
300 do {
301 int count = tile_count;
302 scoped_ptr<EvictionTilePriorityQueue> queue(
303 host_impl_.BuildEvictionQueue(priorities[priority_count]));
304 while (count--) {
305 ASSERT_FALSE(queue->IsEmpty());
306 ASSERT_TRUE(queue->Top().tile());
307 queue->Pop();
309 priority_count = (priority_count + 1) % arraysize(priorities);
310 timer_.NextLap();
311 } while (!timer_.HasTimeLimitExpired());
313 perf_test::PrintResult(
314 "tile_manager_eviction_tile_queue_construct_and_iterate",
316 test_name,
317 timer_.LapsPerSecond(),
318 "runs/s",
319 true);
322 std::vector<FakePictureLayerImpl*> CreateLayers(int layer_count,
323 int tiles_per_layer_count) {
324 // Compute the width/height required for high res to get
325 // tiles_per_layer_count tiles.
326 float width = std::sqrt(static_cast<float>(tiles_per_layer_count));
327 float height = tiles_per_layer_count / width;
329 // Adjust the width and height to account for the fact that tiles
330 // are bigger than 1x1. Also, account for the fact that that we
331 // will be creating one high res and one low res tiling. That is,
332 // width and height should be smaller by sqrt(1 + low_res_scale).
333 // This gives us _approximately_ correct counts.
334 width *= settings_.default_tile_size.width() /
335 std::sqrt(1 + settings_.low_res_contents_scale_factor);
336 height *= settings_.default_tile_size.height() /
337 std::sqrt(1 + settings_.low_res_contents_scale_factor);
339 // Ensure that we start with blank trees and no tiles.
340 host_impl_.ResetTreesForTesting();
341 tile_manager()->FreeResourcesAndCleanUpReleasedTilesForTesting();
343 gfx::Size layer_bounds(width, height);
344 gfx::Size viewport(width / 5, height / 5);
345 host_impl_.SetViewportSize(viewport);
346 SetupDefaultTreesWithFixedTileSize(layer_bounds,
347 settings_.default_tile_size);
349 std::vector<FakePictureLayerImpl*> layers;
351 // Pending layer counts as one layer.
352 layers.push_back(pending_root_layer_);
353 int next_id = id_ + 1;
355 // Create the rest of the layers as children of the root layer.
356 scoped_refptr<FakePicturePileImpl> pile =
357 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize, layer_bounds);
358 while (static_cast<int>(layers.size()) < layer_count) {
359 scoped_ptr<FakePictureLayerImpl> layer =
360 FakePictureLayerImpl::CreateWithRasterSource(
361 host_impl_.pending_tree(), next_id, pile);
362 layer->SetBounds(layer_bounds);
363 layer->SetDrawsContent(true);
364 layers.push_back(layer.get());
365 pending_root_layer_->AddChild(layer.Pass());
366 ++next_id;
369 bool update_lcd_text = false;
370 host_impl_.pending_tree()->UpdateDrawProperties(update_lcd_text);
371 for (FakePictureLayerImpl* layer : layers)
372 layer->CreateAllTiles();
374 return layers;
377 GlobalStateThatImpactsTilePriority GlobalStateForTest() {
378 GlobalStateThatImpactsTilePriority state;
379 gfx::Size tile_size = settings_.default_tile_size;
380 state.soft_memory_limit_in_bytes =
381 10000u * 4u *
382 static_cast<size_t>(tile_size.width() * tile_size.height());
383 state.hard_memory_limit_in_bytes = state.soft_memory_limit_in_bytes;
384 state.num_resources_limit = 10000;
385 state.memory_limit_policy = ALLOW_ANYTHING;
386 state.tree_priority = SMOOTHNESS_TAKES_PRIORITY;
387 return state;
390 void RunPrepareTilesTest(const std::string& test_name,
391 int layer_count,
392 int approximate_tile_count_per_layer) {
393 std::vector<FakePictureLayerImpl*> layers =
394 CreateLayers(layer_count, approximate_tile_count_per_layer);
396 timer_.Reset();
397 bool resourceless_software_draw = false;
398 do {
399 host_impl_.AdvanceToNextFrame(base::TimeDelta::FromMilliseconds(1));
400 for (const auto& layer : layers)
401 layer->UpdateTiles(resourceless_software_draw);
403 GlobalStateThatImpactsTilePriority global_state(GlobalStateForTest());
404 tile_manager()->PrepareTiles(global_state);
405 tile_manager()->UpdateVisibleTiles(global_state);
406 timer_.NextLap();
407 } while (!timer_.HasTimeLimitExpired());
409 perf_test::PrintResult("prepare_tiles", "", test_name,
410 timer_.LapsPerSecond(), "runs/s", true);
413 TileManager* tile_manager() { return host_impl_.tile_manager(); }
415 protected:
416 GlobalStateThatImpactsTilePriority global_state_;
418 TestSharedBitmapManager shared_bitmap_manager_;
419 TestTaskGraphRunner task_graph_runner_;
420 TileMemoryLimitPolicy memory_limit_policy_;
421 int max_tiles_;
422 int id_;
423 FakeImplProxy proxy_;
424 FakeLayerTreeHostImpl host_impl_;
425 FakePictureLayerImpl* pending_root_layer_;
426 FakePictureLayerImpl* active_root_layer_;
427 LapTimer timer_;
428 LayerTreeSettings settings_;
430 static const gfx::Size kDefaultTileSize;
433 const gfx::Size TileManagerPerfTest::kDefaultTileSize(100, 100);
435 TEST_F(TileManagerPerfTest, PrepareTiles) {
436 RunPrepareTilesTest("2_100", 2, 100);
437 RunPrepareTilesTest("2_500", 2, 500);
438 RunPrepareTilesTest("2_1000", 2, 1000);
439 RunPrepareTilesTest("10_100", 10, 100);
440 RunPrepareTilesTest("10_500", 10, 500);
441 RunPrepareTilesTest("10_1000", 10, 1000);
442 RunPrepareTilesTest("50_100", 100, 100);
443 RunPrepareTilesTest("50_500", 100, 500);
444 RunPrepareTilesTest("50_1000", 100, 1000);
447 TEST_F(TileManagerPerfTest, RasterTileQueueConstruct) {
448 RunRasterQueueConstructTest("2", 2);
449 RunRasterQueueConstructTest("10", 10);
450 RunRasterQueueConstructTest("50", 50);
453 TEST_F(TileManagerPerfTest, RasterTileQueueConstructAndIterate) {
454 RunRasterQueueConstructAndIterateTest("2_16", 2, 16);
455 RunRasterQueueConstructAndIterateTest("2_32", 2, 32);
456 RunRasterQueueConstructAndIterateTest("2_64", 2, 64);
457 RunRasterQueueConstructAndIterateTest("2_128", 2, 128);
458 RunRasterQueueConstructAndIterateTest("10_16", 10, 16);
459 RunRasterQueueConstructAndIterateTest("10_32", 10, 32);
460 RunRasterQueueConstructAndIterateTest("10_64", 10, 64);
461 RunRasterQueueConstructAndIterateTest("10_128", 10, 128);
462 RunRasterQueueConstructAndIterateTest("50_16", 50, 16);
463 RunRasterQueueConstructAndIterateTest("50_32", 50, 32);
464 RunRasterQueueConstructAndIterateTest("50_64", 50, 64);
465 RunRasterQueueConstructAndIterateTest("50_128", 50, 128);
468 TEST_F(TileManagerPerfTest, EvictionTileQueueConstruct) {
469 RunEvictionQueueConstructTest("2", 2);
470 RunEvictionQueueConstructTest("10", 10);
471 RunEvictionQueueConstructTest("50", 50);
474 TEST_F(TileManagerPerfTest, EvictionTileQueueConstructAndIterate) {
475 RunEvictionQueueConstructAndIterateTest("2_16", 2, 16);
476 RunEvictionQueueConstructAndIterateTest("2_32", 2, 32);
477 RunEvictionQueueConstructAndIterateTest("2_64", 2, 64);
478 RunEvictionQueueConstructAndIterateTest("2_128", 2, 128);
479 RunEvictionQueueConstructAndIterateTest("10_16", 10, 16);
480 RunEvictionQueueConstructAndIterateTest("10_32", 10, 32);
481 RunEvictionQueueConstructAndIterateTest("10_64", 10, 64);
482 RunEvictionQueueConstructAndIterateTest("10_128", 10, 128);
483 RunEvictionQueueConstructAndIterateTest("50_16", 50, 16);
484 RunEvictionQueueConstructAndIterateTest("50_32", 50, 32);
485 RunEvictionQueueConstructAndIterateTest("50_64", 50, 64);
486 RunEvictionQueueConstructAndIterateTest("50_128", 50, 128);
489 } // namespace
490 } // namespace cc