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>
61 #if 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 #if defined(Q_WS_QPA)
288 static QGLFormat fromPlatformWindowFormat(const QPlatformWindowFormat &format);
289 static QPlatformWindowFormat toPlatformWindowFormat(const QGLFormat &format);
296 friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
297 friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
298 #ifndef QT_NO_DEBUG_STREAM
299 friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
303 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
305 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
306 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
308 #ifndef QT_NO_DEBUG_STREAM
309 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
314 class Q_OPENGL_EXPORT QGLContext
316 Q_DECLARE_PRIVATE(QGLContext)
318 QGLContext(const QGLFormat& format, QPaintDevice* device);
319 QGLContext(const QGLFormat& format);
320 virtual ~QGLContext();
322 virtual bool create(const QGLContext* shareContext = 0);
323 bool isValid() const;
324 bool isSharing() const;
327 static bool areSharing(const QGLContext *context1, const QGLContext *context2);
329 QGLFormat format() const;
330 QGLFormat requestedFormat() const;
331 void setFormat(const QGLFormat& format);
333 // ### Qt 5: return bools + maybe remove virtuals
334 virtual void makeCurrent();
335 virtual void doneCurrent();
337 virtual void swapBuffers() const;
339 QGLFunctions *functions() const;
342 NoBindOption = 0x0000,
343 InvertedYBindOption = 0x0001,
344 MipmapBindOption = 0x0002,
345 PremultipliedAlphaBindOption = 0x0004,
346 LinearFilteringBindOption = 0x0008,
348 MemoryManagedBindOption = 0x0010, // internal flag
349 CanFlipNativePixmapBindOption = 0x0020, // internal flag
350 TemporarilyCachedBindOption = 0x0040, // internal flag
352 DefaultBindOption = LinearFilteringBindOption
353 | InvertedYBindOption
355 InternalBindOption = MemoryManagedBindOption
356 | PremultipliedAlphaBindOption
358 Q_DECLARE_FLAGS(BindOptions, BindOption)
360 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
361 BindOptions options);
362 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
363 BindOptions options);
365 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
366 GLint format = GL_RGBA);
367 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
368 GLint format = GL_RGBA);
369 GLuint bindTexture(const QString &fileName);
371 void deleteTexture(GLuint tx_id);
373 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
374 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
376 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
377 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
378 QMacCompatGLint format = GL_RGBA);
379 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
380 QMacCompatGLint format = GL_RGBA);
381 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
383 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
386 void deleteTexture(QMacCompatGLuint tx_id);
388 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
389 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
392 static void setTextureCacheLimit(int size);
393 static int textureCacheLimit();
395 void *getProcAddress(const QString &proc) const;
396 QPaintDevice* device() const;
397 QColor overlayTransparentColor() const;
399 static const QGLContext* currentContext();
402 static QGLContext *fromPlatformGLContext(QPlatformGLContext *platformContext);
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(QPlatformGLContext *platformContext);
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 QGLWindowSurface;
450 friend class QGLPixmapData;
451 friend class QGLPixmapFilterBase;
452 friend class QGLTextureGlyphCache;
453 friend struct QGLGlyphTexture;
454 friend class QGLContextGroup;
455 friend class QGLSharedResourceGuard;
456 friend class QGLPixmapBlurFilter;
457 friend class QGLExtensions;
458 friend class QGLTexture;
459 friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
462 void updatePaintDevice();
464 friend class QMacGLWindowChangeEvent;
465 friend QGLContextPrivate *qt_phonon_get_dptr(const QGLContext *);
467 friend class QGLFramebufferObject;
468 friend class QGLFramebufferObjectPrivate;
469 friend class QGLFBOGLPaintDevice;
470 friend class QGLPaintDevice;
471 friend class QGLWidgetGLPaintDevice;
472 friend class QX11GLPixmapData;
473 friend class QX11GLSharedContexts;
474 friend class QGLContextResourceBase;
475 friend class QSGDistanceFieldGlyphCache;
477 Q_DISABLE_COPY(QGLContext)
480 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
482 class Q_OPENGL_EXPORT QGLWidget : public QWidget
485 Q_DECLARE_PRIVATE(QGLWidget)
487 explicit QGLWidget(QWidget* parent=0,
488 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
489 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
490 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
491 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
492 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
494 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
495 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
496 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
497 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
498 QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
499 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
503 void qglColor(const QColor& c) const;
504 void qglClearColor(const QColor& c) const;
506 bool isValid() const;
507 bool isSharing() const;
509 // ### Qt 5: return bools
513 bool doubleBuffer() const;
516 QGLFormat format() const;
517 void setFormat(const QGLFormat& format);
519 const QGLContext* context() const;
520 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
521 bool deleteOldContext = true);
523 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
524 QImage grabFrameBuffer(bool withAlpha = false);
526 void makeOverlayCurrent();
527 const QGLContext* overlayContext() const;
529 static QImage convertToGLFormat(const QImage& img);
531 void setMouseTracking(bool enable);
533 const QGLColormap & colormap() const;
534 void setColormap(const QGLColormap & map);
536 void renderText(int x, int y, const QString & str,
537 const QFont & fnt = QFont(), int listBase = 2000);
538 void renderText(double x, double y, double z, const QString & str,
539 const QFont & fnt = QFont(), int listBase = 2000);
540 QPaintEngine *paintEngine() const;
542 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
543 QGLContext::BindOptions options);
544 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
545 QGLContext::BindOptions options);
547 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
548 GLint format = GL_RGBA);
549 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
550 GLint format = GL_RGBA);
552 GLuint bindTexture(const QString &fileName);
554 void deleteTexture(GLuint tx_id);
556 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
557 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
559 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
560 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
561 QMacCompatGLint format = GL_RGBA);
562 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
563 QMacCompatGLint format = GL_RGBA);
564 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
565 QGLContext::BindOptions);
566 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
567 QGLContext::BindOptions);
569 void deleteTexture(QMacCompatGLuint tx_id);
571 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
572 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
576 virtual void updateGL();
577 virtual void updateOverlayGL();
580 bool event(QEvent *);
581 virtual void initializeGL();
582 virtual void resizeGL(int w, int h);
583 virtual void paintGL();
585 virtual void initializeOverlayGL();
586 virtual void resizeOverlayGL(int w, int h);
587 virtual void paintOverlayGL();
589 void setAutoBufferSwap(bool on);
590 bool autoBufferSwap() const;
592 void paintEvent(QPaintEvent*);
593 void resizeEvent(QResizeEvent*);
595 virtual void glInit();
596 virtual void glDraw();
597 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
599 QGLWidget(QGLWidgetPrivate &dd,
600 const QGLFormat &format = QGLFormat(),
602 const QGLWidget* shareWidget = 0,
603 Qt::WindowFlags f = 0);
605 Q_DISABLE_COPY(QGLWidget)
608 friend class QMacGLWindowChangeEvent;
610 friend class QGLDrawable;
611 friend class QGLPixelBuffer;
612 friend class QGLPixelBufferPrivate;
613 friend class QGLContext;
614 friend class QGLContextPrivate;
615 friend class QGLOverlayWidget;
616 friend class QOpenGLPaintEngine;
617 friend class QGLPaintDevice;
618 friend class QGLWidgetGLPaintDevice;
623 // QGLFormat inline functions
626 inline bool QGLFormat::doubleBuffer() const
628 return testOption(QGL::DoubleBuffer);
631 inline bool QGLFormat::depth() const
633 return testOption(QGL::DepthBuffer);
636 inline bool QGLFormat::rgba() const
638 return testOption(QGL::Rgba);
641 inline bool QGLFormat::alpha() const
643 return testOption(QGL::AlphaChannel);
646 inline bool QGLFormat::accum() const
648 return testOption(QGL::AccumBuffer);
651 inline bool QGLFormat::stencil() const
653 return testOption(QGL::StencilBuffer);
656 inline bool QGLFormat::stereo() const
658 return testOption(QGL::StereoBuffers);
661 inline bool QGLFormat::directRendering() const
663 return testOption(QGL::DirectRendering);
666 inline bool QGLFormat::hasOverlay() const
668 return testOption(QGL::HasOverlay);
671 inline bool QGLFormat::sampleBuffers() const
673 return testOption(QGL::SampleBuffers);