Skip to content

feat(image): add Pixels::with_rgba, buffer-reusing Pixels::replace, and mipmapped ImageCache::rgba_into - #1907

Merged
Hona merged 1 commit into
devfrom
feat/148-pixels-streaming
Nov 24, 2026
Merged

Hona merged 1 commit into
devfrom
feat/148-pixels-streaming

Conversation

@Hona

@Hona Hona commented Nov 24, 2026

Copy link
Copy Markdown
Member

Fixes #148
Fixes #1823
Fixes #1385

Summary

Adds three additive streaming and readback methods on Pixels and ImageCache (crates/rune-image, re-exported by rune):

  • Pixels::with_rgba<R>(&self, f: impl FnOnce(u32, u32, &[u8]) -> R) -> Option<R> (crates/rune-image/src/pixels.rs): borrows the current straight-alpha sRGB RGBA frame without copying. Clones only the Arc<Vec<u8>> pointer under the frame lock and releases the lock before invoking f, so calling Pixels::size, set, or replace inside f cannot deadlock and render copies are not blocked. Returns None when empty or holding decoded mipmaps.
  • Pixels::replace(&self, width: u32, height: u32, rgba: Vec<u8>) -> Vec<u8> (crates/rune-image/src/pixels.rs): replaces the frame and returns the previous frame's Vec<u8> for caller buffer reuse (Pixels::set delegates to it). Retains no idle spare buffer inside Pixels; returns an empty Vec on the first frame, when replacing mipmaps, or while a concurrent atlas copy or with_rgba borrow still holds the old Arc.
  • ImageCache::rgba_into(&self, target: &Pixels, width: u32, height: u32, bytes: impl Into<Arc<[u8]>>) (crates/rune-image/src/lib.rs, crates/rune-image/src/from_url.rs): builds linear-premultiplied mipmaps off the main thread directly into an existing Pixels handle without allocating a new Image or content-hashing the bytes (inline for <= 64×64 on native, shared decoder workers for larger frames, and sliced main-thread tasks on wasm32). A monotonic per-handle AtomicU64 ticket enforces latest-wins ordering across Pixels::set, Pixels::replace, and ImageCache::rgba_into, and weak target handles skip abandoned jobs.
sequenceDiagram
    participant Producer
    participant Pixels
    participant Worker as ImageCache worker
    participant Atlas as ImageAtlas::place

    Producer->>Pixels: replace(w, h, buf) -> prev_buf (ticket++)
    Producer->>Pixels: with_rgba(|w, h, bytes| ...) (lock dropped before callback)
    Producer->>Worker: cache.rgba_into(&pixels, w, h, bytes) (ticket = T)
    Worker->>Pixels: store_mips_if_current(T, decoded)
    Note over Pixels: Skips/drops stale mips if ticket != T
    Pixels-->>Atlas: wakes subscribers; Atlas picks mip for drawn size
Loading
// Zero-copy readback and buffer reuse
let spare = pixels.replace(w, h, next_rgba);
let sample = pixels.with_rgba(|w, h, bytes| (w, h, bytes[0]));

// Off-thread mipmapped update into an existing handle
cache.rgba_into(&thumb_pixels, 256, 256, rgba_bytes);

Outside answers followed

  • Pixels::with_rgba: Rust closure-borrow (with_*) idiom matching Pixels::set and the lock-free Arc snapshot pattern already used in ImageAtlas::place_at.
  • Pixels::replace: Rust standard library std::mem::replace / Option::replace / RefCell::replace idiom returning the displaced owned buffer.
  • ImageCache::rgba_into: Rune's ImageCache::rgba linear-light mip_chain pipeline (crates/rune-image/src/mips.rs) and latest-wins request discipline.

Example & docs

  • examples/matched/src/main.rs: added --scene mips (and interactive M toggle / R frame step) comparing a 256×256 1 px checkerboard downscaled at 37×37, 64×64, and 112×112 pt via unmipped Pixels::replace (with recycled buffer counter and Pixels::with_rgba readback) vs mipmapped ImageCache::rgba_into.
  • Added rustdoc examples on all three new methods, updated crates/rune/README.md and examples/README.md, and added changelog/unreleased/2026-11-24-pixels-with-rgba-replace-rgba-into.md.

Verification

  • Windows: cargo test -p rune-image (63 passed + 3 doctests), cargo test -p rune-image --no-default-features (57 passed + 3 doctests), cargo test -p rune --test pixels_streaming (3 passed, including 0-allocation GlobalAlloc proof for 60 replace + with_rgba updates and rgba_into runtime wake/mip selection), cargo test -p rune --test integration pixels (9 passed), cargo test -p rune --test integration radial_srgb (4 passed), cargo test -p rune --lib pixels (25 passed), cargo test -p matched (2 passed), cargo check -p rune -p rune-kit, cargo check-wasm, cargo check-webgl, cargo check -p rune-image --target wasm32-unknown-unknown, scripts/file-size.sh.
  • macOS (arm64): cargo test -p rune-image (64 passed + 3 doctests).
  • Linux (aarch64): cargo check -p rune-image --tests.
  • Not run: live desktop window inspection of examples/matched --scene mips; runtime execution of the wasm32 sliced rgba_into path inside a browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant