This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-10-quiet-head
in repository terminology.

View the commit online.

commit f7ffbe2b4c6edaa88af15e098cce900bf9b797e5
Author: Boris Faure <[email protected]>
AuthorDate: Fri Oct 30 15:14:35 2026 +0000

    colors: flush the truecolour cache when the palette changes
    
    colors_rgb_to_palette() approximates a truecolour by the closest entry
    of the live extended palette, and caches the answers. The cache was
    process-global and never invalidated, so it went stale across colour
    scheme changes, across windows with different schemes, or once OSC 4
    can rewrite the palette.
    
    The cache remembers which Termpty and which palette generation filled
    it, and flushes when either differs. termio_palette_color_set() and
    termio_palette_apply() bump the generation through
    termpty_palette_changed().
    
    With one active terminal nothing changes; when terminals alternate, the
    cache does extra work instead of giving wrong answers. This also fixes
    the staleness after a scheme switch.
---
 src/bin/colors.c          | 56 ++++++++++++++++++++++++++++++++++++++++++-----
 src/bin/colors.h          |  7 +++---
 src/bin/termio.c          |  2 ++
 src/bin/termiointernals.c |  2 ++
 src/bin/termpty.c         | 11 ++++++++++
 src/bin/termpty.h         |  4 ++++
 src/bin/tytest.c          |  1 +
 src/bin/unit_tests.h      |  1 +
 8 files changed, 75 insertions(+), 9 deletions(-)

diff --git a/src/bin/colors.c b/src/bin/colors.c
index b57aa2ac..c234c732 100644
--- a/src/bin/colors.c
+++ b/src/bin/colors.c
@@ -1109,6 +1109,9 @@ colors_shutdown(void)
 #define TCC_PRIME 17 /* smallest prime number larger than @TCC_LEN/2 */
 static struct {
      uint32_t colors[TCC_LEN];
+     /* answers for one terminal's palette generation */
+     const Termpty *owner;
+     unsigned int generation;
 } _truecolor_cache;
 static int _tcc_random_pos = 0;
 
@@ -1148,11 +1151,13 @@ _tcc_insert(const uint32_t color_msb, const uint8_t approximated)
 #endif
 
 uint8_t
-colors_rgb_to_palette(Evas_Object *textgrid, uint8_t r0, uint8_t g0, uint8_t b0)
+colors_rgb_to_palette(Evas_Object *textgrid, const Termpty *ty,
+                      uint8_t r0, uint8_t g0, uint8_t b0)
 {
    uint8_t chosen_color = COL_DEF;
 #if defined(BINARY_TYFUZZ)
    (void) textgrid;
+   (void) ty;
    (void) r0;
    (void) g0;
    (void) b0;
@@ -1164,6 +1169,14 @@ colors_rgb_to_palette(Evas_Object *textgrid, uint8_t r0, uint8_t g0, uint8_t b0)
       | (((uint32_t)g0) << 16)
       | (((uint32_t)b0) << 8);
 
+   if (!ty || (_truecolor_cache.owner != ty) ||
+       (_truecolor_cache.generation != ty->palette_generation))
+     {
+        memset(_truecolor_cache.colors, 0, sizeof(_truecolor_cache.colors));
+        _truecolor_cache.owner = ty;
+        _truecolor_cache.generation = ty ? ty->palette_generation : 0;
+     }
+
    if (_tcc_find(color_msb, &chosen_color))
      return chosen_color;
 
@@ -1253,17 +1266,48 @@ tytest_rgb_to_palette(void)
       {  66,  33, 222,  56 },
    };
    size_t i;
+   Termpty ty;
+
+   memset(&ty, 0, sizeof(ty));
 
    for (i = 0; i < sizeof(table) / sizeof(table[0]); i++)
      {
-        uint8_t idx = colors_rgb_to_palette(NULL, table[i].r, table[i].g,
+        uint8_t idx = colors_rgb_to_palette(NULL, &ty, table[i].r, table[i].g,
                                             table[i].b);
         assert(idx == table[i].idx);
      }
-   /* Repeating one entry exercises the cache: the answer must not depend
-    * on which path (cache hit or full scan) produced it. */
-   assert(colors_rgb_to_palette(NULL, 215, 135, 95) == 173);
-   assert(colors_rgb_to_palette(NULL, 215, 135, 95) == 173);
+   /* repeating one entry exercises the cache */
+   assert(colors_rgb_to_palette(NULL, &ty, 215, 135, 95) == 173);
+   assert(colors_rgb_to_palette(NULL, &ty, 215, 135, 95) == 173);
+
+   return 0;
+}
+
+/* the cache must not answer with colours from an older palette */
+int
+tytest_truecolor_cache_generation(void)
+{
+   Termpty ty, ty2;
+
+   memset(&ty, 0, sizeof(ty));
+   memset(&ty2, 0, sizeof(ty2));
+
+   /* a shared counter: two terminals never share a generation */
+   termpty_palette_changed(&ty);
+   termpty_palette_changed(&ty2);
+   assert(ty.palette_generation != ty2.palette_generation);
+
+   assert(colors_rgb_to_palette(NULL, &ty, 215, 135, 95) == 173);
+   /* a sentinel the generation bump must flush */
+   _truecolor_cache.colors[0] = 0xdeadbeef;
+   termpty_palette_changed(&ty);
+   assert(colors_rgb_to_palette(NULL, &ty, 215, 135, 95) == 173);
+   assert(_truecolor_cache.colors[0] != 0xdeadbeef);
+
+   /* another terminal's palette does not answer either */
+   _truecolor_cache.colors[0] = 0xdeadbeef;
+   assert(colors_rgb_to_palette(NULL, NULL, 215, 135, 95) == 173);
+   assert(_truecolor_cache.colors[0] != 0xdeadbeef);
 
    return 0;
 }
diff --git a/src/bin/colors.h b/src/bin/colors.h
index ab45ea3b..c5716314 100644
--- a/src/bin/colors.h
+++ b/src/bin/colors.h
@@ -63,11 +63,12 @@ colors_palette_color_set(Evas_Object *textgrid,
                          int idx,
                          const Color *c);
 
-/* Approximate an RGB colour by the closest entry of @textgrid's extended
- * 256-colour palette, weighted redmean distance, with a small cache of
- * recent results. Under tyfuzz this always returns COL_DEF. */
+struct tag_Termpty;
+
+/* closest extended-palette entry for an RGB, cached per @ty */
 uint8_t
 colors_rgb_to_palette(Evas_Object *textgrid,
+                      const struct tag_Termpty *ty,
                       uint8_t r, uint8_t g, uint8_t b);
 void
 colors_standard_get(int set,
diff --git a/src/bin/termio.c b/src/bin/termio.c
index 3a3ba133..b5086713 100644
--- a/src/bin/termio.c
+++ b/src/bin/termio.c
@@ -230,6 +230,7 @@ termio_palette_color_set(Evas_Object *obj, int idx, int r, int g, int b)
    sd->palette.overridden[idx / 32] |= 1u << (idx % 32);
    colors_palette_color_set(sd->grid.obj, sd->config->color_scheme,
                             idx, &c);
+   termpty_palette_changed(sd->pty);
 }
 
 /* rewrites the scheme, then the application's palette overrides on top */
@@ -247,6 +248,7 @@ termio_palette_apply(Evas_Object *obj)
           colors_palette_color_set(sd->grid.obj, sd->config->color_scheme,
                                    i, &sd->palette.colors[i]);
      }
+   termpty_palette_changed(sd->pty);
 }
 
 void
diff --git a/src/bin/termiointernals.c b/src/bin/termiointernals.c
index cd24653c..844cf091 100644
--- a/src/bin/termiointernals.c
+++ b/src/bin/termiointernals.c
@@ -2707,6 +2707,7 @@ termio_internal_render(Termio *sd,
                              * but the cell keeps the true value for next
                              * time. */
                             fg = colors_rgb_to_palette(sd->grid.obj,
+                                                       sd->pty,
                                                        (fg >> 16) & 0xff,
                                                        (fg >> 8) & 0xff,
                                                        fg & 0xff);
@@ -2716,6 +2717,7 @@ termio_internal_render(Termio *sd,
                        if (bg_is_rgb && tr_missing)
                          {
                             bg = colors_rgb_to_palette(sd->grid.obj,
+                                                       sd->pty,
                                                        (bg >> 16) & 0xff,
                                                        (bg >> 8) & 0xff,
                                                        bg & 0xff);
diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 4049c6e3..3ad5fe61 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -945,6 +945,17 @@ termpty_config_update(Termpty *ty, Config *config)
    termpty_backlog_size_set(ty, config->scrollback);
 }
 
+void
+termpty_palette_changed(Termpty *ty)
+{
+   /* shared by all terminals, so a reused address never matches the cache */
+   static unsigned int generation;
+
+   if (!ty)
+     return;
+   ty->palette_generation = ++generation;
+}
+
 static Eina_Bool
 _termpty_cell_is_empty(const Termcell *cell)
 {
diff --git a/src/bin/termpty.h b/src/bin/termpty.h
index bc14cf29..56f02146 100644
--- a/src/bin/termpty.h
+++ b/src/bin/termpty.h
@@ -270,6 +270,8 @@ struct tag_Termpty
    unsigned int color_scheme_reporting : 1;
    unsigned int osc_skip : 1; /* discarding an OSC too long for the buffer */
    unsigned int osc_skip_esc : 1; /* and the last codepoint was ESC */
+   /* bumped on each palette rewrite (OSC 4); scopes the truecolour cache */
+   unsigned int palette_generation;
    struct {
        Term_Link *links;
        uint8_t *bitmap;
@@ -354,6 +356,8 @@ void       termpty_sync_output_reset(Termpty *ty);
 /* private — called from termptyesc.c _sync_output_end only */
 void       termpty_sync_output_snapshot_free(Termpty *ty);
 void       termpty_config_update(Termpty *ty, Config *config);
+/* the application rewrote the palette; invalidates the truecolour cache */
+void       termpty_palette_changed(Termpty *ty);
 
 const Termcell *termpty_cellrow_get(Termpty *ty, int y, ssize_t *wret);
 Termcell       *termpty_cellrow_get_writeable(Termpty *ty, int y, ssize_t *wret);
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index d98969e2..d025d8ae 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -101,6 +101,7 @@ static struct {
        { "osc_color_name", tytest_osc_color_name},
        { "xterm_parse_color", tytest_xterm_parse_color},
        { "palette_slots", tytest_palette_slots},
+       { "truecolor_cache_generation", tytest_truecolor_cache_generation},
        { NULL, NULL},
 };
 
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 2100f4a9..d9412619 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -70,6 +70,7 @@ int tytest_osc_color_reset(void);
 int tytest_osc_color_name(void);
 int tytest_xterm_parse_color(void);
 int tytest_palette_slots(void);
+int tytest_truecolor_cache_generation(void);
 
 #endif
 int tytest_keyin_legacy(void);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to