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