| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 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 QT3DRENDER_RENDER_PLATFORMSURFACEFILTER_H |
| 5 | #define QT3DRENDER_RENDER_PLATFORMSURFACEFILTER_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 for the convenience |
| 12 | // of other Qt classes. 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 <private/qt3drender_global_p.h> |
| 19 | |
| 20 | #include <QtCore/qmutex.h> |
| 21 | #include <QtCore/qobject.h> |
| 22 | #include <QtCore/qpointer.h> |
| 23 | #include <QtGui/qsurface.h> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class QOffscreenSurface; |
| 28 | class QWindow; |
| 29 | |
| 30 | namespace Qt3DRender { |
| 31 | namespace Render { |
| 32 | |
| 33 | class AbstractRenderer; |
| 34 | |
| 35 | class Q_3DRENDERSHARED_PRIVATE_EXPORT PlatformSurfaceFilter : public QObject |
| 36 | { |
| 37 | Q_OBJECT |
| 38 | |
| 39 | public: |
| 40 | explicit PlatformSurfaceFilter(QObject *parent = 0); |
| 41 | ~PlatformSurfaceFilter(); |
| 42 | |
| 43 | bool eventFilter(QObject *obj, QEvent *e) override; |
| 44 | |
| 45 | static void lockSurface(); |
| 46 | static void releaseSurface(); |
| 47 | static bool isSurfaceValid(QSurface *surface); |
| 48 | |
| 49 | template<class T> |
| 50 | void setSurface(T *surface) |
| 51 | { |
| 52 | if (m_obj == surface) |
| 53 | return; |
| 54 | |
| 55 | if (m_obj) |
| 56 | m_obj->removeEventFilter(obj: this); |
| 57 | |
| 58 | // Surface is offset from QWindow/QOffscreenSurface due to multiple inheritance |
| 59 | m_surface = static_cast<QSurface *>(surface); |
| 60 | m_obj = surface; |
| 61 | |
| 62 | if (m_obj) { |
| 63 | m_obj->installEventFilter(filterObj: this); |
| 64 | markSurfaceAsValid(); |
| 65 | } |
| 66 | } |
| 67 | private: |
| 68 | QPointer<QObject> m_obj; |
| 69 | QSurface *m_surface; |
| 70 | |
| 71 | static QBasicMutex m_surfacesMutex; |
| 72 | static QHash<QSurface *, bool> m_surfacesValidity; |
| 73 | void markSurfaceAsValid(); |
| 74 | }; |
| 75 | |
| 76 | class Q_3DRENDERSHARED_PRIVATE_EXPORT SurfaceLocker |
| 77 | { |
| 78 | public: |
| 79 | explicit SurfaceLocker(QSurface *surface); |
| 80 | ~SurfaceLocker(); |
| 81 | bool isSurfaceValid() const; |
| 82 | |
| 83 | private: |
| 84 | QSurface *m_surface; |
| 85 | }; |
| 86 | |
| 87 | } // namespace Render |
| 88 | } // namespace Qt3DRender |
| 89 | |
| 90 | QT_END_NAMESPACE |
| 91 | |
| 92 | #endif // QT3DRENDER_RENDER_PLATFORMSURFACEFILTER_H |
| 93 | |