Results 1 to 8 of 8

Thread: QGLWidget, QPainter and shared contexts

  1. #1

    Unhappy QGLWidget, QPainter and shared contexts

    Hi there !

    I'm having trouble using a QPainter (specifically drawing QPixmap) on a QGLWidget.

    Here's my setup :
    I have an application with two QGLWidget sharing the same GL context (this is important).
    I then use a QPainter to draw QPixmaps over the GL rendering.
    Everything is fine except memory usage increases indefinitely. This won't happen if i don't call the QPainter::drawPixmap() function.

    I went through Qt's code and noticed that they use a cache system maintaining GL textures associated to QPixmap. The thing is that when Qt decides to remove a GL texture from the cache, if the context used to create that texture is shared (that's true in my case) Qt won't destory the underlying GL texture (glDeleteTextures) resulting in leaving unreachable GL textures in memory.
    I modified Qt's code to skip the test and it runs just fine (memory usage won't increase).

    As those GL textures are internally created by Qt and as I couldn't find any function to access them, I can't do much about it ...

    So I wondered if any of you had ever encountered the issue, or if I missed something, or if i did something the wrong way ...

    I forgot to mention that some of the QPixmap I draw are updated quite often and that's why the cache has to delete objects.

    Thanks !

    PS: a few references in the code if you're interested :
    the Qt's wrapper class around GL texture objects. Instance of those are being held in the cache.
    file qgl.cpp, line 1288 (Qt 4.2.0 open source edition):
    Qt Code:
    1. class QGLTexture {
    2. public:
    3. QGLTexture(const QGLContext *ctx, GLuint tx_id, qint64 _qt_id, bool _clean = false)
    4. : context(ctx), id(tx_id), qt_id(_qt_id), clean(_clean) {}
    5. ~QGLTexture() {
    6. if (!context->isSharing()) // <--- THERE
    7. glDeleteTextures(1, &id);
    8. }
    9.  
    10. const QGLContext *context;
    11. GLuint id;
    12. qint64 qt_id;
    13. bool clean;
    14. };
    To copy to clipboard, switch view to plain text mode 

    Deleting such an object won't release the underlying GL texture if context is shared ...
    Last edited by bamboo; 10th April 2007 at 14:26. Reason: spelling error

  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, QPainter and shared contexts

    You can either report a bug and/or try to reuse the same texture for a new pixmap (I understand the problem is that the pixmap changes which causes memory usage to grow) and draw directly using GL code.

  3. #3

    Default Re: QGLWidget, QPainter and shared contexts

    Thanks,

    I reported a bug to trolltech last week about that.
    But I can't reuse the same GL texture as I don't have access to it.
    For now i just don't use a QPainter to draw on QGLWidgets. I draw everything I need in an offscreen QPixmap and then use my own code to upload and draw it with OpenGL.

    I'll post here references to the bug I reported if any are interested.

  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, QPainter and shared contexts

    What do you mean you don't have access to it? So what calls BindTexture()?

  5. #5

    Default Re: QGLWidget, QPainter and shared contexts

    Qt does.
    I never dealt directly with OpenGL. I just used the QPainter and drawPixmap().
    Internally drawPixmap() creates GL textures for each of my QPixmap and store them in the cache. Whenever a QPixmap is modified, a new GL texture is associated to it. When the cache hits its size limit, Qt deletes GL textures but as my GL context is shared, glDeleteTexture() is never called (but the Qt wrapper around GL textures is deleted from the cache).

    Basically this is what I was doing :

    Qt Code:
    1. void draw_overlays(QGLWidget *p_GL)
    2. {
    3. QPainter l_Painter(p_GL);
    4.  
    5. // my overlays are QPixmaps
    6. // some of them can be updated often
    7. for_each_of_my_overlays()
    8. {
    9. // this will internally create a GL texture for the QPixmap
    10. // reusing the texture if the QPixmap is the same (cache system)
    11. l_Painter->drawPixmap(l_Overlay);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    I hope I'm clear enough

    edit: I think I understand what you mean. I don't use QGLWidget::bindTexture() neither QGLContext::bindTexture() but I noticed in the docs that if the context is shared the GL texture won't be destroyed. But in my case, Qt creates GL texture, not me.
    Last edited by bamboo; 16th April 2007 at 09:46.

  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, QPainter and shared contexts

    Ok, but this is an overlay. You do have some actual GL code underneath. So you can make a plane, place it orthogonal to the screen and instead of using QPainter simply apply a texture to the plane.

  7. #7

    Default Re: QGLWidget, QPainter and shared contexts

    Yep, that's what I do now to overcome this
    But using QPainter would have been "cleaner" and easier for me as it is designed fot this.
    Anyway I reported the bug to Trolltech as I don't think this is a normal behavior ...
    Thanks !

  8. #8

    Default Re: QGLWidget, QPainter and shared contexts

    Hi again,

    for those who might be interested, the bug has been verified by Qt's team and has been assigned #158971.
    http://www.trolltech.com/developer/t...ntry&id=158971

    Have a nice day !

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.