forge
Explore
Log in

Don't you dare go Hollow

Reset HDR connector properties

Browse files at this commit

969f382e3e52f2a6c9d5f398d19f36dc32df0dd5Michael YangParent: 2865ec3
  • feat
    • reset HDR connector properties
Show full message
feat: reset HDR connector properties
src/backend/tty.rsmodified+112 -31
@@ -250,6 +250,12 @@ struct GammaProps {
250 previous_blob: Option<NonZeroU64>,250 previous_blob: Option<NonZeroU64>,
251 }251 }
252 252
253+struct ConnectorProperties<'a> {
254+ device: &'a DrmDevice,
255+ connector: connector::Handle,
256+ properties: Vec<(property::Info, property::RawValue)>,
257+}
258+
253 impl Tty {259 impl Tty {
254 pub fn new(260 pub fn new(
255 config: Rc<RefCell<Config>>,261 config: Rc<RefCell<Config>>,
@@ -857,14 +863,23 @@ impl Tty {
857 }863 }
858 debug!("picking mode: {mode:?}");864 debug!("picking mode: {mode:?}");
859 865
860- if !niri.config.borrow().debug.keep_max_bpc_unchanged {866+ if let Ok(props) = ConnectorProperties::try_new(&device.drm, connector.handle()) {
861- // We only use 8888 RGB formats, so set max bpc to 8 to allow more types of links to867+ match reset_hdr(&props) {
862- // run.868+ Ok(()) => debug!("reset HDR properties"),
863- match set_max_bpc(&device.drm, connector.handle(), 8) {869+ Err(err) => debug!("error resetting HDR properties: {err:?}"),
864- Ok(bpc) => debug!("set max bpc to {bpc}"),
865- Err(err) => debug!("error setting max bpc: {err:?}"),
866 }870 }
867- }871+
872+ if !niri.config.borrow().debug.keep_max_bpc_unchanged {
873+ // We only use 8888 RGB formats, so set max bpc to 8 to allow more types of links to
874+ // run.
875+ match set_max_bpc(&props, 8) {
876+ Ok(bpc) => debug!("set max bpc to {bpc}"),
877+ Err(err) => debug!("error setting max bpc: {err:?}"),
878+ }
879+ }
880+ } else {
881+ warn!("failed to get connector properties");
882+ };
868 883
869 let mut gamma_props = GammaProps::new(&device.drm, crtc)884 let mut gamma_props = GammaProps::new(&device.drm, crtc)
870 .map_err(|err| debug!("error getting gamma properties: {err:?}"))885 .map_err(|err| debug!("error getting gamma properties: {err:?}"))
@@ -1825,6 +1840,20 @@ impl Tty {
1825 continue;1840 continue;
1826 }1841 }
1827 1842
1843+ if let Ok(props) = ConnectorProperties::try_new(&device.drm, surface.connector) {
1844+ match reset_hdr(&props) {
1845+ Ok(()) => {
1846+ debug!("output {:?}: reset HDR properties", surface.name.connector)
1847+ }
1848+ Err(err) => debug!(
1849+ "output {:?} HDR: error resetting HDR properties: {err:?}",
1850+ surface.name.connector
1851+ ),
1852+ }
1853+ } else {
1854+ warn!("failed to get connector properties");
1855+ };
1856+
1828 // Check if we need to change the mode.1857 // Check if we need to change the mode.
1829 let Some(connector) = device.drm_scanner.connectors().get(&surface.connector)1858 let Some(connector) = device.drm_scanner.connectors().get(&surface.connector)
1830 else {1859 else {
@@ -2452,39 +2481,91 @@ fn get_edid_info(
2452 libdisplay_info::info::Info::parse_edid(&data).context("error parsing EDID")2481 libdisplay_info::info::Info::parse_edid(&data).context("error parsing EDID")
2453 }2482 }
2454 2483
2455-fn set_max_bpc(device: &DrmDevice, connector: connector::Handle, bpc: u64) -> anyhow::Result<u64> {2484+impl<'a> ConnectorProperties<'a> {
2456- let props = device2485+ fn try_new(device: &'a DrmDevice, connector: connector::Handle) -> anyhow::Result<Self> {
2457- .get_properties(connector)2486+ let prop_vals = device
2458- .context("error getting properties")?;2487+ .get_properties(connector)
2459- for (prop, value) in props {2488+ .context("error getting properties")?;
2460- let info = device
2461- .get_property(prop)
2462- .context("error getting property")?;
2463- if info.name().to_str() != Ok("max bpc") {
2464- continue;
2465- }
2466 2489
2467- let property::ValueType::UnsignedRange(min, max) = info.value_type() else {2490+ let mut properties = Vec::new();
2468- bail!("wrong property type")
2469- };
2470 2491
2471- let bpc = bpc.clamp(min, max);2492+ for (prop, value) in prop_vals {
2493+ let info = device
2494+ .get_property(prop)
2495+ .context("error getting property")?;
2472 2496
2473- let property::Value::UnsignedRange(value) = info.value_type().convert_value(value) else {2497+ properties.push((info, value));
2474- bail!("wrong property type")
2475- };
2476- if value == bpc {
2477- return Ok(bpc);
2478 }2498 }
2479 2499
2480- device2500+ Ok(Self {
2481- .set_property(connector, prop, property::Value::UnsignedRange(bpc).into())2501+ device,
2502+ connector,
2503+ properties,
2504+ })
2505+ }
2506+
2507+ fn find(&self, name: &std::ffi::CStr) -> anyhow::Result<&(property::Info, property::RawValue)> {
2508+ for prop in &self.properties {
2509+ if prop.0.name() == name {
2510+ return Ok(prop);
2511+ }
2512+ }
2513+
2514+ Err(anyhow!("couldn't find property: {name:?}"))
2515+ }
2516+}
2517+
2518+const DRM_MODE_COLORIMETRY_DEFAULT: u64 = 0;
2519+
2520+fn reset_hdr(props: &ConnectorProperties) -> anyhow::Result<()> {
2521+ let (info, value) = props.find(c"HDR_OUTPUT_METADATA")?;
2522+ let property::ValueType::Blob = info.value_type() else {
2523+ bail!("wrong property type")
2524+ };
2525+
2526+ if *value != 0 {
2527+ props
2528+ .device
2529+ .set_property(props.connector, info.handle(), 0)
2482 .context("error setting property")?;2530 .context("error setting property")?;
2531+ }
2483 2532
2484- return Ok(bpc);2533+ let (info, value) = props.find(c"Colorspace")?;
2534+ let property::ValueType::Enum(_) = info.value_type() else {
2535+ bail!("wrong property type")
2536+ };
2537+ if *value != DRM_MODE_COLORIMETRY_DEFAULT {
2538+ props
2539+ .device
2540+ .set_property(props.connector, info.handle(), DRM_MODE_COLORIMETRY_DEFAULT)
2541+ .context("error setting property")?;
2485 }2542 }
2486 2543
2487- Err(anyhow!("couldn't find max bpc property"))2544+ Ok(())
2545+}
2546+
2547+fn set_max_bpc(props: &ConnectorProperties, bpc: u64) -> anyhow::Result<u64> {
2548+ let (info, value) = props.find(c"max bpc")?;
2549+ let property::ValueType::UnsignedRange(min, max) = info.value_type() else {
2550+ bail!("wrong property type")
2551+ };
2552+
2553+ let bpc = bpc.clamp(min, max);
2554+ let property::Value::UnsignedRange(value) = info.value_type().convert_value(*value) else {
2555+ bail!("wrong property type")
2556+ };
2557+
2558+ if value != bpc {
2559+ props
2560+ .device
2561+ .set_property(
2562+ props.connector,
2563+ info.handle(),
2564+ property::Value::UnsignedRange(bpc).into(),
2565+ )
2566+ .context("error setting property")?;
2567+ }
2568+ Ok(bpc)
2488 }2569 }
2489 2570
2490 fn is_vrr_capable(device: &DrmDevice, connector: connector::Handle) -> Option<bool> {2571 fn is_vrr_capable(device: &DrmDevice, connector: connector::Handle) -> Option<bool> {