Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / cc / resources / tile_manager_perftest.cc
blob7b096bd261b602e409a7e1deddf775960066bcd4
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_tile_priorities.h"
22 #include "cc/trees/layer_tree_impl.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "testing/perf/perf_test.h"
27 #include "ui/gfx/frame_time.h"
29 namespace cc {
31 namespace {
33 static const int kTimeLimitMillis = 2000;
34 static const int kWarmupRuns = 5;
35 static const int kTimeCheckInterval = 10;
37 class FakeRasterizerImpl : public Rasterizer, public RasterizerTaskClient {
38 public:
39 // Overridden from Rasterizer:
40 void SetClient(RasterizerClient* client) override {}
41 void Shutdown() override {}
42 void ScheduleTasks(RasterTaskQueue* queue) override {
43 for (RasterTaskQueue::Item::Vector::const_iterator it =
44 queue->items.begin();
45 it != queue->items.end();
46 ++it) {
47 RasterTask* task = it->task;
49 task->WillSchedule();
50 task->ScheduleOnOriginThread(this);
51 task->DidSchedule();
53 completed_tasks_.push_back(task);
56 void CheckForCompletedTasks() override {
57 for (RasterTask::Vector::iterator it = completed_tasks_.begin();
58 it != completed_tasks_.end();
59 ++it) {
60 RasterTask* task = it->get();
62 task->WillComplete();
63 task->CompleteOnOriginThread(this);
64 task->DidComplete();
66 task->RunReplyOnOriginThread();
68 completed_tasks_.clear();
71 // Overridden from RasterizerTaskClient:
72 scoped_ptr<RasterBuffer> AcquireBufferForRaster(
73 const Resource* resource) override {
74 return nullptr;
76 void ReleaseBufferForRaster(scoped_ptr<RasterBuffer> buffer) override {}
78 private:
79 RasterTask::Vector completed_tasks_;
81 base::LazyInstance<FakeRasterizerImpl> g_fake_rasterizer =
82 LAZY_INSTANCE_INITIALIZER;
84 class TileManagerPerfTest : public testing::Test {
85 public:
86 TileManagerPerfTest()
87 : memory_limit_policy_(ALLOW_ANYTHING),
88 max_tiles_(10000),
89 id_(7),
90 proxy_(base::MessageLoopProxy::current()),
91 host_impl_(ImplSidePaintingSettings(10000),
92 &proxy_,
93 &shared_bitmap_manager_),
94 timer_(kWarmupRuns,
95 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
96 kTimeCheckInterval) {}
98 void SetTreePriority(TreePriority tree_priority) {
99 GlobalStateThatImpactsTilePriority state;
100 gfx::Size tile_size(256, 256);
102 state.soft_memory_limit_in_bytes = 100 * 1000 * 1000;
103 state.num_resources_limit = max_tiles_;
104 state.hard_memory_limit_in_bytes = state.soft_memory_limit_in_bytes * 2;
105 state.memory_limit_policy = memory_limit_policy_;
106 state.tree_priority = tree_priority;
108 global_state_ = state;
109 host_impl_.resource_pool()->SetResourceUsageLimits(
110 state.soft_memory_limit_in_bytes, 0, state.num_resources_limit);
111 host_impl_.tile_manager()->SetGlobalStateForTesting(state);
114 void SetUp() override {
115 InitializeRenderer();
116 SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
119 virtual void InitializeRenderer() {
120 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d().Pass());
121 tile_manager()->SetRasterizerForTesting(g_fake_rasterizer.Pointer());
124 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
125 scoped_refptr<FakePicturePileImpl> pending_pile =
126 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize, layer_bounds);
127 scoped_refptr<FakePicturePileImpl> active_pile =
128 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize, layer_bounds);
130 SetupTrees(pending_pile, active_pile);
133 void ActivateTree() {
134 host_impl_.ActivateSyncTree();
135 CHECK(!host_impl_.pending_tree());
136 pending_root_layer_ = NULL;
137 active_root_layer_ = static_cast<FakePictureLayerImpl*>(
138 host_impl_.active_tree()->LayerById(id_));
141 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
142 const gfx::Size& tile_size) {
143 SetupDefaultTrees(layer_bounds);
144 pending_root_layer_->set_fixed_tile_size(tile_size);
145 active_root_layer_->set_fixed_tile_size(tile_size);
148 void SetupTrees(scoped_refptr<PicturePileImpl> pending_pile,
149 scoped_refptr<PicturePileImpl> active_pile) {
150 SetupPendingTree(active_pile);
151 ActivateTree();
152 SetupPendingTree(pending_pile);
155 void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
156 host_impl_.CreatePendingTree();
157 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
158 // Clear recycled tree.
159 pending_tree->DetachLayerTree();
161 scoped_ptr<FakePictureLayerImpl> pending_layer =
162 FakePictureLayerImpl::CreateWithRasterSource(pending_tree, id_, pile);
163 pending_layer->SetDrawsContent(true);
164 pending_tree->SetRootLayer(pending_layer.Pass());
166 pending_root_layer_ = static_cast<FakePictureLayerImpl*>(
167 host_impl_.pending_tree()->LayerById(id_));
168 pending_root_layer_->DoPostCommitInitializationIfNeeded();
171 void CreateHighLowResAndSetAllTilesVisible() {
172 // Active layer must get updated first so pending layer can share from it.
173 active_root_layer_->CreateDefaultTilingsAndTiles();
174 active_root_layer_->SetAllTilesVisible();
175 pending_root_layer_->CreateDefaultTilingsAndTiles();
176 pending_root_layer_->SetAllTilesVisible();
179 void RunRasterQueueConstructTest(const std::string& test_name,
180 int layer_count) {
181 TreePriority priorities[] = {SAME_PRIORITY_FOR_BOTH_TREES,
182 SMOOTHNESS_TAKES_PRIORITY,
183 NEW_CONTENT_TAKES_PRIORITY};
184 int priority_count = 0;
186 std::vector<LayerImpl*> layers = CreateLayers(layer_count, 10);
187 bool resourceless_software_draw = false;
188 for (unsigned i = 0; i < layers.size(); ++i) {
189 layers[i]->UpdateTiles(Occlusion(), resourceless_software_draw);
192 timer_.Reset();
193 do {
194 RasterTilePriorityQueue queue;
195 host_impl_.BuildRasterQueue(&queue, priorities[priority_count]);
196 priority_count = (priority_count + 1) % arraysize(priorities);
197 timer_.NextLap();
198 } while (!timer_.HasTimeLimitExpired());
200 perf_test::PrintResult("tile_manager_raster_tile_queue_construct",
202 test_name,
203 timer_.LapsPerSecond(),
204 "runs/s",
205 true);
208 void RunRasterQueueConstructAndIterateTest(const std::string& test_name,
209 int layer_count,
210 unsigned tile_count) {
211 TreePriority priorities[] = {SAME_PRIORITY_FOR_BOTH_TREES,
212 SMOOTHNESS_TAKES_PRIORITY,
213 NEW_CONTENT_TAKES_PRIORITY};
215 std::vector<LayerImpl*> layers = CreateLayers(layer_count, 100);
216 bool resourceless_software_draw = false;
217 for (unsigned i = 0; i < layers.size(); ++i) {
218 layers[i]->UpdateTiles(Occlusion(), resourceless_software_draw);
221 int priority_count = 0;
222 timer_.Reset();
223 do {
224 int count = tile_count;
225 RasterTilePriorityQueue queue;
226 host_impl_.BuildRasterQueue(&queue, priorities[priority_count]);
227 while (count--) {
228 ASSERT_FALSE(queue.IsEmpty());
229 ASSERT_TRUE(queue.Top() != NULL);
230 queue.Pop();
232 priority_count = (priority_count + 1) % arraysize(priorities);
233 timer_.NextLap();
234 } while (!timer_.HasTimeLimitExpired());
236 perf_test::PrintResult(
237 "tile_manager_raster_tile_queue_construct_and_iterate",
239 test_name,
240 timer_.LapsPerSecond(),
241 "runs/s",
242 true);
245 void RunEvictionQueueConstructTest(const std::string& test_name,
246 int layer_count) {
247 TreePriority priorities[] = {SAME_PRIORITY_FOR_BOTH_TREES,
248 SMOOTHNESS_TAKES_PRIORITY,
249 NEW_CONTENT_TAKES_PRIORITY};
250 int priority_count = 0;
252 std::vector<LayerImpl*> layers = CreateLayers(layer_count, 10);
253 bool resourceless_software_draw = false;
254 for (unsigned i = 0; i < layers.size(); ++i) {
255 FakePictureLayerImpl* layer =
256 static_cast<FakePictureLayerImpl*>(layers[i]);
257 layer->UpdateTiles(Occlusion(), resourceless_software_draw);
258 for (size_t j = 0; j < layer->GetTilings()->num_tilings(); ++j) {
259 tile_manager()->InitializeTilesWithResourcesForTesting(
260 layer->GetTilings()->tiling_at(j)->AllTilesForTesting());
264 timer_.Reset();
265 do {
266 EvictionTilePriorityQueue queue;
267 host_impl_.BuildEvictionQueue(&queue, 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 unsigned 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<LayerImpl*> layers = CreateLayers(layer_count, tile_count);
289 bool resourceless_software_draw = false;
290 for (unsigned i = 0; i < layers.size(); ++i) {
291 FakePictureLayerImpl* layer =
292 static_cast<FakePictureLayerImpl*>(layers[i]);
293 layer->UpdateTiles(Occlusion(), resourceless_software_draw);
294 for (size_t j = 0; j < layer->GetTilings()->num_tilings(); ++j) {
295 tile_manager()->InitializeTilesWithResourcesForTesting(
296 layer->GetTilings()->tiling_at(j)->AllTilesForTesting());
300 timer_.Reset();
301 do {
302 int count = tile_count;
303 EvictionTilePriorityQueue queue;
304 host_impl_.BuildEvictionQueue(&queue, priorities[priority_count]);
305 while (count--) {
306 ASSERT_FALSE(queue.IsEmpty());
307 ASSERT_TRUE(queue.Top() != NULL);
308 queue.Pop();
310 priority_count = (priority_count + 1) % arraysize(priorities);
311 timer_.NextLap();
312 } while (!timer_.HasTimeLimitExpired());
314 perf_test::PrintResult(
315 "tile_manager_eviction_tile_queue_construct_and_iterate",
317 test_name,
318 timer_.LapsPerSecond(),
319 "runs/s",
320 true);
323 std::vector<LayerImpl*> CreateLayers(int layer_count,
324 int tiles_per_layer_count) {
325 // Compute the width/height required for high res to get
326 // tiles_per_layer_count tiles.
327 float width = std::sqrt(static_cast<float>(tiles_per_layer_count));
328 float height = tiles_per_layer_count / width;
330 // Adjust the width and height to account for the fact that tiles
331 // are bigger than 1x1. Also, account for the fact that that we
332 // will be creating one high res and one low res tiling. That is,
333 // width and height should be smaller by sqrt(1 + low_res_scale).
334 // This gives us _approximately_ correct counts.
335 width *= settings_.default_tile_size.width() /
336 std::sqrt(1 + settings_.low_res_contents_scale_factor);
337 height *= settings_.default_tile_size.height() /
338 std::sqrt(1 + settings_.low_res_contents_scale_factor);
340 // Ensure that we start with blank trees and no tiles.
341 host_impl_.ResetTreesForTesting();
342 tile_manager()->FreeResourcesAndCleanUpReleasedTilesForTesting();
344 gfx::Size layer_bounds(width, height);
345 gfx::Size viewport(width / 5, height / 5);
346 host_impl_.SetViewportSize(viewport);
347 SetupDefaultTreesWithFixedTileSize(layer_bounds,
348 settings_.default_tile_size);
350 active_root_layer_->CreateDefaultTilingsAndTiles();
351 pending_root_layer_->CreateDefaultTilingsAndTiles();
353 std::vector<LayerImpl*> layers;
355 // Pending layer counts as one layer.
356 layers.push_back(pending_root_layer_);
357 int next_id = id_ + 1;
359 // Create the rest of the layers as children of the root layer.
360 scoped_refptr<FakePicturePileImpl> pile =
361 FakePicturePileImpl::CreateFilledPile(kDefaultTileSize, layer_bounds);
362 while (static_cast<int>(layers.size()) < layer_count) {
363 scoped_ptr<FakePictureLayerImpl> layer =
364 FakePictureLayerImpl::CreateWithRasterSource(
365 host_impl_.pending_tree(), next_id, pile);
366 layer->SetBounds(layer_bounds);
367 layers.push_back(layer.get());
368 pending_root_layer_->AddChild(layer.Pass());
370 FakePictureLayerImpl* fake_layer =
371 static_cast<FakePictureLayerImpl*>(layers.back());
373 fake_layer->SetDrawsContent(true);
374 fake_layer->DoPostCommitInitializationIfNeeded();
375 fake_layer->CreateDefaultTilingsAndTiles();
376 ++next_id;
379 return layers;
382 GlobalStateThatImpactsTilePriority GlobalStateForTest() {
383 GlobalStateThatImpactsTilePriority state;
384 gfx::Size tile_size = settings_.default_tile_size;
385 state.soft_memory_limit_in_bytes =
386 10000u * 4u *
387 static_cast<size_t>(tile_size.width() * tile_size.height());
388 state.hard_memory_limit_in_bytes = state.soft_memory_limit_in_bytes;
389 state.num_resources_limit = 10000;
390 state.memory_limit_policy = ALLOW_ANYTHING;
391 state.tree_priority = SMOOTHNESS_TAKES_PRIORITY;
392 return state;
395 void RunManageTilesTest(const std::string& test_name,
396 int layer_count,
397 int approximate_tile_count_per_layer) {
398 std::vector<LayerImpl*> layers =
399 CreateLayers(layer_count, approximate_tile_count_per_layer);
400 timer_.Reset();
401 bool resourceless_software_draw = false;
402 do {
403 BeginFrameArgs args = CreateBeginFrameArgsForTesting();
404 host_impl_.UpdateCurrentBeginFrameArgs(args);
405 for (unsigned i = 0; i < layers.size(); ++i) {
406 layers[i]->UpdateTiles(Occlusion(), resourceless_software_draw);
409 GlobalStateThatImpactsTilePriority global_state(GlobalStateForTest());
410 tile_manager()->ManageTiles(global_state);
411 tile_manager()->UpdateVisibleTiles();
412 timer_.NextLap();
413 host_impl_.ResetCurrentBeginFrameArgsForNextFrame();
414 } while (!timer_.HasTimeLimitExpired());
416 perf_test::PrintResult(
417 "manage_tiles", "", test_name, timer_.LapsPerSecond(), "runs/s", true);
420 TileManager* tile_manager() { return host_impl_.tile_manager(); }
422 protected:
423 GlobalStateThatImpactsTilePriority global_state_;
425 TestSharedBitmapManager shared_bitmap_manager_;
426 TileMemoryLimitPolicy memory_limit_policy_;
427 int max_tiles_;
428 int id_;
429 FakeImplProxy proxy_;
430 FakeLayerTreeHostImpl host_impl_;
431 FakePictureLayerImpl* pending_root_layer_;
432 FakePictureLayerImpl* active_root_layer_;
433 LapTimer timer_;
434 LayerTreeSettings settings_;
436 static const gfx::Size kDefaultTileSize;
439 const gfx::Size TileManagerPerfTest::kDefaultTileSize(100, 100);
441 TEST_F(TileManagerPerfTest, ManageTiles) {
442 RunManageTilesTest("2_100", 2, 100);
443 RunManageTilesTest("2_500", 2, 500);
444 RunManageTilesTest("2_1000", 2, 1000);
445 RunManageTilesTest("10_100", 10, 100);
446 RunManageTilesTest("10_500", 10, 500);
447 RunManageTilesTest("10_1000", 10, 1000);
448 RunManageTilesTest("50_100", 100, 100);
449 RunManageTilesTest("50_500", 100, 500);
450 RunManageTilesTest("50_1000", 100, 1000);
453 TEST_F(TileManagerPerfTest, RasterTileQueueConstruct) {
454 RunRasterQueueConstructTest("2", 2);
455 RunRasterQueueConstructTest("10", 10);
456 RunRasterQueueConstructTest("50", 50);
459 TEST_F(TileManagerPerfTest, RasterTileQueueConstructAndIterate) {
460 RunRasterQueueConstructAndIterateTest("2_16", 2, 16);
461 RunRasterQueueConstructAndIterateTest("2_32", 2, 32);
462 RunRasterQueueConstructAndIterateTest("2_64", 2, 64);
463 RunRasterQueueConstructAndIterateTest("2_128", 2, 128);
464 RunRasterQueueConstructAndIterateTest("10_16", 10, 16);
465 RunRasterQueueConstructAndIterateTest("10_32", 10, 32);
466 RunRasterQueueConstructAndIterateTest("10_64", 10, 64);
467 RunRasterQueueConstructAndIterateTest("10_128", 10, 128);
468 RunRasterQueueConstructAndIterateTest("50_16", 50, 16);
469 RunRasterQueueConstructAndIterateTest("50_32", 50, 32);
470 RunRasterQueueConstructAndIterateTest("50_64", 50, 64);
471 RunRasterQueueConstructAndIterateTest("50_128", 50, 128);
474 TEST_F(TileManagerPerfTest, EvictionTileQueueConstruct) {
475 RunEvictionQueueConstructTest("2", 2);
476 RunEvictionQueueConstructTest("10", 10);
477 RunEvictionQueueConstructTest("50", 50);
480 TEST_F(TileManagerPerfTest, EvictionTileQueueConstructAndIterate) {
481 RunEvictionQueueConstructAndIterateTest("2_16", 2, 16);
482 RunEvictionQueueConstructAndIterateTest("2_32", 2, 32);
483 RunEvictionQueueConstructAndIterateTest("2_64", 2, 64);
484 RunEvictionQueueConstructAndIterateTest("2_128", 2, 128);
485 RunEvictionQueueConstructAndIterateTest("10_16", 10, 16);
486 RunEvictionQueueConstructAndIterateTest("10_32", 10, 32);
487 RunEvictionQueueConstructAndIterateTest("10_64", 10, 64);
488 RunEvictionQueueConstructAndIterateTest("10_128", 10, 128);
489 RunEvictionQueueConstructAndIterateTest("50_16", 50, 16);
490 RunEvictionQueueConstructAndIterateTest("50_32", 50, 32);
491 RunEvictionQueueConstructAndIterateTest("50_64", 50, 64);
492 RunEvictionQueueConstructAndIterateTest("50_128", 50, 128);
495 } // namespace
497 } // namespace cc