| 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 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | // instance will be set, unset in constructor. Allows static methods to be inlined. |
| 14 | QQuickProfiler *QQuickProfiler::s_instance = nullptr; |
| 15 | quint64 QQuickProfiler::featuresEnabled = 0; |
| 16 | |
| 17 | void QQuickProfiler::initialize(QObject *parent) |
| 18 | { |
| 19 | Q_ASSERT(s_instance == nullptr); |
| 20 | s_instance = new QQuickProfiler(parent); |
| 21 | } |
| 22 | |
| 23 | void 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 | |
| 30 | void QQuickProfiler::registerAnimationCallback() |
| 31 | { |
| 32 | QUnifiedTimer::instance()->registerProfilerCallback(cb: &animationTimerCallback); |
| 33 | } |
| 34 | |
| 35 | class CallbackRegistrationHelper : public QObject { |
| 36 | Q_OBJECT |
| 37 | public: |
| 38 | void registerAnimationTimerCallback() |
| 39 | { |
| 40 | QQuickProfiler::registerAnimationCallback(); |
| 41 | delete this; |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | QQuickProfiler::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 | |
| 59 | QQuickProfiler::~QQuickProfiler() |
| 60 | { |
| 61 | QMutexLocker lock(&m_dataMutex); |
| 62 | featuresEnabled = 0; |
| 63 | s_instance = nullptr; |
| 64 | } |
| 65 | |
| 66 | void QQuickProfiler::startProfilingImpl(quint64 features) |
| 67 | { |
| 68 | QMutexLocker lock(&m_dataMutex); |
| 69 | featuresEnabled = features; |
| 70 | } |
| 71 | |
| 72 | void QQuickProfiler::stopProfilingImpl() |
| 73 | { |
| 74 | QMutexLocker lock(&m_dataMutex); |
| 75 | featuresEnabled = 0; |
| 76 | emit dataReady(data: m_data); |
| 77 | m_data.clear(); |
| 78 | } |
| 79 | |
| 80 | void QQuickProfiler::reportDataImpl() |
| 81 | { |
| 82 | QMutexLocker lock(&m_dataMutex); |
| 83 | emit dataReady(data: m_data); |
| 84 | m_data.clear(); |
| 85 | } |
| 86 | |
| 87 | void QQuickProfiler::setTimer(const QElapsedTimer &t) |
| 88 | { |
| 89 | QMutexLocker lock(&m_dataMutex); |
| 90 | m_timer = t; |
| 91 | } |
| 92 | |
| 93 | QT_END_NAMESPACE |
| 94 | |
| 95 | #include "qquickprofiler.moc" |
| 96 | #include "moc_qquickprofiler_p.cpp" |
| 97 |
