src/gui/painting/qwindowsurface_raster.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 QtGui module 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 <qdebug.h>
       
    43 
       
    44 #include <qglobal.h> // for Q_WS_WIN define (non-PCH)
       
    45 #ifdef Q_WS_WIN
       
    46 #include <qlibrary.h>
       
    47 #include <qt_windows.h>
       
    48 #endif
       
    49 
       
    50 #include <QtGui/qpaintdevice.h>
       
    51 #include <QtGui/qwidget.h>
       
    52 
       
    53 #include "private/qwindowsurface_raster_p.h"
       
    54 #include "private/qnativeimage_p.h"
       
    55 #include "private/qwidget_p.h"
       
    56 
       
    57 #ifdef Q_WS_X11
       
    58 #include "private/qpixmap_x11_p.h"
       
    59 #include "private/qt_x11_p.h"
       
    60 #include "private/qwidget_p.h"
       
    61 #include "qx11info_x11.h"
       
    62 #endif
       
    63 #include "private/qdrawhelper_p.h"
       
    64 
       
    65 #ifdef Q_WS_MAC
       
    66 #include <private/qt_cocoa_helpers_mac_p.h>
       
    67 #endif
       
    68 
       
    69 QT_BEGIN_NAMESPACE
       
    70 
       
    71 class QRasterWindowSurfacePrivate
       
    72 {
       
    73 public:
       
    74     QNativeImage *image;
       
    75 
       
    76 #ifdef Q_WS_X11
       
    77     GC gc;
       
    78 #ifndef QT_NO_XRENDER
       
    79     uint translucentBackground : 1;
       
    80 #endif
       
    81 #endif
       
    82     uint inSetGeometry : 1;
       
    83 };
       
    84 
       
    85 QRasterWindowSurface::QRasterWindowSurface(QWidget *window)
       
    86     : QWindowSurface(window), d_ptr(new QRasterWindowSurfacePrivate)
       
    87 {
       
    88 #ifdef Q_WS_X11
       
    89     d_ptr->gc = XCreateGC(X11->display, window->handle(), 0, 0);
       
    90 #ifndef QT_NO_XRENDER
       
    91     d_ptr->translucentBackground = X11->use_xrender
       
    92         && window->x11Info().depth() == 32;
       
    93 #endif
       
    94 #endif
       
    95     d_ptr->image = 0;
       
    96     d_ptr->inSetGeometry = false;
       
    97     setStaticContentsSupport(true);
       
    98 }
       
    99 
       
   100 
       
   101 QRasterWindowSurface::~QRasterWindowSurface()
       
   102 {
       
   103 #ifdef Q_WS_X11
       
   104     XFreeGC(X11->display, d_ptr->gc);
       
   105 #endif
       
   106     if (d_ptr->image)
       
   107         delete d_ptr->image;
       
   108 }
       
   109 
       
   110 
       
   111 QPaintDevice *QRasterWindowSurface::paintDevice()
       
   112 {
       
   113     return &d_ptr->image->image;
       
   114 }
       
   115 
       
   116 void QRasterWindowSurface::beginPaint(const QRegion &rgn)
       
   117 {
       
   118 #if (defined(Q_WS_X11) && !defined(QT_NO_XRENDER)) || (defined(Q_WS_WIN) && !defined(Q_WS_WINCE))
       
   119     if (!qt_widget_private(window())->isOpaque && window()->testAttribute(Qt::WA_TranslucentBackground)) {
       
   120 #if defined(Q_WS_WIN) && !defined(Q_WS_WINCE)
       
   121         if (d_ptr->image->image.format() != QImage::Format_ARGB32_Premultiplied)
       
   122             prepareBuffer(QImage::Format_ARGB32_Premultiplied, window());
       
   123 #endif
       
   124         QPainter p(&d_ptr->image->image);
       
   125         p.setCompositionMode(QPainter::CompositionMode_Source);
       
   126         const QVector<QRect> rects = rgn.rects();
       
   127         const QColor blank = Qt::transparent;
       
   128         for (QVector<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it) {
       
   129             p.fillRect(*it, blank);
       
   130         }
       
   131     }
       
   132 #else
       
   133     Q_UNUSED(rgn);
       
   134 #endif
       
   135 }
       
   136 
       
   137 void QRasterWindowSurface::flush(QWidget *widget, const QRegion &rgn, const QPoint &offset)
       
   138 {
       
   139     Q_D(QRasterWindowSurface);
       
   140 
       
   141     // Not ready for painting yet, bail out. This can happen in
       
   142     // QWidget::create_sys()
       
   143     if (!d->image || rgn.numRects() == 0)
       
   144         return;
       
   145 
       
   146 #ifdef Q_WS_WIN
       
   147     QRect br = rgn.boundingRect();
       
   148 
       
   149 #ifndef Q_WS_WINCE
       
   150     if (!qt_widget_private(window())->isOpaque
       
   151         && window()->testAttribute(Qt::WA_TranslucentBackground)
       
   152         && (qt_widget_private(window())->data.window_flags & Qt::FramelessWindowHint))
       
   153     {
       
   154         QRect r = window()->frameGeometry();
       
   155         QPoint frameOffset = qt_widget_private(window())->frameStrut().topLeft();
       
   156         QRect dirtyRect = br.translated(offset + frameOffset);
       
   157 
       
   158         SIZE size = {r.width(), r.height()};
       
   159         POINT ptDst = {r.x(), r.y()};
       
   160         POINT ptSrc = {0, 0};
       
   161         BLENDFUNCTION blend = {AC_SRC_OVER, 0, (int)(255.0 * window()->windowOpacity()), Q_AC_SRC_ALPHA};
       
   162         RECT dirty = {dirtyRect.x(), dirtyRect.y(),
       
   163             dirtyRect.x() + dirtyRect.width(), dirtyRect.y() + dirtyRect.height()};
       
   164         Q_UPDATELAYEREDWINDOWINFO info = {sizeof(info), NULL, &ptDst, &size, d->image->hdc, &ptSrc, 0, &blend, Q_ULW_ALPHA, &dirty};
       
   165         ptrUpdateLayeredWindowIndirect(window()->internalWinId(), &info);
       
   166     } else
       
   167 #endif
       
   168     {
       
   169         QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft();
       
   170 
       
   171         HDC widget_dc = widget->getDC();
       
   172 
       
   173         QRect wbr = br.translated(-wOffset);
       
   174         BitBlt(widget_dc, wbr.x(), wbr.y(), wbr.width(), wbr.height(),
       
   175                d->image->hdc, br.x() + offset.x(), br.y() + offset.y(), SRCCOPY);
       
   176         widget->releaseDC(widget_dc);
       
   177     }
       
   178 
       
   179 #ifndef QT_NO_DEBUG
       
   180     static bool flush = !qgetenv("QT_FLUSH_WINDOWSURFACE").isEmpty();
       
   181     if (flush) {
       
   182         SelectObject(qt_win_display_dc(), GetStockObject(BLACK_BRUSH));
       
   183         Rectangle(qt_win_display_dc(), 0, 0, d->image->width() + 2, d->image->height() + 2);
       
   184         BitBlt(qt_win_display_dc(), 1, 1, d->image->width(), d->image->height(),
       
   185                d->image->hdc, 0, 0, SRCCOPY);
       
   186     }
       
   187 #endif
       
   188 
       
   189 #endif
       
   190 
       
   191 #ifdef Q_WS_X11
       
   192     extern void *qt_getClipRects(const QRegion &r, int &num); // in qpaintengine_x11.cpp
       
   193     extern QWidgetData* qt_widget_data(QWidget *);
       
   194     QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft();
       
   195 
       
   196     if (widget->window() != window()) {
       
   197         XFreeGC(X11->display, d_ptr->gc);
       
   198         d_ptr->gc = XCreateGC(X11->display, widget->handle(), 0, 0);
       
   199     }
       
   200 
       
   201     QRegion wrgn(rgn);
       
   202     if (!wOffset.isNull())
       
   203         wrgn.translate(-wOffset);
       
   204     QRect wbr = wrgn.boundingRect();
       
   205 
       
   206     if (wrgn.numRects() != 1) {
       
   207         int num;
       
   208         XRectangle *rects = (XRectangle *)qt_getClipRects(wrgn, num);
       
   209         XSetClipRectangles(X11->display, d_ptr->gc, 0, 0, rects, num, YXBanded);
       
   210     }
       
   211 
       
   212     QRect br = rgn.boundingRect().translated(offset);
       
   213 #ifndef QT_NO_MITSHM
       
   214     if (d_ptr->image->xshmpm) {
       
   215         XCopyArea(X11->display, d_ptr->image->xshmpm, widget->handle(), d_ptr->gc,
       
   216                   br.x(), br.y(), br.width(), br.height(), wbr.x(), wbr.y());
       
   217         XSync(X11->display, False);
       
   218     } else
       
   219 #endif
       
   220     {
       
   221         const QImage &src = d->image->image;
       
   222         br = br.intersected(src.rect());
       
   223         if (src.format() != QImage::Format_RGB32 || widget->x11Info().depth() < 24) {
       
   224             Q_ASSERT(src.depth() >= 16);
       
   225             const QImage sub_src(src.scanLine(br.y()) + br.x() * (uint(src.depth()) / 8),
       
   226                                  br.width(), br.height(), src.bytesPerLine(), src.format());
       
   227             QX11PixmapData *data = new QX11PixmapData(QPixmapData::PixmapType);
       
   228             data->xinfo = widget->x11Info();
       
   229             data->fromImage(sub_src, Qt::NoOpaqueDetection);
       
   230             QPixmap pm = QPixmap(data);
       
   231             XCopyArea(X11->display, pm.handle(), widget->handle(), d_ptr->gc, 0 , 0 , br.width(), br.height(), wbr.x(), wbr.y());
       
   232         } else {
       
   233             // qpaintengine_x11.cpp
       
   234             extern void qt_x11_drawImage(const QRect &rect, const QPoint &pos, const QImage &image, Drawable hd, GC gc, Display *dpy, Visual *visual, int depth);
       
   235             qt_x11_drawImage(br, wbr.topLeft(), src, widget->handle(), d_ptr->gc, X11->display, (Visual *)widget->x11Info().visual(), widget->x11Info().depth());
       
   236         }
       
   237     }
       
   238 
       
   239     if (wrgn.numRects() != 1)
       
   240         XSetClipMask(X11->display, d_ptr->gc, XNone);
       
   241 #endif // FALCON
       
   242 
       
   243 #ifdef Q_WS_MAC
       
   244 
       
   245 //     qDebug() << "Flushing" << widget << rgn << offset;
       
   246 
       
   247 //     d->image->image.save("flush.png");
       
   248 
       
   249     Q_UNUSED(offset);
       
   250     // Get a context for the widget.
       
   251 #ifndef QT_MAC_USE_COCOA
       
   252     CGContextRef context;
       
   253     CGrafPtr port = GetWindowPort(qt_mac_window_for(widget));
       
   254     QDBeginCGContext(port, &context);
       
   255 #else
       
   256     extern CGContextRef qt_mac_graphicsContextFor(QWidget *);
       
   257     CGContextRef context = qt_mac_graphicsContextFor(widget);
       
   258 #endif
       
   259     CGContextSaveGState(context);
       
   260 
       
   261     // Flip context.
       
   262     CGContextTranslateCTM(context, 0, widget->height());
       
   263     CGContextScaleCTM(context, 1, -1);
       
   264 
       
   265     // Clip to region.
       
   266     const QVector<QRect> &rects = rgn.rects();
       
   267     for (int i = 0; i < rects.size(); ++i) {
       
   268         const QRect &rect = rects.at(i);
       
   269         CGContextAddRect(context, CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()));
       
   270     }
       
   271     CGContextClip(context);
       
   272 
       
   273     QRect r = rgn.boundingRect();
       
   274     const CGRect area = CGRectMake(r.x(), r.y(), r.width(), r.height());
       
   275     CGImageRef image = CGBitmapContextCreateImage(d->image->cg);
       
   276     CGImageRef subImage = CGImageCreateWithImageInRect(image, area);
       
   277 
       
   278     qt_mac_drawCGImage(context, &area, subImage);
       
   279     CGImageRelease(subImage);
       
   280     CGImageRelease(image);
       
   281 
       
   282 //     CGSize size = { d->image->image.width(), d->image->image.height() };
       
   283 //     CGLayerRef layer = CGLayerCreateWithContext(d->image->cg, size, 0);
       
   284 //     CGPoint pt = { 0, 0 };
       
   285 //     CGContextDrawLayerAtPoint(context, pt, layer);
       
   286 //     CGLayerRelease(layer);
       
   287 
       
   288     // Restore context.
       
   289     CGContextRestoreGState(context);
       
   290 #ifndef QT_MAC_USE_COCOA
       
   291     QDEndCGContext(port, &context);
       
   292 #else
       
   293     CGContextFlush(context);
       
   294 #endif
       
   295 #endif
       
   296 
       
   297 #ifdef Q_OS_SYMBIAN
       
   298     Q_UNUSED(widget);
       
   299     Q_UNUSED(rgn);
       
   300     Q_UNUSED(offset);
       
   301 #endif
       
   302 }
       
   303 
       
   304 void QRasterWindowSurface::setGeometry(const QRect &rect)
       
   305 {
       
   306     QWindowSurface::setGeometry(rect);
       
   307     Q_D(QRasterWindowSurface);
       
   308     d->inSetGeometry = true;
       
   309     if (d->image == 0 || d->image->width() < rect.width() || d->image->height() < rect.height()) {
       
   310 #if (defined(Q_WS_X11) && !defined(QT_NO_XRENDER)) || (defined(Q_WS_WIN) && !defined(Q_WS_WINCE))
       
   311 #ifndef Q_WS_WIN
       
   312         if (d_ptr->translucentBackground)
       
   313 #else
       
   314         if (!qt_widget_private(window())->isOpaque)
       
   315 #endif
       
   316             prepareBuffer(QImage::Format_ARGB32_Premultiplied, window());
       
   317         else
       
   318 #endif
       
   319             prepareBuffer(QNativeImage::systemFormat(), window());
       
   320     }
       
   321     d->inSetGeometry = false;
       
   322 }
       
   323 
       
   324 // from qwindowsurface.cpp
       
   325 extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);
       
   326 
       
   327 bool QRasterWindowSurface::scroll(const QRegion &area, int dx, int dy)
       
   328 {
       
   329 #ifdef Q_WS_WIN
       
   330     Q_D(QRasterWindowSurface);
       
   331 
       
   332     if (!d->image || !d->image->hdc)
       
   333         return false;
       
   334 
       
   335     QRect rect = area.boundingRect();
       
   336     BitBlt(d->image->hdc, rect.x()+dx, rect.y()+dy, rect.width(), rect.height(),
       
   337            d->image->hdc, rect.x(), rect.y(), SRCCOPY);
       
   338 
       
   339     return true;
       
   340 #else
       
   341     Q_D(QRasterWindowSurface);
       
   342 
       
   343     if (!d->image || d->image->image.isNull())
       
   344         return false;
       
   345 
       
   346     const QVector<QRect> rects = area.rects();
       
   347     for (int i = 0; i < rects.size(); ++i)
       
   348         qt_scrollRectInImage(d->image->image, rects.at(i), QPoint(dx, dy));
       
   349 
       
   350     return true;
       
   351 #endif
       
   352 }
       
   353 
       
   354 
       
   355 void QRasterWindowSurface::prepareBuffer(QImage::Format format, QWidget *widget)
       
   356 {
       
   357     Q_D(QRasterWindowSurface);
       
   358 
       
   359     int width = window()->width();
       
   360     int height = window()->height();
       
   361     if (d->image) {
       
   362         width = qMax(d->image->width(), width);
       
   363         height = qMax(d->image->height(), height);
       
   364     }
       
   365 
       
   366     if (width == 0 || height == 0) {
       
   367         delete d->image;
       
   368         d->image = 0;
       
   369         return;
       
   370     }
       
   371 
       
   372     QNativeImage *oldImage = d->image;
       
   373 
       
   374     d->image = new QNativeImage(width, height, format, false, widget);
       
   375 
       
   376     if (oldImage && d->inSetGeometry && hasStaticContents()) {
       
   377         // Make sure we use the const version of bits() (no detach).
       
   378         const uchar *src = const_cast<const QImage &>(oldImage->image).bits();
       
   379         uchar *dst = d->image->image.bits();
       
   380 
       
   381         const int srcBytesPerLine = oldImage->image.bytesPerLine();
       
   382         const int dstBytesPerLine = d->image->image.bytesPerLine();
       
   383         const int bytesPerPixel = oldImage->image.depth() >> 3;
       
   384 
       
   385         QRegion staticRegion(staticContents());
       
   386         // Make sure we're inside the boundaries of the old image.
       
   387         staticRegion &= QRect(0, 0, oldImage->image.width(), oldImage->image.height());
       
   388         const QVector<QRect> &rects = staticRegion.rects();
       
   389         const QRect *srcRect = rects.constData();
       
   390 
       
   391         // Copy the static content of the old image into the new one.
       
   392         int numRectsLeft = rects.size();
       
   393         do {
       
   394             const int bytesOffset = srcRect->x() * bytesPerPixel;
       
   395             const int dy = srcRect->y();
       
   396 
       
   397             // Adjust src and dst to point to the right offset.
       
   398             const uchar *s = src + dy * srcBytesPerLine + bytesOffset;
       
   399             uchar *d = dst + dy * dstBytesPerLine + bytesOffset;
       
   400             const int numBytes = srcRect->width() * bytesPerPixel;
       
   401 
       
   402             int numScanLinesLeft = srcRect->height();
       
   403             do {
       
   404                 ::memcpy(d, s, numBytes);
       
   405                 d += dstBytesPerLine;
       
   406                 s += srcBytesPerLine;
       
   407             } while (--numScanLinesLeft);
       
   408 
       
   409             ++srcRect;
       
   410         } while (--numRectsLeft);
       
   411     }
       
   412 
       
   413     delete oldImage;
       
   414 }
       
   415 
       
   416 QT_END_NAMESPACE