Skip to main content

gstreamer_gl/
gl_display.rs

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