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