noise

experiments with video performance in plan9
Log | Files | Refs

noise2.c (1107B)


      1 /* attempt at optimisation */
      2 
      3 #include <u.h>
      4 #include <libc.h>
      5 
      6 #include <thread.h>
      7 #include <draw.h>
      8 #include <mouse.h>
      9 
     10 #include "noise.h"
     11 
     12 Image *Ibuf;
     13 long bn;
     14 u8int *buf;
     15 
     16 void
     17 resize(void)
     18 {
     19 	freeimage(Ibuf);
     20 	Ibuf = allocimage(display, screen->r, GREY8, 0, DBlack);
     21 	bn = Dx(Ibuf->r) * Dy(Ibuf->r);
     22 	buf = realloc(buf, bn);
     23 }
     24 
     25 void
     26 procread(void *v)
     27 {
     28 	int rd;
     29 	Channel *c;
     30 	c = v;
     31 	rd = open("/dev/random", OREAD);
     32 	while(1){
     33 		readn(rd, buf, bn);
     34 		if (send(c, nil) < 0) break;
     35 	};
     36 	close(rd);
     37 }
     38 
     39 void
     40 threadmain(int, char**)
     41 {
     42 	Mousectl *mctl;
     43 	Image *It;
     44 	Channel *c;
     45 	initdraw(nil, nil, "noise");
     46 	mctl = initmouse(0, screen);
     47 	It = allocimage(display, Rect(0,0,1,1), RGB24, 1, DYellow);
     48 	resize();
     49 	c = chancreate(sizeof(int), 0);
     50 	proccreate(procread, c, 8 * 1024);
     51 	for(;;){
     52 		if (nbrecv(mctl->resizec, nil) != 0) {
     53 			if (getwindow(display, Refnone) < 0)
     54 				sysfatal("resize failed: %r");
     55 			resize();
     56 		}
     57 		while(nbrecv(mctl->c, nil) != 0);
     58 		recv(c, nil);
     59 		loadimage(Ibuf, Ibuf->r, buf, bn);
     60 		draw(screen, screen->r, Ibuf, 0, Ibuf->r.min);
     61 		drawfps(It);
     62 		flushimage(display, 1);
     63 	}
     64 }