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>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-30 16:42:22 -08:00
1 parent d81003f176
commit cfb25fe699
26 files changed
+2485 -105

No files matched your search

+38 -22
View File
@@ -6,6 +6,18 @@ fn wasmImportCapture(p0 : Int, p1 : Int, p2 : Int, p3 : Int, p4 : Int, p5 : Int)
///|
fn wasmImportRelease(p0 : Int) = "chandra:host/raster@0.1.0" "release"
///|
fn wasmImportDmabufs(p0 : Int) = "chandra:host/raster@0.1.0" "dmabufs"
///|
fn wasmImportImportDmabuf(p0 : Int, p1 : Int, p2 : Int64, p3 : Int, p4 : Int, p5 : Int, p6 : Int) -> Int = "chandra:host/raster@0.1.0" "import-dmabuf"
///|
fn wasmImportCaptureDmabuf(p0 : Int) -> Int = "chandra:host/raster@0.1.0" "capture-dmabuf"
///|
fn wasmImportReleaseDmabuf(p0 : Int) = "chandra:host/raster@0.1.0" "release-dmabuf"
///|
fn wasmImportLoadImage(p0 : Int, p1 : Int) -> Int = "chandra:host/raster@0.1.0" "load-image"
@@ -51,33 +63,19 @@ fn wasmImportSnapshotPng(p0 : Int, p1 : Int, p2 : Int, p3 : Int, p4 : Int) = "c
///|
fn wasmImportSnapshotPixels(p0 : Int, p1 : Int, p2 : Int, p3 : Int, p4 : Int, p5 : Int, p6 : Int) = "chandra:host/raster@0.1.0" "snapshot-pixels"
///|
extern "wasm" fn mbt_ffi_free(position : Int) =
#|(func (param i32) local.get 0 call $moonbit.decref)
///|
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
#|(func (param i32) (param i32) (result i32)
#| local.get 0
#| local.get 1 call $moonbit.init_array16
#| local.get 0)
///|
#owned(bytes)
extern "wasm" fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int =
#|(func (param i32) (result i32) local.get 0)
///|
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
extern "wasm" fn mbt_ffi_load64(offset : Int) -> Int64 =
#|(func (param i32) (result i64) local.get 0 i64.load)
///|
extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) =
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
///|
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) =
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
#owned(str)
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0)
///|
extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
@@ -86,14 +84,25 @@ extern "wasm" fn mbt_ffi_malloc(size : Int) -> Int =
#| local.tee 1 i32.const 0 call $moonbit.init_array8
#| local.get 1)
///|
extern "wasm" fn mbt_ffi_store8(offset : Int, value : Int) =
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store8)
///|
extern "wasm" fn mbt_ffi_free(position : Int) =
#|(func (param i32) local.get 0 call $moonbit.decref)
///|
extern "wasm" fn mbt_ffi_load32(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load)
///|
#owned(str)
extern "wasm" fn mbt_ffi_str2ptr(str : String) -> Int =
#|(func (param i32) (result i32) local.get 0)
extern "wasm" fn mbt_ffi_store32(offset : Int, value : Int) =
#|(func (param i32) (param i32) local.get 0 local.get 1 i32.store)
///|
extern "wasm" fn mbt_ffi_load8_u(offset : Int) -> Int =
#|(func (param i32) (result i32) local.get 0 i32.load8_u)
///|
extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
@@ -102,3 +111,10 @@ extern "wasm" fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] =
#| local.get 1 call $moonbit.init_array8
#| local.get 0)
///|
extern "wasm" fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String =
#|(func (param i32) (param i32) (result i32)
#| local.get 0
#| local.get 1 call $moonbit.init_array16
#| local.get 0)
+56 -31
View File
@@ -10,6 +10,26 @@ fn wasmImportRelease(p0 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportDmabufs(p0 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportImportDmabuf(p0 : Int, p1 : Int, p2 : Int64, p3 : Int, p4 : Int, p5 : Int, p6 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportCaptureDmabuf(p0 : Int) -> Int {
abort("wasm-only host import")
}
///|
fn wasmImportReleaseDmabuf(p0 : Int) -> Unit {
abort("wasm-only host import")
}
///|
fn wasmImportLoadImage(p0 : Int, p1 : Int) -> Int {
abort("wasm-only host import")
@@ -85,43 +105,13 @@ fn wasmImportSnapshotPixels(p0 : Int, p1 : Int, p2 : Int, p3 : Int, p4 : Int, p5
abort("wasm-only host import")
}
///|
fn mbt_ffi_free(position : Int) -> Unit {
abort("wasm-only")
}
///|
fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String {
abort("wasm-only")
}
///|
fn mbt_ffi_bytes2ptr(bytes : FixedArray[Byte]) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_load8_u(offset : Int) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_store8(offset : Int, value : Int) -> Unit {
abort("wasm-only")
}
///|
fn mbt_ffi_store32(offset : Int, value : Int) -> Unit {
abort("wasm-only")
}
///|
fn mbt_ffi_malloc(size : Int) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_load32(offset : Int) -> Int {
fn mbt_ffi_load64(offset : Int) -> Int64 {
abort("wasm-only")
}
@@ -130,7 +120,42 @@ fn mbt_ffi_str2ptr(str : String) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_malloc(size : Int) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_store8(offset : Int, value : Int) -> Unit {
abort("wasm-only")
}
///|
fn mbt_ffi_free(position : Int) -> Unit {
abort("wasm-only")
}
///|
fn mbt_ffi_load32(offset : Int) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_store32(offset : Int, value : Int) -> Unit {
abort("wasm-only")
}
///|
fn mbt_ffi_load8_u(offset : Int) -> Int {
abort("wasm-only")
}
///|
fn mbt_ffi_ptr2bytes(ptr : Int, len : Int) -> FixedArray[Byte] {
abort("wasm-only")
}
///|
fn mbt_ffi_ptr2str(ptr : Int, len : Int) -> String {
abort("wasm-only")
}
@@ -4,6 +4,10 @@ package "chandra/desktop/interface/chandra/host/raster"
// Values
pub fn capture(Int32, Int32, Int32, Int32, Int32, Int32) -> UInt32
pub fn capture_dmabuf(UInt32) -> UInt32
pub fn dmabufs() -> DmabufSupport
pub fn emoji_font() -> Result[String, String]
pub fn emoji_has(Array[String]) -> Array[Bool]
@@ -16,12 +20,16 @@ pub fn grab_recording_frame(Int32, Int32, Int32, Int32, Bool, Int32, Int32) -> B
pub fn image_pixels(Int32, Int32, FixedArray[Byte], UInt32) -> UInt32
pub fn import_dmabuf(Array[DmabufPlane], UInt64, Int32, Int32, UInt32, Bool) -> UInt32
pub fn load_image(String) -> UInt32
pub fn read_frame() -> FixedArray[Byte]
pub fn release(UInt32) -> Unit
pub fn release_dmabuf(UInt32) -> Unit
pub fn send_recording_frame(Int32) -> Int32
pub fn snapshot_frame() -> Bool
@@ -54,6 +62,22 @@ pub(all) struct Disc {
filled : Bool
}
pub(all) struct DmabufFormat {
fourcc : UInt32
modifier : UInt64
}
pub(all) struct DmabufPlane {
fd : Int32
offset : UInt32
stride : UInt32
}
pub(all) struct DmabufSupport {
device : UInt64
formats : Array[DmabufFormat]
}
pub(all) struct Fill {
area : Rect
color : Color
+69
View File
@@ -58,6 +58,22 @@ pub(all) struct Frame {
width : Int; height : Int; wallpaper : Array[Op]?; background : Array[Op]; windows : Array[Window]; panel : Array[Op]; popups : Array[Popup]
}
///|
/// Client buffers that live on the GPU (linux-dmabuf). A pixel format (DRM fourcc) and layout
/// (DRM modifier) the GPU reads.
pub(all) struct DmabufFormat {
fourcc : UInt; modifier : UInt64
}
///|
/// `device`: the `dev_t` of the GPU clients should draw on. No formats: the host draws on the
/// CPU and takes no such buffers.
pub(all) struct DmabufSupport {
device : UInt64; formats : Array[DmabufFormat]
}
///|
pub(all) struct DmabufPlane {
fd : Int; offset : UInt; stride : UInt
}
///|
/// Copy a client buffer (`stride * height` bytes at `offset` of the file behind `fd`) into a
/// host surface. `format` is the wl_shm format (0 ARGB8888, 1 XRGB8888). 0 on failure.
pub fn capture(fd : Int, offset : Int, stride : Int, width : Int, height : Int, format : Int) -> UInt {
@@ -72,6 +88,59 @@ pub fn release(id : UInt) -> Unit {
wasmImportRelease((id).reinterpret_as_int());
}
///|
pub fn dmabufs() -> DmabufSupport {
let return_area = mbt_ffi_malloc(16)
wasmImportDmabufs(return_area);
let array : Array[DmabufFormat] = [];
for index = 0; index < (mbt_ffi_load32((return_area) + 12)); index = index + 1 {
let iter_base = (mbt_ffi_load32((return_area) + 8)) + (index * 16)
array.push(DmabufFormat::{fourcc : (mbt_ffi_load32((iter_base) + 0)).reinterpret_as_uint(), modifier : (mbt_ffi_load64((iter_base) + 8)).reinterpret_as_uint64()})
}
mbt_ffi_free(mbt_ffi_load32((return_area) + 8))
let ret = DmabufSupport::{device : (mbt_ffi_load64((return_area) + 0)).reinterpret_as_uint64(), formats : array}
mbt_ffi_free(return_area)
return ret
}
///|
/// Import a buffer (the fds stay the caller's to close). 0 when the GPU can't read it.
pub fn import_dmabuf(planes : Array[DmabufPlane], modifier : UInt64, width : Int, height : Int, fourcc : UInt, y_invert : Bool) -> UInt {
let address = mbt_ffi_malloc((planes).length() * 12);
for index = 0; index < (planes).length(); index = index + 1 {
let iter_elem : DmabufPlane = (planes)[(index)]
let iter_base = address + (index * 12);
mbt_ffi_store32((iter_base) + 0, (iter_elem).fd)
mbt_ffi_store32((iter_base) + 4, ((iter_elem).offset).reinterpret_as_int())
mbt_ffi_store32((iter_base) + 8, ((iter_elem).stride).reinterpret_as_int())
}
let result : Int = wasmImportImportDmabuf(address, (planes).length(), (modifier).reinterpret_as_int64(), width, height, (fourcc).reinterpret_as_int(), (if y_invert { 1 } else { 0 }));
let ret = (result).reinterpret_as_uint()
mbt_ffi_free(address)
return ret
}
///|
/// What the buffer holds now as a surface, like `capture` (a copy on the GPU: the client may
/// draw into its buffer again at once). 0 on failure.
pub fn capture_dmabuf(id : UInt) -> UInt {
let result : Int = wasmImportCaptureDmabuf((id).reinterpret_as_int());
let ret = (result).reinterpret_as_uint()
return ret
}
///|
pub fn release_dmabuf(id : UInt) -> Unit {
wasmImportReleaseDmabuf((id).reinterpret_as_int());
}
///|
/// Decode the PNG or SVG file at `path` into a host image (an app icon), for `draw-image`.