Any way to speed up / reduce CPU usage when drawing with Cairo?

I wrote an application that Cairo uses to draw things on the screen (more precisely, on Gtk :: DrawingArea). He must constantly redraw everything. It turns out that despite the fact that the graphic drawings are very simple, the X server uses the LOTS of the processor when redrawing, and the applications are very slow. Is there any way to speed this up? Or maybe I should not use DrawingArea and some other widgets?

What I draw is a set of rectangles that the user can move by dragging them with the mouse. The entire drawing is done using on_expose_event, but as the mouse moves (when I click the button) I call queue_draw () to update the drawing.

+4
source share
4 answers

I am finally forced to use a maximum of 25 frames per second using the lock flag.

bool lock = 0; bool needs_redraw = 0; void Redraw(){ if(lock){ needs_redraw = 1; return; } //draw image to a surface needs_redraw = 0; lock = 1; Glib::signal_timeout().connect(Unlock, 20); queue_draw(); } bool Unlock(){ lock = 0; if(needs_redraw) Redraw(); return false; } void on_expose_event(something){ //copy image from surface to widget context } 

This is sample code, but this idea. It will prohibit redrawing, which will be performed more often than once in 20 ms.

0
source

Just a couple of things to check:

Is your drawing in the exposure event?

Draw your image on the surface of Cairo, and then in the exposure event, simply copy from this surface to the surface of the widget.

Do you crop and draw only the desired region?

The expose event gives you the X, Y, width, height area you want to redraw. In cairo, create a rectangle on your surface with these sizes and call clip so you don’t waste time redrawing things that don't have to be.

+9
source

Drawing is expensive, especially text drafting has become the most expensive task of the processor GUI.

The only way to speed it up is to reduce the number of elements drawn. Check if you really draw only the items that you need. The expose event gives you a rectangle. Update only this part of the widget.

Possibly cache elements in a bitmap.

For smooth scrolling, for example, this can help draw content into a bitmap, for example, 500 pixels, so in most cases you just need to copy the image and not draw at all (you usually get rectangles only 5 to 10 pixels high when scrolling )

But you need to give us more information about what you are drawing and that loading the system is the best answer.

+3
source

I found this cairo thread drawing article to solve the speed problem, maybe this helps:

http://cairographics.org/threaded_animation_with_cairo/

About high processor utilization:

Do you have the right hardware accelerated drivers installed for X?

0
source

All Articles