a9cdfac6b757ecd9371c4ca98ab823e5007fb37d
[profile/ivi/qtdeclarative.git] / src / declarative / debugger / qdeclarativedebugtrace.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativedebugtrace_p.h"
43
44 #include <QtCore/qdatastream.h>
45 #include <QtCore/qurl.h>
46 #include <QtCore/qtimer.h>
47 #include <QtCore/qthread.h>
48 #include <QtCore/qcoreapplication.h>
49
50 // this contains QUnifiedTimer
51 #include <private/qabstractanimation_p.h>
52
53 QT_BEGIN_NAMESPACE
54
55 Q_GLOBAL_STATIC(QDeclarativeDebugTrace, traceInstance);
56
57 // convert to a QByteArray that can be sent to the debug client
58 // use of QDataStream can skew results if m_deferredSend == false
59 //     (see tst_qdeclarativedebugtrace::trace() benchmark)
60 QByteArray QDeclarativeDebugData::toByteArray() const
61 {
62     QByteArray data;
63     //### using QDataStream is relatively expensive
64     QDataStream ds(&data, QIODevice::WriteOnly);
65     ds << time << messageType << detailType;
66     if (messageType == (int)QDeclarativeDebugTrace::RangeData)
67         ds << detailData;
68     if (messageType == (int)QDeclarativeDebugTrace::RangeLocation)
69         ds << detailData << line;
70     if (messageType == (int)QDeclarativeDebugTrace::Event &&
71             detailType == (int)QDeclarativeDebugTrace::AnimationFrame)
72         ds << framerate << animationcount;
73     return data;
74 }
75
76 QDeclarativeDebugTrace::QDeclarativeDebugTrace()
77     : QDeclarativeDebugService(QLatin1String("CanvasFrameRate")),
78       m_enabled(false), m_deferredSend(true), m_messageReceived(false)
79 {
80     m_timer.start();
81     if (status() == Enabled) {
82         // wait for first message indicating whether to trace or not
83         while (!m_messageReceived)
84             waitForMessage();
85
86         QUnifiedTimer::instance()->registerProfilerCallback( &animationFrame );
87     }
88 }
89
90 QDeclarativeDebugTrace::~QDeclarativeDebugTrace()
91 {
92 }
93
94 void QDeclarativeDebugTrace::addEngine(QDeclarativeEngine * /*engine*/)
95 {
96     // just make sure that the service is properly registered
97     traceInstance();
98 }
99
100 void QDeclarativeDebugTrace::removeEngine(QDeclarativeEngine */*engine*/)
101 {
102
103 }
104
105 void QDeclarativeDebugTrace::addEvent(EventType t)
106 {
107     if (QDeclarativeDebugService::isDebuggingEnabled())
108         traceInstance()->addEventImpl(t);
109 }
110
111 void QDeclarativeDebugTrace::startRange(RangeType t)
112 {
113     if (QDeclarativeDebugService::isDebuggingEnabled())
114         traceInstance()->startRangeImpl(t);
115 }
116
117 void QDeclarativeDebugTrace::rangeData(RangeType t, const QString &data)
118 {
119     if (QDeclarativeDebugService::isDebuggingEnabled())
120         traceInstance()->rangeDataImpl(t, data);
121 }
122
123 void QDeclarativeDebugTrace::rangeData(RangeType t, const QUrl &data)
124 {
125     if (QDeclarativeDebugService::isDebuggingEnabled())
126         traceInstance()->rangeDataImpl(t, data);
127 }
128
129 void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QString &fileName, int line)
130 {
131     if (QDeclarativeDebugService::isDebuggingEnabled())
132         traceInstance()->rangeLocationImpl(t, fileName, line);
133 }
134
135 void QDeclarativeDebugTrace::rangeLocation(RangeType t, const QUrl &fileName, int line)
136 {
137     if (QDeclarativeDebugService::isDebuggingEnabled())
138         traceInstance()->rangeLocationImpl(t, fileName, line);
139 }
140
141 void QDeclarativeDebugTrace::endRange(RangeType t)
142 {
143     if (QDeclarativeDebugService::isDebuggingEnabled())
144         traceInstance()->endRangeImpl(t);
145 }
146
147 void QDeclarativeDebugTrace::animationFrame(qint64 delta)
148 {
149     Q_ASSERT(QDeclarativeDebugService::isDebuggingEnabled());
150     traceInstance()->animationFrameImpl(delta);
151 }
152
153 void QDeclarativeDebugTrace::addEventImpl(EventType event)
154 {
155     if (status() != Enabled || !m_enabled)
156         return;
157
158     QDeclarativeDebugData ed = {m_timer.nsecsElapsed(), (int)Event, (int)event, QString(), -1, 0, 0};
159     processMessage(ed);
160 }
161
162 void QDeclarativeDebugTrace::startRangeImpl(RangeType range)
163 {
164     if (status() != Enabled || !m_enabled)
165         return;
166
167     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeStart, (int)range, QString(), -1, 0, 0};
168     processMessage(rd);
169 }
170
171 void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QString &rData)
172 {
173     if (status() != Enabled || !m_enabled)
174         return;
175
176     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeData, (int)range, rData, -1, 0, 0};
177     processMessage(rd);
178 }
179
180 void QDeclarativeDebugTrace::rangeDataImpl(RangeType range, const QUrl &rData)
181 {
182     if (status() != Enabled || !m_enabled)
183         return;
184
185     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeData, (int)range, rData.toString(QUrl::FormattingOption(0x100)), -1, 0, 0};
186     processMessage(rd);
187 }
188
189 void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QString &fileName, int line)
190 {
191     if (status() != Enabled || !m_enabled)
192         return;
193
194     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeLocation, (int)range, fileName, line, 0, 0};
195     processMessage(rd);
196 }
197
198 void QDeclarativeDebugTrace::rangeLocationImpl(RangeType range, const QUrl &fileName, int line)
199 {
200     if (status() != Enabled || !m_enabled)
201         return;
202
203     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeLocation, (int)range, fileName.toString(QUrl::FormattingOption(0x100)), line, 0, 0};
204     processMessage(rd);
205 }
206
207 void QDeclarativeDebugTrace::endRangeImpl(RangeType range)
208 {
209     if (status() != Enabled || !m_enabled)
210         return;
211
212     QDeclarativeDebugData rd = {m_timer.nsecsElapsed(), (int)RangeEnd, (int)range, QString(), -1, 0, 0};
213     processMessage(rd);
214 }
215
216 void QDeclarativeDebugTrace::animationFrameImpl(qint64 delta)
217 {
218     if (status() != Enabled || !m_enabled)
219         return;
220
221     int animCount = QUnifiedTimer::instance()->runningAnimationCount();
222
223     if (animCount > 0 && delta > 0) {
224         // trim fps to integer
225         int fps = 1000 / delta;
226         QDeclarativeDebugData ed = {m_timer.nsecsElapsed(), (int)Event, (int)AnimationFrame, QString(), -1, fps, animCount};
227         processMessage(ed);
228     }
229 }
230
231 /*
232     Either send the message directly, or queue up
233     a list of messages to send later (via sendMessages)
234 */
235 void QDeclarativeDebugTrace::processMessage(const QDeclarativeDebugData &message)
236 {
237     QMutexLocker locker(&m_mutex);
238     if (m_deferredSend
239             || (QThread::currentThread() != QCoreApplication::instance()->thread()))
240         m_data.append(message);
241     else
242         sendMessage(message.toByteArray());
243 }
244
245 /*
246     Send the messages queued up by processMessage
247 */
248 void QDeclarativeDebugTrace::sendMessages()
249 {
250     if (m_deferredSend) {
251         QMutexLocker locker(&m_mutex);
252         //### this is a suboptimal way to send batched messages
253         for (int i = 0; i < m_data.count(); ++i)
254             sendMessage(m_data.at(i).toByteArray());
255         m_data.clear();
256
257         //indicate completion
258         QByteArray data;
259         QDataStream ds(&data, QIODevice::WriteOnly);
260         ds << (qint64)-1 << (int)Complete;
261         sendMessage(data);
262     }
263 }
264
265 void QDeclarativeDebugTrace::messageReceived(const QByteArray &message)
266 {
267     QByteArray rwData = message;
268     QDataStream stream(&rwData, QIODevice::ReadOnly);
269
270     bool enabled;
271     stream >> enabled;
272
273     m_messageReceived = true;
274
275     if (m_enabled != enabled) {
276         if (enabled) {
277             m_enabled = true;
278             addEvent(StartTrace);
279         } else {
280             addEvent(EndTrace);
281             m_enabled = false;
282             sendMessages();
283         }
284     }
285 }
286
287 QT_END_NAMESPACE