source: trunk/tools/qttracereplay/main.cpp@ 618

Last change on this file since 618 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 5.8 KB
RevLine 
[556]1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include <QtDebug>
44
45#include <private/qpaintengineex_p.h>
46#include <private/qpaintbuffer_p.h>
47
48class ReplayWidget : public QWidget
49{
50 Q_OBJECT
51public:
52 ReplayWidget(const QString &filename);
53
54 void paintEvent(QPaintEvent *event);
55
56public slots:
57 void updateRect();
58
59public:
60 QList<QRegion> updates;
61 QPaintBuffer buffer;
62
63 int currentFrame;
64 int currentIteration;
65 QTime timer;
66
67 QList<uint> iterationTimes;
68 QString filename;
69};
70
71void ReplayWidget::updateRect()
72{
73 if (!updates.isEmpty())
74 update(updates.at(currentFrame));
75}
76
77void ReplayWidget::paintEvent(QPaintEvent *)
78{
79 QPainter p(this);
80
81// p.setClipRegion(frames.at(currentFrame).updateRegion);
82
83 buffer.draw(&p, currentFrame);
84
85 ++currentFrame;
86 if (currentFrame >= buffer.numFrames()) {
87 currentFrame = 0;
88 ++currentIteration;
89
90 if (currentIteration == 3)
91 timer.start();
92 else if (currentIteration > 3) {
93 iterationTimes << timer.elapsed();
94 timer.restart();
95
96 if (iterationTimes.size() >= 3) {
97 qreal mean = 0;
98 qreal stddev = 0;
99 uint min = INT_MAX;
100
101 for (int i = 0; i < iterationTimes.size(); ++i) {
102 mean += iterationTimes.at(i);
103 min = qMin(min, iterationTimes.at(i));
104 }
105
106 mean /= qreal(iterationTimes.size());
107
108 for (int i = 0; i < iterationTimes.size(); ++i) {
109 qreal delta = iterationTimes.at(i) - mean;
110 stddev += delta * delta;
111 }
112
113 stddev = qSqrt(stddev / iterationTimes.size());
114
115 qSort(iterationTimes.begin(), iterationTimes.end());
116 uint median = iterationTimes.at(iterationTimes.size() / 2);
117
118 stddev = 100 * stddev / mean;
119
120 if (iterationTimes.size() >= 10 || stddev < 4) {
121 printf("%s, iterations: %d, frames: %d, min(ms): %d, median(ms): %d, stddev: %f %%, max(fps): %f\n", qPrintable(filename),
122 iterationTimes.size(), updates.size(), min, median, stddev, 1000. * updates.size() / min);
123 deleteLater();
124 return;
125 }
126 }
127 }
128 }
129
130 QTimer::singleShot(0, this, SLOT(updateRect()));
131}
132
133ReplayWidget::ReplayWidget(const QString &filename_)
134 : currentFrame(0)
135 , currentIteration(0)
136 , filename(filename_)
137{
138 setWindowTitle(filename);
139 QFile file(filename);
140
141 QRect bounds;
142 if (!file.open(QIODevice::ReadOnly)) {
143 printf("Failed to load input file '%s'\n", qPrintable(filename_));
144 return;
145 }
146
147 QDataStream in(&file);
148
149 char *data;
150 uint size;
151 in.readBytes(data, size);
152 bool isTraceFile = size == 7 && qstrncmp(data, "qttrace", 7) == 0;
153 delete [] data;
154 if (!isTraceFile) {
155 printf("File '%s' is not a trace file\n", qPrintable(filename_));
156 return;
157 }
158
159 in >> buffer >> updates;
160 printf("Read paint buffer with %d frames\n", buffer.numFrames());
161
162 resize(buffer.boundingRect().size().toSize());
163
164 setAutoFillBackground(false);
165 setAttribute(Qt::WA_NoSystemBackground);
166
167 QTimer::singleShot(10, this, SLOT(updateRect()));
168}
169
170int main(int argc, char **argv)
171{
172 QApplication app(argc, argv);
173
174 if (argc <= 1 || qstrcmp(argv[1], "-h") == 0 || qstrcmp(argv[1], "--help") == 0) {
175 printf("Replays a tracefile generated with '-graphicssystem trace'\n");
176 printf("Usage:\n > %s [traceFile]\n", argv[0]);
177 return 1;
178 }
179
180 QFile file(argv[1]);
181 if (!file.exists()) {
182 printf("%s does not exist\n", argv[1]);
183 return 1;
184 }
185
186 ReplayWidget *widget = new ReplayWidget(argv[1]);
187
188 if (!widget->updates.isEmpty()) {
189 widget->show();
190 return app.exec();
191 }
192
193}
194#include "main.moc"
Note: See TracBrowser for help on using the repository browser.