XDGA(3)

πŸ’– XDGA πŸ’– β€” the XFree86-DGA client library, explained by your fave Unix bestie

okay so bestie, this is the manual page for XDGA, and it lives in section 3 of the manual (that's the library-functions section, iykyk). It's the client library for the XFree86-DGA extension. Let's get into it πŸ’…


✨ NAME ✨

XDGA β€” Client library for the XFree86-DGA extension.


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <X11/extensions/xf86dga.h>

Bool XDGAQueryExtension(
       Display *dpy,
       int *eventBase,
       int *errorBase)

Bool XDGAQueryVersion(
       Display *dpy,
       int *majorVersion,
       int *minorVersion)

XDGAMode *XDGAQueryModes(
       Display *dpy,
       int screen,
       int *num)

XDGADevice *XDGASetMode(
       Display *dpy,
       int screen,
       int mode)

Bool XDGAOpenFramebuffer(
       Display *dpy,
       int screen)

void XDGACloseFramebuffer(
       Display *dpy,
       int screen)

void XDGASetViewport(
       Display *dpy,
       int screen,
       int x,
       int y,
       int flags)

void XDGAInstallColormap(
       Display *dpy,
       int screen,
       Colormap cmap)

Colormap XDGACreateColormap(
       Display *dpy,
       int screen,
       XDGADevice *device,
       int alloc)

void XDGASelectInput(
       Display *dpy,
       int screen,
       long event_mask)

void XDGAFillRectangle(
       Display *dpy,
       int screen,
       int x,
       int y,
       unsigned int width,
       unsigned int height,
       unsigned long color)

void XDGACopyArea(
       Display *dpy,
       int screen,
       int srcx,
       int srcy,
       unsigned int width,
       unsigned int height,
       int dstx,
       int dsty)

void XDGACopyTransparentArea(
       Display *dpy,
       int screen,
       int srcx,
       int srcy,
       unsigned int width,
       unsigned int height,
       int dstx,
       int dsty,
       unsigned long key)

int XDGAGetViewportStatus(
       Display *dpy,
       int screen)

void XDGASync(
       Display *dpy,
       int screen)

Bool XDGASetClientVersion(
       Display *dpy)

void XDGAChangePixmapMode(
       Display *dpy,
       int screen,
       int *x,
       int *y,
       int mode)

void XDGAKeyEventToXKeyEvent(
       XDGAKeyEvent *dk,
       XKeyEvent *xk)

🌸 DESCRIPTION 🌸

so the tea is: the XFree86-DGA extension is an X server extension that lets client programs get direct access to the video frame buffer. iconic, but also very "don't touch anything". What follows is a brief description of the programming interface for version 2.0 of the XFree86-DGA extension.

ngl, XFree86-DGA is not meant to be a direct rendering API. It's more like a mechanism to "get the X Server out of the way" so that some other direct rendering API can have full access to the hardware. So with that in mind, DGA does hand clients some direct access to the hardware without needing a separate rendering API β€” but that access is limited to direct linear framebuffer access. that's it. that's the whole vibe.

and here's the part where I hold your hand a little 😭 β€” most of the reasons this extension even exists are now better served in other ways. Further development of this extension is not expected, and it may be deprecated in a future release. The features that continue to be useful will either be provided through other existing mechanisms, or through an extension that addresses those needs more specifically. so like… it's giving legacy. 🫢

XDGA gets initialized by passing a number corresponding to a valid XDGAMode to XDGASetMode(). Clients can grab a list of valid modes from XDGAQueryModes(). Each XDGAMode corresponds to a different framebuffer layout.

XDGAQueryModes() returns a pointer to an array of XDGAModes which are valid for the given screen. num is the number of elements in the array. The returned array can be freed with XFree(3). The XDGAMode structure is as follows:

typedef struct {
   int num;
   char *name;
   float verticalRefresh;
   int flags;
   int imageWidth;
   int imageHeight;
   int pixmapWidth;
   int pixmapHeight;
   int bytesPerScanline;
   int byteOrder;
   int depth;
   int bitsPerPixel;
   unsigned long redMask;
   unsigned long greenMask;
   unsigned long blueMask;
   short visualClass;
   int viewportWidth;
   int viewportHeight;
   int xViewportStep;
   int yViewportStep;
   int maxViewportX;
   int maxViewportY;
   int viewportFlags;
   int reserved1;
   int reserved2;
} XDGAMode;

let's go field by field bestie, no skipping πŸ’–

  • num β€” A unique identifying number (num > 0) for the mode. This is the number you reference when initializing the mode.
  • name β€” The name of the corresponding modeline as given in the xorg.conf file.
  • verticalRefresh β€” The vertical refresh rate for the modeline (in Hz).
  • flags β€” Any of the following may be OR'd together:
    • XDGAConcurrentAccess β€” Indicates that concurrent client/server access to the framebuffer is possible. If this flag is not set it is very important to call XDGASync() before directly accessing the framebuffer, if a call to XDGAFillRectangle(), XDGACopyArea() or XDGACopyTransparentArea() or any Xlib rendering function has been made prior to such accesses. (read that twice bestie, it matters πŸ”₯)
    • XDGASolidFillRect β€” Indicates that XDGAFillRectangle() is supported.
    • XDGABlitRect β€” Indicates that XDGACopyArea() is supported.
    • XDGABlitTransRect β€” Indicates that XDGACopyTransparentArea() is supported.
    • XDGAPixmap β€” Indicates that a Pixmap will be returned when the mode is initialized. This means that rendering with Xlib is possible for this mode.
    • XDGAInterlaced
    • XDGADoublescan β€” Indicates that the mode is an interlaced or doublescan mode.
  • imageWidth
  • imageHeight β€” The width and height of the framebuffer area accessible by the client. This rectangle is always justified to the upper left-hand corner.
  • pixmapWidth
  • pixmapHeight β€” The width and height of the framebuffer area accessible by Xlib. This rectangle is always justified to the upper left-hand corner. These fields are only valid if the XDGAPixmap flag is set in the flags field.
  • bytesPerScanline β€” The pitch of the framebuffer in bytes.
  • byteOrder β€” MSBFirst or LSBFirst.
  • depth β€” The number of bits in each pixel which contain usable data.
  • bitsPerPixel β€” The number of bits taken up by each pixel.
  • redMask
  • greenMask
  • blueMask β€” The RGB masks. These do not apply to color-indexed modes.
  • visualClass β€” TrueColor, PseudoColor, DirectColor, etc.
  • viewportWidth
  • viewportHeight β€” The dimensions of the portion of the framebuffer which will be displayed on the screen.
  • xViewportStep
  • yViewportStep β€” The granularity of the x,y viewport positioning possible with the XDGASetViewport() function.
  • maxViewportX
  • maxViewportY β€” The maximum x and y positions possible with the XDGASetViewport() function.
  • viewportFlags β€” Any of the following may be OR'd together:
    • XDGAFlipRetrace β€” Indicates that the hardware can switch viewports during the vertical retrace.
    • XDGAFlipImmediate β€” Indicates that the hardware can switch viewports immediately without waiting for the vertical retrace.

XDGASetMode() initialises the XDGAMode corresponding to num. To exit DGA mode and return to normal server operation, call XDGASetMode() with num set to zero. XDGASetMode() returns a pointer to an XDGADevice if successful. The XDGADevice can be freed with XFree(3). The XDGADevice structure is as follows:

typedef struct {
   XDGAMode mode;
   unsigned char *data;
   Pixmap pixmap;
} XDGADevice;
  • mode β€” The XDGAMode structure, identical to the information returned by XDGAQueryModes().
  • data β€” If direct framebuffer access is desired and possible, this field will contain a pointer to the mapped framebuffer memory. Generally, this field will be zero unless a call to XDGAOpenFramebuffer() is made prior to initialization of the mode.
  • pixmap β€” If the mode supports Xlib rendering as indicated by XDGAPixmap in the flags field, this will contain a Pixmap handle suitable for passing as the drawable argument to Xlib functions. This field will be zero if Xlib rendering is not supported.

XDGAQueryExtension() checks for the presence of the extension and returns the event and error bases.

XDGAQueryVersion() returns the XFree86-DGA major and minor version numbers.

XDGAOpenFramebuffer() maps the framebuffer memory. The client needs sufficient privileges to be able to do this. XDGAOpenFramebuffer() should be called prior to initializing a DGA mode if direct framebuffer access is desired for that mode. XDGAOpenFramebuffer() does not need to be called if direct framebuffer access is not required. If the framebuffer is opened,

XDGACloseFramebuffer() should be called prior to client exit to unmap the memory. (yes bestie, clean up after yourself πŸ’…)

XDGAChangePixmapMode() can be used to change between two pixmap sizes in cases where a Pixmap is available for Xlib rendering. The following values for the mode parameter are available:

  • XDGAPixmapModeLarge β€” The pixmap size is defined by the pixmapWidth and pixmapHeight fields in the XDGAMode structure. The x and y values are ignored in this case.
  • XDGAPixmapModeSmall β€” The pixmap size is defined by the viewportWidth and viewportHeight fields in the XDGAMode structure. In this mode, the x and y values specify where in the framebuffer this pixmap rectangle is located. It may be placed anywhere within the Xlib renderable region described by the pixmapWidth and pixmapHeight fields in the XDGAMode. The x and y values returned are the resultant location of the pixmap and may be different from the requested x,y location due to platform specific alignment constraints. All Xlib rendering is clipped to this pixmap rectangle.

XDGASetViewport() sets the upper left-hand corner of the rectangle of framebuffer that is to be displayed on the screen. Not all locations may be supported by the hardware and requested locations will be adjusted according to the xViewportStep and yViewportStep fields in the XDGAMode.

okay bestie we're back and we're right in the middle of the function tea, no recap, let's GO πŸ’…


the flip flags πŸŒ€

So flags can be either XDGAFlipRetrace or XDGAFlipImmediate β€” they control when the viewport location actually gets moved:

  • XDGAFlipRetrace β€” the viewport moves at the next vertical retrace
  • XDGAFlipImmediate β€” the viewport moves right now, immediately

If you pass literally anything else that isn't one of the values advertised in the mode's viewportFlags field, you get hardware-specific default behavior, which is giving "mystery box, no refunds" energy πŸ™ˆ

Also important, bestie:

  • XDGAFlipImmediate blocks until the flip is done. It's a "wait right here, don't move" situation 🫢
  • XDGAFlipRetrace generally will NOT block, so you have to babysit the viewport status with XDGAGetViewportStatus(). No set-and-forget, periodt.
  • And XDGAFlipImmediate requests that come in while there are pending XDGAFlipRetrace requests? They get ignored. Straight up ghosted πŸ’€

XDGAGetViewportStatus() β€” the tracker πŸ‘€

This one keeps tabs on all the XDGASetViewport() requests that are still pending. The return value has consecutive bits set (LSB justified), and each bit = one pending viewport change. For example:

     while(XDGAGetViewportStatus(dpy, screen));

…waits for all pending viewport changes to finish. And:

     while(0x2 & XDGAGetViewportStatus(dpy, screen));

…waits until all but the last viewport changes have completed. Not the last one. She's still out there πŸ¦‹


XDGACreateColormap() 🎨

It's basically the Xlib XCreateColormap(3) function's cousin, except it takes an XDGADevice instead of a Window and a Visual. And yes, XCreateColormap(3) may create usable colormaps in some cases, but XDGACreateColormap() is the preferred method for making colormaps in DGA β€” because there might not even be an advertised visual compatible with the DGA device. So like… use the DGA one. Trust. ✨

And then: XDGAInstallColormap() is the one you must use to install colormaps in DGA mode. XInstallColormap(3) will not work. Not even a little. Nope 🚫


XDGASelectInput() β€” DGA's own event mechanism πŸŽ€

This function turns on DGA's own event system. It's similar to XSelectInput(3), and all Xlib Key, Button and Motion masks are supported. Here are the DGA events that are defined, and yes, we're pasting them in full because they're iconic:

     typedef struct {
        int type;             /* ButtonPress or ButtonRelease + the DGA event base*/
        unsigned long serial; /* # or last request processed by the server */
        Display *display;     /* Display the event was read from */
        int screen;           /* The screen number the event came from */
        Time time;            /* milliseconds */
        unsigned int state;   /* key or button mask */
        unsigned int button;  /* detail */
     } XDGAButtonEvent;
     typedef struct {
        int type;             /* KeyPress or KeyRelease + the DGA event base*/
        unsigned long serial; /* # or last request processed by the server */
        Display *display;     /* Display the event was read from */
        int screen;           /* The screen number the event came from */
        Time time;            /* milliseconds */
        unsigned int state;   /* key or button mask */
        unsigned int keycode; /* detail */
     } XDGAKeyEvent;
     typedef struct {
        int type;             /* MotionNotify + the DGA event base*/
        unsigned long serial; /* # or last request processed by the server */
        Display *display;     /* Display the event was read from */
        int screen;           /* The screen number the event came from */
        Time time;            /* milliseconds */
        unsigned int state;   /* key or button mask */
        int dx;               /* relative pointer motion */
        int dy;               /* relative pointer motion */
     } XDGAMotionEvent;

And XDGAKeyEventToXKeyEvent() is the lil helper bestie that translates XDGAKeyEvents into XKeyEvents so you can hand them to XLookupKeysym(3). 🫢


XDGAFillRectangle(), XDGACopyArea(), XDGACopyTransparentArea() πŸ–ŒοΈ

Okay so these three come with some reservation, because β€” say it with me β€” DGA is not intended as a rendering API. They're merely convenience routines and they're optionally supported. If they are supported, the associated flags get set in the XDGAMode's flags field. If they're not supported, these functions are no-ops. Nothing happens. Crickets πŸ¦—

Also, they don't give you direct access to the hardware at all β€” they're just context-less operations performed by the server. That's the whole vibe.


XDGASync() β€” the one you do NOT skip 😀

XDGASync() blocks until all server rendering to the framebuffer completes.

Now listen up, because this part matters: if you're using Xlib, or those 3 rendering functions above, you must call XDGASync() before the client directly accesses the framebuffer. Why? Because the server's rendering is asynchronous with the client and might not have finished yet. It's off doing its own thing, living its life 🌈

and this is ESPECIALLY important if the XDGAConcurrentAccess flag is NOT set in the XDGAMode's flags field β€” because concurrent access by the server and the client may result in a system lockup. Like, the actual machine. Gone. Locked. πŸ’€ So fr fr, call XDGASync(). No cap.

πŸ’– SEE ALSO πŸ’–


πŸ‘‘ AUTHORS πŸ‘‘

XFree86-DGA version 2 was written by Mark Vojkovich. And version 1 was written by Jon Tombs, Harm Hanemaayer, and Mark Vojkovich. Legends only, we stan ✨


and that's the whole page, bestie!! this is from X Version 11, specifically libXxf86dga 1.1.6, dated the classic XDGA(3) era β€” so if you ever need to cite her, that's the glow-up receipt πŸŽ€ go forth and flip your viewports responsibly πŸ’…πŸ¦‹