Skip to content

feat(skia): decode JPEG images with SkiaSharp's SKCodec, scaled and bit-identical (#374) - #485

Merged
michaelstonis merged 12 commits into
spec/33-broadside-v1from
374-skia-jpeg-decoder
Nov 10, 2026
Merged

michaelstonis merged 12 commits into
spec/33-broadside-v1from
374-skia-jpeg-decoder

Conversation

@michaelstonis

Copy link
Copy Markdown
Owner

Closes #374.

What

  • SkiaImageDecoder (Broadside.Rendering.Skia) implements IPlatformImageDecoder with SkiaSharp's SKCodec, which wraps libjpeg-turbo:
    • GetScaledDimensions(1/2^r) picks the DCT scaling, and the decoder reports the reduction it delivers.
    • Rows are read in scanline bands of 16 into a pooled buffer, as 8-bit gray or RGBA with a null color space, then packed into the core's DecodedImageBuilder as 1 or 3 components.
    • UseSkiaImageDecoder() registers it.
  • MAUI: PdfView registers the decoder in the engine it builds; the logic moved from PdfView.WithSkia to SkiaViewerOptions.For. An engine the app gives is used as it is. DocumentOptions.WithPlatformImageDecoder("skia", false) turns the decoder off.
  • Core: PlatformImageRequest.ChromaHorizontalSubsampling and ChromaVerticalSubsampling give Hmax / Hc and Vmax / Vc, 0 for patterns that have no single ratio. They are byte-backed, so the struct does not grow.

Deviations from the issue, with reasons

  • Fidelity is Exact, not Tolerance, and the decoder is on by default in the MAUI viewer.
    • The issue's 4:2:0 numbers did not hold. libjpeg's interpolated ("fancy") chroma upsampling missed the tolerance bar: max 34 and 39.9 dB on testorig, 40 to 46 dB on the 4:2:2 and 4:4:0 vectors, and 51.9 dB with 1.075% of pixels over 2 levels on a 4:2:0 photograph.
    • Every other case is bit-identical. libjpeg-turbo's islow IDCT, its reduced IDCTs and its YCbCr transform are the ones the managed decoder ports.
    • CanDecode therefore uses libjpeg-turbo's own rule (jdmaster.c chroma IDCT enlargement, jdsample.c fancy upsampling) to decline the interpolated cases. The rule needs the new chroma ratios.
    • What remains is exact: 4:4:4 and gray at every scale, 4:2:0 from 1/2 down, and 4:2:2 or 4:4:0 at 1/8. That holds on all 27 eligible DCT vectors × 4 scales, on generated photographs, and on 1601 decodes over the 338 DCT files of the fetched corpora.
    • The exact paths are on by default (owner decision 3), so the MAUI viewer turns the decoder on.
  • RGBA, not Rgb888x. Skia's JPEG codec returns InvalidConversion for Rgb888x. The decoder reads RGBA and drops the alpha byte.
  • Scanline bands, not one GetPixels buffer. The only scratch is 16 rows; an image-sized buffer would be 32 MB for 8 MP. The output is identical either way.
  • The decode runs through Task.Run. A synchronous DecodeAsync is never in flight in PlatformImageDecoding.Join, so concurrent tiles each decoded the image (a test measured 8 decodes for 8 tiles). This made the half-width iOS view slower than managed. Filed Platform image decoding: a decoder that completes synchronously is not shared by concurrent prefetches #484 for the core fix, since the ImageIO and Android decoders are exposed the same way.
  • No YUV planes path. SkiaSharp 4.153.1 binds no YUV API: there is no sk_codec_*yuva* export and no managed member. A full-size 4:2:0 image decodes managed.
  • CMYK, YCCK and 12-bit fall back without ImageDecodedManaged. The core's shared rule never offers them, so no diagnostic is recorded. The test asserts that they decode managed and that there are no platform diagnostics. Data Skia rejects (a truncated scan) records ImageDecodedManaged.

Tests

  • Broadside.Rendering.Skia.Tests.SkiaImageDecoderTests, under VerifyPlatformImageDecoder:
    • every vector at every scale is exact, or is declined exactly where libjpeg interpolates;
    • the same holds for the photograph at 4:4:4, 4:2:0 and 4:2:2;
    • CMYK, YCCK and 12-bit decode managed, and a truncated file falls back with ImageDecodedManaged;
    • an Exif-6 image keeps its own orientation, and an image with an embedded gamma 1.8 ICC profile is not converted;
    • registration and turning the decoder off;
    • Skia renders of dct-*.pdf and the covers are byte-identical with the decoder on and off;
    • concurrent tiles share one decode;
    • allocation is per image, not per row;
    • the RGBA packer.
  • Broadside.Viewer.Maui.Tests.SkiaImageDecoderRegistrationTests and PlatformImageEligibilityTests (chroma ratios).

Measurements

The full figures are in docs/benchmarks/skia-image-decoder.md.

  • Decode on macOS (M4 Max), 2550 x 3150 photograph: 4:4:4 64.6 → 22.1 ms at 1/1 and 42.1 → 17.9 ms at 1/2; 4:2:0 37.8 → 15.4 ms at 1/2.
  • First tile on macOS: 4:4:4 at 1206 px 249 → 206 ms; at 390 px 85 → 58 ms.
  • iOS simulator PhotoBench:
    • idle 474–491 → 401–435 ms at full width (4:4:4);
    • idle 187 → 165–174 ms at half width (4:4:4), and 197 → 153–174 ms (4:2:0);
    • tile work falls 25–45%, and RSS is unchanged.
  • issue12841_reduced.pdf (the issue's gate file): unchanged, 175 vs 179 ms. The page draws its 4:2:0 image enlarged, so no reduction applies and the image stays managed.

Checklist

  • Benchmarks: SkiaImageDecodeBenchmarks and SkiaFirstTileBenchmarks. Their gate baseline entries come from CI's artifact.
  • PublicAPI.Unshipped.txt updated for core and Skia.
  • The 7.4.8 conformance row is updated; ConformanceCheck OK.
  • Docs: getting started (MAUI) and the benchmarks page.
  • No new fuzz target. The new parse (chroma ratios) is in PlatformImageEligibility, which the platform-image-eligibility target already covers.

SkiaImageDecodeBenchmarks and SkiaFirstTileBenchmarks are new. The values
are the CI gate's measurements. The 4:2:0 first tile at 1206 px decodes
managed, because the Skia decoder declines full-size 4:2:0.
…e change (#484)

The Skia first-tile cases allocate 128 B less per call once #484's
PlatformImageDecoding change is in; the CI gate measured the new values.
…kia-jpeg-decoder

# Conflicts:
#	docs/conformance/iso-32000-2.md
The Skia first-tile cases include the managed decode and conversion, so
#471's JpegColor kernel moved them by -96/+8 B.
@michaelstonis
michaelstonis merged commit 959f219 into spec/33-broadside-v1 Nov 10, 2026
50 of 51 checks passed
@michaelstonis
michaelstonis deleted the 374-skia-jpeg-decoder branch November 10, 2026 23:04
michaelstonis added a commit that referenced this pull request Nov 11, 2026
The eight SkiaFirstTileBenchmarks cases (added by #485) each allocate
96 B more per call with the #472 changes. This is a one-time cost per
document in the cold path, where every call opens a new document and
converts its image; it is not paid per row or pixel. The packing and
gray-expansion kernels allocate nothing (PixelPackingBenchmarks reads 0).
The baseline takes the gate's measured values.

Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
michaelstonis added a commit that referenced this pull request Nov 11, 2026
#475)

The gate reported only the eight SkiaFirstTileBenchmarks.FirstTile cases, each 224 B per call above the baseline from #485 (these cases include image conversion); every other case is unchanged. Updated to the values the gate measured.
michaelstonis added a commit that referenced this pull request Nov 11, 2026
Reverts the previous baseline update. The next gate run measured the
original values for the eight SkiaFirstTileBenchmarks cases, so their
96 B difference flips from run to run on the same code and #472 does
not cause it. The baseline keeps the values #485 recorded.

Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
michaelstonis added a commit that referenced this pull request Nov 11, 2026
… (#493)

* perf(rendering): vectorize the box filter's channel accumulation (#475)

* docs(benchmarks): record the box filter kernel and its whole-page effect (#475)

* docs(benchmarks): add the browser CI kernel lines for the box filter (#475)

* docs(rendering): say the box filter's vector path is slower in both browser builds (#475)

* test(perf): record the SkiaFirstTileBenchmarks allocations as measured (#475)

The gate reported only the eight SkiaFirstTileBenchmarks.FirstTile cases, each 224 B per call above the baseline from #485 (these cases include image conversion); every other case is unchanged. Updated to the values the gate measured.

* docs(benchmarks): add the x64 bench gate replays for the box filter (#475)
michaelstonis added a commit that referenced this pull request Nov 11, 2026
…xpansion (#472) (#491)

* perf(rendering): vectorize BGRA packing, premultiplication and gray expansion (#472)

- PixelPacking packs a converted RGB row and its folded opacity into
  premultiplied BGRA (or RGBA for WebGL) with Vector128: four overlapping
  loads and one constant shuffle per four pixels, the multiply and the
  rounding division by 255 in 16-bit lanes. Bit-exact with the scalar loop
  it replaces (exhaustive over every color and opacity, and every opaque
  RGB triple).
- ImageRows.PackBgra and PackRgba expose it to the software rasterizer's
  ImageSource and to the Skia, CoreGraphics, Direct2D, Android and WebGL
  image converters, which all carried the same scalar loop.
- GrayExpansion replaces the per-sample three-byte table copy of a
  one-component space whose byte table holds the grays (DeviceGray on an
  RGB device) with three constant shuffles per 16 samples; other tables
  are looked up without the per-sample Slice and CopyTo.
- PixelPackingBenchmarks, KernelBenchmarks cases for the browser builds,
  and the allocation baseline entries at 0.

Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>

* docs(benchmarks): record the #472 pixel packing and gray expansion kernels

Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>

* test(perf): record the SkiaFirstTileBenchmarks allocations after #472

The eight SkiaFirstTileBenchmarks cases (added by #485) each allocate
96 B more per call with the #472 changes. This is a one-time cost per
document in the cold path, where every call opens a new document and
converts its image; it is not paid per row or pixel. The packing and
gray-expansion kernels allocate nothing (PixelPackingBenchmarks reads 0).
The baseline takes the gate's measured values.

Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>

* test(perf): restore the SkiaFirstTileBenchmarks allocation baseline

Reverts the previous baseline update. The next gate run measured the
original values for the eight SkiaFirstTileBenchmarks cases, so their
96 B difference flips from run to run on the same code and #472 does
not cause it. The baseline keeps the values #485 recorded.

Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>

---------

Signed-off-by: Michael Stonis <120685+michaelstonis@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant