| 1 |
/* |
| 2 |
* Copyright 2014 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Author: Florian Boucault <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "upmrenderingtimes.h" |
| 20 |
|
| 21 |
#include <QtCore/QtMath> |
| 22 |
|
| 23 |
UPMRenderingTimes::UPMRenderingTimes(QQuickItem* parent) : |
| 24 |
QQuickItem(parent), |
| 25 |
m_period(1000), |
| 26 |
m_graphModel(new UPMGraphModel(this)), |
| 27 |
m_timerType(RenderTimer::Automatic), |
| 28 |
m_needsNewTimer(true), |
| 29 |
m_window(NULL), |
| 30 |
m_oddFrame(false), |
| 31 |
m_oddFrameRenderTime(0) |
| 32 |
{ |
| 33 |
/* Forward samplesChanged signal from graphModel */ |
| 34 |
QObject::connect(m_graphModel, &UPMGraphModel::samplesChanged, |
| 35 |
this, &UPMRenderingTimes::samplesChanged); |
| 36 |
|
| 37 |
/* Periodically append render time of the most costly frame rendered. |
| 38 |
The period is period / samples */ |
| 39 |
QObject::connect(this, &UPMRenderingTimes::frameRendered, |
| 40 |
this, &UPMRenderingTimes::onFrameRendered); |
| 41 |
} |
| 42 |
|
| 43 |
int UPMRenderingTimes::period() const |
| 44 |
{ |
| 45 |
return m_period; |
| 46 |
} |
| 47 |
|
| 48 |
void UPMRenderingTimes::setPeriod(int period) |
| 49 |
{ |
| 50 |
if (period != m_period) { |
| 51 |
m_period = period; |
| 52 |
Q_EMIT periodChanged(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
int UPMRenderingTimes::samples() const |
| 57 |
{ |
| 58 |
return m_graphModel->samples(); |
| 59 |
} |
| 60 |
|
| 61 |
void UPMRenderingTimes::setSamples(int samples) |
| 62 |
{ |
| 63 |
m_graphModel->setSamples(samples); |
| 64 |
} |
| 65 |
|
| 66 |
UPMGraphModel* UPMRenderingTimes::graphModel() const |
| 67 |
{ |
| 68 |
return m_graphModel; |
| 69 |
} |
| 70 |
|
| 71 |
RenderTimer::TimerType UPMRenderingTimes::timerType() const |
| 72 |
{ |
| 73 |
return m_timerType; |
| 74 |
} |
| 75 |
|
| 76 |
void UPMRenderingTimes::setTimerType(RenderTimer::TimerType timerType) |
| 77 |
{ |
| 78 |
if (timerType != m_timerType) { |
| 79 |
m_timerType = timerType; |
| 80 |
// setup/teardown of RenderTimers need to happen in the rendering thread |
| 81 |
m_needsNewTimer = true; |
| 82 |
Q_EMIT timerTypeChanged(); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// FIXME: can be replaced with connecting to windowChanged() signal introduced in Qt5.2 |
| 87 |
void UPMRenderingTimes::itemChange(ItemChange change, const ItemChangeData & value) |
| 88 |
{ |
| 89 |
if (change == QQuickItem::ItemSceneChange) { |
| 90 |
connectToWindow(value.window); |
| 91 |
} |
| 92 |
QQuickItem::itemChange(change, value); |
| 93 |
} |
| 94 |
|
| 95 |
void UPMRenderingTimes::connectToWindow(QQuickWindow* window) |
| 96 |
{ |
| 97 |
if (window != m_window) { |
| 98 |
if (m_window != NULL) { |
| 99 |
QObject::disconnect(m_window, &QQuickWindow::sceneGraphInitialized, |
| 100 |
this, &UPMRenderingTimes::onSceneGraphInitialized); |
| 101 |
QObject::disconnect(m_window, &QQuickWindow::sceneGraphInvalidated, |
| 102 |
this, &UPMRenderingTimes::onSceneGraphInvalidated); |
| 103 |
QObject::disconnect(m_window, &QQuickWindow::beforeRendering, |
| 104 |
this, &UPMRenderingTimes::onBeforeRendering); |
| 105 |
QObject::disconnect(m_window, &QQuickWindow::afterRendering, |
| 106 |
this, &UPMRenderingTimes::onAfterRendering); |
| 107 |
QObject::disconnect(m_window, &QQuickWindow::frameSwapped, |
| 108 |
this, &UPMRenderingTimes::onFrameSwapped); |
| 109 |
} |
| 110 |
|
| 111 |
m_window = window; |
| 112 |
|
| 113 |
if (m_window != NULL) { |
| 114 |
QObject::connect(m_window, &QQuickWindow::sceneGraphInitialized, |
| 115 |
this, &UPMRenderingTimes::onSceneGraphInitialized, |
| 116 |
Qt::DirectConnection); |
| 117 |
QObject::connect(m_window, &QQuickWindow::sceneGraphInvalidated, |
| 118 |
this, &UPMRenderingTimes::onSceneGraphInvalidated, |
| 119 |
Qt::DirectConnection); |
| 120 |
QObject::connect(m_window, &QQuickWindow::beforeRendering, |
| 121 |
this, &UPMRenderingTimes::onBeforeRendering, Qt::DirectConnection); |
| 122 |
QObject::connect(m_window, &QQuickWindow::afterRendering, |
| 123 |
this, &UPMRenderingTimes::onAfterRendering, Qt::DirectConnection); |
| 124 |
QObject::connect(m_window, &QQuickWindow::frameSwapped, |
| 125 |
this, &UPMRenderingTimes::onFrameSwapped, Qt::DirectConnection); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void UPMRenderingTimes::onSceneGraphInitialized() |
| 131 |
{ |
| 132 |
m_renderingTimer.setup(m_timerType); |
| 133 |
} |
| 134 |
|
| 135 |
void UPMRenderingTimes::onSceneGraphInvalidated() |
| 136 |
{ |
| 137 |
m_renderingTimer.teardown(); |
| 138 |
} |
| 139 |
|
| 140 |
void UPMRenderingTimes::onBeforeRendering() |
| 141 |
{ |
| 142 |
if (m_needsNewTimer) { |
| 143 |
m_renderingTimer.teardown(); |
| 144 |
m_renderingTimer.setup(m_timerType); |
| 145 |
m_needsNewTimer = false; |
| 146 |
} |
| 147 |
m_renderingTimer.start(); |
| 148 |
} |
| 149 |
|
| 150 |
void UPMRenderingTimes::onAfterRendering() |
| 151 |
{ |
| 152 |
Q_EMIT frameRendered(m_renderingTimer.stop()); |
| 153 |
} |
| 154 |
|
| 155 |
void UPMRenderingTimes::onFrameSwapped() |
| 156 |
{ |
| 157 |
} |
| 158 |
|
| 159 |
void UPMRenderingTimes::onFrameRendered(qint64 renderTime) |
| 160 |
{ |
| 161 |
if (!m_oddFrame) { |
| 162 |
appendRenderTime(m_oddFrameRenderTime); |
| 163 |
appendRenderTime(renderTime); |
| 164 |
} else { |
| 165 |
m_oddFrameRenderTime = renderTime; |
| 166 |
} |
| 167 |
m_oddFrame = !m_oddFrame; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
void UPMRenderingTimes::appendRenderTime(qint64 renderTime) |
| 172 |
{ |
| 173 |
int renderTimeInMs = qCeil((qreal)renderTime / 1000000); |
| 174 |
int width = ((qreal)samples() / m_period) * renderTimeInMs; |
| 175 |
m_graphModel->appendValue(width, renderTimeInMs); |
| 176 |
} |