gl/gl_base_memory: Provide manual GLBaseMemory miniobject implementation

Also enable all dependencies used by this type. Copying
AllocationParams is possible without borrowing the source mutable.

The offset and size parameters of `memcpy` are unchecked and may result
in out-of-bounds reads/writes, which is why this function is marked as
`unsafe`.
This commit is contained in:
Marijn Suijten 2020-08-28 14:32:53 +02:00 committed by Sebastian Dröge
parent d596ea45c5
commit 4eddd377e1
3 changed files with 127 additions and 4 deletions

View file

@ -30,14 +30,12 @@ generate = [
"GstGL.GLSLError",
"GstGL.GLWindowError",
# Records
#"GstGL.GLAllocationParams",
#"GstGL.GLRenderbufferAllocationParams",
#"GstGL.GLVideoAllocationParams",
# Objects
#"GstGL.GLBufferPool",
"GstGL.GLFramebuffer",
#"GstGL.GLBaseMemory",
#"GstGL.GLBaseMemoryAllocator",
"GstGL.GLBaseMemoryAllocator",
"GstGL.GLMemoryAllocator",
#"GstGL.GLMemoryPBOAllocator",
#"GstGL.GLRenderbufferAllocator",
]
@ -56,6 +54,7 @@ manual = [
"Gst.PadDirection",
"GstBase.BaseTransform",
"GstBase.PushSrc",
"GstGL.GLBaseMemory",
"GstVideo.VideoAlignment",
"GstVideo.VideoInfo",
"GstVideo.VideoMultiviewFlags",
@ -467,3 +466,35 @@ status = "generate"
[[object.member]]
name = "any"
ignore = true
[[object]]
name = "GstGL.GLAllocationParams"
status = "generate"
[[object.function]]
name = "free_data"
# Function should only be called by subclasses from
# an overridden `free` vfunc.
ignore = true
[[object.function]]
name = "copy_data"
[[object.function.parameter]]
name = "src"
const = true
[[object]]
name = "GstGL.GLVideoAllocationParams"
status = "generate"
[[object.function]]
name = "free_data"
# Function should only be called by subclasses from
# an overridden `free` vfunc.
ignore = true
[[object.function]]
name = "copy_data"
[[object.function.parameter]]
name = "src_vid"
const = true

View file

@ -0,0 +1,90 @@
use glib::object::IsA;
use glib::translate::*;
use crate::GLAllocationParams;
use crate::GLBaseMemoryAllocator;
use ffi::GstGLBaseMemory;
use gst::ffi::GstMemory;
use gst::MemoryRef;
use gst::{result_from_gboolean, LoggableError, CAT_RUST};
gst::mini_object_wrapper!(
GLBaseMemory,
GLBaseMemoryRef,
GstGLBaseMemory,
ffi::gst_gl_base_memory_get_type
);
impl std::ops::Deref for GLBaseMemoryRef {
type Target = MemoryRef;
fn deref(&self) -> &Self::Target {
unsafe { &*(&self.0.mem as *const GstMemory).cast::<Self::Target>() }
}
}
impl std::ops::DerefMut for GLBaseMemoryRef {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(&mut self.0.mem as *mut GstMemory).cast::<Self::Target>() }
}
}
impl GLBaseMemoryRef {
// Note: only intended for subclass usage to allocate the system memory buffer
// on demand. If there is already a non-NULL data pointer in @gl_mem->data,
// then this function imply returns TRUE.
// #[doc(alias = "gst_gl_base_memory_alloc_data")]
// pub fn alloc_data(&mut self) -> bool {
// Self::init_once();
// unsafe { from_glib(ffi::gst_gl_base_memory_alloc_data(&mut self.0)) }
// }
//#[doc(alias = "gst_gl_base_memory_init")]
//pub fn init<P: IsA<gst::Allocator>, Q: IsA<GLContext>>(&mut self, allocator: &P, parent: Option<&mut gst::Memory>, context: &Q, params: Option<&mut gst::AllocationParams>, size: usize, user_data: /*Unimplemented*/Option<Fundamental: Pointer>) {
// unsafe { TODO: call ffi:gst_gl_base_memory_init() }
//}
#[doc(alias = "gst_gl_base_memory_memcpy")]
pub unsafe fn memcpy(
&self,
dest: &mut GLBaseMemory,
offset: isize,
size: isize,
) -> Result<(), LoggableError> {
Self::init_once();
result_from_gboolean!(
ffi::gst_gl_base_memory_memcpy(
mut_override(&self.0),
dest.to_glib_none_mut().0,
offset,
size,
),
CAT_RUST,
"Failed to copy memory"
)
}
#[doc(alias = "gst_gl_base_memory_alloc")]
pub fn alloc<P: IsA<GLBaseMemoryAllocator>>(
allocator: &P,
params: &GLAllocationParams,
) -> Option<GLBaseMemory> {
skip_assert_initialized!();
Self::init_once();
unsafe {
from_glib_full(ffi::gst_gl_base_memory_alloc(
allocator.as_ref().to_glib_none().0,
mut_override(params.to_glib_none().0),
))
}
}
#[doc(alias = "gst_gl_base_memory_init_once")]
fn init_once() {
assert_initialized_main_thread!();
unsafe {
ffi::gst_gl_base_memory_init_once();
}
}
}

View file

@ -37,6 +37,8 @@ mod gl_video_frame;
pub use crate::gl_video_frame::VideoFrameGLExt;
mod gl_sync_meta;
pub use crate::gl_sync_meta::*;
mod gl_base_memory;
pub use self::gl_base_memory::*;
// Re-export all the traits in a prelude module, so that applications
// can always "use gst::prelude::*" without getting conflicts