Warning: That file was not part of the compilation database. It may have many parsing errors.

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
50QT_BEGIN_NAMESPACE
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
58bool 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.
115 qt_glformat_from_eglconfig(format, ctx->config());
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
154bool QGLPixelBufferPrivate::cleanup()
155{
156 // No need to destroy "pbuf" here - it is done in QGLContext::reset().
157 return true;
158}
159
160bool QGLPixelBuffer::bindToDynamicTexture(GLuint texture_id)
161{
162#if QGL_RENDER_TEXTURE
163 Q_D(QGLPixelBuffer);
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
174void QGLPixelBuffer::releaseFromDynamicTexture()
175{
176#if QGL_RENDER_TEXTURE
177 Q_D(QGLPixelBuffer);
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
185GLuint QGLPixelBuffer::generateDynamicTexture() const
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
204bool QGLPixelBuffer::hasOpenGLPbuffers()
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;
211 qt_eglproperties_set_glformat(configProps, QGLFormat::defaultFormat());
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
224QT_END_NAMESPACE
225

Warning: That file was not part of the compilation database. It may have many parsing errors.