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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/codec/SkCodec.h" |
| 9 | #include "include/core/SkColorSpace.h" |
| 10 | #include "include/core/SkData.h" |
| 11 | #include "include/private/SkHalf.h" |
| 12 | #include "src/codec/SkBmpCodec.h" |
| 13 | #include "src/codec/SkCodecPriv.h" |
| 14 | #include "src/codec/SkFrameHolder.h" |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 15 | #ifdef SK_HAS_HEIF_LIBRARY |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/codec/SkHeifCodec.h" |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 17 | #endif |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/codec/SkIcoCodec.h" |
| 19 | #include "src/codec/SkJpegCodec.h" |
Leon Scroggins III | a77f30c | 2020-03-09 14:23:30 -0400 | [diff] [blame] | 20 | #ifdef SK_CODEC_DECODES_PNG |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 21 | #include "src/codec/SkPngCodec.h" |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 22 | #endif |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/core/SkStream.h" |
| 24 | #include "src/codec/SkRawCodec.h" |
| 25 | #include "src/codec/SkWbmpCodec.h" |
| 26 | #include "src/codec/SkWebpCodec.h" |
Leon Scroggins III | e93ec68 | 2018-10-26 09:25:51 -0400 | [diff] [blame] | 27 | #ifdef SK_HAS_WUFFS_LIBRARY |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/codec/SkWuffsCodec.h" |
Hal Canary | 2dad990 | 2019-11-20 16:01:31 -0500 | [diff] [blame] | 29 | #elif defined(SK_USE_LIBGIFCODEC) |
Hal Canary | 1ec9a28 | 2019-11-21 10:15:50 -0500 | [diff] [blame] | 30 | #include "SkGifCodec.h" |
Leon Scroggins III | e93ec68 | 2018-10-26 09:25:51 -0400 | [diff] [blame] | 31 | #endif |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 32 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 33 | struct DecoderProc { |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 34 | bool (*IsFormat)(const void*, size_t); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 35 | std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<SkStream>, SkCodec::Result*); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Mike Klein | 159a959 | 2019-04-26 15:47:56 +0000 | [diff] [blame] | 38 | static std::vector<DecoderProc>* decoders() { |
| 39 | static auto* decoders = new std::vector<DecoderProc> { |
Leon Scroggins III | a77f30c | 2020-03-09 14:23:30 -0400 | [diff] [blame] | 40 | #ifdef SK_CODEC_DECODES_JPEG |
Mike Klein | 159a959 | 2019-04-26 15:47:56 +0000 | [diff] [blame] | 41 | { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream }, |
| 42 | #endif |
Leon Scroggins III | a77f30c | 2020-03-09 14:23:30 -0400 | [diff] [blame] | 43 | #ifdef SK_CODEC_DECODES_WEBP |
Mike Klein | 159a959 | 2019-04-26 15:47:56 +0000 | [diff] [blame] | 44 | { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream }, |
| 45 | #endif |
| 46 | #ifdef SK_HAS_WUFFS_LIBRARY |
| 47 | { SkWuffsCodec_IsFormat, SkWuffsCodec_MakeFromStream }, |
Hal Canary | 2dad990 | 2019-11-20 16:01:31 -0500 | [diff] [blame] | 48 | #elif defined(SK_USE_LIBGIFCODEC) |
Mike Klein | 159a959 | 2019-04-26 15:47:56 +0000 | [diff] [blame] | 49 | { SkGifCodec::IsGif, SkGifCodec::MakeFromStream }, |
| 50 | #endif |
Leon Scroggins III | a77f30c | 2020-03-09 14:23:30 -0400 | [diff] [blame] | 51 | #ifdef SK_CODEC_DECODES_PNG |
Mike Klein | 159a959 | 2019-04-26 15:47:56 +0000 | [diff] [blame] | 52 | { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream }, |
| 53 | #endif |
| 54 | { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream }, |
| 55 | { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream }, |
Mike Klein | 159a959 | 2019-04-26 15:47:56 +0000 | [diff] [blame] | 56 | }; |
| 57 | return decoders; |
| 58 | } |
| 59 | |
| 60 | void SkCodec::Register( |
| 61 | bool (*peek)(const void*, size_t), |
| 62 | std::unique_ptr<SkCodec> (*make)(std::unique_ptr<SkStream>, SkCodec::Result*)) { |
| 63 | decoders()->push_back(DecoderProc{peek, make}); |
| 64 | } |
| 65 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 66 | std::unique_ptr<SkCodec> SkCodec::MakeFromStream( |
| 67 | std::unique_ptr<SkStream> stream, Result* outResult, |
| 68 | SkPngChunkReader* chunkReader, SelectionPolicy selectionPolicy) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 69 | Result resultStorage; |
| 70 | if (!outResult) { |
| 71 | outResult = &resultStorage; |
| 72 | } |
| 73 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 74 | if (!stream) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 75 | *outResult = kInvalidInput; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 76 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 77 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 78 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 79 | if (selectionPolicy != SelectionPolicy::kPreferStillImage |
| 80 | && selectionPolicy != SelectionPolicy::kPreferAnimation) { |
| 81 | *outResult = kInvalidParameters; |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 85 | constexpr size_t bytesToRead = MinBufferedBytesNeeded(); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 86 | |
| 87 | char buffer[bytesToRead]; |
| 88 | size_t bytesRead = stream->peek(buffer, bytesToRead); |
| 89 | |
| 90 | // It is also possible to have a complete image less than bytesToRead bytes |
| 91 | // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead. |
| 92 | // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter |
| 93 | // than bytesToRead, so pass that directly to the decoder. |
| 94 | // It also is possible the stream uses too small a buffer for peeking, but |
| 95 | // we trust the caller to use a large enough buffer. |
| 96 | |
| 97 | if (0 == bytesRead) { |
scroggo | 3ab9f2e | 2016-01-06 09:53:34 -0800 | [diff] [blame] | 98 | // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this |
| 99 | // printf could be useful to notice failures. |
| 100 | // SkCodecPrintf("Encoded image data failed to peek!\n"); |
| 101 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 102 | // It is possible the stream does not support peeking, but does support |
| 103 | // rewinding. |
| 104 | // Attempt to read() and pass the actual amount read to the decoder. |
| 105 | bytesRead = stream->read(buffer, bytesToRead); |
| 106 | if (!stream->rewind()) { |
scroggo | 3ab9f2e | 2016-01-06 09:53:34 -0800 | [diff] [blame] | 107 | SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n"); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 108 | *outResult = kCouldNotRewind; |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 109 | return nullptr; |
| 110 | } |
| 111 | } |
| 112 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 113 | // PNG is special, since we want to be able to supply an SkPngChunkReader. |
| 114 | // But this code follows the same pattern as the loop. |
Leon Scroggins III | a77f30c | 2020-03-09 14:23:30 -0400 | [diff] [blame] | 115 | #ifdef SK_CODEC_DECODES_PNG |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 116 | if (SkPngCodec::IsPng(buffer, bytesRead)) { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 117 | return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader); |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 118 | } else |
| 119 | #endif |
| 120 | { |
Mike Klein | 159a959 | 2019-04-26 15:47:56 +0000 | [diff] [blame] | 121 | for (DecoderProc proc : *decoders()) { |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 122 | if (proc.IsFormat(buffer, bytesRead)) { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 123 | return proc.MakeFromStream(std::move(stream), outResult); |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 124 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 125 | } |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 126 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 127 | #ifdef SK_HAS_HEIF_LIBRARY |
| 128 | if (SkHeifCodec::IsHeif(buffer, bytesRead)) { |
| 129 | return SkHeifCodec::MakeFromStream(std::move(stream), selectionPolicy, outResult); |
| 130 | } |
| 131 | #endif |
| 132 | |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 133 | #ifdef SK_CODEC_DECODES_RAW |
| 134 | // Try to treat the input as RAW if all the other checks failed. |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 135 | return SkRawCodec::MakeFromStream(std::move(stream), outResult); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 136 | #endif |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 137 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 138 | |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 139 | if (bytesRead < bytesToRead) { |
| 140 | *outResult = kIncompleteInput; |
| 141 | } else { |
| 142 | *outResult = kUnimplemented; |
| 143 | } |
| 144 | |
msarett | f44631b | 2016-01-13 10:54:20 -0800 | [diff] [blame] | 145 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 148 | std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 149 | if (!data) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 150 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 151 | } |
Mike Reed | 847068c | 2017-07-26 11:35:53 -0400 | [diff] [blame] | 152 | return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 155 | SkCodec::SkCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream, |
| 156 | SkEncodedOrigin origin) |
| 157 | : fEncodedInfo(std::move(info)) |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 158 | , fSrcXformFormat(srcFormat) |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 159 | , fStream(std::move(stream)) |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 160 | , fNeedsRewind(false) |
| 161 | , fOrigin(origin) |
| 162 | , fDstInfo() |
| 163 | , fOptions() |
| 164 | , fCurrScanline(-1) |
Ivan Afanasyev | 2631558 | 2017-10-09 10:09:53 +0700 | [diff] [blame] | 165 | , fStartedIncrementalDecode(false) |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 166 | {} |
| 167 | |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 168 | SkCodec::~SkCodec() {} |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 169 | |
Brian Salomon | 59c60b0 | 2020-09-01 15:01:15 -0400 | [diff] [blame^] | 170 | bool SkCodec::queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes, |
| 171 | SkYUVAPixmapInfo* yuvaPixmapInfo) const { |
Brian Salomon | be0e42c | 2020-08-27 11:00:04 -0400 | [diff] [blame] | 172 | if (!yuvaPixmapInfo) { |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame] | 173 | return false; |
| 174 | } |
Brian Salomon | 59c60b0 | 2020-09-01 15:01:15 -0400 | [diff] [blame^] | 175 | return this->onQueryYUVAInfo(supportedDataTypes, yuvaPixmapInfo) && |
| 176 | yuvaPixmapInfo->isSupported(supportedDataTypes); |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Brian Salomon | be0e42c | 2020-08-27 11:00:04 -0400 | [diff] [blame] | 179 | SkCodec::Result SkCodec::getYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) { |
| 180 | if (!yuvaPixmaps.isValid()) { |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame] | 181 | return kInvalidInput; |
| 182 | } |
| 183 | if (!this->rewindIfNeeded()) { |
| 184 | return kCouldNotRewind; |
| 185 | } |
Brian Salomon | be0e42c | 2020-08-27 11:00:04 -0400 | [diff] [blame] | 186 | return this->onGetYUVAPlanes(yuvaPixmaps); |
Brian Salomon | 87d42e5 | 2020-08-24 09:18:16 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 189 | bool SkCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, bool needsColorXform) { |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 190 | if (!valid_alpha(dst.alphaType(), srcIsOpaque)) { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | switch (dst.colorType()) { |
| 195 | case kRGBA_8888_SkColorType: |
| 196 | case kBGRA_8888_SkColorType: |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 197 | case kRGBA_F16_SkColorType: |
Leon Scroggins III | 196f319 | 2020-02-03 12:39:54 -0500 | [diff] [blame] | 198 | return true; |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 199 | case kRGB_565_SkColorType: |
| 200 | return srcIsOpaque; |
| 201 | case kGray_8_SkColorType: |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 202 | return SkEncodedInfo::kGray_Color == fEncodedInfo.color() && srcIsOpaque; |
Mike Reed | d6cb11e | 2017-11-30 15:33:04 -0500 | [diff] [blame] | 203 | case kAlpha_8_SkColorType: |
| 204 | // conceptually we can convert anything into alpha_8, but we haven't actually coded |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 205 | // all of those other conversions yet. |
| 206 | return SkEncodedInfo::kXAlpha_Color == fEncodedInfo.color(); |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 207 | default: |
| 208 | return false; |
| 209 | } |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 210 | } |
| 211 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 212 | bool SkCodec::rewindIfNeeded() { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 213 | // Store the value of fNeedsRewind so we can update it. Next read will |
| 214 | // require a rewind. |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 215 | const bool needsRewind = fNeedsRewind; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 216 | fNeedsRewind = true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 217 | if (!needsRewind) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 218 | return true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 219 | } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 220 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 221 | // startScanlineDecode will need to be called before decoding scanlines. |
| 222 | fCurrScanline = -1; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 223 | // startIncrementalDecode will need to be called before incrementalDecode. |
| 224 | fStartedIncrementalDecode = false; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 225 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 226 | // Some codecs do not have a stream. They may hold onto their own data or another codec. |
| 227 | // They must handle rewinding themselves. |
| 228 | if (fStream && !fStream->rewind()) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | |
| 232 | return this->onRewind(); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 233 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 234 | |
Leon Scroggins III | 19e3cd4 | 2019-08-07 16:42:04 -0400 | [diff] [blame] | 235 | static SkIRect frame_rect_on_screen(SkIRect frameRect, |
| 236 | const SkIRect& screenRect) { |
| 237 | if (!frameRect.intersect(screenRect)) { |
| 238 | return SkIRect::MakeEmpty(); |
| 239 | } |
| 240 | |
| 241 | return frameRect; |
| 242 | } |
| 243 | |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 244 | bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes, |
| 245 | SkISize srcDimensions, SkIRect prevRect) { |
Leon Scroggins III | 19e3cd4 | 2019-08-07 16:42:04 -0400 | [diff] [blame] | 246 | prevRect = frame_rect_on_screen(prevRect, SkIRect::MakeSize(srcDimensions)); |
| 247 | if (prevRect.isEmpty()) { |
| 248 | return true; |
| 249 | } |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 250 | const auto dimensions = dstInfo.dimensions(); |
| 251 | if (dimensions != srcDimensions) { |
| 252 | SkRect src = SkRect::Make(srcDimensions); |
| 253 | SkRect dst = SkRect::Make(dimensions); |
| 254 | SkMatrix map = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit); |
| 255 | SkRect asRect = SkRect::Make(prevRect); |
| 256 | if (!map.mapRect(&asRect)) { |
| 257 | return false; |
| 258 | } |
| 259 | asRect.roundIn(&prevRect); |
| 260 | if (prevRect.isEmpty()) { |
| 261 | // Down-scaling shrank the empty portion to nothing, |
| 262 | // so nothing to zero. |
| 263 | return true; |
| 264 | } |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 265 | } |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 266 | |
Brian Salomon | 9241a6d | 2019-10-03 13:26:54 -0400 | [diff] [blame] | 267 | const SkImageInfo info = dstInfo.makeDimensions(prevRect.size()); |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 268 | const size_t bpp = dstInfo.bytesPerPixel(); |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 269 | const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes; |
| 270 | void* eraseDst = SkTAddOffset<void>(pixels, offset); |
Leon Scroggins III | e643a9e | 2018-08-03 16:15:04 -0400 | [diff] [blame] | 271 | SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized); |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 272 | return true; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 276 | const Options& options) { |
| 277 | const int index = options.fFrameIndex; |
| 278 | if (0 == index) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 279 | return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque()) |
| 280 | ? kSuccess : kInvalidConversion; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 281 | } |
| 282 | |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 283 | if (index < 0) { |
| 284 | return kInvalidParameters; |
| 285 | } |
| 286 | |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 287 | if (options.fSubset) { |
| 288 | // If we add support for this, we need to update the code that zeroes |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 289 | // a kRestoreBGColor frame. |
| 290 | return kInvalidParameters; |
| 291 | } |
| 292 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 293 | if (index >= this->onGetFrameCount()) { |
| 294 | return kIncompleteInput; |
| 295 | } |
| 296 | |
| 297 | const auto* frameHolder = this->getFrameHolder(); |
| 298 | SkASSERT(frameHolder); |
| 299 | |
| 300 | const auto* frame = frameHolder->getFrame(index); |
| 301 | SkASSERT(frame); |
| 302 | |
| 303 | const int requiredFrame = frame->getRequiredFrame(); |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 304 | if (requiredFrame != kNoFrame) { |
| 305 | if (options.fPriorFrame != kNoFrame) { |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 306 | // Check for a valid frame as a starting point. Alternatively, we could |
| 307 | // treat an invalid frame as not providing one, but rejecting it will |
| 308 | // make it easier to catch the mistake. |
| 309 | if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) { |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 310 | return kInvalidParameters; |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 311 | } |
| 312 | const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame); |
| 313 | switch (prevFrame->getDisposalMethod()) { |
| 314 | case SkCodecAnimation::DisposalMethod::kRestorePrevious: |
| 315 | return kInvalidParameters; |
| 316 | case SkCodecAnimation::DisposalMethod::kRestoreBGColor: |
| 317 | // If a frame after the required frame is provided, there is no |
| 318 | // need to clear, since it must be covered by the desired frame. |
| 319 | if (options.fPriorFrame == requiredFrame) { |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 320 | SkIRect prevRect = prevFrame->frameRect(); |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 321 | if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) { |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 322 | return kInternalError; |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 323 | } |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 324 | } |
| 325 | break; |
| 326 | default: |
| 327 | break; |
| 328 | } |
| 329 | } else { |
| 330 | Options prevFrameOptions(options); |
| 331 | prevFrameOptions.fFrameIndex = requiredFrame; |
| 332 | prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized; |
| 333 | const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions); |
| 334 | if (result != kSuccess) { |
| 335 | return result; |
| 336 | } |
| 337 | const auto* prevFrame = frameHolder->getFrame(requiredFrame); |
| 338 | const auto disposalMethod = prevFrame->getDisposalMethod(); |
| 339 | if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) { |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 340 | auto prevRect = prevFrame->frameRect(); |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 341 | if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) { |
Leon Scroggins III | 7916c0e | 2018-05-24 13:04:06 -0400 | [diff] [blame] | 342 | return kInternalError; |
| 343 | } |
Leon Scroggins III | ae79f32 | 2017-08-18 10:53:24 -0400 | [diff] [blame] | 344 | } |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 348 | return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha()) |
| 349 | ? kSuccess : kInvalidConversion; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 350 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 351 | |
Leon Scroggins III | 196f319 | 2020-02-03 12:39:54 -0500 | [diff] [blame] | 352 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 353 | const Options* options) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 354 | if (kUnknown_SkColorType == info.colorType()) { |
| 355 | return kInvalidConversion; |
| 356 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 357 | if (nullptr == pixels) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 358 | return kInvalidParameters; |
| 359 | } |
| 360 | if (rowBytes < info.minRowBytes()) { |
| 361 | return kInvalidParameters; |
| 362 | } |
| 363 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 364 | if (!this->rewindIfNeeded()) { |
| 365 | return kCouldNotRewind; |
| 366 | } |
| 367 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 368 | // Default options. |
| 369 | Options optsStorage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 370 | if (nullptr == options) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 371 | options = &optsStorage; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 372 | } else { |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 373 | if (options->fSubset) { |
| 374 | SkIRect subset(*options->fSubset); |
| 375 | if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) { |
| 376 | // FIXME: How to differentiate between not supporting subset at all |
| 377 | // and not supporting this particular subset? |
| 378 | return kUnimplemented; |
| 379 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 380 | } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 381 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 382 | |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 383 | const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, |
| 384 | *options); |
| 385 | if (frameIndexResult != kSuccess) { |
| 386 | return frameIndexResult; |
| 387 | } |
| 388 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 389 | // FIXME: Support subsets somehow? Note that this works for SkWebpCodec |
| 390 | // because it supports arbitrary scaling/subset combinations. |
| 391 | if (!this->dimensionsSupported(info.dimensions())) { |
| 392 | return kInvalidScale; |
| 393 | } |
| 394 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 395 | fDstInfo = info; |
Leon Scroggins III | 4288657 | 2017-01-27 13:16:28 -0500 | [diff] [blame] | 396 | fOptions = *options; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 397 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 398 | // On an incomplete decode, the subclass will specify the number of scanlines that it decoded |
| 399 | // successfully. |
| 400 | int rowsDecoded = 0; |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 401 | const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 402 | |
| 403 | // A return value of kIncompleteInput indicates a truncated image stream. |
| 404 | // In this case, we will fill any uninitialized memory with a default value. |
| 405 | // Some subclasses will take care of filling any uninitialized memory on |
| 406 | // their own. They indicate that all of the memory has been filled by |
| 407 | // setting rowsDecoded equal to the height. |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 408 | if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) { |
Leon Scroggins III | 4288657 | 2017-01-27 13:16:28 -0500 | [diff] [blame] | 409 | // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless |
| 410 | // there is a subset. In that case, it will use the width of the subset. From here, the |
| 411 | // subset will only be non-null in the case of SkWebpCodec, but it treats the subset |
| 412 | // differenty from the other codecs, and it needs to use the width specified by the info. |
| 413 | // Set the subset to null so SkWebpCodec uses the correct width. |
| 414 | fOptions.fSubset = nullptr; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 415 | this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(), |
| 416 | rowsDecoded); |
| 417 | } |
| 418 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 419 | return result; |
| 420 | } |
| 421 | |
Leon Scroggins III | 196f319 | 2020-02-03 12:39:54 -0500 | [diff] [blame] | 422 | SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 423 | size_t rowBytes, const SkCodec::Options* options) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 424 | fStartedIncrementalDecode = false; |
| 425 | |
| 426 | if (kUnknown_SkColorType == info.colorType()) { |
| 427 | return kInvalidConversion; |
| 428 | } |
| 429 | if (nullptr == pixels) { |
| 430 | return kInvalidParameters; |
| 431 | } |
| 432 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 433 | // FIXME: If the rows come after the rows of a previous incremental decode, |
| 434 | // we might be able to skip the rewind, but only the implementation knows |
| 435 | // that. (e.g. PNG will always need to rewind, since we called longjmp, but |
| 436 | // a bottom-up BMP could skip rewinding if the new rows are above the old |
| 437 | // rows.) |
| 438 | if (!this->rewindIfNeeded()) { |
| 439 | return kCouldNotRewind; |
| 440 | } |
| 441 | |
| 442 | // Set options. |
| 443 | Options optsStorage; |
| 444 | if (nullptr == options) { |
| 445 | options = &optsStorage; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 446 | } else { |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 447 | if (options->fSubset) { |
| 448 | SkIRect size = SkIRect::MakeSize(info.dimensions()); |
| 449 | if (!size.contains(*options->fSubset)) { |
| 450 | return kInvalidParameters; |
| 451 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 452 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 453 | const int top = options->fSubset->top(); |
| 454 | const int bottom = options->fSubset->bottom(); |
| 455 | if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) { |
| 456 | return kInvalidParameters; |
| 457 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 461 | const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, |
| 462 | *options); |
| 463 | if (frameIndexResult != kSuccess) { |
| 464 | return frameIndexResult; |
| 465 | } |
| 466 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 467 | if (!this->dimensionsSupported(info.dimensions())) { |
| 468 | return kInvalidScale; |
| 469 | } |
| 470 | |
| 471 | fDstInfo = info; |
| 472 | fOptions = *options; |
| 473 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 474 | const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 475 | if (kSuccess == result) { |
| 476 | fStartedIncrementalDecode = true; |
| 477 | } else if (kUnimplemented == result) { |
| 478 | // FIXME: This is temporarily necessary, until we transition SkCodec |
| 479 | // implementations from scanline decoding to incremental decoding. |
| 480 | // SkAndroidCodec will first attempt to use incremental decoding, but |
| 481 | // will fall back to scanline decoding if incremental returns |
| 482 | // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true |
| 483 | // (after potentially rewinding), but we do not want the next call to |
| 484 | // startScanlineDecode() to do a rewind. |
| 485 | fNeedsRewind = false; |
| 486 | } |
| 487 | return result; |
| 488 | } |
| 489 | |
| 490 | |
Leon Scroggins III | 196f319 | 2020-02-03 12:39:54 -0500 | [diff] [blame] | 491 | SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 492 | const SkCodec::Options* options) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 493 | // Reset fCurrScanline in case of failure. |
| 494 | fCurrScanline = -1; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 495 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 496 | if (!this->rewindIfNeeded()) { |
| 497 | return kCouldNotRewind; |
| 498 | } |
| 499 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 500 | // Set options. |
| 501 | Options optsStorage; |
| 502 | if (nullptr == options) { |
| 503 | options = &optsStorage; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 504 | } else if (options->fSubset) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 505 | SkIRect size = SkIRect::MakeSize(info.dimensions()); |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 506 | if (!size.contains(*options->fSubset)) { |
| 507 | return kInvalidInput; |
| 508 | } |
| 509 | |
| 510 | // We only support subsetting in the x-dimension for scanline decoder. |
| 511 | // Subsetting in the y-dimension can be accomplished using skipScanlines(). |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 512 | if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 513 | return kInvalidInput; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 517 | // Scanline decoding only supports decoding the first frame. |
| 518 | if (options->fFrameIndex != 0) { |
| 519 | return kUnimplemented; |
| 520 | } |
| 521 | |
| 522 | // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior |
| 523 | // frames, which is not supported here anyway, so it is safe to pass nullptr/0. |
| 524 | const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options); |
| 525 | if (frameIndexResult != kSuccess) { |
| 526 | return frameIndexResult; |
| 527 | } |
| 528 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 529 | // FIXME: Support subsets somehow? |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 530 | if (!this->dimensionsSupported(info.dimensions())) { |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 531 | return kInvalidScale; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 534 | const Result result = this->onStartScanlineDecode(info, *options); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 535 | if (result != SkCodec::kSuccess) { |
| 536 | return result; |
| 537 | } |
| 538 | |
| 539 | fCurrScanline = 0; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 540 | fDstInfo = info; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 541 | fOptions = *options; |
| 542 | return kSuccess; |
| 543 | } |
| 544 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 545 | int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 546 | if (fCurrScanline < 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 547 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | SkASSERT(!fDstInfo.isEmpty()); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 551 | if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 552 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 553 | } |
| 554 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 555 | const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes); |
| 556 | if (linesDecoded < countLines) { |
| 557 | this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized, |
| 558 | countLines, linesDecoded); |
| 559 | } |
| 560 | fCurrScanline += countLines; |
| 561 | return linesDecoded; |
| 562 | } |
| 563 | |
| 564 | bool SkCodec::skipScanlines(int countLines) { |
| 565 | if (fCurrScanline < 0) { |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | SkASSERT(!fDstInfo.isEmpty()); |
| 570 | if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) { |
| 571 | // Arguably, we could just skip the scanlines which are remaining, |
| 572 | // and return true. We choose to return false so the client |
| 573 | // can catch their bug. |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | bool result = this->onSkipScanlines(countLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 578 | fCurrScanline += countLines; |
| 579 | return result; |
| 580 | } |
| 581 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 582 | int SkCodec::outputScanline(int inputScanline) const { |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 583 | SkASSERT(0 <= inputScanline && inputScanline < fEncodedInfo.height()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 584 | return this->onOutputScanline(inputScanline); |
| 585 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 586 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 587 | int SkCodec::onOutputScanline(int inputScanline) const { |
| 588 | switch (this->getScanlineOrder()) { |
| 589 | case kTopDown_SkScanlineOrder: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 590 | return inputScanline; |
| 591 | case kBottomUp_SkScanlineOrder: |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 592 | return fEncodedInfo.height() - inputScanline - 1; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 593 | default: |
| 594 | // This case indicates an interlaced gif and is implemented by SkGifCodec. |
| 595 | SkASSERT(false); |
| 596 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 597 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 598 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 599 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 600 | void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes, |
| 601 | ZeroInitialized zeroInit, int linesRequested, int linesDecoded) { |
Leon Scroggins III | a6161b1 | 2018-10-18 14:45:26 -0400 | [diff] [blame] | 602 | if (kYes_ZeroInitialized == zeroInit) { |
| 603 | return; |
| 604 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 605 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 606 | const int linesRemaining = linesRequested - linesDecoded; |
| 607 | SkSampler* sampler = this->getSampler(false); |
| 608 | |
Leon Scroggins III | a6161b1 | 2018-10-18 14:45:26 -0400 | [diff] [blame] | 609 | const int fillWidth = sampler ? sampler->fillWidth() : |
| 610 | fOptions.fSubset ? fOptions.fSubset->width() : |
| 611 | info.width() ; |
| 612 | void* fillDst = this->getScanlineOrder() == kBottomUp_SkScanlineOrder ? dst : |
| 613 | SkTAddOffset<void>(dst, linesDecoded * rowBytes); |
| 614 | const auto fillInfo = info.makeWH(fillWidth, linesRemaining); |
| 615 | SkSampler::Fill(fillInfo, fillDst, rowBytes, kNo_ZeroInitialized); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 616 | } |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 617 | |
Leon Scroggins III | 179559f | 2018-12-10 12:30:12 -0500 | [diff] [blame] | 618 | bool sk_select_xform_format(SkColorType colorType, bool forColorTable, |
| 619 | skcms_PixelFormat* outFormat) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 620 | SkASSERT(outFormat); |
| 621 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 622 | switch (colorType) { |
| 623 | case kRGBA_8888_SkColorType: |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 624 | *outFormat = skcms_PixelFormat_RGBA_8888; |
| 625 | break; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 626 | case kBGRA_8888_SkColorType: |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 627 | *outFormat = skcms_PixelFormat_BGRA_8888; |
| 628 | break; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 629 | case kRGB_565_SkColorType: |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 630 | if (forColorTable) { |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 631 | #ifdef SK_PMCOLOR_IS_RGBA |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 632 | *outFormat = skcms_PixelFormat_RGBA_8888; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 633 | #else |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 634 | *outFormat = skcms_PixelFormat_BGRA_8888; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 635 | #endif |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 636 | break; |
| 637 | } |
| 638 | *outFormat = skcms_PixelFormat_BGR_565; |
| 639 | break; |
| 640 | case kRGBA_F16_SkColorType: |
| 641 | *outFormat = skcms_PixelFormat_RGBA_hhhh; |
| 642 | break; |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 643 | case kGray_8_SkColorType: |
| 644 | *outFormat = skcms_PixelFormat_G_8; |
| 645 | break; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 646 | default: |
Leon Scroggins | 83988ed | 2018-08-24 21:41:24 +0000 | [diff] [blame] | 647 | return false; |
Leon Scroggins | 83988ed | 2018-08-24 21:41:24 +0000 | [diff] [blame] | 648 | } |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 649 | return true; |
| 650 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 651 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 652 | bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha, |
| 653 | bool srcIsOpaque) { |
| 654 | fXformTime = kNo_XformTime; |
| 655 | bool needsColorXform = false; |
Leon Scroggins III | 196f319 | 2020-02-03 12:39:54 -0500 | [diff] [blame] | 656 | if (this->usesColorXform()) { |
Leon Scroggins III | 5dd47e4 | 2018-09-27 15:26:48 -0400 | [diff] [blame] | 657 | if (kRGBA_F16_SkColorType == dstInfo.colorType()) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 658 | needsColorXform = true; |
Leon Scroggins III | 196f319 | 2020-02-03 12:39:54 -0500 | [diff] [blame] | 659 | if (dstInfo.colorSpace()) { |
| 660 | dstInfo.colorSpace()->toProfile(&fDstProfile); |
| 661 | } else { |
| 662 | // Use the srcProfile to avoid conversion. |
| 663 | const auto* srcProfile = fEncodedInfo.profile(); |
| 664 | fDstProfile = srcProfile ? *srcProfile : *skcms_sRGB_profile(); |
| 665 | } |
| 666 | } else if (dstInfo.colorSpace()) { |
| 667 | dstInfo.colorSpace()->toProfile(&fDstProfile); |
Leon Scroggins III | 5dd47e4 | 2018-09-27 15:26:48 -0400 | [diff] [blame] | 668 | const auto* srcProfile = fEncodedInfo.profile(); |
| 669 | if (!srcProfile) { |
| 670 | srcProfile = skcms_sRGB_profile(); |
| 671 | } |
| 672 | if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) { |
| 673 | needsColorXform = true; |
| 674 | } |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 678 | if (!this->conversionSupported(dstInfo, srcIsOpaque, needsColorXform)) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 679 | return false; |
| 680 | } |
| 681 | |
| 682 | if (needsColorXform) { |
| 683 | fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color() |
| 684 | || kRGBA_F16_SkColorType == dstInfo.colorType() |
| 685 | ? kDecodeRow_XformTime : kPalette_XformTime; |
Leon Scroggins III | 179559f | 2018-12-10 12:30:12 -0500 | [diff] [blame] | 686 | if (!sk_select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime, |
| 687 | &fDstXformFormat)) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 688 | return false; |
| 689 | } |
| 690 | if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha |
| 691 | && dstInfo.alphaType() == kPremul_SkAlphaType) { |
| 692 | fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded; |
| 693 | } else { |
| 694 | fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul; |
| 695 | } |
| 696 | } |
| 697 | return true; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | void SkCodec::applyColorXform(void* dst, const void* src, int count) const { |
Leon Scroggins III | 5dd47e4 | 2018-09-27 15:26:48 -0400 | [diff] [blame] | 701 | // It is okay for srcProfile to be null. This will use sRGB. |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 702 | const auto* srcProfile = fEncodedInfo.profile(); |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 703 | SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile, |
| 704 | dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile, |
| 705 | count)); |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 706 | } |
| 707 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 708 | std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() { |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 709 | const int frameCount = this->getFrameCount(); |
| 710 | SkASSERT(frameCount >= 0); |
| 711 | if (frameCount <= 0) { |
| 712 | return std::vector<FrameInfo>{}; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 713 | } |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 714 | |
| 715 | if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) { |
| 716 | // Not animated. |
| 717 | return std::vector<FrameInfo>{}; |
| 718 | } |
| 719 | |
| 720 | std::vector<FrameInfo> result(frameCount); |
| 721 | for (int i = 0; i < frameCount; ++i) { |
| 722 | SkAssertResult(this->onGetFrameInfo(i, &result[i])); |
| 723 | } |
| 724 | return result; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 725 | } |
Leon Scroggins III | fe3da02 | 2018-01-16 11:56:54 -0500 | [diff] [blame] | 726 | |
| 727 | const char* SkCodec::ResultToString(Result result) { |
| 728 | switch (result) { |
| 729 | case kSuccess: |
| 730 | return "success"; |
| 731 | case kIncompleteInput: |
| 732 | return "incomplete input"; |
| 733 | case kErrorInInput: |
| 734 | return "error in input"; |
| 735 | case kInvalidConversion: |
| 736 | return "invalid conversion"; |
| 737 | case kInvalidScale: |
| 738 | return "invalid scale"; |
| 739 | case kInvalidParameters: |
| 740 | return "invalid parameters"; |
| 741 | case kInvalidInput: |
| 742 | return "invalid input"; |
| 743 | case kCouldNotRewind: |
| 744 | return "could not rewind"; |
| 745 | case kInternalError: |
| 746 | return "internal error"; |
| 747 | case kUnimplemented: |
| 748 | return "unimplemented"; |
| 749 | default: |
| 750 | SkASSERT(false); |
| 751 | return "bogus result value"; |
| 752 | } |
| 753 | } |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 754 | |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 755 | static bool independent(const SkFrame& frame) { |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 756 | return frame.getRequiredFrame() == SkCodec::kNoFrame; |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | static bool restore_bg(const SkFrame& frame) { |
| 760 | return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor; |
| 761 | } |
| 762 | |
Nigel Tao | b8ec7f1 | 2019-03-26 10:44:58 +1100 | [diff] [blame] | 763 | // As its name suggests, this method computes a frame's alpha (e.g. completely |
| 764 | // opaque, unpremul, binary) and its required frame (a preceding frame that |
| 765 | // this frame depends on, to draw the complete image at this frame's point in |
| 766 | // the animation stream), and calls this frame's setter methods with that |
| 767 | // computed information. |
| 768 | // |
| 769 | // A required frame of kNoFrame means that this frame is independent: drawing |
| 770 | // the complete image at this frame's point in the animation stream does not |
| 771 | // require first preparing the pixel buffer based on another frame. Instead, |
| 772 | // drawing can start from an uninitialized pixel buffer. |
| 773 | // |
| 774 | // "Uninitialized" is from the SkCodec's caller's point of view. In the SkCodec |
| 775 | // implementation, for independent frames, first party Skia code (in src/codec) |
| 776 | // will typically fill the buffer with a uniform background color (e.g. |
| 777 | // transparent black) before calling into third party codec-specific code (e.g. |
| 778 | // libjpeg or libpng). Pixels outside of the frame's rect will remain this |
| 779 | // background color after drawing this frame. For incomplete decodes, pixels |
| 780 | // inside that rect may be (at least temporarily) set to that background color. |
| 781 | // In an incremental decode, later passes may then overwrite that background |
| 782 | // color. |
| 783 | // |
| 784 | // Determining kNoFrame or otherwise involves testing a number of conditions |
| 785 | // sequentially. The first satisfied condition results in setting the required |
| 786 | // frame to kNoFrame (an "INDx" condition) or to a non-negative frame number (a |
| 787 | // "DEPx" condition), and the function returning early. Those "INDx" and "DEPx" |
| 788 | // labels also map to comments in the function body. |
| 789 | // |
| 790 | // - IND1: this frame is the first frame. |
| 791 | // - IND2: this frame fills out the whole image, and it is completely opaque |
| 792 | // or it overwrites (not blends with) the previous frame. |
| 793 | // - IND3: all preceding frames' disposals are kRestorePrevious. |
| 794 | // - IND4: the prevFrame's disposal is kRestoreBGColor, and it fills out the |
| 795 | // whole image or it is itself otherwise independent. |
| 796 | // - DEP5: this frame reports alpha (it is not completely opaque) and it |
| 797 | // blends with (not overwrites) the previous frame. |
| 798 | // - IND6: this frame's rect covers the rects of all preceding frames back to |
| 799 | // and including the most recent independent frame before this frame. |
| 800 | // - DEP7: unconditional. |
| 801 | // |
| 802 | // The "prevFrame" variable initially points to the previous frame (also known |
| 803 | // as the prior frame), but that variable may iterate further backwards over |
| 804 | // the course of this computation. |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 805 | void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) { |
| 806 | const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha; |
| 807 | const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight); |
| 808 | const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect); |
| 809 | |
| 810 | const int i = frame->frameId(); |
| 811 | if (0 == i) { |
| 812 | frame->setHasAlpha(reportsAlpha || frameRect != screenRect); |
Nigel Tao | b8ec7f1 | 2019-03-26 10:44:58 +1100 | [diff] [blame] | 813 | frame->setRequiredFrame(SkCodec::kNoFrame); // IND1 |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 814 | return; |
| 815 | } |
| 816 | |
| 817 | |
| 818 | const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kPriorFrame; |
| 819 | if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) { |
| 820 | frame->setHasAlpha(reportsAlpha); |
Nigel Tao | b8ec7f1 | 2019-03-26 10:44:58 +1100 | [diff] [blame] | 821 | frame->setRequiredFrame(SkCodec::kNoFrame); // IND2 |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 822 | return; |
| 823 | } |
| 824 | |
| 825 | const SkFrame* prevFrame = this->getFrame(i-1); |
| 826 | while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) { |
| 827 | const int prevId = prevFrame->frameId(); |
| 828 | if (0 == prevId) { |
| 829 | frame->setHasAlpha(true); |
Nigel Tao | b8ec7f1 | 2019-03-26 10:44:58 +1100 | [diff] [blame] | 830 | frame->setRequiredFrame(SkCodec::kNoFrame); // IND3 |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 831 | return; |
| 832 | } |
| 833 | |
| 834 | prevFrame = this->getFrame(prevId - 1); |
| 835 | } |
| 836 | |
| 837 | const bool clearPrevFrame = restore_bg(*prevFrame); |
| 838 | auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect); |
| 839 | |
| 840 | if (clearPrevFrame) { |
| 841 | if (prevFrameRect == screenRect || independent(*prevFrame)) { |
| 842 | frame->setHasAlpha(true); |
Nigel Tao | b8ec7f1 | 2019-03-26 10:44:58 +1100 | [diff] [blame] | 843 | frame->setRequiredFrame(SkCodec::kNoFrame); // IND4 |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 844 | return; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | if (reportsAlpha && blendWithPrevFrame) { |
| 849 | // Note: We could be more aggressive here. If prevFrame clears |
| 850 | // to background color and covers its required frame (and that |
| 851 | // frame is independent), prevFrame could be marked independent. |
| 852 | // Would this extra complexity be worth it? |
Nigel Tao | b8ec7f1 | 2019-03-26 10:44:58 +1100 | [diff] [blame] | 853 | frame->setRequiredFrame(prevFrame->frameId()); // DEP5 |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 854 | frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame); |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | while (frameRect.contains(prevFrameRect)) { |
| 859 | const int prevRequiredFrame = prevFrame->getRequiredFrame(); |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 860 | if (prevRequiredFrame == SkCodec::kNoFrame) { |
Nigel Tao | b8ec7f1 | 2019-03-26 10:44:58 +1100 | [diff] [blame] | 861 | frame->setRequiredFrame(SkCodec::kNoFrame); // IND6 |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 862 | frame->setHasAlpha(true); |
| 863 | return; |
| 864 | } |
| 865 | |
| 866 | prevFrame = this->getFrame(prevRequiredFrame); |
| 867 | prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect); |
| 868 | } |
| 869 | |
Nigel Tao | b8ec7f1 | 2019-03-26 10:44:58 +1100 | [diff] [blame] | 870 | frame->setRequiredFrame(prevFrame->frameId()); // DEP7 |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 871 | if (restore_bg(*prevFrame)) { |
| 872 | frame->setHasAlpha(true); |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 873 | return; |
| 874 | } |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 875 | SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep); |
Nigel Tao | a78b6dc | 2018-07-28 16:48:09 +1000 | [diff] [blame] | 876 | frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame)); |
| 877 | } |
| 878 | |