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"
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"
35 bool DecodeBitmap(const void* buffer
, size_t size
, SkBitmap
* bm
) {
36 const unsigned char* data
= static_cast<const unsigned char *>(buffer
);
39 if (gfx::PNGCodec::Decode(data
, size
, bm
))
43 scoped_ptr
<SkBitmap
> decoded_jpeg(gfx::JPEGCodec::Decode(data
, size
));
53 scoped_refptr
<Picture
> Picture::Create(
54 const gfx::Rect
& layer_rect
,
55 ContentLayerClient
* client
,
56 const gfx::Size
& tile_grid_size
,
57 bool gather_pixel_refs
,
58 RecordingSource::RecordingMode recording_mode
) {
59 scoped_refptr
<Picture
> picture
=
60 make_scoped_refptr(new Picture(layer_rect
, tile_grid_size
));
62 picture
->Record(client
, recording_mode
);
63 if (gather_pixel_refs
)
64 picture
->GatherPixelRefs();
69 Picture::Picture(const gfx::Rect
& layer_rect
, const gfx::Size
& tile_grid_size
)
70 : layer_rect_(layer_rect
), pixel_refs_(tile_grid_size
) {
71 // Instead of recording a trace event for object creation here, we wait for
72 // the picture to be recorded in Picture::Record.
75 scoped_refptr
<Picture
> Picture::CreateFromSkpValue(const base::Value
* value
) {
76 // Decode the picture from base64.
78 if (!value
->GetAsString(&encoded
))
82 base::Base64Decode(encoded
, &decoded
);
83 SkMemoryStream
stream(decoded
.data(), decoded
.size());
85 // Read the picture. This creates an empty picture on failure.
86 SkPicture
* skpicture
= SkPicture::CreateFromStream(&stream
, &DecodeBitmap
);
87 if (skpicture
== NULL
)
90 gfx::Rect
layer_rect(gfx::SkIRectToRect(skpicture
->cullRect().roundOut()));
91 return make_scoped_refptr(new Picture(skpicture
, layer_rect
));
94 scoped_refptr
<Picture
> Picture::CreateFromValue(const base::Value
* raw_value
) {
95 const base::DictionaryValue
* value
= NULL
;
96 if (!raw_value
->GetAsDictionary(&value
))
99 // Decode the picture from base64.
101 if (!value
->GetString("skp64", &encoded
))
105 base::Base64Decode(encoded
, &decoded
);
106 SkMemoryStream
stream(decoded
.data(), decoded
.size());
108 const base::Value
* layer_rect_value
= NULL
;
109 if (!value
->Get("params.layer_rect", &layer_rect_value
))
112 gfx::Rect layer_rect
;
113 if (!MathUtil::FromValue(layer_rect_value
, &layer_rect
))
116 // Read the picture. This creates an empty picture on failure.
117 SkPicture
* skpicture
= SkPicture::CreateFromStream(&stream
, &DecodeBitmap
);
118 if (skpicture
== NULL
)
121 return make_scoped_refptr(new Picture(skpicture
, layer_rect
));
124 Picture::Picture(SkPicture
* picture
, const gfx::Rect
& layer_rect
)
125 : layer_rect_(layer_rect
),
126 picture_(skia::AdoptRef(picture
)),
127 pixel_refs_(layer_rect
.size()) {
130 Picture::Picture(const skia::RefPtr
<SkPicture
>& picture
,
131 const gfx::Rect
& layer_rect
,
132 const PixelRefMap
& pixel_refs
)
133 : layer_rect_(layer_rect
), picture_(picture
), pixel_refs_(pixel_refs
) {
136 Picture::~Picture() {
137 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
138 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture"), "cc::Picture", this);
141 bool Picture::IsSuitableForGpuRasterization(const char** reason
) const {
144 // TODO(hendrikw): SkPicture::suitableForGpuRasterization takes a GrContext.
145 // Currently the GrContext isn't used, and should probably be removed from
147 return picture_
->suitableForGpuRasterization(nullptr, reason
);
150 int Picture::ApproximateOpCount() const {
152 return picture_
->approximateOpCount();
155 size_t Picture::ApproximateMemoryUsage() const {
157 return SkPictureUtils::ApproximateBytesUsed(picture_
.get());
160 bool Picture::HasText() const {
162 return picture_
->hasText();
165 void Picture::Record(ContentLayerClient
* painter
,
166 RecordingSource::RecordingMode recording_mode
) {
170 AsTraceableRecordData(),
176 SkRTreeFactory factory
;
177 SkPictureRecorder recorder
;
179 skia::RefPtr
<SkCanvas
> canvas
;
180 canvas
= skia::SharePtr(recorder
.beginRecording(
181 layer_rect_
.width(), layer_rect_
.height(), &factory
,
182 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag
));
184 ContentLayerClient::PaintingControlSetting painting_control
=
185 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL
;
187 switch (recording_mode
) {
188 case RecordingSource::RECORD_NORMALLY
:
189 // Already setup for normal recording.
191 case RecordingSource::RECORD_WITH_SK_NULL_CANVAS
:
192 canvas
= skia::AdoptRef(SkCreateNullCanvas());
194 case RecordingSource::RECORD_WITH_PAINTING_DISABLED
:
195 // We pass a disable flag through the paint calls when perfromance
196 // testing (the only time this case should ever arise) when we want to
197 // prevent the Blink GraphicsContext object from consuming any compute
199 canvas
= skia::AdoptRef(SkCreateNullCanvas());
200 painting_control
= ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED
;
202 case RecordingSource::RECORD_WITH_CACHING_DISABLED
:
203 // This mode should give the same results as RECORD_NORMALLY.
204 painting_control
= ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED
;
207 // case RecordingSource::RECORD_WITH_CONSTRUCTION_DISABLED should
213 canvas
->translate(SkFloatToScalar(-layer_rect_
.x()),
214 SkFloatToScalar(-layer_rect_
.y()));
216 canvas
->clipRect(gfx::RectToSkRect(layer_rect_
));
218 painter
->PaintContents(canvas
.get(), layer_rect_
, painting_control
);
221 picture_
= skia::AdoptRef(recorder
.endRecordingAsPicture());
227 void Picture::GatherPixelRefs() {
228 TRACE_EVENT2("cc", "Picture::GatherPixelRefs",
229 "width", layer_rect_
.width(),
230 "height", layer_rect_
.height());
233 DCHECK(pixel_refs_
.empty());
234 if (!WillPlayBackBitmaps())
237 pixel_refs_
.GatherPixelRefsFromPicture(picture_
.get());
240 int Picture::Raster(SkCanvas
* canvas
,
241 SkPicture::AbortCallback
* callback
,
242 const Region
& negated_content_region
,
243 float contents_scale
) const {
248 AsTraceableRasterData(contents_scale
));
254 for (Region::Iterator
it(negated_content_region
); it
.has_rect(); it
.next())
255 canvas
->clipRect(gfx::RectToSkRect(it
.rect()), SkRegion::kDifference_Op
);
257 canvas
->scale(contents_scale
, contents_scale
);
258 canvas
->translate(layer_rect_
.x(), layer_rect_
.y());
260 // If we have a callback, we need to call |draw()|, |drawPicture()| doesn't
261 // take a callback. This is used by |AnalysisCanvas| to early out.
262 picture_
->playback(canvas
, callback
);
264 // Prefer to call |drawPicture()| on the canvas since it could place the
265 // entire picture on the canvas instead of parsing the skia operations.
266 canvas
->drawPicture(picture_
.get());
269 canvas
->getClipDeviceBounds(&bounds
);
272 "cc", "Picture::Raster",
273 "num_pixels_rasterized", bounds
.width() * bounds
.height());
274 return bounds
.width() * bounds
.height();
277 void Picture::Replay(SkCanvas
* canvas
, SkPicture::AbortCallback
* callback
) {
278 TRACE_EVENT_BEGIN0("cc", "Picture::Replay");
280 picture_
->playback(canvas
, callback
);
282 canvas
->getClipDeviceBounds(&bounds
);
283 TRACE_EVENT_END1("cc", "Picture::Replay",
284 "num_pixels_replayed", bounds
.width() * bounds
.height());
287 scoped_ptr
<base::Value
> Picture::AsValue() const {
288 // Encode the picture as base64.
289 scoped_ptr
<base::DictionaryValue
> res(new base::DictionaryValue());
290 res
->Set("params.layer_rect", MathUtil::AsValue(layer_rect_
).release());
291 std::string b64_picture
;
292 PictureDebugUtil::SerializeAsBase64(picture_
.get(), &b64_picture
);
293 res
->SetString("skp64", b64_picture
);
297 void Picture::EmitTraceSnapshot() const {
298 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
299 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
300 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
303 TracedPicture::AsTraceablePicture(this));
306 void Picture::EmitTraceSnapshotAlias(Picture
* original
) const {
307 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
308 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
309 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
312 TracedPicture::AsTraceablePictureAlias(original
));
315 PixelRefMap::Iterator
Picture::GetPixelRefMapIterator(
316 const gfx::Rect
& layer_rect
) const {
317 return PixelRefMap::Iterator(layer_rect
, this);
320 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
321 Picture::AsTraceableRasterData(float scale
) const {
322 scoped_refptr
<base::trace_event::TracedValue
> raster_data
=
323 new base::trace_event::TracedValue();
324 TracedValue::SetIDRef(this, raster_data
.get(), "picture_id");
325 raster_data
->SetDouble("scale", scale
);
329 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
330 Picture::AsTraceableRecordData() const {
331 scoped_refptr
<base::trace_event::TracedValue
> record_data
=
332 new base::trace_event::TracedValue();
333 TracedValue::SetIDRef(this, record_data
.get(), "picture_id");
334 MathUtil::AddToTracedValue("layer_rect", layer_rect_
, record_data
.get());