blob: d12646a44b934f8adae7c8afb762f8429e5fa54c [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 III49894f42018-08-24 11:59:04 -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 III49894f42018-08-24 11:59:04 -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 III49894f42018-08-24 11:59:04 -0400159 SkASSERT(!needsColorXform);
160 return kGray_8_SkColorType == srcColor && srcIsOpaque;
Mike Reedd6cb11e2017-11-30 15:33:04 -0500161 case kAlpha_8_SkColorType:
162 // conceptually we can convert anything into alpha_8, but we haven't actually coded
163 // all of those other conversions yet, so only return true for the case we have codec.
164 return fSrcInfo.colorType() == kAlpha_8_SkColorType;;
Leon Scroggins III07418182017-08-15 12:24:02 -0400165 default:
166 return false;
167 }
Leon Scroggins III07418182017-08-15 12:24:02 -0400168}
169
scroggob427db12015-08-12 07:24:13 -0700170bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800171 // Store the value of fNeedsRewind so we can update it. Next read will
172 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700173 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800174 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700175 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700176 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700177 }
scroggob427db12015-08-12 07:24:13 -0700178
scroggo46c57472015-09-30 08:57:13 -0700179 // startScanlineDecode will need to be called before decoding scanlines.
180 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700181 // startIncrementalDecode will need to be called before incrementalDecode.
182 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700183
scroggo19b91532016-10-24 09:03:26 -0700184 // Some codecs do not have a stream. They may hold onto their own data or another codec.
185 // They must handle rewinding themselves.
186 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700187 return false;
188 }
189
190 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800191}
scroggo05245902015-03-25 11:11:52 -0700192
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400193bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
194 SkISize srcDimensions, SkIRect prevRect) {
195 const auto dimensions = dstInfo.dimensions();
196 if (dimensions != srcDimensions) {
197 SkRect src = SkRect::Make(srcDimensions);
198 SkRect dst = SkRect::Make(dimensions);
199 SkMatrix map = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit);
200 SkRect asRect = SkRect::Make(prevRect);
201 if (!map.mapRect(&asRect)) {
202 return false;
203 }
204 asRect.roundIn(&prevRect);
205 if (prevRect.isEmpty()) {
206 // Down-scaling shrank the empty portion to nothing,
207 // so nothing to zero.
208 return true;
209 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400210 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400211
212 if (!prevRect.intersect(dstInfo.bounds())) {
213 SkCodecPrintf("rectangles do not intersect!");
214 SkASSERT(false);
215 return true;
216 }
217
218 const SkImageInfo info = dstInfo.makeWH(prevRect.width(), prevRect.height());
Mike Reed7fcfb622018-02-09 13:26:46 -0500219 const size_t bpp = dstInfo.bytesPerPixel();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400220 const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes;
221 void* eraseDst = SkTAddOffset<void>(pixels, offset);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400222 SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400223 return true;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400224}
225
226SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
227 const Options& options) {
228 const int index = options.fFrameIndex;
229 if (0 == index) {
Leon Scroggins III49894f42018-08-24 11:59:04 -0400230 return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
231 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400232 }
233
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400234 if (index < 0) {
235 return kInvalidParameters;
236 }
237
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500238 if (options.fSubset) {
239 // If we add support for this, we need to update the code that zeroes
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400240 // a kRestoreBGColor frame.
241 return kInvalidParameters;
242 }
243
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400244 if (index >= this->onGetFrameCount()) {
245 return kIncompleteInput;
246 }
247
248 const auto* frameHolder = this->getFrameHolder();
249 SkASSERT(frameHolder);
250
251 const auto* frame = frameHolder->getFrame(index);
252 SkASSERT(frame);
253
254 const int requiredFrame = frame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000255 if (requiredFrame != kNoFrame) {
256 if (options.fPriorFrame != kNoFrame) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400257 // Check for a valid frame as a starting point. Alternatively, we could
258 // treat an invalid frame as not providing one, but rejecting it will
259 // make it easier to catch the mistake.
260 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400261 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400262 }
263 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
264 switch (prevFrame->getDisposalMethod()) {
265 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
266 return kInvalidParameters;
267 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
268 // If a frame after the required frame is provided, there is no
269 // need to clear, since it must be covered by the desired frame.
270 if (options.fPriorFrame == requiredFrame) {
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500271 SkIRect prevRect = prevFrame->frameRect();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400272 if (!zero_rect(info, pixels, rowBytes, fSrcInfo.dimensions(), prevRect)) {
273 return kInternalError;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500274 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400275 }
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) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400291 auto prevRect = prevFrame->frameRect();
292 if (!zero_rect(info, pixels, rowBytes, fSrcInfo.dimensions(), prevRect)) {
293 return kInternalError;
294 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400295 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400296 }
297 }
298
Leon Scroggins III49894f42018-08-24 11:59:04 -0400299 return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
300 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400301}
scroggo8e6c7ad2016-09-16 08:20:38 -0700302
scroggoeb602a52015-07-09 08:16:03 -0700303SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000304 const Options* options) {
scroggoeb602a52015-07-09 08:16:03 -0700305 if (kUnknown_SkColorType == info.colorType()) {
306 return kInvalidConversion;
307 }
halcanary96fcdcc2015-08-27 07:41:13 -0700308 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700309 return kInvalidParameters;
310 }
311 if (rowBytes < info.minRowBytes()) {
312 return kInvalidParameters;
313 }
314
scroggo3a7701c2015-09-30 09:15:14 -0700315 if (!this->rewindIfNeeded()) {
316 return kCouldNotRewind;
317 }
318
scroggoeb602a52015-07-09 08:16:03 -0700319 // Default options.
320 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700321 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700322 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400323 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400324 if (options->fSubset) {
325 SkIRect subset(*options->fSubset);
326 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
327 // FIXME: How to differentiate between not supporting subset at all
328 // and not supporting this particular subset?
329 return kUnimplemented;
330 }
scroggoe7fc14b2015-10-02 13:14:46 -0700331 }
scroggoeb602a52015-07-09 08:16:03 -0700332 }
scroggoe7fc14b2015-10-02 13:14:46 -0700333
Leon Scroggins III07418182017-08-15 12:24:02 -0400334 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
335 *options);
336 if (frameIndexResult != kSuccess) {
337 return frameIndexResult;
338 }
339
scroggoe7fc14b2015-10-02 13:14:46 -0700340 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
341 // because it supports arbitrary scaling/subset combinations.
342 if (!this->dimensionsSupported(info.dimensions())) {
343 return kInvalidScale;
344 }
345
scroggo8e6c7ad2016-09-16 08:20:38 -0700346 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500347 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700348
msarette6dd0042015-10-09 11:07:34 -0700349 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
350 // successfully.
351 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000352 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700353
354 // A return value of kIncompleteInput indicates a truncated image stream.
355 // In this case, we will fill any uninitialized memory with a default value.
356 // Some subclasses will take care of filling any uninitialized memory on
357 // their own. They indicate that all of the memory has been filled by
358 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400359 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500360 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
361 // there is a subset. In that case, it will use the width of the subset. From here, the
362 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
363 // differenty from the other codecs, and it needs to use the width specified by the info.
364 // Set the subset to null so SkWebpCodec uses the correct width.
365 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700366 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
367 rowsDecoded);
368 }
369
scroggoeb602a52015-07-09 08:16:03 -0700370 return result;
371}
372
scroggo8e6c7ad2016-09-16 08:20:38 -0700373SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000374 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700375 fStartedIncrementalDecode = false;
376
377 if (kUnknown_SkColorType == info.colorType()) {
378 return kInvalidConversion;
379 }
380 if (nullptr == pixels) {
381 return kInvalidParameters;
382 }
383
scroggo8e6c7ad2016-09-16 08:20:38 -0700384 // FIXME: If the rows come after the rows of a previous incremental decode,
385 // we might be able to skip the rewind, but only the implementation knows
386 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
387 // a bottom-up BMP could skip rewinding if the new rows are above the old
388 // rows.)
389 if (!this->rewindIfNeeded()) {
390 return kCouldNotRewind;
391 }
392
393 // Set options.
394 Options optsStorage;
395 if (nullptr == options) {
396 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400397 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400398 if (options->fSubset) {
399 SkIRect size = SkIRect::MakeSize(info.dimensions());
400 if (!size.contains(*options->fSubset)) {
401 return kInvalidParameters;
402 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700403
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400404 const int top = options->fSubset->top();
405 const int bottom = options->fSubset->bottom();
406 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
407 return kInvalidParameters;
408 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700409 }
410 }
411
Leon Scroggins III07418182017-08-15 12:24:02 -0400412 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
413 *options);
414 if (frameIndexResult != kSuccess) {
415 return frameIndexResult;
416 }
417
scroggo8e6c7ad2016-09-16 08:20:38 -0700418 if (!this->dimensionsSupported(info.dimensions())) {
419 return kInvalidScale;
420 }
421
422 fDstInfo = info;
423 fOptions = *options;
424
Leon Scroggins571b30f2017-07-11 17:35:31 +0000425 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700426 if (kSuccess == result) {
427 fStartedIncrementalDecode = true;
428 } else if (kUnimplemented == result) {
429 // FIXME: This is temporarily necessary, until we transition SkCodec
430 // implementations from scanline decoding to incremental decoding.
431 // SkAndroidCodec will first attempt to use incremental decoding, but
432 // will fall back to scanline decoding if incremental returns
433 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
434 // (after potentially rewinding), but we do not want the next call to
435 // startScanlineDecode() to do a rewind.
436 fNeedsRewind = false;
437 }
438 return result;
439}
440
441
442SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000443 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700444 // Reset fCurrScanline in case of failure.
445 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700446
scroggo3a7701c2015-09-30 09:15:14 -0700447 if (!this->rewindIfNeeded()) {
448 return kCouldNotRewind;
449 }
450
scroggo46c57472015-09-30 08:57:13 -0700451 // Set options.
452 Options optsStorage;
453 if (nullptr == options) {
454 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700455 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700456 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700457 if (!size.contains(*options->fSubset)) {
458 return kInvalidInput;
459 }
460
461 // We only support subsetting in the x-dimension for scanline decoder.
462 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700463 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700464 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700465 }
466 }
467
Leon Scroggins III07418182017-08-15 12:24:02 -0400468 // Scanline decoding only supports decoding the first frame.
469 if (options->fFrameIndex != 0) {
470 return kUnimplemented;
471 }
472
473 // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
474 // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
475 const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
476 if (frameIndexResult != kSuccess) {
477 return frameIndexResult;
478 }
479
scroggoe7fc14b2015-10-02 13:14:46 -0700480 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700481 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700482 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700483 }
484
Leon Scroggins571b30f2017-07-11 17:35:31 +0000485 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700486 if (result != SkCodec::kSuccess) {
487 return result;
488 }
489
490 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700491 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700492 fOptions = *options;
493 return kSuccess;
494}
495
msarette6dd0042015-10-09 11:07:34 -0700496int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700497 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700498 return 0;
scroggo46c57472015-09-30 08:57:13 -0700499 }
500
501 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700502 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700503 return 0;
scroggo46c57472015-09-30 08:57:13 -0700504 }
505
msarette6dd0042015-10-09 11:07:34 -0700506 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
507 if (linesDecoded < countLines) {
508 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
509 countLines, linesDecoded);
510 }
511 fCurrScanline += countLines;
512 return linesDecoded;
513}
514
515bool SkCodec::skipScanlines(int countLines) {
516 if (fCurrScanline < 0) {
517 return false;
518 }
519
520 SkASSERT(!fDstInfo.isEmpty());
521 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
522 // Arguably, we could just skip the scanlines which are remaining,
523 // and return true. We choose to return false so the client
524 // can catch their bug.
525 return false;
526 }
527
528 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700529 fCurrScanline += countLines;
530 return result;
531}
532
msarette6dd0042015-10-09 11:07:34 -0700533int SkCodec::outputScanline(int inputScanline) const {
534 SkASSERT(0 <= inputScanline && inputScanline < this->getInfo().height());
535 return this->onOutputScanline(inputScanline);
536}
scroggo46c57472015-09-30 08:57:13 -0700537
msarette6dd0042015-10-09 11:07:34 -0700538int SkCodec::onOutputScanline(int inputScanline) const {
539 switch (this->getScanlineOrder()) {
540 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700541 return inputScanline;
542 case kBottomUp_SkScanlineOrder:
543 return this->getInfo().height() - inputScanline - 1;
544 default:
545 // This case indicates an interlaced gif and is implemented by SkGifCodec.
546 SkASSERT(false);
547 return 0;
scroggo46c57472015-09-30 08:57:13 -0700548 }
msarette6dd0042015-10-09 11:07:34 -0700549}
scroggo46c57472015-09-30 08:57:13 -0700550
msarette6dd0042015-10-09 11:07:34 -0700551static void fill_proc(const SkImageInfo& info, void* dst, size_t rowBytes,
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400552 SkCodec::ZeroInitialized zeroInit, SkSampler* sampler) {
msarette6dd0042015-10-09 11:07:34 -0700553 if (sampler) {
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400554 sampler->fill(info, dst, rowBytes, zeroInit);
msarette6dd0042015-10-09 11:07:34 -0700555 } else {
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400556 SkSampler::Fill(info, dst, rowBytes, zeroInit);
msarette6dd0042015-10-09 11:07:34 -0700557 }
558}
559
560void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
561 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
562
563 void* fillDst;
msarette6dd0042015-10-09 11:07:34 -0700564 const int linesRemaining = linesRequested - linesDecoded;
565 SkSampler* sampler = this->getSampler(false);
566
msarett91c22b22016-02-22 12:27:46 -0800567 int fillWidth = info.width();
568 if (fOptions.fSubset) {
569 fillWidth = fOptions.fSubset->width();
570 }
571
msarette6dd0042015-10-09 11:07:34 -0700572 switch (this->getScanlineOrder()) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700573 case kTopDown_SkScanlineOrder: {
msarett91c22b22016-02-22 12:27:46 -0800574 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
msarette6dd0042015-10-09 11:07:34 -0700575 fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400576 fill_proc(fillInfo, fillDst, rowBytes, zeroInit, sampler);
msarette6dd0042015-10-09 11:07:34 -0700577 break;
578 }
579 case kBottomUp_SkScanlineOrder: {
580 fillDst = dst;
msarett91c22b22016-02-22 12:27:46 -0800581 const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400582 fill_proc(fillInfo, fillDst, rowBytes, zeroInit, sampler);
msarette6dd0042015-10-09 11:07:34 -0700583 break;
584 }
msarette6dd0042015-10-09 11:07:34 -0700585 }
scroggo46c57472015-09-30 08:57:13 -0700586}
Matt Sarett313c4632016-10-20 12:35:23 -0400587
Leon Scroggins III49894f42018-08-24 11:59:04 -0400588static inline bool select_xform_format(SkColorType colorType, bool forColorTable,
589 skcms_PixelFormat* outFormat) {
590 SkASSERT(outFormat);
591
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400592 switch (colorType) {
593 case kRGBA_8888_SkColorType:
Leon Scroggins III49894f42018-08-24 11:59:04 -0400594 *outFormat = skcms_PixelFormat_RGBA_8888;
595 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400596 case kBGRA_8888_SkColorType:
Leon Scroggins III49894f42018-08-24 11:59:04 -0400597 *outFormat = skcms_PixelFormat_BGRA_8888;
598 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400599 case kRGB_565_SkColorType:
Leon Scroggins III49894f42018-08-24 11:59:04 -0400600 if (forColorTable) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400601#ifdef SK_PMCOLOR_IS_RGBA
Leon Scroggins III49894f42018-08-24 11:59:04 -0400602 *outFormat = skcms_PixelFormat_RGBA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400603#else
Leon Scroggins III49894f42018-08-24 11:59:04 -0400604 *outFormat = skcms_PixelFormat_BGRA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400605#endif
Leon Scroggins III49894f42018-08-24 11:59:04 -0400606 break;
607 }
608 *outFormat = skcms_PixelFormat_BGR_565;
609 break;
610 case kRGBA_F16_SkColorType:
611 *outFormat = skcms_PixelFormat_RGBA_hhhh;
612 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400613 default:
Leon Scroggins III33d53942018-08-22 16:46:32 -0400614 return false;
Leon Scroggins III33d53942018-08-22 16:46:32 -0400615 }
Matt Sarett313c4632016-10-20 12:35:23 -0400616 return true;
617}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400618
Leon Scroggins III49894f42018-08-24 11:59:04 -0400619bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
620 bool srcIsOpaque) {
621 fXformTime = kNo_XformTime;
622 bool needsColorXform = false;
623 if (this->usesColorXform() && dstInfo.colorSpace()) {
624 dstInfo.colorSpace()->toProfile(&fDstProfile);
625 if (kRGBA_F16_SkColorType == dstInfo.colorType()
626 || !skcms_ApproximatelyEqualProfiles(fEncodedInfo.profile(), &fDstProfile) ) {
627 needsColorXform = true;
628 if (kGray_8_SkColorType == dstInfo.colorType()) {
629 return false;
630 }
631 }
632 }
633
634 if (!this->conversionSupported(dstInfo, fSrcInfo.colorType(), srcIsOpaque, needsColorXform)) {
635 return false;
636 }
637
638 if (needsColorXform) {
639 fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
640 || kRGBA_F16_SkColorType == dstInfo.colorType()
641 ? kDecodeRow_XformTime : kPalette_XformTime;
642 if (!select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
643 &fDstXformFormat)) {
644 return false;
645 }
646 if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
647 && dstInfo.alphaType() == kPremul_SkAlphaType) {
648 fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
649 } else {
650 fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
651 }
652 }
653 return true;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400654}
655
656void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
Leon Scroggins III49894f42018-08-24 11:59:04 -0400657 const auto* srcProfile = fEncodedInfo.profile();
658 SkASSERT(srcProfile);
659 SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
660 dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
661 count));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400662}
663
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400664std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400665 const int frameCount = this->getFrameCount();
666 SkASSERT(frameCount >= 0);
667 if (frameCount <= 0) {
668 return std::vector<FrameInfo>{};
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400669 }
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400670
671 if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
672 // Not animated.
673 return std::vector<FrameInfo>{};
674 }
675
676 std::vector<FrameInfo> result(frameCount);
677 for (int i = 0; i < frameCount; ++i) {
678 SkAssertResult(this->onGetFrameInfo(i, &result[i]));
679 }
680 return result;
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400681}
Leon Scroggins IIIfe3da022018-01-16 11:56:54 -0500682
683const char* SkCodec::ResultToString(Result result) {
684 switch (result) {
685 case kSuccess:
686 return "success";
687 case kIncompleteInput:
688 return "incomplete input";
689 case kErrorInInput:
690 return "error in input";
691 case kInvalidConversion:
692 return "invalid conversion";
693 case kInvalidScale:
694 return "invalid scale";
695 case kInvalidParameters:
696 return "invalid parameters";
697 case kInvalidInput:
698 return "invalid input";
699 case kCouldNotRewind:
700 return "could not rewind";
701 case kInternalError:
702 return "internal error";
703 case kUnimplemented:
704 return "unimplemented";
705 default:
706 SkASSERT(false);
707 return "bogus result value";
708 }
709}
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000710
711static SkIRect frame_rect_on_screen(SkIRect frameRect,
712 const SkIRect& screenRect) {
713 if (!frameRect.intersect(screenRect)) {
714 return SkIRect::MakeEmpty();
715 }
716
717 return frameRect;
718}
719
720static bool independent(const SkFrame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000721 return frame.getRequiredFrame() == SkCodec::kNoFrame;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000722}
723
724static bool restore_bg(const SkFrame& frame) {
725 return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
726}
727
728void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
729 const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
730 const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
731 const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
732
733 const int i = frame->frameId();
734 if (0 == i) {
735 frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
Nigel Tao66bc5242018-08-22 10:56:03 +1000736 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000737 return;
738 }
739
740
741 const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kPriorFrame;
742 if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
743 frame->setHasAlpha(reportsAlpha);
Nigel Tao66bc5242018-08-22 10:56:03 +1000744 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000745 return;
746 }
747
748 const SkFrame* prevFrame = this->getFrame(i-1);
749 while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
750 const int prevId = prevFrame->frameId();
751 if (0 == prevId) {
752 frame->setHasAlpha(true);
Nigel Tao66bc5242018-08-22 10:56:03 +1000753 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000754 return;
755 }
756
757 prevFrame = this->getFrame(prevId - 1);
758 }
759
760 const bool clearPrevFrame = restore_bg(*prevFrame);
761 auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
762
763 if (clearPrevFrame) {
764 if (prevFrameRect == screenRect || independent(*prevFrame)) {
765 frame->setHasAlpha(true);
Nigel Tao66bc5242018-08-22 10:56:03 +1000766 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000767 return;
768 }
769 }
770
771 if (reportsAlpha && blendWithPrevFrame) {
772 // Note: We could be more aggressive here. If prevFrame clears
773 // to background color and covers its required frame (and that
774 // frame is independent), prevFrame could be marked independent.
775 // Would this extra complexity be worth it?
776 frame->setRequiredFrame(prevFrame->frameId());
777 frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
778 return;
779 }
780
781 while (frameRect.contains(prevFrameRect)) {
782 const int prevRequiredFrame = prevFrame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000783 if (prevRequiredFrame == SkCodec::kNoFrame) {
784 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000785 frame->setHasAlpha(true);
786 return;
787 }
788
789 prevFrame = this->getFrame(prevRequiredFrame);
790 prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
791 }
792
793 if (restore_bg(*prevFrame)) {
794 frame->setHasAlpha(true);
795 if (prevFrameRect == screenRect || independent(*prevFrame)) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000796 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000797 } else {
798 // Note: As above, frame could still be independent, e.g. if
799 // prevFrame covers its required frame and that frame is
800 // independent.
801 frame->setRequiredFrame(prevFrame->frameId());
802 }
803 return;
804 }
805
806 SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
807 frame->setRequiredFrame(prevFrame->frameId());
808 frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
809}
810