[Bf-blender-cvs] [71f8d998c40] greasepencil-object: GP: Allocate stroke memory only when required

Charlie Jolly noreply at git.blender.org
Sat Dec 8 12:18:12 CET 2018


Commit: 71f8d998c4073b0a53ed65e9cd823198154522d7
Author: Charlie Jolly
Date:   Sat Dec 8 11:17:57 2018 +0000
Branches: greasepencil-object
https://developer.blender.org/rB71f8d998c4073b0a53ed65e9cd823198154522d7

GP: Allocate stroke memory only when required

===================================================================

M	source/blender/editors/gpencil/gpencil_intern.h
M	source/blender/editors/gpencil/gpencil_primitive.c

===================================================================

diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index c30f07b7c37..2b9cf181284 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -159,6 +159,7 @@ typedef struct tGPDprimitive {
 	short cyclic;                     /* cyclic option */
 	short flip;                       /* flip option */
 	tGPspoint *points;                 /* array of data-points for stroke */   
+	int point_count;                  /* number of edges allocated */
 	int tot_stored_edges;             /* stored number of polygon edges */
 	int tot_edges;                    /* number of polygon edges */
 	int top[2];                       /* first box corner */
diff --git a/source/blender/editors/gpencil/gpencil_primitive.c b/source/blender/editors/gpencil/gpencil_primitive.c
index 7615bf4d159..c0ac1e13a35 100644
--- a/source/blender/editors/gpencil/gpencil_primitive.c
+++ b/source/blender/editors/gpencil/gpencil_primitive.c
@@ -210,6 +210,15 @@ static bool gpencil_primitive_add_poll(bContext *C)
 	return 1;
 }
 
+/* Allocate memory to stroke, adds MAX_EDGES on every call */
+static void gpencil_primitive_allocate_memory(tGPDprimitive *tgpi) {
+	tgpi->point_count += MAX_EDGES + 1;
+	bGPDstroke *gpsf = tgpi->gpf->strokes.first;
+	gpsf->points = MEM_reallocN(gpsf->points, sizeof(bGPDspoint) * tgpi->point_count);
+	if (gpsf->dvert != NULL)
+		gpsf->dvert = MEM_reallocN(gpsf->dvert, sizeof(MDeformVert) * tgpi->point_count);
+	tgpi->points = MEM_reallocN(tgpi->points, sizeof(tGPspoint) * tgpi->point_count);
+}
 
 /* ****************** Primitive Interactive *********************** */
 
@@ -270,8 +279,14 @@ static void gp_primitive_set_initdata(bContext *C, tGPDprimitive *tgpi)
 
 	/* add to strokes */
 	BLI_addtail(&tgpi->gpf->strokes, gps);
+
+	/* allocate memory for storage points */
+	gpencil_primitive_allocate_memory(tgpi);
+
 }
 
+
+
 /* ----------------------- */
 /* Drawing Callbacks */
 
@@ -463,16 +478,10 @@ static void gp_primitive_update_strokes(bContext *C, tGPDprimitive *tgpi)
 	bGPDstroke *gps = tgpi->gpf->strokes.first;
 	GP_Sculpt_Settings *gset = &ts->gp_sculpt;
 
-	/* realloc points to new size */
-	/* TODO: only do this if the size has changed? */
-	gps->points = MEM_reallocN(gps->points, sizeof(bGPDspoint) * (tgpi->tot_edges + tgpi->tot_stored_edges));
-	if (gps->dvert != NULL) {
-		gps->dvert = MEM_reallocN(gps->dvert, sizeof(MDeformVert) * (tgpi->tot_edges + tgpi->tot_stored_edges));
-	}
 	gps->totpoints = (tgpi->tot_edges + tgpi->tot_stored_edges);
 
 	/* compute screen-space coordinates for points */
-	tgpi->points = MEM_reallocN(tgpi->points, sizeof(tGPspoint) * (tgpi->tot_edges + tgpi->tot_stored_edges));
+	
 	tGPspoint *points2D = tgpi->points;
 	switch (tgpi->type) {
 		case GP_STROKE_BOX:
@@ -587,6 +596,9 @@ static void gp_primitive_update_strokes(bContext *C, tGPDprimitive *tgpi)
 static void gpencil_primitive_add_segment(tGPDprimitive *tgpi)
 {
 	tgpi->tot_stored_edges += tgpi->tot_edges;
+	if (tgpi->tot_stored_edges >= tgpi->point_count) {
+		gpencil_primitive_allocate_memory(tgpi);
+	}
 }
 
 /* Update screen and stroke */



More information about the Bf-blender-cvs mailing list