blob: dfd1e0a5e89332ccca47aa924ec2f7affcc8d5e1 [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"
Brian Osman7d1c9ec2018-10-25 13:03:02 +000014#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))
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400131 , fSrcXformFormat(srcFormat)
Mike Reedede7bac2017-07-23 15:30:02 -0400132 , fStream(std::move(stream))
msarett549ca322016-08-17 08:54:08 -0700133 , fNeedsRewind(false)
134 , fOrigin(origin)
135 , fDstInfo()
136 , fOptions()
137 , fCurrScanline(-1)
Ivan Afanasyev26315582017-10-09 10:09:53 +0700138 , fStartedIncrementalDecode(false)
msarett549ca322016-08-17 08:54:08 -0700139{}
140
scroggo9b2cdbf42015-07-10 12:07:02 -0700141SkCodec::~SkCodec() {}
scroggoeb602a52015-07-09 08:16:03 -0700142
Leon Scroggins III712476e2018-10-03 15:47:00 -0400143bool SkCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, bool needsColorXform) {
Leon Scroggins III07418182017-08-15 12:24:02 -0400144 if (!valid_alpha(dst.alphaType(), srcIsOpaque)) {
145 return false;
146 }
147
148 switch (dst.colorType()) {
149 case kRGBA_8888_SkColorType:
150 case kBGRA_8888_SkColorType:
151 return true;
152 case kRGBA_F16_SkColorType:
Mike Kleince4cf722018-05-10 11:29:15 -0400153 return dst.colorSpace();
Leon Scroggins III07418182017-08-15 12:24:02 -0400154 case kRGB_565_SkColorType:
155 return srcIsOpaque;
156 case kGray_8_SkColorType:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400157 return SkEncodedInfo::kGray_Color == fEncodedInfo.color() && srcIsOpaque;
Mike Reedd6cb11e2017-11-30 15:33:04 -0500158 case kAlpha_8_SkColorType:
159 // conceptually we can convert anything into alpha_8, but we haven't actually coded
Leon Scroggins III712476e2018-10-03 15:47:00 -0400160 // all of those other conversions yet.
161 return SkEncodedInfo::kXAlpha_Color == fEncodedInfo.color();
Leon Scroggins III07418182017-08-15 12:24:02 -0400162 default:
163 return false;
164 }
Leon Scroggins III07418182017-08-15 12:24:02 -0400165}
166
scroggob427db12015-08-12 07:24:13 -0700167bool SkCodec::rewindIfNeeded() {
scroggof24f2242015-03-03 08:59:20 -0800168 // Store the value of fNeedsRewind so we can update it. Next read will
169 // require a rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700170 const bool needsRewind = fNeedsRewind;
scroggof24f2242015-03-03 08:59:20 -0800171 fNeedsRewind = true;
halcanarya096d7a2015-03-27 12:16:53 -0700172 if (!needsRewind) {
scroggob427db12015-08-12 07:24:13 -0700173 return true;
halcanarya096d7a2015-03-27 12:16:53 -0700174 }
scroggob427db12015-08-12 07:24:13 -0700175
scroggo46c57472015-09-30 08:57:13 -0700176 // startScanlineDecode will need to be called before decoding scanlines.
177 fCurrScanline = -1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700178 // startIncrementalDecode will need to be called before incrementalDecode.
179 fStartedIncrementalDecode = false;
scroggo46c57472015-09-30 08:57:13 -0700180
scroggo19b91532016-10-24 09:03:26 -0700181 // Some codecs do not have a stream. They may hold onto their own data or another codec.
182 // They must handle rewinding themselves.
183 if (fStream && !fStream->rewind()) {
scroggob427db12015-08-12 07:24:13 -0700184 return false;
185 }
186
187 return this->onRewind();
scroggof24f2242015-03-03 08:59:20 -0800188}
scroggo05245902015-03-25 11:11:52 -0700189
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400190bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
191 SkISize srcDimensions, SkIRect prevRect) {
192 const auto dimensions = dstInfo.dimensions();
193 if (dimensions != srcDimensions) {
194 SkRect src = SkRect::Make(srcDimensions);
195 SkRect dst = SkRect::Make(dimensions);
196 SkMatrix map = SkMatrix::MakeRectToRect(src, dst, SkMatrix::kCenter_ScaleToFit);
197 SkRect asRect = SkRect::Make(prevRect);
198 if (!map.mapRect(&asRect)) {
199 return false;
200 }
201 asRect.roundIn(&prevRect);
202 if (prevRect.isEmpty()) {
203 // Down-scaling shrank the empty portion to nothing,
204 // so nothing to zero.
205 return true;
206 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400207 }
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400208
209 if (!prevRect.intersect(dstInfo.bounds())) {
210 SkCodecPrintf("rectangles do not intersect!");
211 SkASSERT(false);
212 return true;
213 }
214
215 const SkImageInfo info = dstInfo.makeWH(prevRect.width(), prevRect.height());
Mike Reed7fcfb622018-02-09 13:26:46 -0500216 const size_t bpp = dstInfo.bytesPerPixel();
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400217 const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes;
218 void* eraseDst = SkTAddOffset<void>(pixels, offset);
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400219 SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized);
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400220 return true;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400221}
222
223SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
224 const Options& options) {
225 const int index = options.fFrameIndex;
226 if (0 == index) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400227 return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
228 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400229 }
230
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400231 if (index < 0) {
232 return kInvalidParameters;
233 }
234
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500235 if (options.fSubset) {
236 // If we add support for this, we need to update the code that zeroes
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400237 // a kRestoreBGColor frame.
238 return kInvalidParameters;
239 }
240
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400241 if (index >= this->onGetFrameCount()) {
242 return kIncompleteInput;
243 }
244
245 const auto* frameHolder = this->getFrameHolder();
246 SkASSERT(frameHolder);
247
248 const auto* frame = frameHolder->getFrame(index);
249 SkASSERT(frame);
250
251 const int requiredFrame = frame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000252 if (requiredFrame != kNoFrame) {
253 if (options.fPriorFrame != kNoFrame) {
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400254 // Check for a valid frame as a starting point. Alternatively, we could
255 // treat an invalid frame as not providing one, but rejecting it will
256 // make it easier to catch the mistake.
257 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400258 return kInvalidParameters;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400259 }
260 const auto* prevFrame = frameHolder->getFrame(options.fPriorFrame);
261 switch (prevFrame->getDisposalMethod()) {
262 case SkCodecAnimation::DisposalMethod::kRestorePrevious:
263 return kInvalidParameters;
264 case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
265 // If a frame after the required frame is provided, there is no
266 // need to clear, since it must be covered by the desired frame.
267 if (options.fPriorFrame == requiredFrame) {
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500268 SkIRect prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400269 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400270 return kInternalError;
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500271 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400272 }
273 break;
274 default:
275 break;
276 }
277 } else {
278 Options prevFrameOptions(options);
279 prevFrameOptions.fFrameIndex = requiredFrame;
280 prevFrameOptions.fZeroInitialized = kNo_ZeroInitialized;
281 const Result result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
282 if (result != kSuccess) {
283 return result;
284 }
285 const auto* prevFrame = frameHolder->getFrame(requiredFrame);
286 const auto disposalMethod = prevFrame->getDisposalMethod();
287 if (disposalMethod == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400288 auto prevRect = prevFrame->frameRect();
Leon Scroggins III712476e2018-10-03 15:47:00 -0400289 if (!zero_rect(info, pixels, rowBytes, this->dimensions(), prevRect)) {
Leon Scroggins III7916c0e2018-05-24 13:04:06 -0400290 return kInternalError;
291 }
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400292 }
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400293 }
294 }
295
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400296 return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
297 ? kSuccess : kInvalidConversion;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400298}
scroggo8e6c7ad2016-09-16 08:20:38 -0700299
Brian Osman84f8a612018-09-28 10:35:22 -0400300SkCodec::Result SkCodec::getPixels(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000301 const Options* options) {
Brian Osman84f8a612018-09-28 10:35:22 -0400302 SkImageInfo info = dstInfo;
303 if (!info.colorSpace()) {
304 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
305 }
306
scroggoeb602a52015-07-09 08:16:03 -0700307 if (kUnknown_SkColorType == info.colorType()) {
308 return kInvalidConversion;
309 }
halcanary96fcdcc2015-08-27 07:41:13 -0700310 if (nullptr == pixels) {
scroggoeb602a52015-07-09 08:16:03 -0700311 return kInvalidParameters;
312 }
313 if (rowBytes < info.minRowBytes()) {
314 return kInvalidParameters;
315 }
316
scroggo3a7701c2015-09-30 09:15:14 -0700317 if (!this->rewindIfNeeded()) {
318 return kCouldNotRewind;
319 }
320
scroggoeb602a52015-07-09 08:16:03 -0700321 // Default options.
322 Options optsStorage;
halcanary96fcdcc2015-08-27 07:41:13 -0700323 if (nullptr == options) {
scroggoeb602a52015-07-09 08:16:03 -0700324 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400325 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400326 if (options->fSubset) {
327 SkIRect subset(*options->fSubset);
328 if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
329 // FIXME: How to differentiate between not supporting subset at all
330 // and not supporting this particular subset?
331 return kUnimplemented;
332 }
scroggoe7fc14b2015-10-02 13:14:46 -0700333 }
scroggoeb602a52015-07-09 08:16:03 -0700334 }
scroggoe7fc14b2015-10-02 13:14:46 -0700335
Leon Scroggins III07418182017-08-15 12:24:02 -0400336 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
337 *options);
338 if (frameIndexResult != kSuccess) {
339 return frameIndexResult;
340 }
341
scroggoe7fc14b2015-10-02 13:14:46 -0700342 // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
343 // because it supports arbitrary scaling/subset combinations.
344 if (!this->dimensionsSupported(info.dimensions())) {
345 return kInvalidScale;
346 }
347
scroggo8e6c7ad2016-09-16 08:20:38 -0700348 fDstInfo = info;
Leon Scroggins III42886572017-01-27 13:16:28 -0500349 fOptions = *options;
scroggo8e6c7ad2016-09-16 08:20:38 -0700350
msarette6dd0042015-10-09 11:07:34 -0700351 // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
352 // successfully.
353 int rowsDecoded = 0;
Leon Scroggins571b30f2017-07-11 17:35:31 +0000354 const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700355
356 // A return value of kIncompleteInput indicates a truncated image stream.
357 // In this case, we will fill any uninitialized memory with a default value.
358 // Some subclasses will take care of filling any uninitialized memory on
359 // their own. They indicate that all of the memory has been filled by
360 // setting rowsDecoded equal to the height.
Leon Scroggins III674a1842017-07-06 12:26:09 -0400361 if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
Leon Scroggins III42886572017-01-27 13:16:28 -0500362 // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
363 // there is a subset. In that case, it will use the width of the subset. From here, the
364 // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
365 // differenty from the other codecs, and it needs to use the width specified by the info.
366 // Set the subset to null so SkWebpCodec uses the correct width.
367 fOptions.fSubset = nullptr;
msarette6dd0042015-10-09 11:07:34 -0700368 this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
369 rowsDecoded);
370 }
371
scroggoeb602a52015-07-09 08:16:03 -0700372 return result;
373}
374
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400375SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& dstInfo, void* pixels,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000376 size_t rowBytes, const SkCodec::Options* options) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700377 fStartedIncrementalDecode = false;
378
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400379 SkImageInfo info = dstInfo;
380 if (!info.colorSpace()) {
381 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
382 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700383 if (kUnknown_SkColorType == info.colorType()) {
384 return kInvalidConversion;
385 }
386 if (nullptr == pixels) {
387 return kInvalidParameters;
388 }
389
scroggo8e6c7ad2016-09-16 08:20:38 -0700390 // FIXME: If the rows come after the rows of a previous incremental decode,
391 // we might be able to skip the rewind, but only the implementation knows
392 // that. (e.g. PNG will always need to rewind, since we called longjmp, but
393 // a bottom-up BMP could skip rewinding if the new rows are above the old
394 // rows.)
395 if (!this->rewindIfNeeded()) {
396 return kCouldNotRewind;
397 }
398
399 // Set options.
400 Options optsStorage;
401 if (nullptr == options) {
402 options = &optsStorage;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400403 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400404 if (options->fSubset) {
405 SkIRect size = SkIRect::MakeSize(info.dimensions());
406 if (!size.contains(*options->fSubset)) {
407 return kInvalidParameters;
408 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700409
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400410 const int top = options->fSubset->top();
411 const int bottom = options->fSubset->bottom();
412 if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
413 return kInvalidParameters;
414 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700415 }
416 }
417
Leon Scroggins III07418182017-08-15 12:24:02 -0400418 const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
419 *options);
420 if (frameIndexResult != kSuccess) {
421 return frameIndexResult;
422 }
423
scroggo8e6c7ad2016-09-16 08:20:38 -0700424 if (!this->dimensionsSupported(info.dimensions())) {
425 return kInvalidScale;
426 }
427
428 fDstInfo = info;
429 fOptions = *options;
430
Leon Scroggins571b30f2017-07-11 17:35:31 +0000431 const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
scroggo8e6c7ad2016-09-16 08:20:38 -0700432 if (kSuccess == result) {
433 fStartedIncrementalDecode = true;
434 } else if (kUnimplemented == result) {
435 // FIXME: This is temporarily necessary, until we transition SkCodec
436 // implementations from scanline decoding to incremental decoding.
437 // SkAndroidCodec will first attempt to use incremental decoding, but
438 // will fall back to scanline decoding if incremental returns
439 // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
440 // (after potentially rewinding), but we do not want the next call to
441 // startScanlineDecode() to do a rewind.
442 fNeedsRewind = false;
443 }
444 return result;
445}
446
447
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400448SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000449 const SkCodec::Options* options) {
scroggo46c57472015-09-30 08:57:13 -0700450 // Reset fCurrScanline in case of failure.
451 fCurrScanline = -1;
scroggo46c57472015-09-30 08:57:13 -0700452
Leon Scroggins IIIfc38ba72018-10-03 16:19:10 -0400453 SkImageInfo info = dstInfo;
454 if (!info.colorSpace()) {
455 info = info.makeColorSpace(SkColorSpace::MakeSRGB());
456 }
457
scroggo3a7701c2015-09-30 09:15:14 -0700458 if (!this->rewindIfNeeded()) {
459 return kCouldNotRewind;
460 }
461
scroggo46c57472015-09-30 08:57:13 -0700462 // Set options.
463 Options optsStorage;
464 if (nullptr == options) {
465 options = &optsStorage;
scroggoe7fc14b2015-10-02 13:14:46 -0700466 } else if (options->fSubset) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700467 SkIRect size = SkIRect::MakeSize(info.dimensions());
msarettfdb47572015-10-13 12:50:14 -0700468 if (!size.contains(*options->fSubset)) {
469 return kInvalidInput;
470 }
471
472 // We only support subsetting in the x-dimension for scanline decoder.
473 // Subsetting in the y-dimension can be accomplished using skipScanlines().
scroggo8e6c7ad2016-09-16 08:20:38 -0700474 if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
msarettfdb47572015-10-13 12:50:14 -0700475 return kInvalidInput;
scroggoe7fc14b2015-10-02 13:14:46 -0700476 }
477 }
478
Leon Scroggins III07418182017-08-15 12:24:02 -0400479 // Scanline decoding only supports decoding the first frame.
480 if (options->fFrameIndex != 0) {
481 return kUnimplemented;
482 }
483
484 // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
485 // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
486 const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
487 if (frameIndexResult != kSuccess) {
488 return frameIndexResult;
489 }
490
scroggoe7fc14b2015-10-02 13:14:46 -0700491 // FIXME: Support subsets somehow?
scroggo8e6c7ad2016-09-16 08:20:38 -0700492 if (!this->dimensionsSupported(info.dimensions())) {
scroggoe7fc14b2015-10-02 13:14:46 -0700493 return kInvalidScale;
scroggo46c57472015-09-30 08:57:13 -0700494 }
495
Leon Scroggins571b30f2017-07-11 17:35:31 +0000496 const Result result = this->onStartScanlineDecode(info, *options);
scroggo46c57472015-09-30 08:57:13 -0700497 if (result != SkCodec::kSuccess) {
498 return result;
499 }
500
501 fCurrScanline = 0;
scroggo8e6c7ad2016-09-16 08:20:38 -0700502 fDstInfo = info;
scroggo46c57472015-09-30 08:57:13 -0700503 fOptions = *options;
504 return kSuccess;
505}
506
msarette6dd0042015-10-09 11:07:34 -0700507int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
scroggo46c57472015-09-30 08:57:13 -0700508 if (fCurrScanline < 0) {
msarette6dd0042015-10-09 11:07:34 -0700509 return 0;
scroggo46c57472015-09-30 08:57:13 -0700510 }
511
512 SkASSERT(!fDstInfo.isEmpty());
scroggoe7fc14b2015-10-02 13:14:46 -0700513 if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
msarette6dd0042015-10-09 11:07:34 -0700514 return 0;
scroggo46c57472015-09-30 08:57:13 -0700515 }
516
msarette6dd0042015-10-09 11:07:34 -0700517 const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
518 if (linesDecoded < countLines) {
519 this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
520 countLines, linesDecoded);
521 }
522 fCurrScanline += countLines;
523 return linesDecoded;
524}
525
526bool SkCodec::skipScanlines(int countLines) {
527 if (fCurrScanline < 0) {
528 return false;
529 }
530
531 SkASSERT(!fDstInfo.isEmpty());
532 if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
533 // Arguably, we could just skip the scanlines which are remaining,
534 // and return true. We choose to return false so the client
535 // can catch their bug.
536 return false;
537 }
538
539 bool result = this->onSkipScanlines(countLines);
scroggo46c57472015-09-30 08:57:13 -0700540 fCurrScanline += countLines;
541 return result;
542}
543
msarette6dd0042015-10-09 11:07:34 -0700544int SkCodec::outputScanline(int inputScanline) const {
Leon Scroggins III712476e2018-10-03 15:47:00 -0400545 SkASSERT(0 <= inputScanline && inputScanline < fEncodedInfo.height());
msarette6dd0042015-10-09 11:07:34 -0700546 return this->onOutputScanline(inputScanline);
547}
scroggo46c57472015-09-30 08:57:13 -0700548
msarette6dd0042015-10-09 11:07:34 -0700549int SkCodec::onOutputScanline(int inputScanline) const {
550 switch (this->getScanlineOrder()) {
551 case kTopDown_SkScanlineOrder:
msarette6dd0042015-10-09 11:07:34 -0700552 return inputScanline;
553 case kBottomUp_SkScanlineOrder:
Leon Scroggins III712476e2018-10-03 15:47:00 -0400554 return fEncodedInfo.height() - inputScanline - 1;
msarette6dd0042015-10-09 11:07:34 -0700555 default:
556 // This case indicates an interlaced gif and is implemented by SkGifCodec.
557 SkASSERT(false);
558 return 0;
scroggo46c57472015-09-30 08:57:13 -0700559 }
msarette6dd0042015-10-09 11:07:34 -0700560}
scroggo46c57472015-09-30 08:57:13 -0700561
msarette6dd0042015-10-09 11:07:34 -0700562void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
563 ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400564 if (kYes_ZeroInitialized == zeroInit) {
565 return;
566 }
msarette6dd0042015-10-09 11:07:34 -0700567
msarette6dd0042015-10-09 11:07:34 -0700568 const int linesRemaining = linesRequested - linesDecoded;
569 SkSampler* sampler = this->getSampler(false);
570
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -0400571 const int fillWidth = sampler ? sampler->fillWidth() :
572 fOptions.fSubset ? fOptions.fSubset->width() :
573 info.width() ;
574 void* fillDst = this->getScanlineOrder() == kBottomUp_SkScanlineOrder ? dst :
575 SkTAddOffset<void>(dst, linesDecoded * rowBytes);
576 const auto fillInfo = info.makeWH(fillWidth, linesRemaining);
577 SkSampler::Fill(fillInfo, fillDst, rowBytes, kNo_ZeroInitialized);
scroggo46c57472015-09-30 08:57:13 -0700578}
Matt Sarett313c4632016-10-20 12:35:23 -0400579
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400580static inline bool select_xform_format(SkColorType colorType, bool forColorTable,
581 skcms_PixelFormat* outFormat) {
582 SkASSERT(outFormat);
583
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400584 switch (colorType) {
585 case kRGBA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400586 *outFormat = skcms_PixelFormat_RGBA_8888;
587 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400588 case kBGRA_8888_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400589 *outFormat = skcms_PixelFormat_BGRA_8888;
590 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400591 case kRGB_565_SkColorType:
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400592 if (forColorTable) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400593#ifdef SK_PMCOLOR_IS_RGBA
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400594 *outFormat = skcms_PixelFormat_RGBA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400595#else
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400596 *outFormat = skcms_PixelFormat_BGRA_8888;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400597#endif
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400598 break;
599 }
600 *outFormat = skcms_PixelFormat_BGR_565;
601 break;
602 case kRGBA_F16_SkColorType:
603 *outFormat = skcms_PixelFormat_RGBA_hhhh;
604 break;
Brian Osmanb3f38302018-09-07 15:24:44 -0400605 case kGray_8_SkColorType:
606 *outFormat = skcms_PixelFormat_G_8;
607 break;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400608 default:
Leon Scroggins83988ed2018-08-24 21:41:24 +0000609 return false;
Leon Scroggins83988ed2018-08-24 21:41:24 +0000610 }
Matt Sarett313c4632016-10-20 12:35:23 -0400611 return true;
612}
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400613
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400614bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
615 bool srcIsOpaque) {
616 fXformTime = kNo_XformTime;
617 bool needsColorXform = false;
618 if (this->usesColorXform() && dstInfo.colorSpace()) {
619 dstInfo.colorSpace()->toProfile(&fDstProfile);
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400620 if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400621 needsColorXform = true;
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400622 } else {
623 const auto* srcProfile = fEncodedInfo.profile();
624 if (!srcProfile) {
625 srcProfile = skcms_sRGB_profile();
626 }
627 if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) {
628 needsColorXform = true;
629 }
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400630 }
631 }
632
Leon Scroggins III712476e2018-10-03 15:47:00 -0400633 if (!this->conversionSupported(dstInfo, srcIsOpaque, needsColorXform)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400634 return false;
635 }
636
637 if (needsColorXform) {
638 fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
639 || kRGBA_F16_SkColorType == dstInfo.colorType()
640 ? kDecodeRow_XformTime : kPalette_XformTime;
641 if (!select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
642 &fDstXformFormat)) {
643 return false;
644 }
645 if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
646 && dstInfo.alphaType() == kPremul_SkAlphaType) {
647 fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
648 } else {
649 fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
650 }
651 }
652 return true;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400653}
654
655void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
Leon Scroggins III5dd47e42018-09-27 15:26:48 -0400656 // It is okay for srcProfile to be null. This will use sRGB.
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400657 const auto* srcProfile = fEncodedInfo.profile();
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400658 SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
659 dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
660 count));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400661}
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}
Leon Scroggins IIIfe3da022018-01-16 11:56:54 -0500681
682const char* SkCodec::ResultToString(Result result) {
683 switch (result) {
684 case kSuccess:
685 return "success";
686 case kIncompleteInput:
687 return "incomplete input";
688 case kErrorInInput:
689 return "error in input";
690 case kInvalidConversion:
691 return "invalid conversion";
692 case kInvalidScale:
693 return "invalid scale";
694 case kInvalidParameters:
695 return "invalid parameters";
696 case kInvalidInput:
697 return "invalid input";
698 case kCouldNotRewind:
699 return "could not rewind";
700 case kInternalError:
701 return "internal error";
702 case kUnimplemented:
703 return "unimplemented";
704 default:
705 SkASSERT(false);
706 return "bogus result value";
707 }
708}
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000709
710static SkIRect frame_rect_on_screen(SkIRect frameRect,
711 const SkIRect& screenRect) {
712 if (!frameRect.intersect(screenRect)) {
713 return SkIRect::MakeEmpty();
714 }
715
716 return frameRect;
717}
718
719static bool independent(const SkFrame& frame) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000720 return frame.getRequiredFrame() == SkCodec::kNoFrame;
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000721}
722
723static bool restore_bg(const SkFrame& frame) {
724 return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
725}
726
727void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
728 const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
729 const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
730 const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
731
732 const int i = frame->frameId();
733 if (0 == i) {
734 frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
Nigel Tao66bc5242018-08-22 10:56:03 +1000735 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000736 return;
737 }
738
739
740 const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kPriorFrame;
741 if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
742 frame->setHasAlpha(reportsAlpha);
Nigel Tao66bc5242018-08-22 10:56:03 +1000743 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000744 return;
745 }
746
747 const SkFrame* prevFrame = this->getFrame(i-1);
748 while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
749 const int prevId = prevFrame->frameId();
750 if (0 == prevId) {
751 frame->setHasAlpha(true);
Nigel Tao66bc5242018-08-22 10:56:03 +1000752 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000753 return;
754 }
755
756 prevFrame = this->getFrame(prevId - 1);
757 }
758
759 const bool clearPrevFrame = restore_bg(*prevFrame);
760 auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
761
762 if (clearPrevFrame) {
763 if (prevFrameRect == screenRect || independent(*prevFrame)) {
764 frame->setHasAlpha(true);
Nigel Tao66bc5242018-08-22 10:56:03 +1000765 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000766 return;
767 }
768 }
769
770 if (reportsAlpha && blendWithPrevFrame) {
771 // Note: We could be more aggressive here. If prevFrame clears
772 // to background color and covers its required frame (and that
773 // frame is independent), prevFrame could be marked independent.
774 // Would this extra complexity be worth it?
775 frame->setRequiredFrame(prevFrame->frameId());
776 frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
777 return;
778 }
779
780 while (frameRect.contains(prevFrameRect)) {
781 const int prevRequiredFrame = prevFrame->getRequiredFrame();
Nigel Tao66bc5242018-08-22 10:56:03 +1000782 if (prevRequiredFrame == SkCodec::kNoFrame) {
783 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000784 frame->setHasAlpha(true);
785 return;
786 }
787
788 prevFrame = this->getFrame(prevRequiredFrame);
789 prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
790 }
791
792 if (restore_bg(*prevFrame)) {
793 frame->setHasAlpha(true);
794 if (prevFrameRect == screenRect || independent(*prevFrame)) {
Nigel Tao66bc5242018-08-22 10:56:03 +1000795 frame->setRequiredFrame(SkCodec::kNoFrame);
Nigel Taoa78b6dc2018-07-28 16:48:09 +1000796 } else {
797 // Note: As above, frame could still be independent, e.g. if
798 // prevFrame covers its required frame and that frame is
799 // independent.
800 frame->setRequiredFrame(prevFrame->frameId());
801 }
802 return;
803 }
804
805 SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
806 frame->setRequiredFrame(prevFrame->frameId());
807 frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
808}
809