Draw in the host: the wasm compositor sends draw ops, the host rasterises natively
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:
nandiandClaude Sonnet 5.5 committed 2026-11-25 04:39:40 -08:00
1 parent 66bf293085
commit c6e31b4a8f
38 files changed
+2141 -220

No files matched your search

+59 -11
View File
@@ -129,10 +129,21 @@ pub fn MoonPlasmaCompositor::new(
///|
pub fn MoonPlasmaCompositor::close(self : MoonPlasmaCompositor) -> Unit {
for _, win in self.windows {
release_snapshot(win.surface)
}
self.shell.shutdown()
self.server.close()
}
///| Let the host forget a client surface it captured (a no-op without the host rasteriser).
fn release_snapshot(s : @scene.ShmBufferSnapshot?) -> Unit {
match s {
Some(snap) => if snap.host_id != 0U { @sys.surface_release(snap.host_id) }
None => ()
}
}
///| What a frame was drawn from, compared with the next frame's to see whether it changed.
#derive(Eq)
pub(all) struct FrameSig {
@@ -284,6 +295,7 @@ pub fn MoonPlasmaCompositor::step(self : MoonPlasmaCompositor) -> Unit {
Some(win) => {
match snap {
Some(s) => {
release_snapshot(win.surface)
win.surface = Some(s)
self.damage.mark_rect(win.geometry)
}
@@ -300,7 +312,10 @@ pub fn MoonPlasmaCompositor::step(self : MoonPlasmaCompositor) -> Unit {
for p in self.popups {
if p.id == wid {
match snap {
Some(_) => p.surface = snap
Some(_) => {
release_snapshot(p.surface)
p.surface = snap
}
None => ()
}
self.damage.mark_full()
@@ -309,6 +324,10 @@ pub fn MoonPlasmaCompositor::step(self : MoonPlasmaCompositor) -> Unit {
@wayland.CompositorEvent::PopupClosed(wid) => self.close_popup(wid)
@wayland.CompositorEvent::ToplevelClosed(wid) => {
self.close_popup(wid)
match self.windows.get(wid) {
Some(win) => release_snapshot(win.surface)
None => ()
}
ignore(self.windows.remove(wid))
if self.ptr_win == Some(wid) {
self.ptr_win = None
@@ -379,9 +398,9 @@ pub fn MoonPlasmaCompositor::restore_state(self : MoonPlasmaCompositor) -> Unit
}
///|
pub fn MoonPlasmaCompositor::compose_frame(
fn MoonPlasmaCompositor::build_scene(
self : MoonPlasmaCompositor,
) -> @renderer.Pixmap {
) -> (@scene.SceneGraph, Bool) {
self.flush_configures()
let scene = @scene.SceneGraph::new(self.output_w, self.output_h)
let order = self.wm.stacking_order()
@@ -482,22 +501,46 @@ pub fn MoonPlasmaCompositor::compose_frame(
wins: scene.windows.map(w => (w.id, w.geometry, w.focused, w.fullscreen, w.deco_ops)),
popups: scene.popups.map(p => p.0),
}
// Nothing changed since the last frame (no client commit, same shell and decoration ops):
// hand back the previous pixels instead of rasterising 2 million pixels again.
match (self.last_frame, self.last_sig) {
(Some(prev), Some(ps)) =>
if !damaged && ps == sig {
// Unchanged since the last scene (no client commit, same shell and decoration ops)?
let unchanged = match self.last_sig {
Some(ps) => !damaged && ps == sig
None => false
}
if !unchanged {
self.last_sig = Some(sig)
}
self.prof_shell_ms = self.prof_shell_ms + (@sys.monotonic_ms() - t0)
(scene, !unchanged)
}
///| Compose the desktop for the host rasteriser (the wasm compositor): the scene to draw, or None
/// when nothing changed since the last one, so an idle desktop costs nothing to draw or present.
pub fn MoonPlasmaCompositor::compose_scene(
self : MoonPlasmaCompositor,
) -> @scene.SceneGraph? {
let (scene, changed) = self.build_scene()
self.frame_changed = changed
if changed { Some(scene) } else { None }
}
///| Compose the desktop and draw it with the software renderer (the native build and the tests).
/// An unchanged desktop hands back the previous pixels instead of rasterising them again.
pub fn MoonPlasmaCompositor::compose_frame(
self : MoonPlasmaCompositor,
) -> @renderer.Pixmap {
let (scene, changed) = self.build_scene()
match self.last_frame {
Some(prev) =>
if !changed {
self.frame_changed = false
return prev
}
_ => ()
None => ()
}
self.frame_changed = true
self.last_sig = Some(sig)
let t1 = @sys.monotonic_ms()
let px = @renderer.render_scene(scene)
self.last_frame = Some(px)
self.prof_shell_ms = self.prof_shell_ms + (t1 - t0)
self.prof_raster_ms = self.prof_raster_ms + (@sys.monotonic_ms() - t1)
px
}
@@ -638,6 +681,11 @@ fn MoonPlasmaCompositor::close_popup(self : MoonPlasmaCompositor, wid : UInt64)
return
}
let root = self.popup_root(wid)
for p in self.popups {
if p.id == wid {
release_snapshot(p.surface)
}
}
let keep = self.popups.filter(p => p.id != wid)
self.popups.clear()
for p in keep {
+32
View File
@@ -1030,3 +1030,35 @@ test "alt-tab switches to the previous window, tab again goes further back, esca
key(15U, true, 30U)
assert_false(comp.switcher_open())
}
///|
test "compose_scene hands the host a scene only when something changed" {
let socket_path = "/tmp/wayland-moon-scene-" + @sys.getpid().to_string() + ".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.MoonPlasmaCompositor::new(socket_path, theme, 1280, 720).unwrap()
// The first scene is always drawn; the wallpaper and the desktop card are split out of it.
match comp.compose_scene() {
Some(sg) => {
assert_true(sg.wallpaper_ops.length() > 0)
assert_true(sg.panel_ops.length() > 0)
}
None => abort("the first scene must be drawn")
}
// Nothing happened: nothing to draw.
assert_true(comp.compose_scene() is None)
assert_false(comp.frame_changed)
// A new window changes the scene, then it settles again.
ignore(open_and_configure_client(comp, socket_path, "A", "a"))
match comp.compose_scene() {
Some(sg) => assert_eq(sg.windows.length(), 1)
None => abort("a new window must change the scene")
}
assert_true(comp.compose_scene() is None)
// The switcher overlay is part of the scene too.
comp.handle_input(@nested.InputEvent::Key(56U, true), 1U)
ignore(open_and_configure_client(comp, socket_path, "B", "b"))
ignore(comp.compose_scene())
comp.handle_input(@nested.InputEvent::Key(15U, true), 2U)
assert_true(comp.switcher_open())
assert_true(comp.compose_scene() is Some(_))
}