blob: 7bb4669017ec7d11606fc89e8f1c72eb808efd02 [file] [log] [blame]
halcanary@google.comad04eb42013-11-21 15:32:08 +00001/*
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
reed4b3d3be2015-09-17 13:35:19 -070011#include "SkBitmap.h"
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000012#include "SkColor.h"
rileyaabaef862014-09-12 17:45:58 -070013#include "SkImageInfo.h"
msarett4984c3c2016-03-10 05:44:43 -080014#include "SkYUVSizeInfo.h"
halcanary@google.comad04eb42013-11-21 15:32:08 +000015
reed8f343722015-08-13 13:32:39 -070016class GrContext;
Mike Reed34a2ca12016-10-20 15:37:41 -040017class GrContextThreadSafeProxy;
bsalomonafa95e22015-10-12 10:39:46 -070018class GrTexture;
Brian Salomon514baff2016-11-17 15:17:07 -050019class GrSamplerParams;
halcanary@google.comedd370f2013-12-10 21:11:12 +000020class SkBitmap;
halcanary@google.comad04eb42013-11-21 15:32:08 +000021class SkData;
Florin Malitaca795352016-11-16 14:45:34 -050022class SkImage;
halcanary@google.comedd370f2013-12-10 21:11:12 +000023class SkImageGenerator;
fmalita1dedc3d2015-08-04 13:53:14 -070024class SkMatrix;
25class SkPaint;
26class SkPicture;
halcanary@google.comedd370f2013-12-10 21:11:12 +000027
scroggo08649082015-02-13 11:13:34 -080028class SK_API SkImageGenerator : public SkNoncopyable {
halcanary@google.comad04eb42013-11-21 15:32:08 +000029public:
30 /**
31 * The PixelRef which takes ownership of this SkImageGenerator
32 * will call the image generator's destructor.
33 */
34 virtual ~SkImageGenerator() { }
35
reed8f343722015-08-13 13:32:39 -070036 uint32_t uniqueID() const { return fUniqueID; }
37
halcanary@google.comad04eb42013-11-21 15:32:08 +000038 /**
39 * Return a ref to the encoded (i.e. compressed) representation,
reed05dd2512016-01-05 09:16:19 -080040 * of this data. If the GrContext is non-null, then the caller is only interested in
41 * gpu-specific formats, so the impl may return null even if they have encoded data,
42 * assuming they know it is not suitable for the gpu.
halcanary@google.comad04eb42013-11-21 15:32:08 +000043 *
44 * If non-NULL is returned, the caller is responsible for calling
45 * unref() on the data when it is finished.
46 */
reed05dd2512016-01-05 09:16:19 -080047 SkData* refEncodedData(GrContext* ctx = nullptr) {
reed05dd2512016-01-05 09:16:19 -080048 return this->onRefEncodedData(ctx);
reed05dd2512016-01-05 09:16:19 -080049 }
halcanary@google.comad04eb42013-11-21 15:32:08 +000050
51 /**
reed3ef71e32015-03-19 08:31:14 -070052 * Return the ImageInfo associated with this generator.
halcanary@google.comad04eb42013-11-21 15:32:08 +000053 */
reed3ef71e32015-03-19 08:31:14 -070054 const SkImageInfo& getInfo() const { return fInfo; }
halcanary@google.comad04eb42013-11-21 15:32:08 +000055
scroggo95526622015-03-17 05:02:17 -070056 /**
halcanary@google.comad04eb42013-11-21 15:32:08 +000057 * Decode into the given pixels, a block of memory of size at
58 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
59 * bytesPerPixel)
60 *
commit-bot@chromium.orgdd597992013-12-02 22:32:54 +000061 * Repeated calls to this function should give the same results,
62 * allowing the PixelRef to be immutable.
63 *
halcanary@google.comad04eb42013-11-21 15:32:08 +000064 * @param info A description of the format (config, size)
65 * expected by the caller. This can simply be identical
66 * to the info returned by getInfo().
67 *
68 * This contract also allows the caller to specify
69 * different output-configs, which the implementation can
70 * decide to support or not.
71 *
scroggo08649082015-02-13 11:13:34 -080072 * A size that does not match getInfo() implies a request
73 * to scale. If the generator cannot perform this scale,
74 * it will return kInvalidScale.
75 *
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000076 * If info is kIndex8_SkColorType, then the caller must provide storage for up to 256
77 * SkPMColor values in ctable. On success the generator must copy N colors into that storage,
78 * (where N is the logical number of table entries) and set ctableCount to N.
79 *
80 * If info is not kIndex8_SkColorType, then the last two parameters may be NULL. If ctableCount
81 * is not null, it will be set to 0.
82 *
scroggo5315fd42015-07-09 09:08:00 -070083 * @return true on success.
halcanary@google.comad04eb42013-11-21 15:32:08 +000084 */
scroggo5315fd42015-07-09 09:08:00 -070085 bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
86 SkPMColor ctable[], int* ctableCount);
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000087
88 /**
scroggo95526622015-03-17 05:02:17 -070089 * Simplified version of getPixels() that asserts that info is NOT kIndex8_SkColorType and
90 * uses the default Options.
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000091 */
scroggo5315fd42015-07-09 09:08:00 -070092 bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000093
sugoi518d83d2014-07-21 11:37:39 -070094 /**
msarett4984c3c2016-03-10 05:44:43 -080095 * If decoding to YUV is supported, this returns true. Otherwise, this
96 * returns false and does not modify any of the parameters.
sugoi518d83d2014-07-21 11:37:39 -070097 *
msarett4984c3c2016-03-10 05:44:43 -080098 * @param sizeInfo Output parameter indicating the sizes and required
99 * allocation widths of the Y, U, and V planes.
100 * @param colorSpace Output parameter.
sugoi518d83d2014-07-21 11:37:39 -0700101 */
msarett4984c3c2016-03-10 05:44:43 -0800102 bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const;
103
104 /**
105 * Returns true on success and false on failure.
106 * This always attempts to perform a full decode. If the client only
107 * wants size, it should call queryYUV8().
108 *
109 * @param sizeInfo Needs to exactly match the values returned by the
110 * query, except the WidthBytes may be larger than the
111 * recommendation (but not smaller).
112 * @param planes Memory for each of the Y, U, and V planes.
113 */
114 bool getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]);
sugoi518d83d2014-07-21 11:37:39 -0700115
reedb5d818a2015-01-06 11:30:45 -0800116 /**
reed8f343722015-08-13 13:32:39 -0700117 * 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 * Regarding the GrContext parameter:
121 *
122 * The caller may pass NULL for the context. In that case the generator may assume that its
123 * internal context is current. If it has no internal context, then it should just return
124 * null.
125 *
126 * If the caller passes a non-null context, then the generator should only succeed if:
127 * - it has no intrinsic context, and will use the caller's
128 * - its internal context is the same
129 * - it can somehow convert its texture into one that is valid for the provided context.
reed8f343722015-08-13 13:32:39 -0700130 */
bsalomon5f5527f2015-10-15 12:14:55 -0700131 GrTexture* generateTexture(GrContext*, const SkIRect* subset = nullptr);
reed8f343722015-08-13 13:32:39 -0700132
reed7850eb22015-12-02 14:19:47 -0800133 struct SupportedSizes {
134 SkISize fSizes[2];
135 };
136
137 /**
138 * Some generators can efficiently scale their contents. If this is supported, the generator
139 * may only support certain scaled dimensions. Call this with the desired scale factor,
140 * and it will return true if scaling is supported, and in supportedSizes[] it will return
141 * the nearest supported dimensions.
142 *
143 * If no native scaling is supported, or scale is invalid (e.g. scale <= 0 || scale > 1)
144 * this will return false, and the supportedsizes will be undefined.
145 */
146 bool computeScaledDimensions(SkScalar scale, SupportedSizes*);
147
148 /**
149 * Scale the generator's pixels to fit into scaledSize.
150 * This routine also support retrieving only a subset of the pixels. That subset is specified
151 * by the following rectangle (in the scaled space):
152 *
153 * subset = SkIRect::MakeXYWH(subsetOrigin.x(), subsetOrigin.y(),
154 * subsetPixels.width(), subsetPixels.height())
155 *
156 * If subset is not contained inside the scaledSize, this returns false.
157 *
158 * whole = SkIRect::MakeWH(scaledSize.width(), scaledSize.height())
159 * if (!whole.contains(subset)) {
160 * return false;
161 * }
162 *
163 * If the requested colortype/alphatype in pixels is not supported,
164 * or the requested scaledSize is not supported, or the generator encounters an error,
165 * this returns false.
166 */
167 bool generateScaledPixels(const SkISize& scaledSize, const SkIPoint& subsetOrigin,
168 const SkPixmap& subsetPixels);
169
170 bool generateScaledPixels(const SkPixmap& scaledPixels) {
171 return this->generateScaledPixels(SkISize::Make(scaledPixels.width(),
172 scaledPixels.height()),
173 SkIPoint::Make(0, 0), scaledPixels);
174 }
175
reed8f343722015-08-13 13:32:39 -0700176 /**
Florin Malitaca795352016-11-16 14:45:34 -0500177 * External generator API: provides efficient access to externally-managed image data.
178 *
179 * Skia calls accessScaledPixels() during rasterization, to gain temporary access to
Florin Malita58cda8f2016-11-23 10:49:20 -0500180 * the external pixel data. When done, the provided callback is invoked to release the
181 * associated resources.
Florin Malitaca795352016-11-16 14:45:34 -0500182 *
183 * @param srcRect the source rect in use for the current draw
184 * @param totalMatrix full matrix in effect (mapping srcRect -> device space)
185 * @param quality the SkFilterQuality requested for rasterization.
186 * @param rec out param, expected to be set when the call succeeds:
187 *
Florin Malita58cda8f2016-11-23 10:49:20 -0500188 * - fPixmap external pixel data
189 * - fSrcRect is an adjusted srcRect
190 * - fQuality is the adjusted filter quality
191 * - fReleaseProc pixmap release callback, same signature as the
192 * SkBitmap::installPixels() callback
193 * - fReleaseCtx opaque release context argument
Florin Malitaca795352016-11-16 14:45:34 -0500194 *
195 * @return true on success, false otherwise (error or if this API is not supported;
196 * in this case Skia will fall back to its internal scaling and caching
197 * heuristics)
198 *
199 * Implementors can return pixmaps with a different size than requested, by adjusting the
200 * src rect. The contract is that Skia will observe the adjusted src rect, and will map it
201 * to the same dest as the original draw (the impl doesn't get to control the destination).
202 *
203 */
204
205 struct ScaledImageRec {
Florin Malita58cda8f2016-11-23 10:49:20 -0500206 SkPixmap fPixmap;
Florin Malitaca795352016-11-16 14:45:34 -0500207 SkRect fSrcRect;
208 SkFilterQuality fQuality;
Florin Malita58cda8f2016-11-23 10:49:20 -0500209
210 using ReleaseProcT = void (*)(void* pixels, void* releaseCtx);
211
212 ReleaseProcT fReleaseProc;
213 void* fReleaseCtx;
Florin Malitaca795352016-11-16 14:45:34 -0500214 };
215
216 bool accessScaledImage(const SkRect& srcRect, const SkMatrix& totalMatrix,
217 SkFilterQuality quality, ScaledImageRec* rec);
218
219 /**
reedb5d818a2015-01-06 11:30:45 -0800220 * If the default image decoder system can interpret the specified (encoded) data, then
221 * this returns a new ImageGenerator for it. Otherwise this returns NULL. Either way
222 * the caller is still responsible for managing their ownership of the data.
223 */
reed1c846342015-07-09 11:47:36 -0700224 static SkImageGenerator* NewFromEncoded(SkData*);
reedb5d818a2015-01-06 11:30:45 -0800225
fmalita1dedc3d2015-08-04 13:53:14 -0700226 /** Return a new image generator backed by the specified picture. If the size is empty or
227 * the picture is NULL, this returns NULL.
228 * The optional matrix and paint arguments are passed to drawPicture() at rasterization
229 * time.
230 */
231 static SkImageGenerator* NewFromPicture(const SkISize&, const SkPicture*, const SkMatrix*,
232 const SkPaint*);
233
reed4b3d3be2015-09-17 13:35:19 -0700234 bool tryGenerateBitmap(SkBitmap* bm, const SkImageInfo& info, SkBitmap::Allocator* allocator) {
235 return this->tryGenerateBitmap(bm, &info, allocator);
reed4d5b6762015-09-13 11:03:32 -0700236 }
reed4d5b6762015-09-13 11:03:32 -0700237 void generateBitmap(SkBitmap* bm, const SkImageInfo& info) {
reed4b3d3be2015-09-17 13:35:19 -0700238 if (!this->tryGenerateBitmap(bm, &info, nullptr)) {
reed4d5b6762015-09-13 11:03:32 -0700239 sk_throw();
240 }
241 }
242
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000243protected:
aleksandar.stojiljkovic95167752016-05-02 01:43:38 -0700244 enum {
245 kNeedNewImageUniqueID = 0
246 };
247
248 SkImageGenerator(const SkImageInfo& info, uint32_t uniqueId = kNeedNewImageUniqueID);
reed3ef71e32015-03-19 08:31:14 -0700249
Mike Reeddf8599a2016-12-08 13:41:10 -0500250 virtual SkData* onRefEncodedData(GrContext* ctx);
reed3ef71e32015-03-19 08:31:14 -0700251
scroggo5315fd42015-07-09 09:08:00 -0700252 virtual bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
253 SkPMColor ctable[], int* ctableCount);
msarett4984c3c2016-03-10 05:44:43 -0800254
255 virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const {
256 return false;
257 }
258 virtual bool onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) {
259 return false;
260 }
reed3ef71e32015-03-19 08:31:14 -0700261
bsalomon5f5527f2015-10-15 12:14:55 -0700262 virtual GrTexture* onGenerateTexture(GrContext*, const SkIRect*) {
reed935d6cf2015-08-18 11:16:09 -0700263 return nullptr;
264 }
reed8f343722015-08-13 13:32:39 -0700265
reed7850eb22015-12-02 14:19:47 -0800266 virtual bool onComputeScaledDimensions(SkScalar, SupportedSizes*) {
267 return false;
268 }
269 virtual bool onGenerateScaledPixels(const SkISize&, const SkIPoint&, const SkPixmap&) {
270 return false;
271 }
272
Florin Malitaca795352016-11-16 14:45:34 -0500273 virtual bool onAccessScaledImage(const SkRect&, const SkMatrix&, SkFilterQuality,
274 ScaledImageRec*) {
275 return false;
276 }
277
reed4b3d3be2015-09-17 13:35:19 -0700278 bool tryGenerateBitmap(SkBitmap* bm, const SkImageInfo* optionalInfo, SkBitmap::Allocator*);
reed4d5b6762015-09-13 11:03:32 -0700279
reed3ef71e32015-03-19 08:31:14 -0700280private:
281 const SkImageInfo fInfo;
reed8f343722015-08-13 13:32:39 -0700282 const uint32_t fUniqueID;
reed1c846342015-07-09 11:47:36 -0700283
reedd7c05bf2015-07-09 14:08:49 -0700284 // This is our default impl, which may be different on different platforms.
285 // It is called from NewFromEncoded() after it has checked for any runtime factory.
286 // The SkData will never be NULL, as that will have been checked by NewFromEncoded.
reed1c846342015-07-09 11:47:36 -0700287 static SkImageGenerator* NewFromEncodedImpl(SkData*);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000288};
289
290#endif // SkImageGenerator_DEFINED