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" |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 12 | #include "SkColorSpaceXform_Base.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 13 | #include "SkData.h" |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 14 | #include "SkFrameHolder.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 15 | #include "SkGifCodec.h" |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 16 | #include "SkHalf.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 17 | #include "SkIcoCodec.h" |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 18 | #include "SkJpegCodec.h" |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 19 | #ifdef SK_HAS_PNG_LIBRARY |
msarett | be1d555 | 2016-01-21 09:05:23 -0800 | [diff] [blame] | 20 | #include "SkPngCodec.h" |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 21 | #endif |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 22 | #include "SkRawCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 23 | #include "SkStream.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 24 | #include "SkWbmpCodec.h" |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 25 | #include "SkWebpCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 26 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 27 | struct DecoderProc { |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 28 | bool (*IsFormat)(const void*, size_t); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 29 | SkCodec* (*NewFromStream)(SkStream*, SkCodec::Result*); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static const DecoderProc gDecoderProcs[] = { |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 33 | #ifdef SK_HAS_JPEG_LIBRARY |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 34 | { SkJpegCodec::IsJpeg, SkJpegCodec::NewFromStream }, |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 35 | #endif |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 36 | #ifdef SK_HAS_WEBP_LIBRARY |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 37 | { SkWebpCodec::IsWebp, SkWebpCodec::NewFromStream }, |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 38 | #endif |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 39 | { SkGifCodec::IsGif, SkGifCodec::NewFromStream }, |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 40 | #ifdef SK_HAS_PNG_LIBRARY |
msarett | 9bde918 | 2015-03-25 05:27:48 -0700 | [diff] [blame] | 41 | { SkIcoCodec::IsIco, SkIcoCodec::NewFromStream }, |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 42 | #endif |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 43 | { SkBmpCodec::IsBmp, SkBmpCodec::NewFromStream }, |
| 44 | { SkWbmpCodec::IsWbmp, SkWbmpCodec::NewFromStream } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 47 | size_t SkCodec::MinBufferedBytesNeeded() { |
| 48 | return WEBP_VP8_HEADER_SIZE; |
| 49 | } |
| 50 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 51 | SkCodec* SkCodec::NewFromStream(SkStream* stream, |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 52 | Result* outResult, SkPngChunkReader* chunkReader) { |
| 53 | Result resultStorage; |
| 54 | if (!outResult) { |
| 55 | outResult = &resultStorage; |
| 56 | } |
| 57 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 58 | if (!stream) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 59 | *outResult = kInvalidInput; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 60 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 61 | } |
scroggo | 0a7e69c | 2015-04-03 07:22:22 -0700 | [diff] [blame] | 62 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 63 | std::unique_ptr<SkStream> streamDeleter(stream); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 64 | |
| 65 | // 14 is enough to read all of the supported types. |
| 66 | const size_t bytesToRead = 14; |
| 67 | SkASSERT(bytesToRead <= MinBufferedBytesNeeded()); |
| 68 | |
| 69 | char buffer[bytesToRead]; |
| 70 | size_t bytesRead = stream->peek(buffer, bytesToRead); |
| 71 | |
| 72 | // It is also possible to have a complete image less than bytesToRead bytes |
| 73 | // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead. |
| 74 | // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter |
| 75 | // than bytesToRead, so pass that directly to the decoder. |
| 76 | // It also is possible the stream uses too small a buffer for peeking, but |
| 77 | // we trust the caller to use a large enough buffer. |
| 78 | |
| 79 | if (0 == bytesRead) { |
scroggo | 3ab9f2e | 2016-01-06 09:53:34 -0800 | [diff] [blame] | 80 | // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this |
| 81 | // printf could be useful to notice failures. |
| 82 | // SkCodecPrintf("Encoded image data failed to peek!\n"); |
| 83 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 84 | // It is possible the stream does not support peeking, but does support |
| 85 | // rewinding. |
| 86 | // Attempt to read() and pass the actual amount read to the decoder. |
| 87 | bytesRead = stream->read(buffer, bytesToRead); |
| 88 | if (!stream->rewind()) { |
scroggo | 3ab9f2e | 2016-01-06 09:53:34 -0800 | [diff] [blame] | 89 | 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^] | 90 | *outResult = kCouldNotRewind; |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 91 | return nullptr; |
| 92 | } |
| 93 | } |
| 94 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 95 | // PNG is special, since we want to be able to supply an SkPngChunkReader. |
| 96 | // But this code follows the same pattern as the loop. |
msarett | ad3a5c6 | 2016-05-06 07:21:26 -0700 | [diff] [blame] | 97 | #ifdef SK_HAS_PNG_LIBRARY |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 98 | if (SkPngCodec::IsPng(buffer, bytesRead)) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 99 | return SkPngCodec::NewFromStream(streamDeleter.release(), outResult, chunkReader); |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 100 | } else |
| 101 | #endif |
| 102 | { |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 103 | for (DecoderProc proc : gDecoderProcs) { |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 104 | if (proc.IsFormat(buffer, bytesRead)) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 105 | return proc.NewFromStream(streamDeleter.release(), outResult); |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 106 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 107 | } |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 108 | |
| 109 | #ifdef SK_CODEC_DECODES_RAW |
| 110 | // Try to treat the input as RAW if all the other checks failed. |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 111 | return SkRawCodec::NewFromStream(streamDeleter.release(), outResult); |
yujieqin | 916de9f | 2016-01-25 08:26:16 -0800 | [diff] [blame] | 112 | #endif |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 113 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 114 | |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 115 | if (bytesRead < bytesToRead) { |
| 116 | *outResult = kIncompleteInput; |
| 117 | } else { |
| 118 | *outResult = kUnimplemented; |
| 119 | } |
| 120 | |
msarett | f44631b | 2016-01-13 10:54:20 -0800 | [diff] [blame] | 121 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 122 | } |
| 123 | |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 124 | SkCodec* SkCodec::NewFromData(sk_sp<SkData> data, SkPngChunkReader* reader) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 125 | if (!data) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 126 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 127 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame^] | 128 | return NewFromStream(new SkMemoryStream(data), nullptr, reader); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 131 | SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info, |
| 132 | XformFormat srcFormat, SkStream* stream, |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 133 | sk_sp<SkColorSpace> colorSpace, Origin origin) |
| 134 | : fEncodedInfo(info) |
msarett | 530c844 | 2016-07-21 11:57:49 -0700 | [diff] [blame] | 135 | , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace))) |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 136 | , fSrcXformFormat(srcFormat) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 137 | , fStream(stream) |
| 138 | , fNeedsRewind(false) |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 139 | , fOrigin(origin) |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 140 | , fDstInfo() |
| 141 | , fOptions() |
| 142 | , fCurrScanline(-1) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 143 | {} |
| 144 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 145 | SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo, |
| 146 | XformFormat srcFormat, SkStream* stream, Origin origin) |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 147 | : fEncodedInfo(info) |
| 148 | , fSrcInfo(imageInfo) |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 149 | , fSrcXformFormat(srcFormat) |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 150 | , fStream(stream) |
| 151 | , fNeedsRewind(false) |
| 152 | , fOrigin(origin) |
| 153 | , fDstInfo() |
| 154 | , fOptions() |
| 155 | , fCurrScanline(-1) |
| 156 | {} |
| 157 | |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 158 | SkCodec::~SkCodec() {} |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 159 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 160 | bool SkCodec::rewindIfNeeded() { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 161 | // Store the value of fNeedsRewind so we can update it. Next read will |
| 162 | // require a rewind. |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 163 | const bool needsRewind = fNeedsRewind; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 164 | fNeedsRewind = true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 165 | if (!needsRewind) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 166 | return true; |
halcanary | a096d7a | 2015-03-27 12:16:53 -0700 | [diff] [blame] | 167 | } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 168 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 169 | // startScanlineDecode will need to be called before decoding scanlines. |
| 170 | fCurrScanline = -1; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 171 | // startIncrementalDecode will need to be called before incrementalDecode. |
| 172 | fStartedIncrementalDecode = false; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 173 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 174 | // Some codecs do not have a stream. They may hold onto their own data or another codec. |
| 175 | // They must handle rewinding themselves. |
| 176 | if (fStream && !fStream->rewind()) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 177 | return false; |
| 178 | } |
| 179 | |
| 180 | return this->onRewind(); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 181 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 182 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 183 | static void zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes, |
| 184 | SkIRect frameRect) { |
| 185 | if (!frameRect.intersect(dstInfo.bounds())) { |
| 186 | return; |
| 187 | } |
| 188 | const auto info = dstInfo.makeWH(frameRect.width(), frameRect.height()); |
| 189 | const size_t bpp = SkColorTypeBytesPerPixel(dstInfo.colorType()); |
| 190 | const size_t offset = frameRect.x() * bpp + frameRect.y() * rowBytes; |
| 191 | auto* eraseDst = SkTAddOffset<void>(pixels, offset); |
| 192 | SkSampler::Fill(info, eraseDst, rowBytes, 0, SkCodec::kNo_ZeroInitialized); |
| 193 | } |
| 194 | |
| 195 | SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 196 | const Options& options) { |
| 197 | const int index = options.fFrameIndex; |
| 198 | if (0 == index) { |
| 199 | return kSuccess; |
| 200 | } |
| 201 | |
| 202 | if (options.fSubset || info.dimensions() != fSrcInfo.dimensions()) { |
| 203 | // If we add support for these, we need to update the code that zeroes |
| 204 | // a kRestoreBGColor frame. |
| 205 | return kInvalidParameters; |
| 206 | } |
| 207 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 208 | if (index >= this->onGetFrameCount()) { |
| 209 | return kIncompleteInput; |
| 210 | } |
| 211 | |
| 212 | const auto* frameHolder = this->getFrameHolder(); |
| 213 | SkASSERT(frameHolder); |
| 214 | |
| 215 | const auto* frame = frameHolder->getFrame(index); |
| 216 | SkASSERT(frame); |
| 217 | |
| 218 | const int requiredFrame = frame->getRequiredFrame(); |
| 219 | if (requiredFrame == kNone) { |
| 220 | return kSuccess; |
| 221 | } |
| 222 | |
| 223 | if (options.fPriorFrame != kNone) { |
| 224 | // Check for a valid frame as a starting point. Alternatively, we could |
| 225 | // treat an invalid frame as not providing one, but rejecting it will |
| 226 | // make it easier to catch the mistake. |
| 227 | if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) { |
| 228 | return kInvalidParameters; |
| 229 | } |
| 230 | const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame); |
| 231 | switch (prevFrame->getDisposalMethod()) { |
| 232 | case SkCodecAnimation::DisposalMethod::kRestorePrevious: |
| 233 | return kInvalidParameters; |
| 234 | case SkCodecAnimation::DisposalMethod::kRestoreBGColor: |
| 235 | // If a frame after the required frame is provided, there is no |
| 236 | // need to clear, since it must be covered by the desired frame. |
| 237 | if (options.fPriorFrame == requiredFrame) { |
| 238 | zero_rect(info, pixels, rowBytes, prevFrame->frameRect()); |
| 239 | } |
| 240 | break; |
| 241 | default: |
| 242 | break; |
| 243 | } |
| 244 | return kSuccess; |
| 245 | } |
| 246 | |
| 247 | Options prevFrameOptions(options); |
| 248 | prevFrameOptions.fFrameIndex = requiredFrame; |
| 249 | prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized; |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 250 | const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions); |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 251 | if (result == kSuccess) { |
| 252 | const auto* prevFrame = frameHolder->getFrame(requiredFrame); |
| 253 | const auto disposalMethod = prevFrame->getDisposalMethod(); |
| 254 | if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) { |
| 255 | zero_rect(info, pixels, rowBytes, prevFrame->frameRect()); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return result; |
| 260 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 261 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 262 | SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 263 | const Options* options) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 264 | if (kUnknown_SkColorType == info.colorType()) { |
| 265 | return kInvalidConversion; |
| 266 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 267 | if (nullptr == pixels) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 268 | return kInvalidParameters; |
| 269 | } |
| 270 | if (rowBytes < info.minRowBytes()) { |
| 271 | return kInvalidParameters; |
| 272 | } |
| 273 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 274 | if (!this->rewindIfNeeded()) { |
| 275 | return kCouldNotRewind; |
| 276 | } |
| 277 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 278 | // Default options. |
| 279 | Options optsStorage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 280 | if (nullptr == options) { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 281 | options = &optsStorage; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 282 | } else { |
| 283 | const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options); |
| 284 | if (frameIndexResult != kSuccess) { |
| 285 | return frameIndexResult; |
| 286 | } |
| 287 | if (options->fSubset) { |
| 288 | SkIRect subset(*options->fSubset); |
| 289 | if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) { |
| 290 | // FIXME: How to differentiate between not supporting subset at all |
| 291 | // and not supporting this particular subset? |
| 292 | return kUnimplemented; |
| 293 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 294 | } |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 295 | } |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 296 | |
| 297 | // FIXME: Support subsets somehow? Note that this works for SkWebpCodec |
| 298 | // because it supports arbitrary scaling/subset combinations. |
| 299 | if (!this->dimensionsSupported(info.dimensions())) { |
| 300 | return kInvalidScale; |
| 301 | } |
| 302 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 303 | fDstInfo = info; |
Leon Scroggins III | 4288657 | 2017-01-27 13:16:28 -0500 | [diff] [blame] | 304 | fOptions = *options; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 305 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 306 | // On an incomplete decode, the subclass will specify the number of scanlines that it decoded |
| 307 | // successfully. |
| 308 | int rowsDecoded = 0; |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 309 | const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 310 | |
| 311 | // A return value of kIncompleteInput indicates a truncated image stream. |
| 312 | // In this case, we will fill any uninitialized memory with a default value. |
| 313 | // Some subclasses will take care of filling any uninitialized memory on |
| 314 | // their own. They indicate that all of the memory has been filled by |
| 315 | // setting rowsDecoded equal to the height. |
Leon Scroggins III | 674a184 | 2017-07-06 12:26:09 -0400 | [diff] [blame] | 316 | if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) { |
Leon Scroggins III | 4288657 | 2017-01-27 13:16:28 -0500 | [diff] [blame] | 317 | // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless |
| 318 | // there is a subset. In that case, it will use the width of the subset. From here, the |
| 319 | // subset will only be non-null in the case of SkWebpCodec, but it treats the subset |
| 320 | // differenty from the other codecs, and it needs to use the width specified by the info. |
| 321 | // Set the subset to null so SkWebpCodec uses the correct width. |
| 322 | fOptions.fSubset = nullptr; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 323 | this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(), |
| 324 | rowsDecoded); |
| 325 | } |
| 326 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 327 | return result; |
| 328 | } |
| 329 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 330 | SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 331 | size_t rowBytes, const SkCodec::Options* options) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 332 | fStartedIncrementalDecode = false; |
| 333 | |
| 334 | if (kUnknown_SkColorType == info.colorType()) { |
| 335 | return kInvalidConversion; |
| 336 | } |
| 337 | if (nullptr == pixels) { |
| 338 | return kInvalidParameters; |
| 339 | } |
| 340 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 341 | // FIXME: If the rows come after the rows of a previous incremental decode, |
| 342 | // we might be able to skip the rewind, but only the implementation knows |
| 343 | // that. (e.g. PNG will always need to rewind, since we called longjmp, but |
| 344 | // a bottom-up BMP could skip rewinding if the new rows are above the old |
| 345 | // rows.) |
| 346 | if (!this->rewindIfNeeded()) { |
| 347 | return kCouldNotRewind; |
| 348 | } |
| 349 | |
| 350 | // Set options. |
| 351 | Options optsStorage; |
| 352 | if (nullptr == options) { |
| 353 | options = &optsStorage; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 354 | } else { |
| 355 | const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes, *options); |
| 356 | if (frameIndexResult != kSuccess) { |
| 357 | return frameIndexResult; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 358 | } |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 359 | if (options->fSubset) { |
| 360 | SkIRect size = SkIRect::MakeSize(info.dimensions()); |
| 361 | if (!size.contains(*options->fSubset)) { |
| 362 | return kInvalidParameters; |
| 363 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 364 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 365 | const int top = options->fSubset->top(); |
| 366 | const int bottom = options->fSubset->bottom(); |
| 367 | if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) { |
| 368 | return kInvalidParameters; |
| 369 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | if (!this->dimensionsSupported(info.dimensions())) { |
| 374 | return kInvalidScale; |
| 375 | } |
| 376 | |
| 377 | fDstInfo = info; |
| 378 | fOptions = *options; |
| 379 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 380 | const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 381 | if (kSuccess == result) { |
| 382 | fStartedIncrementalDecode = true; |
| 383 | } else if (kUnimplemented == result) { |
| 384 | // FIXME: This is temporarily necessary, until we transition SkCodec |
| 385 | // implementations from scanline decoding to incremental decoding. |
| 386 | // SkAndroidCodec will first attempt to use incremental decoding, but |
| 387 | // will fall back to scanline decoding if incremental returns |
| 388 | // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true |
| 389 | // (after potentially rewinding), but we do not want the next call to |
| 390 | // startScanlineDecode() to do a rewind. |
| 391 | fNeedsRewind = false; |
| 392 | } |
| 393 | return result; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 398 | const SkCodec::Options* options) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 399 | // Reset fCurrScanline in case of failure. |
| 400 | fCurrScanline = -1; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 401 | |
scroggo | 3a7701c | 2015-09-30 09:15:14 -0700 | [diff] [blame] | 402 | if (!this->rewindIfNeeded()) { |
| 403 | return kCouldNotRewind; |
| 404 | } |
| 405 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 406 | // Set options. |
| 407 | Options optsStorage; |
| 408 | if (nullptr == options) { |
| 409 | options = &optsStorage; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 410 | } else if (options->fSubset) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 411 | SkIRect size = SkIRect::MakeSize(info.dimensions()); |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 412 | if (!size.contains(*options->fSubset)) { |
| 413 | return kInvalidInput; |
| 414 | } |
| 415 | |
| 416 | // We only support subsetting in the x-dimension for scanline decoder. |
| 417 | // Subsetting in the y-dimension can be accomplished using skipScanlines(). |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 418 | if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) { |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 419 | return kInvalidInput; |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
| 423 | // FIXME: Support subsets somehow? |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 424 | if (!this->dimensionsSupported(info.dimensions())) { |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 425 | return kInvalidScale; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 428 | const Result result = this->onStartScanlineDecode(info, *options); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 429 | if (result != SkCodec::kSuccess) { |
| 430 | return result; |
| 431 | } |
| 432 | |
| 433 | fCurrScanline = 0; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 434 | fDstInfo = info; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 435 | fOptions = *options; |
| 436 | return kSuccess; |
| 437 | } |
| 438 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 439 | int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 440 | if (fCurrScanline < 0) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 441 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | SkASSERT(!fDstInfo.isEmpty()); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 445 | if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 446 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 447 | } |
| 448 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 449 | const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes); |
| 450 | if (linesDecoded < countLines) { |
| 451 | this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized, |
| 452 | countLines, linesDecoded); |
| 453 | } |
| 454 | fCurrScanline += countLines; |
| 455 | return linesDecoded; |
| 456 | } |
| 457 | |
| 458 | bool SkCodec::skipScanlines(int countLines) { |
| 459 | if (fCurrScanline < 0) { |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | SkASSERT(!fDstInfo.isEmpty()); |
| 464 | if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) { |
| 465 | // Arguably, we could just skip the scanlines which are remaining, |
| 466 | // and return true. We choose to return false so the client |
| 467 | // can catch their bug. |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | bool result = this->onSkipScanlines(countLines); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 472 | fCurrScanline += countLines; |
| 473 | return result; |
| 474 | } |
| 475 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 476 | int SkCodec::outputScanline(int inputScanline) const { |
| 477 | SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height()); |
| 478 | return this->onOutputScanline(inputScanline); |
| 479 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 480 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 481 | int SkCodec::onOutputScanline(int inputScanline) const { |
| 482 | switch (this->getScanlineOrder()) { |
| 483 | case kTopDown_SkScanlineOrder: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 484 | return inputScanline; |
| 485 | case kBottomUp_SkScanlineOrder: |
| 486 | return this->getInfo().height() - inputScanline - 1; |
| 487 | default: |
| 488 | // This case indicates an interlaced gif and is implemented by SkGifCodec. |
| 489 | SkASSERT(false); |
| 490 | return 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 491 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 492 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 493 | |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 494 | uint64_t SkCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
| 495 | switch (dstInfo.colorType()) { |
| 496 | case kRGBA_F16_SkColorType: { |
| 497 | static constexpr uint64_t transparentColor = 0; |
| 498 | static constexpr uint64_t opaqueColor = ((uint64_t) SK_Half1) << 48; |
| 499 | return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor; |
| 500 | } |
| 501 | default: { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 502 | // This not only handles the kN32 case, but also k565, kGray8, since |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 503 | // the low bits are zeros. |
| 504 | return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? |
| 505 | SK_ColorBLACK : SK_ColorTRANSPARENT; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 510 | static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes, |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 511 | uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 512 | if (sampler) { |
| 513 | sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit); |
| 514 | } else { |
| 515 | SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes, |
| 520 | ZeroInitialized zeroInit, int linesRequested, int linesDecoded) { |
| 521 | |
| 522 | void* fillDst; |
msarett | f7eb6fc | 2016-09-13 09:04:11 -0700 | [diff] [blame] | 523 | const uint64_t fillValue = this->getFillValue(info); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 524 | const int linesRemaining = linesRequested - linesDecoded; |
| 525 | SkSampler* sampler = this->getSampler(false); |
| 526 | |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 527 | int fillWidth = info.width(); |
| 528 | if (fOptions.fSubset) { |
| 529 | fillWidth = fOptions.fSubset->width(); |
| 530 | } |
| 531 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 532 | switch (this->getScanlineOrder()) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 533 | case kTopDown_SkScanlineOrder: { |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 534 | const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 535 | fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes); |
| 536 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 537 | break; |
| 538 | } |
| 539 | case kBottomUp_SkScanlineOrder: { |
| 540 | fillDst = dst; |
msarett | 91c22b2 | 2016-02-22 12:27:46 -0800 | [diff] [blame] | 541 | const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 542 | fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); |
| 543 | break; |
| 544 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 545 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 546 | } |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 547 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 548 | static inline SkColorSpaceXform::ColorFormat select_xform_format_ct(SkColorType colorType) { |
| 549 | switch (colorType) { |
| 550 | case kRGBA_8888_SkColorType: |
| 551 | return SkColorSpaceXform::kRGBA_8888_ColorFormat; |
| 552 | case kBGRA_8888_SkColorType: |
| 553 | return SkColorSpaceXform::kBGRA_8888_ColorFormat; |
| 554 | case kRGB_565_SkColorType: |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 555 | #ifdef SK_PMCOLOR_IS_RGBA |
| 556 | return SkColorSpaceXform::kRGBA_8888_ColorFormat; |
| 557 | #else |
| 558 | return SkColorSpaceXform::kBGRA_8888_ColorFormat; |
| 559 | #endif |
| 560 | default: |
| 561 | SkASSERT(false); |
| 562 | return SkColorSpaceXform::kRGBA_8888_ColorFormat; |
| 563 | } |
| 564 | } |
| 565 | |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 566 | bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, |
| 567 | SkTransferFunctionBehavior premulBehavior) { |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 568 | fColorXform = nullptr; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 569 | fXformOnDecode = false; |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 570 | bool needsColorCorrectPremul = needs_premul(dstInfo, fEncodedInfo) && |
| 571 | SkTransferFunctionBehavior::kRespect == premulBehavior; |
| 572 | if (needs_color_xform(dstInfo, fSrcInfo, needsColorCorrectPremul)) { |
| 573 | fColorXform = SkColorSpaceXform_Base::New(fSrcInfo.colorSpace(), dstInfo.colorSpace(), |
| 574 | premulBehavior); |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 575 | if (!fColorXform) { |
| 576 | return false; |
| 577 | } |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 578 | |
| 579 | // We will apply the color xform when reading the color table unless F16 is requested. |
| 580 | fXformOnDecode = SkEncodedInfo::kPalette_Color != fEncodedInfo.color() |
| 581 | || kRGBA_F16_SkColorType == dstInfo.colorType(); |
| 582 | if (fXformOnDecode) { |
| 583 | fDstXformFormat = select_xform_format(dstInfo.colorType()); |
| 584 | } else { |
| 585 | fDstXformFormat = select_xform_format_ct(dstInfo.colorType()); |
| 586 | } |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | return true; |
| 590 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 591 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 592 | void SkCodec::applyColorXform(void* dst, const void* src, int count, SkAlphaType at) const { |
| 593 | SkASSERT(fColorXform); |
| 594 | SkAssertResult(fColorXform->apply(fDstXformFormat, dst, |
| 595 | fSrcXformFormat, src, |
| 596 | count, at)); |
| 597 | } |
| 598 | |
| 599 | void SkCodec::applyColorXform(void* dst, const void* src, int count) const { |
| 600 | auto alphaType = select_xform_alpha(fDstInfo.alphaType(), fSrcInfo.alphaType()); |
| 601 | this->applyColorXform(dst, src, count, alphaType); |
| 602 | } |
| 603 | |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 604 | std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() { |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 605 | const int frameCount = this->getFrameCount(); |
| 606 | SkASSERT(frameCount >= 0); |
| 607 | if (frameCount <= 0) { |
| 608 | return std::vector<FrameInfo>{}; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 609 | } |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 610 | |
| 611 | if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) { |
| 612 | // Not animated. |
| 613 | return std::vector<FrameInfo>{}; |
| 614 | } |
| 615 | |
| 616 | std::vector<FrameInfo> result(frameCount); |
| 617 | for (int i = 0; i < frameCount; ++i) { |
| 618 | SkAssertResult(this->onGetFrameInfo(i, &result[i])); |
| 619 | } |
| 620 | return result; |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 621 | } |