scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 SkCodec_DEFINED |
| 9 | #define SkCodec_DEFINED |
| 10 | |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 11 | #include "../private/SkNoncopyable.h" |
bungeman | f3c15b7 | 2015-08-19 11:56:48 -0700 | [diff] [blame] | 12 | #include "../private/SkTemplates.h" |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 13 | #include "../private/SkEncodedInfo.h" |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 14 | #include "SkCodecAnimation.h" |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 15 | #include "SkColor.h" |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 16 | #include "SkEncodedImageFormat.h" |
Leon Scroggins III | b6ab10f | 2017-10-18 14:42:43 -0400 | [diff] [blame] | 17 | #include "SkEncodedOrigin.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 18 | #include "SkImageInfo.h" |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 19 | #include "SkPixmap.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 20 | #include "SkSize.h" |
scroggo | fffeede | 2015-03-18 10:50:37 -0700 | [diff] [blame] | 21 | #include "SkStream.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 22 | #include "SkTypes.h" |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 23 | #include "SkYUVASizeInfo.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 24 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
msarett | ad8bcfe | 2016-03-07 07:09:03 -0800 | [diff] [blame] | 27 | class SkColorSpace; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 28 | class SkData; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 29 | class SkFrameHolder; |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 30 | class SkPngChunkReader; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 31 | class SkSampler; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 32 | |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 33 | namespace DM { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 34 | class CodecSrc; |
msarett | 9876ac5 | 2016-06-01 14:47:18 -0700 | [diff] [blame] | 35 | class ColorCodecSrc; |
| 36 | } |
| 37 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Abstraction layer directly on top of an image codec. |
| 40 | */ |
Florin Malita | 78c212a | 2016-12-14 13:17:01 -0500 | [diff] [blame] | 41 | class SK_API SkCodec : SkNoncopyable { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 42 | public: |
| 43 | /** |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 44 | * Minimum number of bytes that must be buffered in SkStream input. |
| 45 | * |
| 46 | * An SkStream passed to NewFromStream must be able to use this many |
| 47 | * bytes to determine the image type. Then the same SkStream must be |
| 48 | * passed to the correct decoder to read from the beginning. |
| 49 | * |
| 50 | * This can be accomplished by implementing peek() to support peeking |
| 51 | * this many bytes, or by implementing rewind() to be able to rewind() |
| 52 | * after reading this many bytes. |
| 53 | */ |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 54 | static constexpr size_t MinBufferedBytesNeeded() { return 32; } |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 57 | * Error codes for various SkCodec methods. |
| 58 | */ |
| 59 | enum Result { |
| 60 | /** |
| 61 | * General return value for success. |
| 62 | */ |
| 63 | kSuccess, |
| 64 | /** |
| 65 | * The input is incomplete. A partial image was generated. |
| 66 | */ |
| 67 | kIncompleteInput, |
| 68 | /** |
| 69 | * Like kIncompleteInput, except the input had an error. |
| 70 | * |
| 71 | * If returned from an incremental decode, decoding cannot continue, |
| 72 | * even with more data. |
| 73 | */ |
| 74 | kErrorInInput, |
| 75 | /** |
| 76 | * The generator cannot convert to match the request, ignoring |
| 77 | * dimensions. |
| 78 | */ |
| 79 | kInvalidConversion, |
| 80 | /** |
| 81 | * The generator cannot scale to requested size. |
| 82 | */ |
| 83 | kInvalidScale, |
| 84 | /** |
| 85 | * Parameters (besides info) are invalid. e.g. NULL pixels, rowBytes |
| 86 | * too small, etc. |
| 87 | */ |
| 88 | kInvalidParameters, |
| 89 | /** |
| 90 | * The input did not contain a valid image. |
| 91 | */ |
| 92 | kInvalidInput, |
| 93 | /** |
| 94 | * Fulfilling this request requires rewinding the input, which is not |
| 95 | * supported for this input. |
| 96 | */ |
| 97 | kCouldNotRewind, |
| 98 | /** |
| 99 | * An internal error, such as OOM. |
| 100 | */ |
| 101 | kInternalError, |
| 102 | /** |
| 103 | * This method is not implemented by this codec. |
| 104 | * FIXME: Perhaps this should be kUnsupported? |
| 105 | */ |
| 106 | kUnimplemented, |
| 107 | }; |
| 108 | |
| 109 | /** |
Leon Scroggins III | fe3da02 | 2018-01-16 11:56:54 -0500 | [diff] [blame] | 110 | * Readable string representing the error code. |
| 111 | */ |
| 112 | static const char* ResultToString(Result); |
| 113 | |
| 114 | /** |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 115 | * If this stream represents an encoded image that we know how to decode, |
| 116 | * return an SkCodec that can decode it. Otherwise return NULL. |
| 117 | * |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 118 | * As stated above, this call must be able to peek or read |
| 119 | * MinBufferedBytesNeeded to determine the correct format, and then start |
| 120 | * reading from the beginning. First it will attempt to peek, and it |
| 121 | * assumes that if less than MinBufferedBytesNeeded bytes (but more than |
| 122 | * zero) are returned, this is because the stream is shorter than this, |
| 123 | * so falling back to reading would not provide more data. If peek() |
| 124 | * returns zero bytes, this call will instead attempt to read(). This |
| 125 | * will require that the stream can be rewind()ed. |
| 126 | * |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 127 | * If Result is not NULL, it will be set to either kSuccess if an SkCodec |
| 128 | * is returned or a reason for the failure if NULL is returned. |
| 129 | * |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 130 | * If SkPngChunkReader is not NULL, take a ref and pass it to libpng if |
| 131 | * the image is a png. |
| 132 | * |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 133 | * If the SkPngChunkReader is not NULL then: |
| 134 | * If the image is not a PNG, the SkPngChunkReader will be ignored. |
| 135 | * If the image is a PNG, the SkPngChunkReader will be reffed. |
| 136 | * If the PNG has unknown chunks, the SkPngChunkReader will be used |
| 137 | * to handle these chunks. SkPngChunkReader will be called to read |
| 138 | * any unknown chunk at any point during the creation of the codec |
| 139 | * or the decode. Note that if SkPngChunkReader fails to read a |
| 140 | * chunk, this could result in a failure to create the codec or a |
| 141 | * failure to decode the image. |
| 142 | * If the PNG does not contain unknown chunks, the SkPngChunkReader |
| 143 | * will not be used or modified. |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 144 | * |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 145 | * If NULL is returned, the stream is deleted immediately. Otherwise, the |
| 146 | * SkCodec takes ownership of it, and will delete it when done with it. |
| 147 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 148 | static std::unique_ptr<SkCodec> MakeFromStream(std::unique_ptr<SkStream>, Result* = nullptr, |
| 149 | SkPngChunkReader* = nullptr); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * If this data represents an encoded image that we know how to decode, |
| 153 | * return an SkCodec that can decode it. Otherwise return NULL. |
| 154 | * |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 155 | * If the SkPngChunkReader is not NULL then: |
| 156 | * If the image is not a PNG, the SkPngChunkReader will be ignored. |
| 157 | * If the image is a PNG, the SkPngChunkReader will be reffed. |
| 158 | * If the PNG has unknown chunks, the SkPngChunkReader will be used |
| 159 | * to handle these chunks. SkPngChunkReader will be called to read |
| 160 | * any unknown chunk at any point during the creation of the codec |
| 161 | * or the decode. Note that if SkPngChunkReader fails to read a |
| 162 | * chunk, this could result in a failure to create the codec or a |
| 163 | * failure to decode the image. |
| 164 | * If the PNG does not contain unknown chunks, the SkPngChunkReader |
| 165 | * will not be used or modified. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 166 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 167 | static std::unique_ptr<SkCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr); |
| 168 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 169 | virtual ~SkCodec(); |
| 170 | |
| 171 | /** |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 172 | * Return a reasonable SkImageInfo to decode into. |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 173 | */ |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 174 | SkImageInfo getInfo() const { return fEncodedInfo.makeImageInfo(); } |
| 175 | |
| 176 | SkISize dimensions() const { return {fEncodedInfo.width(), fEncodedInfo.height()}; } |
| 177 | SkIRect bounds() const { |
| 178 | return SkIRect::MakeWH(fEncodedInfo.width(), fEncodedInfo.height()); |
| 179 | } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 180 | |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 181 | /** |
| 182 | * Returns the image orientation stored in the EXIF data. |
| 183 | * If there is no EXIF data, or if we cannot read the EXIF data, returns kTopLeft. |
| 184 | */ |
Leon Scroggins III | b6ab10f | 2017-10-18 14:42:43 -0400 | [diff] [blame] | 185 | SkEncodedOrigin getOrigin() const { return fOrigin; } |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 186 | |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 187 | /** |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 188 | * Return a size that approximately supports the desired scale factor. |
| 189 | * The codec may not be able to scale efficiently to the exact scale |
| 190 | * factor requested, so return a size that approximates that scale. |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 191 | * The returned value is the codec's suggestion for the closest valid |
| 192 | * scale that it can natively support |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 193 | */ |
scroggo | fffeede | 2015-03-18 10:50:37 -0700 | [diff] [blame] | 194 | SkISize getScaledDimensions(float desiredScale) const { |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 195 | // Negative and zero scales are errors. |
| 196 | SkASSERT(desiredScale > 0.0f); |
| 197 | if (desiredScale <= 0.0f) { |
| 198 | return SkISize::Make(0, 0); |
| 199 | } |
| 200 | |
| 201 | // Upscaling is not supported. Return the original size if the client |
| 202 | // requests an upscale. |
| 203 | if (desiredScale >= 1.0f) { |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 204 | return this->dimensions(); |
msarett | b32758a | 2015-08-18 13:22:46 -0700 | [diff] [blame] | 205 | } |
scroggo | fffeede | 2015-03-18 10:50:37 -0700 | [diff] [blame] | 206 | return this->onGetScaledDimensions(desiredScale); |
| 207 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 208 | |
scroggo | 1dd3ea9 | 2015-03-20 11:55:55 -0700 | [diff] [blame] | 209 | /** |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 210 | * Return (via desiredSubset) a subset which can decoded from this codec, |
| 211 | * or false if this codec cannot decode subsets or anything similar to |
| 212 | * desiredSubset. |
| 213 | * |
| 214 | * @param desiredSubset In/out parameter. As input, a desired subset of |
| 215 | * the original bounds (as specified by getInfo). If true is returned, |
| 216 | * desiredSubset may have been modified to a subset which is |
| 217 | * supported. Although a particular change may have been made to |
| 218 | * desiredSubset to create something supported, it is possible other |
| 219 | * changes could result in a valid subset. |
| 220 | * If false is returned, desiredSubset's value is undefined. |
| 221 | * @return true if this codec supports decoding desiredSubset (as |
| 222 | * returned, potentially modified) |
| 223 | */ |
| 224 | bool getValidSubset(SkIRect* desiredSubset) const { |
| 225 | return this->onGetValidSubset(desiredSubset); |
| 226 | } |
| 227 | |
| 228 | /** |
scroggo | 1dd3ea9 | 2015-03-20 11:55:55 -0700 | [diff] [blame] | 229 | * Format of the encoded data. |
| 230 | */ |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 231 | SkEncodedImageFormat getEncodedFormat() const { return this->onGetEncodedFormat(); } |
scroggo | 1dd3ea9 | 2015-03-20 11:55:55 -0700 | [diff] [blame] | 232 | |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 233 | /** |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 234 | * Whether or not the memory passed to getPixels is zero initialized. |
| 235 | */ |
| 236 | enum ZeroInitialized { |
| 237 | /** |
| 238 | * The memory passed to getPixels is zero initialized. The SkCodec |
| 239 | * may take advantage of this by skipping writing zeroes. |
| 240 | */ |
| 241 | kYes_ZeroInitialized, |
| 242 | /** |
| 243 | * The memory passed to getPixels has not been initialized to zero, |
| 244 | * so the SkCodec must write all zeroes to memory. |
| 245 | * |
| 246 | * This is the default. It will be used if no Options struct is used. |
| 247 | */ |
| 248 | kNo_ZeroInitialized, |
| 249 | }; |
| 250 | |
| 251 | /** |
| 252 | * Additional options to pass to getPixels. |
| 253 | */ |
| 254 | struct Options { |
| 255 | Options() |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 256 | : fZeroInitialized(kNo_ZeroInitialized) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 257 | , fSubset(nullptr) |
| 258 | , fFrameIndex(0) |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 259 | , fPriorFrame(kNoFrame) |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 260 | {} |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 261 | |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 262 | ZeroInitialized fZeroInitialized; |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 263 | /** |
| 264 | * If not NULL, represents a subset of the original image to decode. |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 265 | * Must be within the bounds returned by getInfo(). |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 266 | * If the EncodedFormat is SkEncodedImageFormat::kWEBP (the only one which |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 267 | * currently supports subsets), the top and left values must be even. |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 268 | * |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 269 | * In getPixels and incremental decode, we will attempt to decode the |
| 270 | * exact rectangular subset specified by fSubset. |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 271 | * |
| 272 | * In a scanline decode, it does not make sense to specify a subset |
| 273 | * top or subset height, since the client already controls which rows |
| 274 | * to get and which rows to skip. During scanline decodes, we will |
| 275 | * require that the subset top be zero and the subset height be equal |
| 276 | * to the full height. We will, however, use the values of |
| 277 | * subset left and subset width to decode partial scanlines on calls |
| 278 | * to getScanlines(). |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 279 | */ |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 280 | const SkIRect* fSubset; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 281 | |
| 282 | /** |
| 283 | * The frame to decode. |
| 284 | * |
| 285 | * Only meaningful for multi-frame images. |
| 286 | */ |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 287 | int fFrameIndex; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 288 | |
| 289 | /** |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 290 | * If not kNoFrame, the dst already contains the prior frame at this index. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 291 | * |
| 292 | * Only meaningful for multi-frame images. |
| 293 | * |
| 294 | * If fFrameIndex needs to be blended with a prior frame (as reported by |
| 295 | * getFrameInfo[fFrameIndex].fRequiredFrame), the client can set this to |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 296 | * any non-kRestorePrevious frame in [fRequiredFrame, fFrameIndex) to |
| 297 | * indicate that that frame is already in the dst. Options.fZeroInitialized |
| 298 | * is ignored in this case. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 299 | * |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 300 | * If set to kNoFrame, the codec will decode any necessary required frame(s) first. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 301 | */ |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 302 | int fPriorFrame; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | /** |
| 306 | * Decode into the given pixels, a block of memory of size at |
| 307 | * least (info.fHeight - 1) * rowBytes + (info.fWidth * |
| 308 | * bytesPerPixel) |
| 309 | * |
| 310 | * Repeated calls to this function should give the same results, |
| 311 | * allowing the PixelRef to be immutable. |
| 312 | * |
| 313 | * @param info A description of the format (config, size) |
| 314 | * expected by the caller. This can simply be identical |
| 315 | * to the info returned by getInfo(). |
| 316 | * |
| 317 | * This contract also allows the caller to specify |
| 318 | * different output-configs, which the implementation can |
| 319 | * decide to support or not. |
| 320 | * |
| 321 | * A size that does not match getInfo() implies a request |
| 322 | * to scale. If the generator cannot perform this scale, |
| 323 | * it will return kInvalidScale. |
| 324 | * |
msarett | 50ce1f2 | 2016-07-29 06:23:33 -0700 | [diff] [blame] | 325 | * If the info contains a non-null SkColorSpace, the codec |
| 326 | * will perform the appropriate color space transformation. |
| 327 | * If the caller passes in the same color space that was |
| 328 | * reported by the codec, the color space transformation is |
| 329 | * a no-op. |
| 330 | * |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 331 | * If a scanline decode is in progress, scanline mode will end, requiring the client to call |
| 332 | * startScanlineDecode() in order to return to decoding scanlines. |
| 333 | * |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 334 | * @return Result kSuccess, or another value explaining the type of failure. |
| 335 | */ |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 336 | Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options*); |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 337 | |
| 338 | /** |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 339 | * Simplified version of getPixels() that uses the default Options. |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 340 | */ |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 341 | Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 342 | return this->getPixels(info, pixels, rowBytes, nullptr); |
| 343 | } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 344 | |
Mike Reed | 4379869 | 2017-10-17 18:04:32 +0000 | [diff] [blame] | 345 | Result getPixels(const SkPixmap& pm, const Options* opts = nullptr) { |
| 346 | return this->getPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), opts); |
| 347 | } |
| 348 | |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 349 | /** |
| 350 | * If decoding to YUV is supported, this returns true. Otherwise, this |
| 351 | * returns false and does not modify any of the parameters. |
| 352 | * |
| 353 | * @param sizeInfo Output parameter indicating the sizes and required |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 354 | * allocation widths of the Y, U, V, and A planes. Given current codec |
| 355 | * limitations the size of the A plane will always be 0 and the Y, U, V |
| 356 | * channels will always be planar. |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 357 | * @param colorSpace Output parameter. If non-NULL this is set to kJPEG, |
| 358 | * otherwise this is ignored. |
| 359 | */ |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 360 | bool queryYUV8(SkYUVASizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const { |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 361 | if (nullptr == sizeInfo) { |
| 362 | return false; |
| 363 | } |
| 364 | |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 365 | bool result = this->onQueryYUV8(sizeInfo, colorSpace); |
| 366 | if (result) { |
| 367 | for (int i = 0; i <= 2; ++i) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 368 | SkASSERT(sizeInfo->fSizes[i].fWidth > 0 && sizeInfo->fSizes[i].fHeight > 0 && |
| 369 | sizeInfo->fWidthBytes[i] > 0); |
| 370 | } |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 371 | SkASSERT(!sizeInfo->fSizes[3].fWidth && |
| 372 | !sizeInfo->fSizes[3].fHeight && |
| 373 | !sizeInfo->fWidthBytes[3]); |
| 374 | } |
| 375 | return result; |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Returns kSuccess, or another value explaining the type of failure. |
| 380 | * This always attempts to perform a full decode. If the client only |
| 381 | * wants size, it should call queryYUV8(). |
| 382 | * |
| 383 | * @param sizeInfo Needs to exactly match the values returned by the |
| 384 | * query, except the WidthBytes may be larger than the |
| 385 | * recommendation (but not smaller). |
| 386 | * @param planes Memory for each of the Y, U, and V planes. |
| 387 | */ |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 388 | Result getYUV8Planes(const SkYUVASizeInfo& sizeInfo, void* planes[SkYUVASizeInfo::kMaxCount]) { |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 389 | if (!planes || !planes[0] || !planes[1] || !planes[2]) { |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 390 | return kInvalidInput; |
| 391 | } |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 392 | SkASSERT(!planes[3]); // TODO: is this a fair assumption? |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 393 | |
| 394 | if (!this->rewindIfNeeded()) { |
| 395 | return kCouldNotRewind; |
| 396 | } |
| 397 | |
| 398 | return this->onGetYUV8Planes(sizeInfo, planes); |
| 399 | } |
| 400 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 401 | /** |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 402 | * Prepare for an incremental decode with the specified options. |
| 403 | * |
| 404 | * This may require a rewind. |
| 405 | * |
| 406 | * @param dstInfo Info of the destination. If the dimensions do not match |
| 407 | * those of getInfo, this implies a scale. |
| 408 | * @param dst Memory to write to. Needs to be large enough to hold the subset, |
| 409 | * if present, or the full image as described in dstInfo. |
| 410 | * @param options Contains decoding options, including if memory is zero |
| 411 | * initialized and whether to decode a subset. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 412 | * @return Enum representing success or reason for failure. |
| 413 | */ |
| 414 | Result startIncrementalDecode(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 415 | const Options*); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 416 | |
| 417 | Result startIncrementalDecode(const SkImageInfo& dstInfo, void* dst, size_t rowBytes) { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 418 | return this->startIncrementalDecode(dstInfo, dst, rowBytes, nullptr); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Start/continue the incremental decode. |
| 423 | * |
| 424 | * Not valid to call before calling startIncrementalDecode(). |
| 425 | * |
| 426 | * After the first call, should only be called again if more data has been |
| 427 | * provided to the source SkStream. |
| 428 | * |
| 429 | * Unlike getPixels and getScanlines, this does not do any filling. This is |
| 430 | * left up to the caller, since they may be skipping lines or continuing the |
| 431 | * decode later. In the latter case, they may choose to initialize all lines |
| 432 | * first, or only initialize the remaining lines after the first call. |
| 433 | * |
| 434 | * @param rowsDecoded Optional output variable returning the total number of |
| 435 | * lines initialized. Only meaningful if this method returns kIncompleteInput. |
| 436 | * Otherwise the implementation may not set it. |
| 437 | * Note that some implementations may have initialized this many rows, but |
| 438 | * not necessarily finished those rows (e.g. interlaced PNG). This may be |
| 439 | * useful for determining what rows the client needs to initialize. |
| 440 | * @return kSuccess if all lines requested in startIncrementalDecode have |
| 441 | * been completely decoded. kIncompleteInput otherwise. |
| 442 | */ |
| 443 | Result incrementalDecode(int* rowsDecoded = nullptr) { |
| 444 | if (!fStartedIncrementalDecode) { |
| 445 | return kInvalidParameters; |
| 446 | } |
| 447 | return this->onIncrementalDecode(rowsDecoded); |
| 448 | } |
| 449 | |
| 450 | /** |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 451 | * The remaining functions revolve around decoding scanlines. |
| 452 | */ |
| 453 | |
| 454 | /** |
| 455 | * Prepare for a scanline decode with the specified options. |
| 456 | * |
| 457 | * After this call, this class will be ready to decode the first scanline. |
| 458 | * |
| 459 | * This must be called in order to call getScanlines or skipScanlines. |
| 460 | * |
| 461 | * This may require rewinding the stream. |
| 462 | * |
| 463 | * Not all SkCodecs support this. |
| 464 | * |
| 465 | * @param dstInfo Info of the destination. If the dimensions do not match |
| 466 | * those of getInfo, this implies a scale. |
| 467 | * @param options Contains decoding options, including if memory is zero |
| 468 | * initialized. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 469 | * @return Enum representing success or reason for failure. |
| 470 | */ |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 471 | Result startScanlineDecode(const SkImageInfo& dstInfo, const Options* options); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 472 | |
| 473 | /** |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 474 | * Simplified version of startScanlineDecode() that uses the default Options. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 475 | */ |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 476 | Result startScanlineDecode(const SkImageInfo& dstInfo) { |
| 477 | return this->startScanlineDecode(dstInfo, nullptr); |
| 478 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 479 | |
| 480 | /** |
| 481 | * Write the next countLines scanlines into dst. |
| 482 | * |
| 483 | * Not valid to call before calling startScanlineDecode(). |
| 484 | * |
| 485 | * @param dst Must be non-null, and large enough to hold countLines |
| 486 | * scanlines of size rowBytes. |
| 487 | * @param countLines Number of lines to write. |
| 488 | * @param rowBytes Number of bytes per row. Must be large enough to hold |
| 489 | * a scanline based on the SkImageInfo used to create this object. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 490 | * @return the number of lines successfully decoded. If this value is |
| 491 | * less than countLines, this will fill the remaining lines with a |
| 492 | * default value. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 493 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 494 | int getScanlines(void* dst, int countLines, size_t rowBytes); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 495 | |
| 496 | /** |
| 497 | * Skip count scanlines. |
| 498 | * |
| 499 | * Not valid to call before calling startScanlineDecode(). |
| 500 | * |
| 501 | * The default version just calls onGetScanlines and discards the dst. |
| 502 | * NOTE: If skipped lines are the only lines with alpha, this default |
| 503 | * will make reallyHasAlpha return true, when it could have returned |
| 504 | * false. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 505 | * |
| 506 | * @return true if the scanlines were successfully skipped |
| 507 | * false on failure, possible reasons for failure include: |
| 508 | * An incomplete input image stream. |
| 509 | * Calling this function before calling startScanlineDecode(). |
| 510 | * If countLines is less than zero or so large that it moves |
| 511 | * the current scanline past the end of the image. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 512 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 513 | bool skipScanlines(int countLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 514 | |
| 515 | /** |
| 516 | * The order in which rows are output from the scanline decoder is not the |
| 517 | * same for all variations of all image types. This explains the possible |
| 518 | * output row orderings. |
| 519 | */ |
| 520 | enum SkScanlineOrder { |
| 521 | /* |
| 522 | * By far the most common, this indicates that the image can be decoded |
| 523 | * reliably using the scanline decoder, and that rows will be output in |
| 524 | * the logical order. |
| 525 | */ |
| 526 | kTopDown_SkScanlineOrder, |
| 527 | |
| 528 | /* |
| 529 | * This indicates that the scanline decoder reliably outputs rows, but |
| 530 | * they will be returned in reverse order. If the scanline format is |
| 531 | * kBottomUp, the nextScanline() API can be used to determine the actual |
| 532 | * y-coordinate of the next output row, but the client is not forced |
| 533 | * to take advantage of this, given that it's not too tough to keep |
| 534 | * track independently. |
| 535 | * |
| 536 | * For full image decodes, it is safe to get all of the scanlines at |
| 537 | * once, since the decoder will handle inverting the rows as it |
| 538 | * decodes. |
| 539 | * |
| 540 | * For subset decodes and sampling, it is simplest to get and skip |
| 541 | * scanlines one at a time, using the nextScanline() API. It is |
| 542 | * possible to ask for larger chunks at a time, but this should be used |
| 543 | * with caution. As with full image decodes, the decoder will handle |
| 544 | * inverting the requested rows, but rows will still be delivered |
| 545 | * starting from the bottom of the image. |
| 546 | * |
| 547 | * Upside down bmps are an example. |
| 548 | */ |
| 549 | kBottomUp_SkScanlineOrder, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 550 | }; |
| 551 | |
| 552 | /** |
| 553 | * An enum representing the order in which scanlines will be returned by |
| 554 | * the scanline decoder. |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 555 | * |
| 556 | * This is undefined before startScanlineDecode() is called. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 557 | */ |
| 558 | SkScanlineOrder getScanlineOrder() const { return this->onGetScanlineOrder(); } |
| 559 | |
| 560 | /** |
| 561 | * Returns the y-coordinate of the next row to be returned by the scanline |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 562 | * decoder. |
| 563 | * |
| 564 | * This will equal fCurrScanline, except in the case of strangely |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 565 | * encoded image types (bottom-up bmps). |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 566 | * |
| 567 | * Results are undefined when not in scanline decoding mode. |
| 568 | */ |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 569 | int nextScanline() const { return this->outputScanline(fCurrScanline); } |
| 570 | |
| 571 | /** |
msarett | cb0d5c9 | 2015-12-03 12:23:43 -0800 | [diff] [blame] | 572 | * Returns the output y-coordinate of the row that corresponds to an input |
| 573 | * y-coordinate. The input y-coordinate represents where the scanline |
| 574 | * is located in the encoded data. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 575 | * |
| 576 | * This will equal inputScanline, except in the case of strangely |
| 577 | * encoded image types (bottom-up bmps, interlaced gifs). |
| 578 | */ |
| 579 | int outputScanline(int inputScanline) const; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 580 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 581 | /** |
| 582 | * Return the number of frames in the image. |
| 583 | * |
| 584 | * May require reading through the stream. |
| 585 | */ |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 586 | int getFrameCount() { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 587 | return this->onGetFrameCount(); |
| 588 | } |
| 589 | |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 590 | // Sentinel value used when a frame index implies "no frame": |
| 591 | // - FrameInfo::fRequiredFrame set to this value means the frame |
| 592 | // is independent. |
| 593 | // - Options::fPriorFrame set to this value means no (relevant) prior frame |
| 594 | // is residing in dst's memory. |
| 595 | static constexpr int kNoFrame = -1; |
| 596 | |
Mike Klein | eb4d641 | 2018-11-12 16:08:30 +0000 | [diff] [blame] | 597 | // This transitional definition was added in August 2018, and will eventually be removed. |
| 598 | #ifdef SK_LEGACY_SKCODEC_NONE_ENUM |
| 599 | static constexpr int kNone = kNoFrame; |
| 600 | #endif |
| 601 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 602 | /** |
| 603 | * Information about individual frames in a multi-framed image. |
| 604 | */ |
| 605 | struct FrameInfo { |
| 606 | /** |
| 607 | * The frame that this frame needs to be blended with, or |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 608 | * kNoFrame if this frame is independent. |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 609 | * |
| 610 | * Note that this is the *earliest* frame that can be used |
| 611 | * for blending. Any frame from [fRequiredFrame, i) can be |
| 612 | * used, unless its fDisposalMethod is kRestorePrevious. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 613 | */ |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 614 | int fRequiredFrame; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 615 | |
| 616 | /** |
| 617 | * Number of milliseconds to show this frame. |
| 618 | */ |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 619 | int fDuration; |
Leon Scroggins III | 3639faa | 2016-12-08 11:38:58 -0500 | [diff] [blame] | 620 | |
| 621 | /** |
| 622 | * Whether the end marker for this frame is contained in the stream. |
| 623 | * |
| 624 | * Note: this does not guarantee that an attempt to decode will be complete. |
| 625 | * There could be an error in the stream. |
| 626 | */ |
| 627 | bool fFullyReceived; |
Leon Scroggins III | a4db9be | 2017-04-11 10:32:02 -0400 | [diff] [blame] | 628 | |
| 629 | /** |
| 630 | * This is conservative; it will still return non-opaque if e.g. a |
| 631 | * color index-based frame has a color with alpha but does not use it. |
| 632 | */ |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 633 | SkAlphaType fAlphaType; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 634 | |
| 635 | /** |
| 636 | * How this frame should be modified before decoding the next one. |
| 637 | */ |
| 638 | SkCodecAnimation::DisposalMethod fDisposalMethod; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 639 | }; |
| 640 | |
| 641 | /** |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 642 | * Return info about a single frame. |
| 643 | * |
| 644 | * Only supported by multi-frame images. Does not read through the stream, |
| 645 | * so it should be called after getFrameCount() to parse any frames that |
| 646 | * have not already been parsed. |
| 647 | */ |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 648 | bool getFrameInfo(int index, FrameInfo* info) const { |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 649 | if (index < 0) { |
| 650 | return false; |
| 651 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 652 | return this->onGetFrameInfo(index, info); |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Return info about all the frames in the image. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 657 | * |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 658 | * May require reading through the stream to determine info about the |
| 659 | * frames (including the count). |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 660 | * |
| 661 | * As such, future decoding calls may require a rewind. |
| 662 | * |
Nigel Tao | 96597c2 | 2018-08-22 10:36:12 +1000 | [diff] [blame] | 663 | * For still (non-animated) image codecs, this will return an empty vector. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 664 | */ |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 665 | std::vector<FrameInfo> getFrameInfo(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 666 | |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 667 | static constexpr int kRepetitionCountInfinite = -1; |
| 668 | |
| 669 | /** |
Nigel Tao | 96597c2 | 2018-08-22 10:36:12 +1000 | [diff] [blame] | 670 | * Return the number of times to repeat, if this image is animated. This number does not |
| 671 | * include the first play through of each frame. For example, a repetition count of 4 means |
| 672 | * that each frame is played 5 times and then the animation stops. |
| 673 | * |
| 674 | * It can return kRepetitionCountInfinite, a negative number, meaning that the animation |
| 675 | * should loop forever. |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 676 | * |
| 677 | * May require reading the stream to find the repetition count. |
| 678 | * |
| 679 | * As such, future decoding calls may require a rewind. |
| 680 | * |
Nigel Tao | 96597c2 | 2018-08-22 10:36:12 +1000 | [diff] [blame] | 681 | * For still (non-animated) image codecs, this will return 0. |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 682 | */ |
| 683 | int getRepetitionCount() { |
| 684 | return this->onGetRepetitionCount(); |
| 685 | } |
| 686 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 687 | protected: |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 688 | const SkEncodedInfo& getEncodedInfo() const { return fEncodedInfo; } |
| 689 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 690 | using XformFormat = skcms_PixelFormat; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 691 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 692 | SkCodec(SkEncodedInfo&&, |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 693 | XformFormat srcFormat, |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 694 | std::unique_ptr<SkStream>, |
Leon Scroggins III | b6ab10f | 2017-10-18 14:42:43 -0400 | [diff] [blame] | 695 | SkEncodedOrigin = kTopLeft_SkEncodedOrigin); |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 696 | |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 697 | virtual SkISize onGetScaledDimensions(float /*desiredScale*/) const { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 698 | // By default, scaling is not supported. |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 699 | return this->dimensions(); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 700 | } |
| 701 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 702 | // FIXME: What to do about subsets?? |
| 703 | /** |
| 704 | * Subclasses should override if they support dimensions other than the |
| 705 | * srcInfo's. |
| 706 | */ |
| 707 | virtual bool onDimensionsSupported(const SkISize&) { |
| 708 | return false; |
| 709 | } |
| 710 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 711 | virtual SkEncodedImageFormat onGetEncodedFormat() const = 0; |
scroggo | 1dd3ea9 | 2015-03-20 11:55:55 -0700 | [diff] [blame] | 712 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 713 | /** |
| 714 | * @param rowsDecoded When the encoded image stream is incomplete, this function |
| 715 | * will return kIncompleteInput and rowsDecoded will be set to |
| 716 | * the number of scanlines that were successfully decoded. |
| 717 | * This will allow getPixels() to fill the uninitialized memory. |
| 718 | */ |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 719 | virtual Result onGetPixels(const SkImageInfo& info, |
| 720 | void* pixels, size_t rowBytes, const Options&, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 721 | int* rowsDecoded) = 0; |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 722 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 723 | virtual bool onQueryYUV8(SkYUVASizeInfo*, SkYUVColorSpace*) const { |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 724 | return false; |
| 725 | } |
| 726 | |
Jim Van Verth | e24b587 | 2018-10-29 16:26:02 -0400 | [diff] [blame] | 727 | virtual Result onGetYUV8Planes(const SkYUVASizeInfo&, |
| 728 | void*[SkYUVASizeInfo::kMaxCount] /*planes*/) { |
msarett | b714fb0 | 2016-01-22 14:46:42 -0800 | [diff] [blame] | 729 | return kUnimplemented; |
| 730 | } |
| 731 | |
| 732 | virtual bool onGetValidSubset(SkIRect* /*desiredSubset*/) const { |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 733 | // By default, subsets are not supported. |
| 734 | return false; |
| 735 | } |
| 736 | |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 737 | /** |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 738 | * If the stream was previously read, attempt to rewind. |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 739 | * |
| 740 | * If the stream needed to be rewound, call onRewind. |
| 741 | * @returns true if the codec is at the right position and can be used. |
| 742 | * false if there was a failure to rewind. |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 743 | * |
Nigel Tao | 13df981 | 2018-08-08 14:33:58 +1000 | [diff] [blame] | 744 | * This is called by getPixels(), getYUV8Planes(), startIncrementalDecode() and |
| 745 | * startScanlineDecode(). Subclasses may call if they need to rewind at another time. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 746 | */ |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 747 | bool SK_WARN_UNUSED_RESULT rewindIfNeeded(); |
| 748 | |
| 749 | /** |
| 750 | * Called by rewindIfNeeded, if the stream needed to be rewound. |
| 751 | * |
| 752 | * Subclasses should do any set up needed after a rewind. |
| 753 | */ |
| 754 | virtual bool onRewind() { |
| 755 | return true; |
| 756 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 757 | |
msarett | c0e80c1 | 2015-07-01 06:50:35 -0700 | [diff] [blame] | 758 | /** |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 759 | * Get method for the input stream |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 760 | */ |
| 761 | SkStream* stream() { |
| 762 | return fStream.get(); |
| 763 | } |
| 764 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 765 | /** |
| 766 | * The remaining functions revolve around decoding scanlines. |
| 767 | */ |
| 768 | |
| 769 | /** |
| 770 | * Most images types will be kTopDown and will not need to override this function. |
| 771 | */ |
| 772 | virtual SkScanlineOrder onGetScanlineOrder() const { return kTopDown_SkScanlineOrder; } |
| 773 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 774 | const SkImageInfo& dstInfo() const { return fDstInfo; } |
| 775 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 776 | const Options& options() const { return fOptions; } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 777 | |
msarett | cb0d5c9 | 2015-12-03 12:23:43 -0800 | [diff] [blame] | 778 | /** |
| 779 | * Returns the number of scanlines that have been decoded so far. |
| 780 | * This is unaffected by the SkScanlineOrder. |
| 781 | * |
| 782 | * Returns -1 if we have not started a scanline decode. |
| 783 | */ |
| 784 | int currScanline() const { return fCurrScanline; } |
| 785 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 786 | virtual int onOutputScanline(int inputScanline) const; |
| 787 | |
Leon Scroggins III | 9b0ba2c | 2018-11-19 14:52:37 -0500 | [diff] [blame^] | 788 | /** |
| 789 | * Return whether we can convert to dst. |
| 790 | * |
| 791 | * Will be called for the appropriate frame, prior to initializing the colorXform. |
| 792 | */ |
| 793 | virtual bool conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, |
| 794 | bool needsColorXform); |
| 795 | |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 796 | // Some classes never need a colorXform e.g. |
| 797 | // - ICO uses its embedded codec's colorXform |
| 798 | // - WBMP is just Black/White |
| 799 | virtual bool usesColorXform() const { return true; } |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 800 | void applyColorXform(void* dst, const void* src, int count) const; |
| 801 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 802 | bool colorXform() const { return fXformTime != kNo_XformTime; } |
| 803 | bool xformOnDecode() const { return fXformTime == kDecodeRow_XformTime; } |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 804 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 805 | virtual int onGetFrameCount() { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 806 | return 1; |
| 807 | } |
| 808 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 809 | virtual bool onGetFrameInfo(int, FrameInfo*) const { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 810 | return false; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 811 | } |
| 812 | |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 813 | virtual int onGetRepetitionCount() { |
| 814 | return 0; |
| 815 | } |
| 816 | |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 817 | private: |
| 818 | const SkEncodedInfo fEncodedInfo; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 819 | const XformFormat fSrcXformFormat; |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 820 | std::unique_ptr<SkStream> fStream; |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 821 | bool fNeedsRewind; |
Leon Scroggins III | b6ab10f | 2017-10-18 14:42:43 -0400 | [diff] [blame] | 822 | const SkEncodedOrigin fOrigin; |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 823 | |
| 824 | SkImageInfo fDstInfo; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 825 | Options fOptions; |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 826 | |
| 827 | enum XformTime { |
| 828 | kNo_XformTime, |
| 829 | kPalette_XformTime, |
| 830 | kDecodeRow_XformTime, |
| 831 | }; |
| 832 | XformTime fXformTime; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 833 | XformFormat fDstXformFormat; // Based on fDstInfo. |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 834 | skcms_ICCProfile fDstProfile; |
| 835 | skcms_AlphaFormat fDstXformAlphaFormat; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 836 | |
| 837 | // Only meaningful during scanline decodes. |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 838 | int fCurrScanline; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 839 | |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 840 | bool fStartedIncrementalDecode; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 841 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 842 | bool initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha, bool srcIsOpaque); |
| 843 | |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 844 | /** |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 845 | * Return whether these dimensions are supported as a scale. |
| 846 | * |
| 847 | * The codec may choose to cache the information about scale and subset. |
| 848 | * Either way, the same information will be passed to onGetPixels/onStart |
| 849 | * on success. |
| 850 | * |
| 851 | * This must return true for a size returned from getScaledDimensions. |
| 852 | */ |
| 853 | bool dimensionsSupported(const SkISize& dim) { |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 854 | return dim == this->dimensions() || this->onDimensionsSupported(dim); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 857 | /** |
| 858 | * For multi-framed images, return the object with information about the frames. |
| 859 | */ |
| 860 | virtual const SkFrameHolder* getFrameHolder() const { |
| 861 | return nullptr; |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * Check for a valid Options.fFrameIndex, and decode prior frames if necessary. |
| 866 | */ |
| 867 | Result handleFrameIndex(const SkImageInfo&, void* pixels, size_t rowBytes, const Options&); |
| 868 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 869 | // Methods for scanline decoding. |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 870 | virtual Result onStartScanlineDecode(const SkImageInfo& /*dstInfo*/, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 871 | const Options& /*options*/) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 872 | return kUnimplemented; |
| 873 | } |
| 874 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 875 | virtual Result onStartIncrementalDecode(const SkImageInfo& /*dstInfo*/, void*, size_t, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 876 | const Options&) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 877 | return kUnimplemented; |
| 878 | } |
| 879 | |
| 880 | virtual Result onIncrementalDecode(int*) { |
| 881 | return kUnimplemented; |
| 882 | } |
| 883 | |
| 884 | |
msarett | 9b9497e | 2016-02-11 13:29:36 -0800 | [diff] [blame] | 885 | virtual bool onSkipScanlines(int /*countLines*/) { return false; } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 886 | |
msarett | 33bee09 | 2015-11-11 12:43:07 -0800 | [diff] [blame] | 887 | virtual int onGetScanlines(void* /*dst*/, int /*countLines*/, size_t /*rowBytes*/) { return 0; } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 888 | |
| 889 | /** |
| 890 | * On an incomplete decode, getPixels() and getScanlines() will call this function |
| 891 | * to fill any uinitialized memory. |
| 892 | * |
| 893 | * @param dstInfo Contains the destination color type |
| 894 | * Contains the destination alpha type |
| 895 | * Contains the destination width |
| 896 | * The height stored in this info is unused |
| 897 | * @param dst Pointer to the start of destination pixel memory |
| 898 | * @param rowBytes Stride length in destination pixel memory |
| 899 | * @param zeroInit Indicates if memory is zero initialized |
| 900 | * @param linesRequested Number of lines that the client requested |
| 901 | * @param linesDecoded Number of lines that were successfully decoded |
| 902 | */ |
| 903 | void fillIncompleteImage(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, |
| 904 | ZeroInitialized zeroInit, int linesRequested, int linesDecoded); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 905 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 906 | /** |
| 907 | * Return an object which will allow forcing scanline decodes to sample in X. |
| 908 | * |
| 909 | * May create a sampler, if one is not currently being used. Otherwise, does |
| 910 | * not affect ownership. |
| 911 | * |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 912 | * Only valid during scanline decoding or incremental decoding. |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 913 | */ |
msarett | 33bee09 | 2015-11-11 12:43:07 -0800 | [diff] [blame] | 914 | virtual SkSampler* getSampler(bool /*createIfNecessary*/) { return nullptr; } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 915 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 916 | friend class DM::CodecSrc; // for fillIncompleteImage |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 917 | friend class SkSampledCodec; |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 918 | friend class SkIcoCodec; |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 919 | friend class SkAndroidCodec; // for fEncodedInfo |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 920 | }; |
| 921 | #endif // SkCodec_DEFINED |