workspace: move crates under crates
This commit is contained in:
parent
0016bc8cf0
commit
6393fdf3c0
354 changed files with 102 additions and 102 deletions
348
crates/output-types/src/lib.rs
Normal file
348
crates/output-types/src/lib.rs
Normal file
|
|
@ -0,0 +1,348 @@
|
|||
use {
|
||||
jay_cmm::cmm_primaries::Primaries,
|
||||
jay_formats::Format,
|
||||
jay_utils::numcell::NumCell,
|
||||
linearize::Linearize,
|
||||
std::{
|
||||
fmt::{self, Debug, Display, Formatter},
|
||||
hash::{Hash, Hasher},
|
||||
ops::{BitOr, BitOrAssign},
|
||||
rc::Rc,
|
||||
},
|
||||
uapi::{Packed, Pod},
|
||||
};
|
||||
|
||||
macro_rules! linear_ids {
|
||||
($ids:ident, $id:ident $(,)?) => {
|
||||
linear_ids!($ids, $id, u32);
|
||||
};
|
||||
($ids:ident, $id:ident, $ty:ty $(,)?) => {
|
||||
#[derive(Debug)]
|
||||
pub struct $ids {
|
||||
next: NumCell<$ty>,
|
||||
}
|
||||
|
||||
impl Default for $ids {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
next: NumCell::new(1),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl $ids {
|
||||
pub fn next(&self) -> $id {
|
||||
$id(self.next.fetch_add(1))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
|
||||
pub struct $id($ty);
|
||||
|
||||
impl $id {
|
||||
pub fn raw(&self) -> $ty {
|
||||
self.0
|
||||
}
|
||||
|
||||
pub fn from_raw(id: $ty) -> Self {
|
||||
Self(id)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for $id {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
Display::fmt(&self.0, f)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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 {
|
||||
pub width: i32,
|
||||
pub height: i32,
|
||||
pub refresh_rate_millihz: u32,
|
||||
}
|
||||
|
||||
impl Mode {
|
||||
pub fn refresh_nsec(&self) -> u64 {
|
||||
match self.refresh_rate_millihz {
|
||||
0 => u64::MAX,
|
||||
n => 1_000_000_000_000 / (n as u64),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size(&self) -> (i32, i32) {
|
||||
(self.width, self.height)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Mode {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}x{}@{}",
|
||||
self.width,
|
||||
self.height,
|
||||
self.refresh_rate_millihz as f64 / 1000.0,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[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<BackendGammaLutElement>,
|
||||
}
|
||||
|
||||
impl BackendGammaLut {
|
||||
pub fn new(mut gamma_lut: Vec<BackendGammaLutElement>) -> 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<bool>,
|
||||
pub vrr: bool,
|
||||
pub tearing: bool,
|
||||
pub format: &'static Format,
|
||||
pub color_space: BackendColorSpace,
|
||||
pub eotf: BackendEotfs,
|
||||
pub gamma_lut: Option<Rc<BackendGammaLut>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct MonitorInfo {
|
||||
pub modes: Option<Vec<Mode>>,
|
||||
pub output_id: Rc<OutputId>,
|
||||
pub width_mm: i32,
|
||||
pub height_mm: i32,
|
||||
pub non_desktop: bool,
|
||||
pub non_desktop_effective: bool,
|
||||
pub vrr_capable: bool,
|
||||
pub eotfs: Vec<BackendEotfs>,
|
||||
pub color_spaces: Vec<BackendColorSpace>,
|
||||
pub primaries: Primaries,
|
||||
pub luminance: Option<BackendLuminance>,
|
||||
pub state: BackendConnectorState,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||
pub struct OutputIdHash(pub [u8; 32]);
|
||||
|
||||
impl OutputIdHash {
|
||||
pub fn hash(t: impl AsRef<[u8]>) -> Self {
|
||||
Self(*blake3::hash(t.as_ref()).as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Eq, Debug)]
|
||||
pub struct OutputId {
|
||||
pub _connector: Option<String>,
|
||||
pub manufacturer: String,
|
||||
pub model: String,
|
||||
pub serial_number: String,
|
||||
pub hash: OutputIdHash,
|
||||
}
|
||||
|
||||
impl PartialEq for OutputId {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.hash == other.hash
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for OutputId {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.hash.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl OutputId {
|
||||
pub fn new(
|
||||
connector: impl Into<String>,
|
||||
manufacturer: impl Into<String>,
|
||||
model: impl Into<String>,
|
||||
serial_number: impl Into<String>,
|
||||
) -> Rc<Self> {
|
||||
let connector = connector.into();
|
||||
let manufacturer = manufacturer.into();
|
||||
let model = model.into();
|
||||
let serial_number = serial_number.into();
|
||||
Self::new_(connector, manufacturer, model, serial_number)
|
||||
}
|
||||
|
||||
fn new_(
|
||||
connector: String,
|
||||
manufacturer: String,
|
||||
model: String,
|
||||
serial_number: String,
|
||||
) -> Rc<Self> {
|
||||
let connector = serial_number.is_empty().then_some(connector);
|
||||
let mut hasher = blake3::Hasher::new();
|
||||
hasher.update(&[connector.is_some() as u8]);
|
||||
let mut hash = |s: &str| {
|
||||
hasher.update(&(s.len() as u64).to_le_bytes());
|
||||
hasher.update(s.as_bytes());
|
||||
};
|
||||
connector.as_deref().map(&mut hash);
|
||||
hash(&manufacturer);
|
||||
hash(&model);
|
||||
hash(&serial_number);
|
||||
Rc::new(Self {
|
||||
_connector: connector,
|
||||
manufacturer,
|
||||
model,
|
||||
serial_number,
|
||||
hash: OutputIdHash(*hasher.finalize().as_bytes()),
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue