blob: e775d9e02c51f0b2e508057864418876abd70ce0 [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
reed05dd2512016-01-05 09:16:19 -080028#ifdef SK_SUPPORT_LEGACY_REFENCODEDDATA_NOCTX
29 #define SK_REFENCODEDDATA_CTXPARAM
30#else
31 #define SK_REFENCODEDDATA_CTXPARAM GrContext* ctx
32#endif
33
scroggo08649082015-02-13 11:13:34 -080034class SK_API SkImageGenerator : public SkNoncopyable {
halcanary@google.comad04eb42013-11-21 15:32:08 +000035public:
36 /**
37 * The PixelRef which takes ownership of this SkImageGenerator
38 * will call the image generator's destructor.
39 */
40 virtual ~SkImageGenerator() { }
41
reed8f343722015-08-13 13:32:39 -070042 uint32_t uniqueID() const { return fUniqueID; }
43
halcanary@google.comad04eb42013-11-21 15:32:08 +000044 /**
45 * Return a ref to the encoded (i.e. compressed) representation,
reed05dd2512016-01-05 09:16:19 -080046 * of this data. If the GrContext is non-null, then the caller is only interested in
47 * gpu-specific formats, so the impl may return null even if they have encoded data,
48 * assuming they know it is not suitable for the gpu.
halcanary@google.comad04eb42013-11-21 15:32:08 +000049 *
50 * If non-NULL is returned, the caller is responsible for calling
51 * unref() on the data when it is finished.
52 */
reed05dd2512016-01-05 09:16:19 -080053 SkData* refEncodedData(GrContext* ctx = nullptr) {
54#ifdef SK_SUPPORT_LEGACY_REFENCODEDDATA_NOCTX
55 return this->onRefEncodedData();
56#else
57 return this->onRefEncodedData(ctx);
58#endif
59 }
halcanary@google.comad04eb42013-11-21 15:32:08 +000060
61 /**
reed3ef71e32015-03-19 08:31:14 -070062 * Return the ImageInfo associated with this generator.
halcanary@google.comad04eb42013-11-21 15:32:08 +000063 */
reed3ef71e32015-03-19 08:31:14 -070064 const SkImageInfo& getInfo() const { return fInfo; }
halcanary@google.comad04eb42013-11-21 15:32:08 +000065
scroggo95526622015-03-17 05:02:17 -070066 /**
halcanary@google.comad04eb42013-11-21 15:32:08 +000067 * Decode into the given pixels, a block of memory of size at
68 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
69 * bytesPerPixel)
70 *
commit-bot@chromium.orgdd597992013-12-02 22:32:54 +000071 * Repeated calls to this function should give the same results,
72 * allowing the PixelRef to be immutable.
73 *
halcanary@google.comad04eb42013-11-21 15:32:08 +000074 * @param info A description of the format (config, size)
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 *
scroggo08649082015-02-13 11:13:34 -080082 * A size that does not match getInfo() implies a request
83 * to scale. If the generator cannot perform this scale,
84 * it will return kInvalidScale.
85 *
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000086 * If info is kIndex8_SkColorType, then the caller must provide storage for up to 256
87 * SkPMColor values in ctable. On success the generator must copy N colors into that storage,
88 * (where N is the logical number of table entries) and set ctableCount to N.
89 *
90 * If info is not kIndex8_SkColorType, then the last two parameters may be NULL. If ctableCount
91 * is not null, it will be set to 0.
92 *
scroggo5315fd42015-07-09 09:08:00 -070093 * @return true on success.
halcanary@google.comad04eb42013-11-21 15:32:08 +000094 */
scroggo5315fd42015-07-09 09:08:00 -070095 bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
96 SkPMColor ctable[], int* ctableCount);
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +000097
98 /**
scroggo95526622015-03-17 05:02:17 -070099 * Simplified version of getPixels() that asserts that info is NOT kIndex8_SkColorType and
100 * uses the default Options.
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000101 */
scroggo5315fd42015-07-09 09:08:00 -0700102 bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000103
sugoi518d83d2014-07-21 11:37:39 -0700104 /**
msarett4984c3c2016-03-10 05:44:43 -0800105 * If decoding to YUV is supported, this returns true. Otherwise, this
106 * returns false and does not modify any of the parameters.
sugoi518d83d2014-07-21 11:37:39 -0700107 *
msarett4984c3c2016-03-10 05:44:43 -0800108 * @param sizeInfo Output parameter indicating the sizes and required
109 * allocation widths of the Y, U, and V planes.
110 * @param colorSpace Output parameter.
sugoi518d83d2014-07-21 11:37:39 -0700111 */
msarett4984c3c2016-03-10 05:44:43 -0800112 bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const;
113
114 /**
115 * Returns true on success and false on failure.
116 * This always attempts to perform a full decode. If the client only
117 * wants size, it should call queryYUV8().
118 *
119 * @param sizeInfo Needs to exactly match the values returned by the
120 * query, except the WidthBytes may be larger than the
121 * recommendation (but not smaller).
122 * @param planes Memory for each of the Y, U, and V planes.
123 */
124 bool getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]);
sugoi518d83d2014-07-21 11:37:39 -0700125
reedb5d818a2015-01-06 11:30:45 -0800126 /**
reed8f343722015-08-13 13:32:39 -0700127 * If the generator can natively/efficiently return its pixels as a GPU image (backed by a
128 * texture) this will return that image. If not, this will return NULL.
129 *
130 * Regarding the GrContext parameter:
131 *
132 * The caller may pass NULL for the context. In that case the generator may assume that its
133 * internal context is current. If it has no internal context, then it should just return
134 * null.
135 *
136 * If the caller passes a non-null context, then the generator should only succeed if:
137 * - it has no intrinsic context, and will use the caller's
138 * - its internal context is the same
139 * - it can somehow convert its texture into one that is valid for the provided context.
reed8f343722015-08-13 13:32:39 -0700140 */
bsalomon5f5527f2015-10-15 12:14:55 -0700141 GrTexture* generateTexture(GrContext*, const SkIRect* subset = nullptr);
reed8f343722015-08-13 13:32:39 -0700142
reed7850eb22015-12-02 14:19:47 -0800143 struct SupportedSizes {
144 SkISize fSizes[2];
145 };
146
147 /**
148 * Some generators can efficiently scale their contents. If this is supported, the generator
149 * may only support certain scaled dimensions. Call this with the desired scale factor,
150 * and it will return true if scaling is supported, and in supportedSizes[] it will return
151 * the nearest supported dimensions.
152 *
153 * If no native scaling is supported, or scale is invalid (e.g. scale <= 0 || scale > 1)
154 * this will return false, and the supportedsizes will be undefined.
155 */
156 bool computeScaledDimensions(SkScalar scale, SupportedSizes*);
157
158 /**
159 * Scale the generator's pixels to fit into scaledSize.
160 * This routine also support retrieving only a subset of the pixels. That subset is specified
161 * by the following rectangle (in the scaled space):
162 *
163 * subset = SkIRect::MakeXYWH(subsetOrigin.x(), subsetOrigin.y(),
164 * subsetPixels.width(), subsetPixels.height())
165 *
166 * If subset is not contained inside the scaledSize, this returns false.
167 *
168 * whole = SkIRect::MakeWH(scaledSize.width(), scaledSize.height())
169 * if (!whole.contains(subset)) {
170 * return false;
171 * }
172 *
173 * If the requested colortype/alphatype in pixels is not supported,
174 * or the requested scaledSize is not supported, or the generator encounters an error,
175 * this returns false.
176 */
177 bool generateScaledPixels(const SkISize& scaledSize, const SkIPoint& subsetOrigin,
178 const SkPixmap& subsetPixels);
179
180 bool generateScaledPixels(const SkPixmap& scaledPixels) {
181 return this->generateScaledPixels(SkISize::Make(scaledPixels.width(),
182 scaledPixels.height()),
183 SkIPoint::Make(0, 0), scaledPixels);
184 }
185
reed8f343722015-08-13 13:32:39 -0700186 /**
Florin Malitaca795352016-11-16 14:45:34 -0500187 * External generator API: provides efficient access to externally-managed image data.
188 *
189 * Skia calls accessScaledPixels() during rasterization, to gain temporary access to
Florin Malita58cda8f2016-11-23 10:49:20 -0500190 * the external pixel data. When done, the provided callback is invoked to release the
191 * associated resources.
Florin Malitaca795352016-11-16 14:45:34 -0500192 *
193 * @param srcRect the source rect in use for the current draw
194 * @param totalMatrix full matrix in effect (mapping srcRect -> device space)
195 * @param quality the SkFilterQuality requested for rasterization.
196 * @param rec out param, expected to be set when the call succeeds:
197 *
Florin Malita58cda8f2016-11-23 10:49:20 -0500198 * - fPixmap external pixel data
199 * - fSrcRect is an adjusted srcRect
200 * - fQuality is the adjusted filter quality
201 * - fReleaseProc pixmap release callback, same signature as the
202 * SkBitmap::installPixels() callback
203 * - fReleaseCtx opaque release context argument
Florin Malitaca795352016-11-16 14:45:34 -0500204 *
205 * @return true on success, false otherwise (error or if this API is not supported;
206 * in this case Skia will fall back to its internal scaling and caching
207 * heuristics)
208 *
209 * Implementors can return pixmaps with a different size than requested, by adjusting the
210 * src rect. The contract is that Skia will observe the adjusted src rect, and will map it
211 * to the same dest as the original draw (the impl doesn't get to control the destination).
212 *
213 */
214
215 struct ScaledImageRec {
Florin Malita58cda8f2016-11-23 10:49:20 -0500216 SkPixmap fPixmap;
Florin Malitaca795352016-11-16 14:45:34 -0500217 SkRect fSrcRect;
218 SkFilterQuality fQuality;
Florin Malita58cda8f2016-11-23 10:49:20 -0500219
220 using ReleaseProcT = void (*)(void* pixels, void* releaseCtx);
221
222 ReleaseProcT fReleaseProc;
223 void* fReleaseCtx;
Florin Malitaca795352016-11-16 14:45:34 -0500224 };
225
226 bool accessScaledImage(const SkRect& srcRect, const SkMatrix& totalMatrix,
227 SkFilterQuality quality, ScaledImageRec* rec);
228
229 /**
reedb5d818a2015-01-06 11:30:45 -0800230 * If the default image decoder system can interpret the specified (encoded) data, then
231 * this returns a new ImageGenerator for it. Otherwise this returns NULL. Either way
232 * the caller is still responsible for managing their ownership of the data.
233 */
reed1c846342015-07-09 11:47:36 -0700234 static SkImageGenerator* NewFromEncoded(SkData*);
reedb5d818a2015-01-06 11:30:45 -0800235
fmalita1dedc3d2015-08-04 13:53:14 -0700236 /** Return a new image generator backed by the specified picture. If the size is empty or
237 * the picture is NULL, this returns NULL.
238 * The optional matrix and paint arguments are passed to drawPicture() at rasterization
239 * time.
240 */
241 static SkImageGenerator* NewFromPicture(const SkISize&, const SkPicture*, const SkMatrix*,
242 const SkPaint*);
243
reed4b3d3be2015-09-17 13:35:19 -0700244 bool tryGenerateBitmap(SkBitmap* bm, const SkImageInfo& info, SkBitmap::Allocator* allocator) {
245 return this->tryGenerateBitmap(bm, &info, allocator);
reed4d5b6762015-09-13 11:03:32 -0700246 }
reed4d5b6762015-09-13 11:03:32 -0700247 void generateBitmap(SkBitmap* bm, const SkImageInfo& info) {
reed4b3d3be2015-09-17 13:35:19 -0700248 if (!this->tryGenerateBitmap(bm, &info, nullptr)) {
reed4d5b6762015-09-13 11:03:32 -0700249 sk_throw();
250 }
251 }
252
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000253protected:
aleksandar.stojiljkovic95167752016-05-02 01:43:38 -0700254 enum {
255 kNeedNewImageUniqueID = 0
256 };
257
258 SkImageGenerator(const SkImageInfo& info, uint32_t uniqueId = kNeedNewImageUniqueID);
reed3ef71e32015-03-19 08:31:14 -0700259
reed05dd2512016-01-05 09:16:19 -0800260 virtual SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM);
reed3ef71e32015-03-19 08:31:14 -0700261
scroggo5315fd42015-07-09 09:08:00 -0700262 virtual bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
263 SkPMColor ctable[], int* ctableCount);
msarett4984c3c2016-03-10 05:44:43 -0800264
265 virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const {
266 return false;
267 }
268 virtual bool onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) {
269 return false;
270 }
reed3ef71e32015-03-19 08:31:14 -0700271
bsalomon5f5527f2015-10-15 12:14:55 -0700272 virtual GrTexture* onGenerateTexture(GrContext*, const SkIRect*) {
reed935d6cf2015-08-18 11:16:09 -0700273 return nullptr;
274 }
reed8f343722015-08-13 13:32:39 -0700275
reed7850eb22015-12-02 14:19:47 -0800276 virtual bool onComputeScaledDimensions(SkScalar, SupportedSizes*) {
277 return false;
278 }
279 virtual bool onGenerateScaledPixels(const SkISize&, const SkIPoint&, const SkPixmap&) {
280 return false;
281 }
282
Florin Malitaca795352016-11-16 14:45:34 -0500283 virtual bool onAccessScaledImage(const SkRect&, const SkMatrix&, SkFilterQuality,
284 ScaledImageRec*) {
285 return false;
286 }
287
reed4b3d3be2015-09-17 13:35:19 -0700288 bool tryGenerateBitmap(SkBitmap* bm, const SkImageInfo* optionalInfo, SkBitmap::Allocator*);
reed4d5b6762015-09-13 11:03:32 -0700289
reed3ef71e32015-03-19 08:31:14 -0700290private:
291 const SkImageInfo fInfo;
reed8f343722015-08-13 13:32:39 -0700292 const uint32_t fUniqueID;
reed1c846342015-07-09 11:47:36 -0700293
reedd7c05bf2015-07-09 14:08:49 -0700294 // This is our default impl, which may be different on different platforms.
295 // It is called from NewFromEncoded() after it has checked for any runtime factory.
296 // The SkData will never be NULL, as that will have been checked by NewFromEncoded.
reed1c846342015-07-09 11:47:36 -0700297 static SkImageGenerator* NewFromEncodedImpl(SkData*);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000298};
299
300#endif // SkImageGenerator_DEFINED