From 84984274b69af87fac5d8dbdb47c94e1a225cdec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=98yvind=20Kol=C3=A5s?= <pippin@gimp.org>
Date: Sat, 16 Sep 2023 20:42:11 +0200
Subject: [PATCH] st3m_gfx: add spinlock for overlay ctx

The previous spinlock was for the framebuffer which was insufficient on its
own.
---
 components/st3m/st3m_gfx.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/components/st3m/st3m_gfx.c b/components/st3m/st3m_gfx.c
index c4828ab004..c1aa54cb6f 100644
--- a/components/st3m/st3m_gfx.c
+++ b/components/st3m/st3m_gfx.c
@@ -71,7 +71,9 @@ static Ctx *fb_RGB332_ctx = NULL;
 static Ctx *fb_RGBA8_ctx = NULL;
 static Ctx *fb_RGB8_ctx = NULL;
 
+#define ST3M_OSD_LOCK_TIMEOUT 50000  // ~5ms bit more due to overhead
 static int st3m_osd_lock = 0;
+static int st3m_osd_ctx_lock = 0;
 
 typedef struct {
     Ctx *user_ctx;
@@ -181,7 +183,11 @@ void st3m_gfx_start_frame(Ctx *ctx) {
 Ctx *st3m_gfx_ctx(st3m_gfx_mode mode) {
     Ctx *ctx = st3m_gfx_ctx_int(mode);
     if (mode == st3m_gfx_osd) {
-        st3m_osd_lock = 20000;
+        while (st3m_osd_ctx_lock > 0) {
+            st3m_osd_ctx_lock--;
+            usleep(1);
+        }
+        st3m_osd_lock = ST3M_OSD_LOCK_TIMEOUT;
     }
     st3m_gfx_start_frame(ctx);
     return ctx;
@@ -443,9 +449,11 @@ static void st3m_gfx_task(void *_arg) {
                     st3m_osd_lock--;
                     usleep(1);
                 }
+                st3m_osd_ctx_lock = ST3M_OSD_LOCK_TIMEOUT;
                 flow3r_bsp_display_send_fb_osd(
                     user_fb, bits, scale, osd_fb, drawlist->osd_x0,
                     drawlist->osd_y0, drawlist->osd_x1, drawlist->osd_y1);
+                st3m_osd_ctx_lock = 0;
             } else {
                 flow3r_bsp_display_send_fb_osd(user_fb, bits, scale, NULL, 0, 0,
                                                0, 0);
-- 
GitLab