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;
105 DoubleBuffer = 0x0001,
106 DepthBuffer = 0x0002,
108 AlphaChannel = 0x0008,
109 AccumBuffer = 0x0010,
110 StencilBuffer = 0x0020,
111 StereoBuffers = 0x0040,
112 DirectRendering = 0x0080,
114 SampleBuffers = 0x0200,
115 DeprecatedFunctions = 0x0400,
116 SingleBuffer = DoubleBuffer << 16,
117 NoDepthBuffer = DepthBuffer << 16,
118 ColorIndex = Rgba << 16,
119 NoAlphaChannel = AlphaChannel << 16,
120 NoAccumBuffer = AccumBuffer << 16,
121 NoStencilBuffer = StencilBuffer << 16,
122 NoStereoBuffers = StereoBuffers << 16,
123 IndirectRendering = DirectRendering << 16,
124 NoOverlay = HasOverlay << 16,
125 NoSampleBuffers = SampleBuffers << 16,
126 NoDeprecatedFunctions = DeprecatedFunctions << 16
128 Q_DECLARE_FLAGS(FormatOptions, FormatOption)
131 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
133 class QGLFormatPrivate;
135 class Q_OPENGL_EXPORT QGLFormat
139 QGLFormat(QGL::FormatOptions options, int plane = 0);
140 QGLFormat(const QGLFormat &other);
141 QGLFormat &operator=(const QGLFormat &other);
144 void setDepthBufferSize(int size);
145 int depthBufferSize() const;
147 void setAccumBufferSize(int size);
148 int accumBufferSize() const;
150 void setRedBufferSize(int size);
151 int redBufferSize() const;
153 void setGreenBufferSize(int size);
154 int greenBufferSize() const;
156 void setBlueBufferSize(int size);
157 int blueBufferSize() const;
159 void setAlphaBufferSize(int size);
160 int alphaBufferSize() const;
162 void setStencilBufferSize(int size);
163 int stencilBufferSize() const;
165 void setSampleBuffers(bool enable);
166 bool sampleBuffers() const;
168 void setSamples(int numSamples);
171 void setSwapInterval(int interval);
172 int swapInterval() const;
174 bool doubleBuffer() const;
175 void setDoubleBuffer(bool enable);
177 void setDepth(bool enable);
179 void setRgba(bool enable);
181 void setAlpha(bool enable);
183 void setAccum(bool enable);
184 bool stencil() const;
185 void setStencil(bool enable);
187 void setStereo(bool enable);
188 bool directRendering() const;
189 void setDirectRendering(bool enable);
190 bool hasOverlay() const;
191 void setOverlay(bool enable);
194 void setPlane(int plane);
196 void setOption(QGL::FormatOptions opt);
197 bool testOption(QGL::FormatOptions opt) const;
199 static QGLFormat defaultFormat();
200 static void setDefaultFormat(const QGLFormat& f);
202 static QGLFormat defaultOverlayFormat();
203 static void setDefaultOverlayFormat(const QGLFormat& f);
205 static bool hasOpenGL();
206 static bool hasOpenGLOverlays();
208 void setVersion(int major, int minor);
209 int majorVersion() const;
210 int minorVersion() const;
212 enum OpenGLContextProfile {
218 void setProfile(OpenGLContextProfile profile);
219 OpenGLContextProfile profile() const;
221 enum OpenGLVersionFlag {
222 OpenGL_Version_None = 0x00000000,
223 OpenGL_Version_1_1 = 0x00000001,
224 OpenGL_Version_1_2 = 0x00000002,
225 OpenGL_Version_1_3 = 0x00000004,
226 OpenGL_Version_1_4 = 0x00000008,
227 OpenGL_Version_1_5 = 0x00000010,
228 OpenGL_Version_2_0 = 0x00000020,
229 OpenGL_Version_2_1 = 0x00000040,
230 OpenGL_ES_Common_Version_1_0 = 0x00000080,
231 OpenGL_ES_CommonLite_Version_1_0 = 0x00000100,
232 OpenGL_ES_Common_Version_1_1 = 0x00000200,
233 OpenGL_ES_CommonLite_Version_1_1 = 0x00000400,
234 OpenGL_ES_Version_2_0 = 0x00000800,
235 OpenGL_Version_3_0 = 0x00001000,
236 OpenGL_Version_3_1 = 0x00002000,
237 OpenGL_Version_3_2 = 0x00004000,
238 OpenGL_Version_3_3 = 0x00008000,
239 OpenGL_Version_4_0 = 0x00010000
241 Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
243 static OpenGLVersionFlags openGLVersionFlags();
245 static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
246 static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
252 friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
253 friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
254 #ifndef QT_NO_DEBUG_STREAM
255 friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
259 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
261 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
262 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
264 #ifndef QT_NO_DEBUG_STREAM
265 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
270 class Q_OPENGL_EXPORT QGLContext
272 Q_DECLARE_PRIVATE(QGLContext)
274 QGLContext(const QGLFormat& format, QPaintDevice* device);
275 QGLContext(const QGLFormat& format);
276 virtual ~QGLContext();
278 virtual bool create(const QGLContext* shareContext = 0);
279 bool isValid() const;
280 bool isSharing() const;
283 static bool areSharing(const QGLContext *context1, const QGLContext *context2);
285 QGLFormat format() const;
286 QGLFormat requestedFormat() const;
287 void setFormat(const QGLFormat& format);
289 // ### Qt 5: return bools + maybe remove virtuals
290 virtual void makeCurrent();
291 virtual void doneCurrent();
293 virtual void swapBuffers() const;
295 QGLFunctions *functions() const;
298 NoBindOption = 0x0000,
299 InvertedYBindOption = 0x0001,
300 MipmapBindOption = 0x0002,
301 PremultipliedAlphaBindOption = 0x0004,
302 LinearFilteringBindOption = 0x0008,
304 MemoryManagedBindOption = 0x0010, // internal flag
305 CanFlipNativePixmapBindOption = 0x0020, // internal flag
306 TemporarilyCachedBindOption = 0x0040, // internal flag
308 DefaultBindOption = LinearFilteringBindOption
309 | InvertedYBindOption
311 InternalBindOption = MemoryManagedBindOption
312 | PremultipliedAlphaBindOption
314 Q_DECLARE_FLAGS(BindOptions, BindOption)
316 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
317 BindOptions options);
318 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
319 BindOptions options);
321 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
322 GLint format = GL_RGBA);
323 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
324 GLint format = GL_RGBA);
325 GLuint bindTexture(const QString &fileName);
327 void deleteTexture(GLuint tx_id);
329 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
330 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
332 static void setTextureCacheLimit(int size);
333 static int textureCacheLimit();
335 void *getProcAddress(const QString &proc) const;
336 QPaintDevice* device() const;
337 QColor overlayTransparentColor() const;
339 static const QGLContext* currentContext();
341 static QGLContext *fromOpenGLContext(QOpenGLContext *platformContext);
342 QOpenGLContext *contextHandle() const;
345 virtual bool chooseContext(const QGLContext* shareContext = 0);
347 bool deviceIsPixmap() const;
348 bool windowCreated() const;
349 void setWindowCreated(bool on);
350 bool initialized() const;
351 void setInitialized(bool on);
352 void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
354 uint colorIndex(const QColor& c) const;
355 void setValid(bool valid);
356 void setDevice(QPaintDevice *pDev);
359 static QGLContext* currentCtx;
362 QGLContext(QOpenGLContext *windowContext);
364 QScopedPointer<QGLContextPrivate> d_ptr;
366 friend class QGLPixelBuffer;
367 friend class QGLPixelBufferPrivate;
368 friend class QGLWidget;
369 friend class QGLWidgetPrivate;
370 friend class QGLGlyphCache;
371 friend class QGL2PaintEngineEx;
372 friend class QGL2PaintEngineExPrivate;
373 friend class QGLEngineShaderManager;
374 friend class QGLTextureGlyphCache;
375 friend struct QGLGlyphTexture;
376 friend class QGLContextGroup;
377 friend class QGLPixmapBlurFilter;
378 friend class QGLExtensions;
379 friend class QGLTexture;
380 friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
383 void updatePaintDevice();
385 friend class QMacGLWindowChangeEvent;
386 friend QGLContextPrivate *qt_phonon_get_dptr(const QGLContext *);
388 friend class QGLFramebufferObject;
389 friend class QGLFramebufferObjectPrivate;
390 friend class QGLFBOGLPaintDevice;
391 friend class QGLPaintDevice;
392 friend class QGLWidgetGLPaintDevice;
393 friend class QX11GLSharedContexts;
394 friend class QGLContextResourceBase;
395 friend class QSGDistanceFieldGlyphCache;
397 Q_DISABLE_COPY(QGLContext)
400 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
402 class Q_OPENGL_EXPORT QGLWidget : public QWidget
405 Q_DECLARE_PRIVATE(QGLWidget)
407 explicit QGLWidget(QWidget* parent=0,
408 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
409 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
410 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
411 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
412 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
414 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
415 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
416 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
417 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
418 QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
419 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
423 void qglColor(const QColor& c) const;
424 void qglClearColor(const QColor& c) const;
426 bool isValid() const;
427 bool isSharing() const;
429 // ### Qt 5: return bools
433 bool doubleBuffer() const;
436 QGLFormat format() const;
437 void setFormat(const QGLFormat& format);
439 const QGLContext* context() const;
440 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
441 bool deleteOldContext = true);
443 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
444 QImage grabFrameBuffer(bool withAlpha = false);
446 void makeOverlayCurrent();
447 const QGLContext* overlayContext() const;
449 static QImage convertToGLFormat(const QImage& img);
451 void setMouseTracking(bool enable);
453 const QGLColormap & colormap() const;
454 void setColormap(const QGLColormap & map);
456 void renderText(int x, int y, const QString & str,
457 const QFont & fnt = QFont(), int listBase = 2000);
458 void renderText(double x, double y, double z, const QString & str,
459 const QFont & fnt = QFont(), int listBase = 2000);
460 QPaintEngine *paintEngine() const;
462 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
463 QGLContext::BindOptions options);
464 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
465 QGLContext::BindOptions options);
467 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
468 GLint format = GL_RGBA);
469 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
470 GLint format = GL_RGBA);
472 GLuint bindTexture(const QString &fileName);
474 void deleteTexture(GLuint tx_id);
476 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
477 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
480 virtual void updateGL();
481 virtual void updateOverlayGL();
484 bool event(QEvent *);
485 virtual void initializeGL();
486 virtual void resizeGL(int w, int h);
487 virtual void paintGL();
489 virtual void initializeOverlayGL();
490 virtual void resizeOverlayGL(int w, int h);
491 virtual void paintOverlayGL();
493 void setAutoBufferSwap(bool on);
494 bool autoBufferSwap() const;
496 void paintEvent(QPaintEvent*);
497 void resizeEvent(QResizeEvent*);
499 virtual void glInit();
500 virtual void glDraw();
501 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
503 QGLWidget(QGLWidgetPrivate &dd,
504 const QGLFormat &format = QGLFormat(),
506 const QGLWidget* shareWidget = 0,
507 Qt::WindowFlags f = 0);
509 Q_DISABLE_COPY(QGLWidget)
512 friend class QMacGLWindowChangeEvent;
514 friend class QGLDrawable;
515 friend class QGLPixelBuffer;
516 friend class QGLPixelBufferPrivate;
517 friend class QGLContext;
518 friend class QGLContextPrivate;
519 friend class QGLOverlayWidget;
520 friend class QGLPaintDevice;
521 friend class QGLWidgetGLPaintDevice;
526 // QGLFormat inline functions
529 inline bool QGLFormat::doubleBuffer() const
531 return testOption(QGL::DoubleBuffer);
534 inline bool QGLFormat::depth() const
536 return testOption(QGL::DepthBuffer);
539 inline bool QGLFormat::rgba() const
541 return testOption(QGL::Rgba);
544 inline bool QGLFormat::alpha() const
546 return testOption(QGL::AlphaChannel);
549 inline bool QGLFormat::accum() const
551 return testOption(QGL::AccumBuffer);
554 inline bool QGLFormat::stencil() const
556 return testOption(QGL::StencilBuffer);
559 inline bool QGLFormat::stereo() const
561 return testOption(QGL::StereoBuffers);
564 inline bool QGLFormat::directRendering() const
566 return testOption(QGL::DirectRendering);
569 inline bool QGLFormat::hasOverlay() const
571 return testOption(QGL::HasOverlay);
574 inline bool QGLFormat::sampleBuffers() const
576 return testOption(QGL::SampleBuffers);