• 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 "SkBitmap.h"
12 #include "SkColor.h"
13 #include "SkImage.h"
14 #include "SkImageInfo.h"
15 #include "SkYUVAIndex.h"
16 #include "SkYUVASizeInfo.h"
17 
18 class GrContext;
19 class GrContextThreadSafeProxy;
20 class GrTextureProxy;
21 class GrSamplerState;
22 class SkBitmap;
23 class SkData;
24 class SkMatrix;
25 class SkPaint;
26 class SkPicture;
27 
28 class SK_API SkImageGenerator {
29 public:
30     /**
31      *  The PixelRef which takes ownership of this SkImageGenerator
32      *  will call the image generator's destructor.
33      */
~SkImageGenerator()34     virtual ~SkImageGenerator() { }
35 
uniqueID()36     uint32_t uniqueID() const { return fUniqueID; }
37 
38     /**
39      *  Return a ref to the encoded (i.e. compressed) representation
40      *  of this data.
41      *
42      *  If non-NULL is returned, the caller is responsible for calling
43      *  unref() on the data when it is finished.
44      */
refEncodedData()45     sk_sp<SkData> refEncodedData() {
46         return this->onRefEncodedData();
47     }
48 
49     /**
50      *  Return the ImageInfo associated with this generator.
51      */
getInfo()52     const SkImageInfo& getInfo() const { return fInfo; }
53 
54     /**
55      *  Can this generator be used to produce images that will be drawable to the specified context
56      *  (or to CPU, if context is nullptr)?
57      */
isValid(GrContext * context)58     bool isValid(GrContext* context) const {
59         return this->onIsValid(context);
60     }
61 
62     /**
63      *  Decode into the given pixels, a block of memory of size at
64      *  least (info.fHeight - 1) * rowBytes + (info.fWidth *
65      *  bytesPerPixel)
66      *
67      *  Repeated calls to this function should give the same results,
68      *  allowing the PixelRef to be immutable.
69      *
70      *  @param info A description of the format
71      *         expected by the caller.  This can simply be identical
72      *         to the info returned by getInfo().
73      *
74      *         This contract also allows the caller to specify
75      *         different output-configs, which the implementation can
76      *         decide to support or not.
77      *
78      *         A size that does not match getInfo() implies a request
79      *         to scale. If the generator cannot perform this scale,
80      *         it will return false.
81      *
82      *  @return true on success.
83      */
84     bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
85 
86     /**
87      *  If decoding to YUV is supported, this returns true.  Otherwise, this
88      *  returns false and does not modify any of the parameters.
89      *
90      *  @param sizeInfo    Output parameter indicating the sizes and required
91      *                     allocation widths of the Y, U, V, and A planes.
92      *  @param yuvaIndices How the YUVA planes are organized/used
93      *  @param colorSpace  Output parameter.
94      */
95     bool queryYUVA8(SkYUVASizeInfo* sizeInfo,
96                     SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
97                     SkYUVColorSpace* colorSpace) const;
98 
99     /**
100      *  Returns true on success and false on failure.
101      *  This always attempts to perform a full decode.  If the client only
102      *  wants size, it should call queryYUVA8().
103      *
104      *  @param sizeInfo    Needs to exactly match the values returned by the
105      *                     query, except the WidthBytes may be larger than the
106      *                     recommendation (but not smaller).
107      *  @param yuvaIndices Needs to exactly match the values returned by the query.
108      *  @param planes      Memory for the Y, U, V, and A planes. Note that, depending on the
109      *                     settings in yuvaIndices, anywhere from 1..4 planes could be returned.
110      */
111     bool getYUVA8Planes(const SkYUVASizeInfo& sizeInfo,
112                         const SkYUVAIndex yuvaIndices[SkYUVAIndex::kIndexCount],
113                         void* planes[]);
114 
115 #if SK_SUPPORT_GPU
116     /**
117      *  If the generator can natively/efficiently return its pixels as a GPU image (backed by a
118      *  texture) this will return that image. If not, this will return NULL.
119      *
120      *  This routine also supports retrieving only a subset of the pixels. That subset is specified
121      *  by the following rectangle:
122      *
123      *      subset = SkIRect::MakeXYWH(origin.x(), origin.y(), info.width(), info.height())
124      *
125      *  If subset is not contained inside the generator's bounds, this returns false.
126      *
127      *      whole = SkIRect::MakeWH(getInfo().width(), getInfo().height())
128      *      if (!whole.contains(subset)) {
129      *          return false;
130      *      }
131      *
132      *  Regarding the GrContext parameter:
133      *
134      *  It must be non-NULL. The generator should only succeed if:
135      *  - its internal context is the same
136      *  - it can somehow convert its texture into one that is valid for the provided context.
137      *
138      *  If the willNeedMipMaps flag is true, the generator should try to create a TextureProxy that
139      *  at least has the mip levels allocated and the base layer filled in. If this is not possible,
140      *  the generator is allowed to return a non mipped proxy, but this will have some additional
141      *  overhead in later allocating mips and copying of the base layer.
142      */
143     sk_sp<GrTextureProxy> generateTexture(GrContext*, const SkImageInfo& info,
144                                           const SkIPoint& origin,
145                                           bool willNeedMipMaps);
146 #endif
147 
148     /**
149      *  If the default image decoder system can interpret the specified (encoded) data, then
150      *  this returns a new ImageGenerator for it. Otherwise this returns NULL. Either way
151      *  the caller is still responsible for managing their ownership of the data.
152      */
153     static std::unique_ptr<SkImageGenerator> MakeFromEncoded(sk_sp<SkData>);
154 
155     /** Return a new image generator backed by the specified picture.  If the size is empty or
156      *  the picture is NULL, this returns NULL.
157      *  The optional matrix and paint arguments are passed to drawPicture() at rasterization
158      *  time.
159      */
160     static std::unique_ptr<SkImageGenerator> MakeFromPicture(const SkISize&, sk_sp<SkPicture>,
161                                                              const SkMatrix*, const SkPaint*,
162                                                              SkImage::BitDepth,
163                                                              sk_sp<SkColorSpace>);
164 
165 protected:
166     static constexpr int kNeedNewImageUniqueID = 0;
167 
168     SkImageGenerator(const SkImageInfo& info, uint32_t uniqueId = kNeedNewImageUniqueID);
169 
onRefEncodedData()170     virtual sk_sp<SkData> onRefEncodedData() { return nullptr; }
171     struct Options {};
onGetPixels(const SkImageInfo &,void *,size_t,const Options &)172     virtual bool onGetPixels(const SkImageInfo&, void*, size_t, const Options&) { return false; }
onIsValid(GrContext *)173     virtual bool onIsValid(GrContext*) const { return true; }
onQueryYUVA8(SkYUVASizeInfo *,SkYUVAIndex[SkYUVAIndex::kIndexCount],SkYUVColorSpace *)174     virtual bool onQueryYUVA8(SkYUVASizeInfo*, SkYUVAIndex[SkYUVAIndex::kIndexCount],
175                               SkYUVColorSpace*) const { return false; }
onGetYUVA8Planes(const SkYUVASizeInfo &,const SkYUVAIndex[SkYUVAIndex::kIndexCount],void * [4])176     virtual bool onGetYUVA8Planes(const SkYUVASizeInfo&, const SkYUVAIndex[SkYUVAIndex::kIndexCount],
177                                   void*[4] /*planes*/) { return false; }
178 #if SK_SUPPORT_GPU
179     enum class TexGenType {
180         kNone,           //image generator does not implement onGenerateTexture
181         kCheap,          //onGenerateTexture is implemented and it is fast (does not render offscreen)
182         kExpensive,      //onGenerateTexture is implemented and it is relatively slow
183     };
184 
onCanGenerateTexture()185     virtual TexGenType onCanGenerateTexture() const { return TexGenType::kNone; }
186     virtual sk_sp<GrTextureProxy> onGenerateTexture(GrContext*, const SkImageInfo&, const SkIPoint&,
187                                                     bool willNeedMipMaps);  // returns nullptr
188 #endif
189 
190 private:
191     const SkImageInfo fInfo;
192     const uint32_t fUniqueID;
193 
194     friend class SkImage_Lazy;
195 
196     // This is our default impl, which may be different on different platforms.
197     // It is called from NewFromEncoded() after it has checked for any runtime factory.
198     // The SkData will never be NULL, as that will have been checked by NewFromEncoded.
199     static std::unique_ptr<SkImageGenerator> MakeFromEncodedImpl(sk_sp<SkData>);
200 
201     SkImageGenerator(SkImageGenerator&&) = delete;
202     SkImageGenerator(const SkImageGenerator&) = delete;
203     SkImageGenerator& operator=(SkImageGenerator&&) = delete;
204     SkImageGenerator& operator=(const SkImageGenerator&) = delete;
205 };
206 
207 #endif  // SkImageGenerator_DEFINED
208