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