Advertisement
Guest User

Clemens Eisserer

a guest
Jan 2nd, 2009
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <X11/Xlib.h>
  3. #include <X11/extensions/Xrender.h>
  4. #include <X11/extensions/render.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <stdlib.h>
  8. #include <sys/time.h>
  9.  
  10. #include <xcb/xcb.h>
  11. #include <X11/Xlib-xcb.h>
  12.  
  13. #include <sys/uio.h>
  14.  
  15. Display *display;
  16. int screen;
  17. Window root, window;
  18. XEvent event;
  19.  
  20. Pixmap maskPixmap;
  21. GC maskGC;
  22. Picture maskPicture;
  23.  
  24. Picture sourcePicture;
  25. int RENDER;
  26.  
  27.  
  28. void createMaskTile()
  29. {
  30.     XRenderPictFormat *fmt = XRenderFindStandardFormat(display, PictStandardA8);
  31.     maskPixmap = XCreatePixmap(display, window, 256, 256, 8);
  32.  
  33.         XGCValues values;
  34.         maskGC = XCreateGC(display, maskPixmap, 0, &values);
  35.  
  36.     XRenderPictureAttributes pict_attr;
  37.     maskPicture = XRenderCreatePicture(display, maskPixmap, fmt, 0, &pict_attr);
  38.         XRenderColor color_black={.red=0, .green=0, .blue=0, .alpha=0xffff};
  39.         XRenderFillRectangle (display, PictOpClear, maskPicture, &color_black, 0, 0, 512, 512);
  40. }
  41.  
  42. Picture createPicture()
  43. {
  44.     XRenderPictFormat *fmt = XRenderFindStandardFormat(display, PictStandardRGB24);
  45.     Pixmap sourcePixmap = XCreatePixmap(display, window, 2, 2, 24);
  46.     XRenderPictureAttributes pict_attr;
  47.     pict_attr.repeat=RepeatNone;
  48.     Picture picture = XRenderCreatePicture(display, sourcePixmap, fmt, CPRepeat, &pict_attr);
  49.  
  50.         XRenderColor yellow={.red=0xffff, .green=0xffff, .blue=0, .alpha=0xffff};
  51.         XRenderFillRectangle (display, PictOpSrc, picture, &yellow, 0, 0, 1000, 1000);
  52.      
  53.     return picture;
  54. }
  55.  
  56. static void return_socket(void *closure)
  57. {
  58.   printf("return_socket called!\n");
  59.   fflush(stdout);
  60. }
  61.  
  62. int putMask(int width, int height, char* data) {
  63.  
  64.         int paddedWidth = (int) ceil(width / 4.0) * 4;
  65.     int imgDataLengthX = (int) ceil(paddedWidth * height / 4.0);
  66.     int imgBytes = imgDataLengthX * 4;
  67.  
  68.         unsigned int* req_i = (unsigned int*) data;
  69.         unsigned short* req_s = (unsigned short*) data;
  70.         unsigned char* req_b = (unsigned char*) data;
  71.  
  72.         req_b[0] = 72; //PutImage
  73.         req_b[1] = 2; //ZPixmap
  74.         req_s[1] = 6 + imgDataLengthX;  //Request-length
  75.  
  76.         req_i[1] = maskPixmap;
  77.         req_i[2] = XGContextFromGC(maskGC);
  78.         req_s[6] = width;
  79.         req_s[7] = height;
  80.         req_s[8] = 0;
  81.         req_s[9] = 0;
  82.  
  83.         req_b[20] = 0; //LeftPad
  84.         req_b[21] = 8; //Depth
  85.  
  86.         req_b[22] = 0; //Pad
  87.         req_b[23] = 0;
  88.  
  89.         return (6*4 + imgBytes);
  90. }
  91.  
  92. int fillRect(Picture dst, char* data) {
  93.         unsigned int* req_i = (unsigned int*) data;
  94.         unsigned short* req_s = (unsigned short*) data;
  95.         unsigned char* req_b = (unsigned char*) data;
  96.        
  97.         req_b[0] = RENDER;
  98.         req_b[1] = 26; //FillRectangles-Opcode
  99.         req_s[1] = 9;  //Request-length
  100.  
  101.         req_b[4] = 3; //Over
  102.        
  103.         req_i[2] = dst; //TODO: Wie xid von picture rausfinden
  104.  
  105.         req_s[6] = 0xffff; //green
  106.         req_s[7] = 0xffff; //Red
  107.         req_s[8] = 0;      //Blue
  108.         req_s[9] = 0xffff; //A
  109.  
  110.         req_s[10] = 100;
  111.         req_s[11] = 100;
  112.         req_s[12] = 100;
  113.         req_s[13] = 100;
  114.  
  115.         req_s[14] = 200;
  116.         req_s[15] = 200;
  117.         req_s[16] = 10;
  118.         req_s[17] = 10;
  119.  
  120.         return 36;
  121. }
  122.  
  123. int main(int argc, char *argv[])
  124. {
  125.         /*Bosing initialization stuff*/
  126.     display=XOpenDisplay(NULL);
  127.    
  128.     int render_event_base, render_error_base;
  129.     int render_present=XRenderQueryExtension(display, &render_event_base, &render_error_base);
  130.     if (!render_present) {
  131.         fprintf(stderr, "RENDER extension missing!\n");
  132.         abort();
  133.     }
  134.    
  135.     XRenderPictFormat *fmt=XRenderFindStandardFormat(display, PictStandardRGB24);
  136.     screen=DefaultScreen(display);
  137.     root=DefaultRootWindow(display);
  138.    
  139.     window = XCreateWindow(display, root, 0, 0, 640, 480, 0,
  140.         DefaultDepth(display, screen), InputOutput,
  141.         DefaultVisual(display, screen),
  142.         0, NULL);
  143.     XRenderPictureAttributes pict_attr;
  144.    
  145.     pict_attr.poly_edge=PolyEdgeSmooth;
  146.     pict_attr.poly_mode=PolyModeImprecise;
  147.     Picture picture=XRenderCreatePicture(display, window, fmt, CPPolyEdge|CPPolyMode, &pict_attr);
  148.    
  149.     XSelectInput(display, window, KeyPressMask|KeyReleaseMask|ExposureMask
  150.         |ButtonPressMask|StructureNotifyMask);
  151.    
  152.    
  153.     /* now make the window visible */
  154.     XMapWindow(display, window);
  155.    
  156.     XRenderColor color_white = {.red=0xffff, .green=0xffff, .blue=0xffff, .alpha=0xffff};
  157.    
  158.         sourcePicture = createPicture();
  159.         createMaskTile();
  160.  
  161.        /*Fetch the RENDER extension's opcode*/
  162.        char renderName[] = RENDER_NAME;
  163.        int x;
  164.        XQueryExtension(display, renderName , &RENDER, &x, &x);
  165.  
  166.        xcb_connection_t* con = XGetXCBConnection(display);
  167.  
  168.        char* buffer = (char*) malloc(32000);
  169.  
  170.     while(1) {
  171.         XNextEvent(display, &event);
  172.        
  173.         switch(event.type) {
  174.             case Expose:
  175. {
  176.                 XRenderFillRectangle(display, PictOpOver,
  177.                     picture, &color_white, 0, 0, 1000, 1000);
  178.  
  179.     int x = 0;
  180.     for(x=0; x < 1000; x++) {
  181.         int flags = 0;
  182.     uint64_t sent;
  183.         xcb_take_socket(con, &return_socket, &flags, flags, &sent);
  184.        // printf("sent: %d\n", sent);fflush(stdout);
  185.  
  186.         int requests=0;
  187.         int written = 0;
  188.         for(requests = 0; requests < 50; requests++) {
  189.           written += fillRect(picture, &buffer[written]);
  190.         }
  191.  
  192.         struct iovec vect;
  193.         vect.iov_base = buffer;
  194.         vect.iov_len = written;
  195.  
  196.         xcb_writev(con, &vect, 1, requests);
  197.     }
  198.  
  199.  
  200.                 break;
  201.             case DestroyNotify:
  202.                 return 0;
  203.         }
  204.     }
  205.    
  206. }
  207.     return 0;
  208. }
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement