tools/qttracereplay/main.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 tools applications of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 
       
    48 class ReplayWidget : public QWidget
       
    49 {
       
    50     Q_OBJECT
       
    51 public:
       
    52     ReplayWidget(const QString &filename);
       
    53 
       
    54     void paintEvent(QPaintEvent *event);
       
    55 
       
    56 public slots:
       
    57     void updateRect();
       
    58 
       
    59 private:
       
    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 
       
    71 void ReplayWidget::updateRect()
       
    72 {
       
    73     update(updates.at(currentFrame));
       
    74 }
       
    75 
       
    76 void ReplayWidget::paintEvent(QPaintEvent *)
       
    77 {
       
    78     QPainter p(this);
       
    79 
       
    80 //    p.setClipRegion(frames.at(currentFrame).updateRegion);
       
    81 
       
    82     buffer.draw(&p, currentFrame);
       
    83 
       
    84     ++currentFrame;
       
    85     if (currentFrame >= buffer.numFrames()) {
       
    86         currentFrame = 0;
       
    87         ++currentIteration;
       
    88 
       
    89         if (currentIteration == 3)
       
    90             timer.start();
       
    91         else if (currentIteration > 3) {
       
    92             iterationTimes << timer.elapsed();
       
    93             timer.restart();
       
    94 
       
    95             if (iterationTimes.size() >= 3) {
       
    96                 qreal mean = 0;
       
    97                 qreal stddev = 0;
       
    98                 uint min = INT_MAX;
       
    99 
       
   100                 for (int i = 0; i < iterationTimes.size(); ++i) {
       
   101                     mean += iterationTimes.at(i);
       
   102                     min = qMin(min, iterationTimes.at(i));
       
   103                 }
       
   104 
       
   105                 mean /= qreal(iterationTimes.size());
       
   106 
       
   107                 for (int i = 0; i < iterationTimes.size(); ++i) {
       
   108                     qreal delta = iterationTimes.at(i) - mean;
       
   109                     stddev += delta * delta;
       
   110                 }
       
   111 
       
   112                 stddev = qSqrt(stddev / iterationTimes.size());
       
   113 
       
   114                 qSort(iterationTimes.begin(), iterationTimes.end());
       
   115                 uint median = iterationTimes.at(iterationTimes.size() / 2);
       
   116 
       
   117                 stddev = 100 * stddev / mean;
       
   118 
       
   119                 if (iterationTimes.size() >= 10 || stddev < 4) {
       
   120                     printf("%s, iterations: %d, frames: %d, min(ms): %d, median(ms): %d, stddev: %f %%, max(fps): %f\n", qPrintable(filename),
       
   121                             iterationTimes.size(), updates.size(), min, median, stddev, 1000. * updates.size() / min);
       
   122                     deleteLater();
       
   123                     return;
       
   124                 }
       
   125             }
       
   126         }
       
   127     }
       
   128 
       
   129     QTimer::singleShot(0, this, SLOT(updateRect()));
       
   130 }
       
   131 
       
   132 ReplayWidget::ReplayWidget(const QString &filename_)
       
   133     : currentFrame(0)
       
   134     , currentIteration(0)
       
   135     , filename(filename_)
       
   136 {
       
   137     setWindowTitle(filename);
       
   138     QFile file(filename);
       
   139 
       
   140     QRect bounds;
       
   141     if (file.open(QIODevice::ReadOnly)) {
       
   142         QDataStream in(&file);
       
   143         in >> buffer >> updates;
       
   144     }
       
   145 
       
   146     qDebug() << "Read paint buffer with" << buffer.numFrames() << "frames";
       
   147 
       
   148     resize(buffer.boundingRect().size().toSize());
       
   149 
       
   150     setAutoFillBackground(false);
       
   151     setAttribute(Qt::WA_NoSystemBackground);
       
   152 
       
   153     QTimer::singleShot(10, this, SLOT(updateRect()));
       
   154 }
       
   155 
       
   156 int main(int argc, char **argv)
       
   157 {
       
   158     QApplication app(argc, argv);
       
   159 
       
   160     if (argc <= 1) {
       
   161         printf("Usage: %s filename\n", argv[0]);
       
   162         return 1;
       
   163     }
       
   164 
       
   165     ReplayWidget *widget = new ReplayWidget(argv[1]);
       
   166     widget->show();
       
   167 
       
   168     return app.exec();
       
   169 }
       
   170 #include "main.moc"