summaryrefslogtreecommitdiffstats
path: root/src/opengl/qgl_egl.cpp
blob: c521953ec0582e123486ffa3f0bad7fe6bf66086 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtOpenGL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore/qdebug.h>
#include <QtOpenGL/qgl.h>
#include <QtOpenGL/qglpixelbuffer.h>
#include "qgl_p.h"
#include "qgl_egl_p.h"
#include "qglpixelbuffer_p.h"

#ifdef Q_WS_X11
#include <QtGui/private/qpixmap_x11_p.h>
#endif

#if defined(Q_OS_SYMBIAN)
#include <QtGui/private/qgraphicssystemex_symbian_p.h>
#endif

QT_BEGIN_NAMESPACE

QEglProperties *QGLContextPrivate::extraWindowSurfaceCreationProps = NULL;

void qt_eglproperties_set_glformat(QEglProperties& eglProperties, const QGLFormat& glFormat)
{
    int redSize     = glFormat.redBufferSize();
    int greenSize   = glFormat.greenBufferSize();
    int blueSize    = glFormat.blueBufferSize();
    int alphaSize   = glFormat.alphaBufferSize();
    int depthSize   = glFormat.depthBufferSize();
    int stencilSize = glFormat.stencilBufferSize();
    int sampleCount = glFormat.samples();

    bool prefer32Bit = false;
#ifdef Q_OS_SYMBIAN
    // on Symbian we prefer 32-bit configs, unless we're using the low memory GPU
    prefer32Bit = !QSymbianGraphicsSystemEx::hasBCM2727();
#endif

    if (prefer32Bit) {
        if (glFormat.alpha() && alphaSize <= 0)
            alphaSize = 8;
        if (glFormat.depth() && depthSize <= 0)
            depthSize = 24;
        if (glFormat.stencil() && stencilSize <= 0)
            stencilSize = 8;
        if (glFormat.sampleBuffers() && sampleCount <= 0)
            sampleCount = 1;

        redSize   = redSize   > 0 ? redSize   : 8;
        greenSize = greenSize > 0 ? greenSize : 8;
        blueSize  = blueSize  > 0 ? blueSize  : 8;
        alphaSize = alphaSize > 0 ? alphaSize : 8;
        depthSize = depthSize > 0 ? depthSize : 24;
        stencilSize = stencilSize > 0 ? stencilSize : 8;
        sampleCount = sampleCount >= 0 ? sampleCount : 4;
    } else {
        // QGLFormat uses a magic value of -1 to indicate "don't care", even when a buffer of that
        // type has been requested. So we must check QGLFormat's booleans too if size is -1:
        if (glFormat.alpha() && alphaSize <= 0)
            alphaSize = 1;
        if (glFormat.depth() && depthSize <= 0)
            depthSize = 1;
        if (glFormat.stencil() && stencilSize <= 0)
            stencilSize = 1;
        if (glFormat.sampleBuffers() && sampleCount <= 0)
            sampleCount = 1;

        // We want to make sure 16-bit configs are chosen over 32-bit configs as they will provide
        // the best performance. The EGL config selection algorithm is a bit stange in this regard:
        // The selection criteria for EGL_BUFFER_SIZE is "AtLeast", so we can't use it to discard
        // 32-bit configs completely from the selection. So it then comes to the sorting algorithm.
        // The red/green/blue sizes have a sort priority of 3, so they are sorted by first. The sort
        // order is special and described as "by larger _total_ number of color bits.". So EGL will
        // put 32-bit configs in the list before the 16-bit configs. However, the spec also goes on
        // to say "If the requested number of bits in attrib_list for a particular component is 0,
        // then the number of bits for that component is not considered". This part of the spec also
        // seems to imply that setting the red/green/blue bits to zero means none of the components
        // are considered and EGL disregards the entire sorting rule. It then looks to the next
        // highest priority rule, which is EGL_BUFFER_SIZE. Despite the selection criteria being
        // "AtLeast" for EGL_BUFFER_SIZE, it's sort order is "smaller" meaning 16-bit configs are
        // put in the list before 32-bit configs. So, to make sure 16-bit is preffered over 32-bit,
        // we must set the red/green/blue sizes to zero. This has an unfortunate consequence that
        // if the application sets the red/green/blue size to 5/6/5 on the QGLFormat, they will
        // probably get a 32-bit config, even when there's an RGB565 config available. Oh well.

        // Now normalize the values so -1 becomes 0
        redSize   = redSize   > 0 ? redSize   : 0;
        greenSize = greenSize > 0 ? greenSize : 0;
        blueSize  = blueSize  > 0 ? blueSize  : 0;
        alphaSize = alphaSize > 0 ? alphaSize : 0;
        depthSize = depthSize > 0 ? depthSize : 0;
        stencilSize = stencilSize > 0 ? stencilSize : 0;
        sampleCount = sampleCount > 0 ? sampleCount : 0;
    }

    eglProperties.setValue(EGL_RED_SIZE,   redSize);
    eglProperties.setValue(EGL_GREEN_SIZE, greenSize);
    eglProperties.setValue(EGL_BLUE_SIZE,  blueSize);
    eglProperties.setValue(EGL_ALPHA_SIZE, alphaSize);
    eglProperties.setValue(EGL_DEPTH_SIZE, depthSize);
    eglProperties.setValue(EGL_STENCIL_SIZE, stencilSize);
    eglProperties.setValue(EGL_SAMPLES, sampleCount);
    eglProperties.setValue(EGL_SAMPLE_BUFFERS, sampleCount ? 1 : 0);
}

// Updates "format" with the parameters of the selected configuration.
void qt_glformat_from_eglconfig(QGLFormat& format, const EGLConfig config)
{
    EGLint redSize     = 0;
    EGLint greenSize   = 0;
    EGLint blueSize    = 0;
    EGLint alphaSize   = 0;
    EGLint depthSize   = 0;
    EGLint stencilSize = 0;
    EGLint sampleCount = 0;
    EGLint level       = 0;

    EGLDisplay display = QEgl::display();
    eglGetConfigAttrib(display, config, EGL_RED_SIZE,     &redSize);
    eglGetConfigAttrib(display, config, EGL_GREEN_SIZE,   &greenSize);
    eglGetConfigAttrib(display, config, EGL_BLUE_SIZE,    &blueSize);
    eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE,   &alphaSize);
    eglGetConfigAttrib(display, config, EGL_DEPTH_SIZE,   &depthSize);
    eglGetConfigAttrib(display, config, EGL_STENCIL_SIZE, &stencilSize);
    eglGetConfigAttrib(display, config, EGL_SAMPLES,      &sampleCount);
    eglGetConfigAttrib(display, config, EGL_LEVEL,        &level);

    format.setRedBufferSize(redSize);
    format.setGreenBufferSize(greenSize);
    format.setBlueBufferSize(blueSize);
    format.setAlphaBufferSize(alphaSize);
    format.setDepthBufferSize(depthSize);
    format.setStencilBufferSize(stencilSize);
    format.setSamples(sampleCount);
    format.setPlane(level);
    format.setDirectRendering(true); // All EGL contexts are direct-rendered
    format.setRgba(true);            // EGL doesn't support colour index rendering
    format.setStereo(false);         // EGL doesn't support stereo buffers
    format.setAccumBufferSize(0);    // EGL doesn't support accululation buffers
    format.setDoubleBuffer(true);    // We don't support single buffered EGL contexts

    // Clear the EGL error state because some of the above may
    // have errored out because the attribute is not applicable
    // to the surface type.  Such errors don't matter.
    eglGetError();
}

bool QGLFormat::hasOpenGL()
{
    return true;
}

void QGLContext::reset()
{
    Q_D(QGLContext);
    if (!d->valid)
        return;
    d->cleanup();
    doneCurrent();
    if (d->eglContext && d->ownsEglContext) {
        d->destroyEglSurfaceForDevice();
        delete d->eglContext;
    }
    d->ownsEglContext = false;
    d->eglContext = 0;
    d->eglSurface = EGL_NO_SURFACE;
    d->crWin = false;
    d->sharing = false;
    d->valid = false;
    d->transpColor = QColor();
    d->initDone = false;
    QGLContextGroup::removeShare(this);
}

void QGLContext::makeCurrent()
{
    Q_D(QGLContext);
    if (!d->valid || !d->eglContext || d->eglSurfaceForDevice() == EGL_NO_SURFACE) {
        qWarning("QGLContext::makeCurrent(): Cannot make invalid context current");
        return;
    }

    if (d->eglContext->makeCurrent(d->eglSurfaceForDevice())) {
        QGLContextPrivate::setCurrentContext(this);
        if (!d->workaroundsCached) {
            d->workaroundsCached = true;
            const char *renderer = reinterpret_cast<const char *>(glGetString(GL_RENDERER));
            if (!renderer)
                return;
            if ((strstr(renderer, "SGX") || strstr(renderer, "MBX"))) {
                // PowerVR MBX/SGX chips needs to clear all buffers when starting to render
                // a new frame, otherwise there will be a performance penalty to pay for
                // each frame.
                qDebug() << "Found SGX/MBX driver, enabling FullClearOnEveryFrame";
                d->workaround_needsFullClearOnEveryFrame = true;

                // Older PowerVR SGX drivers (like the one in the N900) have a
                // bug which prevents glCopyTexSubImage2D() to work with a POT
                // or GL_ALPHA texture bound to an FBO. The only way to
                // identify that driver is to check the EGL version number for it.
                const char *egl_version = eglQueryString(d->eglContext->display(), EGL_VERSION);

                if (egl_version && strstr(egl_version, "1.3")) {
                    qDebug() << "Found v1.3 driver, enabling brokenFBOReadBack";
                    d->workaround_brokenFBOReadBack = true;
                } else if (egl_version && strstr(egl_version, "1.4")) {
                    qDebug() << "Found v1.4 driver, enabling brokenTexSubImage";
                    d->workaround_brokenTexSubImage = true;

                    // this is a bit complicated; 1.4 version SGX drivers from
                    // Nokia have fixed the brokenFBOReadBack problem, but
                    // official drivers from TI haven't, meaning that things
                    // like the beagleboard are broken unless we hack around it
                    // - but at the same time, we want to not reduce performance
                    // by not enabling this elsewhere.
                    //
                    // so, let's check for a Nokia-specific addon, and only
                    // enable if it isn't present.
                    // (see MeeGo bug #5616)
                    if (!QEgl::hasExtension("EGL_NOK_image_shared")) {
                        // no Nokia extension, this is probably a standard SGX
                        // driver, so enable the workaround
                        qDebug() << "Found non-Nokia v1.4 driver, enabling brokenFBOReadBack";
                        d->workaround_brokenFBOReadBack = true;
                    }
                }
            } else if (strstr(renderer, "VideoCore III")) {
                // Some versions of VideoCore III drivers seem to pollute and use
                // stencil buffer when using glScissors even if stencil test is disabled.
                // Workaround is to clear stencil buffer before disabling scissoring.

                // qDebug() << "Found VideoCore III driver, enabling brokenDisableScissorTest"; 
                d->workaround_brokenScissor = true;
            }
        }
    }
}

void QGLContext::doneCurrent()
{
    Q_D(QGLContext);
    if (d->eglContext)
        d->eglContext->doneCurrent();

    QGLContextPrivate::setCurrentContext(0);
}


void QGLContext::swapBuffers() const
{
    Q_D(const QGLContext);
    if (!d->valid || !d->eglContext)
        return;

    d->eglContext->swapBuffers(d->eglSurfaceForDevice());
}

void QGLContextPrivate::destroyEglSurfaceForDevice()
{
    if (eglSurface != EGL_NO_SURFACE) {
#if defined(Q_WS_X11) || defined(Q_OS_SYMBIAN)
        // Make sure we don't call eglDestroySurface on a surface which
        // was created for a different winId. This applies only to QGLWidget
        // paint device, so make sure this is the one we're operating on
        // (as opposed to a QGLWindowSurface use case).
        if (paintDevice && paintDevice->devType() == QInternal::Widget) {
            QWidget *w = static_cast<QWidget *>(paintDevice);
            if (QGLWidget *wgl = qobject_cast<QGLWidget *>(w)) {
                if (wgl->d_func()->eglSurfaceWindowId != wgl->winId()) {
                    qWarning("WARNING: Potential EGL surface leak! Not destroying surface.");
                    eglSurface = EGL_NO_SURFACE;
                    return;
                }
            }
        }
#endif
        eglDestroySurface(eglContext->display(), eglSurface);
        eglSurface = EGL_NO_SURFACE;
    }
}

EGLSurface QGLContextPrivate::eglSurfaceForDevice() const
{
    // If a QPixmapData had to create the QGLContext, we don't have a paintDevice
    if (!paintDevice)
        return eglSurface;

#ifdef Q_WS_X11
    if (paintDevice->devType() == QInternal::Pixmap) {
        QPixmapData *pmd = static_cast<QPixmap*>(paintDevice)->data_ptr().data();
        if (pmd->classId() == QPixmapData::X11Class) {
            QX11PixmapData* x11PixmapData = static_cast<QX11PixmapData*>(pmd);
            return (EGLSurface)x11PixmapData->gl_surface;
        }
    }
#endif

    if (paintDevice->devType() == QInternal::Pbuffer) {
        QGLPixelBuffer* pbuf = static_cast<QGLPixelBuffer*>(paintDevice);
        return pbuf->d_func()->pbuf;
    }

    return eglSurface;
}

void QGLContextPrivate::swapRegion(const QRegion &region)
{
    if (!valid || !eglContext)
        return;

    eglContext->swapBuffersRegion2NOK(eglSurfaceForDevice(), &region);
}

void QGLContextPrivate::setExtraWindowSurfaceCreationProps(QEglProperties *props)
{
    extraWindowSurfaceCreationProps = props;
}

void QGLWidget::setMouseTracking(bool enable)
{
    QWidget::setMouseTracking(enable);
}

QColor QGLContext::overlayTransparentColor() const
{
    return d_func()->transpColor;
}

uint QGLContext::colorIndex(const QColor &c) const
{
    Q_UNUSED(c);
    return 0;
}

void QGLContext::generateFontDisplayLists(const QFont & fnt, int listBase)
{
    Q_UNUSED(fnt);
    Q_UNUSED(listBase);
}

void *QGLContext::getProcAddress(const QString &proc) const
{
    return (void*)eglGetProcAddress(reinterpret_cast<const char *>(proc.toLatin1().data()));
}

bool QGLWidgetPrivate::renderCxPm(QPixmap*)
{
    return false;
}

QT_END_NAMESPACE