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 *fromGuiGLContext(QGuiGLContext *platformContext);
342 QGuiGLContext *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(QGuiGLContext *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 QGLSharedResourceGuard;
378 friend class QGLPixmapBlurFilter;
379 friend class QGLExtensions;
380 friend class QGLTexture;
381 friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
384 void updatePaintDevice();
386 friend class QMacGLWindowChangeEvent;
387 friend QGLContextPrivate *qt_phonon_get_dptr(const QGLContext *);
389 friend class QGLFramebufferObject;
390 friend class QGLFramebufferObjectPrivate;
391 friend class QGLFBOGLPaintDevice;
392 friend class QGLPaintDevice;
393 friend class QGLWidgetGLPaintDevice;
394 friend class QX11GLSharedContexts;
395 friend class QGLContextResourceBase;
396 friend class QSGDistanceFieldGlyphCache;
398 Q_DISABLE_COPY(QGLContext)
401 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
403 class Q_OPENGL_EXPORT QGLWidget : public QWidget
406 Q_DECLARE_PRIVATE(QGLWidget)
408 explicit QGLWidget(QWidget* parent=0,
409 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
410 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
411 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
412 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
413 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
415 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
416 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
417 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
418 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
419 QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
420 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
424 void qglColor(const QColor& c) const;
425 void qglClearColor(const QColor& c) const;
427 bool isValid() const;
428 bool isSharing() const;
430 // ### Qt 5: return bools
434 bool doubleBuffer() const;
437 QGLFormat format() const;
438 void setFormat(const QGLFormat& format);
440 const QGLContext* context() const;
441 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
442 bool deleteOldContext = true);
444 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
445 QImage grabFrameBuffer(bool withAlpha = false);
447 void makeOverlayCurrent();
448 const QGLContext* overlayContext() const;
450 static QImage convertToGLFormat(const QImage& img);
452 void setMouseTracking(bool enable);
454 const QGLColormap & colormap() const;
455 void setColormap(const QGLColormap & map);
457 void renderText(int x, int y, const QString & str,
458 const QFont & fnt = QFont(), int listBase = 2000);
459 void renderText(double x, double y, double z, const QString & str,
460 const QFont & fnt = QFont(), int listBase = 2000);
461 QPaintEngine *paintEngine() const;
463 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
464 QGLContext::BindOptions options);
465 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
466 QGLContext::BindOptions options);
468 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
469 GLint format = GL_RGBA);
470 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
471 GLint format = GL_RGBA);
473 GLuint bindTexture(const QString &fileName);
475 void deleteTexture(GLuint tx_id);
477 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
478 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
481 virtual void updateGL();
482 virtual void updateOverlayGL();
485 bool event(QEvent *);
486 virtual void initializeGL();
487 virtual void resizeGL(int w, int h);
488 virtual void paintGL();
490 virtual void initializeOverlayGL();
491 virtual void resizeOverlayGL(int w, int h);
492 virtual void paintOverlayGL();
494 void setAutoBufferSwap(bool on);
495 bool autoBufferSwap() const;
497 void paintEvent(QPaintEvent*);
498 void resizeEvent(QResizeEvent*);
500 virtual void glInit();
501 virtual void glDraw();
502 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
504 QGLWidget(QGLWidgetPrivate &dd,
505 const QGLFormat &format = QGLFormat(),
507 const QGLWidget* shareWidget = 0,
508 Qt::WindowFlags f = 0);
510 Q_DISABLE_COPY(QGLWidget)
513 friend class QMacGLWindowChangeEvent;
515 friend class QGLDrawable;
516 friend class QGLPixelBuffer;
517 friend class QGLPixelBufferPrivate;
518 friend class QGLContext;
519 friend class QGLContextPrivate;
520 friend class QGLOverlayWidget;
521 friend class QGLPaintDevice;
522 friend class QGLWidgetGLPaintDevice;
527 // QGLFormat inline functions
530 inline bool QGLFormat::doubleBuffer() const
532 return testOption(QGL::DoubleBuffer);
535 inline bool QGLFormat::depth() const
537 return testOption(QGL::DepthBuffer);
540 inline bool QGLFormat::rgba() const
542 return testOption(QGL::Rgba);
545 inline bool QGLFormat::alpha() const
547 return testOption(QGL::AlphaChannel);
550 inline bool QGLFormat::accum() const
552 return testOption(QGL::AccumBuffer);
555 inline bool QGLFormat::stencil() const
557 return testOption(QGL::StencilBuffer);
560 inline bool QGLFormat::stereo() const
562 return testOption(QGL::StereoBuffers);
565 inline bool QGLFormat::directRendering() const
567 return testOption(QGL::DirectRendering);
570 inline bool QGLFormat::hasOverlay() const
572 return testOption(QGL::HasOverlay);
575 inline bool QGLFormat::sampleBuffers() const
577 return testOption(QGL::SampleBuffers);