1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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>
88 class QGLWidgetPrivate;
89 class QGLContextPrivate;
95 DoubleBuffer = 0x0001,
98 AlphaChannel = 0x0008,
100 StencilBuffer = 0x0020,
101 StereoBuffers = 0x0040,
102 DirectRendering = 0x0080,
104 SampleBuffers = 0x0200,
105 DeprecatedFunctions = 0x0400,
106 SingleBuffer = DoubleBuffer << 16,
107 NoDepthBuffer = DepthBuffer << 16,
108 ColorIndex = Rgba << 16,
109 NoAlphaChannel = AlphaChannel << 16,
110 NoAccumBuffer = AccumBuffer << 16,
111 NoStencilBuffer = StencilBuffer << 16,
112 NoStereoBuffers = StereoBuffers << 16,
113 IndirectRendering = DirectRendering << 16,
114 NoOverlay = HasOverlay << 16,
115 NoSampleBuffers = SampleBuffers << 16,
116 NoDeprecatedFunctions = DeprecatedFunctions << 16
118 Q_DECLARE_FLAGS(FormatOptions, FormatOption)
121 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
123 class QGLFormatPrivate;
125 class Q_OPENGL_EXPORT QGLFormat
129 QGLFormat(QGL::FormatOptions options, int plane = 0);
130 QGLFormat(const QGLFormat &other);
131 QGLFormat &operator=(const QGLFormat &other);
134 void setDepthBufferSize(int size);
135 int depthBufferSize() const;
137 void setAccumBufferSize(int size);
138 int accumBufferSize() const;
140 void setRedBufferSize(int size);
141 int redBufferSize() const;
143 void setGreenBufferSize(int size);
144 int greenBufferSize() const;
146 void setBlueBufferSize(int size);
147 int blueBufferSize() const;
149 void setAlphaBufferSize(int size);
150 int alphaBufferSize() const;
152 void setStencilBufferSize(int size);
153 int stencilBufferSize() const;
155 void setSampleBuffers(bool enable);
156 bool sampleBuffers() const;
158 void setSamples(int numSamples);
161 void setSwapInterval(int interval);
162 int swapInterval() const;
164 bool doubleBuffer() const;
165 void setDoubleBuffer(bool enable);
167 void setDepth(bool enable);
169 void setRgba(bool enable);
171 void setAlpha(bool enable);
173 void setAccum(bool enable);
174 bool stencil() const;
175 void setStencil(bool enable);
177 void setStereo(bool enable);
178 bool directRendering() const;
179 void setDirectRendering(bool enable);
180 bool hasOverlay() const;
181 void setOverlay(bool enable);
184 void setPlane(int plane);
186 void setOption(QGL::FormatOptions opt);
187 bool testOption(QGL::FormatOptions opt) const;
189 static QGLFormat defaultFormat();
190 static void setDefaultFormat(const QGLFormat& f);
192 static QGLFormat defaultOverlayFormat();
193 static void setDefaultOverlayFormat(const QGLFormat& f);
195 static bool hasOpenGL();
196 static bool hasOpenGLOverlays();
198 void setVersion(int major, int minor);
199 int majorVersion() const;
200 int minorVersion() const;
202 enum OpenGLContextProfile {
208 void setProfile(OpenGLContextProfile profile);
209 OpenGLContextProfile profile() const;
211 enum OpenGLVersionFlag {
212 OpenGL_Version_None = 0x00000000,
213 OpenGL_Version_1_1 = 0x00000001,
214 OpenGL_Version_1_2 = 0x00000002,
215 OpenGL_Version_1_3 = 0x00000004,
216 OpenGL_Version_1_4 = 0x00000008,
217 OpenGL_Version_1_5 = 0x00000010,
218 OpenGL_Version_2_0 = 0x00000020,
219 OpenGL_Version_2_1 = 0x00000040,
220 OpenGL_ES_Common_Version_1_0 = 0x00000080,
221 OpenGL_ES_CommonLite_Version_1_0 = 0x00000100,
222 OpenGL_ES_Common_Version_1_1 = 0x00000200,
223 OpenGL_ES_CommonLite_Version_1_1 = 0x00000400,
224 OpenGL_ES_Version_2_0 = 0x00000800,
225 OpenGL_Version_3_0 = 0x00001000,
226 OpenGL_Version_3_1 = 0x00002000,
227 OpenGL_Version_3_2 = 0x00004000,
228 OpenGL_Version_3_3 = 0x00008000,
229 OpenGL_Version_4_0 = 0x00010000
231 Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
233 static OpenGLVersionFlags openGLVersionFlags();
235 static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
236 static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
242 friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
243 friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
244 #ifndef QT_NO_DEBUG_STREAM
245 friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
249 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
251 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
252 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
254 #ifndef QT_NO_DEBUG_STREAM
255 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
260 class Q_OPENGL_EXPORT QGLContext
262 Q_DECLARE_PRIVATE(QGLContext)
264 QGLContext(const QGLFormat& format, QPaintDevice* device);
265 QGLContext(const QGLFormat& format);
266 virtual ~QGLContext();
268 virtual bool create(const QGLContext* shareContext = 0);
269 bool isValid() const;
270 bool isSharing() const;
273 static bool areSharing(const QGLContext *context1, const QGLContext *context2);
275 QGLFormat format() const;
276 QGLFormat requestedFormat() const;
277 void setFormat(const QGLFormat& format);
279 // ### Qt 5: return bools + maybe remove virtuals
280 virtual void makeCurrent();
281 virtual void doneCurrent();
283 virtual void swapBuffers() const;
285 QGLFunctions *functions() const;
288 NoBindOption = 0x0000,
289 InvertedYBindOption = 0x0001,
290 MipmapBindOption = 0x0002,
291 PremultipliedAlphaBindOption = 0x0004,
292 LinearFilteringBindOption = 0x0008,
294 MemoryManagedBindOption = 0x0010, // internal flag
295 CanFlipNativePixmapBindOption = 0x0020, // internal flag
296 TemporarilyCachedBindOption = 0x0040, // internal flag
298 DefaultBindOption = LinearFilteringBindOption
299 | InvertedYBindOption
301 InternalBindOption = MemoryManagedBindOption
302 | PremultipliedAlphaBindOption
304 Q_DECLARE_FLAGS(BindOptions, BindOption)
306 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
307 BindOptions options);
308 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
309 BindOptions options);
311 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
312 GLint format = GL_RGBA);
313 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
314 GLint format = GL_RGBA);
315 GLuint bindTexture(const QString &fileName);
317 void deleteTexture(GLuint tx_id);
319 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
320 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
322 static void setTextureCacheLimit(int size);
323 static int textureCacheLimit();
325 QFunctionPointer getProcAddress(const QString &proc) const;
326 QPaintDevice* device() const;
327 QColor overlayTransparentColor() const;
329 static const QGLContext* currentContext();
331 static QGLContext *fromOpenGLContext(QOpenGLContext *platformContext);
332 QOpenGLContext *contextHandle() const;
335 virtual bool chooseContext(const QGLContext* shareContext = 0);
337 bool deviceIsPixmap() const;
338 bool windowCreated() const;
339 void setWindowCreated(bool on);
340 bool initialized() const;
341 void setInitialized(bool on);
342 void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
344 uint colorIndex(const QColor& c) const;
345 void setValid(bool valid);
346 void setDevice(QPaintDevice *pDev);
349 static QGLContext* currentCtx;
352 QGLContext(QOpenGLContext *windowContext);
354 QScopedPointer<QGLContextPrivate> d_ptr;
356 friend class QGLPixelBuffer;
357 friend class QGLPixelBufferPrivate;
358 friend class QGLWidget;
359 friend class QGLWidgetPrivate;
360 friend class QGLGlyphCache;
361 friend class QGL2PaintEngineEx;
362 friend class QGL2PaintEngineExPrivate;
363 friend class QGLEngineShaderManager;
364 friend class QGLTextureGlyphCache;
365 friend struct QGLGlyphTexture;
366 friend class QGLContextGroup;
367 friend class QGLPixmapBlurFilter;
368 friend class QGLExtensions;
369 friend class QGLTexture;
370 friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
371 friend class QGLFramebufferObject;
372 friend class QGLFramebufferObjectPrivate;
373 friend class QGLFBOGLPaintDevice;
374 friend class QGLPaintDevice;
375 friend class QGLWidgetGLPaintDevice;
376 friend class QX11GLSharedContexts;
377 friend class QGLContextResourceBase;
378 friend class QSGDistanceFieldGlyphCache;
380 Q_DISABLE_COPY(QGLContext)
383 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
385 class Q_OPENGL_EXPORT QGLWidget : public QWidget
388 Q_DECLARE_PRIVATE(QGLWidget)
390 explicit QGLWidget(QWidget* parent=0,
391 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
392 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
393 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
394 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
395 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
398 void qglColor(const QColor& c) const;
399 void qglClearColor(const QColor& c) const;
401 bool isValid() const;
402 bool isSharing() const;
404 // ### Qt 5: return bools
408 bool doubleBuffer() const;
411 QGLFormat format() const;
412 void setFormat(const QGLFormat& format);
414 const QGLContext* context() const;
415 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
416 bool deleteOldContext = true);
418 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
419 QImage grabFrameBuffer(bool withAlpha = false);
421 void makeOverlayCurrent();
422 const QGLContext* overlayContext() const;
424 static QImage convertToGLFormat(const QImage& img);
426 void setMouseTracking(bool enable);
428 const QGLColormap & colormap() const;
429 void setColormap(const QGLColormap & map);
431 void renderText(int x, int y, const QString & str,
432 const QFont & fnt = QFont(), int listBase = 2000);
433 void renderText(double x, double y, double z, const QString & str,
434 const QFont & fnt = QFont(), int listBase = 2000);
435 QPaintEngine *paintEngine() const;
437 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
438 QGLContext::BindOptions options);
439 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
440 QGLContext::BindOptions options);
442 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
443 GLint format = GL_RGBA);
444 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
445 GLint format = GL_RGBA);
447 GLuint bindTexture(const QString &fileName);
449 void deleteTexture(GLuint tx_id);
451 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
452 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
455 virtual void updateGL();
456 virtual void updateOverlayGL();
459 bool event(QEvent *);
460 virtual void initializeGL();
461 virtual void resizeGL(int w, int h);
462 virtual void paintGL();
464 virtual void initializeOverlayGL();
465 virtual void resizeOverlayGL(int w, int h);
466 virtual void paintOverlayGL();
468 void setAutoBufferSwap(bool on);
469 bool autoBufferSwap() const;
471 void paintEvent(QPaintEvent*);
472 void resizeEvent(QResizeEvent*);
474 virtual void glInit();
475 virtual void glDraw();
476 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
478 QGLWidget(QGLWidgetPrivate &dd,
479 const QGLFormat &format = QGLFormat(),
481 const QGLWidget* shareWidget = 0,
482 Qt::WindowFlags f = 0);
484 Q_DISABLE_COPY(QGLWidget)
486 friend class QGLDrawable;
487 friend class QGLPixelBuffer;
488 friend class QGLPixelBufferPrivate;
489 friend class QGLContext;
490 friend class QGLContextPrivate;
491 friend class QGLOverlayWidget;
492 friend class QGLPaintDevice;
493 friend class QGLWidgetGLPaintDevice;
498 // QGLFormat inline functions
501 inline bool QGLFormat::doubleBuffer() const
503 return testOption(QGL::DoubleBuffer);
506 inline bool QGLFormat::depth() const
508 return testOption(QGL::DepthBuffer);
511 inline bool QGLFormat::rgba() const
513 return testOption(QGL::Rgba);
516 inline bool QGLFormat::alpha() const
518 return testOption(QGL::AlphaChannel);
521 inline bool QGLFormat::accum() const
523 return testOption(QGL::AccumBuffer);
526 inline bool QGLFormat::stencil() const
528 return testOption(QGL::StencilBuffer);
531 inline bool QGLFormat::stereo() const
533 return testOption(QGL::StereoBuffers);
536 inline bool QGLFormat::directRendering() const
538 return testOption(QGL::DirectRendering);
541 inline bool QGLFormat::hasOverlay() const
543 return testOption(QGL::HasOverlay);
546 inline bool QGLFormat::sampleBuffers() const
548 return testOption(QGL::SampleBuffers);