Sophie

Sophie

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

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">
<!-- dbscreen.qdoc -->
<head>
  <title>Qt 4.6: Double Buffered 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">Double Buffered Graphics Driver Example<br /><span class="subtitle"></span>
</h1>
<p>Files:</p>
<ul>
<li><a href="qws-dbscreen-dbscreen-cpp.html">qws/dbscreen/dbscreen.cpp</a></li>
<li><a href="qws-dbscreen-dbscreen-h.html">qws/dbscreen/dbscreen.h</a></li>
<li><a href="qws-dbscreen-dbscreendriverplugin-cpp.html">qws/dbscreen/dbscreendriverplugin.cpp</a></li>
<li><a href="qws-dbscreen-dbscreen-pro.html">qws/dbscreen/dbscreen.pro</a></li>
</ul>
<p>The Double Buffered Graphics Driver example shows how to write your own double buffered graphics driver and add it to Qt for Embedded Linux.</p>
<p>Similar to the <a href="qws-svgalib.html">Accelerated Graphics Driver Example</a>, there are three steps to writing and implementing this graphics driver:</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-the-back-buffer">Implementing the Back Buffer</a></li>
<li><a href="#step-3-creating-the-driver-plugin">Creating the Driver Plugin</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>
<p>Note that this is a minimal example and this driver will not work well with widgets painting themself directly to the screen (e.g&#x2e; widgets with the <a href="qt.html#WidgetAttribute-enum">Qt::WA_PaintOnScreen</a> 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 <a href="qt-embedded-testingframebuffer.html">Testing the Linux Framebuffer</a>.</p>
<a name="step-1-creating-a-custom-graphics-driver"></a>
<h2>Step 1: Creating a Custom Graphics Driver</h2>
<p>Usually, a custom graphics driver is created by subclassing the <a href="qscreen.html">QScreen</a> 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.</p>
<p>For our graphics driver, the <tt>DBScreen</tt> class, we reimplement five functions belonging to <a href="qscreen.html">QScreen</a>:</p>
<ul>
<li><a href="qscreen.html#initDevice">initDevice()</a>,</li>
<li><a href="qscreen.html#shutdownDevice">shutdownDevice()</a>,</li>
<li><a href="qscreen.html#blit">blit()</a>,</li>
<li><a href="qscreen.html#solidFill">solidFill()</a>, and</li>
<li><a href="qscreen.html#exposeRegion">exposeRegion()</a>.</li>
</ul>
<pre> class DBScreen : public QLinuxFbScreen
 {
 public:
     DBScreen(int displayId) : QLinuxFbScreen(displayId) {};
     ~DBScreen() {}
     bool initDevice();
     void shutdownDevice();
     void blit(const QImage &amp;image, const QPoint &amp;topLeft, const QRegion &amp;region);
     void solidFill(const QColor &amp;color, const QRegion &amp;region);
     void exposeRegion(QRegion region, int changing);

 private:
     QPainter *painter;
     QImage *image;
 };</pre>
<p>In addition to the abovementioned functions, there is a private instance of <a href="qpainter.html">QPainter</a> and <a href="qimage.html">QImage</a> - <tt>painter</tt>, used for drawing operations on the back buffer, and <tt>image</tt>, the back buffer itself.</p>
<a name="step-2-implementing-the-back-buffer"></a>
<h2>Step 2: Implementing the Back Buffer</h2>
<p>The graphics driver must carry out three main functions:</p>
<ol type="1">
<li>Allocate the back buffer on startup and deallocate it on shutdown.</li>
<li>Draw to the back buffer instead of directly to the screen (which is what QLinuxFbScreen does).</li>
<li>Copy the back buffer to the screen whenever a screen update is done.</li>
</ol>
<a name="device-initializing-and-shutdown"></a>
<h3>Device initializing and shutdown</h3>
<p>We first reimplement <tt>initDevice()</tt> and <tt>shutdownDevice()</tt>.</p>
<p>The <tt>initDevice()</tt> 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 <tt>false</tt>, we return <tt>false</tt>. Otherwise, we initialize the screen cursor with <a href="qscreencursor.html#initSoftwareCursor">QScreenCursor::initSoftwareCursor</a>() as well as instantiate <tt>image</tt> and <tt>painter</tt>. Then, we return <tt>true</tt>.</p>
<pre> bool DBScreen::initDevice()
 {
     if (!QLinuxFbScreen::initDevice())
         return false;

     QScreenCursor::initSoftwareCursor();
     image = new QImage(deviceWidth(), deviceHeight(), pixelFormat());
     painter = new QPainter(image);

     return true;
 }</pre>
<p>The <tt>shutdownDevice()</tt> 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.</p>
<pre> void DBScreen::shutdownDevice()
 {
     QLinuxFbScreen::shutdownDevice();
     delete painter;
     delete image;
 }</pre>
<p>Again, we call the super class implementation to shutdown the Linux framebuffer prior to deleting <tt>image</tt> and <tt>painter</tt>.</p>
<a name="drawing-to-the-back-buffer"></a>
<h3>Drawing to the back buffer</h3>
<p>We move on to the drawing functions - <tt>solidFill()</tt> and <tt>blit()</tt>. In QLinuxFbScreen, these functions draw directly to the Linux framebuffer; but in our driver we reimplement them to draw to the back buffer instead.</p>
<pre> void DBScreen::solidFill(const QColor &amp;color, const QRegion &amp;region)
 {
     QVector&lt;QRect&gt; rects = region.rects();
     for (int i = 0; i  &lt; rects.size(); i++)
         painter-&gt;fillRect(rects.at(i), color);
 }</pre>
<p>The <tt>solidFill()</tt> function is called from <tt>exposeRegion()</tt> to fill the given <tt>region</tt> of the screen with the specified <tt>color</tt>. In this example, we use <tt>painter</tt> to fill rectangles in <tt>image</tt>, the back buffer, according to the given region.</p>
<pre> void DBScreen::blit(const QImage &amp;image, const QPoint &amp;topLeft, const QRegion &amp;region)
 {
     QVector&lt;QRect&gt; rects = region.rects();
     for (int i = 0; i &lt; rects.size(); i++) {
         QRect destRect = rects.at(i);
         QRect srcRect(destRect.x()-topLeft.x(), destRect.y()-topLeft.y(), destRect.width(), destRect.height());
         painter-&gt;drawImage(destRect.topLeft(), image, srcRect);
     }
 }</pre>
<p>The <tt>blit()</tt> function is also called from <tt>exposeRegion()</tt> to copy the given <a href="qregion.html">QRegion</a> object, <tt>region</tt>, in the given <a href="qimage.html">QImage</a> object, <tt>image</tt>, to the <a href="qpoint.html">QPoint</a> object specified by <tt>topLeft</tt>. Once again we use <tt>painter</tt> to draw in the back buffer, <tt>image</tt>.</p>
<a name="displaying-the-buffer-on-the-screen"></a>
<h3>Displaying the buffer on the screen</h3>
<p>The <tt>exposeRegion()</tt> function is called by the Qt for Embedded Linux server whenever a screen update is required. The given <tt>region</tt> is the screen region that needs to be updated and <tt>changing</tt> is is the index into <a href="qwsserver.html#clientWindows">QWSServer::clientWindows</a>() of the window that caused the update.</p>
<pre> void DBScreen::exposeRegion(QRegion region, int changing)
 {
     QLinuxFbScreen::exposeRegion(region, changing);
     QLinuxFbScreen::blit(*image, QPoint(0, 0), region);
 }</pre>
<p>In our implementation, we first call the super class implementation to ensure that <tt>solidFill()</tt> and <tt>blit()</tt> will be called correctly. This causes the changed areas to be updated in the back buffer. We then call the super class' implementation of <tt>blit()</tt> to copy the updated region from the back buffer into the Linux framebuffer.</p>
<a name="step-3-creating-the-driver-plugin"></a>
<h2>Step 3: Creating the Driver Plugin</h2>
<p>Qt provides a high level API for writing Qt extentions. One of the plugin base classes provided is <a href="qscreendriverplugin.html">QScreenDriverPlugin</a>, which we use in this example to create our screen driver plugin.</p>
<pre> class DBScreenDriverPlugin : public QScreenDriverPlugin
 {
 public:
     DBScreenDriverPlugin();
     QScreen* create(const QString&amp; key, int displayId);
     QStringList keys () const;
 };</pre>
<p>There are only two functions to reimplement:</p>
<ul>
<li><a href="qscreendriverplugin.html#create">create()</a> - creates a driver matching the given key</li>
<li><a href="qscreendriverplugin.html#create">keys()</a> - returns a list of valid keys representing the drivers supported by the plugin</li>
</ul>
<pre> QScreen* DBScreenDriverPlugin::create(const QString&amp; key, int displayId)
 {
     if (key.toLower() != &quot;dbscreen&quot;)
         return 0;

     return new DBScreen(displayId);
 }

 QStringList DBScreenDriverPlugin::keys() const
 {
     return QStringList() &lt;&lt; &quot;dbscreen&quot;;
 }</pre>
<p>Our plugin will only support one driver, <tt>dbscreen</tt>.</p>
<p>Lastly, we export the plugin.</p>
<pre> Q_EXPORT_PLUGIN2(dbscreen, DBScreenDriverPlugin)</pre>
<p>For detailed information about the Qt plugin system see <a href="plugins-howto.html">How to Create Qt Plugins.</a></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>