OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "cc/resources/picture.h" | |
6 | |
7 #include <set> | |
8 #include <string> | |
9 | |
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/base/util.h" | |
16 #include "cc/debug/picture_debug_util.h" | |
17 #include "cc/debug/traced_picture.h" | |
18 #include "cc/debug/traced_value.h" | |
19 #include "cc/layers/content_layer_client.h" | |
20 #include "skia/ext/pixel_ref_utils.h" | |
21 #include "third_party/skia/include/core/SkCanvas.h" | |
22 #include "third_party/skia/include/core/SkDrawPictureCallback.h" | |
23 #include "third_party/skia/include/core/SkPaint.h" | |
24 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
25 #include "third_party/skia/include/core/SkStream.h" | |
26 #include "third_party/skia/include/utils/SkNullCanvas.h" | |
27 #include "third_party/skia/include/utils/SkPictureUtils.h" | |
28 #include "ui/gfx/codec/jpeg_codec.h" | |
29 #include "ui/gfx/codec/png_codec.h" | |
30 #include "ui/gfx/geometry/rect_conversions.h" | |
31 #include "ui/gfx/skia_util.h" | |
32 | |
33 namespace cc { | |
34 | |
35 namespace { | |
36 | |
37 bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) { | |
38 const unsigned char* data = static_cast<const unsigned char *>(buffer); | |
39 | |
40 // Try PNG first. | |
41 if (gfx::PNGCodec::Decode(data, size, bm)) | |
42 return true; | |
43 | |
44 // Try JPEG. | |
45 scoped_ptr<SkBitmap> decoded_jpeg(gfx::JPEGCodec::Decode(data, size)); | |
46 if (decoded_jpeg) { | |
47 *bm = *decoded_jpeg; | |
48 return true; | |
49 } | |
50 return false; | |
51 } | |
52 | |
53 } // namespace | |
54 | |
55 scoped_refptr<Picture> Picture::Create( | |
56 const gfx::Rect& layer_rect, | |
57 ContentLayerClient* client, | |
58 const gfx::Size& tile_grid_size, | |
59 bool gather_pixel_refs, | |
60 RecordingSource::RecordingMode recording_mode) { | |
61 scoped_refptr<Picture> picture = | |
62 make_scoped_refptr(new Picture(layer_rect, tile_grid_size)); | |
63 | |
64 picture->Record(client, recording_mode); | |
65 if (gather_pixel_refs) | |
66 picture->GatherPixelRefs(); | |
67 | |
68 return picture; | |
69 } | |
70 | |
71 Picture::Picture(const gfx::Rect& layer_rect, const gfx::Size& tile_grid_size) | |
72 : layer_rect_(layer_rect), pixel_refs_(tile_grid_size) { | |
73 // Instead of recording a trace event for object creation here, we wait for | |
74 // the picture to be recorded in Picture::Record. | |
75 } | |
76 | |
77 scoped_refptr<Picture> Picture::CreateFromSkpValue(const base::Value* value) { | |
78 // Decode the picture from base64. | |
79 std::string encoded; | |
80 if (!value->GetAsString(&encoded)) | |
81 return NULL; | |
82 | |
83 std::string decoded; | |
84 base::Base64Decode(encoded, &decoded); | |
85 SkMemoryStream stream(decoded.data(), decoded.size()); | |
86 | |
87 // Read the picture. This creates an empty picture on failure. | |
88 SkPicture* skpicture = SkPicture::CreateFromStream(&stream, &DecodeBitmap); | |
89 if (skpicture == NULL) | |
90 return NULL; | |
91 | |
92 gfx::Rect layer_rect(gfx::SkIRectToRect(skpicture->cullRect().roundOut())); | |
93 return make_scoped_refptr(new Picture(skpicture, layer_rect)); | |
94 } | |
95 | |
96 scoped_refptr<Picture> Picture::CreateFromValue(const base::Value* raw_value) { | |
97 const base::DictionaryValue* value = NULL; | |
98 if (!raw_value->GetAsDictionary(&value)) | |
99 return NULL; | |
100 | |
101 // Decode the picture from base64. | |
102 std::string encoded; | |
103 if (!value->GetString("skp64", &encoded)) | |
104 return NULL; | |
105 | |
106 std::string decoded; | |
107 base::Base64Decode(encoded, &decoded); | |
108 SkMemoryStream stream(decoded.data(), decoded.size()); | |
109 | |
110 const base::Value* layer_rect_value = NULL; | |
111 if (!value->Get("params.layer_rect", &layer_rect_value)) | |
112 return NULL; | |
113 | |
114 gfx::Rect layer_rect; | |
115 if (!MathUtil::FromValue(layer_rect_value, &layer_rect)) | |
116 return NULL; | |
117 | |
118 // Read the picture. This creates an empty picture on failure. | |
119 SkPicture* skpicture = SkPicture::CreateFromStream(&stream, &DecodeBitmap); | |
120 if (skpicture == NULL) | |
121 return NULL; | |
122 | |
123 return make_scoped_refptr(new Picture(skpicture, layer_rect)); | |
124 } | |
125 | |
126 Picture::Picture(SkPicture* picture, const gfx::Rect& layer_rect) | |
127 : layer_rect_(layer_rect), | |
128 picture_(skia::AdoptRef(picture)), | |
129 pixel_refs_(layer_rect.size()) { | |
130 } | |
131 | |
132 Picture::Picture(const skia::RefPtr<SkPicture>& picture, | |
133 const gfx::Rect& layer_rect, | |
134 const PixelRefMap& pixel_refs) | |
135 : layer_rect_(layer_rect), picture_(picture), pixel_refs_(pixel_refs) { | |
136 } | |
137 | |
138 Picture::~Picture() { | |
139 TRACE_EVENT_OBJECT_DELETED_WITH_ID( | |
140 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture"), "cc::Picture", this); | |
141 } | |
142 | |
143 bool Picture::IsSuitableForGpuRasterization(const char** reason) const { | |
144 DCHECK(picture_); | |
145 | |
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); | |
150 } | |
151 | |
152 int Picture::ApproximateOpCount() const { | |
153 DCHECK(picture_); | |
154 return picture_->approximateOpCount(); | |
155 } | |
156 | |
157 size_t Picture::ApproximateMemoryUsage() const { | |
158 DCHECK(picture_); | |
159 return SkPictureUtils::ApproximateBytesUsed(picture_.get()); | |
160 } | |
161 | |
162 bool Picture::HasText() const { | |
163 DCHECK(picture_); | |
164 return picture_->hasText(); | |
165 } | |
166 | |
167 void Picture::Record(ContentLayerClient* painter, | |
168 RecordingSource::RecordingMode recording_mode) { | |
169 TRACE_EVENT2("cc", | |
170 "Picture::Record", | |
171 "data", | |
172 AsTraceableRecordData(), | |
173 "recording_mode", | |
174 recording_mode); | |
175 | |
176 DCHECK(!picture_); | |
177 | |
178 SkRTreeFactory factory; | |
179 SkPictureRecorder recorder; | |
180 | |
181 skia::RefPtr<SkCanvas> canvas; | |
182 canvas = skia::SharePtr(recorder.beginRecording( | |
183 layer_rect_.width(), layer_rect_.height(), &factory, | |
184 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag)); | |
185 | |
186 ContentLayerClient::PaintingControlSetting painting_control = | |
187 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL; | |
188 | |
189 switch (recording_mode) { | |
190 case RecordingSource::RECORD_NORMALLY: | |
191 // Already setup for normal recording. | |
192 break; | |
193 case RecordingSource::RECORD_WITH_SK_NULL_CANVAS: | |
194 canvas = skia::AdoptRef(SkCreateNullCanvas()); | |
195 break; | |
196 case RecordingSource::RECORD_WITH_PAINTING_DISABLED: | |
197 // We pass a disable flag through the paint calls when perfromance | |
198 // testing (the only time this case should ever arise) when we want to | |
199 // prevent the Blink GraphicsContext object from consuming any compute | |
200 // time. | |
201 canvas = skia::AdoptRef(SkCreateNullCanvas()); | |
202 painting_control = ContentLayerClient::DISPLAY_LIST_PAINTING_DISABLED; | |
203 break; | |
204 case RecordingSource::RECORD_WITH_CACHING_DISABLED: | |
205 // This mode should give the same results as RECORD_NORMALLY. | |
206 painting_control = ContentLayerClient::DISPLAY_LIST_CACHING_DISABLED; | |
207 break; | |
208 default: | |
209 // case RecordingSource::RECORD_WITH_CONSTRUCTION_DISABLED should | |
210 // not be reached | |
211 NOTREACHED(); | |
212 } | |
213 | |
214 canvas->save(); | |
215 canvas->translate(SkFloatToScalar(-layer_rect_.x()), | |
216 SkFloatToScalar(-layer_rect_.y())); | |
217 | |
218 canvas->clipRect(gfx::RectToSkRect(layer_rect_)); | |
219 | |
220 painter->PaintContents(canvas.get(), layer_rect_, painting_control); | |
221 | |
222 canvas->restore(); | |
223 picture_ = skia::AdoptRef(recorder.endRecordingAsPicture()); | |
224 DCHECK(picture_); | |
225 | |
226 EmitTraceSnapshot(); | |
227 } | |
228 | |
229 void Picture::GatherPixelRefs() { | |
230 TRACE_EVENT2("cc", "Picture::GatherPixelRefs", | |
231 "width", layer_rect_.width(), | |
232 "height", layer_rect_.height()); | |
233 | |
234 DCHECK(picture_); | |
235 DCHECK(pixel_refs_.empty()); | |
236 if (!WillPlayBackBitmaps()) | |
237 return; | |
238 | |
239 pixel_refs_.GatherPixelRefsFromPicture(picture_.get()); | |
240 } | |
241 | |
242 int Picture::Raster(SkCanvas* canvas, | |
243 SkPicture::AbortCallback* callback, | |
244 const Region& negated_content_region, | |
245 float contents_scale) const { | |
246 TRACE_EVENT_BEGIN1( | |
247 "cc", | |
248 "Picture::Raster", | |
249 "data", | |
250 AsTraceableRasterData(contents_scale)); | |
251 | |
252 DCHECK(picture_); | |
253 | |
254 canvas->save(); | |
255 | |
256 for (Region::Iterator it(negated_content_region); it.has_rect(); it.next()) | |
257 canvas->clipRect(gfx::RectToSkRect(it.rect()), SkRegion::kDifference_Op); | |
258 | |
259 canvas->scale(contents_scale, contents_scale); | |
260 canvas->translate(layer_rect_.x(), layer_rect_.y()); | |
261 if (callback) { | |
262 // If we have a callback, we need to call |draw()|, |drawPicture()| doesn't | |
263 // take a callback. This is used by |AnalysisCanvas| to early out. | |
264 picture_->playback(canvas, callback); | |
265 } else { | |
266 // Prefer to call |drawPicture()| on the canvas since it could place the | |
267 // entire picture on the canvas instead of parsing the skia operations. | |
268 canvas->drawPicture(picture_.get()); | |
269 } | |
270 SkIRect bounds; | |
271 canvas->getClipDeviceBounds(&bounds); | |
272 canvas->restore(); | |
273 TRACE_EVENT_END1( | |
274 "cc", "Picture::Raster", | |
275 "num_pixels_rasterized", bounds.width() * bounds.height()); | |
276 return bounds.width() * bounds.height(); | |
277 } | |
278 | |
279 void Picture::Replay(SkCanvas* canvas, SkPicture::AbortCallback* callback) { | |
280 TRACE_EVENT_BEGIN0("cc", "Picture::Replay"); | |
281 DCHECK(picture_); | |
282 picture_->playback(canvas, callback); | |
283 SkIRect bounds; | |
284 canvas->getClipDeviceBounds(&bounds); | |
285 TRACE_EVENT_END1("cc", "Picture::Replay", | |
286 "num_pixels_replayed", bounds.width() * bounds.height()); | |
287 } | |
288 | |
289 scoped_ptr<base::Value> Picture::AsValue() const { | |
290 // Encode the picture as base64. | |
291 scoped_ptr<base::DictionaryValue> res(new base::DictionaryValue()); | |
292 res->Set("params.layer_rect", MathUtil::AsValue(layer_rect_).release()); | |
293 std::string b64_picture; | |
294 PictureDebugUtil::SerializeAsBase64(picture_.get(), &b64_picture); | |
295 res->SetString("skp64", b64_picture); | |
296 return res.Pass(); | |
297 } | |
298 | |
299 void Picture::EmitTraceSnapshot() const { | |
300 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | |
301 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," | |
302 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | |
303 "cc::Picture", | |
304 this, | |
305 TracedPicture::AsTraceablePicture(this)); | |
306 } | |
307 | |
308 void Picture::EmitTraceSnapshotAlias(Picture* original) const { | |
309 TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID( | |
310 TRACE_DISABLED_BY_DEFAULT("cc.debug.picture") "," | |
311 TRACE_DISABLED_BY_DEFAULT("devtools.timeline.picture"), | |
312 "cc::Picture", | |
313 this, | |
314 TracedPicture::AsTraceablePictureAlias(original)); | |
315 } | |
316 | |
317 PixelRefMap::Iterator Picture::GetPixelRefMapIterator( | |
318 const gfx::Rect& layer_rect) const { | |
319 return PixelRefMap::Iterator(layer_rect, this); | |
320 } | |
321 | |
322 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | |
323 Picture::AsTraceableRasterData(float scale) const { | |
324 scoped_refptr<base::trace_event::TracedValue> raster_data = | |
325 new base::trace_event::TracedValue(); | |
326 TracedValue::SetIDRef(this, raster_data.get(), "picture_id"); | |
327 raster_data->SetDouble("scale", scale); | |
328 return raster_data; | |
329 } | |
330 | |
331 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | |
332 Picture::AsTraceableRecordData() const { | |
333 scoped_refptr<base::trace_event::TracedValue> record_data = | |
334 new base::trace_event::TracedValue(); | |
335 TracedValue::SetIDRef(this, record_data.get(), "picture_id"); | |
336 MathUtil::AddToTracedValue("layer_rect", layer_rect_, record_data.get()); | |
337 return record_data; | |
338 } | |
339 | |
340 } // namespace cc | |
OLD | NEW |