Screenshots, recording and --gfx-out keep the screen's pixels on the host
CI / test (push) Failing after 17s

New chandra:host/raster functions, on both hosts: write-frame-png (--gfx-out),
grab-recording-frame / send-recording-frame (the recorder streams frames from the
host to ffmpeg) and snapshot-frame / snapshot-png / snapshot-pixels (screenshots
freeze, crop and encode the screen there; the magnifier and the notification's
thumbnail read small scaled copies). The wasm compositor sets them up through
frame_recorder and frame_shots; the native compositor keeps its pixel path.

Moving whole frames as list<u8> is cheap for the Rust host but costs about
0.4 s a 720p frame through the wasmtime C API. A full-screen screenshot now takes
45 ms on the MoonBit host (was 472) and 8 ms on the Rust host (was 112);
recording runs at 30 fps on both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hiNfjrw2edja7LiRLB
This commit is contained in:
Claude committed 2026-11-25 20:03:34 +00:00
1 parent 3b15248124
commit caa12058fd
13 files changed
+668 -26

No files matched your search

+62 -20
View File
@@ -73,7 +73,8 @@ pub fn ShotMode::from_int(i : Int) -> ShotMode? {
///| The selection overlay while it is up.
pub(all) struct ShotOverlay {
/// The frozen screen.
/// The frozen screen. When the host takes screenshots (`frame_shots`) its pixels stay there
/// and `data` is empty.
frame : @renderer.Pixmap
/// Window-pick mode: a click takes the window under it, there is no rectangle.
pick : Bool
@@ -97,6 +98,15 @@ pub(all) struct ShotOverlay {
prev_focus : UInt64?
}
///| Screenshots taken by the host that draws the screen, so the screen's pixels never cross into
/// the compositor: `snapshot` freezes the screen there, `png` crops the frozen screen to PNG
/// bytes, and `pixels` reads a rectangle of it scaled to a size (magnifier, thumbnail).
pub(all) struct FrameShots {
snapshot : () -> Bool
png : (@scene.Rect) -> Bytes
pixels : (@scene.Rect, Int, Int) -> @renderer.Pixmap
}
///| The confirmation shown after a screenshot.
pub(all) struct ShotToast {
title : String
@@ -200,6 +210,20 @@ fn ChandraCompositor::grab_screen(self : ChandraCompositor) -> @renderer.Pixmap?
}
}
///| Freeze what is on screen for a screenshot: on the host when it takes them (an empty pixmap
/// of the screen's size here), else a copy of the pixels.
fn ChandraCompositor::freeze_screen(self : ChandraCompositor) -> @renderer.Pixmap? {
match self.frame_shots {
Some(host) =>
if (host.snapshot)() {
Some({ width: self.output_w, height: self.output_h, data: FixedArray::make(0, b'\x00') })
} else {
None
}
None => self.grab_screen()
}
}
///| A window's rectangle on screen with its title bar and borders (Spectacle's default).
fn ChandraCompositor::outer_rect(self : ChandraCompositor, wid : UInt64) -> @scene.Rect? {
match self.windows.get(wid) {
@@ -242,7 +266,7 @@ pub fn ChandraCompositor::take_screenshot(self : ChandraCompositor, mode : ShotM
if self.shot is Some(_) {
return
}
let frame = match self.grab_screen() {
let frame = match self.freeze_screen() {
Some(f) => f
None => return
}
@@ -377,16 +401,26 @@ fn ChandraCompositor::finish_screenshot(self : ChandraCompositor, frame : @rende
}
let w = x1 - x0
let h = y1 - y0
let data = FixedArray::make(w * h * 4, b'\x00')
for y = 0; y < h; y = y + 1 {
FixedArray::blit_to(frame.data, data, len=w * 4, src_offset=((y0 + y) * frame.width + x0) * 4, dst_offset=y * w * 4)
// `shot` is what the thumbnail is made from: the picture, or a small copy from the host.
let (shot, png) = match self.frame_shots {
Some(host) => {
let crop = @scene.Rect::new(x0, y0, w, h)
let (tw, th) = thumb_size(w, h, 96, 60)
((host.pixels)(crop, tw, th), (host.png)(crop))
}
None => {
let data = FixedArray::make(w * h * 4, b'\x00')
for y = 0; y < h; y = y + 1 {
FixedArray::blit_to(frame.data, data, len=w * 4, src_offset=((y0 + y) * frame.width + x0) * 4, dst_offset=y * w * 4)
}
// Fully opaque: the framebuffer's alpha means nothing in a saved picture.
for i = 3; i < data.length(); i = i + 4 {
data[i] = b'\xff'
}
let shot : @renderer.Pixmap = { width: w, height: h, data }
(shot, @renderer.encode_png_compressed(shot))
}
}
// Fully opaque: the framebuffer's alpha means nothing in a saved picture.
for i = 3; i < data.length(); i = i + 4 {
data[i] = b'\xff'
}
let shot : @renderer.Pixmap = { width: w, height: h, data }
let png = @renderer.encode_png_compressed(shot)
self.server.set_own_selection(["image/png"], png)
let dir = if self.shot_dir.length() > 0 { self.shot_dir } else { pictures_dir() + "/Screenshots" }
let path = save_png(dir, png)
@@ -465,6 +499,16 @@ fn save_png(dir : String, png : Bytes) -> String {
if @sys.write_file_bytes(path, png) == 0 { path } else { "" }
}
///| `w` x `h` fitted into `max_w` x `max_h`, keeping the aspect ratio, never enlarging.
fn thumb_size(w : Int, h : Int, max_w : Int, max_h : Int) -> (Int, Int) {
let (tw, th) = if w * max_h > h * max_w {
(max_w, (h * max_w / w).max(1))
} else {
((w * max_h / h).max(1), max_h)
}
(tw.min(w), th.min(h))
}
///| A small picture of `p` as draw ops (runs of one colour per row become one rectangle),
/// fitted into `max_w` x `max_h`, and its size.
fn thumbnail_ops(p : @renderer.Pixmap, max_w : Int, max_h : Int) -> (Array[@scene.DrawOp], Int, Int) {
@@ -472,14 +516,7 @@ fn thumbnail_ops(p : @renderer.Pixmap, max_w : Int, max_h : Int) -> (Array[@scen
if p.width <= 0 || p.height <= 0 {
return (ops, 0, 0)
}
// Fit, keeping the aspect ratio, never enlarging.
let (tw, th) = if p.width * max_h > p.height * max_w {
(max_w, (p.height * max_w / p.width).max(1))
} else {
((p.width * max_h / p.height).max(1), max_h)
}
let tw = tw.min(p.width)
let th = th.min(p.height)
let (tw, th) = thumb_size(p.width, p.height, max_w, max_h)
for ty = 0; ty < th; ty = ty + 1 {
let sy = ty * p.height / th
let mut run_x = 0
@@ -974,9 +1011,14 @@ fn ChandraCompositor::magnifier_ops(self : ChandraCompositor, o : ShotOverlay, x
let my = if y + 24 + size <= self.output_h { y + 24 } else { y - 24 - size }
let p = self.theme.palette
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(mx - 2, my - 2, size + 4, size + 4), p.border, 2))
// The pixels around (x, y), from the host when it holds the frozen screen.
let (src, ox, oy) = match self.frame_shots {
Some(host) => ((host.pixels)(@scene.Rect::new(x - n / 2, y - n / 2, n, n), n, n), 0, 0)
None => (o.frame, x - n / 2, y - n / 2)
}
for j = 0; j < n; j = j + 1 {
for i = 0; i < n; i = i + 1 {
let c = match o.frame.pixel(x - n / 2 + i, y - n / 2 + j) {
let c = match src.pixel(ox + i, oy + j) {
Some(c) => @scene.Rgba::new(c.r.to_int(), c.g.to_int(), c.b.to_int(), 255)
None => @scene.Rgba::new(0, 0, 0, 255)
}