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
@@ -50,6 +50,13 @@ struct State {
|
||||
pl: Option<plasmoid::Runtime>,
|
||||
/// Fds beyond the first that arrived with the last `recvmsg` on a socket, for `take-fd`.
|
||||
extra_fds: std::collections::HashMap<i32, std::collections::VecDeque<i32>>,
|
||||
/// The screen recording frame in flight (`grab-recording-frame`) and how much of it went out.
|
||||
rec_frame: Vec<u8>,
|
||||
rec_sent: usize,
|
||||
/// The frozen screen of a screenshot (`snapshot-frame`): RGBA8888, its width and height.
|
||||
snap: Vec<u8>,
|
||||
snap_w: i32,
|
||||
snap_h: i32,
|
||||
}
|
||||
|
||||
fn sockaddr_un(path: &str) -> (libc::sockaddr_un, libc::socklen_t) {
|
||||
@@ -427,6 +434,143 @@ impl chandra::host::raster::Host for State {
|
||||
fd
|
||||
}
|
||||
}
|
||||
|
||||
fn write_frame_png(&mut self, path: String) -> bool {
|
||||
let (w, h) = (self.raster.w, self.raster.h);
|
||||
if w <= 0 || h <= 0 {
|
||||
return false;
|
||||
}
|
||||
let Ok(file) = std::fs::File::create(&path) else { return false };
|
||||
let mut enc = png::Encoder::new(std::io::BufWriter::new(file), w as u32, h as u32);
|
||||
enc.set_color(png::ColorType::Rgba);
|
||||
enc.set_depth(png::BitDepth::Eight);
|
||||
enc.set_compression(png::Compression::Fast);
|
||||
match enc.write_header() {
|
||||
Ok(mut wr) => wr.write_image_data(&self.raster.fb).is_ok(),
|
||||
Err(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn grab_recording_frame(&mut self, x: i32, y: i32, w: i32, h: i32, pointer: bool, px: i32, py: i32) -> bool {
|
||||
let (fw, fh) = (self.raster.w, self.raster.h);
|
||||
if w <= 0 || h <= 0 || x < 0 || y < 0 || x + w > fw || y + h > fh {
|
||||
return false;
|
||||
}
|
||||
let (w, h) = (w as usize, h as usize);
|
||||
let mut out = std::mem::take(&mut self.rec_frame);
|
||||
out.clear();
|
||||
out.reserve(w * h * 4);
|
||||
for row in 0..h {
|
||||
let at = ((y as usize + row) * fw as usize + x as usize) * 4;
|
||||
out.extend_from_slice(&self.raster.fb[at..at + w * 4]);
|
||||
}
|
||||
for a in out.iter_mut().skip(3).step_by(4) {
|
||||
*a = 255;
|
||||
}
|
||||
if pointer {
|
||||
draw_record_pointer(&mut out, w as i32, h as i32, px - x, py - y);
|
||||
}
|
||||
self.rec_frame = out;
|
||||
self.rec_sent = 0;
|
||||
true
|
||||
}
|
||||
|
||||
fn send_recording_frame(&mut self, fd: i32) -> i32 {
|
||||
unsafe {
|
||||
let fl = libc::fcntl(fd, libc::F_GETFL);
|
||||
if fl >= 0 && fl & libc::O_NONBLOCK == 0 {
|
||||
libc::fcntl(fd, libc::F_SETFL, fl | libc::O_NONBLOCK);
|
||||
}
|
||||
while self.rec_sent < self.rec_frame.len() {
|
||||
let rest = &self.rec_frame[self.rec_sent..];
|
||||
let n = libc::write(fd, rest.as_ptr() as *const _, rest.len());
|
||||
if n < 0 {
|
||||
let e = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
|
||||
if e == libc::EINTR {
|
||||
continue;
|
||||
}
|
||||
if e == libc::EAGAIN || e == libc::EWOULDBLOCK {
|
||||
break;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
self.rec_sent += n as usize;
|
||||
}
|
||||
}
|
||||
(self.rec_frame.len() - self.rec_sent).min(i32::MAX as usize) as i32
|
||||
}
|
||||
|
||||
fn snapshot_frame(&mut self) -> bool {
|
||||
if self.raster.w <= 0 || self.raster.h <= 0 {
|
||||
return false;
|
||||
}
|
||||
self.snap.clear();
|
||||
self.snap.extend_from_slice(&self.raster.fb);
|
||||
(self.snap_w, self.snap_h) = (self.raster.w, self.raster.h);
|
||||
true
|
||||
}
|
||||
|
||||
fn snapshot_png(&mut self, x: i32, y: i32, w: i32, h: i32) -> Vec<u8> {
|
||||
let (x0, y0) = (x.max(0), y.max(0));
|
||||
let (x1, y1) = (x.saturating_add(w).min(self.snap_w), y.saturating_add(h).min(self.snap_h));
|
||||
if x1 <= x0 || y1 <= y0 {
|
||||
return Vec::new();
|
||||
}
|
||||
let (w, h) = (x1 - x0, y1 - y0);
|
||||
let px = self.snapshot_pixels(x0, y0, w, h, w, h);
|
||||
let mut out = Vec::new();
|
||||
let mut enc = png::Encoder::new(&mut out, w as u32, h as u32);
|
||||
enc.set_color(png::ColorType::Rgba);
|
||||
enc.set_depth(png::BitDepth::Eight);
|
||||
let ok = match enc.write_header() {
|
||||
Ok(mut wr) => wr.write_image_data(&px).is_ok(),
|
||||
Err(_) => false,
|
||||
};
|
||||
if ok { out } else { Vec::new() }
|
||||
}
|
||||
|
||||
fn snapshot_pixels(&mut self, x: i32, y: i32, w: i32, h: i32, out_w: i32, out_h: i32) -> Vec<u8> {
|
||||
if out_w <= 0 || out_h <= 0 || out_w > 16384 || out_h > 16384 {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut out = vec![0u8; out_w as usize * out_h as usize * 4];
|
||||
for ty in 0..out_h {
|
||||
let sy = y as i64 + ty as i64 * h as i64 / out_h as i64;
|
||||
for tx in 0..out_w {
|
||||
let sx = x as i64 + tx as i64 * w as i64 / out_w as i64;
|
||||
let d = (ty as usize * out_w as usize + tx as usize) * 4;
|
||||
if sx >= 0 && sy >= 0 && sx < self.snap_w as i64 && sy < self.snap_h as i64 {
|
||||
let s = (sy as usize * self.snap_w as usize + sx as usize) * 4;
|
||||
out[d..d + 3].copy_from_slice(&self.snap[s..s + 3]);
|
||||
}
|
||||
out[d + 3] = 255;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
/// The arrow pointer with its tip at (x, y) in an RGBA `w` x `h` frame, as the compositor's
|
||||
/// recorder draws it: a black body widening over 13 rows and a short tail, then the white inside.
|
||||
fn draw_record_pointer(px: &mut [u8], w: i32, h: i32, x: i32, y: i32) {
|
||||
let mut fill = |rx: i32, ry: i32, rw: i32, rh: i32, v: u8| {
|
||||
for j in ry.max(0)..(ry + rh).min(h) {
|
||||
for i in rx.max(0)..(rx + rw).min(w) {
|
||||
let at = ((j * w + i) * 4) as usize;
|
||||
px[at..at + 4].copy_from_slice(&[v, v, v, 255]);
|
||||
}
|
||||
}
|
||||
};
|
||||
for i in 0..17 {
|
||||
let (l, bw) = if i < 13 { (0, i * 2 / 3 + 1) } else { (3 + (i - 13), 3) };
|
||||
fill(x + l - 1, y + i - 1, bw + 2, 3, 0);
|
||||
}
|
||||
for i in 1..16 {
|
||||
let (l, bw) = if i < 13 { (0, i * 2 / 3 - 1) } else { (4 + (i - 13), 1) };
|
||||
if bw > 0 {
|
||||
fill(x + l + 1, y + i, bw, 1, 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Host for State {
|
||||
|
||||
Reference in new issue
Block a user