Viadeo Twitter Google Bookmarks ! Facebook Digg MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Accelerated Screen Driver Example


The Accelerated Screen Driver example shows how to write an accelerated screen driver for Qtopia Core.

In Qtopia Core, 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 screen driver to compose the surface images and copy them to the screen. (See the Qtopia Core 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 screen driver implementation, the latter is performed by the paint engine implementation. Typically, both the pixel copying and the painting operations are accelerted using the following approach:

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

After compiling the example code, you should install the screen driver plugin with the command make install. To start an application using the example's screen driver, you can either set the environment variable QWS_DISPLAY or start the program using the -display switch:

 myApplication -qws -display svgalib

Note that in order to keep the example as simple as possible, our driver only works with screen modes having 32 bits per pixel and does not work with multiple processes.

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 Screen Driver

The custom screen driver is created by deriving from the QScreen class. QScreen is the base class for implementing screen drivers in Qtopia Core.

 ** This file is part of the example classes of the Qt Toolkit.
 ** This file may be used under the terms of the GNU General Public
 ** License version 2.0 as published by the Free Software Foundation
 ** and appearing in the file LICENSE.GPL included in the packaging of
 ** this file.  Please review the following information to ensure GNU
 ** General Public Licensing requirements will be met:
 ** If you are unsure which license is appropriate for your use, please
 ** review the following information:
 ** or contact the
 ** sales department at
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE


 #include <QScreen>

 #include <vga.h>
 #include <vgagl.h>

 class SvgalibScreen : public QScreen
     SvgalibScreen(int displayId) : QScreen(displayId) {}
     ~SvgalibScreen() {}

     bool connect(const QString &displaySpec);
     bool initDevice();
     void shutdownDevice();
     void disconnect();

     void setMode(int, int, int) {}
     void blank(bool) {}

     void exposeRegion(QRegion r, int changing);
     void blit(const QImage &img, const QPoint &topLeft, const QRegion &region);
     void solidFill(const QColor &color, const QRegion &region);

     GraphicsContext *context;

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 three functions (exposeRegion(), 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.

 bool SvgalibScreen::connect(const QString &displaySpec)

     int mode = vga_getdefaultmode();
     if (mode <= 0) {
         qCritical("SvgalibScreen::connect(): invalid vga mode");
         return false;

     vga_modeinfo *modeinfo = vga_getmodeinfo(mode);

     QScreen::lstep = modeinfo->linewidth;
     QScreen::dw = QScreen::w = modeinfo->width;
     QScreen::dh = QScreen::h = modeinfo->height;
     QScreen::d = modeinfo->bytesperpixel * 8;
     QScreen::size = QScreen::lstep * dh;
     QScreen::data = 0;

     const int dpi = 72;
     QScreen::physWidth = qRound(QScreen::dw * 25.4 / dpi);
     QScreen::physHeight = qRound(QScreen::dh * 25.4 / dpi);

     return true;

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.

 bool SvgalibScreen::initDevice()
     if (vga_init() != 0) {
         qCritical("SvgalibScreen::initDevice(): unable to initialize svgalib");
         return false;

     int mode = vga_getdefaultmode();
     if (vga_setmode(mode) == -1) {
         qCritical("SvgalibScreen::initialize(): unable to set graphics mode");
         return false;

     if (gl_setcontextvga(mode) != 0) {
         qCritical("SvgalibScreen::initDevice(): unable to set vga context");
         return false;
     context = gl_allocatecontext();

     vga_modeinfo *modeinfo = vga_getmodeinfo(mode);
     if (!(modeinfo->flags & IS_LINEAR)) {
         qCritical("SvgalibScreen::initDevice(): graphics memory not linear");
         return false;
     QScreen::data = vga_getgraphmem();

     return true;

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.

 void SvgalibScreen::shutdownDevice()

 void SvgalibScreen::disconnect()

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 screen driver is fully functional at this point. The rest of this example will show where to take advantage of the accelerated capabilities available on the hardware.

 void SvgalibScreen::exposeRegion(QRegion region, int changing)
     QScreen::exposeRegion(region, changing);
     // flip between buffers if implementing a double buffered driver

The next function we implement is 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 this driver so we just call the base class implementation. On the other hand, if you are implementing a double buffered screen driver, this is where you would like to flip between the buffers.

 void SvgalibScreen::solidFill(const QColor &color, const QRegion &reg)
     if (depth() != 32 || depth() != 16) {
         QScreen::solidFill(color, reg);

     const QVector<QRect> rects = (reg & region()).rects();
     for (int i = 0; i < rects.size(); ++i) {
         const QRect r =;
         gl_fillbox(r.left(),, r.width(), r.height(), color.rgba());

 void SvgalibScreen::blit(const QImage &img, const QPoint &topLeft,
                          const QRegion &reg)
     bool do_fallback = true;

     if (depth() == 16 && img.format() == QImage::Format_RGB16)
         do_fallback = false;
     if (depth() == 32 && img.format() == QImage::Format_ARGB32_Premultiplied)
         do_fallback = false;

     if (do_fallback) {
         QScreen::blit(img, topLeft, reg);

     const QVector<QRect> rects = (reg & region()).rects();

     for (int i = 0; i < rects.size(); ++i) {
         const QRect r =;
         gl_putboxpart(r.x(), r.y(), r.width(), r.height(),
                       img.width(), img.height(),
                       r.x() - topLeft.x(), r.y() - topLeft.y());

Finally, we accelerate the copying of pixels to the screen by reimplementing the solidFill() and blit() functions.

Step 2: Implementing a Custom Raster Paint Engine

Qtopia Core 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.

 #include <private/qpaintengine_raster_p.h>

 class SvgalibPaintEngine : public QRasterPaintEngine

     bool begin(QPaintDevice *device);
     bool end();
     void updateState(const QPaintEngineState &state);
     void drawRects(const QRect *rects, int rectCount);

     void setClip(const QRegion &region);
     void updateClip();

     QPen pen;
     bool simplePen;
     QBrush brush;
     bool simpleBrush;
     QMatrix matrix;
     bool simpleMatrix;
     QRegion clip;
     bool clipEnabled;
     bool simpleClip;
     bool opaque;
     bool aliased;
     bool sourceOver;
     QPaintDevice *device;

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 Qtopia Core. Private header files are not part of a Qtopia Core installation and are only present while compiling Qtopia Core. To be able to compile using private header files you need to use a qmake binary within a compiled Qtopia Core 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:

 bool SvgalibPaintEngine::begin(QPaintDevice *dev)
     device = dev;
     pen = Qt::NoPen;
     simplePen = true;
     brush = Qt::NoBrush;
     simpleBrush = true;
     matrix = QMatrix();
     simpleMatrix = true;
     setClip(QRect(0, 0, device->width(), device->height()));
     opaque = true;
     aliased = true;
     sourceOver = true;

     return QRasterPaintEngine::begin(dev);

 bool SvgalibPaintEngine::end()
     gl_setclippingwindow(0, 0, device->width() - 1, device->height() - 1);
     return QRasterPaintEngine::end();

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

 void SvgalibPaintEngine::updateState(const QPaintEngineState &state)
     QPaintEngine::DirtyFlags flags = state.state();

     if (flags & DirtyTransform) {
         matrix = state.matrix();
         simpleMatrix = (matrix.m12() == 0 && matrix.m21() == 0);

     if (flags & DirtyPen) {
         pen = state.pen();
         simplePen = (pen.width() == 0 || pen.widthF() <= 1)
                     && ( == Qt::NoPen || == Qt::SolidLine)
                     && (pen.color().alpha() == 255);

     if (flags & DirtyBrush) {
         brush = state.brush();
         simpleBrush = ( == Qt::SolidPattern
                        || == Qt::NoBrush)
                       && (brush.color().alpha() == 255);

     if (flags & DirtyClipRegion)

     if (flags & DirtyClipEnabled) {
         clipEnabled = state.isClipEnabled();

     if (flags & DirtyClipPath) {
         simpleClip = false;

     if (flags & DirtyCompositionMode) {
         const QPainter::CompositionMode m = state.compositionMode();
         sourceOver = (m == QPainter::CompositionMode_SourceOver);

     if (flags & DirtyOpacity)
         opaque = (state.opacity() == 256);

     if (flags & DirtyHints)
         aliased = !(state.renderHints() & QPainter::Antialiasing);


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.

 void SvgalibPaintEngine::setClip(const QRegion &region)
     if (region.isEmpty())
         clip = QRect(0, 0, device->width(), device->height());
         clip = & QRect(0, 0, device->width(), device->height());
     clipEnabled = true;

 void SvgalibPaintEngine::updateClip()
     QRegion clipRegion = QRect(0, 0, device->width(), device->height());

     if (!systemClip().isEmpty())
         clipRegion &= systemClip();
     if (clipEnabled)
         clipRegion &= clip;

     simpleClip = (clipRegion.rects().size() <= 1);

     const QRect r = clipRegion.boundingRect();
                          r.x() + r.width(),
                          r.y() + r.height());

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.

 void SvgalibPaintEngine::drawRects(const QRect *rects, int rectCount)
     const bool canAccelerate = simplePen && simpleBrush && simpleMatrix
                                && simpleClip && opaque && aliased
                                && sourceOver;
     if (!canAccelerate) {
         QRasterPaintEngine::drawRects(rects, rectCount);

     for (int i = 0; i < rectCount; ++i) {
         const QRect r = matrix.mapRect(rects[i]);
         if (brush != Qt::NoBrush) {
             gl_fillbox(r.left(),, r.width(), r.height(),
         if (pen != Qt::NoPen) {
             const int c = pen.color().rgba();
             gl_hline(r.left(),, r.right(), c);
             gl_hline(r.left(), r.bottom(), r.right(), c);
             gl_line(r.left(),, r.left(), r.bottom(), c);
             gl_line(r.right(),, r.right(), r.bottom(), c);

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 screen 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:

 class SvgalibPaintDevice : public QCustomRasterPaintDevice
     SvgalibPaintDevice(QWidget *w);

     void* memory() const { return QScreen::instance()->base(); }

     QPaintEngine *paintEngine() const { return pengine; }
     int metric(PaintDeviceMetric m) const;

     SvgalibPaintEngine *pengine;

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 on 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.

 class SvgalibSurface : public QWSWindowSurface
     SvgalibSurface(QWidget *w);

     void setGeometry(const QRect &rect);
     QRect geometry() const { return brect; }

     bool isValidFor(const QWidget *) const { return true; }

     void scroll(const QRegion &region, int dx, int dy);

     const QString key() const { return QLatin1String("svgalib"); }
     const QByteArray data() const { return QByteArray(); }

     bool attach(const QByteArray &) { return true; }
     void detach() {}

     const QImage image() const { return QImage(); }
     QPaintDevice *paintDevice() { return pdevice; }
     QPoint painterOffset() const;

     QRect brect;
     SvgalibPaintDevice *pdevice;

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:

 void SvgalibSurface::scroll(const QRegion &region, int dx, int dy)
     const QVector<QRect> rects = region.rects();
     for (int i = 0; i < rects.size(); ++i) {
         const QRect r =;
         gl_copybox(r.left(),, r.width(), r.height(),
                    r.left() + dx, + dy);

Adjusting the Screen Driver

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

 QWSWindowSurface* SvgalibScreen::createSurface(QWidget *widget) const
     static int onScreenPaint = -1;
     if (onScreenPaint == -1)
         onScreenPaint = qgetenv("QT_ONSCREEN_PAINT").toInt();

     if (onScreenPaint > 0 || widget->testAttribute(Qt::WA_PaintOnScreen))
         return new SvgalibSurface(widget);

     return QScreen::createSurface(widget);

 QWSWindowSurface* SvgalibScreen::createSurface(const QString &key) const
     if (key == QLatin1String("svgalib"))
         return new SvgalibSurface;
     return QScreen::createSurface(key);

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.


Best Of

Actualités les plus lues

  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le blog Digia au hasard


Déploiement d'applications Qt Commercial sur les tablettes Windows 8

Le blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.



Liens utiles


  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.2
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !

Hébergement Web