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 {
397 397
398 struct GammaProps {398 struct GammaProps {
399 crtc: crtc::Handle,399 crtc: crtc::Handle,
400- gamma_lut: property::Handle,400+ mode: GammaMode,
401- gamma_lut_size: u32,401+}
402- previous_blob: Option<NonZeroU64>,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+ },
403 }415 }
404 416
405 struct ConnectorProperties<'a> {417 struct ConnectorProperties<'a> {
@@ -678,7 +690,7 @@ impl Tty {
678 690
679 // Apply pending gamma changes and restore our existing gamma.691 // Apply pending gamma changes and restore our existing gamma.
680 let device = self.devices.get_mut(&node).unwrap();692 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() {
682 if let Ok(mut props) =694 if let Ok(mut props) =
683 ConnectorProperties::try_new(&device.drm, surface.connector)695 ConnectorProperties::try_new(&device.drm, surface.connector)
684 {696 {
@@ -693,18 +705,12 @@ impl Tty {
693 warn!("failed to get connector properties");705 warn!("failed to get connector properties");
694 }706 }
695 707
696- if let Some(ramp) = surface.pending_gamma_change.take() {708+ if let Some(gamma_props) = &mut surface.gamma_props {
697- let ramp = ramp.as_deref();709+ if let Some(ramp) = surface.pending_gamma_change.take() {
698- let res = if let Some(gamma_props) = &mut surface.gamma_props {710+ if let Err(err) = gamma_props.set_gamma(&device.drm, ramp) {
699- gamma_props.set_gamma(&device.drm, ramp)711+ warn!("error applying pending gamma change: {err:?}");
700- } else {712+ }
701- set_gamma_for_crtc(&device.drm, *crtc, ramp)713+ } else if let Err(err) = gamma_props.restore_gamma(&device.drm) {
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 warn!("error restoring gamma: {err:?}");714 warn!("error restoring gamma: {err:?}");
709 }715 }
710 }716 }
@@ -1326,13 +1332,10 @@ impl Tty {
1326 .ok();1332 .ok();
1327 1333
1328 // Reset gamma in case it was set before.1334 // Reset gamma in case it was set before.
1329- let res = if let Some(gamma_props) = &mut gamma_props {1335+ if let Some(gamma_props) = &mut gamma_props {
1330- gamma_props.set_gamma(&device.drm, None)1336+ if let Err(err) = gamma_props.set_gamma(&device.drm, None) {
1331- } else {1337+ debug!("couldn't reset gamma: {err:?}");
1332- set_gamma_for_crtc(&device.drm, crtc, None)1338+ }
1333- };
1334- if let Err(err) = res {
1335- debug!("couldn't reset gamma: {err:?}");
1336 }1339 }
1337 1340
1338 let surface = device1341 let surface = device
@@ -2095,11 +2098,8 @@ impl Tty {
2095 if let Some(gamma_props) = &surface.gamma_props {2098 if let Some(gamma_props) = &surface.gamma_props {
2096 Ok(gamma_props.gamma_size())2099 Ok(gamma_props.gamma_size())
2097 } else {2100 } else {
2098- let info = device2101+ // Setting gamma is not supported.
2099- .drm2102+ Ok(0)
2100- .get_crtc(crtc)
2101- .context("error getting crtc info")?;
2102- Ok(info.gamma_length())
2103 }2103 }
2104 }2104 }
2105 2105
@@ -2119,12 +2119,11 @@ impl Tty {
2119 return Ok(());2119 return Ok(());
2120 }2120 }
2121 2121
2122- let ramp = ramp.as_deref();2122+ let gamma_props = surface
2123- if let Some(gamma_props) = &mut surface.gamma_props {2123+ .gamma_props
2124- gamma_props.set_gamma(&device.drm, ramp)2124+ .as_mut()
2125- } else {2125+ .context("setting gamma is not supported")?;
2126- set_gamma_for_crtc(&device.drm, crtc, ramp)2126+ gamma_props.set_gamma(&device.drm, ramp)
2127- }
2128 }2127 }
2129 2128
2130 fn refresh_ipc_outputs(&self, niri: &mut Niri) {2129 fn refresh_ipc_outputs(&self, niri: &mut Niri) {
@@ -2641,41 +2640,64 @@ impl GammaProps {
2641 2640
2642 match name {2641 match name {
2643 "GAMMA_LUT" => {2642 "GAMMA_LUT" => {
2644- ensure!(2643+ if matches!(info.value_type(), property::ValueType::Blob) {
2645- matches!(info.value_type(), property::ValueType::Blob),2644+ gamma_lut = Some(prop);
2646- "wrong GAMMA_LUT value type"2645+ } else {
2647- );2646+ debug!("wrong GAMMA_LUT value type");
2648- gamma_lut = Some(prop);2647+ }
2649 }2648 }
2650 "GAMMA_LUT_SIZE" => {2649 "GAMMA_LUT_SIZE" => {
2651- ensure!(2650+ if matches!(info.value_type(), property::ValueType::UnsignedRange(_, _)) {
2652- matches!(info.value_type(), property::ValueType::UnsignedRange(_, _)),2651+ gamma_lut_size = Some(value as u32);
2653- "wrong GAMMA_LUT_SIZE value type"2652+ } else {
2654- );2653+ debug!("wrong GAMMA_LUT_SIZE value type");
2655- gamma_lut_size = Some(value as u32);2654+ }
2656 }2655 }
2657 _ => (),2656 _ => (),
2658 }2657 }
2659 }2658 }
2660 2659
2661- let gamma_lut = gamma_lut.context("missing GAMMA_LUT property")?;2660+ let mode = if let (Some(gamma_lut), Some(gamma_lut_size)) = (gamma_lut, gamma_lut_size) {
2662- let gamma_lut_size = gamma_lut_size.context("missing GAMMA_LUT_SIZE property")?;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+ };
2663 2675
2664- Ok(Self {2676+ Ok(Self { crtc, mode })
2665- crtc,
2666- gamma_lut,
2667- gamma_lut_size,
2668- previous_blob: None,
2669- })
2670 }2677 }
2671 2678
2672 fn gamma_size(&self) -> u32 {2679 fn gamma_size(&self) -> u32 {
2673- self.gamma_lut_size2680+ match &self.mode {
2681+ GammaMode::Lut { gamma_lut_size, .. } => *gamma_lut_size,
2682+ GammaMode::Legacy { gamma_size, .. } => *gamma_size,
2683+ }
2674 }2684 }
2675 2685
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<()> {
2677 let _span = tracy_client::span!("GammaProps::set_gamma");2687 let _span = tracy_client::span!("GammaProps::set_gamma");
2678 2688
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+
2679 let blob = if let Some(gamma) = gamma {2701 let blob = if let Some(gamma) = gamma {
2680 let gamma_size = self.gamma_size() as usize;2702 let gamma_size = self.gamma_size() as usize;
2681 2703
@@ -2715,11 +2737,7 @@ impl GammaProps {
2715 2737
2716 let blob = blob.map(NonZeroU64::get).unwrap_or(0);2738 let blob = blob.map(NonZeroU64::get).unwrap_or(0);
2717 device2739 device
2718- .set_property(2740+ .set_property(self.crtc, gamma_lut, property::Value::Blob(blob).into())
2719- self.crtc,
2720- self.gamma_lut,
2721- property::Value::Blob(blob).into(),
2722- )
2723 .context("error setting GAMMA_LUT")2741 .context("error setting GAMMA_LUT")
2724 .inspect_err(|_| {2742 .inspect_err(|_| {
2725 if blob != 0 {2743 if blob != 0 {
@@ -2731,10 +2749,15 @@ impl GammaProps {
2731 })?;2749 })?;
2732 }2750 }
2733 2751
2734- if let Some(blob) = mem::replace(&mut self.previous_blob, blob) {2752+ if let GammaMode::Lut { previous_blob, .. } = &mut self.mode {
2735- if let Err(err) = device.destroy_property_blob(blob.get()) {2753+ if let Some(blob) = mem::replace(previous_blob, blob) {
2736- warn!("error destroying previous GAMMA_LUT blob: {err:?}");2754+ if let Err(err) = device.destroy_property_blob(blob.get()) {
2755+ warn!("error destroying previous GAMMA_LUT blob: {err:?}");
2756+ }
2737 }2757 }
2758+ } else {
2759+ // Legacy early-returns at the start of the function.
2760+ unreachable!();
2738 }2761 }
2739 2762
2740 Ok(())2763 Ok(())
@@ -2743,14 +2766,24 @@ impl GammaProps {
2743 fn restore_gamma(&self, device: &DrmDevice) -> anyhow::Result<()> {2766 fn restore_gamma(&self, device: &DrmDevice) -> anyhow::Result<()> {
2744 let _span = tracy_client::span!("GammaProps::restore_gamma");2767 let _span = tracy_client::span!("GammaProps::restore_gamma");
2745 2768
2746- let blob = self.previous_blob.map(NonZeroU64::get).unwrap_or(0);2769+ match &self.mode {
2747- device2770+ GammaMode::Lut {
2748- .set_property(2771+ gamma_lut,
2749- self.crtc,2772+ previous_blob,
2750- self.gamma_lut,2773+ ..
2751- property::Value::Blob(blob).into(),2774+ } => {
2752- )2775+ let blob = previous_blob.map(NonZeroU64::get).unwrap_or(0);
2753- .context("error setting GAMMA_LUT")?;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+ }
2754 2787
2755 Ok(())2788 Ok(())
2756 }2789 }
@@ -3398,17 +3431,15 @@ fn is_vrr_capable(device: &DrmDevice, connector: connector::Handle) -> Option<bo
3398 info.value_type().convert_value(value).as_boolean()3431 info.value_type().convert_value(value).as_boolean()
3399 }3432 }
3400 3433
3401-pub fn set_gamma_for_crtc(3434+fn set_gamma_for_crtc(
3402 device: &DrmDevice,3435 device: &DrmDevice,
3403 crtc: crtc::Handle,3436 crtc: crtc::Handle,
3437+ gamma_length: u32,
3404 ramp: Option<&[u16]>,3438 ramp: Option<&[u16]>,
3405 ) -> anyhow::Result<()> {3439 ) -> anyhow::Result<()> {
3406 let _span = tracy_client::span!("set_gamma_for_crtc");3440 let _span = tracy_client::span!("set_gamma_for_crtc");
3407 3441
3408- let info = device.get_crtc(crtc).context("error getting crtc info")?;3442+ let gamma_length = gamma_length as usize;
3409- let gamma_length = info.gamma_length() as usize;
3410-
3411- ensure!(gamma_length != 0, "setting gamma is not supported");
3412 3443
3413 let mut temp;3444 let mut temp;
3414 let ramp = if let Some(ramp) = ramp {3445 let ramp = if let Some(ramp) = ramp {