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
] = {
42 "_construction_disabled"};
46 RasterizeAndRecordBenchmark::RasterizeAndRecordBenchmark(
47 scoped_ptr
<base::Value
> value
,
48 const MicroBenchmark::DoneCallback
& callback
)
49 : MicroBenchmark(callback
),
50 record_repeat_count_(kDefaultRecordRepeatCount
),
51 settings_(value
.Pass()),
52 main_thread_benchmark_done_(false),
54 weak_ptr_factory_(this) {
55 base::DictionaryValue
* settings
= nullptr;
56 settings_
->GetAsDictionary(&settings
);
60 if (settings
->HasKey("record_repeat_count"))
61 settings
->GetInteger("record_repeat_count", &record_repeat_count_
);
64 RasterizeAndRecordBenchmark::~RasterizeAndRecordBenchmark() {
65 weak_ptr_factory_
.InvalidateWeakPtrs();
68 void RasterizeAndRecordBenchmark::DidUpdateLayers(LayerTreeHost
* host
) {
70 LayerTreeHostCommon::CallFunctionForSubtree(
72 [this](Layer
* layer
) { layer
->RunMicroBenchmark(this); });
74 DCHECK(!results_
.get());
75 results_
= make_scoped_ptr(new base::DictionaryValue
);
76 results_
->SetInteger("pixels_recorded", record_results_
.pixels_recorded
);
77 results_
->SetInteger("picture_memory_usage", record_results_
.bytes_used
);
79 for (int i
= 0; i
< RecordingSource::RECORDING_MODE_COUNT
; i
++) {
80 std::string name
= base::StringPrintf("record_time%s_ms", kModeSuffixes
[i
]);
81 results_
->SetDouble(name
,
82 record_results_
.total_best_time
[i
].InMillisecondsF());
84 main_thread_benchmark_done_
= true;
87 void RasterizeAndRecordBenchmark::RecordRasterResults(
88 scoped_ptr
<base::Value
> results_value
) {
89 DCHECK(main_thread_benchmark_done_
);
91 base::DictionaryValue
* results
= nullptr;
92 results_value
->GetAsDictionary(&results
);
95 results_
->MergeDictionary(results
);
97 NotifyDone(results_
.Pass());
100 scoped_ptr
<MicroBenchmarkImpl
> RasterizeAndRecordBenchmark::CreateBenchmarkImpl(
101 scoped_refptr
<base::SingleThreadTaskRunner
> origin_task_runner
) {
102 return make_scoped_ptr(new RasterizeAndRecordBenchmarkImpl(
103 origin_task_runner
, settings_
.get(),
104 base::Bind(&RasterizeAndRecordBenchmark::RecordRasterResults
,
105 weak_ptr_factory_
.GetWeakPtr())));
108 void RasterizeAndRecordBenchmark::RunOnLayer(PictureLayer
* layer
) {
111 gfx::Rect visible_layer_rect
= gfx::ScaleToEnclosingRect(
112 layer
->visible_content_rect(), 1.f
/ layer
->contents_scale_x());
113 if (visible_layer_rect
.IsEmpty())
116 if (host_
->settings().use_display_lists
) {
117 RunOnDisplayListLayer(layer
, visible_layer_rect
);
119 RunOnPictureLayer(layer
, visible_layer_rect
);
123 void RasterizeAndRecordBenchmark::RunOnPictureLayer(
125 const gfx::Rect
& visible_layer_rect
) {
126 ContentLayerClient
* painter
= layer
->client();
128 DCHECK(host_
&& !host_
->settings().use_display_lists
);
130 gfx::Size tile_grid_size
= host_
->settings().default_tile_size
;
132 for (int mode_index
= 0; mode_index
< RecordingSource::RECORDING_MODE_COUNT
;
134 RecordingSource::RecordingMode mode
=
135 static_cast<RecordingSource::RecordingMode
>(mode_index
);
137 // Not supported for SkPicture recording.
138 if (mode
== RecordingSource::RECORD_WITH_CONSTRUCTION_DISABLED
)
141 base::TimeDelta min_time
= base::TimeDelta::Max();
142 size_t memory_used
= 0;
144 for (int i
= 0; i
< record_repeat_count_
; ++i
) {
145 // Run for a minimum amount of time to avoid problems with timer
146 // quantization when the layer is very small.
147 LapTimer
timer(kWarmupRuns
,
148 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
150 scoped_refptr
<Picture
> picture
;
152 picture
= Picture::Create(visible_layer_rect
, painter
, tile_grid_size
,
155 // Verify we are recording the same thing each time.
156 DCHECK(memory_used
== picture
->ApproximateMemoryUsage());
158 memory_used
= picture
->ApproximateMemoryUsage();
162 } while (!timer
.HasTimeLimitExpired());
163 base::TimeDelta duration
=
164 base::TimeDelta::FromMillisecondsD(timer
.MsPerLap());
165 if (duration
< min_time
)
169 if (mode
== RecordingSource::RECORD_NORMALLY
) {
170 record_results_
.bytes_used
+= memory_used
;
171 record_results_
.pixels_recorded
+=
172 visible_layer_rect
.width() * visible_layer_rect
.height();
174 record_results_
.total_best_time
[mode_index
] += min_time
;
178 void RasterizeAndRecordBenchmark::RunOnDisplayListLayer(
180 const gfx::Rect
& visible_layer_rect
) {
181 ContentLayerClient
* painter
= layer
->client();
183 DCHECK(host_
&& host_
->settings().use_display_lists
);
185 for (int mode_index
= 0; mode_index
< RecordingSource::RECORDING_MODE_COUNT
;
187 ContentLayerClient::PaintingControlSetting painting_control
=
188 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL
;
189 switch (static_cast<RecordingSource::RecordingMode
>(mode_index
)) {
190 case RecordingSource::RECORD_NORMALLY
:
191 // Already setup for normal recording.
193 case RecordingSource::RECORD_WITH_SK_NULL_CANVAS
:
194 // Not supported for Display List recording.
196 case RecordingSource::RECORD_WITH_PAINTING_DISABLED
:
197 painting_control
= ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED
;
199 case RecordingSource::RECORD_WITH_CACHING_DISABLED
:
200 painting_control
= ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED
;
202 case RecordingSource::RECORD_WITH_CONSTRUCTION_DISABLED
:
204 ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED
;
209 base::TimeDelta min_time
= base::TimeDelta::Max();
210 size_t memory_used
= 0;
212 scoped_refptr
<DisplayItemList
> display_list
;
213 for (int i
= 0; i
< record_repeat_count_
; ++i
) {
214 // Run for a minimum amount of time to avoid problems with timer
215 // quantization when the layer is very small.
216 LapTimer
timer(kWarmupRuns
,
217 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
221 const bool use_cached_picture
= true;
223 DisplayItemList::Create(visible_layer_rect
, use_cached_picture
);
224 painter
->PaintContentsToDisplayList(
225 display_list
.get(), visible_layer_rect
, painting_control
);
226 display_list
->CreateAndCacheSkPicture();
229 // Verify we are recording the same thing each time.
230 DCHECK(memory_used
== display_list
->PictureMemoryUsage());
232 memory_used
= display_list
->PictureMemoryUsage();
236 } while (!timer
.HasTimeLimitExpired());
237 base::TimeDelta duration
=
238 base::TimeDelta::FromMillisecondsD(timer
.MsPerLap());
239 if (duration
< min_time
)
243 if (mode_index
== RecordingSource::RECORD_NORMALLY
) {
244 record_results_
.bytes_used
+= memory_used
;
245 record_results_
.pixels_recorded
+=
246 visible_layer_rect
.width() * visible_layer_rect
.height();
248 record_results_
.total_best_time
[mode_index
] += min_time
;
252 RasterizeAndRecordBenchmark::RecordResults::RecordResults()
253 : pixels_recorded(0), bytes_used(0) {
256 RasterizeAndRecordBenchmark::RecordResults::~RecordResults() {}