util/src/plugins/gfxdrivers/directfb/qdirectfbwindowsurface.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the plugins of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qdirectfbwindowsurface.h"
       
    43 #include "qdirectfbscreen.h"
       
    44 #include "qdirectfbpaintengine.h"
       
    45 
       
    46 #include <private/qwidget_p.h>
       
    47 #include <qwidget.h>
       
    48 #include <qwindowsystem_qws.h>
       
    49 #include <qpaintdevice.h>
       
    50 #include <qvarlengtharray.h>
       
    51 
       
    52 #ifndef QT_NO_QWS_DIRECTFB
       
    53 
       
    54 QT_BEGIN_NAMESPACE
       
    55 
       
    56 QDirectFBWindowSurface::QDirectFBWindowSurface(DFBSurfaceFlipFlags flip, QDirectFBScreen *scr)
       
    57     : QDirectFBPaintDevice(scr)
       
    58     , sibling(0)
       
    59 #ifndef QT_NO_DIRECTFB_WM
       
    60     , dfbWindow(0)
       
    61 #endif
       
    62     , flipFlags(flip)
       
    63     , boundingRectFlip(scr->directFBFlags() & QDirectFBScreen::BoundingRectFlip)
       
    64     , flushPending(false)
       
    65 {
       
    66 #ifdef QT_NO_DIRECTFB_WM
       
    67     mode = Offscreen;
       
    68 #endif
       
    69     setSurfaceFlags(Opaque | Buffered);
       
    70 #ifdef QT_DIRECTFB_TIMING
       
    71     frames = 0;
       
    72     timer.start();
       
    73 #endif
       
    74 }
       
    75 
       
    76 QDirectFBWindowSurface::QDirectFBWindowSurface(DFBSurfaceFlipFlags flip, QDirectFBScreen *scr, QWidget *widget)
       
    77     : QWSWindowSurface(widget), QDirectFBPaintDevice(scr)
       
    78     , sibling(0)
       
    79 #ifndef QT_NO_DIRECTFB_WM
       
    80     , dfbWindow(0)
       
    81 #endif
       
    82     , flipFlags(flip)
       
    83     , boundingRectFlip(scr->directFBFlags() & QDirectFBScreen::BoundingRectFlip)
       
    84     , flushPending(false)
       
    85 {
       
    86     SurfaceFlags flags = 0;
       
    87     if (!widget || widget->window()->windowOpacity() == 0xff)
       
    88         flags |= Opaque;
       
    89 #ifdef QT_NO_DIRECTFB_WM
       
    90     if (widget && widget->testAttribute(Qt::WA_PaintOnScreen)) {
       
    91         flags = RegionReserved;
       
    92         mode = Primary;
       
    93     } else {
       
    94         mode = Offscreen;
       
    95         flags = Buffered;
       
    96     }
       
    97 #endif
       
    98     setSurfaceFlags(flags);
       
    99 #ifdef QT_DIRECTFB_TIMING
       
   100     frames = 0;
       
   101     timer.start();
       
   102 #endif
       
   103 }
       
   104 
       
   105 QDirectFBWindowSurface::~QDirectFBWindowSurface()
       
   106 {
       
   107     releaseSurface();
       
   108     // these are not tracked by QDirectFBScreen so we don't want QDirectFBPaintDevice to release it
       
   109 }
       
   110 
       
   111 bool QDirectFBWindowSurface::isValid() const
       
   112 {
       
   113     return true;
       
   114 }
       
   115 
       
   116 #ifdef QT_DIRECTFB_WM
       
   117 void QDirectFBWindowSurface::raise()
       
   118 {
       
   119     if (IDirectFBWindow *window = directFBWindow()) {
       
   120         window->RaiseToTop(window);
       
   121     }
       
   122 }
       
   123 
       
   124 IDirectFBWindow *QDirectFBWindowSurface::directFBWindow() const
       
   125 {
       
   126     return (dfbWindow ? dfbWindow : (sibling ? sibling->dfbWindow : 0));
       
   127 }
       
   128 
       
   129 void QDirectFBWindowSurface::createWindow(const QRect &rect)
       
   130 {
       
   131     IDirectFBDisplayLayer *layer = screen->dfbDisplayLayer();
       
   132     if (!layer)
       
   133         qFatal("QDirectFBWindowSurface: Unable to get primary display layer!");
       
   134 
       
   135     updateIsOpaque();
       
   136 
       
   137     DFBWindowDescription description;
       
   138     memset(&description, 0, sizeof(DFBWindowDescription));
       
   139 
       
   140     description.flags = DWDESC_CAPS|DWDESC_HEIGHT|DWDESC_WIDTH|DWDESC_POSX|DWDESC_POSY|DWDESC_SURFACE_CAPS|DWDESC_PIXELFORMAT;
       
   141     description.caps = DWCAPS_NODECORATION;
       
   142     description.surface_caps = DSCAPS_NONE;
       
   143     imageFormat = screen->pixelFormat();
       
   144 
       
   145     if (!(surfaceFlags() & Opaque)) {
       
   146         imageFormat = screen->alphaPixmapFormat();
       
   147         description.caps |= DWCAPS_ALPHACHANNEL;
       
   148 #if (Q_DIRECTFB_VERSION >= 0x010200)
       
   149         description.flags |= DWDESC_OPTIONS;
       
   150         description.options |= DWOP_ALPHACHANNEL;
       
   151 #endif
       
   152     }
       
   153     description.pixelformat = QDirectFBScreen::getSurfacePixelFormat(imageFormat);
       
   154     description.posx = rect.x();
       
   155     description.posy = rect.y();
       
   156     description.width = rect.width();
       
   157     description.height = rect.height();
       
   158 
       
   159     if (QDirectFBScreen::isPremultiplied(imageFormat))
       
   160         description.surface_caps = DSCAPS_PREMULTIPLIED;
       
   161 
       
   162     if (screen->directFBFlags() & QDirectFBScreen::VideoOnly)
       
   163         description.surface_caps |= DSCAPS_VIDEOONLY;
       
   164 
       
   165     DFBResult result = layer->CreateWindow(layer, &description, &dfbWindow);
       
   166 
       
   167     if (result != DFB_OK)
       
   168         DirectFBErrorFatal("QDirectFBWindowSurface::createWindow", result);
       
   169 
       
   170     if (window()) {
       
   171         if (window()->windowFlags() & Qt::WindowStaysOnTopHint) {
       
   172             dfbWindow->SetStackingClass(dfbWindow, DWSC_UPPER);
       
   173         }
       
   174         DFBWindowID winid;
       
   175         result = dfbWindow->GetID(dfbWindow, &winid);
       
   176         if (result != DFB_OK) {
       
   177             DirectFBError("QDirectFBWindowSurface::createWindow. Can't get ID", result);
       
   178         } else {
       
   179             window()->setProperty("_q_DirectFBWindowID", winid);
       
   180         }
       
   181     }
       
   182 
       
   183     Q_ASSERT(!dfbSurface);
       
   184     dfbWindow->GetSurface(dfbWindow, &dfbSurface);
       
   185 }
       
   186 
       
   187 static DFBResult setWindowGeometry(IDirectFBWindow *dfbWindow, const QRect &old, const QRect &rect)
       
   188 {
       
   189     DFBResult result = DFB_OK;
       
   190     const bool isMove = old.isEmpty() || rect.topLeft() != old.topLeft();
       
   191     const bool isResize = rect.size() != old.size();
       
   192 
       
   193 #if (Q_DIRECTFB_VERSION >= 0x010000)
       
   194     if (isResize && isMove) {
       
   195         result = dfbWindow->SetBounds(dfbWindow, rect.x(), rect.y(),
       
   196                                       rect.width(), rect.height());
       
   197     } else if (isResize) {
       
   198         result = dfbWindow->Resize(dfbWindow,
       
   199                                    rect.width(), rect.height());
       
   200     } else if (isMove) {
       
   201         result = dfbWindow->MoveTo(dfbWindow, rect.x(), rect.y());
       
   202     }
       
   203 #else
       
   204     if (isResize) {
       
   205         result = dfbWindow->Resize(dfbWindow,
       
   206                                    rect.width(), rect.height());
       
   207     }
       
   208     if (isMove) {
       
   209         result = dfbWindow->MoveTo(dfbWindow, rect.x(), rect.y());
       
   210     }
       
   211 #endif
       
   212     return result;
       
   213 }
       
   214 #endif // QT_NO_DIRECTFB_WM
       
   215 
       
   216 void QDirectFBWindowSurface::setGeometry(const QRect &rect)
       
   217 {
       
   218     const QRect oldRect = geometry();
       
   219     if (oldRect == rect)
       
   220         return;
       
   221 
       
   222     IDirectFBSurface *oldSurface = dfbSurface;
       
   223     const bool sizeChanged = oldRect.size() != rect.size();
       
   224     if (sizeChanged) {
       
   225         delete engine;
       
   226         engine = 0;
       
   227         releaseSurface();
       
   228         Q_ASSERT(!dfbSurface);
       
   229     }
       
   230 
       
   231     if (rect.isNull()) {
       
   232 #ifndef QT_NO_DIRECTFB_WM
       
   233         if (dfbWindow) {
       
   234             if (window())
       
   235                 window()->setProperty("_q_DirectFBWindowID", QVariant());
       
   236 
       
   237             dfbWindow->Release(dfbWindow);
       
   238             dfbWindow = 0;
       
   239         }
       
   240 #endif
       
   241         Q_ASSERT(!dfbSurface);
       
   242 #ifdef QT_DIRECTFB_SUBSURFACE
       
   243         Q_ASSERT(!subSurface);
       
   244 #endif
       
   245     } else {
       
   246 #ifdef QT_DIRECTFB_WM
       
   247         if (!dfbWindow) {
       
   248             createWindow(rect);
       
   249         } else {
       
   250             setWindowGeometry(dfbWindow, oldRect, rect);
       
   251             Q_ASSERT(!sizeChanged || !dfbSurface);
       
   252             if (sizeChanged)
       
   253                 dfbWindow->GetSurface(dfbWindow, &dfbSurface);
       
   254         }
       
   255 #else
       
   256         IDirectFBSurface *primarySurface = screen->primarySurface();
       
   257         DFBResult result = DFB_OK;
       
   258         if (mode == Primary) {
       
   259             Q_ASSERT(primarySurface);
       
   260             if (rect == screen->region().boundingRect()) {
       
   261                 dfbSurface = primarySurface;
       
   262             } else {
       
   263                 const DFBRectangle r = { rect.x(), rect.y(),
       
   264                                          rect.width(), rect.height() };
       
   265                 result = primarySurface->GetSubSurface(primarySurface, &r, &dfbSurface);
       
   266             }
       
   267         } else { // mode == Offscreen
       
   268             if (!dfbSurface) {
       
   269                 dfbSurface = screen->createDFBSurface(rect.size(), surfaceFlags() & Opaque ? screen->pixelFormat() : screen->alphaPixmapFormat(),
       
   270                                                       QDirectFBScreen::DontTrackSurface);
       
   271             }
       
   272         }
       
   273         if (result != DFB_OK)
       
   274             DirectFBErrorFatal("QDirectFBWindowSurface::setGeometry()", result);
       
   275 #endif
       
   276     }
       
   277     if (oldSurface != dfbSurface) {
       
   278         imageFormat = dfbSurface ? QDirectFBScreen::getImageFormat(dfbSurface) : QImage::Format_Invalid;
       
   279     }
       
   280 
       
   281     if (oldRect.size() != rect.size()) {
       
   282         QWSWindowSurface::setGeometry(rect);
       
   283     } else {
       
   284         QWindowSurface::setGeometry(rect);
       
   285     }
       
   286 }
       
   287 
       
   288 QByteArray QDirectFBWindowSurface::permanentState() const
       
   289 {
       
   290     QByteArray state(sizeof(this), 0);
       
   291     *reinterpret_cast<const QDirectFBWindowSurface**>(state.data()) = this;
       
   292     return state;
       
   293 }
       
   294 
       
   295 void QDirectFBWindowSurface::setPermanentState(const QByteArray &state)
       
   296 {
       
   297     if (state.size() == sizeof(this)) {
       
   298         sibling = *reinterpret_cast<QDirectFBWindowSurface *const*>(state.constData());
       
   299         Q_ASSERT(sibling);
       
   300         setSurfaceFlags(sibling->surfaceFlags());
       
   301     }
       
   302 }
       
   303 
       
   304 bool QDirectFBWindowSurface::scroll(const QRegion &region, int dx, int dy)
       
   305 {
       
   306     if (!dfbSurface || !(flipFlags & DSFLIP_BLIT) || region.rectCount() != 1)
       
   307         return false;
       
   308     if (flushPending) {
       
   309         dfbSurface->Flip(dfbSurface, 0, DSFLIP_BLIT);
       
   310     } else {
       
   311         flushPending = true;
       
   312     }
       
   313     dfbSurface->SetBlittingFlags(dfbSurface, DSBLIT_NOFX);
       
   314     const QRect r = region.boundingRect();
       
   315     const DFBRectangle rect = { r.x(), r.y(), r.width(), r.height() };
       
   316     dfbSurface->Blit(dfbSurface, dfbSurface, &rect, r.x() + dx, r.y() + dy);
       
   317     return true;
       
   318 }
       
   319 
       
   320 bool QDirectFBWindowSurface::move(const QPoint &moveBy)
       
   321 {
       
   322     setGeometry(geometry().translated(moveBy));
       
   323     return true;
       
   324 }
       
   325 
       
   326 void QDirectFBWindowSurface::setOpaque(bool opaque)
       
   327 {
       
   328     SurfaceFlags flags = surfaceFlags();
       
   329     if (opaque != (flags & Opaque)) {
       
   330         if (opaque) {
       
   331             flags |= Opaque;
       
   332         } else {
       
   333             flags &= ~Opaque;
       
   334         }
       
   335         setSurfaceFlags(flags);
       
   336     }
       
   337 }
       
   338 
       
   339 
       
   340 void QDirectFBWindowSurface::flush(QWidget *widget, const QRegion &region,
       
   341                                    const QPoint &offset)
       
   342 {
       
   343     QWidget *win = window();
       
   344     if (!win)
       
   345         return;
       
   346 
       
   347     QWExtra *extra = qt_widget_private(widget)->extraData();
       
   348     if (extra && extra->proxyWidget)
       
   349         return;
       
   350 
       
   351     const quint8 windowOpacity = quint8(win->windowOpacity() * 0xff);
       
   352     const QRect windowGeometry = geometry();
       
   353 #ifdef QT_DIRECTFB_WM
       
   354     quint8 currentOpacity;
       
   355     Q_ASSERT(dfbWindow);
       
   356     dfbWindow->GetOpacity(dfbWindow, &currentOpacity);
       
   357     if (currentOpacity != windowOpacity) {
       
   358         dfbWindow->SetOpacity(dfbWindow, windowOpacity);
       
   359     }
       
   360 
       
   361     screen->flipSurface(dfbSurface, flipFlags, region, offset);
       
   362 #else
       
   363     setOpaque(windowOpacity == 0xff);
       
   364     if (mode == Offscreen) {
       
   365         screen->exposeRegion(region.translated(offset + geometry().topLeft()), 0);
       
   366     } else {
       
   367         screen->flipSurface(dfbSurface, flipFlags, region, offset);
       
   368     }
       
   369 #endif
       
   370 
       
   371 #ifdef QT_DIRECTFB_TIMING
       
   372     enum { Secs = 3 };
       
   373     ++frames;
       
   374     if (timer.elapsed() >= Secs * 1000) {
       
   375         qDebug("%d fps", int(double(frames) / double(Secs)));
       
   376         frames = 0;
       
   377         timer.restart();
       
   378     }
       
   379 #endif
       
   380     flushPending = false;
       
   381 }
       
   382 
       
   383 void QDirectFBWindowSurface::beginPaint(const QRegion &)
       
   384 {
       
   385     if (!engine) {
       
   386         engine = new QDirectFBPaintEngine(this);
       
   387     }
       
   388     flushPending = true;
       
   389 }
       
   390 
       
   391 void QDirectFBWindowSurface::endPaint(const QRegion &)
       
   392 {
       
   393 #ifdef QT_NO_DIRECTFB_SUBSURFACE
       
   394     unlockSurface();
       
   395 #endif
       
   396 }
       
   397 
       
   398 IDirectFBSurface *QDirectFBWindowSurface::directFBSurface() const
       
   399 {
       
   400     if (!dfbSurface && sibling && sibling->dfbSurface)
       
   401         return sibling->dfbSurface;
       
   402     return dfbSurface;
       
   403 }
       
   404 
       
   405 
       
   406 IDirectFBSurface *QDirectFBWindowSurface::surfaceForWidget(const QWidget *widget, QRect *rect) const
       
   407 {
       
   408     Q_ASSERT(widget);
       
   409     if (!dfbSurface) {
       
   410         if (sibling && (!sibling->sibling || sibling->dfbSurface))
       
   411             return sibling->surfaceForWidget(widget, rect);
       
   412         return 0;
       
   413     }
       
   414     QWidget *win = window();
       
   415     Q_ASSERT(win);
       
   416     if (rect) {
       
   417         if (win == widget) {
       
   418             *rect = widget->rect();
       
   419         } else {
       
   420             *rect = QRect(widget->mapTo(win, QPoint(0, 0)), widget->size());
       
   421         }
       
   422     }
       
   423 
       
   424     Q_ASSERT(win == widget || win->isAncestorOf(widget));
       
   425     return dfbSurface;
       
   426 }
       
   427 
       
   428 void QDirectFBWindowSurface::releaseSurface()
       
   429 {
       
   430     if (dfbSurface) {
       
   431 #ifdef QT_DIRECTFB_SUBSURFACE
       
   432         releaseSubSurface();
       
   433 #else
       
   434         unlockSurface();
       
   435 #endif
       
   436 #ifdef QT_NO_DIRECTFB_WM
       
   437         Q_ASSERT(screen->primarySurface());
       
   438         if (dfbSurface != screen->primarySurface())
       
   439 #endif
       
   440 
       
   441             dfbSurface->Release(dfbSurface);
       
   442         dfbSurface = 0;
       
   443     }
       
   444 }
       
   445 
       
   446 void QDirectFBWindowSurface::updateIsOpaque()
       
   447 {
       
   448     const QWidget *win = window();
       
   449     Q_ASSERT(win);
       
   450     if (win->testAttribute(Qt::WA_OpaquePaintEvent) || win->testAttribute(Qt::WA_PaintOnScreen)) {
       
   451         setOpaque(true);
       
   452         return;
       
   453     }
       
   454 
       
   455     if (qFuzzyCompare(static_cast<float>(win->windowOpacity()), 1.0f)) {
       
   456         const QPalette &pal = win->palette();
       
   457 
       
   458         if (win->autoFillBackground()) {
       
   459             const QBrush &autoFillBrush = pal.brush(win->backgroundRole());
       
   460             if (autoFillBrush.style() != Qt::NoBrush && autoFillBrush.isOpaque()) {
       
   461                 setOpaque(true);
       
   462                 return;
       
   463             }
       
   464         }
       
   465 
       
   466         if (win->isWindow() && !win->testAttribute(Qt::WA_NoSystemBackground)) {
       
   467             const QBrush &windowBrush = win->palette().brush(QPalette::Window);
       
   468             if (windowBrush.style() != Qt::NoBrush && windowBrush.isOpaque()) {
       
   469                 setOpaque(true);
       
   470                 return;
       
   471             }
       
   472         }
       
   473     }
       
   474     setOpaque(false);
       
   475 }
       
   476 
       
   477 QT_END_NAMESPACE
       
   478 
       
   479 #endif // QT_NO_QWS_DIRECTFB