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;
86 # if defined(Q_OS_MAC)
87 # include <OpenGL/gl.h>
97 #if defined(Q_WS_MAC) && defined (QT_BUILD_OPENGL_LIB) && !defined(QT_MAC_USE_COCOA) && !defined(QDOC)
98 #define Q_MAC_COMPAT_GL_FUNCTIONS
100 template <typename T>
101 struct QMacGLCompatTypes
103 typedef long CompatGLint;
104 typedef unsigned long CompatGLuint;
105 typedef unsigned long CompatGLenum;
109 struct QMacGLCompatTypes<long>
111 typedef int CompatGLint;
112 typedef unsigned int CompatGLuint;
113 typedef unsigned int CompatGLenum;
116 typedef QMacGLCompatTypes<GLint>::CompatGLint QMacCompatGLint;
117 typedef QMacGLCompatTypes<GLint>::CompatGLuint QMacCompatGLuint;
118 typedef QMacGLCompatTypes<GLint>::CompatGLenum QMacCompatGLenum;
123 #define QGL_VERSION 460
124 #define QGL_VERSION_STR "4.6"
125 inline QT3_SUPPORT const char *qGLVersion() {
126 return QGL_VERSION_STR;
130 #if defined(Q_WS_WIN) || defined(Q_WS_MAC)
135 #if defined(Q_WS_X11) && !defined(QT_OPENGL_ES)
136 class QGLOverlayWidget;
138 class QGLWidgetPrivate;
139 class QGLContextPrivate;
144 Q_OPENGL_EXPORT void setPreferredPaintEngine(QPaintEngine::Type engineType);
147 DoubleBuffer = 0x0001,
148 DepthBuffer = 0x0002,
150 AlphaChannel = 0x0008,
151 AccumBuffer = 0x0010,
152 StencilBuffer = 0x0020,
153 StereoBuffers = 0x0040,
154 DirectRendering = 0x0080,
156 SampleBuffers = 0x0200,
157 DeprecatedFunctions = 0x0400,
158 SingleBuffer = DoubleBuffer << 16,
159 NoDepthBuffer = DepthBuffer << 16,
160 ColorIndex = Rgba << 16,
161 NoAlphaChannel = AlphaChannel << 16,
162 NoAccumBuffer = AccumBuffer << 16,
163 NoStencilBuffer = StencilBuffer << 16,
164 NoStereoBuffers = StereoBuffers << 16,
165 IndirectRendering = DirectRendering << 16,
166 NoOverlay = HasOverlay << 16,
167 NoSampleBuffers = SampleBuffers << 16,
168 NoDeprecatedFunctions = DeprecatedFunctions << 16
170 Q_DECLARE_FLAGS(FormatOptions, FormatOption)
173 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
175 class QGLFormatPrivate;
177 class Q_OPENGL_EXPORT QGLFormat
181 QGLFormat(QGL::FormatOptions options, int plane = 0);
182 QGLFormat(const QGLFormat &other);
183 QGLFormat &operator=(const QGLFormat &other);
186 void setDepthBufferSize(int size);
187 int depthBufferSize() const;
189 void setAccumBufferSize(int size);
190 int accumBufferSize() const;
192 void setRedBufferSize(int size);
193 int redBufferSize() const;
195 void setGreenBufferSize(int size);
196 int greenBufferSize() const;
198 void setBlueBufferSize(int size);
199 int blueBufferSize() const;
201 void setAlphaBufferSize(int size);
202 int alphaBufferSize() const;
204 void setStencilBufferSize(int size);
205 int stencilBufferSize() const;
207 void setSampleBuffers(bool enable);
208 bool sampleBuffers() const;
210 void setSamples(int numSamples);
213 void setSwapInterval(int interval);
214 int swapInterval() const;
216 bool doubleBuffer() const;
217 void setDoubleBuffer(bool enable);
219 void setDepth(bool enable);
221 void setRgba(bool enable);
223 void setAlpha(bool enable);
225 void setAccum(bool enable);
226 bool stencil() const;
227 void setStencil(bool enable);
229 void setStereo(bool enable);
230 bool directRendering() const;
231 void setDirectRendering(bool enable);
232 bool hasOverlay() const;
233 void setOverlay(bool enable);
236 void setPlane(int plane);
238 void setOption(QGL::FormatOptions opt);
239 bool testOption(QGL::FormatOptions opt) const;
241 static QGLFormat defaultFormat();
242 static void setDefaultFormat(const QGLFormat& f);
244 static QGLFormat defaultOverlayFormat();
245 static void setDefaultOverlayFormat(const QGLFormat& f);
247 static bool hasOpenGL();
248 static bool hasOpenGLOverlays();
250 void setVersion(int major, int minor);
251 int majorVersion() const;
252 int minorVersion() const;
254 enum OpenGLContextProfile {
260 void setProfile(OpenGLContextProfile profile);
261 OpenGLContextProfile profile() const;
263 enum OpenGLVersionFlag {
264 OpenGL_Version_None = 0x00000000,
265 OpenGL_Version_1_1 = 0x00000001,
266 OpenGL_Version_1_2 = 0x00000002,
267 OpenGL_Version_1_3 = 0x00000004,
268 OpenGL_Version_1_4 = 0x00000008,
269 OpenGL_Version_1_5 = 0x00000010,
270 OpenGL_Version_2_0 = 0x00000020,
271 OpenGL_Version_2_1 = 0x00000040,
272 OpenGL_ES_Common_Version_1_0 = 0x00000080,
273 OpenGL_ES_CommonLite_Version_1_0 = 0x00000100,
274 OpenGL_ES_Common_Version_1_1 = 0x00000200,
275 OpenGL_ES_CommonLite_Version_1_1 = 0x00000400,
276 OpenGL_ES_Version_2_0 = 0x00000800,
277 OpenGL_Version_3_0 = 0x00001000,
278 OpenGL_Version_3_1 = 0x00002000,
279 OpenGL_Version_3_2 = 0x00004000,
280 OpenGL_Version_3_3 = 0x00008000,
281 OpenGL_Version_4_0 = 0x00010000
283 Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
285 static OpenGLVersionFlags openGLVersionFlags();
287 static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
288 static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
294 friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
295 friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
296 #ifndef QT_NO_DEBUG_STREAM
297 friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
301 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
303 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
304 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
306 #ifndef QT_NO_DEBUG_STREAM
307 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
312 class Q_OPENGL_EXPORT QGLContext
314 Q_DECLARE_PRIVATE(QGLContext)
316 QGLContext(const QGLFormat& format, QPaintDevice* device);
317 QGLContext(const QGLFormat& format);
318 virtual ~QGLContext();
320 virtual bool create(const QGLContext* shareContext = 0);
321 bool isValid() const;
322 bool isSharing() const;
325 static bool areSharing(const QGLContext *context1, const QGLContext *context2);
327 QGLFormat format() const;
328 QGLFormat requestedFormat() const;
329 void setFormat(const QGLFormat& format);
331 // ### Qt 5: return bools + maybe remove virtuals
332 virtual void makeCurrent();
333 virtual void doneCurrent();
335 virtual void swapBuffers() const;
337 QGLFunctions *functions() const;
340 NoBindOption = 0x0000,
341 InvertedYBindOption = 0x0001,
342 MipmapBindOption = 0x0002,
343 PremultipliedAlphaBindOption = 0x0004,
344 LinearFilteringBindOption = 0x0008,
346 MemoryManagedBindOption = 0x0010, // internal flag
347 CanFlipNativePixmapBindOption = 0x0020, // internal flag
348 TemporarilyCachedBindOption = 0x0040, // internal flag
350 DefaultBindOption = LinearFilteringBindOption
351 | InvertedYBindOption
353 InternalBindOption = MemoryManagedBindOption
354 | PremultipliedAlphaBindOption
356 Q_DECLARE_FLAGS(BindOptions, BindOption)
358 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
359 BindOptions options);
360 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
361 BindOptions options);
363 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
364 GLint format = GL_RGBA);
365 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
366 GLint format = GL_RGBA);
367 GLuint bindTexture(const QString &fileName);
369 void deleteTexture(GLuint tx_id);
371 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
372 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
374 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
375 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
376 QMacCompatGLint format = GL_RGBA);
377 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
378 QMacCompatGLint format = GL_RGBA);
379 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
381 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
384 void deleteTexture(QMacCompatGLuint tx_id);
386 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
387 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
390 static void setTextureCacheLimit(int size);
391 static int textureCacheLimit();
393 void *getProcAddress(const QString &proc) const;
394 QPaintDevice* device() const;
395 QColor overlayTransparentColor() const;
397 static const QGLContext* currentContext();
400 static QGLContext *fromGuiGLContext(QGuiGLContext *platformContext);
401 QGuiGLContext *contextHandle() const;
405 virtual bool chooseContext(const QGLContext* shareContext = 0);
407 #if defined(Q_WS_WIN)
408 virtual int choosePixelFormat(void* pfd, HDC pdc);
410 #if defined(Q_WS_X11) && defined(QT_NO_EGL)
411 virtual void* tryVisual(const QGLFormat& f, int bufDepth = 1);
412 virtual void* chooseVisual();
414 #if defined(Q_WS_MAC)
415 virtual void* chooseMacVisual(GDHandle);
418 bool deviceIsPixmap() const;
419 bool windowCreated() const;
420 void setWindowCreated(bool on);
421 bool initialized() const;
422 void setInitialized(bool on);
423 void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
425 uint colorIndex(const QColor& c) const;
426 void setValid(bool valid);
427 void setDevice(QPaintDevice *pDev);
430 static QGLContext* currentCtx;
434 QGLContext(QGuiGLContext *windowContext);
437 QScopedPointer<QGLContextPrivate> d_ptr;
439 friend class QGLPixelBuffer;
440 friend class QGLPixelBufferPrivate;
441 friend class QGLWidget;
442 friend class QGLWidgetPrivate;
443 friend class QGLGlyphCache;
444 friend class QOpenGLPaintEngine;
445 friend class QOpenGLPaintEnginePrivate;
446 friend class QGL2PaintEngineEx;
447 friend class QGL2PaintEngineExPrivate;
448 friend class QGLEngineShaderManager;
449 friend class QGLPixmapFilterBase;
450 friend class QGLTextureGlyphCache;
451 friend struct QGLGlyphTexture;
452 friend class QGLContextGroup;
453 friend class QGLSharedResourceGuard;
454 friend class QGLPixmapBlurFilter;
455 friend class QGLExtensions;
456 friend class QGLTexture;
457 friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
460 void updatePaintDevice();
462 friend class QMacGLWindowChangeEvent;
463 friend QGLContextPrivate *qt_phonon_get_dptr(const QGLContext *);
465 friend class QGLFramebufferObject;
466 friend class QGLFramebufferObjectPrivate;
467 friend class QGLFBOGLPaintDevice;
468 friend class QGLPaintDevice;
469 friend class QGLWidgetGLPaintDevice;
470 friend class QX11GLSharedContexts;
471 friend class QGLContextResourceBase;
472 friend class QSGDistanceFieldGlyphCache;
474 Q_DISABLE_COPY(QGLContext)
477 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
479 class Q_OPENGL_EXPORT QGLWidget : public QWidget
482 Q_DECLARE_PRIVATE(QGLWidget)
484 explicit QGLWidget(QWidget* parent=0,
485 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
486 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
487 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
488 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
489 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
491 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
492 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
493 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
494 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
495 QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
496 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
500 void qglColor(const QColor& c) const;
501 void qglClearColor(const QColor& c) const;
503 bool isValid() const;
504 bool isSharing() const;
506 // ### Qt 5: return bools
510 bool doubleBuffer() const;
513 QGLFormat format() const;
514 void setFormat(const QGLFormat& format);
516 const QGLContext* context() const;
517 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
518 bool deleteOldContext = true);
520 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
521 QImage grabFrameBuffer(bool withAlpha = false);
523 void makeOverlayCurrent();
524 const QGLContext* overlayContext() const;
526 static QImage convertToGLFormat(const QImage& img);
528 void setMouseTracking(bool enable);
530 const QGLColormap & colormap() const;
531 void setColormap(const QGLColormap & map);
533 void renderText(int x, int y, const QString & str,
534 const QFont & fnt = QFont(), int listBase = 2000);
535 void renderText(double x, double y, double z, const QString & str,
536 const QFont & fnt = QFont(), int listBase = 2000);
537 QPaintEngine *paintEngine() const;
539 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
540 QGLContext::BindOptions options);
541 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
542 QGLContext::BindOptions options);
544 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
545 GLint format = GL_RGBA);
546 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
547 GLint format = GL_RGBA);
549 GLuint bindTexture(const QString &fileName);
551 void deleteTexture(GLuint tx_id);
553 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
554 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
556 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
557 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
558 QMacCompatGLint format = GL_RGBA);
559 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
560 QMacCompatGLint format = GL_RGBA);
561 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
562 QGLContext::BindOptions);
563 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
564 QGLContext::BindOptions);
566 void deleteTexture(QMacCompatGLuint tx_id);
568 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
569 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
573 virtual void updateGL();
574 virtual void updateOverlayGL();
577 bool event(QEvent *);
578 virtual void initializeGL();
579 virtual void resizeGL(int w, int h);
580 virtual void paintGL();
582 virtual void initializeOverlayGL();
583 virtual void resizeOverlayGL(int w, int h);
584 virtual void paintOverlayGL();
586 void setAutoBufferSwap(bool on);
587 bool autoBufferSwap() const;
589 void paintEvent(QPaintEvent*);
590 void resizeEvent(QResizeEvent*);
592 virtual void glInit();
593 virtual void glDraw();
594 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
596 QGLWidget(QGLWidgetPrivate &dd,
597 const QGLFormat &format = QGLFormat(),
599 const QGLWidget* shareWidget = 0,
600 Qt::WindowFlags f = 0);
602 Q_DISABLE_COPY(QGLWidget)
605 friend class QMacGLWindowChangeEvent;
607 friend class QGLDrawable;
608 friend class QGLPixelBuffer;
609 friend class QGLPixelBufferPrivate;
610 friend class QGLContext;
611 friend class QGLContextPrivate;
612 friend class QGLOverlayWidget;
613 friend class QOpenGLPaintEngine;
614 friend class QGLPaintDevice;
615 friend class QGLWidgetGLPaintDevice;
620 // QGLFormat inline functions
623 inline bool QGLFormat::doubleBuffer() const
625 return testOption(QGL::DoubleBuffer);
628 inline bool QGLFormat::depth() const
630 return testOption(QGL::DepthBuffer);
633 inline bool QGLFormat::rgba() const
635 return testOption(QGL::Rgba);
638 inline bool QGLFormat::alpha() const
640 return testOption(QGL::AlphaChannel);
643 inline bool QGLFormat::accum() const
645 return testOption(QGL::AccumBuffer);
648 inline bool QGLFormat::stencil() const
650 return testOption(QGL::StencilBuffer);
653 inline bool QGLFormat::stereo() const
655 return testOption(QGL::StereoBuffers);
658 inline bool QGLFormat::directRendering() const
660 return testOption(QGL::DirectRendering);
663 inline bool QGLFormat::hasOverlay() const
665 return testOption(QGL::HasOverlay);
668 inline bool QGLFormat::sampleBuffers() const
670 return testOption(QGL::SampleBuffers);