Qt 4.8
qwindowsurface_x11.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <QtGui/QPaintDevice>
43 #include <QtGui/QPainter>
44 #include <QtGui/QPixmap>
45 #include <QtGui/QWidget>
46 
47 #include "private/qt_x11_p.h"
48 #include "private/qpixmap_x11_p.h"
49 #include "private/qwidget_p.h"
50 #include "qx11info_x11.h"
51 #include "qwindowsurface_x11_p.h"
52 
54 
55 extern void *qt_getClipRects(const QRegion &r, int &num); // in qpaintengine_x11.cpp
56 
58 {
61 #ifndef QT_NO_XRENDER
63 #endif
64 };
65 
67  : QWindowSurface(widget), d_ptr(new QX11WindowSurfacePrivate), gc(0)
68 {
69  d_ptr->widget = widget;
70 #ifndef QT_NO_XRENDER
71  d_ptr->translucentBackground = X11->use_xrender
72  && widget->x11Info().depth() == 32;
73 #endif
74 }
75 
76 
78 {
79  delete d_ptr;
80  if (gc) {
81  XFreeGC(X11->display, gc);
82  gc = 0;
83  }
84 }
85 
87 {
88  return &d_ptr->device;
89 }
90 
92 {
93 #ifndef QT_NO_XRENDER
95 
97  if (d_ptr->device.depth() != 32)
98  static_cast<QX11PixmapData *>(d_ptr->device.data_ptr().data())->convertToARGB32();
99  ::Picture src = X11->getSolidFill(d_ptr->device.x11Info().screen(), Qt::transparent);
100  ::Picture dst = d_ptr->device.x11PictureHandle();
101  const QVector<QRect> rects = rgn.rects();
102  const int w = d_ptr->device.width();
103  const int h = d_ptr->device.height();
104  for (QVector<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it)
105  XRenderComposite(X11->display, PictOpSrc, src, 0, dst,
106  0, 0, w, h, it->x(), it->y(),
107  it->width(), it->height());
108  }
109 #endif
110 }
111 
113 {
114  if (d_ptr->device.isNull())
115  return;
116 
117  QPoint wOffset = qt_qwidget_data(widget)->wrect.topLeft();
118  QRegion wrgn(rgn);
119  QRect br = rgn.boundingRect();
120  if (!wOffset.isNull())
121  wrgn.translate(-wOffset);
122  QRect wbr = wrgn.boundingRect();
123 
124  int num;
125  XRectangle *rects = (XRectangle *)qt_getClipRects(wrgn, num);
126  if (num <= 0)
127  return;
128 // qDebug() << "XSetClipRectangles";
129 // for (int i = 0; i < num; ++i)
130 // qDebug() << ' ' << i << rects[i].x << rects[i].x << rects[i].y << rects[i].width << rects[i].height;
131  if (num != 1)
132  XSetClipRectangles(X11->display, gc, 0, 0, rects, num, YXBanded);
133  XCopyArea(X11->display, d_ptr->device.handle(), widget->handle(), gc,
134  br.x() + offset.x(), br.y() + offset.y(), br.width(), br.height(), wbr.x(), wbr.y());
135  if (num != 1)
136  XSetClipMask(X11->display, gc, XNone);
137 }
138 
140 {
142 
143  const QSize size = rect.size();
144 
145  if (d_ptr->device.size() == size || size.width() <= 0 || size.height() <= 0)
146  return;
147 #ifndef QT_NO_XRENDER
149  QPixmap::x11SetDefaultScreen(d_ptr->widget->x11Info().screen());
150 
152  data->xinfo = d_ptr->widget->x11Info();
153  data->resize(size.width(), size.height());
154  d_ptr->device = QPixmap(data);
155  } else
156 #endif
157  {
158  QPixmap::x11SetDefaultScreen(d_ptr->widget->x11Info().screen());
159 
160  QX11PixmapData *oldData = static_cast<QX11PixmapData *>(d_ptr->device.pixmapData());
161 
162  if (oldData && !(oldData->flags & QX11PixmapData::Uninitialized) && hasStaticContents()) {
163  // Copy the content of the old pixmap into the new one.
165  newData->resize(size.width(), size.height());
166  Q_ASSERT(oldData->d == newData->d);
167 
168  QRegion staticRegion(staticContents());
169  // Make sure we're inside the boundaries of the old pixmap.
170  staticRegion &= QRect(0, 0, oldData->w, oldData->h);
171  const QRect boundingRect(staticRegion.boundingRect());
172  const int dx = boundingRect.x();
173  const int dy = boundingRect.y();
174 
175  int num;
176  XRectangle *rects = (XRectangle *)qt_getClipRects(staticRegion, num);
177  GC tmpGc = XCreateGC(X11->display, oldData->hd, 0, 0);
178  XSetClipRectangles(X11->display, tmpGc, 0, 0, rects, num, YXBanded);
179  XCopyArea(X11->display, oldData->hd, newData->hd, tmpGc,
180  dx, dy, qMin(boundingRect.width(), size.width()),
181  qMin(boundingRect.height(), size.height()), dx, dy);
182  XFreeGC(X11->display, tmpGc);
184 
185  d_ptr->device = QPixmap(newData);
186  } else {
187  d_ptr->device = QPixmap(size);
188  }
189  }
190 
191  if (gc) {
192  XFreeGC(X11->display, gc);
193  gc = 0;
194  }
195  if (!d_ptr->device.isNull()) {
196  gc = XCreateGC(X11->display, d_ptr->device.handle(), 0, 0);
197  XSetGraphicsExposures(X11->display, gc, False);
198  }
199 }
200 
201 bool QX11WindowSurface::scroll(const QRegion &area, int dx, int dy)
202 {
203  QRect rect = area.boundingRect();
204 
205  if (d_ptr->device.isNull())
206  return false;
207 
208  GC gc = XCreateGC(X11->display, d_ptr->device.handle(), 0, 0);
209  XCopyArea(X11->display, d_ptr->device.handle(), d_ptr->device.handle(), gc,
210  rect.x(), rect.y(), rect.width(), rect.height(),
211  rect.x()+dx, rect.y()+dy);
212  XFreeGC(X11->display, gc);
213 
214  return true;
215 }
216 
218  const QRect& rect) const
219 {
220  if (!widget || d_ptr->device.isNull())
221  return QPixmap();
222 
223  QRect srcRect;
224 
225  // make sure the rect is inside the widget & clip to widget's rect
226  if (!rect.isEmpty())
227  srcRect = rect & widget->rect();
228  else
229  srcRect = widget->rect();
230 
231  if (srcRect.isEmpty())
232  return QPixmap();
233 
234  // If it's a child widget we have to translate the coordinates
235  if (widget != window())
236  srcRect.translate(widget->mapTo(window(), QPoint(0, 0)));
237 
238  QPixmap::x11SetDefaultScreen(widget->x11Info().screen());
239  QPixmap px(srcRect.width(), srcRect.height());
240 
241  GC tmpGc = XCreateGC(X11->display, d_ptr->device.handle(), 0, 0);
242 
243  // Copy srcRect from the backing store to the new pixmap
244  XSetGraphicsExposures(X11->display, tmpGc, False);
245  XCopyArea(X11->display, d_ptr->device.handle(), px.handle(), tmpGc,
246  srcRect.x(), srcRect.y(), srcRect.width(), srcRect.height(), 0, 0);
247 
248  XFreeGC(X11->display, tmpGc);
249 
250  return px;
251 }
252 
253 QWindowSurface::WindowSurfaceFeatures QX11WindowSurface::features() const
254 {
256 #ifndef QT_NO_XRENDER
258  features |= QWindowSurface::StaticContents;
259 #else
260  features |= QWindowSurface::StaticContents;
261 #endif
262  return features;
263 }
264 
int depth() const
Returns the color depth (bits per pixel) of the X display.
DataPtr & data_ptr()
Definition: qpixmap.h:297
QX11WindowSurface(QWidget *widget)
qreal y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:667
WindowSurfaceFeatures features() const
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void flush(QWidget *widget, const QRegion &region, const QPoint &offset)
Flushes the given region from the specified widget onto the screen.
int width() const
Returns the width of the pixmap.
Definition: qpixmap.cpp:630
QPointer< QWidget > widget
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
QPoint mapTo(QWidget *, const QPoint &) const
Translates the widget coordinate pos to the coordinate system of parent.
Definition: qwidget.cpp:4409
void * qt_getClipRects(const QRegion &r, int &num)
QRect rect(const QWidget *widget) const
Returns the rectangle for widget in the coordinates of this window surface.
#define it(className, varName)
Qt::HANDLE hd
void setGeometry(const QRect &rect)
Sets the currently allocated area to be the given rect.
QPixmap grabWidget(const QWidget *widget, const QRect &rectangle=QRect()) const
Returns a QPixmap generated from the part of the backing store corresponding to widget.
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
const QX11Info & x11Info() const
Returns information about the configuration of the X display used to display the widget.
virtual void setGeometry(const QRect &rect)
Sets the currently allocated area to be the given rect.
int width() const
Returns the width of the rectangle.
Definition: qrect.h:303
virtual QPoint offset(const QWidget *widget) const
Returns the offset of widget in the coordinates of this window surface.
Qt::HANDLE handle() const
Returns the pixmap&#39;s handle to the device context.
Definition: qpixmap.cpp:1299
QRect boundingRect() const
Returns the bounding rectangle of this region.
Definition: qregion.cpp:4363
int depth() const
Returns the depth of the pixmap.
Definition: qpixmap.cpp:695
void resize(int width, int height)
int height() const
Returns the height of the rectangle.
Definition: qrect.h:306
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define X11
Definition: qt_x11_p.h:724
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the vector...
Definition: qvector.h:250
static const QRectF boundingRect(const QPointF *points, int pointCount)
int width() const
Returns the width.
Definition: qsize.h:126
T * data() const
Returns a pointer to the shared data object.
Definition: qshareddata.h:145
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool hasStaticContents() const
qreal height() const
Returns the height of the rectangle.
Definition: qrect.h:710
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
bool isNull() const
Returns true if both the x and y coordinates are set to 0, otherwise returns false.
Definition: qpoint.h:125
static const char * data(const QByteArray &arr)
qreal width() const
Returns the width of the rectangle.
Definition: qrect.h:707
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
QRect wrect
Definition: qwidget.h:145
QX11WindowSurfacePrivate * d_ptr
The QWindowSurface class provides the drawing area for top-level windows.
QRegion staticContents() const
bool isEmpty() const
Returns true if the rectangle is empty, otherwise returns false.
Definition: qrect.h:234
unsigned long Picture
iterator begin()
Returns an STL-style iterator pointing to the first item in the vector.
Definition: qvector.h:247
QRect rect
the internal geometry of the widget excluding any window frame
Definition: qwidget.h:168
int y() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:255
qreal x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:664
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
QVector< QRect > rects() const
Returns an array of non-overlapping rectangles that make up the region.
Definition: qregion.cpp:4412
QPaintDevice * paintDevice()
Implement this function to return the appropriate paint device.
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
struct _XGC * GC
Definition: qwindowdefs.h:117
int height() const
Returns the height of the pixmap.
Definition: qpixmap.cpp:645
void translate(int dx, int dy)
Translates (moves) the region dx along the X axis and dy along the Y axis.
Definition: qregion.cpp:4116
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615
void translate(int dx, int dy)
Moves the rectangle dx along the x axis and dy along the y axis, relative to the current position...
Definition: qrect.h:312
Q_GUI_EXPORT QWidgetData * qt_qwidget_data(QWidget *widget)
Definition: qwidget.cpp:12915
void beginPaint(const QRegion &rgn)
This function is called before painting onto the surface begins, with the region in which the paintin...
QWidget * window() const
Returns a pointer to the top-level window associated with this surface.
bool scroll(const QRegion &area, int dx, int dy)
Scrolls the given area dx pixels to the right and dy downward; both dx and dy may be negative...
int screen() const
Returns the number of the screen currently in use.
static int area(const QSize &s)
Definition: qicon.cpp:155
QPoint topLeft() const
Returns the position of the rectangle&#39;s top-left corner.
Definition: qrect.h:288
QPixmapData * pixmapData() const
Definition: qpixmap.cpp:2277