Files
chandra/compositor/raster/raster_wasm.mbt
T
Claude 024f18ce61
CI / test (push) Canceled after 0s
Screen recordings keep up with animations: every drawn frame, up to 60 fps, with its real time
A recording of an animation looked choppy because frames barely reached ffmpeg: each 8 MB
1080p frame went out only as far as the socket buffer took it on each main loop pass, so a
new frame could be taken only every few hundred ms (V's recording changed picture ~12 times a
second; headless, 2 a second). Frames were also stamped when ffmpeg read them, not when drawn,
and every grab forced a full redraw.

Now:
- Both hosts (wasmhost raster_stub.c, host-wasm main.rs) copy a frame into a 3-slot queue and
  a writer thread sends it, so a frame never waits on the main loop. A full queue (ffmpeg
  behind) means that frame is not taken; the next one is, with its own time.
- The stream is uncompressed RGBA in Matroska (recorder.mbt mkv_stream_header /
  mkv_frame_header; the hosts write the same 42-byte cluster header), each frame stamped
  with the time it was drawn. ffmpeg makes constant 60 fps H.264 from it, under nice -n 10.
- A frame is taken when the desktop draws a changed frame (app.mbt draw_frame,
  compose_frame), at most 60 a second, or when the pointer moves. A still screen sends
  nothing; the REC pill redraws only its own rectangle when its seconds turn.
- ffmpeg going away mid-recording says so in the notification.
- Tests: the cluster header bytes, ffprobe reading the stream with each frame's own time,
  and the recording test now checks a 60 fps file as long as the recording ran. The
  recording tests fail with a clear message when ffmpeg/ffprobe are missing instead of
  returning early; CI installs ffmpeg.

Headless 1080p, one window redrawing at 60 fps (rec-bench.sh in project files
recorder-smooth/): distinct frames in the video 2.1/s -> 58.9/s (MoonBit host), 59.1/s
(Rust host); overview open/close 1.8/s -> 38.7/s (its previews refresh at 5-10/s live).
Compositor kept drawing 57 fps while recording; still screen: compositor 8% CPU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjEUQq16Te1LPYMinT
2026-11-26 04:14:28 +00:00

151 lines
5.3 KiB
MoonBit

///| Send a scene to the host to draw (`chandra:host/raster`).
///
/// The scene is the same one the software renderer takes (`@scene.SceneGraph`); here it is turned
/// into the interface's records and handed over. The host owns the framebuffer and does the pixel
/// work natively; client window buffers are already there (captured at commit, by id).
///|
fn conv_rect(r : @scene.Rect) -> @hostraster.Rect {
{ x: r.x, y: r.y, w: r.w, h: r.h }
}
///|
fn conv_color(c : @scene.Rgba) -> @hostraster.Color {
{ r: c.r, g: c.g, b: c.b, a: c.a }
}
///|
fn conv_align(a : @scene.TextAlign) -> Byte {
match a {
Left => b'\x00'
Center => b'\x01'
Right => b'\x02'
}
}
///|
fn conv_op(op : @scene.DrawOp) -> @hostraster.Op {
match op {
FillRect(r, c, radius) => FillRect({ area: conv_rect(r), color: conv_color(c), radius })
StrokeRect(r, c, width, radius) => StrokeRect({ area: conv_rect(r), color: conv_color(c), width, radius })
Line(x0, y0, x1, y1, c, width) => DrawLine({ x0, y0, x1, y1, color: conv_color(c), width })
Circle(cx, cy, radius, c, filled) => DrawCircle({ cx, cy, radius, color: conv_color(c), filled })
Text(x, y, text, size, c, align, bold) =>
DrawText({ x, y, text, size, color: conv_color(c), align: conv_align(align), bold, mono: false })
MonoText(x, y, text, size, c, align, bold) =>
DrawText({ x, y, text, size, color: conv_color(c), align: conv_align(align), bold, mono: true })
PushClip(r) => PushClip(conv_rect(r))
PopClip => PopClip
Image(r, image) => DrawImage({ area: conv_rect(r), image })
}
}
///|
fn conv_ops(ops : Array[@scene.DrawOp]) -> Array[@hostraster.Op] {
ops.map(conv_op)
}
///| The backdrop (wallpaper and the desktop card on it) the host last drew, and the size it was
/// drawn for. While neither changes the frame says "unchanged" and the host reuses its cached
/// pixels instead of being sent about two thousand ops again and drawing them.
let last_backdrop : Ref[Array[@scene.DrawOp]?] = { val: None }
///|
let last_size : Ref[(Int, Int)] = { val: (0, 0) }
///| Draw `scene` into the host framebuffer.
pub fn submit(scene : @scene.SceneGraph) -> Unit {
let backdrop = scene.wallpaper_ops + scene.background_ops
let same = match last_backdrop.val {
Some(prev) => prev == backdrop && last_size.val == (scene.output_w, scene.output_h)
None => false
}
if !same {
last_backdrop.val = Some(backdrop)
last_size.val = (scene.output_w, scene.output_h)
}
let windows : Array[@hostraster.Window] = scene.windows.map(w => {
let (top, right, bottom, left) = w.deco_insets
{
geometry: conv_rect(w.geometry),
inset_top: top,
inset_right: right,
inset_bottom: bottom,
inset_left: left,
deco: conv_ops(w.deco_ops),
fullscreen: w.fullscreen,
surface: match w.surface {
Some(s) => s.host_id
None => 0U
},
}
})
@hostraster.submit({
width: scene.output_w,
height: scene.output_h,
wallpaper: if same { None } else { Some(conv_ops(backdrop)) },
background: [],
windows,
panel: conv_ops(scene.panel_ops),
popups: scene.popups.map(p => { area: conv_rect(p.0), surface: p.1.host_id }),
})
}
///| The host framebuffer as a pixmap (screenshots and tests; the display path never needs it).
pub fn read_frame(width : Int, height : Int) -> @renderer.Pixmap {
let data = @hostraster.read_frame()
{ width, height, data }
}
///| Save the host framebuffer as a PNG at `path` (the pixels never leave the host).
pub fn write_frame_png(path : String) -> Bool {
@hostraster.write_frame_png(path)
}
///| Queue a copy of `r` of the host framebuffer for the recording, stamped with the host's clock
/// (pointer drawn at screen position (`px`, `py`) when `pointer`). False when the queue is full.
pub fn grab_recording_frame(r : @scene.Rect, pointer : Bool, px : Int, py : Int) -> Bool {
@hostraster.grab_recording_frame(r.x, r.y, r.w, r.h, pointer, px, py)
}
///| Keep the host's recording writer sending to `fd` (frames queued, or -1 once a write failed);
/// a negative `fd` sends what is queued and stops it (0, or -1).
pub fn send_recording_frame(fd : Int) -> Int {
@hostraster.send_recording_frame(fd)
}
///| Freeze the host framebuffer as the screenshot snapshot.
pub fn snapshot_frame() -> Bool {
@hostraster.snapshot_frame()
}
///| `r` of the snapshot, opaque, as PNG bytes.
pub fn snapshot_png(r : @scene.Rect) -> Bytes {
Bytes::from_fixedarray(@hostraster.snapshot_png(r.x, r.y, r.w, r.h))
}
///| `r` of the snapshot scaled to `w` x `h`, opaque.
pub fn snapshot_pixels(r : @scene.Rect, w : Int, h : Int) -> @renderer.Pixmap {
{ width: w, height: h, data: @hostraster.snapshot_pixels(r.x, r.y, r.w, r.h, w, h) }
}
///| `w` x `h` RGBA pixels as an image (`reuse`: an earlier handle of this kind to overwrite, or 0).
pub fn image_pixels(w : Int, h : Int, rgba : Bytes, reuse : UInt) -> UInt {
@hostraster.image_pixels(w, h, rgba.to_fixedarray(), reuse)
}
///| Images already loaded, by path (0: it failed, don't ask the host again).
let loaded_images : Map[String, UInt] = {}
///| Have the host load the PNG or SVG file at `path` for `Image` draw ops: a handle, or 0.
pub fn load_image(path : String) -> UInt {
match loaded_images.get(path) {
Some(h) => h
None => {
let h = @hostraster.load_image(path)
loaded_images.set(path, h)
h
}
}
}