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