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 "SkCanvas.h" 12 #include "SkImagePriv.h" 13 #include "SkSurface.h" 14 #include "SkSurfacePriv.h" 15 16 class SkSurface_Base : public SkSurface { 17 public: 18 SkSurface_Base(int width, int height, const SkSurfaceProps*); 19 SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*); 20 virtual ~SkSurface_Base(); 21 22 virtual GrBackendObject onGetTextureHandle(BackendHandleAccess) { 23 return 0; 24 } 25 26 virtual bool onGetRenderTargetHandle(GrBackendObject*, BackendHandleAccess) { 27 return false; 28 } 29 30 /** 31 * Allocate a canvas that will draw into this surface. We will cache this 32 * canvas, to return the same object to the caller multiple times. We 33 * take ownership, and will call unref() on the canvas when we go out of 34 * scope. 35 */ 36 virtual SkCanvas* onNewCanvas() = 0; 37 38 virtual SkSurface* onNewSurface(const SkImageInfo&) = 0; 39 40 /** 41 * Allocate an SkImage that represents the current contents of the surface. 42 * This needs to be able to outlive the surface itself (if need be), and 43 * must faithfully represent the current contents, even if the surface 44 * is changed after this called (e.g. it is drawn to via its canvas). 45 */ 46 virtual SkImage* onNewImageSnapshot(SkBudgeted, ForceCopyMode) = 0; 47 48 /** 49 * Default implementation: 50 * 51 * image = this->newImageSnapshot(); 52 * if (image) { 53 * image->draw(canvas, ...); 54 * image->unref(); 55 * } 56 */ 57 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); 58 59 /** 60 * Called as a performance hint when the Surface is allowed to make it's contents 61 * undefined. 62 */ 63 virtual void onDiscard() {} 64 65 /** 66 * If the surface is about to change, we call this so that our subclass 67 * can optionally fork their backend (copy-on-write) in case it was 68 * being shared with the cachedImage. 69 */ 70 virtual void onCopyOnWrite(ContentChangeMode) = 0; 71 72 /** 73 * Signal the surface to remind its backing store that it's mutable again. 74 * Called only when we _didn't_ copy-on-write; we assume the copies start mutable. 75 */ 76 virtual void onRestoreBackingMutability() {} 77 78 /** 79 * Issue any pending surface IO to the current backend 3D API and resolve any surface MSAA. 80 */ 81 virtual void onPrepareForExternalIO() {} 82 83 inline SkCanvas* getCachedCanvas(); 84 inline SkImage* refCachedImage(SkBudgeted, ForceUnique); 85 86 bool hasCachedImage() const { return fCachedImage != nullptr; } 87 88 // called by SkSurface to compute a new genID 89 uint32_t newGenerationID(); 90 91 private: 92 SkCanvas* fCachedCanvas; 93 SkImage* fCachedImage; 94 95 void aboutToDraw(ContentChangeMode mode); 96 97 // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw 98 // would trigger a copy-on-write. 99 bool outstandingImageSnapshot() const; 100 101 friend class SkCanvas; 102 friend class SkSurface; 103 104 typedef SkSurface INHERITED; 105 }; 106 107 SkCanvas* SkSurface_Base::getCachedCanvas() { 108 if (nullptr == fCachedCanvas) { 109 fCachedCanvas = this->onNewCanvas(); 110 if (fCachedCanvas) { 111 fCachedCanvas->setSurfaceBase(this); 112 } 113 } 114 return fCachedCanvas; 115 } 116 117 SkImage* SkSurface_Base::refCachedImage(SkBudgeted budgeted, ForceUnique unique) { 118 SkImage* snap = fCachedImage; 119 if (kYes_ForceUnique == unique && snap && !snap->unique()) { 120 snap = nullptr; 121 } 122 if (snap) { 123 return SkRef(snap); 124 } 125 ForceCopyMode fcm = (kYes_ForceUnique == unique) ? kYes_ForceCopyMode : 126 kNo_ForceCopyMode; 127 snap = this->onNewImageSnapshot(budgeted, fcm); 128 if (kNo_ForceUnique == unique) { 129 SkASSERT(!fCachedImage); 130 fCachedImage = SkSafeRef(snap); 131 } 132 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this); 133 return snap; 134 } 135 136 #endif 137