author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
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 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
64 |
// Find the shared context. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
QEglContext *shareContext = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
66 |
if (shareWidget && shareWidget->d_func()->glcx) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
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
|
68 |
|
0 | 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. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
72 |
textureFormat = EGL_NONE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
73 |
if (shareContext) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
74 |
// 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
|
75 |
ctx->setConfig(shareContext->config()); |
0 | 76 |
#if QGL_RENDER_TEXTURE |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
77 |
EGLint value = EGL_FALSE; |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
78 |
if (ctx->configAttrib(EGL_BIND_TO_TEXTURE_RGBA) == EGL_TRUE) |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
79 |
textureFormat = EGL_TEXTURE_RGBA; |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
80 |
else if (ctx->configAttrib(EGL_BIND_TO_TEXTURE_RGB) == EGL_TRUE) |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
81 |
textureFormat = EGL_TEXTURE_RGB; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
QEglProperties configProps; |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
85 |
qt_eglproperties_set_glformat(configProps, f); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
86 |
configProps.setDeviceType(QInternal::Pbuffer); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
87 |
configProps.setRenderableType(ctx->api()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
bool ok = false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
#if QGL_RENDER_TEXTURE |
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 |
configProps.setValue(EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE); |
0 | 92 |
ok = ctx->chooseConfig(configProps, QEgl::BestPixelFormat); |
93 |
if (!ok) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
94 |
// Try again with RGB texture rendering. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
95 |
textureFormat = EGL_TEXTURE_RGB; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
96 |
configProps.removeValue(EGL_BIND_TO_TEXTURE_RGBA); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
97 |
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
|
98 |
ok = ctx->chooseConfig(configProps, QEgl::BestPixelFormat); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
99 |
if (!ok) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
100 |
// 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
|
101 |
configProps.removeValue(EGL_BIND_TO_TEXTURE_RGB); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
102 |
textureFormat = EGL_NONE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
103 |
} |
0 | 104 |
} |
105 |
#endif |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
106 |
if (!ok) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
107 |
if (!ctx->chooseConfig(configProps, QEgl::BestPixelFormat)) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
108 |
delete ctx; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
109 |
ctx = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
110 |
return false; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
111 |
} |
0 | 112 |
} |
113 |
} |
|
114 |
||
115 |
// Retrieve the actual format properties. |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
116 |
qt_glformat_from_eglconfig(format, ctx->config()); |
0 | 117 |
|
118 |
// Create the attributes needed for the pbuffer. |
|
119 |
QEglProperties attribs; |
|
120 |
attribs.setValue(EGL_WIDTH, size.width()); |
|
121 |
attribs.setValue(EGL_HEIGHT, size.height()); |
|
122 |
#if QGL_RENDER_TEXTURE |
|
123 |
if (textureFormat != EGL_NONE) { |
|
124 |
attribs.setValue(EGL_TEXTURE_FORMAT, textureFormat); |
|
125 |
attribs.setValue(EGL_TEXTURE_TARGET, EGL_TEXTURE_2D); |
|
126 |
} |
|
127 |
#endif |
|
128 |
||
129 |
// Create the pbuffer surface. |
|
130 |
pbuf = eglCreatePbufferSurface(ctx->display(), ctx->config(), attribs.properties()); |
|
131 |
#if QGL_RENDER_TEXTURE |
|
132 |
if (pbuf == EGL_NO_SURFACE && textureFormat != EGL_NONE) { |
|
133 |
// Try again with texture rendering disabled. |
|
134 |
textureFormat = EGL_NONE; |
|
135 |
attribs.removeValue(EGL_TEXTURE_FORMAT); |
|
136 |
attribs.removeValue(EGL_TEXTURE_TARGET); |
|
137 |
pbuf = eglCreatePbufferSurface(ctx->display(), ctx->config(), attribs.properties()); |
|
138 |
} |
|
139 |
#endif |
|
140 |
if (pbuf == EGL_NO_SURFACE) { |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
141 |
qWarning() << "QGLPixelBufferPrivate::init(): Unable to create EGL pbuffer surface:" << QEgl::errorString(); |
0 | 142 |
return false; |
143 |
} |
|
144 |
||
145 |
// Create a new context for the configuration. |
|
146 |
if (!ctx->createContext(shareContext)) { |
|
147 |
delete ctx; |
|
148 |
ctx = 0; |
|
149 |
return false; |
|
150 |
} |
|
151 |
||
152 |
return true; |
|
153 |
} |
|
154 |
||
155 |
bool QGLPixelBufferPrivate::cleanup() |
|
156 |
{ |
|
157 |
// No need to destroy "pbuf" here - it is done in QGLContext::reset(). |
|
158 |
return true; |
|
159 |
} |
|
160 |
||
161 |
bool QGLPixelBuffer::bindToDynamicTexture(GLuint texture_id) |
|
162 |
{ |
|
163 |
#if QGL_RENDER_TEXTURE |
|
164 |
Q_D(QGLPixelBuffer); |
|
165 |
if (d->invalid || d->textureFormat == EGL_NONE || !d->ctx) |
|
166 |
return false; |
|
167 |
glBindTexture(GL_TEXTURE_2D, texture_id); |
|
168 |
return eglBindTexImage(d->ctx->display(), d->pbuf, EGL_BACK_BUFFER); |
|
169 |
#else |
|
170 |
Q_UNUSED(texture_id); |
|
171 |
return false; |
|
172 |
#endif |
|
173 |
} |
|
174 |
||
175 |
void QGLPixelBuffer::releaseFromDynamicTexture() |
|
176 |
{ |
|
177 |
#if QGL_RENDER_TEXTURE |
|
178 |
Q_D(QGLPixelBuffer); |
|
179 |
if (d->invalid || d->textureFormat == EGL_NONE || !d->ctx) |
|
180 |
return; |
|
181 |
eglReleaseTexImage(d->ctx->display(), d->pbuf, EGL_BACK_BUFFER); |
|
182 |
#endif |
|
183 |
} |
|
184 |
||
185 |
||
186 |
GLuint QGLPixelBuffer::generateDynamicTexture() const |
|
187 |
{ |
|
188 |
#if QGL_RENDER_TEXTURE |
|
189 |
Q_D(const QGLPixelBuffer); |
|
190 |
GLuint texture; |
|
191 |
glGenTextures(1, &texture); |
|
192 |
glBindTexture(GL_TEXTURE_2D, texture); |
|
193 |
if (d->textureFormat == EGL_TEXTURE_RGB) |
|
194 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, d->req_size.width(), d->req_size.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, 0); |
|
195 |
else |
|
196 |
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, d->req_size.width(), d->req_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); |
|
197 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
198 |
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
199 |
return texture; |
|
200 |
#else |
|
201 |
return 0; |
|
202 |
#endif |
|
203 |
} |
|
204 |
||
205 |
bool QGLPixelBuffer::hasOpenGLPbuffers() |
|
206 |
{ |
|
207 |
// See if we have at least 1 configuration that matches the default format. |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
208 |
EGLDisplay dpy = QEgl::display(); |
0 | 209 |
if (dpy == EGL_NO_DISPLAY) |
210 |
return false; |
|
211 |
QEglProperties configProps; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
212 |
qt_eglproperties_set_glformat(configProps, QGLFormat::defaultFormat()); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
213 |
configProps.setDeviceType(QInternal::Pbuffer); |
0 | 214 |
configProps.setRenderableType(QEgl::OpenGL); |
215 |
do { |
|
216 |
EGLConfig cfg = 0; |
|
217 |
EGLint matching = 0; |
|
218 |
if (eglChooseConfig(dpy, configProps.properties(), |
|
219 |
&cfg, 1, &matching) && matching > 0) |
|
220 |
return true; |
|
221 |
} while (configProps.reduceConfiguration()); |
|
222 |
return false; |
|
223 |
} |
|
224 |
||
225 |
QT_END_NAMESPACE |