Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8e6051afcdb111a0317a58fb64c2abf5 > files > 5565

qt4-doc-4.6.3-0.2mdv2010.2.i586.rpm

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- svgalib.qdoc -->
<head>
  <title>Qt 4.6: Accelerated Graphics Driver Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">Accelerated Graphics Driver Example<br /><span class="subtitle"></span>
</h1>
<p>Files:</p>
<ul>
<li><a href="qws-svgalib-svgalibpaintdevice-cpp.html">qws/svgalib/svgalibpaintdevice.cpp</a></li>
<li><a href="qws-svgalib-svgalibpaintdevice-h.html">qws/svgalib/svgalibpaintdevice.h</a></li>
<li><a href="qws-svgalib-svgalibpaintengine-cpp.html">qws/svgalib/svgalibpaintengine.cpp</a></li>
<li><a href="qws-svgalib-svgalibpaintengine-h.html">qws/svgalib/svgalibpaintengine.h</a></li>
<li><a href="qws-svgalib-svgalibplugin-cpp.html">qws/svgalib/svgalibplugin.cpp</a></li>
<li><a href="qws-svgalib-svgalibscreen-cpp.html">qws/svgalib/svgalibscreen.cpp</a></li>
<li><a href="qws-svgalib-svgalibscreen-h.html">qws/svgalib/svgalibscreen.h</a></li>
<li><a href="qws-svgalib-svgalibsurface-cpp.html">qws/svgalib/svgalibsurface.cpp</a></li>
<li><a href="qws-svgalib-svgalibsurface-h.html">qws/svgalib/svgalibsurface.h</a></li>
<li><a href="qws-svgalib-svgalib-pro.html">qws/svgalib/svgalib.pro</a></li>
</ul>
<p><b>Warning:</b> This example was designed to work with Qt 4.4 and will not work with current versions of Qt. It will be removed from Qt 4.7&#x2e;</p>
<p>The Accelerated Graphics Driver example shows how you can write your own accelerated graphics driver and <a href="qt-embedded-accel.html#add-your-graphics-driver-to-qt-for-embedded-linux">add your graphics driver to Qt for Embedded Linux</a>. In <a href="qt-embedded-linux.html">Qt for Embedded Linux</a>, 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 <a href="qt-embedded-architecture.html">Qt for Embedded Linux Architecture</a> documentation for details.)</p>
<p>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:</p>
<ol type="1">
<li><a href="#step-1-creating-a-custom-graphics-driver">Creating a Custom Graphics Driver</a></li>
<li><a href="#step-2-implementing-a-custom-raster-paint-engine">Implementing a Custom Paint Engine</a></li>
<li><a href="#step-3-making-the-widgets-aware-of-the-custom-paint-engine">Making the Widgets Aware of the Custom Paint Engine</a></li>
</ol>
<p>After compiling the example code, install the graphics driver plugin with the command <tt>make install</tt>. To start an application using the graphics driver, you can either set the environment variable <a href="qt-embedded-envvars.html#qws-display">QWS_DISPLAY</a> and then run the application, or you can just run the application using the <tt>-display</tt> switch:</p>
<pre> myApplication -qws -display svgalib</pre>
<p><table class="generic" align="center" cellpadding="2" cellspacing="1" border="0">
<thead><tr valign="top" class="qt-style"><th>SVGAlib</th></tr></thead>
<tr valign="top" class="odd"><td>Instead of interfacing the graphics hardware directly, this example relies on <a href="http://www.svgalib.org">SVGAlib</a> being installed on your system. <a href="http://www.svgalib.org">SVGAlib</a> 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.</td></tr>
</table></p>
<a name="step-1-creating-a-custom-graphics-driver"></a>
<h2>Step 1: Creating a Custom Graphics Driver</h2>
<p>The custom graphics driver is created by deriving from the <a href="qscreen.html">QScreen</a> class. <a href="qscreen.html">QScreen</a> is the base class for implementing screen/graphics drivers in Qt for Embedded Linux.</p>
<pre> class SvgalibScreen : public QScreen
 {
 public:
     SvgalibScreen(int displayId) : QScreen(displayId) {}
     ~SvgalibScreen() {}

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

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

     void blit(const QImage &amp;img, const QPoint &amp;topLeft, const QRegion &amp;region);
     void solidFill(const QColor &amp;color, const QRegion &amp;region);

 private:
     void initColorMap();
     void blit16To8(const QImage &amp;image,
                    const QPoint &amp;topLeft, const QRegion &amp;region);
     void blit32To8(const QImage &amp;image,
                    const QPoint &amp;topLeft, const QRegion &amp;region);

     GraphicsContext *context;
 };</pre>
<p>The <a href="qscreen.html#connect">connect()</a>, <a href="qscreen.html#disconnect">disconnect()</a>, <a href="qscreen.html#initDevice">initDevice()</a> and <a href="qscreen.html#shutdownDevice">shutdownDevice()</a> functions are declared as pure virtual functions in <a href="qscreen.html">QScreen</a> and must be implemented. They are used to configure the hardware, or query its configuration: <a href="qscreen.html#connect">connect()</a> and <a href="qscreen.html#disconnect">disconnect()</a> are called by both the server and client processes, while the <a href="qscreen.html#initDevice">initDevice()</a> and <a href="qscreen.html#shutdownDevice">shutdownDevice()</a> functions are only called by the server process.</p>
<p><a href="qscreen.html">QScreen</a>'s <a href="qscreen.html#setMode">setMode()</a> and <a href="qscreen.html#blank">blank()</a> functions are also pure virtual, but our driver's implementations are trivial. The last two functions (<a href="qscreen.html#blit">blit()</a> and <a href="qscreen.html#solidFill">solidFill()</a>) are the ones involved in putting pixels on the screen, i.e&#x2e;, we reimplement these functions to perform the pixel copying acceleration.</p>
<p>Finally, the <tt>context</tt> variable is a pointer to a <a href="http://www.svgalib.org">SVGAlib</a> specific type. Note that the details of using the <a href="http://www.svgalib.org">SVGAlib</a> library is beyond the scope of this example.</p>
<a name="svgalibscreen-class-implementation"></a>
<h3>SvgalibScreen Class Implementation</h3>
<p>The <a href="qscreen.html#connect">connect()</a> function is the first function that is called after the constructor returns. It queries <a href="http://www.svgalib.org">SVGAlib</a> about the graphics mode and initializes the variables.</p>
<pre> bool SvgalibScreen::connect(const QString &amp;displaySpec)
 {
     int mode = vga_getdefaultmode();
     if (mode &lt;= 0) {
         qCritical(&quot;SvgalibScreen::connect(): invalid vga mode&quot;);
         return false;
     }

     vga_modeinfo *modeinfo = vga_getmodeinfo(mode);

     QScreen::lstep = modeinfo-&gt;linewidth;
     QScreen::dw = QScreen::w = modeinfo-&gt;width;
     QScreen::dh = QScreen::h = modeinfo-&gt;height;
     QScreen::d = getModeDepth(modeinfo);
     QScreen::size = QScreen::lstep * dh;
     QScreen::data = 0;

     switch (depth()) {
     case 32:
         setPixelFormat(QImage::Format_ARGB32_Premultiplied);
         break;
     case 24:
         setPixelFormat(QImage::Format_RGB888);
         break;
     case 16:
         setPixelFormat(QImage::Format_RGB16);
         break;
     case 15:
         setPixelFormat(QImage::Format_RGB555);
         break;
     default:
         break;
     }

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

     const QStringList args = displaySpec.split(QLatin1Char(':'),
                                                QString::SkipEmptyParts);
     grayscale = args.contains(QLatin1String(&quot;grayscale&quot;), Qt::CaseInsensitive);

     return true;
 }</pre>
<p>It is important that the <a href="qscreen.html#connect">connect()</a> function initializes the <tt>data</tt>, <tt>lstep</tt>, <tt>w</tt>, <tt>h</tt>, <tt>dw</tt>, <tt>dh</tt>, <tt>d</tt>, <tt>physWidth</tt> and <tt>physHeight</tt> variables (inherited from <a href="qscreen.html">QScreen</a>) to ensure that the driver is in a state consistent with the driver configuration.</p>
<p>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.</p>
<pre> bool SvgalibScreen::initDevice()
 {
     if (vga_init() != 0) {
         qCritical(&quot;SvgalibScreen::initDevice(): unable to initialize svgalib&quot;);
         return false;
     }

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

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

     vga_modeinfo *modeinfo = vga_getmodeinfo(mode);
     if (modeinfo-&gt;flags &amp; IS_LINEAR)
         QScreen::data = vga_getgraphmem();

     initColorMap();

     QScreenCursor::initSoftwareCursor();
     return true;
 }</pre>
<p>When the <a href="qscreen.html#connect">connect()</a> function returns, the server process calls the <a href="qscreen.html#initDevice">initDevice()</a> function which is expected to do the necessary hardware initialization, leaving the hardware in a state consistent with the driver configuration.</p>
<p>Note that we have chosen to use the software cursor. If you want to use a hardware cursor, you should create a subclass of <a href="qscreencursor.html">QScreenCursor</a>, create an instance of it, and make the global variable <tt>qt_screencursor</tt> point to this instance.</p>
<pre> void SvgalibScreen::shutdownDevice()
 {
     gl_freecontext(context);
     vga_setmode(TEXT);
 }

 void SvgalibScreen::disconnect()
 {
 }</pre>
<p>Before exiting, the server process will call the <a href="qscreen.html#shutdownDevice">shutdownDevice()</a> 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 <a href="qscreen.html#shutdownDevice">shutdownDevice()</a> returns, the <a href="qscreen.html#disconnect">disconnect()</a> function is called. Our implementation of the latter function is trivial.</p>
<p>Note that, provided that the <tt>QScreen::data</tt> 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.</p>
<p>Whenever an area on the screen needs to be updated, the server will call the <a href="qscreen.html#exposeRegion">exposeRegion()</a> function that paints the given region on screen. The default implementation will do the necessary composing of the top-level windows and call <a href="qscreen.html#solidFill">solidFill()</a> and <a href="qscreen.html#blit">blit()</a> whenever it is required. We do not want to change this behavior in the driver so we do not reimplement <a href="qscreen.html#exposeRegion">exposeRegion()</a>.</p>
<p>To control how the pixels are put onto the screen we need to reimplement the <a href="qscreen.html#solidFill">solidFill()</a> and <a href="qscreen.html#blit">blit()</a> functions.</p>
<pre> void SvgalibScreen::solidFill(const QColor &amp;color, const QRegion &amp;reg)
 {
     int c;
     if (depth() == 4 || depth() == 8)
         c = alloc(color.red(), color.green(), color.blue());
     else
         c = gl_rgbcolor(color.red(), color.green(), color.blue());

     const QVector&lt;QRect&gt; rects = (reg &amp; region()).rects();
     for (int i = 0; i &lt; rects.size(); ++i) {
         const QRect r = rects.at(i);
         gl_fillbox(r.left(), r.top(), r.width(), r.height(), c);
     }
 }

 void SvgalibScreen::blit(const QImage &amp;img, const QPoint &amp;topLeft,
                          const QRegion &amp;reg)
 {
     if (depth() == 8) {
         switch (img.format()) {
         case QImage::Format_RGB16:
             blit16To8(img, topLeft, reg);
             return;
         case QImage::Format_RGB32:
         case QImage::Format_ARGB32:
         case QImage::Format_ARGB32_Premultiplied:
             blit32To8(img, topLeft, reg);
             return;
         default:
             break;
         }
     }

     if (img.format() != pixelFormat()) {
         if (base())
             QScreen::blit(img, topLeft, reg);
         return;
     }

     const QVector&lt;QRect&gt; rects = (reg &amp; region()).rects();

     for (int i = 0; i &lt; rects.size(); ++i) {
         const QRect r = rects.at(i);
         gl_putboxpart(r.x(), r.y(), r.width(), r.height(),
                       img.width(), img.height(),
                       static_cast&lt;void*&gt;(const_cast&lt;uchar*&gt;(img.bits())),
                       r.x() - topLeft.x(), r.y() - topLeft.y());
     }
 }</pre>
<a name="step-2-implementing-a-custom-raster-paint-engine"></a>
<h2>Step 2: Implementing a Custom Raster Paint Engine</h2>
<p><a href="qt-embedded-linux.html">Qt for Embedded Linux</a> uses <a href="qrasterpaintengine.html">QRasterPaintEngine</a> (a raster-based implementation of <a href="qpaintengine.html">QPaintEngine</a>) to implement the painting operations.</p>
<p>Acceleration of the painting operations is done by deriving from <a href="qrasterpaintengine.html">QRasterPaintEngine</a> class. This is a powerful mechanism for accelerating graphic primitives while getting software fallbacks for all the primitives you do not accelerate.</p>
<pre> #include &lt;private/qpaintengine_raster_p.h&gt;

 class SvgalibPaintEngine : public QRasterPaintEngine
 {
 public:
     SvgalibPaintEngine();
     ~SvgalibPaintEngine();

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

 private:
     void setClip(const QRegion &amp;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;
 };</pre>
<p>In this example, we will only accelerate one of the <a href="qrasterpaintengine.html#drawRects">drawRects()</a> functions, i.e&#x2e;, 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.</p>
<p>The paint engine's state is stored in the private member variables, and we reimplement the <a href="qpaintengine.html#updateState">updateState()</a> function to ensure that our custom paint engine's state is updated properly whenever it is required. The private <tt>setClip()</tt> and <tt>updateClip()</tt> functions are only helper function used to simplify the <a href="qpaintengine.html#updateState">updateState()</a> implementation.</p>
<p>We also reimplement <a href="qrasterpaintengine.html">QRasterPaintEngine</a>'s <a href="qrasterpaintengine.html#begin">begin()</a> and <a href="qrasterpaintengine.html#end">end()</a> functions to initialize the paint engine and to do the cleanup when we are done rendering, respectively.</p>
<p><table class="generic" align="center" cellpadding="2" cellspacing="1" border="0">
<thead><tr valign="top" class="qt-style"><th>Private Header Files</th></tr></thead>
<tr valign="top" class="odd"><td>Note the <tt>include</tt> statement used by this class. The files prefixed with <tt>private/</tt> are private headers file within <a href="qt-embedded-linux.html">Qt for Embedded Linux</a>. 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 <tt>qmake</tt> binary within a compiled <a href="qt-embedded-linux.html">Qt for Embedded Linux</a> package.<p><b>Warning:</b> Private header files may change without notice between releases.</p>
</td></tr>
</table></p>
<p>The <a href="qrasterpaintengine.html#begin">begin()</a> function initializes the internal state of the paint engine. Note that it also calls the base class implementation to initialize the parts inherited from <a href="qrasterpaintengine.html">QRasterPaintEngine</a>:</p>
<pre> 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-&gt;width(), device-&gt;height()));
     opaque = true;
     aliased = true;
     sourceOver = true;

     return QRasterPaintEngine::begin(dev);
 }

 bool SvgalibPaintEngine::end()
 {
     gl_setclippingwindow(0, 0, device-&gt;width() - 1, device-&gt;height() - 1);
     return QRasterPaintEngine::end();
 }</pre>
<p>The implementation of the <a href="qrasterpaintengine.html#end">end()</a> function removes the clipping constraints that might have been set in <a href="http://www.svgalib.org">SVGAlib</a>, before calling the corresponding base class implementation.</p>
<pre> void SvgalibPaintEngine::updateState(const QPaintEngineState &amp;state)
 {
     QPaintEngine::DirtyFlags flags = state.state();

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

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

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

     if (flags &amp; DirtyClipRegion)
         setClip(state.clipRegion());

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

     if (flags &amp; DirtyClipPath) {
         setClip(QRegion());
         simpleClip = false;
     }

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

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

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

     QRasterPaintEngine::updateState(state);
 }</pre>
<p>The <a href="qpaintengine.html#updateState">updateState()</a> function updates our custom paint engine's state. The <a href="qpaintenginestate.html">QPaintEngineState</a> class provides information about the active paint engine's current state.</p>
<p>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 <a href="qpaintengine.html#updateState">QPaintEngine::updateState</a>() function is called to update the parts inherited from the base class.</p>
<pre> void SvgalibPaintEngine::setClip(const QRegion &amp;region)
 {
     if (region.isEmpty())
         clip = QRect(0, 0, device-&gt;width(), device-&gt;height());
     else
         clip = matrix.map(region) &amp; QRect(0, 0, device-&gt;width(), device-&gt;height());
     clipEnabled = true;
     updateClip();
 }

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

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

     simpleClip = (clipRegion.rects().size() &lt;= 1);

     const QRect r = clipRegion.boundingRect();
     gl_setclippingwindow(r.left(), r.top(),
                          r.x() + r.width(),
                          r.y() + r.height());
 }</pre>
<p>The <tt>setClip()</tt> helper function is called from our custom implementation of <a href="qpaintengine.html#updateState">updateState()</a>, and enables clipping to the given region. An empty region means that clipping is disabled.</p>
<p>Our custom update function also makes use of the <tt>updateClip()</tt> helper function that checks if the clip is &quot;simple&quot;, i.e&#x2e;, that it can be represented by only one rectangle, and updates the clip region in <a href="http://www.svgalib.org">SVGAlib</a>.</p>
<pre> void SvgalibPaintEngine::drawRects(const QRect *rects, int rectCount)
 {
     const bool canAccelerate = simplePen &amp;&amp; simpleBrush &amp;&amp; simpleMatrix
                                &amp;&amp; simpleClip &amp;&amp; opaque &amp;&amp; aliased
                                &amp;&amp; sourceOver;
     if (!canAccelerate) {
         QRasterPaintEngine::drawRects(rects, rectCount);
         return;
     }

     for (int i = 0; i &lt; rectCount; ++i) {
         const QRect r = matrix.mapRect(rects[i]);
         if (brush != Qt::NoBrush) {
             gl_fillbox(r.left(), r.top(), r.width(), r.height(),
                        brush.color().rgba());
         }
         if (pen != Qt::NoPen) {
             const int c = pen.color().rgba();
             gl_hline(r.left(), r.top(), r.right(), c);
             gl_hline(r.left(), r.bottom(), r.right(), c);
             gl_line(r.left(), r.top(), r.left(), r.bottom(), c);
             gl_line(r.right(), r.top(), r.right(), r.bottom(), c);
         }
     }
 }</pre>
<p>Finally, we accelerated that drawing of non-rotated, aliased and opaque rectangles in our reimplementation of the <a href="qrasterpaintengine.html#drawRects">drawRects()</a> function. The <a href="qrasterpaintengine.html">QRasterPaintEngine</a> fallback is used whenever the rectangle is not simple enough.</p>
<a name="step-3-making-the-widgets-aware-of-the-custom-paint-engine"></a>
<h2>Step 3: Making the Widgets Aware of the Custom Paint Engine</h2>
<p>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.</p>
<ul>
<li><a href="#implementing-a-custom-paint-device">Implementing a Custom Paint Device</a></li>
<li><a href="#implementing-a-custom-window-surface">Implementing a Custom Window Surface</a></li>
<li><a href="#adjusting-the-graphics-driver">Adjusting the Graphics Driver</a></li>
</ul>
<a name="implementing-a-custom-paint-device"></a>
<h3>Implementing a Custom Paint Device</h3>
<p>The custom paint device can be derived from the <a href="qcustomrasterpaintdevice.html">QCustomRasterPaintDevice</a> class. Reimplement its <a href="qpaintdevice.html#paintEngine">paintEngine()</a> and <a href="qcustomrasterpaintdevice.html#memory">memory()</a> functions to activate the accelerated paint engine:</p>
<pre> class SvgalibPaintDevice : public QCustomRasterPaintDevice
 {
 public:
     SvgalibPaintDevice(QWidget *w);
     ~SvgalibPaintDevice();

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

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

 private:
     SvgalibPaintEngine *pengine;
 };</pre>
<p>The <a href="qpaintdevice.html#paintEngine">paintEngine()</a> function should return an instance of the <tt>SvgalibPaintEngine</tt> class. The <a href="qcustomrasterpaintdevice.html#memory">memory()</a> function should return a pointer to the buffer which should be used when drawing the widget.</p>
<p>Our example driver is rendering directly to the screen without any buffering, i.e&#x2e;, our custom pain device's <a href="qcustomrasterpaintdevice.html#memory">memory()</a> function returns a pointer to the framebuffer. For this reason, we must also reimplement the <a href="qpaintdevice.html#metric">metric()</a> function to reflect the metrics of framebuffer.</p>
<a name="implementing-a-custom-window-surface"></a>
<h3>Implementing a Custom Window Surface</h3>
<p>The custom window surface can be derived from the QWSWindowSurface class. QWSWindowSurface manages the memory used when drawing a window.</p>
<pre> class SvgalibSurface : public QWSWindowSurface
 {
 public:
     SvgalibSurface();
     SvgalibSurface(QWidget *w);
     ~SvgalibSurface();

     void setGeometry(const QRect &amp;rect);
     bool isValid() const { return true; }
     bool scroll(const QRegion &amp;region, int dx, int dy);
     QString key() const { return QLatin1String(&quot;svgalib&quot;); }

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

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

 private:
     SvgalibPaintDevice *pdevice;
 };</pre>
<p>We can implement most of the pure virtual functions inherited from QWSWindowSurface as trivial inline functions, except the <a href="qwindowsurface.html#scroll">scroll()</a> function that actually makes use of some hardware acceleration:</p>
<pre> bool SvgalibSurface::scroll(const QRegion &amp;region, int dx, int dy)
 {
     const QVector&lt;QRect&gt; rects = region.rects();
     for (int i = 0; i &lt; rects.size(); ++i) {
         const QRect r = rects.at(i);
         gl_copybox(r.left(), r.top(), r.width(), r.height(),
                    r.left() + dx, r.top() + dy);
     }

     return true;
 }</pre>
<a name="adjusting-the-graphics-driver"></a>
<h3>Adjusting the Graphics Driver</h3>
<p>Finally, we enable the graphics driver to recognize an instance of our custom window surface:</p>
<pre> QWSWindowSurface* SvgalibScreen::createSurface(QWidget *widget) const
 {
     if (base()) {
         static int onScreenPaint = -1;
         if (onScreenPaint == -1)
             onScreenPaint = qgetenv(&quot;QT_ONSCREEN_PAINT&quot;).toInt();

         if (onScreenPaint &gt; 0 || widget-&gt;testAttribute(Qt::WA_PaintOnScreen))
             return new SvgalibSurface(widget);
     }
     return QScreen::createSurface(widget);
 }

 QWSWindowSurface* SvgalibScreen::createSurface(const QString &amp;key) const
 {
     if (key == QLatin1String(&quot;svgalib&quot;))
         return new SvgalibSurface;
     return QScreen::createSurface(key);
 }</pre>
<p>The <a href="qscreen.html#createSurface">createSurface()</a> 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 <a href="qt.html#WidgetAttribute-enum">Qt::WA_PaintOnScreen</a> attribute or the <a href="qt-embedded-envvars.html#qt-onscreen-paint">QT_ONSCREEN_PAINT</a> environment variable is set.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="40%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="40%" align="right"><div align="right">Qt 4.6.3</div></td>
</tr></table></div></address></body>
</html>