diff --git a/src/backend/tty.rs b/src/backend/tty.rs index 8ebd5f04..628ede17 100644 --- a/src/backend/tty.rs +++ b/src/backend/tty.rs @@ -397,9 +397,21 @@ pub struct SurfaceDmabufFeedback { struct GammaProps { crtc: crtc::Handle, - gamma_lut: property::Handle, - gamma_lut_size: u32, - previous_blob: Option, + mode: GammaMode, +} + +enum GammaMode { + /// GAMMA_LUT property. + Lut { + gamma_lut: property::Handle, + gamma_lut_size: u32, + previous_blob: Option, + }, + /// Legacy set_gamma() call. + Legacy { + gamma_size: u32, + previous_ramp: Option>, + }, } struct ConnectorProperties<'a> { @@ -678,7 +690,7 @@ impl Tty { // Apply pending gamma changes and restore our existing gamma. let device = self.devices.get_mut(&node).unwrap(); - for (crtc, surface) in device.surfaces.iter_mut() { + for surface in device.surfaces.values_mut() { if let Ok(mut props) = ConnectorProperties::try_new(&device.drm, surface.connector) { @@ -693,18 +705,12 @@ impl Tty { warn!("failed to get connector properties"); } - if let Some(ramp) = surface.pending_gamma_change.take() { - let ramp = ramp.as_deref(); - let res = if let Some(gamma_props) = &mut surface.gamma_props { - gamma_props.set_gamma(&device.drm, ramp) - } else { - set_gamma_for_crtc(&device.drm, *crtc, ramp) - }; - if let Err(err) = res { - warn!("error applying pending gamma change: {err:?}"); - } - } else if let Some(gamma_props) = &surface.gamma_props { - if let Err(err) = gamma_props.restore_gamma(&device.drm) { + if let Some(gamma_props) = &mut surface.gamma_props { + if let Some(ramp) = surface.pending_gamma_change.take() { + if let Err(err) = gamma_props.set_gamma(&device.drm, ramp) { + warn!("error applying pending gamma change: {err:?}"); + } + } else if let Err(err) = gamma_props.restore_gamma(&device.drm) { warn!("error restoring gamma: {err:?}"); } } @@ -1326,13 +1332,10 @@ impl Tty { .ok(); // Reset gamma in case it was set before. - let res = if let Some(gamma_props) = &mut gamma_props { - gamma_props.set_gamma(&device.drm, None) - } else { - set_gamma_for_crtc(&device.drm, crtc, None) - }; - if let Err(err) = res { - debug!("couldn't reset gamma: {err:?}"); + if let Some(gamma_props) = &mut gamma_props { + if let Err(err) = gamma_props.set_gamma(&device.drm, None) { + debug!("couldn't reset gamma: {err:?}"); + } } let surface = device @@ -2095,11 +2098,8 @@ impl Tty { if let Some(gamma_props) = &surface.gamma_props { Ok(gamma_props.gamma_size()) } else { - let info = device - .drm - .get_crtc(crtc) - .context("error getting crtc info")?; - Ok(info.gamma_length()) + // Setting gamma is not supported. + Ok(0) } } @@ -2119,12 +2119,11 @@ impl Tty { return Ok(()); } - let ramp = ramp.as_deref(); - if let Some(gamma_props) = &mut surface.gamma_props { - gamma_props.set_gamma(&device.drm, ramp) - } else { - set_gamma_for_crtc(&device.drm, crtc, ramp) - } + let gamma_props = surface + .gamma_props + .as_mut() + .context("setting gamma is not supported")?; + gamma_props.set_gamma(&device.drm, ramp) } fn refresh_ipc_outputs(&self, niri: &mut Niri) { @@ -2641,41 +2640,64 @@ impl GammaProps { match name { "GAMMA_LUT" => { - ensure!( - matches!(info.value_type(), property::ValueType::Blob), - "wrong GAMMA_LUT value type" - ); - gamma_lut = Some(prop); + if matches!(info.value_type(), property::ValueType::Blob) { + gamma_lut = Some(prop); + } else { + debug!("wrong GAMMA_LUT value type"); + } } "GAMMA_LUT_SIZE" => { - ensure!( - matches!(info.value_type(), property::ValueType::UnsignedRange(_, _)), - "wrong GAMMA_LUT_SIZE value type" - ); - gamma_lut_size = Some(value as u32); + if matches!(info.value_type(), property::ValueType::UnsignedRange(_, _)) { + gamma_lut_size = Some(value as u32); + } else { + debug!("wrong GAMMA_LUT_SIZE value type"); + } } _ => (), } } - let gamma_lut = gamma_lut.context("missing GAMMA_LUT property")?; - let gamma_lut_size = gamma_lut_size.context("missing GAMMA_LUT_SIZE property")?; + let mode = if let (Some(gamma_lut), Some(gamma_lut_size)) = (gamma_lut, gamma_lut_size) { + GammaMode::Lut { + gamma_lut, + gamma_lut_size, + previous_blob: None, + } + } else { + let info = device.get_crtc(crtc).context("error getting crtc info")?; + let gamma_size = info.gamma_length(); + ensure!(gamma_size != 0, "setting gamma is not supported"); + GammaMode::Legacy { + gamma_size, + previous_ramp: None, + } + }; - Ok(Self { - crtc, - gamma_lut, - gamma_lut_size, - previous_blob: None, - }) + Ok(Self { crtc, mode }) } fn gamma_size(&self) -> u32 { - self.gamma_lut_size + match &self.mode { + GammaMode::Lut { gamma_lut_size, .. } => *gamma_lut_size, + GammaMode::Legacy { gamma_size, .. } => *gamma_size, + } } - fn set_gamma(&mut self, device: &DrmDevice, gamma: Option<&[u16]>) -> anyhow::Result<()> { + fn set_gamma(&mut self, device: &DrmDevice, gamma: Option>) -> anyhow::Result<()> { let _span = tracy_client::span!("GammaProps::set_gamma"); + let gamma_lut = match &mut self.mode { + GammaMode::Lut { gamma_lut, .. } => *gamma_lut, + GammaMode::Legacy { + gamma_size, + previous_ramp, + } => { + set_gamma_for_crtc(device, self.crtc, *gamma_size, gamma.as_deref())?; + *previous_ramp = gamma; + return Ok(()); + } + }; + let blob = if let Some(gamma) = gamma { let gamma_size = self.gamma_size() as usize; @@ -2715,11 +2737,7 @@ impl GammaProps { let blob = blob.map(NonZeroU64::get).unwrap_or(0); device - .set_property( - self.crtc, - self.gamma_lut, - property::Value::Blob(blob).into(), - ) + .set_property(self.crtc, gamma_lut, property::Value::Blob(blob).into()) .context("error setting GAMMA_LUT") .inspect_err(|_| { if blob != 0 { @@ -2731,10 +2749,15 @@ impl GammaProps { })?; } - if let Some(blob) = mem::replace(&mut self.previous_blob, blob) { - if let Err(err) = device.destroy_property_blob(blob.get()) { - warn!("error destroying previous GAMMA_LUT blob: {err:?}"); + if let GammaMode::Lut { previous_blob, .. } = &mut self.mode { + if let Some(blob) = mem::replace(previous_blob, blob) { + if let Err(err) = device.destroy_property_blob(blob.get()) { + warn!("error destroying previous GAMMA_LUT blob: {err:?}"); + } } + } else { + // Legacy early-returns at the start of the function. + unreachable!(); } Ok(()) @@ -2743,14 +2766,24 @@ impl GammaProps { fn restore_gamma(&self, device: &DrmDevice) -> anyhow::Result<()> { let _span = tracy_client::span!("GammaProps::restore_gamma"); - let blob = self.previous_blob.map(NonZeroU64::get).unwrap_or(0); - device - .set_property( - self.crtc, - self.gamma_lut, - property::Value::Blob(blob).into(), - ) - .context("error setting GAMMA_LUT")?; + match &self.mode { + GammaMode::Lut { + gamma_lut, + previous_blob, + .. + } => { + let blob = previous_blob.map(NonZeroU64::get).unwrap_or(0); + device + .set_property(self.crtc, *gamma_lut, property::Value::Blob(blob).into()) + .context("error setting GAMMA_LUT")?; + } + GammaMode::Legacy { + gamma_size, + previous_ramp, + } => { + set_gamma_for_crtc(device, self.crtc, *gamma_size, previous_ramp.as_deref())?; + } + } Ok(()) } @@ -3398,17 +3431,15 @@ fn is_vrr_capable(device: &DrmDevice, connector: connector::Handle) -> Option, ) -> anyhow::Result<()> { let _span = tracy_client::span!("set_gamma_for_crtc"); - let info = device.get_crtc(crtc).context("error getting crtc info")?; - let gamma_length = info.gamma_length() as usize; - - ensure!(gamma_length != 0, "setting gamma is not supported"); + let gamma_length = gamma_length as usize; let mut temp; let ramp = if let Some(ramp) = ramp {