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