X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fx11%2Fmain.c;h=8511a93bd1f8b21811d96517b1ff8c643972699f;hb=9a0d071a2bb88786abc90ff3f4f406e06a66f8e3;hp=a3cc050ca363f1190d88557028c32a9ad5c13cc2;hpb=baf481c2dc52bc0b5f54acb555ddd7701daf05cc;p=retrobench diff --git a/src/x11/main.c b/src/x11/main.c index a3cc050..8511a93 100644 --- a/src/x11/main.c +++ b/src/x11/main.c @@ -1,9 +1,17 @@ #include #include #include +#include +#include +#include +#include +#include #include #include +#include +#include #include "rbench.h" +#include "util.h" enum { QUIT = 1, REDRAW = 2 }; @@ -11,17 +19,43 @@ 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; static unsigned int pending; static Display *dpy; static Window win, root; +static GC gc; +static Visual *vis; static Atom xa_wm_proto, xa_wm_delwin; +static int no_wm; + +static XImage *ximg; +static XShmSegmentInfo shm; +static int wait_putimg; +static int xshm_ev_completion; + int main(int argc, char **argv) { + int num_frames = 0; XEvent ev; + struct timeval tv, tv0; + char *env; + + if((env = getenv("RBENCH_NO_WM"))) { + if(isdigit(env[0])) { + no_wm = atoi(env); + } else { + no_wm = 1; + } + } + + shm.shmid = -1; + shm.shmaddr = (void*)-1; + + signal(SIGINT, sig); read_config("rbench.cfg"); @@ -37,18 +71,77 @@ int main(int argc, char **argv) xa_wm_proto = XInternAtom(dpy, "WM_PROTOCOLS", 0); xa_wm_delwin = XInternAtom(dpy, "WM_DELETE_WINDOW", 0); + if(!XShmQueryExtension(dpy)) { + fprintf(stderr, "X shared memory extension is not available\n"); + XCloseDisplay(dpy); + return 1; + } + xshm_ev_completion = XShmGetEventBase(dpy) + ShmCompletion; + if(!(win = create_win(opt.width, opt.height, opt.bpp))) { return 1; } + gc = XCreateGC(dpy, win, 0, 0); + + if(!(ximg = XShmCreateImage(dpy, vis, opt.bpp, ZPixmap, 0, &shm, opt.width, opt.height))) { + fprintf(stderr, "failed to create shared memory image\n"); + goto end; + } + if((shm.shmid = shmget(IPC_PRIVATE, ximg->bytes_per_line * ximg->height, IPC_CREAT | 0777)) == -1) { + fprintf(stderr, "failed to create shared memory block\n"); + goto end; + } + if((shm.shmaddr = ximg->data = shmat(shm.shmid, 0, 0)) == (void*)-1) { + fprintf(stderr, "failed to attach shared memory block\n"); + goto end; + } + shm.readOnly = True; + if(!XShmAttach(dpy, &shm)) { + fprintf(stderr, "XShmAttach failed"); + goto end; + } + + fb_width = opt.width; + fb_height = opt.height; + 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); - for(;;) { - if(mapped) { + while(!(pending & QUIT)) { + if(mapped) {/* && !wait_putimg) { */ while(XPending(dpy)) { XNextEvent(dpy, &ev); handle_event(&ev); if(pending & QUIT) goto end; } - redraw(); + + 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(); + + XShmPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, ximg->width, ximg->height, False); + XSync(dpy, False); + /*wait_putimg = 1;*/ + } } else { XNextEvent(dpy, &ev); handle_event(&ev); @@ -57,8 +150,26 @@ int main(int argc, char **argv) } end: - XDestroyWindow(dpy, win); + cleanup(); + if(ximg) { + XShmDetach(dpy, &shm); + XDestroyImage(ximg); + if(shm.shmaddr != (void*)-1) { + shmdt(shm.shmaddr); + } + if(shm.shmid != -1) { + shmctl(shm.shmid, IPC_RMID, 0); + } + } + if(win) { + XFreeGC(dpy, gc); + XDestroyWindow(dpy, win); + } XCloseDisplay(dpy); + + if(num_frames) { + printf("avg framerate: %.1f fps\n", (10000 * num_frames / time_msec) / 10.0f); + } return 0; } @@ -66,7 +177,7 @@ static Window create_win(int width, int height, int bpp) { int scr, num_vis; Window win; - XVisualInfo *vis, vinf; + XVisualInfo *vinf, vtmpl; unsigned int vinf_mask; XSetWindowAttributes xattr; XTextProperty txname; @@ -75,24 +186,26 @@ static Window create_win(int width, int height, int bpp) scr = DefaultScreen(dpy); - vinf.screen = scr; - vinf.depth = bpp; - vinf.class = bpp <= 8 ? PseudoColor : TrueColor; + vtmpl.screen = scr; + vtmpl.depth = bpp; + vtmpl.class = bpp <= 8 ? PseudoColor : TrueColor; vinf_mask = VisualScreenMask | VisualDepthMask | VisualClassMask; - if(!(vis = XGetVisualInfo(dpy, vinf_mask, &vinf, &num_vis))) { + if(!(vinf = XGetVisualInfo(dpy, vinf_mask, &vtmpl, &num_vis))) { fprintf(stderr, "failed to find appropriate visual for %d bpp\n", bpp); return 0; } + vis = vinf->visual; - if(!(cmap = XCreateColormap(dpy, root, vis->visual, bpp <= 8 ? AllocAll : AllocNone))) { + if(!(cmap = XCreateColormap(dpy, root, vis, bpp <= 8 ? AllocAll : AllocNone))) { fprintf(stderr, "failed to allocate colormap\n"); return 0; } xattr.background_pixel = BlackPixel(dpy, scr); xattr.colormap = cmap; - win = XCreateWindow(dpy, root, 0, 0, width, height, 0, vis->depth, - InputOutput, vis->visual, CWColormap | CWBackPixel, &xattr); + xattr.override_redirect = no_wm ? True : False; + win = XCreateWindow(dpy, root, 0, 0, width, height, 0, vinf->depth, + InputOutput, vis, CWColormap | CWBackPixel | CWOverrideRedirect, &xattr); if(!win) return 0; XSelectInput(dpy, win, StructureNotifyMask | ExposureMask | KeyPressMask | @@ -157,6 +270,9 @@ static void handle_event(XEvent *ev) break; default: + if(ev->type == xshm_ev_completion) { + wait_putimg = 0; + } break; } } @@ -220,3 +336,8 @@ inval_arg: fprintf(stderr, "invalid argument: %s\n", argv[i]); } return 0; } + +static void sig(int s) +{ + pending |= QUIT; +}