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
11 #include "SkSurface.h"
14 class SkSurface_Base : public SkSurface {
16 SkSurface_Base(int width, int height);
17 explicit SkSurface_Base(const SkImageInfo&);
18 virtual ~SkSurface_Base();
21 * Allocate a canvas that will draw into this surface. We will cache this
22 * canvas, to return the same object to the caller multiple times. We
23 * take ownership, and will call unref() on the canvas when we go out of
26 virtual SkCanvas* onNewCanvas() = 0;
28 virtual SkSurface* onNewSurface(const SkImageInfo&) = 0;
31 * Allocate an SkImage that represents the current contents of the surface.
32 * This needs to be able to outlive the surface itself (if need be), and
33 * must faithfully represent the current contents, even if the surface
34 * is chaged after this calle (e.g. it is drawn to via its canvas).
36 virtual SkImage* onNewImageSnapshot() = 0;
39 * Default implementation:
41 * image = this->newImageSnapshot();
43 * image->draw(canvas, ...);
47 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
50 * Called as a performance hint when the Surface is allowed to make it's contents
53 virtual void onDiscard() {}
56 * If the surface is about to change, we call this so that our subclass
57 * can optionally fork their backend (copy-on-write) in case it was
58 * being shared with the cachedImage.
60 virtual void onCopyOnWrite(ContentChangeMode) = 0;
62 inline SkCanvas* getCachedCanvas();
63 inline SkImage* getCachedImage();
65 // called by SkSurface to compute a new genID
66 uint32_t newGenerationID();
69 SkCanvas* fCachedCanvas;
70 SkImage* fCachedImage;
72 void aboutToDraw(ContentChangeMode mode);
73 friend class SkCanvas;
74 friend class SkSurface;
76 typedef SkSurface INHERITED;
79 SkCanvas* SkSurface_Base::getCachedCanvas() {
80 if (NULL == fCachedCanvas) {
81 fCachedCanvas = this->onNewCanvas();
82 if (NULL != fCachedCanvas) {
83 fCachedCanvas->setSurfaceBase(this);
89 SkImage* SkSurface_Base::getCachedImage() {
90 if (NULL == fCachedImage) {
91 fCachedImage = this->onNewImageSnapshot();
92 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);