1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qbackingstore.h>
5#include <qwindow.h>
6#include <qpixmap.h>
7#include <qpa/qplatformbackingstore.h>
8#include <qpa/qplatformintegration.h>
9#include <qscreen.h>
10#include <qdebug.h>
11#include <qscopedpointer.h>
12
13#include <private/qguiapplication_p.h>
14#include <private/qwindow_p.h>
15
16#include <private/qhighdpiscaling_p.h>
17
18QT_BEGIN_NAMESPACE
19
20class QBackingStorePrivate
21{
22public:
23 QBackingStorePrivate(QWindow *w)
24 : window(w)
25 {
26 }
27
28 // Returns the DPR for the backing store. This is the DPR for the QWindow,
29 // possibly rounded up to the nearest integer.
30 qreal backingStoreDevicePixelRatio() const
31 {
32 // Note: keep in sync with QWidget::metric()!
33 qreal windowDpr = window->devicePixelRatio();
34 return downscale ? std::ceil(x: windowDpr) : windowDpr;
35 }
36
37 // Returns the factor used for converting from device independent to native
38 // backing store sizes. Normally this is just the gui scale factor, however
39 // if the backing store rounds the DPR up to the nearest integer then we also
40 // need to account for the factor introduced by that rounding.
41 qreal deviceIndependentToNativeFactor() const
42 {
43 const qreal roundingFactor = backingStoreDevicePixelRatio() / window->devicePixelRatio();
44 const qreal guiFactor = QHighDpiScaling::factor(context: window);
45 return roundingFactor * guiFactor;
46 }
47
48 QWindow *window;
49 QPlatformBackingStore *platformBackingStore = nullptr;
50 QScopedPointer<QImage> highDpiBackingstore;
51 QRegion staticContents;
52 QSize size;
53 bool downscale = qEnvironmentVariableIntValue(varName: "QT_WIDGETS_HIGHDPI_DOWNSCALE") > 0;
54};
55
56/*!
57 \class QBackingStore
58 \since 5.0
59 \inmodule QtGui
60
61 \brief The QBackingStore class provides a drawing area for QWindow.
62
63 QBackingStore enables the use of QPainter to paint on a QWindow with type
64 RasterSurface. The other way of rendering to a QWindow is through the use
65 of OpenGL with QOpenGLContext.
66
67 A QBackingStore contains a buffered representation of the window contents,
68 and thus supports partial updates by using QPainter to only update a sub
69 region of the window contents.
70
71 QBackingStore might be used by an application that wants to use QPainter
72 without OpenGL acceleration and without the extra overhead of using the
73 QWidget or QGraphicsView UI stacks. For an example of how to use
74 QBackingStore see the \l{Raster Window Example}.
75*/
76
77/*!
78 Constructs an empty surface for the given top-level \a window.
79*/
80QBackingStore::QBackingStore(QWindow *window)
81 : d_ptr(new QBackingStorePrivate(window))
82{
83 if (window->handle()) {
84 // Create platform backingstore up front if we have a platform window,
85 // otherwise delay the creation until absolutely necessary.
86 handle();
87 }
88}
89
90/*!
91 Destroys this surface.
92*/
93QBackingStore::~QBackingStore()
94{
95 delete d_ptr->platformBackingStore;
96}
97
98/*!
99 Returns a pointer to the top-level window associated with this
100 surface.
101*/
102QWindow* QBackingStore::window() const
103{
104 return d_ptr->window;
105}
106
107/*!
108 Begins painting on the backing store surface in the given \a region.
109
110 You should call this function before using the paintDevice() to
111 paint.
112
113 \sa endPaint(), paintDevice()
114*/
115
116void QBackingStore::beginPaint(const QRegion &region)
117{
118 const qreal dpr = d_ptr->backingStoreDevicePixelRatio();
119
120 if (d_ptr->highDpiBackingstore &&
121 d_ptr->highDpiBackingstore->devicePixelRatio() != dpr)
122 resize(size: size());
123
124 QPlatformBackingStore *platformBackingStore = handle();
125 platformBackingStore->beginPaint(QHighDpi::scale(region, scaleFactor: d_ptr->deviceIndependentToNativeFactor()));
126
127 // When QtGui is applying a high-dpi scale factor the backing store
128 // creates a "large" backing store image. This image needs to be
129 // painted on as a high-dpi image, which is done by setting
130 // devicePixelRatio. Do this on a separate image instance that shares
131 // the image data to avoid having the new devicePixelRatio be propagated
132 // back to the platform plugin.
133 QPaintDevice *device = platformBackingStore->paintDevice();
134 if (QHighDpiScaling::isActive() && device->devType() == QInternal::Image) {
135 QImage *source = static_cast<QImage *>(device);
136 const bool needsNewImage = d_ptr->highDpiBackingstore.isNull()
137 || source->data_ptr() != d_ptr->highDpiBackingstore->data_ptr()
138 || source->size() != d_ptr->highDpiBackingstore->size()
139 || source->devicePixelRatio() != d_ptr->highDpiBackingstore->devicePixelRatio();
140 if (needsNewImage) {
141 d_ptr->highDpiBackingstore.reset(
142 other: new QImage(source->bits(), source->width(), source->height(), source->bytesPerLine(), source->format()));
143
144 d_ptr->highDpiBackingstore->setDevicePixelRatio(dpr);
145 }
146 }
147}
148
149/*!
150 Returns the paint device for this surface.
151
152 \warning The device is only valid between calls to beginPaint() and
153 endPaint(). You should not cache the returned value.
154*/
155QPaintDevice *QBackingStore::paintDevice()
156{
157 QPaintDevice *device = handle()->paintDevice();
158
159 if (QHighDpiScaling::isActive() && device->devType() == QInternal::Image)
160 return d_ptr->highDpiBackingstore.data();
161
162 return device;
163}
164
165/*!
166 Ends painting.
167
168 You should call this function after painting with the paintDevice()
169 has ended.
170
171 \sa beginPaint(), paintDevice()
172*/
173void QBackingStore::endPaint()
174{
175 if (paintDevice()->paintingActive())
176 qWarning(msg: "QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?");
177
178 handle()->endPaint();
179}
180
181/*!
182 Flushes the given \a region from the specified \a window onto the
183 screen.
184
185 The \a window must either be the top level window represented by
186 this backingstore, or a non-transient child of that window. Passing
187 \nullptr falls back to using the backingstore's top level window.
188
189 If the \a window is a child window, the \a region should be in child window
190 coordinates, and the \a offset should be the child window's offset in relation
191 to the backingstore's top level window.
192
193 You should call this function after ending painting with endPaint().
194*/
195void QBackingStore::flush(const QRegion &region, QWindow *window, const QPoint &offset)
196{
197 QWindow *topLevelWindow = this->window();
198
199 if (!window)
200 window = topLevelWindow;
201 if (!window->handle()) {
202 qWarning() << "QBackingStore::flush() called for "
203 << window << " which does not have a handle.";
204 return;
205 }
206
207 Q_ASSERT(window == topLevelWindow || topLevelWindow->isAncestorOf(window, QWindow::ExcludeTransients));
208
209 const qreal toNativeFactor = d_ptr->deviceIndependentToNativeFactor();
210
211 QRegion nativeRegion = QHighDpi::scale(region, scaleFactor: toNativeFactor);
212 QPoint nativeOffset;
213 if (!offset.isNull()) {
214 nativeOffset = QHighDpi::scale(pos: offset, scaleFactor: toNativeFactor);
215 // Under fractional DPR, rounding of region and offset may accumulate to an off-by-one
216 QPoint topLeft = region.boundingRect().topLeft() + offset;
217 QPoint nativeTopLeft = QHighDpi::scale(pos: topLeft, scaleFactor: toNativeFactor);
218 QPoint diff = nativeTopLeft - (nativeRegion.boundingRect().topLeft() + nativeOffset);
219 Q_ASSERT(qMax(qAbs(diff.x()), qAbs(diff.y())) <= 1);
220 nativeRegion.translate(p: diff);
221 }
222 handle()->flush(window, region: nativeRegion, offset: nativeOffset);
223}
224
225/*!
226 Sets the size of the window surface to \a size.
227
228 \sa size()
229*/
230void QBackingStore::resize(const QSize &size)
231{
232 d_ptr->size = size;
233 handle()->resize(size: QHighDpi::scale(value: size, scaleFactor: d_ptr->deviceIndependentToNativeFactor()), staticContents: d_ptr->staticContents);
234}
235
236/*!
237 Returns the current size of the window surface.
238*/
239QSize QBackingStore::size() const
240{
241 return d_ptr->size;
242}
243
244/*!
245 Scrolls the given \a area \a dx pixels to the right and \a dy
246 downward; both \a dx and \a dy may be negative.
247
248 Returns \c true if the area was scrolled successfully; false otherwise.
249*/
250bool QBackingStore::scroll(const QRegion &area, int dx, int dy)
251{
252 // Disable scrolling for non-integer scroll deltas. For this case
253 // the existing rendered pixels can't be re-used, and we return
254 // false to signal that a repaint is needed.
255 const qreal toNativeFactor = d_ptr->deviceIndependentToNativeFactor();
256 const qreal nativeDx = QHighDpi::scale(value: qreal(dx), scaleFactor: toNativeFactor);
257 const qreal nativeDy = QHighDpi::scale(value: qreal(dy), scaleFactor: toNativeFactor);
258 if (qFloor(v: nativeDx) != nativeDx || qFloor(v: nativeDy) != nativeDy)
259 return false;
260
261 return handle()->scroll(area: QHighDpi::scale(region: area, scaleFactor: toNativeFactor), dx: nativeDx, dy: nativeDy);
262}
263
264/*!
265 Set \a region as the static contents of this window.
266*/
267void QBackingStore::setStaticContents(const QRegion &region)
268{
269 d_ptr->staticContents = region;
270}
271
272/*!
273 Returns a QRegion representing the area of the window that
274 has static contents.
275*/
276QRegion QBackingStore::staticContents() const
277{
278 return d_ptr->staticContents;
279}
280
281/*!
282 Returns a boolean indicating if this window has static contents or not.
283*/
284bool QBackingStore::hasStaticContents() const
285{
286 return !d_ptr->staticContents.isEmpty();
287}
288
289void Q_GUI_EXPORT qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset)
290{
291 // make sure we don't detach
292 uchar *mem = const_cast<uchar*>(img.constBits());
293
294 qsizetype lineskip = img.bytesPerLine();
295 int depth = img.depth() >> 3;
296
297 const QRect imageRect(0, 0, img.width(), img.height());
298 const QRect sourceRect = rect.intersected(other: imageRect).intersected(other: imageRect.translated(p: -offset));
299 if (sourceRect.isEmpty())
300 return;
301
302 const QRect destRect = sourceRect.translated(p: offset);
303 Q_ASSERT_X(imageRect.contains(destRect), "qt_scrollRectInImage",
304 "The sourceRect should already account for clipping, both pre and post scroll");
305
306 const uchar *src;
307 uchar *dest;
308
309 if (sourceRect.top() < destRect.top()) {
310 src = mem + sourceRect.bottom() * lineskip + sourceRect.left() * depth;
311 dest = mem + (destRect.top() + sourceRect.height() - 1) * lineskip + destRect.left() * depth;
312 lineskip = -lineskip;
313 } else {
314 src = mem + sourceRect.top() * lineskip + sourceRect.left() * depth;
315 dest = mem + destRect.top() * lineskip + destRect.left() * depth;
316 }
317
318 const int w = sourceRect.width();
319 int h = sourceRect.height();
320 const int bytes = w * depth;
321
322 // overlapping segments?
323 if (offset.y() == 0 && qAbs(t: offset.x()) < w) {
324 do {
325 ::memmove(dest: dest, src: src, n: bytes);
326 dest += lineskip;
327 src += lineskip;
328 } while (--h);
329 } else {
330 do {
331 ::memcpy(dest: dest, src: src, n: bytes);
332 dest += lineskip;
333 src += lineskip;
334 } while (--h);
335 }
336}
337
338/*!
339 Returns a pointer to the QPlatformBackingStore implementation
340*/
341QPlatformBackingStore *QBackingStore::handle() const
342{
343 if (!d_ptr->platformBackingStore) {
344 d_ptr->platformBackingStore = QGuiApplicationPrivate::platformIntegration()->createPlatformBackingStore(window: d_ptr->window);
345 d_ptr->platformBackingStore->setBackingStore(const_cast<QBackingStore*>(this));
346 }
347 return d_ptr->platformBackingStore;
348}
349
350QT_END_NAMESPACE
351

source code of qtbase/src/gui/painting/qbackingstore.cpp