tty: Move legacy gamma into GammaProps
Browse files at this commit9e38dd23bc9a1f971f01440497befbb3aa245a1cParent: 008992f1 changed file +106 -75
- src/backend/tty.rs +106 -75
src/backend/tty.rsmodified+106 -75
| @@ -397,9 +397,21 @@ pub struct SurfaceDmabufFeedback { | ||
| 397 | 397 | |
| 398 | 398 | struct GammaProps { |
| 399 | 399 | 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 | + }, | |
| 403 | 415 | } |
| 404 | 416 | |
| 405 | 417 | struct ConnectorProperties<'a> { |
| @@ -678,7 +690,7 @@ impl Tty { | ||
| 678 | 690 | |
| 679 | 691 | // Apply pending gamma changes and restore our existing gamma. |
| 680 | 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 | 694 | if let Ok(mut props) = |
| 683 | 695 | ConnectorProperties::try_new(&device.drm, surface.connector) |
| 684 | 696 | { |
| @@ -693,18 +705,12 @@ impl Tty { | ||
| 693 | 705 | warn!("failed to get connector properties"); |
| 694 | 706 | } |
| 695 | 707 | |
| 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) { | |
| 708 | 714 | warn!("error restoring gamma: {err:?}"); |
| 709 | 715 | } |
| 710 | 716 | } |
| @@ -1326,13 +1332,10 @@ impl Tty { | ||
| 1326 | 1332 | .ok(); |
| 1327 | 1333 | |
| 1328 | 1334 | // 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 | + } | |
| 1336 | 1339 | } |
| 1337 | 1340 | |
| 1338 | 1341 | let surface = device |
| @@ -2095,11 +2098,8 @@ impl Tty { | ||
| 2095 | 2098 | if let Some(gamma_props) = &surface.gamma_props { |
| 2096 | 2099 | Ok(gamma_props.gamma_size()) |
| 2097 | 2100 | } 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) | |
| 2103 | 2103 | } |
| 2104 | 2104 | } |
| 2105 | 2105 | |
| @@ -2119,12 +2119,11 @@ impl Tty { | ||
| 2119 | 2119 | return Ok(()); |
| 2120 | 2120 | } |
| 2121 | 2121 | |
| 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) | |
| 2128 | 2127 | } |
| 2129 | 2128 | |
| 2130 | 2129 | fn refresh_ipc_outputs(&self, niri: &mut Niri) { |
| @@ -2641,41 +2640,64 @@ impl GammaProps { | ||
| 2641 | 2640 | |
| 2642 | 2641 | match name { |
| 2643 | 2642 | "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 | + } | |
| 2649 | 2648 | } |
| 2650 | 2649 | "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 | + } | |
| 2656 | 2655 | } |
| 2657 | 2656 | _ => (), |
| 2658 | 2657 | } |
| 2659 | 2658 | } |
| 2660 | 2659 | |
| 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 | + }; | |
| 2663 | 2675 | |
| 2664 | - Ok(Self { | |
| 2665 | - crtc, | |
| 2666 | - gamma_lut, | |
| 2667 | - gamma_lut_size, | |
| 2668 | - previous_blob: None, | |
| 2669 | - }) | |
| 2676 | + Ok(Self { crtc, mode }) | |
| 2670 | 2677 | } |
| 2671 | 2678 | |
| 2672 | 2679 | 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 | + } | |
| 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 | 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 | 2701 | let blob = if let Some(gamma) = gamma { |
| 2680 | 2702 | let gamma_size = self.gamma_size() as usize; |
| 2681 | 2703 | |
| @@ -2715,11 +2737,7 @@ impl GammaProps { | ||
| 2715 | 2737 | |
| 2716 | 2738 | let blob = blob.map(NonZeroU64::get).unwrap_or(0); |
| 2717 | 2739 | 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()) | |
| 2723 | 2741 | .context("error setting GAMMA_LUT") |
| 2724 | 2742 | .inspect_err(|_| { |
| 2725 | 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) { | |
| 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 | + } | |
| 2737 | 2757 | } |
| 2758 | + } else { | |
| 2759 | + // Legacy early-returns at the start of the function. | |
| 2760 | + unreachable!(); | |
| 2738 | 2761 | } |
| 2739 | 2762 | |
| 2740 | 2763 | Ok(()) |
| @@ -2743,14 +2766,24 @@ impl GammaProps { | ||
| 2743 | 2766 | fn restore_gamma(&self, device: &DrmDevice) -> anyhow::Result<()> { |
| 2744 | 2767 | let _span = tracy_client::span!("GammaProps::restore_gamma"); |
| 2745 | 2768 | |
| 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 | + } | |
| 2754 | 2787 | |
| 2755 | 2788 | Ok(()) |
| 2756 | 2789 | } |
| @@ -3398,17 +3431,15 @@ fn is_vrr_capable(device: &DrmDevice, connector: connector::Handle) -> Option<bo | ||
| 3398 | 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 | 3435 | device: &DrmDevice, |
| 3403 | 3436 | crtc: crtc::Handle, |
| 3437 | + gamma_length: u32, | |
| 3404 | 3438 | ramp: Option<&[u16]>, |
| 3405 | 3439 | ) -> anyhow::Result<()> { |
| 3406 | 3440 | let _span = tracy_client::span!("set_gamma_for_crtc"); |
| 3407 | 3441 | |
| 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; | |
| 3412 | 3443 | |
| 3413 | 3444 | let mut temp; |
| 3414 | 3445 | let ramp = if let Some(ramp) = ramp { |