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:
John Lindgren 2024-10-06 01:42:54 -04:00 committed by Johan Malm
parent 328f873db3
commit d2b161bdf8
14 changed files with 123 additions and 84 deletions

View file

@ -32,21 +32,43 @@
struct lab_data_buffer {
struct wlr_buffer base;
cairo_t *cairo;
void *data;
cairo_surface_t *surface; /* optional */
cairo_t *cairo; /* optional */
void *data; /* owned by surface if surface != NULL */
uint32_t format;
size_t stride;
bool free_on_destroy;
uint32_t unscaled_width;
uint32_t unscaled_height;
/*
* The logical size of the surface in layout pixels.
* The raw pixel data may be larger or smaller.
*/
uint32_t logical_width;
uint32_t logical_height;
};
/* Create a buffer which creates a new cairo CAIRO_FORMAT_ARGB32 surface */
struct lab_data_buffer *buffer_create_cairo(uint32_t width, uint32_t height,
float scale, bool free_on_destroy);
/*
* Create a buffer which holds (and takes ownership of) an existing
* CAIRO_FORMAT_ARGB32 image surface.
*
* The logical size is set to the surface size in pixels, ignoring
* device scale. No cairo context is created.
*/
struct lab_data_buffer *buffer_adopt_cairo_surface(cairo_surface_t *surface);
/* Create a buffer which wraps a given DRM_FORMAT_ARGB8888 pointer */
struct lab_data_buffer *buffer_create_wrap(void *pixel_data, uint32_t width,
uint32_t height, uint32_t stride, bool free_on_destroy);
/*
* Create a buffer which holds a new CAIRO_FORMAT_ARGB32 image surface.
* Additionally create a cairo context for drawing to the surface.
*/
struct lab_data_buffer *buffer_create_cairo(uint32_t logical_width,
uint32_t logical_height, float scale);
/*
* Create a buffer which holds (and takes ownership of) raw pixel data
* in pre-multiplied ARGB32 format.
*
* The logical size is set to the width and height of the pixel data.
* No cairo surface or context is created.
*/
struct lab_data_buffer *buffer_create_from_data(void *pixel_data, uint32_t width,
uint32_t height, uint32_t stride);
#endif /* LABWC_BUFFER_H */