webapps: fix theme color on status bar for Galaxy devices
[chromium-blink-merge.git] / cc / playback / picture.cc
blob102e0762b42d225c0aa4b7de5fac10589ab00eb1
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/discardable_image_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_discardable_images,
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_discardable_images)
68 picture->GatherDiscardableImages();
70 return picture;
73 Picture::Picture(const gfx::Rect& layer_rect, const gfx::Size& tile_grid_size)
74 : layer_rect_(layer_rect), images_(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 images_(layer_rect.size()) {}
133 Picture::Picture(const skia::RefPtr<SkPicture>& picture,
134 const gfx::Rect& layer_rect,
135 const DiscardableImageMap& images)
136 : layer_rect_(layer_rect), picture_(picture), images_(images) {}
138 Picture::~Picture() {
139 TRACE_EVENT_OBJECT_DELETED_WITH_ID(
140 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture"), "cc::Picture", this);
143 bool Picture::IsSuitableForGpuRasterization(const char** reason) const {
144 DCHECK(picture_);
146 // TODO(hendrikw): SkPicture::suitableForGpuRasterization takes a GrContext.
147 // Currently the GrContext isn't used, and should probably be removed from
148 // skia.
149 return picture_->suitableForGpuRasterization(nullptr, reason);
152 int Picture::ApproximateOpCount() const {
153 DCHECK(picture_);
154 return picture_->approximateOpCount();
157 size_t Picture::ApproximateMemoryUsage() const {
158 DCHECK(picture_);
159 return SkPictureUtils::ApproximateBytesUsed(picture_.get());
162 bool Picture::ShouldBeAnalyzedForSolidColor() const {
163 return ApproximateOpCount() <= kOpCountThatIsOkToAnalyze;
166 bool Picture::HasText() const {
167 DCHECK(picture_);
168 return picture_->hasText();
171 void Picture::Record(ContentLayerClient* painter,
172 RecordingSource::RecordingMode recording_mode) {
173 TRACE_EVENT2("cc",
174 "Picture::Record",
175 "data",
176 AsTraceableRecordData(),
177 "recording_mode",
178 recording_mode);
180 DCHECK(!picture_);
182 SkRTreeFactory factory;
183 SkPictureRecorder recorder;
185 skia::RefPtr<SkCanvas> canvas;
186 canvas = skia::SharePtr(recorder.beginRecording(
187 layer_rect_.width(), layer_rect_.height(), &factory,
188 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag));
190 ContentLayerClient::PaintingControlSetting painting_control =
191 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL;
193 switch (recording_mode) {
194 case RecordingSource::RECORD_NORMALLY:
195 // Already setup for normal recording.
196 break;
197 case RecordingSource::RECORD_WITH_SK_NULL_CANVAS:
198 canvas = skia::AdoptRef(SkCreateNullCanvas());
199 break;
200 case RecordingSource::RECORD_WITH_PAINTING_DISABLED:
201 // We pass a disable flag through the paint calls when perfromance
202 // testing (the only time this case should ever arise) when we want to
203 // prevent the Blink GraphicsContext object from consuming any compute
204 // time.
205 canvas = skia::AdoptRef(SkCreateNullCanvas());
206 painting_control = ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED;
207 break;
208 case RecordingSource::RECORD_WITH_CACHING_DISABLED:
209 // This mode should give the same results as RECORD_NORMALLY.
210 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED;
211 break;
212 default:
213 // case RecordingSource::RECORD_WITH_CONSTRUCTION_DISABLED should
214 // not be reached
215 NOTREACHED();
218 canvas->save();
219 canvas->translate(SkFloatToScalar(-layer_rect_.x()),
220 SkFloatToScalar(-layer_rect_.y()));
222 canvas->clipRect(gfx::RectToSkRect(layer_rect_));
224 painter->PaintContents(canvas.get(), layer_rect_, painting_control);
226 canvas->restore();
227 picture_ = skia::AdoptRef(recorder.endRecordingAsPicture());
228 DCHECK(picture_);
230 EmitTraceSnapshot();
233 void Picture::GatherDiscardableImages() {
234 TRACE_EVENT2("cc", "Picture::GatherDiscardableImages", "width",
235 layer_rect_.width(), "height", layer_rect_.height());
237 DCHECK(picture_);
238 DCHECK(images_.empty());
239 if (!WillPlayBackBitmaps())
240 return;
242 images_.GatherImagesFromPicture(picture_.get(), layer_rect_);
245 int Picture::Raster(SkCanvas* canvas,
246 SkPicture::AbortCallback* callback,
247 const Region& negated_content_region,
248 float contents_scale) const {
249 TRACE_EVENT_BEGIN1(
250 "cc",
251 "Picture::Raster",
252 "data",
253 AsTraceableRasterData(contents_scale));
255 DCHECK(picture_);
257 canvas->save();
259 for (Region::Iterator it(negated_content_region); it.has_rect(); it.next())
260 canvas->clipRect(gfx::RectToSkRect(it.rect()), SkRegion::kDifference_Op);
262 canvas->scale(contents_scale, contents_scale);
263 canvas->translate(layer_rect_.x(), layer_rect_.y());
264 if (callback) {
265 // If we have a callback, we need to call |draw()|, |drawPicture()| doesn't
266 // take a callback. This is used by |AnalysisCanvas| to early out.
267 picture_->playback(canvas, callback);
268 } else {
269 // Prefer to call |drawPicture()| on the canvas since it could place the
270 // entire picture on the canvas instead of parsing the skia operations.
271 canvas->drawPicture(picture_.get());
273 SkIRect bounds;
274 canvas->getClipDeviceBounds(&bounds);
275 canvas->restore();
276 TRACE_EVENT_END1("cc", "Picture::Raster", "num_pixels_rasterized",
277 bounds.width() * bounds.height());
278 return bounds.width() * bounds.height();
281 void Picture::Replay(SkCanvas* canvas, SkPicture::AbortCallback* callback) {
282 TRACE_EVENT_BEGIN0("cc", "Picture::Replay");
283 DCHECK(picture_);
284 picture_->playback(canvas, callback);
285 SkIRect bounds;
286 canvas->getClipDeviceBounds(&bounds);
287 TRACE_EVENT_END1("cc", "Picture::Replay", "num_pixels_replayed",
288 bounds.width() * bounds.height());
291 scoped_ptr<base::Value> Picture::AsValue() const {
292 // Encode the picture as base64.
293 scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue());
294 res->Set("params.layer_rect", MathUtil::AsValue(layer_rect_).release());
295 std::string b64_picture;
296 PictureDebugUtil::SerializeAsBase64(picture_.get(), &b64_picture);
297 res->SetString("skp64", b64_picture);
298 return res.Pass();
301 void Picture::EmitTraceSnapshot() const {
302 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
303 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
304 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
305 "cc::Picture",
306 this,
307 TracedPicture::AsTraceablePicture(this));
310 void Picture::EmitTraceSnapshotAlias(Picture* original) const {
311 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(
312 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") ","
313 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"),
314 "cc::Picture",
315 this,
316 TracedPicture::AsTraceablePictureAlias(original));
319 DiscardableImageMap::Iterator Picture::GetDiscardableImageMapIterator(
320 const gfx::Rect& layer_rect) const {
321 return DiscardableImageMap::Iterator(layer_rect, this);
324 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
325 Picture::AsTraceableRasterData(float scale) const {
326 scoped_refptr<base::trace_event::TracedValue> raster_data =
327 new base::trace_event::TracedValue();
328 TracedValue::SetIDRef(this, raster_data.get(), "picture_id");
329 raster_data->SetDouble("scale", scale);
330 return raster_data;
333 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
334 Picture::AsTraceableRecordData() const {
335 scoped_refptr<base::trace_event::TracedValue> record_data =
336 new base::trace_event::TracedValue();
337 TracedValue::SetIDRef(this, record_data.get(), "picture_id");
338 MathUtil::AddToTracedValue("layer_rect", layer_rect_, record_data.get());
339 return record_data;
342 } // namespace cc