Qt 4.8
qwaylandreadbackglxcontext.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 plugins 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 
43 
44 #include "qwaylandshmsurface.h"
46 
47 #include <QtCore/QDebug>
48 
49 static inline void qgl_byteSwapImage(QImage &img, GLenum pixel_type)
50 {
51  const int width = img.width();
52  const int height = img.height();
53 
54  if (pixel_type == GL_UNSIGNED_INT_8_8_8_8_REV
55  || (pixel_type == GL_UNSIGNED_BYTE && QSysInfo::ByteOrder == QSysInfo::LittleEndian))
56  {
57  for (int i = 0; i < height; ++i) {
58  uint *p = (uint *) img.scanLine(i);
59  for (int x = 0; x < width; ++x)
60  p[x] = ((p[x] << 16) & 0xff0000) | ((p[x] >> 16) & 0xff) | (p[x] & 0xff00ff00);
61  }
62  } else {
63  for (int i = 0; i < height; ++i) {
64  uint *p = (uint *) img.scanLine(i);
65  for (int x = 0; x < width; ++x)
66  p[x] = (p[x] << 8) | ((p[x] >> 24) & 0xff);
67  }
68  }
69 }
70 
73  , mGlxIntegration(glxIntegration)
74  , mWindow(window)
75  , mBuffer(0)
76  , mPixmap(0)
77  , mConfig(qglx_findConfig(glxIntegration->xDisplay(),glxIntegration->screen(),window->widget()->platformWindowFormat(),GLX_PIXMAP_BIT))
78  , mGlxPixmap(0)
79 {
80  XVisualInfo *visualInfo = glXGetVisualFromFBConfig(glxIntegration->xDisplay(),mConfig);
81  mContext = glXCreateContext(glxIntegration->xDisplay(),visualInfo,0,TRUE);
82 
84 }
85 
87 {
89 
90  glXMakeCurrent(mGlxIntegration->xDisplay(),mGlxPixmap,mContext);
91 }
92 
94 {
96 }
97 
99 {
100  if (QPlatformGLContext::currentContext() != this) {
101  makeCurrent();
102  }
103 
104  QSize size = mWindow->geometry().size();
105 
106  QImage img(size,QImage::Format_ARGB32);
107  const uchar *constBits = img.bits();
108  void *pixels = const_cast<uchar *>(constBits);
109 
110  glReadPixels(0,0, size.width(), size.height(), GL_RGBA,GL_UNSIGNED_BYTE, pixels);
111 
112  img = img.mirrored();
114  constBits = img.bits();
115 
116  const uchar *constDstBits = mBuffer->image()->bits();
117  uchar *dstBits = const_cast<uchar *>(constDstBits);
118  memcpy(dstBits,constBits,(img.width()*4) * img.height());
119 
120 
121  mWindow->damage(QRegion(QRect(QPoint(0,0),size)));
123 
124 }
125 
127 {
128  return (void *) glXGetProcAddress(reinterpret_cast<GLubyte *>(procName.toLatin1().data()));
129 }
130 
132 {
134 }
135 
137 {
138  QSize size(mWindow->geometry().size());
139  if (size.isEmpty()) {
140  //QGLWidget wants a context for a window without geometry
141  size = QSize(1,1);
142  }
143 
145 
146  delete mBuffer;
147  //XFreePixmap deletes the glxPixmap as well
148  if (mPixmap) {
149  XFreePixmap(mGlxIntegration->xDisplay(),mPixmap);
150  }
151 
153  mWindow->attach(mBuffer);
154  int depth = XDefaultDepth(mGlxIntegration->xDisplay(),mGlxIntegration->screen());
155  mPixmap = XCreatePixmap(mGlxIntegration->xDisplay(),mGlxIntegration->rootWindow(),size.width(),size.height(),depth);
156  XSync(mGlxIntegration->xDisplay(),False);
157 
158  mGlxPixmap = glXCreatePixmap(mGlxIntegration->xDisplay(),mConfig,mPixmap,0);
159 
160  if (!mGlxPixmap) {
161  qDebug() << "Could not make egl surface out of pixmap :(";
162  }
163 }
static const QPlatformGLContext * currentContext()
Returns the last context which called makeCurrent.
QPointer< QWidget > widget
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QPlatformWindowFormat qglx_platformWindowFromGLXFBConfig(Display *display, GLXFBConfig config, GLXContext ctx)
virtual QRect geometry() const
Returnes the current geometry of a window.
#define GL_UNSIGNED_INT_8_8_8_8_REV
virtual void doneCurrent()
Reimplement in subclass to release current context.
The QPlatformWindowFormat class specifies the display format of an OpenGL rendering context and if po...
The QString class provides a Unicode character string.
Definition: qstring.h:83
Q_CORE_EXPORT void qDebug(const char *,...)
unsigned char uchar
Definition: qglobal.h:994
NSWindow * window
int width() const
Returns the width.
Definition: qsize.h:126
QWaylandReadbackGlxIntegration * mGlxIntegration
void swapBuffers()
Reimplement in subclass to native swap buffers calls.
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
unsigned int uint
Definition: qglobal.h:996
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
virtual void makeCurrent()
Reimplement in subclass to do makeCurrent on native GL context.
QImage mirrored(bool horizontally=false, bool vertically=true) const
Returns a mirror of the image, mirrored in the horizontal and/or the vertical direction depending on ...
Definition: qimage.cpp:4922
void attach(QWaylandBuffer *buffer)
QPlatformWindowFormat platformWindowFormat() const
QWidget has the function qplatformWindowFormat().
GLXFBConfig qglx_findConfig(Display *display, int screen, const QPlatformWindowFormat &format, int drawableBit)
void * getProcAddress(const QString &procName)
Reimplement in subclass to native getProcAddr calls.
#define TRUE
Synonym for true.
Definition: qglobal.h:1018
void makeCurrent()
Reimplement in subclass to do makeCurrent on native GL context.
uchar * bits()
Returns a pointer to the first pixel data.
Definition: qimage.cpp:1946
int width() const
Returns the width of the image.
Definition: qimage.cpp:1557
The QPlatformGLContext class provides an abstraction for native GL contexts.
QWaylandReadbackGlxContext(QWaylandReadbackGlxIntegration *glxIntegration, QWaylandReadbackGlxWindow *window)
unsigned int GLenum
Definition: main.cpp:50
#define GL_UNSIGNED_BYTE
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
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 height() const
Returns the height of the image.
Definition: qimage.cpp:1572
void doneCurrent()
Reimplement in subclass to release current context.
#define GL_RGBA
QWaylandReadbackGlxWindow * mWindow
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
void damage(const QRect &rect)
static void qgl_byteSwapImage(QImage &img, GLenum pixel_type)
uchar * scanLine(int)
Returns a pointer to the pixel data at the scanline with index i.
Definition: qimage.cpp:1886