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