Screenshots, recording and --gfx-out keep the screen's pixels on the host
CI / test (push) Failing after 17s
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:
13 files changed
+668
-26
No files matched your search
+13
-2
@@ -144,12 +144,23 @@ pub fn run(args : Array[String]) -> Int {
|
||||
comp.shell.screen_w = width
|
||||
// Screenshots and recordings read the host's framebuffer (it draws the desktop).
|
||||
comp.frame_source = Some(fn() { Some(@raster.read_frame(comp.output_w, comp.output_h)) })
|
||||
// Recording streams frames from the host straight to the encoder.
|
||||
comp.frame_recorder = Some({
|
||||
grab: fn(r, pointer, px, py) { @raster.grab_recording_frame(r, pointer, px, py) },
|
||||
send: fn(fd) { @raster.send_recording_frame(fd) },
|
||||
})
|
||||
// And screenshots crop and encode the frozen screen there.
|
||||
comp.frame_shots = Some({
|
||||
snapshot: fn() { @raster.snapshot_frame() },
|
||||
png: fn(r) { @raster.snapshot_png(r) },
|
||||
pixels: fn(r, w, h) { @raster.snapshot_pixels(r, w, h) },
|
||||
})
|
||||
if gfx_out.length() > 0 && !serve {
|
||||
match comp.compose_scene() {
|
||||
Some(sg) => @raster.submit(sg)
|
||||
None => ()
|
||||
}
|
||||
ignore(@sys.write_file_bytes(gfx_out, @renderer.encode_png(@raster.read_frame(width, height))))
|
||||
ignore(@raster.write_frame_png(gfx_out))
|
||||
comp.close()
|
||||
@sys.log("frame written to " + gfx_out)
|
||||
return 0
|
||||
@@ -329,7 +340,7 @@ pub fn run(args : Array[String]) -> Int {
|
||||
}
|
||||
// No display: --serve keeps a PNG of the latest frame for looking at a live compositor.
|
||||
if serve && gfx_out.length() > 0 && frames % 30 == 0 && need_present {
|
||||
ignore(@sys.write_file_bytes(gfx_out, @renderer.encode_png(@raster.read_frame(width, height))))
|
||||
ignore(@raster.write_frame_png(gfx_out))
|
||||
need_present = false
|
||||
}
|
||||
busy = busy || need_present
|
||||
|
||||
@@ -121,6 +121,12 @@ pub(all) struct ChandraCompositor {
|
||||
/// Reads the screen for screenshots when the host draws it (the wasm compositor sets it);
|
||||
/// None uses the software renderer's last frame.
|
||||
mut frame_source : (() -> @renderer.Pixmap?)?
|
||||
/// Records the screen on the host when the host draws it, so frames never cross into the
|
||||
/// compositor (the wasm compositor sets it); None grabs pixels through `frame_source`.
|
||||
mut frame_recorder : FrameRecorder?
|
||||
/// Takes screenshots on the host when the host draws the screen (the wasm compositor sets
|
||||
/// it); None crops pixels from `frame_source`.
|
||||
mut frame_shots : FrameShots?
|
||||
/// Screen recording (recorder.mbt): the recording running, whether it shows the pointer,
|
||||
/// a press on the REC pill (its release is swallowed too) and the last video saved.
|
||||
mut rec : Recording?
|
||||
@@ -209,6 +215,8 @@ pub fn ChandraCompositor::new(
|
||||
shot_dir: "",
|
||||
last_shot: "",
|
||||
frame_source: None,
|
||||
frame_recorder: None,
|
||||
frame_shots: None,
|
||||
rec: None,
|
||||
rec_pointer: true,
|
||||
rec_press: false,
|
||||
|
||||
@@ -37,6 +37,14 @@ pub(all) struct Recording {
|
||||
mut frames : Int
|
||||
}
|
||||
|
||||
///| Screen recording done by the host that draws the screen: `grab` copies a rectangle of the
|
||||
/// screen (with the pointer at a screen position when asked) into the host's frame, and `send`
|
||||
/// writes what is left of it to the encoder's socket (the bytes still left, or -1).
|
||||
pub(all) struct FrameRecorder {
|
||||
grab : (@scene.Rect, Bool, Int, Int) -> Bool
|
||||
send : (Int) -> Int
|
||||
}
|
||||
|
||||
///| Whether a recording is running.
|
||||
pub fn ChandraCompositor::recording(self : ChandraCompositor) -> Bool {
|
||||
self.rec is Some(_)
|
||||
@@ -95,7 +103,7 @@ pub fn ChandraCompositor::toggle_recording(self : ChandraCompositor, mode : Shot
|
||||
|
||||
///| The overlay, picking what to record.
|
||||
fn ChandraCompositor::open_record_overlay(self : ChandraCompositor, pick : Bool) -> Unit {
|
||||
match self.grab_screen() {
|
||||
match self.freeze_screen() {
|
||||
Some(frame) => {
|
||||
self.open_overlay(frame, pick)
|
||||
match self.shot {
|
||||
@@ -225,6 +233,33 @@ fn ChandraCompositor::recording_tick(self : ChandraCompositor) -> Unit {
|
||||
return
|
||||
}
|
||||
}
|
||||
match self.frame_recorder {
|
||||
Some(host) => {
|
||||
// The host holds the frame in flight.
|
||||
if rec.sent > 0 {
|
||||
let left = (host.send)(rec.fd)
|
||||
if left < 0 {
|
||||
self.stop_recording()
|
||||
return
|
||||
}
|
||||
if left > 0 {
|
||||
return
|
||||
}
|
||||
rec.sent = 0
|
||||
}
|
||||
if now < rec.next_at {
|
||||
return
|
||||
}
|
||||
rec.next_at = now + 33UL
|
||||
if (host.grab)(rec.rect, self.rec_pointer, self.ptr_x.to_int(), self.ptr_y.to_int()) {
|
||||
rec.sent = 1 // a frame is waiting on the host
|
||||
rec.frames = rec.frames + 1
|
||||
}
|
||||
self.damage.mark_full()
|
||||
return
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
// The frame in flight.
|
||||
while rec.sent < rec.pending.length() {
|
||||
let len = (rec.pending.length() - rec.sent).min(1 << 20)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,36 @@ pub fn read_frame(_width : Int, _height : Int) -> @renderer.Pixmap {
|
||||
abort("the host rasteriser is only available to the wasm compositor")
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn write_frame_png(_path : String) -> Bool {
|
||||
abort("the host rasteriser is only available to the wasm compositor")
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn grab_recording_frame(_r : @scene.Rect, _pointer : Bool, _px : Int, _py : Int) -> Bool {
|
||||
abort("the host rasteriser is only available to the wasm compositor")
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn send_recording_frame(_fd : Int) -> Int {
|
||||
abort("the host rasteriser is only available to the wasm compositor")
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn snapshot_frame() -> Bool {
|
||||
abort("the host rasteriser is only available to the wasm compositor")
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn snapshot_png(_r : @scene.Rect) -> Bytes {
|
||||
abort("the host rasteriser is only available to the wasm compositor")
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn snapshot_pixels(_r : @scene.Rect, _w : Int, _h : Int) -> @renderer.Pixmap {
|
||||
abort("the host rasteriser is only available to the wasm compositor")
|
||||
}
|
||||
|
||||
///| Images already loaded, by path (0: it failed, don't try again).
|
||||
let loaded_images : Map[String, UInt] = {}
|
||||
|
||||
|
||||
@@ -97,6 +97,37 @@ pub fn read_frame(width : Int, height : Int) -> @renderer.Pixmap {
|
||||
{ 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)
|
||||
}
|
||||
|
||||
///| Copy `r` of the host framebuffer into the host's recording frame (pointer drawn at screen
|
||||
/// position (`px`, `py`) when `pointer`).
|
||||
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)
|
||||
}
|
||||
|
||||
///| Write what is left of the recording frame to `fd`: the bytes still left, 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) }
|
||||
}
|
||||
|
||||
///| Images already loaded, by path (0: it failed, don't ask the host again).
|
||||
let loaded_images : Map[String, UInt] = {}
|
||||
|
||||
|
||||
Reference in new issue
Block a user