create_for_data#

Creates an image surface that draws directly into a scalar you already hold, rather than into memory of its own.

my $surface = Cairo::ImageSurface->create_for_data( $data, ‘argb32’, $width, $height, $stride);

$stride is the number of bytes between the start of one row of pixels and the start of the next; Cairo::Format::stride_for_width computes the right value. $data must already be at least $height * $stride bytes long.

The scalar is NOT copied: the surface writes into its bytes in place. That makes the scalar the pixels, but it also means the scalar has to stay alive and unmodified for as long as the surface exists. Letting it go out of scope first, or assigning to it, leaves the surface drawing into memory that is no longer there. Wraps cairo_image_surface_create_for_data.