1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtWidgets/qwidget.h>
46 #include <QtGui/qpaintengine.h>
47 #include <QtOpenGL/qglcolormap.h>
48 #include <QtCore/qmap.h>
49 #include <QtCore/qscopedpointer.h>
51 #include <QtGui/QSurfaceFormat>
56 # include <QtCore/qt_windows.h>
60 # include <OpenGL/gl.h>
61 #elif defined(QT_OPENGL_ES_1)
62 # if defined(Q_OS_MAC)
63 # include <OpenGLES/ES1/gl.h>
68 # define GL_DOUBLE GL_FLOAT
71 typedef GLfloat GLdouble;
73 #elif defined(QT_OPENGL_ES_2)
74 # if defined(Q_OS_MAC)
75 # include <OpenGLES/ES2/gl.h>
77 # include <GLES2/gl2.h>
80 # define GL_DOUBLE GL_FLOAT
83 typedef GLfloat GLdouble;
93 #if defined(Q_WS_MAC) && defined (QT_BUILD_OPENGL_LIB) && !defined(QT_MAC_USE_COCOA) && !defined(QDOC)
94 #define Q_MAC_COMPAT_GL_FUNCTIONS
97 struct QMacGLCompatTypes
99 typedef long CompatGLint;
100 typedef unsigned long CompatGLuint;
101 typedef unsigned long CompatGLenum;
105 struct QMacGLCompatTypes<long>
107 typedef int CompatGLint;
108 typedef unsigned int CompatGLuint;
109 typedef unsigned int CompatGLenum;
112 typedef QMacGLCompatTypes<GLint>::CompatGLint QMacCompatGLint;
113 typedef QMacGLCompatTypes<GLint>::CompatGLuint QMacCompatGLuint;
114 typedef QMacGLCompatTypes<GLint>::CompatGLenum QMacCompatGLenum;
119 #define QGL_VERSION 460
120 #define QGL_VERSION_STR "4.6"
121 inline QT3_SUPPORT const char *qGLVersion() {
122 return QGL_VERSION_STR;
126 #if defined(Q_WS_WIN) || defined(Q_WS_MAC)
131 #if defined(Q_WS_X11) && !defined(QT_OPENGL_ES)
132 class QGLOverlayWidget;
134 class QGLWidgetPrivate;
135 class QGLContextPrivate;
140 Q_OPENGL_EXPORT void setPreferredPaintEngine(QPaintEngine::Type engineType);
143 DoubleBuffer = 0x0001,
144 DepthBuffer = 0x0002,
146 AlphaChannel = 0x0008,
147 AccumBuffer = 0x0010,
148 StencilBuffer = 0x0020,
149 StereoBuffers = 0x0040,
150 DirectRendering = 0x0080,
152 SampleBuffers = 0x0200,
153 DeprecatedFunctions = 0x0400,
154 SingleBuffer = DoubleBuffer << 16,
155 NoDepthBuffer = DepthBuffer << 16,
156 ColorIndex = Rgba << 16,
157 NoAlphaChannel = AlphaChannel << 16,
158 NoAccumBuffer = AccumBuffer << 16,
159 NoStencilBuffer = StencilBuffer << 16,
160 NoStereoBuffers = StereoBuffers << 16,
161 IndirectRendering = DirectRendering << 16,
162 NoOverlay = HasOverlay << 16,
163 NoSampleBuffers = SampleBuffers << 16,
164 NoDeprecatedFunctions = DeprecatedFunctions << 16
166 Q_DECLARE_FLAGS(FormatOptions, FormatOption)
169 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
171 class QGLFormatPrivate;
173 class Q_OPENGL_EXPORT QGLFormat
177 QGLFormat(QGL::FormatOptions options, int plane = 0);
178 QGLFormat(const QGLFormat &other);
179 QGLFormat &operator=(const QGLFormat &other);
182 void setDepthBufferSize(int size);
183 int depthBufferSize() const;
185 void setAccumBufferSize(int size);
186 int accumBufferSize() const;
188 void setRedBufferSize(int size);
189 int redBufferSize() const;
191 void setGreenBufferSize(int size);
192 int greenBufferSize() const;
194 void setBlueBufferSize(int size);
195 int blueBufferSize() const;
197 void setAlphaBufferSize(int size);
198 int alphaBufferSize() const;
200 void setStencilBufferSize(int size);
201 int stencilBufferSize() const;
203 void setSampleBuffers(bool enable);
204 bool sampleBuffers() const;
206 void setSamples(int numSamples);
209 void setSwapInterval(int interval);
210 int swapInterval() const;
212 bool doubleBuffer() const;
213 void setDoubleBuffer(bool enable);
215 void setDepth(bool enable);
217 void setRgba(bool enable);
219 void setAlpha(bool enable);
221 void setAccum(bool enable);
222 bool stencil() const;
223 void setStencil(bool enable);
225 void setStereo(bool enable);
226 bool directRendering() const;
227 void setDirectRendering(bool enable);
228 bool hasOverlay() const;
229 void setOverlay(bool enable);
232 void setPlane(int plane);
234 void setOption(QGL::FormatOptions opt);
235 bool testOption(QGL::FormatOptions opt) const;
237 static QGLFormat defaultFormat();
238 static void setDefaultFormat(const QGLFormat& f);
240 static QGLFormat defaultOverlayFormat();
241 static void setDefaultOverlayFormat(const QGLFormat& f);
243 static bool hasOpenGL();
244 static bool hasOpenGLOverlays();
246 void setVersion(int major, int minor);
247 int majorVersion() const;
248 int minorVersion() const;
250 enum OpenGLContextProfile {
256 void setProfile(OpenGLContextProfile profile);
257 OpenGLContextProfile profile() const;
259 enum OpenGLVersionFlag {
260 OpenGL_Version_None = 0x00000000,
261 OpenGL_Version_1_1 = 0x00000001,
262 OpenGL_Version_1_2 = 0x00000002,
263 OpenGL_Version_1_3 = 0x00000004,
264 OpenGL_Version_1_4 = 0x00000008,
265 OpenGL_Version_1_5 = 0x00000010,
266 OpenGL_Version_2_0 = 0x00000020,
267 OpenGL_Version_2_1 = 0x00000040,
268 OpenGL_ES_Common_Version_1_0 = 0x00000080,
269 OpenGL_ES_CommonLite_Version_1_0 = 0x00000100,
270 OpenGL_ES_Common_Version_1_1 = 0x00000200,
271 OpenGL_ES_CommonLite_Version_1_1 = 0x00000400,
272 OpenGL_ES_Version_2_0 = 0x00000800,
273 OpenGL_Version_3_0 = 0x00001000,
274 OpenGL_Version_3_1 = 0x00002000,
275 OpenGL_Version_3_2 = 0x00004000,
276 OpenGL_Version_3_3 = 0x00008000,
277 OpenGL_Version_4_0 = 0x00010000
279 Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
281 static OpenGLVersionFlags openGLVersionFlags();
283 static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
284 static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
290 friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
291 friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
292 #ifndef QT_NO_DEBUG_STREAM
293 friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
297 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
299 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
300 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
302 #ifndef QT_NO_DEBUG_STREAM
303 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
308 class Q_OPENGL_EXPORT QGLContext
310 Q_DECLARE_PRIVATE(QGLContext)
312 QGLContext(const QGLFormat& format, QPaintDevice* device);
313 QGLContext(const QGLFormat& format);
314 virtual ~QGLContext();
316 virtual bool create(const QGLContext* shareContext = 0);
317 bool isValid() const;
318 bool isSharing() const;
321 static bool areSharing(const QGLContext *context1, const QGLContext *context2);
323 QGLFormat format() const;
324 QGLFormat requestedFormat() const;
325 void setFormat(const QGLFormat& format);
327 // ### Qt 5: return bools + maybe remove virtuals
328 virtual void makeCurrent();
329 virtual void doneCurrent();
331 virtual void swapBuffers() const;
333 QGLFunctions *functions() const;
336 NoBindOption = 0x0000,
337 InvertedYBindOption = 0x0001,
338 MipmapBindOption = 0x0002,
339 PremultipliedAlphaBindOption = 0x0004,
340 LinearFilteringBindOption = 0x0008,
342 MemoryManagedBindOption = 0x0010, // internal flag
343 CanFlipNativePixmapBindOption = 0x0020, // internal flag
344 TemporarilyCachedBindOption = 0x0040, // internal flag
346 DefaultBindOption = LinearFilteringBindOption
347 | InvertedYBindOption
349 InternalBindOption = MemoryManagedBindOption
350 | PremultipliedAlphaBindOption
352 Q_DECLARE_FLAGS(BindOptions, BindOption)
354 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
355 BindOptions options);
356 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
357 BindOptions options);
359 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
360 GLint format = GL_RGBA);
361 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
362 GLint format = GL_RGBA);
363 GLuint bindTexture(const QString &fileName);
365 void deleteTexture(GLuint tx_id);
367 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
368 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
370 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
371 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
372 QMacCompatGLint format = GL_RGBA);
373 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
374 QMacCompatGLint format = GL_RGBA);
375 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
377 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
380 void deleteTexture(QMacCompatGLuint tx_id);
382 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
383 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
386 static void setTextureCacheLimit(int size);
387 static int textureCacheLimit();
389 void *getProcAddress(const QString &proc) const;
390 QPaintDevice* device() const;
391 QColor overlayTransparentColor() const;
393 static const QGLContext* currentContext();
396 static QGLContext *fromGuiGLContext(QGuiGLContext *platformContext);
397 QGuiGLContext *contextHandle() const;
401 virtual bool chooseContext(const QGLContext* shareContext = 0);
403 #if defined(Q_WS_WIN)
404 virtual int choosePixelFormat(void* pfd, HDC pdc);
406 #if defined(Q_WS_X11) && defined(QT_NO_EGL)
407 virtual void* tryVisual(const QGLFormat& f, int bufDepth = 1);
408 virtual void* chooseVisual();
410 #if defined(Q_WS_MAC)
411 virtual void* chooseMacVisual(GDHandle);
414 bool deviceIsPixmap() const;
415 bool windowCreated() const;
416 void setWindowCreated(bool on);
417 bool initialized() const;
418 void setInitialized(bool on);
419 void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
421 uint colorIndex(const QColor& c) const;
422 void setValid(bool valid);
423 void setDevice(QPaintDevice *pDev);
426 static QGLContext* currentCtx;
430 QGLContext(QGuiGLContext *windowContext);
433 QScopedPointer<QGLContextPrivate> d_ptr;
435 friend class QGLPixelBuffer;
436 friend class QGLPixelBufferPrivate;
437 friend class QGLWidget;
438 friend class QGLWidgetPrivate;
439 friend class QGLGlyphCache;
440 friend class QOpenGLPaintEngine;
441 friend class QOpenGLPaintEnginePrivate;
442 friend class QGL2PaintEngineEx;
443 friend class QGL2PaintEngineExPrivate;
444 friend class QGLEngineShaderManager;
445 friend class QGLPixmapFilterBase;
446 friend class QGLTextureGlyphCache;
447 friend struct QGLGlyphTexture;
448 friend class QGLContextGroup;
449 friend class QGLSharedResourceGuard;
450 friend class QGLPixmapBlurFilter;
451 friend class QGLExtensions;
452 friend class QGLTexture;
453 friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
456 void updatePaintDevice();
458 friend class QMacGLWindowChangeEvent;
459 friend QGLContextPrivate *qt_phonon_get_dptr(const QGLContext *);
461 friend class QGLFramebufferObject;
462 friend class QGLFramebufferObjectPrivate;
463 friend class QGLFBOGLPaintDevice;
464 friend class QGLPaintDevice;
465 friend class QGLWidgetGLPaintDevice;
466 friend class QX11GLSharedContexts;
467 friend class QGLContextResourceBase;
468 friend class QSGDistanceFieldGlyphCache;
470 Q_DISABLE_COPY(QGLContext)
473 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
475 class Q_OPENGL_EXPORT QGLWidget : public QWidget
478 Q_DECLARE_PRIVATE(QGLWidget)
480 explicit QGLWidget(QWidget* parent=0,
481 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
482 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
483 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
484 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
485 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
487 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
488 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
489 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
490 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
491 QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
492 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
496 void qglColor(const QColor& c) const;
497 void qglClearColor(const QColor& c) const;
499 bool isValid() const;
500 bool isSharing() const;
502 // ### Qt 5: return bools
506 bool doubleBuffer() const;
509 QGLFormat format() const;
510 void setFormat(const QGLFormat& format);
512 const QGLContext* context() const;
513 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
514 bool deleteOldContext = true);
516 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
517 QImage grabFrameBuffer(bool withAlpha = false);
519 void makeOverlayCurrent();
520 const QGLContext* overlayContext() const;
522 static QImage convertToGLFormat(const QImage& img);
524 void setMouseTracking(bool enable);
526 const QGLColormap & colormap() const;
527 void setColormap(const QGLColormap & map);
529 void renderText(int x, int y, const QString & str,
530 const QFont & fnt = QFont(), int listBase = 2000);
531 void renderText(double x, double y, double z, const QString & str,
532 const QFont & fnt = QFont(), int listBase = 2000);
533 QPaintEngine *paintEngine() const;
535 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
536 QGLContext::BindOptions options);
537 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
538 QGLContext::BindOptions options);
540 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
541 GLint format = GL_RGBA);
542 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
543 GLint format = GL_RGBA);
545 GLuint bindTexture(const QString &fileName);
547 void deleteTexture(GLuint tx_id);
549 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
550 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
552 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
553 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
554 QMacCompatGLint format = GL_RGBA);
555 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
556 QMacCompatGLint format = GL_RGBA);
557 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
558 QGLContext::BindOptions);
559 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
560 QGLContext::BindOptions);
562 void deleteTexture(QMacCompatGLuint tx_id);
564 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
565 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
569 virtual void updateGL();
570 virtual void updateOverlayGL();
573 bool event(QEvent *);
574 virtual void initializeGL();
575 virtual void resizeGL(int w, int h);
576 virtual void paintGL();
578 virtual void initializeOverlayGL();
579 virtual void resizeOverlayGL(int w, int h);
580 virtual void paintOverlayGL();
582 void setAutoBufferSwap(bool on);
583 bool autoBufferSwap() const;
585 void paintEvent(QPaintEvent*);
586 void resizeEvent(QResizeEvent*);
588 virtual void glInit();
589 virtual void glDraw();
590 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
592 QGLWidget(QGLWidgetPrivate &dd,
593 const QGLFormat &format = QGLFormat(),
595 const QGLWidget* shareWidget = 0,
596 Qt::WindowFlags f = 0);
598 Q_DISABLE_COPY(QGLWidget)
601 friend class QMacGLWindowChangeEvent;
603 friend class QGLDrawable;
604 friend class QGLPixelBuffer;
605 friend class QGLPixelBufferPrivate;
606 friend class QGLContext;
607 friend class QGLContextPrivate;
608 friend class QGLOverlayWidget;
609 friend class QOpenGLPaintEngine;
610 friend class QGLPaintDevice;
611 friend class QGLWidgetGLPaintDevice;
616 // QGLFormat inline functions
619 inline bool QGLFormat::doubleBuffer() const
621 return testOption(QGL::DoubleBuffer);
624 inline bool QGLFormat::depth() const
626 return testOption(QGL::DepthBuffer);
629 inline bool QGLFormat::rgba() const
631 return testOption(QGL::Rgba);
634 inline bool QGLFormat::alpha() const
636 return testOption(QGL::AlphaChannel);
639 inline bool QGLFormat::accum() const
641 return testOption(QGL::AccumBuffer);
644 inline bool QGLFormat::stencil() const
646 return testOption(QGL::StencilBuffer);
649 inline bool QGLFormat::stereo() const
651 return testOption(QGL::StereoBuffers);
654 inline bool QGLFormat::directRendering() const
656 return testOption(QGL::DirectRendering);
659 inline bool QGLFormat::hasOverlay() const
661 return testOption(QGL::HasOverlay);
664 inline bool QGLFormat::sampleBuffers() const
666 return testOption(QGL::SampleBuffers);