gstreamer_gl/
gl_display.rs

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