Removed Qt 5 todo comments from qgl.h / qgl_p.h.
[profile/ivi/qtbase.git] / src / opengl / qgl.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtOpenGL module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QGL_H
43 #define QGL_H
44
45 #ifndef QT_NO_OPENGL
46
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>
52
53 #include <QtGui/QSurfaceFormat>
54
55 QT_BEGIN_HEADER
56
57 #if defined(Q_OS_WIN)
58 # include <QtCore/qt_windows.h>
59 #endif
60
61 #if defined(Q_OS_MAC)
62 # include <OpenGL/gl.h>
63 #elif defined(QT_OPENGL_ES_2)
64 # if defined(Q_OS_MAC)
65 #  include <OpenGLES/ES2/gl.h>
66 # else
67 #  include <GLES2/gl2.h>
68 # endif
69 # ifndef GL_DOUBLE
70 #  define GL_DOUBLE GL_FLOAT
71 # endif
72 # ifndef GLdouble
73 typedef GLfloat GLdouble;
74 # endif
75 #else
76 # if defined(Q_OS_MAC)
77 #  include <OpenGL/gl.h>
78 # else
79 #  include <GL/gl.h>
80 # endif
81 #endif
82
83 QT_BEGIN_NAMESPACE
84
85
86
87
88 class QPixmap;
89 class QGLWidgetPrivate;
90 class QGLContextPrivate;
91
92 // Namespace class:
93 namespace QGL
94 {
95     enum FormatOption {
96         DoubleBuffer            = 0x0001,
97         DepthBuffer             = 0x0002,
98         Rgba                    = 0x0004,
99         AlphaChannel            = 0x0008,
100         AccumBuffer             = 0x0010,
101         StencilBuffer           = 0x0020,
102         StereoBuffers           = 0x0040,
103         DirectRendering         = 0x0080,
104         HasOverlay              = 0x0100,
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
118     };
119     Q_DECLARE_FLAGS(FormatOptions, FormatOption)
120 }
121
122 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
123
124 class QGLFormatPrivate;
125
126 class Q_OPENGL_EXPORT QGLFormat
127 {
128 public:
129     QGLFormat();
130     QGLFormat(QGL::FormatOptions options, int plane = 0);
131     QGLFormat(const QGLFormat &other);
132     QGLFormat &operator=(const QGLFormat &other);
133     ~QGLFormat();
134
135     void setDepthBufferSize(int size);
136     int  depthBufferSize() const;
137
138     void setAccumBufferSize(int size);
139     int  accumBufferSize() const;
140
141     void setRedBufferSize(int size);
142     int  redBufferSize() const;
143
144     void setGreenBufferSize(int size);
145     int  greenBufferSize() const;
146
147     void setBlueBufferSize(int size);
148     int  blueBufferSize() const;
149
150     void setAlphaBufferSize(int size);
151     int  alphaBufferSize() const;
152
153     void setStencilBufferSize(int size);
154     int  stencilBufferSize() const;
155
156     void setSampleBuffers(bool enable);
157     bool sampleBuffers() const;
158
159     void setSamples(int numSamples);
160     int  samples() const;
161
162     void setSwapInterval(int interval);
163     int  swapInterval() const;
164
165     bool doubleBuffer() const;
166     void setDoubleBuffer(bool enable);
167     bool depth() const;
168     void setDepth(bool enable);
169     bool rgba() const;
170     void setRgba(bool enable);
171     bool alpha() const;
172     void setAlpha(bool enable);
173     bool accum() const;
174     void setAccum(bool enable);
175     bool stencil() const;
176     void setStencil(bool enable);
177     bool stereo() const;
178     void setStereo(bool enable);
179     bool directRendering() const;
180     void setDirectRendering(bool enable);
181     bool hasOverlay() const;
182     void setOverlay(bool enable);
183
184     int plane() const;
185     void setPlane(int plane);
186
187     void setOption(QGL::FormatOptions opt);
188     bool testOption(QGL::FormatOptions opt) const;
189
190     static QGLFormat defaultFormat();
191     static void setDefaultFormat(const QGLFormat& f);
192
193     static QGLFormat defaultOverlayFormat();
194     static void setDefaultOverlayFormat(const QGLFormat& f);
195
196     static bool hasOpenGL();
197     static bool hasOpenGLOverlays();
198
199     void setVersion(int major, int minor);
200     int majorVersion() const;
201     int minorVersion() const;
202
203     enum OpenGLContextProfile {
204         NoProfile,
205         CoreProfile,
206         CompatibilityProfile
207     };
208
209     void setProfile(OpenGLContextProfile profile);
210     OpenGLContextProfile profile() const;
211
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
231     };
232     Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
233
234     static OpenGLVersionFlags openGLVersionFlags();
235
236     static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
237     static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
238 private:
239     QGLFormatPrivate *d;
240
241     void detach();
242
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 &);
247 #endif
248 };
249
250 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
251
252 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
253 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
254
255 #ifndef QT_NO_DEBUG_STREAM
256 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
257 #endif
258
259 class QGLFunctions;
260
261 class Q_OPENGL_EXPORT QGLContext
262 {
263     Q_DECLARE_PRIVATE(QGLContext)
264 public:
265     QGLContext(const QGLFormat& format, QPaintDevice* device);
266     QGLContext(const QGLFormat& format);
267     virtual ~QGLContext();
268
269     virtual bool create(const QGLContext* shareContext = 0);
270     bool isValid() const;
271     bool isSharing() const;
272     void reset();
273
274     static bool areSharing(const QGLContext *context1, const QGLContext *context2);
275
276     QGLFormat format() const;
277     QGLFormat requestedFormat() const;
278     void setFormat(const QGLFormat& format);
279
280     virtual void makeCurrent();
281     virtual void doneCurrent();
282
283     virtual void swapBuffers() const;
284
285     QGLFunctions *functions() const;
286
287     enum BindOption {
288         NoBindOption                            = 0x0000,
289         InvertedYBindOption                     = 0x0001,
290         MipmapBindOption                        = 0x0002,
291         PremultipliedAlphaBindOption            = 0x0004,
292         LinearFilteringBindOption               = 0x0008,
293
294         MemoryManagedBindOption                 = 0x0010, // internal flag
295         CanFlipNativePixmapBindOption           = 0x0020, // internal flag
296         TemporarilyCachedBindOption             = 0x0040, // internal flag
297
298         DefaultBindOption                       = LinearFilteringBindOption
299                                                   | InvertedYBindOption
300                                                   | MipmapBindOption,
301         InternalBindOption                      = MemoryManagedBindOption
302                                                   | PremultipliedAlphaBindOption
303     };
304     Q_DECLARE_FLAGS(BindOptions, BindOption)
305
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);
310
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);
316
317     void deleteTexture(GLuint tx_id);
318
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);
321
322     static void setTextureCacheLimit(int size);
323     static int textureCacheLimit();
324
325     QFunctionPointer getProcAddress(const QString &proc) const;
326     QPaintDevice* device() const;
327     QColor overlayTransparentColor() const;
328
329     static const QGLContext* currentContext();
330
331     static QGLContext *fromOpenGLContext(QOpenGLContext *platformContext);
332     QOpenGLContext *contextHandle() const;
333
334 protected:
335     virtual bool chooseContext(const QGLContext* shareContext = 0);
336
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);
343
344     uint colorIndex(const QColor& c) const;
345     void setValid(bool valid);
346     void setDevice(QPaintDevice *pDev);
347
348 protected:
349     static QGLContext* currentCtx;
350
351 private:
352     QGLContext(QOpenGLContext *windowContext);
353
354     QScopedPointer<QGLContextPrivate> d_ptr;
355
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;
379 private:
380     Q_DISABLE_COPY(QGLContext)
381 };
382
383 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
384
385 class Q_OPENGL_EXPORT QGLWidget : public QWidget
386 {
387     Q_OBJECT
388     Q_DECLARE_PRIVATE(QGLWidget)
389 public:
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);
396     ~QGLWidget();
397
398     void qglColor(const QColor& c) const;
399     void qglClearColor(const QColor& c) const;
400
401     bool isValid() const;
402     bool isSharing() const;
403
404     void makeCurrent();
405     void doneCurrent();
406
407     bool doubleBuffer() const;
408     void swapBuffers();
409
410     QGLFormat format() const;
411     void setFormat(const QGLFormat& format);
412
413     const QGLContext* context() const;
414     void setContext(QGLContext* context, const QGLContext* shareContext = 0,
415                     bool deleteOldContext = true);
416
417     QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
418     QImage grabFrameBuffer(bool withAlpha = false);
419
420     void makeOverlayCurrent();
421     const QGLContext* overlayContext() const;
422
423     static QImage convertToGLFormat(const QImage& img);
424
425     void setMouseTracking(bool enable);
426
427     const QGLColormap & colormap() const;
428     void  setColormap(const QGLColormap & map);
429
430     void renderText(int x, int y, const QString & str,
431                      const QFont & fnt = QFont(), int listBase = 2000);
432     void renderText(double x, double y, double z, const QString & str,
433                      const QFont & fnt = QFont(), int listBase = 2000);
434     QPaintEngine *paintEngine() const;
435
436     GLuint bindTexture(const QImage &image, GLenum target, GLint format,
437                        QGLContext::BindOptions options);
438     GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
439                        QGLContext::BindOptions options);
440
441     GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
442                        GLint format = GL_RGBA);
443     GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
444                        GLint format = GL_RGBA);
445
446     GLuint bindTexture(const QString &fileName);
447
448     void deleteTexture(GLuint tx_id);
449
450     void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
451     void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
452
453 public Q_SLOTS:
454     virtual void updateGL();
455     virtual void updateOverlayGL();
456
457 protected:
458     bool event(QEvent *);
459     virtual void initializeGL();
460     virtual void resizeGL(int w, int h);
461     virtual void paintGL();
462
463     virtual void initializeOverlayGL();
464     virtual void resizeOverlayGL(int w, int h);
465     virtual void paintOverlayGL();
466
467     void setAutoBufferSwap(bool on);
468     bool autoBufferSwap() const;
469
470     void paintEvent(QPaintEvent*);
471     void resizeEvent(QResizeEvent*);
472
473     virtual void glInit();
474     virtual void glDraw();
475     int fontDisplayListBase(const QFont & fnt, int listBase = 2000);
476
477     QGLWidget(QGLWidgetPrivate &dd,
478               const QGLFormat &format = QGLFormat(),
479               QWidget *parent = 0,
480               const QGLWidget* shareWidget = 0,
481               Qt::WindowFlags f = 0);
482 private:
483     Q_DISABLE_COPY(QGLWidget)
484
485     friend class QGLDrawable;
486     friend class QGLPixelBuffer;
487     friend class QGLPixelBufferPrivate;
488     friend class QGLContext;
489     friend class QGLContextPrivate;
490     friend class QGLOverlayWidget;
491     friend class QGLPaintDevice;
492     friend class QGLWidgetGLPaintDevice;
493 };
494
495
496 //
497 // QGLFormat inline functions
498 //
499
500 inline bool QGLFormat::doubleBuffer() const
501 {
502     return testOption(QGL::DoubleBuffer);
503 }
504
505 inline bool QGLFormat::depth() const
506 {
507     return testOption(QGL::DepthBuffer);
508 }
509
510 inline bool QGLFormat::rgba() const
511 {
512     return testOption(QGL::Rgba);
513 }
514
515 inline bool QGLFormat::alpha() const
516 {
517     return testOption(QGL::AlphaChannel);
518 }
519
520 inline bool QGLFormat::accum() const
521 {
522     return testOption(QGL::AccumBuffer);
523 }
524
525 inline bool QGLFormat::stencil() const
526 {
527     return testOption(QGL::StencilBuffer);
528 }
529
530 inline bool QGLFormat::stereo() const
531 {
532     return testOption(QGL::StereoBuffers);
533 }
534
535 inline bool QGLFormat::directRendering() const
536 {
537     return testOption(QGL::DirectRendering);
538 }
539
540 inline bool QGLFormat::hasOverlay() const
541 {
542     return testOption(QGL::HasOverlay);
543 }
544
545 inline bool QGLFormat::sampleBuffers() const
546 {
547     return testOption(QGL::SampleBuffers);
548 }
549
550 QT_END_NAMESPACE
551
552 QT_END_HEADER
553
554 #endif // QT_NO_OPENGL
555 #endif // QGL_H