Update copyright year in license headers.
[profile/ivi/qtbase.git] / src / opengl / qgl.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QGL_H
43 #define QGL_H
44
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>
50
51 #include <QtGui/QSurfaceFormat>
52
53 QT_BEGIN_HEADER
54
55 #if defined(Q_OS_WIN)
56 # include <QtCore/qt_windows.h>
57 #endif
58
59 #if defined(Q_OS_MAC)
60 # include <OpenGL/gl.h>
61 #elif defined(QT_OPENGL_ES_2)
62 # if defined(Q_OS_MAC)
63 #  include <OpenGLES/ES2/gl.h>
64 # else
65 #  include <GLES2/gl2.h>
66 # endif
67 # ifndef GL_DOUBLE
68 #  define GL_DOUBLE GL_FLOAT
69 # endif
70 # ifndef GLdouble
71 typedef GLfloat GLdouble;
72 # endif
73 #else
74 # if defined(Q_OS_MAC)
75 #  include <OpenGL/gl.h>
76 # else
77 #  include <GL/gl.h>
78 # endif
79 #endif
80
81 QT_BEGIN_NAMESPACE
82
83 QT_MODULE(OpenGL)
84
85
86
87 class QPixmap;
88 class QGLWidgetPrivate;
89 class QGLContextPrivate;
90
91 // Namespace class:
92 namespace QGL
93 {
94     enum FormatOption {
95         DoubleBuffer            = 0x0001,
96         DepthBuffer             = 0x0002,
97         Rgba                    = 0x0004,
98         AlphaChannel            = 0x0008,
99         AccumBuffer             = 0x0010,
100         StencilBuffer           = 0x0020,
101         StereoBuffers           = 0x0040,
102         DirectRendering         = 0x0080,
103         HasOverlay              = 0x0100,
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
117     };
118     Q_DECLARE_FLAGS(FormatOptions, FormatOption)
119 }
120
121 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
122
123 class QGLFormatPrivate;
124
125 class Q_OPENGL_EXPORT QGLFormat
126 {
127 public:
128     QGLFormat();
129     QGLFormat(QGL::FormatOptions options, int plane = 0);
130     QGLFormat(const QGLFormat &other);
131     QGLFormat &operator=(const QGLFormat &other);
132     ~QGLFormat();
133
134     void setDepthBufferSize(int size);
135     int  depthBufferSize() const;
136
137     void setAccumBufferSize(int size);
138     int  accumBufferSize() const;
139
140     void setRedBufferSize(int size);
141     int  redBufferSize() const;
142
143     void setGreenBufferSize(int size);
144     int  greenBufferSize() const;
145
146     void setBlueBufferSize(int size);
147     int  blueBufferSize() const;
148
149     void setAlphaBufferSize(int size);
150     int  alphaBufferSize() const;
151
152     void setStencilBufferSize(int size);
153     int  stencilBufferSize() const;
154
155     void setSampleBuffers(bool enable);
156     bool sampleBuffers() const;
157
158     void setSamples(int numSamples);
159     int  samples() const;
160
161     void setSwapInterval(int interval);
162     int  swapInterval() const;
163
164     bool doubleBuffer() const;
165     void setDoubleBuffer(bool enable);
166     bool depth() const;
167     void setDepth(bool enable);
168     bool rgba() const;
169     void setRgba(bool enable);
170     bool alpha() const;
171     void setAlpha(bool enable);
172     bool accum() const;
173     void setAccum(bool enable);
174     bool stencil() const;
175     void setStencil(bool enable);
176     bool stereo() const;
177     void setStereo(bool enable);
178     bool directRendering() const;
179     void setDirectRendering(bool enable);
180     bool hasOverlay() const;
181     void setOverlay(bool enable);
182
183     int plane() const;
184     void setPlane(int plane);
185
186     void setOption(QGL::FormatOptions opt);
187     bool testOption(QGL::FormatOptions opt) const;
188
189     static QGLFormat defaultFormat();
190     static void setDefaultFormat(const QGLFormat& f);
191
192     static QGLFormat defaultOverlayFormat();
193     static void setDefaultOverlayFormat(const QGLFormat& f);
194
195     static bool hasOpenGL();
196     static bool hasOpenGLOverlays();
197
198     void setVersion(int major, int minor);
199     int majorVersion() const;
200     int minorVersion() const;
201
202     enum OpenGLContextProfile {
203         NoProfile,
204         CoreProfile,
205         CompatibilityProfile
206     };
207
208     void setProfile(OpenGLContextProfile profile);
209     OpenGLContextProfile profile() const;
210
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
230     };
231     Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
232
233     static OpenGLVersionFlags openGLVersionFlags();
234
235     static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
236     static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
237 private:
238     QGLFormatPrivate *d;
239
240     void detach();
241
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 &);
246 #endif
247 };
248
249 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
250
251 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
252 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
253
254 #ifndef QT_NO_DEBUG_STREAM
255 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
256 #endif
257
258 class QGLFunctions;
259
260 class Q_OPENGL_EXPORT QGLContext
261 {
262     Q_DECLARE_PRIVATE(QGLContext)
263 public:
264     QGLContext(const QGLFormat& format, QPaintDevice* device);
265     QGLContext(const QGLFormat& format);
266     virtual ~QGLContext();
267
268     virtual bool create(const QGLContext* shareContext = 0);
269     bool isValid() const;
270     bool isSharing() const;
271     void reset();
272
273     static bool areSharing(const QGLContext *context1, const QGLContext *context2);
274
275     QGLFormat format() const;
276     QGLFormat requestedFormat() const;
277     void setFormat(const QGLFormat& format);
278
279     // ### Qt 5: return bools + maybe remove virtuals
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     void *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); // ### Qt 5: remove
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     // ### Qt 5: return bools
405     void makeCurrent();
406     void doneCurrent();
407
408     bool doubleBuffer() const;
409     void swapBuffers();
410
411     QGLFormat format() const;
412     void setFormat(const QGLFormat& format);
413
414     const QGLContext* context() const;
415     void setContext(QGLContext* context, const QGLContext* shareContext = 0,
416                     bool deleteOldContext = true);
417
418     QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
419     QImage grabFrameBuffer(bool withAlpha = false);
420
421     void makeOverlayCurrent();
422     const QGLContext* overlayContext() const;
423
424     static QImage convertToGLFormat(const QImage& img);
425
426     void setMouseTracking(bool enable);
427
428     const QGLColormap & colormap() const;
429     void  setColormap(const QGLColormap & map);
430
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;
436
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);
441
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);
446
447     GLuint bindTexture(const QString &fileName);
448
449     void deleteTexture(GLuint tx_id);
450
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);
453
454 public Q_SLOTS:
455     virtual void updateGL();
456     virtual void updateOverlayGL();
457
458 protected:
459     bool event(QEvent *);
460     virtual void initializeGL();
461     virtual void resizeGL(int w, int h);
462     virtual void paintGL();
463
464     virtual void initializeOverlayGL();
465     virtual void resizeOverlayGL(int w, int h);
466     virtual void paintOverlayGL();
467
468     void setAutoBufferSwap(bool on);
469     bool autoBufferSwap() const;
470
471     void paintEvent(QPaintEvent*);
472     void resizeEvent(QResizeEvent*);
473
474     virtual void glInit();
475     virtual void glDraw();
476     int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
477
478     QGLWidget(QGLWidgetPrivate &dd,
479               const QGLFormat &format = QGLFormat(),
480               QWidget *parent = 0,
481               const QGLWidget* shareWidget = 0,
482               Qt::WindowFlags f = 0);
483 private:
484     Q_DISABLE_COPY(QGLWidget)
485
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;
494 };
495
496
497 //
498 // QGLFormat inline functions
499 //
500
501 inline bool QGLFormat::doubleBuffer() const
502 {
503     return testOption(QGL::DoubleBuffer);
504 }
505
506 inline bool QGLFormat::depth() const
507 {
508     return testOption(QGL::DepthBuffer);
509 }
510
511 inline bool QGLFormat::rgba() const
512 {
513     return testOption(QGL::Rgba);
514 }
515
516 inline bool QGLFormat::alpha() const
517 {
518     return testOption(QGL::AlphaChannel);
519 }
520
521 inline bool QGLFormat::accum() const
522 {
523     return testOption(QGL::AccumBuffer);
524 }
525
526 inline bool QGLFormat::stencil() const
527 {
528     return testOption(QGL::StencilBuffer);
529 }
530
531 inline bool QGLFormat::stereo() const
532 {
533     return testOption(QGL::StereoBuffers);
534 }
535
536 inline bool QGLFormat::directRendering() const
537 {
538     return testOption(QGL::DirectRendering);
539 }
540
541 inline bool QGLFormat::hasOverlay() const
542 {
543     return testOption(QGL::HasOverlay);
544 }
545
546 inline bool QGLFormat::sampleBuffers() const
547 {
548     return testOption(QGL::SampleBuffers);
549 }
550
551 QT_END_NAMESPACE
552
553 QT_END_HEADER
554
555 #endif // QGL_H