/
gstglcontext.c
1889 lines (1583 loc) · 51.2 KB
/
gstglcontext.c
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GStreamer
* Copyright (C) 2013 Matthew Waters <ystreet00@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:gstglcontext
* @short_description: OpenGL context abstraction
* @title: GstGLContext
* @see_also: #GstGLDisplay, #GstGLWindow
*
* #GstGLContext 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.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if defined(ANDROID) || defined(__ANDROID__)
/* Avoid a linker error with _isoc99_sscanf() when building a shared library
* for android
*/
#define _GNU_SOURCE
#endif
#include "gstglcontext.h"
#include <gst/gl/gl.h>
#include <gmodule.h>
#include <string.h>
#include <stdio.h>
#include "gstglcontext_private.h"
#include "gstglfeature.h"
#include "gstglfeature_private.h"
#include "gstglfuncs.h"
#ifndef GL_NUM_EXTENSIONS
#define GL_NUM_EXTENSIONS 0x0000821d
#endif
#if GST_GL_HAVE_PLATFORM_GLX
#include "x11/gstglcontext_glx.h"
#endif
#if GST_GL_HAVE_PLATFORM_EGL
#include "egl/gstglcontext_egl.h"
#endif
#if GST_GL_HAVE_PLATFORM_CGL
#include "cocoa/gstglcontext_cocoa.h"
#endif
#if GST_GL_HAVE_PLATFORM_WGL
#include "wgl/gstglcontext_wgl.h"
#endif
#if GST_GL_HAVE_PLATFORM_EAGL
#include "eagl/gstglcontext_eagl.h"
#endif
extern void _gst_gl_debug_enable (GstGLContext * context);
static GPrivate current_context_key;
static GModule *module_self;
static GOnce module_self_gonce = G_ONCE_INIT;
#if GST_GL_HAVE_OPENGL
static GOnce module_opengl_gonce = G_ONCE_INIT;
static GModule *module_opengl;
static gpointer
load_opengl_module (gpointer user_data)
{
#ifdef GST_GL_LIBGL_MODULE_NAME
module_opengl = g_module_open (GST_GL_LIBGL_MODULE_NAME, G_MODULE_BIND_LAZY);
#else
/* On Linux the .so is only in -dev packages, try with a real soname
* Proper compilers will optimize away the strcmp */
if (g_strcmp0 (G_MODULE_SUFFIX, "so") == 0)
module_opengl = g_module_open ("libGL.so.1", G_MODULE_BIND_LAZY);
/* This automatically handles the suffix and even .la files */
if (!module_opengl)
module_opengl = g_module_open ("libGL", G_MODULE_BIND_LAZY);
#endif
return NULL;
}
#endif
#if GST_GL_HAVE_GLES2
static GOnce module_gles2_gonce = G_ONCE_INIT;
static GModule *module_gles2;
static gpointer
load_gles2_module (gpointer user_data)
{
#ifdef GST_GL_LIBGLESV2_MODULE_NAME
module_gles2 =
g_module_open (GST_GL_LIBGLESV2_MODULE_NAME, G_MODULE_BIND_LAZY);
#else
/* On Linux the .so is only in -dev packages, try with a real soname
* Proper compilers will optimize away the strcmp */
if (g_strcmp0 (G_MODULE_SUFFIX, "so") == 0)
module_gles2 = g_module_open ("libGLESv2.so.2", G_MODULE_BIND_LAZY);
/* This automatically handles the suffix and even .la files */
if (!module_gles2)
module_gles2 = g_module_open ("libGLESv2", G_MODULE_BIND_LAZY);
#endif
return NULL;
}
#endif
static gpointer
load_self_module (gpointer user_data)
{
module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
return NULL;
}
/* Context sharedness is tracked by a refcounted pointer stored in each context
* object to track complex creation/deletion scenarios. As a result,
* sharedness can only be successfully validated between two GstGLContext's
* where one is not a wrapped context.
*
* As there is no API at the winsys level to tell whether two OpenGL contexts
* can share GL resources, this is the next best thing.
*
* XXX: we may need a way to associate two wrapped GstGLContext's as being
* shared however I have not come across a use case that requires this yet.
*/
struct ContextShareGroup
{
int refcount;
};
static struct ContextShareGroup *
_context_share_group_new (void)
{
struct ContextShareGroup *ret = g_new0 (struct ContextShareGroup, 1);
ret->refcount = 1;
return ret;
}
static struct ContextShareGroup *
_context_share_group_ref (struct ContextShareGroup *share)
{
g_atomic_int_inc (&share->refcount);
return share;
}
static void
_context_share_group_unref (struct ContextShareGroup *share)
{
if (g_atomic_int_dec_and_test (&share->refcount))
g_free (share);
}
static gboolean
_context_share_group_is_shared (struct ContextShareGroup *share)
{
return g_atomic_int_get (&share->refcount) > 1;
}
#define GST_CAT_DEFAULT gst_gl_context_debug
GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
GST_DEBUG_CATEGORY_STATIC (gst_gl_debug);
static void _init_debug (void);
static gpointer gst_gl_context_create_thread (GstGLContext * context);
static void gst_gl_context_finalize (GObject * object);
static void gst_gl_context_default_get_gl_platform_version (GstGLContext *
context, gint * major, gint * minor);
struct _GstGLContextPrivate
{
GThread *gl_thread;
GThread *active_thread;
/* conditions */
GMutex render_lock;
GCond create_cond;
GCond destroy_cond;
gboolean created;
gboolean alive;
GWeakRef other_context_ref;
struct ContextShareGroup *sharegroup;
GError **error;
gint gl_major;
gint gl_minor;
gchar *gl_exts;
};
typedef struct
{
GstGLContext parent;
guintptr handle;
GstGLPlatform platform;
GstGLAPI available_apis;
} GstGLWrappedContext;
typedef struct
{
GstGLContextClass parent;
} GstGLWrappedContextClass;
#define gst_gl_context_parent_class parent_class
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GstGLContext, gst_gl_context,
GST_TYPE_OBJECT);
#define GST_TYPE_GL_WRAPPED_CONTEXT (gst_gl_wrapped_context_get_type())
static GType gst_gl_wrapped_context_get_type (void);
G_DEFINE_TYPE (GstGLWrappedContext, gst_gl_wrapped_context,
GST_TYPE_GL_CONTEXT);
#define GST_GL_WRAPPED_CONTEXT(o) (G_TYPE_CHECK_INSTANCE_CAST((o), GST_TYPE_GL_WRAPPED_CONTEXT, GstGLWrappedContext))
#define GST_GL_WRAPPED_CONTEXT_CLASS(k) (G_TYPE_CHECK_CLASS((k), GST_TYPE_GL_CONTEXT, GstGLContextClass))
#define GST_IS_GL_WRAPPED_CONTEXT(o) (G_TYPE_CHECK_INSTANCE_TYPE((o), GST_TYPE_GL_WRAPPED_CONTEXT))
#define GST_IS_GL_WRAPPED_CONTEXT_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE((k), GST_TYPE_GL_WRAPPED_CONTEXT))
#define GST_GL_WRAPPED_CONTEXT_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS((o), GST_TYPE_GL_WRAPPED_CONTEXT, GstGLWrappedContextClass))
/**
* gst_gl_context_error_quark:
*
* Returns: the quark used for #GstGLContext in #GError's
*/
GQuark
gst_gl_context_error_quark (void)
{
return g_quark_from_static_string ("gst-gl-context-error-quark");
}
static void
_ensure_window (GstGLContext * context)
{
GstGLWindow *window;
if (context->window)
return;
window = gst_gl_display_create_window (context->display);
gst_gl_context_set_window (context, window);
gst_object_unref (window);
}
static void
gst_gl_context_init (GstGLContext * context)
{
context->priv = gst_gl_context_get_instance_private (context);
context->window = NULL;
context->gl_vtable = g_slice_alloc0 (sizeof (GstGLFuncs));
g_mutex_init (&context->priv->render_lock);
g_cond_init (&context->priv->create_cond);
g_cond_init (&context->priv->destroy_cond);
context->priv->created = FALSE;
g_weak_ref_init (&context->priv->other_context_ref, NULL);
}
static void
gst_gl_context_class_init (GstGLContextClass * klass)
{
klass->get_proc_address =
GST_DEBUG_FUNCPTR (gst_gl_context_default_get_proc_address);
klass->get_gl_platform_version =
GST_DEBUG_FUNCPTR (gst_gl_context_default_get_gl_platform_version);
G_OBJECT_CLASS (klass)->finalize = gst_gl_context_finalize;
_init_debug ();
}
static void
_init_debug (void)
{
static gsize _init = 0;
if (g_once_init_enter (&_init)) {
GST_DEBUG_CATEGORY_INIT (gst_gl_context_debug, "glcontext", 0,
"glcontext element");
GST_DEBUG_CATEGORY_INIT (gst_gl_debug, "gldebug", 0, "OpenGL Debugging");
g_once_init_leave (&_init, 1);
}
}
/**
* gst_gl_context_new:
* @display: a #GstGLDisplay
*
* Create a new #GstGLContext with the specified @display
*
* Returns: a new #GstGLContext
*
* Since: 1.4
*/
GstGLContext *
gst_gl_context_new (GstGLDisplay * display)
{
GstGLContext *context = NULL;
const gchar *user_choice;
_init_debug ();
user_choice = g_getenv ("GST_GL_PLATFORM");
GST_INFO ("creating a context for display %" GST_PTR_FORMAT
", user choice:%s", display, user_choice);
#if GST_GL_HAVE_PLATFORM_CGL
if (!context && (!user_choice || g_strstr_len (user_choice, 3, "cgl")))
context = GST_GL_CONTEXT (gst_gl_context_cocoa_new (display));
#endif
#if GST_GL_HAVE_PLATFORM_GLX
if (!context && (!user_choice || g_strstr_len (user_choice, 3, "glx")))
context = GST_GL_CONTEXT (gst_gl_context_glx_new (display));
#endif
#if GST_GL_HAVE_PLATFORM_EGL
if (!context && (!user_choice || g_strstr_len (user_choice, 3, "egl")))
context = GST_GL_CONTEXT (gst_gl_context_egl_new (display));
#endif
#if GST_GL_HAVE_PLATFORM_WGL
if (!context && (!user_choice || g_strstr_len (user_choice, 3, "wgl")))
context = GST_GL_CONTEXT (gst_gl_context_wgl_new (display));
#endif
#if GST_GL_HAVE_PLATFORM_EAGL
if (!context && (!user_choice || g_strstr_len (user_choice, 4, "eagl")))
context = GST_GL_CONTEXT (gst_gl_context_eagl_new (display));
#endif
if (!context) {
/* subclass returned a NULL context */
GST_WARNING ("Could not create context. user specified %s",
user_choice ? user_choice : "(null)");
return NULL;
}
context->display = gst_object_ref (display);
GST_DEBUG_OBJECT (context,
"Done creating context for display %" GST_PTR_FORMAT " (user_choice:%s)",
display, user_choice);
return context;
}
/**
* gst_gl_context_new_wrapped:
* @display: a #GstGLDisplay
* @handle: the OpenGL context to wrap
* @context_type: a #GstGLPlatform specifying the type of context in @handle
* @available_apis: a #GstGLAPI containing the available OpenGL apis in @handle
*
* Wraps an existing OpenGL context into a #GstGLContext.
*
* Note: The caller is responsible for ensuring that the OpenGL context
* represented by @handle stays alive while the returned #GstGLContext is
* active.
*
* @context_type must not be %GST_GL_PLATFORM_NONE or %GST_GL_PLATFORM_ANY
*
* @available_apis must not be %GST_GL_API_NONE or %GST_GL_API_ANY
*
* Returns: (transfer full): a #GstGLContext wrapping @handle
*
* Since: 1.4
*/
GstGLContext *
gst_gl_context_new_wrapped (GstGLDisplay * display, guintptr handle,
GstGLPlatform context_type, GstGLAPI available_apis)
{
GstGLContext *context;
GstGLWrappedContext *context_wrap = NULL;
GstGLContextClass *context_class;
GstGLAPI display_api;
_init_debug ();
g_return_val_if_fail (GST_IS_GL_DISPLAY (display), NULL);
g_return_val_if_fail (handle != 0, NULL);
g_return_val_if_fail (available_apis != GST_GL_API_ANY, NULL);
g_return_val_if_fail (available_apis != GST_GL_API_NONE, NULL);
g_return_val_if_fail (context_type != GST_GL_PLATFORM_NONE, NULL);
g_return_val_if_fail (context_type != GST_GL_PLATFORM_ANY, NULL);
display_api = gst_gl_display_get_gl_api (display);
g_return_val_if_fail ((display_api & available_apis) != GST_GL_API_NONE,
NULL);
context_wrap = g_object_new (GST_TYPE_GL_WRAPPED_CONTEXT, NULL);
gst_object_ref_sink (context_wrap);
if (!context_wrap) {
/* subclass returned a NULL context */
GST_ERROR ("Could not wrap existing context");
return NULL;
}
context = (GstGLContext *) context_wrap;
context->display = gst_object_ref (display);
context->priv->sharegroup = _context_share_group_new ();
context_wrap->handle = handle;
context_wrap->platform = context_type;
context_wrap->available_apis = available_apis;
context_class = GST_GL_CONTEXT_GET_CLASS (context);
#if GST_GL_HAVE_PLATFORM_GLX
if (context_type == GST_GL_PLATFORM_GLX) {
context_class->get_current_context = gst_gl_context_glx_get_current_context;
context_class->get_proc_address = gst_gl_context_glx_get_proc_address;
}
#endif
#if GST_GL_HAVE_PLATFORM_EGL
if (context_type == GST_GL_PLATFORM_EGL) {
context_class->get_current_context = gst_gl_context_egl_get_current_context;
context_class->get_proc_address = gst_gl_context_egl_get_proc_address;
}
#endif
#if GST_GL_HAVE_PLATFORM_CGL
if (context_type == GST_GL_PLATFORM_CGL) {
context_class->get_current_context =
gst_gl_context_cocoa_get_current_context;
context_class->get_proc_address = gst_gl_context_default_get_proc_address;
}
#endif
#if GST_GL_HAVE_PLATFORM_WGL
if (context_type == GST_GL_PLATFORM_WGL) {
context_class->get_current_context = gst_gl_context_wgl_get_current_context;
context_class->get_proc_address = gst_gl_context_wgl_get_proc_address;
}
#endif
#if GST_GL_HAVE_PLATFORM_EAGL
if (context_type == GST_GL_PLATFORM_EAGL) {
context_class->get_current_context =
gst_gl_context_eagl_get_current_context;
context_class->get_proc_address = gst_gl_context_default_get_proc_address;
}
#endif
if (!context_class->get_current_context) {
/* we don't have API support */
gst_object_unref (context);
return NULL;
}
return context;
}
/**
* gst_gl_context_get_current_gl_context:
* @context_type: a #GstGLPlatform specifying the type of context to retrieve
*
* Returns: The OpenGL context handle current in the calling thread or %NULL
*
* Since: 1.6
*/
guintptr
gst_gl_context_get_current_gl_context (GstGLPlatform context_type)
{
guintptr handle = 0;
_init_debug ();
#if GST_GL_HAVE_PLATFORM_GLX
if (!handle && (context_type & GST_GL_PLATFORM_GLX) != 0)
handle = gst_gl_context_glx_get_current_context ();
#endif
#if GST_GL_HAVE_PLATFORM_EGL
if (!handle && (context_type & GST_GL_PLATFORM_EGL) != 0)
handle = gst_gl_context_egl_get_current_context ();
#endif
#if GST_GL_HAVE_PLATFORM_CGL
if (!handle && (context_type & GST_GL_PLATFORM_CGL) != 0)
handle = gst_gl_context_cocoa_get_current_context ();
#endif
#if GST_GL_HAVE_PLATFORM_WGL
if (!handle && (context_type & GST_GL_PLATFORM_WGL) != 0)
handle = gst_gl_context_wgl_get_current_context ();
#endif
#if GST_GL_HAVE_PLATFORM_EAGL
if (!handle && (context_type & GST_GL_PLATFORM_EAGL) != 0)
handle = gst_gl_context_eagl_get_current_context ();
#endif
if (!handle)
GST_WARNING ("Could not retrieve current context");
return handle;
}
/**
* gst_gl_context_get_proc_address_with_platform:
* @context_type: a #GstGLPlatform
* @gl_api: a #GstGLAPI
* @name: the name of the function to retrieve
*
* Attempts to use the @context_type specific GetProcAddress implementations
* to retrieve @name.
*
* See also gst_gl_context_get_proc_address().
*
* Returns: a function pointer for @name, or %NULL
*
* Since: 1.6
*/
gpointer
gst_gl_context_get_proc_address_with_platform (GstGLPlatform context_type,
GstGLAPI gl_api, const gchar * name)
{
gpointer ret = NULL;
#if GST_GL_HAVE_PLATFORM_GLX
if (!ret && (context_type & GST_GL_PLATFORM_GLX) != 0)
ret = gst_gl_context_glx_get_proc_address (gl_api, name);
#endif
#if GST_GL_HAVE_PLATFORM_EGL
if (!ret && (context_type & GST_GL_PLATFORM_EGL) != 0)
ret = gst_gl_context_egl_get_proc_address (gl_api, name);
#endif
#if GST_GL_HAVE_PLATFORM_WGL
if (!ret && (context_type & GST_GL_PLATFORM_WGL) != 0)
ret = gst_gl_context_wgl_get_proc_address (gl_api, name);
#endif
/* CGL and EAGL rely on the default impl */
if (!ret)
ret = gst_gl_context_default_get_proc_address (gl_api, name);
return ret;
}
/**
* gst_gl_context_get_current_gl_api:
* @platform: the #GstGLPlatform to retrieve the API for
* @major: (out) (allow-none): the major version
* @minor: (out) (allow-none): the minor version
*
* If an error occurs, @major and @minor are not modified and %GST_GL_API_NONE is
* returned.
*
* Returns: The version supported by the OpenGL context current in the calling
* thread or %GST_GL_API_NONE
*
* Since: 1.6
*/
GstGLAPI
gst_gl_context_get_current_gl_api (GstGLPlatform platform, guint * major,
guint * minor)
{
const GLubyte *(GSTGLAPI * GetString) (GLenum name);
#if GST_GL_HAVE_OPENGL
void (GSTGLAPI * GetIntegerv) (GLenum name, GLuint * n);
#endif
const gchar *version;
gint maj, min, n;
GstGLAPI ret = (1U << 31);
_init_debug ();
while (ret != GST_GL_API_NONE) {
/* FIXME: attempt to delve into the platform specific GetProcAddress */
GetString =
gst_gl_context_get_proc_address_with_platform (platform, ret,
"glGetString");
#if GST_GL_HAVE_OPENGL
GetIntegerv =
gst_gl_context_get_proc_address_with_platform (platform, ret,
"glGetIntegerv");
#endif
if (!GetString) {
goto next;
}
version = (const gchar *) GetString (GL_VERSION);
if (!version)
goto next;
/* strlen (x.x) == 3 */
n = strlen (version);
if (n < 3)
goto next;
if (g_strstr_len (version, 9, "OpenGL ES")) {
/* strlen (OpenGL ES x.x) == 13 */
if (n < 13)
goto next;
sscanf (&version[10], "%d.%d", &maj, &min);
if (maj <= 0 || min < 0)
goto next;
if (maj == 1) {
ret = GST_GL_API_GLES1;
break;
} else if (maj == 2 || maj == 3) {
ret = GST_GL_API_GLES2;
break;
}
goto next;
} else {
sscanf (version, "%d.%d", &maj, &min);
if (maj <= 0 || min < 0)
goto next;
#if GST_GL_HAVE_OPENGL
if (GetIntegerv && (maj > 3 || (maj == 3 && min > 1))) {
GLuint context_flags = 0;
ret = GST_GL_API_NONE;
GetIntegerv (GL_CONTEXT_PROFILE_MASK, &context_flags);
if (context_flags & GL_CONTEXT_CORE_PROFILE_BIT)
ret |= GST_GL_API_OPENGL3;
if (context_flags & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT)
ret |= GST_GL_API_OPENGL;
break;
}
#endif
ret = GST_GL_API_OPENGL;
break;
}
next:
/* iterate through the apis */
ret >>= 1;
}
if (ret == GST_GL_API_NONE)
return GST_GL_API_NONE;
if (major)
*major = maj;
if (minor)
*minor = min;
return ret;
}
static void
gst_gl_context_finalize (GObject * object)
{
GstGLContext *context = GST_GL_CONTEXT (object);
if (context->window) {
gst_gl_window_set_resize_callback (context->window, NULL, NULL, NULL);
gst_gl_window_set_draw_callback (context->window, NULL, NULL, NULL);
g_mutex_lock (&context->priv->render_lock);
if (context->priv->alive) {
GST_INFO_OBJECT (context, "send quit gl window loop");
gst_gl_window_quit (context->window);
GST_INFO_OBJECT (context, "joining gl thread");
while (context->priv->alive)
g_cond_wait (&context->priv->destroy_cond, &context->priv->render_lock);
GST_INFO_OBJECT (context, "gl thread joined");
if (context->priv->gl_thread) {
g_thread_unref (context->priv->gl_thread);
context->priv->gl_thread = NULL;
}
}
g_mutex_unlock (&context->priv->render_lock);
gst_gl_window_set_close_callback (context->window, NULL, NULL, NULL);
gst_object_unref (context->window);
context->window = NULL;
}
if (context->priv->active_thread) {
g_thread_unref (context->priv->active_thread);
context->priv->active_thread = NULL;
}
if (context->priv->gl_thread) {
g_thread_unref (context->priv->gl_thread);
context->priv->gl_thread = NULL;
}
if (context->priv->sharegroup) {
_context_share_group_unref (context->priv->sharegroup);
context->priv->sharegroup = NULL;
}
if (context->display) {
gst_object_unref (context->display);
context->display = NULL;
}
if (context->gl_vtable) {
g_slice_free (GstGLFuncs, context->gl_vtable);
context->gl_vtable = NULL;
}
g_mutex_clear (&context->priv->render_lock);
g_cond_clear (&context->priv->create_cond);
g_cond_clear (&context->priv->destroy_cond);
g_free (context->priv->gl_exts);
g_weak_ref_clear (&context->priv->other_context_ref);
GST_DEBUG_OBJECT (context, "End of finalize");
G_OBJECT_CLASS (gst_gl_context_parent_class)->finalize (object);
}
/**
* gst_gl_context_activate:
* @context: a #GstGLContext
* @activate: %TRUE to activate, %FALSE to deactivate
*
* (De)activate the OpenGL context represented by this @context.
*
* In OpenGL terms, calls eglMakeCurrent or similar with this context and the
* currently set window. See gst_gl_context_set_window() for details.
*
* Returns: Whether the activation succeeded
*
* Since: 1.4
*/
gboolean
gst_gl_context_activate (GstGLContext * context, gboolean activate)
{
GstGLContextClass *context_class;
gboolean result;
g_return_val_if_fail (GST_IS_GL_CONTEXT (context), FALSE);
context_class = GST_GL_CONTEXT_GET_CLASS (context);
g_return_val_if_fail (context_class->activate != NULL, FALSE);
GST_DEBUG_OBJECT (context, "activate:%d", activate);
GST_OBJECT_LOCK (context);
result = context_class->activate (context, activate);
if (result && activate) {
GThread *old_thread = context->priv->active_thread;
context->priv->active_thread = g_thread_ref (g_thread_self ());
if (old_thread) {
g_thread_unref (old_thread);
}
g_private_set (¤t_context_key, context);
} else {
if (context->priv->active_thread) {
g_thread_unref (context->priv->active_thread);
context->priv->active_thread = NULL;
}
g_private_set (¤t_context_key, NULL);
}
GST_OBJECT_UNLOCK (context);
return result;
}
/**
* gst_gl_context_get_thread:
* @context: a #GstGLContext
*
* Returns: (transfer full): The #GThread, @context is current in or NULL
*
* Since: 1.6
*/
GThread *
gst_gl_context_get_thread (GstGLContext * context)
{
GThread *ret;
g_return_val_if_fail (GST_IS_GL_CONTEXT (context), NULL);
GST_OBJECT_LOCK (context);
ret = context->priv->active_thread;
if (ret)
g_thread_ref (ret);
GST_OBJECT_UNLOCK (context);
return ret;
}
/**
* gst_gl_context_get_gl_api:
* @context: a #GstGLContext
*
* Get the currently enabled OpenGL api.
*
* The currently available API may be limited by the #GstGLDisplay in use and/or
* the #GstGLWindow chosen.
*
* Returns: the available OpenGL api
*
* Since: 1.4
*/
GstGLAPI
gst_gl_context_get_gl_api (GstGLContext * context)
{
GstGLContextClass *context_class;
g_return_val_if_fail (GST_IS_GL_CONTEXT (context), GST_GL_API_NONE);
context_class = GST_GL_CONTEXT_GET_CLASS (context);
g_return_val_if_fail (context_class->get_gl_api != NULL, GST_GL_API_NONE);
return context_class->get_gl_api (context);
}
/**
* gst_gl_context_get_proc_address:
* @context: a #GstGLContext
* @name: an opengl function name
*
* Get a function pointer to a specified opengl function, @name. If the the
* specific function does not exist, NULL is returned instead.
*
* Platform specific functions (names starting 'egl', 'glX', 'wgl', etc) can also
* be retrieved using this method.
*
* Note: This function may return valid function pointers that may not be valid
* to call in @context. The caller is responsible for ensuring that the
* returned function is a valid function to call in @context by either checking
* the OpenGL API and version or for an appropriate OpenGL extension.
*
* Note: On success, you need to cast the returned function pointer to the
* correct type to be able to call it correctly. On 32-bit Windows, this will
* include the `GSTGLAPI` identifier to use the correct calling convention.
* e.g.
*
* |[<!-- language="C" -->
* void (GSTGLAPI *PFN_glGetIntegerv) (GLenum name, GLint * ret)
* ]|
*
* Returns: a function pointer or %NULL
*
* Since: 1.4
*/
gpointer
gst_gl_context_get_proc_address (GstGLContext * context, const gchar * name)
{
gpointer ret;
GstGLContextClass *context_class;
GstGLAPI gl_api;
g_return_val_if_fail (GST_IS_GL_CONTEXT (context), NULL);
context_class = GST_GL_CONTEXT_GET_CLASS (context);
g_return_val_if_fail (context_class->get_proc_address != NULL, NULL);
gl_api = gst_gl_context_get_gl_api (context);
ret = context_class->get_proc_address (gl_api, name);
return ret;
}
/**
* gst_gl_context_default_get_proc_address:
* @gl_api: a #GstGLAPI
* @name: then function to get the address of
*
* A default implementation of the various GetProcAddress functions that looks
* for @name in the OpenGL shared libraries or in the current process.
*
* See also: gst_gl_context_get_proc_address()
*
* Returns: an address pointing to @name or %NULL
*
* Since: 1.4
*/
gpointer
gst_gl_context_default_get_proc_address (GstGLAPI gl_api, const gchar * name)
{
gpointer ret = NULL;
/* First try to load symbol from the selected GL API for this context */
#if GST_GL_HAVE_GLES2
if (!ret && (gl_api & GST_GL_API_GLES2)) {
g_once (&module_gles2_gonce, load_gles2_module, NULL);
if (module_gles2)
g_module_symbol (module_gles2, name, &ret);
}
#endif
#if GST_GL_HAVE_OPENGL
if (!ret && (gl_api & (GST_GL_API_OPENGL | GST_GL_API_OPENGL3))) {
g_once (&module_opengl_gonce, load_opengl_module, NULL);
if (module_opengl)
g_module_symbol (module_opengl, name, &ret);
}
#endif
/* Otherwise fall back to the current module */
g_once (&module_self_gonce, load_self_module, NULL);
if (!ret)
g_module_symbol (module_self, name, &ret);
return ret;
}
/**
* gst_gl_context_set_window:
* @context: a #GstGLContext
* @window: (transfer full): a #GstGLWindow
*
* Set's the current window on @context to @window. The window can only be
* changed before gst_gl_context_create() has been called and the @window is not
* already running.
*
* Returns: Whether the window was successfully updated
*
* Since: 1.4
*/
gboolean
gst_gl_context_set_window (GstGLContext * context, GstGLWindow * window)
{
g_return_val_if_fail (!GST_IS_GL_WRAPPED_CONTEXT (context), FALSE);
GST_DEBUG_OBJECT (context, "window:%" GST_PTR_FORMAT, window);
/* we can't change the window while we are running */
if (context->priv->alive)
return FALSE;
if (window)
g_weak_ref_set (&window->context_ref, context);
if (context->window)
gst_object_unref (context->window);
context->window = window ? gst_object_ref (window) : NULL;
return TRUE;
}
/**
* gst_gl_context_get_window:
* @context: a #GstGLContext
*
* Returns: (transfer full) (nullable): the currently set window
*
* Since: 1.4
*/
GstGLWindow *
gst_gl_context_get_window (GstGLContext * context)
{
g_return_val_if_fail (GST_IS_GL_CONTEXT (context), NULL);
if (GST_IS_GL_WRAPPED_CONTEXT (context)) {
GST_WARNING_OBJECT (context, "context is not toplevel, returning NULL");
return NULL;
}
_ensure_window (context);
return gst_object_ref (context->window);
}
/**
* gst_gl_context_can_share:
* @context: a #GstGLContext
* @other_context: another #GstGLContext
*
* Note: This will always fail for two wrapped #GstGLContext's
*
* Returns: whether @context and @other_context are able to share OpenGL
* resources.
*
* Since: 1.6
*/
gboolean