Search code examples
linuxx11shared-memoryxorgxcb

Accessing X11 window pixel data over shared memory


I am writing a program in which I need to access a X11 window's pixel data as efficiently as possible, ideally over shared memory, without needing to make XCB calls every time I want to refresh the pixel data. It's a similar idea as Xvfb's -shmem option, but I want to access per-window pixel data over shared memory, rather than the entire screen.

I can configure the X server in whatever way helps with this. The X server is running headless and not connected to real video hardware.

I am aware of the XShm and XComposite extensions. From what I can see, they each do only one half of what I need:

  • The XComposite extension moves the window pixel data to an offscreen buffer that holds the pixel data.
  • The XShm extension allows creating shared memory regions and pixmaps that can be updated from existing windows (but only on demand, and requiring memory copying, as far as I can tell).

Can I somehow combine these two extensions such that I can directly access the window pixel data from my own application, without needing to continuously ask the X server to copy pixel data?


Solution

  • Per comments, this is not really possible because the X server's memory model assumes that X11 window pixel data lives in video hardware memory, thus the concept of "shared memory" with regular processes doesn't necessarily make sense in this context. In my context, however, the X server is not running on real video hardware.

    The best solution I've found so far for my use-case is to use a multi-headed Xvfb instance with the -fbdir flag set to a directory within /dev/shm, then ensuring that each window is on its own screen that is sized to the window's size. Then, each screen maps to a window one-to-one, and each of them has a memory-mappable file under the directory pointed to by -fbdir.