allocators: Move some methods to extension traits

So they're callable on subclasses without casting first. Also
disambiguate the `alloc()` methods with the ones from the `Allocator`
base class.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1751>
This commit is contained in:
Sebastian Dröge 2025-07-11 15:54:30 +03:00
parent aacf979652
commit e54178dbd7
5 changed files with 23 additions and 9 deletions

View file

@ -22,6 +22,7 @@ use std::{
use anyhow::Error;
use futures::StreamExt;
use gst::{element_error, prelude::*};
use gst_allocators::prelude::*;
use memmap2::MmapMut;
use uds::UnixStreamExt;
@ -83,7 +84,7 @@ fn create_receiver_pipeline(
// will be closed when the memory is released.
let memory = unsafe {
fd_allocator
.alloc(*fd, video_info.size(), gst_allocators::FdMemoryFlags::NONE)
.alloc_fd(*fd, video_info.size(), gst_allocators::FdMemoryFlags::NONE)
.unwrap()
};
let mut buffer = gst::Buffer::new();

View file

@ -42,9 +42,9 @@ impl DmaBufMemoryRef {
}
}
impl DmaBufAllocator {
pub trait DmaBufAllocatorExtManual: IsA<DmaBufAllocator> + 'static {
#[doc(alias = "gst_dmabuf_allocator_alloc")]
pub unsafe fn alloc<A: IntoRawFd>(
unsafe fn alloc_dmabuf<A: IntoRawFd>(
&self,
fd: A,
size: usize,
@ -61,7 +61,7 @@ impl DmaBufAllocator {
#[cfg(feature = "v1_16")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
#[doc(alias = "gst_dmabuf_allocator_alloc_with_flags")]
pub unsafe fn alloc_with_flags(
unsafe fn alloc_dmabuf_with_flags(
&self,
fd: RawFd,
size: usize,
@ -77,3 +77,5 @@ impl DmaBufAllocator {
.ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
}
}
impl<O: IsA<DmaBufAllocator>> DmaBufAllocatorExtManual for O {}

View file

@ -58,9 +58,11 @@ impl DRMDumbAllocator {
.ok_or_else(|| glib::bool_error!("Failed to create allocator"))
}
}
}
pub trait DRMDumbAllocatorExtManual: IsA<DRMDumbAllocator> + 'static {
#[doc(alias = "gst_drm_dumb_allocator_alloc")]
pub unsafe fn alloc(
unsafe fn alloc_drm_dumb(
&self,
drm_fourcc: u32,
width: u32,
@ -69,7 +71,7 @@ impl DRMDumbAllocator {
skip_assert_initialized!();
let mut out_pitch = mem::MaybeUninit::uninit();
Option::<_>::from_glib_full(ffi::gst_drm_dumb_allocator_alloc(
self.to_glib_none().0,
self.as_ref().to_glib_none().0,
drm_fourcc,
width,
height,
@ -79,3 +81,5 @@ impl DRMDumbAllocator {
.map(|mem| (mem, unsafe { out_pitch.assume_init() }))
}
}
impl<O: IsA<DRMDumbAllocator>> DRMDumbAllocatorExtManual for O {}

View file

@ -44,9 +44,9 @@ impl FdMemoryRef {
}
}
impl FdAllocator {
pub trait FdAllocatorExtManual: IsA<FdAllocator> + 'static {
#[doc(alias = "gst_fd_allocator_alloc")]
pub unsafe fn alloc(
unsafe fn alloc_fd(
&self,
fd: RawFd,
size: usize,
@ -65,7 +65,7 @@ impl FdAllocator {
#[cfg(feature = "v1_28")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_28")))]
#[doc(alias = "gst_fd_allocator_alloc_full")]
pub unsafe fn alloc_full(
unsafe fn alloc_fd_full(
allocator: &impl IsA<gst::Allocator>,
fd: RawFd,
maxsize: usize,
@ -86,3 +86,5 @@ impl FdAllocator {
}
}
}
impl<O: IsA<FdAllocator>> FdAllocatorExtManual for O {}

View file

@ -59,6 +59,11 @@ pub mod prelude {
pub use gst::prelude::*;
pub use crate::auto::traits::*;
#[cfg(any(target_os = "linux", docsrs))]
pub use crate::dma_buf_allocator::DmaBufAllocatorExtManual;
#[cfg(any(all(feature = "v1_24", target_os = "linux"), docsrs))]
pub use crate::drm_dumb_allocator::DRMDumbAllocatorExtManual;
pub use crate::fd_allocator::FdAllocatorExtManual;
}
pub mod subclass;