author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
child 7 | f7bc934e204c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 |
** All rights reserved. |
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtOpenGL module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
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 |
||
50 |
#ifdef QT_OPENGL_ES_1_CL |
|
51 |
#include "qgl_cl_p.h" |
|
52 |
#endif |
|
53 |
||
54 |
QT_BEGIN_NAMESPACE |
|
55 |
||
56 |
#ifdef EGL_BIND_TO_TEXTURE_RGBA |
|
57 |
#define QGL_RENDER_TEXTURE 1 |
|
58 |
#else |
|
59 |
#define QGL_RENDER_TEXTURE 0 |
|
60 |
#endif |
|
61 |
||
62 |
bool QGLPixelBufferPrivate::init(const QSize &size, const QGLFormat &f, QGLWidget *shareWidget) |
|
63 |
{ |
|
64 |
// Create the EGL context. |
|
65 |
ctx = new QEglContext(); |
|
66 |
ctx->setApi(QEgl::OpenGL); |
|
67 |
||
68 |
// Open the EGL display. |
|
69 |
if (!ctx->openDisplay(0)) { |
|
70 |
delete ctx; |
|
71 |
ctx = 0; |
|
72 |
return false; |
|
73 |
} |
|
74 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
75 |
// Find the shared context. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
76 |
QEglContext *shareContext = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
77 |
if (shareWidget && shareWidget->d_func()->glcx) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
78 |
shareContext = shareWidget->d_func()->glcx->d_func()->eglContext; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
79 |
|
0 | 80 |
// Choose an appropriate configuration. We use the best format |
81 |
// we can find, even if it is greater than the requested format. |
|
82 |
// We try for a pbuffer that is capable of texture rendering if possible. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
textureFormat = EGL_NONE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
if (shareContext) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
// Use the same configuration as the widget we are sharing with. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
86 |
ctx->setConfig(shareContext->config()); |
0 | 87 |
#if QGL_RENDER_TEXTURE |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
EGLint value = EGL_FALSE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
if (ctx->configAttrib(EGL_BIND_TO_TEXTURE_RGBA, &value) && value) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
90 |
textureFormat = EGL_TEXTURE_RGBA; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
91 |
else if (ctx->configAttrib(EGL_BIND_TO_TEXTURE_RGB, &value) && value) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
92 |
textureFormat = EGL_TEXTURE_RGB; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
93 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
94 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
95 |
QEglProperties configProps; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
96 |
qt_egl_set_format(configProps, QInternal::Pbuffer, f); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
97 |
configProps.setRenderableType(ctx->api()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
98 |
bool ok = false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
99 |
#if QGL_RENDER_TEXTURE |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
100 |
textureFormat = EGL_TEXTURE_RGBA; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
101 |
configProps.setValue(EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE); |
0 | 102 |
ok = ctx->chooseConfig(configProps, QEgl::BestPixelFormat); |
103 |
if (!ok) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
104 |
// Try again with RGB texture rendering. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
105 |
textureFormat = EGL_TEXTURE_RGB; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
106 |
configProps.removeValue(EGL_BIND_TO_TEXTURE_RGBA); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
107 |
configProps.setValue(EGL_BIND_TO_TEXTURE_RGB, EGL_TRUE); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
108 |
ok = ctx->chooseConfig(configProps, QEgl::BestPixelFormat); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
109 |
if (!ok) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
110 |
// One last try for a pbuffer with no texture rendering. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
111 |
configProps.removeValue(EGL_BIND_TO_TEXTURE_RGB); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
112 |
textureFormat = EGL_NONE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
113 |
} |
0 | 114 |
} |
115 |
#endif |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
116 |
if (!ok) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
117 |
if (!ctx->chooseConfig(configProps, QEgl::BestPixelFormat)) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
118 |
delete ctx; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
119 |
ctx = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
120 |
return false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
121 |
} |
0 | 122 |
} |
123 |
} |
|
124 |
||
125 |
// Retrieve the actual format properties. |
|
126 |
qt_egl_update_format(*ctx, format); |
|
127 |
||
128 |
// Create the attributes needed for the pbuffer. |
|
129 |
QEglProperties attribs; |
|
130 |
attribs.setValue(EGL_WIDTH, size.width()); |
|
131 |
attribs.setValue(EGL_HEIGHT, size.height()); |
|
132 |
#if QGL_RENDER_TEXTURE |
|
133 |
if (textureFormat != EGL_NONE) { |
|
134 |
attribs.setValue(EGL_TEXTURE_FORMAT, textureFormat); |
|
135 |
attribs.setValue(EGL_TEXTURE_TARGET, EGL_TEXTURE_2D); |
|
136 |
} |
|
137 |
#endif |
|
138 |
||
139 |
// Create the pbuffer surface. |
|
140 |
pbuf = eglCreatePbufferSurface(ctx->display(), ctx->config(), attribs.properties()); |
|
141 |
#if QGL_RENDER_TEXTURE |
|
142 |
if (pbuf == EGL_NO_SURFACE && textureFormat != EGL_NONE) { |
|
143 |
// Try again with texture rendering disabled. |
|
144 |
textureFormat = EGL_NONE; |
|
145 |
attribs.removeValue(EGL_TEXTURE_FORMAT); |
|
146 |
attribs.removeValue(EGL_TEXTURE_TARGET); |
|
147 |
pbuf = eglCreatePbufferSurface(ctx->display(), ctx->config(), attribs.properties()); |
|
148 |
} |
|
149 |
#endif |
|
150 |
if (pbuf == EGL_NO_SURFACE) { |
|
151 |
qWarning() << "QGLPixelBufferPrivate::init(): Unable to create EGL pbuffer surface:" << QEglContext::errorString(eglGetError()); |
|
152 |
return false; |
|
153 |
} |
|
154 |
||
155 |
// Create a new context for the configuration. |
|
156 |
if (!ctx->createContext(shareContext)) { |
|
157 |
delete ctx; |
|
158 |
ctx = 0; |
|
159 |
return false; |
|
160 |
} |
|
161 |
||
162 |
return true; |
|
163 |
} |
|
164 |
||
165 |
bool QGLPixelBufferPrivate::cleanup() |
|
166 |
{ |
|
167 |
// No need to destroy "pbuf" here - it is done in QGLContext::reset(). |
|
168 |
return true; |
|
169 |
} |
|
170 |
||
171 |
bool QGLPixelBuffer::bindToDynamicTexture(GLuint texture_id) |
|
172 |
{ |
|
173 |
#if QGL_RENDER_TEXTURE |
|
174 |
Q_D(QGLPixelBuffer); |
|
175 |
if (d->invalid || d->textureFormat == EGL_NONE || !d->ctx) |
|
176 |
return false; |
|
177 |
glBindTexture(GL_TEXTURE_2D, texture_id); |
|
178 |
return eglBindTexImage(d->ctx->display(), d->pbuf, EGL_BACK_BUFFER); |
|
179 |
#else |
|
180 |
Q_UNUSED(texture_id); |
|
181 |
return false; |
|
182 |
#endif |
|
183 |
} |
|
184 |
||
185 |
void QGLPixelBuffer::releaseFromDynamicTexture() |
|
186 |
{ |
|
187 |
#if QGL_RENDER_TEXTURE |
|
188 |
Q_D(QGLPixelBuffer); |
|
189 |
if (d->invalid || d->textureFormat == EGL_NONE || !d->ctx) |
|
190 |
return; |
|
191 |
eglReleaseTexImage(d->ctx->display(), d->pbuf, EGL_BACK_BUFFER); |
|
192 |
#endif |
|
193 |
} |
|
194 |
||
195 |
||
196 |
GLuint QGLPixelBuffer::generateDynamicTexture() const |
|
197 |
{ |
|
198 |
#if QGL_RENDER_TEXTURE |
|
199 |
Q_D(const QGLPixelBuffer); |
|
200 |
GLuint texture; |
|
201 |
glGenTextures(1, &texture); |
|
202 |
glBindTexture(GL_TEXTURE_2D, texture); |
|
203 |
if (d->textureFormat == EGL_TEXTURE_RGB) |
|
204 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, d->req_size.width(), d->req_size.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, 0); |
|
205 |
else |
|
206 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, d->req_size.width(), d->req_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); |
|
207 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
208 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
209 |
return texture; |
|
210 |
#else |
|
211 |
return 0; |
|
212 |
#endif |
|
213 |
} |
|
214 |
||
215 |
bool QGLPixelBuffer::hasOpenGLPbuffers() |
|
216 |
{ |
|
217 |
// See if we have at least 1 configuration that matches the default format. |
|
218 |
EGLDisplay dpy = QEglContext::defaultDisplay(0); |
|
219 |
if (dpy == EGL_NO_DISPLAY) |
|
220 |
return false; |
|
221 |
QEglProperties configProps; |
|
222 |
qt_egl_set_format(configProps, QInternal::Pbuffer, QGLFormat::defaultFormat()); |
|
223 |
configProps.setRenderableType(QEgl::OpenGL); |
|
224 |
do { |
|
225 |
EGLConfig cfg = 0; |
|
226 |
EGLint matching = 0; |
|
227 |
if (eglChooseConfig(dpy, configProps.properties(), |
|
228 |
&cfg, 1, &matching) && matching > 0) |
|
229 |
return true; |
|
230 |
} while (configProps.reduceConfiguration()); |
|
231 |
return false; |
|
232 |
} |
|
233 |
||
234 |
QT_END_NAMESPACE |