New GIF codec; new third_party/wuffs dep

Bug: skia:8235
Change-Id: I883e05bc50c48f822b5ac3884f25ae67d21c94a9
Reviewed-on: https://skia-review.googlesource.com/c/136940
Commit-Queue: Leon Scroggins <scroggo@google.com>
Reviewed-by: Leon Scroggins <scroggo@google.com>
This commit is contained in:
Nigel Tao 2018-10-21 16:29:38 +11:00 committed by Skia Commit-Bot
parent 4f598e8c82
commit 60009388be
11 changed files with 1209 additions and 10 deletions

View File

@ -32,6 +32,7 @@ declare_args() {
skia_use_lua = is_skia_dev_build && !is_ios
skia_use_opencl = false
skia_use_piex = !is_win
skia_use_wuffs = false
skia_use_zlib = true
skia_use_metal = false
skia_use_libheif = is_skia_dev_build
@ -802,6 +803,18 @@ optional("webp") {
]
}
optional("wuffs") {
enabled = skia_use_wuffs
public_defines = [ "SK_HAS_WUFFS_LIBRARY" ]
deps = [
"//third_party/wuffs",
]
sources = [
"src/codec/SkWuffsCodec.cpp",
]
}
optional("xml") {
enabled = skia_use_expat
public_defines = [ "SK_XML" ]
@ -874,6 +887,7 @@ component("skia") {
":sse42",
":ssse3",
":webp",
":wuffs",
":xml",
]

1
DEPS
View File

@ -32,6 +32,7 @@ deps = {
"third_party/externals/spirv-tools" : "https://skia.googlesource.com/external/github.com/KhronosGroup/SPIRV-Tools.git@e9e4393b1c5aad7553c05782acefbe32b42644bd",
"third_party/externals/swiftshader" : "https://swiftshader.googlesource.com/SwiftShader@805d7616a05e2465a47dce0db6ddb389846a0ff8",
#"third_party/externals/v8" : "https://chromium.googlesource.com/v8/v8.git@5f1ae66d5634e43563b2d25ea652dfb94c31a3b4",
"third_party/externals/wuffs" : "https://github.com/google/wuffs.git@b5c47e273f7f8862bcf04976453d0ec81e6e6650",
"third_party/externals/zlib" : "https://chromium.googlesource.com/chromium/src/third_party/zlib@ea3ba903faac98b64b2bf8de5e98cd97b335a474",
"third_party/externals/Nima-Cpp" : "https://github.com/2d-inc/Nima-Cpp.git@4bd02269d7d1d2e650950411325eafa15defb084",
"third_party/externals/Nima-Math-Cpp" : "https://github.com/2d-inc/Nima-Math-Cpp.git@e0c12772093fa8860f55358274515b86885f0108",

View File

@ -11,7 +11,6 @@
#include "SkColorSpace.h"
#include "SkData.h"
#include "SkFrameHolder.h"
#include "SkGifCodec.h"
#include "SkHalf.h"
#ifdef SK_HAS_HEIF_LIBRARY
#include "SkHeifCodec.h"
@ -25,6 +24,11 @@
#include "SkStream.h"
#include "SkWbmpCodec.h"
#include "SkWebpCodec.h"
#ifdef SK_HAS_WUFFS_LIBRARY
#include "SkWuffsCodec.h"
#else
#include "SkGifCodec.h"
#endif
struct DecoderProc {
bool (*IsFormat)(const void*, size_t);
@ -38,7 +42,11 @@ static constexpr DecoderProc gDecoderProcs[] = {
#ifdef SK_HAS_WEBP_LIBRARY
{ SkWebpCodec::IsWebp, SkWebpCodec::MakeFromStream },
#endif
#ifdef SK_HAS_WUFFS_LIBRARY
{ SkWuffsCodec_IsFormat, SkWuffsCodec_MakeFromStream },
#else
{ SkGifCodec::IsGif, SkGifCodec::MakeFromStream },
#endif
#ifdef SK_HAS_PNG_LIBRARY
{ SkIcoCodec::IsIco, SkIcoCodec::MakeFromStream },
#endif

793
src/codec/SkWuffsCodec.cpp Normal file
View File

@ -0,0 +1,793 @@
/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkWuffsCodec.h"
#include "../private/SkMalloc.h"
#include "SkFrameHolder.h"
#include "SkSampler.h"
#include "wuffs-v0.2.h"
#define SK_WUFFS_CODEC_BUFFER_SIZE 4096
// TODO(nigeltao): use a swizzler instead of load_u32le and store_etc.
static inline uint32_t load_u32le(uint8_t* p) {
return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | ((uint32_t)(p[2]) << 16) |
((uint32_t)(p[3]) << 24);
}
static inline void store_u32le(uint8_t* p, uint32_t x) {
p[0] = x >> 0;
p[1] = x >> 8;
p[2] = x >> 16;
p[3] = x >> 24;
}
static inline void store_u32le_switched(uint8_t* p, uint32_t x) {
// This could probably be optimized, but in any case, we should use a
// swizzler.
p[0] = x >> 16;
p[1] = x >> 8;
p[2] = x >> 0;
p[3] = x >> 24;
}
static inline void store_565(uint8_t* p, uint32_t argb) {
uint32_t r5 = 0x1F & (argb >> ((8 - 5) + 16));
uint32_t g6 = 0x3F & (argb >> ((8 - 6) + 8));
uint32_t b5 = 0x1F & (argb >> ((8 - 5) + 0));
p[0] = (b5 << 0) | (g6 << 5);
p[1] = (g6 >> 3) | (r5 << 3);
}
static bool fill_buffer(wuffs_base__io_buffer* b, SkStream* s) {
b->compact();
size_t num_read = s->read(b->data.ptr + b->meta.wi, b->data.len - b->meta.wi);
b->meta.wi += num_read;
b->meta.closed = s->isAtEnd();
return num_read > 0;
}
static bool seek_buffer(wuffs_base__io_buffer* b, SkStream* s, uint64_t pos) {
// Try to re-position the io_buffer's meta.ri read-index first, which is
// cheaper than seeking in the backing SkStream.
if ((pos >= b->meta.pos) && (pos - b->meta.pos <= b->meta.wi)) {
b->meta.ri = pos - b->meta.pos;
return true;
}
// Seek in the backing SkStream.
if ((pos > SIZE_MAX) || (!s->seek(pos))) {
return false;
}
b->meta.wi = 0;
b->meta.ri = 0;
b->meta.pos = pos;
b->meta.closed = false;
return true;
}
static SkEncodedInfo::Alpha wuffs_blend_to_skia_alpha(wuffs_base__animation_blend w) {
return (w == WUFFS_BASE__ANIMATION_BLEND__OPAQUE) ? SkEncodedInfo::kOpaque_Alpha
: SkEncodedInfo::kUnpremul_Alpha;
}
static SkCodecAnimation::Blend wuffs_blend_to_skia_blend(wuffs_base__animation_blend w) {
return (w == WUFFS_BASE__ANIMATION_BLEND__SRC) ? SkCodecAnimation::Blend::kBG
: SkCodecAnimation::Blend::kPriorFrame;
}
static SkCodecAnimation::DisposalMethod wuffs_disposal_to_skia_disposal(
wuffs_base__animation_disposal w) {
switch (w) {
case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_BACKGROUND:
return SkCodecAnimation::DisposalMethod::kRestoreBGColor;
case WUFFS_BASE__ANIMATION_DISPOSAL__RESTORE_PREVIOUS:
return SkCodecAnimation::DisposalMethod::kRestorePrevious;
default:
return SkCodecAnimation::DisposalMethod::kKeep;
}
}
// -------------------------------- Class definitions
class SkWuffsCodec;
class SkWuffsFrame final : public SkFrame {
public:
SkWuffsFrame(wuffs_base__frame_config* fc);
SkCodec::FrameInfo frameInfo(bool fullyReceived) const;
uint64_t ioPosition() const;
// SkFrame overrides.
SkEncodedInfo::Alpha onReportedAlpha() const override;
private:
uint64_t fIOPosition;
SkEncodedInfo::Alpha fReportedAlpha;
typedef SkFrame INHERITED;
};
// SkWuffsFrameHolder is a trivial indirector that forwards its calls onto a
// SkWuffsCodec. It is a separate class as SkWuffsCodec would otherwise
// inherit from both SkCodec and SkFrameHolder, and Skia style discourages
// multiple inheritance (e.g. with its "typedef Foo INHERITED" convention).
class SkWuffsFrameHolder final : public SkFrameHolder {
public:
SkWuffsFrameHolder() : INHERITED() {}
void init(SkWuffsCodec* codec, int width, int height);
// SkFrameHolder overrides.
const SkFrame* onGetFrame(int i) const override;
private:
const SkWuffsCodec* fCodec;
typedef SkFrameHolder INHERITED;
};
class SkWuffsCodec final : public SkCodec {
public:
SkWuffsCodec(SkEncodedInfo&& encodedInfo,
std::unique_ptr<SkStream> stream,
std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr,
std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
size_t workbuf_len,
wuffs_base__image_config imgcfg,
wuffs_base__pixel_buffer pixbuf,
wuffs_base__io_buffer iobuf);
const SkWuffsFrame* frame(int i) const;
private:
// SkCodec overrides.
SkEncodedImageFormat onGetEncodedFormat() const override;
Result onGetPixels(const SkImageInfo&, void*, size_t, const Options&, int*) override;
const SkFrameHolder* getFrameHolder() const override;
Result onStartIncrementalDecode(const SkImageInfo& dstInfo,
void* dst,
size_t rowBytes,
const SkCodec::Options& options) override;
Result onIncrementalDecode(int* rowsDecoded) override;
int onGetFrameCount() override;
bool onGetFrameInfo(int, FrameInfo*) const override;
int onGetRepetitionCount() override;
void readFrames();
Result seekFrame(int frameIndex);
Result resetDecoder();
const char* decodeFrameConfig();
const char* decodeFrame();
void updateNumFullyReceivedFrames();
SkWuffsFrameHolder fFrameHolder;
std::unique_ptr<SkStream> fStream;
std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> fDecoder;
std::unique_ptr<uint8_t, decltype(&sk_free)> fPixbufPtr;
std::unique_ptr<uint8_t, decltype(&sk_free)> fWorkbufPtr;
size_t fWorkbufLen;
const uint64_t fFirstFrameIOPosition;
wuffs_base__frame_config fFrameConfig;
wuffs_base__pixel_buffer fPixelBuffer;
wuffs_base__io_buffer fIOBuffer;
// Incremental decoding state.
SkColorType fIncrDecColorType;
uint8_t* fIncrDecDst;
bool fIncrDecHaveFrameConfig;
size_t fIncrDecRowBytes;
uint64_t fNumFullyReceivedFrames;
std::vector<SkWuffsFrame> fFrames;
bool fFramesComplete;
// If calling an fDecoder method returns an incomplete status, then
// fDecoder is suspended in a coroutine (i.e. waiting on I/O or halted on a
// non-recoverable error). To keep its internal proof-of-safety invariants
// consistent, there's only two things you can safely do with a suspended
// Wuffs object: resume the coroutine, or reset all state (memset to zero
// and start again).
//
// If fDecoderIsSuspended, and we aren't sure that we're going to resume
// the coroutine, then we will need to call this->resetDecoder before
// calling other fDecoder methods.
bool fDecoderIsSuspended;
uint8_t fBuffer[SK_WUFFS_CODEC_BUFFER_SIZE];
typedef SkCodec INHERITED;
};
// -------------------------------- SkWuffsFrame implementation
SkWuffsFrame::SkWuffsFrame(wuffs_base__frame_config* fc)
: INHERITED(fc->index()),
fIOPosition(fc->io_position()),
fReportedAlpha(wuffs_blend_to_skia_alpha(fc->blend())) {
wuffs_base__rect_ie_u32 r = fc->bounds();
this->setXYWH(r.min_incl_x, r.min_incl_y, r.width(), r.height());
this->setDisposalMethod(wuffs_disposal_to_skia_disposal(fc->disposal()));
this->setDuration(fc->duration() / WUFFS_BASE__FLICKS_PER_MILLISECOND);
this->setBlend(wuffs_blend_to_skia_blend(fc->blend()));
}
SkCodec::FrameInfo SkWuffsFrame::frameInfo(bool fullyReceived) const {
return ((SkCodec::FrameInfo){
.fRequiredFrame = getRequiredFrame(),
.fDuration = getDuration(),
.fFullyReceived = fullyReceived,
.fAlphaType = hasAlpha() ? kUnpremul_SkAlphaType : kOpaque_SkAlphaType,
.fDisposalMethod = getDisposalMethod(),
});
}
uint64_t SkWuffsFrame::ioPosition() const {
return fIOPosition;
}
SkEncodedInfo::Alpha SkWuffsFrame::onReportedAlpha() const {
return fReportedAlpha;
}
// -------------------------------- SkWuffsFrameHolder implementation
void SkWuffsFrameHolder::init(SkWuffsCodec* codec, int width, int height) {
fCodec = codec;
// Initialize SkFrameHolder's (the superclass) fields.
fScreenWidth = width;
fScreenHeight = height;
}
const SkFrame* SkWuffsFrameHolder::onGetFrame(int i) const {
return fCodec->frame(i);
};
// -------------------------------- SkWuffsCodec implementation
SkWuffsCodec::SkWuffsCodec(SkEncodedInfo&& encodedInfo,
std::unique_ptr<SkStream> stream,
std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> dec,
std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr,
std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr,
size_t workbuf_len,
wuffs_base__image_config imgcfg,
wuffs_base__pixel_buffer pixbuf,
wuffs_base__io_buffer iobuf)
: INHERITED(std::move(encodedInfo),
skcms_PixelFormat_RGBA_8888,
// Pass a nullptr SkStream to the SkCodec constructor. We
// manage the stream ourselves, as the default SkCodec behavior
// is too trigger-happy on rewinding the stream.
nullptr),
fStream(std::move(stream)),
fDecoder(std::move(dec)),
fPixbufPtr(std::move(pixbuf_ptr)),
fWorkbufPtr(std::move(workbuf_ptr)),
fWorkbufLen(workbuf_len),
fFirstFrameIOPosition(imgcfg.first_frame_io_position()),
fFrameConfig((wuffs_base__frame_config){}),
fPixelBuffer(pixbuf),
fIOBuffer((wuffs_base__io_buffer){}),
fIncrDecColorType(kUnknown_SkColorType),
fIncrDecDst(nullptr),
fIncrDecHaveFrameConfig(false),
fIncrDecRowBytes(0),
fNumFullyReceivedFrames(0),
fFramesComplete(false),
fDecoderIsSuspended(false) {
fFrameHolder.init(this, imgcfg.pixcfg.width(), imgcfg.pixcfg.height());
// Initialize fIOBuffer's fields, copying any outstanding data from iobuf to
// fIOBuffer, as iobuf's backing array may not be valid for the lifetime of
// this SkWuffsCodec object, but fIOBuffer's backing array (fBuffer) is.
SkASSERT(iobuf.data.len == SK_WUFFS_CODEC_BUFFER_SIZE);
memmove(fBuffer, iobuf.data.ptr, iobuf.meta.wi);
fIOBuffer = ((wuffs_base__io_buffer){
.data = ((wuffs_base__slice_u8){
.ptr = fBuffer,
.len = SK_WUFFS_CODEC_BUFFER_SIZE,
}),
.meta = iobuf.meta,
});
}
const SkWuffsFrame* SkWuffsCodec::frame(int i) const {
if ((0 <= i) && (static_cast<size_t>(i) < fFrames.size())) {
return &fFrames[i];
}
return nullptr;
}
SkEncodedImageFormat SkWuffsCodec::onGetEncodedFormat() const {
return SkEncodedImageFormat::kGIF;
}
SkCodec::Result SkWuffsCodec::onGetPixels(const SkImageInfo& dstInfo,
void* dst,
size_t rowBytes,
const Options& options,
int* rowsDecoded) {
SkCodec::Result result = this->onStartIncrementalDecode(dstInfo, dst, rowBytes, options);
if (result != kSuccess) {
return result;
}
return this->onIncrementalDecode(rowsDecoded);
}
const SkFrameHolder* SkWuffsCodec::getFrameHolder() const {
return &fFrameHolder;
}
SkCodec::Result SkWuffsCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
void* dst,
size_t rowBytes,
const SkCodec::Options& options) {
if (options.fSubset) {
return SkCodec::kUnimplemented;
}
SkCodec::Result result = this->seekFrame(options.fFrameIndex);
if (result != SkCodec::kSuccess) {
return result;
}
SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized);
fIncrDecColorType = dstInfo.colorType();
fIncrDecDst = static_cast<uint8_t*>(dst);
fIncrDecHaveFrameConfig = false;
fIncrDecRowBytes = rowBytes;
return SkCodec::kSuccess;
}
SkCodec::Result SkWuffsCodec::onIncrementalDecode(int* rowsDecoded) {
if (!fIncrDecDst) {
return SkCodec::kInternalError;
}
if (!fIncrDecHaveFrameConfig) {
const char* status = this->decodeFrameConfig();
if (status == nullptr) {
// No-op.
} else if (status == wuffs_base__suspension__short_read) {
return SkCodec::kIncompleteInput;
} else {
SkCodecPrintf("decodeFrameConfig: %s", status);
return SkCodec::kErrorInInput;
}
fIncrDecHaveFrameConfig = true;
}
SkCodec::Result result = SkCodec::kSuccess;
const char* status = this->decodeFrame();
if (status == nullptr) {
// No-op.
} else if (status == wuffs_base__suspension__short_read) {
result = SkCodec::kIncompleteInput;
} else {
SkCodecPrintf("decodeFrame: %s", status);
return SkCodec::kErrorInInput;
}
// TODO(nigeltao): use a swizzler, once I figure out how it works. For
// now, a C style load/store loop gets the job done.
wuffs_base__rect_ie_u32 r = fFrameConfig.bounds();
wuffs_base__table_u8 pixels = fPixelBuffer.plane(0);
wuffs_base__slice_u8 palette = fPixelBuffer.palette();
SkASSERT(palette.len == 4 * 256);
switch (fIncrDecColorType) {
case kRGB_565_SkColorType:
for (uint32_t y = r.min_incl_y; y < r.max_excl_y; y++) {
uint8_t* d = fIncrDecDst + (y * fIncrDecRowBytes) + (r.min_incl_x * 2);
uint8_t* s = pixels.ptr + (y * pixels.stride) + (r.min_incl_x * 1);
for (uint32_t x = r.min_incl_x; x < r.max_excl_x; x++) {
uint8_t index = *s++;
uint32_t argb = load_u32le(palette.ptr + 4 * static_cast<size_t>(index));
store_565(d, argb);
d += 2;
}
}
break;
case kBGRA_8888_SkColorType:
for (uint32_t y = r.min_incl_y; y < r.max_excl_y; y++) {
uint8_t* d = fIncrDecDst + (y * fIncrDecRowBytes) + (r.min_incl_x * 4);
uint8_t* s = pixels.ptr + (y * pixels.stride) + (r.min_incl_x * 1);
for (uint32_t x = r.min_incl_x; x < r.max_excl_x; x++) {
uint8_t index = *s++;
uint32_t argb = load_u32le(palette.ptr + 4 * static_cast<size_t>(index));
store_u32le(d, argb);
d += 4;
}
}
break;
case kRGBA_8888_SkColorType:
for (uint32_t y = r.min_incl_y; y < r.max_excl_y; y++) {
uint8_t* d = fIncrDecDst + (y * fIncrDecRowBytes) + (r.min_incl_x * 4);
uint8_t* s = pixels.ptr + (y * pixels.stride) + (r.min_incl_x * 1);
for (uint32_t x = r.min_incl_x; x < r.max_excl_x; x++) {
uint8_t index = *s++;
uint32_t argb = load_u32le(palette.ptr + 4 * static_cast<size_t>(index));
store_u32le_switched(d, argb);
d += 4;
}
}
break;
default:
return SkCodec::kUnimplemented;
}
// The semantics of *rowsDecoded is: say you have a 10 pixel high image
// (both the frame and the image). If you only decoded the first 3 rows,
// set this to 3, and then SkCodec (or the caller of incrementalDecode)
// would zero-initialize the remaining 7 (unless the memory was already
// zero-initialized).
//
// Now let's say that the image is still 10 pixels high, but the frame is
// from row 5 to 9. If you only decoded 3 rows, but you initialized the
// first 5, you could return 8, and the caller would zero-initialize the
// final 2. For GIF (where a frame can be smaller than the image and can be
// interlaced), we just zero-initialize all 10 rows ahead of time and
// return the height of the image, so the caller knows it doesn't need to
// do anything.
if (rowsDecoded) {
*rowsDecoded = static_cast<int>(fPixelBuffer.pixcfg.height());
}
if (result == SkCodec::kSuccess) {
fIncrDecColorType = kUnknown_SkColorType;
fIncrDecDst = nullptr;
fIncrDecHaveFrameConfig = false;
fIncrDecRowBytes = 0;
}
return result;
}
int SkWuffsCodec::onGetFrameCount() {
if (!fFramesComplete) {
this->readFrames();
this->updateNumFullyReceivedFrames();
}
return fFrames.size();
}
bool SkWuffsCodec::onGetFrameInfo(int i, SkCodec::FrameInfo* frameInfo) const {
const SkWuffsFrame* f = this->frame(i);
if (!f) {
return false;
}
if (frameInfo) {
*frameInfo = f->frameInfo(static_cast<uint64_t>(i) < this->fNumFullyReceivedFrames);
}
return true;
}
int SkWuffsCodec::onGetRepetitionCount() {
// Convert from Wuffs's loop count to Skia's repeat count. Wuffs' uint32_t
// number is how many times to play the loop. Skia's int number is how many
// times to play the loop *after the first play*. Wuffs and Skia use 0 and
// kRepetitionCountInfinite respectively to mean loop forever.
uint32_t n = wuffs_gif__decoder__num_animation_loops(fDecoder.get());
if (n == 0) {
return SkCodec::kRepetitionCountInfinite;
}
n--;
return n < INT_MAX ? n : INT_MAX;
}
void SkWuffsCodec::readFrames() {
size_t n = fFrames.size();
int i = n ? n - 1 : 0;
if (this->seekFrame(i) != SkCodec::kSuccess) {
return;
}
// Iterate through the frames, converting from Wuffs'
// wuffs_base__frame_config type to Skia's SkWuffsFrame type.
for (; i < INT_MAX; i++) {
const char* status = this->decodeFrameConfig();
if (status == nullptr) {
// No-op.
} else if (status == wuffs_base__warning__end_of_data) {
break;
} else {
return;
}
if (static_cast<size_t>(i) < fFrames.size()) {
continue;
}
fFrames.emplace_back(&fFrameConfig);
SkWuffsFrame* f = &fFrames[fFrames.size() - 1];
fFrameHolder.setAlphaAndRequiredFrame(f);
}
fFramesComplete = true;
}
SkCodec::Result SkWuffsCodec::seekFrame(int frameIndex) {
if (fDecoderIsSuspended) {
SkCodec::Result res = this->resetDecoder();
if (res != SkCodec::kSuccess) {
return res;
}
}
uint64_t pos = 0;
if (frameIndex < 0) {
return SkCodec::kInternalError;
} else if (frameIndex == 0) {
pos = fFirstFrameIOPosition;
} else if (static_cast<size_t>(frameIndex) < fFrames.size()) {
pos = fFrames[frameIndex].ioPosition();
} else {
return SkCodec::kInternalError;
}
if (!seek_buffer(&fIOBuffer, fStream.get(), pos)) {
return SkCodec::kInternalError;
}
const char* status = wuffs_gif__decoder__restart_frame(fDecoder.get(), frameIndex,
fIOBuffer.reader_io_position());
if (status != nullptr) {
return SkCodec::kInternalError;
}
return SkCodec::kSuccess;
}
// An overview of the Wuffs decoding API:
//
// An animated image (such as GIF) has an image header and then N frames. The
// image header gives e.g. the overall image's width and height. Each frame
// consists of a frame header (e.g. frame rectangle bounds, display duration)
// and a payload (the pixels).
//
// In Wuffs terminology, there is one image config and then N pairs of
// (frame_config, frame). To decode everything (without knowing N in advance)
// sequentially:
// - call wuffs_gif__decoder::decode_image_config
// - while (true) {
// - call wuffs_gif__decoder::decode_frame_config
// - if that returned wuffs_base__warning__end_of_data, break
// - call wuffs_gif__decoder::decode_frame
// - }
//
// The first argument to each decode_foo method is the destination struct to
// store the decoded information.
//
// For random (instead of sequential) access to an image's frames, call
// wuffs_gif__decoder::restart_frame to prepare to decode the i'th frame.
// Essentially, it restores the state to be at the top of the while loop above.
// The wuffs_base__io_buffer's reader position will also need to be set at the
// right point in the source data stream. The position for the i'th frame is
// calculated by the i'th decode_frame_config call. You can only call
// restart_frame after decode_image_config is called, explicitly or implicitly
// (see below), as decoding a single frame might require for-all-frames
// information like the overall image dimensions and the global palette.
//
// All of those decode_xxx calls are optional. For example, if
// decode_image_config is not called, then the first decode_frame_config call
// will implicitly parse and verify the image header, before parsing the first
// frame's header. Similarly, you can call only decode_frame N times, without
// calling decode_image_config or decode_frame_config, if you already know
// metadata like N and each frame's rectangle bounds by some other means (e.g.
// this is a first party, statically known image).
//
// Specifically, starting with an unknown (but re-windable) GIF image, if you
// want to just find N (i.e. count the number of frames), you can loop calling
// only the decode_frame_config method and avoid calling the more expensive
// decode_frame method. In terms of the underlying GIF image format, this will
// skip over the LZW-encoded pixel data, avoiding the costly LZW decompression.
//
// Those decode_xxx methods are also suspendible. They will return early (with
// a status code that is_suspendible and therefore isn't is_complete) if there
// isn't enough source data to complete the operation: an incremental decode.
// Calling decode_xxx again with additional source data will resume the
// previous operation, instead of starting a new operation. Calling decode_yyy
// whilst decode_xxx is suspended will result in an error.
//
// Once an error is encountered, whether from invalid source data or from a
// programming error such as calling decode_yyy while suspended in decode_xxx,
// all subsequent calls will be no-ops that return an error. To reset the
// decoder into something that does productive work, memset the entire struct
// to zero, check the Wuffs version and then, in order to be able to call
// restart_frame, call decode_image_config. The io_buffer and its associated
// stream will also need to be rewound.
static SkCodec::Result reset_and_decode_image_config(wuffs_gif__decoder* decoder,
wuffs_base__image_config* imgcfg,
wuffs_base__io_buffer* b,
SkStream* s) {
memset(decoder, 0, sizeof__wuffs_gif__decoder());
const char* status = wuffs_gif__decoder__check_wuffs_version(
decoder, sizeof__wuffs_gif__decoder(), WUFFS_VERSION);
if (status != nullptr) {
SkCodecPrintf("check_wuffs_version: %s", status);
return SkCodec::kInternalError;
}
while (true) {
status = wuffs_gif__decoder__decode_image_config(decoder, imgcfg, b->reader());
if (status == nullptr) {
return SkCodec::kSuccess;
} else if (status != wuffs_base__suspension__short_read) {
SkCodecPrintf("decode_image_config: %s", status);
return SkCodec::kErrorInInput;
} else if (!fill_buffer(b, s)) {
return SkCodec::kIncompleteInput;
}
}
}
SkCodec::Result SkWuffsCodec::resetDecoder() {
if (!fStream->rewind()) {
return SkCodec::kInternalError;
}
fIOBuffer.meta = ((wuffs_base__io_buffer_meta){});
SkCodec::Result result =
reset_and_decode_image_config(fDecoder.get(), nullptr, &fIOBuffer, fStream.get());
if (result == SkCodec::kIncompleteInput) {
return SkCodec::kInternalError;
} else if (result != SkCodec::kSuccess) {
return result;
}
fDecoderIsSuspended = false;
return SkCodec::kSuccess;
}
const char* SkWuffsCodec::decodeFrameConfig() {
while (true) {
const char* status = wuffs_gif__decoder__decode_frame_config(fDecoder.get(), &fFrameConfig,
fIOBuffer.reader());
if ((status == wuffs_base__suspension__short_read) &&
fill_buffer(&fIOBuffer, fStream.get())) {
continue;
}
fDecoderIsSuspended = !wuffs_base__status__is_complete(status);
this->updateNumFullyReceivedFrames();
return status;
}
}
const char* SkWuffsCodec::decodeFrame() {
while (true) {
const char* status =
wuffs_gif__decoder__decode_frame(fDecoder.get(), &fPixelBuffer, fIOBuffer.reader(),
((wuffs_base__slice_u8){
.ptr = fWorkbufPtr.get(),
.len = fWorkbufLen,
}),
NULL);
if ((status == wuffs_base__suspension__short_read) &&
fill_buffer(&fIOBuffer, fStream.get())) {
continue;
}
fDecoderIsSuspended = !wuffs_base__status__is_complete(status);
this->updateNumFullyReceivedFrames();
return status;
}
}
void SkWuffsCodec::updateNumFullyReceivedFrames() {
// wuffs_gif__decoder__num_decoded_frames's return value, n, can change
// over time, both up and down, as we seek back and forth in the underlying
// stream. fNumFullyReceivedFrames is the highest n we've seen.
uint64_t n = wuffs_gif__decoder__num_decoded_frames(fDecoder.get());
if (fNumFullyReceivedFrames < n) {
fNumFullyReceivedFrames = n;
}
}
// -------------------------------- SkWuffsCodec.h functions
bool SkWuffsCodec_IsFormat(const void* buf, size_t bytesRead) {
constexpr const char* gif_ptr = "GIF8";
constexpr size_t gif_len = 4;
return (bytesRead >= gif_len) && (memcmp(buf, gif_ptr, gif_len) == 0);
}
std::unique_ptr<SkCodec> SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream> stream,
SkCodec::Result* result) {
uint8_t buffer[SK_WUFFS_CODEC_BUFFER_SIZE];
wuffs_base__io_buffer iobuf = ((wuffs_base__io_buffer){
.data = ((wuffs_base__slice_u8){
.ptr = buffer,
.len = SK_WUFFS_CODEC_BUFFER_SIZE,
}),
.meta = ((wuffs_base__io_buffer_meta){}),
});
wuffs_base__image_config imgcfg = ((wuffs_base__image_config){});
// Wuffs is primarily a C library, not a C++ one. Furthermore, outside of
// the wuffs_base__etc types, the sizeof a file format specific type like
// GIF's wuffs_gif__decoder can vary between Wuffs versions. If p is of
// type wuffs_gif__decoder*, then the supported API treats p as a pointer
// to an opaque type: a private implementation detail. The API is always
// "set_foo(p, etc)" and not "p->foo = etc".
//
// See https://en.wikipedia.org/wiki/Opaque_pointer#C
//
// Thus, we don't use C++'s new operator (which requires knowing the sizeof
// the struct at compile time). Instead, we use sk_malloc_canfail, with
// sizeof__wuffs_gif__decoder returning the appropriate value for the
// (statically or dynamically) linked version of the Wuffs library.
//
// As a C (not C++) library, none of the Wuffs types have constructors or
// destructors.
//
// In RAII style, we can still use std::unique_ptr with these pointers, but
// we pair the pointer with sk_free instead of C++'s delete.
void* decoder_raw = sk_malloc_canfail(sizeof__wuffs_gif__decoder());
if (!decoder_raw) {
*result = SkCodec::kInternalError;
return nullptr;
}
std::unique_ptr<wuffs_gif__decoder, decltype(&sk_free)> decoder(
reinterpret_cast<wuffs_gif__decoder*>(decoder_raw), &sk_free);
SkCodec::Result reset_result =
reset_and_decode_image_config(decoder.get(), &imgcfg, &iobuf, stream.get());
if (reset_result != SkCodec::kSuccess) {
*result = reset_result;
return nullptr;
}
uint32_t width = imgcfg.pixcfg.width();
uint32_t height = imgcfg.pixcfg.height();
if ((width == 0) || (width > INT_MAX) || (height == 0) || (height > INT_MAX)) {
*result = SkCodec::kInvalidInput;
return nullptr;
}
uint64_t workbuf_len = imgcfg.workbuf_len().max_incl;
void* workbuf_ptr_raw = workbuf_len <= SIZE_MAX ? sk_malloc_canfail(workbuf_len) : nullptr;
if (!workbuf_ptr_raw) {
*result = SkCodec::kInternalError;
return nullptr;
}
std::unique_ptr<uint8_t, decltype(&sk_free)> workbuf_ptr(
reinterpret_cast<uint8_t*>(workbuf_ptr_raw), &sk_free);
uint64_t pixbuf_len = imgcfg.pixcfg.pixbuf_len();
void* pixbuf_ptr_raw = pixbuf_len <= SIZE_MAX ? sk_malloc_canfail(pixbuf_len) : nullptr;
if (!pixbuf_ptr_raw) {
*result = SkCodec::kInternalError;
return nullptr;
}
std::unique_ptr<uint8_t, decltype(&sk_free)> pixbuf_ptr(
reinterpret_cast<uint8_t*>(pixbuf_ptr_raw), &sk_free);
wuffs_base__pixel_buffer pixbuf = ((wuffs_base__pixel_buffer){});
const char* status = pixbuf.set_from_slice(&imgcfg.pixcfg, ((wuffs_base__slice_u8){
.ptr = pixbuf_ptr.get(),
.len = pixbuf_len,
}));
if (status != nullptr) {
SkCodecPrintf("set_from_slice: %s", status);
*result = SkCodec::kInternalError;
return nullptr;
}
// In Skia's API, the alpha we calculate here and return is only for the
// first frame.
SkEncodedInfo::Alpha alpha = imgcfg.first_frame_is_opaque() ? SkEncodedInfo::kOpaque_Alpha
: SkEncodedInfo::kBinary_Alpha;
SkEncodedInfo encodedInfo =
SkEncodedInfo::Make(width, height, SkEncodedInfo::kPalette_Color, alpha, 8);
*result = SkCodec::kSuccess;
return std::unique_ptr<SkCodec>(new SkWuffsCodec(
std::move(encodedInfo), std::move(stream), std::move(decoder), std::move(pixbuf_ptr),
std::move(workbuf_ptr), workbuf_len, imgcfg, pixbuf, iobuf));
}

17
src/codec/SkWuffsCodec.h Normal file
View File

@ -0,0 +1,17 @@
/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkWuffsCodec_DEFINED
#define SkWuffsCodec_DEFINED
#include "SkCodec.h"
// These functions' types match DecoderProc in SkCodec.cpp.
bool SkWuffsCodec_IsFormat(const void*, size_t);
std::unique_ptr<SkCodec> SkWuffsCodec_MakeFromStream(std::unique_ptr<SkStream>, SkCodec::Result*);
#endif // SkWuffsCodec_DEFINED

View File

@ -30,6 +30,9 @@ DEF_TEST(Codec_trunc, r) {
if (!data) {
return;
}
// See also Codec_GifTruncated2 in GifTest.cpp for this magic 23.
//
// TODO: just move this getFrameInfo call to Codec_GifTruncated2?
SkCodec::MakeFromData(SkData::MakeSubset(data.get(), 0, 23))->getFrameInfo();
}

View File

@ -380,13 +380,21 @@ DEF_TEST(Codec_GifPreMap, r) {
REPORTER_ASSERT(r, result == SkCodec::kSuccess);
// Truncate to 23 bytes, just before the color map. This should fail to decode.
//
// See also Codec_GifTruncated2 in GifTest.cpp for this magic 23.
codec = SkCodec::MakeFromData(SkData::MakeWithoutCopy(gNoGlobalColorMap, 23));
REPORTER_ASSERT(r, codec);
if (codec) {
SkBitmap bm;
bm.allocPixels(info);
result = codec->getPixels(info, bm.getPixels(), bm.rowBytes());
// See the comments in Codec_GifTruncated2.
#ifdef SK_HAS_WUFFS_LIBRARY
REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
#else
REPORTER_ASSERT(r, result == SkCodec::kInvalidInput);
#endif
}
// Again, truncate to 23 bytes, this time for an incremental decode. We
@ -399,11 +407,24 @@ DEF_TEST(Codec_GifPreMap, r) {
SkBitmap bm;
bm.allocPixels(info);
result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
// See the comments in Codec_GifTruncated2.
#ifdef SK_HAS_WUFFS_LIBRARY
REPORTER_ASSERT(r, result == SkCodec::kSuccess);
// Note that this is incrementalDecode, not startIncrementalDecode.
result = codec->incrementalDecode();
REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
stream->addNewData(data->size());
#else
REPORTER_ASSERT(r, result == SkCodec::kIncompleteInput);
// Note that this is startIncrementalDecode, not incrementalDecode.
stream->addNewData(data->size());
result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes());
REPORTER_ASSERT(r, result == SkCodec::kSuccess);
#endif
result = codec->incrementalDecode();
REPORTER_ASSERT(r, result == SkCodec::kSuccess);

View File

@ -1419,6 +1419,74 @@ DEF_TEST(Codec_InvalidHeader, r) {
test_invalid_header(r, "invalid_images/b34778578.bmp");
}
/*
For the Codec_InvalidAnimated test, immediately below,
resources/invalid_images/skbug6046.gif is:
00000000: 4749 4638 3961 2000 0000 0000 002c ff00 GIF89a ......,..
00000010: 7400 0600 0000 4001 0021 f904 0a00 0000 t.....@..!......
00000020: 002c ff00 0000 ff00 7400 0606 0606 0601 .,......t.......
00000030: 0021 f904 0000 0000 002c ff00 0000 ffcc .!.......,......
00000040: 1b36 5266 deba 543d .6Rf..T=
It nominally contains 3 frames, but all of them are invalid. It came from a
fuzzer doing random mutations and copies. The breakdown:
@000 6 bytes magic "GIF89a"
@006 7 bytes Logical Screen Descriptor: 0x20 0x00 ... 0x00
- width = 32
- height = 0
- flags = 0x00
- background color index, pixel aspect ratio bytes ignored
@00D 10 bytes Image Descriptor header: 0x2C 0xFF ... 0x40
- origin_x = 255
- origin_y = 116
- width = 6
- height = 0
- flags = 0x40, interlaced
@017 2 bytes Image Descriptor body (pixel data): 0x01 0x00
- lit_width = 1, INVALID, OUTSIDE THE RANGE [2, 8]
- 0x00 byte means "end of data" for this frame
@019 8 bytes Graphic Control Extension: 0x21 0xF9 ... 0x00
- valid, but irrelevant here.
@021 10 bytes Image Descriptor header: 0x2C 0xFF ... 0x06
- origin_x = 255
- origin_y = 0
- width = 255
- height = 116
- flags = 0x06, INVALID, 0x80 BIT ZERO IMPLIES 0x07 BITS SHOULD BE ZERO
@02B 14 bytes Image Descriptor body (pixel data): 0x06 0x06 ... 0x00
- lit_width = 6
- 0x06 precedes a 6 byte block of data
- 0x04 precedes a 4 byte block of data
- 0x00 byte means "end of data" for this frame
@039 10 bytes Image Descriptor header: 0x2C 0xFF ... 0x06
- origin_x = 255
- origin_y = 0
- width = 52479
- height = 13851
- flags = 0x52, INVALID, 0x80 BIT ZERO IMPLIES 0x07 BITS SHOULD BE ZERO
@043 5 bytes Image Descriptor body (pixel data): 0x66 0xDE ... unexpected-EOF
- lit_width = 102, INVALID, OUTSIDE THE RANGE [2, 8]
- 0xDE precedes a 222 byte block of data, INVALIDLY TRUNCATED
On Image Descriptor flags INVALIDITY,
https://www.w3.org/Graphics/GIF/spec-gif89a.txt section 20.c says that "Size of
Local Color Table [the low 3 bits]... should be 0 if there is no Local Color
Table specified [the high bit]."
On LZW literal width (also known as Minimum Code Size) INVALIDITY outside of
the range [2, 8], https://www.w3.org/Graphics/GIF/spec-gif89a.txt Appendix F
says that "Normally this will be the same as the number of [palette index]
bits. Because of some algorithmic constraints however, black & white images
which have one color bit must be indicated as having a code size of 2."
In practice, some GIF decoders, including the old third_party/gif code, don't
enforce this. It says: "currentFrame->setDataSize(this->getOneByte())" with the
only further check being against an upper bound of SK_MAX_DICTIONARY_ENTRY_BITS
(the constant 12).
*/
DEF_TEST(Codec_InvalidAnimated, r) {
// ASAN will complain if there is an issue.
auto path = "invalid_images/skbug6046.gif";
@ -1444,6 +1512,36 @@ DEF_TEST(Codec_InvalidAnimated, r) {
const auto reqFrame = frameInfos[i].fRequiredFrame;
opts.fPriorFrame = reqFrame == i - 1 ? reqFrame : SkCodec::kNoFrame;
auto result = codec->startIncrementalDecode(info, bm.getPixels(), bm.rowBytes(), &opts);
#ifdef SK_HAS_WUFFS_LIBRARY
// We are transitioning from an old GIF implementation to a new (Wuffs)
// GIF implementation.
//
// This test (without SK_HAS_WUFFS_LIBRARY) is overly specific to the
// old implementation. As a fuzzer-discovered test, it's likely that
// what's fundamentally being tested isn't that decoding an invalid GIF
// leads to kSuccess, but that decoding an invalid GIF doesn't lead to
// an ASAN violation.
//
// Each of the 3 frames of the source GIF image is fundamentally
// invalid, as per the "breakdown" comment above. The old
// implementation is happy to call startIncrementalDecode 3 times. The
// new implementation is happy for the first two times, but on the 3rd,
// SkCodec::startIncrementalDecode calls SkCodec::handleFrameIndex
// which calls SkCodec::getPixels on the requiredFrame (the 0'th
// frame), and the new implementation subsequently hits the
// invalid-ness and returns kErrorInInput instead of kSuccess.
//
// Once the transition is complete, we can remove the #ifdef and delete
// the rest of the test function.
if (i == 2) {
if (result != SkCodec::kErrorInInput) {
ERRORF(r, "Unexpected result for decoding frame %i (out of %i) with error %i\n", i,
frameInfos.size(), result);
}
return;
}
#endif
if (result != SkCodec::kSuccess) {
ERRORF(r, "Failed to start decoding frame %i (out of %i) with error %i\n", i,
frameInfos.size(), result);
@ -1535,6 +1633,30 @@ DEF_TEST(Codec_webp_rowsDecoded, r) {
test_info(r, codec.get(), codec->getInfo(), SkCodec::kInvalidInput, nullptr);
}
/*
For the Codec_ossfuzz6274 test, immediately below,
resources/invalid_images/ossfuzz6274.gif is:
00000000: 4749 4638 3961 2000 2000 f120 2020 2020 GIF89a . ..
00000010: 2020 2020 2020 2020 2021 f903 ff20 2020 !...
00000020: 002c 0000 0000 2000 2000 2000 00 .,.... . . ..
@000 6 bytes magic "GIF89a"
@006 7 bytes Logical Screen Descriptor: 0x20 0x00 ... 0x00
- width = 32
- height = 32
- flags = 0xF1, global color table, 4 RGB entries
- background color index, pixel aspect ratio bytes ignored
@00D 12 bytes Color Table: 0x20 0x20 ... 0x20
@019 20 bytes Graphic Control Extension: 0x21 0xF9 ... unexpected-EOF
- 0x03 precedes a 3 byte block of data, INVALID, MUST BE 4
- 0x20 precedes a 32 byte block of data, INVALIDly truncated
https://www.w3.org/Graphics/GIF/spec-gif89a.txt section 23.c says that the
block size (for an 0x21 0xF9 Graphic Control Extension) must be "the fixed
value 4".
*/
DEF_TEST(Codec_ossfuzz6274, r) {
if (GetResourcePath().isEmpty()) {
return;
@ -1542,6 +1664,31 @@ DEF_TEST(Codec_ossfuzz6274, r) {
const char* file = "invalid_images/ossfuzz6274.gif";
auto image = GetResourceAsImage(file);
#ifdef SK_HAS_WUFFS_LIBRARY
// We are transitioning from an old GIF implementation to a new (Wuffs) GIF
// implementation.
//
// This test (without SK_HAS_WUFFS_LIBRARY) is overly specific to the old
// implementation. In the new implementation, the MakeFromStream factory
// method returns a nullptr SkImage*, instead of returning a non-null but
// otherwise all-transparent SkImage*.
//
// Either way, the end-to-end result is the same - the source input is
// rejected as an invalid GIF image - but the two implementations differ in
// how that's represented.
//
// Once the transition is complete, we can remove the #ifdef and delete the
// rest of the test function.
//
// See Codec_GifTruncated3 for the equivalent of the rest of the test
// function, on different (but still truncated) source data.
if (image) {
ERRORF(r, "Invalid data gave non-nullptr image");
}
return;
#endif
if (!image) {
ERRORF(r, "Missing %s", file);
return;

View File

@ -9,6 +9,7 @@
#include "Resources.h"
#include "SkAndroidCodec.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkData.h"
#include "SkImage.h"
#include "SkStream.h"
@ -187,6 +188,20 @@ DEF_TEST(Gif, reporter) {
test_gif_data_no_colormap(reporter, static_cast<void *>(gGIFDataNoColormap),
sizeof(gGIFDataNoColormap));
#ifdef SK_HAS_WUFFS_LIBRARY
// We are transitioning from an old GIF implementation to a new (Wuffs) GIF
// implementation.
//
// This test (without SK_HAS_WUFFS_LIBRARY) is overly specific to the old
// implementation. It claims that, for invalid (truncated) input, we can
// still 'decode' all of the pixels because no matter what palette index
// each pixel is, they're all equivalently transparent. It's not obvious
// that this off-spec behavior is worth preserving. Are real world users
// decoding truncated all-transparent GIF images??
//
// Once the transition is complete, we can remove the #ifdef and delete the
// #else branch.
#else
// Since there is no color map, we do not even need to parse the image data
// to know that we should draw transparent. Truncate the file before the
// data. This should still succeed.
@ -212,6 +227,7 @@ DEF_TEST(Gif, reporter) {
}
}
}
#endif
// test short Gif. 80 is missing a few bytes.
test_gif_data_short(reporter, static_cast<void *>(gGIFData), 80);
@ -261,27 +277,151 @@ DEF_TEST(Codec_GifTruncated, r) {
REPORTER_ASSERT(r, !codec);
}
/*
For the Codec_GifTruncated2 test, immediately below,
resources/images/box.gif's first 23 bytes are:
00000000: 4749 4638 3961 c800 3700 203f 002c 0000 GIF89a..7. ?.,..
00000010: 0000 c800 3700 85 ....7..
The breakdown:
@000 6 bytes magic "GIF89a"
@006 7 bytes Logical Screen Descriptor: 0xC8 0x00 ... 0x00
- width = 200
- height = 55
- flags = 0x20
- background color index, pixel aspect ratio bytes ignored
@00D 10 bytes Image Descriptor header: 0x2C 0x00 ... 0x85
- origin_x = 0
- origin_y = 0
- width = 200
- height = 55
- flags = 0x85, local color table, 64 RGB entries
In particular, 23 bytes is after the header, but before the color table.
*/
DEF_TEST(Codec_GifTruncated2, r) {
// Truncate box.gif at 21, 22 and 23 bytes.
//
// See also Codec_GifTruncated3 in this file, below.
//
// See also Codec_trunc in CodecAnimTest.cpp for this magic 23.
//
// See also Codec_GifPreMap in CodecPartialTest.cpp for this magic 23.
for (int i = 21; i < 24; i++) {
sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
if (!data) {
return;
}
// This is after the header, but before the color table.
data = SkData::MakeSubset(data.get(), 0, 23);
data = SkData::MakeSubset(data.get(), 0, i);
std::unique_ptr<SkCodec> codec(SkCodec::MakeFromData(data));
if (!codec) {
ERRORF(r, "Failed to create codec with partial data");
if (i <= 21) {
if (codec) {
ERRORF(r, "Invalid data gave non-nullptr codec");
}
return;
}
if (!codec) {
ERRORF(r, "Failed to create codec with partial data (truncated at %d)", i);
return;
}
#ifdef SK_HAS_WUFFS_LIBRARY
// We are transitioning from an old GIF implementation to a new (Wuffs)
// GIF implementation.
//
// The input is truncated in the Image Descriptor, before the local
// color table, and before (21) or after (22, 23) the first frame's
// XYWH (left / top / width / height) can be decoded. A detailed
// breakdown of those 23 bytes is in a comment above this function.
//
// With the old implementation, this test claimed that "no frame is
// complete enough that it has its metadata". In terms of the
// underlying file format, this claim is true for truncating at 21
// bytes, but not true for 22 or 23.
//
// At 21 bytes, both the old and new implementation's MakeFromStream
// factory method returns a nullptr SkCodec*, because creating a
// SkCodec requires knowing the image width and height (as its
// constructor takes an SkEncodedInfo argument), and specifically for
// GIF, decoding the image width and height requires decoding the first
// frame's XYWH, as per
// https://raw.githubusercontent.com/google/wuffs/master/test/data/artificial/gif-frame-out-of-bounds.gif.make-artificial.txt
//
// At 22 or 23 bytes, the first frame is complete enough that we can
// fill in all of a SkCodec::FrameInfo's fields (other than
// fFullyReceived). Specifically, we can fill in fRequiredFrame and
// fAlphaType, even though we haven't yet decoded the frame's RGB
// palette entries, as we do know the frame rectangle and that every
// palette entry is fully opaque, due to the lack of a Graphic Control
// Extension before the Image Descriptor.
//
// The new implementation correctly reports that the first frame's
// metadata is complete enough. The old implementation does not.
//
// Once the transition is complete, we can remove the #ifdef and delete
// the #else code.
REPORTER_ASSERT(r, codec->getFrameCount() == 1);
#else
// The old implementation claimed:
//
// Although we correctly created a codec, no frame is
// complete enough that it has its metadata. Returning 0
// ensures that Chromium will not try to create a frame
// too early.
REPORTER_ASSERT(r, codec->getFrameCount() == 0);
#endif
}
}
#ifdef SK_HAS_WUFFS_LIBRARY
// This tests that, after truncating the input, the pixels are still
// zero-initialized. If you comment out the SkSampler::Fill call in
// SkWuffsCodec::onStartIncrementalDecode, the test could still pass (in a
// standard configuration) but should fail with the MSAN memory sanitizer.
DEF_TEST(Codec_GifTruncated3, r) {
sk_sp<SkData> data(GetResourceAsData("images/box.gif"));
if (!data) {
return;
}
data = SkData::MakeSubset(data.get(), 0, 23);
sk_sp<SkImage> image(SkImage::MakeFromEncoded(data));
if (!image) {
ERRORF(r, "Missing image");
return;
}
REPORTER_ASSERT(r, image->width() == 200);
REPORTER_ASSERT(r, image->height() == 55);
SkBitmap bm;
if (!bm.tryAllocPixels(SkImageInfo::MakeN32Premul(200, 55))) {
ERRORF(r, "Failed to allocate pixels");
return;
}
bm.eraseColor(SK_ColorTRANSPARENT);
SkCanvas canvas(bm);
canvas.drawImage(image, 0, 0, nullptr);
for (int i = 0; i < image->width(); ++i)
for (int j = 0; j < image->height(); ++j) {
SkColor actual = SkUnPreMultiply::PMColorToColor(*bm.getAddr32(i, j));
if (actual != SK_ColorTRANSPARENT) {
ERRORF(r, "did not initialize pixels! %i, %i is %x", i, j, actual);
}
}
}
#endif
DEF_TEST(Codec_gif_out_of_palette, r) {
if (GetResourcePath().isEmpty()) {
return;

13
third_party/wuffs/BUILD.gn vendored Normal file
View File

@ -0,0 +1,13 @@
# Copyright 2018 Google Inc.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("../third_party.gni")
third_party("wuffs") {
public_include_dirs = [ "../externals/wuffs/release/c" ]
sources = [
"wuffs.c",
]
}

42
third_party/wuffs/wuffs.c vendored Normal file
View File

@ -0,0 +1,42 @@
/*
* Copyright 2018 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
// The Wuffs library ships as a single file - a .h file - which GN does not
// recognize as something to be compiled, because it ends in .h and not .c or
// .cpp. Instead, this trivial file is a placeholder .c file that is a BUILD.gn
// target for the third party Wuffs library.
//
// Copy/pasting from the Wuffs .h file's comments:
//
// ----
//
// Wuffs ships as a "single file C library" or "header file library" as per
// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
//
// To use that single file as a "foo.c"-like implementation, instead of a
// "foo.h"-like header, #define WUFFS_IMPLEMENTATION before #include'ing or
// compiling it.
//
// ----
#define WUFFS_IMPLEMENTATION
// Defining the WUFFS_CONFIG__MODULE* macros are optional, but it lets users of
// Wuffs' .h file whitelist which parts of Wuffs to build. That file contains
// the entire Wuffs standard library, implementing a variety of codecs and file
// formats. Without this macro definition, an optimizing compiler or linker may
// very well discard Wuffs code for unused codecs, but listing the Wuffs
// modules we use makes that process explicit. Preprocessing means that such
// code simply isn't compiled.
//
// For Skia, we're only interested in particular image codes (e.g. GIF) and
// their dependencies (e.g. BASE, LZW).
#define WUFFS_CONFIG__MODULES
#define WUFFS_CONFIG__MODULE__BASE
#define WUFFS_CONFIG__MODULE__GIF
#define WUFFS_CONFIG__MODULE__LZW
#include "wuffs-v0.2.h"