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_2)
62 # if defined(Q_OS_MAC)
63 # include <OpenGLES/ES2/gl.h>
65 # include <GLES2/gl2.h>
68 # define GL_DOUBLE GL_FLOAT
71 typedef GLfloat GLdouble;
74 # if defined(Q_OS_MAC)
75 # include <OpenGL/gl.h>
86 #define QGL_VERSION 460
87 #define QGL_VERSION_STR "4.6"
88 inline QT3_SUPPORT const char *qGLVersion() {
89 return QGL_VERSION_STR;
93 #if defined(Q_WS_WIN) || defined(Q_WS_MAC)
98 class QGLWidgetPrivate;
99 class QGLContextPrivate;
104 Q_OPENGL_EXPORT void setPreferredPaintEngine(QPaintEngine::Type engineType);
107 DoubleBuffer = 0x0001,
108 DepthBuffer = 0x0002,
110 AlphaChannel = 0x0008,
111 AccumBuffer = 0x0010,
112 StencilBuffer = 0x0020,
113 StereoBuffers = 0x0040,
114 DirectRendering = 0x0080,
116 SampleBuffers = 0x0200,
117 DeprecatedFunctions = 0x0400,
118 SingleBuffer = DoubleBuffer << 16,
119 NoDepthBuffer = DepthBuffer << 16,
120 ColorIndex = Rgba << 16,
121 NoAlphaChannel = AlphaChannel << 16,
122 NoAccumBuffer = AccumBuffer << 16,
123 NoStencilBuffer = StencilBuffer << 16,
124 NoStereoBuffers = StereoBuffers << 16,
125 IndirectRendering = DirectRendering << 16,
126 NoOverlay = HasOverlay << 16,
127 NoSampleBuffers = SampleBuffers << 16,
128 NoDeprecatedFunctions = DeprecatedFunctions << 16
130 Q_DECLARE_FLAGS(FormatOptions, FormatOption)
133 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
135 class QGLFormatPrivate;
137 class Q_OPENGL_EXPORT QGLFormat
141 QGLFormat(QGL::FormatOptions options, int plane = 0);
142 QGLFormat(const QGLFormat &other);
143 QGLFormat &operator=(const QGLFormat &other);
146 void setDepthBufferSize(int size);
147 int depthBufferSize() const;
149 void setAccumBufferSize(int size);
150 int accumBufferSize() const;
152 void setRedBufferSize(int size);
153 int redBufferSize() const;
155 void setGreenBufferSize(int size);
156 int greenBufferSize() const;
158 void setBlueBufferSize(int size);
159 int blueBufferSize() const;
161 void setAlphaBufferSize(int size);
162 int alphaBufferSize() const;
164 void setStencilBufferSize(int size);
165 int stencilBufferSize() const;
167 void setSampleBuffers(bool enable);
168 bool sampleBuffers() const;
170 void setSamples(int numSamples);
173 void setSwapInterval(int interval);
174 int swapInterval() const;
176 bool doubleBuffer() const;
177 void setDoubleBuffer(bool enable);
179 void setDepth(bool enable);
181 void setRgba(bool enable);
183 void setAlpha(bool enable);
185 void setAccum(bool enable);
186 bool stencil() const;
187 void setStencil(bool enable);
189 void setStereo(bool enable);
190 bool directRendering() const;
191 void setDirectRendering(bool enable);
192 bool hasOverlay() const;
193 void setOverlay(bool enable);
196 void setPlane(int plane);
198 void setOption(QGL::FormatOptions opt);
199 bool testOption(QGL::FormatOptions opt) const;
201 static QGLFormat defaultFormat();
202 static void setDefaultFormat(const QGLFormat& f);
204 static QGLFormat defaultOverlayFormat();
205 static void setDefaultOverlayFormat(const QGLFormat& f);
207 static bool hasOpenGL();
208 static bool hasOpenGLOverlays();
210 void setVersion(int major, int minor);
211 int majorVersion() const;
212 int minorVersion() const;
214 enum OpenGLContextProfile {
220 void setProfile(OpenGLContextProfile profile);
221 OpenGLContextProfile profile() const;
223 enum OpenGLVersionFlag {
224 OpenGL_Version_None = 0x00000000,
225 OpenGL_Version_1_1 = 0x00000001,
226 OpenGL_Version_1_2 = 0x00000002,
227 OpenGL_Version_1_3 = 0x00000004,
228 OpenGL_Version_1_4 = 0x00000008,
229 OpenGL_Version_1_5 = 0x00000010,
230 OpenGL_Version_2_0 = 0x00000020,
231 OpenGL_Version_2_1 = 0x00000040,
232 OpenGL_ES_Common_Version_1_0 = 0x00000080,
233 OpenGL_ES_CommonLite_Version_1_0 = 0x00000100,
234 OpenGL_ES_Common_Version_1_1 = 0x00000200,
235 OpenGL_ES_CommonLite_Version_1_1 = 0x00000400,
236 OpenGL_ES_Version_2_0 = 0x00000800,
237 OpenGL_Version_3_0 = 0x00001000,
238 OpenGL_Version_3_1 = 0x00002000,
239 OpenGL_Version_3_2 = 0x00004000,
240 OpenGL_Version_3_3 = 0x00008000,
241 OpenGL_Version_4_0 = 0x00010000
243 Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
245 static OpenGLVersionFlags openGLVersionFlags();
247 static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
248 static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
254 friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
255 friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
256 #ifndef QT_NO_DEBUG_STREAM
257 friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
261 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
263 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
264 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
266 #ifndef QT_NO_DEBUG_STREAM
267 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
272 class Q_OPENGL_EXPORT QGLContext
274 Q_DECLARE_PRIVATE(QGLContext)
276 QGLContext(const QGLFormat& format, QPaintDevice* device);
277 QGLContext(const QGLFormat& format);
278 virtual ~QGLContext();
280 virtual bool create(const QGLContext* shareContext = 0);
281 bool isValid() const;
282 bool isSharing() const;
285 static bool areSharing(const QGLContext *context1, const QGLContext *context2);
287 QGLFormat format() const;
288 QGLFormat requestedFormat() const;
289 void setFormat(const QGLFormat& format);
291 // ### Qt 5: return bools + maybe remove virtuals
292 virtual void makeCurrent();
293 virtual void doneCurrent();
295 virtual void swapBuffers() const;
297 QGLFunctions *functions() const;
300 NoBindOption = 0x0000,
301 InvertedYBindOption = 0x0001,
302 MipmapBindOption = 0x0002,
303 PremultipliedAlphaBindOption = 0x0004,
304 LinearFilteringBindOption = 0x0008,
306 MemoryManagedBindOption = 0x0010, // internal flag
307 CanFlipNativePixmapBindOption = 0x0020, // internal flag
308 TemporarilyCachedBindOption = 0x0040, // internal flag
310 DefaultBindOption = LinearFilteringBindOption
311 | InvertedYBindOption
313 InternalBindOption = MemoryManagedBindOption
314 | PremultipliedAlphaBindOption
316 Q_DECLARE_FLAGS(BindOptions, BindOption)
318 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
319 BindOptions options);
320 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
321 BindOptions options);
323 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
324 GLint format = GL_RGBA);
325 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
326 GLint format = GL_RGBA);
327 GLuint bindTexture(const QString &fileName);
329 void deleteTexture(GLuint tx_id);
331 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
332 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
334 static void setTextureCacheLimit(int size);
335 static int textureCacheLimit();
337 void *getProcAddress(const QString &proc) const;
338 QPaintDevice* device() const;
339 QColor overlayTransparentColor() const;
341 static const QGLContext* currentContext();
343 static QGLContext *fromGuiGLContext(QGuiGLContext *platformContext);
344 QGuiGLContext *contextHandle() const;
347 virtual bool chooseContext(const QGLContext* shareContext = 0);
349 bool deviceIsPixmap() const;
350 bool windowCreated() const;
351 void setWindowCreated(bool on);
352 bool initialized() const;
353 void setInitialized(bool on);
354 void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
356 uint colorIndex(const QColor& c) const;
357 void setValid(bool valid);
358 void setDevice(QPaintDevice *pDev);
361 static QGLContext* currentCtx;
364 QGLContext(QGuiGLContext *windowContext);
366 QScopedPointer<QGLContextPrivate> d_ptr;
368 friend class QGLPixelBuffer;
369 friend class QGLPixelBufferPrivate;
370 friend class QGLWidget;
371 friend class QGLWidgetPrivate;
372 friend class QGLGlyphCache;
373 friend class QGL2PaintEngineEx;
374 friend class QGL2PaintEngineExPrivate;
375 friend class QGLEngineShaderManager;
376 friend class QGLTextureGlyphCache;
377 friend struct QGLGlyphTexture;
378 friend class QGLContextGroup;
379 friend class QGLSharedResourceGuard;
380 friend class QGLPixmapBlurFilter;
381 friend class QGLExtensions;
382 friend class QGLTexture;
383 friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
386 void updatePaintDevice();
388 friend class QMacGLWindowChangeEvent;
389 friend QGLContextPrivate *qt_phonon_get_dptr(const QGLContext *);
391 friend class QGLFramebufferObject;
392 friend class QGLFramebufferObjectPrivate;
393 friend class QGLFBOGLPaintDevice;
394 friend class QGLPaintDevice;
395 friend class QGLWidgetGLPaintDevice;
396 friend class QX11GLSharedContexts;
397 friend class QGLContextResourceBase;
398 friend class QSGDistanceFieldGlyphCache;
400 Q_DISABLE_COPY(QGLContext)
403 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
405 class Q_OPENGL_EXPORT QGLWidget : public QWidget
408 Q_DECLARE_PRIVATE(QGLWidget)
410 explicit QGLWidget(QWidget* parent=0,
411 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
412 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
413 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
414 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
415 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
417 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
418 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
419 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
420 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
421 QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
422 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
426 void qglColor(const QColor& c) const;
427 void qglClearColor(const QColor& c) const;
429 bool isValid() const;
430 bool isSharing() const;
432 // ### Qt 5: return bools
436 bool doubleBuffer() const;
439 QGLFormat format() const;
440 void setFormat(const QGLFormat& format);
442 const QGLContext* context() const;
443 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
444 bool deleteOldContext = true);
446 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
447 QImage grabFrameBuffer(bool withAlpha = false);
449 void makeOverlayCurrent();
450 const QGLContext* overlayContext() const;
452 static QImage convertToGLFormat(const QImage& img);
454 void setMouseTracking(bool enable);
456 const QGLColormap & colormap() const;
457 void setColormap(const QGLColormap & map);
459 void renderText(int x, int y, const QString & str,
460 const QFont & fnt = QFont(), int listBase = 2000);
461 void renderText(double x, double y, double z, const QString & str,
462 const QFont & fnt = QFont(), int listBase = 2000);
463 QPaintEngine *paintEngine() const;
465 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
466 QGLContext::BindOptions options);
467 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
468 QGLContext::BindOptions options);
470 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
471 GLint format = GL_RGBA);
472 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
473 GLint format = GL_RGBA);
475 GLuint bindTexture(const QString &fileName);
477 void deleteTexture(GLuint tx_id);
479 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
480 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
483 virtual void updateGL();
484 virtual void updateOverlayGL();
487 bool event(QEvent *);
488 virtual void initializeGL();
489 virtual void resizeGL(int w, int h);
490 virtual void paintGL();
492 virtual void initializeOverlayGL();
493 virtual void resizeOverlayGL(int w, int h);
494 virtual void paintOverlayGL();
496 void setAutoBufferSwap(bool on);
497 bool autoBufferSwap() const;
499 void paintEvent(QPaintEvent*);
500 void resizeEvent(QResizeEvent*);
502 virtual void glInit();
503 virtual void glDraw();
504 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
506 QGLWidget(QGLWidgetPrivate &dd,
507 const QGLFormat &format = QGLFormat(),
509 const QGLWidget* shareWidget = 0,
510 Qt::WindowFlags f = 0);
512 Q_DISABLE_COPY(QGLWidget)
515 friend class QMacGLWindowChangeEvent;
517 friend class QGLDrawable;
518 friend class QGLPixelBuffer;
519 friend class QGLPixelBufferPrivate;
520 friend class QGLContext;
521 friend class QGLContextPrivate;
522 friend class QGLOverlayWidget;
523 friend class QGLPaintDevice;
524 friend class QGLWidgetGLPaintDevice;
529 // QGLFormat inline functions
532 inline bool QGLFormat::doubleBuffer() const
534 return testOption(QGL::DoubleBuffer);
537 inline bool QGLFormat::depth() const
539 return testOption(QGL::DepthBuffer);
542 inline bool QGLFormat::rgba() const
544 return testOption(QGL::Rgba);
547 inline bool QGLFormat::alpha() const
549 return testOption(QGL::AlphaChannel);
552 inline bool QGLFormat::accum() const
554 return testOption(QGL::AccumBuffer);
557 inline bool QGLFormat::stencil() const
559 return testOption(QGL::StencilBuffer);
562 inline bool QGLFormat::stereo() const
564 return testOption(QGL::StereoBuffers);
567 inline bool QGLFormat::directRendering() const
569 return testOption(QGL::DirectRendering);
572 inline bool QGLFormat::hasOverlay() const
574 return testOption(QGL::HasOverlay);
577 inline bool QGLFormat::sampleBuffers() const
579 return testOption(QGL::SampleBuffers);