Merge remote-tracking branch 'gerrit/master' into containers
[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 #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
84
85
86 class QPixmap;
87 class QGLWidgetPrivate;
88 class QGLContextPrivate;
89
90 // Namespace class:
91 namespace QGL
92 {
93     enum FormatOption {
94         DoubleBuffer            = 0x0001,
95         DepthBuffer             = 0x0002,
96         Rgba                    = 0x0004,
97         AlphaChannel            = 0x0008,
98         AccumBuffer             = 0x0010,
99         StencilBuffer           = 0x0020,
100         StereoBuffers           = 0x0040,
101         DirectRendering         = 0x0080,
102         HasOverlay              = 0x0100,
103         SampleBuffers           = 0x0200,
104         DeprecatedFunctions     = 0x0400,
105         SingleBuffer            = DoubleBuffer    << 16,
106         NoDepthBuffer           = DepthBuffer     << 16,
107         ColorIndex              = Rgba            << 16,
108         NoAlphaChannel          = AlphaChannel    << 16,
109         NoAccumBuffer           = AccumBuffer     << 16,
110         NoStencilBuffer         = StencilBuffer   << 16,
111         NoStereoBuffers         = StereoBuffers   << 16,
112         IndirectRendering       = DirectRendering << 16,
113         NoOverlay               = HasOverlay      << 16,
114         NoSampleBuffers         = SampleBuffers   << 16,
115         NoDeprecatedFunctions   = DeprecatedFunctions << 16
116     };
117     Q_DECLARE_FLAGS(FormatOptions, FormatOption)
118 }
119
120 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
121
122 class QGLFormatPrivate;
123
124 class Q_OPENGL_EXPORT QGLFormat
125 {
126 public:
127     QGLFormat();
128     QGLFormat(QGL::FormatOptions options, int plane = 0);
129     QGLFormat(const QGLFormat &other);
130     QGLFormat &operator=(const QGLFormat &other);
131     ~QGLFormat();
132
133     void setDepthBufferSize(int size);
134     int  depthBufferSize() const;
135
136     void setAccumBufferSize(int size);
137     int  accumBufferSize() const;
138
139     void setRedBufferSize(int size);
140     int  redBufferSize() const;
141
142     void setGreenBufferSize(int size);
143     int  greenBufferSize() const;
144
145     void setBlueBufferSize(int size);
146     int  blueBufferSize() const;
147
148     void setAlphaBufferSize(int size);
149     int  alphaBufferSize() const;
150
151     void setStencilBufferSize(int size);
152     int  stencilBufferSize() const;
153
154     void setSampleBuffers(bool enable);
155     bool sampleBuffers() const;
156
157     void setSamples(int numSamples);
158     int  samples() const;
159
160     void setSwapInterval(int interval);
161     int  swapInterval() const;
162
163     bool doubleBuffer() const;
164     void setDoubleBuffer(bool enable);
165     bool depth() const;
166     void setDepth(bool enable);
167     bool rgba() const;
168     void setRgba(bool enable);
169     bool alpha() const;
170     void setAlpha(bool enable);
171     bool accum() const;
172     void setAccum(bool enable);
173     bool stencil() const;
174     void setStencil(bool enable);
175     bool stereo() const;
176     void setStereo(bool enable);
177     bool directRendering() const;
178     void setDirectRendering(bool enable);
179     bool hasOverlay() const;
180     void setOverlay(bool enable);
181
182     int plane() const;
183     void setPlane(int plane);
184
185     void setOption(QGL::FormatOptions opt);
186     bool testOption(QGL::FormatOptions opt) const;
187
188     static QGLFormat defaultFormat();
189     static void setDefaultFormat(const QGLFormat& f);
190
191     static QGLFormat defaultOverlayFormat();
192     static void setDefaultOverlayFormat(const QGLFormat& f);
193
194     static bool hasOpenGL();
195     static bool hasOpenGLOverlays();
196
197     void setVersion(int major, int minor);
198     int majorVersion() const;
199     int minorVersion() const;
200
201     enum OpenGLContextProfile {
202         NoProfile,
203         CoreProfile,
204         CompatibilityProfile
205     };
206
207     void setProfile(OpenGLContextProfile profile);
208     OpenGLContextProfile profile() const;
209
210     enum OpenGLVersionFlag {
211         OpenGL_Version_None               = 0x00000000,
212         OpenGL_Version_1_1                = 0x00000001,
213         OpenGL_Version_1_2                = 0x00000002,
214         OpenGL_Version_1_3                = 0x00000004,
215         OpenGL_Version_1_4                = 0x00000008,
216         OpenGL_Version_1_5                = 0x00000010,
217         OpenGL_Version_2_0                = 0x00000020,
218         OpenGL_Version_2_1                = 0x00000040,
219         OpenGL_ES_Common_Version_1_0      = 0x00000080,
220         OpenGL_ES_CommonLite_Version_1_0  = 0x00000100,
221         OpenGL_ES_Common_Version_1_1      = 0x00000200,
222         OpenGL_ES_CommonLite_Version_1_1  = 0x00000400,
223         OpenGL_ES_Version_2_0             = 0x00000800,
224         OpenGL_Version_3_0                = 0x00001000,
225         OpenGL_Version_3_1                = 0x00002000,
226         OpenGL_Version_3_2                = 0x00004000,
227         OpenGL_Version_3_3                = 0x00008000,
228         OpenGL_Version_4_0                = 0x00010000
229     };
230     Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
231
232     static OpenGLVersionFlags openGLVersionFlags();
233
234     static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
235     static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
236 private:
237     QGLFormatPrivate *d;
238
239     void detach();
240
241     friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
242     friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
243 #ifndef QT_NO_DEBUG_STREAM
244     friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
245 #endif
246 };
247
248 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
249
250 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
251 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
252
253 #ifndef QT_NO_DEBUG_STREAM
254 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
255 #endif
256
257 class QGLFunctions;
258
259 class Q_OPENGL_EXPORT QGLContext
260 {
261     Q_DECLARE_PRIVATE(QGLContext)
262 public:
263     QGLContext(const QGLFormat& format, QPaintDevice* device);
264     QGLContext(const QGLFormat& format);
265     virtual ~QGLContext();
266
267     virtual bool create(const QGLContext* shareContext = 0);
268     bool isValid() const;
269     bool isSharing() const;
270     void reset();
271
272     static bool areSharing(const QGLContext *context1, const QGLContext *context2);
273
274     QGLFormat format() const;
275     QGLFormat requestedFormat() const;
276     void setFormat(const QGLFormat& format);
277
278     // ### Qt 5: return bools + maybe remove virtuals
279     virtual void makeCurrent();
280     virtual void doneCurrent();
281
282     virtual void swapBuffers() const;
283
284     QGLFunctions *functions() const;
285
286     enum BindOption {
287         NoBindOption                            = 0x0000,
288         InvertedYBindOption                     = 0x0001,
289         MipmapBindOption                        = 0x0002,
290         PremultipliedAlphaBindOption            = 0x0004,
291         LinearFilteringBindOption               = 0x0008,
292
293         MemoryManagedBindOption                 = 0x0010, // internal flag
294         CanFlipNativePixmapBindOption           = 0x0020, // internal flag
295         TemporarilyCachedBindOption             = 0x0040, // internal flag
296
297         DefaultBindOption                       = LinearFilteringBindOption
298                                                   | InvertedYBindOption
299                                                   | MipmapBindOption,
300         InternalBindOption                      = MemoryManagedBindOption
301                                                   | PremultipliedAlphaBindOption
302     };
303     Q_DECLARE_FLAGS(BindOptions, BindOption)
304
305     GLuint bindTexture(const QImage &image, GLenum target, GLint format,
306                        BindOptions options);
307     GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
308                        BindOptions options);
309
310     GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
311                        GLint format = GL_RGBA);
312     GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
313                        GLint format = GL_RGBA);
314     GLuint bindTexture(const QString &fileName);
315
316     void deleteTexture(GLuint tx_id);
317
318     void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
319     void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
320
321     static void setTextureCacheLimit(int size);
322     static int textureCacheLimit();
323
324     QFunctionPointer getProcAddress(const QString &proc) const;
325     QPaintDevice* device() const;
326     QColor overlayTransparentColor() const;
327
328     static const QGLContext* currentContext();
329
330     static QGLContext *fromOpenGLContext(QOpenGLContext *platformContext);
331     QOpenGLContext *contextHandle() const;
332
333 protected:
334     virtual bool chooseContext(const QGLContext* shareContext = 0);
335
336     bool deviceIsPixmap() const;
337     bool windowCreated() const;
338     void setWindowCreated(bool on);
339     bool initialized() const;
340     void setInitialized(bool on);
341     void generateFontDisplayLists(const QFont & fnt, int listBase); // ### Qt 5: remove
342
343     uint colorIndex(const QColor& c) const;
344     void setValid(bool valid);
345     void setDevice(QPaintDevice *pDev);
346
347 protected:
348     static QGLContext* currentCtx;
349
350 private:
351     QGLContext(QOpenGLContext *windowContext);
352
353     QScopedPointer<QGLContextPrivate> d_ptr;
354
355     friend class QGLPixelBuffer;
356     friend class QGLPixelBufferPrivate;
357     friend class QGLWidget;
358     friend class QGLWidgetPrivate;
359     friend class QGLGlyphCache;
360     friend class QGL2PaintEngineEx;
361     friend class QGL2PaintEngineExPrivate;
362     friend class QGLEngineShaderManager;
363     friend class QGLTextureGlyphCache;
364     friend struct QGLGlyphTexture;
365     friend class QGLContextGroup;
366     friend class QGLPixmapBlurFilter;
367     friend class QGLExtensions;
368     friend class QGLTexture;
369     friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
370     friend class QGLFramebufferObject;
371     friend class QGLFramebufferObjectPrivate;
372     friend class QGLFBOGLPaintDevice;
373     friend class QGLPaintDevice;
374     friend class QGLWidgetGLPaintDevice;
375     friend class QX11GLSharedContexts;
376     friend class QGLContextResourceBase;
377     friend class QSGDistanceFieldGlyphCache;
378 private:
379     Q_DISABLE_COPY(QGLContext)
380 };
381
382 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
383
384 class Q_OPENGL_EXPORT QGLWidget : public QWidget
385 {
386     Q_OBJECT
387     Q_DECLARE_PRIVATE(QGLWidget)
388 public:
389     explicit QGLWidget(QWidget* parent=0,
390                        const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
391     explicit QGLWidget(QGLContext *context, QWidget* parent=0,
392                        const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
393     explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
394                        const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
395     ~QGLWidget();
396
397     void qglColor(const QColor& c) const;
398     void qglClearColor(const QColor& c) const;
399
400     bool isValid() const;
401     bool isSharing() const;
402
403     // ### Qt 5: return bools
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); // ### Qt 5: remove
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 // QGL_H