Make QRegion not need to be friends with QVector
[profile/ivi/qtbase.git] / src / gui / painting / qbackingstore.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qbackingstore.h>
43 #include <qwindow.h>
44 #include <qpixmap.h>
45 #include <qpa/qplatformbackingstore.h>
46 #include <qpa/qplatformintegration.h>
47 #include <qscreen.h>
48
49 #include <private/qguiapplication_p.h>
50 #include <private/qwindow_p.h>
51
52 QT_BEGIN_NAMESPACE
53
54 class QBackingStorePrivate
55 {
56 public:
57     QBackingStorePrivate(QWindow *w)
58         : window(w)
59     {
60     }
61
62     QWindow *window;
63     QPlatformBackingStore *platformBackingStore;
64     QRegion staticContents;
65     QSize size;
66 };
67
68 /*!
69     \class QBackingStore
70     \since 5.0
71
72     \brief The QBackingStore class provides the drawing area for top-level windows.
73 */
74
75 /*!
76     Flushes the given \a region from the specified \a window onto the
77     screen.
78
79     Note that the \a offset parameter is currently unused.
80 */
81 void QBackingStore::flush(const QRegion &region, QWindow *win, const QPoint &offset)
82 {
83     if (!win)
84         win = window();
85
86     if (win && !qt_window_private(win)->receivedExpose)
87         qWarning("QBackingStore::flush() called with non-exposed window, behavior is undefined");
88
89     d_ptr->platformBackingStore->flush(win, region, offset);
90 }
91
92 /*!
93     \fn QPaintDevice* QBackingStore::paintDevice()
94
95     Implement this function to return the appropriate paint device.
96 */
97 QPaintDevice *QBackingStore::paintDevice()
98 {
99     return d_ptr->platformBackingStore->paintDevice();
100 }
101
102 /*!
103     Constructs an empty surface for the given top-level \a window.
104 */
105 QBackingStore::QBackingStore(QWindow *window)
106     : d_ptr(new QBackingStorePrivate(window))
107 {
108     d_ptr->platformBackingStore = QGuiApplicationPrivate::platformIntegration()->createPlatformBackingStore(window);
109 }
110
111 /*!
112     Destroys this surface.
113 */
114 QBackingStore::~QBackingStore()
115 {
116     delete d_ptr->platformBackingStore;
117 }
118
119 /*!
120     Returns a pointer to the top-level window associated with this
121     surface.
122 */
123 QWindow* QBackingStore::window() const
124 {
125     return d_ptr->window;
126 }
127
128 /*!
129     This function is called before painting onto the surface begins,
130     with the \a region in which the painting will occur.
131
132     \sa endPaint(), paintDevice()
133 */
134
135 void QBackingStore::beginPaint(const QRegion &region)
136 {
137     d_ptr->platformBackingStore->beginPaint(region);
138 }
139
140 /*!
141     This function is called after painting onto the surface has ended.
142
143     \sa beginPaint(), paintDevice()
144 */
145 void QBackingStore::endPaint()
146 {
147     d_ptr->platformBackingStore->endPaint();
148 }
149
150 /*!
151       Sets the size of the windowsurface to be \a size.
152
153       \sa size()
154 */
155 void QBackingStore::resize(const QSize &size)
156 {
157     d_ptr->size = size;
158     d_ptr->platformBackingStore->resize(size, d_ptr->staticContents);
159 }
160
161 /*!
162     Returns the current size of the windowsurface.
163 */
164 QSize QBackingStore::size() const
165 {
166     return d_ptr->size;
167 }
168
169 /*!
170     Scrolls the given \a area \a dx pixels to the right and \a dy
171     downward; both \a dx and \a dy may be negative.
172
173     Returns true if the area was scrolled successfully; false otherwise.
174 */
175 bool QBackingStore::scroll(const QRegion &area, int dx, int dy)
176 {
177     Q_UNUSED(area);
178     Q_UNUSED(dx);
179     Q_UNUSED(dy);
180
181     return d_ptr->platformBackingStore->scroll(area, dx, dy);
182 }
183
184 void QBackingStore::setStaticContents(const QRegion &region)
185 {
186     d_ptr->staticContents = region;
187 }
188
189 QRegion QBackingStore::staticContents() const
190 {
191     return d_ptr->staticContents;
192 }
193
194 bool QBackingStore::hasStaticContents() const
195 {
196     return !d_ptr->staticContents.isEmpty();
197 }
198
199 void Q_GUI_EXPORT qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset)
200 {
201     // make sure we don't detach
202     uchar *mem = const_cast<uchar*>(const_cast<const QImage &>(img).bits());
203
204     int lineskip = img.bytesPerLine();
205     int depth = img.depth() >> 3;
206
207     const QRect imageRect(0, 0, img.width(), img.height());
208     const QRect r = rect & imageRect & imageRect.translated(-offset);
209     const QPoint p = rect.topLeft() + offset;
210
211     if (r.isEmpty())
212         return;
213
214     const uchar *src;
215     uchar *dest;
216
217     if (r.top() < p.y()) {
218         src = mem + r.bottom() * lineskip + r.left() * depth;
219         dest = mem + (p.y() + r.height() - 1) * lineskip + p.x() * depth;
220         lineskip = -lineskip;
221     } else {
222         src = mem + r.top() * lineskip + r.left() * depth;
223         dest = mem + p.y() * lineskip + p.x() * depth;
224     }
225
226     const int w = r.width();
227     int h = r.height();
228     const int bytes = w * depth;
229
230     // overlapping segments?
231     if (offset.y() == 0 && qAbs(offset.x()) < w) {
232         do {
233             ::memmove(dest, src, bytes);
234             dest += lineskip;
235             src += lineskip;
236         } while (--h);
237     } else {
238         do {
239             ::memcpy(dest, src, bytes);
240             dest += lineskip;
241             src += lineskip;
242         } while (--h);
243     }
244 }
245
246 QPlatformBackingStore *QBackingStore::handle() const
247 {
248     return d_ptr->platformBackingStore;
249 }
250
251 QT_END_NAMESPACE