1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtOpenGL module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
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 |
|
---|
50 | QT_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 |
|
---|
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.
|
---|
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 |
|
---|
154 | bool QGLPixelBufferPrivate::cleanup()
|
---|
155 | {
|
---|
156 | // No need to destroy "pbuf" here - it is done in QGLContext::reset().
|
---|
157 | return true;
|
---|
158 | }
|
---|
159 |
|
---|
160 | bool 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 |
|
---|
174 | void 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 |
|
---|
185 | GLuint 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 |
|
---|
204 | bool 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 |
|
---|
224 | QT_END_NAMESPACE
|
---|