1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 /*
9 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "SkCodecAnimation.h"
34 #include "SkCodecPriv.h"
35 #include "SkColorData.h"
36 #include "SkColorTable.h"
37 #include "SkGifCodec.h"
38 #include "SkMakeUnique.h"
39 #include "SkStream.h"
40 #include "SkSwizzler.h"
41
42 #include <algorithm>
43
44 #define GIF87_STAMP "GIF87a"
45 #define GIF89_STAMP "GIF89a"
46 #define GIF_STAMP_LEN 6
47
48 /*
49 * Checks the start of the stream to see if the image is a gif
50 */
IsGif(const void * buf,size_t bytesRead)51 bool SkGifCodec::IsGif(const void* buf, size_t bytesRead) {
52 if (bytesRead >= GIF_STAMP_LEN) {
53 if (memcmp(GIF87_STAMP, buf, GIF_STAMP_LEN) == 0 ||
54 memcmp(GIF89_STAMP, buf, GIF_STAMP_LEN) == 0)
55 {
56 return true;
57 }
58 }
59 return false;
60 }
61
62 /*
63 * Error function
64 */
gif_error(const char * msg,SkCodec::Result result=SkCodec::kInvalidInput)65 static SkCodec::Result gif_error(const char* msg, SkCodec::Result result = SkCodec::kInvalidInput) {
66 SkCodecPrintf("Gif Error: %s\n", msg);
67 return result;
68 }
69
MakeFromStream(std::unique_ptr<SkStream> stream,Result * result)70 std::unique_ptr<SkCodec> SkGifCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
71 Result* result) {
72 std::unique_ptr<SkGifImageReader> reader(new SkGifImageReader(std::move(stream)));
73 *result = reader->parse(SkGifImageReader::SkGIFSizeQuery);
74 if (*result != kSuccess) {
75 return nullptr;
76 }
77
78 // If no images are in the data, or the first header is not yet defined, we cannot
79 // create a codec. In either case, the width and height are not yet known.
80 auto* frame = reader->frameContext(0);
81 if (!frame || !frame->isHeaderDefined()) {
82 *result = kInvalidInput;
83 return nullptr;
84 }
85
86 // isHeaderDefined() will not return true if the screen size is empty.
87 SkASSERT(reader->screenHeight() > 0 && reader->screenWidth() > 0);
88
89 const auto alpha = reader->firstFrameHasAlpha() ? SkEncodedInfo::kBinary_Alpha
90 : SkEncodedInfo::kOpaque_Alpha;
91 // Use kPalette since Gifs are encoded with a color table.
92 // FIXME: Gifs can actually be encoded with 4-bits per pixel. Using 8 works, but we could skip
93 // expanding to 8 bits and take advantage of the SkSwizzler to work from 4.
94 const auto encodedInfo = SkEncodedInfo::Make(SkEncodedInfo::kPalette_Color, alpha, 8);
95
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
102 const auto imageInfo = SkImageInfo::Make(reader->screenWidth(), reader->screenHeight(),
103 kN32_SkColorType, alphaType,
104 SkColorSpace::MakeSRGB());
105 return std::unique_ptr<SkCodec>(new SkGifCodec(encodedInfo, imageInfo, reader.release()));
106 }
107
onRewind()108 bool SkGifCodec::onRewind() {
109 fReader->clearDecodeState();
110 return true;
111 }
112
SkGifCodec(const SkEncodedInfo & encodedInfo,const SkImageInfo & imageInfo,SkGifImageReader * reader)113 SkGifCodec::SkGifCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo,
114 SkGifImageReader* reader)
115 : INHERITED(encodedInfo, imageInfo, SkColorSpaceXform::kRGBA_8888_ColorFormat, nullptr)
116 , fReader(reader)
117 , fTmpBuffer(nullptr)
118 , fSwizzler(nullptr)
119 , fCurrColorTable(nullptr)
120 , fCurrColorTableIsReal(false)
121 , fFilledBackground(false)
122 , fFirstCallToIncrementalDecode(false)
123 , fDst(nullptr)
124 , fDstRowBytes(0)
125 , fRowsDecoded(0)
126 {
127 reader->setClient(this);
128 }
129
onGetFrameCount()130 int SkGifCodec::onGetFrameCount() {
131 fReader->parse(SkGifImageReader::SkGIFFrameCountQuery);
132 return fReader->imagesCount();
133 }
134
onGetFrameInfo(int i,SkCodec::FrameInfo * frameInfo) const135 bool SkGifCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
136 if (i >= fReader->imagesCount()) {
137 return false;
138 }
139
140 const SkGIFFrameContext* frameContext = fReader->frameContext(i);
141 SkASSERT(frameContext->reachedStartOfData());
142
143 if (frameInfo) {
144 frameInfo->fDuration = frameContext->getDuration();
145 frameInfo->fRequiredFrame = frameContext->getRequiredFrame();
146 frameInfo->fFullyReceived = frameContext->isComplete();
147 frameInfo->fAlphaType = frameContext->hasAlpha() ? kUnpremul_SkAlphaType
148 : kOpaque_SkAlphaType;
149 frameInfo->fDisposalMethod = frameContext->getDisposalMethod();
150 }
151 return true;
152 }
153
onGetRepetitionCount()154 int SkGifCodec::onGetRepetitionCount() {
155 fReader->parse(SkGifImageReader::SkGIFLoopCountQuery);
156 return fReader->loopCount();
157 }
158
159 static constexpr SkColorType kXformSrcColorType = kRGBA_8888_SkColorType;
160 static constexpr SkAlphaType kXformAlphaType = kUnpremul_SkAlphaType;
161
initializeColorTable(const SkImageInfo & dstInfo,int frameIndex)162 void SkGifCodec::initializeColorTable(const SkImageInfo& dstInfo, int frameIndex) {
163 SkColorType colorTableColorType = dstInfo.colorType();
164 if (this->colorXform()) {
165 colorTableColorType = kXformSrcColorType;
166 }
167
168 sk_sp<SkColorTable> currColorTable = fReader->getColorTable(colorTableColorType, frameIndex);
169 fCurrColorTableIsReal = static_cast<bool>(currColorTable);
170 if (!fCurrColorTableIsReal) {
171 // This is possible for an empty frame. Create a dummy with one value (transparent).
172 SkPMColor color = SK_ColorTRANSPARENT;
173 fCurrColorTable.reset(new SkColorTable(&color, 1));
174 } else if (this->colorXform() && !this->xformOnDecode()) {
175 SkPMColor dstColors[256];
176 this->applyColorXform(dstColors, currColorTable->readColors(), currColorTable->count(),
177 kXformAlphaType);
178 fCurrColorTable.reset(new SkColorTable(dstColors, currColorTable->count()));
179 } else {
180 fCurrColorTable = std::move(currColorTable);
181 }
182 }
183
184
prepareToDecode(const SkImageInfo & dstInfo,const Options & opts)185 SkCodec::Result SkGifCodec::prepareToDecode(const SkImageInfo& dstInfo, const Options& opts) {
186 if (opts.fSubset) {
187 return gif_error("Subsets not supported.\n", kUnimplemented);
188 }
189
190 const int frameIndex = opts.fFrameIndex;
191 if (frameIndex > 0 && kRGB_565_SkColorType == dstInfo.colorType()) {
192 // FIXME: In theory, we might be able to support this, but it's not clear that it
193 // is necessary (Chromium does not decode to 565, and Android does not decode
194 // frames beyond the first). Disabling it because it is somewhat difficult:
195 // - If there is a transparent pixel, and this frame draws on top of another frame
196 // (if the frame is independent with a transparent pixel, we should not decode to
197 // 565 anyway, since it is not opaque), we need to skip drawing the transparent
198 // pixels (see writeTransparentPixels in haveDecodedRow). We currently do this by
199 // first swizzling into temporary memory, then copying into the destination. (We
200 // let the swizzler handle it first because it may need to sample.) After
201 // swizzling to 565, we do not know which pixels in our temporary memory
202 // correspond to the transparent pixel, so we do not know what to skip. We could
203 // special case the non-sampled case (no need to swizzle), but as this is
204 // currently unused we can just not support it.
205 return gif_error("Cannot decode multiframe gif (except frame 0) as 565.\n",
206 kInvalidConversion);
207 }
208
209 const auto* frame = fReader->frameContext(frameIndex);
210 SkASSERT(frame);
211 if (0 == frameIndex) {
212 // SkCodec does not have a way to just parse through frame 0, so we
213 // have to do so manually, here.
214 fReader->parse((SkGifImageReader::SkGIFParseQuery) 0);
215 if (!frame->reachedStartOfData()) {
216 // We have parsed enough to know that there is a color map, but cannot
217 // parse the map itself yet. Exit now, so we do not build an incorrect
218 // table.
219 return gif_error("color map not available yet\n", kIncompleteInput);
220 }
221 } else {
222 // Parsing happened in SkCodec::getPixels.
223 SkASSERT(frameIndex < fReader->imagesCount());
224 SkASSERT(frame->reachedStartOfData());
225 }
226
227 if (this->xformOnDecode()) {
228 fXformBuffer.reset(new uint32_t[dstInfo.width()]);
229 sk_bzero(fXformBuffer.get(), dstInfo.width() * sizeof(uint32_t));
230 }
231
232 fTmpBuffer.reset(new uint8_t[dstInfo.minRowBytes()]);
233
234 this->initializeColorTable(dstInfo, frameIndex);
235 this->initializeSwizzler(dstInfo, frameIndex);
236
237 SkASSERT(fCurrColorTable);
238 return kSuccess;
239 }
240
initializeSwizzler(const SkImageInfo & dstInfo,int frameIndex)241 void SkGifCodec::initializeSwizzler(const SkImageInfo& dstInfo, int frameIndex) {
242 const SkGIFFrameContext* frame = fReader->frameContext(frameIndex);
243 // This is only called by prepareToDecode, which ensures frameIndex is in range.
244 SkASSERT(frame);
245
246 const int xBegin = frame->xOffset();
247 const int xEnd = std::min(frame->frameRect().right(), fReader->screenWidth());
248
249 // CreateSwizzler only reads left and right of the frame. We cannot use the frame's raw
250 // frameRect, since it might extend beyond the edge of the frame.
251 SkIRect swizzleRect = SkIRect::MakeLTRB(xBegin, 0, xEnd, 0);
252
253 SkImageInfo swizzlerInfo = dstInfo;
254 if (this->colorXform()) {
255 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
256 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
257 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
258 }
259 }
260
261 // The default Options should be fine:
262 // - we'll ignore if the memory is zero initialized - unless we're the first frame, this won't
263 // matter anyway.
264 // - subsets are not supported for gif
265 // - the swizzler does not need to know about the frame.
266 // We may not be able to use the real Options anyway, since getPixels does not store it (due to
267 // a bug).
268 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(),
269 fCurrColorTable->readColors(), swizzlerInfo, Options(), &swizzleRect));
270 SkASSERT(fSwizzler.get());
271 }
272
273 /*
274 * Initiates the gif decode
275 */
onGetPixels(const SkImageInfo & dstInfo,void * pixels,size_t dstRowBytes,const Options & opts,int * rowsDecoded)276 SkCodec::Result SkGifCodec::onGetPixels(const SkImageInfo& dstInfo,
277 void* pixels, size_t dstRowBytes,
278 const Options& opts,
279 int* rowsDecoded) {
280 Result result = this->prepareToDecode(dstInfo, opts);
281 switch (result) {
282 case kSuccess:
283 break;
284 case kIncompleteInput:
285 // onStartIncrementalDecode treats this as incomplete, since it may
286 // provide more data later, but in this case, no more data will be
287 // provided, and there is nothing to draw. We also cannot return
288 // kIncompleteInput, which will make SkCodec attempt to fill
289 // remaining rows, but that requires an SkSwizzler, which we have
290 // not created.
291 return kInvalidInput;
292 default:
293 return result;
294 }
295
296 if (dstInfo.dimensions() != this->getInfo().dimensions()) {
297 return gif_error("Scaling not supported.\n", kInvalidScale);
298 }
299
300 fDst = pixels;
301 fDstRowBytes = dstRowBytes;
302
303 return this->decodeFrame(true, opts, rowsDecoded);
304 }
305
onStartIncrementalDecode(const SkImageInfo & dstInfo,void * pixels,size_t dstRowBytes,const SkCodec::Options & opts)306 SkCodec::Result SkGifCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
307 void* pixels, size_t dstRowBytes,
308 const SkCodec::Options& opts) {
309 Result result = this->prepareToDecode(dstInfo, opts);
310 if (result != kSuccess) {
311 return result;
312 }
313
314 fDst = pixels;
315 fDstRowBytes = dstRowBytes;
316
317 fFirstCallToIncrementalDecode = true;
318
319 return kSuccess;
320 }
321
onIncrementalDecode(int * rowsDecoded)322 SkCodec::Result SkGifCodec::onIncrementalDecode(int* rowsDecoded) {
323 // It is possible the client has appended more data. Parse, if needed.
324 const auto& options = this->options();
325 const int frameIndex = options.fFrameIndex;
326 fReader->parse((SkGifImageReader::SkGIFParseQuery) frameIndex);
327
328 const bool firstCallToIncrementalDecode = fFirstCallToIncrementalDecode;
329 fFirstCallToIncrementalDecode = false;
330 return this->decodeFrame(firstCallToIncrementalDecode, options, rowsDecoded);
331 }
332
decodeFrame(bool firstAttempt,const Options & opts,int * rowsDecoded)333 SkCodec::Result SkGifCodec::decodeFrame(bool firstAttempt, const Options& opts, int* rowsDecoded) {
334 const SkImageInfo& dstInfo = this->dstInfo();
335 const int frameIndex = opts.fFrameIndex;
336 SkASSERT(frameIndex < fReader->imagesCount());
337 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
338 if (firstAttempt) {
339 // rowsDecoded reports how many rows have been initialized, so a layer above
340 // can fill the rest. In some cases, we fill the background before decoding
341 // (or it is already filled for us), so we report rowsDecoded to be the full
342 // height.
343 bool filledBackground = false;
344 if (frameContext->getRequiredFrame() == kNone) {
345 // We may need to clear to transparent for one of the following reasons:
346 // - The frameRect does not cover the full bounds. haveDecodedRow will
347 // only draw inside the frameRect, so we need to clear the rest.
348 // - The frame is interlaced. There is no obvious way to fill
349 // afterwards for an incomplete image. (FIXME: Does the first pass
350 // cover all rows? If so, we do not have to fill here.)
351 // - There is no color table for this frame. In that case will not
352 // draw anything, so we need to fill.
353 if (frameContext->frameRect() != this->getInfo().bounds()
354 || frameContext->interlaced() || !fCurrColorTableIsReal) {
355 // fill ignores the width (replaces it with the actual, scaled width).
356 // But we need to scale in Y.
357 const int scaledHeight = get_scaled_dimension(dstInfo.height(),
358 fSwizzler->sampleY());
359 auto fillInfo = dstInfo.makeWH(0, scaledHeight);
360 fSwizzler->fill(fillInfo, fDst, fDstRowBytes, this->getFillValue(dstInfo),
361 opts.fZeroInitialized);
362 filledBackground = true;
363 }
364 } else {
365 // Not independent.
366 // SkCodec ensured that the prior frame has been decoded.
367 filledBackground = true;
368 }
369
370 fFilledBackground = filledBackground;
371 if (filledBackground) {
372 // Report the full (scaled) height, since the client will never need to fill.
373 fRowsDecoded = get_scaled_dimension(dstInfo.height(), fSwizzler->sampleY());
374 } else {
375 // This will be updated by haveDecodedRow.
376 fRowsDecoded = 0;
377 }
378 }
379
380 if (!fCurrColorTableIsReal) {
381 // Nothing to draw this frame.
382 return kSuccess;
383 }
384
385 bool frameDecoded = false;
386 const bool fatalError = !fReader->decode(frameIndex, &frameDecoded);
387 if (fatalError || !frameDecoded) {
388 if (rowsDecoded) {
389 *rowsDecoded = fRowsDecoded;
390 }
391 if (fatalError) {
392 return kErrorInInput;
393 }
394 return kIncompleteInput;
395 }
396
397 return kSuccess;
398 }
399
onGetFillValue(const SkImageInfo & dstInfo) const400 uint64_t SkGifCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
401 // Using transparent as the fill value matches the behavior in Chromium,
402 // which ignores the background color.
403 return SK_ColorTRANSPARENT;
404 }
405
applyXformRow(const SkImageInfo & dstInfo,void * dst,const uint8_t * src) const406 void SkGifCodec::applyXformRow(const SkImageInfo& dstInfo, void* dst, const uint8_t* src) const {
407 if (this->xformOnDecode()) {
408 SkASSERT(this->colorXform());
409 fSwizzler->swizzle(fXformBuffer.get(), src);
410
411 const int xformWidth = get_scaled_dimension(dstInfo.width(), fSwizzler->sampleX());
412 this->applyColorXform(dst, fXformBuffer.get(), xformWidth, kXformAlphaType);
413 } else {
414 fSwizzler->swizzle(dst, src);
415 }
416 }
417
418 template <typename T>
blend_line(void * dstAsVoid,const void * srcAsVoid,int width)419 static void blend_line(void* dstAsVoid, const void* srcAsVoid, int width) {
420 T* dst = reinterpret_cast<T*>(dstAsVoid);
421 const T* src = reinterpret_cast<const T*>(srcAsVoid);
422 while (width --> 0) {
423 if (*src != 0) { // GIF pixels are either transparent (== 0) or opaque (!= 0).
424 *dst = *src;
425 }
426 src++;
427 dst++;
428 }
429 }
430
haveDecodedRow(int frameIndex,const unsigned char * rowBegin,int rowNumber,int repeatCount,bool writeTransparentPixels)431 void SkGifCodec::haveDecodedRow(int frameIndex, const unsigned char* rowBegin,
432 int rowNumber, int repeatCount, bool writeTransparentPixels)
433 {
434 const SkGIFFrameContext* frameContext = fReader->frameContext(frameIndex);
435 // The pixel data and coordinates supplied to us are relative to the frame's
436 // origin within the entire image size, i.e.
437 // (frameContext->xOffset, frameContext->yOffset). There is no guarantee
438 // that width == (size().width() - frameContext->xOffset), so
439 // we must ensure we don't run off the end of either the source data or the
440 // row's X-coordinates.
441 const int width = frameContext->width();
442 const int xBegin = frameContext->xOffset();
443 const int yBegin = frameContext->yOffset() + rowNumber;
444 const int xEnd = std::min(xBegin + width, this->getInfo().width());
445 const int yEnd = std::min(yBegin + rowNumber + repeatCount, this->getInfo().height());
446 // FIXME: No need to make the checks on width/xBegin/xEnd for every row. We could instead do
447 // this once in prepareToDecode.
448 if (!width || (xBegin < 0) || (yBegin < 0) || (xEnd <= xBegin) || (yEnd <= yBegin))
449 return;
450
451 // yBegin is the first row in the non-sampled image. dstRow will be the row in the output,
452 // after potentially scaling it.
453 int dstRow = yBegin;
454
455 const int sampleY = fSwizzler->sampleY();
456 if (sampleY > 1) {
457 // Check to see whether this row or one that falls in the repeatCount is needed in the
458 // output.
459 bool foundNecessaryRow = false;
460 for (int i = 0; i < repeatCount; i++) {
461 const int potentialRow = yBegin + i;
462 if (fSwizzler->rowNeeded(potentialRow)) {
463 dstRow = potentialRow / sampleY;
464 const int scaledHeight = get_scaled_dimension(this->dstInfo().height(), sampleY);
465 if (dstRow >= scaledHeight) {
466 return;
467 }
468
469 foundNecessaryRow = true;
470 repeatCount -= i;
471
472 repeatCount = (repeatCount - 1) / sampleY + 1;
473
474 // Make sure the repeatCount does not take us beyond the end of the dst
475 if (dstRow + repeatCount > scaledHeight) {
476 repeatCount = scaledHeight - dstRow;
477 SkASSERT(repeatCount >= 1);
478 }
479 break;
480 }
481 }
482
483 if (!foundNecessaryRow) {
484 return;
485 }
486 } else {
487 // Make sure the repeatCount does not take us beyond the end of the dst
488 SkASSERT(this->dstInfo().height() >= yBegin);
489 repeatCount = SkTMin(repeatCount, this->dstInfo().height() - yBegin);
490 }
491
492 if (!fFilledBackground) {
493 // At this point, we are definitely going to write the row, so count it towards the number
494 // of rows decoded.
495 // We do not consider the repeatCount, which only happens for interlaced, in which case we
496 // have already set fRowsDecoded to the proper value (reflecting that we have filled the
497 // background).
498 fRowsDecoded++;
499 }
500
501 // decodeFrame will early exit if this is false, so this method will not be
502 // called.
503 SkASSERT(fCurrColorTableIsReal);
504
505 // The swizzler takes care of offsetting into the dst width-wise.
506 void* dstLine = SkTAddOffset<void>(fDst, dstRow * fDstRowBytes);
507
508 // We may or may not need to write transparent pixels to the buffer.
509 // If we're compositing against a previous image, it's wrong, but if
510 // we're decoding an interlaced gif and displaying it "Haeberli"-style,
511 // we must write these for passes beyond the first, or the initial passes
512 // will "show through" the later ones.
513 const auto dstInfo = this->dstInfo();
514 if (writeTransparentPixels) {
515 this->applyXformRow(dstInfo, dstLine, rowBegin);
516 } else {
517 this->applyXformRow(dstInfo, fTmpBuffer.get(), rowBegin);
518
519 size_t offsetBytes = fSwizzler->swizzleOffsetBytes();
520 if (dstInfo.colorType() == kRGBA_F16_SkColorType) {
521 // Account for the fact that post-swizzling we converted to F16,
522 // which is twice as wide.
523 offsetBytes *= 2;
524 }
525 const void* src = SkTAddOffset<void>(fTmpBuffer.get(), offsetBytes);
526 void* dst = SkTAddOffset<void>(dstLine, offsetBytes);
527
528 switch (dstInfo.colorType()) {
529 case kBGRA_8888_SkColorType:
530 case kRGBA_8888_SkColorType:
531 blend_line<uint32_t>(dst, src, fSwizzler->swizzleWidth());
532 break;
533 case kRGBA_F16_SkColorType:
534 blend_line<uint64_t>(dst, src, fSwizzler->swizzleWidth());
535 break;
536 default:
537 SkASSERT(false);
538 return;
539 }
540 }
541
542 // Tell the frame to copy the row data if need be.
543 if (repeatCount > 1) {
544 const size_t bytesPerPixel = SkColorTypeBytesPerPixel(this->dstInfo().colorType());
545 const size_t bytesToCopy = fSwizzler->swizzleWidth() * bytesPerPixel;
546 void* copiedLine = SkTAddOffset<void>(dstLine, fSwizzler->swizzleOffsetBytes());
547 void* dst = copiedLine;
548 for (int i = 1; i < repeatCount; i++) {
549 dst = SkTAddOffset<void>(dst, fDstRowBytes);
550 memcpy(dst, copiedLine, bytesToCopy);
551 }
552 }
553 }
554