src/opengl/qpixmapdata_x11gl_egl.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 <private/qgl_p.h>
       
    43 #include <private/qegl_p.h>
       
    44 #include <private/qeglproperties_p.h>
       
    45 #include <private/qpaintengineex_opengl2_p.h>
       
    46 
       
    47 #include "qpixmapdata_x11gl_p.h"
       
    48 
       
    49 QT_BEGIN_NAMESPACE
       
    50 
       
    51 extern EGLConfig qt_chooseEGLConfigForPixmap(bool hasAlpha, bool readOnly); // in qgl_x11egl.cpp
       
    52 extern bool qt_createEGLSurfaceForPixmap(QPixmapData* pmd, bool readOnly); // in qgl_x11egl.cpp
       
    53 
       
    54 // On 16bpp systems, RGB & ARGB pixmaps are different bit-depths and therefore need
       
    55 // different contexts:
       
    56 static EGLContext qPixmapARGBSharedEglContext = EGL_NO_CONTEXT;
       
    57 static EGLContext qPixmapRGBSharedEglContext = EGL_NO_CONTEXT;
       
    58 
       
    59 bool QX11GLPixmapData::hasX11GLPixmaps()
       
    60 {
       
    61     static bool checkedForX11Pixmaps = false;
       
    62     static bool haveX11Pixmaps = false;
       
    63 
       
    64     if (checkedForX11Pixmaps)
       
    65         return haveX11Pixmaps;
       
    66 
       
    67     checkedForX11Pixmaps = true;
       
    68 
       
    69     QX11PixmapData *argbPixmapData = 0;
       
    70     QX11PixmapData *rgbPixmapData = 0;
       
    71     do {
       
    72         if (qgetenv("QT_USE_X11GL_PIXMAPS").isEmpty())
       
    73             break;
       
    74 
       
    75         // Check we actually have EGL configs which support pixmaps
       
    76         EGLConfig argbConfig = qt_chooseEGLConfigForPixmap(true, false);
       
    77         EGLConfig rgbConfig = qt_chooseEGLConfigForPixmap(false, false);
       
    78 
       
    79         if (argbConfig == 0 || rgbConfig == 0)
       
    80             break;
       
    81 
       
    82         // Create the shared contexts:
       
    83         eglBindAPI(EGL_OPENGL_ES_API);
       
    84         EGLint contextAttribs[] = {
       
    85 #if defined(QT_OPENGL_ES_2)
       
    86             EGL_CONTEXT_CLIENT_VERSION, 2,
       
    87 #endif
       
    88             EGL_NONE
       
    89         };
       
    90         qPixmapARGBSharedEglContext = eglCreateContext(QEglContext::defaultDisplay(0),
       
    91                                                        argbConfig, 0, contextAttribs);
       
    92 
       
    93         if (argbConfig == rgbConfig) {
       
    94             // If the configs are the same, we can re-use the same context.
       
    95             qPixmapRGBSharedEglContext = qPixmapARGBSharedEglContext;
       
    96         } else {
       
    97             qPixmapRGBSharedEglContext = eglCreateContext(QEglContext::defaultDisplay(0),
       
    98                                                            rgbConfig, 0, contextAttribs);
       
    99         }
       
   100 
       
   101         argbPixmapData = new QX11PixmapData(QPixmapData::PixmapType);
       
   102         argbPixmapData->resize(100, 100);
       
   103         argbPixmapData->fill(Qt::transparent); // Force ARGB
       
   104 
       
   105         if (!qt_createEGLSurfaceForPixmap(argbPixmapData, false))
       
   106             break;
       
   107 
       
   108         haveX11Pixmaps = eglMakeCurrent(QEglContext::defaultDisplay(0),
       
   109                                         (EGLSurface)argbPixmapData->gl_surface,
       
   110                                         (EGLSurface)argbPixmapData->gl_surface,
       
   111                                         qPixmapARGBSharedEglContext);
       
   112         if (!haveX11Pixmaps) {
       
   113             EGLint err = eglGetError();
       
   114             qWarning() << "Unable to make pixmap config current:" << err << QEglContext::errorString(err);
       
   115             break;
       
   116         }
       
   117 
       
   118         // If the ARGB & RGB configs are the same, we don't need to check RGB too
       
   119         if (haveX11Pixmaps && (argbConfig != rgbConfig)) {
       
   120             rgbPixmapData = new QX11PixmapData(QPixmapData::PixmapType);
       
   121             rgbPixmapData->resize(100, 100);
       
   122             rgbPixmapData->fill(Qt::red);
       
   123 
       
   124             // Try to actually create an EGL pixmap surface
       
   125             if (!qt_createEGLSurfaceForPixmap(rgbPixmapData, false))
       
   126                 break;
       
   127 
       
   128             haveX11Pixmaps = eglMakeCurrent(QEglContext::defaultDisplay(0),
       
   129                                             (EGLSurface)rgbPixmapData->gl_surface,
       
   130                                             (EGLSurface)rgbPixmapData->gl_surface,
       
   131                                             qPixmapRGBSharedEglContext);
       
   132             if (!haveX11Pixmaps) {
       
   133                 EGLint err = eglGetError();
       
   134                 qWarning() << "Unable to make pixmap config current:" << err << QEglContext::errorString(err);
       
   135                 break;
       
   136             }
       
   137         }
       
   138     } while (0);
       
   139 
       
   140     if (qPixmapARGBSharedEglContext || qPixmapRGBSharedEglContext) {
       
   141         eglMakeCurrent(QEglContext::defaultDisplay(0),
       
   142                        EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
       
   143     }
       
   144 
       
   145     if (argbPixmapData) {
       
   146         if (argbPixmapData->gl_surface)
       
   147             QGLContextPrivate::destroyGlSurfaceForPixmap(argbPixmapData);
       
   148         delete argbPixmapData;
       
   149         argbPixmapData = 0;
       
   150     }
       
   151     if (rgbPixmapData) {
       
   152         if (rgbPixmapData->gl_surface)
       
   153             QGLContextPrivate::destroyGlSurfaceForPixmap(rgbPixmapData);
       
   154         delete rgbPixmapData;
       
   155         rgbPixmapData = 0;
       
   156     }
       
   157 
       
   158     if (!haveX11Pixmaps) {
       
   159         // Clean up the context(s) if we can't use X11GL pixmaps
       
   160         if (qPixmapARGBSharedEglContext != EGL_NO_CONTEXT)
       
   161             eglDestroyContext(QEglContext::defaultDisplay(0), qPixmapARGBSharedEglContext);
       
   162 
       
   163         if (qPixmapRGBSharedEglContext != qPixmapARGBSharedEglContext &&
       
   164             qPixmapRGBSharedEglContext != EGL_NO_CONTEXT)
       
   165         {
       
   166             eglDestroyContext(QEglContext::defaultDisplay(0), qPixmapRGBSharedEglContext);
       
   167         }
       
   168         qPixmapRGBSharedEglContext = EGL_NO_CONTEXT;
       
   169         qPixmapARGBSharedEglContext = EGL_NO_CONTEXT;
       
   170     }
       
   171 
       
   172     if (haveX11Pixmaps)
       
   173         qDebug("QX11GLPixmapData is supported");
       
   174     else
       
   175         qDebug("QX11GLPixmapData is *NOT* being used");
       
   176 
       
   177     return haveX11Pixmaps;
       
   178 }
       
   179 
       
   180 QX11GLPixmapData::QX11GLPixmapData()
       
   181     : QX11PixmapData(QPixmapData::PixmapType),
       
   182       ctx(0)
       
   183 {
       
   184 }
       
   185 
       
   186 QX11GLPixmapData::~QX11GLPixmapData()
       
   187 {
       
   188 }
       
   189 
       
   190 static QGL2PaintEngineEx* qt_gl2_engine_for_pixmaps = 0;
       
   191 
       
   192 QPaintEngine* QX11GLPixmapData::paintEngine() const
       
   193 {
       
   194     // We need to create the context before beginPaint - do it here:
       
   195     if (!ctx) {
       
   196         ctx = new QGLContext(glFormat());
       
   197         if (ctx->d_func()->eglContext == 0)
       
   198             ctx->d_func()->eglContext = new QEglContext();
       
   199         ctx->d_func()->eglContext->openDisplay(0); // ;-)
       
   200         ctx->d_func()->eglContext->setApi(QEgl::OpenGL);
       
   201         ctx->d_func()->eglContext->setContext(hasAlphaChannel() ? qPixmapARGBSharedEglContext
       
   202                                                                 : qPixmapRGBSharedEglContext);
       
   203     }
       
   204 
       
   205     if (!qt_gl2_engine_for_pixmaps)
       
   206         qt_gl2_engine_for_pixmaps = new QGL2PaintEngineEx();
       
   207 
       
   208     // Support multiple painters on multiple pixmaps simultaniously
       
   209     if (qt_gl2_engine_for_pixmaps->isActive()) {
       
   210         qWarning("Pixmap paint engine already active");
       
   211         QPaintEngine* engine = new QGL2PaintEngineEx();
       
   212         engine->setAutoDestruct(true);
       
   213         return engine;
       
   214     }
       
   215 
       
   216     return qt_gl2_engine_for_pixmaps;
       
   217 }
       
   218 
       
   219 void QX11GLPixmapData::beginPaint()
       
   220 {
       
   221 //    qDebug("QX11GLPixmapData::beginPaint()");
       
   222     if ((EGLSurface)gl_surface == EGL_NO_SURFACE) {
       
   223         qt_createEGLSurfaceForPixmap(this, false);
       
   224         ctx->d_func()->eglSurface = (EGLSurface)gl_surface;
       
   225         ctx->d_func()->valid = true; // ;-)
       
   226     }
       
   227     QGLPaintDevice::beginPaint();
       
   228 }
       
   229 
       
   230 void QX11GLPixmapData::endPaint()
       
   231 {
       
   232     glFinish();
       
   233     QGLPaintDevice::endPaint();
       
   234 }
       
   235 
       
   236 QGLContext* QX11GLPixmapData::context() const
       
   237 {
       
   238     return ctx;
       
   239 }
       
   240 
       
   241 QSize QX11GLPixmapData::size() const
       
   242 {
       
   243     return QSize(w, h);
       
   244 }
       
   245 
       
   246 
       
   247 QGLFormat QX11GLPixmapData::glFormat()
       
   248 {
       
   249     return QGLFormat::defaultFormat(); //###
       
   250 }
       
   251 
       
   252 QT_END_NAMESPACE