1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qbackingstore.h>
#include <qwindow.h>
#include <qpixmap.h>
#include <qpa/qplatformbackingstore.h>
#include <qpa/qplatformintegration.h>
#include <qscreen.h>
#include <qdebug.h>
#include <qscopedpointer.h>
#include <private/qguiapplication_p.h>
#include <private/qwindow_p.h>
#include <private/qhighdpiscaling_p.h>
QT_BEGIN_NAMESPACE
class QBackingStorePrivate
{
public:
QBackingStorePrivate(QWindow *w)
: window(w)
{
}
QWindow *window;
QPlatformBackingStore *platformBackingStore = nullptr;
QScopedPointer<QImage> highDpiBackingstore;
QRegion staticContents;
QSize size;
};
/*!
\class QBackingStore
\since 5.0
\inmodule QtGui
\brief The QBackingStore class provides a drawing area for QWindow.
QBackingStore enables the use of QPainter to paint on a QWindow with type
RasterSurface. The other way of rendering to a QWindow is through the use
of OpenGL with QOpenGLContext.
A QBackingStore contains a buffered representation of the window contents,
and thus supports partial updates by using QPainter to only update a sub
region of the window contents.
QBackingStore might be used by an application that wants to use QPainter
without OpenGL acceleration and without the extra overhead of using the
QWidget or QGraphicsView UI stacks. For an example of how to use
QBackingStore see the \l{Raster Window Example}.
*/
/*!
Constructs an empty surface for the given top-level \a window.
*/
QBackingStore::QBackingStore(QWindow *window)
: d_ptr(new QBackingStorePrivate(window))
{
if (window->handle()) {
// Create platform backingstore up front if we have a platform window,
// otherwise delay the creation until absolutely necessary.
handle();
}
}
/*!
Destroys this surface.
*/
QBackingStore::~QBackingStore()
{
delete d_ptr->platformBackingStore;
}
/*!
Returns a pointer to the top-level window associated with this
surface.
*/
QWindow* QBackingStore::window() const
{
return d_ptr->window;
}
/*!
Begins painting on the backing store surface in the given \a region.
You should call this function before using the paintDevice() to
paint.
\sa endPaint(), paintDevice()
*/
void QBackingStore::beginPaint(const QRegion ®ion)
{
if (d_ptr->highDpiBackingstore &&
d_ptr->highDpiBackingstore->devicePixelRatio() != d_ptr->window->devicePixelRatio())
resize(size());
QPlatformBackingStore *platformBackingStore = handle();
platformBackingStore->beginPaint(QHighDpi::toNativeLocalRegion(region, d_ptr->window));
// When QtGui is applying a high-dpi scale factor the backing store
// creates a "large" backing store image. This image needs to be
// painted on as a high-dpi image, which is done by setting
// devicePixelRatio. Do this on a separate image instance that shares
// the image data to avoid having the new devicePixelRatio be propagated
// back to the platform plugin.
QPaintDevice *device = platformBackingStore->paintDevice();
if (QHighDpiScaling::isActive() && device->devType() == QInternal::Image) {
QImage *source = static_cast<QImage *>(device);
const bool needsNewImage = d_ptr->highDpiBackingstore.isNull()
|| source->data_ptr() != d_ptr->highDpiBackingstore->data_ptr()
|| source->size() != d_ptr->highDpiBackingstore->size()
|| source->devicePixelRatio() != d_ptr->highDpiBackingstore->devicePixelRatio();
if (needsNewImage) {
qCDebug(lcScaling) << "QBackingStore::beginPaint new backingstore for" << d_ptr->window;
qCDebug(lcScaling) << " source size" << source->size() << "dpr" << source->devicePixelRatio();
d_ptr->highDpiBackingstore.reset(
new QImage(source->bits(), source->width(), source->height(), source->bytesPerLine(), source->format()));
qreal targetDevicePixelRatio = d_ptr->window->devicePixelRatio();
d_ptr->highDpiBackingstore->setDevicePixelRatio(targetDevicePixelRatio);
qCDebug(lcScaling) <<" destination size" << d_ptr->highDpiBackingstore->size()
<< "dpr" << targetDevicePixelRatio;
}
}
}
/*!
Returns the paint device for this surface.
\warning The device is only valid between calls to beginPaint() and
endPaint(). You should not cache the returned value.
*/
QPaintDevice *QBackingStore::paintDevice()
{
QPaintDevice *device = handle()->paintDevice();
if (QHighDpiScaling::isActive() && device->devType() == QInternal::Image)
return d_ptr->highDpiBackingstore.data();
return device;
}
/*!
Ends painting.
You should call this function after painting with the paintDevice()
has ended.
\sa beginPaint(), paintDevice()
*/
void QBackingStore::endPaint()
{
if (paintDevice()->paintingActive())
qWarning("QBackingStore::endPaint() called with active painter; did you forget to destroy it or call QPainter::end() on it?");
handle()->endPaint();
}
static bool isRasterSurface(QWindow *window)
{
switch (window->surfaceType()) {
case QSurface::RasterSurface:
case QSurface::RasterGLSurface:
return true;
default:
return false;
};
}
/*!
Flushes the given \a region from the specified \a window onto the
screen.
The \a window must either be the top level window represented by
this backingstore, or a non-transient child of that window. Passing
\nullptr falls back to using the backingstore's top level window.
If the \a window is a child window, the \a region should be in child window
coordinates, and the \a offset should be the child window's offset in relation
to the backingstore's top level window.
You should call this function after ending painting with endPaint().
*/
void QBackingStore::flush(const QRegion ®ion, QWindow *window, const QPoint &offset)
{
QWindow *topLevelWindow = this->window();
if (!window)
window = topLevelWindow;
if (!window->handle()) {
qWarning() << "QBackingStore::flush() called for "
<< window << " which does not have a handle.";
return;
}
if (!isRasterSurface(window)) {
qWarning() << "Attempted flush to non-raster surface" << window << "of type" << window->surfaceType()
<< (window->inherits("QWidgetWindow") ? "(consider using Qt::WA_PaintOnScreen to exclude "
"from backingstore sync)" : "");
return;
}
#ifdef QBACKINGSTORE_DEBUG
if (window && window->isTopLevel() && !qt_window_private(window)->receivedExpose) {
qWarning().nospace() << "QBackingStore::flush() called with non-exposed window "
<< window << ", behavior is undefined";
}
#endif
Q_ASSERT(window == topLevelWindow || topLevelWindow->isAncestorOf(window, QWindow::ExcludeTransients));
QRegion nativeRegion = QHighDpi::toNativeLocalRegion(region, window);
QPoint nativeOffset;
if (!offset.isNull()) {
nativeOffset = QHighDpi::toNativeLocalPosition(offset, window);
// Under fractional DPR, rounding of region and offset may accumulate to an off-by-one
QPoint topLeft = region.boundingRect().topLeft() + offset;
QPoint nativeTopLeft = QHighDpi::toNativeLocalPosition(topLeft, window);
QPoint diff = nativeTopLeft - (nativeRegion.boundingRect().topLeft() + nativeOffset);
Q_ASSERT(qMax(qAbs(diff.x()), qAbs(diff.y())) <= 1);
nativeRegion.translate(diff);
}
handle()->flush(window, nativeRegion, nativeOffset);
}
/*!
Sets the size of the window surface to \a size.
\sa size()
*/
void QBackingStore::resize(const QSize &size)
{
d_ptr->size = size;
handle()->resize(QHighDpi::toNativePixels(size, d_ptr->window), d_ptr->staticContents);
}
/*!
Returns the current size of the window surface.
*/
QSize QBackingStore::size() const
{
return d_ptr->size;
}
/*!
Scrolls the given \a area \a dx pixels to the right and \a dy
downward; both \a dx and \a dy may be negative.
Returns \c true if the area was scrolled successfully; false otherwise.
*/
bool QBackingStore::scroll(const QRegion &area, int dx, int dy)
{
// Disable scrolling for non-integer scroll deltas. For this case
// the existing rendered pixels can't be re-used, and we return
// false to signal that a repaint is needed.
const qreal nativeDx = QHighDpi::toNativePixels(qreal(dx), d_ptr->window);
const qreal nativeDy = QHighDpi::toNativePixels(qreal(dy), d_ptr->window);
if (qFloor(nativeDx) != nativeDx || qFloor(nativeDy) != nativeDy)
return false;
return handle()->scroll(QHighDpi::toNativeLocalRegion(area, d_ptr->window),
nativeDx, nativeDy);
}
/*!
Set \a region as the static contents of this window.
*/
void QBackingStore::setStaticContents(const QRegion ®ion)
{
d_ptr->staticContents = region;
}
/*!
Returns a QRegion representing the area of the window that
has static contents.
*/
QRegion QBackingStore::staticContents() const
{
return d_ptr->staticContents;
}
/*!
Returns a boolean indicating if this window has static contents or not.
*/
bool QBackingStore::hasStaticContents() const
{
return !d_ptr->staticContents.isEmpty();
}
void Q_GUI_EXPORT qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset)
{
// make sure we don't detach
uchar *mem = const_cast<uchar*>(img.constBits());
int lineskip = img.bytesPerLine();
int depth = img.depth() >> 3;
const QRect imageRect(0, 0, img.width(), img.height());
const QRect sourceRect = rect.intersected(imageRect).intersected(imageRect.translated(-offset));
if (sourceRect.isEmpty())
return;
const QRect destRect = sourceRect.translated(offset);
Q_ASSERT_X(imageRect.contains(destRect), "qt_scrollRectInImage",
"The sourceRect should already account for clipping, both pre and post scroll");
const uchar *src;
uchar *dest;
if (sourceRect.top() < destRect.top()) {
src = mem + sourceRect.bottom() * lineskip + sourceRect.left() * depth;
dest = mem + (destRect.top() + sourceRect.height() - 1) * lineskip + destRect.left() * depth;
lineskip = -lineskip;
} else {
src = mem + sourceRect.top() * lineskip + sourceRect.left() * depth;
dest = mem + destRect.top() * lineskip + destRect.left() * depth;
}
const int w = sourceRect.width();
int h = sourceRect.height();
const int bytes = w * depth;
// overlapping segments?
if (offset.y() == 0 && qAbs(offset.x()) < w) {
do {
::memmove(dest, src, bytes);
dest += lineskip;
src += lineskip;
} while (--h);
} else {
do {
::memcpy(dest, src, bytes);
dest += lineskip;
src += lineskip;
} while (--h);
}
}
/*!
Returns a pointer to the QPlatformBackingStore implementation
*/
QPlatformBackingStore *QBackingStore::handle() const
{
if (!d_ptr->platformBackingStore) {
d_ptr->platformBackingStore = QGuiApplicationPrivate::platformIntegration()->createPlatformBackingStore(d_ptr->window);
d_ptr->platformBackingStore->setBackingStore(const_cast<QBackingStore*>(this));
}
return d_ptr->platformBackingStore;
}
QT_END_NAMESPACE
|