Repository navigation
feat(image): add Pixels::with_rgba, buffer-reusing Pixels::replace, and mipmapped ImageCache::rgba_into - #1907
Merged
Merged
Conversation
…nd mipmapped ImageCache::rgba_into
This was referenced Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #148
Fixes #1823
Fixes #1385
Summary
Adds three additive streaming and readback methods on
PixelsandImageCache(crates/rune-image, re-exported byrune):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 theArc<Vec<u8>>pointer under the frame lock and releases the lock before invokingf, so callingPixels::size,set, orreplaceinsidefcannot deadlock and render copies are not blocked. ReturnsNonewhen 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'sVec<u8>for caller buffer reuse (Pixels::setdelegates to it). Retains no idle spare buffer insidePixels; returns an emptyVecon the first frame, when replacing mipmaps, or while a concurrent atlas copy orwith_rgbaborrow still holds the oldArc.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 existingPixelshandle without allocating a newImageor content-hashing the bytes (inline for<= 64×64on native, shared decoder workers for larger frames, and sliced main-thread tasks onwasm32). A monotonic per-handleAtomicU64ticket enforces latest-wins ordering acrossPixels::set,Pixels::replace, andImageCache::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 sizeOutside answers followed
Pixels::with_rgba: Rust closure-borrow (with_*) idiom matchingPixels::setand the lock-freeArcsnapshot pattern already used inImageAtlas::place_at.Pixels::replace: Rust standard librarystd::mem::replace/Option::replace/RefCell::replaceidiom returning the displaced owned buffer.ImageCache::rgba_into: Rune'sImageCache::rgbalinear-lightmip_chainpipeline (crates/rune-image/src/mips.rs) and latest-wins request discipline.Example & docs
examples/matched/src/main.rs: added--scene mips(and interactiveMtoggle /Rframe step) comparing a 256×256 1 px checkerboard downscaled at 37×37, 64×64, and 112×112 pt via unmippedPixels::replace(with recycled buffer counter andPixels::with_rgbareadback) vs mipmappedImageCache::rgba_into.crates/rune/README.mdandexamples/README.md, and addedchangelog/unreleased/2026-11-24-pixels-with-rgba-replace-rgba-into.md.Verification
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-allocationGlobalAllocproof for 60replace+with_rgbaupdates andrgba_intoruntime 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.cargo test -p rune-image(64 passed + 3 doctests).cargo check -p rune-image --tests.examples/matched --scene mips; runtime execution of thewasm32slicedrgba_intopath inside a browser.