[WIP RFC v2 01/35] WIP: rust/drm: Add fourcc bindings

Lyude Paul lyude at redhat.com
Mon Sep 30 23:09:44 UTC 2024


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 at redhat.com>
---
 rust/bindings/bindings_helper.h |   1 +
 rust/kernel/drm/fourcc.rs       | 127 ++++++++++++++++++++++++++++++++
 rust/kernel/drm/mod.rs          |   1 +
 3 files changed, 129 insertions(+)
 create mode 100644 rust/kernel/drm/fourcc.rs

diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index b2e05f8c2ee7d..04898f70ef1b8 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -9,6 +9,7 @@
 #include <drm/drm_device.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_file.h>
+#include <drm/drm_fourcc.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_gem_shmem_helper.h>
 #include <drm/drm_ioctl.h>
diff --git a/rust/kernel/drm/fourcc.rs b/rust/kernel/drm/fourcc.rs
new file mode 100644
index 0000000000000..b80eba99aa7e4
--- /dev/null
+++ b/rust/kernel/drm/fourcc.rs
@@ -0,0 +1,127 @@
+use bindings;
+use core::{ops::*, slice, ptr};
+
+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: Figure out a more automated way of importing this
+pub const XRGB888: u32 = fourcc_code(b'X', b'R', b'2', b'4');
+
+#[derive(Copy, Clone)]
+#[repr(C)]
+pub struct FormatList<const COUNT: usize> {
+    list: [u32; COUNT],
+    _sentinel: u32,
+}
+
+impl<const COUNT: usize> FormatList<COUNT> {
+    /// Create a new [`FormatList`]
+    pub const fn new(list: [u32; COUNT]) -> Self {
+        Self {
+            list,
+            _sentinel: 0
+        }
+    }
+
+    /// Returns the number of entries in the list, including the sentinel.
+    ///
+    /// This is generally only useful for passing [`FormatList`] to C bindings.
+    pub const fn raw_len(&self) -> usize {
+        COUNT + 1
+    }
+}
+
+impl<const COUNT: usize> Deref for FormatList<COUNT> {
+    type Target = [u32; COUNT];
+
+    fn deref(&self) -> &Self::Target {
+        &self.list
+    }
+}
+
+impl<const COUNT: usize> DerefMut for FormatList<COUNT> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.list
+    }
+}
+
+#[derive(Copy, Clone)]
+#[repr(C)]
+pub struct ModifierList<const COUNT: usize> {
+    list: [u64; COUNT],
+    _sentinel: u64
+}
+
+impl<const COUNT: usize> ModifierList<COUNT> {
+    /// Create a new [`ModifierList`]
+    pub const fn new(list: [u64; COUNT]) -> Self {
+        Self {
+            list,
+            _sentinel: 0
+        }
+    }
+}
+
+impl<const COUNT: usize> Deref for ModifierList<COUNT> {
+    type Target = [u64; COUNT];
+
+    fn deref(&self) -> &Self::Target {
+        &self.list
+    }
+}
+
+impl<const COUNT: usize> DerefMut for ModifierList<COUNT> {
+    fn deref_mut(&mut self) -> &mut Self::Target {
+        &mut self.list
+    }
+}
+
+#[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;
-- 
2.46.1



More information about the dri-devel mailing list