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