| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D 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 | #include "platformsurfacefilter_p.h" |
| 41 | |
| 42 | #include <Qt3DRender/private/abstractrenderer_p.h> |
| 43 | |
| 44 | #include <QMetaObject> |
| 45 | #include <QPlatformSurfaceEvent> |
| 46 | #include <QOffscreenSurface> |
| 47 | #include <QtGui/qwindow.h> |
| 48 | |
| 49 | QT_BEGIN_NAMESPACE |
| 50 | |
| 51 | namespace Qt3DRender { |
| 52 | namespace Render { |
| 53 | |
| 54 | QSemaphore PlatformSurfaceFilter::m_surfacesSemaphore(1); |
| 55 | QHash<QSurface *, bool> PlatformSurfaceFilter::m_surfacesValidity; |
| 56 | |
| 57 | // Surface protection |
| 58 | // The surface is accessible from multiple threads at 3 potential places |
| 59 | // 1) In here (the frontend) when we receive an event |
| 60 | // 2) In the RenderViewJobs |
| 61 | // 3) In the Renderer for the submission |
| 62 | // * We don't need any protection in 2) as we are just copying the pointer |
| 63 | // but not performing any access to the texture as all the information |
| 64 | // we need has been cached in the RenderSurfaceSelector element |
| 65 | // * This leaves us with case 1 and 3. It is important that if the surface |
| 66 | // is about to be destroyed that we let the time to the submission thread |
| 67 | // to complete whatever it is doing with a surface before we have the time |
| 68 | // to process the AboutToBeDestroyed event. For that we have lockSurface, releaseSurface |
| 69 | // on the PlatformSurfaceFilter. But that's not enough, you need to be sure that |
| 70 | // the surface is still valid. When locked, you can use isSurfaceValid to check |
| 71 | // if a surface is still accessible. |
| 72 | // A Surface is valid when it has been created and becomes invalid when AboutToBeDestroyed |
| 73 | // has been called |
| 74 | // SurfaceLocker is a convenience type to perform locking an surface validity check |
| 75 | |
| 76 | PlatformSurfaceFilter::PlatformSurfaceFilter(QObject *parent) |
| 77 | : QObject(parent) |
| 78 | , m_obj(nullptr) |
| 79 | , m_surface(nullptr) |
| 80 | { |
| 81 | qRegisterMetaType<QSurface *>(typeName: "QSurface*" ); |
| 82 | } |
| 83 | |
| 84 | PlatformSurfaceFilter::~PlatformSurfaceFilter() |
| 85 | { |
| 86 | if (m_obj) |
| 87 | m_obj->removeEventFilter(obj: this); |
| 88 | } |
| 89 | |
| 90 | bool PlatformSurfaceFilter::eventFilter(QObject *obj, QEvent *e) |
| 91 | { |
| 92 | if (obj == m_obj && e->type() == QEvent::PlatformSurface) { |
| 93 | QPlatformSurfaceEvent *ev = static_cast<QPlatformSurfaceEvent *>(e); |
| 94 | |
| 95 | switch (ev->surfaceEventType()) { |
| 96 | case QPlatformSurfaceEvent::SurfaceCreated: |
| 97 | // set validy to true |
| 98 | { |
| 99 | markSurfaceAsValid(); |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed: |
| 104 | // set validity to false |
| 105 | { |
| 106 | SurfaceLocker lock(m_surface); |
| 107 | // If we remove it, the call to isSurfaceValid will |
| 108 | // implicitely return false |
| 109 | PlatformSurfaceFilter::m_surfacesValidity.remove(akey: m_surface); |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | default: |
| 114 | qCritical(msg: "Unknown surface type" ); |
| 115 | Q_UNREACHABLE(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (obj == m_obj && e->type() == QEvent::Expose) { |
| 120 | QExposeEvent *ev = static_cast<QExposeEvent *>(e); |
| 121 | Q_UNUSED(ev); |
| 122 | // We could use this to tell to ignore the RenderView |
| 123 | // at submission time since it's not exposed |
| 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | void PlatformSurfaceFilter::lockSurface() |
| 129 | { |
| 130 | PlatformSurfaceFilter::m_surfacesSemaphore.acquire(n: 1); |
| 131 | } |
| 132 | |
| 133 | void PlatformSurfaceFilter::releaseSurface() |
| 134 | { |
| 135 | PlatformSurfaceFilter::m_surfacesSemaphore.release(n: 1); |
| 136 | } |
| 137 | |
| 138 | bool PlatformSurfaceFilter::isSurfaceValid(QSurface *surface) |
| 139 | { |
| 140 | // Should be called only when the surface is locked |
| 141 | // with the semaphore |
| 142 | return m_surfacesValidity.value(akey: surface, adefaultValue: false); |
| 143 | } |
| 144 | |
| 145 | void PlatformSurfaceFilter::markSurfaceAsValid() |
| 146 | { |
| 147 | SurfaceLocker lock(m_surface); |
| 148 | PlatformSurfaceFilter::m_surfacesValidity.insert(akey: m_surface, avalue: true); |
| 149 | } |
| 150 | |
| 151 | SurfaceLocker::SurfaceLocker(QSurface *surface) |
| 152 | : m_surface(surface) |
| 153 | { |
| 154 | PlatformSurfaceFilter::lockSurface(); |
| 155 | } |
| 156 | |
| 157 | SurfaceLocker::~SurfaceLocker() |
| 158 | { |
| 159 | PlatformSurfaceFilter::releaseSurface(); |
| 160 | } |
| 161 | |
| 162 | bool SurfaceLocker::isSurfaceValid() const |
| 163 | { |
| 164 | return PlatformSurfaceFilter::isSurfaceValid(surface: m_surface); |
| 165 | } |
| 166 | |
| 167 | } // namespace Render |
| 168 | } // namespace Qt3DRender |
| 169 | |
| 170 | QT_END_NAMESPACE |
| 171 | |