Skip to main content

gstreamer_gl/
gl_context.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use glib::{prelude::*, translate::*};
4use libc::uintptr_t;
5
6use crate::{GLAPI, GLContext, GLDisplay, GLPlatform, ffi};
7
8impl GLContext {
9    pub unsafe fn new_wrapped<T: IsA<GLDisplay>>(
10        display: &T,
11        handle: uintptr_t,
12        context_type: GLPlatform,
13        available_apis: GLAPI,
14    ) -> Option<Self> {
15        unsafe {
16            from_glib_full(ffi::gst_gl_context_new_wrapped(
17                display.as_ref().to_glib_none().0,
18                handle,
19                context_type.into_glib(),
20                available_apis.into_glib(),
21            ))
22        }
23    }
24
25    #[doc(alias = "get_current_gl_context")]
26    #[doc(alias = "gst_gl_context_get_current_gl_context")]
27    pub fn current_gl_context(context_type: GLPlatform) -> uintptr_t {
28        skip_assert_initialized!();
29        unsafe { ffi::gst_gl_context_get_current_gl_context(context_type.into_glib()) as uintptr_t }
30    }
31
32    #[doc(alias = "get_proc_address_with_platform")]
33    #[doc(alias = "gst_gl_context_get_proc_address_with_platform")]
34    pub fn proc_address_with_platform(
35        context_type: GLPlatform,
36        gl_api: GLAPI,
37        name: &str,
38    ) -> uintptr_t {
39        skip_assert_initialized!();
40        unsafe {
41            ffi::gst_gl_context_get_proc_address_with_platform(
42                context_type.into_glib(),
43                gl_api.into_glib(),
44                name.to_glib_none().0,
45            ) as uintptr_t
46        }
47    }
48}
49
50pub trait GLContextExtManual: IsA<GLContext> + 'static {
51    #[doc(alias = "get_gl_context")]
52    #[doc(alias = "gst_gl_context_get_gl_context")]
53    fn gl_context(&self) -> uintptr_t {
54        unsafe { ffi::gst_gl_context_get_gl_context(self.as_ref().to_glib_none().0) as uintptr_t }
55    }
56
57    #[doc(alias = "get_proc_address")]
58    #[doc(alias = "gst_gl_context_get_proc_address")]
59    fn proc_address(&self, name: &str) -> uintptr_t {
60        unsafe {
61            ffi::gst_gl_context_get_proc_address(
62                self.as_ref().to_glib_none().0,
63                name.to_glib_none().0,
64            ) as uintptr_t
65        }
66    }
67
68    #[doc(alias = "gst_gl_context_thread_add")]
69    fn thread_add<F: FnOnce(&Self) + Send>(&self, func: F) {
70        let mut func = std::mem::ManuallyDrop::new(func);
71        let user_data: *mut F = &mut *func;
72
73        unsafe extern "C" fn trampoline<O: IsA<GLContext>, F: FnOnce(&O) + Send>(
74            context: *mut ffi::GstGLContext,
75            data: glib::ffi::gpointer,
76        ) {
77            unsafe {
78                let func = std::ptr::read(data as *mut F);
79                let context = GLContext::from_glib_borrow(context);
80                func(context.unsafe_cast_ref())
81            }
82        }
83
84        unsafe {
85            ffi::gst_gl_context_thread_add(
86                self.as_ref().to_glib_none().0,
87                Some(trampoline::<Self, F>),
88                user_data as glib::ffi::gpointer,
89            );
90        }
91    }
92}
93
94impl<O: IsA<GLContext>> GLContextExtManual for O {}