XDrawLine and optimization

Peter Harris pharris at opentext.com
Fri Mar 2 15:02:16 PST 2012


On 2012-03-02 17:03, Christopher Howard wrote:
> On 03/02/2012 12:41 PM, Adam Jackson wrote:
>> On 3/2/12 4:21 PM, Christopher Howard wrote:
>>> I don't have much (any?) Xlib programming savvy, but before diving
>>> headlong into the Programming Manual I was hoping to get some
>>> perspective. There is a particular application I use pretty often, and
>>> one of the things it does is to draw filled line graphs on the screen,
>>> which it does by calling XDrawLine() with the appropriate parameters for
>>> each data point. Looking at the code, I was curious if I could optimize
>>> this, by first drawing everything into some kind of local memory buffer
>>> and then passing that into some Xlib function all at once.
>>>
>>> However, since XDrawLine() presumably draws the lines into a buffer
>>> anyway, I'm wondering if there are any realistic gains (or any gains at
>>> all) to be had by such an approach.
>>
>> Might or might not help.  It really depends more what kind of lines
>> you're drawing - in particular what the GC state is you're using to draw
>> them and the characteristics of the surface you're drawing to - and
>> which driver you're using.  Can you give more detail?
>>
>> - ajax
>>
> 
> 
> The code is from conky.c in conky-1.8.1. Before the loop it does one
> 
> code:
> ----------
> XSetLineAttributes(display, window.gc, 1, LineSolid,
>     CapButt, JoinMiter);
> ----------

If you use a line width of 0 (not 1) it is more likely that the driver
can use hardware acceleration (although this is more likely applicable
to older drivers).

If you can use XRenderCompositeTrapezoids instead of XDrawLine, this may
be faster with newer drivers. As a bonus, XRender optionally provides
antialiasing. (A library such as Cairo http://cairographics.org/ will be
much easier to use than bare XRender).

> And each actual line draw is preceded by a call to XSetForeground (the
> color changes in different parts of the graph).

That's going to hurt if you're using a remote server. Xlib won't be able
to coalesce the lines into a single request, so the app will be using a
lot of bandwidth (36 bytes per line, if I've added it up right).

So if you're drawing a million lines (I'm not familiar with conky, but
it's not unreasonable for some applications) it uses less bandwidth to
draw it yourself and do a PutImage at the end, unless your buffer is
larger than 8 megapixels or so.

On a local server, it doesn't make as much difference.

Peter Harris
-- 
               Open Text Connectivity Solutions Group
Peter Harris                    http://connectivity.opentext.com/
Research and Development        Phone: +1 905 762 6001
pharris at opentext.com            Toll Free: 1 877 359 4866



More information about the xorg mailing list