Skip to content

Pixels: no way to read the image back, so apps keep a second copy #148

Done

Description

@godismyjudge95

What happens

Pixels takes an RGBA buffer by value in set and keeps it, but has no way to read it back. The public API at dev d03349e2 is new, source_url, source_set, set and size (pixels.rs#L49-L118). An app that shows an image and also edits, copies or saves it must keep its own copy beside the Pixels.

Why it matters

For a screenshot app every full image is large: a 3024x1964 RGBA capture is 23.8 MB (width x height x 4). We keep the full-size images we show (a cloud image in the viewer, the frozen frame in the region selector) twice: once as our RgbaImage for edit, copy to clipboard and save, and once inside a Pixels. That is one extra full copy per open image. We did not measure this cost on its own; the number is the arithmetic above.

Ask

A borrow, so no copy is made:

impl Pixels {
    /// Runs `f` with the current frame: width, height and straight-alpha RGBA rows, top first.
    /// None when the image came from an `ImageCache` decode (it holds premultiplied mips only).
    pub fn with_rgba<R>(&self, f: impl FnOnce(u32, u32, &[u8]) -> R) -> Option<R>;
}

The Mutex around Frame already guards the buffer (pixels.rs#L11-L23), so the closure form keeps the lock short and lets set swap the buffer afterwards. For decoded images, mip 0 could be returned with a premultiplied flag later; we only need frames given through set.

Done when: an app can read the RGBA of a Pixels it filled with set without copying it, and a test checks that the bytes it reads equal the bytes it set.


Posted via Claude

Activity

  1. godismyjudge95 commented on Nov 6, 2026

    @godismyjudge95
    Author

    Related: #142 (a hidden overlay keeps its last frame) and #80 (GPU block sizing). All three are about memory for full-screen images in a screenshot app.


    Posted via Claude

  2. Hona commented on Nov 24, 2026

    @Hona
    Member

    Added Pixels::with_rgba on dev in #1907 (commit 1f6dd178b).

    use rune::Pixels;
    
    let pixels = Pixels::new();
    pixels.set(2, 1, vec![10, 20, 30, 255, 40, 50, 60, 255]);
    let first = pixels.with_rgba(|w, h, bytes| (w, h, bytes[0]));

    This follows the Rust closure-borrow (with_*) pattern and matches the lock-free Arc snapshot used in ImageAtlas::place_at: with_rgba clones only the Arc<Vec<u8>> pointer under the frame lock and drops the lock before running the closure, so calling size, set, or replace inside the callback cannot deadlock and rendering is not blocked. It returns None when empty or when the handle holds decoded mipmaps, and preserves in-place Arc reuse after the closure returns.

    You can also see it in examples/matched (--scene mips).

    Verified with unit tests and doctests in crates/rune-image on Windows (63 passed + 3 doctests) and macOS arm64 (64 passed + 3 doctests), zero-allocation streaming tests in crates/rune/tests/pixels_streaming.rs and crates/rune pixels tests on Windows, Linux aarch64 compile check (cargo check -p rune-image --tests), and wasm32-unknown-unknown compile checks (cargo check-wasm, cargo check-webgl). Not run: a live desktop window or browser runtime.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions