blob: 59978c6fdf6416310999d90d37aaa887bcc99a38 [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"
Matt Sarettcf3f2342017-03-23 15:32:25 -040012#include "SkColorSpaceXform_Base.h"
msarett1a464672016-01-07 13:17:19 -080013#include "SkData.h"
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -040014#include "SkFrameHolder.h"
msarett1a464672016-01-07 13:17:19 -080015#include "SkGifCodec.h"
msarettf7eb6fc2016-09-13 09:04:11 -070016#include "SkHalf.h"
Leon Scroggins III04be2b52017-08-17 15:13:20 -040017#ifdef SK_HAS_HEIF_LIBRARY
18#include "SkHeifCodec.h"
19#endif
msarett1a464672016-01-07 13:17:19 -080020#include "SkIcoCodec.h"
msarette16b04a2015-04-15 07:32:19 -070021#include "SkJpegCodec.h"
msarettad3a5c62016-05-06 07:21:26 -070022#ifdef SK_HAS_PNG_LIBRARY
msarettbe1d5552016-01-21 09:05:23 -080023#include "SkPngCodec.h"
yujieqin916de9f2016-01-25 08:26:16 -080024#endif
msarett39b2d5a2016-02-17 08:26:31 -080025#include "SkRawCodec.h"
scroggof24f2242015-03-03 08:59:20 -080026#include "SkStream.h"
msarett1a464672016-01-07 13:17:19 -080027#include "SkWbmpCodec.h"
scroggo6f5e6192015-06-18 12:53:43 -070028#include "SkWebpCodec.h"
scroggof24f2242015-03-03 08:59:20 -080029
msarett74114382015-03-16 11:55:18 -070030struct DecoderProc {
scroggodb30be22015-12-08 18:54:13 -080031 bool (*IsFormat)(const void*, size_t);
Mike Reedede7bac2017-07-23 15:30:02 -040032 std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<SkStream>, SkCodec::Result*);
msarett74114382015-03-16 11:55:18 -070033};
34
Leon Scroggins III862c1962017-10-02 16:28:49 -040035static constexpr DecoderProc gDecoderProcs[] = {
msarettad3a5c62016-05-06 07:21:26 -070036#ifdef SK_HAS_JPEG_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040037 { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080038#endif
msarettad3a5c62016-05-06 07:21:26 -070039#ifdef SK_HAS_WEBP_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040040 { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080041#endif
Mike Reedede7bac2017-07-23 15:30:02 -040042 { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
msarettad3a5c62016-05-06 07:21:26 -070043#ifdef SK_HAS_PNG_LIBRARY
Mike Reedede7bac2017-07-23 15:30:02 -040044 { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
msarett39b2d5a2016-02-17 08:26:31 -080045#endif
Mike Reedede7bac2017-07-23 15:30:02 -040046 { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
Leon Scroggins III04be2b52017-08-17 15:13:20 -040047 { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream },
48#ifdef SK_HAS_HEIF_LIBRARY
49 { SkHeifCodec::IsHeif, SkHeifCodec::MakeFromStream },
50#endif
msarett74114382015-03-16 11:55:18 -070051};
52
Mike Reedede7bac2017-07-23 15:30:02 -040053std::unique_ptr<SkCodec> SkCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
54 Result* outResult, SkPngChunkReader* chunkReader) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040055 Result resultStorage;
56 if (!outResult) {
57 outResult = &resultStorage;
58 }
59
scroggof24f2242015-03-03 08:59:20 -080060 if (!stream) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040061 *outResult = kInvalidInput;
halcanary96fcdcc2015-08-27 07:41:13 -070062 return nullptr;
scroggof24f2242015-03-03 08:59:20 -080063 }
scroggo0a7e69c2015-04-03 07:22:22 -070064
Leon Scroggins III04be2b52017-08-17 15:13:20 -040065 constexpr size_t bytesToRead = MinBufferedBytesNeeded();
scroggodb30be22015-12-08 18:54:13 -080066
67 char buffer[bytesToRead];
68 size_t bytesRead = stream->peek(buffer, bytesToRead);
69
70 // It is also possible to have a complete image less than bytesToRead bytes
71 // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
72 // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
73 // than bytesToRead, so pass that directly to the decoder.
74 // It also is possible the stream uses too small a buffer for peeking, but
75 // we trust the caller to use a large enough buffer.
76
77 if (0 == bytesRead) {
scroggo3ab9f2e2016-01-06 09:53:34 -080078 // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
79 // printf could be useful to notice failures.
80 // SkCodecPrintf("Encoded image data failed to peek!\n");
81
scroggodb30be22015-12-08 18:54:13 -080082 // It is possible the stream does not support peeking, but does support
83 // rewinding.
84 // Attempt to read() and pass the actual amount read to the decoder.
85 bytesRead = stream->read(buffer, bytesToRead);
86 if (!stream->rewind()) {
scroggo3ab9f2e2016-01-06 09:53:34 -080087 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
Leon Scroggins III588fb042017-07-14 16:32:31 -040088 *outResult = kCouldNotRewind;
scroggodb30be22015-12-08 18:54:13 -080089 return nullptr;
90 }
91 }
92
scroggocf98fa92015-11-23 08:14:40 -080093 // PNG is special, since we want to be able to supply an SkPngChunkReader.
94 // But this code follows the same pattern as the loop.
msarettad3a5c62016-05-06 07:21:26 -070095#ifdef SK_HAS_PNG_LIBRARY
scroggodb30be22015-12-08 18:54:13 -080096 if (SkPngCodec::IsPng(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -040097 return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
msarett39b2d5a2016-02-17 08:26:31 -080098 } else
99#endif
100 {
scroggocf98fa92015-11-23 08:14:40 -0800101 for (DecoderProc proc : gDecoderProcs) {
scroggodb30be22015-12-08 18:54:13 -0800102 if (proc.IsFormat(buffer, bytesRead)) {
Mike Reedede7bac2017-07-23 15:30:02 -0400103 return proc.MakeFromStream(std::move(stream), outResult);
scroggocf98fa92015-11-23 08:14:40 -0800104 }
msarett74114382015-03-16 11:55:18 -0700105 }
yujieqin916de9f2016-01-25 08:26:16 -0800106
107#ifdef SK_CODEC_DECODES_RAW
108 // Try to treat the input as RAW if all the other checks failed.
Mike Reedede7bac2017-07-23 15:30:02 -0400109 return SkRawCodec::MakeFromStream(std::move(stream), outResult);
yujieqin916de9f2016-01-25 08:26:16 -0800110#endif
scroggof24f2242015-03-03 08:59:20 -0800111 }
msarett8c8f22a2015-04-01 06:58:48 -0700112
Leon Scroggins III588fb042017-07-14 16:32:31 -0400113 if (bytesRead < bytesToRead) {
114 *outResult = kIncompleteInput;
115 } else {
116 *outResult = kUnimplemented;
117 }
118
msarettf44631b2016-01-13 10:54:20 -0800119 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800120}
121
Mike Reedede7bac2017-07-23 15:30:02 -0400122std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
scroggof24f2242015-03-03 08:59:20 -0800123 if (!data) {
halcanary96fcdcc2015-08-27 07:41:13 -0700124 return nullptr;
scroggof24f2242015-03-03 08:59:20 -0800125 }
Mike Reed847068c2017-07-26 11:35:53 -0400126 return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
scroggof24f2242015-03-03 08:59:20 -0800127}
128
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400129SkCodec::SkCodec(int width, int height, const SkEncodedInfo& info,
Mike Reedede7bac2017-07-23 15:30:02 -0400130 XformFormat srcFormat, std::unique_ptr<SkStream> stream,
Leon Scroggins IIIb6ab10f2017-10-18 14:42:43 -0400131 sk_sp<SkColorSpace> colorSpace, SkEncodedOrigin origin)
msarettc30c4182016-04-20 11:53:35 -0700132 : fEncodedInfo(info)
msarett530c8442016-07-21 11:57:49 -0700133 , fSrcInfo(info.makeImageInfo(width, height, std::move(colorSpace)))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400134 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400135 , fStream(std::move(stream))
scroggof24f2242015-03-03 08:59:20 -0800136 , fNeedsRewind(false)
msarett0e6274f2016-03-21 08:04:40 -0700137 , fOrigin(origin)
scroggo46c57472015-09-30 08:57:13 -0700138 , fDstInfo()
139 , fOptions()
140 , fCurrScanline(-1)
Ivan Afanasyev26315582017-10-09 10:09:53 +0700141 , fStartedIncrementalDecode(false)
scroggof24f2242015-03-03 08:59:20 -0800142{}
143
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400144SkCodec::SkCodec(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
Leon Scroggins IIIb6ab10f2017-10-18 14:42:43 -0400145 XformFormat srcFormat, std::unique_ptr<SkStream> stream,
146 SkEncodedOrigin origin)
msarett549ca322016-08-17 08:54:08 -0700147 : fEncodedInfo(info)
148 , fSrcInfo(imageInfo)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400149 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400150 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700151 , fNeedsRewind(false)
152 , fOrigin(origin)
153 , fDstInfo()
154 , fOptions()
155 , fCurrScanline(-1)
Ivan Afanasyev26315582017-10-09 10:09:53 +0700156 , fStartedIncrementalDecode(false)
msarett549ca322016-08-17 08:54:08 -0700157{}
158
scroggo9b2cdbf42015-07-10 12:07:02 -0700159SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700160
Leon Scroggins III07418182017-08-15 12:24:02 -0400161bool SkCodec::conversionSupported(const SkImageInfo& dst, SkEncodedInfo::Color srcColor,
162 bool srcIsOpaque, const SkColorSpace* srcCS) const {
163 if (!valid_alpha(dst.alphaType(), srcIsOpaque)) {
164 return false;
165 }
166
167 switch (dst.colorType()) {
168 case kRGBA_8888_SkColorType:
169 case kBGRA_8888_SkColorType:
170 return true;
171 case kRGBA_F16_SkColorType:
172 return dst.colorSpace() && dst.colorSpace()->gammaIsLinear();
173 case kRGB_565_SkColorType:
174 return srcIsOpaque;
175 case kGray_8_SkColorType:
176 return SkEncodedInfo::kGray_Color == srcColor && srcIsOpaque &&
177 !needs_color_xform(dst, srcCS, false);
178 default:
179 return false;
180 }
181
182}
183
scroggob427db12015-08-12 07:24:13 -0700184bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800185 // Store the value of fNeedsRewind so we can update it. Next read will
186 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700187 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800188 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700189 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700190 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700191 }
scroggob427db12015-08-12 07:24:13 -0700192
scroggo46c57472015-09-30 08:57:13 -0700193 // startScanlineDecode will need to be called before decoding scanlines.
194 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700195 // startIncrementalDecode will need to be called before incrementalDecode.
196 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700197
scroggo19b91532016-10-24 09:03:26 -0700198 // Some codecs do not have a stream. They may hold onto their own data or another codec.
199 // They must handle rewinding themselves.
200 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700201 return false;
202 }
203
204 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800205}
scroggo05245902015-03-25 11:11:52 -0700206
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400207static void zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
208 SkIRect frameRect) {
209 if (!frameRect.intersect(dstInfo.bounds())) {
210 return;
211 }
212 const auto info = dstInfo.makeWH(frameRect.width(), frameRect.height());
213 const size_t bpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
214 const size_t offset = frameRect.x() * bpp + frameRect.y() * rowBytes;
215 auto* eraseDst = SkTAddOffset<void>(pixels, offset);
216 SkSampler::Fill(info, eraseDst, rowBytes, 0, SkCodec::kNo_ZeroInitialized);
217}
218
219SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
220 const Options& options) {
221 const int index = options.fFrameIndex;
222 if (0 == index) {
Leon Scroggins III07418182017-08-15 12:24:02 -0400223 if (!this->conversionSupported(info, fEncodedInfo.color(), fEncodedInfo.opaque(),
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400224 fSrcInfo.colorSpace())
225 || !this->initializeColorXform(info, fEncodedInfo.alpha(), options.fPremulBehavior))
226 {
Leon Scroggins III07418182017-08-15 12:24:02 -0400227 return kInvalidConversion;
228 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400229 return kSuccess;
230 }
231
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400232 if (index < 0) {
233 return kInvalidParameters;
234 }
235
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400236 if (options.fSubset || info.dimensions() != fSrcInfo.dimensions()) {
237 // If we add support for these, we need to update the code that zeroes
238 // a kRestoreBGColor frame.
239 return kInvalidParameters;
240 }
241
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400242 if (index >= this->onGetFrameCount()) {
243 return kIncompleteInput;
244 }
245
246 const auto* frameHolder = this->getFrameHolder();
247 SkASSERT(frameHolder);
248
249 const auto* frame = frameHolder->getFrame(index);
250 SkASSERT(frame);
251
Leon Scroggins III07418182017-08-15 12:24:02 -0400252 if (!this->conversionSupported(info, fEncodedInfo.color(), !frame->hasAlpha(),
253 fSrcInfo.colorSpace())) {
254 return kInvalidConversion;
255 }
256
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400257 const int requiredFrame = frame->getRequiredFrame();
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400258 if (requiredFrame != kNone) {
259 if (options.fPriorFrame != kNone) {
260 // Check for a valid frame as a starting point. Alternatively, we could
261 // treat an invalid frame as not providing one, but rejecting it will
262 // make it easier to catch the mistake.
263 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400264 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400265 }
266 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
267 switch (prevFrame->getDisposalMethod()) {
268 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
269 return kInvalidParameters;
270 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
271 // If a frame after the required frame is provided, there is no
272 // need to clear, since it must be covered by the desired frame.
273 if (options.fPriorFrame == requiredFrame) {
274 zero_rect(info, pixels, rowBytes, prevFrame->frameRect());
275 }
276 break;
277 default:
278 break;
279 }
280 } else {
281 Options prevFrameOptions(options);
282 prevFrameOptions.fFrameIndex = requiredFrame;
283 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
284 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
285 if (result != kSuccess) {
286 return result;
287 }
288 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
289 const auto disposalMethod = prevFrame->getDisposalMethod();
290 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
291 zero_rect(info, pixels, rowBytes, prevFrame->frameRect());
292 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400293 }
294 }
295
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400296 FrameInfo frameInfo;
297 SkAssertResult(this->getFrameInfo(index, &frameInfo));
298 return this->initializeColorXform(info, frameInfo.fAlpha, options.fPremulBehavior)
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
msarettf7eb6fc2016-09-13 09:04:11 -0700550uint64_t SkCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
551 switch (dstInfo.colorType()) {
552 case kRGBA_F16_SkColorType: {
553 static constexpr uint64_t transparentColor = 0;
554 static constexpr uint64_t opaqueColor = ((uint64_t) SK_Half1) << 48;
555 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ? opaqueColor : transparentColor;
556 }
557 default: {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000558 // This not only handles the kN32 case, but also k565, kGray8, since
msarettf7eb6fc2016-09-13 09:04:11 -0700559 // the low bits are zeros.
560 return (kOpaque_SkAlphaType == fSrcInfo.alphaType()) ?
561 SK_ColorBLACK : SK_ColorTRANSPARENT;
562 }
563 }
564}
565
msarette6dd0042015-10-09 11:07:34 -0700566static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
msarettf7eb6fc2016-09-13 09:04:11 -0700567 uint64_t colorOrIndex, SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
msarette6dd0042015-10-09 11:07:34 -0700568 if (sampler) {
569 sampler->fill(info, dst, rowBytes, colorOrIndex, zeroInit);
570 } else {
571 SkSampler::Fill(info, dst, rowBytes, colorOrIndex, zeroInit);
572 }
573}
574
575void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
576 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
577
578 void* fillDst;
msarettf7eb6fc2016-09-13 09:04:11 -0700579 const uint64_t fillValue = this->getFillValue(info);
msarette6dd0042015-10-09 11:07:34 -0700580 const int linesRemaining = linesRequested - linesDecoded;
581 SkSampler* sampler = this->getSampler(false);
582
msarett91c22b22016-02-22 12:27:46 -0800583 int fillWidth = info.width();
584 if (fOptions.fSubset) {
585 fillWidth = fOptions.fSubset->width();
586 }
587
msarette6dd0042015-10-09 11:07:34 -0700588 switch (this->getScanlineOrder()) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700589 case kTopDown_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800590 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700591 fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes);
592 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
593 break;
594 }
595 case kBottomUp_SkScanlineOrder: {
596 fillDst = dst;
msarett91c22b22016-02-22 12:27:46 -0800597 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700598 fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler);
599 break;
600 }
msarette6dd0042015-10-09 11:07:34 -0700601 }
scroggo46c57472015-09-30 08:57:13 -0700602}
Matt Sarett313c4632016-10-20 12:35:23 -0400603
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400604static inline SkColorSpaceXform::ColorFormat select_xform_format_ct(SkColorType colorType) {
605 switch (colorType) {
606 case kRGBA_8888_SkColorType:
607 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
608 case kBGRA_8888_SkColorType:
609 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
610 case kRGB_565_SkColorType:
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400611#ifdef SK_PMCOLOR_IS_RGBA
612 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
613#else
614 return SkColorSpaceXform::kBGRA_8888_ColorFormat;
615#endif
616 default:
617 SkASSERT(false);
618 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
619 }
620}
621
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400622bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
Matt Sarettcf3f2342017-03-23 15:32:25 -0400623 SkTransferFunctionBehavior premulBehavior) {
Matt Sarett313c4632016-10-20 12:35:23 -0400624 fColorXform = nullptr;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400625 fXformOnDecode = false;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400626 if (!this->usesColorXform()) {
627 return true;
628 }
629 bool needsColorCorrectPremul = needs_premul(dstInfo.alphaType(), encodedAlpha) &&
Matt Sarettcf3f2342017-03-23 15:32:25 -0400630 SkTransferFunctionBehavior::kRespect == premulBehavior;
Leon Scroggins III07418182017-08-15 12:24:02 -0400631 if (needs_color_xform(dstInfo, fSrcInfo.colorSpace(), needsColorCorrectPremul)) {
Matt Sarettcf3f2342017-03-23 15:32:25 -0400632 fColorXform = SkColorSpaceXform_Base::New(fSrcInfo.colorSpace(), dstInfo.colorSpace(),
633 premulBehavior);
Matt Sarett313c4632016-10-20 12:35:23 -0400634 if (!fColorXform) {
635 return false;
636 }
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400637
638 // We will apply the color xform when reading the color table unless F16 is requested.
639 fXformOnDecode = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
640 || kRGBA_F16_SkColorType == dstInfo.colorType();
641 if (fXformOnDecode) {
642 fDstXformFormat = select_xform_format(dstInfo.colorType());
643 } else {
644 fDstXformFormat = select_xform_format_ct(dstInfo.colorType());
645 }
Matt Sarett313c4632016-10-20 12:35:23 -0400646 }
647
648 return true;
649}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400650
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400651void SkCodec::applyColorXform(void* dst, const void* src, int count, SkAlphaType at) const {
652 SkASSERT(fColorXform);
653 SkAssertResult(fColorXform->apply(fDstXformFormat, dst,
654 fSrcXformFormat, src,
655 count, at));
656}
657
658void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
659 auto alphaType = select_xform_alpha(fDstInfo.alphaType(), fSrcInfo.alphaType());
660 this->applyColorXform(dst, src, count, alphaType);
661}
662
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400663std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400664 const int frameCount = this->getFrameCount();
665 SkASSERT(frameCount >= 0);
666 if (frameCount <= 0) {
667 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400668 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400669
670 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
671 // Not animated.
672 return std::vector<FrameInfo>{};
673 }
674
675 std::vector<FrameInfo> result(frameCount);
676 for (int i = 0; i < frameCount; ++i) {
677 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
678 }
679 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400680}