[Cogl] [PATCH 2/3] Adds internal cogl closure list utility
Robert Bragg
robert at sixbynine.org
Sat Apr 27 17:12:40 PDT 2013
From: Robert Bragg <robert at linux.intel.com>
This adds some utility code to help us manage lists of closures
consistently within Cogl. The utilities are from Rig and were originally
written by Neil Roberts.
This adapts the way we track CoglOnscreen resize and frame closures to
use the new utilities.
---
cogl/Makefile.am | 2 +
cogl/cogl-closure-list-private.h | 104 +++++++++++++++++++++++++++++++++++++++
cogl/cogl-closure-list.c | 65 ++++++++++++++++++++++++
cogl/cogl-onscreen-private.h | 29 ++---------
cogl/cogl-onscreen.c | 87 +++++++++-----------------------
cogl/cogl-onscreen.h | 4 +-
6 files changed, 199 insertions(+), 92 deletions(-)
create mode 100644 cogl/cogl-closure-list-private.h
create mode 100644 cogl/cogl-closure-list.c
diff --git a/cogl/Makefile.am b/cogl/Makefile.am
index ef83799..b261f11 100644
--- a/cogl/Makefile.am
+++ b/cogl/Makefile.am
@@ -401,6 +401,8 @@ cogl_sources_c = \
$(srcdir)/cogl-gles2-context.c \
$(srcdir)/cogl-error-private.h \
$(srcdir)/cogl-error.c \
+ $(srcdir)/cogl-closure-list-private.h \
+ $(srcdir)/cogl-closure-list.c \
$(NULL)
if USE_GLIB
diff --git a/cogl/cogl-closure-list-private.h b/cogl/cogl-closure-list-private.h
new file mode 100644
index 0000000..d0e10ce
--- /dev/null
+++ b/cogl/cogl-closure-list-private.h
@@ -0,0 +1,104 @@
+/*
+ * Cogl
+ *
+ * An object oriented GL/GLES Abstraction/Utility Layer
+ *
+ * Copyright (C) 2012,2013 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/>.
+ *
+ */
+
+#ifndef _COGL_CLOSURE_LIST_PRIVATE_H_
+#define _COGL_CLOSURE_LIST_PRIVATE_H_
+
+#include "cogl-object.h"
+#include "cogl-queue.h"
+
+/*
+ * This implements a list of callbacks that can be used a bit like
+ * signals in GObject, but that don't have any marshalling overhead.
+ *
+ * The idea is that any Cogl code that wants to provide a callback
+ * point will provide api to add a callback for that particular point.
+ * The function can take a function pointer with the correct
+ * signature. Internally the Cogl code can use cogl_closure_list_add,
+ * cogl_closure_disconnect and cogl_closure_list_disconnect_all
+ *
+ * In the future we could consider exposing the CoglClosure type which
+ * would allow applications to use cogl_closure_disconnect() directly
+ * so we don't need to expose new disconnect apis for each callback
+ * point.
+ */
+
+typedef struct _CoglClosure CoglClosure;
+
+COGL_LIST_HEAD (CoglClosureList, CoglClosure);
+
+struct _CoglClosure
+{
+ COGL_LIST_ENTRY (CoglClosure) list_node;
+
+ void *function;
+ void *user_data;
+ CoglUserDataDestroyCallback destroy_cb;
+};
+
+/*
+ * cogl_closure_disconnect:
+ * @closure: A closure connected to a Cogl closure list
+ *
+ * Removes the given closure from the callback list it is connected to
+ * and destroys it. If the closure was created with a destroy function
+ * then it will be invoked. */
+void
+cogl_closure_disconnect (CoglClosure *closure);
+
+void
+cogl_closure_list_disconnect_all (CoglClosureList *list);
+
+CoglClosure *
+cogl_closure_list_add (CoglClosureList *list,
+ void *function,
+ void *user_data,
+ CoglUserDataDestroyCallback destroy_cb);
+
+/*
+ * cogl_closure_list_invoke:
+ * @list: A pointer to a CoglList containing CoglClosures
+ * @cb_type: The name of a typedef for the closure callback function signature
+ * @...: The the arguments to pass to the callback
+ *
+ * A convenience macro to invoke a closure list.
+ *
+ * Note that the arguments will be evaluated multiple times so it is
+ * not safe to pass expressions that have side-effects.
+ *
+ * Note also that this function ignores the return value from the
+ * callbacks. If you want to handle the return value you should
+ * manually iterate the list and invoke the callbacks yourself.
+ */
+#define cogl_closure_list_invoke(list, cb_type, ...) \
+ G_STMT_START { \
+ CoglClosure *_c, *_tmp; \
+ \
+ COGL_LIST_FOREACH_SAFE (_c, (list), list_node, _tmp) \
+ { \
+ cb_type _cb = _c->function; \
+ _cb (__VA_ARGS__, _c->user_data); \
+ } \
+ } G_STMT_END
+
+#endif /* _COGL_CLOSURE_LIST_PRIVATE_H_ */
diff --git a/cogl/cogl-closure-list.c b/cogl/cogl-closure-list.c
new file mode 100644
index 0000000..9c514b7
--- /dev/null
+++ b/cogl/cogl-closure-list.c
@@ -0,0 +1,65 @@
+/*
+ * Cogl
+ *
+ * An object oriented GL/GLES Abstraction/Utility Layer
+ *
+ * Copyright (C) 2012,2013 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/>.
+ *
+ */
+
+#include <config.h>
+
+#include <glib.h>
+
+#include "cogl-closure-list-private.h"
+
+void
+cogl_closure_disconnect (CoglClosure *closure)
+{
+ COGL_LIST_REMOVE (closure, list_node);
+
+ if (closure->destroy_cb)
+ closure->destroy_cb (closure->user_data);
+
+ g_slice_free (CoglClosure, closure);
+}
+
+void
+cogl_closure_list_disconnect_all (CoglClosureList *list)
+{
+ CoglClosure *closure, *next;
+
+ COGL_LIST_FOREACH_SAFE (closure, list, list_node, next)
+ cogl_closure_disconnect (closure);
+}
+
+CoglClosure *
+cogl_closure_list_add (CoglClosureList *list,
+ void *function,
+ void *user_data,
+ CoglUserDataDestroyCallback destroy_cb)
+{
+ CoglClosure *closure = g_slice_new (CoglClosure);
+
+ closure->function = function;
+ closure->user_data = user_data;
+ closure->destroy_cb = destroy_cb;
+
+ COGL_LIST_INSERT_HEAD (list, closure, list_node);
+
+ return closure;
+}
diff --git a/cogl/cogl-onscreen-private.h b/cogl/cogl-onscreen-private.h
index 5e4c031..4c2ce94 100644
--- a/cogl/cogl-onscreen-private.h
+++ b/cogl/cogl-onscreen-private.h
@@ -27,6 +27,7 @@
#include "cogl-onscreen.h"
#include "cogl-framebuffer-private.h"
#include "cogl-queue.h"
+#include "cogl-closure-list-private.h"
#include <glib.h>
@@ -34,30 +35,6 @@
#include <windows.h>
#endif
-COGL_TAILQ_HEAD (CoglFrameCallbackList, CoglFrameClosure);
-
-struct _CoglFrameClosure
-{
- COGL_TAILQ_ENTRY (CoglFrameClosure) list_node;
-
- CoglFrameCallback callback;
-
- void *user_data;
- CoglUserDataDestroyCallback destroy;
-};
-
-COGL_TAILQ_HEAD (CoglOnscreenResizeCallbackList, CoglOnscreenResizeClosure);
-
-struct _CoglOnscreenResizeClosure
-{
- COGL_TAILQ_ENTRY (CoglOnscreenResizeClosure) list_node;
-
- CoglOnscreenResizeCallback callback;
-
- void *user_data;
- CoglUserDataDestroyCallback destroy;
-};
-
typedef struct _CoglOnscreenEvent CoglOnscreenEvent;
COGL_TAILQ_HEAD (CoglOnscreenEventList, CoglOnscreenEvent);
@@ -91,10 +68,10 @@ struct _CoglOnscreen
CoglBool swap_throttled;
- CoglFrameCallbackList frame_closures;
+ CoglClosureList frame_closures;
CoglBool resizable;
- CoglOnscreenResizeCallbackList resize_closures;
+ CoglClosureList resize_closures;
int64_t frame_counter;
int64_t swap_frame_counter; /* frame counter at last all to
diff --git a/cogl/cogl-onscreen.c b/cogl/cogl-onscreen.c
index 338f747..ba7d888 100644
--- a/cogl/cogl-onscreen.c
+++ b/cogl/cogl-onscreen.c
@@ -32,6 +32,7 @@
#include "cogl-onscreen-template-private.h"
#include "cogl-context-private.h"
#include "cogl-object-private.h"
+#include "cogl-closure-list-private.h"
static void _cogl_onscreen_free (CoglOnscreen *onscreen);
@@ -45,8 +46,8 @@ _cogl_onscreen_init_from_template (CoglOnscreen *onscreen,
{
CoglFramebuffer *framebuffer = COGL_FRAMEBUFFER (onscreen);
- COGL_TAILQ_INIT (&onscreen->frame_closures);
- COGL_TAILQ_INIT (&onscreen->resize_closures);
+ COGL_LIST_INIT (&onscreen->frame_closures);
+ COGL_LIST_INIT (&onscreen->resize_closures);
framebuffer->config = onscreen_template->config;
cogl_object_ref (framebuffer->config.swap_chain);
@@ -88,19 +89,8 @@ _cogl_onscreen_free (CoglOnscreen *onscreen)
const CoglWinsysVtable *winsys = _cogl_framebuffer_get_winsys (framebuffer);
CoglFrameInfo *frame_info;
- while (!COGL_TAILQ_EMPTY (&onscreen->resize_closures))
- {
- CoglOnscreenResizeClosure *resize_closure =
- COGL_TAILQ_FIRST (&onscreen->resize_closures);
- cogl_onscreen_remove_resize_callback (onscreen, resize_closure);
- }
-
- while (!COGL_TAILQ_EMPTY (&onscreen->frame_closures))
- {
- CoglFrameClosure *frame_closure =
- COGL_TAILQ_FIRST (&onscreen->frame_closures);
- cogl_onscreen_remove_frame_callback (onscreen, frame_closure);
- }
+ cogl_closure_list_disconnect_all (&onscreen->resize_closures);
+ cogl_closure_list_disconnect_all (&onscreen->frame_closures);
while ((frame_info = g_queue_pop_tail (&onscreen->pending_frame_infos)))
cogl_object_unref (frame_info);
@@ -323,15 +313,10 @@ cogl_onscreen_add_frame_callback (CoglOnscreen *onscreen,
void *user_data,
CoglUserDataDestroyCallback destroy)
{
- CoglFrameClosure *closure = g_slice_new0 (CoglFrameClosure);
-
- closure->callback = callback;
- closure->user_data = user_data;
- closure->destroy = destroy;
-
- COGL_TAILQ_INSERT_TAIL (&onscreen->frame_closures, closure, list_node);
-
- return closure;
+ return cogl_closure_list_add (&onscreen->frame_closures,
+ callback,
+ user_data,
+ destroy);
}
void
@@ -340,12 +325,7 @@ cogl_onscreen_remove_frame_callback (CoglOnscreen *onscreen,
{
_COGL_RETURN_IF_FAIL (closure);
- if (closure->destroy)
- closure->destroy (closure->user_data);
-
- COGL_TAILQ_REMOVE (&onscreen->frame_closures, closure, list_node);
-
- g_slice_free (CoglFrameClosure, closure);
+ cogl_closure_disconnect (closure);
}
void
@@ -398,16 +378,9 @@ notify_event (CoglOnscreen *onscreen,
CoglFrameEvent event,
CoglFrameInfo *info)
{
- CoglFrameClosure *entry, *tmp;
-
- COGL_TAILQ_FOREACH_SAFE (entry,
- &onscreen->frame_closures,
- list_node,
- tmp)
- {
- entry->callback (onscreen, event, info,
- entry->user_data);
- }
+ cogl_closure_list_invoke (&onscreen->frame_closures,
+ CoglFrameCallback,
+ onscreen, event, info);
}
void
@@ -456,17 +429,13 @@ _cogl_onscreen_notify_complete (CoglOnscreen *onscreen, CoglFrameInfo *info)
void
_cogl_onscreen_notify_resize (CoglOnscreen *onscreen)
{
- CoglOnscreenResizeClosure *closure, *tmp;
CoglFramebuffer *framebuffer = COGL_FRAMEBUFFER (onscreen);
- COGL_TAILQ_FOREACH_SAFE (closure,
- &onscreen->resize_closures,
- list_node,
- tmp)
- closure->callback (onscreen,
- framebuffer->width,
- framebuffer->height,
- closure->user_data);
+ cogl_closure_list_invoke (&onscreen->resize_closures,
+ CoglOnscreenResizeCallback,
+ onscreen,
+ framebuffer->width,
+ framebuffer->height);
}
void
@@ -516,27 +485,17 @@ cogl_onscreen_add_resize_callback (CoglOnscreen *onscreen,
void *user_data,
CoglUserDataDestroyCallback destroy)
{
- CoglOnscreenResizeClosure *closure = g_slice_new (CoglOnscreenResizeClosure);
-
- closure->callback = callback;
- closure->user_data = user_data;
- closure->destroy = destroy;
-
- COGL_TAILQ_INSERT_TAIL (&onscreen->resize_closures, closure, list_node);
-
- return closure;
+ return cogl_closure_list_add (&onscreen->resize_closures,
+ callback,
+ user_data,
+ destroy);
}
void
cogl_onscreen_remove_resize_callback (CoglOnscreen *onscreen,
CoglOnscreenResizeClosure *closure)
{
- if (closure->destroy)
- closure->destroy (closure->user_data);
-
- COGL_TAILQ_REMOVE (&onscreen->resize_closures, closure, list_node);
-
- g_slice_free (CoglOnscreenResizeClosure, closure);
+ cogl_closure_disconnect (closure);
}
int64_t
diff --git a/cogl/cogl-onscreen.h b/cogl/cogl-onscreen.h
index 97e0891..fa4099b 100644
--- a/cogl/cogl-onscreen.h
+++ b/cogl/cogl-onscreen.h
@@ -497,7 +497,7 @@ typedef void (*CoglFrameCallback) (CoglOnscreen *onscreen,
* Since: 1.14
* Stability: unstable
*/
-typedef struct _CoglFrameClosure CoglFrameClosure;
+typedef struct _CoglClosure CoglFrameClosure;
/**
* cogl_onscreen_add_frame_callback:
@@ -662,7 +662,7 @@ typedef void (*CoglOnscreenResizeCallback) (CoglOnscreen *onscreen,
* Since: 2.0
* Stability: unstable
*/
-typedef struct _CoglOnscreenResizeClosure CoglOnscreenResizeClosure;
+typedef struct _CoglClosure CoglOnscreenResizeClosure;
/**
* cogl_onscreen_add_resize_callback:
--
1.8.2.1
More information about the Cogl
mailing list