michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkSurface_Base.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkImagePriv.h" michael@0: #include "SkPicture.h" michael@0: michael@0: /** michael@0: * What does it mean to ask for more than one canvas from a picture? michael@0: * How do we return an Image and then "continue" recording? michael@0: */ michael@0: class SkSurface_Picture : public SkSurface_Base { michael@0: public: michael@0: SkSurface_Picture(int width, int height); michael@0: virtual ~SkSurface_Picture(); michael@0: michael@0: virtual SkCanvas* onNewCanvas() SK_OVERRIDE; michael@0: virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE; michael@0: virtual SkImage* onNewImageSnapshot() SK_OVERRIDE; michael@0: virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, michael@0: const SkPaint*) SK_OVERRIDE; michael@0: virtual void onCopyOnWrite(ContentChangeMode) SK_OVERRIDE; michael@0: michael@0: private: michael@0: SkPicture* fPicture; michael@0: michael@0: typedef SkSurface_Base INHERITED; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkSurface_Picture::SkSurface_Picture(int width, int height) : INHERITED(width, height) { michael@0: fPicture = NULL; michael@0: } michael@0: michael@0: SkSurface_Picture::~SkSurface_Picture() { michael@0: SkSafeUnref(fPicture); michael@0: } michael@0: michael@0: SkCanvas* SkSurface_Picture::onNewCanvas() { michael@0: if (!fPicture) { michael@0: fPicture = SkNEW(SkPicture); michael@0: } michael@0: SkCanvas* canvas = fPicture->beginRecording(this->width(), this->height()); michael@0: canvas->ref(); // our caller will call unref() michael@0: return canvas; michael@0: } michael@0: michael@0: SkSurface* SkSurface_Picture::onNewSurface(const SkImageInfo& info) { michael@0: return SkSurface::NewPicture(info.fWidth, info.fHeight); michael@0: } michael@0: michael@0: SkImage* SkSurface_Picture::onNewImageSnapshot() { michael@0: if (fPicture) { michael@0: return SkNewImageFromPicture(fPicture); michael@0: } else { michael@0: SkImageInfo info; michael@0: info.fWidth = info.fHeight = 0; michael@0: info.fColorType = kPMColor_SkColorType; michael@0: info.fAlphaType = kOpaque_SkAlphaType; michael@0: return SkImage::NewRasterCopy(info, NULL, 0); michael@0: } michael@0: } michael@0: michael@0: void SkSurface_Picture::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, michael@0: const SkPaint* paint) { michael@0: if (!fPicture) { michael@0: return; michael@0: } michael@0: SkImagePrivDrawPicture(canvas, fPicture, x, y, paint); michael@0: } michael@0: michael@0: void SkSurface_Picture::onCopyOnWrite(ContentChangeMode /*mode*/) { michael@0: // We always spawn a copy of the recording picture when we michael@0: // are asked for a snapshot, so we never need to do anything here. michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: michael@0: SkSurface* SkSurface::NewPicture(int width, int height) { michael@0: if ((width | height) < 0) { michael@0: return NULL; michael@0: } michael@0: michael@0: return SkNEW_ARGS(SkSurface_Picture, (width, height)); michael@0: }