Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/painting/qbackingstore.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include <qbackingstore.h> | - | ||||||||||||
35 | #include <qwindow.h> | - | ||||||||||||
36 | #include <qpixmap.h> | - | ||||||||||||
37 | #include <qpa/qplatformbackingstore.h> | - | ||||||||||||
38 | #include <qpa/qplatformintegration.h> | - | ||||||||||||
39 | #include <qscreen.h> | - | ||||||||||||
40 | #include <qdebug.h> | - | ||||||||||||
41 | #include <qscopedpointer.h> | - | ||||||||||||
42 | - | |||||||||||||
43 | #include <private/qguiapplication_p.h> | - | ||||||||||||
44 | #include <private/qwindow_p.h> | - | ||||||||||||
45 | - | |||||||||||||
46 | #include <private/qhighdpiscaling_p.h> | - | ||||||||||||
47 | - | |||||||||||||
48 | QT_BEGIN_NAMESPACE | - | ||||||||||||
49 | - | |||||||||||||
50 | class QBackingStorePrivate | - | ||||||||||||
51 | { | - | ||||||||||||
52 | public: | - | ||||||||||||
53 | QBackingStorePrivate(QWindow *w) | - | ||||||||||||
54 | : window(w) | - | ||||||||||||
55 | { | - | ||||||||||||
56 | } never executed: end of block | 0 | ||||||||||||
57 | - | |||||||||||||
58 | QWindow *window; | - | ||||||||||||
59 | QPlatformBackingStore *platformBackingStore; | - | ||||||||||||
60 | QScopedPointer<QImage> highDpiBackingstore; | - | ||||||||||||
61 | QRegion staticContents; | - | ||||||||||||
62 | QSize size; | - | ||||||||||||
63 | }; | - | ||||||||||||
64 | - | |||||||||||||
65 | /*! | - | ||||||||||||
66 | \class QBackingStore | - | ||||||||||||
67 | \since 5.0 | - | ||||||||||||
68 | \inmodule QtGui | - | ||||||||||||
69 | - | |||||||||||||
70 | \brief The QBackingStore class provides a drawing area for QWindow. | - | ||||||||||||
71 | - | |||||||||||||
72 | QBackingStore enables the use of QPainter to paint on a QWindow with type | - | ||||||||||||
73 | RasterSurface. The other way of rendering to a QWindow is through the use | - | ||||||||||||
74 | of OpenGL with QOpenGLContext. | - | ||||||||||||
75 | - | |||||||||||||
76 | A QBackingStore contains a buffered representation of the window contents, | - | ||||||||||||
77 | and thus supports partial updates by using QPainter to only update a sub | - | ||||||||||||
78 | region of the window contents. | - | ||||||||||||
79 | - | |||||||||||||
80 | QBackingStore might be used by an application that wants to use QPainter | - | ||||||||||||
81 | without OpenGL acceleration and without the extra overhead of using the | - | ||||||||||||
82 | QWidget or QGraphicsView UI stacks. For an example of how to use | - | ||||||||||||
83 | QBackingStore see the \l{Raster Window Example}. | - | ||||||||||||
84 | */ | - | ||||||||||||
85 | - | |||||||||||||
86 | /*! | - | ||||||||||||
87 | Flushes the given \a region from the specified window \a win onto the | - | ||||||||||||
88 | screen. | - | ||||||||||||
89 | - | |||||||||||||
90 | Note that the \a offset parameter is currently unused. | - | ||||||||||||
91 | */ | - | ||||||||||||
92 | void QBackingStore::flush(const QRegion ®ion, QWindow *win, const QPoint &offset) | - | ||||||||||||
93 | { | - | ||||||||||||
94 | if (!win)
| 0 | ||||||||||||
95 | win = window(); never executed: win = window(); | 0 | ||||||||||||
96 | if (!win->handle()) {
| 0 | ||||||||||||
97 | qWarning() << "QBackingStore::flush() called for " | - | ||||||||||||
98 | << win << " which does not have a handle."; | - | ||||||||||||
99 | return; never executed: return; | 0 | ||||||||||||
100 | } | - | ||||||||||||
101 | - | |||||||||||||
102 | #ifdef QBACKINGSTORE_DEBUG | - | ||||||||||||
103 | if (win && win->isTopLevel() && !qt_window_private(win)->receivedExpose) { | - | ||||||||||||
104 | qWarning().nospace() << "QBackingStore::flush() called with non-exposed window " | - | ||||||||||||
105 | << win << ", behavior is undefined"; | - | ||||||||||||
106 | } | - | ||||||||||||
107 | #endif | - | ||||||||||||
108 | - | |||||||||||||
109 | d_ptr->platformBackingStore->flush(win, QHighDpi::toNativeLocalRegion(region, win), | - | ||||||||||||
110 | QHighDpi::toNativeLocalPosition(offset, win)); | - | ||||||||||||
111 | } never executed: end of block | 0 | ||||||||||||
112 | - | |||||||||||||
113 | /*! | - | ||||||||||||
114 | \fn QPaintDevice* QBackingStore::paintDevice() | - | ||||||||||||
115 | - | |||||||||||||
116 | Implement this function to return the appropriate paint device. | - | ||||||||||||
117 | */ | - | ||||||||||||
118 | QPaintDevice *QBackingStore::paintDevice() | - | ||||||||||||
119 | { | - | ||||||||||||
120 | QPaintDevice *device = d_ptr->platformBackingStore->paintDevice(); | - | ||||||||||||
121 | - | |||||||||||||
122 | if (QHighDpiScaling::isActive() && device->devType() == QInternal::Image)
| 0 | ||||||||||||
123 | return d_ptr->highDpiBackingstore.data(); never executed: return d_ptr->highDpiBackingstore.data(); | 0 | ||||||||||||
124 | - | |||||||||||||
125 | return device; never executed: return device; | 0 | ||||||||||||
126 | } | - | ||||||||||||
127 | - | |||||||||||||
128 | /*! | - | ||||||||||||
129 | Constructs an empty surface for the given top-level \a window. | - | ||||||||||||
130 | */ | - | ||||||||||||
131 | QBackingStore::QBackingStore(QWindow *window) | - | ||||||||||||
132 | : d_ptr(new QBackingStorePrivate(window)) | - | ||||||||||||
133 | { | - | ||||||||||||
134 | d_ptr->platformBackingStore = QGuiApplicationPrivate::platformIntegration()->createPlatformBackingStore(window); | - | ||||||||||||
135 | } never executed: end of block | 0 | ||||||||||||
136 | - | |||||||||||||
137 | /*! | - | ||||||||||||
138 | Destroys this surface. | - | ||||||||||||
139 | */ | - | ||||||||||||
140 | QBackingStore::~QBackingStore() | - | ||||||||||||
141 | { | - | ||||||||||||
142 | delete d_ptr->platformBackingStore; | - | ||||||||||||
143 | } never executed: end of block | 0 | ||||||||||||
144 | - | |||||||||||||
145 | /*! | - | ||||||||||||
146 | Returns a pointer to the top-level window associated with this | - | ||||||||||||
147 | surface. | - | ||||||||||||
148 | */ | - | ||||||||||||
149 | QWindow* QBackingStore::window() const | - | ||||||||||||
150 | { | - | ||||||||||||
151 | return d_ptr->window; never executed: return d_ptr->window; | 0 | ||||||||||||
152 | } | - | ||||||||||||
153 | - | |||||||||||||
154 | /*! | - | ||||||||||||
155 | This function is called before painting onto the surface begins, | - | ||||||||||||
156 | with the \a region in which the painting will occur. | - | ||||||||||||
157 | - | |||||||||||||
158 | \sa endPaint(), paintDevice() | - | ||||||||||||
159 | */ | - | ||||||||||||
160 | - | |||||||||||||
161 | void QBackingStore::beginPaint(const QRegion ®ion) | - | ||||||||||||
162 | { | - | ||||||||||||
163 | if (d_ptr->highDpiBackingstore &&
| 0 | ||||||||||||
164 | d_ptr->highDpiBackingstore->devicePixelRatio() != d_ptr->window->devicePixelRatio())
| 0 | ||||||||||||
165 | resize(size()); never executed: resize(size()); | 0 | ||||||||||||
166 | - | |||||||||||||
167 | d_ptr->platformBackingStore->beginPaint(QHighDpi::toNativeLocalRegion(region, d_ptr->window)); | - | ||||||||||||
168 | - | |||||||||||||
169 | // When QtGui is applying a high-dpi scale factor the backing store | - | ||||||||||||
170 | // creates a "large" backing store image. This image needs to be | - | ||||||||||||
171 | // painted on as a high-dpi image, which is done by setting | - | ||||||||||||
172 | // devicePixelRatio. Do this on a separate image instance that shares | - | ||||||||||||
173 | // the image data to avoid having the new devicePixelRatio be propagated | - | ||||||||||||
174 | // back to the platform plugin. | - | ||||||||||||
175 | QPaintDevice *device = d_ptr->platformBackingStore->paintDevice(); | - | ||||||||||||
176 | if (QHighDpiScaling::isActive() && device->devType() == QInternal::Image) {
| 0 | ||||||||||||
177 | QImage *source = static_cast<QImage *>(device); | - | ||||||||||||
178 | const bool needsNewImage = d_ptr->highDpiBackingstore.isNull()
| 0 | ||||||||||||
179 | || source->data_ptr() != d_ptr->highDpiBackingstore->data_ptr()
| 0 | ||||||||||||
180 | || source->size() != d_ptr->highDpiBackingstore->size()
| 0 | ||||||||||||
181 | || source->devicePixelRatio() != d_ptr->highDpiBackingstore->devicePixelRatio();
| 0 | ||||||||||||
182 | if (needsNewImage) {
| 0 | ||||||||||||
183 | qCDebug(lcScaling) << "QBackingStore::beginPaint new backingstore for" << d_ptr->window; never executed: QMessageLogger(__FILE__, 183, __PRETTY_FUNCTION__, lcScaling().categoryName()).debug() << "QBackingStore::beginPaint new backingstore for" << d_ptr->window;
| 0 | ||||||||||||
184 | qCDebug(lcScaling) << " source size" << source->size() << "dpr" << source->devicePixelRatio(); never executed: QMessageLogger(__FILE__, 184, __PRETTY_FUNCTION__, lcScaling().categoryName()).debug() << " source size" << source->size() << "dpr" << source->devicePixelRatio();
| 0 | ||||||||||||
185 | d_ptr->highDpiBackingstore.reset( | - | ||||||||||||
186 | new QImage(source->bits(), source->width(), source->height(), source->bytesPerLine(), source->format())); | - | ||||||||||||
187 | - | |||||||||||||
188 | qreal targetDevicePixelRatio = d_ptr->window->devicePixelRatio(); | - | ||||||||||||
189 | d_ptr->highDpiBackingstore->setDevicePixelRatio(targetDevicePixelRatio); | - | ||||||||||||
190 | qCDebug(lcScaling) <<" destination size" << d_ptr->highDpiBackingstore->size() never executed: QMessageLogger(__FILE__, 190, __PRETTY_FUNCTION__, lcScaling().categoryName()).debug() <<" destination size" << d_ptr->highDpiBackingstore->size() << "dpr" << targetDevicePixelRatio;
| 0 | ||||||||||||
191 | << "dpr" << targetDevicePixelRatio; never executed: QMessageLogger(__FILE__, 190, __PRETTY_FUNCTION__, lcScaling().categoryName()).debug() <<" destination size" << d_ptr->highDpiBackingstore->size() << "dpr" << targetDevicePixelRatio; | 0 | ||||||||||||
192 | } never executed: end of block | 0 | ||||||||||||
193 | } never executed: end of block | 0 | ||||||||||||
194 | } never executed: end of block | 0 | ||||||||||||
195 | - | |||||||||||||
196 | /*! | - | ||||||||||||
197 | This function is called after painting onto the surface has ended. | - | ||||||||||||
198 | - | |||||||||||||
199 | \sa beginPaint(), paintDevice() | - | ||||||||||||
200 | */ | - | ||||||||||||
201 | void QBackingStore::endPaint() | - | ||||||||||||
202 | { | - | ||||||||||||
203 | d_ptr->platformBackingStore->endPaint(); | - | ||||||||||||
204 | } never executed: end of block | 0 | ||||||||||||
205 | - | |||||||||||||
206 | /*! | - | ||||||||||||
207 | Sets the size of the windowsurface to be \a size. | - | ||||||||||||
208 | - | |||||||||||||
209 | \sa size() | - | ||||||||||||
210 | */ | - | ||||||||||||
211 | void QBackingStore::resize(const QSize &size) | - | ||||||||||||
212 | { | - | ||||||||||||
213 | d_ptr->size = size; | - | ||||||||||||
214 | d_ptr->platformBackingStore->resize(QHighDpi::toNativePixels(size, d_ptr->window), d_ptr->staticContents); | - | ||||||||||||
215 | } never executed: end of block | 0 | ||||||||||||
216 | - | |||||||||||||
217 | /*! | - | ||||||||||||
218 | Returns the current size of the windowsurface. | - | ||||||||||||
219 | */ | - | ||||||||||||
220 | QSize QBackingStore::size() const | - | ||||||||||||
221 | { | - | ||||||||||||
222 | return d_ptr->size; never executed: return d_ptr->size; | 0 | ||||||||||||
223 | } | - | ||||||||||||
224 | - | |||||||||||||
225 | /*! | - | ||||||||||||
226 | Scrolls the given \a area \a dx pixels to the right and \a dy | - | ||||||||||||
227 | downward; both \a dx and \a dy may be negative. | - | ||||||||||||
228 | - | |||||||||||||
229 | Returns \c true if the area was scrolled successfully; false otherwise. | - | ||||||||||||
230 | */ | - | ||||||||||||
231 | bool QBackingStore::scroll(const QRegion &area, int dx, int dy) | - | ||||||||||||
232 | { | - | ||||||||||||
233 | // Disable scrolling for non-integer scroll deltas. For this case | - | ||||||||||||
234 | // the the existing rendered pixels can't be re-used, and we return | - | ||||||||||||
235 | // false to signal that a repaint is needed. | - | ||||||||||||
236 | const qreal nativeDx = QHighDpi::toNativePixels(qreal(dx), d_ptr->window); | - | ||||||||||||
237 | const qreal nativeDy = QHighDpi::toNativePixels(qreal(dy), d_ptr->window); | - | ||||||||||||
238 | if (qFloor(nativeDx) != nativeDx || qFloor(nativeDy) != nativeDy)
| 0 | ||||||||||||
239 | return false; never executed: return false; | 0 | ||||||||||||
240 | - | |||||||||||||
241 | return d_ptr->platformBackingStore->scroll(QHighDpi::toNativeLocalRegion(area, d_ptr->window), never executed: return d_ptr->platformBackingStore->scroll(QHighDpi::toNativeLocalRegion(area, d_ptr->window), nativeDx, nativeDy); | 0 | ||||||||||||
242 | nativeDx, nativeDy); never executed: return d_ptr->platformBackingStore->scroll(QHighDpi::toNativeLocalRegion(area, d_ptr->window), nativeDx, nativeDy); | 0 | ||||||||||||
243 | } | - | ||||||||||||
244 | - | |||||||||||||
245 | /*! | - | ||||||||||||
246 | Set \a region as the static contents of this window. | - | ||||||||||||
247 | */ | - | ||||||||||||
248 | void QBackingStore::setStaticContents(const QRegion ®ion) | - | ||||||||||||
249 | { | - | ||||||||||||
250 | d_ptr->staticContents = region; | - | ||||||||||||
251 | } never executed: end of block | 0 | ||||||||||||
252 | - | |||||||||||||
253 | /*! | - | ||||||||||||
254 | Returns a pointer to the QRegion that has the static contents | - | ||||||||||||
255 | of this window. | - | ||||||||||||
256 | */ | - | ||||||||||||
257 | QRegion QBackingStore::staticContents() const | - | ||||||||||||
258 | { | - | ||||||||||||
259 | return d_ptr->staticContents; never executed: return d_ptr->staticContents; | 0 | ||||||||||||
260 | } | - | ||||||||||||
261 | - | |||||||||||||
262 | /*! | - | ||||||||||||
263 | Returns a boolean indicating if this window | - | ||||||||||||
264 | has static contents or not. | - | ||||||||||||
265 | */ | - | ||||||||||||
266 | bool QBackingStore::hasStaticContents() const | - | ||||||||||||
267 | { | - | ||||||||||||
268 | return !d_ptr->staticContents.isEmpty(); never executed: return !d_ptr->staticContents.isEmpty(); | 0 | ||||||||||||
269 | } | - | ||||||||||||
270 | - | |||||||||||||
271 | void Q_GUI_EXPORT qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset) | - | ||||||||||||
272 | { | - | ||||||||||||
273 | // make sure we don't detach | - | ||||||||||||
274 | uchar *mem = const_cast<uchar*>(const_cast<const QImage &>(img).bits()); | - | ||||||||||||
275 | - | |||||||||||||
276 | int lineskip = img.bytesPerLine(); | - | ||||||||||||
277 | int depth = img.depth() >> 3; | - | ||||||||||||
278 | - | |||||||||||||
279 | const QRect imageRect(0, 0, img.width(), img.height()); | - | ||||||||||||
280 | const QRect r = rect & imageRect & imageRect.translated(-offset); | - | ||||||||||||
281 | const QPoint p = rect.topLeft() + offset; | - | ||||||||||||
282 | - | |||||||||||||
283 | if (r.isEmpty())
| 0 | ||||||||||||
284 | return; never executed: return; | 0 | ||||||||||||
285 | - | |||||||||||||
286 | const uchar *src; | - | ||||||||||||
287 | uchar *dest; | - | ||||||||||||
288 | - | |||||||||||||
289 | if (r.top() < p.y()) {
| 0 | ||||||||||||
290 | src = mem + r.bottom() * lineskip + r.left() * depth; | - | ||||||||||||
291 | dest = mem + (p.y() + r.height() - 1) * lineskip + p.x() * depth; | - | ||||||||||||
292 | lineskip = -lineskip; | - | ||||||||||||
293 | } else { never executed: end of block | 0 | ||||||||||||
294 | src = mem + r.top() * lineskip + r.left() * depth; | - | ||||||||||||
295 | dest = mem + p.y() * lineskip + p.x() * depth; | - | ||||||||||||
296 | } never executed: end of block | 0 | ||||||||||||
297 | - | |||||||||||||
298 | const int w = r.width(); | - | ||||||||||||
299 | int h = r.height(); | - | ||||||||||||
300 | const int bytes = w * depth; | - | ||||||||||||
301 | - | |||||||||||||
302 | // overlapping segments? | - | ||||||||||||
303 | if (offset.y() == 0 && qAbs(offset.x()) < w) {
| 0 | ||||||||||||
304 | do { | - | ||||||||||||
305 | ::memmove(dest, src, bytes); | - | ||||||||||||
306 | dest += lineskip; | - | ||||||||||||
307 | src += lineskip; | - | ||||||||||||
308 | } while (--h); never executed: end of block
| 0 | ||||||||||||
309 | } else { never executed: end of block | 0 | ||||||||||||
310 | do { | - | ||||||||||||
311 | ::memcpy(dest, src, bytes); | - | ||||||||||||
312 | dest += lineskip; | - | ||||||||||||
313 | src += lineskip; | - | ||||||||||||
314 | } while (--h); never executed: end of block
| 0 | ||||||||||||
315 | } never executed: end of block | 0 | ||||||||||||
316 | } | - | ||||||||||||
317 | - | |||||||||||||
318 | /*! | - | ||||||||||||
319 | Returns a pointer to the QPlatformBackingStore implementation | - | ||||||||||||
320 | */ | - | ||||||||||||
321 | QPlatformBackingStore *QBackingStore::handle() const | - | ||||||||||||
322 | { | - | ||||||||||||
323 | return d_ptr->platformBackingStore; never executed: return d_ptr->platformBackingStore; | 0 | ||||||||||||
324 | } | - | ||||||||||||
325 | - | |||||||||||||
326 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |