summaryrefslogtreecommitdiffstats
path: root/src/Runtime/api/studio3d/q3dssurfaceviewer.cpp
blob: cea764e730ed42bf867e589b60e52147e014dc86 (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
/****************************************************************************
**
** 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 "q3dssurfaceviewer_p.h"
#include "Qt3DSAudioPlayerImpl.h"
#include "viewerqmlstreamproxy_p.h"
#include "q3dsviewersettings_p.h"
#include "q3dspresentation_p.h"
#include "studioutils_p.h"

#include <QtCore/qdebug.h>
#include <QtGui/qopenglcontext.h>
#include <QtGui/qopenglfunctions.h>
#include <QtGui/qoffscreensurface.h>
#include <QtGui/qwindow.h>
#include <QtGui/QPlatformSurfaceEvent>

#include <QtCore/QFileInfo>

using namespace Q3DSViewer;

QT_BEGIN_NAMESPACE

Q3DSSurfaceViewer::Q3DSSurfaceViewer(QObject *parent)
    : QObject(parent)
    , d_ptr(new Q3DSSurfaceViewerPrivate(this))
{
}

Q3DSSurfaceViewer::~Q3DSSurfaceViewer()
{
    delete d_ptr;
}

bool Q3DSSurfaceViewer::initialize(QSurface *surface, QOpenGLContext *context, GLuint fboId)
{
    return d_ptr->initialize(surface, context, fboId);
}

void Q3DSSurfaceViewer::shutdown()
{
    d_ptr->shutdown();
}

void Q3DSSurfaceViewer::reset()
{
    d_ptr->reset();
}

void Q3DSSurfaceViewer::update()
{
    d_ptr->update();
}

QImage Q3DSSurfaceViewer::grab(const QRect &rect)
{
    return d_ptr->grab(rect);
}

QSize Q3DSSurfaceViewer::size() const
{
    return d_ptr->m_size;
}

void Q3DSSurfaceViewer::setSize(const QSize &size)
{
    d_ptr->setSize(size);
}

bool Q3DSSurfaceViewer::autoSize() const
{
    return d_ptr->m_autoSize;
}

void Q3DSSurfaceViewer::setAutoSize(bool autoSize)
{
    if (d_ptr->m_autoSize != autoSize) {
        d_ptr->m_autoSize = autoSize;
        Q_EMIT autoSizeChanged(autoSize);
    }
}

int Q3DSSurfaceViewer::updateInterval() const
{
    return d_ptr->m_updateInterval;
}

void Q3DSSurfaceViewer::setUpdateInterval(int interval)
{
    d_ptr->setUpdateInterval(interval);
}

bool Q3DSSurfaceViewer::isRunning() const
{
    return d_ptr->m_viewerApp != nullptr;
}

QString Q3DSSurfaceViewer::presentationId() const
{
    return d_ptr->m_id;
}

int Q3DSSurfaceViewer::fboId() const
{
    return d_ptr->m_fboId;
}

QSurface *Q3DSSurfaceViewer::surface() const
{
    return d_ptr->m_surface;
}

QOpenGLContext *Q3DSSurfaceViewer::context() const
{
    return d_ptr->m_context;
}

Q3DSViewerSettings *Q3DSSurfaceViewer::settings() const
{
    return d_ptr->m_settings;
}

Q3DSPresentation *Q3DSSurfaceViewer::presentation() const
{
    return d_ptr->m_presentation;
}

void Q3DSSurfaceViewer::setPresentationId(const QString &id)
{
    if (d_ptr->m_id != id) {
        d_ptr->m_id = id;
        Q_EMIT presentationIdChanged(id);
        if (d_ptr->m_viewerApp)
            d_ptr->m_viewerApp->setPresentationId(id);
    }
}

Q3DSSurfaceViewerPrivate::Q3DSSurfaceViewerPrivate(Q3DSSurfaceViewer *q)
    : QObject(q)
    , q_ptr(q)
    , m_viewerApp(nullptr)
    , m_timer(nullptr)
    , m_updateInterval(-1)
    , m_pixelRatio(1.0)
    , m_fboId(0)
    , m_surface(nullptr)
    , m_context(nullptr)
    , m_autoSize(true)
    , m_settings(new Q3DSViewerSettings(this))
    , m_presentation(new Q3DSPresentation(this))
{
    m_startupTimer.start();
    connect(m_presentation, &Q3DSPresentation::sourceChanged,
            this, &Q3DSSurfaceViewerPrivate::reset);
}

Q3DSSurfaceViewerPrivate::~Q3DSSurfaceViewerPrivate()
{
    releaseRuntime();

    delete m_timer;
}

void Q3DSSurfaceViewerPrivate::reset()
{
    if (m_viewerApp) {
        releaseRuntime();
        initializeRuntime();
    }
}

void Q3DSSurfaceViewerPrivate::setSize(const QSize &size)
{
    if (m_size != size) {
        m_size = size;

        if (m_viewerApp) {
            m_context->makeCurrent(m_surface);
            m_viewerApp->Resize(int(m_size.width() * m_pixelRatio),
                                int(m_size.height() * m_pixelRatio));
        }

        Q_EMIT q_ptr->sizeChanged(m_size);
    }
}

void Q3DSSurfaceViewerPrivate::setUpdateInterval(int interval)
{
    if (m_updateInterval != interval) {
        m_updateInterval = interval;
        resetUpdateTimer();
        Q_EMIT q_ptr->updateIntervalChanged(m_updateInterval);
    }
}

bool Q3DSSurfaceViewerPrivate::initialize(QSurface *surface, QOpenGLContext *context, GLuint fboId)
{
    Q_ASSERT(context);
    Q_ASSERT(surface);

    if (m_presentation->source().isEmpty()) {
        qWarning("Failed to initialize Q3DSSurfaceViewer,"
                 " presentation source must be set before calling initialize()");
        return false;
    }

    QFileInfo info(Q3DSUtils::urlToLocalFileOrQrc(m_presentation->source()));
    if (!info.exists()) {
        qWarning() << "Failed to initialize Q3DSSurfaceViewer, the presentation doesn't exist:"
                   << m_presentation->source().toString();
        return false;
    }

    shutdown();

    m_surface = surface;
    m_context = context;
    m_fboId = fboId;
    if (m_surface->surfaceClass() == QSurface::Window && fboId == 0)
        m_pixelRatio = static_cast<QWindow *>(m_surface)->devicePixelRatio();

    surfaceObject()->installEventFilter(this);

    connect(context, &QOpenGLContext::aboutToBeDestroyed, this, &Q3DSSurfaceViewerPrivate::shutdown);

    bool success = initializeRuntime();

    if (success)
        Q_EMIT q_ptr->runningChanged(true);

    return success;
}


void Q3DSSurfaceViewerPrivate::shutdown()
{
    bool oldInitialized = (m_viewerApp != nullptr);

    if (m_context) {
        disconnect(m_context, &QOpenGLContext::aboutToBeDestroyed,
                   this, &Q3DSSurfaceViewerPrivate::shutdown);
    }

    if (m_surface)
        surfaceObject()->removeEventFilter(this);

    releaseRuntime();

    m_surface = nullptr;
    m_context = nullptr;
    m_fboId = 0;

    if (oldInitialized)
        Q_EMIT q_ptr->runningChanged(false);
}


void Q3DSSurfaceViewerPrivate::update()
{
    if (m_viewerApp && m_viewerApp->IsInitialised()) {
        if (m_surface->surfaceClass() != QSurface::Window
                || static_cast<QWindow *>(m_surface)->isExposed()) {
            m_context->makeCurrent(m_surface);
            if (m_autoSize)
                setSize(m_surface->size());
            m_viewerApp->Render();

            if (m_fboId == 0)
                m_context->swapBuffers(m_surface);

            Q_EMIT q_ptr->frameUpdate();
        }
    }
}

extern Q_GUI_EXPORT QImage qt_gl_read_framebuffer(const QSize &size, bool alpha_format,
                                                  bool include_alpha);

QImage Q3DSSurfaceViewerPrivate::grab(const QRect &rect)
{
    QRect captureRect;
    QSize fullSize = m_size * m_pixelRatio;
    if (rect.isValid()) {
        captureRect = QRect(rect.x() * m_pixelRatio, rect.y() * m_pixelRatio,
                            rect.width() * m_pixelRatio, rect.height() * m_pixelRatio);
    } else {
        captureRect = QRect(0, 0, fullSize.width(), fullSize.height());
    }
    QImage image(captureRect.size(), QImage::Format_ARGB32);

    if (m_surface && m_context && m_viewerApp && m_viewerApp->IsInitialised()
            && (m_surface->surfaceClass() != QSurface::Window
                || static_cast<QWindow *>(m_surface)->isExposed())) {
        m_context->makeCurrent(m_surface);

        // Render now to ensure the image is up to date and will actually exist in case
        // the surface has a non-preserved swap buffer
        if (m_autoSize)
            setSize(m_surface->size());
        m_viewerApp->Render();

        m_context->functions()->glBindFramebuffer(GL_FRAMEBUFFER, m_fboId);
        QImage fullGrab = qt_gl_read_framebuffer(fullSize, false, false);

        // Also update the screen to match the grab, since we just rendered
        if (m_fboId == 0)
            m_context->swapBuffers(m_surface);

        if (captureRect.size() == fullSize)
            image = fullGrab;
        else
            image = fullGrab.copy(captureRect);
    }

    return image;
}

QQmlEngine *Q3DSSurfaceViewer::qmlEngine() const
{
    Q_D(const Q3DSSurfaceViewer);
    return d->qmlEngine;
}

void Q3DSSurfaceViewer::setQmlEngine(QQmlEngine *qmlEngine)
{
    Q_D(Q3DSSurfaceViewer);
    d->qmlEngine = qmlEngine;
}

bool Q3DSSurfaceViewerPrivate::eventFilter(QObject *obj, QEvent *e)
{
    if (m_surface && e->type() == QEvent::PlatformSurface) {
        if (surfaceObject() == obj) {
            QPlatformSurfaceEvent *ev = static_cast<QPlatformSurfaceEvent *>(e);
            if (ev->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed)
                shutdown();
        }
    }
    return QObject::eventFilter(obj, e);
}

bool Q3DSSurfaceViewerPrivate::initializeRuntime()
{
    Q_ASSERT(!m_viewerApp);

    m_context->makeCurrent(m_surface);

    m_viewerApp = &Q3DSViewerApp::Create(m_context, new Qt3DSAudioPlayerImpl(), &m_startupTimer);
    connect(m_viewerApp, &Q3DSViewerApp::SigPresentationReady,
            this->q_ptr, &Q3DSSurfaceViewer::presentationReady);
    connect(m_viewerApp, &Q3DSViewerApp::SigPresentationLoaded,
            this->q_ptr, &Q3DSSurfaceViewer::presentationLoaded);
    Q_ASSERT(m_viewerApp);

    const QString localSource = Q3DSUtils::urlToLocalFileOrQrc(m_presentation->source());

    if (m_autoSize)
        m_size = m_surface->size();

    if (nullptr != qmlEngine)
        m_presentation->d_ptr->streamProxy()->setEngine(qmlEngine);

    if (!m_viewerApp->InitializeApp(int(m_size.width() * m_pixelRatio),
                                    int(m_size.height() * m_pixelRatio),
                                    m_context->format(), m_fboId, localSource,
                                    m_presentation->variantList(),
                                    m_presentation->delayedLoading(),
                                    m_presentation->d_ptr->streamProxy())) {
        releaseRuntime();
        qWarning("Failed to initialize runtime");
        return false;
    }

    if (!m_id.isEmpty())
        m_viewerApp->setPresentationId(m_id);
    m_settings->d_ptr->setViewerApp(m_viewerApp);
    m_presentation->d_ptr->setViewerApp(m_viewerApp);

    resetUpdateTimer();

    return true;
}

void Q3DSSurfaceViewerPrivate::releaseRuntime()
{
    m_settings->d_ptr->setViewerApp(nullptr);
    m_presentation->d_ptr->setViewerApp(nullptr);

    if (m_context && m_surface)
        m_context->makeCurrent(m_surface);

    if (m_viewerApp) {
        m_viewerApp->Release();
        m_viewerApp = nullptr;
    }

    resetUpdateTimer();
}

void Q3DSSurfaceViewerPrivate::resetUpdateTimer()
{
    if (m_viewerApp && m_updateInterval >= 0) {
        if (!m_timer) {
            m_timer = new QTimer();
            connect(m_timer, &QTimer::timeout, this, &Q3DSSurfaceViewerPrivate::update);
        }
        m_timer->start(m_updateInterval);
    } else if (m_timer) {
        m_timer->stop();
    }
}

QObject *Q3DSSurfaceViewerPrivate::surfaceObject()
{
    if (m_surface) {
        if (m_surface->surfaceClass() == QSurface::Window)
            return static_cast<QWindow *>(m_surface);
        else
            return static_cast<QOffscreenSurface *>(m_surface);
    }
    return nullptr;
}

QT_END_NAMESPACE