Qt 4.8
qglpixelbuffer_egl.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 <qdebug.h>
43 #include "qglpixelbuffer.h"
44 #include "qglpixelbuffer_p.h"
45 #include "qgl_egl_p.h"
46 
47 #include <qimage.h>
48 #include <private/qgl_p.h>
49 
51 
52 #ifdef EGL_BIND_TO_TEXTURE_RGBA
53 #define QGL_RENDER_TEXTURE 1
54 #else
55 #define QGL_RENDER_TEXTURE 0
56 #endif
57 
58 bool QGLPixelBufferPrivate::init(const QSize &size, const QGLFormat &f, QGLWidget *shareWidget)
59 {
60  // Create the EGL context.
61  ctx = new QEglContext();
62  ctx->setApi(QEgl::OpenGL);
63 
64  // Find the shared context.
65  QEglContext *shareContext = 0;
66  if (shareWidget && shareWidget->d_func()->glcx)
67  shareContext = shareWidget->d_func()->glcx->d_func()->eglContext;
68 
69  // Choose an appropriate configuration. We use the best format
70  // we can find, even if it is greater than the requested format.
71  // We try for a pbuffer that is capable of texture rendering if possible.
72  textureFormat = EGL_NONE;
73  if (shareContext) {
74  // Use the same configuration as the widget we are sharing with.
75  ctx->setConfig(shareContext->config());
76 #if QGL_RENDER_TEXTURE
77  if (ctx->configAttrib(EGL_BIND_TO_TEXTURE_RGBA) == EGL_TRUE)
78  textureFormat = EGL_TEXTURE_RGBA;
79  else if (ctx->configAttrib(EGL_BIND_TO_TEXTURE_RGB) == EGL_TRUE)
80  textureFormat = EGL_TEXTURE_RGB;
81 #endif
82  } else {
83  QEglProperties configProps;
84  qt_eglproperties_set_glformat(configProps, f);
85  configProps.setDeviceType(QInternal::Pbuffer);
86  configProps.setRenderableType(ctx->api());
87  bool ok = false;
88 #if QGL_RENDER_TEXTURE
89  textureFormat = EGL_TEXTURE_RGBA;
90  configProps.setValue(EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE);
91  ok = ctx->chooseConfig(configProps, QEgl::BestPixelFormat);
92  if (!ok) {
93  // Try again with RGB texture rendering.
94  textureFormat = EGL_TEXTURE_RGB;
95  configProps.removeValue(EGL_BIND_TO_TEXTURE_RGBA);
96  configProps.setValue(EGL_BIND_TO_TEXTURE_RGB, EGL_TRUE);
97  ok = ctx->chooseConfig(configProps, QEgl::BestPixelFormat);
98  if (!ok) {
99  // One last try for a pbuffer with no texture rendering.
100  configProps.removeValue(EGL_BIND_TO_TEXTURE_RGB);
101  textureFormat = EGL_NONE;
102  }
103  }
104 #endif
105  if (!ok) {
106  if (!ctx->chooseConfig(configProps, QEgl::BestPixelFormat)) {
107  delete ctx;
108  ctx = 0;
109  return false;
110  }
111  }
112  }
113 
114  // Retrieve the actual format properties.
116 
117  // Create the attributes needed for the pbuffer.
118  QEglProperties attribs;
119  attribs.setValue(EGL_WIDTH, size.width());
120  attribs.setValue(EGL_HEIGHT, size.height());
121 #if QGL_RENDER_TEXTURE
122  if (textureFormat != EGL_NONE) {
123  attribs.setValue(EGL_TEXTURE_FORMAT, textureFormat);
124  attribs.setValue(EGL_TEXTURE_TARGET, EGL_TEXTURE_2D);
125  }
126 #endif
127 
128  // Create the pbuffer surface.
129  pbuf = eglCreatePbufferSurface(ctx->display(), ctx->config(), attribs.properties());
130 #if QGL_RENDER_TEXTURE
131  if (pbuf == EGL_NO_SURFACE && textureFormat != EGL_NONE) {
132  // Try again with texture rendering disabled.
133  textureFormat = EGL_NONE;
134  attribs.removeValue(EGL_TEXTURE_FORMAT);
135  attribs.removeValue(EGL_TEXTURE_TARGET);
136  pbuf = eglCreatePbufferSurface(ctx->display(), ctx->config(), attribs.properties());
137  }
138 #endif
139  if (pbuf == EGL_NO_SURFACE) {
140  qWarning() << "QGLPixelBufferPrivate::init(): Unable to create EGL pbuffer surface:" << QEgl::errorString();
141  return false;
142  }
143 
144  // Create a new context for the configuration.
145  if (!ctx->createContext(shareContext)) {
146  delete ctx;
147  ctx = 0;
148  return false;
149  }
150 
151  return true;
152 }
153 
155 {
156  // No need to destroy "pbuf" here - it is done in QGLContext::reset().
157  return true;
158 }
159 
161 {
162 #if QGL_RENDER_TEXTURE
164  if (d->invalid || d->textureFormat == EGL_NONE || !d->ctx)
165  return false;
166  glBindTexture(GL_TEXTURE_2D, texture_id);
167  return eglBindTexImage(d->ctx->display(), d->pbuf, EGL_BACK_BUFFER);
168 #else
169  Q_UNUSED(texture_id);
170  return false;
171 #endif
172 }
173 
175 {
176 #if QGL_RENDER_TEXTURE
178  if (d->invalid || d->textureFormat == EGL_NONE || !d->ctx)
179  return;
180  eglReleaseTexImage(d->ctx->display(), d->pbuf, EGL_BACK_BUFFER);
181 #endif
182 }
183 
184 
186 {
187 #if QGL_RENDER_TEXTURE
188  Q_D(const QGLPixelBuffer);
189  GLuint texture;
190  glGenTextures(1, &texture);
191  glBindTexture(GL_TEXTURE_2D, texture);
192  if (d->textureFormat == EGL_TEXTURE_RGB)
193  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, d->req_size.width(), d->req_size.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
194  else
195  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, d->req_size.width(), d->req_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
196  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
197  glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
198  return texture;
199 #else
200  return 0;
201 #endif
202 }
203 
205 {
206  // See if we have at least 1 configuration that matches the default format.
207  EGLDisplay dpy = QEgl::display();
208  if (dpy == EGL_NO_DISPLAY)
209  return false;
210  QEglProperties configProps;
212  configProps.setDeviceType(QInternal::Pbuffer);
213  configProps.setRenderableType(QEgl::OpenGL);
214  do {
215  EGLConfig cfg = 0;
216  EGLint matching = 0;
217  if (eglChooseConfig(dpy, configProps.properties(),
218  &cfg, 1, &matching) && matching > 0)
219  return true;
220  } while (configProps.reduceConfiguration());
221  return false;
222 }
223 
void qt_glformat_from_eglconfig(QGLFormat &format, const EGLConfig config)
Definition: qgl_egl.cpp:145
double d
Definition: qnumeric_p.h:62
#define GL_RGB
#define GL_TEXTURE_MIN_FILTER
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool removeValue(int name)
const EGLint * properties() const
static bool hasOpenGLPbuffers()
Returns true if the OpenGL pbuffer extension is present on this system; otherwise returns false...
void setValue(int name, int value)
#define Q_D(Class)
Definition: qglobal.h:2482
#define GL_TEXTURE_2D
EGLConfig config() const
Definition: qeglcontext_p.h:96
Q_GUI_EXPORT QString errorString(EGLint code=eglGetError())
Definition: qegl.cpp:743
int width() const
Returns the width.
Definition: qsize.h:126
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
void setRenderableType(QEgl::API api)
The QGLFormat class specifies the display format of an OpenGL rendering context.
Definition: qgl.h:175
Q_CORE_EXPORT void qWarning(const char *,...)
The QGLPixelBuffer class encapsulates an OpenGL pbuffer.
bool reduceConfiguration()
bool bindToDynamicTexture(GLuint texture)
Generates and binds a 2D GL texture that is the same size as the pbuffer, and returns the texture&#39;s I...
#define GL_LINEAR
The QGLWidget class is a widget for rendering OpenGL graphics.
Definition: qgl.h:474
#define GL_UNSIGNED_BYTE
static QGLFormat defaultFormat()
Definition: qgl.cpp:1518
int height() const
Returns the height.
Definition: qsize.h:129
bool init(const QSize &size, const QGLFormat &f, QGLWidget *shareWidget)
#define GL_RGBA
GLuint generateDynamicTexture() const
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
#define GL_TEXTURE_MAG_FILTER
void setDeviceType(int devType)
void qt_eglproperties_set_glformat(QEglProperties &eglProperties, const QGLFormat &glFormat)
Definition: qgl_egl.cpp:61
#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
void releaseFromDynamicTexture()
Releases the pbuffer from any previously bound texture.