Skip to content
Snippets Groups Projects
Commit 5a6dfdc9 authored by Lyude Paul's avatar Lyude Paul
Browse files

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's avatarLyude 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
parent 18c18a6b
No related merge requests found
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 }
}
}
......@@ -5,5 +5,6 @@
pub mod device;
pub mod drv;
pub mod file;
pub mod fourcc;
pub mod gem;
pub mod ioctl;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment