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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
// DO NOT EDIT

use crate::{GLDisplay, GLPlatform, GLSLProfile, GLSLVersion, GLWindow, GLAPI};
use glib::{prelude::*, translate::*};

glib::wrapper! {
    /// [`GLContext`][crate::GLContext] wraps an OpenGL context object in a uniform API. As a result
    /// of the limitation on OpenGL context, this object is not thread safe unless
    /// specified and must only be activated in a single thread.
    ///
    /// Environment variables:
    /// - `GST_GL_API`: select which OpenGL API to create and OpenGL context for.
    ///  Depending on the platform, the available values are
    ///  'opengl', 'opengl3' (core profile), and 'gles2'. See the
    ///  the [`GLAPI`][crate::GLAPI] enumeration for more details.
    /// - `GST_GL_PLATFORM`: select which OpenGL platform to create an OpenGL
    ///  context with. Depending on the platform and the
    ///  dependencies available build-time, the available values
    ///  are, 'glx', 'egl', 'cgl', 'wgl', and 'eagl'
    /// - `GST_GL_CONFIG`: select the configuration used for creating the OpenGL
    ///  context and OpenGL surface. Written out as a GstStructure
    ///  that has been serialized to string. e.g.
    ///  `GST_GL_CONFIG="gst-gl-context-config,red-size=8,green-size=8,blue-size=8,alpha-size=8,depth-size=16"`.
    ///  Not all platforms will support the same level of
    ///  functionality.
    ///
    /// This is an Abstract Base Class, you cannot instantiate it.
    ///
    /// # Implements
    ///
    /// [`GLContextExt`][trait@crate::prelude::GLContextExt], [`trait@gst::prelude::ObjectExt`], [`trait@glib::ObjectExt`], [`GLContextExtManual`][trait@crate::prelude::GLContextExtManual]
    #[doc(alias = "GstGLContext")]
    pub struct GLContext(Object<ffi::GstGLContext, ffi::GstGLContextClass>) @extends gst::Object;

    match fn {
        type_ => || ffi::gst_gl_context_get_type(),
    }
}

impl GLContext {
    pub const NONE: Option<&'static GLContext> = None;

    /// Create a new [`GLContext`][crate::GLContext] with the specified `display`
    /// ## `display`
    /// a [`GLDisplay`][crate::GLDisplay]
    ///
    /// # Returns
    ///
    /// a new [`GLContext`][crate::GLContext]
    #[doc(alias = "gst_gl_context_new")]
    pub fn new(display: &impl IsA<GLDisplay>) -> GLContext {
        skip_assert_initialized!();
        unsafe { from_glib_none(ffi::gst_gl_context_new(display.as_ref().to_glib_none().0)) }
    }

    /// See also [`GLContextExt::activate()`][crate::prelude::GLContextExt::activate()].
    ///
    /// # Returns
    ///
    /// the [`GLContext`][crate::GLContext] active in the current thread or [`None`]
    #[doc(alias = "gst_gl_context_get_current")]
    #[doc(alias = "get_current")]
    pub fn current() -> Option<GLContext> {
        assert_initialized_main_thread!();
        unsafe { from_glib_none(ffi::gst_gl_context_get_current()) }
    }

    /// If an error occurs, `major` and `minor` are not modified and `GST_GL_API_NONE` is
    /// returned.
    /// ## `platform`
    /// the [`GLPlatform`][crate::GLPlatform] to retrieve the API for
    ///
    /// # Returns
    ///
    /// The version supported by the OpenGL context current in the calling
    ///  thread or `GST_GL_API_NONE`
    ///
    /// ## `major`
    /// the major version
    ///
    /// ## `minor`
    /// the minor version
    #[doc(alias = "gst_gl_context_get_current_gl_api")]
    #[doc(alias = "get_current_gl_api")]
    pub fn current_gl_api(platform: GLPlatform) -> (GLAPI, u32, u32) {
        assert_initialized_main_thread!();
        unsafe {
            let mut major = std::mem::MaybeUninit::uninit();
            let mut minor = std::mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::gst_gl_context_get_current_gl_api(
                platform.into_glib(),
                major.as_mut_ptr(),
                minor.as_mut_ptr(),
            ));
            (ret, major.assume_init(), minor.assume_init())
        }
    }
}

unsafe impl Send for GLContext {}
unsafe impl Sync for GLContext {}

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

/// Trait containing all [`struct@GLContext`] methods.
///
/// # Implementors
///
/// [`GLContext`][struct@crate::GLContext]
pub trait GLContextExt: IsA<GLContext> + sealed::Sealed + 'static {
    /// (De)activate the OpenGL context represented by this `self`.
    ///
    /// In OpenGL terms, calls eglMakeCurrent or similar with this context and the
    /// currently set window. See [`set_window()`][Self::set_window()] for details.
    /// ## `activate`
    /// [`true`] to activate, [`false`] to deactivate
    ///
    /// # Returns
    ///
    /// Whether the activation succeeded
    #[doc(alias = "gst_gl_context_activate")]
    fn activate(&self, activate: bool) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_gl_context_activate(self.as_ref().to_glib_none().0, activate.into_glib()),
                "Failed to activate OpenGL context"
            )
        }
    }

    /// Note: This will always fail for two wrapped [`GLContext`][crate::GLContext]'s
    /// ## `other_context`
    /// another [`GLContext`][crate::GLContext]
    ///
    /// # Returns
    ///
    /// whether `self` and `other_context` are able to share OpenGL
    ///  resources.
    #[doc(alias = "gst_gl_context_can_share")]
    fn can_share(&self, other_context: &impl IsA<GLContext>) -> bool {
        unsafe {
            from_glib(ffi::gst_gl_context_can_share(
                self.as_ref().to_glib_none().0,
                other_context.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Check for an OpenGL `feature` being supported.
    ///
    /// Note: Most features require that the context be created before it is
    /// possible to determine their existence and so will fail if that is not the
    /// case.
    /// ## `feature`
    /// a platform specific feature
    ///
    /// # Returns
    ///
    /// Whether `feature` is supported by `self`
    #[doc(alias = "gst_gl_context_check_feature")]
    fn check_feature(&self, feature: &str) -> bool {
        unsafe {
            from_glib(ffi::gst_gl_context_check_feature(
                self.as_ref().to_glib_none().0,
                feature.to_glib_none().0,
            ))
        }
    }

    /// Must be called with `self` current.
    /// ## `fbo_target`
    /// the GL value of the framebuffer target, GL_FRAMEBUFFER,
    ///  GL_READ_FRAMEBUFFER, GL_DRAW_FRAMEBUFFER
    ///
    /// # Returns
    ///
    /// whether whether the current framebuffer is complete
    #[doc(alias = "gst_gl_context_check_framebuffer_status")]
    fn check_framebuffer_status(&self, fbo_target: u32) -> bool {
        unsafe {
            from_glib(ffi::gst_gl_context_check_framebuffer_status(
                self.as_ref().to_glib_none().0,
                fbo_target,
            ))
        }
    }

    /// ## `api`
    /// api type required
    /// ## `maj`
    /// major version required
    /// ## `min`
    /// minor version required
    ///
    /// # Returns
    ///
    /// whether OpenGL context implements the required api and specified
    /// version.
    #[doc(alias = "gst_gl_context_check_gl_version")]
    fn check_gl_version(&self, api: GLAPI, maj: i32, min: i32) -> bool {
        unsafe {
            from_glib(ffi::gst_gl_context_check_gl_version(
                self.as_ref().to_glib_none().0,
                api.into_glib(),
                maj,
                min,
            ))
        }
    }

    /// Unbind the current framebuffer
    #[doc(alias = "gst_gl_context_clear_framebuffer")]
    fn clear_framebuffer(&self) {
        unsafe {
            ffi::gst_gl_context_clear_framebuffer(self.as_ref().to_glib_none().0);
        }
    }

    /// Clear's the currently set shader from the GL state machine.
    ///
    /// Note: must be called in the GL thread.
    #[doc(alias = "gst_gl_context_clear_shader")]
    fn clear_shader(&self) {
        unsafe {
            ffi::gst_gl_context_clear_shader(self.as_ref().to_glib_none().0);
        }
    }

    /// Creates an OpenGL context with the specified `other_context` as a context
    /// to share shareable OpenGL objects with. See the OpenGL specification for
    /// what is shared between OpenGL contexts.
    ///
    /// Since 1.20, the configuration can be overriden with the environment variable
    /// `GST_GL_CONFIG` which is a stringified [`gst::Structure`][crate::gst::Structure] as would be returned
    /// from [`config()`][Self::config()]. If `GST_GL_CONFIG` is not set, then the
    /// config will be chosen from `other_context` by calling
    /// [`config()`][Self::config()] on `other_context`. Otherwise, a default
    /// configuration is used.
    ///
    /// Calling [`request_config()`][Self::request_config()]) before calling
    /// [`create()`][Self::create()] will override the config from `other_context` but
    /// will not override the `GST_GL_CONFIG` environment variable.
    ///
    /// If an error occurs, and `error` is not [`None`], then `error` will contain
    /// details of the error and [`false`] will be returned.
    ///
    /// Should only be called once.
    /// ## `other_context`
    /// a [`GLContext`][crate::GLContext] to share OpenGL objects with
    ///
    /// # Returns
    ///
    /// whether the context could successfully be created
    #[doc(alias = "gst_gl_context_create")]
    fn create(&self, other_context: Option<&impl IsA<GLContext>>) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::gst_gl_context_create(
                self.as_ref().to_glib_none().0,
                other_context.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    #[doc(alias = "gst_gl_context_destroy")]
    fn destroy(&self) {
        unsafe {
            ffi::gst_gl_context_destroy(self.as_ref().to_glib_none().0);
        }
    }

    /// Fills `self`'s info (version, extensions, vtable, etc) from the GL
    /// context in the current thread. Typically used with wrapped contexts to
    /// allow wrapped contexts to be used as regular [`GLContext`][crate::GLContext]'s.
    #[doc(alias = "gst_gl_context_fill_info")]
    fn fill_info(&self) -> Result<(), glib::Error> {
        unsafe {
            let mut error = std::ptr::null_mut();
            let is_ok = ffi::gst_gl_context_fill_info(self.as_ref().to_glib_none().0, &mut error);
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Retrieve the OpenGL configuration for this context. The context must
    /// have been successfully created for this function to return a valid value.
    ///
    /// Not all implementations currently support retrieving the config and will
    /// return [`None`] when not supported.
    ///
    /// # Returns
    ///
    /// the configuration chosen for this OpenGL context.
    #[cfg(feature = "v1_20")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
    #[doc(alias = "gst_gl_context_get_config")]
    #[doc(alias = "get_config")]
    fn config(&self) -> Option<gst::Structure> {
        unsafe {
            from_glib_full(ffi::gst_gl_context_get_config(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    ///
    /// # Returns
    ///
    /// the [`GLDisplay`][crate::GLDisplay] associated with this `self`
    #[doc(alias = "gst_gl_context_get_display")]
    #[doc(alias = "get_display")]
    fn display(&self) -> GLDisplay {
        unsafe {
            from_glib_full(ffi::gst_gl_context_get_display(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Get the currently enabled OpenGL api.
    ///
    /// The currently available API may be limited by the [`GLDisplay`][crate::GLDisplay] in use and/or
    /// the [`GLWindow`][crate::GLWindow] chosen.
    ///
    /// # Returns
    ///
    /// the available OpenGL api
    #[doc(alias = "gst_gl_context_get_gl_api")]
    #[doc(alias = "get_gl_api")]
    fn gl_api(&self) -> GLAPI {
        unsafe {
            from_glib(ffi::gst_gl_context_get_gl_api(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Gets the OpenGL platform that used by `self`.
    ///
    /// # Returns
    ///
    /// The platform specific backing OpenGL context
    #[doc(alias = "gst_gl_context_get_gl_platform")]
    #[doc(alias = "get_gl_platform")]
    fn gl_platform(&self) -> GLPlatform {
        unsafe {
            from_glib(ffi::gst_gl_context_get_gl_platform(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Get the version of the OpenGL platform (GLX, EGL, etc) used. Only valid
    /// after a call to [`create()`][Self::create()].
    ///
    /// # Returns
    ///
    ///
    /// ## `major`
    /// return for the major version
    ///
    /// ## `minor`
    /// return for the minor version
    #[doc(alias = "gst_gl_context_get_gl_platform_version")]
    #[doc(alias = "get_gl_platform_version")]
    fn gl_platform_version(&self) -> (i32, i32) {
        unsafe {
            let mut major = std::mem::MaybeUninit::uninit();
            let mut minor = std::mem::MaybeUninit::uninit();
            ffi::gst_gl_context_get_gl_platform_version(
                self.as_ref().to_glib_none().0,
                major.as_mut_ptr(),
                minor.as_mut_ptr(),
            );
            (major.assume_init(), minor.assume_init())
        }
    }

    /// Returns the OpenGL version implemented by `self`. See
    /// [`gl_api()`][Self::gl_api()] for retrieving the OpenGL api implemented by
    /// `self`.
    ///
    /// # Returns
    ///
    ///
    /// ## `maj`
    /// resulting major version
    ///
    /// ## `min`
    /// resulting minor version
    #[doc(alias = "gst_gl_context_get_gl_version")]
    #[doc(alias = "get_gl_version")]
    fn gl_version(&self) -> (i32, i32) {
        unsafe {
            let mut maj = std::mem::MaybeUninit::uninit();
            let mut min = std::mem::MaybeUninit::uninit();
            ffi::gst_gl_context_get_gl_version(
                self.as_ref().to_glib_none().0,
                maj.as_mut_ptr(),
                min.as_mut_ptr(),
            );
            (maj.assume_init(), min.assume_init())
        }
    }

    ///
    /// # Returns
    ///
    /// the currently set window
    #[doc(alias = "gst_gl_context_get_window")]
    #[doc(alias = "get_window")]
    fn window(&self) -> Option<GLWindow> {
        unsafe {
            from_glib_full(ffi::gst_gl_context_get_window(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    ///
    /// # Returns
    ///
    /// Whether the [`GLContext`][crate::GLContext] has been shared with another [`GLContext`][crate::GLContext]
    #[doc(alias = "gst_gl_context_is_shared")]
    fn is_shared(&self) -> bool {
        unsafe {
            from_glib(ffi::gst_gl_context_is_shared(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Set the OpenGL configuration for this context. The context must not
    /// have been created for this function to succeed. Setting a [`None`]
    /// `config` has the affect of removing any specific configuration request.
    ///
    /// Not all implementations currently support retrieving the config and this
    /// function will return FALSE when not supported.
    ///
    /// Note that calling this function may cause a subsequent
    /// [`create()`][Self::create()] to fail if `config` could not be matched with
    /// the platform-specific configuration.
    ///
    /// Note that the actual config used may be differ from the requested values.
    /// ## `gl_config`
    /// a configuration structure for
    ///  configuring the OpenGL context
    ///
    /// # Returns
    ///
    /// whether `gl_config` could be successfully set on `self`
    #[cfg(feature = "v1_20")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_20")))]
    #[doc(alias = "gst_gl_context_request_config")]
    fn request_config(&self, gl_config: Option<gst::Structure>) -> bool {
        unsafe {
            from_glib(ffi::gst_gl_context_request_config(
                self.as_ref().to_glib_none().0,
                gl_config.into_glib_ptr(),
            ))
        }
    }

    /// Will internally set `self` as shared with `share`
    /// ## `share`
    /// another [`GLContext`][crate::GLContext]
    #[doc(alias = "gst_gl_context_set_shared_with")]
    fn set_shared_with(&self, share: &impl IsA<GLContext>) {
        unsafe {
            ffi::gst_gl_context_set_shared_with(
                self.as_ref().to_glib_none().0,
                share.as_ref().to_glib_none().0,
            );
        }
    }

    /// Set's the current window on `self` to `window`. The window can only be
    /// changed before [`create()`][Self::create()] has been called and the `window` is not
    /// already running.
    /// ## `window`
    /// a [`GLWindow`][crate::GLWindow]
    ///
    /// # Returns
    ///
    /// Whether the window was successfully updated
    #[doc(alias = "gst_gl_context_set_window")]
    fn set_window(&self, window: impl IsA<GLWindow>) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_gl_context_set_window(
                    self.as_ref().to_glib_none().0,
                    window.upcast().into_glib_ptr()
                ),
                "Failed to set window"
            )
        }
    }

    /// ## `version`
    /// a [`GLSLVersion`][crate::GLSLVersion]
    /// ## `profile`
    /// a [`GLSLProfile`][crate::GLSLProfile]
    ///
    /// # Returns
    ///
    /// Whether `self` supports the combination of `version` with `profile`
    #[doc(alias = "gst_gl_context_supports_glsl_profile_version")]
    fn supports_glsl_profile_version(&self, version: GLSLVersion, profile: GLSLProfile) -> bool {
        unsafe {
            from_glib(ffi::gst_gl_context_supports_glsl_profile_version(
                self.as_ref().to_glib_none().0,
                version.into_glib(),
                profile.into_glib(),
            ))
        }
    }

    /// ## `version`
    /// a [`GLSLVersion`][crate::GLSLVersion]
    /// ## `profile`
    /// a [`GLSLProfile`][crate::GLSLProfile]
    ///
    /// # Returns
    ///
    /// whether `self` supports the 'precision' specifier in GLSL shaders
    #[cfg(feature = "v1_16")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_gl_context_supports_precision")]
    fn supports_precision(&self, version: GLSLVersion, profile: GLSLProfile) -> bool {
        unsafe {
            from_glib(ffi::gst_gl_context_supports_precision(
                self.as_ref().to_glib_none().0,
                version.into_glib(),
                profile.into_glib(),
            ))
        }
    }

    /// ## `version`
    /// a [`GLSLVersion`][crate::GLSLVersion]
    /// ## `profile`
    /// a [`GLSLProfile`][crate::GLSLProfile]
    ///
    /// # Returns
    ///
    /// whether `self` supports the 'precision highp' specifier in GLSL shaders
    #[cfg(feature = "v1_16")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_gl_context_supports_precision_highp")]
    fn supports_precision_highp(&self, version: GLSLVersion, profile: GLSLProfile) -> bool {
        unsafe {
            from_glib(ffi::gst_gl_context_supports_precision_highp(
                self.as_ref().to_glib_none().0,
                version.into_glib(),
                profile.into_glib(),
            ))
        }
    }

    /// Swap the front and back buffers on the window attached to `self`.
    /// This will display the frame on the next refresh cycle.
    #[doc(alias = "gst_gl_context_swap_buffers")]
    fn swap_buffers(&self) {
        unsafe {
            ffi::gst_gl_context_swap_buffers(self.as_ref().to_glib_none().0);
        }
    }
}

impl<O: IsA<GLContext>> GLContextExt for O {}