1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <qglobal.h> // for Q_WS_WIN define (non-PCH)
47 #include <qt_windows.h>
50 #include <QtGui/qpaintdevice.h>
51 #include <QtGui/qwidget.h>
53 #include "private/qwindowsurface_raster_p.h"
54 #include "private/qnativeimage_p.h"
55 #include "private/qwidget_p.h"
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"
63 #include "private/qdrawhelper_p.h"
66 #include <private/qt_cocoa_helpers_mac_p.h>
67 #include <QMainWindow>
68 #include <private/qmainwindowlayout_p.h>
74 class QRasterWindowSurfacePrivate
85 uint translucentBackground : 1;
88 uint inSetGeometry : 1;
91 QRasterWindowSurface::QRasterWindowSurface(QWidget *window, bool setDefaultSurface)
92 : QWindowSurface(window, setDefaultSurface), d_ptr(new QRasterWindowSurfacePrivate)
95 d_ptr->gc = XCreateGC(X11->display, window->handle(), 0, 0);
97 d_ptr->translucentBackground = X11->use_xrender
98 && window->x11Info().depth() == 32;
101 d_ptr->needsSync = false;
105 d_ptr->inSetGeometry = false;
107 #ifdef QT_MAC_USE_COCOA
109 regionToFlush = QRegion();
110 #endif // QT_MAC_USE_COCOA
114 QRasterWindowSurface::~QRasterWindowSurface()
117 XFreeGC(X11->display, d_ptr->gc);
124 QPaintDevice *QRasterWindowSurface::paintDevice()
126 return &d_ptr->image->image;
129 #if defined(Q_WS_X11) && !defined(QT_NO_MITSHM)
130 void QRasterWindowSurface::syncX()
132 // delay writing to the backbuffer until we know for sure X is done reading from it
133 if (d_ptr->needsSync) {
134 XSync(X11->display, false);
135 d_ptr->needsSync = false;
140 void QRasterWindowSurface::beginPaint(const QRegion &rgn)
142 #if defined(Q_WS_X11) && !defined(QT_NO_MITSHM)
146 #if (defined(Q_WS_X11) && !defined(QT_NO_XRENDER)) || (defined(Q_WS_WIN) && !defined(Q_WS_WINCE))
147 if (!qt_widget_private(window())->isOpaque && window()->testAttribute(Qt::WA_TranslucentBackground)) {
148 #if defined(Q_WS_WIN) && !defined(Q_WS_WINCE)
149 if (d_ptr->image->image.format() != QImage::Format_ARGB32_Premultiplied)
150 prepareBuffer(QImage::Format_ARGB32_Premultiplied, window());
152 QPainter p(&d_ptr->image->image);
153 p.setCompositionMode(QPainter::CompositionMode_Source);
154 const QVector<QRect> rects = rgn.rects();
155 const QColor blank = Qt::transparent;
156 for (QVector<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it) {
157 p.fillRect(*it, blank);
165 void QRasterWindowSurface::flush(QWidget *widget, const QRegion &rgn, const QPoint &offset)
167 Q_D(QRasterWindowSurface);
169 // Not ready for painting yet, bail out. This can happen in
170 // QWidget::create_sys()
171 if (!d->image || rgn.rectCount() == 0)
175 QRect br = rgn.boundingRect();
178 if (!qt_widget_private(window())->isOpaque
179 && window()->testAttribute(Qt::WA_TranslucentBackground)
180 && (qt_widget_private(window())->data.window_flags & Qt::FramelessWindowHint))
182 QRect r = window()->frameGeometry();
183 QPoint frameOffset = qt_widget_private(window())->frameStrut().topLeft();
184 QRect dirtyRect = br.translated(offset + frameOffset);
186 SIZE size = {r.width(), r.height()};
187 POINT ptDst = {r.x(), r.y()};
188 POINT ptSrc = {0, 0};
189 BLENDFUNCTION blend = {AC_SRC_OVER, 0, (int)(255.0 * window()->windowOpacity()), Q_AC_SRC_ALPHA};
190 RECT dirty = {dirtyRect.x(), dirtyRect.y(),
191 dirtyRect.x() + dirtyRect.width(), dirtyRect.y() + dirtyRect.height()};
192 Q_UPDATELAYEREDWINDOWINFO info = {sizeof(info), NULL, &ptDst, &size, d->image->hdc, &ptSrc, 0, &blend, Q_ULW_ALPHA, &dirty};
193 ptrUpdateLayeredWindowIndirect(window()->internalWinId(), &info);
197 QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft();
199 HDC widget_dc = widget->getDC();
201 QRect wbr = br.translated(-wOffset);
202 BitBlt(widget_dc, wbr.x(), wbr.y(), wbr.width(), wbr.height(),
203 d->image->hdc, br.x() + offset.x(), br.y() + offset.y(), SRCCOPY);
204 widget->releaseDC(widget_dc);
208 static bool flush = !qgetenv("QT_FLUSH_WINDOWSURFACE").isEmpty();
210 SelectObject(qt_win_display_dc(), GetStockObject(BLACK_BRUSH));
211 Rectangle(qt_win_display_dc(), 0, 0, d->image->width() + 2, d->image->height() + 2);
212 BitBlt(qt_win_display_dc(), 1, 1, d->image->width(), d->image->height(),
213 d->image->hdc, 0, 0, SRCCOPY);
220 extern void *qt_getClipRects(const QRegion &r, int &num); // in qpaintengine_x11.cpp
221 extern QWidgetData* qt_widget_data(QWidget *);
222 QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft();
224 if (widget->window() != window()) {
225 XFreeGC(X11->display, d_ptr->gc);
226 d_ptr->gc = XCreateGC(X11->display, widget->handle(), 0, 0);
230 if (!wOffset.isNull())
231 wrgn.translate(-wOffset);
232 QRect wbr = wrgn.boundingRect();
234 if (wrgn.rectCount() != 1) {
236 XRectangle *rects = (XRectangle *)qt_getClipRects(wrgn, num);
237 XSetClipRectangles(X11->display, d_ptr->gc, 0, 0, rects, num, YXBanded);
240 QRect br = rgn.boundingRect().translated(offset);
242 if (d_ptr->image->xshmpm) {
243 XCopyArea(X11->display, d_ptr->image->xshmpm, widget->handle(), d_ptr->gc,
244 br.x(), br.y(), br.width(), br.height(), wbr.x(), wbr.y());
245 d_ptr->needsSync = true;
246 } else if (d_ptr->image->xshmimg) {
247 const QImage &src = d->image->image;
248 br = br.intersected(src.rect());
249 XShmPutImage(X11->display, widget->handle(), d_ptr->gc, d_ptr->image->xshmimg,
250 br.x(), br.y(), wbr.x(), wbr.y(), br.width(), br.height(), False);
251 d_ptr->needsSync = true;
255 const QImage &src = d->image->image;
256 br = br.intersected(src.rect());
257 if (src.format() != QImage::Format_RGB32 || widget->x11Info().depth() < 24) {
258 Q_ASSERT(src.depth() >= 16);
259 const QImage sub_src(src.scanLine(br.y()) + br.x() * (uint(src.depth()) / 8),
260 br.width(), br.height(), src.bytesPerLine(), src.format());
261 QX11PixmapData *data = new QX11PixmapData(QPixmapData::PixmapType);
262 data->xinfo = widget->x11Info();
263 data->fromImage(sub_src, Qt::NoOpaqueDetection);
264 QPixmap pm = QPixmap(data);
265 XCopyArea(X11->display, pm.handle(), widget->handle(), d_ptr->gc, 0 , 0 , br.width(), br.height(), wbr.x(), wbr.y());
267 // qpaintengine_x11.cpp
268 extern void qt_x11_drawImage(const QRect &rect, const QPoint &pos, const QImage &image, Drawable hd, GC gc, Display *dpy, Visual *visual, int depth);
269 qt_x11_drawImage(br, wbr.topLeft(), src, widget->handle(), d_ptr->gc, X11->display, (Visual *)widget->x11Info().visual(), widget->x11Info().depth());
273 if (wrgn.rectCount() != 1)
274 XSetClipMask(X11->display, d_ptr->gc, XNone);
281 // This is mainly done for native components like native "open file" dialog.
282 if (widget->testAttribute(Qt::WA_DontShowOnScreen)) {
286 #ifdef QT_MAC_USE_COCOA
288 this->needsFlush = true;
289 this->regionToFlush += rgn;
291 // The actual flushing will be processed in [view drawRect:rect]
292 qt_mac_setNeedsDisplay(widget);
295 // Get a context for the widget.
296 CGContextRef context;
297 CGrafPtr port = GetWindowPort(qt_mac_window_for(widget));
298 QDBeginCGContext(port, &context);
299 CGContextRetain(context);
300 CGContextSaveGState(context);
303 CGContextTranslateCTM(context, 0, widget->height());
304 CGContextScaleCTM(context, 1, -1);
307 const QVector<QRect> &rects = rgn.rects();
308 for (int i = 0; i < rects.size(); ++i) {
309 const QRect &rect = rects.at(i);
310 CGContextAddRect(context, CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()));
312 CGContextClip(context);
314 QRect r = rgn.boundingRect();
315 const CGRect area = CGRectMake(r.x(), r.y(), r.width(), r.height());
316 CGImageRef image = CGBitmapContextCreateImage(d->image->cg);
317 CGImageRef subImage = CGImageCreateWithImageInRect(image, area);
319 qt_mac_drawCGImage(context, &area, subImage);
321 CGImageRelease(subImage);
322 CGImageRelease(image);
324 QDEndCGContext(port, &context);
327 CGContextRestoreGState(context);
328 CGContextRelease(context);
329 #endif // QT_MAC_USE_COCOA
340 void QRasterWindowSurface::setGeometry(const QRect &rect)
342 QWindowSurface::setGeometry(rect);
343 Q_D(QRasterWindowSurface);
344 d->inSetGeometry = true;
345 if (d->image == 0 || d->image->width() < rect.width() || d->image->height() < rect.height()) {
346 #if (defined(Q_WS_X11) && !defined(QT_NO_XRENDER)) || (defined(Q_WS_WIN) && !defined(Q_WS_WINCE))
348 if (d_ptr->translucentBackground)
350 if (!qt_widget_private(window())->isOpaque)
352 prepareBuffer(QImage::Format_ARGB32_Premultiplied, window());
355 prepareBuffer(QNativeImage::systemFormat(), window());
357 d->inSetGeometry = false;
359 #if defined(Q_WS_MAC) && defined(QT_MAC_USE_COCOA)
360 QMainWindow* mWindow = qobject_cast<QMainWindow*>(window());
362 QMainWindowLayout *mLayout = qobject_cast<QMainWindowLayout*>(mWindow->layout());
363 QList<QToolBar *> toolbarList = mLayout->qtoolbarsInUnifiedToolbarList;
365 for (int i = 0; i < toolbarList.size(); ++i) {
366 QToolBar* toolbar = toolbarList.at(i);
367 if (mLayout->toolBarArea(toolbar) == Qt::TopToolBarArea) {
368 QWidget* tbWidget = (QWidget*) toolbar;
369 if (tbWidget->d_func()->unifiedSurface) {
370 tbWidget->d_func()->unifiedSurface->setGeometry(rect);
375 #endif // Q_WS_MAC && QT_MAC_USE_COCOA
379 // from qwindowsurface.cpp
380 extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);
382 bool QRasterWindowSurface::scroll(const QRegion &area, int dx, int dy)
385 Q_D(QRasterWindowSurface);
387 if (!d->image || !d->image->hdc)
390 QRect rect = area.boundingRect();
391 BitBlt(d->image->hdc, rect.x()+dx, rect.y()+dy, rect.width(), rect.height(),
392 d->image->hdc, rect.x(), rect.y(), SRCCOPY);
396 Q_D(QRasterWindowSurface);
398 if (!d->image || d->image->image.isNull())
401 #if defined(Q_WS_X11) && !defined(QT_NO_MITSHM)
405 const QVector<QRect> rects = area.rects();
406 for (int i = 0; i < rects.size(); ++i)
407 qt_scrollRectInImage(d->image->image, rects.at(i), QPoint(dx, dy));
413 QWindowSurface::WindowSurfaceFeatures QRasterWindowSurface::features() const
415 return QWindowSurface::AllFeatures;
418 void QRasterWindowSurface::prepareBuffer(QImage::Format format, QWidget *widget)
420 Q_D(QRasterWindowSurface);
422 int width = window()->width();
423 int height = window()->height();
425 width = qMax(d->image->width(), width);
426 height = qMax(d->image->height(), height);
429 if (width == 0 || height == 0) {
435 QNativeImage *oldImage = d->image;
437 d->image = new QNativeImage(width, height, format, false, widget);
439 if (oldImage && d->inSetGeometry && hasStaticContents()) {
440 // Make sure we use the const version of bits() (no detach).
441 const uchar *src = const_cast<const QImage &>(oldImage->image).bits();
442 uchar *dst = d->image->image.bits();
444 const int srcBytesPerLine = oldImage->image.bytesPerLine();
445 const int dstBytesPerLine = d->image->image.bytesPerLine();
446 const int bytesPerPixel = oldImage->image.depth() >> 3;
448 QRegion staticRegion(staticContents());
449 // Make sure we're inside the boundaries of the old image.
450 staticRegion &= QRect(0, 0, oldImage->image.width(), oldImage->image.height());
451 const QVector<QRect> &rects = staticRegion.rects();
452 const QRect *srcRect = rects.constData();
454 // Copy the static content of the old image into the new one.
455 int numRectsLeft = rects.size();
457 const int bytesOffset = srcRect->x() * bytesPerPixel;
458 const int dy = srcRect->y();
460 // Adjust src and dst to point to the right offset.
461 const uchar *s = src + dy * srcBytesPerLine + bytesOffset;
462 uchar *d = dst + dy * dstBytesPerLine + bytesOffset;
463 const int numBytes = srcRect->width() * bytesPerPixel;
465 int numScanLinesLeft = srcRect->height();
467 ::memcpy(d, s, numBytes);
468 d += dstBytesPerLine;
469 s += srcBytesPerLine;
470 } while (--numScanLinesLeft);
473 } while (--numRectsLeft);
479 #ifdef QT_MAC_USE_COCOA
480 CGContextRef QRasterWindowSurface::imageContext()
482 Q_D(QRasterWindowSurface);
485 #endif // QT_MAC_USE_COCOA