source: trunk/src/opengl/qgl_egl.cpp@ 966

Last change on this file since 966 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 12.2 KB
Line 
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 <QtOpenGL/qgl.h>
43#include <QtOpenGL/qglpixelbuffer.h>
44#include "qgl_p.h"
45#include "qgl_egl_p.h"
46#include "qglpixelbuffer_p.h"
47
48#ifdef Q_WS_X11
49#include <QtGui/private/qpixmap_x11_p.h>
50#endif
51
52QT_BEGIN_NAMESPACE
53
54void qt_eglproperties_set_glformat(QEglProperties& eglProperties, const QGLFormat& glFormat)
55{
56 int redSize = glFormat.redBufferSize();
57 int greenSize = glFormat.greenBufferSize();
58 int blueSize = glFormat.blueBufferSize();
59 int alphaSize = glFormat.alphaBufferSize();
60 int depthSize = glFormat.depthBufferSize();
61 int stencilSize = glFormat.stencilBufferSize();
62 int sampleCount = glFormat.samples();
63
64 // QGLFormat uses a magic value of -1 to indicate "don't care", even when a buffer of that
65 // type has been requested. So we must check QGLFormat's booleans too if size is -1:
66 if (glFormat.alpha() && alphaSize <= 0)
67 alphaSize = 1;
68 if (glFormat.depth() && depthSize <= 0)
69 depthSize = 1;
70 if (glFormat.stencil() && stencilSize <= 0)
71 stencilSize = 1;
72 if (glFormat.sampleBuffers() && sampleCount <= 0)
73 sampleCount = 1;
74
75 // We want to make sure 16-bit configs are chosen over 32-bit configs as they will provide
76 // the best performance. The EGL config selection algorithm is a bit stange in this regard:
77 // The selection criteria for EGL_BUFFER_SIZE is "AtLeast", so we can't use it to discard
78 // 32-bit configs completely from the selection. So it then comes to the sorting algorithm.
79 // The red/green/blue sizes have a sort priority of 3, so they are sorted by first. The sort
80 // order is special and described as "by larger _total_ number of color bits.". So EGL will
81 // put 32-bit configs in the list before the 16-bit configs. However, the spec also goes on
82 // to say "If the requested number of bits in attrib_list for a particular component is 0,
83 // then the number of bits for that component is not considered". This part of the spec also
84 // seems to imply that setting the red/green/blue bits to zero means none of the components
85 // are considered and EGL disregards the entire sorting rule. It then looks to the next
86 // highest priority rule, which is EGL_BUFFER_SIZE. Despite the selection criteria being
87 // "AtLeast" for EGL_BUFFER_SIZE, it's sort order is "smaller" meaning 16-bit configs are
88 // put in the list before 32-bit configs. So, to make sure 16-bit is preffered over 32-bit,
89 // we must set the red/green/blue sizes to zero. This has an unfortunate consequence that
90 // if the application sets the red/green/blue size to 5/6/5 on the QGLFormat, they will
91 // probably get a 32-bit config, even when there's an RGB565 config available. Oh well.
92
93 // Now normalize the values so -1 becomes 0
94 redSize = redSize > 0 ? redSize : 0;
95 greenSize = greenSize > 0 ? greenSize : 0;
96 blueSize = blueSize > 0 ? blueSize : 0;
97 alphaSize = alphaSize > 0 ? alphaSize : 0;
98 depthSize = depthSize > 0 ? depthSize : 0;
99 stencilSize = stencilSize > 0 ? stencilSize : 0;
100 sampleCount = sampleCount > 0 ? sampleCount : 0;
101
102 eglProperties.setValue(EGL_RED_SIZE, redSize);
103 eglProperties.setValue(EGL_GREEN_SIZE, greenSize);
104 eglProperties.setValue(EGL_BLUE_SIZE, blueSize);
105 eglProperties.setValue(EGL_ALPHA_SIZE, alphaSize);
106 eglProperties.setValue(EGL_DEPTH_SIZE, depthSize);
107 eglProperties.setValue(EGL_STENCIL_SIZE, stencilSize);
108 eglProperties.setValue(EGL_SAMPLES, sampleCount);
109 eglProperties.setValue(EGL_SAMPLE_BUFFERS, sampleCount ? 1 : 0);
110}
111
112// Updates "format" with the parameters of the selected configuration.
113void qt_glformat_from_eglconfig(QGLFormat& format, const EGLConfig config)
114{
115 EGLint redSize = 0;
116 EGLint greenSize = 0;
117 EGLint blueSize = 0;
118 EGLint alphaSize = 0;
119 EGLint depthSize = 0;
120 EGLint stencilSize = 0;
121 EGLint sampleCount = 0;
122 EGLint level = 0;
123
124 EGLDisplay display = QEgl::display();
125 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &redSize);
126 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &greenSize);
127 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &blueSize);
128 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &alphaSize);
129 eglGetConfigAttrib(display, config, EGL_DEPTH_SIZE, &depthSize);
130 eglGetConfigAttrib(display, config, EGL_STENCIL_SIZE, &stencilSize);
131 eglGetConfigAttrib(display, config, EGL_SAMPLES, &sampleCount);
132 eglGetConfigAttrib(display, config, EGL_LEVEL, &level);
133
134 format.setRedBufferSize(redSize);
135 format.setGreenBufferSize(greenSize);
136 format.setBlueBufferSize(blueSize);
137 format.setAlphaBufferSize(alphaSize);
138 format.setDepthBufferSize(depthSize);
139 format.setStencilBufferSize(stencilSize);
140 format.setSamples(sampleCount);
141 format.setPlane(level);
142 format.setDirectRendering(true); // All EGL contexts are direct-rendered
143 format.setRgba(true); // EGL doesn't support colour index rendering
144 format.setStereo(false); // EGL doesn't support stereo buffers
145 format.setAccumBufferSize(0); // EGL doesn't support accululation buffers
146 format.setDoubleBuffer(true); // We don't support single buffered EGL contexts
147
148 // Clear the EGL error state because some of the above may
149 // have errored out because the attribute is not applicable
150 // to the surface type. Such errors don't matter.
151 eglGetError();
152}
153
154bool QGLFormat::hasOpenGL()
155{
156 return true;
157}
158
159void QGLContext::reset()
160{
161 Q_D(QGLContext);
162 if (!d->valid)
163 return;
164 d->cleanup();
165 doneCurrent();
166 if (d->eglContext && d->ownsEglContext) {
167 d->destroyEglSurfaceForDevice();
168 delete d->eglContext;
169 }
170 d->ownsEglContext = false;
171 d->eglContext = 0;
172 d->eglSurface = EGL_NO_SURFACE;
173 d->crWin = false;
174 d->sharing = false;
175 d->valid = false;
176 d->transpColor = QColor();
177 d->initDone = false;
178 QGLContextGroup::removeShare(this);
179}
180
181void QGLContext::makeCurrent()
182{
183 Q_D(QGLContext);
184 if (!d->valid || !d->eglContext || d->eglSurfaceForDevice() == EGL_NO_SURFACE) {
185 qWarning("QGLContext::makeCurrent(): Cannot make invalid context current");
186 return;
187 }
188
189 if (d->eglContext->makeCurrent(d->eglSurfaceForDevice())) {
190 QGLContextPrivate::setCurrentContext(this);
191 if (!d->workaroundsCached) {
192 d->workaroundsCached = true;
193 const char *renderer = reinterpret_cast<const char *>(glGetString(GL_RENDERER));
194 if (renderer && (strstr(renderer, "SGX") || strstr(renderer, "MBX"))) {
195 // PowerVR MBX/SGX chips needs to clear all buffers when starting to render
196 // a new frame, otherwise there will be a performance penalty to pay for
197 // each frame.
198 d->workaround_needsFullClearOnEveryFrame = true;
199
200 // Older PowerVR SGX drivers (like the one in the N900) have a
201 // bug which prevents glCopyTexSubImage2D() to work with a POT
202 // or GL_ALPHA texture bound to an FBO. The only way to
203 // identify that driver is to check the EGL version number for it.
204 const char *egl_version = eglQueryString(d->eglContext->display(), EGL_VERSION);
205 if (egl_version && strstr(egl_version, "1.3"))
206 d->workaround_brokenFBOReadBack = true;
207 else if (egl_version && strstr(egl_version, "1.4"))
208 d->workaround_brokenTexSubImage = true;
209 }
210 }
211 }
212}
213
214void QGLContext::doneCurrent()
215{
216 Q_D(QGLContext);
217 if (d->eglContext)
218 d->eglContext->doneCurrent();
219
220 QGLContextPrivate::setCurrentContext(0);
221}
222
223
224void QGLContext::swapBuffers() const
225{
226 Q_D(const QGLContext);
227 if (!d->valid || !d->eglContext)
228 return;
229
230 d->eglContext->swapBuffers(d->eglSurfaceForDevice());
231}
232
233void QGLContextPrivate::destroyEglSurfaceForDevice()
234{
235 if (eglSurface != EGL_NO_SURFACE) {
236#if defined(Q_WS_X11) || defined(Q_OS_SYMBIAN)
237 // Make sure we don't call eglDestroySurface on a surface which
238 // was created for a different winId. This applies only to QGLWidget
239 // paint device, so make sure this is the one we're operating on
240 // (as opposed to a QGLWindowSurface use case).
241 if (paintDevice && paintDevice->devType() == QInternal::Widget) {
242 QWidget *w = static_cast<QWidget *>(paintDevice);
243 if (QGLWidget *wgl = qobject_cast<QGLWidget *>(w)) {
244 if (wgl->d_func()->eglSurfaceWindowId != wgl->winId()) {
245 qWarning("WARNING: Potential EGL surface leak! Not destroying surface.");
246 eglSurface = EGL_NO_SURFACE;
247 return;
248 }
249 }
250 }
251#endif
252 eglDestroySurface(eglContext->display(), eglSurface);
253 eglSurface = EGL_NO_SURFACE;
254 }
255}
256
257EGLSurface QGLContextPrivate::eglSurfaceForDevice() const
258{
259 // If a QPixmapData had to create the QGLContext, we don't have a paintDevice
260 if (!paintDevice)
261 return eglSurface;
262
263#ifdef Q_WS_X11
264 if (paintDevice->devType() == QInternal::Pixmap) {
265 QPixmapData *pmd = static_cast<QPixmap*>(paintDevice)->data_ptr().data();
266 if (pmd->classId() == QPixmapData::X11Class) {
267 QX11PixmapData* x11PixmapData = static_cast<QX11PixmapData*>(pmd);
268 return (EGLSurface)x11PixmapData->gl_surface;
269 }
270 }
271#endif
272
273 if (paintDevice->devType() == QInternal::Pbuffer) {
274 QGLPixelBuffer* pbuf = static_cast<QGLPixelBuffer*>(paintDevice);
275 return pbuf->d_func()->pbuf;
276 }
277
278 return eglSurface;
279}
280
281void QGLContextPrivate::swapRegion(const QRegion &region)
282{
283 if (!valid || !eglContext)
284 return;
285
286 eglContext->swapBuffersRegion2NOK(eglSurfaceForDevice(), &region);
287}
288
289void QGLWidget::setMouseTracking(bool enable)
290{
291 QWidget::setMouseTracking(enable);
292}
293
294QColor QGLContext::overlayTransparentColor() const
295{
296 return d_func()->transpColor;
297}
298
299uint QGLContext::colorIndex(const QColor &c) const
300{
301 Q_UNUSED(c);
302 return 0;
303}
304
305void QGLContext::generateFontDisplayLists(const QFont & fnt, int listBase)
306{
307 Q_UNUSED(fnt);
308 Q_UNUSED(listBase);
309}
310
311void *QGLContext::getProcAddress(const QString &proc) const
312{
313 return (void*)eglGetProcAddress(reinterpret_cast<const char *>(proc.toLatin1().data()));
314}
315
316bool QGLWidgetPrivate::renderCxPm(QPixmap*)
317{
318 return false;
319}
320
321QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.