[lasem] lsm_cairo: convert LsmFilterSurface to reference counted object.



commit 69a2845470acf649b21b0cf654afff57823b428e
Author: Emmanuel Pacaud <emmanuel gnome org>
Date:   Wed Jun 13 09:53:25 2012 +0200

    lsm_cairo: convert LsmFilterSurface to reference counted object.

 src/lsmcairo.c |   54 +++++++++++++++++++-----------------------------------
 src/lsmcairo.h |    3 ++-
 2 files changed, 21 insertions(+), 36 deletions(-)
---
diff --git a/src/lsmcairo.c b/src/lsmcairo.c
index 35d9efc..95c9169 100644
--- a/src/lsmcairo.c
+++ b/src/lsmcairo.c
@@ -33,27 +33,9 @@ struct _LsmFilterSurface {
 	unsigned int y0;
 	unsigned int x1;
 	unsigned int y1;
-};
-
-LsmFilterSurface *
-lsm_filter_surface_duplicate (const LsmFilterSurface *from)
-{
-	LsmFilterSurface *filter_surface;
-
-	g_return_val_if_fail (from != NULL, NULL);
 
-	cairo_surface_reference (from->surface);
-	
-	filter_surface = g_new (LsmFilterSurface, 1);
-	filter_surface->name = g_strdup (from->name);
-	filter_surface->surface = from->surface;
-	filter_surface->x0 = from->x0;
-	filter_surface->x1 = from->x1;
-	filter_surface->y0 = from->y0;
-	filter_surface->y1 = from->y1;
-
-	return filter_surface;
-}
+	gint ref_count;
+};
 
 LsmFilterSurface *
 lsm_filter_surface_new (const char *name, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1)
@@ -83,33 +65,35 @@ lsm_filter_surface_new_with_content (const char *name, unsigned int x0, unsigned
 	filter_surface->x1 = x0 + cairo_image_surface_get_width (surface);
 	filter_surface->y1 = y0 + cairo_image_surface_get_height (surface);
 	filter_surface->surface  = surface;
+	filter_surface->ref_count = 1;
 
 	return filter_surface;
 }
 
-void
-lsm_filter_surface_free (LsmFilterSurface *filter_surface)
+LsmFilterSurface *
+lsm_filter_surface_ref (LsmFilterSurface *filter_surface)
 {
-	g_return_if_fail (filter_surface != NULL);
+	g_return_val_if_fail (filter_surface != NULL, NULL);
 
-	cairo_surface_destroy (filter_surface->surface);
-	g_free (filter_surface->name);
-	g_free (filter_surface);
+	g_atomic_int_inc (&filter_surface->ref_count);
+
+	return filter_surface;
 }
 
-GType
-lsm_filter_surface_get_type (void)
+void
+lsm_filter_surface_unref (LsmFilterSurface *filter_surface)
 {
-	static GType our_type = 0;
-
-	if (our_type == 0)
-		our_type = g_boxed_type_register_static ("LsmFilterSurface",
-							 (GBoxedCopyFunc) lsm_filter_surface_duplicate,
-							 (GBoxedFreeFunc) lsm_filter_surface_free);
+	g_return_if_fail (filter_surface != NULL);
 
-	return our_type;
+	if (g_atomic_int_dec_and_test (&filter_surface->ref_count)) {
+		cairo_surface_destroy (filter_surface->surface);
+		g_free (filter_surface->name);
+		g_free (filter_surface);
+	}
 }
 
+G_DEFINE_BOXED_TYPE (LsmFilterSurface, lsm_filter_surface, lsm_filter_surface_ref, lsm_filter_surface_unref)
+
 static void
 box_blur (LsmFilterSurface *input,
 	  LsmFilterSurface *output,
diff --git a/src/lsmcairo.h b/src/lsmcairo.h
index 181e8f8..0b742e9 100644
--- a/src/lsmcairo.h
+++ b/src/lsmcairo.h
@@ -42,7 +42,8 @@ LsmFilterSurface * 	lsm_filter_surface_new 			(const char *name,
 LsmFilterSurface * 	lsm_filter_surface_new_with_content 	(const char *name,
 								 unsigned int x0, unsigned int y0,
 								 cairo_surface_t *surface);
-void 			lsm_filter_surface_free 		(LsmFilterSurface *filter_surface);
+void 			lsm_filter_surface_unref 		(LsmFilterSurface *filter_surface);
+LsmFilterSurface *	lsm_filter_surface_ref 			(LsmFilterSurface *filter_surface);
 
 void 			lsm_filter_fast_blur 			(LsmFilterSurface *input,
 								 LsmFilterSurface *output,



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