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 #ifndef SkSurface_Base_DEFINED
9 #define SkSurface_Base_DEFINED
12 #include "SkSurface.h"
13 #include "SkSurfacePriv.h"
15 class SkSurface_Base : public SkSurface {
17 SkSurface_Base(int width, int height, const SkSurfaceProps*);
18 SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*);
19 virtual ~SkSurface_Base();
22 * Allocate a canvas that will draw into this surface. We will cache this
23 * canvas, to return the same object to the caller multiple times. We
24 * take ownership, and will call unref() on the canvas when we go out of
27 virtual SkCanvas* onNewCanvas() = 0;
29 virtual SkSurface* onNewSurface(const SkImageInfo&) = 0;
32 * Allocate an SkImage that represents the current contents of the surface.
33 * This needs to be able to outlive the surface itself (if need be), and
34 * must faithfully represent the current contents, even if the surface
35 * is chaged after this calle (e.g. it is drawn to via its canvas).
37 virtual SkImage* onNewImageSnapshot() = 0;
40 * Default implementation:
42 * image = this->newImageSnapshot();
44 * image->draw(canvas, ...);
48 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
51 * Called as a performance hint when the Surface is allowed to make it's contents
54 virtual void onDiscard() {}
57 * If the surface is about to change, we call this so that our subclass
58 * can optionally fork their backend (copy-on-write) in case it was
59 * being shared with the cachedImage.
61 virtual void onCopyOnWrite(ContentChangeMode) = 0;
63 inline SkCanvas* getCachedCanvas();
64 inline SkImage* getCachedImage();
66 // called by SkSurface to compute a new genID
67 uint32_t newGenerationID();
70 SkCanvas* fCachedCanvas;
71 SkImage* fCachedImage;
73 void aboutToDraw(ContentChangeMode mode);
74 friend class SkCanvas;
75 friend class SkSurface;
77 typedef SkSurface INHERITED;
80 SkCanvas* SkSurface_Base::getCachedCanvas() {
81 if (NULL == fCachedCanvas) {
82 fCachedCanvas = this->onNewCanvas();
84 fCachedCanvas->setSurfaceBase(this);
90 SkImage* SkSurface_Base::getCachedImage() {
91 if (NULL == fCachedImage) {
92 fCachedImage = this->onNewImageSnapshot();
93 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);