summaryrefslogtreecommitdiffstats
path: root/src/runtime/q3dsengine_p.h
blob: 386f95cd0c53cb3b14d61150e94ee0ac049ba895 (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
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: http://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$
**
****************************************************************************/

#ifndef Q3DSENGINE_P_H
#define Q3DSENGINE_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QObject>
#include <QElapsedTimer>
#include <QSurfaceFormat>
#include <Qt3DCore/QAspectEngine>
#include "q3dsuipdocument_p.h"
#include "q3dsuiadocument_p.h"
#include "q3dsdatainput.h"
#include "q3dsuiaparser_p.h"
#include "q3dsscenemanager_p.h"
#include "private/q3dsbehaviorobject_p.h"
#include "q3dssourceinfo_p.h"

QT_BEGIN_NAMESPACE

class QKeyEvent;
class QMouseEvent;
class QWheelEvent;
class QTouchEvent;
class QTabletEvent;
class QQmlEngine;
class QQmlComponent;
class Q3DSInlineQmlSubPresentation;
class Q3DSViewportSettings;

namespace Qt3DRender {
class QRenderCapture;
class QRenderCaptureReply;
namespace Quick {
class QScene2D;
}
}

struct Q3DSV_PRIVATE_EXPORT Q3DSBehaviorHandle
{
    Q3DSBehaviorInstance *behaviorInstance = nullptr;
    QQmlComponent *component = nullptr;
    Q3DSBehaviorObject *object = nullptr;

    bool initialized = false;
    bool active = false;

    void updateProperties() const;
};

struct Q3DSV_PRIVATE_EXPORT Q3DSRenderLoopStats
{
    bool usingAsyncRenderAspect = false;
    bool usingSGRenderThread = false;
    float quickDeltaMs = 0;
};

class Q3DSV_PRIVATE_EXPORT Q3DSEngine : public QObject
{
    Q_OBJECT
public:
    Q3DSEngine();
    ~Q3DSEngine();

    enum Flag {
        Force4xMSAA = 0x01,
        EnableProfiling = 0x02,
        WithoutRenderAspect = 0x04,
        PreLoadSubPresentations = 0x08,
        AwakenTheDragon = 0x10
    };
    Q_DECLARE_FLAGS(Flags, Flag)

    static void clearLog();

    Flags flags() const { return m_flags; }
    void setFlags(Flags flags);
    void setFlag(Flag flag, bool enabled);

    void setSharedSubPresentationQmlEngine(QQmlEngine *qmlEngine);
    void setSharedBehaviorQmlEngine(QQmlEngine *qmlEngine);

    static void setQmlProfileUiInstance(QObject *imguiItem) { m_imguiItem = imguiItem; }
    static QObject *qmlProfileUiInstance() { return m_imguiItem; }

    using InlineSubPresList = QVector<Q3DSInlineQmlSubPresentation *>;
    // Load presentation from a uip/uia file.
    bool setSource(const Q3DSSourceInfo &sourceInfo,
                   QString *error = nullptr,
                   const InlineSubPresList &inlineQmlSubPresentations = InlineSubPresList());
    Q3DSSourceInfo source() const;

    // Load presentation from a uip document object.
    bool setDocument(const Q3DSUipDocument &uipDocument, QString *error = nullptr);

    // Load presentation from a uia document object.
    bool setDocument(const Q3DSUiaDocument &uiaDocument, QString *error = nullptr);

    // Provide a pre-constructed presentation. Entry point for programatically
    // constructed scenes.
    bool setPresentation(Q3DSUipPresentation *presentations);

    qint64 behaviorLoadTimeMsecs() const;
    qint64 totalLoadTimeMsecs() const;

    int presentationCount() const;
    QString uipFileName(int index = 0) const;
    Q3DSUipDocument *uipDocument(int index = 0) const;
    Q3DSUipPresentation *presentation(int index = 0) const;
    Q3DSUipPresentation *presentationByName(const QString &name) const;
    Q3DSSceneManager *sceneManager(int index = 0) const;

    Qt3DCore::QAspectEngine *aspectEngine() const;
    Qt3DCore::QEntity *rootEntity() const;

    Q3DSViewportSettings *viewportSettings() const;
    void setViewportSettings(Q3DSViewportSettings *viewportSettings);

    void setOnDemandRendering(bool enabled);
    void setMainLayerComposition(bool enabled);
    void setDelayedLoading(bool enable);

    QSize implicitSize() const;

    void setSurface(QObject *surface);
    QObject *surface() const;

    bool start();

    void resize(const QSize &size, qreal dpr = qreal(1.0), bool forceSynchronous = false);

    void setDataInputValue(const QString &name, const QVariant &value);
    void setDataInputMin(const QString &name, float min);
    void setDataInputMax(const QString &name, float max);
    QVector<Q3DSDataInput *> dataInputs();
    QVector<Q3DSDataInput *> dataInputs(const QVariant &metadataKey);
    bool isValidDataInput(const Q3DSDataInput *di) const;
    QVariant dataInputMetaData(const Q3DSDataInput *di, const QVariant &key) const;
    QList<QVariant> dataInputMetaDataKeys(const Q3DSDataInput *di) const;
    void setDataInputMetaData(const QString &diName, const QVariant &key, const QVariant &metaData);
    void removeDatainputMetadata(const QString &name, const QVariant &key);
    float dataInputMin(const QString &name) const;
    float dataInputMax(const QString &name) const;
    void fireEvent(Q3DSGraphObject *target, Q3DSUipPresentation *presentation, const QString &event);
    void goToTime(Q3DSGraphObject *context, Q3DSUipPresentation *presentation, float milliseconds);
    void goToSlideByName(Q3DSGraphObject *context, Q3DSUipPresentation *presentation, const QString &name);
    void goToSlideByIndex(Q3DSGraphObject *context, Q3DSUipPresentation *presentation, int index);
    void goToSlideByDirection(Q3DSGraphObject *context, Q3DSUipPresentation *presentation, bool next, bool wrap);
    void preloadSlide(Q3DSSlide *slide, Q3DSUipPresentation *presentation);
    void unloadSlide(Q3DSSlide *slide, Q3DSUipPresentation *presentation);

    void handleKeyPressEvent(QKeyEvent *e);
    void handleKeyReleaseEvent(QKeyEvent *e);
    void handleMousePressEvent(QMouseEvent *e, Q3DSLayerNode *targetLayer = nullptr);
    void handleMouseMoveEvent(QMouseEvent *e, Q3DSLayerNode *targetLayer = nullptr);
    void handleMouseReleaseEvent(QMouseEvent *e, Q3DSLayerNode *targetLayer = nullptr);
    void handleMouseDoubleClickEvent(QMouseEvent *e, Q3DSLayerNode *targetLayer = nullptr);
#if QT_CONFIG(wheelevent)
    void handleWheelEvent(QWheelEvent *e, Q3DSLayerNode *targetLayer = nullptr);
#endif
    void handleTouchEvent(QTouchEvent *e, Q3DSLayerNode *targetLayer = nullptr);
#if QT_CONFIG(tabletevent)
    void handleTabletEvent(QTabletEvent *e, Q3DSLayerNode *targetLayer = nullptr);
#endif

    void setAutoToggleProfileUi(bool enabled) { m_autoToggleProfileUi = enabled; }
    void setProfileUiVisible(bool visible, bool openLogAndConsole = false);
    bool isStereoscopic() const;
    bool isProfileUiVisible() const;
    void configureProfileUi(float scale);
    float profileUiScaleFactor() const { return m_profileUiScale; }

    typedef QHash<Q3DSBehaviorInstance *, Q3DSBehaviorHandle> BehaviorMap;

    typedef std::function<void(Q3DSBehaviorInstance *, const QString &)> BehaviorLoadedCallback;
    void loadBehaviorInstance(Q3DSBehaviorInstance *behaviorInstance,
                              Q3DSUipPresentation *pres,
                              BehaviorLoadedCallback callback = nullptr);
    void unloadBehaviorInstance(Q3DSBehaviorInstance *behaviorInstance);
    const BehaviorMap &behaviorHandles() const { return m_behaviorHandles; }

    // These two functions are the only place where the elementPath concept is
    // present in the engine (so that mappings can be made for the 3DS1-style
    // APIs). Proper objects must be used everywhere else.
    Q3DSGraphObject *findObjectByHashIdOrNameOrPath(Q3DSGraphObject *thisObject,
                                                    Q3DSUipPresentation *defaultPresentation,
                                                    const QString &idOrNameOrPath,
                                                    Q3DSUipPresentation **actualPresentation = nullptr) const;
    QString makePath(Q3DSGraphObject *obj) const;

    Qt3DCore::QNodeId layerTextureNodeId(Q3DSLayerNode *layer3DS) const;

    void reportQuickRenderLoopStats(float deltaMs, bool isThreaded);
    Q3DSRenderLoopStats renderLoopStats();

    Q3DSSubPresentation loadSubUipPresentation(const QString &subPresId);

    void createAspectEngine();

public Q_SLOTS:
    void requestGrab();

Q_SIGNALS:
    void presentationLoaded();
    void nextFrameStarting();
    void grabReady(const QImage &image);
    void customSignalEmitted(Q3DSGraphObject *obj, const QString &name);
    void slideEntered(Q3DSGraphObject *context, int index, const QString &name);
    void slideExited(Q3DSGraphObject *context, int index, const QString &name);
    void layerResized(Q3DSLayerNode *layer3DS);
    void layerTextureNodeChanged(Q3DSLayerNode *layer3DS);
    void stereoModeChanged();

private:
    Q_DISABLE_COPY(Q3DSEngine)

    struct Presentation {
        Q3DSSubPresentation subPres;
    };

    struct UipPresentation : Presentation {
        Q3DSUipDocument *uipDocument = nullptr;
        Q3DSSceneManager::Scene q3dscene;
        Q3DSSceneManager *sceneManager = nullptr;
        Q3DSUipPresentation *presentation = nullptr;
    };

    struct QmlPresentation : Presentation {
        // we have either a QmlDocument with the source or filename, or an
        // inline subpresentation object with a QQuickItem* in it
        Q3DSQmlDocument *qmlDocument = nullptr;
        Q3DSInlineQmlSubPresentation *inlineQmlSubPres = nullptr;
        Qt3DRender::Quick::QScene2D *scene2d = nullptr;
    };

    bool loadPresentations();
    void finalizePresentations();
    bool loadUipPresentation(UipPresentation *pres);
    bool buildUipPresentationScene(UipPresentation *pres);
    bool loadSubUipPresentation(UipPresentation *pres);
    bool buildSubUipPresentationScene(UipPresentation *pres);
    bool loadSubQmlPresentation(QmlPresentation *pres);

    bool parseUipDocument(UipPresentation *pres);
    bool parseUiaDocument(Q3DSUiaParser::Uia &uiaDoc, const QString &sourcePrefix);

    void destroy();
    void prepareForReload();

    void loadBehaviors();
    void destroyBehaviorHandle(const Q3DSBehaviorHandle &h);
    void behaviorFrameUpdate(float dt);
    QRect calculateViewport(const QSize &surfaceSize, const QSize &presentationSize) const;
    void registerQmlTypes();
    void initializeDataInputs();

    QObject *m_surface = nullptr;
    QSize m_implicitSize;
    QSize m_size;
    qreal m_dpr = 1;
    Flags m_flags;
    Q3DSSourceInfo m_sourceInfo; // uip or uia file + variant info
    QVector<UipPresentation> m_uipPresentations;
    QVector<QmlPresentation> m_qmlPresentations;
    QVector<Q3DSInlineQmlSubPresentation *> m_inlineQmlPresentations;
    Q3DSDataInputEntry::Map m_dataInputEntries;
    Q3DSDataInputEntry::MetadataMap m_dataInputMetaData;
    // In addition to datainput bookkeeping data, let engine instantiate
    // and manage actual datainput objects created. This removes the need
    // to create datainputs on-the-fly when Q3DSPresentation API client
    // requests a list of datainputs
    QVector<Q3DSDataInput *> m_dataInputs;

    QQmlEngine *m_qmlSubPresentationEngine = nullptr;
    bool m_ownsQmlSubPresentationEngine = false;
    QScopedPointer<Qt3DCore::QAspectEngine> m_aspectEngine;

    qint64 m_loadTime = 0;
    qint64 m_behaviorLoadTime = 0;

    QElapsedTimer m_profilerActivateTimer;
    QElapsedTimer m_sourceLoadTimer;

    Qt3DRender::QRenderCapture *m_capture = nullptr;
    QHash<Qt3DRender::QRenderCaptureReply*, QMetaObject::Connection> m_captureConnections;

    QObject m_profileUiEventSource;
    float m_profileUiScale = 1.0f;
    bool m_autoToggleProfileUi = true;

    QQmlEngine *m_behaviorQmlEngine = nullptr;
    bool m_ownsBehaviorQmlEngine = false;
    BehaviorMap m_behaviorHandles;

    bool m_onDemandRendering = false;
    bool m_mainLayerComposition = true;
    Q3DSViewportSettings *m_viewportSettings = nullptr;
    static QObject *m_imguiItem;

    Q3DSRenderLoopStats m_renderLoopStats;
    QMutex m_renderLoopStatsMutex;
    float m_quickDeltaSum = 0;
    int m_quickDeltaCount = 0;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(Q3DSEngine::Flags)

QT_END_NAMESPACE

#endif // Q3DSENGINE_P_H