picture_layer_impl_perftest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 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.
4
5#include "cc/layers/picture_layer_impl.h"
6
7#include "cc/debug/lap_timer.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_picture_layer_impl.h"
12#include "cc/test/fake_picture_pile_impl.h"
13#include "cc/test/impl_side_painting_settings.h"
14#include "cc/test/test_shared_bitmap_manager.h"
15#include "cc/trees/layer_tree_impl.h"
16#include "testing/gtest/include/gtest/gtest.h"
17#include "testing/perf/perf_test.h"
18
19namespace cc {
20namespace {
21
22static const int kTimeLimitMillis = 2000;
23static const int kWarmupRuns = 5;
24static const int kTimeCheckInterval = 10;
25
26void AddTiling(float scale,
27               FakePictureLayerImpl* layer,
28               std::vector<Tile*>* all_tiles) {
29  PictureLayerTiling* tiling = layer->AddTiling(scale);
30
31  tiling->CreateAllTilesForTesting();
32  std::vector<Tile*> tiling_tiles = tiling->AllTilesForTesting();
33  std::copy(
34      tiling_tiles.begin(), tiling_tiles.end(), std::back_inserter(*all_tiles));
35}
36
37class PictureLayerImplPerfTest : public testing::Test {
38 public:
39  PictureLayerImplPerfTest()
40      : proxy_(base::MessageLoopProxy::current()),
41        host_impl_(ImplSidePaintingSettings(),
42                   &proxy_,
43                   &shared_bitmap_manager_),
44        timer_(kWarmupRuns,
45               base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
46               kTimeCheckInterval) {}
47
48  virtual void SetUp() OVERRIDE {
49    host_impl_.InitializeRenderer(
50        FakeOutputSurface::Create3d().PassAs<OutputSurface>());
51  }
52
53  void SetupPendingTree(const gfx::Size& layer_bounds,
54                        const gfx::Size& tile_size) {
55    scoped_refptr<FakePicturePileImpl> pile =
56        FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
57    host_impl_.CreatePendingTree();
58    LayerTreeImpl* pending_tree = host_impl_.pending_tree();
59    pending_tree->DetachLayerTree();
60
61    scoped_ptr<FakePictureLayerImpl> pending_layer =
62        FakePictureLayerImpl::CreateWithPile(pending_tree, 7, pile);
63    pending_layer->SetDrawsContent(true);
64    pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
65
66    pending_layer_ = static_cast<FakePictureLayerImpl*>(
67        host_impl_.pending_tree()->LayerById(7));
68    pending_layer_->DoPostCommitInitializationIfNeeded();
69  }
70
71  void RunLayerRasterTileIteratorTest(const std::string& test_name,
72                                      int num_tiles,
73                                      const gfx::Size& viewport_size) {
74    host_impl_.SetViewportSize(viewport_size);
75    host_impl_.pending_tree()->UpdateDrawProperties();
76
77    timer_.Reset();
78    do {
79      int count = num_tiles;
80      for (PictureLayerImpl::LayerRasterTileIterator it(pending_layer_, false);
81           it && count;
82           ++it) {
83        --count;
84      }
85      timer_.NextLap();
86    } while (!timer_.HasTimeLimitExpired());
87
88    perf_test::PrintResult("layer_raster_tile_iterator",
89                           "",
90                           test_name,
91                           timer_.LapsPerSecond(),
92                           "runs/s",
93                           true);
94  }
95
96  void RunEvictionIteratorConstructAndIterateTest(
97      const std::string& test_name,
98      int num_tiles,
99      const gfx::Size& viewport_size) {
100    host_impl_.SetViewportSize(viewport_size);
101    host_impl_.pending_tree()->UpdateDrawProperties();
102
103    TreePriority priorities[] = {SAME_PRIORITY_FOR_BOTH_TREES,
104                                 SMOOTHNESS_TAKES_PRIORITY,
105                                 NEW_CONTENT_TAKES_PRIORITY};
106    int priority_count = 0;
107    timer_.Reset();
108    do {
109      int count = num_tiles;
110      PictureLayerImpl::LayerEvictionTileIterator it(
111          pending_layer_, priorities[priority_count]);
112      while (count--) {
113        ASSERT_TRUE(it) << "count: " << count;
114        ASSERT_TRUE(*it != NULL) << "count: " << count;
115        ++it;
116      }
117      priority_count = (priority_count + 1) % arraysize(priorities);
118      timer_.NextLap();
119    } while (!timer_.HasTimeLimitExpired());
120
121    perf_test::PrintResult("layer_eviction_tile_iterator_construct_and_iterate",
122                           "",
123                           test_name,
124                           timer_.LapsPerSecond(),
125                           "runs/s",
126                           true);
127  }
128
129  void RunEvictionIteratorConstructTest(const std::string& test_name,
130                                        const gfx::Rect& viewport) {
131    host_impl_.SetViewportSize(viewport.size());
132    pending_layer_->SetScrollOffset(gfx::Vector2d(viewport.x(), viewport.y()));
133    host_impl_.pending_tree()->UpdateDrawProperties();
134
135    TreePriority priorities[] = {SAME_PRIORITY_FOR_BOTH_TREES,
136                                 SMOOTHNESS_TAKES_PRIORITY,
137                                 NEW_CONTENT_TAKES_PRIORITY};
138    int priority_count = 0;
139    timer_.Reset();
140    do {
141      PictureLayerImpl::LayerEvictionTileIterator it(
142          pending_layer_, priorities[priority_count]);
143      priority_count = (priority_count + 1) % arraysize(priorities);
144      timer_.NextLap();
145    } while (!timer_.HasTimeLimitExpired());
146
147    perf_test::PrintResult("layer_eviction_tile_iterator_construct",
148                           "",
149                           test_name,
150                           timer_.LapsPerSecond(),
151                           "runs/s",
152                           true);
153  }
154
155 protected:
156  TestSharedBitmapManager shared_bitmap_manager_;
157  FakeImplProxy proxy_;
158  FakeLayerTreeHostImpl host_impl_;
159  FakePictureLayerImpl* pending_layer_;
160  LapTimer timer_;
161
162 private:
163  DISALLOW_COPY_AND_ASSIGN(PictureLayerImplPerfTest);
164};
165
166TEST_F(PictureLayerImplPerfTest, LayerRasterTileIterator) {
167  SetupPendingTree(gfx::Size(10000, 10000), gfx::Size(256, 256));
168
169  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
170
171  pending_layer_->AddTiling(low_res_factor);
172  pending_layer_->AddTiling(0.3f);
173  pending_layer_->AddTiling(0.7f);
174  pending_layer_->AddTiling(1.0f);
175  pending_layer_->AddTiling(2.0f);
176
177  RunLayerRasterTileIteratorTest("32_100x100", 32, gfx::Size(100, 100));
178  RunLayerRasterTileIteratorTest("32_500x500", 32, gfx::Size(500, 500));
179  RunLayerRasterTileIteratorTest("64_100x100", 64, gfx::Size(100, 100));
180  RunLayerRasterTileIteratorTest("64_500x500", 64, gfx::Size(500, 500));
181}
182
183TEST_F(PictureLayerImplPerfTest, LayerEvictionTileIteratorConstructAndIterate) {
184  SetupPendingTree(gfx::Size(10000, 10000), gfx::Size(256, 256));
185
186  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
187
188  std::vector<Tile*> all_tiles;
189  AddTiling(low_res_factor, pending_layer_, &all_tiles);
190  AddTiling(0.3f, pending_layer_, &all_tiles);
191  AddTiling(0.7f, pending_layer_, &all_tiles);
192  AddTiling(1.0f, pending_layer_, &all_tiles);
193  AddTiling(2.0f, pending_layer_, &all_tiles);
194
195  ASSERT_TRUE(host_impl_.tile_manager() != NULL);
196  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
197
198  RunEvictionIteratorConstructAndIterateTest(
199      "32_100x100", 32, gfx::Size(100, 100));
200  RunEvictionIteratorConstructAndIterateTest(
201      "32_500x500", 32, gfx::Size(500, 500));
202  RunEvictionIteratorConstructAndIterateTest(
203      "64_100x100", 64, gfx::Size(100, 100));
204  RunEvictionIteratorConstructAndIterateTest(
205      "64_500x500", 64, gfx::Size(500, 500));
206}
207
208TEST_F(PictureLayerImplPerfTest, LayerEvictionTileIteratorConstruct) {
209  SetupPendingTree(gfx::Size(10000, 10000), gfx::Size(256, 256));
210
211  float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
212
213  std::vector<Tile*> all_tiles;
214  AddTiling(low_res_factor, pending_layer_, &all_tiles);
215  AddTiling(0.3f, pending_layer_, &all_tiles);
216  AddTiling(0.7f, pending_layer_, &all_tiles);
217  AddTiling(1.0f, pending_layer_, &all_tiles);
218  AddTiling(2.0f, pending_layer_, &all_tiles);
219
220  ASSERT_TRUE(host_impl_.tile_manager() != NULL);
221  host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
222
223  RunEvictionIteratorConstructTest("0_0_100x100", gfx::Rect(0, 0, 100, 100));
224  RunEvictionIteratorConstructTest("5000_0_100x100",
225                                   gfx::Rect(5000, 0, 100, 100));
226  RunEvictionIteratorConstructTest("9999_0_100x100",
227                                   gfx::Rect(9999, 0, 100, 100));
228}
229
230}  // namespace
231}  // namespace cc
232