From 03f35d79447f16d679fc35f8f9f74c2b7299cf2a Mon Sep 17 00:00:00 2001 From: kossLAN Date: Fri, 29 May 2026 12:54:24 -0400 Subject: [PATCH] output: move connector state into type crate --- Cargo.lock | 4 + output-types/Cargo.toml | 4 + output-types/src/lib.rs | 191 +++++++++++++++++++++++++++++++++++++++- src/backend.rs | 151 ++----------------------------- 4 files changed, 207 insertions(+), 143 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b28329c..584b04d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1001,7 +1001,11 @@ name = "jay-output-types" version = "0.1.0" dependencies = [ "blake3", + "jay-cmm", + "jay-formats", "jay-utils", + "linearize", + "uapi", ] [[package]] diff --git a/output-types/Cargo.toml b/output-types/Cargo.toml index 535bda79..5b43cb0e 100644 --- a/output-types/Cargo.toml +++ b/output-types/Cargo.toml @@ -7,6 +7,10 @@ description = "Output identity types for the Jay compositor" repository = "https://github.com/mahkoh/jay" [dependencies] +jay-cmm = { version = "0.1.0", path = "../cmm" } +jay-formats = { version = "0.1.0", path = "../formats" } jay-utils = { version = "0.1.0", path = "../utils" } blake3 = "1.8.2" +linearize = { version = "0.1.3", features = ["derive"] } +uapi = "0.2.13" diff --git a/output-types/src/lib.rs b/output-types/src/lib.rs index 0ea26cdf..7e4029c3 100644 --- a/output-types/src/lib.rs +++ b/output-types/src/lib.rs @@ -1,10 +1,15 @@ use { + jay_cmm::cmm_primaries::Primaries, + jay_formats::Format, jay_utils::numcell::NumCell, + linearize::Linearize, std::{ - fmt::{Display, Formatter}, + fmt::{self, Debug, Display, Formatter}, hash::{Hash, Hasher}, + ops::{BitOr, BitOrAssign}, rc::Rc, }, + uapi::{Packed, Pod}, }; macro_rules! linear_ids { @@ -54,6 +59,70 @@ macro_rules! linear_ids { linear_ids!(ConnectorIds, ConnectorId); linear_ids!(DrmDeviceIds, DrmDeviceId); +linear_ids!( + BackendConnectorStateSerials, + BackendConnectorStateSerial, + u64 +); + +#[derive(Copy, Clone, Eq, PartialEq, Default)] +pub struct ConnectorCaps(pub u32); + +pub const CONCAP_CONNECTOR: ConnectorCaps = ConnectorCaps(1 << 0); +pub const CONCAP_MODE_SETTING: ConnectorCaps = ConnectorCaps(1 << 1); +pub const CONCAP_PHYSICAL_DISPLAY: ConnectorCaps = ConnectorCaps(1 << 2); + +impl ConnectorCaps { + pub fn none() -> Self { + Self(0) + } + + pub fn contains(self, other: Self) -> bool { + self.0 & other.0 == other.0 + } +} + +impl BitOr for ConnectorCaps { + type Output = Self; + + fn bitor(self, rhs: Self) -> Self::Output { + Self(self.0 | rhs.0) + } +} + +impl BitOrAssign for ConnectorCaps { + fn bitor_assign(&mut self, rhs: Self) { + self.0 |= rhs.0; + } +} + +impl Debug for ConnectorCaps { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + let mut any = false; + let mut v = self.0; + for (cap, name) in [ + (CONCAP_CONNECTOR, "CONCAP_CONNECTOR"), + (CONCAP_MODE_SETTING, "CONCAP_MODE_SETTING"), + (CONCAP_PHYSICAL_DISPLAY, "CONCAP_PHYSICAL_DISPLAY"), + ] { + if v & cap.0 == cap.0 { + if any { + write!(f, "|")?; + } + any = true; + write!(f, "{}", name)?; + v &= !cap.0; + } + } + if !any || v != 0 { + if any { + write!(f, "|")?; + } + write!(f, "0x{:x}", v)?; + } + Ok(()) + } +} #[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)] pub struct Mode { @@ -87,6 +156,126 @@ impl Display for Mode { } } +#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Linearize)] +pub enum BackendEotfs { + #[default] + Default, + Pq, +} + +#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Linearize)] +pub enum BackendColorSpace { + #[default] + Default, + Bt2020, +} + +#[derive(Copy, Clone, Debug)] +pub struct BackendLuminance { + pub min: f64, + pub max: f64, + pub max_fall: f64, +} + +impl BackendEotfs { + pub fn to_drm(self) -> u8 { + match self { + BackendEotfs::Default => 0, + BackendEotfs::Pq => 2, + } + } + + pub const fn name(self) -> &'static str { + match self { + BackendEotfs::Default => "default", + BackendEotfs::Pq => "pq", + } + } +} + +impl BackendColorSpace { + pub fn to_drm(self) -> u64 { + match self { + BackendColorSpace::Default => 0, + BackendColorSpace::Bt2020 => 9, + } + } + + pub const fn name(self) -> &'static str { + match self { + BackendColorSpace::Default => "default", + BackendColorSpace::Bt2020 => "bt2020", + } + } +} + +// kernel: struct drm_color_lut +#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)] +#[repr(C)] +pub struct BackendGammaLutElement { + pub red: u16, + pub green: u16, + pub blue: u16, + pub reserved: u16, +} + +unsafe impl Pod for BackendGammaLutElement {} +unsafe impl Packed for BackendGammaLutElement {} + +#[derive(Debug, Eq)] +pub struct BackendGammaLut { + id: [u8; 32], + pub gamma_lut: Vec, +} + +impl BackendGammaLut { + pub fn new(mut gamma_lut: Vec) -> Self { + for element in &mut gamma_lut { + element.reserved = 0; + } + let gamma_lut_bytes = uapi::as_bytes(&gamma_lut as &[_]); + let id = *blake3::hash(gamma_lut_bytes).as_bytes(); + Self { id, gamma_lut } + } +} + +impl PartialEq for BackendGammaLut { + fn eq(&self, other: &Self) -> bool { + self.id == other.id + } +} + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct BackendConnectorState { + pub serial: BackendConnectorStateSerial, + pub enabled: bool, + pub active: bool, + pub mode: Mode, + pub non_desktop_override: Option, + pub vrr: bool, + pub tearing: bool, + pub format: &'static Format, + pub color_space: BackendColorSpace, + pub eotf: BackendEotfs, + pub gamma_lut: Option>, +} + +#[derive(Clone, Debug)] +pub struct MonitorInfo { + pub modes: Option>, + pub output_id: Rc, + pub width_mm: i32, + pub height_mm: i32, + pub non_desktop: bool, + pub non_desktop_effective: bool, + pub vrr_capable: bool, + pub eotfs: Vec, + pub color_spaces: Vec, + pub primaries: Primaries, + pub luminance: Option, + pub state: BackendConnectorState, +} + #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub struct OutputIdHash(pub [u8; 32]); diff --git a/src/backend.rs b/src/backend.rs index 8bb837e7..bca16b22 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -5,31 +5,31 @@ use { BackendConnectorTransaction, BackendConnectorTransactionError, BackendConnectorTransactionType, BackendConnectorTransactionTypeDyn, }, - cmm::cmm_primaries::Primaries, drm_feedback::DrmFeedback, fixed::Fixed, format::Format, gfx_api::{FdSync, GfxApi, GfxFramebuffer}, - video::drm::{ - ConnectorType, DRM_MODE_COLORIMETRY_BT2020_RGB, DRM_MODE_COLORIMETRY_DEFAULT, - DrmConnector, DrmError, DrmVersion, HDMI_EOTF_SMPTE_ST2084, - HDMI_EOTF_TRADITIONAL_GAMMA_SDR, - }, + video::drm::{ConnectorType, DrmConnector, DrmError, DrmVersion}, }, jay_config::input::SwitchEvent, - linearize::Linearize, std::{ any::Any, error::Error, fmt::{Debug, Display, Formatter}, rc::Rc, }, - uapi::{OwnedFd, Packed, Pod, c}, + uapi::{OwnedFd, c}, }; pub mod transaction; -pub use jay_output_types::{ConnectorId, ConnectorIds, DrmDeviceId, DrmDeviceIds, Mode, OutputId}; +pub use jay_output_types::{ + BackendColorSpace, BackendConnectorState, BackendConnectorStateSerial, + BackendConnectorStateSerials, BackendEotfs, BackendGammaLut, BackendGammaLutElement, + BackendLuminance, CONCAP_CONNECTOR, CONCAP_MODE_SETTING, CONCAP_PHYSICAL_DISPLAY, + ConnectorCaps, ConnectorId, ConnectorIds, DrmDeviceId, DrmDeviceIds, Mode, MonitorInfo, + OutputId, +}; pub use jay_input_types::{ AXIS_120, AxisSource, ButtonState, InputDeviceAccelProfile, InputDeviceCapability, @@ -64,22 +64,6 @@ pub trait Backend: Any { } } -#[derive(Clone, Debug)] -pub struct MonitorInfo { - pub modes: Option>, - pub output_id: Rc, - pub width_mm: i32, - pub height_mm: i32, - pub non_desktop: bool, - pub non_desktop_effective: bool, - pub vrr_capable: bool, - pub eotfs: Vec, - pub color_spaces: Vec, - pub primaries: Primaries, - pub luminance: Option, - pub state: BackendConnectorState, -} - #[derive(Copy, Clone, Debug)] pub struct ConnectorKernelId { pub ty: ConnectorType, @@ -92,13 +76,6 @@ impl Display for ConnectorKernelId { } } -bitflags! { - ConnectorCaps: u32; - CONCAP_CONNECTOR, - CONCAP_MODE_SETTING, - CONCAP_PHYSICAL_DISPLAY, -} - pub trait Connector: Any { fn id(&self) -> ConnectorId; fn kernel_id(&self) -> ConnectorKernelId; @@ -457,113 +434,3 @@ pub trait BackendDrmLease { pub trait BackendDrmLessee { fn created(&self, lease: Rc); } - -#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Linearize)] -pub enum BackendEotfs { - #[default] - Default, - Pq, -} - -#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Linearize)] -pub enum BackendColorSpace { - #[default] - Default, - Bt2020, -} - -#[derive(Copy, Clone, Debug)] -pub struct BackendLuminance { - pub min: f64, - pub max: f64, - pub max_fall: f64, -} - -impl BackendEotfs { - pub fn to_drm(self) -> u8 { - match self { - BackendEotfs::Default => HDMI_EOTF_TRADITIONAL_GAMMA_SDR, - BackendEotfs::Pq => HDMI_EOTF_SMPTE_ST2084, - } - } - - pub const fn name(self) -> &'static str { - match self { - BackendEotfs::Default => "default", - BackendEotfs::Pq => "pq", - } - } -} - -impl BackendColorSpace { - pub fn to_drm(self) -> u64 { - match self { - BackendColorSpace::Default => DRM_MODE_COLORIMETRY_DEFAULT, - BackendColorSpace::Bt2020 => DRM_MODE_COLORIMETRY_BT2020_RGB, - } - } - - pub const fn name(self) -> &'static str { - match self { - BackendColorSpace::Default => "default", - BackendColorSpace::Bt2020 => "bt2020", - } - } -} - -// kernel: struct drm_color_lut -#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)] -#[repr(C)] -pub struct BackendGammaLutElement { - pub red: u16, - pub green: u16, - pub blue: u16, - pub reserved: u16, -} - -unsafe impl Pod for BackendGammaLutElement {} -unsafe impl Packed for BackendGammaLutElement {} - -#[derive(Debug, Eq)] -pub struct BackendGammaLut { - id: [u8; 32], - pub gamma_lut: Vec, -} - -impl BackendGammaLut { - pub fn new(mut gamma_lut: Vec) -> Self { - for element in &mut gamma_lut { - element.reserved = 0; - } - let gamma_lut_bytes = uapi::as_bytes(&gamma_lut as &[_]); - let id = *blake3::hash(gamma_lut_bytes).as_bytes(); - Self { id, gamma_lut } - } -} - -impl PartialEq for BackendGammaLut { - fn eq(&self, other: &Self) -> bool { - self.id == other.id - } -} - -linear_ids!( - BackendConnectorStateSerials, - BackendConnectorStateSerial, - u64 -); - -#[derive(Clone, Debug, Eq, PartialEq)] -pub struct BackendConnectorState { - pub serial: BackendConnectorStateSerial, - pub enabled: bool, - pub active: bool, - pub mode: Mode, - pub non_desktop_override: Option, - pub vrr: bool, - pub tearing: bool, - pub format: &'static Format, - pub color_space: BackendColorSpace, - pub eotf: BackendEotfs, - pub gamma_lut: Option>, -}