summaryrefslogtreecommitdiffstats
path: root/src/api/studio3dqml/q3dsstudio3d.cpp
blob: 3b0ac0db096f129478966b369c58171e3dbe1287 (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/****************************************************************************
**
** Copyright (c) 2016 NVIDIA CORPORATION.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "q3dsstudio3d_p.h"
#include "q3dsrenderer_p.h"
#include "q3dspresentationitem_p.h"

#include <QtStudio3D/private/q3dsviewersettings_p.h>
#include <QtStudio3D/private/q3dspresentation_p.h>
#include <QtStudio3D/private/q3dssceneelement_p.h>
#include <QtStudio3D/private/viewerqmlstreamproxy_p.h>

#include <QtCore/qdebug.h>
#include <QtCore/qfileinfo.h>
#include <QtQuick/qquickwindow.h>
#include <QtQml/qqmlcontext.h>
#include <QtGui/qoffscreensurface.h>
#include <QtGui/qopenglcontext.h>

QT_BEGIN_NAMESPACE

/*!
    \qmltype Studio3D
    \instantiates Q3DSStudio3D
    \inqmlmodule QtStudio3D.OpenGL
    \ingroup OpenGLRuntime
    \inherits Item
    \keyword Studio3D

    \brief Qt 3D Studio presentation viewer.

    This type enables developers to embed Qt 3D Studio presentations in Qt
    Quick.

    \section2 Example Usage

    \qml
    Studio3D {
        id: studio3D
        anchors.fill: parent

        Presentation {
            source: "qrc:///presentation.uia"
            SceneElement {
                id: scene
                elementPath: "Scene"
                currentSlideIndex: 2
            }
            Element {
                id: textLabel
                elementPath: "Scene.Layer.myLabel"
            }
        }
        ViewerSettings {
            showRenderStats: true
        }
        onRunningChanged: {
            console.log("Presentation ready!");
        }
    }
    \endqml

    \section2 Controlling the presentation

    Like the example above suggests, Studio3D and the other types under the
    QtStudio3D import offer more than simply rendering the animated Qt 3D
    Studio presentation. They also offer scene manipulation, including

    \list

    \li querying and changing scene object properties (for example, the
    transform of a model, colors and other settings of a material, etc.) via
    Presentation::getAttribute(), Presentation::setAttribute(), \l Element, and
    \l DataInput,

    \li changing slides (and thus starting the relevant animations and applying
    the scene object property changes associated with the new slide) via
    Presentation::goToSlide(), \l SceneElement, and \l DataInput,

    \li and controlling the timeline (the current playback position for the
    key-frame based animations) both on the main scene and on individual
    Component nodes via Presentation::goToTime(), \l SceneElement, and \l DataInput.

    \endlist

    \sa Presentation
*/

/*!
    \qmlsignal Studio3D::frameDraw()
    \since QtStudio3D.OpenGL 2.8

    This signal is emitted each time a new image has been drawn.
*/

/*!
    \qmlsignal Studio3D::frameUpdate()

    This signal is emitted each time a frame has been rendered.
*/

/*!
    \qmlsignal Studio3D::presentationLoaded()

    This signal is emitted when the presentation has been loaded and is ready
    to be shown.
*/

/*!
    \qmlsignal Studio3D::presentationReady()

    This signal is emitted when the presentation has fully initialized its 3D
    scene for the first frame.

    The difference to presentationLoaded() is that this signal is emitted only
    when the asynchronous operations needed to build to 3D scene and the first
    frame have completed.

    When implementing splash screens via Loader items and the Item::visible
    property, this is the signal that should be used to trigger hiding the
    splash screen.
*/

Q3DSStudio3D::Q3DSStudio3D()
    : m_viewerSettings(nullptr)
    , m_presentation(nullptr)
    , m_emitRunningChange(false)
    , m_isRunning(false)
    , m_eventIgnoreFlags(EnableAllEvents)
    , m_pixelRatio(1.0)
{
    setMirrorVertically(true);
    connect(this, &Q3DSStudio3D::windowChanged, this, &Q3DSStudio3D::handleWindowChanged);
    connect(this, &Q3DSStudio3D::visibleChanged, this, &Q3DSStudio3D::handleVisibleChanged);

    updateEventMasks();
    m_startupTimer.reset(new QElapsedTimer());
    m_startupTimer->restart();
}

Q3DSStudio3D::~Q3DSStudio3D()
{
}

/*!
    \qmlproperty Presentation Studio3D::presentation

    Accessor for the presentation. Applications are expected to create a single
    Presentation child object for Studio3D. If this is omitted, a presentation
    is created automatically.

    This property is read-only.
*/
Q3DSPresentationItem *Q3DSStudio3D::presentation() const
{
    return m_presentation;
}

/*!
    \qmlproperty ViewerSettings Studio3D::viewerSettings

    Accessor for the viewerSettings. Applications are expected to create a single
    ViewerSettings child object for Studio3D. If this is omitted, ViewerSettings
    is created automatically.

    This property is read-only.
*/
Q3DSViewerSettings *Q3DSStudio3D::viewerSettings() const
{
    return m_viewerSettings;
}

/*!
    \qmlproperty string Studio3D::error

    Contains the text for the error message that was generated during the
    loading of the presentation. When no error occurred or there is no
    presentation loaded, the value is an empty string.

    This property is read-only.
*/
QString Q3DSStudio3D::error() const
{
    return m_error;
}

void Q3DSStudio3D::setError(const QString &error)
{
    if (error != m_error) {
        m_error = error;
        Q_EMIT errorChanged(m_error);
    }
}

/*!
    \qmlproperty bool Studio3D::asyncInit
    \since QtStudio3D.OpenGL 2.5

    If set to \c{true}, indicates that renderer initialization should be done asynchronously
    in a helper thread. This improves UI responsiveness while initialization is happening,
    but can lead to slower initialization. Asynchronous initialization may not work properly
    on all platforms.

    Defaults to \c{false}.

    Changing this property after renderer is created doesn't do anything.
*/
bool Q3DSStudio3D::asyncInit() const
{
    return m_asyncInit;
}

void Q3DSStudio3D::setAsyncInit(bool enabled)
{
    if (enabled != m_asyncInit) {
        m_asyncInit = enabled;
        Q_EMIT asyncInitChanged(m_asyncInit);
    }
}

/*!
    \qmlproperty EventIgnoreFlags Studio3D::ignoredEvents

    This property can be used to ignore mouse/wheel/keyboard events.
    By default all events are enabled.
*/

Q3DSStudio3D::EventIgnoreFlags Q3DSStudio3D::ignoredEvents() const
{
    return m_eventIgnoreFlags;
}

void Q3DSStudio3D::setIgnoredEvents(EventIgnoreFlags flags)
{
    if (m_eventIgnoreFlags == flags)
        return;

    m_eventIgnoreFlags = flags;
    updateEventMasks();
    Q_EMIT ignoredEventsChanged();
}

/*!
    \internal
 */
void Q3DSStudio3D::updateEventMasks()
{
    if (m_eventIgnoreFlags.testFlag(IgnoreMouseEvents)) {
        setAcceptedMouseButtons(Qt::NoButton);
        setAcceptHoverEvents(false);
    } else {
        setAcceptedMouseButtons(Qt::MouseButtonMask);
        setAcceptHoverEvents(true);
    }
}

/*!
    \internal
 */
void Q3DSStudio3D::componentComplete()
{
    const auto childObjs = children();
    for (QObject *child : childObjs) {
        auto settings = qobject_cast<Q3DSViewerSettings *>(child);
        if (settings) {
            if (m_viewerSettings)
                qWarning("Duplicate ViewerSettings defined for Studio3D.");
            else
                m_viewerSettings = settings;
        }
        auto presentation = qobject_cast<Q3DSPresentationItem *>(child);
        if (presentation) {
            if (m_presentation)
                qWarning("Duplicate Presentation defined for Studio3D.");
            else
                m_presentation = presentation;
        }
    }
    if (!m_viewerSettings)
        m_viewerSettings = new Q3DSViewerSettings(this);
    if (!m_presentation)
        m_presentation = new Q3DSPresentationItem(this);

    m_viewerSettings->d_ptr->setCommandQueue(&m_pendingCommands);
    m_presentation->d_ptr->setCommandQueue(&m_pendingCommands);

    // Ensure qml stream proxy gets created on main thread
    QQmlContext *ctx = QQmlEngine::contextForObject(this);
    m_presentation->d_ptr->streamProxy()->setEngine(ctx->engine());

    QObject::connect(m_presentation, &Q3DSPresentationItem::sourceChanged, [this]() {
        m_startupTimer->restart();
    });

    QQuickFramebufferObject::componentComplete();
}

/*!
    \internal
 */
void Q3DSStudio3D::handleWindowChanged(QQuickWindow *window)
{
    if (!window)
        return;

    auto createAsyncSurface = [this](QOpenGLContext *qmlContext) {
        if (qmlContext) {
            m_asyncInitSurface->setFormat(qmlContext->format());
            m_asyncInitSurface->create();
        }
    };

    if (m_asyncInit) {
        m_asyncInitSurface.reset(new QOffscreenSurface);
        createAsyncSurface(window->openglContext());
    }

    m_pixelRatio = window->devicePixelRatio();

    // Call tick every frame of the GUI thread to notify QML about new frame via frameUpdate signal
    connect(window, &QQuickWindow::afterAnimating, this, &Q3DSStudio3D::tick);
    // Call update after the frame is handled to queue another frame
    connect(window, &QQuickWindow::afterSynchronizing, this, &Q3DSStudio3D::update);

    if (m_asyncInit && !m_asyncInitSurface->isValid())
        connect(window, &QQuickWindow::openglContextCreated, createAsyncSurface);

    reset();
}

/*!
    \internal
    Queue up a command to inform the renderer of a newly-changed visible/hidden status.
 */
void Q3DSStudio3D::handleVisibleChanged()
{
    m_pendingCommands.m_visibleChanged = true;
    m_pendingCommands.m_visible = isVisible();
}

/*!
    \brief internal
 */
void Q3DSStudio3D::reset()
{
    // Fake a source change to trigger a reloading of the presentation
    m_pendingCommands.m_sourceChanged = true;
    m_pendingCommands.m_source = m_presentation ? m_presentation->source() : QString();
    m_pendingCommands.m_variantListChanged = true;
    m_pendingCommands.m_variantList = m_presentation ? m_presentation->variantList()
                                                     : QStringList();
    m_pendingCommands.m_shaderCacheFileChanged = true;
    m_pendingCommands.m_shaderCacheFile = m_presentation ? m_presentation->shaderCacheFile()
                                                         : QString();
}

/*!
    \internal
 */
void Q3DSStudio3D::requestResponseHandler(const QString &elementPath, CommandType commandType,
                                      void *requestData)
{
    switch (commandType) {
    case CommandType_RequestSlideInfo: {
        Q3DSSceneElement *handler = qobject_cast<Q3DSSceneElement *>(
                    m_presentation->registeredElement(elementPath));
        if (handler)
            handler->d_ptr->requestResponseHandler(commandType, requestData);
        else
            qWarning() << __FUNCTION__ << "RequestSlideInfo response got for unregistered scene.";
        break;
    }
    case CommandType_RequestDataInputs:
    case CommandType_RequestDataOutputs:
    case CommandType_RequestExportShaderCache:
    {
        Q3DSPresentation *handler = qobject_cast<Q3DSPresentation *>(m_presentation);
        if (handler) {
            handler->d_ptr->requestResponseHandler(commandType, requestData);
        } else {
            qWarning() << __FUNCTION__ << "Command " << commandType
                       << "response got for invalid presentation.";
        }
        break;
    }
    default:
        qWarning() << __FUNCTION__ << "Unknown command type.";
        break;
    }
}

/*!
    \internal
    Create the Q3DSRenderer. Invoked automatically by the QML scene graph.
 */
QQuickFramebufferObject::Renderer *Q3DSStudio3D::createRenderer() const
{
    // It is "illegal" to create a connection between the renderer
    // and the plugin, and vice-versa. The only valid time the two
    // may communicate is during Q3DSRenderer::synchronize().
    Q3DSRenderer *renderer = new Q3DSRenderer(isVisible(), m_presentation->d_ptr->streamProxy(),
                                              m_startupTimer.get(), m_asyncInitSurface.data());
    connect(renderer, &Q3DSRenderer::enterSlide,
            m_presentation->d_ptr, &Q3DSPresentationPrivate::handleSlideEntered);
    connect(renderer, &Q3DSRenderer::dataOutputValueUpdated,
            m_presentation->d_ptr, &Q3DSPresentationPrivate::handleDataOutputValueUpdate);
    connect(renderer, &Q3DSRenderer::elementsCreated,
            m_presentation->d_ptr, &Q3DSPresentationPrivate::handleElementsCreated);
    connect(renderer, &Q3DSRenderer::materialsCreated,
            m_presentation->d_ptr, &Q3DSPresentationPrivate::handleMaterialsCreated);
    connect(renderer, &Q3DSRenderer::meshesCreated,
            m_presentation->d_ptr, &Q3DSPresentationPrivate::handleMeshesCreated);
    connect(renderer, &Q3DSRenderer::shaderCacheLoadErrors,
            m_presentation, &Q3DSPresentation::shaderCacheLoadErrors);
    connect(renderer, &Q3DSRenderer::exitSlide,
            m_presentation, &Q3DSPresentation::slideExited);
    connect(renderer, &Q3DSRenderer::customSignalEmitted,
            m_presentation, &Q3DSPresentation::customSignalEmitted);
    connect(renderer, &Q3DSRenderer::requestResponse,
            this, &Q3DSStudio3D::requestResponseHandler);
    connect(renderer, &Q3DSRenderer::presentationLoaded,
            this, &Q3DSStudio3D::presentationLoaded);
    connect(renderer, &Q3DSRenderer::presentationReady,
            this, &Q3DSStudio3D::presentationReady);
    connect(renderer, &Q3DSRenderer::frameDraw,
            this, &Q3DSStudio3D::frameDraw);
    return renderer;
}

/*!
    \qmlproperty bool Studio3D::running

    The value of this property is \c true when the presentation has been loaded
    and is ready to be shown.

    This property is read-only.
*/
bool Q3DSStudio3D::isRunning() const
{
    return m_isRunning;
}

/*!
    \internal
    Emit QML `runningChanged` and `frameUpdate` and signals.
    This method is called every frame, and emits the `frameUpdate` signal every frame,
    regardless of plugin visibility. This allows a hidden Qt3DSView to still process
    information every frame, even though the Renderer is not rendering. You can use
    frameDraw signal when you need to know when a new image has been drawn.

    To prevent expensive onFrameUpdate handlers from being processed when hidden,
    add an early return to the top like:

        onFrameUpdate: {
            if (!visible) return;
            ...
        }
 */
void Q3DSStudio3D::tick()
{
    if (m_emitRunningChange) {
        m_isRunning = true;
        Q_EMIT runningChanged(true);
        m_emitRunningChange = false;
    }

    // Don't call onFrameUpdate until after onInitialize has been called
    if (m_isRunning) {
        // Give QML an opportunity to change Qt3DS values every frame
        Q_EMIT frameUpdate();
    }
}

/*!
    \internal
    Copies the list of commands previously queued up. Called by Q3DSRenderer::synchronize().
  */
void Q3DSStudio3D::getCommands(bool emitInitialize, CommandQueue &renderQueue)
{
    if (m_presentation->d_ptr->m_dataInputsChanged) {
        m_presentation->d_ptr->setDataInputValueBatch();
        m_presentation->d_ptr->m_dataInputsChanged = false;
    }

    if (emitInitialize)
        m_emitRunningChange = true;

    renderQueue.copyCommands(m_pendingCommands);
    m_pendingCommands.clear(false);
}

/*!
    \internal
 */
void Q3DSStudio3D::mousePressEvent(QMouseEvent *event)
{
    if (m_eventIgnoreFlags.testFlag(IgnoreMouseEvents))
        return;

    if (m_pixelRatio != 1.0) {
        QMouseEvent scaledEvent(event->type(), event->pos() * m_pixelRatio,
                                event->button(), event->buttons(), event->modifiers());
        m_presentation->mousePressEvent(&scaledEvent);
    } else {
        m_presentation->mousePressEvent(event);
    }
}

/*!
    \internal
 */
void Q3DSStudio3D::mouseReleaseEvent(QMouseEvent *event)
{
    if (m_eventIgnoreFlags.testFlag(IgnoreMouseEvents))
        return;

    if (m_pixelRatio != 1.0) {
        QMouseEvent scaledEvent(event->type(), event->pos() * m_pixelRatio,
                                event->button(), event->buttons(), event->modifiers());
        m_presentation->mouseReleaseEvent(&scaledEvent);
    } else {
        m_presentation->mouseReleaseEvent(event);
    }
}

/*!
    \internal
 */
void Q3DSStudio3D::mouseMoveEvent(QMouseEvent *event)
{
    if (m_eventIgnoreFlags.testFlag(IgnoreMouseEvents))
        return;

    if (m_pixelRatio != 1.0) {
        QMouseEvent scaledEvent(event->type(), event->pos() * m_pixelRatio,
                                event->button(), event->buttons(), event->modifiers());
        m_presentation->mouseMoveEvent(&scaledEvent);
    } else {
        m_presentation->mouseMoveEvent(event);
    }
}

/*!
    \internal
 */
void Q3DSStudio3D::wheelEvent(QWheelEvent *event)
{
    if (m_eventIgnoreFlags.testFlag(IgnoreWheelEvents))
        return;

    m_presentation->wheelEvent(event);
}

/*!
    \internal
 */
void Q3DSStudio3D::keyPressEvent(QKeyEvent *event)
{
    if (m_eventIgnoreFlags.testFlag(IgnoreKeyboardEvents))
        return;

    m_presentation->keyPressEvent(event);
}

/*!
    \internal
 */
void Q3DSStudio3D::keyReleaseEvent(QKeyEvent *event)
{
    if (m_eventIgnoreFlags.testFlag(IgnoreKeyboardEvents))
        return;

    if (!event->isAutoRepeat())
        m_presentation->keyReleaseEvent(event);
}

QT_END_NAMESPACE