[cogl/wip/example-crate: 1/20] Add internal _cogl_init() function



commit 8b3f73f1b056e32f4cba894ea733d4eee945fd98
Author: Robert Bragg <robert linux intel com>
Date:   Tue Jun 14 22:33:44 2011 +0100

    Add internal _cogl_init() function
    
    This adds a _cogl_init function for Cogl that we expect to be the first
    thing called before anything else is done with Cogl. It's not a public
    API so it's expected that all entry points for Cogl that might be the
    first function used should call _cogl_init().
    
    We currently call _cogl_init() in these functions:
      cogl_renderer_new
      cogl_display_new
      cogl_context_new
      cogl_android_set_native_window
    
    _cogl_init() can be called multiple times, and only the first call has
    any affect.
    
    For example _cogl_init() gives us a place check and parse the COGL_DEBUG
    environment variable.
    
    Since we don't have any need to parse command line arguments (we can
    always get user configuration options from the environment) our init
    function doesn't require argc/argv pointers.
    
    By saying up front that we aren't interested in command line arguments
    that means we can avoid the mess that is GOption based library
    initialization which is extremely fragile due to its lack of dependency
    tracking between modules.

 cogl/cogl-context.c           |    2 ++
 cogl/cogl-debug.c             |   26 +++++++++++++++++---------
 cogl/cogl-debug.h             |    3 +++
 cogl/cogl-display.c           |    3 +++
 cogl/cogl-private.h           |    3 +++
 cogl/cogl-renderer.c          |    3 +++
 cogl/cogl.c                   |   12 ++++++++++++
 cogl/winsys/cogl-winsys-egl.c |    2 ++
 8 files changed, 45 insertions(+), 9 deletions(-)
---
diff --git a/cogl/cogl-context.c b/cogl/cogl-context.c
index ca24d00..837b70d 100644
--- a/cogl/cogl-context.c
+++ b/cogl/cogl-context.c
@@ -111,6 +111,8 @@ cogl_context_new (CoglDisplay *display,
   const CoglWinsysVtable *winsys;
   int i;
 
+  _cogl_init ();
+
 #ifdef COGL_ENABLE_PROFILE
   /* We need to be absolutely sure that uprof has been initialized
    * before calling _cogl_uprof_init. uprof_init (NULL, NULL)
diff --git a/cogl/cogl-debug.c b/cogl/cogl-debug.c
index 46ab5b4..5116348 100644
--- a/cogl/cogl-debug.c
+++ b/cogl/cogl-debug.c
@@ -28,10 +28,10 @@
 #include <stdlib.h>
 #include <glib/gi18n-lib.h>
 
+#include "cogl.h"
+#include "cogl-private.h"
 #include "cogl-debug.h"
 
-#ifdef COGL_ENABLE_DEBUG
-
 /* XXX: If you add a debug option, please also add an option
  * definition to cogl-debug-options.h. This will enable us - for
  * example - to emit a "help" description for the option.
@@ -176,6 +176,7 @@ _cogl_parse_debug_string (const char *value,
     }
 }
 
+#ifdef COGL_ENABLE_DEBUG
 static gboolean
 cogl_arg_debug_cb (const char *key,
                    const char *value,
@@ -209,13 +210,9 @@ static GOptionEntry cogl_args[] = {
   { NULL, },
 };
 
-static gboolean
-pre_parse_hook (GOptionContext  *context,
-                GOptionGroup    *group,
-                gpointer         data,
-                GError         **error)
+void
+_cogl_debug_check_environment (void)
 {
-#ifdef COGL_ENABLE_DEBUG
   const char *env_string;
 
   env_string = g_getenv ("COGL_DEBUG");
@@ -226,11 +223,22 @@ pre_parse_hook (GOptionContext  *context,
                                 FALSE /* don't ignore help */);
       env_string = NULL;
     }
-#endif /* COGL_ENABLE_DEBUG */
+}
+
+static gboolean
+pre_parse_hook (GOptionContext  *context,
+                GOptionGroup    *group,
+                gpointer         data,
+                GError         **error)
+{
+  _cogl_init ();
 
   return TRUE;
 }
 
+/* XXX: GOption based library initialization is not reliable because the
+ * GOption API has no way to represent dependencies between libraries.
+ */
 GOptionGroup *
 cogl_get_option_group (void)
 {
diff --git a/cogl/cogl-debug.h b/cogl/cogl-debug.h
index 292de25..80607ce 100644
--- a/cogl/cogl-debug.h
+++ b/cogl/cogl-debug.h
@@ -116,6 +116,9 @@ extern GHashTable *_cogl_debug_instances;
 
 #endif /* COGL_ENABLE_DEBUG */
 
+void
+_cogl_debug_check_environment (void);
+
 G_END_DECLS
 
 #endif /* __COGL_DEBUG_H__ */
diff --git a/cogl/cogl-display.c b/cogl/cogl-display.c
index aea9d8d..a0e37a2 100644
--- a/cogl/cogl-display.c
+++ b/cogl/cogl-display.c
@@ -29,6 +29,7 @@
 #endif
 
 #include "cogl.h"
+#include "cogl-private.h"
 #include "cogl-object.h"
 
 #include "cogl-display-private.h"
@@ -70,6 +71,8 @@ cogl_display_new (CoglRenderer *renderer,
   CoglDisplay *display = g_slice_new0 (CoglDisplay);
   GError *error = NULL;
 
+  _cogl_init ();
+
   display->renderer = renderer;
   if (renderer)
     cogl_object_ref (renderer);
diff --git a/cogl/cogl-private.h b/cogl/cogl-private.h
index bd09f4e..8db76e1 100644
--- a/cogl/cogl-private.h
+++ b/cogl/cogl-private.h
@@ -48,6 +48,9 @@ _cogl_read_pixels_with_rowstride (int x,
                                   guint8 *pixels,
                                   int rowstride);
 
+void
+_cogl_init (void);
+
 G_END_DECLS
 
 #endif /* __COGL_PRIVATE_H__ */
diff --git a/cogl/cogl-renderer.c b/cogl/cogl-renderer.c
index e4c47c8..06679d4 100644
--- a/cogl/cogl-renderer.c
+++ b/cogl/cogl-renderer.c
@@ -33,6 +33,7 @@
 
 #include "cogl.h"
 #include "cogl-internal.h"
+#include "cogl-private.h"
 #include "cogl-object.h"
 
 #include "cogl-renderer.h"
@@ -112,6 +113,8 @@ cogl_renderer_new (void)
 {
   CoglRenderer *renderer = g_new0 (CoglRenderer, 1);
 
+  _cogl_init ();
+
   renderer->connected = FALSE;
   renderer->event_filters = NULL;
 
diff --git a/cogl/cogl.c b/cogl/cogl.c
index 07cc76d..38cef49 100644
--- a/cogl/cogl.c
+++ b/cogl/cogl.c
@@ -1088,3 +1088,15 @@ _cogl_error_quark (void)
 {
   return g_quark_from_static_string ("cogl-error-quark");
 }
+
+void
+_cogl_init (void)
+{
+  static gsize init_status = 0;
+
+  if (g_once_init_enter (&init_status))
+    {
+      _cogl_debug_check_environment ();
+      g_once_init_leave (&init_status, 1);
+    }
+}
diff --git a/cogl/winsys/cogl-winsys-egl.c b/cogl/winsys/cogl-winsys-egl.c
index 7974ba6..1ae78e3 100644
--- a/cogl/winsys/cogl-winsys-egl.c
+++ b/cogl/winsys/cogl-winsys-egl.c
@@ -223,6 +223,8 @@ static ANativeWindow *android_native_window;
 void
 cogl_android_set_native_window (ANativeWindow *window)
 {
+  _cogl_init ();
+
   android_native_window = window;
 }
 #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]