tty: Avoid modeset on adding device if possible
Browse files at this commit63f58086b9783fd80839ef35df82b05682c1c495Parent: a19326f1 changed file +186 -3
- src/backend/tty.rs +186 -3
src/backend/tty.rsmodified+186 -3
| @@ -40,9 +40,11 @@ use smithay::desktop::utils::OutputPresentationFeedback; | |||
| 40 | use smithay::output::{Mode, Output, OutputModeSource, PhysicalProperties}; | 40 | use smithay::output::{Mode, Output, OutputModeSource, PhysicalProperties}; |
| 41 | use smithay::reexports::calloop::timer::{TimeoutAction, Timer}; | 41 | use smithay::reexports::calloop::timer::{TimeoutAction, Timer}; |
| 42 | use smithay::reexports::calloop::{Dispatcher, LoopHandle, RegistrationToken}; | 42 | use smithay::reexports::calloop::{Dispatcher, LoopHandle, RegistrationToken}; |
| 43 | +use smithay::reexports::drm::control::atomic::AtomicModeReq; | ||
| 44 | +use smithay::reexports::drm::control::dumbbuffer::DumbBuffer; | ||
| 43 | use smithay::reexports::drm::control::{ | 45 | use smithay::reexports::drm::control::{ |
| 44 | - self, connector, crtc, property, Device, Mode as DrmMode, ModeFlags, ModeTypeFlags, | 46 | + self, connector, crtc, plane, property, AtomicCommitFlags, Device, Mode as DrmMode, ModeFlags, |
| 45 | - ResourceHandle, | 47 | + ModeTypeFlags, PlaneType, ResourceHandle, |
| 46 | }; | 48 | }; |
| 47 | use smithay::reexports::gbm::Modifier; | 49 | use smithay::reexports::gbm::Modifier; |
| 48 | use smithay::reexports::input::Libinput; | 50 | use smithay::reexports::input::Libinput; |
| @@ -219,6 +221,154 @@ impl OutputDevice { | |||
| 219 | }; | 221 | }; |
| 220 | info.name.clone() | 222 | info.name.clone() |
| 221 | } | 223 | } |
| 224 | + | ||
| 225 | + fn cleanup_disconnected_resources( | ||
| 226 | + &self, | ||
| 227 | + should_be_off: &dyn Fn(crtc::Handle, &connector::Info) -> bool, | ||
| 228 | + ) -> anyhow::Result<()> { | ||
| 229 | + let _span = tracy_client::span!("OutputDevice::cleanup_disconnected_resources"); | ||
| 230 | + | ||
| 231 | + let mut req = AtomicModeReq::new(); | ||
| 232 | + let plane_handles = self | ||
| 233 | + .drm | ||
| 234 | + .plane_handles() | ||
| 235 | + .context("error getting plane handles")?; | ||
| 236 | + | ||
| 237 | + let mut cleanup = HashSet::new(); | ||
| 238 | + let mut should_be_off_conn = HashSet::new(); | ||
| 239 | + | ||
| 240 | + for (conn, info) in self.drm_scanner.connectors() { | ||
| 241 | + let Some(enc) = info.current_encoder() else { | ||
| 242 | + // Not enabled. | ||
| 243 | + continue; | ||
| 244 | + }; | ||
| 245 | + | ||
| 246 | + let enc = match self.drm.get_encoder(enc) { | ||
| 247 | + Ok(x) => x, | ||
| 248 | + Err(err) => { | ||
| 249 | + debug!("couldn't get encoder: {err:?}"); | ||
| 250 | + continue; | ||
| 251 | + } | ||
| 252 | + }; | ||
| 253 | + | ||
| 254 | + let Some(crtc) = enc.crtc() else { | ||
| 255 | + // Encoder has no CRTC? | ||
| 256 | + continue; | ||
| 257 | + }; | ||
| 258 | + | ||
| 259 | + // All Connected connectors are returned by the DrmScanner and will be attempted for | ||
| 260 | + // connection. | ||
| 261 | + if info.state() == connector::State::Connected { | ||
| 262 | + // But we also need to clear the connectors that should be off according to our | ||
| 263 | + // config, since those ones will not be cleared elsewhere. | ||
| 264 | + if !should_be_off(crtc, info) { | ||
| 265 | + continue; | ||
| 266 | + } | ||
| 267 | + should_be_off_conn.insert(conn); | ||
| 268 | + } | ||
| 269 | + | ||
| 270 | + cleanup.insert(crtc); | ||
| 271 | + | ||
| 272 | + // Clear the connector. | ||
| 273 | + let Some((crtc_id, _, _)) = find_drm_property(&self.drm, *conn, "CRTC_ID") else { | ||
| 274 | + debug!("couldn't find connector CRTC_ID property"); | ||
| 275 | + continue; | ||
| 276 | + }; | ||
| 277 | + | ||
| 278 | + req.add_property(*conn, crtc_id, property::Value::CRTC(None)); | ||
| 279 | + } | ||
| 280 | + | ||
| 281 | + // Don't cleanup CRTCs that also correspond to some connected connectors. | ||
| 282 | + for (conn, crtc) in self.drm_scanner.crtcs() { | ||
| 283 | + // If the connector is enabled, but we're about to disable it, then it will be present | ||
| 284 | + // in the DrmScanner; keep it in the cleanup list. | ||
| 285 | + if should_be_off_conn.contains(&conn.handle()) { | ||
| 286 | + continue; | ||
| 287 | + } | ||
| 288 | + | ||
| 289 | + cleanup.remove(&crtc); | ||
| 290 | + } | ||
| 291 | + | ||
| 292 | + // Legacy fallback. | ||
| 293 | + if !self.drm.is_atomic() { | ||
| 294 | + if let Ok(res_handles) = self.drm.resource_handles() { | ||
| 295 | + for crtc in res_handles.crtcs() { | ||
| 296 | + #[allow(deprecated)] | ||
| 297 | + let _ = self.drm.set_cursor(*crtc, Option::<&DumbBuffer>::None); | ||
| 298 | + } | ||
| 299 | + } | ||
| 300 | + for crtc in cleanup { | ||
| 301 | + let _ = self.drm.set_crtc(crtc, None, (0, 0), &[], None); | ||
| 302 | + } | ||
| 303 | + return Ok(()); | ||
| 304 | + } | ||
| 305 | + | ||
| 306 | + // Disable non-primary planes, and planes belonging to disabled CRTCs. | ||
| 307 | + let is_primary = |plane: plane::Handle| { | ||
| 308 | + if let Some((_, info, value)) = find_drm_property(&self.drm, plane, "type") { | ||
| 309 | + match info.value_type().convert_value(value) { | ||
| 310 | + property::Value::Enum(Some(val)) => val.value() == PlaneType::Primary as u64, | ||
| 311 | + _ => false, | ||
| 312 | + } | ||
| 313 | + } else { | ||
| 314 | + debug!("couldn't find plane type property"); | ||
| 315 | + false | ||
| 316 | + } | ||
| 317 | + }; | ||
| 318 | + | ||
| 319 | + for plane in plane_handles { | ||
| 320 | + let info = match self.drm.get_plane(plane) { | ||
| 321 | + Ok(x) => x, | ||
| 322 | + Err(err) => { | ||
| 323 | + debug!("error getting plane: {err:?}"); | ||
| 324 | + continue; | ||
| 325 | + } | ||
| 326 | + }; | ||
| 327 | + | ||
| 328 | + let Some(crtc) = info.crtc() else { | ||
| 329 | + continue; | ||
| 330 | + }; | ||
| 331 | + | ||
| 332 | + if !cleanup.contains(&crtc) && is_primary(plane) { | ||
| 333 | + continue; | ||
| 334 | + } | ||
| 335 | + | ||
| 336 | + let Some((crtc_id, _, _)) = find_drm_property(&self.drm, plane, "CRTC_ID") else { | ||
| 337 | + debug!("couldn't find plane CRTC_ID property"); | ||
| 338 | + continue; | ||
| 339 | + }; | ||
| 340 | + | ||
| 341 | + let Some((fb_id, _, _)) = find_drm_property(&self.drm, plane, "FB_ID") else { | ||
| 342 | + debug!("couldn't find plane FB_ID property"); | ||
| 343 | + continue; | ||
| 344 | + }; | ||
| 345 | + | ||
| 346 | + req.add_property(plane, crtc_id, property::Value::CRTC(None)); | ||
| 347 | + req.add_property(plane, fb_id, property::Value::Framebuffer(None)); | ||
| 348 | + } | ||
| 349 | + | ||
| 350 | + // Disable the CRTCs. | ||
| 351 | + for crtc in cleanup { | ||
| 352 | + let Some((mode_id, _, _)) = find_drm_property(&self.drm, crtc, "MODE_ID") else { | ||
| 353 | + debug!("couldn't find CRTC MODE_ID property"); | ||
| 354 | + continue; | ||
| 355 | + }; | ||
| 356 | + | ||
| 357 | + let Some((active, _, _)) = find_drm_property(&self.drm, crtc, "ACTIVE") else { | ||
| 358 | + debug!("couldn't find CRTC ACTIVE property"); | ||
| 359 | + continue; | ||
| 360 | + }; | ||
| 361 | + | ||
| 362 | + req.add_property(crtc, mode_id, property::Value::Unknown(0)); | ||
| 363 | + req.add_property(crtc, active, property::Value::Boolean(false)); | ||
| 364 | + } | ||
| 365 | + | ||
| 366 | + self.drm | ||
| 367 | + .atomic_commit(AtomicCommitFlags::ALLOW_MODESET, req) | ||
| 368 | + .context("error doing atomic commit")?; | ||
| 369 | + | ||
| 370 | + Ok(()) | ||
| 371 | + } | ||
| 222 | } | 372 | } |
| 223 | 373 | ||
| 224 | #[derive(Debug, Clone, Copy)] | 374 | #[derive(Debug, Clone, Copy)] |
| @@ -571,7 +721,7 @@ impl Tty { | |||
| 571 | 721 | ||
| 572 | let (drm, drm_notifier) = { | 722 | let (drm, drm_notifier) = { |
| 573 | let _span = tracy_client::span!("DrmDevice::new"); | 723 | let _span = tracy_client::span!("DrmDevice::new"); |
| 574 | - DrmDevice::new(device_fd.clone(), true) | 724 | + DrmDevice::new(device_fd.clone(), false) |
| 575 | }?; | 725 | }?; |
| 576 | let gbm = { | 726 | let gbm = { |
| 577 | let _span = tracy_client::span!("GbmDevice::new"); | 727 | let _span = tracy_client::span!("GbmDevice::new"); |
| @@ -727,6 +877,11 @@ impl Tty { | |||
| 727 | return; | 877 | return; |
| 728 | }; | 878 | }; |
| 729 | 879 | ||
| 880 | + // If the DrmScanner connectors is empty here, then this will be the very first scan after | ||
| 881 | + // the device had just been added. | ||
| 882 | + let just_created = device.drm_scanner.connectors().is_empty(); | ||
| 883 | + | ||
| 884 | + // DrmScanner will preserve any existing connector-CRTC mapping. | ||
| 730 | let scan_result = match device.drm_scanner.scan_connectors(&device.drm) { | 885 | let scan_result = match device.drm_scanner.scan_connectors(&device.drm) { |
| 731 | Ok(x) => x, | 886 | Ok(x) => x, |
| 732 | Err(err) => { | 887 | Err(err) => { |
| @@ -810,6 +965,34 @@ impl Tty { | |||
| 810 | device.known_crtcs.insert(crtc, info); | 965 | device.known_crtcs.insert(crtc, info); |
| 811 | } | 966 | } |
| 812 | 967 | ||
| 968 | + // If the device was just added, we need to cleanup any disconnected connectors and planes. | ||
| 969 | + if just_created { | ||
| 970 | + let device = self.devices.get(&node).unwrap(); | ||
| 971 | + | ||
| 972 | + // Follow the logic in on_output_config_changed(). | ||
| 973 | + let disable_laptop_panels = self.should_disable_laptop_panels(niri.is_lid_closed); | ||
| 974 | + let should_disable = |conn: &str| disable_laptop_panels && is_laptop_panel(conn); | ||
| 975 | + | ||
| 976 | + let config = self.config.borrow(); | ||
| 977 | + let disable_monitor_names = config.debug.disable_monitor_names; | ||
| 978 | + | ||
| 979 | + let should_be_off = |crtc, conn: &connector::Info| { | ||
| 980 | + let output_name = device.known_crtc_name(&crtc, conn, disable_monitor_names); | ||
| 981 | + | ||
| 982 | + let config = config | ||
| 983 | + .outputs | ||
| 984 | + .find(&output_name) | ||
| 985 | + .cloned() | ||
| 986 | + .unwrap_or_default(); | ||
| 987 | + | ||
| 988 | + config.off || should_disable(&output_name.connector) | ||
| 989 | + }; | ||
| 990 | + | ||
| 991 | + if let Err(err) = device.cleanup_disconnected_resources(&should_be_off) { | ||
| 992 | + warn!("error cleaning up connectors: {err:?}"); | ||
| 993 | + } | ||
| 994 | + } | ||
| 995 | + | ||
| 813 | // This will connect any new connectors if needed, and apply other changes, such as | 996 | // This will connect any new connectors if needed, and apply other changes, such as |
| 814 | // connecting back the internal laptop monitor once it becomes the only monitor left. | 997 | // connecting back the internal laptop monitor once it becomes the only monitor left. |
| 815 | // | 998 | // |