blob: eefe8986b7489978254dcd01befbdba3ce7bfe76 [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"
msarett8c8f22a2015-04-01 06:58:48 -070038#include "SkStream.h"
39#include "SkSwizzler.h"
msarett8c8f22a2015-04-01 06:58:48 -070040
scroggo19b91532016-10-24 09:03:26 -070041#include <algorithm>
42
43#define GIF87_STAMP "GIF87a"
44#define GIF89_STAMP "GIF89a"
45#define GIF_STAMP_LEN 6
msarett39b2d5a2016-02-17 08:26:31 -080046
msarett8c8f22a2015-04-01 06:58:48 -070047/*
48 * Checks the start of the stream to see if the image is a gif
49 */
scroggodb30be22015-12-08 18:54:13 -080050bool SkGifCodec::IsGif(const void* buf, size_t bytesRead) {
51 if (bytesRead >= GIF_STAMP_LEN) {
scroggo19b91532016-10-24 09:03:26 -070052 if (memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
bungeman0153dea2015-08-27 16:43:42 -070053 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0)
54 {
msarett8c8f22a2015-04-01 06:58:48 -070055 return true;
56 }
57 }
58 return false;
59}
60
61/*
msarett8c8f22a2015-04-01 06:58:48 -070062 * Error function
63 */
bungeman0153dea2015-08-27 16:43:42 -070064static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCodec::kInvalidInput) {
msarett8c8f22a2015-04-01 06:58:48 -070065 SkCodecPrintf("Gif Error: %s\n", msg);
66 return result;
67}
68
msarett438b2ad2015-04-09 12:43:10 -070069/*
msarett8c8f22a2015-04-01 06:58:48 -070070 * Assumes IsGif was called and returned true
71 * Creates a gif decoder
72 * Reads enough of the stream to determine the image format
73 */
74SkCodec* SkGifCodec::NewFromStream(SkStream* stream) {
scroggo3d3a65c2016-10-24 12:28:30 -070075 std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(stream));
scroggof9acbe22016-10-25 12:43:21 -070076 if (!reader->parse(SkGifImageReader::SkGIFSizeQuery)) {
Leon Scroggins III4993b952016-12-08 11:54:04 -050077 // Fatal error occurred.
scroggo19b91532016-10-24 09:03:26 -070078 return nullptr;
msarett8c8f22a2015-04-01 06:58:48 -070079 }
msarett8c8f22a2015-04-01 06:58:48 -070080
Leon Scroggins III4993b952016-12-08 11:54:04 -050081 // 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()) {
scroggo19b91532016-10-24 09:03:26 -070084 return nullptr;
85 }
86
Leon Scroggins III4993b952016-12-08 11:54:04 -050087 // isHeaderDefined() will not return true if the screen size is empty.
88 SkASSERT(reader->screenHeight() > 0 && reader->screenWidth() > 0);
89
scroggo19b91532016-10-24 09:03:26 -070090 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 Sarett7f650bd2016-10-30 21:25:34 -0400106
scroggo19b91532016-10-24 09:03:26 -0700107 const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(),
Matt Sarett7f650bd2016-10-30 21:25:34 -0400108 colorType, alphaType,
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500109 SkColorSpace::MakeSRGB());
scroggo19b91532016-10-24 09:03:26 -0700110 return new SkGifCodec(encodedInfo, imageInfo, reader.release());
111}
msarett8c8f22a2015-04-01 06:58:48 -0700112
scroggob427db12015-08-12 07:24:13 -0700113bool SkGifCodec::onRewind() {
scroggo19b91532016-10-24 09:03:26 -0700114 fReader->clearDecodeState();
scroggob427db12015-08-12 07:24:13 -0700115 return true;
116}
117
scroggo19b91532016-10-24 09:03:26 -0700118SkGifCodec::SkGifCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo,
scroggo3d3a65c2016-10-24 12:28:30 -0700119 SkGifImageReader* reader)
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400120 : INHERITED(encodedInfo, imageInfo, SkColorSpaceXform::kRGBA_8888_ColorFormat, nullptr)
scroggo19b91532016-10-24 09:03:26 -0700121 , 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);
msarett8c8f22a2015-04-01 06:58:48 -0700133}
msarett10522ff2015-09-07 08:54:01 -0700134
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400135int SkGifCodec::onGetFrameCount() {
scroggof9acbe22016-10-25 12:43:21 -0700136 fReader->parse(SkGifImageReader::SkGIFFrameCountQuery);
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400137 return fReader->imagesCount();
138}
139
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400140bool SkGifCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400141 if (i >= fReader->imagesCount()) {
142 return false;
msarett10522ff2015-09-07 08:54:01 -0700143 }
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400144
145 const SkGIFFrameContext* frameContext = fReader->frameContext(i);
146 if (!frameContext->reachedStartOfData()) {
147 return false;
148 }
149
150 if (frameInfo) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400151 frameInfo->fDuration = frameContext->getDuration();
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400152 frameInfo->fRequiredFrame = frameContext->getRequiredFrame();
153 frameInfo->fFullyReceived = frameContext->isComplete();
154 frameInfo->fAlphaType = frameContext->hasAlpha() ? kUnpremul_SkAlphaType
155 : kOpaque_SkAlphaType;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400156 frameInfo->fDisposalMethod = frameContext->getDisposalMethod();
Leon Scroggins IIIe132e7b2017-04-12 10:49:52 -0400157 }
158 return true;
msarett10522ff2015-09-07 08:54:01 -0700159}
160
scroggoe71b1a12016-11-01 08:28:28 -0700161int SkGifCodec::onGetRepetitionCount() {
162 fReader->parse(SkGifImageReader::SkGIFLoopCountQuery);
163 return fReader->loopCount();
164}
165
Matt Sarett562e6812016-11-08 16:13:43 -0500166static const SkColorType kXformSrcColorType = kRGBA_8888_SkColorType;
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400167static const SkAlphaType kXformAlphaType = kUnpremul_SkAlphaType;
Matt Sarett562e6812016-11-08 16:13:43 -0500168
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400169void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, int frameIndex) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400170 SkColorType colorTableColorType = dstInfo.colorType();
171 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500172 colorTableColorType = kXformSrcColorType;
Matt Sarett61eedeb2016-11-04 13:19:48 -0400173 }
174
175 sk_sp<SkColorTable> currColorTable = fReader->getColorTable(colorTableColorType, frameIndex);
176 fCurrColorTableIsReal = currColorTable;
177 if (!fCurrColorTableIsReal) {
Leon Scroggins IIIa049ac42016-10-27 11:16:11 -0400178 // 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 IIIc6e6a5f2017-06-05 15:53:38 -0400181 } else if (this->colorXform() && !this->xformOnDecode()) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400182 SkPMColor dstColors[256];
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400183 this->applyColorXform(dstColors, currColorTable->readColors(), currColorTable->count(),
184 kXformAlphaType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400185 fCurrColorTable.reset(new SkColorTable(dstColors, currColorTable->count()));
186 } else {
187 fCurrColorTable = std::move(currColorTable);
msarett10522ff2015-09-07 08:54:01 -0700188 }
msarett10522ff2015-09-07 08:54:01 -0700189}
190
scroggo19b91532016-10-24 09:03:26 -0700191
msarett10522ff2015-09-07 08:54:01 -0700192SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
193 int* inputColorCount, const Options& opts) {
scroggo19b91532016-10-24 09:03:26 -0700194 if (opts.fSubset) {
195 return gif_error("Subsets not supported.\n", kUnimplemented);
196 }
197
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400198 const int frameIndex = opts.fFrameIndex;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400199 if (frameIndex > 0 && kRGB_565_SkColorType == dstInfo.colorType()) {
200 // FIXME: In theory, we might be able to support this, but it's not clear that it
201 // is necessary (Chromium does not decode to 565, and Android does not decode
202 // frames beyond the first). Disabling it because it is somewhat difficult:
203 // - If there is a transparent pixel, and this frame draws on top of another frame
204 // (if the frame is independent with a transparent pixel, we should not decode to
205 // 565 anyway, since it is not opaque), we need to skip drawing the transparent
206 // pixels (see writeTransparentPixels in haveDecodedRow). We currently do this by
207 // first swizzling into temporary memory, then copying into the destination. (We
208 // let the swizzler handle it first because it may need to sample.) After
209 // swizzling to 565, we do not know which pixels in our temporary memory
210 // correspond to the transparent pixel, so we do not know what to skip. We could
211 // special case the non-sampled case (no need to swizzle), but as this is
212 // currently unused we can just not support it.
213 return gif_error("Cannot decode multiframe gif (except frame 0) as 565.\n",
214 kInvalidConversion);
scroggo19b91532016-10-24 09:03:26 -0700215 }
216
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400217 const auto* frame = fReader->frameContext(frameIndex);
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400218 SkASSERT(frame);
219 if (0 == frameIndex) {
220 // SkCodec does not have a way to just parse through frame 0, so we
221 // have to do so manually, here.
222 fReader->parse((SkGifImageReader::SkGIFParseQuery) 0);
223 if (!frame->reachedStartOfData()) {
224 // We have parsed enough to know that there is a color map, but cannot
225 // parse the map itself yet. Exit now, so we do not build an incorrect
226 // table.
227 return gif_error("color map not available yet\n", kIncompleteInput);
228 }
229 } else {
230 // Parsing happened in SkCodec::getPixels.
231 SkASSERT(frameIndex < fReader->imagesCount());
232 SkASSERT(frame->reachedStartOfData());
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500233 }
234
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400235 const auto at = frame->hasAlpha() ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
236 const auto srcInfo = this->getInfo().makeAlphaType(at);
237 if (!conversion_possible(dstInfo, srcInfo) ||
238 !this->initializeColorXform(dstInfo, opts.fPremulBehavior))
239 {
240 return gif_error("Cannot convert input type to output type.\n", kInvalidConversion);
241 }
242
243 if (this->xformOnDecode()) {
244 fXformBuffer.reset(new uint32_t[dstInfo.width()]);
245 sk_bzero(fXformBuffer.get(), dstInfo.width() * sizeof(uint32_t));
246 }
247
scroggo19b91532016-10-24 09:03:26 -0700248 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]);
249
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400250 this->initializeColorTable(dstInfo, frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700251 this->initializeSwizzler(dstInfo, frameIndex);
Leon Scroggins IIIfc49b402016-10-31 14:08:56 -0400252
253 SkASSERT(fCurrColorTable);
254 if (inputColorCount) {
255 *inputColorCount = fCurrColorTable->count();
256 }
257 copy_color_table(dstInfo, fCurrColorTable.get(), inputColorPtr, inputColorCount);
258
msarettb30d6982016-02-15 10:18:45 -0800259 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700260}
261
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400262void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex) {
scroggof9acbe22016-10-25 12:43:21 -0700263 const SkGIFFrameContext* frame = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700264 // This is only called by prepareToDecode, which ensures frameIndex is in range.
265 SkASSERT(frame);
msarett10522ff2015-09-07 08:54:01 -0700266
scroggo19b91532016-10-24 09:03:26 -0700267 const int xBegin = frame->xOffset();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400268 const int xEnd = std::min(frame->frameRect().right(), fReader->screenWidth());
scroggo19b91532016-10-24 09:03:26 -0700269
270 // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw
271 // frameRect, since it might extend beyond the edge of the frame.
272 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0);
273
Matt Sarett61eedeb2016-11-04 13:19:48 -0400274 SkImageInfo swizzlerInfo = dstInfo;
275 if (this->colorXform()) {
Matt Sarett562e6812016-11-08 16:13:43 -0500276 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400277 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
278 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
279 }
280 }
281
scroggo19b91532016-10-24 09:03:26 -0700282 // The default Options should be fine:
283 // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't
284 // matter anyway.
285 // - subsets are not supported for gif
286 // - the swizzler does not need to know about the frame.
287 // We may not be able to use the real Options anyway, since getPixels does not store it (due to
288 // a bug).
289 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(),
Matt Sarett61eedeb2016-11-04 13:19:48 -0400290 fCurrColorTable->readColors(), swizzlerInfo, Options(), &swizzleRect));
scroggo19b91532016-10-24 09:03:26 -0700291 SkASSERT(fSwizzler.get());
msarett10522ff2015-09-07 08:54:01 -0700292}
293
294/*
295 * Initiates the gif decode
296 */
297SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo,
scroggo19b91532016-10-24 09:03:26 -0700298 void* pixels, size_t dstRowBytes,
msarett10522ff2015-09-07 08:54:01 -0700299 const Options& opts,
300 SkPMColor* inputColorPtr,
msarette6dd0042015-10-09 11:07:34 -0700301 int* inputColorCount,
302 int* rowsDecoded) {
msarett10522ff2015-09-07 08:54:01 -0700303 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts);
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500304 switch (result) {
305 case kSuccess:
306 break;
307 case kIncompleteInput:
308 // onStartIncrementalDecode treats this as incomplete, since it may
309 // provide more data later, but in this case, no more data will be
310 // provided, and there is nothing to draw. We also cannot return
311 // kIncompleteInput, which will make SkCodec attempt to fill
312 // remaining rows, but that requires an SkSwizzler, which we have
313 // not created.
314 return kInvalidInput;
315 default:
316 return result;
msarett10522ff2015-09-07 08:54:01 -0700317 }
318
319 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
320 return gif_error("Scaling not supported.\n", kInvalidScale);
321 }
322
scroggo19b91532016-10-24 09:03:26 -0700323 fDst = pixels;
324 fDstRowBytes = dstRowBytes;
325
326 return this->decodeFrame(true, opts, rowsDecoded);
327}
328
329SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
330 void* pixels, size_t dstRowBytes,
331 const SkCodec::Options& opts,
332 SkPMColor* inputColorPtr,
333 int* inputColorCount) {
334 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts);
335 if (result != kSuccess) {
336 return result;
msarett10522ff2015-09-07 08:54:01 -0700337 }
338
scroggo19b91532016-10-24 09:03:26 -0700339 fDst = pixels;
340 fDstRowBytes = dstRowBytes;
341
342 fFirstCallToIncrementalDecode = true;
343
msarett10522ff2015-09-07 08:54:01 -0700344 return kSuccess;
345}
346
scroggo19b91532016-10-24 09:03:26 -0700347SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) {
348 // It is possible the client has appended more data. Parse, if needed.
349 const auto& options = this->options();
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400350 const int frameIndex = options.fFrameIndex;
scroggof9acbe22016-10-25 12:43:21 -0700351 fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700352
353 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode;
354 fFirstCallToIncrementalDecode = false;
355 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700356}
357
scroggo19b91532016-10-24 09:03:26 -0700358SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) {
359 const SkImageInfo& dstInfo = this->dstInfo();
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400360 const int frameIndex = opts.fFrameIndex;
scroggo19b91532016-10-24 09:03:26 -0700361 SkASSERT(frameIndex < fReader->imagesCount());
scroggof9acbe22016-10-25 12:43:21 -0700362 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700363 if (firstAttempt) {
364 // rowsDecoded reports how many rows have been initialized, so a layer above
365 // can fill the rest. In some cases, we fill the background before decoding
366 // (or it is already filled for us), so we report rowsDecoded to be the full
367 // height.
368 bool filledBackground = false;
369 if (frameContext->getRequiredFrame() == kNone) {
370 // We may need to clear to transparent for one of the following reasons:
371 // - The frameRect does not cover the full bounds. haveDecodedRow will
372 // only draw inside the frameRect, so we need to clear the rest.
scroggo19b91532016-10-24 09:03:26 -0700373 // - The frame is interlaced. There is no obvious way to fill
374 // afterwards for an incomplete image. (FIXME: Does the first pass
375 // cover all rows? If so, we do not have to fill here.)
scroggo8bce1172016-10-25 13:08:40 -0700376 // - There is no color table for this frame. In that case will not
377 // draw anything, so we need to fill.
scroggo19b91532016-10-24 09:03:26 -0700378 if (frameContext->frameRect() != this->getInfo().bounds()
scroggo8bce1172016-10-25 13:08:40 -0700379 || frameContext->interlaced() || !fCurrColorTableIsReal) {
scroggo19b91532016-10-24 09:03:26 -0700380 // fill ignores the width (replaces it with the actual, scaled width).
381 // But we need to scale in Y.
382 const int scaledHeight = get_scaled_dimension(dstInfo.height(),
383 fSwizzler->sampleY());
384 auto fillInfo = dstInfo.makeWH(0, scaledHeight);
385 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue(dstInfo),
386 opts.fZeroInitialized);
387 filledBackground = true;
388 }
389 } else {
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400390 // Not independent.
391 // SkCodec ensured that the prior frame has been decoded.
scroggo19b91532016-10-24 09:03:26 -0700392 filledBackground = true;
msarett10522ff2015-09-07 08:54:01 -0700393 }
scroggo19b91532016-10-24 09:03:26 -0700394
395 fFilledBackground = filledBackground;
396 if (filledBackground) {
397 // Report the full (scaled) height, since the client will never need to fill.
398 fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY());
399 } else {
400 // This will be updated by haveDecodedRow.
401 fRowsDecoded = 0;
402 }
msarett10522ff2015-09-07 08:54:01 -0700403 }
msarette6dd0042015-10-09 11:07:34 -0700404
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500405 if (!fCurrColorTableIsReal) {
406 // Nothing to draw this frame.
407 return kSuccess;
408 }
409
scroggo3d3a65c2016-10-24 12:28:30 -0700410 // Note: there is a difference between the following call to SkGifImageReader::decode
scroggo19b91532016-10-24 09:03:26 -0700411 // returning false and leaving frameDecoded false:
412 // - If the method returns false, there was an error in the stream. We still treat this as
413 // incomplete, since we have already decoded some rows.
414 // - If frameDecoded is false, that just means that we do not have enough data. If more data
415 // is supplied, we may be able to continue decoding this frame. We also treat this as
416 // incomplete.
417 // FIXME: Ensure that we do not attempt to continue decoding if the method returns false and
418 // more data is supplied.
419 bool frameDecoded = false;
420 if (!fReader->decode(frameIndex, &frameDecoded) || !frameDecoded) {
421 if (rowsDecoded) {
422 *rowsDecoded = fRowsDecoded;
423 }
424 return kIncompleteInput;
425 }
426
427 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700428}
scroggo46c57472015-09-30 08:57:13 -0700429
scroggo19b91532016-10-24 09:03:26 -0700430uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
431 // Note: Using fCurrColorTable relies on having called initializeColorTable already.
432 // This is (currently) safe because this method is only called when filling, after
433 // initializeColorTable has been called.
434 // FIXME: Is there a way to make this less fragile?
435 if (dstInfo.colorType() == kIndex_8_SkColorType && fCurrColorTableIsReal) {
436 // We only support index 8 for the first frame, for backwards
437 // compatibity on Android, so we are using the color table for the first frame.
438 SkASSERT(this->options().fFrameIndex == 0);
439 // Use the transparent index for the first frame.
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400440 const int transPixel = fReader->frameContext(0)->transparentPixel();
441 if (transPixel >= 0 && transPixel < fCurrColorTable->count()) {
scroggo19b91532016-10-24 09:03:26 -0700442 return transPixel;
443 }
444 // Fall through to return SK_ColorTRANSPARENT (i.e. 0). This choice is arbitrary,
445 // but we have to pick something inside the color table, and this one is as good
446 // as any.
447 }
448 // Using transparent as the fill value matches the behavior in Chromium,
449 // which ignores the background color.
450 // If the colorType is kIndex_8, and there was no color table (i.e.
451 // fCurrColorTableIsReal is false), this value (zero) corresponds to the
452 // only entry in the dummy color table provided to the client.
453 return SK_ColorTRANSPARENT;
454}
msarett72261c02015-11-19 15:29:26 -0800455
Matt Sarett61eedeb2016-11-04 13:19:48 -0400456void SkGifCodec::applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const {
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400457 if (this->xformOnDecode()) {
458 SkASSERT(this->colorXform());
Matt Sarett61eedeb2016-11-04 13:19:48 -0400459 fSwizzler->swizzle(fXformBuffer.get(), src);
460
Matt Sarett61eedeb2016-11-04 13:19:48 -0400461 const int xformWidth = get_scaled_dimension(dstInfo.width(), fSwizzler->sampleX());
Leon Scroggins III91f0f732017-06-07 09:31:23 -0400462 this->applyColorXform(dst, fXformBuffer.get(), xformWidth, kXformAlphaType);
Matt Sarett61eedeb2016-11-04 13:19:48 -0400463 } else {
464 fSwizzler->swizzle(dst, src);
465 }
466}
467
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400468bool SkGifCodec::haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
469 int rowNumber, int repeatCount, bool writeTransparentPixels)
scroggo19b91532016-10-24 09:03:26 -0700470{
scroggof9acbe22016-10-25 12:43:21 -0700471 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
scroggo19b91532016-10-24 09:03:26 -0700472 // The pixel data and coordinates supplied to us are relative to the frame's
473 // origin within the entire image size, i.e.
474 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee
475 // that width == (size().width() - frameContext->xOffset), so
476 // we must ensure we don't run off the end of either the source data or the
477 // row's X-coordinates.
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400478 const int width = frameContext->width();
scroggo19b91532016-10-24 09:03:26 -0700479 const int xBegin = frameContext->xOffset();
480 const int yBegin = frameContext->yOffset() + rowNumber;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400481 const int xEnd = std::min(xBegin + width, this->getInfo().width());
482 const int yEnd = std::min(yBegin + rowNumber + repeatCount, this->getInfo().height());
scroggo19b91532016-10-24 09:03:26 -0700483 // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do
484 // this once in prepareToDecode.
485 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin))
486 return true;
487
488 // yBegin is the first row in the non-sampled image. dstRow will be the row in the output,
489 // after potentially scaling it.
490 int dstRow = yBegin;
491
492 const int sampleY = fSwizzler->sampleY();
493 if (sampleY > 1) {
494 // Check to see whether this row or one that falls in the repeatCount is needed in the
495 // output.
496 bool foundNecessaryRow = false;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400497 for (int i = 0; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700498 const int potentialRow = yBegin + i;
499 if (fSwizzler->rowNeeded(potentialRow)) {
500 dstRow = potentialRow / sampleY;
501 const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY);
502 if (dstRow >= scaledHeight) {
503 return true;
504 }
505
506 foundNecessaryRow = true;
507 repeatCount -= i;
508
509 repeatCount = (repeatCount - 1) / sampleY + 1;
510
511 // Make sure the repeatCount does not take us beyond the end of the dst
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400512 if (dstRow + repeatCount > scaledHeight) {
scroggo19b91532016-10-24 09:03:26 -0700513 repeatCount = scaledHeight - dstRow;
514 SkASSERT(repeatCount >= 1);
515 }
516 break;
517 }
518 }
519
520 if (!foundNecessaryRow) {
521 return true;
522 }
Matt Sarett8a4e9c52016-10-25 14:24:50 -0400523 } else {
524 // Make sure the repeatCount does not take us beyond the end of the dst
525 SkASSERT(this->dstInfo().height() >= yBegin);
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400526 repeatCount = SkTMin(repeatCount, this->dstInfo().height() - yBegin);
scroggo19b91532016-10-24 09:03:26 -0700527 }
528
529 if (!fFilledBackground) {
530 // At this point, we are definitely going to write the row, so count it towards the number
531 // of rows decoded.
532 // We do not consider the repeatCount, which only happens for interlaced, in which case we
533 // have already set fRowsDecoded to the proper value (reflecting that we have filled the
534 // background).
535 fRowsDecoded++;
536 }
537
Leon Scroggins III3fc97d72016-12-09 16:39:33 -0500538 // decodeFrame will early exit if this is false, so this method will not be
539 // called.
540 SkASSERT(fCurrColorTableIsReal);
scroggo19b91532016-10-24 09:03:26 -0700541
542 // The swizzler takes care of offsetting into the dst width-wise.
543 void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes);
544
545 // We may or may not need to write transparent pixels to the buffer.
scroggo1285f412016-10-26 13:48:03 -0700546 // If we're compositing against a previous image, it's wrong, but if
547 // we're decoding an interlaced gif and displaying it "Haeberli"-style,
548 // we must write these for passes beyond the first, or the initial passes
549 // will "show through" the later ones.
scroggo19b91532016-10-24 09:03:26 -0700550 const auto dstInfo = this->dstInfo();
scroggo53f63b62016-10-27 08:29:13 -0700551 if (writeTransparentPixels) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400552 this->applyXformRow(dstInfo, dstLine, rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700553 } else {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400554 sk_bzero(fTmpBuffer.get(), dstInfo.minRowBytes());
555 this->applyXformRow(dstInfo, fTmpBuffer.get(), rowBegin);
scroggo19b91532016-10-24 09:03:26 -0700556
557 const size_t offsetBytes = fSwizzler->swizzleOffsetBytes();
558 switch (dstInfo.colorType()) {
559 case kBGRA_8888_SkColorType:
560 case kRGBA_8888_SkColorType: {
561 uint32_t* dstPixel = SkTAddOffset<uint32_t>(dstLine, offsetBytes);
562 uint32_t* srcPixel = SkTAddOffset<uint32_t>(fTmpBuffer.get(), offsetBytes);
563 for (int i = 0; i < fSwizzler->swizzleWidth(); i++) {
564 // Technically SK_ColorTRANSPARENT is an SkPMColor, and srcPixel would have
565 // the opposite swizzle for the non-native swizzle, but TRANSPARENT is all
566 // zeroes, which is the same either way.
567 if (*srcPixel != SK_ColorTRANSPARENT) {
568 *dstPixel = *srcPixel;
569 }
570 dstPixel++;
571 srcPixel++;
572 }
573 break;
574 }
Matt Sarett61eedeb2016-11-04 13:19:48 -0400575 case kRGBA_F16_SkColorType: {
576 uint64_t* dstPixel = SkTAddOffset<uint64_t>(dstLine, offsetBytes);
577 uint64_t* srcPixel = SkTAddOffset<uint64_t>(fTmpBuffer.get(), offsetBytes);
scroggo19b91532016-10-24 09:03:26 -0700578 for (int i = 0; i < fSwizzler->swizzleWidth(); i++) {
Matt Sarett61eedeb2016-11-04 13:19:48 -0400579 if (*srcPixel != 0) {
scroggo19b91532016-10-24 09:03:26 -0700580 *dstPixel = *srcPixel;
581 }
582 dstPixel++;
583 srcPixel++;
584 }
585 break;
586 }
587 default:
588 SkASSERT(false);
589 break;
590 }
591 }
592
593 // Tell the frame to copy the row data if need be.
594 if (repeatCount > 1) {
595 const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().colorType());
596 const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel;
597 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes());
598 void* dst = copiedLine;
Leon Scroggins III249b8e32017-04-17 12:46:33 -0400599 for (int i = 1; i < repeatCount; i++) {
scroggo19b91532016-10-24 09:03:26 -0700600 dst = SkTAddOffset<void>(dst, fDstRowBytes);
601 memcpy(dst, copiedLine, bytesToCopy);
msarett72261c02015-11-19 15:29:26 -0800602 }
603 }
604
605 return true;
606}