1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtGui 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 QEGLPLATFORMCONTEXT_H |
41 | #define QEGLPLATFORMCONTEXT_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtCore/qtextstream.h> |
55 | #include <qpa/qplatformwindow.h> |
56 | #include <qpa/qplatformopenglcontext.h> |
57 | #include <QtCore/qvariant.h> |
58 | #include <QtGui/private/qt_egl_p.h> |
59 | #include <QtGui/private/qopenglcontext_p.h> |
60 | |
61 | QT_BEGIN_NAMESPACE |
62 | |
63 | class Q_GUI_EXPORT QEGLPlatformContext : public QPlatformOpenGLContext, |
64 | public QNativeInterface::QEGLContext |
65 | { |
66 | public: |
67 | enum Flag { |
68 | NoSurfaceless = 0x01 |
69 | }; |
70 | Q_DECLARE_FLAGS(Flags, Flag) |
71 | |
72 | QEGLPlatformContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display, |
73 | EGLConfig *config = nullptr, Flags flags = { }); |
74 | |
75 | template <typename T> |
76 | static QOpenGLContext *createFrom(EGLContext context, EGLDisplay contextDisplay, |
77 | EGLDisplay platformDisplay, QOpenGLContext *shareContext) |
78 | { |
79 | if (!context) |
80 | return nullptr; |
81 | |
82 | // A context belonging to a given EGLDisplay cannot be used with another one |
83 | if (contextDisplay != platformDisplay) { |
84 | qWarning("QEGLPlatformContext: Cannot adopt context from different display" ); |
85 | return nullptr; |
86 | } |
87 | |
88 | QPlatformOpenGLContext *shareHandle = shareContext ? shareContext->handle() : nullptr; |
89 | |
90 | auto *resultingContext = new QOpenGLContext; |
91 | auto *contextPrivate = QOpenGLContextPrivate::get(resultingContext); |
92 | auto *platformContext = new T; |
93 | platformContext->adopt(context, contextDisplay, shareHandle); |
94 | contextPrivate->adopt(platformContext); |
95 | return resultingContext; |
96 | } |
97 | |
98 | ~QEGLPlatformContext(); |
99 | |
100 | void initialize() override; |
101 | bool makeCurrent(QPlatformSurface *surface) override; |
102 | void doneCurrent() override; |
103 | void swapBuffers(QPlatformSurface *surface) override; |
104 | QFunctionPointer getProcAddress(const char *procName) override; |
105 | |
106 | QSurfaceFormat format() const override; |
107 | bool isSharing() const override { return m_shareContext != EGL_NO_CONTEXT; } |
108 | bool isValid() const override { return m_eglContext != EGL_NO_CONTEXT; } |
109 | |
110 | EGLContext nativeContext() const override { return eglContext(); } |
111 | |
112 | EGLContext eglContext() const; |
113 | EGLDisplay eglDisplay() const; |
114 | EGLConfig eglConfig() const; |
115 | |
116 | protected: |
117 | QEGLPlatformContext() {} // For adoption |
118 | virtual EGLSurface eglSurfaceForPlatformSurface(QPlatformSurface *surface) = 0; |
119 | virtual EGLSurface createTemporaryOffscreenSurface(); |
120 | virtual void destroyTemporaryOffscreenSurface(EGLSurface surface); |
121 | virtual void runGLChecks(); |
122 | |
123 | private: |
124 | void adopt(EGLContext context, EGLDisplay display, QPlatformOpenGLContext *shareContext); |
125 | void updateFormatFromGL(); |
126 | |
127 | EGLContext m_eglContext; |
128 | EGLContext m_shareContext; |
129 | EGLDisplay m_eglDisplay; |
130 | EGLConfig m_eglConfig; |
131 | QSurfaceFormat m_format; |
132 | EGLenum m_api; |
133 | int m_swapInterval = -1; |
134 | bool m_swapIntervalEnvChecked = false; |
135 | int m_swapIntervalFromEnv = -1; |
136 | Flags m_flags; |
137 | bool m_ownsContext = false; |
138 | QList<EGLint> m_contextAttrs; |
139 | }; |
140 | |
141 | Q_DECLARE_OPERATORS_FOR_FLAGS(QEGLPlatformContext::Flags) |
142 | |
143 | QT_END_NAMESPACE |
144 | |
145 | #endif //QEGLPLATFORMCONTEXT_H |
146 | |