Trying to get rid of our QML timer.
authorsl
Tue, 03 Jun 2014 16:11:47 +0200
changeset 29a42cc76a2d5a
parent 28 8297924aa384
child 30 c0f274a21d33
Trying to get rid of our QML timer.
We now connect to our window after animation signal from C++.
It looks like it works much better now. Less UI lags smother animation.
MiniDisplay/minidisplay.cpp
MiniDisplay/minidisplay.h
TestsTab.qml
     1.1 --- a/MiniDisplay/minidisplay.cpp	Mon Jun 02 21:56:03 2014 +0200
     1.2 +++ b/MiniDisplay/minidisplay.cpp	Tue Jun 03 16:11:47 2014 +0200
     1.3 @@ -197,6 +197,49 @@
     1.4      iDisplay.BitBlit(bits,w,h,0,0);
     1.5  }
     1.6  
     1.7 +
     1.8 +/**
     1.9 + * @brief MiniDisplay::connectWindow
    1.10 + * @param aWindow
    1.11 + */
    1.12 +void MiniDisplay::connectWindow(QQuickWindow* aWindow, QJSValue aFunction)
    1.13 +{
    1.14 +    QObject::connect(aWindow, SIGNAL(afterAnimating()),
    1.15 +                     this,  SLOT(onAfterAnimating()));
    1.16 +
    1.17 +    iAfterAnimatingCallback = aFunction;
    1.18 +    iWindow = aWindow;
    1.19 +}
    1.20 +
    1.21 +/**
    1.22 + * @brief MiniDisplay::onAfterAnimating
    1.23 + */
    1.24 +void MiniDisplay::onAfterAnimating()
    1.25 +{
    1.26 +    static int frame=0;
    1.27 +    qDebug() << frame << "MiniDisplay::onAfterAnimating";
    1.28 +    /*
    1.29 +    if (iAfterAnimatingCallback.isCallable())
    1.30 +    {
    1.31 +        iAfterAnimatingCallback.call();
    1.32 +    }*/
    1.33 +
    1.34 +    renderWindow(iWindow);
    1.35 +    swapBuffers();
    1.36 +
    1.37 +    //Interrestingly rendering will suspend when we stop changing our text field content
    1.38 +    if (frame%8==0)
    1.39 +    {
    1.40 +        if (iAfterAnimatingCallback.isCallable())
    1.41 +        {
    1.42 +            iAfterAnimatingCallback.call(QJSValueList() << frame);
    1.43 +        }
    1.44 +    }
    1.45 +
    1.46 +    frame++;
    1.47 +}
    1.48 +
    1.49 +
    1.50  /**
    1.51   * @brief MiniDisplay::setPixel
    1.52   * @param x
     2.1 --- a/MiniDisplay/minidisplay.h	Mon Jun 02 21:56:03 2014 +0200
     2.2 +++ b/MiniDisplay/minidisplay.h	Tue Jun 03 16:11:47 2014 +0200
     2.3 @@ -40,6 +40,8 @@
     2.4      Q_INVOKABLE void renderWindow(QQuickWindow* aWindow);
     2.5      Q_INVOKABLE void renderImage(QImage* aImage);
     2.6      //
     2.7 +    Q_INVOKABLE void connectWindow(QQuickWindow* aWindow, QJSValue aFunction);
     2.8 +    //
     2.9      Q_INVOKABLE void setPixel(int x, int y, bool on);
    2.10  
    2.11  
    2.12 @@ -69,6 +71,9 @@
    2.13      QPoint framePosition() const;
    2.14      void setFramePosition(const QPoint& aPoint);
    2.15  
    2.16 +public slots:
    2.17 +    void onAfterAnimating();
    2.18 +
    2.19  signals:
    2.20      void opened();
    2.21      void openError();
    2.22 @@ -98,6 +103,8 @@
    2.23      int iReadAttempt;
    2.24      int iBrightness;
    2.25      QPoint iFramePosition;
    2.26 +    QJSValue iAfterAnimatingCallback;
    2.27 +    QQuickWindow* iWindow;
    2.28  };
    2.29  
    2.30  #endif // MINIDISPLAY_H
     3.1 --- a/TestsTab.qml	Mon Jun 02 21:56:03 2014 +0200
     3.2 +++ b/TestsTab.qml	Tue Jun 03 16:11:47 2014 +0200
     3.3 @@ -37,7 +37,8 @@
     3.4      //! [screen-properties]
     3.5          width: rectangleScreen.width
     3.6          height: rectangleScreen.height
     3.7 -        property int frameCounter:0;
     3.8 +        //property int frameCounter:0;
     3.9 +        property int firstFrame:-1;
    3.10          property var startTime:new Date();
    3.11  
    3.12          Rectangle {
    3.13 @@ -111,76 +112,87 @@
    3.14                  antialiasing: false
    3.15              }
    3.16  
    3.17 +            //onA: {}
    3.18 +
    3.19          }
    3.20  
    3.21  
    3.22 +        function doFrame(frameCount)
    3.23 +        {
    3.24 +            var current = new Date();
    3.25 +            var milliseconds = (current.getTime() - startTime.getTime());
    3.26  
    3.27 +            if (firstFrame==-1)
    3.28 +            {
    3.29 +                firstFrame=frameCount
    3.30 +            }
    3.31 +
    3.32 +
    3.33 +            var frameCounter=frameCount-firstFrame;
    3.34 +            labelFrameCount.text=frameCounter;
    3.35 +            labelTime.text=milliseconds/1000;
    3.36 +            labelTimePerFrame.text=(milliseconds/frameCounter).toFixed(3);
    3.37 +            labelFps.text=(1000/(milliseconds/frameCounter)).toFixed(0);
    3.38 +
    3.39 +            if (checkBoxDoClear.checked)
    3.40 +            {
    3.41 +                display.clear();
    3.42 +            }
    3.43 +
    3.44 +            if (checkBoxRenderToDisplay.checked)
    3.45 +            {
    3.46 +                if (checkBoxFillAndClearOnly.checked)
    3.47 +                {
    3.48 +                    //Trying to make it a worse case scenario for our frame diff algo
    3.49 +                    if (doFill)
    3.50 +                    {
    3.51 +                        display.fill();
    3.52 +                    }
    3.53 +                    else
    3.54 +                    {
    3.55 +                        display.clear();
    3.56 +                    }
    3.57 +
    3.58 +                    if (frameCounter%2)
    3.59 +                    {
    3.60 +                        doFill=!doFill;
    3.61 +                    }
    3.62 +                }
    3.63 +                else if (checkBoxOnePixelOnly.checked)
    3.64 +                {
    3.65 +                    display.setPixel(0,0,frameCounter%2);
    3.66 +                }
    3.67 +                else
    3.68 +                {
    3.69 +                    display.renderWindow(frameWindow);
    3.70 +                }
    3.71 +
    3.72 +                if (!checkBoxNoSwapBuffers.checked)
    3.73 +                {
    3.74 +                    display.swapBuffers();
    3.75 +                }
    3.76 +
    3.77 +            }
    3.78 +        }
    3.79  
    3.80  
    3.81          //! [timer]
    3.82          Timer {
    3.83              id: timer
    3.84 -            interval: frameWindow.timeoutInterval; running: true; repeat: true
    3.85 +            interval: frameWindow.timeoutInterval; running: false; repeat: true
    3.86              property bool doFill: true
    3.87              onTriggered: {
    3.88                  //visible = false
    3.89 -
    3.90 -                var current = new Date();
    3.91 -                var milliseconds = (current.getTime() - frameWindow.startTime.getTime());
    3.92 -
    3.93 -                frameWindow.frameCounter++;
    3.94 -                labelFrameCount.text=frameWindow.frameCounter;
    3.95 -                labelTime.text=milliseconds/1000;
    3.96 -                labelTimePerFrame.text=(milliseconds/frameWindow.frameCounter).toFixed(3);
    3.97 -                labelFps.text=(1000/(milliseconds/frameWindow.frameCounter)).toFixed(0);
    3.98 -
    3.99 -                if (checkBoxDoClear.checked)
   3.100 -                {
   3.101 -                    display.clear();
   3.102 -                }
   3.103 -
   3.104 -                if (checkBoxRenderToDisplay.checked)
   3.105 -                {
   3.106 -                    if (checkBoxFillAndClearOnly.checked)
   3.107 -                    {
   3.108 -                        //Trying to make it a worse case scenario for our frame diff algo
   3.109 -                        if (doFill)
   3.110 -                        {
   3.111 -                            display.fill();
   3.112 -                        }
   3.113 -                        else
   3.114 -                        {
   3.115 -                            display.clear();
   3.116 -                        }
   3.117 -
   3.118 -                        if (frameWindow.frameCounter%2)
   3.119 -                        {
   3.120 -                            doFill=!doFill;
   3.121 -                        }
   3.122 -                    }
   3.123 -                    else if (checkBoxOnePixelOnly.checked)
   3.124 -                    {
   3.125 -                        display.setPixel(0,0,frameWindow.frameCounter%2);
   3.126 -                    }
   3.127 -                    else
   3.128 -                    {
   3.129 -                        display.renderWindow(frameWindow);
   3.130 -                    }
   3.131 -
   3.132 -                    if (!checkBoxNoSwapBuffers.checked)
   3.133 -                    {
   3.134 -                        display.swapBuffers();
   3.135 -                    }
   3.136 -
   3.137 -                }
   3.138 +                //frameWindow.doFrame();
   3.139  
   3.140                  //Signal our timeout
   3.141 -                frameWindow.timeout();
   3.142 +                //frameWindow.timeout();
   3.143              }
   3.144          }
   3.145          //! [timer]
   3.146          Component.onCompleted: {
   3.147 -            visible = true
   3.148 +            visible = true;
   3.149 +            display.connectWindow(frameWindow,doFrame);
   3.150          }
   3.151  
   3.152      }
   3.153 @@ -202,7 +214,8 @@
   3.154              text: qsTr("Reset stats")
   3.155              onClicked: {
   3.156                  frameWindow.startTime = new Date();
   3.157 -                frameWindow.frameCounter = 0;
   3.158 +                //frameWindow.frameCounter = 0;
   3.159 +                frameWindow.firstFrame = -1;
   3.160              }
   3.161          }
   3.162