Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / src / gui / painting / qwindowsurface_raster.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 ** 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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
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 #include <QMainWindow>
68 #include <private/qmainwindowlayout_p.h>
69 #include <QToolBar>
70 #endif
71
72 QT_BEGIN_NAMESPACE
73
74 class QRasterWindowSurfacePrivate
75 {
76 public:
77     QNativeImage *image;
78
79 #ifdef Q_WS_X11
80     GC gc;
81 #ifndef QT_NO_MITSHM
82     uint needsSync : 1;
83 #endif
84 #ifndef QT_NO_XRENDER
85     uint translucentBackground : 1;
86 #endif
87 #endif
88     uint inSetGeometry : 1;
89 };
90
91 QRasterWindowSurface::QRasterWindowSurface(QWidget *window, bool setDefaultSurface)
92     : QWindowSurface(window, setDefaultSurface), d_ptr(new QRasterWindowSurfacePrivate)
93 {
94 #ifdef Q_WS_X11
95     d_ptr->gc = XCreateGC(X11->display, window->handle(), 0, 0);
96 #ifndef QT_NO_XRENDER
97     d_ptr->translucentBackground = X11->use_xrender
98         && window->x11Info().depth() == 32;
99 #endif
100 #ifndef QT_NO_MITHSM
101     d_ptr->needsSync = false;
102 #endif
103 #endif
104     d_ptr->image = 0;
105     d_ptr->inSetGeometry = false;
106
107 #ifdef QT_MAC_USE_COCOA
108     needsFlush = false;
109     regionToFlush = QRegion();
110 #endif // QT_MAC_USE_COCOA
111 }
112
113
114 QRasterWindowSurface::~QRasterWindowSurface()
115 {
116 #ifdef Q_WS_X11
117     XFreeGC(X11->display, d_ptr->gc);
118 #endif
119     if (d_ptr->image)
120         delete d_ptr->image;
121 }
122
123
124 QPaintDevice *QRasterWindowSurface::paintDevice()
125 {
126     return &d_ptr->image->image;
127 }
128
129 #if defined(Q_WS_X11) && !defined(QT_NO_MITSHM)
130 void QRasterWindowSurface::syncX()
131 {
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;
136     }
137 }
138 #endif
139
140 void QRasterWindowSurface::beginPaint(const QRegion &rgn)
141 {
142 #if defined(Q_WS_X11) && !defined(QT_NO_MITSHM)
143     syncX();
144 #endif
145
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());
151 #endif
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);
158         }
159     }
160 #else
161     Q_UNUSED(rgn);
162 #endif
163 }
164
165 void QRasterWindowSurface::flush(QWidget *widget, const QRegion &rgn, const QPoint &offset)
166 {
167     Q_D(QRasterWindowSurface);
168
169     // Not ready for painting yet, bail out. This can happen in
170     // QWidget::create_sys()
171     if (!d->image || rgn.rectCount() == 0)
172         return;
173
174 #ifdef Q_WS_WIN
175     QRect br = rgn.boundingRect();
176
177 #ifndef Q_WS_WINCE
178     if (!qt_widget_private(window())->isOpaque
179         && window()->testAttribute(Qt::WA_TranslucentBackground)
180         && (qt_widget_private(window())->data.window_flags & Qt::FramelessWindowHint))
181     {
182         QRect r = window()->frameGeometry();
183         QPoint frameOffset = qt_widget_private(window())->frameStrut().topLeft();
184         QRect dirtyRect = br.translated(offset + frameOffset);
185
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);
194     } else
195 #endif
196     {
197         QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft();
198
199         HDC widget_dc = widget->getDC();
200
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);
205     }
206
207 #ifndef QT_NO_DEBUG
208     static bool flush = !qgetenv("QT_FLUSH_WINDOWSURFACE").isEmpty();
209     if (flush) {
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);
214     }
215 #endif
216
217 #endif
218
219 #ifdef Q_WS_X11
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();
223
224     if (widget->window() != window()) {
225         XFreeGC(X11->display, d_ptr->gc);
226         d_ptr->gc = XCreateGC(X11->display, widget->handle(), 0, 0);
227     }
228
229     QRegion wrgn(rgn);
230     if (!wOffset.isNull())
231         wrgn.translate(-wOffset);
232     QRect wbr = wrgn.boundingRect();
233
234     if (wrgn.rectCount() != 1) {
235         int num;
236         XRectangle *rects = (XRectangle *)qt_getClipRects(wrgn, num);
237         XSetClipRectangles(X11->display, d_ptr->gc, 0, 0, rects, num, YXBanded);
238     }
239
240     QRect br = rgn.boundingRect().translated(offset);
241 #ifndef QT_NO_MITSHM
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;
252     } else
253 #endif
254     {
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());
266         } else {
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());
270         }
271     }
272
273     if (wrgn.rectCount() != 1)
274         XSetClipMask(X11->display, d_ptr->gc, XNone);
275 #endif // FALCON
276
277 #ifdef Q_WS_MAC
278
279     Q_UNUSED(offset);
280
281     // This is mainly done for native components like native "open file" dialog.
282     if (widget->testAttribute(Qt::WA_DontShowOnScreen)) {
283         return;
284     }
285
286 #ifdef QT_MAC_USE_COCOA
287
288     this->needsFlush = true;
289     this->regionToFlush += rgn;
290
291     // The actual flushing will be processed in [view drawRect:rect]
292     qt_mac_setNeedsDisplay(widget);
293
294 #else
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);
301
302     // Flip context.
303     CGContextTranslateCTM(context, 0, widget->height());
304     CGContextScaleCTM(context, 1, -1);
305
306     // Clip to region.
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()));
311     }
312     CGContextClip(context);
313
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);
318
319     qt_mac_drawCGImage(context, &area, subImage);
320
321     CGImageRelease(subImage);
322     CGImageRelease(image);
323
324     QDEndCGContext(port, &context);
325
326     // Restore context.
327     CGContextRestoreGState(context);
328     CGContextRelease(context);
329 #endif // QT_MAC_USE_COCOA
330
331 #endif // Q_WS_MAC
332
333 #ifdef Q_OS_SYMBIAN
334     Q_UNUSED(widget);
335     Q_UNUSED(rgn);
336     Q_UNUSED(offset);
337 #endif
338 }
339
340 void QRasterWindowSurface::setGeometry(const QRect &rect)
341 {
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))
347 #ifndef Q_WS_WIN
348         if (d_ptr->translucentBackground)
349 #else
350         if (!qt_widget_private(window())->isOpaque)
351 #endif
352             prepareBuffer(QImage::Format_ARGB32_Premultiplied, window());
353         else
354 #endif
355             prepareBuffer(QNativeImage::systemFormat(), window());
356     }
357     d->inSetGeometry = false;
358
359 #if defined(Q_WS_MAC) && defined(QT_MAC_USE_COCOA)
360     QMainWindow* mWindow = qobject_cast<QMainWindow*>(window());
361     if (mWindow) {
362         QMainWindowLayout *mLayout = qobject_cast<QMainWindowLayout*>(mWindow->layout());
363         QList<QToolBar *> toolbarList = mLayout->qtoolbarsInUnifiedToolbarList;
364
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);
371                 }
372             }
373         }
374     }
375 #endif // Q_WS_MAC && QT_MAC_USE_COCOA
376
377 }
378
379 // from qwindowsurface.cpp
380 extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);
381
382 bool QRasterWindowSurface::scroll(const QRegion &area, int dx, int dy)
383 {
384 #ifdef Q_WS_WIN
385     Q_D(QRasterWindowSurface);
386
387     if (!d->image || !d->image->hdc)
388         return false;
389
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);
393
394     return true;
395 #else
396     Q_D(QRasterWindowSurface);
397
398     if (!d->image || d->image->image.isNull())
399         return false;
400
401 #if defined(Q_WS_X11) && !defined(QT_NO_MITSHM)
402     syncX();
403 #endif
404
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));
408
409     return true;
410 #endif
411 }
412
413 QWindowSurface::WindowSurfaceFeatures QRasterWindowSurface::features() const
414 {
415     return QWindowSurface::AllFeatures;
416 }
417
418 void QRasterWindowSurface::prepareBuffer(QImage::Format format, QWidget *widget)
419 {
420     Q_D(QRasterWindowSurface);
421
422     int width = window()->width();
423     int height = window()->height();
424     if (d->image) {
425         width = qMax(d->image->width(), width);
426         height = qMax(d->image->height(), height);
427     }
428
429     if (width == 0 || height == 0) {
430         delete d->image;
431         d->image = 0;
432         return;
433     }
434
435     QNativeImage *oldImage = d->image;
436
437     d->image = new QNativeImage(width, height, format, false, widget);
438
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();
443
444         const int srcBytesPerLine = oldImage->image.bytesPerLine();
445         const int dstBytesPerLine = d->image->image.bytesPerLine();
446         const int bytesPerPixel = oldImage->image.depth() >> 3;
447
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();
453
454         // Copy the static content of the old image into the new one.
455         int numRectsLeft = rects.size();
456         do {
457             const int bytesOffset = srcRect->x() * bytesPerPixel;
458             const int dy = srcRect->y();
459
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;
464
465             int numScanLinesLeft = srcRect->height();
466             do {
467                 ::memcpy(d, s, numBytes);
468                 d += dstBytesPerLine;
469                 s += srcBytesPerLine;
470             } while (--numScanLinesLeft);
471
472             ++srcRect;
473         } while (--numRectsLeft);
474     }
475
476     delete oldImage;
477 }
478
479 #ifdef QT_MAC_USE_COCOA
480 CGContextRef QRasterWindowSurface::imageContext()
481 {
482     Q_D(QRasterWindowSurface);
483     return d->image->cg;
484 }
485 #endif // QT_MAC_USE_COCOA
486
487 QT_END_NAMESPACE