Draw in the host: the wasm compositor sends draw ops, the host rasterises natively
CI / test (push) Failing after 29s
CI / test (push) Failing after 29s
The wasm software renderer cost ~10-20 ns/pixel, so any changed frame took ~100+ ms at 1080p and the desktop swung between idle and very slow. The compositor still builds the scene (windows, decorations, panel, wallpaper) but a new moonplasma:host/raster interface turns it into pixels in the host: - host-wasm/src/raster.rs: a port of compositor/renderer (same blend arithmetic: fills, strokes, lines, circles, text, clips, shadows, surface blits), a wallpaper+desktop-card cache, and captured client surfaces (copied out of the shared memory once at commit, converted to RGBA, solid ones blit as row copies). 9 unit tests; a full frame matches the native software renderer except for text antialiasing (0.3% of pixels). - compositor/raster sends a SceneGraph as ops; compositor.compose_scene() returns it only when something changed. The login session shows the host framebuffer directly (display.present-frame), the nested window gets it as a memfd (raster.export-memfd), --gfx-out reads it (read-frame). - Client buffers never enter wasm: snapshot_shm_buffer captures in the host and keeps an id. - Loop pacing sleeps the rest of the frame budget instead of a fixed 16/8 ms. Full-screen terminal flooding output at 1080p: ~2 fps / 94% CPU -> ~57 fps; idle ~9%. The native build keeps the software renderer. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
66bf293085
commit
c6e31b4a8f
38 files changed
+2141
-220
No files matched your search
+72
-41
@@ -16,6 +16,7 @@ use wasmtime::component::{Component, HasSelf, Linker};
|
||||
use wasmtime::{Config, Engine, Result, Store};
|
||||
|
||||
mod plasmoid;
|
||||
mod raster;
|
||||
#[cfg(feature = "drm")]
|
||||
mod kms;
|
||||
|
||||
@@ -24,14 +25,14 @@ wasmtime::component::bindgen!({
|
||||
world: "compositor",
|
||||
// The desktop value types are the same WIT the modules use: share the Rust types, so draw ops
|
||||
// flow from a module straight to the compositor without conversion.
|
||||
// Draw operations are compared to reuse the cached wallpaper.
|
||||
additional_derives: [PartialEq],
|
||||
with: {
|
||||
"moonplasma:desktop/types@0.1.0": plasmoid::ph::moonplasma::desktop::types,
|
||||
"moonplasma:desktop/theme@0.1.0": plasmoid::ph::moonplasma::desktop::theme,
|
||||
},
|
||||
});
|
||||
|
||||
use ab_glyph::{Font, FontVec, OutlineCurve};
|
||||
use ab_glyph_rasterizer::{point, Rasterizer};
|
||||
use moonplasma::host::font::Glyph;
|
||||
use moonplasma::host::sys::{Host, Received};
|
||||
|
||||
@@ -41,8 +42,10 @@ struct State {
|
||||
private_bus: Option<String>,
|
||||
bus_tried: bool,
|
||||
started: Option<std::time::Instant>,
|
||||
/// TTF faces loaded by the component (`font.load`), by slot.
|
||||
fonts: [Option<FontVec>; 4],
|
||||
/// TTF faces loaded by the component (`font.load`), by slot, and their glyph cache.
|
||||
fonts: raster::Fonts,
|
||||
/// The framebuffer and client surfaces behind `moonplasma:host/raster`.
|
||||
raster: raster::Raster,
|
||||
/// Runs the panel modules; set once the engine exists.
|
||||
pl: Option<plasmoid::Runtime>,
|
||||
}
|
||||
@@ -254,6 +257,10 @@ impl moonplasma::host::display::Host for State {
|
||||
#[cfg(not(feature = "drm"))]
|
||||
false
|
||||
}
|
||||
fn present_frame(&mut self) {
|
||||
#[cfg(feature = "drm")]
|
||||
kms::present(&self.raster.fb, self.raster.w as u32, self.raster.h as u32);
|
||||
}
|
||||
fn present(&mut self, pixels: Vec<u8>, width: u32, height: u32) {
|
||||
#[cfg(feature = "drm")]
|
||||
kms::present(&pixels, width, height);
|
||||
@@ -280,50 +287,73 @@ impl moonplasma::host::display::Host for State {
|
||||
|
||||
impl moonplasma::host::font::Host for State {
|
||||
fn load(&mut self, slot: u32, path: String) -> bool {
|
||||
if slot as usize >= self.fonts.len() {
|
||||
return false;
|
||||
self.fonts.load(slot, &path)
|
||||
}
|
||||
|
||||
fn rasterise(&mut self, slot: u32, codepoint: u32, size: u32) -> Glyph {
|
||||
let g = self.fonts.glyph(slot, codepoint, size);
|
||||
Glyph { width: g.w, height: g.h, xoff: g.xoff, yoff: g.yoff, advance: g.advance, coverage: g.coverage.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
impl moonplasma::host::raster::Host for State {
|
||||
fn capture(&mut self, fd: i32, offset: i32, stride: i32, width: i32, height: i32, format: i32) -> u32 {
|
||||
if fd < 0 || offset < 0 || stride <= 0 || width <= 0 || height <= 0 {
|
||||
return 0;
|
||||
}
|
||||
match std::fs::read(path).ok().and_then(|d| FontVec::try_from_vec(d).ok()) {
|
||||
Some(f) => {
|
||||
self.fonts[slot as usize] = Some(f);
|
||||
true
|
||||
let len = stride as i64 * height as i64;
|
||||
let end = offset as i64 + len;
|
||||
unsafe {
|
||||
let mut st: libc::stat = std::mem::zeroed();
|
||||
if libc::fstat(fd, &mut st) < 0 || (st.st_size as i64) < end {
|
||||
return 0;
|
||||
}
|
||||
// Mapping a large pool only reserves address space; just the pages copied are touched.
|
||||
let p = libc::mmap(std::ptr::null_mut(), end as usize, libc::PROT_READ, libc::MAP_SHARED, fd, 0);
|
||||
if p == libc::MAP_FAILED {
|
||||
return 0;
|
||||
}
|
||||
let src = std::slice::from_raw_parts((p as *const u8).add(offset as usize), len as usize);
|
||||
let surface = raster::Surface::from_bgra(src, stride as usize, width, height, format == 1);
|
||||
libc::munmap(p, end as usize);
|
||||
match surface {
|
||||
Some(s) => self.raster.add_surface(s),
|
||||
None => 0,
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Same maths as stb_truetype in the native build: the size is an em size in pixels, the glyph
|
||||
/// origin sits on the integer pixel grid, and the bitmap box is the outline's floor/ceil bounds.
|
||||
fn rasterise(&mut self, slot: u32, codepoint: u32, size: u32) -> Glyph {
|
||||
let blank = Glyph { width: 0, height: 0, xoff: 0, yoff: 0, advance: 0, coverage: Vec::new() };
|
||||
let Some(Some(font)) = self.fonts.get(slot as usize) else { return blank };
|
||||
let ch = char::from_u32(codepoint).unwrap_or('\u{fffd}');
|
||||
let id = font.glyph_id(ch);
|
||||
let s = size as f32 / font.units_per_em().unwrap_or(1000.0);
|
||||
let advance = (font.h_advance_unscaled(id) * s + 0.5) as i32;
|
||||
let Some(outline) = font.outline(id) else { return Glyph { advance, ..blank } };
|
||||
// Font units grow upward, pixels downward; ab_glyph stores the outline's top edge as
|
||||
// `min.y` and its bottom edge as `max.y`, both in font units.
|
||||
let x0 = (outline.bounds.min.x * s).floor() as i32;
|
||||
let x1 = (outline.bounds.max.x * s).ceil() as i32;
|
||||
let y0 = (-outline.bounds.min.y * s).floor() as i32;
|
||||
let y1 = (-outline.bounds.max.y * s).ceil() as i32;
|
||||
let (w, h) = (x1 - x0, y1 - y0);
|
||||
if w <= 0 || h <= 0 {
|
||||
return Glyph { advance, ..blank };
|
||||
}
|
||||
let tp = |p: ab_glyph::Point| point(p.x * s - x0 as f32, -p.y * s - y0 as f32);
|
||||
let mut r = Rasterizer::new(w as usize, h as usize);
|
||||
for c in &outline.curves {
|
||||
match c {
|
||||
OutlineCurve::Line(a, b) => r.draw_line(tp(*a), tp(*b)),
|
||||
OutlineCurve::Quad(a, b, c) => r.draw_quad(tp(*a), tp(*b), tp(*c)),
|
||||
OutlineCurve::Cubic(a, b, c, d) => r.draw_cubic(tp(*a), tp(*b), tp(*c), tp(*d)),
|
||||
fn release(&mut self, id: u32) {
|
||||
self.raster.release(id);
|
||||
}
|
||||
|
||||
fn submit(&mut self, f: moonplasma::host::raster::Frame) {
|
||||
self.raster.submit(&f, &mut self.fonts);
|
||||
}
|
||||
|
||||
fn read_frame(&mut self) -> Vec<u8> {
|
||||
self.raster.fb.clone()
|
||||
}
|
||||
|
||||
fn export_memfd(&mut self) -> i32 {
|
||||
let data = self.raster.to_argb8888();
|
||||
let Ok(name) = CString::new("moon-plasma-frame") else { return -1 };
|
||||
unsafe {
|
||||
let fd = libc::memfd_create(name.as_ptr(), libc::MFD_CLOEXEC);
|
||||
if fd < 0 {
|
||||
return -1;
|
||||
}
|
||||
let mut done = 0usize;
|
||||
while done < data.len() {
|
||||
let n = libc::write(fd, data[done..].as_ptr() as *const _, data.len() - done);
|
||||
if n <= 0 {
|
||||
libc::close(fd);
|
||||
return -1;
|
||||
}
|
||||
done += n as usize;
|
||||
}
|
||||
fd
|
||||
}
|
||||
let mut coverage = vec![0u8; (w * h) as usize];
|
||||
r.for_each_pixel(|i, a| coverage[i] = (a.clamp(0.0, 1.0) * 255.0 + 0.5) as u8);
|
||||
Glyph { width: w, height: h, xoff: x0, yoff: y0, advance, coverage }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -686,6 +716,7 @@ fn main() -> Result<()> {
|
||||
let mut linker: Linker<State> = Linker::new(&engine);
|
||||
moonplasma::host::sys::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
|
||||
moonplasma::host::font::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
|
||||
moonplasma::host::raster::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
|
||||
moonplasma::host::display::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
|
||||
moonplasma::host::plasmoids::add_to_linker::<State, HasSelf<State>>(&mut linker, |s| s)?;
|
||||
let mut store = Store::new(&engine, State::default());
|
||||
|
||||
Reference in new issue
Block a user