| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 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 | #ifndef QGL_P_H | 
|---|
| 43 | #define QGL_P_H | 
|---|
| 44 |  | 
|---|
| 45 | // | 
|---|
| 46 | //  W A R N I N G | 
|---|
| 47 | //  ------------- | 
|---|
| 48 | // | 
|---|
| 49 | // This file is not part of the Qt API.  It exists for the convenience | 
|---|
| 50 | // of the QGLWidget class.  This header file may change from | 
|---|
| 51 | // version to version without notice, or even be removed. | 
|---|
| 52 | // | 
|---|
| 53 | // We mean it. | 
|---|
| 54 | // | 
|---|
| 55 |  | 
|---|
| 56 | #include "QtOpenGL/qgl.h" | 
|---|
| 57 | #include "QtOpenGL/qglcolormap.h" | 
|---|
| 58 | #include "QtCore/qmap.h" | 
|---|
| 59 | #include "QtCore/qthread.h" | 
|---|
| 60 | #include "QtCore/qthreadstorage.h" | 
|---|
| 61 | #include "QtCore/qhash.h" | 
|---|
| 62 | #include "QtCore/qatomic.h" | 
|---|
| 63 | #include "private/qwidget_p.h" | 
|---|
| 64 | #include "qcache.h" | 
|---|
| 65 | #include "qglpaintdevice_p.h" | 
|---|
| 66 |  | 
|---|
| 67 | #ifndef QT_NO_EGL | 
|---|
| 68 | #include <QtGui/private/qegl_p.h> | 
|---|
| 69 | #endif | 
|---|
| 70 |  | 
|---|
| 71 | QT_BEGIN_NAMESPACE | 
|---|
| 72 |  | 
|---|
| 73 | class QGLContext; | 
|---|
| 74 | class QGLOverlayWidget; | 
|---|
| 75 | class QPixmap; | 
|---|
| 76 | class QPixmapFilter; | 
|---|
| 77 | #ifdef Q_WS_MAC | 
|---|
| 78 | # ifdef qDebug | 
|---|
| 79 | #   define old_qDebug qDebug | 
|---|
| 80 | #   undef qDebug | 
|---|
| 81 | # endif | 
|---|
| 82 | QT_BEGIN_INCLUDE_NAMESPACE | 
|---|
| 83 | #ifndef QT_MAC_USE_COCOA | 
|---|
| 84 | # include <AGL/agl.h> | 
|---|
| 85 | #endif | 
|---|
| 86 | QT_END_INCLUDE_NAMESPACE | 
|---|
| 87 | # ifdef old_qDebug | 
|---|
| 88 | #   undef qDebug | 
|---|
| 89 | #   define qDebug QT_NO_QDEBUG_MACRO | 
|---|
| 90 | #   undef old_qDebug | 
|---|
| 91 | # endif | 
|---|
| 92 | class QMacWindowChangeEvent; | 
|---|
| 93 | #endif | 
|---|
| 94 |  | 
|---|
| 95 | #ifdef Q_WS_QWS | 
|---|
| 96 | class QWSGLWindowSurface; | 
|---|
| 97 | #endif | 
|---|
| 98 |  | 
|---|
| 99 | #ifndef QT_NO_EGL | 
|---|
| 100 | class QEglContext; | 
|---|
| 101 | #endif | 
|---|
| 102 |  | 
|---|
| 103 | QT_BEGIN_INCLUDE_NAMESPACE | 
|---|
| 104 | #include <QtOpenGL/private/qglextensions_p.h> | 
|---|
| 105 | QT_END_INCLUDE_NAMESPACE | 
|---|
| 106 |  | 
|---|
| 107 | class QGLFormatPrivate | 
|---|
| 108 | { | 
|---|
| 109 | public: | 
|---|
| 110 | QGLFormatPrivate() | 
|---|
| 111 | : ref(1) | 
|---|
| 112 | { | 
|---|
| 113 | opts = QGL::DoubleBuffer | QGL::DepthBuffer | QGL::Rgba | QGL::DirectRendering | 
|---|
| 114 | | QGL::StencilBuffer | QGL::DeprecatedFunctions; | 
|---|
| 115 | pln = 0; | 
|---|
| 116 | depthSize = accumSize = stencilSize = redSize = greenSize = blueSize = alphaSize = -1; | 
|---|
| 117 | numSamples = -1; | 
|---|
| 118 | swapInterval = -1; | 
|---|
| 119 | majorVersion = 1; | 
|---|
| 120 | minorVersion = 0; | 
|---|
| 121 | profile = QGLFormat::NoProfile; | 
|---|
| 122 | } | 
|---|
| 123 | QGLFormatPrivate(const QGLFormatPrivate *other) | 
|---|
| 124 | : ref(1), | 
|---|
| 125 | opts(other->opts), | 
|---|
| 126 | pln(other->pln), | 
|---|
| 127 | depthSize(other->depthSize), | 
|---|
| 128 | accumSize(other->accumSize), | 
|---|
| 129 | stencilSize(other->stencilSize), | 
|---|
| 130 | redSize(other->redSize), | 
|---|
| 131 | greenSize(other->greenSize), | 
|---|
| 132 | blueSize(other->blueSize), | 
|---|
| 133 | alphaSize(other->alphaSize), | 
|---|
| 134 | numSamples(other->numSamples), | 
|---|
| 135 | swapInterval(other->swapInterval), | 
|---|
| 136 | majorVersion(other->majorVersion), | 
|---|
| 137 | minorVersion(other->minorVersion), | 
|---|
| 138 | profile(other->profile) | 
|---|
| 139 | { | 
|---|
| 140 | } | 
|---|
| 141 | QAtomicInt ref; | 
|---|
| 142 | QGL::FormatOptions opts; | 
|---|
| 143 | int pln; | 
|---|
| 144 | int depthSize; | 
|---|
| 145 | int accumSize; | 
|---|
| 146 | int stencilSize; | 
|---|
| 147 | int redSize; | 
|---|
| 148 | int greenSize; | 
|---|
| 149 | int blueSize; | 
|---|
| 150 | int alphaSize; | 
|---|
| 151 | int numSamples; | 
|---|
| 152 | int swapInterval; | 
|---|
| 153 | int majorVersion; | 
|---|
| 154 | int minorVersion; | 
|---|
| 155 | QGLFormat::OpenGLContextProfile profile; | 
|---|
| 156 | }; | 
|---|
| 157 |  | 
|---|
| 158 | class QGLWidgetPrivate : public QWidgetPrivate | 
|---|
| 159 | { | 
|---|
| 160 | Q_DECLARE_PUBLIC(QGLWidget) | 
|---|
| 161 | public: | 
|---|
| 162 | QGLWidgetPrivate() : QWidgetPrivate() | 
|---|
| 163 | , disable_clear_on_painter_begin(false) | 
|---|
| 164 | #ifdef Q_WS_QWS | 
|---|
| 165 | , wsurf(0) | 
|---|
| 166 | #endif | 
|---|
| 167 | #if defined(Q_WS_X11) && !defined(QT_NO_EGL) | 
|---|
| 168 | , eglSurfaceWindowId(0) | 
|---|
| 169 | #endif | 
|---|
| 170 | #if defined(Q_OS_SYMBIAN) | 
|---|
| 171 | , eglSurfaceWindowId(0) | 
|---|
| 172 | #endif | 
|---|
| 173 | { | 
|---|
| 174 | isGLWidget = 1; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | ~QGLWidgetPrivate() {} | 
|---|
| 178 |  | 
|---|
| 179 | void init(QGLContext *context, const QGLWidget* shareWidget); | 
|---|
| 180 | void initContext(QGLContext *context, const QGLWidget* shareWidget); | 
|---|
| 181 | bool renderCxPm(QPixmap *pixmap); | 
|---|
| 182 | void cleanupColormaps(); | 
|---|
| 183 | void aboutToDestroy() { | 
|---|
| 184 | if (glcx) | 
|---|
| 185 | glcx->reset(); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | QGLContext *glcx; | 
|---|
| 189 | QGLWidgetGLPaintDevice glDevice; | 
|---|
| 190 | bool autoSwap; | 
|---|
| 191 |  | 
|---|
| 192 | QGLColormap cmap; | 
|---|
| 193 | #ifndef QT_OPENGL_ES | 
|---|
| 194 | QMap<QString, int> displayListCache; | 
|---|
| 195 | #endif | 
|---|
| 196 |  | 
|---|
| 197 | bool disable_clear_on_painter_begin; | 
|---|
| 198 |  | 
|---|
| 199 | #if defined(Q_WS_WIN) | 
|---|
| 200 | void updateColormap(); | 
|---|
| 201 | QGLContext *olcx; | 
|---|
| 202 | #elif defined(Q_WS_X11) | 
|---|
| 203 | QGLOverlayWidget *olw; | 
|---|
| 204 | #ifndef QT_NO_EGL | 
|---|
| 205 | void recreateEglSurface(); | 
|---|
| 206 | WId eglSurfaceWindowId; | 
|---|
| 207 | #endif | 
|---|
| 208 | #elif defined(Q_WS_MAC) | 
|---|
| 209 | QGLContext *olcx; | 
|---|
| 210 | void updatePaintDevice(); | 
|---|
| 211 | #elif defined(Q_WS_QWS) | 
|---|
| 212 | QWSGLWindowSurface *wsurf; | 
|---|
| 213 | #endif | 
|---|
| 214 | #ifdef Q_OS_SYMBIAN | 
|---|
| 215 | void recreateEglSurface(); | 
|---|
| 216 | WId eglSurfaceWindowId; | 
|---|
| 217 | #endif | 
|---|
| 218 | }; | 
|---|
| 219 |  | 
|---|
| 220 | class QGLContextResource; | 
|---|
| 221 | class QGLSharedResourceGuard; | 
|---|
| 222 |  | 
|---|
| 223 | typedef QHash<QString, GLuint> QGLDDSCache; | 
|---|
| 224 |  | 
|---|
| 225 | // QGLContextPrivate has the responsibility of creating context groups. | 
|---|
| 226 | // QGLContextPrivate maintains the reference counter and destroys | 
|---|
| 227 | // context groups when needed. | 
|---|
| 228 | class QGLContextGroup | 
|---|
| 229 | { | 
|---|
| 230 | public: | 
|---|
| 231 | ~QGLContextGroup(); | 
|---|
| 232 |  | 
|---|
| 233 | QGLExtensionFuncs &extensionFuncs() {return m_extensionFuncs;} | 
|---|
| 234 | const QGLContext *context() const {return m_context;} | 
|---|
| 235 | bool isSharing() const { return m_shares.size() >= 2; } | 
|---|
| 236 | QList<const QGLContext *> shares() const { return m_shares; } | 
|---|
| 237 |  | 
|---|
| 238 | void addGuard(QGLSharedResourceGuard *guard); | 
|---|
| 239 | void removeGuard(QGLSharedResourceGuard *guard); | 
|---|
| 240 |  | 
|---|
| 241 | static void addShare(const QGLContext *context, const QGLContext *share); | 
|---|
| 242 | static void removeShare(const QGLContext *context); | 
|---|
| 243 | private: | 
|---|
| 244 | QGLContextGroup(const QGLContext *context); | 
|---|
| 245 |  | 
|---|
| 246 | QGLExtensionFuncs m_extensionFuncs; | 
|---|
| 247 | const QGLContext *m_context; // context group's representative | 
|---|
| 248 | QList<const QGLContext *> m_shares; | 
|---|
| 249 | QHash<QGLContextResource *, void *> m_resources; | 
|---|
| 250 | QGLSharedResourceGuard *m_guards; // double-linked list of active guards. | 
|---|
| 251 | QAtomicInt m_refs; | 
|---|
| 252 | QGLDDSCache m_dds_cache; | 
|---|
| 253 |  | 
|---|
| 254 | void cleanupResources(const QGLContext *ctx); | 
|---|
| 255 |  | 
|---|
| 256 | friend class QGLContext; | 
|---|
| 257 | friend class QGLContextPrivate; | 
|---|
| 258 | friend class QGLContextResource; | 
|---|
| 259 | }; | 
|---|
| 260 |  | 
|---|
| 261 | // Get the context that resources for "ctx" will transfer to once | 
|---|
| 262 | // "ctx" is destroyed.  Returns null if nothing is sharing with ctx. | 
|---|
| 263 | Q_OPENGL_EXPORT const QGLContext *qt_gl_transfer_context(const QGLContext *); | 
|---|
| 264 |  | 
|---|
| 265 | // GL extension definitions | 
|---|
| 266 | class QGLExtensions { | 
|---|
| 267 | public: | 
|---|
| 268 | enum Extension { | 
|---|
| 269 | TextureRectangle        = 0x00000001, | 
|---|
| 270 | SampleBuffers           = 0x00000002, | 
|---|
| 271 | GenerateMipmap          = 0x00000004, | 
|---|
| 272 | TextureCompression      = 0x00000008, | 
|---|
| 273 | FragmentProgram         = 0x00000010, | 
|---|
| 274 | MirroredRepeat          = 0x00000020, | 
|---|
| 275 | FramebufferObject       = 0x00000040, | 
|---|
| 276 | StencilTwoSide          = 0x00000080, | 
|---|
| 277 | StencilWrap             = 0x00000100, | 
|---|
| 278 | PackedDepthStencil      = 0x00000200, | 
|---|
| 279 | NVFloatBuffer           = 0x00000400, | 
|---|
| 280 | PixelBufferObject       = 0x00000800, | 
|---|
| 281 | FramebufferBlit         = 0x00001000, | 
|---|
| 282 | NPOTTextures            = 0x00002000, | 
|---|
| 283 | BGRATextureFormat       = 0x00004000, | 
|---|
| 284 | DDSTextureCompression   = 0x00008000, | 
|---|
| 285 | ETC1TextureCompression  = 0x00010000, | 
|---|
| 286 | PVRTCTextureCompression = 0x00020000, | 
|---|
| 287 | FragmentShader          = 0x00040000, | 
|---|
| 288 | ElementIndexUint        = 0x00080000, | 
|---|
| 289 | Depth24                 = 0x00100000 | 
|---|
| 290 | }; | 
|---|
| 291 | Q_DECLARE_FLAGS(Extensions, Extension) | 
|---|
| 292 |  | 
|---|
| 293 | static Extensions glExtensions(); | 
|---|
| 294 | static Extensions currentContextExtensions(); | 
|---|
| 295 | }; | 
|---|
| 296 |  | 
|---|
| 297 | /* | 
|---|
| 298 | QGLTemporaryContext - the main objective of this class is to have a way of | 
|---|
| 299 | creating a GL context and making it current, without going via QGLWidget | 
|---|
| 300 | and friends. At certain points during GL initialization we need a current | 
|---|
| 301 | context in order decide what GL features are available, and to resolve GL | 
|---|
| 302 | extensions. Having a light-weight way of creating such a context saves | 
|---|
| 303 | initial application startup time, and it doesn't wind up creating recursive | 
|---|
| 304 | conflicts. | 
|---|
| 305 | The class currently uses a private d pointer to hide the platform specific | 
|---|
| 306 | types. This could possibly been done inline with #ifdef'ery, but it causes | 
|---|
| 307 | major headaches on e.g. X11 due to namespace pollution. | 
|---|
| 308 | */ | 
|---|
| 309 | class QGLTemporaryContextPrivate; | 
|---|
| 310 | class QGLTemporaryContext { | 
|---|
| 311 | public: | 
|---|
| 312 | QGLTemporaryContext(bool directRendering = true, QWidget *parent = 0); | 
|---|
| 313 | ~QGLTemporaryContext(); | 
|---|
| 314 |  | 
|---|
| 315 | private: | 
|---|
| 316 | QScopedPointer<QGLTemporaryContextPrivate> d; | 
|---|
| 317 | }; | 
|---|
| 318 |  | 
|---|
| 319 | class QGLTexture; | 
|---|
| 320 | class QGLTextureDestroyer; | 
|---|
| 321 |  | 
|---|
| 322 | // This probably needs to grow to GL_MAX_VERTEX_ATTRIBS, but 3 is ok for now as that's | 
|---|
| 323 | // all the GL2 engine uses: | 
|---|
| 324 | #define QT_GL_VERTEX_ARRAY_TRACKED_COUNT 3 | 
|---|
| 325 |  | 
|---|
| 326 | class QGLContextPrivate | 
|---|
| 327 | { | 
|---|
| 328 | Q_DECLARE_PUBLIC(QGLContext) | 
|---|
| 329 | public: | 
|---|
| 330 | explicit QGLContextPrivate(QGLContext *context); | 
|---|
| 331 | ~QGLContextPrivate(); | 
|---|
| 332 | QGLTexture *bindTexture(const QImage &image, GLenum target, GLint format, | 
|---|
| 333 | QGLContext::BindOptions options); | 
|---|
| 334 | QGLTexture *bindTexture(const QImage &image, GLenum target, GLint format, const qint64 key, | 
|---|
| 335 | QGLContext::BindOptions options); | 
|---|
| 336 | QGLTexture *bindTexture(const QPixmap &pixmap, GLenum target, GLint format, | 
|---|
| 337 | QGLContext::BindOptions options); | 
|---|
| 338 | QGLTexture *textureCacheLookup(const qint64 key, GLenum target); | 
|---|
| 339 | void init(QPaintDevice *dev, const QGLFormat &format); | 
|---|
| 340 | QImage convertToGLFormat(const QImage &image, bool force_premul, GLenum texture_format); | 
|---|
| 341 | int maxTextureSize(); | 
|---|
| 342 |  | 
|---|
| 343 | void cleanup(); | 
|---|
| 344 |  | 
|---|
| 345 | void setVertexAttribArrayEnabled(int arrayIndex, bool enabled = true); | 
|---|
| 346 | void syncGlState(); // Makes sure the GL context's state is what we think it is | 
|---|
| 347 | void swapRegion(const QRegion ®ion); | 
|---|
| 348 |  | 
|---|
| 349 | #if defined(Q_WS_WIN) | 
|---|
| 350 | void updateFormatVersion(); | 
|---|
| 351 | #endif | 
|---|
| 352 |  | 
|---|
| 353 | #if defined(Q_WS_WIN) | 
|---|
| 354 | HGLRC rc; | 
|---|
| 355 | HDC dc; | 
|---|
| 356 | WId        win; | 
|---|
| 357 | int pixelFormatId; | 
|---|
| 358 | QGLCmap* cmap; | 
|---|
| 359 | HBITMAP hbitmap; | 
|---|
| 360 | HDC hbitmap_hdc; | 
|---|
| 361 | #endif | 
|---|
| 362 | #ifndef QT_NO_EGL | 
|---|
| 363 | uint ownsEglContext : 1; | 
|---|
| 364 | QEglContext *eglContext; | 
|---|
| 365 | EGLSurface eglSurface; | 
|---|
| 366 | void destroyEglSurfaceForDevice(); | 
|---|
| 367 | EGLSurface eglSurfaceForDevice() const; | 
|---|
| 368 | #elif defined(Q_WS_X11) || defined(Q_WS_MAC) | 
|---|
| 369 | void* cx; | 
|---|
| 370 | #endif | 
|---|
| 371 | #if defined(Q_WS_X11) || defined(Q_WS_MAC) | 
|---|
| 372 | void* vi; | 
|---|
| 373 | #endif | 
|---|
| 374 | #if defined(Q_WS_X11) | 
|---|
| 375 | void* pbuf; | 
|---|
| 376 | quint32 gpm; | 
|---|
| 377 | int screen; | 
|---|
| 378 | QHash<QPixmapData*, QPixmap> boundPixmaps; | 
|---|
| 379 | QGLTexture *bindTextureFromNativePixmap(QPixmap*, const qint64 key, | 
|---|
| 380 | QGLContext::BindOptions options); | 
|---|
| 381 | static void destroyGlSurfaceForPixmap(QPixmapData*); | 
|---|
| 382 | static void unbindPixmapFromTexture(QPixmapData*); | 
|---|
| 383 | #endif | 
|---|
| 384 | #if defined(Q_WS_MAC) | 
|---|
| 385 | bool update; | 
|---|
| 386 | void *tryFormat(const QGLFormat &format); | 
|---|
| 387 | void clearDrawable(); | 
|---|
| 388 | #endif | 
|---|
| 389 | QGLFormat glFormat; | 
|---|
| 390 | QGLFormat reqFormat; | 
|---|
| 391 | GLuint fbo; | 
|---|
| 392 |  | 
|---|
| 393 | uint valid : 1; | 
|---|
| 394 | uint sharing : 1; | 
|---|
| 395 | uint initDone : 1; | 
|---|
| 396 | uint crWin : 1; | 
|---|
| 397 | uint internal_context : 1; | 
|---|
| 398 | uint version_flags_cached : 1; | 
|---|
| 399 | uint extension_flags_cached : 1; | 
|---|
| 400 |  | 
|---|
| 401 | // workarounds for driver/hw bugs on different platforms | 
|---|
| 402 | uint workaround_needsFullClearOnEveryFrame : 1; | 
|---|
| 403 | uint workaround_brokenFBOReadBack : 1; | 
|---|
| 404 | uint workaround_brokenTexSubImage : 1; | 
|---|
| 405 | uint workaroundsCached : 1; | 
|---|
| 406 |  | 
|---|
| 407 | uint workaround_brokenTextureFromPixmap : 1; | 
|---|
| 408 | uint workaround_brokenTextureFromPixmap_init : 1; | 
|---|
| 409 |  | 
|---|
| 410 | QPaintDevice *paintDevice; | 
|---|
| 411 | QColor transpColor; | 
|---|
| 412 | QGLContext *q_ptr; | 
|---|
| 413 | QGLFormat::OpenGLVersionFlags version_flags; | 
|---|
| 414 | QGLExtensions::Extensions extension_flags; | 
|---|
| 415 |  | 
|---|
| 416 | QGLContextGroup *group; | 
|---|
| 417 | GLint max_texture_size; | 
|---|
| 418 |  | 
|---|
| 419 | GLuint current_fbo; | 
|---|
| 420 | GLuint default_fbo; | 
|---|
| 421 | QPaintEngine *active_engine; | 
|---|
| 422 | QGLTextureDestroyer *texture_destroyer; | 
|---|
| 423 |  | 
|---|
| 424 | bool vertexAttributeArraysEnabledState[QT_GL_VERTEX_ARRAY_TRACKED_COUNT]; | 
|---|
| 425 |  | 
|---|
| 426 | static inline QGLContextGroup *contextGroup(const QGLContext *ctx) { return ctx->d_ptr->group; } | 
|---|
| 427 |  | 
|---|
| 428 | #ifdef Q_WS_WIN | 
|---|
| 429 | static inline QGLExtensionFuncs& extensionFuncs(const QGLContext *ctx) { return ctx->d_ptr->group->extensionFuncs(); } | 
|---|
| 430 | #endif | 
|---|
| 431 |  | 
|---|
| 432 | #if defined(Q_WS_X11) || defined(Q_WS_MAC) || defined(Q_WS_QWS) || defined(Q_OS_SYMBIAN) | 
|---|
| 433 | static QGLExtensionFuncs qt_extensionFuncs; | 
|---|
| 434 | static Q_OPENGL_EXPORT QGLExtensionFuncs& extensionFuncs(const QGLContext *); | 
|---|
| 435 | #endif | 
|---|
| 436 |  | 
|---|
| 437 | static void setCurrentContext(QGLContext *context); | 
|---|
| 438 | }; | 
|---|
| 439 |  | 
|---|
| 440 | Q_DECLARE_OPERATORS_FOR_FLAGS(QGLExtensions::Extensions) | 
|---|
| 441 |  | 
|---|
| 442 | // Temporarily make a context current if not already current or | 
|---|
| 443 | // shared with the current contex.  The previous context is made | 
|---|
| 444 | // current when the object goes out of scope. | 
|---|
| 445 | class Q_OPENGL_EXPORT QGLShareContextScope | 
|---|
| 446 | { | 
|---|
| 447 | public: | 
|---|
| 448 | QGLShareContextScope(const QGLContext *ctx) | 
|---|
| 449 | : m_oldContext(0) | 
|---|
| 450 | { | 
|---|
| 451 | QGLContext *currentContext = const_cast<QGLContext *>(QGLContext::currentContext()); | 
|---|
| 452 | if (currentContext != ctx && !QGLContext::areSharing(ctx, currentContext)) { | 
|---|
| 453 | m_oldContext = currentContext; | 
|---|
| 454 | m_ctx = const_cast<QGLContext *>(ctx); | 
|---|
| 455 | m_ctx->makeCurrent(); | 
|---|
| 456 | } else { | 
|---|
| 457 | m_ctx = currentContext; | 
|---|
| 458 | } | 
|---|
| 459 | } | 
|---|
| 460 |  | 
|---|
| 461 | operator QGLContext *() | 
|---|
| 462 | { | 
|---|
| 463 | return m_ctx; | 
|---|
| 464 | } | 
|---|
| 465 |  | 
|---|
| 466 | QGLContext *operator->() | 
|---|
| 467 | { | 
|---|
| 468 | return m_ctx; | 
|---|
| 469 | } | 
|---|
| 470 |  | 
|---|
| 471 | ~QGLShareContextScope() | 
|---|
| 472 | { | 
|---|
| 473 | if (m_oldContext) | 
|---|
| 474 | m_oldContext->makeCurrent(); | 
|---|
| 475 | } | 
|---|
| 476 |  | 
|---|
| 477 | private: | 
|---|
| 478 | QGLContext *m_oldContext; | 
|---|
| 479 | QGLContext *m_ctx; | 
|---|
| 480 | }; | 
|---|
| 481 |  | 
|---|
| 482 | class QGLTextureDestroyer : public QObject | 
|---|
| 483 | { | 
|---|
| 484 | Q_OBJECT | 
|---|
| 485 | public: | 
|---|
| 486 | QGLTextureDestroyer() : QObject() { | 
|---|
| 487 | qRegisterMetaType<GLuint>("GLuint"); | 
|---|
| 488 | connect(this, SIGNAL(freeTexture(QGLContext *, QPixmapData *, GLuint)), | 
|---|
| 489 | this, SLOT(freeTexture_slot(QGLContext *, QPixmapData *, GLuint))); | 
|---|
| 490 | } | 
|---|
| 491 | void emitFreeTexture(QGLContext *context, QPixmapData *boundPixmap, GLuint id) { | 
|---|
| 492 | emit freeTexture(context, boundPixmap, id); | 
|---|
| 493 | } | 
|---|
| 494 |  | 
|---|
| 495 | Q_SIGNALS: | 
|---|
| 496 | void freeTexture(QGLContext *context, QPixmapData *boundPixmap, GLuint id); | 
|---|
| 497 |  | 
|---|
| 498 | private slots: | 
|---|
| 499 | void freeTexture_slot(QGLContext *context, QPixmapData *boundPixmap, GLuint id) { | 
|---|
| 500 | #if defined(Q_WS_X11) | 
|---|
| 501 | if (boundPixmap) { | 
|---|
| 502 | QGLContext *oldContext = const_cast<QGLContext *>(QGLContext::currentContext()); | 
|---|
| 503 | context->makeCurrent(); | 
|---|
| 504 | // Although glXReleaseTexImage is a glX call, it must be called while there | 
|---|
| 505 | // is a current context - the context the pixmap was bound to a texture in. | 
|---|
| 506 | // Otherwise the release doesn't do anything and you get BadDrawable errors | 
|---|
| 507 | // when you come to delete the context. | 
|---|
| 508 | QGLContextPrivate::unbindPixmapFromTexture(boundPixmap); | 
|---|
| 509 | glDeleteTextures(1, &id); | 
|---|
| 510 | if (oldContext) | 
|---|
| 511 | oldContext->makeCurrent(); | 
|---|
| 512 | return; | 
|---|
| 513 | } | 
|---|
| 514 | #endif | 
|---|
| 515 | QGLShareContextScope scope(context); | 
|---|
| 516 | glDeleteTextures(1, &id); | 
|---|
| 517 | } | 
|---|
| 518 | }; | 
|---|
| 519 |  | 
|---|
| 520 | // ### make QGLContext a QObject in 5.0 and remove the proxy stuff | 
|---|
| 521 | class Q_OPENGL_EXPORT QGLSignalProxy : public QObject | 
|---|
| 522 | { | 
|---|
| 523 | Q_OBJECT | 
|---|
| 524 | public: | 
|---|
| 525 | void emitAboutToDestroyContext(const QGLContext *context) { | 
|---|
| 526 | emit aboutToDestroyContext(context); | 
|---|
| 527 | } | 
|---|
| 528 | static QGLSignalProxy *instance(); | 
|---|
| 529 | Q_SIGNALS: | 
|---|
| 530 | void aboutToDestroyContext(const QGLContext *context); | 
|---|
| 531 | }; | 
|---|
| 532 |  | 
|---|
| 533 | class QGLTexture { | 
|---|
| 534 | public: | 
|---|
| 535 | QGLTexture(QGLContext *ctx = 0, GLuint tx_id = 0, GLenum tx_target = GL_TEXTURE_2D, | 
|---|
| 536 | QGLContext::BindOptions opt = QGLContext::DefaultBindOption) | 
|---|
| 537 | : context(ctx), | 
|---|
| 538 | id(tx_id), | 
|---|
| 539 | target(tx_target), | 
|---|
| 540 | options(opt) | 
|---|
| 541 | #if defined(Q_WS_X11) | 
|---|
| 542 | , boundPixmap(0) | 
|---|
| 543 | #endif | 
|---|
| 544 | {} | 
|---|
| 545 |  | 
|---|
| 546 | ~QGLTexture() { | 
|---|
| 547 | if (options & QGLContext::MemoryManagedBindOption) { | 
|---|
| 548 | Q_ASSERT(context); | 
|---|
| 549 | #if !defined(Q_WS_X11) | 
|---|
| 550 | QPixmapData *boundPixmap = 0; | 
|---|
| 551 | #endif | 
|---|
| 552 | context->d_ptr->texture_destroyer->emitFreeTexture(context, boundPixmap, id); | 
|---|
| 553 | } | 
|---|
| 554 | } | 
|---|
| 555 |  | 
|---|
| 556 | QGLContext *context; | 
|---|
| 557 | GLuint id; | 
|---|
| 558 | GLenum target; | 
|---|
| 559 |  | 
|---|
| 560 | QGLContext::BindOptions options; | 
|---|
| 561 |  | 
|---|
| 562 | #if defined(Q_WS_X11) | 
|---|
| 563 | QPixmapData* boundPixmap; | 
|---|
| 564 | #endif | 
|---|
| 565 |  | 
|---|
| 566 | bool canBindCompressedTexture | 
|---|
| 567 | (const char *buf, int len, const char *format, bool *hasAlpha); | 
|---|
| 568 | QSize bindCompressedTexture | 
|---|
| 569 | (const QString& fileName, const char *format = 0); | 
|---|
| 570 | QSize bindCompressedTexture | 
|---|
| 571 | (const char *buf, int len, const char *format = 0); | 
|---|
| 572 | QSize bindCompressedTextureDDS(const char *buf, int len); | 
|---|
| 573 | QSize bindCompressedTexturePVR(const char *buf, int len); | 
|---|
| 574 | }; | 
|---|
| 575 |  | 
|---|
| 576 | struct QGLTextureCacheKey { | 
|---|
| 577 | qint64 key; | 
|---|
| 578 | QGLContextGroup *group; | 
|---|
| 579 | }; | 
|---|
| 580 |  | 
|---|
| 581 | inline bool operator==(const QGLTextureCacheKey &a, const QGLTextureCacheKey &b) | 
|---|
| 582 | { | 
|---|
| 583 | return a.key == b.key && a.group == b.group; | 
|---|
| 584 | } | 
|---|
| 585 |  | 
|---|
| 586 | inline uint qHash(const QGLTextureCacheKey &key) | 
|---|
| 587 | { | 
|---|
| 588 | return qHash(key.key) ^ qHash(key.group); | 
|---|
| 589 | } | 
|---|
| 590 |  | 
|---|
| 591 |  | 
|---|
| 592 | class Q_AUTOTEST_EXPORT QGLTextureCache { | 
|---|
| 593 | public: | 
|---|
| 594 | QGLTextureCache(); | 
|---|
| 595 | ~QGLTextureCache(); | 
|---|
| 596 |  | 
|---|
| 597 | void insert(QGLContext *ctx, qint64 key, QGLTexture *texture, int cost); | 
|---|
| 598 | void remove(qint64 key); | 
|---|
| 599 | inline int size(); | 
|---|
| 600 | inline void setMaxCost(int newMax); | 
|---|
| 601 | inline int maxCost(); | 
|---|
| 602 | inline QGLTexture* getTexture(QGLContext *ctx, qint64 key); | 
|---|
| 603 |  | 
|---|
| 604 | bool remove(QGLContext *ctx, GLuint textureId); | 
|---|
| 605 | void removeContextTextures(QGLContext *ctx); | 
|---|
| 606 | static QGLTextureCache *instance(); | 
|---|
| 607 | static void cleanupTexturesForCacheKey(qint64 cacheKey); | 
|---|
| 608 | static void cleanupTexturesForPixampData(QPixmapData* pixmap); | 
|---|
| 609 | static void cleanupBeforePixmapDestruction(QPixmapData* pixmap); | 
|---|
| 610 |  | 
|---|
| 611 | private: | 
|---|
| 612 | QCache<QGLTextureCacheKey, QGLTexture> m_cache; | 
|---|
| 613 | QReadWriteLock m_lock; | 
|---|
| 614 | }; | 
|---|
| 615 |  | 
|---|
| 616 | int QGLTextureCache::size() { | 
|---|
| 617 | QReadLocker locker(&m_lock); | 
|---|
| 618 | return m_cache.size(); | 
|---|
| 619 | } | 
|---|
| 620 |  | 
|---|
| 621 | void QGLTextureCache::setMaxCost(int newMax) | 
|---|
| 622 | { | 
|---|
| 623 | QWriteLocker locker(&m_lock); | 
|---|
| 624 | m_cache.setMaxCost(newMax); | 
|---|
| 625 | } | 
|---|
| 626 |  | 
|---|
| 627 | int QGLTextureCache::maxCost() | 
|---|
| 628 | { | 
|---|
| 629 | QReadLocker locker(&m_lock); | 
|---|
| 630 | return m_cache.maxCost(); | 
|---|
| 631 | } | 
|---|
| 632 |  | 
|---|
| 633 | QGLTexture* QGLTextureCache::getTexture(QGLContext *ctx, qint64 key) | 
|---|
| 634 | { | 
|---|
| 635 | QReadLocker locker(&m_lock); | 
|---|
| 636 | const QGLTextureCacheKey cacheKey = {key, QGLContextPrivate::contextGroup(ctx)}; | 
|---|
| 637 | return m_cache.object(cacheKey); | 
|---|
| 638 | } | 
|---|
| 639 |  | 
|---|
| 640 | extern Q_OPENGL_EXPORT QPaintEngine* qt_qgl_paint_engine(); | 
|---|
| 641 |  | 
|---|
| 642 | bool qt_gl_preferGL2Engine(); | 
|---|
| 643 |  | 
|---|
| 644 | inline GLenum qt_gl_preferredTextureFormat() | 
|---|
| 645 | { | 
|---|
| 646 | return (QGLExtensions::glExtensions() & QGLExtensions::BGRATextureFormat) && QSysInfo::ByteOrder == QSysInfo::LittleEndian | 
|---|
| 647 | ? GL_BGRA : GL_RGBA; | 
|---|
| 648 | } | 
|---|
| 649 |  | 
|---|
| 650 | inline GLenum qt_gl_preferredTextureTarget() | 
|---|
| 651 | { | 
|---|
| 652 | #if defined(QT_OPENGL_ES_2) | 
|---|
| 653 | return GL_TEXTURE_2D; | 
|---|
| 654 | #else | 
|---|
| 655 | return (QGLExtensions::glExtensions() & QGLExtensions::TextureRectangle) | 
|---|
| 656 | && !qt_gl_preferGL2Engine() | 
|---|
| 657 | ? GL_TEXTURE_RECTANGLE_NV | 
|---|
| 658 | : GL_TEXTURE_2D; | 
|---|
| 659 | #endif | 
|---|
| 660 | } | 
|---|
| 661 |  | 
|---|
| 662 | // One resource per group of shared contexts. | 
|---|
| 663 | class Q_OPENGL_EXPORT QGLContextResource | 
|---|
| 664 | { | 
|---|
| 665 | public: | 
|---|
| 666 | typedef void (*FreeFunc)(void *); | 
|---|
| 667 | QGLContextResource(FreeFunc f); | 
|---|
| 668 | ~QGLContextResource(); | 
|---|
| 669 | // Set resource 'value' for 'key' and all its shared contexts. | 
|---|
| 670 | void insert(const QGLContext *key, void *value); | 
|---|
| 671 | // Return resource for 'key' or a shared context. | 
|---|
| 672 | void *value(const QGLContext *key); | 
|---|
| 673 | // Cleanup 'value' in response to a context group being destroyed. | 
|---|
| 674 | void cleanup(const QGLContext *ctx, void *value); | 
|---|
| 675 | private: | 
|---|
| 676 | FreeFunc free; | 
|---|
| 677 | QAtomicInt active; | 
|---|
| 678 | }; | 
|---|
| 679 |  | 
|---|
| 680 | // Put a guard around a GL object identifier and its context. | 
|---|
| 681 | // When the context goes away, a shared context will be used | 
|---|
| 682 | // in its place.  If there are no more shared contexts, then | 
|---|
| 683 | // the identifier is returned as zero - it is assumed that the | 
|---|
| 684 | // context destruction cleaned up the identifier in this case. | 
|---|
| 685 | class Q_OPENGL_EXPORT QGLSharedResourceGuard | 
|---|
| 686 | { | 
|---|
| 687 | public: | 
|---|
| 688 | QGLSharedResourceGuard(const QGLContext *context) | 
|---|
| 689 | : m_group(0), m_id(0), m_next(0), m_prev(0) | 
|---|
| 690 | { | 
|---|
| 691 | setContext(context); | 
|---|
| 692 | } | 
|---|
| 693 | QGLSharedResourceGuard(const QGLContext *context, GLuint id) | 
|---|
| 694 | : m_group(0), m_id(id), m_next(0), m_prev(0) | 
|---|
| 695 | { | 
|---|
| 696 | setContext(context); | 
|---|
| 697 | } | 
|---|
| 698 | ~QGLSharedResourceGuard(); | 
|---|
| 699 |  | 
|---|
| 700 | const QGLContext *context() const | 
|---|
| 701 | { | 
|---|
| 702 | return m_group ? m_group->context() : 0; | 
|---|
| 703 | } | 
|---|
| 704 |  | 
|---|
| 705 | void setContext(const QGLContext *context); | 
|---|
| 706 |  | 
|---|
| 707 | GLuint id() const | 
|---|
| 708 | { | 
|---|
| 709 | return m_id; | 
|---|
| 710 | } | 
|---|
| 711 |  | 
|---|
| 712 | void setId(GLuint id) | 
|---|
| 713 | { | 
|---|
| 714 | m_id = id; | 
|---|
| 715 | } | 
|---|
| 716 |  | 
|---|
| 717 | private: | 
|---|
| 718 | QGLContextGroup *m_group; | 
|---|
| 719 | GLuint m_id; | 
|---|
| 720 | QGLSharedResourceGuard *m_next; | 
|---|
| 721 | QGLSharedResourceGuard *m_prev; | 
|---|
| 722 |  | 
|---|
| 723 | friend class QGLContextGroup; | 
|---|
| 724 | }; | 
|---|
| 725 |  | 
|---|
| 726 |  | 
|---|
| 727 | // This class can be used to match GL extensions without doing any mallocs. The | 
|---|
| 728 | // class assumes that the GL extension string ends with a space character, | 
|---|
| 729 | // which it should do on all conformant platforms. Create the object and pass | 
|---|
| 730 | // in a pointer to the extension string, then call match() on each extension | 
|---|
| 731 | // that should be matched. The match() function takes the extension name | 
|---|
| 732 | // *without* the terminating space character as input. | 
|---|
| 733 |  | 
|---|
| 734 | class QGLExtensionMatcher | 
|---|
| 735 | { | 
|---|
| 736 | public: | 
|---|
| 737 | QGLExtensionMatcher(const char *str) | 
|---|
| 738 | : gl_extensions(str), gl_extensions_length(qstrlen(str)) | 
|---|
| 739 | {} | 
|---|
| 740 |  | 
|---|
| 741 | bool match(const char *str) { | 
|---|
| 742 | int str_length = qstrlen(str); | 
|---|
| 743 | const char *extensions = gl_extensions; | 
|---|
| 744 | int extensions_length = gl_extensions_length; | 
|---|
| 745 |  | 
|---|
| 746 | while (1) { | 
|---|
| 747 | // the total length that needs to be matched is the str_length + | 
|---|
| 748 | // the space character that terminates the extension name | 
|---|
| 749 | if (extensions_length < str_length + 1) | 
|---|
| 750 | return false; | 
|---|
| 751 | if (qstrncmp(extensions, str, str_length) == 0 && extensions[str_length] == ' ') | 
|---|
| 752 | return true; | 
|---|
| 753 |  | 
|---|
| 754 | int split_pos = 0; | 
|---|
| 755 | while (split_pos < extensions_length && extensions[split_pos] != ' ') | 
|---|
| 756 | ++split_pos; | 
|---|
| 757 | ++split_pos; // added for the terminating space character | 
|---|
| 758 | extensions += split_pos; | 
|---|
| 759 | extensions_length -= split_pos; | 
|---|
| 760 | } | 
|---|
| 761 | return false; | 
|---|
| 762 | } | 
|---|
| 763 |  | 
|---|
| 764 | private: | 
|---|
| 765 | const char *gl_extensions; | 
|---|
| 766 | int gl_extensions_length; | 
|---|
| 767 | }; | 
|---|
| 768 |  | 
|---|
| 769 | QT_END_NAMESPACE | 
|---|
| 770 |  | 
|---|
| 771 | #endif // QGL_P_H | 
|---|