From 5a6dfdc9de9e1d93f44cd0b18093adc84c1dbee0 Mon Sep 17 00:00:00 2001 From: Lyude Paul <lyude@redhat.com> Date: Mon, 4 Mar 2024 19:01:10 -0500 Subject: [PATCH] WIP: rust/drm: Add fourcc bindings This adds some very basic rust bindings for fourcc. We only have a single format code added for the moment, but this is enough to get a driver registered. TODO: * Write up something to automatically generate constants from the fourcc headers Signed-off-by: Lyude Paul <lyude@redhat.com> --- V3: * Drop FormatList and ModifierList These aren't actually needed as pointed out by Louis Chauvet * Add a constant for FORMAT_MOD_INVALID I realized that we actually need this because the format list isn't terminated with a 0 like I thought, and we can't pick this up automatically through bindgen --- rust/kernel/drm/fourcc.rs | 62 +++++++++++++++++++++++++++++++++++++++ rust/kernel/drm/mod.rs | 1 + 2 files changed, 63 insertions(+) create mode 100644 rust/kernel/drm/fourcc.rs diff --git a/rust/kernel/drm/fourcc.rs b/rust/kernel/drm/fourcc.rs new file mode 100644 index 0000000000000..14111abbc236b --- /dev/null +++ b/rust/kernel/drm/fourcc.rs @@ -0,0 +1,62 @@ +use bindings; + +/// Return a fourcc format code +const fn fourcc_code(a: u8, b: u8, c: u8, d: u8) -> u32 { + (a as u32) | (b as u32) << 8 | (c as u32) << 16 | (d as u32) << 24 +} + +// TODO: We manually import this because we don't have a reasonable way of getting constants from +// function-like macros in bindgen yet. +pub(crate) const FORMAT_MOD_INVALID: u64 = 0xffffffffffffff; + +// TODO: Figure out a more automated way of importing this +pub const XRGB888: u32 = fourcc_code(b'X', b'R', b'2', b'4'); + +#[repr(transparent)] +#[derive(Copy, Clone)] +pub struct FormatInfo { + inner: bindings::drm_format_info, +} + +impl FormatInfo { + // SAFETY: `ptr` must point to a valid instance of a `bindings::drm_format_info` + pub(super) unsafe fn from_raw<'a>(ptr: *const bindings::drm_format_info) -> &'a Self { + // SAFETY: Our data layout is identical + unsafe { &*ptr.cast() } + } + + /// The number of color planes (1 to 3) + pub const fn num_planes(&self) -> u8 { + self.inner.num_planes + } + + /// Does the format embed an alpha component? + pub const fn has_alpha(&self) -> bool { + self.inner.has_alpha + } + + /// The total number of components (color planes + alpha channel, if there is one) + pub const fn num_components(&self) -> u8 { + self.num_planes() + self.has_alpha() as u8 + } + + /// Number of bytes per block (per plane), where blocks are defined as a rectangle of pixels + /// which are stored next to each other in a byte aligned memory region. + pub fn char_per_block(&self) -> &[u8] { + // SAFETY: The union we access here is just for descriptive purposes on the C side, both + // members are identical in data layout + unsafe { &self.inner.__bindgen_anon_1.char_per_block[..self.num_components() as _] } + } +} + +impl AsRef<bindings::drm_format_info> for FormatInfo { + fn as_ref(&self) -> &bindings::drm_format_info { + &self.inner + } +} + +impl From<bindings::drm_format_info> for FormatInfo { + fn from(value: bindings::drm_format_info) -> Self { + Self { inner: value } + } +} diff --git a/rust/kernel/drm/mod.rs b/rust/kernel/drm/mod.rs index c44760a1332fa..2c12dbd181997 100644 --- a/rust/kernel/drm/mod.rs +++ b/rust/kernel/drm/mod.rs @@ -5,5 +5,6 @@ pub mod device; pub mod drv; pub mod file; +pub mod fourcc; pub mod gem; pub mod ioctl; -- GitLab