img: rename lab_img_cache to lab_img_data

This commit is contained in:
Consolatis 2025-01-05 17:26:32 +01:00
parent 7364de2ac5
commit 5d287ebda4
2 changed files with 32 additions and 36 deletions

View file

@ -7,8 +7,6 @@
#include <stdint.h> #include <stdint.h>
#include <wayland-util.h> #include <wayland-util.h>
struct lab_img_cache;
enum lab_img_type { enum lab_img_type {
LAB_IMG_PNG, LAB_IMG_PNG,
LAB_IMG_SVG, LAB_IMG_SVG,
@ -18,7 +16,7 @@ enum lab_img_type {
struct lab_img { struct lab_img {
struct wl_array modifiers; /* lab_img_modifier_func_t */ struct wl_array modifiers; /* lab_img_modifier_func_t */
struct lab_img_cache *cache; struct lab_img_data *data;
}; };
struct lab_img *lab_img_load(enum lab_img_type type, const char *path, struct lab_img *lab_img_load(enum lab_img_type type, const char *path,

View file

@ -19,9 +19,9 @@
#include "labwc.h" #include "labwc.h"
#include "theme.h" #include "theme.h"
struct lab_img_cache { struct lab_img_data {
enum lab_img_type type; enum lab_img_type type;
/* lab_img_cache is refcounted to be shared by multiple lab_imgs */ /* lab_img_data is refcounted to be shared by multiple lab_imgs */
int refcount; int refcount;
/* Handler for the loaded image file */ /* Handler for the loaded image file */
@ -32,11 +32,11 @@ struct lab_img_cache {
}; };
static struct lab_img * static struct lab_img *
create_img(struct lab_img_cache *cache) create_img(struct lab_img_data *img_data)
{ {
struct lab_img *img = znew(*img); struct lab_img *img = znew(*img);
img->cache = cache; img->data = img_data;
cache->refcount++; img_data->refcount++;
wl_array_init(&img->modifiers); wl_array_init(&img->modifiers);
return img; return img;
} }
@ -48,36 +48,36 @@ lab_img_load(enum lab_img_type type, const char *path, float *xbm_color)
return NULL; return NULL;
} }
struct lab_img_cache *cache = znew(*cache); struct lab_img_data *img_data = znew(*img_data);
cache->type = type; img_data->type = type;
switch (type) { switch (type) {
case LAB_IMG_PNG: case LAB_IMG_PNG:
cache->buffer = img_png_load(path); img_data->buffer = img_png_load(path);
break; break;
case LAB_IMG_XBM: case LAB_IMG_XBM:
assert(xbm_color); assert(xbm_color);
cache->buffer = img_xbm_load(path, xbm_color); img_data->buffer = img_xbm_load(path, xbm_color);
break; break;
case LAB_IMG_XPM: case LAB_IMG_XPM:
cache->buffer = img_xpm_load(path); img_data->buffer = img_xpm_load(path);
break; break;
case LAB_IMG_SVG: case LAB_IMG_SVG:
#if HAVE_RSVG #if HAVE_RSVG
cache->svg = img_svg_load(path); img_data->svg = img_svg_load(path);
#endif #endif
break; break;
} }
bool img_is_loaded = (bool)cache->buffer; bool img_is_loaded = (bool)img_data->buffer;
#if HAVE_RSVG #if HAVE_RSVG
img_is_loaded |= (bool)cache->svg; img_is_loaded |= (bool)img_data->svg;
#endif #endif
if (img_is_loaded) { if (img_is_loaded) {
return create_img(cache); return create_img(img_data);
} else { } else {
free(cache); free(img_data);
return NULL; return NULL;
} }
} }
@ -90,17 +90,17 @@ lab_img_load_from_bitmap(const char *bitmap, float *rgba)
return NULL; return NULL;
} }
struct lab_img_cache *cache = znew(*cache); struct lab_img_data *img_data = znew(*img_data);
cache->type = LAB_IMG_XBM; img_data->type = LAB_IMG_XBM;
cache->buffer = buffer; img_data->buffer = buffer;
return create_img(cache); return create_img(img_data);
} }
struct lab_img * struct lab_img *
lab_img_copy(struct lab_img *img) lab_img_copy(struct lab_img *img)
{ {
struct lab_img *new_img = create_img(img->cache); struct lab_img *new_img = create_img(img->data);
wl_array_copy(&new_img->modifiers, &img->modifiers); wl_array_copy(&new_img->modifiers, &img->modifiers);
return new_img; return new_img;
} }
@ -157,16 +157,16 @@ lab_img_render(struct lab_img *img, int width, int height, int padding,
struct lab_data_buffer *buffer = NULL; struct lab_data_buffer *buffer = NULL;
/* Render the image into the buffer for the given size */ /* Render the image into the buffer for the given size */
switch (img->cache->type) { switch (img->data->type) {
case LAB_IMG_PNG: case LAB_IMG_PNG:
case LAB_IMG_XBM: case LAB_IMG_XBM:
case LAB_IMG_XPM: case LAB_IMG_XPM:
buffer = render_cairo_surface(img->cache->buffer->surface, buffer = render_cairo_surface(img->data->buffer->surface,
width, height, padding, scale); width, height, padding, scale);
break; break;
#if HAVE_RSVG #if HAVE_RSVG
case LAB_IMG_SVG: case LAB_IMG_SVG:
buffer = img_svg_render(img->cache->svg, width, height, buffer = img_svg_render(img->data->svg, width, height,
padding, scale); padding, scale);
break; break;
#endif #endif
@ -200,19 +200,17 @@ lab_img_destroy(struct lab_img *img)
return; return;
} }
struct lab_img_cache *cache = img->cache; img->data->refcount--;
cache->refcount--; if (img->data->refcount == 0) {
if (img->data->buffer) {
if (cache->refcount == 0) { wlr_buffer_drop(&img->data->buffer->base);
if (cache->buffer) {
wlr_buffer_drop(&cache->buffer->base);
} }
#if HAVE_RSVG #if HAVE_RSVG
if (cache->svg) { if (img->data->svg) {
g_object_unref(cache->svg); g_object_unref(img->data->svg);
} }
#endif #endif
free(cache); free(img->data);
} }
wl_array_release(&img->modifiers); wl_array_release(&img->modifiers);
@ -225,7 +223,7 @@ lab_img_equal(struct lab_img *img_a, struct lab_img *img_b)
if (img_a == img_b) { if (img_a == img_b) {
return true; return true;
} }
if (!img_a || !img_b || img_a->cache != img_b->cache if (!img_a || !img_b || img_a->data != img_b->data
|| img_a->modifiers.size != img_b->modifiers.size) { || img_a->modifiers.size != img_b->modifiers.size) {
return false; return false;
} }