gstreamer_gl/functions.rs
1use std::ptr;
2
3use glib::{object::IsA, translate::*};
4
5pub use crate::auto::functions::*;
6use crate::{ffi, GLContext, GLDisplay};
7
8/// ## `element`
9/// a [`gst::Element`][crate::gst::Element]
10/// ## `query`
11/// a [`gst::Query`][crate::gst::Query] of type `GST_QUERY_CONTEXT`
12/// ## `display`
13/// a [`GLDisplay`][crate::GLDisplay]
14/// ## `context`
15/// a [`GLContext`][crate::GLContext]
16/// ## `other_context`
17/// application provided [`GLContext`][crate::GLContext]
18///
19/// # Returns
20///
21/// Whether the `query` was successfully responded to from the passed
22/// `display`, `context`, and `other_context`.
23#[doc(alias = "gst_gl_handle_context_query")]
24pub fn gl_handle_context_query(
25 element: &impl IsA<gst::Element>,
26 query: &mut gst::query::Context,
27 display: Option<&impl IsA<GLDisplay>>,
28 context: Option<&impl IsA<GLContext>>,
29 other_context: Option<&impl IsA<GLContext>>,
30) -> bool {
31 skip_assert_initialized!();
32 unsafe {
33 from_glib(ffi::gst_gl_handle_context_query(
34 element.as_ref().to_glib_none().0,
35 query.as_mut_ptr(),
36 display.map(|p| p.as_ref()).to_glib_none().0,
37 context.map(|p| p.as_ref()).to_glib_none().0,
38 other_context.map(|p| p.as_ref()).to_glib_none().0,
39 ))
40 }
41}
42
43/// Helper function for implementing `GstElementClass.set_context()` in
44/// OpenGL capable elements.
45///
46/// Retrieve's the [`GLDisplay`][crate::GLDisplay] or [`GLContext`][crate::GLContext] in `context` and places the
47/// result in `display` or `other_context` respectively.
48/// ## `element`
49/// a [`gst::Element`][crate::gst::Element]
50/// ## `context`
51/// a [`gst::Context`][crate::gst::Context]
52///
53/// # Returns
54///
55/// whether the `display` or `other_context` could be set successfully
56///
57/// ## `display`
58/// location of a [`GLDisplay`][crate::GLDisplay]
59///
60/// ## `other_context`
61/// location of a [`GLContext`][crate::GLContext]
62#[doc(alias = "gst_gl_handle_set_context")]
63pub fn gl_handle_set_context(
64 element: &impl IsA<gst::Element>,
65 context: &gst::Context,
66) -> (Option<GLDisplay>, Option<GLContext>) {
67 skip_assert_initialized!();
68 unsafe {
69 let mut display = ptr::null_mut();
70 let mut other_context = ptr::null_mut();
71 let ret = from_glib(ffi::gst_gl_handle_set_context(
72 element.as_ref().to_glib_none().0,
73 context.to_glib_none().0,
74 &mut display,
75 &mut other_context,
76 ));
77 if ret {
78 (from_glib_full(display), from_glib_full(other_context))
79 } else {
80 (None, None)
81 }
82 }
83}
84
85/// Given `swizzle`, produce `inversion` such that:
86///
87/// `swizzle`[`inversion`[i]] == identity[i] where:
88/// - identity = {0, 1, 2,...}
89/// - unset fields are marked by -1
90/// ## `swizzle`
91/// input swizzle
92///
93/// # Returns
94///
95///
96/// ## `inversion`
97/// resulting inversion
98#[cfg(feature = "v1_24")]
99#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
100#[doc(alias = "gst_gl_swizzle_invert")]
101pub fn gl_swizzle_invert(swizzle: [i32; 4]) -> [i32; 4] {
102 unsafe {
103 use std::mem;
104
105 let mut inversion = mem::MaybeUninit::uninit();
106 ffi::gst_gl_swizzle_invert(
107 mut_override(swizzle.as_ptr() as *const _),
108 inversion.as_mut_ptr(),
109 );
110 inversion.assume_init()
111 }
112}
113
114/// Calculates the swizzle indices for `video_format` and `gl_format` in order to
115/// access a texture such that accessing a texel from a texture through the swizzle
116/// index produces values in the order (R, G, B, A) or (Y, U, V, A).
117///
118/// For multi-planer formats, the swizzle index uses the same component order (RGBA/YUVA)
119/// and should be applied after combining multiple planes into a single rgba/yuva value.
120/// e.g. sampling from a NV12 format would have Y from one texture and UV from
121/// another texture into a (Y, U, V) value. Add an Aplha component and then
122/// perform swizzling. Sampling from NV21 would produce (Y, V, U) which is then
123/// swizzled to (Y, U, V).
124/// ## `video_format`
125/// the `GstVideoFormat` in use
126///
127/// # Returns
128///
129/// whether valid swizzle indices could be found
130///
131/// ## `swizzle`
132/// the returned swizzle indices
133#[cfg(feature = "v1_24")]
134#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
135#[doc(alias = "gst_gl_video_format_swizzle")]
136pub fn gl_video_format_swizzle(video_format: gst_video::VideoFormat) -> Option<[i32; 4]> {
137 unsafe {
138 use std::mem;
139
140 let mut swizzle = mem::MaybeUninit::uninit();
141 let res = from_glib(ffi::gst_gl_video_format_swizzle(
142 video_format.into_glib(),
143 swizzle.as_mut_ptr(),
144 ));
145 if res {
146 Some(swizzle.assume_init())
147 } else {
148 None
149 }
150 }
151}