1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/debug/rasterize_and_record_benchmark.h"
#include <algorithm>
#include <limits>
#include <string>
#include "base/basictypes.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "cc/debug/lap_timer.h"
#include "cc/debug/rasterize_and_record_benchmark_impl.h"
#include "cc/layers/content_layer_client.h"
#include "cc/layers/layer.h"
#include "cc/layers/picture_layer.h"
#include "cc/resources/display_item_list.h"
#include "cc/resources/picture_pile.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/layer_tree_host_common.h"
#include "third_party/skia/include/utils/SkPictureUtils.h"
#include "ui/gfx/geometry/rect.h"
namespace cc {
namespace {
const int kDefaultRecordRepeatCount = 100;
// Parameters for LapTimer.
const int kTimeLimitMillis = 1;
const int kWarmupRuns = 0;
const int kTimeCheckInterval = 1;
const char* kModeSuffixes[RecordingSource::RECORDING_MODE_COUNT] =
{"", "_sk_null_canvas", "_painting_disabled", "_caching_disabled"};
} // namespace
RasterizeAndRecordBenchmark::RasterizeAndRecordBenchmark(
scoped_ptr<base::Value> value,
const MicroBenchmark::DoneCallback& callback)
: MicroBenchmark(callback),
record_repeat_count_(kDefaultRecordRepeatCount),
settings_(value.Pass()),
main_thread_benchmark_done_(false),
host_(nullptr),
weak_ptr_factory_(this) {
base::DictionaryValue* settings = nullptr;
settings_->GetAsDictionary(&settings);
if (!settings)
return;
if (settings->HasKey("record_repeat_count"))
settings->GetInteger("record_repeat_count", &record_repeat_count_);
}
RasterizeAndRecordBenchmark::~RasterizeAndRecordBenchmark() {
weak_ptr_factory_.InvalidateWeakPtrs();
}
void RasterizeAndRecordBenchmark::DidUpdateLayers(LayerTreeHost* host) {
host_ = host;
LayerTreeHostCommon::CallFunctionForSubtree(
host->root_layer(),
base::Bind(&RasterizeAndRecordBenchmark::Run, base::Unretained(this)));
DCHECK(!results_.get());
results_ = make_scoped_ptr(new base::DictionaryValue);
results_->SetInteger("pixels_recorded", record_results_.pixels_recorded);
results_->SetInteger("picture_memory_usage", record_results_.bytes_used);
for (int i = 0; i < RecordingSource::RECORDING_MODE_COUNT; i++) {
std::string name = base::StringPrintf("record_time%s_ms", kModeSuffixes[i]);
results_->SetDouble(name,
record_results_.total_best_time[i].InMillisecondsF());
}
main_thread_benchmark_done_ = true;
}
void RasterizeAndRecordBenchmark::RecordRasterResults(
scoped_ptr<base::Value> results_value) {
DCHECK(main_thread_benchmark_done_);
base::DictionaryValue* results = nullptr;
results_value->GetAsDictionary(&results);
DCHECK(results);
results_->MergeDictionary(results);
NotifyDone(results_.Pass());
}
scoped_ptr<MicroBenchmarkImpl> RasterizeAndRecordBenchmark::CreateBenchmarkImpl(
scoped_refptr<base::MessageLoopProxy> origin_loop) {
return make_scoped_ptr(new RasterizeAndRecordBenchmarkImpl(
origin_loop,
settings_.get(),
base::Bind(&RasterizeAndRecordBenchmark::RecordRasterResults,
weak_ptr_factory_.GetWeakPtr())));
}
void RasterizeAndRecordBenchmark::Run(Layer* layer) {
layer->RunMicroBenchmark(this);
}
void RasterizeAndRecordBenchmark::RunOnLayer(PictureLayer* layer) {
DCHECK(host_);
gfx::Rect visible_content_rect = gfx::ScaleToEnclosingRect(
layer->visible_content_rect(), 1.f / layer->contents_scale_x());
if (visible_content_rect.IsEmpty())
return;
if (host_->settings().use_display_lists) {
RunOnDisplayListLayer(layer, visible_content_rect);
} else {
RunOnPictureLayer(layer, visible_content_rect);
}
}
void RasterizeAndRecordBenchmark::RunOnPictureLayer(
PictureLayer* layer,
const gfx::Rect& visible_content_rect) {
ContentLayerClient* painter = layer->client();
DCHECK(host_ && !host_->settings().use_display_lists);
gfx::Size tile_grid_size = host_->settings().default_tile_size;
for (int mode_index = 0; mode_index < RecordingSource::RECORDING_MODE_COUNT;
mode_index++) {
RecordingSource::RecordingMode mode =
static_cast<RecordingSource::RecordingMode>(mode_index);
base::TimeDelta min_time = base::TimeDelta::Max();
size_t memory_used = 0;
for (int i = 0; i < record_repeat_count_; ++i) {
// Run for a minimum amount of time to avoid problems with timer
// quantization when the layer is very small.
LapTimer timer(kWarmupRuns,
base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
kTimeCheckInterval);
scoped_refptr<Picture> picture;
do {
picture = Picture::Create(visible_content_rect, painter, tile_grid_size,
false, mode);
if (memory_used) {
// Verify we are recording the same thing each time.
DCHECK(memory_used == picture->ApproximateMemoryUsage());
} else {
memory_used = picture->ApproximateMemoryUsage();
}
timer.NextLap();
} while (!timer.HasTimeLimitExpired());
base::TimeDelta duration =
base::TimeDelta::FromMillisecondsD(timer.MsPerLap());
if (duration < min_time)
min_time = duration;
}
if (mode == RecordingSource::RECORD_NORMALLY) {
record_results_.bytes_used += memory_used;
record_results_.pixels_recorded +=
visible_content_rect.width() * visible_content_rect.height();
}
record_results_.total_best_time[mode_index] += min_time;
}
}
void RasterizeAndRecordBenchmark::RunOnDisplayListLayer(
PictureLayer* layer,
const gfx::Rect& visible_content_rect) {
ContentLayerClient* painter = layer->client();
DCHECK(host_ && host_->settings().use_display_lists);
for (int mode_index = 0; mode_index < RecordingSource::RECORDING_MODE_COUNT;
mode_index++) {
ContentLayerClient::PaintingControlSetting painting_control =
ContentLayerClient::PAINTING_BEHAVIOR_NORMAL;
switch (static_cast<RecordingSource::RecordingMode>(mode_index)) {
case RecordingSource::RECORD_NORMALLY:
// Already setup for normal recording.
break;
case RecordingSource::RECORD_WITH_SK_NULL_CANVAS:
// TODO(schenney): Remove this when DisplayList recording is the only
// option. For now, fall through and disable construction.
case RecordingSource::RECORD_WITH_PAINTING_DISABLED:
painting_control =
ContentLayerClient::DISPLAY_LIST_CONSTRUCTION_DISABLED;
break;
case RecordingSource::RECORD_WITH_CACHING_DISABLED:
painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED;
break;
default:
NOTREACHED();
}
base::TimeDelta min_time = base::TimeDelta::Max();
size_t memory_used = 0;
scoped_refptr<DisplayItemList> display_list;
for (int i = 0; i < record_repeat_count_; ++i) {
// Run for a minimum amount of time to avoid problems with timer
// quantization when the layer is very small.
LapTimer timer(kWarmupRuns,
base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
kTimeCheckInterval);
do {
display_list = painter->PaintContentsToDisplayList(visible_content_rect,
painting_control);
if (memory_used) {
// Verify we are recording the same thing each time.
DCHECK(memory_used == display_list->PictureMemoryUsage());
} else {
memory_used = display_list->PictureMemoryUsage();
}
timer.NextLap();
} while (!timer.HasTimeLimitExpired());
base::TimeDelta duration =
base::TimeDelta::FromMillisecondsD(timer.MsPerLap());
if (duration < min_time)
min_time = duration;
}
record_results_.bytes_used += memory_used;
record_results_.pixels_recorded +=
visible_content_rect.width() * visible_content_rect.height();
record_results_.total_best_time[mode_index] += min_time;
}
}
RasterizeAndRecordBenchmark::RecordResults::RecordResults()
: pixels_recorded(0), bytes_used(0) {
}
RasterizeAndRecordBenchmark::RecordResults::~RecordResults() {}
} // namespace cc
|