Visuals

Visuals are a very important concept that is often overlooked. Roughly, a visual defines the memory representation that a piece of hardware uses to store the contents of an image. X supports different kinds of visuals to suit the different kinds of hardware out there.

There are three basic kinds of visuals, each divided into two classes:

These are the six visual classes supported by X (static gray, grayscale, static color, pseudo color, true color, direct color). Why not deal directly with RGB triplets all the time, you may ask? Indeed that would make programming easier. The reason is that not all hardware thinks in terms of RGB triplets, and X acts close to the metal in this respect, so we are forced to deal with it. Fortunately, this is not too hard to do.

To remind yourself of what the different visual classes mean, think in terms of static gray, static color, and true color having read-only intensity mappings, and grayscale, pseudo color, and direct color having read/write mappings.

In addition to the visual class, each visual has a bit depth. This is the number of significant bits that are used to encode the value of every pixel. Most 256-color PC video cards operate on an 8-bit pseudocolor visual. Better video cards operate on 24-bit true color visuals, with eight bits of information per channel. Some Amiga video cards operate on 12-bit pseudocolor visuals, which leads to a palette of 4096 indexed colors. Some exotic hardware uses 8-bit truecolor visuals, using 3/3/2 bits for the RGB channels, respectively.

The best way to know about the visual types your hardware supports is to run the xdpyinfo command. You will get a load of interesting information.

As we mentioned in the previous section, one of the properties of a drawable is its visual class. This defines the image format that must be used to paint stuff on the drawable.

Most low-end hardware supports a single visual class that is used for all the drawables. For example, a SVGA card may run either in 8-bit pseudocolor or 24-bit truecolor; it is one or the other but not both at the same time.

Higher-end hardware, however, may support different simultaneous visual types. For example, many Sun and SGI video cards can handle 8-bit pseudocolor and 24-bit truecolor visuals simultaneously. This means you can operate on windows with different visual types at the same time. Go to an SGI box and run an xterm and run xwininfo on the xterm window; it will probably be using an 8-bit pseudocolor visual. Now run xv or Electric Eyes, and you may see that it uses a 24-bit truecolor visual. The hardware is smart enough to split its video memory in different areas for the pseudocolor and truecolor information, for example.

The reason why it is convenient to have drawables with different visual types at the same time is performance. For example, image information on pseudocolor visuals usually takes up much less space than that on truecolor visuals (a common example is 8 bits per pixel versus 24 bits per pixel, respectively). Using less memory also means that you can paint things faster, as there is less information to push around. This is why most applications which are not graphics-intensive run in lower-quality visuals while programs that require high-quality images run on the fat visuals like truecolor and direct color.

Having different simultaneous visual types also has subtler advantages. Let us consider a piece of video hardware with enough memory to store pseudocolor and truecolor information for every pixel, plus one extra bit. This bit is a flag that selects whether to paint the pixel from the pseudocolor or truecolor memory regions. Say there is a hungry application with some windows on the truecolor visual that are slow to repaint (for example, a 3D rendering). If the application needs to pop up a menu that will soon go away, it is convenient to create the menu on the pseudocolor visual, and set all the toggle bits on the corresponding region to indicate that it will use the pseudocolor visual instead of the truecolor one. Since the information on the truecolor memory region is not erased, only not displayed, the X server can quickly un-pop the menu when it is done by simply switching the toggle bits back to the truecolor indication; there will be no need to request a repaint of that region to the application. This means things will be as fast as possible.