[cairo] Program logic for interactive usage

Hakki Dogusan dogusanh at tr.net
Thu Mar 20 04:22:56 PDT 2008


Hi,


Thanks for reply.

Carl Worth wrote:
> On Wed, 19 Mar 2008 13:25:39 +0200, Hakki Dogusan wrote:
>> - create a stationary context in program startup
>>    mastersurface = cairo_image_surface_create(CAIRO.FORMAT_ARGB32, 
>> width, height)
>>    mastercontext = cairo_create(mastersurface)
> 
> From your description it doesn't sound like you need mastersurface to
> be an *image* surface. So your program may be more efficient if you
> instead use cairo_surface_create_similar here, (and pass to it a
> surface that's targeting your window system---perhaps by calling
> cairo_get_target after your "create a context for paint DC").
> 

I read efficieny warning about image surfaces before. So, was tried 
creating a similar surface upon program entrance without success; I was 
getting "Gdk-CRITICAL **: gdk_cairo_create: assertion `GDK_IS_DRAWABLE 
(drawable)' failed" messages.

I was thought something similar with your "perhaps by calling 
cairo_get_target after your "create a context for paint DC"" comment, 
but creating something on a temporary surface/context was scared me.

Now, using your hint, I'm delaying creation of master surface & context 
till first OnPaint event. My current skeleton is as follows:

-- frame creation: mastercontext=NULL, mastersurface=NULL
-- All drawing routines checks mastercontext for NULL
-- OnPaint:
   -- create temp context cr for DC
   -- if mastercontext == NULL
        tempsurface = cairo_get_target(cr)
        mastersurface = cairo_surface_create_similar(tempsurface, ..)
        mastercontext = cairo_create(mastersurface)
        -- do initial(delayed) master drawings
   -- put masterdrawings
      cairo_set_source_surface(cr, mastersurface, 0, 0)
      cairo_paint(cr)
   --paint temporary drawings


>> - HitTest in Mouse events
>>    for each master-shape's path:
>>      cairo_new_path(mastercontext)
>>      cairo_append_path(mastercontext, path)
>>      result = cairo_in_fill(mastercontext, x, y)
> 
> The cairo_in_fill call is correct, but fairly expensive. As an
> optimization, you might want to add some bounding-box based bailouts
> to avoid having to call cairo_in_fill when it will obviously return
> false.
> 

There are mostly rectangle paths (ie.selection indicator) involved for 
cairo_in_fill test. Will it still be expensive for them? (Sorry, I 
didn't read Cairo sources.)


>> Is my logic reasonable?
> 
> Seems fine to me. Is everything working well for you?
> 

Yes! Was working, and still working fine after changing to new skeleton.


> I hope you're having fun with cairo,
> 

I am :)


> -Carl


--
Regards,
Hakki Dogusan
http://www.dynaset.org/dogusanh/


More information about the cairo mailing list