Flutter Engine
The Flutter Engine
SkSurface_Base.h
Go to the documentation of this file.
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
18
19#include <cstdint>
20#include <memory>
21
25class SkCapabilities;
26class SkColorSpace;
27class SkPaint;
28class SkPixmap;
30class SkSurfaceProps;
31enum GrSurfaceOrigin : int;
32enum SkYUVColorSpace : int;
33namespace skgpu { namespace graphite { class Recorder; } }
34struct SkIRect;
35struct SkISize;
36struct SkImageInfo;
37
38class SkSurface_Base : public SkSurface {
39public:
40 SkSurface_Base(int width, int height, const SkSurfaceProps*);
42 ~SkSurface_Base() override;
43
44 // From SkSurface.h
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.
63 virtual Type type() const { return Type::kNull; }
64
65 // True for surfaces instantiated by pixels in CPU memory
66 bool isRasterBacked() const { return this->type() == Type::kRaster; }
67 // True for surfaces instantiated by Ganesh in GPU memory
68 bool isGaneshBacked() const { return this->type() == Type::kGanesh; }
69 // True for surfaces instantiated by Graphite in GPU memory
70 bool isGraphiteBacked() const { return this->type() == Type::kGraphite; }
71
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
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,
107 /**
108 * Default implementation does a rescale/read/yuv conversion and then calls the callback.
109 */
111 bool readAlpha,
112 sk_sp<SkColorSpace> dstColorSpace,
113 SkIRect srcRect,
114 SkISize dstSize,
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 */
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 */
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 */
157 virtual bool onWait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
158 bool deleteSemaphoresAfterWait) {
159 return false;
160 }
161
162 virtual bool onCharacterize(GrSurfaceCharacterization*) const { return false; }
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).
168
169 inline SkCanvas* getCachedCanvas();
171
172 bool hasCachedImage() const { return fCachedImage != nullptr; }
173
174 // called by SkSurface to compute a new genID
175 uint32_t newGenerationID();
176
177private:
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
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
203 if (fCachedImage) {
204 return fCachedImage;
205 }
206
207 fCachedImage = this->onNewImageSnapshot();
208
209 SkASSERT(!fCachedCanvas || fCachedCanvas->getSurfaceBase() == this);
210 return fCachedImage;
211}
212
214 return static_cast<SkSurface_Base*>(surface);
215}
216
217static inline const SkSurface_Base* asConstSB(const SkSurface* surface) {
218 return static_cast<const SkSurface_Base*>(surface);
219}
220
221#endif
GrSurfaceOrigin
Definition: GrTypes.h:147
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkYUVColorSpace
Definition: SkImageInfo.h:68
static const SkSurface_Base * asConstSB(const SkSurface *surface)
static SkSurface_Base * asSB(SkSurface *surface)
RescaleMode
Definition: SkImage.h:587
RescaleGamma
Definition: SkImage.h:585
virtual GrRecordingContext * onGetRecordingContext() const
virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, bool readAlpha, sk_sp< SkColorSpace > dstColorSpace, SkIRect srcRect, SkISize dstSize, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext)
bool isGraphiteBacked() const
virtual bool onCharacterize(GrSurfaceCharacterization *) const
virtual sk_sp< SkImage > onNewImageSnapshot(const SkIRect *subset=nullptr)
virtual SkCanvas * onNewCanvas()=0
virtual skgpu::graphite::Recorder * onGetRecorder() const
sk_sp< SkImage > refCachedImage()
virtual sk_sp< const SkCapabilities > onCapabilities()
virtual sk_sp< SkSurface > onNewSurface(const SkImageInfo &)=0
uint32_t newGenerationID()
~SkSurface_Base() override
SkSurface_Base(int width, int height, const SkSurfaceProps *)
virtual bool onWait(int numSemaphores, const GrBackendSemaphore *waitSemaphores, bool deleteSemaphoresAfterWait)
virtual void onWritePixels(const SkPixmap &, int x, int y)=0
virtual bool onCopyOnWrite(ContentChangeMode)=0
virtual bool onIsCompatible(const GrSurfaceCharacterization &) const
virtual void onDiscard()
virtual void onRestoreBackingMutability()
SkCanvas * getCachedCanvas()
virtual void onDraw(SkCanvas *, SkScalar x, SkScalar y, const SkSamplingOptions &, const SkPaint *)
bool isRasterBacked() const
virtual void onAsyncRescaleAndReadPixels(const SkImageInfo &, const SkIRect srcRect, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext)
bool isGaneshBacked() const
virtual Type type() const
bool hasCachedImage() const
bool replaceBackendTexture(const GrBackendTexture &, GrSurfaceOrigin, ContentChangeMode, TextureReleaseProc, ReleaseContext) override
ContentChangeMode
Definition: SkSurface.h:203
void(ReadPixelsContext, std::unique_ptr< const AsyncReadResult >) ReadPixelsCallback
Definition: SkSurface.h:469
int width() const
Definition: SkSurface.h:178
int height() const
Definition: SkSurface.h:184
void(*)(ReleaseContext) TextureReleaseProc
Definition: SkSurface.h:251
void * ReleaseContext
Definition: SkSurface.h:249
void * ReadPixelsContext
Definition: SkSurface.h:464
@ kRaster
Suitable for thread which raster data.
Definition: embedder.h:266
VkSurfaceKHR surface
Definition: main.cc:49
float SkScalar
Definition: extension.cpp:12
double y
double x
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
Definition: GpuTools.h:21
Definition: SkRect.h:32
Definition: SkSize.h:16