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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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 &);
310 class Q_OPENGL_EXPORT QGLContext
312 Q_DECLARE_PRIVATE(QGLContext)
314 QGLContext(const QGLFormat& format, QPaintDevice* device);
315 QGLContext(const QGLFormat& format);
316 virtual ~QGLContext();
318 virtual bool create(const QGLContext* shareContext = 0);
319 bool isValid() const;
320 bool isSharing() const;
323 static bool areSharing(const QGLContext *context1, const QGLContext *context2);
325 QGLFormat format() const;
326 QGLFormat requestedFormat() const;
327 void setFormat(const QGLFormat& format);
329 // ### Qt 5: return bools + maybe remove virtuals
330 virtual void makeCurrent();
331 virtual void doneCurrent();
333 virtual void swapBuffers() const;
336 NoBindOption = 0x0000,
337 InvertedYBindOption = 0x0001,
338 MipmapBindOption = 0x0002,
339 PremultipliedAlphaBindOption = 0x0004,
340 LinearFilteringBindOption = 0x0008,
342 MemoryManagedBindOption = 0x0010, // internal flag
343 CanFlipNativePixmapBindOption = 0x0020, // internal flag
344 TemporarilyCachedBindOption = 0x0040, // internal flag
346 DefaultBindOption = LinearFilteringBindOption
347 | InvertedYBindOption
349 InternalBindOption = MemoryManagedBindOption
350 | PremultipliedAlphaBindOption
352 Q_DECLARE_FLAGS(BindOptions, BindOption)
354 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
355 BindOptions options);
356 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
357 BindOptions options);
359 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
360 GLint format = GL_RGBA);
361 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
362 GLint format = GL_RGBA);
363 GLuint bindTexture(const QString &fileName);
365 void deleteTexture(GLuint tx_id);
367 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
368 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
370 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
371 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
372 QMacCompatGLint format = GL_RGBA);
373 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
374 QMacCompatGLint format = GL_RGBA);
375 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
377 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
380 void deleteTexture(QMacCompatGLuint tx_id);
382 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
383 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
386 static void setTextureCacheLimit(int size);
387 static int textureCacheLimit();
389 void *getProcAddress(const QString &proc) const;
390 QPaintDevice* device() const;
391 QColor overlayTransparentColor() const;
393 static const QGLContext* currentContext();
396 static QGLContext *fromPlatformGLContext(QPlatformGLContext *platformContext);
399 virtual bool chooseContext(const QGLContext* shareContext = 0);
401 #if defined(Q_WS_WIN)
402 virtual int choosePixelFormat(void* pfd, HDC pdc);
404 #if defined(Q_WS_X11) && defined(QT_NO_EGL)
405 virtual void* tryVisual(const QGLFormat& f, int bufDepth = 1);
406 virtual void* chooseVisual();
408 #if defined(Q_WS_MAC)
409 virtual void* chooseMacVisual(GDHandle);
412 bool deviceIsPixmap() const;
413 bool windowCreated() const;
414 void setWindowCreated(bool on);
415 bool initialized() const;
416 void setInitialized(bool on);
417 void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
419 uint colorIndex(const QColor& c) const;
420 void setValid(bool valid);
421 void setDevice(QPaintDevice *pDev);
424 static QGLContext* currentCtx;
428 QGLContext(QPlatformGLContext *platformContext);
431 QScopedPointer<QGLContextPrivate> d_ptr;
433 friend class QGLPixelBuffer;
434 friend class QGLPixelBufferPrivate;
435 friend class QGLWidget;
436 friend class QGLWidgetPrivate;
437 friend class QGLGlyphCache;
438 friend class QOpenGLPaintEngine;
439 friend class QOpenGLPaintEnginePrivate;
440 friend class QGL2PaintEngineEx;
441 friend class QGL2PaintEngineExPrivate;
442 friend class QGLEngineShaderManager;
443 friend class QGLWindowSurface;
444 friend class QGLPixmapData;
445 friend class QGLPixmapFilterBase;
446 friend class QGLTextureGlyphCache;
447 friend struct QGLGlyphTexture;
448 friend class QGLContextGroup;
449 friend class QGLSharedResourceGuard;
450 friend class QGLPixmapBlurFilter;
451 friend class QGLExtensions;
452 friend class QGLTexture;
453 friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
456 void updatePaintDevice();
458 friend class QMacGLWindowChangeEvent;
459 friend QGLContextPrivate *qt_phonon_get_dptr(const QGLContext *);
461 friend class QGLFramebufferObject;
462 friend class QGLFramebufferObjectPrivate;
463 friend class QGLFBOGLPaintDevice;
464 friend class QGLPaintDevice;
465 friend class QGLWidgetGLPaintDevice;
466 friend class QX11GLPixmapData;
467 friend class QX11GLSharedContexts;
468 friend class QGLContextResourceBase;
470 Q_DISABLE_COPY(QGLContext)
473 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
475 class Q_OPENGL_EXPORT QGLWidget : public QWidget
478 Q_DECLARE_PRIVATE(QGLWidget)
480 explicit QGLWidget(QWidget* parent=0,
481 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
482 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
483 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
484 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
485 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
487 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
488 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
489 QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
490 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
491 QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
492 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
496 void qglColor(const QColor& c) const;
497 void qglClearColor(const QColor& c) const;
499 bool isValid() const;
500 bool isSharing() const;
502 // ### Qt 5: return bools
506 bool doubleBuffer() const;
509 QGLFormat format() const;
510 void setFormat(const QGLFormat& format);
512 const QGLContext* context() const;
513 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
514 bool deleteOldContext = true);
516 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
517 QImage grabFrameBuffer(bool withAlpha = false);
519 void makeOverlayCurrent();
520 const QGLContext* overlayContext() const;
522 static QImage convertToGLFormat(const QImage& img);
524 void setMouseTracking(bool enable);
526 const QGLColormap & colormap() const;
527 void setColormap(const QGLColormap & map);
529 void renderText(int x, int y, const QString & str,
530 const QFont & fnt = QFont(), int listBase = 2000);
531 void renderText(double x, double y, double z, const QString & str,
532 const QFont & fnt = QFont(), int listBase = 2000);
533 QPaintEngine *paintEngine() const;
535 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
536 QGLContext::BindOptions options);
537 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
538 QGLContext::BindOptions options);
540 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
541 GLint format = GL_RGBA);
542 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
543 GLint format = GL_RGBA);
545 GLuint bindTexture(const QString &fileName);
547 void deleteTexture(GLuint tx_id);
549 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
550 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
552 #ifdef Q_MAC_COMPAT_GL_FUNCTIONS
553 GLuint bindTexture(const QImage &image, QMacCompatGLenum = GL_TEXTURE_2D,
554 QMacCompatGLint format = GL_RGBA);
555 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum = GL_TEXTURE_2D,
556 QMacCompatGLint format = GL_RGBA);
557 GLuint bindTexture(const QImage &image, QMacCompatGLenum, QMacCompatGLint format,
558 QGLContext::BindOptions);
559 GLuint bindTexture(const QPixmap &pixmap, QMacCompatGLenum, QMacCompatGLint format,
560 QGLContext::BindOptions);
562 void deleteTexture(QMacCompatGLuint tx_id);
564 void drawTexture(const QRectF &target, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
565 void drawTexture(const QPointF &point, QMacCompatGLuint textureId, QMacCompatGLenum textureTarget = GL_TEXTURE_2D);
569 virtual void updateGL();
570 virtual void updateOverlayGL();
573 bool event(QEvent *);
574 virtual void initializeGL();
575 virtual void resizeGL(int w, int h);
576 virtual void paintGL();
578 virtual void initializeOverlayGL();
579 virtual void resizeOverlayGL(int w, int h);
580 virtual void paintOverlayGL();
582 void setAutoBufferSwap(bool on);
583 bool autoBufferSwap() const;
585 void paintEvent(QPaintEvent*);
586 void resizeEvent(QResizeEvent*);
588 virtual void glInit();
589 virtual void glDraw();
590 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
593 Q_DISABLE_COPY(QGLWidget)
596 friend class QMacGLWindowChangeEvent;
598 friend class QGLDrawable;
599 friend class QGLPixelBuffer;
600 friend class QGLPixelBufferPrivate;
601 friend class QGLContext;
602 friend class QGLContextPrivate;
603 friend class QGLOverlayWidget;
604 friend class QOpenGLPaintEngine;
605 friend class QGLPaintDevice;
606 friend class QGLWidgetGLPaintDevice;
611 // QGLFormat inline functions
614 inline bool QGLFormat::doubleBuffer() const
616 return testOption(QGL::DoubleBuffer);
619 inline bool QGLFormat::depth() const
621 return testOption(QGL::DepthBuffer);
624 inline bool QGLFormat::rgba() const
626 return testOption(QGL::Rgba);
629 inline bool QGLFormat::alpha() const
631 return testOption(QGL::AlphaChannel);
634 inline bool QGLFormat::accum() const
636 return testOption(QGL::AccumBuffer);
639 inline bool QGLFormat::stencil() const
641 return testOption(QGL::StencilBuffer);
644 inline bool QGLFormat::stereo() const
646 return testOption(QGL::StereoBuffers);
649 inline bool QGLFormat::directRendering() const
651 return testOption(QGL::DirectRendering);
654 inline bool QGLFormat::hasOverlay() const
656 return testOption(QGL::HasOverlay);
659 inline bool QGLFormat::sampleBuffers() const
661 return testOption(QGL::SampleBuffers);