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 // We don't perform per-layer solid color analysis when there are too many skia
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
);
43 if (gfx::PNGCodec::Decode(data
, size
, bm
))
47 scoped_ptr
<SkBitmap
> decoded_jpeg(gfx::JPEGCodec::Decode(data
, size
));
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();
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.
82 if (!value
->GetAsString(&encoded
))
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
)
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
))
103 // Decode the picture from base64.
105 if (!value
->GetString("skp64", &encoded
))
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
))
116 gfx::Rect layer_rect
;
117 if (!MathUtil::FromValue(layer_rect_value
, &layer_rect
))
120 // Read the picture. This creates an empty picture on failure.
121 SkPicture
* skpicture
= SkPicture::CreateFromStream(&stream
, &DecodeBitmap
);
122 if (skpicture
== 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 {
148 // TODO(hendrikw): SkPicture::suitableForGpuRasterization takes a GrContext.
149 // Currently the GrContext isn't used, and should probably be removed from
151 return picture_
->suitableForGpuRasterization(nullptr, reason
);
154 int Picture::ApproximateOpCount() const {
156 return picture_
->approximateOpCount();
159 size_t Picture::ApproximateMemoryUsage() const {
161 return SkPictureUtils::ApproximateBytesUsed(picture_
.get());
164 bool Picture::ShouldBeAnalyzedForSolidColor() const {
165 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze
;
168 bool Picture::HasText() const {
170 return picture_
->hasText();
173 void Picture::Record(ContentLayerClient
* painter
,
174 RecordingSource::RecordingMode recording_mode
) {
178 AsTraceableRecordData(),
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.
199 case RecordingSource::RECORD_WITH_SK_NULL_CANVAS
:
200 canvas
= skia::AdoptRef(SkCreateNullCanvas());
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
207 canvas
= skia::AdoptRef(SkCreateNullCanvas());
208 painting_control
= ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED
;
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
;
215 // case RecordingSource::RECORD_WITH_CONSTRUCTION_DISABLED should
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
);
229 picture_
= skia::AdoptRef(recorder
.endRecordingAsPicture());
235 void Picture::GatherPixelRefs() {
236 TRACE_EVENT2("cc", "Picture::GatherPixelRefs",
237 "width", layer_rect_
.width(),
238 "height", layer_rect_
.height());
241 DCHECK(pixel_refs_
.empty());
242 if (!WillPlayBackBitmaps())
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 {
256 AsTraceableRasterData(contents_scale
));
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());
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
);
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());
277 canvas
->getClipDeviceBounds(&bounds
);
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");
288 picture_
->playback(canvas
, callback
);
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
);
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"),
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"),
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
);
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());