From 2e61633b328e78cc248ca239115fd273fa1c5dc0 Mon Sep 17 00:00:00 2001 From: Marius Vlad Date: Tue, 17 Feb 2026 16:08:56 +0200 Subject: [PATCH] libweston: Use a string for storing surface label This is an alternative to using set_label_func to avoid using get_label() callback. get_label() is inconvenient to set up and to call. It incurs the cost of creating the string every time it is needed. During debug logging, the string is needed much more often than it changes. The new label field simply stores the string, making it easy and cheap to use. As the trade-off, components that set the label string must re-create the string when it changes, whether it is needed or not. For the migration to the new label field, get_label_member callback is used. It will be deleted once the migration is done. Signed-off-by: Marius Vlad Signed-off-by: Pekka Paalanen --- include/libweston/libweston.h | 10 ++++++++ libweston/compositor.c | 47 ++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/include/libweston/libweston.h b/include/libweston/libweston.h index c646f2164..58d6b24d7 100644 --- a/include/libweston/libweston.h +++ b/include/libweston/libweston.h @@ -1976,6 +1976,11 @@ struct weston_surface { void *committed_private; int (*get_label)(struct weston_surface *surface, char *buf, size_t len); + /** human-readable, non-unique label */ + const char *label; + /** freed when changing the label */ + char *label_dyn; + /* * Sent when the surface has been mapped and unmapped, respectively. * The data argument is the weston_surface. @@ -2402,6 +2407,11 @@ void weston_surface_set_label_func(struct weston_surface *surface, int (*desc)(struct weston_surface *, char *, size_t)); +void +weston_surface_set_label(struct weston_surface *surface, char *label); + +void +weston_surface_set_label_static(struct weston_surface *surface, const char *label); void weston_surface_get_content_size(struct weston_surface *surface, diff --git a/libweston/compositor.c b/libweston/compositor.c index 564afd03c..c4b285cd4 100644 --- a/libweston/compositor.c +++ b/libweston/compositor.c @@ -976,7 +976,7 @@ weston_surface_update_preferred_color_profile(struct weston_surface *surface) static int get_label_member(struct weston_surface *surface, char *buf, size_t len) { - return snprintf(buf, len, "(no label)"); + return snprintf(buf, len, "%s", surface->label); } WL_EXPORT struct weston_surface * @@ -1053,6 +1053,9 @@ weston_surface_create(struct weston_compositor *compositor, wl_list_init(&surface->fifo_barrier_link); + /* Set default label */ + weston_surface_set_label(surface, NULL); + return surface; } @@ -2787,6 +2790,8 @@ weston_surface_unref(struct weston_surface *surface) wl_list_remove(&surface->fifo_barrier_link); free(surface->internal_name); + + free(surface->label_dyn); free(surface); } @@ -5452,6 +5457,46 @@ weston_surface_set_label_func(struct weston_surface *surface, surface); } +static void +weston_surface_do_set_label(struct weston_surface *surface, + char *label_dyn, + const char *label_static) +{ + free(surface->label_dyn); + surface->label_dyn = label_dyn; + + if (label_static) + surface->label = label_static; + else + surface->label = "(no label)"; + + weston_timeline_refresh_subscription_objects(surface->compositor, surface); +} +/** + * Set a human-readable label on a surface, malloc'd + * + * \param surface The surface to label. + * \param label A malloc'd string. This function takes the ownership of the + * string and will free() it as necessary. Can be NULL to remove the label. + */ +WL_EXPORT void +weston_surface_set_label(struct weston_surface *surface, char *label) +{ + weston_surface_do_set_label(surface, label, label); +} + +/** + * Set a human-readable label on a surface, static + * + * \param surface The surface to label. + * \param label A static string, never free()'d. Can be NULL to remove the label. + */ +WL_EXPORT void +weston_surface_set_label_static(struct weston_surface *surface, const char *label) +{ + weston_surface_do_set_label(surface, NULL, label); +} + /** Get the size of surface contents * * \param surface The surface to query.