Merge remote-tracking branch 'origin/api_changes'
[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 # if !defined(Q_OS_IOS)
63 # include <OpenGL/gl.h>
64 # else
65 #  if defined(QT_OPENGL_ES_2)
66 #   include <OpenGLES/ES2/gl.h>
67 #  endif
68 # endif
69 #elif defined(QT_OPENGL_ES_2)
70 # include <GLES2/gl2.h>
71 # else
72 # include <GL/gl.h>
73 # endif
74
75 #if defined(QT_OPENGL_ES_2)
76 # ifndef GL_DOUBLE
77 #  define GL_DOUBLE GL_FLOAT
78 # endif
79 # ifndef GLdouble
80 typedef GLfloat GLdouble;
81 # endif
82 #endif
83
84 QT_BEGIN_NAMESPACE
85
86
87
88
89 class QPixmap;
90 class QGLWidgetPrivate;
91 class QGLContextPrivate;
92
93 // Namespace class:
94 namespace QGL
95 {
96     enum FormatOption {
97         DoubleBuffer            = 0x0001,
98         DepthBuffer             = 0x0002,
99         Rgba                    = 0x0004,
100         AlphaChannel            = 0x0008,
101         AccumBuffer             = 0x0010,
102         StencilBuffer           = 0x0020,
103         StereoBuffers           = 0x0040,
104         DirectRendering         = 0x0080,
105         HasOverlay              = 0x0100,
106         SampleBuffers           = 0x0200,
107         DeprecatedFunctions     = 0x0400,
108         SingleBuffer            = DoubleBuffer    << 16,
109         NoDepthBuffer           = DepthBuffer     << 16,
110         ColorIndex              = Rgba            << 16,
111         NoAlphaChannel          = AlphaChannel    << 16,
112         NoAccumBuffer           = AccumBuffer     << 16,
113         NoStencilBuffer         = StencilBuffer   << 16,
114         NoStereoBuffers         = StereoBuffers   << 16,
115         IndirectRendering       = DirectRendering << 16,
116         NoOverlay               = HasOverlay      << 16,
117         NoSampleBuffers         = SampleBuffers   << 16,
118         NoDeprecatedFunctions   = DeprecatedFunctions << 16
119     };
120     Q_DECLARE_FLAGS(FormatOptions, FormatOption)
121 }
122
123 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
124
125 class QGLFormatPrivate;
126
127 class Q_OPENGL_EXPORT QGLFormat
128 {
129 public:
130     QGLFormat();
131     QGLFormat(QGL::FormatOptions options, int plane = 0);
132     QGLFormat(const QGLFormat &other);
133     QGLFormat &operator=(const QGLFormat &other);
134     ~QGLFormat();
135
136     void setDepthBufferSize(int size);
137     int  depthBufferSize() const;
138
139     void setAccumBufferSize(int size);
140     int  accumBufferSize() const;
141
142     void setRedBufferSize(int size);
143     int  redBufferSize() const;
144
145     void setGreenBufferSize(int size);
146     int  greenBufferSize() const;
147
148     void setBlueBufferSize(int size);
149     int  blueBufferSize() const;
150
151     void setAlphaBufferSize(int size);
152     int  alphaBufferSize() const;
153
154     void setStencilBufferSize(int size);
155     int  stencilBufferSize() const;
156
157     void setSampleBuffers(bool enable);
158     bool sampleBuffers() const;
159
160     void setSamples(int numSamples);
161     int  samples() const;
162
163     void setSwapInterval(int interval);
164     int  swapInterval() const;
165
166     bool doubleBuffer() const;
167     void setDoubleBuffer(bool enable);
168     bool depth() const;
169     void setDepth(bool enable);
170     bool rgba() const;
171     void setRgba(bool enable);
172     bool alpha() const;
173     void setAlpha(bool enable);
174     bool accum() const;
175     void setAccum(bool enable);
176     bool stencil() const;
177     void setStencil(bool enable);
178     bool stereo() const;
179     void setStereo(bool enable);
180     bool directRendering() const;
181     void setDirectRendering(bool enable);
182     bool hasOverlay() const;
183     void setOverlay(bool enable);
184
185     int plane() const;
186     void setPlane(int plane);
187
188     void setOption(QGL::FormatOptions opt);
189     bool testOption(QGL::FormatOptions opt) const;
190
191     static QGLFormat defaultFormat();
192     static void setDefaultFormat(const QGLFormat& f);
193
194     static QGLFormat defaultOverlayFormat();
195     static void setDefaultOverlayFormat(const QGLFormat& f);
196
197     static bool hasOpenGL();
198     static bool hasOpenGLOverlays();
199
200     void setVersion(int major, int minor);
201     int majorVersion() const;
202     int minorVersion() const;
203
204     enum OpenGLContextProfile {
205         NoProfile,
206         CoreProfile,
207         CompatibilityProfile
208     };
209
210     void setProfile(OpenGLContextProfile profile);
211     OpenGLContextProfile profile() const;
212
213     enum OpenGLVersionFlag {
214         OpenGL_Version_None               = 0x00000000,
215         OpenGL_Version_1_1                = 0x00000001,
216         OpenGL_Version_1_2                = 0x00000002,
217         OpenGL_Version_1_3                = 0x00000004,
218         OpenGL_Version_1_4                = 0x00000008,
219         OpenGL_Version_1_5                = 0x00000010,
220         OpenGL_Version_2_0                = 0x00000020,
221         OpenGL_Version_2_1                = 0x00000040,
222         OpenGL_ES_Common_Version_1_0      = 0x00000080,
223         OpenGL_ES_CommonLite_Version_1_0  = 0x00000100,
224         OpenGL_ES_Common_Version_1_1      = 0x00000200,
225         OpenGL_ES_CommonLite_Version_1_1  = 0x00000400,
226         OpenGL_ES_Version_2_0             = 0x00000800,
227         OpenGL_Version_3_0                = 0x00001000,
228         OpenGL_Version_3_1                = 0x00002000,
229         OpenGL_Version_3_2                = 0x00004000,
230         OpenGL_Version_3_3                = 0x00008000,
231         OpenGL_Version_4_0                = 0x00010000
232     };
233     Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
234
235     static OpenGLVersionFlags openGLVersionFlags();
236
237     static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
238     static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
239 private:
240     QGLFormatPrivate *d;
241
242     void detach();
243
244     friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
245     friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
246 #ifndef QT_NO_DEBUG_STREAM
247     friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
248 #endif
249 };
250
251 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
252
253 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
254 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
255
256 #ifndef QT_NO_DEBUG_STREAM
257 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
258 #endif
259
260 class QGLFunctions;
261
262 class Q_OPENGL_EXPORT QGLContext
263 {
264     Q_DECLARE_PRIVATE(QGLContext)
265 public:
266     QGLContext(const QGLFormat& format, QPaintDevice* device);
267     QGLContext(const QGLFormat& format);
268     virtual ~QGLContext();
269
270     virtual bool create(const QGLContext* shareContext = 0);
271     bool isValid() const;
272     bool isSharing() const;
273     void reset();
274
275     static bool areSharing(const QGLContext *context1, const QGLContext *context2);
276
277     QGLFormat format() const;
278     QGLFormat requestedFormat() const;
279     void setFormat(const QGLFormat& format);
280
281     virtual void makeCurrent();
282     virtual void doneCurrent();
283
284     virtual void swapBuffers() const;
285
286     QGLFunctions *functions() const;
287
288     enum BindOption {
289         NoBindOption                            = 0x0000,
290         InvertedYBindOption                     = 0x0001,
291         MipmapBindOption                        = 0x0002,
292         PremultipliedAlphaBindOption            = 0x0004,
293         LinearFilteringBindOption               = 0x0008,
294
295         MemoryManagedBindOption                 = 0x0010, // internal flag
296         CanFlipNativePixmapBindOption           = 0x0020, // internal flag
297         TemporarilyCachedBindOption             = 0x0040, // internal flag
298
299         DefaultBindOption                       = LinearFilteringBindOption
300                                                   | InvertedYBindOption
301                                                   | MipmapBindOption,
302         InternalBindOption                      = MemoryManagedBindOption
303                                                   | PremultipliedAlphaBindOption
304     };
305     Q_DECLARE_FLAGS(BindOptions, BindOption)
306
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);
311
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);
317
318     void deleteTexture(GLuint tx_id);
319
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);
322
323     static void setTextureCacheLimit(int size);
324     static int textureCacheLimit();
325
326     QFunctionPointer getProcAddress(const QString &proc) const;
327     QPaintDevice* device() const;
328     QColor overlayTransparentColor() const;
329
330     static const QGLContext* currentContext();
331
332     static QGLContext *fromOpenGLContext(QOpenGLContext *platformContext);
333     QOpenGLContext *contextHandle() const;
334
335 protected:
336     virtual bool chooseContext(const QGLContext* shareContext = 0);
337
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);
344
345     uint colorIndex(const QColor& c) const;
346     void setValid(bool valid);
347     void setDevice(QPaintDevice *pDev);
348
349 protected:
350     static QGLContext* currentCtx;
351
352 private:
353     QGLContext(QOpenGLContext *windowContext);
354
355     QScopedPointer<QGLContextPrivate> d_ptr;
356
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;
380 private:
381     Q_DISABLE_COPY(QGLContext)
382 };
383
384 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
385
386 class Q_OPENGL_EXPORT QGLWidget : public QWidget
387 {
388     Q_OBJECT
389     Q_DECLARE_PRIVATE(QGLWidget)
390 public:
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);
397     ~QGLWidget();
398
399     void qglColor(const QColor& c) const;
400     void qglClearColor(const QColor& c) const;
401
402     bool isValid() const;
403     bool isSharing() const;
404
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);
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 // QT_NO_OPENGL
556 #endif // QGL_H