forge
Explore
Log in

Don't you dare go Hollow

tty: Add CTM fallback to set approximate gamma

Browse files at this commit

c7616326a60d00cafba8c5e0c92bc7a02c8c10f2MrFantOlasParent: 9e38dd2
When neither GAMMA_LUT nor legacy gamma is available, approximate the
gamma ramp with a CTM scaling matrix.
src/backend/tty.rsmodified+95 -23
@@ -11,7 +11,7 @@ use std::time::Duration;
11 use std::{io, mem};11 use std::{io, mem};
12 12
13 use anyhow::{anyhow, bail, ensure, Context};13 use anyhow::{anyhow, bail, ensure, Context};
14-use bytemuck::cast_slice_mut;14+use bytemuck::{bytes_of_mut, cast_slice_mut};
15 use drm_ffi::drm_mode_modeinfo;15 use drm_ffi::drm_mode_modeinfo;
16 use libc::dev_t;16 use libc::dev_t;
17 use niri_config::output::{MaxBpc, Modeline};17 use niri_config::output::{MaxBpc, Modeline};
@@ -412,6 +412,15 @@ enum GammaMode {
412 gamma_size: u32,412 gamma_size: u32,
413 previous_ramp: Option<Vec<u16>>,413 previous_ramp: Option<Vec<u16>>,
414 },414 },
415+ /// Approximation through the CTM property.
416+ ///
417+ /// This is both less accurate (3×3 matrix instead of a LUT), and I think applies per-plane
418+ /// pre-blending on some drivers, as opposed to the post-blending gamma transform. So it's used
419+ /// only as the last resort fallback for Asahi.
420+ Ctm {
421+ ctm: property::Handle,
422+ previous_blob: Option<NonZeroU64>,
423+ },
415 }424 }
416 425
417 struct ConnectorProperties<'a> {426 struct ConnectorProperties<'a> {
@@ -2625,6 +2634,7 @@ impl GammaProps {
2625 fn new(device: &DrmDevice, crtc: crtc::Handle) -> anyhow::Result<Self> {2634 fn new(device: &DrmDevice, crtc: crtc::Handle) -> anyhow::Result<Self> {
2626 let mut gamma_lut = None;2635 let mut gamma_lut = None;
2627 let mut gamma_lut_size = None;2636 let mut gamma_lut_size = None;
2637+ let mut ctm = None;
2628 2638
2629 let props = device2639 let props = device
2630 .get_properties(crtc)2640 .get_properties(crtc)
@@ -2653,6 +2663,13 @@ impl GammaProps {
2653 debug!("wrong GAMMA_LUT_SIZE value type");2663 debug!("wrong GAMMA_LUT_SIZE value type");
2654 }2664 }
2655 }2665 }
2666+ "CTM" => {
2667+ if matches!(info.value_type(), property::ValueType::Blob) {
2668+ ctm = Some(prop);
2669+ } else {
2670+ debug!("wrong CTM value type");
2671+ }
2672+ }
2656 _ => (),2673 _ => (),
2657 }2674 }
2658 }2675 }
@@ -2664,12 +2681,23 @@ impl GammaProps {
2664 previous_blob: None,2681 previous_blob: None,
2665 }2682 }
2666 } else {2683 } else {
2684+ // Try legacy gamma first.
2667 let info = device.get_crtc(crtc).context("error getting crtc info")?;2685 let info = device.get_crtc(crtc).context("error getting crtc info")?;
2668 let gamma_size = info.gamma_length();2686 let gamma_size = info.gamma_length();
2669- ensure!(gamma_size != 0, "setting gamma is not supported");2687+ if gamma_size != 0 {
2670- GammaMode::Legacy {2688+ GammaMode::Legacy {
2671- gamma_size,2689+ gamma_size,
2672- previous_ramp: None,2690+ previous_ramp: None,
2691+ }
2692+ } else {
2693+ let ctm = ctm.context("setting gamma is not supported")?;
2694+ debug!(
2695+ "missing GAMMA_LUT and legacy gamma; using less accurate CTM for gamma control"
2696+ );
2697+ GammaMode::Ctm {
2698+ ctm,
2699+ previous_blob: None,
2700+ }
2673 }2701 }
2674 };2702 };
2675 2703
@@ -2680,14 +2708,16 @@ impl GammaProps {
2680 match &self.mode {2708 match &self.mode {
2681 GammaMode::Lut { gamma_lut_size, .. } => *gamma_lut_size,2709 GammaMode::Lut { gamma_lut_size, .. } => *gamma_lut_size,
2682 GammaMode::Legacy { gamma_size, .. } => *gamma_size,2710 GammaMode::Legacy { gamma_size, .. } => *gamma_size,
2711+ GammaMode::Ctm { .. } => 256, // Smallest size used by drivers.
2683 }2712 }
2684 }2713 }
2685 2714
2686 fn set_gamma(&mut self, device: &DrmDevice, gamma: Option<Vec<u16>>) -> anyhow::Result<()> {2715 fn set_gamma(&mut self, device: &DrmDevice, gamma: Option<Vec<u16>>) -> anyhow::Result<()> {
2687 let _span = tracy_client::span!("GammaProps::set_gamma");2716 let _span = tracy_client::span!("GammaProps::set_gamma");
2688 2717
2689- let gamma_lut = match &mut self.mode {2718+ let (prop, prop_name) = match &mut self.mode {
2690- GammaMode::Lut { gamma_lut, .. } => *gamma_lut,2719+ GammaMode::Lut { gamma_lut, .. } => (*gamma_lut, "GAMMA_LUT"),
2720+ GammaMode::Ctm { ctm, .. } => (*ctm, "CTM"),
2691 GammaMode::Legacy {2721 GammaMode::Legacy {
2692 gamma_size,2722 gamma_size,
2693 previous_ramp,2723 previous_ramp,
@@ -2712,21 +2742,55 @@ impl GammaProps {
2712 pub blue: u16,2742 pub blue: u16,
2713 pub reserved: u16,2743 pub reserved: u16,
2714 }2744 }
2745+ #[allow(non_camel_case_types)]
2746+ #[repr(C)]
2747+ #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
2748+ pub struct drm_color_ctm {
2749+ pub matrix: [u64; 9],
2750+ }
2715 2751
2716 let (red, rest) = gamma.split_at(gamma_size);2752 let (red, rest) = gamma.split_at(gamma_size);
2717 let (green, blue) = rest.split_at(gamma_size);2753 let (green, blue) = rest.split_at(gamma_size);
2718- let mut data = zip(zip(red, green), blue)2754+ let blob = if let GammaMode::Lut { .. } = self.mode {
2719- .map(|((&red, &green), &blue)| drm_color_lut {2755+ let mut data = zip(zip(red, green), blue)
2720- red,2756+ .map(|((&red, &green), &blue)| drm_color_lut {
2721- green,2757+ red,
2722- blue,2758+ green,
2723- reserved: 0,2759+ blue,
2724- })2760+ reserved: 0,
2725- .collect::<Vec<_>>();2761+ })
2726- let data = cast_slice_mut(&mut data);2762+ .collect::<Vec<_>>();
2727 2763
2728- let blob = drm_ffi::mode::create_property_blob(device.as_fd(), data)2764+ drm_ffi::mode::create_property_blob(device.as_fd(), cast_slice_mut(&mut data))
2729- .context("error creating property blob")?;2765+ .context("error creating property blob")?
2766+ } else {
2767+ /// Transforms a u16 gamma value into a S31.32 value for the CTM matrix.
2768+ fn from_u16_to_s31_32(value: u16) -> u64 {
2769+ let normalized = value as f64 / u16::MAX as f64;
2770+ (normalized * (1u64 << 32) as f64) as u64
2771+ }
2772+
2773+ // See https://invent.kde.org/plasma/kwin/-/commit/f2417a85233e1b7c1c68039230c45554f1069694
2774+ // and https://melissawen.github.io/blog/2023/08/21/amd-steamdeck-colors
2775+ // for context. Create an approximation for color conversion based on
2776+ // a linear interpretation of the gamma ramp received.
2777+ let mut data = drm_color_ctm {
2778+ matrix: [
2779+ from_u16_to_s31_32(red[gamma_size - 1]),
2780+ 0,
2781+ 0,
2782+ 0,
2783+ from_u16_to_s31_32(green[gamma_size - 1]),
2784+ 0,
2785+ 0,
2786+ 0,
2787+ from_u16_to_s31_32(blue[gamma_size - 1]),
2788+ ],
2789+ };
2790+
2791+ drm_ffi::mode::create_property_blob(device.as_fd(), bytes_of_mut(&mut data))
2792+ .context("error creating property blob")?
2793+ };
2730 NonZeroU64::new(u64::from(blob.blob_id))2794 NonZeroU64::new(u64::from(blob.blob_id))
2731 } else {2795 } else {
2732 None2796 None
@@ -2737,22 +2801,24 @@ impl GammaProps {
2737 2801
2738 let blob = blob.map(NonZeroU64::get).unwrap_or(0);2802 let blob = blob.map(NonZeroU64::get).unwrap_or(0);
2739 device2803 device
2740- .set_property(self.crtc, gamma_lut, property::Value::Blob(blob).into())2804+ .set_property(self.crtc, prop, property::Value::Blob(blob).into())
2741- .context("error setting GAMMA_LUT")2805+ .with_context(|| format!("error setting {prop_name}"))
2742 .inspect_err(|_| {2806 .inspect_err(|_| {
2743 if blob != 0 {2807 if blob != 0 {
2744 // Destroy the blob we just allocated.2808 // Destroy the blob we just allocated.
2745 if let Err(err) = device.destroy_property_blob(blob) {2809 if let Err(err) = device.destroy_property_blob(blob) {
2746- warn!("error destroying GAMMA_LUT property blob: {err:?}");2810+ warn!("error destroying {prop_name} property blob: {err:?}");
2747 }2811 }
2748 }2812 }
2749 })?;2813 })?;
2750 }2814 }
2751 2815
2752- if let GammaMode::Lut { previous_blob, .. } = &mut self.mode {2816+ if let GammaMode::Lut { previous_blob, .. } | GammaMode::Ctm { previous_blob, .. } =
2817+ &mut self.mode
2818+ {
2753 if let Some(blob) = mem::replace(previous_blob, blob) {2819 if let Some(blob) = mem::replace(previous_blob, blob) {
2754 if let Err(err) = device.destroy_property_blob(blob.get()) {2820 if let Err(err) = device.destroy_property_blob(blob.get()) {
2755- warn!("error destroying previous GAMMA_LUT blob: {err:?}");2821+ warn!("error destroying previous {prop_name} blob: {err:?}");
2756 }2822 }
2757 }2823 }
2758 } else {2824 } else {
@@ -2777,6 +2843,12 @@ impl GammaProps {
2777 .set_property(self.crtc, *gamma_lut, property::Value::Blob(blob).into())2843 .set_property(self.crtc, *gamma_lut, property::Value::Blob(blob).into())
2778 .context("error setting GAMMA_LUT")?;2844 .context("error setting GAMMA_LUT")?;
2779 }2845 }
2846+ GammaMode::Ctm { ctm, previous_blob } => {
2847+ let blob = previous_blob.map(NonZeroU64::get).unwrap_or(0);
2848+ device
2849+ .set_property(self.crtc, *ctm, property::Value::Blob(blob).into())
2850+ .context("error setting CTM")?;
2851+ }
2780 GammaMode::Legacy {2852 GammaMode::Legacy {
2781 gamma_size,2853 gamma_size,
2782 previous_ramp,2854 previous_ramp,