2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkSurface_Base.h"
10 #include "SkImagePriv.h"
11 #include "SkPicture.h"
14 * What does it mean to ask for more than one canvas from a picture?
15 * How do we return an Image and then "continue" recording?
17 class SkSurface_Picture : public SkSurface_Base {
19 SkSurface_Picture(int width, int height);
20 virtual ~SkSurface_Picture();
22 virtual SkCanvas* onNewCanvas() SK_OVERRIDE;
23 virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE;
24 virtual SkImage* onNewImageSnapshot() SK_OVERRIDE;
25 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
26 const SkPaint*) SK_OVERRIDE;
27 virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE;
32 typedef SkSurface_Base INHERITED;
35 ///////////////////////////////////////////////////////////////////////////////
37 SkSurface_Picture::SkSurface_Picture(int width, int height) : INHERITED(width, height) {
41 SkSurface_Picture::~SkSurface_Picture() {
42 SkSafeUnref(fPicture);
45 SkCanvas* SkSurface_Picture::onNewCanvas() {
47 fPicture = SkNEW(SkPicture);
49 SkCanvas* canvas = fPicture->beginRecording(this->width(), this->height());
50 canvas->ref(); // our caller will call unref()
54 SkSurface* SkSurface_Picture::onNewSurface(const SkImageInfo& info) {
55 return SkSurface::NewPicture(info.fWidth, info.fHeight);
58 SkImage* SkSurface_Picture::onNewImageSnapshot() {
60 return SkNewImageFromPicture(fPicture);
63 info.fWidth = info.fHeight = 0;
64 info.fColorType = kPMColor_SkColorType;
65 info.fAlphaType = kOpaque_SkAlphaType;
66 return SkImage::NewRasterCopy(info, NULL, 0);
70 void SkSurface_Picture::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y,
71 const SkPaint* paint) {
75 SkImagePrivDrawPicture(canvas, fPicture, x, y, paint);
78 void SkSurface_Picture::onCopyOnWrite(ContentChangeMode /*mode*/) {
79 // We always spawn a copy of the recording picture when we
80 // are asked for a snapshot, so we never need to do anything here.
83 ///////////////////////////////////////////////////////////////////////////////
86 SkSurface* SkSurface::NewPicture(int width, int height) {
87 if ((width | height) < 0) {
91 return SkNEW_ARGS(SkSurface_Picture, (width, height));