Debian Codegraph @

src/image/SkSurface_Base.h

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