diff options
author | msarett <msarett@google.com> | 2016-02-22 12:27:46 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-22 12:27:46 -0800 |
commit | 91c22b2ea6bd13a31321ead01645467f21858cd0 (patch) | |
tree | 99f66c26c87bbcade9c7eac84099e2fae7c01cd5 /src | |
parent | 7499e69d9991a113c452a2a44a10078df41f3758 (diff) |
Use new jpeg_crop_scanlines() API to optimize jpeg subset decodes
This was adapted from:
https://codereview.chromium.org/1530933003
Subset Decode Runtime (Original / Optimized) on Nexus 6P
TopLeft 0.51x
TopRight 0.56x
Middle 0.71x
BottomLeft 0.79x
BottomRight 0.79x
BUG=skia:4256
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1719073002
Review URL: https://codereview.chromium.org/1719073002
Diffstat (limited to 'src')
-rw-r--r-- | src/codec/SkCodec.cpp | 11 | ||||
-rw-r--r-- | src/codec/SkJpegCodec.cpp | 60 | ||||
-rw-r--r-- | src/codec/SkJpegCodec.h | 4 |
3 files changed, 71 insertions, 4 deletions
diff --git a/src/codec/SkCodec.cpp b/src/codec/SkCodec.cpp index c4f7498f79..d7cfb37497 100644 --- a/src/codec/SkCodec.cpp +++ b/src/codec/SkCodec.cpp @@ -354,23 +354,28 @@ void SkCodec::fillIncompleteImage(const SkImageInfo& info, void* dst, size_t row const int linesRemaining = linesRequested - linesDecoded; SkSampler* sampler = this->getSampler(false); + int fillWidth = info.width(); + if (fOptions.fSubset) { + fillWidth = fOptions.fSubset->width(); + } + switch (this->getScanlineOrder()) { case kTopDown_SkScanlineOrder: case kNone_SkScanlineOrder: { - const SkImageInfo fillInfo = info.makeWH(info.width(), linesRemaining); + const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); fillDst = SkTAddOffset<void>(dst, linesDecoded * rowBytes); fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); break; } case kBottomUp_SkScanlineOrder: { fillDst = dst; - const SkImageInfo fillInfo = info.makeWH(info.width(), linesRemaining); + const SkImageInfo fillInfo = info.makeWH(fillWidth, linesRemaining); fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); break; } case kOutOfOrder_SkScanlineOrder: { SkASSERT(1 == linesRequested || this->getInfo().height() == linesRequested); - const SkImageInfo fillInfo = info.makeWH(info.width(), 1); + const SkImageInfo fillInfo = info.makeWH(fillWidth, 1); for (int srcY = linesDecoded; srcY < linesRequested; srcY++) { fillDst = SkTAddOffset<void>(dst, this->outputScanline(srcY) * rowBytes); fill_proc(fillInfo, fillDst, rowBytes, fillValue, zeroInit, sampler); diff --git a/src/codec/SkJpegCodec.cpp b/src/codec/SkJpegCodec.cpp index e1440af5c3..d36051511c 100644 --- a/src/codec/SkJpegCodec.cpp +++ b/src/codec/SkJpegCodec.cpp @@ -80,6 +80,7 @@ SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream, : INHERITED(srcInfo, stream) , fDecoderMgr(decoderMgr) , fReadyState(decoderMgr->dinfo()->global_state) + , fSwizzlerSubset(SkIRect::MakeEmpty()) {} /* @@ -371,7 +372,16 @@ void SkJpegCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& srcConfig = SkSwizzler::kRGB; } - fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, options)); + Options swizzlerOptions = options; + if (options.fSubset) { + // Use fSwizzlerSubset if this is a subset decode. This is necessary in the case + // where libjpeg-turbo provides a subset and then we need to subset it further. + // Also, verify that fSwizzlerSubset is initialized and valid. + SkASSERT(!fSwizzlerSubset.isEmpty() && fSwizzlerSubset.x() <= options.fSubset->x() && + fSwizzlerSubset.width() == options.fSubset->width()); + swizzlerOptions.fSubset = &fSwizzlerSubset; + } + fSwizzler.reset(SkSwizzler::CreateSwizzler(srcConfig, nullptr, dstInfo, swizzlerOptions)); SkASSERT(fSwizzler); fStorage.reset(get_row_bytes(fDecoderMgr->dinfo())); fSrcRow = fStorage.get(); @@ -411,12 +421,60 @@ SkCodec::Result SkJpegCodec::onStartScanlineDecode(const SkImageInfo& dstInfo, return kInvalidInput; } + if (options.fSubset) { + fSwizzlerSubset = *options.fSubset; + } + +#ifdef TURBO_HAS_CROP + if (options.fSubset) { + uint32_t startX = options.fSubset->x(); + uint32_t width = options.fSubset->width(); + + // libjpeg-turbo may need to align startX to a multiple of the IDCT + // block size. If this is the case, it will decrease the value of + // startX to the appropriate alignment and also increase the value + // of width so that the right edge of the requested subset remains + // the same. + jpeg_crop_scanline(fDecoderMgr->dinfo(), &startX, &width); + + SkASSERT(startX <= (uint32_t) options.fSubset->x()); + SkASSERT(width >= (uint32_t) options.fSubset->width()); + SkASSERT(startX + width >= (uint32_t) options.fSubset->right()); + + // Instruct the swizzler (if it is necessary) to further subset the + // output provided by libjpeg-turbo. + // + // We set this here (rather than in the if statement below), so that + // if (1) we don't need a swizzler for the subset, and (2) we need a + // swizzler for CMYK, the swizzler will still use the proper subset + // dimensions. + // + // Note that the swizzler will ignore the y and height parameters of + // the subset. Since the scanline decoder (and the swizzler) handle + // one row at a time, only the subsetting in the x-dimension matters. + fSwizzlerSubset.setXYWH(options.fSubset->x() - startX, 0, + options.fSubset->width(), options.fSubset->height()); + + // We will need a swizzler if libjpeg-turbo cannot provide the exact + // subset that we request. + if (startX != (uint32_t) options.fSubset->x() || + width != (uint32_t) options.fSubset->width()) { + this->initializeSwizzler(dstInfo, options); + } + } + + // Make sure we have a swizzler if we are converting from CMYK. + if (!fSwizzler && JCS_CMYK == fDecoderMgr->dinfo()->out_color_space) { + this->initializeSwizzler(dstInfo, options); + } +#else // We will need a swizzler if we are performing a subset decode or // converting from CMYK. J_COLOR_SPACE colorSpace = fDecoderMgr->dinfo()->out_color_space; if (options.fSubset || JCS_CMYK == colorSpace || JCS_RGB == colorSpace) { this->initializeSwizzler(dstInfo, options); } +#endif return kSuccess; } diff --git a/src/codec/SkJpegCodec.h b/src/codec/SkJpegCodec.h index 06685cfd4c..bb5ce75375 100644 --- a/src/codec/SkJpegCodec.h +++ b/src/codec/SkJpegCodec.h @@ -115,6 +115,10 @@ private: // scanline decoding SkAutoTMalloc<uint8_t> fStorage; // Only used if sampling is needed uint8_t* fSrcRow; // Only used if sampling is needed + // libjpeg-turbo provides some subsetting. In the case that libjpeg-turbo + // cannot take the exact the subset that we need, we will use the swizzler + // to further subset the output from libjpeg-turbo. + SkIRect fSwizzlerSubset; SkAutoTDelete<SkSwizzler> fSwizzler; typedef SkCodec INHERITED; |