summaryrefslogtreecommitdiffstats
path: root/src/runtime/aping/q3dslayer3d.cpp
blob: 4210f605f8d0269681d1e09cc856437d95e7f972 (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
/****************************************************************************
**
** Copyright (C) 2018 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$
**
****************************************************************************/

#include "q3dslayer3d_p.h"
#include "q3dsobject3d_p.h"
#include "q3dsstudio3dengine_p.h"
#include "q3dslayer3dsgnode_p.h"
#include "q3dsengine_p.h"
#include <QImage>
#include <QQuickWindow>
#include <QSGTexture>
#include <QSGTextureProvider>

QT_BEGIN_NAMESPACE

/*!
    \qmltype Layer3D
    //! \instantiates Q3DSLayer3D
    \inqmlmodule QtStudio3D
    \ingroup 3dstudioruntime2
    \inherits Item
    \since Qt 3D Studio 2.1

    \internal

    \brief An item rendering the contents of a single Qt 3D Studio layer.
 */

class Q3DSLayerTextureProvider : public QSGTextureProvider
{
public:
    QSGTexture *texture() const override { return m_texture; }
    void setTexture(QSGTexture *t) {
        if (t != m_texture) {
            m_texture = t;
            emit textureChanged();
        }
    }
private:
    QSGTexture *m_texture = nullptr;
};

Q3DSLayer3D::Q3DSLayer3D(QQuickItem *parent)
    : QQuickItem(parent)
{
    setFlag(QQuickItem::ItemHasContents, true);

    setAcceptedMouseButtons(Qt::MouseButtonMask);
    // do not opt in for hover - nothing in the 3D world needs it yet
}

// The concept of slides is not exposed in the API. All objects are
// automatically added to an implicit slide.
Q3DSSlide *Q3DSLayer3D::slide() const
{
    return static_cast<Q3DSSlide *>(m_presentation->masterSlide()->firstChild());
}

Q3DSLayer3D::~Q3DSLayer3D()
{
    if (m_engine)
        m_engine->unregisterView(this);

    // Null out m_layer to prevent children from choking when ~QQuickItem
    // generates a parent-is-null change after we return from here. In
    // addition, null out m_object as well recursively since the whole
    // Q3DSGraphObject tree under m_layer3DS is going to be destroyed.
    for (QQuickItem *item : childItems()) {
       Q3DSObject3D *child = qobject_cast<Q3DSObject3D *>(item);
       if (child)
           child->invalidate(Q3DSObject3D::InvalidateLayer | Q3DSObject3D::InvalidateObject);
    }

    if (m_presentation) {
        Q3DSUipPresentation::forAllObjectsInSubTree(m_layer3DS, [this](Q3DSGraphObject *obj) {
            slide()->removeObject(obj);
        });
        // going away for good, so removeChildNode + unregister id
        m_presentation->unlinkObject(m_layer3DS);
        delete m_layer3DS; // also destroys all its children
    }

    delete m_textureProvider;
}

// Relying on updatePaintNode to keep the SG node up-to-date is not feasable
// since the Qt3D rendering of the frame - and so the retrieval of the texture
// IDs - is happening in beforeRendering, which happens after the sync step of
// the Quick scenegraph. From there we cannot dirty the item and it's too late
// anyway. Instead, the SG node has to be updated directly from
// Q3DSStudio3DEngine::Renderer.

QSGNode *Q3DSLayer3D::updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *)
{
    // render thread

    // Have a transparent dummy texture since the node must have a valid
    // texture but it is not known when the Engine::Renderer is able to set a
    // texture on it.
    const QSize dummySize(64, 64);
    if (!m_dummyTexture) {
        QImage img(dummySize, QImage::Format_ARGB32_Premultiplied);
        img.fill(Qt::transparent);
        m_dummyTexture = window()->createTextureFromImage(img);
    }

    Q3DSLayer3DSGNode *n = static_cast<Q3DSLayer3DSGNode *>(node);
    if (!n) {
        n = new Q3DSLayer3DSGNode;
        n->setRect(QRectF(0, 0, dummySize.width(), dummySize.height()));
        n->setTexture(m_dummyTexture);
        n->setOwnsTexture(true);
    }

    m_node = n;

    return n;
}

void Q3DSLayer3D::itemChange(QQuickItem::ItemChange change,
                             const QQuickItem::ItemChangeData &changeData)
{
    switch (change) {
    case ItemSceneChange:
        sync();
        break;

    default:
        break;
    }

    QQuickItem::itemChange(change, changeData);
}

void Q3DSLayer3D::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
{
    QQuickItem::geometryChanged(newGeometry, oldGeometry);
    if (!newGeometry.isEmpty() && newGeometry.size() != oldGeometry.size())
        sync();
}

void Q3DSLayer3D::componentComplete()
{
    QQuickItem::componentComplete();
    sync();
}

QObject *Q3DSLayer3D::engine() const
{
    return m_engine;
}

void Q3DSLayer3D::setEngine(QObject *e)
{
    Q3DSStudio3DEngine *item = qobject_cast<Q3DSStudio3DEngine *>(e);
    if (e && !item) {
        qWarning() << e << "is not a Studio3DEngine";
        return;
    }

    if (m_engine == item)
        return;

    if (m_engine)
        m_engine->unregisterView(this);

    m_engine = item;
    m_engineChanged = true;

    if (m_engine) {
        m_engine->registerView(this);
        connect(m_engine, &QObject::destroyed, this, [this](QObject *obj) {
            if (obj == m_engine) {
                m_engine = nullptr;
                m_presentation = nullptr;
                m_layer3DS = nullptr;
            }
        });
    }

    sync();

    emit engineChanged();
    if (window())
        window()->update();
}

static QByteArray makeIdAndName(Q3DSLayer3D *obj)
{
    QByteArray id = obj->metaObject()->className();
    id += QByteArrayLiteral("_0x");
    id += QByteArray::number((quintptr) obj, 16);
    return id;
}

void Q3DSLayer3D::sync()
{
    if (!m_layer3DS) {
        m_engineChanged = false;

        if (!m_engine)
            return;

        m_presentation = m_engine->presentation();
        if (!m_presentation)
            return;

        // id is always unique, name does not have to be. We will use the same unique string for both.
        const QByteArray id = makeIdAndName(this);
        m_layer3DS = m_presentation->newObject<Q3DSLayerNode>(id);
        m_layer3DS->setName(QString::fromLatin1(id));

        // Communicate the explicit size of the layer as early as possible in
        // order to avoid creating textures with a dummy initial size.
        if (window()) {
            const QSize sz = size().toSize() * window()->effectiveDevicePixelRatio();
            m_engine->handleViewGeometryChange(this, sz);
        }

        // Notify the waiting Object3D, if there is one, to create its subtree
        // and parent it to our m_layer3DS.
        emit layerNodeCreated();

        slide()->addObject(m_layer3DS);
        // The children of m_layer3DS are assumed to be already added to the
        // slide, that is the responsibility of the layerNodeCreated handler in
        // Q3DSObject3D on this path.

        // The order of Layer nodes in the Q3DSGraphObject tree does not matter,
        // it does not have to match the Layer3D order in the QQuickItem tree.
        // The moment of truth. This is when things become alive.
        m_presentation->scene()->appendChildNode(m_layer3DS);

    } else if (m_engineChanged) {
        m_engineChanged = false;

        if (m_presentation) {
            Q3DSUipPresentation::forAllObjectsInSubTree(m_layer3DS, [this](Q3DSGraphObject *obj) {
                slide()->removeObject(obj);
            });
            m_presentation->scene()->removeChildNode(m_layer3DS);
        }

        if (!m_engine) {
            m_presentation = nullptr;
            // engine was set to null, keep the layer object around since
            // we may get associated with the same or another engine later on
            return;
        }

        m_presentation = m_engine->presentation();
        if (m_presentation) {
            Q3DSUipPresentation::forAllObjectsInSubTree(m_layer3DS, [this](Q3DSGraphObject *obj) {
                slide()->addObject(obj);
            });
            m_presentation->scene()->appendChildNode(m_layer3DS);
        }
    }

    if (window() && m_engine) {
        const QSize sz = size().toSize() * window()->effectiveDevicePixelRatio();
        m_engine->handleViewGeometryChange(this, sz);
    }
}

bool Q3DSLayer3D::isTextureProvider() const
{
    return QQuickItem::isTextureProvider() || m_canProvideTexture;
}

QSGTextureProvider *Q3DSLayer3D::textureProvider() const
{
    // render thread

    if (QQuickItem::isTextureProvider())
        return QQuickItem::textureProvider();

    Q_ASSERT(m_canProvideTexture);

    if (!m_textureProvider) {
        m_textureProvider = new Q3DSLayerTextureProvider;
        m_textureProvider->setTexture(m_lastTexture);
    }

    return m_textureProvider;
}

void Q3DSLayer3D::notifyTextureChange(QSGTexture *t, int msaaSampleCount)
{
    // render thread

    if (msaaSampleCount > 1) {
        // the scenegraph itself has no support for multisample textures
        m_canProvideTexture = false;
        return;
    }

    if (m_textureProvider)
        m_textureProvider->setTexture(t);

    m_lastTexture = t;
    m_canProvideTexture = true;
}

void Q3DSLayer3D::keyPressEvent(QKeyEvent *event)
{
    if (m_engine && m_engine->engine())
        m_engine->engine()->handleKeyPressEvent(event);
}

void Q3DSLayer3D::keyReleaseEvent(QKeyEvent *event)
{
    if (m_engine && m_engine->engine())
        m_engine->engine()->handleKeyReleaseEvent(event);
}

void Q3DSLayer3D::mousePressEvent(QMouseEvent *event)
{
    if (m_engine && m_engine->engine() && m_layer3DS)
        m_engine->engine()->handleMousePressEvent(event, m_layer3DS);
}

void Q3DSLayer3D::mouseMoveEvent(QMouseEvent *event)
{
    if (m_engine && m_engine->engine() && m_layer3DS)
        m_engine->engine()->handleMouseMoveEvent(event, m_layer3DS);
}

void Q3DSLayer3D::mouseReleaseEvent(QMouseEvent *event)
{
    if (m_engine && m_engine->engine() && m_layer3DS)
        m_engine->engine()->handleMouseReleaseEvent(event, m_layer3DS);
}

void Q3DSLayer3D::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (m_engine && m_engine->engine() && m_layer3DS)
        m_engine->engine()->handleMouseDoubleClickEvent(event, m_layer3DS);
}

#if QT_CONFIG(wheelevent)
void Q3DSLayer3D::wheelEvent(QWheelEvent *event)
{
    if (m_engine && m_engine->engine() && m_layer3DS)
        m_engine->engine()->handleWheelEvent(event, m_layer3DS);
}
#endif

void Q3DSLayer3D::touchEvent(QTouchEvent *event)
{
    if (m_engine && m_engine->engine() && m_layer3DS)
        m_engine->engine()->handleTouchEvent(event, m_layer3DS);
}

QT_END_NAMESPACE