Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / src / gui / painting / qwindowsurface_s60.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 <qglobal.h> // for Q_WS_WIN define (non-PCH)
43
44 #include <QtGui/qpaintdevice.h>
45 #include <private/qwidget_p.h>
46 #include <private/qwindowsurface_s60_p.h>
47 #include <private/qpixmap_s60_p.h>
48 #include <private/qt_s60_p.h>
49 #include <private/qapplication_p.h>
50 #include <private/qdrawhelper_p.h>
51
52 #ifdef QT_GRAPHICSSYSTEM_RUNTIME
53 #include <private/qgraphicssystem_runtime_p.h>
54 #endif
55
56 QT_BEGIN_NAMESPACE
57
58 struct QS60WindowSurfacePrivate
59 {
60     QPixmap device;
61     QList<QImage*> bufferImages;
62 };
63
64 TDisplayMode displayMode(bool opaque)
65 {
66     TDisplayMode mode = S60->screenDevice()->DisplayMode();
67     if (opaque) {
68         mode = EColor16MU;
69     } else  {
70         if (QSysInfo::symbianVersion() >= QSysInfo::SV_SF_3)
71             mode = Q_SYMBIAN_ECOLOR16MAP; // Symbian^3 WServ has support for ARGB32_PRE
72         else
73             mode = EColor16MA; // Symbian prior to Symbian^3 sw accelerates EColor16MA
74     }
75     return mode;
76 }
77
78 bool blitWriteAlpha(QWidgetPrivate *widgetPrivate)
79 {
80     QWExtra *extra = widgetPrivate->extraData();
81     return extra ? extra->nativePaintMode == QWExtra::BlitWriteAlpha : false;
82 }
83
84 QS60WindowSurface::QS60WindowSurface(QWidget* widget)
85     : QWindowSurface(widget), d_ptr(new QS60WindowSurfacePrivate)
86 {
87     QWidgetPrivate *widgetPrivate = qt_widget_private(widget);
88     const bool opaque = widgetPrivate->isOpaque && !blitWriteAlpha(widgetPrivate);
89     TDisplayMode mode = displayMode(opaque);
90     // We create empty CFbsBitmap here -> it will be resized in setGeometry
91     CFbsBitmap *bitmap = new CFbsBitmap;        // CBase derived object needs check on new
92     Q_CHECK_PTR(bitmap);
93     qt_symbian_throwIfError( bitmap->Create( TSize(0, 0), mode ) );
94
95     QS60PixmapData *data = new QS60PixmapData(QPixmapData::PixmapType);
96     if (data) {
97         data->fromSymbianBitmap(bitmap, true);
98         d_ptr->device = QPixmap(data);
99     }
100 }
101
102 QS60WindowSurface::~QS60WindowSurface()
103 {
104 #if defined(QT_GRAPHICSSYSTEM_RUNTIME) && defined(Q_SYMBIAN_SUPPORTS_SURFACES)
105     if(QApplicationPrivate::runtime_graphics_system) {
106         QRuntimeGraphicsSystem *runtimeGraphicsSystem =
107                 static_cast<QRuntimeGraphicsSystem*>(QApplicationPrivate::graphics_system);
108         if(runtimeGraphicsSystem->graphicsSystemName() == QLatin1String("openvg")) {
109
110             // Graphics system has been switched from raster to openvg.
111             // Issue empty redraw to clear the UI surface
112
113             QWidget *w = window();
114             if (w->testAttribute(Qt::WA_WState_Created)) {
115                 RWindow *const window = static_cast<RWindow *>(w->winId()->DrawableWindow());
116                 window->BeginRedraw();
117                 window->EndRedraw();
118             }
119         }
120     }
121 #endif
122
123     delete d_ptr;
124 }
125
126 void QS60WindowSurface::beginPaint(const QRegion &rgn)
127 {
128 #ifdef Q_SYMBIAN_SUPPORTS_SURFACES
129     S60->wsSession().Finish();
130 #endif
131
132     QWidgetPrivate *windowPrivate = qt_widget_private(window());
133     if (!windowPrivate->isOpaque || blitWriteAlpha(windowPrivate)) {
134         QS60PixmapData *pixmapData = static_cast<QS60PixmapData *>(d_ptr->device.data_ptr().data());
135
136         TDisplayMode mode = displayMode(false);
137         if (pixmapData->cfbsBitmap->DisplayMode() != mode)
138             pixmapData->convertToDisplayMode(mode);
139
140         pixmapData->beginDataAccess();
141
142         if (!windowPrivate->isOpaque) {
143             QPainter p(&pixmapData->image);
144             p.setCompositionMode(QPainter::CompositionMode_Source);
145             const QVector<QRect> rects = rgn.rects();
146             const QColor blank = Qt::transparent;
147             for (QVector<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it) {
148                 p.fillRect(*it, blank);
149             }
150         }
151
152         pixmapData->endDataAccess();
153     }
154 }
155
156 void QS60WindowSurface::endPaint(const QRegion &)
157 {
158     qDeleteAll(d_ptr->bufferImages);
159     d_ptr->bufferImages.clear();
160 }
161
162 QImage* QS60WindowSurface::buffer(const QWidget *widget)
163 {
164     if (widget->window() != window())
165         return 0;
166
167     QPaintDevice *pdev = paintDevice();
168     if (!pdev)
169         return 0;
170
171     const QPoint off = offset(widget);
172     QImage *img = &(static_cast<QS60PixmapData *>(d_ptr->device.data_ptr().data())->image);
173
174     QRect rect(off, widget->size());
175     rect &= QRect(QPoint(), img->size());
176
177     if (rect.isEmpty())
178         return 0;
179
180     img = new QImage(img->scanLine(rect.y()) + rect.x() * img->depth() / 8,
181                      rect.width(), rect.height(),
182                      img->bytesPerLine(), img->format());
183     d_ptr->bufferImages.append(img);
184
185     return img;
186 }
187
188 void QS60WindowSurface::flush(QWidget *widget, const QRegion &region, const QPoint &)
189 {
190     QWidget *window = widget->window();
191     Q_ASSERT(window);
192     QTLWExtra *topExtra = window->d_func()->maybeTopData();
193     Q_ASSERT(topExtra);
194     QRect qr = region.boundingRect();
195     if (!topExtra->inExpose) {
196         topExtra->inExpose = true; // Prevent DrawNow() from calling syncBackingStore() again
197         TRect tr = qt_QRect2TRect(qr);
198         widget->winId()->DrawNow(tr);
199         topExtra->inExpose = false;
200     } else {
201         // This handles the case when syncBackingStore updates content outside of the
202         // original drawing rectangle. This might happen if there are pending update()
203         // events at the same time as we get a Draw() from Symbian.
204         QRect drawRect = qt_TRect2QRect(widget->winId()->DrawableWindow()->GetDrawRect());
205         if (!drawRect.contains(qr))
206             widget->winId()->DrawDeferred();
207     }
208 }
209
210 bool QS60WindowSurface::scroll(const QRegion &area, int dx, int dy)
211 {
212     QRect rect = area.boundingRect();
213
214     if (dx == 0 && dy == 0)
215         return false;
216
217     if (d_ptr->device.isNull())
218         return false;
219
220     QS60PixmapData *data = static_cast<QS60PixmapData*>(d_ptr->device.data_ptr().data());
221     data->scroll(dx, dy, rect);
222
223     return true;
224 }
225
226 QPaintDevice* QS60WindowSurface::paintDevice()
227 {
228     return &d_ptr->device;
229 }
230
231 void QS60WindowSurface::setGeometry(const QRect& rect)
232 {
233     if (rect == geometry())
234         return;
235
236     QS60PixmapData *data = static_cast<QS60PixmapData*>(d_ptr->device.data_ptr().data());
237     data->resize(rect.width(), rect.height());
238
239     QWindowSurface::setGeometry(rect);
240 }
241
242 QWindowSurface::WindowSurfaceFeatures QS60WindowSurface::features() const
243 {
244     return QWindowSurface::AllFeatures;
245 }
246
247 CFbsBitmap* QS60WindowSurface::symbianBitmap() const
248 {
249     QS60PixmapData *data = static_cast<QS60PixmapData*>(d_ptr->device.data_ptr().data());
250     return data->cfbsBitmap;
251 }
252
253 QT_END_NAMESPACE
254