PDA

View Full Version : Performance with many filled polygons on screen



lni
6th September 2009, 10:42
Hi,

I am plotting 5000 polygons, each containing 250 points. It is very slow. The program freezes several seconds whenever repaint is made.

Any advice on how to improve the performance?

Thanks.

wysota
6th September 2009, 12:40
Find the bottleneck first, then fix it.

If you want more details, provide more details yourself.

lni
6th September 2009, 15:53
Find the bottleneck first, then fix it.

If you want more details, provide more details yourself.

I have done the profiling and the cause is identified, it is in
painter->drawPolygon( QPolygonF ), it is called 5000 times, each QPolygonF having 250 points..

When I switch to painter->drawPolyline( QPolygonF ), it is very fast, but of course it is not what I want, because it don't fill the enclosing polygon area.

The QBrush used for the painter is just a Qt::SolidPattern style (It is worse if I use custom QImage as the filling pattern).

I am not sure if this information is enough. The coding itself is very simple...

Remenic
6th September 2009, 16:37
Try painting it to an off-screen bitmap, then blit that bitmap to the screen. Some drivers may have bad performance with certain drawing operations, especially on X11.

wysota
6th September 2009, 17:24
Do those polygons change in any way or do they remain constant all the time? How about using QGraphicsView instead of pure painter? Do you redraw all of the polygons in every paint event or only those that actually need redrawing? Do you have any optimizations in your code (like culling) or do you simply use the "painter's algorithm" (it's very far from being optimal)?

lni
7th September 2009, 02:16
Try painting it to an off-screen bitmap, then blit that bitmap to the screen. Some drivers may have bad performance with certain drawing operations, especially on X11.

That is what I am thinking now. But it needs a lot of work. In addition, the view can be zoom in and details would be lost.


Do those polygons change in any way or do they remain constant all the time? How about using QGraphicsView instead of pure painter? Do you redraw all of the polygons in every paint event or only those that actually need redrawing? Do you have any optimizations in your code (like culling) or do you simply use the "painter's algorithm" (it's very far from being optimal)?

Paint event is controlled by Qt, and redraw will be made when QGraphicsItem::paint is called. I do not do any culling so all polygons will be plotted. I am using QPainter class. How do I use QGraphicsView in replace of QPainter?

Thanks.

wysota
7th September 2009, 02:20
Paint event is controlled by Qt, and redraw will be made when QGraphicsItem::paint is called.

How do I use QGraphicsView in replace of QPainter?
So are you using graphics view or not?

lni
7th September 2009, 02:47
So are you using graphics view or not?

Yes, I am using QGraphicsView and QGraphicsScene. The scene contains 5000 polygons.

wysota
7th September 2009, 05:21
Did you enable caching for the items? Also how did you define shape() and boundingRect() for your items? Did you make a QGLWidget the viewport of your view to enable hardware accelleration? To make things faster you can also mark the view as non-interactive, if you can afford it.

lni
8th September 2009, 15:48
Did you enable caching for the items? Also how did you define shape() and boundingRect() for your items? Did you make a QGLWidget the viewport of your view to enable hardware accelleration? To make things faster you can also mark the view as non-interactive, if you can afford it.

I didn't call setCacheMode so it is using NoCache. The graphics is zoomable and the xy scale can be changed interactively, so I suppose caching is not useful.

The shape is not defined, and the bundingRect is properly computed. I don't use QGLWidget and have never used it. Does it work for both Linux and Window?

I don't understand "mark the view as non-interactive". My graphics display is very interactive, such as zoom, scale changes, object selection, pen and brush can also be edited...

When zoom in to small portion, it is fast because I recompute all sub-boundingRect so only render the portion of object really on screen. But not on "view all" mode.

scascio
14th September 2009, 11:53
Interactive means thats your QGraphicsView is read only.

Another advice for your problem :
if you overload the paint method of your QGraphicsItem, you will find a QStyleOptionGraphicsItem as argument

It stores the rect that need to be repaint, according to the ViewportUpdateMode of your view
and the lod, that is the scale factor of your scene.

Since the painter draw all edges of yours polygons, even if reduced all to one pixel, maybe you need to simplify it before displaying it

S.Cascio