[cogl/fosdem-2012: 9/19] context: Add support for main loop integration



commit f7bfe0145b20ac75634907925b5cdb1859cbd8f5
Author: Neil Roberts <neil linux intel com>
Date:   Fri Dec 16 17:49:28 2011 +0000

    context: Add support for main loop integration
    
    This adds two new functions to CoglContext:
    
    void
    cogl_context_begin_idle (CoglContext *context,
                             CoglPollFD **poll_fds,
                             int *n_poll_fds,
                             gint64 *timeout);
    
    void
    cogl_context_dispatch (CoglContext *context,
                           const CoglPollFD *poll_fds,
                           int n_poll_fds);
    
    The application is expected to call the first function whenever it is
    about to block to go idle, and the second function whenever it comes
    out of idle. This gives Cogl winsys's the ability poll file
    descriptors for events. For example when handing swap complete
    notifications, it can report that it needs to block on a file
    descriptor.
    
    The two functions are backed by winsys virtual functions. There are
    currently no implementations. The default handler for begin_idle just
    reports no file descriptors and an infinite timeout.

 cogl/cogl-context.c                                |   45 ++++++++
 cogl/cogl-context.h                                |  112 ++++++++++++++++++++
 cogl/winsys/cogl-winsys-private.h                  |   10 ++
 configure.ac                                       |   40 +++++++-
 .../cogl-2.0-experimental-sections.txt             |    6 +
 5 files changed, 212 insertions(+), 1 deletions(-)
---
diff --git a/cogl/cogl-context.c b/cogl/cogl-context.c
index 21c10b7..588b3f1 100644
--- a/cogl/cogl-context.c
+++ b/cogl/cogl-context.c
@@ -589,3 +589,48 @@ _cogl_context_set_current_modelview (CoglContext *context,
     cogl_object_unref (context->current_modelview_stack);
   context->current_modelview_stack = stack;
 }
+
+void
+cogl_context_begin_idle (CoglContext *context,
+                         CoglPollFD **poll_fds,
+                         int *n_poll_fds,
+                         gint64 *timeout)
+{
+  const CoglWinsysVtable *winsys;
+
+  _COGL_RETURN_IF_FAIL (cogl_is_context (context));
+  _COGL_RETURN_IF_FAIL (poll_fds != NULL);
+  _COGL_RETURN_IF_FAIL (n_poll_fds != NULL);
+  _COGL_RETURN_IF_FAIL (timeout != NULL);
+
+  winsys = _cogl_context_get_winsys (context);
+
+  if (winsys->context_begin_idle)
+    {
+      winsys->context_begin_idle (context,
+                                  poll_fds,
+                                  n_poll_fds,
+                                  timeout);
+      return;
+    }
+
+  /* By default we'll assume Cogl doesn't need to block on anything */
+  *poll_fds = NULL;
+  *n_poll_fds = 0;
+  *timeout = -1; /* no timeout */
+}
+
+void
+cogl_context_dispatch (CoglContext *context,
+                       const CoglPollFD *poll_fds,
+                       int n_poll_fds)
+{
+  const CoglWinsysVtable *winsys;
+
+  _COGL_RETURN_IF_FAIL (cogl_is_context (context));
+
+  winsys = _cogl_context_get_winsys (context);
+
+  if (winsys->context_dispatch)
+    winsys->context_dispatch (context, poll_fds, n_poll_fds);
+}
diff --git a/cogl/cogl-context.h b/cogl/cogl-context.h
index 56f3b1d..9f6c129 100644
--- a/cogl/cogl-context.h
+++ b/cogl/cogl-context.h
@@ -135,6 +135,118 @@ void
 cogl_android_set_native_window (ANativeWindow *window);
 #endif
 
+/**
+ * CoglPollFDEvent:
+ *
+ * A bitmask of events that Cogl may need to wake on for a file
+ * descriptor. Note that these all have the same values as the
+ * corresponding defines for the poll function call on Unix so they
+ * may be directly passed to poll.
+ *
+ * Since: 1.10
+ * Stability: unstable
+ */
+typedef enum
+{
+  COGL_POLL_FD_EVENT_IN = COGL_SYSDEF_POLLIN,
+  COGL_POLL_FD_EVENT_PRI = COGL_SYSDEF_POLLPRI,
+  COGL_POLL_FD_EVENT_OUT = COGL_SYSDEF_POLLOUT,
+  COGL_POLL_FD_EVENT_ERR = COGL_SYSDEF_POLLERR,
+  COGL_POLL_FD_EVENT_HUP = COGL_SYSDEF_POLLHUP,
+  COGL_POLL_FD_EVENT_NVAL = COGL_SYSDEF_POLLNVAL
+} CoglPollFDEvent;
+
+/**
+ * CoglPollFD:
+ * @fd: The file descriptor to block on
+ * @events: A bitmask of events to block on
+ * @revents: A bitmask of returned events
+ *
+ * A struct for describing the state of a file descriptor that Cogl
+ * needs to block on. The @events field contains a bitmask of
+ * #CoglPollFDEvent<!-- -->s that should cause the application to wake
+ * up. After the application is woken up from idle it should pass back
+ * an array of #CoglPollFD<!-- -->s to Cogl and update the @revents
+ * mask to the actual events that occurred on the file descriptor.
+ *
+ * Note that CoglPollFD is deliberately exactly the same as struct
+ * pollfd on Unix so that it can simply be cast when calling poll.
+ *
+ * Since: 1.10
+ * Stability: unstable
+ */
+typedef struct
+{
+  int fd;
+  short events;
+  short revents;
+} CoglPollFD;
+
+/**
+ * cogl_context_begin_idle:
+ * @context: A #CoglContext
+ * @poll_fds: A return location for a pointer to an array
+ *            of #CoglPollFD<!-- -->s
+ * @timeout: A return location for the maximum length of time to wait
+ *           in microseconds, or -1 to wait indefinitely.
+ *
+ * This should be called whenever an application is about to go idle
+ * so that Cogl has a chance to describe what state it needs to be
+ * woken up on. The assumption is that the application is using a main
+ * loop with something like the poll function call on Unix or the GLib
+ * main loop.
+ *
+ * After the function is called * poll_fds will contain a pointer to
+ * an array of #CoglPollFD structs describing the file descriptors
+ * that Cogl expects. The fd and events members will be updated
+ * accordingly. After the application has completed its idle it is
+ * expected to either update the revents members directly in this
+ * array or to create a copy of the array and update them
+ * there. Either way it should pass a pointer to either array back to
+ * Cogl when calling cogl_context_dispatch().
+ *
+ * When using the %COGL_WINSYS_ID_WGL winsys (where file descriptors
+ * don't make any sense) or %COGL_WINSYS_ID_SDL (where the event
+ * handling functions of SDL don't allow blocking on a file
+ * descriptor) *n_poll_fds is guaranteed to be zero.
+ *
+ * @timeout will contain a maximum amount of time to wait in
+ * microseconds before the application should wake up or -1 if the
+ * application should wait indefinitely. This can also be 0 zero if
+ * Cogl needs to be woken up immediately.
+ *
+ * Stability: unstable
+ * Since: 1.10
+ */
+void
+cogl_context_begin_idle (CoglContext *context,
+                         CoglPollFD **poll_fds,
+                         int *n_poll_fds,
+                         gint64 *timeout);
+
+/**
+ * cogl_context_dispatch:
+ * @context: A #CoglContext
+ * @poll_fds: An array of #CoglPollFD<!-- -->s describing the events
+ *            that have occurred since the application went idle.
+ * @n_poll_fds: The length of the @poll_fds array.
+ *
+ * This should be called whenever an application is woken up from
+ * going idle in its main loop. The @poll_fds array should contain a
+ * list of file descriptors matched with the events that occurred in
+ * revents. The events field is ignored. It is safe to pass in extra
+ * file descriptors that Cogl didn't request from
+ * cogl_context_begin_idle() or a shorter array missing some file
+ * descriptors that Cogl requested.
+ *
+ * Stability: unstable
+ * Since: 1.10
+ */
+void
+cogl_context_dispatch (CoglContext *context,
+                       const CoglPollFD *poll_fds,
+                       int n_poll_fds);
+
 G_END_DECLS
 
 #endif /* __COGL_CONTEXT_H__ */
diff --git a/cogl/winsys/cogl-winsys-private.h b/cogl/winsys/cogl-winsys-private.h
index 7686d23..bf2c0ac 100644
--- a/cogl/winsys/cogl-winsys-private.h
+++ b/cogl/winsys/cogl-winsys-private.h
@@ -151,6 +151,16 @@ typedef struct _CoglWinsysVtable
   (*onscreen_remove_swap_buffers_callback) (CoglOnscreen *onscreen,
                                             unsigned int id);
 
+  void
+  (*context_begin_idle) (CoglContext *context,
+                         CoglPollFD **poll_fds,
+                         int *n_poll_fds,
+                         gint64 *timeout);
+  void
+  (*context_dispatch) (CoglContext *context,
+                       const CoglPollFD *poll_fds,
+                       int n_poll_fds);
+
 #ifdef COGL_HAS_XLIB_SUPPORT
   gboolean
   (*texture_pixmap_x11_create) (CoglTexturePixmapX11 *tex_pixmap);
diff --git a/configure.ac b/configure.ac
index 52ee042..755cb5f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1024,9 +1024,47 @@ dnl available. Cogl has a fallback if needed.
 AC_CHECK_FUNCS([ffs])
 
 dnl ================================================================
+dnl Platform values
+dnl ================================================================
+
+dnl These are values from system headers that we want to copy into the
+dnl public Cogl headers without having to include the system header
+AC_CHECK_HEADER(poll.h,
+        [
+         AC_COMPUTE_INT(COGL_SYSDEF_POLLIN, POLLIN, [#include <poll.h>],
+                        AC_MSG_ERROR([Unable to get value of POLLIN]))
+         AC_COMPUTE_INT(COGL_SYSDEF_POLLPRI, POLLPRI, [#include <poll.h>],
+                        AC_MSG_ERROR([Unable to get value of POLLPRI]))
+         AC_COMPUTE_INT(COGL_SYSDEF_POLLOUT, POLLOUT, [#include <poll.h>],
+                        AC_MSG_ERROR([Unable to get value of POLLOUT]))
+         AC_COMPUTE_INT(COGL_SYSDEF_POLLERR, POLLERR, [#include <poll.h>],
+                        AC_MSG_ERROR([Unable to get value of POLLERR]))
+         AC_COMPUTE_INT(COGL_SYSDEF_POLLHUP, POLLHUP, [#include <poll.h>],
+                        AC_MSG_ERROR([Unable to get value of POLLHUP]))
+         AC_COMPUTE_INT(COGL_SYSDEF_POLLNVAL, POLLNVAL, [#include <poll.h>],
+                        AC_MSG_ERROR([Unable to get value of POLLNVAL]))
+        ],
+        [
+         COGL_SYSDEF_POLLIN=1
+         COGL_SYSDEF_POLLPRI=2
+         COGL_SYSDEF_POLLOUT=4
+         COGL_SYSDEF_POLLERR=8
+         COGL_SYSDEF_POLLHUP=16
+         COGL_SYSDEF_POLLNVAL=32
+        ])
+COGL_DEFINES_EXTRA="$COGL_DEFINES_EXTRA
+#define COGL_SYSDEF_POLLIN $COGL_SYSDEF_POLLIN
+#define COGL_SYSDEF_POLLPRI $COGL_SYSDEF_POLLPRI
+#define COGL_SYSDEF_POLLOUT $COGL_SYSDEF_POLLOUT
+#define COGL_SYSDEF_POLLERR $COGL_SYSDEF_POLLERR
+#define COGL_SYSDEF_POLLHUP $COGL_SYSDEF_POLLHUP
+#define COGL_SYSDEF_POLLNVAL $COGL_SYSDEF_POLLNVAL
+"
+
+dnl ================================================================
 dnl What needs to be substituted in other files
 dnl ================================================================
-COGL_DEFINES=""
+COGL_DEFINES="$COGL_DEFINES_EXTRA"
 for x in $COGL_DEFINES_SYMBOLS; do
   COGL_DEFINES="$COGL_DEFINES
 #define $x 1"
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 6381a4c..7a4feb7 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
@@ -123,6 +123,12 @@ cogl_read_pixels
 <SUBSECTION>
 cogl_flush
 
+<SUBSECTION>
+CoglPollFDEvent
+CoglPollFD
+cogl_context_begin_idle
+cogl_context_dispatch
+
 <SUBSECTION Standard>
 COGL_TYPE_ATTRIBUTE_TYPE
 COGL_TYPE_BLEND_STRING_ERROR



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