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.

Signed-off-by: Neil Roberts <neil@linux.intel.com>
This commit is contained in:
Robert Bragg 2011-06-14 22:33:44 +01:00
parent e5c4c1ce7c
commit a1234ee8d1
8 changed files with 47 additions and 9 deletions

View File

@ -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)

View File

@ -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)
{

View File

@ -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__ */

View File

@ -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);

View File

@ -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__ */

View File

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

View File

@ -1088,3 +1088,17 @@ _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))
{
g_type_init ();
_cogl_debug_check_environment ();
g_once_init_leave (&init_status, 1);
}
}

View File

@ -223,6 +223,8 @@ static ANativeWindow *android_native_window;
void
cogl_android_set_native_window (ANativeWindow *window)
{
_cogl_init ();
android_native_window = window;
}
#endif