gstreamer_allocators/
drm_dumb_allocator.rs1use std::{fmt, mem, os::unix::prelude::IntoRawFd};
2
3use glib::{prelude::*, translate::*};
4use gst::{Memory, MemoryRef};
5
6use crate::{ffi, DRMDumbAllocator, DmaBufMemory};
7
8gst::memory_object_wrapper!(
9 DRMDumbMemory,
10 DRMDumbMemoryRef,
11 gst::ffi::GstMemory,
12 |mem: &gst::MemoryRef| { unsafe { from_glib(ffi::gst_is_drm_dumb_memory(mem.as_mut_ptr())) } },
13 Memory,
14 MemoryRef
15);
16
17impl fmt::Debug for DRMDumbMemory {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 DRMDumbMemoryRef::fmt(self, f)
20 }
21}
22
23impl fmt::Debug for DRMDumbMemoryRef {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 MemoryRef::fmt(self, f)
26 }
27}
28
29impl DRMDumbMemoryRef {
30 #[doc(alias = "gst_drm_dumb_memory_get_handle")]
31 pub fn fd(&self) -> u32 {
32 skip_assert_initialized!();
33 unsafe { ffi::gst_drm_dumb_memory_get_handle(self.as_mut_ptr()) }
34 }
35
36 #[doc(alias = "gst_drm_dumb_memory_export_dmabuf")]
37 pub fn export_dmabuf(&self) -> Result<DmaBufMemory, glib::BoolError> {
38 skip_assert_initialized!();
39 unsafe {
40 Option::<DmaBufMemory>::from_glib_full(ffi::gst_drm_dumb_memory_export_dmabuf(
41 self.as_mut_ptr(),
42 ))
43 .ok_or_else(|| glib::bool_error!("Failed to export as dmabuf"))
44 }
45 }
46}
47
48impl DRMDumbAllocator {
49 #[doc(alias = "gst_drm_dumb_allocator_new_with_fd")]
50 #[doc(alias = "new_with_fd")]
51 pub fn with_fd<A: IntoRawFd>(drm_fd: A) -> Result<DRMDumbAllocator, glib::BoolError> {
52 assert_initialized_main_thread!();
53 unsafe {
54 Option::<gst::Allocator>::from_glib_full(ffi::gst_drm_dumb_allocator_new_with_fd(
55 drm_fd.into_raw_fd(),
56 ))
57 .map(|o| o.unsafe_cast())
58 .ok_or_else(|| glib::bool_error!("Failed to create allocator"))
59 }
60 }
61}
62
63pub trait DRMDumbAllocatorExtManual: IsA<DRMDumbAllocator> + 'static {
64 #[doc(alias = "gst_drm_dumb_allocator_alloc")]
65 unsafe fn alloc_drm_dumb(
66 &self,
67 drm_fourcc: u32,
68 width: u32,
69 height: u32,
70 ) -> Result<(gst::Memory, u32), glib::BoolError> {
71 skip_assert_initialized!();
72 let mut out_pitch = mem::MaybeUninit::uninit();
73 Option::<_>::from_glib_full(ffi::gst_drm_dumb_allocator_alloc(
74 self.as_ref().to_glib_none().0,
75 drm_fourcc,
76 width,
77 height,
78 out_pitch.as_mut_ptr(),
79 ))
80 .ok_or_else(|| glib::bool_error!("Failed to allocate memory"))
81 .map(|mem| (mem, unsafe { out_pitch.assume_init() }))
82 }
83}
84
85impl<O: IsA<DRMDumbAllocator>> DRMDumbAllocatorExtManual for O {}