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.h"
11 #include "base/basictypes.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/values.h"
14 #include "cc/debug/lap_timer.h"
15 #include "cc/debug/rasterize_and_record_benchmark_impl.h"
16 #include "cc/layers/content_layer_client.h"
17 #include "cc/layers/layer.h"
18 #include "cc/layers/picture_layer.h"
19 #include "cc/resources/display_item_list.h"
20 #include "cc/resources/picture_pile.h"
21 #include "cc/trees/layer_tree_host.h"
22 #include "cc/trees/layer_tree_host_common.h"
23 #include "third_party/skia/include/utils/SkPictureUtils.h"
24 #include "ui/gfx/geometry/rect.h"
30 const int kDefaultRecordRepeatCount
= 100;
32 // Parameters for LapTimer.
33 const int kTimeLimitMillis
= 1;
34 const int kWarmupRuns
= 0;
35 const int kTimeCheckInterval
= 1;
37 const char* kModeSuffixes
[RecordingSource::RECORDING_MODE_COUNT
] =
38 {"", "_sk_null_canvas", "_painting_disabled", "_caching_disabled"};
42 RasterizeAndRecordBenchmark::RasterizeAndRecordBenchmark(
43 scoped_ptr
<base::Value
> value
,
44 const MicroBenchmark::DoneCallback
& callback
)
45 : MicroBenchmark(callback
),
46 record_repeat_count_(kDefaultRecordRepeatCount
),
47 settings_(value
.Pass()),
48 main_thread_benchmark_done_(false),
50 weak_ptr_factory_(this) {
51 base::DictionaryValue
* settings
= nullptr;
52 settings_
->GetAsDictionary(&settings
);
56 if (settings
->HasKey("record_repeat_count"))
57 settings
->GetInteger("record_repeat_count", &record_repeat_count_
);
60 RasterizeAndRecordBenchmark::~RasterizeAndRecordBenchmark() {
61 weak_ptr_factory_
.InvalidateWeakPtrs();
64 void RasterizeAndRecordBenchmark::DidUpdateLayers(LayerTreeHost
* host
) {
66 LayerTreeHostCommon::CallFunctionForSubtree(
68 [this](Layer
* layer
) { layer
->RunMicroBenchmark(this); });
70 DCHECK(!results_
.get());
71 results_
= make_scoped_ptr(new base::DictionaryValue
);
72 results_
->SetInteger("pixels_recorded", record_results_
.pixels_recorded
);
73 results_
->SetInteger("picture_memory_usage", record_results_
.bytes_used
);
75 for (int i
= 0; i
< RecordingSource::RECORDING_MODE_COUNT
; i
++) {
76 std::string name
= base::StringPrintf("record_time%s_ms", kModeSuffixes
[i
]);
77 results_
->SetDouble(name
,
78 record_results_
.total_best_time
[i
].InMillisecondsF());
80 main_thread_benchmark_done_
= true;
83 void RasterizeAndRecordBenchmark::RecordRasterResults(
84 scoped_ptr
<base::Value
> results_value
) {
85 DCHECK(main_thread_benchmark_done_
);
87 base::DictionaryValue
* results
= nullptr;
88 results_value
->GetAsDictionary(&results
);
91 results_
->MergeDictionary(results
);
93 NotifyDone(results_
.Pass());
96 scoped_ptr
<MicroBenchmarkImpl
> RasterizeAndRecordBenchmark::CreateBenchmarkImpl(
97 scoped_refptr
<base::MessageLoopProxy
> origin_loop
) {
98 return make_scoped_ptr(new RasterizeAndRecordBenchmarkImpl(
101 base::Bind(&RasterizeAndRecordBenchmark::RecordRasterResults
,
102 weak_ptr_factory_
.GetWeakPtr())));
105 void RasterizeAndRecordBenchmark::RunOnLayer(PictureLayer
* layer
) {
108 gfx::Rect visible_content_rect
= gfx::ScaleToEnclosingRect(
109 layer
->visible_content_rect(), 1.f
/ layer
->contents_scale_x());
110 if (visible_content_rect
.IsEmpty())
113 if (host_
->settings().use_display_lists
) {
114 RunOnDisplayListLayer(layer
, visible_content_rect
);
116 RunOnPictureLayer(layer
, visible_content_rect
);
120 void RasterizeAndRecordBenchmark::RunOnPictureLayer(
122 const gfx::Rect
& visible_content_rect
) {
123 ContentLayerClient
* painter
= layer
->client();
125 DCHECK(host_
&& !host_
->settings().use_display_lists
);
127 gfx::Size tile_grid_size
= host_
->settings().default_tile_size
;
129 for (int mode_index
= 0; mode_index
< RecordingSource::RECORDING_MODE_COUNT
;
131 RecordingSource::RecordingMode mode
=
132 static_cast<RecordingSource::RecordingMode
>(mode_index
);
133 base::TimeDelta min_time
= base::TimeDelta::Max();
134 size_t memory_used
= 0;
136 for (int i
= 0; i
< record_repeat_count_
; ++i
) {
137 // Run for a minimum amount of time to avoid problems with timer
138 // quantization when the layer is very small.
139 LapTimer
timer(kWarmupRuns
,
140 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
142 scoped_refptr
<Picture
> picture
;
144 picture
= Picture::Create(visible_content_rect
, painter
, tile_grid_size
,
147 // Verify we are recording the same thing each time.
148 DCHECK(memory_used
== picture
->ApproximateMemoryUsage());
150 memory_used
= picture
->ApproximateMemoryUsage();
154 } while (!timer
.HasTimeLimitExpired());
155 base::TimeDelta duration
=
156 base::TimeDelta::FromMillisecondsD(timer
.MsPerLap());
157 if (duration
< min_time
)
161 if (mode
== RecordingSource::RECORD_NORMALLY
) {
162 record_results_
.bytes_used
+= memory_used
;
163 record_results_
.pixels_recorded
+=
164 visible_content_rect
.width() * visible_content_rect
.height();
166 record_results_
.total_best_time
[mode_index
] += min_time
;
170 void RasterizeAndRecordBenchmark::RunOnDisplayListLayer(
172 const gfx::Rect
& visible_content_rect
) {
173 ContentLayerClient
* painter
= layer
->client();
175 DCHECK(host_
&& host_
->settings().use_display_lists
);
177 for (int mode_index
= 0; mode_index
< RecordingSource::RECORDING_MODE_COUNT
;
179 ContentLayerClient::PaintingControlSetting painting_control
=
180 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL
;
181 switch (static_cast<RecordingSource::RecordingMode
>(mode_index
)) {
182 case RecordingSource::RECORD_NORMALLY
:
183 // Already setup for normal recording.
185 case RecordingSource::RECORD_WITH_SK_NULL_CANVAS
:
186 // TODO(schenney): Remove this when DisplayList recording is the only
187 // option. For now, fall through and disable construction.
188 case RecordingSource::RECORD_WITH_PAINTING_DISABLED
:
190 ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED
;
192 case RecordingSource::RECORD_WITH_CACHING_DISABLED
:
193 painting_control
= ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED
;
198 base::TimeDelta min_time
= base::TimeDelta::Max();
199 size_t memory_used
= 0;
201 scoped_refptr
<DisplayItemList
> display_list
;
202 for (int i
= 0; i
< record_repeat_count_
; ++i
) {
203 // Run for a minimum amount of time to avoid problems with timer
204 // quantization when the layer is very small.
205 LapTimer
timer(kWarmupRuns
,
206 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
210 display_list
= painter
->PaintContentsToDisplayList(visible_content_rect
,
214 // Verify we are recording the same thing each time.
215 DCHECK(memory_used
== display_list
->PictureMemoryUsage());
217 memory_used
= display_list
->PictureMemoryUsage();
221 } while (!timer
.HasTimeLimitExpired());
222 base::TimeDelta duration
=
223 base::TimeDelta::FromMillisecondsD(timer
.MsPerLap());
224 if (duration
< min_time
)
228 record_results_
.bytes_used
+= memory_used
;
229 record_results_
.pixels_recorded
+=
230 visible_content_rect
.width() * visible_content_rect
.height();
231 record_results_
.total_best_time
[mode_index
] += min_time
;
235 RasterizeAndRecordBenchmark::RecordResults::RecordResults()
236 : pixels_recorded(0), bytes_used(0) {
239 RasterizeAndRecordBenchmark::RecordResults::~RecordResults() {}