• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/codec/SkCodec.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkColorSpace.h"
11 #include "include/core/SkData.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkStream.h"
14 #include "include/private/SkHalf.h"
15 #include "src/codec/SkCodecPriv.h"
16 #include "src/codec/SkFrameHolder.h"
17 
18 // We always include and compile in these BMP codecs
19 #include "src/codec/SkBmpCodec.h"
20 #include "src/codec/SkWbmpCodec.h"
21 
22 #ifdef SK_HAS_ANDROID_CODEC
23 #include "include/codec/SkAndroidCodec.h"
24 #endif
25 
26 #ifdef SK_HAS_HEIF_LIBRARY
27 #include "src/codec/SkHeifCodec.h"
28 #endif
29 
30 #ifdef SK_CODEC_DECODES_JPEG
31 #include "src/codec/SkJpegCodec.h"
32 #endif
33 
34 #ifdef SK_CODEC_DECODES_PNG
35 #include "src/codec/SkIcoCodec.h"
36 #include "src/codec/SkPngCodec.h"
37 #endif
38 
39 #ifdef SK_CODEC_DECODES_RAW
40 #include "src/codec/SkRawCodec.h"
41 #endif
42 
43 #ifdef SK_CODEC_DECODES_WEBP
44 #include "src/codec/SkWebpCodec.h"
45 #endif
46 
47 #ifdef SK_HAS_WUFFS_LIBRARY
48 #include "src/codec/SkWuffsCodec.h"
49 #elif defined(SK_USE_LIBGIFCODEC)
50 #include "SkGifCodec.h"
51 #endif
52 
53 struct DecoderProc {
54     bool (*IsFormat)(const void*, size_t);
55     std::unique_ptr<SkCodec> (*MakeFromStream)(std::unique_ptr<SkStream>, SkCodec::Result*);
56 };
57 
decoders()58 static std::vector<DecoderProc>* decoders() {
59     static auto* decoders = new std::vector<DecoderProc> {
60     #ifdef SK_CODEC_DECODES_JPEG
61         { SkJpegCodec::IsJpeg, SkJpegCodec::MakeFromStream },
62     #endif
63     #ifdef SK_CODEC_DECODES_WEBP
64         { SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
65     #endif
66     #ifdef SK_HAS_WUFFS_LIBRARY
67         { SkWuffsCodec_IsFormat, SkWuffsCodec_MakeFromStream },
68     #elif defined(SK_USE_LIBGIFCODEC)
69         { SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
70     #endif
71     #ifdef SK_CODEC_DECODES_PNG
72         { SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
73     #endif
74         { SkBmpCodec::IsBmp, SkBmpCodec::MakeFromStream },
75         { SkWbmpCodec::IsWbmp, SkWbmpCodec::MakeFromStream },
76     };
77     return decoders;
78 }
79 
Register(bool (* peek)(const void *,size_t),std::unique_ptr<SkCodec> (* make)(std::unique_ptr<SkStream>,SkCodec::Result *))80 void SkCodec::Register(
81             bool                     (*peek)(const void*, size_t),
82             std::unique_ptr<SkCodec> (*make)(std::unique_ptr<SkStream>, SkCodec::Result*)) {
83     decoders()->push_back(DecoderProc{peek, make});
84 }
85 
MakeFromStream(std::unique_ptr<SkStream> stream,Result * outResult,SkPngChunkReader * chunkReader,SelectionPolicy selectionPolicy)86 std::unique_ptr<SkCodec> SkCodec::MakeFromStream(
87         std::unique_ptr<SkStream> stream, Result* outResult,
88         SkPngChunkReader* chunkReader, SelectionPolicy selectionPolicy) {
89     Result resultStorage;
90     if (!outResult) {
91         outResult = &resultStorage;
92     }
93 
94     if (!stream) {
95         *outResult = kInvalidInput;
96         return nullptr;
97     }
98 
99     if (selectionPolicy != SelectionPolicy::kPreferStillImage
100             && selectionPolicy != SelectionPolicy::kPreferAnimation) {
101         *outResult = kInvalidParameters;
102         return nullptr;
103     }
104 
105     constexpr size_t bytesToRead = MinBufferedBytesNeeded();
106 
107     char buffer[bytesToRead];
108     size_t bytesRead = stream->peek(buffer, bytesToRead);
109 
110     // It is also possible to have a complete image less than bytesToRead bytes
111     // (e.g. a 1 x 1 wbmp), meaning peek() would return less than bytesToRead.
112     // Assume that if bytesRead < bytesToRead, but > 0, the stream is shorter
113     // than bytesToRead, so pass that directly to the decoder.
114     // It also is possible the stream uses too small a buffer for peeking, but
115     // we trust the caller to use a large enough buffer.
116 
117     if (0 == bytesRead) {
118         // TODO: After implementing peek in CreateJavaOutputStreamAdaptor.cpp, this
119         // printf could be useful to notice failures.
120         // SkCodecPrintf("Encoded image data failed to peek!\n");
121 
122         // It is possible the stream does not support peeking, but does support
123         // rewinding.
124         // Attempt to read() and pass the actual amount read to the decoder.
125         bytesRead = stream->read(buffer, bytesToRead);
126         if (!stream->rewind()) {
127             SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
128             *outResult = kCouldNotRewind;
129             return nullptr;
130         }
131     }
132 
133     // PNG is special, since we want to be able to supply an SkPngChunkReader.
134     // But this code follows the same pattern as the loop.
135 #ifdef SK_CODEC_DECODES_PNG
136     if (SkPngCodec::IsPng(buffer, bytesRead)) {
137         return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader);
138     } else
139 #endif
140     {
141         for (DecoderProc proc : *decoders()) {
142             if (proc.IsFormat(buffer, bytesRead)) {
143                 return proc.MakeFromStream(std::move(stream), outResult);
144             }
145         }
146 
147 #ifdef SK_HAS_HEIF_LIBRARY
148         SkEncodedImageFormat format;
149         if (SkHeifCodec::IsSupported(buffer, bytesRead, &format)) {
150             return SkHeifCodec::MakeFromStream(std::move(stream), selectionPolicy,
151                     format, outResult);
152         }
153 #endif
154 
155 #ifdef SK_CODEC_DECODES_RAW
156         // Try to treat the input as RAW if all the other checks failed.
157         return SkRawCodec::MakeFromStream(std::move(stream), outResult);
158 #endif
159     }
160 
161     if (bytesRead < bytesToRead) {
162         *outResult = kIncompleteInput;
163     } else {
164         *outResult = kUnimplemented;
165     }
166 
167     return nullptr;
168 }
169 
MakeFromData(sk_sp<SkData> data,SkPngChunkReader * reader)170 std::unique_ptr<SkCodec> SkCodec::MakeFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
171     if (!data) {
172         return nullptr;
173     }
174     return MakeFromStream(SkMemoryStream::Make(std::move(data)), nullptr, reader);
175 }
176 
SkCodec(SkEncodedInfo && info,XformFormat srcFormat,std::unique_ptr<SkStream> stream,SkEncodedOrigin origin)177 SkCodec::SkCodec(SkEncodedInfo&& info, XformFormat srcFormat, std::unique_ptr<SkStream> stream,
178                  SkEncodedOrigin origin)
179     : fEncodedInfo(std::move(info))
180     , fSrcXformFormat(srcFormat)
181     , fStream(std::move(stream))
182     , fNeedsRewind(false)
183     , fOrigin(origin)
184     , fDstInfo()
185     , fOptions()
186     , fCurrScanline(-1)
187     , fStartedIncrementalDecode(false)
188     , fAndroidCodecHandlesFrameIndex(false)
189 {}
190 
~SkCodec()191 SkCodec::~SkCodec() {}
192 
queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes & supportedDataTypes,SkYUVAPixmapInfo * yuvaPixmapInfo) const193 bool SkCodec::queryYUVAInfo(const SkYUVAPixmapInfo::SupportedDataTypes& supportedDataTypes,
194                             SkYUVAPixmapInfo* yuvaPixmapInfo) const {
195     if (!yuvaPixmapInfo) {
196         return false;
197     }
198     return this->onQueryYUVAInfo(supportedDataTypes, yuvaPixmapInfo) &&
199            yuvaPixmapInfo->isSupported(supportedDataTypes);
200 }
201 
getYUVAPlanes(const SkYUVAPixmaps & yuvaPixmaps)202 SkCodec::Result SkCodec::getYUVAPlanes(const SkYUVAPixmaps& yuvaPixmaps) {
203     if (!yuvaPixmaps.isValid()) {
204         return kInvalidInput;
205     }
206     if (!this->rewindIfNeeded()) {
207         return kCouldNotRewind;
208     }
209     return this->onGetYUVAPlanes(yuvaPixmaps);
210 }
211 
conversionSupported(const SkImageInfo & dst,bool srcIsOpaque,bool needsColorXform)212 bool SkCodec::conversionSupported(const SkImageInfo& dst, bool srcIsOpaque, bool needsColorXform) {
213     if (!valid_alpha(dst.alphaType(), srcIsOpaque)) {
214         return false;
215     }
216 
217     switch (dst.colorType()) {
218         case kRGBA_8888_SkColorType:
219         case kBGRA_8888_SkColorType:
220         case kRGBA_F16_SkColorType:
221             return true;
222         case kRGB_565_SkColorType:
223             return srcIsOpaque;
224         case kGray_8_SkColorType:
225             return SkEncodedInfo::kGray_Color == fEncodedInfo.color() && srcIsOpaque;
226         case kAlpha_8_SkColorType:
227             // conceptually we can convert anything into alpha_8, but we haven't actually coded
228             // all of those other conversions yet.
229             return SkEncodedInfo::kXAlpha_Color == fEncodedInfo.color();
230         default:
231             return false;
232     }
233 }
234 
rewindIfNeeded()235 bool SkCodec::rewindIfNeeded() {
236     // Store the value of fNeedsRewind so we can update it. Next read will
237     // require a rewind.
238     const bool needsRewind = fNeedsRewind;
239     fNeedsRewind = true;
240     if (!needsRewind) {
241         return true;
242     }
243 
244     // startScanlineDecode will need to be called before decoding scanlines.
245     fCurrScanline = -1;
246     // startIncrementalDecode will need to be called before incrementalDecode.
247     fStartedIncrementalDecode = false;
248 
249     // Some codecs do not have a stream.  They may hold onto their own data or another codec.
250     // They must handle rewinding themselves.
251     if (fStream && !fStream->rewind()) {
252         return false;
253     }
254 
255     return this->onRewind();
256 }
257 
frame_rect_on_screen(SkIRect frameRect,const SkIRect & screenRect)258 static SkIRect frame_rect_on_screen(SkIRect frameRect,
259                                     const SkIRect& screenRect) {
260     if (!frameRect.intersect(screenRect)) {
261         return SkIRect::MakeEmpty();
262     }
263 
264     return frameRect;
265 }
266 
zero_rect(const SkImageInfo & dstInfo,void * pixels,size_t rowBytes,SkISize srcDimensions,SkIRect prevRect)267 bool zero_rect(const SkImageInfo& dstInfo, void* pixels, size_t rowBytes,
268                SkISize srcDimensions, SkIRect prevRect) {
269     const auto dimensions = dstInfo.dimensions();
270     if (dimensions != srcDimensions) {
271         SkRect src = SkRect::Make(srcDimensions);
272         SkRect dst = SkRect::Make(dimensions);
273         SkMatrix map = SkMatrix::RectToRect(src, dst);
274         SkRect asRect = SkRect::Make(prevRect);
275         if (!map.mapRect(&asRect)) {
276             return false;
277         }
278         asRect.roundOut(&prevRect);
279     }
280 
281     if (!prevRect.intersect(SkIRect::MakeSize(dimensions))) {
282         // Nothing to zero, due to scaling or bad frame rect.
283         return true;
284     }
285 
286     const SkImageInfo info = dstInfo.makeDimensions(prevRect.size());
287     const size_t bpp = dstInfo.bytesPerPixel();
288     const size_t offset = prevRect.x() * bpp + prevRect.y() * rowBytes;
289     void* eraseDst = SkTAddOffset<void>(pixels, offset);
290     SkSampler::Fill(info, eraseDst, rowBytes, SkCodec::kNo_ZeroInitialized);
291     return true;
292 }
293 
handleFrameIndex(const SkImageInfo & info,void * pixels,size_t rowBytes,const Options & options,SkAndroidCodec * androidCodec)294 SkCodec::Result SkCodec::handleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
295                                           const Options& options, SkAndroidCodec* androidCodec) {
296     if (androidCodec) {
297         // This is never set back to false. If SkAndroidCodec is calling this method, its fCodec
298         // should never call it directly.
299         fAndroidCodecHandlesFrameIndex = true;
300     } else if (fAndroidCodecHandlesFrameIndex) {
301         return kSuccess;
302     }
303 
304     if (!this->rewindIfNeeded()) {
305         return kCouldNotRewind;
306     }
307 
308     const int index = options.fFrameIndex;
309     if (0 == index) {
310         return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
311             ? kSuccess : kInvalidConversion;
312     }
313 
314     if (index < 0) {
315         return kInvalidParameters;
316     }
317 
318     if (options.fSubset) {
319         // If we add support for this, we need to update the code that zeroes
320         // a kRestoreBGColor frame.
321         return kInvalidParameters;
322     }
323 
324     if (index >= this->onGetFrameCount()) {
325         return kIncompleteInput;
326     }
327 
328     const auto* frameHolder = this->getFrameHolder();
329     SkASSERT(frameHolder);
330 
331     const auto* frame = frameHolder->getFrame(index);
332     SkASSERT(frame);
333 
334     const int requiredFrame = frame->getRequiredFrame();
335     if (requiredFrame != kNoFrame) {
336         const SkFrame* preppedFrame = nullptr;
337         if (options.fPriorFrame == kNoFrame) {
338             Result result = kInternalError;
339             if (androidCodec) {
340 #ifdef SK_HAS_ANDROID_CODEC
341                 SkAndroidCodec::AndroidOptions prevFrameOptions(
342                         reinterpret_cast<const SkAndroidCodec::AndroidOptions&>(options));
343                 prevFrameOptions.fFrameIndex = requiredFrame;
344                 result = androidCodec->getAndroidPixels(info, pixels, rowBytes, &prevFrameOptions);
345 #endif
346             } else {
347                 Options prevFrameOptions(options);
348                 prevFrameOptions.fFrameIndex = requiredFrame;
349                 result = this->getPixels(info, pixels, rowBytes, &prevFrameOptions);
350             }
351             if (result != kSuccess) {
352                 return result;
353             }
354             preppedFrame = frameHolder->getFrame(requiredFrame);
355         } else {
356             // Check for a valid frame as a starting point. Alternatively, we could
357             // treat an invalid frame as not providing one, but rejecting it will
358             // make it easier to catch the mistake.
359             if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
360                 return kInvalidParameters;
361             }
362             preppedFrame = frameHolder->getFrame(options.fPriorFrame);
363         }
364 
365         SkASSERT(preppedFrame);
366         switch (preppedFrame->getDisposalMethod()) {
367             case SkCodecAnimation::DisposalMethod::kRestorePrevious:
368                 SkASSERT(options.fPriorFrame != kNoFrame);
369                 return kInvalidParameters;
370             case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
371                 // If a frame after the required frame is provided, there is no
372                 // need to clear, since it must be covered by the desired frame.
373                 // FIXME: If the required frame is kRestoreBGColor, we don't actually need to decode
374                 // it, since we'll just clear it to transparent. Instead, we could decode *its*
375                 // required frame and then clear.
376                 if (preppedFrame->frameId() == requiredFrame) {
377                     SkIRect preppedRect = preppedFrame->frameRect();
378                     if (!zero_rect(info, pixels, rowBytes, this->dimensions(), preppedRect)) {
379                         return kInternalError;
380                     }
381                 }
382                 break;
383             default:
384                 break;
385         }
386     }
387 
388     return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
389         ? kSuccess : kInvalidConversion;
390 }
391 
392 #ifdef SK_ENABLE_OHOS_CODEC
callHandleFrameIndex(const SkImageInfo & info,void * pixels,size_t rowBytes,const Options & handleOptions,GetPixelsCallback getPixelsFn)393 SkCodec::Result SkCodec::callHandleFrameIndex(const SkImageInfo& info, void* pixels, size_t rowBytes,
394                                               const Options& handleOptions, GetPixelsCallback getPixelsFn)
395 {
396     if (getPixelsFn) {
397         fAndroidCodecHandlesFrameIndex = true;
398     } else if (fAndroidCodecHandlesFrameIndex) {
399         return kSuccess;
400     }
401     if (!this->rewindIfNeeded()) {
402         return kCouldNotRewind;
403     }
404     const int handleIndex = handleOptions.fFrameIndex;
405     if (0 == handleIndex) {
406         return this->initializeColorXform(info, fEncodedInfo.alpha(), fEncodedInfo.opaque())
407             ? kSuccess : kInvalidConversion;
408     }
409     if (handleIndex < 0) {
410         return kInvalidParameters;
411     }
412     if (handleOptions.fSubset) {
413         return kInvalidParameters;
414     }
415     if (handleIndex >= this->onGetFrameCount()) {
416         return kIncompleteInput;
417     }
418     const auto* frameHolder = this->getFrameHolder();
419     SkASSERT(frameHolder);
420     const auto* frame = frameHolder->getFrame(handleIndex);
421     SkASSERT(frame);
422     const int requiredFrame = frame->getRequiredFrame();
423     if (requiredFrame != kNoFrame) {
424         const SkFrame* preparedFrame = nullptr;
425         if (handleOptions.fPriorFrame == kNoFrame) {
426             Result result = kInternalError;
427             if (getPixelsFn) {
428 #ifdef SK_HAS_ANDROID_CODEC
429                 result = getPixelsFn(info, pixels, rowBytes, handleOptions, requiredFrame);
430 #endif
431             } else {
432                 Options prevHandledFrameOptions(handleOptions);
433                 prevHandledFrameOptions.fFrameIndex = requiredFrame;
434                 result = this->getPixels(info, pixels, rowBytes, &prevHandledFrameOptions);
435             }
436             if (result != kSuccess) {
437                 return result;
438             }
439             preparedFrame = frameHolder->getFrame(requiredFrame);
440         } else {
441             if (handleOptions.fPriorFrame < requiredFrame || handleOptions.fPriorFrame >= handleIndex) {
442                 return kInvalidParameters;
443             }
444             preparedFrame = frameHolder->getFrame(handleOptions.fPriorFrame);
445         }
446 
447         SkASSERT(preparedFrame);
448         switch (preparedFrame->getDisposalMethod()) {
449             case SkCodecAnimation::DisposalMethod::kRestorePrevious:
450                 SkASSERT(handleOptions.fPriorFrame != kNoFrame);
451                 return kInvalidParameters;
452             case SkCodecAnimation::DisposalMethod::kRestoreBGColor:
453                 if (preparedFrame->frameId() == requiredFrame) {
454                     SkIRect preparedRect = preparedFrame->frameRect();
455                     if (!zero_rect(info, pixels, rowBytes, this->dimensions(), preparedRect)) {
456                         return kInternalError;
457                     }
458                 }
459                 break;
460             default:
461                 break;
462         }
463     }
464     return this->initializeColorXform(info, frame->reportedAlpha(), !frame->hasAlpha())
465         ? kSuccess : kInvalidConversion;
466 }
467 #endif
468 
getPixels(const SkImageInfo & info,void * pixels,size_t rowBytes,const Options * options)469 SkCodec::Result SkCodec::getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
470                                    const Options* options) {
471     if (kUnknown_SkColorType == info.colorType()) {
472         return kInvalidConversion;
473     }
474     if (nullptr == pixels) {
475         return kInvalidParameters;
476     }
477     if (rowBytes < info.minRowBytes()) {
478         return kInvalidParameters;
479     }
480 
481     // Default options.
482     Options optsStorage;
483     if (nullptr == options) {
484         options = &optsStorage;
485     } else {
486         if (options->fSubset) {
487             SkIRect subset(*options->fSubset);
488             if (!this->onGetValidSubset(&subset) || subset != *options->fSubset) {
489                 // FIXME: How to differentiate between not supporting subset at all
490                 // and not supporting this particular subset?
491                 return kUnimplemented;
492             }
493         }
494     }
495 
496     const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
497                                                            *options);
498     if (frameIndexResult != kSuccess) {
499         return frameIndexResult;
500     }
501 
502     // FIXME: Support subsets somehow? Note that this works for SkWebpCodec
503     // because it supports arbitrary scaling/subset combinations.
504     if (!this->dimensionsSupported(info.dimensions())) {
505         return kInvalidScale;
506     }
507 
508     fDstInfo = info;
509     fOptions = *options;
510 
511     // On an incomplete decode, the subclass will specify the number of scanlines that it decoded
512     // successfully.
513     int rowsDecoded = 0;
514     const Result result = this->onGetPixels(info, pixels, rowBytes, *options, &rowsDecoded);
515 
516     // A return value of kIncompleteInput indicates a truncated image stream.
517     // In this case, we will fill any uninitialized memory with a default value.
518     // Some subclasses will take care of filling any uninitialized memory on
519     // their own.  They indicate that all of the memory has been filled by
520     // setting rowsDecoded equal to the height.
521     if ((kIncompleteInput == result || kErrorInInput == result) && rowsDecoded != info.height()) {
522         // FIXME: (skbug.com/5772) fillIncompleteImage will fill using the swizzler's width, unless
523         // there is a subset. In that case, it will use the width of the subset. From here, the
524         // subset will only be non-null in the case of SkWebpCodec, but it treats the subset
525         // differenty from the other codecs, and it needs to use the width specified by the info.
526         // Set the subset to null so SkWebpCodec uses the correct width.
527         fOptions.fSubset = nullptr;
528         this->fillIncompleteImage(info, pixels, rowBytes, options->fZeroInitialized, info.height(),
529                 rowsDecoded);
530     }
531 
532     return result;
533 }
534 
getImage(const SkImageInfo & info,const Options * options)535 std::tuple<sk_sp<SkImage>, SkCodec::Result> SkCodec::getImage(const SkImageInfo& info,
536                                                               const Options* options) {
537     SkBitmap bm;
538     if (!bm.tryAllocPixels(info)) {
539         return {nullptr, kInternalError};
540     }
541 
542     Result result = this->getPixels(info, bm.getPixels(), bm.rowBytes(), options);
543     switch (result) {
544         case kSuccess:
545         case kIncompleteInput:
546         case kErrorInInput:
547             bm.setImmutable();
548             return {bm.asImage(), result};
549 
550         default: break;
551     }
552     return {nullptr, result};
553 }
554 
getImage()555 std::tuple<sk_sp<SkImage>, SkCodec::Result> SkCodec::getImage() {
556     return this->getImage(this->getInfo(), nullptr);
557 }
558 
startIncrementalDecode(const SkImageInfo & info,void * pixels,size_t rowBytes,const SkCodec::Options * options)559 SkCodec::Result SkCodec::startIncrementalDecode(const SkImageInfo& info, void* pixels,
560         size_t rowBytes, const SkCodec::Options* options) {
561     fStartedIncrementalDecode = false;
562 
563     if (kUnknown_SkColorType == info.colorType()) {
564         return kInvalidConversion;
565     }
566     if (nullptr == pixels) {
567         return kInvalidParameters;
568     }
569 
570     // Set options.
571     Options optsStorage;
572     if (nullptr == options) {
573         options = &optsStorage;
574     } else {
575         if (options->fSubset) {
576             SkIRect size = SkIRect::MakeSize(info.dimensions());
577             if (!size.contains(*options->fSubset)) {
578                 return kInvalidParameters;
579             }
580 
581             const int top = options->fSubset->top();
582             const int bottom = options->fSubset->bottom();
583             if (top < 0 || top >= info.height() || top >= bottom || bottom > info.height()) {
584                 return kInvalidParameters;
585             }
586         }
587     }
588 
589     const Result frameIndexResult = this->handleFrameIndex(info, pixels, rowBytes,
590                                                            *options);
591     if (frameIndexResult != kSuccess) {
592         return frameIndexResult;
593     }
594 
595     if (!this->dimensionsSupported(info.dimensions())) {
596         return kInvalidScale;
597     }
598 
599     fDstInfo = info;
600     fOptions = *options;
601 
602     const Result result = this->onStartIncrementalDecode(info, pixels, rowBytes, fOptions);
603     if (kSuccess == result) {
604         fStartedIncrementalDecode = true;
605     } else if (kUnimplemented == result) {
606         // FIXME: This is temporarily necessary, until we transition SkCodec
607         // implementations from scanline decoding to incremental decoding.
608         // SkAndroidCodec will first attempt to use incremental decoding, but
609         // will fall back to scanline decoding if incremental returns
610         // kUnimplemented. rewindIfNeeded(), above, set fNeedsRewind to true
611         // (after potentially rewinding), but we do not want the next call to
612         // startScanlineDecode() to do a rewind.
613         fNeedsRewind = false;
614     }
615     return result;
616 }
617 
618 
startScanlineDecode(const SkImageInfo & info,const SkCodec::Options * options)619 SkCodec::Result SkCodec::startScanlineDecode(const SkImageInfo& info,
620         const SkCodec::Options* options) {
621     // Reset fCurrScanline in case of failure.
622     fCurrScanline = -1;
623 
624     // Set options.
625     Options optsStorage;
626     if (nullptr == options) {
627         options = &optsStorage;
628     } else if (options->fSubset) {
629         SkIRect size = SkIRect::MakeSize(info.dimensions());
630         if (!size.contains(*options->fSubset)) {
631             return kInvalidInput;
632         }
633 
634         // We only support subsetting in the x-dimension for scanline decoder.
635         // Subsetting in the y-dimension can be accomplished using skipScanlines().
636         if (options->fSubset->top() != 0 || options->fSubset->height() != info.height()) {
637             return kInvalidInput;
638         }
639     }
640 
641     // Scanline decoding only supports decoding the first frame.
642     if (options->fFrameIndex != 0) {
643         return kUnimplemented;
644     }
645 
646     // The void* dst and rowbytes in handleFrameIndex or only used for decoding prior
647     // frames, which is not supported here anyway, so it is safe to pass nullptr/0.
648     const Result frameIndexResult = this->handleFrameIndex(info, nullptr, 0, *options);
649     if (frameIndexResult != kSuccess) {
650         return frameIndexResult;
651     }
652 
653     // FIXME: Support subsets somehow?
654     if (!this->dimensionsSupported(info.dimensions())) {
655         return kInvalidScale;
656     }
657 
658     const Result result = this->onStartScanlineDecode(info, *options);
659     if (result != SkCodec::kSuccess) {
660         return result;
661     }
662 
663     // FIXME: See startIncrementalDecode. That method set fNeedsRewind to false
664     // so that when onStartScanlineDecode calls rewindIfNeeded it would not
665     // rewind. But it also relies on that call to rewindIfNeeded to set
666     // fNeedsRewind to true for future decodes. When
667     // fAndroidCodecHandlesFrameIndex is true, that call to rewindIfNeeded is
668     // skipped, so this method sets it back to true.
669     SkASSERT(fAndroidCodecHandlesFrameIndex || fNeedsRewind);
670     fNeedsRewind = true;
671 
672     fCurrScanline = 0;
673     fDstInfo = info;
674     fOptions = *options;
675     return kSuccess;
676 }
677 
getScanlines(void * dst,int countLines,size_t rowBytes)678 int SkCodec::getScanlines(void* dst, int countLines, size_t rowBytes) {
679     if (fCurrScanline < 0) {
680         return 0;
681     }
682 
683     SkASSERT(!fDstInfo.isEmpty());
684     if (countLines <= 0 || fCurrScanline + countLines > fDstInfo.height()) {
685         return 0;
686     }
687 
688     const int linesDecoded = this->onGetScanlines(dst, countLines, rowBytes);
689     if (linesDecoded < countLines) {
690         this->fillIncompleteImage(this->dstInfo(), dst, rowBytes, this->options().fZeroInitialized,
691                 countLines, linesDecoded);
692     }
693     fCurrScanline += countLines;
694     return linesDecoded;
695 }
696 
skipScanlines(int countLines)697 bool SkCodec::skipScanlines(int countLines) {
698     if (fCurrScanline < 0) {
699         return false;
700     }
701 
702     SkASSERT(!fDstInfo.isEmpty());
703     if (countLines < 0 || fCurrScanline + countLines > fDstInfo.height()) {
704         // Arguably, we could just skip the scanlines which are remaining,
705         // and return true. We choose to return false so the client
706         // can catch their bug.
707         return false;
708     }
709 
710     bool result = this->onSkipScanlines(countLines);
711     fCurrScanline += countLines;
712     return result;
713 }
714 
outputScanline(int inputScanline) const715 int SkCodec::outputScanline(int inputScanline) const {
716     SkASSERT(0 <= inputScanline && inputScanline < fEncodedInfo.height());
717     return this->onOutputScanline(inputScanline);
718 }
719 
onOutputScanline(int inputScanline) const720 int SkCodec::onOutputScanline(int inputScanline) const {
721     switch (this->getScanlineOrder()) {
722         case kTopDown_SkScanlineOrder:
723             return inputScanline;
724         case kBottomUp_SkScanlineOrder:
725             return fEncodedInfo.height() - inputScanline - 1;
726         default:
727             // This case indicates an interlaced gif and is implemented by SkGifCodec.
728             SkASSERT(false);
729             return 0;
730     }
731 }
732 
fillIncompleteImage(const SkImageInfo & info,void * dst,size_t rowBytes,ZeroInitialized zeroInit,int linesRequested,int linesDecoded)733 void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t rowBytes,
734         ZeroInitialized zeroInit, int linesRequested, int linesDecoded) {
735     if (kYes_ZeroInitialized == zeroInit) {
736         return;
737     }
738 
739     const int linesRemaining = linesRequested - linesDecoded;
740     SkSampler* sampler = this->getSampler(false);
741 
742     const int fillWidth = sampler          ? sampler->fillWidth()      :
743                           fOptions.fSubset ? fOptions.fSubset->width() :
744                                              info.width()              ;
745     void* fillDst = this->getScanlineOrder() == kBottomUp_SkScanlineOrder ? dst :
746                         SkTAddOffset<void>(dst, linesDecoded * rowBytes);
747     const auto fillInfo = info.makeWH(fillWidth, linesRemaining);
748     SkSampler::Fill(fillInfo, fillDst, rowBytes, kNo_ZeroInitialized);
749 }
750 
sk_select_xform_format(SkColorType colorType,bool forColorTable,skcms_PixelFormat * outFormat)751 bool sk_select_xform_format(SkColorType colorType, bool forColorTable,
752                             skcms_PixelFormat* outFormat) {
753     SkASSERT(outFormat);
754 
755     switch (colorType) {
756         case kRGBA_8888_SkColorType:
757             *outFormat = skcms_PixelFormat_RGBA_8888;
758             break;
759         case kBGRA_8888_SkColorType:
760             *outFormat = skcms_PixelFormat_BGRA_8888;
761             break;
762         case kRGB_565_SkColorType:
763             if (forColorTable) {
764 #ifdef SK_PMCOLOR_IS_RGBA
765                 *outFormat = skcms_PixelFormat_RGBA_8888;
766 #else
767                 *outFormat = skcms_PixelFormat_BGRA_8888;
768 #endif
769                 break;
770             }
771             *outFormat = skcms_PixelFormat_BGR_565;
772             break;
773         case kRGBA_F16_SkColorType:
774             *outFormat = skcms_PixelFormat_RGBA_hhhh;
775             break;
776         case kGray_8_SkColorType:
777             *outFormat = skcms_PixelFormat_G_8;
778             break;
779         default:
780             return false;
781     }
782     return true;
783 }
784 
initializeColorXform(const SkImageInfo & dstInfo,SkEncodedInfo::Alpha encodedAlpha,bool srcIsOpaque)785 bool SkCodec::initializeColorXform(const SkImageInfo& dstInfo, SkEncodedInfo::Alpha encodedAlpha,
786                                    bool srcIsOpaque) {
787     fXformTime = kNo_XformTime;
788     bool needsColorXform = false;
789     if (this->usesColorXform()) {
790         if (kRGBA_F16_SkColorType == dstInfo.colorType()) {
791             needsColorXform = true;
792             if (dstInfo.colorSpace()) {
793                 dstInfo.colorSpace()->toProfile(&fDstProfile);
794             } else {
795                 // Use the srcProfile to avoid conversion.
796                 const auto* srcProfile = fEncodedInfo.profile();
797                 fDstProfile = srcProfile ? *srcProfile : *skcms_sRGB_profile();
798             }
799         } else if (dstInfo.colorSpace()) {
800             dstInfo.colorSpace()->toProfile(&fDstProfile);
801             const auto* srcProfile = fEncodedInfo.profile();
802             if (!srcProfile) {
803                 srcProfile = skcms_sRGB_profile();
804             }
805             if (!skcms_ApproximatelyEqualProfiles(srcProfile, &fDstProfile) ) {
806                 needsColorXform = true;
807             }
808         }
809     }
810 
811     if (!this->conversionSupported(dstInfo, srcIsOpaque, needsColorXform)) {
812         return false;
813     }
814 
815     if (needsColorXform) {
816         fXformTime = SkEncodedInfo::kPalette_Color != fEncodedInfo.color()
817                           || kRGBA_F16_SkColorType == dstInfo.colorType()
818                 ? kDecodeRow_XformTime : kPalette_XformTime;
819         if (!sk_select_xform_format(dstInfo.colorType(), fXformTime == kPalette_XformTime,
820                                     &fDstXformFormat)) {
821             return false;
822         }
823         if (encodedAlpha == SkEncodedInfo::kUnpremul_Alpha
824                 && dstInfo.alphaType() == kPremul_SkAlphaType) {
825             fDstXformAlphaFormat = skcms_AlphaFormat_PremulAsEncoded;
826         } else {
827             fDstXformAlphaFormat = skcms_AlphaFormat_Unpremul;
828         }
829     }
830     return true;
831 }
832 
applyColorXform(void * dst,const void * src,int count) const833 void SkCodec::applyColorXform(void* dst, const void* src, int count) const {
834     // It is okay for srcProfile to be null. This will use sRGB.
835     const auto* srcProfile = fEncodedInfo.profile();
836     SkAssertResult(skcms_Transform(src, fSrcXformFormat, skcms_AlphaFormat_Unpremul, srcProfile,
837                                    dst, fDstXformFormat, fDstXformAlphaFormat, &fDstProfile,
838                                    count));
839 }
840 
getFrameInfo()841 std::vector<SkCodec::FrameInfo> SkCodec::getFrameInfo() {
842     const int frameCount = this->getFrameCount();
843     SkASSERT(frameCount >= 0);
844     if (frameCount <= 0) {
845         return std::vector<FrameInfo>{};
846     }
847 
848     if (frameCount == 1 && !this->onGetFrameInfo(0, nullptr)) {
849         // Not animated.
850         return std::vector<FrameInfo>{};
851     }
852 
853     std::vector<FrameInfo> result(frameCount);
854     for (int i = 0; i < frameCount; ++i) {
855         SkAssertResult(this->onGetFrameInfo(i, &result[i]));
856     }
857     return result;
858 }
859 
ResultToString(Result result)860 const char* SkCodec::ResultToString(Result result) {
861     switch (result) {
862         case kSuccess:
863             return "success";
864         case kIncompleteInput:
865             return "incomplete input";
866         case kErrorInInput:
867             return "error in input";
868         case kInvalidConversion:
869             return "invalid conversion";
870         case kInvalidScale:
871             return "invalid scale";
872         case kInvalidParameters:
873             return "invalid parameters";
874         case kInvalidInput:
875             return "invalid input";
876         case kCouldNotRewind:
877             return "could not rewind";
878         case kInternalError:
879             return "internal error";
880         case kUnimplemented:
881             return "unimplemented";
882         default:
883             SkASSERT(false);
884             return "bogus result value";
885     }
886 }
887 
fillIn(SkCodec::FrameInfo * frameInfo,bool fullyReceived) const888 void SkFrame::fillIn(SkCodec::FrameInfo* frameInfo, bool fullyReceived) const {
889     SkASSERT(frameInfo);
890 
891     frameInfo->fRequiredFrame = fRequiredFrame;
892     frameInfo->fDuration = fDuration;
893     frameInfo->fFullyReceived = fullyReceived;
894     frameInfo->fAlphaType = fHasAlpha ? kUnpremul_SkAlphaType
895                                       : kOpaque_SkAlphaType;
896     frameInfo->fHasAlphaWithinBounds = this->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
897     frameInfo->fDisposalMethod = fDisposalMethod;
898     frameInfo->fBlend = fBlend;
899     frameInfo->fFrameRect = fRect;
900 }
901 
independent(const SkFrame & frame)902 static bool independent(const SkFrame& frame) {
903     return frame.getRequiredFrame() == SkCodec::kNoFrame;
904 }
905 
restore_bg(const SkFrame & frame)906 static bool restore_bg(const SkFrame& frame) {
907     return frame.getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestoreBGColor;
908 }
909 
910 // As its name suggests, this method computes a frame's alpha (e.g. completely
911 // opaque, unpremul, binary) and its required frame (a preceding frame that
912 // this frame depends on, to draw the complete image at this frame's point in
913 // the animation stream), and calls this frame's setter methods with that
914 // computed information.
915 //
916 // A required frame of kNoFrame means that this frame is independent: drawing
917 // the complete image at this frame's point in the animation stream does not
918 // require first preparing the pixel buffer based on another frame. Instead,
919 // drawing can start from an uninitialized pixel buffer.
920 //
921 // "Uninitialized" is from the SkCodec's caller's point of view. In the SkCodec
922 // implementation, for independent frames, first party Skia code (in src/codec)
923 // will typically fill the buffer with a uniform background color (e.g.
924 // transparent black) before calling into third party codec-specific code (e.g.
925 // libjpeg or libpng). Pixels outside of the frame's rect will remain this
926 // background color after drawing this frame. For incomplete decodes, pixels
927 // inside that rect may be (at least temporarily) set to that background color.
928 // In an incremental decode, later passes may then overwrite that background
929 // color.
930 //
931 // Determining kNoFrame or otherwise involves testing a number of conditions
932 // sequentially. The first satisfied condition results in setting the required
933 // frame to kNoFrame (an "INDx" condition) or to a non-negative frame number (a
934 // "DEPx" condition), and the function returning early. Those "INDx" and "DEPx"
935 // labels also map to comments in the function body.
936 //
937 //  - IND1: this frame is the first frame.
938 //  - IND2: this frame fills out the whole image, and it is completely opaque
939 //          or it overwrites (not blends with) the previous frame.
940 //  - IND3: all preceding frames' disposals are kRestorePrevious.
941 //  - IND4: the prevFrame's disposal is kRestoreBGColor, and it fills out the
942 //          whole image or it is itself otherwise independent.
943 //  - DEP5: this frame reports alpha (it is not completely opaque) and it
944 //          blends with (not overwrites) the previous frame.
945 //  - IND6: this frame's rect covers the rects of all preceding frames back to
946 //          and including the most recent independent frame before this frame.
947 //  - DEP7: unconditional.
948 //
949 // The "prevFrame" variable initially points to the previous frame (also known
950 // as the prior frame), but that variable may iterate further backwards over
951 // the course of this computation.
setAlphaAndRequiredFrame(SkFrame * frame)952 void SkFrameHolder::setAlphaAndRequiredFrame(SkFrame* frame) {
953     const bool reportsAlpha = frame->reportedAlpha() != SkEncodedInfo::kOpaque_Alpha;
954     const auto screenRect = SkIRect::MakeWH(fScreenWidth, fScreenHeight);
955     const auto frameRect = frame_rect_on_screen(frame->frameRect(), screenRect);
956 
957     const int i = frame->frameId();
958     if (0 == i) {
959         frame->setHasAlpha(reportsAlpha || frameRect != screenRect);
960         frame->setRequiredFrame(SkCodec::kNoFrame);  // IND1
961         return;
962     }
963 
964 
965     const bool blendWithPrevFrame = frame->getBlend() == SkCodecAnimation::Blend::kSrcOver;
966     if ((!reportsAlpha || !blendWithPrevFrame) && frameRect == screenRect) {
967         frame->setHasAlpha(reportsAlpha);
968         frame->setRequiredFrame(SkCodec::kNoFrame);  // IND2
969         return;
970     }
971 
972     const SkFrame* prevFrame = this->getFrame(i-1);
973     while (prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kRestorePrevious) {
974         const int prevId = prevFrame->frameId();
975         if (0 == prevId) {
976             frame->setHasAlpha(true);
977             frame->setRequiredFrame(SkCodec::kNoFrame);  // IND3
978             return;
979         }
980 
981         prevFrame = this->getFrame(prevId - 1);
982     }
983 
984     const bool clearPrevFrame = restore_bg(*prevFrame);
985     auto prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
986 
987     if (clearPrevFrame) {
988         if (prevFrameRect == screenRect || independent(*prevFrame)) {
989             frame->setHasAlpha(true);
990             frame->setRequiredFrame(SkCodec::kNoFrame);  // IND4
991             return;
992         }
993     }
994 
995     if (reportsAlpha && blendWithPrevFrame) {
996         // Note: We could be more aggressive here. If prevFrame clears
997         // to background color and covers its required frame (and that
998         // frame is independent), prevFrame could be marked independent.
999         // Would this extra complexity be worth it?
1000         frame->setRequiredFrame(prevFrame->frameId());  // DEP5
1001         frame->setHasAlpha(prevFrame->hasAlpha() || clearPrevFrame);
1002         return;
1003     }
1004 
1005     while (frameRect.contains(prevFrameRect)) {
1006         const int prevRequiredFrame = prevFrame->getRequiredFrame();
1007         if (prevRequiredFrame == SkCodec::kNoFrame) {
1008             frame->setRequiredFrame(SkCodec::kNoFrame);  // IND6
1009             frame->setHasAlpha(true);
1010             return;
1011         }
1012 
1013         prevFrame = this->getFrame(prevRequiredFrame);
1014         prevFrameRect = frame_rect_on_screen(prevFrame->frameRect(), screenRect);
1015     }
1016 
1017     frame->setRequiredFrame(prevFrame->frameId());  // DEP7
1018     if (restore_bg(*prevFrame)) {
1019         frame->setHasAlpha(true);
1020         return;
1021     }
1022     SkASSERT(prevFrame->getDisposalMethod() == SkCodecAnimation::DisposalMethod::kKeep);
1023     frame->setHasAlpha(prevFrame->hasAlpha() || (reportsAlpha && !blendWithPrevFrame));
1024 }
1025 
1026