CI / test (push) Failing after 19s
Measured on the MoonBit host, 1080p, cloud container (two runs each, before -> after): - full-screen (tiled) client redrawing at 60 fps: 43/39% -> 31/35% - 800x600 floating client at 60 fps: 15.5/13.2% -> 10.4/11.9% - client inside the nested window: nested 18% -> 12%, outer 17% -> 11.6% - Alt+Tab open over two animating clients: 36.6% -> 25.4% - idle (headless / nested): 0.2-0.6% -> 0.1-0.3% wasmhost raster: - capture keeps client pools mapped (keyed by dev/inode/size, dropped after 1 s unused) and reuses released surface buffers, swizzling a word at a time: 3.2 -> 2.1 ms per 1080p commit. - a window whose only change is a new buffer damages just its client area, and the topmost opaque buffer that covers the damage is drawn first (no backdrop copy, desktop card, lower windows or its own decoration under it): submit 3.2 -> 0.8 ms per full-screen frame. - the framebuffer tracks what changed since each output took it. The nested window's frames are a ring of three mapped memfds, each updated only where the screen changed, instead of a new memfd with a full 8 MB conversion and write every frame. - KMS: present copies only the rows the back buffer is missing (this frame's change and the last one's), and kms_stub.c is compiled with -O2 (its stub-cc-flags dropped moon's default, so the per-frame copy ran unoptimised). - images re-filled under the same handle (Alt+Tab previews) damage where the panel draws them: before, a live preview only repainted where it overlapped the window that redrew. compositor: - Alt+Tab previews of a window that keeps redrawing are remade at most every 100 ms (scaling a 1080p buffer takes ~2 ms); the loop keeps its pace while one is pending. - set-tokens is sent to the host only when the theme changes (it was 9 calls a frame). - /proc is sampled only when a module imports `system`; a machine without a battery looks for one every 30 s instead of probing eight sysfs paths every second. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hkh3qygSXtftCTVhSZ
974 lines
25 KiB
MoonBit
974 lines
25 KiB
MoonBit
///| `chandra:host/raster` and `chandra:host/font`: the compositor sends draw operations and window
|
|
/// surface ids; the pixels live here (raster_stub.c) and never cross the component boundary.
|
|
/// What to draw where (layers, the wallpaper cache, damage) is decided here, as in
|
|
/// host-wasm/src/raster.rs.
|
|
|
|
///|
|
|
extern "C" fn rs_resize(w : Int, h : Int) -> Int = "rs_resize"
|
|
|
|
///|
|
|
extern "C" fn rs_clear() = "rs_clear"
|
|
|
|
///|
|
|
extern "C" fn rs_set_clip(on : Int, x : Int, y : Int, w : Int, h : Int) = "rs_set_clip"
|
|
|
|
///|
|
|
extern "C" fn rs_fill_rect(
|
|
x : Int,
|
|
y : Int,
|
|
w : Int,
|
|
h : Int,
|
|
r : Int,
|
|
g : Int,
|
|
b : Int,
|
|
a : Int,
|
|
radius : Int,
|
|
) = "rs_fill_rect"
|
|
|
|
///|
|
|
extern "C" fn rs_stroke_rect(
|
|
x : Int,
|
|
y : Int,
|
|
w : Int,
|
|
h : Int,
|
|
r : Int,
|
|
g : Int,
|
|
b : Int,
|
|
a : Int,
|
|
width : Int,
|
|
radius : Int,
|
|
) = "rs_stroke_rect"
|
|
|
|
///|
|
|
extern "C" fn rs_line(
|
|
x0 : Int,
|
|
y0 : Int,
|
|
x1 : Int,
|
|
y1 : Int,
|
|
r : Int,
|
|
g : Int,
|
|
b : Int,
|
|
a : Int,
|
|
width : Int,
|
|
) = "rs_line"
|
|
|
|
///|
|
|
extern "C" fn rs_circle(
|
|
cx : Int,
|
|
cy : Int,
|
|
radius : Int,
|
|
r : Int,
|
|
g : Int,
|
|
b : Int,
|
|
a : Int,
|
|
filled : Int,
|
|
) = "rs_circle"
|
|
|
|
///| compositor/font's shim (stb_truetype); 0 on success.
|
|
#borrow(path)
|
|
extern "C" fn mp_font_load(slot : Int, path : Bytes) -> Int = "mp_font_load"
|
|
|
|
///|
|
|
extern "C" fn rs_font_loaded(slot : Int) = "rs_font_loaded"
|
|
|
|
///| Load the TTF file at `path` into `slot` (0..3).
|
|
fn font_load(slot : Int, path : String) -> Bool {
|
|
if slot < 0 || slot >= 4 {
|
|
return false
|
|
}
|
|
if mp_font_load(slot, @utf8.encode(path)) != 0 {
|
|
return false
|
|
}
|
|
rs_font_loaded(slot)
|
|
true
|
|
}
|
|
|
|
///|
|
|
extern "C" fn rs_font_has(slot : Int) -> Int = "rs_font_has"
|
|
|
|
///|
|
|
#borrow(info)
|
|
extern "C" fn rs_glyph_info(
|
|
slot : Int,
|
|
cp : Int,
|
|
size : Int,
|
|
info : FixedArray[Int],
|
|
) = "rs_glyph_info"
|
|
|
|
///|
|
|
#borrow(out)
|
|
extern "C" fn rs_glyph_coverage(
|
|
slot : Int,
|
|
cp : Int,
|
|
size : Int,
|
|
out : FixedArray[Byte],
|
|
) = "rs_glyph_coverage"
|
|
|
|
///|
|
|
extern "C" fn rs_draw_glyph(
|
|
slot : Int,
|
|
cp : Int,
|
|
size : Int,
|
|
pen : Int,
|
|
baseline : Int,
|
|
r : Int,
|
|
g : Int,
|
|
b : Int,
|
|
a : Int,
|
|
embolden : Int,
|
|
) = "rs_draw_glyph"
|
|
|
|
///|
|
|
#borrow(rgba)
|
|
extern "C" fn rs_image_put(
|
|
reuse : Int,
|
|
w : Int,
|
|
h : Int,
|
|
rgba : FixedArray[Byte],
|
|
) -> Int = "rs_image_put"
|
|
|
|
///|
|
|
#borrow(data)
|
|
extern "C" fn rs_image_svg(data : Bytes) -> Int = "rs_image_svg"
|
|
|
|
///|
|
|
extern "C" fn rs_draw_image(handle : Int, x : Int, y : Int, w : Int, h : Int) = "rs_draw_image"
|
|
|
|
///|
|
|
extern "C" fn rs_capture(
|
|
fd : Int,
|
|
offset : Int,
|
|
stride : Int,
|
|
w : Int,
|
|
h : Int,
|
|
format : Int,
|
|
) -> UInt = "rs_capture"
|
|
|
|
///|
|
|
extern "C" fn rs_release(id : UInt) = "rs_release"
|
|
|
|
///|
|
|
extern "C" fn rs_thumbnail(id : UInt, w : Int, h : Int, reuse : Int) -> Int = "rs_thumbnail"
|
|
|
|
///|
|
|
extern "C" fn rs_blit_surface(id : UInt, x : Int, y : Int, w : Int, h : Int) -> Int = "rs_blit_surface"
|
|
|
|
///|
|
|
extern "C" fn rs_surface_covers(id : UInt, w : Int, h : Int) -> Int = "rs_surface_covers"
|
|
|
|
///|
|
|
extern "C" fn rs_maps_gc() = "rs_maps_gc"
|
|
|
|
///|
|
|
extern "C" fn rs_image_take_changed(handle : Int) -> Int = "rs_image_take_changed"
|
|
|
|
///| Where the panel layer draws an image whose pixels changed under the same handle (the
|
|
/// Alt+Tab switcher's live previews): the ops alone compare equal, so this is damage too.
|
|
fn changed_images(panel : @wt.Val) -> R {
|
|
let mut d : R = { x: 0, y: 0, w: 0, h: 0 }
|
|
for op in panel.to_list() {
|
|
if op is Variant("draw-image", Some(p)) &&
|
|
rs_image_take_changed(p.field("image").to_int()) != 0 {
|
|
d = union(d, rect_of(p.field("area")))
|
|
}
|
|
}
|
|
d
|
|
}
|
|
|
|
///|
|
|
extern "C" fn rs_damage(x : Int, y : Int, w : Int, h : Int) = "rs_damage"
|
|
|
|
///|
|
|
extern "C" fn rs_save_backdrop() = "rs_save_backdrop"
|
|
|
|
///|
|
|
extern "C" fn rs_has_backdrop() -> Int = "rs_has_backdrop"
|
|
|
|
///|
|
|
extern "C" fn rs_copy_backdrop() = "rs_copy_backdrop"
|
|
|
|
///|
|
|
extern "C" fn rs_width() -> Int = "rs_width"
|
|
|
|
///|
|
|
extern "C" fn rs_height() -> Int = "rs_height"
|
|
|
|
///|
|
|
#borrow(out)
|
|
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
|
|
|
|
///|
|
|
struct R {
|
|
x : Int
|
|
y : Int
|
|
w : Int
|
|
h : Int
|
|
} derive(Eq, Show)
|
|
|
|
///|
|
|
fn R::empty(self : R) -> Bool {
|
|
self.w <= 0 || self.h <= 0
|
|
}
|
|
|
|
///|
|
|
fn rect_of(v : @wt.Val) -> R {
|
|
{
|
|
x: v.field("x").to_int(),
|
|
y: v.field("y").to_int(),
|
|
w: v.field("w").to_int(),
|
|
h: v.field("h").to_int(),
|
|
}
|
|
}
|
|
|
|
///| The overlap of two clips (None: no clip).
|
|
fn intersect(a : R?, b : R?) -> R? {
|
|
match (a, b) {
|
|
(None, k) | (k, None) => k
|
|
(Some(a), Some(b)) => {
|
|
let x0 = @cmp.maximum(a.x, b.x)
|
|
let y0 = @cmp.maximum(a.y, b.y)
|
|
let x1 = @cmp.minimum(a.x + a.w, b.x + b.w)
|
|
let y1 = @cmp.minimum(a.y + a.h, b.y + b.h)
|
|
Some({
|
|
x: x0,
|
|
y: y0,
|
|
w: @cmp.maximum(x1 - x0, 0),
|
|
h: @cmp.maximum(y1 - y0, 0),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
///| The bounding box of two rectangles (an empty one counts for nothing).
|
|
fn union(a : R, b : R) -> R {
|
|
if a.empty() {
|
|
return b
|
|
}
|
|
if b.empty() {
|
|
return a
|
|
}
|
|
let x0 = @cmp.minimum(a.x, b.x)
|
|
let y0 = @cmp.minimum(a.y, b.y)
|
|
let x1 = @cmp.maximum(a.x + a.w, b.x + b.w)
|
|
let y1 = @cmp.maximum(a.y + a.h, b.y + b.h)
|
|
{ x: x0, y: y0, w: x1 - x0, h: y1 - y0 }
|
|
}
|
|
|
|
///|
|
|
fn set_clip(k : R?) -> Unit {
|
|
match k {
|
|
Some(r) => rs_set_clip(1, r.x, r.y, r.w, r.h)
|
|
None => rs_set_clip(0, 0, 0, 0, 0)
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------- text
|
|
|
|
///| The font slot for (bold, mono), and whether its glyphs must be emboldened (a face with no
|
|
/// bold instance gets drawn a pixel wider). Slots: 0 mono, 1 mono bold, 2 sans. None when no
|
|
/// face is loaded at all.
|
|
fn pick_face(bold : Bool, mono : Bool) -> (Int, Bool)? {
|
|
let has_mono = rs_font_has(0) != 0
|
|
let has_sans = rs_font_has(2) != 0
|
|
if !has_mono && !has_sans {
|
|
return None
|
|
}
|
|
let has_bold = has_mono && rs_font_has(1) != 0
|
|
Some(
|
|
if mono && has_mono {
|
|
if bold && has_bold {
|
|
(1, false)
|
|
} else {
|
|
(0, bold)
|
|
}
|
|
} else if has_sans {
|
|
(2, bold)
|
|
} else {
|
|
(0, bold)
|
|
},
|
|
)
|
|
}
|
|
|
|
///|
|
|
let glyph_info : FixedArray[Int] = FixedArray::make(5, 0)
|
|
|
|
///| Advance of `cp` in `slot`.
|
|
fn advance(slot : Int, cp : Int, size : Int) -> Int {
|
|
rs_glyph_info(slot, cp, size, glyph_info)
|
|
glyph_info[4]
|
|
}
|
|
|
|
///|
|
|
fn draw_text(l : @wt.Val, ox : Int, oy : Int) -> Unit {
|
|
let text = l.field("text").to_string()
|
|
let col = l.field("color")
|
|
let ca = col.field("a").to_int()
|
|
if text == "" || ca == 0 {
|
|
return
|
|
}
|
|
let size = @cmp.maximum(l.field("size").to_int(), 0)
|
|
let bold = l.field("bold").to_bool()
|
|
let mono = l.field("mono").to_bool()
|
|
let (slot, embolden) = match pick_face(bold, mono) {
|
|
Some(f) => f
|
|
None => return // no face loaded: nothing to draw with
|
|
}
|
|
let extra = if embolden { 1 } else { 0 }
|
|
let cps : Array[Int] = []
|
|
let mut total_w = 0
|
|
for ch in text {
|
|
let cp = ch.to_int()
|
|
cps.push(cp)
|
|
total_w += advance(slot, cp, size) + extra
|
|
}
|
|
let x = l.field("x").to_int() + ox
|
|
let start_x = match l.field("align").to_int() {
|
|
1 => x - total_w / 2
|
|
2 => x - total_w
|
|
_ => x
|
|
}
|
|
// `y` is the vertical centre of the text; centre the capital height ('H') on it.
|
|
let (cap_slot, _) = match pick_face(false, mono) {
|
|
Some(f) => f
|
|
None => return
|
|
}
|
|
rs_glyph_info(cap_slot, 72, size, glyph_info)
|
|
let baseline = l.field("y").to_int() + oy + -glyph_info[3] / 2
|
|
let r = col.field("r").to_int()
|
|
let g = col.field("g").to_int()
|
|
let b = col.field("b").to_int()
|
|
let mut pen = start_x
|
|
for cp in cps {
|
|
rs_draw_glyph(slot, cp, size, pen, baseline, r, g, b, ca, extra)
|
|
pen += advance(slot, cp, size) + extra
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------- ops
|
|
|
|
///|
|
|
fn rgba(c : @wt.Val) -> (Int, Int, Int, Int) {
|
|
(
|
|
c.field("r").to_int(),
|
|
c.field("g").to_int(),
|
|
c.field("b").to_int(),
|
|
c.field("a").to_int(),
|
|
)
|
|
}
|
|
|
|
///| Draw `ops` offset by (ox, oy). `base` limits everything (the frame's damaged area); a
|
|
/// push-clip op replaces the clip within it.
|
|
fn draw_ops(ops : Array[@wt.Val], ox : Int, oy : Int, base : R?) -> Unit {
|
|
let stack : Array[R] = []
|
|
set_clip(base)
|
|
for op in ops {
|
|
guard op is Variant(name, payload)
|
|
match (name, payload) {
|
|
("fill-rect", Some(f)) => {
|
|
let a = rect_of(f.field("area"))
|
|
let (r, g, b, al) = rgba(f.field("color"))
|
|
rs_fill_rect(
|
|
a.x + ox,
|
|
a.y + oy,
|
|
a.w,
|
|
a.h,
|
|
r,
|
|
g,
|
|
b,
|
|
al,
|
|
f.field("radius").to_int(),
|
|
)
|
|
}
|
|
("stroke-rect", Some(s)) => {
|
|
let a = rect_of(s.field("area"))
|
|
let (r, g, b, al) = rgba(s.field("color"))
|
|
rs_stroke_rect(
|
|
a.x + ox,
|
|
a.y + oy,
|
|
a.w,
|
|
a.h,
|
|
r,
|
|
g,
|
|
b,
|
|
al,
|
|
s.field("width").to_int(),
|
|
s.field("radius").to_int(),
|
|
)
|
|
}
|
|
("draw-line", Some(s)) => {
|
|
let (r, g, b, al) = rgba(s.field("color"))
|
|
rs_line(
|
|
s.field("x0").to_int() + ox,
|
|
s.field("y0").to_int() + oy,
|
|
s.field("x1").to_int() + ox,
|
|
s.field("y1").to_int() + oy,
|
|
r,
|
|
g,
|
|
b,
|
|
al,
|
|
s.field("width").to_int(),
|
|
)
|
|
}
|
|
("draw-circle", Some(d)) => {
|
|
let (r, g, b, al) = rgba(d.field("color"))
|
|
rs_circle(
|
|
d.field("cx").to_int() + ox,
|
|
d.field("cy").to_int() + oy,
|
|
d.field("radius").to_int(),
|
|
r,
|
|
g,
|
|
b,
|
|
al,
|
|
if d.field("filled").to_bool() {
|
|
1
|
|
} else {
|
|
0
|
|
},
|
|
)
|
|
}
|
|
("draw-text", Some(l)) => draw_text(l, ox, oy)
|
|
("push-clip", Some(k)) => {
|
|
let k = rect_of(k)
|
|
stack.push({ x: k.x + ox, y: k.y + oy, w: k.w, h: k.h })
|
|
set_clip(intersect(Some(stack[stack.length() - 1]), base))
|
|
}
|
|
("pop-clip", _) => {
|
|
stack.pop() |> ignore
|
|
set_clip(
|
|
if stack.length() > 0 {
|
|
intersect(Some(stack[stack.length() - 1]), base)
|
|
} else {
|
|
base
|
|
},
|
|
)
|
|
}
|
|
("draw-image", Some(p)) => {
|
|
let a = rect_of(p.field("area"))
|
|
rs_draw_image(p.field("image").to_int(), a.x + ox, a.y + oy, a.w, a.h)
|
|
}
|
|
_ => ()
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------- frames
|
|
|
|
///| A frame as the compositor sent it (the parts compared to find what changed).
|
|
struct Frame {
|
|
width : Int
|
|
height : Int
|
|
wallpaper : @wt.Val? // Some(ops) when sent with this frame
|
|
background : @wt.Val
|
|
windows : Array[@wt.Val]
|
|
panel : @wt.Val
|
|
popups : @wt.Val
|
|
}
|
|
|
|
///| The last frame drawn, and the wallpaper ops the cached backdrop was drawn from.
|
|
let prev_frame : Ref[Frame?] = Ref(None)
|
|
|
|
///|
|
|
let backdrop_ops : Ref[@wt.Val?] = Ref(None)
|
|
|
|
///| Room around a window for its drop shadow (4 px right, 6 px down) and a little slack.
|
|
const SHADOW_PAD : Int = 8
|
|
|
|
///| What a window covers on screen: its decoration and drop shadow, or just itself when fullscreen.
|
|
fn window_footprint(win : @wt.Val) -> R {
|
|
let g = rect_of(win.field("geometry"))
|
|
if win.field("fullscreen").to_bool() {
|
|
return g
|
|
}
|
|
let it = win.field("inset-top").to_int()
|
|
let ir = win.field("inset-right").to_int()
|
|
let ib = win.field("inset-bottom").to_int()
|
|
let il = win.field("inset-left").to_int()
|
|
{
|
|
x: g.x - il - SHADOW_PAD,
|
|
y: g.y - it - SHADOW_PAD,
|
|
w: g.w + il + ir + 2 * SHADOW_PAD,
|
|
h: g.h + it + ib + 2 * SHADOW_PAD,
|
|
}
|
|
}
|
|
|
|
///| True when window `b` is window `a` with only a new client buffer (a client redrawing): same
|
|
/// place, size and decoration.
|
|
fn only_contents_changed(a : @wt.Val, b : @wt.Val) -> Bool {
|
|
a.field("geometry") == b.field("geometry") &&
|
|
a.field("fullscreen") == b.field("fullscreen") &&
|
|
a.field("inset-top") == b.field("inset-top") &&
|
|
a.field("inset-right") == b.field("inset-right") &&
|
|
a.field("inset-bottom") == b.field("inset-bottom") &&
|
|
a.field("inset-left") == b.field("inset-left") &&
|
|
a.field("deco") == b.field("deco")
|
|
}
|
|
|
|
///| True when `inner` lies inside `outer`.
|
|
fn contains(outer : R, inner : R) -> Bool {
|
|
inner.x >= outer.x &&
|
|
inner.y >= outer.y &&
|
|
inner.x + inner.w <= outer.x + outer.w &&
|
|
inner.y + inner.h <= outer.y + outer.h
|
|
}
|
|
|
|
///| The index of the topmost window whose opaque client buffer covers all of `area`: nothing
|
|
/// under it (backdrop, desktop card, lower windows, its own decoration) shows there, so none of
|
|
/// that is drawn. -1 when no window covers it.
|
|
fn occluder(windows : Array[@wt.Val], area : R) -> Int {
|
|
let mut i = windows.length() - 1
|
|
while i >= 0 {
|
|
let win = windows[i]
|
|
let g = rect_of(win.field("geometry"))
|
|
if contains(g, area) &&
|
|
rs_surface_covers(win.field("surface").to_uint(), g.w, g.h) != 0 {
|
|
return i
|
|
}
|
|
i = i - 1
|
|
}
|
|
-1
|
|
}
|
|
|
|
///| The part of the screen `f` changes over `prev`, as one rectangle; None when all of it must
|
|
/// be redrawn. Only window changes are narrowed down: any change to the wallpaper, desktop card,
|
|
/// panel or the set of windows redraws everything.
|
|
fn damage(prev : Frame, f : Frame) -> R? {
|
|
if prev.width != f.width ||
|
|
prev.height != f.height ||
|
|
prev.windows.length() != f.windows.length() {
|
|
return None
|
|
}
|
|
if f.background != prev.background || f.panel != prev.panel {
|
|
return None
|
|
}
|
|
if f.wallpaper is Some(ops) && prev.wallpaper != Some(ops) {
|
|
return None
|
|
}
|
|
let mut d : R = { x: 0, y: 0, w: 0, h: 0 }
|
|
for i, a in prev.windows {
|
|
let b = f.windows[i]
|
|
if a == b {
|
|
continue
|
|
}
|
|
if only_contents_changed(a, b) {
|
|
// A new buffer in the same place: only the client area is drawn again.
|
|
d = union(d, rect_of(b.field("geometry")))
|
|
continue
|
|
}
|
|
d = union(d, union(window_footprint(a), window_footprint(b)))
|
|
}
|
|
d = union(d, changed_images(f.panel))
|
|
if prev.popups != f.popups {
|
|
for p in prev.popups.to_list() {
|
|
d = union(d, rect_of(p.field("area")))
|
|
}
|
|
for p in f.popups.to_list() {
|
|
d = union(d, rect_of(p.field("area")))
|
|
}
|
|
}
|
|
Some(d)
|
|
}
|
|
|
|
///| Remember `f` as the last frame. Its wallpaper ops are kept from the frame that sent them, so
|
|
/// a later frame that resends the same wallpaper still compares equal.
|
|
fn keep_prev(f : Frame) -> Unit {
|
|
let wallpaper = match f.wallpaper {
|
|
Some(_) => f.wallpaper
|
|
None =>
|
|
match prev_frame.val {
|
|
Some(p) => p.wallpaper
|
|
None => None
|
|
}
|
|
}
|
|
prev_frame.val = Some({ ..f, wallpaper, })
|
|
}
|
|
|
|
///| Pixels redrawn by the last submit (for --profile).
|
|
let last_drawn : Ref[Int] = Ref(0)
|
|
|
|
///| Draw a frame into the framebuffer: wallpaper (cached), background, windows bottom to top,
|
|
/// panel, then client popups. The same layering as the software renderer's `render_scene`. Only
|
|
/// the part that differs from the last frame is redrawn.
|
|
fn submit(v : @wt.Val) -> Unit {
|
|
let f : Frame = {
|
|
width: @cmp.maximum(v.field("width").to_int(), 1),
|
|
height: @cmp.maximum(v.field("height").to_int(), 1),
|
|
wallpaper: v.field("wallpaper").to_option(),
|
|
background: v.field("background"),
|
|
windows: v.field("windows").to_list(),
|
|
panel: v.field("panel"),
|
|
popups: v.field("popups"),
|
|
}
|
|
let w = f.width
|
|
let h = f.height
|
|
let resized = rs_resize(w, h) != 0
|
|
// The frame starts from the cached backdrop (which covers every pixel), so the buffer is only
|
|
// cleared when there is none.
|
|
let backdrop_ready = rs_has_backdrop() != 0 &&
|
|
(match (f.wallpaper, backdrop_ops.val) {
|
|
(None, Some(_)) => true
|
|
(Some(ops), Some(o)) => o == ops
|
|
_ => false
|
|
})
|
|
let clip : R? = match prev_frame.val {
|
|
Some(prev) if !resized && backdrop_ready =>
|
|
match damage(prev, f) {
|
|
Some(d) =>
|
|
match intersect(Some(d), Some({ x: 0, y: 0, w, h })) {
|
|
Some(k) if !k.empty() => Some(k)
|
|
_ => {
|
|
// Nothing on screen changed (a window off screen, say).
|
|
last_drawn.val = 0
|
|
keep_prev(f)
|
|
return
|
|
}
|
|
}
|
|
None => None
|
|
}
|
|
_ => None
|
|
}
|
|
if !resized && !backdrop_ready {
|
|
rs_clear()
|
|
}
|
|
last_drawn.val = match clip {
|
|
Some(k) => k.w * k.h
|
|
None => w * h
|
|
}
|
|
rs_maps_gc()
|
|
match clip {
|
|
Some(k) => rs_damage(k.x, k.y, k.w, k.h)
|
|
None => rs_damage(0, 0, w, h)
|
|
}
|
|
// A full-screen (or tiled) client redrawing covers the whole damaged area with its own opaque
|
|
// pixels: start from that window instead of from the backdrop.
|
|
// (Only with the backdrop cached: a new wallpaper must still be drawn once to be kept.)
|
|
let top = if backdrop_ready {
|
|
occluder(f.windows, clip.unwrap_or({ x: 0, y: 0, w, h }))
|
|
} else {
|
|
-1
|
|
}
|
|
let occluded = top >= 0
|
|
let first = if occluded { top } else { 0 }
|
|
|
|
// 1. Wallpaper: drawn once, copied back while unchanged (only the damaged part).
|
|
match f.wallpaper {
|
|
Some(_) if occluded => ()
|
|
Some(ops) =>
|
|
if backdrop_ready {
|
|
set_clip(clip)
|
|
rs_copy_backdrop()
|
|
} else {
|
|
draw_ops(ops.to_list(), 0, 0, None)
|
|
rs_save_backdrop()
|
|
backdrop_ops.val = Some(ops)
|
|
}
|
|
None =>
|
|
if rs_has_backdrop() != 0 && !occluded {
|
|
set_clip(clip)
|
|
rs_copy_backdrop()
|
|
}
|
|
}
|
|
if !occluded {
|
|
draw_ops(f.background.to_list(), 0, 0, clip)
|
|
}
|
|
|
|
// 2. Windows, bottom to top (from the one covering the damage, when one does).
|
|
for wi = first; wi < f.windows.length(); wi = wi + 1 {
|
|
let win = f.windows[wi]
|
|
let g = rect_of(win.field("geometry"))
|
|
if clip is Some(_) {
|
|
match intersect(Some(window_footprint(win)), clip) {
|
|
Some(r) if !r.empty() => ()
|
|
_ => continue
|
|
}
|
|
}
|
|
if !win.field("fullscreen").to_bool() && !(occluded && wi == first) {
|
|
let it = win.field("inset-top").to_int()
|
|
let ir = win.field("inset-right").to_int()
|
|
let ib = win.field("inset-bottom").to_int()
|
|
let il = win.field("inset-left").to_int()
|
|
let outer_x = g.x - il
|
|
let outer_y = g.y - it
|
|
let outer_w = g.w + il + ir
|
|
let outer_h = g.h + it + ib
|
|
// Subtle drop shadow: only the strips that stick out from under the window (and the part
|
|
// under the translucent title bar) are blended.
|
|
let strips : Array[R] = [
|
|
{ x: outer_x + outer_w, y: outer_y, w: 4, h: outer_h + 6 },
|
|
{ x: outer_x, y: outer_y + outer_h, w: outer_w + 4, h: 6 },
|
|
{ x: outer_x, y: outer_y, w: outer_w, h: it + 2 },
|
|
]
|
|
for s in strips {
|
|
match intersect(Some(s), clip) {
|
|
Some(k) if !k.empty() => {
|
|
set_clip(Some(k))
|
|
rs_fill_rect(outer_x + 4, outer_y + 6, outer_w, outer_h, 0, 0, 0, 80, 8)
|
|
}
|
|
_ => ()
|
|
}
|
|
}
|
|
draw_ops(win.field("deco").to_list(), outer_x, outer_y, clip)
|
|
}
|
|
set_clip(clip)
|
|
if rs_blit_surface(win.field("surface").to_uint(), g.x, g.y, g.w, g.h) == 0 {
|
|
rs_fill_rect(g.x, g.y, g.w, g.h, 0x1e, 0x29, 0x3b, 0xff, 0)
|
|
}
|
|
}
|
|
|
|
// 3. Panel (and its popups, menus and notices), then 4. client popups above everything.
|
|
draw_ops(f.panel.to_list(), 0, 0, clip)
|
|
set_clip(clip)
|
|
for p in f.popups.to_list() {
|
|
let a = rect_of(p.field("area"))
|
|
rs_blit_surface(p.field("surface").to_uint(), a.x, a.y, a.w, a.h) |> ignore
|
|
}
|
|
keep_prev(f)
|
|
}
|
|
|
|
// ---------------------------------------------------------------- images
|
|
|
|
///| Image handles by path (0: it failed, don't try again).
|
|
let image_paths : Map[String, Int] = {}
|
|
|
|
///| Biggest image side accepted from a file.
|
|
const MAX_IMAGE : Int = 1024
|
|
|
|
///| Load the PNG or SVG at `path` (an app icon); the same path gives the same handle. 0 on failure.
|
|
fn load_image(path : String) -> Int {
|
|
match image_paths.get(path) {
|
|
Some(h) => return h
|
|
None => ()
|
|
}
|
|
let b = @sys.read_file_bytes(path)
|
|
let h = if b.length() > 8 && b[0] == b'\x89' && b[1] == b'P' && b[2] == b'N' {
|
|
match @renderer.decode_png(b) {
|
|
Some(p) if p.width <= MAX_IMAGE && p.height <= MAX_IMAGE =>
|
|
rs_image_put(0, p.width, p.height, p.data)
|
|
_ => 0
|
|
}
|
|
} else if path.has_suffix(".svg") && b.length() > 0 {
|
|
rs_image_svg(b)
|
|
} else {
|
|
0
|
|
}
|
|
image_paths.set(path, h)
|
|
h
|
|
}
|
|
|
|
///|
|
|
fn is_path_image(h : Int) -> Bool {
|
|
for _, v in image_paths {
|
|
if v == h {
|
|
return true
|
|
}
|
|
}
|
|
false
|
|
}
|
|
|
|
// ---------------------------------------------------------------- the interfaces
|
|
|
|
///|
|
|
fn font_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
|
|
[
|
|
(
|
|
"load",
|
|
a => boolean(font_load(a[0].to_int(), a[1].to_string())),
|
|
),
|
|
(
|
|
"rasterise",
|
|
a => {
|
|
let (slot, cp, size) = (a[0].to_int(), a[1].to_int(), a[2].to_int())
|
|
let info = FixedArray::make(5, 0)
|
|
rs_glyph_info(slot, cp, size, info)
|
|
let cov = FixedArray::make(info[0] * info[1], b'\x00')
|
|
if cov.length() > 0 {
|
|
rs_glyph_coverage(slot, cp, size, cov)
|
|
}
|
|
[
|
|
Record([
|
|
("width", S32(info[0])),
|
|
("height", S32(info[1])),
|
|
("xoff", S32(info[2])),
|
|
("yoff", S32(info[3])),
|
|
("advance", S32(info[4])),
|
|
("coverage", @wt.Val::bytes(Bytes::from_fixedarray(cov))),
|
|
]),
|
|
]
|
|
},
|
|
),
|
|
]
|
|
}
|
|
|
|
///|
|
|
fn raster_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
|
|
[
|
|
(
|
|
"capture",
|
|
a => [
|
|
U32(
|
|
rs_capture(
|
|
a[0].to_int(),
|
|
a[1].to_int(),
|
|
a[2].to_int(),
|
|
a[3].to_int(),
|
|
a[4].to_int(),
|
|
a[5].to_int(),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
(
|
|
"release",
|
|
a => {
|
|
rs_release(a[0].to_uint())
|
|
none
|
|
},
|
|
),
|
|
("load-image", a => [U32(load_image(a[0].to_string()).reinterpret_as_uint())]),
|
|
(
|
|
"thumbnail",
|
|
a => {
|
|
let reuse = a[3].to_int()
|
|
let reuse = if is_path_image(reuse) { 0 } else { reuse }
|
|
[
|
|
U32(
|
|
rs_thumbnail(a[0].to_uint(), a[1].to_int(), a[2].to_int(), reuse).reinterpret_as_uint(),
|
|
),
|
|
]
|
|
},
|
|
),
|
|
(
|
|
"submit",
|
|
a => {
|
|
submit(a[0])
|
|
none
|
|
},
|
|
),
|
|
(
|
|
"read-frame",
|
|
_ => {
|
|
[@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))]
|
|
},
|
|
),
|
|
]
|
|
}
|