blob: 682b69f81e69799e78ce7342c3aa70ae40e7487b [file] [log] [blame]
msarett8c8f22a2015-04-01 06:58:48 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
scroggo19b91532016-10-24 09:03:26 -07008/*
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"
msarett8c8f22a2015-04-01 06:58:48 -070034#include "SkCodecPriv.h"
35#include "SkColorPriv.h"
36#include "SkColorTable.h"
msarett1a464672016-01-07 13:17:19 -080037#include "SkGifCodec.h"
Mike Reedede7bac2017-07-23 15:30:02 -040038#include "SkMakeUnique.h"
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -040039#include "SkRasterPipeline.h"
msarett8c8f22a2015-04-01 06:58:48 -070040#include "SkStream.h"
41#include "SkSwizzler.h"
Mike Klein45c16fa2017-07-18 18:15:13 -040042#include "../jumper/SkJumper.h"
msarett8c8f22a2015-04-01 06:58:48 -070043
scroggo19b91532016-10-24 09:03:26 -070044#include <algorithm>
45
46#define GIF87_STAMP "GIF87a"
47#define GIF89_STAMP "GIF89a"
48#define GIF_STAMP_LEN 6
msarett39b2d5a2016-02-17 08:26:31 -080049
msarett8c8f22a2015-04-01 06:58:48 -070050/*
51 * Checks the start of the stream to see if the image is a gif
52 */
scroggodb30be22015-12-08 18:54:13 -080053bool SkGifCodec::IsGif(const void* buf, size_t bytesRead) {
54 if (bytesRead >= GIF_STAMP_LEN) {
scroggo19b91532016-10-24 09:03:26 -070055 if (memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
bungeman0153dea2015-08-27 16:43:42 -070056 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0)
57 {
msarett8c8f22a2015-04-01 06:58:48 -070058 return true;
59 }
60 }
61 return false;
62}
63
64/*
msarett8c8f22a2015-04-01 06:58:48 -070065 * Error function
66 */
bungeman0153dea2015-08-27 16:43:42 -070067static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCodec::kInvalidInput) {
msarett8c8f22a2015-04-01 06:58:48 -070068 SkCodecPrintf("Gif Error: %s\n", msg);
69 return result;
70}
71
Mike Reedede7bac2017-07-23 15:30:02 -040072std::unique_ptr<SkCodec> SkGifCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
73 Result* result) {
74 std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(std::move(stream)));
Leon Scroggins III588fb042017-07-14 16:32:31 -040075 *result = reader->parse(SkGifImageReader::SkGIFSizeQuery);
76 if (*result != kSuccess) {
scroggo19b91532016-10-24 09:03:26 -070077 return nullptr;
msarett8c8f22a2015-04-01 06:58:48 -070078 }
msarett8c8f22a2015-04-01 06:58:48 -070079
Leon Scroggins III4993b952016-12-08 11:54:04 -050080 // If no images are in the data, or the first header is not yet defined, we cannot
81 // create a codec. In either case, the width and height are not yet known.
Leon Scroggins IIIe726e7c2017-07-18 16:22:52 -040082 auto* frame = reader->frameContext(0);
83 if (!frame || !frame->isHeaderDefined()) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040084 *result = kInvalidInput;
scroggo19b91532016-10-24 09:03:26 -070085 return nullptr;
86 }
87
Leon Scroggins III4993b952016-12-08 11:54:04 -050088 // isHeaderDefined() will not return true if the screen size is empty.
89 SkASSERT(reader->screenHeight() > 0 && reader->screenWidth() > 0);
90
scroggo19b91532016-10-24 09:03:26 -070091 const auto alpha = reader->firstFrameHasAlpha() ? SkEncodedInfo::kBinary_Alpha
92 : SkEncodedInfo::kOpaque_Alpha;
93 // Use kPalette since Gifs are encoded with a color table.
94 // FIXME: Gifs can actually be encoded with 4-bits per pixel. Using 8 works, but we could skip
95 // expanding to 8 bits and take advantage of the SkSwizzler to work from 4.
96 const auto encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8);
97
scroggo19b91532016-10-24 09:03:26 -070098 // The choice of unpremul versus premul is arbitrary, since all colors are either fully
99 // opaque or fully transparent (i.e. kBinary), but we stored the transparent colors as all
100 // zeroes, which is arguably premultiplied.
101 const auto alphaType = reader->firstFrameHasAlpha() ? kUnpremul_SkAlphaType
102 : kOpaque_SkAlphaType;
Matt Sarett7f650bd2016-10-30 21:25:34 -0400103
scroggo19b91532016-10-24 09:03:26 -0700104 const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000105 kN32_SkColorType, alphaType,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500106 SkColorSpace::MakeSRGB());
Mike Reedede7bac2017-07-23 15:30:02 -0400107 return std::unique_ptr<SkCodec>(new SkGifCodec(encodedInfo, imageInfo, reader.release()));
scroggo19b91532016-10-24 09:03:26 -0700108}
msarett8c8f22a2015-04-01 06:58:48 -0700109
scroggob427db12015-08-12 07:24:13 -0700110bool SkGifCodec::onRewind() {
scroggo19b91532016-10-24 09:03:26 -0700111 fReader->clearDecodeState();
scroggob427db12015-08-12 07:24:13 -0700112 return true;
113}
114
scroggo19b91532016-10-24 09:03:26 -0700115SkGifCodec::SkGifCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo,
scroggo3d3a65c2016-10-24 12:28:30 -0700116 SkGifImageReader* reader)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400117 : INHERITED(encodedInfo, imageInfo, SkColorSpaceXform::kRGBA_8888_ColorFormat, nullptr)
scroggo19b91532016-10-24 09:03:26 -0700118 , fReader(reader)
119 , fTmpBuffer(nullptr)
120 , fSwizzler(nullptr)
121 , fCurrColorTable(nullptr)
122 , fCurrColorTableIsReal(false)
123 , fFilledBackground(false)
124 , fFirstCallToIncrementalDecode(false)
125 , fDst(nullptr)
126 , fDstRowBytes(0)
127 , fRowsDecoded(0)
128{
129 reader->setClient(this);
msarett8c8f22a2015-04-01 06:58:48 -0700130}
msarett10522ff2015-09-07 08:54:01 -0700131
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400132int SkGifCodec::onGetFrameCount() {
scroggof9acbe22016-10-25 12:43:21 -0700133 fReader->parse(SkGifImageReader::SkGIFFrameCountQuery);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400134 return fReader->imagesCount();
135}
136
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400137bool SkGifCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400138 if (i >= fReader->imagesCount()) {
139 return false;
msarett10522ff2015-09-07 08:54:01 -0700140 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400141
142 const SkGIFFrameContext* frameContext = fReader->frameContext(i);
Leon Scroggins IIIe726e7c2017-07-18 16:22:52 -0400143 SkASSERT(frameContext->reachedStartOfData());
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400144
145 if (frameInfo) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400146 frameInfo->fDuration = frameContext->getDuration();
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400147 frameInfo->fRequiredFrame = frameContext->getRequiredFrame();
148 frameInfo->fFullyReceived = frameContext->isComplete();
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400149#ifdef SK_LEGACY_FRAME_INFO_ALPHA_TYPE
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400150 frameInfo->fAlphaType = frameContext->hasAlpha() ? kUnpremul_SkAlphaType
151 : kOpaque_SkAlphaType;
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400152#endif
153 frameInfo->fAlpha = frameContext->hasAlpha() ? SkEncodedInfo::kBinary_Alpha
154 : SkEncodedInfo::kOpaque_Alpha;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400155 frameInfo->fDisposalMethod = frameContext->getDisposalMethod();
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400156 }
157 return true;
msarett10522ff2015-09-07 08:54:01 -0700158}
159
scroggoe71b1a12016-11-01 08:28:28 -0700160int SkGifCodec::onGetRepetitionCount() {
161 fReader->parse(SkGifImageReader::SkGIFLoopCountQuery);
162 return fReader->loopCount();
163}
164
Matt Sarett562e6812016-11-08 16:13:43 -0500165static const SkColorType kXformSrcColorType = kRGBA_8888_SkColorType;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400166static const SkAlphaType kXformAlphaType = kUnpremul_SkAlphaType;
Matt Sarett562e6812016-11-08 16:13:43 -0500167
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400168void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, int frameIndex) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400169 SkColorType colorTableColorType = dstInfo.colorType();
170 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500171 colorTableColorType = kXformSrcColorType;
Matt Sarett61eedeb2016-11-04 13:19:48 -0400172 }
173
174 sk_sp<SkColorTable> currColorTable = fReader->getColorTable(colorTableColorType, frameIndex);
Hal Canary9a0e3902017-07-12 11:59:17 -0400175 fCurrColorTableIsReal = static_cast<bool>(currColorTable);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400176 if (!fCurrColorTableIsReal) {
Leon Scroggins IIIa049ac42016-10-27 11:16:11 -0400177 // This is possible for an empty frame. Create a dummy with one value (transparent).
178 SkPMColor color = SK_ColorTRANSPARENT;
179 fCurrColorTable.reset(new SkColorTable(&color, 1));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400180 } else if (this->colorXform() && !this->xformOnDecode()) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400181 SkPMColor dstColors[256];
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400182 this->applyColorXform(dstColors, currColorTable->readColors(), currColorTable->count(),
183 kXformAlphaType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400184 fCurrColorTable.reset(new SkColorTable(dstColors, currColorTable->count()));
185 } else {
186 fCurrColorTable = std::move(currColorTable);
msarett10522ff2015-09-07 08:54:01 -0700187 }
msarett10522ff2015-09-07 08:54:01 -0700188}
189
scroggo19b91532016-10-24 09:03:26 -0700190
Leon Scroggins571b30f2017-07-11 17:35:31 +0000191SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, const Options& opts) {
scroggo19b91532016-10-24 09:03:26 -0700192 if (opts.fSubset) {
193 return gif_error("Subsets not supported.\n", kUnimplemented);
194 }
195
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400196 const int frameIndex = opts.fFrameIndex;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400197 if (frameIndex > 0 && kRGB_565_SkColorType == dstInfo.colorType()) {
198 // FIXME: In theory, we might be able to support this, but it's not clear that it
199 // is necessary (Chromium does not decode to 565, and Android does not decode
200 // frames beyond the first). Disabling it because it is somewhat difficult:
201 // - If there is a transparent pixel, and this frame draws on top of another frame
202 // (if the frame is independent with a transparent pixel, we should not decode to
203 // 565 anyway, since it is not opaque), we need to skip drawing the transparent
204 // pixels (see writeTransparentPixels in haveDecodedRow). We currently do this by
205 // first swizzling into temporary memory, then copying into the destination. (We
206 // let the swizzler handle it first because it may need to sample.) After
207 // swizzling to 565, we do not know which pixels in our temporary memory
208 // correspond to the transparent pixel, so we do not know what to skip. We could
209 // special case the non-sampled case (no need to swizzle), but as this is
210 // currently unused we can just not support it.
211 return gif_error("Cannot decode multiframe gif (except frame 0) as 565.\n",
212 kInvalidConversion);
scroggo19b91532016-10-24 09:03:26 -0700213 }
214
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400215 const auto* frame = fReader->frameContext(frameIndex);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400216 SkASSERT(frame);
217 if (0 == frameIndex) {
218 // SkCodec does not have a way to just parse through frame 0, so we
219 // have to do so manually, here.
220 fReader->parse((SkGifImageReader::SkGIFParseQuery) 0);
221 if (!frame->reachedStartOfData()) {
222 // We have parsed enough to know that there is a color map, but cannot
223 // parse the map itself yet. Exit now, so we do not build an incorrect
224 // table.
225 return gif_error("color map not available yet\n", kIncompleteInput);
226 }
227 } else {
228 // Parsing happened in SkCodec::getPixels.
229 SkASSERT(frameIndex < fReader->imagesCount());
230 SkASSERT(frame->reachedStartOfData());
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500231 }
232
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400233 if (this->xformOnDecode()) {
234 fXformBuffer.reset(new uint32_t[dstInfo.width()]);
235 sk_bzero(fXformBuffer.get(), dstInfo.width() * sizeof(uint32_t));
236 }
237
scroggo19b91532016-10-24 09:03:26 -0700238 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]);
239
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400240 this->initializeColorTable(dstInfo, frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700241 this->initializeSwizzler(dstInfo, frameIndex);
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400242
243 SkASSERT(fCurrColorTable);
msarettb30d6982016-02-15 10:18:45 -0800244 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700245}
246
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400247void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex) {
scroggof9acbe22016-10-25 12:43:21 -0700248 const SkGIFFrameContext* frame = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700249 // This is only called by prepareToDecode, which ensures frameIndex is in range.
250 SkASSERT(frame);
msarett10522ff2015-09-07 08:54:01 -0700251
scroggo19b91532016-10-24 09:03:26 -0700252 const int xBegin = frame->xOffset();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400253 const int xEnd = std::min(frame->frameRect().right(), fReader->screenWidth());
scroggo19b91532016-10-24 09:03:26 -0700254
255 // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw
256 // frameRect, since it might extend beyond the edge of the frame.
257 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0);
258
Matt Sarett61eedeb2016-11-04 13:19:48 -0400259 SkImageInfo swizzlerInfo = dstInfo;
260 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500261 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400262 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
263 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
264 }
265 }
266
scroggo19b91532016-10-24 09:03:26 -0700267 // The default Options should be fine:
268 // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't
269 // matter anyway.
270 // - subsets are not supported for gif
271 // - the swizzler does not need to know about the frame.
272 // We may not be able to use the real Options anyway, since getPixels does not store it (due to
273 // a bug).
274 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(),
Matt Sarett61eedeb2016-11-04 13:19:48 -0400275 fCurrColorTable->readColors(), swizzlerInfo, Options(), &swizzleRect));
scroggo19b91532016-10-24 09:03:26 -0700276 SkASSERT(fSwizzler.get());
msarett10522ff2015-09-07 08:54:01 -0700277}
278
279/*
280 * Initiates the gif decode
281 */
282SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo,
scroggo19b91532016-10-24 09:03:26 -0700283 void* pixels, size_t dstRowBytes,
msarett10522ff2015-09-07 08:54:01 -0700284 const Options& opts,
msarette6dd0042015-10-09 11:07:34 -0700285 int* rowsDecoded) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000286 Result result = this->prepareToDecode(dstInfo, opts);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500287 switch (result) {
288 case kSuccess:
289 break;
290 case kIncompleteInput:
291 // onStartIncrementalDecode treats this as incomplete, since it may
292 // provide more data later, but in this case, no more data will be
293 // provided, and there is nothing to draw. We also cannot return
294 // kIncompleteInput, which will make SkCodec attempt to fill
295 // remaining rows, but that requires an SkSwizzler, which we have
296 // not created.
297 return kInvalidInput;
298 default:
299 return result;
msarett10522ff2015-09-07 08:54:01 -0700300 }
301
302 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
303 return gif_error("Scaling not supported.\n", kInvalidScale);
304 }
305
scroggo19b91532016-10-24 09:03:26 -0700306 fDst = pixels;
307 fDstRowBytes = dstRowBytes;
308
309 return this->decodeFrame(true, opts, rowsDecoded);
310}
311
312SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
313 void* pixels, size_t dstRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000314 const SkCodec::Options& opts) {
315 Result result = this->prepareToDecode(dstInfo, opts);
scroggo19b91532016-10-24 09:03:26 -0700316 if (result != kSuccess) {
317 return result;
msarett10522ff2015-09-07 08:54:01 -0700318 }
319
scroggo19b91532016-10-24 09:03:26 -0700320 fDst = pixels;
321 fDstRowBytes = dstRowBytes;
322
323 fFirstCallToIncrementalDecode = true;
324
msarett10522ff2015-09-07 08:54:01 -0700325 return kSuccess;
326}
327
scroggo19b91532016-10-24 09:03:26 -0700328SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) {
329 // It is possible the client has appended more data. Parse, if needed.
330 const auto& options = this->options();
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400331 const int frameIndex = options.fFrameIndex;
scroggof9acbe22016-10-25 12:43:21 -0700332 fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700333
334 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode;
335 fFirstCallToIncrementalDecode = false;
336 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700337}
338
scroggo19b91532016-10-24 09:03:26 -0700339SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) {
340 const SkImageInfo& dstInfo = this->dstInfo();
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400341 const int frameIndex = opts.fFrameIndex;
scroggo19b91532016-10-24 09:03:26 -0700342 SkASSERT(frameIndex < fReader->imagesCount());
scroggof9acbe22016-10-25 12:43:21 -0700343 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700344 if (firstAttempt) {
345 // rowsDecoded reports how many rows have been initialized, so a layer above
346 // can fill the rest. In some cases, we fill the background before decoding
347 // (or it is already filled for us), so we report rowsDecoded to be the full
348 // height.
349 bool filledBackground = false;
350 if (frameContext->getRequiredFrame() == kNone) {
351 // We may need to clear to transparent for one of the following reasons:
352 // - The frameRect does not cover the full bounds. haveDecodedRow will
353 // only draw inside the frameRect, so we need to clear the rest.
scroggo19b91532016-10-24 09:03:26 -0700354 // - The frame is interlaced. There is no obvious way to fill
355 // afterwards for an incomplete image. (FIXME: Does the first pass
356 // cover all rows? If so, we do not have to fill here.)
scroggo8bce1172016-10-25 13:08:40 -0700357 // - There is no color table for this frame. In that case will not
358 // draw anything, so we need to fill.
scroggo19b91532016-10-24 09:03:26 -0700359 if (frameContext->frameRect() != this->getInfo().bounds()
scroggo8bce1172016-10-25 13:08:40 -0700360 || frameContext->interlaced() || !fCurrColorTableIsReal) {
scroggo19b91532016-10-24 09:03:26 -0700361 // fill ignores the width (replaces it with the actual, scaled width).
362 // But we need to scale in Y.
363 const int scaledHeight = get_scaled_dimension(dstInfo.height(),
364 fSwizzler->sampleY());
365 auto fillInfo = dstInfo.makeWH(0, scaledHeight);
366 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue(dstInfo),
367 opts.fZeroInitialized);
368 filledBackground = true;
369 }
370 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400371 // Not independent.
372 // SkCodec ensured that the prior frame has been decoded.
scroggo19b91532016-10-24 09:03:26 -0700373 filledBackground = true;
msarett10522ff2015-09-07 08:54:01 -0700374 }
scroggo19b91532016-10-24 09:03:26 -0700375
376 fFilledBackground = filledBackground;
377 if (filledBackground) {
378 // Report the full (scaled) height, since the client will never need to fill.
379 fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY());
380 } else {
381 // This will be updated by haveDecodedRow.
382 fRowsDecoded = 0;
383 }
msarett10522ff2015-09-07 08:54:01 -0700384 }
msarette6dd0042015-10-09 11:07:34 -0700385
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500386 if (!fCurrColorTableIsReal) {
387 // Nothing to draw this frame.
388 return kSuccess;
389 }
390
scroggo19b91532016-10-24 09:03:26 -0700391 bool frameDecoded = false;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400392 const bool fatalError = !fReader->decode(frameIndex, &frameDecoded);
393 if (fatalError || !frameDecoded) {
scroggo19b91532016-10-24 09:03:26 -0700394 if (rowsDecoded) {
395 *rowsDecoded = fRowsDecoded;
396 }
Leon Scroggins III674a1842017-07-06 12:26:09 -0400397 if (fatalError) {
398 return kErrorInInput;
399 }
scroggo19b91532016-10-24 09:03:26 -0700400 return kIncompleteInput;
401 }
402
403 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700404}
scroggo46c57472015-09-30 08:57:13 -0700405
scroggo19b91532016-10-24 09:03:26 -0700406uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
scroggo19b91532016-10-24 09:03:26 -0700407 // Using transparent as the fill value matches the behavior in Chromium,
408 // which ignores the background color.
scroggo19b91532016-10-24 09:03:26 -0700409 return SK_ColorTRANSPARENT;
410}
msarett72261c02015-11-19 15:29:26 -0800411
Matt Sarett61eedeb2016-11-04 13:19:48 -0400412void SkGifCodec::applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400413 if (this->xformOnDecode()) {
414 SkASSERT(this->colorXform());
Matt Sarett61eedeb2016-11-04 13:19:48 -0400415 fSwizzler->swizzle(fXformBuffer.get(), src);
416
Matt Sarett61eedeb2016-11-04 13:19:48 -0400417 const int xformWidth = get_scaled_dimension(dstInfo.width(), fSwizzler->sampleX());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400418 this->applyColorXform(dst, fXformBuffer.get(), xformWidth, kXformAlphaType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400419 } else {
420 fSwizzler->swizzle(dst, src);
421 }
422}
423
Leon Scroggins III223ec292017-08-22 14:13:15 -0400424void SkGifCodec::haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400425 int rowNumber, int repeatCount, bool writeTransparentPixels)
scroggo19b91532016-10-24 09:03:26 -0700426{
scroggof9acbe22016-10-25 12:43:21 -0700427 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700428 // The pixel data and coordinates supplied to us are relative to the frame's
429 // origin within the entire image size, i.e.
430 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee
431 // that width == (size().width() - frameContext->xOffset), so
432 // we must ensure we don't run off the end of either the source data or the
433 // row's X-coordinates.
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400434 const int width = frameContext->width();
scroggo19b91532016-10-24 09:03:26 -0700435 const int xBegin = frameContext->xOffset();
436 const int yBegin = frameContext->yOffset() + rowNumber;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400437 const int xEnd = std::min(xBegin + width, this->getInfo().width());
438 const int yEnd = std::min(yBegin + rowNumber + repeatCount, this->getInfo().height());
scroggo19b91532016-10-24 09:03:26 -0700439 // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do
440 // this once in prepareToDecode.
441 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin))
Leon Scroggins III223ec292017-08-22 14:13:15 -0400442 return;
scroggo19b91532016-10-24 09:03:26 -0700443
444 // yBegin is the first row in the non-sampled image. dstRow will be the row in the output,
445 // after potentially scaling it.
446 int dstRow = yBegin;
447
448 const int sampleY = fSwizzler->sampleY();
449 if (sampleY > 1) {
450 // Check to see whether this row or one that falls in the repeatCount is needed in the
451 // output.
452 bool foundNecessaryRow = false;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400453 for (int i = 0; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700454 const int potentialRow = yBegin + i;
455 if (fSwizzler->rowNeeded(potentialRow)) {
456 dstRow = potentialRow / sampleY;
457 const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY);
458 if (dstRow >= scaledHeight) {
Leon Scroggins III223ec292017-08-22 14:13:15 -0400459 return;
scroggo19b91532016-10-24 09:03:26 -0700460 }
461
462 foundNecessaryRow = true;
463 repeatCount -= i;
464
465 repeatCount = (repeatCount - 1) / sampleY + 1;
466
467 // Make sure the repeatCount does not take us beyond the end of the dst
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400468 if (dstRow + repeatCount > scaledHeight) {
scroggo19b91532016-10-24 09:03:26 -0700469 repeatCount = scaledHeight - dstRow;
470 SkASSERT(repeatCount >= 1);
471 }
472 break;
473 }
474 }
475
476 if (!foundNecessaryRow) {
Leon Scroggins III223ec292017-08-22 14:13:15 -0400477 return;
scroggo19b91532016-10-24 09:03:26 -0700478 }
Matt Sarett8a4e9c52016-10-25 14:24:50 -0400479 } else {
480 // Make sure the repeatCount does not take us beyond the end of the dst
481 SkASSERT(this->dstInfo().height() >= yBegin);
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400482 repeatCount = SkTMin(repeatCount, this->dstInfo().height() - yBegin);
scroggo19b91532016-10-24 09:03:26 -0700483 }
484
485 if (!fFilledBackground) {
486 // At this point, we are definitely going to write the row, so count it towards the number
487 // of rows decoded.
488 // We do not consider the repeatCount, which only happens for interlaced, in which case we
489 // have already set fRowsDecoded to the proper value (reflecting that we have filled the
490 // background).
491 fRowsDecoded++;
492 }
493
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500494 // decodeFrame will early exit if this is false, so this method will not be
495 // called.
496 SkASSERT(fCurrColorTableIsReal);
scroggo19b91532016-10-24 09:03:26 -0700497
498 // The swizzler takes care of offsetting into the dst width-wise.
499 void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes);
500
501 // We may or may not need to write transparent pixels to the buffer.
scroggo1285f412016-10-26 13:48:03 -0700502 // If we're compositing against a previous image, it's wrong, but if
503 // we're decoding an interlaced gif and displaying it "Haeberli"-style,
504 // we must write these for passes beyond the first, or the initial passes
505 // will "show through" the later ones.
scroggo19b91532016-10-24 09:03:26 -0700506 const auto dstInfo = this->dstInfo();
scroggo53f63b62016-10-27 08:29:13 -0700507 if (writeTransparentPixels) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400508 this->applyXformRow(dstInfo, dstLine, rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700509 } else {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400510 this->applyXformRow(dstInfo, fTmpBuffer.get(), rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700511
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400512 size_t offsetBytes = fSwizzler->swizzleOffsetBytes();
513 if (dstInfo.colorType() == kRGBA_F16_SkColorType) {
514 // Account for the fact that post-swizzling we converted to F16,
515 // which is twice as wide.
516 offsetBytes *= 2;
517 }
518 SkRasterPipeline_<256> p;
519 SkRasterPipeline::StockStage storeDst;
520 void* src = SkTAddOffset<void>(fTmpBuffer.get(), offsetBytes);
521 void* dst = SkTAddOffset<void>(dstLine, offsetBytes);
Mike Klein45c16fa2017-07-18 18:15:13 -0400522
523 SkJumper_MemoryCtx src_ctx = { src, 0 },
524 dst_ctx = { dst, 0 };
scroggo19b91532016-10-24 09:03:26 -0700525 switch (dstInfo.colorType()) {
526 case kBGRA_8888_SkColorType:
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400527 case kRGBA_8888_SkColorType:
Mike Klein45c16fa2017-07-18 18:15:13 -0400528 p.append(SkRasterPipeline::load_8888_dst, &dst_ctx);
529 p.append(SkRasterPipeline::load_8888, &src_ctx);
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400530 storeDst = SkRasterPipeline::store_8888;
scroggo19b91532016-10-24 09:03:26 -0700531 break;
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400532 case kRGBA_F16_SkColorType:
Mike Klein45c16fa2017-07-18 18:15:13 -0400533 p.append(SkRasterPipeline::load_f16_dst, &dst_ctx);
534 p.append(SkRasterPipeline::load_f16, &src_ctx);
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400535 storeDst = SkRasterPipeline::store_f16;
scroggo19b91532016-10-24 09:03:26 -0700536 break;
scroggo19b91532016-10-24 09:03:26 -0700537 default:
538 SkASSERT(false);
Leon Scroggins III223ec292017-08-22 14:13:15 -0400539 return;
scroggo19b91532016-10-24 09:03:26 -0700540 }
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400541 p.append(SkRasterPipeline::srcover);
542 p.append(storeDst, &dst);
Mike Klein45c16fa2017-07-18 18:15:13 -0400543 p.run(0,0, fSwizzler->swizzleWidth(),1);
scroggo19b91532016-10-24 09:03:26 -0700544 }
545
546 // Tell the frame to copy the row data if need be.
547 if (repeatCount > 1) {
548 const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().colorType());
549 const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel;
550 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes());
551 void* dst = copiedLine;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400552 for (int i = 1; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700553 dst = SkTAddOffset<void>(dst, fDstRowBytes);
554 memcpy(dst, copiedLine, bytesToCopy);
msarett72261c02015-11-19 15:29:26 -0800555 }
556 }
msarett72261c02015-11-19 15:29:26 -0800557}