blob: 3f37bc35cc8d921a38afafb1a0d698a30f09f9cd [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) {
scroggo19b91532016-10-24 09:03:26 -070075 std::unique_ptr<GIFImageReader> reader(new GIFImageReader(stream));
76 if (!reader->parse(GIFImageReader::GIFSizeQuery)) {
77 // Not enough data to determine the size.
78 return nullptr;
msarett8c8f22a2015-04-01 06:58:48 -070079 }
msarett8c8f22a2015-04-01 06:58:48 -070080
scroggo19b91532016-10-24 09:03:26 -070081 if (0 == reader->screenWidth() || 0 == reader->screenHeight()) {
82 return nullptr;
83 }
84
85 const auto alpha = reader->firstFrameHasAlpha() ? SkEncodedInfo::kBinary_Alpha
86 : SkEncodedInfo::kOpaque_Alpha;
87 // Use kPalette since Gifs are encoded with a color table.
88 // FIXME: Gifs can actually be encoded with 4-bits per pixel. Using 8 works, but we could skip
89 // expanding to 8 bits and take advantage of the SkSwizzler to work from 4.
90 const auto encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8);
91
92 // Although the encodedInfo is always kPalette_Color, it is possible that kIndex_8 is
93 // unsupported if the frame is subset and there is no transparent pixel.
94 const auto colorType = reader->firstFrameSupportsIndex8() ? kIndex_8_SkColorType
95 : kN32_SkColorType;
96 // The choice of unpremul versus premul is arbitrary, since all colors are either fully
97 // opaque or fully transparent (i.e. kBinary), but we stored the transparent colors as all
98 // zeroes, which is arguably premultiplied.
99 const auto alphaType = reader->firstFrameHasAlpha() ? kUnpremul_SkAlphaType
100 : kOpaque_SkAlphaType;
101 // FIXME: GIF should default to SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named).
102 const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(),
103 colorType, alphaType);
104 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,
113 GIFImageReader* reader)
114 : INHERITED(encodedInfo, imageInfo, nullptr)
115 , 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
scroggo19b91532016-10-24 09:03:26 -0700129std::vector<SkCodec::FrameInfo> SkGifCodec::onGetFrameInfo() {
130 fReader->parse(GIFImageReader::GIFFrameCountQuery);
131 const size_t size = fReader->imagesCount();
132 std::vector<FrameInfo> result(size);
133 for (size_t i = 0; i < size; i++) {
134 const GIFFrameContext* frameContext = fReader->frameContext(i);
135 result[i].fDuration = frameContext->delayTime();
136 result[i].fRequiredFrame = frameContext->getRequiredFrame();
msarett10522ff2015-09-07 08:54:01 -0700137 }
scroggo19b91532016-10-24 09:03:26 -0700138 return result;
msarett10522ff2015-09-07 08:54:01 -0700139}
140
scroggo19b91532016-10-24 09:03:26 -0700141void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, size_t frameIndex,
142 SkPMColor* inputColorPtr, int* inputColorCount) {
143 fCurrColorTable = fReader->getColorTable(dstInfo.colorType(), frameIndex);
144 fCurrColorTableIsReal = fCurrColorTable;
145 if (!fCurrColorTable) {
146 // This is possible for an empty frame. Create a dummy with one value (transparent).
147 SkPMColor color = SK_ColorTRANSPARENT;
148 fCurrColorTable.reset(new SkColorTable(&color, 1));
msarett10522ff2015-09-07 08:54:01 -0700149 }
150
scroggo19b91532016-10-24 09:03:26 -0700151 if (inputColorCount) {
152 *inputColorCount = fCurrColorTable->count();
msarett10522ff2015-09-07 08:54:01 -0700153 }
154
scroggo19b91532016-10-24 09:03:26 -0700155 copy_color_table(dstInfo, fCurrColorTable.get(), inputColorPtr, inputColorCount);
msarett10522ff2015-09-07 08:54:01 -0700156}
157
scroggo19b91532016-10-24 09:03:26 -0700158
msarett10522ff2015-09-07 08:54:01 -0700159SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, SkPMColor* inputColorPtr,
160 int* inputColorCount, const Options& opts) {
msarett10522ff2015-09-07 08:54:01 -0700161 // Check for valid input parameters
msarett2ecc35f2016-09-08 11:55:16 -0700162 if (!conversion_possible_ignore_color_space(dstInfo, this->getInfo())) {
163 return gif_error("Cannot convert input type to output type.\n", kInvalidConversion);
msarett10522ff2015-09-07 08:54:01 -0700164 }
165
scroggo19b91532016-10-24 09:03:26 -0700166 if (dstInfo.colorType() == kRGBA_F16_SkColorType) {
167 // FIXME: This should be supported.
168 return gif_error("GIF does not yet support F16.\n", kInvalidConversion);
169 }
msarett5af4e0b2015-11-17 11:18:03 -0800170
scroggo19b91532016-10-24 09:03:26 -0700171 if (opts.fSubset) {
172 return gif_error("Subsets not supported.\n", kUnimplemented);
173 }
174
175 const size_t frameIndex = opts.fFrameIndex;
176 if (frameIndex > 0 && dstInfo.colorType() == kIndex_8_SkColorType) {
177 // FIXME: It is possible that a later frame can be decoded to index8, if it does one of the
178 // following:
179 // - Covers the entire previous frame
180 // - Shares a color table (and transparent index) with any prior frames that are showing.
181 // We must support index8 for the first frame to be backwards compatible on Android, but
182 // we do not (currently) need to support later frames as index8.
183 return gif_error("Cannot decode multiframe gif (except frame 0) as index 8.\n",
184 kInvalidConversion);
185 }
186
187 fReader->parse((GIFImageReader::GIFParseQuery) frameIndex);
188
189 if (frameIndex >= fReader->imagesCount()) {
190 return gif_error("frame index out of range!\n", kIncompleteInput);
191 }
192
193 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]);
194
195 // Initialize color table and copy to the client if necessary
196 this->initializeColorTable(dstInfo, frameIndex, inputColorPtr, inputColorCount);
197 this->initializeSwizzler(dstInfo, frameIndex);
msarettb30d6982016-02-15 10:18:45 -0800198 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700199}
200
scroggo19b91532016-10-24 09:03:26 -0700201void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, size_t frameIndex) {
202 const GIFFrameContext* frame = fReader->frameContext(frameIndex);
203 // This is only called by prepareToDecode, which ensures frameIndex is in range.
204 SkASSERT(frame);
msarett10522ff2015-09-07 08:54:01 -0700205
scroggo19b91532016-10-24 09:03:26 -0700206 const int xBegin = frame->xOffset();
207 const int xEnd = std::min(static_cast<int>(frame->xOffset() + frame->width()),
208 static_cast<int>(fReader->screenWidth()));
209
210 // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw
211 // frameRect, since it might extend beyond the edge of the frame.
212 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0);
213
214 // The default Options should be fine:
215 // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't
216 // matter anyway.
217 // - subsets are not supported for gif
218 // - the swizzler does not need to know about the frame.
219 // We may not be able to use the real Options anyway, since getPixels does not store it (due to
220 // a bug).
221 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(),
222 fCurrColorTable->readColors(), dstInfo, Options(), &swizzleRect));
223 SkASSERT(fSwizzler.get());
msarett10522ff2015-09-07 08:54:01 -0700224}
225
226/*
227 * Initiates the gif decode
228 */
229SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo,
scroggo19b91532016-10-24 09:03:26 -0700230 void* pixels, size_t dstRowBytes,
msarett10522ff2015-09-07 08:54:01 -0700231 const Options& opts,
232 SkPMColor* inputColorPtr,
msarette6dd0042015-10-09 11:07:34 -0700233 int* inputColorCount,
234 int* rowsDecoded) {
msarett10522ff2015-09-07 08:54:01 -0700235 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts);
236 if (kSuccess != result) {
237 return result;
238 }
239
240 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
241 return gif_error("Scaling not supported.\n", kInvalidScale);
242 }
243
scroggo19b91532016-10-24 09:03:26 -0700244 fDst = pixels;
245 fDstRowBytes = dstRowBytes;
246
247 return this->decodeFrame(true, opts, rowsDecoded);
248}
249
250SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
251 void* pixels, size_t dstRowBytes,
252 const SkCodec::Options& opts,
253 SkPMColor* inputColorPtr,
254 int* inputColorCount) {
255 Result result = this->prepareToDecode(dstInfo, inputColorPtr, inputColorCount, opts);
256 if (result != kSuccess) {
257 return result;
msarett10522ff2015-09-07 08:54:01 -0700258 }
259
scroggo19b91532016-10-24 09:03:26 -0700260 fDst = pixels;
261 fDstRowBytes = dstRowBytes;
262
263 fFirstCallToIncrementalDecode = true;
264
msarett10522ff2015-09-07 08:54:01 -0700265 return kSuccess;
266}
267
scroggo19b91532016-10-24 09:03:26 -0700268SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) {
269 // It is possible the client has appended more data. Parse, if needed.
270 const auto& options = this->options();
271 const size_t frameIndex = options.fFrameIndex;
272 fReader->parse((GIFImageReader::GIFParseQuery) frameIndex);
273
274 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode;
275 fFirstCallToIncrementalDecode = false;
276 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded);
msarette6dd0042015-10-09 11:07:34 -0700277}
278
scroggo19b91532016-10-24 09:03:26 -0700279SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) {
280 const SkImageInfo& dstInfo = this->dstInfo();
281 const size_t frameIndex = opts.fFrameIndex;
282 SkASSERT(frameIndex < fReader->imagesCount());
283 const GIFFrameContext* frameContext = fReader->frameContext(frameIndex);
284 if (firstAttempt) {
285 // rowsDecoded reports how many rows have been initialized, so a layer above
286 // can fill the rest. In some cases, we fill the background before decoding
287 // (or it is already filled for us), so we report rowsDecoded to be the full
288 // height.
289 bool filledBackground = false;
290 if (frameContext->getRequiredFrame() == kNone) {
291 // We may need to clear to transparent for one of the following reasons:
292 // - The frameRect does not cover the full bounds. haveDecodedRow will
293 // only draw inside the frameRect, so we need to clear the rest.
294 // - There is a valid transparent pixel value. (FIXME: I'm assuming
295 // writeTransparentPixels will be false in this case, based on
296 // Chromium's assumption that it would already be zeroed. If we
297 // change that behavior, could we skip Filling here?)
298 // - The frame is interlaced. There is no obvious way to fill
299 // afterwards for an incomplete image. (FIXME: Does the first pass
300 // cover all rows? If so, we do not have to fill here.)
301 if (frameContext->frameRect() != this->getInfo().bounds()
302 || frameContext->transparentPixel() < MAX_COLORS
303 || frameContext->interlaced()) {
304 // fill ignores the width (replaces it with the actual, scaled width).
305 // But we need to scale in Y.
306 const int scaledHeight = get_scaled_dimension(dstInfo.height(),
307 fSwizzler->sampleY());
308 auto fillInfo = dstInfo.makeWH(0, scaledHeight);
309 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue(dstInfo),
310 opts.fZeroInitialized);
311 filledBackground = true;
312 }
313 } else {
314 // Not independent
315 if (!opts.fHasPriorFrame) {
316 // Decode that frame into pixels.
317 Options prevFrameOpts(opts);
318 prevFrameOpts.fFrameIndex = frameContext->getRequiredFrame();
319 prevFrameOpts.fHasPriorFrame = false;
320 const Result prevResult = this->decodeFrame(true, prevFrameOpts, nullptr);
321 switch (prevResult) {
322 case kSuccess:
323 // Prior frame succeeded. Carry on.
324 break;
325 case kIncompleteInput:
326 // Prior frame was incomplete. So this frame cannot be decoded.
327 return kInvalidInput;
328 default:
329 return prevResult;
330 }
331 }
332 const auto* prevFrame = fReader->frameContext(frameContext->getRequiredFrame());
333 if (prevFrame->getDisposalMethod() == SkCodecAnimation::RestoreBGColor_DisposalMethod) {
334 const SkIRect prevRect = prevFrame->frameRect();
335 auto left = get_scaled_dimension(prevRect.fLeft, fSwizzler->sampleX());
336 auto top = get_scaled_dimension(prevRect.fTop, fSwizzler->sampleY());
337 void* const eraseDst = SkTAddOffset<void>(fDst, top * fDstRowBytes
338 + left * SkColorTypeBytesPerPixel(dstInfo.colorType()));
339 auto width = get_scaled_dimension(prevRect.width(), fSwizzler->sampleX());
340 auto height = get_scaled_dimension(prevRect.height(), fSwizzler->sampleY());
341 // fSwizzler->fill() would fill to the scaled width of the frame, but we want to
342 // fill to the scaled with of the width of the PRIOR frame, so we do all the scaling
343 // ourselves and call the static version.
344 SkSampler::Fill(dstInfo.makeWH(width, height), eraseDst,
345 fDstRowBytes, this->getFillValue(dstInfo), kNo_ZeroInitialized);
346 }
347 filledBackground = true;
msarett10522ff2015-09-07 08:54:01 -0700348 }
scroggo19b91532016-10-24 09:03:26 -0700349
350 fFilledBackground = filledBackground;
351 if (filledBackground) {
352 // Report the full (scaled) height, since the client will never need to fill.
353 fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY());
354 } else {
355 // This will be updated by haveDecodedRow.
356 fRowsDecoded = 0;
357 }
msarett10522ff2015-09-07 08:54:01 -0700358 }
msarette6dd0042015-10-09 11:07:34 -0700359
scroggo19b91532016-10-24 09:03:26 -0700360 // Note: there is a difference between the following call to GIFImageReader::decode
361 // returning false and leaving frameDecoded false:
362 // - If the method returns false, there was an error in the stream. We still treat this as
363 // incomplete, since we have already decoded some rows.
364 // - If frameDecoded is false, that just means that we do not have enough data. If more data
365 // is supplied, we may be able to continue decoding this frame. We also treat this as
366 // incomplete.
367 // FIXME: Ensure that we do not attempt to continue decoding if the method returns false and
368 // more data is supplied.
369 bool frameDecoded = false;
370 if (!fReader->decode(frameIndex, &frameDecoded) || !frameDecoded) {
371 if (rowsDecoded) {
372 *rowsDecoded = fRowsDecoded;
373 }
374 return kIncompleteInput;
375 }
376
377 return kSuccess;
msarett10522ff2015-09-07 08:54:01 -0700378}
scroggo46c57472015-09-30 08:57:13 -0700379
scroggo19b91532016-10-24 09:03:26 -0700380uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
381 // Note: Using fCurrColorTable relies on having called initializeColorTable already.
382 // This is (currently) safe because this method is only called when filling, after
383 // initializeColorTable has been called.
384 // FIXME: Is there a way to make this less fragile?
385 if (dstInfo.colorType() == kIndex_8_SkColorType && fCurrColorTableIsReal) {
386 // We only support index 8 for the first frame, for backwards
387 // compatibity on Android, so we are using the color table for the first frame.
388 SkASSERT(this->options().fFrameIndex == 0);
389 // Use the transparent index for the first frame.
390 const size_t transPixel = fReader->frameContext(0)->transparentPixel();
391 if (transPixel < (size_t) fCurrColorTable->count()) {
392 return transPixel;
393 }
394 // Fall through to return SK_ColorTRANSPARENT (i.e. 0). This choice is arbitrary,
395 // but we have to pick something inside the color table, and this one is as good
396 // as any.
397 }
398 // Using transparent as the fill value matches the behavior in Chromium,
399 // which ignores the background color.
400 // If the colorType is kIndex_8, and there was no color table (i.e.
401 // fCurrColorTableIsReal is false), this value (zero) corresponds to the
402 // only entry in the dummy color table provided to the client.
403 return SK_ColorTRANSPARENT;
404}
msarett72261c02015-11-19 15:29:26 -0800405
scroggo19b91532016-10-24 09:03:26 -0700406bool SkGifCodec::haveDecodedRow(size_t frameIndex, const unsigned char* rowBegin,
407 size_t rowNumber, unsigned repeatCount, bool writeTransparentPixels)
408{
409 const GIFFrameContext* frameContext = fReader->frameContext(frameIndex);
410 // The pixel data and coordinates supplied to us are relative to the frame's
411 // origin within the entire image size, i.e.
412 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee
413 // that width == (size().width() - frameContext->xOffset), so
414 // we must ensure we don't run off the end of either the source data or the
415 // row's X-coordinates.
416 const size_t width = frameContext->width();
417 const int xBegin = frameContext->xOffset();
418 const int yBegin = frameContext->yOffset() + rowNumber;
419 const int xEnd = std::min(static_cast<int>(frameContext->xOffset() + width),
420 this->getInfo().width());
421 const int yEnd = std::min(static_cast<int>(frameContext->yOffset() + rowNumber + repeatCount),
422 this->getInfo().height());
423 // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do
424 // this once in prepareToDecode.
425 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin))
426 return true;
427
428 // yBegin is the first row in the non-sampled image. dstRow will be the row in the output,
429 // after potentially scaling it.
430 int dstRow = yBegin;
431
432 const int sampleY = fSwizzler->sampleY();
433 if (sampleY > 1) {
434 // Check to see whether this row or one that falls in the repeatCount is needed in the
435 // output.
436 bool foundNecessaryRow = false;
437 for (unsigned i = 0; i < repeatCount; i++) {
438 const int potentialRow = yBegin + i;
439 if (fSwizzler->rowNeeded(potentialRow)) {
440 dstRow = potentialRow / sampleY;
441 const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY);
442 if (dstRow >= scaledHeight) {
443 return true;
444 }
445
446 foundNecessaryRow = true;
447 repeatCount -= i;
448
449 repeatCount = (repeatCount - 1) / sampleY + 1;
450
451 // Make sure the repeatCount does not take us beyond the end of the dst
452 if (dstRow + (int) repeatCount > scaledHeight) {
453 repeatCount = scaledHeight - dstRow;
454 SkASSERT(repeatCount >= 1);
455 }
456 break;
457 }
458 }
459
460 if (!foundNecessaryRow) {
461 return true;
462 }
463 }
464
465 if (!fFilledBackground) {
466 // At this point, we are definitely going to write the row, so count it towards the number
467 // of rows decoded.
468 // We do not consider the repeatCount, which only happens for interlaced, in which case we
469 // have already set fRowsDecoded to the proper value (reflecting that we have filled the
470 // background).
471 fRowsDecoded++;
472 }
473
474 if (!fCurrColorTableIsReal) {
475 // No color table, so nothing to draw this frame.
476 // FIXME: We can abort even earlier - no need to decode this frame.
477 return true;
478 }
479
480 // The swizzler takes care of offsetting into the dst width-wise.
481 void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes);
482
483 // We may or may not need to write transparent pixels to the buffer.
484 // If we're compositing against a previous image, it's wrong, and if
485 // we're writing atop a cleared, fully transparent buffer, it's
486 // unnecessary; but if we're decoding an interlaced gif and
487 // displaying it "Haeberli"-style, we must write these for passes
488 // beyond the first, or the initial passes will "show through" the
489 // later ones.
490 const auto dstInfo = this->dstInfo();
491 if (writeTransparentPixels || dstInfo.colorType() == kRGB_565_SkColorType) {
492 fSwizzler->swizzle(dstLine, rowBegin);
493 } else {
494 // We cannot swizzle directly into the dst, since that will write the transparent pixels.
495 // Instead, swizzle into a temporary buffer, and copy that into the dst.
496 {
497 void* const memsetDst = fTmpBuffer.get();
498 // Although onGetFillValue returns a uint64_t, we only use the low eight bits. The
499 // return value is either an 8 bit index (for index8) or SK_ColorTRANSPARENT, which is
500 // all zeroes.
501 const int fillValue = (uint8_t) this->onGetFillValue(dstInfo);
502 const size_t rb = dstInfo.minRowBytes();
503 if (fillValue == 0) {
504 // FIXME: This special case should be unnecessary, and in fact sk_bzero just calls
505 // memset. But without it, the compiler thinks this is trying to pass a zero length
506 // to memset, causing an error.
507 sk_bzero(memsetDst, rb);
508 } else {
509 memset(memsetDst, fillValue, rb);
510 }
511 }
512 fSwizzler->swizzle(fTmpBuffer.get(), rowBegin);
513
514 const size_t offsetBytes = fSwizzler->swizzleOffsetBytes();
515 switch (dstInfo.colorType()) {
516 case kBGRA_8888_SkColorType:
517 case kRGBA_8888_SkColorType: {
518 uint32_t* dstPixel = SkTAddOffset<uint32_t>(dstLine, offsetBytes);
519 uint32_t* srcPixel = SkTAddOffset<uint32_t>(fTmpBuffer.get(), offsetBytes);
520 for (int i = 0; i < fSwizzler->swizzleWidth(); i++) {
521 // Technically SK_ColorTRANSPARENT is an SkPMColor, and srcPixel would have
522 // the opposite swizzle for the non-native swizzle, but TRANSPARENT is all
523 // zeroes, which is the same either way.
524 if (*srcPixel != SK_ColorTRANSPARENT) {
525 *dstPixel = *srcPixel;
526 }
527 dstPixel++;
528 srcPixel++;
529 }
530 break;
531 }
532 case kIndex_8_SkColorType: {
533 uint8_t* dstPixel = SkTAddOffset<uint8_t>(dstLine, offsetBytes);
534 uint8_t* srcPixel = SkTAddOffset<uint8_t>(fTmpBuffer.get(), offsetBytes);
535 for (int i = 0; i < fSwizzler->swizzleWidth(); i++) {
536 if (*srcPixel != frameContext->transparentPixel()) {
537 *dstPixel = *srcPixel;
538 }
539 dstPixel++;
540 srcPixel++;
541 }
542 break;
543 }
544 default:
545 SkASSERT(false);
546 break;
547 }
548 }
549
550 // Tell the frame to copy the row data if need be.
551 if (repeatCount > 1) {
552 const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().colorType());
553 const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel;
554 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes());
555 void* dst = copiedLine;
556 for (unsigned i = 1; i < repeatCount; i++) {
557 dst = SkTAddOffset<void>(dst, fDstRowBytes);
558 memcpy(dst, copiedLine, bytesToCopy);
msarett72261c02015-11-19 15:29:26 -0800559 }
560 }
561
562 return true;
563}