tty: Avoid modeset on session resume
Browse files at this commit6fcdb9242e65ed5825e7fda2a7935b412588b63aParent: 1ae48981 changed file +43 -14
- src/backend/tty.rs +43 -14
src/backend/tty.rsmodified+43 -14
| @@ -222,7 +222,7 @@ impl OutputDevice { | |||
| 222 | info.name.clone() | 222 | info.name.clone() |
| 223 | } | 223 | } |
| 224 | 224 | ||
| 225 | - fn cleanup_disconnected_resources( | 225 | + fn cleanup_mismatching_resources( |
| 226 | &self, | 226 | &self, |
| 227 | should_be_off: &dyn Fn(crtc::Handle, &connector::Info) -> bool, | 227 | should_be_off: &dyn Fn(crtc::Handle, &connector::Info) -> bool, |
| 228 | ) -> anyhow::Result<()> { | 228 | ) -> anyhow::Result<()> { |
| @@ -246,7 +246,29 @@ impl OutputDevice { | |||
| 246 | for (conn, info) in self.drm_scanner.connectors() { | 246 | for (conn, info) in self.drm_scanner.connectors() { |
| 247 | // We only keep the connector if it has a CRTC and the output isn't off in niri. | 247 | // We only keep the connector if it has a CRTC and the output isn't off in niri. |
| 248 | if let Some(crtc) = self.drm_scanner.crtc_for_connector(conn) { | 248 | if let Some(crtc) = self.drm_scanner.crtc_for_connector(conn) { |
| 249 | - if !should_be_off(crtc, info) { | 249 | + // Verify that the connector's current CRTC matches the CRTC we expect. If not, |
| 250 | + // clear the CRTC and the connector so that all connectors can get the expected | ||
| 251 | + // CRTCs afterwards. (We do this because we do not handle CRTC rotations across TTY | ||
| 252 | + // switches.) | ||
| 253 | + let mut has_different_crtc = false; | ||
| 254 | + if let Some(enc) = info.current_encoder() { | ||
| 255 | + match self.drm.get_encoder(enc) { | ||
| 256 | + Ok(enc) => { | ||
| 257 | + if let Some(current_crtc) = enc.crtc() { | ||
| 258 | + if current_crtc != crtc { | ||
| 259 | + has_different_crtc = true; | ||
| 260 | + } | ||
| 261 | + } | ||
| 262 | + } | ||
| 263 | + Err(err) => { | ||
| 264 | + debug!("couldn't get encoder: {err:?}"); | ||
| 265 | + // Err on the safe side. | ||
| 266 | + has_different_crtc = true; | ||
| 267 | + } | ||
| 268 | + } | ||
| 269 | + } | ||
| 270 | + | ||
| 271 | + if !has_different_crtc && !should_be_off(crtc, info) { | ||
| 250 | // Keep the corresponding CRTC. | 272 | // Keep the corresponding CRTC. |
| 251 | cleanup.remove(&crtc); | 273 | cleanup.remove(&crtc); |
| 252 | continue; | 274 | continue; |
| @@ -512,7 +534,7 @@ impl Tty { | |||
| 512 | return; | 534 | return; |
| 513 | } | 535 | } |
| 514 | 536 | ||
| 515 | - self.device_changed(device_id, niri) | 537 | + self.device_changed(device_id, niri, false) |
| 516 | } | 538 | } |
| 517 | UdevEvent::Removed { device_id } => { | 539 | UdevEvent::Removed { device_id } => { |
| 518 | if !self.session.is_active() { | 540 | if !self.session.is_active() { |
| @@ -599,7 +621,7 @@ impl Tty { | |||
| 599 | 621 | ||
| 600 | // It hasn't been removed, update its state as usual. | 622 | // It hasn't been removed, update its state as usual. |
| 601 | let device = self.devices.get_mut(&node).unwrap(); | 623 | let device = self.devices.get_mut(&node).unwrap(); |
| 602 | - if let Err(err) = device.drm.activate(true) { | 624 | + if let Err(err) = device.drm.activate(false) { |
| 603 | warn!("error activating DRM device: {err:?}"); | 625 | warn!("error activating DRM device: {err:?}"); |
| 604 | } | 626 | } |
| 605 | if let Some(lease_state) = &mut device.drm_lease_state { | 627 | if let Some(lease_state) = &mut device.drm_lease_state { |
| @@ -607,7 +629,7 @@ impl Tty { | |||
| 607 | } | 629 | } |
| 608 | 630 | ||
| 609 | // Refresh the connectors. | 631 | // Refresh the connectors. |
| 610 | - self.device_changed(node.dev_id(), niri); | 632 | + self.device_changed(node.dev_id(), niri, true); |
| 611 | 633 | ||
| 612 | // Apply pending gamma changes and restore our existing gamma. | 634 | // Apply pending gamma changes and restore our existing gamma. |
| 613 | let device = self.devices.get_mut(&node).unwrap(); | 635 | let device = self.devices.get_mut(&node).unwrap(); |
| @@ -814,12 +836,12 @@ impl Tty { | |||
| 814 | }; | 836 | }; |
| 815 | assert!(self.devices.insert(node, device).is_none()); | 837 | assert!(self.devices.insert(node, device).is_none()); |
| 816 | 838 | ||
| 817 | - self.device_changed(device_id, niri); | 839 | + self.device_changed(device_id, niri, true); |
| 818 | 840 | ||
| 819 | Ok(()) | 841 | Ok(()) |
| 820 | } | 842 | } |
| 821 | 843 | ||
| 822 | - fn device_changed(&mut self, device_id: dev_t, niri: &mut Niri) { | 844 | + fn device_changed(&mut self, device_id: dev_t, niri: &mut Niri, cleanup: bool) { |
| 823 | debug!("device changed: {device_id}"); | 845 | debug!("device changed: {device_id}"); |
| 824 | 846 | ||
| 825 | let Ok(node) = DrmNode::from_dev_id(device_id) else { | 847 | let Ok(node) = DrmNode::from_dev_id(device_id) else { |
| @@ -851,10 +873,6 @@ impl Tty { | |||
| 851 | return; | 873 | return; |
| 852 | }; | 874 | }; |
| 853 | 875 | ||
| 854 | - // If the DrmScanner connectors is empty here, then this will be the very first scan after | ||
| 855 | - // the device had just been added. | ||
| 856 | - let just_created = device.drm_scanner.connectors().is_empty(); | ||
| 857 | - | ||
| 858 | // DrmScanner will preserve any existing connector-CRTC mapping. | 876 | // DrmScanner will preserve any existing connector-CRTC mapping. |
| 859 | let scan_result = match device.drm_scanner.scan_connectors(&device.drm) { | 877 | let scan_result = match device.drm_scanner.scan_connectors(&device.drm) { |
| 860 | Ok(x) => x, | 878 | Ok(x) => x, |
| @@ -939,8 +957,9 @@ impl Tty { | |||
| 939 | device.known_crtcs.insert(crtc, info); | 957 | device.known_crtcs.insert(crtc, info); |
| 940 | } | 958 | } |
| 941 | 959 | ||
| 942 | - // If the device was just added, we need to cleanup any disconnected connectors and planes. | 960 | + // If the device was just added or resumed, we need to cleanup any disconnected connectors |
| 943 | - if just_created { | 961 | + // and planes. |
| 962 | + if cleanup { | ||
| 944 | let device = self.devices.get(&node).unwrap(); | 963 | let device = self.devices.get(&node).unwrap(); |
| 945 | 964 | ||
| 946 | // Follow the logic in on_output_config_changed(). | 965 | // Follow the logic in on_output_config_changed(). |
| @@ -962,9 +981,19 @@ impl Tty { | |||
| 962 | config.off || should_disable(&output_name.connector) | 981 | config.off || should_disable(&output_name.connector) |
| 963 | }; | 982 | }; |
| 964 | 983 | ||
| 965 | - if let Err(err) = device.cleanup_disconnected_resources(&should_be_off) { | 984 | + if let Err(err) = device.cleanup_mismatching_resources(&should_be_off) { |
| 966 | warn!("error cleaning up connectors: {err:?}"); | 985 | warn!("error cleaning up connectors: {err:?}"); |
| 967 | } | 986 | } |
| 987 | + | ||
| 988 | + let device = self.devices.get_mut(&node).unwrap(); | ||
| 989 | + for surface in device.surfaces.values_mut() { | ||
| 990 | + // We aren't force-clearing the CRTCs, so we need to make the surfaces read the | ||
| 991 | + // updated state after a session resume. This also causes a full damage for the | ||
| 992 | + // next redraw. | ||
| 993 | + if let Err(err) = surface.compositor.reset_state() { | ||
| 994 | + warn!("error resetting DrmCompositor state: {err:?}"); | ||
| 995 | + } | ||
| 996 | + } | ||
| 968 | } | 997 | } |
| 969 | 998 | ||
| 970 | // This will connect any new connectors if needed, and apply other changes, such as | 999 | // This will connect any new connectors if needed, and apply other changes, such as |