Budget fStorage in SkPngCodecBase::allocateStorage Replace `skia_private::AutoTMalloc<uint8_t> fStorage` in `SkPngCodecBase` with `SkCodecPriv::BudgetedBuffer fStorage` so that the color-transform scratch buffer allocated in `SkPngCodecBase::allocateStorage()` is charged against `fMaxDecodeMemory` via `allocateFromBudget()` and uses non-throwing `sk_malloc_canfail`. Bug: b/565484678 Change-Id: I1c0e83bcb84f69cae344b25a4b8e40d655fd321c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1375556 Commit-Queue: Elliot Sisteron <elliotsisteron@google.com> Reviewed-by: Florin Malita <fmalita@google.com>
diff --git a/src/codec/SkPngCodecBase.cpp b/src/codec/SkPngCodecBase.cpp index d6fb715..a93c467 100644 --- a/src/codec/SkPngCodecBase.cpp +++ b/src/codec/SkPngCodecBase.cpp
@@ -160,7 +160,9 @@ } } - this->allocateStorage(dstInfo); + if (Result result = this->allocateStorage(dstInfo); result != kSuccess) { + return result; + } // We can't call `initializeXformParams` here, because `swizzleWidth` may // change *after* `onStartIncrementalDecode` @@ -192,7 +194,8 @@ } } -void SkPngCodecBase::allocateStorage(const SkImageInfo& dstInfo) { +SkCodec::Result SkPngCodecBase::allocateStorage(const SkImageInfo& dstInfo) { + fStorage.reset(); switch (fXformMode) { case kSwizzleOnly_XformMode: break; @@ -207,10 +210,10 @@ // extra precision. Otherwise, we will swizzle to RGBA_8888 before transforming. const size_t bytesPerPixel = (bitsPerPixel > 32) ? bitsPerPixel / 8 : 4; const size_t colorXformBytes = dstInfo.width() * bytesPerPixel; - fStorage.reset(colorXformBytes); - break; + return fStorage.allocateFromBudget(this, colorXformBytes, /*zeroInit=*/false); } } + return kSuccess; } SkCodec::Result SkPngCodecBase::initializeSwizzler(const SkImageInfo& dstInfo, @@ -305,8 +308,8 @@ break; case kSwizzleColor_XformMode: SkASSERT_RELEASE(fSwizzler->swizzleWidth() == fXformWidth); - fSwizzler->swizzle(fStorage.get(), srcRow); - this->applyColorXform(dstRow, fStorage.get(), fXformWidth); + fSwizzler->swizzle(fStorage.data(), srcRow); + this->applyColorXform(dstRow, fStorage.data(), fXformWidth); break; } }
diff --git a/src/codec/SkPngCodecBase.h b/src/codec/SkPngCodecBase.h index dd2c75c..728b3ee 100644 --- a/src/codec/SkPngCodecBase.h +++ b/src/codec/SkPngCodecBase.h
@@ -20,7 +20,7 @@ #include "include/private/SkDebug.h" #include "include/private/SkEncodedInfo.h" #include "include/private/SkGainmapInfo.h" -#include "include/private/SkTemplates.h" +#include "src/codec/SkCodecPriv.h" #include "src/codec/SkPngCompositeChunkReader.h" class SkColorPalette; @@ -99,7 +99,7 @@ // SkCodec overrides: SkEncodedImageFormat onGetEncodedFormat() const final; - void allocateStorage(const SkImageInfo& dstInfo); + Result allocateStorage(const SkImageInfo& dstInfo); Result initializeSwizzler(const SkImageInfo& dstInfo, const Options& options, bool skipFormatConversion, @@ -119,7 +119,7 @@ XformMode fXformMode; std::unique_ptr<SkSwizzler> fSwizzler; - skia_private::AutoTMalloc<uint8_t> fStorage; + SkCodecPriv::BudgetedBuffer fStorage; int fXformWidth = -1; sk_sp<SkColorPalette> fColorTable;
diff --git a/tests/SkPngRustDecoderTest.cpp b/tests/SkPngRustDecoderTest.cpp index de265bb..ff389d9 100644 --- a/tests/SkPngRustDecoderTest.cpp +++ b/tests/SkPngRustDecoderTest.cpp
@@ -2150,9 +2150,9 @@ } // Next tests verify that the single-row interlaced scratch buffers -// (`fDecodedInterlacedFullWidthRow` and `fXformedInterlacedRow`) are charged against -// `fMaxDecodeMemory` even when `fPreblendBuffer` is empty (full-image decode of a non-blended -// interlaced PNG). +// (`fDecodedInterlacedFullWidthRow`, `fXformedInterlacedRow`, and `SkPngCodecBase::fStorage`) are +// charged against `fMaxDecodeMemory` even when `fPreblendBuffer` is empty (full-image decode of a +// non-blended interlaced PNG). // // Note: `plane_interlaced.png` has no embedded ICC profile (`getEncodedInfo().profile() == // nullptr`), so `makeColorSpace(nullptr)` is required for `canReadRow()` to match when @@ -2183,11 +2183,9 @@ } DEF_TEST(RustPngCodec_interlacedRowBufferBudget_xform, r) { - // `kPremul_SkAlphaType` takes the `!canReadRow()` (`incrementalDecodeXForm`) path, which - // allocates both `fDecodedInterlacedFullWidthRow` (1000 bytes) and `fXformedInterlacedRow` - // (1000 bytes). - // Note: `SkPngCodecBase::allocateStorage` is not yet budgeted, so the budget charged here is - // 2 * 1000 = 2000 bytes. + // `kPremul_SkAlphaType` + `nullptr` colorSpace takes the `!canReadRow()` + // (`incrementalDecodeXForm`) path with `kSwizzleOnly_XformMode`, which allocates both + // `fDecodedInterlacedFullWidthRow` (1000 bytes) and `fXformedInterlacedRow` (1000 bytes). constexpr size_t kEncodedRowBytes = 250 * 4; constexpr size_t kDstRowBytes = 250 * 4; @@ -2236,6 +2234,33 @@ } } +DEF_TEST(RustPngCodec_interlacedRowBufferBudget_colorXform, r) { + // Decoding into a non-sRGB color space (`SkColorSpace::MakeSRGBLinear()`) activates + // `SkPngCodecBase::allocateStorage` (`fStorage`, 1000 bytes) in addition to + // `fDecodedInterlacedFullWidthRow` (1000 bytes) and `fXformedInterlacedRow` (1000 bytes). + constexpr size_t kEncodedRowBytes = 250 * 4; + constexpr size_t kDstRowBytes = 250 * 4; + constexpr size_t kColorXformBytes = 250 * 4; + + AssertDecodeBudget(r, + "images/plane_interlaced.png", + kEncodedRowBytes + kDstRowBytes + kColorXformBytes, + [&](std::unique_ptr<SkCodec> codec, + size_t maxDecodeMemory, + SkCodec::Result expectedResult) { + return DecodePixels( + r, + std::move(codec), + [](const SkImageInfo& info) { + return info.makeColorType(kRGBA_8888_SkColorType) + .makeAlphaType(kPremul_SkAlphaType) + .makeColorSpace(SkColorSpace::MakeSRGBLinear()); + }, + MakeBudgetOptions(maxDecodeMemory), + expectedResult); + }); +} + // Regression test helper for b/562804783: like `SkPngCodec`, `SkPngRustCodec` must report // `rowsDecoded` for `kErrorInInput` (not just `kIncompleteInput`), so that callers such as // `SkCodec::getPixels` keep the rows decoded before the error instead of filling the whole image.