util/src/opengl/qgl_qws.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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     QGLTemporaryContext implementation
       
    88 */
       
    89 
       
    90 class QGLTemporaryContextPrivate
       
    91 {
       
    92 public:
       
    93     QGLWidget *widget;
       
    94 };
       
    95 
       
    96 QGLTemporaryContext::QGLTemporaryContext(bool, QWidget *)
       
    97     : d(new QGLTemporaryContextPrivate)
       
    98 {
       
    99     d->widget = new QGLWidget;
       
   100     d->widget->makeCurrent();
       
   101 }
       
   102 
       
   103 QGLTemporaryContext::~QGLTemporaryContext()
       
   104 {
       
   105     delete d->widget;
       
   106 }
       
   107 
       
   108 /*****************************************************************************
       
   109   QOpenGL debug facilities
       
   110  *****************************************************************************/
       
   111 //#define DEBUG_OPENGL_REGION_UPDATE
       
   112 
       
   113 bool QGLFormat::hasOpenGLOverlays()
       
   114 {
       
   115     QGLScreen *glScreen = glScreenForDevice(0);
       
   116     if (glScreen)
       
   117         return (glScreen->options() & QGLScreen::Overlays);
       
   118     else
       
   119         return false;
       
   120 }
       
   121 
       
   122 void qt_egl_add_platform_config(QEglProperties& props, QPaintDevice *device)
       
   123 {
       
   124     // Find the QGLScreen for this paint device.
       
   125     QGLScreen *glScreen = glScreenForDevice(device);
       
   126     if (!glScreen) {
       
   127         qWarning("QGLContext::chooseContext(): The screen is not a QGLScreen");
       
   128         return;
       
   129     }
       
   130     int devType = device->devType();
       
   131     if (devType == QInternal::Image)
       
   132         props.setPixelFormat(static_cast<QImage *>(device)->format());
       
   133     else
       
   134         props.setPixelFormat(glScreen->pixelFormat());
       
   135 }
       
   136 
       
   137 static EGLSurface qt_egl_create_surface
       
   138     (QEglContext *context, QPaintDevice *device,
       
   139      const QEglProperties *properties = 0)
       
   140 {
       
   141     // Get the screen surface functions, which are used to create native ids.
       
   142     QGLScreen *glScreen = glScreenForDevice(device);
       
   143     if (!glScreen)
       
   144         return EGL_NO_SURFACE;
       
   145     QGLScreenSurfaceFunctions *funcs = glScreen->surfaceFunctions();
       
   146     if (!funcs)
       
   147         return EGL_NO_SURFACE;
       
   148 
       
   149     // Create the native drawable for the paint device.
       
   150     int devType = device->devType();
       
   151     EGLNativePixmapType pixmapDrawable = 0;
       
   152     EGLNativeWindowType windowDrawable = 0;
       
   153     bool ok;
       
   154     if (devType == QInternal::Pixmap) {
       
   155         ok = funcs->createNativePixmap(static_cast<QPixmap *>(device), &pixmapDrawable);
       
   156     } else if (devType == QInternal::Image) {
       
   157         ok = funcs->createNativeImage(static_cast<QImage *>(device), &pixmapDrawable);
       
   158     } else {
       
   159         ok = funcs->createNativeWindow(static_cast<QWidget *>(device), &windowDrawable);
       
   160     }
       
   161     if (!ok) {
       
   162         qWarning("QEglContext::createSurface(): Cannot create the native EGL drawable");
       
   163         return EGL_NO_SURFACE;
       
   164     }
       
   165 
       
   166     // Create the EGL surface to draw into, based on the native drawable.
       
   167     const int *props;
       
   168     if (properties)
       
   169         props = properties->properties();
       
   170     else
       
   171         props = 0;
       
   172     EGLSurface surf;
       
   173     if (devType == QInternal::Widget) {
       
   174         surf = eglCreateWindowSurface
       
   175             (context->display(), context->config(), windowDrawable, props);
       
   176     } else {
       
   177         surf = eglCreatePixmapSurface
       
   178             (context->display(), context->config(), pixmapDrawable, props);
       
   179     }
       
   180     if (surf == EGL_NO_SURFACE)
       
   181         qWarning("QEglContext::createSurface(): Unable to create EGL surface, error = 0x%x", eglGetError());
       
   182     return surf;
       
   183 }
       
   184 
       
   185 bool QGLContext::chooseContext(const QGLContext* shareContext)
       
   186 {
       
   187     Q_D(QGLContext);
       
   188 
       
   189     // Validate the device.
       
   190     if (!device())
       
   191         return false;
       
   192     int devType = device()->devType();
       
   193     if (devType != QInternal::Pixmap && devType != QInternal::Image && devType != QInternal::Widget) {
       
   194         qWarning("QGLContext::chooseContext(): Cannot create QGLContext's for paint device type %d", devType);
       
   195         return false;
       
   196     }
       
   197 
       
   198     // Get the display and initialize it.
       
   199     d->eglContext = new QEglContext();
       
   200     d->eglContext->setApi(QEgl::OpenGL);
       
   201 
       
   202     // Construct the configuration we need for this surface.
       
   203     QEglProperties configProps;
       
   204     qt_egl_add_platform_config(configProps, device());
       
   205     qt_egl_set_format(configProps, devType, d->glFormat);
       
   206     configProps.setRenderableType(QEgl::OpenGL);
       
   207 
       
   208     // Search for a matching configuration, reducing the complexity
       
   209     // each time until we get something that matches.
       
   210     if (!d->eglContext->chooseConfig(configProps)) {
       
   211         delete d->eglContext;
       
   212         d->eglContext = 0;
       
   213         return false;
       
   214     }
       
   215 
       
   216     // Inform the higher layers about the actual format properties.
       
   217     qt_egl_update_format(*(d->eglContext), d->glFormat);
       
   218 
       
   219     // Create a new context for the configuration.
       
   220     if (!d->eglContext->createContext
       
   221             (shareContext ? shareContext->d_func()->eglContext : 0)) {
       
   222         delete d->eglContext;
       
   223         d->eglContext = 0;
       
   224         return false;
       
   225     }
       
   226     d->sharing = d->eglContext->isSharing();
       
   227     if (d->sharing && shareContext)
       
   228         const_cast<QGLContext *>(shareContext)->d_func()->sharing = true;
       
   229 
       
   230 #if defined(EGL_VERSION_1_1)
       
   231     if (d->glFormat.swapInterval() != -1 && devType == QInternal::Widget)
       
   232         eglSwapInterval(d->eglContext->display(), d->glFormat.swapInterval());
       
   233 #endif
       
   234 
       
   235     // Create the EGL surface to draw into.  We cannot use
       
   236     // QEglContext::createSurface() because it does not have
       
   237     // access to the QGLScreen.
       
   238     d->eglSurface = qt_egl_create_surface(d->eglContext, device());
       
   239     if (d->eglSurface == EGL_NO_SURFACE) {
       
   240         delete d->eglContext;
       
   241         d->eglContext = 0;
       
   242         return false;
       
   243     }
       
   244 
       
   245     return true;
       
   246 }
       
   247 
       
   248 
       
   249 bool QGLWidget::event(QEvent *e)
       
   250 {
       
   251     return QWidget::event(e);
       
   252 }
       
   253 
       
   254 
       
   255 void QGLWidget::resizeEvent(QResizeEvent *)
       
   256 {
       
   257     Q_D(QGLWidget);
       
   258     if (!isValid())
       
   259         return;
       
   260     makeCurrent();
       
   261     if (!d->glcx->initialized())
       
   262         glInit();
       
   263     resizeGL(width(), height());
       
   264     //handle overlay
       
   265 }
       
   266 
       
   267 const QGLContext* QGLWidget::overlayContext() const
       
   268 {
       
   269     return 0;
       
   270 }
       
   271 
       
   272 void QGLWidget::makeOverlayCurrent()
       
   273 {
       
   274     //handle overlay
       
   275 }
       
   276 
       
   277 void QGLWidget::updateOverlayGL()
       
   278 {
       
   279     //handle overlay
       
   280 }
       
   281 
       
   282 void QGLWidget::setContext(QGLContext *context, const QGLContext* shareContext, bool deleteOldContext)
       
   283 {
       
   284     Q_D(QGLWidget);
       
   285     if(context == 0) {
       
   286         qWarning("QGLWidget::setContext: Cannot set null context");
       
   287         return;
       
   288     }
       
   289 
       
   290     if(d->glcx)
       
   291         d->glcx->doneCurrent();
       
   292     QGLContext* oldcx = d->glcx;
       
   293     d->glcx = context;
       
   294     if(!d->glcx->isValid())
       
   295         d->glcx->create(shareContext ? shareContext : oldcx);
       
   296     if(deleteOldContext)
       
   297         delete oldcx;
       
   298 }
       
   299 
       
   300 void QGLWidgetPrivate::init(QGLContext *context, const QGLWidget* shareWidget)
       
   301 {
       
   302     Q_Q(QGLWidget);
       
   303 
       
   304     QGLScreen *glScreen = glScreenForDevice(q);
       
   305     if (glScreen) {
       
   306         wsurf = static_cast<QWSGLWindowSurface*>(glScreen->createSurface(q));
       
   307         q->setWindowSurface(wsurf);
       
   308     }
       
   309 
       
   310     initContext(context, shareWidget);
       
   311 
       
   312     if(q->isValid() && glcx->format().hasOverlay()) {
       
   313         //no overlay
       
   314         qWarning("QtOpenGL ES doesn't currently support overlays");
       
   315     }
       
   316 }
       
   317 
       
   318 void QGLWidgetPrivate::cleanupColormaps()
       
   319 {
       
   320 }
       
   321 
       
   322 const QGLColormap & QGLWidget::colormap() const
       
   323 {
       
   324     return d_func()->cmap;
       
   325 }
       
   326 
       
   327 void QGLWidget::setColormap(const QGLColormap &)
       
   328 {
       
   329 }
       
   330 
       
   331 QT_END_NAMESPACE