gstglcontext.c 50.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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.
 */

21 22 23 24 25 26 27 28 29 30 31
/**
 * 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.
 */

32 33 34 35
#if HAVE_CONFIG_H
# include "config.h"
#endif

36 37 38 39 40 41 42
#if defined(ANDROID) || defined(__ANDROID__)
/* Avoid a linker error with _isoc99_sscanf() when building a shared library
 * for android
 */
#define _GNU_SOURCE
#endif

43
#include "gstglcontext.h"
44
#include <gst/gl/gl.h>
45

46
#include <gmodule.h>
47 48
#include <string.h>
#include <stdio.h>
49

50
#include "gstglcontext_private.h"
51
#include "gstglfeature.h"
52
#include "gstglfeature_private.h"
53
#include "gstglfuncs.h"
54

55 56 57 58
#ifndef GL_NUM_EXTENSIONS
#define GL_NUM_EXTENSIONS 0x0000821d
#endif

59 60 61 62
#if GST_GL_HAVE_PLATFORM_GLX
#include "x11/gstglcontext_glx.h"
#endif
#if GST_GL_HAVE_PLATFORM_EGL
63
#include "egl/gstglcontext_egl.h"
64
#endif
65
#if GST_GL_HAVE_PLATFORM_CGL
66 67 68 69 70
#include "cocoa/gstglcontext_cocoa.h"
#endif
#if GST_GL_HAVE_PLATFORM_WGL
#include "win32/gstglcontext_wgl.h"
#endif
71 72 73
#if GST_GL_HAVE_PLATFORM_EAGL
#include "eagl/gstglcontext_eagl.h"
#endif
74

75
extern void _gst_gl_debug_enable (GstGLContext * context);
76

77 78
static GPrivate current_context_key;

79
static GModule *module_self;
80
static GOnce module_self_gonce = G_ONCE_INIT;
81 82 83 84 85 86 87 88 89 90 91 92 93

#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 */
94
  if (g_strcmp0 (G_MODULE_SUFFIX, "so") == 0)
95
    module_opengl = g_module_open ("libGL.so.1", G_MODULE_BIND_LAZY);
96 97 98 99

  /* This automatically handles the suffix and even .la files */
  if (!module_opengl)
    module_opengl = g_module_open ("libGL", G_MODULE_BIND_LAZY);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#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 */
119
  if (g_strcmp0 (G_MODULE_SUFFIX, "so") == 0)
120
    module_gles2 = g_module_open ("libGLESv2.so.2", G_MODULE_BIND_LAZY);
121 122 123 124 125

  /* This automatically handles the suffix and even .la files */
  if (!module_gles2)
    module_gles2 = g_module_open ("libGLESv2", G_MODULE_BIND_LAZY);

126 127 128 129 130 131
#endif

  return NULL;
}
#endif

132 133 134 135 136 137 138 139
static gpointer
load_self_module (gpointer user_data)
{
  module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);

  return NULL;
}

140 141 142 143
/* 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.
144 145 146
 *
 * 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.
147 148 149
 *
 * 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 requries this yet.
150
 */
151 152 153 154
struct ContextShareGroup
{
  volatile int refcount;
};
155

156 157
static struct ContextShareGroup *
_context_share_group_new (void)
158
{
159
  struct ContextShareGroup *ret = g_new0 (struct ContextShareGroup, 1);
160

161
  ret->refcount = 1;
162

163 164
  return ret;
}
165

166 167 168 169 170 171
static struct ContextShareGroup *
_context_share_group_ref (struct ContextShareGroup *share)
{
  g_atomic_int_inc (&share->refcount);
  return share;
}
172

173 174 175 176 177 178 179 180 181 182 183
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;
184 185
}

186 187
#define GST_CAT_DEFAULT gst_gl_context_debug
GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
188
GST_DEBUG_CATEGORY_STATIC (gst_gl_debug);
189 190

#define gst_gl_context_parent_class parent_class
191
G_DEFINE_ABSTRACT_TYPE (GstGLContext, gst_gl_context, GST_TYPE_OBJECT);
192 193

#define GST_GL_CONTEXT_GET_PRIVATE(o) \
194
  (G_TYPE_INSTANCE_GET_PRIVATE((o), GST_TYPE_GL_CONTEXT, GstGLContextPrivate))
195

196 197
static void _init_debug (void);

198 199
static gpointer gst_gl_context_create_thread (GstGLContext * context);
static void gst_gl_context_finalize (GObject * object);
200 201
static void gst_gl_context_default_get_gl_platform_version (GstGLContext *
    context, gint * major, gint * minor);
202 203 204 205

struct _GstGLContextPrivate
{
  GThread *gl_thread;
206
  GThread *active_thread;
207 208 209 210

  /* conditions */
  GMutex render_lock;
  GCond create_cond;
211
  GCond destroy_cond;
212 213 214 215

  gboolean created;
  gboolean alive;

216
  GWeakRef other_context_ref;
217
  struct ContextShareGroup *sharegroup;
218
  GError **error;
219 220 221

  gint gl_major;
  gint gl_minor;
222 223

  gchar *gl_exts;
224 225
};

226 227 228 229 230 231 232 233 234 235 236 237 238 239
typedef struct
{
  GstGLContext parent;

  guintptr handle;
  GstGLPlatform platform;
  GstGLAPI available_apis;
} GstGLWrappedContext;

typedef struct
{
  GstGLContextClass parent;
} GstGLWrappedContextClass;

240
#define GST_TYPE_GL_WRAPPED_CONTEXT (gst_gl_wrapped_context_get_type())
241
static GType gst_gl_wrapped_context_get_type (void);
242
G_DEFINE_TYPE (GstGLWrappedContext, gst_gl_wrapped_context,
243
    GST_TYPE_GL_CONTEXT);
244

245 246 247 248 249
#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))
250

251 252 253 254 255 256 257 258 259 260 261 262 263 264
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;

265
  window = gst_gl_display_create_window (context->display);
266 267

  gst_gl_context_set_window (context, window);
268 269

  gst_object_unref (window);
270 271 272 273 274 275 276
}

static void
gst_gl_context_init (GstGLContext * context)
{
  context->priv = GST_GL_CONTEXT_GET_PRIVATE (context);

277
  context->window = NULL;
278 279
  context->gl_vtable = g_slice_alloc0 (sizeof (GstGLFuncs));

280 281 282
  g_mutex_init (&context->priv->render_lock);

  g_cond_init (&context->priv->create_cond);
283
  g_cond_init (&context->priv->destroy_cond);
284
  context->priv->created = FALSE;
285 286

  g_weak_ref_init (&context->priv->other_context_ref, NULL);
287 288 289 290 291 292 293
}

static void
gst_gl_context_class_init (GstGLContextClass * klass)
{
  g_type_class_add_private (klass, sizeof (GstGLContextPrivate));

294 295
  klass->get_proc_address =
      GST_DEBUG_FUNCPTR (gst_gl_context_default_get_proc_address);
296 297
  klass->get_gl_platform_version =
      GST_DEBUG_FUNCPTR (gst_gl_context_default_get_gl_platform_version);
298 299

  G_OBJECT_CLASS (klass)->finalize = gst_gl_context_finalize;
300 301

  _init_debug ();
302 303
}

304 305 306 307 308 309 310 311
static void
_init_debug (void)
{
  static volatile gsize _init = 0;

  if (g_once_init_enter (&_init)) {
    GST_DEBUG_CATEGORY_INIT (gst_gl_context_debug, "glcontext", 0,
        "glcontext element");
312
    GST_DEBUG_CATEGORY_INIT (gst_gl_debug, "gldebug", 0, "OpenGL Debugging");
313 314 315 316
    g_once_init_leave (&_init, 1);
  }
}

317 318 319 320 321 322 323
/**
 * gst_gl_context_new:
 * @display: a #GstGLDisplay
 *
 * Create a new #GstGLContext with the specified @display
 *
 * Returns: a new #GstGLContext
324 325
 *
 * Since: 1.4
326
 */
327 328 329 330 331 332
GstGLContext *
gst_gl_context_new (GstGLDisplay * display)
{
  GstGLContext *context = NULL;
  const gchar *user_choice;

333
  _init_debug ();
334 335

  user_choice = g_getenv ("GST_GL_PLATFORM");
336 337
  GST_INFO ("creating a context for display %" GST_PTR_FORMAT
      ", user choice:%s", display, user_choice);
Julien Isorce's avatar
Julien Isorce committed
338
#if GST_GL_HAVE_PLATFORM_CGL
339
  if (!context && (!user_choice || g_strstr_len (user_choice, 3, "cgl")))
340
    context = GST_GL_CONTEXT (gst_gl_context_cocoa_new (display));
Julien Isorce's avatar
Julien Isorce committed
341
#endif
342 343
#if GST_GL_HAVE_PLATFORM_GLX
  if (!context && (!user_choice || g_strstr_len (user_choice, 3, "glx")))
344
    context = GST_GL_CONTEXT (gst_gl_context_glx_new (display));
345
#endif
346
#if GST_GL_HAVE_PLATFORM_EGL
347
  if (!context && (!user_choice || g_strstr_len (user_choice, 3, "egl")))
348
    context = GST_GL_CONTEXT (gst_gl_context_egl_new (display));
349
#endif
350
#if GST_GL_HAVE_PLATFORM_WGL
351
  if (!context && (!user_choice || g_strstr_len (user_choice, 3, "wgl")))
352
    context = GST_GL_CONTEXT (gst_gl_context_wgl_new (display));
353
#endif
354
#if GST_GL_HAVE_PLATFORM_EAGL
355
  if (!context && (!user_choice || g_strstr_len (user_choice, 4, "eagl")))
356
    context = GST_GL_CONTEXT (gst_gl_context_eagl_new (display));
357
#endif
358

359 360 361 362 363 364 365 366
  if (!context) {
    /* subclass returned a NULL context */
    GST_WARNING ("Could not create context. user specified %s",
        user_choice ? user_choice : "(null)");

    return NULL;
  }

367
  context->display = gst_object_ref (display);
368

369 370 371 372
  GST_DEBUG_OBJECT (context,
      "Done creating context for display %" GST_PTR_FORMAT " (user_choice:%s)",
      display, user_choice);

373 374 375
  return context;
}

376 377 378 379 380 381 382
/**
 * 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
 *
383
 * Wraps an existing OpenGL context into a #GstGLContext.
384
 *
Matthew Waters's avatar
Matthew Waters committed
385 386 387 388
 * Note: The caller is responsible for ensuring that the OpenGL context
 * represented by @handle stays alive while the returned #GstGLContext is
 * active.
 *
389
 * Returns: (transfer full): a #GstGLContext wrapping @handle
390 391
 *
 * Since: 1.4
392 393 394 395 396 397 398
 */
GstGLContext *
gst_gl_context_new_wrapped (GstGLDisplay * display, guintptr handle,
    GstGLPlatform context_type, GstGLAPI available_apis)
{
  GstGLContext *context;
  GstGLWrappedContext *context_wrap = NULL;
399
  GstGLContextClass *context_class;
400
  GstGLAPI display_api;
401 402 403

  _init_debug ();

404 405 406 407
  display_api = gst_gl_display_get_gl_api (display);
  g_return_val_if_fail ((display_api & available_apis) != GST_GL_API_NONE,
      NULL);

408
  context_wrap = g_object_new (GST_TYPE_GL_WRAPPED_CONTEXT, NULL);
409
  gst_object_ref_sink (context_wrap);
410 411 412 413 414 415 416 417 418 419

  if (!context_wrap) {
    /* subclass returned a NULL context */
    GST_ERROR ("Could not wrap existing context");

    return NULL;
  }

  context = (GstGLContext *) context_wrap;

420
  context->display = gst_object_ref (display);
421
  context->priv->sharegroup = _context_share_group_new ();
422 423 424 425
  context_wrap->handle = handle;
  context_wrap->platform = context_type;
  context_wrap->available_apis = available_apis;

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  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;
444
    context_class->get_proc_address = gst_gl_context_default_get_proc_address;
445 446 447 448 449 450 451 452 453 454 455 456
  }
#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;
457
    context_class->get_proc_address = gst_gl_context_default_get_proc_address;
458 459 460
  }
#endif

461 462 463 464 465 466
  if (!context_class->get_current_context) {
    /* we don't have API support */
    gst_object_unref (context);
    return NULL;
  }

467 468 469
  return context;
}

470 471
/**
 * gst_gl_context_get_current_gl_context:
472
 * @context_type: a #GstGLPlatform specifying the type of context to retrieve
473 474
 *
 * Returns: The OpenGL context handle current in the calling thread or %NULL
475 476
 *
 * Since: 1.6
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
 */
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)
507
    GST_WARNING ("Could not retrieve current context");
508 509 510 511

  return handle;
}

Matthew Waters's avatar
Matthew Waters committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
/**
 * 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 retreive @name.
 *
 * See also gst_gl_context_get_proc_address().
 *
 * Returns: a function pointer for @name, or %NULL
 *
 * Since: 1.6
 */
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
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;
}

553 554
/**
 * gst_gl_context_get_current_gl_api:
555
 * @platform: the #GstGLPlatform to retrieve the API for
Matthew Waters's avatar
Matthew Waters committed
556 557
 * @major: (out) (allow-none): the major version
 * @minor: (out) (allow-none): the minor version
558
 *
Matthew Waters's avatar
Matthew Waters committed
559
 * If an error occurs, @major and @minor are not modified and %GST_GL_API_NONE is
560 561 562 563
 * returned.
 *
 * Returns: The version supported by the OpenGL context current in the calling
 *          thread or %GST_GL_API_NONE
564 565
 *
 * Since: 1.6
566 567
 */
GstGLAPI
568 569
gst_gl_context_get_current_gl_api (GstGLPlatform platform, guint * major,
    guint * minor)
570
{
571
  const GLubyte *(GSTGLAPI * GetString) (GLenum name);
572
#if GST_GL_HAVE_OPENGL
573
  void (GSTGLAPI * GetIntegerv) (GLenum name, GLuint * n);
574
#endif
575 576
  const gchar *version;
  gint maj, min, n;
577
  GstGLAPI ret = (1U << 31);
578 579 580 581 582

  _init_debug ();

  while (ret != GST_GL_API_NONE) {
    /* FIXME: attempt to delve into the platform specific GetProcAddress */
583 584 585
    GetString =
        gst_gl_context_get_proc_address_with_platform (platform, ret,
        "glGetString");
586
#if GST_GL_HAVE_OPENGL
587
    GetIntegerv =
588 589
        gst_gl_context_get_proc_address_with_platform (platform, ret,
        "glGetIntegerv");
590
#endif
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
    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))) {
631 632
        GLuint context_flags = 0;

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
        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;
}

662 663 664 665 666
static void
gst_gl_context_finalize (GObject * object)
{
  GstGLContext *context = GST_GL_CONTEXT (object);

667 668 669 670
  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);

671
    g_mutex_lock (&context->priv->render_lock);
672
    if (context->priv->alive) {
673
      GST_INFO_OBJECT (context, "send quit gl window loop");
674 675
      gst_gl_window_quit (context->window);

676
      GST_INFO_OBJECT (context, "joining gl thread");
677 678
      while (context->priv->alive)
        g_cond_wait (&context->priv->destroy_cond, &context->priv->render_lock);
679
      GST_INFO_OBJECT (context, "gl thread joined");
680

681 682 683 684
      if (context->priv->gl_thread) {
        g_thread_unref (context->priv->gl_thread);
        context->priv->gl_thread = NULL;
      }
685
    }
686
    g_mutex_unlock (&context->priv->render_lock);
687

688
    gst_gl_window_set_close_callback (context->window, NULL, NULL, NULL);
689
    gst_object_unref (context->window);
690
    context->window = NULL;
691 692
  }

693 694 695 696 697 698 699 700 701 702 703
  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) {
704
    _context_share_group_unref (context->priv->sharegroup);
705 706
    context->priv->sharegroup = NULL;
  }
707

708 709 710 711
  if (context->display) {
    gst_object_unref (context->display);
    context->display = NULL;
  }
712

713 714 715 716 717
  if (context->gl_vtable) {
    g_slice_free (GstGLFuncs, context->gl_vtable);
    context->gl_vtable = NULL;
  }

718 719 720
  g_mutex_clear (&context->priv->render_lock);

  g_cond_clear (&context->priv->create_cond);
721
  g_cond_clear (&context->priv->destroy_cond);
722

723
  g_free (context->priv->gl_exts);
724
  g_weak_ref_clear (&context->priv->other_context_ref);
725

726
  GST_DEBUG_OBJECT (context, "End of finalize");
727 728 729
  G_OBJECT_CLASS (gst_gl_context_parent_class)->finalize (object);
}

730 731 732 733 734 735 736 737 738 739 740
/**
 * 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
741 742
 *
 * Since: 1.4
743
 */
744 745 746
gboolean
gst_gl_context_activate (GstGLContext * context, gboolean activate)
{
747
  GstGLContextClass *context_class;
748 749
  gboolean result;

750
  g_return_val_if_fail (GST_IS_GL_CONTEXT (context), FALSE);
751 752
  context_class = GST_GL_CONTEXT_GET_CLASS (context);
  g_return_val_if_fail (context_class->activate != NULL, FALSE);
753

754 755
  GST_DEBUG_OBJECT (context, "activate:%d", activate);

756
  GST_OBJECT_LOCK (context);
757
  result = context_class->activate (context, activate);
758

759
  if (result && activate) {
760 761 762 763 764 765
    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);
    }

766 767
    g_private_set (&current_context_key, context);
  } else {
768 769 770 771
    if (context->priv->active_thread) {
      g_thread_unref (context->priv->active_thread);
      context->priv->active_thread = NULL;
    }
772 773
    g_private_set (&current_context_key, NULL);
  }
774 775
  GST_OBJECT_UNLOCK (context);

776 777 778
  return result;
}

779 780 781 782
/**
 * gst_gl_context_get_thread:
 * @context: a #GstGLContext
 *
783
 * Returns: (transfer full): The #GThread, @context is current in or NULL
784 785
 *
 * Since: 1.6
786 787 788 789 790 791 792 793
 */
GThread *
gst_gl_context_get_thread (GstGLContext * context)
{
  GThread *ret;

  GST_OBJECT_LOCK (context);
  ret = context->priv->active_thread;
794 795
  if (ret)
    g_thread_ref (ret);
796 797 798 799 800
  GST_OBJECT_UNLOCK (context);

  return ret;
}

801 802 803 804 805 806 807 808 809
/**
 * 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.
 *
810 811 812
 * Returns: the available OpenGL api
 *
 * Since: 1.4
813
 */
814 815 816
GstGLAPI
gst_gl_context_get_gl_api (GstGLContext * context)
{
817
  GstGLContextClass *context_class;
818

819
  g_return_val_if_fail (GST_IS_GL_CONTEXT (context), GST_GL_API_NONE);
820 821
  context_class = GST_GL_CONTEXT_GET_CLASS (context);
  g_return_val_if_fail (context_class->get_gl_api != NULL, GST_GL_API_NONE);
822

823
  return context_class->get_gl_api (context);
824 825
}

826 827 828 829 830 831 832 833 834
/**
 * 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 specfic functions (names starting 'egl', 'glX', 'wgl', etc) can also
835
 * be retrieved using this method.
836
 *
Matthew Waters's avatar
Matthew Waters committed
837 838 839 840 841 842 843 844
 * 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.
845 846 847 848 849
 * e.g.
 *
 * |[<!-- language="C" -->
 * void (GSTGLAPI *PFN_glGetIntegerv) (GLenum name, GLint * ret)
 * ]|
Matthew Waters's avatar
Matthew Waters committed
850 851
 *
 * Returns: a function pointer or %NULL
852 853
 *
 * Since: 1.4
854
 */
855 856 857 858
gpointer
gst_gl_context_get_proc_address (GstGLContext * context, const gchar * name)
{
  gpointer ret;
859
  GstGLContextClass *context_class;
860
  GstGLAPI gl_api;
861

862
  g_return_val_if_fail (GST_IS_GL_CONTEXT (context), NULL);
863 864
  context_class = GST_GL_CONTEXT_GET_CLASS (context);
  g_return_val_if_fail (context_class->get_proc_address != NULL, NULL);
865

866 867
  gl_api = gst_gl_context_get_gl_api (context);
  ret = context_class->get_proc_address (gl_api, name);
868 869 870 871

  return ret;
}

Matthew Waters's avatar
Matthew Waters committed
872 873 874 875 876 877 878 879 880 881 882 883 884 885
/**
 * 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
 */
886
gpointer
887
gst_gl_context_default_get_proc_address (GstGLAPI gl_api, const gchar * name)
888 889
{
  gpointer ret = NULL;
890

891
  /* First try to load symbol from the selected GL API for this context */
892
#if GST_GL_HAVE_GLES2
893 894 895 896 897
  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);
  }
898
#endif
899

900 901 902 903 904
#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);
905
  }
906
#endif
907

908
  /* Otherwise fall back to the current module */
909
  g_once (&module_self_gonce, load_self_module, NULL);
910 911
  if (!ret)
    g_module_symbol (module_self, name, &ret);
912 913 914 915

  return ret;
}

916 917 918 919 920 921 922 923 924 925
/**
 * 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
926 927
 *
 * Since: 1.4
928
 */
929 930 931
gboolean
gst_gl_context_set_window (GstGLContext * context, GstGLWindow * window)
{
932
  g_return_val_if_fail (!GST_IS_GL_WRAPPED_CONTEXT (context), FALSE);
933

934 935
  GST_DEBUG_OBJECT (context, "window:%" GST_PTR_FORMAT, window);

936 937 938 939
  /* we can't change the window while we are running */
  if (context->priv->alive)
    return FALSE;

940
  if (window)
941 942 943 944 945 946 947 948 949 950
    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;
}

951 952 953 954
/**
 * gst_gl_context_get_window:
 * @context: a #GstGLContext
 *
955
 * Returns: (transfer full) (nullable): the currently set window
956 957
 *
 * Since: 1.4
958
 */
959 960 961
GstGLWindow *
gst_gl_context_get_window (GstGLContext * context)
{
962
  g_return_val_if_fail (GST_IS_GL_CONTEXT (context), NULL);
963

964
  if (GST_IS_GL_WRAPPED_CONTEXT (context)) {
965
    GST_WARNING_OBJECT (context, "context is not toplevel, returning NULL");
966
    return NULL;
967
  }
968

969 970 971 972 973
  _ensure_window (context);

  return gst_object_ref (context->window);
}

974 975 976 977 978
/**
 * gst_gl_context_can_share:
 * @context: a #GstGLContext
 * @other_context: another #GstGLContext
 *
979 980
 * Note: This will always fail for two wrapped #GstGLContext's
 *
981 982 983 984 985 986 987 988
 * Returns: whether @context and @other_context are able to share OpenGL
 *      resources.
 *
 * Since: 1.6
 */
gboolean
gst_gl_context_can_share (GstGLContext * context, GstGLContext * other_context)
{
989 990
  g_return_val_if_fail (GST_IS_GL_CONTEXT (context), FALSE);
  g_return_val_if_fail (GST_IS_GL_CONTEXT (other_context), FALSE);
991

992
  /* check if the contexts are descendants or the root nodes are the same */
993 994
  return context->priv->sharegroup != NULL
      && context->priv->sharegroup == other_context->priv->sharegroup;
995 996
}

997 998 999 1000 1001 1002
/**
 * gst_gl_context_create:
 * @context: a #GstGLContext:
 * @other_context: (allow-none): a #GstGLContext to share OpenGL objects with
 * @error: (allow-none): a #GError
 *
Matthew Waters's avatar
Matthew Waters committed
1003 1004 1005
 * 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.
1006 1007 1008 1009 1010 1011 1012
 *
 * If an error occurs, and @error is not %NULL, then error will contain details
 * of the error and %FALSE will be returned.
 *
 * Should only be called once.
 *
 * Returns: whether the context could successfully be created
1013 1014
 *
 * Since: 1.4
1015
 */
1016 1017
gboolean
gst_gl_context_create (GstGLContext * context,
1018
    GstGLContext * other_context, GError ** error)
1019 1020 1021
{
  gboolean alive = FALSE;

1022 1023
  g_return_val_if_fail (GST_IS_GL_CONTEXT (context), FALSE);
  g_return_val_if_fail (!GST_IS_GL_WRAPPED_CONTEXT (context), FALSE);
1024 1025 1026

  GST_DEBUG_OBJECT (context, " other_context:%" GST_PTR_FORMAT, other_context);

1027 1028 1029 1030 1031
  _ensure_window (context);

  g_mutex_lock (&context->priv->render_lock);

  if (!context->priv->created) {
1032
    g_weak_ref_set (&context->priv->other_context_ref, other_context);
1033
    context->priv->error = error;
1034
    if (other_context == NULL)
1035
      context->priv->sharegroup = _context_share_group_new ();
1036
    else
1037 1038
      context->priv->sharegroup =
          _context_share_group_ref (other_context->priv->sharegroup);
1039 1040 1041 1042

    context->priv->gl_thread = g_thread_new ("gstglcontext",
        (GThreadFunc) gst_gl_context_create_thread, context);

1043 1044
    while (!context->priv->created)
      g_cond_wait (&context->priv->create_cond, &context->priv->render_lock);
1045

1046
    GST_INFO_OBJECT (context, "gl thread created");
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
  }

  alive = context->priv->alive;

  g_mutex_unlock (&context->priv->render_lock);

  return alive;
}

static gboolean
1057
_create_context_info (GstGLContext * context, GstGLAPI gl_api, gint * gl_major,
1058 1059 1060
    gint * gl_minor, GError ** error)
{
  const GstGLFuncs *gl;
1061
  guint maj = 0, min = 0;
1062
  GLenum gl_err = GL_NO_ERROR;
1063
  const gchar *opengl_version = NULL;
1064

1065
  gl = context->gl_vtable;
1066

1067
  if (!gl->GetString || !gl->GetString (GL_VERSION)) {
1068 1069 1070 1071 1072
    g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED,
        "glGetString not defined or returned invalid value");
    return FALSE;
  }

1073 1074 1075 1076 1077 1078
  if (!gl->GetString (GL_SHADING_LANGUAGE_VERSION)) {
    g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED,
        "No GL shader support available");
    return FALSE;
  }

1079
  GST_INFO_OBJECT (context, "GL_VERSION: %s",
1080
      GST_STR_NULL ((const gchar *) gl->GetString (GL_VERSION)));
1081 1082
  GST_INFO_OBJECT (context, "GL_SHADING_LANGUAGE_VERSION: %s",
      GST_STR_NULL ((const gchar *)
1083
          gl->GetString (GL_SHADING_LANGUAGE_VERSION)));
1084
  GST_INFO_OBJECT (context, "GL_VENDOR: %s",
1085
      GST_STR_NULL ((const gchar *) gl->GetString (GL_VENDOR)));
1086
  GST_INFO_OBJECT (context, "GL_RENDERER: %s",
1087
      GST_STR_NULL ((const gchar *) gl->GetString (GL_RENDERER)));
1088 1089 1090 1091 1092 1093 1094 1095

  gl_err = gl->GetError ();
  if (gl_err != GL_NO_ERROR) {
    g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_FAILED,
        "glGetString error: 0x%x", gl_err);
    return FALSE;
  }

1096
  opengl_version = (const gchar *) gl->GetString (GL_VERSION);
1097 1098 1099
  if (opengl_version && gl_api & GST_GL_API_GLES2)
    /* gles starts with "OpenGL ES " */
    opengl_version = &opengl_version[10];
1100

1101 1102
  if (opengl_version)
    sscanf (opengl_version, "%d.%d", &maj, &min);
1103 1104

  /* OpenGL > 1.2.0 */
1105 1106 1107 1108 1109 1110
  if (gl_api & GST_GL_API_OPENGL || gl_api & GST_GL_API_OPENGL3) {
    if ((maj < 1) || (maj < 2 && maj >= 1 && min < 2)) {
      g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_OLD_LIBS,
          "OpenGL >= 1.2.0 required, found %u.%u", maj, min);
      return FALSE;
    }
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  }

  if (gl_major)
    *gl_major = maj;
  if (gl_minor)
    *gl_minor = min;

  return TRUE;
}

1121
static GstGLAPI
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
_compiled_api (void)
{
  GstGLAPI ret = GST_GL_API_NONE;

#if GST_GL_HAVE_OPENGL
  ret |= GST_GL_API_OPENGL | GST_GL_API_OPENGL3;
#endif
#if GST_GL_HAVE_GLES2
  ret |= GST_GL_API_GLES2;
#endif

  return ret;
}

1136 1137 1138
static void
_unlock_create_thread (GstGLContext * context)
{
1139
  context->priv->created = TRUE;
1140
  GST_INFO_OBJECT (context, "gl thread running");
1141
  g_cond_signal (&context->priv->create_cond);
1142 1143 1144
  g_mutex_unlock (&context->priv->render_lock);
}

1145
static GString *
1146 1147 1148
_build_extension_string (GstGLContext * context)
{
  const GstGLFuncs *gl = context->gl_vtable;
1149 1150
  GString *ext_g_str = g_string_sized_new (1024);
  const gchar *ext_const_c_str = NULL;
1151 1152
  GLint i = 0;
  GLint n = 0;
1153 1154 1155 1156

  gl->GetIntegerv (GL_NUM_EXTENSIONS, &n);

  for (i = 0; i < n; i++) {
1157 1158 1159
    ext_const_c_str = (const gchar *) gl->GetStringi (GL_EXTENSIONS, i);
    if (ext_const_c_str)
      g_string_append_printf (ext_g_str, "%s ", ext_const_c_str);
1160 1161
  }

1162
  return ext_g_str;
1163 1164
}

1165
//gboolean
1166
//gst_gl_context_create (GstGLContext * context, GstGLContext * other_context, GError ** error)
1167 1168 1169
static gpointer
gst_gl_context_create_thread (GstGLContext * context)
{
1170
  GstGLContextClass *context_class;
1171
  GstGLWindowClass *window_class;
1172
  GstGLAPI compiled_api, user_api, gl_api, display_api;
1173 1174
  gchar *api_string;
  gchar *compiled_api_s;
1175 1176
  gchar *user_api_s;
  gchar *display_api_s;
1177
  const gchar *user_choice;
1178
  GError **error;
1179
  GstGLContext *other_context;
1180

1181 1182
  g_mutex_lock (&context->priv->render_lock);

1183 1184
  GST_DEBUG_OBJECT (context, "Creating thread");

1185
  error = context->priv->error;
1186
  other_context = g_weak_ref_get (&context->priv->other_context_ref);
1187

1188
  context_class = GST_GL_CONTEXT_GET_CLASS (context);
1189 1190
  window_class = GST_GL_WINDOW_GET_CLASS (context->window);

1191
  display_api = gst_gl_display_get_gl_api_unlocked (context->display);
1192 1193 1194 1195 1196 1197 1198
  if (display_api == GST_GL_API_NONE) {
    g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_WRONG_API,
        "Cannot create context with satisfying requested apis "
        "(display has no GL api!)");
    goto failure;
  }

1199
  if (window_class->open) {
1200
    if (!window_class->open (context->window, error)) {
1201
      GST_WARNING_OBJECT (context, "Failed to open window");
1202
      g_assert (error == NULL || *error != NULL);
1203
      goto failure;
1204
    }
1205 1206 1207
  }

  compiled_api = _compiled_api ();
1208
  compiled_api_s = gst_gl_api_to_string (compiled_api);
1209 1210

  user_choice = g_getenv ("GST_GL_API");
1211
  user_api = gst_gl_api_from_string (user_choice);
1212
  user_api_s = gst_gl_api_to_string (user_api);
1213

1214
  display_api_s = gst_gl_api_to_string (display_api);
1215

1216
  if ((user_api & compiled_api & display_api) == GST_GL_API_NONE) {
1217 1218
    g_set_error (error, GST_GL_CONTEXT_ERROR, GST_GL_CONTEXT_ERROR_WRONG_API,
        "Cannot create context with the user requested api (%s).  "