0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 1 | //! Bare metal. A libseat session, udev for the primary GPU and its |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 2 | //! connectors, libinput for input, and one smithay DRM output per connector, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 3 | //! each rendered when its own vblank lands. |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 4 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 5 | use crate::scene::Draw; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 6 | use crate::state::{State, now_ns}; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 7 | use monolith_config::Vrr; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 8 | use monolith_layout::{OutputId, Rect}; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 9 | use smithay::backend::allocator::Fourcc; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 10 | use smithay::backend::allocator::gbm::{GbmAllocator, GbmBufferFlags, GbmDevice}; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 11 | use smithay::backend::drm::VrrSupport; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 12 | use smithay::backend::drm::compositor::FrameFlags; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 13 | use smithay::backend::drm::exporter::gbm::GbmFramebufferExporter; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 14 | use smithay::backend::drm::output::{DrmOutput, DrmOutputManager, DrmOutputRenderElements}; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 15 | use smithay::backend::drm::{ |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 16 | DrmDevice, DrmDeviceFd, DrmEvent, DrmEventMetadata, DrmEventTime, DrmNode, NodeType, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 17 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 18 | use smithay::backend::input::{ |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 19 | AbsolutePositionEvent, Axis, AxisSource, Event, InputEvent, KeyboardKeyEvent, PointerAxisEvent, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 20 | PointerButtonEvent, PointerMotionEvent, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 21 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 22 | use smithay::backend::libinput::{LibinputInputBackend, LibinputSessionInterface}; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 23 | use smithay::backend::session::libseat::LibSeatSession; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 24 | use smithay::backend::session::{Event as SessionEvent, Session}; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 25 | use smithay::backend::udev::{UdevBackend, UdevEvent, all_gpus, primary_gpu}; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 26 | use smithay::input::pointer::AxisFrame; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 27 | use smithay::output::{Mode, Output, PhysicalProperties, Scale, Subpixel}; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 28 | use smithay::reexports::calloop::timer::{TimeoutAction, Timer}; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 29 | use smithay::reexports::calloop::{EventLoop, LoopHandle, RegistrationToken}; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 30 | use smithay::reexports::drm::Device as _; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 31 | use smithay::reexports::drm::control::{ModeTypeFlags, connector, crtc}; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 32 | use smithay::reexports::input::Libinput; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 33 | use smithay::reexports::rustix::fs::OFlags; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 34 | use smithay::utils::{DeviceFd, Point, Transform}; |
e4b186dffb77 | atagen | 2026-10-16 13:07 UTC | 35 | use smithay::wayland::drm_syncobj::{DrmSyncobjState, supports_syncobj_eventfd}; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 36 | use smithay_drm_extras::drm_scanner::{DrmScanEvent, DrmScanner}; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 37 | use std::collections::{HashMap, VecDeque}; |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 38 | use std::path::{Path, PathBuf}; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 39 | use std::time::Duration; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 40 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 41 | const CLEAR: [f32; 4] = [0.07, 0.075, 0.09, 1.0]; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 42 | /// Frames that only move the cursor stay at or below 60 Hz under VRR, so the |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 43 | /// mouse does not override the refresh rate a game is pacing. |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 44 | const CURSOR_FRAME_NS: u64 = 16_666_667; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 45 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 46 | type Allocator = GbmAllocator<DrmDeviceFd>; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 47 | type Exporter = GbmFramebufferExporter<DrmDeviceFd>; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 48 | type Manager = DrmOutputManager<Allocator, Exporter, (), DrmDeviceFd>; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 49 | type Out = DrmOutput<Allocator, Exporter, (), DrmDeviceFd>; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 50 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 51 | pub struct Drm { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 52 | pub session: LibSeatSession, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 53 | libinput: Libinput, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 54 | handle: LoopHandle<'static, State>, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 55 | node: DrmNode, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 56 | manager: Manager, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 57 | scanner: DrmScanner, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 58 | nvidia: bool, |
fdb16f42492e | atagen | 2026-10-16 13:40 UTC | 59 | frame_flags: FrameFlags, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 60 | surfaces: HashMap<crtc::Handle, Surface>, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 61 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 62 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 63 | struct Surface { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 64 | output: Output, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 65 | id: OutputId, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 66 | drm: Out, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 67 | /// A frame is on its way to the screen and its vblank has not landed. |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 68 | queued: bool, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 69 | redraw: bool, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 70 | last_vblank_ns: Option<u64>, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 71 | refresh_ns: u64, |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 72 | /// The pending render, timed to start just in time for `target_ns`. |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 73 | scheduled: Option<RegistrationToken>, |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 74 | /// The vblank the frame being drawn or queued is meant for. |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 75 | target_ns: Option<u64>, |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 76 | budget: Budget, |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 77 | /// Frames presented since `stats_since`, for the once-a-second debug line. |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 78 | presented: u32, |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 79 | stats_since: u64, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 80 | vrr_capable: bool, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 81 | vrr_on: bool, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 82 | /// Only the cursor moved, which under VRR is drawn at a capped rate. |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 83 | cursor_redraw: bool, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 84 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 85 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 86 | impl Surface { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 87 | /// The first vblank a render starting at `now` can still make, and the |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 88 | /// latest moment to start it. |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 89 | fn next_deadline(&self, now: u64) -> Option<(u64, u64)> { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 90 | let last = self.last_vblank_ns?; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 91 | let refresh = self.refresh_ns; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 92 | if refresh == 0 { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 93 | return None; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 94 | } |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 95 | let budget = self.budget.ns().min(refresh); |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 96 | let periods = (now + budget).saturating_sub(last).div_ceil(refresh).max(1); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 97 | let vblank = last + periods * refresh; |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 98 | Some((vblank, vblank - budget)) |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 99 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 100 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 101 | |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 102 | /// How long before a vblank rendering must start. A high percentile of |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 103 | /// recent CPU render times covers the CPU side without one spike inflating |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 104 | /// it, and a slack that grows on every missed vblank and shrinks over seconds |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 105 | /// while frames land covers GPU time and the commit. |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 106 | struct Budget { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 107 | renders: VecDeque<u64>, |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 108 | slack_ns: u64, |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 109 | /// The slack that last proved enough after a miss. Decay stops there |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 110 | /// for `floor_frames` frames, so it does not keep rediscovering the same |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 111 | /// limit one dropped frame at a time. |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 112 | floor_ns: u64, |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 113 | floor_frames: u64, |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 114 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 115 | |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 116 | impl Budget { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 117 | const KEEP: usize = 32; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 118 | const MIN_SLACK_NS: u64 = 1_000_000; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 119 | const STEP_NS: u64 = 500_000; |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 120 | const DECAY_NS: u64 = 1_000; |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 121 | const FLOOR_NS: u64 = 30_000_000_000; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 122 | |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 123 | fn new() -> Self { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 124 | Self { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 125 | renders: VecDeque::with_capacity(Self::KEEP), |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 126 | slack_ns: 2_000_000, |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 127 | floor_ns: 0, |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 128 | floor_frames: 0, |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 129 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 130 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 131 | |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 132 | fn ns(&self) -> u64 { |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 133 | let mut renders: Vec<u64> = self.renders.iter().copied().collect(); |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 134 | renders.sort_unstable(); |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 135 | let p90 = renders |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 136 | .get(renders.len().saturating_sub(1) * 9 / 10) |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 137 | .copied() |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 138 | .unwrap_or(0); |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 139 | p90 + self.slack_ns |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 140 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 141 | |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 142 | fn record(&mut self, render_ns: u64) { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 143 | if self.renders.len() == Self::KEEP { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 144 | self.renders.pop_front(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 145 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 146 | self.renders.push_back(render_ns); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 147 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 148 | |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 149 | fn missed(&mut self, refresh_ns: u64) { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 150 | self.slack_ns = (self.slack_ns + Self::STEP_NS).min(refresh_ns / 2); |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 151 | self.floor_ns = self.slack_ns; |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 152 | self.floor_frames = Self::FLOOR_NS / refresh_ns.max(1); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 153 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 154 | |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 155 | fn landed(&mut self) { |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 156 | self.floor_frames = self.floor_frames.saturating_sub(1); |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 157 | let floor = if self.floor_frames > 0 { |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 158 | self.floor_ns |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 159 | } else { |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 160 | Self::MIN_SLACK_NS |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 161 | }; |
9c83f07146b1 | atagen | 2026-10-16 13:36 UTC | 162 | self.slack_ns = self.slack_ns.saturating_sub(Self::DECAY_NS).max(floor); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 163 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 164 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 165 | |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 166 | /// The GPU monolith drives on bare metal, the seat's primary or else its |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 167 | /// first. |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 168 | pub fn primary() -> Result<PathBuf, Box<dyn std::error::Error>> { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 169 | let seat = std::env::var("XDG_SEAT").unwrap_or_else(|_| "seat0".into()); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 170 | Ok(match primary_gpu(&seat)? { |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 171 | Some(p) => p, |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 172 | None => all_gpus(&seat)?.into_iter().next().ok_or("no GPU found")?, |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 173 | }) |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 174 | } |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 175 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 176 | pub fn init( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 177 | event_loop: &mut EventLoop<'static, State>, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 178 | state: &mut State, |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 179 | path: &Path, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 180 | ) -> Result<(), Box<dyn std::error::Error>> { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 181 | let (mut session, notifier) = LibSeatSession::new()?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 182 | let seat = session.seat(); |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 183 | let node = DrmNode::from_path(path)?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 184 | tracing::info!(path = %path.display(), "primary GPU"); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 185 | let fd = session.open( |
f27fa4c2ade9 | atagen | 2026-10-16 12:59 UTC | 186 | path, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 187 | OFlags::RDWR | OFlags::CLOEXEC | OFlags::NOCTTY | OFlags::NONBLOCK, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 188 | )?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 189 | let fd = DrmDeviceFd::new(DeviceFd::from(fd)); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 190 | let (device, drm_events) = DrmDevice::new(fd.clone(), true)?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 191 | let nvidia = device |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 192 | .get_driver() |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 193 | .map(|d| d.name().to_string_lossy().to_lowercase().contains("nvidia")) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 194 | .unwrap_or(false); |
e4b186dffb77 | atagen | 2026-10-16 13:07 UTC | 195 | if supports_syncobj_eventfd(&fd) { |
e4b186dffb77 | atagen | 2026-10-16 13:07 UTC | 196 | state.syncobj = Some(DrmSyncobjState::new::<State>(&state.display, fd.clone())); |
e4b186dffb77 | atagen | 2026-10-16 13:07 UTC | 197 | } else { |
e4b186dffb77 | atagen | 2026-10-16 13:07 UTC | 198 | tracing::info!("no syncobj eventfd support, clients stay on implicit sync"); |
e4b186dffb77 | atagen | 2026-10-16 13:07 UTC | 199 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 200 | let gbm = GbmDevice::new(fd)?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 201 | let render_node = node |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 202 | .node_with_type(NodeType::Render) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 203 | .and_then(Result::ok) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 204 | .unwrap_or(node); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 205 | let allocator = GbmAllocator::new( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 206 | gbm.clone(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 207 | GbmBufferFlags::RENDERING | GbmBufferFlags::SCANOUT, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 208 | ); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 209 | let exporter = GbmFramebufferExporter::new(gbm.clone(), render_node.into()); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 210 | let render_formats: Vec<_> = state.renderer.render_formats().iter().copied().collect(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 211 | let manager = DrmOutputManager::new( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 212 | device, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 213 | allocator, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 214 | exporter, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 215 | Some(gbm), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 216 | [Fourcc::Xrgb8888, Fourcc::Argb8888], |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 217 | render_formats, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 218 | ); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 219 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 220 | let mut libinput = |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 221 | Libinput::new_with_udev::<LibinputSessionInterface<LibSeatSession>>(session.clone().into()); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 222 | libinput |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 223 | .udev_assign_seat(&seat) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 224 | .map_err(|()| "libinput could not take the seat")?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 225 | let handle = event_loop.handle(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 226 | handle.insert_source( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 227 | LibinputInputBackend::new(libinput.clone()), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 228 | |event, _, state| { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 229 | on_input(state, event); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 230 | }, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 231 | )?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 232 | handle.insert_source(notifier, |event, _, state| match event { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 233 | SessionEvent::PauseSession => { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 234 | if let Some(drm) = state.drm.as_mut() { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 235 | drm.libinput.suspend(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 236 | drm.manager.pause(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 237 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 238 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 239 | SessionEvent::ActivateSession => { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 240 | if let Some(drm) = state.drm.as_mut() { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 241 | if drm.libinput.resume().is_err() { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 242 | tracing::warn!("libinput did not resume"); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 243 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 244 | if let Err(e) = drm.manager.lock().activate(false) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 245 | tracing::warn!("could not reactivate the GPU: {e}"); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 246 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 247 | for s in drm.surfaces.values_mut() { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 248 | s.queued = false; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 249 | s.redraw = true; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 250 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 251 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 252 | state.needs_redraw = true; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 253 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 254 | })?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 255 | handle.insert_source(drm_events, |event, meta, state| match event { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 256 | DrmEvent::VBlank(crtc) => on_vblank(state, crtc, meta), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 257 | DrmEvent::Error(e) => tracing::warn!("drm: {e}"), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 258 | })?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 259 | let udev = UdevBackend::new(&seat)?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 260 | handle.insert_source(udev, move |event, _, state| { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 261 | if let UdevEvent::Changed { device_id } = event |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 262 | && state |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 263 | .drm |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 264 | .as_ref() |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 265 | .is_some_and(|d| d.node.dev_id() == device_id) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 266 | { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 267 | scan(state); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 268 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 269 | })?; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 270 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 271 | state.drm = Some(Drm { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 272 | session, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 273 | libinput, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 274 | handle, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 275 | node, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 276 | manager, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 277 | scanner: DrmScanner::new(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 278 | nvidia, |
fdb16f42492e | atagen | 2026-10-16 13:40 UTC | 279 | frame_flags: if std::env::var_os("MONOLITH_NO_SCANOUT").is_some() { |
fdb16f42492e | atagen | 2026-10-16 13:40 UTC | 280 | FrameFlags::empty() |
fdb16f42492e | atagen | 2026-10-16 13:40 UTC | 281 | } else { |
fdb16f42492e | atagen | 2026-10-16 13:40 UTC | 282 | FrameFlags::DEFAULT |
fdb16f42492e | atagen | 2026-10-16 13:40 UTC | 283 | }, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 284 | surfaces: HashMap::new(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 285 | }); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 286 | scan(state); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 287 | Ok(()) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 288 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 289 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 290 | /// Brings outputs in line with what is plugged in. |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 291 | fn scan(state: &mut State) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 292 | let Some(drm) = state.drm.as_mut() else { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 293 | return; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 294 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 295 | let events: Vec<DrmScanEvent> = match drm.scanner.scan_connectors(drm.manager.device()) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 296 | Ok(result) => result.into_iter().collect(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 297 | Err(e) => { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 298 | tracing::warn!("scanning connectors failed: {e}"); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 299 | return; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 300 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 301 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 302 | for event in events { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 303 | match event { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 304 | DrmScanEvent::Connected { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 305 | connector, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 306 | crtc: Some(crtc), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 307 | } => connect(state, connector, crtc), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 308 | DrmScanEvent::Disconnected { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 309 | crtc: Some(crtc), .. |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 310 | } => disconnect(state, crtc), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 311 | _ => {} |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 312 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 313 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 314 | state.apply_layout(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 315 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 316 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 317 | fn connect(state: &mut State, connector: connector::Info, crtc: crtc::Handle) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 318 | let name = format!( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 319 | "{}-{}", |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 320 | connector.interface().as_str(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 321 | connector.interface_id() |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 322 | ); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 323 | let Some(&mode) = connector |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 324 | .modes() |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 325 | .iter() |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 326 | .find(|m| m.mode_type().contains(ModeTypeFlags::PREFERRED)) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 327 | .or_else(|| connector.modes().first()) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 328 | else { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 329 | tracing::warn!(name, "connector has no modes"); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 330 | return; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 331 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 332 | let State { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 333 | drm: Some(drm), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 334 | renderer, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 335 | layout, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 336 | space, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 337 | display, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 338 | .. |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 339 | } = state |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 340 | else { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 341 | return; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 342 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 343 | let (mm_w, mm_h) = connector.size().unwrap_or((0, 0)); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 344 | let output = Output::new( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 345 | name.clone(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 346 | PhysicalProperties { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 347 | size: (mm_w as i32, mm_h as i32).into(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 348 | subpixel: Subpixel::Unknown, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 349 | make: "Unknown".into(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 350 | model: name.clone(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 351 | serial_number: String::new(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 352 | }, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 353 | ); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 354 | let wl_mode = Mode::from(mode); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 355 | let x = layout |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 356 | .outputs() |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 357 | .map(|(_, o)| o.area.right()) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 358 | .fold(0.0, f64::max); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 359 | let position = Point::from((x as i32, 0)); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 360 | output.change_current_state( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 361 | Some(wl_mode), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 362 | Some(Transform::Normal), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 363 | Some(Scale::Integer(1)), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 364 | Some(position), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 365 | ); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 366 | output.set_preferred(wl_mode); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 367 | output.create_global::<State>(display); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 368 | let mut planes = drm.manager.device().planes(&crtc).ok(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 369 | if drm.nvidia |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 370 | && let Some(p) = planes.as_mut() |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 371 | { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 372 | // NVIDIA's overlay planes break composition, as anvil also found. |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 373 | p.overlay.clear(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 374 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 375 | let out = match drm.manager.lock().initialize_output::<_, Draw>( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 376 | crtc, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 377 | mode, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 378 | &[connector.handle()], |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 379 | &output, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 380 | planes, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 381 | renderer, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 382 | &DrmOutputRenderElements::default(), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 383 | ) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 384 | Ok(out) => out, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 385 | Err(e) => { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 386 | tracing::warn!(name, "could not light up the output: {e}"); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 387 | return; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 388 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 389 | }; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 390 | let vrr_capable = matches!( |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 391 | out.with_compositor(|c| c.vrr_supported(connector.handle())), |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 392 | Ok(VrrSupport::Supported) |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 393 | ); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 394 | space.map_output(&output, position); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 395 | let (w, h) = mode.size(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 396 | let area = Rect::new(x, 0.0, f64::from(w), f64::from(h)); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 397 | let id = layout.add_output(&name, area); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 398 | tracing::info!( |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 399 | name, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 400 | w, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 401 | h, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 402 | refresh = mode.vrefresh(), |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 403 | vrr_capable, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 404 | "output connected" |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 405 | ); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 406 | drm.surfaces.insert( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 407 | crtc, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 408 | Surface { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 409 | output, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 410 | id, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 411 | drm: out, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 412 | queued: false, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 413 | redraw: true, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 414 | last_vblank_ns: None, |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 415 | scheduled: None, |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 416 | target_ns: None, |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 417 | budget: Budget::new(), |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 418 | presented: 0, |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 419 | stats_since: 0, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 420 | vrr_capable, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 421 | vrr_on: false, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 422 | cursor_redraw: false, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 423 | refresh_ns: 1_000_000_000_000 / u64::from(wl_mode.refresh.max(1) as u32), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 424 | }, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 425 | ); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 426 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 427 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 428 | fn disconnect(state: &mut State, crtc: crtc::Handle) { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 429 | let Some(drm) = state.drm.as_mut() else { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 430 | return; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 431 | }; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 432 | let Some(surface) = drm.surfaces.remove(&crtc) else { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 433 | return; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 434 | }; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 435 | if let Some(token) = surface.scheduled { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 436 | drm.handle.remove(token); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 437 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 438 | tracing::info!(name = surface.output.name(), "output disconnected"); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 439 | state.space.unmap_output(&surface.output); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 440 | state.layout.remove_output(surface.id); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 441 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 442 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 443 | /// The smithay output for a layout output, for protocols that need one. |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 444 | pub fn output_for(drm: &Drm, id: OutputId) -> Option<Output> { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 445 | drm.surfaces |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 446 | .values() |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 447 | .find(|s| s.id == id) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 448 | .map(|s| s.output.clone()) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 449 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 450 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 451 | fn on_vblank(state: &mut State, crtc: crtc::Handle, meta: &mut Option<DrmEventMetadata>) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 452 | let Some(surface) = state.drm.as_mut().and_then(|d| d.surfaces.get_mut(&crtc)) else { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 453 | return; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 454 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 455 | if let Err(e) = surface.drm.frame_submitted() { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 456 | tracing::warn!("frame was not submitted: {e}"); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 457 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 458 | surface.queued = false; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 459 | if let Some(DrmEventTime::Monotonic(t)) = meta.as_ref().map(|m| m.time) { |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 460 | let t = t.as_nanos() as u64; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 461 | if let Some(target) = surface.target_ns.take() { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 462 | if t > target + surface.refresh_ns / 2 { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 463 | surface.budget.missed(surface.refresh_ns); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 464 | tracing::debug!( |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 465 | output = surface.output.name(), |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 466 | late_ns = t - target, |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 467 | budget_ns = surface.budget.ns(), |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 468 | "frame missed its vblank" |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 469 | ); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 470 | } else { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 471 | surface.budget.landed(); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 472 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 473 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 474 | surface.last_vblank_ns = Some(t); |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 475 | surface.presented += 1; |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 476 | let window = t.saturating_sub(surface.stats_since); |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 477 | if window >= 1_000_000_000 { |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 478 | let vblanks = window / surface.refresh_ns.max(1); |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 479 | tracing::debug!( |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 480 | output = surface.output.name(), |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 481 | presented = surface.presented, |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 482 | vblanks, |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 483 | budget_us = surface.budget.ns() / 1000, |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 484 | "frame rate" |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 485 | ); |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 486 | surface.presented = 0; |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 487 | surface.stats_since = t; |
6c3361e382e4 | atagen | 2026-10-16 13:12 UTC | 488 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 489 | } |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 490 | if surface.redraw || surface.cursor_redraw { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 491 | schedule(state, crtc); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 492 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 493 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 494 | |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 495 | /// Starts a render at the last moment that still makes the next reachable |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 496 | /// vblank, so commits arriving until then make it into the frame. |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 497 | fn schedule(state: &mut State, crtc: crtc::Handle) { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 498 | let Some(drm) = state.drm.as_mut() else { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 499 | return; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 500 | }; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 501 | let Some(surface) = drm.surfaces.get_mut(&crtc) else { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 502 | return; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 503 | }; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 504 | if surface.queued || surface.scheduled.is_some() { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 505 | return; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 506 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 507 | let now = now_ns(); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 508 | if surface.vrr_on { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 509 | let since_flip = surface |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 510 | .last_vblank_ns |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 511 | .map_or(u64::MAX, |l| now.saturating_sub(l)); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 512 | if surface.redraw || since_flip >= CURSOR_FRAME_NS { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 513 | draw(state, crtc, now); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 514 | return; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 515 | } |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 516 | let wait = Timer::from_duration(Duration::from_nanos(CURSOR_FRAME_NS - since_flip)); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 517 | let token = drm.handle.insert_source(wait, move |_, _, state| { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 518 | if let Some(s) = state.drm.as_mut().and_then(|d| d.surfaces.get_mut(&crtc)) { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 519 | s.scheduled = None; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 520 | } |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 521 | draw(state, crtc, now_ns()); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 522 | TimeoutAction::Drop |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 523 | }); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 524 | surface.scheduled = token.ok(); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 525 | return; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 526 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 527 | let Some((vblank, start)) = surface.next_deadline(now) else { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 528 | draw(state, crtc, now); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 529 | return; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 530 | }; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 531 | surface.target_ns = Some(vblank); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 532 | let wait = Timer::from_duration(Duration::from_nanos(start.saturating_sub(now))); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 533 | let token = drm.handle.insert_source(wait, move |_, _, state| { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 534 | let target = state.drm.as_mut().and_then(|d| { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 535 | let s = d.surfaces.get_mut(&crtc)?; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 536 | s.scheduled = None; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 537 | s.target_ns |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 538 | }); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 539 | if let Some(target) = target { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 540 | draw(state, crtc, target); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 541 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 542 | TimeoutAction::Drop |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 543 | }); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 544 | surface.scheduled = token.ok(); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 545 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 546 | |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 547 | fn draw(state: &mut State, crtc: crtc::Handle, target: u64) { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 548 | if render_surface(state, crtc, target) { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 549 | schedule(state, crtc); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 550 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 551 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 552 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 553 | /// Renders every output that has something new and no frame in flight. |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 554 | pub fn render(state: &mut State) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 555 | let Some(drm) = state.drm.as_mut() else { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 556 | return; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 557 | }; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 558 | let cursor = std::mem::take(&mut state.cursor_moved); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 559 | let all = std::mem::take(&mut state.needs_redraw); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 560 | for s in drm.surfaces.values_mut() { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 561 | s.redraw |= all; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 562 | s.cursor_redraw |= cursor; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 563 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 564 | let ready: Vec<crtc::Handle> = drm |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 565 | .surfaces |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 566 | .iter() |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 567 | .filter(|(_, s)| (s.redraw || s.cursor_redraw) && !s.queued) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 568 | .map(|(c, _)| *c) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 569 | .collect(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 570 | for crtc in ready { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 571 | schedule(state, crtc); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 572 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 573 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 574 | |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 575 | /// Draws one output for the vblank at `target`. Returns whether it still |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 576 | /// animates without a frame in flight, and so needs another render. |
30aece3a63ce | atagen | 2026-10-16 13:48 UTC | 577 | #[profiling::function] |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 578 | fn render_surface(state: &mut State, crtc: crtc::Handle, target: u64) -> bool { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 579 | let State { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 580 | drm: Some(drm), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 581 | renderer, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 582 | layout, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 583 | windows, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 584 | anim, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 585 | borders, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 586 | config, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 587 | space, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 588 | start, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 589 | cursor, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 590 | cursor_status, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 591 | pointer_pos, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 592 | .. |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 593 | } = state |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 594 | else { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 595 | return false; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 596 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 597 | if !drm.session.is_active() { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 598 | return false; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 599 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 600 | let Some(surface) = drm.surfaces.get_mut(&crtc) else { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 601 | return false; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 602 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 603 | if surface.queued { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 604 | return false; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 605 | } |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 606 | surface.cursor_redraw = false; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 607 | let want_vrr = surface.vrr_capable |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 608 | && match config.output.vrr { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 609 | Vrr::Off => false, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 610 | Vrr::On => true, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 611 | Vrr::Fullscreen => layout.arrange(surface.id).fullscreen.is_some(), |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 612 | }; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 613 | if want_vrr != surface.vrr_on { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 614 | match surface.drm.with_compositor(|c| c.use_vrr(want_vrr)) { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 615 | Ok(()) => { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 616 | surface.vrr_on = want_vrr; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 617 | tracing::info!( |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 618 | output = surface.output.name(), |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 619 | vrr = want_vrr, |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 620 | "adaptive sync" |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 621 | ); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 622 | } |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 623 | Err(e) => { |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 624 | tracing::warn!("could not switch adaptive sync: {e}"); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 625 | surface.vrr_capable = false; |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 626 | } |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 627 | } |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 628 | } |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 629 | let began = std::time::Instant::now(); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 630 | let now = target; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 631 | let Some(area) = layout.output(surface.id).map(|o| o.area) else { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 632 | return false; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 633 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 634 | let mut elements = Vec::new(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 635 | let local = *pointer_pos - Point::from((area.x, area.y)); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 636 | if local.x >= 0.0 && local.y >= 0.0 && local.x < area.w && local.y < area.h { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 637 | elements.extend(cursor.elements(renderer, cursor_status, local)); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 638 | } |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 639 | let scene_began = std::time::Instant::now(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 640 | elements.extend(crate::scene::build( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 641 | layout, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 642 | surface.id, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 643 | windows, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 644 | anim, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 645 | renderer, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 646 | now, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 647 | &config.decoration, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 648 | borders, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 649 | )); |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 650 | let scene_ns = scene_began.elapsed().as_nanos() as u64; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 651 | let animating = anim.active(now); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 652 | anim.prune(now); |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 653 | let render_began = std::time::Instant::now(); |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 654 | let cpu_began = thread_cpu_ns(); |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 655 | let mut queue_ns = 0; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 656 | match surface |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 657 | .drm |
fdb16f42492e | atagen | 2026-10-16 13:40 UTC | 658 | .render_frame(renderer, &elements, CLEAR, drm.frame_flags) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 659 | { |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 660 | Ok(result) if !result.is_empty => { |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 661 | let queue_began = std::time::Instant::now(); |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 662 | match surface.drm.queue_frame(()) { |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 663 | Ok(()) => surface.queued = true, |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 664 | Err(e) => tracing::warn!("queueing a frame failed: {e}"), |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 665 | } |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 666 | queue_ns = queue_began.elapsed().as_nanos() as u64; |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 667 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 668 | Ok(_) => {} |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 669 | Err(e) => tracing::warn!("rendering failed: {e}"), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 670 | } |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 671 | let render_cpu_ns = thread_cpu_ns() - cpu_began; |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 672 | let render_ns = render_began.elapsed().as_nanos() as u64 - queue_ns; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 673 | let took = began.elapsed().as_nanos() as u64; |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 674 | surface.budget.record(took); |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 675 | if took > surface.refresh_ns / 2 { |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 676 | tracing::debug!( |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 677 | output = surface.output.name(), |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 678 | took_ns = took, |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 679 | scene_ns, |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 680 | render_ns, |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 681 | render_cpu_ns, |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 682 | queue_ns, |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 683 | "slow frame" |
32fbd7e2725c | atagen | 2026-10-16 13:19 UTC | 684 | ); |
48af562d968b | atagen | 2026-10-16 12:39 UTC | 685 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 686 | if !surface.queued { |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 687 | surface.target_ns = None; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 688 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 689 | surface.redraw = animating; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 690 | let elapsed = start.elapsed(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 691 | let output = surface.output.clone(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 692 | for window in space.elements_for_output(&output) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 693 | window.send_frame(&output, elapsed, Some(Duration::ZERO), |_, _| { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 694 | Some(output.clone()) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 695 | }); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 696 | } |
6482fe98839d | atagen | 2026-10-16 13:01 UTC | 697 | animating && !surface.queued |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 698 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 699 | |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 700 | /// CPU time this thread has used, to tell a render that works from one that |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 701 | /// waits or is descheduled. |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 702 | fn thread_cpu_ns() -> u64 { |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 703 | let t = smithay::reexports::rustix::time::clock_gettime( |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 704 | smithay::reexports::rustix::time::ClockId::ThreadCPUTime, |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 705 | ); |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 706 | t.tv_sec as u64 * 1_000_000_000 + t.tv_nsec as u64 |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 707 | } |
d96bfa1d0892 | atagen | 2026-10-16 13:38 UTC | 708 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 709 | fn on_input(state: &mut State, event: InputEvent<LibinputInputBackend>) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 710 | match event { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 711 | InputEvent::Keyboard { event } => { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 712 | let time = event.time(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 713 | state.key(event.key_code(), event.state(), time); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 714 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 715 | InputEvent::PointerMotion { event } => { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 716 | let pos = clamp(state, state.pointer_pos + event.delta()); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 717 | state.pointer_to(pos, event.time()); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 718 | state.pointer_frame(); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 719 | state.cursor_moved = true; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 720 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 721 | InputEvent::PointerMotionAbsolute { event } => { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 722 | let Some(area) = state.layout.outputs().next().map(|(_, o)| o.area) else { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 723 | return; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 724 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 725 | let p = event.position_transformed((area.w as i32, area.h as i32).into()); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 726 | let pos = Point::from((area.x + p.x, area.y + p.y)); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 727 | state.pointer_to(pos, event.time()); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 728 | state.pointer_frame(); |
8e6f96f0a32d | atagen | 2026-10-16 22:17 UTC | 729 | state.cursor_moved = true; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 730 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 731 | InputEvent::PointerButton { event } => { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 732 | let time = event.time(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 733 | state.pointer_button(event.button_code(), event.state(), time); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 734 | state.pointer_frame(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 735 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 736 | InputEvent::PointerAxis { event } => { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 737 | let source = event.source(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 738 | let mut frame = AxisFrame::new(event.time()).source(source); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 739 | for axis in [Axis::Horizontal, Axis::Vertical] { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 740 | if let Some(v120) = event.amount_v120(axis) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 741 | frame = frame.v120(axis, v120 as i32); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 742 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 743 | let amount = event |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 744 | .amount(axis) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 745 | .or_else(|| event.amount_v120(axis).map(|v| v * 15.0 / 120.0)); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 746 | match amount { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 747 | Some(a) if a != 0.0 => frame = frame.value(axis, a), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 748 | Some(_) if source == AxisSource::Finger => frame = frame.stop(axis), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 749 | _ => {} |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 750 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 751 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 752 | state.pointer_axis(frame); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 753 | state.pointer_frame(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 754 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 755 | _ => {} |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 756 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 757 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 758 | |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 759 | /// Keeps the pointer on some output, sliding along the edge it hit. |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 760 | fn clamp( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 761 | state: &State, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 762 | pos: Point<f64, smithay::utils::Logical>, |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 763 | ) -> Point<f64, smithay::utils::Logical> { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 764 | let inside = |a: &Rect, p: Point<f64, smithay::utils::Logical>| { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 765 | p.x >= a.x && p.y >= a.y && p.x < a.right() && p.y < a.bottom() |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 766 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 767 | let areas: Vec<Rect> = state.layout.outputs().map(|(_, o)| o.area).collect(); |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 768 | if areas.iter().any(|a| inside(a, pos)) { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 769 | return pos; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 770 | } |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 771 | let Some(a) = areas |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 772 | .iter() |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 773 | .find(|a| inside(a, state.pointer_pos)) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 774 | .or(areas.first()) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 775 | else { |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 776 | return pos; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 777 | }; |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 778 | Point::from(( |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 779 | pos.x.clamp(a.x, a.right() - 1.0), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 780 | pos.y.clamp(a.y, a.bottom() - 1.0), |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 781 | )) |
0c1685f1d412 | atagen | 2026-10-16 12:18 UTC | 782 | } |