@@ -18,6 +18,7 @@
#include <drm/drm_file.h>
#include <drm/drm_fbdev_dma.h>
#include <drm/drm_fbdev_shmem.h>
+#include <drm/drm_framebuffer.h>
#include <drm/drm_gem.h>
#include <drm/drm_gem_framebuffer_helper.h>
#include <drm/drm_gem_shmem_helper.h>
@@ -6,6 +6,7 @@
pub mod connector;
pub mod crtc;
pub mod encoder;
+pub mod framebuffer;
pub mod plane;
use crate::{
new file mode 100644
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0 OR MIT
+
+//! DRM framebuffers.
+//!
+//! C header: [`include/drm/drm_framebuffer.h`](srctree/include/drm/drm_framebuffer.h)
+
+use super::{KmsDriver, ModeObject, RcModeObject};
+use crate::{drm::device::Device, types::*};
+use bindings;
+use core::{marker::*, ptr};
+
+/// The main interface for [`struct drm_framebuffer`].
+///
+/// # Invariants
+///
+/// - `self.0` is initialized for as long as this object is exposed to users.
+/// - This type has an identical data layout to [`struct drm_framebuffer`]
+///
+/// [`struct drm_framebuffer`]: srctree/include/drm/drm_framebuffer.h
+#[repr(transparent)]
+pub struct Framebuffer<T: KmsDriver>(Opaque<bindings::drm_framebuffer>, PhantomData<T>);
+
+// SAFETY:
+// - `self.0` is initialized for as long as this object is exposed to users
+// - `base` is initialized by DRM when `self.0` is initialized, thus `raw_mode_obj()` always returns
+// a valid pointer.
+unsafe impl<T: KmsDriver> ModeObject for Framebuffer<T> {
+ type Driver = T;
+
+ fn drm_dev(&self) -> &Device<Self::Driver> {
+ // SAFETY: `dev` points to an initialized `struct drm_device` for as long as this type is
+ // initialized
+ unsafe { Device::borrow((*self.0.get()).dev) }
+ }
+
+ fn raw_mode_obj(&self) -> *mut bindings::drm_mode_object {
+ // SAFETY: We don't expose Framebuffer<T> to users before its initialized, so `base` is
+ // always initialized
+ unsafe { ptr::addr_of_mut!((*self.0.get()).base) }
+ }
+}
+
+// SAFETY: Framebuffers are refcounted mode objects.
+unsafe impl<T: KmsDriver> RcModeObject for Framebuffer<T> {}
+
+// SAFETY: References to framebuffers are safe to be accessed from any thread
+unsafe impl<T: KmsDriver> Send for Framebuffer<T> {}
+// SAFETY: References to framebuffers are safe to be accessed from any thread
+unsafe impl<T: KmsDriver> Sync for Framebuffer<T> {}
+
+// For implementing ModeObject
+impl<T: KmsDriver> crate::private::Sealed for Framebuffer<T> {}
+
+impl<T: KmsDriver> PartialEq for Framebuffer<T> {
+ fn eq(&self, other: &Self) -> bool {
+ ptr::eq(self.0.get(), other.0.get())
+ }
+}
+impl<T: KmsDriver> Eq for Framebuffer<T> {}
+
+impl<T: KmsDriver> Framebuffer<T> {
+ /// Convert a raw pointer to a `struct drm_framebuffer` into a [`Framebuffer`]
+ ///
+ /// # Safety
+ ///
+ /// The caller guarantews that `ptr` points to a initialized `struct drm_framebuffer` for at
+ /// least the entire lifetime of `'a`.
+ #[inline]
+ #[allow(dead_code)]
+ pub(super) unsafe fn from_raw<'a>(ptr: *const bindings::drm_framebuffer) -> &'a Self {
+ // SAFETY: Our data layout is identical to drm_framebuffer
+ unsafe { &*ptr.cast() }
+ }
+}