| 1 |
// Copyright © 2016 Canonical Ltd. |
| 2 |
// Author: Loïc Molinari <[email protected]> |
| 3 |
// |
| 4 |
// This file is part of Lomiri UI Toolkit. |
| 5 |
// |
| 6 |
// Lomiri UI Toolkit is free software: you can redistribute it and/or modify it |
| 7 |
// under the terms of the GNU Lesser General Public License as published by the |
| 8 |
// Free Software Foundation; version 3. |
| 9 |
// |
| 10 |
// Lomiri UI Toolkit is distributed in the hope that it will be useful, but |
| 11 |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 13 |
// for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with Lomiri UI Toolkit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
#ifndef APPLICATIONMONITOR_P_H |
| 19 |
#define APPLICATIONMONITOR_P_H |
| 20 |
|
| 21 |
#include <QtGlobal> |
| 22 |
|
| 23 |
#include <QtCore/QTimer> |
| 24 |
#include <QtCore/QThread> |
| 25 |
#include <QtCore/QMutex> |
| 26 |
#include <QtCore/QWaitCondition> |
| 27 |
#include <QtCore/QElapsedTimer> |
| 28 |
#include <QtCore/QRunnable> |
| 29 |
#include <QtCore/QAtomicInteger> |
| 30 |
#include <QtGui/QGuiApplication> |
| 31 |
|
| 32 |
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
| 33 |
#include <LomiriMetrics-Qt6/applicationmonitor.h> |
| 34 |
#include <LomiriMetrics-Qt6/private/overlay_p.h> |
| 35 |
#include <LomiriMetrics-Qt6/private/gputimer_p.h> |
| 36 |
#include <LomiriMetrics-Qt6/private/lomirimetricsglobal_p.h> |
| 37 |
#else |
| 38 |
#include <LomiriMetrics/applicationmonitor.h> |
| 39 |
#include <LomiriMetrics/private/overlay_p.h> |
| 40 |
#include <LomiriMetrics/private/gputimer_p.h> |
| 41 |
#include <LomiriMetrics/private/lomirimetricsglobal_p.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
class LoggingThread; |
| 45 |
class WindowMonitor; |
| 46 |
class QQuickWindow; |
| 47 |
|
| 48 |
class LOMIRI_METRICS_PRIVATE_EXPORT UMApplicationMonitorPrivate |
| 49 |
{ |
| 50 |
public: |
| 51 |
static const int maxMonitors = 16; |
| 52 |
static const int maxLoggers = 8; |
| 53 |
|
| 54 |
static inline UMApplicationMonitorPrivate* get(UMApplicationMonitor* applicationMonitor) { |
| 55 |
return applicationMonitor->d_func(); |
| 56 |
} |
| 57 |
|
| 58 |
enum { |
| 59 |
// Lower bit allowed is (1 << 8). |
| 60 |
Overlay = (1 << 8), |
| 61 |
Logging = (1 << 9), |
| 62 |
Started = (1 << 10), |
| 63 |
ClosingDown = (1 << 11), |
| 64 |
// Higher bit allowed is (1 << 15). |
| 65 |
FilterMask = 0x000000ff, |
| 66 |
ApplicationMonitorMask = 0x0000ff00, |
| 67 |
WindowMonitorMask = 0xffff0000 |
| 68 |
}; |
| 69 |
|
| 70 |
UMApplicationMonitorPrivate(UMApplicationMonitor* applicationMonitor); |
| 71 |
~UMApplicationMonitorPrivate(); |
| 72 |
|
| 73 |
void startMonitoring(QQuickWindow* window); |
| 74 |
void start(); |
| 75 |
bool removeMonitor(WindowMonitor* monitor); |
| 76 |
void stopMonitoring(WindowMonitor* monitor); |
| 77 |
void stop(); |
| 78 |
bool hasMonitor(WindowMonitor* monitor); |
| 79 |
void setMonitoringFlags(quint32 flags); |
| 80 |
void processTimeout(); |
| 81 |
|
| 82 |
UMApplicationMonitor* const q_ptr; |
| 83 |
Q_DECLARE_PUBLIC(UMApplicationMonitor) |
| 84 |
|
| 85 |
WindowMonitor* m_monitors[maxMonitors]; |
| 86 |
UMLogger* m_loggers[maxLoggers]; |
| 87 |
LoggingThread* m_loggingThread; |
| 88 |
#if !defined(QT_NO_DEBUG) |
| 89 |
QGuiApplication* m_application; |
| 90 |
#endif |
| 91 |
UMEventUtils m_eventUtils; |
| 92 |
QTimer m_processTimer; |
| 93 |
QMutex m_monitorsMutex; |
| 94 |
int m_monitorCount; |
| 95 |
int m_loggerCount; |
| 96 |
int m_updateInterval[UMEvent::TypeCount]; |
| 97 |
quint32 m_flags; |
| 98 |
alignas(64) UMEvent m_processEvent; |
| 99 |
}; |
| 100 |
|
| 101 |
class LOMIRI_METRICS_PRIVATE_EXPORT LoggingThread : public QThread |
| 102 |
{ |
| 103 |
public: |
| 104 |
LoggingThread(); |
| 105 |
|
| 106 |
void run() override; |
| 107 |
void push(const UMEvent* event); |
| 108 |
void setLoggers(UMLogger** loggers, int count); |
| 109 |
LoggingThread* ref(); |
| 110 |
void deref(); |
| 111 |
|
| 112 |
private: |
| 113 |
enum { |
| 114 |
Waiting = (1 << 0), |
| 115 |
JoinRequested = (1 << 1) |
| 116 |
}; |
| 117 |
|
| 118 |
~LoggingThread(); |
| 119 |
|
| 120 |
UMEvent* m_queue; |
| 121 |
UMLogger* m_loggers[UMApplicationMonitorPrivate::maxLoggers]; |
| 122 |
int m_loggerCount; |
| 123 |
QMutex m_mutex; |
| 124 |
QWaitCondition m_condition; |
| 125 |
QAtomicInteger<quint32> m_refCount; |
| 126 |
qint8 m_queueIndex; |
| 127 |
qint8 m_queueSize; |
| 128 |
quint8 m_flags; |
| 129 |
}; |
| 130 |
|
| 131 |
class LOMIRI_METRICS_PRIVATE_EXPORT WindowMonitorDeleter : public QRunnable |
| 132 |
{ |
| 133 |
public: |
| 134 |
WindowMonitorDeleter(UMApplicationMonitor* applicationMonitor, WindowMonitor* monitor) |
| 135 |
: m_applicationMonitor(applicationMonitor) |
| 136 |
, m_monitor(monitor) { |
| 137 |
DASSERT(applicationMonitor == UMApplicationMonitor::instance()); |
| 138 |
DASSERT(m_applicationMonitor); |
| 139 |
DASSERT(monitor); |
| 140 |
} |
| 141 |
~WindowMonitorDeleter(); |
| 142 |
|
| 143 |
void run() override; |
| 144 |
|
| 145 |
private: |
| 146 |
UMApplicationMonitor* m_applicationMonitor; |
| 147 |
WindowMonitor* m_monitor; |
| 148 |
}; |
| 149 |
|
| 150 |
class LOMIRI_METRICS_PRIVATE_EXPORT WindowMonitorFlagSetter : public QRunnable |
| 151 |
{ |
| 152 |
public: |
| 153 |
WindowMonitorFlagSetter(WindowMonitor* monitor, quint32 flags) |
| 154 |
: m_applicationMonitor(UMApplicationMonitor::instance()) |
| 155 |
, m_monitor(monitor) |
| 156 |
, m_flags(flags) { |
| 157 |
DASSERT(m_applicationMonitor); |
| 158 |
DASSERT(monitor); |
| 159 |
} |
| 160 |
~WindowMonitorFlagSetter(); |
| 161 |
|
| 162 |
void run() override {} |
| 163 |
|
| 164 |
private: |
| 165 |
UMApplicationMonitor* m_applicationMonitor; |
| 166 |
WindowMonitor* m_monitor; |
| 167 |
quint32 m_flags; |
| 168 |
}; |
| 169 |
|
| 170 |
class LOMIRI_METRICS_PRIVATE_EXPORT WindowMonitor : public QObject |
| 171 |
{ |
| 172 |
Q_OBJECT |
| 173 |
|
| 174 |
public: |
| 175 |
WindowMonitor( |
| 176 |
UMApplicationMonitor* applicationMonitor, QQuickWindow* window, |
| 177 |
LoggingThread* loggingThread, quint32 flags, quint32 id); |
| 178 |
~WindowMonitor(); |
| 179 |
|
| 180 |
QQuickWindow* window() const { return m_window; } |
| 181 |
void setProcessEvent(const UMEvent& event); |
| 182 |
|
| 183 |
private Q_SLOTS: |
| 184 |
void windowSceneGraphInitialized(); |
| 185 |
void windowSceneGraphInvalidated(); |
| 186 |
void windowBeforeSynchronizing(); |
| 187 |
void windowAfterSynchronizing(); |
| 188 |
void windowBeforeRendering(); |
| 189 |
void windowAfterRendering(); |
| 190 |
void windowFrameSwapped(); |
| 191 |
void windowSceneGraphAboutToStop(); |
| 192 |
|
| 193 |
private: |
| 194 |
enum { |
| 195 |
// Lower bit allowed is (1 << 16). |
| 196 |
GpuResourcesInitialized = (1 << 16), |
| 197 |
GpuTimerAvailable = (1 << 17), |
| 198 |
SizeChanged = (1 << 18) |
| 199 |
// Higher bit allowed is (1 << 31). |
| 200 |
}; |
| 201 |
|
| 202 |
bool gpuResourcesInitialized() const { return m_flags & GpuResourcesInitialized; } |
| 203 |
void setFlags(quint32 flags) { |
| 204 |
m_flags = (m_flags & UMApplicationMonitorPrivate::WindowMonitorMask) | flags; |
| 205 |
} |
| 206 |
void initializeGpuResources(); |
| 207 |
void finalizeGpuResources(); |
| 208 |
|
| 209 |
UMApplicationMonitor* m_applicationMonitor; |
| 210 |
LoggingThread* m_loggingThread; |
| 211 |
QQuickWindow* m_window; |
| 212 |
GPUTimer m_gpuTimer; |
| 213 |
Overlay m_overlay; // Accessed from different threads (needs locking). |
| 214 |
QMutex m_mutex; |
| 215 |
QElapsedTimer m_sceneGraphTimer; |
| 216 |
QElapsedTimer m_deltaTimer; |
| 217 |
quint32 m_id; |
| 218 |
quint32 m_flags; |
| 219 |
QSize m_frameSize; |
| 220 |
UMEvent m_frameEvent; |
| 221 |
|
| 222 |
friend class WindowMonitorDeleter; |
| 223 |
friend class WindowMonitorFlagSetter; |
| 224 |
}; |
| 225 |
|
| 226 |
#endif // APPLICATIONMONITOR_P_H |