Results 1 to 8 of 8

Thread: QGLWidget updateGL()

  1. #1
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGLWidget updateGL()

    Hi all,
    I'm working on a custom QGLWidget used to perform 2d display for a camera acquisition.
    Basically I grab the image buffer and display in my QGLWidget.
    I noticed that if I call a method from my external grabbing thread the updateGL() doesn't work

    Qt Code:
    1. void MyDisplayGL::DrawBuffer(BYTE* apBuffer)
    2. {
    3. data=apBuffer;
    4. updateGL();
    5. }
    To copy to clipboard, switch view to plain text mode 

    The same for all the OpenGL functions (glTexSubImage2D).

    So there are three ways for me to do the rendering
    - use an internal QTimer that call asyncronusly updateGL()
    - call update() method in my DrawBuffer instead of updateGL()
    - emit a signal in my DrawBuffer and connect to a slot with updateGL()

    But the first method is asyncronous and I should save the buffer(memcpy), the second is much slow and the third doesn't provide a good timing and the video has strange slowings down.

    So hoping this small research helps, I'd like to know your opinion about that and to tell my possible mistakes.

    Thanks in advance

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

    Default Re: QGLWidget updateGL()

    I think you should connect updateGL() to a timer and either store the data from the other thread in some kind of queue, so that it won't interfere, or synchronise threads and store image in a shared buffer(but it'll be slower then) -- shared memory could come in handy here, you might even get away with synchronising here -- make buffers separate (so like a queue) and only assign a pointer to the current buffer in a shared variable. You can use Qt atomic operations for that to avoid the need to block the other thread.

  3. #3
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget updateGL()

    Hi,
    thanks for answering,
    well what I'm trying to understand is if there is a way to have all OpenGL functions calls (including UpdateGL() which is simply a glDraw) perform correctly when called from an external thread. In fact I believe the problem is when calling those functions from the "outside", if I use the internal Qtimer events everything works nice.
    The reason for this is that the external thread must have the control of the display rate in a syncrounous mode, basically it waits until the image is displayed.

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

    Default Re: QGLWidget updateGL()

    Quote Originally Posted by Lele
    Hi,
    thanks for answering,
    well what I'm trying to understand is if there is a way to have all OpenGL functions calls (including UpdateGL() which is simply a glDraw) perform correctly when called from an external thread.
    No.

    The reason for this is that the external thread must have the control of the display rate in a syncrounous mode, basically it waits until the image is displayed.
    You can use custom events for that.

  5. #5
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget updateGL()

    thanks again for answering,
    even with custom events there's the same problem, since you cannot send an event from different threads.
    Basically I'm looking for a workaround in order to perform UpdateGL() synchronously from different thread.

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

    Default Re: QGLWidget updateGL()

    Quote Originally Posted by Lele
    thanks again for answering,
    even with custom events there's the same problem, since you cannot send an event from different threads.
    Yes, you can. postEvent just enqueues an event and the event loop from the thread which owns the receiver will eventually process it.

    Basically I'm looking for a workaround in order to perform UpdateGL() synchronously from different thread.
    If you want to do it synchronously, you'll have to synchronise threads. And I doubt you want it, as it doesn't make much sense to use threads then.

    Why do you want it to be a synchronous call? When some data is ready, just post an event to the GL widget and it will redraw itself ASAP. In the meantime you can continue processing in the worker thread.

  7. #7
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGLWidget updateGL()

    I want to do it synchronously in order to prevent another memcpy of the buffer, I'd like to receive the buffer pointer and write the content directly to the framebuffer of the graphic board. (the display must be as fast as possible, now I have to use update() )
    But there's no way to make OpenGl calls work when called from external thread.

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

    Default Re: QGLWidget updateGL()

    Quote Originally Posted by Lele
    I want to do it synchronously in order to prevent another memcpy of the buffer, I'd like to receive the buffer pointer and write the content directly to the framebuffer of the graphic board. (the display must be as fast as possible, now I have to use update() )
    But there's no way to make OpenGl calls work when called from external thread.
    It doesn't have to be a synchronous call. Buffers can be allocated dynamically and pushed onto a list or something like that...

    Worker thread:
    Qt Code:
    1. MyBufer *b = new MyBuffer();
    2. b->fillWithData(somedata);
    3. mutex.lock();
    4. bufferqueue.push(b);
    5. mutex.unlock();
    6. //...
    To copy to clipboard, switch view to plain text mode 

    Main thread:
    Qt Code:
    1. mutex.lock();
    2. MyBuffer *buf =bufferqueue.pop();
    3. bufferqueue.clear(); // clear unused items (aka "skip frames")
    4. mutex.unlock();
    5. Display(buf);
    6. //...
    To copy to clipboard, switch view to plain text mode 

    Of course the code is simply an idea and not a complete solution. Another possibility would be to use custom events instead of the queue.

  9. The following user says thank you to wysota for this useful post:

    Lele (30th May 2006)

Similar Threads

  1. about QGLWidget and QPainter
    By showhand in forum Qt Programming
    Replies: 5
    Last Post: 12th November 2008, 10:45
  2. [qt4,win,g++] QPainter on a QGLWidget
    By jh in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2008, 06:29
  3. QGLWidget renderText problem
    By mbjerkne in forum Qt Programming
    Replies: 1
    Last Post: 7th April 2006, 21:35
  4. QGLWidget, toolbox and paintGL()
    By mickey in forum Qt Programming
    Replies: 3
    Last Post: 21st March 2006, 01:05
  5. qGLWidget
    By mickey in forum Newbie
    Replies: 8
    Last Post: 24th February 2006, 00:30

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.