Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / debug / rasterize_and_record_benchmark_impl.cc
blobd798dc6705ecd1c19217d06483839419628adb45
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/raster/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, content_rect,
55 contents_scale);
57 *is_solid_color = analysis.is_solid_color;
59 timer.NextLap();
60 } while (!timer.HasTimeLimitExpired());
61 base::TimeDelta duration =
62 base::TimeDelta::FromMillisecondsD(timer.MsPerLap());
63 if (duration < *min_time)
64 *min_time = duration;
68 class FixedInvalidationPictureLayerTilingClient
69 : public PictureLayerTilingClient {
70 public:
71 FixedInvalidationPictureLayerTilingClient(
72 PictureLayerTilingClient* base_client,
73 const Region invalidation)
74 : base_client_(base_client), invalidation_(invalidation) {}
76 ScopedTilePtr CreateTile(const Tile::CreateInfo& info) override {
77 return base_client_->CreateTile(info);
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 bool HasValidTilePriorities() const override {
94 return base_client_->HasValidTilePriorities();
97 bool RequiresHighResToDraw() const override {
98 return base_client_->RequiresHighResToDraw();
101 private:
102 PictureLayerTilingClient* base_client_;
103 Region invalidation_;
106 } // namespace
108 RasterizeAndRecordBenchmarkImpl::RasterizeAndRecordBenchmarkImpl(
109 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner,
110 base::Value* value,
111 const MicroBenchmarkImpl::DoneCallback& callback)
112 : MicroBenchmarkImpl(callback, origin_task_runner),
113 rasterize_repeat_count_(kDefaultRasterizeRepeatCount) {
114 base::DictionaryValue* settings = nullptr;
115 value->GetAsDictionary(&settings);
116 if (!settings)
117 return;
119 if (settings->HasKey("rasterize_repeat_count"))
120 settings->GetInteger("rasterize_repeat_count", &rasterize_repeat_count_);
123 RasterizeAndRecordBenchmarkImpl::~RasterizeAndRecordBenchmarkImpl() {}
125 void RasterizeAndRecordBenchmarkImpl::DidCompleteCommit(
126 LayerTreeHostImpl* host) {
127 LayerTreeHostCommon::CallFunctionForSubtree(
128 host->RootLayer(), [this](LayerImpl* layer) {
129 rasterize_results_.total_layers++;
130 layer->RunMicroBenchmark(this);
133 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
134 result->SetDouble("rasterize_time_ms",
135 rasterize_results_.total_best_time.InMillisecondsF());
136 result->SetDouble("total_pictures_in_pile_size",
137 static_cast<int>(rasterize_results_.total_memory_usage));
138 result->SetInteger("pixels_rasterized", rasterize_results_.pixels_rasterized);
139 result->SetInteger("pixels_rasterized_with_non_solid_color",
140 rasterize_results_.pixels_rasterized_with_non_solid_color);
141 result->SetInteger("pixels_rasterized_as_opaque",
142 rasterize_results_.pixels_rasterized_as_opaque);
143 result->SetInteger("total_layers", rasterize_results_.total_layers);
144 result->SetInteger("total_picture_layers",
145 rasterize_results_.total_picture_layers);
146 result->SetInteger("total_picture_layers_with_no_content",
147 rasterize_results_.total_picture_layers_with_no_content);
148 result->SetInteger("total_picture_layers_off_screen",
149 rasterize_results_.total_picture_layers_off_screen);
151 NotifyDone(result.Pass());
154 void RasterizeAndRecordBenchmarkImpl::RunOnLayer(PictureLayerImpl* layer) {
155 rasterize_results_.total_picture_layers++;
156 if (!layer->CanHaveTilings()) {
157 rasterize_results_.total_picture_layers_with_no_content++;
158 return;
160 if (layer->visible_layer_rect().IsEmpty()) {
161 rasterize_results_.total_picture_layers_off_screen++;
162 return;
165 FixedInvalidationPictureLayerTilingClient client(layer,
166 gfx::Rect(layer->bounds()));
168 // In this benchmark, we will create a local tiling set and measure how long
169 // it takes to rasterize content. As such, the actual settings used here don't
170 // really matter.
171 const LayerTreeSettings& settings = layer->layer_tree_impl()->settings();
172 scoped_ptr<PictureLayerTilingSet> tiling_set = PictureLayerTilingSet::Create(
173 layer->GetTree(), &client, settings.tiling_interest_area_padding,
174 settings.skewport_target_time_in_seconds,
175 settings.skewport_extrapolation_limit_in_content_pixels);
177 PictureLayerTiling* tiling =
178 tiling_set->AddTiling(1.f, layer->GetRasterSource());
179 tiling->set_resolution(HIGH_RESOLUTION);
180 tiling->CreateAllTilesForTesting();
181 RasterSource* raster_source = tiling->raster_source();
182 for (PictureLayerTiling::CoverageIterator it(tiling, 1.f,
183 layer->visible_layer_rect());
184 it; ++it) {
185 DCHECK(*it);
187 gfx::Rect content_rect = (*it)->content_rect();
188 float contents_scale = (*it)->contents_scale();
190 base::TimeDelta min_time;
191 bool is_solid_color = false;
192 RunBenchmark(raster_source, content_rect, contents_scale,
193 rasterize_repeat_count_, &min_time, &is_solid_color);
195 int tile_size = content_rect.width() * content_rect.height();
196 if (layer->contents_opaque())
197 rasterize_results_.pixels_rasterized_as_opaque += tile_size;
199 if (!is_solid_color)
200 rasterize_results_.pixels_rasterized_with_non_solid_color += tile_size;
202 rasterize_results_.pixels_rasterized += tile_size;
203 rasterize_results_.total_best_time += min_time;
206 const RasterSource* layer_raster_source = layer->GetRasterSource();
207 rasterize_results_.total_memory_usage +=
208 layer_raster_source->GetPictureMemoryUsage();
211 RasterizeAndRecordBenchmarkImpl::RasterizeResults::RasterizeResults()
212 : pixels_rasterized(0),
213 pixels_rasterized_with_non_solid_color(0),
214 pixels_rasterized_as_opaque(0),
215 total_memory_usage(0),
216 total_layers(0),
217 total_picture_layers(0),
218 total_picture_layers_with_no_content(0),
219 total_picture_layers_off_screen(0) {
222 RasterizeAndRecordBenchmarkImpl::RasterizeResults::~RasterizeResults() {}
224 } // namespace cc