mirror of
https://github.com/labwc/labwc.git
synced 2025-11-09 13:30:01 -05:00
buffer: reduce unnecessary painting to new cairo surfaces
Add buffer_adopt_cairo_surface(), which allows wrapping an existing cairo image surface in a struct lab_data_buffer. This is useful when loading PNGs since most will be loaded as ARGB32 already. Fix a memory leak in the non-ARGB32 PNG case, where we do still need to paint to a new image surface -- we were leaking the original surface. Eliminate an unnecessary temporary image surface in SVG loading and just render the SVG to the image surface held by the lab_data_buffer. I also cleaned up and clarified the buffer API a bit: - Add a pointer to the held cairo_surface_t (so we can still access it if there is no cairo_t). - Remove the free_on_destroy bool (it was always true). - Rename unscaled_width/height to logical_width/height and add an explanatory comment. It was unclear what "unscaled" meant. - Rename buffer_create_wrap() to buffer_create_from_data(). This is laying groundwork for some more icon fixes I am working on (making sure icons are loaded and rendered at the correct scale).
This commit is contained in:
parent
328f873db3
commit
d2b161bdf8
14 changed files with 123 additions and 84 deletions
|
|
@ -64,10 +64,20 @@ img_png_load(const char *filename, struct lab_data_buffer **buffer)
|
|||
}
|
||||
cairo_surface_flush(image);
|
||||
|
||||
double w = cairo_image_surface_get_width(image);
|
||||
double h = cairo_image_surface_get_height(image);
|
||||
*buffer = buffer_create_cairo((int)w, (int)h, 1.0, true);
|
||||
if (cairo_image_surface_get_format(image) == CAIRO_FORMAT_ARGB32) {
|
||||
/* we can wrap ARGB32 image surfaces directly */
|
||||
*buffer = buffer_adopt_cairo_surface(image);
|
||||
return;
|
||||
}
|
||||
|
||||
/* convert to ARGB32 by painting to a new surface */
|
||||
uint32_t w = cairo_image_surface_get_width(image);
|
||||
uint32_t h = cairo_image_surface_get_height(image);
|
||||
*buffer = buffer_create_cairo(w, h, 1);
|
||||
cairo_t *cairo = (*buffer)->cairo;
|
||||
cairo_set_source_surface(cairo, image, 0, 0);
|
||||
cairo_paint_with_alpha(cairo, 1.0);
|
||||
cairo_paint(cairo);
|
||||
cairo_surface_flush((*buffer)->surface);
|
||||
/* destroy original surface */
|
||||
cairo_surface_destroy(image);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,8 +39,9 @@ img_svg_load(const char *filename, struct lab_data_buffer **buffer,
|
|||
return;
|
||||
}
|
||||
|
||||
cairo_surface_t *image = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, size, size);
|
||||
cairo_t *cr = cairo_create(image);
|
||||
*buffer = buffer_create_cairo(size, size, 1.0);
|
||||
cairo_surface_t *image = (*buffer)->surface;
|
||||
cairo_t *cr = (*buffer)->cairo;
|
||||
|
||||
rsvg_handle_render_document(svg, cr, &viewport, &err);
|
||||
if (err) {
|
||||
|
|
@ -55,15 +56,11 @@ img_svg_load(const char *filename, struct lab_data_buffer **buffer,
|
|||
}
|
||||
cairo_surface_flush(image);
|
||||
|
||||
double w = cairo_image_surface_get_width(image);
|
||||
double h = cairo_image_surface_get_height(image);
|
||||
*buffer = buffer_create_cairo((int)w, (int)h, 1.0, /* free_on_destroy */ true);
|
||||
cairo_t *cairo = (*buffer)->cairo;
|
||||
cairo_set_source_surface(cairo, image, 0, 0);
|
||||
cairo_paint_with_alpha(cairo, 1.0);
|
||||
g_object_unref(svg);
|
||||
return;
|
||||
|
||||
error:
|
||||
cairo_destroy(cr);
|
||||
cairo_surface_destroy(image);
|
||||
wlr_buffer_drop(&(*buffer)->base);
|
||||
*buffer = NULL;
|
||||
g_object_unref(svg);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -266,8 +266,8 @@ img_xbm_from_bitmap(const char *bitmap, struct lab_data_buffer **buffer,
|
|||
}
|
||||
color = argb32(rgba);
|
||||
pixmap = parse_xbm_builtin(bitmap, 6);
|
||||
*buffer = buffer_create_wrap(pixmap.data, pixmap.width, pixmap.height,
|
||||
pixmap.width * 4, /* free_on_destroy */ true);
|
||||
*buffer = buffer_create_from_data(pixmap.data, pixmap.width, pixmap.height,
|
||||
pixmap.width * 4);
|
||||
}
|
||||
|
||||
void
|
||||
|
|
@ -298,9 +298,9 @@ img_xbm_load(const char *filename, struct lab_data_buffer **buffer,
|
|||
return;
|
||||
}
|
||||
|
||||
/* Create buffer with free_on_destroy being true */
|
||||
/* Create buffer */
|
||||
if (pixmap.data) {
|
||||
*buffer = buffer_create_wrap(pixmap.data, pixmap.width,
|
||||
pixmap.height, pixmap.width * 4, true);
|
||||
*buffer = buffer_create_from_data(pixmap.data, pixmap.width,
|
||||
pixmap.height, pixmap.width * 4);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -387,7 +387,7 @@ pixbuf_create_from_xpm(struct file_handle *handle)
|
|||
free(colors);
|
||||
free(name_buf);
|
||||
|
||||
return buffer_create_wrap(data, w, h, 4 * w, true);
|
||||
return buffer_create_from_data(data, w, h, 4 * w);
|
||||
|
||||
out:
|
||||
g_hash_table_destroy(color_hash);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue