Commit 1ed10db6 authored by Lyude Paul's avatar Lyude Paul Committed by Alice Ryhl
Browse files

rust: drm: gem: Add DriverFile type alias



Just to reduce the clutter with the File<…> types in gem.rs.

Signed-off-by: default avatarLyude Paul <lyude@redhat.com>
Reviewed-by: default avatarDaniel Almeida <daniel.almeida@collabora.com>
Acked-by: default avatarDanilo Krummrich <dakr@kernel.org>
Reviewed-by: default avatarAlice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/r/20250908185239.135849-3-lyude@redhat.com


Signed-off-by: default avatarAlice Ryhl <aliceryhl@google.com>
parent 6ea42e91
Loading
Loading
Loading
Loading
+12 −11
Original line number Diff line number Diff line
@@ -14,6 +14,13 @@
};
use core::{mem, ops::Deref, ptr::NonNull};

/// A type alias for retrieving a [`Driver`]s [`DriverFile`] implementation from its
/// [`DriverObject`] implementation.
///
/// [`Driver`]: drm::Driver
/// [`DriverFile`]: drm::file::DriverFile
pub type DriverFile<T> = drm::File<<<T as DriverObject>::Driver as drm::Driver>::File>;

/// GEM object functions, which must be implemented by drivers.
pub trait DriverObject: Sync + Send + Sized {
    /// Parent `Driver` for this object.
@@ -23,19 +30,12 @@ pub trait DriverObject: Sync + Send + Sized {
    fn new(dev: &drm::Device<Self::Driver>, size: usize) -> impl PinInit<Self, Error>;

    /// Open a new handle to an existing object, associated with a File.
    fn open(
        _obj: &<Self::Driver as drm::Driver>::Object,
        _file: &drm::File<<Self::Driver as drm::Driver>::File>,
    ) -> Result {
    fn open(_obj: &<Self::Driver as drm::Driver>::Object, _file: &DriverFile<Self>) -> Result {
        Ok(())
    }

    /// Close a handle to an existing object, associated with a File.
    fn close(
        _obj: &<Self::Driver as drm::Driver>::Object,
        _file: &drm::File<<Self::Driver as drm::Driver>::File>,
    ) {
    }
    fn close(_obj: &<Self::Driver as drm::Driver>::Object, _file: &DriverFile<Self>) {}
}

/// Trait that represents a GEM object subtype
@@ -79,7 +79,8 @@ extern "C" fn open_callback<T: DriverObject>(
    raw_file: *mut bindings::drm_file,
) -> core::ffi::c_int {
    // SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`.
    let file = unsafe { drm::File::<<T::Driver as drm::Driver>::File>::from_raw(raw_file) };
    let file = unsafe { DriverFile::<T>::from_raw(raw_file) };

    // SAFETY: `open_callback` is specified in the AllocOps structure for `DriverObject<T>`,
    // ensuring that `raw_obj` is contained within a `DriverObject<T>`
    let obj = unsafe { <<T::Driver as drm::Driver>::Object as IntoGEMObject>::from_raw(raw_obj) };
@@ -95,7 +96,7 @@ extern "C" fn close_callback<T: DriverObject>(
    raw_file: *mut bindings::drm_file,
) {
    // SAFETY: `open_callback` is only ever called with a valid pointer to a `struct drm_file`.
    let file = unsafe { drm::File::<<T::Driver as drm::Driver>::File>::from_raw(raw_file) };
    let file = unsafe { DriverFile::<T>::from_raw(raw_file) };

    // SAFETY: `close_callback` is specified in the AllocOps structure for `Object<T>`, ensuring
    // that `raw_obj` is indeed contained within a `Object<T>`.