[OLPC-Games] [sugar] Cairo tile engine, and accessing 565 buffers from cairo and C
dhopkins at DonHopkins.com
Mon Apr 16 19:59:18 EDT 2007
Thanks for all the great explanations! That's very helpful.
I want to draw RGB tiles (alpha is not necessary) on the screen as fast
as possible, double buffering them in 16 bit X images, so I can
incrementally update the tiles, then blit them to a composition surface
in the server, then draw sprites and cairo graphics over them, them blit
the resulting image to the screen, all in 16 bits 565 in the server (or
24 bits 888 once that's supported).
I am loading in the tile set from a png file, so it comes in as a 24 bit
Currently I am making a bunch of small tiles in individual surfaces, and
drawing those individually with Cairo.
An alternative would be to keep all tiles in one surface, and translate
and clip the surface appropriately to draw each tile, which would save
memory (because of the memory fragmentation and overhead of having
hundreds of small surfaces versus one big surface). But I am afraid the
overhead of drawing the rectangle, clipping to it, and blitting the
large image through it would be slower than simply drawing a bunch of
small surfaces without any clipping. Is that a valid assumption, or is
there a fast path for drawing a surface clipped through a rectangle? I'm
writing the tile drawing code that calls cairo in C, so the Python
interpreter overhead is not an issue.
Currently the tile surfaces are 24 bit RGB surfaces, that I create by
calling create_similar on the 24 bit RGB tile set I read in from a png
But I would rather make all those tile surfaces (or the one tile set
surface that I clip) in the X server, so they will blit faster.
xHow do I create an cairo xlib surface from Python? Is there a supported
way to call cairo_xlib_surface_create from Python?
When I call the cairo.XlibSurface constructor, it throws an exception
that says the surface cannot be directly instantiated.
The only factory method I can find on cairo.XlibSurface is
create_similar, but I have no idea how to get ahold of an original
cairo.XlibSurface instance to call create_similar on.
The closest thing I can find is a C method on the gdk drawable object,
called ref_cairo_surface, that does what I need, but I don't know how to
call that directly from Python, either.
Maybe I could add a special purpose method to my Python module called
RefCairoSurface that takes a PyGObject, pulls out the GObject, and calls
GDK_DRAWABLE_GET_CLASS on that to get the drawable, then calls
ref_cairo_surface on that to get the cairo surface.
Is there an easy way to get ahold of the cairo surface from a cairo
context, or from a gdk drawable, from Python?
More information about the Games