Files
chandra/wasmhost/bin/main.mbt
T
nandiandClaude Opus 5.5 cfb25fe699 Draw on the GPU: the desktop with OpenGL ES, apps through linux-dmabuf
The host's rasteriser has a GPU half (raster_gpu.c): the same primitives, drawn the same way, on
machines with a real GPU and OpenGL ES 3, straight into the display's buffers in a login session.
EGL and GLES are loaded at run time; without them everything is drawn on the CPU as before
(CHANDRA_GPU=0 does that too). zwp_linux_dmabuf_v1 lets apps draw on the GPU as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 16:42:22 -08:00

111 lines
2.9 KiB
MoonBit

///| The MoonBit host for the compositor component (build/compositor.wasm).
///
/// It runs the component with wasmtime, linked as the prebuilt C API library, and implements the
/// interfaces the component imports (wit/host): sys, font, raster, display and plasmoids. Most of
/// it is the native compositor's own packages (compositor/sys, font, kms, wasm, renderer), so the
/// native and wasm builds share one implementation of each.
///
/// wasmhost build/compositor.wasm --socket PATH [--window | --drm | --gfx-out PNG [--serve]]
///|
#borrow(s)
extern "C" fn c_eprint(s : Bytes) = "wh_eprint"
///|
extern "C" fn c_ignore_sigchld() = "wh_ignore_sigchld"
///|
extern "C" fn c_exit(code : Int) = "wh_exit"
///|
fn exit(code : Int) -> Unit {
c_exit(code)
}
///|
fn eprint(s : String) -> Unit {
c_eprint(@utf8.encode(s))
}
///| Every host function, by interface, in registration order (its id is its index).
let handlers : Array[(Array[@wt.Val]) -> Array[@wt.Val]] = []
///| "interface.function" of each handler, for WASMHOST_TRACE.
let handler_names : Array[String] = []
///|
fn register(
iface : String,
funcs : Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])],
) -> Unit {
let first = handlers.length()
for f in funcs {
handlers.push(f.1)
handler_names.push(iface + "." + f.0)
}
match @wt.define_funcs(iface, funcs.map(f => f.0), first) {
Ok(_) => ()
Err(e) => {
eprint("error: " + e)
exit(1)
}
}
}
///|
fn main {
let argv = @env.args()
let args : Array[String] = if argv.length() > 1 {
argv[1:].to_array()
} else {
[]
}
// First argument may be the component path; otherwise use the default build location.
let path = if args.length() > 0 && args[0].has_suffix(".wasm") {
args.remove(0)
} else {
"build/compositor.wasm"
}
if args.length() > 0 && args[0] == "--" {
args.remove(0) |> ignore
}
c_ignore_sigchld()
ignore(rs_gpu_init(if args.contains("--drm") { 1 } else { 0 }))
match @wt.load(path) {
Ok(_) => ()
Err(e) => {
eprint(
"error: cannot load \{path}: \{e}\n build it with `just compositor-wasm`",
)
exit(1)
}
}
if @sys.getenv("WASMHOST_TRACE") != "" {
@wt.set_handler(fn(id, a) {
eprint("> " + handler_names[id] + " " + a.length().to_string())
handlers[id](a)
})
} else {
@wt.set_handler(fn(id, a) { handlers[id](a) })
}
register("chandra:host/sys@0.1.0", sys_funcs())
register("chandra:host/font@0.1.0", font_funcs())
register("chandra:host/raster@0.1.0", raster_funcs())
register("chandra:host/display@0.1.0", display_funcs())
register("chandra:host/plasmoids@0.1.0", plasmoid_funcs())
match @wt.instantiate() {
Ok(_) => ()
Err(e) => {
eprint("error: " + e)
exit(1)
}
}
match @wt.call("", "run", [@wt.Val::strings(args)]) {
Ok(r) => exit(if r.length() > 0 { r[0].to_int() } else { 0 })
Err(e) => {
eprint("error: " + e)
exit(1)
}
}
}