[Bf-blender-cvs] [3e3efae] master: Viewport Text Drawing: replace single allocs with a memarena

Campbell Barton noreply at git.blender.org
Thu Apr 17 08:04:53 CEST 2014


Commit: 3e3efae7e9420817a6fe35545d97568a69e2ce7c
Author: Campbell Barton
Date:   Thu Apr 17 15:14:07 2014 +1000
https://developer.blender.org/rB3e3efae7e9420817a6fe35545d97568a69e2ce7c

Viewport Text Drawing: replace single allocs with a memarena

- pass label strlen since in many cases its already known.
- use single linked list for cached text drawing.
- add BLI_link_utils.h for single linked list macros.

own tests give approx 22% overall speedup.

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

A	source/blender/blenlib/BLI_link_utils.h
M	source/blender/editors/include/UI_view2d.h
M	source/blender/editors/interface/view2d.c
M	source/blender/editors/space_nla/nla_draw.c
M	source/blender/editors/space_sequencer/sequencer_draw.c
M	source/blender/editors/space_view3d/drawanimviz.c
M	source/blender/editors/space_view3d/drawarmature.c
M	source/blender/editors/space_view3d/drawobject.c
M	source/blender/editors/space_view3d/view3d_intern.h

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

diff --git a/source/blender/blenlib/BLI_link_utils.h b/source/blender/blenlib/BLI_link_utils.h
new file mode 100644
index 0000000..d469b10
--- /dev/null
+++ b/source/blender/blenlib/BLI_link_utils.h
@@ -0,0 +1,46 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#ifndef __BLI_LINK_UTILS_H__
+#define __BLI_LINK_UTILS_H__
+
+/** \file BLI_link_utils.h
+ *  \ingroup bli
+ *  \brief Single link-list utility macros. (header only api).
+ *
+ * Use this api when the structure defines its own ``next`` pointer
+ * and a double linked list such as #ListBase isnt needed.
+ */
+
+#define BLI_LINKS_PREPEND(list, link)  { \
+	CHECK_TYPE_PAIR(list, link); \
+	(link)->next = list; \
+	list = link; \
+} (void)0
+
+#define BLI_LINKS_FREE(list)  { \
+	while (list) { \
+		void *next = list->next; \
+		MEM_freeN(list); \
+		list = next; \
+	} \
+} (void)0
+
+#endif  /* __BLI_LINK_UTILS_H__ */
diff --git a/source/blender/editors/include/UI_view2d.h b/source/blender/editors/include/UI_view2d.h
index 1208446..b087469 100644
--- a/source/blender/editors/include/UI_view2d.h
+++ b/source/blender/editors/include/UI_view2d.h
@@ -210,8 +210,8 @@ void UI_view2d_offset(struct View2D *v2d, float xfac, float yfac);
 short UI_view2d_mouse_in_scrollers(const struct bContext *C, struct View2D *v2d, int x, int y);
 
 /* cached text drawing in v2d, to allow pixel-aligned draw as post process */
-void UI_view2d_text_cache_add(struct View2D *v2d, float x, float y, const char *str, const char col[4]);
-void UI_view2d_text_cache_rectf(struct View2D *v2d, const struct rctf *rect, const char *str, const char col[4]);
+void UI_view2d_text_cache_add(struct View2D *v2d, float x, float y, const char *str, size_t str_len, const char col[4]);
+void UI_view2d_text_cache_rectf(struct View2D *v2d, const struct rctf *rect, const char *str, size_t str_len, const char col[4]);
 void UI_view2d_text_cache_draw(struct ARegion *ar);
 
 /* operators */
diff --git a/source/blender/editors/interface/view2d.c b/source/blender/editors/interface/view2d.c
index f20a196..8f501e2 100644
--- a/source/blender/editors/interface/view2d.c
+++ b/source/blender/editors/interface/view2d.c
@@ -38,9 +38,13 @@
 #include "DNA_scene_types.h"
 #include "DNA_userdef_types.h"
 
-#include "BLI_blenlib.h"
-#include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_listbase.h"
+#include "BLI_link_utils.h"
+#include "BLI_rect.h"
+#include "BLI_linklist.h"
+#include "BLI_math.h"
+#include "BLI_memarena.h"
 #include "BLI_timecode.h"
 
 #include "BKE_context.h"
@@ -2175,56 +2179,76 @@ short UI_view2d_mouse_in_scrollers(const bContext *C, View2D *v2d, int x, int y)
 
 /* ******************* view2d text drawing cache ******************** */
 
-/* assumes caches are used correctly, so for time being no local storage in v2d */
-static ListBase strings = {NULL, NULL};
-
 typedef struct View2DString {
-	struct View2DString *next, *prev;
+	struct View2DString *next;
 	union {
 		unsigned char ub[4];
 		int pack;
 	} col;
-	int mval[2];
 	rcti rect;
+	int mval[2];
 } View2DString;
 
+/* assumes caches are used correctly, so for time being no local storage in v2d */
+static MemArena     *g_v2d_strings_arena = NULL;
+static View2DString *g_v2d_strings = NULL;
 
-void UI_view2d_text_cache_add(View2D *v2d, float x, float y, const char *str, const char col[4])
+void UI_view2d_text_cache_add(View2D *v2d, float x, float y, const char *str, size_t str_len, const char col[4])
 {
 	int mval[2];
 	
+	BLI_assert(str_len == strlen(str));
+
 	UI_view2d_view_to_region(v2d, x, y, mval, mval + 1);
 	
 	if (mval[0] != V2D_IS_CLIPPED && mval[1] != V2D_IS_CLIPPED) {
-		int len = strlen(str) + 1;
-		/* use calloc, rect has to be zeroe'd */
-		View2DString *v2s = MEM_callocN(sizeof(View2DString) + len, "View2DString");
-		char *v2s_str = (char *)(v2s + 1);
-		memcpy(v2s_str, str, len);
+		int alloc_len = str_len + 1;
+		View2DString *v2s;
+
+		if (g_v2d_strings_arena == NULL) {
+			g_v2d_strings_arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 14), __func__);
+		}
+
+		v2s = BLI_memarena_alloc(g_v2d_strings_arena, sizeof(View2DString) + alloc_len);
+
+		BLI_LINKS_PREPEND(g_v2d_strings, v2s);
 
-		BLI_addtail(&strings, v2s);
 		v2s->col.pack = *((int *)col);
+
+		memset(&v2s->rect, 0, sizeof(v2s->rect));
+
 		v2s->mval[0] = mval[0];
 		v2s->mval[1] = mval[1];
+
+		memcpy(v2s + 1, str, alloc_len);
 	}
 }
 
 /* no clip (yet) */
-void UI_view2d_text_cache_rectf(View2D *v2d, const rctf *rect, const char *str, const char col[4])
+void UI_view2d_text_cache_rectf(View2D *v2d, const rctf *rect, const char *str, size_t str_len, const char col[4])
 {
-	int len = strlen(str) + 1;
-	View2DString *v2s = MEM_callocN(sizeof(View2DString) + len, "View2DString");
-	char *v2s_str = (char *)(v2s + 1);
-	memcpy(v2s_str, str, len);
+	int alloc_len = str_len;
+	View2DString *v2s;
+
+	BLI_assert(str_len == strlen(str));
+
+	if (g_v2d_strings_arena == NULL) {
+		g_v2d_strings_arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 14), __func__);
+	}
+
+	v2s = BLI_memarena_alloc(g_v2d_strings_arena, sizeof(View2DString) + alloc_len);
+
+	BLI_LINKS_PREPEND(g_v2d_strings, v2s);
+
+	v2s->col.pack = *((int *)col);
 
 	UI_view2d_to_region_no_clip(v2d, rect->xmin, rect->ymin, &v2s->rect.xmin, &v2s->rect.ymin);
 	UI_view2d_to_region_no_clip(v2d, rect->xmax, rect->ymax, &v2s->rect.xmax, &v2s->rect.ymax);
 
-	v2s->col.pack = *((int *)col);
 	v2s->mval[0] = v2s->rect.xmin;
 	v2s->mval[1] = v2s->rect.ymin;
 
-	BLI_addtail(&strings, v2s);
+	memcpy(v2s + 1, str, alloc_len);
 }
 
 
@@ -2234,7 +2258,7 @@ void UI_view2d_text_cache_draw(ARegion *ar)
 	int col_pack_prev = 0;
 
 	/* investigate using BLF_ascender() */
-	const float default_height = strings.first ? BLF_height_default("28", 3) : 0.0f;
+	const float default_height = g_v2d_strings ? BLF_height_default("28", 3) : 0.0f;
 	
 	// glMatrixMode(GL_PROJECTION);
 	// glPushMatrix();
@@ -2242,7 +2266,7 @@ void UI_view2d_text_cache_draw(ARegion *ar)
 	// glPushMatrix();
 	ED_region_pixelspace(ar);
 
-	for (v2s = strings.first; v2s; v2s = v2s->next) {
+	for (v2s = g_v2d_strings; v2s; v2s = v2s->next) {
 		const char *str = (const char *)(v2s + 1);
 		int xofs = 0, yofs;
 
@@ -2263,14 +2287,17 @@ void UI_view2d_text_cache_draw(ARegion *ar)
 			BLF_disable_default(BLF_CLIPPING);
 		}
 	}
-	
+	g_v2d_strings = NULL;
+
+	if (g_v2d_strings_arena) {
+		BLI_memarena_free(g_v2d_strings_arena);
+		g_v2d_strings_arena = NULL;
+	}
+
 	// glMatrixMode(GL_PROJECTION);
 	// glPopMatrix();
 	// glMatrixMode(GL_MODELVIEW);
 	// glPopMatrix();
-	
-	if (strings.first) 
-		BLI_freelistN(&strings);
 }
 
 
diff --git a/source/blender/editors/space_nla/nla_draw.c b/source/blender/editors/space_nla/nla_draw.c
index cf01ebd..46fd325 100644
--- a/source/blender/editors/space_nla/nla_draw.c
+++ b/source/blender/editors/space_nla/nla_draw.c
@@ -428,16 +428,17 @@ static void nla_draw_strip_text(AnimData *adt, NlaTrack *nlt, NlaStrip *strip, i
 {
 	short notSolo = ((adt && (adt->flag & ADT_NLA_SOLO_TRACK)) && (nlt->flag & NLATRACK_SOLO) == 0);
 	char str[256];
+	size_t str_len;
 	char col[4];
 	float xofs;
 	rctf rect;
 	
 	/* just print the name and the range */
 	if (strip->flag & NLASTRIP_FLAG_TEMP_META) {
-		BLI_snprintf(str, sizeof(str), "%d) Temp-Meta", index);
+		str_len = BLI_snprintf(str, sizeof(str), "%d) Temp-Meta", index);
 	}
 	else {
-		BLI_strncpy(str, strip->name, sizeof(str));
+		str_len = BLI_strncpy_rlen(str, strip->name, sizeof(str));
 	}
 	
 	/* set text color - if colors (see above) are light, draw black text, otherwise draw white */
@@ -470,7 +471,7 @@ static void nla_draw_strip_text(AnimData *adt, NlaTrack *nlt, NlaStrip *strip, i
 	rect.ymax = ymaxc;
 	
 	/* add this string to the cache of texts to draw */
-	UI_view2d_text_cache_rectf(v2d, &rect, str, col);
+	UI_view2d_text_cache_rectf(v2d, &rect, str, str_len, col);
 }
 
 /* add frame extents to cache of text-strings to draw in pixelspace
@@ -480,7 +481,8 @@ static void nla_draw_strip_frames_text(NlaTrack *UNUSED(nlt), NlaStrip *strip, V
 {
 	const float ytol = 1.0f; /* small offset to vertical positioning of text, for legibility */
 	const char col[4] = {220, 220, 220, 255}; /* light gray */
-	char numstr[32] = "";
+	char numstr[32];
+	size_t numstr_len;
 	
 	
 	/* Always draw times above the strip, whereas sequencer drew below + above.
@@ -490,12 +492,12 @@ static void nla_draw_strip_frames_text(NlaTrack *UNUSED(nlt), NlaStrip *strip, V
 	 *	  while also preserving some accuracy, since we do use floats
 	 */
 	/* start frame */
-	BLI_snprintf(numstr, sizeof(numstr), "%.1f", strip->start);
-	UI_view2d_text_cache_add(v2d, strip->start - 1.0f, ymaxc + ytol, numstr, col);
+	numstr_len = BLI_snprintf(numstr, sizeof(numstr), "%.1f", strip->start);
+	UI_view2d_text_cache_add(v2d, strip->start - 1.0f, ymaxc + ytol, numstr, numstr_len, col);
 	
 	/* end frame */
-	BLI_snprintf(numstr, sizeof(numstr), "%.1f", strip->end);
-	UI_view2d_text_cache_add(v2d, strip->end, ymaxc + ytol, numstr, col);
+	numstr_len = BLI_snprintf(numstr, sizeof(numstr), "%.1f", strip->end);
+	UI_view2d_text_cache_add(v2d, strip->end, ymaxc + ytol, numstr, numstr_len, col);
 }
 
 /* ---------------------- */
diff --git a/source/blender/editors/space_sequencer/sequencer_draw.c b/source/blender/editors/space_sequencer/sequencer_draw.c
index fe900b6..a4390d5 100644
--- a/source/blender/editors/space_sequencer/sequencer_draw.c
+++ b/source/blender/editors/space_sequencer/sequencer_draw

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list