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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use glib;
use glib::object::IsA;
use glib::translate::*;
use gst;
use gst_gl_sys;
use std::mem;
use std::ptr;
use GLDisplay;
use GLPlatform;
use GLSLProfile;
use GLSLVersion;
use GLWindow;
use GLAPI;

glib_wrapper! {
    /// `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.
    ///
    /// # Implements
    ///
    /// [`GLContextExt`](trait.GLContextExt.html), [`gst::ObjectExt`](../gst/trait.ObjectExt.html), [`glib::object::ObjectExt`](../glib/object/trait.ObjectExt.html), [`GLContextExtManual`](prelude/trait.GLContextExtManual.html)
    pub struct GLContext(Object<gst_gl_sys::GstGLContext, gst_gl_sys::GstGLContextClass, GLContextClass>) @extends gst::Object;

    match fn {
        get_type => || gst_gl_sys::gst_gl_context_get_type(),
    }
}

impl GLContext {
    /// Create a new `GLContext` with the specified `display`
    /// ## `display`
    /// a `GLDisplay`
    ///
    /// # Returns
    ///
    /// a new `GLContext`
    pub fn new<P: IsA<GLDisplay>>(display: &P) -> GLContext {
        skip_assert_initialized!();
        unsafe {
            from_glib_none(gst_gl_sys::gst_gl_context_new(
                display.as_ref().to_glib_none().0,
            ))
        }
    }

    /// See also `GLContextExt::activate`.
    ///
    /// # Returns
    ///
    /// the `GLContext` active in the current thread or `None`
    pub fn get_current() -> Option<GLContext> {
        assert_initialized_main_thread!();
        unsafe { from_glib_none(gst_gl_sys::gst_gl_context_get_current()) }
    }

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

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

pub const NONE_GL_CONTEXT: Option<&GLContext> = None;

/// Trait containing all `GLContext` methods.
///
/// # Implementors
///
/// [`GLContext`](struct.GLContext.html)
pub trait GLContextExt: '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 `GLContextExt::set_window` for details.
    /// ## `activate`
    /// `true` to activate, `false` to deactivate
    ///
    /// # Returns
    ///
    /// Whether the activation succeeded
    fn activate(&self, activate: bool) -> Result<(), glib::error::BoolError>;

    /// Note: This will always fail for two wrapped `GLContext`'s
    /// ## `other_context`
    /// another `GLContext`
    ///
    /// # Returns
    ///
    /// whether `self` and `other_context` are able to share OpenGL
    ///  resources.
    fn can_share<P: IsA<GLContext>>(&self, other_context: &P) -> bool;

    /// 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`
    fn check_feature(&self, feature: &str) -> bool;

    /// ## `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
    fn check_framebuffer_status(&self, fbo_target: u32) -> bool;

    /// ## `api`
    /// api type required
    /// ## `maj`
    /// major version required
    /// ## `min`
    /// minor version required
    ///
    /// # Returns
    ///
    /// whether OpenGL context implements the required api and specified
    /// version.
    fn check_gl_version(&self, api: GLAPI, maj: i32, min: i32) -> bool;

    /// Unbind the current framebuffer
    fn clear_framebuffer(&self);

    /// Clear's the currently set shader from the GL state machine.
    ///
    /// Note: must be called in the GL thread.
    fn clear_shader(&self);

    /// 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.
    ///
    /// 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` to share OpenGL objects with
    ///
    /// # Returns
    ///
    /// whether the context could successfully be created
    fn create<P: IsA<GLContext>>(&self, other_context: Option<&P>) -> Result<(), glib::Error>;

    /// Destroys an OpenGL context.
    ///
    /// Should only be called after `GLContextExt::create` has been successfully
    /// called for this context.
    fn destroy(&self);

    /// 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`'s.
    fn fill_info(&self) -> Result<(), glib::Error>;

    ///
    /// # Returns
    ///
    /// the `GLDisplay` associated with this `self`
    fn get_display(&self) -> GLDisplay;

    /// Get the currently enabled OpenGL api.
    ///
    /// The currently available API may be limited by the `GLDisplay` in use and/or
    /// the `GLWindow` chosen.
    ///
    /// # Returns
    ///
    /// the available OpenGL api
    fn get_gl_api(&self) -> GLAPI;

    /// Gets the OpenGL platform that used by `self`.
    ///
    /// # Returns
    ///
    /// The platform specific backing OpenGL context
    fn get_gl_platform(&self) -> GLPlatform;

    /// Get the version of the OpenGL platform (GLX, EGL, etc) used. Only valid
    /// after a call to `GLContextExt::create`.
    /// ## `major`
    /// return for the major version
    /// ## `minor`
    /// return for the minor version
    fn get_gl_platform_version(&self) -> (i32, i32);

    /// Returns the OpenGL version implemented by `self`. See
    /// `GLContextExt::get_gl_api` for retrieving the OpenGL api implemented by
    /// `self`.
    /// ## `maj`
    /// resulting major version
    /// ## `min`
    /// resulting minor version
    fn get_gl_version(&self) -> (i32, i32);

    ///
    /// # Returns
    ///
    /// the currently set window
    fn get_window(&self) -> Option<GLWindow>;

    ///
    /// # Returns
    ///
    /// Whether the `GLContext` has been shared with another `GLContext`
    fn is_shared(&self) -> bool;

    /// Will internally set `self` as shared with `share`
    /// ## `share`
    /// another `GLContext`
    fn set_shared_with<P: IsA<GLContext>>(&self, share: &P);

    /// Set's the current window on `self` to `window`. The window can only be
    /// changed before `GLContextExt::create` has been called and the `window` is not
    /// already running.
    /// ## `window`
    /// a `GLWindow`
    ///
    /// # Returns
    ///
    /// Whether the window was successfully updated
    fn set_window<P: IsA<GLWindow>>(&self, window: &P) -> Result<(), glib::error::BoolError>;

    /// ## `version`
    /// a `GLSLVersion`
    /// ## `profile`
    /// a `GLSLProfile`
    ///
    /// # Returns
    ///
    /// Whether `self` supports the combination of `version` with `profile`
    fn supports_glsl_profile_version(&self, version: GLSLVersion, profile: GLSLProfile) -> bool;

    ///
    /// Feature: `v1_16`
    ///
    /// ## `version`
    /// a `GLSLVersion`
    /// ## `profile`
    /// a `GLSLProfile`
    ///
    /// # Returns
    ///
    /// whether `self` supports the 'precision' specifier in GLSL shaders
    #[cfg(any(feature = "v1_16", feature = "dox"))]
    fn supports_precision(&self, version: GLSLVersion, profile: GLSLProfile) -> bool;

    ///
    /// Feature: `v1_16`
    ///
    /// ## `version`
    /// a `GLSLVersion`
    /// ## `profile`
    /// a `GLSLProfile`
    ///
    /// # Returns
    ///
    /// whether `self` supports the 'precision highp' specifier in GLSL shaders
    #[cfg(any(feature = "v1_16", feature = "dox"))]
    fn supports_precision_highp(&self, version: GLSLVersion, profile: GLSLProfile) -> bool;

    /// Swap the front and back buffers on the window attached to `self`.
    /// This will display the frame on the next refresh cycle.
    fn swap_buffers(&self);
}

impl<O: IsA<GLContext>> GLContextExt for O {
    fn activate(&self, activate: bool) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib_result_from_gboolean!(
                gst_gl_sys::gst_gl_context_activate(
                    self.as_ref().to_glib_none().0,
                    activate.to_glib()
                ),
                "Failed to activate OpenGL context"
            )
        }
    }

    fn can_share<P: IsA<GLContext>>(&self, other_context: &P) -> bool {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_context_can_share(
                self.as_ref().to_glib_none().0,
                other_context.as_ref().to_glib_none().0,
            ))
        }
    }

    fn check_feature(&self, feature: &str) -> bool {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_context_check_feature(
                self.as_ref().to_glib_none().0,
                feature.to_glib_none().0,
            ))
        }
    }

    fn check_framebuffer_status(&self, fbo_target: u32) -> bool {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_context_check_framebuffer_status(
                self.as_ref().to_glib_none().0,
                fbo_target,
            ))
        }
    }

    fn check_gl_version(&self, api: GLAPI, maj: i32, min: i32) -> bool {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_context_check_gl_version(
                self.as_ref().to_glib_none().0,
                api.to_glib(),
                maj,
                min,
            ))
        }
    }

    fn clear_framebuffer(&self) {
        unsafe {
            gst_gl_sys::gst_gl_context_clear_framebuffer(self.as_ref().to_glib_none().0);
        }
    }

    fn clear_shader(&self) {
        unsafe {
            gst_gl_sys::gst_gl_context_clear_shader(self.as_ref().to_glib_none().0);
        }
    }

    fn create<P: IsA<GLContext>>(&self, other_context: Option<&P>) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ = gst_gl_sys::gst_gl_context_create(
                self.as_ref().to_glib_none().0,
                other_context.map(|p| p.as_ref()).to_glib_none().0,
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn destroy(&self) {
        unsafe {
            gst_gl_sys::gst_gl_context_destroy(self.as_ref().to_glib_none().0);
        }
    }

    fn fill_info(&self) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            let _ =
                gst_gl_sys::gst_gl_context_fill_info(self.as_ref().to_glib_none().0, &mut error);
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    fn get_display(&self) -> GLDisplay {
        unsafe {
            from_glib_full(gst_gl_sys::gst_gl_context_get_display(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_gl_api(&self) -> GLAPI {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_context_get_gl_api(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_gl_platform(&self) -> GLPlatform {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_context_get_gl_platform(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn get_gl_platform_version(&self) -> (i32, i32) {
        unsafe {
            let mut major = mem::MaybeUninit::uninit();
            let mut minor = mem::MaybeUninit::uninit();
            gst_gl_sys::gst_gl_context_get_gl_platform_version(
                self.as_ref().to_glib_none().0,
                major.as_mut_ptr(),
                minor.as_mut_ptr(),
            );
            let major = major.assume_init();
            let minor = minor.assume_init();
            (major, minor)
        }
    }

    fn get_gl_version(&self) -> (i32, i32) {
        unsafe {
            let mut maj = mem::MaybeUninit::uninit();
            let mut min = mem::MaybeUninit::uninit();
            gst_gl_sys::gst_gl_context_get_gl_version(
                self.as_ref().to_glib_none().0,
                maj.as_mut_ptr(),
                min.as_mut_ptr(),
            );
            let maj = maj.assume_init();
            let min = min.assume_init();
            (maj, min)
        }
    }

    fn get_window(&self) -> Option<GLWindow> {
        unsafe {
            from_glib_full(gst_gl_sys::gst_gl_context_get_window(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn is_shared(&self) -> bool {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_context_is_shared(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn set_shared_with<P: IsA<GLContext>>(&self, share: &P) {
        unsafe {
            gst_gl_sys::gst_gl_context_set_shared_with(
                self.as_ref().to_glib_none().0,
                share.as_ref().to_glib_none().0,
            );
        }
    }

    fn set_window<P: IsA<GLWindow>>(&self, window: &P) -> Result<(), glib::error::BoolError> {
        unsafe {
            glib_result_from_gboolean!(
                gst_gl_sys::gst_gl_context_set_window(
                    self.as_ref().to_glib_none().0,
                    window.as_ref().to_glib_full()
                ),
                "Failed to set window"
            )
        }
    }

    fn supports_glsl_profile_version(&self, version: GLSLVersion, profile: GLSLProfile) -> bool {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_context_supports_glsl_profile_version(
                self.as_ref().to_glib_none().0,
                version.to_glib(),
                profile.to_glib(),
            ))
        }
    }

    #[cfg(any(feature = "v1_16", feature = "dox"))]
    fn supports_precision(&self, version: GLSLVersion, profile: GLSLProfile) -> bool {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_context_supports_precision(
                self.as_ref().to_glib_none().0,
                version.to_glib(),
                profile.to_glib(),
            ))
        }
    }

    #[cfg(any(feature = "v1_16", feature = "dox"))]
    fn supports_precision_highp(&self, version: GLSLVersion, profile: GLSLProfile) -> bool {
        unsafe {
            from_glib(gst_gl_sys::gst_gl_context_supports_precision_highp(
                self.as_ref().to_glib_none().0,
                version.to_glib(),
                profile.to_glib(),
            ))
        }
    }

    fn swap_buffers(&self) {
        unsafe {
            gst_gl_sys::gst_gl_context_swap_buffers(self.as_ref().to_glib_none().0);
        }
    }
}