Merge remote branch 'gerrit/master' into HEAD
[profile/ivi/qtbase.git] / src / opengl / qgl.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 #ifdef QT3_SUPPORT
86 #define QGL_VERSION        460
87 #define QGL_VERSION_STR        "4.6"
88 inline QT3_SUPPORT const char *qGLVersion() {
89     return QGL_VERSION_STR;
90 }
91 #endif
92
93 #if defined(Q_WS_WIN) || defined(Q_WS_MAC)
94 class QGLCmap;
95 #endif
96
97 class QPixmap;
98 class QGLWidgetPrivate;
99 class QGLContextPrivate;
100
101 // Namespace class:
102 namespace QGL
103 {
104     enum FormatOption {
105         DoubleBuffer            = 0x0001,
106         DepthBuffer             = 0x0002,
107         Rgba                    = 0x0004,
108         AlphaChannel            = 0x0008,
109         AccumBuffer             = 0x0010,
110         StencilBuffer           = 0x0020,
111         StereoBuffers           = 0x0040,
112         DirectRendering         = 0x0080,
113         HasOverlay              = 0x0100,
114         SampleBuffers           = 0x0200,
115         DeprecatedFunctions     = 0x0400,
116         SingleBuffer            = DoubleBuffer    << 16,
117         NoDepthBuffer           = DepthBuffer     << 16,
118         ColorIndex              = Rgba            << 16,
119         NoAlphaChannel          = AlphaChannel    << 16,
120         NoAccumBuffer           = AccumBuffer     << 16,
121         NoStencilBuffer         = StencilBuffer   << 16,
122         NoStereoBuffers         = StereoBuffers   << 16,
123         IndirectRendering       = DirectRendering << 16,
124         NoOverlay               = HasOverlay      << 16,
125         NoSampleBuffers         = SampleBuffers   << 16,
126         NoDeprecatedFunctions   = DeprecatedFunctions << 16
127     };
128     Q_DECLARE_FLAGS(FormatOptions, FormatOption)
129 }
130
131 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
132
133 class QGLFormatPrivate;
134
135 class Q_OPENGL_EXPORT QGLFormat
136 {
137 public:
138     QGLFormat();
139     QGLFormat(QGL::FormatOptions options, int plane = 0);
140     QGLFormat(const QGLFormat &other);
141     QGLFormat &operator=(const QGLFormat &other);
142     ~QGLFormat();
143
144     void setDepthBufferSize(int size);
145     int  depthBufferSize() const;
146
147     void setAccumBufferSize(int size);
148     int  accumBufferSize() const;
149
150     void setRedBufferSize(int size);
151     int  redBufferSize() const;
152
153     void setGreenBufferSize(int size);
154     int  greenBufferSize() const;
155
156     void setBlueBufferSize(int size);
157     int  blueBufferSize() const;
158
159     void setAlphaBufferSize(int size);
160     int  alphaBufferSize() const;
161
162     void setStencilBufferSize(int size);
163     int  stencilBufferSize() const;
164
165     void setSampleBuffers(bool enable);
166     bool sampleBuffers() const;
167
168     void setSamples(int numSamples);
169     int  samples() const;
170
171     void setSwapInterval(int interval);
172     int  swapInterval() const;
173
174     bool doubleBuffer() const;
175     void setDoubleBuffer(bool enable);
176     bool depth() const;
177     void setDepth(bool enable);
178     bool rgba() const;
179     void setRgba(bool enable);
180     bool alpha() const;
181     void setAlpha(bool enable);
182     bool accum() const;
183     void setAccum(bool enable);
184     bool stencil() const;
185     void setStencil(bool enable);
186     bool stereo() const;
187     void setStereo(bool enable);
188     bool directRendering() const;
189     void setDirectRendering(bool enable);
190     bool hasOverlay() const;
191     void setOverlay(bool enable);
192
193     int plane() const;
194     void setPlane(int plane);
195
196     void setOption(QGL::FormatOptions opt);
197     bool testOption(QGL::FormatOptions opt) const;
198
199     static QGLFormat defaultFormat();
200     static void setDefaultFormat(const QGLFormat& f);
201
202     static QGLFormat defaultOverlayFormat();
203     static void setDefaultOverlayFormat(const QGLFormat& f);
204
205     static bool hasOpenGL();
206     static bool hasOpenGLOverlays();
207
208     void setVersion(int major, int minor);
209     int majorVersion() const;
210     int minorVersion() const;
211
212     enum OpenGLContextProfile {
213         NoProfile,
214         CoreProfile,
215         CompatibilityProfile
216     };
217
218     void setProfile(OpenGLContextProfile profile);
219     OpenGLContextProfile profile() const;
220
221     enum OpenGLVersionFlag {
222         OpenGL_Version_None               = 0x00000000,
223         OpenGL_Version_1_1                = 0x00000001,
224         OpenGL_Version_1_2                = 0x00000002,
225         OpenGL_Version_1_3                = 0x00000004,
226         OpenGL_Version_1_4                = 0x00000008,
227         OpenGL_Version_1_5                = 0x00000010,
228         OpenGL_Version_2_0                = 0x00000020,
229         OpenGL_Version_2_1                = 0x00000040,
230         OpenGL_ES_Common_Version_1_0      = 0x00000080,
231         OpenGL_ES_CommonLite_Version_1_0  = 0x00000100,
232         OpenGL_ES_Common_Version_1_1      = 0x00000200,
233         OpenGL_ES_CommonLite_Version_1_1  = 0x00000400,
234         OpenGL_ES_Version_2_0             = 0x00000800,
235         OpenGL_Version_3_0                = 0x00001000,
236         OpenGL_Version_3_1                = 0x00002000,
237         OpenGL_Version_3_2                = 0x00004000,
238         OpenGL_Version_3_3                = 0x00008000,
239         OpenGL_Version_4_0                = 0x00010000
240     };
241     Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
242
243     static OpenGLVersionFlags openGLVersionFlags();
244
245     static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
246     static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
247 private:
248     QGLFormatPrivate *d;
249
250     void detach();
251
252     friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
253     friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
254 #ifndef QT_NO_DEBUG_STREAM
255     friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
256 #endif
257 };
258
259 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
260
261 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
262 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
263
264 #ifndef QT_NO_DEBUG_STREAM
265 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
266 #endif
267
268 class QGLFunctions;
269
270 class Q_OPENGL_EXPORT QGLContext
271 {
272     Q_DECLARE_PRIVATE(QGLContext)
273 public:
274     QGLContext(const QGLFormat& format, QPaintDevice* device);
275     QGLContext(const QGLFormat& format);
276     virtual ~QGLContext();
277
278     virtual bool create(const QGLContext* shareContext = 0);
279     bool isValid() const;
280     bool isSharing() const;
281     void reset();
282
283     static bool areSharing(const QGLContext *context1, const QGLContext *context2);
284
285     QGLFormat format() const;
286     QGLFormat requestedFormat() const;
287     void setFormat(const QGLFormat& format);
288
289     // ### Qt 5: return bools + maybe remove virtuals
290     virtual void makeCurrent();
291     virtual void doneCurrent();
292
293     virtual void swapBuffers() const;
294
295     QGLFunctions *functions() const;
296
297     enum BindOption {
298         NoBindOption                            = 0x0000,
299         InvertedYBindOption                     = 0x0001,
300         MipmapBindOption                        = 0x0002,
301         PremultipliedAlphaBindOption            = 0x0004,
302         LinearFilteringBindOption               = 0x0008,
303
304         MemoryManagedBindOption                 = 0x0010, // internal flag
305         CanFlipNativePixmapBindOption           = 0x0020, // internal flag
306         TemporarilyCachedBindOption             = 0x0040, // internal flag
307
308         DefaultBindOption                       = LinearFilteringBindOption
309                                                   | InvertedYBindOption
310                                                   | MipmapBindOption,
311         InternalBindOption                      = MemoryManagedBindOption
312                                                   | PremultipliedAlphaBindOption
313     };
314     Q_DECLARE_FLAGS(BindOptions, BindOption)
315
316     GLuint bindTexture(const QImage &image, GLenum target, GLint format,
317                        BindOptions options);
318     GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
319                        BindOptions options);
320
321     GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
322                        GLint format = GL_RGBA);
323     GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
324                        GLint format = GL_RGBA);
325     GLuint bindTexture(const QString &fileName);
326
327     void deleteTexture(GLuint tx_id);
328
329     void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
330     void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
331
332     static void setTextureCacheLimit(int size);
333     static int textureCacheLimit();
334
335     void *getProcAddress(const QString &proc) const;
336     QPaintDevice* device() const;
337     QColor overlayTransparentColor() const;
338
339     static const QGLContext* currentContext();
340
341     static QGLContext *fromOpenGLContext(QOpenGLContext *platformContext);
342     QOpenGLContext *contextHandle() const;
343
344 protected:
345     virtual bool chooseContext(const QGLContext* shareContext = 0);
346
347     bool deviceIsPixmap() const;
348     bool windowCreated() const;
349     void setWindowCreated(bool on);
350     bool initialized() const;
351     void setInitialized(bool on);
352     void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
353
354     uint colorIndex(const QColor& c) const;
355     void setValid(bool valid);
356     void setDevice(QPaintDevice *pDev);
357
358 protected:
359     static QGLContext* currentCtx;
360
361 private:
362     QGLContext(QOpenGLContext *windowContext);
363
364     QScopedPointer<QGLContextPrivate> d_ptr;
365
366     friend class QGLPixelBuffer;
367     friend class QGLPixelBufferPrivate;
368     friend class QGLWidget;
369     friend class QGLWidgetPrivate;
370     friend class QGLGlyphCache;
371     friend class QGL2PaintEngineEx;
372     friend class QGL2PaintEngineExPrivate;
373     friend class QGLEngineShaderManager;
374     friend class QGLTextureGlyphCache;
375     friend struct QGLGlyphTexture;
376     friend class QGLContextGroup;
377     friend class QGLPixmapBlurFilter;
378     friend class QGLExtensions;
379     friend class QGLTexture;
380     friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
381 #ifdef Q_WS_MAC
382 public:
383     void updatePaintDevice();
384 private:
385     friend class QMacGLWindowChangeEvent;
386     friend QGLContextPrivate *qt_phonon_get_dptr(const QGLContext *);
387 #endif
388     friend class QGLFramebufferObject;
389     friend class QGLFramebufferObjectPrivate;
390     friend class QGLFBOGLPaintDevice;
391     friend class QGLPaintDevice;
392     friend class QGLWidgetGLPaintDevice;
393     friend class QX11GLSharedContexts;
394     friend class QGLContextResourceBase;
395     friend class QSGDistanceFieldGlyphCache;
396 private:
397     Q_DISABLE_COPY(QGLContext)
398 };
399
400 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
401
402 class Q_OPENGL_EXPORT QGLWidget : public QWidget
403 {
404     Q_OBJECT
405     Q_DECLARE_PRIVATE(QGLWidget)
406 public:
407     explicit QGLWidget(QWidget* parent=0,
408                        const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
409     explicit QGLWidget(QGLContext *context, QWidget* parent=0,
410                        const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
411     explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
412                        const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
413 #ifdef QT3_SUPPORT
414     QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
415                                     const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
416     QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
417                                     const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
418     QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
419                                     const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
420 #endif
421     ~QGLWidget();
422
423     void qglColor(const QColor& c) const;
424     void qglClearColor(const QColor& c) const;
425
426     bool isValid() const;
427     bool isSharing() const;
428
429     // ### Qt 5: return bools
430     void makeCurrent();
431     void doneCurrent();
432
433     bool doubleBuffer() const;
434     void swapBuffers();
435
436     QGLFormat format() const;
437     void setFormat(const QGLFormat& format);
438
439     const QGLContext* context() const;
440     void setContext(QGLContext* context, const QGLContext* shareContext = 0,
441                     bool deleteOldContext = true);
442
443     QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
444     QImage grabFrameBuffer(bool withAlpha = false);
445
446     void makeOverlayCurrent();
447     const QGLContext* overlayContext() const;
448
449     static QImage convertToGLFormat(const QImage& img);
450
451     void setMouseTracking(bool enable);
452
453     const QGLColormap & colormap() const;
454     void  setColormap(const QGLColormap & map);
455
456     void renderText(int x, int y, const QString & str,
457                      const QFont & fnt = QFont(), int listBase = 2000);
458     void renderText(double x, double y, double z, const QString & str,
459                      const QFont & fnt = QFont(), int listBase = 2000);
460     QPaintEngine *paintEngine() const;
461
462     GLuint bindTexture(const QImage &image, GLenum target, GLint format,
463                        QGLContext::BindOptions options);
464     GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
465                        QGLContext::BindOptions options);
466
467     GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
468                        GLint format = GL_RGBA);
469     GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
470                        GLint format = GL_RGBA);
471
472     GLuint bindTexture(const QString &fileName);
473
474     void deleteTexture(GLuint tx_id);
475
476     void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
477     void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
478
479 public Q_SLOTS:
480     virtual void updateGL();
481     virtual void updateOverlayGL();
482
483 protected:
484     bool event(QEvent *);
485     virtual void initializeGL();
486     virtual void resizeGL(int w, int h);
487     virtual void paintGL();
488
489     virtual void initializeOverlayGL();
490     virtual void resizeOverlayGL(int w, int h);
491     virtual void paintOverlayGL();
492
493     void setAutoBufferSwap(bool on);
494     bool autoBufferSwap() const;
495
496     void paintEvent(QPaintEvent*);
497     void resizeEvent(QResizeEvent*);
498
499     virtual void glInit();
500     virtual void glDraw();
501     int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
502
503     QGLWidget(QGLWidgetPrivate &dd,
504               const QGLFormat &format = QGLFormat(),
505               QWidget *parent = 0,
506               const QGLWidget* shareWidget = 0,
507               Qt::WindowFlags f = 0);
508 private:
509     Q_DISABLE_COPY(QGLWidget)
510
511 #ifdef Q_WS_MAC
512     friend class QMacGLWindowChangeEvent;
513 #endif
514     friend class QGLDrawable;
515     friend class QGLPixelBuffer;
516     friend class QGLPixelBufferPrivate;
517     friend class QGLContext;
518     friend class QGLContextPrivate;
519     friend class QGLOverlayWidget;
520     friend class QGLPaintDevice;
521     friend class QGLWidgetGLPaintDevice;
522 };
523
524
525 //
526 // QGLFormat inline functions
527 //
528
529 inline bool QGLFormat::doubleBuffer() const
530 {
531     return testOption(QGL::DoubleBuffer);
532 }
533
534 inline bool QGLFormat::depth() const
535 {
536     return testOption(QGL::DepthBuffer);
537 }
538
539 inline bool QGLFormat::rgba() const
540 {
541     return testOption(QGL::Rgba);
542 }
543
544 inline bool QGLFormat::alpha() const
545 {
546     return testOption(QGL::AlphaChannel);
547 }
548
549 inline bool QGLFormat::accum() const
550 {
551     return testOption(QGL::AccumBuffer);
552 }
553
554 inline bool QGLFormat::stencil() const
555 {
556     return testOption(QGL::StencilBuffer);
557 }
558
559 inline bool QGLFormat::stereo() const
560 {
561     return testOption(QGL::StereoBuffers);
562 }
563
564 inline bool QGLFormat::directRendering() const
565 {
566     return testOption(QGL::DirectRendering);
567 }
568
569 inline bool QGLFormat::hasOverlay() const
570 {
571     return testOption(QGL::HasOverlay);
572 }
573
574 inline bool QGLFormat::sampleBuffers() const
575 {
576     return testOption(QGL::SampleBuffers);
577 }
578
579 QT_END_NAMESPACE
580
581 QT_END_HEADER
582
583 #endif // QGL_H