[cairo] I don't see the pattern

Gerdus van Zyl gerdusvanzyl at gmail.com
Thu Dec 6 03:33:46 PST 2007


See this snippet from my rendering code that deals with caching: This
is the best way to do caching as far as I know. ~Gerdus

def _renderone(c,ctx):
	if c._dirty or c._stateid not in c.parent._childcache:
		ts = ctx.get_target()
		childsurface = ts.create_similar(cairo.CONTENT_COLOR_ALPHA,c.width,c.height)
		Rctx = cairo.Context(childsurface)
		
		c.renderctx(Rctx)
		
		c.children._zorder()
		for child in c.children:
			self._renderone(child,[],Rctx)
		
		c.parent._childcache[c._stateid] = childsurface
		c._dirty = False
	else:
		childsurface = c.parent._childcache[c._stateid]
		#print "cachehit",c,c._stateid
	
	
	ctx.set_source_surface(childsurface,c.x,c.y)
	ctx.paint()

On Dec 6, 2007 12:29 PM, Donn <donn.ingle at gmail.com> wrote:
> Hi all,
>  I have been trying to figure out (what and) how patterns work. This is the
> sort of code I've been using:
>
>         cr.push_group()
>         # Create two rects
>         cr.set_source_rgb(1,0,0)
>         cr.rectangle(0,0,50,50)
>         cr.fill()
>         cr.set_source_rgb(1,1,0)
>         cr.rectangle(0,50,50,50)
>         cr.fill()
>         #  I want them to be the 'source' for later
>         cr.pop_group_to_source()
>
>         #cr.translate(100,100) # tried to move the paint ()
>
>         # Attempt to draw a path and fill it from the 'source' above
>         cr.move_to ( 128.0, 25.6)
>         cr.line_to ( 230.4, 230.4)
>         cr.rel_line_to ( -102.4, 0.0)
>         cr.curve_to ( 51.2, 230.4, 51.2, 128.0, 128.0, 128.0)
>         cr.close_path ()
>         #cr.fill() # to see what I just drew
>
>         cr.paint() # paints the two recs in top left: unexpected.
>
> Context:
> You see, I have my little retained mode canvas app going now and on
> each 'tick' it runs through all my objects and calls their draw() commands.
> This results in a lot of cairo.this_that_and_the_other() being run over and
> over again.
>  I don't know how to do it, but I want to lessen the burden on each draw()
> such that if it's not changed from last time ( I keep a set of properties
> like x,y and scale) then I want to "stamp" it from a cached 'source' onto the
> canvas (at it's old position) and avoid calling all the code in the draw()
> method.
>
> So, I was hoping to 'record' the paths and fills and whatnot within a draw()
> method by push_group and then keep them in a reference someplace and then pop
> them to source and 'stamp' them again, perhaps even via images of some
> kind -- is this even making sense?
>
> The code above is a test of the basic idea -- I still don't know how to
> preserve the source between loops, which creates a new context each time.
> (unless I cache bitmaps)
>
> \d
> _______________________________________________
> cairo mailing list
> cairo at cairographics.org
> http://lists.cairographics.org/mailman/listinfo/cairo
>


More information about the cairo mailing list