Restore VRR on TTY switch
Browse files at this commitf9fe86ee3e82ff02ac9f75869cc4219850ae3f4cParent: 2e671521 changed file +83 -26
- src/backend/tty.rs +83 -26
src/backend/tty.rsmodified+83 -26
| @@ -405,6 +405,8 @@ impl Tty { | ||
| 405 | 405 | self.device_changed(node.dev_id(), niri); |
| 406 | 406 | |
| 407 | 407 | // Apply pending gamma changes and restore our existing gamma. |
| 408 | + // | |
| 409 | + // Also, restore our VRR. | |
| 408 | 410 | let device = self.devices.get_mut(&node).unwrap(); |
| 409 | 411 | for (crtc, surface) in device.surfaces.iter_mut() { |
| 410 | 412 | if let Some(ramp) = surface.pending_gamma_change.take() { |
| @@ -422,6 +424,45 @@ impl Tty { | ||
| 422 | 424 | warn!("error restoring gamma: {err:?}"); |
| 423 | 425 | } |
| 424 | 426 | } |
| 427 | + | |
| 428 | + // Restore VRR. | |
| 429 | + let Some(connector) = | |
| 430 | + surface.compositor.pending_connectors().into_iter().next() | |
| 431 | + else { | |
| 432 | + error!("surface pending connectors is empty"); | |
| 433 | + continue; | |
| 434 | + }; | |
| 435 | + let Some(connector) = device.drm_scanner.connectors().get(&connector) | |
| 436 | + else { | |
| 437 | + error!("missing enabled connector in drm_scanner"); | |
| 438 | + continue; | |
| 439 | + }; | |
| 440 | + | |
| 441 | + let output = niri | |
| 442 | + .global_space | |
| 443 | + .outputs() | |
| 444 | + .find(|output| { | |
| 445 | + let tty_state: &TtyOutputState = output.user_data().get().unwrap(); | |
| 446 | + tty_state.node == node && tty_state.crtc == *crtc | |
| 447 | + }) | |
| 448 | + .cloned(); | |
| 449 | + let Some(output) = output else { | |
| 450 | + error!("missing output for crtc: {crtc:?}"); | |
| 451 | + continue; | |
| 452 | + }; | |
| 453 | + let Some(output_state) = niri.output_state.get_mut(&output) else { | |
| 454 | + error!("missing state for output {:?}", surface.name); | |
| 455 | + continue; | |
| 456 | + }; | |
| 457 | + | |
| 458 | + try_to_change_vrr( | |
| 459 | + &device.drm, | |
| 460 | + connector, | |
| 461 | + *crtc, | |
| 462 | + surface, | |
| 463 | + output_state, | |
| 464 | + surface.vrr_enabled, | |
| 465 | + ); | |
| 425 | 466 | } |
| 426 | 467 | } |
| 427 | 468 | |
| @@ -1671,32 +1712,14 @@ impl Tty { | ||
| 1671 | 1712 | }; |
| 1672 | 1713 | |
| 1673 | 1714 | if change_vrr { |
| 1674 | - if is_vrr_capable(&device.drm, connector.handle()) == Some(true) { | |
| 1675 | - let word = if config.variable_refresh_rate { | |
| 1676 | - "enabling" | |
| 1677 | - } else { | |
| 1678 | - "disabling" | |
| 1679 | - }; | |
| 1680 | - | |
| 1681 | - match set_vrr_enabled(&device.drm, crtc, config.variable_refresh_rate) { | |
| 1682 | - Ok(enabled) => { | |
| 1683 | - if enabled != config.variable_refresh_rate { | |
| 1684 | - warn!("output {:?}: failed {} VRR", surface.name, word); | |
| 1685 | - } | |
| 1686 | - | |
| 1687 | - surface.vrr_enabled = enabled; | |
| 1688 | - output_state.frame_clock.set_vrr(enabled); | |
| 1689 | - } | |
| 1690 | - Err(err) => { | |
| 1691 | - warn!("output {:?}: error {} VRR: {err:?}", surface.name, word); | |
| 1692 | - } | |
| 1693 | - } | |
| 1694 | - } else if config.variable_refresh_rate { | |
| 1695 | - warn!( | |
| 1696 | - "output {:?}: cannot enable VRR because connector is not vrr_capable", | |
| 1697 | - surface.name | |
| 1698 | - ); | |
| 1699 | - } | |
| 1715 | + try_to_change_vrr( | |
| 1716 | + &device.drm, | |
| 1717 | + connector, | |
| 1718 | + crtc, | |
| 1719 | + surface, | |
| 1720 | + output_state, | |
| 1721 | + config.variable_refresh_rate, | |
| 1722 | + ); | |
| 1700 | 1723 | } |
| 1701 | 1724 | |
| 1702 | 1725 | if change_mode { |
| @@ -2345,6 +2368,40 @@ pub fn set_gamma_for_crtc( | ||
| 2345 | 2368 | Ok(()) |
| 2346 | 2369 | } |
| 2347 | 2370 | |
| 2371 | +fn try_to_change_vrr( | |
| 2372 | + device: &DrmDevice, | |
| 2373 | + connector: &connector::Info, | |
| 2374 | + crtc: crtc::Handle, | |
| 2375 | + surface: &mut Surface, | |
| 2376 | + output_state: &mut crate::niri::OutputState, | |
| 2377 | + enable_vrr: bool, | |
| 2378 | +) { | |
| 2379 | + let _span = tracy_client::span!("try_to_change_vrr"); | |
| 2380 | + | |
| 2381 | + if is_vrr_capable(device, connector.handle()) == Some(true) { | |
| 2382 | + let word = if enable_vrr { "enabling" } else { "disabling" }; | |
| 2383 | + | |
| 2384 | + match set_vrr_enabled(device, crtc, enable_vrr) { | |
| 2385 | + Ok(enabled) => { | |
| 2386 | + if enabled != enable_vrr { | |
| 2387 | + warn!("output {:?}: failed {} VRR", surface.name, word); | |
| 2388 | + } | |
| 2389 | + | |
| 2390 | + surface.vrr_enabled = enabled; | |
| 2391 | + output_state.frame_clock.set_vrr(enabled); | |
| 2392 | + } | |
| 2393 | + Err(err) => { | |
| 2394 | + warn!("output {:?}: error {} VRR: {err:?}", surface.name, word); | |
| 2395 | + } | |
| 2396 | + } | |
| 2397 | + } else if enable_vrr { | |
| 2398 | + warn!( | |
| 2399 | + "output {:?}: cannot enable VRR because connector is not vrr_capable", | |
| 2400 | + surface.name | |
| 2401 | + ); | |
| 2402 | + } | |
| 2403 | +} | |
| 2404 | + | |
| 2348 | 2405 | #[cfg(test)] |
| 2349 | 2406 | mod tests { |
| 2350 | 2407 | use super::*; |