Ignore:
Timestamp:
Aug 2, 2010, 9:27:30 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/gui/egl/qegl.cpp

    r651 r769  
    4343#include <QtGui/qpixmap.h>
    4444#include <QtGui/qwidget.h>
     45#include <QtCore/qatomic.h>
    4546#include <QtCore/qdebug.h>
    4647#include "qegl_p.h"
    4748
    4849QT_BEGIN_NAMESPACE
     50
     51
     52/*
     53    QEglContextTracker is used to track the EGL contexts that we
     54    create internally in Qt, so that we can call eglTerminate() to
     55    free additional EGL resources when the last context is destroyed.
     56*/
     57
     58class QEglContextTracker
     59{
     60public:
     61    static void ref() { contexts.ref(); }
     62    static void deref() {
     63        if (!contexts.deref()) {
     64            eglTerminate(QEglContext::display());
     65            displayOpen = 0;
     66        }
     67    }
     68    static void setDisplayOpened() { displayOpen = 1; }
     69    static bool displayOpened() { return displayOpen; }
     70
     71private:
     72    static QBasicAtomicInt contexts;
     73    static QBasicAtomicInt displayOpen;
     74};
     75
     76QBasicAtomicInt QEglContextTracker::contexts = Q_BASIC_ATOMIC_INITIALIZER(0);
     77QBasicAtomicInt QEglContextTracker::displayOpen = Q_BASIC_ATOMIC_INITIALIZER(0);
    4978
    5079// Current GL and VG contexts.  These are used to determine if
     
    5584static QEglContext * volatile currentVGContext = 0;
    5685
     86EGLDisplay QEglContext::dpy = EGL_NO_DISPLAY;
     87
    5788QEglContext::QEglContext()
    5889    : apiType(QEgl::OpenGL)
    59     , dpy(EGL_NO_DISPLAY)
    6090    , ctx(EGL_NO_CONTEXT)
    6191    , cfg(0)
     
    6595    , sharing(false)
    6696{
     97    QEglContextTracker::ref();
    6798}
    6899
    69100QEglContext::~QEglContext()
    70101{
    71     destroy();
     102    destroyContext();
    72103
    73104    if (currentGLContext == this)
     
    75106    if (currentVGContext == this)
    76107        currentVGContext = 0;
     108    QEglContextTracker::deref();
    77109}
    78110
     
    85117{
    86118    return current;
    87 }
    88 
    89 // Open the EGL display associated with "device".
    90 bool QEglContext::openDisplay(QPaintDevice *device)
    91 {
    92     if (dpy == EGL_NO_DISPLAY)
    93         dpy = defaultDisplay(device);
    94     return (dpy != EGL_NO_DISPLAY);
    95119}
    96120
     
    103127        // Get the number of matching configurations for this set of properties.
    104128        EGLint matching = 0;
    105         if (!eglChooseConfig(dpy, props.properties(), 0, 0, &matching) || !matching)
     129        if (!eglChooseConfig(display(), props.properties(), 0, 0, &matching) || !matching)
    106130            continue;
    107131
     
    109133        // matching configuration.
    110134        if (match == QEgl::BestPixelFormat) {
    111             eglChooseConfig(dpy, props.properties(), &cfg, 1, &matching);
     135            eglChooseConfig(display(), props.properties(), &cfg, 1, &matching);
    112136            if (matching < 1)
    113137                continue;
     
    119143        EGLint size = matching;
    120144        EGLConfig *configs = new EGLConfig [size];
    121         eglChooseConfig(dpy, props.properties(), configs, size, &matching);
     145        eglChooseConfig(display(), props.properties(), configs, size, &matching);
    122146        for (EGLint index = 0; index < size; ++index) {
    123147            EGLint red, green, blue, alpha;
    124             eglGetConfigAttrib(dpy, configs[index], EGL_RED_SIZE, &red);
    125             eglGetConfigAttrib(dpy, configs[index], EGL_GREEN_SIZE, &green);
    126             eglGetConfigAttrib(dpy, configs[index], EGL_BLUE_SIZE, &blue);
    127             eglGetConfigAttrib(dpy, configs[index], EGL_ALPHA_SIZE, &alpha);
     148            eglGetConfigAttrib(display(), configs[index], EGL_RED_SIZE, &red);
     149            eglGetConfigAttrib(display(), configs[index], EGL_GREEN_SIZE, &green);
     150            eglGetConfigAttrib(display(), configs[index], EGL_BLUE_SIZE, &blue);
     151            eglGetConfigAttrib(display(), configs[index], EGL_ALPHA_SIZE, &alpha);
    128152            if (red == props.value(EGL_RED_SIZE) &&
    129153                    green == props.value(EGL_GREEN_SIZE) &&
     
    180204        shareContext = 0;
    181205    if (shareContext) {
    182         ctx = eglCreateContext(dpy, cfg, shareContext->ctx, contextProps.properties());
     206        ctx = eglCreateContext(display(), cfg, shareContext->ctx, contextProps.properties());
    183207        if (ctx == EGL_NO_CONTEXT) {
    184208            qWarning() << "QEglContext::createContext(): Could not share context:" << errorString(eglGetError());
     
    189213    }
    190214    if (ctx == EGL_NO_CONTEXT) {
    191         ctx = eglCreateContext(dpy, cfg, 0, contextProps.properties());
     215        ctx = eglCreateContext(display(), cfg, 0, contextProps.properties());
    192216        if (ctx == EGL_NO_CONTEXT) {
    193217            qWarning() << "QEglContext::createContext(): Unable to create EGL context:" << errorString(eglGetError());
     
    205229        if (surface == currentSurface)
    206230            doneCurrent();
    207         eglDestroySurface(dpy, surface);
     231        eglDestroySurface(display(), surface);
    208232    }
    209233}
    210234
    211235// Destroy the context.  Note: this does not destroy the surface.
    212 void QEglContext::destroy()
     236void QEglContext::destroyContext()
    213237{
    214238    if (ctx != EGL_NO_CONTEXT && ownsContext)
    215         eglDestroyContext(dpy, ctx);
    216     dpy = EGL_NO_DISPLAY;
     239        eglDestroyContext(display(), ctx);
    217240    ctx = EGL_NO_CONTEXT;
    218241    cfg = 0;
     
    249272#endif
    250273
    251     bool ok = eglMakeCurrent(dpy, surface, surface, ctx);
     274    bool ok = eglMakeCurrent(display(), surface, surface, ctx);
    252275    if (!ok)
    253276        qWarning() << "QEglContext::makeCurrent():" << errorString(eglGetError());
     
    278301#endif
    279302
    280     bool ok = eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
     303    bool ok = eglMakeCurrent(display(), EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    281304    if (!ok)
    282305        qWarning() << "QEglContext::doneCurrent():" << errorString(eglGetError());
     
    300323        return false;
    301324
    302     bool ok = eglSwapBuffers(dpy, surface);
     325    bool ok = eglSwapBuffers(display(), surface);
    303326    if (!ok)
    304327        qWarning() << "QEglContext::swapBuffers():" << errorString(eglGetError());
     
    339362bool QEglContext::configAttrib(int name, EGLint *value) const
    340363{
    341     return eglGetConfigAttrib(dpy, cfg, name, value);
     364    return eglGetConfigAttrib(display(), cfg, name, value);
     365}
     366
     367void QEglContext::clearError()
     368{
     369    eglGetError();
     370}
     371
     372EGLint QEglContext::error()
     373{
     374    return eglGetError();
    342375}
    343376
     
    351384    for (int name = 0x3020; name <= 0x304F; ++name) {
    352385        EGLint value;
    353         if (name != EGL_NONE && eglGetConfigAttrib(dpy, cfg, name, &value))
     386        if (name != EGL_NONE && eglGetConfigAttrib(display(), cfg, name, &value))
    354387            props.setValue(name, value);
    355388    }
     
    358391}
    359392
    360 // Initialize and return the default display.
    361 EGLDisplay QEglContext::defaultDisplay(QPaintDevice *device)
    362 {
    363     static EGLDisplay dpy = EGL_NO_DISPLAY;
    364     if (dpy == EGL_NO_DISPLAY) {
    365         dpy = getDisplay(device);
     393EGLDisplay QEglContext::display()
     394{
     395    if (!QEglContextTracker::displayOpened()) {
     396        dpy = eglGetDisplay(nativeDisplay());
     397        QEglContextTracker::setDisplayOpened();
    366398        if (dpy == EGL_NO_DISPLAY) {
    367             qWarning() << "QEglContext::defaultDisplay(): Cannot open EGL display";
     399            qWarning("QEglContext::display(): Falling back to EGL_DEFAULT_DISPLAY");
     400            dpy = eglGetDisplay(EGLNativeDisplayType(EGL_DEFAULT_DISPLAY));
     401        }
     402        if (dpy == EGL_NO_DISPLAY) {
     403            qWarning("QEglContext::display(): Can't even open the default display");
    368404            return EGL_NO_DISPLAY;
    369405        }
     406
    370407        if (!eglInitialize(dpy, NULL, NULL)) {
    371             qWarning() << "QEglContext::defaultDisplay(): Cannot initialize EGL display:" << errorString(eglGetError());
     408            qWarning() << "QEglContext::display(): Cannot initialize EGL display:" << errorString(eglGetError());
    372409            return EGL_NO_DISPLAY;
    373410        }
    374 #ifdef EGL_OPENGL_ES_API
    375         eglBindAPI(EGL_OPENGL_ES_API);
    376 #endif
    377     }
     411    }
     412
    378413    return dpy;
    379414}
     415
     416#if !defined(Q_WS_X11) && !defined(Q_WS_WINCE) // WinCE & X11 implement this properly
     417EGLNativeDisplayType QEglContext::nativeDisplay()
     418{
     419    return EGL_DEFAULT_DISPLAY;
     420}
     421#endif
    380422
    381423// Return the error string associated with a specific code.
     
    411453    QEglProperties props;
    412454    EGLint count = 0;
    413     if (!eglGetConfigs(dpy, 0, 0, &count) || count < 1)
     455    if (!eglGetConfigs(display(), 0, 0, &count) || count < 1)
    414456        return;
    415457    EGLConfig *configs = new EGLConfig [count];
    416     eglGetConfigs(dpy, configs, count, &count);
     458    eglGetConfigs(display(), configs, count, &count);
    417459    for (EGLint index = 0; index < count; ++index) {
    418460        props = configProperties(configs[index]);
     
    424466QString QEglContext::extensions()
    425467{
    426     const char* exts = eglQueryString(QEglContext::defaultDisplay(0), EGL_EXTENSIONS);
     468    const char* exts = eglQueryString(QEglContext::display(), EGL_EXTENSIONS);
    427469    return QString(QLatin1String(exts));
    428470}
     
    432474    QList<QByteArray> extensions =
    433475        QByteArray(reinterpret_cast<const char *>
    434             (eglQueryString(QEglContext::defaultDisplay(0), EGL_EXTENSIONS))).split(' ');
     476            (eglQueryString(QEglContext::display(), EGL_EXTENSIONS))).split(' ');
    435477    return extensions.contains(extensionName);
    436478}
Note: See TracChangeset for help on using the changeset viewer.