gstreamer_gl/
gl_display.rs

1use crate::{ffi, GLContext, GLDisplay};
2
3use glib::prelude::*;
4use glib::translate::*;
5
6mod sealed {
7    pub trait Sealed {}
8    impl<T: super::IsA<super::GLDisplay>> Sealed for T {}
9}
10
11pub trait GLDisplayExtManual: sealed::Sealed + IsA<GLDisplay> + 'static {
12    ///
13    /// # Returns
14    ///
15    /// the native handle for the display
16    #[doc(alias = "gst_gl_display_get_handle")]
17    #[doc(alias = "get_handle")]
18    fn handle(&self) -> usize {
19        unsafe { ffi::gst_gl_display_get_handle(self.as_ref().to_glib_none().0) }
20    }
21
22    /// Ensures that the display has a valid GL context for the current thread. If
23    /// `context` already contains a valid context, this does nothing.
24    /// ## `other_context`
25    /// other [`GLContext`][crate::GLContext] to share resources with.
26    /// ## `context`
27    /// the resulting [`GLContext`][crate::GLContext]
28    ///
29    /// # Returns
30    ///
31    /// wether `context` contains a valid context.
32    #[cfg(feature = "v1_24")]
33    #[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
34    #[doc(alias = "gst_gl_display_ensure_context")]
35    fn ensure_context(
36        &self,
37        other_context: Option<&impl IsA<GLContext>>,
38        context: &mut Option<GLContext>,
39    ) -> Result<(), glib::Error> {
40        unsafe {
41            let mut err = std::ptr::null_mut();
42            let res = ffi::gst_gl_display_ensure_context(
43                self.as_ref().to_glib_none().0,
44                other_context.map(AsRef::as_ref).to_glib_none().0,
45                context as *mut Option<GLContext> as *mut Option<*mut ffi::GstGLContext>
46                    as *mut *mut ffi::GstGLContext,
47                &mut err,
48            );
49
50            if res == glib::ffi::GFALSE {
51                *context = None;
52                Err(from_glib_full(err))
53            } else {
54                debug_assert!(err.is_null());
55                Ok(())
56            }
57        }
58    }
59}
60
61impl<O: IsA<GLDisplay>> GLDisplayExtManual for O {}
62
63impl GLDisplay {
64    #[doc(alias = "gst_gl_display_get_gl_context_for_thread")]
65    pub fn get_gl_context_for_current_thread(
66        display: &gst::ObjectLockGuard<GLDisplay>,
67    ) -> Option<GLContext> {
68        skip_assert_initialized!();
69        unsafe {
70            let ctx = ffi::gst_gl_display_get_gl_context_for_thread(
71                display.as_ref().to_glib_none().0,
72                std::ptr::null_mut(),
73            );
74            from_glib_full(ctx)
75        }
76    }
77
78    #[doc(alias = "gst_gl_display_create_context")]
79    pub fn create_context(
80        display: &gst::ObjectLockGuard<GLDisplay>,
81        other_context: Option<&impl IsA<GLContext>>,
82    ) -> Result<GLContext, glib::Error> {
83        skip_assert_initialized!();
84        unsafe {
85            let mut p_context = std::ptr::null_mut();
86            let mut error = std::ptr::null_mut();
87            let is_ok = ffi::gst_gl_display_create_context(
88                display.as_ref().to_glib_none().0,
89                other_context.map(|p| p.as_ref()).to_glib_none().0,
90                &mut p_context,
91                &mut error,
92            );
93            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
94            if error.is_null() {
95                Ok(from_glib_full(p_context))
96            } else {
97                Err(from_glib_full(error))
98            }
99        }
100    }
101
102    #[doc(alias = "gst_gl_display_add_context")]
103    pub fn add_context(
104        display: &gst::ObjectLockGuard<GLDisplay>,
105        context: &impl IsA<GLContext>,
106    ) -> Result<(), glib::error::BoolError> {
107        skip_assert_initialized!();
108        unsafe {
109            glib::result_from_gboolean!(
110                ffi::gst_gl_display_add_context(
111                    display.as_ref().to_glib_none().0,
112                    context.as_ref().to_glib_none().0
113                ),
114                "Failed to add OpenGL context"
115            )
116        }
117    }
118
119    #[cfg(feature = "v1_18")]
120    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
121    #[doc(alias = "gst_gl_display_remove_context")]
122    pub fn remove_context(
123        display: &gst::ObjectLockGuard<GLDisplay>,
124        context: &impl IsA<GLContext>,
125    ) {
126        skip_assert_initialized!();
127        unsafe {
128            ffi::gst_gl_display_remove_context(
129                display.as_ref().to_glib_none().0,
130                context.as_ref().to_glib_none().0,
131            );
132        }
133    }
134}