• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SK_API 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     // tell skia try to cache gpu resource when texture resource create.
hintCacheGpuResource()129     virtual void hintCacheGpuResource() {}
130 
131     // return a read-only copy of the pixels. We promise to not modify them,
132     // but only inspect them (or encode them).
133     virtual bool getROPixels(GrDirectContext*, SkBitmap*,
134                              CachingHint = kAllow_CachingHint) const = 0;
135 
136     virtual sk_sp<SkImage> onMakeSubset(GrDirectContext*, const SkIRect&) const = 0;
137 
onRefEncoded()138     virtual sk_sp<SkData> onRefEncoded() const { return nullptr; }
139 
140     virtual bool onAsLegacyBitmap(GrDirectContext*, SkBitmap*) const;
141 
142     enum class Type {
143         kRaster,
144         kRasterPinnable,
145         kLazy,
146         kLazyPicture,
147         kGanesh,
148         kGaneshYUVA,
149         kGraphite,
150         kGraphiteYUVA,
151     };
152 
153     virtual Type type() const = 0;
154 
155     // True for picture-backed and codec-backed
isLazyGenerated()156     bool isLazyGenerated() const override {
157         return this->type() == Type::kLazy || this->type() == Type::kLazyPicture;
158     }
159 
isRasterBacked()160     bool isRasterBacked() const {
161         return this->type() == Type::kRaster || this->type() == Type::kRasterPinnable;
162     }
163 
164     // True for images instantiated by Ganesh in GPU memory
isGaneshBacked()165     bool isGaneshBacked() const {
166         return this->type() == Type::kGanesh || this->type() == Type::kGaneshYUVA;
167     }
168 
169     // True for images instantiated by Graphite in GPU memory
isGraphiteBacked()170     bool isGraphiteBacked() const {
171         return this->type() == Type::kGraphite || this->type() == Type::kGraphiteYUVA;
172     }
173 
isYUVA()174     bool isYUVA() const {
175         return this->type() == Type::kGaneshYUVA || this->type() == Type::kGraphiteYUVA;
176     }
177 
isTextureBacked()178     bool isTextureBacked() const override {
179         return this->isGaneshBacked() || this->isGraphiteBacked();
180     }
181 
182     // Call when this image is part of the key to a resourcecache entry. This allows the cache
183     // to know automatically those entries can be purged when this SkImage deleted.
notifyAddedToRasterCache()184     virtual void notifyAddedToRasterCache() const {
185         fAddedToRasterCache.store(true);
186     }
187 
188     virtual sk_sp<SkImage> onMakeColorTypeAndColorSpace(SkColorType, sk_sp<SkColorSpace>,
189                                                         GrDirectContext*) const = 0;
190 
191     virtual sk_sp<SkImage> onReinterpretColorSpace(sk_sp<SkColorSpace>) const = 0;
192 
193     // on failure, returns nullptr
194     // NOLINTNEXTLINE(performance-unnecessary-value-param)
onMakeWithMipmaps(sk_sp<SkMipmap>)195     virtual sk_sp<SkImage> onMakeWithMipmaps(sk_sp<SkMipmap>) const {
196         return nullptr;
197     }
198 
199     virtual sk_sp<SkImage> onMakeSubset(skgpu::graphite::Recorder*,
200                                         const SkIRect&,
201                                         RequiredProperties) const = 0;
202 
203 protected:
204     SkImage_Base(const SkImageInfo& info, uint32_t uniqueID);
205 
206 private:
207     // Set true by caches when they cache content that's derived from the current pixels.
208     mutable std::atomic<bool> fAddedToRasterCache;
209 };
210 
as_IB(SkImage * image)211 static inline SkImage_Base* as_IB(SkImage* image) {
212     return static_cast<SkImage_Base*>(image);
213 }
214 
as_IB(const sk_sp<SkImage> & image)215 static inline SkImage_Base* as_IB(const sk_sp<SkImage>& image) {
216     return static_cast<SkImage_Base*>(image.get());
217 }
218 
as_IB(const SkImage * image)219 static inline const SkImage_Base* as_IB(const SkImage* image) {
220     return static_cast<const SkImage_Base*>(image);
221 }
222 
as_IB(const sk_sp<const SkImage> & image)223 static inline const SkImage_Base* as_IB(const sk_sp<const SkImage>& image) {
224     return static_cast<const SkImage_Base*>(image.get());
225 }
226 
227 #endif
228