Results 1 to 20 of 20

Thread: Fast Timer Updates vs. Monitor Refresh Rates

  1. #1
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Fast Timer Updates vs. Monitor Refresh Rates

    I need to create a rapidly refreshing widget, updating at around 60Hz.

    I have a prototype, and it seems to lag (skip a frame) every 5-20 frames.

    I would guess that one of two things is happening:
    - The monitor refresh rate of 60Hz is not synchronized with the applications refresh, or
    - The widget does not actually update every 6ms


    The application is meant to flicker between two gradients rapidly. I have made the paintEvent as simple as possible:
    Qt Code:
    1. void Flickerer::paintEvent(QPaintEvent*)
    2. {
    3. p->begin(this);
    4. if(showingG1)
    5. p->drawPixmap(*rect, *buffer1);
    6. else
    7. p->drawPixmap(*rect, *buffer2);
    8. p->end();
    9. }
    To copy to clipboard, switch view to plain text mode 

    A link to the prototype, if it will help (use preset 2 @ 30-60Hz).


    How can I make the lag go away? Is there a way to synchronize to the screen's refresh? Or is there a method faster than drawPixmap()? Perhaps having two widgets, then just showing/hiding one of them on every update?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Maybe you have already read this blog post?
    http://labs.qt.nokia.com/2010/12/02/...l-scene-graph/

    It is about QML but you can take the general guidelines for regular qt widgets too.
    I think the idea is to use some of the OpenGL functions to synchronize with the refresh rate of the screen.

  3. The following user says thank you to tbscope for this useful post:

    artoonie (5th December 2010)

  4. #3
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    I hadn't actually, thank you!

    I had begun this using OpenGL but figured I needed double buffering, which requires GLUT, which conflicts with Qt (or so I read), so I had to switch to Qt-native functions to get automatic double buffering.

    Looks like I'll be going back to OpenGL and testing out swapBuffers.



    Thanks! I'll update this thread on how it goes (for legacy purposes).

    -Armin

  5. #4
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Hi,
    I switched over to a GLWidget, but OpenGl gives the same results. So I tested out the function noted in the link you posted:
    Qt Code:
    1. fmt.setSwapInterval(60); // Should update once per second (but updates faster)
    2. this->setFormat(fmt);
    To copy to clipboard, switch view to plain text mode 

    But the setSwapInterval seems to have no effect. I tried setting the timerInterval to 0 and that didn't work either.


    How do I sync the timer to update with the swapInterval?

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    It may sound as a dumb question but why do you want to update the widget every 6ms? This gives about 167 frames per second, I can hardly think of a reason why anyone would want so many ui updates per second. Especially with a 60Hz display. I can't even think of a hardware that would be capable of doing as many updates and still be able to provide some data to update the ui with.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Quote Originally Posted by wysota View Post
    It may sound as a dumb question but why do you want to update the widget every 6ms? This gives about 167 frames per second, I can hardly think of a reason why anyone would want so many ui updates per second. Especially with a 60Hz display. I can't even think of a hardware that would be capable of doing as many updates and still be able to provide some data to update the ui with.
    Haha, silly typo, my bad-
    I meant 16ms, or more precisely, 16.666...ms.
    I need 60Hz, meaning refresh every time the screen refreshes.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Ok, so how exactly do you force your widget to redraw?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    I'm using a QTimer...is there another way, maybe blocking until a vertical sync event? I've tried using swapBuffers but I can't get it to work without a timer.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Quote Originally Posted by artoonie View Post
    I'm using a QTimer...
    Show us the code.
    Last edited by wysota; 7th December 2010 at 11:24.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Quote Originally Posted by artoonie View Post
    I would guess that one of two things is happening:
    - The monitor refresh rate of 60Hz is not synchronized with the applications refresh, or
    - The widget does not actually update every 6ms
    Both of these things are happening.

    QTimer can be set in whole milliseconds only. Approximating a 1/60 seconds rate with 16 msecs will lead a whole frame difference in time after 24 or 25 frames even if the timer events were perfectly spaced. A 17 msec approximation will extend the sync out to around 48-50 frames. QTimer events are processed when the program returns to the event loop after the time has expired. Depending on what else the program is doing, like rendering your gradient, this might be a significant delay. The combination of the two will lead to a mismatch that is variable with activity.

    MythTV can use timers to maintain video refresh sync, but I believe it also has extensive frame counting over relatively long periods to determine the average frame rate and drops/duplicates frames to realign things periodically. The preferred method in MythTV is to use the OpenGL vsync. I don't know how this can be accessed.

  12. #11
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Quote Originally Posted by wysota View Post
    Quote Originally Posted by artoonie View Post
    I'm using a QTimer...QUOTE]
    Show us the code.
    It's been established that timers are not the way to go, but I'll paste what I have:
    Qt Code:
    1. Flickerer::Flickerer(int timerInterval, QWidget *parent) {
    2. m_timer = new QTimer( this );
    3. connect( m_timer, SIGNAL(timeout()), this, SLOT(timeOutSlot()) );
    4. m_timer->start( timerInterval );
    5. }
    6. void Flickerer::timeOutSlot()
    7. {
    8. showingG1 = !showingG1;
    9. updateGL();
    10. }
    11. void Flickerer::paintGL()
    12. {
    13. if(showingG1) /* paints gradient 1 */ else /* paints gradient 2 */
    14. }
    To copy to clipboard, switch view to plain text mode 




    QTimer can be set in whole milliseconds only. Approximating a 1/60 seconds rate with 16 msecs will lead a whole frame difference in time after 24 or 25 frames even if the timer events were perfectly spaced. A 17 msec approximation will extend the sync out to around 48-50 frames. QTimer events are processed when the program returns to the event loop after the time has expired. Depending on what else the program is doing, like rendering your gradient, this might be a significant delay. The combination of the two will lead to a mismatch that is variable with activity.

    MythTV can use timers to maintain video refresh sync, but I believe it also has extensive frame counting over relatively long periods to determine the average frame rate and drops/duplicates frames to realign things periodically. The preferred method in MythTV is to use the OpenGL vsync. I don't know how this can be accessed.
    I agree- I need to use OpenGL vsync but I don't know how to get a signal from the display so I'd know when to update.



    Thanks you.

  13. #12
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    You do not need to get the signal from the display. OpenGl has a function that does that for you.

    What I would try to do:
    Create a graphicsview that contains the user interface. Make it so that everything inside the graphicsview is painted via OpenGl
    There are lots of tutorials in the Qt labs and documentation.
    And then use the information from the blog post about blocking the eventloop and synchronising the eventloop to the refresh rate of the monitor.

    If you use a graphicsview and opengl, the effects possible with your user interface are infinite, although limited to the contents of the graphicsview.

    Edit:
    Here's a nice example:
    http://labs.qt.nokia.com/2008/06/27/...s-with-opengl/

    Edit 2:
    You need this function:
    http://doc.qt.nokia.com/latest/qglfo...etSwapInterval
    Set it to 1

    Tip:
    QML does this out of the box. It's worth studying the code.
    Last edited by tbscope; 7th December 2010 at 04:34.

  14. #13
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    [Deleted...]


    Added after 59 minutes:


    I've narrowed my problems down to here:
    Qt Code:
    1. fmt.setSwapInterval(1);
    2. QGLWidget* w = new QGLWidget();
    3. w->setFormat(fmt);
    4.  
    5. qDebug("Swap interval: %d", fmt.swapInterval()); // Prints 1
    6. qDebug("Swap interval: %d", w->format().swapInterval()); // Prints -1
    To copy to clipboard, switch view to plain text mode 
    Why is the swap interval not being accepted by the widget? Or am I doing something else wrong here?

    --Update--
    Okay, this is odd. The docs have a format() method, but the setFormat() method (which is linked from the format() description) does not exist, even in the list of all members.
    Last edited by artoonie; 9th December 2010 at 07:48.

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    And this code was compiling? There is a constructor to QGLWidget that takes a format parameter.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #15
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Both ways are equivalent. I get the same behavior with
    Qt Code:
    1. fmt.setSwapInterval(1);
    2. QGLWidget* w = new QGLWidget(fmt);
    To copy to clipboard, switch view to plain text mode 


    Also, quote from here:
    Returns the currently set swap interval. -1 is returned if setting the swap interval isn't supported in the system GL implementation.
    Possibly it's just my system?

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Quote Originally Posted by artoonie View Post
    Possibly it's just my system?
    Hard to believe that unless you have some really broken OpenGL implementation.

    Check for this:
    Under Windows the WGL_EXT_swap_control extension has to be present, and under X11 the GLX_SGI_video_sync extension has to be present.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #17
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    ...looks like that's my problem? My hardware supports OpenGL 1.1 and 10% of OpenGL 1.2. WGL_SWAP_LAYER_BUFFERS_ARB is set to false and WGL_EXT_SWAP_CONTROL is grayed out on the OpenGL Extensions Viewer.

    Guess I'll be testing it elsewhere and let you know.

  19. #18
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    So that turned out to be the problem- perhaps vertex and fragment shaders are also needed?
    I'm using a different computer and I get a message saying the shaders are linked.

    HOWEVER, it still isn't updating without a QTimer...I do remove tearing with fmt->setSwapInterval(1) (whereas tearing is horrid with that line commented out). Is this the expected behavior?

  20. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Quote Originally Posted by artoonie View Post
    HOWEVER, it still isn't updating without a QTimer...
    Why would it? Read what the swap interval does. It doesn't cause any periodic updates.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. #20
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fast Timer Updates vs. Monitor Refresh Rates

    Right, right, I naively assumed some magic might happen without a timer.

    Thank you so much for your help, I really appreciate it.


    -Armin

Similar Threads

  1. X Monitor in a Qt form
    By prasenjit in forum Qt-based Software
    Replies: 1
    Last Post: 29th April 2010, 06:58
  2. Monitor a new added File
    By designer.software in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2010, 09:50
  3. Replies: 2
    Last Post: 10th August 2009, 09:45
  4. Qt system monitor?
    By khopper in forum Qt Programming
    Replies: 2
    Last Post: 7th November 2008, 15:32
  5. How to calculate Monitor Resolution?
    By ashukla in forum Qt Programming
    Replies: 2
    Last Post: 7th October 2007, 07:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.