[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [57882] branches/soc-2013-paint/source/ blender/editors/sculpt_paint: Optimizations for image paint undo stack for drag dot/anchored strokes

Antony Riakiotakis kalast at gmail.com
Sun Jun 30 11:55:29 CEST 2013


Revision: 57882
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=57882
Author:   psy-fi
Date:     2013-06-30 09:55:29 +0000 (Sun, 30 Jun 2013)
Log Message:
-----------
Optimizations for image paint undo stack for drag dot/anchored strokes

* Skip copy of undo tile to temorary buffer when restoring. Gives a nice
speedup.
* When we restore the image from undo tiles, make tiles invalid. On undo
push end, invalid tiles get deleted. This solves saving too many tiles
in the case where user could happilly drag the dot all around the image.

Modified Paths:
--------------
    branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image.c
    branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image_2d.c
    branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_intern.h
    branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_undo.c

Modified: branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image.c
===================================================================
--- branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image.c	2013-06-30 08:29:42 UTC (rev 57881)
+++ branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image.c	2013-06-30 09:55:29 UTC (rev 57882)
@@ -119,6 +119,7 @@
 
 	short source, use_float;
 	char gen_type;
+	bool valid;
 } UndoImageTile;
 
 /* this is a static resource for non-globality,
@@ -140,23 +141,18 @@
 
 static void undo_copy_tile(UndoImageTile *tile, ImBuf *tmpibuf, ImBuf *ibuf, int restore)
 {
-	/* copy or swap contents of tile->rect and region in ibuf->rect */
-	IMB_rectcpy(tmpibuf, ibuf, 0, 0, tile->x * IMAPAINT_TILE_SIZE,
-	            tile->y * IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE);
+	if (restore) {
+		/* swap to the tmpbuf for easy copying */
+		if (ibuf->rect_float) {
+			SWAP(float *, tmpibuf->rect_float, tile->rect.fp);
+		}
+		else {
+			SWAP(unsigned int *, tmpibuf->rect, tile->rect.uint);
+		}
 
-	if (ibuf->rect_float) {
-		SWAP(float *, tmpibuf->rect_float, tile->rect.fp);
-	}
-	else {
-		SWAP(unsigned int *, tmpibuf->rect, tile->rect.uint);
-	}
-
-	if (restore) {
 		IMB_rectcpy(ibuf, tmpibuf, tile->x * IMAPAINT_TILE_SIZE,
 		            tile->y * IMAPAINT_TILE_SIZE, 0, 0, IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE);
 
-		/* swap the contents back or the undo tiles will now contain the dirty version
-		 * of the image that was copied to temporary imbuf at start of function */
 		if (ibuf->rect_float) {
 			SWAP(float *, tmpibuf->rect_float, tile->rect.fp);
 		}
@@ -164,9 +160,21 @@
 			SWAP(unsigned int *, tmpibuf->rect, tile->rect.uint);
 		}
 	}
+	else {
+		/* copy or swap contents of tile->rect and region in ibuf->rect */
+		IMB_rectcpy(tmpibuf, ibuf, 0, 0, tile->x * IMAPAINT_TILE_SIZE,
+		            tile->y * IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE, IMAPAINT_TILE_SIZE);
+
+		if (ibuf->rect_float) {
+			SWAP(float *, tmpibuf->rect_float, tile->rect.fp);
+		}
+		else {
+			SWAP(unsigned int *, tmpibuf->rect, tile->rect.uint);
+		}
+	}
 }
 
-void *image_undo_find_tile(Image *ima, ImBuf *ibuf, int x_tile, int y_tile, unsigned short **mask)
+void *image_undo_find_tile(Image *ima, ImBuf *ibuf, int x_tile, int y_tile, unsigned short **mask, bool validate)
 {
 	ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_IMAGE);
 	UndoImageTile *tile;
@@ -185,6 +193,8 @@
 
 						*mask = tile->mask;
 					}
+					if (validate)
+						tile->valid = true;
 
 					return tile->rect.pt;
 				}
@@ -204,7 +214,7 @@
 	void *data;
 
 	/* check if tile is already pushed */
-	data = image_undo_find_tile(ima, ibuf, x_tile, y_tile, NULL);
+	data = image_undo_find_tile(ima, ibuf, x_tile, y_tile, NULL, true);
 	if (data)
 		return data;
 	
@@ -225,6 +235,7 @@
 	tile->gen_type = ima->gen_type;
 	tile->source = ima->source;
 	tile->use_float = use_float;
+	tile->valid = true;
 
 	undo_copy_tile(tile, *tmpibuf, ibuf, 0);
 	undo_paint_push_count_alloc(UNDO_PAINT_IMAGE, allocsize);
@@ -321,6 +332,22 @@
 		MEM_freeN(tile->rect.pt);
 }
 
+static void image_undo_end(void)
+{
+	ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_IMAGE);
+	UndoImageTile *tile;
+
+	/* first dispose of invalid tiles (may happen due to drag dot for instance) */
+	for (tile = lb->first; tile; tile = tile->next) {
+		if (!tile->valid) {
+			MEM_freeN(tile->rect.pt);
+			BLI_freelinkN (lb, tile);
+		}
+	}
+
+	undo_paint_push_end(UNDO_PAINT_IMAGE);
+}
+
 /* Imagepaint Partial Redraw & Dirty Region */
 
 void imapaint_clear_partial_redraw(void)
@@ -524,18 +551,12 @@
 /* restore painting image to previous state. Used for anchored and drag-dot style brushes*/
 static void paint_stroke_restore(bContext *C)
 {
+	UndoImageTile *tile;
 	ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_IMAGE);
 	image_undo_restore(C, lb);
 
-/* keep these here, it helps to not traverse the whole undo tree if user is
- * stroking all over the image, but this is not the ideal use case.
- * There is a tradeoff between undo initialization time and restore time.
- * Since the stroke is ideally localized, better optimize the initialization case */
-#if 0
-	image_undo_free(lb);
-	BLI_freelistN(lb);
-	undo_paint_push_count_reset(UNDO_PAINT_IMAGE);
-#endif
+	for (tile = lb->first; tile; tile = tile->next)
+		tile->valid = false;
 }
 
 static void paint_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
@@ -607,7 +628,7 @@
 		paint_2d_stroke_done(pop->custom_paint);
 	}
 
-	undo_paint_push_end(UNDO_PAINT_IMAGE);
+	image_undo_end();
 
 	/* duplicate warning, see texpaint_init */
 #if 0

Modified: branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image_2d.c
===================================================================
--- branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image_2d.c	2013-06-30 08:29:42 UTC (rev 57881)
+++ branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image_2d.c	2013-06-30 09:55:29 UTC (rev 57882)
@@ -971,9 +971,9 @@
 					int origy = region[a].desty - ty * IMAPAINT_TILE_SIZE;
 
 					if (s->canvas->rect_float)
-						tmpbuf->rect_float = image_undo_find_tile(s->image, s->canvas, tx, ty, &mask);
+						tmpbuf->rect_float = image_undo_find_tile(s->image, s->canvas, tx, ty, &mask, false);
 					else
-						tmpbuf->rect = image_undo_find_tile(s->image, s->canvas, tx, ty, &mask);
+						tmpbuf->rect = image_undo_find_tile(s->image, s->canvas, tx, ty, &mask, false);
 
 					IMB_rectblend(s->canvas, tmpbuf, frombuf, mask,
 					              maskb, mmask, mask_max,

Modified: branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_intern.h
===================================================================
--- branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_intern.h	2013-06-30 08:29:42 UTC (rev 57881)
+++ branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_intern.h	2013-06-30 09:55:29 UTC (rev 57882)
@@ -141,7 +141,7 @@
 #define IMAPAINT_TILE_NUMBER(size)  (((size) + IMAPAINT_TILE_SIZE - 1) >> IMAPAINT_TILE_BITS)
 
 int image_texture_paint_poll(struct bContext *C);
-void *image_undo_find_tile(struct Image *ima, struct ImBuf *ibuf, int x_tile, int y_tile, unsigned short **mask);
+void *image_undo_find_tile(struct Image *ima, struct ImBuf *ibuf, int x_tile, int y_tile, unsigned short **mask, bool validate);
 void *image_undo_push_tile(struct Image *ima, struct ImBuf *ibuf, struct ImBuf **tmpibuf, int x_tile, int y_tile);
 void image_undo_remove_masks(void);
 void image_undo_restore(struct bContext *C, struct ListBase *lb);
@@ -237,7 +237,6 @@
 void undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free);
 struct ListBase *undo_paint_push_get_list(int type);
 void undo_paint_push_count_alloc(int type, int size);
-void undo_paint_push_count_reset(int type);
 void undo_paint_push_end(int type);
 
 /* paint_hide.c */

Modified: branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_undo.c
===================================================================
--- branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_undo.c	2013-06-30 08:29:42 UTC (rev 57881)
+++ branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_undo.c	2013-06-30 09:55:29 UTC (rev 57882)
@@ -229,14 +229,6 @@
 		MeshUndoStack.current->undosize += size;
 }
 
-void undo_paint_push_count_reset(int type)
-{
-	if (type == UNDO_PAINT_IMAGE)
-		ImageUndoStack.current->undosize = 0;
-	else if (type == UNDO_PAINT_MESH)
-		MeshUndoStack.current->undosize = 0;
-}
-
 void undo_paint_push_end(int type)
 {
 	if (type == UNDO_PAINT_IMAGE)




More information about the Bf-blender-cvs mailing list