forge
Explore
Log in

Don't you dare go Hollow

tty: Move legacy gamma into GammaProps

Browse files at this commit

9e38dd23bc9a1f971f01440497befbb3aa245a1cIvan MolodetskikhParent: 008992f
Also implement restoring legacy gamma on TTY switch.
src/backend/tty.rsmodified+106 -75
@@ -397,9 +397,21 @@ pub struct SurfaceDmabufFeedback {
397397
398398 struct GammaProps {
399399 crtc: crtc::Handle,
400- gamma_lut: property::Handle,
401- gamma_lut_size: u32,
402- previous_blob: Option<NonZeroU64>,
400+ mode: GammaMode,
401+}
402+
403+enum GammaMode {
404+ /// GAMMA_LUT property.
405+ Lut {
406+ gamma_lut: property::Handle,
407+ gamma_lut_size: u32,
408+ previous_blob: Option<NonZeroU64>,
409+ },
410+ /// Legacy set_gamma() call.
411+ Legacy {
412+ gamma_size: u32,
413+ previous_ramp: Option<Vec<u16>>,
414+ },
403415 }
404416
405417 struct ConnectorProperties<'a> {
@@ -678,7 +690,7 @@ impl Tty {
678690
679691 // Apply pending gamma changes and restore our existing gamma.
680692 let device = self.devices.get_mut(&node).unwrap();
681- for (crtc, surface) in device.surfaces.iter_mut() {
693+ for surface in device.surfaces.values_mut() {
682694 if let Ok(mut props) =
683695 ConnectorProperties::try_new(&device.drm, surface.connector)
684696 {
@@ -693,18 +705,12 @@ impl Tty {
693705 warn!("failed to get connector properties");
694706 }
695707
696- if let Some(ramp) = surface.pending_gamma_change.take() {
697- let ramp = ramp.as_deref();
698- let res = if let Some(gamma_props) = &mut surface.gamma_props {
699- gamma_props.set_gamma(&device.drm, ramp)
700- } else {
701- set_gamma_for_crtc(&device.drm, *crtc, ramp)
702- };
703- if let Err(err) = res {
704- warn!("error applying pending gamma change: {err:?}");
705- }
706- } else if let Some(gamma_props) = &surface.gamma_props {
707- if let Err(err) = gamma_props.restore_gamma(&device.drm) {
708+ if let Some(gamma_props) = &mut surface.gamma_props {
709+ if let Some(ramp) = surface.pending_gamma_change.take() {
710+ if let Err(err) = gamma_props.set_gamma(&device.drm, ramp) {
711+ warn!("error applying pending gamma change: {err:?}");
712+ }
713+ } else if let Err(err) = gamma_props.restore_gamma(&device.drm) {
708714 warn!("error restoring gamma: {err:?}");
709715 }
710716 }
@@ -1326,13 +1332,10 @@ impl Tty {
13261332 .ok();
13271333
13281334 // Reset gamma in case it was set before.
1329- let res = if let Some(gamma_props) = &mut gamma_props {
1330- gamma_props.set_gamma(&device.drm, None)
1331- } else {
1332- set_gamma_for_crtc(&device.drm, crtc, None)
1333- };
1334- if let Err(err) = res {
1335- debug!("couldn't reset gamma: {err:?}");
1335+ if let Some(gamma_props) = &mut gamma_props {
1336+ if let Err(err) = gamma_props.set_gamma(&device.drm, None) {
1337+ debug!("couldn't reset gamma: {err:?}");
1338+ }
13361339 }
13371340
13381341 let surface = device
@@ -2095,11 +2098,8 @@ impl Tty {
20952098 if let Some(gamma_props) = &surface.gamma_props {
20962099 Ok(gamma_props.gamma_size())
20972100 } else {
2098- let info = device
2099- .drm
2100- .get_crtc(crtc)
2101- .context("error getting crtc info")?;
2102- Ok(info.gamma_length())
2101+ // Setting gamma is not supported.
2102+ Ok(0)
21032103 }
21042104 }
21052105
@@ -2119,12 +2119,11 @@ impl Tty {
21192119 return Ok(());
21202120 }
21212121
2122- let ramp = ramp.as_deref();
2123- if let Some(gamma_props) = &mut surface.gamma_props {
2124- gamma_props.set_gamma(&device.drm, ramp)
2125- } else {
2126- set_gamma_for_crtc(&device.drm, crtc, ramp)
2127- }
2122+ let gamma_props = surface
2123+ .gamma_props
2124+ .as_mut()
2125+ .context("setting gamma is not supported")?;
2126+ gamma_props.set_gamma(&device.drm, ramp)
21282127 }
21292128
21302129 fn refresh_ipc_outputs(&self, niri: &mut Niri) {
@@ -2641,41 +2640,64 @@ impl GammaProps {
26412640
26422641 match name {
26432642 "GAMMA_LUT" => {
2644- ensure!(
2645- matches!(info.value_type(), property::ValueType::Blob),
2646- "wrong GAMMA_LUT value type"
2647- );
2648- gamma_lut = Some(prop);
2643+ if matches!(info.value_type(), property::ValueType::Blob) {
2644+ gamma_lut = Some(prop);
2645+ } else {
2646+ debug!("wrong GAMMA_LUT value type");
2647+ }
26492648 }
26502649 "GAMMA_LUT_SIZE" => {
2651- ensure!(
2652- matches!(info.value_type(), property::ValueType::UnsignedRange(_, _)),
2653- "wrong GAMMA_LUT_SIZE value type"
2654- );
2655- gamma_lut_size = Some(value as u32);
2650+ if matches!(info.value_type(), property::ValueType::UnsignedRange(_, _)) {
2651+ gamma_lut_size = Some(value as u32);
2652+ } else {
2653+ debug!("wrong GAMMA_LUT_SIZE value type");
2654+ }
26562655 }
26572656 _ => (),
26582657 }
26592658 }
26602659
2661- let gamma_lut = gamma_lut.context("missing GAMMA_LUT property")?;
2662- let gamma_lut_size = gamma_lut_size.context("missing GAMMA_LUT_SIZE property")?;
2660+ let mode = if let (Some(gamma_lut), Some(gamma_lut_size)) = (gamma_lut, gamma_lut_size) {
2661+ GammaMode::Lut {
2662+ gamma_lut,
2663+ gamma_lut_size,
2664+ previous_blob: None,
2665+ }
2666+ } else {
2667+ let info = device.get_crtc(crtc).context("error getting crtc info")?;
2668+ let gamma_size = info.gamma_length();
2669+ ensure!(gamma_size != 0, "setting gamma is not supported");
2670+ GammaMode::Legacy {
2671+ gamma_size,
2672+ previous_ramp: None,
2673+ }
2674+ };
26632675
2664- Ok(Self {
2665- crtc,
2666- gamma_lut,
2667- gamma_lut_size,
2668- previous_blob: None,
2669- })
2676+ Ok(Self { crtc, mode })
26702677 }
26712678
26722679 fn gamma_size(&self) -> u32 {
2673- self.gamma_lut_size
2680+ match &self.mode {
2681+ GammaMode::Lut { gamma_lut_size, .. } => *gamma_lut_size,
2682+ GammaMode::Legacy { gamma_size, .. } => *gamma_size,
2683+ }
26742684 }
26752685
2676- fn set_gamma(&mut self, device: &DrmDevice, gamma: Option<&[u16]>) -> anyhow::Result<()> {
2686+ fn set_gamma(&mut self, device: &DrmDevice, gamma: Option<Vec<u16>>) -> anyhow::Result<()> {
26772687 let _span = tracy_client::span!("GammaProps::set_gamma");
26782688
2689+ let gamma_lut = match &mut self.mode {
2690+ GammaMode::Lut { gamma_lut, .. } => *gamma_lut,
2691+ GammaMode::Legacy {
2692+ gamma_size,
2693+ previous_ramp,
2694+ } => {
2695+ set_gamma_for_crtc(device, self.crtc, *gamma_size, gamma.as_deref())?;
2696+ *previous_ramp = gamma;
2697+ return Ok(());
2698+ }
2699+ };
2700+
26792701 let blob = if let Some(gamma) = gamma {
26802702 let gamma_size = self.gamma_size() as usize;
26812703
@@ -2715,11 +2737,7 @@ impl GammaProps {
27152737
27162738 let blob = blob.map(NonZeroU64::get).unwrap_or(0);
27172739 device
2718- .set_property(
2719- self.crtc,
2720- self.gamma_lut,
2721- property::Value::Blob(blob).into(),
2722- )
2740+ .set_property(self.crtc, gamma_lut, property::Value::Blob(blob).into())
27232741 .context("error setting GAMMA_LUT")
27242742 .inspect_err(|_| {
27252743 if blob != 0 {
@@ -2731,10 +2749,15 @@ impl GammaProps {
27312749 })?;
27322750 }
27332751
2734- if let Some(blob) = mem::replace(&mut self.previous_blob, blob) {
2735- if let Err(err) = device.destroy_property_blob(blob.get()) {
2736- warn!("error destroying previous GAMMA_LUT blob: {err:?}");
2752+ if let GammaMode::Lut { previous_blob, .. } = &mut self.mode {
2753+ if let Some(blob) = mem::replace(previous_blob, blob) {
2754+ if let Err(err) = device.destroy_property_blob(blob.get()) {
2755+ warn!("error destroying previous GAMMA_LUT blob: {err:?}");
2756+ }
27372757 }
2758+ } else {
2759+ // Legacy early-returns at the start of the function.
2760+ unreachable!();
27382761 }
27392762
27402763 Ok(())
@@ -2743,14 +2766,24 @@ impl GammaProps {
27432766 fn restore_gamma(&self, device: &DrmDevice) -> anyhow::Result<()> {
27442767 let _span = tracy_client::span!("GammaProps::restore_gamma");
27452768
2746- let blob = self.previous_blob.map(NonZeroU64::get).unwrap_or(0);
2747- device
2748- .set_property(
2749- self.crtc,
2750- self.gamma_lut,
2751- property::Value::Blob(blob).into(),
2752- )
2753- .context("error setting GAMMA_LUT")?;
2769+ match &self.mode {
2770+ GammaMode::Lut {
2771+ gamma_lut,
2772+ previous_blob,
2773+ ..
2774+ } => {
2775+ let blob = previous_blob.map(NonZeroU64::get).unwrap_or(0);
2776+ device
2777+ .set_property(self.crtc, *gamma_lut, property::Value::Blob(blob).into())
2778+ .context("error setting GAMMA_LUT")?;
2779+ }
2780+ GammaMode::Legacy {
2781+ gamma_size,
2782+ previous_ramp,
2783+ } => {
2784+ set_gamma_for_crtc(device, self.crtc, *gamma_size, previous_ramp.as_deref())?;
2785+ }
2786+ }
27542787
27552788 Ok(())
27562789 }
@@ -3398,17 +3431,15 @@ fn is_vrr_capable(device: &DrmDevice, connector: connector::Handle) -> Option<bo
33983431 info.value_type().convert_value(value).as_boolean()
33993432 }
34003433
3401-pub fn set_gamma_for_crtc(
3434+fn set_gamma_for_crtc(
34023435 device: &DrmDevice,
34033436 crtc: crtc::Handle,
3437+ gamma_length: u32,
34043438 ramp: Option<&[u16]>,
34053439 ) -> anyhow::Result<()> {
34063440 let _span = tracy_client::span!("set_gamma_for_crtc");
34073441
3408- let info = device.get_crtc(crtc).context("error getting crtc info")?;
3409- let gamma_length = info.gamma_length() as usize;
3410-
3411- ensure!(gamma_length != 0, "setting gamma is not supported");
3442+ let gamma_length = gamma_length as usize;
34123443
34133444 let mut temp;
34143445 let ramp = if let Some(ramp) = ramp {