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