code: plan9front

Download patch

ref: bf1dca5be2fda19dc84765ddbf39ae99376b8864
parent: 74cc358c2cfb4c50954a35cbcbeec4e67889f030
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Mon Feb 6 13:35:02 EST 2023

libmemdraw: don't return Buffer copy from calc functions

The calc functions get their buffers passed by value.
This is convenient as the code usually modifies the
buffers during iteration.

However, making ANOTHER copy (odst) and returning it
at the end is a bit silly. We already made a copy
when passing the arguments, and the caller of the
calc function can just reuse the copy it already has.

So changing the return type from Buffer to void.

--- a/sys/src/libmemdraw/draw.c
+++ b/sys/src/libmemdraw/draw.c
@@ -388,7 +388,7 @@
 typedef struct	Param	Param;
 typedef Buffer	Readfn(Param*, uchar*, int);
 typedef void	Writefn(Param*, uchar*, Buffer);
-typedef Buffer	Calcfn(Buffer, Buffer, Buffer, int, int, int);
+typedef void	Calcfn(Buffer, Buffer, Buffer, int, int, int);
 
 enum {
 	MAXBCACHE = 16
@@ -727,7 +727,7 @@
 		bsrc = rdsrc(&z->spar, z->spar.bufbase, srcy);
 		bmask = rdmask(&z->mpar, z->mpar.bufbase, masky);
 		bdst = rddst(&z->dpar, z->dpar.bufbase, dsty);
-		bdst = calc(bdst, bsrc, bmask, dx, isgrey, op);
+		calc(bdst, bsrc, bmask, dx, isgrey, op);
 		wrdst(&z->dpar, z->dpar.bytermin+dsty*z->dpar.bwidth, bdst);
 	}
 
@@ -735,15 +735,14 @@
 	return 1;
 }
 
-static Buffer
+static void
 alphacalc0(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
 {
-	USED(grey);
-	USED(op);
 	USED(b1);
 	USED(b2);
+	USED(grey);
+	USED(op);
 	memset(bdst.rgba, 0, dx*bdst.delta);
-	return bdst;
 }
 
 /*
@@ -790,15 +789,13 @@
 	return 1;
 }
 
-static Buffer
+static void
 alphacalc14(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
 {
-	Buffer obdst;
 	int fd, sadelta;
 	int i, sa, ma, q;
 	ulong t, t1;
 
-	obdst = bdst;
 	sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
 	q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
 
@@ -839,18 +836,15 @@
 		bmask.alpha += bmask.delta;
 		bsrc.alpha += sadelta;
 	}
-	return obdst;
 }
 
-static Buffer
+static void
 alphacalc2810(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
 {
-	Buffer obdst;
 	int fs, sadelta;
 	int i, ma, da, q;
 	ulong t, t1;
 
-	obdst = bdst;
 	sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
 	q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
 
@@ -893,18 +887,15 @@
 		bmask.alpha += bmask.delta;
 		bsrc.alpha += sadelta;
 	}
-	return obdst;
 }
 
-static Buffer
+static void
 alphacalc3679(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
 {
-	Buffer obdst;
 	int fs, fd, sadelta;
 	int i, sa, ma, da, q;
 	ulong t, t1;
 
-	obdst = bdst;
 	sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
 	q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
 
@@ -955,30 +946,27 @@
 		bmask.alpha += bmask.delta;
 		bsrc.alpha += sadelta;
 	}
-	return obdst;
 }
 
-static Buffer
+static void
 alphacalc5(Buffer bdst, Buffer b1, Buffer b2, int dx, int grey, int op)
 {
+	USED(bdst);
+	USED(b1);
+	USED(b2);
 	USED(dx);
 	USED(grey);
 	USED(op);
-	USED(b1);
-	USED(b2);
-	return bdst;
 }
 
-static Buffer
+static void
 alphacalc11(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
 {
-	Buffer obdst;
 	int fd, sadelta;
 	int i, sa, ma, q;
 	ulong t, t1;
 
 	USED(op);
-	obdst = bdst;
 	sadelta = bsrc.alpha == &ones ? 0 : bsrc.delta;
 	q = bsrc.delta == 4 && bdst.delta == 4 && chanmatch(&bdst, &bsrc);
 
@@ -1017,23 +1005,20 @@
 		bmask.alpha += bmask.delta;
 		bsrc.alpha += sadelta;
 	}
-	return obdst;
 }
 
 /*
 not used yet
 source and mask alpha 1
-static Buffer
+static void
 alphacalcS0(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
 {
-	Buffer obdst;
 	int i;
 
 	USED(op);
-	obdst = bdst;
 	if(bsrc.delta == bdst.delta){
 		memmove(bdst.rgba, bsrc.rgba, dx*bdst.delta);
-		return obdst;
+		return;
 	}
 	for(i=0; i<dx; i++){
 		if(grey){
@@ -1056,22 +1041,18 @@
 			bdst.alpha += bdst.delta;
 		}
 	}
-	return obdst;
 }
 */
 
 /* source alpha 1 */
-static Buffer
+static void
 alphacalcS(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
 {
-	Buffer obdst;
 	int fd;
 	int i, ma;
 	ulong t;
 
 	USED(op);
-	obdst = bdst;
-
 	for(i=0; i<dx; i++){
 		ma = *bmask.alpha;
 		fd = 255-ma;
@@ -1097,19 +1078,15 @@
 		}
 		bmask.alpha += bmask.delta;
 	}
-	return obdst;
 }
 
-static Buffer
+static void
 boolcalc14(Buffer bdst, Buffer b1, Buffer bmask, int dx, int grey, int op)
 {
-	Buffer obdst;
 	int i, ma, zero;
 
 	USED(b1);
 
-	obdst = bdst;
-
 	for(i=0; i<dx; i++){
 		ma = *bmask.alpha;
 		zero = ma ? op == DoutS : op == DinS;
@@ -1132,18 +1109,15 @@
 			bdst.alpha += bdst.delta;
 		}
 	}
-	return obdst;
 }
 
-static Buffer
+static void
 boolcalc236789(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
 {
-	Buffer obdst;
 	int fs, fd;
 	int i, ma, da, zero;
 	ulong t;
 
-	obdst = bdst;
 	zero = !(op&1);
 
 	for(i=0; i<dx; i++){
@@ -1187,16 +1161,13 @@
 			bdst.alpha += bdst.delta;
 		}
 	}
-	return obdst;
 }
 
-static Buffer
+static void
 boolcalc1011(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int grey, int op)
 {
-	Buffer obdst;
 	int i, ma, zero;
 
-	obdst = bdst;
 	zero = !(op&1);
 
 	for(i=0; i<dx; i++){
@@ -1233,7 +1204,6 @@
 			bdst.alpha += bdst.delta;
 		}
 	}
-	return obdst;
 }
 /*
  * Replicated cached scan line read.  Call the function listed in the Param,
@@ -1311,7 +1281,7 @@
 	}
 	dx -= n;
 	if(dx == 0)
-		return b;
+		goto done;
 
 	assert(x+i == p->img->r.max.x);
 
@@ -1338,15 +1308,13 @@
 		nbits -= depth;
 	}
 	dx -= n;
-	if(dx == 0)
-		return b;
-
-	assert(dx > 0);
-	/* now we have exactly one full scan line: just replicate the buffer itself until we are done */
-	ow = buf;
-	while(dx--)
-		*w++ = *ow++;
-
+	if(dx > 0){
+		/* now we have exactly one full scan line: just replicate the buffer itself until we are done */
+		ow = buf;
+		while(dx--)
+			*w++ = *ow++;
+	}
+done:
 	return b;
 }
 
@@ -1723,18 +1691,16 @@
 	return b;
 }
 
-static Buffer
+static void
 boolmemmove(Buffer bdst, Buffer bsrc, Buffer b1, int dx, int i, int o)
 {
 	USED(i);
 	USED(o);
 	USED(b1);
-	USED(bsrc);
 	memmove(bdst.red, bsrc.red, dx*bdst.delta);
-	return bdst;
 }
 
-static Buffer
+static void
 boolcopy8(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
 {
 	uchar *m, *r, *w, *ew;
@@ -1748,10 +1714,9 @@
 	for(; w < ew; w++,r++)
 		if(*m++)
 			*w = *r;
-	return bdst;	/* not used */
 }
 
-static Buffer
+static void
 boolcopy16(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
 {
 	uchar *m;
@@ -1766,10 +1731,9 @@
 	for(; w < ew; w++,r++)
 		if(*m++)
 			*w = *r;
-	return bdst;	/* not used */
 }
 
-static Buffer
+static void
 boolcopy24(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
 {
 	uchar *m;
@@ -1791,10 +1755,9 @@
 			r += 3;
 		}
 	}
-	return bdst;	/* not used */
 }
 
-static Buffer
+static void
 boolcopy32(Buffer bdst, Buffer bsrc, Buffer bmask, int dx, int i, int o)
 {
 	uchar *m;
@@ -1809,7 +1772,6 @@
 	for(; w < ew; w++,r++)
 		if(*m++)
 			*w = *r;
-	return bdst;	/* not used */
 }
 
 static Buffer