[Cogl] [PATCH v2] Add conf vars to trick Cogl to think extensions are disabled

Neil Roberts neil at linux.intel.com
Tue Jul 31 08:30:33 PDT 2012


Here is a second attempt at the patch with the two requests from Rob.

The variables are no longer conditionally compiled on COGL_DEBUG so
they are always available. They have been renamed without the “DEBUG”
part because they are no longer just for debugging.

Both variables can now also be set in cogl.conf. If extensions are
disabled from both cogl.conf and the environment variable then the
union of the two lists is used.

Regards,
- Neil

-- >8 --

This adds two new configuration environment variables:

COGL_DISABLE_GL_EXTENSIONS and
COGL_OVERRIDE_GL_VERSION

The variables can also be set in the cogl.conf file using the same
names.

The first one is a list of GL extension names separated by commas.
When set Cogl will assume any extension listed here is not available
by removing it from the string returned from
glGetString(GL_EXTENSIONS). If the string is set in both the config
file and the environment variable then the union of the two lists will
be used.

The second overrides the value returned from glGetString(GL_VERSION).
If the string is set in both places the version from the environment
variable will take priority.

These are sometimes useful for debugging Cogl to test the various
combinations of extensions. It could also be useful to work around
driver bugs where an extension is badly supported and it would be
better not to use it.

The variables in cogl-config that just set a global char * variable
have been put together in an array instead of having a separate blob
of code for each one in order to make it simpler to add new variables.
---
 cogl/cogl-config-private.h    |   2 +
 cogl/cogl-config.c            |  39 ++++++++++------
 cogl/cogl-context-private.h   |   6 +++
 cogl/cogl-context.c           | 105 ++++++++++++++++++++++++++++++++++++++++++
 cogl/cogl-debug.c             |  10 ++++
 cogl/cogl-gpu-info.c          |   2 +-
 cogl/driver/gl/cogl-gl.c      |  10 ++--
 cogl/driver/gles/cogl-gles.c  |   6 +--
 cogl/winsys/cogl-winsys-wgl.c |   2 +-
 9 files changed, 158 insertions(+), 24 deletions(-)

diff --git a/cogl/cogl-config-private.h b/cogl/cogl-config-private.h
index 14b43bb..c946d54 100644
--- a/cogl/cogl-config-private.h
+++ b/cogl/cogl-config-private.h
@@ -33,5 +33,7 @@ _cogl_config_read (void);
 
 extern char *_cogl_config_driver;
 extern char *_cogl_config_renderer;
+extern char *_cogl_config_disable_gl_extensions;
+extern char *_cogl_config_override_gl_version;
 
 #endif /* __COGL_CONFIG_PRIVATE_H */
diff --git a/cogl/cogl-config.c b/cogl/cogl-config.c
index 81cfaa6..a000dd9 100644
--- a/cogl/cogl-config.c
+++ b/cogl/cogl-config.c
@@ -35,11 +35,27 @@
 
 char *_cogl_config_driver;
 char *_cogl_config_renderer;
+char *_cogl_config_disable_gl_extensions;
+char *_cogl_config_override_gl_version;
+
+/* Array of config options that just set a global string */
+static const struct
+{
+  const char *conf_name;
+  char **variable;
+} cogl_config_string_options[] =
+  {
+    { "COGL_DRIVER", &_cogl_config_driver },
+    { "COGL_RENDERER", &_cogl_config_renderer },
+    { "COGL_DISABLE_GL_EXTENSIONS", &_cogl_config_disable_gl_extensions },
+    { "COGL_OVERRIDE_GL_VERSION", &_cogl_config_override_gl_version }
+  };
 
 static void
 _cogl_config_process (GKeyFile *key_file)
 {
   char *value;
+  int i;
 
   value = g_key_file_get_string (key_file, "global", "COGL_DEBUG", NULL);
   if (value)
@@ -59,22 +75,17 @@ _cogl_config_process (GKeyFile *key_file)
       g_free (value);
     }
 
-  value = g_key_file_get_string (key_file, "global", "COGL_DRIVER", NULL);
-  if (value)
+  for (i = 0; i < G_N_ELEMENTS (cogl_config_string_options); i++)
     {
-      if (_cogl_config_driver)
-        g_free (_cogl_config_driver);
-
-      _cogl_config_driver = value;
-    }
+      const char *conf_name = cogl_config_string_options[i].conf_name;
+      char **variable = cogl_config_string_options[i].variable;
 
-  value = g_key_file_get_string (key_file, "global", "COGL_RENDERER", NULL);
-  if (value)
-    {
-      if (_cogl_config_renderer)
-        g_free (_cogl_config_renderer);
-
-      _cogl_config_renderer = value;
+      value = g_key_file_get_string (key_file, "global", conf_name, NULL);
+      if (value)
+        {
+          g_free (*variable);
+          *variable = value;
+        }
     }
 }
 
diff --git a/cogl/cogl-context-private.h b/cogl/cogl-context-private.h
index ffa5da9..0be6000 100644
--- a/cogl/cogl-context-private.h
+++ b/cogl/cogl-context-private.h
@@ -340,4 +340,10 @@ void
 _cogl_context_set_current_modelview_entry (CoglContext *context,
                                            CoglMatrixEntry *entry);
 
+const char *
+_cogl_context_get_gl_extensions (CoglContext *context);
+
+const char *
+_cogl_context_get_gl_version (CoglContext *context);
+
 #endif /* __COGL_CONTEXT_PRIVATE_H */
diff --git a/cogl/cogl-context.c b/cogl/cogl-context.c
index 4973d64..4c959c7 100644
--- a/cogl/cogl-context.c
+++ b/cogl/cogl-context.c
@@ -48,6 +48,7 @@
 #include "cogl-attribute-private.h"
 #include "cogl1-context.h"
 #include "cogl-gpu-info-private.h"
+#include "cogl-config-private.h"
 
 #include <string.h>
 
@@ -602,3 +603,107 @@ _cogl_context_set_current_modelview_entry (CoglContext *context,
     _cogl_matrix_entry_unref (context->current_modelview_entry);
   context->current_modelview_entry = entry;
 }
+
+const char *
+_cogl_context_get_gl_extensions (CoglContext *context)
+{
+  const char *env_disabled_extensions;
+
+  if ((env_disabled_extensions = g_getenv ("COGL_DISABLE_GL_EXTENSIONS"))
+      || _cogl_config_disable_gl_extensions)
+    {
+      static CoglUserDataKey extensions_key;
+      const char *enabled_extensions;
+      char **split_enabled_extensions;
+      char **split_env_disabled_extensions;
+      char **split_conf_disabled_extensions;
+      char **e;
+      GString *result;
+
+      /* We need to return a const string so we'll attach the results
+       * to the CoglContext to avoid leaking the generated string.
+       * This string is only used rarely so we are using
+       * cogl_object_set_user_data instead of adding an explicit
+       * member to CoglContext to avoid making the struct bigger */
+
+      enabled_extensions =
+        cogl_object_get_user_data (COGL_OBJECT (context), &extensions_key);
+      if (enabled_extensions)
+        return enabled_extensions;
+
+      enabled_extensions = (const char *) context->glGetString (GL_EXTENSIONS);
+
+      split_enabled_extensions = g_strsplit (enabled_extensions,
+                                             " ",
+                                             0 /* no max tokens */);
+
+      if (env_disabled_extensions)
+        split_env_disabled_extensions =
+          g_strsplit (env_disabled_extensions,
+                      ",",
+                      0 /* no max tokens */);
+      else
+        split_env_disabled_extensions = NULL;
+
+      if (_cogl_config_disable_gl_extensions)
+        split_conf_disabled_extensions =
+          g_strsplit (_cogl_config_disable_gl_extensions,
+                      ",",
+                      0 /* no max tokens */);
+      else
+        split_conf_disabled_extensions = NULL;
+
+      result = g_string_new (NULL);
+
+      for (e = split_enabled_extensions; *e; e++)
+        {
+          char **d;
+
+          if (split_env_disabled_extensions)
+            for (d = split_env_disabled_extensions; *d; d++)
+              if (!strcmp (*e, *d))
+                goto disabled;
+          if (split_conf_disabled_extensions)
+            for (d = split_conf_disabled_extensions; *d; d++)
+              if (!strcmp (*e, *d))
+                goto disabled;
+
+          if (result->len > 0)
+            g_string_append_c (result, ' ');
+          g_string_append (result, *e);
+
+        disabled:
+          continue;
+        }
+
+      enabled_extensions = g_string_free (result, FALSE);
+
+      g_strfreev (split_enabled_extensions);
+      if (split_env_disabled_extensions)
+        g_strfreev (split_env_disabled_extensions);
+      if (split_conf_disabled_extensions)
+        g_strfreev (split_conf_disabled_extensions);
+
+      cogl_object_set_user_data (COGL_OBJECT (context),
+                                 &extensions_key,
+                                 (void *) enabled_extensions,
+                                 (CoglUserDataDestroyCallback) g_free);
+
+      return enabled_extensions;
+    }
+  else
+    return (const char *) context->glGetString (GL_EXTENSIONS);
+}
+
+const char *
+_cogl_context_get_gl_version (CoglContext *context)
+{
+  const char *version_override;
+
+  if ((version_override = g_getenv ("COGL_OVERRIDE_GL_VERSION")))
+    return version_override;
+  else if (_cogl_config_override_gl_version)
+    return _cogl_config_override_gl_version;
+  else
+    return (const char *) context->glGetString (GL_VERSION);
+}
diff --git a/cogl/cogl-debug.c b/cogl/cogl-debug.c
index 5161b4c..1a7e7e6 100644
--- a/cogl/cogl-debug.c
+++ b/cogl/cogl-debug.c
@@ -182,6 +182,16 @@ _cogl_parse_debug_string (const char *value,
       OPT (IGNORED, "ignored", "verbose", "ignored", \
            N_("Enables all non-behavioural debug options"));
 #undef OPT
+
+      g_printerr ("\n"
+                  "%28s\n"
+                  " COGL_DISABLE_GL_EXTENSIONS: %s\n"
+                  "   COGL_OVERRIDE_GL_VERSION: %s\n",
+                  _("Additional environment variables:"),
+                  _("Comma-separated list of GL extensions to pretend are "
+                    "disabled"),
+                  _("Override the GL version that Cogl will assume the driver "
+                    "supports"));
       exit (1);
     }
   else
diff --git a/cogl/cogl-gpu-info.c b/cogl/cogl-gpu-info.c
index 53e5bb2..8e64d04 100644
--- a/cogl/cogl-gpu-info.c
+++ b/cogl/cogl-gpu-info.c
@@ -482,7 +482,7 @@ _cogl_gpu_info_init (CoglContext *ctx,
   int i;
 
   strings.renderer_string = (const char *) ctx->glGetString (GL_RENDERER);
-  strings.version_string = (const char *) ctx->glGetString (GL_VERSION);
+  strings.version_string = _cogl_context_get_gl_version (ctx);
   strings.vendor_string = (const char *) ctx->glGetString (GL_VENDOR);
 
   /* Determine the driver package */
diff --git a/cogl/driver/gl/cogl-gl.c b/cogl/driver/gl/cogl-gl.c
index ce0c9bc..0f833eb 100644
--- a/cogl/driver/gl/cogl-gl.c
+++ b/cogl/driver/gl/cogl-gl.c
@@ -255,7 +255,7 @@ _cogl_get_gl_version (CoglContext *ctx,
   int major = 0, minor = 0;
 
   /* Get the OpenGL version number */
-  if ((version_string = (const char *) ctx->glGetString (GL_VERSION)) == NULL)
+  if ((version_string = _cogl_context_get_gl_version (ctx)) == NULL)
     return FALSE;
 
   /* Extract the major number */
@@ -303,7 +303,7 @@ check_gl_version (CoglContext *ctx,
   if (COGL_CHECK_GL_VERSION (major, minor, 1, 3))
     return TRUE;
 
-  gl_extensions = (const char*) ctx->glGetString (GL_EXTENSIONS);
+  gl_extensions = _cogl_context_get_gl_extensions (ctx);
 
   /* OpenGL 1.2 is only supported if we have the multitexturing
      extension */
@@ -362,8 +362,8 @@ _cogl_driver_update_features (CoglContext *ctx,
              "  GL_EXTENSIONS: %s",
              ctx->glGetString (GL_VENDOR),
              ctx->glGetString (GL_RENDERER),
-             ctx->glGetString (GL_VERSION),
-             ctx->glGetString (GL_EXTENSIONS));
+             _cogl_context_get_gl_version (ctx),
+             _cogl_context_get_gl_extensions (ctx));
 
   _cogl_get_gl_version (ctx, &gl_major, &gl_minor);
 
@@ -379,7 +379,7 @@ _cogl_driver_update_features (CoglContext *ctx,
   if (COGL_CHECK_GL_VERSION (gl_major, gl_minor, 1, 4))
     COGL_FLAGS_SET (ctx->features, COGL_FEATURE_ID_MIRRORED_REPEAT, TRUE);
 
-  gl_extensions = (const char *)ctx->glGetString (GL_EXTENSIONS);
+  gl_extensions = _cogl_context_get_gl_extensions (ctx);
 
   _cogl_feature_check_ext_functions (ctx,
                                      gl_major,
diff --git a/cogl/driver/gles/cogl-gles.c b/cogl/driver/gles/cogl-gles.c
index 8f1e91d..d6a8feb 100644
--- a/cogl/driver/gles/cogl-gles.c
+++ b/cogl/driver/gles/cogl-gles.c
@@ -218,12 +218,12 @@ _cogl_driver_update_features (CoglContext *context,
              "  GL_EXTENSIONS: %s",
              context->glGetString (GL_VENDOR),
              context->glGetString (GL_RENDERER),
-             context->glGetString (GL_VERSION),
-             context->glGetString (GL_EXTENSIONS));
+             _cogl_context_get_gl_version (context),
+             _cogl_context_get_gl_extensions (context));
 
   _cogl_gpu_info_init (context, &context->gpu);
 
-  gl_extensions = (const char*) context->glGetString (GL_EXTENSIONS);
+  gl_extensions = _cogl_context_get_gl_extensions (context);
 
   _cogl_feature_check_ext_functions (context,
                                      -1 /* GL major version */,
diff --git a/cogl/winsys/cogl-winsys-wgl.c b/cogl/winsys/cogl-winsys-wgl.c
index 61cd335..418e728 100644
--- a/cogl/winsys/cogl-winsys-wgl.c
+++ b/cogl/winsys/cogl-winsys-wgl.c
@@ -580,7 +580,7 @@ get_wgl_extensions_string (HDC dc)
      extensions isn't supported then we can at least fake it to
      support the swap control extension */
   if (_cogl_check_extension ("WGL_EXT_swap_control",
-                             (char *) ctx->glGetString (GL_EXTENSIONS)))
+                             _cogl_context_get_gl_extensions (ctx)))
     return "WGL_EXT_swap_control";
 
   return NULL;
-- 
1.7.11.3.g3c3efa5



More information about the Cogl mailing list