Implement gamma adjustment via GAMMA_LUT property
Browse files at this commit6839a118bb691fc022c8ac9e2564717ae995b5ceParent: 9ae3cad3 changed files +199 -17
- Cargo.lock +1 -0
- Cargo.toml +2 -1
- src/backend/tty.rs +196 -16
Cargo.lockmodified+1 -0
| @@ -2123,6 +2123,7 @@ dependencies = [ | |||
| 2123 | "calloop 0.13.0", | 2123 | "calloop 0.13.0", |
| 2124 | "clap", | 2124 | "clap", |
| 2125 | "directories", | 2125 | "directories", |
| 2126 | + "drm-ffi", | ||
| 2126 | "futures-util", | 2127 | "futures-util", |
| 2127 | "git-version", | 2128 | "git-version", |
| 2128 | "glam", | 2129 | "glam", |
Cargo.tomlmodified+2 -1
| @@ -45,10 +45,11 @@ arrayvec = "0.7.4" | |||
| 45 | async-channel = { version = "2.2.0", optional = true } | 45 | async-channel = { version = "2.2.0", optional = true } |
| 46 | async-io = { version = "1.13.0", optional = true } | 46 | async-io = { version = "1.13.0", optional = true } |
| 47 | bitflags = "2.4.2" | 47 | bitflags = "2.4.2" |
| 48 | -bytemuck = "1.14.3" | 48 | +bytemuck = { version = "1.14.3", features = ["derive"] } |
| 49 | calloop = { version = "0.13.0", features = ["executor", "futures-io"] } | 49 | calloop = { version = "0.13.0", features = ["executor", "futures-io"] } |
| 50 | clap = { workspace = true, features = ["string"] } | 50 | clap = { workspace = true, features = ["string"] } |
| 51 | directories = "5.0.1" | 51 | directories = "5.0.1" |
| 52 | +drm-ffi = "0.7.1" | ||
| 52 | futures-util = { version = "0.3.30", default-features = false, features = ["std", "io"] } | 53 | futures-util = { version = "0.3.30", default-features = false, features = ["std", "io"] } |
| 53 | git-version = "0.3.9" | 54 | git-version = "0.3.9" |
| 54 | glam = "0.25.0" | 55 | glam = "0.25.0" |
src/backend/tty.rsmodified+196 -16
| @@ -2,6 +2,8 @@ use std::cell::RefCell; | |||
| 2 | use std::collections::{HashMap, HashSet}; | 2 | use std::collections::{HashMap, HashSet}; |
| 3 | use std::fmt::Write; | 3 | use std::fmt::Write; |
| 4 | use std::iter::zip; | 4 | use std::iter::zip; |
| 5 | +use std::num::NonZeroU64; | ||
| 6 | +use std::os::fd::AsFd; | ||
| 5 | use std::panic::{catch_unwind, AssertUnwindSafe}; | 7 | use std::panic::{catch_unwind, AssertUnwindSafe}; |
| 6 | use std::path::Path; | 8 | use std::path::Path; |
| 7 | use std::rc::Rc; | 9 | use std::rc::Rc; |
| @@ -10,6 +12,7 @@ use std::time::Duration; | |||
| 10 | use std::{io, mem}; | 12 | use std::{io, mem}; |
| 11 | 13 | ||
| 12 | use anyhow::{anyhow, bail, ensure, Context}; | 14 | use anyhow::{anyhow, bail, ensure, Context}; |
| 15 | +use bytemuck::cast_slice_mut; | ||
| 13 | use libc::dev_t; | 16 | use libc::dev_t; |
| 14 | use niri_config::Config; | 17 | use niri_config::Config; |
| 15 | use smithay::backend::allocator::dmabuf::Dmabuf; | 18 | use smithay::backend::allocator::dmabuf::Dmabuf; |
| @@ -165,6 +168,7 @@ struct Surface { | |||
| 165 | name: String, | 168 | name: String, |
| 166 | compositor: GbmDrmCompositor, | 169 | compositor: GbmDrmCompositor, |
| 167 | dmabuf_feedback: Option<SurfaceDmabufFeedback>, | 170 | dmabuf_feedback: Option<SurfaceDmabufFeedback>, |
| 171 | + gamma_props: Option<GammaProps>, | ||
| 168 | /// Tracy frame that goes from vblank to vblank. | 172 | /// Tracy frame that goes from vblank to vblank. |
| 169 | vblank_frame: Option<tracy_client::Frame>, | 173 | vblank_frame: Option<tracy_client::Frame>, |
| 170 | /// Frame name for the VBlank frame. | 174 | /// Frame name for the VBlank frame. |
| @@ -181,6 +185,13 @@ pub struct SurfaceDmabufFeedback { | |||
| 181 | pub scanout: DmabufFeedback, | 185 | pub scanout: DmabufFeedback, |
| 182 | } | 186 | } |
| 183 | 187 | ||
| 188 | +struct GammaProps { | ||
| 189 | + crtc: crtc::Handle, | ||
| 190 | + gamma_lut: property::Handle, | ||
| 191 | + gamma_lut_size: property::Handle, | ||
| 192 | + previous_blob: Option<NonZeroU64>, | ||
| 193 | +} | ||
| 194 | + | ||
| 184 | impl Tty { | 195 | impl Tty { |
| 185 | pub fn new( | 196 | pub fn new( |
| 186 | config: Rc<RefCell<Config>>, | 197 | config: Rc<RefCell<Config>>, |
| @@ -703,8 +714,17 @@ impl Tty { | |||
| 703 | Err(err) => debug!("error setting max bpc: {err:?}"), | 714 | Err(err) => debug!("error setting max bpc: {err:?}"), |
| 704 | } | 715 | } |
| 705 | 716 | ||
| 706 | - // Reset gamma to linear in case it was set before. | 717 | + let mut gamma_props = GammaProps::new(&device.drm, crtc) |
| 707 | - if let Err(err) = set_gamma_for_crtc(&device.drm, crtc, None) { | 718 | + .map_err(|err| debug!("error getting gamma properties: {err:?}")) |
| 719 | + .ok(); | ||
| 720 | + | ||
| 721 | + // Reset gamma in case it was set before. | ||
| 722 | + let res = if let Some(gamma_props) = &mut gamma_props { | ||
| 723 | + gamma_props.set_gamma(&device.drm, None) | ||
| 724 | + } else { | ||
| 725 | + set_gamma_for_crtc(&device.drm, crtc, None) | ||
| 726 | + }; | ||
| 727 | + if let Err(err) = res { | ||
| 708 | debug!("error resetting gamma: {err:?}"); | 728 | debug!("error resetting gamma: {err:?}"); |
| 709 | } | 729 | } |
| 710 | 730 | ||
| @@ -818,6 +838,7 @@ impl Tty { | |||
| 818 | name: output_name.clone(), | 838 | name: output_name.clone(), |
| 819 | compositor, | 839 | compositor, |
| 820 | dmabuf_feedback, | 840 | dmabuf_feedback, |
| 841 | + gamma_props, | ||
| 821 | vblank_frame: None, | 842 | vblank_frame: None, |
| 822 | vblank_frame_name, | 843 | vblank_frame_name, |
| 823 | time_since_presentation_plot_name, | 844 | time_since_presentation_plot_name, |
| @@ -1258,20 +1279,34 @@ impl Tty { | |||
| 1258 | .devices | 1279 | .devices |
| 1259 | .get(&tty_state.node) | 1280 | .get(&tty_state.node) |
| 1260 | .context("missing device")?; | 1281 | .context("missing device")?; |
| 1261 | - let info = device | 1282 | + |
| 1262 | - .drm | 1283 | + let surface = device.surfaces.get(&crtc).context("missing surface")?; |
| 1263 | - .get_crtc(crtc) | 1284 | + if let Some(gamma_props) = &surface.gamma_props { |
| 1264 | - .context("error getting crtc info")?; | 1285 | + gamma_props.gamma_size(&device.drm) |
| 1265 | - Ok(info.gamma_length()) | 1286 | + } else { |
| 1287 | + let info = device | ||
| 1288 | + .drm | ||
| 1289 | + .get_crtc(crtc) | ||
| 1290 | + .context("error getting crtc info")?; | ||
| 1291 | + Ok(info.gamma_length()) | ||
| 1292 | + } | ||
| 1266 | } | 1293 | } |
| 1267 | 1294 | ||
| 1268 | - pub fn set_gamma(&self, output: &Output, ramp: Option<&[u16]>) -> anyhow::Result<()> { | 1295 | + pub fn set_gamma(&mut self, output: &Output, ramp: Option<&[u16]>) -> anyhow::Result<()> { |
| 1269 | let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); | 1296 | let tty_state = output.user_data().get::<TtyOutputState>().unwrap(); |
| 1297 | + let crtc = tty_state.crtc; | ||
| 1298 | + | ||
| 1270 | let device = self | 1299 | let device = self |
| 1271 | .devices | 1300 | .devices |
| 1272 | - .get(&tty_state.node) | 1301 | + .get_mut(&tty_state.node) |
| 1273 | .context("missing device")?; | 1302 | .context("missing device")?; |
| 1274 | - set_gamma_for_crtc(&device.drm, tty_state.crtc, ramp) | 1303 | + |
| 1304 | + let surface = device.surfaces.get_mut(&crtc).context("missing surface")?; | ||
| 1305 | + if let Some(gamma_props) = &mut surface.gamma_props { | ||
| 1306 | + gamma_props.set_gamma(&device.drm, ramp) | ||
| 1307 | + } else { | ||
| 1308 | + set_gamma_for_crtc(&device.drm, crtc, ramp) | ||
| 1309 | + } | ||
| 1275 | } | 1310 | } |
| 1276 | 1311 | ||
| 1277 | fn refresh_ipc_outputs(&self) { | 1312 | fn refresh_ipc_outputs(&self) { |
| @@ -1525,6 +1560,130 @@ impl Tty { | |||
| 1525 | } | 1560 | } |
| 1526 | } | 1561 | } |
| 1527 | 1562 | ||
| 1563 | +impl GammaProps { | ||
| 1564 | + fn new(device: &DrmDevice, crtc: crtc::Handle) -> anyhow::Result<Self> { | ||
| 1565 | + let mut gamma_lut = None; | ||
| 1566 | + let mut gamma_lut_size = None; | ||
| 1567 | + | ||
| 1568 | + let props = device | ||
| 1569 | + .get_properties(crtc) | ||
| 1570 | + .context("error getting properties")?; | ||
| 1571 | + for (prop, _) in props { | ||
| 1572 | + let Ok(info) = device.get_property(prop) else { | ||
| 1573 | + continue; | ||
| 1574 | + }; | ||
| 1575 | + | ||
| 1576 | + let Ok(name) = info.name().to_str() else { | ||
| 1577 | + continue; | ||
| 1578 | + }; | ||
| 1579 | + | ||
| 1580 | + match name { | ||
| 1581 | + "GAMMA_LUT" => { | ||
| 1582 | + ensure!( | ||
| 1583 | + matches!(info.value_type(), property::ValueType::Blob), | ||
| 1584 | + "wrong GAMMA_LUT value type" | ||
| 1585 | + ); | ||
| 1586 | + gamma_lut = Some(prop); | ||
| 1587 | + } | ||
| 1588 | + "GAMMA_LUT_SIZE" => { | ||
| 1589 | + ensure!( | ||
| 1590 | + matches!(info.value_type(), property::ValueType::UnsignedRange(_, _)), | ||
| 1591 | + "wrong GAMMA_LUT_SIZE value type" | ||
| 1592 | + ); | ||
| 1593 | + gamma_lut_size = Some(prop); | ||
| 1594 | + } | ||
| 1595 | + _ => (), | ||
| 1596 | + } | ||
| 1597 | + } | ||
| 1598 | + | ||
| 1599 | + let gamma_lut = gamma_lut.context("missing GAMMA_LUT property")?; | ||
| 1600 | + let gamma_lut_size = gamma_lut_size.context("missing GAMMA_LUT_SIZE property")?; | ||
| 1601 | + | ||
| 1602 | + Ok(Self { | ||
| 1603 | + crtc, | ||
| 1604 | + gamma_lut, | ||
| 1605 | + gamma_lut_size, | ||
| 1606 | + previous_blob: None, | ||
| 1607 | + }) | ||
| 1608 | + } | ||
| 1609 | + | ||
| 1610 | + fn gamma_size(&self, device: &DrmDevice) -> anyhow::Result<u32> { | ||
| 1611 | + let value = get_drm_property(device, self.crtc, self.gamma_lut_size) | ||
| 1612 | + .context("missing GAMMA_LUT_SIZE property")?; | ||
| 1613 | + Ok(value as u32) | ||
| 1614 | + } | ||
| 1615 | + | ||
| 1616 | + fn set_gamma(&mut self, device: &DrmDevice, gamma: Option<&[u16]>) -> anyhow::Result<()> { | ||
| 1617 | + let _span = tracy_client::span!("GammaProps::set_gamma"); | ||
| 1618 | + | ||
| 1619 | + let blob = if let Some(gamma) = gamma { | ||
| 1620 | + let gamma_size = self | ||
| 1621 | + .gamma_size(device) | ||
| 1622 | + .context("error getting gamma size")? as usize; | ||
| 1623 | + | ||
| 1624 | + ensure!(gamma.len() == gamma_size * 3, "wrong gamma length"); | ||
| 1625 | + | ||
| 1626 | + #[allow(non_camel_case_types)] | ||
| 1627 | + #[repr(C)] | ||
| 1628 | + #[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)] | ||
| 1629 | + pub struct drm_color_lut { | ||
| 1630 | + pub red: u16, | ||
| 1631 | + pub green: u16, | ||
| 1632 | + pub blue: u16, | ||
| 1633 | + pub reserved: u16, | ||
| 1634 | + } | ||
| 1635 | + | ||
| 1636 | + let (red, rest) = gamma.split_at(gamma_size); | ||
| 1637 | + let (blue, green) = rest.split_at(gamma_size); | ||
| 1638 | + let mut data = zip(zip(red, blue), green) | ||
| 1639 | + .map(|((&red, &green), &blue)| drm_color_lut { | ||
| 1640 | + red, | ||
| 1641 | + green, | ||
| 1642 | + blue, | ||
| 1643 | + reserved: 0, | ||
| 1644 | + }) | ||
| 1645 | + .collect::<Vec<_>>(); | ||
| 1646 | + let data = cast_slice_mut(&mut data); | ||
| 1647 | + | ||
| 1648 | + let blob = drm_ffi::mode::create_property_blob(device.as_fd(), data) | ||
| 1649 | + .context("error creating property blob")?; | ||
| 1650 | + NonZeroU64::new(u64::from(blob.blob_id)) | ||
| 1651 | + } else { | ||
| 1652 | + None | ||
| 1653 | + }; | ||
| 1654 | + | ||
| 1655 | + { | ||
| 1656 | + let _span = tracy_client::span!("set_property"); | ||
| 1657 | + | ||
| 1658 | + let blob = blob.map(NonZeroU64::get).unwrap_or(0); | ||
| 1659 | + device | ||
| 1660 | + .set_property( | ||
| 1661 | + self.crtc, | ||
| 1662 | + self.gamma_lut, | ||
| 1663 | + property::Value::Blob(blob).into(), | ||
| 1664 | + ) | ||
| 1665 | + .context("error setting GAMMA_LUT") | ||
| 1666 | + .map_err(|err| { | ||
| 1667 | + if blob != 0 { | ||
| 1668 | + // Destroy the blob we just allocated. | ||
| 1669 | + if let Err(err) = device.destroy_property_blob(blob) { | ||
| 1670 | + warn!("error destroying GAMMA_LUT property blob: {err:?}"); | ||
| 1671 | + } | ||
| 1672 | + } | ||
| 1673 | + err | ||
| 1674 | + })?; | ||
| 1675 | + } | ||
| 1676 | + | ||
| 1677 | + if let Some(blob) = mem::replace(&mut self.previous_blob, blob) { | ||
| 1678 | + if let Err(err) = device.destroy_property_blob(blob.get()) { | ||
| 1679 | + warn!("error destroying previous GAMMA_LUT blob: {err:?}"); | ||
| 1680 | + } | ||
| 1681 | + } | ||
| 1682 | + | ||
| 1683 | + Ok(()) | ||
| 1684 | + } | ||
| 1685 | +} | ||
| 1686 | + | ||
| 1528 | fn primary_node_from_config(config: &Config) -> Option<(DrmNode, DrmNode)> { | 1687 | fn primary_node_from_config(config: &Config) -> Option<(DrmNode, DrmNode)> { |
| 1529 | let path = config.debug.render_drm_device.as_ref()?; | 1688 | let path = config.debug.render_drm_device.as_ref()?; |
| 1530 | debug!("attempting to use render node from config: {path:?}"); | 1689 | debug!("attempting to use render node from config: {path:?}"); |
| @@ -1606,7 +1765,11 @@ fn surface_dmabuf_feedback( | |||
| 1606 | Ok(SurfaceDmabufFeedback { render, scanout }) | 1765 | Ok(SurfaceDmabufFeedback { render, scanout }) |
| 1607 | } | 1766 | } |
| 1608 | 1767 | ||
| 1609 | -fn find_drm_property(drm: &DrmDevice, crtc: crtc::Handle, name: &str) -> Option<property::Handle> { | 1768 | +fn find_drm_property( |
| 1769 | + drm: &DrmDevice, | ||
| 1770 | + crtc: crtc::Handle, | ||
| 1771 | + name: &str, | ||
| 1772 | +) -> Option<(property::Handle, property::RawValue)> { | ||
| 1610 | let props = match drm.get_properties(crtc) { | 1773 | let props = match drm.get_properties(crtc) { |
| 1611 | Ok(props) => props, | 1774 | Ok(props) => props, |
| 1612 | Err(err) => { | 1775 | Err(err) => { |
| @@ -1615,17 +1778,34 @@ fn find_drm_property(drm: &DrmDevice, crtc: crtc::Handle, name: &str) -> Option< | |||
| 1615 | } | 1778 | } |
| 1616 | }; | 1779 | }; |
| 1617 | 1780 | ||
| 1618 | - let (handles, _) = props.as_props_and_values(); | 1781 | + props.into_iter().find_map(|(handle, value)| { |
| 1619 | - handles.iter().find_map(|handle| { | 1782 | + let info = drm.get_property(handle).ok()?; |
| 1620 | - let info = drm.get_property(*handle).ok()?; | ||
| 1621 | let n = info.name().to_str().ok()?; | 1783 | let n = info.name().to_str().ok()?; |
| 1622 | 1784 | ||
| 1623 | - (n == name).then_some(*handle) | 1785 | + (n == name).then_some((handle, value)) |
| 1624 | }) | 1786 | }) |
| 1625 | } | 1787 | } |
| 1626 | 1788 | ||
| 1789 | +fn get_drm_property( | ||
| 1790 | + drm: &DrmDevice, | ||
| 1791 | + crtc: crtc::Handle, | ||
| 1792 | + prop: property::Handle, | ||
| 1793 | +) -> Option<property::RawValue> { | ||
| 1794 | + let props = match drm.get_properties(crtc) { | ||
| 1795 | + Ok(props) => props, | ||
| 1796 | + Err(err) => { | ||
| 1797 | + warn!("error getting CRTC properties: {err:?}"); | ||
| 1798 | + return None; | ||
| 1799 | + } | ||
| 1800 | + }; | ||
| 1801 | + | ||
| 1802 | + props | ||
| 1803 | + .into_iter() | ||
| 1804 | + .find_map(|(handle, value)| (handle == prop).then_some(value)) | ||
| 1805 | +} | ||
| 1806 | + | ||
| 1627 | fn set_crtc_active(drm: &DrmDevice, crtc: crtc::Handle, active: bool) { | 1807 | fn set_crtc_active(drm: &DrmDevice, crtc: crtc::Handle, active: bool) { |
| 1628 | - let Some(prop) = find_drm_property(drm, crtc, "ACTIVE") else { | 1808 | + let Some((prop, _)) = find_drm_property(drm, crtc, "ACTIVE") else { |
| 1629 | return; | 1809 | return; |
| 1630 | }; | 1810 | }; |
| 1631 | 1811 | ||