• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 SkImageGenerator_DEFINED
9 #define SkImageGenerator_DEFINED
10 
11 #include "include/core/SkBitmap.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkSurfaceProps.h"
16 #include "include/core/SkYUVAPixmaps.h"
17 
18 #include <memory>
19 #include <optional>
20 
21 class GrRecordingContext;
22 class GrSurfaceProxyView;
23 class GrSamplerState;
24 class SkBitmap;
25 class SkData;
26 class SkMatrix;
27 class SkPaint;
28 class SkPicture;
29 
30 enum class GrImageTexGenPolicy : int;
31 
32 class SK_API SkImageGenerator {
33 public:
34     /**
35      *  The PixelRef which takes ownership of this SkImageGenerator
36      *  will call the image generator's destructor.
37      */
~SkImageGenerator()38     virtual ~SkImageGenerator() { }
39 
uniqueID()40     uint32_t uniqueID() const { return fUniqueID; }
41 
42     /**
43      *  Return a ref to the encoded (i.e. compressed) representation
44      *  of this data.
45      *
46      *  If non-NULL is returned, the caller is responsible for calling
47      *  unref() on the data when it is finished.
48      */
refEncodedData()49     sk_sp<SkData> refEncodedData() {
50         return this->onRefEncodedData();
51     }
52 
53     /**
54      *  Return the ImageInfo associated with this generator.
55      */
getInfo()56     const SkImageInfo& getInfo() const { return fInfo; }
57 
58     /**
59      *  Can this generator be used to produce images that will be drawable to the specified context
60      *  (or to CPU, if context is nullptr)?
61      */
isValid(GrRecordingContext * context)62     bool isValid(GrRecordingContext* context) const {
63         return this->onIsValid(context);
64     }
65 
66     /**
67      *  Decode into the given pixels, a block of memory of size at
68      *  least (info.fHeight - 1) * rowBytes + (info.fWidth *
69      *  bytesPerPixel)
70      *
71      *  Repeated calls to this function should give the same results,
72      *  allowing the PixelRef to be immutable.
73      *
74      *  @param info A description of the format
75      *         expected by the caller.  This can simply be identical
76      *         to the info returned by getInfo().
77      *
78      *         This contract also allows the caller to specify
79      *         different output-configs, which the implementation can
80      *         decide to support or not.
81      *
82      *         A size that does not match getInfo() implies a request
83      *         to scale. If the generator cannot perform this scale,
84      *         it will return false.
85      *
86      *  @return true on success.
87      */
88     bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
89 
getPixels(const SkPixmap & pm)90     bool getPixels(const SkPixmap& pm) {
91         return this->getPixels(pm.info(), pm.writable_addr(), pm.rowBytes());
92     }
93 
94     /**
95      *  If decoding to YUV is supported, this returns true. Otherwise, this
96      *  returns false and the caller will ignore output parameter yuvaPixmapInfo.
97      *
98      * @param  supportedDataTypes Indicates the data type/planar config combinations that are
99      *                            supported by the caller. If the generator supports decoding to
100      *                            YUV(A), but not as a type in supportedDataTypes, this method
101      *                            returns false.
102      *  @param yuvaPixmapInfo Output parameter that specifies the planar configuration, subsampling,
103      *                        orientation, chroma siting, plane color types, and row bytes.
104      */
105     bool queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
106                        SkYUVAPixmapInfo* yuvaPixmapInfo) const;
107 
108     /**
109      *  Returns true on success and false on failure.
110      *  This always attempts to perform a full decode. To get the planar
111      *  configuration without decoding use queryYUVAInfo().
112      *
113      *  @param yuvaPixmaps  Contains preallocated pixmaps configured according to a successful call
114      *                      to queryYUVAInfo().
115      */
116     bool getYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps);
117 
118 #if defined(SK_GANESH)
119     /**
120      *  If the generator can natively/efficiently return its pixels as a GPU image (backed by a
121      *  texture) this will return that image. If not, this will return NULL.
122      *
123      *  Regarding the GrRecordingContext parameter:
124      *
125      *  It must be non-NULL. The generator should only succeed if:
126      *  - its internal context is the same
127      *  - it can somehow convert its texture into one that is valid for the provided context.
128      *
129      *  If the mipmapped parameter is kYes, the generator should try to create a TextureProxy that
130      *  at least has the mip levels allocated and the base layer filled in. If this is not possible,
131      *  the generator is allowed to return a non mipped proxy, but this will have some additional
132      *  overhead in later allocating mips and copying of the base layer.
133      *
134      *  GrImageTexGenPolicy determines whether or not a new texture must be created (and its budget
135      *  status) or whether this may (but is not required to) return a pre-existing texture that is
136      *  retained by the generator (kDraw).
137      */
138     GrSurfaceProxyView generateTexture(GrRecordingContext*,
139                                        const SkImageInfo& info,
140                                        skgpu::Mipmapped mipmapped,
141                                        GrImageTexGenPolicy);
142 #endif
143 
144 #if SK_GRAPHITE
145     sk_sp<SkImage> makeTextureImage(skgpu::graphite::Recorder*,
146                                     const SkImageInfo&,
147                                     skgpu::Mipmapped);
148 #endif
149 
150     /**
151      *  If the default image decoder system can interpret the specified (encoded) data, then
152      *  this returns a new ImageGenerator for it. Otherwise this returns NULL. Either way
153      *  the caller is still responsible for managing their ownership of the data.
154      *  By default, images will be converted to premultiplied pixels. The alpha type can be
155      *  overridden by specifying kPremul_SkAlphaType or kUnpremul_SkAlphaType. Specifying
156      *  kOpaque_SkAlphaType is not supported, and will return NULL.
157      */
158     static std::unique_ptr<SkImageGenerator> MakeFromEncoded(
159             sk_sp<SkData>, std::optional<SkAlphaType> = std::nullopt);
160 
161     /** Return a new image generator backed by the specified picture.  If the size is empty or
162      *  the picture is NULL, this returns NULL.
163      *  The optional matrix and paint arguments are passed to drawPicture() at rasterization
164      *  time.
165      */
166     static std::unique_ptr<SkImageGenerator> MakeFromPicture(const SkISize&, sk_sp<SkPicture>,
167                                                              const SkMatrix*, const SkPaint*,
168                                                              SkImage::BitDepth,
169                                                              sk_sp<SkColorSpace>,
170                                                              SkSurfaceProps props = {});
171 
172 protected:
173     static constexpr int kNeedNewImageUniqueID = 0;
174 
175     SkImageGenerator(const SkImageInfo& info, uint32_t uniqueId = kNeedNewImageUniqueID);
176 
onRefEncodedData()177     virtual sk_sp<SkData> onRefEncodedData() { return nullptr; }
178     struct Options {};
onGetPixels(const SkImageInfo &,void *,size_t,const Options &)179     virtual bool onGetPixels(const SkImageInfo&, void*, size_t, const Options&) { return false; }
onIsValid(GrRecordingContext *)180     virtual bool onIsValid(GrRecordingContext*) const { return true; }
onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes &,SkYUVAPixmapInfo *)181     virtual bool onQueryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes&,
182                                  SkYUVAPixmapInfo*) const { return false; }
onGetYUVAPlanes(const SkYUVAPixmaps &)183     virtual bool onGetYUVAPlanes(const SkYUVAPixmaps&) { return false; }
184 #if defined(SK_GANESH)
185     // returns nullptr
186     virtual GrSurfaceProxyView onGenerateTexture(GrRecordingContext*, const SkImageInfo&,
187                                                  GrMipmapped, GrImageTexGenPolicy);
188 
189     // Most internal SkImageGenerators produce textures and views that use kTopLeft_GrSurfaceOrigin.
190     // If the generator may produce textures with different origins (e.g.
191     // GrAHardwareBufferImageGenerator) it should override this function to return the correct
192     // origin.
origin()193     virtual GrSurfaceOrigin origin() const { return kTopLeft_GrSurfaceOrigin; }
194 #endif
195 
196 #if SK_GRAPHITE
197     virtual sk_sp<SkImage> onMakeTextureImage(skgpu::graphite::Recorder*,
198                                               const SkImageInfo&,
199                                               skgpu::Mipmapped);
200 #endif
201 
202 private:
203     const SkImageInfo fInfo;
204     const uint32_t fUniqueID;
205 
206     friend class SkImage_Lazy;
207 
208     // This is our default impl, which may be different on different platforms.
209     // It is called from NewFromEncoded() after it has checked for any runtime factory.
210     // The SkData will never be NULL, as that will have been checked by NewFromEncoded.
211     static std::unique_ptr<SkImageGenerator> MakeFromEncodedImpl(sk_sp<SkData>,
212                                                                  std::optional<SkAlphaType>);
213 
214     SkImageGenerator(SkImageGenerator&&) = delete;
215     SkImageGenerator(const SkImageGenerator&) = delete;
216     SkImageGenerator& operator=(SkImageGenerator&&) = delete;
217     SkImageGenerator& operator=(const SkImageGenerator&) = delete;
218 };
219 
220 #endif  // SkImageGenerator_DEFINED
221