1 /* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkSurface_Base_DEFINED 9 #define SkSurface_Base_DEFINED 10 11 #include "SkSurface.h" 12 13 class SkSurface_Base : public SkSurface { 14 public: 15 SkSurface_Base(int width, int height); 16 virtual ~SkSurface_Base(); 17 18 /** 19 * Allocate a canvas that will draw into this surface. We will cache this 20 * canvas, to return the same object to the caller multiple times. We 21 * take ownership, and will call unref() on the canvas when we go out of 22 * scope. 23 */ 24 virtual SkCanvas* onNewCanvas() = 0; 25 26 virtual SkSurface* onNewSurface(const SkImage::Info&) = 0; 27 28 /** 29 * Allocate an SkImage that represents the current contents of the surface. 30 * This needs to be able to outlive the surface itself (if need be), and 31 * must faithfully represent the current contents, even if the surface 32 * is chaged after this calle (e.g. it is drawn to via its canvas). 33 */ 34 virtual SkImage* onNewImageShapshot() = 0; 35 36 /** 37 * Default implementation: 38 * 39 * image = this->newImageSnapshot(); 40 * if (image) { 41 * image->draw(canvas, ...); 42 * image->unref(); 43 * } 44 */ 45 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); 46 47 /** 48 * If the surface is about to change, we call this so that our subclass 49 * can optionally fork their backend (copy-on-write) in case it was 50 * being shared with the cachedImage. 51 * 52 * The default implementation does nothing. 53 */ 54 virtual void onCopyOnWrite(SkImage* cachedImage, SkCanvas*) = 0; 55 56 inline SkCanvas* getCachedCanvas(); 57 inline SkImage* getCachedImage(); 58 59 // called by SkSurface to compute a new genID 60 uint32_t newGenerationID(); 61 62 private: 63 SkCanvas* fCachedCanvas; 64 SkImage* fCachedImage; 65 66 void aboutToDraw(SkCanvas*); 67 friend class SkCanvas; 68 friend class SkSurface; 69 70 inline void installIntoCanvasForDirtyNotification(); 71 72 typedef SkSurface INHERITED; 73 }; 74 75 #endif 76