Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / cc / debug / rasterize_and_record_benchmark_impl.cc
blob1dc110b4fac3bad1929fa04db73daee0cf8c55d2
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(float contents_scale,
77 const gfx::Rect& content_rect) override {
78 return base_client_->CreateTile(contents_scale, content_rect);
81 gfx::Size CalculateTileSize(const gfx::Size& content_bounds) const override {
82 return base_client_->CalculateTileSize(content_bounds);
85 // This is the only function that returns something different from the base
86 // client. Avoids sharing tiles in this area.
87 const Region* GetPendingInvalidation() override { return &invalidation_; }
89 const PictureLayerTiling* GetPendingOrActiveTwinTiling(
90 const PictureLayerTiling* tiling) const override {
91 return base_client_->GetPendingOrActiveTwinTiling(tiling);
94 bool HasValidTilePriorities() const override {
95 return base_client_->HasValidTilePriorities();
98 bool RequiresHighResToDraw() const override {
99 return base_client_->RequiresHighResToDraw();
102 private:
103 PictureLayerTilingClient* base_client_;
104 Region invalidation_;
107 } // namespace
109 RasterizeAndRecordBenchmarkImpl::RasterizeAndRecordBenchmarkImpl(
110 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner,
111 base::Value* value,
112 const MicroBenchmarkImpl::DoneCallback& callback)
113 : MicroBenchmarkImpl(callback, origin_task_runner),
114 rasterize_repeat_count_(kDefaultRasterizeRepeatCount) {
115 base::DictionaryValue* settings = nullptr;
116 value->GetAsDictionary(&settings);
117 if (!settings)
118 return;
120 if (settings->HasKey("rasterize_repeat_count"))
121 settings->GetInteger("rasterize_repeat_count", &rasterize_repeat_count_);
124 RasterizeAndRecordBenchmarkImpl::~RasterizeAndRecordBenchmarkImpl() {}
126 void RasterizeAndRecordBenchmarkImpl::DidCompleteCommit(
127 LayerTreeHostImpl* host) {
128 LayerTreeHostCommon::CallFunctionForSubtree(
129 host->RootLayer(), [this](LayerImpl* layer) {
130 rasterize_results_.total_layers++;
131 layer->RunMicroBenchmark(this);
134 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
135 result->SetDouble("rasterize_time_ms",
136 rasterize_results_.total_best_time.InMillisecondsF());
137 result->SetDouble("total_pictures_in_pile_size",
138 static_cast<int>(rasterize_results_.total_memory_usage));
139 result->SetInteger("pixels_rasterized", rasterize_results_.pixels_rasterized);
140 result->SetInteger("pixels_rasterized_with_non_solid_color",
141 rasterize_results_.pixels_rasterized_with_non_solid_color);
142 result->SetInteger("pixels_rasterized_as_opaque",
143 rasterize_results_.pixels_rasterized_as_opaque);
144 result->SetInteger("total_layers", rasterize_results_.total_layers);
145 result->SetInteger("total_picture_layers",
146 rasterize_results_.total_picture_layers);
147 result->SetInteger("total_picture_layers_with_no_content",
148 rasterize_results_.total_picture_layers_with_no_content);
149 result->SetInteger("total_picture_layers_off_screen",
150 rasterize_results_.total_picture_layers_off_screen);
152 NotifyDone(result.Pass());
155 void RasterizeAndRecordBenchmarkImpl::RunOnLayer(PictureLayerImpl* layer) {
156 rasterize_results_.total_picture_layers++;
157 if (!layer->CanHaveTilings()) {
158 rasterize_results_.total_picture_layers_with_no_content++;
159 return;
161 if (layer->visible_layer_rect().IsEmpty()) {
162 rasterize_results_.total_picture_layers_off_screen++;
163 return;
166 FixedInvalidationPictureLayerTilingClient client(layer,
167 gfx::Rect(layer->bounds()));
169 // In this benchmark, we will create a local tiling set and measure how long
170 // it takes to rasterize content. As such, the actual settings used here don't
171 // really matter.
172 const LayerTreeSettings& settings = layer->layer_tree_impl()->settings();
173 scoped_ptr<PictureLayerTilingSet> tiling_set = PictureLayerTilingSet::Create(
174 layer->GetTree(), &client, settings.max_tiles_for_interest_area,
175 settings.skewport_target_time_in_seconds,
176 settings.skewport_extrapolation_limit_in_content_pixels);
178 PictureLayerTiling* tiling =
179 tiling_set->AddTiling(1.f, layer->GetRasterSource());
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