Blame gst-libs/gst/gl/gstglfeature.c

Packit 971217
/*
Packit 971217
 * GStreamer
Packit 971217
 * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <string.h>
Packit 971217
Packit 971217
#include "gstglfeature.h"
Packit 971217
Packit 971217
#include "gstglcontext.h"
Packit 971217
#include "gstglfeature_private.h"
Packit 971217
#include "gstglfuncs.h"
Packit 971217
Packit 971217
#define GST_CAT_DEFAULT gl_feature
Packit 971217
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
Packit 971217
Packit 971217
static void
Packit 971217
_init_debug (void)
Packit 971217
{
Packit 971217
  static volatile gsize _init = 0;
Packit 971217
Packit 971217
  if (g_once_init_enter (&_init)) {
Packit 971217
    GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glfeature", 0,
Packit 971217
        "OpenGL feature detection");
Packit 971217
    g_once_init_leave (&_init, 1);
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_check_extension:
Packit 971217
 * @name: the extension to search for
Packit 971217
 * @ext: the list of possible extensions
Packit 971217
 *
Packit 971217
 * Returns: whether @name is in the space seperated list of @ext
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_gl_check_extension (const char *name, const gchar * ext)
Packit 971217
{
Packit 971217
  char *end;
Packit 971217
  int name_len, n;
Packit 971217
Packit 971217
  if (name == NULL || ext == NULL)
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  end = (char *) (ext + strlen (ext));
Packit 971217
Packit 971217
  name_len = strlen (name);
Packit 971217
Packit 971217
  while (ext < end) {
Packit 971217
    n = strcspn (ext, " ");
Packit 971217
Packit 971217
    if ((name_len == n) && (!strncmp (name, ext, n)))
Packit 971217
      return TRUE;
Packit 971217
    ext += (n + 1);
Packit 971217
  }
Packit 971217
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
/* Define a set of arrays containing the functions required from GL
Packit 971217
   for each feature */
Packit 971217
#define GST_GL_EXT_BEGIN(name,                                            \
Packit 971217
                       gl_availability,                                 \
Packit 971217
                       min_gl_major, min_gl_minor,                      \
Packit 971217
                       min_gles_major, min_gles_minor,                  \
Packit 971217
                       namespaces, extension_names)                     \
Packit 971217
  static const GstGLFeatureFunction gst_gl_ext_ ## name ## _funcs[] = {
Packit 971217
#define GST_GL_EXT_FUNCTION(ret, name, args)                          \
Packit 971217
  { G_STRINGIFY (name), G_STRUCT_OFFSET (GstGLFuncs, name) },
Packit 971217
#define GST_GL_EXT_END()                      \
Packit 971217
  { NULL, 0 },                                  \
Packit 971217
  };
Packit 971217
#include "glprototypes/all_functions.h"
Packit 971217
Packit 971217
#undef GST_GL_EXT_BEGIN
Packit 971217
#undef GST_GL_EXT_FUNCTION
Packit 971217
#undef GST_GL_EXT_END
Packit 971217
Packit 971217
#define GST_GL_EXT_BEGIN(name,                                          \
Packit 971217
                       gl_availability,                                 \
Packit 971217
                       min_gl_major, min_gl_minor,                      \
Packit 971217
                       min_gles_major, min_gles_minor,                  \
Packit 971217
                       namespaces, extension_names)                     \
Packit 971217
  { G_STRINGIFY (name), gl_availability, min_gl_major, min_gl_minor, min_gles_major,        \
Packit 971217
    min_gles_minor, namespaces, extension_names,                        \
Packit 971217
    gst_gl_ext_ ## name ## _funcs },
Packit 971217
#define GST_GL_EXT_FUNCTION(ret, name, args)
Packit 971217
#define GST_GL_EXT_END()
Packit 971217
Packit 971217
static const GstGLFeatureData gst_gl_feature_ext_functions_data[] = {
Packit 971217
#include "glprototypes/all_functions.h"
Packit 971217
};
Packit 971217
Packit 971217
#undef GST_GL_EXT_BEGIN
Packit 971217
#undef GST_GL_EXT_FUNCTION
Packit 971217
#undef GST_GL_EXT_END
Packit 971217
Packit 971217
static gboolean
Packit 971217
_gst_gl_feature_check_for_extension (const GstGLFeatureData * data,
Packit 971217
    const char *driver_prefix, const char *extensions_string,
Packit 971217
    const char **suffix)
Packit 971217
{
Packit 971217
  const char *namespace, *namespace_suffix;
Packit 971217
  unsigned int namespace_len;
Packit 971217
Packit 971217
  g_return_val_if_fail (suffix != NULL, FALSE);
Packit 971217
Packit 971217
  for (namespace = data->namespaces; *namespace;
Packit 971217
      namespace += strlen (namespace) + 1) {
Packit 971217
    const char *extension;
Packit 971217
    GString *full_extension_name = g_string_new ("");
Packit 971217
Packit 971217
    /* If the namespace part contains a ':' then the suffix for
Packit 971217
       the function names is different from the name space */
Packit 971217
    if ((namespace_suffix = strchr (namespace, ':'))) {
Packit 971217
      namespace_len = namespace_suffix - namespace;
Packit 971217
      namespace_suffix++;
Packit 971217
    } else {
Packit 971217
      namespace_len = strlen (namespace);
Packit 971217
      namespace_suffix = namespace;
Packit 971217
    }
Packit 971217
Packit 971217
    for (extension = data->extension_names; *extension;
Packit 971217
        extension += strlen (extension) + 1) {
Packit 971217
      g_string_assign (full_extension_name, driver_prefix);
Packit 971217
      g_string_append_c (full_extension_name, '_');
Packit 971217
      g_string_append_len (full_extension_name, namespace, namespace_len);
Packit 971217
      g_string_append_c (full_extension_name, '_');
Packit 971217
      g_string_append (full_extension_name, extension);
Packit 971217
Packit 971217
      if (gst_gl_check_extension (full_extension_name->str, extensions_string)) {
Packit 971217
        GST_TRACE ("found %s in extension string", full_extension_name->str);
Packit 971217
        break;
Packit 971217
      }
Packit 971217
    }
Packit 971217
Packit 971217
    g_string_free (full_extension_name, TRUE);
Packit 971217
Packit 971217
    /* If we found an extension with this namespace then use it
Packit 971217
       as the suffix */
Packit 971217
    if (*extension) {
Packit 971217
      *suffix = namespace_suffix;
Packit 971217
      return TRUE;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
gboolean
Packit 971217
_gst_gl_feature_check (GstGLContext * context,
Packit 971217
    const char *driver_prefix,
Packit 971217
    const GstGLFeatureData * data,
Packit 971217
    int gl_major, int gl_minor, const char *extensions_string)
Packit 971217
{
Packit 971217
  char *full_function_name = NULL;
Packit 971217
  gboolean in_core = FALSE;
Packit 971217
  const char *suffix = NULL;
Packit 971217
  int func_num;
Packit 971217
  GstGLFuncs *gst_gl = context->gl_vtable;
Packit 971217
  guint gl_min = 0, gl_maj = 0;
Packit 971217
  GstGLAPI gl_api = gst_gl_context_get_gl_api (context);
Packit 971217
Packit 971217
  if (gl_api & (GST_GL_API_OPENGL | GST_GL_API_OPENGL3)) {
Packit 971217
    gl_maj = data->min_gl_major;
Packit 971217
    gl_min = data->min_gl_minor;
Packit 971217
  } else if (gl_api & (GST_GL_API_GLES1 | GST_GL_API_GLES2)) {
Packit 971217
    gl_maj = data->min_gles_major;
Packit 971217
    gl_min = data->min_gles_minor;
Packit 971217
  }
Packit 971217
Packit 971217
  GST_DEBUG ("%s, 0x%x, %d.%d vs 0x%x, %d.%d", data->feature_name,
Packit 971217
      data->gl_availability, gl_maj, gl_min,
Packit 971217
      gst_gl_context_get_gl_api (context), gl_major, gl_minor);
Packit 971217
Packit 971217
  /* First check whether the functions should be directly provided by
Packit 971217
     GL */
Packit 971217
  if (gst_gl_context_check_gl_version (context, data->gl_availability, gl_maj,
Packit 971217
          gl_min)) {
Packit 971217
    in_core = TRUE;
Packit 971217
    suffix = "";
Packit 971217
  } else {
Packit 971217
    /* Otherwise try all of the extensions */
Packit 971217
    if (!_gst_gl_feature_check_for_extension (data, driver_prefix,
Packit 971217
            extensions_string, &suffix))
Packit 971217
      goto error;
Packit 971217
  }
Packit 971217
Packit 971217
  /* If we couldn't find anything that provides the functions then
Packit 971217
     give up */
Packit 971217
  if (suffix == NULL)
Packit 971217
    goto error;
Packit 971217
Packit 971217
  /* Try to get all of the entry points */
Packit 971217
  for (func_num = 0; data->functions[func_num].name; func_num++) {
Packit 971217
    void *func;
Packit 971217
Packit 971217
    g_free (full_function_name);
Packit 971217
Packit 971217
    full_function_name = g_strconcat ("gl", data->functions[func_num].name,
Packit 971217
        suffix, NULL);
Packit 971217
    GST_TRACE ("%s should %sbe in core", full_function_name,
Packit 971217
        in_core ? "" : "not ");
Packit 971217
    func = gst_gl_context_get_proc_address (context, full_function_name);
Packit 971217
Packit 971217
    if (func == NULL && in_core) {
Packit 971217
      GST_TRACE ("%s was not found in core, trying the extension version",
Packit 971217
          full_function_name);
Packit 971217
      if (!_gst_gl_feature_check_for_extension (data, driver_prefix,
Packit 971217
              extensions_string, &suffix)) {
Packit 971217
        goto error;
Packit 971217
      } else {
Packit 971217
        g_free (full_function_name);
Packit 971217
        full_function_name = g_strconcat ("gl", data->functions[func_num].name,
Packit 971217
            suffix, NULL);
Packit 971217
        func = gst_gl_context_get_proc_address (context, full_function_name);
Packit 971217
      }
Packit 971217
    }
Packit 971217
Packit 971217
    if (func == NULL) {
Packit 971217
      goto error;
Packit 971217
    }
Packit 971217
Packit 971217
    /* Set the function pointer in the context */
Packit 971217
    *(void **) ((guint8 *) gst_gl +
Packit 971217
        data->functions[func_num].pointer_offset) = func;
Packit 971217
Packit 971217
  }
Packit 971217
Packit 971217
  g_free (full_function_name);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
Packit 971217
  /* If the extension isn't found or one of the functions wasn't found
Packit 971217
   * then set all of the functions pointers to NULL so we can safely
Packit 971217
   * do feature testing by just looking at the function pointers */
Packit 971217
error:
Packit 971217
  GST_DEBUG ("failed to find feature %s", data->feature_name);
Packit 971217
Packit 971217
  for (func_num = 0; data->functions[func_num].name; func_num++) {
Packit 971217
    *(void **) ((guint8 *) gst_gl +
Packit 971217
        data->functions[func_num].pointer_offset) = NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  if (full_function_name) {
Packit 971217
    GST_DEBUG ("failed to find function %s", full_function_name);
Packit 971217
    g_free (full_function_name);
Packit 971217
  }
Packit 971217
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
void
Packit 971217
_gst_gl_feature_check_ext_functions (GstGLContext * context,
Packit 971217
    int gl_major, int gl_minor, const char *gl_extensions)
Packit 971217
{
Packit 971217
  int i;
Packit 971217
Packit 971217
  _init_debug ();
Packit 971217
Packit 971217
  for (i = 0; i < G_N_ELEMENTS (gst_gl_feature_ext_functions_data); i++) {
Packit 971217
    _gst_gl_feature_check (context, "GL",
Packit 971217
        gst_gl_feature_ext_functions_data + i, gl_major, gl_minor,
Packit 971217
        gl_extensions);
Packit 971217
  }
Packit 971217
}