Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / cc / playback / picture.cc
blob8ce9c66ff8f89db44bf7967e50e911532382579a
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 bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) {
36 const unsigned char* data = static_cast<const unsigned char *>(buffer);
38 // Try PNG first.
39 if (gfx::PNGCodec::Decode(data, size, bm))
40 return true;
42 // Try JPEG.
43 scoped_ptr<SkBitmap> decoded_jpeg(gfx::JPEGCodec::Decode(data, size));
44 if (decoded_jpeg) {
45 *bm = *decoded_jpeg;
46 return true;
48 return false;
51 } // namespace
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();
66 return picture;
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.
77 std::string encoded;
78 if (!value->GetAsString(&encoded))
79 return NULL;
81 std::string decoded;
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)
88 return 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))
97 return NULL;
99 // Decode the picture from base64.
100 std::string encoded;
101 if (!value->GetString("skp64", &encoded))
102 return NULL;
104 std::string decoded;
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))
110 return NULL;
112 gfx::Rect layer_rect;
113 if (!MathUtil::FromValue(layer_rect_value, &layer_rect))
114 return NULL;
116 // Read the picture. This creates an empty picture on failure.
117 SkPicture* skpicture = SkPicture::CreateFromStream(&stream, &DecodeBitmap);
118 if (skpicture == NULL)
119 return 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 {
142 DCHECK(picture_);
144 // TODO(hendrikw): SkPicture::suitableForGpuRasterization takes a GrContext.
145 // Currently the GrContext isn't used, and should probably be removed from
146 // skia.
147 return picture_->suitableForGpuRasterization(nullptr, reason);
150 int Picture::ApproximateOpCount() const {
151 DCHECK(picture_);
152 return picture_->approximateOpCount();
155 size_t Picture::ApproximateMemoryUsage() const {
156 DCHECK(picture_);
157 return SkPictureUtils::ApproximateBytesUsed(picture_.get());
160 bool Picture::HasText() const {
161 DCHECK(picture_);
162 return picture_->hasText();
165 void Picture::Record(ContentLayerClient* painter,
166 RecordingSource::RecordingMode recording_mode) {
167 TRACE_EVENT2("cc",
168 "Picture::Record",
169 "data",
170 AsTraceableRecordData(),
171 "recording_mode",
172 recording_mode);
174 DCHECK(!picture_);
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.
190 break;
191 case RecordingSource::RECORD_WITH_SK_NULL_CANVAS:
192 canvas = skia::AdoptRef(SkCreateNullCanvas());
193 break;
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
198 // time.
199 canvas = skia::AdoptRef(SkCreateNullCanvas());
200 painting_control = ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED;
201 break;
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;
205 break;
206 default:
207 // case RecordingSource::RECORD_WITH_CONSTRUCTION_DISABLED should
208 // not be reached
209 NOTREACHED();
212 canvas->save();
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);
220 canvas->restore();
221 picture_ = skia::AdoptRef(recorder.endRecordingAsPicture());
222 DCHECK(picture_);
224 EmitTraceSnapshot();
227 void Picture::GatherPixelRefs() {
228 TRACE_EVENT2("cc", "Picture::GatherPixelRefs",
229 "width", layer_rect_.width(),
230 "height", layer_rect_.height());
232 DCHECK(picture_);
233 DCHECK(pixel_refs_.empty());
234 if (!WillPlayBackBitmaps())
235 return;
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 {
244 TRACE_EVENT_BEGIN1(
245 "cc",
246 "Picture::Raster",
247 "data",
248 AsTraceableRasterData(contents_scale));
250 DCHECK(picture_);
252 canvas->save();
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());
259 if (callback) {
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);
263 } else {
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());
268 SkIRect bounds;
269 canvas->getClipDeviceBounds(&bounds);
270 canvas->restore();
271 TRACE_EVENT_END1(
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");
279 DCHECK(picture_);
280 picture_->playback(canvas, callback);
281 SkIRect bounds;
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);
294 return res.Pass();
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"),
301 "cc::Picture",
302 this,
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"),
310 "cc::Picture",
311 this,
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);
326 return raster_data;
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());
335 return record_data;
338 } // namespace cc