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