|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# Accelerated Graphics Driver Example

The Accelerated Graphics Driver example shows how you can write your own accelerated graphics driver and add your graphics driver to Qt for Embedded Linux. In Qt for Embedded Linux, painting is a pure software implementation and is normally performed in two steps: The clients render each window onto a corresponding surface (stored in memory) using a paint engine, and then the server uses the graphics driver to compose the surface images and copy them to the screen. (See the Qt for Embedded Linux Architecture documentation for details.)

The rendering can be accelerated in two ways: Either by accelerating the copying of pixels to the screen, or by accelerating the explicit painting operations. The first is done in the graphics driver implementation, the latter is performed by the paint engine implementation. Typically, both the pixel copying and the painting operations are accelerated using the following approach:

1. Creating a Custom Graphics Driver
2. Implementing a Custom Paint Engine
3. Making the Widgets Aware of the Custom Paint Engine

After compiling the example code, install the graphics driver plugin with the command `make install`. To start an application using the graphics driver, you can either set the environment variable QWS_DISPLAY and then run the application, or you can just run the application using the `-display` switch:

     myApplication -qws -display svgalib

SVGAlib
Instead of interfacing the graphics hardware directly, this example relies on SVGAlib being installed on your system. SVGAlib is a small graphics library which provides acceleration for many common graphics cards used on desktop computers. It should work on most workstations and has a small and simple API.

## Step 1: Creating a Custom Graphics Driver

The custom graphics driver is created by deriving from the QScreen class. QScreen is the base class for implementing screen/graphics drivers in Qt for Embedded Linux.

The connect(), disconnect(), initDevice() and shutdownDevice() functions are declared as pure virtual functions in QScreen and must be implemented. They are used to configure the hardware, or query its configuration: connect() and disconnect() are called by both the server and client processes, while the initDevice() and shutdownDevice() functions are only called by the server process.

QScreen's setMode() and blank() functions are also pure virtual, but our driver's implementations are trivial. The last two functions (blit() and solidFill()) are the ones involved in putting pixels on the screen, i.e., we reimplement these functions to perform the pixel copying acceleration.

Finally, the `context` variable is a pointer to a SVGAlib specific type. Note that the details of using the SVGAlib library is beyond the scope of this example.

### SvgalibScreen Class Implementation

The connect() function is the first function that is called after the constructor returns. It queries SVGAlib about the graphics mode and initializes the variables.

It is important that the connect() function initializes the `data`, `lstep`, `w`, `h`, `dw`, `dh`, `d`, `physWidth` and `physHeight` variables (inherited from QScreen) to ensure that the driver is in a state consistent with the driver configuration.

In this particular example we do not have any information of the real physical size of the screen, so we set these values with the assumption of a screen with 72 DPI.

When the connect() function returns, the server process calls the initDevice() function which is expected to do the necessary hardware initialization, leaving the hardware in a state consistent with the driver configuration.

Note that we have chosen to use the software cursor. If you want to use a hardware cursor, you should create a subclass of QScreenCursor, create an instance of it, and make the global variable `qt_screencursor` point to this instance.

Before exiting, the server process will call the shutdownDevice() function to do the necessary hardware cleanup. Again, it is important that the function leaves the hardware in a state consistent with the driver configuration. When shutdownDevice() returns, the disconnect() function is called. Our implementation of the latter function is trivial.

Note that, provided that the `QScreen::data` variable points to a valid linear framebuffer, the graphics driver is fully functional as a simple screen driver at this point. The rest of this example will show where to take advantage of the accelerated capabilities available on the hardware.

Whenever an area on the screen needs to be updated, the server will call the exposeRegion() function that paints the given region on screen. The default implementation will do the necessary composing of the top-level windows and call solidFill() and blit() whenever it is required. We do not want to change this behavior in the driver so we do not reimplement exposeRegion().

To control how the pixels are put onto the screen we need to reimplement the solidFill() and blit() functions.

## Step 2: Implementing a Custom Raster Paint Engine

Qt for Embedded Linux uses QRasterPaintEngine (a raster-based implementation of QPaintEngine) to implement the painting operations.

Acceleration of the painting operations is done by deriving from QRasterPaintEngine class. This is a powerful mechanism for accelerating graphic primitives while getting software fallbacks for all the primitives you do not accelerate.

In this example, we will only accelerate one of the drawRects() functions, i.e., only non-rotated, aliased and opaque rectangles will be rendered using accelerated painting. All other primitives are rendered using the base class's unaccelerated implementation.

The paint engine's state is stored in the private member variables, and we reimplement the updateState() function to ensure that our custom paint engine's state is updated properly whenever it is required. The private `setClip()` and `updateClip()` functions are only helper function used to simplify the updateState() implementation.

We also reimplement QRasterPaintEngine's begin() and end() functions to initialize the paint engine and to do the cleanup when we are done rendering, respectively.

Private Header Files
Note the `include` statement used by this class. The files prefixed with `private/` are private headers file within Qt for Embedded Linux. Private header files are not part of the standard installation and are only present while compiling Qt. To be able to compile using private header files you need to use a `qmake` binary within a compiled Qt for Embedded Linux package. Warning: Private header files may change without notice between releases.

The begin() function initializes the internal state of the paint engine. Note that it also calls the base class implementation to initialize the parts inherited from QRasterPaintEngine:

The implementation of the end() function removes the clipping constraints that might have been set in SVGAlib, before calling the corresponding base class implementation.

The updateState() function updates our custom paint engine's state. The QPaintEngineState class provides information about the active paint engine's current state.

Note that we only accept and save the current matrix if it doesn't do any shearing. The pen is accepted if it is opaque and only one pixel wide. The rest of the engine's properties are updated following the same pattern. Again it is important that the QRasterPaintEngine::updateState() function is called to update the parts inherited from the base class.

The `setClip()` helper function is called from our custom implementation of updateState(), and enables clipping to the given region. An empty region means that clipping is disabled.

Our custom update function also makes use of the `updateClip()` helper function that checks if the clip is "simple", i.e., that it can be represented by only one rectangle, and updates the clip region in SVGAlib.

Finally, we accelerated that drawing of non-rotated, aliased and opaque rectangles in our reimplementation of the drawRects() function. The QRasterPaintEngine fallback is used whenever the rectangle is not simple enough.

## Step 3: Making the Widgets Aware of the Custom Paint Engine

To activate the custom paint engine, we also need to implement a corresponding paint device and window surface and make some minor adjustments of the graphics driver.

- Implementing a Custom Paint Device
- Implementing a Custom Window Surface
- Adjusting the Graphics Driver

### Implementing a Custom Paint Device

The custom paint device can be derived from the QCustomRasterPaintDevice class. Reimplement its paintEngine() and memory() functions to activate the accelerated paint engine:

The paintEngine() function should return an instance of the `SvgalibPaintEngine` class. The memory() function should return a pointer to the buffer which should be used when drawing the widget.

Our example driver is rendering directly to the screen without any buffering, i.e., our custom pain device's memory() function returns a pointer to the framebuffer. For this reason, we must also reimplement the metric() function to reflect the metrics of framebuffer.

### Implementing a Custom Window Surface

The custom window surface can be derived from the QWSWindowSurface class. QWSWindowSurface manages the memory used when drawing a window.

We can implement most of the pure virtual functions inherited from QWSWindowSurface as trivial inline functions, except the scroll() function that actually makes use of some hardware acceleration:

### Adjusting the Graphics Driver

Finally, we enable the graphics driver to recognize an instance of our custom window surface:

The createSurface() functions are factory functions that determines what kind of surface a top-level window is using. In our example we only use the custom surface if the given window has the Qt::WA_PaintOnScreen attribute or the QT_ONSCREEN_PAINT environment variable is set.