| 1 | // Copyright (C) 2020 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QEGLPLATFORMCONTEXT_H |
| 5 | #define QEGLPLATFORMCONTEXT_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtCore/qtextstream.h> |
| 19 | #include <qpa/qplatformwindow.h> |
| 20 | #include <qpa/qplatformopenglcontext.h> |
| 21 | #include <QtCore/qvariant.h> |
| 22 | #include <QtGui/private/qt_egl_p.h> |
| 23 | #include <QtGui/private/qopenglcontext_p.h> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class Q_GUI_EXPORT QEGLPlatformContext : public QPlatformOpenGLContext, |
| 28 | public QNativeInterface::QEGLContext |
| 29 | { |
| 30 | public: |
| 31 | enum Flag { |
| 32 | NoSurfaceless = 0x01 |
| 33 | }; |
| 34 | Q_DECLARE_FLAGS(Flags, Flag) |
| 35 | |
| 36 | QEGLPlatformContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display, |
| 37 | EGLConfig *config = nullptr, Flags flags = { }); |
| 38 | |
| 39 | template <typename T> |
| 40 | static QOpenGLContext *createFrom(EGLContext context, EGLDisplay contextDisplay, |
| 41 | EGLDisplay platformDisplay, QOpenGLContext *shareContext) |
| 42 | { |
| 43 | if (!context) |
| 44 | return nullptr; |
| 45 | |
| 46 | // A context belonging to a given EGLDisplay cannot be used with another one |
| 47 | if (contextDisplay != platformDisplay) { |
| 48 | qWarning(msg: "QEGLPlatformContext: Cannot adopt context from different display" ); |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | QPlatformOpenGLContext *shareHandle = shareContext ? shareContext->handle() : nullptr; |
| 53 | |
| 54 | auto *resultingContext = new QOpenGLContext; |
| 55 | auto *contextPrivate = QOpenGLContextPrivate::get(context: resultingContext); |
| 56 | auto *platformContext = new T; |
| 57 | platformContext->adopt(context, contextDisplay, shareHandle); |
| 58 | contextPrivate->adopt(platformContext); |
| 59 | return resultingContext; |
| 60 | } |
| 61 | |
| 62 | ~QEGLPlatformContext(); |
| 63 | |
| 64 | void initialize() override; |
| 65 | bool makeCurrent(QPlatformSurface *surface) override; |
| 66 | void doneCurrent() override; |
| 67 | void swapBuffers(QPlatformSurface *surface) override; |
| 68 | QFunctionPointer getProcAddress(const char *procName) override; |
| 69 | |
| 70 | QSurfaceFormat format() const override; |
| 71 | bool isSharing() const override { return m_shareContext != EGL_NO_CONTEXT; } |
| 72 | bool isValid() const override { return m_eglContext != EGL_NO_CONTEXT && !m_markedInvalid; } |
| 73 | |
| 74 | EGLContext nativeContext() const override { return eglContext(); } |
| 75 | EGLConfig config() const override { return eglConfig(); } |
| 76 | EGLDisplay display() const override { return eglDisplay(); } |
| 77 | |
| 78 | virtual void invalidateContext() override { m_markedInvalid = true; } |
| 79 | |
| 80 | EGLContext eglContext() const; |
| 81 | EGLDisplay eglDisplay() const; |
| 82 | EGLConfig eglConfig() const; |
| 83 | |
| 84 | protected: |
| 85 | QEGLPlatformContext() {} // For adoption |
| 86 | virtual EGLSurface eglSurfaceForPlatformSurface(QPlatformSurface *surface) = 0; |
| 87 | virtual EGLSurface createTemporaryOffscreenSurface(); |
| 88 | virtual void destroyTemporaryOffscreenSurface(EGLSurface surface); |
| 89 | virtual void runGLChecks(); |
| 90 | |
| 91 | private: |
| 92 | void adopt(EGLContext context, EGLDisplay display, QPlatformOpenGLContext *shareContext); |
| 93 | void updateFormatFromGL(); |
| 94 | |
| 95 | EGLContext m_eglContext; |
| 96 | EGLContext m_shareContext; |
| 97 | EGLDisplay m_eglDisplay; |
| 98 | EGLConfig m_eglConfig; |
| 99 | QSurfaceFormat m_format; |
| 100 | EGLenum m_api; |
| 101 | int m_swapInterval = -1; |
| 102 | bool m_swapIntervalEnvChecked = false; |
| 103 | int m_swapIntervalFromEnv = -1; |
| 104 | Flags m_flags; |
| 105 | bool m_ownsContext = false; |
| 106 | QList<EGLint> m_contextAttrs; |
| 107 | |
| 108 | bool m_markedInvalid = false; |
| 109 | }; |
| 110 | |
| 111 | Q_DECLARE_OPERATORS_FOR_FLAGS(QEGLPlatformContext::Flags) |
| 112 | |
| 113 | QT_END_NAMESPACE |
| 114 | |
| 115 | #endif //QEGLPLATFORMCONTEXT_H |
| 116 | |