disable two ClientCertStoreChromeOSTest.* unit_tests on Valgrind bots
[chromium-blink-merge.git] / cc / playback / picture.cc
blobf2429accc81f711fa3cbd91797ebbe6ebf97a485
1 // Copyright 2012 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/playback/picture.h"
7 #include <set>
8 #include <string>
10 #include "base/base64.h"
11 #include "base/trace_event/trace_event.h"
12 #include "base/trace_event/trace_event_argument.h"
13 #include "base/values.h"
14 #include "cc/base/math_util.h"
15 #include "cc/debug/picture_debug_util.h"
16 #include "cc/debug/traced_picture.h"
17 #include "cc/debug/traced_value.h"
18 #include "cc/layers/content_layer_client.h"
19 #include "skia/ext/pixel_ref_utils.h"
20 #include "third_party/skia/include/core/SkCanvas.h"
21 #include "third_party/skia/include/core/SkPaint.h"
22 #include "third_party/skia/include/core/SkPictureRecorder.h"
23 #include "third_party/skia/include/core/SkStream.h"
24 #include "third_party/skia/include/utils/SkNullCanvas.h"
25 #include "third_party/skia/include/utils/SkPictureUtils.h"
26 #include "ui/gfx/codec/jpeg_codec.h"
27 #include "ui/gfx/codec/png_codec.h"
28 #include "ui/gfx/geometry/rect_conversions.h"
29 #include "ui/gfx/skia_util.h"
31 namespace cc {
33 namespace {
35 // We don't perform per-layer solid color analysis when there are too many skia
36 // operations.
37 const int kOpCountThatIsOkToAnalyze = 10;
39 bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) {
40 const unsigned char* data = static_cast<const unsigned char *>(buffer);
42 // Try PNG first.
43 if (gfx::PNGCodec::Decode(data, size, bm))
44 return true;
46 // Try JPEG.
47 scoped_ptr<SkBitmap> decoded_jpeg(gfx::JPEGCodec::Decode(data, size));
48 if (decoded_jpeg) {
49 *bm = *decoded_jpeg;
50 return true;
52 return false;
55 } // namespace
57 scoped_refptr<Picture> Picture::Create(
58 const gfx::Rect& layer_rect,
59 ContentLayerClient* client,
60 const gfx::Size& tile_grid_size,
61 bool gather_pixel_refs,
62 RecordingSource::RecordingMode recording_mode) {
63 scoped_refptr<Picture> picture =
64 make_scoped_refptr(new Picture(layer_rect, tile_grid_size));
66 picture->Record(client, recording_mode);
67 if (gather_pixel_refs)
68 picture->GatherPixelRefs();
70 return picture;
73 Picture::Picture(const gfx::Rect& layer_rect, const gfx::Size& tile_grid_size)
74 : layer_rect_(layer_rect), pixel_refs_(tile_grid_size) {
75 // Instead of recording a trace event for object creation here, we wait for
76 // the picture to be recorded in Picture::Record.
79 scoped_refptr<Picture> Picture::CreateFromSkpValue(const base::Value* value) {
80 // Decode the picture from base64.
81 std::string encoded;
82 if (!value->GetAsString(&encoded))
83 return NULL;
85 std::string decoded;
86 base::Base64Decode(encoded, &decoded);
87 SkMemoryStream stream(decoded.data(), decoded.size());
89 // Read the picture. This creates an empty picture on failure.
90 SkPicture* skpicture = SkPicture::CreateFromStream(&stream, &DecodeBitmap);
91 if (skpicture == NULL)
92 return NULL;
94 gfx::Rect layer_rect(gfx::SkIRectToRect(skpicture->cullRect().roundOut()));
95 return make_scoped_refptr(new Picture(skpicture, layer_rect));
98 scoped_refptr<Picture> Picture::CreateFromValue(const base::Value* raw_value) {
99 const base::DictionaryValue* value = NULL;
100 if (!raw_value->GetAsDictionary(&value))
101 return NULL;
103 // Decode the picture from base64.
104 std::string encoded;
105 if (!value->GetString("skp64", &encoded))
106 return NULL;
108 std::string decoded;
109 base::Base64Decode(encoded, &decoded);
110 SkMemoryStream stream(decoded.data(), decoded.size());
112 const base::Value* layer_rect_value = NULL;
113 if (!value->Get("params.layer_rect", &layer_rect_value))
114 return NULL;
116 gfx::Rect layer_rect;
117 if (!MathUtil::FromValue(layer_rect_value, &layer_rect))
118 return NULL;
120 // Read the picture. This creates an empty picture on failure.
121 SkPicture* skpicture = SkPicture::CreateFromStream(&stream, &DecodeBitmap);
122 if (skpicture == NULL)
123 return NULL;
125 return make_scoped_refptr(new Picture(skpicture, layer_rect));
128 Picture::Picture(SkPicture* picture, const gfx::Rect& layer_rect)
129 : layer_rect_(layer_rect),
130 picture_(skia::AdoptRef(picture)),
131 pixel_refs_(layer_rect.size()) {
134 Picture::Picture(const skia::RefPtr<SkPicture>& picture,
135 const gfx::Rect& layer_rect,
136 const PixelRefMap& pixel_refs)
137 : layer_rect_(layer_rect), picture_(picture), pixel_refs_(pixel_refs) {
140 Picture::~Picture() {
141 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
142 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture"), "cc::Picture", this);
145 bool Picture::IsSuitableForGpuRasterization(const char** reason) const {
146 DCHECK(picture_);
148 // TODO(hendrikw): SkPicture::suitableForGpuRasterization takes a GrContext.
149 // Currently the GrContext isn't used, and should probably be removed from
150 // skia.
151 return picture_->suitableForGpuRasterization(nullptr, reason);
154 int Picture::ApproximateOpCount() const {
155 DCHECK(picture_);
156 return picture_->approximateOpCount();
159 size_t Picture::ApproximateMemoryUsage() const {
160 DCHECK(picture_);
161 return SkPictureUtils::ApproximateBytesUsed(picture_.get());
164 bool Picture::ShouldBeAnalyzedForSolidColor() const {
165 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze;
168 bool Picture::HasText() const {
169 DCHECK(picture_);
170 return picture_->hasText();
173 void Picture::Record(ContentLayerClient* painter,
174 RecordingSource::RecordingMode recording_mode) {
175 TRACE_EVENT2("cc",
176 "Picture::Record",
177 "data",
178 AsTraceableRecordData(),
179 "recording_mode",
180 recording_mode);
182 DCHECK(!picture_);
184 SkRTreeFactory factory;
185 SkPictureRecorder recorder;
187 skia::RefPtr<SkCanvas> canvas;
188 canvas = skia::SharePtr(recorder.beginRecording(
189 layer_rect_.width(), layer_rect_.height(), &factory,
190 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag));
192 ContentLayerClient::PaintingControlSetting painting_control =
193 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL;
195 switch (recording_mode) {
196 case RecordingSource::RECORD_NORMALLY:
197 // Already setup for normal recording.
198 break;
199 case RecordingSource::RECORD_WITH_SK_NULL_CANVAS:
200 canvas = skia::AdoptRef(SkCreateNullCanvas());
201 break;
202 case RecordingSource::RECORD_WITH_PAINTING_DISABLED:
203 // We pass a disable flag through the paint calls when perfromance
204 // testing (the only time this case should ever arise) when we want to
205 // prevent the Blink GraphicsContext object from consuming any compute
206 // time.
207 canvas = skia::AdoptRef(SkCreateNullCanvas());
208 painting_control = ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED;
209 break;
210 case RecordingSource::RECORD_WITH_CACHING_DISABLED:
211 // This mode should give the same results as RECORD_NORMALLY.
212 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED;
213 break;
214 default:
215 // case RecordingSource::RECORD_WITH_CONSTRUCTION_DISABLED should
216 // not be reached
217 NOTREACHED();
220 canvas->save();
221 canvas->translate(SkFloatToScalar(-layer_rect_.x()),
222 SkFloatToScalar(-layer_rect_.y()));
224 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
226 painter->PaintContents(canvas.get(), layer_rect_, painting_control);
228 canvas->restore();
229 picture_ = skia::AdoptRef(recorder.endRecordingAsPicture());
230 DCHECK(picture_);
232 EmitTraceSnapshot();
235 void Picture::GatherPixelRefs() {
236 TRACE_EVENT2("cc", "Picture::GatherPixelRefs",
237 "width", layer_rect_.width(),
238 "height", layer_rect_.height());
240 DCHECK(picture_);
241 DCHECK(pixel_refs_.empty());
242 if (!WillPlayBackBitmaps())
243 return;
245 pixel_refs_.GatherPixelRefsFromPicture(picture_.get(), layer_rect_);
248 int Picture::Raster(SkCanvas* canvas,
249 SkPicture::AbortCallback* callback,
250 const Region& negated_content_region,
251 float contents_scale) const {
252 TRACE_EVENT_BEGIN1(
253 "cc",
254 "Picture::Raster",
255 "data",
256 AsTraceableRasterData(contents_scale));
258 DCHECK(picture_);
260 canvas->save();
262 for (Region::Iterator it(negated_content_region); it.has_rect(); it.next())
263 canvas->clipRect(gfx::RectToSkRect(it.rect()), SkRegion::kDifference_Op);
265 canvas->scale(contents_scale, contents_scale);
266 canvas->translate(layer_rect_.x(), layer_rect_.y());
267 if (callback) {
268 // If we have a callback, we need to call |draw()|, |drawPicture()| doesn't
269 // take a callback. This is used by |AnalysisCanvas| to early out.
270 picture_->playback(canvas, callback);
271 } else {
272 // Prefer to call |drawPicture()| on the canvas since it could place the
273 // entire picture on the canvas instead of parsing the skia operations.
274 canvas->drawPicture(picture_.get());
276 SkIRect bounds;
277 canvas->getClipDeviceBounds(&bounds);
278 canvas->restore();
279 TRACE_EVENT_END1(
280 "cc", "Picture::Raster",
281 "num_pixels_rasterized", bounds.width() * bounds.height());
282 return bounds.width() * bounds.height();
285 void Picture::Replay(SkCanvas* canvas, SkPicture::AbortCallback* callback) {
286 TRACE_EVENT_BEGIN0("cc", "Picture::Replay");
287 DCHECK(picture_);
288 picture_->playback(canvas, callback);
289 SkIRect bounds;
290 canvas->getClipDeviceBounds(&bounds);
291 TRACE_EVENT_END1("cc", "Picture::Replay",
292 "num_pixels_replayed", bounds.width() * bounds.height());
295 scoped_ptr<base::Value> Picture::AsValue() const {
296 // Encode the picture as base64.
297 scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue());
298 res->Set("params.layer_rect", MathUtil::AsValue(layer_rect_).release());
299 std::string b64_picture;
300 PictureDebugUtil::SerializeAsBase64(picture_.get(), &b64_picture);
301 res->SetString("skp64", b64_picture);
302 return res.Pass();
305 void Picture::EmitTraceSnapshot() const {
306 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
307 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
308 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
309 "cc::Picture",
310 this,
311 TracedPicture::AsTraceablePicture(this));
314 void Picture::EmitTraceSnapshotAlias(Picture* original) const {
315 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
316 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
317 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
318 "cc::Picture",
319 this,
320 TracedPicture::AsTraceablePictureAlias(original));
323 PixelRefMap::Iterator Picture::GetPixelRefMapIterator(
324 const gfx::Rect& layer_rect) const {
325 return PixelRefMap::Iterator(layer_rect, this);
328 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
329 Picture::AsTraceableRasterData(float scale) const {
330 scoped_refptr<base::trace_event::TracedValue> raster_data =
331 new base::trace_event::TracedValue();
332 TracedValue::SetIDRef(this, raster_data.get(), "picture_id");
333 raster_data->SetDouble("scale", scale);
334 return raster_data;
337 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
338 Picture::AsTraceableRecordData() const {
339 scoped_refptr<base::trace_event::TracedValue> record_data =
340 new base::trace_event::TracedValue();
341 TracedValue::SetIDRef(this, record_data.get(), "picture_id");
342 MathUtil::AddToTracedValue("layer_rect", layer_rect_, record_data.get());
343 return record_data;
346 } // namespace cc