commit 08fc4868fc1b2ecf69caddbfd35b8bf895563801
parent 7238e384a8efc72cf0f9fbc7296c055583481536
Author: glenda <glenda@kobeni>
Date: Sat, 6 Jan 2024 20:49:32 +0000
src/noise: a graphics benchmark
Diffstat:
6 files changed, 163 insertions(+), 0 deletions(-)
diff --git a/src/noise/config.h b/src/noise/config.h
@@ -0,0 +1,6 @@
+#include "dnoise.h"
+
+Scene *plist[] = {
+ &DNoiseScene,
+ nil,
+};
diff --git a/src/noise/dnoise.c b/src/noise/dnoise.c
@@ -0,0 +1,56 @@
+/* simple/naive noise fill scene */
+
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+
+#include "noise.h"
+
+#include "dnoise.h"
+
+struct DNoise_State {
+ uchar *buf;
+ Image *Ibuf;
+ long n;
+ int f;
+} dns;
+
+void dn_init(void *)
+{
+ dns.n = Dx(screen->r) * Dy(screen->r) * sizeof(u32int);
+ dns.buf = malloc(dns.n);
+ dns.f = open("/dev/random", OREAD);
+ if (dns.buf == nil) sysfatal("dn_init failed to malloc buf: %r");
+ if (dns.f < 0) sysfatal("dn_init couldn't open /dev/random: %r");
+ lockdisplay(display);
+ dns.Ibuf = allocimage(display,
+ Rect(0, 0, Dx(screen->r), Dy(screen->r)), RGB24, 0, DBlack);
+ unlockdisplay(display);
+}
+
+void dn_draw(void *)
+{
+ read(dns.f, dns.buf, dns.n);
+ //loadimage(screen, screen->r, dns.buf, dns.n);
+ loadimage(dns.Ibuf, dns.Ibuf->r, dns.buf, dns.n);
+ draw(screen, screen->r, dns.Ibuf, nil, ZP);
+}
+
+void dn_stop(void *)
+{
+ close(dns.f);
+ dns.n = 0;
+ close(dns.f);
+ free(dns.buf);
+ dns.buf = nil;
+ lockdisplay(display);
+ freeimage(dns.Ibuf);
+ unlockdisplay(display);
+}
+
+Scene DNoiseScene = {
+ "dumb noise",
+ dn_init,
+ dn_draw,
+ dn_stop,
+ nil};
diff --git a/src/noise/dnoise.h b/src/noise/dnoise.h
@@ -0,0 +1 @@
+extern Scene DNoiseScene;
diff --git a/src/noise/main.c b/src/noise/main.c
@@ -0,0 +1,75 @@
+#include <u.h>
+#include <libc.h>
+#include <thread.h>
+#include <draw.h>
+#include <mouse.h>
+#include <cursor.h>
+
+#include "noise.h"
+
+#include "config.h"
+
+Scene *cs;
+
+void
+procdraw(void *v)
+{
+ Scene *sp = v;
+ threadsetname("draw-loop");
+ for (;;) {
+ lockdisplay(display);
+ if (sp->draw != nil) sp->draw(sp->aux);
+ flushimage(display, 1);
+ unlockdisplay(display);
+ yield();
+ }
+}
+
+void
+threadmain(int, char **)
+{
+ cs = plist[0];
+
+ int dtid;
+ Mousectl *mctl;
+ Mouse mouse;
+ int resize[2];
+ initdraw(nil, nil, "ШУМ");
+ display->locking = 1;
+ mctl = initmouse(nil, screen);
+
+ draw(screen, screen->r, display->black, nil, ZP);
+ flushimage(display, Refnone);
+ unlockdisplay(display);
+
+ if (cs->init != nil) cs->init(cs->aux);
+ dtid = proccreate(procdraw, cs, 8 * 1024);
+
+ Alt alts[4] = {
+ {mctl->c, &mouse, CHANRCV},
+ {mctl->resizec, resize, CHANRCV},
+ {nil, nil, CHANEND},
+ };
+ for (;;) {
+ switch(alt(alts)) {
+ case 0: // mouse
+ break;
+ case 1: // resize
+ threadkill(dtid);
+ lockdisplay(display);
+ if (getwindow(display, Refnone) < 0)
+ sysfatal("resize failed: %r");
+
+ draw(screen, screen->r, display->black, nil, ZP);
+ unlockdisplay(display);
+
+ if (cs->stop != nil) cs->stop(cs->aux);
+ if (cs->init != nil) cs->init(cs->aux);
+ dtid = proccreate(procdraw, cs, 8 * 1024);
+
+ break;
+ case 2: //noop
+ break;
+ }
+ }
+}
diff --git a/src/noise/mkfile b/src/noise/mkfile
@@ -0,0 +1,14 @@
+</$objtype/mkfile
+
+TARG=noise
+BIN=.
+OFILES=\
+ main.$O\
+ dnoise.$O\
+
+HFILES=\
+ noise.h\
+ dnoise.h\
+ config.h\
+
+</sys/src/cmd/mkone
diff --git a/src/noise/noise.h b/src/noise/noise.h
@@ -0,0 +1,11 @@
+typedef struct Scene Scene;
+
+struct Scene {
+ char *name;
+ void (*init)(void *);
+ void (*draw)(void *);
+ void (*stop)(void *);
+ void *aux;
+};
+
+// void drawfps(Image *);