override redirect, obey ximage rgb masks, restore completion event
authorJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 17 May 2021 05:44:49 +0000 (08:44 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Mon, 17 May 2021 05:44:49 +0000 (08:44 +0300)
GNUmakefile
src/rbench.c
src/rbench.h
src/util.c [new file with mode: 0644]
src/util.h [new file with mode: 0644]
src/x11/main.c

index 450b272..d2c211c 100644 (file)
@@ -5,11 +5,11 @@ bin = rbench
 
 warn = -pedantic -Wall -Wno-deprecated-declarations
 dbg = -g
-#opt = -O3 -ffast-math
+opt = -O3 -ffast-math
 inc = -Isrc
 
 CFLAGS = -pedantic $(warn) $(dbg) $(opt) $(inc) -MMD
-LDFLAGS = -lX11 -lXext -lm
+LDFLAGS = -L/usr/X11R6/lib -lX11 -lXext -lm
 
 $(bin): $(obj)
        $(CC) -o $@ $(obj) $(LDFLAGS)
index 005013c..3d8eace 100644 (file)
@@ -14,11 +14,16 @@ struct options opt = {
 };
 
 int fb_width, fb_height, fb_bpp, fb_pitch;
+int fb_rshift, fb_gshift, fb_bshift;
+unsigned int fb_rmask, fb_gmask, fb_bmask;
 void *framebuf;
 unsigned int time_msec;
 
 int init(void)
 {
+       printf("initialized graphics %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
+       printf("  rgb mask: %x %x %x\n", fb_rmask, fb_gmask, fb_bmask);
+       printf("  rgb shift: %d %d %d\n", fb_rshift, fb_gshift, fb_bshift);
        return 0;
 }
 
@@ -48,22 +53,14 @@ void redraw(void)
 
        switch(fb_bpp) {
        case 15:
-               fbptr16 = framebuf;
-               for(i=0; i<fb_height; i++) {
-                       for(j=0; j<fb_width; j++) {
-                               XORRGB(j + xoffs, i + yoffs, zoom, r, g, b);
-                               *fbptr16++ = ((r & 0x1f) << 10) | ((g & 0x1f) << 5) | (b & 0x1f);
-                       }
-                       fbptr16 += (fb_pitch >> 1) - fb_width;
-               }
-               break;
-
        case 16:
                fbptr16 = framebuf;
                for(i=0; i<fb_height; i++) {
                        for(j=0; j<fb_width; j++) {
                                XORRGB(j + xoffs, i + yoffs, zoom, r, g, b);
-                               *fbptr16++ = ((r & 0x1f) << 11) | ((g & 0x3f) << 5) | (b & 0x1f);
+                               *fbptr16++ = (((r >> 3) << fb_rshift) & fb_rmask) |
+                                       (((g >> 2) << fb_gshift) & fb_gmask) |
+                                       (((b >> 3) << fb_bshift) & fb_bmask);
                        }
                        fbptr16 += (fb_pitch >> 1) - fb_width;
                }
@@ -87,7 +84,9 @@ void redraw(void)
                for(i=0; i<fb_height; i++) {
                        for(j=0; j<fb_width; j++) {
                                XORRGB(j + xoffs, i + yoffs, zoom, r, g, b);
-                               *fbptr32++ = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
+                               *fbptr32++ = (((r) << fb_rshift) & fb_rmask) |
+                                       (((g) << fb_gshift) & fb_gmask) |
+                                       (((b) << fb_bshift) & fb_bmask);
                        }
                        fbptr32 += (fb_pitch >> 2) - fb_width;
                }
index 8efb0d1..c48d6f1 100644 (file)
@@ -7,6 +7,8 @@ struct options {
 
 extern struct options opt;
 extern int fb_width, fb_height, fb_bpp, fb_pitch;
+extern int fb_rshift, fb_gshift, fb_bshift;
+extern unsigned int fb_rmask, fb_gmask, fb_bmask;
 extern void *framebuf;
 extern unsigned int time_msec;
 
diff --git a/src/util.c b/src/util.c
new file mode 100644 (file)
index 0000000..82a8bfc
--- /dev/null
@@ -0,0 +1,13 @@
+#include "util.h"
+
+int mask_to_shift(unsigned int mask)
+{
+       int s = 0;
+       if(mask) {
+               while(!(mask & 1)) {
+                       mask >>= 1;
+                       s++;
+               }
+       }
+       return s;
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644 (file)
index 0000000..001aedc
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef UTIL_H_
+#define UTIL_H_
+
+int mask_to_shift(unsigned int mask);
+
+#endif /* UTIL_H_ */
index 036e765..c1f4b87 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <signal.h>
 #include <sys/time.h>
 #include <sys/ipc.h>
 #include <sys/shm.h>
@@ -9,6 +10,7 @@
 #include <X11/keysym.h>
 #include <X11/extensions/XShm.h>
 #include "rbench.h"
+#include "util.h"
 
 enum { QUIT = 1, REDRAW = 2 };
 
@@ -16,6 +18,7 @@ static Window create_win(int width, int height, int bpp);
 static void handle_event(XEvent *ev);
 static int translate_keysym(KeySym sym);
 static int parse_args(int argc, char **argv);
+static void sig(int s);
 
 static int win_width, win_height;
 static int mapped;
@@ -33,13 +36,15 @@ static int xshm_ev_completion;
 
 int main(int argc, char **argv)
 {
-       int num_frames;
+       int num_frames = 0;
        XEvent ev;
        struct timeval tv, tv0;
 
        shm.shmid = -1;
        shm.shmaddr = (void*)-1;
 
+       signal(SIGINT, sig);
+
        read_config("rbench.cfg");
 
        if(parse_args(argc, argv) == -1) {
@@ -86,18 +91,27 @@ int main(int argc, char **argv)
 
        fb_width = opt.width;
        fb_height = opt.height;
-       fb_bpp = opt.bpp >= 24 ? 32 : opt.bpp;
+       if(opt.bpp >= 24) {
+               fb_bpp = ximg->bytes_per_line < fb_width * 4 ? 24 : 32;
+       } else {
+               fb_bpp = opt.bpp;
+       }
        framebuf = ximg->data;
        fb_pitch = ximg->bytes_per_line;
+       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;
        }
 
        gettimeofday(&tv0, 0);
-       num_frames = 0;
 
-       for(;;) {
+       while(!(pending & QUIT)) {
                if(mapped) {/* && !wait_putimg) { */
                        while(XPending(dpy)) {
                                XNextEvent(dpy, &ev);
@@ -105,14 +119,16 @@ int main(int argc, char **argv)
                                if(pending & QUIT) goto end;
                        }
 
-                       gettimeofday(&tv, 0);
-                       time_msec = (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
-                       num_frames++;
+                       if(!wait_putimg) {
+                               gettimeofday(&tv, 0);
+                               time_msec = (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
+                               num_frames++;
 
-                       redraw();
+                               redraw();
 
-                       XShmPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, ximg->width, ximg->height, False);
-                       /*wait_putimg = 1;*/
+                               XShmPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, ximg->width, ximg->height, True);
+                               wait_putimg = 1;
+                       }
                } else {
                        XNextEvent(dpy, &ev);
                        handle_event(&ev);
@@ -174,8 +190,9 @@ static Window create_win(int width, int height, int bpp)
 
        xattr.background_pixel = BlackPixel(dpy, scr);
        xattr.colormap = cmap;
+       xattr.override_redirect = True;
        win = XCreateWindow(dpy, root, 0, 0, width, height, 0, vinf->depth,
-                       InputOutput, vis, CWColormap | CWBackPixel, &xattr);
+                       InputOutput, vis, CWColormap | CWBackPixel | CWOverrideRedirect, &xattr);
        if(!win) return 0;
 
        XSelectInput(dpy, win, StructureNotifyMask | ExposureMask | KeyPressMask |
@@ -306,3 +323,8 @@ inval_arg:  fprintf(stderr, "invalid argument: %s\n", argv[i]);
        }
        return 0;
 }
+
+static void sig(int s)
+{
+       pending |= QUIT;
+}