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