[cairo] problem refreshing cairo surface

Zoltán Vörös zvoros at gmail.com
Sun Nov 13 02:54:43 PST 2011



On 11/13/2011 06:47 AM, Kalle Vahlman wrote:
> I don't immediately spot the error, though there is a possible race
> condition between configure_event (which is an asynchronous event by
> nature) and trying to use the context it creates in on_load(). That
> should probably give an error though. Otherwise the principle seems
> what is generally used; have a cache of similar surface to update
> whenever and invalidate the widget for painting that cache surface
> when needed.
>
> But as a general advice, cairo contexes are not meant to be stored for
> longer periods. Just create a new one whenever doing rendering. I'm
> not sure if that is your problem but you might as well fix that first.
Thanks for the comments! I think, in the second part of your mail, you 
referred to the line

         self.cairo_ctx = cairo.Context(self.surface)

in the original code. I have taken that out, and now it looks like this

class Preview:
     def __init__(self):
         self.da = Gtk.DrawingArea()
         self.da.set_size_request(400, 600)
         self.da.connect('configure-event', self.configure_event)
         self.da.connect('draw', self.on_draw)
         self.da.show()

     def configure_event(self, da, event):
         allocation = da.get_allocation()
         self.surface = 
da.get_window().create_similar_surface(cairo.CONTENT_COLOR,
                                                                 
allocation.width,
                                                                 
allocation.height)

         cairo_ctx = cairo.Context(self.surface)
         cairo_ctx.set_source_rgb(1, 1, 1)
         cairo_ctx.paint()
         return True

     def on_draw(self, da, cairo_ctx):
         cairo_ctx.set_source_surface(self.surface, 0, 0)
         cairo_ctx.paint()
         return True

     pass

and in the function on_load I create the context as

         self.DA = Preview()
         cairo_ctx = cairo.Context(self.DA.surface)

However, this still does not solve the problem. No matter what I try to 
do on the cairo context, it is just not shown. The surface is created, I 
know that, because it appears in the vertical box (that part of the code 
was shown in my previous mail), but only with the settings that are 
given in the __init__ function. It seems that all subsequent 
modifications of the surface are just ignored...
In order to avoid race conditions, I tried to insert

         while Gtk.events_pending():
             Gtk.main_iteration()

in the code, but that achieves nothing at all. Any further comments?
Cheers,
Zoltán


More information about the cairo mailing list