From 9328fd29e99532f6d603dc005f57560ac6b40362 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Wed, 19 May 2021 04:12:25 +0300 Subject: [PATCH] started fbdev port --- .gitignore | 4 +- Makefile | 29 +++++++--- src/fbdev/main.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/x11/main.c | 5 +- 4 files changed, 186 insertions(+), 12 deletions(-) create mode 100644 src/fbdev/main.c diff --git a/.gitignore b/.gitignore index e1839fe..4b1e283 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ *.o *.d *.swp -rbench +rbench_x11 +rbench_fbdev +rbench.exe sinlut.s tools/lutgen diff --git a/Makefile b/Makefile index 4c1087e..cf9cd98 100644 --- 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 -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 -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 >$@ @@ -22,7 +33,7 @@ sinlut.s: tools/lutgen .PHONY: clean clean: - $(RM) $(obj) $(bin) + $(RM) $(obj_x11) $(obj_fbdev) $(bin_x11) $(bin_fbdev) .PHONY: cleandep cleandep: diff --git a/src/fbdev/main.c b/src/fbdev/main.c new file mode 100644 index 0000000..3c5def6 --- /dev/null +++ b/src/fbdev/main.c @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 : resolution\n" + " -b : color depth\n" + " -h: print usage and exit\n"; + +static int parse_args(int argc, char **argv) +{ + int i; + + for(i=1; iwidth, 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); -- 1.7.10.4