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"
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"
24 const int kDefaultRasterizeRepeatCount
= 100;
26 class BenchmarkRasterTask
: public Task
{
28 BenchmarkRasterTask(PicturePileImpl
* picture_pile
,
29 const gfx::Rect
& content_rect
,
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
),
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
;
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_
; }
79 virtual ~BenchmarkRasterTask() {}
81 PicturePileImpl
* picture_pile_
;
82 gfx::Rect content_rect_
;
83 float contents_scale_
;
86 base::TimeDelta best_time_
;
89 class FixedInvalidationPictureLayerTilingClient
90 : public PictureLayerTilingClient
{
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
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(); }
136 PictureLayerTilingClient
* base_client_
;
137 Region invalidation_
;
142 RasterizeAndRecordBenchmarkImpl::RasterizeAndRecordBenchmarkImpl(
143 scoped_refptr
<base::MessageLoopProxy
> origin_loop
,
145 const MicroBenchmarkImpl::DoneCallback
& callback
)
146 : MicroBenchmarkImpl(callback
, origin_loop
),
147 rasterize_repeat_count_(kDefaultRasterizeRepeatCount
) {
148 base::DictionaryValue
* settings
= NULL
;
149 value
->GetAsDictionary(&settings
);
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(
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
++;
196 if (layer
->visible_content_rect().IsEmpty()) {
197 rasterize_results_
.total_picture_layers_off_screen
++;
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());
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
,
227 rasterize_repeat_count_
));
231 graph
.nodes
.push_back(
232 TaskGraph::Node(benchmark_raster_task
.get(),
233 RasterWorkerPool::kBenchmarkRasterTaskPriority
,
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),
265 total_picture_layers(0),
266 total_picture_layers_with_no_content(0),
267 total_picture_layers_off_screen(0) {}
269 RasterizeAndRecordBenchmarkImpl::RasterizeResults::~RasterizeResults() {}