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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use ffi::{GstGLFilter, GstGLFilterClass, GstGLMemory};
use glib::translate::*;
use gst::{
    ffi::GstBuffer, result_from_gboolean, Buffer, Caps, LoggableError, PadDirection, CAT_RUST,
};

use super::prelude::*;
use crate::{prelude::*, GLFilter, GLMemory};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum GLFilterMode {
    Buffer,
    Texture,
}

pub trait GLFilterImpl: GLFilterImplExt + GLBaseFilterImpl {
    const MODE: GLFilterMode;
    // rustdoc-stripper-ignore-next
    /// Calls [`add_rgba_pad_templates`](ffi::gst_gl_filter_add_rgba_pad_templates)
    /// in [`GLFilter::class_init`] if [`true`].
    const ADD_RGBA_PAD_TEMPLATES: bool = true;

    fn set_caps(&self, incaps: &Caps, outcaps: &Caps) -> Result<(), LoggableError> {
        GLFilterImplExt::parent_set_caps(self, incaps, outcaps)
    }

    fn filter(&self, input: &Buffer, output: &Buffer) -> Result<(), LoggableError> {
        self.parent_filter(input, output)
    }

    fn filter_texture(&self, input: &GLMemory, output: &GLMemory) -> Result<(), LoggableError> {
        self.parent_filter_texture(input, output)
    }

    fn init_fbo(&self) -> Result<(), LoggableError> {
        self.parent_init_fbo()
    }

    fn transform_internal_caps(
        &self,
        direction: PadDirection,
        caps: &Caps,
        filter_caps: Option<&Caps>,
    ) -> Option<Caps> {
        self.parent_transform_internal_caps(direction, caps, filter_caps)
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::GLFilterImplExt> Sealed for T {}
}

pub trait GLFilterImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_set_caps(&self, incaps: &Caps, outcaps: &Caps) -> Result<(), LoggableError> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut GstGLFilterClass;

            (*parent_class)
                .set_caps
                .map(|f| {
                    result_from_gboolean!(
                        f(
                            self.obj().unsafe_cast_ref::<GLFilter>().to_glib_none().0,
                            incaps.to_glib_none().0,
                            outcaps.to_glib_none().0,
                        ),
                        CAT_RUST,
                        "Parent function `set_caps` failed"
                    )
                })
                .unwrap_or(Ok(()))
        }
    }

    fn parent_filter(&self, input: &Buffer, output: &Buffer) -> Result<(), LoggableError> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut GstGLFilterClass;

            (*parent_class)
                .filter
                .map(|f| {
                    result_from_gboolean!(
                        f(
                            self.obj().unsafe_cast_ref::<GLFilter>().to_glib_none().0,
                            input.to_glib_none().0,
                            output.to_glib_none().0,
                        ),
                        CAT_RUST,
                        "Parent function `filter` failed"
                    )
                })
                .unwrap_or(Ok(()))
        }
    }

    fn parent_filter_texture(
        &self,
        input: &GLMemory,
        output: &GLMemory,
    ) -> Result<(), LoggableError> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut GstGLFilterClass;

            (*parent_class)
                .filter_texture
                .map(|f| {
                    result_from_gboolean!(
                        f(
                            self.obj().unsafe_cast_ref::<GLFilter>().to_glib_none().0,
                            input.to_glib_none().0,
                            output.to_glib_none().0,
                        ),
                        CAT_RUST,
                        "Parent function `filter_texture` failed"
                    )
                })
                .unwrap_or(Ok(()))
        }
    }

    fn parent_init_fbo(&self) -> Result<(), LoggableError> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut GstGLFilterClass;

            (*parent_class)
                .init_fbo
                .map(|f| {
                    result_from_gboolean!(
                        f(self.obj().unsafe_cast_ref::<GLFilter>().to_glib_none().0),
                        CAT_RUST,
                        "Parent function `init_fbo` failed"
                    )
                })
                .unwrap_or(Ok(()))
        }
    }
    fn parent_transform_internal_caps(
        &self,
        direction: PadDirection,
        caps: &Caps,
        filter_caps: Option<&Caps>,
    ) -> Option<Caps> {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut GstGLFilterClass;

            let f = (*parent_class)
                .transform_internal_caps
                .expect("Missing parent function `transform_internal_caps`");

            from_glib_full(f(
                self.obj().unsafe_cast_ref::<GLFilter>().to_glib_none().0,
                direction.into_glib(),
                caps.to_glib_none().0,
                filter_caps.to_glib_none().0,
            ))
        }
    }
}

impl<T: GLFilterImpl> GLFilterImplExt for T {}

unsafe impl<T: GLFilterImpl> IsSubclassable<T> for GLFilter {
    fn class_init(klass: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(klass);
        let klass = klass.as_mut();
        klass.set_caps = Some(set_caps::<T>);
        klass.init_fbo = Some(init_fbo::<T>);
        klass.transform_internal_caps = Some(transform_internal_caps::<T>);

        match <T as GLFilterImpl>::MODE {
            GLFilterMode::Buffer => {
                klass.filter = Some(filter::<T>);
                klass.filter_texture = None;
            }
            GLFilterMode::Texture => {
                klass.filter = None;
                klass.filter_texture = Some(filter_texture::<T>);
            }
        }

        if <T as GLFilterImpl>::ADD_RGBA_PAD_TEMPLATES {
            unsafe { ffi::gst_gl_filter_add_rgba_pad_templates(klass) }
        }
    }
}

unsafe extern "C" fn filter<T: GLFilterImpl>(
    ptr: *mut GstGLFilter,
    input: *mut GstBuffer,
    output: *mut GstBuffer,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    gst::panic_to_error!(imp, false, {
        match imp.filter(&from_glib_borrow(input), &from_glib_borrow(output)) {
            Ok(()) => true,
            Err(err) => {
                err.log_with_imp(imp);
                false
            }
        }
    })
    .into_glib()
}

unsafe extern "C" fn filter_texture<T: GLFilterImpl>(
    ptr: *mut GstGLFilter,
    input: *mut GstGLMemory,
    output: *mut GstGLMemory,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    gst::panic_to_error!(imp, false, {
        match imp.filter_texture(&from_glib_borrow(input), &from_glib_borrow(output)) {
            Ok(()) => true,
            Err(err) => {
                err.log_with_imp(imp);
                false
            }
        }
    })
    .into_glib()
}

unsafe extern "C" fn init_fbo<T: GLFilterImpl>(ptr: *mut GstGLFilter) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    gst::panic_to_error!(imp, false, {
        match imp.init_fbo() {
            Ok(()) => true,
            Err(err) => {
                err.log_with_imp(imp);
                false
            }
        }
    })
    .into_glib()
}

unsafe extern "C" fn set_caps<T: GLFilterImpl>(
    ptr: *mut GstGLFilter,
    incaps: *mut gst::ffi::GstCaps,
    outcaps: *mut gst::ffi::GstCaps,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    gst::panic_to_error!(imp, false, {
        match GLFilterImpl::set_caps(imp, &from_glib_borrow(incaps), &from_glib_borrow(outcaps)) {
            Ok(()) => true,
            Err(err) => {
                err.log_with_imp(imp);
                false
            }
        }
    })
    .into_glib()
}

unsafe extern "C" fn transform_internal_caps<T: GLFilterImpl>(
    ptr: *mut GstGLFilter,
    direction: gst::ffi::GstPadDirection,
    caps: *mut gst::ffi::GstCaps,
    filter_caps: *mut gst::ffi::GstCaps,
) -> *mut gst::ffi::GstCaps {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    gst::panic_to_error!(imp, None, {
        let filter_caps: Borrowed<Option<Caps>> = from_glib_borrow(filter_caps);

        imp.transform_internal_caps(
            from_glib(direction),
            &from_glib_borrow(caps),
            filter_caps.as_ref().as_ref(),
        )
    })
    .map(|caps| caps.into_glib_ptr())
    .unwrap_or(std::ptr::null_mut())
}