blob: 87221de27b555f700560c429c9110e52ae1bfd6e [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;
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000018 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE;
reed@google.com58b21ec2012-07-30 18:20:12 +000019
junov@chromium.org45c3db82013-04-11 17:52:05 +000020 SkPicture* getPicture() { return fPicture; }
21
reed@google.com58b21ec2012-07-30 18:20:12 +000022private:
23 SkPicture* fPicture;
24
25 typedef SkImage_Base INHERITED;
26};
27
28///////////////////////////////////////////////////////////////////////////////
29
30SkImage_Picture::SkImage_Picture(SkPicture* pict) : INHERITED(pict->width(), pict->height()) {
31 pict->endRecording();
32 pict->ref();
33 fPicture = pict;
34}
35
36SkImage_Picture::~SkImage_Picture() {
37 fPicture->unref();
38}
39
40void SkImage_Picture::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
41 const SkPaint* paint) {
42 SkImagePrivDrawPicture(canvas, fPicture, x, y, paint);
43}
44
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000045void SkImage_Picture::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
46 const SkPaint* paint) {
47 SkImagePrivDrawPicture(canvas, fPicture, src, dst, paint);
48}
49
reed@google.com9ea5a3b2012-07-30 21:03:46 +000050SkImage* SkNewImageFromPicture(const SkPicture* srcPicture) {
51 /**
52 * We want to snapshot the playback status of the picture, w/o affecting
53 * its ability to continue recording (if needed).
54 *
55 * Optimally this will shared as much data/buffers as it can with
56 * srcPicture, and srcPicture will perform a copy-on-write as needed if it
57 * needs to mutate them later on.
58 */
59 SkAutoTUnref<SkPicture> playback(SkNEW_ARGS(SkPicture, (*srcPicture)));
60
61 return SkNEW_ARGS(SkImage_Picture, (playback));
reed@google.com58b21ec2012-07-30 18:20:12 +000062}
junov@chromium.org45c3db82013-04-11 17:52:05 +000063
64SkPicture* SkPictureImageGetPicture(SkImage* pictureImage) {
65 return static_cast<SkImage_Picture*>(pictureImage)->getPicture();
66}