blob: df472b9ee7045ea31ea6f44936b8665f889f2eb5 [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"
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -040038#include "SkRasterPipeline.h"
msarett8c8f22a2015-04-01 06:58:48 -070039#include "SkStream.h"
40#include "SkSwizzler.h"
msarett8c8f22a2015-04-01 06:58:48 -070041
scroggo19b91532016-10-24 09:03:26 -070042#include <algorithm>
43
44#define GIF87_STAMP "GIF87a"
45#define GIF89_STAMP "GIF89a"
46#define GIF_STAMP_LEN 6
msarett39b2d5a2016-02-17 08:26:31 -080047
msarett8c8f22a2015-04-01 06:58:48 -070048/*
49 * Checks the start of the stream to see if the image is a gif
50 */
scroggodb30be22015-12-08 18:54:13 -080051bool SkGifCodec::IsGif(const void* buf, size_t bytesRead) {
52 if (bytesRead >= GIF_STAMP_LEN) {
scroggo19b91532016-10-24 09:03:26 -070053 if (memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
bungeman0153dea2015-08-27 16:43:42 -070054 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0)
55 {
msarett8c8f22a2015-04-01 06:58:48 -070056 return true;
57 }
58 }
59 return false;
60}
61
62/*
msarett8c8f22a2015-04-01 06:58:48 -070063 * Error function
64 */
bungeman0153dea2015-08-27 16:43:42 -070065static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCodec::kInvalidInput) {
msarett8c8f22a2015-04-01 06:58:48 -070066 SkCodecPrintf("Gif Error: %s\n", msg);
67 return result;
68}
69
Leon Scroggins III588fb042017-07-14 16:32:31 -040070SkCodec* SkGifCodec::NewFromStream(SkStream* stream, Result* result) {
scroggo3d3a65c2016-10-24 12:28:30 -070071 std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(stream));
Leon Scroggins III588fb042017-07-14 16:32:31 -040072 *result = reader->parse(SkGifImageReader::SkGIFSizeQuery);
73 if (*result != kSuccess) {
scroggo19b91532016-10-24 09:03:26 -070074 return nullptr;
msarett8c8f22a2015-04-01 06:58:48 -070075 }
msarett8c8f22a2015-04-01 06:58:48 -070076
Leon Scroggins III4993b952016-12-08 11:54:04 -050077 // If no images are in the data, or the first header is not yet defined, we cannot
78 // create a codec. In either case, the width and height are not yet known.
Leon Scroggins IIIe726e7c2017-07-18 16:22:52 -040079 auto* frame = reader->frameContext(0);
80 if (!frame || !frame->isHeaderDefined()) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040081 *result = kInvalidInput;
scroggo19b91532016-10-24 09:03:26 -070082 return nullptr;
83 }
84
Leon Scroggins III4993b952016-12-08 11:54:04 -050085 // isHeaderDefined() will not return true if the screen size is empty.
86 SkASSERT(reader->screenHeight() > 0 && reader->screenWidth() > 0);
87
scroggo19b91532016-10-24 09:03:26 -070088 const auto alpha = reader->firstFrameHasAlpha() ? SkEncodedInfo::kBinary_Alpha
89 : SkEncodedInfo::kOpaque_Alpha;
90 // Use kPalette since Gifs are encoded with a color table.
91 // FIXME: Gifs can actually be encoded with 4-bits per pixel. Using 8 works, but we could skip
92 // expanding to 8 bits and take advantage of the SkSwizzler to work from 4.
93 const auto encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8);
94
scroggo19b91532016-10-24 09:03:26 -070095 // The choice of unpremul versus premul is arbitrary, since all colors are either fully
96 // opaque or fully transparent (i.e. kBinary), but we stored the transparent colors as all
97 // zeroes, which is arguably premultiplied.
98 const auto alphaType = reader->firstFrameHasAlpha() ? kUnpremul_SkAlphaType
99 : kOpaque_SkAlphaType;
Matt Sarett7f650bd2016-10-30 21:25:34 -0400100
scroggo19b91532016-10-24 09:03:26 -0700101 const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(),
Leon Scroggins571b30f2017-07-11 17:35:31 +0000102 kN32_SkColorType, alphaType,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500103 SkColorSpace::MakeSRGB());
scroggo19b91532016-10-24 09:03:26 -0700104 return new SkGifCodec(encodedInfo, imageInfo, reader.release());
105}
msarett8c8f22a2015-04-01 06:58:48 -0700106
scroggob427db12015-08-12 07:24:13 -0700107bool SkGifCodec::onRewind() {
scroggo19b91532016-10-24 09:03:26 -0700108 fReader->clearDecodeState();
scroggob427db12015-08-12 07:24:13 -0700109 return true;
110}
111
scroggo19b91532016-10-24 09:03:26 -0700112SkGifCodec::SkGifCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo,
scroggo3d3a65c2016-10-24 12:28:30 -0700113 SkGifImageReader* reader)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400114 : INHERITED(encodedInfo, imageInfo, SkColorSpaceXform::kRGBA_8888_ColorFormat, nullptr)
scroggo19b91532016-10-24 09:03:26 -0700115 , fReader(reader)
116 , fTmpBuffer(nullptr)
117 , fSwizzler(nullptr)
118 , fCurrColorTable(nullptr)
119 , fCurrColorTableIsReal(false)
120 , fFilledBackground(false)
121 , fFirstCallToIncrementalDecode(false)
122 , fDst(nullptr)
123 , fDstRowBytes(0)
124 , fRowsDecoded(0)
125{
126 reader->setClient(this);
msarett8c8f22a2015-04-01 06:58:48 -0700127}
msarett10522ff2015-09-07 08:54:01 -0700128
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400129int SkGifCodec::onGetFrameCount() {
scroggof9acbe22016-10-25 12:43:21 -0700130 fReader->parse(SkGifImageReader::SkGIFFrameCountQuery);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400131 return fReader->imagesCount();
132}
133
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400134bool SkGifCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400135 if (i >= fReader->imagesCount()) {
136 return false;
msarett10522ff2015-09-07 08:54:01 -0700137 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400138
139 const SkGIFFrameContext* frameContext = fReader->frameContext(i);
Leon Scroggins IIIe726e7c2017-07-18 16:22:52 -0400140 SkASSERT(frameContext->reachedStartOfData());
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400141
142 if (frameInfo) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400143 frameInfo->fDuration = frameContext->getDuration();
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400144 frameInfo->fRequiredFrame = frameContext->getRequiredFrame();
145 frameInfo->fFullyReceived = frameContext->isComplete();
146 frameInfo->fAlphaType = frameContext->hasAlpha() ? kUnpremul_SkAlphaType
147 : kOpaque_SkAlphaType;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400148 frameInfo->fDisposalMethod = frameContext->getDisposalMethod();
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400149 }
150 return true;
msarett10522ff2015-09-07 08:54:01 -0700151}
152
scroggoe71b1a12016-11-01 08:28:28 -0700153int SkGifCodec::onGetRepetitionCount() {
154 fReader->parse(SkGifImageReader::SkGIFLoopCountQuery);
155 return fReader->loopCount();
156}
157
Matt Sarett562e6812016-11-08 16:13:43 -0500158static const SkColorType kXformSrcColorType = kRGBA_8888_SkColorType;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400159static const SkAlphaType kXformAlphaType = kUnpremul_SkAlphaType;
Matt Sarett562e6812016-11-08 16:13:43 -0500160
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400161void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, int frameIndex) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400162 SkColorType colorTableColorType = dstInfo.colorType();
163 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500164 colorTableColorType = kXformSrcColorType;
Matt Sarett61eedeb2016-11-04 13:19:48 -0400165 }
166
167 sk_sp<SkColorTable> currColorTable = fReader->getColorTable(colorTableColorType, frameIndex);
Hal Canary9a0e3902017-07-12 11:59:17 -0400168 fCurrColorTableIsReal = static_cast<bool>(currColorTable);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400169 if (!fCurrColorTableIsReal) {
Leon Scroggins IIIa049ac42016-10-27 11:16:11 -0400170 // This is possible for an empty frame. Create a dummy with one value (transparent).
171 SkPMColor color = SK_ColorTRANSPARENT;
172 fCurrColorTable.reset(new SkColorTable(&color, 1));
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400173 } else if (this->colorXform() && !this->xformOnDecode()) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400174 SkPMColor dstColors[256];
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400175 this->applyColorXform(dstColors, currColorTable->readColors(), currColorTable->count(),
176 kXformAlphaType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400177 fCurrColorTable.reset(new SkColorTable(dstColors, currColorTable->count()));
178 } else {
179 fCurrColorTable = std::move(currColorTable);
msarett10522ff2015-09-07 08:54:01 -0700180 }
msarett10522ff2015-09-07 08:54:01 -0700181}
182
scroggo19b91532016-10-24 09:03:26 -0700183
Leon Scroggins571b30f2017-07-11 17:35:31 +0000184SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, const Options& opts) {
scroggo19b91532016-10-24 09:03:26 -0700185 if (opts.fSubset) {
186 return gif_error("Subsets not supported.\n", kUnimplemented);
187 }
188
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400189 const int frameIndex = opts.fFrameIndex;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400190 if (frameIndex > 0 && kRGB_565_SkColorType == dstInfo.colorType()) {
191 // FIXME: In theory, we might be able to support this, but it's not clear that it
192 // is necessary (Chromium does not decode to 565, and Android does not decode
193 // frames beyond the first). Disabling it because it is somewhat difficult:
194 // - If there is a transparent pixel, and this frame draws on top of another frame
195 // (if the frame is independent with a transparent pixel, we should not decode to
196 // 565 anyway, since it is not opaque), we need to skip drawing the transparent
197 // pixels (see writeTransparentPixels in haveDecodedRow). We currently do this by
198 // first swizzling into temporary memory, then copying into the destination. (We
199 // let the swizzler handle it first because it may need to sample.) After
200 // swizzling to 565, we do not know which pixels in our temporary memory
201 // correspond to the transparent pixel, so we do not know what to skip. We could
202 // special case the non-sampled case (no need to swizzle), but as this is
203 // currently unused we can just not support it.
204 return gif_error("Cannot decode multiframe gif (except frame 0) as 565.\n",
205 kInvalidConversion);
scroggo19b91532016-10-24 09:03:26 -0700206 }
207
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400208 const auto* frame = fReader->frameContext(frameIndex);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400209 SkASSERT(frame);
210 if (0 == frameIndex) {
211 // SkCodec does not have a way to just parse through frame 0, so we
212 // have to do so manually, here.
213 fReader->parse((SkGifImageReader::SkGIFParseQuery) 0);
214 if (!frame->reachedStartOfData()) {
215 // We have parsed enough to know that there is a color map, but cannot
216 // parse the map itself yet. Exit now, so we do not build an incorrect
217 // table.
218 return gif_error("color map not available yet\n", kIncompleteInput);
219 }
220 } else {
221 // Parsing happened in SkCodec::getPixels.
222 SkASSERT(frameIndex < fReader->imagesCount());
223 SkASSERT(frame->reachedStartOfData());
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500224 }
225
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400226 const auto at = frame->hasAlpha() ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
227 const auto srcInfo = this->getInfo().makeAlphaType(at);
228 if (!conversion_possible(dstInfo, srcInfo) ||
229 !this->initializeColorXform(dstInfo, opts.fPremulBehavior))
230 {
231 return gif_error("Cannot convert input type to output type.\n", kInvalidConversion);
232 }
233
234 if (this->xformOnDecode()) {
235 fXformBuffer.reset(new uint32_t[dstInfo.width()]);
236 sk_bzero(fXformBuffer.get(), dstInfo.width() * sizeof(uint32_t));
237 }
238
scroggo19b91532016-10-24 09:03:26 -0700239 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]);
240
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400241 this->initializeColorTable(dstInfo, frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700242 this->initializeSwizzler(dstInfo, frameIndex);
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400243
244 SkASSERT(fCurrColorTable);
msarettb30d6982016-02-15 10:18:45 -0800245 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700246}
247
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400248void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex) {
scroggof9acbe22016-10-25 12:43:21 -0700249 const SkGIFFrameContext* frame = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700250 // This is only called by prepareToDecode, which ensures frameIndex is in range.
251 SkASSERT(frame);
msarett10522ff2015-09-07 08:54:01 -0700252
scroggo19b91532016-10-24 09:03:26 -0700253 const int xBegin = frame->xOffset();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400254 const int xEnd = std::min(frame->frameRect().right(), fReader->screenWidth());
scroggo19b91532016-10-24 09:03:26 -0700255
256 // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw
257 // frameRect, since it might extend beyond the edge of the frame.
258 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0);
259
Matt Sarett61eedeb2016-11-04 13:19:48 -0400260 SkImageInfo swizzlerInfo = dstInfo;
261 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500262 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400263 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
264 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
265 }
266 }
267
scroggo19b91532016-10-24 09:03:26 -0700268 // The default Options should be fine:
269 // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't
270 // matter anyway.
271 // - subsets are not supported for gif
272 // - the swizzler does not need to know about the frame.
273 // We may not be able to use the real Options anyway, since getPixels does not store it (due to
274 // a bug).
275 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(),
Matt Sarett61eedeb2016-11-04 13:19:48 -0400276 fCurrColorTable->readColors(), swizzlerInfo, Options(), &swizzleRect));
scroggo19b91532016-10-24 09:03:26 -0700277 SkASSERT(fSwizzler.get());
msarett10522ff2015-09-07 08:54:01 -0700278}
279
280/*
281 * Initiates the gif decode
282 */
283SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo,
scroggo19b91532016-10-24 09:03:26 -0700284 void* pixels, size_t dstRowBytes,
msarett10522ff2015-09-07 08:54:01 -0700285 const Options& opts,
msarette6dd0042015-10-09 11:07:34 -0700286 int* rowsDecoded) {
Leon Scroggins571b30f2017-07-11 17:35:31 +0000287 Result result = this->prepareToDecode(dstInfo, opts);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500288 switch (result) {
289 case kSuccess:
290 break;
291 case kIncompleteInput:
292 // onStartIncrementalDecode treats this as incomplete, since it may
293 // provide more data later, but in this case, no more data will be
294 // provided, and there is nothing to draw. We also cannot return
295 // kIncompleteInput, which will make SkCodec attempt to fill
296 // remaining rows, but that requires an SkSwizzler, which we have
297 // not created.
298 return kInvalidInput;
299 default:
300 return result;
msarett10522ff2015-09-07 08:54:01 -0700301 }
302
303 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
304 return gif_error("Scaling not supported.\n", kInvalidScale);
305 }
306
scroggo19b91532016-10-24 09:03:26 -0700307 fDst = pixels;
308 fDstRowBytes = dstRowBytes;
309
310 return this->decodeFrame(true, opts, rowsDecoded);
311}
312
313SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
314 void* pixels, size_t dstRowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000315 const SkCodec::Options& opts) {
316 Result result = this->prepareToDecode(dstInfo, opts);
scroggo19b91532016-10-24 09:03:26 -0700317 if (result != kSuccess) {
318 return result;
msarett10522ff2015-09-07 08:54:01 -0700319 }
320
scroggo19b91532016-10-24 09:03:26 -0700321 fDst = pixels;
322 fDstRowBytes = dstRowBytes;
323
324 fFirstCallToIncrementalDecode = true;
325
msarett10522ff2015-09-07 08:54:01 -0700326 return kSuccess;
327}
328
scroggo19b91532016-10-24 09:03:26 -0700329SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) {
330 // It is possible the client has appended more data. Parse, if needed.
331 const auto& options = this->options();
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400332 const int frameIndex = options.fFrameIndex;
scroggof9acbe22016-10-25 12:43:21 -0700333 fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700334
335 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode;
336 fFirstCallToIncrementalDecode = false;
337 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700338}
339
scroggo19b91532016-10-24 09:03:26 -0700340SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) {
341 const SkImageInfo& dstInfo = this->dstInfo();
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400342 const int frameIndex = opts.fFrameIndex;
scroggo19b91532016-10-24 09:03:26 -0700343 SkASSERT(frameIndex < fReader->imagesCount());
scroggof9acbe22016-10-25 12:43:21 -0700344 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700345 if (firstAttempt) {
346 // rowsDecoded reports how many rows have been initialized, so a layer above
347 // can fill the rest. In some cases, we fill the background before decoding
348 // (or it is already filled for us), so we report rowsDecoded to be the full
349 // height.
350 bool filledBackground = false;
351 if (frameContext->getRequiredFrame() == kNone) {
352 // We may need to clear to transparent for one of the following reasons:
353 // - The frameRect does not cover the full bounds. haveDecodedRow will
354 // only draw inside the frameRect, so we need to clear the rest.
scroggo19b91532016-10-24 09:03:26 -0700355 // - The frame is interlaced. There is no obvious way to fill
356 // afterwards for an incomplete image. (FIXME: Does the first pass
357 // cover all rows? If so, we do not have to fill here.)
scroggo8bce1172016-10-25 13:08:40 -0700358 // - There is no color table for this frame. In that case will not
359 // draw anything, so we need to fill.
scroggo19b91532016-10-24 09:03:26 -0700360 if (frameContext->frameRect() != this->getInfo().bounds()
scroggo8bce1172016-10-25 13:08:40 -0700361 || frameContext->interlaced() || !fCurrColorTableIsReal) {
scroggo19b91532016-10-24 09:03:26 -0700362 // fill ignores the width (replaces it with the actual, scaled width).
363 // But we need to scale in Y.
364 const int scaledHeight = get_scaled_dimension(dstInfo.height(),
365 fSwizzler->sampleY());
366 auto fillInfo = dstInfo.makeWH(0, scaledHeight);
367 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue(dstInfo),
368 opts.fZeroInitialized);
369 filledBackground = true;
370 }
371 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400372 // Not independent.
373 // SkCodec ensured that the prior frame has been decoded.
scroggo19b91532016-10-24 09:03:26 -0700374 filledBackground = true;
msarett10522ff2015-09-07 08:54:01 -0700375 }
scroggo19b91532016-10-24 09:03:26 -0700376
377 fFilledBackground = filledBackground;
378 if (filledBackground) {
379 // Report the full (scaled) height, since the client will never need to fill.
380 fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY());
381 } else {
382 // This will be updated by haveDecodedRow.
383 fRowsDecoded = 0;
384 }
msarett10522ff2015-09-07 08:54:01 -0700385 }
msarette6dd0042015-10-09 11:07:34 -0700386
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500387 if (!fCurrColorTableIsReal) {
388 // Nothing to draw this frame.
389 return kSuccess;
390 }
391
scroggo19b91532016-10-24 09:03:26 -0700392 bool frameDecoded = false;
Leon Scroggins III674a1842017-07-06 12:26:09 -0400393 const bool fatalError = !fReader->decode(frameIndex, &frameDecoded);
394 if (fatalError || !frameDecoded) {
scroggo19b91532016-10-24 09:03:26 -0700395 if (rowsDecoded) {
396 *rowsDecoded = fRowsDecoded;
397 }
Leon Scroggins III674a1842017-07-06 12:26:09 -0400398 if (fatalError) {
399 return kErrorInInput;
400 }
scroggo19b91532016-10-24 09:03:26 -0700401 return kIncompleteInput;
402 }
403
404 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700405}
scroggo46c57472015-09-30 08:57:13 -0700406
scroggo19b91532016-10-24 09:03:26 -0700407uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
scroggo19b91532016-10-24 09:03:26 -0700408 // Using transparent as the fill value matches the behavior in Chromium,
409 // which ignores the background color.
scroggo19b91532016-10-24 09:03:26 -0700410 return SK_ColorTRANSPARENT;
411}
msarett72261c02015-11-19 15:29:26 -0800412
Matt Sarett61eedeb2016-11-04 13:19:48 -0400413void SkGifCodec::applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400414 if (this->xformOnDecode()) {
415 SkASSERT(this->colorXform());
Matt Sarett61eedeb2016-11-04 13:19:48 -0400416 fSwizzler->swizzle(fXformBuffer.get(), src);
417
Matt Sarett61eedeb2016-11-04 13:19:48 -0400418 const int xformWidth = get_scaled_dimension(dstInfo.width(), fSwizzler->sampleX());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400419 this->applyColorXform(dst, fXformBuffer.get(), xformWidth, kXformAlphaType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400420 } else {
421 fSwizzler->swizzle(dst, src);
422 }
423}
424
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400425bool SkGifCodec::haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
426 int rowNumber, int repeatCount, bool writeTransparentPixels)
scroggo19b91532016-10-24 09:03:26 -0700427{
scroggof9acbe22016-10-25 12:43:21 -0700428 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700429 // The pixel data and coordinates supplied to us are relative to the frame's
430 // origin within the entire image size, i.e.
431 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee
432 // that width == (size().width() - frameContext->xOffset), so
433 // we must ensure we don't run off the end of either the source data or the
434 // row's X-coordinates.
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400435 const int width = frameContext->width();
scroggo19b91532016-10-24 09:03:26 -0700436 const int xBegin = frameContext->xOffset();
437 const int yBegin = frameContext->yOffset() + rowNumber;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400438 const int xEnd = std::min(xBegin + width, this->getInfo().width());
439 const int yEnd = std::min(yBegin + rowNumber + repeatCount, this->getInfo().height());
scroggo19b91532016-10-24 09:03:26 -0700440 // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do
441 // this once in prepareToDecode.
442 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin))
443 return true;
444
445 // yBegin is the first row in the non-sampled image. dstRow will be the row in the output,
446 // after potentially scaling it.
447 int dstRow = yBegin;
448
449 const int sampleY = fSwizzler->sampleY();
450 if (sampleY > 1) {
451 // Check to see whether this row or one that falls in the repeatCount is needed in the
452 // output.
453 bool foundNecessaryRow = false;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400454 for (int i = 0; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700455 const int potentialRow = yBegin + i;
456 if (fSwizzler->rowNeeded(potentialRow)) {
457 dstRow = potentialRow / sampleY;
458 const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY);
459 if (dstRow >= scaledHeight) {
460 return true;
461 }
462
463 foundNecessaryRow = true;
464 repeatCount -= i;
465
466 repeatCount = (repeatCount - 1) / sampleY + 1;
467
468 // Make sure the repeatCount does not take us beyond the end of the dst
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400469 if (dstRow + repeatCount > scaledHeight) {
scroggo19b91532016-10-24 09:03:26 -0700470 repeatCount = scaledHeight - dstRow;
471 SkASSERT(repeatCount >= 1);
472 }
473 break;
474 }
475 }
476
477 if (!foundNecessaryRow) {
478 return true;
479 }
Matt Sarett8a4e9c52016-10-25 14:24:50 -0400480 } else {
481 // Make sure the repeatCount does not take us beyond the end of the dst
482 SkASSERT(this->dstInfo().height() >= yBegin);
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400483 repeatCount = SkTMin(repeatCount, this->dstInfo().height() - yBegin);
scroggo19b91532016-10-24 09:03:26 -0700484 }
485
486 if (!fFilledBackground) {
487 // At this point, we are definitely going to write the row, so count it towards the number
488 // of rows decoded.
489 // We do not consider the repeatCount, which only happens for interlaced, in which case we
490 // have already set fRowsDecoded to the proper value (reflecting that we have filled the
491 // background).
492 fRowsDecoded++;
493 }
494
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500495 // decodeFrame will early exit if this is false, so this method will not be
496 // called.
497 SkASSERT(fCurrColorTableIsReal);
scroggo19b91532016-10-24 09:03:26 -0700498
499 // The swizzler takes care of offsetting into the dst width-wise.
500 void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes);
501
502 // We may or may not need to write transparent pixels to the buffer.
scroggo1285f412016-10-26 13:48:03 -0700503 // If we're compositing against a previous image, it's wrong, but if
504 // we're decoding an interlaced gif and displaying it "Haeberli"-style,
505 // we must write these for passes beyond the first, or the initial passes
506 // will "show through" the later ones.
scroggo19b91532016-10-24 09:03:26 -0700507 const auto dstInfo = this->dstInfo();
scroggo53f63b62016-10-27 08:29:13 -0700508 if (writeTransparentPixels) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400509 this->applyXformRow(dstInfo, dstLine, rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700510 } else {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400511 this->applyXformRow(dstInfo, fTmpBuffer.get(), rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700512
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400513 size_t offsetBytes = fSwizzler->swizzleOffsetBytes();
514 if (dstInfo.colorType() == kRGBA_F16_SkColorType) {
515 // Account for the fact that post-swizzling we converted to F16,
516 // which is twice as wide.
517 offsetBytes *= 2;
518 }
519 SkRasterPipeline_<256> p;
520 SkRasterPipeline::StockStage storeDst;
521 void* src = SkTAddOffset<void>(fTmpBuffer.get(), offsetBytes);
522 void* dst = SkTAddOffset<void>(dstLine, offsetBytes);
scroggo19b91532016-10-24 09:03:26 -0700523 switch (dstInfo.colorType()) {
524 case kBGRA_8888_SkColorType:
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400525 case kRGBA_8888_SkColorType:
526 p.append(SkRasterPipeline::load_8888_dst, &dst);
527 p.append(SkRasterPipeline::load_8888, &src);
528 storeDst = SkRasterPipeline::store_8888;
scroggo19b91532016-10-24 09:03:26 -0700529 break;
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400530 case kRGBA_F16_SkColorType:
531 p.append(SkRasterPipeline::load_f16_dst, &dst);
532 p.append(SkRasterPipeline::load_f16, &src);
533 storeDst = SkRasterPipeline::store_f16;
scroggo19b91532016-10-24 09:03:26 -0700534 break;
scroggo19b91532016-10-24 09:03:26 -0700535 default:
536 SkASSERT(false);
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400537 return false;
scroggo19b91532016-10-24 09:03:26 -0700538 break;
539 }
Leon Scroggins IIIe43fdb32017-07-17 19:41:46 -0400540 p.append(SkRasterPipeline::srcover);
541 p.append(storeDst, &dst);
542 p.run(0, 0, fSwizzler->swizzleWidth());
scroggo19b91532016-10-24 09:03:26 -0700543 }
544
545 // Tell the frame to copy the row data if need be.
546 if (repeatCount > 1) {
547 const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().colorType());
548 const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel;
549 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes());
550 void* dst = copiedLine;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400551 for (int i = 1; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700552 dst = SkTAddOffset<void>(dst, fDstRowBytes);
553 memcpy(dst, copiedLine, bytesToCopy);
msarett72261c02015-11-19 15:29:26 -0800554 }
555 }
556
557 return true;
558}