Files
chandra/compositor/shell/render_mode.mbt
Claude 71f0b858d6
CI / test (push) Failing after 3m2s
Smoother animations without a GPU: faster rasteriser, frame pacing, software/hardware mode
Measured headless at 1920x1080 with CHANDRA_FRAME_TRACE (new: one log line per drawn frame):
- Overview open: 46 -> 62 fps, worst frame gap 69 -> 36 ms, raster 14 -> 5 ms a frame.
- Live Starfield: 19 -> 30 fps (its target), raster 49 -> 13 ms, CPU 97% -> 46%.

Rasteriser (MoonBit host; circles and previews also in the Rust host):
- Blending works on two channels at a time in 16-bit lanes with an exact /255: same pixels,
  about 3x quicker on big translucent fills (the overview's dim, the starfield's nebulae).
- Filled circles are drawn as row spans, rings skip their empty middle (a sqrt per pixel of the
  bounding box before). Same pixels; a Rust test checks it against the old loop.
- Window previews gliding in the overview are sampled straight from the preview at each frame's
  size instead of filtered and cached at a size used once; the MoonBit host also gets the
  one-large-size cache and 4096 px draw limit the Rust host had (it kept every size and didn't
  draw previews past 1024 px).
- Image scaling sums each output row's columns once, with 32-bit sums and division.

Frame pacing: frames are drawn only when the output can take them (no page flip in flight on
KMS; the previous frame shown nested and on X11), with a frame every 250 ms for an output that
never gets ready. The KMS loop runs at 125 Hz, so animations used to rasterise frames that were
never shown.

Software or hardware mode (compositor/shell/render_mode.mbt, docs/rendering.md): detected from
the DRM render nodes' drivers (and NVIDIA's proprietary driver, LIBGL_ALWAYS_SOFTWARE),
overridden by CHANDRA_RENDER=software|hardware or Settings > Session "Lighter animations"
(state.edn :render, saved only when it differs from what was detected). Software mode remakes
changing previews every 200 ms instead of 100 ms and caps desktop modules at ~30 fps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hkTQp5uN5FtwpzrXUA
2026-11-26 01:55:50 +00:00

189 lines
6.3 KiB
MoonBit

///| Software or hardware rendering: whether this machine has a GPU that apps can draw with.
///
/// Chandra itself always draws the desktop on the CPU (the host's rasteriser), so the GPU never
/// makes the compositor's own frames cheaper. What it changes is how much CPU is left over: with
/// a GPU, apps (browsers, video, games) draw there; without one they draw with llvmpipe or their
/// own software paths on the same cores as Chandra. So in software mode the desktop asks for
/// less: window previews are remade less often while they are on screen, and a desktop module
/// (Live Starfield) gets at most 30 frames a second.
///
/// Detected at start (`detect_render_mode`): a DRM render node whose driver drives a real GPU
/// (i915, xe, amdgpu, radeon, nouveau, nvidia, Arm and other SoC GPUs) means hardware; none, or
/// only virtual/display-only ones (simpledrm, vkms, virtio_gpu, qxl, bochs, vmwgfx, ...), or
/// LIBGL_ALWAYS_SOFTWARE=1 mean software. Overrides, strongest first: the CHANDRA_RENDER
/// environment variable (`software` or `hardware`), then Settings > Session "Lighter
/// animations", saved in state.edn as `:render :software` or `:render :hardware` only when it
/// differs from what was detected.
///|
pub(all) enum RenderMode {
Software
Hardware
} derive(Eq, Show, Debug)
///| The name state.edn and CHANDRA_RENDER use.
pub fn RenderMode::name(self : RenderMode) -> String {
match self {
Software => "software"
Hardware => "hardware"
}
}
///| "software" / "hardware" (any case, also "sw" / "hw", "gpu" / "cpu").
pub fn RenderMode::parse(s : String) -> RenderMode? {
match s.trim(chars=" \n\t").to_lower().to_string() {
"software" | "sw" | "cpu" => Some(Software)
"hardware" | "hw" | "gpu" => Some(Hardware)
_ => None
}
}
///| DRM drivers that only scan out or emulate a display: a render node from one of these is no
/// GPU to draw with.
let software_drm_drivers : Array[String] = [
"simpledrm", "simple-framebuffer", "efifb", "vesafb", "vkms", "vgem", "virtio_gpu", "virtio-gpu",
"qxl", "bochs", "bochs-drm", "cirrus", "cirrus-qemu", "vboxvideo", "vmwgfx", "hyperv_drm", "udl",
"gud", "ast", "mgag200", "xen-front",
]
///| The driver named by a sysfs `uevent` file's DRIVER= line ("" when it has none).
pub fn uevent_driver(text : String) -> String {
for line in text.split("\n") {
let l = line.to_string()
if l.has_prefix("DRIVER=") {
return l[7:].to_string()
}
}
""
}
///| Hardware or software from the DRM render nodes' drivers (render node drivers as found, in
/// order), the NVIDIA proprietary driver being loaded, and LIBGL_ALWAYS_SOFTWARE. Also why, for
/// the log and Settings.
pub fn classify_render(
drivers : Array[String],
nvidia : Bool,
libgl_software : Bool,
) -> (RenderMode, String) {
if libgl_software {
return (Software, "LIBGL_ALWAYS_SOFTWARE is set")
}
for d in drivers {
if d.length() > 0 && !software_drm_drivers.contains(d) {
return (Hardware, "GPU: " + d)
}
}
if nvidia {
return (Hardware, "GPU: nvidia")
}
match drivers.iter().find_first(d => d.length() > 0) {
Some(d) => (Software, "no GPU (only " + d + ")")
None => (Software, "no GPU found")
}
}
///| Look at this machine: see `classify_render`.
pub fn detect_render_mode() -> (RenderMode, String) {
let drivers : Array[String] = []
for n = 128; n < 136; n = n + 1 {
let b = @sys.read_proc_bytes("/sys/class/drm/renderD" + n.to_string() + "/device/uevent")
if b.length() > 0 {
drivers.push(uevent_driver(bytes_text(b)))
}
}
let nvidia = @sys.read_proc_bytes("/proc/driver/nvidia/version").length() > 0
let libgl = @sys.getenv("LIBGL_ALWAYS_SOFTWARE")
classify_render(drivers, nvidia, libgl.length() > 0 && libgl != "0" && libgl != "false")
}
///|
fn bytes_text(b : Bytes) -> String {
let sb = StringBuilder()
for c in b {
sb.write_char(Int::unsafe_to_char(c.to_int()))
}
sb.to_string()
}
///| Set the detected mode (and why) at start; CHANDRA_RENDER, when set, replaces it.
pub fn DesktopShell::set_render_detected(self : DesktopShell, mode : RenderMode, why : String) -> Unit {
match RenderMode::parse(@sys.getenv("CHANDRA_RENDER")) {
Some(m) => {
self.render_detected = m
self.render_why = "CHANDRA_RENDER=" + m.name()
self.render_forced = true
}
None => {
self.render_detected = mode
self.render_why = why
self.render_forced = false
}
}
}
///| The mode in effect: CHANDRA_RENDER, else the Settings choice, else what was detected.
pub fn DesktopShell::render_mode(self : DesktopShell) -> RenderMode {
if self.render_forced {
return self.render_detected
}
match self.render_choice {
Some(m) => m
None => self.render_detected
}
}
///| True when the desktop should ask less of the CPU (no GPU).
pub fn DesktopShell::software_render(self : DesktopShell) -> Bool {
self.render_mode() == Software
}
///| One line for the log: the mode and where it came from.
pub fn DesktopShell::render_summary(self : DesktopShell) -> String {
let from = if self.render_forced {
self.render_why
} else if self.render_choice is Some(_) {
"chosen in Settings; detected " + self.render_detected.name() + ", " + self.render_why
} else {
self.render_why
}
"rendering: " + self.render_mode().name() + " (" + from + ")"
}
///| Choose software (true) or hardware rendering in Settings: remembered only when it differs
/// from what was detected.
pub fn DesktopShell::choose_software_render(self : DesktopShell, on : Bool) -> Unit {
let m = if on { Software } else { Hardware }
self.render_choice = if m == self.render_detected { None } else { Some(m) }
}
///| The Settings switch's label: what it does and what was found.
fn DesktopShell::render_setting_label(self : DesktopShell) -> String {
let found = if self.render_forced {
self.render_why
} else {
match self.render_detected {
Software => "no GPU found"
Hardware => "GPU found"
}
}
"Lighter animations (" + found + ")"
}
///| How often (ms) a window preview that keeps changing is remade while it is on screen.
pub fn DesktopShell::preview_refresh_ms(self : DesktopShell) -> UInt64 {
if self.software_render() {
200UL
} else {
100UL
}
}
///| The shortest time (ms) between a desktop module's frames.
pub fn DesktopShell::desktop_min_frame_ms(self : DesktopShell) -> Int {
if self.software_render() {
33
} else {
16
}
}