Beginner Questions About XCB - xcb_alloc_color is too slow

Sam Varshavchik sam.varshavchik at gmail.com
Sun Feb 21 03:08:21 UTC 2021


On Sat, Feb 20, 2021 at 9:37 PM Mark Solomonik
<marik.solomonik at gmail.com> wrote:
>
> Hello,
> I am trying to implement a 3D engine from scratch with XCB. To do this, I have to set the color of every individual pixel before drawing it.
>
> I noticed that calling xcb_alloc_color and xcb_alloc_color_reply are bottlenecking the program and making it slow to draw individual triangles.

I made the same mistake a while ago. AllocColor is superceded by the
RENDER protocol extension which, if you dig into it, you can find out
which bits in a 24 or a 32 bit pixel correspond to the
red/green/blue/alpha channels. AllocColor was used in prehistoric
times when dinosaurs roamed the earth and VGA hardware was able to
handle a maximum of 16, 32, or similar number of colors on screen, so
each window registered which handful of colors it wanted to use, and
each time you switched windows the X server loaded the window's colors
into the hardware registered that corresponded to each possible 4, 5,
or similar bits per pixel (and all other windows from other
applications changed into weird colors).

Those days are long gone. It's safe to assume that modern hardware
supports RENDER.

The bad news is that, when starting from scratch, it'll take at least
three months to pour through RENDER's documentation (that's a very
optimistic estimate, it took me much longer than that) before
everything soaks into your brain, and you "get it".

> Is this an appropriate way to set the color? Is there a more efficient way to do so?

This is as efficient as it gets. Each function call is a round-trip to
the X11 server.

> Slightly unrelated question: as someone completely new to Xorg and Linux graphics in general,
> how do you deal with the lack of documentation for XCB?

There's nothing to document. XCB is just a thin wrapper around the
underlying X11 protocol. Which explains why you are unhappy about the
performance of AllocColor, you did not realize that what this does is
send a request to the X server and wait for the response.

You need to find the documentation for whichever parts you want to
use. For the core X11 protocol it's

https://www.x.org/releases/X11R7.7/doc/xproto/x11protocol.html

You will find a curious pattern of X11 requests specified in this
documentation matching one-for-one with the xcb functions you're
already using, and their parameters. And then the light bulb over the
top of your head goes off.

The only thing that XCB really does on its own is the allocation of
XIDs, but that's not too difficult to figure out. RENDER is here:

https://www.x.org/releases/X11R7.7/doc/renderproto/renderproto.txt

The RENDER requests documented there will also match, bit by bit, with
the xcb_render* functions in xcb, and so on. Probably the stuff that
is the least match is all the setup stuff, which is where you will
find all the goodies related to the video hardware and how to decode
which bits in a pixel correspond to which channel. But once you find
one end of the thread in the header files the rest won't be hard to
unravel.


More information about the xorg mailing list