forge
Explore
Log in

Don't you dare go Hollow

feature: add on-demand vrr (#586)

Browse files at this commit

f1894f6f9a7470b8b1493cf96b28a48e8a704636UnverifiedMichael Yang and Ivan MolodetskikhParent: dfc2d45
* feature: add on-demand vrr

* Don't require connector::Info in try_to_set_vrr

* Improve VRR help message

* Rename connector_handle => connector

* Fix tracy span name

* Move on demand vrr flag set higher

* wiki: Mention on-demand VRR

---------
niri-config/src/lib.rsmodified+26 -4
@@ -324,11 +324,25 @@ pub struct Output {
324324 #[knuffel(child, unwrap(argument, str))]
325325 pub mode: Option<ConfiguredMode>,
326326 #[knuffel(child)]
327- pub variable_refresh_rate: bool,
327+ pub variable_refresh_rate: Option<Vrr>,
328328 #[knuffel(child, default = DEFAULT_BACKGROUND_COLOR)]
329329 pub background_color: Color,
330330 }
331331
332+impl Output {
333+ pub fn is_vrr_always_on(&self) -> bool {
334+ self.variable_refresh_rate == Some(Vrr { on_demand: false })
335+ }
336+
337+ pub fn is_vrr_on_demand(&self) -> bool {
338+ self.variable_refresh_rate == Some(Vrr { on_demand: true })
339+ }
340+
341+ pub fn is_vrr_always_off(&self) -> bool {
342+ self.variable_refresh_rate.is_none()
343+ }
344+}
345+
332346 impl Default for Output {
333347 fn default() -> Self {
334348 Self {
@@ -338,7 +352,7 @@ impl Default for Output {
338352 transform: Transform::Normal,
339353 position: None,
340354 mode: None,
341- variable_refresh_rate: false,
355+ variable_refresh_rate: None,
342356 background_color: DEFAULT_BACKGROUND_COLOR,
343357 }
344358 }
@@ -352,6 +366,12 @@ pub struct Position {
352366 pub y: i32,
353367 }
354368
369+#[derive(knuffel::Decode, Debug, Clone, PartialEq, Default)]
370+pub struct Vrr {
371+ #[knuffel(property, default = false)]
372+ pub on_demand: bool,
373+}
374+
355375 // MIN and MAX generics are only used during parsing to check the value.
356376 #[derive(Debug, Default, Clone, Copy, PartialEq)]
357377 pub struct FloatOrInt<const MIN: i32, const MAX: i32>(pub f64);
@@ -896,6 +916,8 @@ pub struct WindowRule {
896916 pub clip_to_geometry: Option<bool>,
897917 #[knuffel(child, unwrap(argument))]
898918 pub block_out_from: Option<BlockOutFrom>,
919+ #[knuffel(child, unwrap(argument))]
920+ pub variable_refresh_rate: Option<bool>,
899921 }
900922
901923 // Remember to update the PartialEq impl when adding fields!
@@ -2705,7 +2727,7 @@ mod tests {
27052727 transform "flipped-90"
27062728 position x=10 y=20
27072729 mode "1920x1080@144"
2708- variable-refresh-rate
2730+ variable-refresh-rate on-demand=true
27092731 background-color "rgba(25, 25, 102, 1.0)"
27102732 }
27112733
@@ -2889,7 +2911,7 @@ mod tests {
28892911 height: 1080,
28902912 refresh: Some(144.),
28912913 }),
2892- variable_refresh_rate: true,
2914+ variable_refresh_rate: Some(Vrr { on_demand: true }),
28932915 background_color: Color::from_rgba8_unpremul(25, 25, 102, 255),
28942916 }]),
28952917 layout: Layout {
niri-ipc/src/lib.rsmodified+25 -11
@@ -352,18 +352,11 @@ pub enum OutputAction {
352352 #[cfg_attr(feature = "clap", command(subcommand))]
353353 position: PositionToSet,
354354 },
355- /// Toggle variable refresh rate.
355+ /// Set the variable refresh rate mode.
356356 Vrr {
357- /// Whether to enable variable refresh rate.
358- #[cfg_attr(
359- feature = "clap",
360- arg(
361- value_name = "ON|OFF",
362- action = clap::ArgAction::Set,
363- value_parser = clap::builder::BoolishValueParser::new(),
364- ),
365- )]
366- enable: bool,
357+ /// Variable refresh rate mode to set.
358+ #[cfg_attr(feature = "clap", command(flatten))]
359+ vrr: VrrToSet,
367360 },
368361 }
369362
@@ -425,6 +418,27 @@ pub struct ConfiguredPosition {
425418 pub y: i32,
426419 }
427420
421+/// Output VRR to set.
422+#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
423+#[cfg_attr(feature = "clap", derive(clap::Args))]
424+#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
425+pub struct VrrToSet {
426+ /// Whether to enable variable refresh rate.
427+ #[cfg_attr(
428+ feature = "clap",
429+ arg(
430+ value_name = "ON|OFF",
431+ action = clap::ArgAction::Set,
432+ value_parser = clap::builder::BoolishValueParser::new(),
433+ hide_possible_values = true,
434+ ),
435+ )]
436+ pub vrr: bool,
437+ /// Only enable when the output shows a window matching the variable-refresh-rate window rule.
438+ #[cfg_attr(feature = "clap", arg(long))]
439+ pub on_demand: bool,
440+}
441+
428442 /// Connected output.
429443 #[derive(Debug, Serialize, Deserialize, Clone)]
430444 #[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
src/backend/mod.rsmodified+7 -0
@@ -153,6 +153,13 @@ impl Backend {
153153 }
154154 }
155155
156+ pub fn set_output_on_demand_vrr(&mut self, niri: &mut Niri, output: &Output, enable_vrr: bool) {
157+ match self {
158+ Backend::Tty(tty) => tty.set_output_on_demand_vrr(niri, output, enable_vrr),
159+ Backend::Winit(_) => (),
160+ }
161+ }
162+
156163 pub fn on_output_config_changed(&mut self, niri: &mut Niri) {
157164 match self {
158165 Backend::Tty(tty) => tty.on_output_config_changed(niri),
src/backend/tty.rsmodified+50 -38
@@ -180,6 +180,7 @@ struct TtyOutputState {
180180 struct Surface {
181181 name: String,
182182 compositor: GbmDrmCompositor,
183+ connector: connector::Handle,
183184 dmabuf_feedback: Option<SurfaceDmabufFeedback>,
184185 gamma_props: Option<GammaProps>,
185186 /// Gamma change to apply upon session resume.
@@ -426,18 +427,6 @@ impl Tty {
426427 }
427428
428429 // 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-
441430 let output = niri
442431 .global_space
443432 .outputs()
@@ -457,7 +446,7 @@ impl Tty {
457446
458447 try_to_change_vrr(
459448 &device.drm,
460- connector,
449+ surface.connector,
461450 *crtc,
462451 surface,
463452 output_state,
@@ -832,15 +821,13 @@ impl Tty {
832821 let mut vrr_enabled = false;
833822 if let Some(capable) = is_vrr_capable(&device.drm, connector.handle()) {
834823 if capable {
835- let word = if config.variable_refresh_rate {
836- "enabling"
837- } else {
838- "disabling"
839- };
824+ // Even if on-demand, we still disable it until later checks.
825+ let vrr = config.is_vrr_always_on();
826+ let word = if vrr { "enabling" } else { "disabling" };
840827
841- match set_vrr_enabled(&device.drm, crtc, config.variable_refresh_rate) {
828+ match set_vrr_enabled(&device.drm, crtc, vrr) {
842829 Ok(enabled) => {
843- if enabled != config.variable_refresh_rate {
830+ if enabled != vrr {
844831 warn!("failed {} VRR", word);
845832 }
846833
@@ -851,13 +838,13 @@ impl Tty {
851838 }
852839 }
853840 } else {
854- if config.variable_refresh_rate {
841+ if !config.is_vrr_always_off() {
855842 warn!("cannot enable VRR because connector is not vrr_capable");
856843 }
857844
858845 // Try to disable it anyway to work around a bug where resetting DRM state causes
859846 // vrr_capable to be reset to 0, potentially leaving VRR_ENABLED at 1.
860- let res = set_vrr_enabled(&device.drm, crtc, config.variable_refresh_rate);
847+ let res = set_vrr_enabled(&device.drm, crtc, false);
861848 if matches!(res, Ok(true)) {
862849 warn!("error disabling VRR");
863850
@@ -865,7 +852,7 @@ impl Tty {
865852 vrr_enabled = true;
866853 }
867854 }
868- } else if config.variable_refresh_rate {
855+ } else if !config.is_vrr_always_off() {
869856 warn!("cannot enable VRR because connector is not vrr_capable");
870857 }
871858
@@ -1017,6 +1004,7 @@ impl Tty {
10171004
10181005 let surface = Surface {
10191006 name: output_name.clone(),
1007+ connector: connector.handle(),
10201008 compositor,
10211009 dmabuf_feedback,
10221010 gamma_props,
@@ -1661,6 +1649,33 @@ impl Tty {
16611649 }
16621650 }
16631651
1652+ pub fn set_output_on_demand_vrr(&mut self, niri: &mut Niri, output: &Output, enable_vrr: bool) {
1653+ let _span = tracy_client::span!("Tty::set_output_on_demand_vrr");
1654+
1655+ let output_state = niri.output_state.get_mut(output).unwrap();
1656+ output_state.on_demand_vrr_enabled = enable_vrr;
1657+ if output_state.frame_clock.vrr() == enable_vrr {
1658+ return;
1659+ }
1660+ for (&node, device) in self.devices.iter_mut() {
1661+ for (&crtc, surface) in device.surfaces.iter_mut() {
1662+ let tty_state: &TtyOutputState = output.user_data().get().unwrap();
1663+ if tty_state.node == node && tty_state.crtc == crtc {
1664+ try_to_change_vrr(
1665+ &device.drm,
1666+ surface.connector,
1667+ crtc,
1668+ surface,
1669+ output_state,
1670+ enable_vrr,
1671+ );
1672+ self.refresh_ipc_outputs(niri);
1673+ return;
1674+ }
1675+ }
1676+ }
1677+ }
1678+
16641679 pub fn on_output_config_changed(&mut self, niri: &mut Niri) {
16651680 let _span = tracy_client::span!("Tty::on_output_config_changed");
16661681
@@ -1675,9 +1690,7 @@ impl Tty {
16751690 let mut to_connect = vec![];
16761691
16771692 for (&node, device) in &mut self.devices {
1678- for surface in device.surfaces.values_mut() {
1679- let crtc = surface.compositor.crtc();
1680-
1693+ for (&crtc, surface) in device.surfaces.iter_mut() {
16811694 let config = self
16821695 .config
16831696 .borrow()
@@ -1691,12 +1704,8 @@ impl Tty {
16911704 }
16921705
16931706 // Check if we need to change the mode.
1694- let Some(connector) = surface.compositor.pending_connectors().into_iter().next()
1707+ let Some(connector) = device.drm_scanner.connectors().get(&surface.connector)
16951708 else {
1696- error!("surface pending connectors is empty");
1697- continue;
1698- };
1699- let Some(connector) = device.drm_scanner.connectors().get(&connector) else {
17001709 error!("missing enabled connector in drm_scanner");
17011710 continue;
17021711 };
@@ -1707,8 +1716,9 @@ impl Tty {
17071716 };
17081717
17091718 let change_mode = surface.compositor.pending_mode() != mode;
1710- let change_vrr = surface.vrr_enabled != config.variable_refresh_rate;
1711- if !change_mode && !change_vrr {
1719+ let change_always_vrr = surface.vrr_enabled != config.is_vrr_always_on();
1720+ let is_on_demand_vrr = config.is_vrr_on_demand();
1721+ if !change_mode && !change_always_vrr && !is_on_demand_vrr {
17121722 continue;
17131723 }
17141724
@@ -1729,14 +1739,16 @@ impl Tty {
17291739 continue;
17301740 };
17311741
1732- if change_vrr {
1742+ if (is_on_demand_vrr && surface.vrr_enabled != output_state.on_demand_vrr_enabled)
1743+ || (!is_on_demand_vrr && change_always_vrr)
1744+ {
17331745 try_to_change_vrr(
17341746 &device.drm,
1735- connector,
1747+ connector.handle(),
17361748 crtc,
17371749 surface,
17381750 output_state,
1739- config.variable_refresh_rate,
1751+ !surface.vrr_enabled,
17401752 );
17411753 }
17421754
@@ -2388,7 +2400,7 @@ pub fn set_gamma_for_crtc(
23882400
23892401 fn try_to_change_vrr(
23902402 device: &DrmDevice,
2391- connector: &connector::Info,
2403+ connector: connector::Handle,
23922404 crtc: crtc::Handle,
23932405 surface: &mut Surface,
23942406 output_state: &mut crate::niri::OutputState,
@@ -2396,7 +2408,7 @@ fn try_to_change_vrr(
23962408 ) {
23972409 let _span = tracy_client::span!("try_to_change_vrr");
23982410
2399- if is_vrr_capable(device, connector.handle()) == Some(true) {
2411+ if is_vrr_capable(device, connector) == Some(true) {
24002412 let word = if enable_vrr { "enabling" } else { "disabling" };
24012413
24022414 match set_vrr_enabled(device, crtc, enable_vrr) {
src/frame_clock.rsmodified+4 -0
@@ -40,6 +40,10 @@ impl FrameClock {
4040 self.last_presentation_time = None;
4141 }
4242
43+ pub fn vrr(&self) -> bool {
44+ self.vrr
45+ }
46+
4347 pub fn presented(&mut self, presentation_time: Duration) {
4448 if presentation_time.is_zero() {
4549 // Not interested in these.
src/niri.rsmodified+46 -7
@@ -299,6 +299,7 @@ pub struct OutputState {
299299 pub global: GlobalId,
300300 pub frame_clock: FrameClock,
301301 pub redraw_state: RedrawState,
302+ pub on_demand_vrr_enabled: bool,
302303 // After the last redraw, some ongoing animations still remain.
303304 pub unfinished_animations_remain: bool,
304305 /// Last sequence received in a vblank event.
@@ -1202,8 +1203,14 @@ impl State {
12021203 }
12031204 }
12041205 }
1205- niri_ipc::OutputAction::Vrr { enable } => {
1206- config.variable_refresh_rate = enable;
1206+ niri_ipc::OutputAction::Vrr { vrr } => {
1207+ config.variable_refresh_rate = if vrr.vrr {
1208+ Some(niri_config::Vrr {
1209+ on_demand: vrr.on_demand,
1210+ })
1211+ } else {
1212+ None
1213+ }
12071214 }
12081215 }
12091216 }
@@ -2005,6 +2012,7 @@ impl Niri {
20052012 let state = OutputState {
20062013 global,
20072014 redraw_state: RedrawState::Idle,
2015+ on_demand_vrr_enabled: false,
20082016 unfinished_animations_remain: false,
20092017 frame_clock: FrameClock::new(refresh_interval, vrr),
20102018 last_drm_sequence: None,
@@ -3089,6 +3097,8 @@ impl Niri {
30893097 lock_state => self.lock_state = lock_state,
30903098 }
30913099
3100+ self.refresh_on_demand_vrr(backend, output);
3101+
30923102 // Send the frame callbacks.
30933103 //
30943104 // FIXME: The logic here could be a bit smarter. Currently, during an animation, the
@@ -3118,6 +3128,39 @@ impl Niri {
31183128 });
31193129 }
31203130
3131+ pub fn refresh_on_demand_vrr(&mut self, backend: &mut Backend, output: &Output) {
3132+ let _span = tracy_client::span!("Niri::refresh_on_demand_vrr");
3133+ let Some(on_demand) = self
3134+ .config
3135+ .borrow()
3136+ .outputs
3137+ .find(&output.name())
3138+ .map(|output| output.is_vrr_on_demand())
3139+ else {
3140+ warn!("error getting output config for {}", output.name());
3141+ return;
3142+ };
3143+ if !on_demand {
3144+ return;
3145+ }
3146+
3147+ let current = self.layout.windows_for_output(output).any(|mapped| {
3148+ mapped.rules().variable_refresh_rate == Some(true) && {
3149+ let mut visible = false;
3150+ mapped.window.with_surfaces(|surface, states| {
3151+ if !visible
3152+ && surface_primary_scanout_output(surface, states).as_ref() == Some(output)
3153+ {
3154+ visible = true;
3155+ }
3156+ });
3157+ visible
3158+ }
3159+ });
3160+
3161+ backend.set_output_on_demand_vrr(self, output, current);
3162+ }
3163+
31213164 pub fn update_primary_scanout_output(
31223165 &self,
31233166 output: &Output,
@@ -3181,13 +3224,9 @@ impl Niri {
31813224 let offscreen_id = offscreen_id.as_ref();
31823225
31833226 win.with_surfaces(|surface, states| {
3184- states
3185- .data_map
3186- .insert_if_missing_threadsafe(Mutex::<PrimaryScanoutOutput>::default);
31873227 let surface_primary_scanout_output = states
31883228 .data_map
3189- .get::<Mutex<PrimaryScanoutOutput>>()
3190- .unwrap();
3229+ .get_or_insert_threadsafe(Mutex::<PrimaryScanoutOutput>::default);
31913230 surface_primary_scanout_output
31923231 .lock()
31933232 .unwrap()
src/protocols/output_management.rsmodified+5 -5
@@ -3,7 +3,7 @@ use std::collections::HashMap;
33 use std::iter::zip;
44 use std::mem;
55
6-use niri_config::FloatOrInt;
6+use niri_config::{FloatOrInt, Vrr};
77 use niri_ipc::Transform;
88 use smithay::reexports::wayland_protocols_wlr::output_management::v1::server::{
99 zwlr_output_configuration_head_v1, zwlr_output_configuration_v1, zwlr_output_head_v1,
@@ -693,9 +693,9 @@ where
693693 new_config.scale = Some(FloatOrInt(scale));
694694 }
695695 zwlr_output_configuration_head_v1::Request::SetAdaptiveSync { state } => {
696- let enabled = match state {
697- WEnum::Value(AdaptiveSyncState::Enabled) => true,
698- WEnum::Value(AdaptiveSyncState::Disabled) => false,
696+ let vrr = match state {
697+ WEnum::Value(AdaptiveSyncState::Enabled) => Some(Vrr { on_demand: false }),
698+ WEnum::Value(AdaptiveSyncState::Disabled) => None,
699699 _ => {
700700 warn!("SetAdaptativeSync: unknown requested adaptative sync");
701701 conf_head.post_error(
@@ -705,7 +705,7 @@ where
705705 return;
706706 }
707707 };
708- new_config.variable_refresh_rate = enabled;
708+ new_config.variable_refresh_rate = vrr;
709709 }
710710 _ => unreachable!(),
711711 }
src/window/mod.rsmodified+7 -0
@@ -72,6 +72,9 @@ pub struct ResolvedWindowRules {
7272
7373 /// Whether to block out this window from certain render targets.
7474 pub block_out_from: Option<BlockOutFrom>,
75+
76+ /// Whether to enable VRR on this window's primary output if it is on-demand.
77+ pub variable_refresh_rate: Option<bool>,
7578 }
7679
7780 impl<'a> WindowRef<'a> {
@@ -132,6 +135,7 @@ impl ResolvedWindowRules {
132135 geometry_corner_radius: None,
133136 clip_to_geometry: None,
134137 block_out_from: None,
138+ variable_refresh_rate: None,
135139 }
136140 }
137141
@@ -231,6 +235,9 @@ impl ResolvedWindowRules {
231235 if let Some(x) = rule.block_out_from {
232236 resolved.block_out_from = Some(x);
233237 }
238+ if let Some(x) = rule.variable_refresh_rate {
239+ resolved.variable_refresh_rate = Some(x);
240+ }
234241 }
235242
236243 resolved.open_on_output = open_on_output.map(|x| x.to_owned());
wiki/Configuration:-Outputs.mdmodified+10 -1
@@ -12,7 +12,7 @@ output "eDP-1" {
1212 scale 2.0
1313 transform "90"
1414 position x=1280 y=0
15- variable-refresh-rate
15+ variable-refresh-rate // on-demand=true
1616 background-color "#003300"
1717 }
1818
@@ -147,6 +147,15 @@ output "HDMI-A-1" {
147147 }
148148 ```
149149
150+<sup>Since: 0.1.9</sup> You can also set the `on-demand=true` property, which will only enable VRR when this output shows a window matching the `variable-refresh-rate` window rule.
151+This is helpful to avoid various issues with VRR, since it can be disabled most of the time, and only enabled for specific windows, like games or video players.
152+
153+```kdl
154+output "HDMI-A-1" {
155+ variable-refresh-rate on-demand=true
156+}
157+```
158+
150159 ### `background-color`
151160
152161 <sup>Since: 0.1.8</sup>
wiki/Configuration:-Window-Rules.mdmodified+21 -0
@@ -47,6 +47,7 @@ window-rule {
4747 opacity 0.5
4848 block-out-from "screencast"
4949 // block-out-from "screen-capture"
50+ variable-refresh-rate true
5051
5152 focus-ring {
5253 // off
@@ -391,6 +392,26 @@ window-rule {
391392 }
392393 ```
393394
395+#### `variable-refresh-rate`
396+
397+<sup>Since: 0.1.9</sup>
398+
399+If set to true, whenever this window displays on an output with on-demand VRR, it will enable VRR on that output.
400+
401+```kdl
402+// Configure some output with on-demand VRR.
403+output "HDMI-A-1" {
404+ variable-refresh-rate on-demand=true
405+}
406+
407+// Enable on-demand VRR when mpv displays on the output.
408+window-rule {
409+ match app-id="^mpv$"
410+
411+ variable-refresh-rate true
412+}
413+```
414+
394415 #### `draw-border-with-background`
395416
396417 Override whether the border and the focus ring draw with a background.