blob: ecf253f49ea0cfaa5948771cdd32a241ea810606 [file] [log] [blame]
scroggof24f2242015-03-03 08:59:20 -08001/*
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
msarettb46e5e22015-07-30 11:36:40 -07008#include "SkBmpCodec.h"
scroggof24f2242015-03-03 08:59:20 -08009#include "SkCodec.h"
msarett8c8f22a2015-04-01 06:58:48 -070010#include "SkCodecPriv.h"
msarettad8bcfe2016-03-07 07:09:03 -080011#include "SkColorSpace.h"
msarett1a464672016-01-07 13:17:19 -080012#include "SkData.h"
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -040013#include "SkFrameHolder.h"
msarett1a464672016-01-07 13:17:19 -080014#include "SkGifCodec.h"
msarettf7eb6fc2016-09-13 09:04:11 -070015#include "SkHalf.h"
Leon Scroggins III04be2b52017-08-17 15:13:20 -040016#ifdef SK_HAS_HEIF_LIBRARY
17#include "SkHeifCodec.h"
18#endif
msarett1a464672016-01-07 13:17:19 -080019#include "SkIcoCodec.h"
msarette16b04a2015-04-15 07:32:19 -070020#include "SkJpegCodec.h"
msarettad3a5c62016-05-06 07:21:26 -070021#ifdef SK_HAS_PNG_LIBRARY
msarettbe1d5552016-01-21 09:05:23 -080022#include "SkPngCodec.h"
yujieqin916de9f2016-01-25 08:26:16 -080023#endif
msarett39b2d5a2016-02-17 08:26:31 -080024#include "SkRawCodec.h"
scroggof24f2242015-03-03 08:59:20 -080025#include "SkStream.h"
msarett1a464672016-01-07 13:17:19 -080026#include "SkWbmpCodec.h"
scroggo6f5e6192015-06-18 12:53:43 -070027#include "SkWebpCodec.h"
scroggof24f2242015-03-03 08:59:20 -080028
msarett74114382015-03-16 11:55:18 -070029struct DecoderProc {
scroggodb30be22015-12-08 18:54:13 -080030 bool (*IsFormat)(const void*, size_t);
Mike Reedede7bac2017-07-23 15:30:02 -040031 std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<SkStream>, SkCodec::Result*);
msarett74114382015-03-16 11:55:18 -070032};
33
Leon Scroggins III862c1962017-10-02 16:28:49 -040034static constexpr DecoderProc gDecoderProcs[] = {
msarettad3a5c62016-05-06 07:21:26 -070035#ifdef SK_HAS_JPEG_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040036 { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080037#endif
msarettad3a5c62016-05-06 07:21:26 -070038#ifdef SK_HAS_WEBP_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040039 { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080040#endif
Mike Reedede7bac2017-07-23 15:30:02 -040041 { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
msarettad3a5c62016-05-06 07:21:26 -070042#ifdef SK_HAS_PNG_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040043 { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080044#endif
Mike Reedede7bac2017-07-23 15:30:02 -040045 { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
Leon Scroggins III04be2b52017-08-17 15:13:20 -040046 { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream },
47#ifdef SK_HAS_HEIF_LIBRARY
48 { SkHeifCodec::IsHeif, SkHeifCodec::MakeFromStream },
49#endif
msarett74114382015-03-16 11:55:18 -070050};
51
Mike Reedede7bac2017-07-23 15:30:02 -040052std::unique_ptr<SkCodec> SkCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
53 Result* outResult, SkPngChunkReader* chunkReader) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040054 Result resultStorage;
55 if (!outResult) {
56 outResult = &resultStorage;
57 }
58
scroggof24f2242015-03-03 08:59:20 -080059 if (!stream) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040060 *outResult = kInvalidInput;
halcanary96fcdcc2015-08-27 07:41:13 -070061 return nullptr;
scroggof24f2242015-03-03 08:59:20 -080062 }
scroggo0a7e69c2015-04-03 07:22:22 -070063
Leon Scroggins III04be2b52017-08-17 15:13:20 -040064 constexpr size_t bytesToRead = MinBufferedBytesNeeded();
scroggodb30be22015-12-08 18:54:13 -080065
66 char buffer[bytesToRead];
67 size_t bytesRead = stream->peek(buffer, bytesToRead);
68
69 // It is also possible to have a complete image less than bytesToRead bytes
70 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
71 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
72 // than bytesToRead, so pass that directly to the decoder.
73 // It also is possible the stream uses too small a buffer for peeking, but
74 // we trust the caller to use a large enough buffer.
75
76 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -080077 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
78 // printf could be useful to notice failures.
79 // SkCodecPrintf("Encoded image data failed to peek!\n");
80
scroggodb30be22015-12-08 18:54:13 -080081 // It is possible the stream does not support peeking, but does support
82 // rewinding.
83 // Attempt to read() and pass the actual amount read to the decoder.
84 bytesRead = stream->read(buffer, bytesToRead);
85 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -080086 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040087 *outResult = kCouldNotRewind;
scroggodb30be22015-12-08 18:54:13 -080088 return nullptr;
89 }
90 }
91
scroggocf98fa92015-11-23 08:14:40 -080092 // PNG is special, since we want to be able to supply an SkPngChunkReader.
93 // But this code follows the same pattern as the loop.
msarettad3a5c62016-05-06 07:21:26 -070094#ifdef SK_HAS_PNG_LIBRARY
scroggodb30be22015-12-08 18:54:13 -080095 if (SkPngCodec::IsPng(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -040096 return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -080097 } else
98#endif
99 {
scroggocf98fa92015-11-23 08:14:40 -0800100 for (DecoderProc proc : gDecoderProcs) {
scroggodb30be22015-12-08 18:54:13 -0800101 if (proc.IsFormat(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400102 return proc.MakeFromStream(std::move(stream), outResult);
scroggocf98fa92015-11-23 08:14:40 -0800103 }
msarett74114382015-03-16 11:55:18 -0700104 }
yujieqin916de9f2016-01-25 08:26:16 -0800105
106#ifdef SK_CODEC_DECODES_RAW
107 // Try to treat the input as RAW if all the other checks failed.
Mike Reedede7bac2017-07-23 15:30:02 -0400108 return SkRawCodec::MakeFromStream(std::move(stream), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800109#endif
scroggof24f2242015-03-03 08:59:20 -0800110 }
msarett8c8f22a2015-04-01 06:58:48 -0700111
Leon Scroggins III588fb042017-07-14 16:32:31 -0400112 if (bytesRead < bytesToRead) {
113 *outResult = kIncompleteInput;
114 } else {
115 *outResult = kUnimplemented;
116 }
117
msarettf44631b2016-01-13 10:54:20 -0800118 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800119}
120
Mike Reedede7bac2017-07-23 15:30:02 -0400121std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800122 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700123 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800124 }
Mike Reed847068c2017-07-26 11:35:53 -0400125 return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800126}
127
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400128SkCodec::SkCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream,
129 SkEncodedOrigin origin)
130 : fEncodedInfo(std::move(info))
131 , fSrcInfo(fEncodedInfo.makeImageInfo())
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400132 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400133 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700134 , fNeedsRewind(false)
135 , fOrigin(origin)
136 , fDstInfo()
137 , fOptions()
138 , fCurrScanline(-1)
Ivan Afanasyev26315582017-10-09 10:09:53 +0700139 , fStartedIncrementalDecode(false)
msarett549ca322016-08-17 08:54:08 -0700140{}
141
scroggo9b2cdbf42015-07-10 12:07:02 -0700142SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700143
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500144bool SkCodec::conversionSupported(const SkImageInfo& dst, SkColorType srcColor,
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400145 bool srcIsOpaque, bool needsColorXform) {
Leon Scroggins III07418182017-08-15 12:24:02 -0400146 if (!valid_alpha(dst.alphaType(), srcIsOpaque)) {
147 return false;
148 }
149
150 switch (dst.colorType()) {
151 case kRGBA_8888_SkColorType:
152 case kBGRA_8888_SkColorType:
153 return true;
154 case kRGBA_F16_SkColorType:
Mike Kleince4cf722018-05-10 11:29:15 -0400155 return dst.colorSpace();
Leon Scroggins III07418182017-08-15 12:24:02 -0400156 case kRGB_565_SkColorType:
157 return srcIsOpaque;
158 case kGray_8_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400159 return kGray_8_SkColorType == srcColor && srcIsOpaque;
Mike Reedd6cb11e2017-11-30 15:33:04 -0500160 case kAlpha_8_SkColorType:
161 // conceptually we can convert anything into alpha_8, but we haven't actually coded
162 // all of those other conversions yet, so only return true for the case we have codec.
163 return fSrcInfo.colorType() == kAlpha_8_SkColorType;;
Leon Scroggins III07418182017-08-15 12:24:02 -0400164 default:
165 return false;
166 }
Leon Scroggins III07418182017-08-15 12:24:02 -0400167}
168
scroggob427db12015-08-12 07:24:13 -0700169bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800170 // Store the value of fNeedsRewind so we can update it. Next read will
171 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700172 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800173 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700174 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700175 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700176 }
scroggob427db12015-08-12 07:24:13 -0700177
scroggo46c57472015-09-30 08:57:13 -0700178 // startScanlineDecode will need to be called before decoding scanlines.
179 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700180 // startIncrementalDecode will need to be called before incrementalDecode.
181 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700182
scroggo19b91532016-10-24 09:03:26 -0700183 // Some codecs do not have a stream. They may hold onto their own data or another codec.
184 // They must handle rewinding themselves.
185 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700186 return false;
187 }
188
189 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800190}
scroggo05245902015-03-25 11:11:52 -0700191
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400192bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
193 SkISize srcDimensions, SkIRect prevRect) {
194 const auto dimensions = dstInfo.dimensions();
195 if (dimensions != srcDimensions) {
196 SkRect src = SkRect::Make(srcDimensions);
197 SkRect dst = SkRect::Make(dimensions);
198 SkMatrix map = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit);
199 SkRect asRect = SkRect::Make(prevRect);
200 if (!map.mapRect(&asRect)) {
201 return false;
202 }
203 asRect.roundIn(&prevRect);
204 if (prevRect.isEmpty()) {
205 // Down-scaling shrank the empty portion to nothing,
206 // so nothing to zero.
207 return true;
208 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400209 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400210
211 if (!prevRect.intersect(dstInfo.bounds())) {
212 SkCodecPrintf("rectangles do not intersect!");
213 SkASSERT(false);
214 return true;
215 }
216
217 const SkImageInfo info = dstInfo.makeWH(prevRect.width(), prevRect.height());
Mike Reed7fcfb622018-02-09 13:26:46 -0500218 const size_t bpp = dstInfo.bytesPerPixel();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400219 const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes;
220 void* eraseDst = SkTAddOffset<void>(pixels, offset);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400221 SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400222 return true;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400223}
224
225SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
226 const Options& options) {
227 const int index = options.fFrameIndex;
228 if (0 == index) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400229 return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
230 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400231 }
232
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400233 if (index < 0) {
234 return kInvalidParameters;
235 }
236
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500237 if (options.fSubset) {
238 // If we add support for this, we need to update the code that zeroes
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400239 // a kRestoreBGColor frame.
240 return kInvalidParameters;
241 }
242
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400243 if (index >= this->onGetFrameCount()) {
244 return kIncompleteInput;
245 }
246
247 const auto* frameHolder = this->getFrameHolder();
248 SkASSERT(frameHolder);
249
250 const auto* frame = frameHolder->getFrame(index);
251 SkASSERT(frame);
252
253 const int requiredFrame = frame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000254 if (requiredFrame != kNoFrame) {
255 if (options.fPriorFrame != kNoFrame) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400256 // Check for a valid frame as a starting point. Alternatively, we could
257 // treat an invalid frame as not providing one, but rejecting it will
258 // make it easier to catch the mistake.
259 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400260 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400261 }
262 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
263 switch (prevFrame->getDisposalMethod()) {
264 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
265 return kInvalidParameters;
266 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
267 // If a frame after the required frame is provided, there is no
268 // need to clear, since it must be covered by the desired frame.
269 if (options.fPriorFrame == requiredFrame) {
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500270 SkIRect prevRect = prevFrame->frameRect();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400271 if (!zero_rect(info, pixels, rowBytes, fSrcInfo.dimensions(), prevRect)) {
272 return kInternalError;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500273 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400274 }
275 break;
276 default:
277 break;
278 }
279 } else {
280 Options prevFrameOptions(options);
281 prevFrameOptions.fFrameIndex = requiredFrame;
282 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
283 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
284 if (result != kSuccess) {
285 return result;
286 }
287 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
288 const auto disposalMethod = prevFrame->getDisposalMethod();
289 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400290 auto prevRect = prevFrame->frameRect();
291 if (!zero_rect(info, pixels, rowBytes, fSrcInfo.dimensions(), prevRect)) {
292 return kInternalError;
293 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400294 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400295 }
296 }
297
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400298 return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
299 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400300}
scroggo8e6c7ad2016-09-16 08:20:38 -0700301
scroggoeb602a52015-07-09 08:16:03 -0700302SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000303 const Options* options) {
scroggoeb602a52015-07-09 08:16:03 -0700304 if (kUnknown_SkColorType == info.colorType()) {
305 return kInvalidConversion;
306 }
halcanary96fcdcc2015-08-27 07:41:13 -0700307 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700308 return kInvalidParameters;
309 }
310 if (rowBytes < info.minRowBytes()) {
311 return kInvalidParameters;
312 }
313
scroggo3a7701c2015-09-30 09:15:14 -0700314 if (!this->rewindIfNeeded()) {
315 return kCouldNotRewind;
316 }
317
scroggoeb602a52015-07-09 08:16:03 -0700318 // Default options.
319 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700320 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700321 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400322 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400323 if (options->fSubset) {
324 SkIRect subset(*options->fSubset);
325 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
326 // FIXME: How to differentiate between not supporting subset at all
327 // and not supporting this particular subset?
328 return kUnimplemented;
329 }
scroggoe7fc14b2015-10-02 13:14:46 -0700330 }
scroggoeb602a52015-07-09 08:16:03 -0700331 }
scroggoe7fc14b2015-10-02 13:14:46 -0700332
Leon Scroggins III07418182017-08-15 12:24:02 -0400333 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
334 *options);
335 if (frameIndexResult != kSuccess) {
336 return frameIndexResult;
337 }
338
scroggoe7fc14b2015-10-02 13:14:46 -0700339 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
340 // because it supports arbitrary scaling/subset combinations.
341 if (!this->dimensionsSupported(info.dimensions())) {
342 return kInvalidScale;
343 }
344
scroggo8e6c7ad2016-09-16 08:20:38 -0700345 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500346 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700347
msarette6dd0042015-10-09 11:07:34 -0700348 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
349 // successfully.
350 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000351 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700352
353 // A return value of kIncompleteInput indicates a truncated image stream.
354 // In this case, we will fill any uninitialized memory with a default value.
355 // Some subclasses will take care of filling any uninitialized memory on
356 // their own. They indicate that all of the memory has been filled by
357 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400358 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500359 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
360 // there is a subset. In that case, it will use the width of the subset. From here, the
361 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
362 // differenty from the other codecs, and it needs to use the width specified by the info.
363 // Set the subset to null so SkWebpCodec uses the correct width.
364 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700365 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
366 rowsDecoded);
367 }
368
scroggoeb602a52015-07-09 08:16:03 -0700369 return result;
370}
371
scroggo8e6c7ad2016-09-16 08:20:38 -0700372SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000373 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700374 fStartedIncrementalDecode = false;
375
376 if (kUnknown_SkColorType == info.colorType()) {
377 return kInvalidConversion;
378 }
379 if (nullptr == pixels) {
380 return kInvalidParameters;
381 }
382
scroggo8e6c7ad2016-09-16 08:20:38 -0700383 // FIXME: If the rows come after the rows of a previous incremental decode,
384 // we might be able to skip the rewind, but only the implementation knows
385 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
386 // a bottom-up BMP could skip rewinding if the new rows are above the old
387 // rows.)
388 if (!this->rewindIfNeeded()) {
389 return kCouldNotRewind;
390 }
391
392 // Set options.
393 Options optsStorage;
394 if (nullptr == options) {
395 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400396 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400397 if (options->fSubset) {
398 SkIRect size = SkIRect::MakeSize(info.dimensions());
399 if (!size.contains(*options->fSubset)) {
400 return kInvalidParameters;
401 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700402
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400403 const int top = options->fSubset->top();
404 const int bottom = options->fSubset->bottom();
405 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
406 return kInvalidParameters;
407 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700408 }
409 }
410
Leon Scroggins III07418182017-08-15 12:24:02 -0400411 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
412 *options);
413 if (frameIndexResult != kSuccess) {
414 return frameIndexResult;
415 }
416
scroggo8e6c7ad2016-09-16 08:20:38 -0700417 if (!this->dimensionsSupported(info.dimensions())) {
418 return kInvalidScale;
419 }
420
421 fDstInfo = info;
422 fOptions = *options;
423
Leon Scroggins571b30f2017-07-11 17:35:31 +0000424 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700425 if (kSuccess == result) {
426 fStartedIncrementalDecode = true;
427 } else if (kUnimplemented == result) {
428 // FIXME: This is temporarily necessary, until we transition SkCodec
429 // implementations from scanline decoding to incremental decoding.
430 // SkAndroidCodec will first attempt to use incremental decoding, but
431 // will fall back to scanline decoding if incremental returns
432 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
433 // (after potentially rewinding), but we do not want the next call to
434 // startScanlineDecode() to do a rewind.
435 fNeedsRewind = false;
436 }
437 return result;
438}
439
440
441SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000442 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700443 // Reset fCurrScanline in case of failure.
444 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700445
scroggo3a7701c2015-09-30 09:15:14 -0700446 if (!this->rewindIfNeeded()) {
447 return kCouldNotRewind;
448 }
449
scroggo46c57472015-09-30 08:57:13 -0700450 // Set options.
451 Options optsStorage;
452 if (nullptr == options) {
453 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700454 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700455 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700456 if (!size.contains(*options->fSubset)) {
457 return kInvalidInput;
458 }
459
460 // We only support subsetting in the x-dimension for scanline decoder.
461 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700462 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700463 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700464 }
465 }
466
Leon Scroggins III07418182017-08-15 12:24:02 -0400467 // Scanline decoding only supports decoding the first frame.
468 if (options->fFrameIndex != 0) {
469 return kUnimplemented;
470 }
471
472 // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
473 // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
474 const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
475 if (frameIndexResult != kSuccess) {
476 return frameIndexResult;
477 }
478
scroggoe7fc14b2015-10-02 13:14:46 -0700479 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700480 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700481 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700482 }
483
Leon Scroggins571b30f2017-07-11 17:35:31 +0000484 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700485 if (result != SkCodec::kSuccess) {
486 return result;
487 }
488
489 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700490 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700491 fOptions = *options;
492 return kSuccess;
493}
494
msarette6dd0042015-10-09 11:07:34 -0700495int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700496 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700497 return 0;
scroggo46c57472015-09-30 08:57:13 -0700498 }
499
500 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700501 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700502 return 0;
scroggo46c57472015-09-30 08:57:13 -0700503 }
504
msarette6dd0042015-10-09 11:07:34 -0700505 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
506 if (linesDecoded < countLines) {
507 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
508 countLines, linesDecoded);
509 }
510 fCurrScanline += countLines;
511 return linesDecoded;
512}
513
514bool SkCodec::skipScanlines(int countLines) {
515 if (fCurrScanline < 0) {
516 return false;
517 }
518
519 SkASSERT(!fDstInfo.isEmpty());
520 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
521 // Arguably, we could just skip the scanlines which are remaining,
522 // and return true. We choose to return false so the client
523 // can catch their bug.
524 return false;
525 }
526
527 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700528 fCurrScanline += countLines;
529 return result;
530}
531
msarette6dd0042015-10-09 11:07:34 -0700532int SkCodec::outputScanline(int inputScanline) const {
533 SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height());
534 return this->onOutputScanline(inputScanline);
535}
scroggo46c57472015-09-30 08:57:13 -0700536
msarette6dd0042015-10-09 11:07:34 -0700537int SkCodec::onOutputScanline(int inputScanline) const {
538 switch (this->getScanlineOrder()) {
539 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700540 return inputScanline;
541 case kBottomUp_SkScanlineOrder:
542 return this->getInfo().height() - inputScanline - 1;
543 default:
544 // This case indicates an interlaced gif and is implemented by SkGifCodec.
545 SkASSERT(false);
546 return 0;
scroggo46c57472015-09-30 08:57:13 -0700547 }
msarette6dd0042015-10-09 11:07:34 -0700548}
scroggo46c57472015-09-30 08:57:13 -0700549
msarette6dd0042015-10-09 11:07:34 -0700550static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400551 SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
msarette6dd0042015-10-09 11:07:34 -0700552 if (sampler) {
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400553 sampler->fill(info, dst, rowBytes, zeroInit);
msarette6dd0042015-10-09 11:07:34 -0700554 } else {
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400555 SkSampler::Fill(info, dst, rowBytes, zeroInit);
msarette6dd0042015-10-09 11:07:34 -0700556 }
557}
558
559void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
560 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
561
562 void* fillDst;
msarette6dd0042015-10-09 11:07:34 -0700563 const int linesRemaining = linesRequested - linesDecoded;
564 SkSampler* sampler = this->getSampler(false);
565
msarett91c22b22016-02-22 12:27:46 -0800566 int fillWidth = info.width();
567 if (fOptions.fSubset) {
568 fillWidth = fOptions.fSubset->width();
569 }
570
msarette6dd0042015-10-09 11:07:34 -0700571 switch (this->getScanlineOrder()) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700572 case kTopDown_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800573 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700574 fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400575 fill_proc(fillInfo, fillDst, rowBytes, zeroInit, sampler);
msarette6dd0042015-10-09 11:07:34 -0700576 break;
577 }
578 case kBottomUp_SkScanlineOrder: {
579 fillDst = dst;
msarett91c22b22016-02-22 12:27:46 -0800580 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400581 fill_proc(fillInfo, fillDst, rowBytes, zeroInit, sampler);
msarette6dd0042015-10-09 11:07:34 -0700582 break;
583 }
msarette6dd0042015-10-09 11:07:34 -0700584 }
scroggo46c57472015-09-30 08:57:13 -0700585}
Matt Sarett313c4632016-10-20 12:35:23 -0400586
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400587static inline bool select_xform_format(SkColorType colorType, bool forColorTable,
588 skcms_PixelFormat* outFormat) {
589 SkASSERT(outFormat);
590
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400591 switch (colorType) {
592 case kRGBA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400593 *outFormat = skcms_PixelFormat_RGBA_8888;
594 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400595 case kBGRA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400596 *outFormat = skcms_PixelFormat_BGRA_8888;
597 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400598 case kRGB_565_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400599 if (forColorTable) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400600#ifdef SK_PMCOLOR_IS_RGBA
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400601 *outFormat = skcms_PixelFormat_RGBA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400602#else
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400603 *outFormat = skcms_PixelFormat_BGRA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400604#endif
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400605 break;
606 }
607 *outFormat = skcms_PixelFormat_BGR_565;
608 break;
609 case kRGBA_F16_SkColorType:
610 *outFormat = skcms_PixelFormat_RGBA_hhhh;
611 break;
Brian Osmanb3f38302018-09-07 15:24:44 -0400612 case kGray_8_SkColorType:
613 *outFormat = skcms_PixelFormat_G_8;
614 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400615 default:
Leon Scroggins83988ed2018-08-24 21:41:24 +0000616 return false;
Leon Scroggins83988ed2018-08-24 21:41:24 +0000617 }
Matt Sarett313c4632016-10-20 12:35:23 -0400618 return true;
619}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400620
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400621bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
622 bool srcIsOpaque) {
623 fXformTime = kNo_XformTime;
624 bool needsColorXform = false;
625 if (this->usesColorXform() && dstInfo.colorSpace()) {
626 dstInfo.colorSpace()->toProfile(&fDstProfile);
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400627 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400628 needsColorXform = true;
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400629 } else {
630 const auto* srcProfile = fEncodedInfo.profile();
631 if (!srcProfile) {
632 srcProfile = skcms_sRGB_profile();
633 }
634 if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) {
635 needsColorXform = true;
636 }
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400637 }
638 }
639
640 if (!this->conversionSupported(dstInfo, fSrcInfo.colorType(), srcIsOpaque, needsColorXform)) {
641 return false;
642 }
643
644 if (needsColorXform) {
645 fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
646 || kRGBA_F16_SkColorType == dstInfo.colorType()
647 ? kDecodeRow_XformTime : kPalette_XformTime;
648 if (!select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
649 &fDstXformFormat)) {
650 return false;
651 }
652 if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
653 && dstInfo.alphaType() == kPremul_SkAlphaType) {
654 fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
655 } else {
656 fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
657 }
658 }
659 return true;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400660}
661
662void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400663 // It is okay for srcProfile to be null. This will use sRGB.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400664 const auto* srcProfile = fEncodedInfo.profile();
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400665 SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
666 dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
667 count));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400668}
669
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400670std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400671 const int frameCount = this->getFrameCount();
672 SkASSERT(frameCount >= 0);
673 if (frameCount <= 0) {
674 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400675 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400676
677 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
678 // Not animated.
679 return std::vector<FrameInfo>{};
680 }
681
682 std::vector<FrameInfo> result(frameCount);
683 for (int i = 0; i < frameCount; ++i) {
684 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
685 }
686 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400687}
Leon Scroggins IIIfe3da022018-01-16 11:56:54 -0500688
689const char* SkCodec::ResultToString(Result result) {
690 switch (result) {
691 case kSuccess:
692 return "success";
693 case kIncompleteInput:
694 return "incomplete input";
695 case kErrorInInput:
696 return "error in input";
697 case kInvalidConversion:
698 return "invalid conversion";
699 case kInvalidScale:
700 return "invalid scale";
701 case kInvalidParameters:
702 return "invalid parameters";
703 case kInvalidInput:
704 return "invalid input";
705 case kCouldNotRewind:
706 return "could not rewind";
707 case kInternalError:
708 return "internal error";
709 case kUnimplemented:
710 return "unimplemented";
711 default:
712 SkASSERT(false);
713 return "bogus result value";
714 }
715}
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000716
717static SkIRect frame_rect_on_screen(SkIRect frameRect,
718 const SkIRect& screenRect) {
719 if (!frameRect.intersect(screenRect)) {
720 return SkIRect::MakeEmpty();
721 }
722
723 return frameRect;
724}
725
726static bool independent(const SkFrame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000727 return frame.getRequiredFrame() == SkCodec::kNoFrame;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000728}
729
730static bool restore_bg(const SkFrame& frame) {
731 return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
732}
733
734void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
735 const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
736 const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
737 const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
738
739 const int i = frame->frameId();
740 if (0 == i) {
741 frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
Nigel Tao66bc5242018-08-22 10:56:03 +1000742 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000743 return;
744 }
745
746
747 const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kPriorFrame;
748 if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
749 frame->setHasAlpha(reportsAlpha);
Nigel Tao66bc5242018-08-22 10:56:03 +1000750 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000751 return;
752 }
753
754 const SkFrame* prevFrame = this->getFrame(i-1);
755 while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
756 const int prevId = prevFrame->frameId();
757 if (0 == prevId) {
758 frame->setHasAlpha(true);
Nigel Tao66bc5242018-08-22 10:56:03 +1000759 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000760 return;
761 }
762
763 prevFrame = this->getFrame(prevId - 1);
764 }
765
766 const bool clearPrevFrame = restore_bg(*prevFrame);
767 auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
768
769 if (clearPrevFrame) {
770 if (prevFrameRect == screenRect || independent(*prevFrame)) {
771 frame->setHasAlpha(true);
Nigel Tao66bc5242018-08-22 10:56:03 +1000772 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000773 return;
774 }
775 }
776
777 if (reportsAlpha && blendWithPrevFrame) {
778 // Note: We could be more aggressive here. If prevFrame clears
779 // to background color and covers its required frame (and that
780 // frame is independent), prevFrame could be marked independent.
781 // Would this extra complexity be worth it?
782 frame->setRequiredFrame(prevFrame->frameId());
783 frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
784 return;
785 }
786
787 while (frameRect.contains(prevFrameRect)) {
788 const int prevRequiredFrame = prevFrame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000789 if (prevRequiredFrame == SkCodec::kNoFrame) {
790 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000791 frame->setHasAlpha(true);
792 return;
793 }
794
795 prevFrame = this->getFrame(prevRequiredFrame);
796 prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
797 }
798
799 if (restore_bg(*prevFrame)) {
800 frame->setHasAlpha(true);
801 if (prevFrameRect == screenRect || independent(*prevFrame)) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000802 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000803 } else {
804 // Note: As above, frame could still be independent, e.g. if
805 // prevFrame covers its required frame and that frame is
806 // independent.
807 frame->setRequiredFrame(prevFrame->frameId());
808 }
809 return;
810 }
811
812 SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
813 frame->setRequiredFrame(prevFrame->frameId());
814 frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
815}
816