blob: 02e6ff2bf9537b6b847c0fdb5017d3a1924a0cd4 [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"
Mike Klein45c16fa2017-07-18 18:15:13 -040019#include "../jumper/SkJumper.h"
scroggo6f5e6192015-06-18 12:53:43 -070020
21// A WebP decoder on top of (subset of) libwebp
22// For more information on WebP image format, and libwebp library, see:
23// https://code.google.com/speed/webp/
24// http://www.webmproject.org/code/#libwebp-webp-image-library
25// https://chromium.googlesource.com/webm/libwebp
26
27// If moving libwebp out of skia source tree, path for webp headers must be
28// updated accordingly. Here, we enforce using local copy in webp sub-directory.
29#include "webp/decode.h"
msarett9d15dab2016-08-24 07:36:06 -070030#include "webp/demux.h"
scroggo6f5e6192015-06-18 12:53:43 -070031#include "webp/encode.h"
32
scroggodb30be22015-12-08 18:54:13 -080033bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
scroggo6f5e6192015-06-18 12:53:43 -070034 // WEBP starts with the following:
35 // RIFFXXXXWEBPVP
36 // Where XXXX is unspecified.
scroggodb30be22015-12-08 18:54:13 -080037 const char* bytes = static_cast<const char*>(buf);
38 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
scroggo6f5e6192015-06-18 12:53:43 -070039}
40
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040041static SkAlphaType alpha_type(bool hasAlpha) {
42 return hasAlpha ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
43}
44
scroggo6f5e6192015-06-18 12:53:43 -070045// Parse headers of RIFF container, and check for valid Webp (VP8) content.
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040046// Returns an SkWebpCodec on success
Leon Scroggins III588fb042017-07-14 16:32:31 -040047SkCodec* SkWebpCodec::NewFromStream(SkStream* stream, Result* result) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040048 std::unique_ptr<SkStream> streamDeleter(stream);
msarettac6c7502016-04-25 09:30:24 -070049
msarettff2a6c82016-09-07 11:23:28 -070050 // Webp demux needs a contiguous data buffer.
51 sk_sp<SkData> data = nullptr;
52 if (stream->getMemoryBase()) {
53 // It is safe to make without copy because we'll hold onto the stream.
54 data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength());
55 } else {
56 data = SkCopyStreamToData(stream);
scroggodb30be22015-12-08 18:54:13 -080057
msarettff2a6c82016-09-07 11:23:28 -070058 // If we are forced to copy the stream to a data, we can go ahead and delete the stream.
59 streamDeleter.reset(nullptr);
60 }
61
62 // It's a little strange that the |demux| will outlive |webpData|, though it needs the
63 // pointer in |webpData| to remain valid. This works because the pointer remains valid
64 // until the SkData is freed.
65 WebPData webpData = { data->bytes(), data->size() };
Leon Scroggins III588fb042017-07-14 16:32:31 -040066 WebPDemuxState state;
67 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> demux(WebPDemuxPartial(&webpData, &state));
68 switch (state) {
69 case WEBP_DEMUX_PARSE_ERROR:
70 *result = kInvalidInput;
71 return nullptr;
72 case WEBP_DEMUX_PARSING_HEADER:
73 *result = kIncompleteInput;
74 return nullptr;
75 case WEBP_DEMUX_PARSED_HEADER:
76 case WEBP_DEMUX_DONE:
77 SkASSERT(demux);
78 break;
scroggo6f5e6192015-06-18 12:53:43 -070079 }
80
Matt Sarett5c496172017-02-07 17:01:16 -050081 const int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
82 const int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
83
84 // Sanity check for image size that's about to be decoded.
85 {
86 const int64_t size = sk_64_mul(width, height);
Matt Sarett5c496172017-02-07 17:01:16 -050087 // now check that if we are 4-bytes per pixel, we also don't overflow
Leon Scroggins III588fb042017-07-14 16:32:31 -040088 if (!sk_64_isS32(size) || sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
89 *result = kInvalidInput;
Matt Sarett5c496172017-02-07 17:01:16 -050090 return nullptr;
91 }
92 }
93
msarettff2a6c82016-09-07 11:23:28 -070094 WebPChunkIterator chunkIterator;
95 SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
96 sk_sp<SkColorSpace> colorSpace = nullptr;
97 if (WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) {
Brian Osman526972e2016-10-24 09:24:02 -040098 colorSpace = SkColorSpace::MakeICC(chunkIterator.chunk.bytes, chunkIterator.chunk.size);
scroggo6f5e6192015-06-18 12:53:43 -070099 }
msarettff2a6c82016-09-07 11:23:28 -0700100 if (!colorSpace) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500101 colorSpace = SkColorSpace::MakeSRGB();
msarettff2a6c82016-09-07 11:23:28 -0700102 }
103
Matt Sarett5c496172017-02-07 17:01:16 -0500104 // Get the first frame and its "features" to determine the color and alpha types.
msarettff2a6c82016-09-07 11:23:28 -0700105 WebPIterator frame;
106 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
107 if (!WebPDemuxGetFrame(demux, 1, &frame)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400108 *result = kIncompleteInput;
msarettff2a6c82016-09-07 11:23:28 -0700109 return nullptr;
110 }
111
msarettff2a6c82016-09-07 11:23:28 -0700112 WebPBitstreamFeatures features;
Leon Scroggins III588fb042017-07-14 16:32:31 -0400113 switch (WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features)) {
114 case VP8_STATUS_OK:
115 break;
116 case VP8_STATUS_SUSPENDED:
117 case VP8_STATUS_NOT_ENOUGH_DATA:
118 *result = kIncompleteInput;
119 return nullptr;
120 default:
121 *result = kInvalidInput;
122 return nullptr;
msarettff2a6c82016-09-07 11:23:28 -0700123 }
124
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400125 const bool hasAlpha = SkToBool(frame.has_alpha)
126 || frame.width != width || frame.height != height;
msarettac6c7502016-04-25 09:30:24 -0700127 SkEncodedInfo::Color color;
128 SkEncodedInfo::Alpha alpha;
129 switch (features.format) {
130 case 0:
Matt Sarett5c496172017-02-07 17:01:16 -0500131 // This indicates a "mixed" format. We could see this for
132 // animated webps (multiple fragments).
msarettac6c7502016-04-25 09:30:24 -0700133 // We could also guess kYUV here, but I think it makes more
134 // sense to guess kBGRA which is likely closer to the final
135 // output. Otherwise, we might end up converting
136 // BGRA->YUVA->BGRA.
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400137 // Fallthrough:
138 case 2:
139 // This is the lossless format (BGRA).
140 if (hasAlpha) {
141 color = SkEncodedInfo::kBGRA_Color;
142 alpha = SkEncodedInfo::kUnpremul_Alpha;
143 } else {
144 color = SkEncodedInfo::kBGRX_Color;
145 alpha = SkEncodedInfo::kOpaque_Alpha;
146 }
msarettac6c7502016-04-25 09:30:24 -0700147 break;
148 case 1:
149 // This is the lossy format (YUV).
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400150 if (hasAlpha) {
msarettac6c7502016-04-25 09:30:24 -0700151 color = SkEncodedInfo::kYUVA_Color;
msarettc30c4182016-04-20 11:53:35 -0700152 alpha = SkEncodedInfo::kUnpremul_Alpha;
msarettac6c7502016-04-25 09:30:24 -0700153 } else {
154 color = SkEncodedInfo::kYUV_Color;
155 alpha = SkEncodedInfo::kOpaque_Alpha;
156 }
157 break;
msarettac6c7502016-04-25 09:30:24 -0700158 default:
Leon Scroggins III588fb042017-07-14 16:32:31 -0400159 *result = kInvalidInput;
msarettac6c7502016-04-25 09:30:24 -0700160 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -0700161 }
scroggo6f5e6192015-06-18 12:53:43 -0700162
Leon Scroggins III588fb042017-07-14 16:32:31 -0400163 *result = kSuccess;
msarettac6c7502016-04-25 09:30:24 -0700164 SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
Leon Scroggins III588fb042017-07-14 16:32:31 -0400165 return new SkWebpCodec(width, height, info, std::move(colorSpace),
166 streamDeleter.release(), demux.release(),
167 std::move(data));
scroggo6f5e6192015-06-18 12:53:43 -0700168}
169
scroggo6f5e6192015-06-18 12:53:43 -0700170SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const {
171 SkISize dim = this->getInfo().dimensions();
msaretta0c414d2015-06-19 07:34:30 -0700172 // SkCodec treats zero dimensional images as errors, so the minimum size
173 // that we will recommend is 1x1.
174 dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth));
175 dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight));
scroggo6f5e6192015-06-18 12:53:43 -0700176 return dim;
177}
178
scroggoe7fc14b2015-10-02 13:14:46 -0700179bool SkWebpCodec::onDimensionsSupported(const SkISize& dim) {
180 const SkImageInfo& info = this->getInfo();
181 return dim.width() >= 1 && dim.width() <= info.width()
182 && dim.height() >= 1 && dim.height() <= info.height();
183}
184
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400185static WEBP_CSP_MODE webp_decode_mode(const SkImageInfo& info) {
186 const bool premultiply = info.alphaType() == kPremul_SkAlphaType;
187 switch (info.colorType()) {
scroggo6f5e6192015-06-18 12:53:43 -0700188 case kBGRA_8888_SkColorType:
189 return premultiply ? MODE_bgrA : MODE_BGRA;
190 case kRGBA_8888_SkColorType:
191 return premultiply ? MODE_rgbA : MODE_RGBA;
scroggo74992b52015-08-06 13:50:15 -0700192 case kRGB_565_SkColorType:
193 return MODE_RGB_565;
scroggo6f5e6192015-06-18 12:53:43 -0700194 default:
195 return MODE_LAST;
196 }
197}
198
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400199SkWebpCodec::Frame* SkWebpCodec::FrameHolder::appendNewFrame(bool hasAlpha) {
200 const int i = this->size();
201 fFrames.emplace_back(i, hasAlpha);
202 return &fFrames[i];
203}
204
scroggob636b452015-07-22 07:16:20 -0700205bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
206 if (!desiredSubset) {
207 return false;
208 }
209
msarettfdb47572015-10-13 12:50:14 -0700210 SkIRect dimensions = SkIRect::MakeSize(this->getInfo().dimensions());
211 if (!dimensions.contains(*desiredSubset)) {
scroggob636b452015-07-22 07:16:20 -0700212 return false;
213 }
214
215 // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we
216 // decode this exact subset.
217 // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested.
218 desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1;
219 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1;
220 return true;
221}
222
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400223int SkWebpCodec::onGetRepetitionCount() {
224 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
225 if (!(flags & ANIMATION_FLAG)) {
226 return 0;
227 }
228
229 const int repCount = WebPDemuxGetI(fDemux.get(), WEBP_FF_LOOP_COUNT);
230 if (0 == repCount) {
231 return kRepetitionCountInfinite;
232 }
233
234 return repCount;
235}
236
237int SkWebpCodec::onGetFrameCount() {
238 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
239 if (!(flags & ANIMATION_FLAG)) {
240 return 1;
241 }
242
243 const uint32_t oldFrameCount = fFrameHolder.size();
244 if (fFailed) {
245 return oldFrameCount;
246 }
247
248 const uint32_t frameCount = WebPDemuxGetI(fDemux, WEBP_FF_FRAME_COUNT);
249 if (oldFrameCount == frameCount) {
250 // We have already parsed this.
251 return frameCount;
252 }
253
254 fFrameHolder.reserve(frameCount);
255
256 for (uint32_t i = oldFrameCount; i < frameCount; i++) {
257 WebPIterator iter;
258 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoIter(&iter);
259
260 if (!WebPDemuxGetFrame(fDemux.get(), i + 1, &iter)) {
261 fFailed = true;
262 break;
263 }
264
265 // libwebp only reports complete frames of an animated image.
266 SkASSERT(iter.complete);
267
268 Frame* frame = fFrameHolder.appendNewFrame(iter.has_alpha);
269 frame->setXYWH(iter.x_offset, iter.y_offset, iter.width, iter.height);
270 frame->setDisposalMethod(iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ?
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400271 SkCodecAnimation::DisposalMethod::kRestoreBGColor :
272 SkCodecAnimation::DisposalMethod::kKeep);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400273 frame->setDuration(iter.duration);
274 if (WEBP_MUX_BLEND != iter.blend_method) {
275 frame->setBlend(SkCodecAnimation::Blend::kBG);
276 }
277 fFrameHolder.setAlphaAndRequiredFrame(frame);
278 }
279
280 return fFrameHolder.size();
281
282}
283
284const SkFrame* SkWebpCodec::FrameHolder::onGetFrame(int i) const {
285 return static_cast<const SkFrame*>(this->frame(i));
286}
287
288const SkWebpCodec::Frame* SkWebpCodec::FrameHolder::frame(int i) const {
289 SkASSERT(i >= 0 && i < this->size());
290 return &fFrames[i];
291}
292
293bool SkWebpCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const {
294 if (i >= fFrameHolder.size()) {
295 return false;
296 }
297
298 const Frame* frame = fFrameHolder.frame(i);
299 if (!frame) {
300 return false;
301 }
302
303 if (frameInfo) {
304 frameInfo->fRequiredFrame = frame->getRequiredFrame();
305 frameInfo->fDuration = frame->getDuration();
306 // libwebp only reports fully received frames for an
307 // animated image.
308 frameInfo->fFullyReceived = true;
309 frameInfo->fAlphaType = alpha_type(frame->hasAlpha());
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400310 frameInfo->fDisposalMethod = frame->getDisposalMethod();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400311 }
312
313 return true;
314}
315
316static bool is_8888(SkColorType colorType) {
317 switch (colorType) {
318 case kRGBA_8888_SkColorType:
319 case kBGRA_8888_SkColorType:
320 return true;
321 default:
322 return false;
323 }
324}
325
326static void pick_memory_stages(SkColorType ct, SkRasterPipeline::StockStage* load,
327 SkRasterPipeline::StockStage* store) {
328 switch(ct) {
329 case kUnknown_SkColorType:
330 case kAlpha_8_SkColorType:
331 case kARGB_4444_SkColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400332 case kGray_8_SkColorType:
333 SkASSERT(false);
334 break;
335 case kRGB_565_SkColorType:
336 if (load) *load = SkRasterPipeline::load_565;
337 if (store) *store = SkRasterPipeline::store_565;
338 break;
339 case kRGBA_8888_SkColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400340 if (load) *load = SkRasterPipeline::load_8888;
341 if (store) *store = SkRasterPipeline::store_8888;
342 break;
Mike Kleinc2d20762017-06-27 19:53:21 -0400343 case kBGRA_8888_SkColorType:
344 if (load) *load = SkRasterPipeline::load_bgra;
345 if (store) *store = SkRasterPipeline::store_bgra;
346 break;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400347 case kRGBA_F16_SkColorType:
348 if (load) *load = SkRasterPipeline::load_f16;
349 if (store) *store = SkRasterPipeline::store_f16;
350 break;
351 }
352}
353
354static void blend_line(SkColorType dstCT, void* dst,
Mike Klein45c16fa2017-07-18 18:15:13 -0400355 SkColorType srcCT, const void* src,
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400356 bool needsSrgbToLinear, SkAlphaType at,
357 int width) {
358 // Setup conversion from the source and dest, which will be the same.
Mike Kleinb24704d2017-05-24 07:53:00 -0400359 SkRasterPipeline_<256> convert_to_linear_premul;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400360 if (needsSrgbToLinear) {
361 convert_to_linear_premul.append_from_srgb(at);
362 }
363 if (kUnpremul_SkAlphaType == at) {
364 // srcover assumes premultiplied inputs.
365 convert_to_linear_premul.append(SkRasterPipeline::premul);
366 }
367
Mike Klein45c16fa2017-07-18 18:15:13 -0400368 SkJumper_MemoryCtx dst_ctx = { (void*)dst, 0 },
369 src_ctx = { (void*)src, 0 };
370
Mike Kleinb24704d2017-05-24 07:53:00 -0400371 SkRasterPipeline_<256> p;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400372 SkRasterPipeline::StockStage load_dst, store_dst;
373 pick_memory_stages(dstCT, &load_dst, &store_dst);
374
375 // Load the final dst.
Mike Klein45c16fa2017-07-18 18:15:13 -0400376 p.append(load_dst, &dst_ctx);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400377 p.extend(convert_to_linear_premul);
378 p.append(SkRasterPipeline::move_src_dst);
379
380 // Load the src.
381 SkRasterPipeline::StockStage load_src;
382 pick_memory_stages(srcCT, &load_src, nullptr);
Mike Klein45c16fa2017-07-18 18:15:13 -0400383 p.append(load_src, &src_ctx);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400384 p.extend(convert_to_linear_premul);
385
386 p.append(SkRasterPipeline::srcover);
387
388 // Convert back to dst.
389 if (kUnpremul_SkAlphaType == at) {
390 p.append(SkRasterPipeline::unpremul);
391 }
392 if (needsSrgbToLinear) {
393 p.append(SkRasterPipeline::to_srgb);
394 }
Mike Klein45c16fa2017-07-18 18:15:13 -0400395 p.append(store_dst, &dst_ctx);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400396
Mike Klein45c16fa2017-07-18 18:15:13 -0400397 p.run(0,0, width,1);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400398}
399
scroggoeb602a52015-07-09 08:16:03 -0700400SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000401 const Options& options, int* rowsDecodedPtr) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400402 const int index = options.fFrameIndex;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400403 SkASSERT(0 == index || index < fFrameHolder.size());
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400404
405 const auto& srcInfo = this->getInfo();
Matt Sarettcf3f2342017-03-23 15:32:25 -0400406 {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400407 auto info = srcInfo;
408 if (index > 0) {
409 auto alphaType = alpha_type(fFrameHolder.frame(index)->hasAlpha());
410 info = info.makeAlphaType(alphaType);
411 }
412 if (!conversion_possible(dstInfo, info) ||
413 !this->initializeColorXform(dstInfo, options.fPremulBehavior))
414 {
415 return kInvalidConversion;
416 }
417 }
418
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400419 SkASSERT(0 == index || (!options.fSubset && dstInfo.dimensions() == srcInfo.dimensions()));
scroggo6f5e6192015-06-18 12:53:43 -0700420
421 WebPDecoderConfig config;
422 if (0 == WebPInitDecoderConfig(&config)) {
423 // ABI mismatch.
424 // FIXME: New enum for this?
425 return kInvalidInput;
426 }
427
428 // Free any memory associated with the buffer. Must be called last, so we declare it first.
429 SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output));
430
Matt Sarett5c496172017-02-07 17:01:16 -0500431 WebPIterator frame;
432 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400433 // If this succeeded in onGetFrameCount(), it should succeed again here.
434 SkAssertResult(WebPDemuxGetFrame(fDemux, index + 1, &frame));
Matt Sarett604971e2017-02-06 09:51:48 -0500435
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400436 const bool independent = index == 0 ? true :
437 (fFrameHolder.frame(index)->getRequiredFrame() == kNone);
Matt Sarett5c496172017-02-07 17:01:16 -0500438 // Get the frameRect. libwebp will have already signaled an error if this is not fully
439 // contained by the canvas.
440 auto frameRect = SkIRect::MakeXYWH(frame.x_offset, frame.y_offset, frame.width, frame.height);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400441 SkASSERT(srcInfo.bounds().contains(frameRect));
442 const bool frameIsSubset = frameRect != srcInfo.bounds();
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400443 if (independent && frameIsSubset) {
444 SkSampler::Fill(dstInfo, dst, rowBytes, 0, options.fZeroInitialized);
Matt Sarett604971e2017-02-06 09:51:48 -0500445 }
446
Matt Sarett5c496172017-02-07 17:01:16 -0500447 int dstX = frameRect.x();
448 int dstY = frameRect.y();
449 int subsetWidth = frameRect.width();
450 int subsetHeight = frameRect.height();
451 if (options.fSubset) {
452 SkIRect subset = *options.fSubset;
453 SkASSERT(this->getInfo().bounds().contains(subset));
454 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
455 SkASSERT(this->getValidSubset(&subset) && subset == *options.fSubset);
456
457 if (!SkIRect::IntersectsNoEmptyCheck(subset, frameRect)) {
458 return kSuccess;
459 }
460
461 int minXOffset = SkTMin(dstX, subset.x());
462 int minYOffset = SkTMin(dstY, subset.y());
463 dstX -= minXOffset;
464 dstY -= minYOffset;
465 frameRect.offset(-minXOffset, -minYOffset);
466 subset.offset(-minXOffset, -minYOffset);
467
468 // Just like we require that the requested subset x and y offset are even, libwebp
469 // guarantees that the frame x and y offset are even (it's actually impossible to specify
470 // an odd frame offset). So we can still guarantee that the adjusted offsets are even.
471 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
472
473 SkIRect intersection;
474 SkAssertResult(intersection.intersect(frameRect, subset));
475 subsetWidth = intersection.width();
476 subsetHeight = intersection.height();
477
478 config.options.use_cropping = 1;
479 config.options.crop_left = subset.x();
480 config.options.crop_top = subset.y();
481 config.options.crop_width = subsetWidth;
482 config.options.crop_height = subsetHeight;
483 }
484
485 // Ignore the frame size and offset when determining if scaling is necessary.
486 int scaledWidth = subsetWidth;
487 int scaledHeight = subsetHeight;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400488 SkISize srcSize = options.fSubset ? options.fSubset->size() : srcInfo.dimensions();
Matt Sarett5c496172017-02-07 17:01:16 -0500489 if (srcSize != dstInfo.dimensions()) {
scroggo6f5e6192015-06-18 12:53:43 -0700490 config.options.use_scaling = 1;
Matt Sarett5c496172017-02-07 17:01:16 -0500491
492 if (frameIsSubset) {
493 float scaleX = ((float) dstInfo.width()) / srcSize.width();
494 float scaleY = ((float) dstInfo.height()) / srcSize.height();
495
496 // We need to be conservative here and floor rather than round.
497 // Otherwise, we may find ourselves decoding off the end of memory.
498 dstX = scaleX * dstX;
499 scaledWidth = scaleX * scaledWidth;
500 dstY = scaleY * dstY;
501 scaledHeight = scaleY * scaledHeight;
502 if (0 == scaledWidth || 0 == scaledHeight) {
503 return kSuccess;
504 }
505 } else {
506 scaledWidth = dstInfo.width();
507 scaledHeight = dstInfo.height();
508 }
509
510 config.options.scaled_width = scaledWidth;
511 config.options.scaled_height = scaledHeight;
scroggo6f5e6192015-06-18 12:53:43 -0700512 }
513
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400514 const bool blendWithPrevFrame = !independent && frame.blend_method == WEBP_MUX_BLEND
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400515 && frame.has_alpha;
516 if (blendWithPrevFrame && options.fPremulBehavior == SkTransferFunctionBehavior::kRespect) {
517 // Blending is done with SkRasterPipeline, which requires a color space that is valid for
518 // rendering.
519 const auto* cs = dstInfo.colorSpace();
520 if (!cs || (!cs->gammaCloseToSRGB() && !cs->gammaIsLinear())) {
521 return kInvalidConversion;
522 }
523 }
524
525 SkBitmap webpDst;
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400526 auto webpInfo = dstInfo;
527 if (!frame.has_alpha) {
528 webpInfo = webpInfo.makeAlphaType(kOpaque_SkAlphaType);
529 }
530 if (this->colorXform()) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400531 // Swizzling between RGBA and BGRA is zero cost in a color transform. So when we have a
532 // color transform, we should decode to whatever is easiest for libwebp, and then let the
533 // color transform swizzle if necessary.
534 // Lossy webp is encoded as YUV (so RGBA and BGRA are the same cost). Lossless webp is
535 // 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 -0400536 webpInfo = webpInfo.makeColorType(kBGRA_8888_SkColorType);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400537
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400538 if (webpInfo.alphaType() == kPremul_SkAlphaType) {
539 webpInfo = webpInfo.makeAlphaType(kUnpremul_SkAlphaType);
540 }
541 }
542
543 if ((this->colorXform() && !is_8888(dstInfo.colorType())) || blendWithPrevFrame) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400544 // We will decode the entire image and then perform the color transform. libwebp
545 // does not provide a row-by-row API. This is a shame particularly when we do not want
546 // 8888, since we will need to create another image sized buffer.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400547 webpDst.allocPixels(webpInfo);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400548 } else {
549 // libwebp can decode directly into the output memory.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400550 webpDst.installPixels(webpInfo, dst, rowBytes);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400551 }
552
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400553 config.output.colorspace = webp_decode_mode(webpInfo);
scroggo6f5e6192015-06-18 12:53:43 -0700554 config.output.is_external_memory = 1;
555
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400556 config.output.u.RGBA.rgba = reinterpret_cast<uint8_t*>(webpDst.getAddr(dstX, dstY));
557 config.output.u.RGBA.stride = static_cast<int>(webpDst.rowBytes());
558 config.output.u.RGBA.size = webpDst.getSafeSize();
msarettff2a6c82016-09-07 11:23:28 -0700559
halcanary96fcdcc2015-08-27 07:41:13 -0700560 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
scroggo6f5e6192015-06-18 12:53:43 -0700561 if (!idec) {
562 return kInvalidInput;
563 }
564
msarette99883f2016-09-08 06:05:35 -0700565 int rowsDecoded;
566 SkCodec::Result result;
msarettff2a6c82016-09-07 11:23:28 -0700567 switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) {
568 case VP8_STATUS_OK:
Matt Sarett5c496172017-02-07 17:01:16 -0500569 rowsDecoded = scaledHeight;
msarette99883f2016-09-08 06:05:35 -0700570 result = kSuccess;
571 break;
msarettff2a6c82016-09-07 11:23:28 -0700572 case VP8_STATUS_SUSPENDED:
Matt Sarett5c496172017-02-07 17:01:16 -0500573 WebPIDecGetRGB(idec, &rowsDecoded, nullptr, nullptr, nullptr);
574 *rowsDecodedPtr = rowsDecoded + dstY;
msarette99883f2016-09-08 06:05:35 -0700575 result = kIncompleteInput;
576 break;
msarettff2a6c82016-09-07 11:23:28 -0700577 default:
578 return kInvalidInput;
scroggo6f5e6192015-06-18 12:53:43 -0700579 }
msarette99883f2016-09-08 06:05:35 -0700580
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400581 // We're only transforming the new part of the frame, so no need to worry about the
582 // final composited alpha.
583 const auto srcAlpha = 0 == index ? srcInfo.alphaType() : alpha_type(frame.has_alpha);
584 const auto xformAlphaType = select_xform_alpha(dstInfo.alphaType(), srcAlpha);
585 const bool needsSrgbToLinear = dstInfo.gammaCloseToSRGB() &&
586 options.fPremulBehavior == SkTransferFunctionBehavior::kRespect;
587
588 const size_t dstBpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
589 dst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY);
590 const size_t srcRowBytes = config.output.u.RGBA.stride;
591
592 const auto dstCT = dstInfo.colorType();
Matt Sarett313c4632016-10-20 12:35:23 -0400593 if (this->colorXform()) {
Matt Sarett5c496172017-02-07 17:01:16 -0500594 uint32_t* xformSrc = (uint32_t*) config.output.u.RGBA.rgba;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400595 SkBitmap tmp;
596 void* xformDst;
597
598 if (blendWithPrevFrame) {
599 // Xform into temporary bitmap big enough for one row.
600 tmp.allocPixels(dstInfo.makeWH(scaledWidth, 1));
601 xformDst = tmp.getPixels();
602 } else {
603 xformDst = dst;
604 }
Robert Phillipsb3050b92017-02-06 13:12:18 +0000605 for (int y = 0; y < rowsDecoded; y++) {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400606 this->applyColorXform(xformDst, xformSrc, scaledWidth, xformAlphaType);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400607 if (blendWithPrevFrame) {
Mike Klein45c16fa2017-07-18 18:15:13 -0400608 blend_line(dstCT, dst, dstCT, xformDst, needsSrgbToLinear, xformAlphaType,
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400609 scaledWidth);
610 dst = SkTAddOffset<void>(dst, rowBytes);
611 } else {
612 xformDst = SkTAddOffset<void>(xformDst, rowBytes);
613 }
Matt Sarett5c496172017-02-07 17:01:16 -0500614 xformSrc = SkTAddOffset<uint32_t>(xformSrc, srcRowBytes);
msarette99883f2016-09-08 06:05:35 -0700615 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400616 } else if (blendWithPrevFrame) {
617 const uint8_t* src = config.output.u.RGBA.rgba;
618
619 for (int y = 0; y < rowsDecoded; y++) {
Mike Klein45c16fa2017-07-18 18:15:13 -0400620 blend_line(dstCT, dst, webpDst.colorType(), src, needsSrgbToLinear,
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400621 xformAlphaType, scaledWidth);
622 src = SkTAddOffset<const uint8_t>(src, srcRowBytes);
623 dst = SkTAddOffset<void>(dst, rowBytes);
624 }
msarette99883f2016-09-08 06:05:35 -0700625 }
626
627 return result;
scroggo6f5e6192015-06-18 12:53:43 -0700628}
629
msarett9d15dab2016-08-24 07:36:06 -0700630SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info,
msarettff2a6c82016-09-07 11:23:28 -0700631 sk_sp<SkColorSpace> colorSpace, SkStream* stream, WebPDemuxer* demux,
632 sk_sp<SkData> data)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400633 : INHERITED(width, height, info, SkColorSpaceXform::kBGRA_8888_ColorFormat, stream,
634 std::move(colorSpace))
msarettff2a6c82016-09-07 11:23:28 -0700635 , fDemux(demux)
636 , fData(std::move(data))
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400637 , fFailed(false)
638{
639 fFrameHolder.setScreenSize(width, height);
640}