summaryrefslogtreecommitdiff
path: root/cogl/cogl-renderer.c
diff options
context:
space:
mode:
authorNeil Roberts <neil@linux.intel.com>2011-07-07 20:44:56 +0100
committerNeil Roberts <neil@linux.intel.com>2011-07-11 12:57:38 +0100
commitb2e735ff7f4094056765ff6adb5c74fd51d11e4a (patch)
treefa2d813eef3d138c86208d0a263fb94580059367 /cogl/cogl-renderer.c
parent5f181973a68ba9048f89cbf623ddcdc95ce7c415 (diff)
downloadcogl-b2e735ff7f4094056765ff6adb5c74fd51d11e4a.tar.gz
Dynamically load the GL or GLES library
The GL or GLES library is now dynamically loaded by the CoglRenderer so that it can choose between GL, GLES1 and GLES2 at runtime. The library is loaded by the renderer because it needs to be done before calling eglInitialize. There is a new environment variable called COGL_DRIVER to choose between gl, gles1 or gles2. The #ifdefs for HAVE_COGL_GL, HAVE_COGL_GLES and HAVE_COGL_GLES2 have been changed so that they don't assume the ifdefs are mutually exclusive. They haven't been removed entirely so that it's possible to compile the GLES backends without the the enums from the GL headers. When using GLX the winsys additionally dynamically loads libGL because that also contains the GLX API. It can't be linked in directly because that would probably conflict with the GLES API if the EGL is selected. When compiling with EGL support the library links directly to libEGL because it doesn't contain any GL API so it shouldn't have any conflicts. When building for WGL or OSX Cogl still directly links against the GL API so there is a #define in config.h so that Cogl won't try to dlopen the library. Cogl-pango previously had a #ifdef to detect when the GL backend is used so that it can sneakily pass GL_QUADS to cogl_vertex_buffer_draw. This is now changed so that it queries the CoglContext for the backend. However to get this to work Cogl now needs to export the _cogl_context_get_default symbol and cogl-pango needs some extra -I flags to so that it can include cogl-context-private.h
Diffstat (limited to 'cogl/cogl-renderer.c')
-rw-r--r--cogl/cogl-renderer.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/cogl/cogl-renderer.c b/cogl/cogl-renderer.c
index 026d4abb..8b76a77e 100644
--- a/cogl/cogl-renderer.c
+++ b/cogl/cogl-renderer.c
@@ -100,6 +100,11 @@ _cogl_renderer_free (CoglRenderer *renderer)
const CoglWinsysVtable *winsys = _cogl_renderer_get_winsys (renderer);
winsys->renderer_disconnect (renderer);
+#ifndef HAVE_DIRECTLY_LINKED_GL_LIBRARY
+ if (renderer->libgl_module)
+ g_module_close (renderer->libgl_module);
+#endif
+
g_slist_foreach (renderer->event_filters,
(GFunc) native_filter_closure_free,
NULL);
@@ -166,6 +171,75 @@ cogl_renderer_check_onscreen_template (CoglRenderer *renderer,
return TRUE;
}
+static gboolean
+_cogl_renderer_choose_driver (CoglRenderer *renderer,
+ GError **error)
+{
+ const char *driver_name = g_getenv ("COGL_DRIVER");
+ const char *libgl_name;
+#ifndef HAVE_DIRECTLY_LINKED_GL_LIBRARY
+ char *libgl_module_path;
+#endif
+
+#ifdef HAVE_COGL_GL
+ if (driver_name == NULL || !strcmp (driver_name, "gl"))
+ {
+ renderer->driver = COGL_DRIVER_GL;
+ libgl_name = COGL_GL_LIBNAME;
+ goto found;
+ }
+#endif
+
+#ifdef HAVE_COGL_GLES2
+ if (driver_name == NULL || !strcmp (driver_name, "gles2"))
+ {
+ renderer->driver = COGL_DRIVER_GLES2;
+ libgl_name = COGL_GLES2_LIBNAME;
+ goto found;
+ }
+#endif
+
+#ifdef HAVE_COGL_GLES
+ if (driver_name == NULL || !strcmp (driver_name, "gles1"))
+ {
+ renderer->driver = COGL_DRIVER_GLES1;
+ libgl_name = COGL_GLES1_LIBNAME;
+ goto found;
+ }
+#endif
+
+ g_set_error (error,
+ COGL_DRIVER_ERROR,
+ COGL_DRIVER_ERROR_NO_SUITABLE_DRIVER_FOUND,
+ "No suitable driver found");
+ return FALSE;
+
+ found:
+
+#ifndef HAVE_DIRECTLY_LINKED_GL_LIBRARY
+
+ libgl_module_path = g_module_build_path (NULL, /* standard lib search path */
+ libgl_name);
+
+ renderer->libgl_module = g_module_open (libgl_module_path,
+ G_MODULE_BIND_LAZY);
+
+ g_free (libgl_module_path);
+
+ if (renderer->libgl_module == NULL)
+ {
+ g_set_error (error, COGL_DRIVER_ERROR,
+ COGL_DRIVER_ERROR_FAILED_TO_LOAD_LIBRARY,
+ "Failed to dynamically open the GL library \"%s\"",
+ libgl_name);
+ return FALSE;
+ }
+
+#endif /* HAVE_DIRECTLY_LINKED_GL_LIBRARY */
+
+ return TRUE;
+}
+
/* Final connection API */
gboolean
@@ -177,6 +251,12 @@ cogl_renderer_connect (CoglRenderer *renderer, GError **error)
if (renderer->connected)
return TRUE;
+ /* The driver needs to be chosen before connecting the renderer
+ because eglInitialize requires the library containing the GL API
+ to be loaded before its called */
+ if (!_cogl_renderer_choose_driver (renderer, error))
+ return FALSE;
+
error_message = g_string_new ("");
for (i = 0; i < G_N_ELEMENTS (_cogl_winsys_vtable_getters); i++)
{