GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / cc / resources / picture.cc
blobe88ff21dc42076570a14aa2b71562182298da2f7
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/resources/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/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"
33 namespace cc {
35 namespace {
37 bool DecodeBitmap(const void* buffer, size_t size, SkBitmap* bm) {
38 const unsigned char* data = static_cast<const unsigned char *>(buffer);
40 // Try PNG first.
41 if (gfx::PNGCodec::Decode(data, size, bm))
42 return true;
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;
50 return false;
53 } // namespace
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));
64 picture->Record(client, recording_mode);
65 if (gather_pixel_refs)
66 picture->GatherPixelRefs();
68 return picture;
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.
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;
83 std::string decoded;
84 base::Base64Decode(encoded, &decoded);
85 SkMemoryStream stream(decoded.data(), decoded.size());
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;
92 gfx::Rect layer_rect(gfx::SkIRectToRect(skpicture->cullRect().roundOut()));
93 return make_scoped_refptr(new Picture(skpicture, layer_rect));
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;
101 // Decode the picture from base64.
102 std::string encoded;
103 if (!value->GetString("skp64", &encoded))
104 return NULL;
106 std::string decoded;
107 base::Base64Decode(encoded, &decoded);
108 SkMemoryStream stream(decoded.data(), decoded.size());
110 const base::Value* layer_rect_value = NULL;
111 if (!value->Get("params.layer_rect", &layer_rect_value))
112 return NULL;
114 gfx::Rect layer_rect;
115 if (!MathUtil::FromValue(layer_rect_value, &layer_rect))
116 return NULL;
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;
123 return make_scoped_refptr(new Picture(skpicture, layer_rect));
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()) {
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) {
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::HasText() const {
163 DCHECK(picture_);
164 return picture_->hasText();
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);
176 DCHECK(!picture_);
178 SkRTreeFactory factory;
179 SkPictureRecorder recorder;
181 skia::RefPtr<SkCanvas> canvas;
182 canvas = skia::SharePtr(recorder.beginRecording(
183 layer_rect_.width(), layer_rect_.height(), &factory,
184 SkPictureRecorder::kComputeSaveLayerInfo_RecordFlag));
186 ContentLayerClient::PaintingControlSetting painting_control =
187 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL;
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_CONSTRUCTION_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 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.endRecording());
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