Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / cc / resources / tile_manager_perftest.cc
blobe81270d8cf56f87c9dc4f1d5b11b1d1059911733
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/resources/tile.h"
7 #include "cc/resources/tile_priority.h"
8 #include "cc/test/fake_impl_proxy.h"
9 #include "cc/test/fake_layer_tree_host_impl.h"
10 #include "cc/test/fake_output_surface.h"
11 #include "cc/test/fake_output_surface_client.h"
12 #include "cc/test/fake_picture_layer_impl.h"
13 #include "cc/test/fake_picture_pile_impl.h"
14 #include "cc/test/fake_tile_manager.h"
15 #include "cc/test/fake_tile_manager_client.h"
16 #include "cc/test/impl_side_painting_settings.h"
17 #include "cc/test/lap_timer.h"
18 #include "cc/test/test_shared_bitmap_manager.h"
19 #include "cc/test/test_tile_priorities.h"
20 #include "cc/trees/layer_tree_impl.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "testing/perf/perf_test.h"
25 namespace cc {
27 namespace {
29 static const int kTimeLimitMillis = 2000;
30 static const int kWarmupRuns = 5;
31 static const int kTimeCheckInterval = 10;
33 class TileManagerPerfTest : public testing::Test {
34 public:
35 typedef std::vector<std::pair<scoped_refptr<Tile>, ManagedTileBin> >
36 TileBinVector;
38 TileManagerPerfTest()
39 : timer_(kWarmupRuns,
40 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
41 kTimeCheckInterval) {}
43 // Overridden from testing::Test:
44 virtual void SetUp() OVERRIDE {
45 output_surface_ = FakeOutputSurface::Create3d();
46 CHECK(output_surface_->BindToClient(&output_surface_client_));
48 shared_bitmap_manager_.reset(new TestSharedBitmapManager());
49 resource_provider_ = ResourceProvider::Create(
50 output_surface_.get(), shared_bitmap_manager_.get(), 0, false, 1);
51 resource_pool_ = ResourcePool::Create(
52 resource_provider_.get(), GL_TEXTURE_2D, RGBA_8888);
53 size_t raster_task_limit_bytes = 32 * 1024 * 1024; // 16-64MB in practice.
54 tile_manager_ = make_scoped_ptr(new FakeTileManager(
55 &tile_manager_client_, resource_pool_.get(), raster_task_limit_bytes));
56 picture_pile_ = FakePicturePileImpl::CreateInfiniteFilledPile();
59 GlobalStateThatImpactsTilePriority GlobalStateForTest() {
60 GlobalStateThatImpactsTilePriority state;
61 gfx::Size tile_size = settings_.default_tile_size;
62 state.soft_memory_limit_in_bytes =
63 10000u * 4u *
64 static_cast<size_t>(tile_size.width() * tile_size.height());
65 state.hard_memory_limit_in_bytes = state.soft_memory_limit_in_bytes;
66 state.num_resources_limit = 10000;
67 state.memory_limit_policy = ALLOW_ANYTHING;
68 state.tree_priority = SMOOTHNESS_TAKES_PRIORITY;
69 return state;
72 virtual void TearDown() OVERRIDE {
73 tile_manager_.reset(NULL);
74 picture_pile_ = NULL;
77 TilePriority GetTilePriorityFromBin(ManagedTileBin bin) {
78 switch (bin) {
79 case NOW_AND_READY_TO_DRAW_BIN:
80 case NOW_BIN:
81 return TilePriorityForNowBin();
82 case SOON_BIN:
83 return TilePriorityForSoonBin();
84 case EVENTUALLY_AND_ACTIVE_BIN:
85 case EVENTUALLY_BIN:
86 return TilePriorityForEventualBin();
87 case AT_LAST_BIN:
88 case AT_LAST_AND_ACTIVE_BIN:
89 case NEVER_BIN:
90 return TilePriority();
91 default:
92 NOTREACHED();
93 return TilePriority();
97 ManagedTileBin GetNextBin(ManagedTileBin bin) {
98 switch (bin) {
99 case NOW_AND_READY_TO_DRAW_BIN:
100 case NOW_BIN:
101 return SOON_BIN;
102 case SOON_BIN:
103 return EVENTUALLY_BIN;
104 case EVENTUALLY_AND_ACTIVE_BIN:
105 case EVENTUALLY_BIN:
106 return NEVER_BIN;
107 case AT_LAST_BIN:
108 case AT_LAST_AND_ACTIVE_BIN:
109 case NEVER_BIN:
110 return NOW_BIN;
111 default:
112 NOTREACHED();
113 return NEVER_BIN;
117 void CreateBinTiles(int count, ManagedTileBin bin, TileBinVector* tiles) {
118 for (int i = 0; i < count; ++i) {
119 scoped_refptr<Tile> tile =
120 tile_manager_->CreateTile(picture_pile_.get(),
121 settings_.default_tile_size,
122 gfx::Rect(),
123 gfx::Rect(),
124 1.0,
127 Tile::USE_LCD_TEXT);
128 tile->SetPriority(ACTIVE_TREE, GetTilePriorityFromBin(bin));
129 tile->SetPriority(PENDING_TREE, GetTilePriorityFromBin(bin));
130 tiles->push_back(std::make_pair(tile, bin));
134 void CreateTiles(int count, TileBinVector* tiles) {
135 // Roughly an equal amount of all bins.
136 int count_per_bin = count / NUM_BINS;
137 CreateBinTiles(count_per_bin, NOW_BIN, tiles);
138 CreateBinTiles(count_per_bin, SOON_BIN, tiles);
139 CreateBinTiles(count_per_bin, EVENTUALLY_BIN, tiles);
140 CreateBinTiles(count - 3 * count_per_bin, NEVER_BIN, tiles);
143 void RunManageTilesTest(const std::string& test_name,
144 unsigned tile_count,
145 int priority_change_percent) {
146 DCHECK_GE(tile_count, 100u);
147 DCHECK_GE(priority_change_percent, 0);
148 DCHECK_LE(priority_change_percent, 100);
149 TileBinVector tiles;
150 CreateTiles(tile_count, &tiles);
151 timer_.Reset();
152 do {
153 if (priority_change_percent > 0) {
154 for (unsigned i = 0; i < tile_count;
155 i += 100 / priority_change_percent) {
156 Tile* tile = tiles[i].first.get();
157 ManagedTileBin bin = GetNextBin(tiles[i].second);
158 tile->SetPriority(ACTIVE_TREE, GetTilePriorityFromBin(bin));
159 tile->SetPriority(PENDING_TREE, GetTilePriorityFromBin(bin));
160 tiles[i].second = bin;
164 GlobalStateThatImpactsTilePriority global_state(GlobalStateForTest());
165 resource_pool_->SetResourceUsageLimits(
166 global_state.soft_memory_limit_in_bytes,
168 global_state.num_resources_limit);
169 tile_manager_->ManageTiles(global_state);
170 tile_manager_->UpdateVisibleTiles();
171 timer_.NextLap();
172 } while (!timer_.HasTimeLimitExpired());
174 perf_test::PrintResult(
175 "manage_tiles", "", test_name, timer_.LapsPerSecond(), "runs/s", true);
178 private:
179 FakeTileManagerClient tile_manager_client_;
180 LayerTreeSettings settings_;
181 scoped_ptr<FakeTileManager> tile_manager_;
182 scoped_refptr<FakePicturePileImpl> picture_pile_;
183 FakeOutputSurfaceClient output_surface_client_;
184 scoped_ptr<FakeOutputSurface> output_surface_;
185 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
186 scoped_ptr<ResourceProvider> resource_provider_;
187 scoped_ptr<ResourcePool> resource_pool_;
188 LapTimer timer_;
191 TEST_F(TileManagerPerfTest, ManageTiles) {
192 RunManageTilesTest("100_0", 100, 0);
193 RunManageTilesTest("1000_0", 1000, 0);
194 RunManageTilesTest("10000_0", 10000, 0);
195 RunManageTilesTest("100_10", 100, 10);
196 RunManageTilesTest("1000_10", 1000, 10);
197 RunManageTilesTest("10000_10", 10000, 10);
198 RunManageTilesTest("100_100", 100, 100);
199 RunManageTilesTest("1000_100", 1000, 100);
200 RunManageTilesTest("10000_100", 10000, 100);
203 class TileManagerTileIteratorPerfTest : public testing::Test,
204 public TileManagerClient {
205 public:
206 TileManagerTileIteratorPerfTest()
207 : memory_limit_policy_(ALLOW_ANYTHING),
208 max_tiles_(10000),
209 ready_to_activate_(false),
210 id_(7),
211 proxy_(base::MessageLoopProxy::current()),
212 host_impl_(ImplSidePaintingSettings(),
213 &proxy_,
214 &shared_bitmap_manager_),
215 timer_(kWarmupRuns,
216 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
217 kTimeCheckInterval) {}
219 void SetTreePriority(TreePriority tree_priority) {
220 GlobalStateThatImpactsTilePriority state;
221 gfx::Size tile_size(256, 256);
223 state.soft_memory_limit_in_bytes = 100 * 1000 * 1000;
224 state.num_resources_limit = max_tiles_;
225 state.hard_memory_limit_in_bytes = state.soft_memory_limit_in_bytes * 2;
226 state.memory_limit_policy = memory_limit_policy_;
227 state.tree_priority = tree_priority;
229 global_state_ = state;
230 host_impl_.resource_pool()->SetResourceUsageLimits(
231 state.soft_memory_limit_in_bytes, 0, state.num_resources_limit);
232 host_impl_.tile_manager()->SetGlobalStateForTesting(state);
235 virtual void SetUp() OVERRIDE {
236 InitializeRenderer();
237 SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
240 virtual void InitializeRenderer() {
241 host_impl_.InitializeRenderer(
242 FakeOutputSurface::Create3d().PassAs<OutputSurface>());
245 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
246 gfx::Size tile_size(100, 100);
248 scoped_refptr<FakePicturePileImpl> pending_pile =
249 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
250 scoped_refptr<FakePicturePileImpl> active_pile =
251 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
253 SetupTrees(pending_pile, active_pile);
256 void ActivateTree() {
257 host_impl_.ActivatePendingTree();
258 CHECK(!host_impl_.pending_tree());
259 pending_layer_ = NULL;
260 active_layer_ = static_cast<FakePictureLayerImpl*>(
261 host_impl_.active_tree()->LayerById(id_));
264 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
265 const gfx::Size& tile_size) {
266 SetupDefaultTrees(layer_bounds);
267 pending_layer_->set_fixed_tile_size(tile_size);
268 active_layer_->set_fixed_tile_size(tile_size);
271 void SetupTrees(scoped_refptr<PicturePileImpl> pending_pile,
272 scoped_refptr<PicturePileImpl> active_pile) {
273 SetupPendingTree(active_pile);
274 ActivateTree();
275 SetupPendingTree(pending_pile);
278 void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
279 host_impl_.CreatePendingTree();
280 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
281 // Clear recycled tree.
282 pending_tree->DetachLayerTree();
284 scoped_ptr<FakePictureLayerImpl> pending_layer =
285 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
286 pending_layer->SetDrawsContent(true);
287 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
289 pending_layer_ = static_cast<FakePictureLayerImpl*>(
290 host_impl_.pending_tree()->LayerById(id_));
291 pending_layer_->DoPostCommitInitializationIfNeeded();
294 void CreateHighLowResAndSetAllTilesVisible() {
295 // Active layer must get updated first so pending layer can share from it.
296 active_layer_->CreateDefaultTilingsAndTiles();
297 active_layer_->SetAllTilesVisible();
298 pending_layer_->CreateDefaultTilingsAndTiles();
299 pending_layer_->SetAllTilesVisible();
302 void RunTest(const std::string& test_name, unsigned tile_count) {
303 timer_.Reset();
304 do {
305 for (TileManager::RasterTileIterator it(tile_manager(),
306 SAME_PRIORITY_FOR_BOTH_TREES);
307 it && tile_count;
308 ++it) {
309 --tile_count;
311 ASSERT_EQ(0u, tile_count);
312 timer_.NextLap();
313 } while (!timer_.HasTimeLimitExpired());
315 perf_test::PrintResult("tile_manager_raster_tile_iterator",
317 test_name,
318 timer_.LapsPerSecond(),
319 "runs/s",
320 true);
323 // TileManagerClient implementation.
324 virtual void NotifyReadyToActivate() OVERRIDE { ready_to_activate_ = true; }
325 virtual void NotifyTileInitialized(const Tile* tile) OVERRIDE {}
327 TileManager* tile_manager() { return host_impl_.tile_manager(); }
329 protected:
330 GlobalStateThatImpactsTilePriority global_state_;
332 TestSharedBitmapManager shared_bitmap_manager_;
333 TileMemoryLimitPolicy memory_limit_policy_;
334 int max_tiles_;
335 bool ready_to_activate_;
336 int id_;
337 FakeImplProxy proxy_;
338 FakeLayerTreeHostImpl host_impl_;
339 FakePictureLayerImpl* pending_layer_;
340 FakePictureLayerImpl* active_layer_;
341 LapTimer timer_;
344 TEST_F(TileManagerTileIteratorPerfTest, RasterTileIterator) {
345 SetupDefaultTrees(gfx::Size(10000, 10000));
346 active_layer_->CreateDefaultTilingsAndTiles();
347 pending_layer_->CreateDefaultTilingsAndTiles();
349 RunTest("2_16", 16);
350 RunTest("2_32", 32);
351 RunTest("2_64", 64);
352 RunTest("2_128", 128);
355 } // namespace
357 } // namespace cc