msarett | 8c8f22a | 2015-04-01 06:58:48 -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 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 8 | /* |
| 9 | * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * 1. Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in the |
| 18 | * documentation and/or other materials provided with the distribution. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 21 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 28 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | #include "SkCodecAnimation.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 34 | #include "SkCodecPriv.h" |
| 35 | #include "SkColorPriv.h" |
| 36 | #include "SkColorTable.h" |
msarett | 1a46467 | 2016-01-07 13:17:19 -0800 | [diff] [blame] | 37 | #include "SkGifCodec.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 38 | #include "SkStream.h" |
| 39 | #include "SkSwizzler.h" |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 40 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 41 | #include <algorithm> |
| 42 | |
| 43 | #define GIF87_STAMP "GIF87a" |
| 44 | #define GIF89_STAMP "GIF89a" |
| 45 | #define GIF_STAMP_LEN 6 |
msarett | 39b2d5a | 2016-02-17 08:26:31 -0800 | [diff] [blame] | 46 | |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Checks the start of the stream to see if the image is a gif |
| 49 | */ |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 50 | bool SkGifCodec::IsGif(const void* buf, size_t bytesRead) { |
| 51 | if (bytesRead >= GIF_STAMP_LEN) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 52 | if (memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 || |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 53 | memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0) |
| 54 | { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | /* |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 62 | * Error function |
| 63 | */ |
bungeman | 0153dea | 2015-08-27 16:43:42 -0700 | [diff] [blame] | 64 | static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCodec::kInvalidInput) { |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 65 | SkCodecPrintf("Gif Error: %s\n", msg); |
| 66 | return result; |
| 67 | } |
| 68 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 69 | /* |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 70 | * Assumes IsGif was called and returned true |
| 71 | * Creates a gif decoder |
| 72 | * Reads enough of the stream to determine the image format |
| 73 | */ |
| 74 | SkCodec* SkGifCodec::NewFromStream(SkStream* stream) { |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 75 | std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(stream)); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 76 | if (!reader->parse(SkGifImageReader::SkGIFSizeQuery)) { |
Leon Scroggins III | 4993b95 | 2016-12-08 11:54:04 -0500 | [diff] [blame] | 77 | // Fatal error occurred. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 78 | return nullptr; |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 79 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 80 | |
Leon Scroggins III | 4993b95 | 2016-12-08 11:54:04 -0500 | [diff] [blame] | 81 | // If no images are in the data, or the first header is not yet defined, we cannot |
| 82 | // create a codec. In either case, the width and height are not yet known. |
| 83 | if (0 == reader->imagesCount() || !reader->frameContext(0)->isHeaderDefined()) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 84 | return nullptr; |
| 85 | } |
| 86 | |
Leon Scroggins III | 4993b95 | 2016-12-08 11:54:04 -0500 | [diff] [blame] | 87 | // isHeaderDefined() will not return true if the screen size is empty. |
| 88 | SkASSERT(reader->screenHeight() > 0 && reader->screenWidth() > 0); |
| 89 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 90 | const auto alpha = reader->firstFrameHasAlpha() ? SkEncodedInfo::kBinary_Alpha |
| 91 | : SkEncodedInfo::kOpaque_Alpha; |
| 92 | // Use kPalette since Gifs are encoded with a color table. |
| 93 | // FIXME: Gifs can actually be encoded with 4-bits per pixel. Using 8 works, but we could skip |
| 94 | // expanding to 8 bits and take advantage of the SkSwizzler to work from 4. |
| 95 | const auto encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8); |
| 96 | |
| 97 | // Although the encodedInfo is always kPalette_Color, it is possible that kIndex_8 is |
| 98 | // unsupported if the frame is subset and there is no transparent pixel. |
| 99 | const auto colorType = reader->firstFrameSupportsIndex8() ? kIndex_8_SkColorType |
| 100 | : kN32_SkColorType; |
| 101 | // The choice of unpremul versus premul is arbitrary, since all colors are either fully |
| 102 | // opaque or fully transparent (i.e. kBinary), but we stored the transparent colors as all |
| 103 | // zeroes, which is arguably premultiplied. |
| 104 | const auto alphaType = reader->firstFrameHasAlpha() ? kUnpremul_SkAlphaType |
| 105 | : kOpaque_SkAlphaType; |
Matt Sarett | 7f650bd | 2016-10-30 21:25:34 -0400 | [diff] [blame] | 106 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 107 | const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(), |
Matt Sarett | 7f650bd | 2016-10-30 21:25:34 -0400 | [diff] [blame] | 108 | colorType, alphaType, |
Matt Sarett | 77a7a1b | 2017-02-07 13:56:11 -0500 | [diff] [blame] | 109 | SkColorSpace::MakeSRGB()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 110 | return new SkGifCodec(encodedInfo, imageInfo, reader.release()); |
| 111 | } |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 112 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 113 | bool SkGifCodec::onRewind() { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 114 | fReader->clearDecodeState(); |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 118 | SkGifCodec::SkGifCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo, |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 119 | SkGifImageReader* reader) |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 120 | : INHERITED(encodedInfo, imageInfo, SkColorSpaceXform::kRGBA_8888_ColorFormat, nullptr) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 121 | , fReader(reader) |
| 122 | , fTmpBuffer(nullptr) |
| 123 | , fSwizzler(nullptr) |
| 124 | , fCurrColorTable(nullptr) |
| 125 | , fCurrColorTableIsReal(false) |
| 126 | , fFilledBackground(false) |
| 127 | , fFirstCallToIncrementalDecode(false) |
| 128 | , fDst(nullptr) |
| 129 | , fDstRowBytes(0) |
| 130 | , fRowsDecoded(0) |
| 131 | { |
| 132 | reader->setClient(this); |
msarett | 8c8f22a | 2015-04-01 06:58:48 -0700 | [diff] [blame] | 133 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 134 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 135 | int SkGifCodec::onGetFrameCount() { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 136 | fReader->parse(SkGifImageReader::SkGIFFrameCountQuery); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 137 | return fReader->imagesCount(); |
| 138 | } |
| 139 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 140 | bool SkGifCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const { |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 141 | if (i >= fReader->imagesCount()) { |
| 142 | return false; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 143 | } |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 144 | |
| 145 | const SkGIFFrameContext* frameContext = fReader->frameContext(i); |
| 146 | if (!frameContext->reachedStartOfData()) { |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | if (frameInfo) { |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 151 | frameInfo->fDuration = frameContext->getDuration(); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 152 | frameInfo->fRequiredFrame = frameContext->getRequiredFrame(); |
| 153 | frameInfo->fFullyReceived = frameContext->isComplete(); |
| 154 | frameInfo->fAlphaType = frameContext->hasAlpha() ? kUnpremul_SkAlphaType |
| 155 | : kOpaque_SkAlphaType; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 156 | frameInfo->fDisposalMethod = frameContext->getDisposalMethod(); |
Leon Scroggins III | e132e7b | 2017-04-12 10:49:52 -0400 | [diff] [blame] | 157 | } |
| 158 | return true; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 159 | } |
| 160 | |
scroggo | e71b1a1 | 2016-11-01 08:28:28 -0700 | [diff] [blame] | 161 | int SkGifCodec::onGetRepetitionCount() { |
| 162 | fReader->parse(SkGifImageReader::SkGIFLoopCountQuery); |
| 163 | return fReader->loopCount(); |
| 164 | } |
| 165 | |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 166 | static const SkColorType kXformSrcColorType = kRGBA_8888_SkColorType; |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 167 | static const SkAlphaType kXformAlphaType = kUnpremul_SkAlphaType; |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 168 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 169 | void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, int frameIndex) { |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 170 | SkColorType colorTableColorType = dstInfo.colorType(); |
| 171 | if (this->colorXform()) { |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 172 | colorTableColorType = kXformSrcColorType; |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | sk_sp<SkColorTable> currColorTable = fReader->getColorTable(colorTableColorType, frameIndex); |
| 176 | fCurrColorTableIsReal = currColorTable; |
| 177 | if (!fCurrColorTableIsReal) { |
Leon Scroggins III | a049ac4 | 2016-10-27 11:16:11 -0400 | [diff] [blame] | 178 | // This is possible for an empty frame. Create a dummy with one value (transparent). |
| 179 | SkPMColor color = SK_ColorTRANSPARENT; |
| 180 | fCurrColorTable.reset(new SkColorTable(&color, 1)); |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 181 | } else if (this->colorXform() && !this->xformOnDecode()) { |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 182 | SkPMColor dstColors[256]; |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 183 | this->applyColorXform(dstColors, currColorTable->readColors(), currColorTable->count(), |
| 184 | kXformAlphaType); |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 185 | fCurrColorTable.reset(new SkColorTable(dstColors, currColorTable->count())); |
| 186 | } else { |
| 187 | fCurrColorTable = std::move(currColorTable); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 188 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 189 | } |
| 190 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 191 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 192 | SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr, |
| 193 | int* inputColorCount, const Options& opts) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 194 | if (opts.fSubset) { |
| 195 | return gif_error("Subsets not supported.\n", kUnimplemented); |
| 196 | } |
| 197 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 198 | const int frameIndex = opts.fFrameIndex; |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 199 | if (frameIndex > 0) { |
| 200 | switch (dstInfo.colorType()) { |
| 201 | case kIndex_8_SkColorType: |
| 202 | // FIXME: It is possible that a later frame can be decoded to index8, if it does one |
| 203 | // of the following: |
| 204 | // - Covers the entire previous frame |
| 205 | // - Shares a color table (and transparent index) with any prior frames that are |
| 206 | // showing. |
| 207 | // We must support index8 for the first frame to be backwards compatible on Android, |
| 208 | // but we do not (currently) need to support later frames as index8. |
| 209 | return gif_error("Cannot decode multiframe gif (except frame 0) as index 8.\n", |
| 210 | kInvalidConversion); |
| 211 | case kRGB_565_SkColorType: |
| 212 | // FIXME: In theory, we might be able to support this, but it's not clear that it |
| 213 | // is necessary (Chromium does not decode to 565, and Android does not decode |
| 214 | // frames beyond the first). Disabling it because it is somewhat difficult: |
| 215 | // - If there is a transparent pixel, and this frame draws on top of another frame |
| 216 | // (if the frame is independent with a transparent pixel, we should not decode to |
| 217 | // 565 anyway, since it is not opaque), we need to skip drawing the transparent |
| 218 | // pixels (see writeTransparentPixels in haveDecodedRow). We currently do this by |
| 219 | // first swizzling into temporary memory, then copying into the destination. (We |
| 220 | // let the swizzler handle it first because it may need to sample.) After |
| 221 | // swizzling to 565, we do not know which pixels in our temporary memory |
| 222 | // correspond to the transparent pixel, so we do not know what to skip. We could |
| 223 | // special case the non-sampled case (no need to swizzle), but as this is |
| 224 | // currently unused we can just not support it. |
| 225 | return gif_error("Cannot decode multiframe gif (except frame 0) as 565.\n", |
| 226 | kInvalidConversion); |
| 227 | default: |
| 228 | break; |
| 229 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 230 | } |
| 231 | |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 232 | fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 233 | |
| 234 | if (frameIndex >= fReader->imagesCount()) { |
| 235 | return gif_error("frame index out of range!\n", kIncompleteInput); |
| 236 | } |
| 237 | |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 238 | const auto* frame = fReader->frameContext(frameIndex); |
| 239 | if (!frame->reachedStartOfData()) { |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 240 | // We have parsed enough to know that there is a color map, but cannot |
| 241 | // parse the map itself yet. Exit now, so we do not build an incorrect |
| 242 | // table. |
| 243 | return gif_error("color map not available yet\n", kIncompleteInput); |
| 244 | } |
| 245 | |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 246 | const auto at = frame->hasAlpha() ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType; |
| 247 | const auto srcInfo = this->getInfo().makeAlphaType(at); |
| 248 | if (!conversion_possible(dstInfo, srcInfo) || |
| 249 | !this->initializeColorXform(dstInfo, opts.fPremulBehavior)) |
| 250 | { |
| 251 | return gif_error("Cannot convert input type to output type.\n", kInvalidConversion); |
| 252 | } |
| 253 | |
| 254 | if (this->xformOnDecode()) { |
| 255 | fXformBuffer.reset(new uint32_t[dstInfo.width()]); |
| 256 | sk_bzero(fXformBuffer.get(), dstInfo.width() * sizeof(uint32_t)); |
| 257 | } |
| 258 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 259 | fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]); |
| 260 | |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 261 | this->initializeColorTable(dstInfo, frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 262 | this->initializeSwizzler(dstInfo, frameIndex); |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 263 | |
| 264 | SkASSERT(fCurrColorTable); |
| 265 | if (inputColorCount) { |
| 266 | *inputColorCount = fCurrColorTable->count(); |
| 267 | } |
| 268 | copy_color_table(dstInfo, fCurrColorTable.get(), inputColorPtr, inputColorCount); |
| 269 | |
msarett | b30d698 | 2016-02-15 10:18:45 -0800 | [diff] [blame] | 270 | return kSuccess; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 273 | void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex) { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 274 | const SkGIFFrameContext* frame = fReader->frameContext(frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 275 | // This is only called by prepareToDecode, which ensures frameIndex is in range. |
| 276 | SkASSERT(frame); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 277 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 278 | const int xBegin = frame->xOffset(); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 279 | const int xEnd = std::min(frame->frameRect().right(), fReader->screenWidth()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 280 | |
| 281 | // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw |
| 282 | // frameRect, since it might extend beyond the edge of the frame. |
| 283 | SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0); |
| 284 | |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 285 | SkImageInfo swizzlerInfo = dstInfo; |
| 286 | if (this->colorXform()) { |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 287 | swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType); |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 288 | if (kPremul_SkAlphaType == dstInfo.alphaType()) { |
| 289 | swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType); |
| 290 | } |
| 291 | } |
| 292 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 293 | // The default Options should be fine: |
| 294 | // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't |
| 295 | // matter anyway. |
| 296 | // - subsets are not supported for gif |
| 297 | // - the swizzler does not need to know about the frame. |
| 298 | // We may not be able to use the real Options anyway, since getPixels does not store it (due to |
| 299 | // a bug). |
| 300 | fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 301 | fCurrColorTable->readColors(), swizzlerInfo, Options(), &swizzleRect)); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 302 | SkASSERT(fSwizzler.get()); |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Initiates the gif decode |
| 307 | */ |
| 308 | SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo, |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 309 | void* pixels, size_t dstRowBytes, |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 310 | const Options& opts, |
| 311 | SkPMColor* inputColorPtr, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 312 | int* inputColorCount, |
| 313 | int* rowsDecoded) { |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 314 | Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts); |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 315 | switch (result) { |
| 316 | case kSuccess: |
| 317 | break; |
| 318 | case kIncompleteInput: |
| 319 | // onStartIncrementalDecode treats this as incomplete, since it may |
| 320 | // provide more data later, but in this case, no more data will be |
| 321 | // provided, and there is nothing to draw. We also cannot return |
| 322 | // kIncompleteInput, which will make SkCodec attempt to fill |
| 323 | // remaining rows, but that requires an SkSwizzler, which we have |
| 324 | // not created. |
| 325 | return kInvalidInput; |
| 326 | default: |
| 327 | return result; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | if (dstInfo.dimensions() != this->getInfo().dimensions()) { |
| 331 | return gif_error("Scaling not supported.\n", kInvalidScale); |
| 332 | } |
| 333 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 334 | fDst = pixels; |
| 335 | fDstRowBytes = dstRowBytes; |
| 336 | |
| 337 | return this->decodeFrame(true, opts, rowsDecoded); |
| 338 | } |
| 339 | |
| 340 | SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo, |
| 341 | void* pixels, size_t dstRowBytes, |
| 342 | const SkCodec::Options& opts, |
| 343 | SkPMColor* inputColorPtr, |
| 344 | int* inputColorCount) { |
| 345 | Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts); |
| 346 | if (result != kSuccess) { |
| 347 | return result; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 348 | } |
| 349 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 350 | fDst = pixels; |
| 351 | fDstRowBytes = dstRowBytes; |
| 352 | |
| 353 | fFirstCallToIncrementalDecode = true; |
| 354 | |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 355 | return kSuccess; |
| 356 | } |
| 357 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 358 | SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) { |
| 359 | // It is possible the client has appended more data. Parse, if needed. |
| 360 | const auto& options = this->options(); |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 361 | const int frameIndex = options.fFrameIndex; |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 362 | fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 363 | |
| 364 | const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode; |
| 365 | fFirstCallToIncrementalDecode = false; |
| 366 | return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 367 | } |
| 368 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 369 | SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) { |
| 370 | const SkImageInfo& dstInfo = this->dstInfo(); |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 371 | const int frameIndex = opts.fFrameIndex; |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 372 | SkASSERT(frameIndex < fReader->imagesCount()); |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 373 | const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 374 | if (firstAttempt) { |
| 375 | // rowsDecoded reports how many rows have been initialized, so a layer above |
| 376 | // can fill the rest. In some cases, we fill the background before decoding |
| 377 | // (or it is already filled for us), so we report rowsDecoded to be the full |
| 378 | // height. |
| 379 | bool filledBackground = false; |
| 380 | if (frameContext->getRequiredFrame() == kNone) { |
| 381 | // We may need to clear to transparent for one of the following reasons: |
| 382 | // - The frameRect does not cover the full bounds. haveDecodedRow will |
| 383 | // only draw inside the frameRect, so we need to clear the rest. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 384 | // - The frame is interlaced. There is no obvious way to fill |
| 385 | // afterwards for an incomplete image. (FIXME: Does the first pass |
| 386 | // cover all rows? If so, we do not have to fill here.) |
scroggo | 8bce117 | 2016-10-25 13:08:40 -0700 | [diff] [blame] | 387 | // - There is no color table for this frame. In that case will not |
| 388 | // draw anything, so we need to fill. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 389 | if (frameContext->frameRect() != this->getInfo().bounds() |
scroggo | 8bce117 | 2016-10-25 13:08:40 -0700 | [diff] [blame] | 390 | || frameContext->interlaced() || !fCurrColorTableIsReal) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 391 | // fill ignores the width (replaces it with the actual, scaled width). |
| 392 | // But we need to scale in Y. |
| 393 | const int scaledHeight = get_scaled_dimension(dstInfo.height(), |
| 394 | fSwizzler->sampleY()); |
| 395 | auto fillInfo = dstInfo.makeWH(0, scaledHeight); |
| 396 | fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue(dstInfo), |
| 397 | opts.fZeroInitialized); |
| 398 | filledBackground = true; |
| 399 | } |
| 400 | } else { |
| 401 | // Not independent |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 402 | // FIXME: Share this code with WEBP |
| 403 | const int reqFrame = frameContext->getRequiredFrame(); |
| 404 | if (opts.fPriorFrame != kNone) { |
| 405 | if (opts.fPriorFrame < reqFrame || opts.fPriorFrame >= frameIndex) { |
| 406 | // Alternatively, we could correct this to kNone. |
| 407 | return kInvalidParameters; |
| 408 | } |
| 409 | const auto* prevFrame = fReader->frameContext(opts.fPriorFrame); |
| 410 | if (prevFrame->getDisposalMethod() |
| 411 | == SkCodecAnimation::DisposalMethod::kRestorePrevious) { |
| 412 | // Similarly, this could be corrected to kNone. |
| 413 | return kInvalidParameters; |
| 414 | } |
| 415 | } else { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 416 | // Decode that frame into pixels. |
| 417 | Options prevFrameOpts(opts); |
| 418 | prevFrameOpts.fFrameIndex = frameContext->getRequiredFrame(); |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 419 | prevFrameOpts.fPriorFrame = kNone; |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 420 | // The prior frame may have a different color table, so update it and the |
| 421 | // swizzler. |
| 422 | this->initializeColorTable(dstInfo, prevFrameOpts.fFrameIndex); |
| 423 | this->initializeSwizzler(dstInfo, prevFrameOpts.fFrameIndex); |
| 424 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 425 | const Result prevResult = this->decodeFrame(true, prevFrameOpts, nullptr); |
| 426 | switch (prevResult) { |
| 427 | case kSuccess: |
| 428 | // Prior frame succeeded. Carry on. |
| 429 | break; |
| 430 | case kIncompleteInput: |
| 431 | // Prior frame was incomplete. So this frame cannot be decoded. |
| 432 | return kInvalidInput; |
| 433 | default: |
| 434 | return prevResult; |
| 435 | } |
Leon Scroggins III | fc49b40 | 2016-10-31 14:08:56 -0400 | [diff] [blame] | 436 | |
| 437 | // Go back to using the correct color table for this frame. |
| 438 | this->initializeColorTable(dstInfo, frameIndex); |
| 439 | this->initializeSwizzler(dstInfo, frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 440 | } |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 441 | |
| 442 | // If the required frame is RestoreBG, we need to erase it. If a frame after the |
| 443 | // required frame is provided, there is no need to erase, since it will be covered |
| 444 | // anyway. |
| 445 | if (opts.fPriorFrame == reqFrame || opts.fPriorFrame == kNone) { |
| 446 | const auto* prevFrame = fReader->frameContext(reqFrame); |
| 447 | if (prevFrame->getDisposalMethod() |
| 448 | == SkCodecAnimation::DisposalMethod::kRestoreBGColor) { |
| 449 | SkIRect prevRect = prevFrame->frameRect(); |
| 450 | if (prevRect.intersect(this->getInfo().bounds())) { |
| 451 | // Do the divide ourselves for left and top, since we do not want |
| 452 | // get_scaled_dimension to upgrade 0 to 1. (This is similar to |
| 453 | // SkSampledCodec's sampling of the subset.) |
| 454 | const auto sampleX = fSwizzler->sampleX(); |
| 455 | const auto sampleY = fSwizzler->sampleY(); |
| 456 | auto left = prevRect.fLeft / sampleX; |
| 457 | auto top = prevRect.fTop / sampleY; |
| 458 | void* const eraseDst = SkTAddOffset<void>(fDst, top * fDstRowBytes |
| 459 | + left * SkColorTypeBytesPerPixel(dstInfo.colorType())); |
| 460 | auto width = get_scaled_dimension(prevRect.width(), sampleX); |
| 461 | auto height = get_scaled_dimension(prevRect.height(), sampleY); |
| 462 | // fSwizzler->fill() would fill to the scaled width of the frame, but we |
| 463 | // want to fill to the scaled with of the width of the PRIOR frame, so we |
| 464 | // do all the scaling ourselves and call the static version. |
| 465 | SkSampler::Fill(dstInfo.makeWH(width, height), eraseDst, fDstRowBytes, |
| 466 | this->getFillValue(dstInfo), kNo_ZeroInitialized); |
| 467 | } |
Leon Scroggins III | 56e3209 | 2016-12-12 17:10:46 -0500 | [diff] [blame] | 468 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 469 | } |
| 470 | filledBackground = true; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 471 | } |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 472 | |
| 473 | fFilledBackground = filledBackground; |
| 474 | if (filledBackground) { |
| 475 | // Report the full (scaled) height, since the client will never need to fill. |
| 476 | fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY()); |
| 477 | } else { |
| 478 | // This will be updated by haveDecodedRow. |
| 479 | fRowsDecoded = 0; |
| 480 | } |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 481 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 482 | |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 483 | if (!fCurrColorTableIsReal) { |
| 484 | // Nothing to draw this frame. |
| 485 | return kSuccess; |
| 486 | } |
| 487 | |
scroggo | 3d3a65c | 2016-10-24 12:28:30 -0700 | [diff] [blame] | 488 | // Note: there is a difference between the following call to SkGifImageReader::decode |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 489 | // returning false and leaving frameDecoded false: |
| 490 | // - If the method returns false, there was an error in the stream. We still treat this as |
| 491 | // incomplete, since we have already decoded some rows. |
| 492 | // - If frameDecoded is false, that just means that we do not have enough data. If more data |
| 493 | // is supplied, we may be able to continue decoding this frame. We also treat this as |
| 494 | // incomplete. |
| 495 | // FIXME: Ensure that we do not attempt to continue decoding if the method returns false and |
| 496 | // more data is supplied. |
| 497 | bool frameDecoded = false; |
| 498 | if (!fReader->decode(frameIndex, &frameDecoded) || !frameDecoded) { |
| 499 | if (rowsDecoded) { |
| 500 | *rowsDecoded = fRowsDecoded; |
| 501 | } |
| 502 | return kIncompleteInput; |
| 503 | } |
| 504 | |
| 505 | return kSuccess; |
msarett | 10522ff | 2015-09-07 08:54:01 -0700 | [diff] [blame] | 506 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 507 | |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 508 | uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const { |
| 509 | // Note: Using fCurrColorTable relies on having called initializeColorTable already. |
| 510 | // This is (currently) safe because this method is only called when filling, after |
| 511 | // initializeColorTable has been called. |
| 512 | // FIXME: Is there a way to make this less fragile? |
| 513 | if (dstInfo.colorType() == kIndex_8_SkColorType && fCurrColorTableIsReal) { |
| 514 | // We only support index 8 for the first frame, for backwards |
| 515 | // compatibity on Android, so we are using the color table for the first frame. |
| 516 | SkASSERT(this->options().fFrameIndex == 0); |
| 517 | // Use the transparent index for the first frame. |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 518 | const int transPixel = fReader->frameContext(0)->transparentPixel(); |
| 519 | if (transPixel >= 0 && transPixel < fCurrColorTable->count()) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 520 | return transPixel; |
| 521 | } |
| 522 | // Fall through to return SK_ColorTRANSPARENT (i.e. 0). This choice is arbitrary, |
| 523 | // but we have to pick something inside the color table, and this one is as good |
| 524 | // as any. |
| 525 | } |
| 526 | // Using transparent as the fill value matches the behavior in Chromium, |
| 527 | // which ignores the background color. |
| 528 | // If the colorType is kIndex_8, and there was no color table (i.e. |
| 529 | // fCurrColorTableIsReal is false), this value (zero) corresponds to the |
| 530 | // only entry in the dummy color table provided to the client. |
| 531 | return SK_ColorTRANSPARENT; |
| 532 | } |
msarett | 72261c0 | 2015-11-19 15:29:26 -0800 | [diff] [blame] | 533 | |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 534 | void SkGifCodec::applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const { |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 535 | if (this->xformOnDecode()) { |
| 536 | SkASSERT(this->colorXform()); |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 537 | fSwizzler->swizzle(fXformBuffer.get(), src); |
| 538 | |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 539 | const int xformWidth = get_scaled_dimension(dstInfo.width(), fSwizzler->sampleX()); |
Leon Scroggins III | 91f0f73 | 2017-06-07 09:31:23 -0400 | [diff] [blame] | 540 | this->applyColorXform(dst, fXformBuffer.get(), xformWidth, kXformAlphaType); |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 541 | } else { |
| 542 | fSwizzler->swizzle(dst, src); |
| 543 | } |
| 544 | } |
| 545 | |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 546 | bool SkGifCodec::haveDecodedRow(int frameIndex, const unsigned char* rowBegin, |
| 547 | int rowNumber, int repeatCount, bool writeTransparentPixels) |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 548 | { |
scroggo | f9acbe2 | 2016-10-25 12:43:21 -0700 | [diff] [blame] | 549 | const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 550 | // The pixel data and coordinates supplied to us are relative to the frame's |
| 551 | // origin within the entire image size, i.e. |
| 552 | // (frameContext->xOffset, frameContext->yOffset). There is no guarantee |
| 553 | // that width == (size().width() - frameContext->xOffset), so |
| 554 | // we must ensure we don't run off the end of either the source data or the |
| 555 | // row's X-coordinates. |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 556 | const int width = frameContext->width(); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 557 | const int xBegin = frameContext->xOffset(); |
| 558 | const int yBegin = frameContext->yOffset() + rowNumber; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 559 | const int xEnd = std::min(xBegin + width, this->getInfo().width()); |
| 560 | const int yEnd = std::min(yBegin + rowNumber + repeatCount, this->getInfo().height()); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 561 | // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do |
| 562 | // this once in prepareToDecode. |
| 563 | if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin)) |
| 564 | return true; |
| 565 | |
| 566 | // yBegin is the first row in the non-sampled image. dstRow will be the row in the output, |
| 567 | // after potentially scaling it. |
| 568 | int dstRow = yBegin; |
| 569 | |
| 570 | const int sampleY = fSwizzler->sampleY(); |
| 571 | if (sampleY > 1) { |
| 572 | // Check to see whether this row or one that falls in the repeatCount is needed in the |
| 573 | // output. |
| 574 | bool foundNecessaryRow = false; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 575 | for (int i = 0; i < repeatCount; i++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 576 | const int potentialRow = yBegin + i; |
| 577 | if (fSwizzler->rowNeeded(potentialRow)) { |
| 578 | dstRow = potentialRow / sampleY; |
| 579 | const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY); |
| 580 | if (dstRow >= scaledHeight) { |
| 581 | return true; |
| 582 | } |
| 583 | |
| 584 | foundNecessaryRow = true; |
| 585 | repeatCount -= i; |
| 586 | |
| 587 | repeatCount = (repeatCount - 1) / sampleY + 1; |
| 588 | |
| 589 | // Make sure the repeatCount does not take us beyond the end of the dst |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 590 | if (dstRow + repeatCount > scaledHeight) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 591 | repeatCount = scaledHeight - dstRow; |
| 592 | SkASSERT(repeatCount >= 1); |
| 593 | } |
| 594 | break; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | if (!foundNecessaryRow) { |
| 599 | return true; |
| 600 | } |
Matt Sarett | 8a4e9c5 | 2016-10-25 14:24:50 -0400 | [diff] [blame] | 601 | } else { |
| 602 | // Make sure the repeatCount does not take us beyond the end of the dst |
| 603 | SkASSERT(this->dstInfo().height() >= yBegin); |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 604 | repeatCount = SkTMin(repeatCount, this->dstInfo().height() - yBegin); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | if (!fFilledBackground) { |
| 608 | // At this point, we are definitely going to write the row, so count it towards the number |
| 609 | // of rows decoded. |
| 610 | // We do not consider the repeatCount, which only happens for interlaced, in which case we |
| 611 | // have already set fRowsDecoded to the proper value (reflecting that we have filled the |
| 612 | // background). |
| 613 | fRowsDecoded++; |
| 614 | } |
| 615 | |
Leon Scroggins III | 3fc97d7 | 2016-12-09 16:39:33 -0500 | [diff] [blame] | 616 | // decodeFrame will early exit if this is false, so this method will not be |
| 617 | // called. |
| 618 | SkASSERT(fCurrColorTableIsReal); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 619 | |
| 620 | // The swizzler takes care of offsetting into the dst width-wise. |
| 621 | void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes); |
| 622 | |
| 623 | // We may or may not need to write transparent pixels to the buffer. |
scroggo | 1285f41 | 2016-10-26 13:48:03 -0700 | [diff] [blame] | 624 | // If we're compositing against a previous image, it's wrong, but if |
| 625 | // we're decoding an interlaced gif and displaying it "Haeberli"-style, |
| 626 | // we must write these for passes beyond the first, or the initial passes |
| 627 | // will "show through" the later ones. |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 628 | const auto dstInfo = this->dstInfo(); |
scroggo | 53f63b6 | 2016-10-27 08:29:13 -0700 | [diff] [blame] | 629 | if (writeTransparentPixels) { |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 630 | this->applyXformRow(dstInfo, dstLine, rowBegin); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 631 | } else { |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 632 | sk_bzero(fTmpBuffer.get(), dstInfo.minRowBytes()); |
| 633 | this->applyXformRow(dstInfo, fTmpBuffer.get(), rowBegin); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 634 | |
| 635 | const size_t offsetBytes = fSwizzler->swizzleOffsetBytes(); |
| 636 | switch (dstInfo.colorType()) { |
| 637 | case kBGRA_8888_SkColorType: |
| 638 | case kRGBA_8888_SkColorType: { |
| 639 | uint32_t* dstPixel = SkTAddOffset<uint32_t>(dstLine, offsetBytes); |
| 640 | uint32_t* srcPixel = SkTAddOffset<uint32_t>(fTmpBuffer.get(), offsetBytes); |
| 641 | for (int i = 0; i < fSwizzler->swizzleWidth(); i++) { |
| 642 | // Technically SK_ColorTRANSPARENT is an SkPMColor, and srcPixel would have |
| 643 | // the opposite swizzle for the non-native swizzle, but TRANSPARENT is all |
| 644 | // zeroes, which is the same either way. |
| 645 | if (*srcPixel != SK_ColorTRANSPARENT) { |
| 646 | *dstPixel = *srcPixel; |
| 647 | } |
| 648 | dstPixel++; |
| 649 | srcPixel++; |
| 650 | } |
| 651 | break; |
| 652 | } |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 653 | case kRGBA_F16_SkColorType: { |
| 654 | uint64_t* dstPixel = SkTAddOffset<uint64_t>(dstLine, offsetBytes); |
| 655 | uint64_t* srcPixel = SkTAddOffset<uint64_t>(fTmpBuffer.get(), offsetBytes); |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 656 | for (int i = 0; i < fSwizzler->swizzleWidth(); i++) { |
Matt Sarett | 61eedeb | 2016-11-04 13:19:48 -0400 | [diff] [blame] | 657 | if (*srcPixel != 0) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 658 | *dstPixel = *srcPixel; |
| 659 | } |
| 660 | dstPixel++; |
| 661 | srcPixel++; |
| 662 | } |
| 663 | break; |
| 664 | } |
| 665 | default: |
| 666 | SkASSERT(false); |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // Tell the frame to copy the row data if need be. |
| 672 | if (repeatCount > 1) { |
| 673 | const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().colorType()); |
| 674 | const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel; |
| 675 | void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes()); |
| 676 | void* dst = copiedLine; |
Leon Scroggins III | 249b8e3 | 2017-04-17 12:46:33 -0400 | [diff] [blame] | 677 | for (int i = 1; i < repeatCount; i++) { |
scroggo | 19b9153 | 2016-10-24 09:03:26 -0700 | [diff] [blame] | 678 | dst = SkTAddOffset<void>(dst, fDstRowBytes); |
| 679 | memcpy(dst, copiedLine, bytesToCopy); |
msarett | 72261c0 | 2015-11-19 15:29:26 -0800 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
| 683 | return true; |
| 684 | } |