|
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 |
|
75 // Choose an appropriate configuration. We use the best format |
|
76 // we can find, even if it is greater than the requested format. |
|
77 // We try for a pbuffer that is capable of texture rendering if possible. |
|
78 QEglProperties configProps; |
|
79 qt_egl_set_format(configProps, QInternal::Pbuffer, f); |
|
80 configProps.setRenderableType(ctx->api()); |
|
81 bool ok = false; |
|
82 #if QGL_RENDER_TEXTURE |
|
83 textureFormat = EGL_TEXTURE_RGBA; |
|
84 configProps.setValue(EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE); |
|
85 ok = ctx->chooseConfig(configProps, QEgl::BestPixelFormat); |
|
86 if (!ok) { |
|
87 // Try again with RGB texture rendering. |
|
88 textureFormat = EGL_TEXTURE_RGB; |
|
89 configProps.removeValue(EGL_BIND_TO_TEXTURE_RGBA); |
|
90 configProps.setValue(EGL_BIND_TO_TEXTURE_RGB, EGL_TRUE); |
|
91 ok = ctx->chooseConfig(configProps, QEgl::BestPixelFormat); |
|
92 if (!ok) { |
|
93 // One last try for a pbuffer with no texture rendering. |
|
94 configProps.removeValue(EGL_BIND_TO_TEXTURE_RGB); |
|
95 textureFormat = EGL_NONE; |
|
96 } |
|
97 } |
|
98 #else |
|
99 textureFormat = EGL_NONE; |
|
100 #endif |
|
101 if (!ok) { |
|
102 if (!ctx->chooseConfig(configProps, QEgl::BestPixelFormat)) { |
|
103 delete ctx; |
|
104 ctx = 0; |
|
105 return false; |
|
106 } |
|
107 } |
|
108 |
|
109 // Retrieve the actual format properties. |
|
110 qt_egl_update_format(*ctx, format); |
|
111 |
|
112 // Create the attributes needed for the pbuffer. |
|
113 QEglProperties attribs; |
|
114 attribs.setValue(EGL_WIDTH, size.width()); |
|
115 attribs.setValue(EGL_HEIGHT, size.height()); |
|
116 #if QGL_RENDER_TEXTURE |
|
117 if (textureFormat != EGL_NONE) { |
|
118 attribs.setValue(EGL_TEXTURE_FORMAT, textureFormat); |
|
119 attribs.setValue(EGL_TEXTURE_TARGET, EGL_TEXTURE_2D); |
|
120 } |
|
121 #endif |
|
122 |
|
123 // Create the pbuffer surface. |
|
124 pbuf = eglCreatePbufferSurface(ctx->display(), ctx->config(), attribs.properties()); |
|
125 #if QGL_RENDER_TEXTURE |
|
126 if (pbuf == EGL_NO_SURFACE && textureFormat != EGL_NONE) { |
|
127 // Try again with texture rendering disabled. |
|
128 textureFormat = EGL_NONE; |
|
129 attribs.removeValue(EGL_TEXTURE_FORMAT); |
|
130 attribs.removeValue(EGL_TEXTURE_TARGET); |
|
131 pbuf = eglCreatePbufferSurface(ctx->display(), ctx->config(), attribs.properties()); |
|
132 } |
|
133 #endif |
|
134 if (pbuf == EGL_NO_SURFACE) { |
|
135 qWarning() << "QGLPixelBufferPrivate::init(): Unable to create EGL pbuffer surface:" << QEglContext::errorString(eglGetError()); |
|
136 return false; |
|
137 } |
|
138 |
|
139 // Create a new context for the configuration. |
|
140 QEglContext *shareContext = 0; |
|
141 if (shareWidget && shareWidget->d_func()->glcx) |
|
142 shareContext = shareWidget->d_func()->glcx->d_func()->eglContext; |
|
143 if (!ctx->createContext(shareContext)) { |
|
144 delete ctx; |
|
145 ctx = 0; |
|
146 return false; |
|
147 } |
|
148 |
|
149 return true; |
|
150 } |
|
151 |
|
152 bool QGLPixelBufferPrivate::cleanup() |
|
153 { |
|
154 // No need to destroy "pbuf" here - it is done in QGLContext::reset(). |
|
155 return true; |
|
156 } |
|
157 |
|
158 bool QGLPixelBuffer::bindToDynamicTexture(GLuint texture_id) |
|
159 { |
|
160 #if QGL_RENDER_TEXTURE |
|
161 Q_D(QGLPixelBuffer); |
|
162 if (d->invalid || d->textureFormat == EGL_NONE || !d->ctx) |
|
163 return false; |
|
164 glBindTexture(GL_TEXTURE_2D, texture_id); |
|
165 return eglBindTexImage(d->ctx->display(), d->pbuf, EGL_BACK_BUFFER); |
|
166 #else |
|
167 Q_UNUSED(texture_id); |
|
168 return false; |
|
169 #endif |
|
170 } |
|
171 |
|
172 void QGLPixelBuffer::releaseFromDynamicTexture() |
|
173 { |
|
174 #if QGL_RENDER_TEXTURE |
|
175 Q_D(QGLPixelBuffer); |
|
176 if (d->invalid || d->textureFormat == EGL_NONE || !d->ctx) |
|
177 return; |
|
178 eglReleaseTexImage(d->ctx->display(), d->pbuf, EGL_BACK_BUFFER); |
|
179 #endif |
|
180 } |
|
181 |
|
182 |
|
183 GLuint QGLPixelBuffer::generateDynamicTexture() const |
|
184 { |
|
185 #if QGL_RENDER_TEXTURE |
|
186 Q_D(const QGLPixelBuffer); |
|
187 GLuint texture; |
|
188 glGenTextures(1, &texture); |
|
189 glBindTexture(GL_TEXTURE_2D, texture); |
|
190 if (d->textureFormat == EGL_TEXTURE_RGB) |
|
191 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, d->req_size.width(), d->req_size.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, 0); |
|
192 else |
|
193 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, d->req_size.width(), d->req_size.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); |
|
194 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
195 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
196 return texture; |
|
197 #else |
|
198 return 0; |
|
199 #endif |
|
200 } |
|
201 |
|
202 bool QGLPixelBuffer::hasOpenGLPbuffers() |
|
203 { |
|
204 // See if we have at least 1 configuration that matches the default format. |
|
205 EGLDisplay dpy = QEglContext::defaultDisplay(0); |
|
206 if (dpy == EGL_NO_DISPLAY) |
|
207 return false; |
|
208 QEglProperties configProps; |
|
209 qt_egl_set_format(configProps, QInternal::Pbuffer, QGLFormat::defaultFormat()); |
|
210 configProps.setRenderableType(QEgl::OpenGL); |
|
211 do { |
|
212 EGLConfig cfg = 0; |
|
213 EGLint matching = 0; |
|
214 if (eglChooseConfig(dpy, configProps.properties(), |
|
215 &cfg, 1, &matching) && matching > 0) |
|
216 return true; |
|
217 } while (configProps.reduceConfiguration()); |
|
218 return false; |
|
219 } |
|
220 |
|
221 QT_END_NAMESPACE |