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