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