1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickprofiler_p.h"
5
6#include <QtQml/private/qqmlabstractprofileradapter_p.h>
7
8#include <QtCore/qcoreapplication.h>
9#include <QtCore/qthread.h>
10
11QT_BEGIN_NAMESPACE
12
13// instance will be set, unset in constructor. Allows static methods to be inlined.
14QQuickProfiler *QQuickProfiler::s_instance = nullptr;
15quint64 QQuickProfiler::featuresEnabled = 0;
16
17void QQuickProfiler::initialize(QObject *parent)
18{
19 Q_ASSERT(s_instance == nullptr);
20 s_instance = new QQuickProfiler(parent);
21}
22
23void animationTimerCallback(qint64 delta)
24{
25 Q_QUICK_PROFILE(QQuickProfiler::ProfileAnimations, animationFrame(delta,
26 QThread::currentThread() == QCoreApplication::instance()->thread() ?
27 QQuickProfiler::GuiThread : QQuickProfiler::RenderThread));
28}
29
30void QQuickProfiler::registerAnimationCallback()
31{
32 QUnifiedTimer::instance()->registerProfilerCallback(cb: &animationTimerCallback);
33}
34
35class CallbackRegistrationHelper : public QObject {
36 Q_OBJECT
37public:
38 void registerAnimationTimerCallback()
39 {
40 QQuickProfiler::registerAnimationCallback();
41 delete this;
42 }
43};
44
45QQuickProfiler::QQuickProfiler(QObject *parent) : QObject(parent)
46{
47 // This is safe because at this point the m_instance isn't initialized, yet.
48 m_timer.start();
49 CallbackRegistrationHelper *helper = new CallbackRegistrationHelper; // will delete itself
50 helper->moveToThread(thread: QCoreApplication::instance()->thread());
51
52 // Queue the signal to have the animation timer registration run in the right thread;
53 QObject signalSource;
54 connect(sender: &signalSource, signal: &QObject::destroyed,
55 context: helper, slot: &CallbackRegistrationHelper::registerAnimationTimerCallback,
56 type: Qt::QueuedConnection);
57}
58
59QQuickProfiler::~QQuickProfiler()
60{
61 QMutexLocker lock(&m_dataMutex);
62 featuresEnabled = 0;
63 s_instance = nullptr;
64}
65
66void QQuickProfiler::startProfilingImpl(quint64 features)
67{
68 QMutexLocker lock(&m_dataMutex);
69 featuresEnabled = features;
70}
71
72void QQuickProfiler::stopProfilingImpl()
73{
74 QMutexLocker lock(&m_dataMutex);
75 featuresEnabled = 0;
76 emit dataReady(data: m_data);
77 m_data.clear();
78}
79
80void QQuickProfiler::reportDataImpl()
81{
82 QMutexLocker lock(&m_dataMutex);
83 emit dataReady(data: m_data);
84 m_data.clear();
85}
86
87void QQuickProfiler::setTimer(const QElapsedTimer &t)
88{
89 QMutexLocker lock(&m_dataMutex);
90 m_timer = t;
91}
92
93QT_END_NAMESPACE
94
95#include "qquickprofiler.moc"
96#include "moc_qquickprofiler_p.cpp"
97

source code of qtdeclarative/src/quick/util/qquickprofiler.cpp