Results 1 to 19 of 19

Thread: How to speed up a transparent QGraphicsTextItem in Opengl?

  1. #1
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to speed up a transparent QGraphicsTextItem in Opengl?

    Hi there!

    I'm trying to increase the rendering speed of a transparent QGraphicsTextItem. Buffering the output in a pixmap using the GraphicsItem CacheMode doesn't work for me, because the scenes perspective changes a lot and with it the background behind the transparent text.

    So I thought of compiling an opengl-displaylist of the QGraphicsTextItem.paint. Did anyone already try that?

    My problem is that before I can call paint myself, I need to setup a QStyleOptionGraphicsItem which seems like a complex undertaking. Is there a simple way?

    Thx in advance!

    Johannes

  2. #2
    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: How to speed up a transparent QGraphicsTextItem in Opengl?

    Quote Originally Posted by JohannesMunk View Post
    I'm trying to increase the rendering speed of a transparent QGraphicsTextItem. Buffering the output in a pixmap using the GraphicsItem CacheMode doesn't work for me, because the scenes perspective changes a lot and with it the background behind the transparent text.
    The background is irrelevant. The cache contains the item only - without its background. The cache would only be useless if you constantly changed items themselves. In other situations at least one of the cache modes should introduce speedup.

    So I thought of compiling an opengl-displaylist of the QGraphicsTextItem.paint. Did anyone already try that?
    That's essentially what Qt does automatically if you use OpenGL viewport and do caching. It keeps the pixmap in memory of your gfx card and just blits it to the framebuffer.

    My problem is that before I can call paint myself, I need to setup a QStyleOptionGraphicsItem which seems like a complex undertaking. Is there a simple way?
    Contents of the style option object depends on the state of the view so the best you can do is to rerun paint everytime this object changes which is basically what Qt already does with caching.
    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.


  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    First of all: Thx for your fast response!

    Quote Originally Posted by wysota View Post
    The background is irrelevant. The cache contains the item only - without its background. The cache would only be useless if you constantly changed items themselves. In other situations at least one of the cache modes should introduce speedup.
    Is this achieved by alphablending the pixmap/texture?

    Quote Originally Posted by wysota View Post
    That's essentially what Qt does automatically if you use OpenGL viewport and do caching. It keeps the pixmap in memory of your gfx card and just blits it to the framebuffer.
    Caching a pixmap is not what I had in mind when I spoke of an OpenGL-Displaylist. I sort of hoped to compile the actual vertex ops etc..

    Quote Originally Posted by wysota View Post
    Contents of the style option object depends on the state of the view so the best you can do is to rerun paint everytime this object changes which is basically what Qt already does with caching.
    Yes, so far that's clear. But if I would want to render the TextItem once outside a paintevent to compile a displaylist myself, I need to pass a valid style option. How do I get that?

    Probably my problem with the cachemodes is that for nearly each repaint (camera position changed) I'm changing the projection of all items. My old 3d mapping thing..

    Where in the Qt sources do I find the caching modes implementation for the opengl painter?

    Thx again!

    Johannes

  4. #4
    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: How to speed up a transparent QGraphicsTextItem in Opengl?

    Quote Originally Posted by JohannesMunk View Post
    Is this achieved by alphablending the pixmap/texture?
    Yep, something like that.

    Caching a pixmap is not what I had in mind when I spoke of an OpenGL-Displaylist. I sort of hoped to compile the actual vertex ops etc..
    The text is most likely rasterized first so you'd get four vertices and a texture. You get the same for a pixmap.

    Yes, so far that's clear. But if I would want to render the TextItem once outside a paintevent to compile a displaylist myself, I need to pass a valid style option. How do I get that?
    You need to fill one properly but then there is a question what would you fill in there. I assure you - you won't do better than what Qt already does in this field. You can't do better than blitting a texture into the buffer.

    Probably my problem with the cachemodes is that for nearly each repaint (camera position changed) I'm changing the projection of all items.
    You should be able to use ItemCoordinateCache with this approach. As long as you modify the transformations of items and not their internals, this should work.

    Where in the Qt sources do I find the caching modes implementation for the opengl painter?
    Either in src/gui/graphicsview/qgraphicsitem* or in src/opengl/qpaintengine_opengl* and src/opengl/qpixmapdata_gl*. I assume the bare caching code is the same regardless of the backend - it just stores an appropriate pixmap. The magic is how the pixmap works.
    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.


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

    JohannesMunk (27th October 2009)

  6. #5
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Thx for your help, but using ItemCoordinateCache just gives me a solid black background. I suppose when the text-background is cached my background still is black.

    I will investigate this further, when there is more time for it. Just thought I might ask and rule out any obvious solutions.

    Gn8!

    Johannes

  7. #6
    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: How to speed up a transparent QGraphicsTextItem in Opengl?

    Quote Originally Posted by JohannesMunk View Post
    Thx for your help, but using ItemCoordinateCache just gives me a solid black background.
    How exactly do you activate the cache?
    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.


  8. #7
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    In the constructor of my ProjectionItem I create the textchilditem:

    Qt Code:
    1. _textitem = new QGraphicsTextItem(text,this);
    2. ...
    3. _textitem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
    To copy to clipboard, switch view to plain text mode 

  9. #8
    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: How to speed up a transparent QGraphicsTextItem in Opengl?

    And what is the boundingRect() of the item?

    Edit: I'm not able to reproduce the behaviour you observe using the following code. Can you change it so that it reproduces your problem?

    Qt Code:
    1. #include <QtGui>
    2. #include <QGLWidget>
    3.  
    4. int main(int argc, char **argv){
    5. QApplication app(argc, argv);
    6. scene.setBackgroundBrush(Qt::blue);
    7. view.setScene(&scene);
    8. view.setViewport(new QGLWidget);
    9. QString txt = "Some text";
    10. QGraphicsTextItem *item = scene.addText(txt);
    11. QFont f;
    12. f.setPointSize(36);
    13. item->setFont(f);
    14. item->setFlag(QGraphicsItem::ItemIsMovable);
    15. item->setCacheMode(QGraphicsItem::ItemCoordinateCache);
    16. QTransform trans;
    17. trans.rotate(60, Qt::YAxis);
    18. item->setTransform(trans);
    19. item->setOpacity(0.7);
    20. view.show();
    21. return app.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 27th October 2009 at 23:41.
    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.


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

    JohannesMunk (28th October 2009)

  11. #9
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    The BoundingRect for the ProjectedItem is

    QPainterPath CGL3dProjectedItem::shape() const
    {
    QPainterPath p;
    QPolygonF polygon = mapToScene(this->childrenBoundingRect());
    p.addPolygon(polygon);
    return p;
    }
    virtual QRectF boundingRect() const {return _bounds;}

    in UpdateTransformation:
    ..
    _bounds = shape().boundingRect();
    ..

    The black area is fine. Exactly the area of the textitem. And the text shows as it should with transparent background as soon as I disable the caching.

    I do really appreciate your efforts!

  12. #10
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    I have seen your example. Added a gradient to the background and it still works. Amazing :->

    I don't know exactly, where my problem comes in. With the transformation or the way I'm drawing the background. I am drawing the background directly with gl. Maybe thats the problem.

    I will implement something simple but similiar and report back.

  13. #11
    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: How to speed up a transparent QGraphicsTextItem in Opengl?

    Your implementation of shape() and boundingRect() is awfully slow. You are creating painter paths all the time. Cache all the data you can (meaning the shape and the bounding rect), this should already give you some speed improvement.

    BTW. The background shouldn't influence the contents of the item's cache... I suspect this is caused by an incorrect result of boundingRect() at the time the cache is being enabled.
    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.


  14. #12
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Well the GL Background isn't it:

    Qt Code:
    1. #include <QtGui>
    2. #include <QGLWidget>
    3.  
    4. class CGLScene : public QGraphicsScene
    5. {
    6. protected:
    7. void drawBackground(QPainter *painter, const QRectF &rect)
    8. {
    9. glClearColor(1.0,0.0,0.0,1.0);
    10. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    11. }
    12. };
    13.  
    14. int main(int argc, char **argv){
    15. QApplication app(argc, argv);
    16. CGLScene scene;
    17.  
    18. view.setScene(&scene);
    19. view.setViewport(new QGLWidget);
    20. QString txt = "Some text";
    21. QGraphicsTextItem *item = scene.addText(txt);
    22. QFont f;
    23. f.setPointSize(36);
    24. item->setFont(f);
    25. item->setFlag(QGraphicsItem::ItemIsMovable);
    26. item->setCacheMode(QGraphicsItem::ItemCoordinateCache);
    27. QTransform trans;
    28. trans.rotate(60, Qt::YAxis);
    29. item->setTransform(trans);
    30. item->setOpacity(0.7);
    31. view.show();
    32. return app.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

    Problem is, introducing the perspective would blow this up a lot. We would need the camera, the projecteditem etc.. I could integrate the TextItem into the ProjectionDemo I did some time ago.. But than it would be a lot of code for you to read..

  15. #13
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Quote Originally Posted by wysota View Post
    Your implementation of shape() and boundingRect() is awfully slow. You are creating painter paths all the time. Cache all the data you can (meaning the shape and the bounding rect), this should already give you some speed improvement.
    Yes, I know. But that's definitely not the speed problem. It makes no difference if I have just one TextItem (thus only one transformation, shape, boundingrect calculation) with a lot of text or lots of textitems with just one line of text. Both are equally slow.

    Quote Originally Posted by wysota View Post
    BTW. The background shouldn't influence the contents of the item's cache... I suspect this is caused by an incorrect result of boundingRect() at the time the cache is being enabled.
    That is definitely the case! The cache is enabled in the constructor. The boundingrect changes all the time. Through the perspective change..

    We are on to something here :->

  16. #14
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Mmh.. What can I do about it?

    The ProjectedItem changes its projection. And thus its boundingrect.

    I don't set the boundingrect of the textitem at all. I just leave it as it is. Because its a childitem of the projecteditem it gets drawn at the right place.

    The ProjectedParentItem has no caching activated.

    When would be the right time to activate caching?

  17. #15
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Hey Wysota!

    I took the time and added the TextItem to the Widget3D Demo.



    So whenever you have the time you are welcome to look at the code. If not, it'll stay that slow a little longer :->

    Most relevant file is widget3d.h (CGL3dProjectedItem, CGL3dTextItem, ..).

    There you can disable the cachemode. And it works just fine.

    I think the core problem is: How can I use caching when the transformation and thus the boundingrect changes all the time?

    Thx a lot for your help so far!

    Johannes
    Attached Files Attached Files

  18. #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: How to speed up a transparent QGraphicsTextItem in Opengl?

    You have to isolate why the background is black. You can see it is not black in my code so there is nothing wrong with the cache in general. As for your bounding rect - see that when your item has no children its bounding rect will be invalid. If you create the cache at that time and not pass the amount of memory you want allocated for the item's cache, it will allocate as much memory as needed to fit your bounding rect. Which in your case is... 0. This is likely to lead to problems although I'd expect everything to be black, not just the background.

    I suggest you start from scratch (or with my code) and build upon it until you encounter a situation where cache fails. It might take shorter than wild bug chasing.
    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.


  19. #17
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Caching is only enabled for the TextItem! That is a child to a projecteditem, whose shape/boundingrect code I have given you earlier. The caching of the TextItem should not be affected by that, or is it?

    The TextItems-Bounding-Rect seems to be fine, as the cache-related black background suggests.

  20. #18
    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: How to speed up a transparent QGraphicsTextItem in Opengl?

    So is everything alright now or not?
    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. #19
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to speed up a transparent QGraphicsTextItem in Opengl?

    Hi there!

    I'm afraid it is not. The solid black background stays in my application. As you suggested, I continued our minimal error example in introducing the 3DTextItem and a dummy ProjectedItem. Sadly, still works.

    main.cpp:

    Qt Code:
    1. #include <QtGui>
    2. #include <QGLWidget>
    3. #include "textitem.h"
    4.  
    5. class CGLScene : public QGraphicsScene
    6. {
    7. protected:
    8. void drawBackground(QPainter *painter, const QRectF &rect)
    9. {
    10. glClearColor(1.0,1.0,0.0,1.0);
    11. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    12. }
    13. };
    14.  
    15. int main(int argc, char **argv){
    16. QApplication app(argc, argv);
    17. CGLScene scene;
    18.  
    19. view.setScene(&scene);
    20. view.setViewport(new QGLWidget);
    21.  
    22. CGL3dTextItem* item = new CGL3dTextItem("");
    23. item->setHTML("<font color=#FF0000>FAT TEST </font>");
    24.  
    25. scene.addItem(item);
    26.  
    27. QFont f;
    28. f.setPointSize(36);
    29. item->setFont(f);
    30. item->setFlag(QGraphicsItem::ItemIsMovable);
    31. //item->setCacheMode(QGraphicsItem::ItemCoordinateCache);
    32.  
    33. QTransform trans;
    34. trans.rotate(60, Qt::YAxis);
    35. item->setTransform(trans);
    36. item->setOpacity(1);
    37. view.show();
    38. return app.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 

    textitem.h - a separate header file for moc to start up.
    Qt Code:
    1. #ifndef TEXTITEM_H
    2. #define TEXTITEM_H
    3.  
    4. #include <QtGui>
    5. #include <QObject>
    6.  
    7. class CGL3dProjectedItem : public QObject, public QGraphicsItem
    8. { Q_OBJECT
    9. protected:
    10. virtual QRectF boundingRect() const {return shape().boundingRect();}
    11. virtual QPainterPath shape() const {QPainterPath p;QPolygonF polygon = mapToScene(this->childrenBoundingRect());p.addPolygon(polygon);return p;}
    12. virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) {}
    13. };
    14.  
    15. class CGL3dTextItem : public CGL3dProjectedItem
    16. { Q_OBJECT
    17. Q_PROPERTY(QObject* textitem READ getTextItem)
    18. Q_PROPERTY(QString text READ getText WRITE setText)
    19. Q_PROPERTY(QString html READ getHTML WRITE setHTML)
    20. Q_PROPERTY(QFont font READ getFont WRITE setFont)
    21. public:
    22. CGL3dTextItem(QString text) : CGL3dProjectedItem()
    23. {
    24. _textitem = new QGraphicsTextItem(text,this);
    25. _textitem->setTextInteractionFlags(Qt::TextBrowserInteraction);
    26. QTextOption textoption(Qt::AlignCenter);
    27. textoption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
    28. _textitem->document()->setDefaultTextOption(textoption);
    29. _textitem->setCacheMode(QGraphicsItem::ItemCoordinateCache);
    30. //setHandlesChildEvents(true);
    31. }
    32. enum { Type = UserType+2};
    33. int type() const { return Type;}
    34. QObject* getTextItem() {return _textitem;}
    35. QString getText() {return _textitem->toPlainText();}
    36. void setText(QString plaintext) {_textitem->setPlainText(plaintext);TextChanged();}
    37. QString getHTML() {return _textitem->toHtml();}
    38. void setHTML(QString htmltext) {_textitem->setHtml(htmltext);TextChanged();}
    39. QFont getFont() {return _textitem->font();}
    40. void setFont(QFont f) {_textitem->setFont(f);TextChanged();}
    41. protected:
    42. void TextChanged()
    43. {
    44. // SetCenterPos(CGL3d(-_textitem->document()->size().width()/2,0,0));
    45. //qDebug() << "TextItem Center: " << CenterPos().toString();
    46. }
    47. private:
    48. QGraphicsTextItem* _textitem;
    49. };
    50.  
    51. #endif // TEXTITEM_H
    To copy to clipboard, switch view to plain text mode 

    So maybe the problem is hidden in the kind of transformation I am doing. I will investigate.

    Thx

    Joh

Similar Threads

  1. OpenGL and Qt Question
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2009, 18:04
  2. Does OpenGL speed up the loading image?
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 4th December 2008, 09:26
  3. QMatrix rotate on QGraphicsTextItem speed on render..
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 22nd December 2007, 19:46
  4. OpenGL app at full speed? Can you do it?
    By mattropolis in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2007, 10:54
  5. Qtopia Core & OpenGL ES?
    By zelko in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 28th May 2007, 07:21

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.