Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XGetImage takes a lot of time to run

Tags:

c

linux

x11

xlib

XGetImage takes 3-4 seconds to execute and completely freezes X11

Display *display;
    display = XOpenDisplay(NULL);
if (!display) {fprintf(stderr, "unable to connect to display");return 7;}
    Window w;
    int x,y,i;
    unsigned m;
    Window root = XDefaultRootWindow(display);
if (!root) {fprintf(stderr, "unable to open rootwindow");return 8;}
    //sleep(1);
    if(!XQueryPointer(display,root,&root,&w,&x,&y,&i,&i,&m))
{  printf("unable to query pointer\n"); return 9;}
    XImage *image;
    XWindowAttributes attr;
    XGetWindowAttributes(display, root, &attr);
    image = XGetImage(display,root,0,0,attr.width,attr.height,AllPlanes,XYPixmap);
    XCloseDisplay(display);
if (!image) {printf("unable to get image\n"); return 10;}

In the Xorg log:

[ 13234.693] AUDIT: Thu Jan  7 20:12:13 2016: 3856: client 45 connected from local host ( uid=500 gid=500 pid=12993 )
  Auth name: MIT-MAGIC-COOKIE-1 ID: 153
[ 13238.774] AUDIT: Thu Jan  7 20:12:18 2016: 3856: client 45 disconnected

time:
real    0m4.080s
user    0m0.002s
sys 0m0.007s

Ideally I want this function to run in less than 0.1 seconds

like image 621
Adel Ahmed Avatar asked Mar 13 '23 16:03

Adel Ahmed


1 Answers

XYPixmap is a very specialized format that doesn't have many uses. You should use ZPixmap nearly always.

XYPixmap works plane by plane. What does it mean? Take bit 0 of every pixel, and tightly pack all these bits in an array of unsigned int. That's youir plane 0. Then take bit 1 of every pixel, and pack all these bits in an array. That's your plane 1. Then take bit 2 of every pixel...

   Framebuffer
   __________________________________________________________________
  / 
   Pixel 0                     Pixel 1                      Pixel 2
   [0][1][2][3][4][5][6][7]    [0][1][2][3][4][5][6][7]     [0][1][2]....
    |                           |                            |
    |  +------------------------+                            |
    |  |                                                     |
    |  |  +--------------------------------------------------+
    |  |  |
    v  v  v
   [0][0][0]..... \
   (Plane 0)      |
                  |
   [1][1][1]....  | Result
   (Plane 1)      |
   ....           |
   [7][7][7]....  |
   (Plane 7)      |
                  /

If your framebuffer is stored like this, which is the case for most modern hardware, that's a lot of bit manipulation!

The picture shows 8 bit pixels, but it's the same for any other depth.

ZPixmap on the other hand takes entire pixels and stuffs them into an array:

   Framebuffer
   __________________________________________________________________
  / 
   Pixel 0                     Pixel 1                      Pixel 2
   [0][1][2][3][4][5][6][7]    [0][1][2][3][4][5][6][7]     [0][1][2]....
    |  |  |  |  |  |  |  |      |  |  |  |  |  |  |  |       |  |  |
    v  v  v  v  v  v  v  v      v  v  v  v  v  v  v  v       v  v  v
   [0][1][2][3][4][5][6][7]    [0][1][2][3][4][5][6][7]     [0][1][2]....
  \_____________________________________________________________________
    Result

This is simple direct copying, which should be very fast.

like image 168
n. 1.8e9-where's-my-share m. Avatar answered Mar 19 '23 10:03

n. 1.8e9-where's-my-share m.