Flutter Engine
The Flutter Engine
SkSurface.cpp
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
9
12#include "include/core/SkCapabilities.h" // IWYU pragma: keep
17#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
26
27#include <cstddef>
28#include <cstdint>
29#include <utility>
30
32class GrRecordingContext; // IWYU pragma: keep
33class SkPaint;
35namespace skgpu { namespace graphite { class Recorder; } }
36
38 : fFlags(0)
39 , fPixelGeometry(kUnknown_SkPixelGeometry)
40 , fTextContrast(SK_GAMMA_CONTRAST)
41 , fTextGamma(SK_GAMMA_EXPONENT) {}
42
44 : fFlags(flags)
45 , fPixelGeometry(pg)
46 , fTextContrast(SK_GAMMA_CONTRAST)
47 , fTextGamma(SK_GAMMA_EXPONENT) {}
48
51 SkScalar textContrast,
52 SkScalar textGamma)
53 : fFlags(flags), fPixelGeometry(pg), fTextContrast(textContrast), fTextGamma(textGamma) {}
54
56 : fProps(SkSurfacePropsCopyOrDefault(props)), fWidth(width), fHeight(height)
57{
58 SkASSERT(fWidth > 0);
59 SkASSERT(fHeight > 0);
60 fGenerationID = 0;
61}
62
64 : fProps(SkSurfacePropsCopyOrDefault(props)), fWidth(info.width()), fHeight(info.height())
65{
66 SkASSERT(fWidth > 0);
67 SkASSERT(fHeight > 0);
68 fGenerationID = 0;
69}
70
72 if (0 == fGenerationID) {
73 fGenerationID = asSB(this)->newGenerationID();
74 }
75 return fGenerationID;
76}
77
79 sk_ignore_unused_variable(asSB(this)->aboutToDraw(mode));
80}
81
83 return asSB(this)->getCachedCanvas();
84}
85
87 return asSB(this)->onCapabilities();
88}
89
91 return asSB(this)->refCachedImage();
92}
93
95 const SkIRect surfBounds = { 0, 0, fWidth, fHeight };
96 SkIRect bounds = srcBounds;
97 if (!bounds.intersect(surfBounds)) {
98 return nullptr;
99 }
100 SkASSERT(!bounds.isEmpty());
101 if (bounds == surfBounds) {
102 return this->makeImageSnapshot();
103 } else {
104 return asSB(this)->onNewImageSnapshot(&bounds);
105 }
106}
107
109 return asSB(this)->onNewSurface(info);
110}
111
113 return this->makeSurface(this->imageInfo().makeWH(width, height));
114}
115
117 const SkPaint* paint) {
118 asSB(this)->onDraw(canvas, x, y, sampling, paint);
119}
120
122 return this->getCanvas()->peekPixels(pmap);
123}
124
125bool SkSurface::readPixels(const SkPixmap& pm, int srcX, int srcY) {
126 return this->getCanvas()->readPixels(pm, srcX, srcY);
127}
128
129bool SkSurface::readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
130 int srcX, int srcY) {
131 return this->readPixels({dstInfo, dstPixels, dstRowBytes}, srcX, srcY);
132}
133
134bool SkSurface::readPixels(const SkBitmap& bitmap, int srcX, int srcY) {
135 SkPixmap pm;
136 return bitmap.peekPixels(&pm) && this->readPixels(pm, srcX, srcY);
137}
138
140 const SkIRect& srcRect,
141 RescaleGamma rescaleGamma,
142 RescaleMode rescaleMode,
144 ReadPixelsContext context) {
145 if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) ||
147 callback(context, nullptr);
148 return;
149 }
151 info, srcRect, rescaleGamma, rescaleMode, callback, context);
152}
153
155 sk_sp<SkColorSpace> dstColorSpace,
156 const SkIRect& srcRect,
157 const SkISize& dstSize,
158 RescaleGamma rescaleGamma,
159 RescaleMode rescaleMode,
161 ReadPixelsContext context) {
162 if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || dstSize.isZero() ||
163 (dstSize.width() & 0b1) || (dstSize.height() & 0b1)) {
164 callback(context, nullptr);
165 return;
166 }
167 asSB(this)->onAsyncRescaleAndReadPixelsYUV420(yuvColorSpace,
168 /*readAlpha=*/false,
169 std::move(dstColorSpace),
170 srcRect,
171 dstSize,
172 rescaleGamma,
173 rescaleMode,
174 callback,
175 context);
176}
177
179 sk_sp<SkColorSpace> dstColorSpace,
180 const SkIRect& srcRect,
181 const SkISize& dstSize,
182 RescaleGamma rescaleGamma,
183 RescaleMode rescaleMode,
185 ReadPixelsContext context) {
186 if (!SkIRect::MakeWH(this->width(), this->height()).contains(srcRect) || dstSize.isZero() ||
187 (dstSize.width() & 0b1) || (dstSize.height() & 0b1)) {
188 callback(context, nullptr);
189 return;
190 }
191 asSB(this)->onAsyncRescaleAndReadPixelsYUV420(yuvColorSpace,
192 /*readAlpha=*/true,
193 std::move(dstColorSpace),
194 srcRect,
195 dstSize,
196 rescaleGamma,
197 rescaleMode,
198 callback,
199 context);
200}
201
202void SkSurface::writePixels(const SkPixmap& pmap, int x, int y) {
203 if (pmap.addr() == nullptr || pmap.width() <= 0 || pmap.height() <= 0) {
204 return;
205 }
206
207 const SkIRect srcR = SkIRect::MakeXYWH(x, y, pmap.width(), pmap.height());
208 const SkIRect dstR = SkIRect::MakeWH(this->width(), this->height());
209 if (SkIRect::Intersects(srcR, dstR)) {
211 if (srcR.contains(dstR)) {
213 }
214 if (!asSB(this)->aboutToDraw(mode)) {
215 return;
216 }
217 asSB(this)->onWritePixels(pmap, x, y);
218 }
219}
220
221void SkSurface::writePixels(const SkBitmap& src, int x, int y) {
222 SkPixmap pm;
223 if (src.peekPixels(&pm)) {
224 this->writePixels(pm, x, y);
225 }
226}
227
229 return asConstSB(this)->onGetRecordingContext();
230}
231
233
234bool SkSurface::wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores,
235 bool deleteSemaphoresAfterWait) {
236 return asSB(this)->onWait(numSemaphores, waitSemaphores, deleteSemaphoresAfterWait);
237}
238
240 return asConstSB(this)->onCharacterize(characterization);
241}
242
243bool SkSurface::isCompatible(const GrSurfaceCharacterization& characterization) const {
244 return asConstSB(this)->onIsCompatible(characterization);
245}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
uint16_t fFlags
Definition: ShapeLayer.cpp:106
#define SkASSERT(cond)
Definition: SkAssert.h:116
static bool SkImageInfoIsValid(const SkImageInfo &info)
SkYUVColorSpace
Definition: SkImageInfo.h:68
static SkSurfaceProps SkSurfacePropsCopyOrDefault(const SkSurfaceProps *props)
Definition: SkSurfacePriv.h:15
SkPixelGeometry
@ kUnknown_SkPixelGeometry
static const SkSurface_Base * asConstSB(const SkSurface *surface)
static SkSurface_Base * asSB(SkSurface *surface)
void sk_ignore_unused_variable(const T &)
Definition: SkTemplates.h:37
#define SK_GAMMA_CONTRAST
Definition: SkTypes.h:94
#define SK_GAMMA_EXPONENT
Definition: SkTypes.h:86
bool peekPixels(SkPixmap *pixmap)
Definition: SkCanvas.cpp:1237
bool readPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY)
Definition: SkCanvas.cpp:382
RescaleMode
Definition: SkImage.h:587
RescaleGamma
Definition: SkImage.h:585
int width() const
Definition: SkPixmap.h:160
const void * addr() const
Definition: SkPixmap.h:153
int height() const
Definition: SkPixmap.h:166
virtual GrRecordingContext * onGetRecordingContext() const
virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, bool readAlpha, sk_sp< SkColorSpace > dstColorSpace, SkIRect srcRect, SkISize dstSize, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext)
virtual bool onCharacterize(GrSurfaceCharacterization *) const
virtual sk_sp< SkImage > onNewImageSnapshot(const SkIRect *subset=nullptr)
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()
virtual bool onWait(int numSemaphores, const GrBackendSemaphore *waitSemaphores, bool deleteSemaphoresAfterWait)
virtual void onWritePixels(const SkPixmap &, int x, int y)=0
virtual bool onIsCompatible(const GrSurfaceCharacterization &) const
SkCanvas * getCachedCanvas()
virtual void onDraw(SkCanvas *, SkScalar x, SkScalar y, const SkSamplingOptions &, const SkPaint *)
virtual void onAsyncRescaleAndReadPixels(const SkImageInfo &, const SkIRect srcRect, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext)
ContentChangeMode
Definition: SkSurface.h:203
@ kDiscard_ContentChangeMode
discards surface on change
Definition: SkSurface.h:204
@ kRetain_ContentChangeMode
preserves surface on change
Definition: SkSurface.h:205
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
void(ReadPixelsContext, std::unique_ptr< const AsyncReadResult >) ReadPixelsCallback
Definition: SkSurface.h:469
int width() const
Definition: SkSurface.h:178
GrRecordingContext * recordingContext() const
Definition: SkSurface.cpp:228
sk_sp< const SkCapabilities > capabilities()
Definition: SkSurface.cpp:86
bool characterize(GrSurfaceCharacterization *characterization) const
Definition: SkSurface.cpp:239
sk_sp< SkSurface > makeSurface(const SkImageInfo &imageInfo)
Definition: SkSurface.cpp:108
void draw(SkCanvas *canvas, SkScalar x, SkScalar y, const SkSamplingOptions &sampling, const SkPaint *paint)
Definition: SkSurface.cpp:116
void asyncRescaleAndReadPixels(const SkImageInfo &info, const SkIRect &srcRect, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context)
Definition: SkSurface.cpp:139
bool isCompatible(const GrSurfaceCharacterization &characterization) const
Definition: SkSurface.cpp:243
virtual SkImageInfo imageInfo() const
Definition: SkSurface.h:188
bool peekPixels(SkPixmap *pixmap)
Definition: SkSurface.cpp:121
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
int height() const
Definition: SkSurface.h:184
void asyncRescaleAndReadPixelsYUVA420(SkYUVColorSpace yuvColorSpace, sk_sp< SkColorSpace > dstColorSpace, const SkIRect &srcRect, const SkISize &dstSize, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context)
Definition: SkSurface.cpp:178
void asyncRescaleAndReadPixelsYUV420(SkYUVColorSpace yuvColorSpace, sk_sp< SkColorSpace > dstColorSpace, const SkIRect &srcRect, const SkISize &dstSize, RescaleGamma rescaleGamma, RescaleMode rescaleMode, ReadPixelsCallback callback, ReadPixelsContext context)
Definition: SkSurface.cpp:154
SkSurface(int width, int height, const SkSurfaceProps *surfaceProps)
Definition: SkSurface.cpp:55
void writePixels(const SkPixmap &src, int dstX, int dstY)
Definition: SkSurface.cpp:202
void notifyContentWillChange(ContentChangeMode mode)
Definition: SkSurface.cpp:78
bool wait(int numSemaphores, const GrBackendSemaphore *waitSemaphores, bool deleteSemaphoresAfterWait=true)
Definition: SkSurface.cpp:234
skgpu::graphite::Recorder * recorder() const
Definition: SkSurface.cpp:232
bool readPixels(const SkPixmap &dst, int srcX, int srcY)
Definition: SkSurface.cpp:125
void * ReadPixelsContext
Definition: SkSurface.h:464
uint32_t generationID()
Definition: SkSurface.cpp:71
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
FlutterSemanticsFlag flags
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
double y
double x
Optional< SkRect > bounds
Definition: SkRecords.h:189
SkSamplingOptions sampling
Definition: SkRecords.h:337
Definition: bitmap.py:1
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
constexpr bool contains(std::string_view str, std::string_view needle)
Definition: SkStringView.h:41
int32_t height
int32_t width
Definition: SkRect.h:32
static bool Intersects(const SkIRect &a, const SkIRect &b)
Definition: SkRect.h:535
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition: SkRect.h:56
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: SkRect.h:104
bool contains(int32_t x, int32_t y) const
Definition: SkRect.h:463
Definition: SkSize.h:16
constexpr int32_t width() const
Definition: SkSize.h:36
constexpr int32_t height() const
Definition: SkSize.h:37
bool isZero() const
Definition: SkSize.h:28