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: #ifndef SkSurface_Base_DEFINED michael@0: #define SkSurface_Base_DEFINED michael@0: michael@0: #include "SkSurface.h" michael@0: #include "SkCanvas.h" michael@0: michael@0: class SkSurface_Base : public SkSurface { michael@0: public: michael@0: SkSurface_Base(int width, int height); michael@0: explicit SkSurface_Base(const SkImageInfo&); michael@0: virtual ~SkSurface_Base(); michael@0: michael@0: /** michael@0: * Allocate a canvas that will draw into this surface. We will cache this michael@0: * canvas, to return the same object to the caller multiple times. We michael@0: * take ownership, and will call unref() on the canvas when we go out of michael@0: * scope. michael@0: */ michael@0: virtual SkCanvas* onNewCanvas() = 0; michael@0: michael@0: virtual SkSurface* onNewSurface(const SkImageInfo&) = 0; michael@0: michael@0: /** michael@0: * Allocate an SkImage that represents the current contents of the surface. michael@0: * This needs to be able to outlive the surface itself (if need be), and michael@0: * must faithfully represent the current contents, even if the surface michael@0: * is chaged after this calle (e.g. it is drawn to via its canvas). michael@0: */ michael@0: virtual SkImage* onNewImageSnapshot() = 0; michael@0: michael@0: /** michael@0: * Default implementation: michael@0: * michael@0: * image = this->newImageSnapshot(); michael@0: * if (image) { michael@0: * image->draw(canvas, ...); michael@0: * image->unref(); michael@0: * } michael@0: */ michael@0: virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); michael@0: michael@0: /** michael@0: * If the surface is about to change, we call this so that our subclass michael@0: * can optionally fork their backend (copy-on-write) in case it was michael@0: * being shared with the cachedImage. michael@0: */ michael@0: virtual void onCopyOnWrite(ContentChangeMode) = 0; michael@0: michael@0: inline SkCanvas* getCachedCanvas(); michael@0: inline SkImage* getCachedImage(); michael@0: michael@0: // called by SkSurface to compute a new genID michael@0: uint32_t newGenerationID(); michael@0: michael@0: private: michael@0: SkCanvas* fCachedCanvas; michael@0: SkImage* fCachedImage; michael@0: michael@0: void aboutToDraw(ContentChangeMode mode); michael@0: friend class SkCanvas; michael@0: friend class SkSurface; michael@0: michael@0: inline void installIntoCanvasForDirtyNotification(); michael@0: michael@0: typedef SkSurface INHERITED; michael@0: }; michael@0: michael@0: SkCanvas* SkSurface_Base::getCachedCanvas() { michael@0: if (NULL == fCachedCanvas) { michael@0: fCachedCanvas = this->onNewCanvas(); michael@0: this->installIntoCanvasForDirtyNotification(); michael@0: } michael@0: return fCachedCanvas; michael@0: } michael@0: michael@0: SkImage* SkSurface_Base::getCachedImage() { michael@0: if (NULL == fCachedImage) { michael@0: fCachedImage = this->onNewImageSnapshot(); michael@0: this->installIntoCanvasForDirtyNotification(); michael@0: } michael@0: return fCachedImage; michael@0: } michael@0: michael@0: void SkSurface_Base::installIntoCanvasForDirtyNotification() { michael@0: if (NULL != fCachedCanvas) { michael@0: fCachedCanvas->setSurfaceBase(this); michael@0: } michael@0: } michael@0: michael@0: #endif