1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use std::ptr;

use glib::{object::IsA, translate::*};

pub use crate::auto::functions::*;
use crate::{GLContext, GLDisplay};

/// ## `element`
/// a [`gst::Element`][crate::gst::Element]
/// ## `query`
/// a [`gst::Query`][crate::gst::Query] of type `GST_QUERY_CONTEXT`
/// ## `display`
/// a [`GLDisplay`][crate::GLDisplay]
/// ## `context`
/// a [`GLContext`][crate::GLContext]
/// ## `other_context`
/// application provided [`GLContext`][crate::GLContext]
///
/// # Returns
///
/// Whether the `query` was successfully responded to from the passed
///  `display`, `context`, and `other_context`.
#[doc(alias = "gst_gl_handle_context_query")]
pub fn gl_handle_context_query(
    element: &impl IsA<gst::Element>,
    query: &mut gst::query::Context,
    display: Option<&impl IsA<GLDisplay>>,
    context: Option<&impl IsA<GLContext>>,
    other_context: Option<&impl IsA<GLContext>>,
) -> bool {
    skip_assert_initialized!();
    unsafe {
        from_glib(ffi::gst_gl_handle_context_query(
            element.as_ref().to_glib_none().0,
            query.as_mut_ptr(),
            display.map(|p| p.as_ref()).to_glib_none().0,
            context.map(|p| p.as_ref()).to_glib_none().0,
            other_context.map(|p| p.as_ref()).to_glib_none().0,
        ))
    }
}

/// Helper function for implementing `GstElementClass.set_context()` in
/// OpenGL capable elements.
///
/// Retrieve's the [`GLDisplay`][crate::GLDisplay] or [`GLContext`][crate::GLContext] in `context` and places the
/// result in `display` or `other_context` respectively.
/// ## `element`
/// a [`gst::Element`][crate::gst::Element]
/// ## `context`
/// a [`gst::Context`][crate::gst::Context]
///
/// # Returns
///
/// whether the `display` or `other_context` could be set successfully
///
/// ## `display`
/// location of a [`GLDisplay`][crate::GLDisplay]
///
/// ## `other_context`
/// location of a [`GLContext`][crate::GLContext]
#[doc(alias = "gst_gl_handle_set_context")]
pub fn gl_handle_set_context(
    element: &impl IsA<gst::Element>,
    context: &gst::Context,
) -> (Option<GLDisplay>, Option<GLContext>) {
    skip_assert_initialized!();
    unsafe {
        let mut display = ptr::null_mut();
        let mut other_context = ptr::null_mut();
        let ret = from_glib(ffi::gst_gl_handle_set_context(
            element.as_ref().to_glib_none().0,
            context.to_glib_none().0,
            &mut display,
            &mut other_context,
        ));
        if ret {
            (from_glib_full(display), from_glib_full(other_context))
        } else {
            (None, None)
        }
    }
}

/// Given `swizzle`, produce `inversion` such that:
///
/// `swizzle`[`inversion`[i]] == identity[i] where:
/// - identity = {0, 1, 2,...}
/// - unset fields are marked by -1
/// ## `swizzle`
/// input swizzle
///
/// # Returns
///
///
/// ## `inversion`
/// resulting inversion
#[cfg(feature = "v1_24")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
#[doc(alias = "gst_gl_swizzle_invert")]
pub fn gl_swizzle_invert(swizzle: [i32; 4]) -> [i32; 4] {
    unsafe {
        use std::mem;

        let mut inversion = mem::MaybeUninit::uninit();
        ffi::gst_gl_swizzle_invert(
            mut_override(swizzle.as_ptr() as *const _),
            inversion.as_mut_ptr(),
        );
        inversion.assume_init()
    }
}

/// Calculates the swizzle indices for `video_format` and `gl_format` in order to
/// access a texture such that accessing a texel from a texture through the swizzle
/// index produces values in the order (R, G, B, A) or (Y, U, V, A).
///
/// For multi-planer formats, the swizzle index uses the same component order (RGBA/YUVA)
/// and should be applied after combining multiple planes into a single rgba/yuva value.
/// e.g. sampling from a NV12 format would have Y from one texture and UV from
/// another texture into a (Y, U, V) value. Add an Aplha component and then
/// perform swizzling. Sampling from NV21 would produce (Y, V, U) which is then
/// swizzled to (Y, U, V).
/// ## `video_format`
/// the `GstVideoFormat` in use
///
/// # Returns
///
/// whether valid swizzle indices could be found
///
/// ## `swizzle`
/// the returned swizzle indices
#[cfg(feature = "v1_24")]
#[cfg_attr(docsrs, doc(cfg(feature = "v1_24")))]
#[doc(alias = "gst_gl_video_format_swizzle")]
pub fn gl_video_format_swizzle(video_format: gst_video::VideoFormat) -> Option<[i32; 4]> {
    unsafe {
        use std::mem;

        let mut swizzle = mem::MaybeUninit::uninit();
        let res = from_glib(ffi::gst_gl_video_format_swizzle(
            video_format.into_glib(),
            swizzle.as_mut_ptr(),
        ));
        if res {
            Some(swizzle.assume_init())
        } else {
            None
        }
    }
}