src/opengl/qgl_qws.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 "qgl.h"
       
    43 #include "qgl_egl_p.h"
       
    44 #include "qglpixelbuffer.h"
       
    45 
       
    46 #include <qglscreen_qws.h>
       
    47 #include <qscreenproxy_qws.h>
       
    48 #include <private/qglwindowsurface_qws_p.h>
       
    49 
       
    50 #include <private/qbackingstore_p.h>
       
    51 #include <private/qfont_p.h>
       
    52 #include <private/qfontengine_p.h>
       
    53 #include <private/qgl_p.h>
       
    54 #include <private/qpaintengine_opengl_p.h>
       
    55 #include <qpixmap.h>
       
    56 #include <qtimer.h>
       
    57 #include <qapplication.h>
       
    58 #include <qstack.h>
       
    59 #include <qdesktopwidget.h>
       
    60 #include <qdebug.h>
       
    61 #include <qvarlengtharray.h>
       
    62 
       
    63 QT_BEGIN_NAMESPACE
       
    64 
       
    65 static QGLScreen *glScreenForDevice(QPaintDevice *device)
       
    66 {
       
    67     QScreen *screen = qt_screen;
       
    68     if (screen->classId() == QScreen::MultiClass) {
       
    69         int screenNumber;
       
    70         if (device && device->devType() == QInternal::Widget)
       
    71             screenNumber = qApp->desktop()->screenNumber(static_cast<QWidget *>(device));
       
    72         else
       
    73             screenNumber = 0;
       
    74         screen = screen->subScreens()[screenNumber];
       
    75     }
       
    76     while (screen->classId() == QScreen::ProxyClass ||
       
    77            screen->classId() == QScreen::TransformedClass) {
       
    78         screen = static_cast<QProxyScreen *>(screen)->screen();
       
    79     }
       
    80     if (screen->classId() == QScreen::GLClass)
       
    81         return static_cast<QGLScreen *>(screen);
       
    82     else
       
    83         return 0;
       
    84 }
       
    85 
       
    86 /*****************************************************************************
       
    87   QOpenGL debug facilities
       
    88  *****************************************************************************/
       
    89 //#define DEBUG_OPENGL_REGION_UPDATE
       
    90 
       
    91 bool QGLFormat::hasOpenGLOverlays()
       
    92 {
       
    93     QGLScreen *glScreen = glScreenForDevice(0);
       
    94     if (glScreen)
       
    95         return (glScreen->options() & QGLScreen::Overlays);
       
    96     else
       
    97         return false;
       
    98 }
       
    99 
       
   100 void qt_egl_add_platform_config(QEglProperties& props, QPaintDevice *device)
       
   101 {
       
   102     // Find the QGLScreen for this paint device.
       
   103     QGLScreen *glScreen = glScreenForDevice(device);
       
   104     if (!glScreen) {
       
   105         qWarning("QGLContext::chooseContext(): The screen is not a QGLScreen");
       
   106         return;
       
   107     }
       
   108     int devType = device->devType();
       
   109     if (devType == QInternal::Image)
       
   110         props.setPixelFormat(static_cast<QImage *>(device)->format());
       
   111     else
       
   112         props.setPixelFormat(glScreen->pixelFormat());
       
   113 }
       
   114 
       
   115 static EGLSurface qt_egl_create_surface
       
   116     (QEglContext *context, QPaintDevice *device,
       
   117      const QEglProperties *properties = 0)
       
   118 {
       
   119     // Get the screen surface functions, which are used to create native ids.
       
   120     QGLScreen *glScreen = glScreenForDevice(device);
       
   121     if (!glScreen)
       
   122         return EGL_NO_SURFACE;
       
   123     QGLScreenSurfaceFunctions *funcs = glScreen->surfaceFunctions();
       
   124     if (!funcs)
       
   125         return EGL_NO_SURFACE;
       
   126 
       
   127     // Create the native drawable for the paint device.
       
   128     int devType = device->devType();
       
   129     EGLNativePixmapType pixmapDrawable = 0;
       
   130     EGLNativeWindowType windowDrawable = 0;
       
   131     bool ok;
       
   132     if (devType == QInternal::Pixmap) {
       
   133         ok = funcs->createNativePixmap(static_cast<QPixmap *>(device), &pixmapDrawable);
       
   134     } else if (devType == QInternal::Image) {
       
   135         ok = funcs->createNativeImage(static_cast<QImage *>(device), &pixmapDrawable);
       
   136     } else {
       
   137         ok = funcs->createNativeWindow(static_cast<QWidget *>(device), &windowDrawable);
       
   138     }
       
   139     if (!ok) {
       
   140         qWarning("QEglContext::createSurface(): Cannot create the native EGL drawable");
       
   141         return EGL_NO_SURFACE;
       
   142     }
       
   143 
       
   144     // Create the EGL surface to draw into, based on the native drawable.
       
   145     const int *props;
       
   146     if (properties)
       
   147         props = properties->properties();
       
   148     else
       
   149         props = 0;
       
   150     EGLSurface surf;
       
   151     if (devType == QInternal::Widget) {
       
   152         surf = eglCreateWindowSurface
       
   153             (context->display(), context->config(), windowDrawable, props);
       
   154     } else {
       
   155         surf = eglCreatePixmapSurface
       
   156             (context->display(), context->config(), pixmapDrawable, props);
       
   157     }
       
   158     if (surf == EGL_NO_SURFACE)
       
   159         qWarning("QEglContext::createSurface(): Unable to create EGL surface, error = 0x%x", eglGetError());
       
   160     return surf;
       
   161 }
       
   162 
       
   163 bool QGLContext::chooseContext(const QGLContext* shareContext)
       
   164 {
       
   165     Q_D(QGLContext);
       
   166 
       
   167     // Validate the device.
       
   168     if (!device())
       
   169         return false;
       
   170     int devType = device()->devType();
       
   171     if (devType != QInternal::Pixmap && devType != QInternal::Image && devType != QInternal::Widget) {
       
   172         qWarning("QGLContext::chooseContext(): Cannot create QGLContext's for paint device type %d", devType);
       
   173         return false;
       
   174     }
       
   175 
       
   176     // Get the display and initialize it.
       
   177     d->eglContext = new QEglContext();
       
   178     d->eglContext->setApi(QEgl::OpenGL);
       
   179     if (!d->eglContext->openDisplay(device())) {
       
   180         delete d->eglContext;
       
   181         d->eglContext = 0;
       
   182         return false;
       
   183     }
       
   184 
       
   185     // Construct the configuration we need for this surface.
       
   186     QEglProperties configProps;
       
   187     qt_egl_add_platform_config(configProps, device());
       
   188     qt_egl_set_format(configProps, devType, d->glFormat);
       
   189     configProps.setRenderableType(QEgl::OpenGL);
       
   190 
       
   191     // Search for a matching configuration, reducing the complexity
       
   192     // each time until we get something that matches.
       
   193     if (!d->eglContext->chooseConfig(configProps)) {
       
   194         delete d->eglContext;
       
   195         d->eglContext = 0;
       
   196         return false;
       
   197     }
       
   198 
       
   199     // Inform the higher layers about the actual format properties.
       
   200     qt_egl_update_format(*(d->eglContext), d->glFormat);
       
   201 
       
   202     // Create a new context for the configuration.
       
   203     if (!d->eglContext->createContext
       
   204             (shareContext ? shareContext->d_func()->eglContext : 0)) {
       
   205         delete d->eglContext;
       
   206         d->eglContext = 0;
       
   207         return false;
       
   208     }
       
   209 
       
   210 #if defined(EGL_VERSION_1_1)
       
   211     if (d->glFormat.swapInterval() != -1 && devType == QInternal::Widget)
       
   212         eglSwapInterval(d->eglContext->display(), d->glFormat.swapInterval());
       
   213 #endif
       
   214 
       
   215     // Create the EGL surface to draw into.  We cannot use
       
   216     // QEglContext::createSurface() because it does not have
       
   217     // access to the QGLScreen.
       
   218     d->eglSurface = qt_egl_create_surface(d->eglContext, device());
       
   219     if (d->eglSurface == EGL_NO_SURFACE) {
       
   220         delete d->eglContext;
       
   221         d->eglContext = 0;
       
   222         return false;
       
   223     }
       
   224 
       
   225     return true;
       
   226 }
       
   227 
       
   228 
       
   229 bool QGLWidget::event(QEvent *e)
       
   230 {
       
   231     return QWidget::event(e);
       
   232 }
       
   233 
       
   234 
       
   235 void QGLWidget::resizeEvent(QResizeEvent *)
       
   236 {
       
   237     Q_D(QGLWidget);
       
   238     if (!isValid())
       
   239         return;
       
   240     makeCurrent();
       
   241     if (!d->glcx->initialized())
       
   242         glInit();
       
   243     resizeGL(width(), height());
       
   244     //handle overlay
       
   245 }
       
   246 
       
   247 const QGLContext* QGLWidget::overlayContext() const
       
   248 {
       
   249     return 0;
       
   250 }
       
   251 
       
   252 void QGLWidget::makeOverlayCurrent()
       
   253 {
       
   254     //handle overlay
       
   255 }
       
   256 
       
   257 void QGLWidget::updateOverlayGL()
       
   258 {
       
   259     //handle overlay
       
   260 }
       
   261 
       
   262 void QGLWidget::setContext(QGLContext *context, const QGLContext* shareContext, bool deleteOldContext)
       
   263 {
       
   264     Q_D(QGLWidget);
       
   265     if(context == 0) {
       
   266         qWarning("QGLWidget::setContext: Cannot set null context");
       
   267         return;
       
   268     }
       
   269 
       
   270     if(d->glcx)
       
   271         d->glcx->doneCurrent();
       
   272     QGLContext* oldcx = d->glcx;
       
   273     d->glcx = context;
       
   274     if(!d->glcx->isValid())
       
   275         d->glcx->create(shareContext ? shareContext : oldcx);
       
   276     if(deleteOldContext)
       
   277         delete oldcx;
       
   278 }
       
   279 
       
   280 void QGLWidgetPrivate::init(QGLContext *context, const QGLWidget* shareWidget)
       
   281 {
       
   282     Q_Q(QGLWidget);
       
   283 
       
   284     QGLScreen *glScreen = glScreenForDevice(q);
       
   285     if (glScreen) {
       
   286         wsurf = static_cast<QWSGLWindowSurface*>(glScreen->createSurface(q));
       
   287         q->setWindowSurface(wsurf);
       
   288     }
       
   289 
       
   290     initContext(context, shareWidget);
       
   291 
       
   292     if(q->isValid() && glcx->format().hasOverlay()) {
       
   293         //no overlay
       
   294         qWarning("QtOpenGL ES doesn't currently support overlays");
       
   295     }
       
   296 }
       
   297 
       
   298 void QGLWidgetPrivate::cleanupColormaps()
       
   299 {
       
   300 }
       
   301 
       
   302 const QGLColormap & QGLWidget::colormap() const
       
   303 {
       
   304     return d_func()->cmap;
       
   305 }
       
   306 
       
   307 void QGLWidget::setColormap(const QGLColormap &)
       
   308 {
       
   309 }
       
   310 
       
   311 void QGLExtensions::init()
       
   312 {
       
   313     static bool init_done = false;
       
   314 
       
   315     if (init_done)
       
   316         return;
       
   317     init_done = true;
       
   318 
       
   319     // We need a context current to initialize the extensions,
       
   320     // but getting a valid EGLNativeWindowType this early can be
       
   321     // problematic under QWS.  So use a pbuffer instead.
       
   322     //
       
   323     // Unfortunately OpenGL/ES 2.0 systems don't normally
       
   324     // support pbuffers, so we have no choice but to try
       
   325     // our luck with a window on those systems.
       
   326 #if defined(QT_OPENGL_ES_2)
       
   327     QGLWidget tmpWidget;
       
   328     tmpWidget.makeCurrent();
       
   329 
       
   330     init_extensions();
       
   331 
       
   332     tmpWidget.doneCurrent();
       
   333 #else
       
   334     QGLPixelBuffer pbuffer(16, 16);
       
   335     pbuffer.makeCurrent();
       
   336 
       
   337     init_extensions();
       
   338 
       
   339     pbuffer.doneCurrent();
       
   340 #endif
       
   341 }
       
   342 
       
   343 QT_END_NAMESPACE