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 enum SkColorType : int;
27 enum SkYUVColorSpace : int;
28 struct SkIRect;
29 struct SkISize;
30 struct SkImageInfo;
31
32 enum {
33 kNeedNewImageUniqueID = 0
34 };
35
36 namespace skgpu { namespace graphite { class Recorder; } }
37
38 class SkImage_Base : public SkImage {
39 public:
40 ~SkImage_Base() override;
41
42 // From SkImage.h
43 sk_sp<SkImage> makeColorSpace(GrDirectContext*, sk_sp<SkColorSpace>) const override;
44 sk_sp<SkImage> makeColorSpace(skgpu::graphite::Recorder*,
45 sk_sp<SkColorSpace>,
46 RequiredProperties) const override;
47 sk_sp<SkImage> makeColorTypeAndColorSpace(GrDirectContext* dContext,
48 SkColorType targetColorType,
49 sk_sp<SkColorSpace> targetCS) const override;
50 sk_sp<SkImage> makeColorTypeAndColorSpace(skgpu::graphite::Recorder*,
51 SkColorType,
52 sk_sp<SkColorSpace>,
53 RequiredProperties) const override;
54 sk_sp<SkImage> makeSubset(GrDirectContext* direct, const SkIRect& subset) const override;
55 sk_sp<SkImage> makeSubset(skgpu::graphite::Recorder*,
56 const SkIRect&,
57 RequiredProperties) const override;
58
textureSize()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.
onPeekPixels(SkPixmap *)62 virtual bool onPeekPixels(SkPixmap*) const { return false; }
63
onPeekBitmap()64 virtual const SkBitmap* onPeekBitmap() const { return nullptr; }
65
66 virtual bool onReadPixels(GrDirectContext*,
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)
onReadPixelsGraphite(skgpu::graphite::Recorder *,const SkPixmap & dst,int srcX,int srcY)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
onPeekMips()84 virtual SkMipmap* onPeekMips() const { return nullptr; }
85
refMips()86 sk_sp<SkMipmap> refMips() const {
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,
95 RescaleGamma,
96 RescaleMode,
97 ReadPixelsCallback,
98 ReadPixelsContext) const;
99 /**
100 * Default implementation does a rescale/read/yuv conversion and then calls the callback.
101 */
102 virtual void onAsyncRescaleAndReadPixelsYUV420(SkYUVColorSpace,
103 bool readAlpha,
104 sk_sp<SkColorSpace> dstColorSpace,
105 SkIRect srcRect,
106 SkISize dstSize,
107 RescaleGamma,
108 RescaleMode,
109 ReadPixelsCallback,
110 ReadPixelsContext) const;
111
context()112 virtual GrImageContext* context() const { return nullptr; }
113
114 /** this->context() try-casted to GrDirectContext. Useful for migrations – avoid otherwise! */
directContext()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.
generatingSurfaceIsDeleted()119 virtual void generatingSurfaceIsDeleted() {}
120
121 // return a read-only copy of the pixels. We promise to not modify them,
122 // but only inspect them (or encode them).
123 virtual bool getROPixels(GrDirectContext*, SkBitmap*,
124 CachingHint = kAllow_CachingHint) const = 0;
125
126 virtual sk_sp<SkImage> onMakeSubset(GrDirectContext*, const SkIRect&) const = 0;
127
onRefEncoded()128 virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
129
130 virtual bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const;
131
132 enum class Type {
133 kRaster,
134 kRasterPinnable,
135 kLazy,
136 kLazyPicture,
137 kGanesh,
138 kGaneshYUVA,
139 kGraphite,
140 kGraphiteYUVA,
141 };
142
143 virtual Type type() const = 0;
144
145 // True for picture-backed and codec-backed
isLazyGenerated()146 bool isLazyGenerated() const override {
147 return this->type() == Type::kLazy || this->type() == Type::kLazyPicture;
148 }
149
isRasterBacked()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
isGaneshBacked()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
isGraphiteBacked()160 bool isGraphiteBacked() const {
161 return this->type() == Type::kGraphite || this->type() == Type::kGraphiteYUVA;
162 }
163
isYUVA()164 bool isYUVA() const {
165 return this->type() == Type::kGaneshYUVA || this->type() == Type::kGraphiteYUVA;
166 }
167
isTextureBacked()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.
notifyAddedToRasterCache()174 virtual void notifyAddedToRasterCache() const {
175 fAddedToRasterCache.store(true);
176 }
177
178 virtual sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>,
179 GrDirectContext*) const = 0;
180
181 virtual sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const = 0;
182
183 // on failure, returns nullptr
184 // NOLINTNEXTLINE(performance-unnecessary-value-param)
onMakeWithMipmaps(sk_sp<SkMipmap>)185 virtual sk_sp<SkImage> onMakeWithMipmaps(sk_sp<SkMipmap>) const {
186 return nullptr;
187 }
188
189 virtual sk_sp<SkImage> onMakeSubset(skgpu::graphite::Recorder*,
190 const SkIRect&,
191 RequiredProperties) const = 0;
192
193 protected:
194 SkImage_Base(const SkImageInfo& info, uint32_t uniqueID);
195
196 private:
197 // Set true by caches when they cache content that's derived from the current pixels.
198 mutable std::atomic<bool> fAddedToRasterCache;
199 };
200
as_IB(SkImage * image)201 static inline SkImage_Base* as_IB(SkImage* image) {
202 return static_cast<SkImage_Base*>(image);
203 }
204
as_IB(const sk_sp<SkImage> & image)205 static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
206 return static_cast<SkImage_Base*>(image.get());
207 }
208
as_IB(const SkImage * image)209 static inline const SkImage_Base* as_IB(const SkImage* image) {
210 return static_cast<const SkImage_Base*>(image);
211 }
212
as_IB(const sk_sp<const SkImage> & image)213 static 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
218