blob: be934fa23884b12af9500571876f5278104b47df [file] [log] [blame]
reed@google.com58b21ec2012-07-30 18:20:12 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkImage_Base.h"
9#include "SkImagePriv.h"
10#include "SkPicture.h"
11
12class SkImage_Picture : public SkImage_Base {
13public:
14 SkImage_Picture(SkPicture*);
15 virtual ~SkImage_Picture();
16
17 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
18
junov@chromium.org45c3db82013-04-11 17:52:05 +000019 SkPicture* getPicture() { return fPicture; }
20
reed@google.com58b21ec2012-07-30 18:20:12 +000021private:
22 SkPicture* fPicture;
23
24 typedef SkImage_Base INHERITED;
25};
26
27///////////////////////////////////////////////////////////////////////////////
28
29SkImage_Picture::SkImage_Picture(SkPicture* pict) : INHERITED(pict->width(), pict->height()) {
30 pict->endRecording();
31 pict->ref();
32 fPicture = pict;
33}
34
35SkImage_Picture::~SkImage_Picture() {
36 fPicture->unref();
37}
38
39void SkImage_Picture::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
40 const SkPaint* paint) {
41 SkImagePrivDrawPicture(canvas, fPicture, x, y, paint);
42}
43
reed@google.com9ea5a3b2012-07-30 21:03:46 +000044SkImage* SkNewImageFromPicture(const SkPicture* srcPicture) {
45 /**
46 * We want to snapshot the playback status of the picture, w/o affecting
47 * its ability to continue recording (if needed).
48 *
49 * Optimally this will shared as much data/buffers as it can with
50 * srcPicture, and srcPicture will perform a copy-on-write as needed if it
51 * needs to mutate them later on.
52 */
53 SkAutoTUnref<SkPicture> playback(SkNEW_ARGS(SkPicture, (*srcPicture)));
54
55 return SkNEW_ARGS(SkImage_Picture, (playback));
reed@google.com58b21ec2012-07-30 18:20:12 +000056}
junov@chromium.org45c3db82013-04-11 17:52:05 +000057
58SkPicture* SkPictureImageGetPicture(SkImage* pictureImage) {
59 return static_cast<SkImage_Picture*>(pictureImage)->getPicture();
60}