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
+115
-3
@@ -175,6 +175,59 @@ extern "C" fn rs_read_frame(out : FixedArray[Byte]) = "rs_read_frame"
|
||||
///|
|
||||
extern "C" fn rs_export_memfd() -> Int = "rs_export_memfd"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_grab_recording(
|
||||
x : Int,
|
||||
y : Int,
|
||||
w : Int,
|
||||
h : Int,
|
||||
pointer : Int,
|
||||
px : Int,
|
||||
py : Int,
|
||||
) -> Int = "rs_grab_recording"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_send_recording(fd : Int) -> Int = "rs_send_recording"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_snapshot() -> Int = "rs_snapshot"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_snapshot_w() -> Int = "rs_snapshot_w"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_snapshot_h() -> Int = "rs_snapshot_h"
|
||||
|
||||
///|
|
||||
#borrow(out)
|
||||
extern "C" fn rs_snapshot_pixels(
|
||||
x : Int,
|
||||
y : Int,
|
||||
w : Int,
|
||||
h : Int,
|
||||
out_w : Int,
|
||||
out_h : Int,
|
||||
out : FixedArray[Byte],
|
||||
) -> Unit = "rs_snapshot_pixels"
|
||||
|
||||
///| `x`, `y`, `w` x `h` of the screenshot snapshot scaled to `out_w` x `out_h`, opaque.
|
||||
fn snapshot_pixels(x : Int, y : Int, w : Int, h : Int, out_w : Int, out_h : Int) -> FixedArray[Byte] {
|
||||
if out_w <= 0 || out_h <= 0 || out_w > 16384 || out_h > 16384 {
|
||||
return FixedArray::make(0, b'\x00')
|
||||
}
|
||||
let out = FixedArray::make(out_w * out_h * 4, b'\x00')
|
||||
rs_snapshot_pixels(x, y, w, h, out_w, out_h, out)
|
||||
out
|
||||
}
|
||||
|
||||
///| The framebuffer as a pixmap.
|
||||
fn frame_pixmap() -> @renderer.Pixmap {
|
||||
let (width, height) = (rs_width(), rs_height())
|
||||
let data = FixedArray::make(width * height * 4, b'\x00')
|
||||
rs_read_frame(data)
|
||||
{ width, height, data }
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- geometry
|
||||
|
||||
///|
|
||||
@@ -764,11 +817,70 @@ fn raster_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
|
||||
(
|
||||
"read-frame",
|
||||
_ => {
|
||||
let out = FixedArray::make(rs_width() * rs_height() * 4, b'\x00')
|
||||
rs_read_frame(out)
|
||||
[@wt.Val::bytes(Bytes::from_fixedarray(out))]
|
||||
[@wt.Val::bytes(Bytes::from_fixedarray(frame_pixmap().data))]
|
||||
},
|
||||
),
|
||||
("export-memfd", _ => s32(rs_export_memfd())),
|
||||
(
|
||||
"write-frame-png",
|
||||
a => {
|
||||
let p = frame_pixmap()
|
||||
let ok = p.width > 0 &&
|
||||
@sys.write_file_bytes(a[0].to_string(), @renderer.encode_png(p)) == 0
|
||||
[Bool(ok)]
|
||||
},
|
||||
),
|
||||
(
|
||||
"grab-recording-frame",
|
||||
a => {
|
||||
let n = rs_grab_recording(
|
||||
a[0].to_int(),
|
||||
a[1].to_int(),
|
||||
a[2].to_int(),
|
||||
a[3].to_int(),
|
||||
if a[4].to_bool() { 1 } else { 0 },
|
||||
a[5].to_int(),
|
||||
a[6].to_int(),
|
||||
)
|
||||
[Bool(n != 0)]
|
||||
},
|
||||
),
|
||||
("send-recording-frame", a => s32(rs_send_recording(a[0].to_int()))),
|
||||
("snapshot-frame", _ => [Bool(rs_snapshot() != 0)]),
|
||||
(
|
||||
"snapshot-png",
|
||||
a => {
|
||||
// Clipped to the snapshot; a 1:1 copy is a scale by one.
|
||||
let (sw, sh) = (rs_snapshot_w(), rs_snapshot_h())
|
||||
let x0 = a[0].to_int().max(0)
|
||||
let y0 = a[1].to_int().max(0)
|
||||
let x1 = (a[0].to_int() + a[2].to_int()).min(sw)
|
||||
let y1 = (a[1].to_int() + a[3].to_int()).min(sh)
|
||||
if x1 <= x0 || y1 <= y0 {
|
||||
return [@wt.Val::bytes(b"")]
|
||||
}
|
||||
let (w, h) = (x1 - x0, y1 - y0)
|
||||
let p : @renderer.Pixmap = {
|
||||
width: w,
|
||||
height: h,
|
||||
data: snapshot_pixels(x0, y0, w, h, w, h),
|
||||
}
|
||||
[@wt.Val::bytes(@renderer.encode_png_compressed(p))]
|
||||
},
|
||||
),
|
||||
(
|
||||
"snapshot-pixels",
|
||||
a => {
|
||||
let px = snapshot_pixels(
|
||||
a[0].to_int(),
|
||||
a[1].to_int(),
|
||||
a[2].to_int(),
|
||||
a[3].to_int(),
|
||||
a[4].to_int(),
|
||||
a[5].to_int(),
|
||||
)
|
||||
[@wt.Val::bytes(Bytes::from_fixedarray(px))]
|
||||
},
|
||||
),
|
||||
]
|
||||
}
|
||||
@@ -8,12 +8,15 @@
|
||||
* Glyphs come from stb_truetype through compositor/font's shim (mp_font_*); SVG icons from
|
||||
* nanosvg (third_party/nanosvg). */
|
||||
#define _GNU_SOURCE
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include "moonbit.h"
|
||||
@@ -605,3 +608,90 @@ int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h);
|
||||
void rs_present_kms(void) {
|
||||
if (fb) mp_kms_present((moonbit_bytes_t)fb, fb_w, fb_h);
|
||||
}
|
||||
|
||||
/* ---- screen recording: the frame in flight stays here ---- */
|
||||
|
||||
static uint8_t *rec_px;
|
||||
static size_t rec_len, rec_sent;
|
||||
|
||||
static void rec_fill(int32_t w, int32_t h, int32_t x, int32_t y, int32_t rw, int32_t rh, uint8_t v) {
|
||||
for (int32_t j = imax(y, 0); j < imin(y + rh, h); j++)
|
||||
for (int32_t i = imax(x, 0); i < imin(x + rw, w); i++) {
|
||||
uint8_t *p = rec_px + ((size_t)j * w + i) * 4;
|
||||
p[0] = p[1] = p[2] = v; p[3] = 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
/* The arrow pointer with its tip at (x, y), as recorder.mbt's draw_pointer draws it: a black body
|
||||
* widening over 13 rows and a short tail, then the white inside. */
|
||||
static void rec_pointer(int32_t w, int32_t h, int32_t x, int32_t y) {
|
||||
for (int i = 0; i < 17; i++) {
|
||||
int l = i < 13 ? 0 : 3 + (i - 13), bw = i < 13 ? i * 2 / 3 + 1 : 3;
|
||||
rec_fill(w, h, x + l - 1, y + i - 1, bw + 2, 3, 0);
|
||||
}
|
||||
for (int i = 1; i < 16; i++) {
|
||||
int l = i < 13 ? 0 : 4 + (i - 13), bw = i < 13 ? i * 2 / 3 - 1 : 1;
|
||||
if (bw > 0) rec_fill(w, h, x + l + 1, y + i, bw, 1, 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t rs_grab_recording(int32_t x, int32_t y, int32_t w, int32_t h, int32_t pointer, int32_t px, int32_t py) {
|
||||
if (!fb || w <= 0 || h <= 0 || x < 0 || y < 0 || x + w > fb_w || y + h > fb_h) return 0;
|
||||
size_t n = (size_t)w * h * 4;
|
||||
if (n != rec_len) { free(rec_px); rec_px = malloc(n); rec_len = n; }
|
||||
for (int32_t j = 0; j < h; j++) memcpy(rec_px + (size_t)j * w * 4, fb + ((size_t)(y + j) * fb_w + x) * 4, (size_t)w * 4);
|
||||
for (size_t i = 3; i < n; i += 4) rec_px[i] = 0xff;
|
||||
if (pointer) rec_pointer(w, h, px - x, py - y);
|
||||
rec_sent = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int32_t rs_send_recording(int32_t fd) {
|
||||
if (!rec_px) return 0;
|
||||
int fl = fcntl(fd, F_GETFL);
|
||||
if (fl >= 0 && !(fl & O_NONBLOCK)) fcntl(fd, F_SETFL, fl | O_NONBLOCK);
|
||||
while (rec_sent < rec_len) {
|
||||
ssize_t w = send(fd, rec_px + rec_sent, rec_len - rec_sent, MSG_NOSIGNAL);
|
||||
if (w < 0 && errno == ENOTSOCK) w = write(fd, rec_px + rec_sent, rec_len - rec_sent);
|
||||
if (w < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
|
||||
return -1;
|
||||
}
|
||||
rec_sent += (size_t)w;
|
||||
}
|
||||
size_t left = rec_len - rec_sent;
|
||||
return left > 0x7fffffff ? 0x7fffffff : (int32_t)left;
|
||||
}
|
||||
|
||||
/* ---- screenshots: the frozen screen stays here ---- */
|
||||
|
||||
static uint8_t *snap;
|
||||
static int32_t snap_w, snap_h;
|
||||
|
||||
int32_t rs_snapshot(void) {
|
||||
if (!fb) return 0;
|
||||
size_t n = (size_t)fb_w * fb_h * 4;
|
||||
if (!snap || snap_w != fb_w || snap_h != fb_h) { free(snap); snap = malloc(n); snap_w = fb_w; snap_h = fb_h; }
|
||||
memcpy(snap, fb, n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int32_t rs_snapshot_w(void) { return snap ? snap_w : 0; }
|
||||
int32_t rs_snapshot_h(void) { return snap ? snap_h : 0; }
|
||||
|
||||
/* `x`, `y`, `w` x `h` of the snapshot scaled (nearest) to `ow` x `oh` into `out` (ow * oh * 4
|
||||
* bytes), opaque; off the snapshot is black. */
|
||||
void rs_snapshot_pixels(int32_t x, int32_t y, int32_t w, int32_t h, int32_t ow, int32_t oh, moonbit_bytes_t out) {
|
||||
if ((size_t)Moonbit_array_length(out) < (size_t)ow * oh * 4 || ow <= 0 || oh <= 0) return;
|
||||
for (int32_t ty = 0; ty < oh; ty++) {
|
||||
int32_t sy = y + (int32_t)((int64_t)ty * h / oh);
|
||||
for (int32_t tx = 0; tx < ow; tx++) {
|
||||
int32_t sx = x + (int32_t)((int64_t)tx * w / ow);
|
||||
uint8_t *d = out + ((size_t)ty * ow + tx) * 4;
|
||||
if (snap && sx >= 0 && sy >= 0 && sx < snap_w && sy < snap_h) memcpy(d, snap + ((size_t)sy * snap_w + sx) * 4, 3);
|
||||
else d[0] = d[1] = d[2] = 0;
|
||||
d[3] = 0xff;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user