| 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("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(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 |
bool checkGraphicsReset(); |
| 91 |
|
| 92 |
private: |
| 93 |
QList<EGLint> buildContextAttributes(const QSurfaceFormat &format); |
| 94 |
void adopt(EGLContext context, EGLDisplay display, QPlatformOpenGLContext *shareContext); |
| 95 |
void updateFormatFromGL(); |
| 96 |
|
| 97 |
#ifndef QT_NO_OPENGL |
| 98 |
bool hasExtension(const char *name); |
| 99 |
#endif |
| 100 |
|
| 101 |
EGLContext m_eglContext; |
| 102 |
EGLContext m_shareContext; |
| 103 |
EGLDisplay m_eglDisplay; |
| 104 |
EGLConfig m_eglConfig; |
| 105 |
QSurfaceFormat m_format; |
| 106 |
EGLenum m_api; |
| 107 |
int m_swapInterval = -1; |
| 108 |
bool m_swapIntervalEnvChecked = false; |
| 109 |
int m_swapIntervalFromEnv = -1; |
| 110 |
Flags m_flags; |
| 111 |
bool m_ownsContext = false; |
| 112 |
QList<EGLint> m_contextAttrs; |
| 113 |
|
| 114 |
bool m_markedInvalid = false; |
| 115 |
GLenum (QOPENGLF_APIENTRYP m_getGraphicsResetStatus)() = nullptr; |
| 116 |
}; |
| 117 |
|
| 118 |
Q_DECLARE_OPERATORS_FOR_FLAGS(QEGLPlatformContext::Flags) |
| 119 |
|
| 120 |
QT_END_NAMESPACE |
| 121 |
|
| 122 |
#endif //QEGLPLATFORMCONTEXT_H |