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