blob: f702a3ae983916740f4e7b1c92edcd408999e823 [file] [log] [blame]
scroggo6f5e6192015-06-18 12:53:43 -07001/*
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
Leon Scroggins III557fbbe2017-05-23 09:37:21 -04008#include "SkBitmap.h"
9#include "SkCanvas.h"
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040010#include "SkCodecAnimation.h"
11#include "SkCodecAnimationPriv.h"
scroggocc2feb12015-08-14 08:32:46 -070012#include "SkCodecPriv.h"
msarette99883f2016-09-08 06:05:35 -070013#include "SkColorSpaceXform.h"
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040014#include "SkRasterPipeline.h"
Matt Sarett5c496172017-02-07 17:01:16 -050015#include "SkSampler.h"
msarettff2a6c82016-09-07 11:23:28 -070016#include "SkStreamPriv.h"
scroggo6f5e6192015-06-18 12:53:43 -070017#include "SkTemplates.h"
Matt Sarett5c496172017-02-07 17:01:16 -050018#include "SkWebpCodec.h"
scroggo6f5e6192015-06-18 12:53:43 -070019
20// A WebP decoder on top of (subset of) libwebp
21// For more information on WebP image format, and libwebp library, see:
22// https://code.google.com/speed/webp/
23// http://www.webmproject.org/code/#libwebp-webp-image-library
24// https://chromium.googlesource.com/webm/libwebp
25
26// If moving libwebp out of skia source tree, path for webp headers must be
27// updated accordingly. Here, we enforce using local copy in webp sub-directory.
28#include "webp/decode.h"
msarett9d15dab2016-08-24 07:36:06 -070029#include "webp/demux.h"
scroggo6f5e6192015-06-18 12:53:43 -070030#include "webp/encode.h"
31
scroggodb30be22015-12-08 18:54:13 -080032bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
scroggo6f5e6192015-06-18 12:53:43 -070033 // WEBP starts with the following:
34 // RIFFXXXXWEBPVP
35 // Where XXXX is unspecified.
scroggodb30be22015-12-08 18:54:13 -080036 const char* bytes = static_cast<const char*>(buf);
37 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
scroggo6f5e6192015-06-18 12:53:43 -070038}
39
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040040static SkAlphaType alpha_type(bool hasAlpha) {
41 return hasAlpha ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
42}
43
scroggo6f5e6192015-06-18 12:53:43 -070044// Parse headers of RIFF container, and check for valid Webp (VP8) content.
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040045// Returns an SkWebpCodec on success
msarettac6c7502016-04-25 09:30:24 -070046SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040047 std::unique_ptr<SkStream> streamDeleter(stream);
msarettac6c7502016-04-25 09:30:24 -070048
msarettff2a6c82016-09-07 11:23:28 -070049 // Webp demux needs a contiguous data buffer.
50 sk_sp<SkData> data = nullptr;
51 if (stream->getMemoryBase()) {
52 // It is safe to make without copy because we'll hold onto the stream.
53 data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength());
54 } else {
55 data = SkCopyStreamToData(stream);
scroggodb30be22015-12-08 18:54:13 -080056
msarettff2a6c82016-09-07 11:23:28 -070057 // If we are forced to copy the stream to a data, we can go ahead and delete the stream.
58 streamDeleter.reset(nullptr);
59 }
60
61 // It's a little strange that the |demux| will outlive |webpData|, though it needs the
62 // pointer in |webpData| to remain valid. This works because the pointer remains valid
63 // until the SkData is freed.
64 WebPData webpData = { data->bytes(), data->size() };
65 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> demux(WebPDemuxPartial(&webpData, nullptr));
66 if (nullptr == demux) {
msarettac6c7502016-04-25 09:30:24 -070067 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070068 }
69
Matt Sarett5c496172017-02-07 17:01:16 -050070 const int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
71 const int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
72
73 // Sanity check for image size that's about to be decoded.
74 {
75 const int64_t size = sk_64_mul(width, height);
76 if (!sk_64_isS32(size)) {
77 return nullptr;
78 }
79 // now check that if we are 4-bytes per pixel, we also don't overflow
80 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
81 return nullptr;
82 }
83 }
84
msarettff2a6c82016-09-07 11:23:28 -070085 WebPChunkIterator chunkIterator;
86 SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
87 sk_sp<SkColorSpace> colorSpace = nullptr;
raftiasd737bee2016-12-08 10:53:24 -050088 bool unsupportedICC = false;
msarettff2a6c82016-09-07 11:23:28 -070089 if (WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) {
Brian Osman526972e2016-10-24 09:24:02 -040090 colorSpace = SkColorSpace::MakeICC(chunkIterator.chunk.bytes, chunkIterator.chunk.size);
raftiasd737bee2016-12-08 10:53:24 -050091 if (!colorSpace) {
92 unsupportedICC = true;
93 }
scroggo6f5e6192015-06-18 12:53:43 -070094 }
msarettff2a6c82016-09-07 11:23:28 -070095 if (!colorSpace) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -050096 colorSpace = SkColorSpace::MakeSRGB();
msarettff2a6c82016-09-07 11:23:28 -070097 }
98
Matt Sarett5c496172017-02-07 17:01:16 -050099 // Get the first frame and its "features" to determine the color and alpha types.
msarettff2a6c82016-09-07 11:23:28 -0700100 WebPIterator frame;
101 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
102 if (!WebPDemuxGetFrame(demux, 1, &frame)) {
103 return nullptr;
104 }
105
msarettff2a6c82016-09-07 11:23:28 -0700106 WebPBitstreamFeatures features;
107 VP8StatusCode status = WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features);
108 if (VP8_STATUS_OK != status) {
109 return nullptr;
110 }
111
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400112 const bool hasAlpha = SkToBool(frame.has_alpha)
113 || frame.width != width || frame.height != height;
msarettac6c7502016-04-25 09:30:24 -0700114 SkEncodedInfo::Color color;
115 SkEncodedInfo::Alpha alpha;
116 switch (features.format) {
117 case 0:
Matt Sarett5c496172017-02-07 17:01:16 -0500118 // This indicates a "mixed" format. We could see this for
119 // animated webps (multiple fragments).
msarettac6c7502016-04-25 09:30:24 -0700120 // We could also guess kYUV here, but I think it makes more
121 // sense to guess kBGRA which is likely closer to the final
122 // output. Otherwise, we might end up converting
123 // BGRA->YUVA->BGRA.
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400124 // Fallthrough:
125 case 2:
126 // This is the lossless format (BGRA).
127 if (hasAlpha) {
128 color = SkEncodedInfo::kBGRA_Color;
129 alpha = SkEncodedInfo::kUnpremul_Alpha;
130 } else {
131 color = SkEncodedInfo::kBGRX_Color;
132 alpha = SkEncodedInfo::kOpaque_Alpha;
133 }
msarettac6c7502016-04-25 09:30:24 -0700134 break;
135 case 1:
136 // This is the lossy format (YUV).
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400137 if (hasAlpha) {
msarettac6c7502016-04-25 09:30:24 -0700138 color = SkEncodedInfo::kYUVA_Color;
msarettc30c4182016-04-20 11:53:35 -0700139 alpha = SkEncodedInfo::kUnpremul_Alpha;
msarettac6c7502016-04-25 09:30:24 -0700140 } else {
141 color = SkEncodedInfo::kYUV_Color;
142 alpha = SkEncodedInfo::kOpaque_Alpha;
143 }
144 break;
msarettac6c7502016-04-25 09:30:24 -0700145 default:
146 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -0700147 }
scroggo6f5e6192015-06-18 12:53:43 -0700148
msarettac6c7502016-04-25 09:30:24 -0700149 SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
Matt Sarett5c496172017-02-07 17:01:16 -0500150 SkWebpCodec* codecOut = new SkWebpCodec(width, height, info, std::move(colorSpace),
151 streamDeleter.release(), demux.release(),
152 std::move(data));
raftiasd737bee2016-12-08 10:53:24 -0500153 codecOut->setUnsupportedICC(unsupportedICC);
154 return codecOut;
scroggo6f5e6192015-06-18 12:53:43 -0700155}
156
scroggo6f5e6192015-06-18 12:53:43 -0700157SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const {
158 SkISize dim = this->getInfo().dimensions();
msaretta0c414d2015-06-19 07:34:30 -0700159 // SkCodec treats zero dimensional images as errors, so the minimum size
160 // that we will recommend is 1x1.
161 dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth));
162 dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight));
scroggo6f5e6192015-06-18 12:53:43 -0700163 return dim;
164}
165
scroggoe7fc14b2015-10-02 13:14:46 -0700166bool SkWebpCodec::onDimensionsSupported(const SkISize& dim) {
167 const SkImageInfo& info = this->getInfo();
168 return dim.width() >= 1 && dim.width() <= info.width()
169 && dim.height() >= 1 && dim.height() <= info.height();
170}
171
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400172static WEBP_CSP_MODE webp_decode_mode(const SkImageInfo& info) {
173 const bool premultiply = info.alphaType() == kPremul_SkAlphaType;
174 switch (info.colorType()) {
scroggo6f5e6192015-06-18 12:53:43 -0700175 case kBGRA_8888_SkColorType:
176 return premultiply ? MODE_bgrA : MODE_BGRA;
177 case kRGBA_8888_SkColorType:
178 return premultiply ? MODE_rgbA : MODE_RGBA;
scroggo74992b52015-08-06 13:50:15 -0700179 case kRGB_565_SkColorType:
180 return MODE_RGB_565;
scroggo6f5e6192015-06-18 12:53:43 -0700181 default:
182 return MODE_LAST;
183 }
184}
185
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400186SkWebpCodec::Frame* SkWebpCodec::FrameHolder::appendNewFrame(bool hasAlpha) {
187 const int i = this->size();
188 fFrames.emplace_back(i, hasAlpha);
189 return &fFrames[i];
190}
191
scroggob636b452015-07-22 07:16:20 -0700192bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
193 if (!desiredSubset) {
194 return false;
195 }
196
msarettfdb47572015-10-13 12:50:14 -0700197 SkIRect dimensions = SkIRect::MakeSize(this->getInfo().dimensions());
198 if (!dimensions.contains(*desiredSubset)) {
scroggob636b452015-07-22 07:16:20 -0700199 return false;
200 }
201
202 // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we
203 // decode this exact subset.
204 // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested.
205 desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1;
206 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1;
207 return true;
208}
209
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400210int SkWebpCodec::onGetRepetitionCount() {
211 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
212 if (!(flags & ANIMATION_FLAG)) {
213 return 0;
214 }
215
216 const int repCount = WebPDemuxGetI(fDemux.get(), WEBP_FF_LOOP_COUNT);
217 if (0 == repCount) {
218 return kRepetitionCountInfinite;
219 }
220
221 return repCount;
222}
223
224int SkWebpCodec::onGetFrameCount() {
225 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
226 if (!(flags & ANIMATION_FLAG)) {
227 return 1;
228 }
229
230 const uint32_t oldFrameCount = fFrameHolder.size();
231 if (fFailed) {
232 return oldFrameCount;
233 }
234
235 const uint32_t frameCount = WebPDemuxGetI(fDemux, WEBP_FF_FRAME_COUNT);
236 if (oldFrameCount == frameCount) {
237 // We have already parsed this.
238 return frameCount;
239 }
240
241 fFrameHolder.reserve(frameCount);
242
243 for (uint32_t i = oldFrameCount; i < frameCount; i++) {
244 WebPIterator iter;
245 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoIter(&iter);
246
247 if (!WebPDemuxGetFrame(fDemux.get(), i + 1, &iter)) {
248 fFailed = true;
249 break;
250 }
251
252 // libwebp only reports complete frames of an animated image.
253 SkASSERT(iter.complete);
254
255 Frame* frame = fFrameHolder.appendNewFrame(iter.has_alpha);
256 frame->setXYWH(iter.x_offset, iter.y_offset, iter.width, iter.height);
257 frame->setDisposalMethod(iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ?
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400258 SkCodecAnimation::DisposalMethod::kRestoreBGColor :
259 SkCodecAnimation::DisposalMethod::kKeep);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400260 frame->setDuration(iter.duration);
261 if (WEBP_MUX_BLEND != iter.blend_method) {
262 frame->setBlend(SkCodecAnimation::Blend::kBG);
263 }
264 fFrameHolder.setAlphaAndRequiredFrame(frame);
265 }
266
267 return fFrameHolder.size();
268
269}
270
271const SkFrame* SkWebpCodec::FrameHolder::onGetFrame(int i) const {
272 return static_cast<const SkFrame*>(this->frame(i));
273}
274
275const SkWebpCodec::Frame* SkWebpCodec::FrameHolder::frame(int i) const {
276 SkASSERT(i >= 0 && i < this->size());
277 return &fFrames[i];
278}
279
280bool SkWebpCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const {
281 if (i >= fFrameHolder.size()) {
282 return false;
283 }
284
285 const Frame* frame = fFrameHolder.frame(i);
286 if (!frame) {
287 return false;
288 }
289
290 if (frameInfo) {
291 frameInfo->fRequiredFrame = frame->getRequiredFrame();
292 frameInfo->fDuration = frame->getDuration();
293 // libwebp only reports fully received frames for an
294 // animated image.
295 frameInfo->fFullyReceived = true;
296 frameInfo->fAlphaType = alpha_type(frame->hasAlpha());
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400297 frameInfo->fDisposalMethod = frame->getDisposalMethod();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400298 }
299
300 return true;
301}
302
303static bool is_8888(SkColorType colorType) {
304 switch (colorType) {
305 case kRGBA_8888_SkColorType:
306 case kBGRA_8888_SkColorType:
307 return true;
308 default:
309 return false;
310 }
311}
312
313static void pick_memory_stages(SkColorType ct, SkRasterPipeline::StockStage* load,
314 SkRasterPipeline::StockStage* store) {
315 switch(ct) {
316 case kUnknown_SkColorType:
317 case kAlpha_8_SkColorType:
318 case kARGB_4444_SkColorType:
319 case kIndex_8_SkColorType:
320 case kGray_8_SkColorType:
321 SkASSERT(false);
322 break;
323 case kRGB_565_SkColorType:
324 if (load) *load = SkRasterPipeline::load_565;
325 if (store) *store = SkRasterPipeline::store_565;
326 break;
327 case kRGBA_8888_SkColorType:
328 case kBGRA_8888_SkColorType:
329 if (load) *load = SkRasterPipeline::load_8888;
330 if (store) *store = SkRasterPipeline::store_8888;
331 break;
332 case kRGBA_F16_SkColorType:
333 if (load) *load = SkRasterPipeline::load_f16;
334 if (store) *store = SkRasterPipeline::store_f16;
335 break;
336 }
337}
338
339static void blend_line(SkColorType dstCT, void* dst,
340 SkColorType srcCT, void* src,
341 bool needsSrgbToLinear, SkAlphaType at,
342 int width) {
343 // Setup conversion from the source and dest, which will be the same.
Mike Kleinb24704d2017-05-24 07:53:00 -0400344 SkRasterPipeline_<256> convert_to_linear_premul;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400345 if (needsSrgbToLinear) {
346 convert_to_linear_premul.append_from_srgb(at);
347 }
348 if (kUnpremul_SkAlphaType == at) {
349 // srcover assumes premultiplied inputs.
350 convert_to_linear_premul.append(SkRasterPipeline::premul);
351 }
352
Mike Kleinb24704d2017-05-24 07:53:00 -0400353 SkRasterPipeline_<256> p;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400354 SkRasterPipeline::StockStage load_dst, store_dst;
355 pick_memory_stages(dstCT, &load_dst, &store_dst);
356
357 // Load the final dst.
358 p.append(load_dst, dst);
359 p.extend(convert_to_linear_premul);
360 p.append(SkRasterPipeline::move_src_dst);
361
362 // Load the src.
363 SkRasterPipeline::StockStage load_src;
364 pick_memory_stages(srcCT, &load_src, nullptr);
365 p.append(load_src, src);
366 if (dstCT != srcCT) {
367 SkASSERT(kBGRA_8888_SkColorType == srcCT);
368 p.append(SkRasterPipeline::swap_rb);
369 }
370 p.extend(convert_to_linear_premul);
371
372 p.append(SkRasterPipeline::srcover);
373
374 // Convert back to dst.
375 if (kUnpremul_SkAlphaType == at) {
376 p.append(SkRasterPipeline::unpremul);
377 }
378 if (needsSrgbToLinear) {
379 p.append(SkRasterPipeline::to_srgb);
380 }
381 p.append(store_dst, dst);
382
Mike Klein761d27c2017-06-01 12:37:08 -0400383 p.run(0,0, width);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400384}
385
scroggoeb602a52015-07-09 08:16:03 -0700386SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
msarette6dd0042015-10-09 11:07:34 -0700387 const Options& options, SkPMColor*, int*,
msarette99883f2016-09-08 06:05:35 -0700388 int* rowsDecodedPtr) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400389 // Ensure that we have parsed this far.
390 const int index = options.fFrameIndex;
391 if (index >= this->onGetFrameCount()) {
392 return kIncompleteInput;
393 }
394
395 const auto& srcInfo = this->getInfo();
Matt Sarettcf3f2342017-03-23 15:32:25 -0400396 {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400397 auto info = srcInfo;
398 if (index > 0) {
399 auto alphaType = alpha_type(fFrameHolder.frame(index)->hasAlpha());
400 info = info.makeAlphaType(alphaType);
401 }
402 if (!conversion_possible(dstInfo, info) ||
403 !this->initializeColorXform(dstInfo, options.fPremulBehavior))
404 {
405 return kInvalidConversion;
406 }
407 }
408
409 if (index > 0 && (options.fSubset || dstInfo.dimensions() != srcInfo.dimensions())) {
410 // Subsetting and scaling are tricky when asking for frames beyond frame 0. In order to
411 // support it, we'll need to determine the proper rectangle for a
412 // WEBP_MUX_DISPOSE_BACKGROUND required frame before erasing it. (Currently the order
413 // is backwards.) Disable until it becomes clear that supporting it is important.
414 return kUnimplemented;
scroggo6f5e6192015-06-18 12:53:43 -0700415 }
416
417 WebPDecoderConfig config;
418 if (0 == WebPInitDecoderConfig(&config)) {
419 // ABI mismatch.
420 // FIXME: New enum for this?
421 return kInvalidInput;
422 }
423
424 // Free any memory associated with the buffer. Must be called last, so we declare it first.
425 SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output));
426
Matt Sarett5c496172017-02-07 17:01:16 -0500427 WebPIterator frame;
428 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400429 // If this succeeded in onGetFrameCount(), it should succeed again here.
430 SkAssertResult(WebPDemuxGetFrame(fDemux, index + 1, &frame));
Matt Sarett604971e2017-02-06 09:51:48 -0500431
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400432 const int requiredFrame = index == 0 ? kNone : fFrameHolder.frame(index)->getRequiredFrame();
Matt Sarett5c496172017-02-07 17:01:16 -0500433 // Get the frameRect. libwebp will have already signaled an error if this is not fully
434 // contained by the canvas.
435 auto frameRect = SkIRect::MakeXYWH(frame.x_offset, frame.y_offset, frame.width, frame.height);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400436 SkASSERT(srcInfo.bounds().contains(frameRect));
437 const bool frameIsSubset = frameRect != srcInfo.bounds();
438 if (kNone == requiredFrame) {
439 if (frameIsSubset) {
440 SkSampler::Fill(dstInfo, dst, rowBytes, 0, options.fZeroInitialized);
441 }
442 } else {
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400443 // FIXME: Share with GIF
444 if (options.fPriorFrame != kNone) {
445 if (options.fPriorFrame < requiredFrame || options.fPriorFrame >= index) {
446 return kInvalidParameters;
447 }
448 } else {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400449 Options prevFrameOpts(options);
450 prevFrameOpts.fFrameIndex = requiredFrame;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400451 prevFrameOpts.fPriorFrame = kNone;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400452 const auto result = this->getPixels(dstInfo, dst, rowBytes, &prevFrameOpts,
453 nullptr, nullptr);
454 switch (result) {
455 case kSuccess:
456 break;
457 case kIncompleteInput:
458 return kInvalidInput;
459 default:
460 return result;
461 }
462 }
463
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400464 if (options.fPriorFrame == requiredFrame || options.fPriorFrame == kNone) {
465 // Dispose bg color
466 const Frame* priorFrame = fFrameHolder.frame(requiredFrame);
467 if (priorFrame->getDisposalMethod()
468 == SkCodecAnimation::DisposalMethod::kRestoreBGColor) {
469 // FIXME: If we add support for scaling/subsets, this rectangle needs to be
470 // adjusted.
471 const auto priorRect = priorFrame->frameRect();
472 const auto info = dstInfo.makeWH(priorRect.width(), priorRect.height());
473 const size_t bpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
474 const size_t offset = priorRect.x() * bpp + priorRect.y() * rowBytes;
475 auto* eraseDst = SkTAddOffset<void>(dst, offset);
476 SkSampler::Fill(info, eraseDst, rowBytes, 0, kNo_ZeroInitialized);
477 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400478 }
Matt Sarett604971e2017-02-06 09:51:48 -0500479 }
480
Matt Sarett5c496172017-02-07 17:01:16 -0500481 int dstX = frameRect.x();
482 int dstY = frameRect.y();
483 int subsetWidth = frameRect.width();
484 int subsetHeight = frameRect.height();
485 if (options.fSubset) {
486 SkIRect subset = *options.fSubset;
487 SkASSERT(this->getInfo().bounds().contains(subset));
488 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
489 SkASSERT(this->getValidSubset(&subset) && subset == *options.fSubset);
490
491 if (!SkIRect::IntersectsNoEmptyCheck(subset, frameRect)) {
492 return kSuccess;
493 }
494
495 int minXOffset = SkTMin(dstX, subset.x());
496 int minYOffset = SkTMin(dstY, subset.y());
497 dstX -= minXOffset;
498 dstY -= minYOffset;
499 frameRect.offset(-minXOffset, -minYOffset);
500 subset.offset(-minXOffset, -minYOffset);
501
502 // Just like we require that the requested subset x and y offset are even, libwebp
503 // guarantees that the frame x and y offset are even (it's actually impossible to specify
504 // an odd frame offset). So we can still guarantee that the adjusted offsets are even.
505 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
506
507 SkIRect intersection;
508 SkAssertResult(intersection.intersect(frameRect, subset));
509 subsetWidth = intersection.width();
510 subsetHeight = intersection.height();
511
512 config.options.use_cropping = 1;
513 config.options.crop_left = subset.x();
514 config.options.crop_top = subset.y();
515 config.options.crop_width = subsetWidth;
516 config.options.crop_height = subsetHeight;
517 }
518
519 // Ignore the frame size and offset when determining if scaling is necessary.
520 int scaledWidth = subsetWidth;
521 int scaledHeight = subsetHeight;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400522 SkISize srcSize = options.fSubset ? options.fSubset->size() : srcInfo.dimensions();
Matt Sarett5c496172017-02-07 17:01:16 -0500523 if (srcSize != dstInfo.dimensions()) {
scroggo6f5e6192015-06-18 12:53:43 -0700524 config.options.use_scaling = 1;
Matt Sarett5c496172017-02-07 17:01:16 -0500525
526 if (frameIsSubset) {
527 float scaleX = ((float) dstInfo.width()) / srcSize.width();
528 float scaleY = ((float) dstInfo.height()) / srcSize.height();
529
530 // We need to be conservative here and floor rather than round.
531 // Otherwise, we may find ourselves decoding off the end of memory.
532 dstX = scaleX * dstX;
533 scaledWidth = scaleX * scaledWidth;
534 dstY = scaleY * dstY;
535 scaledHeight = scaleY * scaledHeight;
536 if (0 == scaledWidth || 0 == scaledHeight) {
537 return kSuccess;
538 }
539 } else {
540 scaledWidth = dstInfo.width();
541 scaledHeight = dstInfo.height();
542 }
543
544 config.options.scaled_width = scaledWidth;
545 config.options.scaled_height = scaledHeight;
scroggo6f5e6192015-06-18 12:53:43 -0700546 }
547
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400548 const bool blendWithPrevFrame = requiredFrame != kNone && frame.blend_method == WEBP_MUX_BLEND
549 && frame.has_alpha;
550 if (blendWithPrevFrame && options.fPremulBehavior == SkTransferFunctionBehavior::kRespect) {
551 // Blending is done with SkRasterPipeline, which requires a color space that is valid for
552 // rendering.
553 const auto* cs = dstInfo.colorSpace();
554 if (!cs || (!cs->gammaCloseToSRGB() && !cs->gammaIsLinear())) {
555 return kInvalidConversion;
556 }
557 }
558
559 SkBitmap webpDst;
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400560 auto webpInfo = dstInfo;
561 if (!frame.has_alpha) {
562 webpInfo = webpInfo.makeAlphaType(kOpaque_SkAlphaType);
563 }
564 if (this->colorXform()) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400565 // Swizzling between RGBA and BGRA is zero cost in a color transform. So when we have a
566 // color transform, we should decode to whatever is easiest for libwebp, and then let the
567 // color transform swizzle if necessary.
568 // Lossy webp is encoded as YUV (so RGBA and BGRA are the same cost). Lossless webp is
569 // encoded as BGRA. This means decoding to BGRA is either faster or the same cost as RGBA.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400570 webpInfo = webpInfo.makeColorType(kBGRA_8888_SkColorType);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400571
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400572 if (webpInfo.alphaType() == kPremul_SkAlphaType) {
573 webpInfo = webpInfo.makeAlphaType(kUnpremul_SkAlphaType);
574 }
575 }
576
577 if ((this->colorXform() && !is_8888(dstInfo.colorType())) || blendWithPrevFrame) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400578 // We will decode the entire image and then perform the color transform. libwebp
579 // does not provide a row-by-row API. This is a shame particularly when we do not want
580 // 8888, since we will need to create another image sized buffer.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400581 webpDst.allocPixels(webpInfo);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400582 } else {
583 // libwebp can decode directly into the output memory.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400584 webpDst.installPixels(webpInfo, dst, rowBytes);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400585 }
586
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400587 config.output.colorspace = webp_decode_mode(webpInfo);
scroggo6f5e6192015-06-18 12:53:43 -0700588 config.output.is_external_memory = 1;
589
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400590 config.output.u.RGBA.rgba = reinterpret_cast<uint8_t*>(webpDst.getAddr(dstX, dstY));
591 config.output.u.RGBA.stride = static_cast<int>(webpDst.rowBytes());
592 config.output.u.RGBA.size = webpDst.getSafeSize();
msarettff2a6c82016-09-07 11:23:28 -0700593
halcanary96fcdcc2015-08-27 07:41:13 -0700594 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
scroggo6f5e6192015-06-18 12:53:43 -0700595 if (!idec) {
596 return kInvalidInput;
597 }
598
msarette99883f2016-09-08 06:05:35 -0700599 int rowsDecoded;
600 SkCodec::Result result;
msarettff2a6c82016-09-07 11:23:28 -0700601 switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) {
602 case VP8_STATUS_OK:
Matt Sarett5c496172017-02-07 17:01:16 -0500603 rowsDecoded = scaledHeight;
msarette99883f2016-09-08 06:05:35 -0700604 result = kSuccess;
605 break;
msarettff2a6c82016-09-07 11:23:28 -0700606 case VP8_STATUS_SUSPENDED:
Matt Sarett5c496172017-02-07 17:01:16 -0500607 WebPIDecGetRGB(idec, &rowsDecoded, nullptr, nullptr, nullptr);
608 *rowsDecodedPtr = rowsDecoded + dstY;
msarette99883f2016-09-08 06:05:35 -0700609 result = kIncompleteInput;
610 break;
msarettff2a6c82016-09-07 11:23:28 -0700611 default:
612 return kInvalidInput;
scroggo6f5e6192015-06-18 12:53:43 -0700613 }
msarette99883f2016-09-08 06:05:35 -0700614
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400615 // We're only transforming the new part of the frame, so no need to worry about the
616 // final composited alpha.
617 const auto srcAlpha = 0 == index ? srcInfo.alphaType() : alpha_type(frame.has_alpha);
618 const auto xformAlphaType = select_xform_alpha(dstInfo.alphaType(), srcAlpha);
619 const bool needsSrgbToLinear = dstInfo.gammaCloseToSRGB() &&
620 options.fPremulBehavior == SkTransferFunctionBehavior::kRespect;
621
622 const size_t dstBpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
623 dst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY);
624 const size_t srcRowBytes = config.output.u.RGBA.stride;
625
626 const auto dstCT = dstInfo.colorType();
Matt Sarett313c4632016-10-20 12:35:23 -0400627 if (this->colorXform()) {
Matt Sarett5c496172017-02-07 17:01:16 -0500628 uint32_t* xformSrc = (uint32_t*) config.output.u.RGBA.rgba;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400629 SkBitmap tmp;
630 void* xformDst;
631
632 if (blendWithPrevFrame) {
633 // Xform into temporary bitmap big enough for one row.
634 tmp.allocPixels(dstInfo.makeWH(scaledWidth, 1));
635 xformDst = tmp.getPixels();
636 } else {
637 xformDst = dst;
638 }
Robert Phillipsb3050b92017-02-06 13:12:18 +0000639 for (int y = 0; y < rowsDecoded; y++) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400640 this->applyColorXform(xformDst, xformSrc, scaledWidth, xformAlphaType);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400641 if (blendWithPrevFrame) {
642 blend_line(dstCT, &dst, dstCT, &xformDst, needsSrgbToLinear, xformAlphaType,
643 scaledWidth);
644 dst = SkTAddOffset<void>(dst, rowBytes);
645 } else {
646 xformDst = SkTAddOffset<void>(xformDst, rowBytes);
647 }
Matt Sarett5c496172017-02-07 17:01:16 -0500648 xformSrc = SkTAddOffset<uint32_t>(xformSrc, srcRowBytes);
msarette99883f2016-09-08 06:05:35 -0700649 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400650 } else if (blendWithPrevFrame) {
651 const uint8_t* src = config.output.u.RGBA.rgba;
652
653 for (int y = 0; y < rowsDecoded; y++) {
654 blend_line(dstCT, &dst, webpDst.colorType(), &src, needsSrgbToLinear,
655 xformAlphaType, scaledWidth);
656 src = SkTAddOffset<const uint8_t>(src, srcRowBytes);
657 dst = SkTAddOffset<void>(dst, rowBytes);
658 }
msarette99883f2016-09-08 06:05:35 -0700659 }
660
661 return result;
scroggo6f5e6192015-06-18 12:53:43 -0700662}
663
msarett9d15dab2016-08-24 07:36:06 -0700664SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info,
msarettff2a6c82016-09-07 11:23:28 -0700665 sk_sp<SkColorSpace> colorSpace, SkStream* stream, WebPDemuxer* demux,
666 sk_sp<SkData> data)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400667 : INHERITED(width, height, info, SkColorSpaceXform::kBGRA_8888_ColorFormat, stream,
668 std::move(colorSpace))
msarettff2a6c82016-09-07 11:23:28 -0700669 , fDemux(demux)
670 , fData(std::move(data))
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400671 , fFailed(false)
672{
673 fFrameHolder.setScreenSize(width, height);
674}