summaryrefslogtreecommitdiffstats
path: root/src/render/backend/platformsurfacefilter.cpp
blob: a696cde01892fb94b7d9247163cedccb77cf437f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "platformsurfacefilter_p.h"

#include <Qt3DRender/private/abstractrenderer_p.h>

#include <QMetaObject>
#include <QPlatformSurfaceEvent>
#include <QOffscreenSurface>
#include <QtGui/qwindow.h>

QT_BEGIN_NAMESPACE

namespace Qt3DRender {
namespace Render {

QSemaphore PlatformSurfaceFilter::m_surfacesSemaphore(1);
QHash<QSurface *, bool> PlatformSurfaceFilter::m_surfacesValidity;

// Surface protection
// The surface is accessible from multiple threads at 3 potential places
// 1) In here (the frontend) when we receive an event
// 2) In the RenderViewJobs
// 3) In the Renderer for the submission
// * We don't need any protection in 2) as we are just copying the pointer
// but not performing any access to the texture as all the information
// we need has been cached in the RenderSurfaceSelector element
// * This leaves us with case 1 and 3. It is important that if the surface
// is about to be destroyed that we let the time to the submission thread
// to complete whatever it is doing with a surface before we have the time
// to process the AboutToBeDestroyed event. For that we have lockSurface, releaseSurface
// on the PlatformSurfaceFilter. But that's not enough, you need to be sure that
// the surface is still valid. When locked, you can use isSurfaceValid to check
// if a surface is still accessible.
// A Surface is valid when it has been created and becomes invalid when AboutToBeDestroyed
// has been called
// SurfaceLocker is a convenience type to perform locking an surface validity check

PlatformSurfaceFilter::PlatformSurfaceFilter(QObject *parent)
    : QObject(parent)
    , m_obj(nullptr)
    , m_surface(nullptr)
{
    qRegisterMetaType<QSurface *>("QSurface*");
}

PlatformSurfaceFilter::~PlatformSurfaceFilter()
{
    if (m_obj)
        m_obj->removeEventFilter(this);
}

bool PlatformSurfaceFilter::eventFilter(QObject *obj, QEvent *e)
{
    if (obj == m_obj && e->type() == QEvent::PlatformSurface) {
        QPlatformSurfaceEvent *ev = static_cast<QPlatformSurfaceEvent *>(e);

        switch (ev->surfaceEventType()) {
        case QPlatformSurfaceEvent::SurfaceCreated:
            // set validy to true
        {
            markSurfaceAsValid();
            break;
        }

        case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed:
            // set validity to false
        {
            SurfaceLocker lock(m_surface);
            // If we remove it, the call to isSurfaceValid will
            // implicitely return false
            PlatformSurfaceFilter::m_surfacesValidity.remove(m_surface);
            break;
        }

        default:
            qCritical("Unknown surface type");
            Q_UNREACHABLE();
        }
    }

    if (obj == m_obj && e->type() == QEvent::Expose) {
        QExposeEvent *ev = static_cast<QExposeEvent *>(e);
        Q_UNUSED(ev);
        // We could use this to tell to ignore the RenderView
        // at submission time since it's not exposed
    }
    return false;
}

void PlatformSurfaceFilter::lockSurface()
{
    PlatformSurfaceFilter::m_surfacesSemaphore.acquire(1);
}

void PlatformSurfaceFilter::releaseSurface()
{
    PlatformSurfaceFilter::m_surfacesSemaphore.release(1);
}

bool PlatformSurfaceFilter::isSurfaceValid(QSurface *surface)
{
    // Should be called only when the surface is locked
    // with the semaphore
    return m_surfacesValidity.value(surface, false);
}

void PlatformSurfaceFilter::markSurfaceAsValid()
{
    SurfaceLocker lock(m_surface);
    PlatformSurfaceFilter::m_surfacesValidity.insert(m_surface, true);
}

SurfaceLocker::SurfaceLocker(QSurface *surface)
    : m_surface(surface)
{
    PlatformSurfaceFilter::lockSurface();
}

SurfaceLocker::~SurfaceLocker()
{
    PlatformSurfaceFilter::releaseSurface();
}

bool SurfaceLocker::isSurfaceValid() const
{
    return PlatformSurfaceFilter::isSurfaceValid(m_surface);
}

} // namespace Render
} // namespace Qt3DRender

QT_END_NAMESPACE