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 <QtGui/qwidget.h>
46 #include <QtGui/qpaintengine.h>
47 #include <QtOpenGL/qglcolormap.h>
48 #include <QtCore/qmap.h>
49 #include <QtCore/qscopedpointer.h>
52 #include <QtGui/QPlatformWindowFormat>
58 # include <QtCore/qt_windows.h>
62 # include <OpenGL/gl.h>
63 #elif defined(QT_OPENGL_ES_1)
64 # if defined(Q_OS_MAC)
65 # include <OpenGLES/ES1/gl.h>
70 # define GL_DOUBLE GL_FLOAT
73 typedef GLfloat GLdouble;
75 #elif defined(QT_OPENGL_ES_2)
76 # if defined(Q_OS_MAC)
77 # include <OpenGLES/ES2/gl.h>
79 # include <GLES2/gl2.h>
82 # define GL_DOUBLE GL_FLOAT
85 typedef GLfloat GLdouble;
95 #if defined(Q_WS_MAC) && defined (QT_BUILD_OPENGL_LIB) && !defined(QT_MAC_USE_COCOA) && !defined(QDOC)
96 #define Q_MAC_COMPAT_GL_FUNCTIONS
99 struct QMacGLCompatTypes
101 typedef long CompatGLint;
102 typedef unsigned long CompatGLuint;
103 typedef unsigned long CompatGLenum;
107 struct QMacGLCompatTypes<long>
109 typedef int CompatGLint;
110 typedef unsigned int CompatGLuint;
111 typedef unsigned int CompatGLenum;
114 typedef QMacGLCompatTypes<GLint>::CompatGLint QMacCompatGLint;
115 typedef QMacGLCompatTypes<GLint>::CompatGLuint QMacCompatGLuint;
116 typedef QMacGLCompatTypes<GLint>::CompatGLenum QMacCompatGLenum;
121 #define QGL_VERSION 460
122 #define QGL_VERSION_STR "4.6"
123 inline QT3_SUPPORT const char *qGLVersion() {
124 return QGL_VERSION_STR;
128 #if defined(Q_WS_WIN) || defined(Q_WS_MAC)
133 #if defined(Q_WS_X11) && !defined(QT_OPENGL_ES)
134 class QGLOverlayWidget;
136 class QGLWidgetPrivate;
137 class QGLContextPrivate;
142 Q_OPENGL_EXPORT void setPreferredPaintEngine(QPaintEngine::Type engineType);
145 DoubleBuffer = 0x0001,
146 DepthBuffer = 0x0002,
148 AlphaChannel = 0x0008,
149 AccumBuffer = 0x0010,
150 StencilBuffer = 0x0020,
151 StereoBuffers = 0x0040,
152 DirectRendering = 0x0080,
154 SampleBuffers = 0x0200,
155 DeprecatedFunctions = 0x0400,
156 SingleBuffer = DoubleBuffer << 16,
157 NoDepthBuffer = DepthBuffer << 16,
158 ColorIndex = Rgba << 16,
159 NoAlphaChannel = AlphaChannel << 16,
160 NoAccumBuffer = AccumBuffer << 16,
161 NoStencilBuffer = StencilBuffer << 16,
162 NoStereoBuffers = StereoBuffers << 16,
163 IndirectRendering = DirectRendering << 16,
164 NoOverlay = HasOverlay << 16,
165 NoSampleBuffers = SampleBuffers << 16,
166 NoDeprecatedFunctions = DeprecatedFunctions << 16
168 Q_DECLARE_FLAGS(FormatOptions, FormatOption)
171 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
173 class QGLFormatPrivate;
175 class Q_OPENGL_EXPORT QGLFormat
179 QGLFormat(QGL::FormatOptions options, int plane = 0);
180 QGLFormat(const QGLFormat &other);
181 QGLFormat &operator=(const QGLFormat &other);
184 void setDepthBufferSize(int size);
185 int depthBufferSize() const;
187 void setAccumBufferSize(int size);
188 int accumBufferSize() const;
190 void setRedBufferSize(int size);
191 int redBufferSize() const;
193 void setGreenBufferSize(int size);
194 int greenBufferSize() const;
196 void setBlueBufferSize(int size);
197 int blueBufferSize() const;
199 void setAlphaBufferSize(int size);
200 int alphaBufferSize() const;
202 void setStencilBufferSize(int size);
203 int stencilBufferSize() const;
205 void setSampleBuffers(bool enable);
206 bool sampleBuffers() const;
208 void setSamples(int numSamples);
211 void setSwapInterval(int interval);
212 int swapInterval() const;
214 bool doubleBuffer() const;
215 void setDoubleBuffer(bool enable);
217 void setDepth(bool enable);
219 void setRgba(bool enable);
221 void setAlpha(bool enable);
223 void setAccum(bool enable);
224 bool stencil() const;
225 void setStencil(bool enable);
227 void setStereo(bool enable);
228 bool directRendering() const;
229 void setDirectRendering(bool enable);
230 bool hasOverlay() const;
231 void setOverlay(bool enable);
234 void setPlane(int plane);
236 void setOption(QGL::FormatOptions opt);
237 bool testOption(QGL::FormatOptions opt) const;
239 static QGLFormat defaultFormat();
240 static void setDefaultFormat(const QGLFormat& f);
242 static QGLFormat defaultOverlayFormat();
243 static void setDefaultOverlayFormat(const QGLFormat& f);
245 static bool hasOpenGL();
246 static bool hasOpenGLOverlays();
248 void setVersion(int major, int minor);
249 int majorVersion() const;
250 int minorVersion() const;
252 enum OpenGLContextProfile {
258 void setProfile(OpenGLContextProfile profile);
259 OpenGLContextProfile profile() const;
261 enum OpenGLVersionFlag {
262 OpenGL_Version_None = 0x00000000,
263 OpenGL_Version_1_1 = 0x00000001,
264 OpenGL_Version_1_2 = 0x00000002,
265 OpenGL_Version_1_3 = 0x00000004,
266 OpenGL_Version_1_4 = 0x00000008,
267 OpenGL_Version_1_5 = 0x00000010,
268 OpenGL_Version_2_0 = 0x00000020,
269 OpenGL_Version_2_1 = 0x00000040,
270 OpenGL_ES_Common_Version_1_0 = 0x00000080,
271 OpenGL_ES_CommonLite_Version_1_0 = 0x00000100,
272 OpenGL_ES_Common_Version_1_1 = 0x00000200,
273 OpenGL_ES_CommonLite_Version_1_1 = 0x00000400,
274 OpenGL_ES_Version_2_0 = 0x00000800,
275 OpenGL_Version_3_0 = 0x00001000,
276 OpenGL_Version_3_1 = 0x00002000,
277 OpenGL_Version_3_2 = 0x00004000,
278 OpenGL_Version_3_3 = 0x00008000,
279 OpenGL_Version_4_0 = 0x00010000
281 Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
283 static OpenGLVersionFlags openGLVersionFlags();
285 #if defined(Q_WS_QPA)
286 static QGLFormat fromPlatformWindowFormat(const QPlatformWindowFormat &format);
287 static QPlatformWindowFormat toPlatformWindowFormat(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 *fromPlatformGLContext(QPlatformGLContext *platformContext);
403 virtual bool chooseContext(const QGLContext* shareContext = 0);
405 #if defined(Q_WS_WIN)
406 virtual int choosePixelFormat(void* pfd, HDC pdc);
408 #if defined(Q_WS_X11) && defined(QT_NO_EGL)
409 virtual void* tryVisual(const QGLFormat& f, int bufDepth = 1);
410 virtual void* chooseVisual();
412 #if defined(Q_WS_MAC)
413 virtual void* chooseMacVisual(GDHandle);
416 bool deviceIsPixmap() const;
417 bool windowCreated() const;
418 void setWindowCreated(bool on);
419 bool initialized() const;
420 void setInitialized(bool on);
421 void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
423 uint colorIndex(const QColor& c) const;
424 void setValid(bool valid);
425 void setDevice(QPaintDevice *pDev);
428 static QGLContext* currentCtx;
432 QGLContext(QPlatformGLContext *platformContext);
435 QScopedPointer<QGLContextPrivate> d_ptr;
437 friend class QGLPixelBuffer;
438 friend class QGLPixelBufferPrivate;
439 friend class QGLWidget;
440 friend class QGLWidgetPrivate;
441 friend class QGLGlyphCache;
442 friend class QOpenGLPaintEngine;
443 friend class QOpenGLPaintEnginePrivate;
444 friend class QGL2PaintEngineEx;
445 friend class QGL2PaintEngineExPrivate;
446 friend class QGLEngineShaderManager;
447 friend class QGLWindowSurface;
448 friend class QGLPixmapData;
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 QX11GLPixmapData;
471 friend class QX11GLSharedContexts;
472 friend class QGLContextResourceBase;
473 friend class QSGDistanceFieldGlyphCache;
475 Q_DISABLE_COPY(QGLContext)
478 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
480 class Q_OPENGL_EXPORT QGLWidget : public QWidget
483 Q_DECLARE_PRIVATE(QGLWidget)
485 explicit QGLWidget(QWidget* parent=0,
486 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
487 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
488 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
489 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
490 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
492 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
493 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
494 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
495 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
496 QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
497 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
501 void qglColor(const QColor& c) const;
502 void qglClearColor(const QColor& c) const;
504 bool isValid() const;
505 bool isSharing() const;
507 // ### Qt 5: return bools
511 bool doubleBuffer() const;
514 QGLFormat format() const;
515 void setFormat(const QGLFormat& format);
517 const QGLContext* context() const;
518 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
519 bool deleteOldContext = true);
521 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
522 QImage grabFrameBuffer(bool withAlpha = false);
524 void makeOverlayCurrent();
525 const QGLContext* overlayContext() const;
527 static QImage convertToGLFormat(const QImage& img);
529 void setMouseTracking(bool enable);
531 const QGLColormap & colormap() const;
532 void setColormap(const QGLColormap & map);
534 void renderText(int x, int y, const QString & str,
535 const QFont & fnt = QFont(), int listBase = 2000);
536 void renderText(double x, double y, double z, const QString & str,
537 const QFont & fnt = QFont(), int listBase = 2000);
538 QPaintEngine *paintEngine() const;
540 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
541 QGLContext::BindOptions options);
542 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
543 QGLContext::BindOptions options);
545 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
546 GLint format = GL_RGBA);
547 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
548 GLint format = GL_RGBA);
550 GLuint bindTexture(const QString &fileName);
552 void deleteTexture(GLuint tx_id);
554 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
555 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
557 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
558 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
559 QMacCompatGLint format = GL_RGBA);
560 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
561 QMacCompatGLint format = GL_RGBA);
562 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
563 QGLContext::BindOptions);
564 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
565 QGLContext::BindOptions);
567 void deleteTexture(QMacCompatGLuint tx_id);
569 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
570 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
574 virtual void updateGL();
575 virtual void updateOverlayGL();
578 bool event(QEvent *);
579 virtual void initializeGL();
580 virtual void resizeGL(int w, int h);
581 virtual void paintGL();
583 virtual void initializeOverlayGL();
584 virtual void resizeOverlayGL(int w, int h);
585 virtual void paintOverlayGL();
587 void setAutoBufferSwap(bool on);
588 bool autoBufferSwap() const;
590 void paintEvent(QPaintEvent*);
591 void resizeEvent(QResizeEvent*);
593 virtual void glInit();
594 virtual void glDraw();
595 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
597 QGLWidget(QGLWidgetPrivate &dd,
598 const QGLFormat &format = QGLFormat(),
600 const QGLWidget* shareWidget = 0,
601 Qt::WindowFlags f = 0);
603 Q_DISABLE_COPY(QGLWidget)
606 friend class QMacGLWindowChangeEvent;
608 friend class QGLDrawable;
609 friend class QGLPixelBuffer;
610 friend class QGLPixelBufferPrivate;
611 friend class QGLContext;
612 friend class QGLContextPrivate;
613 friend class QGLOverlayWidget;
614 friend class QOpenGLPaintEngine;
615 friend class QGLPaintDevice;
616 friend class QGLWidgetGLPaintDevice;
621 // QGLFormat inline functions
624 inline bool QGLFormat::doubleBuffer() const
626 return testOption(QGL::DoubleBuffer);
629 inline bool QGLFormat::depth() const
631 return testOption(QGL::DepthBuffer);
634 inline bool QGLFormat::rgba() const
636 return testOption(QGL::Rgba);
639 inline bool QGLFormat::alpha() const
641 return testOption(QGL::AlphaChannel);
644 inline bool QGLFormat::accum() const
646 return testOption(QGL::AccumBuffer);
649 inline bool QGLFormat::stencil() const
651 return testOption(QGL::StencilBuffer);
654 inline bool QGLFormat::stereo() const
656 return testOption(QGL::StereoBuffers);
659 inline bool QGLFormat::directRendering() const
661 return testOption(QGL::DirectRendering);
664 inline bool QGLFormat::hasOverlay() const
666 return testOption(QGL::HasOverlay);
669 inline bool QGLFormat::sampleBuffers() const
671 return testOption(QGL::SampleBuffers);