///| 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 } }