Remove implicit conversions from scoped_refptr to T* in cc/
[chromium-blink-merge.git] / cc / debug / rasterize_and_record_benchmark_impl.cc
blobaee027131572e5041d64da75a550caef8fe569a4
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/raster_worker_pool.h"
16 #include "cc/trees/layer_tree_host_common.h"
17 #include "cc/trees/layer_tree_host_impl.h"
18 #include "ui/gfx/rect.h"
20 namespace cc {
22 namespace {
24 const int kDefaultRasterizeRepeatCount = 100;
26 class BenchmarkRasterTask : public Task {
27 public:
28 BenchmarkRasterTask(PicturePileImpl* picture_pile,
29 const gfx::Rect& content_rect,
30 float contents_scale,
31 size_t repeat_count)
32 : picture_pile_(picture_pile),
33 content_rect_(content_rect),
34 contents_scale_(contents_scale),
35 repeat_count_(repeat_count),
36 is_solid_color_(false),
37 best_time_(base::TimeDelta::Max()) {}
39 // Overridden from Task:
40 virtual void RunOnWorkerThread() OVERRIDE {
41 // Parameters for LapTimer.
42 const int kTimeLimitMillis = 1;
43 const int kWarmupRuns = 0;
44 const int kTimeCheckInterval = 1;
46 for (size_t i = 0; i < repeat_count_; ++i) {
47 // Run for a minimum amount of time to avoid problems with timer
48 // quantization when the layer is very small.
49 LapTimer timer(kWarmupRuns,
50 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
51 kTimeCheckInterval);
52 do {
53 SkBitmap bitmap;
54 bitmap.allocPixels(SkImageInfo::MakeN32Premul(content_rect_.width(),
55 content_rect_.height()));
56 SkCanvas canvas(bitmap);
57 PicturePileImpl::Analysis analysis;
59 picture_pile_->AnalyzeInRect(
60 content_rect_, contents_scale_, &analysis, NULL);
61 picture_pile_->RasterToBitmap(
62 &canvas, content_rect_, contents_scale_, NULL);
64 is_solid_color_ = analysis.is_solid_color;
66 timer.NextLap();
67 } while (!timer.HasTimeLimitExpired());
68 base::TimeDelta duration =
69 base::TimeDelta::FromMillisecondsD(timer.MsPerLap());
70 if (duration < best_time_)
71 best_time_ = duration;
75 bool IsSolidColor() const { return is_solid_color_; }
76 base::TimeDelta GetBestTime() const { return best_time_; }
78 private:
79 virtual ~BenchmarkRasterTask() {}
81 PicturePileImpl* picture_pile_;
82 gfx::Rect content_rect_;
83 float contents_scale_;
84 size_t repeat_count_;
85 bool is_solid_color_;
86 base::TimeDelta best_time_;
89 class FixedInvalidationPictureLayerTilingClient
90 : public PictureLayerTilingClient {
91 public:
92 FixedInvalidationPictureLayerTilingClient(
93 PictureLayerTilingClient* base_client,
94 const Region invalidation)
95 : base_client_(base_client), invalidation_(invalidation) {}
97 virtual scoped_refptr<Tile> CreateTile(
98 PictureLayerTiling* tiling,
99 const gfx::Rect& content_rect) OVERRIDE {
100 return base_client_->CreateTile(tiling, content_rect);
103 virtual PicturePileImpl* GetPile() OVERRIDE {
104 return base_client_->GetPile();
107 virtual gfx::Size CalculateTileSize(
108 const gfx::Size& content_bounds) const OVERRIDE {
109 return base_client_->CalculateTileSize(content_bounds);
112 // This is the only function that returns something different from the base
113 // client.
114 virtual const Region* GetInvalidation() OVERRIDE { return &invalidation_; }
116 virtual const PictureLayerTiling* GetTwinTiling(
117 const PictureLayerTiling* tiling) const OVERRIDE {
118 return base_client_->GetTwinTiling(tiling);
121 virtual size_t GetMaxTilesForInterestArea() const OVERRIDE {
122 return base_client_->GetMaxTilesForInterestArea();
125 virtual float GetSkewportTargetTimeInSeconds() const OVERRIDE {
126 return base_client_->GetSkewportTargetTimeInSeconds();
129 virtual int GetSkewportExtrapolationLimitInContentPixels() const OVERRIDE {
130 return base_client_->GetSkewportExtrapolationLimitInContentPixels();
133 virtual WhichTree GetTree() const OVERRIDE { return base_client_->GetTree(); }
135 private:
136 PictureLayerTilingClient* base_client_;
137 Region invalidation_;
140 } // namespace
142 RasterizeAndRecordBenchmarkImpl::RasterizeAndRecordBenchmarkImpl(
143 scoped_refptr<base::MessageLoopProxy> origin_loop,
144 base::Value* value,
145 const MicroBenchmarkImpl::DoneCallback& callback)
146 : MicroBenchmarkImpl(callback, origin_loop),
147 rasterize_repeat_count_(kDefaultRasterizeRepeatCount) {
148 base::DictionaryValue* settings = NULL;
149 value->GetAsDictionary(&settings);
150 if (!settings)
151 return;
153 if (settings->HasKey("rasterize_repeat_count"))
154 settings->GetInteger("rasterize_repeat_count", &rasterize_repeat_count_);
157 RasterizeAndRecordBenchmarkImpl::~RasterizeAndRecordBenchmarkImpl() {}
159 void RasterizeAndRecordBenchmarkImpl::DidCompleteCommit(
160 LayerTreeHostImpl* host) {
161 LayerTreeHostCommon::CallFunctionForSubtree(
162 host->RootLayer(),
163 base::Bind(&RasterizeAndRecordBenchmarkImpl::Run,
164 base::Unretained(this)));
166 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
167 result->SetDouble("rasterize_time_ms",
168 rasterize_results_.total_best_time.InMillisecondsF());
169 result->SetInteger("pixels_rasterized", rasterize_results_.pixels_rasterized);
170 result->SetInteger("pixels_rasterized_with_non_solid_color",
171 rasterize_results_.pixels_rasterized_with_non_solid_color);
172 result->SetInteger("pixels_rasterized_as_opaque",
173 rasterize_results_.pixels_rasterized_as_opaque);
174 result->SetInteger("total_layers", rasterize_results_.total_layers);
175 result->SetInteger("total_picture_layers",
176 rasterize_results_.total_picture_layers);
177 result->SetInteger("total_picture_layers_with_no_content",
178 rasterize_results_.total_picture_layers_with_no_content);
179 result->SetInteger("total_picture_layers_off_screen",
180 rasterize_results_.total_picture_layers_off_screen);
182 NotifyDone(result.PassAs<base::Value>());
185 void RasterizeAndRecordBenchmarkImpl::Run(LayerImpl* layer) {
186 rasterize_results_.total_layers++;
187 layer->RunMicroBenchmark(this);
190 void RasterizeAndRecordBenchmarkImpl::RunOnLayer(PictureLayerImpl* layer) {
191 rasterize_results_.total_picture_layers++;
192 if (!layer->DrawsContent()) {
193 rasterize_results_.total_picture_layers_with_no_content++;
194 return;
196 if (layer->visible_content_rect().IsEmpty()) {
197 rasterize_results_.total_picture_layers_off_screen++;
198 return;
201 TaskGraphRunner* task_graph_runner = RasterWorkerPool::GetTaskGraphRunner();
202 DCHECK(task_graph_runner);
204 if (!task_namespace_.IsValid())
205 task_namespace_ = task_graph_runner->GetNamespaceToken();
207 FixedInvalidationPictureLayerTilingClient client(
208 layer, gfx::Rect(layer->content_bounds()));
209 PictureLayerTilingSet tiling_set(&client, layer->content_bounds());
211 PictureLayerTiling* tiling = tiling_set.AddTiling(layer->contents_scale_x());
212 tiling->CreateAllTilesForTesting();
213 for (PictureLayerTiling::CoverageIterator it(
214 tiling, layer->contents_scale_x(), layer->visible_content_rect());
216 ++it) {
217 DCHECK(*it);
219 PicturePileImpl* picture_pile = (*it)->picture_pile();
220 gfx::Rect content_rect = (*it)->content_rect();
221 float contents_scale = (*it)->contents_scale();
223 scoped_refptr<BenchmarkRasterTask> benchmark_raster_task(
224 new BenchmarkRasterTask(picture_pile,
225 content_rect,
226 contents_scale,
227 rasterize_repeat_count_));
229 TaskGraph graph;
231 graph.nodes.push_back(
232 TaskGraph::Node(benchmark_raster_task.get(),
233 RasterWorkerPool::kBenchmarkRasterTaskPriority,
234 0u));
236 task_graph_runner->ScheduleTasks(task_namespace_, &graph);
237 task_graph_runner->WaitForTasksToFinishRunning(task_namespace_);
239 Task::Vector completed_tasks;
240 task_graph_runner->CollectCompletedTasks(task_namespace_, &completed_tasks);
241 DCHECK_EQ(1u, completed_tasks.size());
242 DCHECK_EQ(completed_tasks[0], benchmark_raster_task);
244 int tile_size = content_rect.width() * content_rect.height();
245 base::TimeDelta min_time = benchmark_raster_task->GetBestTime();
246 bool is_solid_color = benchmark_raster_task->IsSolidColor();
248 if (layer->contents_opaque())
249 rasterize_results_.pixels_rasterized_as_opaque += tile_size;
251 if (!is_solid_color) {
252 rasterize_results_.pixels_rasterized_with_non_solid_color += tile_size;
255 rasterize_results_.pixels_rasterized += tile_size;
256 rasterize_results_.total_best_time += min_time;
260 RasterizeAndRecordBenchmarkImpl::RasterizeResults::RasterizeResults()
261 : pixels_rasterized(0),
262 pixels_rasterized_with_non_solid_color(0),
263 pixels_rasterized_as_opaque(0),
264 total_layers(0),
265 total_picture_layers(0),
266 total_picture_layers_with_no_content(0),
267 total_picture_layers_off_screen(0) {}
269 RasterizeAndRecordBenchmarkImpl::RasterizeResults::~RasterizeResults() {}
271 } // namespace cc