summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNeil Roberts <neil@linux.intel.com>2011-12-19 14:38:28 +0000
committerNeil Roberts <neil@linux.intel.com>2012-01-05 13:40:59 +0000
commit85d79ab4fa56eccd9f65c0538e564f6d0d2cb065 (patch)
tree819ac776e8f97f7461cd3d9ccfa90e2aee46b0f0
parent181b875a3d73860b5568768658d82b7dd7492107 (diff)
downloadcogl-85d79ab4fa56eccd9f65c0538e564f6d0d2cb065.tar.gz
Add a utility to make a GSource out of a CoglContext
The GSource is created using cogl_glib_source_new which takes a pointer to a CoglContext. The source calls cogl_poll_get_info() in its prepare function and cogl_poll_dispatch() in its dispatch function. The poll FDs on the source are updated according to what Cogl reports. The header is only included and the source only compiled if Cogl is configured with GLib support. Reviewed-by: Robert Bragg <robert@linux.intel.com>
-rw-r--r--cogl/Makefile.am7
-rw-r--r--cogl/cogl-glib-source.c194
-rw-r--r--cogl/cogl-glib-source.h59
-rw-r--r--cogl/cogl.h3
-rw-r--r--doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt1
5 files changed, 264 insertions, 0 deletions
diff --git a/cogl/Makefile.am b/cogl/Makefile.am
index d92643f7..0c223b86 100644
--- a/cogl/Makefile.am
+++ b/cogl/Makefile.am
@@ -348,6 +348,13 @@ cogl_sources_c = \
$(srcdir)/gl-prototypes/cogl-glsl-functions.h \
$(NULL)
+if USE_GLIB
+cogl_public_h += \
+ $(srcdir)/cogl-glib-source.h
+cogl_sources_c += \
+ $(srcdir)/cogl-glib-source.c
+endif
+
if SUPPORT_XLIB
cogl_public_h += \
$(srcdir)/cogl-clutter-xlib.h \
diff --git a/cogl/cogl-glib-source.c b/cogl/cogl-glib-source.c
new file mode 100644
index 00000000..1cff8e5c
--- /dev/null
+++ b/cogl/cogl-glib-source.c
@@ -0,0 +1,194 @@
+/*
+ * Cogl
+ *
+ * An object oriented GL/GLES Abstraction/Utility Layer
+ *
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "cogl-glib-source.h"
+#include "cogl-poll.h"
+
+typedef struct _CoglGLibSource
+{
+ GSource source;
+
+ CoglContext *context;
+
+ GArray *poll_fds;
+
+ gint64 expiration_time;
+} CoglGLibSource;
+
+static gboolean
+cogl_glib_source_poll_fds_changed (CoglGLibSource *cogl_source,
+ const CoglPollFD *poll_fds,
+ int n_poll_fds)
+{
+ int i;
+
+ if (cogl_source->poll_fds->len != n_poll_fds)
+ return TRUE;
+
+ for (i = 0; i < n_poll_fds; i++)
+ if (g_array_index (cogl_source->poll_fds, CoglPollFD, i).fd !=
+ poll_fds[i].fd)
+ return TRUE;
+
+ return FALSE;
+}
+
+static gboolean
+cogl_glib_source_prepare (GSource *source, int *timeout)
+{
+ CoglGLibSource *cogl_source = (CoglGLibSource *) source;
+ CoglPollFD *poll_fds;
+ int n_poll_fds;
+ gint64 cogl_timeout;
+ int i;
+
+ cogl_poll_get_info (cogl_source->context,
+ &poll_fds,
+ &n_poll_fds,
+ &cogl_timeout);
+
+ /* We have to be careful not to call g_source_add/remove_poll unless
+ the FDs have changed because it will cause the main loop to
+ immediately wake up. If we call it every time the source is
+ prepared it will effectively never go idle. */
+ if (cogl_glib_source_poll_fds_changed (cogl_source, poll_fds, n_poll_fds))
+ {
+ /* Remove any existing polls before adding the new ones */
+ for (i = 0; i < cogl_source->poll_fds->len; i++)
+ {
+ GPollFD *poll_fd = &g_array_index (cogl_source->poll_fds, GPollFD, i);
+ g_source_remove_poll (source, poll_fd);
+ }
+
+ g_array_set_size (cogl_source->poll_fds, n_poll_fds);
+
+ for (i = 0; i < n_poll_fds; i++)
+ {
+ GPollFD *poll_fd = &g_array_index (cogl_source->poll_fds, GPollFD, i);
+ poll_fd->fd = poll_fds[i].fd;
+ g_source_add_poll (source, poll_fd);
+ }
+ }
+
+ /* Update the events */
+ for (i = 0; i < n_poll_fds; i++)
+ {
+ GPollFD *poll_fd = &g_array_index (cogl_source->poll_fds, GPollFD, i);
+ poll_fd->events = poll_fds[i].events;
+ poll_fd->revents = 0;
+ }
+
+ if (cogl_timeout == -1)
+ {
+ *timeout = -1;
+ cogl_source->expiration_time = -1;
+ }
+ else
+ {
+ /* Round up to ensure that we don't try again too early */
+ *timeout = (cogl_timeout + 999) / 1000;
+ cogl_source->expiration_time = (g_source_get_time (source) +
+ cogl_timeout);
+ }
+
+ return *timeout == 0;
+}
+
+static gboolean
+cogl_glib_source_check (GSource *source)
+{
+ CoglGLibSource *cogl_source = (CoglGLibSource *) source;
+ int i;
+
+ if (cogl_source->expiration_time >= 0 &&
+ g_source_get_time (source) >= cogl_source->expiration_time)
+ return TRUE;
+
+ for (i = 0; i < cogl_source->poll_fds->len; i++)
+ {
+ GPollFD *poll_fd = &g_array_index (cogl_source->poll_fds, GPollFD, i);
+ if (poll_fd->revents != 0)
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gboolean
+cogl_glib_source_dispatch (GSource *source,
+ GSourceFunc callback,
+ void *user_data)
+{
+ CoglGLibSource *cogl_source = (CoglGLibSource *) source;
+ CoglPollFD *poll_fds =
+ (CoglPollFD *) &g_array_index (cogl_source->poll_fds, GPollFD, 0);
+
+ cogl_poll_dispatch (cogl_source->context,
+ poll_fds,
+ cogl_source->poll_fds->len);
+
+ return TRUE;
+}
+
+static void
+cogl_glib_source_finalize (GSource *source)
+{
+ CoglGLibSource *cogl_source = (CoglGLibSource *) source;
+
+ cogl_object_unref (cogl_source->context);
+
+ g_array_free (cogl_source->poll_fds, TRUE);
+}
+
+static GSourceFuncs
+cogl_glib_source_funcs =
+ {
+ cogl_glib_source_prepare,
+ cogl_glib_source_check,
+ cogl_glib_source_dispatch,
+ cogl_glib_source_finalize
+ };
+
+GSource *
+cogl_glib_source_new (CoglContext *context,
+ int priority)
+{
+ GSource *source;
+ CoglGLibSource *cogl_source;
+
+ source = g_source_new (&cogl_glib_source_funcs,
+ sizeof (CoglGLibSource));
+ cogl_source = (CoglGLibSource *) source;
+
+ cogl_source->context = cogl_object_ref (context);
+ cogl_source->poll_fds = g_array_new (FALSE, FALSE, sizeof (GPollFD));
+
+ if (priority != G_PRIORITY_DEFAULT)
+ g_source_set_priority (source, priority);
+
+ return source;
+}
diff --git a/cogl/cogl-glib-source.h b/cogl/cogl-glib-source.h
new file mode 100644
index 00000000..6bd5dd46
--- /dev/null
+++ b/cogl/cogl-glib-source.h
@@ -0,0 +1,59 @@
+/*
+ * Cogl
+ *
+ * An object oriented GL/GLES Abstraction/Utility Layer
+ *
+ * Copyright (C) 2011 Intel Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ */
+
+#if !defined(__COGL_H_INSIDE__) && !defined(CLUTTER_COMPILATION)
+#error "Only <cogl/cogl.h> can be included directly."
+#endif
+
+#ifndef __COGL_GSOURCE_H__
+#define __COGL_GSOURCE_H__
+
+#include <glib.h>
+#include <cogl/cogl-context.h>
+
+G_BEGIN_DECLS
+
+/**
+ * cogl_glib_source_new:
+ * @context: A #CoglContext
+ * @priority: The priority of the #GSource
+ *
+ * Creates a #GSource which handles Cogl's internal system event
+ * processing. This can be used as a convenience instead of
+ * cogl_poll_get_info() and cogl_poll_dispatch() in applications that
+ * are already using the GLib main loop. After this is called the
+ * #GSource should be attached to the main loop using
+ * g_source_attach().
+ *
+ * Return value: a new #GSource
+ *
+ * Stability: unstable
+ * Since: 1.10
+ */
+GSource *
+cogl_glib_source_new (CoglContext *context,
+ int priority);
+
+G_END_DECLS
+
+#endif /* __COGL_GSOURCE_H__ */
diff --git a/cogl/cogl.h b/cogl/cogl.h
index d04dc632..4ac29ac6 100644
--- a/cogl/cogl.h
+++ b/cogl/cogl.h
@@ -104,6 +104,9 @@ typedef struct _CoglFramebuffer CoglFramebuffer;
#if COGL_HAS_WIN32_SUPPORT
#include <cogl/cogl-win32-renderer.h>
#endif
+#ifdef COGL_HAS_GLIB_SUPPORT
+#include <cogl/cogl-glib-source.h>
+#endif
/* XXX: This will definitly go away once all the Clutter winsys
* code has been migrated down into Cogl! */
#include <cogl/cogl-clutter.h>
diff --git a/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt b/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt
index cbd07a00..df52c210 100644
--- a/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt
+++ b/doc/reference/cogl-2.0-experimental/cogl-2.0-experimental-sections.txt
@@ -174,6 +174,7 @@ CoglPollFDEvent
CoglPollFD
cogl_poll_get_info
cogl_poll_dispatch
+cogl_glib_source_new
</SECTION>
<SECTION>