forge
Explore
Log in

Don't you dare go Hollow

Update for Smithay VRR changes

Browse files at this commit

e6d8932b3bd0486bf56309f3578f809625abc9a2Ivan MolodetskikhParent: 70f96cc
src/backend/tty.rsmodified+70 -148
@@ -20,7 +20,7 @@ use smithay::backend::allocator::gbm::{GbmAllocator, GbmBufferFlags, GbmDevice};
20 use smithay::backend::allocator::Fourcc;20 use smithay::backend::allocator::Fourcc;
21 use smithay::backend::drm::compositor::{DrmCompositor, PrimaryPlaneElement};21 use smithay::backend::drm::compositor::{DrmCompositor, PrimaryPlaneElement};
22 use smithay::backend::drm::{22 use smithay::backend::drm::{
23- DrmDevice, DrmDeviceFd, DrmEvent, DrmEventMetadata, DrmEventTime, DrmNode, NodeType,23+ DrmDevice, DrmDeviceFd, DrmEvent, DrmEventMetadata, DrmEventTime, DrmNode, NodeType, VrrSupport,
24 };24 };
25 use smithay::backend::egl::context::ContextPriority;25 use smithay::backend::egl::context::ContextPriority;
26 use smithay::backend::egl::{EGLDevice, EGLDisplay};26 use smithay::backend::egl::{EGLDevice, EGLDisplay};
@@ -184,7 +184,6 @@ struct Surface {
184 gamma_props: Option<GammaProps>,184 gamma_props: Option<GammaProps>,
185 /// Gamma change to apply upon session resume.185 /// Gamma change to apply upon session resume.
186 pending_gamma_change: Option<Option<Vec<u16>>>,186 pending_gamma_change: Option<Option<Vec<u16>>>,
187- vrr_enabled: bool,
188 /// Tracy frame that goes from vblank to vblank.187 /// Tracy frame that goes from vblank to vblank.
189 vblank_frame: Option<tracy_client::Frame>,188 vblank_frame: Option<tracy_client::Frame>,
190 /// Frame name for the VBlank frame.189 /// Frame name for the VBlank frame.
@@ -405,8 +404,6 @@ impl Tty {
405 self.device_changed(node.dev_id(), niri);404 self.device_changed(node.dev_id(), niri);
406 405
407 // Apply pending gamma changes and restore our existing gamma.406 // Apply pending gamma changes and restore our existing gamma.
408- //
409- // Also, restore our VRR.
410 let device = self.devices.get_mut(&node).unwrap();407 let device = self.devices.get_mut(&node).unwrap();
411 for (crtc, surface) in device.surfaces.iter_mut() {408 for (crtc, surface) in device.surfaces.iter_mut() {
412 if let Some(ramp) = surface.pending_gamma_change.take() {409 if let Some(ramp) = surface.pending_gamma_change.take() {
@@ -424,33 +421,6 @@ impl Tty {
424 warn!("error restoring gamma: {err:?}");421 warn!("error restoring gamma: {err:?}");
425 }422 }
426 }423 }
427-
428- // Restore VRR.
429- let output = niri
430- .global_space
431- .outputs()
432- .find(|output| {
433- let tty_state: &TtyOutputState = output.user_data().get().unwrap();
434- tty_state.node == node && tty_state.crtc == *crtc
435- })
436- .cloned();
437- let Some(output) = output else {
438- error!("missing output for crtc: {crtc:?}");
439- continue;
440- };
441- let Some(output_state) = niri.output_state.get_mut(&output) else {
442- error!("missing state for output {:?}", surface.name.connector);
443- continue;
444- };
445-
446- try_to_change_vrr(
447- &device.drm,
448- surface.connector,
449- *crtc,
450- surface,
451- output_state,
452- surface.vrr_enabled,
453- );
454 }424 }
455 }425 }
456 426
@@ -822,45 +792,6 @@ impl Tty {
822 Err(err) => debug!("error setting max bpc: {err:?}"),792 Err(err) => debug!("error setting max bpc: {err:?}"),
823 }793 }
824 794
825- // Try to enable VRR if requested.
826- let mut vrr_enabled = false;
827- if let Some(capable) = is_vrr_capable(&device.drm, connector.handle()) {
828- if capable {
829- // Even if on-demand, we still disable it until later checks.
830- let vrr = config.is_vrr_always_on();
831- let word = if vrr { "enabling" } else { "disabling" };
832-
833- match set_vrr_enabled(&device.drm, crtc, vrr) {
834- Ok(enabled) => {
835- if enabled != vrr {
836- warn!("failed {} VRR", word);
837- }
838-
839- vrr_enabled = enabled;
840- }
841- Err(err) => {
842- warn!("error {} VRR: {err:?}", word);
843- }
844- }
845- } else {
846- if !config.is_vrr_always_off() {
847- warn!("cannot enable VRR because connector is not vrr_capable");
848- }
849-
850- // Try to disable it anyway to work around a bug where resetting DRM state causes
851- // vrr_capable to be reset to 0, potentially leaving VRR_ENABLED at 1.
852- let res = set_vrr_enabled(&device.drm, crtc, false);
853- if matches!(res, Ok(true)) {
854- warn!("error disabling VRR");
855-
856- // So that we can try it again later.
857- vrr_enabled = true;
858- }
859- }
860- } else if !config.is_vrr_always_off() {
861- warn!("cannot enable VRR because connector is not vrr_capable");
862- }
863-
864 let mut gamma_props = GammaProps::new(&device.drm, crtc)795 let mut gamma_props = GammaProps::new(&device.drm, crtc)
865 .map_err(|err| debug!("error getting gamma properties: {err:?}"))796 .map_err(|err| debug!("error getting gamma properties: {err:?}"))
866 .ok();797 .ok();
@@ -879,6 +810,31 @@ impl Tty {
879 .drm810 .drm
880 .create_surface(crtc, mode, &[connector.handle()])?;811 .create_surface(crtc, mode, &[connector.handle()])?;
881 812
813+ // Try to enable VRR if requested.
814+ match surface.vrr_supported(connector.handle()) {
815+ Ok(VrrSupport::Supported | VrrSupport::RequiresModeset) => {
816+ // Even if on-demand, we still disable it until later checks.
817+ let vrr = config.is_vrr_always_on();
818+ let word = if vrr { "enabling" } else { "disabling" };
819+
820+ if let Err(err) = surface.use_vrr(vrr) {
821+ warn!("error {} VRR: {err:?}", word);
822+ }
823+ }
824+ Ok(VrrSupport::NotSupported) => {
825+ if !config.is_vrr_always_off() {
826+ warn!("cannot enable VRR because connector does not support it");
827+ }
828+
829+ // Try to disable it anyway to work around a bug where resetting DRM state causes
830+ // vrr_capable to be reset to 0, potentially leaving VRR_ENABLED at 1.
831+ let _ = surface.use_vrr(false);
832+ }
833+ Err(err) => {
834+ warn!("error querying for VRR support: {err:?}");
835+ }
836+ }
837+
882 // Create GBM allocator.838 // Create GBM allocator.
883 let gbm_flags = GbmBufferFlags::RENDERING | GbmBufferFlags::SCANOUT;839 let gbm_flags = GbmBufferFlags::RENDERING | GbmBufferFlags::SCANOUT;
884 let allocator = GbmAllocator::new(device.gbm.clone(), gbm_flags);840 let allocator = GbmAllocator::new(device.gbm.clone(), gbm_flags);
@@ -1038,6 +994,8 @@ impl Tty {
1038 }994 }
1039 }995 }
1040 996
997+ let vrr_enabled = compositor.vrr_enabled();
998+
1041 let vblank_frame_name =999 let vblank_frame_name =
1042 tracy_client::FrameName::new_leak(format!("vblank on {connector_name}"));1000 tracy_client::FrameName::new_leak(format!("vblank on {connector_name}"));
1043 let time_since_presentation_plot_name = tracy_client::PlotName::new_leak(format!(1001 let time_since_presentation_plot_name = tracy_client::PlotName::new_leak(format!(
@@ -1055,7 +1013,6 @@ impl Tty {
1055 compositor,1013 compositor,
1056 dmabuf_feedback,1014 dmabuf_feedback,
1057 gamma_props,1015 gamma_props,
1058- vrr_enabled,
1059 pending_gamma_change: None,1016 pending_gamma_change: None,
1060 vblank_frame: None,1017 vblank_frame: None,
1061 vblank_frame_name,1018 vblank_frame_name,
@@ -1622,8 +1579,17 @@ impl Tty {
1622 }1579 }
1623 }1580 }
1624 1581
1625- let vrr_supported = is_vrr_capable(&device.drm, connector.handle()) == Some(true);1582+ let vrr_supported = surface
1626- let vrr_enabled = surface.map_or(false, |surface| surface.vrr_enabled);1583+ .map(|surface| {
1584+ matches!(
1585+ surface.compositor.vrr_supported(connector.handle()),
1586+ Ok(VrrSupport::Supported | VrrSupport::RequiresModeset)
1587+ )
1588+ })
1589+ .unwrap_or_else(|| {
1590+ is_vrr_capable(&device.drm, connector.handle()) == Some(true)
1591+ });
1592+ let vrr_enabled = surface.map_or(false, |surface| surface.compositor.vrr_enabled());
1627 1593
1628 let logical = niri1594 let logical = niri
1629 .global_space1595 .global_space
@@ -1708,14 +1674,17 @@ impl Tty {
1708 for (&crtc, surface) in device.surfaces.iter_mut() {1674 for (&crtc, surface) in device.surfaces.iter_mut() {
1709 let tty_state: &TtyOutputState = output.user_data().get().unwrap();1675 let tty_state: &TtyOutputState = output.user_data().get().unwrap();
1710 if tty_state.node == node && tty_state.crtc == crtc {1676 if tty_state.node == node && tty_state.crtc == crtc {
1711- try_to_change_vrr(1677+ let word = if enable_vrr { "enabling" } else { "disabling" };
1712- &device.drm,1678+ if let Err(err) = surface.compositor.use_vrr(enable_vrr) {
1713- surface.connector,1679+ warn!(
1714- crtc,1680+ "output {:?}: error {} VRR: {err:?}",
1715- surface,1681+ surface.name.connector, word
1716- output_state,1682+ );
1717- enable_vrr,1683+ }
1718- );1684+ output_state
1685+ .frame_clock
1686+ .set_vrr(surface.compositor.vrr_enabled());
1687+
1719 self.refresh_ipc_outputs(niri);1688 self.refresh_ipc_outputs(niri);
1720 return;1689 return;
1721 }1690 }
@@ -1781,8 +1750,11 @@ impl Tty {
1781 };1750 };
1782 1751
1783 let change_mode = surface.compositor.pending_mode() != mode;1752 let change_mode = surface.compositor.pending_mode() != mode;
1784- let change_always_vrr = surface.vrr_enabled != config.is_vrr_always_on();1753+
1754+ let vrr_enabled = surface.compositor.vrr_enabled();
1755+ let change_always_vrr = vrr_enabled != config.is_vrr_always_on();
1785 let is_on_demand_vrr = config.is_vrr_on_demand();1756 let is_on_demand_vrr = config.is_vrr_on_demand();
1757+
1786 if !change_mode && !change_always_vrr && !is_on_demand_vrr {1758 if !change_mode && !change_always_vrr && !is_on_demand_vrr {
1787 continue;1759 continue;
1788 }1760 }
@@ -1804,17 +1776,20 @@ impl Tty {
1804 continue;1776 continue;
1805 };1777 };
1806 1778
1807- if (is_on_demand_vrr && surface.vrr_enabled != output_state.on_demand_vrr_enabled)1779+ if (is_on_demand_vrr && vrr_enabled != output_state.on_demand_vrr_enabled)
1808 || (!is_on_demand_vrr && change_always_vrr)1780 || (!is_on_demand_vrr && change_always_vrr)
1809 {1781 {
1810- try_to_change_vrr(1782+ let vrr = !vrr_enabled;
1811- &device.drm,1783+ let word = if vrr { "enabling" } else { "disabling" };
1812- connector.handle(),1784+ if let Err(err) = surface.compositor.use_vrr(vrr) {
1813- crtc,1785+ warn!(
1814- surface,1786+ "output {:?}: error {} VRR: {err:?}",
1815- output_state,1787+ surface.name.connector, word
1816- !surface.vrr_enabled,1788+ );
1817- );1789+ }
1790+ output_state
1791+ .frame_clock
1792+ .set_vrr(surface.compositor.vrr_enabled());
1818 }1793 }
1819 1794
1820 if change_mode {1795 if change_mode {
@@ -1846,8 +1821,10 @@ impl Tty {
1846 let wl_mode = Mode::from(mode);1821 let wl_mode = Mode::from(mode);
1847 output.change_current_state(Some(wl_mode), None, None, None);1822 output.change_current_state(Some(wl_mode), None, None, None);
1848 output.set_preferred(wl_mode);1823 output.set_preferred(wl_mode);
1849- output_state.frame_clock =1824+ output_state.frame_clock = FrameClock::new(
1850- FrameClock::new(Some(refresh_interval(mode)), surface.vrr_enabled);1825+ Some(refresh_interval(mode)),
1826+ surface.compositor.vrr_enabled(),
1827+ );
1851 niri.output_resized(&output);1828 niri.output_resized(&output);
1852 }1829 }
1853 }1830 }
@@ -2431,24 +2408,6 @@ fn is_vrr_capable(device: &DrmDevice, connector: connector::Handle) -> Option<bo
2431 info.value_type().convert_value(value).as_boolean()2408 info.value_type().convert_value(value).as_boolean()
2432 }2409 }
2433 2410
2434-fn set_vrr_enabled(device: &DrmDevice, crtc: crtc::Handle, enabled: bool) -> anyhow::Result<bool> {
2435- let (prop, info, _) =
2436- find_drm_property(device, crtc, "VRR_ENABLED").context("VRR_ENABLED property missing")?;
2437-
2438- let value = property::Value::UnsignedRange(if enabled { 1 } else { 0 });
2439- device
2440- .set_property(crtc, prop, value.into())
2441- .context("error setting VRR_ENABLED property")?;
2442-
2443- let value = get_drm_property(device, crtc, prop)
2444- .context("VRR_ENABLED property missing after setting")?;
2445- match info.value_type().convert_value(value) {
2446- property::Value::UnsignedRange(value) => Ok(value == 1),
2447- property::Value::Boolean(value) => Ok(value),
2448- _ => bail!("wrong VRR_ENABLED property type"),
2449- }
2450-}
2451-
2452 pub fn set_gamma_for_crtc(2411 pub fn set_gamma_for_crtc(
2453 device: &DrmDevice,2412 device: &DrmDevice,
2454 crtc: crtc::Handle,2413 crtc: crtc::Handle,
@@ -2494,43 +2453,6 @@ pub fn set_gamma_for_crtc(
2494 Ok(())2453 Ok(())
2495 }2454 }
2496 2455
2497-fn try_to_change_vrr(
2498- device: &DrmDevice,
2499- connector: connector::Handle,
2500- crtc: crtc::Handle,
2501- surface: &mut Surface,
2502- output_state: &mut crate::niri::OutputState,
2503- enable_vrr: bool,
2504-) {
2505- let _span = tracy_client::span!("try_to_change_vrr");
2506-
2507- if is_vrr_capable(device, connector) == Some(true) {
2508- let word = if enable_vrr { "enabling" } else { "disabling" };
2509-
2510- match set_vrr_enabled(device, crtc, enable_vrr) {
2511- Ok(enabled) => {
2512- if enabled != enable_vrr {
2513- warn!("output {:?}: failed {} VRR", surface.name.connector, word);
2514- }
2515-
2516- surface.vrr_enabled = enabled;
2517- output_state.frame_clock.set_vrr(enabled);
2518- }
2519- Err(err) => {
2520- warn!(
2521- "output {:?}: error {} VRR: {err:?}",
2522- surface.name.connector, word
2523- );
2524- }
2525- }
2526- } else if enable_vrr {
2527- warn!(
2528- "output {:?}: cannot enable VRR because connector is not vrr_capable",
2529- surface.name.connector
2530- );
2531- }
2532-}
2533-
2534 fn format_connector_name(connector: &connector::Info) -> String {2456 fn format_connector_name(connector: &connector::Info) -> String {
2535 format!(2457 format!(
2536 "{}-{}",2458 "{}-{}",