src/opengl/qpixmapdata_x11gl_egl.cpp
changeset 7 f7bc934e204c
parent 0 1918ee327afb
child 30 5dc02b23752f
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
     7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
     8 **
     8 **
    37 **
    37 **
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
       
    42 #include <QDebug>
       
    43 
    42 #include <private/qgl_p.h>
    44 #include <private/qgl_p.h>
    43 #include <private/qegl_p.h>
    45 #include <private/qegl_p.h>
    44 #include <private/qeglproperties_p.h>
    46 #include <private/qeglproperties_p.h>
       
    47 
       
    48 #if !defined(QT_OPENGL_ES_1) && !defined(QT_OPENGL_ES_1_CL)
    45 #include <private/qpaintengineex_opengl2_p.h>
    49 #include <private/qpaintengineex_opengl2_p.h>
       
    50 #endif
       
    51 
       
    52 #ifndef QT_OPENGL_ES_2
       
    53 #include <private/qpaintengine_opengl_p.h>
       
    54 #endif
    46 
    55 
    47 #include "qpixmapdata_x11gl_p.h"
    56 #include "qpixmapdata_x11gl_p.h"
    48 
    57 
    49 QT_BEGIN_NAMESPACE
    58 QT_BEGIN_NAMESPACE
    50 
    59 
    85 #if defined(QT_OPENGL_ES_2)
    94 #if defined(QT_OPENGL_ES_2)
    86             EGL_CONTEXT_CLIENT_VERSION, 2,
    95             EGL_CONTEXT_CLIENT_VERSION, 2,
    87 #endif
    96 #endif
    88             EGL_NONE
    97             EGL_NONE
    89         };
    98         };
    90         qPixmapARGBSharedEglContext = eglCreateContext(QEglContext::defaultDisplay(0),
    99         qPixmapARGBSharedEglContext = eglCreateContext(QEglContext::display(),
    91                                                        argbConfig, 0, contextAttribs);
   100                                                        argbConfig, 0, contextAttribs);
    92 
   101 
    93         if (argbConfig == rgbConfig) {
   102         if (argbConfig == rgbConfig) {
    94             // If the configs are the same, we can re-use the same context.
   103             // If the configs are the same, we can re-use the same context.
    95             qPixmapRGBSharedEglContext = qPixmapARGBSharedEglContext;
   104             qPixmapRGBSharedEglContext = qPixmapARGBSharedEglContext;
    96         } else {
   105         } else {
    97             qPixmapRGBSharedEglContext = eglCreateContext(QEglContext::defaultDisplay(0),
   106             qPixmapRGBSharedEglContext = eglCreateContext(QEglContext::display(),
    98                                                            rgbConfig, 0, contextAttribs);
   107                                                            rgbConfig, 0, contextAttribs);
    99         }
   108         }
   100 
   109 
   101         argbPixmapData = new QX11PixmapData(QPixmapData::PixmapType);
   110         argbPixmapData = new QX11PixmapData(QPixmapData::PixmapType);
   102         argbPixmapData->resize(100, 100);
   111         argbPixmapData->resize(100, 100);
   103         argbPixmapData->fill(Qt::transparent); // Force ARGB
   112         argbPixmapData->fill(Qt::transparent); // Force ARGB
   104 
   113 
   105         if (!qt_createEGLSurfaceForPixmap(argbPixmapData, false))
   114         if (!qt_createEGLSurfaceForPixmap(argbPixmapData, false))
   106             break;
   115             break;
   107 
   116 
   108         haveX11Pixmaps = eglMakeCurrent(QEglContext::defaultDisplay(0),
   117         haveX11Pixmaps = eglMakeCurrent(QEglContext::display(),
   109                                         (EGLSurface)argbPixmapData->gl_surface,
   118                                         (EGLSurface)argbPixmapData->gl_surface,
   110                                         (EGLSurface)argbPixmapData->gl_surface,
   119                                         (EGLSurface)argbPixmapData->gl_surface,
   111                                         qPixmapARGBSharedEglContext);
   120                                         qPixmapARGBSharedEglContext);
   112         if (!haveX11Pixmaps) {
   121         if (!haveX11Pixmaps) {
   113             EGLint err = eglGetError();
   122             EGLint err = eglGetError();
   123 
   132 
   124             // Try to actually create an EGL pixmap surface
   133             // Try to actually create an EGL pixmap surface
   125             if (!qt_createEGLSurfaceForPixmap(rgbPixmapData, false))
   134             if (!qt_createEGLSurfaceForPixmap(rgbPixmapData, false))
   126                 break;
   135                 break;
   127 
   136 
   128             haveX11Pixmaps = eglMakeCurrent(QEglContext::defaultDisplay(0),
   137             haveX11Pixmaps = eglMakeCurrent(QEglContext::display(),
   129                                             (EGLSurface)rgbPixmapData->gl_surface,
   138                                             (EGLSurface)rgbPixmapData->gl_surface,
   130                                             (EGLSurface)rgbPixmapData->gl_surface,
   139                                             (EGLSurface)rgbPixmapData->gl_surface,
   131                                             qPixmapRGBSharedEglContext);
   140                                             qPixmapRGBSharedEglContext);
   132             if (!haveX11Pixmaps) {
   141             if (!haveX11Pixmaps) {
   133                 EGLint err = eglGetError();
   142                 EGLint err = eglGetError();
   136             }
   145             }
   137         }
   146         }
   138     } while (0);
   147     } while (0);
   139 
   148 
   140     if (qPixmapARGBSharedEglContext || qPixmapRGBSharedEglContext) {
   149     if (qPixmapARGBSharedEglContext || qPixmapRGBSharedEglContext) {
   141         eglMakeCurrent(QEglContext::defaultDisplay(0),
   150         eglMakeCurrent(QEglContext::display(),
   142                        EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
   151                        EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
   143     }
   152     }
   144 
   153 
   145     if (argbPixmapData) {
   154     if (argbPixmapData) {
   146         if (argbPixmapData->gl_surface)
   155         if (argbPixmapData->gl_surface)
   156     }
   165     }
   157 
   166 
   158     if (!haveX11Pixmaps) {
   167     if (!haveX11Pixmaps) {
   159         // Clean up the context(s) if we can't use X11GL pixmaps
   168         // Clean up the context(s) if we can't use X11GL pixmaps
   160         if (qPixmapARGBSharedEglContext != EGL_NO_CONTEXT)
   169         if (qPixmapARGBSharedEglContext != EGL_NO_CONTEXT)
   161             eglDestroyContext(QEglContext::defaultDisplay(0), qPixmapARGBSharedEglContext);
   170             eglDestroyContext(QEglContext::display(), qPixmapARGBSharedEglContext);
   162 
   171 
   163         if (qPixmapRGBSharedEglContext != qPixmapARGBSharedEglContext &&
   172         if (qPixmapRGBSharedEglContext != qPixmapARGBSharedEglContext &&
   164             qPixmapRGBSharedEglContext != EGL_NO_CONTEXT)
   173             qPixmapRGBSharedEglContext != EGL_NO_CONTEXT)
   165         {
   174         {
   166             eglDestroyContext(QEglContext::defaultDisplay(0), qPixmapRGBSharedEglContext);
   175             eglDestroyContext(QEglContext::display(), qPixmapRGBSharedEglContext);
   167         }
   176         }
   168         qPixmapRGBSharedEglContext = EGL_NO_CONTEXT;
   177         qPixmapRGBSharedEglContext = EGL_NO_CONTEXT;
   169         qPixmapARGBSharedEglContext = EGL_NO_CONTEXT;
   178         qPixmapARGBSharedEglContext = EGL_NO_CONTEXT;
   170     }
   179     }
   171 
   180 
   185 
   194 
   186 QX11GLPixmapData::~QX11GLPixmapData()
   195 QX11GLPixmapData::~QX11GLPixmapData()
   187 {
   196 {
   188 }
   197 }
   189 
   198 
   190 static QGL2PaintEngineEx* qt_gl2_engine_for_pixmaps = 0;
   199 #if !defined(QT_OPENGL_ES_1) && !defined(QT_OPENGL_ES_1_CL)
       
   200 Q_GLOBAL_STATIC(QGL2PaintEngineEx, qt_gl_pixmap_2_engine)
       
   201 #endif
       
   202 
       
   203 #ifndef QT_OPENGL_ES_2
       
   204 Q_GLOBAL_STATIC(QOpenGLPaintEngine, qt_gl_pixmap_engine)
       
   205 #endif
       
   206 
   191 
   207 
   192 QPaintEngine* QX11GLPixmapData::paintEngine() const
   208 QPaintEngine* QX11GLPixmapData::paintEngine() const
   193 {
   209 {
   194     // We need to create the context before beginPaint - do it here:
   210     // We need to create the context before beginPaint - do it here:
   195     if (!ctx) {
   211     if (!ctx) {
   196         ctx = new QGLContext(glFormat());
   212         ctx = new QGLContext(glFormat());
   197         if (ctx->d_func()->eglContext == 0)
   213         if (ctx->d_func()->eglContext == 0)
   198             ctx->d_func()->eglContext = new QEglContext();
   214             ctx->d_func()->eglContext = new QEglContext();
   199         ctx->d_func()->eglContext->openDisplay(0); // ;-)
       
   200         ctx->d_func()->eglContext->setApi(QEgl::OpenGL);
   215         ctx->d_func()->eglContext->setApi(QEgl::OpenGL);
   201         ctx->d_func()->eglContext->setContext(hasAlphaChannel() ? qPixmapARGBSharedEglContext
   216         ctx->d_func()->eglContext->setContext(hasAlphaChannel() ? qPixmapARGBSharedEglContext
   202                                                                 : qPixmapRGBSharedEglContext);
   217                                                                 : qPixmapRGBSharedEglContext);
   203     }
   218     }
   204 
   219 
   205     if (!qt_gl2_engine_for_pixmaps)
   220     QPaintEngine* engine;
   206         qt_gl2_engine_for_pixmaps = new QGL2PaintEngineEx();
   221 
       
   222 #if defined(QT_OPENGL_ES_1) || defined(QT_OPENGL_ES_1_CL)
       
   223     engine = qt_gl_pixmap_engine();
       
   224 #elif defined(QT_OPENGL_ES_2)
       
   225     engine = qt_gl_pixmap_2_engine();
       
   226 #else
       
   227     if (qt_gl_preferGL2Engine())
       
   228         engine = qt_gl_pixmap_2_engine();
       
   229     else
       
   230         engine = qt_gl_pixmap_engine();
       
   231 #endif
       
   232 
       
   233 
   207 
   234 
   208     // Support multiple painters on multiple pixmaps simultaniously
   235     // Support multiple painters on multiple pixmaps simultaniously
   209     if (qt_gl2_engine_for_pixmaps->isActive()) {
   236     if (engine->isActive()) {
   210         qWarning("Pixmap paint engine already active");
   237         qWarning("Pixmap paint engine already active");
   211         QPaintEngine* engine = new QGL2PaintEngineEx();
   238 
       
   239 #if defined(QT_OPENGL_ES_1) || defined(QT_OPENGL_ES_1_CL)
       
   240         engine = new QOpenGLPaintEngine;
       
   241 #elif defined(QT_OPENGL_ES_2)
       
   242         engine = new QGL2PaintEngineEx;
       
   243 #else
       
   244         if (qt_gl_preferGL2Engine())
       
   245             engine = new QGL2PaintEngineEx;
       
   246         else
       
   247             engine = new QOpenGLPaintEngine;
       
   248 #endif
       
   249 
   212         engine->setAutoDestruct(true);
   250         engine->setAutoDestruct(true);
   213         return engine;
   251         return engine;
   214     }
   252     }
   215 
   253 
   216     return qt_gl2_engine_for_pixmaps;
   254     return engine;
   217 }
   255 }
   218 
   256 
   219 void QX11GLPixmapData::beginPaint()
   257 void QX11GLPixmapData::beginPaint()
   220 {
   258 {
   221 //    qDebug("QX11GLPixmapData::beginPaint()");
   259 //    qDebug("QX11GLPixmapData::beginPaint()");