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/SkImage.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkSamplingOptions.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkSurface.h"
17 #include "include/core/SkTypes.h"
18
19 #include <cstdint>
20 #include <memory>
21
22 class GrBackendSemaphore;
23 class GrBackendTexture;
24 class GrRecordingContext;
25 class SkCapabilities;
26 class SkColorSpace;
27 class SkPaint;
28 class SkPixmap;
29 class GrSurfaceCharacterization;
30 class SkSurfaceProps;
31 enum GrSurfaceOrigin : int;
32 enum SkYUVColorSpace : int;
33 namespace skgpu { namespace graphite { class Recorder; } }
34 struct SkIRect;
35 struct SkISize;
36 struct SkImageInfo;
37
38 class SkSurface_Base : public SkSurface {
39 public:
40 SkSurface_Base(int width, int height, const SkSurfaceProps*);
41 SkSurface_Base(const SkImageInfo&, const SkSurfaceProps*);
42 ~SkSurface_Base() override;
43
44 // From SkSurface.h
replaceBackendTexture(const GrBackendTexture &,GrSurfaceOrigin,ContentChangeMode,TextureReleaseProc,ReleaseContext)45 bool replaceBackendTexture(const GrBackendTexture&,
46 GrSurfaceOrigin,
47 ContentChangeMode,
48 TextureReleaseProc,
49 ReleaseContext) override {
50 return false;
51 }
52
53 enum class Type {
54 kNull, // intentionally associating 0 with a null canvas
55 kGanesh,
56 kGraphite,
57 kRaster,
58 };
59
60 // TODO(kjlubick) Android directly subclasses SkSurface_Base for tests, so we
61 // cannot make this a pure virtual. They seem to want a surface that is spy-able
62 // or mockable, so maybe we should provide something like that.
type()63 virtual Type type() const { return Type::kNull; }
64
65 // True for surfaces instantiated by pixels in CPU memory
isRasterBacked()66 bool isRasterBacked() const { return this->type() == Type::kRaster; }
67 // True for surfaces instantiated by Ganesh in GPU memory
isGaneshBacked()68 bool isGaneshBacked() const { return this->type() == Type::kGanesh; }
69 // True for surfaces instantiated by Graphite in GPU memory
isGraphiteBacked()70 bool isGraphiteBacked() const { return this->type() == Type::kGraphite; }
71
72 virtual GrRecordingContext* onGetRecordingContext() const;
73 virtual skgpu::graphite::Recorder* onGetRecorder() const;
74
75 /**
76 * Allocate a canvas that will draw into this surface. We will cache this
77 * canvas, to return the same object to the caller multiple times. We
78 * take ownership, and will call unref() on the canvas when we go out of
79 * scope.
80 */
81 virtual SkCanvas* onNewCanvas() = 0;
82
83 virtual sk_sp<SkSurface> onNewSurface(const SkImageInfo&) = 0;
84
85 /**
86 * Allocate an SkImage that represents the current contents of the surface.
87 * This needs to be able to outlive the surface itself (if need be), and
88 * must faithfully represent the current contents, even if the surface
89 * is changed after this called (e.g. it is drawn to via its canvas).
90 *
91 * If a subset is specified, the the impl must make a copy, rather than try to wait
92 * on copy-on-write.
93 */
94 virtual sk_sp<SkImage> onNewImageSnapshot(const SkIRect* subset = nullptr) { return nullptr; }
95
96 virtual void onWritePixels(const SkPixmap&, int x, int y) = 0;
97
98 /**
99 * Default implementation does a rescale/read and then calls the callback.
100 */
101 virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&,
102 const SkIRect srcRect,
103 RescaleGamma,
104 RescaleMode,
105 ReadPixelsCallback,
106 ReadPixelsContext);
107 /**
108 * Default implementation does a rescale/read/yuv conversion and then calls the callback.
109 */
110 virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
111 bool readAlpha,
112 sk_sp<SkColorSpace> dstColorSpace,
113 SkIRect srcRect,
114 SkISize dstSize,
115 RescaleGamma,
116 RescaleMode,
117 ReadPixelsCallback,
118 ReadPixelsContext);
119
120 /**
121 * Default implementation:
122 *
123 * image = this->newImageSnapshot();
124 * if (image) {
125 * image->draw(canvas, ...);
126 * image->unref();
127 * }
128 */
129 virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkSamplingOptions&,const SkPaint*);
130
131 /**
132 * Called as a performance hint when the Surface is allowed to make it's contents
133 * undefined.
134 */
onDiscard()135 virtual void onDiscard() {}
136
137 /**
138 * If the surface is about to change, we call this so that our subclass
139 * can optionally fork their backend (copy-on-write) in case it was
140 * being shared with the cachedImage.
141 *
142 * Returns false if the backing cannot be un-shared.
143 */
144 [[nodiscard]] virtual bool onCopyOnWrite(ContentChangeMode) = 0;
145
146 /**
147 * Signal the surface to remind its backing store that it's mutable again.
148 * Called only when we _didn't_ copy-on-write; we assume the copies start mutable.
149 */
onRestoreBackingMutability()150 virtual void onRestoreBackingMutability() {}
151
152 /**
153 * Caused the current backend 3D API to wait on the passed in semaphores before executing new
154 * commands on the gpu. Any previously submitting commands will not be blocked by these
155 * semaphores.
156 */
onWait(int numSemaphores,const GrBackendSemaphore * waitSemaphores,bool deleteSemaphoresAfterWait)157 virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
158 bool deleteSemaphoresAfterWait) {
159 return false;
160 }
161
onCharacterize(GrSurfaceCharacterization *)162 virtual bool onCharacterize(GrSurfaceCharacterization*) const { return false; }
onIsCompatible(const GrSurfaceCharacterization &)163 virtual bool onIsCompatible(const GrSurfaceCharacterization&) const { return false; }
164
165 // TODO: Remove this (make it pure virtual) after updating Android (which has a class derived
166 // from SkSurface_Base).
167 virtual sk_sp<const SkCapabilities> onCapabilities();
168
169 inline SkCanvas* getCachedCanvas();
170 inline sk_sp<SkImage> refCachedImage();
171
hasCachedImage()172 bool hasCachedImage() const { return fCachedImage != nullptr; }
173
174 // called by SkSurface to compute a new genID
175 uint32_t newGenerationID();
176
177 private:
178 std::unique_ptr<SkCanvas> fCachedCanvas = nullptr;
179 sk_sp<SkImage> fCachedImage = nullptr;
180
181 // Returns false if drawing should not take place (allocation failure).
182 [[nodiscard]] bool aboutToDraw(ContentChangeMode mode);
183
184 // Returns true if there is an outstanding image-snapshot, indicating that a call to aboutToDraw
185 // would trigger a copy-on-write.
186 bool outstandingImageSnapshot() const;
187
188 friend class SkCanvas;
189 friend class SkSurface;
190 };
191
getCachedCanvas()192 SkCanvas* SkSurface_Base::getCachedCanvas() {
193 if (nullptr == fCachedCanvas) {
194 fCachedCanvas = std::unique_ptr<SkCanvas>(this->onNewCanvas());
195 if (fCachedCanvas) {
196 fCachedCanvas->setSurfaceBase(this);
197 }
198 }
199 return fCachedCanvas.get();
200 }
201
refCachedImage()202 sk_sp<SkImage> SkSurface_Base::refCachedImage() {
203 if (fCachedImage) {
204 return fCachedImage;
205 }
206
207 fCachedImage = this->onNewImageSnapshot();
208
209 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
210 return fCachedImage;
211 }
212
asSB(SkSurface * surface)213 static inline SkSurface_Base* asSB(SkSurface* surface) {
214 return static_cast<SkSurface_Base*>(surface);
215 }
216
asConstSB(const SkSurface * surface)217 static inline const SkSurface_Base* asConstSB(const SkSurface* surface) {
218 return static_cast<const SkSurface_Base*>(surface);
219 }
220
221 #endif
222