Skip to content

feat(android): decode images at the resolution they are drawn (#428 part 4) - #440

Merged
michaelstonis merged 3 commits into
spec/33-broadside-v1from
428-android-scaled-decode
Oct 27, 2026
Merged

michaelstonis merged 3 commits into
spec/33-broadside-v1from
428-android-scaled-decode

Conversation

@michaelstonis

Copy link
Copy Markdown
Owner

Part 4 of #428: the Android Canvas backend decodes images at the resolution they are drawn, as Skia (#429) and WebGL (#435) do.

What changed

  • API. AndroidCanvasBackend.ScaledImageDecode (off by default) and ImageSamplesPerPixel (2; 1 is faster and coarser; values below 1 throw) for list renders. Page renders (Render(PdfPage, …, RenderOptions)) follow RenderOptions.ScaledImageDecode / ImageSamplesPerPixel, off by default and forced off under overprint preview. This is viewer-only: nothing turns it on for print or export. The Android backend has no tile backend, so there is no PrefetchImagesAsync to implement.
  • Seam (public API only): r = image.GetReduction(userToPixels, k, converter to RGB), then GetSamplesAt(r), and those samples are converted with DecodeMapFor(samples). The geometry comes from the source size and the delivered reduction (SampleGrid). The overhang past the unit square is clipped in both the resampling path and the canvas's own sampling.
  • Masks. A soft mask of the image's size folds in only when its samples have the same delivered reduction. Explicit masks fold only at reduction 0. Any other mask is reduced by its own rule (DisplayImage.GetReduction(…, null) for soft masks, DisplayImageMask.GetReduction for stencil and explicit masks, as 8-bit coverage).
  • Cache. The converted-image cache is keyed by the delivered reduction. A finer conversion serves a coarser request, and pixels already cached at a finer reduction never call GetSamplesAt again. A new reduction clears the entry's resampling slots.
  • Fallback. The software rasterizer that groups and soft masks fall back to gets the same setting.
  • Banded resampling. ImageReducer now resamples in bands of at most 64 sample rows, ported from Skia (MAUI/Skia tiles: every pooled tile backend converts the same full-size image (3.7 GB peak on a catalogue page) #432). The pixels are the same. For the full-resolution cover at 1080 px, peak RSS drops from 477 to 350 MiB and the managed heap from 436 to 184 MiB.
  • Off. With scaled decode off the output is the same pixels as before. All golden, uncached and zero-allocation tests pass unchanged on the emulator.

Tests

AndroidScaledImageDecodeTests (21 tests). The device app (ScaledImageDecodeRun, run with -e scaled 1 as an extra phase of the shared instrumentation run) renders each case reduced first, then in full, and reports only the figures, so no large pixel files are pulled back. The cases:

  • the catalogue cover at 390, 300 and 150 px (reductions 1/2, 1/4, 1/8)
  • the corpus DCT pages at 1/4
  • every ScaledImagePages page (depths, stencil, soft masks of the image's size and of another size, an explicit mask of another size, a colour key that is never reduced)
  • a curved conversion capped at 1/2
  • 1:1 renders give identical pixels
  • page renders are off by default
  • k = 1 reduces one step further
  • a zoom in and back out converts two images in total
  • the kept converted pixels are the reduced size
  • a reduced replay allocates 0 B

On the scratch Android 15 arm64 emulator, all 365 tests of Broadside.Rendering.Android.Tests pass (7 skipped, as before). At k = 2 the lowest PSNR is 42.1 dB (explicit mask of another size, 1.5 % of pixels more than 8 levels off); every cover case is at least 46.7 dB.

Measurements

On the emulator, with the catalogue cover drawn as 256-px tiles (docs/benchmarks/native-scaled-image-decode.md):

  • When a reduction applies, the first pass is 2.3x (1/2) to 5x (1/4) faster.
  • Peak RSS falls by about 100 MiB.
  • At 1080 px with k = 2 there is no reduction, so nothing changes.

Local gate: the solution builds with 0 warnings (the Android projects build Release with warnings as errors), Core tests pass, dotnet format is clean, the conformance check is OK, and check-adr-refs.sh is OK.

Refs #428 (part 4 of 5)

🤖 Generated with Claude Code

…Android Canvas backend (#428 part 4)

AndroidCanvasBackend gains ScaledImageDecode (off) and ImageSamplesPerPixel (2) for
list renders; page renders take RenderOptions.ScaledImageDecode. With it on, an image
is converted at DisplayImage.GetReduction for the canvas (the backend's own RGB
converter caps the codec reduction), the reduced samples are mapped through a
SampleGrid built from the source size and the delivered reduction, and the overhang
is clipped to the unit square. A soft mask of the image's size folds only at the same
delivered reduction; explicit masks fold only at reduction 0; otherwise each mask is
reduced by its own rule (stencils as 8-bit coverage). The converted-image cache is
keyed by the delivered reduction, a finer conversion serving a coarser request. The
software rasterizer groups and soft masks fall back to gets the same setting.
Off, the pixels are unchanged (golden and allocation tests pass as before).

The backend's resampling now works in bands of at most 64 sample rows, as Skia's
does since #432: same pixels, peak RSS 477 -> 350 MiB for the full-resolution cover.

Tests: AndroidScaledImageDecodeTests (21) over figures the device app reports
(ScaledImageDecodeRun): catalogue cover at phone widths, corpus DCT pages, the shared
large-image pages with masks of another size and a colour key, a curved conversion,
1:1 identical, page renders off by default, k = 1, zoom in/out conversions, zero
allocation on a reduced replay. Measurements in
docs/benchmarks/native-scaled-image-decode.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 27, 2026 19:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	docs/benchmarks/native-scaled-image-decode.md
#	docs/conformance/iso-32000-2.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	docs/benchmarks/native-scaled-image-decode.md
#	docs/conformance/iso-32000-2.md
@michaelstonis
michaelstonis merged commit 7648ecf into spec/33-broadside-v1 Oct 27, 2026
29 checks passed
@michaelstonis
michaelstonis deleted the 428-android-scaled-decode branch October 27, 2026 20:58
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.

2 participants