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