Build fix after setPreferredPaintEngine was removed
[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 *fromGuiGLContext(QGuiGLContext *platformContext);
342     QGuiGLContext *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(QGuiGLContext *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 QGLSharedResourceGuard;
378     friend class QGLPixmapBlurFilter;
379     friend class QGLExtensions;
380     friend class QGLTexture;
381     friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
382 #ifdef Q_WS_MAC
383 public:
384     void updatePaintDevice();
385 private:
386     friend class QMacGLWindowChangeEvent;
387     friend QGLContextPrivate *qt_phonon_get_dptr(const QGLContext *);
388 #endif
389     friend class QGLFramebufferObject;
390     friend class QGLFramebufferObjectPrivate;
391     friend class QGLFBOGLPaintDevice;
392     friend class QGLPaintDevice;
393     friend class QGLWidgetGLPaintDevice;
394     friend class QX11GLSharedContexts;
395     friend class QGLContextResourceBase;
396     friend class QSGDistanceFieldGlyphCache;
397 private:
398     Q_DISABLE_COPY(QGLContext)
399 };
400
401 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
402
403 class Q_OPENGL_EXPORT QGLWidget : public QWidget
404 {
405     Q_OBJECT
406     Q_DECLARE_PRIVATE(QGLWidget)
407 public:
408     explicit QGLWidget(QWidget* parent=0,
409                        const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
410     explicit QGLWidget(QGLContext *context, QWidget* parent=0,
411                        const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
412     explicit QGLWidget(const QGLFormat& format, QWidget* parent=0,
413                        const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
414 #ifdef QT3_SUPPORT
415     QT3_SUPPORT_CONSTRUCTOR QGLWidget(QWidget* parent, const char* name,
416                                     const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
417     QT3_SUPPORT_CONSTRUCTOR QGLWidget(QGLContext *context, QWidget* parent, const char* name,
418                                     const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
419     QT3_SUPPORT_CONSTRUCTOR QGLWidget(const QGLFormat& format, QWidget* parent, const char* name,
420                                     const QGLWidget* shareWidget = 0, Qt::WindowFlags f=0);
421 #endif
422     ~QGLWidget();
423
424     void qglColor(const QColor& c) const;
425     void qglClearColor(const QColor& c) const;
426
427     bool isValid() const;
428     bool isSharing() const;
429
430     // ### Qt 5: return bools
431     void makeCurrent();
432     void doneCurrent();
433
434     bool doubleBuffer() const;
435     void swapBuffers();
436
437     QGLFormat format() const;
438     void setFormat(const QGLFormat& format);
439
440     const QGLContext* context() const;
441     void setContext(QGLContext* context, const QGLContext* shareContext = 0,
442                     bool deleteOldContext = true);
443
444     QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
445     QImage grabFrameBuffer(bool withAlpha = false);
446
447     void makeOverlayCurrent();
448     const QGLContext* overlayContext() const;
449
450     static QImage convertToGLFormat(const QImage& img);
451
452     void setMouseTracking(bool enable);
453
454     const QGLColormap & colormap() const;
455     void  setColormap(const QGLColormap & map);
456
457     void renderText(int x, int y, const QString & str,
458                      const QFont & fnt = QFont(), int listBase = 2000);
459     void renderText(double x, double y, double z, const QString & str,
460                      const QFont & fnt = QFont(), int listBase = 2000);
461     QPaintEngine *paintEngine() const;
462
463     GLuint bindTexture(const QImage &image, GLenum target, GLint format,
464                        QGLContext::BindOptions options);
465     GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
466                        QGLContext::BindOptions options);
467
468     GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
469                        GLint format = GL_RGBA);
470     GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
471                        GLint format = GL_RGBA);
472
473     GLuint bindTexture(const QString &fileName);
474
475     void deleteTexture(GLuint tx_id);
476
477     void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
478     void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
479
480 public Q_SLOTS:
481     virtual void updateGL();
482     virtual void updateOverlayGL();
483
484 protected:
485     bool event(QEvent *);
486     virtual void initializeGL();
487     virtual void resizeGL(int w, int h);
488     virtual void paintGL();
489
490     virtual void initializeOverlayGL();
491     virtual void resizeOverlayGL(int w, int h);
492     virtual void paintOverlayGL();
493
494     void setAutoBufferSwap(bool on);
495     bool autoBufferSwap() const;
496
497     void paintEvent(QPaintEvent*);
498     void resizeEvent(QResizeEvent*);
499
500     virtual void glInit();
501     virtual void glDraw();
502     int fontDisplayListBase(const QFont & fnt, int listBase = 2000); // ### Qt 5: remove
503
504     QGLWidget(QGLWidgetPrivate &dd,
505               const QGLFormat &format = QGLFormat(),
506               QWidget *parent = 0,
507               const QGLWidget* shareWidget = 0,
508               Qt::WindowFlags f = 0);
509 private:
510     Q_DISABLE_COPY(QGLWidget)
511
512 #ifdef Q_WS_MAC
513     friend class QMacGLWindowChangeEvent;
514 #endif
515     friend class QGLDrawable;
516     friend class QGLPixelBuffer;
517     friend class QGLPixelBufferPrivate;
518     friend class QGLContext;
519     friend class QGLContextPrivate;
520     friend class QGLOverlayWidget;
521     friend class QGLPaintDevice;
522     friend class QGLWidgetGLPaintDevice;
523 };
524
525
526 //
527 // QGLFormat inline functions
528 //
529
530 inline bool QGLFormat::doubleBuffer() const
531 {
532     return testOption(QGL::DoubleBuffer);
533 }
534
535 inline bool QGLFormat::depth() const
536 {
537     return testOption(QGL::DepthBuffer);
538 }
539
540 inline bool QGLFormat::rgba() const
541 {
542     return testOption(QGL::Rgba);
543 }
544
545 inline bool QGLFormat::alpha() const
546 {
547     return testOption(QGL::AlphaChannel);
548 }
549
550 inline bool QGLFormat::accum() const
551 {
552     return testOption(QGL::AccumBuffer);
553 }
554
555 inline bool QGLFormat::stencil() const
556 {
557     return testOption(QGL::StencilBuffer);
558 }
559
560 inline bool QGLFormat::stereo() const
561 {
562     return testOption(QGL::StereoBuffers);
563 }
564
565 inline bool QGLFormat::directRendering() const
566 {
567     return testOption(QGL::DirectRendering);
568 }
569
570 inline bool QGLFormat::hasOverlay() const
571 {
572     return testOption(QGL::HasOverlay);
573 }
574
575 inline bool QGLFormat::sampleBuffers() const
576 {
577     return testOption(QGL::SampleBuffers);
578 }
579
580 QT_END_NAMESPACE
581
582 QT_END_HEADER
583
584 #endif // QGL_H