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 "include/core/SkCanvas.h"
12 #include "include/core/SkSurface.h"
13 #include "src/core/SkImagePriv.h"
14 #include "src/core/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 GrBackendTexture onGetBackendTexture(BackendHandleAccess);
23 virtual GrBackendRenderTarget onGetBackendRenderTarget(BackendHandleAccess);
24 virtual bool onReplaceBackendTexture(const GrBackendTexture&,
25 GrSurfaceOrigin,
26 TextureReleaseProc,
27 ReleaseContext);
28 /**
29 * Allocate a canvas that will draw into this surface. We will cache this
30 * canvas, to return the same object to the caller multiple times. We
31 * take ownership, and will call unref() on the canvas when we go out of
32 * scope.
33 */
34 virtual SkCanvas* onNewCanvas() = 0;
35
36 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0;
37
38 /**
39 * Allocate an SkImage that represents the current contents of the surface.
40 * This needs to be able to outlive the surface itself (if need be), and
41 * must faithfully represent the current contents, even if the surface
42 * is changed after this called (e.g. it is drawn to via its canvas).
43 *
44 * If a subset is specified, the the impl must make a copy, rather than try to wait
45 * on copy-on-write.
46 */
47 virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; }
48
49 virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
50
51 /**
52 * Default implementation does a rescale/read and then calls the callback.
53 */
54 virtual void onAsyncRescaleAndReadPixels(const SkImageInfo& info, const SkIRect& srcRect,
55 RescaleGamma rescaleGamma,
56 SkFilterQuality rescaleQuality,
57 ReadPixelsCallback callback,
58 ReadPixelsContext context);
59 /**
60 * Default implementation does a rescale/read/yuv conversion and then calls the callback.
61 */
62 virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace,
63 sk_sp<SkColorSpace> dstColorSpace,
64 const SkIRect& srcRect, int dstW, int dstH,
65 RescaleGamma rescaleGamma,
66 SkFilterQuality rescaleQuality,
67 ReadPixelsCallbackYUV420 callback,
68 ReadPixelsContext context);
69
70 /**
71 * Default implementation:
72 *
73 * image = this->newImageSnapshot();
74 * if (image) {
75 * image->draw(canvas, ...);
76 * image->unref();
77 * }
78 */
79 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
80
81 /**
82 * Called as a performance hint when the Surface is allowed to make it's contents
83 * undefined.
84 */
onDiscard()85 virtual void onDiscard() {}
86
87 /**
88 * If the surface is about to change, we call this so that our subclass
89 * can optionally fork their backend (copy-on-write) in case it was
90 * being shared with the cachedImage.
91 */
92 virtual void onCopyOnWrite(ContentChangeMode) = 0;
93
94 /**
95 * Signal the surface to remind its backing store that it's mutable again.
96 * Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
97 */
onRestoreBackingMutability()98 virtual void onRestoreBackingMutability() {}
99
100 /**
101 * Issue any pending surface IO to the current backend 3D API and resolve any surface MSAA.
102 * Inserts the requested number of semaphores for the gpu to signal when work is complete on the
103 * gpu and inits the array of GrBackendSemaphores with the signaled semaphores.
104 */
onFlush(BackendSurfaceAccess access,const GrFlushInfo &)105 virtual GrSemaphoresSubmitted onFlush(BackendSurfaceAccess access, const GrFlushInfo&) {
106 return GrSemaphoresSubmitted::kNo;
107 }
108
109 /**
110 * Caused the current backend 3D API to wait on the passed in semaphores before executing new
111 * commands on the gpu. Any previously submitting commands will not be blocked by these
112 * semaphores.
113 */
onWait(int numSemaphores,const GrBackendSemaphore * waitSemaphores)114 virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores) {
115 return false;
116 }
117
onCharacterize(SkSurfaceCharacterization *)118 virtual bool onCharacterize(SkSurfaceCharacterization*) const { return false; }
onIsCompatible(const SkSurfaceCharacterization &)119 virtual bool onIsCompatible(const SkSurfaceCharacterization&) const { return false; }
onDraw(const SkDeferredDisplayList *)120 virtual bool onDraw(const SkDeferredDisplayList*) { return false; }
121
122 inline SkCanvas* getCachedCanvas();
123 inline sk_sp<SkImage> refCachedImage();
124
hasCachedImage()125 bool hasCachedImage() const { return fCachedImage != nullptr; }
126
127 // called by SkSurface to compute a new genID
128 uint32_t newGenerationID();
129
130 private:
131 std::unique_ptr<SkCanvas> fCachedCanvas;
132 sk_sp<SkImage> fCachedImage;
133
134 void aboutToDraw(ContentChangeMode mode);
135
136 // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
137 // would trigger a copy-on-write.
138 bool outstandingImageSnapshot() const;
139
140 friend class SkCanvas;
141 friend class SkSurface;
142
143 typedef SkSurface INHERITED;
144 };
145
getCachedCanvas()146 SkCanvas* SkSurface_Base::getCachedCanvas() {
147 if (nullptr == fCachedCanvas) {
148 fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
149 if (fCachedCanvas) {
150 fCachedCanvas->setSurfaceBase(this);
151 }
152 }
153 return fCachedCanvas.get();
154 }
155
refCachedImage()156 sk_sp<SkImage> SkSurface_Base::refCachedImage() {
157 if (fCachedImage) {
158 return fCachedImage;
159 }
160
161 fCachedImage = this->onNewImageSnapshot();
162
163 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
164 return fCachedImage;
165 }
166
167 #endif
168