Fix missing .Pass() on returning scoped_ptr<SdchManager::DictionarySet>
[chromium-blink-merge.git] / cc / debug / rasterize_and_record_benchmark_impl.cc
blob187bb68a833a8e1235dff67c09d1718768b22345
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 "cc/debug/rasterize_and_record_benchmark_impl.h"
7 #include <algorithm>
8 #include <limits>
10 #include "base/basictypes.h"
11 #include "base/values.h"
12 #include "cc/debug/lap_timer.h"
13 #include "cc/layers/layer_impl.h"
14 #include "cc/layers/picture_layer_impl.h"
15 #include "cc/resources/tile_task_worker_pool.h"
16 #include "cc/trees/layer_tree_host_common.h"
17 #include "cc/trees/layer_tree_host_impl.h"
18 #include "cc/trees/layer_tree_impl.h"
19 #include "ui/gfx/geometry/rect.h"
21 namespace cc {
23 namespace {
25 const int kDefaultRasterizeRepeatCount = 100;
27 void RunBenchmark(RasterSource* raster_source,
28 const gfx::Rect& content_rect,
29 float contents_scale,
30 size_t repeat_count,
31 base::TimeDelta* min_time,
32 bool* is_solid_color) {
33 // Parameters for LapTimer.
34 const int kTimeLimitMillis = 1;
35 const int kWarmupRuns = 0;
36 const int kTimeCheckInterval = 1;
38 *min_time = base::TimeDelta::Max();
39 for (size_t i = 0; i < repeat_count; ++i) {
40 // Run for a minimum amount of time to avoid problems with timer
41 // quantization when the layer is very small.
42 LapTimer timer(kWarmupRuns,
43 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
44 kTimeCheckInterval);
45 do {
46 SkBitmap bitmap;
47 bitmap.allocPixels(SkImageInfo::MakeN32Premul(content_rect.width(),
48 content_rect.height()));
49 SkCanvas canvas(bitmap);
50 RasterSource::SolidColorAnalysis analysis;
52 raster_source->PerformSolidColorAnalysis(content_rect, contents_scale,
53 &analysis);
54 raster_source->PlaybackToCanvas(&canvas, content_rect, contents_scale);
56 *is_solid_color = analysis.is_solid_color;
58 timer.NextLap();
59 } while (!timer.HasTimeLimitExpired());
60 base::TimeDelta duration =
61 base::TimeDelta::FromMillisecondsD(timer.MsPerLap());
62 if (duration < *min_time)
63 *min_time = duration;
67 class FixedInvalidationPictureLayerTilingClient
68 : public PictureLayerTilingClient {
69 public:
70 FixedInvalidationPictureLayerTilingClient(
71 PictureLayerTilingClient* base_client,
72 const Region invalidation)
73 : base_client_(base_client), invalidation_(invalidation) {}
75 scoped_refptr<Tile> CreateTile(float contents_scale,
76 const gfx::Rect& content_rect) override {
77 return base_client_->CreateTile(contents_scale, content_rect);
80 gfx::Size CalculateTileSize(const gfx::Size& content_bounds) const override {
81 return base_client_->CalculateTileSize(content_bounds);
84 // This is the only function that returns something different from the base
85 // client. Avoids sharing tiles in this area.
86 const Region* GetPendingInvalidation() override { return &invalidation_; }
88 const PictureLayerTiling* GetPendingOrActiveTwinTiling(
89 const PictureLayerTiling* tiling) const override {
90 return base_client_->GetPendingOrActiveTwinTiling(tiling);
93 PictureLayerTiling* GetRecycledTwinTiling(
94 const PictureLayerTiling* tiling) override {
95 return base_client_->GetRecycledTwinTiling(tiling);
98 TilePriority::PriorityBin GetMaxTilePriorityBin() const override {
99 return base_client_->GetMaxTilePriorityBin();
102 WhichTree GetTree() const override { return base_client_->GetTree(); }
104 bool RequiresHighResToDraw() const override {
105 return base_client_->RequiresHighResToDraw();
108 void TilingLiveRectChanged() override {}
110 private:
111 PictureLayerTilingClient* base_client_;
112 Region invalidation_;
115 } // namespace
117 RasterizeAndRecordBenchmarkImpl::RasterizeAndRecordBenchmarkImpl(
118 scoped_refptr<base::MessageLoopProxy> origin_loop,
119 base::Value* value,
120 const MicroBenchmarkImpl::DoneCallback& callback)
121 : MicroBenchmarkImpl(callback, origin_loop),
122 rasterize_repeat_count_(kDefaultRasterizeRepeatCount) {
123 base::DictionaryValue* settings = nullptr;
124 value->GetAsDictionary(&settings);
125 if (!settings)
126 return;
128 if (settings->HasKey("rasterize_repeat_count"))
129 settings->GetInteger("rasterize_repeat_count", &rasterize_repeat_count_);
132 RasterizeAndRecordBenchmarkImpl::~RasterizeAndRecordBenchmarkImpl() {}
134 void RasterizeAndRecordBenchmarkImpl::DidCompleteCommit(
135 LayerTreeHostImpl* host) {
136 LayerTreeHostCommon::CallFunctionForSubtree(
137 host->RootLayer(),
138 base::Bind(&RasterizeAndRecordBenchmarkImpl::Run,
139 base::Unretained(this)));
141 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
142 result->SetDouble("rasterize_time_ms",
143 rasterize_results_.total_best_time.InMillisecondsF());
144 result->SetDouble("total_pictures_in_pile_size",
145 rasterize_results_.total_memory_usage);
146 result->SetInteger("pixels_rasterized", rasterize_results_.pixels_rasterized);
147 result->SetInteger("pixels_rasterized_with_non_solid_color",
148 rasterize_results_.pixels_rasterized_with_non_solid_color);
149 result->SetInteger("pixels_rasterized_as_opaque",
150 rasterize_results_.pixels_rasterized_as_opaque);
151 result->SetInteger("total_layers", rasterize_results_.total_layers);
152 result->SetInteger("total_picture_layers",
153 rasterize_results_.total_picture_layers);
154 result->SetInteger("total_picture_layers_with_no_content",
155 rasterize_results_.total_picture_layers_with_no_content);
156 result->SetInteger("total_picture_layers_off_screen",
157 rasterize_results_.total_picture_layers_off_screen);
159 NotifyDone(result.Pass());
162 void RasterizeAndRecordBenchmarkImpl::Run(LayerImpl* layer) {
163 rasterize_results_.total_layers++;
164 layer->RunMicroBenchmark(this);
167 void RasterizeAndRecordBenchmarkImpl::RunOnLayer(PictureLayerImpl* layer) {
168 rasterize_results_.total_picture_layers++;
169 if (!layer->CanHaveTilings()) {
170 rasterize_results_.total_picture_layers_with_no_content++;
171 return;
173 if (layer->visible_content_rect().IsEmpty()) {
174 rasterize_results_.total_picture_layers_off_screen++;
175 return;
178 FixedInvalidationPictureLayerTilingClient client(
179 layer, gfx::Rect(layer->content_bounds()));
181 // In this benchmark, we will create a local tiling set and measure how long
182 // it takes to rasterize content. As such, the actual settings used here don't
183 // really matter.
184 const LayerTreeSettings& settings = layer->layer_tree_impl()->settings();
185 scoped_ptr<PictureLayerTilingSet> tiling_set = PictureLayerTilingSet::Create(
186 &client, settings.max_tiles_for_interest_area,
187 settings.skewport_target_time_in_seconds,
188 settings.skewport_extrapolation_limit_in_content_pixels);
190 PictureLayerTiling* tiling = tiling_set->AddTiling(layer->contents_scale_x(),
191 layer->GetRasterSource());
192 tiling->CreateAllTilesForTesting();
193 for (PictureLayerTiling::CoverageIterator it(
194 tiling, layer->contents_scale_x(), layer->visible_content_rect());
196 ++it) {
197 DCHECK(*it);
199 RasterSource* raster_source = (*it)->raster_source();
200 gfx::Rect content_rect = (*it)->content_rect();
201 float contents_scale = (*it)->contents_scale();
203 base::TimeDelta min_time;
204 bool is_solid_color = false;
205 RunBenchmark(raster_source, content_rect, contents_scale,
206 rasterize_repeat_count_, &min_time, &is_solid_color);
208 int tile_size = content_rect.width() * content_rect.height();
209 if (layer->contents_opaque())
210 rasterize_results_.pixels_rasterized_as_opaque += tile_size;
212 if (!is_solid_color)
213 rasterize_results_.pixels_rasterized_with_non_solid_color += tile_size;
215 rasterize_results_.pixels_rasterized += tile_size;
216 rasterize_results_.total_best_time += min_time;
219 const RasterSource* layer_raster_source = layer->GetRasterSource();
220 rasterize_results_.total_memory_usage +=
221 layer_raster_source->GetPictureMemoryUsage();
224 RasterizeAndRecordBenchmarkImpl::RasterizeResults::RasterizeResults()
225 : pixels_rasterized(0),
226 pixels_rasterized_with_non_solid_color(0),
227 pixels_rasterized_as_opaque(0),
228 total_memory_usage(0),
229 total_layers(0),
230 total_picture_layers(0),
231 total_picture_layers_with_no_content(0),
232 total_picture_layers_off_screen(0) {
235 RasterizeAndRecordBenchmarkImpl::RasterizeResults::~RasterizeResults() {}
237 } // namespace cc