summaryrefslogtreecommitdiffstats
path: root/src/quick3d/quick3drender/scene2d/scene2d.cpp
blob: 210cc5f0f18048792d7413a530992927fa4c22c4 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** 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 http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <Qt3DCore/qpropertyupdatedchange.h>
#include <Qt3DQuickRender/qscene2d.h>

#include <private/qscene2d_p.h>
#include <private/scene2d_p.h>
#include <private/graphicscontext_p.h>
#include <private/texture_p.h>
#include <private/nodemanagers_p.h>


QT_BEGIN_NAMESPACE

using namespace Qt3DRender::Quick;

namespace Qt3DRender {

namespace Render {

namespace Quick {

RenderQmlEventHandler::RenderQmlEventHandler(Scene2D *node)
    : QObject()
    , m_node(node)
{
}

// Event handler for the RenderQmlToTexture::renderThread
bool RenderQmlEventHandler::event(QEvent *e)
{
    switch (e->type()) {

    case RENDER: {
        m_node->render();
        return true;
    }

    case INITIALIZE: {
        m_node->initializeRender();
        return true;
    }

    case QUIT: {
        m_node->cleanup();
        return true;
    }

    default:
        break;
    }
    return QObject::event(e);
}

Scene2D::Scene2D()
    : FrameGraphNode(FrameGraphNode::InvalidNodeType)
    , m_context(nullptr)
    , m_sharedObject(nullptr)
    , m_renderThread(nullptr)
    , m_graphicsContext(nullptr)
    , m_texture(nullptr)
    , m_initialized(false)
    , m_renderInitialized(false)
    , m_renderOnce(false)
{

}

Scene2D::~Scene2D()
{
    // this gets called from aspect thread. Wait for the render thread then delete it.
    if (m_renderThread) {
        m_renderThread->wait(1000);
        delete m_renderThread;
    }
}

void Scene2D::setTexture(Qt3DCore::QNodeId textureId)
{
    m_textureId = textureId;
    attach();
    checkInitialized();
}

void Scene2D::checkInitialized()
{
    if (!m_initialized && m_textureId != Qt3DCore::QNodeId()) {

        // Create render thread
        m_renderThread = new QThread();
        m_renderThread->setObjectName(QStringLiteral("Scene2D::renderThread"));
        m_sharedObject->m_renderThread = m_renderThread;

        // Create event handler for the render thread
        m_sharedObject->m_renderObject = new RenderQmlEventHandler(this);
        m_sharedObject->m_renderObject->moveToThread(m_sharedObject->m_renderThread);
        m_sharedObject->m_renderThread->start();

        // Notify main thread we have been initialized
        QCoreApplication::postEvent(m_sharedObject->m_renderManager, new QEvent(INITIALIZED));

        // Initialize render thread
        QCoreApplication::postEvent(m_sharedObject->m_renderObject, new QEvent(INITIALIZE));

        m_initialized = true;
    }
}

void Scene2D::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change)
{
    const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<QScene2DData>>(change);
    const auto &data = typedChange->data;
    m_renderOnce = m_renderOnce;
    setSharedObject(data.sharedObject);
    setTexture(data.textureId);
}

void Scene2D::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e)
{
    if (e->type() == Qt3DCore::PropertyUpdated) {
        Qt3DCore::QPropertyUpdatedChangePtr propertyChange
                = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(e);
        if (propertyChange->propertyName() == QByteArrayLiteral("enabled"))
            setEnabled(propertyChange->value().toBool());
        else if (propertyChange->propertyName() == QByteArrayLiteral("dirty")) {
            // sent to trigger backend update when the texture gets rendered
            // so do nothing here
        }
        else if (propertyChange->propertyName() == QByteArrayLiteral("renderOnce"))
            m_renderOnce = propertyChange->value().toBool();
        else if (propertyChange->propertyName() == QByteArrayLiteral("texture")) {
            Qt3DCore::QNodeId textureId = propertyChange->value().value<Qt3DCore::QNodeId>();
            setTexture(textureId);
        }
        markDirty(AbstractRenderer::AllDirty);
    }
    FrameGraphNode::sceneChangeEvent(e);
}

void Scene2D::setSharedObject(Qt3DRender::Quick::Scene2DSharedObjectPtr sharedObject)
{
    m_sharedObject = sharedObject;
}

void Scene2D::initializeRender()
{
    if (!m_renderInitialized) {
        Qt3DRender::Render::Renderer *renderer
                = static_cast<Qt3DRender::Render::Renderer *>(this->renderer());
        if (!renderer)
            return;

        QSurfaceFormat format;
        format.setDepthBufferSize(24);
        format.setStencilBufferSize(8);

        m_context = new QOpenGLContext();
        m_context->setFormat(format);

        m_context->setShareContext(renderer->shareContext());
        m_context->create();

        m_graphicsContext = new GraphicsContext();
        m_graphicsContext->setOpenGLContext(m_context);
        m_graphicsContext->setRenderer(renderer);

        m_graphicsContext->makeCurrent(m_sharedObject->m_surface);
        m_sharedObject->m_renderControl->initialize(m_context);
        m_graphicsContext->doneCurrent();

        QCoreApplication::postEvent(m_sharedObject->m_renderManager, new QEvent(PREPARE));
        m_renderInitialized = true;
    }
}

void Scene2D::attach()
{
    m_attachments = AttachmentPack();
    Attachment attach;
    attach.m_mipLevel = 0;
    attach.m_textureUuid = m_textureId;
    attach.m_point = QRenderTargetOutput::AttachmentPoint::Color0;

//    m_attachments.addAttachment(attach);
}

void Scene2D::render()
{
    if (m_initialized && m_sharedObject && this->isEnabled()) {

        QMutexLocker lock(&m_sharedObject->m_mutex);

        // Lookup backend texture
        if (m_texture == nullptr) {
            m_texture = renderer()->nodeManagers()->textureManager()->lookupResource(m_textureId);
            if (!m_texture) {
                qCDebug(Render::Framegraph) << Q_FUNC_INFO << "Texture not set";
                return;
            }
        }

        m_graphicsContext->makeCurrent(m_sharedObject->m_surface);

        // Don't create the OpenGL texture in this thread.
        const bool canUseTexture = !m_texture->isTextureReset();

        if (canUseTexture) {
            // Activate fbo for the texture
            QOpenGLTexture *glTex = m_texture->getOrCreateGLTexture();
            const QSize textureSize = QSize(glTex->width(), glTex->height());

            GLuint fbo = 0; //m_graphicsContext->activateRenderTargetForQmlRender(this, m_attachments, 0);

            if (fbo != m_sharedObject->m_quickWindow->renderTargetId())
                m_sharedObject->m_quickWindow->setRenderTarget(fbo, textureSize);

            m_texture->textureLock()->lock();
        }
        // Call disallow rendering while mutex is locked
        if (canUseTexture && m_renderOnce)
            m_sharedObject->disallowRender();

        // Need to call sync even if the texture is not in use
        if (m_sharedObject->isSyncRequested()) {

            m_sharedObject->clearSyncRequest();

            m_sharedObject->m_renderControl->sync();

            // gui thread can now continue
            m_sharedObject->wakeWaiting();
            lock.unlock();
        }

        if (canUseTexture) {

            // Render
            m_sharedObject->m_renderControl->render();

            // Tell main thread we are done so it can begin cleanup
            if (m_renderOnce)
                QCoreApplication::postEvent(m_sharedObject->m_renderManager, new QEvent(RENDERED));

            m_sharedObject->m_quickWindow->resetOpenGLState();
            m_context->functions()->glFlush();
            m_texture->textureLock()->unlock();
        }
        m_graphicsContext->doneCurrent();
    }
}

void Scene2D::cleanup()
{
    if (m_renderInitialized && m_initialized) {
        m_context->makeCurrent(m_sharedObject->m_surface);
        m_sharedObject->m_renderControl->invalidate();
        m_context->doneCurrent();
        m_sharedObject->m_renderThread->quit();
        delete m_sharedObject->m_renderObject;
        m_sharedObject->m_renderObject = nullptr;
        delete m_context;
        m_context = nullptr;
        m_sharedObject = nullptr;
        delete m_graphicsContext;
        m_graphicsContext = nullptr;
        m_renderInitialized = false;
        m_initialized = false;
    }
}

} // namespace Quick

} // namespace Render

} // namespace Qt3DRender

QT_END_NAMESPACE