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
onGetTextureHandle(BackendHandleAccess)22 virtual GrBackendObject onGetTextureHandle(BackendHandleAccess) {
23 return 0;
24 }
25
onGetRenderTargetHandle(GrBackendObject *,BackendHandleAccess)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 sk_sp<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 sk_sp<SkImage> onNewImageSnapshot() = 0;
47
48 virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
49
50 /**
51 * Default implementation:
52 *
53 * image = this->newImageSnapshot();
54 * if (image) {
55 * image->draw(canvas, ...);
56 * image->unref();
57 * }
58 */
59 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
60
61 /**
62 * Called as a performance hint when the Surface is allowed to make it's contents
63 * undefined.
64 */
onDiscard()65 virtual void onDiscard() {}
66
67 /**
68 * If the surface is about to change, we call this so that our subclass
69 * can optionally fork their backend (copy-on-write) in case it was
70 * being shared with the cachedImage.
71 */
72 virtual void onCopyOnWrite(ContentChangeMode) = 0;
73
74 /**
75 * Signal the surface to remind its backing store that it's mutable again.
76 * Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
77 */
onRestoreBackingMutability()78 virtual void onRestoreBackingMutability() {}
79
80 /**
81 * Issue any pending surface IO to the current backend 3D API and resolve any surface MSAA.
82 * Inserts the requested number of semaphores for the gpu to signal when work is complete on the
83 * gpu and inits the array of GrBackendSemaphores with the signaled semaphores.
84 */
onFlush(int numSemaphores,GrBackendSemaphore signalSemaphores[])85 virtual GrSemaphoresSubmitted onFlush(int numSemaphores,
86 GrBackendSemaphore signalSemaphores[]) {
87 return GrSemaphoresSubmitted::kNo;
88 }
89
90 /**
91 * Caused the current backend 3D API to wait on the passed in semaphores before executing new
92 * commands on the gpu. Any previously submitting commands will not be blocked by these
93 * semaphores.
94 */
onWait(int numSemaphores,const GrBackendSemaphore * waitSemaphores)95 virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
96 return false;
97 }
98
onCharacterize(SkSurfaceCharacterization *)99 virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; }
onDraw(const SkDeferredDisplayList *)100 virtual bool onDraw(const SkDeferredDisplayList*) { return false; }
101
102 inline SkCanvas* getCachedCanvas();
103 inline sk_sp<SkImage> refCachedImage();
104
hasCachedImage()105 bool hasCachedImage() const { return fCachedImage != nullptr; }
106
107 // called by SkSurface to compute a new genID
108 uint32_t newGenerationID();
109
110 private:
111 std::unique_ptr<SkCanvas> fCachedCanvas;
112 sk_sp<SkImage> fCachedImage;
113
114 void aboutToDraw(ContentChangeMode mode);
115
116 // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
117 // would trigger a copy-on-write.
118 bool outstandingImageSnapshot() const;
119
120 friend class SkCanvas;
121 friend class SkSurface;
122
123 typedef SkSurface INHERITED;
124 };
125
getCachedCanvas()126 SkCanvas* SkSurface_Base::getCachedCanvas() {
127 if (nullptr == fCachedCanvas) {
128 fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
129 if (fCachedCanvas) {
130 fCachedCanvas->setSurfaceBase(this);
131 }
132 }
133 return fCachedCanvas.get();
134 }
135
refCachedImage()136 sk_sp<SkImage> SkSurface_Base::refCachedImage() {
137 if (fCachedImage) {
138 return fCachedImage;
139 }
140
141 fCachedImage = this->onNewImageSnapshot();
142
143 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
144 return fCachedImage;
145 }
146
147 #endif
148