| 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 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 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 | if (!d->eglContext->openDisplay(device())) { | 
|---|
| 202 | delete d->eglContext; | 
|---|
| 203 | d->eglContext = 0; | 
|---|
| 204 | return false; | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | // Construct the configuration we need for this surface. | 
|---|
| 208 | QEglProperties configProps; | 
|---|
| 209 | qt_egl_add_platform_config(configProps, device()); | 
|---|
| 210 | qt_egl_set_format(configProps, devType, d->glFormat); | 
|---|
| 211 | configProps.setRenderableType(QEgl::OpenGL); | 
|---|
| 212 |  | 
|---|
| 213 | // Search for a matching configuration, reducing the complexity | 
|---|
| 214 | // each time until we get something that matches. | 
|---|
| 215 | if (!d->eglContext->chooseConfig(configProps)) { | 
|---|
| 216 | delete d->eglContext; | 
|---|
| 217 | d->eglContext = 0; | 
|---|
| 218 | return false; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | // Inform the higher layers about the actual format properties. | 
|---|
| 222 | qt_egl_update_format(*(d->eglContext), d->glFormat); | 
|---|
| 223 |  | 
|---|
| 224 | // Create a new context for the configuration. | 
|---|
| 225 | if (!d->eglContext->createContext | 
|---|
| 226 | (shareContext ? shareContext->d_func()->eglContext : 0)) { | 
|---|
| 227 | delete d->eglContext; | 
|---|
| 228 | d->eglContext = 0; | 
|---|
| 229 | return false; | 
|---|
| 230 | } | 
|---|
| 231 | d->sharing = d->eglContext->isSharing(); | 
|---|
| 232 | if (d->sharing && shareContext) | 
|---|
| 233 | const_cast<QGLContext *>(shareContext)->d_func()->sharing = true; | 
|---|
| 234 |  | 
|---|
| 235 | #if defined(EGL_VERSION_1_1) | 
|---|
| 236 | if (d->glFormat.swapInterval() != -1 && devType == QInternal::Widget) | 
|---|
| 237 | eglSwapInterval(d->eglContext->display(), d->glFormat.swapInterval()); | 
|---|
| 238 | #endif | 
|---|
| 239 |  | 
|---|
| 240 | // Create the EGL surface to draw into.  We cannot use | 
|---|
| 241 | // QEglContext::createSurface() because it does not have | 
|---|
| 242 | // access to the QGLScreen. | 
|---|
| 243 | d->eglSurface = qt_egl_create_surface(d->eglContext, device()); | 
|---|
| 244 | if (d->eglSurface == EGL_NO_SURFACE) { | 
|---|
| 245 | delete d->eglContext; | 
|---|
| 246 | d->eglContext = 0; | 
|---|
| 247 | return false; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | return true; | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 |  | 
|---|
| 254 | bool QGLWidget::event(QEvent *e) | 
|---|
| 255 | { | 
|---|
| 256 | return QWidget::event(e); | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 |  | 
|---|
| 260 | void QGLWidget::resizeEvent(QResizeEvent *) | 
|---|
| 261 | { | 
|---|
| 262 | Q_D(QGLWidget); | 
|---|
| 263 | if (!isValid()) | 
|---|
| 264 | return; | 
|---|
| 265 | makeCurrent(); | 
|---|
| 266 | if (!d->glcx->initialized()) | 
|---|
| 267 | glInit(); | 
|---|
| 268 | resizeGL(width(), height()); | 
|---|
| 269 | //handle overlay | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | const QGLContext* QGLWidget::overlayContext() const | 
|---|
| 273 | { | 
|---|
| 274 | return 0; | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | void QGLWidget::makeOverlayCurrent() | 
|---|
| 278 | { | 
|---|
| 279 | //handle overlay | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | void QGLWidget::updateOverlayGL() | 
|---|
| 283 | { | 
|---|
| 284 | //handle overlay | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 | void QGLWidget::setContext(QGLContext *context, const QGLContext* shareContext, bool deleteOldContext) | 
|---|
| 288 | { | 
|---|
| 289 | Q_D(QGLWidget); | 
|---|
| 290 | if(context == 0) { | 
|---|
| 291 | qWarning("QGLWidget::setContext: Cannot set null context"); | 
|---|
| 292 | return; | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | if(d->glcx) | 
|---|
| 296 | d->glcx->doneCurrent(); | 
|---|
| 297 | QGLContext* oldcx = d->glcx; | 
|---|
| 298 | d->glcx = context; | 
|---|
| 299 | if(!d->glcx->isValid()) | 
|---|
| 300 | d->glcx->create(shareContext ? shareContext : oldcx); | 
|---|
| 301 | if(deleteOldContext) | 
|---|
| 302 | delete oldcx; | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | void QGLWidgetPrivate::init(QGLContext *context, const QGLWidget* shareWidget) | 
|---|
| 306 | { | 
|---|
| 307 | Q_Q(QGLWidget); | 
|---|
| 308 |  | 
|---|
| 309 | QGLScreen *glScreen = glScreenForDevice(q); | 
|---|
| 310 | if (glScreen) { | 
|---|
| 311 | wsurf = static_cast<QWSGLWindowSurface*>(glScreen->createSurface(q)); | 
|---|
| 312 | q->setWindowSurface(wsurf); | 
|---|
| 313 | } | 
|---|
| 314 |  | 
|---|
| 315 | initContext(context, shareWidget); | 
|---|
| 316 |  | 
|---|
| 317 | if(q->isValid() && glcx->format().hasOverlay()) { | 
|---|
| 318 | //no overlay | 
|---|
| 319 | qWarning("QtOpenGL ES doesn't currently support overlays"); | 
|---|
| 320 | } | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | void QGLWidgetPrivate::cleanupColormaps() | 
|---|
| 324 | { | 
|---|
| 325 | } | 
|---|
| 326 |  | 
|---|
| 327 | const QGLColormap & QGLWidget::colormap() const | 
|---|
| 328 | { | 
|---|
| 329 | return d_func()->cmap; | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | void QGLWidget::setColormap(const QGLColormap &) | 
|---|
| 333 | { | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | QT_END_NAMESPACE | 
|---|