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