Skip to content

Commit e00b341

Browse files
committed
GFX/DISPLAY: Move gfx_display_draw_bg coords to caller stack
The video_coords struct filled by gfx_display_draw_bg is stored in the caller's draw struct and read after the function returns, so it must outlive the call. Previously this was a function-scope static, which worked but was shared mutable state — racy under threaded video when multiple draws overlap. Make coords a parameter, and have each caller declare it as a stack local alongside its existing gfx_display_ctx_draw_t draw. Per-frame, per-caller ownership: no dangling pointer, no shared state. Resolves the thread-safety concern from 0abe6dc without the use-after-return bug that 904c384 reverted.
1 parent fae7936 commit e00b341

4 files changed

Lines changed: 19 additions & 12 deletions

File tree

gfx/gfx_display.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,14 @@ void gfx_display_draw_text(
458458
void gfx_display_draw_bg(
459459
gfx_display_t *p_disp,
460460
gfx_display_ctx_draw_t *draw,
461+
struct video_coords *coords,
461462
void *userdata, bool add_opacity_to_wallpaper,
462463
float override_opacity)
463464
{
464-
static struct video_coords coords;
465465
const float *new_vertex = NULL;
466466
const float *new_tex_coord = NULL;
467467
gfx_display_ctx_driver_t *dispctx = p_disp->dispctx;
468-
if (!dispctx || !draw)
468+
if (!dispctx || !draw || !coords)
469469
return;
470470

471471
if (draw->vertex)
@@ -478,13 +478,13 @@ void gfx_display_draw_bg(
478478
else if (dispctx->get_default_tex_coords)
479479
new_tex_coord = dispctx->get_default_tex_coords();
480480

481-
coords.vertices = (unsigned)draw->vertex_count;
482-
coords.vertex = new_vertex;
483-
coords.tex_coord = new_tex_coord;
484-
coords.lut_tex_coord = new_tex_coord;
485-
coords.color = (const float*)draw->color;
481+
coords->vertices = (unsigned)draw->vertex_count;
482+
coords->vertex = new_vertex;
483+
coords->tex_coord = new_tex_coord;
484+
coords->lut_tex_coord = new_tex_coord;
485+
coords->color = (const float*)draw->color;
486486

487-
draw->coords = &coords;
487+
draw->coords = coords;
488488
draw->scale_factor = 1.0f;
489489
draw->rotation = 0.0f;
490490

gfx/gfx_display.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,13 @@ void gfx_display_draw_keyboard(
247247
char *grid[], unsigned id,
248248
unsigned text_color);
249249

250+
/* Note: coords must outlive the call — its address is stored in
251+
* draw->coords and read by the caller's subsequent dispctx->draw().
252+
* Callers should declare coords as a stack local alongside draw. */
250253
void gfx_display_draw_bg(
251254
gfx_display_t *p_disp,
252255
gfx_display_ctx_draw_t *draw,
256+
struct video_coords *coords,
253257
void *userdata,
254258
bool add_opacity, float opacity_override);
255259

menu/drivers/materialui.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6211,9 +6211,10 @@ static void materialui_render_background(
62116211
/* Draw background */
62126212
if (dispctx)
62136213
{
6214+
struct video_coords coords;
62146215
if (dispctx->blend_begin)
62156216
dispctx->blend_begin(userdata);
6216-
gfx_display_draw_bg(p_disp, &draw, userdata,
6217+
gfx_display_draw_bg(p_disp, &draw, &coords, userdata,
62176218
add_opacity, opacity_override);
62186219
if (dispctx->draw)
62196220
if (draw.height > 0 && draw.width > 0)

menu/drivers/xmb.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7372,6 +7372,7 @@ static void xmb_draw_bg(
73727372
float *coord_white)
73737373
{
73747374
gfx_display_ctx_draw_t draw;
7375+
struct video_coords coords;
73757376

73767377
draw.x = 0;
73777378
draw.y = 0;
@@ -7400,7 +7401,7 @@ static void xmb_draw_bg(
74007401
draw.color = &coord_white[0];
74017402

74027403
gfx_display_set_alpha(draw.color, coord_white[3]);
7403-
gfx_display_draw_bg(p_disp, &draw, userdata, true, menu_wallpaper_opacity);
7404+
gfx_display_draw_bg(p_disp, &draw, &coords, userdata, true, menu_wallpaper_opacity);
74047405

74057406
if (dispctx->draw)
74067407
dispctx->draw(&draw, userdata, video_width, video_height);
@@ -7412,7 +7413,7 @@ static void xmb_draw_bg(
74127413
draw.texture = 0;
74137414

74147415
gfx_display_set_alpha(draw.color, coord_white[3]);
7415-
gfx_display_draw_bg(p_disp, &draw, userdata, true, alpha);
7416+
gfx_display_draw_bg(p_disp, &draw, &coords, userdata, true, alpha);
74167417

74177418
if (dispctx->draw)
74187419
dispctx->draw(&draw, userdata, video_width, video_height);
@@ -7473,6 +7474,7 @@ static void xmb_draw_dark_layer(
74737474
float alpha)
74747475
{
74757476
gfx_display_ctx_draw_t draw;
7477+
struct video_coords coords;
74767478
float black[16] = {
74777479
0, 0, 0, 1,
74787480
0, 0, 0, 1,
@@ -7496,7 +7498,7 @@ static void xmb_draw_dark_layer(
74967498

74977499
if (dispctx->blend_begin)
74987500
dispctx->blend_begin(userdata);
7499-
gfx_display_draw_bg(p_disp, &draw, userdata, true, MIN(xmb->alpha, alpha));
7501+
gfx_display_draw_bg(p_disp, &draw, &coords, userdata, true, MIN(xmb->alpha, alpha));
75007502
if (draw.height > 0 && draw.width > 0)
75017503
if (dispctx && dispctx->draw)
75027504
dispctx->draw(&draw, userdata, width, height);

0 commit comments

Comments
 (0)