1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtOpenGL module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
47 #include <QtWidgets/qwidget.h>
48 #include <QtGui/qpaintengine.h>
49 #include <QtOpenGL/qglcolormap.h>
50 #include <QtCore/qmap.h>
51 #include <QtCore/qscopedpointer.h>
53 #include <QtGui/QSurfaceFormat>
58 # include <QtCore/qt_windows.h>
62 # include <OpenGL/gl.h>
63 #elif defined(QT_OPENGL_ES_2)
64 # if defined(Q_OS_MAC)
65 # include <OpenGLES/ES2/gl.h>
67 # include <GLES2/gl2.h>
70 # define GL_DOUBLE GL_FLOAT
73 typedef GLfloat GLdouble;
76 # if defined(Q_OS_MAC)
77 # include <OpenGL/gl.h>
89 class QGLWidgetPrivate;
90 class QGLContextPrivate;
96 DoubleBuffer = 0x0001,
99 AlphaChannel = 0x0008,
100 AccumBuffer = 0x0010,
101 StencilBuffer = 0x0020,
102 StereoBuffers = 0x0040,
103 DirectRendering = 0x0080,
105 SampleBuffers = 0x0200,
106 DeprecatedFunctions = 0x0400,
107 SingleBuffer = DoubleBuffer << 16,
108 NoDepthBuffer = DepthBuffer << 16,
109 ColorIndex = Rgba << 16,
110 NoAlphaChannel = AlphaChannel << 16,
111 NoAccumBuffer = AccumBuffer << 16,
112 NoStencilBuffer = StencilBuffer << 16,
113 NoStereoBuffers = StereoBuffers << 16,
114 IndirectRendering = DirectRendering << 16,
115 NoOverlay = HasOverlay << 16,
116 NoSampleBuffers = SampleBuffers << 16,
117 NoDeprecatedFunctions = DeprecatedFunctions << 16
119 Q_DECLARE_FLAGS(FormatOptions, FormatOption)
122 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
124 class QGLFormatPrivate;
126 class Q_OPENGL_EXPORT QGLFormat
130 QGLFormat(QGL::FormatOptions options, int plane = 0);
131 QGLFormat(const QGLFormat &other);
132 QGLFormat &operator=(const QGLFormat &other);
135 void setDepthBufferSize(int size);
136 int depthBufferSize() const;
138 void setAccumBufferSize(int size);
139 int accumBufferSize() const;
141 void setRedBufferSize(int size);
142 int redBufferSize() const;
144 void setGreenBufferSize(int size);
145 int greenBufferSize() const;
147 void setBlueBufferSize(int size);
148 int blueBufferSize() const;
150 void setAlphaBufferSize(int size);
151 int alphaBufferSize() const;
153 void setStencilBufferSize(int size);
154 int stencilBufferSize() const;
156 void setSampleBuffers(bool enable);
157 bool sampleBuffers() const;
159 void setSamples(int numSamples);
162 void setSwapInterval(int interval);
163 int swapInterval() const;
165 bool doubleBuffer() const;
166 void setDoubleBuffer(bool enable);
168 void setDepth(bool enable);
170 void setRgba(bool enable);
172 void setAlpha(bool enable);
174 void setAccum(bool enable);
175 bool stencil() const;
176 void setStencil(bool enable);
178 void setStereo(bool enable);
179 bool directRendering() const;
180 void setDirectRendering(bool enable);
181 bool hasOverlay() const;
182 void setOverlay(bool enable);
185 void setPlane(int plane);
187 void setOption(QGL::FormatOptions opt);
188 bool testOption(QGL::FormatOptions opt) const;
190 static QGLFormat defaultFormat();
191 static void setDefaultFormat(const QGLFormat& f);
193 static QGLFormat defaultOverlayFormat();
194 static void setDefaultOverlayFormat(const QGLFormat& f);
196 static bool hasOpenGL();
197 static bool hasOpenGLOverlays();
199 void setVersion(int major, int minor);
200 int majorVersion() const;
201 int minorVersion() const;
203 enum OpenGLContextProfile {
209 void setProfile(OpenGLContextProfile profile);
210 OpenGLContextProfile profile() const;
212 enum OpenGLVersionFlag {
213 OpenGL_Version_None = 0x00000000,
214 OpenGL_Version_1_1 = 0x00000001,
215 OpenGL_Version_1_2 = 0x00000002,
216 OpenGL_Version_1_3 = 0x00000004,
217 OpenGL_Version_1_4 = 0x00000008,
218 OpenGL_Version_1_5 = 0x00000010,
219 OpenGL_Version_2_0 = 0x00000020,
220 OpenGL_Version_2_1 = 0x00000040,
221 OpenGL_ES_Common_Version_1_0 = 0x00000080,
222 OpenGL_ES_CommonLite_Version_1_0 = 0x00000100,
223 OpenGL_ES_Common_Version_1_1 = 0x00000200,
224 OpenGL_ES_CommonLite_Version_1_1 = 0x00000400,
225 OpenGL_ES_Version_2_0 = 0x00000800,
226 OpenGL_Version_3_0 = 0x00001000,
227 OpenGL_Version_3_1 = 0x00002000,
228 OpenGL_Version_3_2 = 0x00004000,
229 OpenGL_Version_3_3 = 0x00008000,
230 OpenGL_Version_4_0 = 0x00010000
232 Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
234 static OpenGLVersionFlags openGLVersionFlags();
236 static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
237 static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
243 friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
244 friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
245 #ifndef QT_NO_DEBUG_STREAM
246 friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
250 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
252 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
253 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
255 #ifndef QT_NO_DEBUG_STREAM
256 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
261 class Q_OPENGL_EXPORT QGLContext
263 Q_DECLARE_PRIVATE(QGLContext)
265 QGLContext(const QGLFormat& format, QPaintDevice* device);
266 QGLContext(const QGLFormat& format);
267 virtual ~QGLContext();
269 virtual bool create(const QGLContext* shareContext = 0);
270 bool isValid() const;
271 bool isSharing() const;
274 static bool areSharing(const QGLContext *context1, const QGLContext *context2);
276 QGLFormat format() const;
277 QGLFormat requestedFormat() const;
278 void setFormat(const QGLFormat& format);
280 // ### Qt 5: return bools + maybe remove virtuals
281 virtual void makeCurrent();
282 virtual void doneCurrent();
284 virtual void swapBuffers() const;
286 QGLFunctions *functions() const;
289 NoBindOption = 0x0000,
290 InvertedYBindOption = 0x0001,
291 MipmapBindOption = 0x0002,
292 PremultipliedAlphaBindOption = 0x0004,
293 LinearFilteringBindOption = 0x0008,
295 MemoryManagedBindOption = 0x0010, // internal flag
296 CanFlipNativePixmapBindOption = 0x0020, // internal flag
297 TemporarilyCachedBindOption = 0x0040, // internal flag
299 DefaultBindOption = LinearFilteringBindOption
300 | InvertedYBindOption
302 InternalBindOption = MemoryManagedBindOption
303 | PremultipliedAlphaBindOption
305 Q_DECLARE_FLAGS(BindOptions, BindOption)
307 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
308 BindOptions options);
309 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
310 BindOptions options);
312 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
313 GLint format = GL_RGBA);
314 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
315 GLint format = GL_RGBA);
316 GLuint bindTexture(const QString &fileName);
318 void deleteTexture(GLuint tx_id);
320 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
321 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
323 static void setTextureCacheLimit(int size);
324 static int textureCacheLimit();
326 QFunctionPointer getProcAddress(const QString &proc) const;
327 QPaintDevice* device() const;
328 QColor overlayTransparentColor() const;
330 static const QGLContext* currentContext();
332 static QGLContext *fromOpenGLContext(QOpenGLContext *platformContext);
333 QOpenGLContext *contextHandle() const;
336 virtual bool chooseContext(const QGLContext* shareContext = 0);
338 bool deviceIsPixmap() const;
339 bool windowCreated() const;
340 void setWindowCreated(bool on);
341 bool initialized() const;
342 void setInitialized(bool on);
343 void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
345 uint colorIndex(const QColor& c) const;
346 void setValid(bool valid);
347 void setDevice(QPaintDevice *pDev);
350 static QGLContext* currentCtx;
353 QGLContext(QOpenGLContext *windowContext);
355 QScopedPointer<QGLContextPrivate> d_ptr;
357 friend class QGLPixelBuffer;
358 friend class QGLPixelBufferPrivate;
359 friend class QGLWidget;
360 friend class QGLWidgetPrivate;
361 friend class QGLGlyphCache;
362 friend class QGL2PaintEngineEx;
363 friend class QGL2PaintEngineExPrivate;
364 friend class QGLEngineShaderManager;
365 friend class QGLTextureGlyphCache;
366 friend struct QGLGlyphTexture;
367 friend class QGLContextGroup;
368 friend class QGLPixmapBlurFilter;
369 friend class QGLExtensions;
370 friend class QGLTexture;
371 friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
372 friend class QGLFramebufferObject;
373 friend class QGLFramebufferObjectPrivate;
374 friend class QGLFBOGLPaintDevice;
375 friend class QGLPaintDevice;
376 friend class QGLWidgetGLPaintDevice;
377 friend class QX11GLSharedContexts;
378 friend class QGLContextResourceBase;
379 friend class QSGDistanceFieldGlyphCache;
381 Q_DISABLE_COPY(QGLContext)
384 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
386 class Q_OPENGL_EXPORT QGLWidget : public QWidget
389 Q_DECLARE_PRIVATE(QGLWidget)
391 explicit QGLWidget(QWidget* parent=0,
392 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
393 explicit QGLWidget(QGLContext *context, QWidget* parent=0,
394 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
395 explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
396 const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
399 void qglColor(const QColor& c) const;
400 void qglClearColor(const QColor& c) const;
402 bool isValid() const;
403 bool isSharing() const;
405 // ### Qt 5: return bools
409 bool doubleBuffer() const;
412 QGLFormat format() const;
413 void setFormat(const QGLFormat& format);
415 const QGLContext* context() const;
416 void setContext(QGLContext* context, const QGLContext* shareContext = 0,
417 bool deleteOldContext = true);
419 QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
420 QImage grabFrameBuffer(bool withAlpha = false);
422 void makeOverlayCurrent();
423 const QGLContext* overlayContext() const;
425 static QImage convertToGLFormat(const QImage& img);
427 void setMouseTracking(bool enable);
429 const QGLColormap & colormap() const;
430 void setColormap(const QGLColormap & map);
432 void renderText(int x, int y, const QString & str,
433 const QFont & fnt = QFont(), int listBase = 2000);
434 void renderText(double x, double y, double z, const QString & str,
435 const QFont & fnt = QFont(), int listBase = 2000);
436 QPaintEngine *paintEngine() const;
438 GLuint bindTexture(const QImage &image, GLenum target, GLint format,
439 QGLContext::BindOptions options);
440 GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
441 QGLContext::BindOptions options);
443 GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
444 GLint format = GL_RGBA);
445 GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
446 GLint format = GL_RGBA);
448 GLuint bindTexture(const QString &fileName);
450 void deleteTexture(GLuint tx_id);
452 void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
453 void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
456 virtual void updateGL();
457 virtual void updateOverlayGL();
460 bool event(QEvent *);
461 virtual void initializeGL();
462 virtual void resizeGL(int w, int h);
463 virtual void paintGL();
465 virtual void initializeOverlayGL();
466 virtual void resizeOverlayGL(int w, int h);
467 virtual void paintOverlayGL();
469 void setAutoBufferSwap(bool on);
470 bool autoBufferSwap() const;
472 void paintEvent(QPaintEvent*);
473 void resizeEvent(QResizeEvent*);
475 virtual void glInit();
476 virtual void glDraw();
477 int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
479 QGLWidget(QGLWidgetPrivate &dd,
480 const QGLFormat &format = QGLFormat(),
482 const QGLWidget* shareWidget = 0,
483 Qt::WindowFlags f = 0);
485 Q_DISABLE_COPY(QGLWidget)
487 friend class QGLDrawable;
488 friend class QGLPixelBuffer;
489 friend class QGLPixelBufferPrivate;
490 friend class QGLContext;
491 friend class QGLContextPrivate;
492 friend class QGLOverlayWidget;
493 friend class QGLPaintDevice;
494 friend class QGLWidgetGLPaintDevice;
499 // QGLFormat inline functions
502 inline bool QGLFormat::doubleBuffer() const
504 return testOption(QGL::DoubleBuffer);
507 inline bool QGLFormat::depth() const
509 return testOption(QGL::DepthBuffer);
512 inline bool QGLFormat::rgba() const
514 return testOption(QGL::Rgba);
517 inline bool QGLFormat::alpha() const
519 return testOption(QGL::AlphaChannel);
522 inline bool QGLFormat::accum() const
524 return testOption(QGL::AccumBuffer);
527 inline bool QGLFormat::stencil() const
529 return testOption(QGL::StencilBuffer);
532 inline bool QGLFormat::stereo() const
534 return testOption(QGL::StereoBuffers);
537 inline bool QGLFormat::directRendering() const
539 return testOption(QGL::DirectRendering);
542 inline bool QGLFormat::hasOverlay() const
544 return testOption(QGL::HasOverlay);
547 inline bool QGLFormat::sampleBuffers() const
549 return testOption(QGL::SampleBuffers);
556 #endif // QT_NO_OPENGL