Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkImage_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 SkImage_Base_DEFINED
9#define SkImage_Base_DEFINED
10
11#include "include/core/SkData.h"
15#include "src/core/SkMipmap.h"
16
17#include <atomic>
18#include <cstddef>
19#include <cstdint>
20
21class GrDirectContext;
22class GrImageContext;
23class SkBitmap;
24class SkColorSpace;
25class SkPixmap;
26enum SkColorType : int;
27enum SkYUVColorSpace : int;
28struct SkIRect;
29struct SkISize;
30struct SkImageInfo;
31
32enum {
34};
35
36namespace skgpu { namespace graphite { class Recorder; } }
37
38class SkImage_Base : public SkImage {
39public:
40 ~SkImage_Base() override;
41
42 // From SkImage.h
46 RequiredProperties) const override;
48 SkColorType targetColorType,
49 sk_sp<SkColorSpace> targetCS) const override;
53 RequiredProperties) const override;
54 sk_sp<SkImage> makeSubset(GrDirectContext* direct, const SkIRect& subset) const override;
56 const SkIRect&,
57 RequiredProperties) const override;
58
59 size_t textureSize() const override { return 0; }
60
61 // Methods that we want to use elsewhere in Skia, but not be a part of the public API.
62 virtual bool onPeekPixels(SkPixmap*) const { return false; }
63
64 virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
65
67 const SkImageInfo& dstInfo,
68 void* dstPixels,
69 size_t dstRowBytes,
70 int srcX,
71 int srcY,
72 CachingHint) const = 0;
73
74#if defined(GRAPHITE_TEST_UTILS)
75 virtual bool onReadPixelsGraphite(skgpu::graphite::Recorder*,
76 const SkPixmap& dst,
77 int srcX,
78 int srcY) const { return false; }
79#endif
80
81 virtual bool onHasMipmaps() const = 0;
82 virtual bool onIsProtected() const = 0;
83
84 virtual SkMipmap* onPeekMips() const { return nullptr; }
85
87 return sk_ref_sp(this->onPeekMips());
88 }
89
90 /**
91 * Default implementation does a rescale/read and then calls the callback.
92 */
93 virtual void onAsyncRescaleAndReadPixels(const SkImageInfo&,
94 SkIRect srcRect,
98 ReadPixelsContext) const;
99 /**
100 * Default implementation does a rescale/read/yuv conversion and then calls the callback.
101 */
103 bool readAlpha,
104 sk_sp<SkColorSpace> dstColorSpace,
105 SkIRect srcRect,
106 SkISize dstSize,
110 ReadPixelsContext) const;
111
112 virtual GrImageContext* context() const { return nullptr; }
113
114 /** this->context() try-casted to GrDirectContext. Useful for migrations – avoid otherwise! */
115 virtual GrDirectContext* directContext() const { return nullptr; }
116
117 // If this image is the current cached image snapshot of a surface then this is called when the
118 // surface is destroyed to indicate no further writes may happen to surface backing store.
120
121 // return a read-only copy of the pixels. We promise to not modify them,
122 // but only inspect them (or encode them).
124 CachingHint = kAllow_CachingHint) const = 0;
125
127
128 virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
129
130 virtual bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const;
131
132 enum class Type {
133 kRaster,
135 kLazy,
137 kGanesh,
139 kGraphite,
141 };
142
143 virtual Type type() const = 0;
144
145 // True for picture-backed and codec-backed
146 bool isLazyGenerated() const override {
147 return this->type() == Type::kLazy || this->type() == Type::kLazyPicture;
148 }
149
150 bool isRasterBacked() const {
151 return this->type() == Type::kRaster || this->type() == Type::kRasterPinnable;
152 }
153
154 // True for images instantiated by Ganesh in GPU memory
155 bool isGaneshBacked() const {
156 return this->type() == Type::kGanesh || this->type() == Type::kGaneshYUVA;
157 }
158
159 // True for images instantiated by Graphite in GPU memory
160 bool isGraphiteBacked() const {
161 return this->type() == Type::kGraphite || this->type() == Type::kGraphiteYUVA;
162 }
163
164 bool isYUVA() const {
165 return this->type() == Type::kGaneshYUVA || this->type() == Type::kGraphiteYUVA;
166 }
167
168 bool isTextureBacked() const override {
169 return this->isGaneshBacked() || this->isGraphiteBacked();
170 }
171
172 // Call when this image is part of the key to a resourcecache entry. This allows the cache
173 // to know automatically those entries can be purged when this SkImage deleted.
174 virtual void notifyAddedToRasterCache() const {
175 fAddedToRasterCache.store(true);
176 }
177
179 GrDirectContext*) const = 0;
180
182
183 // on failure, returns nullptr
184 // NOLINTNEXTLINE(performance-unnecessary-value-param)
186 return nullptr;
187 }
188
190 const SkIRect&,
191 RequiredProperties) const = 0;
192
193protected:
194 SkImage_Base(const SkImageInfo& info, uint32_t uniqueID);
195
196private:
197 // Set true by caches when they cache content that's derived from the current pixels.
198 mutable std::atomic<bool> fAddedToRasterCache;
199};
200
201static inline SkImage_Base* as_IB(SkImage* image) {
202 return static_cast<SkImage_Base*>(image);
203}
204
205static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
206 return static_cast<SkImage_Base*>(image.get());
207}
208
209static inline const SkImage_Base* as_IB(const SkImage* image) {
210 return static_cast<const SkImage_Base*>(image);
211}
212
213static inline const SkImage_Base* as_IB(const sk_sp<const SkImage>& image) {
214 return static_cast<const SkImage_Base*>(image.get());
215}
216
217#endif
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
SkColorType
Definition SkColorType.h:19
SkYUVColorSpace
Definition SkImageInfo.h:68
@ kNeedNewImageUniqueID
static SkImage_Base * as_IB(SkImage *image)
sk_sp< T > sk_ref_sp(T *obj)
Definition SkRefCnt.h:381
Type::kYUV Type::kRGBA() int(0.7 *637)
virtual sk_sp< SkImage > onMakeWithMipmaps(sk_sp< SkMipmap >) const
virtual void generatingSurfaceIsDeleted()
sk_sp< SkMipmap > refMips() const
virtual bool onAsLegacyBitmap(GrDirectContext *, SkBitmap *) const
~SkImage_Base() override
bool isYUVA() const
virtual GrDirectContext * directContext() const
bool isRasterBacked() const
virtual sk_sp< SkImage > onMakeSubset(GrDirectContext *, const SkIRect &) const =0
virtual sk_sp< SkImage > onReinterpretColorSpace(sk_sp< SkColorSpace >) const =0
sk_sp< SkImage > makeSubset(GrDirectContext *direct, const SkIRect &subset) const override
virtual sk_sp< SkImage > onMakeSubset(skgpu::graphite::Recorder *, const SkIRect &, RequiredProperties) const =0
virtual GrImageContext * context() const
sk_sp< SkImage > makeColorTypeAndColorSpace(GrDirectContext *dContext, SkColorType targetColorType, sk_sp< SkColorSpace > targetCS) const override
bool isLazyGenerated() const override
virtual sk_sp< SkImage > onMakeColorTypeAndColorSpace(SkColorType, sk_sp< SkColorSpace >, GrDirectContext *) const =0
bool isGaneshBacked() const
virtual bool onReadPixels(GrDirectContext *, const SkImageInfo &dstInfo, void *dstPixels, size_t dstRowBytes, int srcX, int srcY, CachingHint) const =0
virtual sk_sp< SkData > onRefEncoded() const
virtual bool onIsProtected() const =0
virtual SkMipmap * onPeekMips() const
virtual bool getROPixels(GrDirectContext *, SkBitmap *, CachingHint=kAllow_CachingHint) const =0
bool isTextureBacked() const override
sk_sp< SkImage > makeColorSpace(GrDirectContext *, sk_sp< SkColorSpace >) const override
virtual bool onHasMipmaps() const =0
virtual bool onPeekPixels(SkPixmap *) const
virtual void notifyAddedToRasterCache() const
virtual Type type() const =0
virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace, bool readAlpha, sk_sp< SkColorSpace > dstColorSpace, SkIRect srcRect, SkISize dstSize, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext) const
virtual const SkBitmap * onPeekBitmap() const
size_t textureSize() const override
virtual void onAsyncRescaleAndReadPixels(const SkImageInfo &, SkIRect srcRect, RescaleGamma, RescaleMode, ReadPixelsCallback, ReadPixelsContext) const
bool isGraphiteBacked() const
uint32_t uniqueID() const
Definition SkImage.h:311
RescaleMode
Definition SkImage.h:587
RescaleGamma
Definition SkImage.h:585
friend class SkImage_Base
Definition SkImage.h:936
CachingHint
Definition SkImage.h:463
@ kAllow_CachingHint
allows internally caching decoded and copied pixels
Definition SkImage.h:464
T * get() const
Definition SkRefCnt.h:303
sk_sp< SkImage > image
Definition examples.cpp:29
SkImage::ReadPixelsContext ReadPixelsContext
Definition Device.cpp:81
SkImage::ReadPixelsCallback ReadPixelsCallback
Definition Device.cpp:80