Qt 4.8
qwindowsurface_x11gl.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 QtOpenGL 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 <QTime>
43 #include <QDebug>
44 
45 #include <private/qt_x11_p.h>
46 #include <private/qimagepixmapcleanuphooks_p.h>
47 
48 #include "qwindowsurface_x11gl_p.h"
49 #include "qpixmapdata_x11gl_p.h"
50 
52 
54  : QWindowSurface(window), m_windowGC(0), m_pixmapGC(0), m_window(window)
55 {
56 }
57 
59 {
60  if (m_windowGC)
61  XFree(m_windowGC);
62  if (m_pixmapGC)
63  XFree(m_pixmapGC);
64 }
65 
67 {
68  return &m_backBuffer;
69 }
70 
71 extern void *qt_getClipRects(const QRegion &r, int &num); // in qpaintengine_x11.cpp
72 
73 void QX11GLWindowSurface::flush(QWidget *widget, const QRegion &widgetRegion, const QPoint &offset)
74 {
75  // We don't need to know the widget which initiated the flush. Instead we just use the offset
76  // to translate the widgetRegion:
77  Q_UNUSED(widget);
78 
79  if (m_backBuffer.isNull()) {
80  qDebug("QX11GLWindowSurface::flush() - backBuffer is null, not flushing anything");
81  return;
82  }
83 
84  Q_ASSERT(window()->size() != m_backBuffer.size());
85 
86  // Wait for all GL rendering to the back buffer pixmap to complete before trying to
87  // copy it to the window. We do this by making sure the pixmap's context is current
88  // and then call eglWaitClient. The EGL 1.4 spec says eglWaitClient doesn't have to
89  // block, just that "All rendering calls...are guaranteed to be executed before native
90  // rendering calls". This makes it potentially less expensive than glFinish.
91  QGLContext* ctx = static_cast<QX11GLPixmapData*>(m_backBuffer.data_ptr().data())->context();
92  if (QGLContext::currentContext() != ctx && ctx && ctx->isValid())
93  ctx->makeCurrent();
94  eglWaitClient();
95 
96  if (m_windowGC == 0) {
97  XGCValues attribs;
98  attribs.graphics_exposures = False;
99  m_windowGC = XCreateGC(X11->display, m_window->handle(), GCGraphicsExposures, &attribs);
100  }
101 
102  int rectCount;
103  XRectangle *rects = (XRectangle *)qt_getClipRects(widgetRegion, rectCount);
104  if (rectCount <= 0)
105  return;
106 
107  XSetClipRectangles(X11->display, m_windowGC, 0, 0, rects, rectCount, YXBanded);
108 
109  QRect dirtyRect = widgetRegion.boundingRect().translated(-offset);
110  XCopyArea(X11->display, m_backBuffer.handle(), m_window->handle(), m_windowGC,
111  dirtyRect.x(), dirtyRect.y(), dirtyRect.width(), dirtyRect.height(),
112  dirtyRect.x(), dirtyRect.y());
113 
114  // Make sure the blit of the update from the back buffer to the window completes
115  // before allowing rendering to start again to the back buffer. Otherwise the GPU
116  // might start rendering to the back buffer again while the blit takes place.
117  eglWaitNative(EGL_CORE_NATIVE_ENGINE);
118 }
119 
121 {
122  if (rect.width() > m_backBuffer.size().width() || rect.height() > m_backBuffer.size().height()) {
124  QSize newSize = rect.size();
125  pd->resize(newSize.width(), newSize.height());
126  m_backBuffer = QPixmap(pd);
127  if (window()->testAttribute(Qt::WA_TranslucentBackground))
129  if (m_pixmapGC) {
130  XFreeGC(X11->display, m_pixmapGC);
131  m_pixmapGC = 0;
132  }
133  }
134 
136 }
137 
138 bool QX11GLWindowSurface::scroll(const QRegion &area, int dx, int dy)
139 {
140  if (m_backBuffer.isNull())
141  return false;
142 
144 
145  // Make sure all GL rendering is complete before starting the scroll operation:
146  QGLContext* ctx = static_cast<QX11GLPixmapData*>(m_backBuffer.data_ptr().data())->context();
147  if (QGLContext::currentContext() != ctx && ctx && ctx->isValid())
148  ctx->makeCurrent();
149  eglWaitClient();
150 
151  if (!m_pixmapGC)
152  m_pixmapGC = XCreateGC(X11->display, m_backBuffer.handle(), 0, 0);
153 
154  foreach (const QRect& rect, area.rects()) {
155  XCopyArea(X11->display, m_backBuffer.handle(), m_backBuffer.handle(), m_pixmapGC,
156  rect.x(), rect.y(), rect.width(), rect.height(),
157  rect.x()+dx, rect.y()+dy);
158  }
159 
160  // Make sure the scroll operation is complete before allowing GL rendering to resume
161  eglWaitNative(EGL_CORE_NATIVE_ENGINE);
162 
163  return true;
164 }
165 
166 
168 {
169  if (!widget || m_backBuffer.isNull())
170  return QPixmap();
171 
172  QRect srcRect;
173 
174  // make sure the rect is inside the widget & clip to widget's rect
175  if (!rect.isEmpty())
176  srcRect = rect & widget->rect();
177  else
178  srcRect = widget->rect();
179 
180  if (srcRect.isEmpty())
181  return QPixmap();
182 
183  // If it's a child widget we have to translate the coordinates
184  if (widget != window())
185  srcRect.translate(widget->mapTo(window(), QPoint(0, 0)));
186 
187  QPixmap::x11SetDefaultScreen(widget->x11Info().screen());
188 
190  pmd->resize(srcRect.width(), srcRect.height());
191  QPixmap px(pmd);
192 
193  GC tmpGc = XCreateGC(X11->display, m_backBuffer.handle(), 0, 0);
194 
195  // Make sure all GL rendering is complete before copying the window
196  QGLContext* ctx = static_cast<QX11GLPixmapData*>(m_backBuffer.pixmapData())->context();
197  if (QGLContext::currentContext() != ctx && ctx && ctx->isValid())
198  ctx->makeCurrent();
199  eglWaitClient();
200 
201  // Copy srcRect from the backing store to the new pixmap
202  XSetGraphicsExposures(X11->display, tmpGc, False);
203  XCopyArea(X11->display, m_backBuffer.handle(), px.handle(), tmpGc,
204  srcRect.x(), srcRect.y(), srcRect.width(), srcRect.height(), 0, 0);
205  XFreeGC(X11->display, tmpGc);
206 
207  // Wait until the copy has finised before allowing more rendering into the back buffer
208  eglWaitNative(EGL_CORE_NATIVE_ENGINE);
209 
210  return px;
211 }
212 
DataPtr & data_ptr()
Definition: qpixmap.h:297
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
QX11GLWindowSurface(QWidget *window)
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
QRect rect(const QWidget *widget) const
Returns the rectangle for widget in the coordinates of this window surface.
void flush(QWidget *widget, const QRegion &region, const QPoint &offset)
Flushes the given region from the specified widget onto the screen.
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.
QRect translated(int dx, int dy) const
Returns a copy of the rectangle that is translated dx along the x axis and dy along the y axis...
Definition: qrect.h:328
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
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
void setGeometry(const QRect &rect)
Sets the currently allocated area to be the given rect.
static const QGLContext * currentContext()
Returns the current context, i.e.
Definition: qgl.cpp:3545
Q_CORE_EXPORT void qDebug(const char *,...)
NSWindow * window
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
void * qt_getClipRects(const QRegion &r, int &num)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QGLContext class encapsulates an OpenGL rendering context.
Definition: qgl.h:310
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
ClassId classId() const
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...
The QWindowSurface class provides the drawing area for top-level windows.
void fill(const QColor &fillColor=Qt::white)
Fills the pixmap with the given color.
Definition: qpixmap.cpp:1080
bool isEmpty() const
Returns true if the rectangle is empty, otherwise returns false.
Definition: qrect.h:234
bool isValid() const
Returns true if a GL rendering context has been successfully created; otherwise returns false...
Definition: qgl.cpp:3477
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
int x() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:252
#define ctx
Definition: qgl.cpp:6094
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
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
QPaintDevice * paintDevice()
Implement this function to return the appropriate paint device.
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
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 QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
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
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QWidget * window() const
Returns a pointer to the top-level window associated with this surface.
int screen() const
Returns the number of the screen currently in use.
static int area(const QSize &s)
Definition: qicon.cpp:155
QPixmapData * pixmapData() const
Definition: qpixmap.cpp:2277