From dc4d1cf676236b26d3d726bfe307fca34b47d97a Mon Sep 17 00:00:00 2001 From: Derek Foreman Date: Tue, 5 May 2026 11:38:46 -0500 Subject: [PATCH] trace: Use generics to simplify annotation usage Use C's Generic() functionality to make all the annotation usage have a single entrypoint. Signed-off-by: Derek Foreman --- libweston/perfetto/u_perfetto.cc | 32 ++++++++++++++++++++++ libweston/perfetto/u_perfetto.h | 18 +++++++++++++ libweston/renderer-gl/gl-renderer.c | 42 ++++++++++++++--------------- libweston/renderer-gl/gl-shaders.c | 26 +++++++++--------- libweston/surface-state.c | 10 +++---- libweston/weston-trace.h | 42 +++++++++-------------------- 6 files changed, 101 insertions(+), 69 deletions(-) diff --git a/libweston/perfetto/u_perfetto.cc b/libweston/perfetto/u_perfetto.cc index b7a96fd2a..76b6921cf 100644 --- a/libweston/perfetto/u_perfetto.cc +++ b/libweston/perfetto/u_perfetto.cc @@ -227,6 +227,38 @@ util_perfetto_trace_commit_annotate_func_flow(uint64_t id, const char *name, }); } +void +perfetto_annotate_int(struct weston_debug_annotation *annots, + unsigned int count, + const char *key, + int value) +{ + annots[count].type = WESTON_DEBUG_ANNOTATION_INT_VAL; + annots[count].ivalue = value; + annots[count].key = key; +} + +void +perfetto_annotate_float(struct weston_debug_annotation *annots, + unsigned int count, + const char *key, + float value) +{ + annots[count].type = WESTON_DEBUG_ANNOTATION_FLOAT_VAL; + annots[count].fvalue = value; + annots[count].key = key; +} + +void +perfetto_annotate_string(struct weston_debug_annotation *annots, + unsigned int count, + const char *key, + const char *value) +{ + annots[count].type = WESTON_DEBUG_ANNOTATION_STR_VAL; + annots[count].svalue = value; + annots[count].key = key; +} class UtilPerfettoObserver : public perfetto::TrackEventSessionObserver { public: diff --git a/libweston/perfetto/u_perfetto.h b/libweston/perfetto/u_perfetto.h index 4c84b7250..cb1234285 100644 --- a/libweston/perfetto/u_perfetto.h +++ b/libweston/perfetto/u_perfetto.h @@ -92,6 +92,24 @@ uint64_t util_perfetto_next_id(void); uint64_t util_perfetto_new_track(const char *name); +void +perfetto_annotate_int(struct weston_debug_annotation *annots, + unsigned int count, + const char *key, + int value); + +void +perfetto_annotate_float(struct weston_debug_annotation *annots, + unsigned int count, + const char *key, + float value); + +void +perfetto_annotate_string(struct weston_debug_annotation *annots, + unsigned int count, + const char *key, + const char *value); + #else /* HAVE_PERFETTO */ static inline void diff --git a/libweston/renderer-gl/gl-renderer.c b/libweston/renderer-gl/gl-renderer.c index 5eb45f6cd..985845602 100644 --- a/libweston/renderer-gl/gl-renderer.c +++ b/libweston/renderer-gl/gl-renderer.c @@ -436,12 +436,12 @@ gl_log_paint_node_bbox_and_region(struct gl_renderer *gr, struct weston_paint_no WESTON_TRACE_BEGIN_ANNOTATION(); - WESTON_TRACE_ANNOTATE_ADD_STR("paint node", pnode->internal_name); - WESTON_TRACE_ANNOTATE_ADD_STR("type", str); - WESTON_TRACE_ANNOTATE_ADD_INT("x", box_x); - WESTON_TRACE_ANNOTATE_ADD_INT("y", box_y); - WESTON_TRACE_ANNOTATE_ADD_INT("box_width", box_width); - WESTON_TRACE_ANNOTATE_ADD_INT("box_height", box_height); + WESTON_TRACE_ANNOTATE_ADD("paint node", pnode->internal_name); + WESTON_TRACE_ANNOTATE_ADD("type", str); + WESTON_TRACE_ANNOTATE_ADD("x", box_x); + WESTON_TRACE_ANNOTATE_ADD("y", box_y); + WESTON_TRACE_ANNOTATE_ADD("box_width", box_width); + WESTON_TRACE_ANNOTATE_ADD("box_height", box_height); WESTON_TRACE_COMMIT_ANNOTATION(&pnode->flow_id); @@ -2364,15 +2364,15 @@ set_blend_state(struct gl_renderer *gr, struct weston_paint_node *pnode, bool st if (state) { glEnable(GL_BLEND); gl_log_paint_node(gr, "\t\tblending enabled\n"); - WESTON_TRACE_ANNOTATE_ADD_STR("blending", "enabled"); + WESTON_TRACE_ANNOTATE_ADD("blending", "enabled"); } else { glDisable(GL_BLEND); gl_log_paint_node(gr, "\t\tblending disabled\n"); - WESTON_TRACE_ANNOTATE_ADD_STR("blending", "disabled"); + WESTON_TRACE_ANNOTATE_ADD("blending", "disabled"); } if (pnode) { - WESTON_TRACE_ANNOTATE_ADD_STR("paint node", pnode->internal_name); + WESTON_TRACE_ANNOTATE_ADD("paint node", pnode->internal_name); WESTON_TRACE_COMMIT_ANNOTATION(&pnode->flow_id); } else { WESTON_TRACE_COMMIT_ANNOTATION(NULL); @@ -2546,7 +2546,7 @@ apply_color_effect(struct gl_renderer *gr, struct weston_paint_node *pnode, stru WESTON_TRACE_FUNC_FLOW(&pnode->flow_id); WESTON_TRACE_BEGIN_ANNOTATION(); - WESTON_TRACE_ANNOTATE_ADD_STR("paint node", pnode->internal_name); + WESTON_TRACE_ANNOTATE_ADD("paint node", pnode->internal_name); weston_assert_f32_eq(compositor, a, 1.0f); @@ -2556,7 +2556,7 @@ apply_color_effect(struct gl_renderer *gr, struct weston_paint_node *pnode, stru *g = 1.0f - *g; *b = 1.0f - *b; gl_log_paint_node(gr, "\t\tcolor effect: inversion\n"); - WESTON_TRACE_ANNOTATE_ADD_STR("color effect", "inversion"); + WESTON_TRACE_ANNOTATE_ADD("color effect", "inversion"); WESTON_TRACE_COMMIT_ANNOTATION(&pnode->flow_id); return; case WESTON_OUTPUT_COLOR_EFFECT_TYPE_GRAYSCALE: @@ -2564,7 +2564,7 @@ apply_color_effect(struct gl_renderer *gr, struct weston_paint_node *pnode, stru *g = *r; *b = *r; gl_log_paint_node(gr, "\t\tcolor effect: grayscale\n"); - WESTON_TRACE_ANNOTATE_ADD_STR("color effect", "greyscale"); + WESTON_TRACE_ANNOTATE_ADD("color effect", "greyscale"); WESTON_TRACE_COMMIT_ANNOTATION(&pnode->flow_id); return; case WESTON_OUTPUT_COLOR_EFFECT_TYPE_CVD_CORRECTION: @@ -2579,7 +2579,7 @@ apply_color_effect(struct gl_renderer *gr, struct weston_paint_node *pnode, stru weston_log_scope_printf(gr->paint_node_scope, "\t\tcolor effect: cvd - %s\n", weston_output_cvd_type_to_str(effect->u.cvd)); - WESTON_TRACE_ANNOTATE_ADD_STR("color effect", + WESTON_TRACE_ANNOTATE_ADD("color effect", weston_output_cvd_type_to_str(effect->u.cvd)); WESTON_TRACE_COMMIT_ANNOTATION(&pnode->flow_id); return; @@ -2646,22 +2646,22 @@ draw_paint_node(struct weston_paint_node *pnode, pixman_region32_init(&repaint); pixman_region32_intersect(&repaint, &pnode->visible, damage); - WESTON_TRACE_ANNOTATE_ADD_STR("paint node", pnode->internal_name); - WESTON_TRACE_ANNOTATE_ADD_STR("label", pnode->surface->label); - WESTON_TRACE_ANNOTATE_ADD_INT("surface id", pnode->surface->s_id); + WESTON_TRACE_ANNOTATE_ADD("paint node", pnode->internal_name); + WESTON_TRACE_ANNOTATE_ADD("label", pnode->surface->label); + WESTON_TRACE_ANNOTATE_ADD("surface id", pnode->surface->s_id); gl_log_paint_node_start(gr, pnode); if (!pixman_region32_not_empty(&repaint)) { gl_log_paint_node(gr, "\t\tskipped repaint: repaint region empty\n"); - WESTON_TRACE_ANNOTATE_ADD_STR("skipped repaint", "repaint region empty"); + WESTON_TRACE_ANNOTATE_ADD("skipped repaint", "repaint region empty"); goto out; } if (pnode->is_fully_transparent) { gl_log_paint_node(gr, "\t\tskipped repaint: paint node transparent\n"); gs->used_in_output_repaint = true; /* sort of */ - WESTON_TRACE_ANNOTATE_ADD_STR("skipped repaint", "paint node transparent"); + WESTON_TRACE_ANNOTATE_ADD("skipped repaint", "paint node transparent"); goto out; } @@ -2669,7 +2669,7 @@ draw_paint_node(struct weston_paint_node *pnode, pnode->valid_transform && (pnode->surf_xform_valid && !pnode->surf_xform.transform)) { gl_log_paint_node(gr, "\t\toptimize: using glClear\n"); - WESTON_TRACE_ANNOTATE_ADD_STR("optimization", "using glClear"); + WESTON_TRACE_ANNOTATE_ADD("optimization", "using glClear"); clear_region(gr, pnode, &repaint); gs->used_in_output_repaint = true; goto out; @@ -2677,13 +2677,13 @@ draw_paint_node(struct weston_paint_node *pnode, if (ensure_surface_buffer_is_ready(gr, gs, pnode) < 0) { gl_log_paint_node(gr, "\t\tskipped repaint: buffer not ready\n"); - WESTON_TRACE_ANNOTATE_ADD_STR("skipped repaint", "buffer not ready"); + WESTON_TRACE_ANNOTATE_ADD("skipped repaint", "buffer not ready"); goto out; } if (!gl_shader_config_init_for_paint_node(&sconf, pnode)) { gl_log_paint_node(gr, "\t\tskipped repaint: shader config failure\n"); - WESTON_TRACE_ANNOTATE_ADD_STR("skipped repaint", "shader config failure"); + WESTON_TRACE_ANNOTATE_ADD("skipped repaint", "shader config failure"); goto out; } diff --git a/libweston/renderer-gl/gl-shaders.c b/libweston/renderer-gl/gl-shaders.c index 8861a69b4..35170db98 100644 --- a/libweston/renderer-gl/gl-shaders.c +++ b/libweston/renderer-gl/gl-shaders.c @@ -856,7 +856,7 @@ gl_shader_load_config(struct gl_renderer *gr, struct weston_paint_node *pnode, WESTON_TRACE_BEGIN_ANNOTATION(); if (pnode) { - WESTON_TRACE_ANNOTATE_ADD_STR("paint node", pnode->internal_name); + WESTON_TRACE_ANNOTATE_ADD("paint node", pnode->internal_name); } glUniformMatrix4fv(shader->proj_uniform, @@ -871,10 +871,10 @@ gl_shader_load_config(struct gl_renderer *gr, struct weston_paint_node *pnode, "\t\tcolor: r: %.2f, g: %.2f, b: %.2f, a: %.2f\n", sconf->unicolor[0], sconf->unicolor[1], sconf->unicolor[2], sconf->unicolor[3]); - WESTON_TRACE_ANNOTATE_ADD_FLOAT("color r", sconf->unicolor[0]); - WESTON_TRACE_ANNOTATE_ADD_FLOAT("color g", sconf->unicolor[1]); - WESTON_TRACE_ANNOTATE_ADD_FLOAT("color b", sconf->unicolor[2]); - WESTON_TRACE_ANNOTATE_ADD_FLOAT("color a", sconf->unicolor[3]); + WESTON_TRACE_ANNOTATE_ADD("color r", sconf->unicolor[0]); + WESTON_TRACE_ANNOTATE_ADD("color g", sconf->unicolor[1]); + WESTON_TRACE_ANNOTATE_ADD("color b", sconf->unicolor[2]); + WESTON_TRACE_ANNOTATE_ADD("color a", sconf->unicolor[3]); glUniform4fv(shader->color_uniform, 1, sconf->unicolor); } if (shader->tint_uniform != -1) { @@ -882,15 +882,15 @@ gl_shader_load_config(struct gl_renderer *gr, struct weston_paint_node *pnode, "\t\ttint: r: %.2f, g: %.2f, b: %.2f, a: %.2f\n", sconf->tint[0], sconf->tint[1], sconf->tint[2], sconf->tint[3]); - WESTON_TRACE_ANNOTATE_ADD_FLOAT("tint r", sconf->tint[0]); - WESTON_TRACE_ANNOTATE_ADD_FLOAT("tint g", sconf->tint[1]); - WESTON_TRACE_ANNOTATE_ADD_FLOAT("tint b", sconf->tint[2]); - WESTON_TRACE_ANNOTATE_ADD_FLOAT("tint a", sconf->tint[3]); + WESTON_TRACE_ANNOTATE_ADD("tint r", sconf->tint[0]); + WESTON_TRACE_ANNOTATE_ADD("tint g", sconf->tint[1]); + WESTON_TRACE_ANNOTATE_ADD("tint b", sconf->tint[2]); + WESTON_TRACE_ANNOTATE_ADD("tint a", sconf->tint[3]); glUniform4fv(shader->tint_uniform, 1, sconf->tint); } weston_log_scope_printf(gr->paint_node_scope, "\t\talpha: %.2f\n", sconf->view_alpha); - WESTON_TRACE_ANNOTATE_ADD_FLOAT("alpha", sconf->view_alpha); + WESTON_TRACE_ANNOTATE_ADD("alpha", sconf->view_alpha); glUniform1f(shader->view_alpha_uniform, sconf->view_alpha); assert(sconf->input_num <= SHADER_INPUT_TEX_MAX); @@ -935,11 +935,11 @@ gl_shader_load_config(struct gl_renderer *gr, struct weston_paint_node *pnode, break; case SHADER_COLOR_EFFECT_INVERSION: weston_log_scope_printf(gr->paint_node_scope, "\t\tcolor effect: inversion\n"); - WESTON_TRACE_ANNOTATE_ADD_STR("color effect", "inversion"); + WESTON_TRACE_ANNOTATE_ADD("color effect", "inversion"); break; case SHADER_COLOR_EFFECT_GRAYSCALE: weston_log_scope_printf(gr->paint_node_scope, "\t\tcolor effect: grayscale\n"); - WESTON_TRACE_ANNOTATE_ADD_STR("color effect", "greyscale"); + WESTON_TRACE_ANNOTATE_ADD("color effect", "greyscale"); break; case SHADER_COLOR_EFFECT_CVD_CORRECTION: weston_assert_int_ne(gr->compositor, shader->cvd_correction_uniform, -1); @@ -948,7 +948,7 @@ gl_shader_load_config(struct gl_renderer *gr, struct weston_paint_node *pnode, glUniformMatrix3fv(shader->cvd_correction_uniform, 1, GL_FALSE, sconf->color_effect.cvd.correction.colmaj); - WESTON_TRACE_ANNOTATE_ADD_STR("color effect", + WESTON_TRACE_ANNOTATE_ADD("color effect", weston_output_cvd_type_to_str(sconf->color_effect.cvd)); break; } diff --git a/libweston/surface-state.c b/libweston/surface-state.c index 1513655d4..a05496887 100644 --- a/libweston/surface-state.c +++ b/libweston/surface-state.c @@ -206,11 +206,11 @@ weston_surface_attach(struct weston_surface *surface, WESTON_TRACE_BEGIN_ANNOTATION(); if (buffer) { - WESTON_TRACE_ANNOTATE_ADD_STR("surface", surface->internal_name); - WESTON_TRACE_ANNOTATE_ADD_STR("format", buffer->pixel_format->drm_format_name); - WESTON_TRACE_ANNOTATE_ADD_STR("modifier", buffer->format_modifier_name); - WESTON_TRACE_ANNOTATE_ADD_INT("width", buffer->width); - WESTON_TRACE_ANNOTATE_ADD_INT("height", buffer->height); + WESTON_TRACE_ANNOTATE_ADD("surface", surface->internal_name); + WESTON_TRACE_ANNOTATE_ADD("format", buffer->pixel_format->drm_format_name); + WESTON_TRACE_ANNOTATE_ADD("modifier", buffer->format_modifier_name); + WESTON_TRACE_ANNOTATE_ADD("width", buffer->width); + WESTON_TRACE_ANNOTATE_ADD("height", buffer->height); } WESTON_TRACE_ANNOTATE_FUNC_FLOW(&surface->flow_id); diff --git a/libweston/weston-trace.h b/libweston/weston-trace.h index b2ba55492..49c207725 100644 --- a/libweston/weston-trace.h +++ b/libweston/weston-trace.h @@ -77,26 +77,16 @@ struct weston_debug_annotation __pd_annots[WESTON_MAX_DEBUG_ANNOTS]; \ unsigned int __pd_i = 0 -#define _WESTON_TRACE_ANNOTATE_ADD_INT(k, v) \ +#define _WESTON_TRACE_ANNOTATE_ADD(k, v) \ weston_assert_u32_gt(NULL, WESTON_MAX_DEBUG_ANNOTS, __pd_i); \ - __pd_annots[__pd_i].type = WESTON_DEBUG_ANNOTATION_INT_VAL; \ - __pd_annots[__pd_i].ivalue = v; \ - __pd_annots[__pd_i].key = k; \ - __pd_i++ - -#define _WESTON_TRACE_ANNOTATE_ADD_FLOAT(k, v) \ - weston_assert_u32_gt(NULL, WESTON_MAX_DEBUG_ANNOTS, __pd_i); \ - __pd_annots[__pd_i].type = WESTON_DEBUG_ANNOTATION_FLOAT_VAL; \ - __pd_annots[__pd_i].fvalue = v; \ - __pd_annots[__pd_i].key = k; \ - __pd_i++ - -#define _WESTON_TRACE_ANNOTATE_ADD_STR(k, v) \ - weston_assert_u32_gt(NULL, WESTON_MAX_DEBUG_ANNOTS, __pd_i); \ - __pd_annots[__pd_i].type = WESTON_DEBUG_ANNOTATION_STR_VAL; \ - __pd_annots[__pd_i].svalue = v; \ - __pd_annots[__pd_i].key = k; \ - __pd_i++ + _Generic((v), \ + int: perfetto_annotate_int, \ + unsigned int: perfetto_annotate_int, \ + float: perfetto_annotate_float, \ + char *: perfetto_annotate_string, \ + const char *: perfetto_annotate_string \ + ) (__pd_annots, __pd_i, k, v); \ + __pd_i++; #define _WESTON_TRACE_COMMIT_ANNOTATION(id, name) \ do { \ @@ -240,9 +230,7 @@ _weston_trace_scope_end(int *scope) #define _WESTON_TRACE_BEGIN_ANNOTATION() #define _WESTON_TRACE_COMMIT_ANNOTATION(id, name) -#define _WESTON_TRACE_ANNOTATE_ADD_INT(k, v) -#define _WESTON_TRACE_ANNOTATE_ADD_FLOAT(k, v) -#define _WESTON_TRACE_ANNOTATE_ADD_STR(k, v) +#define _WESTON_TRACE_ANNOTATE_ADD(k, v) #define _WESTON_TRACE_ANNOTATE_FUNC() #define _WESTON_TRACE_ANNOTATE_FUNC_FLOW(id, name) @@ -261,14 +249,8 @@ _weston_trace_scope_end(int *scope) #define WESTON_TRACE_BEGIN_ANNOTATION() \ _WESTON_TRACE_BEGIN_ANNOTATION() -#define WESTON_TRACE_ANNOTATE_ADD_INT(k, v) \ - _WESTON_TRACE_ANNOTATE_ADD_INT(k, v) - -#define WESTON_TRACE_ANNOTATE_ADD_FLOAT(k, v) \ - _WESTON_TRACE_ANNOTATE_ADD_FLOAT(k, v) - -#define WESTON_TRACE_ANNOTATE_ADD_STR(k, v) \ - _WESTON_TRACE_ANNOTATE_ADD_STR(k, v) +#define WESTON_TRACE_ANNOTATE_ADD(k, v) \ + _WESTON_TRACE_ANNOTATE_ADD(k, v) #define WESTON_TRACE_COMMIT_ANNOTATION(id) \ _WESTON_TRACE_COMMIT_ANNOTATION(id, __func__)