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