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

# Double Buffered Graphics Driver Example

The Double Buffered Graphics Driver example shows how to write your own double buffered graphics driver and add it to Qt for Embedded Linux.

Similar to the Accelerated Graphics Driver Example, there are three steps to writing and implementing this graphics driver:

1. Creating a Custom Graphics Driver
2. Implementing the Back Buffer
3. Creating the Driver Plugin

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.

Note that this is a minimal example and this driver will not work well with widgets painting themself directly to the screen (e.g. widgets with the Qt::WA_PaintOnScreen window attribute set). Also, the example requires the Linux framebuffer to be set up correctly and with the correct device permissions. For further information, refer to Testing the Linux Framebuffer.

## Step 1: Creating a Custom Graphics Driver

Usually, a custom graphics driver is created by subclassing the QScreen class, the base class for implementing screen or graphics drivers in Qt for Embedded Linux. In this example, however, we subclass the QLinuxFbScreen class instead, to ensure that our driver uses the Linux framebuffer.

For our graphics driver, the `DBScreen` class, we reimplement five functions belonging to QScreen:

- initDevice(),
- shutdownDevice(),
- blit(),
- solidFill(), and
- exposeRegion().

In addition to the abovementioned functions, there is a private instance of QPainter and QImage - `painter`, used for drawing operations on the back buffer, and `image`, the back buffer itself.

## Step 2: Implementing the Back Buffer

The graphics driver must carry out three main functions:

1. Allocate the back buffer on startup and deallocate it on shutdown.
2. Draw to the back buffer instead of directly to the screen (which is what QLinuxFbScreen does).
3. Copy the back buffer to the screen whenever a screen update is done.

### Device initializing and shutdown

We first reimplement `initDevice()` and `shutdownDevice()`.

The `initDevice()` function initializes the framebuffer. We reimplement this function to enable accelerated drivers to set up the graphic card. For this example, we first call the super class' implementation to set up the Linux framebuffer. If this call returns `false`, we return `false`. Otherwise, we initialize the screen cursor with QScreenCursor::initSoftwareCursor() as well as instantiate `image` and `painter`. Then, we return `true`.

The `shutdownDevice()` function's default implementation only hides the mouse cursor. Hence, we reimplement it to carry out the necessary cleanup before the Qt for Embedded Linux server exits.

Again, we call the super class implementation to shutdown the Linux framebuffer prior to deleting `image` and `painter`.

### Drawing to the back buffer

We move on to the drawing functions - `solidFill()` and `blit()`. In QLinuxFbScreen, these functions draw directly to the Linux framebuffer; but in our driver we reimplement them to draw to the back buffer instead.

The `solidFill()` function is called from `exposeRegion()` to fill the given `region` of the screen with the specified `color`. In this example, we use `painter` to fill rectangles in `image`, the back buffer, according to the given region.

The `blit()` function is also called from `exposeRegion()` to copy the given QRegion object, `region`, in the given QImage object, `image`, to the QPoint object specified by `topLeft`. Once again we use `painter` to draw in the back buffer, `image`.

### Displaying the buffer on the screen

The `exposeRegion()` function is called by the Qt for Embedded Linux server whenever a screen update is required. The given `region` is the screen region that needs to be updated and `changing` is is the index into QWSServer::clientWindows() of the window that caused the update.

In our implementation, we first call the super class implementation to ensure that `solidFill()` and `blit()` will be called correctly. This causes the changed areas to be updated in the back buffer. We then call the super class' implementation of `blit()` to copy the updated region from the back buffer into the Linux framebuffer.

## Step 3: Creating the Driver Plugin

Qt provides a high level API for writing Qt extentions. One of the plugin base classes provided is QScreenDriverPlugin, which we use in this example to create our screen driver plugin.

There are only two functions to reimplement:

- create() - creates a driver matching the given key
- keys() - returns a list of valid keys representing the drivers supported by the plugin

Our plugin will only support one driver, `dbscreen`.

Lastly, we export the plugin.

For detailed information about the Qt plugin system see How to Create Qt Plugins.