• 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 "src/codec/SkWebpCodec.h"
9 
10 #include "include/codec/SkCodec.h"
11 #include "include/codec/SkCodecAnimation.h"
12 #include "include/codec/SkWebpDecoder.h"
13 #include "include/core/SkAlphaType.h"
14 #include "include/core/SkBitmap.h"
15 #include "include/core/SkColorType.h"
16 #include "include/core/SkImageInfo.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkStream.h"
20 #include "include/private/base/SkAlign.h"
21 #include "include/private/base/SkMath.h"
22 #include "include/private/base/SkNoncopyable.h"
23 #include "include/private/base/SkTFitsIn.h"
24 #include "include/private/base/SkTemplates.h"
25 #include "include/private/base/SkTo.h"
26 #include "modules/skcms/skcms.h"
27 #include "src/codec/SkParseEncodedOrigin.h"
28 #include "src/codec/SkSampler.h"
29 #include "src/core/SkRasterPipeline.h"
30 #include "src/core/SkRasterPipelineOpContexts.h"
31 #include "src/core/SkRasterPipelineOpList.h"
32 #include "src/core/SkStreamPriv.h"
33 
34 #include <algorithm>
35 #include <cstdint>
36 #include <cstring>
37 #include <optional>
38 #include <utility>
39 
40 // A WebP decoder on top of (subset of) libwebp
41 // For more information on WebP image format, and libwebp library, see:
42 //   https://code.google.com/speed/webp/
43 //   http://www.webmproject.org/code/#libwebp-webp-image-library
44 //   https://chromium.googlesource.com/webm/libwebp
45 
46 // If moving libwebp out of skia source tree, path for webp headers must be
47 // updated accordingly. Here, we enforce using local copy in webp sub-directory.
48 #include "webp/decode.h"  // NO_G3_REWRITE
49 #include "webp/demux.h"  // NO_G3_REWRITE
50 #include "webp/mux_types.h"  // NO_G3_REWRITE
51 
IsWebp(const void * buf,size_t bytesRead)52 bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
53     // WEBP starts with the following:
54     // RIFFXXXXWEBPVP
55     // Where XXXX is unspecified.
56     const char* bytes = static_cast<const char*>(buf);
57     return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
58 }
59 
60 // Parse headers of RIFF container, and check for valid Webp (VP8) content.
61 // Returns an SkWebpCodec on success
MakeFromStream(std::unique_ptr<SkStream> stream,Result * result)62 std::unique_ptr<SkCodec> SkWebpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
63                                                      Result* result) {
64     SkASSERT(result);
65     if (!stream) {
66         *result = SkCodec::kInvalidInput;
67         return nullptr;
68     }
69     // Webp demux needs a contiguous data buffer.
70     sk_sp<SkData> data = nullptr;
71     if (stream->getMemoryBase()) {
72         // It is safe to make without copy because we'll hold onto the stream.
73         data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength());
74     } else {
75         data = SkCopyStreamToData(stream.get());
76 
77         // If we are forced to copy the stream to a data, we can go ahead and delete the stream.
78         stream.reset(nullptr);
79     }
80 
81     // It's a little strange that the |demux| will outlive |webpData|, though it needs the
82     // pointer in |webpData| to remain valid.  This works because the pointer remains valid
83     // until the SkData is freed.
84     WebPData webpData = { data->bytes(), data->size() };
85     WebPDemuxState state;
86     SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> demux(WebPDemuxPartial(&webpData, &state));
87     switch (state) {
88         case WEBP_DEMUX_PARSE_ERROR:
89             *result = kInvalidInput;
90             return nullptr;
91         case WEBP_DEMUX_PARSING_HEADER:
92             *result = kIncompleteInput;
93             return nullptr;
94         case WEBP_DEMUX_PARSED_HEADER:
95         case WEBP_DEMUX_DONE:
96             SkASSERT(demux);
97             break;
98     }
99 
100     const int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
101     const int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
102 
103     // Validate the image size that's about to be decoded.
104     {
105         const int64_t size = sk_64_mul(width, height);
106         // now check that if we are 4-bytes per pixel, we also don't overflow
107         if (!SkTFitsIn<int32_t>(size) || SkTo<int32_t>(size) > (0x7FFFFFFF >> 2)) {
108             *result = kInvalidInput;
109             return nullptr;
110         }
111     }
112 
113     std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr;
114     {
115         WebPChunkIterator chunkIterator;
116         SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
117         if (WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) {
118             // FIXME: I think this could be MakeWithoutCopy
119             auto chunk = SkData::MakeWithCopy(chunkIterator.chunk.bytes, chunkIterator.chunk.size);
120             profile = SkEncodedInfo::ICCProfile::Make(std::move(chunk));
121         }
122         if (profile && profile->profile()->data_color_space != skcms_Signature_RGB) {
123             profile = nullptr;
124         }
125     }
126 
127     SkEncodedOrigin origin = kDefault_SkEncodedOrigin;
128     {
129         WebPChunkIterator chunkIterator;
130         SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
131         if (WebPDemuxGetChunk(demux, "EXIF", 1, &chunkIterator)) {
132             SkParseEncodedOrigin(chunkIterator.chunk.bytes, chunkIterator.chunk.size, &origin);
133         }
134     }
135 
136     // Get the first frame and its "features" to determine the color and alpha types.
137     WebPIterator frame;
138     SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
139     if (!WebPDemuxGetFrame(demux, 1, &frame)) {
140         *result = kIncompleteInput;
141         return nullptr;
142     }
143 
144     WebPBitstreamFeatures features;
145     switch (WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features)) {
146         case VP8_STATUS_OK:
147             break;
148         case VP8_STATUS_SUSPENDED:
149         case VP8_STATUS_NOT_ENOUGH_DATA:
150             *result = kIncompleteInput;
151             return nullptr;
152         default:
153             *result = kInvalidInput;
154             return nullptr;
155     }
156 
157     const bool hasAlpha = SkToBool(frame.has_alpha)
158             || frame.width != width || frame.height != height;
159     SkEncodedInfo::Color color;
160     SkEncodedInfo::Alpha alpha;
161     switch (features.format) {
162         case 0:
163             // This indicates a "mixed" format.  We could see this for
164             // animated webps (multiple fragments).
165             // We could also guess kYUV here, but I think it makes more
166             // sense to guess kBGRA which is likely closer to the final
167             // output.  Otherwise, we might end up converting
168             // BGRA->YUVA->BGRA.
169             [[fallthrough]];
170         case 2:
171             // This is the lossless format (BGRA).
172             if (hasAlpha) {
173                 color = SkEncodedInfo::kBGRA_Color;
174                 alpha = SkEncodedInfo::kUnpremul_Alpha;
175             } else {
176                 color = SkEncodedInfo::kBGRX_Color;
177                 alpha = SkEncodedInfo::kOpaque_Alpha;
178             }
179             break;
180         case 1:
181             // This is the lossy format (YUV).
182             if (hasAlpha) {
183                 color = SkEncodedInfo::kYUVA_Color;
184                 alpha = SkEncodedInfo::kUnpremul_Alpha;
185             } else {
186                 color = SkEncodedInfo::kYUV_Color;
187                 alpha = SkEncodedInfo::kOpaque_Alpha;
188             }
189             break;
190         default:
191             *result = kInvalidInput;
192             return nullptr;
193     }
194 
195 
196     *result = kSuccess;
197     SkEncodedInfo info = SkEncodedInfo::Make(width, height, color, alpha, 8, std::move(profile));
198     return std::unique_ptr<SkCodec>(new SkWebpCodec(std::move(info), std::move(stream),
199                                                     demux.release(), std::move(data), origin));
200 }
201 
webp_decode_mode(SkColorType dstCT,bool premultiply)202 static WEBP_CSP_MODE webp_decode_mode(SkColorType dstCT, bool premultiply) {
203     switch (dstCT) {
204         case kBGRA_8888_SkColorType:
205             return premultiply ? MODE_bgrA : MODE_BGRA;
206         case kRGBA_8888_SkColorType:
207             return premultiply ? MODE_rgbA : MODE_RGBA;
208         case kRGB_565_SkColorType:
209             return MODE_RGB_565;
210         default:
211             return MODE_LAST;
212     }
213 }
214 
appendNewFrame(bool hasAlpha)215 SkWebpCodec::Frame* SkWebpCodec::FrameHolder::appendNewFrame(bool hasAlpha) {
216     const int i = this->size();
217     fFrames.emplace_back(i, hasAlpha ? SkEncodedInfo::kUnpremul_Alpha
218                                      : SkEncodedInfo::kOpaque_Alpha);
219     return &fFrames[i];
220 }
221 
onGetValidSubset(SkIRect * desiredSubset) const222 bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
223     if (!desiredSubset) {
224         return false;
225     }
226 
227     if (!this->bounds().contains(*desiredSubset)) {
228         return false;
229     }
230 
231     // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we
232     // decode this exact subset.
233     // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested.
234     desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1;
235     desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1;
236     return true;
237 }
238 
onGetRepetitionCount()239 int SkWebpCodec::onGetRepetitionCount() {
240     auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
241     if (!(flags & ANIMATION_FLAG)) {
242         return 0;
243     }
244 
245     int loopCount = WebPDemuxGetI(fDemux.get(), WEBP_FF_LOOP_COUNT);
246     if (0 == loopCount) {
247         return kRepetitionCountInfinite;
248     }
249 
250     loopCount--;
251     return loopCount;
252 }
253 
onIsAnimated()254 SkCodec::IsAnimated SkWebpCodec::onIsAnimated() {
255     auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
256     return (flags & ANIMATION_FLAG) != 0 ? IsAnimated::kYes : IsAnimated::kNo;
257 }
258 
onGetFrameCount()259 int SkWebpCodec::onGetFrameCount() {
260     auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
261     if (!(flags & ANIMATION_FLAG)) {
262         return 1;
263     }
264 
265     const uint32_t oldFrameCount = fFrameHolder.size();
266     if (fFailed) {
267         return oldFrameCount;
268     }
269 
270     const uint32_t frameCount = WebPDemuxGetI(fDemux, WEBP_FF_FRAME_COUNT);
271     if (oldFrameCount == frameCount) {
272         // We have already parsed this.
273         return frameCount;
274     }
275 
276     fFrameHolder.reserve(frameCount);
277 
278     for (uint32_t i = oldFrameCount; i < frameCount; i++) {
279         WebPIterator iter;
280         SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoIter(&iter);
281 
282         if (!WebPDemuxGetFrame(fDemux.get(), i + 1, &iter)) {
283             fFailed = true;
284             break;
285         }
286 
287         // libwebp only reports complete frames of an animated image.
288         SkASSERT(iter.complete);
289 
290         Frame* frame = fFrameHolder.appendNewFrame(iter.has_alpha);
291         frame->setXYWH(iter.x_offset, iter.y_offset, iter.width, iter.height);
292         frame->setDisposalMethod(iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ?
293                 SkCodecAnimation::DisposalMethod::kRestoreBGColor :
294                 SkCodecAnimation::DisposalMethod::kKeep);
295         frame->setDuration(iter.duration);
296         if (WEBP_MUX_BLEND != iter.blend_method) {
297             frame->setBlend(SkCodecAnimation::Blend::kSrc);
298         }
299         fFrameHolder.setAlphaAndRequiredFrame(frame);
300     }
301 
302     return fFrameHolder.size();
303 
304 }
305 
onGetFrame(int i) const306 const SkFrame* SkWebpCodec::FrameHolder::onGetFrame(int i) const {
307     return static_cast<const SkFrame*>(this->frame(i));
308 }
309 
frame(int i) const310 const SkWebpCodec::Frame* SkWebpCodec::FrameHolder::frame(int i) const {
311     SkASSERT(i >= 0 && i < this->size());
312     return &fFrames[i];
313 }
314 
onGetFrameInfo(int i,FrameInfo * frameInfo) const315 bool SkWebpCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const {
316     if (i >= fFrameHolder.size()) {
317         return false;
318     }
319 
320     const Frame* frame = fFrameHolder.frame(i);
321     if (!frame) {
322         return false;
323     }
324 
325     if (frameInfo) {
326         // libwebp only reports fully received frames for an
327         // animated image.
328         frame->fillIn(frameInfo, true);
329     }
330 
331     return true;
332 }
333 
is_8888(SkColorType colorType)334 static bool is_8888(SkColorType colorType) {
335     switch (colorType) {
336         case kRGBA_8888_SkColorType:
337         case kBGRA_8888_SkColorType:
338             return true;
339         default:
340             return false;
341     }
342 }
343 
344 namespace {
345 
346 // Requires that the src input be unpremultiplied (or opaque).
347 class RPBlender final : SkNoncopyable {
348 public:
RPBlender(SkColorType dstCT,SkColorType srcCT,SkAlphaType dstAt,bool srcHasAlpha)349     RPBlender(SkColorType dstCT, SkColorType srcCT, SkAlphaType dstAt, bool srcHasAlpha)
350     {
351         fRP.appendLoadDst(dstCT, &fDstCtx);
352         if (kUnpremul_SkAlphaType == dstAt) {
353             fRP.append(SkRasterPipelineOp::premul_dst);
354         }
355 
356         fRP.appendLoad(srcCT, &fSrcCtx);
357         if (srcHasAlpha) {
358             fRP.append(SkRasterPipelineOp::premul);
359         }
360 
361         fRP.append(SkRasterPipelineOp::srcover);
362 
363         if (kUnpremul_SkAlphaType == dstAt) {
364             fRP.append(SkRasterPipelineOp::unpremul);
365         }
366         fRP.appendStore(dstCT, &fDstCtx);
367     }
368 
blendLine(void * dst,const void * src,int width)369     void blendLine(void* dst, const void* src, int width) {
370         fDstCtx = {                    dst, 0 };
371         fSrcCtx = { const_cast<void*>(src), 0 };
372 
373         fRP.run(0, 0, width, 1);
374     }
375 
376 private:
377     SkRasterPipeline_MemoryCtx fDstCtx, fSrcCtx;
378 
379     SkRasterPipeline_<256> fRP;
380 };
381 
382 }  // namespace
383 
onGetPixels(const SkImageInfo & dstInfo,void * dst,size_t rowBytes,const Options & options,int * rowsDecodedPtr)384 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
385                                          const Options& options, int* rowsDecodedPtr) {
386     const int index = options.fFrameIndex;
387     SkASSERT(0 == index || index < fFrameHolder.size());
388     SkASSERT(0 == index || !options.fSubset);
389 
390     WebPDecoderConfig config;
391     if (0 == WebPInitDecoderConfig(&config)) {
392         // ABI mismatch.
393         // FIXME: New enum for this?
394         return kInvalidInput;
395     }
396 
397     // Free any memory associated with the buffer. Must be called last, so we declare it first.
398     SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output));
399 
400     WebPIterator frame;
401     SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
402     // If this succeeded in onGetFrameCount(), it should succeed again here.
403     SkAssertResult(WebPDemuxGetFrame(fDemux, index + 1, &frame));
404 
405     const bool independent = index == 0 ? true :
406             (fFrameHolder.frame(index)->getRequiredFrame() == kNoFrame);
407     // Get the frameRect.  libwebp will have already signaled an error if this is not fully
408     // contained by the canvas.
409     auto frameRect = SkIRect::MakeXYWH(frame.x_offset, frame.y_offset, frame.width, frame.height);
410     SkASSERT(this->bounds().contains(frameRect));
411     const bool frameIsSubset = frameRect != this->bounds();
412     if (independent && frameIsSubset) {
413         SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized);
414     }
415 
416     int dstX = frameRect.x();
417     int dstY = frameRect.y();
418     int subsetWidth = frameRect.width();
419     int subsetHeight = frameRect.height();
420     if (options.fSubset) {
421         SkIRect subset = *options.fSubset;
422         SkASSERT(this->bounds().contains(subset));
423         SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
424         SkASSERT(this->getValidSubset(&subset) && subset == *options.fSubset);
425 
426         if (!SkIRect::Intersects(subset, frameRect)) {
427             return kSuccess;
428         }
429 
430         int minXOffset = std::min(dstX, subset.x());
431         int minYOffset = std::min(dstY, subset.y());
432         dstX -= minXOffset;
433         dstY -= minYOffset;
434         frameRect.offset(-minXOffset, -minYOffset);
435         subset.offset(-minXOffset, -minYOffset);
436 
437         // Just like we require that the requested subset x and y offset are even, libwebp
438         // guarantees that the frame x and y offset are even (it's actually impossible to specify
439         // an odd frame offset).  So we can still guarantee that the adjusted offsets are even.
440         SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
441 
442         SkIRect intersection;
443         SkAssertResult(intersection.intersect(frameRect, subset));
444         subsetWidth = intersection.width();
445         subsetHeight = intersection.height();
446 
447         config.options.use_cropping = 1;
448         config.options.crop_left = subset.x();
449         config.options.crop_top = subset.y();
450         config.options.crop_width = subsetWidth;
451         config.options.crop_height = subsetHeight;
452     }
453 
454     // Ignore the frame size and offset when determining if scaling is necessary.
455     int scaledWidth = subsetWidth;
456     int scaledHeight = subsetHeight;
457     SkISize srcSize = options.fSubset ? options.fSubset->size() : this->dimensions();
458     if (srcSize != dstInfo.dimensions()) {
459         config.options.use_scaling = 1;
460 
461         if (frameIsSubset) {
462             float scaleX = ((float) dstInfo.width()) / srcSize.width();
463             float scaleY = ((float) dstInfo.height()) / srcSize.height();
464 
465             // We need to be conservative here and floor rather than round.
466             // Otherwise, we may find ourselves decoding off the end of memory.
467             dstX = scaleX * dstX;
468             scaledWidth = scaleX * scaledWidth;
469             dstY = scaleY * dstY;
470             scaledHeight = scaleY * scaledHeight;
471             if (0 == scaledWidth || 0 == scaledHeight) {
472                 return kSuccess;
473             }
474         } else {
475             scaledWidth = dstInfo.width();
476             scaledHeight = dstInfo.height();
477         }
478 
479         config.options.scaled_width = scaledWidth;
480         config.options.scaled_height = scaledHeight;
481     }
482 
483     const bool blendWithPrevFrame = !independent && frame.blend_method == WEBP_MUX_BLEND
484         && frame.has_alpha;
485 
486     auto webpInfo = dstInfo;
487     if (!frame.has_alpha) {
488         webpInfo = webpInfo.makeAlphaType(kOpaque_SkAlphaType);
489     } else if (this->colorXform() || blendWithPrevFrame) {
490         // the colorXform and blend_line expect unpremul.
491         webpInfo = webpInfo.makeAlphaType(kUnpremul_SkAlphaType);
492     }
493     if (this->colorXform()) {
494         // Swizzling between RGBA and BGRA is zero cost in a color transform.  So when we have a
495         // color transform, we should decode to whatever is easiest for libwebp, and then let the
496         // color transform swizzle if necessary.
497         // Lossy webp is encoded as YUV (so RGBA and BGRA are the same cost).  Lossless webp is
498         // encoded as BGRA. This means decoding to BGRA is either faster or the same cost as RGBA.
499         webpInfo = webpInfo.makeColorType(kBGRA_8888_SkColorType);
500     }
501 
502     SkBitmap webpDst;
503     if ((this->colorXform() && !is_8888(dstInfo.colorType())) || blendWithPrevFrame) {
504         // We will decode the entire image and then perform the color transform.  libwebp
505         // does not provide a row-by-row API.  This is a shame particularly when we do not want
506         // 8888, since we will need to create another image sized buffer.
507         webpDst.allocPixels(webpInfo);
508     } else {
509         // libwebp can decode directly into the output memory.
510         webpDst.installPixels(webpInfo, dst, rowBytes);
511     }
512 
513     config.output.colorspace = webp_decode_mode(webpInfo.colorType(),
514             webpInfo.alphaType() == kPremul_SkAlphaType);
515     config.output.is_external_memory = 1;
516 
517     config.output.u.RGBA.rgba = reinterpret_cast<uint8_t*>(webpDst.getAddr(dstX, dstY));
518     config.output.u.RGBA.stride = static_cast<int>(webpDst.rowBytes());
519     config.output.u.RGBA.size = webpDst.computeByteSize();
520 
521     SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
522     if (!idec) {
523         return kInvalidInput;
524     }
525 
526     int rowsDecoded = 0;
527     SkCodec::Result result;
528     switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) {
529         case VP8_STATUS_OK:
530             rowsDecoded = scaledHeight;
531             result = kSuccess;
532             break;
533         case VP8_STATUS_SUSPENDED:
534             if (!WebPIDecGetRGB(idec, &rowsDecoded, nullptr, nullptr, nullptr)
535                     || rowsDecoded <= 0) {
536                 return kInvalidInput;
537             }
538             *rowsDecodedPtr = rowsDecoded + dstY;
539             result = kIncompleteInput;
540             break;
541         default:
542             return kInvalidInput;
543     }
544 
545     const size_t dstBpp = dstInfo.bytesPerPixel();
546     dst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY);
547     const size_t srcRowBytes = config.output.u.RGBA.stride;
548 
549     const auto dstCT = dstInfo.colorType();
550     if (this->colorXform()) {
551         uint32_t* xformSrc = (uint32_t*) config.output.u.RGBA.rgba;
552         SkBitmap tmp;
553         void* xformDst;
554 
555         if (blendWithPrevFrame) {
556             // Xform into temporary bitmap big enough for one row.
557             tmp.allocPixels(dstInfo.makeWH(scaledWidth, 1));
558             xformDst = tmp.getPixels();
559         } else {
560             xformDst = dst;
561         }
562 
563         std::optional<RPBlender> blender;
564         if (blendWithPrevFrame) {
565             blender.emplace(dstCT, dstCT, dstInfo.alphaType(), frame.has_alpha);
566         }
567 
568         for (int y = 0; y < rowsDecoded; y++) {
569             this->applyColorXform(xformDst, xformSrc, scaledWidth);
570             if (blendWithPrevFrame) {
571                 blender->blendLine(dst, xformDst, scaledWidth);
572                 dst = SkTAddOffset<void>(dst, rowBytes);
573             } else {
574                 xformDst = SkTAddOffset<void>(xformDst, rowBytes);
575             }
576             xformSrc = SkTAddOffset<uint32_t>(xformSrc, srcRowBytes);
577         }
578     } else if (blendWithPrevFrame) {
579         RPBlender blender(dstCT, webpDst.colorType(), dstInfo.alphaType(), frame.has_alpha);
580 
581         const uint8_t* src = config.output.u.RGBA.rgba;
582 
583         for (int y = 0; y < rowsDecoded; y++) {
584             blender.blendLine(dst, src, scaledWidth);
585             src = SkTAddOffset<const uint8_t>(src, srcRowBytes);
586             dst = SkTAddOffset<void>(dst, rowBytes);
587         }
588     }
589 
590     return result;
591 }
592 
SkWebpCodec(SkEncodedInfo && info,std::unique_ptr<SkStream> stream,WebPDemuxer * demux,sk_sp<SkData> data,SkEncodedOrigin origin)593 SkWebpCodec::SkWebpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
594                          WebPDemuxer* demux, sk_sp<SkData> data, SkEncodedOrigin origin)
595     : INHERITED(std::move(info), skcms_PixelFormat_BGRA_8888, std::move(stream),
596                 origin)
597     , fDemux(demux)
598     , fData(std::move(data))
599     , fFailed(false)
600 {
601     const auto& eInfo = this->getEncodedInfo();
602     fFrameHolder.setScreenSize(eInfo.width(), eInfo.height());
603 }
604 
605 namespace SkWebpDecoder {
IsWebp(const void * data,size_t len)606 bool IsWebp(const void* data, size_t len) {
607     return SkWebpCodec::IsWebp(data, len);
608 }
609 
Decode(std::unique_ptr<SkStream> stream,SkCodec::Result * outResult,SkCodecs::DecodeContext)610 std::unique_ptr<SkCodec> Decode(std::unique_ptr<SkStream> stream,
611                                 SkCodec::Result* outResult,
612                                 SkCodecs::DecodeContext) {
613     SkCodec::Result resultStorage;
614     if (!outResult) {
615         outResult = &resultStorage;
616     }
617     return SkWebpCodec::MakeFromStream(std::move(stream), outResult);
618 }
619 
Decode(sk_sp<SkData> data,SkCodec::Result * outResult,SkCodecs::DecodeContext)620 std::unique_ptr<SkCodec> Decode(sk_sp<SkData> data,
621                                 SkCodec::Result* outResult,
622                                 SkCodecs::DecodeContext) {
623     if (!data) {
624         if (outResult) {
625             *outResult = SkCodec::kInvalidInput;
626         }
627         return nullptr;
628     }
629     return Decode(SkMemoryStream::Make(std::move(data)), outResult, nullptr);
630 }
631 }  // namespace SkWebpDecoder
632