[gimp] app: cache bezier representation in GimpVectors object



commit 5abae32923fa6d76e9aa023110695c08ddaf70a4
Author: Sven Neumann <sven gimp org>
Date:   Thu Sep 16 21:36:51 2010 +0200

    app: cache bezier representation in GimpVectors object
    
    Introduce gimp_vectors_get_bezier() which creates the bezier
    representation on demand and then caches it for subsequent calls
    until the vectors object is frozen.
    
    At some point we should introduce GimpVectors::changed instead
    of relying on the fact that a vectors object is always frozen
    and thawn whenever it is changed...

 app/display/gimpdisplayshell-draw.c   |   10 +------
 app/vectors/gimpvectors.c             |   41 +++++++++++++++++++++++++++++---
 app/vectors/gimpvectors.h             |   16 +++++++-----
 app/widgets/gimpviewrenderervectors.c |   15 +++++-------
 devel-docs/app/app-sections.txt       |    2 +-
 5 files changed, 55 insertions(+), 29 deletions(-)
---
diff --git a/app/display/gimpdisplayshell-draw.c b/app/display/gimpdisplayshell-draw.c
index b93607b..b79ce9d 100644
--- a/app/display/gimpdisplayshell-draw.c
+++ b/app/display/gimpdisplayshell-draw.c
@@ -593,7 +593,7 @@ gimp_display_shell_draw_active_vectors (GimpDisplayShell *shell,
 
   while ((stroke = gimp_vectors_stroke_get_next (vectors, stroke)))
     {
-      GimpBezierDesc *desc = gimp_vectors_make_bezier (vectors);
+      const GimpBezierDesc *desc = gimp_vectors_get_bezier (vectors);
 
       if (desc)
         {
@@ -606,9 +606,6 @@ gimp_display_shell_draw_active_vectors (GimpDisplayShell *shell,
           cairo_set_line_width (cr, width);
           cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
           cairo_stroke (cr);
-
-          g_free (desc->data);
-          g_free (desc);
         }
     }
 }
@@ -623,7 +620,7 @@ gimp_display_shell_draw_inactive_vectors (GimpDisplayShell *shell,
 
   while ((stroke = gimp_vectors_stroke_get_next (vectors, stroke)))
     {
-      GimpBezierDesc *desc = gimp_vectors_make_bezier (vectors);
+      const GimpBezierDesc *desc = gimp_vectors_get_bezier (vectors);
 
       if (desc)
         {
@@ -636,9 +633,6 @@ gimp_display_shell_draw_inactive_vectors (GimpDisplayShell *shell,
           cairo_set_line_width (cr, width);
           cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
           cairo_stroke (cr);
-
-          g_free (desc->data);
-          g_free (desc);
         }
     }
 }
diff --git a/app/vectors/gimpvectors.c b/app/vectors/gimpvectors.c
index b8c1968..e788cba 100644
--- a/app/vectors/gimpvectors.c
+++ b/app/vectors/gimpvectors.c
@@ -119,6 +119,7 @@ static void       gimp_vectors_to_selection  (GimpItem          *item,
                                               gdouble            feather_radius_x,
                                               gdouble            feather_radius_y);
 
+static void       gimp_vectors_real_freeze          (GimpVectors       *vectors);
 static void       gimp_vectors_real_thaw            (GimpVectors       *vectors);
 static void       gimp_vectors_real_stroke_add      (GimpVectors       *vectors,
                                                      GimpStroke        *stroke);
@@ -144,6 +145,8 @@ static gint       gimp_vectors_real_interpolate     (const GimpVectors *vectors,
                                                      gdouble            precision,
                                                      gint               max_points,
                                                      GimpCoords        *ret_coords);
+
+static GimpBezierDesc * gimp_vectors_make_bezier      (const GimpVectors *vectors);
 static GimpBezierDesc * gimp_vectors_real_make_bezier (const GimpVectors *vectors);
 
 
@@ -217,7 +220,7 @@ gimp_vectors_class_init (GimpVectorsClass *klass)
   item_class->raise_failed         = _("Path cannot be raised higher.");
   item_class->lower_failed         = _("Path cannot be lowered more.");
 
-  klass->freeze                    = NULL;
+  klass->freeze                    = gimp_vectors_real_freeze;
   klass->thaw                      = gimp_vectors_real_thaw;
 
   klass->stroke_add                = gimp_vectors_real_stroke_add;
@@ -252,6 +255,13 @@ gimp_vectors_finalize (GObject *object)
 {
   GimpVectors *vectors = GIMP_VECTORS (object);
 
+  if (vectors->bezier_desc)
+    {
+      g_free (vectors->bezier_desc->data);
+      g_free (vectors->bezier_desc);
+      vectors->bezier_desc = NULL;
+    }
+
   if (vectors->strokes)
     {
       g_list_foreach (vectors->strokes, (GFunc) g_object_unref, NULL);
@@ -595,6 +605,19 @@ gimp_vectors_to_selection (GimpItem       *item,
 }
 
 static void
+gimp_vectors_real_freeze (GimpVectors *vectors)
+{
+  /*  release cached bezier representation  */
+
+  if (vectors->bezier_desc)
+    {
+      g_free (vectors->bezier_desc->data);
+      g_free (vectors->bezier_desc);
+      vectors->bezier_desc = NULL;
+    }
+}
+
+static void
 gimp_vectors_real_thaw (GimpVectors *vectors)
 {
   gimp_viewable_invalidate_preview (GIMP_VIEWABLE (vectors));
@@ -1080,12 +1103,22 @@ gimp_vectors_real_interpolate (const GimpVectors *vectors,
   return 0;
 }
 
-
-GimpBezierDesc *
-gimp_vectors_make_bezier (const GimpVectors *vectors)
+const GimpBezierDesc *
+gimp_vectors_get_bezier (GimpVectors *vectors)
 {
   g_return_val_if_fail (GIMP_IS_VECTORS (vectors), NULL);
 
+  if (! vectors->bezier_desc)
+    {
+      vectors->bezier_desc = gimp_vectors_make_bezier (vectors);
+    }
+
+  return  vectors->bezier_desc;
+}
+
+static GimpBezierDesc *
+gimp_vectors_make_bezier (const GimpVectors *vectors)
+{
   return GIMP_VECTORS_GET_CLASS (vectors)->make_bezier (vectors);
 }
 
diff --git a/app/vectors/gimpvectors.h b/app/vectors/gimpvectors.h
index 9a91f15..a5ca52c 100644
--- a/app/vectors/gimpvectors.h
+++ b/app/vectors/gimpvectors.h
@@ -35,13 +35,15 @@ typedef struct _GimpVectorsClass  GimpVectorsClass;
 
 struct _GimpVectors
 {
-  GimpItem  parent_instance;
+  GimpItem        parent_instance;
 
-  GList    *strokes;            /* The List of GimpStrokes        */
-  gint      last_stroke_ID;
+  GList          *strokes;        /* The List of GimpStrokes        */
+  gint            last_stroke_ID;
 
-  gint      freeze_count;
-  gdouble   precision;
+  gint            freeze_count;
+  gdouble         precision;
+
+  GimpBezierDesc *bezier_desc;    /* Cached bezier representation   */
 };
 
 struct _GimpVectorsClass
@@ -172,8 +174,8 @@ gint            gimp_vectors_interpolate        (const GimpVectors  *vectors,
 
 /* usually overloaded */
 
-/* creates a bezier representation. */
+/* returns a bezier representation */
+const GimpBezierDesc * gimp_vectors_get_bezier  (GimpVectors        *vectors);
 
-GimpBezierDesc * gimp_vectors_make_bezier       (const GimpVectors  *vectors);
 
 #endif /* __GIMP_VECTORS_H__ */
diff --git a/app/widgets/gimpviewrenderervectors.c b/app/widgets/gimpviewrenderervectors.c
index 5066353..5a928c9 100644
--- a/app/widgets/gimpviewrenderervectors.c
+++ b/app/widgets/gimpviewrenderervectors.c
@@ -69,9 +69,9 @@ gimp_view_renderer_vectors_draw (GimpViewRenderer   *renderer,
                                  cairo_t            *cr,
                                  const GdkRectangle *area)
 {
-  GtkStyle       *style   = gtk_widget_get_style (widget);
-  GimpVectors    *vectors = GIMP_VECTORS (renderer->viewable);
-  GimpBezierDesc *bezdesc;
+  GtkStyle             *style   = gtk_widget_get_style (widget);
+  GimpVectors          *vectors = GIMP_VECTORS (renderer->viewable);
+  const GimpBezierDesc *desc;
 
   gdk_cairo_set_source_color (cr, &style->white);
 
@@ -82,9 +82,9 @@ gimp_view_renderer_vectors_draw (GimpViewRenderer   *renderer,
   cairo_clip_preserve (cr);
   cairo_fill (cr);
 
-  bezdesc = gimp_vectors_make_bezier (vectors);
+  desc = gimp_vectors_get_bezier (vectors);
 
-  if (bezdesc)
+  if (desc)
     {
       gdouble xscale;
       gdouble yscale;
@@ -103,10 +103,7 @@ gimp_view_renderer_vectors_draw (GimpViewRenderer   *renderer,
       cairo_set_line_width (cr, MAX (xscale, yscale));
       gdk_cairo_set_source_color (cr, &style->black);
 
-      cairo_append_path (cr, (cairo_path_t *) bezdesc);
+      cairo_append_path (cr, (cairo_path_t *) desc);
       cairo_stroke (cr);
-
-      g_free (bezdesc->data);
-      g_free (bezdesc);
     }
 }
diff --git a/devel-docs/app/app-sections.txt b/devel-docs/app/app-sections.txt
index df780c7..470e618 100644
--- a/devel-docs/app/app-sections.txt
+++ b/devel-docs/app/app-sections.txt
@@ -5557,13 +5557,13 @@ gimp_stroke_get_distance
 gimp_stroke_get_point_at_dist
 gimp_stroke_interpolate
 gimp_stroke_duplicate
-gimp_stroke_make_bezier
 gimp_stroke_translate
 gimp_stroke_scale
 gimp_stroke_rotate
 gimp_stroke_flip
 gimp_stroke_flip_free
 gimp_stroke_transform
+gimp_stroke_get_bezier
 gimp_stroke_get_draw_anchors
 gimp_stroke_get_draw_controls
 gimp_stroke_get_draw_lines



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