2015-12-10 05:06:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
** Contact: http://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 http://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at http://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 2.1 or version 3 as published by the Free
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
**
|
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <qdebug.h>
|
2016-09-30 00:42:20 +00:00
|
|
|
#include <qpaintdevice.h>
|
|
|
|
#include <qwidget.h>
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
#include "qwindowsurface_raster_p.h"
|
2016-09-08 22:02:36 +00:00
|
|
|
#include "qimage.h"
|
2015-12-10 05:06:13 +02:00
|
|
|
#include "qwidget_p.h"
|
|
|
|
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
#include "qpixmap_x11_p.h"
|
|
|
|
#include "qt_x11_p.h"
|
|
|
|
#include "qwidget_p.h"
|
|
|
|
#include "qx11info_x11.h"
|
|
|
|
#endif
|
|
|
|
#include "qdrawhelper_p.h"
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
class QRasterWindowSurfacePrivate
|
|
|
|
{
|
|
|
|
public:
|
2016-09-08 22:02:36 +00:00
|
|
|
QImage *image;
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
GC gc;
|
2016-07-10 02:53:30 +03:00
|
|
|
#ifndef QT_NO_XSHM
|
2015-12-10 05:06:13 +02:00
|
|
|
uint needsSync : 1;
|
|
|
|
#endif
|
|
|
|
#ifndef QT_NO_XRENDER
|
|
|
|
uint translucentBackground : 1;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
uint inSetGeometry : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
QRasterWindowSurface::QRasterWindowSurface(QWidget *window, bool setDefaultSurface)
|
|
|
|
: QWindowSurface(window, setDefaultSurface), d_ptr(new QRasterWindowSurfacePrivate)
|
|
|
|
{
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
d_ptr->gc = XCreateGC(X11->display, window->handle(), 0, 0);
|
|
|
|
#ifndef QT_NO_XRENDER
|
|
|
|
d_ptr->translucentBackground = X11->use_xrender
|
|
|
|
&& window->x11Info().depth() == 32;
|
|
|
|
#endif
|
2016-07-10 02:53:30 +03:00
|
|
|
#ifndef QT_NO_XSHM
|
2015-12-10 05:06:13 +02:00
|
|
|
d_ptr->needsSync = false;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
d_ptr->image = 0;
|
|
|
|
d_ptr->inSetGeometry = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QRasterWindowSurface::~QRasterWindowSurface()
|
|
|
|
{
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
XFreeGC(X11->display, d_ptr->gc);
|
|
|
|
#endif
|
|
|
|
if (d_ptr->image)
|
|
|
|
delete d_ptr->image;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPaintDevice *QRasterWindowSurface::paintDevice()
|
|
|
|
{
|
2016-09-08 22:02:36 +00:00
|
|
|
return d_ptr->image;
|
2015-12-10 05:06:13 +02:00
|
|
|
}
|
|
|
|
|
2016-07-10 02:53:30 +03:00
|
|
|
#if defined(Q_WS_X11) && !defined(QT_NO_XSHM)
|
2015-12-10 05:06:13 +02:00
|
|
|
void QRasterWindowSurface::syncX()
|
|
|
|
{
|
|
|
|
// delay writing to the backbuffer until we know for sure X is done reading from it
|
|
|
|
if (d_ptr->needsSync) {
|
|
|
|
XSync(X11->display, false);
|
|
|
|
d_ptr->needsSync = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void QRasterWindowSurface::beginPaint(const QRegion &rgn)
|
|
|
|
{
|
2016-07-10 02:53:30 +03:00
|
|
|
#if defined(Q_WS_X11) && !defined(QT_NO_XSHM)
|
2015-12-10 05:06:13 +02:00
|
|
|
syncX();
|
|
|
|
#endif
|
|
|
|
|
2016-09-30 00:42:20 +00:00
|
|
|
#if defined(Q_WS_X11) && !defined(QT_NO_XRENDER)
|
2015-12-10 05:06:13 +02:00
|
|
|
if (!qt_widget_private(window())->isOpaque && window()->testAttribute(Qt::WA_TranslucentBackground)) {
|
2016-09-08 22:02:36 +00:00
|
|
|
QPainter p(d_ptr->image);
|
2015-12-10 05:06:13 +02:00
|
|
|
p.setCompositionMode(QPainter::CompositionMode_Source);
|
|
|
|
const QVector<QRect> rects = rgn.rects();
|
|
|
|
for (QVector<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it) {
|
2016-11-05 03:33:04 +00:00
|
|
|
p.fillRect(*it, Qt::transparent);
|
2015-12-10 05:06:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
Q_UNUSED(rgn);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void QRasterWindowSurface::flush(QWidget *widget, const QRegion &rgn, const QPoint &offset)
|
|
|
|
{
|
|
|
|
Q_D(QRasterWindowSurface);
|
|
|
|
|
|
|
|
// Not ready for painting yet, bail out. This can happen in
|
|
|
|
// QWidget::create_sys()
|
|
|
|
if (!d->image || rgn.rectCount() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
extern void *qt_getClipRects(const QRegion &r, int &num); // in qpaintengine_x11.cpp
|
|
|
|
QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft();
|
|
|
|
|
|
|
|
if (widget->window() != window()) {
|
|
|
|
XFreeGC(X11->display, d_ptr->gc);
|
|
|
|
d_ptr->gc = XCreateGC(X11->display, widget->handle(), 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion wrgn(rgn);
|
|
|
|
if (!wOffset.isNull())
|
|
|
|
wrgn.translate(-wOffset);
|
|
|
|
|
|
|
|
if (wrgn.rectCount() != 1) {
|
|
|
|
int num;
|
|
|
|
XRectangle *rects = (XRectangle *)qt_getClipRects(wrgn, num);
|
|
|
|
XSetClipRectangles(X11->display, d_ptr->gc, 0, 0, rects, num, YXBanded);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint widgetOffset = offset + wOffset;
|
2016-09-08 22:02:36 +00:00
|
|
|
QRect clipRect = widget->rect().translated(widgetOffset).intersected(d_ptr->image->rect());
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
QRect br = rgn.boundingRect().translated(offset).intersected(clipRect);
|
|
|
|
QPoint wpos = br.topLeft() - widgetOffset;
|
|
|
|
|
2016-09-08 22:02:36 +00:00
|
|
|
int depth = widget->x11Info().depth();
|
|
|
|
const QImage *src = d->image;
|
|
|
|
if (src->format() != QImage::Format_RGB32 || (depth != 24 && depth != 32) || X11->bppForDepth.value(depth) != 32) {
|
|
|
|
Q_ASSERT(src->depth() >= 16);
|
|
|
|
const QImage sub_src(src->scanLine(br.y()) + br.x() * (uint(src->depth()) / 8),
|
|
|
|
br.width(), br.height(), src->bytesPerLine(), src->format());
|
|
|
|
QX11PixmapData *data = new QX11PixmapData(QPixmapData::PixmapType);
|
|
|
|
data->xinfo = widget->x11Info();
|
|
|
|
data->fromImage(sub_src, Qt::NoOpaqueDetection);
|
|
|
|
QPixmap pm = QPixmap(data);
|
|
|
|
XCopyArea(X11->display, pm.handle(), widget->handle(), d_ptr->gc, 0 , 0 , br.width(), br.height(), wpos.x(), wpos.y());
|
|
|
|
} else {
|
|
|
|
// qpaintengine_x11.cpp
|
|
|
|
extern void qt_x11_drawImage(const QRect &rect, const QPoint &pos, const QImage *image, Drawable hd, GC gc, Display *dpy, Visual *visual, int depth);
|
|
|
|
qt_x11_drawImage(br, wpos, src, widget->handle(), d_ptr->gc, X11->display, (Visual *)widget->x11Info().visual(), depth);
|
2015-12-10 05:06:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (wrgn.rectCount() != 1)
|
|
|
|
XSetClipMask(X11->display, d_ptr->gc, XNone);
|
2016-08-06 13:21:14 +00:00
|
|
|
#endif // Q_WS_X11
|
2015-12-10 05:06:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void QRasterWindowSurface::setGeometry(const QRect &rect)
|
|
|
|
{
|
|
|
|
QWindowSurface::setGeometry(rect);
|
|
|
|
Q_D(QRasterWindowSurface);
|
|
|
|
d->inSetGeometry = true;
|
|
|
|
if (d->image == 0 || d->image->width() < rect.width() || d->image->height() < rect.height()) {
|
2016-09-30 00:42:20 +00:00
|
|
|
#if defined(Q_WS_X11) && !defined(QT_NO_XRENDER)
|
2015-12-10 05:06:13 +02:00
|
|
|
if (d_ptr->translucentBackground)
|
|
|
|
prepareBuffer(QImage::Format_ARGB32_Premultiplied, window());
|
|
|
|
else
|
|
|
|
#endif
|
2016-09-08 22:02:36 +00:00
|
|
|
prepareBuffer(QImage::systemFormat(), window());
|
2015-12-10 05:06:13 +02:00
|
|
|
}
|
|
|
|
d->inSetGeometry = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// from qwindowsurface.cpp
|
2016-09-08 22:02:36 +00:00
|
|
|
extern void qt_scrollRectInImage(const QImage *img, const QRect &rect, const QPoint &offset);
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
bool QRasterWindowSurface::scroll(const QRegion &area, int dx, int dy)
|
|
|
|
{
|
|
|
|
Q_D(QRasterWindowSurface);
|
|
|
|
|
2016-09-08 22:02:36 +00:00
|
|
|
if (!d->image || d->image->isNull())
|
2015-12-10 05:06:13 +02:00
|
|
|
return false;
|
|
|
|
|
2016-07-10 02:53:30 +03:00
|
|
|
#if defined(Q_WS_X11) && !defined(QT_NO_XSHM)
|
2015-12-10 05:06:13 +02:00
|
|
|
syncX();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const QVector<QRect> rects = area.rects();
|
|
|
|
for (int i = 0; i < rects.size(); ++i)
|
2016-09-08 22:02:36 +00:00
|
|
|
qt_scrollRectInImage(d->image, rects.at(i), QPoint(dx, dy));
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWindowSurface::WindowSurfaceFeatures QRasterWindowSurface::features() const
|
|
|
|
{
|
|
|
|
return QWindowSurface::AllFeatures;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QRasterWindowSurface::prepareBuffer(QImage::Format format, QWidget *widget)
|
|
|
|
{
|
|
|
|
Q_D(QRasterWindowSurface);
|
|
|
|
|
|
|
|
int width = window()->width();
|
|
|
|
int height = window()->height();
|
|
|
|
if (d->image) {
|
|
|
|
width = qMax(d->image->width(), width);
|
|
|
|
height = qMax(d->image->height(), height);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width == 0 || height == 0) {
|
|
|
|
delete d->image;
|
|
|
|
d->image = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-08 22:02:36 +00:00
|
|
|
QImage *oldImage = d->image;
|
2015-12-10 05:06:13 +02:00
|
|
|
|
2016-09-08 22:02:36 +00:00
|
|
|
d->image = new QImage(width, height, format);
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
if (oldImage && d->inSetGeometry && hasStaticContents()) {
|
2016-09-08 22:02:36 +00:00
|
|
|
const uchar *src = oldImage->bits();
|
|
|
|
uchar *dst = d->image->bits();
|
2015-12-10 05:06:13 +02:00
|
|
|
|
2016-09-08 22:02:36 +00:00
|
|
|
const int srcBytesPerLine = oldImage->bytesPerLine();
|
|
|
|
const int dstBytesPerLine = d->image->bytesPerLine();
|
|
|
|
const int bytesPerPixel = oldImage->depth() >> 3;
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
QRegion staticRegion(staticContents());
|
|
|
|
// Make sure we're inside the boundaries of the old image.
|
2016-09-08 22:02:36 +00:00
|
|
|
staticRegion &= QRect(0, 0, oldImage->width(), oldImage->height());
|
2015-12-10 05:06:13 +02:00
|
|
|
const QVector<QRect> &rects = staticRegion.rects();
|
|
|
|
const QRect *srcRect = rects.constData();
|
|
|
|
|
|
|
|
// Copy the static content of the old image into the new one.
|
|
|
|
int numRectsLeft = rects.size();
|
|
|
|
do {
|
|
|
|
const int bytesOffset = srcRect->x() * bytesPerPixel;
|
|
|
|
const int dy = srcRect->y();
|
|
|
|
|
|
|
|
// Adjust src and dst to point to the right offset.
|
|
|
|
const uchar *s = src + dy * srcBytesPerLine + bytesOffset;
|
|
|
|
uchar *d = dst + dy * dstBytesPerLine + bytesOffset;
|
|
|
|
const int numBytes = srcRect->width() * bytesPerPixel;
|
|
|
|
|
|
|
|
int numScanLinesLeft = srcRect->height();
|
|
|
|
do {
|
|
|
|
::memcpy(d, s, numBytes);
|
|
|
|
d += dstBytesPerLine;
|
|
|
|
s += srcBytesPerLine;
|
|
|
|
} while (--numScanLinesLeft);
|
|
|
|
|
|
|
|
++srcRect;
|
|
|
|
} while (--numRectsLeft);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete oldImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|