#define _XOPEN_SOURCE 500 #include #include #include #include #include #include #include #include #include #include #include #include #include #define GUID_RGBA32 0x41424752 int main (int argc, char* argv[]) { int rgb_width = 320; int rgb_height = 200; int xv_port = -1; int adaptor; int i, j, ret, _x, _y; unsigned int _d, _w, _h; long secsb, secsa, frames; XvAdaptorInfo *ai; XvImage *rgb_image; unsigned int p_num_adaptors; Display *dpy; Window window, _dw; XSizeHints hint; XSetWindowAttributes xswa; XVisualInfo vinfo; int screen; unsigned long mask; XEvent event; GC gc; /** for shm */ int shmem_flag = 0; XShmSegmentInfo rgb_shminfo; adaptor = -1; dpy = XOpenDisplay(NULL); if (dpy == NULL) { printf("Cannot open Display.\n"); exit (-1); } screen = DefaultScreen(dpy); /** find best display */ if (XMatchVisualInfo(dpy, screen, 24, TrueColor, &vinfo)) { printf(" found 24bit TrueColor\n"); } else if (XMatchVisualInfo(dpy, screen, 16, TrueColor, &vinfo)) { printf(" found 16bit TrueColor\n"); } else if (XMatchVisualInfo(dpy, screen, 15, TrueColor, &vinfo)) { printf(" found 15bit TrueColor\n"); } else if (XMatchVisualInfo(dpy, screen, 8, PseudoColor, &vinfo)) { printf(" found 8bit PseudoColor\n"); } else if (XMatchVisualInfo(dpy, screen, 8, GrayScale, &vinfo)) { printf(" found 8bit GrayScale\n"); } else if (XMatchVisualInfo(dpy, screen, 8, StaticGray, &vinfo)) { printf(" found 8bit StaticGray\n"); } else if (XMatchVisualInfo(dpy, screen, 1, StaticGray, &vinfo)) { printf(" found 1bit StaticGray\n"); } else { printf("requires 16 bit display\n"); exit (-1); } hint.x = 1; hint.y = 1; hint.width = rgb_width; hint.height = rgb_height; hint.flags = PPosition | PSize; xswa.colormap = XCreateColormap(dpy, DefaultRootWindow(dpy), vinfo.visual, AllocNone); xswa.event_mask = StructureNotifyMask | ExposureMask; xswa.background_pixel = 0; xswa.border_pixel = 0; mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; window = XCreateWindow(dpy, DefaultRootWindow(dpy), 0, 0, rgb_width, rgb_height, 0, vinfo.depth, InputOutput, vinfo.visual, mask, &xswa); XStoreName(dpy, window, "firstxv"); XSetIconName(dpy, window, "firstxv"); XSelectInput(dpy, window, StructureNotifyMask); /** Map window */ XMapWindow(dpy, window); /** Wait for map. */ do { XNextEvent(dpy, &event); } while (event.type != MapNotify || event.xmap.event != window); if (XShmQueryExtension(dpy)) shmem_flag = 1; if (!shmem_flag) { printf("no shmem available.\n"); exit (-1); } ret = XvQueryAdaptors(dpy, DefaultRootWindow(dpy), &p_num_adaptors, &ai); if (ret != Success) { if (ret == XvBadExtension) printf("XvBadExtension returned at XvQueryExtension.\n"); else if (ret == XvBadAlloc) printf("XvBadAlloc returned at XvQueryExtension.\n"); else printf("other error happaned at XvQueryAdaptors.\n"); exit (-1); } if (p_num_adaptors > 0) { xv_port = ai[0].base_id; XvFreeAdaptorInfo(ai); } if (xv_port == -1) exit (0); gc = XCreateGC(dpy, window, 0, 0); if(XvSetPortAttribute(dpy, xv_port, XInternAtom(dpy, "XV_SET_DEFAULTS", False), 1) != Success) { printf("XvSetPortAttribute failed !\n"); exit (-1); } if(XvSetPortAttribute(dpy, xv_port, XInternAtom(dpy, "XV_ENCODING", False), GUID_RGBA32) != Success) { printf("XvSetPortAttribute failed !\n"); exit (-1); } rgb_image = XvShmCreateImage(dpy, xv_port, GUID_RGBA32, 0, rgb_width, rgb_height, &rgb_shminfo); rgb_shminfo.shmid = shmget(IPC_PRIVATE, rgb_image->data_size, IPC_CREAT | 0777); rgb_shminfo.shmaddr = rgb_image->data = shmat(rgb_shminfo.shmid, 0, 0); rgb_shminfo.readOnly = False; if (!XShmAttach(dpy, &rgb_shminfo)) { printf("XShmAttach failed !\n"); exit (-1); } for (i = 0; i < rgb_image->height; i++) { for (j = 0; j < rgb_image->width; j++) { uint32_t c = 0x000000; if(iheight / 3 && jwidth / 3) /* Top left square, fill with RED */ c = 0xff0000; else if(i>=rgb_image->height / 3 && i<2*rgb_image->height / 3 && j>=rgb_image->width / 3 && j<2*rgb_image->width / 3) /* Center square, fill with GREEN */ c = 0x00ff00; else if(i>=2*rgb_image->height / 3 && j>=2*rgb_image->width / 3) /* Bottom right square, fill with BLUE */ c = 0x0000ff; ((uint32_t *)rgb_image->data)[rgb_image->width*i + j] = c; } } while (1) { frames = secsa = secsb = 0; time(&secsa); while (frames < 200) { XGetGeometry(dpy, window, &_dw, &_x, &_y, &_w, &_h, &_d, &_d); XvShmPutImage(dpy, xv_port, window, gc, rgb_image, 0, 0, rgb_image->width, rgb_image->height, 0, 0, _w, _h, True); frames++; } time(&secsb); printf("%ld frames in %ld seconds; %.4f fps\n", frames, secsb-secsa, (double) frames/(secsb-secsa)); } return 0; }