started fbdev port
authorJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 19 May 2021 01:12:25 +0000 (04:12 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Wed, 19 May 2021 01:12:25 +0000 (04:12 +0300)
.gitignore
Makefile
src/fbdev/main.c [new file with mode: 0644]
src/x11/main.c

index e1839fe..4b1e283 100644 (file)
@@ -1,6 +1,8 @@
 *.o
 *.d
 *.swp
 *.o
 *.d
 *.swp
-rbench
+rbench_x11
+rbench_fbdev
+rbench.exe
 sinlut.s
 tools/lutgen
 sinlut.s
 tools/lutgen
index 4c1087e..cf9cd98 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,19 +1,30 @@
-src = $(wildcard src/*.c) $(wildcard src/x11/*.c)
+src = $(wildcard src/*.c)
+src_x11 = $(wildcard src/x11/*.c)
+src_fbdev = $(wildcard src/fbdev/*.c)
 ssrc = sinlut.s
 ssrc = sinlut.s
-obj = $(src:.c=.o) $(ssrc:.s=.o)
-dep = $(src:.c=.d)
-bin = rbench
+obj_x11 = $(src:.c=.o) $(src_x11:.c=.o) $(ssrc:.s=.o)
+obj_fbdev = $(src:.c=.o) $(src_fbdev:.c=.o) $(ssrc:.s=.o)
+dep = $(src:.c=.d) $(src_x11:.c=.d) $(src_fbdev:.c=.d)
+bin_x11 = rbench_x11
+bin_fbdev = rbench_fbdev
 
 warn = -pedantic -Wall -Wno-deprecated-declarations
 dbg = -g
 opt = -O3 -ffast-math
 inc = -Isrc
 
 
 warn = -pedantic -Wall -Wno-deprecated-declarations
 dbg = -g
 opt = -O3 -ffast-math
 inc = -Isrc
 
-CFLAGS = -pedantic $(warn) $(dbg) $(opt) $(inc) -MMD
-LDFLAGS = -L/usr/X11R6/lib -lX11 -lXext -lm
+CFLAGS = -pedantic $(warn) $(dbg) $(opt) $(inc) -fno-strict-aliasing -MMD
+LDFLAGS_x11 = -L/usr/X11R6/lib -lX11 -lXext
+LDFLAGS_fbdev =
 
 
-$(bin): $(obj)
-       $(CC) -o $@ $(obj) $(LDFLAGS)
+.PHONY: all
+all: $(bin_x11) $(bin_fbdev)
+
+$(bin_x11): $(obj_x11)
+       $(CC) -o $@ $(obj_x11) $(LDFLAGS_x11)
+
+$(bin_fbdev): $(obj_fbdev)
+       $(CC) -o $@ $(obj_fbdev) $(LDFLAGS_fbdev)
 
 sinlut.s: tools/lutgen
        tools/lutgen >$@
 
 sinlut.s: tools/lutgen
        tools/lutgen >$@
@@ -22,7 +33,7 @@ sinlut.s: tools/lutgen
 
 .PHONY: clean
 clean:
 
 .PHONY: clean
 clean:
-       $(RM) $(obj) $(bin)
+       $(RM) $(obj_x11) $(obj_fbdev) $(bin_x11) $(bin_fbdev)
 
 .PHONY: cleandep
 cleandep:
 
 .PHONY: cleandep
 cleandep:
diff --git a/src/fbdev/main.c b/src/fbdev/main.c
new file mode 100644 (file)
index 0000000..3c5def6
--- /dev/null
@@ -0,0 +1,160 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <signal.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/time.h>
+#include <sys/mman.h>
+#include "rbench.h"
+#include "util.h"
+
+
+static int init_fbdev(int xsz, int ysz, int bpp);
+static void close_fbdev(void);
+static int parse_args(int argc, char **argv);
+
+static int fbfd = -1;
+static int fbsz;
+static void *fbmem;
+
+
+int main(int argc, char **argv)
+{
+       int num_frames = 0;
+       struct timeval tv, tv0;
+
+       read_config("rbench.cfg");
+
+       if(parse_args(argc, argv) == -1) {
+               return 1;
+       }
+
+       if(init_fbdev(opt.width, opt.height, opt.bpp) == -1) {
+               return 1;
+       }
+
+       fb_width = opt.width;
+       fb_height = opt.height;
+       fb_bpp = opt.bpp;
+       fb_pitch = opt.width * opt.bpp / 8;
+
+       if(!(framebuf = malloc(fb_pitch * fb_height))) {
+               fprintf(stderr, "failed to allocate %dx%d (%d bpp) framebuffer\n",
+                               fb_width, fb_height, fb_bpp);
+               return 1;
+       }
+       /*
+       fb_rmask = ximg->red_mask;
+       fb_gmask = ximg->green_mask;
+       fb_bmask = ximg->blue_mask;
+       fb_rshift = mask_to_shift(fb_rmask);
+       fb_gshift = mask_to_shift(fb_gmask);
+       fb_bshift = mask_to_shift(fb_bmask);
+       */
+
+       if(init() == -1) {
+               goto end;
+       }
+
+       /* TODO: set terminal raw and disable cursor */
+
+       gettimeofday(&tv0, 0);
+
+       for(;;) {
+               /* TODO read input */
+
+               gettimeofday(&tv, 0);
+               time_msec = (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
+               num_frames++;
+
+               redraw();
+
+               /* TODO copy to fb */
+       }
+
+end:
+       cleanup();
+       close_fbdev();
+
+       if(num_frames) {
+               printf("avg framerate: %.1f fps\n", (10000 * num_frames / time_msec) / 10.0f);
+       }
+       return 0;
+}
+
+int init_fbdev(int xsz, int ysz, int bpp)
+{
+       if((fbfd = open("/dev/fb0", O_RDWR)) == -1) {
+               perror("failed to open framebuffer device");
+               return -1;
+       }
+
+       /* TODO modeset ioctl */
+
+       fbsz = xsz * ysz * bpp / 8;     /* XXX */
+
+       if((fbmem = mmap(0, fbsz, PROT_WRITE, MAP_SHARED, fbfd, 0)) == (void*)-1) {
+               perror("failed to map framebuffer");
+               return -1;
+       }
+
+       return 0;
+}
+
+void close_fbdev(void)
+{
+       if(fbmem) {
+               munmap(fbmem, fbsz);
+       }
+       if(fbfd >= 0) {
+               close(fbfd);
+       }
+}
+
+static const char *usage_str =
+       "Usage: %s [options]\n"
+       "Options:\n"
+       "  -s <WxH>: resolution\n"
+       "  -b <bpp>: color depth\n"
+       "  -h: print usage and exit\n";
+
+static int parse_args(int argc, char **argv)
+{
+       int i;
+
+       for(i=1; i<argc; i++) {
+               if(argv[i][0] == '-') {
+                       if(argv[i][2]) {
+                               goto inval_arg;
+                       }
+                       switch(argv[i][1]) {
+                       case 's':
+                               if(!argv[++i] || sscanf(argv[i], "%dx%d", &opt.width, &opt.height) != 2) {
+                                       fprintf(stderr, "-s must be followed by WxH\n");
+                                       return -1;
+                               }
+                               break;
+
+                       case 'b':
+                               if(!argv[++i] || !(opt.bpp = atoi(argv[i]))) {
+                                       fprintf(stderr, "-b must be followed by the color depth\n");
+                                       return -1;
+                               }
+                               break;
+
+                       case 'h':
+                               printf(usage_str, argv[0]);
+                               exit(0);
+
+                       default:
+                               goto inval_arg;
+                       }
+               } else {
+inval_arg:     fprintf(stderr, "invalid argument: %s\n", argv[i]);
+                       return -1;
+               }
+       }
+       return 0;
+}
index 59b1765..8511a93 100644 (file)
@@ -138,8 +138,9 @@ int main(int argc, char **argv)
 
                                redraw();
 
 
                                redraw();
 
-                               XShmPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, ximg->width, ximg->height, True);
-                               wait_putimg = 1;
+                               XShmPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, ximg->width, ximg->height, False);
+                               XSync(dpy, False);
+                               /*wait_putimg = 1;*/
                        }
                } else {
                        XNextEvent(dpy, &ev);
                        }
                } else {
                        XNextEvent(dpy, &ev);