1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtOpenGL module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QGL_H
41 #define QGL_H
42 
43 #ifndef QT_NO_OPENGL
44 
45 #include <QtGui/qopengl.h>
46 #include <QtWidgets/qwidget.h>
47 #include <QtGui/qpaintengine.h>
48 #include <QtOpenGL/qglcolormap.h>
49 #include <QtCore/qmap.h>
50 #include <QtCore/qscopedpointer.h>
51 
52 #include <QtGui/QSurfaceFormat>
53 
54 QT_BEGIN_NAMESPACE
55 
56 
57 
58 
59 class QPixmap;
60 class QGLWidgetPrivate;
61 class QGLContextPrivate;
62 
63 // Namespace class:
64 namespace QGL
65 {
66     enum FormatOption {
67         DoubleBuffer            = 0x0001,
68         DepthBuffer             = 0x0002,
69         Rgba                    = 0x0004,
70         AlphaChannel            = 0x0008,
71         AccumBuffer             = 0x0010,
72         StencilBuffer           = 0x0020,
73         StereoBuffers           = 0x0040,
74         DirectRendering         = 0x0080,
75         HasOverlay              = 0x0100,
76         SampleBuffers           = 0x0200,
77         DeprecatedFunctions     = 0x0400,
78         SingleBuffer            = DoubleBuffer    << 16,
79         NoDepthBuffer           = DepthBuffer     << 16,
80         ColorIndex              = Rgba            << 16,
81         NoAlphaChannel          = AlphaChannel    << 16,
82         NoAccumBuffer           = AccumBuffer     << 16,
83         NoStencilBuffer         = StencilBuffer   << 16,
84         NoStereoBuffers         = StereoBuffers   << 16,
85         IndirectRendering       = DirectRendering << 16,
86         NoOverlay               = HasOverlay      << 16,
87         NoSampleBuffers         = SampleBuffers   << 16,
88         NoDeprecatedFunctions   = DeprecatedFunctions << 16
89     };
90     Q_DECLARE_FLAGS(FormatOptions, FormatOption)
91 }
92 
93 Q_DECLARE_OPERATORS_FOR_FLAGS(QGL::FormatOptions)
94 
95 class QGLFormatPrivate;
96 
97 class Q_OPENGL_EXPORT QGLFormat
98 {
99 public:
100     QGLFormat();
101     QGLFormat(QGL::FormatOptions options, int plane = 0);
102     QGLFormat(const QGLFormat &other);
103     QGLFormat &operator=(const QGLFormat &other);
104     ~QGLFormat();
105 
106     void setDepthBufferSize(int size);
107     int  depthBufferSize() const;
108 
109     void setAccumBufferSize(int size);
110     int  accumBufferSize() const;
111 
112     void setRedBufferSize(int size);
113     int  redBufferSize() const;
114 
115     void setGreenBufferSize(int size);
116     int  greenBufferSize() const;
117 
118     void setBlueBufferSize(int size);
119     int  blueBufferSize() const;
120 
121     void setAlphaBufferSize(int size);
122     int  alphaBufferSize() const;
123 
124     void setStencilBufferSize(int size);
125     int  stencilBufferSize() const;
126 
127     void setSampleBuffers(bool enable);
128     bool sampleBuffers() const;
129 
130     void setSamples(int numSamples);
131     int  samples() const;
132 
133     void setSwapInterval(int interval);
134     int  swapInterval() const;
135 
136     bool doubleBuffer() const;
137     void setDoubleBuffer(bool enable);
138     bool depth() const;
139     void setDepth(bool enable);
140     bool rgba() const;
141     void setRgba(bool enable);
142     bool alpha() const;
143     void setAlpha(bool enable);
144     bool accum() const;
145     void setAccum(bool enable);
146     bool stencil() const;
147     void setStencil(bool enable);
148     bool stereo() const;
149     void setStereo(bool enable);
150     bool directRendering() const;
151     void setDirectRendering(bool enable);
152     bool hasOverlay() const;
153     void setOverlay(bool enable);
154 
155     int plane() const;
156     void setPlane(int plane);
157 
158     void setOption(QGL::FormatOptions opt);
159     bool testOption(QGL::FormatOptions opt) const;
160 
161     static QGLFormat defaultFormat();
162     static void setDefaultFormat(const QGLFormat& f);
163 
164     static QGLFormat defaultOverlayFormat();
165     static void setDefaultOverlayFormat(const QGLFormat& f);
166 
167     static bool hasOpenGL();
168     static bool hasOpenGLOverlays();
169 
170     void setVersion(int major, int minor);
171     int majorVersion() const;
172     int minorVersion() const;
173 
174     enum OpenGLContextProfile {
175         NoProfile,
176         CoreProfile,
177         CompatibilityProfile
178     };
179 
180     void setProfile(OpenGLContextProfile profile);
181     OpenGLContextProfile profile() const;
182 
183     enum OpenGLVersionFlag {
184         OpenGL_Version_None               = 0x00000000,
185         OpenGL_Version_1_1                = 0x00000001,
186         OpenGL_Version_1_2                = 0x00000002,
187         OpenGL_Version_1_3                = 0x00000004,
188         OpenGL_Version_1_4                = 0x00000008,
189         OpenGL_Version_1_5                = 0x00000010,
190         OpenGL_Version_2_0                = 0x00000020,
191         OpenGL_Version_2_1                = 0x00000040,
192         OpenGL_ES_Common_Version_1_0      = 0x00000080,
193         OpenGL_ES_CommonLite_Version_1_0  = 0x00000100,
194         OpenGL_ES_Common_Version_1_1      = 0x00000200,
195         OpenGL_ES_CommonLite_Version_1_1  = 0x00000400,
196         OpenGL_ES_Version_2_0             = 0x00000800,
197         OpenGL_Version_3_0                = 0x00001000,
198         OpenGL_Version_3_1                = 0x00002000,
199         OpenGL_Version_3_2                = 0x00004000,
200         OpenGL_Version_3_3                = 0x00008000,
201         OpenGL_Version_4_0                = 0x00010000,
202         OpenGL_Version_4_1                = 0x00020000,
203         OpenGL_Version_4_2                = 0x00040000,
204         OpenGL_Version_4_3                = 0x00080000
205     };
206     Q_DECLARE_FLAGS(OpenGLVersionFlags, OpenGLVersionFlag)
207 
208     static OpenGLVersionFlags openGLVersionFlags();
209 
210     static QGLFormat fromSurfaceFormat(const QSurfaceFormat &format);
211     static QSurfaceFormat toSurfaceFormat(const QGLFormat &format);
212 private:
213     QGLFormatPrivate *d;
214 
215     void detach();
216 
217     friend Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
218     friend Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
219 #ifndef QT_NO_DEBUG_STREAM
220     friend Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
221 #endif
222 };
223 
224 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLFormat::OpenGLVersionFlags)
225 
226 Q_OPENGL_EXPORT bool operator==(const QGLFormat&, const QGLFormat&);
227 Q_OPENGL_EXPORT bool operator!=(const QGLFormat&, const QGLFormat&);
228 
229 #ifndef QT_NO_DEBUG_STREAM
230 Q_OPENGL_EXPORT QDebug operator<<(QDebug, const QGLFormat &);
231 #endif
232 
233 class QGLFunctions;
234 
235 class Q_OPENGL_EXPORT QGLContext
236 {
237     Q_DECLARE_PRIVATE(QGLContext)
238 public:
239     QGLContext(const QGLFormat& format, QPaintDevice* device);
240     QGLContext(const QGLFormat& format);
241     virtual ~QGLContext();
242 
243     virtual bool create(const QGLContext* shareContext = nullptr);
244     bool isValid() const;
245     bool isSharing() const;
246     void reset();
247 
248     static bool areSharing(const QGLContext *context1, const QGLContext *context2);
249 
250     QGLFormat format() const;
251     QGLFormat requestedFormat() const;
252     void setFormat(const QGLFormat& format);
253 
254     void moveToThread(QThread *thread);
255 
256     virtual void makeCurrent();
257     virtual void doneCurrent();
258 
259     virtual void swapBuffers() const;
260 
261     QGLFunctions *functions() const;
262 
263     enum BindOption {
264         NoBindOption                            = 0x0000,
265         InvertedYBindOption                     = 0x0001,
266         MipmapBindOption                        = 0x0002,
267         PremultipliedAlphaBindOption            = 0x0004,
268         LinearFilteringBindOption               = 0x0008,
269 
270         MemoryManagedBindOption                 = 0x0010, // internal flag
271         CanFlipNativePixmapBindOption           = 0x0020, // internal flag
272         TemporarilyCachedBindOption             = 0x0040, // internal flag
273 
274         DefaultBindOption                       = LinearFilteringBindOption
275                                                   | InvertedYBindOption
276                                                   | MipmapBindOption,
277         InternalBindOption                      = MemoryManagedBindOption
278                                                   | PremultipliedAlphaBindOption
279     };
280     Q_DECLARE_FLAGS(BindOptions, BindOption)
281 
282     GLuint bindTexture(const QImage &image, GLenum target, GLint format,
283                        BindOptions options);
284     GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
285                        BindOptions options);
286 
287     GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
288                        GLint format = GL_RGBA);
289     GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
290                        GLint format = GL_RGBA);
291     GLuint bindTexture(const QString &fileName);
292 
293     void deleteTexture(GLuint tx_id);
294 
295     void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
296     void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
297 
298     static void setTextureCacheLimit(int size);
299     static int textureCacheLimit();
300 
301     QFunctionPointer getProcAddress(const QString &proc) const;
302     QPaintDevice* device() const;
303     QColor overlayTransparentColor() const;
304 
305     static const QGLContext* currentContext();
306 
307     static QGLContext *fromOpenGLContext(QOpenGLContext *platformContext);
308     QOpenGLContext *contextHandle() const;
309 
310 protected:
311     virtual bool chooseContext(const QGLContext* shareContext = nullptr);
312 
313     bool deviceIsPixmap() const;
314     bool windowCreated() const;
315     void setWindowCreated(bool on);
316     bool initialized() const;
317     void setInitialized(bool on);
318 
319     uint colorIndex(const QColor& c) const;
320     void setValid(bool valid);
321     void setDevice(QPaintDevice *pDev);
322 
323 protected:
324     static QGLContext* currentCtx;
325 
326 private:
327     QGLContext(QOpenGLContext *windowContext);
328 
329     QScopedPointer<QGLContextPrivate> d_ptr;
330 
331     friend class QGLPixelBuffer;
332     friend class QGLPixelBufferPrivate;
333     friend class QGLWidget;
334     friend class QGLWidgetPrivate;
335     friend class QGLGlyphCache;
336     friend class QGL2PaintEngineEx;
337     friend class QGL2PaintEngineExPrivate;
338     friend class QGLEngineShaderManager;
339     friend class QGLTextureGlyphCache;
340     friend struct QGLGlyphTexture;
341     friend class QGLContextGroup;
342     friend class QGLPixmapBlurFilter;
343     friend class QGLTexture;
344     friend QGLFormat::OpenGLVersionFlags QGLFormat::openGLVersionFlags();
345     friend class QGLFramebufferObject;
346     friend class QGLFramebufferObjectPrivate;
347     friend class QGLFBOGLPaintDevice;
348     friend class QGLPaintDevice;
349     friend class QGLWidgetGLPaintDevice;
350     friend class QX11GLSharedContexts;
351     friend class QGLContextResourceBase;
352     friend class QSGDistanceFieldGlyphCache;
353 private:
354     Q_DISABLE_COPY(QGLContext)
355 };
356 
Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)357 Q_DECLARE_OPERATORS_FOR_FLAGS(QGLContext::BindOptions)
358 
359 class Q_OPENGL_EXPORT QGLWidget : public QWidget
360 {
361     Q_OBJECT
362     Q_DECLARE_PRIVATE(QGLWidget)
363 public:
364     explicit QGLWidget(QWidget* parent=nullptr,
365                        const QGLWidget* shareWidget = nullptr, Qt::WindowFlags f=Qt::WindowFlags());
366     explicit QGLWidget(QGLContext *context, QWidget* parent=nullptr,
367                        const QGLWidget* shareWidget = nullptr, Qt::WindowFlags f=Qt::WindowFlags());
368     explicit QGLWidget(const QGLFormat& format, QWidget* parent=nullptr,
369                        const QGLWidget* shareWidget = nullptr, Qt::WindowFlags f=Qt::WindowFlags());
370     ~QGLWidget();
371 
372     void qglColor(const QColor& c) const;
373     void qglClearColor(const QColor& c) const;
374 
375     bool isValid() const;
376     bool isSharing() const;
377 
378     void makeCurrent();
379     void doneCurrent();
380 
381     bool doubleBuffer() const;
382     void swapBuffers();
383 
384     QGLFormat format() const;
385     void setFormat(const QGLFormat& format);
386 
387     QGLContext* context() const;
388     void setContext(QGLContext* context, const QGLContext* shareContext = nullptr,
389                     bool deleteOldContext = true);
390 
391     QPixmap renderPixmap(int w = 0, int h = 0, bool useContext = false);
392     QImage grabFrameBuffer(bool withAlpha = false);
393 
394     void makeOverlayCurrent();
395     const QGLContext* overlayContext() const;
396 
397     static QImage convertToGLFormat(const QImage& img);
398 
399     const QGLColormap & colormap() const;
400     void  setColormap(const QGLColormap & map);
401 
402     void renderText(int x, int y, const QString & str,
403                     const QFont & fnt = QFont());
404     void renderText(double x, double y, double z, const QString & str,
405                     const QFont & fnt = QFont());
406     QPaintEngine *paintEngine() const override;
407 
408     GLuint bindTexture(const QImage &image, GLenum target, GLint format,
409                        QGLContext::BindOptions options);
410     GLuint bindTexture(const QPixmap &pixmap, GLenum target, GLint format,
411                        QGLContext::BindOptions options);
412 
413     GLuint bindTexture(const QImage &image, GLenum target = GL_TEXTURE_2D,
414                        GLint format = GL_RGBA);
415     GLuint bindTexture(const QPixmap &pixmap, GLenum target = GL_TEXTURE_2D,
416                        GLint format = GL_RGBA);
417 
418     GLuint bindTexture(const QString &fileName);
419 
420     void deleteTexture(GLuint tx_id);
421 
422     void drawTexture(const QRectF &target, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
423     void drawTexture(const QPointF &point, GLuint textureId, GLenum textureTarget = GL_TEXTURE_2D);
424 
425 public Q_SLOTS:
426     virtual void updateGL();
427     virtual void updateOverlayGL();
428 
429 protected:
430     bool event(QEvent *) override;
431     virtual void initializeGL();
432     virtual void resizeGL(int w, int h);
433     virtual void paintGL();
434 
435     virtual void initializeOverlayGL();
436     virtual void resizeOverlayGL(int w, int h);
437     virtual void paintOverlayGL();
438 
439     void setAutoBufferSwap(bool on);
440     bool autoBufferSwap() const;
441 
442     void paintEvent(QPaintEvent*) override;
443     void resizeEvent(QResizeEvent*) override;
444 
445     virtual void glInit();
446     virtual void glDraw();
447 
448     QGLWidget(QGLWidgetPrivate &dd,
449               const QGLFormat &format = QGLFormat(),
450               QWidget *parent = nullptr,
451               const QGLWidget* shareWidget = nullptr,
452               Qt::WindowFlags f = Qt::WindowFlags());
453 private:
454     Q_DISABLE_COPY(QGLWidget)
455 
456     friend class QGLDrawable;
457     friend class QGLPixelBuffer;
458     friend class QGLPixelBufferPrivate;
459     friend class QGLContext;
460     friend class QGLContextPrivate;
461     friend class QGLOverlayWidget;
462     friend class QGLPaintDevice;
463     friend class QGLWidgetGLPaintDevice;
464 };
465 
466 
467 //
468 // QGLFormat inline functions
469 //
470 
doubleBuffer()471 inline bool QGLFormat::doubleBuffer() const
472 {
473     return testOption(QGL::DoubleBuffer);
474 }
475 
depth()476 inline bool QGLFormat::depth() const
477 {
478     return testOption(QGL::DepthBuffer);
479 }
480 
rgba()481 inline bool QGLFormat::rgba() const
482 {
483     return testOption(QGL::Rgba);
484 }
485 
alpha()486 inline bool QGLFormat::alpha() const
487 {
488     return testOption(QGL::AlphaChannel);
489 }
490 
accum()491 inline bool QGLFormat::accum() const
492 {
493     return testOption(QGL::AccumBuffer);
494 }
495 
stencil()496 inline bool QGLFormat::stencil() const
497 {
498     return testOption(QGL::StencilBuffer);
499 }
500 
stereo()501 inline bool QGLFormat::stereo() const
502 {
503     return testOption(QGL::StereoBuffers);
504 }
505 
directRendering()506 inline bool QGLFormat::directRendering() const
507 {
508     return testOption(QGL::DirectRendering);
509 }
510 
hasOverlay()511 inline bool QGLFormat::hasOverlay() const
512 {
513     return testOption(QGL::HasOverlay);
514 }
515 
sampleBuffers()516 inline bool QGLFormat::sampleBuffers() const
517 {
518     return testOption(QGL::SampleBuffers);
519 }
520 
521 QT_END_NAMESPACE
522 
523 #endif // QT_NO_OPENGL
524 #endif // QGL_H
525