halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 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 | |
reed | 4b3d3be | 2015-09-17 13:35:19 -0700 | [diff] [blame] | 11 | #include "SkBitmap.h" |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 12 | #include "SkColor.h" |
rileya | abaef86 | 2014-09-12 17:45:58 -0700 | [diff] [blame] | 13 | #include "SkImageInfo.h" |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 14 | #include "SkYUVSizeInfo.h" |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 15 | |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 16 | class GrContext; |
Mike Reed | 34a2ca1 | 2016-10-20 15:37:41 -0400 | [diff] [blame] | 17 | class GrContextThreadSafeProxy; |
bsalomon | afa95e2 | 2015-10-12 10:39:46 -0700 | [diff] [blame] | 18 | class GrTexture; |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 19 | class GrSamplerParams; |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 20 | class SkBitmap; |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 21 | class SkData; |
Florin Malita | ca79535 | 2016-11-16 14:45:34 -0500 | [diff] [blame] | 22 | class SkImage; |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 23 | class SkImageGenerator; |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 24 | class SkMatrix; |
| 25 | class SkPaint; |
| 26 | class SkPicture; |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 27 | |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 28 | #ifdef SK_SUPPORT_LEGACY_REFENCODEDDATA_NOCTX |
| 29 | #define SK_REFENCODEDDATA_CTXPARAM |
| 30 | #else |
| 31 | #define SK_REFENCODEDDATA_CTXPARAM GrContext* ctx |
| 32 | #endif |
| 33 | |
scroggo | 0864908 | 2015-02-13 11:13:34 -0800 | [diff] [blame] | 34 | class SK_API SkImageGenerator : public SkNoncopyable { |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 35 | public: |
| 36 | /** |
| 37 | * The PixelRef which takes ownership of this SkImageGenerator |
| 38 | * will call the image generator's destructor. |
| 39 | */ |
| 40 | virtual ~SkImageGenerator() { } |
| 41 | |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 42 | uint32_t uniqueID() const { return fUniqueID; } |
| 43 | |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 44 | /** |
| 45 | * Return a ref to the encoded (i.e. compressed) representation, |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 46 | * 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.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 49 | * |
| 50 | * If non-NULL is returned, the caller is responsible for calling |
| 51 | * unref() on the data when it is finished. |
| 52 | */ |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 53 | 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.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 62 | * Return the ImageInfo associated with this generator. |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 63 | */ |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 64 | const SkImageInfo& getInfo() const { return fInfo; } |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 65 | |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 66 | /** |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 67 | * 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.org | dd59799 | 2013-12-02 22:32:54 +0000 | [diff] [blame] | 71 | * Repeated calls to this function should give the same results, |
| 72 | * allowing the PixelRef to be immutable. |
| 73 | * |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 74 | * @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 | * |
scroggo | 0864908 | 2015-02-13 11:13:34 -0800 | [diff] [blame] | 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 kInvalidScale. |
| 85 | * |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 86 | * 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 | * |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame] | 93 | * @return true on success. |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 94 | */ |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame] | 95 | bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 96 | SkPMColor ctable[], int* ctableCount); |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 97 | |
| 98 | /** |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 99 | * Simplified version of getPixels() that asserts that info is NOT kIndex8_SkColorType and |
| 100 | * uses the default Options. |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 101 | */ |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame] | 102 | bool getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes); |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 103 | |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 104 | /** |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 105 | * If decoding to YUV is supported, this returns true. Otherwise, this |
| 106 | * returns false and does not modify any of the parameters. |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 107 | * |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 108 | * @param sizeInfo Output parameter indicating the sizes and required |
| 109 | * allocation widths of the Y, U, and V planes. |
| 110 | * @param colorSpace Output parameter. |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 111 | */ |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 112 | 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]); |
sugoi | 518d83d | 2014-07-21 11:37:39 -0700 | [diff] [blame] | 125 | |
reed | b5d818a | 2015-01-06 11:30:45 -0800 | [diff] [blame] | 126 | /** |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 127 | * 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. |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 140 | */ |
bsalomon | 5f5527f | 2015-10-15 12:14:55 -0700 | [diff] [blame] | 141 | GrTexture* generateTexture(GrContext*, const SkIRect* subset = nullptr); |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 142 | |
reed | 7850eb2 | 2015-12-02 14:19:47 -0800 | [diff] [blame] | 143 | 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 | |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 186 | /** |
Florin Malita | ca79535 | 2016-11-16 14:45:34 -0500 | [diff] [blame] | 187 | * External generator API: provides efficient access to externally-managed image data. |
| 188 | * |
| 189 | * Skia calls accessScaledPixels() during rasterization, to gain temporary access to |
Florin Malita | 58cda8f | 2016-11-23 10:49:20 -0500 | [diff] [blame] | 190 | * the external pixel data. When done, the provided callback is invoked to release the |
| 191 | * associated resources. |
Florin Malita | ca79535 | 2016-11-16 14:45:34 -0500 | [diff] [blame] | 192 | * |
| 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 Malita | 58cda8f | 2016-11-23 10:49:20 -0500 | [diff] [blame] | 198 | * - 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 Malita | ca79535 | 2016-11-16 14:45:34 -0500 | [diff] [blame] | 204 | * |
| 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 Malita | 58cda8f | 2016-11-23 10:49:20 -0500 | [diff] [blame] | 216 | SkPixmap fPixmap; |
Florin Malita | ca79535 | 2016-11-16 14:45:34 -0500 | [diff] [blame] | 217 | SkRect fSrcRect; |
| 218 | SkFilterQuality fQuality; |
Florin Malita | 58cda8f | 2016-11-23 10:49:20 -0500 | [diff] [blame] | 219 | |
| 220 | using ReleaseProcT = void (*)(void* pixels, void* releaseCtx); |
| 221 | |
| 222 | ReleaseProcT fReleaseProc; |
| 223 | void* fReleaseCtx; |
Florin Malita | ca79535 | 2016-11-16 14:45:34 -0500 | [diff] [blame] | 224 | }; |
| 225 | |
| 226 | bool accessScaledImage(const SkRect& srcRect, const SkMatrix& totalMatrix, |
| 227 | SkFilterQuality quality, ScaledImageRec* rec); |
| 228 | |
| 229 | /** |
reed | b5d818a | 2015-01-06 11:30:45 -0800 | [diff] [blame] | 230 | * 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 | */ |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 234 | static SkImageGenerator* NewFromEncoded(SkData*); |
reed | b5d818a | 2015-01-06 11:30:45 -0800 | [diff] [blame] | 235 | |
fmalita | 1dedc3d | 2015-08-04 13:53:14 -0700 | [diff] [blame] | 236 | /** 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 | |
reed | 4b3d3be | 2015-09-17 13:35:19 -0700 | [diff] [blame] | 244 | bool tryGenerateBitmap(SkBitmap* bm, const SkImageInfo& info, SkBitmap::Allocator* allocator) { |
| 245 | return this->tryGenerateBitmap(bm, &info, allocator); |
reed | 4d5b676 | 2015-09-13 11:03:32 -0700 | [diff] [blame] | 246 | } |
reed | 4d5b676 | 2015-09-13 11:03:32 -0700 | [diff] [blame] | 247 | void generateBitmap(SkBitmap* bm, const SkImageInfo& info) { |
reed | 4b3d3be | 2015-09-17 13:35:19 -0700 | [diff] [blame] | 248 | if (!this->tryGenerateBitmap(bm, &info, nullptr)) { |
reed | 4d5b676 | 2015-09-13 11:03:32 -0700 | [diff] [blame] | 249 | sk_throw(); |
| 250 | } |
| 251 | } |
| 252 | |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 253 | protected: |
aleksandar.stojiljkovic | 9516775 | 2016-05-02 01:43:38 -0700 | [diff] [blame] | 254 | enum { |
| 255 | kNeedNewImageUniqueID = 0 |
| 256 | }; |
| 257 | |
| 258 | SkImageGenerator(const SkImageInfo& info, uint32_t uniqueId = kNeedNewImageUniqueID); |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 259 | |
reed | 05dd251 | 2016-01-05 09:16:19 -0800 | [diff] [blame] | 260 | virtual SkData* onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM); |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 261 | |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame] | 262 | virtual bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 263 | SkPMColor ctable[], int* ctableCount); |
msarett | 4984c3c | 2016-03-10 05:44:43 -0800 | [diff] [blame] | 264 | |
| 265 | virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const { |
| 266 | return false; |
| 267 | } |
| 268 | virtual bool onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) { |
| 269 | return false; |
| 270 | } |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 271 | |
bsalomon | 5f5527f | 2015-10-15 12:14:55 -0700 | [diff] [blame] | 272 | virtual GrTexture* onGenerateTexture(GrContext*, const SkIRect*) { |
reed | 935d6cf | 2015-08-18 11:16:09 -0700 | [diff] [blame] | 273 | return nullptr; |
| 274 | } |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 275 | |
reed | 7850eb2 | 2015-12-02 14:19:47 -0800 | [diff] [blame] | 276 | 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 Malita | ca79535 | 2016-11-16 14:45:34 -0500 | [diff] [blame] | 283 | virtual bool onAccessScaledImage(const SkRect&, const SkMatrix&, SkFilterQuality, |
| 284 | ScaledImageRec*) { |
| 285 | return false; |
| 286 | } |
| 287 | |
reed | 4b3d3be | 2015-09-17 13:35:19 -0700 | [diff] [blame] | 288 | bool tryGenerateBitmap(SkBitmap* bm, const SkImageInfo* optionalInfo, SkBitmap::Allocator*); |
reed | 4d5b676 | 2015-09-13 11:03:32 -0700 | [diff] [blame] | 289 | |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 290 | private: |
| 291 | const SkImageInfo fInfo; |
reed | 8f34372 | 2015-08-13 13:32:39 -0700 | [diff] [blame] | 292 | const uint32_t fUniqueID; |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 293 | |
reed | d7c05bf | 2015-07-09 14:08:49 -0700 | [diff] [blame] | 294 | // 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. |
reed | 1c84634 | 2015-07-09 11:47:36 -0700 | [diff] [blame] | 297 | static SkImageGenerator* NewFromEncodedImpl(SkData*); |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | #endif // SkImageGenerator_DEFINED |